blob: 162e55590a549636e410c53836f301ce9fed830c [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020028 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
55#ifdef FEAT_CMDHIST
56typedef struct hist_entry
57{
58 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020059 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020061 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000062} histentry_T;
63
64static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
65static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
66static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
67 /* identifying (unique) number of newest history entry */
68static int hislen = 0; /* actual length of history tables */
69
Bram Moolenaard25c16e2016-01-29 22:13:30 +010070static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
Bram Moolenaard25c16e2016-01-29 22:13:30 +010072static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075# endif
76#endif
77
78#ifdef FEAT_RIGHTLEFT
79static int cmd_hkmap = 0; /* Hebrew mapping during command line */
80#endif
81
82#ifdef FEAT_FKMAP
83static int cmd_fkmap = 0; /* Farsi mapping during command line */
84#endif
85
Bram Moolenaard25c16e2016-01-29 22:13:30 +010086static int cmdline_charsize(int idx);
87static void set_cmdspos(void);
88static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void alloc_cmdbuff(int len);
93static int realloc_cmdbuff(int len);
94static void draw_cmdline(int start, int len);
95static void save_cmdline(struct cmdline_info *ccp);
96static void restore_cmdline(struct cmdline_info *ccp);
97static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000098#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010099static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
101#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100102static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100104static void redrawcmdprompt(void);
105static void cursorcmd(void);
106static int ccheck_abbr(int);
107static int nextwild(expand_T *xp, int type, int options, int escape);
108static void escape_fname(char_u **pp);
109static int showmatches(expand_T *xp, int wildmenu);
110static void set_expand_context(expand_T *xp);
111static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
112static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100114static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100115static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100116static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200117# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100121static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
122static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123# endif
124#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200125#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_CMDWIN
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static int ex_window(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200133#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
134static int
135#ifdef __BORLANDC__
136_RTLENTRYF
137#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100138sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200139#endif
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200140#ifdef FEAT_SEARCH_EXTRA
141static void set_search_match(pos_T *t);
142#endif
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144/*
145 * getcmdline() - accept a command line starting with firstc.
146 *
147 * firstc == ':' get ":" command line.
148 * firstc == '/' or '?' get search pattern
149 * firstc == '=' get expression
150 * firstc == '@' get text for input() function
151 * firstc == '>' get text for debug mode
152 * firstc == NUL get text for :insert command
153 * firstc == -1 like NUL, and break on CTRL-C
154 *
155 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
156 * command line.
157 *
158 * Careful: getcmdline() can be called recursively!
159 *
160 * Return pointer to allocated string if there is a commandline, NULL
161 * otherwise.
162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100164getcmdline(
165 int firstc,
166 long count UNUSED, /* only used for incremental search */
167 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168{
169 int c;
170 int i;
171 int j;
172 int gotesc = FALSE; /* TRUE when <ESC> just typed */
173 int do_abbr; /* when TRUE check for abbr. */
174#ifdef FEAT_CMDHIST
175 char_u *lookfor = NULL; /* string to match */
176 int hiscnt; /* current history line in use */
177 int histype; /* history type to be used */
178#endif
179#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200180 pos_T search_start; /* where 'incsearch' starts searching */
181 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 colnr_T old_curswant;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200183 colnr_T init_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 colnr_T old_leftcol;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200185 colnr_T init_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 linenr_T old_topline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200187 linenr_T init_topline = curwin->w_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200188 pos_T match_start = curwin->w_cursor;
189 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190# ifdef FEAT_DIFF
191 int old_topfill;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200192 int init_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193# endif
194 linenr_T old_botline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200195 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 int did_incsearch = FALSE;
197 int incsearch_postponed = FALSE;
198#endif
199 int did_wild_list = FALSE; /* did wild_list() recently */
200 int wim_index = 0; /* index in wim_flags[] */
201 int res;
202 int save_msg_scroll = msg_scroll;
203 int save_State = State; /* remember State when called */
204 int some_key_typed = FALSE; /* one of the keys was typed */
205#ifdef FEAT_MOUSE
206 /* mouse drag and release events are ignored, unless they are
207 * preceded with a mouse down event */
208 int ignore_drag_release = TRUE;
209#endif
210#ifdef FEAT_EVAL
211 int break_ctrl_c = FALSE;
212#endif
213 expand_T xpc;
214 long *b_im_ptr = NULL;
Bram Moolenaar4d050402017-01-09 12:58:11 +0100215#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) \
216 || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_CMDWIN)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000217 /* Everything that may work recursively should save and restore the
218 * current command line in save_ccline. That includes update_screen(), a
219 * custom status line may invoke ":normal". */
220 struct cmdline_info save_ccline;
221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#ifdef FEAT_EVAL
224 if (firstc == -1)
225 {
226 firstc = NUL;
227 break_ctrl_c = TRUE;
228 }
229#endif
230#ifdef FEAT_RIGHTLEFT
231 /* start without Hebrew mapping for a command line */
232 if (firstc == ':' || firstc == '=' || firstc == '>')
233 cmd_hkmap = 0;
234#endif
235
236 ccline.overstrike = FALSE; /* always start in insert mode */
237#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200238 clearpos(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200239 save_cursor = curwin->w_cursor; /* may be restored later */
240 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 old_curswant = curwin->w_curswant;
242 old_leftcol = curwin->w_leftcol;
243 old_topline = curwin->w_topline;
244# ifdef FEAT_DIFF
245 old_topfill = curwin->w_topfill;
246# endif
247 old_botline = curwin->w_botline;
248#endif
249
250 /*
251 * set some variables for redrawcmd()
252 */
253 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000254 ccline.cmdindent = (firstc > 0 ? indent : 0);
255
256 /* alloc initial ccline.cmdbuff */
257 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 if (ccline.cmdbuff == NULL)
259 return NULL; /* out of memory */
260 ccline.cmdlen = ccline.cmdpos = 0;
261 ccline.cmdbuff[0] = NUL;
262
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000263 /* autoindent for :insert and :append */
264 if (firstc <= 0)
265 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200266 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000267 ccline.cmdbuff[indent] = NUL;
268 ccline.cmdpos = indent;
269 ccline.cmdspos = indent;
270 ccline.cmdlen = indent;
271 }
272
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000274 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275
276#ifdef FEAT_RIGHTLEFT
277 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
278 && (firstc == '/' || firstc == '?'))
279 cmdmsg_rl = TRUE;
280 else
281 cmdmsg_rl = FALSE;
282#endif
283
284 redir_off = TRUE; /* don't redirect the typed command */
285 if (!cmd_silent)
286 {
287 i = msg_scrolled;
288 msg_scrolled = 0; /* avoid wait_return message */
289 gotocmdline(TRUE);
290 msg_scrolled += i;
291 redrawcmdprompt(); /* draw prompt or indent */
292 set_cmdspos();
293 }
294 xpc.xp_context = EXPAND_NOTHING;
295 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000296#ifndef BACKSLASH_IN_FILENAME
297 xpc.xp_shell = FALSE;
298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000300#if defined(FEAT_EVAL)
301 if (ccline.input_fn)
302 {
303 xpc.xp_context = ccline.xp_context;
304 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000305# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000306 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000307# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000308 }
309#endif
310
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 /*
312 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
313 * doing ":@0" when register 0 doesn't contain a CR.
314 */
315 msg_scroll = FALSE;
316
317 State = CMDLINE;
318
319 if (firstc == '/' || firstc == '?' || firstc == '@')
320 {
321 /* Use ":lmap" mappings for search pattern and input(). */
322 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
323 b_im_ptr = &curbuf->b_p_iminsert;
324 else
325 b_im_ptr = &curbuf->b_p_imsearch;
326 if (*b_im_ptr == B_IMODE_LMAP)
327 State |= LANGMAP;
328#ifdef USE_IM_CONTROL
329 im_set_active(*b_im_ptr == B_IMODE_IM);
330#endif
331 }
332#ifdef USE_IM_CONTROL
333 else if (p_imcmdline)
334 im_set_active(TRUE);
335#endif
336
337#ifdef FEAT_MOUSE
338 setmouse();
339#endif
340#ifdef CURSOR_SHAPE
341 ui_cursor_shape(); /* may show different cursor shape */
342#endif
343
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000344 /* When inside an autocommand for writing "exiting" may be set and
345 * terminal mode set to cooked. Need to set raw mode here then. */
346 settmode(TMODE_RAW);
347
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#ifdef FEAT_CMDHIST
349 init_history();
350 hiscnt = hislen; /* set hiscnt to impossible history value */
351 histype = hist_char2type(firstc);
352#endif
353
354#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000355 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#endif
357
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200358 /* If something above caused an error, reset the flags, we do want to type
359 * and execute commands. Display may be messed up a bit. */
360 if (did_emsg)
361 redrawcmd();
362 did_emsg = FALSE;
363 got_int = FALSE;
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 /*
366 * Collect the command string, handling editing keys.
367 */
368 for (;;)
369 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000370 redir_off = TRUE; /* Don't redirect the typed command.
371 Repeated, because a ":redir" inside
372 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#ifdef USE_ON_FLY_SCROLL
374 dont_scroll = FALSE; /* allow scrolling here */
375#endif
376 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
377
378 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000379
380 /* Get a character. Ignore K_IGNORE, it should not do anything, such
381 * as stop completion. */
382 do
383 {
384 c = safe_vgetc();
385 } while (c == K_IGNORE);
386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 if (KeyTyped)
388 {
389 some_key_typed = TRUE;
390#ifdef FEAT_RIGHTLEFT
391 if (cmd_hkmap)
392 c = hkmap(c);
393# ifdef FEAT_FKMAP
394 if (cmd_fkmap)
395 c = cmdl_fkmap(c);
396# endif
397 if (cmdmsg_rl && !KeyStuffed)
398 {
399 /* Invert horizontal movements and operations. Only when
400 * typed by the user directly, not when the result of a
401 * mapping. */
402 switch (c)
403 {
404 case K_RIGHT: c = K_LEFT; break;
405 case K_S_RIGHT: c = K_S_LEFT; break;
406 case K_C_RIGHT: c = K_C_LEFT; break;
407 case K_LEFT: c = K_RIGHT; break;
408 case K_S_LEFT: c = K_S_RIGHT; break;
409 case K_C_LEFT: c = K_C_RIGHT; break;
410 }
411 }
412#endif
413 }
414
415 /*
416 * Ignore got_int when CTRL-C was typed here.
417 * Don't ignore it in :global, we really need to break then, e.g., for
418 * ":g/pat/normal /pat" (without the <CR>).
419 * Don't ignore it for the input() function.
420 */
421 if ((c == Ctrl_C
422#ifdef UNIX
423 || c == intr_char
424#endif
425 )
426#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
427 && firstc != '@'
428#endif
429#ifdef FEAT_EVAL
430 && !break_ctrl_c
431#endif
432 && !global_busy)
433 got_int = FALSE;
434
435#ifdef FEAT_CMDHIST
436 /* free old command line when finished moving around in the history
437 * list */
438 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000439 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000440 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 && c != K_PAGEDOWN && c != K_PAGEUP
442 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000443 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
445 {
446 vim_free(lookfor);
447 lookfor = NULL;
448 }
449#endif
450
451 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000452 * When there are matching completions to select <S-Tab> works like
453 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000455 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 c = Ctrl_P;
457
458#ifdef FEAT_WILDMENU
459 /* Special translations for 'wildmenu' */
460 if (did_wild_list && p_wmnu)
461 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000462 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000464 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 c = Ctrl_N;
466 }
467 /* Hitting CR after "emenu Name.": complete submenu */
468 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
469 && ccline.cmdpos > 1
470 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
471 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
472 && (c == '\n' || c == '\r' || c == K_KENTER))
473 c = K_DOWN;
474#endif
475
476 /* free expanded names when finished walking through matches */
477 if (xpc.xp_numfiles != -1
478 && !(c == p_wc && KeyTyped) && c != p_wcm
479 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
480 && c != Ctrl_L)
481 {
482 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
483 did_wild_list = FALSE;
484#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000485 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486#endif
487 xpc.xp_context = EXPAND_NOTHING;
488 wim_index = 0;
489#ifdef FEAT_WILDMENU
490 if (p_wmnu && wild_menu_showing != 0)
491 {
492 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000493 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000494
495 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000496 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
498 if (wild_menu_showing == WM_SCROLLED)
499 {
500 /* Entered command line, move it up */
501 cmdline_row--;
502 redrawcmd();
503 }
504 else if (save_p_ls != -1)
505 {
506 /* restore 'laststatus' and 'winminheight' */
507 p_ls = save_p_ls;
508 p_wmh = save_p_wmh;
509 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000510 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000512 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 redrawcmd();
514 save_p_ls = -1;
515 }
516 else
517 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 redraw_statuslines();
520 }
521 KeyTyped = skt;
522 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000523 if (ccline.input_fn)
524 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 }
526#endif
527 }
528
529#ifdef FEAT_WILDMENU
530 /* Special translations for 'wildmenu' */
531 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
532 {
533 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000534 if (c == K_DOWN && ccline.cmdpos > 0
535 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000537 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 {
539 /* Hitting <Up>: Remove one submenu name in front of the
540 * cursor */
541 int found = FALSE;
542
543 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
544 i = 0;
545 while (--j > 0)
546 {
547 /* check for start of menu name */
548 if (ccline.cmdbuff[j] == ' '
549 && ccline.cmdbuff[j - 1] != '\\')
550 {
551 i = j + 1;
552 break;
553 }
554 /* check for start of submenu name */
555 if (ccline.cmdbuff[j] == '.'
556 && ccline.cmdbuff[j - 1] != '\\')
557 {
558 if (found)
559 {
560 i = j + 1;
561 break;
562 }
563 else
564 found = TRUE;
565 }
566 }
567 if (i > 0)
568 cmdline_del(i);
569 c = p_wc;
570 xpc.xp_context = EXPAND_NOTHING;
571 }
572 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000573 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000574 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000575 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {
577 char_u upseg[5];
578
579 upseg[0] = PATHSEP;
580 upseg[1] = '.';
581 upseg[2] = '.';
582 upseg[3] = PATHSEP;
583 upseg[4] = NUL;
584
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000585 if (c == K_DOWN
586 && ccline.cmdpos > 0
587 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
588 && (ccline.cmdpos < 3
589 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
591 {
592 /* go down a directory */
593 c = p_wc;
594 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000595 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {
597 /* If in a direct ancestor, strip off one ../ to go down */
598 int found = FALSE;
599
600 j = ccline.cmdpos;
601 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
602 while (--j > i)
603 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000604#ifdef FEAT_MBYTE
605 if (has_mbyte)
606 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 if (vim_ispathsep(ccline.cmdbuff[j]))
609 {
610 found = TRUE;
611 break;
612 }
613 }
614 if (found
615 && ccline.cmdbuff[j - 1] == '.'
616 && ccline.cmdbuff[j - 2] == '.'
617 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
618 {
619 cmdline_del(j - 2);
620 c = p_wc;
621 }
622 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000623 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 {
625 /* go up a directory */
626 int found = FALSE;
627
628 j = ccline.cmdpos - 1;
629 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
630 while (--j > i)
631 {
632#ifdef FEAT_MBYTE
633 if (has_mbyte)
634 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
635#endif
636 if (vim_ispathsep(ccline.cmdbuff[j])
637#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100638 && vim_strchr((char_u *)" *?[{`$%#",
639 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640#endif
641 )
642 {
643 if (found)
644 {
645 i = j + 1;
646 break;
647 }
648 else
649 found = TRUE;
650 }
651 }
652
653 if (!found)
654 j = i;
655 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
656 j += 4;
657 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
658 && j == i)
659 j += 3;
660 else
661 j = 0;
662 if (j > 0)
663 {
664 /* TODO this is only for DOS/UNIX systems - need to put in
665 * machine-specific stuff here and in upseg init */
666 cmdline_del(j);
667 put_on_cmdline(upseg + 1, 3, FALSE);
668 }
669 else if (ccline.cmdpos > i)
670 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100671
672 /* Now complete in the new directory. Set KeyTyped in case the
673 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100675 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678
679#endif /* FEAT_WILDMENU */
680
681 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
682 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
683 if (c == Ctrl_BSL)
684 {
685 ++no_mapping;
686 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000687 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 --no_mapping;
689 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200690 /* CTRL-\ e doesn't work when obtaining an expression, unless it
691 * is in a mapping. */
692 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
693 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
695 vungetc(c);
696 c = Ctrl_BSL;
697 }
698#ifdef FEAT_EVAL
699 else if (c == 'e')
700 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200701 char_u *p = NULL;
702 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703
704 /*
705 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000706 * Need to save and restore the current command line, to be
707 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 */
709 if (ccline.cmdpos == ccline.cmdlen)
710 new_cmdpos = 99999; /* keep it at the end */
711 else
712 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000713
714 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000716 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (c == '=')
718 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000719 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000720 * to avoid nasty things like going to another buffer when
721 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000722 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000723 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000725 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000726 restore_cmdline(&save_ccline);
727
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200728 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200730 len = (int)STRLEN(p);
731 if (realloc_cmdbuff(len + 1) == OK)
732 {
733 ccline.cmdlen = len;
734 STRCPY(ccline.cmdbuff, p);
735 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200737 /* Restore the cursor or use the position set with
738 * set_cmdline_pos(). */
739 if (new_cmdpos > ccline.cmdlen)
740 ccline.cmdpos = ccline.cmdlen;
741 else
742 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200744 KeyTyped = FALSE; /* Don't do p_wc completion. */
745 redrawcmd();
746 goto cmdline_changed;
747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749 }
750 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100751 got_int = FALSE; /* don't abandon the command line */
752 did_emsg = FALSE;
753 emsg_on_display = FALSE;
754 redrawcmd();
755 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 }
757#endif
758 else
759 {
760 if (c == Ctrl_G && p_im && restart_edit == 0)
761 restart_edit = 'a';
762 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
763 in history */
764 goto returncmd; /* back to Normal mode */
765 }
766 }
767
768#ifdef FEAT_CMDWIN
769 if (c == cedit_key || c == K_CMDWIN)
770 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200771 if (ex_normal_busy == 0 && got_int == FALSE)
772 {
773 /*
774 * Open a window to edit the command line (and history).
775 */
Bram Moolenaarc695cec2017-01-08 20:00:04 +0100776 save_cmdline(&save_ccline);
Bram Moolenaar58da7072014-09-09 18:45:49 +0200777 c = ex_window();
Bram Moolenaarc695cec2017-01-08 20:00:04 +0100778 restore_cmdline(&save_ccline);
Bram Moolenaar58da7072014-09-09 18:45:49 +0200779 some_key_typed = TRUE;
780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782# ifdef FEAT_DIGRAPHS
783 else
784# endif
785#endif
786#ifdef FEAT_DIGRAPHS
787 c = do_digraph(c);
788#endif
789
790 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
791 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
792 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000793 /* In Ex mode a backslash escapes a newline. */
794 if (exmode_active
795 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000796 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000797 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000798 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000800 if (c == K_KENTER)
801 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000803 else
804 {
805 gotesc = FALSE; /* Might have typed ESC previously, don't
806 truncate the cmdline now. */
807 if (ccheck_abbr(c + ABBR_OFF))
808 goto cmdline_changed;
809 if (!cmd_silent)
810 {
811 windgoto(msg_row, 0);
812 out_flush();
813 }
814 break;
815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
817
818 /*
819 * Completion for 'wildchar' or 'wildcharm' key.
820 * - hitting <ESC> twice means: abandon command line.
821 * - wildcard expansion is only done when the 'wildchar' key is really
822 * typed, not when it comes from a macro
823 */
824 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
825 {
826 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
827 {
828 /* if 'wildmode' contains "list" may still need to list */
829 if (xpc.xp_numfiles > 1
830 && !did_wild_list
831 && (wim_flags[wim_index] & WIM_LIST))
832 {
833 (void)showmatches(&xpc, FALSE);
834 redrawcmd();
835 did_wild_list = TRUE;
836 }
837 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100838 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
839 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100841 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
842 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 else
844 res = OK; /* don't insert 'wildchar' now */
845 }
846 else /* typed p_wc first time */
847 {
848 wim_index = 0;
849 j = ccline.cmdpos;
850 /* if 'wildmode' first contains "longest", get longest
851 * common part */
852 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100853 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
854 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100856 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
857 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859 /* if interrupted while completing, behave like it failed */
860 if (got_int)
861 {
862 (void)vpeekc(); /* remove <C-C> from input stream */
863 got_int = FALSE; /* don't abandon the command line */
864 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
865#ifdef FEAT_WILDMENU
866 xpc.xp_context = EXPAND_NOTHING;
867#endif
868 goto cmdline_changed;
869 }
870
871 /* when more than one match, and 'wildmode' first contains
872 * "list", or no change and 'wildmode' contains "longest,list",
873 * list all matches */
874 if (res == OK && xpc.xp_numfiles > 1)
875 {
876 /* a "longest" that didn't do anything is skipped (but not
877 * "list:longest") */
878 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
879 wim_index = 1;
880 if ((wim_flags[wim_index] & WIM_LIST)
881#ifdef FEAT_WILDMENU
882 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
883#endif
884 )
885 {
886 if (!(wim_flags[0] & WIM_LONGEST))
887 {
888#ifdef FEAT_WILDMENU
889 int p_wmnu_save = p_wmnu;
890 p_wmnu = 0;
891#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100892 /* remove match */
893 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#ifdef FEAT_WILDMENU
895 p_wmnu = p_wmnu_save;
896#endif
897 }
898#ifdef FEAT_WILDMENU
899 (void)showmatches(&xpc, p_wmnu
900 && ((wim_flags[wim_index] & WIM_LIST) == 0));
901#else
902 (void)showmatches(&xpc, FALSE);
903#endif
904 redrawcmd();
905 did_wild_list = TRUE;
906 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100907 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
908 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100910 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
911 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200914 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916#ifdef FEAT_WILDMENU
917 else if (xpc.xp_numfiles == -1)
918 xpc.xp_context = EXPAND_NOTHING;
919#endif
920 }
921 if (wim_index < 3)
922 ++wim_index;
923 if (c == ESC)
924 gotesc = TRUE;
925 if (res == OK)
926 goto cmdline_changed;
927 }
928
929 gotesc = FALSE;
930
931 /* <S-Tab> goes to last match, in a clumsy way */
932 if (c == K_S_TAB && KeyTyped)
933 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100934 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
935 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
936 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 goto cmdline_changed;
938 }
939
940 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
941 c = NL;
942
943 do_abbr = TRUE; /* default: check for abbreviation */
944
945 /*
946 * Big switch for a typed command line character.
947 */
948 switch (c)
949 {
950 case K_BS:
951 case Ctrl_H:
952 case K_DEL:
953 case K_KDEL:
954 case Ctrl_W:
955#ifdef FEAT_FKMAP
956 if (cmd_fkmap && c == K_BS)
957 c = K_DEL;
958#endif
959 if (c == K_KDEL)
960 c = K_DEL;
961
962 /*
963 * delete current character is the same as backspace on next
964 * character, except at end of line
965 */
966 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
967 ++ccline.cmdpos;
968#ifdef FEAT_MBYTE
969 if (has_mbyte && c == K_DEL)
970 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
971 ccline.cmdbuff + ccline.cmdpos);
972#endif
973 if (ccline.cmdpos > 0)
974 {
975 char_u *p;
976
977 j = ccline.cmdpos;
978 p = ccline.cmdbuff + j;
979#ifdef FEAT_MBYTE
980 if (has_mbyte)
981 {
982 p = mb_prevptr(ccline.cmdbuff, p);
983 if (c == Ctrl_W)
984 {
985 while (p > ccline.cmdbuff && vim_isspace(*p))
986 p = mb_prevptr(ccline.cmdbuff, p);
987 i = mb_get_class(p);
988 while (p > ccline.cmdbuff && mb_get_class(p) == i)
989 p = mb_prevptr(ccline.cmdbuff, p);
990 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000991 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
993 }
994 else
995#endif
996 if (c == Ctrl_W)
997 {
998 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
999 --p;
1000 i = vim_iswordc(p[-1]);
1001 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1002 && vim_iswordc(p[-1]) == i)
1003 --p;
1004 }
1005 else
1006 --p;
1007 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1008 ccline.cmdlen -= j - ccline.cmdpos;
1009 i = ccline.cmdpos;
1010 while (i < ccline.cmdlen)
1011 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1012
1013 /* Truncate at the end, required for multi-byte chars. */
1014 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001015#ifdef FEAT_SEARCH_EXTRA
1016 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001017 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001018 search_start = save_cursor;
1019 /* save view settings, so that the screen
1020 * won't be restored at the wrong position */
1021 old_curswant = init_curswant;
1022 old_leftcol = init_leftcol;
1023 old_topline = init_topline;
1024# ifdef FEAT_DIFF
1025 old_topfill = init_topfill;
1026# endif
1027 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001028 }
1029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 redrawcmd();
1031 }
1032 else if (ccline.cmdlen == 0 && c != Ctrl_W
1033 && ccline.cmdprompt == NULL && indent == 0)
1034 {
1035 /* In ex and debug mode it doesn't make sense to return. */
1036 if (exmode_active
1037#ifdef FEAT_EVAL
1038 || ccline.cmdfirstc == '>'
1039#endif
1040 )
1041 goto cmdline_not_changed;
1042
1043 vim_free(ccline.cmdbuff); /* no commandline to return */
1044 ccline.cmdbuff = NULL;
1045 if (!cmd_silent)
1046 {
1047#ifdef FEAT_RIGHTLEFT
1048 if (cmdmsg_rl)
1049 msg_col = Columns;
1050 else
1051#endif
1052 msg_col = 0;
1053 msg_putchar(' '); /* delete ':' */
1054 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001055#ifdef FEAT_SEARCH_EXTRA
1056 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001057 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 redraw_cmdline = TRUE;
1060 goto returncmd; /* back to cmd mode */
1061 }
1062 goto cmdline_changed;
1063
1064 case K_INS:
1065 case K_KINS:
1066#ifdef FEAT_FKMAP
1067 /* if Farsi mode set, we are in reverse insert mode -
1068 Do not change the mode */
1069 if (cmd_fkmap)
1070 beep_flush();
1071 else
1072#endif
1073 ccline.overstrike = !ccline.overstrike;
1074#ifdef CURSOR_SHAPE
1075 ui_cursor_shape(); /* may show different cursor shape */
1076#endif
1077 goto cmdline_not_changed;
1078
1079 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001080 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
1082 /* ":lmap" mappings exists, toggle use of mappings. */
1083 State ^= LANGMAP;
1084#ifdef USE_IM_CONTROL
1085 im_set_active(FALSE); /* Disable input method */
1086#endif
1087 if (b_im_ptr != NULL)
1088 {
1089 if (State & LANGMAP)
1090 *b_im_ptr = B_IMODE_LMAP;
1091 else
1092 *b_im_ptr = B_IMODE_NONE;
1093 }
1094 }
1095#ifdef USE_IM_CONTROL
1096 else
1097 {
1098 /* There are no ":lmap" mappings, toggle IM. When
1099 * 'imdisable' is set don't try getting the status, it's
1100 * always off. */
1101 if ((p_imdisable && b_im_ptr != NULL)
1102 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1103 {
1104 im_set_active(FALSE); /* Disable input method */
1105 if (b_im_ptr != NULL)
1106 *b_im_ptr = B_IMODE_NONE;
1107 }
1108 else
1109 {
1110 im_set_active(TRUE); /* Enable input method */
1111 if (b_im_ptr != NULL)
1112 *b_im_ptr = B_IMODE_IM;
1113 }
1114 }
1115#endif
1116 if (b_im_ptr != NULL)
1117 {
1118 if (b_im_ptr == &curbuf->b_p_iminsert)
1119 set_iminsert_global();
1120 else
1121 set_imsearch_global();
1122 }
1123#ifdef CURSOR_SHAPE
1124 ui_cursor_shape(); /* may show different cursor shape */
1125#endif
1126#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1127 /* Show/unshow value of 'keymap' in status lines later. */
1128 status_redraw_curbuf();
1129#endif
1130 goto cmdline_not_changed;
1131
1132/* case '@': only in very old vi */
1133 case Ctrl_U:
1134 /* delete all characters left of the cursor */
1135 j = ccline.cmdpos;
1136 ccline.cmdlen -= j;
1137 i = ccline.cmdpos = 0;
1138 while (i < ccline.cmdlen)
1139 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1140 /* Truncate at the end, required for multi-byte chars. */
1141 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001142#ifdef FEAT_SEARCH_EXTRA
1143 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001144 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 redrawcmd();
1147 goto cmdline_changed;
1148
1149#ifdef FEAT_CLIPBOARD
1150 case Ctrl_Y:
1151 /* Copy the modeless selection, if there is one. */
1152 if (clip_star.state != SELECT_CLEARED)
1153 {
1154 if (clip_star.state == SELECT_DONE)
1155 clip_copy_modeless_selection(TRUE);
1156 goto cmdline_not_changed;
1157 }
1158 break;
1159#endif
1160
1161 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1162 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001163 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001164 * ":normal" runs out of characters. */
1165 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001166 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 goto cmdline_not_changed;
1168
1169 gotesc = TRUE; /* will free ccline.cmdbuff after
1170 putting it in history */
1171 goto returncmd; /* back to cmd mode */
1172
1173 case Ctrl_R: /* insert register */
1174#ifdef USE_ON_FLY_SCROLL
1175 dont_scroll = TRUE; /* disallow scrolling here */
1176#endif
1177 putcmdline('"', TRUE);
1178 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001179 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (i == Ctrl_O)
1181 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1182 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001183 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 --no_mapping;
1185#ifdef FEAT_EVAL
1186 /*
1187 * Insert the result of an expression.
1188 * Need to save the current command line, to be able to enter
1189 * a new one...
1190 */
1191 new_cmdpos = -1;
1192 if (c == '=')
1193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1195 {
1196 beep_flush();
1197 c = ESC;
1198 }
1199 else
1200 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001201 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001203 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 }
1206#endif
1207 if (c != ESC) /* use ESC to cancel inserting register */
1208 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001209 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001210
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001211#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001212 /* When there was a serious error abort getting the
1213 * command line. */
1214 if (aborting())
1215 {
1216 gotesc = TRUE; /* will free ccline.cmdbuff after
1217 putting it in history */
1218 goto returncmd; /* back to cmd mode */
1219 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 KeyTyped = FALSE; /* Don't do p_wc completion. */
1222#ifdef FEAT_EVAL
1223 if (new_cmdpos >= 0)
1224 {
1225 /* set_cmdline_pos() was used */
1226 if (new_cmdpos > ccline.cmdlen)
1227 ccline.cmdpos = ccline.cmdlen;
1228 else
1229 ccline.cmdpos = new_cmdpos;
1230 }
1231#endif
1232 }
1233 redrawcmd();
1234 goto cmdline_changed;
1235
1236 case Ctrl_D:
1237 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1238 break; /* Use ^D as normal char instead */
1239
1240 redrawcmd();
1241 continue; /* don't do incremental search now */
1242
1243 case K_RIGHT:
1244 case K_S_RIGHT:
1245 case K_C_RIGHT:
1246 do
1247 {
1248 if (ccline.cmdpos >= ccline.cmdlen)
1249 break;
1250 i = cmdline_charsize(ccline.cmdpos);
1251 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1252 break;
1253 ccline.cmdspos += i;
1254#ifdef FEAT_MBYTE
1255 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001256 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 + ccline.cmdpos);
1258 else
1259#endif
1260 ++ccline.cmdpos;
1261 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001262 while ((c == K_S_RIGHT || c == K_C_RIGHT
1263 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1265#ifdef FEAT_MBYTE
1266 if (has_mbyte)
1267 set_cmdspos_cursor();
1268#endif
1269 goto cmdline_not_changed;
1270
1271 case K_LEFT:
1272 case K_S_LEFT:
1273 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001274 if (ccline.cmdpos == 0)
1275 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 do
1277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 --ccline.cmdpos;
1279#ifdef FEAT_MBYTE
1280 if (has_mbyte) /* move to first byte of char */
1281 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1282 ccline.cmdbuff + ccline.cmdpos);
1283#endif
1284 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1285 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001286 while (ccline.cmdpos > 0
1287 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001288 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1290#ifdef FEAT_MBYTE
1291 if (has_mbyte)
1292 set_cmdspos_cursor();
1293#endif
1294 goto cmdline_not_changed;
1295
1296 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001297 /* Ignore mouse event or ex_window() result. */
1298 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
Bram Moolenaar4770d092006-01-12 23:22:24 +00001300#ifdef FEAT_GUI_W32
1301 /* On Win32 ignore <M-F4>, we get it when closing the window was
1302 * cancelled. */
1303 case K_F4:
1304 if (mod_mask == MOD_MASK_ALT)
1305 {
1306 redrawcmd(); /* somehow the cmdline is cleared */
1307 goto cmdline_not_changed;
1308 }
1309 break;
1310#endif
1311
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312#ifdef FEAT_MOUSE
1313 case K_MIDDLEDRAG:
1314 case K_MIDDLERELEASE:
1315 goto cmdline_not_changed; /* Ignore mouse */
1316
1317 case K_MIDDLEMOUSE:
1318# ifdef FEAT_GUI
1319 /* When GUI is active, also paste when 'mouse' is empty */
1320 if (!gui.in_use)
1321# endif
1322 if (!mouse_has(MOUSE_COMMAND))
1323 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001324# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001326 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001328# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001329 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 redrawcmd();
1331 goto cmdline_changed;
1332
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001333# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001335 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 redrawcmd();
1337 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001338# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
1340 case K_LEFTDRAG:
1341 case K_LEFTRELEASE:
1342 case K_RIGHTDRAG:
1343 case K_RIGHTRELEASE:
1344 /* Ignore drag and release events when the button-down wasn't
1345 * seen before. */
1346 if (ignore_drag_release)
1347 goto cmdline_not_changed;
1348 /* FALLTHROUGH */
1349 case K_LEFTMOUSE:
1350 case K_RIGHTMOUSE:
1351 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1352 ignore_drag_release = TRUE;
1353 else
1354 ignore_drag_release = FALSE;
1355# ifdef FEAT_GUI
1356 /* When GUI is active, also move when 'mouse' is empty */
1357 if (!gui.in_use)
1358# endif
1359 if (!mouse_has(MOUSE_COMMAND))
1360 goto cmdline_not_changed; /* Ignore mouse */
1361# ifdef FEAT_CLIPBOARD
1362 if (mouse_row < cmdline_row && clip_star.available)
1363 {
1364 int button, is_click, is_drag;
1365
1366 /*
1367 * Handle modeless selection.
1368 */
1369 button = get_mouse_button(KEY2TERMCAP1(c),
1370 &is_click, &is_drag);
1371 if (mouse_model_popup() && button == MOUSE_LEFT
1372 && (mod_mask & MOD_MASK_SHIFT))
1373 {
1374 /* Translate shift-left to right button. */
1375 button = MOUSE_RIGHT;
1376 mod_mask &= ~MOD_MASK_SHIFT;
1377 }
1378 clip_modeless(button, is_click, is_drag);
1379 goto cmdline_not_changed;
1380 }
1381# endif
1382
1383 set_cmdspos();
1384 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1385 ++ccline.cmdpos)
1386 {
1387 i = cmdline_charsize(ccline.cmdpos);
1388 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1389 && mouse_col < ccline.cmdspos % Columns + i)
1390 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001391# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 if (has_mbyte)
1393 {
1394 /* Count ">" for double-wide char that doesn't fit. */
1395 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001396 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 + ccline.cmdpos) - 1;
1398 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001399# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 ccline.cmdspos += i;
1401 }
1402 goto cmdline_not_changed;
1403
1404 /* Mouse scroll wheel: ignored here */
1405 case K_MOUSEDOWN:
1406 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001407 case K_MOUSELEFT:
1408 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /* Alternate buttons ignored here */
1410 case K_X1MOUSE:
1411 case K_X1DRAG:
1412 case K_X1RELEASE:
1413 case K_X2MOUSE:
1414 case K_X2DRAG:
1415 case K_X2RELEASE:
1416 goto cmdline_not_changed;
1417
1418#endif /* FEAT_MOUSE */
1419
1420#ifdef FEAT_GUI
1421 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1422 case K_LEFTRELEASE_NM:
1423 goto cmdline_not_changed;
1424
1425 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001426 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 {
1428 gui_do_scroll();
1429 redrawcmd();
1430 }
1431 goto cmdline_not_changed;
1432
1433 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001434 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001436 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 redrawcmd();
1438 }
1439 goto cmdline_not_changed;
1440#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001441#ifdef FEAT_GUI_TABLINE
1442 case K_TABLINE:
1443 case K_TABMENU:
1444 /* Don't want to change any tabs here. Make sure the same tab
1445 * is still selected. */
1446 if (gui_use_tabline())
1447 gui_mch_set_curtab(tabpage_index(curtab));
1448 goto cmdline_not_changed;
1449#endif
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 case K_SELECT: /* end of Select mode mapping - ignore */
1452 goto cmdline_not_changed;
1453
1454 case Ctrl_B: /* begin of command line */
1455 case K_HOME:
1456 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 case K_S_HOME:
1458 case K_C_HOME:
1459 ccline.cmdpos = 0;
1460 set_cmdspos();
1461 goto cmdline_not_changed;
1462
1463 case Ctrl_E: /* end of command line */
1464 case K_END:
1465 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 case K_S_END:
1467 case K_C_END:
1468 ccline.cmdpos = ccline.cmdlen;
1469 set_cmdspos_cursor();
1470 goto cmdline_not_changed;
1471
1472 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001473 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 break;
1475 goto cmdline_changed;
1476
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001477 case Ctrl_L:
1478#ifdef FEAT_SEARCH_EXTRA
1479 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1480 {
1481 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001482 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001483 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001484 curwin->w_cursor = match_end;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001485 if (!equalpos(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001486 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001487 c = gchar_cursor();
1488 /* If 'ignorecase' and 'smartcase' are set and the
1489 * command line has no uppercase characters, convert
1490 * the character to lowercase */
1491 if (p_ic && p_scs
1492 && !pat_has_uppercase(ccline.cmdbuff))
1493 c = MB_TOLOWER(c);
1494 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001495 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001496 if (c == firstc || vim_strchr((char_u *)(
1497 p_magic ? "\\^$.*[" : "\\^$"), c)
1498 != NULL)
1499 {
1500 /* put a backslash before special
1501 * characters */
1502 stuffcharReadbuff(c);
1503 c = '\\';
1504 }
1505 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001506 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001507 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001508 }
1509 goto cmdline_not_changed;
1510 }
1511#endif
1512
1513 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001514 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 break;
1516 goto cmdline_changed;
1517
1518 case Ctrl_N: /* next match */
1519 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001520 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001522 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1523 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001525 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 }
Bram Moolenaar11956692016-08-27 16:26:56 +02001527 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
1529#ifdef FEAT_CMDHIST
1530 case K_UP:
1531 case K_DOWN:
1532 case K_S_UP:
1533 case K_S_DOWN:
1534 case K_PAGEUP:
1535 case K_KPAGEUP:
1536 case K_PAGEDOWN:
1537 case K_KPAGEDOWN:
1538 if (hislen == 0 || firstc == NUL) /* no history */
1539 goto cmdline_not_changed;
1540
1541 i = hiscnt;
1542
1543 /* save current command string so it can be restored later */
1544 if (lookfor == NULL)
1545 {
1546 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1547 goto cmdline_not_changed;
1548 lookfor[ccline.cmdpos] = NUL;
1549 }
1550
1551 j = (int)STRLEN(lookfor);
1552 for (;;)
1553 {
1554 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001555 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001556 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 {
1558 if (hiscnt == hislen) /* first time */
1559 hiscnt = hisidx[histype];
1560 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1561 hiscnt = hislen - 1;
1562 else if (hiscnt != hisidx[histype] + 1)
1563 --hiscnt;
1564 else /* at top of list */
1565 {
1566 hiscnt = i;
1567 break;
1568 }
1569 }
1570 else /* one step forwards */
1571 {
1572 /* on last entry, clear the line */
1573 if (hiscnt == hisidx[histype])
1574 {
1575 hiscnt = hislen;
1576 break;
1577 }
1578
1579 /* not on a history line, nothing to do */
1580 if (hiscnt == hislen)
1581 break;
1582 if (hiscnt == hislen - 1) /* wrap around */
1583 hiscnt = 0;
1584 else
1585 ++hiscnt;
1586 }
1587 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1588 {
1589 hiscnt = i;
1590 break;
1591 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001592 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001593 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 || STRNCMP(history[histype][hiscnt].hisstr,
1595 lookfor, (size_t)j) == 0)
1596 break;
1597 }
1598
1599 if (hiscnt != i) /* jumped to other entry */
1600 {
1601 char_u *p;
1602 int len;
1603 int old_firstc;
1604
1605 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001606 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (hiscnt == hislen)
1608 p = lookfor; /* back to the old one */
1609 else
1610 p = history[histype][hiscnt].hisstr;
1611
1612 if (histype == HIST_SEARCH
1613 && p != lookfor
1614 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1615 {
1616 /* Correct for the separator character used when
1617 * adding the history entry vs the one used now.
1618 * First loop: count length.
1619 * Second loop: copy the characters. */
1620 for (i = 0; i <= 1; ++i)
1621 {
1622 len = 0;
1623 for (j = 0; p[j] != NUL; ++j)
1624 {
1625 /* Replace old sep with new sep, unless it is
1626 * escaped. */
1627 if (p[j] == old_firstc
1628 && (j == 0 || p[j - 1] != '\\'))
1629 {
1630 if (i > 0)
1631 ccline.cmdbuff[len] = firstc;
1632 }
1633 else
1634 {
1635 /* Escape new sep, unless it is already
1636 * escaped. */
1637 if (p[j] == firstc
1638 && (j == 0 || p[j - 1] != '\\'))
1639 {
1640 if (i > 0)
1641 ccline.cmdbuff[len] = '\\';
1642 ++len;
1643 }
1644 if (i > 0)
1645 ccline.cmdbuff[len] = p[j];
1646 }
1647 ++len;
1648 }
1649 if (i == 0)
1650 {
1651 alloc_cmdbuff(len);
1652 if (ccline.cmdbuff == NULL)
1653 goto returncmd;
1654 }
1655 }
1656 ccline.cmdbuff[len] = NUL;
1657 }
1658 else
1659 {
1660 alloc_cmdbuff((int)STRLEN(p));
1661 if (ccline.cmdbuff == NULL)
1662 goto returncmd;
1663 STRCPY(ccline.cmdbuff, p);
1664 }
1665
1666 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1667 redrawcmd();
1668 goto cmdline_changed;
1669 }
1670 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001671#endif
1672 goto cmdline_not_changed;
1673
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001674#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001675 case Ctrl_G: /* next match */
1676 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001677 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1678 {
1679 pos_T t;
1680 int search_flags = SEARCH_KEEP + SEARCH_NOOF
1681 + SEARCH_PEEK;
1682
1683 if (char_avail())
1684 continue;
1685 cursor_off();
1686 out_flush();
1687 if (c == Ctrl_G)
1688 {
1689 t = match_end;
1690 search_flags += SEARCH_COL;
1691 }
1692 else
1693 t = match_start;
1694 ++emsg_off;
1695 i = searchit(curwin, curbuf, &t,
1696 c == Ctrl_G ? FORWARD : BACKWARD,
1697 ccline.cmdbuff, count, search_flags,
1698 RE_SEARCH, 0, NULL);
1699 --emsg_off;
1700 if (i)
1701 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001702 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001703 match_end = t;
1704 match_start = t;
1705 if (c == Ctrl_T && firstc == '/')
1706 {
1707 /* move just before the current match, so that
1708 * when nv_search finishes the cursor will be
1709 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001710 search_start = t;
1711 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001712 }
Bram Moolenaardda933d2016-09-03 21:04:58 +02001713 if (lt(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001714 {
1715 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001716 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001717 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001718 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001719 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001720 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001721 }
1722
1723 set_search_match(&match_end);
1724 curwin->w_cursor = match_start;
1725 changed_cline_bef_curs();
1726 update_topline();
1727 validate_cursor();
1728 highlight_match = TRUE;
1729 old_curswant = curwin->w_curswant;
1730 old_leftcol = curwin->w_leftcol;
1731 old_topline = curwin->w_topline;
1732# ifdef FEAT_DIFF
1733 old_topfill = curwin->w_topfill;
1734# endif
1735 old_botline = curwin->w_botline;
1736 update_screen(NOT_VALID);
1737 redrawcmdline();
1738 }
1739 else
1740 vim_beep(BO_ERROR);
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001741 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001742 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#endif
1745
1746 case Ctrl_V:
1747 case Ctrl_Q:
1748#ifdef FEAT_MOUSE
1749 ignore_drag_release = TRUE;
1750#endif
1751 putcmdline('^', TRUE);
1752 c = get_literal(); /* get next (two) character(s) */
1753 do_abbr = FALSE; /* don't do abbreviation now */
1754#ifdef FEAT_MBYTE
1755 /* may need to remove ^ when composing char was typed */
1756 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1757 {
1758 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1759 msg_putchar(' ');
1760 cursorcmd();
1761 }
1762#endif
1763 break;
1764
1765#ifdef FEAT_DIGRAPHS
1766 case Ctrl_K:
1767#ifdef FEAT_MOUSE
1768 ignore_drag_release = TRUE;
1769#endif
1770 putcmdline('?', TRUE);
1771#ifdef USE_ON_FLY_SCROLL
1772 dont_scroll = TRUE; /* disallow scrolling here */
1773#endif
1774 c = get_digraph(TRUE);
1775 if (c != NUL)
1776 break;
1777
1778 redrawcmd();
1779 goto cmdline_not_changed;
1780#endif /* FEAT_DIGRAPHS */
1781
1782#ifdef FEAT_RIGHTLEFT
1783 case Ctrl__: /* CTRL-_: switch language mode */
1784 if (!p_ari)
1785 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001786# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (p_altkeymap)
1788 {
1789 cmd_fkmap = !cmd_fkmap;
1790 if (cmd_fkmap) /* in Farsi always in Insert mode */
1791 ccline.overstrike = FALSE;
1792 }
1793 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 cmd_hkmap = !cmd_hkmap;
1796 goto cmdline_not_changed;
1797#endif
1798
1799 default:
1800#ifdef UNIX
1801 if (c == intr_char)
1802 {
1803 gotesc = TRUE; /* will free ccline.cmdbuff after
1804 putting it in history */
1805 goto returncmd; /* back to Normal mode */
1806 }
1807#endif
1808 /*
1809 * Normal character with no special meaning. Just set mod_mask
1810 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1811 * the string <S-Space>. This should only happen after ^V.
1812 */
1813 if (!IS_SPECIAL(c))
1814 mod_mask = 0x0;
1815 break;
1816 }
1817 /*
1818 * End of switch on command line character.
1819 * We come here if we have a normal character.
1820 */
1821
Bram Moolenaarede3e632013-06-23 16:16:19 +02001822 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823#ifdef FEAT_MBYTE
1824 /* Add ABBR_OFF for characters above 0x100, this is
1825 * what check_abbr() expects. */
1826 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1827#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001828 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 goto cmdline_changed;
1830
1831 /*
1832 * put the character in the command line
1833 */
1834 if (IS_SPECIAL(c) || mod_mask != 0)
1835 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1836 else
1837 {
1838#ifdef FEAT_MBYTE
1839 if (has_mbyte)
1840 {
1841 j = (*mb_char2bytes)(c, IObuff);
1842 IObuff[j] = NUL; /* exclude composing chars */
1843 put_on_cmdline(IObuff, j, TRUE);
1844 }
1845 else
1846#endif
1847 {
1848 IObuff[0] = c;
1849 put_on_cmdline(IObuff, 1, TRUE);
1850 }
1851 }
1852 goto cmdline_changed;
1853
1854/*
1855 * This part implements incremental searches for "/" and "?"
1856 * Jump to cmdline_not_changed when a character has been read but the command
1857 * line did not change. Then we only search and redraw if something changed in
1858 * the past.
1859 * Jump to cmdline_changed when the command line did change.
1860 * (Sorry for the goto's, I know it is ugly).
1861 */
1862cmdline_not_changed:
1863#ifdef FEAT_SEARCH_EXTRA
1864 if (!incsearch_postponed)
1865 continue;
1866#endif
1867
1868cmdline_changed:
1869#ifdef FEAT_SEARCH_EXTRA
1870 /*
1871 * 'incsearch' highlighting.
1872 */
1873 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1874 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001875 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001876#ifdef FEAT_RELTIME
1877 proftime_T tm;
1878#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001879
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 /* if there is a character waiting, search and redraw later */
1881 if (char_avail())
1882 {
1883 incsearch_postponed = TRUE;
1884 continue;
1885 }
1886 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001887 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889 /* If there is no command line, don't do anything */
1890 if (ccline.cmdlen == 0)
1891 i = 0;
1892 else
1893 {
1894 cursor_off(); /* so the user knows we're busy */
1895 out_flush();
1896 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001897#ifdef FEAT_RELTIME
1898 /* Set the time limit to half a second. */
1899 profile_setlimit(500L, &tm);
1900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001902 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1903#ifdef FEAT_RELTIME
1904 &tm
1905#else
1906 NULL
1907#endif
1908 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 --emsg_off;
1910 /* if interrupted while searching, behave like it failed */
1911 if (got_int)
1912 {
1913 (void)vpeekc(); /* remove <C-C> from input stream */
1914 got_int = FALSE; /* don't abandon the command line */
1915 i = 0;
1916 }
1917 else if (char_avail())
1918 /* cancelled searching because a char was typed */
1919 incsearch_postponed = TRUE;
1920 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001921 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 highlight_match = TRUE; /* highlight position */
1923 else
1924 highlight_match = FALSE; /* remove highlight */
1925
1926 /* first restore the old curwin values, so the screen is
1927 * positioned in the same way as the actual search command */
1928 curwin->w_leftcol = old_leftcol;
1929 curwin->w_topline = old_topline;
1930# ifdef FEAT_DIFF
1931 curwin->w_topfill = old_topfill;
1932# endif
1933 curwin->w_botline = old_botline;
1934 changed_cline_bef_curs();
1935 update_topline();
1936
1937 if (i != 0)
1938 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001939 pos_T save_pos = curwin->w_cursor;
1940
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001941 match_start = curwin->w_cursor;
1942 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001944 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001945 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001946 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001948 else
1949 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001950
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001952# ifdef FEAT_WINDOWS
1953 /* May redraw the status line to show the cursor position. */
1954 if (p_ru && curwin->w_status_height > 0)
1955 curwin->w_redr_status = TRUE;
1956# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001958 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001959 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001960 restore_cmdline(&save_ccline);
1961
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001962 /* Leave it at the end to make CTRL-R CTRL-W work. */
1963 if (i != 0)
1964 curwin->w_cursor = end_pos;
1965
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 msg_starthere();
1967 redrawcmdline();
1968 did_incsearch = TRUE;
1969 }
1970#else /* FEAT_SEARCH_EXTRA */
1971 ;
1972#endif
1973
1974#ifdef FEAT_RIGHTLEFT
1975 if (cmdmsg_rl
1976# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001977 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978# endif
1979 )
1980 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001981 * right-left typing. Not efficient, but it works.
1982 * Do it only when there are no characters left to read
1983 * to avoid useless intermediate redraws. */
1984 if (vpeekc() == NUL)
1985 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986#endif
1987 }
1988
1989returncmd:
1990
1991#ifdef FEAT_RIGHTLEFT
1992 cmdmsg_rl = FALSE;
1993#endif
1994
1995#ifdef FEAT_FKMAP
1996 cmd_fkmap = 0;
1997#endif
1998
1999 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002000 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002#ifdef FEAT_SEARCH_EXTRA
2003 if (did_incsearch)
2004 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002005 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002006 curwin->w_cursor = save_cursor;
2007 else
2008 {
2009 if (!equalpos(save_cursor, search_start))
2010 {
2011 /* put the '" mark at the original position */
2012 curwin->w_cursor = save_cursor;
2013 setpcmark();
2014 }
2015 curwin->w_cursor = search_start;
2016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 curwin->w_curswant = old_curswant;
2018 curwin->w_leftcol = old_leftcol;
2019 curwin->w_topline = old_topline;
2020# ifdef FEAT_DIFF
2021 curwin->w_topfill = old_topfill;
2022# endif
2023 curwin->w_botline = old_botline;
2024 highlight_match = FALSE;
2025 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002026 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 }
2028#endif
2029
2030 if (ccline.cmdbuff != NULL)
2031 {
2032 /*
2033 * Put line in history buffer (":" and "=" only when it was typed).
2034 */
2035#ifdef FEAT_CMDHIST
2036 if (ccline.cmdlen && firstc != NUL
2037 && (some_key_typed || histype == HIST_SEARCH))
2038 {
2039 add_to_history(histype, ccline.cmdbuff, TRUE,
2040 histype == HIST_SEARCH ? firstc : NUL);
2041 if (firstc == ':')
2042 {
2043 vim_free(new_last_cmdline);
2044 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2045 }
2046 }
2047#endif
2048
2049 if (gotesc) /* abandon command line */
2050 {
2051 vim_free(ccline.cmdbuff);
2052 ccline.cmdbuff = NULL;
2053 if (msg_scrolled == 0)
2054 compute_cmdrow();
2055 MSG("");
2056 redraw_cmdline = TRUE;
2057 }
2058 }
2059
2060 /*
2061 * If the screen was shifted up, redraw the whole screen (later).
2062 * If the line is too long, clear it, so ruler and shown command do
2063 * not get printed in the middle of it.
2064 */
2065 msg_check();
2066 msg_scroll = save_msg_scroll;
2067 redir_off = FALSE;
2068
2069 /* When the command line was typed, no need for a wait-return prompt. */
2070 if (some_key_typed)
2071 need_wait_return = FALSE;
2072
2073 State = save_State;
2074#ifdef USE_IM_CONTROL
2075 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2076 im_save_status(b_im_ptr);
2077 im_set_active(FALSE);
2078#endif
2079#ifdef FEAT_MOUSE
2080 setmouse();
2081#endif
2082#ifdef CURSOR_SHAPE
2083 ui_cursor_shape(); /* may show different cursor shape */
2084#endif
2085
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002086 {
2087 char_u *p = ccline.cmdbuff;
2088
2089 /* Make ccline empty, getcmdline() may try to use it. */
2090 ccline.cmdbuff = NULL;
2091 return p;
2092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093}
2094
2095#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2096/*
2097 * Get a command line with a prompt.
2098 * This is prepared to be called recursively from getcmdline() (e.g. by
2099 * f_input() when evaluating an expression from CTRL-R =).
2100 * Returns the command line in allocated memory, or NULL.
2101 */
2102 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002103getcmdline_prompt(
2104 int firstc,
2105 char_u *prompt, /* command line prompt */
2106 int attr, /* attributes for prompt */
2107 int xp_context, /* type of expansion */
2108 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109{
2110 char_u *s;
2111 struct cmdline_info save_ccline;
2112 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002113 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002115 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 ccline.cmdprompt = prompt;
2117 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002118# ifdef FEAT_EVAL
2119 ccline.xp_context = xp_context;
2120 ccline.xp_arg = xp_arg;
2121 ccline.input_fn = (firstc == '@');
2122# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002123 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002125 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002126 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002127 /* Restore msg_col, the prompt from input() may have changed it.
2128 * But only if called recursively and the commandline is therefore being
2129 * restored to an old one; if not, the input() prompt stays on the screen,
2130 * so we need its modified msg_col left intact. */
2131 if (ccline.cmdbuff != NULL)
2132 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134 return s;
2135}
2136#endif
2137
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002138/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002139 * Return TRUE when the text must not be changed and we can't switch to
2140 * another window or buffer. Used when editing the command line, evaluating
2141 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002142 */
2143 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002144text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002145{
2146#ifdef FEAT_CMDWIN
2147 if (cmdwin_type != 0)
2148 return TRUE;
2149#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002150 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002151}
2152
2153/*
2154 * Give an error message for a command that isn't allowed while the cmdline
2155 * window is open or editing the cmdline in another way.
2156 */
2157 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002158text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002159{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002160 EMSG(_(get_text_locked_msg()));
2161}
2162
2163 char_u *
2164get_text_locked_msg(void)
2165{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002166#ifdef FEAT_CMDWIN
2167 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002168 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002169#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002170 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002171}
2172
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173#if defined(FEAT_AUTOCMD) || defined(PROTO)
2174/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002175 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2176 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002177 */
2178 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002179curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002180{
2181 if (curbuf_lock > 0)
2182 {
2183 EMSG(_("E788: Not allowed to edit another buffer now"));
2184 return TRUE;
2185 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002186 return allbuf_locked();
2187}
2188
2189/*
2190 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2191 * message.
2192 */
2193 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002194allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002195{
2196 if (allbuf_lock > 0)
2197 {
2198 EMSG(_("E811: Not allowed to change buffer information now"));
2199 return TRUE;
2200 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201 return FALSE;
2202}
2203#endif
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002206cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207{
2208#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2209 if (cmdline_star > 0) /* showing '*', always 1 position */
2210 return 1;
2211#endif
2212 return ptr2cells(ccline.cmdbuff + idx);
2213}
2214
2215/*
2216 * Compute the offset of the cursor on the command line for the prompt and
2217 * indent.
2218 */
2219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002220set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002222 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 ccline.cmdspos = 1 + ccline.cmdindent;
2224 else
2225 ccline.cmdspos = 0 + ccline.cmdindent;
2226}
2227
2228/*
2229 * Compute the screen position for the cursor on the command line.
2230 */
2231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002232set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233{
2234 int i, m, c;
2235
2236 set_cmdspos();
2237 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002240 if (m < 0) /* overflow, Columns or Rows at weird value */
2241 m = MAXCOL;
2242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 else
2244 m = MAXCOL;
2245 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2246 {
2247 c = cmdline_charsize(i);
2248#ifdef FEAT_MBYTE
2249 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2250 if (has_mbyte)
2251 correct_cmdspos(i, c);
2252#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002253 /* If the cmdline doesn't fit, show cursor on last visible char.
2254 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if ((ccline.cmdspos += c) >= m)
2256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 ccline.cmdspos -= c;
2258 break;
2259 }
2260#ifdef FEAT_MBYTE
2261 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002262 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263#endif
2264 }
2265}
2266
2267#ifdef FEAT_MBYTE
2268/*
2269 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2270 * character that doesn't fit, so that a ">" must be displayed.
2271 */
2272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002273correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002275 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2277 && ccline.cmdspos % Columns + cells > Columns)
2278 ccline.cmdspos++;
2279}
2280#endif
2281
2282/*
2283 * Get an Ex command line for the ":" command.
2284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002286getexline(
2287 int c, /* normally ':', NUL for ":append" */
2288 void *cookie UNUSED,
2289 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290{
2291 /* When executing a register, remove ':' that's in front of each line. */
2292 if (exec_from_reg && vpeekc() == ':')
2293 (void)vgetc();
2294 return getcmdline(c, 1L, indent);
2295}
2296
2297/*
2298 * Get an Ex command line for Ex mode.
2299 * In Ex mode we only use the OS supplied line editing features and no
2300 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002301 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002304getexmodeline(
2305 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002307 void *cookie UNUSED,
2308 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 garray_T line_ga;
2311 char_u *pend;
2312 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002313 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002314 int escaped = FALSE; /* CTRL-V typed */
2315 int vcol = 0;
2316 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002317 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002318 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
2320 /* Switch cursor on now. This avoids that it happens after the "\n", which
2321 * confuses the system function that computes tabstops. */
2322 cursor_on();
2323
2324 /* always start in column 0; write a newline if necessary */
2325 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002326 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002328 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002330 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002331 if (p_prompt)
2332 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 while (indent-- > 0)
2334 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337
2338 ga_init2(&line_ga, 1, 30);
2339
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002340 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002341 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002342 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002343 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002344 while (indent >= 8)
2345 {
2346 ga_append(&line_ga, TAB);
2347 msg_puts((char_u *)" ");
2348 indent -= 8;
2349 }
2350 while (indent-- > 0)
2351 {
2352 ga_append(&line_ga, ' ');
2353 msg_putchar(' ');
2354 }
2355 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002356 ++no_mapping;
2357 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002358
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 /*
2360 * Get the line, one character at a time.
2361 */
2362 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002363 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002365 long sw;
2366 char_u *s;
2367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 if (ga_grow(&line_ga, 40) == FAIL)
2369 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002371 /* Get one character at a time. Don't use inchar(), it can't handle
2372 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002373 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002374 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002377 * Handle line editing.
2378 * Previously this was left to the system, putting the terminal in
2379 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002381 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002383 msg_putchar('\n');
2384 break;
2385 }
2386
2387 if (!escaped)
2388 {
2389 /* CR typed means "enter", which is NL */
2390 if (c1 == '\r')
2391 c1 = '\n';
2392
2393 if (c1 == BS || c1 == K_BS
2394 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002396 if (line_ga.ga_len > 0)
2397 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002398#ifdef FEAT_MBYTE
2399 if (has_mbyte)
2400 {
2401 p = (char_u *)line_ga.ga_data;
2402 p[line_ga.ga_len] = NUL;
2403 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2404 line_ga.ga_len -= len;
2405 }
2406 else
2407#endif
2408 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002409 goto redraw;
2410 }
2411 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002414 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002416 msg_col = startcol;
2417 msg_clr_eos();
2418 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002419 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002420 }
2421
2422 if (c1 == Ctrl_T)
2423 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002424 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002425 p = (char_u *)line_ga.ga_data;
2426 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002427 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002428 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002429add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002430 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002432 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002433 p = (char_u *)line_ga.ga_data;
2434 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002435 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2436 *s = ' ';
2437 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002439redraw:
2440 /* redraw the line */
2441 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002442 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002443 p = (char_u *)line_ga.ga_data;
2444 p[line_ga.ga_len] = NUL;
2445 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002447 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002449 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002451 msg_putchar(' ');
2452 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002453 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002457 len = MB_PTR2LEN(p);
2458 msg_outtrans_len(p, len);
2459 vcol += ptr2cells(p);
2460 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
2462 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002463 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002464 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002465 continue;
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002468 if (c1 == Ctrl_D)
2469 {
2470 /* Delete one shiftwidth. */
2471 p = (char_u *)line_ga.ga_data;
2472 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002474 if (prev_char == '^')
2475 ex_keep_indent = TRUE;
2476 indent = 0;
2477 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 }
2479 else
2480 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002481 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002482 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002483 if (indent > 0)
2484 {
2485 --indent;
2486 indent -= indent % get_sw_value(curbuf);
2487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002489 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002490 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002491 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002492 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2493 --line_ga.ga_len;
2494 }
2495 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002497
2498 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2499 {
2500 escaped = TRUE;
2501 continue;
2502 }
2503
2504 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2505 if (IS_SPECIAL(c1))
2506 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002508
2509 if (IS_SPECIAL(c1))
2510 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002511#ifdef FEAT_MBYTE
2512 if (has_mbyte)
2513 len = (*mb_char2bytes)(c1,
2514 (char_u *)line_ga.ga_data + line_ga.ga_len);
2515 else
2516#endif
2517 {
2518 len = 1;
2519 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2520 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002521 if (c1 == '\n')
2522 msg_putchar('\n');
2523 else if (c1 == TAB)
2524 {
2525 /* Don't use chartabsize(), 'ts' can be different */
2526 do
2527 {
2528 msg_putchar(' ');
2529 } while (++vcol % 8);
2530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002533 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002534 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002535 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002537 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002538 escaped = FALSE;
2539
2540 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002541 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002542
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002543 /* We are done when a NL is entered, but not when it comes after an
2544 * odd number of backslashes, that results in a NUL. */
2545 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002547 int bcount = 0;
2548
2549 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2550 ++bcount;
2551
2552 if (bcount > 0)
2553 {
2554 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2555 * "\NL", etc. */
2556 line_ga.ga_len -= (bcount + 1) / 2;
2557 pend -= (bcount + 1) / 2;
2558 pend[-1] = '\n';
2559 }
2560
2561 if ((bcount & 1) == 0)
2562 {
2563 --line_ga.ga_len;
2564 --pend;
2565 *pend = NUL;
2566 break;
2567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569 }
2570
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002571 --no_mapping;
2572 --allow_keys;
2573
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 /* make following messages go to the next line */
2575 msg_didout = FALSE;
2576 msg_col = 0;
2577 if (msg_row < Rows - 1)
2578 ++msg_row;
2579 emsg_on_display = FALSE; /* don't want ui_delay() */
2580
2581 if (got_int)
2582 ga_clear(&line_ga);
2583
2584 return (char_u *)line_ga.ga_data;
2585}
2586
Bram Moolenaare344bea2005-09-01 20:46:49 +00002587# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2588 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589/*
2590 * Return TRUE if ccline.overstrike is on.
2591 */
2592 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002593cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
2595 return ccline.overstrike;
2596}
2597
2598/*
2599 * Return TRUE if the cursor is at the end of the cmdline.
2600 */
2601 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002602cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
2604 return (ccline.cmdpos >= ccline.cmdlen);
2605}
2606#endif
2607
Bram Moolenaar9372a112005-12-06 19:59:18 +00002608#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609/*
2610 * Return the virtual column number at the current cursor position.
2611 * This is used by the IM code to obtain the start of the preedit string.
2612 */
2613 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002614cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2617 return MAXCOL;
2618
2619# ifdef FEAT_MBYTE
2620 if (has_mbyte)
2621 {
2622 colnr_T col;
2623 int i = 0;
2624
2625 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002626 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
2628 return col;
2629 }
2630 else
2631# endif
2632 return ccline.cmdpos;
2633}
2634#endif
2635
2636#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2637/*
2638 * If part of the command line is an IM preedit string, redraw it with
2639 * IM feedback attributes. The cursor position is restored after drawing.
2640 */
2641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002642redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
2644 if ((State & CMDLINE)
2645 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002646 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 && !p_imdisable
2648 && im_is_preediting())
2649 {
2650 int cmdpos = 0;
2651 int cmdspos;
2652 int old_row;
2653 int old_col;
2654 colnr_T col;
2655
2656 old_row = msg_row;
2657 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002658 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659
2660# ifdef FEAT_MBYTE
2661 if (has_mbyte)
2662 {
2663 for (col = 0; col < preedit_start_col
2664 && cmdpos < ccline.cmdlen; ++col)
2665 {
2666 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002667 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
2669 }
2670 else
2671# endif
2672 {
2673 cmdspos += preedit_start_col;
2674 cmdpos += preedit_start_col;
2675 }
2676
2677 msg_row = cmdline_row + (cmdspos / (int)Columns);
2678 msg_col = cmdspos % (int)Columns;
2679 if (msg_row >= Rows)
2680 msg_row = Rows - 1;
2681
2682 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2683 {
2684 int char_len;
2685 int char_attr;
2686
2687 char_attr = im_get_feedback_attr(col);
2688 if (char_attr < 0)
2689 break; /* end of preedit string */
2690
2691# ifdef FEAT_MBYTE
2692 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002693 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 else
2695# endif
2696 char_len = 1;
2697
2698 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2699 cmdpos += char_len;
2700 }
2701
2702 msg_row = old_row;
2703 msg_col = old_col;
2704 }
2705}
2706#endif /* FEAT_XIM && FEAT_GUI_GTK */
2707
2708/*
2709 * Allocate a new command line buffer.
2710 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2711 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2712 */
2713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002714alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
2716 /*
2717 * give some extra space to avoid having to allocate all the time
2718 */
2719 if (len < 80)
2720 len = 100;
2721 else
2722 len += 20;
2723
2724 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2725 ccline.cmdbufflen = len;
2726}
2727
2728/*
2729 * Re-allocate the command line to length len + something extra.
2730 * return FAIL for failure, OK otherwise
2731 */
2732 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002733realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734{
2735 char_u *p;
2736
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002737 if (len < ccline.cmdbufflen)
2738 return OK; /* no need to resize */
2739
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 p = ccline.cmdbuff;
2741 alloc_cmdbuff(len); /* will get some more */
2742 if (ccline.cmdbuff == NULL) /* out of memory */
2743 {
2744 ccline.cmdbuff = p; /* keep the old one */
2745 return FAIL;
2746 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002747 /* There isn't always a NUL after the command, but it may need to be
2748 * there, thus copy up to the NUL and add a NUL. */
2749 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2750 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002752
2753 if (ccline.xpc != NULL
2754 && ccline.xpc->xp_pattern != NULL
2755 && ccline.xpc->xp_context != EXPAND_NOTHING
2756 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2757 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002758 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002759
2760 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2761 * to point into the newly allocated memory. */
2762 if (i >= 0 && i <= ccline.cmdlen)
2763 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2764 }
2765
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 return OK;
2767}
2768
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002769#if defined(FEAT_ARABIC) || defined(PROTO)
2770static char_u *arshape_buf = NULL;
2771
2772# if defined(EXITFREE) || defined(PROTO)
2773 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002774free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002775{
2776 vim_free(arshape_buf);
2777}
2778# endif
2779#endif
2780
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781/*
2782 * Draw part of the cmdline at the current cursor position. But draw stars
2783 * when cmdline_star is TRUE.
2784 */
2785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002786draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787{
2788#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2789 int i;
2790
2791 if (cmdline_star > 0)
2792 for (i = 0; i < len; ++i)
2793 {
2794 msg_putchar('*');
2795# ifdef FEAT_MBYTE
2796 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002797 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798# endif
2799 }
2800 else
2801#endif
2802#ifdef FEAT_ARABIC
2803 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 static int buflen = 0;
2806 char_u *p;
2807 int j;
2808 int newlen = 0;
2809 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002810 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 int prev_c = 0;
2812 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002813 int u8c;
2814 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
2817 /*
2818 * Do arabic shaping into a temporary buffer. This is very
2819 * inefficient!
2820 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002821 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 {
2823 /* Re-allocate the buffer. We keep it around to avoid a lot of
2824 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002825 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002826 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002827 arshape_buf = alloc(buflen);
2828 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 return; /* out of memory */
2830 }
2831
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002832 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2833 {
2834 /* Prepend a space to draw the leading composing char on. */
2835 arshape_buf[0] = ' ';
2836 newlen = 1;
2837 }
2838
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 for (j = start; j < start + len; j += mb_l)
2840 {
2841 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002842 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002843 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 if (ARABIC_CHAR(u8c))
2845 {
2846 /* Do Arabic shaping. */
2847 if (cmdmsg_rl)
2848 {
2849 /* displaying from right to left */
2850 pc = prev_c;
2851 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002852 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 if (j + mb_l >= start + len)
2854 nc = NUL;
2855 else
2856 nc = utf_ptr2char(p + mb_l);
2857 }
2858 else
2859 {
2860 /* displaying from left to right */
2861 if (j + mb_l >= start + len)
2862 pc = NUL;
2863 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002864 {
2865 int pcc[MAX_MCO];
2866
2867 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002869 pc1 = pcc[0];
2870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 nc = prev_c;
2872 }
2873 prev_c = u8c;
2874
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002875 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002877 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002878 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002880 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2881 if (u8cc[1] != 0)
2882 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002883 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885 }
2886 else
2887 {
2888 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002889 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 newlen += mb_l;
2891 }
2892 }
2893
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002894 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 }
2896 else
2897#endif
2898 msg_outtrans_len(ccline.cmdbuff + start, len);
2899}
2900
2901/*
2902 * Put a character on the command line. Shifts the following text to the
2903 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2904 * "c" must be printable (fit in one display cell)!
2905 */
2906 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002907putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908{
2909 if (cmd_silent)
2910 return;
2911 msg_no_more = TRUE;
2912 msg_putchar(c);
2913 if (shift)
2914 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2915 msg_no_more = FALSE;
2916 cursorcmd();
2917}
2918
2919/*
2920 * Undo a putcmdline(c, FALSE).
2921 */
2922 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002923unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924{
2925 if (cmd_silent)
2926 return;
2927 msg_no_more = TRUE;
2928 if (ccline.cmdlen == ccline.cmdpos)
2929 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002930#ifdef FEAT_MBYTE
2931 else if (has_mbyte)
2932 draw_cmdline(ccline.cmdpos,
2933 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 else
2936 draw_cmdline(ccline.cmdpos, 1);
2937 msg_no_more = FALSE;
2938 cursorcmd();
2939}
2940
2941/*
2942 * Put the given string, of the given length, onto the command line.
2943 * If len is -1, then STRLEN() is used to calculate the length.
2944 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2945 * part will be redrawn, otherwise it will not. If this function is called
2946 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2947 * called afterwards.
2948 */
2949 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002950put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
2952 int retval;
2953 int i;
2954 int m;
2955 int c;
2956
2957 if (len < 0)
2958 len = (int)STRLEN(str);
2959
2960 /* Check if ccline.cmdbuff needs to be longer */
2961 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002962 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 else
2964 retval = OK;
2965 if (retval == OK)
2966 {
2967 if (!ccline.overstrike)
2968 {
2969 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2970 ccline.cmdbuff + ccline.cmdpos,
2971 (size_t)(ccline.cmdlen - ccline.cmdpos));
2972 ccline.cmdlen += len;
2973 }
2974 else
2975 {
2976#ifdef FEAT_MBYTE
2977 if (has_mbyte)
2978 {
2979 /* Count nr of characters in the new string. */
2980 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002981 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 ++m;
2983 /* Count nr of bytes in cmdline that are overwritten by these
2984 * characters. */
2985 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002986 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 --m;
2988 if (i < ccline.cmdlen)
2989 {
2990 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2991 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2992 ccline.cmdlen += ccline.cmdpos + len - i;
2993 }
2994 else
2995 ccline.cmdlen = ccline.cmdpos + len;
2996 }
2997 else
2998#endif
2999 if (ccline.cmdpos + len > ccline.cmdlen)
3000 ccline.cmdlen = ccline.cmdpos + len;
3001 }
3002 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3003 ccline.cmdbuff[ccline.cmdlen] = NUL;
3004
3005#ifdef FEAT_MBYTE
3006 if (enc_utf8)
3007 {
3008 /* When the inserted text starts with a composing character,
3009 * backup to the character before it. There could be two of them.
3010 */
3011 i = 0;
3012 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3013 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3014 {
3015 i = (*mb_head_off)(ccline.cmdbuff,
3016 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3017 ccline.cmdpos -= i;
3018 len += i;
3019 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3020 }
3021# ifdef FEAT_ARABIC
3022 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3023 {
3024 /* Check the previous character for Arabic combining pair. */
3025 i = (*mb_head_off)(ccline.cmdbuff,
3026 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3027 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3028 + ccline.cmdpos - i), c))
3029 {
3030 ccline.cmdpos -= i;
3031 len += i;
3032 }
3033 else
3034 i = 0;
3035 }
3036# endif
3037 if (i != 0)
3038 {
3039 /* Also backup the cursor position. */
3040 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3041 ccline.cmdspos -= i;
3042 msg_col -= i;
3043 if (msg_col < 0)
3044 {
3045 msg_col += Columns;
3046 --msg_row;
3047 }
3048 }
3049 }
3050#endif
3051
3052 if (redraw && !cmd_silent)
3053 {
3054 msg_no_more = TRUE;
3055 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003056 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3058 /* Avoid clearing the rest of the line too often. */
3059 if (cmdline_row != i || ccline.overstrike)
3060 msg_clr_eos();
3061 msg_no_more = FALSE;
3062 }
3063#ifdef FEAT_FKMAP
3064 /*
3065 * If we are in Farsi command mode, the character input must be in
3066 * Insert mode. So do not advance the cmdpos.
3067 */
3068 if (!cmd_fkmap)
3069#endif
3070 {
3071 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003074 if (m < 0) /* overflow, Columns or Rows at weird value */
3075 m = MAXCOL;
3076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 else
3078 m = MAXCOL;
3079 for (i = 0; i < len; ++i)
3080 {
3081 c = cmdline_charsize(ccline.cmdpos);
3082#ifdef FEAT_MBYTE
3083 /* count ">" for a double-wide char that doesn't fit. */
3084 if (has_mbyte)
3085 correct_cmdspos(ccline.cmdpos, c);
3086#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003087 /* Stop cursor at the end of the screen, but do increment the
3088 * insert position, so that entering a very long command
3089 * works, even though you can't see it. */
3090 if (ccline.cmdspos + c < m)
3091 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092#ifdef FEAT_MBYTE
3093 if (has_mbyte)
3094 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003095 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 if (c > len - i - 1)
3097 c = len - i - 1;
3098 ccline.cmdpos += c;
3099 i += c;
3100 }
3101#endif
3102 ++ccline.cmdpos;
3103 }
3104 }
3105 }
3106 if (redraw)
3107 msg_check();
3108 return retval;
3109}
3110
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003111static struct cmdline_info prev_ccline;
3112static int prev_ccline_used = FALSE;
3113
3114/*
3115 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3116 * and overwrite it. But get_cmdline_str() may need it, thus make it
3117 * available globally in prev_ccline.
3118 */
3119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003120save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003121{
3122 if (!prev_ccline_used)
3123 {
3124 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3125 prev_ccline_used = TRUE;
3126 }
3127 *ccp = prev_ccline;
3128 prev_ccline = ccline;
3129 ccline.cmdbuff = NULL;
3130 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003131 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003132}
3133
3134/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003135 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003136 */
3137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003138restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003139{
3140 ccline = prev_ccline;
3141 prev_ccline = *ccp;
3142}
3143
Bram Moolenaar5a305422006-04-28 22:38:25 +00003144#if defined(FEAT_EVAL) || defined(PROTO)
3145/*
3146 * Save the command line into allocated memory. Returns a pointer to be
3147 * passed to restore_cmdline_alloc() later.
3148 * Returns NULL when failed.
3149 */
3150 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003151save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003152{
3153 struct cmdline_info *p;
3154
3155 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3156 if (p != NULL)
3157 save_cmdline(p);
3158 return (char_u *)p;
3159}
3160
3161/*
3162 * Restore the command line from the return value of save_cmdline_alloc().
3163 */
3164 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003165restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003166{
3167 if (p != NULL)
3168 {
3169 restore_cmdline((struct cmdline_info *)p);
3170 vim_free(p);
3171 }
3172}
3173#endif
3174
Bram Moolenaar8299df92004-07-10 09:47:34 +00003175/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003176 * Paste a yank register into the command line.
3177 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003178 * insert_reg() can't be used here, because special characters from the
3179 * register contents will be interpreted as commands.
3180 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003181 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003182 */
3183 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003184cmdline_paste(
3185 int regname,
3186 int literally, /* Insert text literally instead of "as typed" */
3187 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003188{
3189 long i;
3190 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003191 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003192 int allocated;
3193 struct cmdline_info save_ccline;
3194
3195 /* check for valid regname; also accept special characters for CTRL-R in
3196 * the command line */
3197 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3198 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3199 return FAIL;
3200
3201 /* A register containing CTRL-R can cause an endless loop. Allow using
3202 * CTRL-C to break the loop. */
3203 line_breakcheck();
3204 if (got_int)
3205 return FAIL;
3206
3207#ifdef FEAT_CLIPBOARD
3208 regname = may_get_selection(regname);
3209#endif
3210
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003211 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003212 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003213 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003214 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003215 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003216 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003217 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003218
3219 if (i)
3220 {
3221 /* Got the value of a special register in "arg". */
3222 if (arg == NULL)
3223 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003224
3225 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3226 * part of the word. */
3227 p = arg;
3228 if (p_is && regname == Ctrl_W)
3229 {
3230 char_u *w;
3231 int len;
3232
3233 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003234 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003235 {
3236#ifdef FEAT_MBYTE
3237 if (has_mbyte)
3238 {
3239 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3240 if (!vim_iswordc(mb_ptr2char(w - len)))
3241 break;
3242 w -= len;
3243 }
3244 else
3245#endif
3246 {
3247 if (!vim_iswordc(w[-1]))
3248 break;
3249 --w;
3250 }
3251 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003252 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003253 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3254 p += len;
3255 }
3256
3257 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003258 if (allocated)
3259 vim_free(arg);
3260 return OK;
3261 }
3262
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003263 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003264}
3265
3266/*
3267 * Put a string on the command line.
3268 * When "literally" is TRUE, insert literally.
3269 * When "literally" is FALSE, insert as typed, but don't leave the command
3270 * line.
3271 */
3272 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003273cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003274{
3275 int c, cv;
3276
3277 if (literally)
3278 put_on_cmdline(s, -1, TRUE);
3279 else
3280 while (*s != NUL)
3281 {
3282 cv = *s;
3283 if (cv == Ctrl_V && s[1])
3284 ++s;
3285#ifdef FEAT_MBYTE
3286 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003287 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003288 else
3289#endif
3290 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003291 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3292 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003293#ifdef UNIX
3294 || c == intr_char
3295#endif
3296 || (c == Ctrl_BSL && *s == Ctrl_N))
3297 stuffcharReadbuff(Ctrl_V);
3298 stuffcharReadbuff(c);
3299 }
3300}
3301
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302#ifdef FEAT_WILDMENU
3303/*
3304 * Delete characters on the command line, from "from" to the current
3305 * position.
3306 */
3307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003308cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3311 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3312 ccline.cmdlen -= ccline.cmdpos - from;
3313 ccline.cmdpos = from;
3314}
3315#endif
3316
3317/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003318 * This function is called when the screen size changes and with incremental
3319 * search and in other situations where the command line may have been
3320 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 */
3322 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003323redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
3325 if (cmd_silent)
3326 return;
3327 need_wait_return = FALSE;
3328 compute_cmdrow();
3329 redrawcmd();
3330 cursorcmd();
3331}
3332
3333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003334redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 int i;
3337
3338 if (cmd_silent)
3339 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003340 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 msg_putchar(ccline.cmdfirstc);
3342 if (ccline.cmdprompt != NULL)
3343 {
3344 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3345 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3346 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003347 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 --ccline.cmdindent;
3349 }
3350 else
3351 for (i = ccline.cmdindent; i > 0; --i)
3352 msg_putchar(' ');
3353}
3354
3355/*
3356 * Redraw what is currently on the command line.
3357 */
3358 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003359redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 if (cmd_silent)
3362 return;
3363
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003364 /* when 'incsearch' is set there may be no command line while redrawing */
3365 if (ccline.cmdbuff == NULL)
3366 {
3367 windgoto(cmdline_row, 0);
3368 msg_clr_eos();
3369 return;
3370 }
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 msg_start();
3373 redrawcmdprompt();
3374
3375 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3376 msg_no_more = TRUE;
3377 draw_cmdline(0, ccline.cmdlen);
3378 msg_clr_eos();
3379 msg_no_more = FALSE;
3380
3381 set_cmdspos_cursor();
3382
3383 /*
3384 * An emsg() before may have set msg_scroll. This is used in normal mode,
3385 * in cmdline mode we can reset them now.
3386 */
3387 msg_scroll = FALSE; /* next message overwrites cmdline */
3388
3389 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3390 * in cmdline mode */
3391 skip_redraw = FALSE;
3392}
3393
3394 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003395compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003397 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 cmdline_row = Rows - 1;
3399 else
3400 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3401 + W_STATUS_HEIGHT(lastwin);
3402}
3403
3404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003405cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406{
3407 if (cmd_silent)
3408 return;
3409
3410#ifdef FEAT_RIGHTLEFT
3411 if (cmdmsg_rl)
3412 {
3413 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3414 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3415 if (msg_row <= 0)
3416 msg_row = Rows - 1;
3417 }
3418 else
3419#endif
3420 {
3421 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3422 msg_col = ccline.cmdspos % (int)Columns;
3423 if (msg_row >= Rows)
3424 msg_row = Rows - 1;
3425 }
3426
3427 windgoto(msg_row, msg_col);
3428#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3429 redrawcmd_preedit();
3430#endif
3431#ifdef MCH_CURSOR_SHAPE
3432 mch_update_cursor();
3433#endif
3434}
3435
3436 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003437gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
3439 msg_start();
3440#ifdef FEAT_RIGHTLEFT
3441 if (cmdmsg_rl)
3442 msg_col = Columns - 1;
3443 else
3444#endif
3445 msg_col = 0; /* always start in column 0 */
3446 if (clr) /* clear the bottom line(s) */
3447 msg_clr_eos(); /* will reset clear_cmdline */
3448 windgoto(cmdline_row, 0);
3449}
3450
3451/*
3452 * Check the word in front of the cursor for an abbreviation.
3453 * Called when the non-id character "c" has been entered.
3454 * When an abbreviation is recognized it is removed from the text with
3455 * backspaces and the replacement string is inserted, followed by "c".
3456 */
3457 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003458ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
3460 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3461 return FALSE;
3462
3463 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3464}
3465
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003466#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3467 static int
3468#ifdef __BORLANDC__
3469_RTLENTRYF
3470#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003471sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003472{
3473 char_u *p1 = *(char_u **)s1;
3474 char_u *p2 = *(char_u **)s2;
3475
3476 if (*p1 != '<' && *p2 == '<') return -1;
3477 if (*p1 == '<' && *p2 != '<') return 1;
3478 return STRCMP(p1, p2);
3479}
3480#endif
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482/*
3483 * Return FAIL if this is not an appropriate context in which to do
3484 * completion of anything, return OK if it is (even if there are no matches).
3485 * For the caller, this means that the character is just passed through like a
3486 * normal character (instead of being expanded). This allows :s/^I^D etc.
3487 */
3488 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003489nextwild(
3490 expand_T *xp,
3491 int type,
3492 int options, /* extra options for ExpandOne() */
3493 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 int i, j;
3496 char_u *p1;
3497 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 int difflen;
3499 int v;
3500
3501 if (xp->xp_numfiles == -1)
3502 {
3503 set_expand_context(xp);
3504 cmd_showtail = expand_showtail(xp);
3505 }
3506
3507 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3508 {
3509 beep_flush();
3510 return OK; /* Something illegal on command line */
3511 }
3512 if (xp->xp_context == EXPAND_NOTHING)
3513 {
3514 /* Caller can use the character as a normal char instead */
3515 return FAIL;
3516 }
3517
3518 MSG_PUTS("..."); /* show that we are busy */
3519 out_flush();
3520
3521 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003522 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
3524 if (type == WILD_NEXT || type == WILD_PREV)
3525 {
3526 /*
3527 * Get next/previous match for a previous expanded pattern.
3528 */
3529 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3530 }
3531 else
3532 {
3533 /*
3534 * Translate string into pattern and expand it.
3535 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003536 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3537 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 p2 = NULL;
3539 else
3540 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003541 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003542 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3543 if (escape)
3544 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003545
3546 if (p_wic)
3547 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003548 p2 = ExpandOne(xp, p1,
3549 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003550 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003552 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (p2 != NULL && type == WILD_LONGEST)
3554 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003555 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (ccline.cmdbuff[i + j] == '*'
3557 || ccline.cmdbuff[i + j] == '?')
3558 break;
3559 if ((int)STRLEN(p2) < j)
3560 {
3561 vim_free(p2);
3562 p2 = NULL;
3563 }
3564 }
3565 }
3566 }
3567
3568 if (p2 != NULL && !got_int)
3569 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003570 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003571 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003573 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 xp->xp_pattern = ccline.cmdbuff + i;
3575 }
3576 else
3577 v = OK;
3578 if (v == OK)
3579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003580 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3581 &ccline.cmdbuff[ccline.cmdpos],
3582 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3583 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 ccline.cmdlen += difflen;
3585 ccline.cmdpos += difflen;
3586 }
3587 }
3588 vim_free(p2);
3589
3590 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003591 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
3593 /* When expanding a ":map" command and no matches are found, assume that
3594 * the key is supposed to be inserted literally */
3595 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3596 return FAIL;
3597
3598 if (xp->xp_numfiles <= 0 && p2 == NULL)
3599 beep_flush();
3600 else if (xp->xp_numfiles == 1)
3601 /* free expanded pattern */
3602 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3603
3604 return OK;
3605}
3606
3607/*
3608 * Do wildcard expansion on the string 'str'.
3609 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003610 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 * Return NULL for failure.
3612 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003613 * "orig" is the originally expanded string, copied to allocated memory. It
3614 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3615 * WILD_PREV "orig" should be NULL.
3616 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003617 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3618 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 *
3620 * mode = WILD_FREE: just free previously expanded matches
3621 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3622 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3623 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3624 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3625 * mode = WILD_ALL: return all matches concatenated
3626 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003627 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 *
3629 * options = WILD_LIST_NOTFOUND: list entries without a match
3630 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3631 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3632 * options = WILD_NO_BEEP: Don't beep for multiple matches
3633 * options = WILD_ADD_SLASH: add a slash after directory names
3634 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3635 * options = WILD_SILENT: don't print warning messages
3636 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003637 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 *
3639 * The variables xp->xp_context and xp->xp_backslash must have been set!
3640 */
3641 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003642ExpandOne(
3643 expand_T *xp,
3644 char_u *str,
3645 char_u *orig, /* allocated copy of original of expanded string */
3646 int options,
3647 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648{
3649 char_u *ss = NULL;
3650 static int findex;
3651 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003652 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 int i;
3654 long_u len;
3655 int non_suf_match; /* number without matching suffix */
3656
3657 /*
3658 * first handle the case of using an old match
3659 */
3660 if (mode == WILD_NEXT || mode == WILD_PREV)
3661 {
3662 if (xp->xp_numfiles > 0)
3663 {
3664 if (mode == WILD_PREV)
3665 {
3666 if (findex == -1)
3667 findex = xp->xp_numfiles;
3668 --findex;
3669 }
3670 else /* mode == WILD_NEXT */
3671 ++findex;
3672
3673 /*
3674 * When wrapping around, return the original string, set findex to
3675 * -1.
3676 */
3677 if (findex < 0)
3678 {
3679 if (orig_save == NULL)
3680 findex = xp->xp_numfiles - 1;
3681 else
3682 findex = -1;
3683 }
3684 if (findex >= xp->xp_numfiles)
3685 {
3686 if (orig_save == NULL)
3687 findex = 0;
3688 else
3689 findex = -1;
3690 }
3691#ifdef FEAT_WILDMENU
3692 if (p_wmnu)
3693 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3694 findex, cmd_showtail);
3695#endif
3696 if (findex == -1)
3697 return vim_strsave(orig_save);
3698 return vim_strsave(xp->xp_files[findex]);
3699 }
3700 else
3701 return NULL;
3702 }
3703
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003704 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3706 {
3707 FreeWild(xp->xp_numfiles, xp->xp_files);
3708 xp->xp_numfiles = -1;
3709 vim_free(orig_save);
3710 orig_save = NULL;
3711 }
3712 findex = 0;
3713
3714 if (mode == WILD_FREE) /* only release file name */
3715 return NULL;
3716
3717 if (xp->xp_numfiles == -1)
3718 {
3719 vim_free(orig_save);
3720 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003721 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722
3723 /*
3724 * Do the expansion.
3725 */
3726 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3727 options) == FAIL)
3728 {
3729#ifdef FNAME_ILLEGAL
3730 /* Illegal file name has been silently skipped. But when there
3731 * are wildcards, the real problem is that there was no match,
3732 * causing the pattern to be added, which has illegal characters.
3733 */
3734 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3735 EMSG2(_(e_nomatch2), str);
3736#endif
3737 }
3738 else if (xp->xp_numfiles == 0)
3739 {
3740 if (!(options & WILD_SILENT))
3741 EMSG2(_(e_nomatch2), str);
3742 }
3743 else
3744 {
3745 /* Escape the matches for use on the command line. */
3746 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3747
3748 /*
3749 * Check for matching suffixes in file names.
3750 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003751 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3752 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 {
3754 if (xp->xp_numfiles)
3755 non_suf_match = xp->xp_numfiles;
3756 else
3757 non_suf_match = 1;
3758 if ((xp->xp_context == EXPAND_FILES
3759 || xp->xp_context == EXPAND_DIRECTORIES)
3760 && xp->xp_numfiles > 1)
3761 {
3762 /*
3763 * More than one match; check suffix.
3764 * The files will have been sorted on matching suffix in
3765 * expand_wildcards, only need to check the first two.
3766 */
3767 non_suf_match = 0;
3768 for (i = 0; i < 2; ++i)
3769 if (match_suffix(xp->xp_files[i]))
3770 ++non_suf_match;
3771 }
3772 if (non_suf_match != 1)
3773 {
3774 /* Can we ever get here unless it's while expanding
3775 * interactively? If not, we can get rid of this all
3776 * together. Don't really want to wait for this message
3777 * (and possibly have to hit return to continue!).
3778 */
3779 if (!(options & WILD_SILENT))
3780 EMSG(_(e_toomany));
3781 else if (!(options & WILD_NO_BEEP))
3782 beep_flush();
3783 }
3784 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3785 ss = vim_strsave(xp->xp_files[0]);
3786 }
3787 }
3788 }
3789
3790 /* Find longest common part */
3791 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3792 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003793 int mb_len = 1;
3794 int c0, ci;
3795
3796 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003798#ifdef FEAT_MBYTE
3799 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003801 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3802 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3803 }
3804 else
3805#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003806 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003807 for (i = 1; i < xp->xp_numfiles; ++i)
3808 {
3809#ifdef FEAT_MBYTE
3810 if (has_mbyte)
3811 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3812 else
3813#endif
3814 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003815 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003817 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003818 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003820 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 break;
3822 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003823 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 break;
3825 }
3826 if (i < xp->xp_numfiles)
3827 {
3828 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003829 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 break;
3831 }
3832 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 ss = alloc((unsigned)len + 1);
3835 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003836 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 findex = -1; /* next p_wc gets first one */
3838 }
3839
3840 /* Concatenate all matching names */
3841 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3842 {
3843 len = 0;
3844 for (i = 0; i < xp->xp_numfiles; ++i)
3845 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3846 ss = lalloc(len, TRUE);
3847 if (ss != NULL)
3848 {
3849 *ss = NUL;
3850 for (i = 0; i < xp->xp_numfiles; ++i)
3851 {
3852 STRCAT(ss, xp->xp_files[i]);
3853 if (i != xp->xp_numfiles - 1)
3854 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3855 }
3856 }
3857 }
3858
3859 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3860 ExpandCleanup(xp);
3861
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003862 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003863 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003864 vim_free(orig);
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 return ss;
3867}
3868
3869/*
3870 * Prepare an expand structure for use.
3871 */
3872 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003873ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003875 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003876 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003878#ifndef BACKSLASH_IN_FILENAME
3879 xp->xp_shell = FALSE;
3880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 xp->xp_numfiles = -1;
3882 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003883#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3884 xp->xp_arg = NULL;
3885#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003886 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887}
3888
3889/*
3890 * Cleanup an expand structure after use.
3891 */
3892 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003893ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
3895 if (xp->xp_numfiles >= 0)
3896 {
3897 FreeWild(xp->xp_numfiles, xp->xp_files);
3898 xp->xp_numfiles = -1;
3899 }
3900}
3901
3902 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003903ExpandEscape(
3904 expand_T *xp,
3905 char_u *str,
3906 int numfiles,
3907 char_u **files,
3908 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909{
3910 int i;
3911 char_u *p;
3912
3913 /*
3914 * May change home directory back to "~"
3915 */
3916 if (options & WILD_HOME_REPLACE)
3917 tilde_replace(str, numfiles, files);
3918
3919 if (options & WILD_ESCAPE)
3920 {
3921 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003922 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003923 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 || xp->xp_context == EXPAND_BUFFERS
3925 || xp->xp_context == EXPAND_DIRECTORIES)
3926 {
3927 /*
3928 * Insert a backslash into a file name before a space, \, %, #
3929 * and wildmatch characters, except '~'.
3930 */
3931 for (i = 0; i < numfiles; ++i)
3932 {
3933 /* for ":set path=" we need to escape spaces twice */
3934 if (xp->xp_backslash == XP_BS_THREE)
3935 {
3936 p = vim_strsave_escaped(files[i], (char_u *)" ");
3937 if (p != NULL)
3938 {
3939 vim_free(files[i]);
3940 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003941#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 p = vim_strsave_escaped(files[i], (char_u *)" ");
3943 if (p != NULL)
3944 {
3945 vim_free(files[i]);
3946 files[i] = p;
3947 }
3948#endif
3949 }
3950 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003951#ifdef BACKSLASH_IN_FILENAME
3952 p = vim_strsave_fnameescape(files[i], FALSE);
3953#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003954 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 if (p != NULL)
3957 {
3958 vim_free(files[i]);
3959 files[i] = p;
3960 }
3961
3962 /* If 'str' starts with "\~", replace "~" at start of
3963 * files[i] with "\~". */
3964 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003965 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003968
3969 /* If the first file starts with a '+' escape it. Otherwise it
3970 * could be seen as "+cmd". */
3971 if (*files[0] == '+')
3972 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974 else if (xp->xp_context == EXPAND_TAGS)
3975 {
3976 /*
3977 * Insert a backslash before characters in a tag name that
3978 * would terminate the ":tag" command.
3979 */
3980 for (i = 0; i < numfiles; ++i)
3981 {
3982 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3983 if (p != NULL)
3984 {
3985 vim_free(files[i]);
3986 files[i] = p;
3987 }
3988 }
3989 }
3990 }
3991}
3992
3993/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003994 * Escape special characters in "fname" for when used as a file name argument
3995 * after a Vim command, or, when "shell" is non-zero, a shell command.
3996 * Returns the result in allocated memory.
3997 */
3998 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003999vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004000{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004001 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004002#ifdef BACKSLASH_IN_FILENAME
4003 char_u buf[20];
4004 int j = 0;
4005
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004006 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004007 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004008 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004009 buf[j++] = *p;
4010 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004011 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004012#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004013 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4014 if (shell && csh_like_shell() && p != NULL)
4015 {
4016 char_u *s;
4017
4018 /* For csh and similar shells need to put two backslashes before '!'.
4019 * One is taken by Vim, one by the shell. */
4020 s = vim_strsave_escaped(p, (char_u *)"!");
4021 vim_free(p);
4022 p = s;
4023 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004024#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004025
4026 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4027 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004028 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004029 escape_fname(&p);
4030
4031 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004032}
4033
4034/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004035 * Put a backslash before the file name in "pp", which is in allocated memory.
4036 */
4037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004038escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004039{
4040 char_u *p;
4041
4042 p = alloc((unsigned)(STRLEN(*pp) + 2));
4043 if (p != NULL)
4044 {
4045 p[0] = '\\';
4046 STRCPY(p + 1, *pp);
4047 vim_free(*pp);
4048 *pp = p;
4049 }
4050}
4051
4052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 * For each file name in files[num_files]:
4054 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4055 */
4056 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004057tilde_replace(
4058 char_u *orig_pat,
4059 int num_files,
4060 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061{
4062 int i;
4063 char_u *p;
4064
4065 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4066 {
4067 for (i = 0; i < num_files; ++i)
4068 {
4069 p = home_replace_save(NULL, files[i]);
4070 if (p != NULL)
4071 {
4072 vim_free(files[i]);
4073 files[i] = p;
4074 }
4075 }
4076 }
4077}
4078
4079/*
4080 * Show all matches for completion on the command line.
4081 * Returns EXPAND_NOTHING when the character that triggered expansion should
4082 * be inserted like a normal character.
4083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004085showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086{
4087#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4088 int num_files;
4089 char_u **files_found;
4090 int i, j, k;
4091 int maxlen;
4092 int lines;
4093 int columns;
4094 char_u *p;
4095 int lastlen;
4096 int attr;
4097 int showtail;
4098
4099 if (xp->xp_numfiles == -1)
4100 {
4101 set_expand_context(xp);
4102 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4103 &num_files, &files_found);
4104 showtail = expand_showtail(xp);
4105 if (i != EXPAND_OK)
4106 return i;
4107
4108 }
4109 else
4110 {
4111 num_files = xp->xp_numfiles;
4112 files_found = xp->xp_files;
4113 showtail = cmd_showtail;
4114 }
4115
4116#ifdef FEAT_WILDMENU
4117 if (!wildmenu)
4118 {
4119#endif
4120 msg_didany = FALSE; /* lines_left will be set */
4121 msg_start(); /* prepare for paging */
4122 msg_putchar('\n');
4123 out_flush();
4124 cmdline_row = msg_row;
4125 msg_didany = FALSE; /* lines_left will be set again */
4126 msg_start(); /* prepare for paging */
4127#ifdef FEAT_WILDMENU
4128 }
4129#endif
4130
4131 if (got_int)
4132 got_int = FALSE; /* only int. the completion, not the cmd line */
4133#ifdef FEAT_WILDMENU
4134 else if (wildmenu)
4135 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
4136#endif
4137 else
4138 {
4139 /* find the length of the longest file name */
4140 maxlen = 0;
4141 for (i = 0; i < num_files; ++i)
4142 {
4143 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004144 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 || xp->xp_context == EXPAND_BUFFERS))
4146 {
4147 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4148 j = vim_strsize(NameBuff);
4149 }
4150 else
4151 j = vim_strsize(L_SHOWFILE(i));
4152 if (j > maxlen)
4153 maxlen = j;
4154 }
4155
4156 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4157 lines = num_files;
4158 else
4159 {
4160 /* compute the number of columns and lines for the listing */
4161 maxlen += 2; /* two spaces between file names */
4162 columns = ((int)Columns + 2) / maxlen;
4163 if (columns < 1)
4164 columns = 1;
4165 lines = (num_files + columns - 1) / columns;
4166 }
4167
4168 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4169
4170 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4171 {
4172 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4173 msg_clr_eos();
4174 msg_advance(maxlen - 3);
4175 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4176 }
4177
4178 /* list the files line by line */
4179 for (i = 0; i < lines; ++i)
4180 {
4181 lastlen = 999;
4182 for (k = i; k < num_files; k += lines)
4183 {
4184 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4185 {
4186 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4187 p = files_found[k] + STRLEN(files_found[k]) + 1;
4188 msg_advance(maxlen + 1);
4189 msg_puts(p);
4190 msg_advance(maxlen + 3);
4191 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4192 break;
4193 }
4194 for (j = maxlen - lastlen; --j >= 0; )
4195 msg_putchar(' ');
4196 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004197 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 || xp->xp_context == EXPAND_BUFFERS)
4199 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004200 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004201 if (xp->xp_numfiles != -1)
4202 {
4203 char_u *halved_slash;
4204 char_u *exp_path;
4205
4206 /* Expansion was done before and special characters
4207 * were escaped, need to halve backslashes. Also
4208 * $HOME has been replaced with ~/. */
4209 exp_path = expand_env_save_opt(files_found[k], TRUE);
4210 halved_slash = backslash_halve_save(
4211 exp_path != NULL ? exp_path : files_found[k]);
4212 j = mch_isdir(halved_slash != NULL ? halved_slash
4213 : files_found[k]);
4214 vim_free(exp_path);
4215 vim_free(halved_slash);
4216 }
4217 else
4218 /* Expansion was done here, file names are literal. */
4219 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 if (showtail)
4221 p = L_SHOWFILE(k);
4222 else
4223 {
4224 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4225 TRUE);
4226 p = NameBuff;
4227 }
4228 }
4229 else
4230 {
4231 j = FALSE;
4232 p = L_SHOWFILE(k);
4233 }
4234 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4235 }
4236 if (msg_col > 0) /* when not wrapped around */
4237 {
4238 msg_clr_eos();
4239 msg_putchar('\n');
4240 }
4241 out_flush(); /* show one line at a time */
4242 if (got_int)
4243 {
4244 got_int = FALSE;
4245 break;
4246 }
4247 }
4248
4249 /*
4250 * we redraw the command below the lines that we have just listed
4251 * This is a bit tricky, but it saves a lot of screen updating.
4252 */
4253 cmdline_row = msg_row; /* will put it back later */
4254 }
4255
4256 if (xp->xp_numfiles == -1)
4257 FreeWild(num_files, files_found);
4258
4259 return EXPAND_OK;
4260}
4261
4262/*
4263 * Private gettail for showmatches() (and win_redr_status_matches()):
4264 * Find tail of file name path, but ignore trailing "/".
4265 */
4266 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004267sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268{
4269 char_u *p;
4270 char_u *t = s;
4271 int had_sep = FALSE;
4272
4273 for (p = s; *p != NUL; )
4274 {
4275 if (vim_ispathsep(*p)
4276#ifdef BACKSLASH_IN_FILENAME
4277 && !rem_backslash(p)
4278#endif
4279 )
4280 had_sep = TRUE;
4281 else if (had_sep)
4282 {
4283 t = p;
4284 had_sep = FALSE;
4285 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004286 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288 return t;
4289}
4290
4291/*
4292 * Return TRUE if we only need to show the tail of completion matches.
4293 * When not completing file names or there is a wildcard in the path FALSE is
4294 * returned.
4295 */
4296 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004297expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298{
4299 char_u *s;
4300 char_u *end;
4301
4302 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004303 if (xp->xp_context != EXPAND_FILES
4304 && xp->xp_context != EXPAND_SHELLCMD
4305 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 return FALSE;
4307
4308 end = gettail(xp->xp_pattern);
4309 if (end == xp->xp_pattern) /* there is no path separator */
4310 return FALSE;
4311
4312 for (s = xp->xp_pattern; s < end; s++)
4313 {
4314 /* Skip escaped wildcards. Only when the backslash is not a path
4315 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4316 if (rem_backslash(s))
4317 ++s;
4318 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4319 return FALSE;
4320 }
4321 return TRUE;
4322}
4323
4324/*
4325 * Prepare a string for expansion.
4326 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004327 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 * When expanding other names: The string will be used with regcomp(). Copy
4329 * the name into allocated memory and prepend "^".
4330 */
4331 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004332addstar(
4333 char_u *fname,
4334 int len,
4335 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
4337 char_u *retval;
4338 int i, j;
4339 int new_len;
4340 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004341 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004343 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004344 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004345 && context != EXPAND_SHELLCMD
4346 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
4348 /*
4349 * Matching will be done internally (on something other than files).
4350 * So we convert the file-matching-type wildcards into our kind for
4351 * use with vim_regcomp(). First work out how long it will be:
4352 */
4353
4354 /* For help tags the translation is done in find_help_tags().
4355 * For a tag pattern starting with "/" no translation is needed. */
4356 if (context == EXPAND_HELP
4357 || context == EXPAND_COLORS
4358 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004359 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004360 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004361 || context == EXPAND_PACKADD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 || (context == EXPAND_TAGS && fname[0] == '/'))
4363 retval = vim_strnsave(fname, len);
4364 else
4365 {
4366 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4367 for (i = 0; i < len; i++)
4368 {
4369 if (fname[i] == '*' || fname[i] == '~')
4370 new_len++; /* '*' needs to be replaced by ".*"
4371 '~' needs to be replaced by "\~" */
4372
4373 /* Buffer names are like file names. "." should be literal */
4374 if (context == EXPAND_BUFFERS && fname[i] == '.')
4375 new_len++; /* "." becomes "\." */
4376
4377 /* Custom expansion takes care of special things, match
4378 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004379 if ((context == EXPAND_USER_DEFINED
4380 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 new_len++; /* '\' becomes "\\" */
4382 }
4383 retval = alloc(new_len);
4384 if (retval != NULL)
4385 {
4386 retval[0] = '^';
4387 j = 1;
4388 for (i = 0; i < len; i++, j++)
4389 {
4390 /* Skip backslash. But why? At least keep it for custom
4391 * expansion. */
4392 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004393 && context != EXPAND_USER_LIST
4394 && fname[i] == '\\'
4395 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 break;
4397
4398 switch (fname[i])
4399 {
4400 case '*': retval[j++] = '.';
4401 break;
4402 case '~': retval[j++] = '\\';
4403 break;
4404 case '?': retval[j] = '.';
4405 continue;
4406 case '.': if (context == EXPAND_BUFFERS)
4407 retval[j++] = '\\';
4408 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004409 case '\\': if (context == EXPAND_USER_DEFINED
4410 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 retval[j++] = '\\';
4412 break;
4413 }
4414 retval[j] = fname[i];
4415 }
4416 retval[j] = NUL;
4417 }
4418 }
4419 }
4420 else
4421 {
4422 retval = alloc(len + 4);
4423 if (retval != NULL)
4424 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004425 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004428 * Don't add a star to *, ~, ~user, $var or `cmd`.
4429 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 * ~ would be at the start of the file name, but not the tail.
4431 * $ could be anywhere in the tail.
4432 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004433 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 */
4435 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004436 ends_in_star = (len > 0 && retval[len - 1] == '*');
4437#ifndef BACKSLASH_IN_FILENAME
4438 for (i = len - 2; i >= 0; --i)
4439 {
4440 if (retval[i] != '\\')
4441 break;
4442 ends_in_star = !ends_in_star;
4443 }
4444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004446 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 && vim_strchr(tail, '$') == NULL
4448 && vim_strchr(retval, '`') == NULL)
4449 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004450 else if (len > 0 && retval[len - 1] == '$')
4451 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 retval[len] = NUL;
4453 }
4454 }
4455 return retval;
4456}
4457
4458/*
4459 * Must parse the command line so far to work out what context we are in.
4460 * Completion can then be done based on that context.
4461 * This routine sets the variables:
4462 * xp->xp_pattern The start of the pattern to be expanded within
4463 * the command line (ends at the cursor).
4464 * xp->xp_context The type of thing to expand. Will be one of:
4465 *
4466 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4467 * the command line, like an unknown command. Caller
4468 * should beep.
4469 * EXPAND_NOTHING Unrecognised context for completion, use char like
4470 * a normal char, rather than for completion. eg
4471 * :s/^I/
4472 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4473 * it.
4474 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4475 * EXPAND_FILES After command with XFILE set, or after setting
4476 * with P_EXPAND set. eg :e ^I, :w>>^I
4477 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4478 * when we know only directories are of interest. eg
4479 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004480 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4482 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4483 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4484 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4485 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4486 * EXPAND_EVENTS Complete event names
4487 * EXPAND_SYNTAX Complete :syntax command arguments
4488 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4489 * EXPAND_AUGROUP Complete autocommand group names
4490 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4491 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4492 * eg :unmap a^I , :cunab x^I
4493 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4494 * eg :call sub^I
4495 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4496 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4497 * names in expressions, eg :while s^I
4498 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004499 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 */
4501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004502set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004504 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 if (ccline.cmdfirstc != ':'
4506#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004507 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004508 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509#endif
4510 )
4511 {
4512 xp->xp_context = EXPAND_NOTHING;
4513 return;
4514 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004515 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516}
4517
4518 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004519set_cmd_context(
4520 expand_T *xp,
4521 char_u *str, /* start of command line */
4522 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004523 int col, /* position of cursor */
4524 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
4526 int old_char = NUL;
4527 char_u *nextcomm;
4528
4529 /*
4530 * Avoid a UMR warning from Purify, only save the character if it has been
4531 * written before.
4532 */
4533 if (col < len)
4534 old_char = str[col];
4535 str[col] = NUL;
4536 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004537
4538#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004539 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004540 {
4541# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004542 /* pass CMD_SIZE because there is no real command */
4543 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004544# endif
4545 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004546 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004547 {
4548 xp->xp_context = ccline.xp_context;
4549 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004550# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004551 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004552# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004553 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004554 else
4555#endif
4556 while (nextcomm != NULL)
4557 nextcomm = set_one_cmd_context(xp, nextcomm);
4558
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004559 /* Store the string here so that call_user_expand_func() can get to them
4560 * easily. */
4561 xp->xp_line = str;
4562 xp->xp_col = col;
4563
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 str[col] = old_char;
4565}
4566
4567/*
4568 * Expand the command line "str" from context "xp".
4569 * "xp" must have been set by set_cmd_context().
4570 * xp->xp_pattern points into "str", to where the text that is to be expanded
4571 * starts.
4572 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4573 * cursor.
4574 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4575 * key that triggered expansion literally.
4576 * Returns EXPAND_OK otherwise.
4577 */
4578 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004579expand_cmdline(
4580 expand_T *xp,
4581 char_u *str, /* start of command line */
4582 int col, /* position of cursor */
4583 int *matchcount, /* return: nr of matches */
4584 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585{
4586 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004587 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
4589 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4590 {
4591 beep_flush();
4592 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4593 }
4594 if (xp->xp_context == EXPAND_NOTHING)
4595 {
4596 /* Caller can use the character as a normal char instead */
4597 return EXPAND_NOTHING;
4598 }
4599
4600 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004601 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4602 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (file_str == NULL)
4604 return EXPAND_UNSUCCESSFUL;
4605
Bram Moolenaar94950a92010-12-02 16:01:29 +01004606 if (p_wic)
4607 options += WILD_ICASE;
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004610 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
4612 *matchcount = 0;
4613 *matches = NULL;
4614 }
4615 vim_free(file_str);
4616
4617 return EXPAND_OK;
4618}
4619
4620#ifdef FEAT_MULTI_LANG
4621/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004622 * Cleanup matches for help tags:
4623 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4624 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004626static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
4628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004629cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 int i, j;
4632 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004633 char_u buf[4];
4634 char_u *p = buf;
4635
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004636 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004637 {
4638 *p++ = '@';
4639 *p++ = p_hlg[0];
4640 *p++ = p_hlg[1];
4641 }
4642 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
4644 for (i = 0; i < num_file; ++i)
4645 {
4646 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004647 if (len <= 0)
4648 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004649 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
4651 /* Sorting on priority means the same item in another language may
4652 * be anywhere. Search all items for a match up to the "@en". */
4653 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004654 if (j != i && (int)STRLEN(file[j]) == len + 3
4655 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 break;
4657 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004658 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 file[i][len] = NUL;
4660 }
4661 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004662
4663 if (*buf != NUL)
4664 for (i = 0; i < num_file; ++i)
4665 {
4666 len = (int)STRLEN(file[i]) - 3;
4667 if (len <= 0)
4668 continue;
4669 if (STRCMP(file[i] + len, buf) == 0)
4670 {
4671 /* remove the default language */
4672 file[i][len] = NUL;
4673 }
4674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675}
4676#endif
4677
4678/*
4679 * Do the expansion based on xp->xp_context and "pat".
4680 */
4681 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004682ExpandFromContext(
4683 expand_T *xp,
4684 char_u *pat,
4685 int *num_file,
4686 char_u ***file,
4687 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
4689#ifdef FEAT_CMDL_COMPL
4690 regmatch_T regmatch;
4691#endif
4692 int ret;
4693 int flags;
4694
4695 flags = EW_DIR; /* include directories */
4696 if (options & WILD_LIST_NOTFOUND)
4697 flags |= EW_NOTFOUND;
4698 if (options & WILD_ADD_SLASH)
4699 flags |= EW_ADDSLASH;
4700 if (options & WILD_KEEP_ALL)
4701 flags |= EW_KEEPALL;
4702 if (options & WILD_SILENT)
4703 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004704 if (options & WILD_ALLLINKS)
4705 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004707 if (xp->xp_context == EXPAND_FILES
4708 || xp->xp_context == EXPAND_DIRECTORIES
4709 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711 /*
4712 * Expand file or directory names.
4713 */
4714 int free_pat = FALSE;
4715 int i;
4716
4717 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4718 * space */
4719 if (xp->xp_backslash != XP_BS_NONE)
4720 {
4721 free_pat = TRUE;
4722 pat = vim_strsave(pat);
4723 for (i = 0; pat[i]; ++i)
4724 if (pat[i] == '\\')
4725 {
4726 if (xp->xp_backslash == XP_BS_THREE
4727 && pat[i + 1] == '\\'
4728 && pat[i + 2] == '\\'
4729 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004730 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 if (xp->xp_backslash == XP_BS_ONE
4732 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004733 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735 }
4736
4737 if (xp->xp_context == EXPAND_FILES)
4738 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004739 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4740 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 else
4742 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004743 if (options & WILD_ICASE)
4744 flags |= EW_ICASE;
4745
Bram Moolenaard7834d32009-12-02 16:14:36 +00004746 /* Expand wildcards, supporting %:h and the like. */
4747 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 if (free_pat)
4749 vim_free(pat);
4750 return ret;
4751 }
4752
4753 *file = (char_u **)"";
4754 *num_file = 0;
4755 if (xp->xp_context == EXPAND_HELP)
4756 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004757 /* With an empty argument we would get all the help tags, which is
4758 * very slow. Get matches for "help" instead. */
4759 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4760 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
4762#ifdef FEAT_MULTI_LANG
4763 cleanup_help_tags(*num_file, *file);
4764#endif
4765 return OK;
4766 }
4767 return FAIL;
4768 }
4769
4770#ifndef FEAT_CMDL_COMPL
4771 return FAIL;
4772#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004773 if (xp->xp_context == EXPAND_SHELLCMD)
4774 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 if (xp->xp_context == EXPAND_OLD_SETTING)
4776 return ExpandOldSetting(num_file, file);
4777 if (xp->xp_context == EXPAND_BUFFERS)
4778 return ExpandBufnames(pat, num_file, file, options);
4779 if (xp->xp_context == EXPAND_TAGS
4780 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4781 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4782 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004783 {
4784 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004785 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4786 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004789 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004790 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004791 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004792 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004793 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004794 {
4795 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004796 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004797 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004798 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004799 {
4800 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004801 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004802 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004803# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4804 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004805 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004806# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004807 if (xp->xp_context == EXPAND_PACKADD)
4808 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
4810 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4811 if (regmatch.regprog == NULL)
4812 return FAIL;
4813
4814 /* set ignore-case according to p_ic, p_scs and pat */
4815 regmatch.rm_ic = ignorecase(pat);
4816
4817 if (xp->xp_context == EXPAND_SETTINGS
4818 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4819 ret = ExpandSettings(xp, &regmatch, num_file, file);
4820 else if (xp->xp_context == EXPAND_MAPPINGS)
4821 ret = ExpandMappings(&regmatch, num_file, file);
4822# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4823 else if (xp->xp_context == EXPAND_USER_DEFINED)
4824 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4825# endif
4826 else
4827 {
4828 static struct expgen
4829 {
4830 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004831 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004833 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 } tab[] =
4835 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004836 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4837 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004838 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004839#ifdef FEAT_CMDHIST
4840 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004843 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004844 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004845 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4846 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4847 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848#endif
4849#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004850 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4851 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4852 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4853 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854#endif
4855#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004856 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4857 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858#endif
4859#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004860 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004862#ifdef FEAT_PROFILE
4863 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4864#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004865 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004867 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4868 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004870#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004871 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004872#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004873#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004874 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004875#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004876#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004877 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4880 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004881 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4882 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004884 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004885 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 };
4887 int i;
4888
4889 /*
4890 * Find a context in the table and call the ExpandGeneric() with the
4891 * right function to do the expansion.
4892 */
4893 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004894 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (xp->xp_context == tab[i].context)
4896 {
4897 if (tab[i].ic)
4898 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004899 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004900 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 break;
4902 }
4903 }
4904
Bram Moolenaar473de612013-06-08 18:19:48 +02004905 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906
4907 return ret;
4908#endif /* FEAT_CMDL_COMPL */
4909}
4910
4911#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4912/*
4913 * Expand a list of names.
4914 *
4915 * Generic function for command line completion. It calls a function to
4916 * obtain strings, one by one. The strings are matched against a regexp
4917 * program. Matching strings are copied into an array, which is returned.
4918 *
4919 * Returns OK when no problems encountered, FAIL for error (out of memory).
4920 */
4921 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004922ExpandGeneric(
4923 expand_T *xp,
4924 regmatch_T *regmatch,
4925 int *num_file,
4926 char_u ***file,
4927 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004929 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
4931 int i;
4932 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004933 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 char_u *str;
4935
4936 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004937 * round == 0: count the number of matching names
4938 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004940 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 {
4942 for (i = 0; ; ++i)
4943 {
4944 str = (*func)(xp, i);
4945 if (str == NULL) /* end of list */
4946 break;
4947 if (*str == NUL) /* skip empty strings */
4948 continue;
4949
4950 if (vim_regexec(regmatch, str, (colnr_T)0))
4951 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004952 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004954 if (escaped)
4955 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4956 else
4957 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 (*file)[count] = str;
4959#ifdef FEAT_MENU
4960 if (func == get_menu_names && str != NULL)
4961 {
4962 /* test for separator added by get_menu_names() */
4963 str += STRLEN(str) - 1;
4964 if (*str == '\001')
4965 *str = '.';
4966 }
4967#endif
4968 }
4969 ++count;
4970 }
4971 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004972 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
4974 if (count == 0)
4975 return OK;
4976 *num_file = count;
4977 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4978 if (*file == NULL)
4979 {
4980 *file = (char_u **)"";
4981 return FAIL;
4982 }
4983 count = 0;
4984 }
4985 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004986
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004987 /* Sort the results. Keep menu's in the specified order. */
4988 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004989 {
4990 if (xp->xp_context == EXPAND_EXPRESSION
4991 || xp->xp_context == EXPAND_FUNCTIONS
4992 || xp->xp_context == EXPAND_USER_FUNC)
4993 /* <SNR> functions should be sorted to the end. */
4994 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4995 sort_func_compare);
4996 else
4997 sort_strings(*file, *num_file);
4998 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004999
Bram Moolenaar4f688582007-07-24 12:34:30 +00005000#ifdef FEAT_CMDL_COMPL
5001 /* Reset the variables used for special highlight names expansion, so that
5002 * they don't show up when getting normal highlight names by ID. */
5003 reset_expand_highlight();
5004#endif
5005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 return OK;
5007}
5008
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005009/*
5010 * Complete a shell command.
5011 * Returns FAIL or OK;
5012 */
5013 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005014expand_shellcmd(
5015 char_u *filepat, /* pattern to match with command names */
5016 int *num_file, /* return: number of matches */
5017 char_u ***file, /* return: array with matches */
5018 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005019{
5020 char_u *pat;
5021 int i;
5022 char_u *path;
5023 int mustfree = FALSE;
5024 garray_T ga;
5025 char_u *buf = alloc(MAXPATHL);
5026 size_t l;
5027 char_u *s, *e;
5028 int flags = flagsarg;
5029 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005030 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005031
5032 if (buf == NULL)
5033 return FAIL;
5034
5035 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5036 * space */
5037 pat = vim_strsave(filepat);
5038 for (i = 0; pat[i]; ++i)
5039 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005040 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005041
Bram Moolenaarb5971142015-03-21 17:32:19 +01005042 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005043
5044 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005045 if (mch_isFullName(pat))
5046 path = (char_u *)" ";
5047 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005048 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5049 path = (char_u *)".";
5050 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005051 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005052 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005053 if (path == NULL)
5054 path = (char_u *)"";
5055 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005056
5057 /*
5058 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005059 * collect them in "ga". When "." is not in $PATH also expand for the
5060 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005061 */
5062 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005063 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005064 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005065 if (*s == NUL)
5066 {
5067 if (did_curdir)
5068 break;
5069 /* Find directories in the current directory, path is empty. */
5070 did_curdir = TRUE;
5071 }
5072 else if (*s == '.')
5073 did_curdir = TRUE;
5074
Bram Moolenaar68c31742006-09-02 15:54:18 +00005075 if (*s == ' ')
5076 ++s; /* Skip space used for absolute path name. */
5077
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005078#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005079 e = vim_strchr(s, ';');
5080#else
5081 e = vim_strchr(s, ':');
5082#endif
5083 if (e == NULL)
5084 e = s + STRLEN(s);
5085
5086 l = e - s;
5087 if (l > MAXPATHL - 5)
5088 break;
5089 vim_strncpy(buf, s, l);
5090 add_pathsep(buf);
5091 l = STRLEN(buf);
5092 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5093
5094 /* Expand matches in one directory of $PATH. */
5095 ret = expand_wildcards(1, &buf, num_file, file, flags);
5096 if (ret == OK)
5097 {
5098 if (ga_grow(&ga, *num_file) == FAIL)
5099 FreeWild(*num_file, *file);
5100 else
5101 {
5102 for (i = 0; i < *num_file; ++i)
5103 {
5104 s = (*file)[i];
5105 if (STRLEN(s) > l)
5106 {
5107 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005108 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005109 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5110 }
5111 else
5112 vim_free(s);
5113 }
5114 vim_free(*file);
5115 }
5116 }
5117 if (*e != NUL)
5118 ++e;
5119 }
5120 *file = ga.ga_data;
5121 *num_file = ga.ga_len;
5122
5123 vim_free(buf);
5124 vim_free(pat);
5125 if (mustfree)
5126 vim_free(path);
5127 return OK;
5128}
5129
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005132static 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 +00005133
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00005135 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005136 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005138 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005139call_user_expand_func(
5140 void *(*user_expand_func)(char_u *, int, char_u **, int),
5141 expand_T *xp,
5142 int *num_file,
5143 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005145 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005146 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005149 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005150 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151
Bram Moolenaarb7515462013-06-29 12:58:33 +02005152 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005153 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 *num_file = 0;
5155 *file = NULL;
5156
Bram Moolenaarb7515462013-06-29 12:58:33 +02005157 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005158 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005159 keep = ccline.cmdbuff[ccline.cmdlen];
5160 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005161 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005162
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005163 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005164 args[1] = xp->xp_line;
5165 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 args[2] = num;
5167
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005168 /* Save the cmdline, we don't know what the function may do. */
5169 save_ccline = ccline;
5170 ccline.cmdbuff = NULL;
5171 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005173
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005174 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005175
5176 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005178 if (ccline.cmdbuff != NULL)
5179 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005180
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005181 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005182 return ret;
5183}
5184
5185/*
5186 * Expand names with a function defined by the user.
5187 */
5188 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005189ExpandUserDefined(
5190 expand_T *xp,
5191 regmatch_T *regmatch,
5192 int *num_file,
5193 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005194{
5195 char_u *retstr;
5196 char_u *s;
5197 char_u *e;
5198 char_u keep;
5199 garray_T ga;
5200
5201 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5202 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 return FAIL;
5204
5205 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005206 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
5208 e = vim_strchr(s, '\n');
5209 if (e == NULL)
5210 e = s + STRLEN(s);
5211 keep = *e;
5212 *e = 0;
5213
5214 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5215 {
5216 *e = keep;
5217 if (*e != NUL)
5218 ++e;
5219 continue;
5220 }
5221
5222 if (ga_grow(&ga, 1) == FAIL)
5223 break;
5224
5225 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5226 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227
5228 *e = keep;
5229 if (*e != NUL)
5230 ++e;
5231 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005232 vim_free(retstr);
5233 *file = ga.ga_data;
5234 *num_file = ga.ga_len;
5235 return OK;
5236}
5237
5238/*
5239 * Expand names with a list returned by a function defined by the user.
5240 */
5241 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005242ExpandUserList(
5243 expand_T *xp,
5244 int *num_file,
5245 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005246{
5247 list_T *retlist;
5248 listitem_T *li;
5249 garray_T ga;
5250
5251 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5252 if (retlist == NULL)
5253 return FAIL;
5254
5255 ga_init2(&ga, (int)sizeof(char *), 3);
5256 /* Loop over the items in the list. */
5257 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5258 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005259 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5260 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005261
5262 if (ga_grow(&ga, 1) == FAIL)
5263 break;
5264
5265 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005266 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005267 ++ga.ga_len;
5268 }
5269 list_unref(retlist);
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 *file = ga.ga_data;
5272 *num_file = ga.ga_len;
5273 return OK;
5274}
5275#endif
5276
5277/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005278 * Expand color scheme, compiler or filetype names.
5279 * Search from 'runtimepath':
5280 * 'runtimepath'/{dirnames}/{pat}.vim
5281 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5282 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5283 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5284 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005285 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 */
5287 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005288ExpandRTDir(
5289 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005290 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005291 int *num_file,
5292 char_u ***file,
5293 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 char_u *s;
5296 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005297 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005299 int i;
5300 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
5302 *num_file = 0;
5303 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005304 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005305 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005307 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005309 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5310 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005312 ga_clear_strings(&ga);
5313 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005315 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005316 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005317 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005319
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005320 if (flags & DIP_START) {
5321 for (i = 0; dirnames[i] != NULL; ++i)
5322 {
5323 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5324 if (s == NULL)
5325 {
5326 ga_clear_strings(&ga);
5327 return FAIL;
5328 }
5329 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5330 globpath(p_pp, s, &ga, 0);
5331 vim_free(s);
5332 }
5333 }
5334
5335 if (flags & DIP_OPT) {
5336 for (i = 0; dirnames[i] != NULL; ++i)
5337 {
5338 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5339 if (s == NULL)
5340 {
5341 ga_clear_strings(&ga);
5342 return FAIL;
5343 }
5344 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5345 globpath(p_pp, s, &ga, 0);
5346 vim_free(s);
5347 }
5348 }
5349
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005350 for (i = 0; i < ga.ga_len; ++i)
5351 {
5352 match = ((char_u **)ga.ga_data)[i];
5353 s = match;
5354 e = s + STRLEN(s);
5355 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5356 {
5357 e -= 4;
5358 for (s = e; s > match; mb_ptr_back(match, s))
5359 if (s < match || vim_ispathsep(*s))
5360 break;
5361 ++s;
5362 *e = NUL;
5363 mch_memmove(match, s, e - s + 1);
5364 }
5365 }
5366
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005367 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005368 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005369
5370 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005371 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005372 remove_duplicates(&ga);
5373
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 *file = ga.ga_data;
5375 *num_file = ga.ga_len;
5376 return OK;
5377}
5378
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005379/*
5380 * Expand loadplugin names:
5381 * 'packpath'/pack/ * /opt/{pat}
5382 */
5383 static int
5384ExpandPackAddDir(
5385 char_u *pat,
5386 int *num_file,
5387 char_u ***file)
5388{
5389 char_u *s;
5390 char_u *e;
5391 char_u *match;
5392 garray_T ga;
5393 int i;
5394 int pat_len;
5395
5396 *num_file = 0;
5397 *file = NULL;
5398 pat_len = (int)STRLEN(pat);
5399 ga_init2(&ga, (int)sizeof(char *), 10);
5400
5401 s = alloc((unsigned)(pat_len + 26));
5402 if (s == NULL)
5403 {
5404 ga_clear_strings(&ga);
5405 return FAIL;
5406 }
5407 sprintf((char *)s, "pack/*/opt/%s*", pat);
5408 globpath(p_pp, s, &ga, 0);
5409 vim_free(s);
5410
5411 for (i = 0; i < ga.ga_len; ++i)
5412 {
5413 match = ((char_u **)ga.ga_data)[i];
5414 s = gettail(match);
5415 e = s + STRLEN(s);
5416 mch_memmove(match, s, e - s + 1);
5417 }
5418
5419 if (ga.ga_len == 0)
5420 return FAIL;
5421
5422 /* Sort and remove duplicates which can happen when specifying multiple
5423 * directories in dirnames. */
5424 remove_duplicates(&ga);
5425
5426 *file = ga.ga_data;
5427 *num_file = ga.ga_len;
5428 return OK;
5429}
5430
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431#endif
5432
5433#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5434/*
5435 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005436 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005438 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005439globpath(
5440 char_u *path,
5441 char_u *file,
5442 garray_T *ga,
5443 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
5445 expand_T xpc;
5446 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 int num_p;
5449 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
5451 buf = alloc(MAXPATHL);
5452 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005453 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005455 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005457
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 /* Loop over all entries in {path}. */
5459 while (*path != NUL)
5460 {
5461 /* Copy one item of the path to buf[] and concatenate the file name. */
5462 copy_option_part(&path, buf, MAXPATHL, ",");
5463 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5464 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005465# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005466 /* Using the platform's path separator (\) makes vim incorrectly
5467 * treat it as an escape character, use '/' instead. */
5468 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5469 STRCAT(buf, "/");
5470# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005472# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005474 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5475 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005477 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005479 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 for (i = 0; i < num_p; ++i)
5482 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005483 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005484 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005485 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005488
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 FreeWild(num_p, p);
5490 }
5491 }
5492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
5494 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495}
5496
5497#endif
5498
5499#if defined(FEAT_CMDHIST) || defined(PROTO)
5500
5501/*********************************
5502 * Command line history stuff *
5503 *********************************/
5504
5505/*
5506 * Translate a history character to the associated type number.
5507 */
5508 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005509hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
5511 if (c == ':')
5512 return HIST_CMD;
5513 if (c == '=')
5514 return HIST_EXPR;
5515 if (c == '@')
5516 return HIST_INPUT;
5517 if (c == '>')
5518 return HIST_DEBUG;
5519 return HIST_SEARCH; /* must be '?' or '/' */
5520}
5521
5522/*
5523 * Table of history names.
5524 * These names are used in :history and various hist...() functions.
5525 * It is sufficient to give the significant prefix of a history name.
5526 */
5527
5528static char *(history_names[]) =
5529{
5530 "cmd",
5531 "search",
5532 "expr",
5533 "input",
5534 "debug",
5535 NULL
5536};
5537
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005538#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5539/*
5540 * Function given to ExpandGeneric() to obtain the possible first
5541 * arguments of the ":history command.
5542 */
5543 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005545{
5546 static char_u compl[2] = { NUL, NUL };
5547 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005548 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005549 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5550
5551 if (idx < short_names_count)
5552 {
5553 compl[0] = (char_u)short_names[idx];
5554 return compl;
5555 }
5556 if (idx < short_names_count + history_name_count)
5557 return (char_u *)history_names[idx - short_names_count];
5558 if (idx == short_names_count + history_name_count)
5559 return (char_u *)"all";
5560 return NULL;
5561}
5562#endif
5563
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564/*
5565 * init_history() - Initialize the command line history.
5566 * Also used to re-allocate the history when the size changes.
5567 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005568 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005569init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570{
5571 int newlen; /* new length of history table */
5572 histentry_T *temp;
5573 int i;
5574 int j;
5575 int type;
5576
5577 /*
5578 * If size of history table changed, reallocate it
5579 */
5580 newlen = (int)p_hi;
5581 if (newlen != hislen) /* history length changed */
5582 {
5583 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5584 {
5585 if (newlen)
5586 {
5587 temp = (histentry_T *)lalloc(
5588 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5589 if (temp == NULL) /* out of memory! */
5590 {
5591 if (type == 0) /* first one: just keep the old length */
5592 {
5593 newlen = hislen;
5594 break;
5595 }
5596 /* Already changed one table, now we can only have zero
5597 * length for all tables. */
5598 newlen = 0;
5599 type = -1;
5600 continue;
5601 }
5602 }
5603 else
5604 temp = NULL;
5605 if (newlen == 0 || temp != NULL)
5606 {
5607 if (hisidx[type] < 0) /* there are no entries yet */
5608 {
5609 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005610 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 }
5612 else if (newlen > hislen) /* array becomes bigger */
5613 {
5614 for (i = 0; i <= hisidx[type]; ++i)
5615 temp[i] = history[type][i];
5616 j = i;
5617 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005618 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 for ( ; j < hislen; ++i, ++j)
5620 temp[i] = history[type][j];
5621 }
5622 else /* array becomes smaller or 0 */
5623 {
5624 j = hisidx[type];
5625 for (i = newlen - 1; ; --i)
5626 {
5627 if (i >= 0) /* copy newest entries */
5628 temp[i] = history[type][j];
5629 else /* remove older entries */
5630 vim_free(history[type][j].hisstr);
5631 if (--j < 0)
5632 j = hislen - 1;
5633 if (j == hisidx[type])
5634 break;
5635 }
5636 hisidx[type] = newlen - 1;
5637 }
5638 vim_free(history[type]);
5639 history[type] = temp;
5640 }
5641 }
5642 hislen = newlen;
5643 }
5644}
5645
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005647clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005648{
5649 hisptr->hisnum = 0;
5650 hisptr->viminfo = FALSE;
5651 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005652 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005653}
5654
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655/*
5656 * Check if command line 'str' is already in history.
5657 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5658 */
5659 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005660in_history(
5661 int type,
5662 char_u *str,
5663 int move_to_front, /* Move the entry to the front if it exists */
5664 int sep,
5665 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666{
5667 int i;
5668 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005669 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
5671 if (hisidx[type] < 0)
5672 return FALSE;
5673 i = hisidx[type];
5674 do
5675 {
5676 if (history[type][i].hisstr == NULL)
5677 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005678
5679 /* For search history, check that the separator character matches as
5680 * well. */
5681 p = history[type][i].hisstr;
5682 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005683 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005684 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 {
5686 if (!move_to_front)
5687 return TRUE;
5688 last_i = i;
5689 break;
5690 }
5691 if (--i < 0)
5692 i = hislen - 1;
5693 } while (i != hisidx[type]);
5694
5695 if (last_i >= 0)
5696 {
5697 str = history[type][i].hisstr;
5698 while (i != hisidx[type])
5699 {
5700 if (++i >= hislen)
5701 i = 0;
5702 history[type][last_i] = history[type][i];
5703 last_i = i;
5704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005706 history[type][i].viminfo = FALSE;
5707 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005708 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 return TRUE;
5710 }
5711 return FALSE;
5712}
5713
5714/*
5715 * Convert history name (from table above) to its HIST_ equivalent.
5716 * When "name" is empty, return "cmd" history.
5717 * Returns -1 for unknown history name.
5718 */
5719 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005720get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
5722 int i;
5723 int len = (int)STRLEN(name);
5724
5725 /* No argument: use current history. */
5726 if (len == 0)
5727 return hist_char2type(ccline.cmdfirstc);
5728
5729 for (i = 0; history_names[i] != NULL; ++i)
5730 if (STRNICMP(name, history_names[i], len) == 0)
5731 return i;
5732
5733 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5734 return hist_char2type(name[0]);
5735
5736 return -1;
5737}
5738
5739static int last_maptick = -1; /* last seen maptick */
5740
5741/*
5742 * Add the given string to the given history. If the string is already in the
5743 * history then it is moved to the front. "histype" may be one of he HIST_
5744 * values.
5745 */
5746 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005747add_to_history(
5748 int histype,
5749 char_u *new_entry,
5750 int in_map, /* consider maptick when inside a mapping */
5751 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
5753 histentry_T *hisptr;
5754 int len;
5755
5756 if (hislen == 0) /* no history */
5757 return;
5758
Bram Moolenaara939e432013-11-09 05:30:26 +01005759 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5760 return;
5761
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 /*
5763 * Searches inside the same mapping overwrite each other, so that only
5764 * the last line is kept. Be careful not to remove a line that was moved
5765 * down, only lines that were added.
5766 */
5767 if (histype == HIST_SEARCH && in_map)
5768 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005769 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771 /* Current line is from the same mapping, remove it */
5772 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5773 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005774 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 --hisnum[histype];
5776 if (--hisidx[HIST_SEARCH] < 0)
5777 hisidx[HIST_SEARCH] = hislen - 1;
5778 }
5779 last_maptick = -1;
5780 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005781 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 {
5783 if (++hisidx[histype] == hislen)
5784 hisidx[histype] = 0;
5785 hisptr = &history[histype][hisidx[histype]];
5786 vim_free(hisptr->hisstr);
5787
5788 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005789 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5791 if (hisptr->hisstr != NULL)
5792 hisptr->hisstr[len + 1] = sep;
5793
5794 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005795 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005796 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 if (histype == HIST_SEARCH && in_map)
5798 last_maptick = maptick;
5799 }
5800}
5801
5802#if defined(FEAT_EVAL) || defined(PROTO)
5803
5804/*
5805 * Get identifier of newest history entry.
5806 * "histype" may be one of the HIST_ values.
5807 */
5808 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005809get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
5811 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5812 || hisidx[histype] < 0)
5813 return -1;
5814
5815 return history[histype][hisidx[histype]].hisnum;
5816}
5817
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 * Calculate history index from a number:
5820 * num > 0: seen as identifying number of a history entry
5821 * num < 0: relative position in history wrt newest entry
5822 * "histype" may be one of the HIST_ values.
5823 */
5824 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005825calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 int i;
5828 histentry_T *hist;
5829 int wrapped = FALSE;
5830
5831 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5832 || (i = hisidx[histype]) < 0 || num == 0)
5833 return -1;
5834
5835 hist = history[histype];
5836 if (num > 0)
5837 {
5838 while (hist[i].hisnum > num)
5839 if (--i < 0)
5840 {
5841 if (wrapped)
5842 break;
5843 i += hislen;
5844 wrapped = TRUE;
5845 }
5846 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5847 return i;
5848 }
5849 else if (-num <= hislen)
5850 {
5851 i += num + 1;
5852 if (i < 0)
5853 i += hislen;
5854 if (hist[i].hisstr != NULL)
5855 return i;
5856 }
5857 return -1;
5858}
5859
5860/*
5861 * Get a history entry by its index.
5862 * "histype" may be one of the HIST_ values.
5863 */
5864 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005865get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 idx = calc_hist_idx(histype, idx);
5868 if (idx >= 0)
5869 return history[histype][idx].hisstr;
5870 else
5871 return (char_u *)"";
5872}
5873
5874/*
5875 * Clear all entries of a history.
5876 * "histype" may be one of the HIST_ values.
5877 */
5878 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005879clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881 int i;
5882 histentry_T *hisptr;
5883
5884 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5885 {
5886 hisptr = history[histype];
5887 for (i = hislen; i--;)
5888 {
5889 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005890 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005891 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 }
5893 hisidx[histype] = -1; /* mark history as cleared */
5894 hisnum[histype] = 0; /* reset identifier counter */
5895 return OK;
5896 }
5897 return FAIL;
5898}
5899
5900/*
5901 * Remove all entries matching {str} from a history.
5902 * "histype" may be one of the HIST_ values.
5903 */
5904 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005905del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 regmatch_T regmatch;
5908 histentry_T *hisptr;
5909 int idx;
5910 int i;
5911 int last;
5912 int found = FALSE;
5913
5914 regmatch.regprog = NULL;
5915 regmatch.rm_ic = FALSE; /* always match case */
5916 if (hislen != 0
5917 && histype >= 0
5918 && histype < HIST_COUNT
5919 && *str != NUL
5920 && (idx = hisidx[histype]) >= 0
5921 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5922 != NULL)
5923 {
5924 i = last = idx;
5925 do
5926 {
5927 hisptr = &history[histype][i];
5928 if (hisptr->hisstr == NULL)
5929 break;
5930 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5931 {
5932 found = TRUE;
5933 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005934 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 }
5936 else
5937 {
5938 if (i != last)
5939 {
5940 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005941 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 }
5943 if (--last < 0)
5944 last += hislen;
5945 }
5946 if (--i < 0)
5947 i += hislen;
5948 } while (i != idx);
5949 if (history[histype][idx].hisstr == NULL)
5950 hisidx[histype] = -1;
5951 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005952 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 return found;
5954}
5955
5956/*
5957 * Remove an indexed entry from a history.
5958 * "histype" may be one of the HIST_ values.
5959 */
5960 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005961del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 int i, j;
5964
5965 i = calc_hist_idx(histype, idx);
5966 if (i < 0)
5967 return FALSE;
5968 idx = hisidx[histype];
5969 vim_free(history[histype][i].hisstr);
5970
5971 /* When deleting the last added search string in a mapping, reset
5972 * last_maptick, so that the last added search string isn't deleted again.
5973 */
5974 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5975 last_maptick = -1;
5976
5977 while (i != idx)
5978 {
5979 j = (i + 1) % hislen;
5980 history[histype][i] = history[histype][j];
5981 i = j;
5982 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005983 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 if (--i < 0)
5985 i += hislen;
5986 hisidx[histype] = i;
5987 return TRUE;
5988}
5989
5990#endif /* FEAT_EVAL */
5991
5992#if defined(FEAT_CRYPT) || defined(PROTO)
5993/*
5994 * Very specific function to remove the value in ":set key=val" from the
5995 * history.
5996 */
5997 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005998remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999{
6000 char_u *p;
6001 int i;
6002
6003 i = hisidx[HIST_CMD];
6004 if (i < 0)
6005 return;
6006 p = history[HIST_CMD][i].hisstr;
6007 if (p != NULL)
6008 for ( ; *p; ++p)
6009 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6010 {
6011 p = vim_strchr(p + 3, '=');
6012 if (p == NULL)
6013 break;
6014 ++p;
6015 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
6016 if (p[i] == '\\' && p[i + 1])
6017 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006018 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 --p;
6020 }
6021}
6022#endif
6023
6024#endif /* FEAT_CMDHIST */
6025
Bram Moolenaar064154c2016-03-19 22:50:43 +01006026#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006027/*
6028 * Get pointer to the command line info to use. cmdline_paste() may clear
6029 * ccline and put the previous value in prev_ccline.
6030 */
6031 static struct cmdline_info *
6032get_ccline_ptr(void)
6033{
6034 if ((State & CMDLINE) == 0)
6035 return NULL;
6036 if (ccline.cmdbuff != NULL)
6037 return &ccline;
6038 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6039 return &prev_ccline;
6040 return NULL;
6041}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006042#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006043
Bram Moolenaar064154c2016-03-19 22:50:43 +01006044#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006045/*
6046 * Get the current command line in allocated memory.
6047 * Only works when the command line is being edited.
6048 * Returns NULL when something is wrong.
6049 */
6050 char_u *
6051get_cmdline_str(void)
6052{
6053 struct cmdline_info *p = get_ccline_ptr();
6054
6055 if (p == NULL)
6056 return NULL;
6057 return vim_strnsave(p->cmdbuff, p->cmdlen);
6058}
6059
6060/*
6061 * Get the current command line position, counted in bytes.
6062 * Zero is the first position.
6063 * Only works when the command line is being edited.
6064 * Returns -1 when something is wrong.
6065 */
6066 int
6067get_cmdline_pos(void)
6068{
6069 struct cmdline_info *p = get_ccline_ptr();
6070
6071 if (p == NULL)
6072 return -1;
6073 return p->cmdpos;
6074}
6075
6076/*
6077 * Set the command line byte position to "pos". Zero is the first position.
6078 * Only works when the command line is being edited.
6079 * Returns 1 when failed, 0 when OK.
6080 */
6081 int
6082set_cmdline_pos(
6083 int pos)
6084{
6085 struct cmdline_info *p = get_ccline_ptr();
6086
6087 if (p == NULL)
6088 return 1;
6089
6090 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6091 * changed the command line. */
6092 if (pos < 0)
6093 new_cmdpos = 0;
6094 else
6095 new_cmdpos = pos;
6096 return 0;
6097}
6098#endif
6099
6100#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6101/*
6102 * Get the current command-line type.
6103 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6104 * Only works when the command line is being edited.
6105 * Returns NUL when something is wrong.
6106 */
6107 int
6108get_cmdline_type(void)
6109{
6110 struct cmdline_info *p = get_ccline_ptr();
6111
6112 if (p == NULL)
6113 return NUL;
6114 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006115 return
6116# ifdef FEAT_EVAL
6117 (p->input_fn) ? '@' :
6118# endif
6119 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006120 return p->cmdfirstc;
6121}
6122#endif
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6125/*
6126 * Get indices "num1,num2" that specify a range within a list (not a range of
6127 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6128 * Returns OK if parsed successfully, otherwise FAIL.
6129 */
6130 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006131get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
6133 int len;
6134 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006135 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136
6137 *str = skipwhite(*str);
6138 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6139 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006140 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 *str += len;
6142 *num1 = (int)num;
6143 first = TRUE;
6144 }
6145 *str = skipwhite(*str);
6146 if (**str == ',') /* parse "to" part of range */
6147 {
6148 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006149 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (len > 0)
6151 {
6152 *num2 = (int)num;
6153 *str = skipwhite(*str + len);
6154 }
6155 else if (!first) /* no number given at all */
6156 return FAIL;
6157 }
6158 else if (first) /* only one number given */
6159 *num2 = *num1;
6160 return OK;
6161}
6162#endif
6163
6164#if defined(FEAT_CMDHIST) || defined(PROTO)
6165/*
6166 * :history command - print a history
6167 */
6168 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006169ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
6171 histentry_T *hist;
6172 int histype1 = HIST_CMD;
6173 int histype2 = HIST_CMD;
6174 int hisidx1 = 1;
6175 int hisidx2 = -1;
6176 int idx;
6177 int i, j, k;
6178 char_u *end;
6179 char_u *arg = eap->arg;
6180
6181 if (hislen == 0)
6182 {
6183 MSG(_("'history' option is zero"));
6184 return;
6185 }
6186
6187 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6188 {
6189 end = arg;
6190 while (ASCII_ISALPHA(*end)
6191 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6192 end++;
6193 i = *end;
6194 *end = NUL;
6195 histype1 = get_histtype(arg);
6196 if (histype1 == -1)
6197 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006198 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 {
6200 histype1 = 0;
6201 histype2 = HIST_COUNT-1;
6202 }
6203 else
6204 {
6205 *end = i;
6206 EMSG(_(e_trailing));
6207 return;
6208 }
6209 }
6210 else
6211 histype2 = histype1;
6212 *end = i;
6213 }
6214 else
6215 end = arg;
6216 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6217 {
6218 EMSG(_(e_trailing));
6219 return;
6220 }
6221
6222 for (; !got_int && histype1 <= histype2; ++histype1)
6223 {
6224 STRCPY(IObuff, "\n # ");
6225 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6226 MSG_PUTS_TITLE(IObuff);
6227 idx = hisidx[histype1];
6228 hist = history[histype1];
6229 j = hisidx1;
6230 k = hisidx2;
6231 if (j < 0)
6232 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6233 if (k < 0)
6234 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6235 if (idx >= 0 && j <= k)
6236 for (i = idx + 1; !got_int; ++i)
6237 {
6238 if (i == hislen)
6239 i = 0;
6240 if (hist[i].hisstr != NULL
6241 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6242 {
6243 msg_putchar('\n');
6244 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6245 hist[i].hisnum);
6246 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6247 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006248 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 else
6250 STRCAT(IObuff, hist[i].hisstr);
6251 msg_outtrans(IObuff);
6252 out_flush();
6253 }
6254 if (i == idx)
6255 break;
6256 }
6257 }
6258}
6259#endif
6260
6261#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006262/*
6263 * Buffers for history read from a viminfo file. Only valid while reading.
6264 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006265static histentry_T *viminfo_history[HIST_COUNT] =
6266 {NULL, NULL, NULL, NULL, NULL};
6267static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6268static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269static int viminfo_add_at_front = FALSE;
6270
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006271static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272
6273/*
6274 * Translate a history type number to the associated character.
6275 */
6276 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006277hist_type2char(
6278 int type,
6279 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280{
6281 if (type == HIST_CMD)
6282 return ':';
6283 if (type == HIST_SEARCH)
6284 {
6285 if (use_question)
6286 return '?';
6287 else
6288 return '/';
6289 }
6290 if (type == HIST_EXPR)
6291 return '=';
6292 return '@';
6293}
6294
6295/*
6296 * Prepare for reading the history from the viminfo file.
6297 * This allocates history arrays to store the read history lines.
6298 */
6299 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006300prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 int i;
6303 int num;
6304 int type;
6305 int len;
6306
6307 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006308 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 if (asklen > hislen)
6310 asklen = hislen;
6311
6312 for (type = 0; type < HIST_COUNT; ++type)
6313 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006314 /* Count the number of empty spaces in the history list. Entries read
6315 * from viminfo previously are also considered empty. If there are
6316 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006318 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 num++;
6320 len = asklen;
6321 if (num > len)
6322 len = num;
6323 if (len <= 0)
6324 viminfo_history[type] = NULL;
6325 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006326 viminfo_history[type] = (histentry_T *)lalloc(
6327 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 if (viminfo_history[type] == NULL)
6329 len = 0;
6330 viminfo_hislen[type] = len;
6331 viminfo_hisidx[type] = 0;
6332 }
6333}
6334
6335/*
6336 * Accept a line from the viminfo, store it in the history array when it's
6337 * new.
6338 */
6339 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006340read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342 int type;
6343 long_u len;
6344 char_u *val;
6345 char_u *p;
6346
6347 type = hist_char2type(virp->vir_line[0]);
6348 if (viminfo_hisidx[type] < viminfo_hislen[type])
6349 {
6350 val = viminfo_readstring(virp, 1, TRUE);
6351 if (val != NULL && *val != NUL)
6352 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006353 int sep = (*val == ' ' ? NUL : *val);
6354
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006356 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 {
6358 /* Need to re-allocate to append the separator byte. */
6359 len = STRLEN(val);
6360 p = lalloc(len + 2, TRUE);
6361 if (p != NULL)
6362 {
6363 if (type == HIST_SEARCH)
6364 {
6365 /* Search entry: Move the separator from the first
6366 * column to after the NUL. */
6367 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006368 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
6370 else
6371 {
6372 /* Not a search entry: No separator in the viminfo
6373 * file, add a NUL separator. */
6374 mch_memmove(p, val, (size_t)len + 1);
6375 p[len + 1] = NUL;
6376 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006377 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6378 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006379 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6380 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006381 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 }
6383 }
6384 }
6385 vim_free(val);
6386 }
6387 return viminfo_readline(virp);
6388}
6389
Bram Moolenaar07219f92013-04-14 23:19:36 +02006390/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006391 * Accept a new style history line from the viminfo, store it in the history
6392 * array when it's new.
6393 */
6394 void
6395handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006396 garray_T *values,
6397 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006398{
6399 int type;
6400 long_u len;
6401 char_u *val;
6402 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006403 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006404
6405 /* Check the format:
6406 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006407 if (values->ga_len < 4
6408 || vp[0].bv_type != BVAL_NR
6409 || vp[1].bv_type != BVAL_NR
6410 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6411 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006412 return;
6413
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006414 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006415 if (type >= HIST_COUNT)
6416 return;
6417 if (viminfo_hisidx[type] < viminfo_hislen[type])
6418 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006419 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006420 if (val != NULL && *val != NUL)
6421 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006422 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6423 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006424 int idx;
6425 int overwrite = FALSE;
6426
6427 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6428 {
6429 /* If lines were written by an older Vim we need to avoid
6430 * getting duplicates. See if the entry already exists. */
6431 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6432 {
6433 p = viminfo_history[type][idx].hisstr;
6434 if (STRCMP(val, p) == 0
6435 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6436 {
6437 overwrite = TRUE;
6438 break;
6439 }
6440 }
6441
6442 if (!overwrite)
6443 {
6444 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006445 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006446 p = lalloc(len + 2, TRUE);
6447 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006448 else
6449 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006450 if (p != NULL)
6451 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006452 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006453 if (!overwrite)
6454 {
6455 mch_memmove(p, val, (size_t)len + 1);
6456 /* Put the separator after the NUL. */
6457 p[len + 1] = sep;
6458 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006459 viminfo_history[type][idx].hisnum = 0;
6460 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006461 viminfo_hisidx[type]++;
6462 }
6463 }
6464 }
6465 }
6466 }
6467}
6468
6469/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006470 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006471 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006472 static void
6473concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
6475 int idx;
6476 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006477
6478 idx = hisidx[type] + viminfo_hisidx[type];
6479 if (idx >= hislen)
6480 idx -= hislen;
6481 else if (idx < 0)
6482 idx = hislen - 1;
6483 if (viminfo_add_at_front)
6484 hisidx[type] = idx;
6485 else
6486 {
6487 if (hisidx[type] == -1)
6488 hisidx[type] = hislen - 1;
6489 do
6490 {
6491 if (history[type][idx].hisstr != NULL
6492 || history[type][idx].viminfo)
6493 break;
6494 if (++idx == hislen)
6495 idx = 0;
6496 } while (idx != hisidx[type]);
6497 if (idx != hisidx[type] && --idx < 0)
6498 idx = hislen - 1;
6499 }
6500 for (i = 0; i < viminfo_hisidx[type]; i++)
6501 {
6502 vim_free(history[type][idx].hisstr);
6503 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6504 history[type][idx].viminfo = TRUE;
6505 history[type][idx].time_set = viminfo_history[type][i].time_set;
6506 if (--idx < 0)
6507 idx = hislen - 1;
6508 }
6509 idx += 1;
6510 idx %= hislen;
6511 for (i = 0; i < viminfo_hisidx[type]; i++)
6512 {
6513 history[type][idx++].hisnum = ++hisnum[type];
6514 idx %= hislen;
6515 }
6516}
6517
6518#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6519 static int
6520#ifdef __BORLANDC__
6521_RTLENTRYF
6522#endif
6523sort_hist(const void *s1, const void *s2)
6524{
6525 histentry_T *p1 = *(histentry_T **)s1;
6526 histentry_T *p2 = *(histentry_T **)s2;
6527
6528 if (p1->time_set < p2->time_set) return -1;
6529 if (p1->time_set > p2->time_set) return 1;
6530 return 0;
6531}
6532#endif
6533
6534/*
6535 * Merge history lines from viminfo and lines typed in this Vim based on the
6536 * timestamp;
6537 */
6538 static void
6539merge_history(int type)
6540{
6541 int max_len;
6542 histentry_T **tot_hist;
6543 histentry_T *new_hist;
6544 int i;
6545 int len;
6546
6547 /* Make one long list with all entries. */
6548 max_len = hislen + viminfo_hisidx[type];
6549 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6550 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6551 if (tot_hist == NULL || new_hist == NULL)
6552 {
6553 vim_free(tot_hist);
6554 vim_free(new_hist);
6555 return;
6556 }
6557 for (i = 0; i < viminfo_hisidx[type]; i++)
6558 tot_hist[i] = &viminfo_history[type][i];
6559 len = i;
6560 for (i = 0; i < hislen; i++)
6561 if (history[type][i].hisstr != NULL)
6562 tot_hist[len++] = &history[type][i];
6563
6564 /* Sort the list on timestamp. */
6565 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6566
6567 /* Keep the newest ones. */
6568 for (i = 0; i < hislen; i++)
6569 {
6570 if (i < len)
6571 {
6572 new_hist[i] = *tot_hist[i];
6573 tot_hist[i]->hisstr = NULL;
6574 if (new_hist[i].hisnum == 0)
6575 new_hist[i].hisnum = ++hisnum[type];
6576 }
6577 else
6578 clear_hist_entry(&new_hist[i]);
6579 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006580 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006581
6582 /* Free what is not kept. */
6583 for (i = 0; i < viminfo_hisidx[type]; i++)
6584 vim_free(viminfo_history[type][i].hisstr);
6585 for (i = 0; i < hislen; i++)
6586 vim_free(history[type][i].hisstr);
6587 vim_free(history[type]);
6588 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006589 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006590}
6591
6592/*
6593 * Finish reading history lines from viminfo. Not used when writing viminfo.
6594 */
6595 void
6596finish_viminfo_history(vir_T *virp)
6597{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006599 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600
6601 for (type = 0; type < HIST_COUNT; ++type)
6602 {
6603 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006604 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006605
6606 if (merge)
6607 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006609 concat_history(type);
6610
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 vim_free(viminfo_history[type]);
6612 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006613 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615}
6616
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006617/*
6618 * Write history to viminfo file in "fp".
6619 * When "merge" is TRUE merge history lines with a previously read viminfo
6620 * file, data is in viminfo_history[].
6621 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006624write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 int i;
6627 int type;
6628 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006629 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630
6631 init_history();
6632 if (hislen == 0)
6633 return;
6634 for (type = 0; type < HIST_COUNT; ++type)
6635 {
6636 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6637 if (num_saved == 0)
6638 continue;
6639 if (num_saved < 0) /* Use default */
6640 num_saved = hislen;
6641 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6642 type == HIST_CMD ? _("Command Line") :
6643 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006644 type == HIST_EXPR ? _("Expression") :
6645 type == HIST_INPUT ? _("Input Line") :
6646 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 if (num_saved > hislen)
6648 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006649
6650 /*
6651 * Merge typed and viminfo history:
6652 * round 1: history of typed commands.
6653 * round 2: history from recently read viminfo.
6654 */
6655 for (round = 1; round <= 2; ++round)
6656 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006657 if (round == 1)
6658 /* start at newest entry, somewhere in the list */
6659 i = hisidx[type];
6660 else if (viminfo_hisidx[type] > 0)
6661 /* start at newest entry, first in the list */
6662 i = 0;
6663 else
6664 /* empty list */
6665 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006666 if (i >= 0)
6667 while (num_saved > 0
6668 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006670 char_u *p;
6671 time_t timestamp;
6672 int c = NUL;
6673
6674 if (round == 1)
6675 {
6676 p = history[type][i].hisstr;
6677 timestamp = history[type][i].time_set;
6678 }
6679 else
6680 {
6681 p = viminfo_history[type] == NULL ? NULL
6682 : viminfo_history[type][i].hisstr;
6683 timestamp = viminfo_history[type] == NULL ? 0
6684 : viminfo_history[type][i].time_set;
6685 }
6686
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006687 if (p != NULL && (round == 2
6688 || !merge
6689 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006691 --num_saved;
6692 fputc(hist_type2char(type, TRUE), fp);
6693 /* For the search history: put the separator in the
6694 * second column; use a space if there isn't one. */
6695 if (type == HIST_SEARCH)
6696 {
6697 c = p[STRLEN(p) + 1];
6698 putc(c == NUL ? ' ' : c, fp);
6699 }
6700 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006701
6702 {
6703 char cbuf[NUMBUFLEN];
6704
6705 /* New style history with a bar line. Format:
6706 * |{bartype},{histtype},{timestamp},{separator},"text" */
6707 if (c == NUL)
6708 cbuf[0] = NUL;
6709 else
6710 sprintf(cbuf, "%d", c);
6711 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6712 type, (long)timestamp, cbuf);
6713 barline_writestring(fp, p, LSIZE - 20);
6714 putc('\n', fp);
6715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006717 if (round == 1)
6718 {
6719 /* Decrement index, loop around and stop when back at
6720 * the start. */
6721 if (--i < 0)
6722 i = hislen - 1;
6723 if (i == hisidx[type])
6724 break;
6725 }
6726 else
6727 {
6728 /* Increment index. Stop at the end in the while. */
6729 ++i;
6730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006732 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006733 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006734 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006735 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006736 vim_free(viminfo_history[type]);
6737 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006738 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 }
6740}
6741#endif /* FEAT_VIMINFO */
6742
6743#if defined(FEAT_FKMAP) || defined(PROTO)
6744/*
6745 * Write a character at the current cursor+offset position.
6746 * It is directly written into the command buffer block.
6747 */
6748 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006749cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
6751 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6752 {
6753 EMSG(_("E198: cmd_pchar beyond the command length"));
6754 return;
6755 }
6756 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6757 ccline.cmdbuff[ccline.cmdlen] = NUL;
6758}
6759
6760 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006761cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6764 {
6765 /* EMSG(_("cmd_gchar beyond the command length")); */
6766 return NUL;
6767 }
6768 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6769}
6770#endif
6771
6772#if defined(FEAT_CMDWIN) || defined(PROTO)
6773/*
6774 * Open a window on the current command line and history. Allow editing in
6775 * the window. Returns when the window is closed.
6776 * Returns:
6777 * CR if the command is to be executed
6778 * Ctrl_C if it is to be abandoned
6779 * K_IGNORE if editing continues
6780 */
6781 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006782ex_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006785 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006787 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 win_T *wp;
6789 int i;
6790 linenr_T lnum;
6791 int histtype;
6792 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006793#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 int save_restart_edit = restart_edit;
6797 int save_State = State;
6798 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006799#ifdef FEAT_RIGHTLEFT
6800 int save_cmdmsg_rl = cmdmsg_rl;
6801#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006802#ifdef FEAT_FOLDING
6803 int save_KeyTyped;
6804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805
6806 /* Can't do this recursively. Can't do it when typing a password. */
6807 if (cmdwin_type != 0
6808# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6809 || cmdline_star > 0
6810# endif
6811 )
6812 {
6813 beep_flush();
6814 return K_IGNORE;
6815 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006816 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817
6818 /* Save current window sizes. */
6819 win_size_save(&winsizes);
6820
6821# ifdef FEAT_AUTOCMD
6822 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006823 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006825 /* don't use a new tab page */
6826 cmdmod.tab = 0;
6827
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 /* Create a window for the command-line buffer. */
6829 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6830 {
6831 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006832# ifdef FEAT_AUTOCMD
6833 unblock_autocmds();
6834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 return K_IGNORE;
6836 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006837 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
6839 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006840 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006841 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6843 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6844 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006845#ifdef FEAT_FOLDING
6846 curwin->w_p_fen = FALSE;
6847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006849 curwin->w_p_rl = cmdmsg_rl;
6850 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006852 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853
6854# ifdef FEAT_AUTOCMD
6855 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006856 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857# endif
6858
Bram Moolenaar46152342005-09-07 21:18:43 +00006859 /* Showing the prompt may have set need_wait_return, reset it. */
6860 need_wait_return = FALSE;
6861
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006862 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6864 {
6865 if (p_wc == TAB)
6866 {
6867 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6868 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6869 }
6870 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6871 }
6872
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006873 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6874 * sets 'textwidth' to 78). */
6875 curbuf->b_p_tw = 0;
6876
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 /* Fill the buffer with the history. */
6878 init_history();
6879 if (hislen > 0)
6880 {
6881 i = hisidx[histtype];
6882 if (i >= 0)
6883 {
6884 lnum = 0;
6885 do
6886 {
6887 if (++i == hislen)
6888 i = 0;
6889 if (history[histtype][i].hisstr != NULL)
6890 ml_append(lnum++, history[histtype][i].hisstr,
6891 (colnr_T)0, FALSE);
6892 }
6893 while (i != hisidx[histtype]);
6894 }
6895 }
6896
6897 /* Replace the empty last line with the current command-line and put the
6898 * cursor there. */
6899 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6900 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6901 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006902 changed_line_abv_curs();
6903 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006904 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
6906 /* Save the command line info, can be used recursively. */
6907 save_ccline = ccline;
6908 ccline.cmdbuff = NULL;
6909 ccline.cmdprompt = NULL;
6910
6911 /* No Ex mode here! */
6912 exmode_active = 0;
6913
6914 State = NORMAL;
6915# ifdef FEAT_MOUSE
6916 setmouse();
6917# endif
6918
6919# ifdef FEAT_AUTOCMD
6920 /* Trigger CmdwinEnter autocommands. */
6921 typestr[0] = cmdwin_type;
6922 typestr[1] = NUL;
6923 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006924 if (restart_edit != 0) /* autocmd with ":startinsert" */
6925 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926# endif
6927
6928 i = RedrawingDisabled;
6929 RedrawingDisabled = 0;
6930
6931 /*
6932 * Call the main loop until <CR> or CTRL-C is typed.
6933 */
6934 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006935 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
6937 RedrawingDisabled = i;
6938
6939# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006940
6941# ifdef FEAT_FOLDING
6942 save_KeyTyped = KeyTyped;
6943# endif
6944
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 /* Trigger CmdwinLeave autocommands. */
6946 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006947
6948# ifdef FEAT_FOLDING
6949 /* Restore KeyTyped in case it is modified by autocommands */
6950 KeyTyped = save_KeyTyped;
6951# endif
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953# endif
6954
Bram Moolenaarccc18222007-05-10 18:25:20 +00006955 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 ccline = save_ccline;
6957 cmdwin_type = 0;
6958
6959 exmode_active = save_exmode;
6960
Bram Moolenaarf9821062008-06-20 16:31:07 +00006961 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006963 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
6965 cmdwin_result = Ctrl_C;
6966 EMSG(_("E199: Active window or buffer deleted"));
6967 }
6968 else
6969 {
6970# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6971 /* autocmds may abort script processing */
6972 if (aborting() && cmdwin_result != K_IGNORE)
6973 cmdwin_result = Ctrl_C;
6974# endif
6975 /* Set the new command line from the cmdline buffer. */
6976 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006977 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006979 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6980
6981 if (histtype == HIST_CMD)
6982 {
6983 /* Execute the command directly. */
6984 ccline.cmdbuff = vim_strsave((char_u *)p);
6985 cmdwin_result = CAR;
6986 }
6987 else
6988 {
6989 /* First need to cancel what we were doing. */
6990 ccline.cmdbuff = NULL;
6991 stuffcharReadbuff(':');
6992 stuffReadbuff((char_u *)p);
6993 stuffcharReadbuff(CAR);
6994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 }
6996 else if (cmdwin_result == K_XF2) /* :qa typed */
6997 {
6998 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6999 cmdwin_result = CAR;
7000 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007001 else if (cmdwin_result == Ctrl_C)
7002 {
7003 /* :q or :close, don't execute any command
7004 * and don't modify the cmd window. */
7005 ccline.cmdbuff = NULL;
7006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 else
7008 ccline.cmdbuff = vim_strsave(ml_get_curline());
7009 if (ccline.cmdbuff == NULL)
7010 cmdwin_result = Ctrl_C;
7011 else
7012 {
7013 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7014 ccline.cmdbufflen = ccline.cmdlen + 1;
7015 ccline.cmdpos = curwin->w_cursor.col;
7016 if (ccline.cmdpos > ccline.cmdlen)
7017 ccline.cmdpos = ccline.cmdlen;
7018 if (cmdwin_result == K_IGNORE)
7019 {
7020 set_cmdspos_cursor();
7021 redrawcmd();
7022 }
7023 }
7024
7025# ifdef FEAT_AUTOCMD
7026 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007027 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007029# ifdef FEAT_CONCEAL
7030 /* Avoid command-line window first character being concealed. */
7031 curwin->w_p_cole = 0;
7032# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007034 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 win_goto(old_curwin);
7036 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007037
7038 /* win_close() may have already wiped the buffer when 'bh' is
7039 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007040 if (bufref_valid(&bufref))
7041 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
7043 /* Restore window sizes. */
7044 win_size_restore(&winsizes);
7045
7046# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007047 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048# endif
7049 }
7050
7051 ga_clear(&winsizes);
7052 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007053# ifdef FEAT_RIGHTLEFT
7054 cmdmsg_rl = save_cmdmsg_rl;
7055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056
7057 State = save_State;
7058# ifdef FEAT_MOUSE
7059 setmouse();
7060# endif
7061
7062 return cmdwin_result;
7063}
7064#endif /* FEAT_CMDWIN */
7065
7066/*
7067 * Used for commands that either take a simple command string argument, or:
7068 * cmd << endmarker
7069 * {script}
7070 * endmarker
7071 * Returns a pointer to allocated memory with {script} or NULL.
7072 */
7073 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007074script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 char_u *theline;
7077 char *end_pattern = NULL;
7078 char dot[] = ".";
7079 garray_T ga;
7080
7081 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7082 return NULL;
7083
7084 ga_init2(&ga, 1, 0x400);
7085
7086 if (cmd[2] != NUL)
7087 end_pattern = (char *)skipwhite(cmd + 2);
7088 else
7089 end_pattern = dot;
7090
7091 for (;;)
7092 {
7093 theline = eap->getline(
7094#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007095 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096#endif
7097 NUL, eap->cookie, 0);
7098
7099 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007100 {
7101 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104
7105 ga_concat(&ga, theline);
7106 ga_append(&ga, '\n');
7107 vim_free(theline);
7108 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007109 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
7111 return (char_u *)ga.ga_data;
7112}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007113
7114#ifdef FEAT_SEARCH_EXTRA
7115 static void
7116set_search_match(pos_T *t)
7117{
7118 /*
7119 * First move cursor to end of match, then to the start. This
7120 * moves the whole match onto the screen when 'nowrap' is set.
7121 */
7122 t->lnum += search_match_lines;
7123 t->col = search_match_endcol;
7124 if (t->lnum > curbuf->b_ml.ml_line_count)
7125 {
7126 t->lnum = curbuf->b_ml.ml_line_count;
7127 coladvance((colnr_T)MAXCOL);
7128 }
7129}
7130#endif