blob: 717a0122486b24e765be5b416e02ff807562af08 [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 Moolenaarfafcf0d2017-10-19 18:35:51 +0200148
149#ifdef FEAT_AUTOCMD
150 static void
151trigger_cmd_autocmd(int typechar, int evt)
152{
153 char_u typestr[2];
154
155 typestr[0] = typechar;
156 typestr[1] = NUL;
157 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
158}
159#endif
160
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200162 * Abandon the command line.
163 */
164 static void
165abandon_cmdline(void)
166{
167 vim_free(ccline.cmdbuff);
168 ccline.cmdbuff = NULL;
169 if (msg_scrolled == 0)
170 compute_cmdrow();
171 MSG("");
172 redraw_cmdline = TRUE;
173}
174
175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 * getcmdline() - accept a command line starting with firstc.
177 *
178 * firstc == ':' get ":" command line.
179 * firstc == '/' or '?' get search pattern
180 * firstc == '=' get expression
181 * firstc == '@' get text for input() function
182 * firstc == '>' get text for debug mode
183 * firstc == NUL get text for :insert command
184 * firstc == -1 like NUL, and break on CTRL-C
185 *
186 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
187 * command line.
188 *
189 * Careful: getcmdline() can be called recursively!
190 *
191 * Return pointer to allocated string if there is a commandline, NULL
192 * otherwise.
193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100195getcmdline(
196 int firstc,
197 long count UNUSED, /* only used for incremental search */
198 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199{
200 int c;
201 int i;
202 int j;
203 int gotesc = FALSE; /* TRUE when <ESC> just typed */
204 int do_abbr; /* when TRUE check for abbr. */
205#ifdef FEAT_CMDHIST
206 char_u *lookfor = NULL; /* string to match */
207 int hiscnt; /* current history line in use */
208 int histype; /* history type to be used */
209#endif
210#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200211 pos_T search_start; /* where 'incsearch' starts searching */
212 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 colnr_T old_curswant;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200214 colnr_T init_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 colnr_T old_leftcol;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200216 colnr_T init_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 linenr_T old_topline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200218 linenr_T init_topline = curwin->w_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200219 pos_T match_start = curwin->w_cursor;
220 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221# ifdef FEAT_DIFF
222 int old_topfill;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200223 int init_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224# endif
225 linenr_T old_botline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200226 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 int did_incsearch = FALSE;
228 int incsearch_postponed = FALSE;
229#endif
230 int did_wild_list = FALSE; /* did wild_list() recently */
231 int wim_index = 0; /* index in wim_flags[] */
232 int res;
233 int save_msg_scroll = msg_scroll;
234 int save_State = State; /* remember State when called */
235 int some_key_typed = FALSE; /* one of the keys was typed */
236#ifdef FEAT_MOUSE
237 /* mouse drag and release events are ignored, unless they are
238 * preceded with a mouse down event */
239 int ignore_drag_release = TRUE;
240#endif
241#ifdef FEAT_EVAL
242 int break_ctrl_c = FALSE;
243#endif
244 expand_T xpc;
245 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100246#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000247 /* Everything that may work recursively should save and restore the
248 * current command line in save_ccline. That includes update_screen(), a
249 * custom status line may invoke ":normal". */
250 struct cmdline_info save_ccline;
251#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200252#ifdef FEAT_AUTOCMD
253 int cmdline_type;
254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifdef FEAT_EVAL
257 if (firstc == -1)
258 {
259 firstc = NUL;
260 break_ctrl_c = TRUE;
261 }
262#endif
263#ifdef FEAT_RIGHTLEFT
264 /* start without Hebrew mapping for a command line */
265 if (firstc == ':' || firstc == '=' || firstc == '>')
266 cmd_hkmap = 0;
267#endif
268
269 ccline.overstrike = FALSE; /* always start in insert mode */
270#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100271 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200272 save_cursor = curwin->w_cursor; /* may be restored later */
273 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 old_curswant = curwin->w_curswant;
275 old_leftcol = curwin->w_leftcol;
276 old_topline = curwin->w_topline;
277# ifdef FEAT_DIFF
278 old_topfill = curwin->w_topfill;
279# endif
280 old_botline = curwin->w_botline;
281#endif
282
283 /*
284 * set some variables for redrawcmd()
285 */
286 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000287 ccline.cmdindent = (firstc > 0 ? indent : 0);
288
289 /* alloc initial ccline.cmdbuff */
290 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 if (ccline.cmdbuff == NULL)
292 return NULL; /* out of memory */
293 ccline.cmdlen = ccline.cmdpos = 0;
294 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100295 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000297 /* autoindent for :insert and :append */
298 if (firstc <= 0)
299 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200300 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000301 ccline.cmdbuff[indent] = NUL;
302 ccline.cmdpos = indent;
303 ccline.cmdspos = indent;
304 ccline.cmdlen = indent;
305 }
306
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000308 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
310#ifdef FEAT_RIGHTLEFT
311 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
312 && (firstc == '/' || firstc == '?'))
313 cmdmsg_rl = TRUE;
314 else
315 cmdmsg_rl = FALSE;
316#endif
317
318 redir_off = TRUE; /* don't redirect the typed command */
319 if (!cmd_silent)
320 {
321 i = msg_scrolled;
322 msg_scrolled = 0; /* avoid wait_return message */
323 gotocmdline(TRUE);
324 msg_scrolled += i;
325 redrawcmdprompt(); /* draw prompt or indent */
326 set_cmdspos();
327 }
328 xpc.xp_context = EXPAND_NOTHING;
329 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000330#ifndef BACKSLASH_IN_FILENAME
331 xpc.xp_shell = FALSE;
332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000334#if defined(FEAT_EVAL)
335 if (ccline.input_fn)
336 {
337 xpc.xp_context = ccline.xp_context;
338 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000339# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000340 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000341# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000342 }
343#endif
344
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 /*
346 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
347 * doing ":@0" when register 0 doesn't contain a CR.
348 */
349 msg_scroll = FALSE;
350
351 State = CMDLINE;
352
353 if (firstc == '/' || firstc == '?' || firstc == '@')
354 {
355 /* Use ":lmap" mappings for search pattern and input(). */
356 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
357 b_im_ptr = &curbuf->b_p_iminsert;
358 else
359 b_im_ptr = &curbuf->b_p_imsearch;
360 if (*b_im_ptr == B_IMODE_LMAP)
361 State |= LANGMAP;
362#ifdef USE_IM_CONTROL
363 im_set_active(*b_im_ptr == B_IMODE_IM);
364#endif
365 }
366#ifdef USE_IM_CONTROL
367 else if (p_imcmdline)
368 im_set_active(TRUE);
369#endif
370
371#ifdef FEAT_MOUSE
372 setmouse();
373#endif
374#ifdef CURSOR_SHAPE
375 ui_cursor_shape(); /* may show different cursor shape */
376#endif
377
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000378 /* When inside an autocommand for writing "exiting" may be set and
379 * terminal mode set to cooked. Need to set raw mode here then. */
380 settmode(TMODE_RAW);
381
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200382#ifdef FEAT_AUTOCMD
383 /* Trigger CmdlineEnter autocommands. */
384 cmdline_type = firstc == NUL ? '-' : firstc;
385 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
386#endif
387
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388#ifdef FEAT_CMDHIST
389 init_history();
390 hiscnt = hislen; /* set hiscnt to impossible history value */
391 histype = hist_char2type(firstc);
392#endif
393
394#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000395 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396#endif
397
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200398 /* If something above caused an error, reset the flags, we do want to type
399 * and execute commands. Display may be messed up a bit. */
400 if (did_emsg)
401 redrawcmd();
402 did_emsg = FALSE;
403 got_int = FALSE;
404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 /*
406 * Collect the command string, handling editing keys.
407 */
408 for (;;)
409 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000410 redir_off = TRUE; /* Don't redirect the typed command.
411 Repeated, because a ":redir" inside
412 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413#ifdef USE_ON_FLY_SCROLL
414 dont_scroll = FALSE; /* allow scrolling here */
415#endif
416 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
417
418 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000419
420 /* Get a character. Ignore K_IGNORE, it should not do anything, such
421 * as stop completion. */
422 do
423 {
424 c = safe_vgetc();
425 } while (c == K_IGNORE);
426
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 if (KeyTyped)
428 {
429 some_key_typed = TRUE;
430#ifdef FEAT_RIGHTLEFT
431 if (cmd_hkmap)
432 c = hkmap(c);
433# ifdef FEAT_FKMAP
434 if (cmd_fkmap)
435 c = cmdl_fkmap(c);
436# endif
437 if (cmdmsg_rl && !KeyStuffed)
438 {
439 /* Invert horizontal movements and operations. Only when
440 * typed by the user directly, not when the result of a
441 * mapping. */
442 switch (c)
443 {
444 case K_RIGHT: c = K_LEFT; break;
445 case K_S_RIGHT: c = K_S_LEFT; break;
446 case K_C_RIGHT: c = K_C_LEFT; break;
447 case K_LEFT: c = K_RIGHT; break;
448 case K_S_LEFT: c = K_S_RIGHT; break;
449 case K_C_LEFT: c = K_C_RIGHT; break;
450 }
451 }
452#endif
453 }
454
455 /*
456 * Ignore got_int when CTRL-C was typed here.
457 * Don't ignore it in :global, we really need to break then, e.g., for
458 * ":g/pat/normal /pat" (without the <CR>).
459 * Don't ignore it for the input() function.
460 */
461 if ((c == Ctrl_C
462#ifdef UNIX
463 || c == intr_char
464#endif
465 )
466#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
467 && firstc != '@'
468#endif
469#ifdef FEAT_EVAL
470 && !break_ctrl_c
471#endif
472 && !global_busy)
473 got_int = FALSE;
474
475#ifdef FEAT_CMDHIST
476 /* free old command line when finished moving around in the history
477 * list */
478 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000479 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000480 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 && c != K_PAGEDOWN && c != K_PAGEUP
482 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000483 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
485 {
486 vim_free(lookfor);
487 lookfor = NULL;
488 }
489#endif
490
491 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000492 * When there are matching completions to select <S-Tab> works like
493 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000495 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 c = Ctrl_P;
497
498#ifdef FEAT_WILDMENU
499 /* Special translations for 'wildmenu' */
500 if (did_wild_list && p_wmnu)
501 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000502 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000504 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 c = Ctrl_N;
506 }
507 /* Hitting CR after "emenu Name.": complete submenu */
508 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
509 && ccline.cmdpos > 1
510 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
511 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
512 && (c == '\n' || c == '\r' || c == K_KENTER))
513 c = K_DOWN;
514#endif
515
516 /* free expanded names when finished walking through matches */
517 if (xpc.xp_numfiles != -1
518 && !(c == p_wc && KeyTyped) && c != p_wcm
519 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
520 && c != Ctrl_L)
521 {
522 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
523 did_wild_list = FALSE;
524#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000525 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526#endif
527 xpc.xp_context = EXPAND_NOTHING;
528 wim_index = 0;
529#ifdef FEAT_WILDMENU
530 if (p_wmnu && wild_menu_showing != 0)
531 {
532 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000533 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000534
535 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000536 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
538 if (wild_menu_showing == WM_SCROLLED)
539 {
540 /* Entered command line, move it up */
541 cmdline_row--;
542 redrawcmd();
543 }
544 else if (save_p_ls != -1)
545 {
546 /* restore 'laststatus' and 'winminheight' */
547 p_ls = save_p_ls;
548 p_wmh = save_p_wmh;
549 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000550 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000552 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 redrawcmd();
554 save_p_ls = -1;
555 }
556 else
557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 redraw_statuslines();
560 }
561 KeyTyped = skt;
562 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000563 if (ccline.input_fn)
564 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
566#endif
567 }
568
569#ifdef FEAT_WILDMENU
570 /* Special translations for 'wildmenu' */
571 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
572 {
573 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000574 if (c == K_DOWN && ccline.cmdpos > 0
575 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000577 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
579 /* Hitting <Up>: Remove one submenu name in front of the
580 * cursor */
581 int found = FALSE;
582
583 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
584 i = 0;
585 while (--j > 0)
586 {
587 /* check for start of menu name */
588 if (ccline.cmdbuff[j] == ' '
589 && ccline.cmdbuff[j - 1] != '\\')
590 {
591 i = j + 1;
592 break;
593 }
594 /* check for start of submenu name */
595 if (ccline.cmdbuff[j] == '.'
596 && ccline.cmdbuff[j - 1] != '\\')
597 {
598 if (found)
599 {
600 i = j + 1;
601 break;
602 }
603 else
604 found = TRUE;
605 }
606 }
607 if (i > 0)
608 cmdline_del(i);
609 c = p_wc;
610 xpc.xp_context = EXPAND_NOTHING;
611 }
612 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000613 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000614 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000615 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {
617 char_u upseg[5];
618
619 upseg[0] = PATHSEP;
620 upseg[1] = '.';
621 upseg[2] = '.';
622 upseg[3] = PATHSEP;
623 upseg[4] = NUL;
624
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000625 if (c == K_DOWN
626 && ccline.cmdpos > 0
627 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
628 && (ccline.cmdpos < 3
629 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
631 {
632 /* go down a directory */
633 c = p_wc;
634 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000635 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 {
637 /* If in a direct ancestor, strip off one ../ to go down */
638 int found = FALSE;
639
640 j = ccline.cmdpos;
641 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
642 while (--j > i)
643 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000644#ifdef FEAT_MBYTE
645 if (has_mbyte)
646 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 if (vim_ispathsep(ccline.cmdbuff[j]))
649 {
650 found = TRUE;
651 break;
652 }
653 }
654 if (found
655 && ccline.cmdbuff[j - 1] == '.'
656 && ccline.cmdbuff[j - 2] == '.'
657 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
658 {
659 cmdline_del(j - 2);
660 c = p_wc;
661 }
662 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000663 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
665 /* go up a directory */
666 int found = FALSE;
667
668 j = ccline.cmdpos - 1;
669 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
670 while (--j > i)
671 {
672#ifdef FEAT_MBYTE
673 if (has_mbyte)
674 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
675#endif
676 if (vim_ispathsep(ccline.cmdbuff[j])
677#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100678 && vim_strchr((char_u *)" *?[{`$%#",
679 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680#endif
681 )
682 {
683 if (found)
684 {
685 i = j + 1;
686 break;
687 }
688 else
689 found = TRUE;
690 }
691 }
692
693 if (!found)
694 j = i;
695 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
696 j += 4;
697 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
698 && j == i)
699 j += 3;
700 else
701 j = 0;
702 if (j > 0)
703 {
704 /* TODO this is only for DOS/UNIX systems - need to put in
705 * machine-specific stuff here and in upseg init */
706 cmdline_del(j);
707 put_on_cmdline(upseg + 1, 3, FALSE);
708 }
709 else if (ccline.cmdpos > i)
710 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100711
712 /* Now complete in the new directory. Set KeyTyped in case the
713 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100715 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718
719#endif /* FEAT_WILDMENU */
720
721 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
722 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
723 if (c == Ctrl_BSL)
724 {
725 ++no_mapping;
726 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000727 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 --no_mapping;
729 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200730 /* CTRL-\ e doesn't work when obtaining an expression, unless it
731 * is in a mapping. */
732 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
733 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 {
735 vungetc(c);
736 c = Ctrl_BSL;
737 }
738#ifdef FEAT_EVAL
739 else if (c == 'e')
740 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200741 char_u *p = NULL;
742 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 /*
745 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000746 * Need to save and restore the current command line, to be
747 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 */
749 if (ccline.cmdpos == ccline.cmdlen)
750 new_cmdpos = 99999; /* keep it at the end */
751 else
752 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000753
754 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000756 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 if (c == '=')
758 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000759 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000760 * to avoid nasty things like going to another buffer when
761 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000762 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000763 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000765 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000766 restore_cmdline(&save_ccline);
767
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200768 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200770 len = (int)STRLEN(p);
771 if (realloc_cmdbuff(len + 1) == OK)
772 {
773 ccline.cmdlen = len;
774 STRCPY(ccline.cmdbuff, p);
775 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200777 /* Restore the cursor or use the position set with
778 * set_cmdline_pos(). */
779 if (new_cmdpos > ccline.cmdlen)
780 ccline.cmdpos = ccline.cmdlen;
781 else
782 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200784 KeyTyped = FALSE; /* Don't do p_wc completion. */
785 redrawcmd();
786 goto cmdline_changed;
787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 }
789 }
790 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100791 got_int = FALSE; /* don't abandon the command line */
792 did_emsg = FALSE;
793 emsg_on_display = FALSE;
794 redrawcmd();
795 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 }
797#endif
798 else
799 {
800 if (c == Ctrl_G && p_im && restart_edit == 0)
801 restart_edit = 'a';
802 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
803 in history */
804 goto returncmd; /* back to Normal mode */
805 }
806 }
807
808#ifdef FEAT_CMDWIN
809 if (c == cedit_key || c == K_CMDWIN)
810 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200811 if (ex_normal_busy == 0 && got_int == FALSE)
812 {
813 /*
814 * Open a window to edit the command line (and history).
815 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200816 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200817 some_key_typed = TRUE;
818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 }
820# ifdef FEAT_DIGRAPHS
821 else
822# endif
823#endif
824#ifdef FEAT_DIGRAPHS
825 c = do_digraph(c);
826#endif
827
828 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
829 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
830 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000831 /* In Ex mode a backslash escapes a newline. */
832 if (exmode_active
833 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000834 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000835 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000836 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000838 if (c == K_KENTER)
839 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000841 else
842 {
843 gotesc = FALSE; /* Might have typed ESC previously, don't
844 truncate the cmdline now. */
845 if (ccheck_abbr(c + ABBR_OFF))
846 goto cmdline_changed;
847 if (!cmd_silent)
848 {
849 windgoto(msg_row, 0);
850 out_flush();
851 }
852 break;
853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855
856 /*
857 * Completion for 'wildchar' or 'wildcharm' key.
858 * - hitting <ESC> twice means: abandon command line.
859 * - wildcard expansion is only done when the 'wildchar' key is really
860 * typed, not when it comes from a macro
861 */
862 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
863 {
864 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
865 {
866 /* if 'wildmode' contains "list" may still need to list */
867 if (xpc.xp_numfiles > 1
868 && !did_wild_list
869 && (wim_flags[wim_index] & WIM_LIST))
870 {
871 (void)showmatches(&xpc, FALSE);
872 redrawcmd();
873 did_wild_list = TRUE;
874 }
875 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100876 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
877 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100879 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
880 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 else
882 res = OK; /* don't insert 'wildchar' now */
883 }
884 else /* typed p_wc first time */
885 {
886 wim_index = 0;
887 j = ccline.cmdpos;
888 /* if 'wildmode' first contains "longest", get longest
889 * common part */
890 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100891 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
892 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100894 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
895 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897 /* if interrupted while completing, behave like it failed */
898 if (got_int)
899 {
900 (void)vpeekc(); /* remove <C-C> from input stream */
901 got_int = FALSE; /* don't abandon the command line */
902 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
903#ifdef FEAT_WILDMENU
904 xpc.xp_context = EXPAND_NOTHING;
905#endif
906 goto cmdline_changed;
907 }
908
909 /* when more than one match, and 'wildmode' first contains
910 * "list", or no change and 'wildmode' contains "longest,list",
911 * list all matches */
912 if (res == OK && xpc.xp_numfiles > 1)
913 {
914 /* a "longest" that didn't do anything is skipped (but not
915 * "list:longest") */
916 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
917 wim_index = 1;
918 if ((wim_flags[wim_index] & WIM_LIST)
919#ifdef FEAT_WILDMENU
920 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
921#endif
922 )
923 {
924 if (!(wim_flags[0] & WIM_LONGEST))
925 {
926#ifdef FEAT_WILDMENU
927 int p_wmnu_save = p_wmnu;
928 p_wmnu = 0;
929#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100930 /* remove match */
931 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932#ifdef FEAT_WILDMENU
933 p_wmnu = p_wmnu_save;
934#endif
935 }
936#ifdef FEAT_WILDMENU
937 (void)showmatches(&xpc, p_wmnu
938 && ((wim_flags[wim_index] & WIM_LIST) == 0));
939#else
940 (void)showmatches(&xpc, FALSE);
941#endif
942 redrawcmd();
943 did_wild_list = TRUE;
944 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100945 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
946 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100948 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
949 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200952 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954#ifdef FEAT_WILDMENU
955 else if (xpc.xp_numfiles == -1)
956 xpc.xp_context = EXPAND_NOTHING;
957#endif
958 }
959 if (wim_index < 3)
960 ++wim_index;
961 if (c == ESC)
962 gotesc = TRUE;
963 if (res == OK)
964 goto cmdline_changed;
965 }
966
967 gotesc = FALSE;
968
969 /* <S-Tab> goes to last match, in a clumsy way */
970 if (c == K_S_TAB && KeyTyped)
971 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100972 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
973 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
974 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 goto cmdline_changed;
976 }
977
978 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
979 c = NL;
980
981 do_abbr = TRUE; /* default: check for abbreviation */
982
983 /*
984 * Big switch for a typed command line character.
985 */
986 switch (c)
987 {
988 case K_BS:
989 case Ctrl_H:
990 case K_DEL:
991 case K_KDEL:
992 case Ctrl_W:
993#ifdef FEAT_FKMAP
994 if (cmd_fkmap && c == K_BS)
995 c = K_DEL;
996#endif
997 if (c == K_KDEL)
998 c = K_DEL;
999
1000 /*
1001 * delete current character is the same as backspace on next
1002 * character, except at end of line
1003 */
1004 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1005 ++ccline.cmdpos;
1006#ifdef FEAT_MBYTE
1007 if (has_mbyte && c == K_DEL)
1008 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1009 ccline.cmdbuff + ccline.cmdpos);
1010#endif
1011 if (ccline.cmdpos > 0)
1012 {
1013 char_u *p;
1014
1015 j = ccline.cmdpos;
1016 p = ccline.cmdbuff + j;
1017#ifdef FEAT_MBYTE
1018 if (has_mbyte)
1019 {
1020 p = mb_prevptr(ccline.cmdbuff, p);
1021 if (c == Ctrl_W)
1022 {
1023 while (p > ccline.cmdbuff && vim_isspace(*p))
1024 p = mb_prevptr(ccline.cmdbuff, p);
1025 i = mb_get_class(p);
1026 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1027 p = mb_prevptr(ccline.cmdbuff, p);
1028 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001029 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 }
1031 }
1032 else
1033#endif
1034 if (c == Ctrl_W)
1035 {
1036 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1037 --p;
1038 i = vim_iswordc(p[-1]);
1039 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1040 && vim_iswordc(p[-1]) == i)
1041 --p;
1042 }
1043 else
1044 --p;
1045 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1046 ccline.cmdlen -= j - ccline.cmdpos;
1047 i = ccline.cmdpos;
1048 while (i < ccline.cmdlen)
1049 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1050
1051 /* Truncate at the end, required for multi-byte chars. */
1052 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001053#ifdef FEAT_SEARCH_EXTRA
1054 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001055 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001056 search_start = save_cursor;
1057 /* save view settings, so that the screen
1058 * won't be restored at the wrong position */
1059 old_curswant = init_curswant;
1060 old_leftcol = init_leftcol;
1061 old_topline = init_topline;
1062# ifdef FEAT_DIFF
1063 old_topfill = init_topfill;
1064# endif
1065 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001066 }
1067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 redrawcmd();
1069 }
1070 else if (ccline.cmdlen == 0 && c != Ctrl_W
1071 && ccline.cmdprompt == NULL && indent == 0)
1072 {
1073 /* In ex and debug mode it doesn't make sense to return. */
1074 if (exmode_active
1075#ifdef FEAT_EVAL
1076 || ccline.cmdfirstc == '>'
1077#endif
1078 )
1079 goto cmdline_not_changed;
1080
1081 vim_free(ccline.cmdbuff); /* no commandline to return */
1082 ccline.cmdbuff = NULL;
1083 if (!cmd_silent)
1084 {
1085#ifdef FEAT_RIGHTLEFT
1086 if (cmdmsg_rl)
1087 msg_col = Columns;
1088 else
1089#endif
1090 msg_col = 0;
1091 msg_putchar(' '); /* delete ':' */
1092 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001093#ifdef FEAT_SEARCH_EXTRA
1094 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001095 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 redraw_cmdline = TRUE;
1098 goto returncmd; /* back to cmd mode */
1099 }
1100 goto cmdline_changed;
1101
1102 case K_INS:
1103 case K_KINS:
1104#ifdef FEAT_FKMAP
1105 /* if Farsi mode set, we are in reverse insert mode -
1106 Do not change the mode */
1107 if (cmd_fkmap)
1108 beep_flush();
1109 else
1110#endif
1111 ccline.overstrike = !ccline.overstrike;
1112#ifdef CURSOR_SHAPE
1113 ui_cursor_shape(); /* may show different cursor shape */
1114#endif
1115 goto cmdline_not_changed;
1116
1117 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001118 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
1120 /* ":lmap" mappings exists, toggle use of mappings. */
1121 State ^= LANGMAP;
1122#ifdef USE_IM_CONTROL
1123 im_set_active(FALSE); /* Disable input method */
1124#endif
1125 if (b_im_ptr != NULL)
1126 {
1127 if (State & LANGMAP)
1128 *b_im_ptr = B_IMODE_LMAP;
1129 else
1130 *b_im_ptr = B_IMODE_NONE;
1131 }
1132 }
1133#ifdef USE_IM_CONTROL
1134 else
1135 {
1136 /* There are no ":lmap" mappings, toggle IM. When
1137 * 'imdisable' is set don't try getting the status, it's
1138 * always off. */
1139 if ((p_imdisable && b_im_ptr != NULL)
1140 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1141 {
1142 im_set_active(FALSE); /* Disable input method */
1143 if (b_im_ptr != NULL)
1144 *b_im_ptr = B_IMODE_NONE;
1145 }
1146 else
1147 {
1148 im_set_active(TRUE); /* Enable input method */
1149 if (b_im_ptr != NULL)
1150 *b_im_ptr = B_IMODE_IM;
1151 }
1152 }
1153#endif
1154 if (b_im_ptr != NULL)
1155 {
1156 if (b_im_ptr == &curbuf->b_p_iminsert)
1157 set_iminsert_global();
1158 else
1159 set_imsearch_global();
1160 }
1161#ifdef CURSOR_SHAPE
1162 ui_cursor_shape(); /* may show different cursor shape */
1163#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001164#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 /* Show/unshow value of 'keymap' in status lines later. */
1166 status_redraw_curbuf();
1167#endif
1168 goto cmdline_not_changed;
1169
1170/* case '@': only in very old vi */
1171 case Ctrl_U:
1172 /* delete all characters left of the cursor */
1173 j = ccline.cmdpos;
1174 ccline.cmdlen -= j;
1175 i = ccline.cmdpos = 0;
1176 while (i < ccline.cmdlen)
1177 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1178 /* Truncate at the end, required for multi-byte chars. */
1179 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001180#ifdef FEAT_SEARCH_EXTRA
1181 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001182 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 redrawcmd();
1185 goto cmdline_changed;
1186
1187#ifdef FEAT_CLIPBOARD
1188 case Ctrl_Y:
1189 /* Copy the modeless selection, if there is one. */
1190 if (clip_star.state != SELECT_CLEARED)
1191 {
1192 if (clip_star.state == SELECT_DONE)
1193 clip_copy_modeless_selection(TRUE);
1194 goto cmdline_not_changed;
1195 }
1196 break;
1197#endif
1198
1199 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1200 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001201 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001202 * ":normal" runs out of characters. */
1203 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001204 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 goto cmdline_not_changed;
1206
1207 gotesc = TRUE; /* will free ccline.cmdbuff after
1208 putting it in history */
1209 goto returncmd; /* back to cmd mode */
1210
1211 case Ctrl_R: /* insert register */
1212#ifdef USE_ON_FLY_SCROLL
1213 dont_scroll = TRUE; /* disallow scrolling here */
1214#endif
1215 putcmdline('"', TRUE);
1216 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001217 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 if (i == Ctrl_O)
1219 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1220 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001221 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001222 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 --no_mapping;
1224#ifdef FEAT_EVAL
1225 /*
1226 * Insert the result of an expression.
1227 * Need to save the current command line, to be able to enter
1228 * a new one...
1229 */
1230 new_cmdpos = -1;
1231 if (c == '=')
1232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1234 {
1235 beep_flush();
1236 c = ESC;
1237 }
1238 else
1239 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001240 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001242 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 }
1244 }
1245#endif
1246 if (c != ESC) /* use ESC to cancel inserting register */
1247 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001248 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001249
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001250#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001251 /* When there was a serious error abort getting the
1252 * command line. */
1253 if (aborting())
1254 {
1255 gotesc = TRUE; /* will free ccline.cmdbuff after
1256 putting it in history */
1257 goto returncmd; /* back to cmd mode */
1258 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 KeyTyped = FALSE; /* Don't do p_wc completion. */
1261#ifdef FEAT_EVAL
1262 if (new_cmdpos >= 0)
1263 {
1264 /* set_cmdline_pos() was used */
1265 if (new_cmdpos > ccline.cmdlen)
1266 ccline.cmdpos = ccline.cmdlen;
1267 else
1268 ccline.cmdpos = new_cmdpos;
1269 }
1270#endif
1271 }
1272 redrawcmd();
1273 goto cmdline_changed;
1274
1275 case Ctrl_D:
1276 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1277 break; /* Use ^D as normal char instead */
1278
1279 redrawcmd();
1280 continue; /* don't do incremental search now */
1281
1282 case K_RIGHT:
1283 case K_S_RIGHT:
1284 case K_C_RIGHT:
1285 do
1286 {
1287 if (ccline.cmdpos >= ccline.cmdlen)
1288 break;
1289 i = cmdline_charsize(ccline.cmdpos);
1290 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1291 break;
1292 ccline.cmdspos += i;
1293#ifdef FEAT_MBYTE
1294 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001295 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 + ccline.cmdpos);
1297 else
1298#endif
1299 ++ccline.cmdpos;
1300 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001301 while ((c == K_S_RIGHT || c == K_C_RIGHT
1302 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1304#ifdef FEAT_MBYTE
1305 if (has_mbyte)
1306 set_cmdspos_cursor();
1307#endif
1308 goto cmdline_not_changed;
1309
1310 case K_LEFT:
1311 case K_S_LEFT:
1312 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001313 if (ccline.cmdpos == 0)
1314 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 do
1316 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 --ccline.cmdpos;
1318#ifdef FEAT_MBYTE
1319 if (has_mbyte) /* move to first byte of char */
1320 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1321 ccline.cmdbuff + ccline.cmdpos);
1322#endif
1323 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1324 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001325 while (ccline.cmdpos > 0
1326 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001327 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1329#ifdef FEAT_MBYTE
1330 if (has_mbyte)
1331 set_cmdspos_cursor();
1332#endif
1333 goto cmdline_not_changed;
1334
1335 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001336 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001337 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
Bram Moolenaar4770d092006-01-12 23:22:24 +00001339#ifdef FEAT_GUI_W32
1340 /* On Win32 ignore <M-F4>, we get it when closing the window was
1341 * cancelled. */
1342 case K_F4:
1343 if (mod_mask == MOD_MASK_ALT)
1344 {
1345 redrawcmd(); /* somehow the cmdline is cleared */
1346 goto cmdline_not_changed;
1347 }
1348 break;
1349#endif
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#ifdef FEAT_MOUSE
1352 case K_MIDDLEDRAG:
1353 case K_MIDDLERELEASE:
1354 goto cmdline_not_changed; /* Ignore mouse */
1355
1356 case K_MIDDLEMOUSE:
1357# ifdef FEAT_GUI
1358 /* When GUI is active, also paste when 'mouse' is empty */
1359 if (!gui.in_use)
1360# endif
1361 if (!mouse_has(MOUSE_COMMAND))
1362 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001363# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001365 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001367# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001368 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 redrawcmd();
1370 goto cmdline_changed;
1371
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001372# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001374 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 redrawcmd();
1376 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001377# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
1379 case K_LEFTDRAG:
1380 case K_LEFTRELEASE:
1381 case K_RIGHTDRAG:
1382 case K_RIGHTRELEASE:
1383 /* Ignore drag and release events when the button-down wasn't
1384 * seen before. */
1385 if (ignore_drag_release)
1386 goto cmdline_not_changed;
1387 /* FALLTHROUGH */
1388 case K_LEFTMOUSE:
1389 case K_RIGHTMOUSE:
1390 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1391 ignore_drag_release = TRUE;
1392 else
1393 ignore_drag_release = FALSE;
1394# ifdef FEAT_GUI
1395 /* When GUI is active, also move when 'mouse' is empty */
1396 if (!gui.in_use)
1397# endif
1398 if (!mouse_has(MOUSE_COMMAND))
1399 goto cmdline_not_changed; /* Ignore mouse */
1400# ifdef FEAT_CLIPBOARD
1401 if (mouse_row < cmdline_row && clip_star.available)
1402 {
1403 int button, is_click, is_drag;
1404
1405 /*
1406 * Handle modeless selection.
1407 */
1408 button = get_mouse_button(KEY2TERMCAP1(c),
1409 &is_click, &is_drag);
1410 if (mouse_model_popup() && button == MOUSE_LEFT
1411 && (mod_mask & MOD_MASK_SHIFT))
1412 {
1413 /* Translate shift-left to right button. */
1414 button = MOUSE_RIGHT;
1415 mod_mask &= ~MOD_MASK_SHIFT;
1416 }
1417 clip_modeless(button, is_click, is_drag);
1418 goto cmdline_not_changed;
1419 }
1420# endif
1421
1422 set_cmdspos();
1423 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1424 ++ccline.cmdpos)
1425 {
1426 i = cmdline_charsize(ccline.cmdpos);
1427 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1428 && mouse_col < ccline.cmdspos % Columns + i)
1429 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001430# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (has_mbyte)
1432 {
1433 /* Count ">" for double-wide char that doesn't fit. */
1434 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001435 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 + ccline.cmdpos) - 1;
1437 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001438# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 ccline.cmdspos += i;
1440 }
1441 goto cmdline_not_changed;
1442
1443 /* Mouse scroll wheel: ignored here */
1444 case K_MOUSEDOWN:
1445 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001446 case K_MOUSELEFT:
1447 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 /* Alternate buttons ignored here */
1449 case K_X1MOUSE:
1450 case K_X1DRAG:
1451 case K_X1RELEASE:
1452 case K_X2MOUSE:
1453 case K_X2DRAG:
1454 case K_X2RELEASE:
1455 goto cmdline_not_changed;
1456
1457#endif /* FEAT_MOUSE */
1458
1459#ifdef FEAT_GUI
1460 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1461 case K_LEFTRELEASE_NM:
1462 goto cmdline_not_changed;
1463
1464 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001465 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 {
1467 gui_do_scroll();
1468 redrawcmd();
1469 }
1470 goto cmdline_not_changed;
1471
1472 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001473 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001475 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 redrawcmd();
1477 }
1478 goto cmdline_not_changed;
1479#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001480#ifdef FEAT_GUI_TABLINE
1481 case K_TABLINE:
1482 case K_TABMENU:
1483 /* Don't want to change any tabs here. Make sure the same tab
1484 * is still selected. */
1485 if (gui_use_tabline())
1486 gui_mch_set_curtab(tabpage_index(curtab));
1487 goto cmdline_not_changed;
1488#endif
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 case K_SELECT: /* end of Select mode mapping - ignore */
1491 goto cmdline_not_changed;
1492
1493 case Ctrl_B: /* begin of command line */
1494 case K_HOME:
1495 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 case K_S_HOME:
1497 case K_C_HOME:
1498 ccline.cmdpos = 0;
1499 set_cmdspos();
1500 goto cmdline_not_changed;
1501
1502 case Ctrl_E: /* end of command line */
1503 case K_END:
1504 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 case K_S_END:
1506 case K_C_END:
1507 ccline.cmdpos = ccline.cmdlen;
1508 set_cmdspos_cursor();
1509 goto cmdline_not_changed;
1510
1511 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001512 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 break;
1514 goto cmdline_changed;
1515
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001516 case Ctrl_L:
1517#ifdef FEAT_SEARCH_EXTRA
1518 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1519 {
1520 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001521 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001522 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001523 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001524 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001525 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001526 c = gchar_cursor();
1527 /* If 'ignorecase' and 'smartcase' are set and the
1528 * command line has no uppercase characters, convert
1529 * the character to lowercase */
1530 if (p_ic && p_scs
1531 && !pat_has_uppercase(ccline.cmdbuff))
1532 c = MB_TOLOWER(c);
1533 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001534 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001535 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001536 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001537 != NULL)
1538 {
1539 /* put a backslash before special
1540 * characters */
1541 stuffcharReadbuff(c);
1542 c = '\\';
1543 }
1544 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001545 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001546 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001547 }
1548 goto cmdline_not_changed;
1549 }
1550#endif
1551
1552 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001553 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 break;
1555 goto cmdline_changed;
1556
1557 case Ctrl_N: /* next match */
1558 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001559 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001561 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1562 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001564 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001567 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 case K_UP:
1569 case K_DOWN:
1570 case K_S_UP:
1571 case K_S_DOWN:
1572 case K_PAGEUP:
1573 case K_KPAGEUP:
1574 case K_PAGEDOWN:
1575 case K_KPAGEDOWN:
1576 if (hislen == 0 || firstc == NUL) /* no history */
1577 goto cmdline_not_changed;
1578
1579 i = hiscnt;
1580
1581 /* save current command string so it can be restored later */
1582 if (lookfor == NULL)
1583 {
1584 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1585 goto cmdline_not_changed;
1586 lookfor[ccline.cmdpos] = NUL;
1587 }
1588
1589 j = (int)STRLEN(lookfor);
1590 for (;;)
1591 {
1592 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001593 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001594 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 {
1596 if (hiscnt == hislen) /* first time */
1597 hiscnt = hisidx[histype];
1598 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1599 hiscnt = hislen - 1;
1600 else if (hiscnt != hisidx[histype] + 1)
1601 --hiscnt;
1602 else /* at top of list */
1603 {
1604 hiscnt = i;
1605 break;
1606 }
1607 }
1608 else /* one step forwards */
1609 {
1610 /* on last entry, clear the line */
1611 if (hiscnt == hisidx[histype])
1612 {
1613 hiscnt = hislen;
1614 break;
1615 }
1616
1617 /* not on a history line, nothing to do */
1618 if (hiscnt == hislen)
1619 break;
1620 if (hiscnt == hislen - 1) /* wrap around */
1621 hiscnt = 0;
1622 else
1623 ++hiscnt;
1624 }
1625 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1626 {
1627 hiscnt = i;
1628 break;
1629 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001630 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001631 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 || STRNCMP(history[histype][hiscnt].hisstr,
1633 lookfor, (size_t)j) == 0)
1634 break;
1635 }
1636
1637 if (hiscnt != i) /* jumped to other entry */
1638 {
1639 char_u *p;
1640 int len;
1641 int old_firstc;
1642
1643 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001644 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 if (hiscnt == hislen)
1646 p = lookfor; /* back to the old one */
1647 else
1648 p = history[histype][hiscnt].hisstr;
1649
1650 if (histype == HIST_SEARCH
1651 && p != lookfor
1652 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1653 {
1654 /* Correct for the separator character used when
1655 * adding the history entry vs the one used now.
1656 * First loop: count length.
1657 * Second loop: copy the characters. */
1658 for (i = 0; i <= 1; ++i)
1659 {
1660 len = 0;
1661 for (j = 0; p[j] != NUL; ++j)
1662 {
1663 /* Replace old sep with new sep, unless it is
1664 * escaped. */
1665 if (p[j] == old_firstc
1666 && (j == 0 || p[j - 1] != '\\'))
1667 {
1668 if (i > 0)
1669 ccline.cmdbuff[len] = firstc;
1670 }
1671 else
1672 {
1673 /* Escape new sep, unless it is already
1674 * escaped. */
1675 if (p[j] == firstc
1676 && (j == 0 || p[j - 1] != '\\'))
1677 {
1678 if (i > 0)
1679 ccline.cmdbuff[len] = '\\';
1680 ++len;
1681 }
1682 if (i > 0)
1683 ccline.cmdbuff[len] = p[j];
1684 }
1685 ++len;
1686 }
1687 if (i == 0)
1688 {
1689 alloc_cmdbuff(len);
1690 if (ccline.cmdbuff == NULL)
1691 goto returncmd;
1692 }
1693 }
1694 ccline.cmdbuff[len] = NUL;
1695 }
1696 else
1697 {
1698 alloc_cmdbuff((int)STRLEN(p));
1699 if (ccline.cmdbuff == NULL)
1700 goto returncmd;
1701 STRCPY(ccline.cmdbuff, p);
1702 }
1703
1704 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1705 redrawcmd();
1706 goto cmdline_changed;
1707 }
1708 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001709#endif
1710 goto cmdline_not_changed;
1711
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001712#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001713 case Ctrl_G: /* next match */
1714 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001715 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1716 {
1717 pos_T t;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001718 int search_flags = SEARCH_NOOF;
Bram Moolenaar11956692016-08-27 16:26:56 +02001719
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001720 save_last_search_pattern();
Bram Moolenaar11956692016-08-27 16:26:56 +02001721 cursor_off();
1722 out_flush();
1723 if (c == Ctrl_G)
1724 {
1725 t = match_end;
1726 search_flags += SEARCH_COL;
1727 }
1728 else
1729 t = match_start;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001730 if (!p_hls)
1731 search_flags += SEARCH_KEEP;
Bram Moolenaar11956692016-08-27 16:26:56 +02001732 ++emsg_off;
1733 i = searchit(curwin, curbuf, &t,
1734 c == Ctrl_G ? FORWARD : BACKWARD,
1735 ccline.cmdbuff, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001736 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001737 --emsg_off;
1738 if (i)
1739 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001740 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001741 match_end = t;
1742 match_start = t;
1743 if (c == Ctrl_T && firstc == '/')
1744 {
1745 /* move just before the current match, so that
1746 * when nv_search finishes the cursor will be
1747 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001748 search_start = t;
1749 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001750 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001751 else if (c == Ctrl_G && firstc == '?')
1752 {
1753 /* move just after the current match, so that
1754 * when nv_search finishes the cursor will be
1755 * put back on the match */
1756 search_start = t;
1757 (void)incl(&search_start);
1758 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001759 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001760 {
1761 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001762 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001763 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001764 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001765 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001766 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001767 }
1768
1769 set_search_match(&match_end);
1770 curwin->w_cursor = match_start;
1771 changed_cline_bef_curs();
1772 update_topline();
1773 validate_cursor();
1774 highlight_match = TRUE;
1775 old_curswant = curwin->w_curswant;
1776 old_leftcol = curwin->w_leftcol;
1777 old_topline = curwin->w_topline;
1778# ifdef FEAT_DIFF
1779 old_topfill = curwin->w_topfill;
1780# endif
1781 old_botline = curwin->w_botline;
1782 update_screen(NOT_VALID);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001783 restore_last_search_pattern();
Bram Moolenaar11956692016-08-27 16:26:56 +02001784 redrawcmdline();
1785 }
1786 else
1787 vim_beep(BO_ERROR);
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001788 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001789 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001790 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#endif
1792
1793 case Ctrl_V:
1794 case Ctrl_Q:
1795#ifdef FEAT_MOUSE
1796 ignore_drag_release = TRUE;
1797#endif
1798 putcmdline('^', TRUE);
1799 c = get_literal(); /* get next (two) character(s) */
1800 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001801 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802#ifdef FEAT_MBYTE
1803 /* may need to remove ^ when composing char was typed */
1804 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1805 {
1806 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1807 msg_putchar(' ');
1808 cursorcmd();
1809 }
1810#endif
1811 break;
1812
1813#ifdef FEAT_DIGRAPHS
1814 case Ctrl_K:
1815#ifdef FEAT_MOUSE
1816 ignore_drag_release = TRUE;
1817#endif
1818 putcmdline('?', TRUE);
1819#ifdef USE_ON_FLY_SCROLL
1820 dont_scroll = TRUE; /* disallow scrolling here */
1821#endif
1822 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001823 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if (c != NUL)
1825 break;
1826
1827 redrawcmd();
1828 goto cmdline_not_changed;
1829#endif /* FEAT_DIGRAPHS */
1830
1831#ifdef FEAT_RIGHTLEFT
1832 case Ctrl__: /* CTRL-_: switch language mode */
1833 if (!p_ari)
1834 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001835# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if (p_altkeymap)
1837 {
1838 cmd_fkmap = !cmd_fkmap;
1839 if (cmd_fkmap) /* in Farsi always in Insert mode */
1840 ccline.overstrike = FALSE;
1841 }
1842 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 cmd_hkmap = !cmd_hkmap;
1845 goto cmdline_not_changed;
1846#endif
1847
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001848 case K_PS:
1849 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1850 goto cmdline_changed;
1851
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 default:
1853#ifdef UNIX
1854 if (c == intr_char)
1855 {
1856 gotesc = TRUE; /* will free ccline.cmdbuff after
1857 putting it in history */
1858 goto returncmd; /* back to Normal mode */
1859 }
1860#endif
1861 /*
1862 * Normal character with no special meaning. Just set mod_mask
1863 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1864 * the string <S-Space>. This should only happen after ^V.
1865 */
1866 if (!IS_SPECIAL(c))
1867 mod_mask = 0x0;
1868 break;
1869 }
1870 /*
1871 * End of switch on command line character.
1872 * We come here if we have a normal character.
1873 */
1874
Bram Moolenaarede3e632013-06-23 16:16:19 +02001875 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876#ifdef FEAT_MBYTE
1877 /* Add ABBR_OFF for characters above 0x100, this is
1878 * what check_abbr() expects. */
1879 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1880#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001881 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 goto cmdline_changed;
1883
1884 /*
1885 * put the character in the command line
1886 */
1887 if (IS_SPECIAL(c) || mod_mask != 0)
1888 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1889 else
1890 {
1891#ifdef FEAT_MBYTE
1892 if (has_mbyte)
1893 {
1894 j = (*mb_char2bytes)(c, IObuff);
1895 IObuff[j] = NUL; /* exclude composing chars */
1896 put_on_cmdline(IObuff, j, TRUE);
1897 }
1898 else
1899#endif
1900 {
1901 IObuff[0] = c;
1902 put_on_cmdline(IObuff, 1, TRUE);
1903 }
1904 }
1905 goto cmdline_changed;
1906
1907/*
1908 * This part implements incremental searches for "/" and "?"
1909 * Jump to cmdline_not_changed when a character has been read but the command
1910 * line did not change. Then we only search and redraw if something changed in
1911 * the past.
1912 * Jump to cmdline_changed when the command line did change.
1913 * (Sorry for the goto's, I know it is ugly).
1914 */
1915cmdline_not_changed:
1916#ifdef FEAT_SEARCH_EXTRA
1917 if (!incsearch_postponed)
1918 continue;
1919#endif
1920
1921cmdline_changed:
1922#ifdef FEAT_SEARCH_EXTRA
1923 /*
1924 * 'incsearch' highlighting.
1925 */
1926 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1927 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001928 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001929#ifdef FEAT_RELTIME
1930 proftime_T tm;
1931#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001932
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 /* if there is a character waiting, search and redraw later */
1934 if (char_avail())
1935 {
1936 incsearch_postponed = TRUE;
1937 continue;
1938 }
1939 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001940 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001941 save_last_search_pattern();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
1943 /* If there is no command line, don't do anything */
1944 if (ccline.cmdlen == 0)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 i = 0;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001947 SET_NO_HLSEARCH(TRUE); /* turn off previous highlight */
1948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 else
1950 {
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001951 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 cursor_off(); /* so the user knows we're busy */
1953 out_flush();
1954 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001955#ifdef FEAT_RELTIME
1956 /* Set the time limit to half a second. */
1957 profile_setlimit(500L, &tm);
1958#endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001959 if (!p_hls)
1960 search_flags += SEARCH_KEEP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001962 search_flags,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001963#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001964 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001965#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001966 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001967#endif
1968 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 --emsg_off;
1970 /* if interrupted while searching, behave like it failed */
1971 if (got_int)
1972 {
1973 (void)vpeekc(); /* remove <C-C> from input stream */
1974 got_int = FALSE; /* don't abandon the command line */
1975 i = 0;
1976 }
1977 else if (char_avail())
1978 /* cancelled searching because a char was typed */
1979 incsearch_postponed = TRUE;
1980 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001981 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 highlight_match = TRUE; /* highlight position */
1983 else
1984 highlight_match = FALSE; /* remove highlight */
1985
1986 /* first restore the old curwin values, so the screen is
1987 * positioned in the same way as the actual search command */
1988 curwin->w_leftcol = old_leftcol;
1989 curwin->w_topline = old_topline;
1990# ifdef FEAT_DIFF
1991 curwin->w_topfill = old_topfill;
1992# endif
1993 curwin->w_botline = old_botline;
1994 changed_cline_bef_curs();
1995 update_topline();
1996
1997 if (i != 0)
1998 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001999 pos_T save_pos = curwin->w_cursor;
2000
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002001 match_start = curwin->w_cursor;
2002 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002004 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002005 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002006 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002008 else
2009 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00002012 /* May redraw the status line to show the cursor position. */
2013 if (p_ru && curwin->w_status_height > 0)
2014 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002016 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002017 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002018 restore_cmdline(&save_ccline);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002019 restore_last_search_pattern();
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002020
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002021 /* Leave it at the end to make CTRL-R CTRL-W work. */
2022 if (i != 0)
2023 curwin->w_cursor = end_pos;
2024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 msg_starthere();
2026 redrawcmdline();
2027 did_incsearch = TRUE;
2028 }
2029#else /* FEAT_SEARCH_EXTRA */
2030 ;
2031#endif
2032
2033#ifdef FEAT_RIGHTLEFT
2034 if (cmdmsg_rl
2035# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002036 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037# endif
2038 )
2039 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002040 * right-left typing. Not efficient, but it works.
2041 * Do it only when there are no characters left to read
2042 * to avoid useless intermediate redraws. */
2043 if (vpeekc() == NUL)
2044 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#endif
2046 }
2047
2048returncmd:
2049
2050#ifdef FEAT_RIGHTLEFT
2051 cmdmsg_rl = FALSE;
2052#endif
2053
2054#ifdef FEAT_FKMAP
2055 cmd_fkmap = 0;
2056#endif
2057
2058 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002059 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060
2061#ifdef FEAT_SEARCH_EXTRA
2062 if (did_incsearch)
2063 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002064 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002065 curwin->w_cursor = save_cursor;
2066 else
2067 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002068 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002069 {
2070 /* put the '" mark at the original position */
2071 curwin->w_cursor = save_cursor;
2072 setpcmark();
2073 }
2074 curwin->w_cursor = search_start;
2075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 curwin->w_curswant = old_curswant;
2077 curwin->w_leftcol = old_leftcol;
2078 curwin->w_topline = old_topline;
2079# ifdef FEAT_DIFF
2080 curwin->w_topfill = old_topfill;
2081# endif
2082 curwin->w_botline = old_botline;
2083 highlight_match = FALSE;
2084 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002085 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
2087#endif
2088
2089 if (ccline.cmdbuff != NULL)
2090 {
2091 /*
2092 * Put line in history buffer (":" and "=" only when it was typed).
2093 */
2094#ifdef FEAT_CMDHIST
2095 if (ccline.cmdlen && firstc != NUL
2096 && (some_key_typed || histype == HIST_SEARCH))
2097 {
2098 add_to_history(histype, ccline.cmdbuff, TRUE,
2099 histype == HIST_SEARCH ? firstc : NUL);
2100 if (firstc == ':')
2101 {
2102 vim_free(new_last_cmdline);
2103 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2104 }
2105 }
2106#endif
2107
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002108 if (gotesc)
2109 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 }
2111
2112 /*
2113 * If the screen was shifted up, redraw the whole screen (later).
2114 * If the line is too long, clear it, so ruler and shown command do
2115 * not get printed in the middle of it.
2116 */
2117 msg_check();
2118 msg_scroll = save_msg_scroll;
2119 redir_off = FALSE;
2120
2121 /* When the command line was typed, no need for a wait-return prompt. */
2122 if (some_key_typed)
2123 need_wait_return = FALSE;
2124
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002125#ifdef FEAT_AUTOCMD
2126 /* Trigger CmdlineLeave autocommands. */
2127 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
2128#endif
2129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 State = save_State;
2131#ifdef USE_IM_CONTROL
2132 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2133 im_save_status(b_im_ptr);
2134 im_set_active(FALSE);
2135#endif
2136#ifdef FEAT_MOUSE
2137 setmouse();
2138#endif
2139#ifdef CURSOR_SHAPE
2140 ui_cursor_shape(); /* may show different cursor shape */
2141#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002142 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002144 {
2145 char_u *p = ccline.cmdbuff;
2146
2147 /* Make ccline empty, getcmdline() may try to use it. */
2148 ccline.cmdbuff = NULL;
2149 return p;
2150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151}
2152
2153#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2154/*
2155 * Get a command line with a prompt.
2156 * This is prepared to be called recursively from getcmdline() (e.g. by
2157 * f_input() when evaluating an expression from CTRL-R =).
2158 * Returns the command line in allocated memory, or NULL.
2159 */
2160 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002161getcmdline_prompt(
2162 int firstc,
2163 char_u *prompt, /* command line prompt */
2164 int attr, /* attributes for prompt */
2165 int xp_context, /* type of expansion */
2166 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167{
2168 char_u *s;
2169 struct cmdline_info save_ccline;
2170 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002171 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002173 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 ccline.cmdprompt = prompt;
2175 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002176# ifdef FEAT_EVAL
2177 ccline.xp_context = xp_context;
2178 ccline.xp_arg = xp_arg;
2179 ccline.input_fn = (firstc == '@');
2180# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002181 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002183 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002184 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002185 /* Restore msg_col, the prompt from input() may have changed it.
2186 * But only if called recursively and the commandline is therefore being
2187 * restored to an old one; if not, the input() prompt stays on the screen,
2188 * so we need its modified msg_col left intact. */
2189 if (ccline.cmdbuff != NULL)
2190 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 return s;
2193}
2194#endif
2195
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002196/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002197 * Return TRUE when the text must not be changed and we can't switch to
2198 * another window or buffer. Used when editing the command line, evaluating
2199 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002200 */
2201 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002202text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002203{
2204#ifdef FEAT_CMDWIN
2205 if (cmdwin_type != 0)
2206 return TRUE;
2207#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002208 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002209}
2210
2211/*
2212 * Give an error message for a command that isn't allowed while the cmdline
2213 * window is open or editing the cmdline in another way.
2214 */
2215 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002216text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002217{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002218 EMSG(_(get_text_locked_msg()));
2219}
2220
2221 char_u *
2222get_text_locked_msg(void)
2223{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002224#ifdef FEAT_CMDWIN
2225 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002226 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002227#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002228 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002229}
2230
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002231#if defined(FEAT_AUTOCMD) || defined(PROTO)
2232/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002233 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2234 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002235 */
2236 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002237curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238{
2239 if (curbuf_lock > 0)
2240 {
2241 EMSG(_("E788: Not allowed to edit another buffer now"));
2242 return TRUE;
2243 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002244 return allbuf_locked();
2245}
2246
2247/*
2248 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2249 * message.
2250 */
2251 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002252allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002253{
2254 if (allbuf_lock > 0)
2255 {
2256 EMSG(_("E811: Not allowed to change buffer information now"));
2257 return TRUE;
2258 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002259 return FALSE;
2260}
2261#endif
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002264cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265{
2266#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2267 if (cmdline_star > 0) /* showing '*', always 1 position */
2268 return 1;
2269#endif
2270 return ptr2cells(ccline.cmdbuff + idx);
2271}
2272
2273/*
2274 * Compute the offset of the cursor on the command line for the prompt and
2275 * indent.
2276 */
2277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002278set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002280 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 ccline.cmdspos = 1 + ccline.cmdindent;
2282 else
2283 ccline.cmdspos = 0 + ccline.cmdindent;
2284}
2285
2286/*
2287 * Compute the screen position for the cursor on the command line.
2288 */
2289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002290set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291{
2292 int i, m, c;
2293
2294 set_cmdspos();
2295 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002298 if (m < 0) /* overflow, Columns or Rows at weird value */
2299 m = MAXCOL;
2300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 else
2302 m = MAXCOL;
2303 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2304 {
2305 c = cmdline_charsize(i);
2306#ifdef FEAT_MBYTE
2307 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2308 if (has_mbyte)
2309 correct_cmdspos(i, c);
2310#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002311 /* If the cmdline doesn't fit, show cursor on last visible char.
2312 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if ((ccline.cmdspos += c) >= m)
2314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 ccline.cmdspos -= c;
2316 break;
2317 }
2318#ifdef FEAT_MBYTE
2319 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002320 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321#endif
2322 }
2323}
2324
2325#ifdef FEAT_MBYTE
2326/*
2327 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2328 * character that doesn't fit, so that a ">" must be displayed.
2329 */
2330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002331correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002333 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2335 && ccline.cmdspos % Columns + cells > Columns)
2336 ccline.cmdspos++;
2337}
2338#endif
2339
2340/*
2341 * Get an Ex command line for the ":" command.
2342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002344getexline(
2345 int c, /* normally ':', NUL for ":append" */
2346 void *cookie UNUSED,
2347 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349 /* When executing a register, remove ':' that's in front of each line. */
2350 if (exec_from_reg && vpeekc() == ':')
2351 (void)vgetc();
2352 return getcmdline(c, 1L, indent);
2353}
2354
2355/*
2356 * Get an Ex command line for Ex mode.
2357 * In Ex mode we only use the OS supplied line editing features and no
2358 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002359 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002362getexmodeline(
2363 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002364 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002365 void *cookie UNUSED,
2366 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002368 garray_T line_ga;
2369 char_u *pend;
2370 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002371 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002372 int escaped = FALSE; /* CTRL-V typed */
2373 int vcol = 0;
2374 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002375 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002376 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378 /* Switch cursor on now. This avoids that it happens after the "\n", which
2379 * confuses the system function that computes tabstops. */
2380 cursor_on();
2381
2382 /* always start in column 0; write a newline if necessary */
2383 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002384 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002386 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002388 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002389 if (p_prompt)
2390 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 while (indent-- > 0)
2392 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 }
2395
2396 ga_init2(&line_ga, 1, 30);
2397
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002398 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002399 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002400 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002401 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002402 while (indent >= 8)
2403 {
2404 ga_append(&line_ga, TAB);
2405 msg_puts((char_u *)" ");
2406 indent -= 8;
2407 }
2408 while (indent-- > 0)
2409 {
2410 ga_append(&line_ga, ' ');
2411 msg_putchar(' ');
2412 }
2413 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002414 ++no_mapping;
2415 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 /*
2418 * Get the line, one character at a time.
2419 */
2420 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002421 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002423 long sw;
2424 char_u *s;
2425
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (ga_grow(&line_ga, 40) == FAIL)
2427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaarba748c82017-02-26 14:00:07 +01002429 /*
2430 * Get one character at a time.
2431 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002432 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002433
2434 /* Check for a ":normal" command and no more characters left. */
2435 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2436 c1 = '\n';
2437 else
2438 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002441 * Handle line editing.
2442 * Previously this was left to the system, putting the terminal in
2443 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002445 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002447 msg_putchar('\n');
2448 break;
2449 }
2450
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002451 if (c1 == K_PS)
2452 {
2453 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2454 goto redraw;
2455 }
2456
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002457 if (!escaped)
2458 {
2459 /* CR typed means "enter", which is NL */
2460 if (c1 == '\r')
2461 c1 = '\n';
2462
2463 if (c1 == BS || c1 == K_BS
2464 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002466 if (line_ga.ga_len > 0)
2467 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002468#ifdef FEAT_MBYTE
2469 if (has_mbyte)
2470 {
2471 p = (char_u *)line_ga.ga_data;
2472 p[line_ga.ga_len] = NUL;
2473 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2474 line_ga.ga_len -= len;
2475 }
2476 else
2477#endif
2478 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002479 goto redraw;
2480 }
2481 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 }
2483
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002484 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002486 msg_col = startcol;
2487 msg_clr_eos();
2488 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002489 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002490 }
2491
2492 if (c1 == Ctrl_T)
2493 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002494 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002495 p = (char_u *)line_ga.ga_data;
2496 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002497 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002498 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002499add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002500 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002502 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002503 p = (char_u *)line_ga.ga_data;
2504 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002505 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2506 *s = ' ';
2507 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002509redraw:
2510 /* redraw the line */
2511 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002512 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002513 p = (char_u *)line_ga.ga_data;
2514 p[line_ga.ga_len] = NUL;
2515 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002517 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002519 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002521 msg_putchar(' ');
2522 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002523 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002527 len = MB_PTR2LEN(p);
2528 msg_outtrans_len(p, len);
2529 vcol += ptr2cells(p);
2530 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 }
2532 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002533 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002534 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002535 continue;
2536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002538 if (c1 == Ctrl_D)
2539 {
2540 /* Delete one shiftwidth. */
2541 p = (char_u *)line_ga.ga_data;
2542 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002544 if (prev_char == '^')
2545 ex_keep_indent = TRUE;
2546 indent = 0;
2547 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549 else
2550 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002551 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002552 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002553 if (indent > 0)
2554 {
2555 --indent;
2556 indent -= indent % get_sw_value(curbuf);
2557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002559 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002560 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002561 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002562 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2563 --line_ga.ga_len;
2564 }
2565 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002567
2568 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2569 {
2570 escaped = TRUE;
2571 continue;
2572 }
2573
2574 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2575 if (IS_SPECIAL(c1))
2576 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002578
2579 if (IS_SPECIAL(c1))
2580 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002581#ifdef FEAT_MBYTE
2582 if (has_mbyte)
2583 len = (*mb_char2bytes)(c1,
2584 (char_u *)line_ga.ga_data + line_ga.ga_len);
2585 else
2586#endif
2587 {
2588 len = 1;
2589 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2590 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002591 if (c1 == '\n')
2592 msg_putchar('\n');
2593 else if (c1 == TAB)
2594 {
2595 /* Don't use chartabsize(), 'ts' can be different */
2596 do
2597 {
2598 msg_putchar(' ');
2599 } while (++vcol % 8);
2600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002603 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002604 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002605 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002607 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002608 escaped = FALSE;
2609
2610 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002611 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002612
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002613 /* We are done when a NL is entered, but not when it comes after an
2614 * odd number of backslashes, that results in a NUL. */
2615 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002617 int bcount = 0;
2618
2619 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2620 ++bcount;
2621
2622 if (bcount > 0)
2623 {
2624 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2625 * "\NL", etc. */
2626 line_ga.ga_len -= (bcount + 1) / 2;
2627 pend -= (bcount + 1) / 2;
2628 pend[-1] = '\n';
2629 }
2630
2631 if ((bcount & 1) == 0)
2632 {
2633 --line_ga.ga_len;
2634 --pend;
2635 *pend = NUL;
2636 break;
2637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639 }
2640
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641 --no_mapping;
2642 --allow_keys;
2643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 /* make following messages go to the next line */
2645 msg_didout = FALSE;
2646 msg_col = 0;
2647 if (msg_row < Rows - 1)
2648 ++msg_row;
2649 emsg_on_display = FALSE; /* don't want ui_delay() */
2650
2651 if (got_int)
2652 ga_clear(&line_ga);
2653
2654 return (char_u *)line_ga.ga_data;
2655}
2656
Bram Moolenaare344bea2005-09-01 20:46:49 +00002657# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2658 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659/*
2660 * Return TRUE if ccline.overstrike is on.
2661 */
2662 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002663cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
2665 return ccline.overstrike;
2666}
2667
2668/*
2669 * Return TRUE if the cursor is at the end of the cmdline.
2670 */
2671 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002672cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 return (ccline.cmdpos >= ccline.cmdlen);
2675}
2676#endif
2677
Bram Moolenaar9372a112005-12-06 19:59:18 +00002678#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679/*
2680 * Return the virtual column number at the current cursor position.
2681 * This is used by the IM code to obtain the start of the preedit string.
2682 */
2683 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002684cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
2686 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2687 return MAXCOL;
2688
2689# ifdef FEAT_MBYTE
2690 if (has_mbyte)
2691 {
2692 colnr_T col;
2693 int i = 0;
2694
2695 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002696 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697
2698 return col;
2699 }
2700 else
2701# endif
2702 return ccline.cmdpos;
2703}
2704#endif
2705
2706#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2707/*
2708 * If part of the command line is an IM preedit string, redraw it with
2709 * IM feedback attributes. The cursor position is restored after drawing.
2710 */
2711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002712redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 if ((State & CMDLINE)
2715 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002716 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 && !p_imdisable
2718 && im_is_preediting())
2719 {
2720 int cmdpos = 0;
2721 int cmdspos;
2722 int old_row;
2723 int old_col;
2724 colnr_T col;
2725
2726 old_row = msg_row;
2727 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002728 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730# ifdef FEAT_MBYTE
2731 if (has_mbyte)
2732 {
2733 for (col = 0; col < preedit_start_col
2734 && cmdpos < ccline.cmdlen; ++col)
2735 {
2736 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002737 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 }
2739 }
2740 else
2741# endif
2742 {
2743 cmdspos += preedit_start_col;
2744 cmdpos += preedit_start_col;
2745 }
2746
2747 msg_row = cmdline_row + (cmdspos / (int)Columns);
2748 msg_col = cmdspos % (int)Columns;
2749 if (msg_row >= Rows)
2750 msg_row = Rows - 1;
2751
2752 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2753 {
2754 int char_len;
2755 int char_attr;
2756
2757 char_attr = im_get_feedback_attr(col);
2758 if (char_attr < 0)
2759 break; /* end of preedit string */
2760
2761# ifdef FEAT_MBYTE
2762 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002763 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 else
2765# endif
2766 char_len = 1;
2767
2768 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2769 cmdpos += char_len;
2770 }
2771
2772 msg_row = old_row;
2773 msg_col = old_col;
2774 }
2775}
2776#endif /* FEAT_XIM && FEAT_GUI_GTK */
2777
2778/*
2779 * Allocate a new command line buffer.
2780 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2781 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2782 */
2783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002784alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785{
2786 /*
2787 * give some extra space to avoid having to allocate all the time
2788 */
2789 if (len < 80)
2790 len = 100;
2791 else
2792 len += 20;
2793
2794 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2795 ccline.cmdbufflen = len;
2796}
2797
2798/*
2799 * Re-allocate the command line to length len + something extra.
2800 * return FAIL for failure, OK otherwise
2801 */
2802 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002803realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804{
2805 char_u *p;
2806
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002807 if (len < ccline.cmdbufflen)
2808 return OK; /* no need to resize */
2809
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 p = ccline.cmdbuff;
2811 alloc_cmdbuff(len); /* will get some more */
2812 if (ccline.cmdbuff == NULL) /* out of memory */
2813 {
2814 ccline.cmdbuff = p; /* keep the old one */
2815 return FAIL;
2816 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002817 /* There isn't always a NUL after the command, but it may need to be
2818 * there, thus copy up to the NUL and add a NUL. */
2819 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2820 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002822
2823 if (ccline.xpc != NULL
2824 && ccline.xpc->xp_pattern != NULL
2825 && ccline.xpc->xp_context != EXPAND_NOTHING
2826 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2827 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002828 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002829
2830 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2831 * to point into the newly allocated memory. */
2832 if (i >= 0 && i <= ccline.cmdlen)
2833 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2834 }
2835
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 return OK;
2837}
2838
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002839#if defined(FEAT_ARABIC) || defined(PROTO)
2840static char_u *arshape_buf = NULL;
2841
2842# if defined(EXITFREE) || defined(PROTO)
2843 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002844free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002845{
2846 vim_free(arshape_buf);
2847}
2848# endif
2849#endif
2850
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851/*
2852 * Draw part of the cmdline at the current cursor position. But draw stars
2853 * when cmdline_star is TRUE.
2854 */
2855 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002856draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2859 int i;
2860
2861 if (cmdline_star > 0)
2862 for (i = 0; i < len; ++i)
2863 {
2864 msg_putchar('*');
2865# ifdef FEAT_MBYTE
2866 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002867 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868# endif
2869 }
2870 else
2871#endif
2872#ifdef FEAT_ARABIC
2873 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 static int buflen = 0;
2876 char_u *p;
2877 int j;
2878 int newlen = 0;
2879 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002880 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 int prev_c = 0;
2882 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002883 int u8c;
2884 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886
2887 /*
2888 * Do arabic shaping into a temporary buffer. This is very
2889 * inefficient!
2890 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002891 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 {
2893 /* Re-allocate the buffer. We keep it around to avoid a lot of
2894 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002895 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002896 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002897 arshape_buf = alloc(buflen);
2898 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 return; /* out of memory */
2900 }
2901
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002902 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2903 {
2904 /* Prepend a space to draw the leading composing char on. */
2905 arshape_buf[0] = ' ';
2906 newlen = 1;
2907 }
2908
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 for (j = start; j < start + len; j += mb_l)
2910 {
2911 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002912 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002913 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 if (ARABIC_CHAR(u8c))
2915 {
2916 /* Do Arabic shaping. */
2917 if (cmdmsg_rl)
2918 {
2919 /* displaying from right to left */
2920 pc = prev_c;
2921 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002922 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 if (j + mb_l >= start + len)
2924 nc = NUL;
2925 else
2926 nc = utf_ptr2char(p + mb_l);
2927 }
2928 else
2929 {
2930 /* displaying from left to right */
2931 if (j + mb_l >= start + len)
2932 pc = NUL;
2933 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002934 {
2935 int pcc[MAX_MCO];
2936
2937 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002939 pc1 = pcc[0];
2940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 nc = prev_c;
2942 }
2943 prev_c = u8c;
2944
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002945 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002947 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002948 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002950 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2951 if (u8cc[1] != 0)
2952 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002953 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
2955 }
2956 else
2957 {
2958 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002959 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 newlen += mb_l;
2961 }
2962 }
2963
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002964 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
2966 else
2967#endif
2968 msg_outtrans_len(ccline.cmdbuff + start, len);
2969}
2970
2971/*
2972 * Put a character on the command line. Shifts the following text to the
2973 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2974 * "c" must be printable (fit in one display cell)!
2975 */
2976 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002977putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978{
2979 if (cmd_silent)
2980 return;
2981 msg_no_more = TRUE;
2982 msg_putchar(c);
2983 if (shift)
2984 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2985 msg_no_more = FALSE;
2986 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02002987 extra_char = c;
2988 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989}
2990
2991/*
2992 * Undo a putcmdline(c, FALSE).
2993 */
2994 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002995unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 if (cmd_silent)
2998 return;
2999 msg_no_more = TRUE;
3000 if (ccline.cmdlen == ccline.cmdpos)
3001 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003002#ifdef FEAT_MBYTE
3003 else if (has_mbyte)
3004 draw_cmdline(ccline.cmdpos,
3005 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 else
3008 draw_cmdline(ccline.cmdpos, 1);
3009 msg_no_more = FALSE;
3010 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003011 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012}
3013
3014/*
3015 * Put the given string, of the given length, onto the command line.
3016 * If len is -1, then STRLEN() is used to calculate the length.
3017 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3018 * part will be redrawn, otherwise it will not. If this function is called
3019 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3020 * called afterwards.
3021 */
3022 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003023put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024{
3025 int retval;
3026 int i;
3027 int m;
3028 int c;
3029
3030 if (len < 0)
3031 len = (int)STRLEN(str);
3032
3033 /* Check if ccline.cmdbuff needs to be longer */
3034 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003035 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 else
3037 retval = OK;
3038 if (retval == OK)
3039 {
3040 if (!ccline.overstrike)
3041 {
3042 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3043 ccline.cmdbuff + ccline.cmdpos,
3044 (size_t)(ccline.cmdlen - ccline.cmdpos));
3045 ccline.cmdlen += len;
3046 }
3047 else
3048 {
3049#ifdef FEAT_MBYTE
3050 if (has_mbyte)
3051 {
3052 /* Count nr of characters in the new string. */
3053 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003054 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 ++m;
3056 /* Count nr of bytes in cmdline that are overwritten by these
3057 * characters. */
3058 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003059 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 --m;
3061 if (i < ccline.cmdlen)
3062 {
3063 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3064 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3065 ccline.cmdlen += ccline.cmdpos + len - i;
3066 }
3067 else
3068 ccline.cmdlen = ccline.cmdpos + len;
3069 }
3070 else
3071#endif
3072 if (ccline.cmdpos + len > ccline.cmdlen)
3073 ccline.cmdlen = ccline.cmdpos + len;
3074 }
3075 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3076 ccline.cmdbuff[ccline.cmdlen] = NUL;
3077
3078#ifdef FEAT_MBYTE
3079 if (enc_utf8)
3080 {
3081 /* When the inserted text starts with a composing character,
3082 * backup to the character before it. There could be two of them.
3083 */
3084 i = 0;
3085 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3086 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3087 {
3088 i = (*mb_head_off)(ccline.cmdbuff,
3089 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3090 ccline.cmdpos -= i;
3091 len += i;
3092 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3093 }
3094# ifdef FEAT_ARABIC
3095 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3096 {
3097 /* Check the previous character for Arabic combining pair. */
3098 i = (*mb_head_off)(ccline.cmdbuff,
3099 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3100 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3101 + ccline.cmdpos - i), c))
3102 {
3103 ccline.cmdpos -= i;
3104 len += i;
3105 }
3106 else
3107 i = 0;
3108 }
3109# endif
3110 if (i != 0)
3111 {
3112 /* Also backup the cursor position. */
3113 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3114 ccline.cmdspos -= i;
3115 msg_col -= i;
3116 if (msg_col < 0)
3117 {
3118 msg_col += Columns;
3119 --msg_row;
3120 }
3121 }
3122 }
3123#endif
3124
3125 if (redraw && !cmd_silent)
3126 {
3127 msg_no_more = TRUE;
3128 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003129 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3131 /* Avoid clearing the rest of the line too often. */
3132 if (cmdline_row != i || ccline.overstrike)
3133 msg_clr_eos();
3134 msg_no_more = FALSE;
3135 }
3136#ifdef FEAT_FKMAP
3137 /*
3138 * If we are in Farsi command mode, the character input must be in
3139 * Insert mode. So do not advance the cmdpos.
3140 */
3141 if (!cmd_fkmap)
3142#endif
3143 {
3144 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003147 if (m < 0) /* overflow, Columns or Rows at weird value */
3148 m = MAXCOL;
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 else
3151 m = MAXCOL;
3152 for (i = 0; i < len; ++i)
3153 {
3154 c = cmdline_charsize(ccline.cmdpos);
3155#ifdef FEAT_MBYTE
3156 /* count ">" for a double-wide char that doesn't fit. */
3157 if (has_mbyte)
3158 correct_cmdspos(ccline.cmdpos, c);
3159#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003160 /* Stop cursor at the end of the screen, but do increment the
3161 * insert position, so that entering a very long command
3162 * works, even though you can't see it. */
3163 if (ccline.cmdspos + c < m)
3164 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165#ifdef FEAT_MBYTE
3166 if (has_mbyte)
3167 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003168 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (c > len - i - 1)
3170 c = len - i - 1;
3171 ccline.cmdpos += c;
3172 i += c;
3173 }
3174#endif
3175 ++ccline.cmdpos;
3176 }
3177 }
3178 }
3179 if (redraw)
3180 msg_check();
3181 return retval;
3182}
3183
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003184static struct cmdline_info prev_ccline;
3185static int prev_ccline_used = FALSE;
3186
3187/*
3188 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3189 * and overwrite it. But get_cmdline_str() may need it, thus make it
3190 * available globally in prev_ccline.
3191 */
3192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003193save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003194{
3195 if (!prev_ccline_used)
3196 {
3197 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3198 prev_ccline_used = TRUE;
3199 }
3200 *ccp = prev_ccline;
3201 prev_ccline = ccline;
3202 ccline.cmdbuff = NULL;
3203 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003204 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003205}
3206
3207/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003208 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003209 */
3210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003211restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003212{
3213 ccline = prev_ccline;
3214 prev_ccline = *ccp;
3215}
3216
Bram Moolenaar5a305422006-04-28 22:38:25 +00003217#if defined(FEAT_EVAL) || defined(PROTO)
3218/*
3219 * Save the command line into allocated memory. Returns a pointer to be
3220 * passed to restore_cmdline_alloc() later.
3221 * Returns NULL when failed.
3222 */
3223 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003224save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003225{
3226 struct cmdline_info *p;
3227
3228 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3229 if (p != NULL)
3230 save_cmdline(p);
3231 return (char_u *)p;
3232}
3233
3234/*
3235 * Restore the command line from the return value of save_cmdline_alloc().
3236 */
3237 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003238restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003239{
3240 if (p != NULL)
3241 {
3242 restore_cmdline((struct cmdline_info *)p);
3243 vim_free(p);
3244 }
3245}
3246#endif
3247
Bram Moolenaar8299df92004-07-10 09:47:34 +00003248/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003249 * Paste a yank register into the command line.
3250 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003251 * insert_reg() can't be used here, because special characters from the
3252 * register contents will be interpreted as commands.
3253 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003254 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003255 */
3256 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003257cmdline_paste(
3258 int regname,
3259 int literally, /* Insert text literally instead of "as typed" */
3260 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003261{
3262 long i;
3263 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003264 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003265 int allocated;
3266 struct cmdline_info save_ccline;
3267
3268 /* check for valid regname; also accept special characters for CTRL-R in
3269 * the command line */
3270 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3271 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3272 return FAIL;
3273
3274 /* A register containing CTRL-R can cause an endless loop. Allow using
3275 * CTRL-C to break the loop. */
3276 line_breakcheck();
3277 if (got_int)
3278 return FAIL;
3279
3280#ifdef FEAT_CLIPBOARD
3281 regname = may_get_selection(regname);
3282#endif
3283
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003284 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003285 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003286 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003287 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003288 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003289 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003290 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003291
3292 if (i)
3293 {
3294 /* Got the value of a special register in "arg". */
3295 if (arg == NULL)
3296 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003297
3298 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3299 * part of the word. */
3300 p = arg;
3301 if (p_is && regname == Ctrl_W)
3302 {
3303 char_u *w;
3304 int len;
3305
3306 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003307 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003308 {
3309#ifdef FEAT_MBYTE
3310 if (has_mbyte)
3311 {
3312 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3313 if (!vim_iswordc(mb_ptr2char(w - len)))
3314 break;
3315 w -= len;
3316 }
3317 else
3318#endif
3319 {
3320 if (!vim_iswordc(w[-1]))
3321 break;
3322 --w;
3323 }
3324 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003325 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003326 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3327 p += len;
3328 }
3329
3330 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003331 if (allocated)
3332 vim_free(arg);
3333 return OK;
3334 }
3335
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003336 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003337}
3338
3339/*
3340 * Put a string on the command line.
3341 * When "literally" is TRUE, insert literally.
3342 * When "literally" is FALSE, insert as typed, but don't leave the command
3343 * line.
3344 */
3345 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003346cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003347{
3348 int c, cv;
3349
3350 if (literally)
3351 put_on_cmdline(s, -1, TRUE);
3352 else
3353 while (*s != NUL)
3354 {
3355 cv = *s;
3356 if (cv == Ctrl_V && s[1])
3357 ++s;
3358#ifdef FEAT_MBYTE
3359 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003360 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003361 else
3362#endif
3363 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003364 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3365 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003366#ifdef UNIX
3367 || c == intr_char
3368#endif
3369 || (c == Ctrl_BSL && *s == Ctrl_N))
3370 stuffcharReadbuff(Ctrl_V);
3371 stuffcharReadbuff(c);
3372 }
3373}
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#ifdef FEAT_WILDMENU
3376/*
3377 * Delete characters on the command line, from "from" to the current
3378 * position.
3379 */
3380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003381cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
3383 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3384 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3385 ccline.cmdlen -= ccline.cmdpos - from;
3386 ccline.cmdpos = from;
3387}
3388#endif
3389
3390/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003391 * This function is called when the screen size changes and with incremental
3392 * search and in other situations where the command line may have been
3393 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 */
3395 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003396redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003398 redrawcmdline_ex(TRUE);
3399}
3400
3401 void
3402redrawcmdline_ex(int do_compute_cmdrow)
3403{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (cmd_silent)
3405 return;
3406 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003407 if (do_compute_cmdrow)
3408 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 redrawcmd();
3410 cursorcmd();
3411}
3412
3413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003414redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
3416 int i;
3417
3418 if (cmd_silent)
3419 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003420 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 msg_putchar(ccline.cmdfirstc);
3422 if (ccline.cmdprompt != NULL)
3423 {
3424 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3425 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3426 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003427 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 --ccline.cmdindent;
3429 }
3430 else
3431 for (i = ccline.cmdindent; i > 0; --i)
3432 msg_putchar(' ');
3433}
3434
3435/*
3436 * Redraw what is currently on the command line.
3437 */
3438 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003439redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
3441 if (cmd_silent)
3442 return;
3443
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003444 /* when 'incsearch' is set there may be no command line while redrawing */
3445 if (ccline.cmdbuff == NULL)
3446 {
3447 windgoto(cmdline_row, 0);
3448 msg_clr_eos();
3449 return;
3450 }
3451
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 msg_start();
3453 redrawcmdprompt();
3454
3455 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3456 msg_no_more = TRUE;
3457 draw_cmdline(0, ccline.cmdlen);
3458 msg_clr_eos();
3459 msg_no_more = FALSE;
3460
3461 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003462 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003463 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 /*
3466 * An emsg() before may have set msg_scroll. This is used in normal mode,
3467 * in cmdline mode we can reset them now.
3468 */
3469 msg_scroll = FALSE; /* next message overwrites cmdline */
3470
3471 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3472 * in cmdline mode */
3473 skip_redraw = FALSE;
3474}
3475
3476 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003477compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003479 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 cmdline_row = Rows - 1;
3481 else
3482 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003483 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484}
3485
3486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003487cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488{
3489 if (cmd_silent)
3490 return;
3491
3492#ifdef FEAT_RIGHTLEFT
3493 if (cmdmsg_rl)
3494 {
3495 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3496 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3497 if (msg_row <= 0)
3498 msg_row = Rows - 1;
3499 }
3500 else
3501#endif
3502 {
3503 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3504 msg_col = ccline.cmdspos % (int)Columns;
3505 if (msg_row >= Rows)
3506 msg_row = Rows - 1;
3507 }
3508
3509 windgoto(msg_row, msg_col);
3510#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003511 if (p_imst == IM_ON_THE_SPOT)
3512 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513#endif
3514#ifdef MCH_CURSOR_SHAPE
3515 mch_update_cursor();
3516#endif
3517}
3518
3519 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003520gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521{
3522 msg_start();
3523#ifdef FEAT_RIGHTLEFT
3524 if (cmdmsg_rl)
3525 msg_col = Columns - 1;
3526 else
3527#endif
3528 msg_col = 0; /* always start in column 0 */
3529 if (clr) /* clear the bottom line(s) */
3530 msg_clr_eos(); /* will reset clear_cmdline */
3531 windgoto(cmdline_row, 0);
3532}
3533
3534/*
3535 * Check the word in front of the cursor for an abbreviation.
3536 * Called when the non-id character "c" has been entered.
3537 * When an abbreviation is recognized it is removed from the text with
3538 * backspaces and the replacement string is inserted, followed by "c".
3539 */
3540 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003541ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542{
3543 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3544 return FALSE;
3545
3546 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3547}
3548
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003549#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3550 static int
3551#ifdef __BORLANDC__
3552_RTLENTRYF
3553#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003554sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003555{
3556 char_u *p1 = *(char_u **)s1;
3557 char_u *p2 = *(char_u **)s2;
3558
3559 if (*p1 != '<' && *p2 == '<') return -1;
3560 if (*p1 == '<' && *p2 != '<') return 1;
3561 return STRCMP(p1, p2);
3562}
3563#endif
3564
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565/*
3566 * Return FAIL if this is not an appropriate context in which to do
3567 * completion of anything, return OK if it is (even if there are no matches).
3568 * For the caller, this means that the character is just passed through like a
3569 * normal character (instead of being expanded). This allows :s/^I^D etc.
3570 */
3571 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003572nextwild(
3573 expand_T *xp,
3574 int type,
3575 int options, /* extra options for ExpandOne() */
3576 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
3578 int i, j;
3579 char_u *p1;
3580 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 int difflen;
3582 int v;
3583
3584 if (xp->xp_numfiles == -1)
3585 {
3586 set_expand_context(xp);
3587 cmd_showtail = expand_showtail(xp);
3588 }
3589
3590 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3591 {
3592 beep_flush();
3593 return OK; /* Something illegal on command line */
3594 }
3595 if (xp->xp_context == EXPAND_NOTHING)
3596 {
3597 /* Caller can use the character as a normal char instead */
3598 return FAIL;
3599 }
3600
3601 MSG_PUTS("..."); /* show that we are busy */
3602 out_flush();
3603
3604 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003605 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
3607 if (type == WILD_NEXT || type == WILD_PREV)
3608 {
3609 /*
3610 * Get next/previous match for a previous expanded pattern.
3611 */
3612 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3613 }
3614 else
3615 {
3616 /*
3617 * Translate string into pattern and expand it.
3618 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003619 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3620 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 p2 = NULL;
3622 else
3623 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003624 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003625 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3626 if (escape)
3627 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003628
3629 if (p_wic)
3630 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003631 p2 = ExpandOne(xp, p1,
3632 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003633 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003635 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 if (p2 != NULL && type == WILD_LONGEST)
3637 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003638 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (ccline.cmdbuff[i + j] == '*'
3640 || ccline.cmdbuff[i + j] == '?')
3641 break;
3642 if ((int)STRLEN(p2) < j)
3643 {
3644 vim_free(p2);
3645 p2 = NULL;
3646 }
3647 }
3648 }
3649 }
3650
3651 if (p2 != NULL && !got_int)
3652 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003653 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003654 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003656 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 xp->xp_pattern = ccline.cmdbuff + i;
3658 }
3659 else
3660 v = OK;
3661 if (v == OK)
3662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003663 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3664 &ccline.cmdbuff[ccline.cmdpos],
3665 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3666 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 ccline.cmdlen += difflen;
3668 ccline.cmdpos += difflen;
3669 }
3670 }
3671 vim_free(p2);
3672
3673 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003674 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
3676 /* When expanding a ":map" command and no matches are found, assume that
3677 * the key is supposed to be inserted literally */
3678 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3679 return FAIL;
3680
3681 if (xp->xp_numfiles <= 0 && p2 == NULL)
3682 beep_flush();
3683 else if (xp->xp_numfiles == 1)
3684 /* free expanded pattern */
3685 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3686
3687 return OK;
3688}
3689
3690/*
3691 * Do wildcard expansion on the string 'str'.
3692 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003693 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 * Return NULL for failure.
3695 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003696 * "orig" is the originally expanded string, copied to allocated memory. It
3697 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3698 * WILD_PREV "orig" should be NULL.
3699 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003700 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3701 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 *
3703 * mode = WILD_FREE: just free previously expanded matches
3704 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3705 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3706 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3707 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3708 * mode = WILD_ALL: return all matches concatenated
3709 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003710 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 *
3712 * options = WILD_LIST_NOTFOUND: list entries without a match
3713 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3714 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3715 * options = WILD_NO_BEEP: Don't beep for multiple matches
3716 * options = WILD_ADD_SLASH: add a slash after directory names
3717 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3718 * options = WILD_SILENT: don't print warning messages
3719 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003720 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 *
3722 * The variables xp->xp_context and xp->xp_backslash must have been set!
3723 */
3724 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003725ExpandOne(
3726 expand_T *xp,
3727 char_u *str,
3728 char_u *orig, /* allocated copy of original of expanded string */
3729 int options,
3730 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
3732 char_u *ss = NULL;
3733 static int findex;
3734 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003735 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 int i;
3737 long_u len;
3738 int non_suf_match; /* number without matching suffix */
3739
3740 /*
3741 * first handle the case of using an old match
3742 */
3743 if (mode == WILD_NEXT || mode == WILD_PREV)
3744 {
3745 if (xp->xp_numfiles > 0)
3746 {
3747 if (mode == WILD_PREV)
3748 {
3749 if (findex == -1)
3750 findex = xp->xp_numfiles;
3751 --findex;
3752 }
3753 else /* mode == WILD_NEXT */
3754 ++findex;
3755
3756 /*
3757 * When wrapping around, return the original string, set findex to
3758 * -1.
3759 */
3760 if (findex < 0)
3761 {
3762 if (orig_save == NULL)
3763 findex = xp->xp_numfiles - 1;
3764 else
3765 findex = -1;
3766 }
3767 if (findex >= xp->xp_numfiles)
3768 {
3769 if (orig_save == NULL)
3770 findex = 0;
3771 else
3772 findex = -1;
3773 }
3774#ifdef FEAT_WILDMENU
3775 if (p_wmnu)
3776 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3777 findex, cmd_showtail);
3778#endif
3779 if (findex == -1)
3780 return vim_strsave(orig_save);
3781 return vim_strsave(xp->xp_files[findex]);
3782 }
3783 else
3784 return NULL;
3785 }
3786
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003787 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3789 {
3790 FreeWild(xp->xp_numfiles, xp->xp_files);
3791 xp->xp_numfiles = -1;
3792 vim_free(orig_save);
3793 orig_save = NULL;
3794 }
3795 findex = 0;
3796
3797 if (mode == WILD_FREE) /* only release file name */
3798 return NULL;
3799
3800 if (xp->xp_numfiles == -1)
3801 {
3802 vim_free(orig_save);
3803 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003804 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805
3806 /*
3807 * Do the expansion.
3808 */
3809 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3810 options) == FAIL)
3811 {
3812#ifdef FNAME_ILLEGAL
3813 /* Illegal file name has been silently skipped. But when there
3814 * are wildcards, the real problem is that there was no match,
3815 * causing the pattern to be added, which has illegal characters.
3816 */
3817 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3818 EMSG2(_(e_nomatch2), str);
3819#endif
3820 }
3821 else if (xp->xp_numfiles == 0)
3822 {
3823 if (!(options & WILD_SILENT))
3824 EMSG2(_(e_nomatch2), str);
3825 }
3826 else
3827 {
3828 /* Escape the matches for use on the command line. */
3829 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3830
3831 /*
3832 * Check for matching suffixes in file names.
3833 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003834 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3835 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 {
3837 if (xp->xp_numfiles)
3838 non_suf_match = xp->xp_numfiles;
3839 else
3840 non_suf_match = 1;
3841 if ((xp->xp_context == EXPAND_FILES
3842 || xp->xp_context == EXPAND_DIRECTORIES)
3843 && xp->xp_numfiles > 1)
3844 {
3845 /*
3846 * More than one match; check suffix.
3847 * The files will have been sorted on matching suffix in
3848 * expand_wildcards, only need to check the first two.
3849 */
3850 non_suf_match = 0;
3851 for (i = 0; i < 2; ++i)
3852 if (match_suffix(xp->xp_files[i]))
3853 ++non_suf_match;
3854 }
3855 if (non_suf_match != 1)
3856 {
3857 /* Can we ever get here unless it's while expanding
3858 * interactively? If not, we can get rid of this all
3859 * together. Don't really want to wait for this message
3860 * (and possibly have to hit return to continue!).
3861 */
3862 if (!(options & WILD_SILENT))
3863 EMSG(_(e_toomany));
3864 else if (!(options & WILD_NO_BEEP))
3865 beep_flush();
3866 }
3867 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3868 ss = vim_strsave(xp->xp_files[0]);
3869 }
3870 }
3871 }
3872
3873 /* Find longest common part */
3874 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3875 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003876 int mb_len = 1;
3877 int c0, ci;
3878
3879 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003881#ifdef FEAT_MBYTE
3882 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003884 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3885 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3886 }
3887 else
3888#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003889 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003890 for (i = 1; i < xp->xp_numfiles; ++i)
3891 {
3892#ifdef FEAT_MBYTE
3893 if (has_mbyte)
3894 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3895 else
3896#endif
3897 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003898 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003900 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003901 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003903 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 break;
3905 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003906 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 break;
3908 }
3909 if (i < xp->xp_numfiles)
3910 {
3911 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003912 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 break;
3914 }
3915 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003916
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 ss = alloc((unsigned)len + 1);
3918 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003919 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 findex = -1; /* next p_wc gets first one */
3921 }
3922
3923 /* Concatenate all matching names */
3924 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3925 {
3926 len = 0;
3927 for (i = 0; i < xp->xp_numfiles; ++i)
3928 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3929 ss = lalloc(len, TRUE);
3930 if (ss != NULL)
3931 {
3932 *ss = NUL;
3933 for (i = 0; i < xp->xp_numfiles; ++i)
3934 {
3935 STRCAT(ss, xp->xp_files[i]);
3936 if (i != xp->xp_numfiles - 1)
3937 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3938 }
3939 }
3940 }
3941
3942 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3943 ExpandCleanup(xp);
3944
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003945 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003946 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003947 vim_free(orig);
3948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 return ss;
3950}
3951
3952/*
3953 * Prepare an expand structure for use.
3954 */
3955 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003956ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003958 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003959 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003961#ifndef BACKSLASH_IN_FILENAME
3962 xp->xp_shell = FALSE;
3963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 xp->xp_numfiles = -1;
3965 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003966#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3967 xp->xp_arg = NULL;
3968#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003969 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970}
3971
3972/*
3973 * Cleanup an expand structure after use.
3974 */
3975 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003976ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 if (xp->xp_numfiles >= 0)
3979 {
3980 FreeWild(xp->xp_numfiles, xp->xp_files);
3981 xp->xp_numfiles = -1;
3982 }
3983}
3984
3985 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003986ExpandEscape(
3987 expand_T *xp,
3988 char_u *str,
3989 int numfiles,
3990 char_u **files,
3991 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
3993 int i;
3994 char_u *p;
3995
3996 /*
3997 * May change home directory back to "~"
3998 */
3999 if (options & WILD_HOME_REPLACE)
4000 tilde_replace(str, numfiles, files);
4001
4002 if (options & WILD_ESCAPE)
4003 {
4004 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004005 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004006 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 || xp->xp_context == EXPAND_BUFFERS
4008 || xp->xp_context == EXPAND_DIRECTORIES)
4009 {
4010 /*
4011 * Insert a backslash into a file name before a space, \, %, #
4012 * and wildmatch characters, except '~'.
4013 */
4014 for (i = 0; i < numfiles; ++i)
4015 {
4016 /* for ":set path=" we need to escape spaces twice */
4017 if (xp->xp_backslash == XP_BS_THREE)
4018 {
4019 p = vim_strsave_escaped(files[i], (char_u *)" ");
4020 if (p != NULL)
4021 {
4022 vim_free(files[i]);
4023 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004024#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 p = vim_strsave_escaped(files[i], (char_u *)" ");
4026 if (p != NULL)
4027 {
4028 vim_free(files[i]);
4029 files[i] = p;
4030 }
4031#endif
4032 }
4033 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004034#ifdef BACKSLASH_IN_FILENAME
4035 p = vim_strsave_fnameescape(files[i], FALSE);
4036#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004037 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (p != NULL)
4040 {
4041 vim_free(files[i]);
4042 files[i] = p;
4043 }
4044
4045 /* If 'str' starts with "\~", replace "~" at start of
4046 * files[i] with "\~". */
4047 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004048 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004051
4052 /* If the first file starts with a '+' escape it. Otherwise it
4053 * could be seen as "+cmd". */
4054 if (*files[0] == '+')
4055 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 }
4057 else if (xp->xp_context == EXPAND_TAGS)
4058 {
4059 /*
4060 * Insert a backslash before characters in a tag name that
4061 * would terminate the ":tag" command.
4062 */
4063 for (i = 0; i < numfiles; ++i)
4064 {
4065 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4066 if (p != NULL)
4067 {
4068 vim_free(files[i]);
4069 files[i] = p;
4070 }
4071 }
4072 }
4073 }
4074}
4075
4076/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004077 * Escape special characters in "fname" for when used as a file name argument
4078 * after a Vim command, or, when "shell" is non-zero, a shell command.
4079 * Returns the result in allocated memory.
4080 */
4081 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004082vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004083{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004084 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004085#ifdef BACKSLASH_IN_FILENAME
4086 char_u buf[20];
4087 int j = 0;
4088
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004089 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004090 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004091 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004092 buf[j++] = *p;
4093 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004094 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004095#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004096 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4097 if (shell && csh_like_shell() && p != NULL)
4098 {
4099 char_u *s;
4100
4101 /* For csh and similar shells need to put two backslashes before '!'.
4102 * One is taken by Vim, one by the shell. */
4103 s = vim_strsave_escaped(p, (char_u *)"!");
4104 vim_free(p);
4105 p = s;
4106 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004107#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004108
4109 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4110 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004111 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004112 escape_fname(&p);
4113
4114 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004115}
4116
4117/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004118 * Put a backslash before the file name in "pp", which is in allocated memory.
4119 */
4120 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004121escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004122{
4123 char_u *p;
4124
4125 p = alloc((unsigned)(STRLEN(*pp) + 2));
4126 if (p != NULL)
4127 {
4128 p[0] = '\\';
4129 STRCPY(p + 1, *pp);
4130 vim_free(*pp);
4131 *pp = p;
4132 }
4133}
4134
4135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 * For each file name in files[num_files]:
4137 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4138 */
4139 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004140tilde_replace(
4141 char_u *orig_pat,
4142 int num_files,
4143 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144{
4145 int i;
4146 char_u *p;
4147
4148 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4149 {
4150 for (i = 0; i < num_files; ++i)
4151 {
4152 p = home_replace_save(NULL, files[i]);
4153 if (p != NULL)
4154 {
4155 vim_free(files[i]);
4156 files[i] = p;
4157 }
4158 }
4159 }
4160}
4161
4162/*
4163 * Show all matches for completion on the command line.
4164 * Returns EXPAND_NOTHING when the character that triggered expansion should
4165 * be inserted like a normal character.
4166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004168showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
4170#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4171 int num_files;
4172 char_u **files_found;
4173 int i, j, k;
4174 int maxlen;
4175 int lines;
4176 int columns;
4177 char_u *p;
4178 int lastlen;
4179 int attr;
4180 int showtail;
4181
4182 if (xp->xp_numfiles == -1)
4183 {
4184 set_expand_context(xp);
4185 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4186 &num_files, &files_found);
4187 showtail = expand_showtail(xp);
4188 if (i != EXPAND_OK)
4189 return i;
4190
4191 }
4192 else
4193 {
4194 num_files = xp->xp_numfiles;
4195 files_found = xp->xp_files;
4196 showtail = cmd_showtail;
4197 }
4198
4199#ifdef FEAT_WILDMENU
4200 if (!wildmenu)
4201 {
4202#endif
4203 msg_didany = FALSE; /* lines_left will be set */
4204 msg_start(); /* prepare for paging */
4205 msg_putchar('\n');
4206 out_flush();
4207 cmdline_row = msg_row;
4208 msg_didany = FALSE; /* lines_left will be set again */
4209 msg_start(); /* prepare for paging */
4210#ifdef FEAT_WILDMENU
4211 }
4212#endif
4213
4214 if (got_int)
4215 got_int = FALSE; /* only int. the completion, not the cmd line */
4216#ifdef FEAT_WILDMENU
4217 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004218 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219#endif
4220 else
4221 {
4222 /* find the length of the longest file name */
4223 maxlen = 0;
4224 for (i = 0; i < num_files; ++i)
4225 {
4226 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004227 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 || xp->xp_context == EXPAND_BUFFERS))
4229 {
4230 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4231 j = vim_strsize(NameBuff);
4232 }
4233 else
4234 j = vim_strsize(L_SHOWFILE(i));
4235 if (j > maxlen)
4236 maxlen = j;
4237 }
4238
4239 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4240 lines = num_files;
4241 else
4242 {
4243 /* compute the number of columns and lines for the listing */
4244 maxlen += 2; /* two spaces between file names */
4245 columns = ((int)Columns + 2) / maxlen;
4246 if (columns < 1)
4247 columns = 1;
4248 lines = (num_files + columns - 1) / columns;
4249 }
4250
Bram Moolenaar8820b482017-03-16 17:23:31 +01004251 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
4253 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4254 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004255 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 msg_clr_eos();
4257 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004258 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260
4261 /* list the files line by line */
4262 for (i = 0; i < lines; ++i)
4263 {
4264 lastlen = 999;
4265 for (k = i; k < num_files; k += lines)
4266 {
4267 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4268 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004269 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 p = files_found[k] + STRLEN(files_found[k]) + 1;
4271 msg_advance(maxlen + 1);
4272 msg_puts(p);
4273 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004274 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 break;
4276 }
4277 for (j = maxlen - lastlen; --j >= 0; )
4278 msg_putchar(' ');
4279 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004280 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 || xp->xp_context == EXPAND_BUFFERS)
4282 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004283 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004284 if (xp->xp_numfiles != -1)
4285 {
4286 char_u *halved_slash;
4287 char_u *exp_path;
4288
4289 /* Expansion was done before and special characters
4290 * were escaped, need to halve backslashes. Also
4291 * $HOME has been replaced with ~/. */
4292 exp_path = expand_env_save_opt(files_found[k], TRUE);
4293 halved_slash = backslash_halve_save(
4294 exp_path != NULL ? exp_path : files_found[k]);
4295 j = mch_isdir(halved_slash != NULL ? halved_slash
4296 : files_found[k]);
4297 vim_free(exp_path);
4298 vim_free(halved_slash);
4299 }
4300 else
4301 /* Expansion was done here, file names are literal. */
4302 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (showtail)
4304 p = L_SHOWFILE(k);
4305 else
4306 {
4307 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4308 TRUE);
4309 p = NameBuff;
4310 }
4311 }
4312 else
4313 {
4314 j = FALSE;
4315 p = L_SHOWFILE(k);
4316 }
4317 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4318 }
4319 if (msg_col > 0) /* when not wrapped around */
4320 {
4321 msg_clr_eos();
4322 msg_putchar('\n');
4323 }
4324 out_flush(); /* show one line at a time */
4325 if (got_int)
4326 {
4327 got_int = FALSE;
4328 break;
4329 }
4330 }
4331
4332 /*
4333 * we redraw the command below the lines that we have just listed
4334 * This is a bit tricky, but it saves a lot of screen updating.
4335 */
4336 cmdline_row = msg_row; /* will put it back later */
4337 }
4338
4339 if (xp->xp_numfiles == -1)
4340 FreeWild(num_files, files_found);
4341
4342 return EXPAND_OK;
4343}
4344
4345/*
4346 * Private gettail for showmatches() (and win_redr_status_matches()):
4347 * Find tail of file name path, but ignore trailing "/".
4348 */
4349 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004350sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351{
4352 char_u *p;
4353 char_u *t = s;
4354 int had_sep = FALSE;
4355
4356 for (p = s; *p != NUL; )
4357 {
4358 if (vim_ispathsep(*p)
4359#ifdef BACKSLASH_IN_FILENAME
4360 && !rem_backslash(p)
4361#endif
4362 )
4363 had_sep = TRUE;
4364 else if (had_sep)
4365 {
4366 t = p;
4367 had_sep = FALSE;
4368 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004369 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 return t;
4372}
4373
4374/*
4375 * Return TRUE if we only need to show the tail of completion matches.
4376 * When not completing file names or there is a wildcard in the path FALSE is
4377 * returned.
4378 */
4379 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004380expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
4382 char_u *s;
4383 char_u *end;
4384
4385 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004386 if (xp->xp_context != EXPAND_FILES
4387 && xp->xp_context != EXPAND_SHELLCMD
4388 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 return FALSE;
4390
4391 end = gettail(xp->xp_pattern);
4392 if (end == xp->xp_pattern) /* there is no path separator */
4393 return FALSE;
4394
4395 for (s = xp->xp_pattern; s < end; s++)
4396 {
4397 /* Skip escaped wildcards. Only when the backslash is not a path
4398 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4399 if (rem_backslash(s))
4400 ++s;
4401 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4402 return FALSE;
4403 }
4404 return TRUE;
4405}
4406
4407/*
4408 * Prepare a string for expansion.
4409 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004410 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 * When expanding other names: The string will be used with regcomp(). Copy
4412 * the name into allocated memory and prepend "^".
4413 */
4414 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004415addstar(
4416 char_u *fname,
4417 int len,
4418 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419{
4420 char_u *retval;
4421 int i, j;
4422 int new_len;
4423 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004424 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004426 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004427 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004428 && context != EXPAND_SHELLCMD
4429 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
4431 /*
4432 * Matching will be done internally (on something other than files).
4433 * So we convert the file-matching-type wildcards into our kind for
4434 * use with vim_regcomp(). First work out how long it will be:
4435 */
4436
4437 /* For help tags the translation is done in find_help_tags().
4438 * For a tag pattern starting with "/" no translation is needed. */
4439 if (context == EXPAND_HELP
4440 || context == EXPAND_COLORS
4441 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004442 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004443 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004444 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004445 || ((context == EXPAND_TAGS_LISTFILES
4446 || context == EXPAND_TAGS)
4447 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 retval = vim_strnsave(fname, len);
4449 else
4450 {
4451 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4452 for (i = 0; i < len; i++)
4453 {
4454 if (fname[i] == '*' || fname[i] == '~')
4455 new_len++; /* '*' needs to be replaced by ".*"
4456 '~' needs to be replaced by "\~" */
4457
4458 /* Buffer names are like file names. "." should be literal */
4459 if (context == EXPAND_BUFFERS && fname[i] == '.')
4460 new_len++; /* "." becomes "\." */
4461
4462 /* Custom expansion takes care of special things, match
4463 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004464 if ((context == EXPAND_USER_DEFINED
4465 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 new_len++; /* '\' becomes "\\" */
4467 }
4468 retval = alloc(new_len);
4469 if (retval != NULL)
4470 {
4471 retval[0] = '^';
4472 j = 1;
4473 for (i = 0; i < len; i++, j++)
4474 {
4475 /* Skip backslash. But why? At least keep it for custom
4476 * expansion. */
4477 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004478 && context != EXPAND_USER_LIST
4479 && fname[i] == '\\'
4480 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 break;
4482
4483 switch (fname[i])
4484 {
4485 case '*': retval[j++] = '.';
4486 break;
4487 case '~': retval[j++] = '\\';
4488 break;
4489 case '?': retval[j] = '.';
4490 continue;
4491 case '.': if (context == EXPAND_BUFFERS)
4492 retval[j++] = '\\';
4493 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004494 case '\\': if (context == EXPAND_USER_DEFINED
4495 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 retval[j++] = '\\';
4497 break;
4498 }
4499 retval[j] = fname[i];
4500 }
4501 retval[j] = NUL;
4502 }
4503 }
4504 }
4505 else
4506 {
4507 retval = alloc(len + 4);
4508 if (retval != NULL)
4509 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004510 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
4512 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004513 * Don't add a star to *, ~, ~user, $var or `cmd`.
4514 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 * ~ would be at the start of the file name, but not the tail.
4516 * $ could be anywhere in the tail.
4517 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004518 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 */
4520 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004521 ends_in_star = (len > 0 && retval[len - 1] == '*');
4522#ifndef BACKSLASH_IN_FILENAME
4523 for (i = len - 2; i >= 0; --i)
4524 {
4525 if (retval[i] != '\\')
4526 break;
4527 ends_in_star = !ends_in_star;
4528 }
4529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004531 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 && vim_strchr(tail, '$') == NULL
4533 && vim_strchr(retval, '`') == NULL)
4534 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004535 else if (len > 0 && retval[len - 1] == '$')
4536 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 retval[len] = NUL;
4538 }
4539 }
4540 return retval;
4541}
4542
4543/*
4544 * Must parse the command line so far to work out what context we are in.
4545 * Completion can then be done based on that context.
4546 * This routine sets the variables:
4547 * xp->xp_pattern The start of the pattern to be expanded within
4548 * the command line (ends at the cursor).
4549 * xp->xp_context The type of thing to expand. Will be one of:
4550 *
4551 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4552 * the command line, like an unknown command. Caller
4553 * should beep.
4554 * EXPAND_NOTHING Unrecognised context for completion, use char like
4555 * a normal char, rather than for completion. eg
4556 * :s/^I/
4557 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4558 * it.
4559 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4560 * EXPAND_FILES After command with XFILE set, or after setting
4561 * with P_EXPAND set. eg :e ^I, :w>>^I
4562 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4563 * when we know only directories are of interest. eg
4564 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004565 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4567 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4568 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4569 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4570 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4571 * EXPAND_EVENTS Complete event names
4572 * EXPAND_SYNTAX Complete :syntax command arguments
4573 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4574 * EXPAND_AUGROUP Complete autocommand group names
4575 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4576 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4577 * eg :unmap a^I , :cunab x^I
4578 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4579 * eg :call sub^I
4580 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4581 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4582 * names in expressions, eg :while s^I
4583 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004584 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 */
4586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004587set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004589 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 if (ccline.cmdfirstc != ':'
4591#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004592 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004593 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#endif
4595 )
4596 {
4597 xp->xp_context = EXPAND_NOTHING;
4598 return;
4599 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004600 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601}
4602
4603 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004604set_cmd_context(
4605 expand_T *xp,
4606 char_u *str, /* start of command line */
4607 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004608 int col, /* position of cursor */
4609 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
4611 int old_char = NUL;
4612 char_u *nextcomm;
4613
4614 /*
4615 * Avoid a UMR warning from Purify, only save the character if it has been
4616 * written before.
4617 */
4618 if (col < len)
4619 old_char = str[col];
4620 str[col] = NUL;
4621 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004622
4623#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004624 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004625 {
4626# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004627 /* pass CMD_SIZE because there is no real command */
4628 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004629# endif
4630 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004631 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004632 {
4633 xp->xp_context = ccline.xp_context;
4634 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004635# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004636 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004637# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004638 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004639 else
4640#endif
4641 while (nextcomm != NULL)
4642 nextcomm = set_one_cmd_context(xp, nextcomm);
4643
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004644 /* Store the string here so that call_user_expand_func() can get to them
4645 * easily. */
4646 xp->xp_line = str;
4647 xp->xp_col = col;
4648
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 str[col] = old_char;
4650}
4651
4652/*
4653 * Expand the command line "str" from context "xp".
4654 * "xp" must have been set by set_cmd_context().
4655 * xp->xp_pattern points into "str", to where the text that is to be expanded
4656 * starts.
4657 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4658 * cursor.
4659 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4660 * key that triggered expansion literally.
4661 * Returns EXPAND_OK otherwise.
4662 */
4663 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004664expand_cmdline(
4665 expand_T *xp,
4666 char_u *str, /* start of command line */
4667 int col, /* position of cursor */
4668 int *matchcount, /* return: nr of matches */
4669 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
4671 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004672 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4675 {
4676 beep_flush();
4677 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4678 }
4679 if (xp->xp_context == EXPAND_NOTHING)
4680 {
4681 /* Caller can use the character as a normal char instead */
4682 return EXPAND_NOTHING;
4683 }
4684
4685 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004686 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4687 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 if (file_str == NULL)
4689 return EXPAND_UNSUCCESSFUL;
4690
Bram Moolenaar94950a92010-12-02 16:01:29 +01004691 if (p_wic)
4692 options += WILD_ICASE;
4693
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004695 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
4697 *matchcount = 0;
4698 *matches = NULL;
4699 }
4700 vim_free(file_str);
4701
4702 return EXPAND_OK;
4703}
4704
4705#ifdef FEAT_MULTI_LANG
4706/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004707 * Cleanup matches for help tags:
4708 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4709 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004711static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
4713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004714cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
4716 int i, j;
4717 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004718 char_u buf[4];
4719 char_u *p = buf;
4720
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004721 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004722 {
4723 *p++ = '@';
4724 *p++ = p_hlg[0];
4725 *p++ = p_hlg[1];
4726 }
4727 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 for (i = 0; i < num_file; ++i)
4730 {
4731 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004732 if (len <= 0)
4733 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004734 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
4736 /* Sorting on priority means the same item in another language may
4737 * be anywhere. Search all items for a match up to the "@en". */
4738 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004739 if (j != i && (int)STRLEN(file[j]) == len + 3
4740 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 break;
4742 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004743 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 file[i][len] = NUL;
4745 }
4746 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004747
4748 if (*buf != NUL)
4749 for (i = 0; i < num_file; ++i)
4750 {
4751 len = (int)STRLEN(file[i]) - 3;
4752 if (len <= 0)
4753 continue;
4754 if (STRCMP(file[i] + len, buf) == 0)
4755 {
4756 /* remove the default language */
4757 file[i][len] = NUL;
4758 }
4759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760}
4761#endif
4762
4763/*
4764 * Do the expansion based on xp->xp_context and "pat".
4765 */
4766 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004767ExpandFromContext(
4768 expand_T *xp,
4769 char_u *pat,
4770 int *num_file,
4771 char_u ***file,
4772 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773{
4774#ifdef FEAT_CMDL_COMPL
4775 regmatch_T regmatch;
4776#endif
4777 int ret;
4778 int flags;
4779
4780 flags = EW_DIR; /* include directories */
4781 if (options & WILD_LIST_NOTFOUND)
4782 flags |= EW_NOTFOUND;
4783 if (options & WILD_ADD_SLASH)
4784 flags |= EW_ADDSLASH;
4785 if (options & WILD_KEEP_ALL)
4786 flags |= EW_KEEPALL;
4787 if (options & WILD_SILENT)
4788 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004789 if (options & WILD_ALLLINKS)
4790 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004792 if (xp->xp_context == EXPAND_FILES
4793 || xp->xp_context == EXPAND_DIRECTORIES
4794 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
4796 /*
4797 * Expand file or directory names.
4798 */
4799 int free_pat = FALSE;
4800 int i;
4801
4802 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4803 * space */
4804 if (xp->xp_backslash != XP_BS_NONE)
4805 {
4806 free_pat = TRUE;
4807 pat = vim_strsave(pat);
4808 for (i = 0; pat[i]; ++i)
4809 if (pat[i] == '\\')
4810 {
4811 if (xp->xp_backslash == XP_BS_THREE
4812 && pat[i + 1] == '\\'
4813 && pat[i + 2] == '\\'
4814 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004815 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if (xp->xp_backslash == XP_BS_ONE
4817 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004818 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820 }
4821
4822 if (xp->xp_context == EXPAND_FILES)
4823 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004824 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4825 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 else
4827 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004828 if (options & WILD_ICASE)
4829 flags |= EW_ICASE;
4830
Bram Moolenaard7834d32009-12-02 16:14:36 +00004831 /* Expand wildcards, supporting %:h and the like. */
4832 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (free_pat)
4834 vim_free(pat);
4835 return ret;
4836 }
4837
4838 *file = (char_u **)"";
4839 *num_file = 0;
4840 if (xp->xp_context == EXPAND_HELP)
4841 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004842 /* With an empty argument we would get all the help tags, which is
4843 * very slow. Get matches for "help" instead. */
4844 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4845 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 {
4847#ifdef FEAT_MULTI_LANG
4848 cleanup_help_tags(*num_file, *file);
4849#endif
4850 return OK;
4851 }
4852 return FAIL;
4853 }
4854
4855#ifndef FEAT_CMDL_COMPL
4856 return FAIL;
4857#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004858 if (xp->xp_context == EXPAND_SHELLCMD)
4859 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (xp->xp_context == EXPAND_OLD_SETTING)
4861 return ExpandOldSetting(num_file, file);
4862 if (xp->xp_context == EXPAND_BUFFERS)
4863 return ExpandBufnames(pat, num_file, file, options);
4864 if (xp->xp_context == EXPAND_TAGS
4865 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4866 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4867 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004868 {
4869 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004870 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4871 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004874 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004875 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004876 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004877 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004878 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004879 {
4880 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004881 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004882 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004883 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004884 {
4885 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004886 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004887 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004888# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4889 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004890 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004891# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004892 if (xp->xp_context == EXPAND_PACKADD)
4893 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
4895 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4896 if (regmatch.regprog == NULL)
4897 return FAIL;
4898
4899 /* set ignore-case according to p_ic, p_scs and pat */
4900 regmatch.rm_ic = ignorecase(pat);
4901
4902 if (xp->xp_context == EXPAND_SETTINGS
4903 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4904 ret = ExpandSettings(xp, &regmatch, num_file, file);
4905 else if (xp->xp_context == EXPAND_MAPPINGS)
4906 ret = ExpandMappings(&regmatch, num_file, file);
4907# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4908 else if (xp->xp_context == EXPAND_USER_DEFINED)
4909 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4910# endif
4911 else
4912 {
4913 static struct expgen
4914 {
4915 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004916 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004918 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 } tab[] =
4920 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004921 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4922 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004923 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004924 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004925#ifdef FEAT_CMDHIST
4926 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004929 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004930 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004931 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4932 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4933 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#endif
4935#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004936 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4937 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4938 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4939 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940#endif
4941#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004942 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4943 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#endif
4945#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004946 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004948#ifdef FEAT_PROFILE
4949 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4950#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004951 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004953 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4954 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004956#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004957 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004958#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004959#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004960 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004961#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004962#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004963 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4966 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004967 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4968 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004970 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004971 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 };
4973 int i;
4974
4975 /*
4976 * Find a context in the table and call the ExpandGeneric() with the
4977 * right function to do the expansion.
4978 */
4979 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004980 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (xp->xp_context == tab[i].context)
4982 {
4983 if (tab[i].ic)
4984 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004985 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004986 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 break;
4988 }
4989 }
4990
Bram Moolenaar473de612013-06-08 18:19:48 +02004991 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 return ret;
4994#endif /* FEAT_CMDL_COMPL */
4995}
4996
4997#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4998/*
4999 * Expand a list of names.
5000 *
5001 * Generic function for command line completion. It calls a function to
5002 * obtain strings, one by one. The strings are matched against a regexp
5003 * program. Matching strings are copied into an array, which is returned.
5004 *
5005 * Returns OK when no problems encountered, FAIL for error (out of memory).
5006 */
5007 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005008ExpandGeneric(
5009 expand_T *xp,
5010 regmatch_T *regmatch,
5011 int *num_file,
5012 char_u ***file,
5013 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005015 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016{
5017 int i;
5018 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005019 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 char_u *str;
5021
5022 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005023 * round == 0: count the number of matching names
5024 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005026 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
5028 for (i = 0; ; ++i)
5029 {
5030 str = (*func)(xp, i);
5031 if (str == NULL) /* end of list */
5032 break;
5033 if (*str == NUL) /* skip empty strings */
5034 continue;
5035
5036 if (vim_regexec(regmatch, str, (colnr_T)0))
5037 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005038 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005040 if (escaped)
5041 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5042 else
5043 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 (*file)[count] = str;
5045#ifdef FEAT_MENU
5046 if (func == get_menu_names && str != NULL)
5047 {
5048 /* test for separator added by get_menu_names() */
5049 str += STRLEN(str) - 1;
5050 if (*str == '\001')
5051 *str = '.';
5052 }
5053#endif
5054 }
5055 ++count;
5056 }
5057 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005058 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 {
5060 if (count == 0)
5061 return OK;
5062 *num_file = count;
5063 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5064 if (*file == NULL)
5065 {
5066 *file = (char_u **)"";
5067 return FAIL;
5068 }
5069 count = 0;
5070 }
5071 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005072
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005073 /* Sort the results. Keep menu's in the specified order. */
5074 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005075 {
5076 if (xp->xp_context == EXPAND_EXPRESSION
5077 || xp->xp_context == EXPAND_FUNCTIONS
5078 || xp->xp_context == EXPAND_USER_FUNC)
5079 /* <SNR> functions should be sorted to the end. */
5080 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5081 sort_func_compare);
5082 else
5083 sort_strings(*file, *num_file);
5084 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005085
Bram Moolenaar4f688582007-07-24 12:34:30 +00005086#ifdef FEAT_CMDL_COMPL
5087 /* Reset the variables used for special highlight names expansion, so that
5088 * they don't show up when getting normal highlight names by ID. */
5089 reset_expand_highlight();
5090#endif
5091
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 return OK;
5093}
5094
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005095/*
5096 * Complete a shell command.
5097 * Returns FAIL or OK;
5098 */
5099 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005100expand_shellcmd(
5101 char_u *filepat, /* pattern to match with command names */
5102 int *num_file, /* return: number of matches */
5103 char_u ***file, /* return: array with matches */
5104 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005105{
5106 char_u *pat;
5107 int i;
5108 char_u *path;
5109 int mustfree = FALSE;
5110 garray_T ga;
5111 char_u *buf = alloc(MAXPATHL);
5112 size_t l;
5113 char_u *s, *e;
5114 int flags = flagsarg;
5115 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005116 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005117
5118 if (buf == NULL)
5119 return FAIL;
5120
5121 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5122 * space */
5123 pat = vim_strsave(filepat);
5124 for (i = 0; pat[i]; ++i)
5125 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005126 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005127
Bram Moolenaarb5971142015-03-21 17:32:19 +01005128 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005129
5130 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005131 if (mch_isFullName(pat))
5132 path = (char_u *)" ";
5133 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005134 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5135 path = (char_u *)".";
5136 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005137 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005138 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005139 if (path == NULL)
5140 path = (char_u *)"";
5141 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005142
5143 /*
5144 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005145 * collect them in "ga". When "." is not in $PATH also expand for the
5146 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005147 */
5148 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005149 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005150 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005151 if (*s == NUL)
5152 {
5153 if (did_curdir)
5154 break;
5155 /* Find directories in the current directory, path is empty. */
5156 did_curdir = TRUE;
5157 }
5158 else if (*s == '.')
5159 did_curdir = TRUE;
5160
Bram Moolenaar68c31742006-09-02 15:54:18 +00005161 if (*s == ' ')
5162 ++s; /* Skip space used for absolute path name. */
5163
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005164#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005165 e = vim_strchr(s, ';');
5166#else
5167 e = vim_strchr(s, ':');
5168#endif
5169 if (e == NULL)
5170 e = s + STRLEN(s);
5171
5172 l = e - s;
5173 if (l > MAXPATHL - 5)
5174 break;
5175 vim_strncpy(buf, s, l);
5176 add_pathsep(buf);
5177 l = STRLEN(buf);
5178 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5179
5180 /* Expand matches in one directory of $PATH. */
5181 ret = expand_wildcards(1, &buf, num_file, file, flags);
5182 if (ret == OK)
5183 {
5184 if (ga_grow(&ga, *num_file) == FAIL)
5185 FreeWild(*num_file, *file);
5186 else
5187 {
5188 for (i = 0; i < *num_file; ++i)
5189 {
5190 s = (*file)[i];
5191 if (STRLEN(s) > l)
5192 {
5193 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005194 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005195 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5196 }
5197 else
5198 vim_free(s);
5199 }
5200 vim_free(*file);
5201 }
5202 }
5203 if (*e != NUL)
5204 ++e;
5205 }
5206 *file = ga.ga_data;
5207 *num_file = ga.ga_len;
5208
5209 vim_free(buf);
5210 vim_free(pat);
5211 if (mustfree)
5212 vim_free(path);
5213 return OK;
5214}
5215
5216
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005218static 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 +00005219
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005221 * Call "user_expand_func()" to invoke a user defined Vim script function and
5222 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005224 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005225call_user_expand_func(
5226 void *(*user_expand_func)(char_u *, int, char_u **, int),
5227 expand_T *xp,
5228 int *num_file,
5229 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005231 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005232 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005235 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005236 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237
Bram Moolenaarb7515462013-06-29 12:58:33 +02005238 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005239 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 *num_file = 0;
5241 *file = NULL;
5242
Bram Moolenaarb7515462013-06-29 12:58:33 +02005243 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005244 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005245 keep = ccline.cmdbuff[ccline.cmdlen];
5246 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005247 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005248
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005249 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005250 args[1] = xp->xp_line;
5251 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 args[2] = num;
5253
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005254 /* Save the cmdline, we don't know what the function may do. */
5255 save_ccline = ccline;
5256 ccline.cmdbuff = NULL;
5257 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005259
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005260 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005261
5262 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005264 if (ccline.cmdbuff != NULL)
5265 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005266
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005267 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005268 return ret;
5269}
5270
5271/*
5272 * Expand names with a function defined by the user.
5273 */
5274 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005275ExpandUserDefined(
5276 expand_T *xp,
5277 regmatch_T *regmatch,
5278 int *num_file,
5279 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005280{
5281 char_u *retstr;
5282 char_u *s;
5283 char_u *e;
5284 char_u keep;
5285 garray_T ga;
5286
5287 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5288 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 return FAIL;
5290
5291 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005292 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
5294 e = vim_strchr(s, '\n');
5295 if (e == NULL)
5296 e = s + STRLEN(s);
5297 keep = *e;
5298 *e = 0;
5299
5300 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5301 {
5302 *e = keep;
5303 if (*e != NUL)
5304 ++e;
5305 continue;
5306 }
5307
5308 if (ga_grow(&ga, 1) == FAIL)
5309 break;
5310
5311 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5312 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
5314 *e = keep;
5315 if (*e != NUL)
5316 ++e;
5317 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005318 vim_free(retstr);
5319 *file = ga.ga_data;
5320 *num_file = ga.ga_len;
5321 return OK;
5322}
5323
5324/*
5325 * Expand names with a list returned by a function defined by the user.
5326 */
5327 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005328ExpandUserList(
5329 expand_T *xp,
5330 int *num_file,
5331 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005332{
5333 list_T *retlist;
5334 listitem_T *li;
5335 garray_T ga;
5336
5337 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5338 if (retlist == NULL)
5339 return FAIL;
5340
5341 ga_init2(&ga, (int)sizeof(char *), 3);
5342 /* Loop over the items in the list. */
5343 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5344 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005345 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5346 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005347
5348 if (ga_grow(&ga, 1) == FAIL)
5349 break;
5350
5351 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005352 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005353 ++ga.ga_len;
5354 }
5355 list_unref(retlist);
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 *file = ga.ga_data;
5358 *num_file = ga.ga_len;
5359 return OK;
5360}
5361#endif
5362
5363/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005364 * Expand color scheme, compiler or filetype names.
5365 * Search from 'runtimepath':
5366 * 'runtimepath'/{dirnames}/{pat}.vim
5367 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5368 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5369 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5370 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005371 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 */
5373 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005374ExpandRTDir(
5375 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005376 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005377 int *num_file,
5378 char_u ***file,
5379 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 char_u *s;
5382 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005383 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005385 int i;
5386 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
5388 *num_file = 0;
5389 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005390 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005391 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005393 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005395 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5396 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005398 ga_clear_strings(&ga);
5399 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005401 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005402 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005403 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005405
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005406 if (flags & DIP_START) {
5407 for (i = 0; dirnames[i] != NULL; ++i)
5408 {
5409 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5410 if (s == NULL)
5411 {
5412 ga_clear_strings(&ga);
5413 return FAIL;
5414 }
5415 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5416 globpath(p_pp, s, &ga, 0);
5417 vim_free(s);
5418 }
5419 }
5420
5421 if (flags & DIP_OPT) {
5422 for (i = 0; dirnames[i] != NULL; ++i)
5423 {
5424 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5425 if (s == NULL)
5426 {
5427 ga_clear_strings(&ga);
5428 return FAIL;
5429 }
5430 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5431 globpath(p_pp, s, &ga, 0);
5432 vim_free(s);
5433 }
5434 }
5435
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005436 for (i = 0; i < ga.ga_len; ++i)
5437 {
5438 match = ((char_u **)ga.ga_data)[i];
5439 s = match;
5440 e = s + STRLEN(s);
5441 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5442 {
5443 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005444 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005445 if (s < match || vim_ispathsep(*s))
5446 break;
5447 ++s;
5448 *e = NUL;
5449 mch_memmove(match, s, e - s + 1);
5450 }
5451 }
5452
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005453 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005454 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005455
5456 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005457 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005458 remove_duplicates(&ga);
5459
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 *file = ga.ga_data;
5461 *num_file = ga.ga_len;
5462 return OK;
5463}
5464
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005465/*
5466 * Expand loadplugin names:
5467 * 'packpath'/pack/ * /opt/{pat}
5468 */
5469 static int
5470ExpandPackAddDir(
5471 char_u *pat,
5472 int *num_file,
5473 char_u ***file)
5474{
5475 char_u *s;
5476 char_u *e;
5477 char_u *match;
5478 garray_T ga;
5479 int i;
5480 int pat_len;
5481
5482 *num_file = 0;
5483 *file = NULL;
5484 pat_len = (int)STRLEN(pat);
5485 ga_init2(&ga, (int)sizeof(char *), 10);
5486
5487 s = alloc((unsigned)(pat_len + 26));
5488 if (s == NULL)
5489 {
5490 ga_clear_strings(&ga);
5491 return FAIL;
5492 }
5493 sprintf((char *)s, "pack/*/opt/%s*", pat);
5494 globpath(p_pp, s, &ga, 0);
5495 vim_free(s);
5496
5497 for (i = 0; i < ga.ga_len; ++i)
5498 {
5499 match = ((char_u **)ga.ga_data)[i];
5500 s = gettail(match);
5501 e = s + STRLEN(s);
5502 mch_memmove(match, s, e - s + 1);
5503 }
5504
5505 if (ga.ga_len == 0)
5506 return FAIL;
5507
5508 /* Sort and remove duplicates which can happen when specifying multiple
5509 * directories in dirnames. */
5510 remove_duplicates(&ga);
5511
5512 *file = ga.ga_data;
5513 *num_file = ga.ga_len;
5514 return OK;
5515}
5516
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517#endif
5518
5519#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5520/*
5521 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005522 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005524 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005525globpath(
5526 char_u *path,
5527 char_u *file,
5528 garray_T *ga,
5529 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530{
5531 expand_T xpc;
5532 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 int num_p;
5535 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
5537 buf = alloc(MAXPATHL);
5538 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005539 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005541 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005543
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 /* Loop over all entries in {path}. */
5545 while (*path != NUL)
5546 {
5547 /* Copy one item of the path to buf[] and concatenate the file name. */
5548 copy_option_part(&path, buf, MAXPATHL, ",");
5549 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5550 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005551# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005552 /* Using the platform's path separator (\) makes vim incorrectly
5553 * treat it as an escape character, use '/' instead. */
5554 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5555 STRCAT(buf, "/");
5556# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005558# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005560 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5561 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005563 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005565 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 for (i = 0; i < num_p; ++i)
5568 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005569 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005570 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005571 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005574
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 FreeWild(num_p, p);
5576 }
5577 }
5578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
5580 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581}
5582
5583#endif
5584
5585#if defined(FEAT_CMDHIST) || defined(PROTO)
5586
5587/*********************************
5588 * Command line history stuff *
5589 *********************************/
5590
5591/*
5592 * Translate a history character to the associated type number.
5593 */
5594 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
5597 if (c == ':')
5598 return HIST_CMD;
5599 if (c == '=')
5600 return HIST_EXPR;
5601 if (c == '@')
5602 return HIST_INPUT;
5603 if (c == '>')
5604 return HIST_DEBUG;
5605 return HIST_SEARCH; /* must be '?' or '/' */
5606}
5607
5608/*
5609 * Table of history names.
5610 * These names are used in :history and various hist...() functions.
5611 * It is sufficient to give the significant prefix of a history name.
5612 */
5613
5614static char *(history_names[]) =
5615{
5616 "cmd",
5617 "search",
5618 "expr",
5619 "input",
5620 "debug",
5621 NULL
5622};
5623
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005624#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5625/*
5626 * Function given to ExpandGeneric() to obtain the possible first
5627 * arguments of the ":history command.
5628 */
5629 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005630get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005631{
5632 static char_u compl[2] = { NUL, NUL };
5633 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005634 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005635 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5636
5637 if (idx < short_names_count)
5638 {
5639 compl[0] = (char_u)short_names[idx];
5640 return compl;
5641 }
5642 if (idx < short_names_count + history_name_count)
5643 return (char_u *)history_names[idx - short_names_count];
5644 if (idx == short_names_count + history_name_count)
5645 return (char_u *)"all";
5646 return NULL;
5647}
5648#endif
5649
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650/*
5651 * init_history() - Initialize the command line history.
5652 * Also used to re-allocate the history when the size changes.
5653 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005655init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 int newlen; /* new length of history table */
5658 histentry_T *temp;
5659 int i;
5660 int j;
5661 int type;
5662
5663 /*
5664 * If size of history table changed, reallocate it
5665 */
5666 newlen = (int)p_hi;
5667 if (newlen != hislen) /* history length changed */
5668 {
5669 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5670 {
5671 if (newlen)
5672 {
5673 temp = (histentry_T *)lalloc(
5674 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5675 if (temp == NULL) /* out of memory! */
5676 {
5677 if (type == 0) /* first one: just keep the old length */
5678 {
5679 newlen = hislen;
5680 break;
5681 }
5682 /* Already changed one table, now we can only have zero
5683 * length for all tables. */
5684 newlen = 0;
5685 type = -1;
5686 continue;
5687 }
5688 }
5689 else
5690 temp = NULL;
5691 if (newlen == 0 || temp != NULL)
5692 {
5693 if (hisidx[type] < 0) /* there are no entries yet */
5694 {
5695 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005696 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
5698 else if (newlen > hislen) /* array becomes bigger */
5699 {
5700 for (i = 0; i <= hisidx[type]; ++i)
5701 temp[i] = history[type][i];
5702 j = i;
5703 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005704 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 for ( ; j < hislen; ++i, ++j)
5706 temp[i] = history[type][j];
5707 }
5708 else /* array becomes smaller or 0 */
5709 {
5710 j = hisidx[type];
5711 for (i = newlen - 1; ; --i)
5712 {
5713 if (i >= 0) /* copy newest entries */
5714 temp[i] = history[type][j];
5715 else /* remove older entries */
5716 vim_free(history[type][j].hisstr);
5717 if (--j < 0)
5718 j = hislen - 1;
5719 if (j == hisidx[type])
5720 break;
5721 }
5722 hisidx[type] = newlen - 1;
5723 }
5724 vim_free(history[type]);
5725 history[type] = temp;
5726 }
5727 }
5728 hislen = newlen;
5729 }
5730}
5731
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005733clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005734{
5735 hisptr->hisnum = 0;
5736 hisptr->viminfo = FALSE;
5737 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005738 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005739}
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741/*
5742 * Check if command line 'str' is already in history.
5743 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5744 */
5745 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005746in_history(
5747 int type,
5748 char_u *str,
5749 int move_to_front, /* Move the entry to the front if it exists */
5750 int sep,
5751 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752{
5753 int i;
5754 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005755 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756
5757 if (hisidx[type] < 0)
5758 return FALSE;
5759 i = hisidx[type];
5760 do
5761 {
5762 if (history[type][i].hisstr == NULL)
5763 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005764
5765 /* For search history, check that the separator character matches as
5766 * well. */
5767 p = history[type][i].hisstr;
5768 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005769 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005770 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
5772 if (!move_to_front)
5773 return TRUE;
5774 last_i = i;
5775 break;
5776 }
5777 if (--i < 0)
5778 i = hislen - 1;
5779 } while (i != hisidx[type]);
5780
5781 if (last_i >= 0)
5782 {
5783 str = history[type][i].hisstr;
5784 while (i != hisidx[type])
5785 {
5786 if (++i >= hislen)
5787 i = 0;
5788 history[type][last_i] = history[type][i];
5789 last_i = i;
5790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005792 history[type][i].viminfo = FALSE;
5793 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005794 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 return TRUE;
5796 }
5797 return FALSE;
5798}
5799
5800/*
5801 * Convert history name (from table above) to its HIST_ equivalent.
5802 * When "name" is empty, return "cmd" history.
5803 * Returns -1 for unknown history name.
5804 */
5805 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005806get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
5808 int i;
5809 int len = (int)STRLEN(name);
5810
5811 /* No argument: use current history. */
5812 if (len == 0)
5813 return hist_char2type(ccline.cmdfirstc);
5814
5815 for (i = 0; history_names[i] != NULL; ++i)
5816 if (STRNICMP(name, history_names[i], len) == 0)
5817 return i;
5818
5819 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5820 return hist_char2type(name[0]);
5821
5822 return -1;
5823}
5824
5825static int last_maptick = -1; /* last seen maptick */
5826
5827/*
5828 * Add the given string to the given history. If the string is already in the
5829 * history then it is moved to the front. "histype" may be one of he HIST_
5830 * values.
5831 */
5832 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005833add_to_history(
5834 int histype,
5835 char_u *new_entry,
5836 int in_map, /* consider maptick when inside a mapping */
5837 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
5839 histentry_T *hisptr;
5840 int len;
5841
5842 if (hislen == 0) /* no history */
5843 return;
5844
Bram Moolenaara939e432013-11-09 05:30:26 +01005845 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5846 return;
5847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 /*
5849 * Searches inside the same mapping overwrite each other, so that only
5850 * the last line is kept. Be careful not to remove a line that was moved
5851 * down, only lines that were added.
5852 */
5853 if (histype == HIST_SEARCH && in_map)
5854 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005855 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 {
5857 /* Current line is from the same mapping, remove it */
5858 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5859 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005860 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 --hisnum[histype];
5862 if (--hisidx[HIST_SEARCH] < 0)
5863 hisidx[HIST_SEARCH] = hislen - 1;
5864 }
5865 last_maptick = -1;
5866 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005867 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 {
5869 if (++hisidx[histype] == hislen)
5870 hisidx[histype] = 0;
5871 hisptr = &history[histype][hisidx[histype]];
5872 vim_free(hisptr->hisstr);
5873
5874 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005875 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5877 if (hisptr->hisstr != NULL)
5878 hisptr->hisstr[len + 1] = sep;
5879
5880 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005881 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005882 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 if (histype == HIST_SEARCH && in_map)
5884 last_maptick = maptick;
5885 }
5886}
5887
5888#if defined(FEAT_EVAL) || defined(PROTO)
5889
5890/*
5891 * Get identifier of newest history entry.
5892 * "histype" may be one of the HIST_ values.
5893 */
5894 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005895get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
5897 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5898 || hisidx[histype] < 0)
5899 return -1;
5900
5901 return history[histype][hisidx[histype]].hisnum;
5902}
5903
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 * Calculate history index from a number:
5906 * num > 0: seen as identifying number of a history entry
5907 * num < 0: relative position in history wrt newest entry
5908 * "histype" may be one of the HIST_ values.
5909 */
5910 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005911calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912{
5913 int i;
5914 histentry_T *hist;
5915 int wrapped = FALSE;
5916
5917 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5918 || (i = hisidx[histype]) < 0 || num == 0)
5919 return -1;
5920
5921 hist = history[histype];
5922 if (num > 0)
5923 {
5924 while (hist[i].hisnum > num)
5925 if (--i < 0)
5926 {
5927 if (wrapped)
5928 break;
5929 i += hislen;
5930 wrapped = TRUE;
5931 }
5932 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5933 return i;
5934 }
5935 else if (-num <= hislen)
5936 {
5937 i += num + 1;
5938 if (i < 0)
5939 i += hislen;
5940 if (hist[i].hisstr != NULL)
5941 return i;
5942 }
5943 return -1;
5944}
5945
5946/*
5947 * Get a history entry by its index.
5948 * "histype" may be one of the HIST_ values.
5949 */
5950 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005951get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952{
5953 idx = calc_hist_idx(histype, idx);
5954 if (idx >= 0)
5955 return history[histype][idx].hisstr;
5956 else
5957 return (char_u *)"";
5958}
5959
5960/*
5961 * Clear all entries of a history.
5962 * "histype" may be one of the HIST_ values.
5963 */
5964 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005965clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
5967 int i;
5968 histentry_T *hisptr;
5969
5970 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5971 {
5972 hisptr = history[histype];
5973 for (i = hislen; i--;)
5974 {
5975 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005976 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005977 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 }
5979 hisidx[histype] = -1; /* mark history as cleared */
5980 hisnum[histype] = 0; /* reset identifier counter */
5981 return OK;
5982 }
5983 return FAIL;
5984}
5985
5986/*
5987 * Remove all entries matching {str} from a history.
5988 * "histype" may be one of the HIST_ values.
5989 */
5990 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005991del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
5993 regmatch_T regmatch;
5994 histentry_T *hisptr;
5995 int idx;
5996 int i;
5997 int last;
5998 int found = FALSE;
5999
6000 regmatch.regprog = NULL;
6001 regmatch.rm_ic = FALSE; /* always match case */
6002 if (hislen != 0
6003 && histype >= 0
6004 && histype < HIST_COUNT
6005 && *str != NUL
6006 && (idx = hisidx[histype]) >= 0
6007 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6008 != NULL)
6009 {
6010 i = last = idx;
6011 do
6012 {
6013 hisptr = &history[histype][i];
6014 if (hisptr->hisstr == NULL)
6015 break;
6016 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6017 {
6018 found = TRUE;
6019 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006020 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 }
6022 else
6023 {
6024 if (i != last)
6025 {
6026 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006027 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 }
6029 if (--last < 0)
6030 last += hislen;
6031 }
6032 if (--i < 0)
6033 i += hislen;
6034 } while (i != idx);
6035 if (history[histype][idx].hisstr == NULL)
6036 hisidx[histype] = -1;
6037 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006038 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 return found;
6040}
6041
6042/*
6043 * Remove an indexed entry from a history.
6044 * "histype" may be one of the HIST_ values.
6045 */
6046 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006047del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
6049 int i, j;
6050
6051 i = calc_hist_idx(histype, idx);
6052 if (i < 0)
6053 return FALSE;
6054 idx = hisidx[histype];
6055 vim_free(history[histype][i].hisstr);
6056
6057 /* When deleting the last added search string in a mapping, reset
6058 * last_maptick, so that the last added search string isn't deleted again.
6059 */
6060 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6061 last_maptick = -1;
6062
6063 while (i != idx)
6064 {
6065 j = (i + 1) % hislen;
6066 history[histype][i] = history[histype][j];
6067 i = j;
6068 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006069 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (--i < 0)
6071 i += hislen;
6072 hisidx[histype] = i;
6073 return TRUE;
6074}
6075
6076#endif /* FEAT_EVAL */
6077
6078#if defined(FEAT_CRYPT) || defined(PROTO)
6079/*
6080 * Very specific function to remove the value in ":set key=val" from the
6081 * history.
6082 */
6083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006084remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
6086 char_u *p;
6087 int i;
6088
6089 i = hisidx[HIST_CMD];
6090 if (i < 0)
6091 return;
6092 p = history[HIST_CMD][i].hisstr;
6093 if (p != NULL)
6094 for ( ; *p; ++p)
6095 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6096 {
6097 p = vim_strchr(p + 3, '=');
6098 if (p == NULL)
6099 break;
6100 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006101 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 if (p[i] == '\\' && p[i + 1])
6103 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006104 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 --p;
6106 }
6107}
6108#endif
6109
6110#endif /* FEAT_CMDHIST */
6111
Bram Moolenaar064154c2016-03-19 22:50:43 +01006112#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006113/*
6114 * Get pointer to the command line info to use. cmdline_paste() may clear
6115 * ccline and put the previous value in prev_ccline.
6116 */
6117 static struct cmdline_info *
6118get_ccline_ptr(void)
6119{
6120 if ((State & CMDLINE) == 0)
6121 return NULL;
6122 if (ccline.cmdbuff != NULL)
6123 return &ccline;
6124 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6125 return &prev_ccline;
6126 return NULL;
6127}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006128#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006129
Bram Moolenaar064154c2016-03-19 22:50:43 +01006130#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006131/*
6132 * Get the current command line in allocated memory.
6133 * Only works when the command line is being edited.
6134 * Returns NULL when something is wrong.
6135 */
6136 char_u *
6137get_cmdline_str(void)
6138{
6139 struct cmdline_info *p = get_ccline_ptr();
6140
6141 if (p == NULL)
6142 return NULL;
6143 return vim_strnsave(p->cmdbuff, p->cmdlen);
6144}
6145
6146/*
6147 * Get the current command line position, counted in bytes.
6148 * Zero is the first position.
6149 * Only works when the command line is being edited.
6150 * Returns -1 when something is wrong.
6151 */
6152 int
6153get_cmdline_pos(void)
6154{
6155 struct cmdline_info *p = get_ccline_ptr();
6156
6157 if (p == NULL)
6158 return -1;
6159 return p->cmdpos;
6160}
6161
6162/*
6163 * Set the command line byte position to "pos". Zero is the first position.
6164 * Only works when the command line is being edited.
6165 * Returns 1 when failed, 0 when OK.
6166 */
6167 int
6168set_cmdline_pos(
6169 int pos)
6170{
6171 struct cmdline_info *p = get_ccline_ptr();
6172
6173 if (p == NULL)
6174 return 1;
6175
6176 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6177 * changed the command line. */
6178 if (pos < 0)
6179 new_cmdpos = 0;
6180 else
6181 new_cmdpos = pos;
6182 return 0;
6183}
6184#endif
6185
6186#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6187/*
6188 * Get the current command-line type.
6189 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6190 * Only works when the command line is being edited.
6191 * Returns NUL when something is wrong.
6192 */
6193 int
6194get_cmdline_type(void)
6195{
6196 struct cmdline_info *p = get_ccline_ptr();
6197
6198 if (p == NULL)
6199 return NUL;
6200 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006201 return
6202# ifdef FEAT_EVAL
6203 (p->input_fn) ? '@' :
6204# endif
6205 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006206 return p->cmdfirstc;
6207}
6208#endif
6209
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6211/*
6212 * Get indices "num1,num2" that specify a range within a list (not a range of
6213 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6214 * Returns OK if parsed successfully, otherwise FAIL.
6215 */
6216 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006217get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218{
6219 int len;
6220 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006221 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
6223 *str = skipwhite(*str);
6224 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6225 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006226 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 *str += len;
6228 *num1 = (int)num;
6229 first = TRUE;
6230 }
6231 *str = skipwhite(*str);
6232 if (**str == ',') /* parse "to" part of range */
6233 {
6234 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006235 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 if (len > 0)
6237 {
6238 *num2 = (int)num;
6239 *str = skipwhite(*str + len);
6240 }
6241 else if (!first) /* no number given at all */
6242 return FAIL;
6243 }
6244 else if (first) /* only one number given */
6245 *num2 = *num1;
6246 return OK;
6247}
6248#endif
6249
6250#if defined(FEAT_CMDHIST) || defined(PROTO)
6251/*
6252 * :history command - print a history
6253 */
6254 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006255ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 histentry_T *hist;
6258 int histype1 = HIST_CMD;
6259 int histype2 = HIST_CMD;
6260 int hisidx1 = 1;
6261 int hisidx2 = -1;
6262 int idx;
6263 int i, j, k;
6264 char_u *end;
6265 char_u *arg = eap->arg;
6266
6267 if (hislen == 0)
6268 {
6269 MSG(_("'history' option is zero"));
6270 return;
6271 }
6272
6273 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6274 {
6275 end = arg;
6276 while (ASCII_ISALPHA(*end)
6277 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6278 end++;
6279 i = *end;
6280 *end = NUL;
6281 histype1 = get_histtype(arg);
6282 if (histype1 == -1)
6283 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006284 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 {
6286 histype1 = 0;
6287 histype2 = HIST_COUNT-1;
6288 }
6289 else
6290 {
6291 *end = i;
6292 EMSG(_(e_trailing));
6293 return;
6294 }
6295 }
6296 else
6297 histype2 = histype1;
6298 *end = i;
6299 }
6300 else
6301 end = arg;
6302 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6303 {
6304 EMSG(_(e_trailing));
6305 return;
6306 }
6307
6308 for (; !got_int && histype1 <= histype2; ++histype1)
6309 {
6310 STRCPY(IObuff, "\n # ");
6311 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6312 MSG_PUTS_TITLE(IObuff);
6313 idx = hisidx[histype1];
6314 hist = history[histype1];
6315 j = hisidx1;
6316 k = hisidx2;
6317 if (j < 0)
6318 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6319 if (k < 0)
6320 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6321 if (idx >= 0 && j <= k)
6322 for (i = idx + 1; !got_int; ++i)
6323 {
6324 if (i == hislen)
6325 i = 0;
6326 if (hist[i].hisstr != NULL
6327 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6328 {
6329 msg_putchar('\n');
6330 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6331 hist[i].hisnum);
6332 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6333 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006334 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 else
6336 STRCAT(IObuff, hist[i].hisstr);
6337 msg_outtrans(IObuff);
6338 out_flush();
6339 }
6340 if (i == idx)
6341 break;
6342 }
6343 }
6344}
6345#endif
6346
6347#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006348/*
6349 * Buffers for history read from a viminfo file. Only valid while reading.
6350 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006351static histentry_T *viminfo_history[HIST_COUNT] =
6352 {NULL, NULL, NULL, NULL, NULL};
6353static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6354static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355static int viminfo_add_at_front = FALSE;
6356
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006357static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358
6359/*
6360 * Translate a history type number to the associated character.
6361 */
6362 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006363hist_type2char(
6364 int type,
6365 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
6367 if (type == HIST_CMD)
6368 return ':';
6369 if (type == HIST_SEARCH)
6370 {
6371 if (use_question)
6372 return '?';
6373 else
6374 return '/';
6375 }
6376 if (type == HIST_EXPR)
6377 return '=';
6378 return '@';
6379}
6380
6381/*
6382 * Prepare for reading the history from the viminfo file.
6383 * This allocates history arrays to store the read history lines.
6384 */
6385 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006386prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
6388 int i;
6389 int num;
6390 int type;
6391 int len;
6392
6393 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006394 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 if (asklen > hislen)
6396 asklen = hislen;
6397
6398 for (type = 0; type < HIST_COUNT; ++type)
6399 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006400 /* Count the number of empty spaces in the history list. Entries read
6401 * from viminfo previously are also considered empty. If there are
6402 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006404 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 num++;
6406 len = asklen;
6407 if (num > len)
6408 len = num;
6409 if (len <= 0)
6410 viminfo_history[type] = NULL;
6411 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006412 viminfo_history[type] = (histentry_T *)lalloc(
6413 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 if (viminfo_history[type] == NULL)
6415 len = 0;
6416 viminfo_hislen[type] = len;
6417 viminfo_hisidx[type] = 0;
6418 }
6419}
6420
6421/*
6422 * Accept a line from the viminfo, store it in the history array when it's
6423 * new.
6424 */
6425 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006426read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 int type;
6429 long_u len;
6430 char_u *val;
6431 char_u *p;
6432
6433 type = hist_char2type(virp->vir_line[0]);
6434 if (viminfo_hisidx[type] < viminfo_hislen[type])
6435 {
6436 val = viminfo_readstring(virp, 1, TRUE);
6437 if (val != NULL && *val != NUL)
6438 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006439 int sep = (*val == ' ' ? NUL : *val);
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006442 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 {
6444 /* Need to re-allocate to append the separator byte. */
6445 len = STRLEN(val);
6446 p = lalloc(len + 2, TRUE);
6447 if (p != NULL)
6448 {
6449 if (type == HIST_SEARCH)
6450 {
6451 /* Search entry: Move the separator from the first
6452 * column to after the NUL. */
6453 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006454 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 }
6456 else
6457 {
6458 /* Not a search entry: No separator in the viminfo
6459 * file, add a NUL separator. */
6460 mch_memmove(p, val, (size_t)len + 1);
6461 p[len + 1] = NUL;
6462 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006463 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6464 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006465 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6466 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006467 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 }
6469 }
6470 }
6471 vim_free(val);
6472 }
6473 return viminfo_readline(virp);
6474}
6475
Bram Moolenaar07219f92013-04-14 23:19:36 +02006476/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006477 * Accept a new style history line from the viminfo, store it in the history
6478 * array when it's new.
6479 */
6480 void
6481handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006482 garray_T *values,
6483 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006484{
6485 int type;
6486 long_u len;
6487 char_u *val;
6488 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006489 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006490
6491 /* Check the format:
6492 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006493 if (values->ga_len < 4
6494 || vp[0].bv_type != BVAL_NR
6495 || vp[1].bv_type != BVAL_NR
6496 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6497 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006498 return;
6499
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006500 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006501 if (type >= HIST_COUNT)
6502 return;
6503 if (viminfo_hisidx[type] < viminfo_hislen[type])
6504 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006505 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006506 if (val != NULL && *val != NUL)
6507 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006508 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6509 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006510 int idx;
6511 int overwrite = FALSE;
6512
6513 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6514 {
6515 /* If lines were written by an older Vim we need to avoid
6516 * getting duplicates. See if the entry already exists. */
6517 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6518 {
6519 p = viminfo_history[type][idx].hisstr;
6520 if (STRCMP(val, p) == 0
6521 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6522 {
6523 overwrite = TRUE;
6524 break;
6525 }
6526 }
6527
6528 if (!overwrite)
6529 {
6530 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006531 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006532 p = lalloc(len + 2, TRUE);
6533 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006534 else
6535 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006536 if (p != NULL)
6537 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006538 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006539 if (!overwrite)
6540 {
6541 mch_memmove(p, val, (size_t)len + 1);
6542 /* Put the separator after the NUL. */
6543 p[len + 1] = sep;
6544 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006545 viminfo_history[type][idx].hisnum = 0;
6546 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006547 viminfo_hisidx[type]++;
6548 }
6549 }
6550 }
6551 }
6552 }
6553}
6554
6555/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006556 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006557 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006558 static void
6559concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 int idx;
6562 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006563
6564 idx = hisidx[type] + viminfo_hisidx[type];
6565 if (idx >= hislen)
6566 idx -= hislen;
6567 else if (idx < 0)
6568 idx = hislen - 1;
6569 if (viminfo_add_at_front)
6570 hisidx[type] = idx;
6571 else
6572 {
6573 if (hisidx[type] == -1)
6574 hisidx[type] = hislen - 1;
6575 do
6576 {
6577 if (history[type][idx].hisstr != NULL
6578 || history[type][idx].viminfo)
6579 break;
6580 if (++idx == hislen)
6581 idx = 0;
6582 } while (idx != hisidx[type]);
6583 if (idx != hisidx[type] && --idx < 0)
6584 idx = hislen - 1;
6585 }
6586 for (i = 0; i < viminfo_hisidx[type]; i++)
6587 {
6588 vim_free(history[type][idx].hisstr);
6589 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6590 history[type][idx].viminfo = TRUE;
6591 history[type][idx].time_set = viminfo_history[type][i].time_set;
6592 if (--idx < 0)
6593 idx = hislen - 1;
6594 }
6595 idx += 1;
6596 idx %= hislen;
6597 for (i = 0; i < viminfo_hisidx[type]; i++)
6598 {
6599 history[type][idx++].hisnum = ++hisnum[type];
6600 idx %= hislen;
6601 }
6602}
6603
6604#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6605 static int
6606#ifdef __BORLANDC__
6607_RTLENTRYF
6608#endif
6609sort_hist(const void *s1, const void *s2)
6610{
6611 histentry_T *p1 = *(histentry_T **)s1;
6612 histentry_T *p2 = *(histentry_T **)s2;
6613
6614 if (p1->time_set < p2->time_set) return -1;
6615 if (p1->time_set > p2->time_set) return 1;
6616 return 0;
6617}
6618#endif
6619
6620/*
6621 * Merge history lines from viminfo and lines typed in this Vim based on the
6622 * timestamp;
6623 */
6624 static void
6625merge_history(int type)
6626{
6627 int max_len;
6628 histentry_T **tot_hist;
6629 histentry_T *new_hist;
6630 int i;
6631 int len;
6632
6633 /* Make one long list with all entries. */
6634 max_len = hislen + viminfo_hisidx[type];
6635 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6636 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6637 if (tot_hist == NULL || new_hist == NULL)
6638 {
6639 vim_free(tot_hist);
6640 vim_free(new_hist);
6641 return;
6642 }
6643 for (i = 0; i < viminfo_hisidx[type]; i++)
6644 tot_hist[i] = &viminfo_history[type][i];
6645 len = i;
6646 for (i = 0; i < hislen; i++)
6647 if (history[type][i].hisstr != NULL)
6648 tot_hist[len++] = &history[type][i];
6649
6650 /* Sort the list on timestamp. */
6651 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6652
6653 /* Keep the newest ones. */
6654 for (i = 0; i < hislen; i++)
6655 {
6656 if (i < len)
6657 {
6658 new_hist[i] = *tot_hist[i];
6659 tot_hist[i]->hisstr = NULL;
6660 if (new_hist[i].hisnum == 0)
6661 new_hist[i].hisnum = ++hisnum[type];
6662 }
6663 else
6664 clear_hist_entry(&new_hist[i]);
6665 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006666 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006667
6668 /* Free what is not kept. */
6669 for (i = 0; i < viminfo_hisidx[type]; i++)
6670 vim_free(viminfo_history[type][i].hisstr);
6671 for (i = 0; i < hislen; i++)
6672 vim_free(history[type][i].hisstr);
6673 vim_free(history[type]);
6674 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006675 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006676}
6677
6678/*
6679 * Finish reading history lines from viminfo. Not used when writing viminfo.
6680 */
6681 void
6682finish_viminfo_history(vir_T *virp)
6683{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006685 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
6687 for (type = 0; type < HIST_COUNT; ++type)
6688 {
6689 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006690 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006691
6692 if (merge)
6693 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006695 concat_history(type);
6696
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 vim_free(viminfo_history[type]);
6698 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006699 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 }
6701}
6702
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006703/*
6704 * Write history to viminfo file in "fp".
6705 * When "merge" is TRUE merge history lines with a previously read viminfo
6706 * file, data is in viminfo_history[].
6707 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006710write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
6712 int i;
6713 int type;
6714 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006715 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716
6717 init_history();
6718 if (hislen == 0)
6719 return;
6720 for (type = 0; type < HIST_COUNT; ++type)
6721 {
6722 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6723 if (num_saved == 0)
6724 continue;
6725 if (num_saved < 0) /* Use default */
6726 num_saved = hislen;
6727 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6728 type == HIST_CMD ? _("Command Line") :
6729 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006730 type == HIST_EXPR ? _("Expression") :
6731 type == HIST_INPUT ? _("Input Line") :
6732 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (num_saved > hislen)
6734 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006735
6736 /*
6737 * Merge typed and viminfo history:
6738 * round 1: history of typed commands.
6739 * round 2: history from recently read viminfo.
6740 */
6741 for (round = 1; round <= 2; ++round)
6742 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006743 if (round == 1)
6744 /* start at newest entry, somewhere in the list */
6745 i = hisidx[type];
6746 else if (viminfo_hisidx[type] > 0)
6747 /* start at newest entry, first in the list */
6748 i = 0;
6749 else
6750 /* empty list */
6751 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006752 if (i >= 0)
6753 while (num_saved > 0
6754 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006756 char_u *p;
6757 time_t timestamp;
6758 int c = NUL;
6759
6760 if (round == 1)
6761 {
6762 p = history[type][i].hisstr;
6763 timestamp = history[type][i].time_set;
6764 }
6765 else
6766 {
6767 p = viminfo_history[type] == NULL ? NULL
6768 : viminfo_history[type][i].hisstr;
6769 timestamp = viminfo_history[type] == NULL ? 0
6770 : viminfo_history[type][i].time_set;
6771 }
6772
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006773 if (p != NULL && (round == 2
6774 || !merge
6775 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006777 --num_saved;
6778 fputc(hist_type2char(type, TRUE), fp);
6779 /* For the search history: put the separator in the
6780 * second column; use a space if there isn't one. */
6781 if (type == HIST_SEARCH)
6782 {
6783 c = p[STRLEN(p) + 1];
6784 putc(c == NUL ? ' ' : c, fp);
6785 }
6786 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006787
6788 {
6789 char cbuf[NUMBUFLEN];
6790
6791 /* New style history with a bar line. Format:
6792 * |{bartype},{histtype},{timestamp},{separator},"text" */
6793 if (c == NUL)
6794 cbuf[0] = NUL;
6795 else
6796 sprintf(cbuf, "%d", c);
6797 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6798 type, (long)timestamp, cbuf);
6799 barline_writestring(fp, p, LSIZE - 20);
6800 putc('\n', fp);
6801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006803 if (round == 1)
6804 {
6805 /* Decrement index, loop around and stop when back at
6806 * the start. */
6807 if (--i < 0)
6808 i = hislen - 1;
6809 if (i == hisidx[type])
6810 break;
6811 }
6812 else
6813 {
6814 /* Increment index. Stop at the end in the while. */
6815 ++i;
6816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006818 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006819 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006820 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006821 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006822 vim_free(viminfo_history[type]);
6823 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006824 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
6826}
6827#endif /* FEAT_VIMINFO */
6828
6829#if defined(FEAT_FKMAP) || defined(PROTO)
6830/*
6831 * Write a character at the current cursor+offset position.
6832 * It is directly written into the command buffer block.
6833 */
6834 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006835cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836{
6837 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6838 {
6839 EMSG(_("E198: cmd_pchar beyond the command length"));
6840 return;
6841 }
6842 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6843 ccline.cmdbuff[ccline.cmdlen] = NUL;
6844}
6845
6846 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006847cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6850 {
6851 /* EMSG(_("cmd_gchar beyond the command length")); */
6852 return NUL;
6853 }
6854 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6855}
6856#endif
6857
6858#if defined(FEAT_CMDWIN) || defined(PROTO)
6859/*
6860 * Open a window on the current command line and history. Allow editing in
6861 * the window. Returns when the window is closed.
6862 * Returns:
6863 * CR if the command is to be executed
6864 * Ctrl_C if it is to be abandoned
6865 * K_IGNORE if editing continues
6866 */
6867 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006868open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006871 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006873 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 win_T *wp;
6875 int i;
6876 linenr_T lnum;
6877 int histtype;
6878 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 int save_restart_edit = restart_edit;
6880 int save_State = State;
6881 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006882#ifdef FEAT_RIGHTLEFT
6883 int save_cmdmsg_rl = cmdmsg_rl;
6884#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006885#ifdef FEAT_FOLDING
6886 int save_KeyTyped;
6887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888
6889 /* Can't do this recursively. Can't do it when typing a password. */
6890 if (cmdwin_type != 0
6891# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6892 || cmdline_star > 0
6893# endif
6894 )
6895 {
6896 beep_flush();
6897 return K_IGNORE;
6898 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006899 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900
6901 /* Save current window sizes. */
6902 win_size_save(&winsizes);
6903
6904# ifdef FEAT_AUTOCMD
6905 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006906 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006908 /* don't use a new tab page */
6909 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006910 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006911
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 /* Create a window for the command-line buffer. */
6913 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6914 {
6915 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006916# ifdef FEAT_AUTOCMD
6917 unblock_autocmds();
6918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 return K_IGNORE;
6920 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006921 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922
6923 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006924 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006925 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006928#ifdef FEAT_FOLDING
6929 curwin->w_p_fen = FALSE;
6930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006932 curwin->w_p_rl = cmdmsg_rl;
6933 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006935 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936
6937# ifdef FEAT_AUTOCMD
6938 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006939 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006940 /* But don't allow switching to another buffer. */
6941 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942# endif
6943
Bram Moolenaar46152342005-09-07 21:18:43 +00006944 /* Showing the prompt may have set need_wait_return, reset it. */
6945 need_wait_return = FALSE;
6946
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006947 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6949 {
6950 if (p_wc == TAB)
6951 {
6952 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6953 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6954 }
6955 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6956 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006957# ifdef FEAT_AUTOCMD
6958 --curbuf_lock;
6959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006961 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6962 * sets 'textwidth' to 78). */
6963 curbuf->b_p_tw = 0;
6964
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 /* Fill the buffer with the history. */
6966 init_history();
6967 if (hislen > 0)
6968 {
6969 i = hisidx[histtype];
6970 if (i >= 0)
6971 {
6972 lnum = 0;
6973 do
6974 {
6975 if (++i == hislen)
6976 i = 0;
6977 if (history[histtype][i].hisstr != NULL)
6978 ml_append(lnum++, history[histtype][i].hisstr,
6979 (colnr_T)0, FALSE);
6980 }
6981 while (i != hisidx[histtype]);
6982 }
6983 }
6984
6985 /* Replace the empty last line with the current command-line and put the
6986 * cursor there. */
6987 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6988 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6989 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006990 changed_line_abv_curs();
6991 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006992 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
6994 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01006995 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
6997 /* No Ex mode here! */
6998 exmode_active = 0;
6999
7000 State = NORMAL;
7001# ifdef FEAT_MOUSE
7002 setmouse();
7003# endif
7004
7005# ifdef FEAT_AUTOCMD
7006 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007007 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007008 if (restart_edit != 0) /* autocmd with ":startinsert" */
7009 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010# endif
7011
7012 i = RedrawingDisabled;
7013 RedrawingDisabled = 0;
7014
7015 /*
7016 * Call the main loop until <CR> or CTRL-C is typed.
7017 */
7018 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007019 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
7021 RedrawingDisabled = i;
7022
7023# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007024
7025# ifdef FEAT_FOLDING
7026 save_KeyTyped = KeyTyped;
7027# endif
7028
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007030 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007031
7032# ifdef FEAT_FOLDING
7033 /* Restore KeyTyped in case it is modified by autocommands */
7034 KeyTyped = save_KeyTyped;
7035# endif
7036
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037# endif
7038
Bram Moolenaarccc18222007-05-10 18:25:20 +00007039 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007040 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 cmdwin_type = 0;
7042
7043 exmode_active = save_exmode;
7044
Bram Moolenaarf9821062008-06-20 16:31:07 +00007045 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007047 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 {
7049 cmdwin_result = Ctrl_C;
7050 EMSG(_("E199: Active window or buffer deleted"));
7051 }
7052 else
7053 {
7054# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7055 /* autocmds may abort script processing */
7056 if (aborting() && cmdwin_result != K_IGNORE)
7057 cmdwin_result = Ctrl_C;
7058# endif
7059 /* Set the new command line from the cmdline buffer. */
7060 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007061 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007063 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7064
7065 if (histtype == HIST_CMD)
7066 {
7067 /* Execute the command directly. */
7068 ccline.cmdbuff = vim_strsave((char_u *)p);
7069 cmdwin_result = CAR;
7070 }
7071 else
7072 {
7073 /* First need to cancel what we were doing. */
7074 ccline.cmdbuff = NULL;
7075 stuffcharReadbuff(':');
7076 stuffReadbuff((char_u *)p);
7077 stuffcharReadbuff(CAR);
7078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 }
7080 else if (cmdwin_result == K_XF2) /* :qa typed */
7081 {
7082 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7083 cmdwin_result = CAR;
7084 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007085 else if (cmdwin_result == Ctrl_C)
7086 {
7087 /* :q or :close, don't execute any command
7088 * and don't modify the cmd window. */
7089 ccline.cmdbuff = NULL;
7090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 else
7092 ccline.cmdbuff = vim_strsave(ml_get_curline());
7093 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007094 {
7095 ccline.cmdbuff = vim_strsave((char_u *)"");
7096 ccline.cmdlen = 0;
7097 ccline.cmdbufflen = 1;
7098 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 else
7102 {
7103 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7104 ccline.cmdbufflen = ccline.cmdlen + 1;
7105 ccline.cmdpos = curwin->w_cursor.col;
7106 if (ccline.cmdpos > ccline.cmdlen)
7107 ccline.cmdpos = ccline.cmdlen;
7108 if (cmdwin_result == K_IGNORE)
7109 {
7110 set_cmdspos_cursor();
7111 redrawcmd();
7112 }
7113 }
7114
7115# ifdef FEAT_AUTOCMD
7116 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007117 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007119# ifdef FEAT_CONCEAL
7120 /* Avoid command-line window first character being concealed. */
7121 curwin->w_p_cole = 0;
7122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007124 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 win_goto(old_curwin);
7126 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007127
7128 /* win_close() may have already wiped the buffer when 'bh' is
7129 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007130 if (bufref_valid(&bufref))
7131 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
7133 /* Restore window sizes. */
7134 win_size_restore(&winsizes);
7135
7136# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007137 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138# endif
7139 }
7140
7141 ga_clear(&winsizes);
7142 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007143# ifdef FEAT_RIGHTLEFT
7144 cmdmsg_rl = save_cmdmsg_rl;
7145# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
7147 State = save_State;
7148# ifdef FEAT_MOUSE
7149 setmouse();
7150# endif
7151
7152 return cmdwin_result;
7153}
7154#endif /* FEAT_CMDWIN */
7155
7156/*
7157 * Used for commands that either take a simple command string argument, or:
7158 * cmd << endmarker
7159 * {script}
7160 * endmarker
7161 * Returns a pointer to allocated memory with {script} or NULL.
7162 */
7163 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007164script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166 char_u *theline;
7167 char *end_pattern = NULL;
7168 char dot[] = ".";
7169 garray_T ga;
7170
7171 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7172 return NULL;
7173
7174 ga_init2(&ga, 1, 0x400);
7175
7176 if (cmd[2] != NUL)
7177 end_pattern = (char *)skipwhite(cmd + 2);
7178 else
7179 end_pattern = dot;
7180
7181 for (;;)
7182 {
7183 theline = eap->getline(
7184#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007185 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186#endif
7187 NUL, eap->cookie, 0);
7188
7189 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007190 {
7191 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
7195 ga_concat(&ga, theline);
7196 ga_append(&ga, '\n');
7197 vim_free(theline);
7198 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007199 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
7201 return (char_u *)ga.ga_data;
7202}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007203
7204#ifdef FEAT_SEARCH_EXTRA
7205 static void
7206set_search_match(pos_T *t)
7207{
7208 /*
7209 * First move cursor to end of match, then to the start. This
7210 * moves the whole match onto the screen when 'nowrap' is set.
7211 */
7212 t->lnum += search_match_lines;
7213 t->col = search_match_endcol;
7214 if (t->lnum > curbuf->b_ml.ml_line_count)
7215 {
7216 t->lnum = curbuf->b_ml.ml_line_count;
7217 coladvance((colnr_T)MAXCOL);
7218 }
7219}
7220#endif