blob: 642e090107bc682bcacf85206551ea9a49abfbd1 [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 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020061 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000062} histentry_T;
63
64static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
65static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
66static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
67 /* identifying (unique) number of newest history entry */
68static int hislen = 0; /* actual length of history tables */
69
Bram Moolenaard25c16e2016-01-29 22:13:30 +010070static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
Bram Moolenaard25c16e2016-01-29 22:13:30 +010072static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075# endif
76#endif
77
78#ifdef FEAT_RIGHTLEFT
79static int cmd_hkmap = 0; /* Hebrew mapping during command line */
80#endif
81
82#ifdef FEAT_FKMAP
83static int cmd_fkmap = 0; /* Farsi mapping during command line */
84#endif
85
Bram Moolenaard25c16e2016-01-29 22:13:30 +010086static int cmdline_charsize(int idx);
87static void set_cmdspos(void);
88static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void alloc_cmdbuff(int len);
93static int realloc_cmdbuff(int len);
94static void draw_cmdline(int start, int len);
95static void save_cmdline(struct cmdline_info *ccp);
96static void restore_cmdline(struct cmdline_info *ccp);
97static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010099static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
101#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100102static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100104static void redrawcmdprompt(void);
105static void cursorcmd(void);
106static int ccheck_abbr(int);
107static int nextwild(expand_T *xp, int type, int options, int escape);
108static void escape_fname(char_u **pp);
109static int showmatches(expand_T *xp, int wildmenu);
110static void set_expand_context(expand_T *xp);
111static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
112static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100114static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100115static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100116static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200117# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100121static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
122static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123# endif
124#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200125#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_CMDWIN
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static int ex_window(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200133#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
134static int
135#ifdef __BORLANDC__
136_RTLENTRYF
137#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100138sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200139#endif
140
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141/*
142 * getcmdline() - accept a command line starting with firstc.
143 *
144 * firstc == ':' get ":" command line.
145 * firstc == '/' or '?' get search pattern
146 * firstc == '=' get expression
147 * firstc == '@' get text for input() function
148 * firstc == '>' get text for debug mode
149 * firstc == NUL get text for :insert command
150 * firstc == -1 like NUL, and break on CTRL-C
151 *
152 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
153 * command line.
154 *
155 * Careful: getcmdline() can be called recursively!
156 *
157 * Return pointer to allocated string if there is a commandline, NULL
158 * otherwise.
159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100161getcmdline(
162 int firstc,
163 long count UNUSED, /* only used for incremental search */
164 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165{
166 int c;
167 int i;
168 int j;
169 int gotesc = FALSE; /* TRUE when <ESC> just typed */
170 int do_abbr; /* when TRUE check for abbr. */
171#ifdef FEAT_CMDHIST
172 char_u *lookfor = NULL; /* string to match */
173 int hiscnt; /* current history line in use */
174 int histype; /* history type to be used */
175#endif
176#ifdef FEAT_SEARCH_EXTRA
177 pos_T old_cursor;
178 colnr_T old_curswant;
179 colnr_T old_leftcol;
180 linenr_T old_topline;
181# ifdef FEAT_DIFF
182 int old_topfill;
183# endif
184 linenr_T old_botline;
185 int did_incsearch = FALSE;
186 int incsearch_postponed = FALSE;
187#endif
188 int did_wild_list = FALSE; /* did wild_list() recently */
189 int wim_index = 0; /* index in wim_flags[] */
190 int res;
191 int save_msg_scroll = msg_scroll;
192 int save_State = State; /* remember State when called */
193 int some_key_typed = FALSE; /* one of the keys was typed */
194#ifdef FEAT_MOUSE
195 /* mouse drag and release events are ignored, unless they are
196 * preceded with a mouse down event */
197 int ignore_drag_release = TRUE;
198#endif
199#ifdef FEAT_EVAL
200 int break_ctrl_c = FALSE;
201#endif
202 expand_T xpc;
203 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000204#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
205 /* Everything that may work recursively should save and restore the
206 * current command line in save_ccline. That includes update_screen(), a
207 * custom status line may invoke ":normal". */
208 struct cmdline_info save_ccline;
209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211#ifdef FEAT_EVAL
212 if (firstc == -1)
213 {
214 firstc = NUL;
215 break_ctrl_c = TRUE;
216 }
217#endif
218#ifdef FEAT_RIGHTLEFT
219 /* start without Hebrew mapping for a command line */
220 if (firstc == ':' || firstc == '=' || firstc == '>')
221 cmd_hkmap = 0;
222#endif
223
224 ccline.overstrike = FALSE; /* always start in insert mode */
225#ifdef FEAT_SEARCH_EXTRA
226 old_cursor = curwin->w_cursor; /* needs to be restored later */
227 old_curswant = curwin->w_curswant;
228 old_leftcol = curwin->w_leftcol;
229 old_topline = curwin->w_topline;
230# ifdef FEAT_DIFF
231 old_topfill = curwin->w_topfill;
232# endif
233 old_botline = curwin->w_botline;
234#endif
235
236 /*
237 * set some variables for redrawcmd()
238 */
239 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000240 ccline.cmdindent = (firstc > 0 ? indent : 0);
241
242 /* alloc initial ccline.cmdbuff */
243 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 if (ccline.cmdbuff == NULL)
245 return NULL; /* out of memory */
246 ccline.cmdlen = ccline.cmdpos = 0;
247 ccline.cmdbuff[0] = NUL;
248
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000249 /* autoindent for :insert and :append */
250 if (firstc <= 0)
251 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200252 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000253 ccline.cmdbuff[indent] = NUL;
254 ccline.cmdpos = indent;
255 ccline.cmdspos = indent;
256 ccline.cmdlen = indent;
257 }
258
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000260 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
262#ifdef FEAT_RIGHTLEFT
263 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
264 && (firstc == '/' || firstc == '?'))
265 cmdmsg_rl = TRUE;
266 else
267 cmdmsg_rl = FALSE;
268#endif
269
270 redir_off = TRUE; /* don't redirect the typed command */
271 if (!cmd_silent)
272 {
273 i = msg_scrolled;
274 msg_scrolled = 0; /* avoid wait_return message */
275 gotocmdline(TRUE);
276 msg_scrolled += i;
277 redrawcmdprompt(); /* draw prompt or indent */
278 set_cmdspos();
279 }
280 xpc.xp_context = EXPAND_NOTHING;
281 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000282#ifndef BACKSLASH_IN_FILENAME
283 xpc.xp_shell = FALSE;
284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000286#if defined(FEAT_EVAL)
287 if (ccline.input_fn)
288 {
289 xpc.xp_context = ccline.xp_context;
290 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000291# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000292 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000293# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000294 }
295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 /*
298 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
299 * doing ":@0" when register 0 doesn't contain a CR.
300 */
301 msg_scroll = FALSE;
302
303 State = CMDLINE;
304
305 if (firstc == '/' || firstc == '?' || firstc == '@')
306 {
307 /* Use ":lmap" mappings for search pattern and input(). */
308 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
309 b_im_ptr = &curbuf->b_p_iminsert;
310 else
311 b_im_ptr = &curbuf->b_p_imsearch;
312 if (*b_im_ptr == B_IMODE_LMAP)
313 State |= LANGMAP;
314#ifdef USE_IM_CONTROL
315 im_set_active(*b_im_ptr == B_IMODE_IM);
316#endif
317 }
318#ifdef USE_IM_CONTROL
319 else if (p_imcmdline)
320 im_set_active(TRUE);
321#endif
322
323#ifdef FEAT_MOUSE
324 setmouse();
325#endif
326#ifdef CURSOR_SHAPE
327 ui_cursor_shape(); /* may show different cursor shape */
328#endif
329
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000330 /* When inside an autocommand for writing "exiting" may be set and
331 * terminal mode set to cooked. Need to set raw mode here then. */
332 settmode(TMODE_RAW);
333
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#ifdef FEAT_CMDHIST
335 init_history();
336 hiscnt = hislen; /* set hiscnt to impossible history value */
337 histype = hist_char2type(firstc);
338#endif
339
340#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000341 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#endif
343
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200344 /* If something above caused an error, reset the flags, we do want to type
345 * and execute commands. Display may be messed up a bit. */
346 if (did_emsg)
347 redrawcmd();
348 did_emsg = FALSE;
349 got_int = FALSE;
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 /*
352 * Collect the command string, handling editing keys.
353 */
354 for (;;)
355 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000356 redir_off = TRUE; /* Don't redirect the typed command.
357 Repeated, because a ":redir" inside
358 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef USE_ON_FLY_SCROLL
360 dont_scroll = FALSE; /* allow scrolling here */
361#endif
362 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
363
364 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000365
366 /* Get a character. Ignore K_IGNORE, it should not do anything, such
367 * as stop completion. */
368 do
369 {
370 c = safe_vgetc();
371 } while (c == K_IGNORE);
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373 if (KeyTyped)
374 {
375 some_key_typed = TRUE;
376#ifdef FEAT_RIGHTLEFT
377 if (cmd_hkmap)
378 c = hkmap(c);
379# ifdef FEAT_FKMAP
380 if (cmd_fkmap)
381 c = cmdl_fkmap(c);
382# endif
383 if (cmdmsg_rl && !KeyStuffed)
384 {
385 /* Invert horizontal movements and operations. Only when
386 * typed by the user directly, not when the result of a
387 * mapping. */
388 switch (c)
389 {
390 case K_RIGHT: c = K_LEFT; break;
391 case K_S_RIGHT: c = K_S_LEFT; break;
392 case K_C_RIGHT: c = K_C_LEFT; break;
393 case K_LEFT: c = K_RIGHT; break;
394 case K_S_LEFT: c = K_S_RIGHT; break;
395 case K_C_LEFT: c = K_C_RIGHT; break;
396 }
397 }
398#endif
399 }
400
401 /*
402 * Ignore got_int when CTRL-C was typed here.
403 * Don't ignore it in :global, we really need to break then, e.g., for
404 * ":g/pat/normal /pat" (without the <CR>).
405 * Don't ignore it for the input() function.
406 */
407 if ((c == Ctrl_C
408#ifdef UNIX
409 || c == intr_char
410#endif
411 )
412#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
413 && firstc != '@'
414#endif
415#ifdef FEAT_EVAL
416 && !break_ctrl_c
417#endif
418 && !global_busy)
419 got_int = FALSE;
420
421#ifdef FEAT_CMDHIST
422 /* free old command line when finished moving around in the history
423 * list */
424 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000425 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000426 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 && c != K_PAGEDOWN && c != K_PAGEUP
428 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000429 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
431 {
432 vim_free(lookfor);
433 lookfor = NULL;
434 }
435#endif
436
437 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000438 * When there are matching completions to select <S-Tab> works like
439 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000441 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 c = Ctrl_P;
443
444#ifdef FEAT_WILDMENU
445 /* Special translations for 'wildmenu' */
446 if (did_wild_list && p_wmnu)
447 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000448 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000450 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 c = Ctrl_N;
452 }
453 /* Hitting CR after "emenu Name.": complete submenu */
454 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
455 && ccline.cmdpos > 1
456 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
457 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
458 && (c == '\n' || c == '\r' || c == K_KENTER))
459 c = K_DOWN;
460#endif
461
462 /* free expanded names when finished walking through matches */
463 if (xpc.xp_numfiles != -1
464 && !(c == p_wc && KeyTyped) && c != p_wcm
465 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
466 && c != Ctrl_L)
467 {
468 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
469 did_wild_list = FALSE;
470#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000471 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472#endif
473 xpc.xp_context = EXPAND_NOTHING;
474 wim_index = 0;
475#ifdef FEAT_WILDMENU
476 if (p_wmnu && wild_menu_showing != 0)
477 {
478 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000479 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000480
481 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000482 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483
484 if (wild_menu_showing == WM_SCROLLED)
485 {
486 /* Entered command line, move it up */
487 cmdline_row--;
488 redrawcmd();
489 }
490 else if (save_p_ls != -1)
491 {
492 /* restore 'laststatus' and 'winminheight' */
493 p_ls = save_p_ls;
494 p_wmh = save_p_wmh;
495 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000496 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000498 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 redrawcmd();
500 save_p_ls = -1;
501 }
502 else
503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 redraw_statuslines();
506 }
507 KeyTyped = skt;
508 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000509 if (ccline.input_fn)
510 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 }
512#endif
513 }
514
515#ifdef FEAT_WILDMENU
516 /* Special translations for 'wildmenu' */
517 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
518 {
519 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000520 if (c == K_DOWN && ccline.cmdpos > 0
521 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000523 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
525 /* Hitting <Up>: Remove one submenu name in front of the
526 * cursor */
527 int found = FALSE;
528
529 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
530 i = 0;
531 while (--j > 0)
532 {
533 /* check for start of menu name */
534 if (ccline.cmdbuff[j] == ' '
535 && ccline.cmdbuff[j - 1] != '\\')
536 {
537 i = j + 1;
538 break;
539 }
540 /* check for start of submenu name */
541 if (ccline.cmdbuff[j] == '.'
542 && ccline.cmdbuff[j - 1] != '\\')
543 {
544 if (found)
545 {
546 i = j + 1;
547 break;
548 }
549 else
550 found = TRUE;
551 }
552 }
553 if (i > 0)
554 cmdline_del(i);
555 c = p_wc;
556 xpc.xp_context = EXPAND_NOTHING;
557 }
558 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000559 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000560 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000561 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 {
563 char_u upseg[5];
564
565 upseg[0] = PATHSEP;
566 upseg[1] = '.';
567 upseg[2] = '.';
568 upseg[3] = PATHSEP;
569 upseg[4] = NUL;
570
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000571 if (c == K_DOWN
572 && ccline.cmdpos > 0
573 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
574 && (ccline.cmdpos < 3
575 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
577 {
578 /* go down a directory */
579 c = p_wc;
580 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000581 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {
583 /* If in a direct ancestor, strip off one ../ to go down */
584 int found = FALSE;
585
586 j = ccline.cmdpos;
587 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
588 while (--j > i)
589 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000590#ifdef FEAT_MBYTE
591 if (has_mbyte)
592 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (vim_ispathsep(ccline.cmdbuff[j]))
595 {
596 found = TRUE;
597 break;
598 }
599 }
600 if (found
601 && ccline.cmdbuff[j - 1] == '.'
602 && ccline.cmdbuff[j - 2] == '.'
603 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
604 {
605 cmdline_del(j - 2);
606 c = p_wc;
607 }
608 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000609 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 {
611 /* go up a directory */
612 int found = FALSE;
613
614 j = ccline.cmdpos - 1;
615 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
616 while (--j > i)
617 {
618#ifdef FEAT_MBYTE
619 if (has_mbyte)
620 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
621#endif
622 if (vim_ispathsep(ccline.cmdbuff[j])
623#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100624 && vim_strchr((char_u *)" *?[{`$%#",
625 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626#endif
627 )
628 {
629 if (found)
630 {
631 i = j + 1;
632 break;
633 }
634 else
635 found = TRUE;
636 }
637 }
638
639 if (!found)
640 j = i;
641 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
642 j += 4;
643 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
644 && j == i)
645 j += 3;
646 else
647 j = 0;
648 if (j > 0)
649 {
650 /* TODO this is only for DOS/UNIX systems - need to put in
651 * machine-specific stuff here and in upseg init */
652 cmdline_del(j);
653 put_on_cmdline(upseg + 1, 3, FALSE);
654 }
655 else if (ccline.cmdpos > i)
656 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100657
658 /* Now complete in the new directory. Set KeyTyped in case the
659 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100661 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664
665#endif /* FEAT_WILDMENU */
666
667 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
668 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
669 if (c == Ctrl_BSL)
670 {
671 ++no_mapping;
672 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000673 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 --no_mapping;
675 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200676 /* CTRL-\ e doesn't work when obtaining an expression, unless it
677 * is in a mapping. */
678 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
679 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
681 vungetc(c);
682 c = Ctrl_BSL;
683 }
684#ifdef FEAT_EVAL
685 else if (c == 'e')
686 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200687 char_u *p = NULL;
688 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690 /*
691 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000692 * Need to save and restore the current command line, to be
693 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 */
695 if (ccline.cmdpos == ccline.cmdlen)
696 new_cmdpos = 99999; /* keep it at the end */
697 else
698 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000699
700 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000702 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 if (c == '=')
704 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000705 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000706 * to avoid nasty things like going to another buffer when
707 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000708 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000709 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000711 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000712 restore_cmdline(&save_ccline);
713
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200714 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200716 len = (int)STRLEN(p);
717 if (realloc_cmdbuff(len + 1) == OK)
718 {
719 ccline.cmdlen = len;
720 STRCPY(ccline.cmdbuff, p);
721 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200723 /* Restore the cursor or use the position set with
724 * set_cmdline_pos(). */
725 if (new_cmdpos > ccline.cmdlen)
726 ccline.cmdpos = ccline.cmdlen;
727 else
728 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200730 KeyTyped = FALSE; /* Don't do p_wc completion. */
731 redrawcmd();
732 goto cmdline_changed;
733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735 }
736 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100737 got_int = FALSE; /* don't abandon the command line */
738 did_emsg = FALSE;
739 emsg_on_display = FALSE;
740 redrawcmd();
741 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
743#endif
744 else
745 {
746 if (c == Ctrl_G && p_im && restart_edit == 0)
747 restart_edit = 'a';
748 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
749 in history */
750 goto returncmd; /* back to Normal mode */
751 }
752 }
753
754#ifdef FEAT_CMDWIN
755 if (c == cedit_key || c == K_CMDWIN)
756 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200757 if (ex_normal_busy == 0 && got_int == FALSE)
758 {
759 /*
760 * Open a window to edit the command line (and history).
761 */
762 c = ex_window();
763 some_key_typed = TRUE;
764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 }
766# ifdef FEAT_DIGRAPHS
767 else
768# endif
769#endif
770#ifdef FEAT_DIGRAPHS
771 c = do_digraph(c);
772#endif
773
774 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
775 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
776 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000777 /* In Ex mode a backslash escapes a newline. */
778 if (exmode_active
779 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000780 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000781 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000782 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000784 if (c == K_KENTER)
785 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000787 else
788 {
789 gotesc = FALSE; /* Might have typed ESC previously, don't
790 truncate the cmdline now. */
791 if (ccheck_abbr(c + ABBR_OFF))
792 goto cmdline_changed;
793 if (!cmd_silent)
794 {
795 windgoto(msg_row, 0);
796 out_flush();
797 }
798 break;
799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801
802 /*
803 * Completion for 'wildchar' or 'wildcharm' key.
804 * - hitting <ESC> twice means: abandon command line.
805 * - wildcard expansion is only done when the 'wildchar' key is really
806 * typed, not when it comes from a macro
807 */
808 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
809 {
810 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
811 {
812 /* if 'wildmode' contains "list" may still need to list */
813 if (xpc.xp_numfiles > 1
814 && !did_wild_list
815 && (wim_flags[wim_index] & WIM_LIST))
816 {
817 (void)showmatches(&xpc, FALSE);
818 redrawcmd();
819 did_wild_list = TRUE;
820 }
821 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100822 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
823 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100825 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
826 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 else
828 res = OK; /* don't insert 'wildchar' now */
829 }
830 else /* typed p_wc first time */
831 {
832 wim_index = 0;
833 j = ccline.cmdpos;
834 /* if 'wildmode' first contains "longest", get longest
835 * common part */
836 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100837 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
838 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100840 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
841 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
843 /* if interrupted while completing, behave like it failed */
844 if (got_int)
845 {
846 (void)vpeekc(); /* remove <C-C> from input stream */
847 got_int = FALSE; /* don't abandon the command line */
848 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
849#ifdef FEAT_WILDMENU
850 xpc.xp_context = EXPAND_NOTHING;
851#endif
852 goto cmdline_changed;
853 }
854
855 /* when more than one match, and 'wildmode' first contains
856 * "list", or no change and 'wildmode' contains "longest,list",
857 * list all matches */
858 if (res == OK && xpc.xp_numfiles > 1)
859 {
860 /* a "longest" that didn't do anything is skipped (but not
861 * "list:longest") */
862 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
863 wim_index = 1;
864 if ((wim_flags[wim_index] & WIM_LIST)
865#ifdef FEAT_WILDMENU
866 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
867#endif
868 )
869 {
870 if (!(wim_flags[0] & WIM_LONGEST))
871 {
872#ifdef FEAT_WILDMENU
873 int p_wmnu_save = p_wmnu;
874 p_wmnu = 0;
875#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100876 /* remove match */
877 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#ifdef FEAT_WILDMENU
879 p_wmnu = p_wmnu_save;
880#endif
881 }
882#ifdef FEAT_WILDMENU
883 (void)showmatches(&xpc, p_wmnu
884 && ((wim_flags[wim_index] & WIM_LIST) == 0));
885#else
886 (void)showmatches(&xpc, FALSE);
887#endif
888 redrawcmd();
889 did_wild_list = TRUE;
890 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100891 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
892 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100894 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
895 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200898 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900#ifdef FEAT_WILDMENU
901 else if (xpc.xp_numfiles == -1)
902 xpc.xp_context = EXPAND_NOTHING;
903#endif
904 }
905 if (wim_index < 3)
906 ++wim_index;
907 if (c == ESC)
908 gotesc = TRUE;
909 if (res == OK)
910 goto cmdline_changed;
911 }
912
913 gotesc = FALSE;
914
915 /* <S-Tab> goes to last match, in a clumsy way */
916 if (c == K_S_TAB && KeyTyped)
917 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100918 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
919 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
920 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 goto cmdline_changed;
922 }
923
924 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
925 c = NL;
926
927 do_abbr = TRUE; /* default: check for abbreviation */
928
929 /*
930 * Big switch for a typed command line character.
931 */
932 switch (c)
933 {
934 case K_BS:
935 case Ctrl_H:
936 case K_DEL:
937 case K_KDEL:
938 case Ctrl_W:
939#ifdef FEAT_FKMAP
940 if (cmd_fkmap && c == K_BS)
941 c = K_DEL;
942#endif
943 if (c == K_KDEL)
944 c = K_DEL;
945
946 /*
947 * delete current character is the same as backspace on next
948 * character, except at end of line
949 */
950 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
951 ++ccline.cmdpos;
952#ifdef FEAT_MBYTE
953 if (has_mbyte && c == K_DEL)
954 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
955 ccline.cmdbuff + ccline.cmdpos);
956#endif
957 if (ccline.cmdpos > 0)
958 {
959 char_u *p;
960
961 j = ccline.cmdpos;
962 p = ccline.cmdbuff + j;
963#ifdef FEAT_MBYTE
964 if (has_mbyte)
965 {
966 p = mb_prevptr(ccline.cmdbuff, p);
967 if (c == Ctrl_W)
968 {
969 while (p > ccline.cmdbuff && vim_isspace(*p))
970 p = mb_prevptr(ccline.cmdbuff, p);
971 i = mb_get_class(p);
972 while (p > ccline.cmdbuff && mb_get_class(p) == i)
973 p = mb_prevptr(ccline.cmdbuff, p);
974 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000975 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 }
978 else
979#endif
980 if (c == Ctrl_W)
981 {
982 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
983 --p;
984 i = vim_iswordc(p[-1]);
985 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
986 && vim_iswordc(p[-1]) == i)
987 --p;
988 }
989 else
990 --p;
991 ccline.cmdpos = (int)(p - ccline.cmdbuff);
992 ccline.cmdlen -= j - ccline.cmdpos;
993 i = ccline.cmdpos;
994 while (i < ccline.cmdlen)
995 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
996
997 /* Truncate at the end, required for multi-byte chars. */
998 ccline.cmdbuff[ccline.cmdlen] = NUL;
999 redrawcmd();
1000 }
1001 else if (ccline.cmdlen == 0 && c != Ctrl_W
1002 && ccline.cmdprompt == NULL && indent == 0)
1003 {
1004 /* In ex and debug mode it doesn't make sense to return. */
1005 if (exmode_active
1006#ifdef FEAT_EVAL
1007 || ccline.cmdfirstc == '>'
1008#endif
1009 )
1010 goto cmdline_not_changed;
1011
1012 vim_free(ccline.cmdbuff); /* no commandline to return */
1013 ccline.cmdbuff = NULL;
1014 if (!cmd_silent)
1015 {
1016#ifdef FEAT_RIGHTLEFT
1017 if (cmdmsg_rl)
1018 msg_col = Columns;
1019 else
1020#endif
1021 msg_col = 0;
1022 msg_putchar(' '); /* delete ':' */
1023 }
1024 redraw_cmdline = TRUE;
1025 goto returncmd; /* back to cmd mode */
1026 }
1027 goto cmdline_changed;
1028
1029 case K_INS:
1030 case K_KINS:
1031#ifdef FEAT_FKMAP
1032 /* if Farsi mode set, we are in reverse insert mode -
1033 Do not change the mode */
1034 if (cmd_fkmap)
1035 beep_flush();
1036 else
1037#endif
1038 ccline.overstrike = !ccline.overstrike;
1039#ifdef CURSOR_SHAPE
1040 ui_cursor_shape(); /* may show different cursor shape */
1041#endif
1042 goto cmdline_not_changed;
1043
1044 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001045 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
1047 /* ":lmap" mappings exists, toggle use of mappings. */
1048 State ^= LANGMAP;
1049#ifdef USE_IM_CONTROL
1050 im_set_active(FALSE); /* Disable input method */
1051#endif
1052 if (b_im_ptr != NULL)
1053 {
1054 if (State & LANGMAP)
1055 *b_im_ptr = B_IMODE_LMAP;
1056 else
1057 *b_im_ptr = B_IMODE_NONE;
1058 }
1059 }
1060#ifdef USE_IM_CONTROL
1061 else
1062 {
1063 /* There are no ":lmap" mappings, toggle IM. When
1064 * 'imdisable' is set don't try getting the status, it's
1065 * always off. */
1066 if ((p_imdisable && b_im_ptr != NULL)
1067 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1068 {
1069 im_set_active(FALSE); /* Disable input method */
1070 if (b_im_ptr != NULL)
1071 *b_im_ptr = B_IMODE_NONE;
1072 }
1073 else
1074 {
1075 im_set_active(TRUE); /* Enable input method */
1076 if (b_im_ptr != NULL)
1077 *b_im_ptr = B_IMODE_IM;
1078 }
1079 }
1080#endif
1081 if (b_im_ptr != NULL)
1082 {
1083 if (b_im_ptr == &curbuf->b_p_iminsert)
1084 set_iminsert_global();
1085 else
1086 set_imsearch_global();
1087 }
1088#ifdef CURSOR_SHAPE
1089 ui_cursor_shape(); /* may show different cursor shape */
1090#endif
1091#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1092 /* Show/unshow value of 'keymap' in status lines later. */
1093 status_redraw_curbuf();
1094#endif
1095 goto cmdline_not_changed;
1096
1097/* case '@': only in very old vi */
1098 case Ctrl_U:
1099 /* delete all characters left of the cursor */
1100 j = ccline.cmdpos;
1101 ccline.cmdlen -= j;
1102 i = ccline.cmdpos = 0;
1103 while (i < ccline.cmdlen)
1104 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1105 /* Truncate at the end, required for multi-byte chars. */
1106 ccline.cmdbuff[ccline.cmdlen] = NUL;
1107 redrawcmd();
1108 goto cmdline_changed;
1109
1110#ifdef FEAT_CLIPBOARD
1111 case Ctrl_Y:
1112 /* Copy the modeless selection, if there is one. */
1113 if (clip_star.state != SELECT_CLEARED)
1114 {
1115 if (clip_star.state == SELECT_DONE)
1116 clip_copy_modeless_selection(TRUE);
1117 goto cmdline_not_changed;
1118 }
1119 break;
1120#endif
1121
1122 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1123 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001124 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001125 * ":normal" runs out of characters. */
1126 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001127 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 goto cmdline_not_changed;
1129
1130 gotesc = TRUE; /* will free ccline.cmdbuff after
1131 putting it in history */
1132 goto returncmd; /* back to cmd mode */
1133
1134 case Ctrl_R: /* insert register */
1135#ifdef USE_ON_FLY_SCROLL
1136 dont_scroll = TRUE; /* disallow scrolling here */
1137#endif
1138 putcmdline('"', TRUE);
1139 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001140 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 if (i == Ctrl_O)
1142 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1143 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001144 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 --no_mapping;
1146#ifdef FEAT_EVAL
1147 /*
1148 * Insert the result of an expression.
1149 * Need to save the current command line, to be able to enter
1150 * a new one...
1151 */
1152 new_cmdpos = -1;
1153 if (c == '=')
1154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1156 {
1157 beep_flush();
1158 c = ESC;
1159 }
1160 else
1161 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001162 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001164 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166 }
1167#endif
1168 if (c != ESC) /* use ESC to cancel inserting register */
1169 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001170 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001171
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001172#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001173 /* When there was a serious error abort getting the
1174 * command line. */
1175 if (aborting())
1176 {
1177 gotesc = TRUE; /* will free ccline.cmdbuff after
1178 putting it in history */
1179 goto returncmd; /* back to cmd mode */
1180 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 KeyTyped = FALSE; /* Don't do p_wc completion. */
1183#ifdef FEAT_EVAL
1184 if (new_cmdpos >= 0)
1185 {
1186 /* set_cmdline_pos() was used */
1187 if (new_cmdpos > ccline.cmdlen)
1188 ccline.cmdpos = ccline.cmdlen;
1189 else
1190 ccline.cmdpos = new_cmdpos;
1191 }
1192#endif
1193 }
1194 redrawcmd();
1195 goto cmdline_changed;
1196
1197 case Ctrl_D:
1198 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1199 break; /* Use ^D as normal char instead */
1200
1201 redrawcmd();
1202 continue; /* don't do incremental search now */
1203
1204 case K_RIGHT:
1205 case K_S_RIGHT:
1206 case K_C_RIGHT:
1207 do
1208 {
1209 if (ccline.cmdpos >= ccline.cmdlen)
1210 break;
1211 i = cmdline_charsize(ccline.cmdpos);
1212 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1213 break;
1214 ccline.cmdspos += i;
1215#ifdef FEAT_MBYTE
1216 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001217 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 + ccline.cmdpos);
1219 else
1220#endif
1221 ++ccline.cmdpos;
1222 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001223 while ((c == K_S_RIGHT || c == K_C_RIGHT
1224 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1226#ifdef FEAT_MBYTE
1227 if (has_mbyte)
1228 set_cmdspos_cursor();
1229#endif
1230 goto cmdline_not_changed;
1231
1232 case K_LEFT:
1233 case K_S_LEFT:
1234 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001235 if (ccline.cmdpos == 0)
1236 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 do
1238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 --ccline.cmdpos;
1240#ifdef FEAT_MBYTE
1241 if (has_mbyte) /* move to first byte of char */
1242 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1243 ccline.cmdbuff + ccline.cmdpos);
1244#endif
1245 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1246 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001247 while (ccline.cmdpos > 0
1248 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001249 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1251#ifdef FEAT_MBYTE
1252 if (has_mbyte)
1253 set_cmdspos_cursor();
1254#endif
1255 goto cmdline_not_changed;
1256
1257 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001258 /* Ignore mouse event or ex_window() result. */
1259 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
Bram Moolenaar4770d092006-01-12 23:22:24 +00001261#ifdef FEAT_GUI_W32
1262 /* On Win32 ignore <M-F4>, we get it when closing the window was
1263 * cancelled. */
1264 case K_F4:
1265 if (mod_mask == MOD_MASK_ALT)
1266 {
1267 redrawcmd(); /* somehow the cmdline is cleared */
1268 goto cmdline_not_changed;
1269 }
1270 break;
1271#endif
1272
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273#ifdef FEAT_MOUSE
1274 case K_MIDDLEDRAG:
1275 case K_MIDDLERELEASE:
1276 goto cmdline_not_changed; /* Ignore mouse */
1277
1278 case K_MIDDLEMOUSE:
1279# ifdef FEAT_GUI
1280 /* When GUI is active, also paste when 'mouse' is empty */
1281 if (!gui.in_use)
1282# endif
1283 if (!mouse_has(MOUSE_COMMAND))
1284 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001285# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001287 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001289# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001290 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 redrawcmd();
1292 goto cmdline_changed;
1293
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001294# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001296 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 redrawcmd();
1298 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300
1301 case K_LEFTDRAG:
1302 case K_LEFTRELEASE:
1303 case K_RIGHTDRAG:
1304 case K_RIGHTRELEASE:
1305 /* Ignore drag and release events when the button-down wasn't
1306 * seen before. */
1307 if (ignore_drag_release)
1308 goto cmdline_not_changed;
1309 /* FALLTHROUGH */
1310 case K_LEFTMOUSE:
1311 case K_RIGHTMOUSE:
1312 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1313 ignore_drag_release = TRUE;
1314 else
1315 ignore_drag_release = FALSE;
1316# ifdef FEAT_GUI
1317 /* When GUI is active, also move when 'mouse' is empty */
1318 if (!gui.in_use)
1319# endif
1320 if (!mouse_has(MOUSE_COMMAND))
1321 goto cmdline_not_changed; /* Ignore mouse */
1322# ifdef FEAT_CLIPBOARD
1323 if (mouse_row < cmdline_row && clip_star.available)
1324 {
1325 int button, is_click, is_drag;
1326
1327 /*
1328 * Handle modeless selection.
1329 */
1330 button = get_mouse_button(KEY2TERMCAP1(c),
1331 &is_click, &is_drag);
1332 if (mouse_model_popup() && button == MOUSE_LEFT
1333 && (mod_mask & MOD_MASK_SHIFT))
1334 {
1335 /* Translate shift-left to right button. */
1336 button = MOUSE_RIGHT;
1337 mod_mask &= ~MOD_MASK_SHIFT;
1338 }
1339 clip_modeless(button, is_click, is_drag);
1340 goto cmdline_not_changed;
1341 }
1342# endif
1343
1344 set_cmdspos();
1345 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1346 ++ccline.cmdpos)
1347 {
1348 i = cmdline_charsize(ccline.cmdpos);
1349 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1350 && mouse_col < ccline.cmdspos % Columns + i)
1351 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001352# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (has_mbyte)
1354 {
1355 /* Count ">" for double-wide char that doesn't fit. */
1356 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001357 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 + ccline.cmdpos) - 1;
1359 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 ccline.cmdspos += i;
1362 }
1363 goto cmdline_not_changed;
1364
1365 /* Mouse scroll wheel: ignored here */
1366 case K_MOUSEDOWN:
1367 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001368 case K_MOUSELEFT:
1369 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 /* Alternate buttons ignored here */
1371 case K_X1MOUSE:
1372 case K_X1DRAG:
1373 case K_X1RELEASE:
1374 case K_X2MOUSE:
1375 case K_X2DRAG:
1376 case K_X2RELEASE:
1377 goto cmdline_not_changed;
1378
1379#endif /* FEAT_MOUSE */
1380
1381#ifdef FEAT_GUI
1382 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1383 case K_LEFTRELEASE_NM:
1384 goto cmdline_not_changed;
1385
1386 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001387 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 {
1389 gui_do_scroll();
1390 redrawcmd();
1391 }
1392 goto cmdline_not_changed;
1393
1394 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001395 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001397 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 redrawcmd();
1399 }
1400 goto cmdline_not_changed;
1401#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001402#ifdef FEAT_GUI_TABLINE
1403 case K_TABLINE:
1404 case K_TABMENU:
1405 /* Don't want to change any tabs here. Make sure the same tab
1406 * is still selected. */
1407 if (gui_use_tabline())
1408 gui_mch_set_curtab(tabpage_index(curtab));
1409 goto cmdline_not_changed;
1410#endif
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 case K_SELECT: /* end of Select mode mapping - ignore */
1413 goto cmdline_not_changed;
1414
1415 case Ctrl_B: /* begin of command line */
1416 case K_HOME:
1417 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 case K_S_HOME:
1419 case K_C_HOME:
1420 ccline.cmdpos = 0;
1421 set_cmdspos();
1422 goto cmdline_not_changed;
1423
1424 case Ctrl_E: /* end of command line */
1425 case K_END:
1426 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 case K_S_END:
1428 case K_C_END:
1429 ccline.cmdpos = ccline.cmdlen;
1430 set_cmdspos_cursor();
1431 goto cmdline_not_changed;
1432
1433 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001434 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 break;
1436 goto cmdline_changed;
1437
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001438 case Ctrl_L:
1439#ifdef FEAT_SEARCH_EXTRA
1440 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1441 {
1442 /* Add a character from under the cursor for 'incsearch' */
1443 if (did_incsearch
1444 && !equalpos(curwin->w_cursor, old_cursor))
1445 {
1446 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001447 /* If 'ignorecase' and 'smartcase' are set and the
1448 * command line has no uppercase characters, convert
1449 * the character to lowercase */
1450 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1451 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001452 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001453 {
1454 if (c == firstc || vim_strchr((char_u *)(
1455 p_magic ? "\\^$.*[" : "\\^$"), c)
1456 != NULL)
1457 {
1458 /* put a backslash before special characters */
1459 stuffcharReadbuff(c);
1460 c = '\\';
1461 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001462 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001463 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001464 }
1465 goto cmdline_not_changed;
1466 }
1467#endif
1468
1469 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001470 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 break;
1472 goto cmdline_changed;
1473
1474 case Ctrl_N: /* next match */
1475 case Ctrl_P: /* previous match */
1476 if (xpc.xp_numfiles > 0)
1477 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001478 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1479 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 break;
1481 goto cmdline_changed;
1482 }
1483
1484#ifdef FEAT_CMDHIST
1485 case K_UP:
1486 case K_DOWN:
1487 case K_S_UP:
1488 case K_S_DOWN:
1489 case K_PAGEUP:
1490 case K_KPAGEUP:
1491 case K_PAGEDOWN:
1492 case K_KPAGEDOWN:
1493 if (hislen == 0 || firstc == NUL) /* no history */
1494 goto cmdline_not_changed;
1495
1496 i = hiscnt;
1497
1498 /* save current command string so it can be restored later */
1499 if (lookfor == NULL)
1500 {
1501 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1502 goto cmdline_not_changed;
1503 lookfor[ccline.cmdpos] = NUL;
1504 }
1505
1506 j = (int)STRLEN(lookfor);
1507 for (;;)
1508 {
1509 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001510 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001511 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 {
1513 if (hiscnt == hislen) /* first time */
1514 hiscnt = hisidx[histype];
1515 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1516 hiscnt = hislen - 1;
1517 else if (hiscnt != hisidx[histype] + 1)
1518 --hiscnt;
1519 else /* at top of list */
1520 {
1521 hiscnt = i;
1522 break;
1523 }
1524 }
1525 else /* one step forwards */
1526 {
1527 /* on last entry, clear the line */
1528 if (hiscnt == hisidx[histype])
1529 {
1530 hiscnt = hislen;
1531 break;
1532 }
1533
1534 /* not on a history line, nothing to do */
1535 if (hiscnt == hislen)
1536 break;
1537 if (hiscnt == hislen - 1) /* wrap around */
1538 hiscnt = 0;
1539 else
1540 ++hiscnt;
1541 }
1542 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1543 {
1544 hiscnt = i;
1545 break;
1546 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001547 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001548 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 || STRNCMP(history[histype][hiscnt].hisstr,
1550 lookfor, (size_t)j) == 0)
1551 break;
1552 }
1553
1554 if (hiscnt != i) /* jumped to other entry */
1555 {
1556 char_u *p;
1557 int len;
1558 int old_firstc;
1559
1560 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001561 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (hiscnt == hislen)
1563 p = lookfor; /* back to the old one */
1564 else
1565 p = history[histype][hiscnt].hisstr;
1566
1567 if (histype == HIST_SEARCH
1568 && p != lookfor
1569 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1570 {
1571 /* Correct for the separator character used when
1572 * adding the history entry vs the one used now.
1573 * First loop: count length.
1574 * Second loop: copy the characters. */
1575 for (i = 0; i <= 1; ++i)
1576 {
1577 len = 0;
1578 for (j = 0; p[j] != NUL; ++j)
1579 {
1580 /* Replace old sep with new sep, unless it is
1581 * escaped. */
1582 if (p[j] == old_firstc
1583 && (j == 0 || p[j - 1] != '\\'))
1584 {
1585 if (i > 0)
1586 ccline.cmdbuff[len] = firstc;
1587 }
1588 else
1589 {
1590 /* Escape new sep, unless it is already
1591 * escaped. */
1592 if (p[j] == firstc
1593 && (j == 0 || p[j - 1] != '\\'))
1594 {
1595 if (i > 0)
1596 ccline.cmdbuff[len] = '\\';
1597 ++len;
1598 }
1599 if (i > 0)
1600 ccline.cmdbuff[len] = p[j];
1601 }
1602 ++len;
1603 }
1604 if (i == 0)
1605 {
1606 alloc_cmdbuff(len);
1607 if (ccline.cmdbuff == NULL)
1608 goto returncmd;
1609 }
1610 }
1611 ccline.cmdbuff[len] = NUL;
1612 }
1613 else
1614 {
1615 alloc_cmdbuff((int)STRLEN(p));
1616 if (ccline.cmdbuff == NULL)
1617 goto returncmd;
1618 STRCPY(ccline.cmdbuff, p);
1619 }
1620
1621 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1622 redrawcmd();
1623 goto cmdline_changed;
1624 }
1625 beep_flush();
1626 goto cmdline_not_changed;
1627#endif
1628
1629 case Ctrl_V:
1630 case Ctrl_Q:
1631#ifdef FEAT_MOUSE
1632 ignore_drag_release = TRUE;
1633#endif
1634 putcmdline('^', TRUE);
1635 c = get_literal(); /* get next (two) character(s) */
1636 do_abbr = FALSE; /* don't do abbreviation now */
1637#ifdef FEAT_MBYTE
1638 /* may need to remove ^ when composing char was typed */
1639 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1640 {
1641 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1642 msg_putchar(' ');
1643 cursorcmd();
1644 }
1645#endif
1646 break;
1647
1648#ifdef FEAT_DIGRAPHS
1649 case Ctrl_K:
1650#ifdef FEAT_MOUSE
1651 ignore_drag_release = TRUE;
1652#endif
1653 putcmdline('?', TRUE);
1654#ifdef USE_ON_FLY_SCROLL
1655 dont_scroll = TRUE; /* disallow scrolling here */
1656#endif
1657 c = get_digraph(TRUE);
1658 if (c != NUL)
1659 break;
1660
1661 redrawcmd();
1662 goto cmdline_not_changed;
1663#endif /* FEAT_DIGRAPHS */
1664
1665#ifdef FEAT_RIGHTLEFT
1666 case Ctrl__: /* CTRL-_: switch language mode */
1667 if (!p_ari)
1668 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001669# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (p_altkeymap)
1671 {
1672 cmd_fkmap = !cmd_fkmap;
1673 if (cmd_fkmap) /* in Farsi always in Insert mode */
1674 ccline.overstrike = FALSE;
1675 }
1676 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 cmd_hkmap = !cmd_hkmap;
1679 goto cmdline_not_changed;
1680#endif
1681
1682 default:
1683#ifdef UNIX
1684 if (c == intr_char)
1685 {
1686 gotesc = TRUE; /* will free ccline.cmdbuff after
1687 putting it in history */
1688 goto returncmd; /* back to Normal mode */
1689 }
1690#endif
1691 /*
1692 * Normal character with no special meaning. Just set mod_mask
1693 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1694 * the string <S-Space>. This should only happen after ^V.
1695 */
1696 if (!IS_SPECIAL(c))
1697 mod_mask = 0x0;
1698 break;
1699 }
1700 /*
1701 * End of switch on command line character.
1702 * We come here if we have a normal character.
1703 */
1704
Bram Moolenaarede3e632013-06-23 16:16:19 +02001705 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706#ifdef FEAT_MBYTE
1707 /* Add ABBR_OFF for characters above 0x100, this is
1708 * what check_abbr() expects. */
1709 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1710#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001711 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 goto cmdline_changed;
1713
1714 /*
1715 * put the character in the command line
1716 */
1717 if (IS_SPECIAL(c) || mod_mask != 0)
1718 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1719 else
1720 {
1721#ifdef FEAT_MBYTE
1722 if (has_mbyte)
1723 {
1724 j = (*mb_char2bytes)(c, IObuff);
1725 IObuff[j] = NUL; /* exclude composing chars */
1726 put_on_cmdline(IObuff, j, TRUE);
1727 }
1728 else
1729#endif
1730 {
1731 IObuff[0] = c;
1732 put_on_cmdline(IObuff, 1, TRUE);
1733 }
1734 }
1735 goto cmdline_changed;
1736
1737/*
1738 * This part implements incremental searches for "/" and "?"
1739 * Jump to cmdline_not_changed when a character has been read but the command
1740 * line did not change. Then we only search and redraw if something changed in
1741 * the past.
1742 * Jump to cmdline_changed when the command line did change.
1743 * (Sorry for the goto's, I know it is ugly).
1744 */
1745cmdline_not_changed:
1746#ifdef FEAT_SEARCH_EXTRA
1747 if (!incsearch_postponed)
1748 continue;
1749#endif
1750
1751cmdline_changed:
1752#ifdef FEAT_SEARCH_EXTRA
1753 /*
1754 * 'incsearch' highlighting.
1755 */
1756 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1757 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001758 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001759#ifdef FEAT_RELTIME
1760 proftime_T tm;
1761#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 /* if there is a character waiting, search and redraw later */
1764 if (char_avail())
1765 {
1766 incsearch_postponed = TRUE;
1767 continue;
1768 }
1769 incsearch_postponed = FALSE;
1770 curwin->w_cursor = old_cursor; /* start at old position */
1771
1772 /* If there is no command line, don't do anything */
1773 if (ccline.cmdlen == 0)
1774 i = 0;
1775 else
1776 {
1777 cursor_off(); /* so the user knows we're busy */
1778 out_flush();
1779 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001780#ifdef FEAT_RELTIME
1781 /* Set the time limit to half a second. */
1782 profile_setlimit(500L, &tm);
1783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001785 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1786#ifdef FEAT_RELTIME
1787 &tm
1788#else
1789 NULL
1790#endif
1791 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 --emsg_off;
1793 /* if interrupted while searching, behave like it failed */
1794 if (got_int)
1795 {
1796 (void)vpeekc(); /* remove <C-C> from input stream */
1797 got_int = FALSE; /* don't abandon the command line */
1798 i = 0;
1799 }
1800 else if (char_avail())
1801 /* cancelled searching because a char was typed */
1802 incsearch_postponed = TRUE;
1803 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001804 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 highlight_match = TRUE; /* highlight position */
1806 else
1807 highlight_match = FALSE; /* remove highlight */
1808
1809 /* first restore the old curwin values, so the screen is
1810 * positioned in the same way as the actual search command */
1811 curwin->w_leftcol = old_leftcol;
1812 curwin->w_topline = old_topline;
1813# ifdef FEAT_DIFF
1814 curwin->w_topfill = old_topfill;
1815# endif
1816 curwin->w_botline = old_botline;
1817 changed_cline_bef_curs();
1818 update_topline();
1819
1820 if (i != 0)
1821 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001822 pos_T save_pos = curwin->w_cursor;
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001825 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 * moves the whole match onto the screen when 'nowrap' is set.
1827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 curwin->w_cursor.lnum += search_match_lines;
1829 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001830 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1831 {
1832 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1833 coladvance((colnr_T)MAXCOL);
1834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001836 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001837 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001839 else
1840 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001843# ifdef FEAT_WINDOWS
1844 /* May redraw the status line to show the cursor position. */
1845 if (p_ru && curwin->w_status_height > 0)
1846 curwin->w_redr_status = TRUE;
1847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001849 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001850 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001851 restore_cmdline(&save_ccline);
1852
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001853 /* Leave it at the end to make CTRL-R CTRL-W work. */
1854 if (i != 0)
1855 curwin->w_cursor = end_pos;
1856
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 msg_starthere();
1858 redrawcmdline();
1859 did_incsearch = TRUE;
1860 }
1861#else /* FEAT_SEARCH_EXTRA */
1862 ;
1863#endif
1864
1865#ifdef FEAT_RIGHTLEFT
1866 if (cmdmsg_rl
1867# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001868 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869# endif
1870 )
1871 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001872 * right-left typing. Not efficient, but it works.
1873 * Do it only when there are no characters left to read
1874 * to avoid useless intermediate redraws. */
1875 if (vpeekc() == NUL)
1876 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877#endif
1878 }
1879
1880returncmd:
1881
1882#ifdef FEAT_RIGHTLEFT
1883 cmdmsg_rl = FALSE;
1884#endif
1885
1886#ifdef FEAT_FKMAP
1887 cmd_fkmap = 0;
1888#endif
1889
1890 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001891 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
1893#ifdef FEAT_SEARCH_EXTRA
1894 if (did_incsearch)
1895 {
1896 curwin->w_cursor = old_cursor;
1897 curwin->w_curswant = old_curswant;
1898 curwin->w_leftcol = old_leftcol;
1899 curwin->w_topline = old_topline;
1900# ifdef FEAT_DIFF
1901 curwin->w_topfill = old_topfill;
1902# endif
1903 curwin->w_botline = old_botline;
1904 highlight_match = FALSE;
1905 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001906 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908#endif
1909
1910 if (ccline.cmdbuff != NULL)
1911 {
1912 /*
1913 * Put line in history buffer (":" and "=" only when it was typed).
1914 */
1915#ifdef FEAT_CMDHIST
1916 if (ccline.cmdlen && firstc != NUL
1917 && (some_key_typed || histype == HIST_SEARCH))
1918 {
1919 add_to_history(histype, ccline.cmdbuff, TRUE,
1920 histype == HIST_SEARCH ? firstc : NUL);
1921 if (firstc == ':')
1922 {
1923 vim_free(new_last_cmdline);
1924 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1925 }
1926 }
1927#endif
1928
1929 if (gotesc) /* abandon command line */
1930 {
1931 vim_free(ccline.cmdbuff);
1932 ccline.cmdbuff = NULL;
1933 if (msg_scrolled == 0)
1934 compute_cmdrow();
1935 MSG("");
1936 redraw_cmdline = TRUE;
1937 }
1938 }
1939
1940 /*
1941 * If the screen was shifted up, redraw the whole screen (later).
1942 * If the line is too long, clear it, so ruler and shown command do
1943 * not get printed in the middle of it.
1944 */
1945 msg_check();
1946 msg_scroll = save_msg_scroll;
1947 redir_off = FALSE;
1948
1949 /* When the command line was typed, no need for a wait-return prompt. */
1950 if (some_key_typed)
1951 need_wait_return = FALSE;
1952
1953 State = save_State;
1954#ifdef USE_IM_CONTROL
1955 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1956 im_save_status(b_im_ptr);
1957 im_set_active(FALSE);
1958#endif
1959#ifdef FEAT_MOUSE
1960 setmouse();
1961#endif
1962#ifdef CURSOR_SHAPE
1963 ui_cursor_shape(); /* may show different cursor shape */
1964#endif
1965
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001966 {
1967 char_u *p = ccline.cmdbuff;
1968
1969 /* Make ccline empty, getcmdline() may try to use it. */
1970 ccline.cmdbuff = NULL;
1971 return p;
1972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973}
1974
1975#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1976/*
1977 * Get a command line with a prompt.
1978 * This is prepared to be called recursively from getcmdline() (e.g. by
1979 * f_input() when evaluating an expression from CTRL-R =).
1980 * Returns the command line in allocated memory, or NULL.
1981 */
1982 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001983getcmdline_prompt(
1984 int firstc,
1985 char_u *prompt, /* command line prompt */
1986 int attr, /* attributes for prompt */
1987 int xp_context, /* type of expansion */
1988 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990 char_u *s;
1991 struct cmdline_info save_ccline;
1992 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01001993 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001995 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 ccline.cmdprompt = prompt;
1997 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001998# ifdef FEAT_EVAL
1999 ccline.xp_context = xp_context;
2000 ccline.xp_arg = xp_arg;
2001 ccline.input_fn = (firstc == '@');
2002# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002003 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002005 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002006 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002007 /* Restore msg_col, the prompt from input() may have changed it.
2008 * But only if called recursively and the commandline is therefore being
2009 * restored to an old one; if not, the input() prompt stays on the screen,
2010 * so we need its modified msg_col left intact. */
2011 if (ccline.cmdbuff != NULL)
2012 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 return s;
2015}
2016#endif
2017
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002018/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002019 * Return TRUE when the text must not be changed and we can't switch to
2020 * another window or buffer. Used when editing the command line, evaluating
2021 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002022 */
2023 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002024text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002025{
2026#ifdef FEAT_CMDWIN
2027 if (cmdwin_type != 0)
2028 return TRUE;
2029#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002030 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002031}
2032
2033/*
2034 * Give an error message for a command that isn't allowed while the cmdline
2035 * window is open or editing the cmdline in another way.
2036 */
2037 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002038text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002039{
2040#ifdef FEAT_CMDWIN
2041 if (cmdwin_type != 0)
2042 EMSG(_(e_cmdwin));
2043 else
2044#endif
2045 EMSG(_(e_secure));
2046}
2047
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002048#if defined(FEAT_AUTOCMD) || defined(PROTO)
2049/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002050 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2051 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002052 */
2053 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002054curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002055{
2056 if (curbuf_lock > 0)
2057 {
2058 EMSG(_("E788: Not allowed to edit another buffer now"));
2059 return TRUE;
2060 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002061 return allbuf_locked();
2062}
2063
2064/*
2065 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2066 * message.
2067 */
2068 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002069allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002070{
2071 if (allbuf_lock > 0)
2072 {
2073 EMSG(_("E811: Not allowed to change buffer information now"));
2074 return TRUE;
2075 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002076 return FALSE;
2077}
2078#endif
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002081cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2084 if (cmdline_star > 0) /* showing '*', always 1 position */
2085 return 1;
2086#endif
2087 return ptr2cells(ccline.cmdbuff + idx);
2088}
2089
2090/*
2091 * Compute the offset of the cursor on the command line for the prompt and
2092 * indent.
2093 */
2094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002095set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002097 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 ccline.cmdspos = 1 + ccline.cmdindent;
2099 else
2100 ccline.cmdspos = 0 + ccline.cmdindent;
2101}
2102
2103/*
2104 * Compute the screen position for the cursor on the command line.
2105 */
2106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002107set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
2109 int i, m, c;
2110
2111 set_cmdspos();
2112 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002115 if (m < 0) /* overflow, Columns or Rows at weird value */
2116 m = MAXCOL;
2117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 else
2119 m = MAXCOL;
2120 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2121 {
2122 c = cmdline_charsize(i);
2123#ifdef FEAT_MBYTE
2124 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2125 if (has_mbyte)
2126 correct_cmdspos(i, c);
2127#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002128 /* If the cmdline doesn't fit, show cursor on last visible char.
2129 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 if ((ccline.cmdspos += c) >= m)
2131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 ccline.cmdspos -= c;
2133 break;
2134 }
2135#ifdef FEAT_MBYTE
2136 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002137 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138#endif
2139 }
2140}
2141
2142#ifdef FEAT_MBYTE
2143/*
2144 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2145 * character that doesn't fit, so that a ">" must be displayed.
2146 */
2147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002148correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002150 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2152 && ccline.cmdspos % Columns + cells > Columns)
2153 ccline.cmdspos++;
2154}
2155#endif
2156
2157/*
2158 * Get an Ex command line for the ":" command.
2159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002161getexline(
2162 int c, /* normally ':', NUL for ":append" */
2163 void *cookie UNUSED,
2164 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
2166 /* When executing a register, remove ':' that's in front of each line. */
2167 if (exec_from_reg && vpeekc() == ':')
2168 (void)vgetc();
2169 return getcmdline(c, 1L, indent);
2170}
2171
2172/*
2173 * Get an Ex command line for Ex mode.
2174 * In Ex mode we only use the OS supplied line editing features and no
2175 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002176 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002179getexmodeline(
2180 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002181 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002182 void *cookie UNUSED,
2183 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 garray_T line_ga;
2186 char_u *pend;
2187 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002188 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002189 int escaped = FALSE; /* CTRL-V typed */
2190 int vcol = 0;
2191 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002192 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002193 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 /* Switch cursor on now. This avoids that it happens after the "\n", which
2196 * confuses the system function that computes tabstops. */
2197 cursor_on();
2198
2199 /* always start in column 0; write a newline if necessary */
2200 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002201 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002203 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002205 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002206 if (p_prompt)
2207 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 while (indent-- > 0)
2209 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212
2213 ga_init2(&line_ga, 1, 30);
2214
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002215 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002216 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002217 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002218 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002219 while (indent >= 8)
2220 {
2221 ga_append(&line_ga, TAB);
2222 msg_puts((char_u *)" ");
2223 indent -= 8;
2224 }
2225 while (indent-- > 0)
2226 {
2227 ga_append(&line_ga, ' ');
2228 msg_putchar(' ');
2229 }
2230 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002231 ++no_mapping;
2232 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002233
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 /*
2235 * Get the line, one character at a time.
2236 */
2237 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002238 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002240 long sw;
2241 char_u *s;
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (ga_grow(&line_ga, 40) == FAIL)
2244 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002246 /* Get one character at a time. Don't use inchar(), it can't handle
2247 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002248 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002249 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002252 * Handle line editing.
2253 * Previously this was left to the system, putting the terminal in
2254 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002256 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002258 msg_putchar('\n');
2259 break;
2260 }
2261
2262 if (!escaped)
2263 {
2264 /* CR typed means "enter", which is NL */
2265 if (c1 == '\r')
2266 c1 = '\n';
2267
2268 if (c1 == BS || c1 == K_BS
2269 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002271 if (line_ga.ga_len > 0)
2272 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002273#ifdef FEAT_MBYTE
2274 if (has_mbyte)
2275 {
2276 p = (char_u *)line_ga.ga_data;
2277 p[line_ga.ga_len] = NUL;
2278 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2279 line_ga.ga_len -= len;
2280 }
2281 else
2282#endif
2283 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002284 goto redraw;
2285 }
2286 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002289 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002291 msg_col = startcol;
2292 msg_clr_eos();
2293 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002294 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002295 }
2296
2297 if (c1 == Ctrl_T)
2298 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002299 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002300 p = (char_u *)line_ga.ga_data;
2301 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002302 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002303 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002304add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002305 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002307 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002308 p = (char_u *)line_ga.ga_data;
2309 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2311 *s = ' ';
2312 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002314redraw:
2315 /* redraw the line */
2316 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002317 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002318 p = (char_u *)line_ga.ga_data;
2319 p[line_ga.ga_len] = NUL;
2320 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002322 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002324 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002326 msg_putchar(' ');
2327 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002328 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002332 len = MB_PTR2LEN(p);
2333 msg_outtrans_len(p, len);
2334 vcol += ptr2cells(p);
2335 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002338 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002339 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002340 continue;
2341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002343 if (c1 == Ctrl_D)
2344 {
2345 /* Delete one shiftwidth. */
2346 p = (char_u *)line_ga.ga_data;
2347 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002349 if (prev_char == '^')
2350 ex_keep_indent = TRUE;
2351 indent = 0;
2352 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354 else
2355 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002356 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002357 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002358 if (indent > 0)
2359 {
2360 --indent;
2361 indent -= indent % get_sw_value(curbuf);
2362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002364 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002365 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002366 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002367 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2368 --line_ga.ga_len;
2369 }
2370 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002372
2373 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2374 {
2375 escaped = TRUE;
2376 continue;
2377 }
2378
2379 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2380 if (IS_SPECIAL(c1))
2381 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002383
2384 if (IS_SPECIAL(c1))
2385 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002386#ifdef FEAT_MBYTE
2387 if (has_mbyte)
2388 len = (*mb_char2bytes)(c1,
2389 (char_u *)line_ga.ga_data + line_ga.ga_len);
2390 else
2391#endif
2392 {
2393 len = 1;
2394 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2395 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002396 if (c1 == '\n')
2397 msg_putchar('\n');
2398 else if (c1 == TAB)
2399 {
2400 /* Don't use chartabsize(), 'ts' can be different */
2401 do
2402 {
2403 msg_putchar(' ');
2404 } while (++vcol % 8);
2405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002408 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002409 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002410 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002412 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002413 escaped = FALSE;
2414
2415 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002416 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002417
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002418 /* We are done when a NL is entered, but not when it comes after an
2419 * odd number of backslashes, that results in a NUL. */
2420 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002422 int bcount = 0;
2423
2424 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2425 ++bcount;
2426
2427 if (bcount > 0)
2428 {
2429 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2430 * "\NL", etc. */
2431 line_ga.ga_len -= (bcount + 1) / 2;
2432 pend -= (bcount + 1) / 2;
2433 pend[-1] = '\n';
2434 }
2435
2436 if ((bcount & 1) == 0)
2437 {
2438 --line_ga.ga_len;
2439 --pend;
2440 *pend = NUL;
2441 break;
2442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
2444 }
2445
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002446 --no_mapping;
2447 --allow_keys;
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 /* make following messages go to the next line */
2450 msg_didout = FALSE;
2451 msg_col = 0;
2452 if (msg_row < Rows - 1)
2453 ++msg_row;
2454 emsg_on_display = FALSE; /* don't want ui_delay() */
2455
2456 if (got_int)
2457 ga_clear(&line_ga);
2458
2459 return (char_u *)line_ga.ga_data;
2460}
2461
Bram Moolenaare344bea2005-09-01 20:46:49 +00002462# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2463 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464/*
2465 * Return TRUE if ccline.overstrike is on.
2466 */
2467 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002468cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 return ccline.overstrike;
2471}
2472
2473/*
2474 * Return TRUE if the cursor is at the end of the cmdline.
2475 */
2476 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002477cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478{
2479 return (ccline.cmdpos >= ccline.cmdlen);
2480}
2481#endif
2482
Bram Moolenaar9372a112005-12-06 19:59:18 +00002483#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484/*
2485 * Return the virtual column number at the current cursor position.
2486 * This is used by the IM code to obtain the start of the preedit string.
2487 */
2488 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002489cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2492 return MAXCOL;
2493
2494# ifdef FEAT_MBYTE
2495 if (has_mbyte)
2496 {
2497 colnr_T col;
2498 int i = 0;
2499
2500 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002501 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 return col;
2504 }
2505 else
2506# endif
2507 return ccline.cmdpos;
2508}
2509#endif
2510
2511#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2512/*
2513 * If part of the command line is an IM preedit string, redraw it with
2514 * IM feedback attributes. The cursor position is restored after drawing.
2515 */
2516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002517redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519 if ((State & CMDLINE)
2520 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002521 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 && !p_imdisable
2523 && im_is_preediting())
2524 {
2525 int cmdpos = 0;
2526 int cmdspos;
2527 int old_row;
2528 int old_col;
2529 colnr_T col;
2530
2531 old_row = msg_row;
2532 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002533 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535# ifdef FEAT_MBYTE
2536 if (has_mbyte)
2537 {
2538 for (col = 0; col < preedit_start_col
2539 && cmdpos < ccline.cmdlen; ++col)
2540 {
2541 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002542 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544 }
2545 else
2546# endif
2547 {
2548 cmdspos += preedit_start_col;
2549 cmdpos += preedit_start_col;
2550 }
2551
2552 msg_row = cmdline_row + (cmdspos / (int)Columns);
2553 msg_col = cmdspos % (int)Columns;
2554 if (msg_row >= Rows)
2555 msg_row = Rows - 1;
2556
2557 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2558 {
2559 int char_len;
2560 int char_attr;
2561
2562 char_attr = im_get_feedback_attr(col);
2563 if (char_attr < 0)
2564 break; /* end of preedit string */
2565
2566# ifdef FEAT_MBYTE
2567 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002568 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 else
2570# endif
2571 char_len = 1;
2572
2573 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2574 cmdpos += char_len;
2575 }
2576
2577 msg_row = old_row;
2578 msg_col = old_col;
2579 }
2580}
2581#endif /* FEAT_XIM && FEAT_GUI_GTK */
2582
2583/*
2584 * Allocate a new command line buffer.
2585 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2586 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2587 */
2588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002589alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
2591 /*
2592 * give some extra space to avoid having to allocate all the time
2593 */
2594 if (len < 80)
2595 len = 100;
2596 else
2597 len += 20;
2598
2599 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2600 ccline.cmdbufflen = len;
2601}
2602
2603/*
2604 * Re-allocate the command line to length len + something extra.
2605 * return FAIL for failure, OK otherwise
2606 */
2607 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002608realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 char_u *p;
2611
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002612 if (len < ccline.cmdbufflen)
2613 return OK; /* no need to resize */
2614
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 p = ccline.cmdbuff;
2616 alloc_cmdbuff(len); /* will get some more */
2617 if (ccline.cmdbuff == NULL) /* out of memory */
2618 {
2619 ccline.cmdbuff = p; /* keep the old one */
2620 return FAIL;
2621 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002622 /* There isn't always a NUL after the command, but it may need to be
2623 * there, thus copy up to the NUL and add a NUL. */
2624 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2625 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002627
2628 if (ccline.xpc != NULL
2629 && ccline.xpc->xp_pattern != NULL
2630 && ccline.xpc->xp_context != EXPAND_NOTHING
2631 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2632 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002633 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002634
2635 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2636 * to point into the newly allocated memory. */
2637 if (i >= 0 && i <= ccline.cmdlen)
2638 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2639 }
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 return OK;
2642}
2643
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002644#if defined(FEAT_ARABIC) || defined(PROTO)
2645static char_u *arshape_buf = NULL;
2646
2647# if defined(EXITFREE) || defined(PROTO)
2648 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002649free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002650{
2651 vim_free(arshape_buf);
2652}
2653# endif
2654#endif
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656/*
2657 * Draw part of the cmdline at the current cursor position. But draw stars
2658 * when cmdline_star is TRUE.
2659 */
2660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002661draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2664 int i;
2665
2666 if (cmdline_star > 0)
2667 for (i = 0; i < len; ++i)
2668 {
2669 msg_putchar('*');
2670# ifdef FEAT_MBYTE
2671 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002672 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673# endif
2674 }
2675 else
2676#endif
2677#ifdef FEAT_ARABIC
2678 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 static int buflen = 0;
2681 char_u *p;
2682 int j;
2683 int newlen = 0;
2684 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002685 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 int prev_c = 0;
2687 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002688 int u8c;
2689 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 /*
2693 * Do arabic shaping into a temporary buffer. This is very
2694 * inefficient!
2695 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002696 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
2698 /* Re-allocate the buffer. We keep it around to avoid a lot of
2699 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002700 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002701 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002702 arshape_buf = alloc(buflen);
2703 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 return; /* out of memory */
2705 }
2706
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002707 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2708 {
2709 /* Prepend a space to draw the leading composing char on. */
2710 arshape_buf[0] = ' ';
2711 newlen = 1;
2712 }
2713
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 for (j = start; j < start + len; j += mb_l)
2715 {
2716 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002717 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002718 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (ARABIC_CHAR(u8c))
2720 {
2721 /* Do Arabic shaping. */
2722 if (cmdmsg_rl)
2723 {
2724 /* displaying from right to left */
2725 pc = prev_c;
2726 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002727 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (j + mb_l >= start + len)
2729 nc = NUL;
2730 else
2731 nc = utf_ptr2char(p + mb_l);
2732 }
2733 else
2734 {
2735 /* displaying from left to right */
2736 if (j + mb_l >= start + len)
2737 pc = NUL;
2738 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002739 {
2740 int pcc[MAX_MCO];
2741
2742 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002744 pc1 = pcc[0];
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 nc = prev_c;
2747 }
2748 prev_c = u8c;
2749
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002750 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002752 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002753 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002755 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2756 if (u8cc[1] != 0)
2757 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002758 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
2760 }
2761 else
2762 {
2763 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002764 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 newlen += mb_l;
2766 }
2767 }
2768
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002769 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 else
2772#endif
2773 msg_outtrans_len(ccline.cmdbuff + start, len);
2774}
2775
2776/*
2777 * Put a character on the command line. Shifts the following text to the
2778 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2779 * "c" must be printable (fit in one display cell)!
2780 */
2781 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002782putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 if (cmd_silent)
2785 return;
2786 msg_no_more = TRUE;
2787 msg_putchar(c);
2788 if (shift)
2789 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2790 msg_no_more = FALSE;
2791 cursorcmd();
2792}
2793
2794/*
2795 * Undo a putcmdline(c, FALSE).
2796 */
2797 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002798unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 if (cmd_silent)
2801 return;
2802 msg_no_more = TRUE;
2803 if (ccline.cmdlen == ccline.cmdpos)
2804 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002805#ifdef FEAT_MBYTE
2806 else if (has_mbyte)
2807 draw_cmdline(ccline.cmdpos,
2808 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 else
2811 draw_cmdline(ccline.cmdpos, 1);
2812 msg_no_more = FALSE;
2813 cursorcmd();
2814}
2815
2816/*
2817 * Put the given string, of the given length, onto the command line.
2818 * If len is -1, then STRLEN() is used to calculate the length.
2819 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2820 * part will be redrawn, otherwise it will not. If this function is called
2821 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2822 * called afterwards.
2823 */
2824 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002825put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
2827 int retval;
2828 int i;
2829 int m;
2830 int c;
2831
2832 if (len < 0)
2833 len = (int)STRLEN(str);
2834
2835 /* Check if ccline.cmdbuff needs to be longer */
2836 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002837 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 else
2839 retval = OK;
2840 if (retval == OK)
2841 {
2842 if (!ccline.overstrike)
2843 {
2844 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2845 ccline.cmdbuff + ccline.cmdpos,
2846 (size_t)(ccline.cmdlen - ccline.cmdpos));
2847 ccline.cmdlen += len;
2848 }
2849 else
2850 {
2851#ifdef FEAT_MBYTE
2852 if (has_mbyte)
2853 {
2854 /* Count nr of characters in the new string. */
2855 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002856 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 ++m;
2858 /* Count nr of bytes in cmdline that are overwritten by these
2859 * characters. */
2860 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002861 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 --m;
2863 if (i < ccline.cmdlen)
2864 {
2865 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2866 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2867 ccline.cmdlen += ccline.cmdpos + len - i;
2868 }
2869 else
2870 ccline.cmdlen = ccline.cmdpos + len;
2871 }
2872 else
2873#endif
2874 if (ccline.cmdpos + len > ccline.cmdlen)
2875 ccline.cmdlen = ccline.cmdpos + len;
2876 }
2877 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2878 ccline.cmdbuff[ccline.cmdlen] = NUL;
2879
2880#ifdef FEAT_MBYTE
2881 if (enc_utf8)
2882 {
2883 /* When the inserted text starts with a composing character,
2884 * backup to the character before it. There could be two of them.
2885 */
2886 i = 0;
2887 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2888 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2889 {
2890 i = (*mb_head_off)(ccline.cmdbuff,
2891 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2892 ccline.cmdpos -= i;
2893 len += i;
2894 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2895 }
2896# ifdef FEAT_ARABIC
2897 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2898 {
2899 /* Check the previous character for Arabic combining pair. */
2900 i = (*mb_head_off)(ccline.cmdbuff,
2901 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2902 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2903 + ccline.cmdpos - i), c))
2904 {
2905 ccline.cmdpos -= i;
2906 len += i;
2907 }
2908 else
2909 i = 0;
2910 }
2911# endif
2912 if (i != 0)
2913 {
2914 /* Also backup the cursor position. */
2915 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2916 ccline.cmdspos -= i;
2917 msg_col -= i;
2918 if (msg_col < 0)
2919 {
2920 msg_col += Columns;
2921 --msg_row;
2922 }
2923 }
2924 }
2925#endif
2926
2927 if (redraw && !cmd_silent)
2928 {
2929 msg_no_more = TRUE;
2930 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002931 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2933 /* Avoid clearing the rest of the line too often. */
2934 if (cmdline_row != i || ccline.overstrike)
2935 msg_clr_eos();
2936 msg_no_more = FALSE;
2937 }
2938#ifdef FEAT_FKMAP
2939 /*
2940 * If we are in Farsi command mode, the character input must be in
2941 * Insert mode. So do not advance the cmdpos.
2942 */
2943 if (!cmd_fkmap)
2944#endif
2945 {
2946 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002949 if (m < 0) /* overflow, Columns or Rows at weird value */
2950 m = MAXCOL;
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 else
2953 m = MAXCOL;
2954 for (i = 0; i < len; ++i)
2955 {
2956 c = cmdline_charsize(ccline.cmdpos);
2957#ifdef FEAT_MBYTE
2958 /* count ">" for a double-wide char that doesn't fit. */
2959 if (has_mbyte)
2960 correct_cmdspos(ccline.cmdpos, c);
2961#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002962 /* Stop cursor at the end of the screen, but do increment the
2963 * insert position, so that entering a very long command
2964 * works, even though you can't see it. */
2965 if (ccline.cmdspos + c < m)
2966 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967#ifdef FEAT_MBYTE
2968 if (has_mbyte)
2969 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002970 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (c > len - i - 1)
2972 c = len - i - 1;
2973 ccline.cmdpos += c;
2974 i += c;
2975 }
2976#endif
2977 ++ccline.cmdpos;
2978 }
2979 }
2980 }
2981 if (redraw)
2982 msg_check();
2983 return retval;
2984}
2985
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002986static struct cmdline_info prev_ccline;
2987static int prev_ccline_used = FALSE;
2988
2989/*
2990 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2991 * and overwrite it. But get_cmdline_str() may need it, thus make it
2992 * available globally in prev_ccline.
2993 */
2994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002995save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002996{
2997 if (!prev_ccline_used)
2998 {
2999 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3000 prev_ccline_used = TRUE;
3001 }
3002 *ccp = prev_ccline;
3003 prev_ccline = ccline;
3004 ccline.cmdbuff = NULL;
3005 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003006 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003007}
3008
3009/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003010 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003011 */
3012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003013restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003014{
3015 ccline = prev_ccline;
3016 prev_ccline = *ccp;
3017}
3018
Bram Moolenaar5a305422006-04-28 22:38:25 +00003019#if defined(FEAT_EVAL) || defined(PROTO)
3020/*
3021 * Save the command line into allocated memory. Returns a pointer to be
3022 * passed to restore_cmdline_alloc() later.
3023 * Returns NULL when failed.
3024 */
3025 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003026save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003027{
3028 struct cmdline_info *p;
3029
3030 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3031 if (p != NULL)
3032 save_cmdline(p);
3033 return (char_u *)p;
3034}
3035
3036/*
3037 * Restore the command line from the return value of save_cmdline_alloc().
3038 */
3039 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003040restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003041{
3042 if (p != NULL)
3043 {
3044 restore_cmdline((struct cmdline_info *)p);
3045 vim_free(p);
3046 }
3047}
3048#endif
3049
Bram Moolenaar8299df92004-07-10 09:47:34 +00003050/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003051 * Paste a yank register into the command line.
3052 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003053 * insert_reg() can't be used here, because special characters from the
3054 * register contents will be interpreted as commands.
3055 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003056 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003057 */
3058 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003059cmdline_paste(
3060 int regname,
3061 int literally, /* Insert text literally instead of "as typed" */
3062 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003063{
3064 long i;
3065 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003066 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003067 int allocated;
3068 struct cmdline_info save_ccline;
3069
3070 /* check for valid regname; also accept special characters for CTRL-R in
3071 * the command line */
3072 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3073 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3074 return FAIL;
3075
3076 /* A register containing CTRL-R can cause an endless loop. Allow using
3077 * CTRL-C to break the loop. */
3078 line_breakcheck();
3079 if (got_int)
3080 return FAIL;
3081
3082#ifdef FEAT_CLIPBOARD
3083 regname = may_get_selection(regname);
3084#endif
3085
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003086 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003087 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003088 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003089 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003090 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003091 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003092 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003093
3094 if (i)
3095 {
3096 /* Got the value of a special register in "arg". */
3097 if (arg == NULL)
3098 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003099
3100 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3101 * part of the word. */
3102 p = arg;
3103 if (p_is && regname == Ctrl_W)
3104 {
3105 char_u *w;
3106 int len;
3107
3108 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003109 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003110 {
3111#ifdef FEAT_MBYTE
3112 if (has_mbyte)
3113 {
3114 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3115 if (!vim_iswordc(mb_ptr2char(w - len)))
3116 break;
3117 w -= len;
3118 }
3119 else
3120#endif
3121 {
3122 if (!vim_iswordc(w[-1]))
3123 break;
3124 --w;
3125 }
3126 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003127 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003128 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3129 p += len;
3130 }
3131
3132 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003133 if (allocated)
3134 vim_free(arg);
3135 return OK;
3136 }
3137
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003138 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003139}
3140
3141/*
3142 * Put a string on the command line.
3143 * When "literally" is TRUE, insert literally.
3144 * When "literally" is FALSE, insert as typed, but don't leave the command
3145 * line.
3146 */
3147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003148cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003149{
3150 int c, cv;
3151
3152 if (literally)
3153 put_on_cmdline(s, -1, TRUE);
3154 else
3155 while (*s != NUL)
3156 {
3157 cv = *s;
3158 if (cv == Ctrl_V && s[1])
3159 ++s;
3160#ifdef FEAT_MBYTE
3161 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003162 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003163 else
3164#endif
3165 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003166 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3167 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003168#ifdef UNIX
3169 || c == intr_char
3170#endif
3171 || (c == Ctrl_BSL && *s == Ctrl_N))
3172 stuffcharReadbuff(Ctrl_V);
3173 stuffcharReadbuff(c);
3174 }
3175}
3176
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177#ifdef FEAT_WILDMENU
3178/*
3179 * Delete characters on the command line, from "from" to the current
3180 * position.
3181 */
3182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003183cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3186 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3187 ccline.cmdlen -= ccline.cmdpos - from;
3188 ccline.cmdpos = from;
3189}
3190#endif
3191
3192/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003193 * This function is called when the screen size changes and with incremental
3194 * search and in other situations where the command line may have been
3195 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 */
3197 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003198redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
3200 if (cmd_silent)
3201 return;
3202 need_wait_return = FALSE;
3203 compute_cmdrow();
3204 redrawcmd();
3205 cursorcmd();
3206}
3207
3208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003209redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 int i;
3212
3213 if (cmd_silent)
3214 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003215 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 msg_putchar(ccline.cmdfirstc);
3217 if (ccline.cmdprompt != NULL)
3218 {
3219 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3220 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3221 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003222 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 --ccline.cmdindent;
3224 }
3225 else
3226 for (i = ccline.cmdindent; i > 0; --i)
3227 msg_putchar(' ');
3228}
3229
3230/*
3231 * Redraw what is currently on the command line.
3232 */
3233 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003234redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235{
3236 if (cmd_silent)
3237 return;
3238
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003239 /* when 'incsearch' is set there may be no command line while redrawing */
3240 if (ccline.cmdbuff == NULL)
3241 {
3242 windgoto(cmdline_row, 0);
3243 msg_clr_eos();
3244 return;
3245 }
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 msg_start();
3248 redrawcmdprompt();
3249
3250 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3251 msg_no_more = TRUE;
3252 draw_cmdline(0, ccline.cmdlen);
3253 msg_clr_eos();
3254 msg_no_more = FALSE;
3255
3256 set_cmdspos_cursor();
3257
3258 /*
3259 * An emsg() before may have set msg_scroll. This is used in normal mode,
3260 * in cmdline mode we can reset them now.
3261 */
3262 msg_scroll = FALSE; /* next message overwrites cmdline */
3263
3264 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3265 * in cmdline mode */
3266 skip_redraw = FALSE;
3267}
3268
3269 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003270compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003272 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 cmdline_row = Rows - 1;
3274 else
3275 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3276 + W_STATUS_HEIGHT(lastwin);
3277}
3278
3279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003280cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
3282 if (cmd_silent)
3283 return;
3284
3285#ifdef FEAT_RIGHTLEFT
3286 if (cmdmsg_rl)
3287 {
3288 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3289 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3290 if (msg_row <= 0)
3291 msg_row = Rows - 1;
3292 }
3293 else
3294#endif
3295 {
3296 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3297 msg_col = ccline.cmdspos % (int)Columns;
3298 if (msg_row >= Rows)
3299 msg_row = Rows - 1;
3300 }
3301
3302 windgoto(msg_row, msg_col);
3303#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3304 redrawcmd_preedit();
3305#endif
3306#ifdef MCH_CURSOR_SHAPE
3307 mch_update_cursor();
3308#endif
3309}
3310
3311 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003312gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313{
3314 msg_start();
3315#ifdef FEAT_RIGHTLEFT
3316 if (cmdmsg_rl)
3317 msg_col = Columns - 1;
3318 else
3319#endif
3320 msg_col = 0; /* always start in column 0 */
3321 if (clr) /* clear the bottom line(s) */
3322 msg_clr_eos(); /* will reset clear_cmdline */
3323 windgoto(cmdline_row, 0);
3324}
3325
3326/*
3327 * Check the word in front of the cursor for an abbreviation.
3328 * Called when the non-id character "c" has been entered.
3329 * When an abbreviation is recognized it is removed from the text with
3330 * backspaces and the replacement string is inserted, followed by "c".
3331 */
3332 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003333ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3336 return FALSE;
3337
3338 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3339}
3340
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003341#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3342 static int
3343#ifdef __BORLANDC__
3344_RTLENTRYF
3345#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003346sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003347{
3348 char_u *p1 = *(char_u **)s1;
3349 char_u *p2 = *(char_u **)s2;
3350
3351 if (*p1 != '<' && *p2 == '<') return -1;
3352 if (*p1 == '<' && *p2 != '<') return 1;
3353 return STRCMP(p1, p2);
3354}
3355#endif
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357/*
3358 * Return FAIL if this is not an appropriate context in which to do
3359 * completion of anything, return OK if it is (even if there are no matches).
3360 * For the caller, this means that the character is just passed through like a
3361 * normal character (instead of being expanded). This allows :s/^I^D etc.
3362 */
3363 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003364nextwild(
3365 expand_T *xp,
3366 int type,
3367 int options, /* extra options for ExpandOne() */
3368 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 int i, j;
3371 char_u *p1;
3372 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 int difflen;
3374 int v;
3375
3376 if (xp->xp_numfiles == -1)
3377 {
3378 set_expand_context(xp);
3379 cmd_showtail = expand_showtail(xp);
3380 }
3381
3382 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3383 {
3384 beep_flush();
3385 return OK; /* Something illegal on command line */
3386 }
3387 if (xp->xp_context == EXPAND_NOTHING)
3388 {
3389 /* Caller can use the character as a normal char instead */
3390 return FAIL;
3391 }
3392
3393 MSG_PUTS("..."); /* show that we are busy */
3394 out_flush();
3395
3396 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003397 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
3399 if (type == WILD_NEXT || type == WILD_PREV)
3400 {
3401 /*
3402 * Get next/previous match for a previous expanded pattern.
3403 */
3404 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3405 }
3406 else
3407 {
3408 /*
3409 * Translate string into pattern and expand it.
3410 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003411 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3412 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 p2 = NULL;
3414 else
3415 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003416 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003417 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3418 if (escape)
3419 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003420
3421 if (p_wic)
3422 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003423 p2 = ExpandOne(xp, p1,
3424 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003425 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003427 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 if (p2 != NULL && type == WILD_LONGEST)
3429 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003430 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 if (ccline.cmdbuff[i + j] == '*'
3432 || ccline.cmdbuff[i + j] == '?')
3433 break;
3434 if ((int)STRLEN(p2) < j)
3435 {
3436 vim_free(p2);
3437 p2 = NULL;
3438 }
3439 }
3440 }
3441 }
3442
3443 if (p2 != NULL && !got_int)
3444 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003445 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003446 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003448 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 xp->xp_pattern = ccline.cmdbuff + i;
3450 }
3451 else
3452 v = OK;
3453 if (v == OK)
3454 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003455 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3456 &ccline.cmdbuff[ccline.cmdpos],
3457 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3458 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 ccline.cmdlen += difflen;
3460 ccline.cmdpos += difflen;
3461 }
3462 }
3463 vim_free(p2);
3464
3465 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003466 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467
3468 /* When expanding a ":map" command and no matches are found, assume that
3469 * the key is supposed to be inserted literally */
3470 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3471 return FAIL;
3472
3473 if (xp->xp_numfiles <= 0 && p2 == NULL)
3474 beep_flush();
3475 else if (xp->xp_numfiles == 1)
3476 /* free expanded pattern */
3477 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3478
3479 return OK;
3480}
3481
3482/*
3483 * Do wildcard expansion on the string 'str'.
3484 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003485 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 * Return NULL for failure.
3487 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003488 * "orig" is the originally expanded string, copied to allocated memory. It
3489 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3490 * WILD_PREV "orig" should be NULL.
3491 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003492 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3493 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 *
3495 * mode = WILD_FREE: just free previously expanded matches
3496 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3497 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3498 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3499 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3500 * mode = WILD_ALL: return all matches concatenated
3501 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003502 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 *
3504 * options = WILD_LIST_NOTFOUND: list entries without a match
3505 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3506 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3507 * options = WILD_NO_BEEP: Don't beep for multiple matches
3508 * options = WILD_ADD_SLASH: add a slash after directory names
3509 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3510 * options = WILD_SILENT: don't print warning messages
3511 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003512 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 *
3514 * The variables xp->xp_context and xp->xp_backslash must have been set!
3515 */
3516 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003517ExpandOne(
3518 expand_T *xp,
3519 char_u *str,
3520 char_u *orig, /* allocated copy of original of expanded string */
3521 int options,
3522 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523{
3524 char_u *ss = NULL;
3525 static int findex;
3526 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003527 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 int i;
3529 long_u len;
3530 int non_suf_match; /* number without matching suffix */
3531
3532 /*
3533 * first handle the case of using an old match
3534 */
3535 if (mode == WILD_NEXT || mode == WILD_PREV)
3536 {
3537 if (xp->xp_numfiles > 0)
3538 {
3539 if (mode == WILD_PREV)
3540 {
3541 if (findex == -1)
3542 findex = xp->xp_numfiles;
3543 --findex;
3544 }
3545 else /* mode == WILD_NEXT */
3546 ++findex;
3547
3548 /*
3549 * When wrapping around, return the original string, set findex to
3550 * -1.
3551 */
3552 if (findex < 0)
3553 {
3554 if (orig_save == NULL)
3555 findex = xp->xp_numfiles - 1;
3556 else
3557 findex = -1;
3558 }
3559 if (findex >= xp->xp_numfiles)
3560 {
3561 if (orig_save == NULL)
3562 findex = 0;
3563 else
3564 findex = -1;
3565 }
3566#ifdef FEAT_WILDMENU
3567 if (p_wmnu)
3568 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3569 findex, cmd_showtail);
3570#endif
3571 if (findex == -1)
3572 return vim_strsave(orig_save);
3573 return vim_strsave(xp->xp_files[findex]);
3574 }
3575 else
3576 return NULL;
3577 }
3578
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003579 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3581 {
3582 FreeWild(xp->xp_numfiles, xp->xp_files);
3583 xp->xp_numfiles = -1;
3584 vim_free(orig_save);
3585 orig_save = NULL;
3586 }
3587 findex = 0;
3588
3589 if (mode == WILD_FREE) /* only release file name */
3590 return NULL;
3591
3592 if (xp->xp_numfiles == -1)
3593 {
3594 vim_free(orig_save);
3595 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003596 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
3598 /*
3599 * Do the expansion.
3600 */
3601 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3602 options) == FAIL)
3603 {
3604#ifdef FNAME_ILLEGAL
3605 /* Illegal file name has been silently skipped. But when there
3606 * are wildcards, the real problem is that there was no match,
3607 * causing the pattern to be added, which has illegal characters.
3608 */
3609 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3610 EMSG2(_(e_nomatch2), str);
3611#endif
3612 }
3613 else if (xp->xp_numfiles == 0)
3614 {
3615 if (!(options & WILD_SILENT))
3616 EMSG2(_(e_nomatch2), str);
3617 }
3618 else
3619 {
3620 /* Escape the matches for use on the command line. */
3621 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3622
3623 /*
3624 * Check for matching suffixes in file names.
3625 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003626 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3627 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 {
3629 if (xp->xp_numfiles)
3630 non_suf_match = xp->xp_numfiles;
3631 else
3632 non_suf_match = 1;
3633 if ((xp->xp_context == EXPAND_FILES
3634 || xp->xp_context == EXPAND_DIRECTORIES)
3635 && xp->xp_numfiles > 1)
3636 {
3637 /*
3638 * More than one match; check suffix.
3639 * The files will have been sorted on matching suffix in
3640 * expand_wildcards, only need to check the first two.
3641 */
3642 non_suf_match = 0;
3643 for (i = 0; i < 2; ++i)
3644 if (match_suffix(xp->xp_files[i]))
3645 ++non_suf_match;
3646 }
3647 if (non_suf_match != 1)
3648 {
3649 /* Can we ever get here unless it's while expanding
3650 * interactively? If not, we can get rid of this all
3651 * together. Don't really want to wait for this message
3652 * (and possibly have to hit return to continue!).
3653 */
3654 if (!(options & WILD_SILENT))
3655 EMSG(_(e_toomany));
3656 else if (!(options & WILD_NO_BEEP))
3657 beep_flush();
3658 }
3659 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3660 ss = vim_strsave(xp->xp_files[0]);
3661 }
3662 }
3663 }
3664
3665 /* Find longest common part */
3666 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3667 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003668 int mb_len = 1;
3669 int c0, ci;
3670
3671 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003673#ifdef FEAT_MBYTE
3674 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003676 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3677 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3678 }
3679 else
3680#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003681 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003682 for (i = 1; i < xp->xp_numfiles; ++i)
3683 {
3684#ifdef FEAT_MBYTE
3685 if (has_mbyte)
3686 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3687 else
3688#endif
3689 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003690 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003692 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003693 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003695 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 break;
3697 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003698 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 break;
3700 }
3701 if (i < xp->xp_numfiles)
3702 {
3703 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003704 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 break;
3706 }
3707 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 ss = alloc((unsigned)len + 1);
3710 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003711 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 findex = -1; /* next p_wc gets first one */
3713 }
3714
3715 /* Concatenate all matching names */
3716 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3717 {
3718 len = 0;
3719 for (i = 0; i < xp->xp_numfiles; ++i)
3720 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3721 ss = lalloc(len, TRUE);
3722 if (ss != NULL)
3723 {
3724 *ss = NUL;
3725 for (i = 0; i < xp->xp_numfiles; ++i)
3726 {
3727 STRCAT(ss, xp->xp_files[i]);
3728 if (i != xp->xp_numfiles - 1)
3729 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3730 }
3731 }
3732 }
3733
3734 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3735 ExpandCleanup(xp);
3736
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003737 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003738 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003739 vim_free(orig);
3740
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 return ss;
3742}
3743
3744/*
3745 * Prepare an expand structure for use.
3746 */
3747 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003748ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003750 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003751 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003753#ifndef BACKSLASH_IN_FILENAME
3754 xp->xp_shell = FALSE;
3755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 xp->xp_numfiles = -1;
3757 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003758#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3759 xp->xp_arg = NULL;
3760#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003761 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762}
3763
3764/*
3765 * Cleanup an expand structure after use.
3766 */
3767 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003768ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
3770 if (xp->xp_numfiles >= 0)
3771 {
3772 FreeWild(xp->xp_numfiles, xp->xp_files);
3773 xp->xp_numfiles = -1;
3774 }
3775}
3776
3777 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003778ExpandEscape(
3779 expand_T *xp,
3780 char_u *str,
3781 int numfiles,
3782 char_u **files,
3783 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
3785 int i;
3786 char_u *p;
3787
3788 /*
3789 * May change home directory back to "~"
3790 */
3791 if (options & WILD_HOME_REPLACE)
3792 tilde_replace(str, numfiles, files);
3793
3794 if (options & WILD_ESCAPE)
3795 {
3796 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003797 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003798 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 || xp->xp_context == EXPAND_BUFFERS
3800 || xp->xp_context == EXPAND_DIRECTORIES)
3801 {
3802 /*
3803 * Insert a backslash into a file name before a space, \, %, #
3804 * and wildmatch characters, except '~'.
3805 */
3806 for (i = 0; i < numfiles; ++i)
3807 {
3808 /* for ":set path=" we need to escape spaces twice */
3809 if (xp->xp_backslash == XP_BS_THREE)
3810 {
3811 p = vim_strsave_escaped(files[i], (char_u *)" ");
3812 if (p != NULL)
3813 {
3814 vim_free(files[i]);
3815 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003816#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 p = vim_strsave_escaped(files[i], (char_u *)" ");
3818 if (p != NULL)
3819 {
3820 vim_free(files[i]);
3821 files[i] = p;
3822 }
3823#endif
3824 }
3825 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003826#ifdef BACKSLASH_IN_FILENAME
3827 p = vim_strsave_fnameescape(files[i], FALSE);
3828#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003829 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 if (p != NULL)
3832 {
3833 vim_free(files[i]);
3834 files[i] = p;
3835 }
3836
3837 /* If 'str' starts with "\~", replace "~" at start of
3838 * files[i] with "\~". */
3839 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003840 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003843
3844 /* If the first file starts with a '+' escape it. Otherwise it
3845 * could be seen as "+cmd". */
3846 if (*files[0] == '+')
3847 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 }
3849 else if (xp->xp_context == EXPAND_TAGS)
3850 {
3851 /*
3852 * Insert a backslash before characters in a tag name that
3853 * would terminate the ":tag" command.
3854 */
3855 for (i = 0; i < numfiles; ++i)
3856 {
3857 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3858 if (p != NULL)
3859 {
3860 vim_free(files[i]);
3861 files[i] = p;
3862 }
3863 }
3864 }
3865 }
3866}
3867
3868/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003869 * Escape special characters in "fname" for when used as a file name argument
3870 * after a Vim command, or, when "shell" is non-zero, a shell command.
3871 * Returns the result in allocated memory.
3872 */
3873 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003874vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003875{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003876 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003877#ifdef BACKSLASH_IN_FILENAME
3878 char_u buf[20];
3879 int j = 0;
3880
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003881 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003882 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003883 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003884 buf[j++] = *p;
3885 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003886 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003887#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003888 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3889 if (shell && csh_like_shell() && p != NULL)
3890 {
3891 char_u *s;
3892
3893 /* For csh and similar shells need to put two backslashes before '!'.
3894 * One is taken by Vim, one by the shell. */
3895 s = vim_strsave_escaped(p, (char_u *)"!");
3896 vim_free(p);
3897 p = s;
3898 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003899#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003900
3901 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3902 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003903 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003904 escape_fname(&p);
3905
3906 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003907}
3908
3909/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003910 * Put a backslash before the file name in "pp", which is in allocated memory.
3911 */
3912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003913escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003914{
3915 char_u *p;
3916
3917 p = alloc((unsigned)(STRLEN(*pp) + 2));
3918 if (p != NULL)
3919 {
3920 p[0] = '\\';
3921 STRCPY(p + 1, *pp);
3922 vim_free(*pp);
3923 *pp = p;
3924 }
3925}
3926
3927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 * For each file name in files[num_files]:
3929 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3930 */
3931 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003932tilde_replace(
3933 char_u *orig_pat,
3934 int num_files,
3935 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937 int i;
3938 char_u *p;
3939
3940 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3941 {
3942 for (i = 0; i < num_files; ++i)
3943 {
3944 p = home_replace_save(NULL, files[i]);
3945 if (p != NULL)
3946 {
3947 vim_free(files[i]);
3948 files[i] = p;
3949 }
3950 }
3951 }
3952}
3953
3954/*
3955 * Show all matches for completion on the command line.
3956 * Returns EXPAND_NOTHING when the character that triggered expansion should
3957 * be inserted like a normal character.
3958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003960showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961{
3962#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3963 int num_files;
3964 char_u **files_found;
3965 int i, j, k;
3966 int maxlen;
3967 int lines;
3968 int columns;
3969 char_u *p;
3970 int lastlen;
3971 int attr;
3972 int showtail;
3973
3974 if (xp->xp_numfiles == -1)
3975 {
3976 set_expand_context(xp);
3977 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3978 &num_files, &files_found);
3979 showtail = expand_showtail(xp);
3980 if (i != EXPAND_OK)
3981 return i;
3982
3983 }
3984 else
3985 {
3986 num_files = xp->xp_numfiles;
3987 files_found = xp->xp_files;
3988 showtail = cmd_showtail;
3989 }
3990
3991#ifdef FEAT_WILDMENU
3992 if (!wildmenu)
3993 {
3994#endif
3995 msg_didany = FALSE; /* lines_left will be set */
3996 msg_start(); /* prepare for paging */
3997 msg_putchar('\n');
3998 out_flush();
3999 cmdline_row = msg_row;
4000 msg_didany = FALSE; /* lines_left will be set again */
4001 msg_start(); /* prepare for paging */
4002#ifdef FEAT_WILDMENU
4003 }
4004#endif
4005
4006 if (got_int)
4007 got_int = FALSE; /* only int. the completion, not the cmd line */
4008#ifdef FEAT_WILDMENU
4009 else if (wildmenu)
4010 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
4011#endif
4012 else
4013 {
4014 /* find the length of the longest file name */
4015 maxlen = 0;
4016 for (i = 0; i < num_files; ++i)
4017 {
4018 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004019 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 || xp->xp_context == EXPAND_BUFFERS))
4021 {
4022 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4023 j = vim_strsize(NameBuff);
4024 }
4025 else
4026 j = vim_strsize(L_SHOWFILE(i));
4027 if (j > maxlen)
4028 maxlen = j;
4029 }
4030
4031 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4032 lines = num_files;
4033 else
4034 {
4035 /* compute the number of columns and lines for the listing */
4036 maxlen += 2; /* two spaces between file names */
4037 columns = ((int)Columns + 2) / maxlen;
4038 if (columns < 1)
4039 columns = 1;
4040 lines = (num_files + columns - 1) / columns;
4041 }
4042
4043 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4044
4045 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4046 {
4047 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4048 msg_clr_eos();
4049 msg_advance(maxlen - 3);
4050 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4051 }
4052
4053 /* list the files line by line */
4054 for (i = 0; i < lines; ++i)
4055 {
4056 lastlen = 999;
4057 for (k = i; k < num_files; k += lines)
4058 {
4059 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4060 {
4061 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4062 p = files_found[k] + STRLEN(files_found[k]) + 1;
4063 msg_advance(maxlen + 1);
4064 msg_puts(p);
4065 msg_advance(maxlen + 3);
4066 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4067 break;
4068 }
4069 for (j = maxlen - lastlen; --j >= 0; )
4070 msg_putchar(' ');
4071 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004072 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 || xp->xp_context == EXPAND_BUFFERS)
4074 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004075 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004076 if (xp->xp_numfiles != -1)
4077 {
4078 char_u *halved_slash;
4079 char_u *exp_path;
4080
4081 /* Expansion was done before and special characters
4082 * were escaped, need to halve backslashes. Also
4083 * $HOME has been replaced with ~/. */
4084 exp_path = expand_env_save_opt(files_found[k], TRUE);
4085 halved_slash = backslash_halve_save(
4086 exp_path != NULL ? exp_path : files_found[k]);
4087 j = mch_isdir(halved_slash != NULL ? halved_slash
4088 : files_found[k]);
4089 vim_free(exp_path);
4090 vim_free(halved_slash);
4091 }
4092 else
4093 /* Expansion was done here, file names are literal. */
4094 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (showtail)
4096 p = L_SHOWFILE(k);
4097 else
4098 {
4099 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4100 TRUE);
4101 p = NameBuff;
4102 }
4103 }
4104 else
4105 {
4106 j = FALSE;
4107 p = L_SHOWFILE(k);
4108 }
4109 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4110 }
4111 if (msg_col > 0) /* when not wrapped around */
4112 {
4113 msg_clr_eos();
4114 msg_putchar('\n');
4115 }
4116 out_flush(); /* show one line at a time */
4117 if (got_int)
4118 {
4119 got_int = FALSE;
4120 break;
4121 }
4122 }
4123
4124 /*
4125 * we redraw the command below the lines that we have just listed
4126 * This is a bit tricky, but it saves a lot of screen updating.
4127 */
4128 cmdline_row = msg_row; /* will put it back later */
4129 }
4130
4131 if (xp->xp_numfiles == -1)
4132 FreeWild(num_files, files_found);
4133
4134 return EXPAND_OK;
4135}
4136
4137/*
4138 * Private gettail for showmatches() (and win_redr_status_matches()):
4139 * Find tail of file name path, but ignore trailing "/".
4140 */
4141 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004142sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144 char_u *p;
4145 char_u *t = s;
4146 int had_sep = FALSE;
4147
4148 for (p = s; *p != NUL; )
4149 {
4150 if (vim_ispathsep(*p)
4151#ifdef BACKSLASH_IN_FILENAME
4152 && !rem_backslash(p)
4153#endif
4154 )
4155 had_sep = TRUE;
4156 else if (had_sep)
4157 {
4158 t = p;
4159 had_sep = FALSE;
4160 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004161 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
4163 return t;
4164}
4165
4166/*
4167 * Return TRUE if we only need to show the tail of completion matches.
4168 * When not completing file names or there is a wildcard in the path FALSE is
4169 * returned.
4170 */
4171 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004172expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173{
4174 char_u *s;
4175 char_u *end;
4176
4177 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004178 if (xp->xp_context != EXPAND_FILES
4179 && xp->xp_context != EXPAND_SHELLCMD
4180 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 return FALSE;
4182
4183 end = gettail(xp->xp_pattern);
4184 if (end == xp->xp_pattern) /* there is no path separator */
4185 return FALSE;
4186
4187 for (s = xp->xp_pattern; s < end; s++)
4188 {
4189 /* Skip escaped wildcards. Only when the backslash is not a path
4190 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4191 if (rem_backslash(s))
4192 ++s;
4193 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4194 return FALSE;
4195 }
4196 return TRUE;
4197}
4198
4199/*
4200 * Prepare a string for expansion.
4201 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004202 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 * When expanding other names: The string will be used with regcomp(). Copy
4204 * the name into allocated memory and prepend "^".
4205 */
4206 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004207addstar(
4208 char_u *fname,
4209 int len,
4210 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
4212 char_u *retval;
4213 int i, j;
4214 int new_len;
4215 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004216 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004218 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004219 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004220 && context != EXPAND_SHELLCMD
4221 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
4223 /*
4224 * Matching will be done internally (on something other than files).
4225 * So we convert the file-matching-type wildcards into our kind for
4226 * use with vim_regcomp(). First work out how long it will be:
4227 */
4228
4229 /* For help tags the translation is done in find_help_tags().
4230 * For a tag pattern starting with "/" no translation is needed. */
4231 if (context == EXPAND_HELP
4232 || context == EXPAND_COLORS
4233 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004234 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004235 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004236 || context == EXPAND_PACKADD
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/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004496 * Cleanup matches for help tags:
4497 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4498 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004500static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004503cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504{
4505 int i, j;
4506 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004507 char_u buf[4];
4508 char_u *p = buf;
4509
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004510 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004511 {
4512 *p++ = '@';
4513 *p++ = p_hlg[0];
4514 *p++ = p_hlg[1];
4515 }
4516 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
4518 for (i = 0; i < num_file; ++i)
4519 {
4520 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004521 if (len <= 0)
4522 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004523 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 {
4525 /* Sorting on priority means the same item in another language may
4526 * be anywhere. Search all items for a match up to the "@en". */
4527 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004528 if (j != i && (int)STRLEN(file[j]) == len + 3
4529 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 break;
4531 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004532 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 file[i][len] = NUL;
4534 }
4535 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004536
4537 if (*buf != NUL)
4538 for (i = 0; i < num_file; ++i)
4539 {
4540 len = (int)STRLEN(file[i]) - 3;
4541 if (len <= 0)
4542 continue;
4543 if (STRCMP(file[i] + len, buf) == 0)
4544 {
4545 /* remove the default language */
4546 file[i][len] = NUL;
4547 }
4548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549}
4550#endif
4551
4552/*
4553 * Do the expansion based on xp->xp_context and "pat".
4554 */
4555 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004556ExpandFromContext(
4557 expand_T *xp,
4558 char_u *pat,
4559 int *num_file,
4560 char_u ***file,
4561 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562{
4563#ifdef FEAT_CMDL_COMPL
4564 regmatch_T regmatch;
4565#endif
4566 int ret;
4567 int flags;
4568
4569 flags = EW_DIR; /* include directories */
4570 if (options & WILD_LIST_NOTFOUND)
4571 flags |= EW_NOTFOUND;
4572 if (options & WILD_ADD_SLASH)
4573 flags |= EW_ADDSLASH;
4574 if (options & WILD_KEEP_ALL)
4575 flags |= EW_KEEPALL;
4576 if (options & WILD_SILENT)
4577 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004578 if (options & WILD_ALLLINKS)
4579 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004581 if (xp->xp_context == EXPAND_FILES
4582 || xp->xp_context == EXPAND_DIRECTORIES
4583 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
4585 /*
4586 * Expand file or directory names.
4587 */
4588 int free_pat = FALSE;
4589 int i;
4590
4591 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4592 * space */
4593 if (xp->xp_backslash != XP_BS_NONE)
4594 {
4595 free_pat = TRUE;
4596 pat = vim_strsave(pat);
4597 for (i = 0; pat[i]; ++i)
4598 if (pat[i] == '\\')
4599 {
4600 if (xp->xp_backslash == XP_BS_THREE
4601 && pat[i + 1] == '\\'
4602 && pat[i + 2] == '\\'
4603 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004604 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (xp->xp_backslash == XP_BS_ONE
4606 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004607 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609 }
4610
4611 if (xp->xp_context == EXPAND_FILES)
4612 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004613 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4614 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 else
4616 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004617 if (options & WILD_ICASE)
4618 flags |= EW_ICASE;
4619
Bram Moolenaard7834d32009-12-02 16:14:36 +00004620 /* Expand wildcards, supporting %:h and the like. */
4621 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (free_pat)
4623 vim_free(pat);
4624 return ret;
4625 }
4626
4627 *file = (char_u **)"";
4628 *num_file = 0;
4629 if (xp->xp_context == EXPAND_HELP)
4630 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004631 /* With an empty argument we would get all the help tags, which is
4632 * very slow. Get matches for "help" instead. */
4633 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4634 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 {
4636#ifdef FEAT_MULTI_LANG
4637 cleanup_help_tags(*num_file, *file);
4638#endif
4639 return OK;
4640 }
4641 return FAIL;
4642 }
4643
4644#ifndef FEAT_CMDL_COMPL
4645 return FAIL;
4646#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004647 if (xp->xp_context == EXPAND_SHELLCMD)
4648 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if (xp->xp_context == EXPAND_OLD_SETTING)
4650 return ExpandOldSetting(num_file, file);
4651 if (xp->xp_context == EXPAND_BUFFERS)
4652 return ExpandBufnames(pat, num_file, file, options);
4653 if (xp->xp_context == EXPAND_TAGS
4654 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4655 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4656 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004657 {
4658 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004659 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4660 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004663 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004664 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004665 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004666 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004667 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004668 {
4669 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004670 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004671 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004672 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004673 {
4674 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004675 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004676 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004677# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4678 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004679 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004680# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004681 if (xp->xp_context == EXPAND_PACKADD)
4682 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
4684 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4685 if (regmatch.regprog == NULL)
4686 return FAIL;
4687
4688 /* set ignore-case according to p_ic, p_scs and pat */
4689 regmatch.rm_ic = ignorecase(pat);
4690
4691 if (xp->xp_context == EXPAND_SETTINGS
4692 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4693 ret = ExpandSettings(xp, &regmatch, num_file, file);
4694 else if (xp->xp_context == EXPAND_MAPPINGS)
4695 ret = ExpandMappings(&regmatch, num_file, file);
4696# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4697 else if (xp->xp_context == EXPAND_USER_DEFINED)
4698 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4699# endif
4700 else
4701 {
4702 static struct expgen
4703 {
4704 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004705 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004707 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 } tab[] =
4709 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004710 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4711 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004712#ifdef FEAT_CMDHIST
4713 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004716 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004717 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004718 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4719 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4720 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721#endif
4722#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004723 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4724 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4725 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4726 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727#endif
4728#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004729 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4730 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731#endif
4732#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004733 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004735#ifdef FEAT_PROFILE
4736 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4737#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004738 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004740 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4741 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004743#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004744 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004745#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004746#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004747 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004748#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004749#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004750 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4753 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004754 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4755 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004757 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004758 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 };
4760 int i;
4761
4762 /*
4763 * Find a context in the table and call the ExpandGeneric() with the
4764 * right function to do the expansion.
4765 */
4766 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004767 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (xp->xp_context == tab[i].context)
4769 {
4770 if (tab[i].ic)
4771 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004772 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004773 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 break;
4775 }
4776 }
4777
Bram Moolenaar473de612013-06-08 18:19:48 +02004778 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 return ret;
4781#endif /* FEAT_CMDL_COMPL */
4782}
4783
4784#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4785/*
4786 * Expand a list of names.
4787 *
4788 * Generic function for command line completion. It calls a function to
4789 * obtain strings, one by one. The strings are matched against a regexp
4790 * program. Matching strings are copied into an array, which is returned.
4791 *
4792 * Returns OK when no problems encountered, FAIL for error (out of memory).
4793 */
4794 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004795ExpandGeneric(
4796 expand_T *xp,
4797 regmatch_T *regmatch,
4798 int *num_file,
4799 char_u ***file,
4800 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004802 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
4804 int i;
4805 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004806 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 char_u *str;
4808
4809 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004810 * round == 0: count the number of matching names
4811 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004813 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
4815 for (i = 0; ; ++i)
4816 {
4817 str = (*func)(xp, i);
4818 if (str == NULL) /* end of list */
4819 break;
4820 if (*str == NUL) /* skip empty strings */
4821 continue;
4822
4823 if (vim_regexec(regmatch, str, (colnr_T)0))
4824 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004825 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004827 if (escaped)
4828 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4829 else
4830 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 (*file)[count] = str;
4832#ifdef FEAT_MENU
4833 if (func == get_menu_names && str != NULL)
4834 {
4835 /* test for separator added by get_menu_names() */
4836 str += STRLEN(str) - 1;
4837 if (*str == '\001')
4838 *str = '.';
4839 }
4840#endif
4841 }
4842 ++count;
4843 }
4844 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004845 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 {
4847 if (count == 0)
4848 return OK;
4849 *num_file = count;
4850 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4851 if (*file == NULL)
4852 {
4853 *file = (char_u **)"";
4854 return FAIL;
4855 }
4856 count = 0;
4857 }
4858 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004859
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004860 /* Sort the results. Keep menu's in the specified order. */
4861 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004862 {
4863 if (xp->xp_context == EXPAND_EXPRESSION
4864 || xp->xp_context == EXPAND_FUNCTIONS
4865 || xp->xp_context == EXPAND_USER_FUNC)
4866 /* <SNR> functions should be sorted to the end. */
4867 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4868 sort_func_compare);
4869 else
4870 sort_strings(*file, *num_file);
4871 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004872
Bram Moolenaar4f688582007-07-24 12:34:30 +00004873#ifdef FEAT_CMDL_COMPL
4874 /* Reset the variables used for special highlight names expansion, so that
4875 * they don't show up when getting normal highlight names by ID. */
4876 reset_expand_highlight();
4877#endif
4878
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 return OK;
4880}
4881
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004882/*
4883 * Complete a shell command.
4884 * Returns FAIL or OK;
4885 */
4886 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004887expand_shellcmd(
4888 char_u *filepat, /* pattern to match with command names */
4889 int *num_file, /* return: number of matches */
4890 char_u ***file, /* return: array with matches */
4891 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004892{
4893 char_u *pat;
4894 int i;
4895 char_u *path;
4896 int mustfree = FALSE;
4897 garray_T ga;
4898 char_u *buf = alloc(MAXPATHL);
4899 size_t l;
4900 char_u *s, *e;
4901 int flags = flagsarg;
4902 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01004903 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004904
4905 if (buf == NULL)
4906 return FAIL;
4907
4908 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4909 * space */
4910 pat = vim_strsave(filepat);
4911 for (i = 0; pat[i]; ++i)
4912 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004913 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004914
Bram Moolenaarb5971142015-03-21 17:32:19 +01004915 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004916
4917 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004918 if (mch_isFullName(pat))
4919 path = (char_u *)" ";
4920 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004921 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4922 path = (char_u *)".";
4923 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004924 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004925 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004926 if (path == NULL)
4927 path = (char_u *)"";
4928 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004929
4930 /*
4931 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01004932 * collect them in "ga". When "." is not in $PATH also expand for the
4933 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004934 */
4935 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01004936 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004937 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01004938 if (*s == NUL)
4939 {
4940 if (did_curdir)
4941 break;
4942 /* Find directories in the current directory, path is empty. */
4943 did_curdir = TRUE;
4944 }
4945 else if (*s == '.')
4946 did_curdir = TRUE;
4947
Bram Moolenaar68c31742006-09-02 15:54:18 +00004948 if (*s == ' ')
4949 ++s; /* Skip space used for absolute path name. */
4950
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004951#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004952 e = vim_strchr(s, ';');
4953#else
4954 e = vim_strchr(s, ':');
4955#endif
4956 if (e == NULL)
4957 e = s + STRLEN(s);
4958
4959 l = e - s;
4960 if (l > MAXPATHL - 5)
4961 break;
4962 vim_strncpy(buf, s, l);
4963 add_pathsep(buf);
4964 l = STRLEN(buf);
4965 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4966
4967 /* Expand matches in one directory of $PATH. */
4968 ret = expand_wildcards(1, &buf, num_file, file, flags);
4969 if (ret == OK)
4970 {
4971 if (ga_grow(&ga, *num_file) == FAIL)
4972 FreeWild(*num_file, *file);
4973 else
4974 {
4975 for (i = 0; i < *num_file; ++i)
4976 {
4977 s = (*file)[i];
4978 if (STRLEN(s) > l)
4979 {
4980 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004981 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004982 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4983 }
4984 else
4985 vim_free(s);
4986 }
4987 vim_free(*file);
4988 }
4989 }
4990 if (*e != NUL)
4991 ++e;
4992 }
4993 *file = ga.ga_data;
4994 *num_file = ga.ga_len;
4995
4996 vim_free(buf);
4997 vim_free(pat);
4998 if (mustfree)
4999 vim_free(path);
5000 return OK;
5001}
5002
5003
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005005static 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 +00005006
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00005008 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005009 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005011 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005012call_user_expand_func(
5013 void *(*user_expand_func)(char_u *, int, char_u **, int),
5014 expand_T *xp,
5015 int *num_file,
5016 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005018 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005019 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005022 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005023 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
Bram Moolenaarb7515462013-06-29 12:58:33 +02005025 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005026 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 *num_file = 0;
5028 *file = NULL;
5029
Bram Moolenaarb7515462013-06-29 12:58:33 +02005030 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005031 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005032 keep = ccline.cmdbuff[ccline.cmdlen];
5033 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005034 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005035
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005036 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005037 args[1] = xp->xp_line;
5038 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 args[2] = num;
5040
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005041 /* Save the cmdline, we don't know what the function may do. */
5042 save_ccline = ccline;
5043 ccline.cmdbuff = NULL;
5044 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005046
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005047 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005048
5049 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005051 if (ccline.cmdbuff != NULL)
5052 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005053
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005054 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005055 return ret;
5056}
5057
5058/*
5059 * Expand names with a function defined by the user.
5060 */
5061 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005062ExpandUserDefined(
5063 expand_T *xp,
5064 regmatch_T *regmatch,
5065 int *num_file,
5066 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005067{
5068 char_u *retstr;
5069 char_u *s;
5070 char_u *e;
5071 char_u keep;
5072 garray_T ga;
5073
5074 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5075 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 return FAIL;
5077
5078 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005079 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
5081 e = vim_strchr(s, '\n');
5082 if (e == NULL)
5083 e = s + STRLEN(s);
5084 keep = *e;
5085 *e = 0;
5086
5087 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5088 {
5089 *e = keep;
5090 if (*e != NUL)
5091 ++e;
5092 continue;
5093 }
5094
5095 if (ga_grow(&ga, 1) == FAIL)
5096 break;
5097
5098 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5099 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
5101 *e = keep;
5102 if (*e != NUL)
5103 ++e;
5104 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005105 vim_free(retstr);
5106 *file = ga.ga_data;
5107 *num_file = ga.ga_len;
5108 return OK;
5109}
5110
5111/*
5112 * Expand names with a list returned by a function defined by the user.
5113 */
5114 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005115ExpandUserList(
5116 expand_T *xp,
5117 int *num_file,
5118 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005119{
5120 list_T *retlist;
5121 listitem_T *li;
5122 garray_T ga;
5123
5124 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5125 if (retlist == NULL)
5126 return FAIL;
5127
5128 ga_init2(&ga, (int)sizeof(char *), 3);
5129 /* Loop over the items in the list. */
5130 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5131 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005132 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5133 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005134
5135 if (ga_grow(&ga, 1) == FAIL)
5136 break;
5137
5138 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005139 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005140 ++ga.ga_len;
5141 }
5142 list_unref(retlist);
5143
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 *file = ga.ga_data;
5145 *num_file = ga.ga_len;
5146 return OK;
5147}
5148#endif
5149
5150/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005151 * Expand color scheme, compiler or filetype names.
5152 * Search from 'runtimepath':
5153 * 'runtimepath'/{dirnames}/{pat}.vim
5154 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5155 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5156 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5157 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005158 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 */
5160 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005161ExpandRTDir(
5162 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005163 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005164 int *num_file,
5165 char_u ***file,
5166 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 char_u *s;
5169 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005170 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005172 int i;
5173 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174
5175 *num_file = 0;
5176 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005177 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005178 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005180 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005182 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5183 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005185 ga_clear_strings(&ga);
5186 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005188 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005189 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005190 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005192
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005193 if (flags & DIP_START) {
5194 for (i = 0; dirnames[i] != NULL; ++i)
5195 {
5196 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5197 if (s == NULL)
5198 {
5199 ga_clear_strings(&ga);
5200 return FAIL;
5201 }
5202 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5203 globpath(p_pp, s, &ga, 0);
5204 vim_free(s);
5205 }
5206 }
5207
5208 if (flags & DIP_OPT) {
5209 for (i = 0; dirnames[i] != NULL; ++i)
5210 {
5211 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5212 if (s == NULL)
5213 {
5214 ga_clear_strings(&ga);
5215 return FAIL;
5216 }
5217 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5218 globpath(p_pp, s, &ga, 0);
5219 vim_free(s);
5220 }
5221 }
5222
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005223 for (i = 0; i < ga.ga_len; ++i)
5224 {
5225 match = ((char_u **)ga.ga_data)[i];
5226 s = match;
5227 e = s + STRLEN(s);
5228 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5229 {
5230 e -= 4;
5231 for (s = e; s > match; mb_ptr_back(match, s))
5232 if (s < match || vim_ispathsep(*s))
5233 break;
5234 ++s;
5235 *e = NUL;
5236 mch_memmove(match, s, e - s + 1);
5237 }
5238 }
5239
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005240 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005241 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005242
5243 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005244 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005245 remove_duplicates(&ga);
5246
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 *file = ga.ga_data;
5248 *num_file = ga.ga_len;
5249 return OK;
5250}
5251
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005252/*
5253 * Expand loadplugin names:
5254 * 'packpath'/pack/ * /opt/{pat}
5255 */
5256 static int
5257ExpandPackAddDir(
5258 char_u *pat,
5259 int *num_file,
5260 char_u ***file)
5261{
5262 char_u *s;
5263 char_u *e;
5264 char_u *match;
5265 garray_T ga;
5266 int i;
5267 int pat_len;
5268
5269 *num_file = 0;
5270 *file = NULL;
5271 pat_len = (int)STRLEN(pat);
5272 ga_init2(&ga, (int)sizeof(char *), 10);
5273
5274 s = alloc((unsigned)(pat_len + 26));
5275 if (s == NULL)
5276 {
5277 ga_clear_strings(&ga);
5278 return FAIL;
5279 }
5280 sprintf((char *)s, "pack/*/opt/%s*", pat);
5281 globpath(p_pp, s, &ga, 0);
5282 vim_free(s);
5283
5284 for (i = 0; i < ga.ga_len; ++i)
5285 {
5286 match = ((char_u **)ga.ga_data)[i];
5287 s = gettail(match);
5288 e = s + STRLEN(s);
5289 mch_memmove(match, s, e - s + 1);
5290 }
5291
5292 if (ga.ga_len == 0)
5293 return FAIL;
5294
5295 /* Sort and remove duplicates which can happen when specifying multiple
5296 * directories in dirnames. */
5297 remove_duplicates(&ga);
5298
5299 *file = ga.ga_data;
5300 *num_file = ga.ga_len;
5301 return OK;
5302}
5303
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304#endif
5305
5306#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5307/*
5308 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005309 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005311 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005312globpath(
5313 char_u *path,
5314 char_u *file,
5315 garray_T *ga,
5316 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 expand_T xpc;
5319 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 int num_p;
5322 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323
5324 buf = alloc(MAXPATHL);
5325 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005326 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005328 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 /* Loop over all entries in {path}. */
5332 while (*path != NUL)
5333 {
5334 /* Copy one item of the path to buf[] and concatenate the file name. */
5335 copy_option_part(&path, buf, MAXPATHL, ",");
5336 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5337 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005338# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005339 /* Using the platform's path separator (\) makes vim incorrectly
5340 * treat it as an escape character, use '/' instead. */
5341 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5342 STRCAT(buf, "/");
5343# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005347 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5348 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005350 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005352 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 for (i = 0; i < num_p; ++i)
5355 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005356 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005357 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005358 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005361
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 FreeWild(num_p, p);
5363 }
5364 }
5365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366
5367 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368}
5369
5370#endif
5371
5372#if defined(FEAT_CMDHIST) || defined(PROTO)
5373
5374/*********************************
5375 * Command line history stuff *
5376 *********************************/
5377
5378/*
5379 * Translate a history character to the associated type number.
5380 */
5381 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005382hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383{
5384 if (c == ':')
5385 return HIST_CMD;
5386 if (c == '=')
5387 return HIST_EXPR;
5388 if (c == '@')
5389 return HIST_INPUT;
5390 if (c == '>')
5391 return HIST_DEBUG;
5392 return HIST_SEARCH; /* must be '?' or '/' */
5393}
5394
5395/*
5396 * Table of history names.
5397 * These names are used in :history and various hist...() functions.
5398 * It is sufficient to give the significant prefix of a history name.
5399 */
5400
5401static char *(history_names[]) =
5402{
5403 "cmd",
5404 "search",
5405 "expr",
5406 "input",
5407 "debug",
5408 NULL
5409};
5410
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005411#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5412/*
5413 * Function given to ExpandGeneric() to obtain the possible first
5414 * arguments of the ":history command.
5415 */
5416 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005417get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005418{
5419 static char_u compl[2] = { NUL, NUL };
5420 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005421 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005422 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5423
5424 if (idx < short_names_count)
5425 {
5426 compl[0] = (char_u)short_names[idx];
5427 return compl;
5428 }
5429 if (idx < short_names_count + history_name_count)
5430 return (char_u *)history_names[idx - short_names_count];
5431 if (idx == short_names_count + history_name_count)
5432 return (char_u *)"all";
5433 return NULL;
5434}
5435#endif
5436
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437/*
5438 * init_history() - Initialize the command line history.
5439 * Also used to re-allocate the history when the size changes.
5440 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005441 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005442init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
5444 int newlen; /* new length of history table */
5445 histentry_T *temp;
5446 int i;
5447 int j;
5448 int type;
5449
5450 /*
5451 * If size of history table changed, reallocate it
5452 */
5453 newlen = (int)p_hi;
5454 if (newlen != hislen) /* history length changed */
5455 {
5456 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5457 {
5458 if (newlen)
5459 {
5460 temp = (histentry_T *)lalloc(
5461 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5462 if (temp == NULL) /* out of memory! */
5463 {
5464 if (type == 0) /* first one: just keep the old length */
5465 {
5466 newlen = hislen;
5467 break;
5468 }
5469 /* Already changed one table, now we can only have zero
5470 * length for all tables. */
5471 newlen = 0;
5472 type = -1;
5473 continue;
5474 }
5475 }
5476 else
5477 temp = NULL;
5478 if (newlen == 0 || temp != NULL)
5479 {
5480 if (hisidx[type] < 0) /* there are no entries yet */
5481 {
5482 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005483 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 }
5485 else if (newlen > hislen) /* array becomes bigger */
5486 {
5487 for (i = 0; i <= hisidx[type]; ++i)
5488 temp[i] = history[type][i];
5489 j = i;
5490 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005491 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 for ( ; j < hislen; ++i, ++j)
5493 temp[i] = history[type][j];
5494 }
5495 else /* array becomes smaller or 0 */
5496 {
5497 j = hisidx[type];
5498 for (i = newlen - 1; ; --i)
5499 {
5500 if (i >= 0) /* copy newest entries */
5501 temp[i] = history[type][j];
5502 else /* remove older entries */
5503 vim_free(history[type][j].hisstr);
5504 if (--j < 0)
5505 j = hislen - 1;
5506 if (j == hisidx[type])
5507 break;
5508 }
5509 hisidx[type] = newlen - 1;
5510 }
5511 vim_free(history[type]);
5512 history[type] = temp;
5513 }
5514 }
5515 hislen = newlen;
5516 }
5517}
5518
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005520clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005521{
5522 hisptr->hisnum = 0;
5523 hisptr->viminfo = FALSE;
5524 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005525 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005526}
5527
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528/*
5529 * Check if command line 'str' is already in history.
5530 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5531 */
5532 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533in_history(
5534 int type,
5535 char_u *str,
5536 int move_to_front, /* Move the entry to the front if it exists */
5537 int sep,
5538 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539{
5540 int i;
5541 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005542 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
5544 if (hisidx[type] < 0)
5545 return FALSE;
5546 i = hisidx[type];
5547 do
5548 {
5549 if (history[type][i].hisstr == NULL)
5550 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005551
5552 /* For search history, check that the separator character matches as
5553 * well. */
5554 p = history[type][i].hisstr;
5555 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005556 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005557 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 {
5559 if (!move_to_front)
5560 return TRUE;
5561 last_i = i;
5562 break;
5563 }
5564 if (--i < 0)
5565 i = hislen - 1;
5566 } while (i != hisidx[type]);
5567
5568 if (last_i >= 0)
5569 {
5570 str = history[type][i].hisstr;
5571 while (i != hisidx[type])
5572 {
5573 if (++i >= hislen)
5574 i = 0;
5575 history[type][last_i] = history[type][i];
5576 last_i = i;
5577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005579 history[type][i].viminfo = FALSE;
5580 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005581 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 return TRUE;
5583 }
5584 return FALSE;
5585}
5586
5587/*
5588 * Convert history name (from table above) to its HIST_ equivalent.
5589 * When "name" is empty, return "cmd" history.
5590 * Returns -1 for unknown history name.
5591 */
5592 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005593get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
5595 int i;
5596 int len = (int)STRLEN(name);
5597
5598 /* No argument: use current history. */
5599 if (len == 0)
5600 return hist_char2type(ccline.cmdfirstc);
5601
5602 for (i = 0; history_names[i] != NULL; ++i)
5603 if (STRNICMP(name, history_names[i], len) == 0)
5604 return i;
5605
5606 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5607 return hist_char2type(name[0]);
5608
5609 return -1;
5610}
5611
5612static int last_maptick = -1; /* last seen maptick */
5613
5614/*
5615 * Add the given string to the given history. If the string is already in the
5616 * history then it is moved to the front. "histype" may be one of he HIST_
5617 * values.
5618 */
5619 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005620add_to_history(
5621 int histype,
5622 char_u *new_entry,
5623 int in_map, /* consider maptick when inside a mapping */
5624 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 histentry_T *hisptr;
5627 int len;
5628
5629 if (hislen == 0) /* no history */
5630 return;
5631
Bram Moolenaara939e432013-11-09 05:30:26 +01005632 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5633 return;
5634
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 /*
5636 * Searches inside the same mapping overwrite each other, so that only
5637 * the last line is kept. Be careful not to remove a line that was moved
5638 * down, only lines that were added.
5639 */
5640 if (histype == HIST_SEARCH && in_map)
5641 {
5642 if (maptick == last_maptick)
5643 {
5644 /* Current line is from the same mapping, remove it */
5645 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5646 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005647 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 --hisnum[histype];
5649 if (--hisidx[HIST_SEARCH] < 0)
5650 hisidx[HIST_SEARCH] = hislen - 1;
5651 }
5652 last_maptick = -1;
5653 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005654 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
5656 if (++hisidx[histype] == hislen)
5657 hisidx[histype] = 0;
5658 hisptr = &history[histype][hisidx[histype]];
5659 vim_free(hisptr->hisstr);
5660
5661 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005662 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5664 if (hisptr->hisstr != NULL)
5665 hisptr->hisstr[len + 1] = sep;
5666
5667 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005668 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005669 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 if (histype == HIST_SEARCH && in_map)
5671 last_maptick = maptick;
5672 }
5673}
5674
5675#if defined(FEAT_EVAL) || defined(PROTO)
5676
5677/*
5678 * Get identifier of newest history entry.
5679 * "histype" may be one of the HIST_ values.
5680 */
5681 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005682get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5685 || hisidx[histype] < 0)
5686 return -1;
5687
5688 return history[histype][hisidx[histype]].hisnum;
5689}
5690
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 * Calculate history index from a number:
5693 * num > 0: seen as identifying number of a history entry
5694 * num < 0: relative position in history wrt newest entry
5695 * "histype" may be one of the HIST_ values.
5696 */
5697 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005698calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 int i;
5701 histentry_T *hist;
5702 int wrapped = FALSE;
5703
5704 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5705 || (i = hisidx[histype]) < 0 || num == 0)
5706 return -1;
5707
5708 hist = history[histype];
5709 if (num > 0)
5710 {
5711 while (hist[i].hisnum > num)
5712 if (--i < 0)
5713 {
5714 if (wrapped)
5715 break;
5716 i += hislen;
5717 wrapped = TRUE;
5718 }
5719 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5720 return i;
5721 }
5722 else if (-num <= hislen)
5723 {
5724 i += num + 1;
5725 if (i < 0)
5726 i += hislen;
5727 if (hist[i].hisstr != NULL)
5728 return i;
5729 }
5730 return -1;
5731}
5732
5733/*
5734 * Get a history entry by its index.
5735 * "histype" may be one of the HIST_ values.
5736 */
5737 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005738get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 idx = calc_hist_idx(histype, idx);
5741 if (idx >= 0)
5742 return history[histype][idx].hisstr;
5743 else
5744 return (char_u *)"";
5745}
5746
5747/*
5748 * Clear all entries of a history.
5749 * "histype" may be one of the HIST_ values.
5750 */
5751 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005752clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753{
5754 int i;
5755 histentry_T *hisptr;
5756
5757 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5758 {
5759 hisptr = history[histype];
5760 for (i = hislen; i--;)
5761 {
5762 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005763 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005764 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
5766 hisidx[histype] = -1; /* mark history as cleared */
5767 hisnum[histype] = 0; /* reset identifier counter */
5768 return OK;
5769 }
5770 return FAIL;
5771}
5772
5773/*
5774 * Remove all entries matching {str} from a history.
5775 * "histype" may be one of the HIST_ values.
5776 */
5777 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005778del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779{
5780 regmatch_T regmatch;
5781 histentry_T *hisptr;
5782 int idx;
5783 int i;
5784 int last;
5785 int found = FALSE;
5786
5787 regmatch.regprog = NULL;
5788 regmatch.rm_ic = FALSE; /* always match case */
5789 if (hislen != 0
5790 && histype >= 0
5791 && histype < HIST_COUNT
5792 && *str != NUL
5793 && (idx = hisidx[histype]) >= 0
5794 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5795 != NULL)
5796 {
5797 i = last = idx;
5798 do
5799 {
5800 hisptr = &history[histype][i];
5801 if (hisptr->hisstr == NULL)
5802 break;
5803 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5804 {
5805 found = TRUE;
5806 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005807 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
5809 else
5810 {
5811 if (i != last)
5812 {
5813 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005814 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
5816 if (--last < 0)
5817 last += hislen;
5818 }
5819 if (--i < 0)
5820 i += hislen;
5821 } while (i != idx);
5822 if (history[histype][idx].hisstr == NULL)
5823 hisidx[histype] = -1;
5824 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005825 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 return found;
5827}
5828
5829/*
5830 * Remove an indexed entry from a history.
5831 * "histype" may be one of the HIST_ values.
5832 */
5833 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005834del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 int i, j;
5837
5838 i = calc_hist_idx(histype, idx);
5839 if (i < 0)
5840 return FALSE;
5841 idx = hisidx[histype];
5842 vim_free(history[histype][i].hisstr);
5843
5844 /* When deleting the last added search string in a mapping, reset
5845 * last_maptick, so that the last added search string isn't deleted again.
5846 */
5847 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5848 last_maptick = -1;
5849
5850 while (i != idx)
5851 {
5852 j = (i + 1) % hislen;
5853 history[histype][i] = history[histype][j];
5854 i = j;
5855 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005856 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 if (--i < 0)
5858 i += hislen;
5859 hisidx[histype] = i;
5860 return TRUE;
5861}
5862
5863#endif /* FEAT_EVAL */
5864
5865#if defined(FEAT_CRYPT) || defined(PROTO)
5866/*
5867 * Very specific function to remove the value in ":set key=val" from the
5868 * history.
5869 */
5870 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005871remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872{
5873 char_u *p;
5874 int i;
5875
5876 i = hisidx[HIST_CMD];
5877 if (i < 0)
5878 return;
5879 p = history[HIST_CMD][i].hisstr;
5880 if (p != NULL)
5881 for ( ; *p; ++p)
5882 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5883 {
5884 p = vim_strchr(p + 3, '=');
5885 if (p == NULL)
5886 break;
5887 ++p;
5888 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5889 if (p[i] == '\\' && p[i + 1])
5890 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005891 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 --p;
5893 }
5894}
5895#endif
5896
5897#endif /* FEAT_CMDHIST */
5898
Bram Moolenaar064154c2016-03-19 22:50:43 +01005899#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01005900/*
5901 * Get pointer to the command line info to use. cmdline_paste() may clear
5902 * ccline and put the previous value in prev_ccline.
5903 */
5904 static struct cmdline_info *
5905get_ccline_ptr(void)
5906{
5907 if ((State & CMDLINE) == 0)
5908 return NULL;
5909 if (ccline.cmdbuff != NULL)
5910 return &ccline;
5911 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5912 return &prev_ccline;
5913 return NULL;
5914}
Bram Moolenaar064154c2016-03-19 22:50:43 +01005915#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01005916
Bram Moolenaar064154c2016-03-19 22:50:43 +01005917#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01005918/*
5919 * Get the current command line in allocated memory.
5920 * Only works when the command line is being edited.
5921 * Returns NULL when something is wrong.
5922 */
5923 char_u *
5924get_cmdline_str(void)
5925{
5926 struct cmdline_info *p = get_ccline_ptr();
5927
5928 if (p == NULL)
5929 return NULL;
5930 return vim_strnsave(p->cmdbuff, p->cmdlen);
5931}
5932
5933/*
5934 * Get the current command line position, counted in bytes.
5935 * Zero is the first position.
5936 * Only works when the command line is being edited.
5937 * Returns -1 when something is wrong.
5938 */
5939 int
5940get_cmdline_pos(void)
5941{
5942 struct cmdline_info *p = get_ccline_ptr();
5943
5944 if (p == NULL)
5945 return -1;
5946 return p->cmdpos;
5947}
5948
5949/*
5950 * Set the command line byte position to "pos". Zero is the first position.
5951 * Only works when the command line is being edited.
5952 * Returns 1 when failed, 0 when OK.
5953 */
5954 int
5955set_cmdline_pos(
5956 int pos)
5957{
5958 struct cmdline_info *p = get_ccline_ptr();
5959
5960 if (p == NULL)
5961 return 1;
5962
5963 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5964 * changed the command line. */
5965 if (pos < 0)
5966 new_cmdpos = 0;
5967 else
5968 new_cmdpos = pos;
5969 return 0;
5970}
5971#endif
5972
5973#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
5974/*
5975 * Get the current command-line type.
5976 * Returns ':' or '/' or '?' or '@' or '>' or '-'
5977 * Only works when the command line is being edited.
5978 * Returns NUL when something is wrong.
5979 */
5980 int
5981get_cmdline_type(void)
5982{
5983 struct cmdline_info *p = get_ccline_ptr();
5984
5985 if (p == NULL)
5986 return NUL;
5987 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01005988 return
5989# ifdef FEAT_EVAL
5990 (p->input_fn) ? '@' :
5991# endif
5992 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01005993 return p->cmdfirstc;
5994}
5995#endif
5996
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5998/*
5999 * Get indices "num1,num2" that specify a range within a list (not a range of
6000 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6001 * Returns OK if parsed successfully, otherwise FAIL.
6002 */
6003 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006004get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 int len;
6007 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006008 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
6010 *str = skipwhite(*str);
6011 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6012 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006013 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 *str += len;
6015 *num1 = (int)num;
6016 first = TRUE;
6017 }
6018 *str = skipwhite(*str);
6019 if (**str == ',') /* parse "to" part of range */
6020 {
6021 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006022 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 if (len > 0)
6024 {
6025 *num2 = (int)num;
6026 *str = skipwhite(*str + len);
6027 }
6028 else if (!first) /* no number given at all */
6029 return FAIL;
6030 }
6031 else if (first) /* only one number given */
6032 *num2 = *num1;
6033 return OK;
6034}
6035#endif
6036
6037#if defined(FEAT_CMDHIST) || defined(PROTO)
6038/*
6039 * :history command - print a history
6040 */
6041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006042ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 histentry_T *hist;
6045 int histype1 = HIST_CMD;
6046 int histype2 = HIST_CMD;
6047 int hisidx1 = 1;
6048 int hisidx2 = -1;
6049 int idx;
6050 int i, j, k;
6051 char_u *end;
6052 char_u *arg = eap->arg;
6053
6054 if (hislen == 0)
6055 {
6056 MSG(_("'history' option is zero"));
6057 return;
6058 }
6059
6060 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6061 {
6062 end = arg;
6063 while (ASCII_ISALPHA(*end)
6064 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6065 end++;
6066 i = *end;
6067 *end = NUL;
6068 histype1 = get_histtype(arg);
6069 if (histype1 == -1)
6070 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006071 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {
6073 histype1 = 0;
6074 histype2 = HIST_COUNT-1;
6075 }
6076 else
6077 {
6078 *end = i;
6079 EMSG(_(e_trailing));
6080 return;
6081 }
6082 }
6083 else
6084 histype2 = histype1;
6085 *end = i;
6086 }
6087 else
6088 end = arg;
6089 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6090 {
6091 EMSG(_(e_trailing));
6092 return;
6093 }
6094
6095 for (; !got_int && histype1 <= histype2; ++histype1)
6096 {
6097 STRCPY(IObuff, "\n # ");
6098 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6099 MSG_PUTS_TITLE(IObuff);
6100 idx = hisidx[histype1];
6101 hist = history[histype1];
6102 j = hisidx1;
6103 k = hisidx2;
6104 if (j < 0)
6105 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6106 if (k < 0)
6107 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6108 if (idx >= 0 && j <= k)
6109 for (i = idx + 1; !got_int; ++i)
6110 {
6111 if (i == hislen)
6112 i = 0;
6113 if (hist[i].hisstr != NULL
6114 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6115 {
6116 msg_putchar('\n');
6117 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6118 hist[i].hisnum);
6119 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6120 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006121 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 else
6123 STRCAT(IObuff, hist[i].hisstr);
6124 msg_outtrans(IObuff);
6125 out_flush();
6126 }
6127 if (i == idx)
6128 break;
6129 }
6130 }
6131}
6132#endif
6133
6134#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006135/*
6136 * Buffers for history read from a viminfo file. Only valid while reading.
6137 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006138static histentry_T *viminfo_history[HIST_COUNT] =
6139 {NULL, NULL, NULL, NULL, NULL};
6140static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6141static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142static int viminfo_add_at_front = FALSE;
6143
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006144static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145
6146/*
6147 * Translate a history type number to the associated character.
6148 */
6149 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006150hist_type2char(
6151 int type,
6152 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
6154 if (type == HIST_CMD)
6155 return ':';
6156 if (type == HIST_SEARCH)
6157 {
6158 if (use_question)
6159 return '?';
6160 else
6161 return '/';
6162 }
6163 if (type == HIST_EXPR)
6164 return '=';
6165 return '@';
6166}
6167
6168/*
6169 * Prepare for reading the history from the viminfo file.
6170 * This allocates history arrays to store the read history lines.
6171 */
6172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006173prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
6175 int i;
6176 int num;
6177 int type;
6178 int len;
6179
6180 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006181 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 if (asklen > hislen)
6183 asklen = hislen;
6184
6185 for (type = 0; type < HIST_COUNT; ++type)
6186 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006187 /* Count the number of empty spaces in the history list. Entries read
6188 * from viminfo previously are also considered empty. If there are
6189 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006191 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 num++;
6193 len = asklen;
6194 if (num > len)
6195 len = num;
6196 if (len <= 0)
6197 viminfo_history[type] = NULL;
6198 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006199 viminfo_history[type] = (histentry_T *)lalloc(
6200 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 if (viminfo_history[type] == NULL)
6202 len = 0;
6203 viminfo_hislen[type] = len;
6204 viminfo_hisidx[type] = 0;
6205 }
6206}
6207
6208/*
6209 * Accept a line from the viminfo, store it in the history array when it's
6210 * new.
6211 */
6212 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006213read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
6215 int type;
6216 long_u len;
6217 char_u *val;
6218 char_u *p;
6219
6220 type = hist_char2type(virp->vir_line[0]);
6221 if (viminfo_hisidx[type] < viminfo_hislen[type])
6222 {
6223 val = viminfo_readstring(virp, 1, TRUE);
6224 if (val != NULL && *val != NUL)
6225 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006226 int sep = (*val == ' ' ? NUL : *val);
6227
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006229 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
6231 /* Need to re-allocate to append the separator byte. */
6232 len = STRLEN(val);
6233 p = lalloc(len + 2, TRUE);
6234 if (p != NULL)
6235 {
6236 if (type == HIST_SEARCH)
6237 {
6238 /* Search entry: Move the separator from the first
6239 * column to after the NUL. */
6240 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006241 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 }
6243 else
6244 {
6245 /* Not a search entry: No separator in the viminfo
6246 * file, add a NUL separator. */
6247 mch_memmove(p, val, (size_t)len + 1);
6248 p[len + 1] = NUL;
6249 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006250 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6251 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006252 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6253 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006254 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 }
6256 }
6257 }
6258 vim_free(val);
6259 }
6260 return viminfo_readline(virp);
6261}
6262
Bram Moolenaar07219f92013-04-14 23:19:36 +02006263/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006264 * Accept a new style history line from the viminfo, store it in the history
6265 * array when it's new.
6266 */
6267 void
6268handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006269 garray_T *values,
6270 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006271{
6272 int type;
6273 long_u len;
6274 char_u *val;
6275 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006276 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006277
6278 /* Check the format:
6279 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006280 if (values->ga_len < 4
6281 || vp[0].bv_type != BVAL_NR
6282 || vp[1].bv_type != BVAL_NR
6283 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6284 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006285 return;
6286
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006287 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006288 if (type >= HIST_COUNT)
6289 return;
6290 if (viminfo_hisidx[type] < viminfo_hislen[type])
6291 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006292 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006293 if (val != NULL && *val != NUL)
6294 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006295 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6296 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006297 int idx;
6298 int overwrite = FALSE;
6299
6300 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6301 {
6302 /* If lines were written by an older Vim we need to avoid
6303 * getting duplicates. See if the entry already exists. */
6304 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6305 {
6306 p = viminfo_history[type][idx].hisstr;
6307 if (STRCMP(val, p) == 0
6308 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6309 {
6310 overwrite = TRUE;
6311 break;
6312 }
6313 }
6314
6315 if (!overwrite)
6316 {
6317 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006318 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006319 p = lalloc(len + 2, TRUE);
6320 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006321 else
6322 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006323 if (p != NULL)
6324 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006325 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006326 if (!overwrite)
6327 {
6328 mch_memmove(p, val, (size_t)len + 1);
6329 /* Put the separator after the NUL. */
6330 p[len + 1] = sep;
6331 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006332 viminfo_history[type][idx].hisnum = 0;
6333 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006334 viminfo_hisidx[type]++;
6335 }
6336 }
6337 }
6338 }
6339 }
6340}
6341
6342/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006343 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006344 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006345 static void
6346concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 int idx;
6349 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006350
6351 idx = hisidx[type] + viminfo_hisidx[type];
6352 if (idx >= hislen)
6353 idx -= hislen;
6354 else if (idx < 0)
6355 idx = hislen - 1;
6356 if (viminfo_add_at_front)
6357 hisidx[type] = idx;
6358 else
6359 {
6360 if (hisidx[type] == -1)
6361 hisidx[type] = hislen - 1;
6362 do
6363 {
6364 if (history[type][idx].hisstr != NULL
6365 || history[type][idx].viminfo)
6366 break;
6367 if (++idx == hislen)
6368 idx = 0;
6369 } while (idx != hisidx[type]);
6370 if (idx != hisidx[type] && --idx < 0)
6371 idx = hislen - 1;
6372 }
6373 for (i = 0; i < viminfo_hisidx[type]; i++)
6374 {
6375 vim_free(history[type][idx].hisstr);
6376 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6377 history[type][idx].viminfo = TRUE;
6378 history[type][idx].time_set = viminfo_history[type][i].time_set;
6379 if (--idx < 0)
6380 idx = hislen - 1;
6381 }
6382 idx += 1;
6383 idx %= hislen;
6384 for (i = 0; i < viminfo_hisidx[type]; i++)
6385 {
6386 history[type][idx++].hisnum = ++hisnum[type];
6387 idx %= hislen;
6388 }
6389}
6390
6391#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6392 static int
6393#ifdef __BORLANDC__
6394_RTLENTRYF
6395#endif
6396sort_hist(const void *s1, const void *s2)
6397{
6398 histentry_T *p1 = *(histentry_T **)s1;
6399 histentry_T *p2 = *(histentry_T **)s2;
6400
6401 if (p1->time_set < p2->time_set) return -1;
6402 if (p1->time_set > p2->time_set) return 1;
6403 return 0;
6404}
6405#endif
6406
6407/*
6408 * Merge history lines from viminfo and lines typed in this Vim based on the
6409 * timestamp;
6410 */
6411 static void
6412merge_history(int type)
6413{
6414 int max_len;
6415 histentry_T **tot_hist;
6416 histentry_T *new_hist;
6417 int i;
6418 int len;
6419
6420 /* Make one long list with all entries. */
6421 max_len = hislen + viminfo_hisidx[type];
6422 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6423 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6424 if (tot_hist == NULL || new_hist == NULL)
6425 {
6426 vim_free(tot_hist);
6427 vim_free(new_hist);
6428 return;
6429 }
6430 for (i = 0; i < viminfo_hisidx[type]; i++)
6431 tot_hist[i] = &viminfo_history[type][i];
6432 len = i;
6433 for (i = 0; i < hislen; i++)
6434 if (history[type][i].hisstr != NULL)
6435 tot_hist[len++] = &history[type][i];
6436
6437 /* Sort the list on timestamp. */
6438 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6439
6440 /* Keep the newest ones. */
6441 for (i = 0; i < hislen; i++)
6442 {
6443 if (i < len)
6444 {
6445 new_hist[i] = *tot_hist[i];
6446 tot_hist[i]->hisstr = NULL;
6447 if (new_hist[i].hisnum == 0)
6448 new_hist[i].hisnum = ++hisnum[type];
6449 }
6450 else
6451 clear_hist_entry(&new_hist[i]);
6452 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006453 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006454
6455 /* Free what is not kept. */
6456 for (i = 0; i < viminfo_hisidx[type]; i++)
6457 vim_free(viminfo_history[type][i].hisstr);
6458 for (i = 0; i < hislen; i++)
6459 vim_free(history[type][i].hisstr);
6460 vim_free(history[type]);
6461 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006462 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006463}
6464
6465/*
6466 * Finish reading history lines from viminfo. Not used when writing viminfo.
6467 */
6468 void
6469finish_viminfo_history(vir_T *virp)
6470{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006472 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
6474 for (type = 0; type < HIST_COUNT; ++type)
6475 {
6476 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006477 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006478
6479 if (merge)
6480 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006482 concat_history(type);
6483
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 vim_free(viminfo_history[type]);
6485 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006486 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 }
6488}
6489
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006490/*
6491 * Write history to viminfo file in "fp".
6492 * When "merge" is TRUE merge history lines with a previously read viminfo
6493 * file, data is in viminfo_history[].
6494 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006497write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 int i;
6500 int type;
6501 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006502 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
6504 init_history();
6505 if (hislen == 0)
6506 return;
6507 for (type = 0; type < HIST_COUNT; ++type)
6508 {
6509 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6510 if (num_saved == 0)
6511 continue;
6512 if (num_saved < 0) /* Use default */
6513 num_saved = hislen;
6514 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6515 type == HIST_CMD ? _("Command Line") :
6516 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006517 type == HIST_EXPR ? _("Expression") :
6518 type == HIST_INPUT ? _("Input Line") :
6519 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 if (num_saved > hislen)
6521 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006522
6523 /*
6524 * Merge typed and viminfo history:
6525 * round 1: history of typed commands.
6526 * round 2: history from recently read viminfo.
6527 */
6528 for (round = 1; round <= 2; ++round)
6529 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006530 if (round == 1)
6531 /* start at newest entry, somewhere in the list */
6532 i = hisidx[type];
6533 else if (viminfo_hisidx[type] > 0)
6534 /* start at newest entry, first in the list */
6535 i = 0;
6536 else
6537 /* empty list */
6538 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006539 if (i >= 0)
6540 while (num_saved > 0
6541 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006543 char_u *p;
6544 time_t timestamp;
6545 int c = NUL;
6546
6547 if (round == 1)
6548 {
6549 p = history[type][i].hisstr;
6550 timestamp = history[type][i].time_set;
6551 }
6552 else
6553 {
6554 p = viminfo_history[type] == NULL ? NULL
6555 : viminfo_history[type][i].hisstr;
6556 timestamp = viminfo_history[type] == NULL ? 0
6557 : viminfo_history[type][i].time_set;
6558 }
6559
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006560 if (p != NULL && (round == 2
6561 || !merge
6562 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006564 --num_saved;
6565 fputc(hist_type2char(type, TRUE), fp);
6566 /* For the search history: put the separator in the
6567 * second column; use a space if there isn't one. */
6568 if (type == HIST_SEARCH)
6569 {
6570 c = p[STRLEN(p) + 1];
6571 putc(c == NUL ? ' ' : c, fp);
6572 }
6573 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006574
6575 {
6576 char cbuf[NUMBUFLEN];
6577
6578 /* New style history with a bar line. Format:
6579 * |{bartype},{histtype},{timestamp},{separator},"text" */
6580 if (c == NUL)
6581 cbuf[0] = NUL;
6582 else
6583 sprintf(cbuf, "%d", c);
6584 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6585 type, (long)timestamp, cbuf);
6586 barline_writestring(fp, p, LSIZE - 20);
6587 putc('\n', fp);
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006590 if (round == 1)
6591 {
6592 /* Decrement index, loop around and stop when back at
6593 * the start. */
6594 if (--i < 0)
6595 i = hislen - 1;
6596 if (i == hisidx[type])
6597 break;
6598 }
6599 else
6600 {
6601 /* Increment index. Stop at the end in the while. */
6602 ++i;
6603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006605 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006606 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006607 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006608 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006609 vim_free(viminfo_history[type]);
6610 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006611 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 }
6613}
6614#endif /* FEAT_VIMINFO */
6615
6616#if defined(FEAT_FKMAP) || defined(PROTO)
6617/*
6618 * Write a character at the current cursor+offset position.
6619 * It is directly written into the command buffer block.
6620 */
6621 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006622cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6625 {
6626 EMSG(_("E198: cmd_pchar beyond the command length"));
6627 return;
6628 }
6629 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6630 ccline.cmdbuff[ccline.cmdlen] = NUL;
6631}
6632
6633 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006634cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6637 {
6638 /* EMSG(_("cmd_gchar beyond the command length")); */
6639 return NUL;
6640 }
6641 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6642}
6643#endif
6644
6645#if defined(FEAT_CMDWIN) || defined(PROTO)
6646/*
6647 * Open a window on the current command line and history. Allow editing in
6648 * the window. Returns when the window is closed.
6649 * Returns:
6650 * CR if the command is to be executed
6651 * Ctrl_C if it is to be abandoned
6652 * K_IGNORE if editing continues
6653 */
6654 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006655ex_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006658 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006660 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 win_T *wp;
6662 int i;
6663 linenr_T lnum;
6664 int histtype;
6665 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006666#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 int save_restart_edit = restart_edit;
6670 int save_State = State;
6671 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006672#ifdef FEAT_RIGHTLEFT
6673 int save_cmdmsg_rl = cmdmsg_rl;
6674#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006675#ifdef FEAT_FOLDING
6676 int save_KeyTyped;
6677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678
6679 /* Can't do this recursively. Can't do it when typing a password. */
6680 if (cmdwin_type != 0
6681# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6682 || cmdline_star > 0
6683# endif
6684 )
6685 {
6686 beep_flush();
6687 return K_IGNORE;
6688 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006689 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
6691 /* Save current window sizes. */
6692 win_size_save(&winsizes);
6693
6694# ifdef FEAT_AUTOCMD
6695 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006696 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006698 /* don't use a new tab page */
6699 cmdmod.tab = 0;
6700
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 /* Create a window for the command-line buffer. */
6702 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6703 {
6704 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006705# ifdef FEAT_AUTOCMD
6706 unblock_autocmds();
6707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 return K_IGNORE;
6709 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006710 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
6712 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006713 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006714 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6716 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6717 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006718#ifdef FEAT_FOLDING
6719 curwin->w_p_fen = FALSE;
6720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006722 curwin->w_p_rl = cmdmsg_rl;
6723 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006725 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726
6727# ifdef FEAT_AUTOCMD
6728 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006729 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730# endif
6731
Bram Moolenaar46152342005-09-07 21:18:43 +00006732 /* Showing the prompt may have set need_wait_return, reset it. */
6733 need_wait_return = FALSE;
6734
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006735 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6737 {
6738 if (p_wc == TAB)
6739 {
6740 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6741 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6742 }
6743 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6744 }
6745
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006746 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6747 * sets 'textwidth' to 78). */
6748 curbuf->b_p_tw = 0;
6749
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 /* Fill the buffer with the history. */
6751 init_history();
6752 if (hislen > 0)
6753 {
6754 i = hisidx[histtype];
6755 if (i >= 0)
6756 {
6757 lnum = 0;
6758 do
6759 {
6760 if (++i == hislen)
6761 i = 0;
6762 if (history[histtype][i].hisstr != NULL)
6763 ml_append(lnum++, history[histtype][i].hisstr,
6764 (colnr_T)0, FALSE);
6765 }
6766 while (i != hisidx[histtype]);
6767 }
6768 }
6769
6770 /* Replace the empty last line with the current command-line and put the
6771 * cursor there. */
6772 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6773 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6774 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006775 changed_line_abv_curs();
6776 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006777 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
6779 /* Save the command line info, can be used recursively. */
6780 save_ccline = ccline;
6781 ccline.cmdbuff = NULL;
6782 ccline.cmdprompt = NULL;
6783
6784 /* No Ex mode here! */
6785 exmode_active = 0;
6786
6787 State = NORMAL;
6788# ifdef FEAT_MOUSE
6789 setmouse();
6790# endif
6791
6792# ifdef FEAT_AUTOCMD
6793 /* Trigger CmdwinEnter autocommands. */
6794 typestr[0] = cmdwin_type;
6795 typestr[1] = NUL;
6796 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006797 if (restart_edit != 0) /* autocmd with ":startinsert" */
6798 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799# endif
6800
6801 i = RedrawingDisabled;
6802 RedrawingDisabled = 0;
6803
6804 /*
6805 * Call the main loop until <CR> or CTRL-C is typed.
6806 */
6807 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006808 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
6810 RedrawingDisabled = i;
6811
6812# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006813
6814# ifdef FEAT_FOLDING
6815 save_KeyTyped = KeyTyped;
6816# endif
6817
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 /* Trigger CmdwinLeave autocommands. */
6819 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006820
6821# ifdef FEAT_FOLDING
6822 /* Restore KeyTyped in case it is modified by autocommands */
6823 KeyTyped = save_KeyTyped;
6824# endif
6825
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826# endif
6827
Bram Moolenaarccc18222007-05-10 18:25:20 +00006828 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 ccline = save_ccline;
6830 cmdwin_type = 0;
6831
6832 exmode_active = save_exmode;
6833
Bram Moolenaarf9821062008-06-20 16:31:07 +00006834 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006836 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 {
6838 cmdwin_result = Ctrl_C;
6839 EMSG(_("E199: Active window or buffer deleted"));
6840 }
6841 else
6842 {
6843# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6844 /* autocmds may abort script processing */
6845 if (aborting() && cmdwin_result != K_IGNORE)
6846 cmdwin_result = Ctrl_C;
6847# endif
6848 /* Set the new command line from the cmdline buffer. */
6849 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006850 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006852 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6853
6854 if (histtype == HIST_CMD)
6855 {
6856 /* Execute the command directly. */
6857 ccline.cmdbuff = vim_strsave((char_u *)p);
6858 cmdwin_result = CAR;
6859 }
6860 else
6861 {
6862 /* First need to cancel what we were doing. */
6863 ccline.cmdbuff = NULL;
6864 stuffcharReadbuff(':');
6865 stuffReadbuff((char_u *)p);
6866 stuffcharReadbuff(CAR);
6867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 }
6869 else if (cmdwin_result == K_XF2) /* :qa typed */
6870 {
6871 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6872 cmdwin_result = CAR;
6873 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006874 else if (cmdwin_result == Ctrl_C)
6875 {
6876 /* :q or :close, don't execute any command
6877 * and don't modify the cmd window. */
6878 ccline.cmdbuff = NULL;
6879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 else
6881 ccline.cmdbuff = vim_strsave(ml_get_curline());
6882 if (ccline.cmdbuff == NULL)
6883 cmdwin_result = Ctrl_C;
6884 else
6885 {
6886 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6887 ccline.cmdbufflen = ccline.cmdlen + 1;
6888 ccline.cmdpos = curwin->w_cursor.col;
6889 if (ccline.cmdpos > ccline.cmdlen)
6890 ccline.cmdpos = ccline.cmdlen;
6891 if (cmdwin_result == K_IGNORE)
6892 {
6893 set_cmdspos_cursor();
6894 redrawcmd();
6895 }
6896 }
6897
6898# ifdef FEAT_AUTOCMD
6899 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006900 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02006902# ifdef FEAT_CONCEAL
6903 /* Avoid command-line window first character being concealed. */
6904 curwin->w_p_cole = 0;
6905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006907 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 win_goto(old_curwin);
6909 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006910
6911 /* win_close() may have already wiped the buffer when 'bh' is
6912 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006913 if (bufref_valid(&bufref))
6914 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915
6916 /* Restore window sizes. */
6917 win_size_restore(&winsizes);
6918
6919# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006920 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921# endif
6922 }
6923
6924 ga_clear(&winsizes);
6925 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006926# ifdef FEAT_RIGHTLEFT
6927 cmdmsg_rl = save_cmdmsg_rl;
6928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929
6930 State = save_State;
6931# ifdef FEAT_MOUSE
6932 setmouse();
6933# endif
6934
6935 return cmdwin_result;
6936}
6937#endif /* FEAT_CMDWIN */
6938
6939/*
6940 * Used for commands that either take a simple command string argument, or:
6941 * cmd << endmarker
6942 * {script}
6943 * endmarker
6944 * Returns a pointer to allocated memory with {script} or NULL.
6945 */
6946 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006947script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948{
6949 char_u *theline;
6950 char *end_pattern = NULL;
6951 char dot[] = ".";
6952 garray_T ga;
6953
6954 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6955 return NULL;
6956
6957 ga_init2(&ga, 1, 0x400);
6958
6959 if (cmd[2] != NUL)
6960 end_pattern = (char *)skipwhite(cmd + 2);
6961 else
6962 end_pattern = dot;
6963
6964 for (;;)
6965 {
6966 theline = eap->getline(
6967#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006968 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#endif
6970 NUL, eap->cookie, 0);
6971
6972 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006973 {
6974 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
6978 ga_concat(&ga, theline);
6979 ga_append(&ga, '\n');
6980 vim_free(theline);
6981 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006982 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
6984 return (char_u *)ga.ga_data;
6985}