blob: a0c9877243bd4acb13682e2455a883dc9bea9a21 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 */
61} histentry_T;
62
63static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
64static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
65static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
66 /* identifying (unique) number of newest history entry */
67static int hislen = 0; /* actual length of history tables */
68
Bram Moolenaard25c16e2016-01-29 22:13:30 +010069static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaard25c16e2016-01-29 22:13:30 +010071static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000072# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010073static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074# endif
75#endif
76
77#ifdef FEAT_RIGHTLEFT
78static int cmd_hkmap = 0; /* Hebrew mapping during command line */
79#endif
80
81#ifdef FEAT_FKMAP
82static int cmd_fkmap = 0; /* Farsi mapping during command line */
83#endif
84
Bram Moolenaard25c16e2016-01-29 22:13:30 +010085static int cmdline_charsize(int idx);
86static void set_cmdspos(void);
87static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000088#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010089static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static void alloc_cmdbuff(int len);
92static int realloc_cmdbuff(int len);
93static void draw_cmdline(int start, int len);
94static void save_cmdline(struct cmdline_info *ccp);
95static void restore_cmdline(struct cmdline_info *ccp);
96static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
100#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100101static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmdprompt(void);
104static void cursorcmd(void);
105static int ccheck_abbr(int);
106static int nextwild(expand_T *xp, int type, int options, int escape);
107static void escape_fname(char_u **pp);
108static int showmatches(expand_T *xp, int wildmenu);
109static void set_expand_context(expand_T *xp);
110static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
111static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100113static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
114static int ExpandRTDir(char_u *pat, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200115# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100116static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200117# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100119static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
120static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121# endif
122#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200123#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100124static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127#ifdef FEAT_CMDWIN
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100128static int ex_window(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#endif
130
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200131#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
132static int
133#ifdef __BORLANDC__
134_RTLENTRYF
135#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100136sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200137#endif
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139/*
140 * getcmdline() - accept a command line starting with firstc.
141 *
142 * firstc == ':' get ":" command line.
143 * firstc == '/' or '?' get search pattern
144 * firstc == '=' get expression
145 * firstc == '@' get text for input() function
146 * firstc == '>' get text for debug mode
147 * firstc == NUL get text for :insert command
148 * firstc == -1 like NUL, and break on CTRL-C
149 *
150 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
151 * command line.
152 *
153 * Careful: getcmdline() can be called recursively!
154 *
155 * Return pointer to allocated string if there is a commandline, NULL
156 * otherwise.
157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100159getcmdline(
160 int firstc,
161 long count UNUSED, /* only used for incremental search */
162 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163{
164 int c;
165 int i;
166 int j;
167 int gotesc = FALSE; /* TRUE when <ESC> just typed */
168 int do_abbr; /* when TRUE check for abbr. */
169#ifdef FEAT_CMDHIST
170 char_u *lookfor = NULL; /* string to match */
171 int hiscnt; /* current history line in use */
172 int histype; /* history type to be used */
173#endif
174#ifdef FEAT_SEARCH_EXTRA
175 pos_T old_cursor;
176 colnr_T old_curswant;
177 colnr_T old_leftcol;
178 linenr_T old_topline;
179# ifdef FEAT_DIFF
180 int old_topfill;
181# endif
182 linenr_T old_botline;
183 int did_incsearch = FALSE;
184 int incsearch_postponed = FALSE;
185#endif
186 int did_wild_list = FALSE; /* did wild_list() recently */
187 int wim_index = 0; /* index in wim_flags[] */
188 int res;
189 int save_msg_scroll = msg_scroll;
190 int save_State = State; /* remember State when called */
191 int some_key_typed = FALSE; /* one of the keys was typed */
192#ifdef FEAT_MOUSE
193 /* mouse drag and release events are ignored, unless they are
194 * preceded with a mouse down event */
195 int ignore_drag_release = TRUE;
196#endif
197#ifdef FEAT_EVAL
198 int break_ctrl_c = FALSE;
199#endif
200 expand_T xpc;
201 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000202#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
203 /* Everything that may work recursively should save and restore the
204 * current command line in save_ccline. That includes update_screen(), a
205 * custom status line may invoke ":normal". */
206 struct cmdline_info save_ccline;
207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
209#ifdef FEAT_SNIFF
210 want_sniff_request = 0;
211#endif
212#ifdef FEAT_EVAL
213 if (firstc == -1)
214 {
215 firstc = NUL;
216 break_ctrl_c = TRUE;
217 }
218#endif
219#ifdef FEAT_RIGHTLEFT
220 /* start without Hebrew mapping for a command line */
221 if (firstc == ':' || firstc == '=' || firstc == '>')
222 cmd_hkmap = 0;
223#endif
224
225 ccline.overstrike = FALSE; /* always start in insert mode */
226#ifdef FEAT_SEARCH_EXTRA
227 old_cursor = curwin->w_cursor; /* needs to be restored later */
228 old_curswant = curwin->w_curswant;
229 old_leftcol = curwin->w_leftcol;
230 old_topline = curwin->w_topline;
231# ifdef FEAT_DIFF
232 old_topfill = curwin->w_topfill;
233# endif
234 old_botline = curwin->w_botline;
235#endif
236
237 /*
238 * set some variables for redrawcmd()
239 */
240 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000241 ccline.cmdindent = (firstc > 0 ? indent : 0);
242
243 /* alloc initial ccline.cmdbuff */
244 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 if (ccline.cmdbuff == NULL)
246 return NULL; /* out of memory */
247 ccline.cmdlen = ccline.cmdpos = 0;
248 ccline.cmdbuff[0] = NUL;
249
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000250 /* autoindent for :insert and :append */
251 if (firstc <= 0)
252 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200253 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000254 ccline.cmdbuff[indent] = NUL;
255 ccline.cmdpos = indent;
256 ccline.cmdspos = indent;
257 ccline.cmdlen = indent;
258 }
259
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000261 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263#ifdef FEAT_RIGHTLEFT
264 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
265 && (firstc == '/' || firstc == '?'))
266 cmdmsg_rl = TRUE;
267 else
268 cmdmsg_rl = FALSE;
269#endif
270
271 redir_off = TRUE; /* don't redirect the typed command */
272 if (!cmd_silent)
273 {
274 i = msg_scrolled;
275 msg_scrolled = 0; /* avoid wait_return message */
276 gotocmdline(TRUE);
277 msg_scrolled += i;
278 redrawcmdprompt(); /* draw prompt or indent */
279 set_cmdspos();
280 }
281 xpc.xp_context = EXPAND_NOTHING;
282 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000283#ifndef BACKSLASH_IN_FILENAME
284 xpc.xp_shell = FALSE;
285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000287#if defined(FEAT_EVAL)
288 if (ccline.input_fn)
289 {
290 xpc.xp_context = ccline.xp_context;
291 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000292# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000293 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000294# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000295 }
296#endif
297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 /*
299 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
300 * doing ":@0" when register 0 doesn't contain a CR.
301 */
302 msg_scroll = FALSE;
303
304 State = CMDLINE;
305
306 if (firstc == '/' || firstc == '?' || firstc == '@')
307 {
308 /* Use ":lmap" mappings for search pattern and input(). */
309 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
310 b_im_ptr = &curbuf->b_p_iminsert;
311 else
312 b_im_ptr = &curbuf->b_p_imsearch;
313 if (*b_im_ptr == B_IMODE_LMAP)
314 State |= LANGMAP;
315#ifdef USE_IM_CONTROL
316 im_set_active(*b_im_ptr == B_IMODE_IM);
317#endif
318 }
319#ifdef USE_IM_CONTROL
320 else if (p_imcmdline)
321 im_set_active(TRUE);
322#endif
323
324#ifdef FEAT_MOUSE
325 setmouse();
326#endif
327#ifdef CURSOR_SHAPE
328 ui_cursor_shape(); /* may show different cursor shape */
329#endif
330
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000331 /* When inside an autocommand for writing "exiting" may be set and
332 * terminal mode set to cooked. Need to set raw mode here then. */
333 settmode(TMODE_RAW);
334
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#ifdef FEAT_CMDHIST
336 init_history();
337 hiscnt = hislen; /* set hiscnt to impossible history value */
338 histype = hist_char2type(firstc);
339#endif
340
341#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000342 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#endif
344
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200345 /* If something above caused an error, reset the flags, we do want to type
346 * and execute commands. Display may be messed up a bit. */
347 if (did_emsg)
348 redrawcmd();
349 did_emsg = FALSE;
350 got_int = FALSE;
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 /*
353 * Collect the command string, handling editing keys.
354 */
355 for (;;)
356 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000357 redir_off = TRUE; /* Don't redirect the typed command.
358 Repeated, because a ":redir" inside
359 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#ifdef USE_ON_FLY_SCROLL
361 dont_scroll = FALSE; /* allow scrolling here */
362#endif
363 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
364
365 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000366
367 /* Get a character. Ignore K_IGNORE, it should not do anything, such
368 * as stop completion. */
369 do
370 {
371 c = safe_vgetc();
372 } while (c == K_IGNORE);
373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (KeyTyped)
375 {
376 some_key_typed = TRUE;
377#ifdef FEAT_RIGHTLEFT
378 if (cmd_hkmap)
379 c = hkmap(c);
380# ifdef FEAT_FKMAP
381 if (cmd_fkmap)
382 c = cmdl_fkmap(c);
383# endif
384 if (cmdmsg_rl && !KeyStuffed)
385 {
386 /* Invert horizontal movements and operations. Only when
387 * typed by the user directly, not when the result of a
388 * mapping. */
389 switch (c)
390 {
391 case K_RIGHT: c = K_LEFT; break;
392 case K_S_RIGHT: c = K_S_LEFT; break;
393 case K_C_RIGHT: c = K_C_LEFT; break;
394 case K_LEFT: c = K_RIGHT; break;
395 case K_S_LEFT: c = K_S_RIGHT; break;
396 case K_C_LEFT: c = K_C_RIGHT; break;
397 }
398 }
399#endif
400 }
401
402 /*
403 * Ignore got_int when CTRL-C was typed here.
404 * Don't ignore it in :global, we really need to break then, e.g., for
405 * ":g/pat/normal /pat" (without the <CR>).
406 * Don't ignore it for the input() function.
407 */
408 if ((c == Ctrl_C
409#ifdef UNIX
410 || c == intr_char
411#endif
412 )
413#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
414 && firstc != '@'
415#endif
416#ifdef FEAT_EVAL
417 && !break_ctrl_c
418#endif
419 && !global_busy)
420 got_int = FALSE;
421
422#ifdef FEAT_CMDHIST
423 /* free old command line when finished moving around in the history
424 * list */
425 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000426 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000427 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 && c != K_PAGEDOWN && c != K_PAGEUP
429 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000430 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
432 {
433 vim_free(lookfor);
434 lookfor = NULL;
435 }
436#endif
437
438 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000439 * When there are matching completions to select <S-Tab> works like
440 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000442 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 c = Ctrl_P;
444
445#ifdef FEAT_WILDMENU
446 /* Special translations for 'wildmenu' */
447 if (did_wild_list && p_wmnu)
448 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000449 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000451 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 c = Ctrl_N;
453 }
454 /* Hitting CR after "emenu Name.": complete submenu */
455 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
456 && ccline.cmdpos > 1
457 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
458 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
459 && (c == '\n' || c == '\r' || c == K_KENTER))
460 c = K_DOWN;
461#endif
462
463 /* free expanded names when finished walking through matches */
464 if (xpc.xp_numfiles != -1
465 && !(c == p_wc && KeyTyped) && c != p_wcm
466 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
467 && c != Ctrl_L)
468 {
469 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
470 did_wild_list = FALSE;
471#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000472 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#endif
474 xpc.xp_context = EXPAND_NOTHING;
475 wim_index = 0;
476#ifdef FEAT_WILDMENU
477 if (p_wmnu && wild_menu_showing != 0)
478 {
479 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000480 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000481
482 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000483 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
485 if (wild_menu_showing == WM_SCROLLED)
486 {
487 /* Entered command line, move it up */
488 cmdline_row--;
489 redrawcmd();
490 }
491 else if (save_p_ls != -1)
492 {
493 /* restore 'laststatus' and 'winminheight' */
494 p_ls = save_p_ls;
495 p_wmh = save_p_wmh;
496 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000497 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000499 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 redrawcmd();
501 save_p_ls = -1;
502 }
503 else
504 {
505# ifdef FEAT_VERTSPLIT
506 win_redraw_last_status(topframe);
507# else
508 lastwin->w_redr_status = TRUE;
509# endif
510 redraw_statuslines();
511 }
512 KeyTyped = skt;
513 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000514 if (ccline.input_fn)
515 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 }
517#endif
518 }
519
520#ifdef FEAT_WILDMENU
521 /* Special translations for 'wildmenu' */
522 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
523 {
524 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000525 if (c == K_DOWN && ccline.cmdpos > 0
526 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000528 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
530 /* Hitting <Up>: Remove one submenu name in front of the
531 * cursor */
532 int found = FALSE;
533
534 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
535 i = 0;
536 while (--j > 0)
537 {
538 /* check for start of menu name */
539 if (ccline.cmdbuff[j] == ' '
540 && ccline.cmdbuff[j - 1] != '\\')
541 {
542 i = j + 1;
543 break;
544 }
545 /* check for start of submenu name */
546 if (ccline.cmdbuff[j] == '.'
547 && ccline.cmdbuff[j - 1] != '\\')
548 {
549 if (found)
550 {
551 i = j + 1;
552 break;
553 }
554 else
555 found = TRUE;
556 }
557 }
558 if (i > 0)
559 cmdline_del(i);
560 c = p_wc;
561 xpc.xp_context = EXPAND_NOTHING;
562 }
563 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000564 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000565 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000566 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {
568 char_u upseg[5];
569
570 upseg[0] = PATHSEP;
571 upseg[1] = '.';
572 upseg[2] = '.';
573 upseg[3] = PATHSEP;
574 upseg[4] = NUL;
575
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000576 if (c == K_DOWN
577 && ccline.cmdpos > 0
578 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
579 && (ccline.cmdpos < 3
580 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
582 {
583 /* go down a directory */
584 c = p_wc;
585 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000586 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {
588 /* If in a direct ancestor, strip off one ../ to go down */
589 int found = FALSE;
590
591 j = ccline.cmdpos;
592 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
593 while (--j > i)
594 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000595#ifdef FEAT_MBYTE
596 if (has_mbyte)
597 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (vim_ispathsep(ccline.cmdbuff[j]))
600 {
601 found = TRUE;
602 break;
603 }
604 }
605 if (found
606 && ccline.cmdbuff[j - 1] == '.'
607 && ccline.cmdbuff[j - 2] == '.'
608 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
609 {
610 cmdline_del(j - 2);
611 c = p_wc;
612 }
613 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000614 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
616 /* go up a directory */
617 int found = FALSE;
618
619 j = ccline.cmdpos - 1;
620 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
621 while (--j > i)
622 {
623#ifdef FEAT_MBYTE
624 if (has_mbyte)
625 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
626#endif
627 if (vim_ispathsep(ccline.cmdbuff[j])
628#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100629 && vim_strchr((char_u *)" *?[{`$%#",
630 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#endif
632 )
633 {
634 if (found)
635 {
636 i = j + 1;
637 break;
638 }
639 else
640 found = TRUE;
641 }
642 }
643
644 if (!found)
645 j = i;
646 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
647 j += 4;
648 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
649 && j == i)
650 j += 3;
651 else
652 j = 0;
653 if (j > 0)
654 {
655 /* TODO this is only for DOS/UNIX systems - need to put in
656 * machine-specific stuff here and in upseg init */
657 cmdline_del(j);
658 put_on_cmdline(upseg + 1, 3, FALSE);
659 }
660 else if (ccline.cmdpos > i)
661 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100662
663 /* Now complete in the new directory. Set KeyTyped in case the
664 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100666 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 }
668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
670#endif /* FEAT_WILDMENU */
671
672 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
673 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
674 if (c == Ctrl_BSL)
675 {
676 ++no_mapping;
677 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000678 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 --no_mapping;
680 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200681 /* CTRL-\ e doesn't work when obtaining an expression, unless it
682 * is in a mapping. */
683 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
684 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
686 vungetc(c);
687 c = Ctrl_BSL;
688 }
689#ifdef FEAT_EVAL
690 else if (c == 'e')
691 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200692 char_u *p = NULL;
693 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
695 /*
696 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000697 * Need to save and restore the current command line, to be
698 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 */
700 if (ccline.cmdpos == ccline.cmdlen)
701 new_cmdpos = 99999; /* keep it at the end */
702 else
703 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000704
705 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000707 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (c == '=')
709 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000710 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000711 * to avoid nasty things like going to another buffer when
712 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000713 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000714 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000716 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000717 restore_cmdline(&save_ccline);
718
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200719 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200721 len = (int)STRLEN(p);
722 if (realloc_cmdbuff(len + 1) == OK)
723 {
724 ccline.cmdlen = len;
725 STRCPY(ccline.cmdbuff, p);
726 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200728 /* Restore the cursor or use the position set with
729 * set_cmdline_pos(). */
730 if (new_cmdpos > ccline.cmdlen)
731 ccline.cmdpos = ccline.cmdlen;
732 else
733 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200735 KeyTyped = FALSE; /* Don't do p_wc completion. */
736 redrawcmd();
737 goto cmdline_changed;
738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740 }
741 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100742 got_int = FALSE; /* don't abandon the command line */
743 did_emsg = FALSE;
744 emsg_on_display = FALSE;
745 redrawcmd();
746 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 }
748#endif
749 else
750 {
751 if (c == Ctrl_G && p_im && restart_edit == 0)
752 restart_edit = 'a';
753 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
754 in history */
755 goto returncmd; /* back to Normal mode */
756 }
757 }
758
759#ifdef FEAT_CMDWIN
760 if (c == cedit_key || c == K_CMDWIN)
761 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200762 if (ex_normal_busy == 0 && got_int == FALSE)
763 {
764 /*
765 * Open a window to edit the command line (and history).
766 */
767 c = ex_window();
768 some_key_typed = TRUE;
769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
771# ifdef FEAT_DIGRAPHS
772 else
773# endif
774#endif
775#ifdef FEAT_DIGRAPHS
776 c = do_digraph(c);
777#endif
778
779 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
780 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
781 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000782 /* In Ex mode a backslash escapes a newline. */
783 if (exmode_active
784 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000785 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000786 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000787 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000789 if (c == K_KENTER)
790 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000792 else
793 {
794 gotesc = FALSE; /* Might have typed ESC previously, don't
795 truncate the cmdline now. */
796 if (ccheck_abbr(c + ABBR_OFF))
797 goto cmdline_changed;
798 if (!cmd_silent)
799 {
800 windgoto(msg_row, 0);
801 out_flush();
802 }
803 break;
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806
807 /*
808 * Completion for 'wildchar' or 'wildcharm' key.
809 * - hitting <ESC> twice means: abandon command line.
810 * - wildcard expansion is only done when the 'wildchar' key is really
811 * typed, not when it comes from a macro
812 */
813 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
814 {
815 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
816 {
817 /* if 'wildmode' contains "list" may still need to list */
818 if (xpc.xp_numfiles > 1
819 && !did_wild_list
820 && (wim_flags[wim_index] & WIM_LIST))
821 {
822 (void)showmatches(&xpc, FALSE);
823 redrawcmd();
824 did_wild_list = TRUE;
825 }
826 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100827 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
828 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100830 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
831 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 else
833 res = OK; /* don't insert 'wildchar' now */
834 }
835 else /* typed p_wc first time */
836 {
837 wim_index = 0;
838 j = ccline.cmdpos;
839 /* if 'wildmode' first contains "longest", get longest
840 * common part */
841 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100842 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
843 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100845 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
846 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
848 /* if interrupted while completing, behave like it failed */
849 if (got_int)
850 {
851 (void)vpeekc(); /* remove <C-C> from input stream */
852 got_int = FALSE; /* don't abandon the command line */
853 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
854#ifdef FEAT_WILDMENU
855 xpc.xp_context = EXPAND_NOTHING;
856#endif
857 goto cmdline_changed;
858 }
859
860 /* when more than one match, and 'wildmode' first contains
861 * "list", or no change and 'wildmode' contains "longest,list",
862 * list all matches */
863 if (res == OK && xpc.xp_numfiles > 1)
864 {
865 /* a "longest" that didn't do anything is skipped (but not
866 * "list:longest") */
867 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
868 wim_index = 1;
869 if ((wim_flags[wim_index] & WIM_LIST)
870#ifdef FEAT_WILDMENU
871 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
872#endif
873 )
874 {
875 if (!(wim_flags[0] & WIM_LONGEST))
876 {
877#ifdef FEAT_WILDMENU
878 int p_wmnu_save = p_wmnu;
879 p_wmnu = 0;
880#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100881 /* remove match */
882 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#ifdef FEAT_WILDMENU
884 p_wmnu = p_wmnu_save;
885#endif
886 }
887#ifdef FEAT_WILDMENU
888 (void)showmatches(&xpc, p_wmnu
889 && ((wim_flags[wim_index] & WIM_LIST) == 0));
890#else
891 (void)showmatches(&xpc, FALSE);
892#endif
893 redrawcmd();
894 did_wild_list = TRUE;
895 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100896 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
897 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100899 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
900 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 }
902 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200903 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
905#ifdef FEAT_WILDMENU
906 else if (xpc.xp_numfiles == -1)
907 xpc.xp_context = EXPAND_NOTHING;
908#endif
909 }
910 if (wim_index < 3)
911 ++wim_index;
912 if (c == ESC)
913 gotesc = TRUE;
914 if (res == OK)
915 goto cmdline_changed;
916 }
917
918 gotesc = FALSE;
919
920 /* <S-Tab> goes to last match, in a clumsy way */
921 if (c == K_S_TAB && KeyTyped)
922 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100923 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
924 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
925 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 goto cmdline_changed;
927 }
928
929 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
930 c = NL;
931
932 do_abbr = TRUE; /* default: check for abbreviation */
933
934 /*
935 * Big switch for a typed command line character.
936 */
937 switch (c)
938 {
939 case K_BS:
940 case Ctrl_H:
941 case K_DEL:
942 case K_KDEL:
943 case Ctrl_W:
944#ifdef FEAT_FKMAP
945 if (cmd_fkmap && c == K_BS)
946 c = K_DEL;
947#endif
948 if (c == K_KDEL)
949 c = K_DEL;
950
951 /*
952 * delete current character is the same as backspace on next
953 * character, except at end of line
954 */
955 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
956 ++ccline.cmdpos;
957#ifdef FEAT_MBYTE
958 if (has_mbyte && c == K_DEL)
959 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
960 ccline.cmdbuff + ccline.cmdpos);
961#endif
962 if (ccline.cmdpos > 0)
963 {
964 char_u *p;
965
966 j = ccline.cmdpos;
967 p = ccline.cmdbuff + j;
968#ifdef FEAT_MBYTE
969 if (has_mbyte)
970 {
971 p = mb_prevptr(ccline.cmdbuff, p);
972 if (c == Ctrl_W)
973 {
974 while (p > ccline.cmdbuff && vim_isspace(*p))
975 p = mb_prevptr(ccline.cmdbuff, p);
976 i = mb_get_class(p);
977 while (p > ccline.cmdbuff && mb_get_class(p) == i)
978 p = mb_prevptr(ccline.cmdbuff, p);
979 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000980 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982 }
983 else
984#endif
985 if (c == Ctrl_W)
986 {
987 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
988 --p;
989 i = vim_iswordc(p[-1]);
990 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
991 && vim_iswordc(p[-1]) == i)
992 --p;
993 }
994 else
995 --p;
996 ccline.cmdpos = (int)(p - ccline.cmdbuff);
997 ccline.cmdlen -= j - ccline.cmdpos;
998 i = ccline.cmdpos;
999 while (i < ccline.cmdlen)
1000 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1001
1002 /* Truncate at the end, required for multi-byte chars. */
1003 ccline.cmdbuff[ccline.cmdlen] = NUL;
1004 redrawcmd();
1005 }
1006 else if (ccline.cmdlen == 0 && c != Ctrl_W
1007 && ccline.cmdprompt == NULL && indent == 0)
1008 {
1009 /* In ex and debug mode it doesn't make sense to return. */
1010 if (exmode_active
1011#ifdef FEAT_EVAL
1012 || ccline.cmdfirstc == '>'
1013#endif
1014 )
1015 goto cmdline_not_changed;
1016
1017 vim_free(ccline.cmdbuff); /* no commandline to return */
1018 ccline.cmdbuff = NULL;
1019 if (!cmd_silent)
1020 {
1021#ifdef FEAT_RIGHTLEFT
1022 if (cmdmsg_rl)
1023 msg_col = Columns;
1024 else
1025#endif
1026 msg_col = 0;
1027 msg_putchar(' '); /* delete ':' */
1028 }
1029 redraw_cmdline = TRUE;
1030 goto returncmd; /* back to cmd mode */
1031 }
1032 goto cmdline_changed;
1033
1034 case K_INS:
1035 case K_KINS:
1036#ifdef FEAT_FKMAP
1037 /* if Farsi mode set, we are in reverse insert mode -
1038 Do not change the mode */
1039 if (cmd_fkmap)
1040 beep_flush();
1041 else
1042#endif
1043 ccline.overstrike = !ccline.overstrike;
1044#ifdef CURSOR_SHAPE
1045 ui_cursor_shape(); /* may show different cursor shape */
1046#endif
1047 goto cmdline_not_changed;
1048
1049 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001050 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
1052 /* ":lmap" mappings exists, toggle use of mappings. */
1053 State ^= LANGMAP;
1054#ifdef USE_IM_CONTROL
1055 im_set_active(FALSE); /* Disable input method */
1056#endif
1057 if (b_im_ptr != NULL)
1058 {
1059 if (State & LANGMAP)
1060 *b_im_ptr = B_IMODE_LMAP;
1061 else
1062 *b_im_ptr = B_IMODE_NONE;
1063 }
1064 }
1065#ifdef USE_IM_CONTROL
1066 else
1067 {
1068 /* There are no ":lmap" mappings, toggle IM. When
1069 * 'imdisable' is set don't try getting the status, it's
1070 * always off. */
1071 if ((p_imdisable && b_im_ptr != NULL)
1072 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1073 {
1074 im_set_active(FALSE); /* Disable input method */
1075 if (b_im_ptr != NULL)
1076 *b_im_ptr = B_IMODE_NONE;
1077 }
1078 else
1079 {
1080 im_set_active(TRUE); /* Enable input method */
1081 if (b_im_ptr != NULL)
1082 *b_im_ptr = B_IMODE_IM;
1083 }
1084 }
1085#endif
1086 if (b_im_ptr != NULL)
1087 {
1088 if (b_im_ptr == &curbuf->b_p_iminsert)
1089 set_iminsert_global();
1090 else
1091 set_imsearch_global();
1092 }
1093#ifdef CURSOR_SHAPE
1094 ui_cursor_shape(); /* may show different cursor shape */
1095#endif
1096#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1097 /* Show/unshow value of 'keymap' in status lines later. */
1098 status_redraw_curbuf();
1099#endif
1100 goto cmdline_not_changed;
1101
1102/* case '@': only in very old vi */
1103 case Ctrl_U:
1104 /* delete all characters left of the cursor */
1105 j = ccline.cmdpos;
1106 ccline.cmdlen -= j;
1107 i = ccline.cmdpos = 0;
1108 while (i < ccline.cmdlen)
1109 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1110 /* Truncate at the end, required for multi-byte chars. */
1111 ccline.cmdbuff[ccline.cmdlen] = NUL;
1112 redrawcmd();
1113 goto cmdline_changed;
1114
1115#ifdef FEAT_CLIPBOARD
1116 case Ctrl_Y:
1117 /* Copy the modeless selection, if there is one. */
1118 if (clip_star.state != SELECT_CLEARED)
1119 {
1120 if (clip_star.state == SELECT_DONE)
1121 clip_copy_modeless_selection(TRUE);
1122 goto cmdline_not_changed;
1123 }
1124 break;
1125#endif
1126
1127 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1128 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001129 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001130 * ":normal" runs out of characters. */
1131 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001132 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 goto cmdline_not_changed;
1134
1135 gotesc = TRUE; /* will free ccline.cmdbuff after
1136 putting it in history */
1137 goto returncmd; /* back to cmd mode */
1138
1139 case Ctrl_R: /* insert register */
1140#ifdef USE_ON_FLY_SCROLL
1141 dont_scroll = TRUE; /* disallow scrolling here */
1142#endif
1143 putcmdline('"', TRUE);
1144 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001145 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (i == Ctrl_O)
1147 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1148 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001149 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 --no_mapping;
1151#ifdef FEAT_EVAL
1152 /*
1153 * Insert the result of an expression.
1154 * Need to save the current command line, to be able to enter
1155 * a new one...
1156 */
1157 new_cmdpos = -1;
1158 if (c == '=')
1159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1161 {
1162 beep_flush();
1163 c = ESC;
1164 }
1165 else
1166 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001167 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001169 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171 }
1172#endif
1173 if (c != ESC) /* use ESC to cancel inserting register */
1174 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001175 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001176
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001177#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001178 /* When there was a serious error abort getting the
1179 * command line. */
1180 if (aborting())
1181 {
1182 gotesc = TRUE; /* will free ccline.cmdbuff after
1183 putting it in history */
1184 goto returncmd; /* back to cmd mode */
1185 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 KeyTyped = FALSE; /* Don't do p_wc completion. */
1188#ifdef FEAT_EVAL
1189 if (new_cmdpos >= 0)
1190 {
1191 /* set_cmdline_pos() was used */
1192 if (new_cmdpos > ccline.cmdlen)
1193 ccline.cmdpos = ccline.cmdlen;
1194 else
1195 ccline.cmdpos = new_cmdpos;
1196 }
1197#endif
1198 }
1199 redrawcmd();
1200 goto cmdline_changed;
1201
1202 case Ctrl_D:
1203 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1204 break; /* Use ^D as normal char instead */
1205
1206 redrawcmd();
1207 continue; /* don't do incremental search now */
1208
1209 case K_RIGHT:
1210 case K_S_RIGHT:
1211 case K_C_RIGHT:
1212 do
1213 {
1214 if (ccline.cmdpos >= ccline.cmdlen)
1215 break;
1216 i = cmdline_charsize(ccline.cmdpos);
1217 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1218 break;
1219 ccline.cmdspos += i;
1220#ifdef FEAT_MBYTE
1221 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001222 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 + ccline.cmdpos);
1224 else
1225#endif
1226 ++ccline.cmdpos;
1227 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001228 while ((c == K_S_RIGHT || c == K_C_RIGHT
1229 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1231#ifdef FEAT_MBYTE
1232 if (has_mbyte)
1233 set_cmdspos_cursor();
1234#endif
1235 goto cmdline_not_changed;
1236
1237 case K_LEFT:
1238 case K_S_LEFT:
1239 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001240 if (ccline.cmdpos == 0)
1241 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 do
1243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 --ccline.cmdpos;
1245#ifdef FEAT_MBYTE
1246 if (has_mbyte) /* move to first byte of char */
1247 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1248 ccline.cmdbuff + ccline.cmdpos);
1249#endif
1250 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1251 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001252 while (ccline.cmdpos > 0
1253 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001254 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1256#ifdef FEAT_MBYTE
1257 if (has_mbyte)
1258 set_cmdspos_cursor();
1259#endif
1260 goto cmdline_not_changed;
1261
1262 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001263 /* Ignore mouse event or ex_window() result. */
1264 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaar4770d092006-01-12 23:22:24 +00001266#ifdef FEAT_GUI_W32
1267 /* On Win32 ignore <M-F4>, we get it when closing the window was
1268 * cancelled. */
1269 case K_F4:
1270 if (mod_mask == MOD_MASK_ALT)
1271 {
1272 redrawcmd(); /* somehow the cmdline is cleared */
1273 goto cmdline_not_changed;
1274 }
1275 break;
1276#endif
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278#ifdef FEAT_MOUSE
1279 case K_MIDDLEDRAG:
1280 case K_MIDDLERELEASE:
1281 goto cmdline_not_changed; /* Ignore mouse */
1282
1283 case K_MIDDLEMOUSE:
1284# ifdef FEAT_GUI
1285 /* When GUI is active, also paste when 'mouse' is empty */
1286 if (!gui.in_use)
1287# endif
1288 if (!mouse_has(MOUSE_COMMAND))
1289 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001290# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001292 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001294# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001295 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 redrawcmd();
1297 goto cmdline_changed;
1298
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001299# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001301 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 redrawcmd();
1303 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306 case K_LEFTDRAG:
1307 case K_LEFTRELEASE:
1308 case K_RIGHTDRAG:
1309 case K_RIGHTRELEASE:
1310 /* Ignore drag and release events when the button-down wasn't
1311 * seen before. */
1312 if (ignore_drag_release)
1313 goto cmdline_not_changed;
1314 /* FALLTHROUGH */
1315 case K_LEFTMOUSE:
1316 case K_RIGHTMOUSE:
1317 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1318 ignore_drag_release = TRUE;
1319 else
1320 ignore_drag_release = FALSE;
1321# ifdef FEAT_GUI
1322 /* When GUI is active, also move when 'mouse' is empty */
1323 if (!gui.in_use)
1324# endif
1325 if (!mouse_has(MOUSE_COMMAND))
1326 goto cmdline_not_changed; /* Ignore mouse */
1327# ifdef FEAT_CLIPBOARD
1328 if (mouse_row < cmdline_row && clip_star.available)
1329 {
1330 int button, is_click, is_drag;
1331
1332 /*
1333 * Handle modeless selection.
1334 */
1335 button = get_mouse_button(KEY2TERMCAP1(c),
1336 &is_click, &is_drag);
1337 if (mouse_model_popup() && button == MOUSE_LEFT
1338 && (mod_mask & MOD_MASK_SHIFT))
1339 {
1340 /* Translate shift-left to right button. */
1341 button = MOUSE_RIGHT;
1342 mod_mask &= ~MOD_MASK_SHIFT;
1343 }
1344 clip_modeless(button, is_click, is_drag);
1345 goto cmdline_not_changed;
1346 }
1347# endif
1348
1349 set_cmdspos();
1350 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1351 ++ccline.cmdpos)
1352 {
1353 i = cmdline_charsize(ccline.cmdpos);
1354 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1355 && mouse_col < ccline.cmdspos % Columns + i)
1356 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001357# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (has_mbyte)
1359 {
1360 /* Count ">" for double-wide char that doesn't fit. */
1361 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001362 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 + ccline.cmdpos) - 1;
1364 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 ccline.cmdspos += i;
1367 }
1368 goto cmdline_not_changed;
1369
1370 /* Mouse scroll wheel: ignored here */
1371 case K_MOUSEDOWN:
1372 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001373 case K_MOUSELEFT:
1374 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 /* Alternate buttons ignored here */
1376 case K_X1MOUSE:
1377 case K_X1DRAG:
1378 case K_X1RELEASE:
1379 case K_X2MOUSE:
1380 case K_X2DRAG:
1381 case K_X2RELEASE:
1382 goto cmdline_not_changed;
1383
1384#endif /* FEAT_MOUSE */
1385
1386#ifdef FEAT_GUI
1387 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1388 case K_LEFTRELEASE_NM:
1389 goto cmdline_not_changed;
1390
1391 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001392 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 {
1394 gui_do_scroll();
1395 redrawcmd();
1396 }
1397 goto cmdline_not_changed;
1398
1399 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001400 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001402 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 redrawcmd();
1404 }
1405 goto cmdline_not_changed;
1406#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001407#ifdef FEAT_GUI_TABLINE
1408 case K_TABLINE:
1409 case K_TABMENU:
1410 /* Don't want to change any tabs here. Make sure the same tab
1411 * is still selected. */
1412 if (gui_use_tabline())
1413 gui_mch_set_curtab(tabpage_index(curtab));
1414 goto cmdline_not_changed;
1415#endif
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 case K_SELECT: /* end of Select mode mapping - ignore */
1418 goto cmdline_not_changed;
1419
1420 case Ctrl_B: /* begin of command line */
1421 case K_HOME:
1422 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 case K_S_HOME:
1424 case K_C_HOME:
1425 ccline.cmdpos = 0;
1426 set_cmdspos();
1427 goto cmdline_not_changed;
1428
1429 case Ctrl_E: /* end of command line */
1430 case K_END:
1431 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 case K_S_END:
1433 case K_C_END:
1434 ccline.cmdpos = ccline.cmdlen;
1435 set_cmdspos_cursor();
1436 goto cmdline_not_changed;
1437
1438 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001439 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 break;
1441 goto cmdline_changed;
1442
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001443 case Ctrl_L:
1444#ifdef FEAT_SEARCH_EXTRA
1445 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1446 {
1447 /* Add a character from under the cursor for 'incsearch' */
1448 if (did_incsearch
1449 && !equalpos(curwin->w_cursor, old_cursor))
1450 {
1451 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001452 /* If 'ignorecase' and 'smartcase' are set and the
1453 * command line has no uppercase characters, convert
1454 * the character to lowercase */
1455 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1456 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001457 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001458 {
1459 if (c == firstc || vim_strchr((char_u *)(
1460 p_magic ? "\\^$.*[" : "\\^$"), c)
1461 != NULL)
1462 {
1463 /* put a backslash before special characters */
1464 stuffcharReadbuff(c);
1465 c = '\\';
1466 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001467 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001468 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001469 }
1470 goto cmdline_not_changed;
1471 }
1472#endif
1473
1474 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001475 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 break;
1477 goto cmdline_changed;
1478
1479 case Ctrl_N: /* next match */
1480 case Ctrl_P: /* previous match */
1481 if (xpc.xp_numfiles > 0)
1482 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001483 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1484 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 break;
1486 goto cmdline_changed;
1487 }
1488
1489#ifdef FEAT_CMDHIST
1490 case K_UP:
1491 case K_DOWN:
1492 case K_S_UP:
1493 case K_S_DOWN:
1494 case K_PAGEUP:
1495 case K_KPAGEUP:
1496 case K_PAGEDOWN:
1497 case K_KPAGEDOWN:
1498 if (hislen == 0 || firstc == NUL) /* no history */
1499 goto cmdline_not_changed;
1500
1501 i = hiscnt;
1502
1503 /* save current command string so it can be restored later */
1504 if (lookfor == NULL)
1505 {
1506 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1507 goto cmdline_not_changed;
1508 lookfor[ccline.cmdpos] = NUL;
1509 }
1510
1511 j = (int)STRLEN(lookfor);
1512 for (;;)
1513 {
1514 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001515 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001516 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
1518 if (hiscnt == hislen) /* first time */
1519 hiscnt = hisidx[histype];
1520 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1521 hiscnt = hislen - 1;
1522 else if (hiscnt != hisidx[histype] + 1)
1523 --hiscnt;
1524 else /* at top of list */
1525 {
1526 hiscnt = i;
1527 break;
1528 }
1529 }
1530 else /* one step forwards */
1531 {
1532 /* on last entry, clear the line */
1533 if (hiscnt == hisidx[histype])
1534 {
1535 hiscnt = hislen;
1536 break;
1537 }
1538
1539 /* not on a history line, nothing to do */
1540 if (hiscnt == hislen)
1541 break;
1542 if (hiscnt == hislen - 1) /* wrap around */
1543 hiscnt = 0;
1544 else
1545 ++hiscnt;
1546 }
1547 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1548 {
1549 hiscnt = i;
1550 break;
1551 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001552 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001553 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 || STRNCMP(history[histype][hiscnt].hisstr,
1555 lookfor, (size_t)j) == 0)
1556 break;
1557 }
1558
1559 if (hiscnt != i) /* jumped to other entry */
1560 {
1561 char_u *p;
1562 int len;
1563 int old_firstc;
1564
1565 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001566 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (hiscnt == hislen)
1568 p = lookfor; /* back to the old one */
1569 else
1570 p = history[histype][hiscnt].hisstr;
1571
1572 if (histype == HIST_SEARCH
1573 && p != lookfor
1574 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1575 {
1576 /* Correct for the separator character used when
1577 * adding the history entry vs the one used now.
1578 * First loop: count length.
1579 * Second loop: copy the characters. */
1580 for (i = 0; i <= 1; ++i)
1581 {
1582 len = 0;
1583 for (j = 0; p[j] != NUL; ++j)
1584 {
1585 /* Replace old sep with new sep, unless it is
1586 * escaped. */
1587 if (p[j] == old_firstc
1588 && (j == 0 || p[j - 1] != '\\'))
1589 {
1590 if (i > 0)
1591 ccline.cmdbuff[len] = firstc;
1592 }
1593 else
1594 {
1595 /* Escape new sep, unless it is already
1596 * escaped. */
1597 if (p[j] == firstc
1598 && (j == 0 || p[j - 1] != '\\'))
1599 {
1600 if (i > 0)
1601 ccline.cmdbuff[len] = '\\';
1602 ++len;
1603 }
1604 if (i > 0)
1605 ccline.cmdbuff[len] = p[j];
1606 }
1607 ++len;
1608 }
1609 if (i == 0)
1610 {
1611 alloc_cmdbuff(len);
1612 if (ccline.cmdbuff == NULL)
1613 goto returncmd;
1614 }
1615 }
1616 ccline.cmdbuff[len] = NUL;
1617 }
1618 else
1619 {
1620 alloc_cmdbuff((int)STRLEN(p));
1621 if (ccline.cmdbuff == NULL)
1622 goto returncmd;
1623 STRCPY(ccline.cmdbuff, p);
1624 }
1625
1626 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1627 redrawcmd();
1628 goto cmdline_changed;
1629 }
1630 beep_flush();
1631 goto cmdline_not_changed;
1632#endif
1633
1634 case Ctrl_V:
1635 case Ctrl_Q:
1636#ifdef FEAT_MOUSE
1637 ignore_drag_release = TRUE;
1638#endif
1639 putcmdline('^', TRUE);
1640 c = get_literal(); /* get next (two) character(s) */
1641 do_abbr = FALSE; /* don't do abbreviation now */
1642#ifdef FEAT_MBYTE
1643 /* may need to remove ^ when composing char was typed */
1644 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1645 {
1646 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1647 msg_putchar(' ');
1648 cursorcmd();
1649 }
1650#endif
1651 break;
1652
1653#ifdef FEAT_DIGRAPHS
1654 case Ctrl_K:
1655#ifdef FEAT_MOUSE
1656 ignore_drag_release = TRUE;
1657#endif
1658 putcmdline('?', TRUE);
1659#ifdef USE_ON_FLY_SCROLL
1660 dont_scroll = TRUE; /* disallow scrolling here */
1661#endif
1662 c = get_digraph(TRUE);
1663 if (c != NUL)
1664 break;
1665
1666 redrawcmd();
1667 goto cmdline_not_changed;
1668#endif /* FEAT_DIGRAPHS */
1669
1670#ifdef FEAT_RIGHTLEFT
1671 case Ctrl__: /* CTRL-_: switch language mode */
1672 if (!p_ari)
1673 break;
1674#ifdef FEAT_FKMAP
1675 if (p_altkeymap)
1676 {
1677 cmd_fkmap = !cmd_fkmap;
1678 if (cmd_fkmap) /* in Farsi always in Insert mode */
1679 ccline.overstrike = FALSE;
1680 }
1681 else /* Hebrew is default */
1682#endif
1683 cmd_hkmap = !cmd_hkmap;
1684 goto cmdline_not_changed;
1685#endif
1686
1687 default:
1688#ifdef UNIX
1689 if (c == intr_char)
1690 {
1691 gotesc = TRUE; /* will free ccline.cmdbuff after
1692 putting it in history */
1693 goto returncmd; /* back to Normal mode */
1694 }
1695#endif
1696 /*
1697 * Normal character with no special meaning. Just set mod_mask
1698 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1699 * the string <S-Space>. This should only happen after ^V.
1700 */
1701 if (!IS_SPECIAL(c))
1702 mod_mask = 0x0;
1703 break;
1704 }
1705 /*
1706 * End of switch on command line character.
1707 * We come here if we have a normal character.
1708 */
1709
Bram Moolenaarede3e632013-06-23 16:16:19 +02001710 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#ifdef FEAT_MBYTE
1712 /* Add ABBR_OFF for characters above 0x100, this is
1713 * what check_abbr() expects. */
1714 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1715#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001716 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 goto cmdline_changed;
1718
1719 /*
1720 * put the character in the command line
1721 */
1722 if (IS_SPECIAL(c) || mod_mask != 0)
1723 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1724 else
1725 {
1726#ifdef FEAT_MBYTE
1727 if (has_mbyte)
1728 {
1729 j = (*mb_char2bytes)(c, IObuff);
1730 IObuff[j] = NUL; /* exclude composing chars */
1731 put_on_cmdline(IObuff, j, TRUE);
1732 }
1733 else
1734#endif
1735 {
1736 IObuff[0] = c;
1737 put_on_cmdline(IObuff, 1, TRUE);
1738 }
1739 }
1740 goto cmdline_changed;
1741
1742/*
1743 * This part implements incremental searches for "/" and "?"
1744 * Jump to cmdline_not_changed when a character has been read but the command
1745 * line did not change. Then we only search and redraw if something changed in
1746 * the past.
1747 * Jump to cmdline_changed when the command line did change.
1748 * (Sorry for the goto's, I know it is ugly).
1749 */
1750cmdline_not_changed:
1751#ifdef FEAT_SEARCH_EXTRA
1752 if (!incsearch_postponed)
1753 continue;
1754#endif
1755
1756cmdline_changed:
1757#ifdef FEAT_SEARCH_EXTRA
1758 /*
1759 * 'incsearch' highlighting.
1760 */
1761 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1762 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001763 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001764#ifdef FEAT_RELTIME
1765 proftime_T tm;
1766#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 /* if there is a character waiting, search and redraw later */
1769 if (char_avail())
1770 {
1771 incsearch_postponed = TRUE;
1772 continue;
1773 }
1774 incsearch_postponed = FALSE;
1775 curwin->w_cursor = old_cursor; /* start at old position */
1776
1777 /* If there is no command line, don't do anything */
1778 if (ccline.cmdlen == 0)
1779 i = 0;
1780 else
1781 {
1782 cursor_off(); /* so the user knows we're busy */
1783 out_flush();
1784 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001785#ifdef FEAT_RELTIME
1786 /* Set the time limit to half a second. */
1787 profile_setlimit(500L, &tm);
1788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001790 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1791#ifdef FEAT_RELTIME
1792 &tm
1793#else
1794 NULL
1795#endif
1796 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 --emsg_off;
1798 /* if interrupted while searching, behave like it failed */
1799 if (got_int)
1800 {
1801 (void)vpeekc(); /* remove <C-C> from input stream */
1802 got_int = FALSE; /* don't abandon the command line */
1803 i = 0;
1804 }
1805 else if (char_avail())
1806 /* cancelled searching because a char was typed */
1807 incsearch_postponed = TRUE;
1808 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001809 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 highlight_match = TRUE; /* highlight position */
1811 else
1812 highlight_match = FALSE; /* remove highlight */
1813
1814 /* first restore the old curwin values, so the screen is
1815 * positioned in the same way as the actual search command */
1816 curwin->w_leftcol = old_leftcol;
1817 curwin->w_topline = old_topline;
1818# ifdef FEAT_DIFF
1819 curwin->w_topfill = old_topfill;
1820# endif
1821 curwin->w_botline = old_botline;
1822 changed_cline_bef_curs();
1823 update_topline();
1824
1825 if (i != 0)
1826 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001827 pos_T save_pos = curwin->w_cursor;
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001830 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 * moves the whole match onto the screen when 'nowrap' is set.
1832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 curwin->w_cursor.lnum += search_match_lines;
1834 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001835 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1836 {
1837 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1838 coladvance((colnr_T)MAXCOL);
1839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001841 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001842 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 else
1845 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001848# ifdef FEAT_WINDOWS
1849 /* May redraw the status line to show the cursor position. */
1850 if (p_ru && curwin->w_status_height > 0)
1851 curwin->w_redr_status = TRUE;
1852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001854 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001855 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001856 restore_cmdline(&save_ccline);
1857
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001858 /* Leave it at the end to make CTRL-R CTRL-W work. */
1859 if (i != 0)
1860 curwin->w_cursor = end_pos;
1861
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 msg_starthere();
1863 redrawcmdline();
1864 did_incsearch = TRUE;
1865 }
1866#else /* FEAT_SEARCH_EXTRA */
1867 ;
1868#endif
1869
1870#ifdef FEAT_RIGHTLEFT
1871 if (cmdmsg_rl
1872# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001873 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874# endif
1875 )
1876 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001877 * right-left typing. Not efficient, but it works.
1878 * Do it only when there are no characters left to read
1879 * to avoid useless intermediate redraws. */
1880 if (vpeekc() == NUL)
1881 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882#endif
1883 }
1884
1885returncmd:
1886
1887#ifdef FEAT_RIGHTLEFT
1888 cmdmsg_rl = FALSE;
1889#endif
1890
1891#ifdef FEAT_FKMAP
1892 cmd_fkmap = 0;
1893#endif
1894
1895 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001896 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
1898#ifdef FEAT_SEARCH_EXTRA
1899 if (did_incsearch)
1900 {
1901 curwin->w_cursor = old_cursor;
1902 curwin->w_curswant = old_curswant;
1903 curwin->w_leftcol = old_leftcol;
1904 curwin->w_topline = old_topline;
1905# ifdef FEAT_DIFF
1906 curwin->w_topfill = old_topfill;
1907# endif
1908 curwin->w_botline = old_botline;
1909 highlight_match = FALSE;
1910 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001911 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913#endif
1914
1915 if (ccline.cmdbuff != NULL)
1916 {
1917 /*
1918 * Put line in history buffer (":" and "=" only when it was typed).
1919 */
1920#ifdef FEAT_CMDHIST
1921 if (ccline.cmdlen && firstc != NUL
1922 && (some_key_typed || histype == HIST_SEARCH))
1923 {
1924 add_to_history(histype, ccline.cmdbuff, TRUE,
1925 histype == HIST_SEARCH ? firstc : NUL);
1926 if (firstc == ':')
1927 {
1928 vim_free(new_last_cmdline);
1929 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1930 }
1931 }
1932#endif
1933
1934 if (gotesc) /* abandon command line */
1935 {
1936 vim_free(ccline.cmdbuff);
1937 ccline.cmdbuff = NULL;
1938 if (msg_scrolled == 0)
1939 compute_cmdrow();
1940 MSG("");
1941 redraw_cmdline = TRUE;
1942 }
1943 }
1944
1945 /*
1946 * If the screen was shifted up, redraw the whole screen (later).
1947 * If the line is too long, clear it, so ruler and shown command do
1948 * not get printed in the middle of it.
1949 */
1950 msg_check();
1951 msg_scroll = save_msg_scroll;
1952 redir_off = FALSE;
1953
1954 /* When the command line was typed, no need for a wait-return prompt. */
1955 if (some_key_typed)
1956 need_wait_return = FALSE;
1957
1958 State = save_State;
1959#ifdef USE_IM_CONTROL
1960 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1961 im_save_status(b_im_ptr);
1962 im_set_active(FALSE);
1963#endif
1964#ifdef FEAT_MOUSE
1965 setmouse();
1966#endif
1967#ifdef CURSOR_SHAPE
1968 ui_cursor_shape(); /* may show different cursor shape */
1969#endif
1970
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001971 {
1972 char_u *p = ccline.cmdbuff;
1973
1974 /* Make ccline empty, getcmdline() may try to use it. */
1975 ccline.cmdbuff = NULL;
1976 return p;
1977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978}
1979
1980#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1981/*
1982 * Get a command line with a prompt.
1983 * This is prepared to be called recursively from getcmdline() (e.g. by
1984 * f_input() when evaluating an expression from CTRL-R =).
1985 * Returns the command line in allocated memory, or NULL.
1986 */
1987 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001988getcmdline_prompt(
1989 int firstc,
1990 char_u *prompt, /* command line prompt */
1991 int attr, /* attributes for prompt */
1992 int xp_context, /* type of expansion */
1993 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994{
1995 char_u *s;
1996 struct cmdline_info save_ccline;
1997 int msg_col_save = msg_col;
1998
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001999 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 ccline.cmdprompt = prompt;
2001 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002002# ifdef FEAT_EVAL
2003 ccline.xp_context = xp_context;
2004 ccline.xp_arg = xp_arg;
2005 ccline.input_fn = (firstc == '@');
2006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002008 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002009 /* Restore msg_col, the prompt from input() may have changed it.
2010 * But only if called recursively and the commandline is therefore being
2011 * restored to an old one; if not, the input() prompt stays on the screen,
2012 * so we need its modified msg_col left intact. */
2013 if (ccline.cmdbuff != NULL)
2014 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016 return s;
2017}
2018#endif
2019
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002020/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002021 * Return TRUE when the text must not be changed and we can't switch to
2022 * another window or buffer. Used when editing the command line, evaluating
2023 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002024 */
2025 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002026text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002027{
2028#ifdef FEAT_CMDWIN
2029 if (cmdwin_type != 0)
2030 return TRUE;
2031#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002032 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002033}
2034
2035/*
2036 * Give an error message for a command that isn't allowed while the cmdline
2037 * window is open or editing the cmdline in another way.
2038 */
2039 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002040text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002041{
2042#ifdef FEAT_CMDWIN
2043 if (cmdwin_type != 0)
2044 EMSG(_(e_cmdwin));
2045 else
2046#endif
2047 EMSG(_(e_secure));
2048}
2049
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002050#if defined(FEAT_AUTOCMD) || defined(PROTO)
2051/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002052 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2053 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002054 */
2055 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002056curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002057{
2058 if (curbuf_lock > 0)
2059 {
2060 EMSG(_("E788: Not allowed to edit another buffer now"));
2061 return TRUE;
2062 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002063 return allbuf_locked();
2064}
2065
2066/*
2067 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2068 * message.
2069 */
2070 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002071allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002072{
2073 if (allbuf_lock > 0)
2074 {
2075 EMSG(_("E811: Not allowed to change buffer information now"));
2076 return TRUE;
2077 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002078 return FALSE;
2079}
2080#endif
2081
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002083cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084{
2085#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2086 if (cmdline_star > 0) /* showing '*', always 1 position */
2087 return 1;
2088#endif
2089 return ptr2cells(ccline.cmdbuff + idx);
2090}
2091
2092/*
2093 * Compute the offset of the cursor on the command line for the prompt and
2094 * indent.
2095 */
2096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002097set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002099 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 ccline.cmdspos = 1 + ccline.cmdindent;
2101 else
2102 ccline.cmdspos = 0 + ccline.cmdindent;
2103}
2104
2105/*
2106 * Compute the screen position for the cursor on the command line.
2107 */
2108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002109set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 int i, m, c;
2112
2113 set_cmdspos();
2114 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002117 if (m < 0) /* overflow, Columns or Rows at weird value */
2118 m = MAXCOL;
2119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 else
2121 m = MAXCOL;
2122 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2123 {
2124 c = cmdline_charsize(i);
2125#ifdef FEAT_MBYTE
2126 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2127 if (has_mbyte)
2128 correct_cmdspos(i, c);
2129#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002130 /* If the cmdline doesn't fit, show cursor on last visible char.
2131 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if ((ccline.cmdspos += c) >= m)
2133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 ccline.cmdspos -= c;
2135 break;
2136 }
2137#ifdef FEAT_MBYTE
2138 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002139 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140#endif
2141 }
2142}
2143
2144#ifdef FEAT_MBYTE
2145/*
2146 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2147 * character that doesn't fit, so that a ">" must be displayed.
2148 */
2149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002150correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002152 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2154 && ccline.cmdspos % Columns + cells > Columns)
2155 ccline.cmdspos++;
2156}
2157#endif
2158
2159/*
2160 * Get an Ex command line for the ":" command.
2161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002163getexline(
2164 int c, /* normally ':', NUL for ":append" */
2165 void *cookie UNUSED,
2166 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
2168 /* When executing a register, remove ':' that's in front of each line. */
2169 if (exec_from_reg && vpeekc() == ':')
2170 (void)vgetc();
2171 return getcmdline(c, 1L, indent);
2172}
2173
2174/*
2175 * Get an Ex command line for Ex mode.
2176 * In Ex mode we only use the OS supplied line editing features and no
2177 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002178 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002181getexmodeline(
2182 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002183 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002184 void *cookie UNUSED,
2185 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002187 garray_T line_ga;
2188 char_u *pend;
2189 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002190 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002191 int escaped = FALSE; /* CTRL-V typed */
2192 int vcol = 0;
2193 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002194 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002195 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196
2197 /* Switch cursor on now. This avoids that it happens after the "\n", which
2198 * confuses the system function that computes tabstops. */
2199 cursor_on();
2200
2201 /* always start in column 0; write a newline if necessary */
2202 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002203 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002205 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002207 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002208 if (p_prompt)
2209 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 while (indent-- > 0)
2211 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 }
2214
2215 ga_init2(&line_ga, 1, 30);
2216
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002217 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002218 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002219 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002220 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002221 while (indent >= 8)
2222 {
2223 ga_append(&line_ga, TAB);
2224 msg_puts((char_u *)" ");
2225 indent -= 8;
2226 }
2227 while (indent-- > 0)
2228 {
2229 ga_append(&line_ga, ' ');
2230 msg_putchar(' ');
2231 }
2232 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002233 ++no_mapping;
2234 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 /*
2237 * Get the line, one character at a time.
2238 */
2239 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002240 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002242 long sw;
2243 char_u *s;
2244
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 if (ga_grow(&line_ga, 40) == FAIL)
2246 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002248 /* Get one character at a time. Don't use inchar(), it can't handle
2249 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002250 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002251 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002254 * Handle line editing.
2255 * Previously this was left to the system, putting the terminal in
2256 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002258 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002260 msg_putchar('\n');
2261 break;
2262 }
2263
2264 if (!escaped)
2265 {
2266 /* CR typed means "enter", which is NL */
2267 if (c1 == '\r')
2268 c1 = '\n';
2269
2270 if (c1 == BS || c1 == K_BS
2271 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002273 if (line_ga.ga_len > 0)
2274 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002275#ifdef FEAT_MBYTE
2276 if (has_mbyte)
2277 {
2278 p = (char_u *)line_ga.ga_data;
2279 p[line_ga.ga_len] = NUL;
2280 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2281 line_ga.ga_len -= len;
2282 }
2283 else
2284#endif
2285 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002286 goto redraw;
2287 }
2288 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002291 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002293 msg_col = startcol;
2294 msg_clr_eos();
2295 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002296 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002297 }
2298
2299 if (c1 == Ctrl_T)
2300 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002301 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002302 p = (char_u *)line_ga.ga_data;
2303 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002304 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002305 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002307 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002309 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002310 p = (char_u *)line_ga.ga_data;
2311 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002312 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2313 *s = ' ';
2314 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002316redraw:
2317 /* redraw the line */
2318 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002319 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002320 p = (char_u *)line_ga.ga_data;
2321 p[line_ga.ga_len] = NUL;
2322 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002324 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002326 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002328 msg_putchar(' ');
2329 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002330 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002332 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002334 len = MB_PTR2LEN(p);
2335 msg_outtrans_len(p, len);
2336 vcol += ptr2cells(p);
2337 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002340 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002341 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342 continue;
2343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002345 if (c1 == Ctrl_D)
2346 {
2347 /* Delete one shiftwidth. */
2348 p = (char_u *)line_ga.ga_data;
2349 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002351 if (prev_char == '^')
2352 ex_keep_indent = TRUE;
2353 indent = 0;
2354 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356 else
2357 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002358 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002359 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002360 if (indent > 0)
2361 {
2362 --indent;
2363 indent -= indent % get_sw_value(curbuf);
2364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002366 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002367 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002368 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002369 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2370 --line_ga.ga_len;
2371 }
2372 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002374
2375 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2376 {
2377 escaped = TRUE;
2378 continue;
2379 }
2380
2381 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2382 if (IS_SPECIAL(c1))
2383 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002385
2386 if (IS_SPECIAL(c1))
2387 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002388#ifdef FEAT_MBYTE
2389 if (has_mbyte)
2390 len = (*mb_char2bytes)(c1,
2391 (char_u *)line_ga.ga_data + line_ga.ga_len);
2392 else
2393#endif
2394 {
2395 len = 1;
2396 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2397 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002398 if (c1 == '\n')
2399 msg_putchar('\n');
2400 else if (c1 == TAB)
2401 {
2402 /* Don't use chartabsize(), 'ts' can be different */
2403 do
2404 {
2405 msg_putchar(' ');
2406 } while (++vcol % 8);
2407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002410 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002411 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002412 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002414 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002415 escaped = FALSE;
2416
2417 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002418 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002419
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002420 /* We are done when a NL is entered, but not when it comes after an
2421 * odd number of backslashes, that results in a NUL. */
2422 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002424 int bcount = 0;
2425
2426 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2427 ++bcount;
2428
2429 if (bcount > 0)
2430 {
2431 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2432 * "\NL", etc. */
2433 line_ga.ga_len -= (bcount + 1) / 2;
2434 pend -= (bcount + 1) / 2;
2435 pend[-1] = '\n';
2436 }
2437
2438 if ((bcount & 1) == 0)
2439 {
2440 --line_ga.ga_len;
2441 --pend;
2442 *pend = NUL;
2443 break;
2444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 }
2446 }
2447
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002448 --no_mapping;
2449 --allow_keys;
2450
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 /* make following messages go to the next line */
2452 msg_didout = FALSE;
2453 msg_col = 0;
2454 if (msg_row < Rows - 1)
2455 ++msg_row;
2456 emsg_on_display = FALSE; /* don't want ui_delay() */
2457
2458 if (got_int)
2459 ga_clear(&line_ga);
2460
2461 return (char_u *)line_ga.ga_data;
2462}
2463
Bram Moolenaare344bea2005-09-01 20:46:49 +00002464# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2465 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466/*
2467 * Return TRUE if ccline.overstrike is on.
2468 */
2469 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002470cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
2472 return ccline.overstrike;
2473}
2474
2475/*
2476 * Return TRUE if the cursor is at the end of the cmdline.
2477 */
2478 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002479cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
2481 return (ccline.cmdpos >= ccline.cmdlen);
2482}
2483#endif
2484
Bram Moolenaar9372a112005-12-06 19:59:18 +00002485#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486/*
2487 * Return the virtual column number at the current cursor position.
2488 * This is used by the IM code to obtain the start of the preedit string.
2489 */
2490 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002491cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2494 return MAXCOL;
2495
2496# ifdef FEAT_MBYTE
2497 if (has_mbyte)
2498 {
2499 colnr_T col;
2500 int i = 0;
2501
2502 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002503 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505 return col;
2506 }
2507 else
2508# endif
2509 return ccline.cmdpos;
2510}
2511#endif
2512
2513#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2514/*
2515 * If part of the command line is an IM preedit string, redraw it with
2516 * IM feedback attributes. The cursor position is restored after drawing.
2517 */
2518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002519redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
2521 if ((State & CMDLINE)
2522 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002523 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 && !p_imdisable
2525 && im_is_preediting())
2526 {
2527 int cmdpos = 0;
2528 int cmdspos;
2529 int old_row;
2530 int old_col;
2531 colnr_T col;
2532
2533 old_row = msg_row;
2534 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002535 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536
2537# ifdef FEAT_MBYTE
2538 if (has_mbyte)
2539 {
2540 for (col = 0; col < preedit_start_col
2541 && cmdpos < ccline.cmdlen; ++col)
2542 {
2543 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002544 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546 }
2547 else
2548# endif
2549 {
2550 cmdspos += preedit_start_col;
2551 cmdpos += preedit_start_col;
2552 }
2553
2554 msg_row = cmdline_row + (cmdspos / (int)Columns);
2555 msg_col = cmdspos % (int)Columns;
2556 if (msg_row >= Rows)
2557 msg_row = Rows - 1;
2558
2559 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2560 {
2561 int char_len;
2562 int char_attr;
2563
2564 char_attr = im_get_feedback_attr(col);
2565 if (char_attr < 0)
2566 break; /* end of preedit string */
2567
2568# ifdef FEAT_MBYTE
2569 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002570 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 else
2572# endif
2573 char_len = 1;
2574
2575 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2576 cmdpos += char_len;
2577 }
2578
2579 msg_row = old_row;
2580 msg_col = old_col;
2581 }
2582}
2583#endif /* FEAT_XIM && FEAT_GUI_GTK */
2584
2585/*
2586 * Allocate a new command line buffer.
2587 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2588 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2589 */
2590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002591alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592{
2593 /*
2594 * give some extra space to avoid having to allocate all the time
2595 */
2596 if (len < 80)
2597 len = 100;
2598 else
2599 len += 20;
2600
2601 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2602 ccline.cmdbufflen = len;
2603}
2604
2605/*
2606 * Re-allocate the command line to length len + something extra.
2607 * return FAIL for failure, OK otherwise
2608 */
2609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002610realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611{
2612 char_u *p;
2613
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002614 if (len < ccline.cmdbufflen)
2615 return OK; /* no need to resize */
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 p = ccline.cmdbuff;
2618 alloc_cmdbuff(len); /* will get some more */
2619 if (ccline.cmdbuff == NULL) /* out of memory */
2620 {
2621 ccline.cmdbuff = p; /* keep the old one */
2622 return FAIL;
2623 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002624 /* There isn't always a NUL after the command, but it may need to be
2625 * there, thus copy up to the NUL and add a NUL. */
2626 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2627 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002629
2630 if (ccline.xpc != NULL
2631 && ccline.xpc->xp_pattern != NULL
2632 && ccline.xpc->xp_context != EXPAND_NOTHING
2633 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2634 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002635 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002636
2637 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2638 * to point into the newly allocated memory. */
2639 if (i >= 0 && i <= ccline.cmdlen)
2640 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2641 }
2642
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 return OK;
2644}
2645
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002646#if defined(FEAT_ARABIC) || defined(PROTO)
2647static char_u *arshape_buf = NULL;
2648
2649# if defined(EXITFREE) || defined(PROTO)
2650 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002651free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002652{
2653 vim_free(arshape_buf);
2654}
2655# endif
2656#endif
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658/*
2659 * Draw part of the cmdline at the current cursor position. But draw stars
2660 * when cmdline_star is TRUE.
2661 */
2662 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002663draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
2665#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2666 int i;
2667
2668 if (cmdline_star > 0)
2669 for (i = 0; i < len; ++i)
2670 {
2671 msg_putchar('*');
2672# ifdef FEAT_MBYTE
2673 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002674 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675# endif
2676 }
2677 else
2678#endif
2679#ifdef FEAT_ARABIC
2680 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2681 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 static int buflen = 0;
2683 char_u *p;
2684 int j;
2685 int newlen = 0;
2686 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002687 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 int prev_c = 0;
2689 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002690 int u8c;
2691 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693
2694 /*
2695 * Do arabic shaping into a temporary buffer. This is very
2696 * inefficient!
2697 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002698 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
2700 /* Re-allocate the buffer. We keep it around to avoid a lot of
2701 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002702 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002703 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002704 arshape_buf = alloc(buflen);
2705 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 return; /* out of memory */
2707 }
2708
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002709 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2710 {
2711 /* Prepend a space to draw the leading composing char on. */
2712 arshape_buf[0] = ' ';
2713 newlen = 1;
2714 }
2715
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 for (j = start; j < start + len; j += mb_l)
2717 {
2718 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002719 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002720 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 if (ARABIC_CHAR(u8c))
2722 {
2723 /* Do Arabic shaping. */
2724 if (cmdmsg_rl)
2725 {
2726 /* displaying from right to left */
2727 pc = prev_c;
2728 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002729 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 if (j + mb_l >= start + len)
2731 nc = NUL;
2732 else
2733 nc = utf_ptr2char(p + mb_l);
2734 }
2735 else
2736 {
2737 /* displaying from left to right */
2738 if (j + mb_l >= start + len)
2739 pc = NUL;
2740 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002741 {
2742 int pcc[MAX_MCO];
2743
2744 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002746 pc1 = pcc[0];
2747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 nc = prev_c;
2749 }
2750 prev_c = u8c;
2751
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002752 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002754 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002755 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002757 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2758 if (u8cc[1] != 0)
2759 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002760 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
2762 }
2763 else
2764 {
2765 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002766 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 newlen += mb_l;
2768 }
2769 }
2770
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002771 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
2773 else
2774#endif
2775 msg_outtrans_len(ccline.cmdbuff + start, len);
2776}
2777
2778/*
2779 * Put a character on the command line. Shifts the following text to the
2780 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2781 * "c" must be printable (fit in one display cell)!
2782 */
2783 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002784putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785{
2786 if (cmd_silent)
2787 return;
2788 msg_no_more = TRUE;
2789 msg_putchar(c);
2790 if (shift)
2791 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2792 msg_no_more = FALSE;
2793 cursorcmd();
2794}
2795
2796/*
2797 * Undo a putcmdline(c, FALSE).
2798 */
2799 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002800unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 if (cmd_silent)
2803 return;
2804 msg_no_more = TRUE;
2805 if (ccline.cmdlen == ccline.cmdpos)
2806 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002807#ifdef FEAT_MBYTE
2808 else if (has_mbyte)
2809 draw_cmdline(ccline.cmdpos,
2810 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 else
2813 draw_cmdline(ccline.cmdpos, 1);
2814 msg_no_more = FALSE;
2815 cursorcmd();
2816}
2817
2818/*
2819 * Put the given string, of the given length, onto the command line.
2820 * If len is -1, then STRLEN() is used to calculate the length.
2821 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2822 * part will be redrawn, otherwise it will not. If this function is called
2823 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2824 * called afterwards.
2825 */
2826 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002827put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828{
2829 int retval;
2830 int i;
2831 int m;
2832 int c;
2833
2834 if (len < 0)
2835 len = (int)STRLEN(str);
2836
2837 /* Check if ccline.cmdbuff needs to be longer */
2838 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002839 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 else
2841 retval = OK;
2842 if (retval == OK)
2843 {
2844 if (!ccline.overstrike)
2845 {
2846 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2847 ccline.cmdbuff + ccline.cmdpos,
2848 (size_t)(ccline.cmdlen - ccline.cmdpos));
2849 ccline.cmdlen += len;
2850 }
2851 else
2852 {
2853#ifdef FEAT_MBYTE
2854 if (has_mbyte)
2855 {
2856 /* Count nr of characters in the new string. */
2857 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002858 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 ++m;
2860 /* Count nr of bytes in cmdline that are overwritten by these
2861 * characters. */
2862 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002863 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 --m;
2865 if (i < ccline.cmdlen)
2866 {
2867 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2868 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2869 ccline.cmdlen += ccline.cmdpos + len - i;
2870 }
2871 else
2872 ccline.cmdlen = ccline.cmdpos + len;
2873 }
2874 else
2875#endif
2876 if (ccline.cmdpos + len > ccline.cmdlen)
2877 ccline.cmdlen = ccline.cmdpos + len;
2878 }
2879 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2880 ccline.cmdbuff[ccline.cmdlen] = NUL;
2881
2882#ifdef FEAT_MBYTE
2883 if (enc_utf8)
2884 {
2885 /* When the inserted text starts with a composing character,
2886 * backup to the character before it. There could be two of them.
2887 */
2888 i = 0;
2889 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2890 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2891 {
2892 i = (*mb_head_off)(ccline.cmdbuff,
2893 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2894 ccline.cmdpos -= i;
2895 len += i;
2896 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2897 }
2898# ifdef FEAT_ARABIC
2899 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2900 {
2901 /* Check the previous character for Arabic combining pair. */
2902 i = (*mb_head_off)(ccline.cmdbuff,
2903 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2904 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2905 + ccline.cmdpos - i), c))
2906 {
2907 ccline.cmdpos -= i;
2908 len += i;
2909 }
2910 else
2911 i = 0;
2912 }
2913# endif
2914 if (i != 0)
2915 {
2916 /* Also backup the cursor position. */
2917 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2918 ccline.cmdspos -= i;
2919 msg_col -= i;
2920 if (msg_col < 0)
2921 {
2922 msg_col += Columns;
2923 --msg_row;
2924 }
2925 }
2926 }
2927#endif
2928
2929 if (redraw && !cmd_silent)
2930 {
2931 msg_no_more = TRUE;
2932 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002933 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2935 /* Avoid clearing the rest of the line too often. */
2936 if (cmdline_row != i || ccline.overstrike)
2937 msg_clr_eos();
2938 msg_no_more = FALSE;
2939 }
2940#ifdef FEAT_FKMAP
2941 /*
2942 * If we are in Farsi command mode, the character input must be in
2943 * Insert mode. So do not advance the cmdpos.
2944 */
2945 if (!cmd_fkmap)
2946#endif
2947 {
2948 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002951 if (m < 0) /* overflow, Columns or Rows at weird value */
2952 m = MAXCOL;
2953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 else
2955 m = MAXCOL;
2956 for (i = 0; i < len; ++i)
2957 {
2958 c = cmdline_charsize(ccline.cmdpos);
2959#ifdef FEAT_MBYTE
2960 /* count ">" for a double-wide char that doesn't fit. */
2961 if (has_mbyte)
2962 correct_cmdspos(ccline.cmdpos, c);
2963#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002964 /* Stop cursor at the end of the screen, but do increment the
2965 * insert position, so that entering a very long command
2966 * works, even though you can't see it. */
2967 if (ccline.cmdspos + c < m)
2968 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969#ifdef FEAT_MBYTE
2970 if (has_mbyte)
2971 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002972 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if (c > len - i - 1)
2974 c = len - i - 1;
2975 ccline.cmdpos += c;
2976 i += c;
2977 }
2978#endif
2979 ++ccline.cmdpos;
2980 }
2981 }
2982 }
2983 if (redraw)
2984 msg_check();
2985 return retval;
2986}
2987
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002988static struct cmdline_info prev_ccline;
2989static int prev_ccline_used = FALSE;
2990
2991/*
2992 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2993 * and overwrite it. But get_cmdline_str() may need it, thus make it
2994 * available globally in prev_ccline.
2995 */
2996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002997save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002998{
2999 if (!prev_ccline_used)
3000 {
3001 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3002 prev_ccline_used = TRUE;
3003 }
3004 *ccp = prev_ccline;
3005 prev_ccline = ccline;
3006 ccline.cmdbuff = NULL;
3007 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003008 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003009}
3010
3011/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003012 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003013 */
3014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003015restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003016{
3017 ccline = prev_ccline;
3018 prev_ccline = *ccp;
3019}
3020
Bram Moolenaar5a305422006-04-28 22:38:25 +00003021#if defined(FEAT_EVAL) || defined(PROTO)
3022/*
3023 * Save the command line into allocated memory. Returns a pointer to be
3024 * passed to restore_cmdline_alloc() later.
3025 * Returns NULL when failed.
3026 */
3027 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003028save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003029{
3030 struct cmdline_info *p;
3031
3032 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3033 if (p != NULL)
3034 save_cmdline(p);
3035 return (char_u *)p;
3036}
3037
3038/*
3039 * Restore the command line from the return value of save_cmdline_alloc().
3040 */
3041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003042restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003043{
3044 if (p != NULL)
3045 {
3046 restore_cmdline((struct cmdline_info *)p);
3047 vim_free(p);
3048 }
3049}
3050#endif
3051
Bram Moolenaar8299df92004-07-10 09:47:34 +00003052/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003053 * Paste a yank register into the command line.
3054 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003055 * insert_reg() can't be used here, because special characters from the
3056 * register contents will be interpreted as commands.
3057 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003058 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003059 */
3060 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003061cmdline_paste(
3062 int regname,
3063 int literally, /* Insert text literally instead of "as typed" */
3064 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003065{
3066 long i;
3067 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003068 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003069 int allocated;
3070 struct cmdline_info save_ccline;
3071
3072 /* check for valid regname; also accept special characters for CTRL-R in
3073 * the command line */
3074 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3075 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3076 return FAIL;
3077
3078 /* A register containing CTRL-R can cause an endless loop. Allow using
3079 * CTRL-C to break the loop. */
3080 line_breakcheck();
3081 if (got_int)
3082 return FAIL;
3083
3084#ifdef FEAT_CLIPBOARD
3085 regname = may_get_selection(regname);
3086#endif
3087
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003088 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003089 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003090 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003091 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003092 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003093 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003094 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003095
3096 if (i)
3097 {
3098 /* Got the value of a special register in "arg". */
3099 if (arg == NULL)
3100 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003101
3102 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3103 * part of the word. */
3104 p = arg;
3105 if (p_is && regname == Ctrl_W)
3106 {
3107 char_u *w;
3108 int len;
3109
3110 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003111 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003112 {
3113#ifdef FEAT_MBYTE
3114 if (has_mbyte)
3115 {
3116 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3117 if (!vim_iswordc(mb_ptr2char(w - len)))
3118 break;
3119 w -= len;
3120 }
3121 else
3122#endif
3123 {
3124 if (!vim_iswordc(w[-1]))
3125 break;
3126 --w;
3127 }
3128 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003129 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003130 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3131 p += len;
3132 }
3133
3134 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003135 if (allocated)
3136 vim_free(arg);
3137 return OK;
3138 }
3139
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003140 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003141}
3142
3143/*
3144 * Put a string on the command line.
3145 * When "literally" is TRUE, insert literally.
3146 * When "literally" is FALSE, insert as typed, but don't leave the command
3147 * line.
3148 */
3149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003150cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003151{
3152 int c, cv;
3153
3154 if (literally)
3155 put_on_cmdline(s, -1, TRUE);
3156 else
3157 while (*s != NUL)
3158 {
3159 cv = *s;
3160 if (cv == Ctrl_V && s[1])
3161 ++s;
3162#ifdef FEAT_MBYTE
3163 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003164 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003165 else
3166#endif
3167 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003168 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3169 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003170#ifdef UNIX
3171 || c == intr_char
3172#endif
3173 || (c == Ctrl_BSL && *s == Ctrl_N))
3174 stuffcharReadbuff(Ctrl_V);
3175 stuffcharReadbuff(c);
3176 }
3177}
3178
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179#ifdef FEAT_WILDMENU
3180/*
3181 * Delete characters on the command line, from "from" to the current
3182 * position.
3183 */
3184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003185cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186{
3187 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3188 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3189 ccline.cmdlen -= ccline.cmdpos - from;
3190 ccline.cmdpos = from;
3191}
3192#endif
3193
3194/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003195 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 * search
3197 */
3198 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003199redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 if (cmd_silent)
3202 return;
3203 need_wait_return = FALSE;
3204 compute_cmdrow();
3205 redrawcmd();
3206 cursorcmd();
3207}
3208
3209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003210redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 int i;
3213
3214 if (cmd_silent)
3215 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003216 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 msg_putchar(ccline.cmdfirstc);
3218 if (ccline.cmdprompt != NULL)
3219 {
3220 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3221 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3222 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003223 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 --ccline.cmdindent;
3225 }
3226 else
3227 for (i = ccline.cmdindent; i > 0; --i)
3228 msg_putchar(' ');
3229}
3230
3231/*
3232 * Redraw what is currently on the command line.
3233 */
3234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003235redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 if (cmd_silent)
3238 return;
3239
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003240 /* when 'incsearch' is set there may be no command line while redrawing */
3241 if (ccline.cmdbuff == NULL)
3242 {
3243 windgoto(cmdline_row, 0);
3244 msg_clr_eos();
3245 return;
3246 }
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 msg_start();
3249 redrawcmdprompt();
3250
3251 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3252 msg_no_more = TRUE;
3253 draw_cmdline(0, ccline.cmdlen);
3254 msg_clr_eos();
3255 msg_no_more = FALSE;
3256
3257 set_cmdspos_cursor();
3258
3259 /*
3260 * An emsg() before may have set msg_scroll. This is used in normal mode,
3261 * in cmdline mode we can reset them now.
3262 */
3263 msg_scroll = FALSE; /* next message overwrites cmdline */
3264
3265 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3266 * in cmdline mode */
3267 skip_redraw = FALSE;
3268}
3269
3270 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003271compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003273 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 cmdline_row = Rows - 1;
3275 else
3276 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3277 + W_STATUS_HEIGHT(lastwin);
3278}
3279
3280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003281cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
3283 if (cmd_silent)
3284 return;
3285
3286#ifdef FEAT_RIGHTLEFT
3287 if (cmdmsg_rl)
3288 {
3289 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3290 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3291 if (msg_row <= 0)
3292 msg_row = Rows - 1;
3293 }
3294 else
3295#endif
3296 {
3297 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3298 msg_col = ccline.cmdspos % (int)Columns;
3299 if (msg_row >= Rows)
3300 msg_row = Rows - 1;
3301 }
3302
3303 windgoto(msg_row, msg_col);
3304#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3305 redrawcmd_preedit();
3306#endif
3307#ifdef MCH_CURSOR_SHAPE
3308 mch_update_cursor();
3309#endif
3310}
3311
3312 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003313gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315 msg_start();
3316#ifdef FEAT_RIGHTLEFT
3317 if (cmdmsg_rl)
3318 msg_col = Columns - 1;
3319 else
3320#endif
3321 msg_col = 0; /* always start in column 0 */
3322 if (clr) /* clear the bottom line(s) */
3323 msg_clr_eos(); /* will reset clear_cmdline */
3324 windgoto(cmdline_row, 0);
3325}
3326
3327/*
3328 * Check the word in front of the cursor for an abbreviation.
3329 * Called when the non-id character "c" has been entered.
3330 * When an abbreviation is recognized it is removed from the text with
3331 * backspaces and the replacement string is inserted, followed by "c".
3332 */
3333 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003334ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3337 return FALSE;
3338
3339 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3340}
3341
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003342#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3343 static int
3344#ifdef __BORLANDC__
3345_RTLENTRYF
3346#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003347sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003348{
3349 char_u *p1 = *(char_u **)s1;
3350 char_u *p2 = *(char_u **)s2;
3351
3352 if (*p1 != '<' && *p2 == '<') return -1;
3353 if (*p1 == '<' && *p2 != '<') return 1;
3354 return STRCMP(p1, p2);
3355}
3356#endif
3357
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358/*
3359 * Return FAIL if this is not an appropriate context in which to do
3360 * completion of anything, return OK if it is (even if there are no matches).
3361 * For the caller, this means that the character is just passed through like a
3362 * normal character (instead of being expanded). This allows :s/^I^D etc.
3363 */
3364 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003365nextwild(
3366 expand_T *xp,
3367 int type,
3368 int options, /* extra options for ExpandOne() */
3369 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370{
3371 int i, j;
3372 char_u *p1;
3373 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 int difflen;
3375 int v;
3376
3377 if (xp->xp_numfiles == -1)
3378 {
3379 set_expand_context(xp);
3380 cmd_showtail = expand_showtail(xp);
3381 }
3382
3383 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3384 {
3385 beep_flush();
3386 return OK; /* Something illegal on command line */
3387 }
3388 if (xp->xp_context == EXPAND_NOTHING)
3389 {
3390 /* Caller can use the character as a normal char instead */
3391 return FAIL;
3392 }
3393
3394 MSG_PUTS("..."); /* show that we are busy */
3395 out_flush();
3396
3397 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003398 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
3400 if (type == WILD_NEXT || type == WILD_PREV)
3401 {
3402 /*
3403 * Get next/previous match for a previous expanded pattern.
3404 */
3405 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3406 }
3407 else
3408 {
3409 /*
3410 * Translate string into pattern and expand it.
3411 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003412 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3413 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 p2 = NULL;
3415 else
3416 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003417 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003418 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3419 if (escape)
3420 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003421
3422 if (p_wic)
3423 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003424 p2 = ExpandOne(xp, p1,
3425 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003426 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003428 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 if (p2 != NULL && type == WILD_LONGEST)
3430 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003431 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (ccline.cmdbuff[i + j] == '*'
3433 || ccline.cmdbuff[i + j] == '?')
3434 break;
3435 if ((int)STRLEN(p2) < j)
3436 {
3437 vim_free(p2);
3438 p2 = NULL;
3439 }
3440 }
3441 }
3442 }
3443
3444 if (p2 != NULL && !got_int)
3445 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003446 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003447 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003449 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 xp->xp_pattern = ccline.cmdbuff + i;
3451 }
3452 else
3453 v = OK;
3454 if (v == OK)
3455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003456 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3457 &ccline.cmdbuff[ccline.cmdpos],
3458 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3459 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 ccline.cmdlen += difflen;
3461 ccline.cmdpos += difflen;
3462 }
3463 }
3464 vim_free(p2);
3465
3466 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003467 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
3469 /* When expanding a ":map" command and no matches are found, assume that
3470 * the key is supposed to be inserted literally */
3471 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3472 return FAIL;
3473
3474 if (xp->xp_numfiles <= 0 && p2 == NULL)
3475 beep_flush();
3476 else if (xp->xp_numfiles == 1)
3477 /* free expanded pattern */
3478 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3479
3480 return OK;
3481}
3482
3483/*
3484 * Do wildcard expansion on the string 'str'.
3485 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003486 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 * Return NULL for failure.
3488 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003489 * "orig" is the originally expanded string, copied to allocated memory. It
3490 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3491 * WILD_PREV "orig" should be NULL.
3492 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003493 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3494 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 *
3496 * mode = WILD_FREE: just free previously expanded matches
3497 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3498 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3499 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3500 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3501 * mode = WILD_ALL: return all matches concatenated
3502 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003503 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 *
3505 * options = WILD_LIST_NOTFOUND: list entries without a match
3506 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3507 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3508 * options = WILD_NO_BEEP: Don't beep for multiple matches
3509 * options = WILD_ADD_SLASH: add a slash after directory names
3510 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3511 * options = WILD_SILENT: don't print warning messages
3512 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003513 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 *
3515 * The variables xp->xp_context and xp->xp_backslash must have been set!
3516 */
3517 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003518ExpandOne(
3519 expand_T *xp,
3520 char_u *str,
3521 char_u *orig, /* allocated copy of original of expanded string */
3522 int options,
3523 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
3525 char_u *ss = NULL;
3526 static int findex;
3527 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003528 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 int i;
3530 long_u len;
3531 int non_suf_match; /* number without matching suffix */
3532
3533 /*
3534 * first handle the case of using an old match
3535 */
3536 if (mode == WILD_NEXT || mode == WILD_PREV)
3537 {
3538 if (xp->xp_numfiles > 0)
3539 {
3540 if (mode == WILD_PREV)
3541 {
3542 if (findex == -1)
3543 findex = xp->xp_numfiles;
3544 --findex;
3545 }
3546 else /* mode == WILD_NEXT */
3547 ++findex;
3548
3549 /*
3550 * When wrapping around, return the original string, set findex to
3551 * -1.
3552 */
3553 if (findex < 0)
3554 {
3555 if (orig_save == NULL)
3556 findex = xp->xp_numfiles - 1;
3557 else
3558 findex = -1;
3559 }
3560 if (findex >= xp->xp_numfiles)
3561 {
3562 if (orig_save == NULL)
3563 findex = 0;
3564 else
3565 findex = -1;
3566 }
3567#ifdef FEAT_WILDMENU
3568 if (p_wmnu)
3569 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3570 findex, cmd_showtail);
3571#endif
3572 if (findex == -1)
3573 return vim_strsave(orig_save);
3574 return vim_strsave(xp->xp_files[findex]);
3575 }
3576 else
3577 return NULL;
3578 }
3579
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003580 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3582 {
3583 FreeWild(xp->xp_numfiles, xp->xp_files);
3584 xp->xp_numfiles = -1;
3585 vim_free(orig_save);
3586 orig_save = NULL;
3587 }
3588 findex = 0;
3589
3590 if (mode == WILD_FREE) /* only release file name */
3591 return NULL;
3592
3593 if (xp->xp_numfiles == -1)
3594 {
3595 vim_free(orig_save);
3596 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003597 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
3599 /*
3600 * Do the expansion.
3601 */
3602 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3603 options) == FAIL)
3604 {
3605#ifdef FNAME_ILLEGAL
3606 /* Illegal file name has been silently skipped. But when there
3607 * are wildcards, the real problem is that there was no match,
3608 * causing the pattern to be added, which has illegal characters.
3609 */
3610 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3611 EMSG2(_(e_nomatch2), str);
3612#endif
3613 }
3614 else if (xp->xp_numfiles == 0)
3615 {
3616 if (!(options & WILD_SILENT))
3617 EMSG2(_(e_nomatch2), str);
3618 }
3619 else
3620 {
3621 /* Escape the matches for use on the command line. */
3622 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3623
3624 /*
3625 * Check for matching suffixes in file names.
3626 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003627 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3628 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 {
3630 if (xp->xp_numfiles)
3631 non_suf_match = xp->xp_numfiles;
3632 else
3633 non_suf_match = 1;
3634 if ((xp->xp_context == EXPAND_FILES
3635 || xp->xp_context == EXPAND_DIRECTORIES)
3636 && xp->xp_numfiles > 1)
3637 {
3638 /*
3639 * More than one match; check suffix.
3640 * The files will have been sorted on matching suffix in
3641 * expand_wildcards, only need to check the first two.
3642 */
3643 non_suf_match = 0;
3644 for (i = 0; i < 2; ++i)
3645 if (match_suffix(xp->xp_files[i]))
3646 ++non_suf_match;
3647 }
3648 if (non_suf_match != 1)
3649 {
3650 /* Can we ever get here unless it's while expanding
3651 * interactively? If not, we can get rid of this all
3652 * together. Don't really want to wait for this message
3653 * (and possibly have to hit return to continue!).
3654 */
3655 if (!(options & WILD_SILENT))
3656 EMSG(_(e_toomany));
3657 else if (!(options & WILD_NO_BEEP))
3658 beep_flush();
3659 }
3660 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3661 ss = vim_strsave(xp->xp_files[0]);
3662 }
3663 }
3664 }
3665
3666 /* Find longest common part */
3667 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3668 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003669 int mb_len = 1;
3670 int c0, ci;
3671
3672 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003674#ifdef FEAT_MBYTE
3675 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003677 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3678 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3679 }
3680 else
3681#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003682 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003683 for (i = 1; i < xp->xp_numfiles; ++i)
3684 {
3685#ifdef FEAT_MBYTE
3686 if (has_mbyte)
3687 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3688 else
3689#endif
3690 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003691 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003693 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003694 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003696 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 break;
3698 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003699 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 break;
3701 }
3702 if (i < xp->xp_numfiles)
3703 {
3704 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003705 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 break;
3707 }
3708 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 ss = alloc((unsigned)len + 1);
3711 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003712 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 findex = -1; /* next p_wc gets first one */
3714 }
3715
3716 /* Concatenate all matching names */
3717 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3718 {
3719 len = 0;
3720 for (i = 0; i < xp->xp_numfiles; ++i)
3721 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3722 ss = lalloc(len, TRUE);
3723 if (ss != NULL)
3724 {
3725 *ss = NUL;
3726 for (i = 0; i < xp->xp_numfiles; ++i)
3727 {
3728 STRCAT(ss, xp->xp_files[i]);
3729 if (i != xp->xp_numfiles - 1)
3730 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3731 }
3732 }
3733 }
3734
3735 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3736 ExpandCleanup(xp);
3737
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003738 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003739 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003740 vim_free(orig);
3741
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return ss;
3743}
3744
3745/*
3746 * Prepare an expand structure for use.
3747 */
3748 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003749ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003751 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003752 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003754#ifndef BACKSLASH_IN_FILENAME
3755 xp->xp_shell = FALSE;
3756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 xp->xp_numfiles = -1;
3758 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003759#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3760 xp->xp_arg = NULL;
3761#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003762 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763}
3764
3765/*
3766 * Cleanup an expand structure after use.
3767 */
3768 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003769ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
3771 if (xp->xp_numfiles >= 0)
3772 {
3773 FreeWild(xp->xp_numfiles, xp->xp_files);
3774 xp->xp_numfiles = -1;
3775 }
3776}
3777
3778 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003779ExpandEscape(
3780 expand_T *xp,
3781 char_u *str,
3782 int numfiles,
3783 char_u **files,
3784 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
3786 int i;
3787 char_u *p;
3788
3789 /*
3790 * May change home directory back to "~"
3791 */
3792 if (options & WILD_HOME_REPLACE)
3793 tilde_replace(str, numfiles, files);
3794
3795 if (options & WILD_ESCAPE)
3796 {
3797 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003798 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003799 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 || xp->xp_context == EXPAND_BUFFERS
3801 || xp->xp_context == EXPAND_DIRECTORIES)
3802 {
3803 /*
3804 * Insert a backslash into a file name before a space, \, %, #
3805 * and wildmatch characters, except '~'.
3806 */
3807 for (i = 0; i < numfiles; ++i)
3808 {
3809 /* for ":set path=" we need to escape spaces twice */
3810 if (xp->xp_backslash == XP_BS_THREE)
3811 {
3812 p = vim_strsave_escaped(files[i], (char_u *)" ");
3813 if (p != NULL)
3814 {
3815 vim_free(files[i]);
3816 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003817#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 p = vim_strsave_escaped(files[i], (char_u *)" ");
3819 if (p != NULL)
3820 {
3821 vim_free(files[i]);
3822 files[i] = p;
3823 }
3824#endif
3825 }
3826 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003827#ifdef BACKSLASH_IN_FILENAME
3828 p = vim_strsave_fnameescape(files[i], FALSE);
3829#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003830 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (p != NULL)
3833 {
3834 vim_free(files[i]);
3835 files[i] = p;
3836 }
3837
3838 /* If 'str' starts with "\~", replace "~" at start of
3839 * files[i] with "\~". */
3840 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003841 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003844
3845 /* If the first file starts with a '+' escape it. Otherwise it
3846 * could be seen as "+cmd". */
3847 if (*files[0] == '+')
3848 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 }
3850 else if (xp->xp_context == EXPAND_TAGS)
3851 {
3852 /*
3853 * Insert a backslash before characters in a tag name that
3854 * would terminate the ":tag" command.
3855 */
3856 for (i = 0; i < numfiles; ++i)
3857 {
3858 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3859 if (p != NULL)
3860 {
3861 vim_free(files[i]);
3862 files[i] = p;
3863 }
3864 }
3865 }
3866 }
3867}
3868
3869/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003870 * Escape special characters in "fname" for when used as a file name argument
3871 * after a Vim command, or, when "shell" is non-zero, a shell command.
3872 * Returns the result in allocated memory.
3873 */
3874 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003875vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003876{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003877 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003878#ifdef BACKSLASH_IN_FILENAME
3879 char_u buf[20];
3880 int j = 0;
3881
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003882 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003883 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003884 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003885 buf[j++] = *p;
3886 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003887 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003888#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003889 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3890 if (shell && csh_like_shell() && p != NULL)
3891 {
3892 char_u *s;
3893
3894 /* For csh and similar shells need to put two backslashes before '!'.
3895 * One is taken by Vim, one by the shell. */
3896 s = vim_strsave_escaped(p, (char_u *)"!");
3897 vim_free(p);
3898 p = s;
3899 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003900#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003901
3902 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3903 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003904 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003905 escape_fname(&p);
3906
3907 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003908}
3909
3910/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003911 * Put a backslash before the file name in "pp", which is in allocated memory.
3912 */
3913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003914escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003915{
3916 char_u *p;
3917
3918 p = alloc((unsigned)(STRLEN(*pp) + 2));
3919 if (p != NULL)
3920 {
3921 p[0] = '\\';
3922 STRCPY(p + 1, *pp);
3923 vim_free(*pp);
3924 *pp = p;
3925 }
3926}
3927
3928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 * For each file name in files[num_files]:
3930 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3931 */
3932 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003933tilde_replace(
3934 char_u *orig_pat,
3935 int num_files,
3936 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938 int i;
3939 char_u *p;
3940
3941 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3942 {
3943 for (i = 0; i < num_files; ++i)
3944 {
3945 p = home_replace_save(NULL, files[i]);
3946 if (p != NULL)
3947 {
3948 vim_free(files[i]);
3949 files[i] = p;
3950 }
3951 }
3952 }
3953}
3954
3955/*
3956 * Show all matches for completion on the command line.
3957 * Returns EXPAND_NOTHING when the character that triggered expansion should
3958 * be inserted like a normal character.
3959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003961showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
3963#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3964 int num_files;
3965 char_u **files_found;
3966 int i, j, k;
3967 int maxlen;
3968 int lines;
3969 int columns;
3970 char_u *p;
3971 int lastlen;
3972 int attr;
3973 int showtail;
3974
3975 if (xp->xp_numfiles == -1)
3976 {
3977 set_expand_context(xp);
3978 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3979 &num_files, &files_found);
3980 showtail = expand_showtail(xp);
3981 if (i != EXPAND_OK)
3982 return i;
3983
3984 }
3985 else
3986 {
3987 num_files = xp->xp_numfiles;
3988 files_found = xp->xp_files;
3989 showtail = cmd_showtail;
3990 }
3991
3992#ifdef FEAT_WILDMENU
3993 if (!wildmenu)
3994 {
3995#endif
3996 msg_didany = FALSE; /* lines_left will be set */
3997 msg_start(); /* prepare for paging */
3998 msg_putchar('\n');
3999 out_flush();
4000 cmdline_row = msg_row;
4001 msg_didany = FALSE; /* lines_left will be set again */
4002 msg_start(); /* prepare for paging */
4003#ifdef FEAT_WILDMENU
4004 }
4005#endif
4006
4007 if (got_int)
4008 got_int = FALSE; /* only int. the completion, not the cmd line */
4009#ifdef FEAT_WILDMENU
4010 else if (wildmenu)
4011 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
4012#endif
4013 else
4014 {
4015 /* find the length of the longest file name */
4016 maxlen = 0;
4017 for (i = 0; i < num_files; ++i)
4018 {
4019 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004020 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 || xp->xp_context == EXPAND_BUFFERS))
4022 {
4023 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4024 j = vim_strsize(NameBuff);
4025 }
4026 else
4027 j = vim_strsize(L_SHOWFILE(i));
4028 if (j > maxlen)
4029 maxlen = j;
4030 }
4031
4032 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4033 lines = num_files;
4034 else
4035 {
4036 /* compute the number of columns and lines for the listing */
4037 maxlen += 2; /* two spaces between file names */
4038 columns = ((int)Columns + 2) / maxlen;
4039 if (columns < 1)
4040 columns = 1;
4041 lines = (num_files + columns - 1) / columns;
4042 }
4043
4044 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4045
4046 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4047 {
4048 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4049 msg_clr_eos();
4050 msg_advance(maxlen - 3);
4051 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4052 }
4053
4054 /* list the files line by line */
4055 for (i = 0; i < lines; ++i)
4056 {
4057 lastlen = 999;
4058 for (k = i; k < num_files; k += lines)
4059 {
4060 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4061 {
4062 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4063 p = files_found[k] + STRLEN(files_found[k]) + 1;
4064 msg_advance(maxlen + 1);
4065 msg_puts(p);
4066 msg_advance(maxlen + 3);
4067 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4068 break;
4069 }
4070 for (j = maxlen - lastlen; --j >= 0; )
4071 msg_putchar(' ');
4072 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004073 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 || xp->xp_context == EXPAND_BUFFERS)
4075 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004076 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004077 if (xp->xp_numfiles != -1)
4078 {
4079 char_u *halved_slash;
4080 char_u *exp_path;
4081
4082 /* Expansion was done before and special characters
4083 * were escaped, need to halve backslashes. Also
4084 * $HOME has been replaced with ~/. */
4085 exp_path = expand_env_save_opt(files_found[k], TRUE);
4086 halved_slash = backslash_halve_save(
4087 exp_path != NULL ? exp_path : files_found[k]);
4088 j = mch_isdir(halved_slash != NULL ? halved_slash
4089 : files_found[k]);
4090 vim_free(exp_path);
4091 vim_free(halved_slash);
4092 }
4093 else
4094 /* Expansion was done here, file names are literal. */
4095 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 if (showtail)
4097 p = L_SHOWFILE(k);
4098 else
4099 {
4100 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4101 TRUE);
4102 p = NameBuff;
4103 }
4104 }
4105 else
4106 {
4107 j = FALSE;
4108 p = L_SHOWFILE(k);
4109 }
4110 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4111 }
4112 if (msg_col > 0) /* when not wrapped around */
4113 {
4114 msg_clr_eos();
4115 msg_putchar('\n');
4116 }
4117 out_flush(); /* show one line at a time */
4118 if (got_int)
4119 {
4120 got_int = FALSE;
4121 break;
4122 }
4123 }
4124
4125 /*
4126 * we redraw the command below the lines that we have just listed
4127 * This is a bit tricky, but it saves a lot of screen updating.
4128 */
4129 cmdline_row = msg_row; /* will put it back later */
4130 }
4131
4132 if (xp->xp_numfiles == -1)
4133 FreeWild(num_files, files_found);
4134
4135 return EXPAND_OK;
4136}
4137
4138/*
4139 * Private gettail for showmatches() (and win_redr_status_matches()):
4140 * Find tail of file name path, but ignore trailing "/".
4141 */
4142 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004143sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144{
4145 char_u *p;
4146 char_u *t = s;
4147 int had_sep = FALSE;
4148
4149 for (p = s; *p != NUL; )
4150 {
4151 if (vim_ispathsep(*p)
4152#ifdef BACKSLASH_IN_FILENAME
4153 && !rem_backslash(p)
4154#endif
4155 )
4156 had_sep = TRUE;
4157 else if (had_sep)
4158 {
4159 t = p;
4160 had_sep = FALSE;
4161 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004162 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 }
4164 return t;
4165}
4166
4167/*
4168 * Return TRUE if we only need to show the tail of completion matches.
4169 * When not completing file names or there is a wildcard in the path FALSE is
4170 * returned.
4171 */
4172 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004173expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
4175 char_u *s;
4176 char_u *end;
4177
4178 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004179 if (xp->xp_context != EXPAND_FILES
4180 && xp->xp_context != EXPAND_SHELLCMD
4181 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return FALSE;
4183
4184 end = gettail(xp->xp_pattern);
4185 if (end == xp->xp_pattern) /* there is no path separator */
4186 return FALSE;
4187
4188 for (s = xp->xp_pattern; s < end; s++)
4189 {
4190 /* Skip escaped wildcards. Only when the backslash is not a path
4191 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4192 if (rem_backslash(s))
4193 ++s;
4194 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4195 return FALSE;
4196 }
4197 return TRUE;
4198}
4199
4200/*
4201 * Prepare a string for expansion.
4202 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004203 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 * When expanding other names: The string will be used with regcomp(). Copy
4205 * the name into allocated memory and prepend "^".
4206 */
4207 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004208addstar(
4209 char_u *fname,
4210 int len,
4211 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
4213 char_u *retval;
4214 int i, j;
4215 int new_len;
4216 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004217 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004219 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004220 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004221 && context != EXPAND_SHELLCMD
4222 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 {
4224 /*
4225 * Matching will be done internally (on something other than files).
4226 * So we convert the file-matching-type wildcards into our kind for
4227 * use with vim_regcomp(). First work out how long it will be:
4228 */
4229
4230 /* For help tags the translation is done in find_help_tags().
4231 * For a tag pattern starting with "/" no translation is needed. */
4232 if (context == EXPAND_HELP
4233 || context == EXPAND_COLORS
4234 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004235 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004236 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 || (context == EXPAND_TAGS && fname[0] == '/'))
4238 retval = vim_strnsave(fname, len);
4239 else
4240 {
4241 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4242 for (i = 0; i < len; i++)
4243 {
4244 if (fname[i] == '*' || fname[i] == '~')
4245 new_len++; /* '*' needs to be replaced by ".*"
4246 '~' needs to be replaced by "\~" */
4247
4248 /* Buffer names are like file names. "." should be literal */
4249 if (context == EXPAND_BUFFERS && fname[i] == '.')
4250 new_len++; /* "." becomes "\." */
4251
4252 /* Custom expansion takes care of special things, match
4253 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004254 if ((context == EXPAND_USER_DEFINED
4255 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 new_len++; /* '\' becomes "\\" */
4257 }
4258 retval = alloc(new_len);
4259 if (retval != NULL)
4260 {
4261 retval[0] = '^';
4262 j = 1;
4263 for (i = 0; i < len; i++, j++)
4264 {
4265 /* Skip backslash. But why? At least keep it for custom
4266 * expansion. */
4267 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004268 && context != EXPAND_USER_LIST
4269 && fname[i] == '\\'
4270 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 break;
4272
4273 switch (fname[i])
4274 {
4275 case '*': retval[j++] = '.';
4276 break;
4277 case '~': retval[j++] = '\\';
4278 break;
4279 case '?': retval[j] = '.';
4280 continue;
4281 case '.': if (context == EXPAND_BUFFERS)
4282 retval[j++] = '\\';
4283 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004284 case '\\': if (context == EXPAND_USER_DEFINED
4285 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 retval[j++] = '\\';
4287 break;
4288 }
4289 retval[j] = fname[i];
4290 }
4291 retval[j] = NUL;
4292 }
4293 }
4294 }
4295 else
4296 {
4297 retval = alloc(len + 4);
4298 if (retval != NULL)
4299 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004300 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
4302 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004303 * Don't add a star to *, ~, ~user, $var or `cmd`.
4304 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 * ~ would be at the start of the file name, but not the tail.
4306 * $ could be anywhere in the tail.
4307 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004308 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 */
4310 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004311 ends_in_star = (len > 0 && retval[len - 1] == '*');
4312#ifndef BACKSLASH_IN_FILENAME
4313 for (i = len - 2; i >= 0; --i)
4314 {
4315 if (retval[i] != '\\')
4316 break;
4317 ends_in_star = !ends_in_star;
4318 }
4319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004321 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 && vim_strchr(tail, '$') == NULL
4323 && vim_strchr(retval, '`') == NULL)
4324 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004325 else if (len > 0 && retval[len - 1] == '$')
4326 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 retval[len] = NUL;
4328 }
4329 }
4330 return retval;
4331}
4332
4333/*
4334 * Must parse the command line so far to work out what context we are in.
4335 * Completion can then be done based on that context.
4336 * This routine sets the variables:
4337 * xp->xp_pattern The start of the pattern to be expanded within
4338 * the command line (ends at the cursor).
4339 * xp->xp_context The type of thing to expand. Will be one of:
4340 *
4341 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4342 * the command line, like an unknown command. Caller
4343 * should beep.
4344 * EXPAND_NOTHING Unrecognised context for completion, use char like
4345 * a normal char, rather than for completion. eg
4346 * :s/^I/
4347 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4348 * it.
4349 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4350 * EXPAND_FILES After command with XFILE set, or after setting
4351 * with P_EXPAND set. eg :e ^I, :w>>^I
4352 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4353 * when we know only directories are of interest. eg
4354 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004355 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4357 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4358 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4359 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4360 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4361 * EXPAND_EVENTS Complete event names
4362 * EXPAND_SYNTAX Complete :syntax command arguments
4363 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4364 * EXPAND_AUGROUP Complete autocommand group names
4365 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4366 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4367 * eg :unmap a^I , :cunab x^I
4368 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4369 * eg :call sub^I
4370 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4371 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4372 * names in expressions, eg :while s^I
4373 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004374 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 */
4376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004377set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004379 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 if (ccline.cmdfirstc != ':'
4381#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004382 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004383 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384#endif
4385 )
4386 {
4387 xp->xp_context = EXPAND_NOTHING;
4388 return;
4389 }
4390 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4391}
4392
4393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004394set_cmd_context(
4395 expand_T *xp,
4396 char_u *str, /* start of command line */
4397 int len, /* length of command line (excl. NUL) */
4398 int col) /* position of cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
4400 int old_char = NUL;
4401 char_u *nextcomm;
4402
4403 /*
4404 * Avoid a UMR warning from Purify, only save the character if it has been
4405 * written before.
4406 */
4407 if (col < len)
4408 old_char = str[col];
4409 str[col] = NUL;
4410 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004411
4412#ifdef FEAT_EVAL
4413 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004414 {
4415# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004416 /* pass CMD_SIZE because there is no real command */
4417 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004418# endif
4419 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004420 else if (ccline.input_fn)
4421 {
4422 xp->xp_context = ccline.xp_context;
4423 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004424# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004425 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004426# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004427 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004428 else
4429#endif
4430 while (nextcomm != NULL)
4431 nextcomm = set_one_cmd_context(xp, nextcomm);
4432
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004433 /* Store the string here so that call_user_expand_func() can get to them
4434 * easily. */
4435 xp->xp_line = str;
4436 xp->xp_col = col;
4437
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 str[col] = old_char;
4439}
4440
4441/*
4442 * Expand the command line "str" from context "xp".
4443 * "xp" must have been set by set_cmd_context().
4444 * xp->xp_pattern points into "str", to where the text that is to be expanded
4445 * starts.
4446 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4447 * cursor.
4448 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4449 * key that triggered expansion literally.
4450 * Returns EXPAND_OK otherwise.
4451 */
4452 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004453expand_cmdline(
4454 expand_T *xp,
4455 char_u *str, /* start of command line */
4456 int col, /* position of cursor */
4457 int *matchcount, /* return: nr of matches */
4458 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
4460 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004461 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
4463 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4464 {
4465 beep_flush();
4466 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4467 }
4468 if (xp->xp_context == EXPAND_NOTHING)
4469 {
4470 /* Caller can use the character as a normal char instead */
4471 return EXPAND_NOTHING;
4472 }
4473
4474 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004475 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4476 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (file_str == NULL)
4478 return EXPAND_UNSUCCESSFUL;
4479
Bram Moolenaar94950a92010-12-02 16:01:29 +01004480 if (p_wic)
4481 options += WILD_ICASE;
4482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004484 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 *matchcount = 0;
4487 *matches = NULL;
4488 }
4489 vim_free(file_str);
4490
4491 return EXPAND_OK;
4492}
4493
4494#ifdef FEAT_MULTI_LANG
4495/*
4496 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4497 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004498static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
4500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004501cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502{
4503 int i, j;
4504 int len;
4505
4506 for (i = 0; i < num_file; ++i)
4507 {
4508 len = (int)STRLEN(file[i]) - 3;
4509 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4510 {
4511 /* Sorting on priority means the same item in another language may
4512 * be anywhere. Search all items for a match up to the "@en". */
4513 for (j = 0; j < num_file; ++j)
4514 if (j != i
4515 && (int)STRLEN(file[j]) == len + 3
4516 && STRNCMP(file[i], file[j], len + 1) == 0)
4517 break;
4518 if (j == num_file)
4519 file[i][len] = NUL;
4520 }
4521 }
4522}
4523#endif
4524
4525/*
4526 * Do the expansion based on xp->xp_context and "pat".
4527 */
4528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004529ExpandFromContext(
4530 expand_T *xp,
4531 char_u *pat,
4532 int *num_file,
4533 char_u ***file,
4534 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535{
4536#ifdef FEAT_CMDL_COMPL
4537 regmatch_T regmatch;
4538#endif
4539 int ret;
4540 int flags;
4541
4542 flags = EW_DIR; /* include directories */
4543 if (options & WILD_LIST_NOTFOUND)
4544 flags |= EW_NOTFOUND;
4545 if (options & WILD_ADD_SLASH)
4546 flags |= EW_ADDSLASH;
4547 if (options & WILD_KEEP_ALL)
4548 flags |= EW_KEEPALL;
4549 if (options & WILD_SILENT)
4550 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004551 if (options & WILD_ALLLINKS)
4552 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004554 if (xp->xp_context == EXPAND_FILES
4555 || xp->xp_context == EXPAND_DIRECTORIES
4556 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
4558 /*
4559 * Expand file or directory names.
4560 */
4561 int free_pat = FALSE;
4562 int i;
4563
4564 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4565 * space */
4566 if (xp->xp_backslash != XP_BS_NONE)
4567 {
4568 free_pat = TRUE;
4569 pat = vim_strsave(pat);
4570 for (i = 0; pat[i]; ++i)
4571 if (pat[i] == '\\')
4572 {
4573 if (xp->xp_backslash == XP_BS_THREE
4574 && pat[i + 1] == '\\'
4575 && pat[i + 2] == '\\'
4576 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004577 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 if (xp->xp_backslash == XP_BS_ONE
4579 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004580 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 }
4583
4584 if (xp->xp_context == EXPAND_FILES)
4585 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004586 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4587 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 else
4589 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004590 if (options & WILD_ICASE)
4591 flags |= EW_ICASE;
4592
Bram Moolenaard7834d32009-12-02 16:14:36 +00004593 /* Expand wildcards, supporting %:h and the like. */
4594 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 if (free_pat)
4596 vim_free(pat);
4597 return ret;
4598 }
4599
4600 *file = (char_u **)"";
4601 *num_file = 0;
4602 if (xp->xp_context == EXPAND_HELP)
4603 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004604 /* With an empty argument we would get all the help tags, which is
4605 * very slow. Get matches for "help" instead. */
4606 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4607 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
4609#ifdef FEAT_MULTI_LANG
4610 cleanup_help_tags(*num_file, *file);
4611#endif
4612 return OK;
4613 }
4614 return FAIL;
4615 }
4616
4617#ifndef FEAT_CMDL_COMPL
4618 return FAIL;
4619#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004620 if (xp->xp_context == EXPAND_SHELLCMD)
4621 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (xp->xp_context == EXPAND_OLD_SETTING)
4623 return ExpandOldSetting(num_file, file);
4624 if (xp->xp_context == EXPAND_BUFFERS)
4625 return ExpandBufnames(pat, num_file, file, options);
4626 if (xp->xp_context == EXPAND_TAGS
4627 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4628 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4629 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004630 {
4631 char *directories[] = {"colors", NULL};
4632 return ExpandRTDir(pat, num_file, file, directories);
4633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004635 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004636 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004637 return ExpandRTDir(pat, num_file, file, directories);
4638 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004639 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004640 {
4641 char *directories[] = {"syntax", NULL};
4642 return ExpandRTDir(pat, num_file, file, directories);
4643 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004644 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004645 {
4646 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4647 return ExpandRTDir(pat, num_file, file, directories);
4648 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004649# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4650 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004651 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4655 if (regmatch.regprog == NULL)
4656 return FAIL;
4657
4658 /* set ignore-case according to p_ic, p_scs and pat */
4659 regmatch.rm_ic = ignorecase(pat);
4660
4661 if (xp->xp_context == EXPAND_SETTINGS
4662 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4663 ret = ExpandSettings(xp, &regmatch, num_file, file);
4664 else if (xp->xp_context == EXPAND_MAPPINGS)
4665 ret = ExpandMappings(&regmatch, num_file, file);
4666# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4667 else if (xp->xp_context == EXPAND_USER_DEFINED)
4668 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4669# endif
4670 else
4671 {
4672 static struct expgen
4673 {
4674 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004675 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004677 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 } tab[] =
4679 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004680 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4681 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004682#ifdef FEAT_CMDHIST
4683 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004686 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004687 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004688 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4689 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4690 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691#endif
4692#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004693 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4694 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4695 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4696 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#endif
4698#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004699 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4700 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701#endif
4702#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004703 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004705#ifdef FEAT_PROFILE
4706 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4707#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004708 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004710 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4711 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004713#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004714 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004715#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004716#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004717 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004718#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004719#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004720 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4723 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004724 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4725 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004727 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004728 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 };
4730 int i;
4731
4732 /*
4733 * Find a context in the table and call the ExpandGeneric() with the
4734 * right function to do the expansion.
4735 */
4736 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004737 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 if (xp->xp_context == tab[i].context)
4739 {
4740 if (tab[i].ic)
4741 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004742 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004743 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 break;
4745 }
4746 }
4747
Bram Moolenaar473de612013-06-08 18:19:48 +02004748 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749
4750 return ret;
4751#endif /* FEAT_CMDL_COMPL */
4752}
4753
4754#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4755/*
4756 * Expand a list of names.
4757 *
4758 * Generic function for command line completion. It calls a function to
4759 * obtain strings, one by one. The strings are matched against a regexp
4760 * program. Matching strings are copied into an array, which is returned.
4761 *
4762 * Returns OK when no problems encountered, FAIL for error (out of memory).
4763 */
4764 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004765ExpandGeneric(
4766 expand_T *xp,
4767 regmatch_T *regmatch,
4768 int *num_file,
4769 char_u ***file,
4770 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004772 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
4774 int i;
4775 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004776 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 char_u *str;
4778
4779 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004780 * round == 0: count the number of matching names
4781 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004783 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
4785 for (i = 0; ; ++i)
4786 {
4787 str = (*func)(xp, i);
4788 if (str == NULL) /* end of list */
4789 break;
4790 if (*str == NUL) /* skip empty strings */
4791 continue;
4792
4793 if (vim_regexec(regmatch, str, (colnr_T)0))
4794 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004795 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004797 if (escaped)
4798 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4799 else
4800 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 (*file)[count] = str;
4802#ifdef FEAT_MENU
4803 if (func == get_menu_names && str != NULL)
4804 {
4805 /* test for separator added by get_menu_names() */
4806 str += STRLEN(str) - 1;
4807 if (*str == '\001')
4808 *str = '.';
4809 }
4810#endif
4811 }
4812 ++count;
4813 }
4814 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004815 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 if (count == 0)
4818 return OK;
4819 *num_file = count;
4820 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4821 if (*file == NULL)
4822 {
4823 *file = (char_u **)"";
4824 return FAIL;
4825 }
4826 count = 0;
4827 }
4828 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004829
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004830 /* Sort the results. Keep menu's in the specified order. */
4831 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004832 {
4833 if (xp->xp_context == EXPAND_EXPRESSION
4834 || xp->xp_context == EXPAND_FUNCTIONS
4835 || xp->xp_context == EXPAND_USER_FUNC)
4836 /* <SNR> functions should be sorted to the end. */
4837 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4838 sort_func_compare);
4839 else
4840 sort_strings(*file, *num_file);
4841 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004842
Bram Moolenaar4f688582007-07-24 12:34:30 +00004843#ifdef FEAT_CMDL_COMPL
4844 /* Reset the variables used for special highlight names expansion, so that
4845 * they don't show up when getting normal highlight names by ID. */
4846 reset_expand_highlight();
4847#endif
4848
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 return OK;
4850}
4851
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004852/*
4853 * Complete a shell command.
4854 * Returns FAIL or OK;
4855 */
4856 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004857expand_shellcmd(
4858 char_u *filepat, /* pattern to match with command names */
4859 int *num_file, /* return: number of matches */
4860 char_u ***file, /* return: array with matches */
4861 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004862{
4863 char_u *pat;
4864 int i;
4865 char_u *path;
4866 int mustfree = FALSE;
4867 garray_T ga;
4868 char_u *buf = alloc(MAXPATHL);
4869 size_t l;
4870 char_u *s, *e;
4871 int flags = flagsarg;
4872 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01004873 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004874
4875 if (buf == NULL)
4876 return FAIL;
4877
4878 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4879 * space */
4880 pat = vim_strsave(filepat);
4881 for (i = 0; pat[i]; ++i)
4882 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004883 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004884
Bram Moolenaarb5971142015-03-21 17:32:19 +01004885 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004886
4887 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004888 if (mch_isFullName(pat))
4889 path = (char_u *)" ";
4890 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004891 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4892 path = (char_u *)".";
4893 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004894 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004895 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004896 if (path == NULL)
4897 path = (char_u *)"";
4898 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004899
4900 /*
4901 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01004902 * collect them in "ga". When "." is not in $PATH also expand for the
4903 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004904 */
4905 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01004906 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004907 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01004908 if (*s == NUL)
4909 {
4910 if (did_curdir)
4911 break;
4912 /* Find directories in the current directory, path is empty. */
4913 did_curdir = TRUE;
4914 }
4915 else if (*s == '.')
4916 did_curdir = TRUE;
4917
Bram Moolenaar68c31742006-09-02 15:54:18 +00004918 if (*s == ' ')
4919 ++s; /* Skip space used for absolute path name. */
4920
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004921#if defined(MSDOS) || defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004922 e = vim_strchr(s, ';');
4923#else
4924 e = vim_strchr(s, ':');
4925#endif
4926 if (e == NULL)
4927 e = s + STRLEN(s);
4928
4929 l = e - s;
4930 if (l > MAXPATHL - 5)
4931 break;
4932 vim_strncpy(buf, s, l);
4933 add_pathsep(buf);
4934 l = STRLEN(buf);
4935 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4936
4937 /* Expand matches in one directory of $PATH. */
4938 ret = expand_wildcards(1, &buf, num_file, file, flags);
4939 if (ret == OK)
4940 {
4941 if (ga_grow(&ga, *num_file) == FAIL)
4942 FreeWild(*num_file, *file);
4943 else
4944 {
4945 for (i = 0; i < *num_file; ++i)
4946 {
4947 s = (*file)[i];
4948 if (STRLEN(s) > l)
4949 {
4950 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004951 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004952 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4953 }
4954 else
4955 vim_free(s);
4956 }
4957 vim_free(*file);
4958 }
4959 }
4960 if (*e != NUL)
4961 ++e;
4962 }
4963 *file = ga.ga_data;
4964 *num_file = ga.ga_len;
4965
4966 vim_free(buf);
4967 vim_free(pat);
4968 if (mustfree)
4969 vim_free(path);
4970 return OK;
4971}
4972
4973
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004975static 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 +00004976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004978 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004979 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004981 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004982call_user_expand_func(
4983 void *(*user_expand_func)(char_u *, int, char_u **, int),
4984 expand_T *xp,
4985 int *num_file,
4986 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02004988 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004989 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004992 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004993 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
Bram Moolenaarb7515462013-06-29 12:58:33 +02004995 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004996 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 *num_file = 0;
4998 *file = NULL;
4999
Bram Moolenaarb7515462013-06-29 12:58:33 +02005000 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005001 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005002 keep = ccline.cmdbuff[ccline.cmdlen];
5003 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005004 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005005
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005006 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005007 args[1] = xp->xp_line;
5008 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 args[2] = num;
5010
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005011 /* Save the cmdline, we don't know what the function may do. */
5012 save_ccline = ccline;
5013 ccline.cmdbuff = NULL;
5014 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005016
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005017 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005018
5019 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005021 if (ccline.cmdbuff != NULL)
5022 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005023
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005024 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005025 return ret;
5026}
5027
5028/*
5029 * Expand names with a function defined by the user.
5030 */
5031 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005032ExpandUserDefined(
5033 expand_T *xp,
5034 regmatch_T *regmatch,
5035 int *num_file,
5036 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005037{
5038 char_u *retstr;
5039 char_u *s;
5040 char_u *e;
5041 char_u keep;
5042 garray_T ga;
5043
5044 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5045 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 return FAIL;
5047
5048 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005049 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 {
5051 e = vim_strchr(s, '\n');
5052 if (e == NULL)
5053 e = s + STRLEN(s);
5054 keep = *e;
5055 *e = 0;
5056
5057 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5058 {
5059 *e = keep;
5060 if (*e != NUL)
5061 ++e;
5062 continue;
5063 }
5064
5065 if (ga_grow(&ga, 1) == FAIL)
5066 break;
5067
5068 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5069 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 *e = keep;
5072 if (*e != NUL)
5073 ++e;
5074 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005075 vim_free(retstr);
5076 *file = ga.ga_data;
5077 *num_file = ga.ga_len;
5078 return OK;
5079}
5080
5081/*
5082 * Expand names with a list returned by a function defined by the user.
5083 */
5084 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005085ExpandUserList(
5086 expand_T *xp,
5087 int *num_file,
5088 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005089{
5090 list_T *retlist;
5091 listitem_T *li;
5092 garray_T ga;
5093
5094 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5095 if (retlist == NULL)
5096 return FAIL;
5097
5098 ga_init2(&ga, (int)sizeof(char *), 3);
5099 /* Loop over the items in the list. */
5100 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5101 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005102 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5103 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005104
5105 if (ga_grow(&ga, 1) == FAIL)
5106 break;
5107
5108 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005109 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005110 ++ga.ga_len;
5111 }
5112 list_unref(retlist);
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 *file = ga.ga_data;
5115 *num_file = ga.ga_len;
5116 return OK;
5117}
5118#endif
5119
5120/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005121 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005122 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005123 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 */
5125 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005126ExpandRTDir(
5127 char_u *pat,
5128 int *num_file,
5129 char_u ***file,
5130 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 char_u *s;
5133 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005134 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005136 int i;
5137 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 *num_file = 0;
5140 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005141 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005142 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005144 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005146 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5147 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005149 ga_clear_strings(&ga);
5150 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005152 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005153 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005154 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005156
5157 for (i = 0; i < ga.ga_len; ++i)
5158 {
5159 match = ((char_u **)ga.ga_data)[i];
5160 s = match;
5161 e = s + STRLEN(s);
5162 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5163 {
5164 e -= 4;
5165 for (s = e; s > match; mb_ptr_back(match, s))
5166 if (s < match || vim_ispathsep(*s))
5167 break;
5168 ++s;
5169 *e = NUL;
5170 mch_memmove(match, s, e - s + 1);
5171 }
5172 }
5173
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005174 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005175 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005176
5177 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005178 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005179 remove_duplicates(&ga);
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 *file = ga.ga_data;
5182 *num_file = ga.ga_len;
5183 return OK;
5184}
5185
5186#endif
5187
5188#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5189/*
5190 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005191 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005193 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005194globpath(
5195 char_u *path,
5196 char_u *file,
5197 garray_T *ga,
5198 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 expand_T xpc;
5201 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 int num_p;
5204 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
5206 buf = alloc(MAXPATHL);
5207 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005208 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005210 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 /* Loop over all entries in {path}. */
5214 while (*path != NUL)
5215 {
5216 /* Copy one item of the path to buf[] and concatenate the file name. */
5217 copy_option_part(&path, buf, MAXPATHL, ",");
5218 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5219 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005220# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005221 /* Using the platform's path separator (\) makes vim incorrectly
5222 * treat it as an escape character, use '/' instead. */
5223 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5224 STRCAT(buf, "/");
5225# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005229 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5230 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005232 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005234 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 for (i = 0; i < num_p; ++i)
5237 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005238 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005239 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005240 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 FreeWild(num_p, p);
5245 }
5246 }
5247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
5249 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250}
5251
5252#endif
5253
5254#if defined(FEAT_CMDHIST) || defined(PROTO)
5255
5256/*********************************
5257 * Command line history stuff *
5258 *********************************/
5259
5260/*
5261 * Translate a history character to the associated type number.
5262 */
5263 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005264hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
5266 if (c == ':')
5267 return HIST_CMD;
5268 if (c == '=')
5269 return HIST_EXPR;
5270 if (c == '@')
5271 return HIST_INPUT;
5272 if (c == '>')
5273 return HIST_DEBUG;
5274 return HIST_SEARCH; /* must be '?' or '/' */
5275}
5276
5277/*
5278 * Table of history names.
5279 * These names are used in :history and various hist...() functions.
5280 * It is sufficient to give the significant prefix of a history name.
5281 */
5282
5283static char *(history_names[]) =
5284{
5285 "cmd",
5286 "search",
5287 "expr",
5288 "input",
5289 "debug",
5290 NULL
5291};
5292
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005293#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5294/*
5295 * Function given to ExpandGeneric() to obtain the possible first
5296 * arguments of the ":history command.
5297 */
5298 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005299get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005300{
5301 static char_u compl[2] = { NUL, NUL };
5302 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005303 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005304 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5305
5306 if (idx < short_names_count)
5307 {
5308 compl[0] = (char_u)short_names[idx];
5309 return compl;
5310 }
5311 if (idx < short_names_count + history_name_count)
5312 return (char_u *)history_names[idx - short_names_count];
5313 if (idx == short_names_count + history_name_count)
5314 return (char_u *)"all";
5315 return NULL;
5316}
5317#endif
5318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319/*
5320 * init_history() - Initialize the command line history.
5321 * Also used to re-allocate the history when the size changes.
5322 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005323 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005324init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326 int newlen; /* new length of history table */
5327 histentry_T *temp;
5328 int i;
5329 int j;
5330 int type;
5331
5332 /*
5333 * If size of history table changed, reallocate it
5334 */
5335 newlen = (int)p_hi;
5336 if (newlen != hislen) /* history length changed */
5337 {
5338 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5339 {
5340 if (newlen)
5341 {
5342 temp = (histentry_T *)lalloc(
5343 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5344 if (temp == NULL) /* out of memory! */
5345 {
5346 if (type == 0) /* first one: just keep the old length */
5347 {
5348 newlen = hislen;
5349 break;
5350 }
5351 /* Already changed one table, now we can only have zero
5352 * length for all tables. */
5353 newlen = 0;
5354 type = -1;
5355 continue;
5356 }
5357 }
5358 else
5359 temp = NULL;
5360 if (newlen == 0 || temp != NULL)
5361 {
5362 if (hisidx[type] < 0) /* there are no entries yet */
5363 {
5364 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005365 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367 else if (newlen > hislen) /* array becomes bigger */
5368 {
5369 for (i = 0; i <= hisidx[type]; ++i)
5370 temp[i] = history[type][i];
5371 j = i;
5372 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005373 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 for ( ; j < hislen; ++i, ++j)
5375 temp[i] = history[type][j];
5376 }
5377 else /* array becomes smaller or 0 */
5378 {
5379 j = hisidx[type];
5380 for (i = newlen - 1; ; --i)
5381 {
5382 if (i >= 0) /* copy newest entries */
5383 temp[i] = history[type][j];
5384 else /* remove older entries */
5385 vim_free(history[type][j].hisstr);
5386 if (--j < 0)
5387 j = hislen - 1;
5388 if (j == hisidx[type])
5389 break;
5390 }
5391 hisidx[type] = newlen - 1;
5392 }
5393 vim_free(history[type]);
5394 history[type] = temp;
5395 }
5396 }
5397 hislen = newlen;
5398 }
5399}
5400
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005402clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005403{
5404 hisptr->hisnum = 0;
5405 hisptr->viminfo = FALSE;
5406 hisptr->hisstr = NULL;
5407}
5408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409/*
5410 * Check if command line 'str' is already in history.
5411 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5412 */
5413 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005414in_history(
5415 int type,
5416 char_u *str,
5417 int move_to_front, /* Move the entry to the front if it exists */
5418 int sep,
5419 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
5421 int i;
5422 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005423 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
5425 if (hisidx[type] < 0)
5426 return FALSE;
5427 i = hisidx[type];
5428 do
5429 {
5430 if (history[type][i].hisstr == NULL)
5431 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005432
5433 /* For search history, check that the separator character matches as
5434 * well. */
5435 p = history[type][i].hisstr;
5436 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005437 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005438 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
5440 if (!move_to_front)
5441 return TRUE;
5442 last_i = i;
5443 break;
5444 }
5445 if (--i < 0)
5446 i = hislen - 1;
5447 } while (i != hisidx[type]);
5448
5449 if (last_i >= 0)
5450 {
5451 str = history[type][i].hisstr;
5452 while (i != hisidx[type])
5453 {
5454 if (++i >= hislen)
5455 i = 0;
5456 history[type][last_i] = history[type][i];
5457 last_i = i;
5458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005460 history[type][i].viminfo = FALSE;
5461 history[type][i].hisstr = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 return TRUE;
5463 }
5464 return FALSE;
5465}
5466
5467/*
5468 * Convert history name (from table above) to its HIST_ equivalent.
5469 * When "name" is empty, return "cmd" history.
5470 * Returns -1 for unknown history name.
5471 */
5472 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005473get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
5475 int i;
5476 int len = (int)STRLEN(name);
5477
5478 /* No argument: use current history. */
5479 if (len == 0)
5480 return hist_char2type(ccline.cmdfirstc);
5481
5482 for (i = 0; history_names[i] != NULL; ++i)
5483 if (STRNICMP(name, history_names[i], len) == 0)
5484 return i;
5485
5486 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5487 return hist_char2type(name[0]);
5488
5489 return -1;
5490}
5491
5492static int last_maptick = -1; /* last seen maptick */
5493
5494/*
5495 * Add the given string to the given history. If the string is already in the
5496 * history then it is moved to the front. "histype" may be one of he HIST_
5497 * values.
5498 */
5499 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005500add_to_history(
5501 int histype,
5502 char_u *new_entry,
5503 int in_map, /* consider maptick when inside a mapping */
5504 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 histentry_T *hisptr;
5507 int len;
5508
5509 if (hislen == 0) /* no history */
5510 return;
5511
Bram Moolenaara939e432013-11-09 05:30:26 +01005512 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5513 return;
5514
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 /*
5516 * Searches inside the same mapping overwrite each other, so that only
5517 * the last line is kept. Be careful not to remove a line that was moved
5518 * down, only lines that were added.
5519 */
5520 if (histype == HIST_SEARCH && in_map)
5521 {
5522 if (maptick == last_maptick)
5523 {
5524 /* Current line is from the same mapping, remove it */
5525 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5526 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005527 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 --hisnum[histype];
5529 if (--hisidx[HIST_SEARCH] < 0)
5530 hisidx[HIST_SEARCH] = hislen - 1;
5531 }
5532 last_maptick = -1;
5533 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005534 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
5536 if (++hisidx[histype] == hislen)
5537 hisidx[histype] = 0;
5538 hisptr = &history[histype][hisidx[histype]];
5539 vim_free(hisptr->hisstr);
5540
5541 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005542 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5544 if (hisptr->hisstr != NULL)
5545 hisptr->hisstr[len + 1] = sep;
5546
5547 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005548 hisptr->viminfo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 if (histype == HIST_SEARCH && in_map)
5550 last_maptick = maptick;
5551 }
5552}
5553
5554#if defined(FEAT_EVAL) || defined(PROTO)
5555
5556/*
5557 * Get identifier of newest history entry.
5558 * "histype" may be one of the HIST_ values.
5559 */
5560 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005561get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
5563 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5564 || hisidx[histype] < 0)
5565 return -1;
5566
5567 return history[histype][hisidx[histype]].hisnum;
5568}
5569
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005570static struct cmdline_info *get_ccline_ptr(void);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005571
5572/*
5573 * Get pointer to the command line info to use. cmdline_paste() may clear
5574 * ccline and put the previous value in prev_ccline.
5575 */
5576 static struct cmdline_info *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577get_ccline_ptr(void)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005578{
5579 if ((State & CMDLINE) == 0)
5580 return NULL;
5581 if (ccline.cmdbuff != NULL)
5582 return &ccline;
5583 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5584 return &prev_ccline;
5585 return NULL;
5586}
5587
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588/*
5589 * Get the current command line in allocated memory.
5590 * Only works when the command line is being edited.
5591 * Returns NULL when something is wrong.
5592 */
5593 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005594get_cmdline_str(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005596 struct cmdline_info *p = get_ccline_ptr();
5597
5598 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005600 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601}
5602
5603/*
5604 * Get the current command line position, counted in bytes.
5605 * Zero is the first position.
5606 * Only works when the command line is being edited.
5607 * Returns -1 when something is wrong.
5608 */
5609 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610get_cmdline_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005612 struct cmdline_info *p = get_ccline_ptr();
5613
5614 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005616 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617}
5618
5619/*
5620 * Set the command line byte position to "pos". Zero is the first position.
5621 * Only works when the command line is being edited.
5622 * Returns 1 when failed, 0 when OK.
5623 */
5624 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625set_cmdline_pos(
5626 int pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005628 struct cmdline_info *p = get_ccline_ptr();
5629
5630 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 return 1;
5632
5633 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5634 * changed the command line. */
5635 if (pos < 0)
5636 new_cmdpos = 0;
5637 else
5638 new_cmdpos = pos;
5639 return 0;
5640}
5641
5642/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005643 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005644 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005645 * Only works when the command line is being edited.
5646 * Returns NUL when something is wrong.
5647 */
5648 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005649get_cmdline_type(void)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005650{
5651 struct cmdline_info *p = get_ccline_ptr();
5652
5653 if (p == NULL)
5654 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005655 if (p->cmdfirstc == NUL)
5656 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005657 return p->cmdfirstc;
5658}
5659
5660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 * Calculate history index from a number:
5662 * num > 0: seen as identifying number of a history entry
5663 * num < 0: relative position in history wrt newest entry
5664 * "histype" may be one of the HIST_ values.
5665 */
5666 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005667calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
5669 int i;
5670 histentry_T *hist;
5671 int wrapped = FALSE;
5672
5673 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5674 || (i = hisidx[histype]) < 0 || num == 0)
5675 return -1;
5676
5677 hist = history[histype];
5678 if (num > 0)
5679 {
5680 while (hist[i].hisnum > num)
5681 if (--i < 0)
5682 {
5683 if (wrapped)
5684 break;
5685 i += hislen;
5686 wrapped = TRUE;
5687 }
5688 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5689 return i;
5690 }
5691 else if (-num <= hislen)
5692 {
5693 i += num + 1;
5694 if (i < 0)
5695 i += hislen;
5696 if (hist[i].hisstr != NULL)
5697 return i;
5698 }
5699 return -1;
5700}
5701
5702/*
5703 * Get a history entry by its index.
5704 * "histype" may be one of the HIST_ values.
5705 */
5706 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005707get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 idx = calc_hist_idx(histype, idx);
5710 if (idx >= 0)
5711 return history[histype][idx].hisstr;
5712 else
5713 return (char_u *)"";
5714}
5715
5716/*
5717 * Clear all entries of a history.
5718 * "histype" may be one of the HIST_ values.
5719 */
5720 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005721clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 int i;
5724 histentry_T *hisptr;
5725
5726 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5727 {
5728 hisptr = history[histype];
5729 for (i = hislen; i--;)
5730 {
5731 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005732 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734 hisidx[histype] = -1; /* mark history as cleared */
5735 hisnum[histype] = 0; /* reset identifier counter */
5736 return OK;
5737 }
5738 return FAIL;
5739}
5740
5741/*
5742 * Remove all entries matching {str} from a history.
5743 * "histype" may be one of the HIST_ values.
5744 */
5745 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005746del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 regmatch_T regmatch;
5749 histentry_T *hisptr;
5750 int idx;
5751 int i;
5752 int last;
5753 int found = FALSE;
5754
5755 regmatch.regprog = NULL;
5756 regmatch.rm_ic = FALSE; /* always match case */
5757 if (hislen != 0
5758 && histype >= 0
5759 && histype < HIST_COUNT
5760 && *str != NUL
5761 && (idx = hisidx[histype]) >= 0
5762 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5763 != NULL)
5764 {
5765 i = last = idx;
5766 do
5767 {
5768 hisptr = &history[histype][i];
5769 if (hisptr->hisstr == NULL)
5770 break;
5771 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5772 {
5773 found = TRUE;
5774 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005775 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 }
5777 else
5778 {
5779 if (i != last)
5780 {
5781 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005782 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
5784 if (--last < 0)
5785 last += hislen;
5786 }
5787 if (--i < 0)
5788 i += hislen;
5789 } while (i != idx);
5790 if (history[histype][idx].hisstr == NULL)
5791 hisidx[histype] = -1;
5792 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005793 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 return found;
5795}
5796
5797/*
5798 * Remove an indexed entry from a history.
5799 * "histype" may be one of the HIST_ values.
5800 */
5801 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005802del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
5804 int i, j;
5805
5806 i = calc_hist_idx(histype, idx);
5807 if (i < 0)
5808 return FALSE;
5809 idx = hisidx[histype];
5810 vim_free(history[histype][i].hisstr);
5811
5812 /* When deleting the last added search string in a mapping, reset
5813 * last_maptick, so that the last added search string isn't deleted again.
5814 */
5815 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5816 last_maptick = -1;
5817
5818 while (i != idx)
5819 {
5820 j = (i + 1) % hislen;
5821 history[histype][i] = history[histype][j];
5822 i = j;
5823 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005824 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 if (--i < 0)
5826 i += hislen;
5827 hisidx[histype] = i;
5828 return TRUE;
5829}
5830
5831#endif /* FEAT_EVAL */
5832
5833#if defined(FEAT_CRYPT) || defined(PROTO)
5834/*
5835 * Very specific function to remove the value in ":set key=val" from the
5836 * history.
5837 */
5838 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005839remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
5841 char_u *p;
5842 int i;
5843
5844 i = hisidx[HIST_CMD];
5845 if (i < 0)
5846 return;
5847 p = history[HIST_CMD][i].hisstr;
5848 if (p != NULL)
5849 for ( ; *p; ++p)
5850 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5851 {
5852 p = vim_strchr(p + 3, '=');
5853 if (p == NULL)
5854 break;
5855 ++p;
5856 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5857 if (p[i] == '\\' && p[i + 1])
5858 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005859 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 --p;
5861 }
5862}
5863#endif
5864
5865#endif /* FEAT_CMDHIST */
5866
5867#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5868/*
5869 * Get indices "num1,num2" that specify a range within a list (not a range of
5870 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5871 * Returns OK if parsed successfully, otherwise FAIL.
5872 */
5873 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005874get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 int len;
5877 int first = FALSE;
5878 long num;
5879
5880 *str = skipwhite(*str);
5881 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5882 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005883 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 *str += len;
5885 *num1 = (int)num;
5886 first = TRUE;
5887 }
5888 *str = skipwhite(*str);
5889 if (**str == ',') /* parse "to" part of range */
5890 {
5891 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005892 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 if (len > 0)
5894 {
5895 *num2 = (int)num;
5896 *str = skipwhite(*str + len);
5897 }
5898 else if (!first) /* no number given at all */
5899 return FAIL;
5900 }
5901 else if (first) /* only one number given */
5902 *num2 = *num1;
5903 return OK;
5904}
5905#endif
5906
5907#if defined(FEAT_CMDHIST) || defined(PROTO)
5908/*
5909 * :history command - print a history
5910 */
5911 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005912ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
5914 histentry_T *hist;
5915 int histype1 = HIST_CMD;
5916 int histype2 = HIST_CMD;
5917 int hisidx1 = 1;
5918 int hisidx2 = -1;
5919 int idx;
5920 int i, j, k;
5921 char_u *end;
5922 char_u *arg = eap->arg;
5923
5924 if (hislen == 0)
5925 {
5926 MSG(_("'history' option is zero"));
5927 return;
5928 }
5929
5930 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5931 {
5932 end = arg;
5933 while (ASCII_ISALPHA(*end)
5934 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5935 end++;
5936 i = *end;
5937 *end = NUL;
5938 histype1 = get_histtype(arg);
5939 if (histype1 == -1)
5940 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005941 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {
5943 histype1 = 0;
5944 histype2 = HIST_COUNT-1;
5945 }
5946 else
5947 {
5948 *end = i;
5949 EMSG(_(e_trailing));
5950 return;
5951 }
5952 }
5953 else
5954 histype2 = histype1;
5955 *end = i;
5956 }
5957 else
5958 end = arg;
5959 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5960 {
5961 EMSG(_(e_trailing));
5962 return;
5963 }
5964
5965 for (; !got_int && histype1 <= histype2; ++histype1)
5966 {
5967 STRCPY(IObuff, "\n # ");
5968 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5969 MSG_PUTS_TITLE(IObuff);
5970 idx = hisidx[histype1];
5971 hist = history[histype1];
5972 j = hisidx1;
5973 k = hisidx2;
5974 if (j < 0)
5975 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5976 if (k < 0)
5977 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5978 if (idx >= 0 && j <= k)
5979 for (i = idx + 1; !got_int; ++i)
5980 {
5981 if (i == hislen)
5982 i = 0;
5983 if (hist[i].hisstr != NULL
5984 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5985 {
5986 msg_putchar('\n');
5987 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5988 hist[i].hisnum);
5989 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5990 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005991 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 else
5993 STRCAT(IObuff, hist[i].hisstr);
5994 msg_outtrans(IObuff);
5995 out_flush();
5996 }
5997 if (i == idx)
5998 break;
5999 }
6000 }
6001}
6002#endif
6003
6004#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006005/*
6006 * Buffers for history read from a viminfo file. Only valid while reading.
6007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
6009static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
6010static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
6011static int viminfo_add_at_front = FALSE;
6012
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006013static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014
6015/*
6016 * Translate a history type number to the associated character.
6017 */
6018 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006019hist_type2char(
6020 int type,
6021 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022{
6023 if (type == HIST_CMD)
6024 return ':';
6025 if (type == HIST_SEARCH)
6026 {
6027 if (use_question)
6028 return '?';
6029 else
6030 return '/';
6031 }
6032 if (type == HIST_EXPR)
6033 return '=';
6034 return '@';
6035}
6036
6037/*
6038 * Prepare for reading the history from the viminfo file.
6039 * This allocates history arrays to store the read history lines.
6040 */
6041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006042prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 int i;
6045 int num;
6046 int type;
6047 int len;
6048
6049 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006050 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 if (asklen > hislen)
6052 asklen = hislen;
6053
6054 for (type = 0; type < HIST_COUNT; ++type)
6055 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006056 /* Count the number of empty spaces in the history list. Entries read
6057 * from viminfo previously are also considered empty. If there are
6058 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006060 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 num++;
6062 len = asklen;
6063 if (num > len)
6064 len = num;
6065 if (len <= 0)
6066 viminfo_history[type] = NULL;
6067 else
6068 viminfo_history[type] =
6069 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6070 if (viminfo_history[type] == NULL)
6071 len = 0;
6072 viminfo_hislen[type] = len;
6073 viminfo_hisidx[type] = 0;
6074 }
6075}
6076
6077/*
6078 * Accept a line from the viminfo, store it in the history array when it's
6079 * new.
6080 */
6081 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006082read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
6084 int type;
6085 long_u len;
6086 char_u *val;
6087 char_u *p;
6088
6089 type = hist_char2type(virp->vir_line[0]);
6090 if (viminfo_hisidx[type] < viminfo_hislen[type])
6091 {
6092 val = viminfo_readstring(virp, 1, TRUE);
6093 if (val != NULL && *val != NUL)
6094 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006095 int sep = (*val == ' ' ? NUL : *val);
6096
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006098 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 {
6100 /* Need to re-allocate to append the separator byte. */
6101 len = STRLEN(val);
6102 p = lalloc(len + 2, TRUE);
6103 if (p != NULL)
6104 {
6105 if (type == HIST_SEARCH)
6106 {
6107 /* Search entry: Move the separator from the first
6108 * column to after the NUL. */
6109 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006110 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 }
6112 else
6113 {
6114 /* Not a search entry: No separator in the viminfo
6115 * file, add a NUL separator. */
6116 mch_memmove(p, val, (size_t)len + 1);
6117 p[len + 1] = NUL;
6118 }
6119 viminfo_history[type][viminfo_hisidx[type]++] = p;
6120 }
6121 }
6122 }
6123 vim_free(val);
6124 }
6125 return viminfo_readline(virp);
6126}
6127
Bram Moolenaar07219f92013-04-14 23:19:36 +02006128/*
6129 * Finish reading history lines from viminfo. Not used when writing viminfo.
6130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006132finish_viminfo_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
6134 int idx;
6135 int i;
6136 int type;
6137
6138 for (type = 0; type < HIST_COUNT; ++type)
6139 {
6140 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006141 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 idx = hisidx[type] + viminfo_hisidx[type];
6143 if (idx >= hislen)
6144 idx -= hislen;
6145 else if (idx < 0)
6146 idx = hislen - 1;
6147 if (viminfo_add_at_front)
6148 hisidx[type] = idx;
6149 else
6150 {
6151 if (hisidx[type] == -1)
6152 hisidx[type] = hislen - 1;
6153 do
6154 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006155 if (history[type][idx].hisstr != NULL
6156 || history[type][idx].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 break;
6158 if (++idx == hislen)
6159 idx = 0;
6160 } while (idx != hisidx[type]);
6161 if (idx != hisidx[type] && --idx < 0)
6162 idx = hislen - 1;
6163 }
6164 for (i = 0; i < viminfo_hisidx[type]; i++)
6165 {
6166 vim_free(history[type][idx].hisstr);
6167 history[type][idx].hisstr = viminfo_history[type][i];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006168 history[type][idx].viminfo = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 if (--idx < 0)
6170 idx = hislen - 1;
6171 }
6172 idx += 1;
6173 idx %= hislen;
6174 for (i = 0; i < viminfo_hisidx[type]; i++)
6175 {
6176 history[type][idx++].hisnum = ++hisnum[type];
6177 idx %= hislen;
6178 }
6179 vim_free(viminfo_history[type]);
6180 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006181 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 }
6183}
6184
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006185/*
6186 * Write history to viminfo file in "fp".
6187 * When "merge" is TRUE merge history lines with a previously read viminfo
6188 * file, data is in viminfo_history[].
6189 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006192write_viminfo_history(
6193 FILE *fp,
6194 int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 int i;
6197 int type;
6198 int num_saved;
6199 char_u *p;
6200 int c;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006201 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202
6203 init_history();
6204 if (hislen == 0)
6205 return;
6206 for (type = 0; type < HIST_COUNT; ++type)
6207 {
6208 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6209 if (num_saved == 0)
6210 continue;
6211 if (num_saved < 0) /* Use default */
6212 num_saved = hislen;
6213 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6214 type == HIST_CMD ? _("Command Line") :
6215 type == HIST_SEARCH ? _("Search String") :
6216 type == HIST_EXPR ? _("Expression") :
6217 _("Input Line"));
6218 if (num_saved > hislen)
6219 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006220
6221 /*
6222 * Merge typed and viminfo history:
6223 * round 1: history of typed commands.
6224 * round 2: history from recently read viminfo.
6225 */
6226 for (round = 1; round <= 2; ++round)
6227 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006228 if (round == 1)
6229 /* start at newest entry, somewhere in the list */
6230 i = hisidx[type];
6231 else if (viminfo_hisidx[type] > 0)
6232 /* start at newest entry, first in the list */
6233 i = 0;
6234 else
6235 /* empty list */
6236 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006237 if (i >= 0)
6238 while (num_saved > 0
6239 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006241 p = round == 1 ? history[type][i].hisstr
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006242 : viminfo_history[type] == NULL ? NULL
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006243 : viminfo_history[type][i];
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006244 if (p != NULL && (round == 2
6245 || !merge
6246 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006248 --num_saved;
6249 fputc(hist_type2char(type, TRUE), fp);
6250 /* For the search history: put the separator in the
6251 * second column; use a space if there isn't one. */
6252 if (type == HIST_SEARCH)
6253 {
6254 c = p[STRLEN(p) + 1];
6255 putc(c == NUL ? ' ' : c, fp);
6256 }
6257 viminfo_writestring(fp, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006259 if (round == 1)
6260 {
6261 /* Decrement index, loop around and stop when back at
6262 * the start. */
6263 if (--i < 0)
6264 i = hislen - 1;
6265 if (i == hisidx[type])
6266 break;
6267 }
6268 else
6269 {
6270 /* Increment index. Stop at the end in the while. */
6271 ++i;
6272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006274 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006275 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006276 if (viminfo_history[type] != NULL)
6277 vim_free(viminfo_history[type][i]);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006278 vim_free(viminfo_history[type]);
6279 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006280 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 }
6282}
6283#endif /* FEAT_VIMINFO */
6284
6285#if defined(FEAT_FKMAP) || defined(PROTO)
6286/*
6287 * Write a character at the current cursor+offset position.
6288 * It is directly written into the command buffer block.
6289 */
6290 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6294 {
6295 EMSG(_("E198: cmd_pchar beyond the command length"));
6296 return;
6297 }
6298 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6299 ccline.cmdbuff[ccline.cmdlen] = NUL;
6300}
6301
6302 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006303cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304{
6305 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6306 {
6307 /* EMSG(_("cmd_gchar beyond the command length")); */
6308 return NUL;
6309 }
6310 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6311}
6312#endif
6313
6314#if defined(FEAT_CMDWIN) || defined(PROTO)
6315/*
6316 * Open a window on the current command line and history. Allow editing in
6317 * the window. Returns when the window is closed.
6318 * Returns:
6319 * CR if the command is to be executed
6320 * Ctrl_C if it is to be abandoned
6321 * K_IGNORE if editing continues
6322 */
6323 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006324ex_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 struct cmdline_info save_ccline;
6327 buf_T *old_curbuf = curbuf;
6328 win_T *old_curwin = curwin;
6329 buf_T *bp;
6330 win_T *wp;
6331 int i;
6332 linenr_T lnum;
6333 int histtype;
6334 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006335#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 int save_restart_edit = restart_edit;
6339 int save_State = State;
6340 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006341#ifdef FEAT_RIGHTLEFT
6342 int save_cmdmsg_rl = cmdmsg_rl;
6343#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006344#ifdef FEAT_FOLDING
6345 int save_KeyTyped;
6346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347
6348 /* Can't do this recursively. Can't do it when typing a password. */
6349 if (cmdwin_type != 0
6350# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6351 || cmdline_star > 0
6352# endif
6353 )
6354 {
6355 beep_flush();
6356 return K_IGNORE;
6357 }
6358
6359 /* Save current window sizes. */
6360 win_size_save(&winsizes);
6361
6362# ifdef FEAT_AUTOCMD
6363 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006364 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006366 /* don't use a new tab page */
6367 cmdmod.tab = 0;
6368
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 /* Create a window for the command-line buffer. */
6370 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6371 {
6372 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006373# ifdef FEAT_AUTOCMD
6374 unblock_autocmds();
6375# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 return K_IGNORE;
6377 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006378 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
6380 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006381 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006382 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6384 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6385 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006386#ifdef FEAT_FOLDING
6387 curwin->w_p_fen = FALSE;
6388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006390 curwin->w_p_rl = cmdmsg_rl;
6391 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006393 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
6395# ifdef FEAT_AUTOCMD
6396 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006397 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398# endif
6399
Bram Moolenaar46152342005-09-07 21:18:43 +00006400 /* Showing the prompt may have set need_wait_return, reset it. */
6401 need_wait_return = FALSE;
6402
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006403 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6405 {
6406 if (p_wc == TAB)
6407 {
6408 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6409 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6410 }
6411 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6412 }
6413
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006414 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6415 * sets 'textwidth' to 78). */
6416 curbuf->b_p_tw = 0;
6417
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 /* Fill the buffer with the history. */
6419 init_history();
6420 if (hislen > 0)
6421 {
6422 i = hisidx[histtype];
6423 if (i >= 0)
6424 {
6425 lnum = 0;
6426 do
6427 {
6428 if (++i == hislen)
6429 i = 0;
6430 if (history[histtype][i].hisstr != NULL)
6431 ml_append(lnum++, history[histtype][i].hisstr,
6432 (colnr_T)0, FALSE);
6433 }
6434 while (i != hisidx[histtype]);
6435 }
6436 }
6437
6438 /* Replace the empty last line with the current command-line and put the
6439 * cursor there. */
6440 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6441 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6442 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006443 changed_line_abv_curs();
6444 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006445 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446
6447 /* Save the command line info, can be used recursively. */
6448 save_ccline = ccline;
6449 ccline.cmdbuff = NULL;
6450 ccline.cmdprompt = NULL;
6451
6452 /* No Ex mode here! */
6453 exmode_active = 0;
6454
6455 State = NORMAL;
6456# ifdef FEAT_MOUSE
6457 setmouse();
6458# endif
6459
6460# ifdef FEAT_AUTOCMD
6461 /* Trigger CmdwinEnter autocommands. */
6462 typestr[0] = cmdwin_type;
6463 typestr[1] = NUL;
6464 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006465 if (restart_edit != 0) /* autocmd with ":startinsert" */
6466 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467# endif
6468
6469 i = RedrawingDisabled;
6470 RedrawingDisabled = 0;
6471
6472 /*
6473 * Call the main loop until <CR> or CTRL-C is typed.
6474 */
6475 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006476 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477
6478 RedrawingDisabled = i;
6479
6480# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006481
6482# ifdef FEAT_FOLDING
6483 save_KeyTyped = KeyTyped;
6484# endif
6485
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 /* Trigger CmdwinLeave autocommands. */
6487 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006488
6489# ifdef FEAT_FOLDING
6490 /* Restore KeyTyped in case it is modified by autocommands */
6491 KeyTyped = save_KeyTyped;
6492# endif
6493
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494# endif
6495
Bram Moolenaarccc18222007-05-10 18:25:20 +00006496 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 ccline = save_ccline;
6498 cmdwin_type = 0;
6499
6500 exmode_active = save_exmode;
6501
Bram Moolenaarf9821062008-06-20 16:31:07 +00006502 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 * this happens! */
6504 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6505 {
6506 cmdwin_result = Ctrl_C;
6507 EMSG(_("E199: Active window or buffer deleted"));
6508 }
6509 else
6510 {
6511# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6512 /* autocmds may abort script processing */
6513 if (aborting() && cmdwin_result != K_IGNORE)
6514 cmdwin_result = Ctrl_C;
6515# endif
6516 /* Set the new command line from the cmdline buffer. */
6517 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006518 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006520 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6521
6522 if (histtype == HIST_CMD)
6523 {
6524 /* Execute the command directly. */
6525 ccline.cmdbuff = vim_strsave((char_u *)p);
6526 cmdwin_result = CAR;
6527 }
6528 else
6529 {
6530 /* First need to cancel what we were doing. */
6531 ccline.cmdbuff = NULL;
6532 stuffcharReadbuff(':');
6533 stuffReadbuff((char_u *)p);
6534 stuffcharReadbuff(CAR);
6535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 }
6537 else if (cmdwin_result == K_XF2) /* :qa typed */
6538 {
6539 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6540 cmdwin_result = CAR;
6541 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006542 else if (cmdwin_result == Ctrl_C)
6543 {
6544 /* :q or :close, don't execute any command
6545 * and don't modify the cmd window. */
6546 ccline.cmdbuff = NULL;
6547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 else
6549 ccline.cmdbuff = vim_strsave(ml_get_curline());
6550 if (ccline.cmdbuff == NULL)
6551 cmdwin_result = Ctrl_C;
6552 else
6553 {
6554 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6555 ccline.cmdbufflen = ccline.cmdlen + 1;
6556 ccline.cmdpos = curwin->w_cursor.col;
6557 if (ccline.cmdpos > ccline.cmdlen)
6558 ccline.cmdpos = ccline.cmdlen;
6559 if (cmdwin_result == K_IGNORE)
6560 {
6561 set_cmdspos_cursor();
6562 redrawcmd();
6563 }
6564 }
6565
6566# ifdef FEAT_AUTOCMD
6567 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006568 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02006570# ifdef FEAT_CONCEAL
6571 /* Avoid command-line window first character being concealed. */
6572 curwin->w_p_cole = 0;
6573# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 wp = curwin;
6575 bp = curbuf;
6576 win_goto(old_curwin);
6577 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006578
6579 /* win_close() may have already wiped the buffer when 'bh' is
6580 * set to 'wipe' */
6581 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006582 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
6584 /* Restore window sizes. */
6585 win_size_restore(&winsizes);
6586
6587# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006588 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589# endif
6590 }
6591
6592 ga_clear(&winsizes);
6593 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006594# ifdef FEAT_RIGHTLEFT
6595 cmdmsg_rl = save_cmdmsg_rl;
6596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
6598 State = save_State;
6599# ifdef FEAT_MOUSE
6600 setmouse();
6601# endif
6602
6603 return cmdwin_result;
6604}
6605#endif /* FEAT_CMDWIN */
6606
6607/*
6608 * Used for commands that either take a simple command string argument, or:
6609 * cmd << endmarker
6610 * {script}
6611 * endmarker
6612 * Returns a pointer to allocated memory with {script} or NULL.
6613 */
6614 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006615script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 char_u *theline;
6618 char *end_pattern = NULL;
6619 char dot[] = ".";
6620 garray_T ga;
6621
6622 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6623 return NULL;
6624
6625 ga_init2(&ga, 1, 0x400);
6626
6627 if (cmd[2] != NUL)
6628 end_pattern = (char *)skipwhite(cmd + 2);
6629 else
6630 end_pattern = dot;
6631
6632 for (;;)
6633 {
6634 theline = eap->getline(
6635#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006636 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637#endif
6638 NUL, eap->cookie, 0);
6639
6640 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006641 {
6642 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645
6646 ga_concat(&ga, theline);
6647 ga_append(&ga, '\n');
6648 vim_free(theline);
6649 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006650 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652 return (char_u *)ga.ga_data;
6653}