blob: cf99ae2a838de59487b2e348b5b6d23d15dbeab1 [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 Moolenaar111ff9f2005-03-08 22:40:03 +0000215#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
216 /* Everything that may work recursively should save and restore the
217 * current command line in save_ccline. That includes update_screen(), a
218 * custom status line may invoke ":normal". */
219 struct cmdline_info save_ccline;
220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222#ifdef FEAT_EVAL
223 if (firstc == -1)
224 {
225 firstc = NUL;
226 break_ctrl_c = TRUE;
227 }
228#endif
229#ifdef FEAT_RIGHTLEFT
230 /* start without Hebrew mapping for a command line */
231 if (firstc == ':' || firstc == '=' || firstc == '>')
232 cmd_hkmap = 0;
233#endif
234
235 ccline.overstrike = FALSE; /* always start in insert mode */
236#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200237 clearpos(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200238 save_cursor = curwin->w_cursor; /* may be restored later */
239 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 old_curswant = curwin->w_curswant;
241 old_leftcol = curwin->w_leftcol;
242 old_topline = curwin->w_topline;
243# ifdef FEAT_DIFF
244 old_topfill = curwin->w_topfill;
245# endif
246 old_botline = curwin->w_botline;
247#endif
248
249 /*
250 * set some variables for redrawcmd()
251 */
252 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000253 ccline.cmdindent = (firstc > 0 ? indent : 0);
254
255 /* alloc initial ccline.cmdbuff */
256 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 if (ccline.cmdbuff == NULL)
258 return NULL; /* out of memory */
259 ccline.cmdlen = ccline.cmdpos = 0;
260 ccline.cmdbuff[0] = NUL;
261
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000262 /* autoindent for :insert and :append */
263 if (firstc <= 0)
264 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200265 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000266 ccline.cmdbuff[indent] = NUL;
267 ccline.cmdpos = indent;
268 ccline.cmdspos = indent;
269 ccline.cmdlen = indent;
270 }
271
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000273 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275#ifdef FEAT_RIGHTLEFT
276 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
277 && (firstc == '/' || firstc == '?'))
278 cmdmsg_rl = TRUE;
279 else
280 cmdmsg_rl = FALSE;
281#endif
282
283 redir_off = TRUE; /* don't redirect the typed command */
284 if (!cmd_silent)
285 {
286 i = msg_scrolled;
287 msg_scrolled = 0; /* avoid wait_return message */
288 gotocmdline(TRUE);
289 msg_scrolled += i;
290 redrawcmdprompt(); /* draw prompt or indent */
291 set_cmdspos();
292 }
293 xpc.xp_context = EXPAND_NOTHING;
294 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000295#ifndef BACKSLASH_IN_FILENAME
296 xpc.xp_shell = FALSE;
297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000299#if defined(FEAT_EVAL)
300 if (ccline.input_fn)
301 {
302 xpc.xp_context = ccline.xp_context;
303 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000304# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000305 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000306# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000307 }
308#endif
309
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 /*
311 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
312 * doing ":@0" when register 0 doesn't contain a CR.
313 */
314 msg_scroll = FALSE;
315
316 State = CMDLINE;
317
318 if (firstc == '/' || firstc == '?' || firstc == '@')
319 {
320 /* Use ":lmap" mappings for search pattern and input(). */
321 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
322 b_im_ptr = &curbuf->b_p_iminsert;
323 else
324 b_im_ptr = &curbuf->b_p_imsearch;
325 if (*b_im_ptr == B_IMODE_LMAP)
326 State |= LANGMAP;
327#ifdef USE_IM_CONTROL
328 im_set_active(*b_im_ptr == B_IMODE_IM);
329#endif
330 }
331#ifdef USE_IM_CONTROL
332 else if (p_imcmdline)
333 im_set_active(TRUE);
334#endif
335
336#ifdef FEAT_MOUSE
337 setmouse();
338#endif
339#ifdef CURSOR_SHAPE
340 ui_cursor_shape(); /* may show different cursor shape */
341#endif
342
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000343 /* When inside an autocommand for writing "exiting" may be set and
344 * terminal mode set to cooked. Need to set raw mode here then. */
345 settmode(TMODE_RAW);
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#ifdef FEAT_CMDHIST
348 init_history();
349 hiscnt = hislen; /* set hiscnt to impossible history value */
350 histype = hist_char2type(firstc);
351#endif
352
353#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000354 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355#endif
356
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200357 /* If something above caused an error, reset the flags, we do want to type
358 * and execute commands. Display may be messed up a bit. */
359 if (did_emsg)
360 redrawcmd();
361 did_emsg = FALSE;
362 got_int = FALSE;
363
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 /*
365 * Collect the command string, handling editing keys.
366 */
367 for (;;)
368 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000369 redir_off = TRUE; /* Don't redirect the typed command.
370 Repeated, because a ":redir" inside
371 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372#ifdef USE_ON_FLY_SCROLL
373 dont_scroll = FALSE; /* allow scrolling here */
374#endif
375 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
376
377 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000378
379 /* Get a character. Ignore K_IGNORE, it should not do anything, such
380 * as stop completion. */
381 do
382 {
383 c = safe_vgetc();
384 } while (c == K_IGNORE);
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 if (KeyTyped)
387 {
388 some_key_typed = TRUE;
389#ifdef FEAT_RIGHTLEFT
390 if (cmd_hkmap)
391 c = hkmap(c);
392# ifdef FEAT_FKMAP
393 if (cmd_fkmap)
394 c = cmdl_fkmap(c);
395# endif
396 if (cmdmsg_rl && !KeyStuffed)
397 {
398 /* Invert horizontal movements and operations. Only when
399 * typed by the user directly, not when the result of a
400 * mapping. */
401 switch (c)
402 {
403 case K_RIGHT: c = K_LEFT; break;
404 case K_S_RIGHT: c = K_S_LEFT; break;
405 case K_C_RIGHT: c = K_C_LEFT; break;
406 case K_LEFT: c = K_RIGHT; break;
407 case K_S_LEFT: c = K_S_RIGHT; break;
408 case K_C_LEFT: c = K_C_RIGHT; break;
409 }
410 }
411#endif
412 }
413
414 /*
415 * Ignore got_int when CTRL-C was typed here.
416 * Don't ignore it in :global, we really need to break then, e.g., for
417 * ":g/pat/normal /pat" (without the <CR>).
418 * Don't ignore it for the input() function.
419 */
420 if ((c == Ctrl_C
421#ifdef UNIX
422 || c == intr_char
423#endif
424 )
425#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
426 && firstc != '@'
427#endif
428#ifdef FEAT_EVAL
429 && !break_ctrl_c
430#endif
431 && !global_busy)
432 got_int = FALSE;
433
434#ifdef FEAT_CMDHIST
435 /* free old command line when finished moving around in the history
436 * list */
437 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000438 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000439 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 && c != K_PAGEDOWN && c != K_PAGEUP
441 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000442 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
444 {
445 vim_free(lookfor);
446 lookfor = NULL;
447 }
448#endif
449
450 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000451 * When there are matching completions to select <S-Tab> works like
452 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000454 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 c = Ctrl_P;
456
457#ifdef FEAT_WILDMENU
458 /* Special translations for 'wildmenu' */
459 if (did_wild_list && p_wmnu)
460 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000461 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000463 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 c = Ctrl_N;
465 }
466 /* Hitting CR after "emenu Name.": complete submenu */
467 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
468 && ccline.cmdpos > 1
469 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
470 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
471 && (c == '\n' || c == '\r' || c == K_KENTER))
472 c = K_DOWN;
473#endif
474
475 /* free expanded names when finished walking through matches */
476 if (xpc.xp_numfiles != -1
477 && !(c == p_wc && KeyTyped) && c != p_wcm
478 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
479 && c != Ctrl_L)
480 {
481 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
482 did_wild_list = FALSE;
483#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000484 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485#endif
486 xpc.xp_context = EXPAND_NOTHING;
487 wim_index = 0;
488#ifdef FEAT_WILDMENU
489 if (p_wmnu && wild_menu_showing != 0)
490 {
491 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000492 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000493
494 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000495 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496
497 if (wild_menu_showing == WM_SCROLLED)
498 {
499 /* Entered command line, move it up */
500 cmdline_row--;
501 redrawcmd();
502 }
503 else if (save_p_ls != -1)
504 {
505 /* restore 'laststatus' and 'winminheight' */
506 p_ls = save_p_ls;
507 p_wmh = save_p_wmh;
508 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000509 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000511 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 redrawcmd();
513 save_p_ls = -1;
514 }
515 else
516 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 redraw_statuslines();
519 }
520 KeyTyped = skt;
521 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000522 if (ccline.input_fn)
523 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 }
525#endif
526 }
527
528#ifdef FEAT_WILDMENU
529 /* Special translations for 'wildmenu' */
530 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
531 {
532 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000533 if (c == K_DOWN && ccline.cmdpos > 0
534 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000536 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 {
538 /* Hitting <Up>: Remove one submenu name in front of the
539 * cursor */
540 int found = FALSE;
541
542 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
543 i = 0;
544 while (--j > 0)
545 {
546 /* check for start of menu name */
547 if (ccline.cmdbuff[j] == ' '
548 && ccline.cmdbuff[j - 1] != '\\')
549 {
550 i = j + 1;
551 break;
552 }
553 /* check for start of submenu name */
554 if (ccline.cmdbuff[j] == '.'
555 && ccline.cmdbuff[j - 1] != '\\')
556 {
557 if (found)
558 {
559 i = j + 1;
560 break;
561 }
562 else
563 found = TRUE;
564 }
565 }
566 if (i > 0)
567 cmdline_del(i);
568 c = p_wc;
569 xpc.xp_context = EXPAND_NOTHING;
570 }
571 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000572 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000573 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000574 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 {
576 char_u upseg[5];
577
578 upseg[0] = PATHSEP;
579 upseg[1] = '.';
580 upseg[2] = '.';
581 upseg[3] = PATHSEP;
582 upseg[4] = NUL;
583
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000584 if (c == K_DOWN
585 && ccline.cmdpos > 0
586 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
587 && (ccline.cmdpos < 3
588 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
590 {
591 /* go down a directory */
592 c = p_wc;
593 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000594 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 {
596 /* If in a direct ancestor, strip off one ../ to go down */
597 int found = FALSE;
598
599 j = ccline.cmdpos;
600 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
601 while (--j > i)
602 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000603#ifdef FEAT_MBYTE
604 if (has_mbyte)
605 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (vim_ispathsep(ccline.cmdbuff[j]))
608 {
609 found = TRUE;
610 break;
611 }
612 }
613 if (found
614 && ccline.cmdbuff[j - 1] == '.'
615 && ccline.cmdbuff[j - 2] == '.'
616 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
617 {
618 cmdline_del(j - 2);
619 c = p_wc;
620 }
621 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000622 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {
624 /* go up a directory */
625 int found = FALSE;
626
627 j = ccline.cmdpos - 1;
628 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
629 while (--j > i)
630 {
631#ifdef FEAT_MBYTE
632 if (has_mbyte)
633 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
634#endif
635 if (vim_ispathsep(ccline.cmdbuff[j])
636#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100637 && vim_strchr((char_u *)" *?[{`$%#",
638 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639#endif
640 )
641 {
642 if (found)
643 {
644 i = j + 1;
645 break;
646 }
647 else
648 found = TRUE;
649 }
650 }
651
652 if (!found)
653 j = i;
654 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
655 j += 4;
656 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
657 && j == i)
658 j += 3;
659 else
660 j = 0;
661 if (j > 0)
662 {
663 /* TODO this is only for DOS/UNIX systems - need to put in
664 * machine-specific stuff here and in upseg init */
665 cmdline_del(j);
666 put_on_cmdline(upseg + 1, 3, FALSE);
667 }
668 else if (ccline.cmdpos > i)
669 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100670
671 /* Now complete in the new directory. Set KeyTyped in case the
672 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100674 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 }
676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678#endif /* FEAT_WILDMENU */
679
680 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
681 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
682 if (c == Ctrl_BSL)
683 {
684 ++no_mapping;
685 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000686 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 --no_mapping;
688 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200689 /* CTRL-\ e doesn't work when obtaining an expression, unless it
690 * is in a mapping. */
691 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
692 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {
694 vungetc(c);
695 c = Ctrl_BSL;
696 }
697#ifdef FEAT_EVAL
698 else if (c == 'e')
699 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200700 char_u *p = NULL;
701 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702
703 /*
704 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000705 * Need to save and restore the current command line, to be
706 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 */
708 if (ccline.cmdpos == ccline.cmdlen)
709 new_cmdpos = 99999; /* keep it at the end */
710 else
711 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000712
713 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000715 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 if (c == '=')
717 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000718 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000719 * to avoid nasty things like going to another buffer when
720 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000721 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000722 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000724 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000725 restore_cmdline(&save_ccline);
726
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200727 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200729 len = (int)STRLEN(p);
730 if (realloc_cmdbuff(len + 1) == OK)
731 {
732 ccline.cmdlen = len;
733 STRCPY(ccline.cmdbuff, p);
734 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200736 /* Restore the cursor or use the position set with
737 * set_cmdline_pos(). */
738 if (new_cmdpos > ccline.cmdlen)
739 ccline.cmdpos = ccline.cmdlen;
740 else
741 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200743 KeyTyped = FALSE; /* Don't do p_wc completion. */
744 redrawcmd();
745 goto cmdline_changed;
746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 }
748 }
749 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100750 got_int = FALSE; /* don't abandon the command line */
751 did_emsg = FALSE;
752 emsg_on_display = FALSE;
753 redrawcmd();
754 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 }
756#endif
757 else
758 {
759 if (c == Ctrl_G && p_im && restart_edit == 0)
760 restart_edit = 'a';
761 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
762 in history */
763 goto returncmd; /* back to Normal mode */
764 }
765 }
766
767#ifdef FEAT_CMDWIN
768 if (c == cedit_key || c == K_CMDWIN)
769 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200770 if (ex_normal_busy == 0 && got_int == FALSE)
771 {
772 /*
773 * Open a window to edit the command line (and history).
774 */
Bram Moolenaarc695cec2017-01-08 20:00:04 +0100775 save_cmdline(&save_ccline);
Bram Moolenaar58da7072014-09-09 18:45:49 +0200776 c = ex_window();
Bram Moolenaarc695cec2017-01-08 20:00:04 +0100777 restore_cmdline(&save_ccline);
Bram Moolenaar58da7072014-09-09 18:45:49 +0200778 some_key_typed = TRUE;
779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 }
781# ifdef FEAT_DIGRAPHS
782 else
783# endif
784#endif
785#ifdef FEAT_DIGRAPHS
786 c = do_digraph(c);
787#endif
788
789 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
790 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
791 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000792 /* In Ex mode a backslash escapes a newline. */
793 if (exmode_active
794 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000795 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000796 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000797 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000799 if (c == K_KENTER)
800 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000802 else
803 {
804 gotesc = FALSE; /* Might have typed ESC previously, don't
805 truncate the cmdline now. */
806 if (ccheck_abbr(c + ABBR_OFF))
807 goto cmdline_changed;
808 if (!cmd_silent)
809 {
810 windgoto(msg_row, 0);
811 out_flush();
812 }
813 break;
814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816
817 /*
818 * Completion for 'wildchar' or 'wildcharm' key.
819 * - hitting <ESC> twice means: abandon command line.
820 * - wildcard expansion is only done when the 'wildchar' key is really
821 * typed, not when it comes from a macro
822 */
823 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
824 {
825 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
826 {
827 /* if 'wildmode' contains "list" may still need to list */
828 if (xpc.xp_numfiles > 1
829 && !did_wild_list
830 && (wim_flags[wim_index] & WIM_LIST))
831 {
832 (void)showmatches(&xpc, FALSE);
833 redrawcmd();
834 did_wild_list = TRUE;
835 }
836 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100837 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
838 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100840 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
841 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 else
843 res = OK; /* don't insert 'wildchar' now */
844 }
845 else /* typed p_wc first time */
846 {
847 wim_index = 0;
848 j = ccline.cmdpos;
849 /* if 'wildmode' first contains "longest", get longest
850 * common part */
851 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100852 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
853 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100855 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
856 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
858 /* if interrupted while completing, behave like it failed */
859 if (got_int)
860 {
861 (void)vpeekc(); /* remove <C-C> from input stream */
862 got_int = FALSE; /* don't abandon the command line */
863 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
864#ifdef FEAT_WILDMENU
865 xpc.xp_context = EXPAND_NOTHING;
866#endif
867 goto cmdline_changed;
868 }
869
870 /* when more than one match, and 'wildmode' first contains
871 * "list", or no change and 'wildmode' contains "longest,list",
872 * list all matches */
873 if (res == OK && xpc.xp_numfiles > 1)
874 {
875 /* a "longest" that didn't do anything is skipped (but not
876 * "list:longest") */
877 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
878 wim_index = 1;
879 if ((wim_flags[wim_index] & WIM_LIST)
880#ifdef FEAT_WILDMENU
881 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
882#endif
883 )
884 {
885 if (!(wim_flags[0] & WIM_LONGEST))
886 {
887#ifdef FEAT_WILDMENU
888 int p_wmnu_save = p_wmnu;
889 p_wmnu = 0;
890#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100891 /* remove match */
892 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#ifdef FEAT_WILDMENU
894 p_wmnu = p_wmnu_save;
895#endif
896 }
897#ifdef FEAT_WILDMENU
898 (void)showmatches(&xpc, p_wmnu
899 && ((wim_flags[wim_index] & WIM_LIST) == 0));
900#else
901 (void)showmatches(&xpc, FALSE);
902#endif
903 redrawcmd();
904 did_wild_list = TRUE;
905 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100906 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
907 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100909 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
910 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200913 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
915#ifdef FEAT_WILDMENU
916 else if (xpc.xp_numfiles == -1)
917 xpc.xp_context = EXPAND_NOTHING;
918#endif
919 }
920 if (wim_index < 3)
921 ++wim_index;
922 if (c == ESC)
923 gotesc = TRUE;
924 if (res == OK)
925 goto cmdline_changed;
926 }
927
928 gotesc = FALSE;
929
930 /* <S-Tab> goes to last match, in a clumsy way */
931 if (c == K_S_TAB && KeyTyped)
932 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100933 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
934 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
935 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 goto cmdline_changed;
937 }
938
939 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
940 c = NL;
941
942 do_abbr = TRUE; /* default: check for abbreviation */
943
944 /*
945 * Big switch for a typed command line character.
946 */
947 switch (c)
948 {
949 case K_BS:
950 case Ctrl_H:
951 case K_DEL:
952 case K_KDEL:
953 case Ctrl_W:
954#ifdef FEAT_FKMAP
955 if (cmd_fkmap && c == K_BS)
956 c = K_DEL;
957#endif
958 if (c == K_KDEL)
959 c = K_DEL;
960
961 /*
962 * delete current character is the same as backspace on next
963 * character, except at end of line
964 */
965 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
966 ++ccline.cmdpos;
967#ifdef FEAT_MBYTE
968 if (has_mbyte && c == K_DEL)
969 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
970 ccline.cmdbuff + ccline.cmdpos);
971#endif
972 if (ccline.cmdpos > 0)
973 {
974 char_u *p;
975
976 j = ccline.cmdpos;
977 p = ccline.cmdbuff + j;
978#ifdef FEAT_MBYTE
979 if (has_mbyte)
980 {
981 p = mb_prevptr(ccline.cmdbuff, p);
982 if (c == Ctrl_W)
983 {
984 while (p > ccline.cmdbuff && vim_isspace(*p))
985 p = mb_prevptr(ccline.cmdbuff, p);
986 i = mb_get_class(p);
987 while (p > ccline.cmdbuff && mb_get_class(p) == i)
988 p = mb_prevptr(ccline.cmdbuff, p);
989 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000990 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 }
992 }
993 else
994#endif
995 if (c == Ctrl_W)
996 {
997 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
998 --p;
999 i = vim_iswordc(p[-1]);
1000 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1001 && vim_iswordc(p[-1]) == i)
1002 --p;
1003 }
1004 else
1005 --p;
1006 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1007 ccline.cmdlen -= j - ccline.cmdpos;
1008 i = ccline.cmdpos;
1009 while (i < ccline.cmdlen)
1010 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1011
1012 /* Truncate at the end, required for multi-byte chars. */
1013 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001014#ifdef FEAT_SEARCH_EXTRA
1015 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001016 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001017 search_start = save_cursor;
1018 /* save view settings, so that the screen
1019 * won't be restored at the wrong position */
1020 old_curswant = init_curswant;
1021 old_leftcol = init_leftcol;
1022 old_topline = init_topline;
1023# ifdef FEAT_DIFF
1024 old_topfill = init_topfill;
1025# endif
1026 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001027 }
1028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 redrawcmd();
1030 }
1031 else if (ccline.cmdlen == 0 && c != Ctrl_W
1032 && ccline.cmdprompt == NULL && indent == 0)
1033 {
1034 /* In ex and debug mode it doesn't make sense to return. */
1035 if (exmode_active
1036#ifdef FEAT_EVAL
1037 || ccline.cmdfirstc == '>'
1038#endif
1039 )
1040 goto cmdline_not_changed;
1041
1042 vim_free(ccline.cmdbuff); /* no commandline to return */
1043 ccline.cmdbuff = NULL;
1044 if (!cmd_silent)
1045 {
1046#ifdef FEAT_RIGHTLEFT
1047 if (cmdmsg_rl)
1048 msg_col = Columns;
1049 else
1050#endif
1051 msg_col = 0;
1052 msg_putchar(' '); /* delete ':' */
1053 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001054#ifdef FEAT_SEARCH_EXTRA
1055 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001056 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 redraw_cmdline = TRUE;
1059 goto returncmd; /* back to cmd mode */
1060 }
1061 goto cmdline_changed;
1062
1063 case K_INS:
1064 case K_KINS:
1065#ifdef FEAT_FKMAP
1066 /* if Farsi mode set, we are in reverse insert mode -
1067 Do not change the mode */
1068 if (cmd_fkmap)
1069 beep_flush();
1070 else
1071#endif
1072 ccline.overstrike = !ccline.overstrike;
1073#ifdef CURSOR_SHAPE
1074 ui_cursor_shape(); /* may show different cursor shape */
1075#endif
1076 goto cmdline_not_changed;
1077
1078 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001079 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 {
1081 /* ":lmap" mappings exists, toggle use of mappings. */
1082 State ^= LANGMAP;
1083#ifdef USE_IM_CONTROL
1084 im_set_active(FALSE); /* Disable input method */
1085#endif
1086 if (b_im_ptr != NULL)
1087 {
1088 if (State & LANGMAP)
1089 *b_im_ptr = B_IMODE_LMAP;
1090 else
1091 *b_im_ptr = B_IMODE_NONE;
1092 }
1093 }
1094#ifdef USE_IM_CONTROL
1095 else
1096 {
1097 /* There are no ":lmap" mappings, toggle IM. When
1098 * 'imdisable' is set don't try getting the status, it's
1099 * always off. */
1100 if ((p_imdisable && b_im_ptr != NULL)
1101 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1102 {
1103 im_set_active(FALSE); /* Disable input method */
1104 if (b_im_ptr != NULL)
1105 *b_im_ptr = B_IMODE_NONE;
1106 }
1107 else
1108 {
1109 im_set_active(TRUE); /* Enable input method */
1110 if (b_im_ptr != NULL)
1111 *b_im_ptr = B_IMODE_IM;
1112 }
1113 }
1114#endif
1115 if (b_im_ptr != NULL)
1116 {
1117 if (b_im_ptr == &curbuf->b_p_iminsert)
1118 set_iminsert_global();
1119 else
1120 set_imsearch_global();
1121 }
1122#ifdef CURSOR_SHAPE
1123 ui_cursor_shape(); /* may show different cursor shape */
1124#endif
1125#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1126 /* Show/unshow value of 'keymap' in status lines later. */
1127 status_redraw_curbuf();
1128#endif
1129 goto cmdline_not_changed;
1130
1131/* case '@': only in very old vi */
1132 case Ctrl_U:
1133 /* delete all characters left of the cursor */
1134 j = ccline.cmdpos;
1135 ccline.cmdlen -= j;
1136 i = ccline.cmdpos = 0;
1137 while (i < ccline.cmdlen)
1138 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1139 /* Truncate at the end, required for multi-byte chars. */
1140 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001141#ifdef FEAT_SEARCH_EXTRA
1142 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001143 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 redrawcmd();
1146 goto cmdline_changed;
1147
1148#ifdef FEAT_CLIPBOARD
1149 case Ctrl_Y:
1150 /* Copy the modeless selection, if there is one. */
1151 if (clip_star.state != SELECT_CLEARED)
1152 {
1153 if (clip_star.state == SELECT_DONE)
1154 clip_copy_modeless_selection(TRUE);
1155 goto cmdline_not_changed;
1156 }
1157 break;
1158#endif
1159
1160 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1161 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001162 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001163 * ":normal" runs out of characters. */
1164 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001165 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 goto cmdline_not_changed;
1167
1168 gotesc = TRUE; /* will free ccline.cmdbuff after
1169 putting it in history */
1170 goto returncmd; /* back to cmd mode */
1171
1172 case Ctrl_R: /* insert register */
1173#ifdef USE_ON_FLY_SCROLL
1174 dont_scroll = TRUE; /* disallow scrolling here */
1175#endif
1176 putcmdline('"', TRUE);
1177 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001178 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 if (i == Ctrl_O)
1180 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1181 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001182 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 --no_mapping;
1184#ifdef FEAT_EVAL
1185 /*
1186 * Insert the result of an expression.
1187 * Need to save the current command line, to be able to enter
1188 * a new one...
1189 */
1190 new_cmdpos = -1;
1191 if (c == '=')
1192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1194 {
1195 beep_flush();
1196 c = ESC;
1197 }
1198 else
1199 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001200 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001202 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204 }
1205#endif
1206 if (c != ESC) /* use ESC to cancel inserting register */
1207 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001208 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001209
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001210#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001211 /* When there was a serious error abort getting the
1212 * command line. */
1213 if (aborting())
1214 {
1215 gotesc = TRUE; /* will free ccline.cmdbuff after
1216 putting it in history */
1217 goto returncmd; /* back to cmd mode */
1218 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 KeyTyped = FALSE; /* Don't do p_wc completion. */
1221#ifdef FEAT_EVAL
1222 if (new_cmdpos >= 0)
1223 {
1224 /* set_cmdline_pos() was used */
1225 if (new_cmdpos > ccline.cmdlen)
1226 ccline.cmdpos = ccline.cmdlen;
1227 else
1228 ccline.cmdpos = new_cmdpos;
1229 }
1230#endif
1231 }
1232 redrawcmd();
1233 goto cmdline_changed;
1234
1235 case Ctrl_D:
1236 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1237 break; /* Use ^D as normal char instead */
1238
1239 redrawcmd();
1240 continue; /* don't do incremental search now */
1241
1242 case K_RIGHT:
1243 case K_S_RIGHT:
1244 case K_C_RIGHT:
1245 do
1246 {
1247 if (ccline.cmdpos >= ccline.cmdlen)
1248 break;
1249 i = cmdline_charsize(ccline.cmdpos);
1250 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1251 break;
1252 ccline.cmdspos += i;
1253#ifdef FEAT_MBYTE
1254 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001255 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 + ccline.cmdpos);
1257 else
1258#endif
1259 ++ccline.cmdpos;
1260 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001261 while ((c == K_S_RIGHT || c == K_C_RIGHT
1262 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1264#ifdef FEAT_MBYTE
1265 if (has_mbyte)
1266 set_cmdspos_cursor();
1267#endif
1268 goto cmdline_not_changed;
1269
1270 case K_LEFT:
1271 case K_S_LEFT:
1272 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001273 if (ccline.cmdpos == 0)
1274 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 do
1276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 --ccline.cmdpos;
1278#ifdef FEAT_MBYTE
1279 if (has_mbyte) /* move to first byte of char */
1280 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1281 ccline.cmdbuff + ccline.cmdpos);
1282#endif
1283 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1284 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001285 while (ccline.cmdpos > 0
1286 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001287 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1289#ifdef FEAT_MBYTE
1290 if (has_mbyte)
1291 set_cmdspos_cursor();
1292#endif
1293 goto cmdline_not_changed;
1294
1295 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001296 /* Ignore mouse event or ex_window() result. */
1297 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
Bram Moolenaar4770d092006-01-12 23:22:24 +00001299#ifdef FEAT_GUI_W32
1300 /* On Win32 ignore <M-F4>, we get it when closing the window was
1301 * cancelled. */
1302 case K_F4:
1303 if (mod_mask == MOD_MASK_ALT)
1304 {
1305 redrawcmd(); /* somehow the cmdline is cleared */
1306 goto cmdline_not_changed;
1307 }
1308 break;
1309#endif
1310
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311#ifdef FEAT_MOUSE
1312 case K_MIDDLEDRAG:
1313 case K_MIDDLERELEASE:
1314 goto cmdline_not_changed; /* Ignore mouse */
1315
1316 case K_MIDDLEMOUSE:
1317# ifdef FEAT_GUI
1318 /* When GUI is active, also paste when 'mouse' is empty */
1319 if (!gui.in_use)
1320# endif
1321 if (!mouse_has(MOUSE_COMMAND))
1322 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001323# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001325 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001327# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001328 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 redrawcmd();
1330 goto cmdline_changed;
1331
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001332# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001334 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 redrawcmd();
1336 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 case K_LEFTDRAG:
1340 case K_LEFTRELEASE:
1341 case K_RIGHTDRAG:
1342 case K_RIGHTRELEASE:
1343 /* Ignore drag and release events when the button-down wasn't
1344 * seen before. */
1345 if (ignore_drag_release)
1346 goto cmdline_not_changed;
1347 /* FALLTHROUGH */
1348 case K_LEFTMOUSE:
1349 case K_RIGHTMOUSE:
1350 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1351 ignore_drag_release = TRUE;
1352 else
1353 ignore_drag_release = FALSE;
1354# ifdef FEAT_GUI
1355 /* When GUI is active, also move when 'mouse' is empty */
1356 if (!gui.in_use)
1357# endif
1358 if (!mouse_has(MOUSE_COMMAND))
1359 goto cmdline_not_changed; /* Ignore mouse */
1360# ifdef FEAT_CLIPBOARD
1361 if (mouse_row < cmdline_row && clip_star.available)
1362 {
1363 int button, is_click, is_drag;
1364
1365 /*
1366 * Handle modeless selection.
1367 */
1368 button = get_mouse_button(KEY2TERMCAP1(c),
1369 &is_click, &is_drag);
1370 if (mouse_model_popup() && button == MOUSE_LEFT
1371 && (mod_mask & MOD_MASK_SHIFT))
1372 {
1373 /* Translate shift-left to right button. */
1374 button = MOUSE_RIGHT;
1375 mod_mask &= ~MOD_MASK_SHIFT;
1376 }
1377 clip_modeless(button, is_click, is_drag);
1378 goto cmdline_not_changed;
1379 }
1380# endif
1381
1382 set_cmdspos();
1383 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1384 ++ccline.cmdpos)
1385 {
1386 i = cmdline_charsize(ccline.cmdpos);
1387 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1388 && mouse_col < ccline.cmdspos % Columns + i)
1389 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001390# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 if (has_mbyte)
1392 {
1393 /* Count ">" for double-wide char that doesn't fit. */
1394 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001395 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 + ccline.cmdpos) - 1;
1397 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 ccline.cmdspos += i;
1400 }
1401 goto cmdline_not_changed;
1402
1403 /* Mouse scroll wheel: ignored here */
1404 case K_MOUSEDOWN:
1405 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001406 case K_MOUSELEFT:
1407 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /* Alternate buttons ignored here */
1409 case K_X1MOUSE:
1410 case K_X1DRAG:
1411 case K_X1RELEASE:
1412 case K_X2MOUSE:
1413 case K_X2DRAG:
1414 case K_X2RELEASE:
1415 goto cmdline_not_changed;
1416
1417#endif /* FEAT_MOUSE */
1418
1419#ifdef FEAT_GUI
1420 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1421 case K_LEFTRELEASE_NM:
1422 goto cmdline_not_changed;
1423
1424 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001425 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 {
1427 gui_do_scroll();
1428 redrawcmd();
1429 }
1430 goto cmdline_not_changed;
1431
1432 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001433 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001435 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 redrawcmd();
1437 }
1438 goto cmdline_not_changed;
1439#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001440#ifdef FEAT_GUI_TABLINE
1441 case K_TABLINE:
1442 case K_TABMENU:
1443 /* Don't want to change any tabs here. Make sure the same tab
1444 * is still selected. */
1445 if (gui_use_tabline())
1446 gui_mch_set_curtab(tabpage_index(curtab));
1447 goto cmdline_not_changed;
1448#endif
1449
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 case K_SELECT: /* end of Select mode mapping - ignore */
1451 goto cmdline_not_changed;
1452
1453 case Ctrl_B: /* begin of command line */
1454 case K_HOME:
1455 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 case K_S_HOME:
1457 case K_C_HOME:
1458 ccline.cmdpos = 0;
1459 set_cmdspos();
1460 goto cmdline_not_changed;
1461
1462 case Ctrl_E: /* end of command line */
1463 case K_END:
1464 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 case K_S_END:
1466 case K_C_END:
1467 ccline.cmdpos = ccline.cmdlen;
1468 set_cmdspos_cursor();
1469 goto cmdline_not_changed;
1470
1471 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001472 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 break;
1474 goto cmdline_changed;
1475
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001476 case Ctrl_L:
1477#ifdef FEAT_SEARCH_EXTRA
1478 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1479 {
1480 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001481 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001482 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001483 curwin->w_cursor = match_end;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001484 if (!equalpos(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001485 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001486 c = gchar_cursor();
1487 /* If 'ignorecase' and 'smartcase' are set and the
1488 * command line has no uppercase characters, convert
1489 * the character to lowercase */
1490 if (p_ic && p_scs
1491 && !pat_has_uppercase(ccline.cmdbuff))
1492 c = MB_TOLOWER(c);
1493 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001494 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001495 if (c == firstc || vim_strchr((char_u *)(
1496 p_magic ? "\\^$.*[" : "\\^$"), c)
1497 != NULL)
1498 {
1499 /* put a backslash before special
1500 * characters */
1501 stuffcharReadbuff(c);
1502 c = '\\';
1503 }
1504 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001505 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001506 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001507 }
1508 goto cmdline_not_changed;
1509 }
1510#endif
1511
1512 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001513 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 break;
1515 goto cmdline_changed;
1516
1517 case Ctrl_N: /* next match */
1518 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001519 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001521 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1522 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001524 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
Bram Moolenaar11956692016-08-27 16:26:56 +02001526 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
1528#ifdef FEAT_CMDHIST
1529 case K_UP:
1530 case K_DOWN:
1531 case K_S_UP:
1532 case K_S_DOWN:
1533 case K_PAGEUP:
1534 case K_KPAGEUP:
1535 case K_PAGEDOWN:
1536 case K_KPAGEDOWN:
1537 if (hislen == 0 || firstc == NUL) /* no history */
1538 goto cmdline_not_changed;
1539
1540 i = hiscnt;
1541
1542 /* save current command string so it can be restored later */
1543 if (lookfor == NULL)
1544 {
1545 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1546 goto cmdline_not_changed;
1547 lookfor[ccline.cmdpos] = NUL;
1548 }
1549
1550 j = (int)STRLEN(lookfor);
1551 for (;;)
1552 {
1553 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001554 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001555 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
1557 if (hiscnt == hislen) /* first time */
1558 hiscnt = hisidx[histype];
1559 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1560 hiscnt = hislen - 1;
1561 else if (hiscnt != hisidx[histype] + 1)
1562 --hiscnt;
1563 else /* at top of list */
1564 {
1565 hiscnt = i;
1566 break;
1567 }
1568 }
1569 else /* one step forwards */
1570 {
1571 /* on last entry, clear the line */
1572 if (hiscnt == hisidx[histype])
1573 {
1574 hiscnt = hislen;
1575 break;
1576 }
1577
1578 /* not on a history line, nothing to do */
1579 if (hiscnt == hislen)
1580 break;
1581 if (hiscnt == hislen - 1) /* wrap around */
1582 hiscnt = 0;
1583 else
1584 ++hiscnt;
1585 }
1586 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1587 {
1588 hiscnt = i;
1589 break;
1590 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001591 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001592 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 || STRNCMP(history[histype][hiscnt].hisstr,
1594 lookfor, (size_t)j) == 0)
1595 break;
1596 }
1597
1598 if (hiscnt != i) /* jumped to other entry */
1599 {
1600 char_u *p;
1601 int len;
1602 int old_firstc;
1603
1604 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001605 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 if (hiscnt == hislen)
1607 p = lookfor; /* back to the old one */
1608 else
1609 p = history[histype][hiscnt].hisstr;
1610
1611 if (histype == HIST_SEARCH
1612 && p != lookfor
1613 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1614 {
1615 /* Correct for the separator character used when
1616 * adding the history entry vs the one used now.
1617 * First loop: count length.
1618 * Second loop: copy the characters. */
1619 for (i = 0; i <= 1; ++i)
1620 {
1621 len = 0;
1622 for (j = 0; p[j] != NUL; ++j)
1623 {
1624 /* Replace old sep with new sep, unless it is
1625 * escaped. */
1626 if (p[j] == old_firstc
1627 && (j == 0 || p[j - 1] != '\\'))
1628 {
1629 if (i > 0)
1630 ccline.cmdbuff[len] = firstc;
1631 }
1632 else
1633 {
1634 /* Escape new sep, unless it is already
1635 * escaped. */
1636 if (p[j] == firstc
1637 && (j == 0 || p[j - 1] != '\\'))
1638 {
1639 if (i > 0)
1640 ccline.cmdbuff[len] = '\\';
1641 ++len;
1642 }
1643 if (i > 0)
1644 ccline.cmdbuff[len] = p[j];
1645 }
1646 ++len;
1647 }
1648 if (i == 0)
1649 {
1650 alloc_cmdbuff(len);
1651 if (ccline.cmdbuff == NULL)
1652 goto returncmd;
1653 }
1654 }
1655 ccline.cmdbuff[len] = NUL;
1656 }
1657 else
1658 {
1659 alloc_cmdbuff((int)STRLEN(p));
1660 if (ccline.cmdbuff == NULL)
1661 goto returncmd;
1662 STRCPY(ccline.cmdbuff, p);
1663 }
1664
1665 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1666 redrawcmd();
1667 goto cmdline_changed;
1668 }
1669 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001670#endif
1671 goto cmdline_not_changed;
1672
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001673#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001674 case Ctrl_G: /* next match */
1675 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001676 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1677 {
1678 pos_T t;
1679 int search_flags = SEARCH_KEEP + SEARCH_NOOF
1680 + SEARCH_PEEK;
1681
1682 if (char_avail())
1683 continue;
1684 cursor_off();
1685 out_flush();
1686 if (c == Ctrl_G)
1687 {
1688 t = match_end;
1689 search_flags += SEARCH_COL;
1690 }
1691 else
1692 t = match_start;
1693 ++emsg_off;
1694 i = searchit(curwin, curbuf, &t,
1695 c == Ctrl_G ? FORWARD : BACKWARD,
1696 ccline.cmdbuff, count, search_flags,
1697 RE_SEARCH, 0, NULL);
1698 --emsg_off;
1699 if (i)
1700 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001701 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001702 match_end = t;
1703 match_start = t;
1704 if (c == Ctrl_T && firstc == '/')
1705 {
1706 /* move just before the current match, so that
1707 * when nv_search finishes the cursor will be
1708 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001709 search_start = t;
1710 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001711 }
Bram Moolenaardda933d2016-09-03 21:04:58 +02001712 if (lt(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001713 {
1714 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001715 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001716 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001717 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001718 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001719 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001720 }
1721
1722 set_search_match(&match_end);
1723 curwin->w_cursor = match_start;
1724 changed_cline_bef_curs();
1725 update_topline();
1726 validate_cursor();
1727 highlight_match = TRUE;
1728 old_curswant = curwin->w_curswant;
1729 old_leftcol = curwin->w_leftcol;
1730 old_topline = curwin->w_topline;
1731# ifdef FEAT_DIFF
1732 old_topfill = curwin->w_topfill;
1733# endif
1734 old_botline = curwin->w_botline;
1735 update_screen(NOT_VALID);
1736 redrawcmdline();
1737 }
1738 else
1739 vim_beep(BO_ERROR);
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001740 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001741 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001742 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
1744
1745 case Ctrl_V:
1746 case Ctrl_Q:
1747#ifdef FEAT_MOUSE
1748 ignore_drag_release = TRUE;
1749#endif
1750 putcmdline('^', TRUE);
1751 c = get_literal(); /* get next (two) character(s) */
1752 do_abbr = FALSE; /* don't do abbreviation now */
1753#ifdef FEAT_MBYTE
1754 /* may need to remove ^ when composing char was typed */
1755 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1756 {
1757 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1758 msg_putchar(' ');
1759 cursorcmd();
1760 }
1761#endif
1762 break;
1763
1764#ifdef FEAT_DIGRAPHS
1765 case Ctrl_K:
1766#ifdef FEAT_MOUSE
1767 ignore_drag_release = TRUE;
1768#endif
1769 putcmdline('?', TRUE);
1770#ifdef USE_ON_FLY_SCROLL
1771 dont_scroll = TRUE; /* disallow scrolling here */
1772#endif
1773 c = get_digraph(TRUE);
1774 if (c != NUL)
1775 break;
1776
1777 redrawcmd();
1778 goto cmdline_not_changed;
1779#endif /* FEAT_DIGRAPHS */
1780
1781#ifdef FEAT_RIGHTLEFT
1782 case Ctrl__: /* CTRL-_: switch language mode */
1783 if (!p_ari)
1784 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001785# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 if (p_altkeymap)
1787 {
1788 cmd_fkmap = !cmd_fkmap;
1789 if (cmd_fkmap) /* in Farsi always in Insert mode */
1790 ccline.overstrike = FALSE;
1791 }
1792 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 cmd_hkmap = !cmd_hkmap;
1795 goto cmdline_not_changed;
1796#endif
1797
1798 default:
1799#ifdef UNIX
1800 if (c == intr_char)
1801 {
1802 gotesc = TRUE; /* will free ccline.cmdbuff after
1803 putting it in history */
1804 goto returncmd; /* back to Normal mode */
1805 }
1806#endif
1807 /*
1808 * Normal character with no special meaning. Just set mod_mask
1809 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1810 * the string <S-Space>. This should only happen after ^V.
1811 */
1812 if (!IS_SPECIAL(c))
1813 mod_mask = 0x0;
1814 break;
1815 }
1816 /*
1817 * End of switch on command line character.
1818 * We come here if we have a normal character.
1819 */
1820
Bram Moolenaarede3e632013-06-23 16:16:19 +02001821 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#ifdef FEAT_MBYTE
1823 /* Add ABBR_OFF for characters above 0x100, this is
1824 * what check_abbr() expects. */
1825 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1826#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001827 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 goto cmdline_changed;
1829
1830 /*
1831 * put the character in the command line
1832 */
1833 if (IS_SPECIAL(c) || mod_mask != 0)
1834 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1835 else
1836 {
1837#ifdef FEAT_MBYTE
1838 if (has_mbyte)
1839 {
1840 j = (*mb_char2bytes)(c, IObuff);
1841 IObuff[j] = NUL; /* exclude composing chars */
1842 put_on_cmdline(IObuff, j, TRUE);
1843 }
1844 else
1845#endif
1846 {
1847 IObuff[0] = c;
1848 put_on_cmdline(IObuff, 1, TRUE);
1849 }
1850 }
1851 goto cmdline_changed;
1852
1853/*
1854 * This part implements incremental searches for "/" and "?"
1855 * Jump to cmdline_not_changed when a character has been read but the command
1856 * line did not change. Then we only search and redraw if something changed in
1857 * the past.
1858 * Jump to cmdline_changed when the command line did change.
1859 * (Sorry for the goto's, I know it is ugly).
1860 */
1861cmdline_not_changed:
1862#ifdef FEAT_SEARCH_EXTRA
1863 if (!incsearch_postponed)
1864 continue;
1865#endif
1866
1867cmdline_changed:
1868#ifdef FEAT_SEARCH_EXTRA
1869 /*
1870 * 'incsearch' highlighting.
1871 */
1872 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1873 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001874 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001875#ifdef FEAT_RELTIME
1876 proftime_T tm;
1877#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 /* if there is a character waiting, search and redraw later */
1880 if (char_avail())
1881 {
1882 incsearch_postponed = TRUE;
1883 continue;
1884 }
1885 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001886 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
1888 /* If there is no command line, don't do anything */
1889 if (ccline.cmdlen == 0)
1890 i = 0;
1891 else
1892 {
1893 cursor_off(); /* so the user knows we're busy */
1894 out_flush();
1895 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001896#ifdef FEAT_RELTIME
1897 /* Set the time limit to half a second. */
1898 profile_setlimit(500L, &tm);
1899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001901 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1902#ifdef FEAT_RELTIME
1903 &tm
1904#else
1905 NULL
1906#endif
1907 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 --emsg_off;
1909 /* if interrupted while searching, behave like it failed */
1910 if (got_int)
1911 {
1912 (void)vpeekc(); /* remove <C-C> from input stream */
1913 got_int = FALSE; /* don't abandon the command line */
1914 i = 0;
1915 }
1916 else if (char_avail())
1917 /* cancelled searching because a char was typed */
1918 incsearch_postponed = TRUE;
1919 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001920 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 highlight_match = TRUE; /* highlight position */
1922 else
1923 highlight_match = FALSE; /* remove highlight */
1924
1925 /* first restore the old curwin values, so the screen is
1926 * positioned in the same way as the actual search command */
1927 curwin->w_leftcol = old_leftcol;
1928 curwin->w_topline = old_topline;
1929# ifdef FEAT_DIFF
1930 curwin->w_topfill = old_topfill;
1931# endif
1932 curwin->w_botline = old_botline;
1933 changed_cline_bef_curs();
1934 update_topline();
1935
1936 if (i != 0)
1937 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001938 pos_T save_pos = curwin->w_cursor;
1939
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001940 match_start = curwin->w_cursor;
1941 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001943 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001944 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001945 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001947 else
1948 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001951# ifdef FEAT_WINDOWS
1952 /* May redraw the status line to show the cursor position. */
1953 if (p_ru && curwin->w_status_height > 0)
1954 curwin->w_redr_status = TRUE;
1955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001957 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001958 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001959 restore_cmdline(&save_ccline);
1960
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001961 /* Leave it at the end to make CTRL-R CTRL-W work. */
1962 if (i != 0)
1963 curwin->w_cursor = end_pos;
1964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 msg_starthere();
1966 redrawcmdline();
1967 did_incsearch = TRUE;
1968 }
1969#else /* FEAT_SEARCH_EXTRA */
1970 ;
1971#endif
1972
1973#ifdef FEAT_RIGHTLEFT
1974 if (cmdmsg_rl
1975# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001976 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977# endif
1978 )
1979 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001980 * right-left typing. Not efficient, but it works.
1981 * Do it only when there are no characters left to read
1982 * to avoid useless intermediate redraws. */
1983 if (vpeekc() == NUL)
1984 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985#endif
1986 }
1987
1988returncmd:
1989
1990#ifdef FEAT_RIGHTLEFT
1991 cmdmsg_rl = FALSE;
1992#endif
1993
1994#ifdef FEAT_FKMAP
1995 cmd_fkmap = 0;
1996#endif
1997
1998 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001999 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001#ifdef FEAT_SEARCH_EXTRA
2002 if (did_incsearch)
2003 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002004 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002005 curwin->w_cursor = save_cursor;
2006 else
2007 {
2008 if (!equalpos(save_cursor, search_start))
2009 {
2010 /* put the '" mark at the original position */
2011 curwin->w_cursor = save_cursor;
2012 setpcmark();
2013 }
2014 curwin->w_cursor = search_start;
2015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 curwin->w_curswant = old_curswant;
2017 curwin->w_leftcol = old_leftcol;
2018 curwin->w_topline = old_topline;
2019# ifdef FEAT_DIFF
2020 curwin->w_topfill = old_topfill;
2021# endif
2022 curwin->w_botline = old_botline;
2023 highlight_match = FALSE;
2024 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002025 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027#endif
2028
2029 if (ccline.cmdbuff != NULL)
2030 {
2031 /*
2032 * Put line in history buffer (":" and "=" only when it was typed).
2033 */
2034#ifdef FEAT_CMDHIST
2035 if (ccline.cmdlen && firstc != NUL
2036 && (some_key_typed || histype == HIST_SEARCH))
2037 {
2038 add_to_history(histype, ccline.cmdbuff, TRUE,
2039 histype == HIST_SEARCH ? firstc : NUL);
2040 if (firstc == ':')
2041 {
2042 vim_free(new_last_cmdline);
2043 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2044 }
2045 }
2046#endif
2047
2048 if (gotesc) /* abandon command line */
2049 {
2050 vim_free(ccline.cmdbuff);
2051 ccline.cmdbuff = NULL;
2052 if (msg_scrolled == 0)
2053 compute_cmdrow();
2054 MSG("");
2055 redraw_cmdline = TRUE;
2056 }
2057 }
2058
2059 /*
2060 * If the screen was shifted up, redraw the whole screen (later).
2061 * If the line is too long, clear it, so ruler and shown command do
2062 * not get printed in the middle of it.
2063 */
2064 msg_check();
2065 msg_scroll = save_msg_scroll;
2066 redir_off = FALSE;
2067
2068 /* When the command line was typed, no need for a wait-return prompt. */
2069 if (some_key_typed)
2070 need_wait_return = FALSE;
2071
2072 State = save_State;
2073#ifdef USE_IM_CONTROL
2074 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2075 im_save_status(b_im_ptr);
2076 im_set_active(FALSE);
2077#endif
2078#ifdef FEAT_MOUSE
2079 setmouse();
2080#endif
2081#ifdef CURSOR_SHAPE
2082 ui_cursor_shape(); /* may show different cursor shape */
2083#endif
2084
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002085 {
2086 char_u *p = ccline.cmdbuff;
2087
2088 /* Make ccline empty, getcmdline() may try to use it. */
2089 ccline.cmdbuff = NULL;
2090 return p;
2091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092}
2093
2094#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2095/*
2096 * Get a command line with a prompt.
2097 * This is prepared to be called recursively from getcmdline() (e.g. by
2098 * f_input() when evaluating an expression from CTRL-R =).
2099 * Returns the command line in allocated memory, or NULL.
2100 */
2101 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002102getcmdline_prompt(
2103 int firstc,
2104 char_u *prompt, /* command line prompt */
2105 int attr, /* attributes for prompt */
2106 int xp_context, /* type of expansion */
2107 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
2109 char_u *s;
2110 struct cmdline_info save_ccline;
2111 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002112 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002114 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 ccline.cmdprompt = prompt;
2116 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002117# ifdef FEAT_EVAL
2118 ccline.xp_context = xp_context;
2119 ccline.xp_arg = xp_arg;
2120 ccline.input_fn = (firstc == '@');
2121# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002122 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002124 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002125 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002126 /* Restore msg_col, the prompt from input() may have changed it.
2127 * But only if called recursively and the commandline is therefore being
2128 * restored to an old one; if not, the input() prompt stays on the screen,
2129 * so we need its modified msg_col left intact. */
2130 if (ccline.cmdbuff != NULL)
2131 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
2133 return s;
2134}
2135#endif
2136
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002137/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002138 * Return TRUE when the text must not be changed and we can't switch to
2139 * another window or buffer. Used when editing the command line, evaluating
2140 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002141 */
2142 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002143text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002144{
2145#ifdef FEAT_CMDWIN
2146 if (cmdwin_type != 0)
2147 return TRUE;
2148#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002149 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002150}
2151
2152/*
2153 * Give an error message for a command that isn't allowed while the cmdline
2154 * window is open or editing the cmdline in another way.
2155 */
2156 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002157text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002158{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002159 EMSG(_(get_text_locked_msg()));
2160}
2161
2162 char_u *
2163get_text_locked_msg(void)
2164{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002165#ifdef FEAT_CMDWIN
2166 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002167 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002168#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002169 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002170}
2171
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002172#if defined(FEAT_AUTOCMD) || defined(PROTO)
2173/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002174 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2175 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002176 */
2177 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002178curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002179{
2180 if (curbuf_lock > 0)
2181 {
2182 EMSG(_("E788: Not allowed to edit another buffer now"));
2183 return TRUE;
2184 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002185 return allbuf_locked();
2186}
2187
2188/*
2189 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2190 * message.
2191 */
2192 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002193allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002194{
2195 if (allbuf_lock > 0)
2196 {
2197 EMSG(_("E811: Not allowed to change buffer information now"));
2198 return TRUE;
2199 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002200 return FALSE;
2201}
2202#endif
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002205cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206{
2207#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2208 if (cmdline_star > 0) /* showing '*', always 1 position */
2209 return 1;
2210#endif
2211 return ptr2cells(ccline.cmdbuff + idx);
2212}
2213
2214/*
2215 * Compute the offset of the cursor on the command line for the prompt and
2216 * indent.
2217 */
2218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002219set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002221 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 ccline.cmdspos = 1 + ccline.cmdindent;
2223 else
2224 ccline.cmdspos = 0 + ccline.cmdindent;
2225}
2226
2227/*
2228 * Compute the screen position for the cursor on the command line.
2229 */
2230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002231set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
2233 int i, m, c;
2234
2235 set_cmdspos();
2236 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002239 if (m < 0) /* overflow, Columns or Rows at weird value */
2240 m = MAXCOL;
2241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 else
2243 m = MAXCOL;
2244 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2245 {
2246 c = cmdline_charsize(i);
2247#ifdef FEAT_MBYTE
2248 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2249 if (has_mbyte)
2250 correct_cmdspos(i, c);
2251#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002252 /* If the cmdline doesn't fit, show cursor on last visible char.
2253 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 if ((ccline.cmdspos += c) >= m)
2255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 ccline.cmdspos -= c;
2257 break;
2258 }
2259#ifdef FEAT_MBYTE
2260 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002261 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#endif
2263 }
2264}
2265
2266#ifdef FEAT_MBYTE
2267/*
2268 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2269 * character that doesn't fit, so that a ">" must be displayed.
2270 */
2271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002272correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002274 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2276 && ccline.cmdspos % Columns + cells > Columns)
2277 ccline.cmdspos++;
2278}
2279#endif
2280
2281/*
2282 * Get an Ex command line for the ":" command.
2283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002285getexline(
2286 int c, /* normally ':', NUL for ":append" */
2287 void *cookie UNUSED,
2288 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289{
2290 /* When executing a register, remove ':' that's in front of each line. */
2291 if (exec_from_reg && vpeekc() == ':')
2292 (void)vgetc();
2293 return getcmdline(c, 1L, indent);
2294}
2295
2296/*
2297 * Get an Ex command line for Ex mode.
2298 * In Ex mode we only use the OS supplied line editing features and no
2299 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002300 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002303getexmodeline(
2304 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002305 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002306 void *cookie UNUSED,
2307 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002309 garray_T line_ga;
2310 char_u *pend;
2311 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002312 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002313 int escaped = FALSE; /* CTRL-V typed */
2314 int vcol = 0;
2315 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002316 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002317 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318
2319 /* Switch cursor on now. This avoids that it happens after the "\n", which
2320 * confuses the system function that computes tabstops. */
2321 cursor_on();
2322
2323 /* always start in column 0; write a newline if necessary */
2324 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002325 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002327 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002329 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002330 if (p_prompt)
2331 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 while (indent-- > 0)
2333 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336
2337 ga_init2(&line_ga, 1, 30);
2338
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002339 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002340 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002341 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002342 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002343 while (indent >= 8)
2344 {
2345 ga_append(&line_ga, TAB);
2346 msg_puts((char_u *)" ");
2347 indent -= 8;
2348 }
2349 while (indent-- > 0)
2350 {
2351 ga_append(&line_ga, ' ');
2352 msg_putchar(' ');
2353 }
2354 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002355 ++no_mapping;
2356 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002357
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 /*
2359 * Get the line, one character at a time.
2360 */
2361 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002362 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002364 long sw;
2365 char_u *s;
2366
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 if (ga_grow(&line_ga, 40) == FAIL)
2368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002370 /* Get one character at a time. Don't use inchar(), it can't handle
2371 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002372 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002373 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374
2375 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002376 * Handle line editing.
2377 * Previously this was left to the system, putting the terminal in
2378 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002380 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002382 msg_putchar('\n');
2383 break;
2384 }
2385
2386 if (!escaped)
2387 {
2388 /* CR typed means "enter", which is NL */
2389 if (c1 == '\r')
2390 c1 = '\n';
2391
2392 if (c1 == BS || c1 == K_BS
2393 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002395 if (line_ga.ga_len > 0)
2396 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002397#ifdef FEAT_MBYTE
2398 if (has_mbyte)
2399 {
2400 p = (char_u *)line_ga.ga_data;
2401 p[line_ga.ga_len] = NUL;
2402 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2403 line_ga.ga_len -= len;
2404 }
2405 else
2406#endif
2407 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002408 goto redraw;
2409 }
2410 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002413 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002415 msg_col = startcol;
2416 msg_clr_eos();
2417 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002418 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002419 }
2420
2421 if (c1 == Ctrl_T)
2422 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002423 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002424 p = (char_u *)line_ga.ga_data;
2425 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002426 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002427 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002428add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002429 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002431 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002432 p = (char_u *)line_ga.ga_data;
2433 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002434 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2435 *s = ' ';
2436 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002438redraw:
2439 /* redraw the line */
2440 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002441 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002442 p = (char_u *)line_ga.ga_data;
2443 p[line_ga.ga_len] = NUL;
2444 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002446 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002448 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002450 msg_putchar(' ');
2451 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002452 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002454 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002456 len = MB_PTR2LEN(p);
2457 msg_outtrans_len(p, len);
2458 vcol += ptr2cells(p);
2459 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
2461 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002462 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002463 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002464 continue;
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002467 if (c1 == Ctrl_D)
2468 {
2469 /* Delete one shiftwidth. */
2470 p = (char_u *)line_ga.ga_data;
2471 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002473 if (prev_char == '^')
2474 ex_keep_indent = TRUE;
2475 indent = 0;
2476 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
2478 else
2479 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002480 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002481 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002482 if (indent > 0)
2483 {
2484 --indent;
2485 indent -= indent % get_sw_value(curbuf);
2486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002488 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002489 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002490 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002491 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2492 --line_ga.ga_len;
2493 }
2494 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002496
2497 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2498 {
2499 escaped = TRUE;
2500 continue;
2501 }
2502
2503 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2504 if (IS_SPECIAL(c1))
2505 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002507
2508 if (IS_SPECIAL(c1))
2509 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002510#ifdef FEAT_MBYTE
2511 if (has_mbyte)
2512 len = (*mb_char2bytes)(c1,
2513 (char_u *)line_ga.ga_data + line_ga.ga_len);
2514 else
2515#endif
2516 {
2517 len = 1;
2518 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2519 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002520 if (c1 == '\n')
2521 msg_putchar('\n');
2522 else if (c1 == TAB)
2523 {
2524 /* Don't use chartabsize(), 'ts' can be different */
2525 do
2526 {
2527 msg_putchar(' ');
2528 } while (++vcol % 8);
2529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002532 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002533 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002534 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002536 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002537 escaped = FALSE;
2538
2539 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002540 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002541
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002542 /* We are done when a NL is entered, but not when it comes after an
2543 * odd number of backslashes, that results in a NUL. */
2544 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002546 int bcount = 0;
2547
2548 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2549 ++bcount;
2550
2551 if (bcount > 0)
2552 {
2553 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2554 * "\NL", etc. */
2555 line_ga.ga_len -= (bcount + 1) / 2;
2556 pend -= (bcount + 1) / 2;
2557 pend[-1] = '\n';
2558 }
2559
2560 if ((bcount & 1) == 0)
2561 {
2562 --line_ga.ga_len;
2563 --pend;
2564 *pend = NUL;
2565 break;
2566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
2568 }
2569
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002570 --no_mapping;
2571 --allow_keys;
2572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 /* make following messages go to the next line */
2574 msg_didout = FALSE;
2575 msg_col = 0;
2576 if (msg_row < Rows - 1)
2577 ++msg_row;
2578 emsg_on_display = FALSE; /* don't want ui_delay() */
2579
2580 if (got_int)
2581 ga_clear(&line_ga);
2582
2583 return (char_u *)line_ga.ga_data;
2584}
2585
Bram Moolenaare344bea2005-09-01 20:46:49 +00002586# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2587 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588/*
2589 * Return TRUE if ccline.overstrike is on.
2590 */
2591 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002592cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593{
2594 return ccline.overstrike;
2595}
2596
2597/*
2598 * Return TRUE if the cursor is at the end of the cmdline.
2599 */
2600 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002601cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
2603 return (ccline.cmdpos >= ccline.cmdlen);
2604}
2605#endif
2606
Bram Moolenaar9372a112005-12-06 19:59:18 +00002607#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608/*
2609 * Return the virtual column number at the current cursor position.
2610 * This is used by the IM code to obtain the start of the preedit string.
2611 */
2612 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002613cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2616 return MAXCOL;
2617
2618# ifdef FEAT_MBYTE
2619 if (has_mbyte)
2620 {
2621 colnr_T col;
2622 int i = 0;
2623
2624 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002625 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626
2627 return col;
2628 }
2629 else
2630# endif
2631 return ccline.cmdpos;
2632}
2633#endif
2634
2635#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2636/*
2637 * If part of the command line is an IM preedit string, redraw it with
2638 * IM feedback attributes. The cursor position is restored after drawing.
2639 */
2640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002641redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642{
2643 if ((State & CMDLINE)
2644 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002645 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 && !p_imdisable
2647 && im_is_preediting())
2648 {
2649 int cmdpos = 0;
2650 int cmdspos;
2651 int old_row;
2652 int old_col;
2653 colnr_T col;
2654
2655 old_row = msg_row;
2656 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002657 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659# ifdef FEAT_MBYTE
2660 if (has_mbyte)
2661 {
2662 for (col = 0; col < preedit_start_col
2663 && cmdpos < ccline.cmdlen; ++col)
2664 {
2665 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002666 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668 }
2669 else
2670# endif
2671 {
2672 cmdspos += preedit_start_col;
2673 cmdpos += preedit_start_col;
2674 }
2675
2676 msg_row = cmdline_row + (cmdspos / (int)Columns);
2677 msg_col = cmdspos % (int)Columns;
2678 if (msg_row >= Rows)
2679 msg_row = Rows - 1;
2680
2681 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2682 {
2683 int char_len;
2684 int char_attr;
2685
2686 char_attr = im_get_feedback_attr(col);
2687 if (char_attr < 0)
2688 break; /* end of preedit string */
2689
2690# ifdef FEAT_MBYTE
2691 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002692 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 else
2694# endif
2695 char_len = 1;
2696
2697 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2698 cmdpos += char_len;
2699 }
2700
2701 msg_row = old_row;
2702 msg_col = old_col;
2703 }
2704}
2705#endif /* FEAT_XIM && FEAT_GUI_GTK */
2706
2707/*
2708 * Allocate a new command line buffer.
2709 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2710 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2711 */
2712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002713alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714{
2715 /*
2716 * give some extra space to avoid having to allocate all the time
2717 */
2718 if (len < 80)
2719 len = 100;
2720 else
2721 len += 20;
2722
2723 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2724 ccline.cmdbufflen = len;
2725}
2726
2727/*
2728 * Re-allocate the command line to length len + something extra.
2729 * return FAIL for failure, OK otherwise
2730 */
2731 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002732realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
2734 char_u *p;
2735
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002736 if (len < ccline.cmdbufflen)
2737 return OK; /* no need to resize */
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 p = ccline.cmdbuff;
2740 alloc_cmdbuff(len); /* will get some more */
2741 if (ccline.cmdbuff == NULL) /* out of memory */
2742 {
2743 ccline.cmdbuff = p; /* keep the old one */
2744 return FAIL;
2745 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002746 /* There isn't always a NUL after the command, but it may need to be
2747 * there, thus copy up to the NUL and add a NUL. */
2748 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2749 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002751
2752 if (ccline.xpc != NULL
2753 && ccline.xpc->xp_pattern != NULL
2754 && ccline.xpc->xp_context != EXPAND_NOTHING
2755 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2756 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002757 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002758
2759 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2760 * to point into the newly allocated memory. */
2761 if (i >= 0 && i <= ccline.cmdlen)
2762 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2763 }
2764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 return OK;
2766}
2767
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002768#if defined(FEAT_ARABIC) || defined(PROTO)
2769static char_u *arshape_buf = NULL;
2770
2771# if defined(EXITFREE) || defined(PROTO)
2772 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002773free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002774{
2775 vim_free(arshape_buf);
2776}
2777# endif
2778#endif
2779
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780/*
2781 * Draw part of the cmdline at the current cursor position. But draw stars
2782 * when cmdline_star is TRUE.
2783 */
2784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002785draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786{
2787#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2788 int i;
2789
2790 if (cmdline_star > 0)
2791 for (i = 0; i < len; ++i)
2792 {
2793 msg_putchar('*');
2794# ifdef FEAT_MBYTE
2795 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002796 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797# endif
2798 }
2799 else
2800#endif
2801#ifdef FEAT_ARABIC
2802 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 static int buflen = 0;
2805 char_u *p;
2806 int j;
2807 int newlen = 0;
2808 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002809 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 int prev_c = 0;
2811 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002812 int u8c;
2813 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815
2816 /*
2817 * Do arabic shaping into a temporary buffer. This is very
2818 * inefficient!
2819 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002820 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
2822 /* Re-allocate the buffer. We keep it around to avoid a lot of
2823 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002824 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002825 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002826 arshape_buf = alloc(buflen);
2827 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 return; /* out of memory */
2829 }
2830
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002831 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2832 {
2833 /* Prepend a space to draw the leading composing char on. */
2834 arshape_buf[0] = ' ';
2835 newlen = 1;
2836 }
2837
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 for (j = start; j < start + len; j += mb_l)
2839 {
2840 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002841 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002842 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 if (ARABIC_CHAR(u8c))
2844 {
2845 /* Do Arabic shaping. */
2846 if (cmdmsg_rl)
2847 {
2848 /* displaying from right to left */
2849 pc = prev_c;
2850 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002851 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 if (j + mb_l >= start + len)
2853 nc = NUL;
2854 else
2855 nc = utf_ptr2char(p + mb_l);
2856 }
2857 else
2858 {
2859 /* displaying from left to right */
2860 if (j + mb_l >= start + len)
2861 pc = NUL;
2862 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002863 {
2864 int pcc[MAX_MCO];
2865
2866 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002868 pc1 = pcc[0];
2869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 nc = prev_c;
2871 }
2872 prev_c = u8c;
2873
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002874 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002876 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002877 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002879 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2880 if (u8cc[1] != 0)
2881 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002882 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884 }
2885 else
2886 {
2887 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002888 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 newlen += mb_l;
2890 }
2891 }
2892
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002893 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 }
2895 else
2896#endif
2897 msg_outtrans_len(ccline.cmdbuff + start, len);
2898}
2899
2900/*
2901 * Put a character on the command line. Shifts the following text to the
2902 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2903 * "c" must be printable (fit in one display cell)!
2904 */
2905 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002906putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907{
2908 if (cmd_silent)
2909 return;
2910 msg_no_more = TRUE;
2911 msg_putchar(c);
2912 if (shift)
2913 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2914 msg_no_more = FALSE;
2915 cursorcmd();
2916}
2917
2918/*
2919 * Undo a putcmdline(c, FALSE).
2920 */
2921 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002922unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923{
2924 if (cmd_silent)
2925 return;
2926 msg_no_more = TRUE;
2927 if (ccline.cmdlen == ccline.cmdpos)
2928 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002929#ifdef FEAT_MBYTE
2930 else if (has_mbyte)
2931 draw_cmdline(ccline.cmdpos,
2932 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 else
2935 draw_cmdline(ccline.cmdpos, 1);
2936 msg_no_more = FALSE;
2937 cursorcmd();
2938}
2939
2940/*
2941 * Put the given string, of the given length, onto the command line.
2942 * If len is -1, then STRLEN() is used to calculate the length.
2943 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2944 * part will be redrawn, otherwise it will not. If this function is called
2945 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2946 * called afterwards.
2947 */
2948 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002949put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950{
2951 int retval;
2952 int i;
2953 int m;
2954 int c;
2955
2956 if (len < 0)
2957 len = (int)STRLEN(str);
2958
2959 /* Check if ccline.cmdbuff needs to be longer */
2960 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002961 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 else
2963 retval = OK;
2964 if (retval == OK)
2965 {
2966 if (!ccline.overstrike)
2967 {
2968 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2969 ccline.cmdbuff + ccline.cmdpos,
2970 (size_t)(ccline.cmdlen - ccline.cmdpos));
2971 ccline.cmdlen += len;
2972 }
2973 else
2974 {
2975#ifdef FEAT_MBYTE
2976 if (has_mbyte)
2977 {
2978 /* Count nr of characters in the new string. */
2979 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002980 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 ++m;
2982 /* Count nr of bytes in cmdline that are overwritten by these
2983 * characters. */
2984 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002985 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 --m;
2987 if (i < ccline.cmdlen)
2988 {
2989 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2990 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2991 ccline.cmdlen += ccline.cmdpos + len - i;
2992 }
2993 else
2994 ccline.cmdlen = ccline.cmdpos + len;
2995 }
2996 else
2997#endif
2998 if (ccline.cmdpos + len > ccline.cmdlen)
2999 ccline.cmdlen = ccline.cmdpos + len;
3000 }
3001 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3002 ccline.cmdbuff[ccline.cmdlen] = NUL;
3003
3004#ifdef FEAT_MBYTE
3005 if (enc_utf8)
3006 {
3007 /* When the inserted text starts with a composing character,
3008 * backup to the character before it. There could be two of them.
3009 */
3010 i = 0;
3011 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3012 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3013 {
3014 i = (*mb_head_off)(ccline.cmdbuff,
3015 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3016 ccline.cmdpos -= i;
3017 len += i;
3018 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3019 }
3020# ifdef FEAT_ARABIC
3021 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3022 {
3023 /* Check the previous character for Arabic combining pair. */
3024 i = (*mb_head_off)(ccline.cmdbuff,
3025 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3026 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3027 + ccline.cmdpos - i), c))
3028 {
3029 ccline.cmdpos -= i;
3030 len += i;
3031 }
3032 else
3033 i = 0;
3034 }
3035# endif
3036 if (i != 0)
3037 {
3038 /* Also backup the cursor position. */
3039 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3040 ccline.cmdspos -= i;
3041 msg_col -= i;
3042 if (msg_col < 0)
3043 {
3044 msg_col += Columns;
3045 --msg_row;
3046 }
3047 }
3048 }
3049#endif
3050
3051 if (redraw && !cmd_silent)
3052 {
3053 msg_no_more = TRUE;
3054 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003055 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3057 /* Avoid clearing the rest of the line too often. */
3058 if (cmdline_row != i || ccline.overstrike)
3059 msg_clr_eos();
3060 msg_no_more = FALSE;
3061 }
3062#ifdef FEAT_FKMAP
3063 /*
3064 * If we are in Farsi command mode, the character input must be in
3065 * Insert mode. So do not advance the cmdpos.
3066 */
3067 if (!cmd_fkmap)
3068#endif
3069 {
3070 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003071 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003073 if (m < 0) /* overflow, Columns or Rows at weird value */
3074 m = MAXCOL;
3075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 else
3077 m = MAXCOL;
3078 for (i = 0; i < len; ++i)
3079 {
3080 c = cmdline_charsize(ccline.cmdpos);
3081#ifdef FEAT_MBYTE
3082 /* count ">" for a double-wide char that doesn't fit. */
3083 if (has_mbyte)
3084 correct_cmdspos(ccline.cmdpos, c);
3085#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003086 /* Stop cursor at the end of the screen, but do increment the
3087 * insert position, so that entering a very long command
3088 * works, even though you can't see it. */
3089 if (ccline.cmdspos + c < m)
3090 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091#ifdef FEAT_MBYTE
3092 if (has_mbyte)
3093 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003094 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 if (c > len - i - 1)
3096 c = len - i - 1;
3097 ccline.cmdpos += c;
3098 i += c;
3099 }
3100#endif
3101 ++ccline.cmdpos;
3102 }
3103 }
3104 }
3105 if (redraw)
3106 msg_check();
3107 return retval;
3108}
3109
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003110static struct cmdline_info prev_ccline;
3111static int prev_ccline_used = FALSE;
3112
3113/*
3114 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3115 * and overwrite it. But get_cmdline_str() may need it, thus make it
3116 * available globally in prev_ccline.
3117 */
3118 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003119save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003120{
3121 if (!prev_ccline_used)
3122 {
3123 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3124 prev_ccline_used = TRUE;
3125 }
3126 *ccp = prev_ccline;
3127 prev_ccline = ccline;
3128 ccline.cmdbuff = NULL;
3129 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003130 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003131}
3132
3133/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003134 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003135 */
3136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003137restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003138{
3139 ccline = prev_ccline;
3140 prev_ccline = *ccp;
3141}
3142
Bram Moolenaar5a305422006-04-28 22:38:25 +00003143#if defined(FEAT_EVAL) || defined(PROTO)
3144/*
3145 * Save the command line into allocated memory. Returns a pointer to be
3146 * passed to restore_cmdline_alloc() later.
3147 * Returns NULL when failed.
3148 */
3149 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003150save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003151{
3152 struct cmdline_info *p;
3153
3154 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3155 if (p != NULL)
3156 save_cmdline(p);
3157 return (char_u *)p;
3158}
3159
3160/*
3161 * Restore the command line from the return value of save_cmdline_alloc().
3162 */
3163 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003164restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003165{
3166 if (p != NULL)
3167 {
3168 restore_cmdline((struct cmdline_info *)p);
3169 vim_free(p);
3170 }
3171}
3172#endif
3173
Bram Moolenaar8299df92004-07-10 09:47:34 +00003174/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003175 * Paste a yank register into the command line.
3176 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003177 * insert_reg() can't be used here, because special characters from the
3178 * register contents will be interpreted as commands.
3179 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003180 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003181 */
3182 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003183cmdline_paste(
3184 int regname,
3185 int literally, /* Insert text literally instead of "as typed" */
3186 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003187{
3188 long i;
3189 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003190 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003191 int allocated;
3192 struct cmdline_info save_ccline;
3193
3194 /* check for valid regname; also accept special characters for CTRL-R in
3195 * the command line */
3196 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3197 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3198 return FAIL;
3199
3200 /* A register containing CTRL-R can cause an endless loop. Allow using
3201 * CTRL-C to break the loop. */
3202 line_breakcheck();
3203 if (got_int)
3204 return FAIL;
3205
3206#ifdef FEAT_CLIPBOARD
3207 regname = may_get_selection(regname);
3208#endif
3209
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003210 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003211 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003212 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003213 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003214 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003215 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003216 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003217
3218 if (i)
3219 {
3220 /* Got the value of a special register in "arg". */
3221 if (arg == NULL)
3222 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003223
3224 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3225 * part of the word. */
3226 p = arg;
3227 if (p_is && regname == Ctrl_W)
3228 {
3229 char_u *w;
3230 int len;
3231
3232 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003233 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003234 {
3235#ifdef FEAT_MBYTE
3236 if (has_mbyte)
3237 {
3238 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3239 if (!vim_iswordc(mb_ptr2char(w - len)))
3240 break;
3241 w -= len;
3242 }
3243 else
3244#endif
3245 {
3246 if (!vim_iswordc(w[-1]))
3247 break;
3248 --w;
3249 }
3250 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003251 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003252 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3253 p += len;
3254 }
3255
3256 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003257 if (allocated)
3258 vim_free(arg);
3259 return OK;
3260 }
3261
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003262 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003263}
3264
3265/*
3266 * Put a string on the command line.
3267 * When "literally" is TRUE, insert literally.
3268 * When "literally" is FALSE, insert as typed, but don't leave the command
3269 * line.
3270 */
3271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003272cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003273{
3274 int c, cv;
3275
3276 if (literally)
3277 put_on_cmdline(s, -1, TRUE);
3278 else
3279 while (*s != NUL)
3280 {
3281 cv = *s;
3282 if (cv == Ctrl_V && s[1])
3283 ++s;
3284#ifdef FEAT_MBYTE
3285 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003286 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003287 else
3288#endif
3289 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003290 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3291 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003292#ifdef UNIX
3293 || c == intr_char
3294#endif
3295 || (c == Ctrl_BSL && *s == Ctrl_N))
3296 stuffcharReadbuff(Ctrl_V);
3297 stuffcharReadbuff(c);
3298 }
3299}
3300
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301#ifdef FEAT_WILDMENU
3302/*
3303 * Delete characters on the command line, from "from" to the current
3304 * position.
3305 */
3306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003307cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3310 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3311 ccline.cmdlen -= ccline.cmdpos - from;
3312 ccline.cmdpos = from;
3313}
3314#endif
3315
3316/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003317 * This function is called when the screen size changes and with incremental
3318 * search and in other situations where the command line may have been
3319 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 */
3321 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003322redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
3324 if (cmd_silent)
3325 return;
3326 need_wait_return = FALSE;
3327 compute_cmdrow();
3328 redrawcmd();
3329 cursorcmd();
3330}
3331
3332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003333redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 int i;
3336
3337 if (cmd_silent)
3338 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003339 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 msg_putchar(ccline.cmdfirstc);
3341 if (ccline.cmdprompt != NULL)
3342 {
3343 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3344 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3345 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003346 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 --ccline.cmdindent;
3348 }
3349 else
3350 for (i = ccline.cmdindent; i > 0; --i)
3351 msg_putchar(' ');
3352}
3353
3354/*
3355 * Redraw what is currently on the command line.
3356 */
3357 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003358redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359{
3360 if (cmd_silent)
3361 return;
3362
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003363 /* when 'incsearch' is set there may be no command line while redrawing */
3364 if (ccline.cmdbuff == NULL)
3365 {
3366 windgoto(cmdline_row, 0);
3367 msg_clr_eos();
3368 return;
3369 }
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 msg_start();
3372 redrawcmdprompt();
3373
3374 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3375 msg_no_more = TRUE;
3376 draw_cmdline(0, ccline.cmdlen);
3377 msg_clr_eos();
3378 msg_no_more = FALSE;
3379
3380 set_cmdspos_cursor();
3381
3382 /*
3383 * An emsg() before may have set msg_scroll. This is used in normal mode,
3384 * in cmdline mode we can reset them now.
3385 */
3386 msg_scroll = FALSE; /* next message overwrites cmdline */
3387
3388 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3389 * in cmdline mode */
3390 skip_redraw = FALSE;
3391}
3392
3393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003394compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003396 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 cmdline_row = Rows - 1;
3398 else
3399 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3400 + W_STATUS_HEIGHT(lastwin);
3401}
3402
3403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003404cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
3406 if (cmd_silent)
3407 return;
3408
3409#ifdef FEAT_RIGHTLEFT
3410 if (cmdmsg_rl)
3411 {
3412 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3413 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3414 if (msg_row <= 0)
3415 msg_row = Rows - 1;
3416 }
3417 else
3418#endif
3419 {
3420 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3421 msg_col = ccline.cmdspos % (int)Columns;
3422 if (msg_row >= Rows)
3423 msg_row = Rows - 1;
3424 }
3425
3426 windgoto(msg_row, msg_col);
3427#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3428 redrawcmd_preedit();
3429#endif
3430#ifdef MCH_CURSOR_SHAPE
3431 mch_update_cursor();
3432#endif
3433}
3434
3435 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003436gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437{
3438 msg_start();
3439#ifdef FEAT_RIGHTLEFT
3440 if (cmdmsg_rl)
3441 msg_col = Columns - 1;
3442 else
3443#endif
3444 msg_col = 0; /* always start in column 0 */
3445 if (clr) /* clear the bottom line(s) */
3446 msg_clr_eos(); /* will reset clear_cmdline */
3447 windgoto(cmdline_row, 0);
3448}
3449
3450/*
3451 * Check the word in front of the cursor for an abbreviation.
3452 * Called when the non-id character "c" has been entered.
3453 * When an abbreviation is recognized it is removed from the text with
3454 * backspaces and the replacement string is inserted, followed by "c".
3455 */
3456 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003457ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3460 return FALSE;
3461
3462 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3463}
3464
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003465#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3466 static int
3467#ifdef __BORLANDC__
3468_RTLENTRYF
3469#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003470sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003471{
3472 char_u *p1 = *(char_u **)s1;
3473 char_u *p2 = *(char_u **)s2;
3474
3475 if (*p1 != '<' && *p2 == '<') return -1;
3476 if (*p1 == '<' && *p2 != '<') return 1;
3477 return STRCMP(p1, p2);
3478}
3479#endif
3480
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481/*
3482 * Return FAIL if this is not an appropriate context in which to do
3483 * completion of anything, return OK if it is (even if there are no matches).
3484 * For the caller, this means that the character is just passed through like a
3485 * normal character (instead of being expanded). This allows :s/^I^D etc.
3486 */
3487 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003488nextwild(
3489 expand_T *xp,
3490 int type,
3491 int options, /* extra options for ExpandOne() */
3492 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494 int i, j;
3495 char_u *p1;
3496 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 int difflen;
3498 int v;
3499
3500 if (xp->xp_numfiles == -1)
3501 {
3502 set_expand_context(xp);
3503 cmd_showtail = expand_showtail(xp);
3504 }
3505
3506 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3507 {
3508 beep_flush();
3509 return OK; /* Something illegal on command line */
3510 }
3511 if (xp->xp_context == EXPAND_NOTHING)
3512 {
3513 /* Caller can use the character as a normal char instead */
3514 return FAIL;
3515 }
3516
3517 MSG_PUTS("..."); /* show that we are busy */
3518 out_flush();
3519
3520 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003521 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523 if (type == WILD_NEXT || type == WILD_PREV)
3524 {
3525 /*
3526 * Get next/previous match for a previous expanded pattern.
3527 */
3528 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3529 }
3530 else
3531 {
3532 /*
3533 * Translate string into pattern and expand it.
3534 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003535 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3536 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 p2 = NULL;
3538 else
3539 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003540 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003541 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3542 if (escape)
3543 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003544
3545 if (p_wic)
3546 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003547 p2 = ExpandOne(xp, p1,
3548 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003549 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003551 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (p2 != NULL && type == WILD_LONGEST)
3553 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003554 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (ccline.cmdbuff[i + j] == '*'
3556 || ccline.cmdbuff[i + j] == '?')
3557 break;
3558 if ((int)STRLEN(p2) < j)
3559 {
3560 vim_free(p2);
3561 p2 = NULL;
3562 }
3563 }
3564 }
3565 }
3566
3567 if (p2 != NULL && !got_int)
3568 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003569 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003570 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003572 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 xp->xp_pattern = ccline.cmdbuff + i;
3574 }
3575 else
3576 v = OK;
3577 if (v == OK)
3578 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003579 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3580 &ccline.cmdbuff[ccline.cmdpos],
3581 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3582 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 ccline.cmdlen += difflen;
3584 ccline.cmdpos += difflen;
3585 }
3586 }
3587 vim_free(p2);
3588
3589 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003590 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592 /* When expanding a ":map" command and no matches are found, assume that
3593 * the key is supposed to be inserted literally */
3594 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3595 return FAIL;
3596
3597 if (xp->xp_numfiles <= 0 && p2 == NULL)
3598 beep_flush();
3599 else if (xp->xp_numfiles == 1)
3600 /* free expanded pattern */
3601 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3602
3603 return OK;
3604}
3605
3606/*
3607 * Do wildcard expansion on the string 'str'.
3608 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003609 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 * Return NULL for failure.
3611 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003612 * "orig" is the originally expanded string, copied to allocated memory. It
3613 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3614 * WILD_PREV "orig" should be NULL.
3615 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003616 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3617 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 *
3619 * mode = WILD_FREE: just free previously expanded matches
3620 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3621 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3622 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3623 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3624 * mode = WILD_ALL: return all matches concatenated
3625 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003626 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 *
3628 * options = WILD_LIST_NOTFOUND: list entries without a match
3629 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3630 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3631 * options = WILD_NO_BEEP: Don't beep for multiple matches
3632 * options = WILD_ADD_SLASH: add a slash after directory names
3633 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3634 * options = WILD_SILENT: don't print warning messages
3635 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003636 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 *
3638 * The variables xp->xp_context and xp->xp_backslash must have been set!
3639 */
3640 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003641ExpandOne(
3642 expand_T *xp,
3643 char_u *str,
3644 char_u *orig, /* allocated copy of original of expanded string */
3645 int options,
3646 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647{
3648 char_u *ss = NULL;
3649 static int findex;
3650 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003651 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 int i;
3653 long_u len;
3654 int non_suf_match; /* number without matching suffix */
3655
3656 /*
3657 * first handle the case of using an old match
3658 */
3659 if (mode == WILD_NEXT || mode == WILD_PREV)
3660 {
3661 if (xp->xp_numfiles > 0)
3662 {
3663 if (mode == WILD_PREV)
3664 {
3665 if (findex == -1)
3666 findex = xp->xp_numfiles;
3667 --findex;
3668 }
3669 else /* mode == WILD_NEXT */
3670 ++findex;
3671
3672 /*
3673 * When wrapping around, return the original string, set findex to
3674 * -1.
3675 */
3676 if (findex < 0)
3677 {
3678 if (orig_save == NULL)
3679 findex = xp->xp_numfiles - 1;
3680 else
3681 findex = -1;
3682 }
3683 if (findex >= xp->xp_numfiles)
3684 {
3685 if (orig_save == NULL)
3686 findex = 0;
3687 else
3688 findex = -1;
3689 }
3690#ifdef FEAT_WILDMENU
3691 if (p_wmnu)
3692 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3693 findex, cmd_showtail);
3694#endif
3695 if (findex == -1)
3696 return vim_strsave(orig_save);
3697 return vim_strsave(xp->xp_files[findex]);
3698 }
3699 else
3700 return NULL;
3701 }
3702
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003703 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3705 {
3706 FreeWild(xp->xp_numfiles, xp->xp_files);
3707 xp->xp_numfiles = -1;
3708 vim_free(orig_save);
3709 orig_save = NULL;
3710 }
3711 findex = 0;
3712
3713 if (mode == WILD_FREE) /* only release file name */
3714 return NULL;
3715
3716 if (xp->xp_numfiles == -1)
3717 {
3718 vim_free(orig_save);
3719 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003720 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
3722 /*
3723 * Do the expansion.
3724 */
3725 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3726 options) == FAIL)
3727 {
3728#ifdef FNAME_ILLEGAL
3729 /* Illegal file name has been silently skipped. But when there
3730 * are wildcards, the real problem is that there was no match,
3731 * causing the pattern to be added, which has illegal characters.
3732 */
3733 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3734 EMSG2(_(e_nomatch2), str);
3735#endif
3736 }
3737 else if (xp->xp_numfiles == 0)
3738 {
3739 if (!(options & WILD_SILENT))
3740 EMSG2(_(e_nomatch2), str);
3741 }
3742 else
3743 {
3744 /* Escape the matches for use on the command line. */
3745 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3746
3747 /*
3748 * Check for matching suffixes in file names.
3749 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003750 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3751 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
3753 if (xp->xp_numfiles)
3754 non_suf_match = xp->xp_numfiles;
3755 else
3756 non_suf_match = 1;
3757 if ((xp->xp_context == EXPAND_FILES
3758 || xp->xp_context == EXPAND_DIRECTORIES)
3759 && xp->xp_numfiles > 1)
3760 {
3761 /*
3762 * More than one match; check suffix.
3763 * The files will have been sorted on matching suffix in
3764 * expand_wildcards, only need to check the first two.
3765 */
3766 non_suf_match = 0;
3767 for (i = 0; i < 2; ++i)
3768 if (match_suffix(xp->xp_files[i]))
3769 ++non_suf_match;
3770 }
3771 if (non_suf_match != 1)
3772 {
3773 /* Can we ever get here unless it's while expanding
3774 * interactively? If not, we can get rid of this all
3775 * together. Don't really want to wait for this message
3776 * (and possibly have to hit return to continue!).
3777 */
3778 if (!(options & WILD_SILENT))
3779 EMSG(_(e_toomany));
3780 else if (!(options & WILD_NO_BEEP))
3781 beep_flush();
3782 }
3783 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3784 ss = vim_strsave(xp->xp_files[0]);
3785 }
3786 }
3787 }
3788
3789 /* Find longest common part */
3790 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3791 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003792 int mb_len = 1;
3793 int c0, ci;
3794
3795 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003797#ifdef FEAT_MBYTE
3798 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003800 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3801 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3802 }
3803 else
3804#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003805 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003806 for (i = 1; i < xp->xp_numfiles; ++i)
3807 {
3808#ifdef FEAT_MBYTE
3809 if (has_mbyte)
3810 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3811 else
3812#endif
3813 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003814 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003816 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003817 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003819 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 break;
3821 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003822 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 break;
3824 }
3825 if (i < xp->xp_numfiles)
3826 {
3827 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003828 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 break;
3830 }
3831 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003832
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 ss = alloc((unsigned)len + 1);
3834 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003835 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 findex = -1; /* next p_wc gets first one */
3837 }
3838
3839 /* Concatenate all matching names */
3840 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3841 {
3842 len = 0;
3843 for (i = 0; i < xp->xp_numfiles; ++i)
3844 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3845 ss = lalloc(len, TRUE);
3846 if (ss != NULL)
3847 {
3848 *ss = NUL;
3849 for (i = 0; i < xp->xp_numfiles; ++i)
3850 {
3851 STRCAT(ss, xp->xp_files[i]);
3852 if (i != xp->xp_numfiles - 1)
3853 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3854 }
3855 }
3856 }
3857
3858 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3859 ExpandCleanup(xp);
3860
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003861 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003862 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003863 vim_free(orig);
3864
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 return ss;
3866}
3867
3868/*
3869 * Prepare an expand structure for use.
3870 */
3871 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003872ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003874 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003875 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003877#ifndef BACKSLASH_IN_FILENAME
3878 xp->xp_shell = FALSE;
3879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 xp->xp_numfiles = -1;
3881 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003882#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3883 xp->xp_arg = NULL;
3884#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003885 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886}
3887
3888/*
3889 * Cleanup an expand structure after use.
3890 */
3891 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003892ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893{
3894 if (xp->xp_numfiles >= 0)
3895 {
3896 FreeWild(xp->xp_numfiles, xp->xp_files);
3897 xp->xp_numfiles = -1;
3898 }
3899}
3900
3901 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003902ExpandEscape(
3903 expand_T *xp,
3904 char_u *str,
3905 int numfiles,
3906 char_u **files,
3907 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908{
3909 int i;
3910 char_u *p;
3911
3912 /*
3913 * May change home directory back to "~"
3914 */
3915 if (options & WILD_HOME_REPLACE)
3916 tilde_replace(str, numfiles, files);
3917
3918 if (options & WILD_ESCAPE)
3919 {
3920 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003921 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003922 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 || xp->xp_context == EXPAND_BUFFERS
3924 || xp->xp_context == EXPAND_DIRECTORIES)
3925 {
3926 /*
3927 * Insert a backslash into a file name before a space, \, %, #
3928 * and wildmatch characters, except '~'.
3929 */
3930 for (i = 0; i < numfiles; ++i)
3931 {
3932 /* for ":set path=" we need to escape spaces twice */
3933 if (xp->xp_backslash == XP_BS_THREE)
3934 {
3935 p = vim_strsave_escaped(files[i], (char_u *)" ");
3936 if (p != NULL)
3937 {
3938 vim_free(files[i]);
3939 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003940#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 p = vim_strsave_escaped(files[i], (char_u *)" ");
3942 if (p != NULL)
3943 {
3944 vim_free(files[i]);
3945 files[i] = p;
3946 }
3947#endif
3948 }
3949 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003950#ifdef BACKSLASH_IN_FILENAME
3951 p = vim_strsave_fnameescape(files[i], FALSE);
3952#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003953 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 if (p != NULL)
3956 {
3957 vim_free(files[i]);
3958 files[i] = p;
3959 }
3960
3961 /* If 'str' starts with "\~", replace "~" at start of
3962 * files[i] with "\~". */
3963 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003964 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 }
3966 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003967
3968 /* If the first file starts with a '+' escape it. Otherwise it
3969 * could be seen as "+cmd". */
3970 if (*files[0] == '+')
3971 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 }
3973 else if (xp->xp_context == EXPAND_TAGS)
3974 {
3975 /*
3976 * Insert a backslash before characters in a tag name that
3977 * would terminate the ":tag" command.
3978 */
3979 for (i = 0; i < numfiles; ++i)
3980 {
3981 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3982 if (p != NULL)
3983 {
3984 vim_free(files[i]);
3985 files[i] = p;
3986 }
3987 }
3988 }
3989 }
3990}
3991
3992/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003993 * Escape special characters in "fname" for when used as a file name argument
3994 * after a Vim command, or, when "shell" is non-zero, a shell command.
3995 * Returns the result in allocated memory.
3996 */
3997 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003998vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003999{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004000 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004001#ifdef BACKSLASH_IN_FILENAME
4002 char_u buf[20];
4003 int j = 0;
4004
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004005 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004006 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004007 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004008 buf[j++] = *p;
4009 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004010 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004011#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004012 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4013 if (shell && csh_like_shell() && p != NULL)
4014 {
4015 char_u *s;
4016
4017 /* For csh and similar shells need to put two backslashes before '!'.
4018 * One is taken by Vim, one by the shell. */
4019 s = vim_strsave_escaped(p, (char_u *)"!");
4020 vim_free(p);
4021 p = s;
4022 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004023#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004024
4025 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4026 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004027 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004028 escape_fname(&p);
4029
4030 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004031}
4032
4033/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004034 * Put a backslash before the file name in "pp", which is in allocated memory.
4035 */
4036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004037escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004038{
4039 char_u *p;
4040
4041 p = alloc((unsigned)(STRLEN(*pp) + 2));
4042 if (p != NULL)
4043 {
4044 p[0] = '\\';
4045 STRCPY(p + 1, *pp);
4046 vim_free(*pp);
4047 *pp = p;
4048 }
4049}
4050
4051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 * For each file name in files[num_files]:
4053 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4054 */
4055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004056tilde_replace(
4057 char_u *orig_pat,
4058 int num_files,
4059 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060{
4061 int i;
4062 char_u *p;
4063
4064 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4065 {
4066 for (i = 0; i < num_files; ++i)
4067 {
4068 p = home_replace_save(NULL, files[i]);
4069 if (p != NULL)
4070 {
4071 vim_free(files[i]);
4072 files[i] = p;
4073 }
4074 }
4075 }
4076}
4077
4078/*
4079 * Show all matches for completion on the command line.
4080 * Returns EXPAND_NOTHING when the character that triggered expansion should
4081 * be inserted like a normal character.
4082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004084showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
4086#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4087 int num_files;
4088 char_u **files_found;
4089 int i, j, k;
4090 int maxlen;
4091 int lines;
4092 int columns;
4093 char_u *p;
4094 int lastlen;
4095 int attr;
4096 int showtail;
4097
4098 if (xp->xp_numfiles == -1)
4099 {
4100 set_expand_context(xp);
4101 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4102 &num_files, &files_found);
4103 showtail = expand_showtail(xp);
4104 if (i != EXPAND_OK)
4105 return i;
4106
4107 }
4108 else
4109 {
4110 num_files = xp->xp_numfiles;
4111 files_found = xp->xp_files;
4112 showtail = cmd_showtail;
4113 }
4114
4115#ifdef FEAT_WILDMENU
4116 if (!wildmenu)
4117 {
4118#endif
4119 msg_didany = FALSE; /* lines_left will be set */
4120 msg_start(); /* prepare for paging */
4121 msg_putchar('\n');
4122 out_flush();
4123 cmdline_row = msg_row;
4124 msg_didany = FALSE; /* lines_left will be set again */
4125 msg_start(); /* prepare for paging */
4126#ifdef FEAT_WILDMENU
4127 }
4128#endif
4129
4130 if (got_int)
4131 got_int = FALSE; /* only int. the completion, not the cmd line */
4132#ifdef FEAT_WILDMENU
4133 else if (wildmenu)
4134 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
4135#endif
4136 else
4137 {
4138 /* find the length of the longest file name */
4139 maxlen = 0;
4140 for (i = 0; i < num_files; ++i)
4141 {
4142 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004143 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 || xp->xp_context == EXPAND_BUFFERS))
4145 {
4146 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4147 j = vim_strsize(NameBuff);
4148 }
4149 else
4150 j = vim_strsize(L_SHOWFILE(i));
4151 if (j > maxlen)
4152 maxlen = j;
4153 }
4154
4155 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4156 lines = num_files;
4157 else
4158 {
4159 /* compute the number of columns and lines for the listing */
4160 maxlen += 2; /* two spaces between file names */
4161 columns = ((int)Columns + 2) / maxlen;
4162 if (columns < 1)
4163 columns = 1;
4164 lines = (num_files + columns - 1) / columns;
4165 }
4166
4167 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4168
4169 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4170 {
4171 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4172 msg_clr_eos();
4173 msg_advance(maxlen - 3);
4174 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4175 }
4176
4177 /* list the files line by line */
4178 for (i = 0; i < lines; ++i)
4179 {
4180 lastlen = 999;
4181 for (k = i; k < num_files; k += lines)
4182 {
4183 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4184 {
4185 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4186 p = files_found[k] + STRLEN(files_found[k]) + 1;
4187 msg_advance(maxlen + 1);
4188 msg_puts(p);
4189 msg_advance(maxlen + 3);
4190 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4191 break;
4192 }
4193 for (j = maxlen - lastlen; --j >= 0; )
4194 msg_putchar(' ');
4195 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004196 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 || xp->xp_context == EXPAND_BUFFERS)
4198 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004199 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004200 if (xp->xp_numfiles != -1)
4201 {
4202 char_u *halved_slash;
4203 char_u *exp_path;
4204
4205 /* Expansion was done before and special characters
4206 * were escaped, need to halve backslashes. Also
4207 * $HOME has been replaced with ~/. */
4208 exp_path = expand_env_save_opt(files_found[k], TRUE);
4209 halved_slash = backslash_halve_save(
4210 exp_path != NULL ? exp_path : files_found[k]);
4211 j = mch_isdir(halved_slash != NULL ? halved_slash
4212 : files_found[k]);
4213 vim_free(exp_path);
4214 vim_free(halved_slash);
4215 }
4216 else
4217 /* Expansion was done here, file names are literal. */
4218 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 if (showtail)
4220 p = L_SHOWFILE(k);
4221 else
4222 {
4223 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4224 TRUE);
4225 p = NameBuff;
4226 }
4227 }
4228 else
4229 {
4230 j = FALSE;
4231 p = L_SHOWFILE(k);
4232 }
4233 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4234 }
4235 if (msg_col > 0) /* when not wrapped around */
4236 {
4237 msg_clr_eos();
4238 msg_putchar('\n');
4239 }
4240 out_flush(); /* show one line at a time */
4241 if (got_int)
4242 {
4243 got_int = FALSE;
4244 break;
4245 }
4246 }
4247
4248 /*
4249 * we redraw the command below the lines that we have just listed
4250 * This is a bit tricky, but it saves a lot of screen updating.
4251 */
4252 cmdline_row = msg_row; /* will put it back later */
4253 }
4254
4255 if (xp->xp_numfiles == -1)
4256 FreeWild(num_files, files_found);
4257
4258 return EXPAND_OK;
4259}
4260
4261/*
4262 * Private gettail for showmatches() (and win_redr_status_matches()):
4263 * Find tail of file name path, but ignore trailing "/".
4264 */
4265 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004266sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267{
4268 char_u *p;
4269 char_u *t = s;
4270 int had_sep = FALSE;
4271
4272 for (p = s; *p != NUL; )
4273 {
4274 if (vim_ispathsep(*p)
4275#ifdef BACKSLASH_IN_FILENAME
4276 && !rem_backslash(p)
4277#endif
4278 )
4279 had_sep = TRUE;
4280 else if (had_sep)
4281 {
4282 t = p;
4283 had_sep = FALSE;
4284 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004285 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 return t;
4288}
4289
4290/*
4291 * Return TRUE if we only need to show the tail of completion matches.
4292 * When not completing file names or there is a wildcard in the path FALSE is
4293 * returned.
4294 */
4295 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004296expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
4298 char_u *s;
4299 char_u *end;
4300
4301 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004302 if (xp->xp_context != EXPAND_FILES
4303 && xp->xp_context != EXPAND_SHELLCMD
4304 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 return FALSE;
4306
4307 end = gettail(xp->xp_pattern);
4308 if (end == xp->xp_pattern) /* there is no path separator */
4309 return FALSE;
4310
4311 for (s = xp->xp_pattern; s < end; s++)
4312 {
4313 /* Skip escaped wildcards. Only when the backslash is not a path
4314 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4315 if (rem_backslash(s))
4316 ++s;
4317 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4318 return FALSE;
4319 }
4320 return TRUE;
4321}
4322
4323/*
4324 * Prepare a string for expansion.
4325 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004326 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 * When expanding other names: The string will be used with regcomp(). Copy
4328 * the name into allocated memory and prepend "^".
4329 */
4330 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004331addstar(
4332 char_u *fname,
4333 int len,
4334 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
4336 char_u *retval;
4337 int i, j;
4338 int new_len;
4339 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004340 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004342 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004343 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004344 && context != EXPAND_SHELLCMD
4345 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
4347 /*
4348 * Matching will be done internally (on something other than files).
4349 * So we convert the file-matching-type wildcards into our kind for
4350 * use with vim_regcomp(). First work out how long it will be:
4351 */
4352
4353 /* For help tags the translation is done in find_help_tags().
4354 * For a tag pattern starting with "/" no translation is needed. */
4355 if (context == EXPAND_HELP
4356 || context == EXPAND_COLORS
4357 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004358 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004359 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004360 || context == EXPAND_PACKADD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 || (context == EXPAND_TAGS && fname[0] == '/'))
4362 retval = vim_strnsave(fname, len);
4363 else
4364 {
4365 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4366 for (i = 0; i < len; i++)
4367 {
4368 if (fname[i] == '*' || fname[i] == '~')
4369 new_len++; /* '*' needs to be replaced by ".*"
4370 '~' needs to be replaced by "\~" */
4371
4372 /* Buffer names are like file names. "." should be literal */
4373 if (context == EXPAND_BUFFERS && fname[i] == '.')
4374 new_len++; /* "." becomes "\." */
4375
4376 /* Custom expansion takes care of special things, match
4377 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004378 if ((context == EXPAND_USER_DEFINED
4379 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 new_len++; /* '\' becomes "\\" */
4381 }
4382 retval = alloc(new_len);
4383 if (retval != NULL)
4384 {
4385 retval[0] = '^';
4386 j = 1;
4387 for (i = 0; i < len; i++, j++)
4388 {
4389 /* Skip backslash. But why? At least keep it for custom
4390 * expansion. */
4391 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004392 && context != EXPAND_USER_LIST
4393 && fname[i] == '\\'
4394 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 break;
4396
4397 switch (fname[i])
4398 {
4399 case '*': retval[j++] = '.';
4400 break;
4401 case '~': retval[j++] = '\\';
4402 break;
4403 case '?': retval[j] = '.';
4404 continue;
4405 case '.': if (context == EXPAND_BUFFERS)
4406 retval[j++] = '\\';
4407 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004408 case '\\': if (context == EXPAND_USER_DEFINED
4409 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 retval[j++] = '\\';
4411 break;
4412 }
4413 retval[j] = fname[i];
4414 }
4415 retval[j] = NUL;
4416 }
4417 }
4418 }
4419 else
4420 {
4421 retval = alloc(len + 4);
4422 if (retval != NULL)
4423 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004424 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
4426 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004427 * Don't add a star to *, ~, ~user, $var or `cmd`.
4428 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 * ~ would be at the start of the file name, but not the tail.
4430 * $ could be anywhere in the tail.
4431 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004432 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 */
4434 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004435 ends_in_star = (len > 0 && retval[len - 1] == '*');
4436#ifndef BACKSLASH_IN_FILENAME
4437 for (i = len - 2; i >= 0; --i)
4438 {
4439 if (retval[i] != '\\')
4440 break;
4441 ends_in_star = !ends_in_star;
4442 }
4443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004445 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 && vim_strchr(tail, '$') == NULL
4447 && vim_strchr(retval, '`') == NULL)
4448 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004449 else if (len > 0 && retval[len - 1] == '$')
4450 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 retval[len] = NUL;
4452 }
4453 }
4454 return retval;
4455}
4456
4457/*
4458 * Must parse the command line so far to work out what context we are in.
4459 * Completion can then be done based on that context.
4460 * This routine sets the variables:
4461 * xp->xp_pattern The start of the pattern to be expanded within
4462 * the command line (ends at the cursor).
4463 * xp->xp_context The type of thing to expand. Will be one of:
4464 *
4465 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4466 * the command line, like an unknown command. Caller
4467 * should beep.
4468 * EXPAND_NOTHING Unrecognised context for completion, use char like
4469 * a normal char, rather than for completion. eg
4470 * :s/^I/
4471 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4472 * it.
4473 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4474 * EXPAND_FILES After command with XFILE set, or after setting
4475 * with P_EXPAND set. eg :e ^I, :w>>^I
4476 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4477 * when we know only directories are of interest. eg
4478 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004479 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4481 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4482 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4483 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4484 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4485 * EXPAND_EVENTS Complete event names
4486 * EXPAND_SYNTAX Complete :syntax command arguments
4487 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4488 * EXPAND_AUGROUP Complete autocommand group names
4489 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4490 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4491 * eg :unmap a^I , :cunab x^I
4492 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4493 * eg :call sub^I
4494 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4495 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4496 * names in expressions, eg :while s^I
4497 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004498 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 */
4500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004501set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004503 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (ccline.cmdfirstc != ':'
4505#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004506 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004507 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508#endif
4509 )
4510 {
4511 xp->xp_context = EXPAND_NOTHING;
4512 return;
4513 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004514 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515}
4516
4517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004518set_cmd_context(
4519 expand_T *xp,
4520 char_u *str, /* start of command line */
4521 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004522 int col, /* position of cursor */
4523 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524{
4525 int old_char = NUL;
4526 char_u *nextcomm;
4527
4528 /*
4529 * Avoid a UMR warning from Purify, only save the character if it has been
4530 * written before.
4531 */
4532 if (col < len)
4533 old_char = str[col];
4534 str[col] = NUL;
4535 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004536
4537#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004538 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004539 {
4540# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004541 /* pass CMD_SIZE because there is no real command */
4542 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004543# endif
4544 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004545 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004546 {
4547 xp->xp_context = ccline.xp_context;
4548 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004549# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004550 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004551# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004552 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004553 else
4554#endif
4555 while (nextcomm != NULL)
4556 nextcomm = set_one_cmd_context(xp, nextcomm);
4557
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004558 /* Store the string here so that call_user_expand_func() can get to them
4559 * easily. */
4560 xp->xp_line = str;
4561 xp->xp_col = col;
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 str[col] = old_char;
4564}
4565
4566/*
4567 * Expand the command line "str" from context "xp".
4568 * "xp" must have been set by set_cmd_context().
4569 * xp->xp_pattern points into "str", to where the text that is to be expanded
4570 * starts.
4571 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4572 * cursor.
4573 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4574 * key that triggered expansion literally.
4575 * Returns EXPAND_OK otherwise.
4576 */
4577 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004578expand_cmdline(
4579 expand_T *xp,
4580 char_u *str, /* start of command line */
4581 int col, /* position of cursor */
4582 int *matchcount, /* return: nr of matches */
4583 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584{
4585 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004586 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4589 {
4590 beep_flush();
4591 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4592 }
4593 if (xp->xp_context == EXPAND_NOTHING)
4594 {
4595 /* Caller can use the character as a normal char instead */
4596 return EXPAND_NOTHING;
4597 }
4598
4599 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004600 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4601 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 if (file_str == NULL)
4603 return EXPAND_UNSUCCESSFUL;
4604
Bram Moolenaar94950a92010-12-02 16:01:29 +01004605 if (p_wic)
4606 options += WILD_ICASE;
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004609 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {
4611 *matchcount = 0;
4612 *matches = NULL;
4613 }
4614 vim_free(file_str);
4615
4616 return EXPAND_OK;
4617}
4618
4619#ifdef FEAT_MULTI_LANG
4620/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004621 * Cleanup matches for help tags:
4622 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4623 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004625static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
4627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004628cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630 int i, j;
4631 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004632 char_u buf[4];
4633 char_u *p = buf;
4634
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004635 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004636 {
4637 *p++ = '@';
4638 *p++ = p_hlg[0];
4639 *p++ = p_hlg[1];
4640 }
4641 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
4643 for (i = 0; i < num_file; ++i)
4644 {
4645 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004646 if (len <= 0)
4647 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004648 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
4650 /* Sorting on priority means the same item in another language may
4651 * be anywhere. Search all items for a match up to the "@en". */
4652 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004653 if (j != i && (int)STRLEN(file[j]) == len + 3
4654 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 break;
4656 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004657 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 file[i][len] = NUL;
4659 }
4660 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004661
4662 if (*buf != NUL)
4663 for (i = 0; i < num_file; ++i)
4664 {
4665 len = (int)STRLEN(file[i]) - 3;
4666 if (len <= 0)
4667 continue;
4668 if (STRCMP(file[i] + len, buf) == 0)
4669 {
4670 /* remove the default language */
4671 file[i][len] = NUL;
4672 }
4673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674}
4675#endif
4676
4677/*
4678 * Do the expansion based on xp->xp_context and "pat".
4679 */
4680 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004681ExpandFromContext(
4682 expand_T *xp,
4683 char_u *pat,
4684 int *num_file,
4685 char_u ***file,
4686 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
4688#ifdef FEAT_CMDL_COMPL
4689 regmatch_T regmatch;
4690#endif
4691 int ret;
4692 int flags;
4693
4694 flags = EW_DIR; /* include directories */
4695 if (options & WILD_LIST_NOTFOUND)
4696 flags |= EW_NOTFOUND;
4697 if (options & WILD_ADD_SLASH)
4698 flags |= EW_ADDSLASH;
4699 if (options & WILD_KEEP_ALL)
4700 flags |= EW_KEEPALL;
4701 if (options & WILD_SILENT)
4702 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004703 if (options & WILD_ALLLINKS)
4704 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004706 if (xp->xp_context == EXPAND_FILES
4707 || xp->xp_context == EXPAND_DIRECTORIES
4708 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
4710 /*
4711 * Expand file or directory names.
4712 */
4713 int free_pat = FALSE;
4714 int i;
4715
4716 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4717 * space */
4718 if (xp->xp_backslash != XP_BS_NONE)
4719 {
4720 free_pat = TRUE;
4721 pat = vim_strsave(pat);
4722 for (i = 0; pat[i]; ++i)
4723 if (pat[i] == '\\')
4724 {
4725 if (xp->xp_backslash == XP_BS_THREE
4726 && pat[i + 1] == '\\'
4727 && pat[i + 2] == '\\'
4728 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004729 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (xp->xp_backslash == XP_BS_ONE
4731 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004732 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 }
4734 }
4735
4736 if (xp->xp_context == EXPAND_FILES)
4737 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004738 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4739 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 else
4741 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004742 if (options & WILD_ICASE)
4743 flags |= EW_ICASE;
4744
Bram Moolenaard7834d32009-12-02 16:14:36 +00004745 /* Expand wildcards, supporting %:h and the like. */
4746 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (free_pat)
4748 vim_free(pat);
4749 return ret;
4750 }
4751
4752 *file = (char_u **)"";
4753 *num_file = 0;
4754 if (xp->xp_context == EXPAND_HELP)
4755 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004756 /* With an empty argument we would get all the help tags, which is
4757 * very slow. Get matches for "help" instead. */
4758 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4759 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
4761#ifdef FEAT_MULTI_LANG
4762 cleanup_help_tags(*num_file, *file);
4763#endif
4764 return OK;
4765 }
4766 return FAIL;
4767 }
4768
4769#ifndef FEAT_CMDL_COMPL
4770 return FAIL;
4771#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004772 if (xp->xp_context == EXPAND_SHELLCMD)
4773 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if (xp->xp_context == EXPAND_OLD_SETTING)
4775 return ExpandOldSetting(num_file, file);
4776 if (xp->xp_context == EXPAND_BUFFERS)
4777 return ExpandBufnames(pat, num_file, file, options);
4778 if (xp->xp_context == EXPAND_TAGS
4779 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4780 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4781 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004782 {
4783 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004784 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4785 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004788 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004789 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004790 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004791 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004792 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004793 {
4794 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004795 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004796 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004797 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004798 {
4799 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004800 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004801 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004802# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4803 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004804 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004805# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004806 if (xp->xp_context == EXPAND_PACKADD)
4807 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808
4809 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4810 if (regmatch.regprog == NULL)
4811 return FAIL;
4812
4813 /* set ignore-case according to p_ic, p_scs and pat */
4814 regmatch.rm_ic = ignorecase(pat);
4815
4816 if (xp->xp_context == EXPAND_SETTINGS
4817 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4818 ret = ExpandSettings(xp, &regmatch, num_file, file);
4819 else if (xp->xp_context == EXPAND_MAPPINGS)
4820 ret = ExpandMappings(&regmatch, num_file, file);
4821# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4822 else if (xp->xp_context == EXPAND_USER_DEFINED)
4823 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4824# endif
4825 else
4826 {
4827 static struct expgen
4828 {
4829 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004830 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004832 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 } tab[] =
4834 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004835 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4836 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004837 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004838#ifdef FEAT_CMDHIST
4839 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004842 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004843 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004844 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4845 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4846 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#endif
4848#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004849 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4850 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4851 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4852 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853#endif
4854#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004855 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4856 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#endif
4858#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004859 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004861#ifdef FEAT_PROFILE
4862 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4863#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004864 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004866 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4867 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004869#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004870 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004871#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004872#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004873 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004874#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004875#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004876 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4879 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004880 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4881 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004883 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004884 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 };
4886 int i;
4887
4888 /*
4889 * Find a context in the table and call the ExpandGeneric() with the
4890 * right function to do the expansion.
4891 */
4892 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004893 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 if (xp->xp_context == tab[i].context)
4895 {
4896 if (tab[i].ic)
4897 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004898 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004899 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 break;
4901 }
4902 }
4903
Bram Moolenaar473de612013-06-08 18:19:48 +02004904 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
4906 return ret;
4907#endif /* FEAT_CMDL_COMPL */
4908}
4909
4910#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4911/*
4912 * Expand a list of names.
4913 *
4914 * Generic function for command line completion. It calls a function to
4915 * obtain strings, one by one. The strings are matched against a regexp
4916 * program. Matching strings are copied into an array, which is returned.
4917 *
4918 * Returns OK when no problems encountered, FAIL for error (out of memory).
4919 */
4920 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004921ExpandGeneric(
4922 expand_T *xp,
4923 regmatch_T *regmatch,
4924 int *num_file,
4925 char_u ***file,
4926 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004928 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929{
4930 int i;
4931 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004932 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 char_u *str;
4934
4935 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004936 * round == 0: count the number of matching names
4937 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004939 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 for (i = 0; ; ++i)
4942 {
4943 str = (*func)(xp, i);
4944 if (str == NULL) /* end of list */
4945 break;
4946 if (*str == NUL) /* skip empty strings */
4947 continue;
4948
4949 if (vim_regexec(regmatch, str, (colnr_T)0))
4950 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004951 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004953 if (escaped)
4954 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4955 else
4956 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 (*file)[count] = str;
4958#ifdef FEAT_MENU
4959 if (func == get_menu_names && str != NULL)
4960 {
4961 /* test for separator added by get_menu_names() */
4962 str += STRLEN(str) - 1;
4963 if (*str == '\001')
4964 *str = '.';
4965 }
4966#endif
4967 }
4968 ++count;
4969 }
4970 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004971 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 if (count == 0)
4974 return OK;
4975 *num_file = count;
4976 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4977 if (*file == NULL)
4978 {
4979 *file = (char_u **)"";
4980 return FAIL;
4981 }
4982 count = 0;
4983 }
4984 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004985
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004986 /* Sort the results. Keep menu's in the specified order. */
4987 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004988 {
4989 if (xp->xp_context == EXPAND_EXPRESSION
4990 || xp->xp_context == EXPAND_FUNCTIONS
4991 || xp->xp_context == EXPAND_USER_FUNC)
4992 /* <SNR> functions should be sorted to the end. */
4993 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4994 sort_func_compare);
4995 else
4996 sort_strings(*file, *num_file);
4997 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004998
Bram Moolenaar4f688582007-07-24 12:34:30 +00004999#ifdef FEAT_CMDL_COMPL
5000 /* Reset the variables used for special highlight names expansion, so that
5001 * they don't show up when getting normal highlight names by ID. */
5002 reset_expand_highlight();
5003#endif
5004
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 return OK;
5006}
5007
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005008/*
5009 * Complete a shell command.
5010 * Returns FAIL or OK;
5011 */
5012 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005013expand_shellcmd(
5014 char_u *filepat, /* pattern to match with command names */
5015 int *num_file, /* return: number of matches */
5016 char_u ***file, /* return: array with matches */
5017 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005018{
5019 char_u *pat;
5020 int i;
5021 char_u *path;
5022 int mustfree = FALSE;
5023 garray_T ga;
5024 char_u *buf = alloc(MAXPATHL);
5025 size_t l;
5026 char_u *s, *e;
5027 int flags = flagsarg;
5028 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005029 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005030
5031 if (buf == NULL)
5032 return FAIL;
5033
5034 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5035 * space */
5036 pat = vim_strsave(filepat);
5037 for (i = 0; pat[i]; ++i)
5038 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005039 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005040
Bram Moolenaarb5971142015-03-21 17:32:19 +01005041 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005042
5043 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005044 if (mch_isFullName(pat))
5045 path = (char_u *)" ";
5046 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005047 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5048 path = (char_u *)".";
5049 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005050 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005051 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005052 if (path == NULL)
5053 path = (char_u *)"";
5054 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005055
5056 /*
5057 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005058 * collect them in "ga". When "." is not in $PATH also expand for the
5059 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005060 */
5061 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005062 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005063 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005064 if (*s == NUL)
5065 {
5066 if (did_curdir)
5067 break;
5068 /* Find directories in the current directory, path is empty. */
5069 did_curdir = TRUE;
5070 }
5071 else if (*s == '.')
5072 did_curdir = TRUE;
5073
Bram Moolenaar68c31742006-09-02 15:54:18 +00005074 if (*s == ' ')
5075 ++s; /* Skip space used for absolute path name. */
5076
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005077#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005078 e = vim_strchr(s, ';');
5079#else
5080 e = vim_strchr(s, ':');
5081#endif
5082 if (e == NULL)
5083 e = s + STRLEN(s);
5084
5085 l = e - s;
5086 if (l > MAXPATHL - 5)
5087 break;
5088 vim_strncpy(buf, s, l);
5089 add_pathsep(buf);
5090 l = STRLEN(buf);
5091 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5092
5093 /* Expand matches in one directory of $PATH. */
5094 ret = expand_wildcards(1, &buf, num_file, file, flags);
5095 if (ret == OK)
5096 {
5097 if (ga_grow(&ga, *num_file) == FAIL)
5098 FreeWild(*num_file, *file);
5099 else
5100 {
5101 for (i = 0; i < *num_file; ++i)
5102 {
5103 s = (*file)[i];
5104 if (STRLEN(s) > l)
5105 {
5106 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005107 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005108 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5109 }
5110 else
5111 vim_free(s);
5112 }
5113 vim_free(*file);
5114 }
5115 }
5116 if (*e != NUL)
5117 ++e;
5118 }
5119 *file = ga.ga_data;
5120 *num_file = ga.ga_len;
5121
5122 vim_free(buf);
5123 vim_free(pat);
5124 if (mustfree)
5125 vim_free(path);
5126 return OK;
5127}
5128
5129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005131static 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 +00005132
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00005134 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005135 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005137 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005138call_user_expand_func(
5139 void *(*user_expand_func)(char_u *, int, char_u **, int),
5140 expand_T *xp,
5141 int *num_file,
5142 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005144 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005145 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005148 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005149 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150
Bram Moolenaarb7515462013-06-29 12:58:33 +02005151 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005152 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 *num_file = 0;
5154 *file = NULL;
5155
Bram Moolenaarb7515462013-06-29 12:58:33 +02005156 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005157 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005158 keep = ccline.cmdbuff[ccline.cmdlen];
5159 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005160 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005161
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005162 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005163 args[1] = xp->xp_line;
5164 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 args[2] = num;
5166
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005167 /* Save the cmdline, we don't know what the function may do. */
5168 save_ccline = ccline;
5169 ccline.cmdbuff = NULL;
5170 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005172
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005173 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005174
5175 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005177 if (ccline.cmdbuff != NULL)
5178 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005179
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005180 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005181 return ret;
5182}
5183
5184/*
5185 * Expand names with a function defined by the user.
5186 */
5187 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005188ExpandUserDefined(
5189 expand_T *xp,
5190 regmatch_T *regmatch,
5191 int *num_file,
5192 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005193{
5194 char_u *retstr;
5195 char_u *s;
5196 char_u *e;
5197 char_u keep;
5198 garray_T ga;
5199
5200 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5201 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 return FAIL;
5203
5204 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005205 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
5207 e = vim_strchr(s, '\n');
5208 if (e == NULL)
5209 e = s + STRLEN(s);
5210 keep = *e;
5211 *e = 0;
5212
5213 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5214 {
5215 *e = keep;
5216 if (*e != NUL)
5217 ++e;
5218 continue;
5219 }
5220
5221 if (ga_grow(&ga, 1) == FAIL)
5222 break;
5223
5224 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5225 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
5227 *e = keep;
5228 if (*e != NUL)
5229 ++e;
5230 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005231 vim_free(retstr);
5232 *file = ga.ga_data;
5233 *num_file = ga.ga_len;
5234 return OK;
5235}
5236
5237/*
5238 * Expand names with a list returned by a function defined by the user.
5239 */
5240 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005241ExpandUserList(
5242 expand_T *xp,
5243 int *num_file,
5244 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005245{
5246 list_T *retlist;
5247 listitem_T *li;
5248 garray_T ga;
5249
5250 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5251 if (retlist == NULL)
5252 return FAIL;
5253
5254 ga_init2(&ga, (int)sizeof(char *), 3);
5255 /* Loop over the items in the list. */
5256 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5257 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005258 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5259 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005260
5261 if (ga_grow(&ga, 1) == FAIL)
5262 break;
5263
5264 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005265 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005266 ++ga.ga_len;
5267 }
5268 list_unref(retlist);
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 *file = ga.ga_data;
5271 *num_file = ga.ga_len;
5272 return OK;
5273}
5274#endif
5275
5276/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005277 * Expand color scheme, compiler or filetype names.
5278 * Search from 'runtimepath':
5279 * 'runtimepath'/{dirnames}/{pat}.vim
5280 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5281 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5282 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5283 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005284 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 */
5286 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005287ExpandRTDir(
5288 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005289 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005290 int *num_file,
5291 char_u ***file,
5292 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 char_u *s;
5295 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005296 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005298 int i;
5299 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 *num_file = 0;
5302 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005303 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005304 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005306 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005308 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5309 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005311 ga_clear_strings(&ga);
5312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005314 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005315 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005316 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005318
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005319 if (flags & DIP_START) {
5320 for (i = 0; dirnames[i] != NULL; ++i)
5321 {
5322 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5323 if (s == NULL)
5324 {
5325 ga_clear_strings(&ga);
5326 return FAIL;
5327 }
5328 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5329 globpath(p_pp, s, &ga, 0);
5330 vim_free(s);
5331 }
5332 }
5333
5334 if (flags & DIP_OPT) {
5335 for (i = 0; dirnames[i] != NULL; ++i)
5336 {
5337 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5338 if (s == NULL)
5339 {
5340 ga_clear_strings(&ga);
5341 return FAIL;
5342 }
5343 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5344 globpath(p_pp, s, &ga, 0);
5345 vim_free(s);
5346 }
5347 }
5348
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005349 for (i = 0; i < ga.ga_len; ++i)
5350 {
5351 match = ((char_u **)ga.ga_data)[i];
5352 s = match;
5353 e = s + STRLEN(s);
5354 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5355 {
5356 e -= 4;
5357 for (s = e; s > match; mb_ptr_back(match, s))
5358 if (s < match || vim_ispathsep(*s))
5359 break;
5360 ++s;
5361 *e = NUL;
5362 mch_memmove(match, s, e - s + 1);
5363 }
5364 }
5365
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005366 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005367 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005368
5369 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005370 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005371 remove_duplicates(&ga);
5372
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 *file = ga.ga_data;
5374 *num_file = ga.ga_len;
5375 return OK;
5376}
5377
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005378/*
5379 * Expand loadplugin names:
5380 * 'packpath'/pack/ * /opt/{pat}
5381 */
5382 static int
5383ExpandPackAddDir(
5384 char_u *pat,
5385 int *num_file,
5386 char_u ***file)
5387{
5388 char_u *s;
5389 char_u *e;
5390 char_u *match;
5391 garray_T ga;
5392 int i;
5393 int pat_len;
5394
5395 *num_file = 0;
5396 *file = NULL;
5397 pat_len = (int)STRLEN(pat);
5398 ga_init2(&ga, (int)sizeof(char *), 10);
5399
5400 s = alloc((unsigned)(pat_len + 26));
5401 if (s == NULL)
5402 {
5403 ga_clear_strings(&ga);
5404 return FAIL;
5405 }
5406 sprintf((char *)s, "pack/*/opt/%s*", pat);
5407 globpath(p_pp, s, &ga, 0);
5408 vim_free(s);
5409
5410 for (i = 0; i < ga.ga_len; ++i)
5411 {
5412 match = ((char_u **)ga.ga_data)[i];
5413 s = gettail(match);
5414 e = s + STRLEN(s);
5415 mch_memmove(match, s, e - s + 1);
5416 }
5417
5418 if (ga.ga_len == 0)
5419 return FAIL;
5420
5421 /* Sort and remove duplicates which can happen when specifying multiple
5422 * directories in dirnames. */
5423 remove_duplicates(&ga);
5424
5425 *file = ga.ga_data;
5426 *num_file = ga.ga_len;
5427 return OK;
5428}
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430#endif
5431
5432#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5433/*
5434 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005435 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005437 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005438globpath(
5439 char_u *path,
5440 char_u *file,
5441 garray_T *ga,
5442 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
5444 expand_T xpc;
5445 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 int num_p;
5448 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
5450 buf = alloc(MAXPATHL);
5451 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005452 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005454 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005456
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 /* Loop over all entries in {path}. */
5458 while (*path != NUL)
5459 {
5460 /* Copy one item of the path to buf[] and concatenate the file name. */
5461 copy_option_part(&path, buf, MAXPATHL, ",");
5462 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5463 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005464# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005465 /* Using the platform's path separator (\) makes vim incorrectly
5466 * treat it as an escape character, use '/' instead. */
5467 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5468 STRCAT(buf, "/");
5469# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005473 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5474 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005476 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005478 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 for (i = 0; i < num_p; ++i)
5481 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005482 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005483 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005484 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005487
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 FreeWild(num_p, p);
5489 }
5490 }
5491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494}
5495
5496#endif
5497
5498#if defined(FEAT_CMDHIST) || defined(PROTO)
5499
5500/*********************************
5501 * Command line history stuff *
5502 *********************************/
5503
5504/*
5505 * Translate a history character to the associated type number.
5506 */
5507 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005508hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509{
5510 if (c == ':')
5511 return HIST_CMD;
5512 if (c == '=')
5513 return HIST_EXPR;
5514 if (c == '@')
5515 return HIST_INPUT;
5516 if (c == '>')
5517 return HIST_DEBUG;
5518 return HIST_SEARCH; /* must be '?' or '/' */
5519}
5520
5521/*
5522 * Table of history names.
5523 * These names are used in :history and various hist...() functions.
5524 * It is sufficient to give the significant prefix of a history name.
5525 */
5526
5527static char *(history_names[]) =
5528{
5529 "cmd",
5530 "search",
5531 "expr",
5532 "input",
5533 "debug",
5534 NULL
5535};
5536
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005537#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5538/*
5539 * Function given to ExpandGeneric() to obtain the possible first
5540 * arguments of the ":history command.
5541 */
5542 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005544{
5545 static char_u compl[2] = { NUL, NUL };
5546 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005547 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005548 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5549
5550 if (idx < short_names_count)
5551 {
5552 compl[0] = (char_u)short_names[idx];
5553 return compl;
5554 }
5555 if (idx < short_names_count + history_name_count)
5556 return (char_u *)history_names[idx - short_names_count];
5557 if (idx == short_names_count + history_name_count)
5558 return (char_u *)"all";
5559 return NULL;
5560}
5561#endif
5562
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563/*
5564 * init_history() - Initialize the command line history.
5565 * Also used to re-allocate the history when the size changes.
5566 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005567 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 int newlen; /* new length of history table */
5571 histentry_T *temp;
5572 int i;
5573 int j;
5574 int type;
5575
5576 /*
5577 * If size of history table changed, reallocate it
5578 */
5579 newlen = (int)p_hi;
5580 if (newlen != hislen) /* history length changed */
5581 {
5582 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5583 {
5584 if (newlen)
5585 {
5586 temp = (histentry_T *)lalloc(
5587 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5588 if (temp == NULL) /* out of memory! */
5589 {
5590 if (type == 0) /* first one: just keep the old length */
5591 {
5592 newlen = hislen;
5593 break;
5594 }
5595 /* Already changed one table, now we can only have zero
5596 * length for all tables. */
5597 newlen = 0;
5598 type = -1;
5599 continue;
5600 }
5601 }
5602 else
5603 temp = NULL;
5604 if (newlen == 0 || temp != NULL)
5605 {
5606 if (hisidx[type] < 0) /* there are no entries yet */
5607 {
5608 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005609 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 }
5611 else if (newlen > hislen) /* array becomes bigger */
5612 {
5613 for (i = 0; i <= hisidx[type]; ++i)
5614 temp[i] = history[type][i];
5615 j = i;
5616 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005617 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 for ( ; j < hislen; ++i, ++j)
5619 temp[i] = history[type][j];
5620 }
5621 else /* array becomes smaller or 0 */
5622 {
5623 j = hisidx[type];
5624 for (i = newlen - 1; ; --i)
5625 {
5626 if (i >= 0) /* copy newest entries */
5627 temp[i] = history[type][j];
5628 else /* remove older entries */
5629 vim_free(history[type][j].hisstr);
5630 if (--j < 0)
5631 j = hislen - 1;
5632 if (j == hisidx[type])
5633 break;
5634 }
5635 hisidx[type] = newlen - 1;
5636 }
5637 vim_free(history[type]);
5638 history[type] = temp;
5639 }
5640 }
5641 hislen = newlen;
5642 }
5643}
5644
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005647{
5648 hisptr->hisnum = 0;
5649 hisptr->viminfo = FALSE;
5650 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005651 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005652}
5653
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654/*
5655 * Check if command line 'str' is already in history.
5656 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5657 */
5658 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659in_history(
5660 int type,
5661 char_u *str,
5662 int move_to_front, /* Move the entry to the front if it exists */
5663 int sep,
5664 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 int i;
5667 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005668 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669
5670 if (hisidx[type] < 0)
5671 return FALSE;
5672 i = hisidx[type];
5673 do
5674 {
5675 if (history[type][i].hisstr == NULL)
5676 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005677
5678 /* For search history, check that the separator character matches as
5679 * well. */
5680 p = history[type][i].hisstr;
5681 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005682 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005683 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 {
5685 if (!move_to_front)
5686 return TRUE;
5687 last_i = i;
5688 break;
5689 }
5690 if (--i < 0)
5691 i = hislen - 1;
5692 } while (i != hisidx[type]);
5693
5694 if (last_i >= 0)
5695 {
5696 str = history[type][i].hisstr;
5697 while (i != hisidx[type])
5698 {
5699 if (++i >= hislen)
5700 i = 0;
5701 history[type][last_i] = history[type][i];
5702 last_i = i;
5703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005705 history[type][i].viminfo = FALSE;
5706 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005707 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 return TRUE;
5709 }
5710 return FALSE;
5711}
5712
5713/*
5714 * Convert history name (from table above) to its HIST_ equivalent.
5715 * When "name" is empty, return "cmd" history.
5716 * Returns -1 for unknown history name.
5717 */
5718 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005719get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
5721 int i;
5722 int len = (int)STRLEN(name);
5723
5724 /* No argument: use current history. */
5725 if (len == 0)
5726 return hist_char2type(ccline.cmdfirstc);
5727
5728 for (i = 0; history_names[i] != NULL; ++i)
5729 if (STRNICMP(name, history_names[i], len) == 0)
5730 return i;
5731
5732 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5733 return hist_char2type(name[0]);
5734
5735 return -1;
5736}
5737
5738static int last_maptick = -1; /* last seen maptick */
5739
5740/*
5741 * Add the given string to the given history. If the string is already in the
5742 * history then it is moved to the front. "histype" may be one of he HIST_
5743 * values.
5744 */
5745 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005746add_to_history(
5747 int histype,
5748 char_u *new_entry,
5749 int in_map, /* consider maptick when inside a mapping */
5750 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751{
5752 histentry_T *hisptr;
5753 int len;
5754
5755 if (hislen == 0) /* no history */
5756 return;
5757
Bram Moolenaara939e432013-11-09 05:30:26 +01005758 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5759 return;
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 /*
5762 * Searches inside the same mapping overwrite each other, so that only
5763 * the last line is kept. Be careful not to remove a line that was moved
5764 * down, only lines that were added.
5765 */
5766 if (histype == HIST_SEARCH && in_map)
5767 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005768 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
5770 /* Current line is from the same mapping, remove it */
5771 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5772 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005773 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 --hisnum[histype];
5775 if (--hisidx[HIST_SEARCH] < 0)
5776 hisidx[HIST_SEARCH] = hislen - 1;
5777 }
5778 last_maptick = -1;
5779 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005780 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 {
5782 if (++hisidx[histype] == hislen)
5783 hisidx[histype] = 0;
5784 hisptr = &history[histype][hisidx[histype]];
5785 vim_free(hisptr->hisstr);
5786
5787 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005788 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5790 if (hisptr->hisstr != NULL)
5791 hisptr->hisstr[len + 1] = sep;
5792
5793 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005794 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005795 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 if (histype == HIST_SEARCH && in_map)
5797 last_maptick = maptick;
5798 }
5799}
5800
5801#if defined(FEAT_EVAL) || defined(PROTO)
5802
5803/*
5804 * Get identifier of newest history entry.
5805 * "histype" may be one of the HIST_ values.
5806 */
5807 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005808get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809{
5810 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5811 || hisidx[histype] < 0)
5812 return -1;
5813
5814 return history[histype][hisidx[histype]].hisnum;
5815}
5816
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005817/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 * Calculate history index from a number:
5819 * num > 0: seen as identifying number of a history entry
5820 * num < 0: relative position in history wrt newest entry
5821 * "histype" may be one of the HIST_ values.
5822 */
5823 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005824calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 int i;
5827 histentry_T *hist;
5828 int wrapped = FALSE;
5829
5830 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5831 || (i = hisidx[histype]) < 0 || num == 0)
5832 return -1;
5833
5834 hist = history[histype];
5835 if (num > 0)
5836 {
5837 while (hist[i].hisnum > num)
5838 if (--i < 0)
5839 {
5840 if (wrapped)
5841 break;
5842 i += hislen;
5843 wrapped = TRUE;
5844 }
5845 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5846 return i;
5847 }
5848 else if (-num <= hislen)
5849 {
5850 i += num + 1;
5851 if (i < 0)
5852 i += hislen;
5853 if (hist[i].hisstr != NULL)
5854 return i;
5855 }
5856 return -1;
5857}
5858
5859/*
5860 * Get a history entry by its index.
5861 * "histype" may be one of the HIST_ values.
5862 */
5863 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005864get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 idx = calc_hist_idx(histype, idx);
5867 if (idx >= 0)
5868 return history[histype][idx].hisstr;
5869 else
5870 return (char_u *)"";
5871}
5872
5873/*
5874 * Clear all entries of a history.
5875 * "histype" may be one of the HIST_ values.
5876 */
5877 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005878clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879{
5880 int i;
5881 histentry_T *hisptr;
5882
5883 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5884 {
5885 hisptr = history[histype];
5886 for (i = hislen; i--;)
5887 {
5888 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005889 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005890 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 }
5892 hisidx[histype] = -1; /* mark history as cleared */
5893 hisnum[histype] = 0; /* reset identifier counter */
5894 return OK;
5895 }
5896 return FAIL;
5897}
5898
5899/*
5900 * Remove all entries matching {str} from a history.
5901 * "histype" may be one of the HIST_ values.
5902 */
5903 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005904del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905{
5906 regmatch_T regmatch;
5907 histentry_T *hisptr;
5908 int idx;
5909 int i;
5910 int last;
5911 int found = FALSE;
5912
5913 regmatch.regprog = NULL;
5914 regmatch.rm_ic = FALSE; /* always match case */
5915 if (hislen != 0
5916 && histype >= 0
5917 && histype < HIST_COUNT
5918 && *str != NUL
5919 && (idx = hisidx[histype]) >= 0
5920 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5921 != NULL)
5922 {
5923 i = last = idx;
5924 do
5925 {
5926 hisptr = &history[histype][i];
5927 if (hisptr->hisstr == NULL)
5928 break;
5929 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5930 {
5931 found = TRUE;
5932 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005933 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
5935 else
5936 {
5937 if (i != last)
5938 {
5939 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005940 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 }
5942 if (--last < 0)
5943 last += hislen;
5944 }
5945 if (--i < 0)
5946 i += hislen;
5947 } while (i != idx);
5948 if (history[histype][idx].hisstr == NULL)
5949 hisidx[histype] = -1;
5950 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005951 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 return found;
5953}
5954
5955/*
5956 * Remove an indexed entry from a history.
5957 * "histype" may be one of the HIST_ values.
5958 */
5959 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005960del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
5962 int i, j;
5963
5964 i = calc_hist_idx(histype, idx);
5965 if (i < 0)
5966 return FALSE;
5967 idx = hisidx[histype];
5968 vim_free(history[histype][i].hisstr);
5969
5970 /* When deleting the last added search string in a mapping, reset
5971 * last_maptick, so that the last added search string isn't deleted again.
5972 */
5973 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5974 last_maptick = -1;
5975
5976 while (i != idx)
5977 {
5978 j = (i + 1) % hislen;
5979 history[histype][i] = history[histype][j];
5980 i = j;
5981 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005982 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 if (--i < 0)
5984 i += hislen;
5985 hisidx[histype] = i;
5986 return TRUE;
5987}
5988
5989#endif /* FEAT_EVAL */
5990
5991#if defined(FEAT_CRYPT) || defined(PROTO)
5992/*
5993 * Very specific function to remove the value in ":set key=val" from the
5994 * history.
5995 */
5996 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005997remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998{
5999 char_u *p;
6000 int i;
6001
6002 i = hisidx[HIST_CMD];
6003 if (i < 0)
6004 return;
6005 p = history[HIST_CMD][i].hisstr;
6006 if (p != NULL)
6007 for ( ; *p; ++p)
6008 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6009 {
6010 p = vim_strchr(p + 3, '=');
6011 if (p == NULL)
6012 break;
6013 ++p;
6014 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
6015 if (p[i] == '\\' && p[i + 1])
6016 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006017 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 --p;
6019 }
6020}
6021#endif
6022
6023#endif /* FEAT_CMDHIST */
6024
Bram Moolenaar064154c2016-03-19 22:50:43 +01006025#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006026/*
6027 * Get pointer to the command line info to use. cmdline_paste() may clear
6028 * ccline and put the previous value in prev_ccline.
6029 */
6030 static struct cmdline_info *
6031get_ccline_ptr(void)
6032{
6033 if ((State & CMDLINE) == 0)
6034 return NULL;
6035 if (ccline.cmdbuff != NULL)
6036 return &ccline;
6037 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6038 return &prev_ccline;
6039 return NULL;
6040}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006041#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006042
Bram Moolenaar064154c2016-03-19 22:50:43 +01006043#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006044/*
6045 * Get the current command line in allocated memory.
6046 * Only works when the command line is being edited.
6047 * Returns NULL when something is wrong.
6048 */
6049 char_u *
6050get_cmdline_str(void)
6051{
6052 struct cmdline_info *p = get_ccline_ptr();
6053
6054 if (p == NULL)
6055 return NULL;
6056 return vim_strnsave(p->cmdbuff, p->cmdlen);
6057}
6058
6059/*
6060 * Get the current command line position, counted in bytes.
6061 * Zero is the first position.
6062 * Only works when the command line is being edited.
6063 * Returns -1 when something is wrong.
6064 */
6065 int
6066get_cmdline_pos(void)
6067{
6068 struct cmdline_info *p = get_ccline_ptr();
6069
6070 if (p == NULL)
6071 return -1;
6072 return p->cmdpos;
6073}
6074
6075/*
6076 * Set the command line byte position to "pos". Zero is the first position.
6077 * Only works when the command line is being edited.
6078 * Returns 1 when failed, 0 when OK.
6079 */
6080 int
6081set_cmdline_pos(
6082 int pos)
6083{
6084 struct cmdline_info *p = get_ccline_ptr();
6085
6086 if (p == NULL)
6087 return 1;
6088
6089 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6090 * changed the command line. */
6091 if (pos < 0)
6092 new_cmdpos = 0;
6093 else
6094 new_cmdpos = pos;
6095 return 0;
6096}
6097#endif
6098
6099#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6100/*
6101 * Get the current command-line type.
6102 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6103 * Only works when the command line is being edited.
6104 * Returns NUL when something is wrong.
6105 */
6106 int
6107get_cmdline_type(void)
6108{
6109 struct cmdline_info *p = get_ccline_ptr();
6110
6111 if (p == NULL)
6112 return NUL;
6113 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006114 return
6115# ifdef FEAT_EVAL
6116 (p->input_fn) ? '@' :
6117# endif
6118 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006119 return p->cmdfirstc;
6120}
6121#endif
6122
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6124/*
6125 * Get indices "num1,num2" that specify a range within a list (not a range of
6126 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6127 * Returns OK if parsed successfully, otherwise FAIL.
6128 */
6129 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006130get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131{
6132 int len;
6133 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006134 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
6136 *str = skipwhite(*str);
6137 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6138 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006139 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 *str += len;
6141 *num1 = (int)num;
6142 first = TRUE;
6143 }
6144 *str = skipwhite(*str);
6145 if (**str == ',') /* parse "to" part of range */
6146 {
6147 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006148 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 if (len > 0)
6150 {
6151 *num2 = (int)num;
6152 *str = skipwhite(*str + len);
6153 }
6154 else if (!first) /* no number given at all */
6155 return FAIL;
6156 }
6157 else if (first) /* only one number given */
6158 *num2 = *num1;
6159 return OK;
6160}
6161#endif
6162
6163#if defined(FEAT_CMDHIST) || defined(PROTO)
6164/*
6165 * :history command - print a history
6166 */
6167 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006168ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169{
6170 histentry_T *hist;
6171 int histype1 = HIST_CMD;
6172 int histype2 = HIST_CMD;
6173 int hisidx1 = 1;
6174 int hisidx2 = -1;
6175 int idx;
6176 int i, j, k;
6177 char_u *end;
6178 char_u *arg = eap->arg;
6179
6180 if (hislen == 0)
6181 {
6182 MSG(_("'history' option is zero"));
6183 return;
6184 }
6185
6186 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6187 {
6188 end = arg;
6189 while (ASCII_ISALPHA(*end)
6190 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6191 end++;
6192 i = *end;
6193 *end = NUL;
6194 histype1 = get_histtype(arg);
6195 if (histype1 == -1)
6196 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006197 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 {
6199 histype1 = 0;
6200 histype2 = HIST_COUNT-1;
6201 }
6202 else
6203 {
6204 *end = i;
6205 EMSG(_(e_trailing));
6206 return;
6207 }
6208 }
6209 else
6210 histype2 = histype1;
6211 *end = i;
6212 }
6213 else
6214 end = arg;
6215 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6216 {
6217 EMSG(_(e_trailing));
6218 return;
6219 }
6220
6221 for (; !got_int && histype1 <= histype2; ++histype1)
6222 {
6223 STRCPY(IObuff, "\n # ");
6224 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6225 MSG_PUTS_TITLE(IObuff);
6226 idx = hisidx[histype1];
6227 hist = history[histype1];
6228 j = hisidx1;
6229 k = hisidx2;
6230 if (j < 0)
6231 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6232 if (k < 0)
6233 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6234 if (idx >= 0 && j <= k)
6235 for (i = idx + 1; !got_int; ++i)
6236 {
6237 if (i == hislen)
6238 i = 0;
6239 if (hist[i].hisstr != NULL
6240 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6241 {
6242 msg_putchar('\n');
6243 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6244 hist[i].hisnum);
6245 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6246 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006247 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 else
6249 STRCAT(IObuff, hist[i].hisstr);
6250 msg_outtrans(IObuff);
6251 out_flush();
6252 }
6253 if (i == idx)
6254 break;
6255 }
6256 }
6257}
6258#endif
6259
6260#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006261/*
6262 * Buffers for history read from a viminfo file. Only valid while reading.
6263 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006264static histentry_T *viminfo_history[HIST_COUNT] =
6265 {NULL, NULL, NULL, NULL, NULL};
6266static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6267static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268static int viminfo_add_at_front = FALSE;
6269
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006270static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271
6272/*
6273 * Translate a history type number to the associated character.
6274 */
6275 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006276hist_type2char(
6277 int type,
6278 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 if (type == HIST_CMD)
6281 return ':';
6282 if (type == HIST_SEARCH)
6283 {
6284 if (use_question)
6285 return '?';
6286 else
6287 return '/';
6288 }
6289 if (type == HIST_EXPR)
6290 return '=';
6291 return '@';
6292}
6293
6294/*
6295 * Prepare for reading the history from the viminfo file.
6296 * This allocates history arrays to store the read history lines.
6297 */
6298 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006299prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
6301 int i;
6302 int num;
6303 int type;
6304 int len;
6305
6306 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006307 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 if (asklen > hislen)
6309 asklen = hislen;
6310
6311 for (type = 0; type < HIST_COUNT; ++type)
6312 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006313 /* Count the number of empty spaces in the history list. Entries read
6314 * from viminfo previously are also considered empty. If there are
6315 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006317 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 num++;
6319 len = asklen;
6320 if (num > len)
6321 len = num;
6322 if (len <= 0)
6323 viminfo_history[type] = NULL;
6324 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006325 viminfo_history[type] = (histentry_T *)lalloc(
6326 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (viminfo_history[type] == NULL)
6328 len = 0;
6329 viminfo_hislen[type] = len;
6330 viminfo_hisidx[type] = 0;
6331 }
6332}
6333
6334/*
6335 * Accept a line from the viminfo, store it in the history array when it's
6336 * new.
6337 */
6338 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006339read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340{
6341 int type;
6342 long_u len;
6343 char_u *val;
6344 char_u *p;
6345
6346 type = hist_char2type(virp->vir_line[0]);
6347 if (viminfo_hisidx[type] < viminfo_hislen[type])
6348 {
6349 val = viminfo_readstring(virp, 1, TRUE);
6350 if (val != NULL && *val != NUL)
6351 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006352 int sep = (*val == ' ' ? NUL : *val);
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006355 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 {
6357 /* Need to re-allocate to append the separator byte. */
6358 len = STRLEN(val);
6359 p = lalloc(len + 2, TRUE);
6360 if (p != NULL)
6361 {
6362 if (type == HIST_SEARCH)
6363 {
6364 /* Search entry: Move the separator from the first
6365 * column to after the NUL. */
6366 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006367 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 }
6369 else
6370 {
6371 /* Not a search entry: No separator in the viminfo
6372 * file, add a NUL separator. */
6373 mch_memmove(p, val, (size_t)len + 1);
6374 p[len + 1] = NUL;
6375 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006376 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6377 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006378 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6379 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006380 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 }
6382 }
6383 }
6384 vim_free(val);
6385 }
6386 return viminfo_readline(virp);
6387}
6388
Bram Moolenaar07219f92013-04-14 23:19:36 +02006389/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006390 * Accept a new style history line from the viminfo, store it in the history
6391 * array when it's new.
6392 */
6393 void
6394handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006395 garray_T *values,
6396 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006397{
6398 int type;
6399 long_u len;
6400 char_u *val;
6401 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006402 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006403
6404 /* Check the format:
6405 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006406 if (values->ga_len < 4
6407 || vp[0].bv_type != BVAL_NR
6408 || vp[1].bv_type != BVAL_NR
6409 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6410 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006411 return;
6412
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006413 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006414 if (type >= HIST_COUNT)
6415 return;
6416 if (viminfo_hisidx[type] < viminfo_hislen[type])
6417 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006418 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006419 if (val != NULL && *val != NUL)
6420 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006421 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6422 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006423 int idx;
6424 int overwrite = FALSE;
6425
6426 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6427 {
6428 /* If lines were written by an older Vim we need to avoid
6429 * getting duplicates. See if the entry already exists. */
6430 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6431 {
6432 p = viminfo_history[type][idx].hisstr;
6433 if (STRCMP(val, p) == 0
6434 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6435 {
6436 overwrite = TRUE;
6437 break;
6438 }
6439 }
6440
6441 if (!overwrite)
6442 {
6443 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006444 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006445 p = lalloc(len + 2, TRUE);
6446 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006447 else
6448 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006449 if (p != NULL)
6450 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006451 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006452 if (!overwrite)
6453 {
6454 mch_memmove(p, val, (size_t)len + 1);
6455 /* Put the separator after the NUL. */
6456 p[len + 1] = sep;
6457 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006458 viminfo_history[type][idx].hisnum = 0;
6459 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006460 viminfo_hisidx[type]++;
6461 }
6462 }
6463 }
6464 }
6465 }
6466}
6467
6468/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006469 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006470 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006471 static void
6472concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
6474 int idx;
6475 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006476
6477 idx = hisidx[type] + viminfo_hisidx[type];
6478 if (idx >= hislen)
6479 idx -= hislen;
6480 else if (idx < 0)
6481 idx = hislen - 1;
6482 if (viminfo_add_at_front)
6483 hisidx[type] = idx;
6484 else
6485 {
6486 if (hisidx[type] == -1)
6487 hisidx[type] = hislen - 1;
6488 do
6489 {
6490 if (history[type][idx].hisstr != NULL
6491 || history[type][idx].viminfo)
6492 break;
6493 if (++idx == hislen)
6494 idx = 0;
6495 } while (idx != hisidx[type]);
6496 if (idx != hisidx[type] && --idx < 0)
6497 idx = hislen - 1;
6498 }
6499 for (i = 0; i < viminfo_hisidx[type]; i++)
6500 {
6501 vim_free(history[type][idx].hisstr);
6502 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6503 history[type][idx].viminfo = TRUE;
6504 history[type][idx].time_set = viminfo_history[type][i].time_set;
6505 if (--idx < 0)
6506 idx = hislen - 1;
6507 }
6508 idx += 1;
6509 idx %= hislen;
6510 for (i = 0; i < viminfo_hisidx[type]; i++)
6511 {
6512 history[type][idx++].hisnum = ++hisnum[type];
6513 idx %= hislen;
6514 }
6515}
6516
6517#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6518 static int
6519#ifdef __BORLANDC__
6520_RTLENTRYF
6521#endif
6522sort_hist(const void *s1, const void *s2)
6523{
6524 histentry_T *p1 = *(histentry_T **)s1;
6525 histentry_T *p2 = *(histentry_T **)s2;
6526
6527 if (p1->time_set < p2->time_set) return -1;
6528 if (p1->time_set > p2->time_set) return 1;
6529 return 0;
6530}
6531#endif
6532
6533/*
6534 * Merge history lines from viminfo and lines typed in this Vim based on the
6535 * timestamp;
6536 */
6537 static void
6538merge_history(int type)
6539{
6540 int max_len;
6541 histentry_T **tot_hist;
6542 histentry_T *new_hist;
6543 int i;
6544 int len;
6545
6546 /* Make one long list with all entries. */
6547 max_len = hislen + viminfo_hisidx[type];
6548 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6549 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6550 if (tot_hist == NULL || new_hist == NULL)
6551 {
6552 vim_free(tot_hist);
6553 vim_free(new_hist);
6554 return;
6555 }
6556 for (i = 0; i < viminfo_hisidx[type]; i++)
6557 tot_hist[i] = &viminfo_history[type][i];
6558 len = i;
6559 for (i = 0; i < hislen; i++)
6560 if (history[type][i].hisstr != NULL)
6561 tot_hist[len++] = &history[type][i];
6562
6563 /* Sort the list on timestamp. */
6564 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6565
6566 /* Keep the newest ones. */
6567 for (i = 0; i < hislen; i++)
6568 {
6569 if (i < len)
6570 {
6571 new_hist[i] = *tot_hist[i];
6572 tot_hist[i]->hisstr = NULL;
6573 if (new_hist[i].hisnum == 0)
6574 new_hist[i].hisnum = ++hisnum[type];
6575 }
6576 else
6577 clear_hist_entry(&new_hist[i]);
6578 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006579 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006580
6581 /* Free what is not kept. */
6582 for (i = 0; i < viminfo_hisidx[type]; i++)
6583 vim_free(viminfo_history[type][i].hisstr);
6584 for (i = 0; i < hislen; i++)
6585 vim_free(history[type][i].hisstr);
6586 vim_free(history[type]);
6587 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006588 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006589}
6590
6591/*
6592 * Finish reading history lines from viminfo. Not used when writing viminfo.
6593 */
6594 void
6595finish_viminfo_history(vir_T *virp)
6596{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006598 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
6600 for (type = 0; type < HIST_COUNT; ++type)
6601 {
6602 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006603 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006604
6605 if (merge)
6606 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006608 concat_history(type);
6609
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 vim_free(viminfo_history[type]);
6611 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006612 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 }
6614}
6615
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006616/*
6617 * Write history to viminfo file in "fp".
6618 * When "merge" is TRUE merge history lines with a previously read viminfo
6619 * file, data is in viminfo_history[].
6620 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006623write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
6625 int i;
6626 int type;
6627 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006628 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
6630 init_history();
6631 if (hislen == 0)
6632 return;
6633 for (type = 0; type < HIST_COUNT; ++type)
6634 {
6635 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6636 if (num_saved == 0)
6637 continue;
6638 if (num_saved < 0) /* Use default */
6639 num_saved = hislen;
6640 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6641 type == HIST_CMD ? _("Command Line") :
6642 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006643 type == HIST_EXPR ? _("Expression") :
6644 type == HIST_INPUT ? _("Input Line") :
6645 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (num_saved > hislen)
6647 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006648
6649 /*
6650 * Merge typed and viminfo history:
6651 * round 1: history of typed commands.
6652 * round 2: history from recently read viminfo.
6653 */
6654 for (round = 1; round <= 2; ++round)
6655 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006656 if (round == 1)
6657 /* start at newest entry, somewhere in the list */
6658 i = hisidx[type];
6659 else if (viminfo_hisidx[type] > 0)
6660 /* start at newest entry, first in the list */
6661 i = 0;
6662 else
6663 /* empty list */
6664 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006665 if (i >= 0)
6666 while (num_saved > 0
6667 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006669 char_u *p;
6670 time_t timestamp;
6671 int c = NUL;
6672
6673 if (round == 1)
6674 {
6675 p = history[type][i].hisstr;
6676 timestamp = history[type][i].time_set;
6677 }
6678 else
6679 {
6680 p = viminfo_history[type] == NULL ? NULL
6681 : viminfo_history[type][i].hisstr;
6682 timestamp = viminfo_history[type] == NULL ? 0
6683 : viminfo_history[type][i].time_set;
6684 }
6685
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006686 if (p != NULL && (round == 2
6687 || !merge
6688 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006690 --num_saved;
6691 fputc(hist_type2char(type, TRUE), fp);
6692 /* For the search history: put the separator in the
6693 * second column; use a space if there isn't one. */
6694 if (type == HIST_SEARCH)
6695 {
6696 c = p[STRLEN(p) + 1];
6697 putc(c == NUL ? ' ' : c, fp);
6698 }
6699 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006700
6701 {
6702 char cbuf[NUMBUFLEN];
6703
6704 /* New style history with a bar line. Format:
6705 * |{bartype},{histtype},{timestamp},{separator},"text" */
6706 if (c == NUL)
6707 cbuf[0] = NUL;
6708 else
6709 sprintf(cbuf, "%d", c);
6710 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6711 type, (long)timestamp, cbuf);
6712 barline_writestring(fp, p, LSIZE - 20);
6713 putc('\n', fp);
6714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006716 if (round == 1)
6717 {
6718 /* Decrement index, loop around and stop when back at
6719 * the start. */
6720 if (--i < 0)
6721 i = hislen - 1;
6722 if (i == hisidx[type])
6723 break;
6724 }
6725 else
6726 {
6727 /* Increment index. Stop at the end in the while. */
6728 ++i;
6729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006731 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006732 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006733 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006734 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006735 vim_free(viminfo_history[type]);
6736 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006737 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 }
6739}
6740#endif /* FEAT_VIMINFO */
6741
6742#if defined(FEAT_FKMAP) || defined(PROTO)
6743/*
6744 * Write a character at the current cursor+offset position.
6745 * It is directly written into the command buffer block.
6746 */
6747 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006748cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6751 {
6752 EMSG(_("E198: cmd_pchar beyond the command length"));
6753 return;
6754 }
6755 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6756 ccline.cmdbuff[ccline.cmdlen] = NUL;
6757}
6758
6759 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006760cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
6762 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6763 {
6764 /* EMSG(_("cmd_gchar beyond the command length")); */
6765 return NUL;
6766 }
6767 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6768}
6769#endif
6770
6771#if defined(FEAT_CMDWIN) || defined(PROTO)
6772/*
6773 * Open a window on the current command line and history. Allow editing in
6774 * the window. Returns when the window is closed.
6775 * Returns:
6776 * CR if the command is to be executed
6777 * Ctrl_C if it is to be abandoned
6778 * K_IGNORE if editing continues
6779 */
6780 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006781ex_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
6783 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006784 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006786 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 win_T *wp;
6788 int i;
6789 linenr_T lnum;
6790 int histtype;
6791 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006792#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 int save_restart_edit = restart_edit;
6796 int save_State = State;
6797 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006798#ifdef FEAT_RIGHTLEFT
6799 int save_cmdmsg_rl = cmdmsg_rl;
6800#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006801#ifdef FEAT_FOLDING
6802 int save_KeyTyped;
6803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804
6805 /* Can't do this recursively. Can't do it when typing a password. */
6806 if (cmdwin_type != 0
6807# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6808 || cmdline_star > 0
6809# endif
6810 )
6811 {
6812 beep_flush();
6813 return K_IGNORE;
6814 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006815 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816
6817 /* Save current window sizes. */
6818 win_size_save(&winsizes);
6819
6820# ifdef FEAT_AUTOCMD
6821 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006822 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006824 /* don't use a new tab page */
6825 cmdmod.tab = 0;
6826
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 /* Create a window for the command-line buffer. */
6828 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6829 {
6830 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006831# ifdef FEAT_AUTOCMD
6832 unblock_autocmds();
6833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 return K_IGNORE;
6835 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006836 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837
6838 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006839 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006840 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6842 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6843 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006844#ifdef FEAT_FOLDING
6845 curwin->w_p_fen = FALSE;
6846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006848 curwin->w_p_rl = cmdmsg_rl;
6849 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006851 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
6853# ifdef FEAT_AUTOCMD
6854 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006855 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856# endif
6857
Bram Moolenaar46152342005-09-07 21:18:43 +00006858 /* Showing the prompt may have set need_wait_return, reset it. */
6859 need_wait_return = FALSE;
6860
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006861 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6863 {
6864 if (p_wc == TAB)
6865 {
6866 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6867 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6868 }
6869 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6870 }
6871
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006872 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6873 * sets 'textwidth' to 78). */
6874 curbuf->b_p_tw = 0;
6875
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 /* Fill the buffer with the history. */
6877 init_history();
6878 if (hislen > 0)
6879 {
6880 i = hisidx[histtype];
6881 if (i >= 0)
6882 {
6883 lnum = 0;
6884 do
6885 {
6886 if (++i == hislen)
6887 i = 0;
6888 if (history[histtype][i].hisstr != NULL)
6889 ml_append(lnum++, history[histtype][i].hisstr,
6890 (colnr_T)0, FALSE);
6891 }
6892 while (i != hisidx[histtype]);
6893 }
6894 }
6895
6896 /* Replace the empty last line with the current command-line and put the
6897 * cursor there. */
6898 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6899 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6900 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006901 changed_line_abv_curs();
6902 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006903 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
6905 /* Save the command line info, can be used recursively. */
6906 save_ccline = ccline;
6907 ccline.cmdbuff = NULL;
6908 ccline.cmdprompt = NULL;
6909
6910 /* No Ex mode here! */
6911 exmode_active = 0;
6912
6913 State = NORMAL;
6914# ifdef FEAT_MOUSE
6915 setmouse();
6916# endif
6917
6918# ifdef FEAT_AUTOCMD
6919 /* Trigger CmdwinEnter autocommands. */
6920 typestr[0] = cmdwin_type;
6921 typestr[1] = NUL;
6922 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006923 if (restart_edit != 0) /* autocmd with ":startinsert" */
6924 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925# endif
6926
6927 i = RedrawingDisabled;
6928 RedrawingDisabled = 0;
6929
6930 /*
6931 * Call the main loop until <CR> or CTRL-C is typed.
6932 */
6933 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006934 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
6936 RedrawingDisabled = i;
6937
6938# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006939
6940# ifdef FEAT_FOLDING
6941 save_KeyTyped = KeyTyped;
6942# endif
6943
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 /* Trigger CmdwinLeave autocommands. */
6945 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006946
6947# ifdef FEAT_FOLDING
6948 /* Restore KeyTyped in case it is modified by autocommands */
6949 KeyTyped = save_KeyTyped;
6950# endif
6951
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952# endif
6953
Bram Moolenaarccc18222007-05-10 18:25:20 +00006954 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 ccline = save_ccline;
6956 cmdwin_type = 0;
6957
6958 exmode_active = save_exmode;
6959
Bram Moolenaarf9821062008-06-20 16:31:07 +00006960 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006962 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 {
6964 cmdwin_result = Ctrl_C;
6965 EMSG(_("E199: Active window or buffer deleted"));
6966 }
6967 else
6968 {
6969# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6970 /* autocmds may abort script processing */
6971 if (aborting() && cmdwin_result != K_IGNORE)
6972 cmdwin_result = Ctrl_C;
6973# endif
6974 /* Set the new command line from the cmdline buffer. */
6975 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006976 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006978 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6979
6980 if (histtype == HIST_CMD)
6981 {
6982 /* Execute the command directly. */
6983 ccline.cmdbuff = vim_strsave((char_u *)p);
6984 cmdwin_result = CAR;
6985 }
6986 else
6987 {
6988 /* First need to cancel what we were doing. */
6989 ccline.cmdbuff = NULL;
6990 stuffcharReadbuff(':');
6991 stuffReadbuff((char_u *)p);
6992 stuffcharReadbuff(CAR);
6993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 }
6995 else if (cmdwin_result == K_XF2) /* :qa typed */
6996 {
6997 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6998 cmdwin_result = CAR;
6999 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007000 else if (cmdwin_result == Ctrl_C)
7001 {
7002 /* :q or :close, don't execute any command
7003 * and don't modify the cmd window. */
7004 ccline.cmdbuff = NULL;
7005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 else
7007 ccline.cmdbuff = vim_strsave(ml_get_curline());
7008 if (ccline.cmdbuff == NULL)
7009 cmdwin_result = Ctrl_C;
7010 else
7011 {
7012 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7013 ccline.cmdbufflen = ccline.cmdlen + 1;
7014 ccline.cmdpos = curwin->w_cursor.col;
7015 if (ccline.cmdpos > ccline.cmdlen)
7016 ccline.cmdpos = ccline.cmdlen;
7017 if (cmdwin_result == K_IGNORE)
7018 {
7019 set_cmdspos_cursor();
7020 redrawcmd();
7021 }
7022 }
7023
7024# ifdef FEAT_AUTOCMD
7025 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007026 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007028# ifdef FEAT_CONCEAL
7029 /* Avoid command-line window first character being concealed. */
7030 curwin->w_p_cole = 0;
7031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007033 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 win_goto(old_curwin);
7035 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007036
7037 /* win_close() may have already wiped the buffer when 'bh' is
7038 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007039 if (bufref_valid(&bufref))
7040 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041
7042 /* Restore window sizes. */
7043 win_size_restore(&winsizes);
7044
7045# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007046 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047# endif
7048 }
7049
7050 ga_clear(&winsizes);
7051 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007052# ifdef FEAT_RIGHTLEFT
7053 cmdmsg_rl = save_cmdmsg_rl;
7054# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
7056 State = save_State;
7057# ifdef FEAT_MOUSE
7058 setmouse();
7059# endif
7060
7061 return cmdwin_result;
7062}
7063#endif /* FEAT_CMDWIN */
7064
7065/*
7066 * Used for commands that either take a simple command string argument, or:
7067 * cmd << endmarker
7068 * {script}
7069 * endmarker
7070 * Returns a pointer to allocated memory with {script} or NULL.
7071 */
7072 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007073script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074{
7075 char_u *theline;
7076 char *end_pattern = NULL;
7077 char dot[] = ".";
7078 garray_T ga;
7079
7080 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7081 return NULL;
7082
7083 ga_init2(&ga, 1, 0x400);
7084
7085 if (cmd[2] != NUL)
7086 end_pattern = (char *)skipwhite(cmd + 2);
7087 else
7088 end_pattern = dot;
7089
7090 for (;;)
7091 {
7092 theline = eap->getline(
7093#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007094 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095#endif
7096 NUL, eap->cookie, 0);
7097
7098 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007099 {
7100 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
7104 ga_concat(&ga, theline);
7105 ga_append(&ga, '\n');
7106 vim_free(theline);
7107 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007108 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
7110 return (char_u *)ga.ga_data;
7111}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007112
7113#ifdef FEAT_SEARCH_EXTRA
7114 static void
7115set_search_match(pos_T *t)
7116{
7117 /*
7118 * First move cursor to end of match, then to the start. This
7119 * moves the whole match onto the screen when 'nowrap' is set.
7120 */
7121 t->lnum += search_match_lines;
7122 t->col = search_match_endcol;
7123 if (t->lnum > curbuf->b_ml.ml_line_count)
7124 {
7125 t->lnum = curbuf->b_ml.ml_line_count;
7126 coladvance((colnr_T)MAXCOL);
7127 }
7128}
7129#endif