blob: 894756da27db19d7cff104afdf465d8257256bb2 [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
Bram Moolenaara92522f2017-07-15 15:21:38 +020055static int extra_char = NUL; /* extra character to display when redrawing
56 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020057static int extra_char_shift;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_CMDHIST
60typedef struct hist_entry
61{
62 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020063 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020065 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000066} histentry_T;
67
68static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
69static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
70static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
71 /* identifying (unique) number of newest history entry */
72static int hislen = 0; /* actual length of history tables */
73
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079# endif
80#endif
81
82#ifdef FEAT_RIGHTLEFT
83static int cmd_hkmap = 0; /* Hebrew mapping during command line */
84#endif
85
86#ifdef FEAT_FKMAP
87static int cmd_fkmap = 0; /* Farsi mapping during command line */
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int cmdline_charsize(int idx);
91static void set_cmdspos(void);
92static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void alloc_cmdbuff(int len);
97static int realloc_cmdbuff(int len);
98static void draw_cmdline(int start, int len);
99static void save_cmdline(struct cmdline_info *ccp);
100static void restore_cmdline(struct cmdline_info *ccp);
101static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100106static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static void redrawcmdprompt(void);
109static void cursorcmd(void);
110static int ccheck_abbr(int);
111static int nextwild(expand_T *xp, int type, int options, int escape);
112static void escape_fname(char_u **pp);
113static int showmatches(expand_T *xp, int wildmenu);
114static void set_expand_context(expand_T *xp);
115static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
116static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100119static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100120static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200121# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100125static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
126static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200129#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200134static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
136
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
138static int
139#ifdef __BORLANDC__
140_RTLENTRYF
141#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100142sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143#endif
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200144#ifdef FEAT_SEARCH_EXTRA
145static void set_search_match(pos_T *t);
146#endif
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200147
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148/*
149 * getcmdline() - accept a command line starting with firstc.
150 *
151 * firstc == ':' get ":" command line.
152 * firstc == '/' or '?' get search pattern
153 * firstc == '=' get expression
154 * firstc == '@' get text for input() function
155 * firstc == '>' get text for debug mode
156 * firstc == NUL get text for :insert command
157 * firstc == -1 like NUL, and break on CTRL-C
158 *
159 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
160 * command line.
161 *
162 * Careful: getcmdline() can be called recursively!
163 *
164 * Return pointer to allocated string if there is a commandline, NULL
165 * otherwise.
166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100168getcmdline(
169 int firstc,
170 long count UNUSED, /* only used for incremental search */
171 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172{
173 int c;
174 int i;
175 int j;
176 int gotesc = FALSE; /* TRUE when <ESC> just typed */
177 int do_abbr; /* when TRUE check for abbr. */
178#ifdef FEAT_CMDHIST
179 char_u *lookfor = NULL; /* string to match */
180 int hiscnt; /* current history line in use */
181 int histype; /* history type to be used */
182#endif
183#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200184 pos_T search_start; /* where 'incsearch' starts searching */
185 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 colnr_T old_curswant;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200187 colnr_T init_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 colnr_T old_leftcol;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200189 colnr_T init_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 linenr_T old_topline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200191 linenr_T init_topline = curwin->w_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200192 pos_T match_start = curwin->w_cursor;
193 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194# ifdef FEAT_DIFF
195 int old_topfill;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200196 int init_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197# endif
198 linenr_T old_botline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200199 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 int did_incsearch = FALSE;
201 int incsearch_postponed = FALSE;
202#endif
203 int did_wild_list = FALSE; /* did wild_list() recently */
204 int wim_index = 0; /* index in wim_flags[] */
205 int res;
206 int save_msg_scroll = msg_scroll;
207 int save_State = State; /* remember State when called */
208 int some_key_typed = FALSE; /* one of the keys was typed */
209#ifdef FEAT_MOUSE
210 /* mouse drag and release events are ignored, unless they are
211 * preceded with a mouse down event */
212 int ignore_drag_release = TRUE;
213#endif
214#ifdef FEAT_EVAL
215 int break_ctrl_c = FALSE;
216#endif
217 expand_T xpc;
218 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100219#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000220 /* Everything that may work recursively should save and restore the
221 * current command line in save_ccline. That includes update_screen(), a
222 * custom status line may invoke ":normal". */
223 struct cmdline_info save_ccline;
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#ifdef FEAT_EVAL
227 if (firstc == -1)
228 {
229 firstc = NUL;
230 break_ctrl_c = TRUE;
231 }
232#endif
233#ifdef FEAT_RIGHTLEFT
234 /* start without Hebrew mapping for a command line */
235 if (firstc == ':' || firstc == '=' || firstc == '>')
236 cmd_hkmap = 0;
237#endif
238
239 ccline.overstrike = FALSE; /* always start in insert mode */
240#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100241 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200242 save_cursor = curwin->w_cursor; /* may be restored later */
243 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 old_curswant = curwin->w_curswant;
245 old_leftcol = curwin->w_leftcol;
246 old_topline = curwin->w_topline;
247# ifdef FEAT_DIFF
248 old_topfill = curwin->w_topfill;
249# endif
250 old_botline = curwin->w_botline;
251#endif
252
253 /*
254 * set some variables for redrawcmd()
255 */
256 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000257 ccline.cmdindent = (firstc > 0 ? indent : 0);
258
259 /* alloc initial ccline.cmdbuff */
260 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261 if (ccline.cmdbuff == NULL)
262 return NULL; /* out of memory */
263 ccline.cmdlen = ccline.cmdpos = 0;
264 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100265 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000267 /* autoindent for :insert and :append */
268 if (firstc <= 0)
269 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200270 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000271 ccline.cmdbuff[indent] = NUL;
272 ccline.cmdpos = indent;
273 ccline.cmdspos = indent;
274 ccline.cmdlen = indent;
275 }
276
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000278 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280#ifdef FEAT_RIGHTLEFT
281 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
282 && (firstc == '/' || firstc == '?'))
283 cmdmsg_rl = TRUE;
284 else
285 cmdmsg_rl = FALSE;
286#endif
287
288 redir_off = TRUE; /* don't redirect the typed command */
289 if (!cmd_silent)
290 {
291 i = msg_scrolled;
292 msg_scrolled = 0; /* avoid wait_return message */
293 gotocmdline(TRUE);
294 msg_scrolled += i;
295 redrawcmdprompt(); /* draw prompt or indent */
296 set_cmdspos();
297 }
298 xpc.xp_context = EXPAND_NOTHING;
299 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000300#ifndef BACKSLASH_IN_FILENAME
301 xpc.xp_shell = FALSE;
302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000304#if defined(FEAT_EVAL)
305 if (ccline.input_fn)
306 {
307 xpc.xp_context = ccline.xp_context;
308 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000309# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000310 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000311# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000312 }
313#endif
314
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 /*
316 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
317 * doing ":@0" when register 0 doesn't contain a CR.
318 */
319 msg_scroll = FALSE;
320
321 State = CMDLINE;
322
323 if (firstc == '/' || firstc == '?' || firstc == '@')
324 {
325 /* Use ":lmap" mappings for search pattern and input(). */
326 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
327 b_im_ptr = &curbuf->b_p_iminsert;
328 else
329 b_im_ptr = &curbuf->b_p_imsearch;
330 if (*b_im_ptr == B_IMODE_LMAP)
331 State |= LANGMAP;
332#ifdef USE_IM_CONTROL
333 im_set_active(*b_im_ptr == B_IMODE_IM);
334#endif
335 }
336#ifdef USE_IM_CONTROL
337 else if (p_imcmdline)
338 im_set_active(TRUE);
339#endif
340
341#ifdef FEAT_MOUSE
342 setmouse();
343#endif
344#ifdef CURSOR_SHAPE
345 ui_cursor_shape(); /* may show different cursor shape */
346#endif
347
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000348 /* When inside an autocommand for writing "exiting" may be set and
349 * terminal mode set to cooked. Need to set raw mode here then. */
350 settmode(TMODE_RAW);
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef FEAT_CMDHIST
353 init_history();
354 hiscnt = hislen; /* set hiscnt to impossible history value */
355 histype = hist_char2type(firstc);
356#endif
357
358#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000359 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#endif
361
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200362 /* If something above caused an error, reset the flags, we do want to type
363 * and execute commands. Display may be messed up a bit. */
364 if (did_emsg)
365 redrawcmd();
366 did_emsg = FALSE;
367 got_int = FALSE;
368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 /*
370 * Collect the command string, handling editing keys.
371 */
372 for (;;)
373 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000374 redir_off = TRUE; /* Don't redirect the typed command.
375 Repeated, because a ":redir" inside
376 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377#ifdef USE_ON_FLY_SCROLL
378 dont_scroll = FALSE; /* allow scrolling here */
379#endif
380 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
381
382 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000383
384 /* Get a character. Ignore K_IGNORE, it should not do anything, such
385 * as stop completion. */
386 do
387 {
388 c = safe_vgetc();
389 } while (c == K_IGNORE);
390
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 if (KeyTyped)
392 {
393 some_key_typed = TRUE;
394#ifdef FEAT_RIGHTLEFT
395 if (cmd_hkmap)
396 c = hkmap(c);
397# ifdef FEAT_FKMAP
398 if (cmd_fkmap)
399 c = cmdl_fkmap(c);
400# endif
401 if (cmdmsg_rl && !KeyStuffed)
402 {
403 /* Invert horizontal movements and operations. Only when
404 * typed by the user directly, not when the result of a
405 * mapping. */
406 switch (c)
407 {
408 case K_RIGHT: c = K_LEFT; break;
409 case K_S_RIGHT: c = K_S_LEFT; break;
410 case K_C_RIGHT: c = K_C_LEFT; break;
411 case K_LEFT: c = K_RIGHT; break;
412 case K_S_LEFT: c = K_S_RIGHT; break;
413 case K_C_LEFT: c = K_C_RIGHT; break;
414 }
415 }
416#endif
417 }
418
419 /*
420 * Ignore got_int when CTRL-C was typed here.
421 * Don't ignore it in :global, we really need to break then, e.g., for
422 * ":g/pat/normal /pat" (without the <CR>).
423 * Don't ignore it for the input() function.
424 */
425 if ((c == Ctrl_C
426#ifdef UNIX
427 || c == intr_char
428#endif
429 )
430#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
431 && firstc != '@'
432#endif
433#ifdef FEAT_EVAL
434 && !break_ctrl_c
435#endif
436 && !global_busy)
437 got_int = FALSE;
438
439#ifdef FEAT_CMDHIST
440 /* free old command line when finished moving around in the history
441 * list */
442 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000443 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000444 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 && c != K_PAGEDOWN && c != K_PAGEUP
446 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000447 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
449 {
450 vim_free(lookfor);
451 lookfor = NULL;
452 }
453#endif
454
455 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000456 * When there are matching completions to select <S-Tab> works like
457 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000459 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 c = Ctrl_P;
461
462#ifdef FEAT_WILDMENU
463 /* Special translations for 'wildmenu' */
464 if (did_wild_list && p_wmnu)
465 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000466 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000468 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 c = Ctrl_N;
470 }
471 /* Hitting CR after "emenu Name.": complete submenu */
472 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
473 && ccline.cmdpos > 1
474 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
475 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
476 && (c == '\n' || c == '\r' || c == K_KENTER))
477 c = K_DOWN;
478#endif
479
480 /* free expanded names when finished walking through matches */
481 if (xpc.xp_numfiles != -1
482 && !(c == p_wc && KeyTyped) && c != p_wcm
483 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
484 && c != Ctrl_L)
485 {
486 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
487 did_wild_list = FALSE;
488#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000489 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490#endif
491 xpc.xp_context = EXPAND_NOTHING;
492 wim_index = 0;
493#ifdef FEAT_WILDMENU
494 if (p_wmnu && wild_menu_showing != 0)
495 {
496 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000497 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000498
499 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000500 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
502 if (wild_menu_showing == WM_SCROLLED)
503 {
504 /* Entered command line, move it up */
505 cmdline_row--;
506 redrawcmd();
507 }
508 else if (save_p_ls != -1)
509 {
510 /* restore 'laststatus' and 'winminheight' */
511 p_ls = save_p_ls;
512 p_wmh = save_p_wmh;
513 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000514 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000516 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 redrawcmd();
518 save_p_ls = -1;
519 }
520 else
521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 redraw_statuslines();
524 }
525 KeyTyped = skt;
526 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000527 if (ccline.input_fn)
528 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 }
530#endif
531 }
532
533#ifdef FEAT_WILDMENU
534 /* Special translations for 'wildmenu' */
535 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
536 {
537 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000538 if (c == K_DOWN && ccline.cmdpos > 0
539 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000541 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 {
543 /* Hitting <Up>: Remove one submenu name in front of the
544 * cursor */
545 int found = FALSE;
546
547 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
548 i = 0;
549 while (--j > 0)
550 {
551 /* check for start of menu name */
552 if (ccline.cmdbuff[j] == ' '
553 && ccline.cmdbuff[j - 1] != '\\')
554 {
555 i = j + 1;
556 break;
557 }
558 /* check for start of submenu name */
559 if (ccline.cmdbuff[j] == '.'
560 && ccline.cmdbuff[j - 1] != '\\')
561 {
562 if (found)
563 {
564 i = j + 1;
565 break;
566 }
567 else
568 found = TRUE;
569 }
570 }
571 if (i > 0)
572 cmdline_del(i);
573 c = p_wc;
574 xpc.xp_context = EXPAND_NOTHING;
575 }
576 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000577 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000578 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000579 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
581 char_u upseg[5];
582
583 upseg[0] = PATHSEP;
584 upseg[1] = '.';
585 upseg[2] = '.';
586 upseg[3] = PATHSEP;
587 upseg[4] = NUL;
588
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000589 if (c == K_DOWN
590 && ccline.cmdpos > 0
591 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
592 && (ccline.cmdpos < 3
593 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
595 {
596 /* go down a directory */
597 c = p_wc;
598 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000599 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 {
601 /* If in a direct ancestor, strip off one ../ to go down */
602 int found = FALSE;
603
604 j = ccline.cmdpos;
605 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
606 while (--j > i)
607 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000608#ifdef FEAT_MBYTE
609 if (has_mbyte)
610 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (vim_ispathsep(ccline.cmdbuff[j]))
613 {
614 found = TRUE;
615 break;
616 }
617 }
618 if (found
619 && ccline.cmdbuff[j - 1] == '.'
620 && ccline.cmdbuff[j - 2] == '.'
621 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
622 {
623 cmdline_del(j - 2);
624 c = p_wc;
625 }
626 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000627 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
629 /* go up a directory */
630 int found = FALSE;
631
632 j = ccline.cmdpos - 1;
633 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
634 while (--j > i)
635 {
636#ifdef FEAT_MBYTE
637 if (has_mbyte)
638 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
639#endif
640 if (vim_ispathsep(ccline.cmdbuff[j])
641#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100642 && vim_strchr((char_u *)" *?[{`$%#",
643 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#endif
645 )
646 {
647 if (found)
648 {
649 i = j + 1;
650 break;
651 }
652 else
653 found = TRUE;
654 }
655 }
656
657 if (!found)
658 j = i;
659 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
660 j += 4;
661 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
662 && j == i)
663 j += 3;
664 else
665 j = 0;
666 if (j > 0)
667 {
668 /* TODO this is only for DOS/UNIX systems - need to put in
669 * machine-specific stuff here and in upseg init */
670 cmdline_del(j);
671 put_on_cmdline(upseg + 1, 3, FALSE);
672 }
673 else if (ccline.cmdpos > i)
674 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100675
676 /* Now complete in the new directory. Set KeyTyped in case the
677 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100679 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 }
681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
683#endif /* FEAT_WILDMENU */
684
685 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
686 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
687 if (c == Ctrl_BSL)
688 {
689 ++no_mapping;
690 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000691 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 --no_mapping;
693 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200694 /* CTRL-\ e doesn't work when obtaining an expression, unless it
695 * is in a mapping. */
696 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
697 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
699 vungetc(c);
700 c = Ctrl_BSL;
701 }
702#ifdef FEAT_EVAL
703 else if (c == 'e')
704 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200705 char_u *p = NULL;
706 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
708 /*
709 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000710 * Need to save and restore the current command line, to be
711 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 */
713 if (ccline.cmdpos == ccline.cmdlen)
714 new_cmdpos = 99999; /* keep it at the end */
715 else
716 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000717
718 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000720 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 if (c == '=')
722 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000723 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000724 * to avoid nasty things like going to another buffer when
725 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000726 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000727 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000729 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000730 restore_cmdline(&save_ccline);
731
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200732 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200734 len = (int)STRLEN(p);
735 if (realloc_cmdbuff(len + 1) == OK)
736 {
737 ccline.cmdlen = len;
738 STRCPY(ccline.cmdbuff, p);
739 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200741 /* Restore the cursor or use the position set with
742 * set_cmdline_pos(). */
743 if (new_cmdpos > ccline.cmdlen)
744 ccline.cmdpos = ccline.cmdlen;
745 else
746 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200748 KeyTyped = FALSE; /* Don't do p_wc completion. */
749 redrawcmd();
750 goto cmdline_changed;
751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
753 }
754 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100755 got_int = FALSE; /* don't abandon the command line */
756 did_emsg = FALSE;
757 emsg_on_display = FALSE;
758 redrawcmd();
759 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 }
761#endif
762 else
763 {
764 if (c == Ctrl_G && p_im && restart_edit == 0)
765 restart_edit = 'a';
766 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
767 in history */
768 goto returncmd; /* back to Normal mode */
769 }
770 }
771
772#ifdef FEAT_CMDWIN
773 if (c == cedit_key || c == K_CMDWIN)
774 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200775 if (ex_normal_busy == 0 && got_int == FALSE)
776 {
777 /*
778 * Open a window to edit the command line (and history).
779 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200780 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200781 some_key_typed = TRUE;
782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784# ifdef FEAT_DIGRAPHS
785 else
786# endif
787#endif
788#ifdef FEAT_DIGRAPHS
789 c = do_digraph(c);
790#endif
791
792 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
793 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
794 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000795 /* In Ex mode a backslash escapes a newline. */
796 if (exmode_active
797 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000798 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000799 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000800 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000802 if (c == K_KENTER)
803 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000805 else
806 {
807 gotesc = FALSE; /* Might have typed ESC previously, don't
808 truncate the cmdline now. */
809 if (ccheck_abbr(c + ABBR_OFF))
810 goto cmdline_changed;
811 if (!cmd_silent)
812 {
813 windgoto(msg_row, 0);
814 out_flush();
815 }
816 break;
817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 }
819
820 /*
821 * Completion for 'wildchar' or 'wildcharm' key.
822 * - hitting <ESC> twice means: abandon command line.
823 * - wildcard expansion is only done when the 'wildchar' key is really
824 * typed, not when it comes from a macro
825 */
826 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
827 {
828 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
829 {
830 /* if 'wildmode' contains "list" may still need to list */
831 if (xpc.xp_numfiles > 1
832 && !did_wild_list
833 && (wim_flags[wim_index] & WIM_LIST))
834 {
835 (void)showmatches(&xpc, FALSE);
836 redrawcmd();
837 did_wild_list = TRUE;
838 }
839 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100840 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
841 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100843 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
844 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 else
846 res = OK; /* don't insert 'wildchar' now */
847 }
848 else /* typed p_wc first time */
849 {
850 wim_index = 0;
851 j = ccline.cmdpos;
852 /* if 'wildmode' first contains "longest", get longest
853 * common part */
854 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100855 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
856 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100858 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
859 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
861 /* if interrupted while completing, behave like it failed */
862 if (got_int)
863 {
864 (void)vpeekc(); /* remove <C-C> from input stream */
865 got_int = FALSE; /* don't abandon the command line */
866 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
867#ifdef FEAT_WILDMENU
868 xpc.xp_context = EXPAND_NOTHING;
869#endif
870 goto cmdline_changed;
871 }
872
873 /* when more than one match, and 'wildmode' first contains
874 * "list", or no change and 'wildmode' contains "longest,list",
875 * list all matches */
876 if (res == OK && xpc.xp_numfiles > 1)
877 {
878 /* a "longest" that didn't do anything is skipped (but not
879 * "list:longest") */
880 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
881 wim_index = 1;
882 if ((wim_flags[wim_index] & WIM_LIST)
883#ifdef FEAT_WILDMENU
884 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
885#endif
886 )
887 {
888 if (!(wim_flags[0] & WIM_LONGEST))
889 {
890#ifdef FEAT_WILDMENU
891 int p_wmnu_save = p_wmnu;
892 p_wmnu = 0;
893#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100894 /* remove match */
895 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896#ifdef FEAT_WILDMENU
897 p_wmnu = p_wmnu_save;
898#endif
899 }
900#ifdef FEAT_WILDMENU
901 (void)showmatches(&xpc, p_wmnu
902 && ((wim_flags[wim_index] & WIM_LIST) == 0));
903#else
904 (void)showmatches(&xpc, FALSE);
905#endif
906 redrawcmd();
907 did_wild_list = TRUE;
908 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100909 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
910 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100912 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
913 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
915 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200916 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918#ifdef FEAT_WILDMENU
919 else if (xpc.xp_numfiles == -1)
920 xpc.xp_context = EXPAND_NOTHING;
921#endif
922 }
923 if (wim_index < 3)
924 ++wim_index;
925 if (c == ESC)
926 gotesc = TRUE;
927 if (res == OK)
928 goto cmdline_changed;
929 }
930
931 gotesc = FALSE;
932
933 /* <S-Tab> goes to last match, in a clumsy way */
934 if (c == K_S_TAB && KeyTyped)
935 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100936 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
937 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
938 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 goto cmdline_changed;
940 }
941
942 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
943 c = NL;
944
945 do_abbr = TRUE; /* default: check for abbreviation */
946
947 /*
948 * Big switch for a typed command line character.
949 */
950 switch (c)
951 {
952 case K_BS:
953 case Ctrl_H:
954 case K_DEL:
955 case K_KDEL:
956 case Ctrl_W:
957#ifdef FEAT_FKMAP
958 if (cmd_fkmap && c == K_BS)
959 c = K_DEL;
960#endif
961 if (c == K_KDEL)
962 c = K_DEL;
963
964 /*
965 * delete current character is the same as backspace on next
966 * character, except at end of line
967 */
968 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
969 ++ccline.cmdpos;
970#ifdef FEAT_MBYTE
971 if (has_mbyte && c == K_DEL)
972 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
973 ccline.cmdbuff + ccline.cmdpos);
974#endif
975 if (ccline.cmdpos > 0)
976 {
977 char_u *p;
978
979 j = ccline.cmdpos;
980 p = ccline.cmdbuff + j;
981#ifdef FEAT_MBYTE
982 if (has_mbyte)
983 {
984 p = mb_prevptr(ccline.cmdbuff, p);
985 if (c == Ctrl_W)
986 {
987 while (p > ccline.cmdbuff && vim_isspace(*p))
988 p = mb_prevptr(ccline.cmdbuff, p);
989 i = mb_get_class(p);
990 while (p > ccline.cmdbuff && mb_get_class(p) == i)
991 p = mb_prevptr(ccline.cmdbuff, p);
992 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000993 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
995 }
996 else
997#endif
998 if (c == Ctrl_W)
999 {
1000 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1001 --p;
1002 i = vim_iswordc(p[-1]);
1003 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1004 && vim_iswordc(p[-1]) == i)
1005 --p;
1006 }
1007 else
1008 --p;
1009 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1010 ccline.cmdlen -= j - ccline.cmdpos;
1011 i = ccline.cmdpos;
1012 while (i < ccline.cmdlen)
1013 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1014
1015 /* Truncate at the end, required for multi-byte chars. */
1016 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001017#ifdef FEAT_SEARCH_EXTRA
1018 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001019 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001020 search_start = save_cursor;
1021 /* save view settings, so that the screen
1022 * won't be restored at the wrong position */
1023 old_curswant = init_curswant;
1024 old_leftcol = init_leftcol;
1025 old_topline = init_topline;
1026# ifdef FEAT_DIFF
1027 old_topfill = init_topfill;
1028# endif
1029 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001030 }
1031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 redrawcmd();
1033 }
1034 else if (ccline.cmdlen == 0 && c != Ctrl_W
1035 && ccline.cmdprompt == NULL && indent == 0)
1036 {
1037 /* In ex and debug mode it doesn't make sense to return. */
1038 if (exmode_active
1039#ifdef FEAT_EVAL
1040 || ccline.cmdfirstc == '>'
1041#endif
1042 )
1043 goto cmdline_not_changed;
1044
1045 vim_free(ccline.cmdbuff); /* no commandline to return */
1046 ccline.cmdbuff = NULL;
1047 if (!cmd_silent)
1048 {
1049#ifdef FEAT_RIGHTLEFT
1050 if (cmdmsg_rl)
1051 msg_col = Columns;
1052 else
1053#endif
1054 msg_col = 0;
1055 msg_putchar(' '); /* delete ':' */
1056 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001057#ifdef FEAT_SEARCH_EXTRA
1058 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001059 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 redraw_cmdline = TRUE;
1062 goto returncmd; /* back to cmd mode */
1063 }
1064 goto cmdline_changed;
1065
1066 case K_INS:
1067 case K_KINS:
1068#ifdef FEAT_FKMAP
1069 /* if Farsi mode set, we are in reverse insert mode -
1070 Do not change the mode */
1071 if (cmd_fkmap)
1072 beep_flush();
1073 else
1074#endif
1075 ccline.overstrike = !ccline.overstrike;
1076#ifdef CURSOR_SHAPE
1077 ui_cursor_shape(); /* may show different cursor shape */
1078#endif
1079 goto cmdline_not_changed;
1080
1081 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001082 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 /* ":lmap" mappings exists, toggle use of mappings. */
1085 State ^= LANGMAP;
1086#ifdef USE_IM_CONTROL
1087 im_set_active(FALSE); /* Disable input method */
1088#endif
1089 if (b_im_ptr != NULL)
1090 {
1091 if (State & LANGMAP)
1092 *b_im_ptr = B_IMODE_LMAP;
1093 else
1094 *b_im_ptr = B_IMODE_NONE;
1095 }
1096 }
1097#ifdef USE_IM_CONTROL
1098 else
1099 {
1100 /* There are no ":lmap" mappings, toggle IM. When
1101 * 'imdisable' is set don't try getting the status, it's
1102 * always off. */
1103 if ((p_imdisable && b_im_ptr != NULL)
1104 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1105 {
1106 im_set_active(FALSE); /* Disable input method */
1107 if (b_im_ptr != NULL)
1108 *b_im_ptr = B_IMODE_NONE;
1109 }
1110 else
1111 {
1112 im_set_active(TRUE); /* Enable input method */
1113 if (b_im_ptr != NULL)
1114 *b_im_ptr = B_IMODE_IM;
1115 }
1116 }
1117#endif
1118 if (b_im_ptr != NULL)
1119 {
1120 if (b_im_ptr == &curbuf->b_p_iminsert)
1121 set_iminsert_global();
1122 else
1123 set_imsearch_global();
1124 }
1125#ifdef CURSOR_SHAPE
1126 ui_cursor_shape(); /* may show different cursor shape */
1127#endif
1128#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1129 /* Show/unshow value of 'keymap' in status lines later. */
1130 status_redraw_curbuf();
1131#endif
1132 goto cmdline_not_changed;
1133
1134/* case '@': only in very old vi */
1135 case Ctrl_U:
1136 /* delete all characters left of the cursor */
1137 j = ccline.cmdpos;
1138 ccline.cmdlen -= j;
1139 i = ccline.cmdpos = 0;
1140 while (i < ccline.cmdlen)
1141 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1142 /* Truncate at the end, required for multi-byte chars. */
1143 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001144#ifdef FEAT_SEARCH_EXTRA
1145 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001146 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 redrawcmd();
1149 goto cmdline_changed;
1150
1151#ifdef FEAT_CLIPBOARD
1152 case Ctrl_Y:
1153 /* Copy the modeless selection, if there is one. */
1154 if (clip_star.state != SELECT_CLEARED)
1155 {
1156 if (clip_star.state == SELECT_DONE)
1157 clip_copy_modeless_selection(TRUE);
1158 goto cmdline_not_changed;
1159 }
1160 break;
1161#endif
1162
1163 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1164 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001165 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001166 * ":normal" runs out of characters. */
1167 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001168 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 goto cmdline_not_changed;
1170
1171 gotesc = TRUE; /* will free ccline.cmdbuff after
1172 putting it in history */
1173 goto returncmd; /* back to cmd mode */
1174
1175 case Ctrl_R: /* insert register */
1176#ifdef USE_ON_FLY_SCROLL
1177 dont_scroll = TRUE; /* disallow scrolling here */
1178#endif
1179 putcmdline('"', TRUE);
1180 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001181 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 if (i == Ctrl_O)
1183 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1184 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001185 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001186 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 --no_mapping;
1188#ifdef FEAT_EVAL
1189 /*
1190 * Insert the result of an expression.
1191 * Need to save the current command line, to be able to enter
1192 * a new one...
1193 */
1194 new_cmdpos = -1;
1195 if (c == '=')
1196 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1198 {
1199 beep_flush();
1200 c = ESC;
1201 }
1202 else
1203 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001204 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001206 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
1208 }
1209#endif
1210 if (c != ESC) /* use ESC to cancel inserting register */
1211 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001212 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001213
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001214#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001215 /* When there was a serious error abort getting the
1216 * command line. */
1217 if (aborting())
1218 {
1219 gotesc = TRUE; /* will free ccline.cmdbuff after
1220 putting it in history */
1221 goto returncmd; /* back to cmd mode */
1222 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 KeyTyped = FALSE; /* Don't do p_wc completion. */
1225#ifdef FEAT_EVAL
1226 if (new_cmdpos >= 0)
1227 {
1228 /* set_cmdline_pos() was used */
1229 if (new_cmdpos > ccline.cmdlen)
1230 ccline.cmdpos = ccline.cmdlen;
1231 else
1232 ccline.cmdpos = new_cmdpos;
1233 }
1234#endif
1235 }
1236 redrawcmd();
1237 goto cmdline_changed;
1238
1239 case Ctrl_D:
1240 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1241 break; /* Use ^D as normal char instead */
1242
1243 redrawcmd();
1244 continue; /* don't do incremental search now */
1245
1246 case K_RIGHT:
1247 case K_S_RIGHT:
1248 case K_C_RIGHT:
1249 do
1250 {
1251 if (ccline.cmdpos >= ccline.cmdlen)
1252 break;
1253 i = cmdline_charsize(ccline.cmdpos);
1254 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1255 break;
1256 ccline.cmdspos += i;
1257#ifdef FEAT_MBYTE
1258 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001259 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 + ccline.cmdpos);
1261 else
1262#endif
1263 ++ccline.cmdpos;
1264 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001265 while ((c == K_S_RIGHT || c == K_C_RIGHT
1266 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1268#ifdef FEAT_MBYTE
1269 if (has_mbyte)
1270 set_cmdspos_cursor();
1271#endif
1272 goto cmdline_not_changed;
1273
1274 case K_LEFT:
1275 case K_S_LEFT:
1276 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001277 if (ccline.cmdpos == 0)
1278 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 do
1280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 --ccline.cmdpos;
1282#ifdef FEAT_MBYTE
1283 if (has_mbyte) /* move to first byte of char */
1284 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1285 ccline.cmdbuff + ccline.cmdpos);
1286#endif
1287 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1288 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001289 while (ccline.cmdpos > 0
1290 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001291 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1293#ifdef FEAT_MBYTE
1294 if (has_mbyte)
1295 set_cmdspos_cursor();
1296#endif
1297 goto cmdline_not_changed;
1298
1299 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001300 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001301 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302
Bram Moolenaar4770d092006-01-12 23:22:24 +00001303#ifdef FEAT_GUI_W32
1304 /* On Win32 ignore <M-F4>, we get it when closing the window was
1305 * cancelled. */
1306 case K_F4:
1307 if (mod_mask == MOD_MASK_ALT)
1308 {
1309 redrawcmd(); /* somehow the cmdline is cleared */
1310 goto cmdline_not_changed;
1311 }
1312 break;
1313#endif
1314
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315#ifdef FEAT_MOUSE
1316 case K_MIDDLEDRAG:
1317 case K_MIDDLERELEASE:
1318 goto cmdline_not_changed; /* Ignore mouse */
1319
1320 case K_MIDDLEMOUSE:
1321# ifdef FEAT_GUI
1322 /* When GUI is active, also paste when 'mouse' is empty */
1323 if (!gui.in_use)
1324# endif
1325 if (!mouse_has(MOUSE_COMMAND))
1326 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001327# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001329 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001331# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001332 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 redrawcmd();
1334 goto cmdline_changed;
1335
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001336# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001338 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 redrawcmd();
1340 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 case K_LEFTDRAG:
1344 case K_LEFTRELEASE:
1345 case K_RIGHTDRAG:
1346 case K_RIGHTRELEASE:
1347 /* Ignore drag and release events when the button-down wasn't
1348 * seen before. */
1349 if (ignore_drag_release)
1350 goto cmdline_not_changed;
1351 /* FALLTHROUGH */
1352 case K_LEFTMOUSE:
1353 case K_RIGHTMOUSE:
1354 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1355 ignore_drag_release = TRUE;
1356 else
1357 ignore_drag_release = FALSE;
1358# ifdef FEAT_GUI
1359 /* When GUI is active, also move when 'mouse' is empty */
1360 if (!gui.in_use)
1361# endif
1362 if (!mouse_has(MOUSE_COMMAND))
1363 goto cmdline_not_changed; /* Ignore mouse */
1364# ifdef FEAT_CLIPBOARD
1365 if (mouse_row < cmdline_row && clip_star.available)
1366 {
1367 int button, is_click, is_drag;
1368
1369 /*
1370 * Handle modeless selection.
1371 */
1372 button = get_mouse_button(KEY2TERMCAP1(c),
1373 &is_click, &is_drag);
1374 if (mouse_model_popup() && button == MOUSE_LEFT
1375 && (mod_mask & MOD_MASK_SHIFT))
1376 {
1377 /* Translate shift-left to right button. */
1378 button = MOUSE_RIGHT;
1379 mod_mask &= ~MOD_MASK_SHIFT;
1380 }
1381 clip_modeless(button, is_click, is_drag);
1382 goto cmdline_not_changed;
1383 }
1384# endif
1385
1386 set_cmdspos();
1387 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1388 ++ccline.cmdpos)
1389 {
1390 i = cmdline_charsize(ccline.cmdpos);
1391 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1392 && mouse_col < ccline.cmdspos % Columns + i)
1393 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001394# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 if (has_mbyte)
1396 {
1397 /* Count ">" for double-wide char that doesn't fit. */
1398 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001399 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 + ccline.cmdpos) - 1;
1401 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 ccline.cmdspos += i;
1404 }
1405 goto cmdline_not_changed;
1406
1407 /* Mouse scroll wheel: ignored here */
1408 case K_MOUSEDOWN:
1409 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001410 case K_MOUSELEFT:
1411 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 /* Alternate buttons ignored here */
1413 case K_X1MOUSE:
1414 case K_X1DRAG:
1415 case K_X1RELEASE:
1416 case K_X2MOUSE:
1417 case K_X2DRAG:
1418 case K_X2RELEASE:
1419 goto cmdline_not_changed;
1420
1421#endif /* FEAT_MOUSE */
1422
1423#ifdef FEAT_GUI
1424 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1425 case K_LEFTRELEASE_NM:
1426 goto cmdline_not_changed;
1427
1428 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001429 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 {
1431 gui_do_scroll();
1432 redrawcmd();
1433 }
1434 goto cmdline_not_changed;
1435
1436 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001437 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001439 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 redrawcmd();
1441 }
1442 goto cmdline_not_changed;
1443#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001444#ifdef FEAT_GUI_TABLINE
1445 case K_TABLINE:
1446 case K_TABMENU:
1447 /* Don't want to change any tabs here. Make sure the same tab
1448 * is still selected. */
1449 if (gui_use_tabline())
1450 gui_mch_set_curtab(tabpage_index(curtab));
1451 goto cmdline_not_changed;
1452#endif
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 case K_SELECT: /* end of Select mode mapping - ignore */
1455 goto cmdline_not_changed;
1456
1457 case Ctrl_B: /* begin of command line */
1458 case K_HOME:
1459 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 case K_S_HOME:
1461 case K_C_HOME:
1462 ccline.cmdpos = 0;
1463 set_cmdspos();
1464 goto cmdline_not_changed;
1465
1466 case Ctrl_E: /* end of command line */
1467 case K_END:
1468 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 case K_S_END:
1470 case K_C_END:
1471 ccline.cmdpos = ccline.cmdlen;
1472 set_cmdspos_cursor();
1473 goto cmdline_not_changed;
1474
1475 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001476 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 break;
1478 goto cmdline_changed;
1479
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001480 case Ctrl_L:
1481#ifdef FEAT_SEARCH_EXTRA
1482 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1483 {
1484 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001485 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001486 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001487 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001488 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001489 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001490 c = gchar_cursor();
1491 /* If 'ignorecase' and 'smartcase' are set and the
1492 * command line has no uppercase characters, convert
1493 * the character to lowercase */
1494 if (p_ic && p_scs
1495 && !pat_has_uppercase(ccline.cmdbuff))
1496 c = MB_TOLOWER(c);
1497 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001498 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001499 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001500 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001501 != NULL)
1502 {
1503 /* put a backslash before special
1504 * characters */
1505 stuffcharReadbuff(c);
1506 c = '\\';
1507 }
1508 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001509 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001510 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001511 }
1512 goto cmdline_not_changed;
1513 }
1514#endif
1515
1516 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001517 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 break;
1519 goto cmdline_changed;
1520
1521 case Ctrl_N: /* next match */
1522 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001523 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001525 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1526 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001528 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
Bram Moolenaar11956692016-08-27 16:26:56 +02001530 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531
1532#ifdef FEAT_CMDHIST
1533 case K_UP:
1534 case K_DOWN:
1535 case K_S_UP:
1536 case K_S_DOWN:
1537 case K_PAGEUP:
1538 case K_KPAGEUP:
1539 case K_PAGEDOWN:
1540 case K_KPAGEDOWN:
1541 if (hislen == 0 || firstc == NUL) /* no history */
1542 goto cmdline_not_changed;
1543
1544 i = hiscnt;
1545
1546 /* save current command string so it can be restored later */
1547 if (lookfor == NULL)
1548 {
1549 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1550 goto cmdline_not_changed;
1551 lookfor[ccline.cmdpos] = NUL;
1552 }
1553
1554 j = (int)STRLEN(lookfor);
1555 for (;;)
1556 {
1557 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001558 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001559 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
1561 if (hiscnt == hislen) /* first time */
1562 hiscnt = hisidx[histype];
1563 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1564 hiscnt = hislen - 1;
1565 else if (hiscnt != hisidx[histype] + 1)
1566 --hiscnt;
1567 else /* at top of list */
1568 {
1569 hiscnt = i;
1570 break;
1571 }
1572 }
1573 else /* one step forwards */
1574 {
1575 /* on last entry, clear the line */
1576 if (hiscnt == hisidx[histype])
1577 {
1578 hiscnt = hislen;
1579 break;
1580 }
1581
1582 /* not on a history line, nothing to do */
1583 if (hiscnt == hislen)
1584 break;
1585 if (hiscnt == hislen - 1) /* wrap around */
1586 hiscnt = 0;
1587 else
1588 ++hiscnt;
1589 }
1590 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1591 {
1592 hiscnt = i;
1593 break;
1594 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001595 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001596 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 || STRNCMP(history[histype][hiscnt].hisstr,
1598 lookfor, (size_t)j) == 0)
1599 break;
1600 }
1601
1602 if (hiscnt != i) /* jumped to other entry */
1603 {
1604 char_u *p;
1605 int len;
1606 int old_firstc;
1607
1608 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001609 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (hiscnt == hislen)
1611 p = lookfor; /* back to the old one */
1612 else
1613 p = history[histype][hiscnt].hisstr;
1614
1615 if (histype == HIST_SEARCH
1616 && p != lookfor
1617 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1618 {
1619 /* Correct for the separator character used when
1620 * adding the history entry vs the one used now.
1621 * First loop: count length.
1622 * Second loop: copy the characters. */
1623 for (i = 0; i <= 1; ++i)
1624 {
1625 len = 0;
1626 for (j = 0; p[j] != NUL; ++j)
1627 {
1628 /* Replace old sep with new sep, unless it is
1629 * escaped. */
1630 if (p[j] == old_firstc
1631 && (j == 0 || p[j - 1] != '\\'))
1632 {
1633 if (i > 0)
1634 ccline.cmdbuff[len] = firstc;
1635 }
1636 else
1637 {
1638 /* Escape new sep, unless it is already
1639 * escaped. */
1640 if (p[j] == firstc
1641 && (j == 0 || p[j - 1] != '\\'))
1642 {
1643 if (i > 0)
1644 ccline.cmdbuff[len] = '\\';
1645 ++len;
1646 }
1647 if (i > 0)
1648 ccline.cmdbuff[len] = p[j];
1649 }
1650 ++len;
1651 }
1652 if (i == 0)
1653 {
1654 alloc_cmdbuff(len);
1655 if (ccline.cmdbuff == NULL)
1656 goto returncmd;
1657 }
1658 }
1659 ccline.cmdbuff[len] = NUL;
1660 }
1661 else
1662 {
1663 alloc_cmdbuff((int)STRLEN(p));
1664 if (ccline.cmdbuff == NULL)
1665 goto returncmd;
1666 STRCPY(ccline.cmdbuff, p);
1667 }
1668
1669 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1670 redrawcmd();
1671 goto cmdline_changed;
1672 }
1673 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001674#endif
1675 goto cmdline_not_changed;
1676
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001677#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001678 case Ctrl_G: /* next match */
1679 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001680 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1681 {
1682 pos_T t;
1683 int search_flags = SEARCH_KEEP + SEARCH_NOOF
1684 + SEARCH_PEEK;
1685
1686 if (char_avail())
1687 continue;
1688 cursor_off();
1689 out_flush();
1690 if (c == Ctrl_G)
1691 {
1692 t = match_end;
1693 search_flags += SEARCH_COL;
1694 }
1695 else
1696 t = match_start;
1697 ++emsg_off;
1698 i = searchit(curwin, curbuf, &t,
1699 c == Ctrl_G ? FORWARD : BACKWARD,
1700 ccline.cmdbuff, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001701 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001702 --emsg_off;
1703 if (i)
1704 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001705 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001706 match_end = t;
1707 match_start = t;
1708 if (c == Ctrl_T && firstc == '/')
1709 {
1710 /* move just before the current match, so that
1711 * when nv_search finishes the cursor will be
1712 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001713 search_start = t;
1714 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001715 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001716 else if (c == Ctrl_G && firstc == '?')
1717 {
1718 /* move just after the current match, so that
1719 * when nv_search finishes the cursor will be
1720 * put back on the match */
1721 search_start = t;
1722 (void)incl(&search_start);
1723 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001724 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001725 {
1726 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001727 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001728 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001729 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001730 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001731 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001732 }
1733
1734 set_search_match(&match_end);
1735 curwin->w_cursor = match_start;
1736 changed_cline_bef_curs();
1737 update_topline();
1738 validate_cursor();
1739 highlight_match = TRUE;
1740 old_curswant = curwin->w_curswant;
1741 old_leftcol = curwin->w_leftcol;
1742 old_topline = curwin->w_topline;
1743# ifdef FEAT_DIFF
1744 old_topfill = curwin->w_topfill;
1745# endif
1746 old_botline = curwin->w_botline;
1747 update_screen(NOT_VALID);
1748 redrawcmdline();
1749 }
1750 else
1751 vim_beep(BO_ERROR);
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001752 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001753 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001754 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755#endif
1756
1757 case Ctrl_V:
1758 case Ctrl_Q:
1759#ifdef FEAT_MOUSE
1760 ignore_drag_release = TRUE;
1761#endif
1762 putcmdline('^', TRUE);
1763 c = get_literal(); /* get next (two) character(s) */
1764 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001765 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_MBYTE
1767 /* may need to remove ^ when composing char was typed */
1768 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1769 {
1770 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1771 msg_putchar(' ');
1772 cursorcmd();
1773 }
1774#endif
1775 break;
1776
1777#ifdef FEAT_DIGRAPHS
1778 case Ctrl_K:
1779#ifdef FEAT_MOUSE
1780 ignore_drag_release = TRUE;
1781#endif
1782 putcmdline('?', TRUE);
1783#ifdef USE_ON_FLY_SCROLL
1784 dont_scroll = TRUE; /* disallow scrolling here */
1785#endif
1786 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001787 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 if (c != NUL)
1789 break;
1790
1791 redrawcmd();
1792 goto cmdline_not_changed;
1793#endif /* FEAT_DIGRAPHS */
1794
1795#ifdef FEAT_RIGHTLEFT
1796 case Ctrl__: /* CTRL-_: switch language mode */
1797 if (!p_ari)
1798 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001799# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 if (p_altkeymap)
1801 {
1802 cmd_fkmap = !cmd_fkmap;
1803 if (cmd_fkmap) /* in Farsi always in Insert mode */
1804 ccline.overstrike = FALSE;
1805 }
1806 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 cmd_hkmap = !cmd_hkmap;
1809 goto cmdline_not_changed;
1810#endif
1811
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001812 case K_PS:
1813 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1814 goto cmdline_changed;
1815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 default:
1817#ifdef UNIX
1818 if (c == intr_char)
1819 {
1820 gotesc = TRUE; /* will free ccline.cmdbuff after
1821 putting it in history */
1822 goto returncmd; /* back to Normal mode */
1823 }
1824#endif
1825 /*
1826 * Normal character with no special meaning. Just set mod_mask
1827 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1828 * the string <S-Space>. This should only happen after ^V.
1829 */
1830 if (!IS_SPECIAL(c))
1831 mod_mask = 0x0;
1832 break;
1833 }
1834 /*
1835 * End of switch on command line character.
1836 * We come here if we have a normal character.
1837 */
1838
Bram Moolenaarede3e632013-06-23 16:16:19 +02001839 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#ifdef FEAT_MBYTE
1841 /* Add ABBR_OFF for characters above 0x100, this is
1842 * what check_abbr() expects. */
1843 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1844#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001845 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 goto cmdline_changed;
1847
1848 /*
1849 * put the character in the command line
1850 */
1851 if (IS_SPECIAL(c) || mod_mask != 0)
1852 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1853 else
1854 {
1855#ifdef FEAT_MBYTE
1856 if (has_mbyte)
1857 {
1858 j = (*mb_char2bytes)(c, IObuff);
1859 IObuff[j] = NUL; /* exclude composing chars */
1860 put_on_cmdline(IObuff, j, TRUE);
1861 }
1862 else
1863#endif
1864 {
1865 IObuff[0] = c;
1866 put_on_cmdline(IObuff, 1, TRUE);
1867 }
1868 }
1869 goto cmdline_changed;
1870
1871/*
1872 * This part implements incremental searches for "/" and "?"
1873 * Jump to cmdline_not_changed when a character has been read but the command
1874 * line did not change. Then we only search and redraw if something changed in
1875 * the past.
1876 * Jump to cmdline_changed when the command line did change.
1877 * (Sorry for the goto's, I know it is ugly).
1878 */
1879cmdline_not_changed:
1880#ifdef FEAT_SEARCH_EXTRA
1881 if (!incsearch_postponed)
1882 continue;
1883#endif
1884
1885cmdline_changed:
1886#ifdef FEAT_SEARCH_EXTRA
1887 /*
1888 * 'incsearch' highlighting.
1889 */
1890 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1891 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001892 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001893#ifdef FEAT_RELTIME
1894 proftime_T tm;
1895#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 /* if there is a character waiting, search and redraw later */
1898 if (char_avail())
1899 {
1900 incsearch_postponed = TRUE;
1901 continue;
1902 }
1903 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001904 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
1906 /* If there is no command line, don't do anything */
1907 if (ccline.cmdlen == 0)
1908 i = 0;
1909 else
1910 {
1911 cursor_off(); /* so the user knows we're busy */
1912 out_flush();
1913 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001914#ifdef FEAT_RELTIME
1915 /* Set the time limit to half a second. */
1916 profile_setlimit(500L, &tm);
1917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001919 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1920#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001921 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001922#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001923 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001924#endif
1925 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 --emsg_off;
1927 /* if interrupted while searching, behave like it failed */
1928 if (got_int)
1929 {
1930 (void)vpeekc(); /* remove <C-C> from input stream */
1931 got_int = FALSE; /* don't abandon the command line */
1932 i = 0;
1933 }
1934 else if (char_avail())
1935 /* cancelled searching because a char was typed */
1936 incsearch_postponed = TRUE;
1937 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001938 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 highlight_match = TRUE; /* highlight position */
1940 else
1941 highlight_match = FALSE; /* remove highlight */
1942
1943 /* first restore the old curwin values, so the screen is
1944 * positioned in the same way as the actual search command */
1945 curwin->w_leftcol = old_leftcol;
1946 curwin->w_topline = old_topline;
1947# ifdef FEAT_DIFF
1948 curwin->w_topfill = old_topfill;
1949# endif
1950 curwin->w_botline = old_botline;
1951 changed_cline_bef_curs();
1952 update_topline();
1953
1954 if (i != 0)
1955 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001956 pos_T save_pos = curwin->w_cursor;
1957
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001958 match_start = curwin->w_cursor;
1959 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001961 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001962 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001963 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001965 else
1966 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001969# ifdef FEAT_WINDOWS
1970 /* May redraw the status line to show the cursor position. */
1971 if (p_ru && curwin->w_status_height > 0)
1972 curwin->w_redr_status = TRUE;
1973# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001975 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001976 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001977 restore_cmdline(&save_ccline);
1978
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001979 /* Leave it at the end to make CTRL-R CTRL-W work. */
1980 if (i != 0)
1981 curwin->w_cursor = end_pos;
1982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 msg_starthere();
1984 redrawcmdline();
1985 did_incsearch = TRUE;
1986 }
1987#else /* FEAT_SEARCH_EXTRA */
1988 ;
1989#endif
1990
1991#ifdef FEAT_RIGHTLEFT
1992 if (cmdmsg_rl
1993# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001994 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995# endif
1996 )
1997 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001998 * right-left typing. Not efficient, but it works.
1999 * Do it only when there are no characters left to read
2000 * to avoid useless intermediate redraws. */
2001 if (vpeekc() == NUL)
2002 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003#endif
2004 }
2005
2006returncmd:
2007
2008#ifdef FEAT_RIGHTLEFT
2009 cmdmsg_rl = FALSE;
2010#endif
2011
2012#ifdef FEAT_FKMAP
2013 cmd_fkmap = 0;
2014#endif
2015
2016 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002017 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018
2019#ifdef FEAT_SEARCH_EXTRA
2020 if (did_incsearch)
2021 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002022 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002023 curwin->w_cursor = save_cursor;
2024 else
2025 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002026 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002027 {
2028 /* put the '" mark at the original position */
2029 curwin->w_cursor = save_cursor;
2030 setpcmark();
2031 }
2032 curwin->w_cursor = search_start;
2033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 curwin->w_curswant = old_curswant;
2035 curwin->w_leftcol = old_leftcol;
2036 curwin->w_topline = old_topline;
2037# ifdef FEAT_DIFF
2038 curwin->w_topfill = old_topfill;
2039# endif
2040 curwin->w_botline = old_botline;
2041 highlight_match = FALSE;
2042 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002043 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
2045#endif
2046
2047 if (ccline.cmdbuff != NULL)
2048 {
2049 /*
2050 * Put line in history buffer (":" and "=" only when it was typed).
2051 */
2052#ifdef FEAT_CMDHIST
2053 if (ccline.cmdlen && firstc != NUL
2054 && (some_key_typed || histype == HIST_SEARCH))
2055 {
2056 add_to_history(histype, ccline.cmdbuff, TRUE,
2057 histype == HIST_SEARCH ? firstc : NUL);
2058 if (firstc == ':')
2059 {
2060 vim_free(new_last_cmdline);
2061 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2062 }
2063 }
2064#endif
2065
2066 if (gotesc) /* abandon command line */
2067 {
2068 vim_free(ccline.cmdbuff);
2069 ccline.cmdbuff = NULL;
2070 if (msg_scrolled == 0)
2071 compute_cmdrow();
2072 MSG("");
2073 redraw_cmdline = TRUE;
2074 }
2075 }
2076
2077 /*
2078 * If the screen was shifted up, redraw the whole screen (later).
2079 * If the line is too long, clear it, so ruler and shown command do
2080 * not get printed in the middle of it.
2081 */
2082 msg_check();
2083 msg_scroll = save_msg_scroll;
2084 redir_off = FALSE;
2085
2086 /* When the command line was typed, no need for a wait-return prompt. */
2087 if (some_key_typed)
2088 need_wait_return = FALSE;
2089
2090 State = save_State;
2091#ifdef USE_IM_CONTROL
2092 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2093 im_save_status(b_im_ptr);
2094 im_set_active(FALSE);
2095#endif
2096#ifdef FEAT_MOUSE
2097 setmouse();
2098#endif
2099#ifdef CURSOR_SHAPE
2100 ui_cursor_shape(); /* may show different cursor shape */
2101#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002102 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002104 {
2105 char_u *p = ccline.cmdbuff;
2106
2107 /* Make ccline empty, getcmdline() may try to use it. */
2108 ccline.cmdbuff = NULL;
2109 return p;
2110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111}
2112
2113#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2114/*
2115 * Get a command line with a prompt.
2116 * This is prepared to be called recursively from getcmdline() (e.g. by
2117 * f_input() when evaluating an expression from CTRL-R =).
2118 * Returns the command line in allocated memory, or NULL.
2119 */
2120 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002121getcmdline_prompt(
2122 int firstc,
2123 char_u *prompt, /* command line prompt */
2124 int attr, /* attributes for prompt */
2125 int xp_context, /* type of expansion */
2126 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127{
2128 char_u *s;
2129 struct cmdline_info save_ccline;
2130 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002131 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002133 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 ccline.cmdprompt = prompt;
2135 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002136# ifdef FEAT_EVAL
2137 ccline.xp_context = xp_context;
2138 ccline.xp_arg = xp_arg;
2139 ccline.input_fn = (firstc == '@');
2140# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002141 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002143 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002144 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002145 /* Restore msg_col, the prompt from input() may have changed it.
2146 * But only if called recursively and the commandline is therefore being
2147 * restored to an old one; if not, the input() prompt stays on the screen,
2148 * so we need its modified msg_col left intact. */
2149 if (ccline.cmdbuff != NULL)
2150 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 return s;
2153}
2154#endif
2155
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002156/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002157 * Return TRUE when the text must not be changed and we can't switch to
2158 * another window or buffer. Used when editing the command line, evaluating
2159 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002160 */
2161 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002162text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002163{
2164#ifdef FEAT_CMDWIN
2165 if (cmdwin_type != 0)
2166 return TRUE;
2167#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002168 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002169}
2170
2171/*
2172 * Give an error message for a command that isn't allowed while the cmdline
2173 * window is open or editing the cmdline in another way.
2174 */
2175 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002176text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002177{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002178 EMSG(_(get_text_locked_msg()));
2179}
2180
2181 char_u *
2182get_text_locked_msg(void)
2183{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002184#ifdef FEAT_CMDWIN
2185 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002186 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002187#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002188 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002189}
2190
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002191#if defined(FEAT_AUTOCMD) || defined(PROTO)
2192/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002193 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2194 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002195 */
2196 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002197curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002198{
2199 if (curbuf_lock > 0)
2200 {
2201 EMSG(_("E788: Not allowed to edit another buffer now"));
2202 return TRUE;
2203 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002204 return allbuf_locked();
2205}
2206
2207/*
2208 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2209 * message.
2210 */
2211 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002212allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002213{
2214 if (allbuf_lock > 0)
2215 {
2216 EMSG(_("E811: Not allowed to change buffer information now"));
2217 return TRUE;
2218 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219 return FALSE;
2220}
2221#endif
2222
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002224cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225{
2226#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2227 if (cmdline_star > 0) /* showing '*', always 1 position */
2228 return 1;
2229#endif
2230 return ptr2cells(ccline.cmdbuff + idx);
2231}
2232
2233/*
2234 * Compute the offset of the cursor on the command line for the prompt and
2235 * indent.
2236 */
2237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002238set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002240 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 ccline.cmdspos = 1 + ccline.cmdindent;
2242 else
2243 ccline.cmdspos = 0 + ccline.cmdindent;
2244}
2245
2246/*
2247 * Compute the screen position for the cursor on the command line.
2248 */
2249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002250set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251{
2252 int i, m, c;
2253
2254 set_cmdspos();
2255 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002258 if (m < 0) /* overflow, Columns or Rows at weird value */
2259 m = MAXCOL;
2260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 else
2262 m = MAXCOL;
2263 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2264 {
2265 c = cmdline_charsize(i);
2266#ifdef FEAT_MBYTE
2267 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2268 if (has_mbyte)
2269 correct_cmdspos(i, c);
2270#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002271 /* If the cmdline doesn't fit, show cursor on last visible char.
2272 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if ((ccline.cmdspos += c) >= m)
2274 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 ccline.cmdspos -= c;
2276 break;
2277 }
2278#ifdef FEAT_MBYTE
2279 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002280 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281#endif
2282 }
2283}
2284
2285#ifdef FEAT_MBYTE
2286/*
2287 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2288 * character that doesn't fit, so that a ">" must be displayed.
2289 */
2290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002291correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002293 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2295 && ccline.cmdspos % Columns + cells > Columns)
2296 ccline.cmdspos++;
2297}
2298#endif
2299
2300/*
2301 * Get an Ex command line for the ":" command.
2302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002304getexline(
2305 int c, /* normally ':', NUL for ":append" */
2306 void *cookie UNUSED,
2307 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308{
2309 /* When executing a register, remove ':' that's in front of each line. */
2310 if (exec_from_reg && vpeekc() == ':')
2311 (void)vgetc();
2312 return getcmdline(c, 1L, indent);
2313}
2314
2315/*
2316 * Get an Ex command line for Ex mode.
2317 * In Ex mode we only use the OS supplied line editing features and no
2318 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002319 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002322getexmodeline(
2323 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002324 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002325 void *cookie UNUSED,
2326 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002328 garray_T line_ga;
2329 char_u *pend;
2330 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002331 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002332 int escaped = FALSE; /* CTRL-V typed */
2333 int vcol = 0;
2334 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002335 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002336 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 /* Switch cursor on now. This avoids that it happens after the "\n", which
2339 * confuses the system function that computes tabstops. */
2340 cursor_on();
2341
2342 /* always start in column 0; write a newline if necessary */
2343 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002344 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002346 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002348 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002349 if (p_prompt)
2350 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 while (indent-- > 0)
2352 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
2355
2356 ga_init2(&line_ga, 1, 30);
2357
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002358 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002359 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002360 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002361 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002362 while (indent >= 8)
2363 {
2364 ga_append(&line_ga, TAB);
2365 msg_puts((char_u *)" ");
2366 indent -= 8;
2367 }
2368 while (indent-- > 0)
2369 {
2370 ga_append(&line_ga, ' ');
2371 msg_putchar(' ');
2372 }
2373 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002374 ++no_mapping;
2375 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002376
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 /*
2378 * Get the line, one character at a time.
2379 */
2380 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002381 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002383 long sw;
2384 char_u *s;
2385
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 if (ga_grow(&line_ga, 40) == FAIL)
2387 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaarba748c82017-02-26 14:00:07 +01002389 /*
2390 * Get one character at a time.
2391 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002392 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002393
2394 /* Check for a ":normal" command and no more characters left. */
2395 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2396 c1 = '\n';
2397 else
2398 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002401 * Handle line editing.
2402 * Previously this was left to the system, putting the terminal in
2403 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002405 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002407 msg_putchar('\n');
2408 break;
2409 }
2410
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002411 if (c1 == K_PS)
2412 {
2413 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2414 goto redraw;
2415 }
2416
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002417 if (!escaped)
2418 {
2419 /* CR typed means "enter", which is NL */
2420 if (c1 == '\r')
2421 c1 = '\n';
2422
2423 if (c1 == BS || c1 == K_BS
2424 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002426 if (line_ga.ga_len > 0)
2427 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002428#ifdef FEAT_MBYTE
2429 if (has_mbyte)
2430 {
2431 p = (char_u *)line_ga.ga_data;
2432 p[line_ga.ga_len] = NUL;
2433 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2434 line_ga.ga_len -= len;
2435 }
2436 else
2437#endif
2438 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002439 goto redraw;
2440 }
2441 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002444 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002446 msg_col = startcol;
2447 msg_clr_eos();
2448 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002449 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002450 }
2451
2452 if (c1 == Ctrl_T)
2453 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002454 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002455 p = (char_u *)line_ga.ga_data;
2456 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002457 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002458 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002459add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002460 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002462 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002463 p = (char_u *)line_ga.ga_data;
2464 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002465 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2466 *s = ' ';
2467 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002469redraw:
2470 /* redraw the line */
2471 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002472 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002473 p = (char_u *)line_ga.ga_data;
2474 p[line_ga.ga_len] = NUL;
2475 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002477 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002479 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002481 msg_putchar(' ');
2482 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002483 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002487 len = MB_PTR2LEN(p);
2488 msg_outtrans_len(p, len);
2489 vcol += ptr2cells(p);
2490 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002493 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002494 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002495 continue;
2496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002498 if (c1 == Ctrl_D)
2499 {
2500 /* Delete one shiftwidth. */
2501 p = (char_u *)line_ga.ga_data;
2502 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002504 if (prev_char == '^')
2505 ex_keep_indent = TRUE;
2506 indent = 0;
2507 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 }
2509 else
2510 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002511 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002512 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002513 if (indent > 0)
2514 {
2515 --indent;
2516 indent -= indent % get_sw_value(curbuf);
2517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002519 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002520 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002521 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002522 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2523 --line_ga.ga_len;
2524 }
2525 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002527
2528 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2529 {
2530 escaped = TRUE;
2531 continue;
2532 }
2533
2534 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2535 if (IS_SPECIAL(c1))
2536 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002538
2539 if (IS_SPECIAL(c1))
2540 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002541#ifdef FEAT_MBYTE
2542 if (has_mbyte)
2543 len = (*mb_char2bytes)(c1,
2544 (char_u *)line_ga.ga_data + line_ga.ga_len);
2545 else
2546#endif
2547 {
2548 len = 1;
2549 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2550 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002551 if (c1 == '\n')
2552 msg_putchar('\n');
2553 else if (c1 == TAB)
2554 {
2555 /* Don't use chartabsize(), 'ts' can be different */
2556 do
2557 {
2558 msg_putchar(' ');
2559 } while (++vcol % 8);
2560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002563 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002564 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002565 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002567 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002568 escaped = FALSE;
2569
2570 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002571 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002572
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002573 /* We are done when a NL is entered, but not when it comes after an
2574 * odd number of backslashes, that results in a NUL. */
2575 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002577 int bcount = 0;
2578
2579 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2580 ++bcount;
2581
2582 if (bcount > 0)
2583 {
2584 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2585 * "\NL", etc. */
2586 line_ga.ga_len -= (bcount + 1) / 2;
2587 pend -= (bcount + 1) / 2;
2588 pend[-1] = '\n';
2589 }
2590
2591 if ((bcount & 1) == 0)
2592 {
2593 --line_ga.ga_len;
2594 --pend;
2595 *pend = NUL;
2596 break;
2597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599 }
2600
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002601 --no_mapping;
2602 --allow_keys;
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 /* make following messages go to the next line */
2605 msg_didout = FALSE;
2606 msg_col = 0;
2607 if (msg_row < Rows - 1)
2608 ++msg_row;
2609 emsg_on_display = FALSE; /* don't want ui_delay() */
2610
2611 if (got_int)
2612 ga_clear(&line_ga);
2613
2614 return (char_u *)line_ga.ga_data;
2615}
2616
Bram Moolenaare344bea2005-09-01 20:46:49 +00002617# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2618 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619/*
2620 * Return TRUE if ccline.overstrike is on.
2621 */
2622 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002623cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
2625 return ccline.overstrike;
2626}
2627
2628/*
2629 * Return TRUE if the cursor is at the end of the cmdline.
2630 */
2631 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002632cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633{
2634 return (ccline.cmdpos >= ccline.cmdlen);
2635}
2636#endif
2637
Bram Moolenaar9372a112005-12-06 19:59:18 +00002638#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639/*
2640 * Return the virtual column number at the current cursor position.
2641 * This is used by the IM code to obtain the start of the preedit string.
2642 */
2643 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002644cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
2646 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2647 return MAXCOL;
2648
2649# ifdef FEAT_MBYTE
2650 if (has_mbyte)
2651 {
2652 colnr_T col;
2653 int i = 0;
2654
2655 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002656 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 return col;
2659 }
2660 else
2661# endif
2662 return ccline.cmdpos;
2663}
2664#endif
2665
2666#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2667/*
2668 * If part of the command line is an IM preedit string, redraw it with
2669 * IM feedback attributes. The cursor position is restored after drawing.
2670 */
2671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002672redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 if ((State & CMDLINE)
2675 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002676 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 && !p_imdisable
2678 && im_is_preediting())
2679 {
2680 int cmdpos = 0;
2681 int cmdspos;
2682 int old_row;
2683 int old_col;
2684 colnr_T col;
2685
2686 old_row = msg_row;
2687 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002688 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690# ifdef FEAT_MBYTE
2691 if (has_mbyte)
2692 {
2693 for (col = 0; col < preedit_start_col
2694 && cmdpos < ccline.cmdlen; ++col)
2695 {
2696 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002697 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699 }
2700 else
2701# endif
2702 {
2703 cmdspos += preedit_start_col;
2704 cmdpos += preedit_start_col;
2705 }
2706
2707 msg_row = cmdline_row + (cmdspos / (int)Columns);
2708 msg_col = cmdspos % (int)Columns;
2709 if (msg_row >= Rows)
2710 msg_row = Rows - 1;
2711
2712 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2713 {
2714 int char_len;
2715 int char_attr;
2716
2717 char_attr = im_get_feedback_attr(col);
2718 if (char_attr < 0)
2719 break; /* end of preedit string */
2720
2721# ifdef FEAT_MBYTE
2722 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002723 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 else
2725# endif
2726 char_len = 1;
2727
2728 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2729 cmdpos += char_len;
2730 }
2731
2732 msg_row = old_row;
2733 msg_col = old_col;
2734 }
2735}
2736#endif /* FEAT_XIM && FEAT_GUI_GTK */
2737
2738/*
2739 * Allocate a new command line buffer.
2740 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2741 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2742 */
2743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002744alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
2746 /*
2747 * give some extra space to avoid having to allocate all the time
2748 */
2749 if (len < 80)
2750 len = 100;
2751 else
2752 len += 20;
2753
2754 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2755 ccline.cmdbufflen = len;
2756}
2757
2758/*
2759 * Re-allocate the command line to length len + something extra.
2760 * return FAIL for failure, OK otherwise
2761 */
2762 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002763realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 char_u *p;
2766
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002767 if (len < ccline.cmdbufflen)
2768 return OK; /* no need to resize */
2769
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 p = ccline.cmdbuff;
2771 alloc_cmdbuff(len); /* will get some more */
2772 if (ccline.cmdbuff == NULL) /* out of memory */
2773 {
2774 ccline.cmdbuff = p; /* keep the old one */
2775 return FAIL;
2776 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002777 /* There isn't always a NUL after the command, but it may need to be
2778 * there, thus copy up to the NUL and add a NUL. */
2779 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2780 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002782
2783 if (ccline.xpc != NULL
2784 && ccline.xpc->xp_pattern != NULL
2785 && ccline.xpc->xp_context != EXPAND_NOTHING
2786 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2787 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002788 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002789
2790 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2791 * to point into the newly allocated memory. */
2792 if (i >= 0 && i <= ccline.cmdlen)
2793 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2794 }
2795
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 return OK;
2797}
2798
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002799#if defined(FEAT_ARABIC) || defined(PROTO)
2800static char_u *arshape_buf = NULL;
2801
2802# if defined(EXITFREE) || defined(PROTO)
2803 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002804free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002805{
2806 vim_free(arshape_buf);
2807}
2808# endif
2809#endif
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811/*
2812 * Draw part of the cmdline at the current cursor position. But draw stars
2813 * when cmdline_star is TRUE.
2814 */
2815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002816draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
2818#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2819 int i;
2820
2821 if (cmdline_star > 0)
2822 for (i = 0; i < len; ++i)
2823 {
2824 msg_putchar('*');
2825# ifdef FEAT_MBYTE
2826 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002827 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828# endif
2829 }
2830 else
2831#endif
2832#ifdef FEAT_ARABIC
2833 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 static int buflen = 0;
2836 char_u *p;
2837 int j;
2838 int newlen = 0;
2839 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002840 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 int prev_c = 0;
2842 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002843 int u8c;
2844 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
2847 /*
2848 * Do arabic shaping into a temporary buffer. This is very
2849 * inefficient!
2850 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002851 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
2853 /* Re-allocate the buffer. We keep it around to avoid a lot of
2854 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002855 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002856 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002857 arshape_buf = alloc(buflen);
2858 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 return; /* out of memory */
2860 }
2861
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002862 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2863 {
2864 /* Prepend a space to draw the leading composing char on. */
2865 arshape_buf[0] = ' ';
2866 newlen = 1;
2867 }
2868
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 for (j = start; j < start + len; j += mb_l)
2870 {
2871 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002872 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002873 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 if (ARABIC_CHAR(u8c))
2875 {
2876 /* Do Arabic shaping. */
2877 if (cmdmsg_rl)
2878 {
2879 /* displaying from right to left */
2880 pc = prev_c;
2881 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002882 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 if (j + mb_l >= start + len)
2884 nc = NUL;
2885 else
2886 nc = utf_ptr2char(p + mb_l);
2887 }
2888 else
2889 {
2890 /* displaying from left to right */
2891 if (j + mb_l >= start + len)
2892 pc = NUL;
2893 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002894 {
2895 int pcc[MAX_MCO];
2896
2897 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002899 pc1 = pcc[0];
2900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 nc = prev_c;
2902 }
2903 prev_c = u8c;
2904
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002905 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002907 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002908 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002910 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2911 if (u8cc[1] != 0)
2912 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002913 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
2915 }
2916 else
2917 {
2918 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002919 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 newlen += mb_l;
2921 }
2922 }
2923
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002924 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
2926 else
2927#endif
2928 msg_outtrans_len(ccline.cmdbuff + start, len);
2929}
2930
2931/*
2932 * Put a character on the command line. Shifts the following text to the
2933 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2934 * "c" must be printable (fit in one display cell)!
2935 */
2936 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002937putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938{
2939 if (cmd_silent)
2940 return;
2941 msg_no_more = TRUE;
2942 msg_putchar(c);
2943 if (shift)
2944 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2945 msg_no_more = FALSE;
2946 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02002947 extra_char = c;
2948 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949}
2950
2951/*
2952 * Undo a putcmdline(c, FALSE).
2953 */
2954 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002955unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 if (cmd_silent)
2958 return;
2959 msg_no_more = TRUE;
2960 if (ccline.cmdlen == ccline.cmdpos)
2961 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002962#ifdef FEAT_MBYTE
2963 else if (has_mbyte)
2964 draw_cmdline(ccline.cmdpos,
2965 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 else
2968 draw_cmdline(ccline.cmdpos, 1);
2969 msg_no_more = FALSE;
2970 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02002971 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972}
2973
2974/*
2975 * Put the given string, of the given length, onto the command line.
2976 * If len is -1, then STRLEN() is used to calculate the length.
2977 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2978 * part will be redrawn, otherwise it will not. If this function is called
2979 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2980 * called afterwards.
2981 */
2982 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002983put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 int retval;
2986 int i;
2987 int m;
2988 int c;
2989
2990 if (len < 0)
2991 len = (int)STRLEN(str);
2992
2993 /* Check if ccline.cmdbuff needs to be longer */
2994 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002995 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 else
2997 retval = OK;
2998 if (retval == OK)
2999 {
3000 if (!ccline.overstrike)
3001 {
3002 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3003 ccline.cmdbuff + ccline.cmdpos,
3004 (size_t)(ccline.cmdlen - ccline.cmdpos));
3005 ccline.cmdlen += len;
3006 }
3007 else
3008 {
3009#ifdef FEAT_MBYTE
3010 if (has_mbyte)
3011 {
3012 /* Count nr of characters in the new string. */
3013 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003014 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 ++m;
3016 /* Count nr of bytes in cmdline that are overwritten by these
3017 * characters. */
3018 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003019 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 --m;
3021 if (i < ccline.cmdlen)
3022 {
3023 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3024 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3025 ccline.cmdlen += ccline.cmdpos + len - i;
3026 }
3027 else
3028 ccline.cmdlen = ccline.cmdpos + len;
3029 }
3030 else
3031#endif
3032 if (ccline.cmdpos + len > ccline.cmdlen)
3033 ccline.cmdlen = ccline.cmdpos + len;
3034 }
3035 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3036 ccline.cmdbuff[ccline.cmdlen] = NUL;
3037
3038#ifdef FEAT_MBYTE
3039 if (enc_utf8)
3040 {
3041 /* When the inserted text starts with a composing character,
3042 * backup to the character before it. There could be two of them.
3043 */
3044 i = 0;
3045 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3046 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3047 {
3048 i = (*mb_head_off)(ccline.cmdbuff,
3049 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3050 ccline.cmdpos -= i;
3051 len += i;
3052 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3053 }
3054# ifdef FEAT_ARABIC
3055 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3056 {
3057 /* Check the previous character for Arabic combining pair. */
3058 i = (*mb_head_off)(ccline.cmdbuff,
3059 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3060 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3061 + ccline.cmdpos - i), c))
3062 {
3063 ccline.cmdpos -= i;
3064 len += i;
3065 }
3066 else
3067 i = 0;
3068 }
3069# endif
3070 if (i != 0)
3071 {
3072 /* Also backup the cursor position. */
3073 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3074 ccline.cmdspos -= i;
3075 msg_col -= i;
3076 if (msg_col < 0)
3077 {
3078 msg_col += Columns;
3079 --msg_row;
3080 }
3081 }
3082 }
3083#endif
3084
3085 if (redraw && !cmd_silent)
3086 {
3087 msg_no_more = TRUE;
3088 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003089 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3091 /* Avoid clearing the rest of the line too often. */
3092 if (cmdline_row != i || ccline.overstrike)
3093 msg_clr_eos();
3094 msg_no_more = FALSE;
3095 }
3096#ifdef FEAT_FKMAP
3097 /*
3098 * If we are in Farsi command mode, the character input must be in
3099 * Insert mode. So do not advance the cmdpos.
3100 */
3101 if (!cmd_fkmap)
3102#endif
3103 {
3104 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003107 if (m < 0) /* overflow, Columns or Rows at weird value */
3108 m = MAXCOL;
3109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 else
3111 m = MAXCOL;
3112 for (i = 0; i < len; ++i)
3113 {
3114 c = cmdline_charsize(ccline.cmdpos);
3115#ifdef FEAT_MBYTE
3116 /* count ">" for a double-wide char that doesn't fit. */
3117 if (has_mbyte)
3118 correct_cmdspos(ccline.cmdpos, c);
3119#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003120 /* Stop cursor at the end of the screen, but do increment the
3121 * insert position, so that entering a very long command
3122 * works, even though you can't see it. */
3123 if (ccline.cmdspos + c < m)
3124 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125#ifdef FEAT_MBYTE
3126 if (has_mbyte)
3127 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003128 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 if (c > len - i - 1)
3130 c = len - i - 1;
3131 ccline.cmdpos += c;
3132 i += c;
3133 }
3134#endif
3135 ++ccline.cmdpos;
3136 }
3137 }
3138 }
3139 if (redraw)
3140 msg_check();
3141 return retval;
3142}
3143
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003144static struct cmdline_info prev_ccline;
3145static int prev_ccline_used = FALSE;
3146
3147/*
3148 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3149 * and overwrite it. But get_cmdline_str() may need it, thus make it
3150 * available globally in prev_ccline.
3151 */
3152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003153save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003154{
3155 if (!prev_ccline_used)
3156 {
3157 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3158 prev_ccline_used = TRUE;
3159 }
3160 *ccp = prev_ccline;
3161 prev_ccline = ccline;
3162 ccline.cmdbuff = NULL;
3163 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003164 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003165}
3166
3167/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003168 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003169 */
3170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003171restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003172{
3173 ccline = prev_ccline;
3174 prev_ccline = *ccp;
3175}
3176
Bram Moolenaar5a305422006-04-28 22:38:25 +00003177#if defined(FEAT_EVAL) || defined(PROTO)
3178/*
3179 * Save the command line into allocated memory. Returns a pointer to be
3180 * passed to restore_cmdline_alloc() later.
3181 * Returns NULL when failed.
3182 */
3183 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003184save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003185{
3186 struct cmdline_info *p;
3187
3188 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3189 if (p != NULL)
3190 save_cmdline(p);
3191 return (char_u *)p;
3192}
3193
3194/*
3195 * Restore the command line from the return value of save_cmdline_alloc().
3196 */
3197 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003198restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003199{
3200 if (p != NULL)
3201 {
3202 restore_cmdline((struct cmdline_info *)p);
3203 vim_free(p);
3204 }
3205}
3206#endif
3207
Bram Moolenaar8299df92004-07-10 09:47:34 +00003208/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003209 * Paste a yank register into the command line.
3210 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003211 * insert_reg() can't be used here, because special characters from the
3212 * register contents will be interpreted as commands.
3213 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003214 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003215 */
3216 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003217cmdline_paste(
3218 int regname,
3219 int literally, /* Insert text literally instead of "as typed" */
3220 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003221{
3222 long i;
3223 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003224 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003225 int allocated;
3226 struct cmdline_info save_ccline;
3227
3228 /* check for valid regname; also accept special characters for CTRL-R in
3229 * the command line */
3230 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3231 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3232 return FAIL;
3233
3234 /* A register containing CTRL-R can cause an endless loop. Allow using
3235 * CTRL-C to break the loop. */
3236 line_breakcheck();
3237 if (got_int)
3238 return FAIL;
3239
3240#ifdef FEAT_CLIPBOARD
3241 regname = may_get_selection(regname);
3242#endif
3243
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003244 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003245 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003246 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003247 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003248 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003249 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003250 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003251
3252 if (i)
3253 {
3254 /* Got the value of a special register in "arg". */
3255 if (arg == NULL)
3256 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003257
3258 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3259 * part of the word. */
3260 p = arg;
3261 if (p_is && regname == Ctrl_W)
3262 {
3263 char_u *w;
3264 int len;
3265
3266 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003267 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003268 {
3269#ifdef FEAT_MBYTE
3270 if (has_mbyte)
3271 {
3272 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3273 if (!vim_iswordc(mb_ptr2char(w - len)))
3274 break;
3275 w -= len;
3276 }
3277 else
3278#endif
3279 {
3280 if (!vim_iswordc(w[-1]))
3281 break;
3282 --w;
3283 }
3284 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003285 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003286 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3287 p += len;
3288 }
3289
3290 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003291 if (allocated)
3292 vim_free(arg);
3293 return OK;
3294 }
3295
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003296 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003297}
3298
3299/*
3300 * Put a string on the command line.
3301 * When "literally" is TRUE, insert literally.
3302 * When "literally" is FALSE, insert as typed, but don't leave the command
3303 * line.
3304 */
3305 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003306cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003307{
3308 int c, cv;
3309
3310 if (literally)
3311 put_on_cmdline(s, -1, TRUE);
3312 else
3313 while (*s != NUL)
3314 {
3315 cv = *s;
3316 if (cv == Ctrl_V && s[1])
3317 ++s;
3318#ifdef FEAT_MBYTE
3319 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003320 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003321 else
3322#endif
3323 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003324 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3325 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003326#ifdef UNIX
3327 || c == intr_char
3328#endif
3329 || (c == Ctrl_BSL && *s == Ctrl_N))
3330 stuffcharReadbuff(Ctrl_V);
3331 stuffcharReadbuff(c);
3332 }
3333}
3334
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335#ifdef FEAT_WILDMENU
3336/*
3337 * Delete characters on the command line, from "from" to the current
3338 * position.
3339 */
3340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003341cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342{
3343 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3344 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3345 ccline.cmdlen -= ccline.cmdpos - from;
3346 ccline.cmdpos = from;
3347}
3348#endif
3349
3350/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003351 * This function is called when the screen size changes and with incremental
3352 * search and in other situations where the command line may have been
3353 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 */
3355 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003356redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003358 redrawcmdline_ex(TRUE);
3359}
3360
3361 void
3362redrawcmdline_ex(int do_compute_cmdrow)
3363{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 if (cmd_silent)
3365 return;
3366 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003367 if (do_compute_cmdrow)
3368 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 redrawcmd();
3370 cursorcmd();
3371}
3372
3373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003374redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 int i;
3377
3378 if (cmd_silent)
3379 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003380 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 msg_putchar(ccline.cmdfirstc);
3382 if (ccline.cmdprompt != NULL)
3383 {
3384 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3385 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3386 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003387 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 --ccline.cmdindent;
3389 }
3390 else
3391 for (i = ccline.cmdindent; i > 0; --i)
3392 msg_putchar(' ');
3393}
3394
3395/*
3396 * Redraw what is currently on the command line.
3397 */
3398 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003399redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 if (cmd_silent)
3402 return;
3403
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003404 /* when 'incsearch' is set there may be no command line while redrawing */
3405 if (ccline.cmdbuff == NULL)
3406 {
3407 windgoto(cmdline_row, 0);
3408 msg_clr_eos();
3409 return;
3410 }
3411
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 msg_start();
3413 redrawcmdprompt();
3414
3415 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3416 msg_no_more = TRUE;
3417 draw_cmdline(0, ccline.cmdlen);
3418 msg_clr_eos();
3419 msg_no_more = FALSE;
3420
3421 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003422 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003423 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 /*
3426 * An emsg() before may have set msg_scroll. This is used in normal mode,
3427 * in cmdline mode we can reset them now.
3428 */
3429 msg_scroll = FALSE; /* next message overwrites cmdline */
3430
3431 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3432 * in cmdline mode */
3433 skip_redraw = FALSE;
3434}
3435
3436 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003437compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003439 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 cmdline_row = Rows - 1;
3441 else
3442 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3443 + W_STATUS_HEIGHT(lastwin);
3444}
3445
3446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003447cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 if (cmd_silent)
3450 return;
3451
3452#ifdef FEAT_RIGHTLEFT
3453 if (cmdmsg_rl)
3454 {
3455 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3456 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3457 if (msg_row <= 0)
3458 msg_row = Rows - 1;
3459 }
3460 else
3461#endif
3462 {
3463 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3464 msg_col = ccline.cmdspos % (int)Columns;
3465 if (msg_row >= Rows)
3466 msg_row = Rows - 1;
3467 }
3468
3469 windgoto(msg_row, msg_col);
3470#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3471 redrawcmd_preedit();
3472#endif
3473#ifdef MCH_CURSOR_SHAPE
3474 mch_update_cursor();
3475#endif
3476}
3477
3478 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003479gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481 msg_start();
3482#ifdef FEAT_RIGHTLEFT
3483 if (cmdmsg_rl)
3484 msg_col = Columns - 1;
3485 else
3486#endif
3487 msg_col = 0; /* always start in column 0 */
3488 if (clr) /* clear the bottom line(s) */
3489 msg_clr_eos(); /* will reset clear_cmdline */
3490 windgoto(cmdline_row, 0);
3491}
3492
3493/*
3494 * Check the word in front of the cursor for an abbreviation.
3495 * Called when the non-id character "c" has been entered.
3496 * When an abbreviation is recognized it is removed from the text with
3497 * backspaces and the replacement string is inserted, followed by "c".
3498 */
3499 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003500ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501{
3502 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3503 return FALSE;
3504
3505 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3506}
3507
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003508#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3509 static int
3510#ifdef __BORLANDC__
3511_RTLENTRYF
3512#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003513sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003514{
3515 char_u *p1 = *(char_u **)s1;
3516 char_u *p2 = *(char_u **)s2;
3517
3518 if (*p1 != '<' && *p2 == '<') return -1;
3519 if (*p1 == '<' && *p2 != '<') return 1;
3520 return STRCMP(p1, p2);
3521}
3522#endif
3523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524/*
3525 * Return FAIL if this is not an appropriate context in which to do
3526 * completion of anything, return OK if it is (even if there are no matches).
3527 * For the caller, this means that the character is just passed through like a
3528 * normal character (instead of being expanded). This allows :s/^I^D etc.
3529 */
3530 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003531nextwild(
3532 expand_T *xp,
3533 int type,
3534 int options, /* extra options for ExpandOne() */
3535 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536{
3537 int i, j;
3538 char_u *p1;
3539 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 int difflen;
3541 int v;
3542
3543 if (xp->xp_numfiles == -1)
3544 {
3545 set_expand_context(xp);
3546 cmd_showtail = expand_showtail(xp);
3547 }
3548
3549 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3550 {
3551 beep_flush();
3552 return OK; /* Something illegal on command line */
3553 }
3554 if (xp->xp_context == EXPAND_NOTHING)
3555 {
3556 /* Caller can use the character as a normal char instead */
3557 return FAIL;
3558 }
3559
3560 MSG_PUTS("..."); /* show that we are busy */
3561 out_flush();
3562
3563 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003564 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
3566 if (type == WILD_NEXT || type == WILD_PREV)
3567 {
3568 /*
3569 * Get next/previous match for a previous expanded pattern.
3570 */
3571 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3572 }
3573 else
3574 {
3575 /*
3576 * Translate string into pattern and expand it.
3577 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003578 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3579 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 p2 = NULL;
3581 else
3582 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003583 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003584 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3585 if (escape)
3586 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003587
3588 if (p_wic)
3589 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003590 p2 = ExpandOne(xp, p1,
3591 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003592 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003594 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if (p2 != NULL && type == WILD_LONGEST)
3596 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003597 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 if (ccline.cmdbuff[i + j] == '*'
3599 || ccline.cmdbuff[i + j] == '?')
3600 break;
3601 if ((int)STRLEN(p2) < j)
3602 {
3603 vim_free(p2);
3604 p2 = NULL;
3605 }
3606 }
3607 }
3608 }
3609
3610 if (p2 != NULL && !got_int)
3611 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003612 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003613 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003615 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 xp->xp_pattern = ccline.cmdbuff + i;
3617 }
3618 else
3619 v = OK;
3620 if (v == OK)
3621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003622 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3623 &ccline.cmdbuff[ccline.cmdpos],
3624 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3625 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 ccline.cmdlen += difflen;
3627 ccline.cmdpos += difflen;
3628 }
3629 }
3630 vim_free(p2);
3631
3632 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003633 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
3635 /* When expanding a ":map" command and no matches are found, assume that
3636 * the key is supposed to be inserted literally */
3637 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3638 return FAIL;
3639
3640 if (xp->xp_numfiles <= 0 && p2 == NULL)
3641 beep_flush();
3642 else if (xp->xp_numfiles == 1)
3643 /* free expanded pattern */
3644 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3645
3646 return OK;
3647}
3648
3649/*
3650 * Do wildcard expansion on the string 'str'.
3651 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003652 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 * Return NULL for failure.
3654 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003655 * "orig" is the originally expanded string, copied to allocated memory. It
3656 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3657 * WILD_PREV "orig" should be NULL.
3658 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003659 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3660 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 *
3662 * mode = WILD_FREE: just free previously expanded matches
3663 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3664 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3665 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3666 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3667 * mode = WILD_ALL: return all matches concatenated
3668 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003669 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 *
3671 * options = WILD_LIST_NOTFOUND: list entries without a match
3672 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3673 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3674 * options = WILD_NO_BEEP: Don't beep for multiple matches
3675 * options = WILD_ADD_SLASH: add a slash after directory names
3676 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3677 * options = WILD_SILENT: don't print warning messages
3678 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003679 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 *
3681 * The variables xp->xp_context and xp->xp_backslash must have been set!
3682 */
3683 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003684ExpandOne(
3685 expand_T *xp,
3686 char_u *str,
3687 char_u *orig, /* allocated copy of original of expanded string */
3688 int options,
3689 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
3691 char_u *ss = NULL;
3692 static int findex;
3693 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003694 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 int i;
3696 long_u len;
3697 int non_suf_match; /* number without matching suffix */
3698
3699 /*
3700 * first handle the case of using an old match
3701 */
3702 if (mode == WILD_NEXT || mode == WILD_PREV)
3703 {
3704 if (xp->xp_numfiles > 0)
3705 {
3706 if (mode == WILD_PREV)
3707 {
3708 if (findex == -1)
3709 findex = xp->xp_numfiles;
3710 --findex;
3711 }
3712 else /* mode == WILD_NEXT */
3713 ++findex;
3714
3715 /*
3716 * When wrapping around, return the original string, set findex to
3717 * -1.
3718 */
3719 if (findex < 0)
3720 {
3721 if (orig_save == NULL)
3722 findex = xp->xp_numfiles - 1;
3723 else
3724 findex = -1;
3725 }
3726 if (findex >= xp->xp_numfiles)
3727 {
3728 if (orig_save == NULL)
3729 findex = 0;
3730 else
3731 findex = -1;
3732 }
3733#ifdef FEAT_WILDMENU
3734 if (p_wmnu)
3735 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3736 findex, cmd_showtail);
3737#endif
3738 if (findex == -1)
3739 return vim_strsave(orig_save);
3740 return vim_strsave(xp->xp_files[findex]);
3741 }
3742 else
3743 return NULL;
3744 }
3745
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003746 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3748 {
3749 FreeWild(xp->xp_numfiles, xp->xp_files);
3750 xp->xp_numfiles = -1;
3751 vim_free(orig_save);
3752 orig_save = NULL;
3753 }
3754 findex = 0;
3755
3756 if (mode == WILD_FREE) /* only release file name */
3757 return NULL;
3758
3759 if (xp->xp_numfiles == -1)
3760 {
3761 vim_free(orig_save);
3762 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003763 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
3765 /*
3766 * Do the expansion.
3767 */
3768 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3769 options) == FAIL)
3770 {
3771#ifdef FNAME_ILLEGAL
3772 /* Illegal file name has been silently skipped. But when there
3773 * are wildcards, the real problem is that there was no match,
3774 * causing the pattern to be added, which has illegal characters.
3775 */
3776 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3777 EMSG2(_(e_nomatch2), str);
3778#endif
3779 }
3780 else if (xp->xp_numfiles == 0)
3781 {
3782 if (!(options & WILD_SILENT))
3783 EMSG2(_(e_nomatch2), str);
3784 }
3785 else
3786 {
3787 /* Escape the matches for use on the command line. */
3788 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3789
3790 /*
3791 * Check for matching suffixes in file names.
3792 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003793 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3794 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 {
3796 if (xp->xp_numfiles)
3797 non_suf_match = xp->xp_numfiles;
3798 else
3799 non_suf_match = 1;
3800 if ((xp->xp_context == EXPAND_FILES
3801 || xp->xp_context == EXPAND_DIRECTORIES)
3802 && xp->xp_numfiles > 1)
3803 {
3804 /*
3805 * More than one match; check suffix.
3806 * The files will have been sorted on matching suffix in
3807 * expand_wildcards, only need to check the first two.
3808 */
3809 non_suf_match = 0;
3810 for (i = 0; i < 2; ++i)
3811 if (match_suffix(xp->xp_files[i]))
3812 ++non_suf_match;
3813 }
3814 if (non_suf_match != 1)
3815 {
3816 /* Can we ever get here unless it's while expanding
3817 * interactively? If not, we can get rid of this all
3818 * together. Don't really want to wait for this message
3819 * (and possibly have to hit return to continue!).
3820 */
3821 if (!(options & WILD_SILENT))
3822 EMSG(_(e_toomany));
3823 else if (!(options & WILD_NO_BEEP))
3824 beep_flush();
3825 }
3826 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3827 ss = vim_strsave(xp->xp_files[0]);
3828 }
3829 }
3830 }
3831
3832 /* Find longest common part */
3833 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3834 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003835 int mb_len = 1;
3836 int c0, ci;
3837
3838 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003840#ifdef FEAT_MBYTE
3841 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003843 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3844 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3845 }
3846 else
3847#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003848 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003849 for (i = 1; i < xp->xp_numfiles; ++i)
3850 {
3851#ifdef FEAT_MBYTE
3852 if (has_mbyte)
3853 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3854 else
3855#endif
3856 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003857 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003859 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003860 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003862 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 break;
3864 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003865 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 break;
3867 }
3868 if (i < xp->xp_numfiles)
3869 {
3870 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003871 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 break;
3873 }
3874 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 ss = alloc((unsigned)len + 1);
3877 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003878 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 findex = -1; /* next p_wc gets first one */
3880 }
3881
3882 /* Concatenate all matching names */
3883 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3884 {
3885 len = 0;
3886 for (i = 0; i < xp->xp_numfiles; ++i)
3887 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3888 ss = lalloc(len, TRUE);
3889 if (ss != NULL)
3890 {
3891 *ss = NUL;
3892 for (i = 0; i < xp->xp_numfiles; ++i)
3893 {
3894 STRCAT(ss, xp->xp_files[i]);
3895 if (i != xp->xp_numfiles - 1)
3896 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3897 }
3898 }
3899 }
3900
3901 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3902 ExpandCleanup(xp);
3903
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003904 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003905 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003906 vim_free(orig);
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 return ss;
3909}
3910
3911/*
3912 * Prepare an expand structure for use.
3913 */
3914 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003915ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003917 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003918 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003920#ifndef BACKSLASH_IN_FILENAME
3921 xp->xp_shell = FALSE;
3922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 xp->xp_numfiles = -1;
3924 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003925#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3926 xp->xp_arg = NULL;
3927#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003928 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929}
3930
3931/*
3932 * Cleanup an expand structure after use.
3933 */
3934 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003935ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937 if (xp->xp_numfiles >= 0)
3938 {
3939 FreeWild(xp->xp_numfiles, xp->xp_files);
3940 xp->xp_numfiles = -1;
3941 }
3942}
3943
3944 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003945ExpandEscape(
3946 expand_T *xp,
3947 char_u *str,
3948 int numfiles,
3949 char_u **files,
3950 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951{
3952 int i;
3953 char_u *p;
3954
3955 /*
3956 * May change home directory back to "~"
3957 */
3958 if (options & WILD_HOME_REPLACE)
3959 tilde_replace(str, numfiles, files);
3960
3961 if (options & WILD_ESCAPE)
3962 {
3963 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003964 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003965 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 || xp->xp_context == EXPAND_BUFFERS
3967 || xp->xp_context == EXPAND_DIRECTORIES)
3968 {
3969 /*
3970 * Insert a backslash into a file name before a space, \, %, #
3971 * and wildmatch characters, except '~'.
3972 */
3973 for (i = 0; i < numfiles; ++i)
3974 {
3975 /* for ":set path=" we need to escape spaces twice */
3976 if (xp->xp_backslash == XP_BS_THREE)
3977 {
3978 p = vim_strsave_escaped(files[i], (char_u *)" ");
3979 if (p != NULL)
3980 {
3981 vim_free(files[i]);
3982 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003983#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 p = vim_strsave_escaped(files[i], (char_u *)" ");
3985 if (p != NULL)
3986 {
3987 vim_free(files[i]);
3988 files[i] = p;
3989 }
3990#endif
3991 }
3992 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003993#ifdef BACKSLASH_IN_FILENAME
3994 p = vim_strsave_fnameescape(files[i], FALSE);
3995#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003996 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (p != NULL)
3999 {
4000 vim_free(files[i]);
4001 files[i] = p;
4002 }
4003
4004 /* If 'str' starts with "\~", replace "~" at start of
4005 * files[i] with "\~". */
4006 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004007 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004010
4011 /* If the first file starts with a '+' escape it. Otherwise it
4012 * could be seen as "+cmd". */
4013 if (*files[0] == '+')
4014 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
4016 else if (xp->xp_context == EXPAND_TAGS)
4017 {
4018 /*
4019 * Insert a backslash before characters in a tag name that
4020 * would terminate the ":tag" command.
4021 */
4022 for (i = 0; i < numfiles; ++i)
4023 {
4024 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4025 if (p != NULL)
4026 {
4027 vim_free(files[i]);
4028 files[i] = p;
4029 }
4030 }
4031 }
4032 }
4033}
4034
4035/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004036 * Escape special characters in "fname" for when used as a file name argument
4037 * after a Vim command, or, when "shell" is non-zero, a shell command.
4038 * Returns the result in allocated memory.
4039 */
4040 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004041vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004042{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004043 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004044#ifdef BACKSLASH_IN_FILENAME
4045 char_u buf[20];
4046 int j = 0;
4047
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004048 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004049 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004050 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004051 buf[j++] = *p;
4052 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004053 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004054#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004055 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4056 if (shell && csh_like_shell() && p != NULL)
4057 {
4058 char_u *s;
4059
4060 /* For csh and similar shells need to put two backslashes before '!'.
4061 * One is taken by Vim, one by the shell. */
4062 s = vim_strsave_escaped(p, (char_u *)"!");
4063 vim_free(p);
4064 p = s;
4065 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004066#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004067
4068 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4069 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004070 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004071 escape_fname(&p);
4072
4073 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004074}
4075
4076/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004077 * Put a backslash before the file name in "pp", which is in allocated memory.
4078 */
4079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004080escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004081{
4082 char_u *p;
4083
4084 p = alloc((unsigned)(STRLEN(*pp) + 2));
4085 if (p != NULL)
4086 {
4087 p[0] = '\\';
4088 STRCPY(p + 1, *pp);
4089 vim_free(*pp);
4090 *pp = p;
4091 }
4092}
4093
4094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 * For each file name in files[num_files]:
4096 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4097 */
4098 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004099tilde_replace(
4100 char_u *orig_pat,
4101 int num_files,
4102 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103{
4104 int i;
4105 char_u *p;
4106
4107 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4108 {
4109 for (i = 0; i < num_files; ++i)
4110 {
4111 p = home_replace_save(NULL, files[i]);
4112 if (p != NULL)
4113 {
4114 vim_free(files[i]);
4115 files[i] = p;
4116 }
4117 }
4118 }
4119}
4120
4121/*
4122 * Show all matches for completion on the command line.
4123 * Returns EXPAND_NOTHING when the character that triggered expansion should
4124 * be inserted like a normal character.
4125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004127showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128{
4129#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4130 int num_files;
4131 char_u **files_found;
4132 int i, j, k;
4133 int maxlen;
4134 int lines;
4135 int columns;
4136 char_u *p;
4137 int lastlen;
4138 int attr;
4139 int showtail;
4140
4141 if (xp->xp_numfiles == -1)
4142 {
4143 set_expand_context(xp);
4144 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4145 &num_files, &files_found);
4146 showtail = expand_showtail(xp);
4147 if (i != EXPAND_OK)
4148 return i;
4149
4150 }
4151 else
4152 {
4153 num_files = xp->xp_numfiles;
4154 files_found = xp->xp_files;
4155 showtail = cmd_showtail;
4156 }
4157
4158#ifdef FEAT_WILDMENU
4159 if (!wildmenu)
4160 {
4161#endif
4162 msg_didany = FALSE; /* lines_left will be set */
4163 msg_start(); /* prepare for paging */
4164 msg_putchar('\n');
4165 out_flush();
4166 cmdline_row = msg_row;
4167 msg_didany = FALSE; /* lines_left will be set again */
4168 msg_start(); /* prepare for paging */
4169#ifdef FEAT_WILDMENU
4170 }
4171#endif
4172
4173 if (got_int)
4174 got_int = FALSE; /* only int. the completion, not the cmd line */
4175#ifdef FEAT_WILDMENU
4176 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004177 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178#endif
4179 else
4180 {
4181 /* find the length of the longest file name */
4182 maxlen = 0;
4183 for (i = 0; i < num_files; ++i)
4184 {
4185 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004186 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 || xp->xp_context == EXPAND_BUFFERS))
4188 {
4189 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4190 j = vim_strsize(NameBuff);
4191 }
4192 else
4193 j = vim_strsize(L_SHOWFILE(i));
4194 if (j > maxlen)
4195 maxlen = j;
4196 }
4197
4198 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4199 lines = num_files;
4200 else
4201 {
4202 /* compute the number of columns and lines for the listing */
4203 maxlen += 2; /* two spaces between file names */
4204 columns = ((int)Columns + 2) / maxlen;
4205 if (columns < 1)
4206 columns = 1;
4207 lines = (num_files + columns - 1) / columns;
4208 }
4209
Bram Moolenaar8820b482017-03-16 17:23:31 +01004210 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
4212 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4213 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004214 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 msg_clr_eos();
4216 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004217 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
4219
4220 /* list the files line by line */
4221 for (i = 0; i < lines; ++i)
4222 {
4223 lastlen = 999;
4224 for (k = i; k < num_files; k += lines)
4225 {
4226 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4227 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004228 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 p = files_found[k] + STRLEN(files_found[k]) + 1;
4230 msg_advance(maxlen + 1);
4231 msg_puts(p);
4232 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004233 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 break;
4235 }
4236 for (j = maxlen - lastlen; --j >= 0; )
4237 msg_putchar(' ');
4238 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004239 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 || xp->xp_context == EXPAND_BUFFERS)
4241 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004242 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004243 if (xp->xp_numfiles != -1)
4244 {
4245 char_u *halved_slash;
4246 char_u *exp_path;
4247
4248 /* Expansion was done before and special characters
4249 * were escaped, need to halve backslashes. Also
4250 * $HOME has been replaced with ~/. */
4251 exp_path = expand_env_save_opt(files_found[k], TRUE);
4252 halved_slash = backslash_halve_save(
4253 exp_path != NULL ? exp_path : files_found[k]);
4254 j = mch_isdir(halved_slash != NULL ? halved_slash
4255 : files_found[k]);
4256 vim_free(exp_path);
4257 vim_free(halved_slash);
4258 }
4259 else
4260 /* Expansion was done here, file names are literal. */
4261 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 if (showtail)
4263 p = L_SHOWFILE(k);
4264 else
4265 {
4266 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4267 TRUE);
4268 p = NameBuff;
4269 }
4270 }
4271 else
4272 {
4273 j = FALSE;
4274 p = L_SHOWFILE(k);
4275 }
4276 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4277 }
4278 if (msg_col > 0) /* when not wrapped around */
4279 {
4280 msg_clr_eos();
4281 msg_putchar('\n');
4282 }
4283 out_flush(); /* show one line at a time */
4284 if (got_int)
4285 {
4286 got_int = FALSE;
4287 break;
4288 }
4289 }
4290
4291 /*
4292 * we redraw the command below the lines that we have just listed
4293 * This is a bit tricky, but it saves a lot of screen updating.
4294 */
4295 cmdline_row = msg_row; /* will put it back later */
4296 }
4297
4298 if (xp->xp_numfiles == -1)
4299 FreeWild(num_files, files_found);
4300
4301 return EXPAND_OK;
4302}
4303
4304/*
4305 * Private gettail for showmatches() (and win_redr_status_matches()):
4306 * Find tail of file name path, but ignore trailing "/".
4307 */
4308 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004309sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310{
4311 char_u *p;
4312 char_u *t = s;
4313 int had_sep = FALSE;
4314
4315 for (p = s; *p != NUL; )
4316 {
4317 if (vim_ispathsep(*p)
4318#ifdef BACKSLASH_IN_FILENAME
4319 && !rem_backslash(p)
4320#endif
4321 )
4322 had_sep = TRUE;
4323 else if (had_sep)
4324 {
4325 t = p;
4326 had_sep = FALSE;
4327 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004328 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
4330 return t;
4331}
4332
4333/*
4334 * Return TRUE if we only need to show the tail of completion matches.
4335 * When not completing file names or there is a wildcard in the path FALSE is
4336 * returned.
4337 */
4338 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004339expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
4341 char_u *s;
4342 char_u *end;
4343
4344 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004345 if (xp->xp_context != EXPAND_FILES
4346 && xp->xp_context != EXPAND_SHELLCMD
4347 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 return FALSE;
4349
4350 end = gettail(xp->xp_pattern);
4351 if (end == xp->xp_pattern) /* there is no path separator */
4352 return FALSE;
4353
4354 for (s = xp->xp_pattern; s < end; s++)
4355 {
4356 /* Skip escaped wildcards. Only when the backslash is not a path
4357 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4358 if (rem_backslash(s))
4359 ++s;
4360 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4361 return FALSE;
4362 }
4363 return TRUE;
4364}
4365
4366/*
4367 * Prepare a string for expansion.
4368 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004369 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 * When expanding other names: The string will be used with regcomp(). Copy
4371 * the name into allocated memory and prepend "^".
4372 */
4373 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004374addstar(
4375 char_u *fname,
4376 int len,
4377 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378{
4379 char_u *retval;
4380 int i, j;
4381 int new_len;
4382 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004383 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004385 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004386 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004387 && context != EXPAND_SHELLCMD
4388 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
4390 /*
4391 * Matching will be done internally (on something other than files).
4392 * So we convert the file-matching-type wildcards into our kind for
4393 * use with vim_regcomp(). First work out how long it will be:
4394 */
4395
4396 /* For help tags the translation is done in find_help_tags().
4397 * For a tag pattern starting with "/" no translation is needed. */
4398 if (context == EXPAND_HELP
4399 || context == EXPAND_COLORS
4400 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004401 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004402 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004403 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004404 || ((context == EXPAND_TAGS_LISTFILES
4405 || context == EXPAND_TAGS)
4406 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 retval = vim_strnsave(fname, len);
4408 else
4409 {
4410 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4411 for (i = 0; i < len; i++)
4412 {
4413 if (fname[i] == '*' || fname[i] == '~')
4414 new_len++; /* '*' needs to be replaced by ".*"
4415 '~' needs to be replaced by "\~" */
4416
4417 /* Buffer names are like file names. "." should be literal */
4418 if (context == EXPAND_BUFFERS && fname[i] == '.')
4419 new_len++; /* "." becomes "\." */
4420
4421 /* Custom expansion takes care of special things, match
4422 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004423 if ((context == EXPAND_USER_DEFINED
4424 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 new_len++; /* '\' becomes "\\" */
4426 }
4427 retval = alloc(new_len);
4428 if (retval != NULL)
4429 {
4430 retval[0] = '^';
4431 j = 1;
4432 for (i = 0; i < len; i++, j++)
4433 {
4434 /* Skip backslash. But why? At least keep it for custom
4435 * expansion. */
4436 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004437 && context != EXPAND_USER_LIST
4438 && fname[i] == '\\'
4439 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 break;
4441
4442 switch (fname[i])
4443 {
4444 case '*': retval[j++] = '.';
4445 break;
4446 case '~': retval[j++] = '\\';
4447 break;
4448 case '?': retval[j] = '.';
4449 continue;
4450 case '.': if (context == EXPAND_BUFFERS)
4451 retval[j++] = '\\';
4452 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004453 case '\\': if (context == EXPAND_USER_DEFINED
4454 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 retval[j++] = '\\';
4456 break;
4457 }
4458 retval[j] = fname[i];
4459 }
4460 retval[j] = NUL;
4461 }
4462 }
4463 }
4464 else
4465 {
4466 retval = alloc(len + 4);
4467 if (retval != NULL)
4468 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004469 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004472 * Don't add a star to *, ~, ~user, $var or `cmd`.
4473 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 * ~ would be at the start of the file name, but not the tail.
4475 * $ could be anywhere in the tail.
4476 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004477 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 */
4479 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004480 ends_in_star = (len > 0 && retval[len - 1] == '*');
4481#ifndef BACKSLASH_IN_FILENAME
4482 for (i = len - 2; i >= 0; --i)
4483 {
4484 if (retval[i] != '\\')
4485 break;
4486 ends_in_star = !ends_in_star;
4487 }
4488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004490 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 && vim_strchr(tail, '$') == NULL
4492 && vim_strchr(retval, '`') == NULL)
4493 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004494 else if (len > 0 && retval[len - 1] == '$')
4495 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 retval[len] = NUL;
4497 }
4498 }
4499 return retval;
4500}
4501
4502/*
4503 * Must parse the command line so far to work out what context we are in.
4504 * Completion can then be done based on that context.
4505 * This routine sets the variables:
4506 * xp->xp_pattern The start of the pattern to be expanded within
4507 * the command line (ends at the cursor).
4508 * xp->xp_context The type of thing to expand. Will be one of:
4509 *
4510 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4511 * the command line, like an unknown command. Caller
4512 * should beep.
4513 * EXPAND_NOTHING Unrecognised context for completion, use char like
4514 * a normal char, rather than for completion. eg
4515 * :s/^I/
4516 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4517 * it.
4518 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4519 * EXPAND_FILES After command with XFILE set, or after setting
4520 * with P_EXPAND set. eg :e ^I, :w>>^I
4521 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4522 * when we know only directories are of interest. eg
4523 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004524 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4526 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4527 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4528 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4529 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4530 * EXPAND_EVENTS Complete event names
4531 * EXPAND_SYNTAX Complete :syntax command arguments
4532 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4533 * EXPAND_AUGROUP Complete autocommand group names
4534 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4535 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4536 * eg :unmap a^I , :cunab x^I
4537 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4538 * eg :call sub^I
4539 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4540 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4541 * names in expressions, eg :while s^I
4542 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004543 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 */
4545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004546set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004548 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (ccline.cmdfirstc != ':'
4550#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004551 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004552 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553#endif
4554 )
4555 {
4556 xp->xp_context = EXPAND_NOTHING;
4557 return;
4558 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004559 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560}
4561
4562 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004563set_cmd_context(
4564 expand_T *xp,
4565 char_u *str, /* start of command line */
4566 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004567 int col, /* position of cursor */
4568 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569{
4570 int old_char = NUL;
4571 char_u *nextcomm;
4572
4573 /*
4574 * Avoid a UMR warning from Purify, only save the character if it has been
4575 * written before.
4576 */
4577 if (col < len)
4578 old_char = str[col];
4579 str[col] = NUL;
4580 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004581
4582#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004583 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004584 {
4585# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004586 /* pass CMD_SIZE because there is no real command */
4587 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004588# endif
4589 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004590 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004591 {
4592 xp->xp_context = ccline.xp_context;
4593 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004594# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004595 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004596# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004597 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004598 else
4599#endif
4600 while (nextcomm != NULL)
4601 nextcomm = set_one_cmd_context(xp, nextcomm);
4602
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004603 /* Store the string here so that call_user_expand_func() can get to them
4604 * easily. */
4605 xp->xp_line = str;
4606 xp->xp_col = col;
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 str[col] = old_char;
4609}
4610
4611/*
4612 * Expand the command line "str" from context "xp".
4613 * "xp" must have been set by set_cmd_context().
4614 * xp->xp_pattern points into "str", to where the text that is to be expanded
4615 * starts.
4616 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4617 * cursor.
4618 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4619 * key that triggered expansion literally.
4620 * Returns EXPAND_OK otherwise.
4621 */
4622 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004623expand_cmdline(
4624 expand_T *xp,
4625 char_u *str, /* start of command line */
4626 int col, /* position of cursor */
4627 int *matchcount, /* return: nr of matches */
4628 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004631 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
4633 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4634 {
4635 beep_flush();
4636 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4637 }
4638 if (xp->xp_context == EXPAND_NOTHING)
4639 {
4640 /* Caller can use the character as a normal char instead */
4641 return EXPAND_NOTHING;
4642 }
4643
4644 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004645 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4646 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (file_str == NULL)
4648 return EXPAND_UNSUCCESSFUL;
4649
Bram Moolenaar94950a92010-12-02 16:01:29 +01004650 if (p_wic)
4651 options += WILD_ICASE;
4652
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004654 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
4656 *matchcount = 0;
4657 *matches = NULL;
4658 }
4659 vim_free(file_str);
4660
4661 return EXPAND_OK;
4662}
4663
4664#ifdef FEAT_MULTI_LANG
4665/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004666 * Cleanup matches for help tags:
4667 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4668 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004670static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004673cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 int i, j;
4676 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004677 char_u buf[4];
4678 char_u *p = buf;
4679
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004680 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004681 {
4682 *p++ = '@';
4683 *p++ = p_hlg[0];
4684 *p++ = p_hlg[1];
4685 }
4686 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
4688 for (i = 0; i < num_file; ++i)
4689 {
4690 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004691 if (len <= 0)
4692 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004693 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
4695 /* Sorting on priority means the same item in another language may
4696 * be anywhere. Search all items for a match up to the "@en". */
4697 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004698 if (j != i && (int)STRLEN(file[j]) == len + 3
4699 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 break;
4701 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004702 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 file[i][len] = NUL;
4704 }
4705 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004706
4707 if (*buf != NUL)
4708 for (i = 0; i < num_file; ++i)
4709 {
4710 len = (int)STRLEN(file[i]) - 3;
4711 if (len <= 0)
4712 continue;
4713 if (STRCMP(file[i] + len, buf) == 0)
4714 {
4715 /* remove the default language */
4716 file[i][len] = NUL;
4717 }
4718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719}
4720#endif
4721
4722/*
4723 * Do the expansion based on xp->xp_context and "pat".
4724 */
4725 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004726ExpandFromContext(
4727 expand_T *xp,
4728 char_u *pat,
4729 int *num_file,
4730 char_u ***file,
4731 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
4733#ifdef FEAT_CMDL_COMPL
4734 regmatch_T regmatch;
4735#endif
4736 int ret;
4737 int flags;
4738
4739 flags = EW_DIR; /* include directories */
4740 if (options & WILD_LIST_NOTFOUND)
4741 flags |= EW_NOTFOUND;
4742 if (options & WILD_ADD_SLASH)
4743 flags |= EW_ADDSLASH;
4744 if (options & WILD_KEEP_ALL)
4745 flags |= EW_KEEPALL;
4746 if (options & WILD_SILENT)
4747 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004748 if (options & WILD_ALLLINKS)
4749 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004751 if (xp->xp_context == EXPAND_FILES
4752 || xp->xp_context == EXPAND_DIRECTORIES
4753 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
4755 /*
4756 * Expand file or directory names.
4757 */
4758 int free_pat = FALSE;
4759 int i;
4760
4761 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4762 * space */
4763 if (xp->xp_backslash != XP_BS_NONE)
4764 {
4765 free_pat = TRUE;
4766 pat = vim_strsave(pat);
4767 for (i = 0; pat[i]; ++i)
4768 if (pat[i] == '\\')
4769 {
4770 if (xp->xp_backslash == XP_BS_THREE
4771 && pat[i + 1] == '\\'
4772 && pat[i + 2] == '\\'
4773 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004774 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 if (xp->xp_backslash == XP_BS_ONE
4776 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004777 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 }
4779 }
4780
4781 if (xp->xp_context == EXPAND_FILES)
4782 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004783 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4784 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 else
4786 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004787 if (options & WILD_ICASE)
4788 flags |= EW_ICASE;
4789
Bram Moolenaard7834d32009-12-02 16:14:36 +00004790 /* Expand wildcards, supporting %:h and the like. */
4791 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (free_pat)
4793 vim_free(pat);
4794 return ret;
4795 }
4796
4797 *file = (char_u **)"";
4798 *num_file = 0;
4799 if (xp->xp_context == EXPAND_HELP)
4800 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004801 /* With an empty argument we would get all the help tags, which is
4802 * very slow. Get matches for "help" instead. */
4803 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4804 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806#ifdef FEAT_MULTI_LANG
4807 cleanup_help_tags(*num_file, *file);
4808#endif
4809 return OK;
4810 }
4811 return FAIL;
4812 }
4813
4814#ifndef FEAT_CMDL_COMPL
4815 return FAIL;
4816#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004817 if (xp->xp_context == EXPAND_SHELLCMD)
4818 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 if (xp->xp_context == EXPAND_OLD_SETTING)
4820 return ExpandOldSetting(num_file, file);
4821 if (xp->xp_context == EXPAND_BUFFERS)
4822 return ExpandBufnames(pat, num_file, file, options);
4823 if (xp->xp_context == EXPAND_TAGS
4824 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4825 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4826 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004827 {
4828 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004829 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4830 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004833 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004834 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004835 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004836 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004837 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004838 {
4839 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004840 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004841 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004842 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004843 {
4844 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004845 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004846 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004847# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4848 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004849 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004850# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004851 if (xp->xp_context == EXPAND_PACKADD)
4852 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4855 if (regmatch.regprog == NULL)
4856 return FAIL;
4857
4858 /* set ignore-case according to p_ic, p_scs and pat */
4859 regmatch.rm_ic = ignorecase(pat);
4860
4861 if (xp->xp_context == EXPAND_SETTINGS
4862 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4863 ret = ExpandSettings(xp, &regmatch, num_file, file);
4864 else if (xp->xp_context == EXPAND_MAPPINGS)
4865 ret = ExpandMappings(&regmatch, num_file, file);
4866# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4867 else if (xp->xp_context == EXPAND_USER_DEFINED)
4868 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4869# endif
4870 else
4871 {
4872 static struct expgen
4873 {
4874 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004875 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004877 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 } tab[] =
4879 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004880 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4881 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004882 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004883#ifdef FEAT_CMDHIST
4884 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004887 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004888 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004889 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4890 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4891 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#endif
4893#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004894 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4895 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4896 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4897 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#endif
4899#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004900 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4901 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#endif
4903#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004904 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004906#ifdef FEAT_PROFILE
4907 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4908#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004909 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004911 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4912 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004914#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004915 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004916#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004917#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004918 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004919#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004920#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004921 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4924 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004925 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4926 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004928 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004929 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 };
4931 int i;
4932
4933 /*
4934 * Find a context in the table and call the ExpandGeneric() with the
4935 * right function to do the expansion.
4936 */
4937 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004938 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (xp->xp_context == tab[i].context)
4940 {
4941 if (tab[i].ic)
4942 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004943 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004944 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 break;
4946 }
4947 }
4948
Bram Moolenaar473de612013-06-08 18:19:48 +02004949 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 return ret;
4952#endif /* FEAT_CMDL_COMPL */
4953}
4954
4955#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4956/*
4957 * Expand a list of names.
4958 *
4959 * Generic function for command line completion. It calls a function to
4960 * obtain strings, one by one. The strings are matched against a regexp
4961 * program. Matching strings are copied into an array, which is returned.
4962 *
4963 * Returns OK when no problems encountered, FAIL for error (out of memory).
4964 */
4965 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004966ExpandGeneric(
4967 expand_T *xp,
4968 regmatch_T *regmatch,
4969 int *num_file,
4970 char_u ***file,
4971 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004973 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
4975 int i;
4976 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004977 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 char_u *str;
4979
4980 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004981 * round == 0: count the number of matching names
4982 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004984 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 for (i = 0; ; ++i)
4987 {
4988 str = (*func)(xp, i);
4989 if (str == NULL) /* end of list */
4990 break;
4991 if (*str == NUL) /* skip empty strings */
4992 continue;
4993
4994 if (vim_regexec(regmatch, str, (colnr_T)0))
4995 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004996 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004998 if (escaped)
4999 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5000 else
5001 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 (*file)[count] = str;
5003#ifdef FEAT_MENU
5004 if (func == get_menu_names && str != NULL)
5005 {
5006 /* test for separator added by get_menu_names() */
5007 str += STRLEN(str) - 1;
5008 if (*str == '\001')
5009 *str = '.';
5010 }
5011#endif
5012 }
5013 ++count;
5014 }
5015 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005016 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
5018 if (count == 0)
5019 return OK;
5020 *num_file = count;
5021 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5022 if (*file == NULL)
5023 {
5024 *file = (char_u **)"";
5025 return FAIL;
5026 }
5027 count = 0;
5028 }
5029 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005030
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005031 /* Sort the results. Keep menu's in the specified order. */
5032 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005033 {
5034 if (xp->xp_context == EXPAND_EXPRESSION
5035 || xp->xp_context == EXPAND_FUNCTIONS
5036 || xp->xp_context == EXPAND_USER_FUNC)
5037 /* <SNR> functions should be sorted to the end. */
5038 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5039 sort_func_compare);
5040 else
5041 sort_strings(*file, *num_file);
5042 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005043
Bram Moolenaar4f688582007-07-24 12:34:30 +00005044#ifdef FEAT_CMDL_COMPL
5045 /* Reset the variables used for special highlight names expansion, so that
5046 * they don't show up when getting normal highlight names by ID. */
5047 reset_expand_highlight();
5048#endif
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 return OK;
5051}
5052
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005053/*
5054 * Complete a shell command.
5055 * Returns FAIL or OK;
5056 */
5057 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005058expand_shellcmd(
5059 char_u *filepat, /* pattern to match with command names */
5060 int *num_file, /* return: number of matches */
5061 char_u ***file, /* return: array with matches */
5062 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005063{
5064 char_u *pat;
5065 int i;
5066 char_u *path;
5067 int mustfree = FALSE;
5068 garray_T ga;
5069 char_u *buf = alloc(MAXPATHL);
5070 size_t l;
5071 char_u *s, *e;
5072 int flags = flagsarg;
5073 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005074 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005075
5076 if (buf == NULL)
5077 return FAIL;
5078
5079 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5080 * space */
5081 pat = vim_strsave(filepat);
5082 for (i = 0; pat[i]; ++i)
5083 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005084 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005085
Bram Moolenaarb5971142015-03-21 17:32:19 +01005086 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005087
5088 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005089 if (mch_isFullName(pat))
5090 path = (char_u *)" ";
5091 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005092 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5093 path = (char_u *)".";
5094 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005095 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005096 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005097 if (path == NULL)
5098 path = (char_u *)"";
5099 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005100
5101 /*
5102 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005103 * collect them in "ga". When "." is not in $PATH also expand for the
5104 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005105 */
5106 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005107 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005108 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005109 if (*s == NUL)
5110 {
5111 if (did_curdir)
5112 break;
5113 /* Find directories in the current directory, path is empty. */
5114 did_curdir = TRUE;
5115 }
5116 else if (*s == '.')
5117 did_curdir = TRUE;
5118
Bram Moolenaar68c31742006-09-02 15:54:18 +00005119 if (*s == ' ')
5120 ++s; /* Skip space used for absolute path name. */
5121
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005122#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005123 e = vim_strchr(s, ';');
5124#else
5125 e = vim_strchr(s, ':');
5126#endif
5127 if (e == NULL)
5128 e = s + STRLEN(s);
5129
5130 l = e - s;
5131 if (l > MAXPATHL - 5)
5132 break;
5133 vim_strncpy(buf, s, l);
5134 add_pathsep(buf);
5135 l = STRLEN(buf);
5136 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5137
5138 /* Expand matches in one directory of $PATH. */
5139 ret = expand_wildcards(1, &buf, num_file, file, flags);
5140 if (ret == OK)
5141 {
5142 if (ga_grow(&ga, *num_file) == FAIL)
5143 FreeWild(*num_file, *file);
5144 else
5145 {
5146 for (i = 0; i < *num_file; ++i)
5147 {
5148 s = (*file)[i];
5149 if (STRLEN(s) > l)
5150 {
5151 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005152 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005153 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5154 }
5155 else
5156 vim_free(s);
5157 }
5158 vim_free(*file);
5159 }
5160 }
5161 if (*e != NUL)
5162 ++e;
5163 }
5164 *file = ga.ga_data;
5165 *num_file = ga.ga_len;
5166
5167 vim_free(buf);
5168 vim_free(pat);
5169 if (mustfree)
5170 vim_free(path);
5171 return OK;
5172}
5173
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005176static 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 +00005177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005179 * Call "user_expand_func()" to invoke a user defined Vim script function and
5180 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005182 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005183call_user_expand_func(
5184 void *(*user_expand_func)(char_u *, int, char_u **, int),
5185 expand_T *xp,
5186 int *num_file,
5187 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005189 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005190 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005193 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005194 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
Bram Moolenaarb7515462013-06-29 12:58:33 +02005196 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005197 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 *num_file = 0;
5199 *file = NULL;
5200
Bram Moolenaarb7515462013-06-29 12:58:33 +02005201 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005202 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005203 keep = ccline.cmdbuff[ccline.cmdlen];
5204 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005205 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005206
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005207 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005208 args[1] = xp->xp_line;
5209 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 args[2] = num;
5211
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005212 /* Save the cmdline, we don't know what the function may do. */
5213 save_ccline = ccline;
5214 ccline.cmdbuff = NULL;
5215 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005217
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005218 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005219
5220 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005222 if (ccline.cmdbuff != NULL)
5223 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005224
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005225 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005226 return ret;
5227}
5228
5229/*
5230 * Expand names with a function defined by the user.
5231 */
5232 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005233ExpandUserDefined(
5234 expand_T *xp,
5235 regmatch_T *regmatch,
5236 int *num_file,
5237 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005238{
5239 char_u *retstr;
5240 char_u *s;
5241 char_u *e;
5242 char_u keep;
5243 garray_T ga;
5244
5245 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5246 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 return FAIL;
5248
5249 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005250 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
5252 e = vim_strchr(s, '\n');
5253 if (e == NULL)
5254 e = s + STRLEN(s);
5255 keep = *e;
5256 *e = 0;
5257
5258 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5259 {
5260 *e = keep;
5261 if (*e != NUL)
5262 ++e;
5263 continue;
5264 }
5265
5266 if (ga_grow(&ga, 1) == FAIL)
5267 break;
5268
5269 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5270 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
5272 *e = keep;
5273 if (*e != NUL)
5274 ++e;
5275 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005276 vim_free(retstr);
5277 *file = ga.ga_data;
5278 *num_file = ga.ga_len;
5279 return OK;
5280}
5281
5282/*
5283 * Expand names with a list returned by a function defined by the user.
5284 */
5285 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005286ExpandUserList(
5287 expand_T *xp,
5288 int *num_file,
5289 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005290{
5291 list_T *retlist;
5292 listitem_T *li;
5293 garray_T ga;
5294
5295 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5296 if (retlist == NULL)
5297 return FAIL;
5298
5299 ga_init2(&ga, (int)sizeof(char *), 3);
5300 /* Loop over the items in the list. */
5301 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5302 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005303 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5304 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005305
5306 if (ga_grow(&ga, 1) == FAIL)
5307 break;
5308
5309 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005310 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005311 ++ga.ga_len;
5312 }
5313 list_unref(retlist);
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 *file = ga.ga_data;
5316 *num_file = ga.ga_len;
5317 return OK;
5318}
5319#endif
5320
5321/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005322 * Expand color scheme, compiler or filetype names.
5323 * Search from 'runtimepath':
5324 * 'runtimepath'/{dirnames}/{pat}.vim
5325 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5326 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5327 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5328 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005329 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 */
5331 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005332ExpandRTDir(
5333 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005334 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005335 int *num_file,
5336 char_u ***file,
5337 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 char_u *s;
5340 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005341 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005343 int i;
5344 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345
5346 *num_file = 0;
5347 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005348 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005349 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005351 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005353 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5354 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005356 ga_clear_strings(&ga);
5357 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005359 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005360 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005361 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005363
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005364 if (flags & DIP_START) {
5365 for (i = 0; dirnames[i] != NULL; ++i)
5366 {
5367 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5368 if (s == NULL)
5369 {
5370 ga_clear_strings(&ga);
5371 return FAIL;
5372 }
5373 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5374 globpath(p_pp, s, &ga, 0);
5375 vim_free(s);
5376 }
5377 }
5378
5379 if (flags & DIP_OPT) {
5380 for (i = 0; dirnames[i] != NULL; ++i)
5381 {
5382 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5383 if (s == NULL)
5384 {
5385 ga_clear_strings(&ga);
5386 return FAIL;
5387 }
5388 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5389 globpath(p_pp, s, &ga, 0);
5390 vim_free(s);
5391 }
5392 }
5393
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005394 for (i = 0; i < ga.ga_len; ++i)
5395 {
5396 match = ((char_u **)ga.ga_data)[i];
5397 s = match;
5398 e = s + STRLEN(s);
5399 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5400 {
5401 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005402 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005403 if (s < match || vim_ispathsep(*s))
5404 break;
5405 ++s;
5406 *e = NUL;
5407 mch_memmove(match, s, e - s + 1);
5408 }
5409 }
5410
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005411 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005412 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005413
5414 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005415 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005416 remove_duplicates(&ga);
5417
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 *file = ga.ga_data;
5419 *num_file = ga.ga_len;
5420 return OK;
5421}
5422
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005423/*
5424 * Expand loadplugin names:
5425 * 'packpath'/pack/ * /opt/{pat}
5426 */
5427 static int
5428ExpandPackAddDir(
5429 char_u *pat,
5430 int *num_file,
5431 char_u ***file)
5432{
5433 char_u *s;
5434 char_u *e;
5435 char_u *match;
5436 garray_T ga;
5437 int i;
5438 int pat_len;
5439
5440 *num_file = 0;
5441 *file = NULL;
5442 pat_len = (int)STRLEN(pat);
5443 ga_init2(&ga, (int)sizeof(char *), 10);
5444
5445 s = alloc((unsigned)(pat_len + 26));
5446 if (s == NULL)
5447 {
5448 ga_clear_strings(&ga);
5449 return FAIL;
5450 }
5451 sprintf((char *)s, "pack/*/opt/%s*", pat);
5452 globpath(p_pp, s, &ga, 0);
5453 vim_free(s);
5454
5455 for (i = 0; i < ga.ga_len; ++i)
5456 {
5457 match = ((char_u **)ga.ga_data)[i];
5458 s = gettail(match);
5459 e = s + STRLEN(s);
5460 mch_memmove(match, s, e - s + 1);
5461 }
5462
5463 if (ga.ga_len == 0)
5464 return FAIL;
5465
5466 /* Sort and remove duplicates which can happen when specifying multiple
5467 * directories in dirnames. */
5468 remove_duplicates(&ga);
5469
5470 *file = ga.ga_data;
5471 *num_file = ga.ga_len;
5472 return OK;
5473}
5474
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475#endif
5476
5477#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5478/*
5479 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005480 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483globpath(
5484 char_u *path,
5485 char_u *file,
5486 garray_T *ga,
5487 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 expand_T xpc;
5490 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 int num_p;
5493 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
5495 buf = alloc(MAXPATHL);
5496 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005497 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005499 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005501
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 /* Loop over all entries in {path}. */
5503 while (*path != NUL)
5504 {
5505 /* Copy one item of the path to buf[] and concatenate the file name. */
5506 copy_option_part(&path, buf, MAXPATHL, ",");
5507 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5508 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005509# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005510 /* Using the platform's path separator (\) makes vim incorrectly
5511 * treat it as an escape character, use '/' instead. */
5512 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5513 STRCAT(buf, "/");
5514# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005516# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005518 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5519 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005521 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005523 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 for (i = 0; i < num_p; ++i)
5526 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005527 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005528 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005529 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 FreeWild(num_p, p);
5534 }
5535 }
5536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539}
5540
5541#endif
5542
5543#if defined(FEAT_CMDHIST) || defined(PROTO)
5544
5545/*********************************
5546 * Command line history stuff *
5547 *********************************/
5548
5549/*
5550 * Translate a history character to the associated type number.
5551 */
5552 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 if (c == ':')
5556 return HIST_CMD;
5557 if (c == '=')
5558 return HIST_EXPR;
5559 if (c == '@')
5560 return HIST_INPUT;
5561 if (c == '>')
5562 return HIST_DEBUG;
5563 return HIST_SEARCH; /* must be '?' or '/' */
5564}
5565
5566/*
5567 * Table of history names.
5568 * These names are used in :history and various hist...() functions.
5569 * It is sufficient to give the significant prefix of a history name.
5570 */
5571
5572static char *(history_names[]) =
5573{
5574 "cmd",
5575 "search",
5576 "expr",
5577 "input",
5578 "debug",
5579 NULL
5580};
5581
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005582#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5583/*
5584 * Function given to ExpandGeneric() to obtain the possible first
5585 * arguments of the ":history command.
5586 */
5587 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005588get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005589{
5590 static char_u compl[2] = { NUL, NUL };
5591 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005592 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005593 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5594
5595 if (idx < short_names_count)
5596 {
5597 compl[0] = (char_u)short_names[idx];
5598 return compl;
5599 }
5600 if (idx < short_names_count + history_name_count)
5601 return (char_u *)history_names[idx - short_names_count];
5602 if (idx == short_names_count + history_name_count)
5603 return (char_u *)"all";
5604 return NULL;
5605}
5606#endif
5607
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608/*
5609 * init_history() - Initialize the command line history.
5610 * Also used to re-allocate the history when the size changes.
5611 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005612 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 int newlen; /* new length of history table */
5616 histentry_T *temp;
5617 int i;
5618 int j;
5619 int type;
5620
5621 /*
5622 * If size of history table changed, reallocate it
5623 */
5624 newlen = (int)p_hi;
5625 if (newlen != hislen) /* history length changed */
5626 {
5627 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5628 {
5629 if (newlen)
5630 {
5631 temp = (histentry_T *)lalloc(
5632 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5633 if (temp == NULL) /* out of memory! */
5634 {
5635 if (type == 0) /* first one: just keep the old length */
5636 {
5637 newlen = hislen;
5638 break;
5639 }
5640 /* Already changed one table, now we can only have zero
5641 * length for all tables. */
5642 newlen = 0;
5643 type = -1;
5644 continue;
5645 }
5646 }
5647 else
5648 temp = NULL;
5649 if (newlen == 0 || temp != NULL)
5650 {
5651 if (hisidx[type] < 0) /* there are no entries yet */
5652 {
5653 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005654 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
5656 else if (newlen > hislen) /* array becomes bigger */
5657 {
5658 for (i = 0; i <= hisidx[type]; ++i)
5659 temp[i] = history[type][i];
5660 j = i;
5661 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005662 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 for ( ; j < hislen; ++i, ++j)
5664 temp[i] = history[type][j];
5665 }
5666 else /* array becomes smaller or 0 */
5667 {
5668 j = hisidx[type];
5669 for (i = newlen - 1; ; --i)
5670 {
5671 if (i >= 0) /* copy newest entries */
5672 temp[i] = history[type][j];
5673 else /* remove older entries */
5674 vim_free(history[type][j].hisstr);
5675 if (--j < 0)
5676 j = hislen - 1;
5677 if (j == hisidx[type])
5678 break;
5679 }
5680 hisidx[type] = newlen - 1;
5681 }
5682 vim_free(history[type]);
5683 history[type] = temp;
5684 }
5685 }
5686 hislen = newlen;
5687 }
5688}
5689
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005691clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005692{
5693 hisptr->hisnum = 0;
5694 hisptr->viminfo = FALSE;
5695 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005696 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005697}
5698
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699/*
5700 * Check if command line 'str' is already in history.
5701 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5702 */
5703 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005704in_history(
5705 int type,
5706 char_u *str,
5707 int move_to_front, /* Move the entry to the front if it exists */
5708 int sep,
5709 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 int i;
5712 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005713 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
5715 if (hisidx[type] < 0)
5716 return FALSE;
5717 i = hisidx[type];
5718 do
5719 {
5720 if (history[type][i].hisstr == NULL)
5721 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005722
5723 /* For search history, check that the separator character matches as
5724 * well. */
5725 p = history[type][i].hisstr;
5726 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005727 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005728 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
5730 if (!move_to_front)
5731 return TRUE;
5732 last_i = i;
5733 break;
5734 }
5735 if (--i < 0)
5736 i = hislen - 1;
5737 } while (i != hisidx[type]);
5738
5739 if (last_i >= 0)
5740 {
5741 str = history[type][i].hisstr;
5742 while (i != hisidx[type])
5743 {
5744 if (++i >= hislen)
5745 i = 0;
5746 history[type][last_i] = history[type][i];
5747 last_i = i;
5748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005750 history[type][i].viminfo = FALSE;
5751 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005752 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 return TRUE;
5754 }
5755 return FALSE;
5756}
5757
5758/*
5759 * Convert history name (from table above) to its HIST_ equivalent.
5760 * When "name" is empty, return "cmd" history.
5761 * Returns -1 for unknown history name.
5762 */
5763 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005764get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766 int i;
5767 int len = (int)STRLEN(name);
5768
5769 /* No argument: use current history. */
5770 if (len == 0)
5771 return hist_char2type(ccline.cmdfirstc);
5772
5773 for (i = 0; history_names[i] != NULL; ++i)
5774 if (STRNICMP(name, history_names[i], len) == 0)
5775 return i;
5776
5777 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5778 return hist_char2type(name[0]);
5779
5780 return -1;
5781}
5782
5783static int last_maptick = -1; /* last seen maptick */
5784
5785/*
5786 * Add the given string to the given history. If the string is already in the
5787 * history then it is moved to the front. "histype" may be one of he HIST_
5788 * values.
5789 */
5790 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005791add_to_history(
5792 int histype,
5793 char_u *new_entry,
5794 int in_map, /* consider maptick when inside a mapping */
5795 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
5797 histentry_T *hisptr;
5798 int len;
5799
5800 if (hislen == 0) /* no history */
5801 return;
5802
Bram Moolenaara939e432013-11-09 05:30:26 +01005803 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5804 return;
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 /*
5807 * Searches inside the same mapping overwrite each other, so that only
5808 * the last line is kept. Be careful not to remove a line that was moved
5809 * down, only lines that were added.
5810 */
5811 if (histype == HIST_SEARCH && in_map)
5812 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005813 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 {
5815 /* Current line is from the same mapping, remove it */
5816 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5817 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005818 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 --hisnum[histype];
5820 if (--hisidx[HIST_SEARCH] < 0)
5821 hisidx[HIST_SEARCH] = hislen - 1;
5822 }
5823 last_maptick = -1;
5824 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005825 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {
5827 if (++hisidx[histype] == hislen)
5828 hisidx[histype] = 0;
5829 hisptr = &history[histype][hisidx[histype]];
5830 vim_free(hisptr->hisstr);
5831
5832 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005833 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5835 if (hisptr->hisstr != NULL)
5836 hisptr->hisstr[len + 1] = sep;
5837
5838 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005839 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005840 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 if (histype == HIST_SEARCH && in_map)
5842 last_maptick = maptick;
5843 }
5844}
5845
5846#if defined(FEAT_EVAL) || defined(PROTO)
5847
5848/*
5849 * Get identifier of newest history entry.
5850 * "histype" may be one of the HIST_ values.
5851 */
5852 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005853get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5856 || hisidx[histype] < 0)
5857 return -1;
5858
5859 return history[histype][hisidx[histype]].hisnum;
5860}
5861
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 * Calculate history index from a number:
5864 * num > 0: seen as identifying number of a history entry
5865 * num < 0: relative position in history wrt newest entry
5866 * "histype" may be one of the HIST_ values.
5867 */
5868 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005869calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
5871 int i;
5872 histentry_T *hist;
5873 int wrapped = FALSE;
5874
5875 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5876 || (i = hisidx[histype]) < 0 || num == 0)
5877 return -1;
5878
5879 hist = history[histype];
5880 if (num > 0)
5881 {
5882 while (hist[i].hisnum > num)
5883 if (--i < 0)
5884 {
5885 if (wrapped)
5886 break;
5887 i += hislen;
5888 wrapped = TRUE;
5889 }
5890 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5891 return i;
5892 }
5893 else if (-num <= hislen)
5894 {
5895 i += num + 1;
5896 if (i < 0)
5897 i += hislen;
5898 if (hist[i].hisstr != NULL)
5899 return i;
5900 }
5901 return -1;
5902}
5903
5904/*
5905 * Get a history entry by its index.
5906 * "histype" may be one of the HIST_ values.
5907 */
5908 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005909get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910{
5911 idx = calc_hist_idx(histype, idx);
5912 if (idx >= 0)
5913 return history[histype][idx].hisstr;
5914 else
5915 return (char_u *)"";
5916}
5917
5918/*
5919 * Clear all entries of a history.
5920 * "histype" may be one of the HIST_ values.
5921 */
5922 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005923clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 int i;
5926 histentry_T *hisptr;
5927
5928 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5929 {
5930 hisptr = history[histype];
5931 for (i = hislen; i--;)
5932 {
5933 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005934 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005935 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
5937 hisidx[histype] = -1; /* mark history as cleared */
5938 hisnum[histype] = 0; /* reset identifier counter */
5939 return OK;
5940 }
5941 return FAIL;
5942}
5943
5944/*
5945 * Remove all entries matching {str} from a history.
5946 * "histype" may be one of the HIST_ values.
5947 */
5948 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005949del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 regmatch_T regmatch;
5952 histentry_T *hisptr;
5953 int idx;
5954 int i;
5955 int last;
5956 int found = FALSE;
5957
5958 regmatch.regprog = NULL;
5959 regmatch.rm_ic = FALSE; /* always match case */
5960 if (hislen != 0
5961 && histype >= 0
5962 && histype < HIST_COUNT
5963 && *str != NUL
5964 && (idx = hisidx[histype]) >= 0
5965 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5966 != NULL)
5967 {
5968 i = last = idx;
5969 do
5970 {
5971 hisptr = &history[histype][i];
5972 if (hisptr->hisstr == NULL)
5973 break;
5974 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5975 {
5976 found = TRUE;
5977 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005978 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 }
5980 else
5981 {
5982 if (i != last)
5983 {
5984 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005985 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 }
5987 if (--last < 0)
5988 last += hislen;
5989 }
5990 if (--i < 0)
5991 i += hislen;
5992 } while (i != idx);
5993 if (history[histype][idx].hisstr == NULL)
5994 hisidx[histype] = -1;
5995 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005996 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 return found;
5998}
5999
6000/*
6001 * Remove an indexed entry from a history.
6002 * "histype" may be one of the HIST_ values.
6003 */
6004 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006005del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006{
6007 int i, j;
6008
6009 i = calc_hist_idx(histype, idx);
6010 if (i < 0)
6011 return FALSE;
6012 idx = hisidx[histype];
6013 vim_free(history[histype][i].hisstr);
6014
6015 /* When deleting the last added search string in a mapping, reset
6016 * last_maptick, so that the last added search string isn't deleted again.
6017 */
6018 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6019 last_maptick = -1;
6020
6021 while (i != idx)
6022 {
6023 j = (i + 1) % hislen;
6024 history[histype][i] = history[histype][j];
6025 i = j;
6026 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006027 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 if (--i < 0)
6029 i += hislen;
6030 hisidx[histype] = i;
6031 return TRUE;
6032}
6033
6034#endif /* FEAT_EVAL */
6035
6036#if defined(FEAT_CRYPT) || defined(PROTO)
6037/*
6038 * Very specific function to remove the value in ":set key=val" from the
6039 * history.
6040 */
6041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006042remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 char_u *p;
6045 int i;
6046
6047 i = hisidx[HIST_CMD];
6048 if (i < 0)
6049 return;
6050 p = history[HIST_CMD][i].hisstr;
6051 if (p != NULL)
6052 for ( ; *p; ++p)
6053 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6054 {
6055 p = vim_strchr(p + 3, '=');
6056 if (p == NULL)
6057 break;
6058 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006059 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 if (p[i] == '\\' && p[i + 1])
6061 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006062 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 --p;
6064 }
6065}
6066#endif
6067
6068#endif /* FEAT_CMDHIST */
6069
Bram Moolenaar064154c2016-03-19 22:50:43 +01006070#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006071/*
6072 * Get pointer to the command line info to use. cmdline_paste() may clear
6073 * ccline and put the previous value in prev_ccline.
6074 */
6075 static struct cmdline_info *
6076get_ccline_ptr(void)
6077{
6078 if ((State & CMDLINE) == 0)
6079 return NULL;
6080 if (ccline.cmdbuff != NULL)
6081 return &ccline;
6082 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6083 return &prev_ccline;
6084 return NULL;
6085}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006086#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006087
Bram Moolenaar064154c2016-03-19 22:50:43 +01006088#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006089/*
6090 * Get the current command line in allocated memory.
6091 * Only works when the command line is being edited.
6092 * Returns NULL when something is wrong.
6093 */
6094 char_u *
6095get_cmdline_str(void)
6096{
6097 struct cmdline_info *p = get_ccline_ptr();
6098
6099 if (p == NULL)
6100 return NULL;
6101 return vim_strnsave(p->cmdbuff, p->cmdlen);
6102}
6103
6104/*
6105 * Get the current command line position, counted in bytes.
6106 * Zero is the first position.
6107 * Only works when the command line is being edited.
6108 * Returns -1 when something is wrong.
6109 */
6110 int
6111get_cmdline_pos(void)
6112{
6113 struct cmdline_info *p = get_ccline_ptr();
6114
6115 if (p == NULL)
6116 return -1;
6117 return p->cmdpos;
6118}
6119
6120/*
6121 * Set the command line byte position to "pos". Zero is the first position.
6122 * Only works when the command line is being edited.
6123 * Returns 1 when failed, 0 when OK.
6124 */
6125 int
6126set_cmdline_pos(
6127 int pos)
6128{
6129 struct cmdline_info *p = get_ccline_ptr();
6130
6131 if (p == NULL)
6132 return 1;
6133
6134 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6135 * changed the command line. */
6136 if (pos < 0)
6137 new_cmdpos = 0;
6138 else
6139 new_cmdpos = pos;
6140 return 0;
6141}
6142#endif
6143
6144#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6145/*
6146 * Get the current command-line type.
6147 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6148 * Only works when the command line is being edited.
6149 * Returns NUL when something is wrong.
6150 */
6151 int
6152get_cmdline_type(void)
6153{
6154 struct cmdline_info *p = get_ccline_ptr();
6155
6156 if (p == NULL)
6157 return NUL;
6158 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006159 return
6160# ifdef FEAT_EVAL
6161 (p->input_fn) ? '@' :
6162# endif
6163 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006164 return p->cmdfirstc;
6165}
6166#endif
6167
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6169/*
6170 * Get indices "num1,num2" that specify a range within a list (not a range of
6171 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6172 * Returns OK if parsed successfully, otherwise FAIL.
6173 */
6174 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006175get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176{
6177 int len;
6178 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006179 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
6181 *str = skipwhite(*str);
6182 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6183 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006184 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 *str += len;
6186 *num1 = (int)num;
6187 first = TRUE;
6188 }
6189 *str = skipwhite(*str);
6190 if (**str == ',') /* parse "to" part of range */
6191 {
6192 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006193 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 if (len > 0)
6195 {
6196 *num2 = (int)num;
6197 *str = skipwhite(*str + len);
6198 }
6199 else if (!first) /* no number given at all */
6200 return FAIL;
6201 }
6202 else if (first) /* only one number given */
6203 *num2 = *num1;
6204 return OK;
6205}
6206#endif
6207
6208#if defined(FEAT_CMDHIST) || defined(PROTO)
6209/*
6210 * :history command - print a history
6211 */
6212 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006213ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214{
6215 histentry_T *hist;
6216 int histype1 = HIST_CMD;
6217 int histype2 = HIST_CMD;
6218 int hisidx1 = 1;
6219 int hisidx2 = -1;
6220 int idx;
6221 int i, j, k;
6222 char_u *end;
6223 char_u *arg = eap->arg;
6224
6225 if (hislen == 0)
6226 {
6227 MSG(_("'history' option is zero"));
6228 return;
6229 }
6230
6231 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6232 {
6233 end = arg;
6234 while (ASCII_ISALPHA(*end)
6235 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6236 end++;
6237 i = *end;
6238 *end = NUL;
6239 histype1 = get_histtype(arg);
6240 if (histype1 == -1)
6241 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006242 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {
6244 histype1 = 0;
6245 histype2 = HIST_COUNT-1;
6246 }
6247 else
6248 {
6249 *end = i;
6250 EMSG(_(e_trailing));
6251 return;
6252 }
6253 }
6254 else
6255 histype2 = histype1;
6256 *end = i;
6257 }
6258 else
6259 end = arg;
6260 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6261 {
6262 EMSG(_(e_trailing));
6263 return;
6264 }
6265
6266 for (; !got_int && histype1 <= histype2; ++histype1)
6267 {
6268 STRCPY(IObuff, "\n # ");
6269 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6270 MSG_PUTS_TITLE(IObuff);
6271 idx = hisidx[histype1];
6272 hist = history[histype1];
6273 j = hisidx1;
6274 k = hisidx2;
6275 if (j < 0)
6276 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6277 if (k < 0)
6278 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6279 if (idx >= 0 && j <= k)
6280 for (i = idx + 1; !got_int; ++i)
6281 {
6282 if (i == hislen)
6283 i = 0;
6284 if (hist[i].hisstr != NULL
6285 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6286 {
6287 msg_putchar('\n');
6288 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6289 hist[i].hisnum);
6290 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6291 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006292 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 else
6294 STRCAT(IObuff, hist[i].hisstr);
6295 msg_outtrans(IObuff);
6296 out_flush();
6297 }
6298 if (i == idx)
6299 break;
6300 }
6301 }
6302}
6303#endif
6304
6305#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006306/*
6307 * Buffers for history read from a viminfo file. Only valid while reading.
6308 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006309static histentry_T *viminfo_history[HIST_COUNT] =
6310 {NULL, NULL, NULL, NULL, NULL};
6311static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6312static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313static int viminfo_add_at_front = FALSE;
6314
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006315static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
6317/*
6318 * Translate a history type number to the associated character.
6319 */
6320 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006321hist_type2char(
6322 int type,
6323 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 if (type == HIST_CMD)
6326 return ':';
6327 if (type == HIST_SEARCH)
6328 {
6329 if (use_question)
6330 return '?';
6331 else
6332 return '/';
6333 }
6334 if (type == HIST_EXPR)
6335 return '=';
6336 return '@';
6337}
6338
6339/*
6340 * Prepare for reading the history from the viminfo file.
6341 * This allocates history arrays to store the read history lines.
6342 */
6343 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006344prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 int i;
6347 int num;
6348 int type;
6349 int len;
6350
6351 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006352 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 if (asklen > hislen)
6354 asklen = hislen;
6355
6356 for (type = 0; type < HIST_COUNT; ++type)
6357 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006358 /* Count the number of empty spaces in the history list. Entries read
6359 * from viminfo previously are also considered empty. If there are
6360 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006362 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 num++;
6364 len = asklen;
6365 if (num > len)
6366 len = num;
6367 if (len <= 0)
6368 viminfo_history[type] = NULL;
6369 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006370 viminfo_history[type] = (histentry_T *)lalloc(
6371 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 if (viminfo_history[type] == NULL)
6373 len = 0;
6374 viminfo_hislen[type] = len;
6375 viminfo_hisidx[type] = 0;
6376 }
6377}
6378
6379/*
6380 * Accept a line from the viminfo, store it in the history array when it's
6381 * new.
6382 */
6383 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006384read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 int type;
6387 long_u len;
6388 char_u *val;
6389 char_u *p;
6390
6391 type = hist_char2type(virp->vir_line[0]);
6392 if (viminfo_hisidx[type] < viminfo_hislen[type])
6393 {
6394 val = viminfo_readstring(virp, 1, TRUE);
6395 if (val != NULL && *val != NUL)
6396 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006397 int sep = (*val == ' ' ? NUL : *val);
6398
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006400 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 {
6402 /* Need to re-allocate to append the separator byte. */
6403 len = STRLEN(val);
6404 p = lalloc(len + 2, TRUE);
6405 if (p != NULL)
6406 {
6407 if (type == HIST_SEARCH)
6408 {
6409 /* Search entry: Move the separator from the first
6410 * column to after the NUL. */
6411 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006412 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 }
6414 else
6415 {
6416 /* Not a search entry: No separator in the viminfo
6417 * file, add a NUL separator. */
6418 mch_memmove(p, val, (size_t)len + 1);
6419 p[len + 1] = NUL;
6420 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006421 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6422 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006423 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6424 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006425 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427 }
6428 }
6429 vim_free(val);
6430 }
6431 return viminfo_readline(virp);
6432}
6433
Bram Moolenaar07219f92013-04-14 23:19:36 +02006434/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006435 * Accept a new style history line from the viminfo, store it in the history
6436 * array when it's new.
6437 */
6438 void
6439handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006440 garray_T *values,
6441 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006442{
6443 int type;
6444 long_u len;
6445 char_u *val;
6446 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006447 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006448
6449 /* Check the format:
6450 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006451 if (values->ga_len < 4
6452 || vp[0].bv_type != BVAL_NR
6453 || vp[1].bv_type != BVAL_NR
6454 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6455 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006456 return;
6457
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006458 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006459 if (type >= HIST_COUNT)
6460 return;
6461 if (viminfo_hisidx[type] < viminfo_hislen[type])
6462 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006463 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006464 if (val != NULL && *val != NUL)
6465 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006466 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6467 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006468 int idx;
6469 int overwrite = FALSE;
6470
6471 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6472 {
6473 /* If lines were written by an older Vim we need to avoid
6474 * getting duplicates. See if the entry already exists. */
6475 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6476 {
6477 p = viminfo_history[type][idx].hisstr;
6478 if (STRCMP(val, p) == 0
6479 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6480 {
6481 overwrite = TRUE;
6482 break;
6483 }
6484 }
6485
6486 if (!overwrite)
6487 {
6488 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006489 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006490 p = lalloc(len + 2, TRUE);
6491 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006492 else
6493 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006494 if (p != NULL)
6495 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006496 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006497 if (!overwrite)
6498 {
6499 mch_memmove(p, val, (size_t)len + 1);
6500 /* Put the separator after the NUL. */
6501 p[len + 1] = sep;
6502 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006503 viminfo_history[type][idx].hisnum = 0;
6504 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006505 viminfo_hisidx[type]++;
6506 }
6507 }
6508 }
6509 }
6510 }
6511}
6512
6513/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006514 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006515 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006516 static void
6517concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
6519 int idx;
6520 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006521
6522 idx = hisidx[type] + viminfo_hisidx[type];
6523 if (idx >= hislen)
6524 idx -= hislen;
6525 else if (idx < 0)
6526 idx = hislen - 1;
6527 if (viminfo_add_at_front)
6528 hisidx[type] = idx;
6529 else
6530 {
6531 if (hisidx[type] == -1)
6532 hisidx[type] = hislen - 1;
6533 do
6534 {
6535 if (history[type][idx].hisstr != NULL
6536 || history[type][idx].viminfo)
6537 break;
6538 if (++idx == hislen)
6539 idx = 0;
6540 } while (idx != hisidx[type]);
6541 if (idx != hisidx[type] && --idx < 0)
6542 idx = hislen - 1;
6543 }
6544 for (i = 0; i < viminfo_hisidx[type]; i++)
6545 {
6546 vim_free(history[type][idx].hisstr);
6547 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6548 history[type][idx].viminfo = TRUE;
6549 history[type][idx].time_set = viminfo_history[type][i].time_set;
6550 if (--idx < 0)
6551 idx = hislen - 1;
6552 }
6553 idx += 1;
6554 idx %= hislen;
6555 for (i = 0; i < viminfo_hisidx[type]; i++)
6556 {
6557 history[type][idx++].hisnum = ++hisnum[type];
6558 idx %= hislen;
6559 }
6560}
6561
6562#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6563 static int
6564#ifdef __BORLANDC__
6565_RTLENTRYF
6566#endif
6567sort_hist(const void *s1, const void *s2)
6568{
6569 histentry_T *p1 = *(histentry_T **)s1;
6570 histentry_T *p2 = *(histentry_T **)s2;
6571
6572 if (p1->time_set < p2->time_set) return -1;
6573 if (p1->time_set > p2->time_set) return 1;
6574 return 0;
6575}
6576#endif
6577
6578/*
6579 * Merge history lines from viminfo and lines typed in this Vim based on the
6580 * timestamp;
6581 */
6582 static void
6583merge_history(int type)
6584{
6585 int max_len;
6586 histentry_T **tot_hist;
6587 histentry_T *new_hist;
6588 int i;
6589 int len;
6590
6591 /* Make one long list with all entries. */
6592 max_len = hislen + viminfo_hisidx[type];
6593 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6594 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6595 if (tot_hist == NULL || new_hist == NULL)
6596 {
6597 vim_free(tot_hist);
6598 vim_free(new_hist);
6599 return;
6600 }
6601 for (i = 0; i < viminfo_hisidx[type]; i++)
6602 tot_hist[i] = &viminfo_history[type][i];
6603 len = i;
6604 for (i = 0; i < hislen; i++)
6605 if (history[type][i].hisstr != NULL)
6606 tot_hist[len++] = &history[type][i];
6607
6608 /* Sort the list on timestamp. */
6609 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6610
6611 /* Keep the newest ones. */
6612 for (i = 0; i < hislen; i++)
6613 {
6614 if (i < len)
6615 {
6616 new_hist[i] = *tot_hist[i];
6617 tot_hist[i]->hisstr = NULL;
6618 if (new_hist[i].hisnum == 0)
6619 new_hist[i].hisnum = ++hisnum[type];
6620 }
6621 else
6622 clear_hist_entry(&new_hist[i]);
6623 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006624 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006625
6626 /* Free what is not kept. */
6627 for (i = 0; i < viminfo_hisidx[type]; i++)
6628 vim_free(viminfo_history[type][i].hisstr);
6629 for (i = 0; i < hislen; i++)
6630 vim_free(history[type][i].hisstr);
6631 vim_free(history[type]);
6632 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006633 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006634}
6635
6636/*
6637 * Finish reading history lines from viminfo. Not used when writing viminfo.
6638 */
6639 void
6640finish_viminfo_history(vir_T *virp)
6641{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006643 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645 for (type = 0; type < HIST_COUNT; ++type)
6646 {
6647 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006648 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006649
6650 if (merge)
6651 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006653 concat_history(type);
6654
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 vim_free(viminfo_history[type]);
6656 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006657 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 }
6659}
6660
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006661/*
6662 * Write history to viminfo file in "fp".
6663 * When "merge" is TRUE merge history lines with a previously read viminfo
6664 * file, data is in viminfo_history[].
6665 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006668write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670 int i;
6671 int type;
6672 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006673 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674
6675 init_history();
6676 if (hislen == 0)
6677 return;
6678 for (type = 0; type < HIST_COUNT; ++type)
6679 {
6680 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6681 if (num_saved == 0)
6682 continue;
6683 if (num_saved < 0) /* Use default */
6684 num_saved = hislen;
6685 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6686 type == HIST_CMD ? _("Command Line") :
6687 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006688 type == HIST_EXPR ? _("Expression") :
6689 type == HIST_INPUT ? _("Input Line") :
6690 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 if (num_saved > hislen)
6692 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006693
6694 /*
6695 * Merge typed and viminfo history:
6696 * round 1: history of typed commands.
6697 * round 2: history from recently read viminfo.
6698 */
6699 for (round = 1; round <= 2; ++round)
6700 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006701 if (round == 1)
6702 /* start at newest entry, somewhere in the list */
6703 i = hisidx[type];
6704 else if (viminfo_hisidx[type] > 0)
6705 /* start at newest entry, first in the list */
6706 i = 0;
6707 else
6708 /* empty list */
6709 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006710 if (i >= 0)
6711 while (num_saved > 0
6712 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006714 char_u *p;
6715 time_t timestamp;
6716 int c = NUL;
6717
6718 if (round == 1)
6719 {
6720 p = history[type][i].hisstr;
6721 timestamp = history[type][i].time_set;
6722 }
6723 else
6724 {
6725 p = viminfo_history[type] == NULL ? NULL
6726 : viminfo_history[type][i].hisstr;
6727 timestamp = viminfo_history[type] == NULL ? 0
6728 : viminfo_history[type][i].time_set;
6729 }
6730
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006731 if (p != NULL && (round == 2
6732 || !merge
6733 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006735 --num_saved;
6736 fputc(hist_type2char(type, TRUE), fp);
6737 /* For the search history: put the separator in the
6738 * second column; use a space if there isn't one. */
6739 if (type == HIST_SEARCH)
6740 {
6741 c = p[STRLEN(p) + 1];
6742 putc(c == NUL ? ' ' : c, fp);
6743 }
6744 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006745
6746 {
6747 char cbuf[NUMBUFLEN];
6748
6749 /* New style history with a bar line. Format:
6750 * |{bartype},{histtype},{timestamp},{separator},"text" */
6751 if (c == NUL)
6752 cbuf[0] = NUL;
6753 else
6754 sprintf(cbuf, "%d", c);
6755 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6756 type, (long)timestamp, cbuf);
6757 barline_writestring(fp, p, LSIZE - 20);
6758 putc('\n', fp);
6759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006761 if (round == 1)
6762 {
6763 /* Decrement index, loop around and stop when back at
6764 * the start. */
6765 if (--i < 0)
6766 i = hislen - 1;
6767 if (i == hisidx[type])
6768 break;
6769 }
6770 else
6771 {
6772 /* Increment index. Stop at the end in the while. */
6773 ++i;
6774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006776 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006777 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006778 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006779 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006780 vim_free(viminfo_history[type]);
6781 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006782 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 }
6784}
6785#endif /* FEAT_VIMINFO */
6786
6787#if defined(FEAT_FKMAP) || defined(PROTO)
6788/*
6789 * Write a character at the current cursor+offset position.
6790 * It is directly written into the command buffer block.
6791 */
6792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006793cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794{
6795 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6796 {
6797 EMSG(_("E198: cmd_pchar beyond the command length"));
6798 return;
6799 }
6800 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6801 ccline.cmdbuff[ccline.cmdlen] = NUL;
6802}
6803
6804 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006805cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806{
6807 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6808 {
6809 /* EMSG(_("cmd_gchar beyond the command length")); */
6810 return NUL;
6811 }
6812 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6813}
6814#endif
6815
6816#if defined(FEAT_CMDWIN) || defined(PROTO)
6817/*
6818 * Open a window on the current command line and history. Allow editing in
6819 * the window. Returns when the window is closed.
6820 * Returns:
6821 * CR if the command is to be executed
6822 * Ctrl_C if it is to be abandoned
6823 * K_IGNORE if editing continues
6824 */
6825 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006826open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827{
6828 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006829 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006831 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 win_T *wp;
6833 int i;
6834 linenr_T lnum;
6835 int histtype;
6836 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006837#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 int save_restart_edit = restart_edit;
6841 int save_State = State;
6842 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006843#ifdef FEAT_RIGHTLEFT
6844 int save_cmdmsg_rl = cmdmsg_rl;
6845#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006846#ifdef FEAT_FOLDING
6847 int save_KeyTyped;
6848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849
6850 /* Can't do this recursively. Can't do it when typing a password. */
6851 if (cmdwin_type != 0
6852# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6853 || cmdline_star > 0
6854# endif
6855 )
6856 {
6857 beep_flush();
6858 return K_IGNORE;
6859 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006860 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861
6862 /* Save current window sizes. */
6863 win_size_save(&winsizes);
6864
6865# ifdef FEAT_AUTOCMD
6866 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006867 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006869 /* don't use a new tab page */
6870 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006871 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006872
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 /* Create a window for the command-line buffer. */
6874 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6875 {
6876 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006877# ifdef FEAT_AUTOCMD
6878 unblock_autocmds();
6879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 return K_IGNORE;
6881 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006882 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883
6884 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006885 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006886 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006889#ifdef FEAT_FOLDING
6890 curwin->w_p_fen = FALSE;
6891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006893 curwin->w_p_rl = cmdmsg_rl;
6894 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006896 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
6898# ifdef FEAT_AUTOCMD
6899 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006900 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006901 /* But don't allow switching to another buffer. */
6902 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903# endif
6904
Bram Moolenaar46152342005-09-07 21:18:43 +00006905 /* Showing the prompt may have set need_wait_return, reset it. */
6906 need_wait_return = FALSE;
6907
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006908 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6910 {
6911 if (p_wc == TAB)
6912 {
6913 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6914 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6915 }
6916 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6917 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006918# ifdef FEAT_AUTOCMD
6919 --curbuf_lock;
6920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006922 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6923 * sets 'textwidth' to 78). */
6924 curbuf->b_p_tw = 0;
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 /* Fill the buffer with the history. */
6927 init_history();
6928 if (hislen > 0)
6929 {
6930 i = hisidx[histtype];
6931 if (i >= 0)
6932 {
6933 lnum = 0;
6934 do
6935 {
6936 if (++i == hislen)
6937 i = 0;
6938 if (history[histtype][i].hisstr != NULL)
6939 ml_append(lnum++, history[histtype][i].hisstr,
6940 (colnr_T)0, FALSE);
6941 }
6942 while (i != hisidx[histtype]);
6943 }
6944 }
6945
6946 /* Replace the empty last line with the current command-line and put the
6947 * cursor there. */
6948 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6949 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6950 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006951 changed_line_abv_curs();
6952 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006953 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01006956 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
6958 /* No Ex mode here! */
6959 exmode_active = 0;
6960
6961 State = NORMAL;
6962# ifdef FEAT_MOUSE
6963 setmouse();
6964# endif
6965
6966# ifdef FEAT_AUTOCMD
6967 /* Trigger CmdwinEnter autocommands. */
6968 typestr[0] = cmdwin_type;
6969 typestr[1] = NUL;
6970 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006971 if (restart_edit != 0) /* autocmd with ":startinsert" */
6972 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973# endif
6974
6975 i = RedrawingDisabled;
6976 RedrawingDisabled = 0;
6977
6978 /*
6979 * Call the main loop until <CR> or CTRL-C is typed.
6980 */
6981 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006982 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
6984 RedrawingDisabled = i;
6985
6986# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006987
6988# ifdef FEAT_FOLDING
6989 save_KeyTyped = KeyTyped;
6990# endif
6991
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 /* Trigger CmdwinLeave autocommands. */
6993 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006994
6995# ifdef FEAT_FOLDING
6996 /* Restore KeyTyped in case it is modified by autocommands */
6997 KeyTyped = save_KeyTyped;
6998# endif
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000# endif
7001
Bram Moolenaarccc18222007-05-10 18:25:20 +00007002 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007003 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 cmdwin_type = 0;
7005
7006 exmode_active = save_exmode;
7007
Bram Moolenaarf9821062008-06-20 16:31:07 +00007008 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007010 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
7012 cmdwin_result = Ctrl_C;
7013 EMSG(_("E199: Active window or buffer deleted"));
7014 }
7015 else
7016 {
7017# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7018 /* autocmds may abort script processing */
7019 if (aborting() && cmdwin_result != K_IGNORE)
7020 cmdwin_result = Ctrl_C;
7021# endif
7022 /* Set the new command line from the cmdline buffer. */
7023 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007024 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007026 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7027
7028 if (histtype == HIST_CMD)
7029 {
7030 /* Execute the command directly. */
7031 ccline.cmdbuff = vim_strsave((char_u *)p);
7032 cmdwin_result = CAR;
7033 }
7034 else
7035 {
7036 /* First need to cancel what we were doing. */
7037 ccline.cmdbuff = NULL;
7038 stuffcharReadbuff(':');
7039 stuffReadbuff((char_u *)p);
7040 stuffcharReadbuff(CAR);
7041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
7043 else if (cmdwin_result == K_XF2) /* :qa typed */
7044 {
7045 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7046 cmdwin_result = CAR;
7047 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007048 else if (cmdwin_result == Ctrl_C)
7049 {
7050 /* :q or :close, don't execute any command
7051 * and don't modify the cmd window. */
7052 ccline.cmdbuff = NULL;
7053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 else
7055 ccline.cmdbuff = vim_strsave(ml_get_curline());
7056 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007057 {
7058 ccline.cmdbuff = vim_strsave((char_u *)"");
7059 ccline.cmdlen = 0;
7060 ccline.cmdbufflen = 1;
7061 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 else
7065 {
7066 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7067 ccline.cmdbufflen = ccline.cmdlen + 1;
7068 ccline.cmdpos = curwin->w_cursor.col;
7069 if (ccline.cmdpos > ccline.cmdlen)
7070 ccline.cmdpos = ccline.cmdlen;
7071 if (cmdwin_result == K_IGNORE)
7072 {
7073 set_cmdspos_cursor();
7074 redrawcmd();
7075 }
7076 }
7077
7078# ifdef FEAT_AUTOCMD
7079 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007080 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007082# ifdef FEAT_CONCEAL
7083 /* Avoid command-line window first character being concealed. */
7084 curwin->w_p_cole = 0;
7085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007087 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 win_goto(old_curwin);
7089 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007090
7091 /* win_close() may have already wiped the buffer when 'bh' is
7092 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007093 if (bufref_valid(&bufref))
7094 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095
7096 /* Restore window sizes. */
7097 win_size_restore(&winsizes);
7098
7099# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007100 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101# endif
7102 }
7103
7104 ga_clear(&winsizes);
7105 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007106# ifdef FEAT_RIGHTLEFT
7107 cmdmsg_rl = save_cmdmsg_rl;
7108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109
7110 State = save_State;
7111# ifdef FEAT_MOUSE
7112 setmouse();
7113# endif
7114
7115 return cmdwin_result;
7116}
7117#endif /* FEAT_CMDWIN */
7118
7119/*
7120 * Used for commands that either take a simple command string argument, or:
7121 * cmd << endmarker
7122 * {script}
7123 * endmarker
7124 * Returns a pointer to allocated memory with {script} or NULL.
7125 */
7126 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007127script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 char_u *theline;
7130 char *end_pattern = NULL;
7131 char dot[] = ".";
7132 garray_T ga;
7133
7134 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7135 return NULL;
7136
7137 ga_init2(&ga, 1, 0x400);
7138
7139 if (cmd[2] != NUL)
7140 end_pattern = (char *)skipwhite(cmd + 2);
7141 else
7142 end_pattern = dot;
7143
7144 for (;;)
7145 {
7146 theline = eap->getline(
7147#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007148 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149#endif
7150 NUL, eap->cookie, 0);
7151
7152 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007153 {
7154 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
7158 ga_concat(&ga, theline);
7159 ga_append(&ga, '\n');
7160 vim_free(theline);
7161 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007162 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
7164 return (char_u *)ga.ga_data;
7165}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007166
7167#ifdef FEAT_SEARCH_EXTRA
7168 static void
7169set_search_match(pos_T *t)
7170{
7171 /*
7172 * First move cursor to end of match, then to the start. This
7173 * moves the whole match onto the screen when 'nowrap' is set.
7174 */
7175 t->lnum += search_match_lines;
7176 t->col = search_match_endcol;
7177 if (t->lnum > curbuf->b_ml.ml_line_count)
7178 {
7179 t->lnum = curbuf->b_ml.ml_line_count;
7180 coladvance((colnr_T)MAXCOL);
7181 }
7182}
7183#endif