blob: 01286ac29764d3fa779b30f632f0275e4d6e9ef7 [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 Moolenaar4d850512017-02-09 18:25:14 +0100215#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000216 /* 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 Moolenaarb5aedf32017-03-12 18:23:53 +0100237 CLEAR_POS(&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 */
775 c = ex_window();
776 some_key_typed = TRUE;
777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 }
779# ifdef FEAT_DIGRAPHS
780 else
781# endif
782#endif
783#ifdef FEAT_DIGRAPHS
784 c = do_digraph(c);
785#endif
786
787 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
788 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
789 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000790 /* In Ex mode a backslash escapes a newline. */
791 if (exmode_active
792 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000793 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000794 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000795 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000797 if (c == K_KENTER)
798 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000800 else
801 {
802 gotesc = FALSE; /* Might have typed ESC previously, don't
803 truncate the cmdline now. */
804 if (ccheck_abbr(c + ABBR_OFF))
805 goto cmdline_changed;
806 if (!cmd_silent)
807 {
808 windgoto(msg_row, 0);
809 out_flush();
810 }
811 break;
812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 }
814
815 /*
816 * Completion for 'wildchar' or 'wildcharm' key.
817 * - hitting <ESC> twice means: abandon command line.
818 * - wildcard expansion is only done when the 'wildchar' key is really
819 * typed, not when it comes from a macro
820 */
821 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
822 {
823 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
824 {
825 /* if 'wildmode' contains "list" may still need to list */
826 if (xpc.xp_numfiles > 1
827 && !did_wild_list
828 && (wim_flags[wim_index] & WIM_LIST))
829 {
830 (void)showmatches(&xpc, FALSE);
831 redrawcmd();
832 did_wild_list = TRUE;
833 }
834 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100835 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
836 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100838 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
839 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 else
841 res = OK; /* don't insert 'wildchar' now */
842 }
843 else /* typed p_wc first time */
844 {
845 wim_index = 0;
846 j = ccline.cmdpos;
847 /* if 'wildmode' first contains "longest", get longest
848 * common part */
849 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100850 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
851 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100853 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
854 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 /* if interrupted while completing, behave like it failed */
857 if (got_int)
858 {
859 (void)vpeekc(); /* remove <C-C> from input stream */
860 got_int = FALSE; /* don't abandon the command line */
861 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
862#ifdef FEAT_WILDMENU
863 xpc.xp_context = EXPAND_NOTHING;
864#endif
865 goto cmdline_changed;
866 }
867
868 /* when more than one match, and 'wildmode' first contains
869 * "list", or no change and 'wildmode' contains "longest,list",
870 * list all matches */
871 if (res == OK && xpc.xp_numfiles > 1)
872 {
873 /* a "longest" that didn't do anything is skipped (but not
874 * "list:longest") */
875 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
876 wim_index = 1;
877 if ((wim_flags[wim_index] & WIM_LIST)
878#ifdef FEAT_WILDMENU
879 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
880#endif
881 )
882 {
883 if (!(wim_flags[0] & WIM_LONGEST))
884 {
885#ifdef FEAT_WILDMENU
886 int p_wmnu_save = p_wmnu;
887 p_wmnu = 0;
888#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100889 /* remove match */
890 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#ifdef FEAT_WILDMENU
892 p_wmnu = p_wmnu_save;
893#endif
894 }
895#ifdef FEAT_WILDMENU
896 (void)showmatches(&xpc, p_wmnu
897 && ((wim_flags[wim_index] & WIM_LIST) == 0));
898#else
899 (void)showmatches(&xpc, FALSE);
900#endif
901 redrawcmd();
902 did_wild_list = TRUE;
903 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100904 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
905 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100907 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
908 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 }
910 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200911 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913#ifdef FEAT_WILDMENU
914 else if (xpc.xp_numfiles == -1)
915 xpc.xp_context = EXPAND_NOTHING;
916#endif
917 }
918 if (wim_index < 3)
919 ++wim_index;
920 if (c == ESC)
921 gotesc = TRUE;
922 if (res == OK)
923 goto cmdline_changed;
924 }
925
926 gotesc = FALSE;
927
928 /* <S-Tab> goes to last match, in a clumsy way */
929 if (c == K_S_TAB && KeyTyped)
930 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100931 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
932 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
933 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 goto cmdline_changed;
935 }
936
937 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
938 c = NL;
939
940 do_abbr = TRUE; /* default: check for abbreviation */
941
942 /*
943 * Big switch for a typed command line character.
944 */
945 switch (c)
946 {
947 case K_BS:
948 case Ctrl_H:
949 case K_DEL:
950 case K_KDEL:
951 case Ctrl_W:
952#ifdef FEAT_FKMAP
953 if (cmd_fkmap && c == K_BS)
954 c = K_DEL;
955#endif
956 if (c == K_KDEL)
957 c = K_DEL;
958
959 /*
960 * delete current character is the same as backspace on next
961 * character, except at end of line
962 */
963 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
964 ++ccline.cmdpos;
965#ifdef FEAT_MBYTE
966 if (has_mbyte && c == K_DEL)
967 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
968 ccline.cmdbuff + ccline.cmdpos);
969#endif
970 if (ccline.cmdpos > 0)
971 {
972 char_u *p;
973
974 j = ccline.cmdpos;
975 p = ccline.cmdbuff + j;
976#ifdef FEAT_MBYTE
977 if (has_mbyte)
978 {
979 p = mb_prevptr(ccline.cmdbuff, p);
980 if (c == Ctrl_W)
981 {
982 while (p > ccline.cmdbuff && vim_isspace(*p))
983 p = mb_prevptr(ccline.cmdbuff, p);
984 i = mb_get_class(p);
985 while (p > ccline.cmdbuff && mb_get_class(p) == i)
986 p = mb_prevptr(ccline.cmdbuff, p);
987 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000988 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990 }
991 else
992#endif
993 if (c == Ctrl_W)
994 {
995 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
996 --p;
997 i = vim_iswordc(p[-1]);
998 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
999 && vim_iswordc(p[-1]) == i)
1000 --p;
1001 }
1002 else
1003 --p;
1004 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1005 ccline.cmdlen -= j - ccline.cmdpos;
1006 i = ccline.cmdpos;
1007 while (i < ccline.cmdlen)
1008 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1009
1010 /* Truncate at the end, required for multi-byte chars. */
1011 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001012#ifdef FEAT_SEARCH_EXTRA
1013 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001014 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001015 search_start = save_cursor;
1016 /* save view settings, so that the screen
1017 * won't be restored at the wrong position */
1018 old_curswant = init_curswant;
1019 old_leftcol = init_leftcol;
1020 old_topline = init_topline;
1021# ifdef FEAT_DIFF
1022 old_topfill = init_topfill;
1023# endif
1024 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001025 }
1026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 redrawcmd();
1028 }
1029 else if (ccline.cmdlen == 0 && c != Ctrl_W
1030 && ccline.cmdprompt == NULL && indent == 0)
1031 {
1032 /* In ex and debug mode it doesn't make sense to return. */
1033 if (exmode_active
1034#ifdef FEAT_EVAL
1035 || ccline.cmdfirstc == '>'
1036#endif
1037 )
1038 goto cmdline_not_changed;
1039
1040 vim_free(ccline.cmdbuff); /* no commandline to return */
1041 ccline.cmdbuff = NULL;
1042 if (!cmd_silent)
1043 {
1044#ifdef FEAT_RIGHTLEFT
1045 if (cmdmsg_rl)
1046 msg_col = Columns;
1047 else
1048#endif
1049 msg_col = 0;
1050 msg_putchar(' '); /* delete ':' */
1051 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001052#ifdef FEAT_SEARCH_EXTRA
1053 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001054 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 redraw_cmdline = TRUE;
1057 goto returncmd; /* back to cmd mode */
1058 }
1059 goto cmdline_changed;
1060
1061 case K_INS:
1062 case K_KINS:
1063#ifdef FEAT_FKMAP
1064 /* if Farsi mode set, we are in reverse insert mode -
1065 Do not change the mode */
1066 if (cmd_fkmap)
1067 beep_flush();
1068 else
1069#endif
1070 ccline.overstrike = !ccline.overstrike;
1071#ifdef CURSOR_SHAPE
1072 ui_cursor_shape(); /* may show different cursor shape */
1073#endif
1074 goto cmdline_not_changed;
1075
1076 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001077 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
1079 /* ":lmap" mappings exists, toggle use of mappings. */
1080 State ^= LANGMAP;
1081#ifdef USE_IM_CONTROL
1082 im_set_active(FALSE); /* Disable input method */
1083#endif
1084 if (b_im_ptr != NULL)
1085 {
1086 if (State & LANGMAP)
1087 *b_im_ptr = B_IMODE_LMAP;
1088 else
1089 *b_im_ptr = B_IMODE_NONE;
1090 }
1091 }
1092#ifdef USE_IM_CONTROL
1093 else
1094 {
1095 /* There are no ":lmap" mappings, toggle IM. When
1096 * 'imdisable' is set don't try getting the status, it's
1097 * always off. */
1098 if ((p_imdisable && b_im_ptr != NULL)
1099 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1100 {
1101 im_set_active(FALSE); /* Disable input method */
1102 if (b_im_ptr != NULL)
1103 *b_im_ptr = B_IMODE_NONE;
1104 }
1105 else
1106 {
1107 im_set_active(TRUE); /* Enable input method */
1108 if (b_im_ptr != NULL)
1109 *b_im_ptr = B_IMODE_IM;
1110 }
1111 }
1112#endif
1113 if (b_im_ptr != NULL)
1114 {
1115 if (b_im_ptr == &curbuf->b_p_iminsert)
1116 set_iminsert_global();
1117 else
1118 set_imsearch_global();
1119 }
1120#ifdef CURSOR_SHAPE
1121 ui_cursor_shape(); /* may show different cursor shape */
1122#endif
1123#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1124 /* Show/unshow value of 'keymap' in status lines later. */
1125 status_redraw_curbuf();
1126#endif
1127 goto cmdline_not_changed;
1128
1129/* case '@': only in very old vi */
1130 case Ctrl_U:
1131 /* delete all characters left of the cursor */
1132 j = ccline.cmdpos;
1133 ccline.cmdlen -= j;
1134 i = ccline.cmdpos = 0;
1135 while (i < ccline.cmdlen)
1136 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1137 /* Truncate at the end, required for multi-byte chars. */
1138 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001139#ifdef FEAT_SEARCH_EXTRA
1140 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001141 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 redrawcmd();
1144 goto cmdline_changed;
1145
1146#ifdef FEAT_CLIPBOARD
1147 case Ctrl_Y:
1148 /* Copy the modeless selection, if there is one. */
1149 if (clip_star.state != SELECT_CLEARED)
1150 {
1151 if (clip_star.state == SELECT_DONE)
1152 clip_copy_modeless_selection(TRUE);
1153 goto cmdline_not_changed;
1154 }
1155 break;
1156#endif
1157
1158 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1159 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001160 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001161 * ":normal" runs out of characters. */
1162 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001163 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 goto cmdline_not_changed;
1165
1166 gotesc = TRUE; /* will free ccline.cmdbuff after
1167 putting it in history */
1168 goto returncmd; /* back to cmd mode */
1169
1170 case Ctrl_R: /* insert register */
1171#ifdef USE_ON_FLY_SCROLL
1172 dont_scroll = TRUE; /* disallow scrolling here */
1173#endif
1174 putcmdline('"', TRUE);
1175 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001176 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 if (i == Ctrl_O)
1178 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1179 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001180 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 --no_mapping;
1182#ifdef FEAT_EVAL
1183 /*
1184 * Insert the result of an expression.
1185 * Need to save the current command line, to be able to enter
1186 * a new one...
1187 */
1188 new_cmdpos = -1;
1189 if (c == '=')
1190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1192 {
1193 beep_flush();
1194 c = ESC;
1195 }
1196 else
1197 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001198 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001200 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202 }
1203#endif
1204 if (c != ESC) /* use ESC to cancel inserting register */
1205 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001206 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001207
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001208#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001209 /* When there was a serious error abort getting the
1210 * command line. */
1211 if (aborting())
1212 {
1213 gotesc = TRUE; /* will free ccline.cmdbuff after
1214 putting it in history */
1215 goto returncmd; /* back to cmd mode */
1216 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 KeyTyped = FALSE; /* Don't do p_wc completion. */
1219#ifdef FEAT_EVAL
1220 if (new_cmdpos >= 0)
1221 {
1222 /* set_cmdline_pos() was used */
1223 if (new_cmdpos > ccline.cmdlen)
1224 ccline.cmdpos = ccline.cmdlen;
1225 else
1226 ccline.cmdpos = new_cmdpos;
1227 }
1228#endif
1229 }
1230 redrawcmd();
1231 goto cmdline_changed;
1232
1233 case Ctrl_D:
1234 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1235 break; /* Use ^D as normal char instead */
1236
1237 redrawcmd();
1238 continue; /* don't do incremental search now */
1239
1240 case K_RIGHT:
1241 case K_S_RIGHT:
1242 case K_C_RIGHT:
1243 do
1244 {
1245 if (ccline.cmdpos >= ccline.cmdlen)
1246 break;
1247 i = cmdline_charsize(ccline.cmdpos);
1248 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1249 break;
1250 ccline.cmdspos += i;
1251#ifdef FEAT_MBYTE
1252 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001253 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 + ccline.cmdpos);
1255 else
1256#endif
1257 ++ccline.cmdpos;
1258 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001259 while ((c == K_S_RIGHT || c == K_C_RIGHT
1260 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1262#ifdef FEAT_MBYTE
1263 if (has_mbyte)
1264 set_cmdspos_cursor();
1265#endif
1266 goto cmdline_not_changed;
1267
1268 case K_LEFT:
1269 case K_S_LEFT:
1270 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001271 if (ccline.cmdpos == 0)
1272 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 do
1274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 --ccline.cmdpos;
1276#ifdef FEAT_MBYTE
1277 if (has_mbyte) /* move to first byte of char */
1278 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1279 ccline.cmdbuff + ccline.cmdpos);
1280#endif
1281 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1282 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001283 while (ccline.cmdpos > 0
1284 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001285 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1287#ifdef FEAT_MBYTE
1288 if (has_mbyte)
1289 set_cmdspos_cursor();
1290#endif
1291 goto cmdline_not_changed;
1292
1293 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001294 /* Ignore mouse event or ex_window() result. */
1295 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
Bram Moolenaar4770d092006-01-12 23:22:24 +00001297#ifdef FEAT_GUI_W32
1298 /* On Win32 ignore <M-F4>, we get it when closing the window was
1299 * cancelled. */
1300 case K_F4:
1301 if (mod_mask == MOD_MASK_ALT)
1302 {
1303 redrawcmd(); /* somehow the cmdline is cleared */
1304 goto cmdline_not_changed;
1305 }
1306 break;
1307#endif
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309#ifdef FEAT_MOUSE
1310 case K_MIDDLEDRAG:
1311 case K_MIDDLERELEASE:
1312 goto cmdline_not_changed; /* Ignore mouse */
1313
1314 case K_MIDDLEMOUSE:
1315# ifdef FEAT_GUI
1316 /* When GUI is active, also paste when 'mouse' is empty */
1317 if (!gui.in_use)
1318# endif
1319 if (!mouse_has(MOUSE_COMMAND))
1320 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001321# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001323 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001325# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001326 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 redrawcmd();
1328 goto cmdline_changed;
1329
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001330# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001332 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 redrawcmd();
1334 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001335# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
1337 case K_LEFTDRAG:
1338 case K_LEFTRELEASE:
1339 case K_RIGHTDRAG:
1340 case K_RIGHTRELEASE:
1341 /* Ignore drag and release events when the button-down wasn't
1342 * seen before. */
1343 if (ignore_drag_release)
1344 goto cmdline_not_changed;
1345 /* FALLTHROUGH */
1346 case K_LEFTMOUSE:
1347 case K_RIGHTMOUSE:
1348 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1349 ignore_drag_release = TRUE;
1350 else
1351 ignore_drag_release = FALSE;
1352# ifdef FEAT_GUI
1353 /* When GUI is active, also move when 'mouse' is empty */
1354 if (!gui.in_use)
1355# endif
1356 if (!mouse_has(MOUSE_COMMAND))
1357 goto cmdline_not_changed; /* Ignore mouse */
1358# ifdef FEAT_CLIPBOARD
1359 if (mouse_row < cmdline_row && clip_star.available)
1360 {
1361 int button, is_click, is_drag;
1362
1363 /*
1364 * Handle modeless selection.
1365 */
1366 button = get_mouse_button(KEY2TERMCAP1(c),
1367 &is_click, &is_drag);
1368 if (mouse_model_popup() && button == MOUSE_LEFT
1369 && (mod_mask & MOD_MASK_SHIFT))
1370 {
1371 /* Translate shift-left to right button. */
1372 button = MOUSE_RIGHT;
1373 mod_mask &= ~MOD_MASK_SHIFT;
1374 }
1375 clip_modeless(button, is_click, is_drag);
1376 goto cmdline_not_changed;
1377 }
1378# endif
1379
1380 set_cmdspos();
1381 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1382 ++ccline.cmdpos)
1383 {
1384 i = cmdline_charsize(ccline.cmdpos);
1385 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1386 && mouse_col < ccline.cmdspos % Columns + i)
1387 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001388# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 if (has_mbyte)
1390 {
1391 /* Count ">" for double-wide char that doesn't fit. */
1392 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001393 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 + ccline.cmdpos) - 1;
1395 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 ccline.cmdspos += i;
1398 }
1399 goto cmdline_not_changed;
1400
1401 /* Mouse scroll wheel: ignored here */
1402 case K_MOUSEDOWN:
1403 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001404 case K_MOUSELEFT:
1405 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /* Alternate buttons ignored here */
1407 case K_X1MOUSE:
1408 case K_X1DRAG:
1409 case K_X1RELEASE:
1410 case K_X2MOUSE:
1411 case K_X2DRAG:
1412 case K_X2RELEASE:
1413 goto cmdline_not_changed;
1414
1415#endif /* FEAT_MOUSE */
1416
1417#ifdef FEAT_GUI
1418 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1419 case K_LEFTRELEASE_NM:
1420 goto cmdline_not_changed;
1421
1422 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001423 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 {
1425 gui_do_scroll();
1426 redrawcmd();
1427 }
1428 goto cmdline_not_changed;
1429
1430 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001431 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001433 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 redrawcmd();
1435 }
1436 goto cmdline_not_changed;
1437#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001438#ifdef FEAT_GUI_TABLINE
1439 case K_TABLINE:
1440 case K_TABMENU:
1441 /* Don't want to change any tabs here. Make sure the same tab
1442 * is still selected. */
1443 if (gui_use_tabline())
1444 gui_mch_set_curtab(tabpage_index(curtab));
1445 goto cmdline_not_changed;
1446#endif
1447
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 case K_SELECT: /* end of Select mode mapping - ignore */
1449 goto cmdline_not_changed;
1450
1451 case Ctrl_B: /* begin of command line */
1452 case K_HOME:
1453 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 case K_S_HOME:
1455 case K_C_HOME:
1456 ccline.cmdpos = 0;
1457 set_cmdspos();
1458 goto cmdline_not_changed;
1459
1460 case Ctrl_E: /* end of command line */
1461 case K_END:
1462 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 case K_S_END:
1464 case K_C_END:
1465 ccline.cmdpos = ccline.cmdlen;
1466 set_cmdspos_cursor();
1467 goto cmdline_not_changed;
1468
1469 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001470 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 break;
1472 goto cmdline_changed;
1473
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001474 case Ctrl_L:
1475#ifdef FEAT_SEARCH_EXTRA
1476 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1477 {
1478 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001479 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001480 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001481 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001482 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001483 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001484 c = gchar_cursor();
1485 /* If 'ignorecase' and 'smartcase' are set and the
1486 * command line has no uppercase characters, convert
1487 * the character to lowercase */
1488 if (p_ic && p_scs
1489 && !pat_has_uppercase(ccline.cmdbuff))
1490 c = MB_TOLOWER(c);
1491 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001492 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001493 if (c == firstc || vim_strchr((char_u *)(
1494 p_magic ? "\\^$.*[" : "\\^$"), c)
1495 != NULL)
1496 {
1497 /* put a backslash before special
1498 * characters */
1499 stuffcharReadbuff(c);
1500 c = '\\';
1501 }
1502 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001503 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001504 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001505 }
1506 goto cmdline_not_changed;
1507 }
1508#endif
1509
1510 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001511 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 break;
1513 goto cmdline_changed;
1514
1515 case Ctrl_N: /* next match */
1516 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001517 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001519 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1520 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001522 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
Bram Moolenaar11956692016-08-27 16:26:56 +02001524 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
1526#ifdef FEAT_CMDHIST
1527 case K_UP:
1528 case K_DOWN:
1529 case K_S_UP:
1530 case K_S_DOWN:
1531 case K_PAGEUP:
1532 case K_KPAGEUP:
1533 case K_PAGEDOWN:
1534 case K_KPAGEDOWN:
1535 if (hislen == 0 || firstc == NUL) /* no history */
1536 goto cmdline_not_changed;
1537
1538 i = hiscnt;
1539
1540 /* save current command string so it can be restored later */
1541 if (lookfor == NULL)
1542 {
1543 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1544 goto cmdline_not_changed;
1545 lookfor[ccline.cmdpos] = NUL;
1546 }
1547
1548 j = (int)STRLEN(lookfor);
1549 for (;;)
1550 {
1551 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001552 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001553 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 {
1555 if (hiscnt == hislen) /* first time */
1556 hiscnt = hisidx[histype];
1557 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1558 hiscnt = hislen - 1;
1559 else if (hiscnt != hisidx[histype] + 1)
1560 --hiscnt;
1561 else /* at top of list */
1562 {
1563 hiscnt = i;
1564 break;
1565 }
1566 }
1567 else /* one step forwards */
1568 {
1569 /* on last entry, clear the line */
1570 if (hiscnt == hisidx[histype])
1571 {
1572 hiscnt = hislen;
1573 break;
1574 }
1575
1576 /* not on a history line, nothing to do */
1577 if (hiscnt == hislen)
1578 break;
1579 if (hiscnt == hislen - 1) /* wrap around */
1580 hiscnt = 0;
1581 else
1582 ++hiscnt;
1583 }
1584 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1585 {
1586 hiscnt = i;
1587 break;
1588 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001589 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001590 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 || STRNCMP(history[histype][hiscnt].hisstr,
1592 lookfor, (size_t)j) == 0)
1593 break;
1594 }
1595
1596 if (hiscnt != i) /* jumped to other entry */
1597 {
1598 char_u *p;
1599 int len;
1600 int old_firstc;
1601
1602 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001603 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (hiscnt == hislen)
1605 p = lookfor; /* back to the old one */
1606 else
1607 p = history[histype][hiscnt].hisstr;
1608
1609 if (histype == HIST_SEARCH
1610 && p != lookfor
1611 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1612 {
1613 /* Correct for the separator character used when
1614 * adding the history entry vs the one used now.
1615 * First loop: count length.
1616 * Second loop: copy the characters. */
1617 for (i = 0; i <= 1; ++i)
1618 {
1619 len = 0;
1620 for (j = 0; p[j] != NUL; ++j)
1621 {
1622 /* Replace old sep with new sep, unless it is
1623 * escaped. */
1624 if (p[j] == old_firstc
1625 && (j == 0 || p[j - 1] != '\\'))
1626 {
1627 if (i > 0)
1628 ccline.cmdbuff[len] = firstc;
1629 }
1630 else
1631 {
1632 /* Escape new sep, unless it is already
1633 * escaped. */
1634 if (p[j] == firstc
1635 && (j == 0 || p[j - 1] != '\\'))
1636 {
1637 if (i > 0)
1638 ccline.cmdbuff[len] = '\\';
1639 ++len;
1640 }
1641 if (i > 0)
1642 ccline.cmdbuff[len] = p[j];
1643 }
1644 ++len;
1645 }
1646 if (i == 0)
1647 {
1648 alloc_cmdbuff(len);
1649 if (ccline.cmdbuff == NULL)
1650 goto returncmd;
1651 }
1652 }
1653 ccline.cmdbuff[len] = NUL;
1654 }
1655 else
1656 {
1657 alloc_cmdbuff((int)STRLEN(p));
1658 if (ccline.cmdbuff == NULL)
1659 goto returncmd;
1660 STRCPY(ccline.cmdbuff, p);
1661 }
1662
1663 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1664 redrawcmd();
1665 goto cmdline_changed;
1666 }
1667 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001668#endif
1669 goto cmdline_not_changed;
1670
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001671#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001672 case Ctrl_G: /* next match */
1673 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001674 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1675 {
1676 pos_T t;
1677 int search_flags = SEARCH_KEEP + SEARCH_NOOF
1678 + SEARCH_PEEK;
1679
1680 if (char_avail())
1681 continue;
1682 cursor_off();
1683 out_flush();
1684 if (c == Ctrl_G)
1685 {
1686 t = match_end;
1687 search_flags += SEARCH_COL;
1688 }
1689 else
1690 t = match_start;
1691 ++emsg_off;
1692 i = searchit(curwin, curbuf, &t,
1693 c == Ctrl_G ? FORWARD : BACKWARD,
1694 ccline.cmdbuff, count, search_flags,
1695 RE_SEARCH, 0, NULL);
1696 --emsg_off;
1697 if (i)
1698 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001699 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001700 match_end = t;
1701 match_start = t;
1702 if (c == Ctrl_T && firstc == '/')
1703 {
1704 /* move just before the current match, so that
1705 * when nv_search finishes the cursor will be
1706 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001707 search_start = t;
1708 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001709 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001710 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001711 {
1712 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001713 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001714 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001715 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001716 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001717 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001718 }
1719
1720 set_search_match(&match_end);
1721 curwin->w_cursor = match_start;
1722 changed_cline_bef_curs();
1723 update_topline();
1724 validate_cursor();
1725 highlight_match = TRUE;
1726 old_curswant = curwin->w_curswant;
1727 old_leftcol = curwin->w_leftcol;
1728 old_topline = curwin->w_topline;
1729# ifdef FEAT_DIFF
1730 old_topfill = curwin->w_topfill;
1731# endif
1732 old_botline = curwin->w_botline;
1733 update_screen(NOT_VALID);
1734 redrawcmdline();
1735 }
1736 else
1737 vim_beep(BO_ERROR);
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001738 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001739 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001740 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741#endif
1742
1743 case Ctrl_V:
1744 case Ctrl_Q:
1745#ifdef FEAT_MOUSE
1746 ignore_drag_release = TRUE;
1747#endif
1748 putcmdline('^', TRUE);
1749 c = get_literal(); /* get next (two) character(s) */
1750 do_abbr = FALSE; /* don't do abbreviation now */
1751#ifdef FEAT_MBYTE
1752 /* may need to remove ^ when composing char was typed */
1753 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1754 {
1755 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1756 msg_putchar(' ');
1757 cursorcmd();
1758 }
1759#endif
1760 break;
1761
1762#ifdef FEAT_DIGRAPHS
1763 case Ctrl_K:
1764#ifdef FEAT_MOUSE
1765 ignore_drag_release = TRUE;
1766#endif
1767 putcmdline('?', TRUE);
1768#ifdef USE_ON_FLY_SCROLL
1769 dont_scroll = TRUE; /* disallow scrolling here */
1770#endif
1771 c = get_digraph(TRUE);
1772 if (c != NUL)
1773 break;
1774
1775 redrawcmd();
1776 goto cmdline_not_changed;
1777#endif /* FEAT_DIGRAPHS */
1778
1779#ifdef FEAT_RIGHTLEFT
1780 case Ctrl__: /* CTRL-_: switch language mode */
1781 if (!p_ari)
1782 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001783# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 if (p_altkeymap)
1785 {
1786 cmd_fkmap = !cmd_fkmap;
1787 if (cmd_fkmap) /* in Farsi always in Insert mode */
1788 ccline.overstrike = FALSE;
1789 }
1790 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001791# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 cmd_hkmap = !cmd_hkmap;
1793 goto cmdline_not_changed;
1794#endif
1795
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001796 case K_PS:
1797 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1798 goto cmdline_changed;
1799
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 default:
1801#ifdef UNIX
1802 if (c == intr_char)
1803 {
1804 gotesc = TRUE; /* will free ccline.cmdbuff after
1805 putting it in history */
1806 goto returncmd; /* back to Normal mode */
1807 }
1808#endif
1809 /*
1810 * Normal character with no special meaning. Just set mod_mask
1811 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1812 * the string <S-Space>. This should only happen after ^V.
1813 */
1814 if (!IS_SPECIAL(c))
1815 mod_mask = 0x0;
1816 break;
1817 }
1818 /*
1819 * End of switch on command line character.
1820 * We come here if we have a normal character.
1821 */
1822
Bram Moolenaarede3e632013-06-23 16:16:19 +02001823 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#ifdef FEAT_MBYTE
1825 /* Add ABBR_OFF for characters above 0x100, this is
1826 * what check_abbr() expects. */
1827 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1828#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001829 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 goto cmdline_changed;
1831
1832 /*
1833 * put the character in the command line
1834 */
1835 if (IS_SPECIAL(c) || mod_mask != 0)
1836 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1837 else
1838 {
1839#ifdef FEAT_MBYTE
1840 if (has_mbyte)
1841 {
1842 j = (*mb_char2bytes)(c, IObuff);
1843 IObuff[j] = NUL; /* exclude composing chars */
1844 put_on_cmdline(IObuff, j, TRUE);
1845 }
1846 else
1847#endif
1848 {
1849 IObuff[0] = c;
1850 put_on_cmdline(IObuff, 1, TRUE);
1851 }
1852 }
1853 goto cmdline_changed;
1854
1855/*
1856 * This part implements incremental searches for "/" and "?"
1857 * Jump to cmdline_not_changed when a character has been read but the command
1858 * line did not change. Then we only search and redraw if something changed in
1859 * the past.
1860 * Jump to cmdline_changed when the command line did change.
1861 * (Sorry for the goto's, I know it is ugly).
1862 */
1863cmdline_not_changed:
1864#ifdef FEAT_SEARCH_EXTRA
1865 if (!incsearch_postponed)
1866 continue;
1867#endif
1868
1869cmdline_changed:
1870#ifdef FEAT_SEARCH_EXTRA
1871 /*
1872 * 'incsearch' highlighting.
1873 */
1874 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1875 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001876 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001877#ifdef FEAT_RELTIME
1878 proftime_T tm;
1879#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 /* if there is a character waiting, search and redraw later */
1882 if (char_avail())
1883 {
1884 incsearch_postponed = TRUE;
1885 continue;
1886 }
1887 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001888 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890 /* If there is no command line, don't do anything */
1891 if (ccline.cmdlen == 0)
1892 i = 0;
1893 else
1894 {
1895 cursor_off(); /* so the user knows we're busy */
1896 out_flush();
1897 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001898#ifdef FEAT_RELTIME
1899 /* Set the time limit to half a second. */
1900 profile_setlimit(500L, &tm);
1901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001903 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1904#ifdef FEAT_RELTIME
1905 &tm
1906#else
1907 NULL
1908#endif
1909 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 --emsg_off;
1911 /* if interrupted while searching, behave like it failed */
1912 if (got_int)
1913 {
1914 (void)vpeekc(); /* remove <C-C> from input stream */
1915 got_int = FALSE; /* don't abandon the command line */
1916 i = 0;
1917 }
1918 else if (char_avail())
1919 /* cancelled searching because a char was typed */
1920 incsearch_postponed = TRUE;
1921 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001922 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 highlight_match = TRUE; /* highlight position */
1924 else
1925 highlight_match = FALSE; /* remove highlight */
1926
1927 /* first restore the old curwin values, so the screen is
1928 * positioned in the same way as the actual search command */
1929 curwin->w_leftcol = old_leftcol;
1930 curwin->w_topline = old_topline;
1931# ifdef FEAT_DIFF
1932 curwin->w_topfill = old_topfill;
1933# endif
1934 curwin->w_botline = old_botline;
1935 changed_cline_bef_curs();
1936 update_topline();
1937
1938 if (i != 0)
1939 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001940 pos_T save_pos = curwin->w_cursor;
1941
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001942 match_start = curwin->w_cursor;
1943 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001945 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001946 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001947 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001949 else
1950 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001951
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001953# ifdef FEAT_WINDOWS
1954 /* May redraw the status line to show the cursor position. */
1955 if (p_ru && curwin->w_status_height > 0)
1956 curwin->w_redr_status = TRUE;
1957# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001959 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001960 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001961 restore_cmdline(&save_ccline);
1962
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001963 /* Leave it at the end to make CTRL-R CTRL-W work. */
1964 if (i != 0)
1965 curwin->w_cursor = end_pos;
1966
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 msg_starthere();
1968 redrawcmdline();
1969 did_incsearch = TRUE;
1970 }
1971#else /* FEAT_SEARCH_EXTRA */
1972 ;
1973#endif
1974
1975#ifdef FEAT_RIGHTLEFT
1976 if (cmdmsg_rl
1977# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001978 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979# endif
1980 )
1981 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001982 * right-left typing. Not efficient, but it works.
1983 * Do it only when there are no characters left to read
1984 * to avoid useless intermediate redraws. */
1985 if (vpeekc() == NUL)
1986 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987#endif
1988 }
1989
1990returncmd:
1991
1992#ifdef FEAT_RIGHTLEFT
1993 cmdmsg_rl = FALSE;
1994#endif
1995
1996#ifdef FEAT_FKMAP
1997 cmd_fkmap = 0;
1998#endif
1999
2000 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002001 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
2003#ifdef FEAT_SEARCH_EXTRA
2004 if (did_incsearch)
2005 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002006 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002007 curwin->w_cursor = save_cursor;
2008 else
2009 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002010 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002011 {
2012 /* put the '" mark at the original position */
2013 curwin->w_cursor = save_cursor;
2014 setpcmark();
2015 }
2016 curwin->w_cursor = search_start;
2017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 curwin->w_curswant = old_curswant;
2019 curwin->w_leftcol = old_leftcol;
2020 curwin->w_topline = old_topline;
2021# ifdef FEAT_DIFF
2022 curwin->w_topfill = old_topfill;
2023# endif
2024 curwin->w_botline = old_botline;
2025 highlight_match = FALSE;
2026 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002027 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029#endif
2030
2031 if (ccline.cmdbuff != NULL)
2032 {
2033 /*
2034 * Put line in history buffer (":" and "=" only when it was typed).
2035 */
2036#ifdef FEAT_CMDHIST
2037 if (ccline.cmdlen && firstc != NUL
2038 && (some_key_typed || histype == HIST_SEARCH))
2039 {
2040 add_to_history(histype, ccline.cmdbuff, TRUE,
2041 histype == HIST_SEARCH ? firstc : NUL);
2042 if (firstc == ':')
2043 {
2044 vim_free(new_last_cmdline);
2045 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2046 }
2047 }
2048#endif
2049
2050 if (gotesc) /* abandon command line */
2051 {
2052 vim_free(ccline.cmdbuff);
2053 ccline.cmdbuff = NULL;
2054 if (msg_scrolled == 0)
2055 compute_cmdrow();
2056 MSG("");
2057 redraw_cmdline = TRUE;
2058 }
2059 }
2060
2061 /*
2062 * If the screen was shifted up, redraw the whole screen (later).
2063 * If the line is too long, clear it, so ruler and shown command do
2064 * not get printed in the middle of it.
2065 */
2066 msg_check();
2067 msg_scroll = save_msg_scroll;
2068 redir_off = FALSE;
2069
2070 /* When the command line was typed, no need for a wait-return prompt. */
2071 if (some_key_typed)
2072 need_wait_return = FALSE;
2073
2074 State = save_State;
2075#ifdef USE_IM_CONTROL
2076 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2077 im_save_status(b_im_ptr);
2078 im_set_active(FALSE);
2079#endif
2080#ifdef FEAT_MOUSE
2081 setmouse();
2082#endif
2083#ifdef CURSOR_SHAPE
2084 ui_cursor_shape(); /* may show different cursor shape */
2085#endif
2086
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002087 {
2088 char_u *p = ccline.cmdbuff;
2089
2090 /* Make ccline empty, getcmdline() may try to use it. */
2091 ccline.cmdbuff = NULL;
2092 return p;
2093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094}
2095
2096#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2097/*
2098 * Get a command line with a prompt.
2099 * This is prepared to be called recursively from getcmdline() (e.g. by
2100 * f_input() when evaluating an expression from CTRL-R =).
2101 * Returns the command line in allocated memory, or NULL.
2102 */
2103 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002104getcmdline_prompt(
2105 int firstc,
2106 char_u *prompt, /* command line prompt */
2107 int attr, /* attributes for prompt */
2108 int xp_context, /* type of expansion */
2109 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 char_u *s;
2112 struct cmdline_info save_ccline;
2113 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002114 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002116 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 ccline.cmdprompt = prompt;
2118 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002119# ifdef FEAT_EVAL
2120 ccline.xp_context = xp_context;
2121 ccline.xp_arg = xp_arg;
2122 ccline.input_fn = (firstc == '@');
2123# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002124 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002126 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002127 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002128 /* Restore msg_col, the prompt from input() may have changed it.
2129 * But only if called recursively and the commandline is therefore being
2130 * restored to an old one; if not, the input() prompt stays on the screen,
2131 * so we need its modified msg_col left intact. */
2132 if (ccline.cmdbuff != NULL)
2133 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
2135 return s;
2136}
2137#endif
2138
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002139/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002140 * Return TRUE when the text must not be changed and we can't switch to
2141 * another window or buffer. Used when editing the command line, evaluating
2142 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002143 */
2144 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002145text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002146{
2147#ifdef FEAT_CMDWIN
2148 if (cmdwin_type != 0)
2149 return TRUE;
2150#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002151 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002152}
2153
2154/*
2155 * Give an error message for a command that isn't allowed while the cmdline
2156 * window is open or editing the cmdline in another way.
2157 */
2158 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002159text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002160{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002161 EMSG(_(get_text_locked_msg()));
2162}
2163
2164 char_u *
2165get_text_locked_msg(void)
2166{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002167#ifdef FEAT_CMDWIN
2168 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002169 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002170#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002171 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002172}
2173
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002174#if defined(FEAT_AUTOCMD) || defined(PROTO)
2175/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002176 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2177 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002178 */
2179 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002180curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002181{
2182 if (curbuf_lock > 0)
2183 {
2184 EMSG(_("E788: Not allowed to edit another buffer now"));
2185 return TRUE;
2186 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002187 return allbuf_locked();
2188}
2189
2190/*
2191 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2192 * message.
2193 */
2194 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002195allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002196{
2197 if (allbuf_lock > 0)
2198 {
2199 EMSG(_("E811: Not allowed to change buffer information now"));
2200 return TRUE;
2201 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002202 return FALSE;
2203}
2204#endif
2205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002207cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208{
2209#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2210 if (cmdline_star > 0) /* showing '*', always 1 position */
2211 return 1;
2212#endif
2213 return ptr2cells(ccline.cmdbuff + idx);
2214}
2215
2216/*
2217 * Compute the offset of the cursor on the command line for the prompt and
2218 * indent.
2219 */
2220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002221set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002223 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 ccline.cmdspos = 1 + ccline.cmdindent;
2225 else
2226 ccline.cmdspos = 0 + ccline.cmdindent;
2227}
2228
2229/*
2230 * Compute the screen position for the cursor on the command line.
2231 */
2232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002233set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235 int i, m, c;
2236
2237 set_cmdspos();
2238 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002239 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002241 if (m < 0) /* overflow, Columns or Rows at weird value */
2242 m = MAXCOL;
2243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 else
2245 m = MAXCOL;
2246 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2247 {
2248 c = cmdline_charsize(i);
2249#ifdef FEAT_MBYTE
2250 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2251 if (has_mbyte)
2252 correct_cmdspos(i, c);
2253#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002254 /* If the cmdline doesn't fit, show cursor on last visible char.
2255 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if ((ccline.cmdspos += c) >= m)
2257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 ccline.cmdspos -= c;
2259 break;
2260 }
2261#ifdef FEAT_MBYTE
2262 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002263 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#endif
2265 }
2266}
2267
2268#ifdef FEAT_MBYTE
2269/*
2270 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2271 * character that doesn't fit, so that a ">" must be displayed.
2272 */
2273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002274correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002276 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2278 && ccline.cmdspos % Columns + cells > Columns)
2279 ccline.cmdspos++;
2280}
2281#endif
2282
2283/*
2284 * Get an Ex command line for the ":" command.
2285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002287getexline(
2288 int c, /* normally ':', NUL for ":append" */
2289 void *cookie UNUSED,
2290 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 /* When executing a register, remove ':' that's in front of each line. */
2293 if (exec_from_reg && vpeekc() == ':')
2294 (void)vgetc();
2295 return getcmdline(c, 1L, indent);
2296}
2297
2298/*
2299 * Get an Ex command line for Ex mode.
2300 * In Ex mode we only use the OS supplied line editing features and no
2301 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002302 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002305getexmodeline(
2306 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002307 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002308 void *cookie UNUSED,
2309 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002311 garray_T line_ga;
2312 char_u *pend;
2313 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002314 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002315 int escaped = FALSE; /* CTRL-V typed */
2316 int vcol = 0;
2317 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002318 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002319 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321 /* Switch cursor on now. This avoids that it happens after the "\n", which
2322 * confuses the system function that computes tabstops. */
2323 cursor_on();
2324
2325 /* always start in column 0; write a newline if necessary */
2326 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002327 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002329 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002331 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002332 if (p_prompt)
2333 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 while (indent-- > 0)
2335 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338
2339 ga_init2(&line_ga, 1, 30);
2340
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002341 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002343 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002344 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002345 while (indent >= 8)
2346 {
2347 ga_append(&line_ga, TAB);
2348 msg_puts((char_u *)" ");
2349 indent -= 8;
2350 }
2351 while (indent-- > 0)
2352 {
2353 ga_append(&line_ga, ' ');
2354 msg_putchar(' ');
2355 }
2356 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002357 ++no_mapping;
2358 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 /*
2361 * Get the line, one character at a time.
2362 */
2363 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002364 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002366 long sw;
2367 char_u *s;
2368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 if (ga_grow(&line_ga, 40) == FAIL)
2370 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
Bram Moolenaarba748c82017-02-26 14:00:07 +01002372 /*
2373 * Get one character at a time.
2374 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002375 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002376
2377 /* Check for a ":normal" command and no more characters left. */
2378 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2379 c1 = '\n';
2380 else
2381 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
2383 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002384 * Handle line editing.
2385 * Previously this was left to the system, putting the terminal in
2386 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002388 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002390 msg_putchar('\n');
2391 break;
2392 }
2393
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002394 if (c1 == K_PS)
2395 {
2396 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2397 goto redraw;
2398 }
2399
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002400 if (!escaped)
2401 {
2402 /* CR typed means "enter", which is NL */
2403 if (c1 == '\r')
2404 c1 = '\n';
2405
2406 if (c1 == BS || c1 == K_BS
2407 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002409 if (line_ga.ga_len > 0)
2410 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002411#ifdef FEAT_MBYTE
2412 if (has_mbyte)
2413 {
2414 p = (char_u *)line_ga.ga_data;
2415 p[line_ga.ga_len] = NUL;
2416 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2417 line_ga.ga_len -= len;
2418 }
2419 else
2420#endif
2421 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002422 goto redraw;
2423 }
2424 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002427 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002429 msg_col = startcol;
2430 msg_clr_eos();
2431 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002432 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002433 }
2434
2435 if (c1 == Ctrl_T)
2436 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002437 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002438 p = (char_u *)line_ga.ga_data;
2439 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002440 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002441 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002442add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002443 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002445 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002446 p = (char_u *)line_ga.ga_data;
2447 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002448 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2449 *s = ' ';
2450 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002452redraw:
2453 /* redraw the line */
2454 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002455 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002456 p = (char_u *)line_ga.ga_data;
2457 p[line_ga.ga_len] = NUL;
2458 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002460 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002462 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002464 msg_putchar(' ');
2465 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002466 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002468 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002470 len = MB_PTR2LEN(p);
2471 msg_outtrans_len(p, len);
2472 vcol += ptr2cells(p);
2473 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
2475 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002476 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002477 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002478 continue;
2479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002481 if (c1 == Ctrl_D)
2482 {
2483 /* Delete one shiftwidth. */
2484 p = (char_u *)line_ga.ga_data;
2485 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002487 if (prev_char == '^')
2488 ex_keep_indent = TRUE;
2489 indent = 0;
2490 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492 else
2493 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002494 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002495 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002496 if (indent > 0)
2497 {
2498 --indent;
2499 indent -= indent % get_sw_value(curbuf);
2500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002502 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002503 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002504 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002505 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2506 --line_ga.ga_len;
2507 }
2508 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002510
2511 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2512 {
2513 escaped = TRUE;
2514 continue;
2515 }
2516
2517 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2518 if (IS_SPECIAL(c1))
2519 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002521
2522 if (IS_SPECIAL(c1))
2523 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002524#ifdef FEAT_MBYTE
2525 if (has_mbyte)
2526 len = (*mb_char2bytes)(c1,
2527 (char_u *)line_ga.ga_data + line_ga.ga_len);
2528 else
2529#endif
2530 {
2531 len = 1;
2532 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2533 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002534 if (c1 == '\n')
2535 msg_putchar('\n');
2536 else if (c1 == TAB)
2537 {
2538 /* Don't use chartabsize(), 'ts' can be different */
2539 do
2540 {
2541 msg_putchar(' ');
2542 } while (++vcol % 8);
2543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002546 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002547 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002548 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002550 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002551 escaped = FALSE;
2552
2553 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002554 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002555
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002556 /* We are done when a NL is entered, but not when it comes after an
2557 * odd number of backslashes, that results in a NUL. */
2558 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002560 int bcount = 0;
2561
2562 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2563 ++bcount;
2564
2565 if (bcount > 0)
2566 {
2567 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2568 * "\NL", etc. */
2569 line_ga.ga_len -= (bcount + 1) / 2;
2570 pend -= (bcount + 1) / 2;
2571 pend[-1] = '\n';
2572 }
2573
2574 if ((bcount & 1) == 0)
2575 {
2576 --line_ga.ga_len;
2577 --pend;
2578 *pend = NUL;
2579 break;
2580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582 }
2583
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002584 --no_mapping;
2585 --allow_keys;
2586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 /* make following messages go to the next line */
2588 msg_didout = FALSE;
2589 msg_col = 0;
2590 if (msg_row < Rows - 1)
2591 ++msg_row;
2592 emsg_on_display = FALSE; /* don't want ui_delay() */
2593
2594 if (got_int)
2595 ga_clear(&line_ga);
2596
2597 return (char_u *)line_ga.ga_data;
2598}
2599
Bram Moolenaare344bea2005-09-01 20:46:49 +00002600# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2601 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602/*
2603 * Return TRUE if ccline.overstrike is on.
2604 */
2605 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002606cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 return ccline.overstrike;
2609}
2610
2611/*
2612 * Return TRUE if the cursor is at the end of the cmdline.
2613 */
2614 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002615cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617 return (ccline.cmdpos >= ccline.cmdlen);
2618}
2619#endif
2620
Bram Moolenaar9372a112005-12-06 19:59:18 +00002621#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622/*
2623 * Return the virtual column number at the current cursor position.
2624 * This is used by the IM code to obtain the start of the preedit string.
2625 */
2626 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002627cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628{
2629 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2630 return MAXCOL;
2631
2632# ifdef FEAT_MBYTE
2633 if (has_mbyte)
2634 {
2635 colnr_T col;
2636 int i = 0;
2637
2638 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002639 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
2641 return col;
2642 }
2643 else
2644# endif
2645 return ccline.cmdpos;
2646}
2647#endif
2648
2649#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2650/*
2651 * If part of the command line is an IM preedit string, redraw it with
2652 * IM feedback attributes. The cursor position is restored after drawing.
2653 */
2654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002655redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 if ((State & CMDLINE)
2658 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002659 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 && !p_imdisable
2661 && im_is_preediting())
2662 {
2663 int cmdpos = 0;
2664 int cmdspos;
2665 int old_row;
2666 int old_col;
2667 colnr_T col;
2668
2669 old_row = msg_row;
2670 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002671 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672
2673# ifdef FEAT_MBYTE
2674 if (has_mbyte)
2675 {
2676 for (col = 0; col < preedit_start_col
2677 && cmdpos < ccline.cmdlen; ++col)
2678 {
2679 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002680 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682 }
2683 else
2684# endif
2685 {
2686 cmdspos += preedit_start_col;
2687 cmdpos += preedit_start_col;
2688 }
2689
2690 msg_row = cmdline_row + (cmdspos / (int)Columns);
2691 msg_col = cmdspos % (int)Columns;
2692 if (msg_row >= Rows)
2693 msg_row = Rows - 1;
2694
2695 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2696 {
2697 int char_len;
2698 int char_attr;
2699
2700 char_attr = im_get_feedback_attr(col);
2701 if (char_attr < 0)
2702 break; /* end of preedit string */
2703
2704# ifdef FEAT_MBYTE
2705 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002706 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 else
2708# endif
2709 char_len = 1;
2710
2711 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2712 cmdpos += char_len;
2713 }
2714
2715 msg_row = old_row;
2716 msg_col = old_col;
2717 }
2718}
2719#endif /* FEAT_XIM && FEAT_GUI_GTK */
2720
2721/*
2722 * Allocate a new command line buffer.
2723 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2724 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2725 */
2726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002727alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 /*
2730 * give some extra space to avoid having to allocate all the time
2731 */
2732 if (len < 80)
2733 len = 100;
2734 else
2735 len += 20;
2736
2737 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2738 ccline.cmdbufflen = len;
2739}
2740
2741/*
2742 * Re-allocate the command line to length len + something extra.
2743 * return FAIL for failure, OK otherwise
2744 */
2745 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002746realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 char_u *p;
2749
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002750 if (len < ccline.cmdbufflen)
2751 return OK; /* no need to resize */
2752
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 p = ccline.cmdbuff;
2754 alloc_cmdbuff(len); /* will get some more */
2755 if (ccline.cmdbuff == NULL) /* out of memory */
2756 {
2757 ccline.cmdbuff = p; /* keep the old one */
2758 return FAIL;
2759 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002760 /* There isn't always a NUL after the command, but it may need to be
2761 * there, thus copy up to the NUL and add a NUL. */
2762 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2763 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002765
2766 if (ccline.xpc != NULL
2767 && ccline.xpc->xp_pattern != NULL
2768 && ccline.xpc->xp_context != EXPAND_NOTHING
2769 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2770 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002771 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002772
2773 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2774 * to point into the newly allocated memory. */
2775 if (i >= 0 && i <= ccline.cmdlen)
2776 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2777 }
2778
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 return OK;
2780}
2781
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002782#if defined(FEAT_ARABIC) || defined(PROTO)
2783static char_u *arshape_buf = NULL;
2784
2785# if defined(EXITFREE) || defined(PROTO)
2786 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002787free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002788{
2789 vim_free(arshape_buf);
2790}
2791# endif
2792#endif
2793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794/*
2795 * Draw part of the cmdline at the current cursor position. But draw stars
2796 * when cmdline_star is TRUE.
2797 */
2798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002799draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800{
2801#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2802 int i;
2803
2804 if (cmdline_star > 0)
2805 for (i = 0; i < len; ++i)
2806 {
2807 msg_putchar('*');
2808# ifdef FEAT_MBYTE
2809 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002810 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811# endif
2812 }
2813 else
2814#endif
2815#ifdef FEAT_ARABIC
2816 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 static int buflen = 0;
2819 char_u *p;
2820 int j;
2821 int newlen = 0;
2822 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002823 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 int prev_c = 0;
2825 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002826 int u8c;
2827 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
2830 /*
2831 * Do arabic shaping into a temporary buffer. This is very
2832 * inefficient!
2833 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002834 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
2836 /* Re-allocate the buffer. We keep it around to avoid a lot of
2837 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002838 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002839 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002840 arshape_buf = alloc(buflen);
2841 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 return; /* out of memory */
2843 }
2844
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002845 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2846 {
2847 /* Prepend a space to draw the leading composing char on. */
2848 arshape_buf[0] = ' ';
2849 newlen = 1;
2850 }
2851
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 for (j = start; j < start + len; j += mb_l)
2853 {
2854 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002855 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002856 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 if (ARABIC_CHAR(u8c))
2858 {
2859 /* Do Arabic shaping. */
2860 if (cmdmsg_rl)
2861 {
2862 /* displaying from right to left */
2863 pc = prev_c;
2864 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002865 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (j + mb_l >= start + len)
2867 nc = NUL;
2868 else
2869 nc = utf_ptr2char(p + mb_l);
2870 }
2871 else
2872 {
2873 /* displaying from left to right */
2874 if (j + mb_l >= start + len)
2875 pc = NUL;
2876 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002877 {
2878 int pcc[MAX_MCO];
2879
2880 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002882 pc1 = pcc[0];
2883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 nc = prev_c;
2885 }
2886 prev_c = u8c;
2887
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002888 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002890 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002891 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002893 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2894 if (u8cc[1] != 0)
2895 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002896 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 }
2898 }
2899 else
2900 {
2901 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002902 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 newlen += mb_l;
2904 }
2905 }
2906
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002907 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 }
2909 else
2910#endif
2911 msg_outtrans_len(ccline.cmdbuff + start, len);
2912}
2913
2914/*
2915 * Put a character on the command line. Shifts the following text to the
2916 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2917 * "c" must be printable (fit in one display cell)!
2918 */
2919 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002920putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
2922 if (cmd_silent)
2923 return;
2924 msg_no_more = TRUE;
2925 msg_putchar(c);
2926 if (shift)
2927 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2928 msg_no_more = FALSE;
2929 cursorcmd();
2930}
2931
2932/*
2933 * Undo a putcmdline(c, FALSE).
2934 */
2935 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002936unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937{
2938 if (cmd_silent)
2939 return;
2940 msg_no_more = TRUE;
2941 if (ccline.cmdlen == ccline.cmdpos)
2942 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002943#ifdef FEAT_MBYTE
2944 else if (has_mbyte)
2945 draw_cmdline(ccline.cmdpos,
2946 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 else
2949 draw_cmdline(ccline.cmdpos, 1);
2950 msg_no_more = FALSE;
2951 cursorcmd();
2952}
2953
2954/*
2955 * Put the given string, of the given length, onto the command line.
2956 * If len is -1, then STRLEN() is used to calculate the length.
2957 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2958 * part will be redrawn, otherwise it will not. If this function is called
2959 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2960 * called afterwards.
2961 */
2962 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002963put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
2965 int retval;
2966 int i;
2967 int m;
2968 int c;
2969
2970 if (len < 0)
2971 len = (int)STRLEN(str);
2972
2973 /* Check if ccline.cmdbuff needs to be longer */
2974 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002975 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 else
2977 retval = OK;
2978 if (retval == OK)
2979 {
2980 if (!ccline.overstrike)
2981 {
2982 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2983 ccline.cmdbuff + ccline.cmdpos,
2984 (size_t)(ccline.cmdlen - ccline.cmdpos));
2985 ccline.cmdlen += len;
2986 }
2987 else
2988 {
2989#ifdef FEAT_MBYTE
2990 if (has_mbyte)
2991 {
2992 /* Count nr of characters in the new string. */
2993 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002994 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 ++m;
2996 /* Count nr of bytes in cmdline that are overwritten by these
2997 * characters. */
2998 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002999 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 --m;
3001 if (i < ccline.cmdlen)
3002 {
3003 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3004 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3005 ccline.cmdlen += ccline.cmdpos + len - i;
3006 }
3007 else
3008 ccline.cmdlen = ccline.cmdpos + len;
3009 }
3010 else
3011#endif
3012 if (ccline.cmdpos + len > ccline.cmdlen)
3013 ccline.cmdlen = ccline.cmdpos + len;
3014 }
3015 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3016 ccline.cmdbuff[ccline.cmdlen] = NUL;
3017
3018#ifdef FEAT_MBYTE
3019 if (enc_utf8)
3020 {
3021 /* When the inserted text starts with a composing character,
3022 * backup to the character before it. There could be two of them.
3023 */
3024 i = 0;
3025 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3026 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3027 {
3028 i = (*mb_head_off)(ccline.cmdbuff,
3029 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3030 ccline.cmdpos -= i;
3031 len += i;
3032 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3033 }
3034# ifdef FEAT_ARABIC
3035 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3036 {
3037 /* Check the previous character for Arabic combining pair. */
3038 i = (*mb_head_off)(ccline.cmdbuff,
3039 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3040 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3041 + ccline.cmdpos - i), c))
3042 {
3043 ccline.cmdpos -= i;
3044 len += i;
3045 }
3046 else
3047 i = 0;
3048 }
3049# endif
3050 if (i != 0)
3051 {
3052 /* Also backup the cursor position. */
3053 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3054 ccline.cmdspos -= i;
3055 msg_col -= i;
3056 if (msg_col < 0)
3057 {
3058 msg_col += Columns;
3059 --msg_row;
3060 }
3061 }
3062 }
3063#endif
3064
3065 if (redraw && !cmd_silent)
3066 {
3067 msg_no_more = TRUE;
3068 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003069 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3071 /* Avoid clearing the rest of the line too often. */
3072 if (cmdline_row != i || ccline.overstrike)
3073 msg_clr_eos();
3074 msg_no_more = FALSE;
3075 }
3076#ifdef FEAT_FKMAP
3077 /*
3078 * If we are in Farsi command mode, the character input must be in
3079 * Insert mode. So do not advance the cmdpos.
3080 */
3081 if (!cmd_fkmap)
3082#endif
3083 {
3084 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003087 if (m < 0) /* overflow, Columns or Rows at weird value */
3088 m = MAXCOL;
3089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 else
3091 m = MAXCOL;
3092 for (i = 0; i < len; ++i)
3093 {
3094 c = cmdline_charsize(ccline.cmdpos);
3095#ifdef FEAT_MBYTE
3096 /* count ">" for a double-wide char that doesn't fit. */
3097 if (has_mbyte)
3098 correct_cmdspos(ccline.cmdpos, c);
3099#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003100 /* Stop cursor at the end of the screen, but do increment the
3101 * insert position, so that entering a very long command
3102 * works, even though you can't see it. */
3103 if (ccline.cmdspos + c < m)
3104 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105#ifdef FEAT_MBYTE
3106 if (has_mbyte)
3107 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003108 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 if (c > len - i - 1)
3110 c = len - i - 1;
3111 ccline.cmdpos += c;
3112 i += c;
3113 }
3114#endif
3115 ++ccline.cmdpos;
3116 }
3117 }
3118 }
3119 if (redraw)
3120 msg_check();
3121 return retval;
3122}
3123
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003124static struct cmdline_info prev_ccline;
3125static int prev_ccline_used = FALSE;
3126
3127/*
3128 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3129 * and overwrite it. But get_cmdline_str() may need it, thus make it
3130 * available globally in prev_ccline.
3131 */
3132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003133save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003134{
3135 if (!prev_ccline_used)
3136 {
3137 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3138 prev_ccline_used = TRUE;
3139 }
3140 *ccp = prev_ccline;
3141 prev_ccline = ccline;
3142 ccline.cmdbuff = NULL;
3143 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003144 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003145}
3146
3147/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003148 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003149 */
3150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003151restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003152{
3153 ccline = prev_ccline;
3154 prev_ccline = *ccp;
3155}
3156
Bram Moolenaar5a305422006-04-28 22:38:25 +00003157#if defined(FEAT_EVAL) || defined(PROTO)
3158/*
3159 * Save the command line into allocated memory. Returns a pointer to be
3160 * passed to restore_cmdline_alloc() later.
3161 * Returns NULL when failed.
3162 */
3163 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003164save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003165{
3166 struct cmdline_info *p;
3167
3168 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3169 if (p != NULL)
3170 save_cmdline(p);
3171 return (char_u *)p;
3172}
3173
3174/*
3175 * Restore the command line from the return value of save_cmdline_alloc().
3176 */
3177 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003178restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003179{
3180 if (p != NULL)
3181 {
3182 restore_cmdline((struct cmdline_info *)p);
3183 vim_free(p);
3184 }
3185}
3186#endif
3187
Bram Moolenaar8299df92004-07-10 09:47:34 +00003188/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003189 * Paste a yank register into the command line.
3190 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003191 * insert_reg() can't be used here, because special characters from the
3192 * register contents will be interpreted as commands.
3193 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003194 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003195 */
3196 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003197cmdline_paste(
3198 int regname,
3199 int literally, /* Insert text literally instead of "as typed" */
3200 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003201{
3202 long i;
3203 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003204 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003205 int allocated;
3206 struct cmdline_info save_ccline;
3207
3208 /* check for valid regname; also accept special characters for CTRL-R in
3209 * the command line */
3210 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3211 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3212 return FAIL;
3213
3214 /* A register containing CTRL-R can cause an endless loop. Allow using
3215 * CTRL-C to break the loop. */
3216 line_breakcheck();
3217 if (got_int)
3218 return FAIL;
3219
3220#ifdef FEAT_CLIPBOARD
3221 regname = may_get_selection(regname);
3222#endif
3223
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003224 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003225 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003226 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003227 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003228 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003229 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003230 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003231
3232 if (i)
3233 {
3234 /* Got the value of a special register in "arg". */
3235 if (arg == NULL)
3236 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003237
3238 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3239 * part of the word. */
3240 p = arg;
3241 if (p_is && regname == Ctrl_W)
3242 {
3243 char_u *w;
3244 int len;
3245
3246 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003247 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003248 {
3249#ifdef FEAT_MBYTE
3250 if (has_mbyte)
3251 {
3252 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3253 if (!vim_iswordc(mb_ptr2char(w - len)))
3254 break;
3255 w -= len;
3256 }
3257 else
3258#endif
3259 {
3260 if (!vim_iswordc(w[-1]))
3261 break;
3262 --w;
3263 }
3264 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003265 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003266 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3267 p += len;
3268 }
3269
3270 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003271 if (allocated)
3272 vim_free(arg);
3273 return OK;
3274 }
3275
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003276 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003277}
3278
3279/*
3280 * Put a string on the command line.
3281 * When "literally" is TRUE, insert literally.
3282 * When "literally" is FALSE, insert as typed, but don't leave the command
3283 * line.
3284 */
3285 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003286cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003287{
3288 int c, cv;
3289
3290 if (literally)
3291 put_on_cmdline(s, -1, TRUE);
3292 else
3293 while (*s != NUL)
3294 {
3295 cv = *s;
3296 if (cv == Ctrl_V && s[1])
3297 ++s;
3298#ifdef FEAT_MBYTE
3299 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003300 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003301 else
3302#endif
3303 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003304 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3305 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003306#ifdef UNIX
3307 || c == intr_char
3308#endif
3309 || (c == Ctrl_BSL && *s == Ctrl_N))
3310 stuffcharReadbuff(Ctrl_V);
3311 stuffcharReadbuff(c);
3312 }
3313}
3314
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315#ifdef FEAT_WILDMENU
3316/*
3317 * Delete characters on the command line, from "from" to the current
3318 * position.
3319 */
3320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003321cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322{
3323 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3324 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3325 ccline.cmdlen -= ccline.cmdpos - from;
3326 ccline.cmdpos = from;
3327}
3328#endif
3329
3330/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003331 * This function is called when the screen size changes and with incremental
3332 * search and in other situations where the command line may have been
3333 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 */
3335 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003336redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338 if (cmd_silent)
3339 return;
3340 need_wait_return = FALSE;
3341 compute_cmdrow();
3342 redrawcmd();
3343 cursorcmd();
3344}
3345
3346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003347redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 int i;
3350
3351 if (cmd_silent)
3352 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003353 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 msg_putchar(ccline.cmdfirstc);
3355 if (ccline.cmdprompt != NULL)
3356 {
3357 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3358 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3359 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003360 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 --ccline.cmdindent;
3362 }
3363 else
3364 for (i = ccline.cmdindent; i > 0; --i)
3365 msg_putchar(' ');
3366}
3367
3368/*
3369 * Redraw what is currently on the command line.
3370 */
3371 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003372redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 if (cmd_silent)
3375 return;
3376
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003377 /* when 'incsearch' is set there may be no command line while redrawing */
3378 if (ccline.cmdbuff == NULL)
3379 {
3380 windgoto(cmdline_row, 0);
3381 msg_clr_eos();
3382 return;
3383 }
3384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 msg_start();
3386 redrawcmdprompt();
3387
3388 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3389 msg_no_more = TRUE;
3390 draw_cmdline(0, ccline.cmdlen);
3391 msg_clr_eos();
3392 msg_no_more = FALSE;
3393
3394 set_cmdspos_cursor();
3395
3396 /*
3397 * An emsg() before may have set msg_scroll. This is used in normal mode,
3398 * in cmdline mode we can reset them now.
3399 */
3400 msg_scroll = FALSE; /* next message overwrites cmdline */
3401
3402 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3403 * in cmdline mode */
3404 skip_redraw = FALSE;
3405}
3406
3407 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003408compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003410 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 cmdline_row = Rows - 1;
3412 else
3413 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3414 + W_STATUS_HEIGHT(lastwin);
3415}
3416
3417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003418cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419{
3420 if (cmd_silent)
3421 return;
3422
3423#ifdef FEAT_RIGHTLEFT
3424 if (cmdmsg_rl)
3425 {
3426 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3427 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3428 if (msg_row <= 0)
3429 msg_row = Rows - 1;
3430 }
3431 else
3432#endif
3433 {
3434 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3435 msg_col = ccline.cmdspos % (int)Columns;
3436 if (msg_row >= Rows)
3437 msg_row = Rows - 1;
3438 }
3439
3440 windgoto(msg_row, msg_col);
3441#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3442 redrawcmd_preedit();
3443#endif
3444#ifdef MCH_CURSOR_SHAPE
3445 mch_update_cursor();
3446#endif
3447}
3448
3449 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003450gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451{
3452 msg_start();
3453#ifdef FEAT_RIGHTLEFT
3454 if (cmdmsg_rl)
3455 msg_col = Columns - 1;
3456 else
3457#endif
3458 msg_col = 0; /* always start in column 0 */
3459 if (clr) /* clear the bottom line(s) */
3460 msg_clr_eos(); /* will reset clear_cmdline */
3461 windgoto(cmdline_row, 0);
3462}
3463
3464/*
3465 * Check the word in front of the cursor for an abbreviation.
3466 * Called when the non-id character "c" has been entered.
3467 * When an abbreviation is recognized it is removed from the text with
3468 * backspaces and the replacement string is inserted, followed by "c".
3469 */
3470 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003471ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3474 return FALSE;
3475
3476 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3477}
3478
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003479#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3480 static int
3481#ifdef __BORLANDC__
3482_RTLENTRYF
3483#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003484sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003485{
3486 char_u *p1 = *(char_u **)s1;
3487 char_u *p2 = *(char_u **)s2;
3488
3489 if (*p1 != '<' && *p2 == '<') return -1;
3490 if (*p1 == '<' && *p2 != '<') return 1;
3491 return STRCMP(p1, p2);
3492}
3493#endif
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495/*
3496 * Return FAIL if this is not an appropriate context in which to do
3497 * completion of anything, return OK if it is (even if there are no matches).
3498 * For the caller, this means that the character is just passed through like a
3499 * normal character (instead of being expanded). This allows :s/^I^D etc.
3500 */
3501 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003502nextwild(
3503 expand_T *xp,
3504 int type,
3505 int options, /* extra options for ExpandOne() */
3506 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508 int i, j;
3509 char_u *p1;
3510 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 int difflen;
3512 int v;
3513
3514 if (xp->xp_numfiles == -1)
3515 {
3516 set_expand_context(xp);
3517 cmd_showtail = expand_showtail(xp);
3518 }
3519
3520 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3521 {
3522 beep_flush();
3523 return OK; /* Something illegal on command line */
3524 }
3525 if (xp->xp_context == EXPAND_NOTHING)
3526 {
3527 /* Caller can use the character as a normal char instead */
3528 return FAIL;
3529 }
3530
3531 MSG_PUTS("..."); /* show that we are busy */
3532 out_flush();
3533
3534 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003535 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 if (type == WILD_NEXT || type == WILD_PREV)
3538 {
3539 /*
3540 * Get next/previous match for a previous expanded pattern.
3541 */
3542 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3543 }
3544 else
3545 {
3546 /*
3547 * Translate string into pattern and expand it.
3548 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003549 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3550 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 p2 = NULL;
3552 else
3553 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003554 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003555 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3556 if (escape)
3557 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003558
3559 if (p_wic)
3560 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003561 p2 = ExpandOne(xp, p1,
3562 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003563 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003565 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (p2 != NULL && type == WILD_LONGEST)
3567 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003568 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (ccline.cmdbuff[i + j] == '*'
3570 || ccline.cmdbuff[i + j] == '?')
3571 break;
3572 if ((int)STRLEN(p2) < j)
3573 {
3574 vim_free(p2);
3575 p2 = NULL;
3576 }
3577 }
3578 }
3579 }
3580
3581 if (p2 != NULL && !got_int)
3582 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003583 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003584 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003586 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 xp->xp_pattern = ccline.cmdbuff + i;
3588 }
3589 else
3590 v = OK;
3591 if (v == OK)
3592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003593 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3594 &ccline.cmdbuff[ccline.cmdpos],
3595 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3596 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 ccline.cmdlen += difflen;
3598 ccline.cmdpos += difflen;
3599 }
3600 }
3601 vim_free(p2);
3602
3603 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003604 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
3606 /* When expanding a ":map" command and no matches are found, assume that
3607 * the key is supposed to be inserted literally */
3608 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3609 return FAIL;
3610
3611 if (xp->xp_numfiles <= 0 && p2 == NULL)
3612 beep_flush();
3613 else if (xp->xp_numfiles == 1)
3614 /* free expanded pattern */
3615 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3616
3617 return OK;
3618}
3619
3620/*
3621 * Do wildcard expansion on the string 'str'.
3622 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003623 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 * Return NULL for failure.
3625 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003626 * "orig" is the originally expanded string, copied to allocated memory. It
3627 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3628 * WILD_PREV "orig" should be NULL.
3629 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003630 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3631 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 *
3633 * mode = WILD_FREE: just free previously expanded matches
3634 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3635 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3636 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3637 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3638 * mode = WILD_ALL: return all matches concatenated
3639 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003640 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 *
3642 * options = WILD_LIST_NOTFOUND: list entries without a match
3643 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3644 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3645 * options = WILD_NO_BEEP: Don't beep for multiple matches
3646 * options = WILD_ADD_SLASH: add a slash after directory names
3647 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3648 * options = WILD_SILENT: don't print warning messages
3649 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003650 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 *
3652 * The variables xp->xp_context and xp->xp_backslash must have been set!
3653 */
3654 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003655ExpandOne(
3656 expand_T *xp,
3657 char_u *str,
3658 char_u *orig, /* allocated copy of original of expanded string */
3659 int options,
3660 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661{
3662 char_u *ss = NULL;
3663 static int findex;
3664 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003665 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 int i;
3667 long_u len;
3668 int non_suf_match; /* number without matching suffix */
3669
3670 /*
3671 * first handle the case of using an old match
3672 */
3673 if (mode == WILD_NEXT || mode == WILD_PREV)
3674 {
3675 if (xp->xp_numfiles > 0)
3676 {
3677 if (mode == WILD_PREV)
3678 {
3679 if (findex == -1)
3680 findex = xp->xp_numfiles;
3681 --findex;
3682 }
3683 else /* mode == WILD_NEXT */
3684 ++findex;
3685
3686 /*
3687 * When wrapping around, return the original string, set findex to
3688 * -1.
3689 */
3690 if (findex < 0)
3691 {
3692 if (orig_save == NULL)
3693 findex = xp->xp_numfiles - 1;
3694 else
3695 findex = -1;
3696 }
3697 if (findex >= xp->xp_numfiles)
3698 {
3699 if (orig_save == NULL)
3700 findex = 0;
3701 else
3702 findex = -1;
3703 }
3704#ifdef FEAT_WILDMENU
3705 if (p_wmnu)
3706 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3707 findex, cmd_showtail);
3708#endif
3709 if (findex == -1)
3710 return vim_strsave(orig_save);
3711 return vim_strsave(xp->xp_files[findex]);
3712 }
3713 else
3714 return NULL;
3715 }
3716
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003717 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3719 {
3720 FreeWild(xp->xp_numfiles, xp->xp_files);
3721 xp->xp_numfiles = -1;
3722 vim_free(orig_save);
3723 orig_save = NULL;
3724 }
3725 findex = 0;
3726
3727 if (mode == WILD_FREE) /* only release file name */
3728 return NULL;
3729
3730 if (xp->xp_numfiles == -1)
3731 {
3732 vim_free(orig_save);
3733 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003734 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
3736 /*
3737 * Do the expansion.
3738 */
3739 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3740 options) == FAIL)
3741 {
3742#ifdef FNAME_ILLEGAL
3743 /* Illegal file name has been silently skipped. But when there
3744 * are wildcards, the real problem is that there was no match,
3745 * causing the pattern to be added, which has illegal characters.
3746 */
3747 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3748 EMSG2(_(e_nomatch2), str);
3749#endif
3750 }
3751 else if (xp->xp_numfiles == 0)
3752 {
3753 if (!(options & WILD_SILENT))
3754 EMSG2(_(e_nomatch2), str);
3755 }
3756 else
3757 {
3758 /* Escape the matches for use on the command line. */
3759 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3760
3761 /*
3762 * Check for matching suffixes in file names.
3763 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003764 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3765 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 {
3767 if (xp->xp_numfiles)
3768 non_suf_match = xp->xp_numfiles;
3769 else
3770 non_suf_match = 1;
3771 if ((xp->xp_context == EXPAND_FILES
3772 || xp->xp_context == EXPAND_DIRECTORIES)
3773 && xp->xp_numfiles > 1)
3774 {
3775 /*
3776 * More than one match; check suffix.
3777 * The files will have been sorted on matching suffix in
3778 * expand_wildcards, only need to check the first two.
3779 */
3780 non_suf_match = 0;
3781 for (i = 0; i < 2; ++i)
3782 if (match_suffix(xp->xp_files[i]))
3783 ++non_suf_match;
3784 }
3785 if (non_suf_match != 1)
3786 {
3787 /* Can we ever get here unless it's while expanding
3788 * interactively? If not, we can get rid of this all
3789 * together. Don't really want to wait for this message
3790 * (and possibly have to hit return to continue!).
3791 */
3792 if (!(options & WILD_SILENT))
3793 EMSG(_(e_toomany));
3794 else if (!(options & WILD_NO_BEEP))
3795 beep_flush();
3796 }
3797 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3798 ss = vim_strsave(xp->xp_files[0]);
3799 }
3800 }
3801 }
3802
3803 /* Find longest common part */
3804 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3805 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003806 int mb_len = 1;
3807 int c0, ci;
3808
3809 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003811#ifdef FEAT_MBYTE
3812 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003814 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3815 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3816 }
3817 else
3818#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003819 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003820 for (i = 1; i < xp->xp_numfiles; ++i)
3821 {
3822#ifdef FEAT_MBYTE
3823 if (has_mbyte)
3824 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3825 else
3826#endif
3827 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003828 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003830 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003831 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003833 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 break;
3835 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003836 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 break;
3838 }
3839 if (i < xp->xp_numfiles)
3840 {
3841 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003842 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 break;
3844 }
3845 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003846
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 ss = alloc((unsigned)len + 1);
3848 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003849 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 findex = -1; /* next p_wc gets first one */
3851 }
3852
3853 /* Concatenate all matching names */
3854 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3855 {
3856 len = 0;
3857 for (i = 0; i < xp->xp_numfiles; ++i)
3858 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3859 ss = lalloc(len, TRUE);
3860 if (ss != NULL)
3861 {
3862 *ss = NUL;
3863 for (i = 0; i < xp->xp_numfiles; ++i)
3864 {
3865 STRCAT(ss, xp->xp_files[i]);
3866 if (i != xp->xp_numfiles - 1)
3867 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3868 }
3869 }
3870 }
3871
3872 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3873 ExpandCleanup(xp);
3874
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003875 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003876 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003877 vim_free(orig);
3878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 return ss;
3880}
3881
3882/*
3883 * Prepare an expand structure for use.
3884 */
3885 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003886ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003888 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003889 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003891#ifndef BACKSLASH_IN_FILENAME
3892 xp->xp_shell = FALSE;
3893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 xp->xp_numfiles = -1;
3895 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003896#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3897 xp->xp_arg = NULL;
3898#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003899 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900}
3901
3902/*
3903 * Cleanup an expand structure after use.
3904 */
3905 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003906ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907{
3908 if (xp->xp_numfiles >= 0)
3909 {
3910 FreeWild(xp->xp_numfiles, xp->xp_files);
3911 xp->xp_numfiles = -1;
3912 }
3913}
3914
3915 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003916ExpandEscape(
3917 expand_T *xp,
3918 char_u *str,
3919 int numfiles,
3920 char_u **files,
3921 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922{
3923 int i;
3924 char_u *p;
3925
3926 /*
3927 * May change home directory back to "~"
3928 */
3929 if (options & WILD_HOME_REPLACE)
3930 tilde_replace(str, numfiles, files);
3931
3932 if (options & WILD_ESCAPE)
3933 {
3934 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003935 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003936 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 || xp->xp_context == EXPAND_BUFFERS
3938 || xp->xp_context == EXPAND_DIRECTORIES)
3939 {
3940 /*
3941 * Insert a backslash into a file name before a space, \, %, #
3942 * and wildmatch characters, except '~'.
3943 */
3944 for (i = 0; i < numfiles; ++i)
3945 {
3946 /* for ":set path=" we need to escape spaces twice */
3947 if (xp->xp_backslash == XP_BS_THREE)
3948 {
3949 p = vim_strsave_escaped(files[i], (char_u *)" ");
3950 if (p != NULL)
3951 {
3952 vim_free(files[i]);
3953 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003954#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 p = vim_strsave_escaped(files[i], (char_u *)" ");
3956 if (p != NULL)
3957 {
3958 vim_free(files[i]);
3959 files[i] = p;
3960 }
3961#endif
3962 }
3963 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003964#ifdef BACKSLASH_IN_FILENAME
3965 p = vim_strsave_fnameescape(files[i], FALSE);
3966#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003967 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (p != NULL)
3970 {
3971 vim_free(files[i]);
3972 files[i] = p;
3973 }
3974
3975 /* If 'str' starts with "\~", replace "~" at start of
3976 * files[i] with "\~". */
3977 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003978 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 }
3980 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003981
3982 /* If the first file starts with a '+' escape it. Otherwise it
3983 * could be seen as "+cmd". */
3984 if (*files[0] == '+')
3985 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 }
3987 else if (xp->xp_context == EXPAND_TAGS)
3988 {
3989 /*
3990 * Insert a backslash before characters in a tag name that
3991 * would terminate the ":tag" command.
3992 */
3993 for (i = 0; i < numfiles; ++i)
3994 {
3995 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3996 if (p != NULL)
3997 {
3998 vim_free(files[i]);
3999 files[i] = p;
4000 }
4001 }
4002 }
4003 }
4004}
4005
4006/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004007 * Escape special characters in "fname" for when used as a file name argument
4008 * after a Vim command, or, when "shell" is non-zero, a shell command.
4009 * Returns the result in allocated memory.
4010 */
4011 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004012vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004013{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004014 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004015#ifdef BACKSLASH_IN_FILENAME
4016 char_u buf[20];
4017 int j = 0;
4018
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004019 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004020 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004021 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004022 buf[j++] = *p;
4023 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004024 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004025#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004026 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4027 if (shell && csh_like_shell() && p != NULL)
4028 {
4029 char_u *s;
4030
4031 /* For csh and similar shells need to put two backslashes before '!'.
4032 * One is taken by Vim, one by the shell. */
4033 s = vim_strsave_escaped(p, (char_u *)"!");
4034 vim_free(p);
4035 p = s;
4036 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004037#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004038
4039 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4040 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004041 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004042 escape_fname(&p);
4043
4044 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004045}
4046
4047/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004048 * Put a backslash before the file name in "pp", which is in allocated memory.
4049 */
4050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004051escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004052{
4053 char_u *p;
4054
4055 p = alloc((unsigned)(STRLEN(*pp) + 2));
4056 if (p != NULL)
4057 {
4058 p[0] = '\\';
4059 STRCPY(p + 1, *pp);
4060 vim_free(*pp);
4061 *pp = p;
4062 }
4063}
4064
4065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 * For each file name in files[num_files]:
4067 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4068 */
4069 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004070tilde_replace(
4071 char_u *orig_pat,
4072 int num_files,
4073 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074{
4075 int i;
4076 char_u *p;
4077
4078 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4079 {
4080 for (i = 0; i < num_files; ++i)
4081 {
4082 p = home_replace_save(NULL, files[i]);
4083 if (p != NULL)
4084 {
4085 vim_free(files[i]);
4086 files[i] = p;
4087 }
4088 }
4089 }
4090}
4091
4092/*
4093 * Show all matches for completion on the command line.
4094 * Returns EXPAND_NOTHING when the character that triggered expansion should
4095 * be inserted like a normal character.
4096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004098showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4101 int num_files;
4102 char_u **files_found;
4103 int i, j, k;
4104 int maxlen;
4105 int lines;
4106 int columns;
4107 char_u *p;
4108 int lastlen;
4109 int attr;
4110 int showtail;
4111
4112 if (xp->xp_numfiles == -1)
4113 {
4114 set_expand_context(xp);
4115 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4116 &num_files, &files_found);
4117 showtail = expand_showtail(xp);
4118 if (i != EXPAND_OK)
4119 return i;
4120
4121 }
4122 else
4123 {
4124 num_files = xp->xp_numfiles;
4125 files_found = xp->xp_files;
4126 showtail = cmd_showtail;
4127 }
4128
4129#ifdef FEAT_WILDMENU
4130 if (!wildmenu)
4131 {
4132#endif
4133 msg_didany = FALSE; /* lines_left will be set */
4134 msg_start(); /* prepare for paging */
4135 msg_putchar('\n');
4136 out_flush();
4137 cmdline_row = msg_row;
4138 msg_didany = FALSE; /* lines_left will be set again */
4139 msg_start(); /* prepare for paging */
4140#ifdef FEAT_WILDMENU
4141 }
4142#endif
4143
4144 if (got_int)
4145 got_int = FALSE; /* only int. the completion, not the cmd line */
4146#ifdef FEAT_WILDMENU
4147 else if (wildmenu)
4148 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
4149#endif
4150 else
4151 {
4152 /* find the length of the longest file name */
4153 maxlen = 0;
4154 for (i = 0; i < num_files; ++i)
4155 {
4156 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004157 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 || xp->xp_context == EXPAND_BUFFERS))
4159 {
4160 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4161 j = vim_strsize(NameBuff);
4162 }
4163 else
4164 j = vim_strsize(L_SHOWFILE(i));
4165 if (j > maxlen)
4166 maxlen = j;
4167 }
4168
4169 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4170 lines = num_files;
4171 else
4172 {
4173 /* compute the number of columns and lines for the listing */
4174 maxlen += 2; /* two spaces between file names */
4175 columns = ((int)Columns + 2) / maxlen;
4176 if (columns < 1)
4177 columns = 1;
4178 lines = (num_files + columns - 1) / columns;
4179 }
4180
4181 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4182
4183 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4184 {
4185 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4186 msg_clr_eos();
4187 msg_advance(maxlen - 3);
4188 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4189 }
4190
4191 /* list the files line by line */
4192 for (i = 0; i < lines; ++i)
4193 {
4194 lastlen = 999;
4195 for (k = i; k < num_files; k += lines)
4196 {
4197 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4198 {
4199 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4200 p = files_found[k] + STRLEN(files_found[k]) + 1;
4201 msg_advance(maxlen + 1);
4202 msg_puts(p);
4203 msg_advance(maxlen + 3);
4204 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4205 break;
4206 }
4207 for (j = maxlen - lastlen; --j >= 0; )
4208 msg_putchar(' ');
4209 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004210 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 || xp->xp_context == EXPAND_BUFFERS)
4212 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004213 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004214 if (xp->xp_numfiles != -1)
4215 {
4216 char_u *halved_slash;
4217 char_u *exp_path;
4218
4219 /* Expansion was done before and special characters
4220 * were escaped, need to halve backslashes. Also
4221 * $HOME has been replaced with ~/. */
4222 exp_path = expand_env_save_opt(files_found[k], TRUE);
4223 halved_slash = backslash_halve_save(
4224 exp_path != NULL ? exp_path : files_found[k]);
4225 j = mch_isdir(halved_slash != NULL ? halved_slash
4226 : files_found[k]);
4227 vim_free(exp_path);
4228 vim_free(halved_slash);
4229 }
4230 else
4231 /* Expansion was done here, file names are literal. */
4232 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (showtail)
4234 p = L_SHOWFILE(k);
4235 else
4236 {
4237 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4238 TRUE);
4239 p = NameBuff;
4240 }
4241 }
4242 else
4243 {
4244 j = FALSE;
4245 p = L_SHOWFILE(k);
4246 }
4247 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4248 }
4249 if (msg_col > 0) /* when not wrapped around */
4250 {
4251 msg_clr_eos();
4252 msg_putchar('\n');
4253 }
4254 out_flush(); /* show one line at a time */
4255 if (got_int)
4256 {
4257 got_int = FALSE;
4258 break;
4259 }
4260 }
4261
4262 /*
4263 * we redraw the command below the lines that we have just listed
4264 * This is a bit tricky, but it saves a lot of screen updating.
4265 */
4266 cmdline_row = msg_row; /* will put it back later */
4267 }
4268
4269 if (xp->xp_numfiles == -1)
4270 FreeWild(num_files, files_found);
4271
4272 return EXPAND_OK;
4273}
4274
4275/*
4276 * Private gettail for showmatches() (and win_redr_status_matches()):
4277 * Find tail of file name path, but ignore trailing "/".
4278 */
4279 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004280sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281{
4282 char_u *p;
4283 char_u *t = s;
4284 int had_sep = FALSE;
4285
4286 for (p = s; *p != NUL; )
4287 {
4288 if (vim_ispathsep(*p)
4289#ifdef BACKSLASH_IN_FILENAME
4290 && !rem_backslash(p)
4291#endif
4292 )
4293 had_sep = TRUE;
4294 else if (had_sep)
4295 {
4296 t = p;
4297 had_sep = FALSE;
4298 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004299 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 return t;
4302}
4303
4304/*
4305 * Return TRUE if we only need to show the tail of completion matches.
4306 * When not completing file names or there is a wildcard in the path FALSE is
4307 * returned.
4308 */
4309 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004310expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311{
4312 char_u *s;
4313 char_u *end;
4314
4315 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004316 if (xp->xp_context != EXPAND_FILES
4317 && xp->xp_context != EXPAND_SHELLCMD
4318 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return FALSE;
4320
4321 end = gettail(xp->xp_pattern);
4322 if (end == xp->xp_pattern) /* there is no path separator */
4323 return FALSE;
4324
4325 for (s = xp->xp_pattern; s < end; s++)
4326 {
4327 /* Skip escaped wildcards. Only when the backslash is not a path
4328 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4329 if (rem_backslash(s))
4330 ++s;
4331 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4332 return FALSE;
4333 }
4334 return TRUE;
4335}
4336
4337/*
4338 * Prepare a string for expansion.
4339 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004340 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 * When expanding other names: The string will be used with regcomp(). Copy
4342 * the name into allocated memory and prepend "^".
4343 */
4344 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004345addstar(
4346 char_u *fname,
4347 int len,
4348 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
4350 char_u *retval;
4351 int i, j;
4352 int new_len;
4353 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004354 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004356 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004357 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004358 && context != EXPAND_SHELLCMD
4359 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
4361 /*
4362 * Matching will be done internally (on something other than files).
4363 * So we convert the file-matching-type wildcards into our kind for
4364 * use with vim_regcomp(). First work out how long it will be:
4365 */
4366
4367 /* For help tags the translation is done in find_help_tags().
4368 * For a tag pattern starting with "/" no translation is needed. */
4369 if (context == EXPAND_HELP
4370 || context == EXPAND_COLORS
4371 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004372 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004373 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004374 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004375 || ((context == EXPAND_TAGS_LISTFILES
4376 || context == EXPAND_TAGS)
4377 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 retval = vim_strnsave(fname, len);
4379 else
4380 {
4381 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4382 for (i = 0; i < len; i++)
4383 {
4384 if (fname[i] == '*' || fname[i] == '~')
4385 new_len++; /* '*' needs to be replaced by ".*"
4386 '~' needs to be replaced by "\~" */
4387
4388 /* Buffer names are like file names. "." should be literal */
4389 if (context == EXPAND_BUFFERS && fname[i] == '.')
4390 new_len++; /* "." becomes "\." */
4391
4392 /* Custom expansion takes care of special things, match
4393 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004394 if ((context == EXPAND_USER_DEFINED
4395 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 new_len++; /* '\' becomes "\\" */
4397 }
4398 retval = alloc(new_len);
4399 if (retval != NULL)
4400 {
4401 retval[0] = '^';
4402 j = 1;
4403 for (i = 0; i < len; i++, j++)
4404 {
4405 /* Skip backslash. But why? At least keep it for custom
4406 * expansion. */
4407 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004408 && context != EXPAND_USER_LIST
4409 && fname[i] == '\\'
4410 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 break;
4412
4413 switch (fname[i])
4414 {
4415 case '*': retval[j++] = '.';
4416 break;
4417 case '~': retval[j++] = '\\';
4418 break;
4419 case '?': retval[j] = '.';
4420 continue;
4421 case '.': if (context == EXPAND_BUFFERS)
4422 retval[j++] = '\\';
4423 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004424 case '\\': if (context == EXPAND_USER_DEFINED
4425 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 retval[j++] = '\\';
4427 break;
4428 }
4429 retval[j] = fname[i];
4430 }
4431 retval[j] = NUL;
4432 }
4433 }
4434 }
4435 else
4436 {
4437 retval = alloc(len + 4);
4438 if (retval != NULL)
4439 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004440 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004443 * Don't add a star to *, ~, ~user, $var or `cmd`.
4444 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 * ~ would be at the start of the file name, but not the tail.
4446 * $ could be anywhere in the tail.
4447 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004448 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 */
4450 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004451 ends_in_star = (len > 0 && retval[len - 1] == '*');
4452#ifndef BACKSLASH_IN_FILENAME
4453 for (i = len - 2; i >= 0; --i)
4454 {
4455 if (retval[i] != '\\')
4456 break;
4457 ends_in_star = !ends_in_star;
4458 }
4459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004461 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 && vim_strchr(tail, '$') == NULL
4463 && vim_strchr(retval, '`') == NULL)
4464 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004465 else if (len > 0 && retval[len - 1] == '$')
4466 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 retval[len] = NUL;
4468 }
4469 }
4470 return retval;
4471}
4472
4473/*
4474 * Must parse the command line so far to work out what context we are in.
4475 * Completion can then be done based on that context.
4476 * This routine sets the variables:
4477 * xp->xp_pattern The start of the pattern to be expanded within
4478 * the command line (ends at the cursor).
4479 * xp->xp_context The type of thing to expand. Will be one of:
4480 *
4481 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4482 * the command line, like an unknown command. Caller
4483 * should beep.
4484 * EXPAND_NOTHING Unrecognised context for completion, use char like
4485 * a normal char, rather than for completion. eg
4486 * :s/^I/
4487 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4488 * it.
4489 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4490 * EXPAND_FILES After command with XFILE set, or after setting
4491 * with P_EXPAND set. eg :e ^I, :w>>^I
4492 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4493 * when we know only directories are of interest. eg
4494 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004495 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4497 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4498 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4499 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4500 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4501 * EXPAND_EVENTS Complete event names
4502 * EXPAND_SYNTAX Complete :syntax command arguments
4503 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4504 * EXPAND_AUGROUP Complete autocommand group names
4505 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4506 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4507 * eg :unmap a^I , :cunab x^I
4508 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4509 * eg :call sub^I
4510 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4511 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4512 * names in expressions, eg :while s^I
4513 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004514 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 */
4516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004517set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004519 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (ccline.cmdfirstc != ':'
4521#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004522 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004523 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524#endif
4525 )
4526 {
4527 xp->xp_context = EXPAND_NOTHING;
4528 return;
4529 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004530 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531}
4532
4533 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004534set_cmd_context(
4535 expand_T *xp,
4536 char_u *str, /* start of command line */
4537 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004538 int col, /* position of cursor */
4539 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540{
4541 int old_char = NUL;
4542 char_u *nextcomm;
4543
4544 /*
4545 * Avoid a UMR warning from Purify, only save the character if it has been
4546 * written before.
4547 */
4548 if (col < len)
4549 old_char = str[col];
4550 str[col] = NUL;
4551 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004552
4553#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004554 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004555 {
4556# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004557 /* pass CMD_SIZE because there is no real command */
4558 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004559# endif
4560 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004561 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004562 {
4563 xp->xp_context = ccline.xp_context;
4564 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004565# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004566 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004567# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004568 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004569 else
4570#endif
4571 while (nextcomm != NULL)
4572 nextcomm = set_one_cmd_context(xp, nextcomm);
4573
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004574 /* Store the string here so that call_user_expand_func() can get to them
4575 * easily. */
4576 xp->xp_line = str;
4577 xp->xp_col = col;
4578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 str[col] = old_char;
4580}
4581
4582/*
4583 * Expand the command line "str" from context "xp".
4584 * "xp" must have been set by set_cmd_context().
4585 * xp->xp_pattern points into "str", to where the text that is to be expanded
4586 * starts.
4587 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4588 * cursor.
4589 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4590 * key that triggered expansion literally.
4591 * Returns EXPAND_OK otherwise.
4592 */
4593 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004594expand_cmdline(
4595 expand_T *xp,
4596 char_u *str, /* start of command line */
4597 int col, /* position of cursor */
4598 int *matchcount, /* return: nr of matches */
4599 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600{
4601 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004602 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
4604 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4605 {
4606 beep_flush();
4607 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4608 }
4609 if (xp->xp_context == EXPAND_NOTHING)
4610 {
4611 /* Caller can use the character as a normal char instead */
4612 return EXPAND_NOTHING;
4613 }
4614
4615 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004616 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4617 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 if (file_str == NULL)
4619 return EXPAND_UNSUCCESSFUL;
4620
Bram Moolenaar94950a92010-12-02 16:01:29 +01004621 if (p_wic)
4622 options += WILD_ICASE;
4623
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004625 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
4627 *matchcount = 0;
4628 *matches = NULL;
4629 }
4630 vim_free(file_str);
4631
4632 return EXPAND_OK;
4633}
4634
4635#ifdef FEAT_MULTI_LANG
4636/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004637 * Cleanup matches for help tags:
4638 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4639 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004641static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
4643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004644cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
4646 int i, j;
4647 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004648 char_u buf[4];
4649 char_u *p = buf;
4650
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004651 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004652 {
4653 *p++ = '@';
4654 *p++ = p_hlg[0];
4655 *p++ = p_hlg[1];
4656 }
4657 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
4659 for (i = 0; i < num_file; ++i)
4660 {
4661 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004662 if (len <= 0)
4663 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004664 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
4666 /* Sorting on priority means the same item in another language may
4667 * be anywhere. Search all items for a match up to the "@en". */
4668 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004669 if (j != i && (int)STRLEN(file[j]) == len + 3
4670 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 break;
4672 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004673 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 file[i][len] = NUL;
4675 }
4676 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004677
4678 if (*buf != NUL)
4679 for (i = 0; i < num_file; ++i)
4680 {
4681 len = (int)STRLEN(file[i]) - 3;
4682 if (len <= 0)
4683 continue;
4684 if (STRCMP(file[i] + len, buf) == 0)
4685 {
4686 /* remove the default language */
4687 file[i][len] = NUL;
4688 }
4689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690}
4691#endif
4692
4693/*
4694 * Do the expansion based on xp->xp_context and "pat".
4695 */
4696 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004697ExpandFromContext(
4698 expand_T *xp,
4699 char_u *pat,
4700 int *num_file,
4701 char_u ***file,
4702 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
4704#ifdef FEAT_CMDL_COMPL
4705 regmatch_T regmatch;
4706#endif
4707 int ret;
4708 int flags;
4709
4710 flags = EW_DIR; /* include directories */
4711 if (options & WILD_LIST_NOTFOUND)
4712 flags |= EW_NOTFOUND;
4713 if (options & WILD_ADD_SLASH)
4714 flags |= EW_ADDSLASH;
4715 if (options & WILD_KEEP_ALL)
4716 flags |= EW_KEEPALL;
4717 if (options & WILD_SILENT)
4718 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004719 if (options & WILD_ALLLINKS)
4720 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004722 if (xp->xp_context == EXPAND_FILES
4723 || xp->xp_context == EXPAND_DIRECTORIES
4724 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
4726 /*
4727 * Expand file or directory names.
4728 */
4729 int free_pat = FALSE;
4730 int i;
4731
4732 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4733 * space */
4734 if (xp->xp_backslash != XP_BS_NONE)
4735 {
4736 free_pat = TRUE;
4737 pat = vim_strsave(pat);
4738 for (i = 0; pat[i]; ++i)
4739 if (pat[i] == '\\')
4740 {
4741 if (xp->xp_backslash == XP_BS_THREE
4742 && pat[i + 1] == '\\'
4743 && pat[i + 2] == '\\'
4744 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004745 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 if (xp->xp_backslash == XP_BS_ONE
4747 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004748 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 }
4750 }
4751
4752 if (xp->xp_context == EXPAND_FILES)
4753 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004754 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4755 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 else
4757 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004758 if (options & WILD_ICASE)
4759 flags |= EW_ICASE;
4760
Bram Moolenaard7834d32009-12-02 16:14:36 +00004761 /* Expand wildcards, supporting %:h and the like. */
4762 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 if (free_pat)
4764 vim_free(pat);
4765 return ret;
4766 }
4767
4768 *file = (char_u **)"";
4769 *num_file = 0;
4770 if (xp->xp_context == EXPAND_HELP)
4771 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004772 /* With an empty argument we would get all the help tags, which is
4773 * very slow. Get matches for "help" instead. */
4774 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4775 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 {
4777#ifdef FEAT_MULTI_LANG
4778 cleanup_help_tags(*num_file, *file);
4779#endif
4780 return OK;
4781 }
4782 return FAIL;
4783 }
4784
4785#ifndef FEAT_CMDL_COMPL
4786 return FAIL;
4787#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004788 if (xp->xp_context == EXPAND_SHELLCMD)
4789 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 if (xp->xp_context == EXPAND_OLD_SETTING)
4791 return ExpandOldSetting(num_file, file);
4792 if (xp->xp_context == EXPAND_BUFFERS)
4793 return ExpandBufnames(pat, num_file, file, options);
4794 if (xp->xp_context == EXPAND_TAGS
4795 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4796 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4797 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004798 {
4799 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004800 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4801 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004804 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004805 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004806 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004807 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004808 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004809 {
4810 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004811 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004812 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004813 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004814 {
4815 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004816 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004817 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004818# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4819 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004820 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004821# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004822 if (xp->xp_context == EXPAND_PACKADD)
4823 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
4825 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4826 if (regmatch.regprog == NULL)
4827 return FAIL;
4828
4829 /* set ignore-case according to p_ic, p_scs and pat */
4830 regmatch.rm_ic = ignorecase(pat);
4831
4832 if (xp->xp_context == EXPAND_SETTINGS
4833 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4834 ret = ExpandSettings(xp, &regmatch, num_file, file);
4835 else if (xp->xp_context == EXPAND_MAPPINGS)
4836 ret = ExpandMappings(&regmatch, num_file, file);
4837# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4838 else if (xp->xp_context == EXPAND_USER_DEFINED)
4839 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4840# endif
4841 else
4842 {
4843 static struct expgen
4844 {
4845 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004846 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004848 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 } tab[] =
4850 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004851 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4852 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004853 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004854#ifdef FEAT_CMDHIST
4855 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004858 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004859 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004860 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4861 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4862 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863#endif
4864#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004865 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4866 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4867 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4868 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869#endif
4870#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004871 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4872 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873#endif
4874#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004875 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004877#ifdef FEAT_PROFILE
4878 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4879#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004880 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004882 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4883 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004885#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004886 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004887#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004888#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004889 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004890#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004891#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004892 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4895 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004896 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4897 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004899 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004900 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 };
4902 int i;
4903
4904 /*
4905 * Find a context in the table and call the ExpandGeneric() with the
4906 * right function to do the expansion.
4907 */
4908 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004909 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 if (xp->xp_context == tab[i].context)
4911 {
4912 if (tab[i].ic)
4913 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004914 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004915 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 break;
4917 }
4918 }
4919
Bram Moolenaar473de612013-06-08 18:19:48 +02004920 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
4922 return ret;
4923#endif /* FEAT_CMDL_COMPL */
4924}
4925
4926#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4927/*
4928 * Expand a list of names.
4929 *
4930 * Generic function for command line completion. It calls a function to
4931 * obtain strings, one by one. The strings are matched against a regexp
4932 * program. Matching strings are copied into an array, which is returned.
4933 *
4934 * Returns OK when no problems encountered, FAIL for error (out of memory).
4935 */
4936 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004937ExpandGeneric(
4938 expand_T *xp,
4939 regmatch_T *regmatch,
4940 int *num_file,
4941 char_u ***file,
4942 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004944 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
4946 int i;
4947 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004948 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 char_u *str;
4950
4951 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004952 * round == 0: count the number of matching names
4953 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004955 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
4957 for (i = 0; ; ++i)
4958 {
4959 str = (*func)(xp, i);
4960 if (str == NULL) /* end of list */
4961 break;
4962 if (*str == NUL) /* skip empty strings */
4963 continue;
4964
4965 if (vim_regexec(regmatch, str, (colnr_T)0))
4966 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004967 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004969 if (escaped)
4970 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4971 else
4972 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 (*file)[count] = str;
4974#ifdef FEAT_MENU
4975 if (func == get_menu_names && str != NULL)
4976 {
4977 /* test for separator added by get_menu_names() */
4978 str += STRLEN(str) - 1;
4979 if (*str == '\001')
4980 *str = '.';
4981 }
4982#endif
4983 }
4984 ++count;
4985 }
4986 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004987 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
4989 if (count == 0)
4990 return OK;
4991 *num_file = count;
4992 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4993 if (*file == NULL)
4994 {
4995 *file = (char_u **)"";
4996 return FAIL;
4997 }
4998 count = 0;
4999 }
5000 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005001
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005002 /* Sort the results. Keep menu's in the specified order. */
5003 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005004 {
5005 if (xp->xp_context == EXPAND_EXPRESSION
5006 || xp->xp_context == EXPAND_FUNCTIONS
5007 || xp->xp_context == EXPAND_USER_FUNC)
5008 /* <SNR> functions should be sorted to the end. */
5009 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5010 sort_func_compare);
5011 else
5012 sort_strings(*file, *num_file);
5013 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005014
Bram Moolenaar4f688582007-07-24 12:34:30 +00005015#ifdef FEAT_CMDL_COMPL
5016 /* Reset the variables used for special highlight names expansion, so that
5017 * they don't show up when getting normal highlight names by ID. */
5018 reset_expand_highlight();
5019#endif
5020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 return OK;
5022}
5023
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005024/*
5025 * Complete a shell command.
5026 * Returns FAIL or OK;
5027 */
5028 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005029expand_shellcmd(
5030 char_u *filepat, /* pattern to match with command names */
5031 int *num_file, /* return: number of matches */
5032 char_u ***file, /* return: array with matches */
5033 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005034{
5035 char_u *pat;
5036 int i;
5037 char_u *path;
5038 int mustfree = FALSE;
5039 garray_T ga;
5040 char_u *buf = alloc(MAXPATHL);
5041 size_t l;
5042 char_u *s, *e;
5043 int flags = flagsarg;
5044 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005045 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005046
5047 if (buf == NULL)
5048 return FAIL;
5049
5050 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5051 * space */
5052 pat = vim_strsave(filepat);
5053 for (i = 0; pat[i]; ++i)
5054 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005055 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005056
Bram Moolenaarb5971142015-03-21 17:32:19 +01005057 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005058
5059 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005060 if (mch_isFullName(pat))
5061 path = (char_u *)" ";
5062 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005063 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5064 path = (char_u *)".";
5065 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005066 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005067 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005068 if (path == NULL)
5069 path = (char_u *)"";
5070 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005071
5072 /*
5073 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005074 * collect them in "ga". When "." is not in $PATH also expand for the
5075 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005076 */
5077 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005078 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005079 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005080 if (*s == NUL)
5081 {
5082 if (did_curdir)
5083 break;
5084 /* Find directories in the current directory, path is empty. */
5085 did_curdir = TRUE;
5086 }
5087 else if (*s == '.')
5088 did_curdir = TRUE;
5089
Bram Moolenaar68c31742006-09-02 15:54:18 +00005090 if (*s == ' ')
5091 ++s; /* Skip space used for absolute path name. */
5092
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005093#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005094 e = vim_strchr(s, ';');
5095#else
5096 e = vim_strchr(s, ':');
5097#endif
5098 if (e == NULL)
5099 e = s + STRLEN(s);
5100
5101 l = e - s;
5102 if (l > MAXPATHL - 5)
5103 break;
5104 vim_strncpy(buf, s, l);
5105 add_pathsep(buf);
5106 l = STRLEN(buf);
5107 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5108
5109 /* Expand matches in one directory of $PATH. */
5110 ret = expand_wildcards(1, &buf, num_file, file, flags);
5111 if (ret == OK)
5112 {
5113 if (ga_grow(&ga, *num_file) == FAIL)
5114 FreeWild(*num_file, *file);
5115 else
5116 {
5117 for (i = 0; i < *num_file; ++i)
5118 {
5119 s = (*file)[i];
5120 if (STRLEN(s) > l)
5121 {
5122 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005123 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005124 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5125 }
5126 else
5127 vim_free(s);
5128 }
5129 vim_free(*file);
5130 }
5131 }
5132 if (*e != NUL)
5133 ++e;
5134 }
5135 *file = ga.ga_data;
5136 *num_file = ga.ga_len;
5137
5138 vim_free(buf);
5139 vim_free(pat);
5140 if (mustfree)
5141 vim_free(path);
5142 return OK;
5143}
5144
5145
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005147static 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 +00005148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005150 * Call "user_expand_func()" to invoke a user defined Vim script function and
5151 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005153 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005154call_user_expand_func(
5155 void *(*user_expand_func)(char_u *, int, char_u **, int),
5156 expand_T *xp,
5157 int *num_file,
5158 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005160 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005161 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005164 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005165 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166
Bram Moolenaarb7515462013-06-29 12:58:33 +02005167 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005168 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 *num_file = 0;
5170 *file = NULL;
5171
Bram Moolenaarb7515462013-06-29 12:58:33 +02005172 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005173 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005174 keep = ccline.cmdbuff[ccline.cmdlen];
5175 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005176 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005177
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005178 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005179 args[1] = xp->xp_line;
5180 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 args[2] = num;
5182
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005183 /* Save the cmdline, we don't know what the function may do. */
5184 save_ccline = ccline;
5185 ccline.cmdbuff = NULL;
5186 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005188
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005189 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005190
5191 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005193 if (ccline.cmdbuff != NULL)
5194 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005195
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005196 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005197 return ret;
5198}
5199
5200/*
5201 * Expand names with a function defined by the user.
5202 */
5203 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005204ExpandUserDefined(
5205 expand_T *xp,
5206 regmatch_T *regmatch,
5207 int *num_file,
5208 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005209{
5210 char_u *retstr;
5211 char_u *s;
5212 char_u *e;
5213 char_u keep;
5214 garray_T ga;
5215
5216 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5217 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 return FAIL;
5219
5220 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005221 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
5223 e = vim_strchr(s, '\n');
5224 if (e == NULL)
5225 e = s + STRLEN(s);
5226 keep = *e;
5227 *e = 0;
5228
5229 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5230 {
5231 *e = keep;
5232 if (*e != NUL)
5233 ++e;
5234 continue;
5235 }
5236
5237 if (ga_grow(&ga, 1) == FAIL)
5238 break;
5239
5240 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5241 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
5243 *e = keep;
5244 if (*e != NUL)
5245 ++e;
5246 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005247 vim_free(retstr);
5248 *file = ga.ga_data;
5249 *num_file = ga.ga_len;
5250 return OK;
5251}
5252
5253/*
5254 * Expand names with a list returned by a function defined by the user.
5255 */
5256 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005257ExpandUserList(
5258 expand_T *xp,
5259 int *num_file,
5260 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005261{
5262 list_T *retlist;
5263 listitem_T *li;
5264 garray_T ga;
5265
5266 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5267 if (retlist == NULL)
5268 return FAIL;
5269
5270 ga_init2(&ga, (int)sizeof(char *), 3);
5271 /* Loop over the items in the list. */
5272 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5273 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005274 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5275 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005276
5277 if (ga_grow(&ga, 1) == FAIL)
5278 break;
5279
5280 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005281 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005282 ++ga.ga_len;
5283 }
5284 list_unref(retlist);
5285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 *file = ga.ga_data;
5287 *num_file = ga.ga_len;
5288 return OK;
5289}
5290#endif
5291
5292/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005293 * Expand color scheme, compiler or filetype names.
5294 * Search from 'runtimepath':
5295 * 'runtimepath'/{dirnames}/{pat}.vim
5296 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5297 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5298 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5299 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005300 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 */
5302 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005303ExpandRTDir(
5304 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005305 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005306 int *num_file,
5307 char_u ***file,
5308 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 char_u *s;
5311 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005312 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005314 int i;
5315 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
5317 *num_file = 0;
5318 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005319 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005320 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005322 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005324 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5325 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005327 ga_clear_strings(&ga);
5328 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005330 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005331 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005332 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005334
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005335 if (flags & DIP_START) {
5336 for (i = 0; dirnames[i] != NULL; ++i)
5337 {
5338 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5339 if (s == NULL)
5340 {
5341 ga_clear_strings(&ga);
5342 return FAIL;
5343 }
5344 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5345 globpath(p_pp, s, &ga, 0);
5346 vim_free(s);
5347 }
5348 }
5349
5350 if (flags & DIP_OPT) {
5351 for (i = 0; dirnames[i] != NULL; ++i)
5352 {
5353 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5354 if (s == NULL)
5355 {
5356 ga_clear_strings(&ga);
5357 return FAIL;
5358 }
5359 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5360 globpath(p_pp, s, &ga, 0);
5361 vim_free(s);
5362 }
5363 }
5364
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005365 for (i = 0; i < ga.ga_len; ++i)
5366 {
5367 match = ((char_u **)ga.ga_data)[i];
5368 s = match;
5369 e = s + STRLEN(s);
5370 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5371 {
5372 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005373 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005374 if (s < match || vim_ispathsep(*s))
5375 break;
5376 ++s;
5377 *e = NUL;
5378 mch_memmove(match, s, e - s + 1);
5379 }
5380 }
5381
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005382 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005383 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005384
5385 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005386 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005387 remove_duplicates(&ga);
5388
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 *file = ga.ga_data;
5390 *num_file = ga.ga_len;
5391 return OK;
5392}
5393
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005394/*
5395 * Expand loadplugin names:
5396 * 'packpath'/pack/ * /opt/{pat}
5397 */
5398 static int
5399ExpandPackAddDir(
5400 char_u *pat,
5401 int *num_file,
5402 char_u ***file)
5403{
5404 char_u *s;
5405 char_u *e;
5406 char_u *match;
5407 garray_T ga;
5408 int i;
5409 int pat_len;
5410
5411 *num_file = 0;
5412 *file = NULL;
5413 pat_len = (int)STRLEN(pat);
5414 ga_init2(&ga, (int)sizeof(char *), 10);
5415
5416 s = alloc((unsigned)(pat_len + 26));
5417 if (s == NULL)
5418 {
5419 ga_clear_strings(&ga);
5420 return FAIL;
5421 }
5422 sprintf((char *)s, "pack/*/opt/%s*", pat);
5423 globpath(p_pp, s, &ga, 0);
5424 vim_free(s);
5425
5426 for (i = 0; i < ga.ga_len; ++i)
5427 {
5428 match = ((char_u **)ga.ga_data)[i];
5429 s = gettail(match);
5430 e = s + STRLEN(s);
5431 mch_memmove(match, s, e - s + 1);
5432 }
5433
5434 if (ga.ga_len == 0)
5435 return FAIL;
5436
5437 /* Sort and remove duplicates which can happen when specifying multiple
5438 * directories in dirnames. */
5439 remove_duplicates(&ga);
5440
5441 *file = ga.ga_data;
5442 *num_file = ga.ga_len;
5443 return OK;
5444}
5445
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446#endif
5447
5448#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5449/*
5450 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005451 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005453 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005454globpath(
5455 char_u *path,
5456 char_u *file,
5457 garray_T *ga,
5458 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 expand_T xpc;
5461 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 int num_p;
5464 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465
5466 buf = alloc(MAXPATHL);
5467 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005468 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005470 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005472
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 /* Loop over all entries in {path}. */
5474 while (*path != NUL)
5475 {
5476 /* Copy one item of the path to buf[] and concatenate the file name. */
5477 copy_option_part(&path, buf, MAXPATHL, ",");
5478 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5479 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005480# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005481 /* Using the platform's path separator (\) makes vim incorrectly
5482 * treat it as an escape character, use '/' instead. */
5483 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5484 STRCAT(buf, "/");
5485# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005489 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5490 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005492 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005494 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 for (i = 0; i < num_p; ++i)
5497 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005498 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005499 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005500 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005503
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 FreeWild(num_p, p);
5505 }
5506 }
5507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510}
5511
5512#endif
5513
5514#if defined(FEAT_CMDHIST) || defined(PROTO)
5515
5516/*********************************
5517 * Command line history stuff *
5518 *********************************/
5519
5520/*
5521 * Translate a history character to the associated type number.
5522 */
5523 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005524hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 if (c == ':')
5527 return HIST_CMD;
5528 if (c == '=')
5529 return HIST_EXPR;
5530 if (c == '@')
5531 return HIST_INPUT;
5532 if (c == '>')
5533 return HIST_DEBUG;
5534 return HIST_SEARCH; /* must be '?' or '/' */
5535}
5536
5537/*
5538 * Table of history names.
5539 * These names are used in :history and various hist...() functions.
5540 * It is sufficient to give the significant prefix of a history name.
5541 */
5542
5543static char *(history_names[]) =
5544{
5545 "cmd",
5546 "search",
5547 "expr",
5548 "input",
5549 "debug",
5550 NULL
5551};
5552
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005553#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5554/*
5555 * Function given to ExpandGeneric() to obtain the possible first
5556 * arguments of the ":history command.
5557 */
5558 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005560{
5561 static char_u compl[2] = { NUL, NUL };
5562 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005563 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005564 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5565
5566 if (idx < short_names_count)
5567 {
5568 compl[0] = (char_u)short_names[idx];
5569 return compl;
5570 }
5571 if (idx < short_names_count + history_name_count)
5572 return (char_u *)history_names[idx - short_names_count];
5573 if (idx == short_names_count + history_name_count)
5574 return (char_u *)"all";
5575 return NULL;
5576}
5577#endif
5578
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579/*
5580 * init_history() - Initialize the command line history.
5581 * Also used to re-allocate the history when the size changes.
5582 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005583 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005584init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585{
5586 int newlen; /* new length of history table */
5587 histentry_T *temp;
5588 int i;
5589 int j;
5590 int type;
5591
5592 /*
5593 * If size of history table changed, reallocate it
5594 */
5595 newlen = (int)p_hi;
5596 if (newlen != hislen) /* history length changed */
5597 {
5598 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5599 {
5600 if (newlen)
5601 {
5602 temp = (histentry_T *)lalloc(
5603 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5604 if (temp == NULL) /* out of memory! */
5605 {
5606 if (type == 0) /* first one: just keep the old length */
5607 {
5608 newlen = hislen;
5609 break;
5610 }
5611 /* Already changed one table, now we can only have zero
5612 * length for all tables. */
5613 newlen = 0;
5614 type = -1;
5615 continue;
5616 }
5617 }
5618 else
5619 temp = NULL;
5620 if (newlen == 0 || temp != NULL)
5621 {
5622 if (hisidx[type] < 0) /* there are no entries yet */
5623 {
5624 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005625 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else if (newlen > hislen) /* array becomes bigger */
5628 {
5629 for (i = 0; i <= hisidx[type]; ++i)
5630 temp[i] = history[type][i];
5631 j = i;
5632 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005633 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 for ( ; j < hislen; ++i, ++j)
5635 temp[i] = history[type][j];
5636 }
5637 else /* array becomes smaller or 0 */
5638 {
5639 j = hisidx[type];
5640 for (i = newlen - 1; ; --i)
5641 {
5642 if (i >= 0) /* copy newest entries */
5643 temp[i] = history[type][j];
5644 else /* remove older entries */
5645 vim_free(history[type][j].hisstr);
5646 if (--j < 0)
5647 j = hislen - 1;
5648 if (j == hisidx[type])
5649 break;
5650 }
5651 hisidx[type] = newlen - 1;
5652 }
5653 vim_free(history[type]);
5654 history[type] = temp;
5655 }
5656 }
5657 hislen = newlen;
5658 }
5659}
5660
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005662clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005663{
5664 hisptr->hisnum = 0;
5665 hisptr->viminfo = FALSE;
5666 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005667 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005668}
5669
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670/*
5671 * Check if command line 'str' is already in history.
5672 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5673 */
5674 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005675in_history(
5676 int type,
5677 char_u *str,
5678 int move_to_front, /* Move the entry to the front if it exists */
5679 int sep,
5680 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 int i;
5683 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005684 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686 if (hisidx[type] < 0)
5687 return FALSE;
5688 i = hisidx[type];
5689 do
5690 {
5691 if (history[type][i].hisstr == NULL)
5692 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005693
5694 /* For search history, check that the separator character matches as
5695 * well. */
5696 p = history[type][i].hisstr;
5697 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005698 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005699 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
5701 if (!move_to_front)
5702 return TRUE;
5703 last_i = i;
5704 break;
5705 }
5706 if (--i < 0)
5707 i = hislen - 1;
5708 } while (i != hisidx[type]);
5709
5710 if (last_i >= 0)
5711 {
5712 str = history[type][i].hisstr;
5713 while (i != hisidx[type])
5714 {
5715 if (++i >= hislen)
5716 i = 0;
5717 history[type][last_i] = history[type][i];
5718 last_i = i;
5719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005721 history[type][i].viminfo = FALSE;
5722 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005723 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 return TRUE;
5725 }
5726 return FALSE;
5727}
5728
5729/*
5730 * Convert history name (from table above) to its HIST_ equivalent.
5731 * When "name" is empty, return "cmd" history.
5732 * Returns -1 for unknown history name.
5733 */
5734 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005735get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737 int i;
5738 int len = (int)STRLEN(name);
5739
5740 /* No argument: use current history. */
5741 if (len == 0)
5742 return hist_char2type(ccline.cmdfirstc);
5743
5744 for (i = 0; history_names[i] != NULL; ++i)
5745 if (STRNICMP(name, history_names[i], len) == 0)
5746 return i;
5747
5748 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5749 return hist_char2type(name[0]);
5750
5751 return -1;
5752}
5753
5754static int last_maptick = -1; /* last seen maptick */
5755
5756/*
5757 * Add the given string to the given history. If the string is already in the
5758 * history then it is moved to the front. "histype" may be one of he HIST_
5759 * values.
5760 */
5761 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005762add_to_history(
5763 int histype,
5764 char_u *new_entry,
5765 int in_map, /* consider maptick when inside a mapping */
5766 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
5768 histentry_T *hisptr;
5769 int len;
5770
5771 if (hislen == 0) /* no history */
5772 return;
5773
Bram Moolenaara939e432013-11-09 05:30:26 +01005774 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5775 return;
5776
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 /*
5778 * Searches inside the same mapping overwrite each other, so that only
5779 * the last line is kept. Be careful not to remove a line that was moved
5780 * down, only lines that were added.
5781 */
5782 if (histype == HIST_SEARCH && in_map)
5783 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005784 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 {
5786 /* Current line is from the same mapping, remove it */
5787 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5788 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005789 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 --hisnum[histype];
5791 if (--hisidx[HIST_SEARCH] < 0)
5792 hisidx[HIST_SEARCH] = hislen - 1;
5793 }
5794 last_maptick = -1;
5795 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005796 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
5798 if (++hisidx[histype] == hislen)
5799 hisidx[histype] = 0;
5800 hisptr = &history[histype][hisidx[histype]];
5801 vim_free(hisptr->hisstr);
5802
5803 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005804 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5806 if (hisptr->hisstr != NULL)
5807 hisptr->hisstr[len + 1] = sep;
5808
5809 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005810 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005811 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 if (histype == HIST_SEARCH && in_map)
5813 last_maptick = maptick;
5814 }
5815}
5816
5817#if defined(FEAT_EVAL) || defined(PROTO)
5818
5819/*
5820 * Get identifier of newest history entry.
5821 * "histype" may be one of the HIST_ values.
5822 */
5823 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005824get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5827 || hisidx[histype] < 0)
5828 return -1;
5829
5830 return history[histype][hisidx[histype]].hisnum;
5831}
5832
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 * Calculate history index from a number:
5835 * num > 0: seen as identifying number of a history entry
5836 * num < 0: relative position in history wrt newest entry
5837 * "histype" may be one of the HIST_ values.
5838 */
5839 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005840calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 int i;
5843 histentry_T *hist;
5844 int wrapped = FALSE;
5845
5846 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5847 || (i = hisidx[histype]) < 0 || num == 0)
5848 return -1;
5849
5850 hist = history[histype];
5851 if (num > 0)
5852 {
5853 while (hist[i].hisnum > num)
5854 if (--i < 0)
5855 {
5856 if (wrapped)
5857 break;
5858 i += hislen;
5859 wrapped = TRUE;
5860 }
5861 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5862 return i;
5863 }
5864 else if (-num <= hislen)
5865 {
5866 i += num + 1;
5867 if (i < 0)
5868 i += hislen;
5869 if (hist[i].hisstr != NULL)
5870 return i;
5871 }
5872 return -1;
5873}
5874
5875/*
5876 * Get a history entry by its index.
5877 * "histype" may be one of the HIST_ values.
5878 */
5879 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005880get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 idx = calc_hist_idx(histype, idx);
5883 if (idx >= 0)
5884 return history[histype][idx].hisstr;
5885 else
5886 return (char_u *)"";
5887}
5888
5889/*
5890 * Clear all entries of a history.
5891 * "histype" may be one of the HIST_ values.
5892 */
5893 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005894clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895{
5896 int i;
5897 histentry_T *hisptr;
5898
5899 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5900 {
5901 hisptr = history[histype];
5902 for (i = hislen; i--;)
5903 {
5904 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005905 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005906 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
5908 hisidx[histype] = -1; /* mark history as cleared */
5909 hisnum[histype] = 0; /* reset identifier counter */
5910 return OK;
5911 }
5912 return FAIL;
5913}
5914
5915/*
5916 * Remove all entries matching {str} from a history.
5917 * "histype" may be one of the HIST_ values.
5918 */
5919 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005920del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
5922 regmatch_T regmatch;
5923 histentry_T *hisptr;
5924 int idx;
5925 int i;
5926 int last;
5927 int found = FALSE;
5928
5929 regmatch.regprog = NULL;
5930 regmatch.rm_ic = FALSE; /* always match case */
5931 if (hislen != 0
5932 && histype >= 0
5933 && histype < HIST_COUNT
5934 && *str != NUL
5935 && (idx = hisidx[histype]) >= 0
5936 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5937 != NULL)
5938 {
5939 i = last = idx;
5940 do
5941 {
5942 hisptr = &history[histype][i];
5943 if (hisptr->hisstr == NULL)
5944 break;
5945 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5946 {
5947 found = TRUE;
5948 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005949 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 }
5951 else
5952 {
5953 if (i != last)
5954 {
5955 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005956 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 }
5958 if (--last < 0)
5959 last += hislen;
5960 }
5961 if (--i < 0)
5962 i += hislen;
5963 } while (i != idx);
5964 if (history[histype][idx].hisstr == NULL)
5965 hisidx[histype] = -1;
5966 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005967 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 return found;
5969}
5970
5971/*
5972 * Remove an indexed entry from a history.
5973 * "histype" may be one of the HIST_ values.
5974 */
5975 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005976del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 int i, j;
5979
5980 i = calc_hist_idx(histype, idx);
5981 if (i < 0)
5982 return FALSE;
5983 idx = hisidx[histype];
5984 vim_free(history[histype][i].hisstr);
5985
5986 /* When deleting the last added search string in a mapping, reset
5987 * last_maptick, so that the last added search string isn't deleted again.
5988 */
5989 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5990 last_maptick = -1;
5991
5992 while (i != idx)
5993 {
5994 j = (i + 1) % hislen;
5995 history[histype][i] = history[histype][j];
5996 i = j;
5997 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005998 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 if (--i < 0)
6000 i += hislen;
6001 hisidx[histype] = i;
6002 return TRUE;
6003}
6004
6005#endif /* FEAT_EVAL */
6006
6007#if defined(FEAT_CRYPT) || defined(PROTO)
6008/*
6009 * Very specific function to remove the value in ":set key=val" from the
6010 * history.
6011 */
6012 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006013remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
6015 char_u *p;
6016 int i;
6017
6018 i = hisidx[HIST_CMD];
6019 if (i < 0)
6020 return;
6021 p = history[HIST_CMD][i].hisstr;
6022 if (p != NULL)
6023 for ( ; *p; ++p)
6024 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6025 {
6026 p = vim_strchr(p + 3, '=');
6027 if (p == NULL)
6028 break;
6029 ++p;
6030 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
6031 if (p[i] == '\\' && p[i + 1])
6032 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006033 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 --p;
6035 }
6036}
6037#endif
6038
6039#endif /* FEAT_CMDHIST */
6040
Bram Moolenaar064154c2016-03-19 22:50:43 +01006041#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006042/*
6043 * Get pointer to the command line info to use. cmdline_paste() may clear
6044 * ccline and put the previous value in prev_ccline.
6045 */
6046 static struct cmdline_info *
6047get_ccline_ptr(void)
6048{
6049 if ((State & CMDLINE) == 0)
6050 return NULL;
6051 if (ccline.cmdbuff != NULL)
6052 return &ccline;
6053 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6054 return &prev_ccline;
6055 return NULL;
6056}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006057#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006058
Bram Moolenaar064154c2016-03-19 22:50:43 +01006059#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006060/*
6061 * Get the current command line in allocated memory.
6062 * Only works when the command line is being edited.
6063 * Returns NULL when something is wrong.
6064 */
6065 char_u *
6066get_cmdline_str(void)
6067{
6068 struct cmdline_info *p = get_ccline_ptr();
6069
6070 if (p == NULL)
6071 return NULL;
6072 return vim_strnsave(p->cmdbuff, p->cmdlen);
6073}
6074
6075/*
6076 * Get the current command line position, counted in bytes.
6077 * Zero is the first position.
6078 * Only works when the command line is being edited.
6079 * Returns -1 when something is wrong.
6080 */
6081 int
6082get_cmdline_pos(void)
6083{
6084 struct cmdline_info *p = get_ccline_ptr();
6085
6086 if (p == NULL)
6087 return -1;
6088 return p->cmdpos;
6089}
6090
6091/*
6092 * Set the command line byte position to "pos". Zero is the first position.
6093 * Only works when the command line is being edited.
6094 * Returns 1 when failed, 0 when OK.
6095 */
6096 int
6097set_cmdline_pos(
6098 int pos)
6099{
6100 struct cmdline_info *p = get_ccline_ptr();
6101
6102 if (p == NULL)
6103 return 1;
6104
6105 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6106 * changed the command line. */
6107 if (pos < 0)
6108 new_cmdpos = 0;
6109 else
6110 new_cmdpos = pos;
6111 return 0;
6112}
6113#endif
6114
6115#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6116/*
6117 * Get the current command-line type.
6118 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6119 * Only works when the command line is being edited.
6120 * Returns NUL when something is wrong.
6121 */
6122 int
6123get_cmdline_type(void)
6124{
6125 struct cmdline_info *p = get_ccline_ptr();
6126
6127 if (p == NULL)
6128 return NUL;
6129 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006130 return
6131# ifdef FEAT_EVAL
6132 (p->input_fn) ? '@' :
6133# endif
6134 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006135 return p->cmdfirstc;
6136}
6137#endif
6138
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6140/*
6141 * Get indices "num1,num2" that specify a range within a list (not a range of
6142 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6143 * Returns OK if parsed successfully, otherwise FAIL.
6144 */
6145 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006146get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 int len;
6149 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006150 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
6152 *str = skipwhite(*str);
6153 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6154 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006155 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 *str += len;
6157 *num1 = (int)num;
6158 first = TRUE;
6159 }
6160 *str = skipwhite(*str);
6161 if (**str == ',') /* parse "to" part of range */
6162 {
6163 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006164 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 if (len > 0)
6166 {
6167 *num2 = (int)num;
6168 *str = skipwhite(*str + len);
6169 }
6170 else if (!first) /* no number given at all */
6171 return FAIL;
6172 }
6173 else if (first) /* only one number given */
6174 *num2 = *num1;
6175 return OK;
6176}
6177#endif
6178
6179#if defined(FEAT_CMDHIST) || defined(PROTO)
6180/*
6181 * :history command - print a history
6182 */
6183 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006184ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
6186 histentry_T *hist;
6187 int histype1 = HIST_CMD;
6188 int histype2 = HIST_CMD;
6189 int hisidx1 = 1;
6190 int hisidx2 = -1;
6191 int idx;
6192 int i, j, k;
6193 char_u *end;
6194 char_u *arg = eap->arg;
6195
6196 if (hislen == 0)
6197 {
6198 MSG(_("'history' option is zero"));
6199 return;
6200 }
6201
6202 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6203 {
6204 end = arg;
6205 while (ASCII_ISALPHA(*end)
6206 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6207 end++;
6208 i = *end;
6209 *end = NUL;
6210 histype1 = get_histtype(arg);
6211 if (histype1 == -1)
6212 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006213 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
6215 histype1 = 0;
6216 histype2 = HIST_COUNT-1;
6217 }
6218 else
6219 {
6220 *end = i;
6221 EMSG(_(e_trailing));
6222 return;
6223 }
6224 }
6225 else
6226 histype2 = histype1;
6227 *end = i;
6228 }
6229 else
6230 end = arg;
6231 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6232 {
6233 EMSG(_(e_trailing));
6234 return;
6235 }
6236
6237 for (; !got_int && histype1 <= histype2; ++histype1)
6238 {
6239 STRCPY(IObuff, "\n # ");
6240 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6241 MSG_PUTS_TITLE(IObuff);
6242 idx = hisidx[histype1];
6243 hist = history[histype1];
6244 j = hisidx1;
6245 k = hisidx2;
6246 if (j < 0)
6247 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6248 if (k < 0)
6249 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6250 if (idx >= 0 && j <= k)
6251 for (i = idx + 1; !got_int; ++i)
6252 {
6253 if (i == hislen)
6254 i = 0;
6255 if (hist[i].hisstr != NULL
6256 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6257 {
6258 msg_putchar('\n');
6259 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6260 hist[i].hisnum);
6261 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6262 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006263 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 else
6265 STRCAT(IObuff, hist[i].hisstr);
6266 msg_outtrans(IObuff);
6267 out_flush();
6268 }
6269 if (i == idx)
6270 break;
6271 }
6272 }
6273}
6274#endif
6275
6276#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006277/*
6278 * Buffers for history read from a viminfo file. Only valid while reading.
6279 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006280static histentry_T *viminfo_history[HIST_COUNT] =
6281 {NULL, NULL, NULL, NULL, NULL};
6282static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6283static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284static int viminfo_add_at_front = FALSE;
6285
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006286static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287
6288/*
6289 * Translate a history type number to the associated character.
6290 */
6291 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006292hist_type2char(
6293 int type,
6294 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295{
6296 if (type == HIST_CMD)
6297 return ':';
6298 if (type == HIST_SEARCH)
6299 {
6300 if (use_question)
6301 return '?';
6302 else
6303 return '/';
6304 }
6305 if (type == HIST_EXPR)
6306 return '=';
6307 return '@';
6308}
6309
6310/*
6311 * Prepare for reading the history from the viminfo file.
6312 * This allocates history arrays to store the read history lines.
6313 */
6314 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 int i;
6318 int num;
6319 int type;
6320 int len;
6321
6322 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006323 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if (asklen > hislen)
6325 asklen = hislen;
6326
6327 for (type = 0; type < HIST_COUNT; ++type)
6328 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006329 /* Count the number of empty spaces in the history list. Entries read
6330 * from viminfo previously are also considered empty. If there are
6331 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006333 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 num++;
6335 len = asklen;
6336 if (num > len)
6337 len = num;
6338 if (len <= 0)
6339 viminfo_history[type] = NULL;
6340 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006341 viminfo_history[type] = (histentry_T *)lalloc(
6342 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (viminfo_history[type] == NULL)
6344 len = 0;
6345 viminfo_hislen[type] = len;
6346 viminfo_hisidx[type] = 0;
6347 }
6348}
6349
6350/*
6351 * Accept a line from the viminfo, store it in the history array when it's
6352 * new.
6353 */
6354 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006355read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356{
6357 int type;
6358 long_u len;
6359 char_u *val;
6360 char_u *p;
6361
6362 type = hist_char2type(virp->vir_line[0]);
6363 if (viminfo_hisidx[type] < viminfo_hislen[type])
6364 {
6365 val = viminfo_readstring(virp, 1, TRUE);
6366 if (val != NULL && *val != NUL)
6367 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006368 int sep = (*val == ' ' ? NUL : *val);
6369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006371 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 {
6373 /* Need to re-allocate to append the separator byte. */
6374 len = STRLEN(val);
6375 p = lalloc(len + 2, TRUE);
6376 if (p != NULL)
6377 {
6378 if (type == HIST_SEARCH)
6379 {
6380 /* Search entry: Move the separator from the first
6381 * column to after the NUL. */
6382 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006383 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
6385 else
6386 {
6387 /* Not a search entry: No separator in the viminfo
6388 * file, add a NUL separator. */
6389 mch_memmove(p, val, (size_t)len + 1);
6390 p[len + 1] = NUL;
6391 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006392 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6393 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006394 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6395 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006396 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 }
6398 }
6399 }
6400 vim_free(val);
6401 }
6402 return viminfo_readline(virp);
6403}
6404
Bram Moolenaar07219f92013-04-14 23:19:36 +02006405/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006406 * Accept a new style history line from the viminfo, store it in the history
6407 * array when it's new.
6408 */
6409 void
6410handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006411 garray_T *values,
6412 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006413{
6414 int type;
6415 long_u len;
6416 char_u *val;
6417 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006418 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006419
6420 /* Check the format:
6421 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006422 if (values->ga_len < 4
6423 || vp[0].bv_type != BVAL_NR
6424 || vp[1].bv_type != BVAL_NR
6425 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6426 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006427 return;
6428
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006429 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006430 if (type >= HIST_COUNT)
6431 return;
6432 if (viminfo_hisidx[type] < viminfo_hislen[type])
6433 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006434 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006435 if (val != NULL && *val != NUL)
6436 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006437 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6438 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006439 int idx;
6440 int overwrite = FALSE;
6441
6442 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6443 {
6444 /* If lines were written by an older Vim we need to avoid
6445 * getting duplicates. See if the entry already exists. */
6446 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6447 {
6448 p = viminfo_history[type][idx].hisstr;
6449 if (STRCMP(val, p) == 0
6450 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6451 {
6452 overwrite = TRUE;
6453 break;
6454 }
6455 }
6456
6457 if (!overwrite)
6458 {
6459 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006460 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006461 p = lalloc(len + 2, TRUE);
6462 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006463 else
6464 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006465 if (p != NULL)
6466 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006467 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006468 if (!overwrite)
6469 {
6470 mch_memmove(p, val, (size_t)len + 1);
6471 /* Put the separator after the NUL. */
6472 p[len + 1] = sep;
6473 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006474 viminfo_history[type][idx].hisnum = 0;
6475 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006476 viminfo_hisidx[type]++;
6477 }
6478 }
6479 }
6480 }
6481 }
6482}
6483
6484/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006485 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006486 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006487 static void
6488concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
6490 int idx;
6491 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006492
6493 idx = hisidx[type] + viminfo_hisidx[type];
6494 if (idx >= hislen)
6495 idx -= hislen;
6496 else if (idx < 0)
6497 idx = hislen - 1;
6498 if (viminfo_add_at_front)
6499 hisidx[type] = idx;
6500 else
6501 {
6502 if (hisidx[type] == -1)
6503 hisidx[type] = hislen - 1;
6504 do
6505 {
6506 if (history[type][idx].hisstr != NULL
6507 || history[type][idx].viminfo)
6508 break;
6509 if (++idx == hislen)
6510 idx = 0;
6511 } while (idx != hisidx[type]);
6512 if (idx != hisidx[type] && --idx < 0)
6513 idx = hislen - 1;
6514 }
6515 for (i = 0; i < viminfo_hisidx[type]; i++)
6516 {
6517 vim_free(history[type][idx].hisstr);
6518 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6519 history[type][idx].viminfo = TRUE;
6520 history[type][idx].time_set = viminfo_history[type][i].time_set;
6521 if (--idx < 0)
6522 idx = hislen - 1;
6523 }
6524 idx += 1;
6525 idx %= hislen;
6526 for (i = 0; i < viminfo_hisidx[type]; i++)
6527 {
6528 history[type][idx++].hisnum = ++hisnum[type];
6529 idx %= hislen;
6530 }
6531}
6532
6533#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6534 static int
6535#ifdef __BORLANDC__
6536_RTLENTRYF
6537#endif
6538sort_hist(const void *s1, const void *s2)
6539{
6540 histentry_T *p1 = *(histentry_T **)s1;
6541 histentry_T *p2 = *(histentry_T **)s2;
6542
6543 if (p1->time_set < p2->time_set) return -1;
6544 if (p1->time_set > p2->time_set) return 1;
6545 return 0;
6546}
6547#endif
6548
6549/*
6550 * Merge history lines from viminfo and lines typed in this Vim based on the
6551 * timestamp;
6552 */
6553 static void
6554merge_history(int type)
6555{
6556 int max_len;
6557 histentry_T **tot_hist;
6558 histentry_T *new_hist;
6559 int i;
6560 int len;
6561
6562 /* Make one long list with all entries. */
6563 max_len = hislen + viminfo_hisidx[type];
6564 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6565 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6566 if (tot_hist == NULL || new_hist == NULL)
6567 {
6568 vim_free(tot_hist);
6569 vim_free(new_hist);
6570 return;
6571 }
6572 for (i = 0; i < viminfo_hisidx[type]; i++)
6573 tot_hist[i] = &viminfo_history[type][i];
6574 len = i;
6575 for (i = 0; i < hislen; i++)
6576 if (history[type][i].hisstr != NULL)
6577 tot_hist[len++] = &history[type][i];
6578
6579 /* Sort the list on timestamp. */
6580 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6581
6582 /* Keep the newest ones. */
6583 for (i = 0; i < hislen; i++)
6584 {
6585 if (i < len)
6586 {
6587 new_hist[i] = *tot_hist[i];
6588 tot_hist[i]->hisstr = NULL;
6589 if (new_hist[i].hisnum == 0)
6590 new_hist[i].hisnum = ++hisnum[type];
6591 }
6592 else
6593 clear_hist_entry(&new_hist[i]);
6594 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006595 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006596
6597 /* Free what is not kept. */
6598 for (i = 0; i < viminfo_hisidx[type]; i++)
6599 vim_free(viminfo_history[type][i].hisstr);
6600 for (i = 0; i < hislen; i++)
6601 vim_free(history[type][i].hisstr);
6602 vim_free(history[type]);
6603 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006604 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006605}
6606
6607/*
6608 * Finish reading history lines from viminfo. Not used when writing viminfo.
6609 */
6610 void
6611finish_viminfo_history(vir_T *virp)
6612{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006614 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
6616 for (type = 0; type < HIST_COUNT; ++type)
6617 {
6618 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006619 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006620
6621 if (merge)
6622 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006624 concat_history(type);
6625
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 vim_free(viminfo_history[type]);
6627 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006628 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 }
6630}
6631
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006632/*
6633 * Write history to viminfo file in "fp".
6634 * When "merge" is TRUE merge history lines with a previously read viminfo
6635 * file, data is in viminfo_history[].
6636 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006639write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640{
6641 int i;
6642 int type;
6643 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006644 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645
6646 init_history();
6647 if (hislen == 0)
6648 return;
6649 for (type = 0; type < HIST_COUNT; ++type)
6650 {
6651 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6652 if (num_saved == 0)
6653 continue;
6654 if (num_saved < 0) /* Use default */
6655 num_saved = hislen;
6656 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6657 type == HIST_CMD ? _("Command Line") :
6658 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006659 type == HIST_EXPR ? _("Expression") :
6660 type == HIST_INPUT ? _("Input Line") :
6661 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 if (num_saved > hislen)
6663 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006664
6665 /*
6666 * Merge typed and viminfo history:
6667 * round 1: history of typed commands.
6668 * round 2: history from recently read viminfo.
6669 */
6670 for (round = 1; round <= 2; ++round)
6671 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006672 if (round == 1)
6673 /* start at newest entry, somewhere in the list */
6674 i = hisidx[type];
6675 else if (viminfo_hisidx[type] > 0)
6676 /* start at newest entry, first in the list */
6677 i = 0;
6678 else
6679 /* empty list */
6680 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006681 if (i >= 0)
6682 while (num_saved > 0
6683 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006685 char_u *p;
6686 time_t timestamp;
6687 int c = NUL;
6688
6689 if (round == 1)
6690 {
6691 p = history[type][i].hisstr;
6692 timestamp = history[type][i].time_set;
6693 }
6694 else
6695 {
6696 p = viminfo_history[type] == NULL ? NULL
6697 : viminfo_history[type][i].hisstr;
6698 timestamp = viminfo_history[type] == NULL ? 0
6699 : viminfo_history[type][i].time_set;
6700 }
6701
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006702 if (p != NULL && (round == 2
6703 || !merge
6704 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006706 --num_saved;
6707 fputc(hist_type2char(type, TRUE), fp);
6708 /* For the search history: put the separator in the
6709 * second column; use a space if there isn't one. */
6710 if (type == HIST_SEARCH)
6711 {
6712 c = p[STRLEN(p) + 1];
6713 putc(c == NUL ? ' ' : c, fp);
6714 }
6715 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006716
6717 {
6718 char cbuf[NUMBUFLEN];
6719
6720 /* New style history with a bar line. Format:
6721 * |{bartype},{histtype},{timestamp},{separator},"text" */
6722 if (c == NUL)
6723 cbuf[0] = NUL;
6724 else
6725 sprintf(cbuf, "%d", c);
6726 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6727 type, (long)timestamp, cbuf);
6728 barline_writestring(fp, p, LSIZE - 20);
6729 putc('\n', fp);
6730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006732 if (round == 1)
6733 {
6734 /* Decrement index, loop around and stop when back at
6735 * the start. */
6736 if (--i < 0)
6737 i = hislen - 1;
6738 if (i == hisidx[type])
6739 break;
6740 }
6741 else
6742 {
6743 /* Increment index. Stop at the end in the while. */
6744 ++i;
6745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006747 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006748 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006749 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006750 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006751 vim_free(viminfo_history[type]);
6752 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006753 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 }
6755}
6756#endif /* FEAT_VIMINFO */
6757
6758#if defined(FEAT_FKMAP) || defined(PROTO)
6759/*
6760 * Write a character at the current cursor+offset position.
6761 * It is directly written into the command buffer block.
6762 */
6763 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006764cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
6766 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6767 {
6768 EMSG(_("E198: cmd_pchar beyond the command length"));
6769 return;
6770 }
6771 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6772 ccline.cmdbuff[ccline.cmdlen] = NUL;
6773}
6774
6775 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006776cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6779 {
6780 /* EMSG(_("cmd_gchar beyond the command length")); */
6781 return NUL;
6782 }
6783 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6784}
6785#endif
6786
6787#if defined(FEAT_CMDWIN) || defined(PROTO)
6788/*
6789 * Open a window on the current command line and history. Allow editing in
6790 * the window. Returns when the window is closed.
6791 * Returns:
6792 * CR if the command is to be executed
6793 * Ctrl_C if it is to be abandoned
6794 * K_IGNORE if editing continues
6795 */
6796 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006797ex_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
6799 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006800 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006802 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 win_T *wp;
6804 int i;
6805 linenr_T lnum;
6806 int histtype;
6807 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006808#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 int save_restart_edit = restart_edit;
6812 int save_State = State;
6813 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006814#ifdef FEAT_RIGHTLEFT
6815 int save_cmdmsg_rl = cmdmsg_rl;
6816#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006817#ifdef FEAT_FOLDING
6818 int save_KeyTyped;
6819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820
6821 /* Can't do this recursively. Can't do it when typing a password. */
6822 if (cmdwin_type != 0
6823# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6824 || cmdline_star > 0
6825# endif
6826 )
6827 {
6828 beep_flush();
6829 return K_IGNORE;
6830 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006831 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832
6833 /* Save current window sizes. */
6834 win_size_save(&winsizes);
6835
6836# ifdef FEAT_AUTOCMD
6837 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006838 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006840 /* don't use a new tab page */
6841 cmdmod.tab = 0;
6842
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 /* Create a window for the command-line buffer. */
6844 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6845 {
6846 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006847# ifdef FEAT_AUTOCMD
6848 unblock_autocmds();
6849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 return K_IGNORE;
6851 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006852 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853
6854 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006855 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006856 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6858 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6859 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006860#ifdef FEAT_FOLDING
6861 curwin->w_p_fen = FALSE;
6862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006864 curwin->w_p_rl = cmdmsg_rl;
6865 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006867 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868
6869# ifdef FEAT_AUTOCMD
6870 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006871 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872# endif
6873
Bram Moolenaar46152342005-09-07 21:18:43 +00006874 /* Showing the prompt may have set need_wait_return, reset it. */
6875 need_wait_return = FALSE;
6876
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006877 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6879 {
6880 if (p_wc == TAB)
6881 {
6882 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6883 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6884 }
6885 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6886 }
6887
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006888 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6889 * sets 'textwidth' to 78). */
6890 curbuf->b_p_tw = 0;
6891
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 /* Fill the buffer with the history. */
6893 init_history();
6894 if (hislen > 0)
6895 {
6896 i = hisidx[histtype];
6897 if (i >= 0)
6898 {
6899 lnum = 0;
6900 do
6901 {
6902 if (++i == hislen)
6903 i = 0;
6904 if (history[histtype][i].hisstr != NULL)
6905 ml_append(lnum++, history[histtype][i].hisstr,
6906 (colnr_T)0, FALSE);
6907 }
6908 while (i != hisidx[histtype]);
6909 }
6910 }
6911
6912 /* Replace the empty last line with the current command-line and put the
6913 * cursor there. */
6914 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6915 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6916 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006917 changed_line_abv_curs();
6918 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006919 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
6921 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01006922 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
6924 /* No Ex mode here! */
6925 exmode_active = 0;
6926
6927 State = NORMAL;
6928# ifdef FEAT_MOUSE
6929 setmouse();
6930# endif
6931
6932# ifdef FEAT_AUTOCMD
6933 /* Trigger CmdwinEnter autocommands. */
6934 typestr[0] = cmdwin_type;
6935 typestr[1] = NUL;
6936 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006937 if (restart_edit != 0) /* autocmd with ":startinsert" */
6938 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939# endif
6940
6941 i = RedrawingDisabled;
6942 RedrawingDisabled = 0;
6943
6944 /*
6945 * Call the main loop until <CR> or CTRL-C is typed.
6946 */
6947 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006948 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
6950 RedrawingDisabled = i;
6951
6952# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006953
6954# ifdef FEAT_FOLDING
6955 save_KeyTyped = KeyTyped;
6956# endif
6957
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 /* Trigger CmdwinLeave autocommands. */
6959 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006960
6961# ifdef FEAT_FOLDING
6962 /* Restore KeyTyped in case it is modified by autocommands */
6963 KeyTyped = save_KeyTyped;
6964# endif
6965
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966# endif
6967
Bram Moolenaarccc18222007-05-10 18:25:20 +00006968 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01006969 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 cmdwin_type = 0;
6971
6972 exmode_active = save_exmode;
6973
Bram Moolenaarf9821062008-06-20 16:31:07 +00006974 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006976 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 {
6978 cmdwin_result = Ctrl_C;
6979 EMSG(_("E199: Active window or buffer deleted"));
6980 }
6981 else
6982 {
6983# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6984 /* autocmds may abort script processing */
6985 if (aborting() && cmdwin_result != K_IGNORE)
6986 cmdwin_result = Ctrl_C;
6987# endif
6988 /* Set the new command line from the cmdline buffer. */
6989 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006990 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006992 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6993
6994 if (histtype == HIST_CMD)
6995 {
6996 /* Execute the command directly. */
6997 ccline.cmdbuff = vim_strsave((char_u *)p);
6998 cmdwin_result = CAR;
6999 }
7000 else
7001 {
7002 /* First need to cancel what we were doing. */
7003 ccline.cmdbuff = NULL;
7004 stuffcharReadbuff(':');
7005 stuffReadbuff((char_u *)p);
7006 stuffcharReadbuff(CAR);
7007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 }
7009 else if (cmdwin_result == K_XF2) /* :qa typed */
7010 {
7011 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7012 cmdwin_result = CAR;
7013 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007014 else if (cmdwin_result == Ctrl_C)
7015 {
7016 /* :q or :close, don't execute any command
7017 * and don't modify the cmd window. */
7018 ccline.cmdbuff = NULL;
7019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 else
7021 ccline.cmdbuff = vim_strsave(ml_get_curline());
7022 if (ccline.cmdbuff == NULL)
7023 cmdwin_result = Ctrl_C;
7024 else
7025 {
7026 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7027 ccline.cmdbufflen = ccline.cmdlen + 1;
7028 ccline.cmdpos = curwin->w_cursor.col;
7029 if (ccline.cmdpos > ccline.cmdlen)
7030 ccline.cmdpos = ccline.cmdlen;
7031 if (cmdwin_result == K_IGNORE)
7032 {
7033 set_cmdspos_cursor();
7034 redrawcmd();
7035 }
7036 }
7037
7038# ifdef FEAT_AUTOCMD
7039 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007040 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007042# ifdef FEAT_CONCEAL
7043 /* Avoid command-line window first character being concealed. */
7044 curwin->w_p_cole = 0;
7045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007047 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 win_goto(old_curwin);
7049 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007050
7051 /* win_close() may have already wiped the buffer when 'bh' is
7052 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007053 if (bufref_valid(&bufref))
7054 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
7056 /* Restore window sizes. */
7057 win_size_restore(&winsizes);
7058
7059# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007060 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061# endif
7062 }
7063
7064 ga_clear(&winsizes);
7065 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007066# ifdef FEAT_RIGHTLEFT
7067 cmdmsg_rl = save_cmdmsg_rl;
7068# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069
7070 State = save_State;
7071# ifdef FEAT_MOUSE
7072 setmouse();
7073# endif
7074
7075 return cmdwin_result;
7076}
7077#endif /* FEAT_CMDWIN */
7078
7079/*
7080 * Used for commands that either take a simple command string argument, or:
7081 * cmd << endmarker
7082 * {script}
7083 * endmarker
7084 * Returns a pointer to allocated memory with {script} or NULL.
7085 */
7086 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007087script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088{
7089 char_u *theline;
7090 char *end_pattern = NULL;
7091 char dot[] = ".";
7092 garray_T ga;
7093
7094 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7095 return NULL;
7096
7097 ga_init2(&ga, 1, 0x400);
7098
7099 if (cmd[2] != NUL)
7100 end_pattern = (char *)skipwhite(cmd + 2);
7101 else
7102 end_pattern = dot;
7103
7104 for (;;)
7105 {
7106 theline = eap->getline(
7107#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007108 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109#endif
7110 NUL, eap->cookie, 0);
7111
7112 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007113 {
7114 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
7118 ga_concat(&ga, theline);
7119 ga_append(&ga, '\n');
7120 vim_free(theline);
7121 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007122 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123
7124 return (char_u *)ga.ga_data;
7125}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007126
7127#ifdef FEAT_SEARCH_EXTRA
7128 static void
7129set_search_match(pos_T *t)
7130{
7131 /*
7132 * First move cursor to end of match, then to the start. This
7133 * moves the whole match onto the screen when 'nowrap' is set.
7134 */
7135 t->lnum += search_match_lines;
7136 t->col = search_match_endcol;
7137 if (t->lnum > curbuf->b_ml.ml_line_count)
7138 {
7139 t->lnum = curbuf->b_ml.ml_line_count;
7140 coladvance((colnr_T)MAXCOL);
7141 }
7142}
7143#endif