blob: 6ca707e43265f1c896709f8108314901ed375352 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020028 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
55#ifdef FEAT_CMDHIST
56typedef struct hist_entry
57{
58 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020059 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 char_u *hisstr; /* actual entry, separator char after the NUL */
61} histentry_T;
62
63static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
64static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
65static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
66 /* identifying (unique) number of newest history entry */
67static int hislen = 0; /* actual length of history tables */
68
69static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaar07219f92013-04-14 23:19:36 +020071static int in_history __ARGS((int, char_u *, int, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072# ifdef FEAT_EVAL
73static int calc_hist_idx __ARGS((int histype, int num));
74# endif
75#endif
76
77#ifdef FEAT_RIGHTLEFT
78static int cmd_hkmap = 0; /* Hebrew mapping during command line */
79#endif
80
81#ifdef FEAT_FKMAP
82static int cmd_fkmap = 0; /* Farsi mapping during command line */
83#endif
84
85static int cmdline_charsize __ARGS((int idx));
86static void set_cmdspos __ARGS((void));
87static void set_cmdspos_cursor __ARGS((void));
88#ifdef FEAT_MBYTE
89static void correct_cmdspos __ARGS((int idx, int cells));
90#endif
91static void alloc_cmdbuff __ARGS((int len));
92static int realloc_cmdbuff __ARGS((int len));
93static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000094static void save_cmdline __ARGS((struct cmdline_info *ccp));
95static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar1769d5a2006-10-17 14:25:24 +000096static int cmdline_paste __ARGS((int regname, int literally, int remcr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
98static void redrawcmd_preedit __ARGS((void));
99#endif
100#ifdef FEAT_WILDMENU
101static void cmdline_del __ARGS((int from));
102#endif
103static void redrawcmdprompt __ARGS((void));
104static void cursorcmd __ARGS((void));
105static int ccheck_abbr __ARGS((int));
Bram Moolenaarb3479632012-11-28 16:49:58 +0100106static int nextwild __ARGS((expand_T *xp, int type, int options, int escape));
Bram Moolenaar45360022005-07-21 21:08:21 +0000107static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108static int showmatches __ARGS((expand_T *xp, int wildmenu));
109static void set_expand_context __ARGS((expand_T *xp));
110static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
111static int expand_showtail __ARGS((expand_T *xp));
112#ifdef FEAT_CMDL_COMPL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000113static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar0c7437a2011-06-26 19:40:23 +0200114static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname[]));
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200115# ifdef FEAT_CMDHIST
116static char_u *get_history_arg __ARGS((expand_T *xp, int idx));
117# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
119static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000120static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121# endif
122#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200123#ifdef FEAT_CMDHIST
124static void clear_hist_entry __ARGS((histentry_T *hisptr));
125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127#ifdef FEAT_CMDWIN
128static int ex_window __ARGS((void));
129#endif
130
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200131#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
132static int
133#ifdef __BORLANDC__
134_RTLENTRYF
135#endif
136sort_func_compare __ARGS((const void *s1, const void *s2));
137#endif
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139/*
140 * getcmdline() - accept a command line starting with firstc.
141 *
142 * firstc == ':' get ":" command line.
143 * firstc == '/' or '?' get search pattern
144 * firstc == '=' get expression
145 * firstc == '@' get text for input() function
146 * firstc == '>' get text for debug mode
147 * firstc == NUL get text for :insert command
148 * firstc == -1 like NUL, and break on CTRL-C
149 *
150 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
151 * command line.
152 *
153 * Careful: getcmdline() can be called recursively!
154 *
155 * Return pointer to allocated string if there is a commandline, NULL
156 * otherwise.
157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 char_u *
159getcmdline(firstc, count, indent)
160 int firstc;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000161 long count UNUSED; /* only used for incremental search */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 int indent; /* indent for inside conditionals */
163{
164 int c;
165 int i;
166 int j;
167 int gotesc = FALSE; /* TRUE when <ESC> just typed */
168 int do_abbr; /* when TRUE check for abbr. */
169#ifdef FEAT_CMDHIST
170 char_u *lookfor = NULL; /* string to match */
171 int hiscnt; /* current history line in use */
172 int histype; /* history type to be used */
173#endif
174#ifdef FEAT_SEARCH_EXTRA
175 pos_T old_cursor;
176 colnr_T old_curswant;
177 colnr_T old_leftcol;
178 linenr_T old_topline;
179# ifdef FEAT_DIFF
180 int old_topfill;
181# endif
182 linenr_T old_botline;
183 int did_incsearch = FALSE;
184 int incsearch_postponed = FALSE;
185#endif
186 int did_wild_list = FALSE; /* did wild_list() recently */
187 int wim_index = 0; /* index in wim_flags[] */
188 int res;
189 int save_msg_scroll = msg_scroll;
190 int save_State = State; /* remember State when called */
191 int some_key_typed = FALSE; /* one of the keys was typed */
192#ifdef FEAT_MOUSE
193 /* mouse drag and release events are ignored, unless they are
194 * preceded with a mouse down event */
195 int ignore_drag_release = TRUE;
196#endif
197#ifdef FEAT_EVAL
198 int break_ctrl_c = FALSE;
199#endif
200 expand_T xpc;
201 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000202#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
203 /* Everything that may work recursively should save and restore the
204 * current command line in save_ccline. That includes update_screen(), a
205 * custom status line may invoke ":normal". */
206 struct cmdline_info save_ccline;
207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
209#ifdef FEAT_SNIFF
210 want_sniff_request = 0;
211#endif
212#ifdef FEAT_EVAL
213 if (firstc == -1)
214 {
215 firstc = NUL;
216 break_ctrl_c = TRUE;
217 }
218#endif
219#ifdef FEAT_RIGHTLEFT
220 /* start without Hebrew mapping for a command line */
221 if (firstc == ':' || firstc == '=' || firstc == '>')
222 cmd_hkmap = 0;
223#endif
224
225 ccline.overstrike = FALSE; /* always start in insert mode */
226#ifdef FEAT_SEARCH_EXTRA
227 old_cursor = curwin->w_cursor; /* needs to be restored later */
228 old_curswant = curwin->w_curswant;
229 old_leftcol = curwin->w_leftcol;
230 old_topline = curwin->w_topline;
231# ifdef FEAT_DIFF
232 old_topfill = curwin->w_topfill;
233# endif
234 old_botline = curwin->w_botline;
235#endif
236
237 /*
238 * set some variables for redrawcmd()
239 */
240 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000241 ccline.cmdindent = (firstc > 0 ? indent : 0);
242
243 /* alloc initial ccline.cmdbuff */
244 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 if (ccline.cmdbuff == NULL)
246 return NULL; /* out of memory */
247 ccline.cmdlen = ccline.cmdpos = 0;
248 ccline.cmdbuff[0] = NUL;
249
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000250 /* autoindent for :insert and :append */
251 if (firstc <= 0)
252 {
253 copy_spaces(ccline.cmdbuff, indent);
254 ccline.cmdbuff[indent] = NUL;
255 ccline.cmdpos = indent;
256 ccline.cmdspos = indent;
257 ccline.cmdlen = indent;
258 }
259
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000261 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263#ifdef FEAT_RIGHTLEFT
264 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
265 && (firstc == '/' || firstc == '?'))
266 cmdmsg_rl = TRUE;
267 else
268 cmdmsg_rl = FALSE;
269#endif
270
271 redir_off = TRUE; /* don't redirect the typed command */
272 if (!cmd_silent)
273 {
274 i = msg_scrolled;
275 msg_scrolled = 0; /* avoid wait_return message */
276 gotocmdline(TRUE);
277 msg_scrolled += i;
278 redrawcmdprompt(); /* draw prompt or indent */
279 set_cmdspos();
280 }
281 xpc.xp_context = EXPAND_NOTHING;
282 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000283#ifndef BACKSLASH_IN_FILENAME
284 xpc.xp_shell = FALSE;
285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000287#if defined(FEAT_EVAL)
288 if (ccline.input_fn)
289 {
290 xpc.xp_context = ccline.xp_context;
291 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000292# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000293 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000294# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000295 }
296#endif
297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 /*
299 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
300 * doing ":@0" when register 0 doesn't contain a CR.
301 */
302 msg_scroll = FALSE;
303
304 State = CMDLINE;
305
306 if (firstc == '/' || firstc == '?' || firstc == '@')
307 {
308 /* Use ":lmap" mappings for search pattern and input(). */
309 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
310 b_im_ptr = &curbuf->b_p_iminsert;
311 else
312 b_im_ptr = &curbuf->b_p_imsearch;
313 if (*b_im_ptr == B_IMODE_LMAP)
314 State |= LANGMAP;
315#ifdef USE_IM_CONTROL
316 im_set_active(*b_im_ptr == B_IMODE_IM);
317#endif
318 }
319#ifdef USE_IM_CONTROL
320 else if (p_imcmdline)
321 im_set_active(TRUE);
322#endif
323
324#ifdef FEAT_MOUSE
325 setmouse();
326#endif
327#ifdef CURSOR_SHAPE
328 ui_cursor_shape(); /* may show different cursor shape */
329#endif
330
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000331 /* When inside an autocommand for writing "exiting" may be set and
332 * terminal mode set to cooked. Need to set raw mode here then. */
333 settmode(TMODE_RAW);
334
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#ifdef FEAT_CMDHIST
336 init_history();
337 hiscnt = hislen; /* set hiscnt to impossible history value */
338 histype = hist_char2type(firstc);
339#endif
340
341#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000342 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#endif
344
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200345 /* If something above caused an error, reset the flags, we do want to type
346 * and execute commands. Display may be messed up a bit. */
347 if (did_emsg)
348 redrawcmd();
349 did_emsg = FALSE;
350 got_int = FALSE;
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 /*
353 * Collect the command string, handling editing keys.
354 */
355 for (;;)
356 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000357 redir_off = TRUE; /* Don't redirect the typed command.
358 Repeated, because a ":redir" inside
359 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#ifdef USE_ON_FLY_SCROLL
361 dont_scroll = FALSE; /* allow scrolling here */
362#endif
363 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
364
365 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000366
367 /* Get a character. Ignore K_IGNORE, it should not do anything, such
368 * as stop completion. */
369 do
370 {
371 c = safe_vgetc();
372 } while (c == K_IGNORE);
373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (KeyTyped)
375 {
376 some_key_typed = TRUE;
377#ifdef FEAT_RIGHTLEFT
378 if (cmd_hkmap)
379 c = hkmap(c);
380# ifdef FEAT_FKMAP
381 if (cmd_fkmap)
382 c = cmdl_fkmap(c);
383# endif
384 if (cmdmsg_rl && !KeyStuffed)
385 {
386 /* Invert horizontal movements and operations. Only when
387 * typed by the user directly, not when the result of a
388 * mapping. */
389 switch (c)
390 {
391 case K_RIGHT: c = K_LEFT; break;
392 case K_S_RIGHT: c = K_S_LEFT; break;
393 case K_C_RIGHT: c = K_C_LEFT; break;
394 case K_LEFT: c = K_RIGHT; break;
395 case K_S_LEFT: c = K_S_RIGHT; break;
396 case K_C_LEFT: c = K_C_RIGHT; break;
397 }
398 }
399#endif
400 }
401
402 /*
403 * Ignore got_int when CTRL-C was typed here.
404 * Don't ignore it in :global, we really need to break then, e.g., for
405 * ":g/pat/normal /pat" (without the <CR>).
406 * Don't ignore it for the input() function.
407 */
408 if ((c == Ctrl_C
409#ifdef UNIX
410 || c == intr_char
411#endif
412 )
413#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
414 && firstc != '@'
415#endif
416#ifdef FEAT_EVAL
417 && !break_ctrl_c
418#endif
419 && !global_busy)
420 got_int = FALSE;
421
422#ifdef FEAT_CMDHIST
423 /* free old command line when finished moving around in the history
424 * list */
425 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000426 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000427 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 && c != K_PAGEDOWN && c != K_PAGEUP
429 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000430 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
432 {
433 vim_free(lookfor);
434 lookfor = NULL;
435 }
436#endif
437
438 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000439 * When there are matching completions to select <S-Tab> works like
440 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000442 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 c = Ctrl_P;
444
445#ifdef FEAT_WILDMENU
446 /* Special translations for 'wildmenu' */
447 if (did_wild_list && p_wmnu)
448 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000449 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000451 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 c = Ctrl_N;
453 }
454 /* Hitting CR after "emenu Name.": complete submenu */
455 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
456 && ccline.cmdpos > 1
457 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
458 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
459 && (c == '\n' || c == '\r' || c == K_KENTER))
460 c = K_DOWN;
461#endif
462
463 /* free expanded names when finished walking through matches */
464 if (xpc.xp_numfiles != -1
465 && !(c == p_wc && KeyTyped) && c != p_wcm
466 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
467 && c != Ctrl_L)
468 {
469 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
470 did_wild_list = FALSE;
471#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000472 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#endif
474 xpc.xp_context = EXPAND_NOTHING;
475 wim_index = 0;
476#ifdef FEAT_WILDMENU
477 if (p_wmnu && wild_menu_showing != 0)
478 {
479 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000480 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000481
482 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000483 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484
485 if (wild_menu_showing == WM_SCROLLED)
486 {
487 /* Entered command line, move it up */
488 cmdline_row--;
489 redrawcmd();
490 }
491 else if (save_p_ls != -1)
492 {
493 /* restore 'laststatus' and 'winminheight' */
494 p_ls = save_p_ls;
495 p_wmh = save_p_wmh;
496 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000497 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000499 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 redrawcmd();
501 save_p_ls = -1;
502 }
503 else
504 {
505# ifdef FEAT_VERTSPLIT
506 win_redraw_last_status(topframe);
507# else
508 lastwin->w_redr_status = TRUE;
509# endif
510 redraw_statuslines();
511 }
512 KeyTyped = skt;
513 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000514 if (ccline.input_fn)
515 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 }
517#endif
518 }
519
520#ifdef FEAT_WILDMENU
521 /* Special translations for 'wildmenu' */
522 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
523 {
524 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000525 if (c == K_DOWN && ccline.cmdpos > 0
526 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000528 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
530 /* Hitting <Up>: Remove one submenu name in front of the
531 * cursor */
532 int found = FALSE;
533
534 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
535 i = 0;
536 while (--j > 0)
537 {
538 /* check for start of menu name */
539 if (ccline.cmdbuff[j] == ' '
540 && ccline.cmdbuff[j - 1] != '\\')
541 {
542 i = j + 1;
543 break;
544 }
545 /* check for start of submenu name */
546 if (ccline.cmdbuff[j] == '.'
547 && ccline.cmdbuff[j - 1] != '\\')
548 {
549 if (found)
550 {
551 i = j + 1;
552 break;
553 }
554 else
555 found = TRUE;
556 }
557 }
558 if (i > 0)
559 cmdline_del(i);
560 c = p_wc;
561 xpc.xp_context = EXPAND_NOTHING;
562 }
563 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000564 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000565 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000566 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {
568 char_u upseg[5];
569
570 upseg[0] = PATHSEP;
571 upseg[1] = '.';
572 upseg[2] = '.';
573 upseg[3] = PATHSEP;
574 upseg[4] = NUL;
575
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000576 if (c == K_DOWN
577 && ccline.cmdpos > 0
578 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
579 && (ccline.cmdpos < 3
580 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
582 {
583 /* go down a directory */
584 c = p_wc;
585 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000586 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 {
588 /* If in a direct ancestor, strip off one ../ to go down */
589 int found = FALSE;
590
591 j = ccline.cmdpos;
592 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
593 while (--j > i)
594 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000595#ifdef FEAT_MBYTE
596 if (has_mbyte)
597 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (vim_ispathsep(ccline.cmdbuff[j]))
600 {
601 found = TRUE;
602 break;
603 }
604 }
605 if (found
606 && ccline.cmdbuff[j - 1] == '.'
607 && ccline.cmdbuff[j - 2] == '.'
608 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
609 {
610 cmdline_del(j - 2);
611 c = p_wc;
612 }
613 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000614 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 {
616 /* go up a directory */
617 int found = FALSE;
618
619 j = ccline.cmdpos - 1;
620 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
621 while (--j > i)
622 {
623#ifdef FEAT_MBYTE
624 if (has_mbyte)
625 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
626#endif
627 if (vim_ispathsep(ccline.cmdbuff[j])
628#ifdef BACKSLASH_IN_FILENAME
629 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
630 == NULL
631#endif
632 )
633 {
634 if (found)
635 {
636 i = j + 1;
637 break;
638 }
639 else
640 found = TRUE;
641 }
642 }
643
644 if (!found)
645 j = i;
646 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
647 j += 4;
648 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
649 && j == i)
650 j += 3;
651 else
652 j = 0;
653 if (j > 0)
654 {
655 /* TODO this is only for DOS/UNIX systems - need to put in
656 * machine-specific stuff here and in upseg init */
657 cmdline_del(j);
658 put_on_cmdline(upseg + 1, 3, FALSE);
659 }
660 else if (ccline.cmdpos > i)
661 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100662
663 /* Now complete in the new directory. Set KeyTyped in case the
664 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100666 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 }
668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
670#endif /* FEAT_WILDMENU */
671
672 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
673 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
674 if (c == Ctrl_BSL)
675 {
676 ++no_mapping;
677 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000678 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 --no_mapping;
680 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200681 /* CTRL-\ e doesn't work when obtaining an expression, unless it
682 * is in a mapping. */
683 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
684 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
686 vungetc(c);
687 c = Ctrl_BSL;
688 }
689#ifdef FEAT_EVAL
690 else if (c == 'e')
691 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200692 char_u *p = NULL;
693 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
695 /*
696 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000697 * Need to save and restore the current command line, to be
698 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 */
700 if (ccline.cmdpos == ccline.cmdlen)
701 new_cmdpos = 99999; /* keep it at the end */
702 else
703 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000704
705 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000707 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 if (c == '=')
709 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000710 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000711 * to avoid nasty things like going to another buffer when
712 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000713 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000714 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000716 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000717 restore_cmdline(&save_ccline);
718
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200719 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200721 len = (int)STRLEN(p);
722 if (realloc_cmdbuff(len + 1) == OK)
723 {
724 ccline.cmdlen = len;
725 STRCPY(ccline.cmdbuff, p);
726 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200728 /* Restore the cursor or use the position set with
729 * set_cmdline_pos(). */
730 if (new_cmdpos > ccline.cmdlen)
731 ccline.cmdpos = ccline.cmdlen;
732 else
733 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200735 KeyTyped = FALSE; /* Don't do p_wc completion. */
736 redrawcmd();
737 goto cmdline_changed;
738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740 }
741 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100742 got_int = FALSE; /* don't abandon the command line */
743 did_emsg = FALSE;
744 emsg_on_display = FALSE;
745 redrawcmd();
746 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 }
748#endif
749 else
750 {
751 if (c == Ctrl_G && p_im && restart_edit == 0)
752 restart_edit = 'a';
753 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
754 in history */
755 goto returncmd; /* back to Normal mode */
756 }
757 }
758
759#ifdef FEAT_CMDWIN
760 if (c == cedit_key || c == K_CMDWIN)
761 {
762 /*
763 * Open a window to edit the command line (and history).
764 */
765 c = ex_window();
766 some_key_typed = TRUE;
767 }
768# ifdef FEAT_DIGRAPHS
769 else
770# endif
771#endif
772#ifdef FEAT_DIGRAPHS
773 c = do_digraph(c);
774#endif
775
776 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
777 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
778 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000779 /* In Ex mode a backslash escapes a newline. */
780 if (exmode_active
781 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000782 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000783 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000784 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000786 if (c == K_KENTER)
787 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000789 else
790 {
791 gotesc = FALSE; /* Might have typed ESC previously, don't
792 truncate the cmdline now. */
793 if (ccheck_abbr(c + ABBR_OFF))
794 goto cmdline_changed;
795 if (!cmd_silent)
796 {
797 windgoto(msg_row, 0);
798 out_flush();
799 }
800 break;
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
803
804 /*
805 * Completion for 'wildchar' or 'wildcharm' key.
806 * - hitting <ESC> twice means: abandon command line.
807 * - wildcard expansion is only done when the 'wildchar' key is really
808 * typed, not when it comes from a macro
809 */
810 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
811 {
812 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
813 {
814 /* if 'wildmode' contains "list" may still need to list */
815 if (xpc.xp_numfiles > 1
816 && !did_wild_list
817 && (wim_flags[wim_index] & WIM_LIST))
818 {
819 (void)showmatches(&xpc, FALSE);
820 redrawcmd();
821 did_wild_list = TRUE;
822 }
823 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100824 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
825 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100827 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
828 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 else
830 res = OK; /* don't insert 'wildchar' now */
831 }
832 else /* typed p_wc first time */
833 {
834 wim_index = 0;
835 j = ccline.cmdpos;
836 /* if 'wildmode' first contains "longest", get longest
837 * common part */
838 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100839 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
840 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100842 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
843 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845 /* if interrupted while completing, behave like it failed */
846 if (got_int)
847 {
848 (void)vpeekc(); /* remove <C-C> from input stream */
849 got_int = FALSE; /* don't abandon the command line */
850 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
851#ifdef FEAT_WILDMENU
852 xpc.xp_context = EXPAND_NOTHING;
853#endif
854 goto cmdline_changed;
855 }
856
857 /* when more than one match, and 'wildmode' first contains
858 * "list", or no change and 'wildmode' contains "longest,list",
859 * list all matches */
860 if (res == OK && xpc.xp_numfiles > 1)
861 {
862 /* a "longest" that didn't do anything is skipped (but not
863 * "list:longest") */
864 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
865 wim_index = 1;
866 if ((wim_flags[wim_index] & WIM_LIST)
867#ifdef FEAT_WILDMENU
868 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
869#endif
870 )
871 {
872 if (!(wim_flags[0] & WIM_LONGEST))
873 {
874#ifdef FEAT_WILDMENU
875 int p_wmnu_save = p_wmnu;
876 p_wmnu = 0;
877#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100878 /* remove match */
879 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880#ifdef FEAT_WILDMENU
881 p_wmnu = p_wmnu_save;
882#endif
883 }
884#ifdef FEAT_WILDMENU
885 (void)showmatches(&xpc, p_wmnu
886 && ((wim_flags[wim_index] & WIM_LIST) == 0));
887#else
888 (void)showmatches(&xpc, FALSE);
889#endif
890 redrawcmd();
891 did_wild_list = TRUE;
892 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100893 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
894 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100896 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
897 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899 else
900 vim_beep();
901 }
902#ifdef FEAT_WILDMENU
903 else if (xpc.xp_numfiles == -1)
904 xpc.xp_context = EXPAND_NOTHING;
905#endif
906 }
907 if (wim_index < 3)
908 ++wim_index;
909 if (c == ESC)
910 gotesc = TRUE;
911 if (res == OK)
912 goto cmdline_changed;
913 }
914
915 gotesc = FALSE;
916
917 /* <S-Tab> goes to last match, in a clumsy way */
918 if (c == K_S_TAB && KeyTyped)
919 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100920 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
921 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
922 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 goto cmdline_changed;
924 }
925
926 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
927 c = NL;
928
929 do_abbr = TRUE; /* default: check for abbreviation */
930
931 /*
932 * Big switch for a typed command line character.
933 */
934 switch (c)
935 {
936 case K_BS:
937 case Ctrl_H:
938 case K_DEL:
939 case K_KDEL:
940 case Ctrl_W:
941#ifdef FEAT_FKMAP
942 if (cmd_fkmap && c == K_BS)
943 c = K_DEL;
944#endif
945 if (c == K_KDEL)
946 c = K_DEL;
947
948 /*
949 * delete current character is the same as backspace on next
950 * character, except at end of line
951 */
952 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
953 ++ccline.cmdpos;
954#ifdef FEAT_MBYTE
955 if (has_mbyte && c == K_DEL)
956 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
957 ccline.cmdbuff + ccline.cmdpos);
958#endif
959 if (ccline.cmdpos > 0)
960 {
961 char_u *p;
962
963 j = ccline.cmdpos;
964 p = ccline.cmdbuff + j;
965#ifdef FEAT_MBYTE
966 if (has_mbyte)
967 {
968 p = mb_prevptr(ccline.cmdbuff, p);
969 if (c == Ctrl_W)
970 {
971 while (p > ccline.cmdbuff && vim_isspace(*p))
972 p = mb_prevptr(ccline.cmdbuff, p);
973 i = mb_get_class(p);
974 while (p > ccline.cmdbuff && mb_get_class(p) == i)
975 p = mb_prevptr(ccline.cmdbuff, p);
976 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000977 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 }
979 }
980 else
981#endif
982 if (c == Ctrl_W)
983 {
984 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
985 --p;
986 i = vim_iswordc(p[-1]);
987 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
988 && vim_iswordc(p[-1]) == i)
989 --p;
990 }
991 else
992 --p;
993 ccline.cmdpos = (int)(p - ccline.cmdbuff);
994 ccline.cmdlen -= j - ccline.cmdpos;
995 i = ccline.cmdpos;
996 while (i < ccline.cmdlen)
997 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
998
999 /* Truncate at the end, required for multi-byte chars. */
1000 ccline.cmdbuff[ccline.cmdlen] = NUL;
1001 redrawcmd();
1002 }
1003 else if (ccline.cmdlen == 0 && c != Ctrl_W
1004 && ccline.cmdprompt == NULL && indent == 0)
1005 {
1006 /* In ex and debug mode it doesn't make sense to return. */
1007 if (exmode_active
1008#ifdef FEAT_EVAL
1009 || ccline.cmdfirstc == '>'
1010#endif
1011 )
1012 goto cmdline_not_changed;
1013
1014 vim_free(ccline.cmdbuff); /* no commandline to return */
1015 ccline.cmdbuff = NULL;
1016 if (!cmd_silent)
1017 {
1018#ifdef FEAT_RIGHTLEFT
1019 if (cmdmsg_rl)
1020 msg_col = Columns;
1021 else
1022#endif
1023 msg_col = 0;
1024 msg_putchar(' '); /* delete ':' */
1025 }
1026 redraw_cmdline = TRUE;
1027 goto returncmd; /* back to cmd mode */
1028 }
1029 goto cmdline_changed;
1030
1031 case K_INS:
1032 case K_KINS:
1033#ifdef FEAT_FKMAP
1034 /* if Farsi mode set, we are in reverse insert mode -
1035 Do not change the mode */
1036 if (cmd_fkmap)
1037 beep_flush();
1038 else
1039#endif
1040 ccline.overstrike = !ccline.overstrike;
1041#ifdef CURSOR_SHAPE
1042 ui_cursor_shape(); /* may show different cursor shape */
1043#endif
1044 goto cmdline_not_changed;
1045
1046 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001047 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 {
1049 /* ":lmap" mappings exists, toggle use of mappings. */
1050 State ^= LANGMAP;
1051#ifdef USE_IM_CONTROL
1052 im_set_active(FALSE); /* Disable input method */
1053#endif
1054 if (b_im_ptr != NULL)
1055 {
1056 if (State & LANGMAP)
1057 *b_im_ptr = B_IMODE_LMAP;
1058 else
1059 *b_im_ptr = B_IMODE_NONE;
1060 }
1061 }
1062#ifdef USE_IM_CONTROL
1063 else
1064 {
1065 /* There are no ":lmap" mappings, toggle IM. When
1066 * 'imdisable' is set don't try getting the status, it's
1067 * always off. */
1068 if ((p_imdisable && b_im_ptr != NULL)
1069 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1070 {
1071 im_set_active(FALSE); /* Disable input method */
1072 if (b_im_ptr != NULL)
1073 *b_im_ptr = B_IMODE_NONE;
1074 }
1075 else
1076 {
1077 im_set_active(TRUE); /* Enable input method */
1078 if (b_im_ptr != NULL)
1079 *b_im_ptr = B_IMODE_IM;
1080 }
1081 }
1082#endif
1083 if (b_im_ptr != NULL)
1084 {
1085 if (b_im_ptr == &curbuf->b_p_iminsert)
1086 set_iminsert_global();
1087 else
1088 set_imsearch_global();
1089 }
1090#ifdef CURSOR_SHAPE
1091 ui_cursor_shape(); /* may show different cursor shape */
1092#endif
1093#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1094 /* Show/unshow value of 'keymap' in status lines later. */
1095 status_redraw_curbuf();
1096#endif
1097 goto cmdline_not_changed;
1098
1099/* case '@': only in very old vi */
1100 case Ctrl_U:
1101 /* delete all characters left of the cursor */
1102 j = ccline.cmdpos;
1103 ccline.cmdlen -= j;
1104 i = ccline.cmdpos = 0;
1105 while (i < ccline.cmdlen)
1106 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1107 /* Truncate at the end, required for multi-byte chars. */
1108 ccline.cmdbuff[ccline.cmdlen] = NUL;
1109 redrawcmd();
1110 goto cmdline_changed;
1111
1112#ifdef FEAT_CLIPBOARD
1113 case Ctrl_Y:
1114 /* Copy the modeless selection, if there is one. */
1115 if (clip_star.state != SELECT_CLEARED)
1116 {
1117 if (clip_star.state == SELECT_DONE)
1118 clip_copy_modeless_selection(TRUE);
1119 goto cmdline_not_changed;
1120 }
1121 break;
1122#endif
1123
1124 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1125 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001126 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001127 * ":normal" runs out of characters. */
1128 if (exmode_active
1129#ifdef FEAT_EX_EXTRA
1130 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1131#endif
1132 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 goto cmdline_not_changed;
1134
1135 gotesc = TRUE; /* will free ccline.cmdbuff after
1136 putting it in history */
1137 goto returncmd; /* back to cmd mode */
1138
1139 case Ctrl_R: /* insert register */
1140#ifdef USE_ON_FLY_SCROLL
1141 dont_scroll = TRUE; /* disallow scrolling here */
1142#endif
1143 putcmdline('"', TRUE);
1144 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001145 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (i == Ctrl_O)
1147 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1148 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001149 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 --no_mapping;
1151#ifdef FEAT_EVAL
1152 /*
1153 * Insert the result of an expression.
1154 * Need to save the current command line, to be able to enter
1155 * a new one...
1156 */
1157 new_cmdpos = -1;
1158 if (c == '=')
1159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1161 {
1162 beep_flush();
1163 c = ESC;
1164 }
1165 else
1166 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001167 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001169 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171 }
1172#endif
1173 if (c != ESC) /* use ESC to cancel inserting register */
1174 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001175 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001176
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001177#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001178 /* When there was a serious error abort getting the
1179 * command line. */
1180 if (aborting())
1181 {
1182 gotesc = TRUE; /* will free ccline.cmdbuff after
1183 putting it in history */
1184 goto returncmd; /* back to cmd mode */
1185 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 KeyTyped = FALSE; /* Don't do p_wc completion. */
1188#ifdef FEAT_EVAL
1189 if (new_cmdpos >= 0)
1190 {
1191 /* set_cmdline_pos() was used */
1192 if (new_cmdpos > ccline.cmdlen)
1193 ccline.cmdpos = ccline.cmdlen;
1194 else
1195 ccline.cmdpos = new_cmdpos;
1196 }
1197#endif
1198 }
1199 redrawcmd();
1200 goto cmdline_changed;
1201
1202 case Ctrl_D:
1203 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1204 break; /* Use ^D as normal char instead */
1205
1206 redrawcmd();
1207 continue; /* don't do incremental search now */
1208
1209 case K_RIGHT:
1210 case K_S_RIGHT:
1211 case K_C_RIGHT:
1212 do
1213 {
1214 if (ccline.cmdpos >= ccline.cmdlen)
1215 break;
1216 i = cmdline_charsize(ccline.cmdpos);
1217 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1218 break;
1219 ccline.cmdspos += i;
1220#ifdef FEAT_MBYTE
1221 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001222 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 + ccline.cmdpos);
1224 else
1225#endif
1226 ++ccline.cmdpos;
1227 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001228 while ((c == K_S_RIGHT || c == K_C_RIGHT
1229 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1231#ifdef FEAT_MBYTE
1232 if (has_mbyte)
1233 set_cmdspos_cursor();
1234#endif
1235 goto cmdline_not_changed;
1236
1237 case K_LEFT:
1238 case K_S_LEFT:
1239 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001240 if (ccline.cmdpos == 0)
1241 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 do
1243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 --ccline.cmdpos;
1245#ifdef FEAT_MBYTE
1246 if (has_mbyte) /* move to first byte of char */
1247 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1248 ccline.cmdbuff + ccline.cmdpos);
1249#endif
1250 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1251 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001252 while (ccline.cmdpos > 0
1253 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001254 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1256#ifdef FEAT_MBYTE
1257 if (has_mbyte)
1258 set_cmdspos_cursor();
1259#endif
1260 goto cmdline_not_changed;
1261
1262 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001263 /* Ignore mouse event or ex_window() result. */
1264 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaar4770d092006-01-12 23:22:24 +00001266#ifdef FEAT_GUI_W32
1267 /* On Win32 ignore <M-F4>, we get it when closing the window was
1268 * cancelled. */
1269 case K_F4:
1270 if (mod_mask == MOD_MASK_ALT)
1271 {
1272 redrawcmd(); /* somehow the cmdline is cleared */
1273 goto cmdline_not_changed;
1274 }
1275 break;
1276#endif
1277
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278#ifdef FEAT_MOUSE
1279 case K_MIDDLEDRAG:
1280 case K_MIDDLERELEASE:
1281 goto cmdline_not_changed; /* Ignore mouse */
1282
1283 case K_MIDDLEMOUSE:
1284# ifdef FEAT_GUI
1285 /* When GUI is active, also paste when 'mouse' is empty */
1286 if (!gui.in_use)
1287# endif
1288 if (!mouse_has(MOUSE_COMMAND))
1289 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001290# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001292 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001294# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001295 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 redrawcmd();
1297 goto cmdline_changed;
1298
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001299# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001301 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 redrawcmd();
1303 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306 case K_LEFTDRAG:
1307 case K_LEFTRELEASE:
1308 case K_RIGHTDRAG:
1309 case K_RIGHTRELEASE:
1310 /* Ignore drag and release events when the button-down wasn't
1311 * seen before. */
1312 if (ignore_drag_release)
1313 goto cmdline_not_changed;
1314 /* FALLTHROUGH */
1315 case K_LEFTMOUSE:
1316 case K_RIGHTMOUSE:
1317 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1318 ignore_drag_release = TRUE;
1319 else
1320 ignore_drag_release = FALSE;
1321# ifdef FEAT_GUI
1322 /* When GUI is active, also move when 'mouse' is empty */
1323 if (!gui.in_use)
1324# endif
1325 if (!mouse_has(MOUSE_COMMAND))
1326 goto cmdline_not_changed; /* Ignore mouse */
1327# ifdef FEAT_CLIPBOARD
1328 if (mouse_row < cmdline_row && clip_star.available)
1329 {
1330 int button, is_click, is_drag;
1331
1332 /*
1333 * Handle modeless selection.
1334 */
1335 button = get_mouse_button(KEY2TERMCAP1(c),
1336 &is_click, &is_drag);
1337 if (mouse_model_popup() && button == MOUSE_LEFT
1338 && (mod_mask & MOD_MASK_SHIFT))
1339 {
1340 /* Translate shift-left to right button. */
1341 button = MOUSE_RIGHT;
1342 mod_mask &= ~MOD_MASK_SHIFT;
1343 }
1344 clip_modeless(button, is_click, is_drag);
1345 goto cmdline_not_changed;
1346 }
1347# endif
1348
1349 set_cmdspos();
1350 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1351 ++ccline.cmdpos)
1352 {
1353 i = cmdline_charsize(ccline.cmdpos);
1354 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1355 && mouse_col < ccline.cmdspos % Columns + i)
1356 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001357# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (has_mbyte)
1359 {
1360 /* Count ">" for double-wide char that doesn't fit. */
1361 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001362 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 + ccline.cmdpos) - 1;
1364 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 ccline.cmdspos += i;
1367 }
1368 goto cmdline_not_changed;
1369
1370 /* Mouse scroll wheel: ignored here */
1371 case K_MOUSEDOWN:
1372 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001373 case K_MOUSELEFT:
1374 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 /* Alternate buttons ignored here */
1376 case K_X1MOUSE:
1377 case K_X1DRAG:
1378 case K_X1RELEASE:
1379 case K_X2MOUSE:
1380 case K_X2DRAG:
1381 case K_X2RELEASE:
1382 goto cmdline_not_changed;
1383
1384#endif /* FEAT_MOUSE */
1385
1386#ifdef FEAT_GUI
1387 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1388 case K_LEFTRELEASE_NM:
1389 goto cmdline_not_changed;
1390
1391 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001392 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 {
1394 gui_do_scroll();
1395 redrawcmd();
1396 }
1397 goto cmdline_not_changed;
1398
1399 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001400 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001402 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 redrawcmd();
1404 }
1405 goto cmdline_not_changed;
1406#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001407#ifdef FEAT_GUI_TABLINE
1408 case K_TABLINE:
1409 case K_TABMENU:
1410 /* Don't want to change any tabs here. Make sure the same tab
1411 * is still selected. */
1412 if (gui_use_tabline())
1413 gui_mch_set_curtab(tabpage_index(curtab));
1414 goto cmdline_not_changed;
1415#endif
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 case K_SELECT: /* end of Select mode mapping - ignore */
1418 goto cmdline_not_changed;
1419
1420 case Ctrl_B: /* begin of command line */
1421 case K_HOME:
1422 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 case K_S_HOME:
1424 case K_C_HOME:
1425 ccline.cmdpos = 0;
1426 set_cmdspos();
1427 goto cmdline_not_changed;
1428
1429 case Ctrl_E: /* end of command line */
1430 case K_END:
1431 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 case K_S_END:
1433 case K_C_END:
1434 ccline.cmdpos = ccline.cmdlen;
1435 set_cmdspos_cursor();
1436 goto cmdline_not_changed;
1437
1438 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001439 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 break;
1441 goto cmdline_changed;
1442
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001443 case Ctrl_L:
1444#ifdef FEAT_SEARCH_EXTRA
1445 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1446 {
1447 /* Add a character from under the cursor for 'incsearch' */
1448 if (did_incsearch
1449 && !equalpos(curwin->w_cursor, old_cursor))
1450 {
1451 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001452 /* If 'ignorecase' and 'smartcase' are set and the
1453 * command line has no uppercase characters, convert
1454 * the character to lowercase */
1455 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1456 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001457 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001458 {
1459 if (c == firstc || vim_strchr((char_u *)(
1460 p_magic ? "\\^$.*[" : "\\^$"), c)
1461 != NULL)
1462 {
1463 /* put a backslash before special characters */
1464 stuffcharReadbuff(c);
1465 c = '\\';
1466 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001467 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001468 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001469 }
1470 goto cmdline_not_changed;
1471 }
1472#endif
1473
1474 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001475 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 break;
1477 goto cmdline_changed;
1478
1479 case Ctrl_N: /* next match */
1480 case Ctrl_P: /* previous match */
1481 if (xpc.xp_numfiles > 0)
1482 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001483 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1484 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 break;
1486 goto cmdline_changed;
1487 }
1488
1489#ifdef FEAT_CMDHIST
1490 case K_UP:
1491 case K_DOWN:
1492 case K_S_UP:
1493 case K_S_DOWN:
1494 case K_PAGEUP:
1495 case K_KPAGEUP:
1496 case K_PAGEDOWN:
1497 case K_KPAGEDOWN:
1498 if (hislen == 0 || firstc == NUL) /* no history */
1499 goto cmdline_not_changed;
1500
1501 i = hiscnt;
1502
1503 /* save current command string so it can be restored later */
1504 if (lookfor == NULL)
1505 {
1506 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1507 goto cmdline_not_changed;
1508 lookfor[ccline.cmdpos] = NUL;
1509 }
1510
1511 j = (int)STRLEN(lookfor);
1512 for (;;)
1513 {
1514 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001515 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001516 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
1518 if (hiscnt == hislen) /* first time */
1519 hiscnt = hisidx[histype];
1520 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1521 hiscnt = hislen - 1;
1522 else if (hiscnt != hisidx[histype] + 1)
1523 --hiscnt;
1524 else /* at top of list */
1525 {
1526 hiscnt = i;
1527 break;
1528 }
1529 }
1530 else /* one step forwards */
1531 {
1532 /* on last entry, clear the line */
1533 if (hiscnt == hisidx[histype])
1534 {
1535 hiscnt = hislen;
1536 break;
1537 }
1538
1539 /* not on a history line, nothing to do */
1540 if (hiscnt == hislen)
1541 break;
1542 if (hiscnt == hislen - 1) /* wrap around */
1543 hiscnt = 0;
1544 else
1545 ++hiscnt;
1546 }
1547 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1548 {
1549 hiscnt = i;
1550 break;
1551 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001552 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001553 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 || STRNCMP(history[histype][hiscnt].hisstr,
1555 lookfor, (size_t)j) == 0)
1556 break;
1557 }
1558
1559 if (hiscnt != i) /* jumped to other entry */
1560 {
1561 char_u *p;
1562 int len;
1563 int old_firstc;
1564
1565 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001566 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (hiscnt == hislen)
1568 p = lookfor; /* back to the old one */
1569 else
1570 p = history[histype][hiscnt].hisstr;
1571
1572 if (histype == HIST_SEARCH
1573 && p != lookfor
1574 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1575 {
1576 /* Correct for the separator character used when
1577 * adding the history entry vs the one used now.
1578 * First loop: count length.
1579 * Second loop: copy the characters. */
1580 for (i = 0; i <= 1; ++i)
1581 {
1582 len = 0;
1583 for (j = 0; p[j] != NUL; ++j)
1584 {
1585 /* Replace old sep with new sep, unless it is
1586 * escaped. */
1587 if (p[j] == old_firstc
1588 && (j == 0 || p[j - 1] != '\\'))
1589 {
1590 if (i > 0)
1591 ccline.cmdbuff[len] = firstc;
1592 }
1593 else
1594 {
1595 /* Escape new sep, unless it is already
1596 * escaped. */
1597 if (p[j] == firstc
1598 && (j == 0 || p[j - 1] != '\\'))
1599 {
1600 if (i > 0)
1601 ccline.cmdbuff[len] = '\\';
1602 ++len;
1603 }
1604 if (i > 0)
1605 ccline.cmdbuff[len] = p[j];
1606 }
1607 ++len;
1608 }
1609 if (i == 0)
1610 {
1611 alloc_cmdbuff(len);
1612 if (ccline.cmdbuff == NULL)
1613 goto returncmd;
1614 }
1615 }
1616 ccline.cmdbuff[len] = NUL;
1617 }
1618 else
1619 {
1620 alloc_cmdbuff((int)STRLEN(p));
1621 if (ccline.cmdbuff == NULL)
1622 goto returncmd;
1623 STRCPY(ccline.cmdbuff, p);
1624 }
1625
1626 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1627 redrawcmd();
1628 goto cmdline_changed;
1629 }
1630 beep_flush();
1631 goto cmdline_not_changed;
1632#endif
1633
1634 case Ctrl_V:
1635 case Ctrl_Q:
1636#ifdef FEAT_MOUSE
1637 ignore_drag_release = TRUE;
1638#endif
1639 putcmdline('^', TRUE);
1640 c = get_literal(); /* get next (two) character(s) */
1641 do_abbr = FALSE; /* don't do abbreviation now */
1642#ifdef FEAT_MBYTE
1643 /* may need to remove ^ when composing char was typed */
1644 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1645 {
1646 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1647 msg_putchar(' ');
1648 cursorcmd();
1649 }
1650#endif
1651 break;
1652
1653#ifdef FEAT_DIGRAPHS
1654 case Ctrl_K:
1655#ifdef FEAT_MOUSE
1656 ignore_drag_release = TRUE;
1657#endif
1658 putcmdline('?', TRUE);
1659#ifdef USE_ON_FLY_SCROLL
1660 dont_scroll = TRUE; /* disallow scrolling here */
1661#endif
1662 c = get_digraph(TRUE);
1663 if (c != NUL)
1664 break;
1665
1666 redrawcmd();
1667 goto cmdline_not_changed;
1668#endif /* FEAT_DIGRAPHS */
1669
1670#ifdef FEAT_RIGHTLEFT
1671 case Ctrl__: /* CTRL-_: switch language mode */
1672 if (!p_ari)
1673 break;
1674#ifdef FEAT_FKMAP
1675 if (p_altkeymap)
1676 {
1677 cmd_fkmap = !cmd_fkmap;
1678 if (cmd_fkmap) /* in Farsi always in Insert mode */
1679 ccline.overstrike = FALSE;
1680 }
1681 else /* Hebrew is default */
1682#endif
1683 cmd_hkmap = !cmd_hkmap;
1684 goto cmdline_not_changed;
1685#endif
1686
1687 default:
1688#ifdef UNIX
1689 if (c == intr_char)
1690 {
1691 gotesc = TRUE; /* will free ccline.cmdbuff after
1692 putting it in history */
1693 goto returncmd; /* back to Normal mode */
1694 }
1695#endif
1696 /*
1697 * Normal character with no special meaning. Just set mod_mask
1698 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1699 * the string <S-Space>. This should only happen after ^V.
1700 */
1701 if (!IS_SPECIAL(c))
1702 mod_mask = 0x0;
1703 break;
1704 }
1705 /*
1706 * End of switch on command line character.
1707 * We come here if we have a normal character.
1708 */
1709
Bram Moolenaarede3e632013-06-23 16:16:19 +02001710 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#ifdef FEAT_MBYTE
1712 /* Add ABBR_OFF for characters above 0x100, this is
1713 * what check_abbr() expects. */
1714 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1715#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001716 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 goto cmdline_changed;
1718
1719 /*
1720 * put the character in the command line
1721 */
1722 if (IS_SPECIAL(c) || mod_mask != 0)
1723 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1724 else
1725 {
1726#ifdef FEAT_MBYTE
1727 if (has_mbyte)
1728 {
1729 j = (*mb_char2bytes)(c, IObuff);
1730 IObuff[j] = NUL; /* exclude composing chars */
1731 put_on_cmdline(IObuff, j, TRUE);
1732 }
1733 else
1734#endif
1735 {
1736 IObuff[0] = c;
1737 put_on_cmdline(IObuff, 1, TRUE);
1738 }
1739 }
1740 goto cmdline_changed;
1741
1742/*
1743 * This part implements incremental searches for "/" and "?"
1744 * Jump to cmdline_not_changed when a character has been read but the command
1745 * line did not change. Then we only search and redraw if something changed in
1746 * the past.
1747 * Jump to cmdline_changed when the command line did change.
1748 * (Sorry for the goto's, I know it is ugly).
1749 */
1750cmdline_not_changed:
1751#ifdef FEAT_SEARCH_EXTRA
1752 if (!incsearch_postponed)
1753 continue;
1754#endif
1755
1756cmdline_changed:
1757#ifdef FEAT_SEARCH_EXTRA
1758 /*
1759 * 'incsearch' highlighting.
1760 */
1761 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1762 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001763 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001764#ifdef FEAT_RELTIME
1765 proftime_T tm;
1766#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 /* if there is a character waiting, search and redraw later */
1769 if (char_avail())
1770 {
1771 incsearch_postponed = TRUE;
1772 continue;
1773 }
1774 incsearch_postponed = FALSE;
1775 curwin->w_cursor = old_cursor; /* start at old position */
1776
1777 /* If there is no command line, don't do anything */
1778 if (ccline.cmdlen == 0)
1779 i = 0;
1780 else
1781 {
1782 cursor_off(); /* so the user knows we're busy */
1783 out_flush();
1784 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001785#ifdef FEAT_RELTIME
1786 /* Set the time limit to half a second. */
1787 profile_setlimit(500L, &tm);
1788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001790 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1791#ifdef FEAT_RELTIME
1792 &tm
1793#else
1794 NULL
1795#endif
1796 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 --emsg_off;
1798 /* if interrupted while searching, behave like it failed */
1799 if (got_int)
1800 {
1801 (void)vpeekc(); /* remove <C-C> from input stream */
1802 got_int = FALSE; /* don't abandon the command line */
1803 i = 0;
1804 }
1805 else if (char_avail())
1806 /* cancelled searching because a char was typed */
1807 incsearch_postponed = TRUE;
1808 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001809 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 highlight_match = TRUE; /* highlight position */
1811 else
1812 highlight_match = FALSE; /* remove highlight */
1813
1814 /* first restore the old curwin values, so the screen is
1815 * positioned in the same way as the actual search command */
1816 curwin->w_leftcol = old_leftcol;
1817 curwin->w_topline = old_topline;
1818# ifdef FEAT_DIFF
1819 curwin->w_topfill = old_topfill;
1820# endif
1821 curwin->w_botline = old_botline;
1822 changed_cline_bef_curs();
1823 update_topline();
1824
1825 if (i != 0)
1826 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001827 pos_T save_pos = curwin->w_cursor;
1828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001830 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 * moves the whole match onto the screen when 'nowrap' is set.
1832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 curwin->w_cursor.lnum += search_match_lines;
1834 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001835 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1836 {
1837 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1838 coladvance((colnr_T)MAXCOL);
1839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001841 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001842 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 else
1845 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001848# ifdef FEAT_WINDOWS
1849 /* May redraw the status line to show the cursor position. */
1850 if (p_ru && curwin->w_status_height > 0)
1851 curwin->w_redr_status = TRUE;
1852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001854 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001855 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001856 restore_cmdline(&save_ccline);
1857
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001858 /* Leave it at the end to make CTRL-R CTRL-W work. */
1859 if (i != 0)
1860 curwin->w_cursor = end_pos;
1861
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 msg_starthere();
1863 redrawcmdline();
1864 did_incsearch = TRUE;
1865 }
1866#else /* FEAT_SEARCH_EXTRA */
1867 ;
1868#endif
1869
1870#ifdef FEAT_RIGHTLEFT
1871 if (cmdmsg_rl
1872# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001873 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874# endif
1875 )
1876 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001877 * right-left typing. Not efficient, but it works.
1878 * Do it only when there are no characters left to read
1879 * to avoid useless intermediate redraws. */
1880 if (vpeekc() == NUL)
1881 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882#endif
1883 }
1884
1885returncmd:
1886
1887#ifdef FEAT_RIGHTLEFT
1888 cmdmsg_rl = FALSE;
1889#endif
1890
1891#ifdef FEAT_FKMAP
1892 cmd_fkmap = 0;
1893#endif
1894
1895 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001896 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
1898#ifdef FEAT_SEARCH_EXTRA
1899 if (did_incsearch)
1900 {
1901 curwin->w_cursor = old_cursor;
1902 curwin->w_curswant = old_curswant;
1903 curwin->w_leftcol = old_leftcol;
1904 curwin->w_topline = old_topline;
1905# ifdef FEAT_DIFF
1906 curwin->w_topfill = old_topfill;
1907# endif
1908 curwin->w_botline = old_botline;
1909 highlight_match = FALSE;
1910 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001911 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913#endif
1914
1915 if (ccline.cmdbuff != NULL)
1916 {
1917 /*
1918 * Put line in history buffer (":" and "=" only when it was typed).
1919 */
1920#ifdef FEAT_CMDHIST
1921 if (ccline.cmdlen && firstc != NUL
1922 && (some_key_typed || histype == HIST_SEARCH))
1923 {
1924 add_to_history(histype, ccline.cmdbuff, TRUE,
1925 histype == HIST_SEARCH ? firstc : NUL);
1926 if (firstc == ':')
1927 {
1928 vim_free(new_last_cmdline);
1929 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1930 }
1931 }
1932#endif
1933
1934 if (gotesc) /* abandon command line */
1935 {
1936 vim_free(ccline.cmdbuff);
1937 ccline.cmdbuff = NULL;
1938 if (msg_scrolled == 0)
1939 compute_cmdrow();
1940 MSG("");
1941 redraw_cmdline = TRUE;
1942 }
1943 }
1944
1945 /*
1946 * If the screen was shifted up, redraw the whole screen (later).
1947 * If the line is too long, clear it, so ruler and shown command do
1948 * not get printed in the middle of it.
1949 */
1950 msg_check();
1951 msg_scroll = save_msg_scroll;
1952 redir_off = FALSE;
1953
1954 /* When the command line was typed, no need for a wait-return prompt. */
1955 if (some_key_typed)
1956 need_wait_return = FALSE;
1957
1958 State = save_State;
1959#ifdef USE_IM_CONTROL
1960 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1961 im_save_status(b_im_ptr);
1962 im_set_active(FALSE);
1963#endif
1964#ifdef FEAT_MOUSE
1965 setmouse();
1966#endif
1967#ifdef CURSOR_SHAPE
1968 ui_cursor_shape(); /* may show different cursor shape */
1969#endif
1970
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001971 {
1972 char_u *p = ccline.cmdbuff;
1973
1974 /* Make ccline empty, getcmdline() may try to use it. */
1975 ccline.cmdbuff = NULL;
1976 return p;
1977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978}
1979
1980#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1981/*
1982 * Get a command line with a prompt.
1983 * This is prepared to be called recursively from getcmdline() (e.g. by
1984 * f_input() when evaluating an expression from CTRL-R =).
1985 * Returns the command line in allocated memory, or NULL.
1986 */
1987 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001988getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 int firstc;
1990 char_u *prompt; /* command line prompt */
1991 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001992 int xp_context; /* type of expansion */
1993 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994{
1995 char_u *s;
1996 struct cmdline_info save_ccline;
1997 int msg_col_save = msg_col;
1998
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001999 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 ccline.cmdprompt = prompt;
2001 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002002# ifdef FEAT_EVAL
2003 ccline.xp_context = xp_context;
2004 ccline.xp_arg = xp_arg;
2005 ccline.input_fn = (firstc == '@');
2006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002008 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002009 /* Restore msg_col, the prompt from input() may have changed it.
2010 * But only if called recursively and the commandline is therefore being
2011 * restored to an old one; if not, the input() prompt stays on the screen,
2012 * so we need its modified msg_col left intact. */
2013 if (ccline.cmdbuff != NULL)
2014 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016 return s;
2017}
2018#endif
2019
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002020/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002021 * Return TRUE when the text must not be changed and we can't switch to
2022 * another window or buffer. Used when editing the command line, evaluating
2023 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002024 */
2025 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002026text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002027{
2028#ifdef FEAT_CMDWIN
2029 if (cmdwin_type != 0)
2030 return TRUE;
2031#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002032 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002033}
2034
2035/*
2036 * Give an error message for a command that isn't allowed while the cmdline
2037 * window is open or editing the cmdline in another way.
2038 */
2039 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002040text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002041{
2042#ifdef FEAT_CMDWIN
2043 if (cmdwin_type != 0)
2044 EMSG(_(e_cmdwin));
2045 else
2046#endif
2047 EMSG(_(e_secure));
2048}
2049
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002050#if defined(FEAT_AUTOCMD) || defined(PROTO)
2051/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002052 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2053 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002054 */
2055 int
2056curbuf_locked()
2057{
2058 if (curbuf_lock > 0)
2059 {
2060 EMSG(_("E788: Not allowed to edit another buffer now"));
2061 return TRUE;
2062 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002063 return allbuf_locked();
2064}
2065
2066/*
2067 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2068 * message.
2069 */
2070 int
2071allbuf_locked()
2072{
2073 if (allbuf_lock > 0)
2074 {
2075 EMSG(_("E811: Not allowed to change buffer information now"));
2076 return TRUE;
2077 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002078 return FALSE;
2079}
2080#endif
2081
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 static int
2083cmdline_charsize(idx)
2084 int idx;
2085{
2086#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2087 if (cmdline_star > 0) /* showing '*', always 1 position */
2088 return 1;
2089#endif
2090 return ptr2cells(ccline.cmdbuff + idx);
2091}
2092
2093/*
2094 * Compute the offset of the cursor on the command line for the prompt and
2095 * indent.
2096 */
2097 static void
2098set_cmdspos()
2099{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002100 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 ccline.cmdspos = 1 + ccline.cmdindent;
2102 else
2103 ccline.cmdspos = 0 + ccline.cmdindent;
2104}
2105
2106/*
2107 * Compute the screen position for the cursor on the command line.
2108 */
2109 static void
2110set_cmdspos_cursor()
2111{
2112 int i, m, c;
2113
2114 set_cmdspos();
2115 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002118 if (m < 0) /* overflow, Columns or Rows at weird value */
2119 m = MAXCOL;
2120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 else
2122 m = MAXCOL;
2123 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2124 {
2125 c = cmdline_charsize(i);
2126#ifdef FEAT_MBYTE
2127 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2128 if (has_mbyte)
2129 correct_cmdspos(i, c);
2130#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002131 /* If the cmdline doesn't fit, show cursor on last visible char.
2132 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if ((ccline.cmdspos += c) >= m)
2134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 ccline.cmdspos -= c;
2136 break;
2137 }
2138#ifdef FEAT_MBYTE
2139 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002140 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141#endif
2142 }
2143}
2144
2145#ifdef FEAT_MBYTE
2146/*
2147 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2148 * character that doesn't fit, so that a ">" must be displayed.
2149 */
2150 static void
2151correct_cmdspos(idx, cells)
2152 int idx;
2153 int cells;
2154{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002155 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2157 && ccline.cmdspos % Columns + cells > Columns)
2158 ccline.cmdspos++;
2159}
2160#endif
2161
2162/*
2163 * Get an Ex command line for the ":" command.
2164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002166getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002168 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 int indent; /* indent for inside conditionals */
2170{
2171 /* When executing a register, remove ':' that's in front of each line. */
2172 if (exec_from_reg && vpeekc() == ':')
2173 (void)vgetc();
2174 return getcmdline(c, 1L, indent);
2175}
2176
2177/*
2178 * Get an Ex command line for Ex mode.
2179 * In Ex mode we only use the OS supplied line editing features and no
2180 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002181 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002184getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 int promptc; /* normally ':', NUL for ":append" and '?' for
2186 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002187 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 int indent; /* indent for inside conditionals */
2189{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002190 garray_T line_ga;
2191 char_u *pend;
2192 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002193 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002194 int escaped = FALSE; /* CTRL-V typed */
2195 int vcol = 0;
2196 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002197 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002198 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199
2200 /* Switch cursor on now. This avoids that it happens after the "\n", which
2201 * confuses the system function that computes tabstops. */
2202 cursor_on();
2203
2204 /* always start in column 0; write a newline if necessary */
2205 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002206 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002208 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002210 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211 if (p_prompt)
2212 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 while (indent-- > 0)
2214 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 }
2217
2218 ga_init2(&line_ga, 1, 30);
2219
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002220 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002221 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002222 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002223 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002224 while (indent >= 8)
2225 {
2226 ga_append(&line_ga, TAB);
2227 msg_puts((char_u *)" ");
2228 indent -= 8;
2229 }
2230 while (indent-- > 0)
2231 {
2232 ga_append(&line_ga, ' ');
2233 msg_putchar(' ');
2234 }
2235 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002236 ++no_mapping;
2237 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 /*
2240 * Get the line, one character at a time.
2241 */
2242 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002243 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
2245 if (ga_grow(&line_ga, 40) == FAIL)
2246 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002248 /* Get one character at a time. Don't use inchar(), it can't handle
2249 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002250 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002251 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
2253 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002254 * Handle line editing.
2255 * Previously this was left to the system, putting the terminal in
2256 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002258 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002260 msg_putchar('\n');
2261 break;
2262 }
2263
2264 if (!escaped)
2265 {
2266 /* CR typed means "enter", which is NL */
2267 if (c1 == '\r')
2268 c1 = '\n';
2269
2270 if (c1 == BS || c1 == K_BS
2271 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002273 if (line_ga.ga_len > 0)
2274 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002275#ifdef FEAT_MBYTE
2276 if (has_mbyte)
2277 {
2278 p = (char_u *)line_ga.ga_data;
2279 p[line_ga.ga_len] = NUL;
2280 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2281 line_ga.ga_len -= len;
2282 }
2283 else
2284#endif
2285 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002286 goto redraw;
2287 }
2288 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 }
2290
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002291 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002293 msg_col = startcol;
2294 msg_clr_eos();
2295 line_ga.ga_len = 0;
2296 continue;
2297 }
2298
2299 if (c1 == Ctrl_T)
2300 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002301 long sw = get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002302
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002303 p = (char_u *)line_ga.ga_data;
2304 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002305 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002306 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002307add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002308 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 char_u *s = skipwhite(p);
2311
2312 ga_grow(&line_ga, 1);
2313 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2314 *s = ' ';
2315 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002317redraw:
2318 /* redraw the line */
2319 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002320 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002321 p = (char_u *)line_ga.ga_data;
2322 p[line_ga.ga_len] = NUL;
2323 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002325 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002327 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002329 msg_putchar(' ');
2330 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002331 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002333 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002335 len = MB_PTR2LEN(p);
2336 msg_outtrans_len(p, len);
2337 vcol += ptr2cells(p);
2338 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002341 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002342 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002343 continue;
2344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002346 if (c1 == Ctrl_D)
2347 {
2348 /* Delete one shiftwidth. */
2349 p = (char_u *)line_ga.ga_data;
2350 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002352 if (prev_char == '^')
2353 ex_keep_indent = TRUE;
2354 indent = 0;
2355 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357 else
2358 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002359 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002360 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002361 --indent;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01002362 indent -= indent % get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002364 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002365 {
2366 char_u *s = skipwhite(p);
2367
2368 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2369 --line_ga.ga_len;
2370 }
2371 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002373
2374 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2375 {
2376 escaped = TRUE;
2377 continue;
2378 }
2379
2380 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2381 if (IS_SPECIAL(c1))
2382 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002384
2385 if (IS_SPECIAL(c1))
2386 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002387#ifdef FEAT_MBYTE
2388 if (has_mbyte)
2389 len = (*mb_char2bytes)(c1,
2390 (char_u *)line_ga.ga_data + line_ga.ga_len);
2391 else
2392#endif
2393 {
2394 len = 1;
2395 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2396 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002397 if (c1 == '\n')
2398 msg_putchar('\n');
2399 else if (c1 == TAB)
2400 {
2401 /* Don't use chartabsize(), 'ts' can be different */
2402 do
2403 {
2404 msg_putchar(' ');
2405 } while (++vcol % 8);
2406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002409 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002410 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002411 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002413 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002414 escaped = FALSE;
2415
2416 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002417 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002418
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002419 /* We are done when a NL is entered, but not when it comes after an
2420 * odd number of backslashes, that results in a NUL. */
2421 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002423 int bcount = 0;
2424
2425 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2426 ++bcount;
2427
2428 if (bcount > 0)
2429 {
2430 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2431 * "\NL", etc. */
2432 line_ga.ga_len -= (bcount + 1) / 2;
2433 pend -= (bcount + 1) / 2;
2434 pend[-1] = '\n';
2435 }
2436
2437 if ((bcount & 1) == 0)
2438 {
2439 --line_ga.ga_len;
2440 --pend;
2441 *pend = NUL;
2442 break;
2443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 }
2445 }
2446
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002447 --no_mapping;
2448 --allow_keys;
2449
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 /* make following messages go to the next line */
2451 msg_didout = FALSE;
2452 msg_col = 0;
2453 if (msg_row < Rows - 1)
2454 ++msg_row;
2455 emsg_on_display = FALSE; /* don't want ui_delay() */
2456
2457 if (got_int)
2458 ga_clear(&line_ga);
2459
2460 return (char_u *)line_ga.ga_data;
2461}
2462
Bram Moolenaare344bea2005-09-01 20:46:49 +00002463# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2464 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465/*
2466 * Return TRUE if ccline.overstrike is on.
2467 */
2468 int
2469cmdline_overstrike()
2470{
2471 return ccline.overstrike;
2472}
2473
2474/*
2475 * Return TRUE if the cursor is at the end of the cmdline.
2476 */
2477 int
2478cmdline_at_end()
2479{
2480 return (ccline.cmdpos >= ccline.cmdlen);
2481}
2482#endif
2483
Bram Moolenaar9372a112005-12-06 19:59:18 +00002484#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485/*
2486 * Return the virtual column number at the current cursor position.
2487 * This is used by the IM code to obtain the start of the preedit string.
2488 */
2489 colnr_T
2490cmdline_getvcol_cursor()
2491{
2492 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2493 return MAXCOL;
2494
2495# ifdef FEAT_MBYTE
2496 if (has_mbyte)
2497 {
2498 colnr_T col;
2499 int i = 0;
2500
2501 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002502 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
2504 return col;
2505 }
2506 else
2507# endif
2508 return ccline.cmdpos;
2509}
2510#endif
2511
2512#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2513/*
2514 * If part of the command line is an IM preedit string, redraw it with
2515 * IM feedback attributes. The cursor position is restored after drawing.
2516 */
2517 static void
2518redrawcmd_preedit()
2519{
2520 if ((State & CMDLINE)
2521 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002522 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 && !p_imdisable
2524 && im_is_preediting())
2525 {
2526 int cmdpos = 0;
2527 int cmdspos;
2528 int old_row;
2529 int old_col;
2530 colnr_T col;
2531
2532 old_row = msg_row;
2533 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002534 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
2536# ifdef FEAT_MBYTE
2537 if (has_mbyte)
2538 {
2539 for (col = 0; col < preedit_start_col
2540 && cmdpos < ccline.cmdlen; ++col)
2541 {
2542 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002543 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
2545 }
2546 else
2547# endif
2548 {
2549 cmdspos += preedit_start_col;
2550 cmdpos += preedit_start_col;
2551 }
2552
2553 msg_row = cmdline_row + (cmdspos / (int)Columns);
2554 msg_col = cmdspos % (int)Columns;
2555 if (msg_row >= Rows)
2556 msg_row = Rows - 1;
2557
2558 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2559 {
2560 int char_len;
2561 int char_attr;
2562
2563 char_attr = im_get_feedback_attr(col);
2564 if (char_attr < 0)
2565 break; /* end of preedit string */
2566
2567# ifdef FEAT_MBYTE
2568 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002569 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 else
2571# endif
2572 char_len = 1;
2573
2574 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2575 cmdpos += char_len;
2576 }
2577
2578 msg_row = old_row;
2579 msg_col = old_col;
2580 }
2581}
2582#endif /* FEAT_XIM && FEAT_GUI_GTK */
2583
2584/*
2585 * Allocate a new command line buffer.
2586 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2587 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2588 */
2589 static void
2590alloc_cmdbuff(len)
2591 int len;
2592{
2593 /*
2594 * give some extra space to avoid having to allocate all the time
2595 */
2596 if (len < 80)
2597 len = 100;
2598 else
2599 len += 20;
2600
2601 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2602 ccline.cmdbufflen = len;
2603}
2604
2605/*
2606 * Re-allocate the command line to length len + something extra.
2607 * return FAIL for failure, OK otherwise
2608 */
2609 static int
2610realloc_cmdbuff(len)
2611 int len;
2612{
2613 char_u *p;
2614
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002615 if (len < ccline.cmdbufflen)
2616 return OK; /* no need to resize */
2617
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 p = ccline.cmdbuff;
2619 alloc_cmdbuff(len); /* will get some more */
2620 if (ccline.cmdbuff == NULL) /* out of memory */
2621 {
2622 ccline.cmdbuff = p; /* keep the old one */
2623 return FAIL;
2624 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002625 /* There isn't always a NUL after the command, but it may need to be
2626 * there, thus copy up to the NUL and add a NUL. */
2627 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2628 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002630
2631 if (ccline.xpc != NULL
2632 && ccline.xpc->xp_pattern != NULL
2633 && ccline.xpc->xp_context != EXPAND_NOTHING
2634 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2635 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002636 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002637
2638 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2639 * to point into the newly allocated memory. */
2640 if (i >= 0 && i <= ccline.cmdlen)
2641 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2642 }
2643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 return OK;
2645}
2646
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002647#if defined(FEAT_ARABIC) || defined(PROTO)
2648static char_u *arshape_buf = NULL;
2649
2650# if defined(EXITFREE) || defined(PROTO)
2651 void
2652free_cmdline_buf()
2653{
2654 vim_free(arshape_buf);
2655}
2656# endif
2657#endif
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659/*
2660 * Draw part of the cmdline at the current cursor position. But draw stars
2661 * when cmdline_star is TRUE.
2662 */
2663 static void
2664draw_cmdline(start, len)
2665 int start;
2666 int len;
2667{
2668#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2669 int i;
2670
2671 if (cmdline_star > 0)
2672 for (i = 0; i < len; ++i)
2673 {
2674 msg_putchar('*');
2675# ifdef FEAT_MBYTE
2676 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002677 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678# endif
2679 }
2680 else
2681#endif
2682#ifdef FEAT_ARABIC
2683 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 static int buflen = 0;
2686 char_u *p;
2687 int j;
2688 int newlen = 0;
2689 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002690 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 int prev_c = 0;
2692 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002693 int u8c;
2694 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697 /*
2698 * Do arabic shaping into a temporary buffer. This is very
2699 * inefficient!
2700 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002701 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
2703 /* Re-allocate the buffer. We keep it around to avoid a lot of
2704 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002705 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002706 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002707 arshape_buf = alloc(buflen);
2708 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 return; /* out of memory */
2710 }
2711
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002712 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2713 {
2714 /* Prepend a space to draw the leading composing char on. */
2715 arshape_buf[0] = ' ';
2716 newlen = 1;
2717 }
2718
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 for (j = start; j < start + len; j += mb_l)
2720 {
2721 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002722 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002723 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 if (ARABIC_CHAR(u8c))
2725 {
2726 /* Do Arabic shaping. */
2727 if (cmdmsg_rl)
2728 {
2729 /* displaying from right to left */
2730 pc = prev_c;
2731 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002732 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (j + mb_l >= start + len)
2734 nc = NUL;
2735 else
2736 nc = utf_ptr2char(p + mb_l);
2737 }
2738 else
2739 {
2740 /* displaying from left to right */
2741 if (j + mb_l >= start + len)
2742 pc = NUL;
2743 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002744 {
2745 int pcc[MAX_MCO];
2746
2747 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002749 pc1 = pcc[0];
2750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 nc = prev_c;
2752 }
2753 prev_c = u8c;
2754
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002755 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002757 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002758 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002760 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2761 if (u8cc[1] != 0)
2762 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002763 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
2765 }
2766 else
2767 {
2768 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002769 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 newlen += mb_l;
2771 }
2772 }
2773
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002774 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
2776 else
2777#endif
2778 msg_outtrans_len(ccline.cmdbuff + start, len);
2779}
2780
2781/*
2782 * Put a character on the command line. Shifts the following text to the
2783 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2784 * "c" must be printable (fit in one display cell)!
2785 */
2786 void
2787putcmdline(c, shift)
2788 int c;
2789 int shift;
2790{
2791 if (cmd_silent)
2792 return;
2793 msg_no_more = TRUE;
2794 msg_putchar(c);
2795 if (shift)
2796 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2797 msg_no_more = FALSE;
2798 cursorcmd();
2799}
2800
2801/*
2802 * Undo a putcmdline(c, FALSE).
2803 */
2804 void
2805unputcmdline()
2806{
2807 if (cmd_silent)
2808 return;
2809 msg_no_more = TRUE;
2810 if (ccline.cmdlen == ccline.cmdpos)
2811 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002812#ifdef FEAT_MBYTE
2813 else if (has_mbyte)
2814 draw_cmdline(ccline.cmdpos,
2815 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 else
2818 draw_cmdline(ccline.cmdpos, 1);
2819 msg_no_more = FALSE;
2820 cursorcmd();
2821}
2822
2823/*
2824 * Put the given string, of the given length, onto the command line.
2825 * If len is -1, then STRLEN() is used to calculate the length.
2826 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2827 * part will be redrawn, otherwise it will not. If this function is called
2828 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2829 * called afterwards.
2830 */
2831 int
2832put_on_cmdline(str, len, redraw)
2833 char_u *str;
2834 int len;
2835 int redraw;
2836{
2837 int retval;
2838 int i;
2839 int m;
2840 int c;
2841
2842 if (len < 0)
2843 len = (int)STRLEN(str);
2844
2845 /* Check if ccline.cmdbuff needs to be longer */
2846 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002847 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 else
2849 retval = OK;
2850 if (retval == OK)
2851 {
2852 if (!ccline.overstrike)
2853 {
2854 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2855 ccline.cmdbuff + ccline.cmdpos,
2856 (size_t)(ccline.cmdlen - ccline.cmdpos));
2857 ccline.cmdlen += len;
2858 }
2859 else
2860 {
2861#ifdef FEAT_MBYTE
2862 if (has_mbyte)
2863 {
2864 /* Count nr of characters in the new string. */
2865 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002866 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 ++m;
2868 /* Count nr of bytes in cmdline that are overwritten by these
2869 * characters. */
2870 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002871 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 --m;
2873 if (i < ccline.cmdlen)
2874 {
2875 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2876 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2877 ccline.cmdlen += ccline.cmdpos + len - i;
2878 }
2879 else
2880 ccline.cmdlen = ccline.cmdpos + len;
2881 }
2882 else
2883#endif
2884 if (ccline.cmdpos + len > ccline.cmdlen)
2885 ccline.cmdlen = ccline.cmdpos + len;
2886 }
2887 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2888 ccline.cmdbuff[ccline.cmdlen] = NUL;
2889
2890#ifdef FEAT_MBYTE
2891 if (enc_utf8)
2892 {
2893 /* When the inserted text starts with a composing character,
2894 * backup to the character before it. There could be two of them.
2895 */
2896 i = 0;
2897 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2898 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2899 {
2900 i = (*mb_head_off)(ccline.cmdbuff,
2901 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2902 ccline.cmdpos -= i;
2903 len += i;
2904 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2905 }
2906# ifdef FEAT_ARABIC
2907 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2908 {
2909 /* Check the previous character for Arabic combining pair. */
2910 i = (*mb_head_off)(ccline.cmdbuff,
2911 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2912 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2913 + ccline.cmdpos - i), c))
2914 {
2915 ccline.cmdpos -= i;
2916 len += i;
2917 }
2918 else
2919 i = 0;
2920 }
2921# endif
2922 if (i != 0)
2923 {
2924 /* Also backup the cursor position. */
2925 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2926 ccline.cmdspos -= i;
2927 msg_col -= i;
2928 if (msg_col < 0)
2929 {
2930 msg_col += Columns;
2931 --msg_row;
2932 }
2933 }
2934 }
2935#endif
2936
2937 if (redraw && !cmd_silent)
2938 {
2939 msg_no_more = TRUE;
2940 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002941 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2943 /* Avoid clearing the rest of the line too often. */
2944 if (cmdline_row != i || ccline.overstrike)
2945 msg_clr_eos();
2946 msg_no_more = FALSE;
2947 }
2948#ifdef FEAT_FKMAP
2949 /*
2950 * If we are in Farsi command mode, the character input must be in
2951 * Insert mode. So do not advance the cmdpos.
2952 */
2953 if (!cmd_fkmap)
2954#endif
2955 {
2956 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002959 if (m < 0) /* overflow, Columns or Rows at weird value */
2960 m = MAXCOL;
2961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 else
2963 m = MAXCOL;
2964 for (i = 0; i < len; ++i)
2965 {
2966 c = cmdline_charsize(ccline.cmdpos);
2967#ifdef FEAT_MBYTE
2968 /* count ">" for a double-wide char that doesn't fit. */
2969 if (has_mbyte)
2970 correct_cmdspos(ccline.cmdpos, c);
2971#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002972 /* Stop cursor at the end of the screen, but do increment the
2973 * insert position, so that entering a very long command
2974 * works, even though you can't see it. */
2975 if (ccline.cmdspos + c < m)
2976 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977#ifdef FEAT_MBYTE
2978 if (has_mbyte)
2979 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002980 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 if (c > len - i - 1)
2982 c = len - i - 1;
2983 ccline.cmdpos += c;
2984 i += c;
2985 }
2986#endif
2987 ++ccline.cmdpos;
2988 }
2989 }
2990 }
2991 if (redraw)
2992 msg_check();
2993 return retval;
2994}
2995
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002996static struct cmdline_info prev_ccline;
2997static int prev_ccline_used = FALSE;
2998
2999/*
3000 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3001 * and overwrite it. But get_cmdline_str() may need it, thus make it
3002 * available globally in prev_ccline.
3003 */
3004 static void
3005save_cmdline(ccp)
3006 struct cmdline_info *ccp;
3007{
3008 if (!prev_ccline_used)
3009 {
3010 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3011 prev_ccline_used = TRUE;
3012 }
3013 *ccp = prev_ccline;
3014 prev_ccline = ccline;
3015 ccline.cmdbuff = NULL;
3016 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003017 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003018}
3019
3020/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003021 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003022 */
3023 static void
3024restore_cmdline(ccp)
3025 struct cmdline_info *ccp;
3026{
3027 ccline = prev_ccline;
3028 prev_ccline = *ccp;
3029}
3030
Bram Moolenaar5a305422006-04-28 22:38:25 +00003031#if defined(FEAT_EVAL) || defined(PROTO)
3032/*
3033 * Save the command line into allocated memory. Returns a pointer to be
3034 * passed to restore_cmdline_alloc() later.
3035 * Returns NULL when failed.
3036 */
3037 char_u *
3038save_cmdline_alloc()
3039{
3040 struct cmdline_info *p;
3041
3042 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3043 if (p != NULL)
3044 save_cmdline(p);
3045 return (char_u *)p;
3046}
3047
3048/*
3049 * Restore the command line from the return value of save_cmdline_alloc().
3050 */
3051 void
3052restore_cmdline_alloc(p)
3053 char_u *p;
3054{
3055 if (p != NULL)
3056 {
3057 restore_cmdline((struct cmdline_info *)p);
3058 vim_free(p);
3059 }
3060}
3061#endif
3062
Bram Moolenaar8299df92004-07-10 09:47:34 +00003063/*
3064 * paste a yank register into the command line.
3065 * used by CTRL-R command in command-line mode
3066 * insert_reg() can't be used here, because special characters from the
3067 * register contents will be interpreted as commands.
3068 *
3069 * return FAIL for failure, OK otherwise
3070 */
3071 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003072cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003073 int regname;
3074 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003075 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003076{
3077 long i;
3078 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003079 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003080 int allocated;
3081 struct cmdline_info save_ccline;
3082
3083 /* check for valid regname; also accept special characters for CTRL-R in
3084 * the command line */
3085 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3086 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3087 return FAIL;
3088
3089 /* A register containing CTRL-R can cause an endless loop. Allow using
3090 * CTRL-C to break the loop. */
3091 line_breakcheck();
3092 if (got_int)
3093 return FAIL;
3094
3095#ifdef FEAT_CLIPBOARD
3096 regname = may_get_selection(regname);
3097#endif
3098
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003099 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003100 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003101 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003102 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003103 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003104 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003105 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003106
3107 if (i)
3108 {
3109 /* Got the value of a special register in "arg". */
3110 if (arg == NULL)
3111 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003112
3113 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3114 * part of the word. */
3115 p = arg;
3116 if (p_is && regname == Ctrl_W)
3117 {
3118 char_u *w;
3119 int len;
3120
3121 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003122 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003123 {
3124#ifdef FEAT_MBYTE
3125 if (has_mbyte)
3126 {
3127 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3128 if (!vim_iswordc(mb_ptr2char(w - len)))
3129 break;
3130 w -= len;
3131 }
3132 else
3133#endif
3134 {
3135 if (!vim_iswordc(w[-1]))
3136 break;
3137 --w;
3138 }
3139 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003140 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003141 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3142 p += len;
3143 }
3144
3145 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003146 if (allocated)
3147 vim_free(arg);
3148 return OK;
3149 }
3150
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003151 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003152}
3153
3154/*
3155 * Put a string on the command line.
3156 * When "literally" is TRUE, insert literally.
3157 * When "literally" is FALSE, insert as typed, but don't leave the command
3158 * line.
3159 */
3160 void
3161cmdline_paste_str(s, literally)
3162 char_u *s;
3163 int literally;
3164{
3165 int c, cv;
3166
3167 if (literally)
3168 put_on_cmdline(s, -1, TRUE);
3169 else
3170 while (*s != NUL)
3171 {
3172 cv = *s;
3173 if (cv == Ctrl_V && s[1])
3174 ++s;
3175#ifdef FEAT_MBYTE
3176 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003177 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003178 else
3179#endif
3180 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003181 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3182 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003183#ifdef UNIX
3184 || c == intr_char
3185#endif
3186 || (c == Ctrl_BSL && *s == Ctrl_N))
3187 stuffcharReadbuff(Ctrl_V);
3188 stuffcharReadbuff(c);
3189 }
3190}
3191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192#ifdef FEAT_WILDMENU
3193/*
3194 * Delete characters on the command line, from "from" to the current
3195 * position.
3196 */
3197 static void
3198cmdline_del(from)
3199 int from;
3200{
3201 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3202 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3203 ccline.cmdlen -= ccline.cmdpos - from;
3204 ccline.cmdpos = from;
3205}
3206#endif
3207
3208/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003209 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 * search
3211 */
3212 void
3213redrawcmdline()
3214{
3215 if (cmd_silent)
3216 return;
3217 need_wait_return = FALSE;
3218 compute_cmdrow();
3219 redrawcmd();
3220 cursorcmd();
3221}
3222
3223 static void
3224redrawcmdprompt()
3225{
3226 int i;
3227
3228 if (cmd_silent)
3229 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003230 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 msg_putchar(ccline.cmdfirstc);
3232 if (ccline.cmdprompt != NULL)
3233 {
3234 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3235 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3236 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003237 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 --ccline.cmdindent;
3239 }
3240 else
3241 for (i = ccline.cmdindent; i > 0; --i)
3242 msg_putchar(' ');
3243}
3244
3245/*
3246 * Redraw what is currently on the command line.
3247 */
3248 void
3249redrawcmd()
3250{
3251 if (cmd_silent)
3252 return;
3253
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003254 /* when 'incsearch' is set there may be no command line while redrawing */
3255 if (ccline.cmdbuff == NULL)
3256 {
3257 windgoto(cmdline_row, 0);
3258 msg_clr_eos();
3259 return;
3260 }
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 msg_start();
3263 redrawcmdprompt();
3264
3265 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3266 msg_no_more = TRUE;
3267 draw_cmdline(0, ccline.cmdlen);
3268 msg_clr_eos();
3269 msg_no_more = FALSE;
3270
3271 set_cmdspos_cursor();
3272
3273 /*
3274 * An emsg() before may have set msg_scroll. This is used in normal mode,
3275 * in cmdline mode we can reset them now.
3276 */
3277 msg_scroll = FALSE; /* next message overwrites cmdline */
3278
3279 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3280 * in cmdline mode */
3281 skip_redraw = FALSE;
3282}
3283
3284 void
3285compute_cmdrow()
3286{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003287 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 cmdline_row = Rows - 1;
3289 else
3290 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3291 + W_STATUS_HEIGHT(lastwin);
3292}
3293
3294 static void
3295cursorcmd()
3296{
3297 if (cmd_silent)
3298 return;
3299
3300#ifdef FEAT_RIGHTLEFT
3301 if (cmdmsg_rl)
3302 {
3303 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3304 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3305 if (msg_row <= 0)
3306 msg_row = Rows - 1;
3307 }
3308 else
3309#endif
3310 {
3311 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3312 msg_col = ccline.cmdspos % (int)Columns;
3313 if (msg_row >= Rows)
3314 msg_row = Rows - 1;
3315 }
3316
3317 windgoto(msg_row, msg_col);
3318#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3319 redrawcmd_preedit();
3320#endif
3321#ifdef MCH_CURSOR_SHAPE
3322 mch_update_cursor();
3323#endif
3324}
3325
3326 void
3327gotocmdline(clr)
3328 int clr;
3329{
3330 msg_start();
3331#ifdef FEAT_RIGHTLEFT
3332 if (cmdmsg_rl)
3333 msg_col = Columns - 1;
3334 else
3335#endif
3336 msg_col = 0; /* always start in column 0 */
3337 if (clr) /* clear the bottom line(s) */
3338 msg_clr_eos(); /* will reset clear_cmdline */
3339 windgoto(cmdline_row, 0);
3340}
3341
3342/*
3343 * Check the word in front of the cursor for an abbreviation.
3344 * Called when the non-id character "c" has been entered.
3345 * When an abbreviation is recognized it is removed from the text with
3346 * backspaces and the replacement string is inserted, followed by "c".
3347 */
3348 static int
3349ccheck_abbr(c)
3350 int c;
3351{
3352 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3353 return FALSE;
3354
3355 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3356}
3357
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003358#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3359 static int
3360#ifdef __BORLANDC__
3361_RTLENTRYF
3362#endif
3363sort_func_compare(s1, s2)
3364 const void *s1;
3365 const void *s2;
3366{
3367 char_u *p1 = *(char_u **)s1;
3368 char_u *p2 = *(char_u **)s2;
3369
3370 if (*p1 != '<' && *p2 == '<') return -1;
3371 if (*p1 == '<' && *p2 != '<') return 1;
3372 return STRCMP(p1, p2);
3373}
3374#endif
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376/*
3377 * Return FAIL if this is not an appropriate context in which to do
3378 * completion of anything, return OK if it is (even if there are no matches).
3379 * For the caller, this means that the character is just passed through like a
3380 * normal character (instead of being expanded). This allows :s/^I^D etc.
3381 */
3382 static int
Bram Moolenaarb3479632012-11-28 16:49:58 +01003383nextwild(xp, type, options, escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 expand_T *xp;
3385 int type;
3386 int options; /* extra options for ExpandOne() */
Bram Moolenaarb3479632012-11-28 16:49:58 +01003387 int escape; /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389 int i, j;
3390 char_u *p1;
3391 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 int difflen;
3393 int v;
3394
3395 if (xp->xp_numfiles == -1)
3396 {
3397 set_expand_context(xp);
3398 cmd_showtail = expand_showtail(xp);
3399 }
3400
3401 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3402 {
3403 beep_flush();
3404 return OK; /* Something illegal on command line */
3405 }
3406 if (xp->xp_context == EXPAND_NOTHING)
3407 {
3408 /* Caller can use the character as a normal char instead */
3409 return FAIL;
3410 }
3411
3412 MSG_PUTS("..."); /* show that we are busy */
3413 out_flush();
3414
3415 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003416 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417
3418 if (type == WILD_NEXT || type == WILD_PREV)
3419 {
3420 /*
3421 * Get next/previous match for a previous expanded pattern.
3422 */
3423 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3424 }
3425 else
3426 {
3427 /*
3428 * Translate string into pattern and expand it.
3429 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003430 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3431 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 p2 = NULL;
3433 else
3434 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003435 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003436 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3437 if (escape)
3438 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003439
3440 if (p_wic)
3441 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003442 p2 = ExpandOne(xp, p1,
3443 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003444 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003446 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 if (p2 != NULL && type == WILD_LONGEST)
3448 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003449 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 if (ccline.cmdbuff[i + j] == '*'
3451 || ccline.cmdbuff[i + j] == '?')
3452 break;
3453 if ((int)STRLEN(p2) < j)
3454 {
3455 vim_free(p2);
3456 p2 = NULL;
3457 }
3458 }
3459 }
3460 }
3461
3462 if (p2 != NULL && !got_int)
3463 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003464 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003465 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003467 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 xp->xp_pattern = ccline.cmdbuff + i;
3469 }
3470 else
3471 v = OK;
3472 if (v == OK)
3473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003474 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3475 &ccline.cmdbuff[ccline.cmdpos],
3476 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3477 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 ccline.cmdlen += difflen;
3479 ccline.cmdpos += difflen;
3480 }
3481 }
3482 vim_free(p2);
3483
3484 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003485 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
3487 /* When expanding a ":map" command and no matches are found, assume that
3488 * the key is supposed to be inserted literally */
3489 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3490 return FAIL;
3491
3492 if (xp->xp_numfiles <= 0 && p2 == NULL)
3493 beep_flush();
3494 else if (xp->xp_numfiles == 1)
3495 /* free expanded pattern */
3496 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3497
3498 return OK;
3499}
3500
3501/*
3502 * Do wildcard expansion on the string 'str'.
3503 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003504 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 * Return NULL for failure.
3506 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003507 * "orig" is the originally expanded string, copied to allocated memory. It
3508 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3509 * WILD_PREV "orig" should be NULL.
3510 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003511 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3512 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 *
3514 * mode = WILD_FREE: just free previously expanded matches
3515 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3516 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3517 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3518 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3519 * mode = WILD_ALL: return all matches concatenated
3520 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003521 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 *
3523 * options = WILD_LIST_NOTFOUND: list entries without a match
3524 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3525 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3526 * options = WILD_NO_BEEP: Don't beep for multiple matches
3527 * options = WILD_ADD_SLASH: add a slash after directory names
3528 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3529 * options = WILD_SILENT: don't print warning messages
3530 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003531 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 *
3533 * The variables xp->xp_context and xp->xp_backslash must have been set!
3534 */
3535 char_u *
3536ExpandOne(xp, str, orig, options, mode)
3537 expand_T *xp;
3538 char_u *str;
3539 char_u *orig; /* allocated copy of original of expanded string */
3540 int options;
3541 int mode;
3542{
3543 char_u *ss = NULL;
3544 static int findex;
3545 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003546 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 int i;
3548 long_u len;
3549 int non_suf_match; /* number without matching suffix */
3550
3551 /*
3552 * first handle the case of using an old match
3553 */
3554 if (mode == WILD_NEXT || mode == WILD_PREV)
3555 {
3556 if (xp->xp_numfiles > 0)
3557 {
3558 if (mode == WILD_PREV)
3559 {
3560 if (findex == -1)
3561 findex = xp->xp_numfiles;
3562 --findex;
3563 }
3564 else /* mode == WILD_NEXT */
3565 ++findex;
3566
3567 /*
3568 * When wrapping around, return the original string, set findex to
3569 * -1.
3570 */
3571 if (findex < 0)
3572 {
3573 if (orig_save == NULL)
3574 findex = xp->xp_numfiles - 1;
3575 else
3576 findex = -1;
3577 }
3578 if (findex >= xp->xp_numfiles)
3579 {
3580 if (orig_save == NULL)
3581 findex = 0;
3582 else
3583 findex = -1;
3584 }
3585#ifdef FEAT_WILDMENU
3586 if (p_wmnu)
3587 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3588 findex, cmd_showtail);
3589#endif
3590 if (findex == -1)
3591 return vim_strsave(orig_save);
3592 return vim_strsave(xp->xp_files[findex]);
3593 }
3594 else
3595 return NULL;
3596 }
3597
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003598 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3600 {
3601 FreeWild(xp->xp_numfiles, xp->xp_files);
3602 xp->xp_numfiles = -1;
3603 vim_free(orig_save);
3604 orig_save = NULL;
3605 }
3606 findex = 0;
3607
3608 if (mode == WILD_FREE) /* only release file name */
3609 return NULL;
3610
3611 if (xp->xp_numfiles == -1)
3612 {
3613 vim_free(orig_save);
3614 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003615 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 /*
3618 * Do the expansion.
3619 */
3620 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3621 options) == FAIL)
3622 {
3623#ifdef FNAME_ILLEGAL
3624 /* Illegal file name has been silently skipped. But when there
3625 * are wildcards, the real problem is that there was no match,
3626 * causing the pattern to be added, which has illegal characters.
3627 */
3628 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3629 EMSG2(_(e_nomatch2), str);
3630#endif
3631 }
3632 else if (xp->xp_numfiles == 0)
3633 {
3634 if (!(options & WILD_SILENT))
3635 EMSG2(_(e_nomatch2), str);
3636 }
3637 else
3638 {
3639 /* Escape the matches for use on the command line. */
3640 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3641
3642 /*
3643 * Check for matching suffixes in file names.
3644 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003645 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3646 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
3648 if (xp->xp_numfiles)
3649 non_suf_match = xp->xp_numfiles;
3650 else
3651 non_suf_match = 1;
3652 if ((xp->xp_context == EXPAND_FILES
3653 || xp->xp_context == EXPAND_DIRECTORIES)
3654 && xp->xp_numfiles > 1)
3655 {
3656 /*
3657 * More than one match; check suffix.
3658 * The files will have been sorted on matching suffix in
3659 * expand_wildcards, only need to check the first two.
3660 */
3661 non_suf_match = 0;
3662 for (i = 0; i < 2; ++i)
3663 if (match_suffix(xp->xp_files[i]))
3664 ++non_suf_match;
3665 }
3666 if (non_suf_match != 1)
3667 {
3668 /* Can we ever get here unless it's while expanding
3669 * interactively? If not, we can get rid of this all
3670 * together. Don't really want to wait for this message
3671 * (and possibly have to hit return to continue!).
3672 */
3673 if (!(options & WILD_SILENT))
3674 EMSG(_(e_toomany));
3675 else if (!(options & WILD_NO_BEEP))
3676 beep_flush();
3677 }
3678 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3679 ss = vim_strsave(xp->xp_files[0]);
3680 }
3681 }
3682 }
3683
3684 /* Find longest common part */
3685 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3686 {
3687 for (len = 0; xp->xp_files[0][len]; ++len)
3688 {
3689 for (i = 0; i < xp->xp_numfiles; ++i)
3690 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003691 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003693 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003694 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
3696 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3697 TOLOWER_LOC(xp->xp_files[0][len]))
3698 break;
3699 }
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003700 else if (xp->xp_files[i][len] != xp->xp_files[0][len])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 break;
3702 }
3703 if (i < xp->xp_numfiles)
3704 {
3705 if (!(options & WILD_NO_BEEP))
3706 vim_beep();
3707 break;
3708 }
3709 }
3710 ss = alloc((unsigned)len + 1);
3711 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003712 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 findex = -1; /* next p_wc gets first one */
3714 }
3715
3716 /* Concatenate all matching names */
3717 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3718 {
3719 len = 0;
3720 for (i = 0; i < xp->xp_numfiles; ++i)
3721 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3722 ss = lalloc(len, TRUE);
3723 if (ss != NULL)
3724 {
3725 *ss = NUL;
3726 for (i = 0; i < xp->xp_numfiles; ++i)
3727 {
3728 STRCAT(ss, xp->xp_files[i]);
3729 if (i != xp->xp_numfiles - 1)
3730 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3731 }
3732 }
3733 }
3734
3735 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3736 ExpandCleanup(xp);
3737
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003738 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003739 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003740 vim_free(orig);
3741
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return ss;
3743}
3744
3745/*
3746 * Prepare an expand structure for use.
3747 */
3748 void
3749ExpandInit(xp)
3750 expand_T *xp;
3751{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003752 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003753 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003755#ifndef BACKSLASH_IN_FILENAME
3756 xp->xp_shell = FALSE;
3757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 xp->xp_numfiles = -1;
3759 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003760#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3761 xp->xp_arg = NULL;
3762#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003763 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764}
3765
3766/*
3767 * Cleanup an expand structure after use.
3768 */
3769 void
3770ExpandCleanup(xp)
3771 expand_T *xp;
3772{
3773 if (xp->xp_numfiles >= 0)
3774 {
3775 FreeWild(xp->xp_numfiles, xp->xp_files);
3776 xp->xp_numfiles = -1;
3777 }
3778}
3779
3780 void
3781ExpandEscape(xp, str, numfiles, files, options)
3782 expand_T *xp;
3783 char_u *str;
3784 int numfiles;
3785 char_u **files;
3786 int options;
3787{
3788 int i;
3789 char_u *p;
3790
3791 /*
3792 * May change home directory back to "~"
3793 */
3794 if (options & WILD_HOME_REPLACE)
3795 tilde_replace(str, numfiles, files);
3796
3797 if (options & WILD_ESCAPE)
3798 {
3799 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003800 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003801 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 || xp->xp_context == EXPAND_BUFFERS
3803 || xp->xp_context == EXPAND_DIRECTORIES)
3804 {
3805 /*
3806 * Insert a backslash into a file name before a space, \, %, #
3807 * and wildmatch characters, except '~'.
3808 */
3809 for (i = 0; i < numfiles; ++i)
3810 {
3811 /* for ":set path=" we need to escape spaces twice */
3812 if (xp->xp_backslash == XP_BS_THREE)
3813 {
3814 p = vim_strsave_escaped(files[i], (char_u *)" ");
3815 if (p != NULL)
3816 {
3817 vim_free(files[i]);
3818 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003819#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 p = vim_strsave_escaped(files[i], (char_u *)" ");
3821 if (p != NULL)
3822 {
3823 vim_free(files[i]);
3824 files[i] = p;
3825 }
3826#endif
3827 }
3828 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003829#ifdef BACKSLASH_IN_FILENAME
3830 p = vim_strsave_fnameescape(files[i], FALSE);
3831#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003832 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 if (p != NULL)
3835 {
3836 vim_free(files[i]);
3837 files[i] = p;
3838 }
3839
3840 /* If 'str' starts with "\~", replace "~" at start of
3841 * files[i] with "\~". */
3842 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003843 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003846
3847 /* If the first file starts with a '+' escape it. Otherwise it
3848 * could be seen as "+cmd". */
3849 if (*files[0] == '+')
3850 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 }
3852 else if (xp->xp_context == EXPAND_TAGS)
3853 {
3854 /*
3855 * Insert a backslash before characters in a tag name that
3856 * would terminate the ":tag" command.
3857 */
3858 for (i = 0; i < numfiles; ++i)
3859 {
3860 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3861 if (p != NULL)
3862 {
3863 vim_free(files[i]);
3864 files[i] = p;
3865 }
3866 }
3867 }
3868 }
3869}
3870
3871/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003872 * Escape special characters in "fname" for when used as a file name argument
3873 * after a Vim command, or, when "shell" is non-zero, a shell command.
3874 * Returns the result in allocated memory.
3875 */
3876 char_u *
3877vim_strsave_fnameescape(fname, shell)
3878 char_u *fname;
3879 int shell;
3880{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003881 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003882#ifdef BACKSLASH_IN_FILENAME
3883 char_u buf[20];
3884 int j = 0;
3885
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003886 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003887 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003888 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003889 buf[j++] = *p;
3890 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003891 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003892#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003893 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3894 if (shell && csh_like_shell() && p != NULL)
3895 {
3896 char_u *s;
3897
3898 /* For csh and similar shells need to put two backslashes before '!'.
3899 * One is taken by Vim, one by the shell. */
3900 s = vim_strsave_escaped(p, (char_u *)"!");
3901 vim_free(p);
3902 p = s;
3903 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003904#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003905
3906 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3907 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003908 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003909 escape_fname(&p);
3910
3911 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003912}
3913
3914/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003915 * Put a backslash before the file name in "pp", which is in allocated memory.
3916 */
3917 static void
3918escape_fname(pp)
3919 char_u **pp;
3920{
3921 char_u *p;
3922
3923 p = alloc((unsigned)(STRLEN(*pp) + 2));
3924 if (p != NULL)
3925 {
3926 p[0] = '\\';
3927 STRCPY(p + 1, *pp);
3928 vim_free(*pp);
3929 *pp = p;
3930 }
3931}
3932
3933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 * For each file name in files[num_files]:
3935 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3936 */
3937 void
3938tilde_replace(orig_pat, num_files, files)
3939 char_u *orig_pat;
3940 int num_files;
3941 char_u **files;
3942{
3943 int i;
3944 char_u *p;
3945
3946 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3947 {
3948 for (i = 0; i < num_files; ++i)
3949 {
3950 p = home_replace_save(NULL, files[i]);
3951 if (p != NULL)
3952 {
3953 vim_free(files[i]);
3954 files[i] = p;
3955 }
3956 }
3957 }
3958}
3959
3960/*
3961 * Show all matches for completion on the command line.
3962 * Returns EXPAND_NOTHING when the character that triggered expansion should
3963 * be inserted like a normal character.
3964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 static int
3966showmatches(xp, wildmenu)
3967 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003968 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969{
3970#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3971 int num_files;
3972 char_u **files_found;
3973 int i, j, k;
3974 int maxlen;
3975 int lines;
3976 int columns;
3977 char_u *p;
3978 int lastlen;
3979 int attr;
3980 int showtail;
3981
3982 if (xp->xp_numfiles == -1)
3983 {
3984 set_expand_context(xp);
3985 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3986 &num_files, &files_found);
3987 showtail = expand_showtail(xp);
3988 if (i != EXPAND_OK)
3989 return i;
3990
3991 }
3992 else
3993 {
3994 num_files = xp->xp_numfiles;
3995 files_found = xp->xp_files;
3996 showtail = cmd_showtail;
3997 }
3998
3999#ifdef FEAT_WILDMENU
4000 if (!wildmenu)
4001 {
4002#endif
4003 msg_didany = FALSE; /* lines_left will be set */
4004 msg_start(); /* prepare for paging */
4005 msg_putchar('\n');
4006 out_flush();
4007 cmdline_row = msg_row;
4008 msg_didany = FALSE; /* lines_left will be set again */
4009 msg_start(); /* prepare for paging */
4010#ifdef FEAT_WILDMENU
4011 }
4012#endif
4013
4014 if (got_int)
4015 got_int = FALSE; /* only int. the completion, not the cmd line */
4016#ifdef FEAT_WILDMENU
4017 else if (wildmenu)
4018 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
4019#endif
4020 else
4021 {
4022 /* find the length of the longest file name */
4023 maxlen = 0;
4024 for (i = 0; i < num_files; ++i)
4025 {
4026 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004027 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 || xp->xp_context == EXPAND_BUFFERS))
4029 {
4030 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4031 j = vim_strsize(NameBuff);
4032 }
4033 else
4034 j = vim_strsize(L_SHOWFILE(i));
4035 if (j > maxlen)
4036 maxlen = j;
4037 }
4038
4039 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4040 lines = num_files;
4041 else
4042 {
4043 /* compute the number of columns and lines for the listing */
4044 maxlen += 2; /* two spaces between file names */
4045 columns = ((int)Columns + 2) / maxlen;
4046 if (columns < 1)
4047 columns = 1;
4048 lines = (num_files + columns - 1) / columns;
4049 }
4050
4051 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4052
4053 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4054 {
4055 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4056 msg_clr_eos();
4057 msg_advance(maxlen - 3);
4058 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4059 }
4060
4061 /* list the files line by line */
4062 for (i = 0; i < lines; ++i)
4063 {
4064 lastlen = 999;
4065 for (k = i; k < num_files; k += lines)
4066 {
4067 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4068 {
4069 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4070 p = files_found[k] + STRLEN(files_found[k]) + 1;
4071 msg_advance(maxlen + 1);
4072 msg_puts(p);
4073 msg_advance(maxlen + 3);
4074 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4075 break;
4076 }
4077 for (j = maxlen - lastlen; --j >= 0; )
4078 msg_putchar(' ');
4079 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004080 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 || xp->xp_context == EXPAND_BUFFERS)
4082 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004083 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004084 if (xp->xp_numfiles != -1)
4085 {
4086 char_u *halved_slash;
4087 char_u *exp_path;
4088
4089 /* Expansion was done before and special characters
4090 * were escaped, need to halve backslashes. Also
4091 * $HOME has been replaced with ~/. */
4092 exp_path = expand_env_save_opt(files_found[k], TRUE);
4093 halved_slash = backslash_halve_save(
4094 exp_path != NULL ? exp_path : files_found[k]);
4095 j = mch_isdir(halved_slash != NULL ? halved_slash
4096 : files_found[k]);
4097 vim_free(exp_path);
4098 vim_free(halved_slash);
4099 }
4100 else
4101 /* Expansion was done here, file names are literal. */
4102 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if (showtail)
4104 p = L_SHOWFILE(k);
4105 else
4106 {
4107 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4108 TRUE);
4109 p = NameBuff;
4110 }
4111 }
4112 else
4113 {
4114 j = FALSE;
4115 p = L_SHOWFILE(k);
4116 }
4117 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4118 }
4119 if (msg_col > 0) /* when not wrapped around */
4120 {
4121 msg_clr_eos();
4122 msg_putchar('\n');
4123 }
4124 out_flush(); /* show one line at a time */
4125 if (got_int)
4126 {
4127 got_int = FALSE;
4128 break;
4129 }
4130 }
4131
4132 /*
4133 * we redraw the command below the lines that we have just listed
4134 * This is a bit tricky, but it saves a lot of screen updating.
4135 */
4136 cmdline_row = msg_row; /* will put it back later */
4137 }
4138
4139 if (xp->xp_numfiles == -1)
4140 FreeWild(num_files, files_found);
4141
4142 return EXPAND_OK;
4143}
4144
4145/*
4146 * Private gettail for showmatches() (and win_redr_status_matches()):
4147 * Find tail of file name path, but ignore trailing "/".
4148 */
4149 char_u *
4150sm_gettail(s)
4151 char_u *s;
4152{
4153 char_u *p;
4154 char_u *t = s;
4155 int had_sep = FALSE;
4156
4157 for (p = s; *p != NUL; )
4158 {
4159 if (vim_ispathsep(*p)
4160#ifdef BACKSLASH_IN_FILENAME
4161 && !rem_backslash(p)
4162#endif
4163 )
4164 had_sep = TRUE;
4165 else if (had_sep)
4166 {
4167 t = p;
4168 had_sep = FALSE;
4169 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004170 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172 return t;
4173}
4174
4175/*
4176 * Return TRUE if we only need to show the tail of completion matches.
4177 * When not completing file names or there is a wildcard in the path FALSE is
4178 * returned.
4179 */
4180 static int
4181expand_showtail(xp)
4182 expand_T *xp;
4183{
4184 char_u *s;
4185 char_u *end;
4186
4187 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004188 if (xp->xp_context != EXPAND_FILES
4189 && xp->xp_context != EXPAND_SHELLCMD
4190 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FALSE;
4192
4193 end = gettail(xp->xp_pattern);
4194 if (end == xp->xp_pattern) /* there is no path separator */
4195 return FALSE;
4196
4197 for (s = xp->xp_pattern; s < end; s++)
4198 {
4199 /* Skip escaped wildcards. Only when the backslash is not a path
4200 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4201 if (rem_backslash(s))
4202 ++s;
4203 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4204 return FALSE;
4205 }
4206 return TRUE;
4207}
4208
4209/*
4210 * Prepare a string for expansion.
4211 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004212 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 * When expanding other names: The string will be used with regcomp(). Copy
4214 * the name into allocated memory and prepend "^".
4215 */
4216 char_u *
4217addstar(fname, len, context)
4218 char_u *fname;
4219 int len;
4220 int context; /* EXPAND_FILES etc. */
4221{
4222 char_u *retval;
4223 int i, j;
4224 int new_len;
4225 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004226 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004228 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004229 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004230 && context != EXPAND_SHELLCMD
4231 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 {
4233 /*
4234 * Matching will be done internally (on something other than files).
4235 * So we convert the file-matching-type wildcards into our kind for
4236 * use with vim_regcomp(). First work out how long it will be:
4237 */
4238
4239 /* For help tags the translation is done in find_help_tags().
4240 * For a tag pattern starting with "/" no translation is needed. */
4241 if (context == EXPAND_HELP
4242 || context == EXPAND_COLORS
4243 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004244 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004245 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 || (context == EXPAND_TAGS && fname[0] == '/'))
4247 retval = vim_strnsave(fname, len);
4248 else
4249 {
4250 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4251 for (i = 0; i < len; i++)
4252 {
4253 if (fname[i] == '*' || fname[i] == '~')
4254 new_len++; /* '*' needs to be replaced by ".*"
4255 '~' needs to be replaced by "\~" */
4256
4257 /* Buffer names are like file names. "." should be literal */
4258 if (context == EXPAND_BUFFERS && fname[i] == '.')
4259 new_len++; /* "." becomes "\." */
4260
4261 /* Custom expansion takes care of special things, match
4262 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004263 if ((context == EXPAND_USER_DEFINED
4264 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 new_len++; /* '\' becomes "\\" */
4266 }
4267 retval = alloc(new_len);
4268 if (retval != NULL)
4269 {
4270 retval[0] = '^';
4271 j = 1;
4272 for (i = 0; i < len; i++, j++)
4273 {
4274 /* Skip backslash. But why? At least keep it for custom
4275 * expansion. */
4276 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004277 && context != EXPAND_USER_LIST
4278 && fname[i] == '\\'
4279 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 break;
4281
4282 switch (fname[i])
4283 {
4284 case '*': retval[j++] = '.';
4285 break;
4286 case '~': retval[j++] = '\\';
4287 break;
4288 case '?': retval[j] = '.';
4289 continue;
4290 case '.': if (context == EXPAND_BUFFERS)
4291 retval[j++] = '\\';
4292 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004293 case '\\': if (context == EXPAND_USER_DEFINED
4294 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 retval[j++] = '\\';
4296 break;
4297 }
4298 retval[j] = fname[i];
4299 }
4300 retval[j] = NUL;
4301 }
4302 }
4303 }
4304 else
4305 {
4306 retval = alloc(len + 4);
4307 if (retval != NULL)
4308 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004309 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004312 * Don't add a star to *, ~, ~user, $var or `cmd`.
4313 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 * ~ would be at the start of the file name, but not the tail.
4315 * $ could be anywhere in the tail.
4316 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004317 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 */
4319 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004320 ends_in_star = (len > 0 && retval[len - 1] == '*');
4321#ifndef BACKSLASH_IN_FILENAME
4322 for (i = len - 2; i >= 0; --i)
4323 {
4324 if (retval[i] != '\\')
4325 break;
4326 ends_in_star = !ends_in_star;
4327 }
4328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004330 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 && vim_strchr(tail, '$') == NULL
4332 && vim_strchr(retval, '`') == NULL)
4333 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004334 else if (len > 0 && retval[len - 1] == '$')
4335 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 retval[len] = NUL;
4337 }
4338 }
4339 return retval;
4340}
4341
4342/*
4343 * Must parse the command line so far to work out what context we are in.
4344 * Completion can then be done based on that context.
4345 * This routine sets the variables:
4346 * xp->xp_pattern The start of the pattern to be expanded within
4347 * the command line (ends at the cursor).
4348 * xp->xp_context The type of thing to expand. Will be one of:
4349 *
4350 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4351 * the command line, like an unknown command. Caller
4352 * should beep.
4353 * EXPAND_NOTHING Unrecognised context for completion, use char like
4354 * a normal char, rather than for completion. eg
4355 * :s/^I/
4356 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4357 * it.
4358 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4359 * EXPAND_FILES After command with XFILE set, or after setting
4360 * with P_EXPAND set. eg :e ^I, :w>>^I
4361 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4362 * when we know only directories are of interest. eg
4363 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004364 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4366 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4367 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4368 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4369 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4370 * EXPAND_EVENTS Complete event names
4371 * EXPAND_SYNTAX Complete :syntax command arguments
4372 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4373 * EXPAND_AUGROUP Complete autocommand group names
4374 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4375 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4376 * eg :unmap a^I , :cunab x^I
4377 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4378 * eg :call sub^I
4379 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4380 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4381 * names in expressions, eg :while s^I
4382 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004383 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 */
4385 static void
4386set_expand_context(xp)
4387 expand_T *xp;
4388{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004389 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 if (ccline.cmdfirstc != ':'
4391#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004392 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004393 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394#endif
4395 )
4396 {
4397 xp->xp_context = EXPAND_NOTHING;
4398 return;
4399 }
4400 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4401}
4402
4403 void
4404set_cmd_context(xp, str, len, col)
4405 expand_T *xp;
4406 char_u *str; /* start of command line */
4407 int len; /* length of command line (excl. NUL) */
4408 int col; /* position of cursor */
4409{
4410 int old_char = NUL;
4411 char_u *nextcomm;
4412
4413 /*
4414 * Avoid a UMR warning from Purify, only save the character if it has been
4415 * written before.
4416 */
4417 if (col < len)
4418 old_char = str[col];
4419 str[col] = NUL;
4420 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004421
4422#ifdef FEAT_EVAL
4423 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004424 {
4425# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004426 /* pass CMD_SIZE because there is no real command */
4427 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004428# endif
4429 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004430 else if (ccline.input_fn)
4431 {
4432 xp->xp_context = ccline.xp_context;
4433 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004434# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004435 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004436# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004437 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004438 else
4439#endif
4440 while (nextcomm != NULL)
4441 nextcomm = set_one_cmd_context(xp, nextcomm);
4442
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004443 /* Store the string here so that call_user_expand_func() can get to them
4444 * easily. */
4445 xp->xp_line = str;
4446 xp->xp_col = col;
4447
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 str[col] = old_char;
4449}
4450
4451/*
4452 * Expand the command line "str" from context "xp".
4453 * "xp" must have been set by set_cmd_context().
4454 * xp->xp_pattern points into "str", to where the text that is to be expanded
4455 * starts.
4456 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4457 * cursor.
4458 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4459 * key that triggered expansion literally.
4460 * Returns EXPAND_OK otherwise.
4461 */
4462 int
4463expand_cmdline(xp, str, col, matchcount, matches)
4464 expand_T *xp;
4465 char_u *str; /* start of command line */
4466 int col; /* position of cursor */
4467 int *matchcount; /* return: nr of matches */
4468 char_u ***matches; /* return: array of pointers to matches */
4469{
4470 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004471 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
4473 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4474 {
4475 beep_flush();
4476 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4477 }
4478 if (xp->xp_context == EXPAND_NOTHING)
4479 {
4480 /* Caller can use the character as a normal char instead */
4481 return EXPAND_NOTHING;
4482 }
4483
4484 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004485 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4486 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (file_str == NULL)
4488 return EXPAND_UNSUCCESSFUL;
4489
Bram Moolenaar94950a92010-12-02 16:01:29 +01004490 if (p_wic)
4491 options += WILD_ICASE;
4492
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004494 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 {
4496 *matchcount = 0;
4497 *matches = NULL;
4498 }
4499 vim_free(file_str);
4500
4501 return EXPAND_OK;
4502}
4503
4504#ifdef FEAT_MULTI_LANG
4505/*
4506 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4507 */
4508static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4509
4510 static void
4511cleanup_help_tags(num_file, file)
4512 int num_file;
4513 char_u **file;
4514{
4515 int i, j;
4516 int len;
4517
4518 for (i = 0; i < num_file; ++i)
4519 {
4520 len = (int)STRLEN(file[i]) - 3;
4521 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4522 {
4523 /* Sorting on priority means the same item in another language may
4524 * be anywhere. Search all items for a match up to the "@en". */
4525 for (j = 0; j < num_file; ++j)
4526 if (j != i
4527 && (int)STRLEN(file[j]) == len + 3
4528 && STRNCMP(file[i], file[j], len + 1) == 0)
4529 break;
4530 if (j == num_file)
4531 file[i][len] = NUL;
4532 }
4533 }
4534}
4535#endif
4536
4537/*
4538 * Do the expansion based on xp->xp_context and "pat".
4539 */
4540 static int
4541ExpandFromContext(xp, pat, num_file, file, options)
4542 expand_T *xp;
4543 char_u *pat;
4544 int *num_file;
4545 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004546 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547{
4548#ifdef FEAT_CMDL_COMPL
4549 regmatch_T regmatch;
4550#endif
4551 int ret;
4552 int flags;
4553
4554 flags = EW_DIR; /* include directories */
4555 if (options & WILD_LIST_NOTFOUND)
4556 flags |= EW_NOTFOUND;
4557 if (options & WILD_ADD_SLASH)
4558 flags |= EW_ADDSLASH;
4559 if (options & WILD_KEEP_ALL)
4560 flags |= EW_KEEPALL;
4561 if (options & WILD_SILENT)
4562 flags |= EW_SILENT;
4563
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004564 if (xp->xp_context == EXPAND_FILES
4565 || xp->xp_context == EXPAND_DIRECTORIES
4566 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 {
4568 /*
4569 * Expand file or directory names.
4570 */
4571 int free_pat = FALSE;
4572 int i;
4573
4574 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4575 * space */
4576 if (xp->xp_backslash != XP_BS_NONE)
4577 {
4578 free_pat = TRUE;
4579 pat = vim_strsave(pat);
4580 for (i = 0; pat[i]; ++i)
4581 if (pat[i] == '\\')
4582 {
4583 if (xp->xp_backslash == XP_BS_THREE
4584 && pat[i + 1] == '\\'
4585 && pat[i + 2] == '\\'
4586 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004587 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (xp->xp_backslash == XP_BS_ONE
4589 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004590 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592 }
4593
4594 if (xp->xp_context == EXPAND_FILES)
4595 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004596 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4597 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 else
4599 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004600 if (options & WILD_ICASE)
4601 flags |= EW_ICASE;
4602
Bram Moolenaard7834d32009-12-02 16:14:36 +00004603 /* Expand wildcards, supporting %:h and the like. */
4604 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (free_pat)
4606 vim_free(pat);
4607 return ret;
4608 }
4609
4610 *file = (char_u **)"";
4611 *num_file = 0;
4612 if (xp->xp_context == EXPAND_HELP)
4613 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004614 /* With an empty argument we would get all the help tags, which is
4615 * very slow. Get matches for "help" instead. */
4616 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4617 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 {
4619#ifdef FEAT_MULTI_LANG
4620 cleanup_help_tags(*num_file, *file);
4621#endif
4622 return OK;
4623 }
4624 return FAIL;
4625 }
4626
4627#ifndef FEAT_CMDL_COMPL
4628 return FAIL;
4629#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004630 if (xp->xp_context == EXPAND_SHELLCMD)
4631 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 if (xp->xp_context == EXPAND_OLD_SETTING)
4633 return ExpandOldSetting(num_file, file);
4634 if (xp->xp_context == EXPAND_BUFFERS)
4635 return ExpandBufnames(pat, num_file, file, options);
4636 if (xp->xp_context == EXPAND_TAGS
4637 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4638 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4639 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004640 {
4641 char *directories[] = {"colors", NULL};
4642 return ExpandRTDir(pat, num_file, file, directories);
4643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004645 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004646 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004647 return ExpandRTDir(pat, num_file, file, directories);
4648 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004649 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004650 {
4651 char *directories[] = {"syntax", NULL};
4652 return ExpandRTDir(pat, num_file, file, directories);
4653 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004654 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004655 {
4656 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4657 return ExpandRTDir(pat, num_file, file, directories);
4658 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004659# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4660 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004661 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004662# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4665 if (regmatch.regprog == NULL)
4666 return FAIL;
4667
4668 /* set ignore-case according to p_ic, p_scs and pat */
4669 regmatch.rm_ic = ignorecase(pat);
4670
4671 if (xp->xp_context == EXPAND_SETTINGS
4672 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4673 ret = ExpandSettings(xp, &regmatch, num_file, file);
4674 else if (xp->xp_context == EXPAND_MAPPINGS)
4675 ret = ExpandMappings(&regmatch, num_file, file);
4676# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4677 else if (xp->xp_context == EXPAND_USER_DEFINED)
4678 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4679# endif
4680 else
4681 {
4682 static struct expgen
4683 {
4684 int context;
4685 char_u *((*func)__ARGS((expand_T *, int)));
4686 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004687 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 } tab[] =
4689 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004690 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4691 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004692#ifdef FEAT_CMDHIST
4693 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004696 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4697 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4698 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4699 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700#endif
4701#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004702 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4703 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4704 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4705 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706#endif
4707#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004708 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4709 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710#endif
4711#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004712 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004714#ifdef FEAT_PROFILE
4715 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4716#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004717 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004719 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4720 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004722#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004723 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004724#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004725#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004726 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004727#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004728#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004729 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4732 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004733 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4734 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004736 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004737 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 };
4739 int i;
4740
4741 /*
4742 * Find a context in the table and call the ExpandGeneric() with the
4743 * right function to do the expansion.
4744 */
4745 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004746 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if (xp->xp_context == tab[i].context)
4748 {
4749 if (tab[i].ic)
4750 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004751 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004752 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 break;
4754 }
4755 }
4756
Bram Moolenaar473de612013-06-08 18:19:48 +02004757 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
4759 return ret;
4760#endif /* FEAT_CMDL_COMPL */
4761}
4762
4763#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4764/*
4765 * Expand a list of names.
4766 *
4767 * Generic function for command line completion. It calls a function to
4768 * obtain strings, one by one. The strings are matched against a regexp
4769 * program. Matching strings are copied into an array, which is returned.
4770 *
4771 * Returns OK when no problems encountered, FAIL for error (out of memory).
4772 */
4773 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004774ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 expand_T *xp;
4776 regmatch_T *regmatch;
4777 int *num_file;
4778 char_u ***file;
4779 char_u *((*func)__ARGS((expand_T *, int)));
4780 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004781 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783 int i;
4784 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004785 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 char_u *str;
4787
4788 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004789 * round == 0: count the number of matching names
4790 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004792 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
4794 for (i = 0; ; ++i)
4795 {
4796 str = (*func)(xp, i);
4797 if (str == NULL) /* end of list */
4798 break;
4799 if (*str == NUL) /* skip empty strings */
4800 continue;
4801
4802 if (vim_regexec(regmatch, str, (colnr_T)0))
4803 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004804 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004806 if (escaped)
4807 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4808 else
4809 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 (*file)[count] = str;
4811#ifdef FEAT_MENU
4812 if (func == get_menu_names && str != NULL)
4813 {
4814 /* test for separator added by get_menu_names() */
4815 str += STRLEN(str) - 1;
4816 if (*str == '\001')
4817 *str = '.';
4818 }
4819#endif
4820 }
4821 ++count;
4822 }
4823 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004824 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 {
4826 if (count == 0)
4827 return OK;
4828 *num_file = count;
4829 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4830 if (*file == NULL)
4831 {
4832 *file = (char_u **)"";
4833 return FAIL;
4834 }
4835 count = 0;
4836 }
4837 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004838
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004839 /* Sort the results. Keep menu's in the specified order. */
4840 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004841 {
4842 if (xp->xp_context == EXPAND_EXPRESSION
4843 || xp->xp_context == EXPAND_FUNCTIONS
4844 || xp->xp_context == EXPAND_USER_FUNC)
4845 /* <SNR> functions should be sorted to the end. */
4846 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4847 sort_func_compare);
4848 else
4849 sort_strings(*file, *num_file);
4850 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004851
Bram Moolenaar4f688582007-07-24 12:34:30 +00004852#ifdef FEAT_CMDL_COMPL
4853 /* Reset the variables used for special highlight names expansion, so that
4854 * they don't show up when getting normal highlight names by ID. */
4855 reset_expand_highlight();
4856#endif
4857
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 return OK;
4859}
4860
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004861/*
4862 * Complete a shell command.
4863 * Returns FAIL or OK;
4864 */
4865 static int
4866expand_shellcmd(filepat, num_file, file, flagsarg)
4867 char_u *filepat; /* pattern to match with command names */
4868 int *num_file; /* return: number of matches */
4869 char_u ***file; /* return: array with matches */
4870 int flagsarg; /* EW_ flags */
4871{
4872 char_u *pat;
4873 int i;
4874 char_u *path;
4875 int mustfree = FALSE;
4876 garray_T ga;
4877 char_u *buf = alloc(MAXPATHL);
4878 size_t l;
4879 char_u *s, *e;
4880 int flags = flagsarg;
4881 int ret;
4882
4883 if (buf == NULL)
4884 return FAIL;
4885
4886 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4887 * space */
4888 pat = vim_strsave(filepat);
4889 for (i = 0; pat[i]; ++i)
4890 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004891 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004892
4893 flags |= EW_FILE | EW_EXEC;
4894
4895 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004896 if (mch_isFullName(pat))
4897 path = (char_u *)" ";
4898 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004899 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4900 path = (char_u *)".";
4901 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004902 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004903 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004904 if (path == NULL)
4905 path = (char_u *)"";
4906 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004907
4908 /*
4909 * Go over all directories in $PATH. Expand matches in that directory and
4910 * collect them in "ga".
4911 */
4912 ga_init2(&ga, (int)sizeof(char *), 10);
4913 for (s = path; *s != NUL; s = e)
4914 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004915 if (*s == ' ')
4916 ++s; /* Skip space used for absolute path name. */
4917
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004918#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4919 e = vim_strchr(s, ';');
4920#else
4921 e = vim_strchr(s, ':');
4922#endif
4923 if (e == NULL)
4924 e = s + STRLEN(s);
4925
4926 l = e - s;
4927 if (l > MAXPATHL - 5)
4928 break;
4929 vim_strncpy(buf, s, l);
4930 add_pathsep(buf);
4931 l = STRLEN(buf);
4932 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4933
4934 /* Expand matches in one directory of $PATH. */
4935 ret = expand_wildcards(1, &buf, num_file, file, flags);
4936 if (ret == OK)
4937 {
4938 if (ga_grow(&ga, *num_file) == FAIL)
4939 FreeWild(*num_file, *file);
4940 else
4941 {
4942 for (i = 0; i < *num_file; ++i)
4943 {
4944 s = (*file)[i];
4945 if (STRLEN(s) > l)
4946 {
4947 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004948 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004949 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4950 }
4951 else
4952 vim_free(s);
4953 }
4954 vim_free(*file);
4955 }
4956 }
4957 if (*e != NUL)
4958 ++e;
4959 }
4960 *file = ga.ga_data;
4961 *num_file = ga.ga_len;
4962
4963 vim_free(buf);
4964 vim_free(pat);
4965 if (mustfree)
4966 vim_free(path);
4967 return OK;
4968}
4969
4970
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004972static void * call_user_expand_func __ARGS((void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int)), expand_T *xp, int *num_file, char_u ***file));
4973
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004975 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004976 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004978 static void *
4979call_user_expand_func(user_expand_func, xp, num_file, file)
4980 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 int *num_file;
4983 char_u ***file;
4984{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02004985 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004986 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004989 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004990 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
Bram Moolenaarb7515462013-06-29 12:58:33 +02004992 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004993 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 *num_file = 0;
4995 *file = NULL;
4996
Bram Moolenaarb7515462013-06-29 12:58:33 +02004997 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00004998 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00004999 keep = ccline.cmdbuff[ccline.cmdlen];
5000 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005001 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005002
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005003 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005004 args[1] = xp->xp_line;
5005 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 args[2] = num;
5007
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005008 /* Save the cmdline, we don't know what the function may do. */
5009 save_ccline = ccline;
5010 ccline.cmdbuff = NULL;
5011 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005013
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005014 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005015
5016 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005018 if (ccline.cmdbuff != NULL)
5019 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005020
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005021 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005022 return ret;
5023}
5024
5025/*
5026 * Expand names with a function defined by the user.
5027 */
5028 static int
5029ExpandUserDefined(xp, regmatch, num_file, file)
5030 expand_T *xp;
5031 regmatch_T *regmatch;
5032 int *num_file;
5033 char_u ***file;
5034{
5035 char_u *retstr;
5036 char_u *s;
5037 char_u *e;
5038 char_u keep;
5039 garray_T ga;
5040
5041 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5042 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 return FAIL;
5044
5045 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005046 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 e = vim_strchr(s, '\n');
5049 if (e == NULL)
5050 e = s + STRLEN(s);
5051 keep = *e;
5052 *e = 0;
5053
5054 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5055 {
5056 *e = keep;
5057 if (*e != NUL)
5058 ++e;
5059 continue;
5060 }
5061
5062 if (ga_grow(&ga, 1) == FAIL)
5063 break;
5064
5065 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5066 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
5068 *e = keep;
5069 if (*e != NUL)
5070 ++e;
5071 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005072 vim_free(retstr);
5073 *file = ga.ga_data;
5074 *num_file = ga.ga_len;
5075 return OK;
5076}
5077
5078/*
5079 * Expand names with a list returned by a function defined by the user.
5080 */
5081 static int
5082ExpandUserList(xp, num_file, file)
5083 expand_T *xp;
5084 int *num_file;
5085 char_u ***file;
5086{
5087 list_T *retlist;
5088 listitem_T *li;
5089 garray_T ga;
5090
5091 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5092 if (retlist == NULL)
5093 return FAIL;
5094
5095 ga_init2(&ga, (int)sizeof(char *), 3);
5096 /* Loop over the items in the list. */
5097 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5098 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005099 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5100 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005101
5102 if (ga_grow(&ga, 1) == FAIL)
5103 break;
5104
5105 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005106 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005107 ++ga.ga_len;
5108 }
5109 list_unref(retlist);
5110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 *file = ga.ga_data;
5112 *num_file = ga.ga_len;
5113 return OK;
5114}
5115#endif
5116
5117/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005118 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005119 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005120 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 */
5122 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005123ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 char_u *pat;
5125 int *num_file;
5126 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005127 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 char_u *s;
5130 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005131 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005133 int i;
5134 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
5136 *num_file = 0;
5137 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005138 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005139 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005141 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005143 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5144 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005146 ga_clear_strings(&ga);
5147 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005149 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005150 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005151 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005153
5154 for (i = 0; i < ga.ga_len; ++i)
5155 {
5156 match = ((char_u **)ga.ga_data)[i];
5157 s = match;
5158 e = s + STRLEN(s);
5159 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5160 {
5161 e -= 4;
5162 for (s = e; s > match; mb_ptr_back(match, s))
5163 if (s < match || vim_ispathsep(*s))
5164 break;
5165 ++s;
5166 *e = NUL;
5167 mch_memmove(match, s, e - s + 1);
5168 }
5169 }
5170
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005171 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005172 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005173
5174 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005175 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005176 remove_duplicates(&ga);
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 *file = ga.ga_data;
5179 *num_file = ga.ga_len;
5180 return OK;
5181}
5182
5183#endif
5184
5185#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5186/*
5187 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005188 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005190 void
5191globpath(path, file, ga, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 char_u *path;
5193 char_u *file;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005194 garray_T *ga;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005195 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196{
5197 expand_T xpc;
5198 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 int num_p;
5201 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203 buf = alloc(MAXPATHL);
5204 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005205 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005207 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 /* Loop over all entries in {path}. */
5211 while (*path != NUL)
5212 {
5213 /* Copy one item of the path to buf[] and concatenate the file name. */
5214 copy_option_part(&path, buf, MAXPATHL, ",");
5215 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5216 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005217# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005218 /* Using the platform's path separator (\) makes vim incorrectly
5219 * treat it as an escape character, use '/' instead. */
5220 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5221 STRCAT(buf, "/");
5222# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005226 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5227 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005229 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005231 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 for (i = 0; i < num_p; ++i)
5234 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005235 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005236 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005237 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 FreeWild(num_p, p);
5242 }
5243 }
5244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247}
5248
5249#endif
5250
5251#if defined(FEAT_CMDHIST) || defined(PROTO)
5252
5253/*********************************
5254 * Command line history stuff *
5255 *********************************/
5256
5257/*
5258 * Translate a history character to the associated type number.
5259 */
5260 static int
5261hist_char2type(c)
5262 int c;
5263{
5264 if (c == ':')
5265 return HIST_CMD;
5266 if (c == '=')
5267 return HIST_EXPR;
5268 if (c == '@')
5269 return HIST_INPUT;
5270 if (c == '>')
5271 return HIST_DEBUG;
5272 return HIST_SEARCH; /* must be '?' or '/' */
5273}
5274
5275/*
5276 * Table of history names.
5277 * These names are used in :history and various hist...() functions.
5278 * It is sufficient to give the significant prefix of a history name.
5279 */
5280
5281static char *(history_names[]) =
5282{
5283 "cmd",
5284 "search",
5285 "expr",
5286 "input",
5287 "debug",
5288 NULL
5289};
5290
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005291#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5292/*
5293 * Function given to ExpandGeneric() to obtain the possible first
5294 * arguments of the ":history command.
5295 */
5296 static char_u *
5297get_history_arg(xp, idx)
5298 expand_T *xp UNUSED;
5299 int idx;
5300{
5301 static char_u compl[2] = { NUL, NUL };
5302 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005303 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005304 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5305
5306 if (idx < short_names_count)
5307 {
5308 compl[0] = (char_u)short_names[idx];
5309 return compl;
5310 }
5311 if (idx < short_names_count + history_name_count)
5312 return (char_u *)history_names[idx - short_names_count];
5313 if (idx == short_names_count + history_name_count)
5314 return (char_u *)"all";
5315 return NULL;
5316}
5317#endif
5318
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319/*
5320 * init_history() - Initialize the command line history.
5321 * Also used to re-allocate the history when the size changes.
5322 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005323 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324init_history()
5325{
5326 int newlen; /* new length of history table */
5327 histentry_T *temp;
5328 int i;
5329 int j;
5330 int type;
5331
5332 /*
5333 * If size of history table changed, reallocate it
5334 */
5335 newlen = (int)p_hi;
5336 if (newlen != hislen) /* history length changed */
5337 {
5338 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5339 {
5340 if (newlen)
5341 {
5342 temp = (histentry_T *)lalloc(
5343 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5344 if (temp == NULL) /* out of memory! */
5345 {
5346 if (type == 0) /* first one: just keep the old length */
5347 {
5348 newlen = hislen;
5349 break;
5350 }
5351 /* Already changed one table, now we can only have zero
5352 * length for all tables. */
5353 newlen = 0;
5354 type = -1;
5355 continue;
5356 }
5357 }
5358 else
5359 temp = NULL;
5360 if (newlen == 0 || temp != NULL)
5361 {
5362 if (hisidx[type] < 0) /* there are no entries yet */
5363 {
5364 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005365 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367 else if (newlen > hislen) /* array becomes bigger */
5368 {
5369 for (i = 0; i <= hisidx[type]; ++i)
5370 temp[i] = history[type][i];
5371 j = i;
5372 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005373 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 for ( ; j < hislen; ++i, ++j)
5375 temp[i] = history[type][j];
5376 }
5377 else /* array becomes smaller or 0 */
5378 {
5379 j = hisidx[type];
5380 for (i = newlen - 1; ; --i)
5381 {
5382 if (i >= 0) /* copy newest entries */
5383 temp[i] = history[type][j];
5384 else /* remove older entries */
5385 vim_free(history[type][j].hisstr);
5386 if (--j < 0)
5387 j = hislen - 1;
5388 if (j == hisidx[type])
5389 break;
5390 }
5391 hisidx[type] = newlen - 1;
5392 }
5393 vim_free(history[type]);
5394 history[type] = temp;
5395 }
5396 }
5397 hislen = newlen;
5398 }
5399}
5400
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005401 static void
5402clear_hist_entry(hisptr)
5403 histentry_T *hisptr;
5404{
5405 hisptr->hisnum = 0;
5406 hisptr->viminfo = FALSE;
5407 hisptr->hisstr = NULL;
5408}
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410/*
5411 * Check if command line 'str' is already in history.
5412 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5413 */
5414 static int
Bram Moolenaar07219f92013-04-14 23:19:36 +02005415in_history(type, str, move_to_front, sep, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 int type;
5417 char_u *str;
5418 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005419 int sep;
Bram Moolenaar07219f92013-04-14 23:19:36 +02005420 int writing; /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421{
5422 int i;
5423 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005424 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425
5426 if (hisidx[type] < 0)
5427 return FALSE;
5428 i = hisidx[type];
5429 do
5430 {
5431 if (history[type][i].hisstr == NULL)
5432 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005433
5434 /* For search history, check that the separator character matches as
5435 * well. */
5436 p = history[type][i].hisstr;
5437 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005438 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005439 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 {
5441 if (!move_to_front)
5442 return TRUE;
5443 last_i = i;
5444 break;
5445 }
5446 if (--i < 0)
5447 i = hislen - 1;
5448 } while (i != hisidx[type]);
5449
5450 if (last_i >= 0)
5451 {
5452 str = history[type][i].hisstr;
5453 while (i != hisidx[type])
5454 {
5455 if (++i >= hislen)
5456 i = 0;
5457 history[type][last_i] = history[type][i];
5458 last_i = i;
5459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005461 history[type][i].viminfo = FALSE;
5462 history[type][i].hisstr = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 return TRUE;
5464 }
5465 return FALSE;
5466}
5467
5468/*
5469 * Convert history name (from table above) to its HIST_ equivalent.
5470 * When "name" is empty, return "cmd" history.
5471 * Returns -1 for unknown history name.
5472 */
5473 int
5474get_histtype(name)
5475 char_u *name;
5476{
5477 int i;
5478 int len = (int)STRLEN(name);
5479
5480 /* No argument: use current history. */
5481 if (len == 0)
5482 return hist_char2type(ccline.cmdfirstc);
5483
5484 for (i = 0; history_names[i] != NULL; ++i)
5485 if (STRNICMP(name, history_names[i], len) == 0)
5486 return i;
5487
5488 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5489 return hist_char2type(name[0]);
5490
5491 return -1;
5492}
5493
5494static int last_maptick = -1; /* last seen maptick */
5495
5496/*
5497 * Add the given string to the given history. If the string is already in the
5498 * history then it is moved to the front. "histype" may be one of he HIST_
5499 * values.
5500 */
5501 void
5502add_to_history(histype, new_entry, in_map, sep)
5503 int histype;
5504 char_u *new_entry;
5505 int in_map; /* consider maptick when inside a mapping */
5506 int sep; /* separator character used (search hist) */
5507{
5508 histentry_T *hisptr;
5509 int len;
5510
5511 if (hislen == 0) /* no history */
5512 return;
5513
Bram Moolenaara939e432013-11-09 05:30:26 +01005514 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5515 return;
5516
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 /*
5518 * Searches inside the same mapping overwrite each other, so that only
5519 * the last line is kept. Be careful not to remove a line that was moved
5520 * down, only lines that were added.
5521 */
5522 if (histype == HIST_SEARCH && in_map)
5523 {
5524 if (maptick == last_maptick)
5525 {
5526 /* Current line is from the same mapping, remove it */
5527 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5528 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005529 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 --hisnum[histype];
5531 if (--hisidx[HIST_SEARCH] < 0)
5532 hisidx[HIST_SEARCH] = hislen - 1;
5533 }
5534 last_maptick = -1;
5535 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005536 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 if (++hisidx[histype] == hislen)
5539 hisidx[histype] = 0;
5540 hisptr = &history[histype][hisidx[histype]];
5541 vim_free(hisptr->hisstr);
5542
5543 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005544 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5546 if (hisptr->hisstr != NULL)
5547 hisptr->hisstr[len + 1] = sep;
5548
5549 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005550 hisptr->viminfo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 if (histype == HIST_SEARCH && in_map)
5552 last_maptick = maptick;
5553 }
5554}
5555
5556#if defined(FEAT_EVAL) || defined(PROTO)
5557
5558/*
5559 * Get identifier of newest history entry.
5560 * "histype" may be one of the HIST_ values.
5561 */
5562 int
5563get_history_idx(histype)
5564 int histype;
5565{
5566 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5567 || hisidx[histype] < 0)
5568 return -1;
5569
5570 return history[histype][hisidx[histype]].hisnum;
5571}
5572
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005573static struct cmdline_info *get_ccline_ptr __ARGS((void));
5574
5575/*
5576 * Get pointer to the command line info to use. cmdline_paste() may clear
5577 * ccline and put the previous value in prev_ccline.
5578 */
5579 static struct cmdline_info *
5580get_ccline_ptr()
5581{
5582 if ((State & CMDLINE) == 0)
5583 return NULL;
5584 if (ccline.cmdbuff != NULL)
5585 return &ccline;
5586 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5587 return &prev_ccline;
5588 return NULL;
5589}
5590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591/*
5592 * Get the current command line in allocated memory.
5593 * Only works when the command line is being edited.
5594 * Returns NULL when something is wrong.
5595 */
5596 char_u *
5597get_cmdline_str()
5598{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005599 struct cmdline_info *p = get_ccline_ptr();
5600
5601 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005603 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604}
5605
5606/*
5607 * Get the current command line position, counted in bytes.
5608 * Zero is the first position.
5609 * Only works when the command line is being edited.
5610 * Returns -1 when something is wrong.
5611 */
5612 int
5613get_cmdline_pos()
5614{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005615 struct cmdline_info *p = get_ccline_ptr();
5616
5617 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005619 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620}
5621
5622/*
5623 * Set the command line byte position to "pos". Zero is the first position.
5624 * Only works when the command line is being edited.
5625 * Returns 1 when failed, 0 when OK.
5626 */
5627 int
5628set_cmdline_pos(pos)
5629 int pos;
5630{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005631 struct cmdline_info *p = get_ccline_ptr();
5632
5633 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 return 1;
5635
5636 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5637 * changed the command line. */
5638 if (pos < 0)
5639 new_cmdpos = 0;
5640 else
5641 new_cmdpos = pos;
5642 return 0;
5643}
5644
5645/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005646 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005647 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005648 * Only works when the command line is being edited.
5649 * Returns NUL when something is wrong.
5650 */
5651 int
5652get_cmdline_type()
5653{
5654 struct cmdline_info *p = get_ccline_ptr();
5655
5656 if (p == NULL)
5657 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005658 if (p->cmdfirstc == NUL)
5659 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005660 return p->cmdfirstc;
5661}
5662
5663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 * Calculate history index from a number:
5665 * num > 0: seen as identifying number of a history entry
5666 * num < 0: relative position in history wrt newest entry
5667 * "histype" may be one of the HIST_ values.
5668 */
5669 static int
5670calc_hist_idx(histype, num)
5671 int histype;
5672 int num;
5673{
5674 int i;
5675 histentry_T *hist;
5676 int wrapped = FALSE;
5677
5678 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5679 || (i = hisidx[histype]) < 0 || num == 0)
5680 return -1;
5681
5682 hist = history[histype];
5683 if (num > 0)
5684 {
5685 while (hist[i].hisnum > num)
5686 if (--i < 0)
5687 {
5688 if (wrapped)
5689 break;
5690 i += hislen;
5691 wrapped = TRUE;
5692 }
5693 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5694 return i;
5695 }
5696 else if (-num <= hislen)
5697 {
5698 i += num + 1;
5699 if (i < 0)
5700 i += hislen;
5701 if (hist[i].hisstr != NULL)
5702 return i;
5703 }
5704 return -1;
5705}
5706
5707/*
5708 * Get a history entry by its index.
5709 * "histype" may be one of the HIST_ values.
5710 */
5711 char_u *
5712get_history_entry(histype, idx)
5713 int histype;
5714 int idx;
5715{
5716 idx = calc_hist_idx(histype, idx);
5717 if (idx >= 0)
5718 return history[histype][idx].hisstr;
5719 else
5720 return (char_u *)"";
5721}
5722
5723/*
5724 * Clear all entries of a history.
5725 * "histype" may be one of the HIST_ values.
5726 */
5727 int
5728clr_history(histype)
5729 int histype;
5730{
5731 int i;
5732 histentry_T *hisptr;
5733
5734 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5735 {
5736 hisptr = history[histype];
5737 for (i = hislen; i--;)
5738 {
5739 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005740 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
5742 hisidx[histype] = -1; /* mark history as cleared */
5743 hisnum[histype] = 0; /* reset identifier counter */
5744 return OK;
5745 }
5746 return FAIL;
5747}
5748
5749/*
5750 * Remove all entries matching {str} from a history.
5751 * "histype" may be one of the HIST_ values.
5752 */
5753 int
5754del_history_entry(histype, str)
5755 int histype;
5756 char_u *str;
5757{
5758 regmatch_T regmatch;
5759 histentry_T *hisptr;
5760 int idx;
5761 int i;
5762 int last;
5763 int found = FALSE;
5764
5765 regmatch.regprog = NULL;
5766 regmatch.rm_ic = FALSE; /* always match case */
5767 if (hislen != 0
5768 && histype >= 0
5769 && histype < HIST_COUNT
5770 && *str != NUL
5771 && (idx = hisidx[histype]) >= 0
5772 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5773 != NULL)
5774 {
5775 i = last = idx;
5776 do
5777 {
5778 hisptr = &history[histype][i];
5779 if (hisptr->hisstr == NULL)
5780 break;
5781 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5782 {
5783 found = TRUE;
5784 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005785 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 }
5787 else
5788 {
5789 if (i != last)
5790 {
5791 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005792 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 }
5794 if (--last < 0)
5795 last += hislen;
5796 }
5797 if (--i < 0)
5798 i += hislen;
5799 } while (i != idx);
5800 if (history[histype][idx].hisstr == NULL)
5801 hisidx[histype] = -1;
5802 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005803 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 return found;
5805}
5806
5807/*
5808 * Remove an indexed entry from a history.
5809 * "histype" may be one of the HIST_ values.
5810 */
5811 int
5812del_history_idx(histype, idx)
5813 int histype;
5814 int idx;
5815{
5816 int i, j;
5817
5818 i = calc_hist_idx(histype, idx);
5819 if (i < 0)
5820 return FALSE;
5821 idx = hisidx[histype];
5822 vim_free(history[histype][i].hisstr);
5823
5824 /* When deleting the last added search string in a mapping, reset
5825 * last_maptick, so that the last added search string isn't deleted again.
5826 */
5827 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5828 last_maptick = -1;
5829
5830 while (i != idx)
5831 {
5832 j = (i + 1) % hislen;
5833 history[histype][i] = history[histype][j];
5834 i = j;
5835 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005836 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 if (--i < 0)
5838 i += hislen;
5839 hisidx[histype] = i;
5840 return TRUE;
5841}
5842
5843#endif /* FEAT_EVAL */
5844
5845#if defined(FEAT_CRYPT) || defined(PROTO)
5846/*
5847 * Very specific function to remove the value in ":set key=val" from the
5848 * history.
5849 */
5850 void
5851remove_key_from_history()
5852{
5853 char_u *p;
5854 int i;
5855
5856 i = hisidx[HIST_CMD];
5857 if (i < 0)
5858 return;
5859 p = history[HIST_CMD][i].hisstr;
5860 if (p != NULL)
5861 for ( ; *p; ++p)
5862 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5863 {
5864 p = vim_strchr(p + 3, '=');
5865 if (p == NULL)
5866 break;
5867 ++p;
5868 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5869 if (p[i] == '\\' && p[i + 1])
5870 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005871 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 --p;
5873 }
5874}
5875#endif
5876
5877#endif /* FEAT_CMDHIST */
5878
5879#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5880/*
5881 * Get indices "num1,num2" that specify a range within a list (not a range of
5882 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5883 * Returns OK if parsed successfully, otherwise FAIL.
5884 */
5885 int
5886get_list_range(str, num1, num2)
5887 char_u **str;
5888 int *num1;
5889 int *num2;
5890{
5891 int len;
5892 int first = FALSE;
5893 long num;
5894
5895 *str = skipwhite(*str);
5896 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5897 {
5898 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5899 *str += len;
5900 *num1 = (int)num;
5901 first = TRUE;
5902 }
5903 *str = skipwhite(*str);
5904 if (**str == ',') /* parse "to" part of range */
5905 {
5906 *str = skipwhite(*str + 1);
5907 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5908 if (len > 0)
5909 {
5910 *num2 = (int)num;
5911 *str = skipwhite(*str + len);
5912 }
5913 else if (!first) /* no number given at all */
5914 return FAIL;
5915 }
5916 else if (first) /* only one number given */
5917 *num2 = *num1;
5918 return OK;
5919}
5920#endif
5921
5922#if defined(FEAT_CMDHIST) || defined(PROTO)
5923/*
5924 * :history command - print a history
5925 */
5926 void
5927ex_history(eap)
5928 exarg_T *eap;
5929{
5930 histentry_T *hist;
5931 int histype1 = HIST_CMD;
5932 int histype2 = HIST_CMD;
5933 int hisidx1 = 1;
5934 int hisidx2 = -1;
5935 int idx;
5936 int i, j, k;
5937 char_u *end;
5938 char_u *arg = eap->arg;
5939
5940 if (hislen == 0)
5941 {
5942 MSG(_("'history' option is zero"));
5943 return;
5944 }
5945
5946 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5947 {
5948 end = arg;
5949 while (ASCII_ISALPHA(*end)
5950 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5951 end++;
5952 i = *end;
5953 *end = NUL;
5954 histype1 = get_histtype(arg);
5955 if (histype1 == -1)
5956 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005957 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 {
5959 histype1 = 0;
5960 histype2 = HIST_COUNT-1;
5961 }
5962 else
5963 {
5964 *end = i;
5965 EMSG(_(e_trailing));
5966 return;
5967 }
5968 }
5969 else
5970 histype2 = histype1;
5971 *end = i;
5972 }
5973 else
5974 end = arg;
5975 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5976 {
5977 EMSG(_(e_trailing));
5978 return;
5979 }
5980
5981 for (; !got_int && histype1 <= histype2; ++histype1)
5982 {
5983 STRCPY(IObuff, "\n # ");
5984 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5985 MSG_PUTS_TITLE(IObuff);
5986 idx = hisidx[histype1];
5987 hist = history[histype1];
5988 j = hisidx1;
5989 k = hisidx2;
5990 if (j < 0)
5991 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5992 if (k < 0)
5993 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5994 if (idx >= 0 && j <= k)
5995 for (i = idx + 1; !got_int; ++i)
5996 {
5997 if (i == hislen)
5998 i = 0;
5999 if (hist[i].hisstr != NULL
6000 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6001 {
6002 msg_putchar('\n');
6003 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6004 hist[i].hisnum);
6005 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6006 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006007 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 else
6009 STRCAT(IObuff, hist[i].hisstr);
6010 msg_outtrans(IObuff);
6011 out_flush();
6012 }
6013 if (i == idx)
6014 break;
6015 }
6016 }
6017}
6018#endif
6019
6020#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006021/*
6022 * Buffers for history read from a viminfo file. Only valid while reading.
6023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
6025static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
6026static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
6027static int viminfo_add_at_front = FALSE;
6028
6029static int hist_type2char __ARGS((int type, int use_question));
6030
6031/*
6032 * Translate a history type number to the associated character.
6033 */
6034 static int
6035hist_type2char(type, use_question)
6036 int type;
6037 int use_question; /* use '?' instead of '/' */
6038{
6039 if (type == HIST_CMD)
6040 return ':';
6041 if (type == HIST_SEARCH)
6042 {
6043 if (use_question)
6044 return '?';
6045 else
6046 return '/';
6047 }
6048 if (type == HIST_EXPR)
6049 return '=';
6050 return '@';
6051}
6052
6053/*
6054 * Prepare for reading the history from the viminfo file.
6055 * This allocates history arrays to store the read history lines.
6056 */
6057 void
Bram Moolenaar07219f92013-04-14 23:19:36 +02006058prepare_viminfo_history(asklen, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 int asklen;
Bram Moolenaar07219f92013-04-14 23:19:36 +02006060 int writing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
6062 int i;
6063 int num;
6064 int type;
6065 int len;
6066
6067 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006068 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 if (asklen > hislen)
6070 asklen = hislen;
6071
6072 for (type = 0; type < HIST_COUNT; ++type)
6073 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006074 /* Count the number of empty spaces in the history list. Entries read
6075 * from viminfo previously are also considered empty. If there are
6076 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006078 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 num++;
6080 len = asklen;
6081 if (num > len)
6082 len = num;
6083 if (len <= 0)
6084 viminfo_history[type] = NULL;
6085 else
6086 viminfo_history[type] =
6087 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6088 if (viminfo_history[type] == NULL)
6089 len = 0;
6090 viminfo_hislen[type] = len;
6091 viminfo_hisidx[type] = 0;
6092 }
6093}
6094
6095/*
6096 * Accept a line from the viminfo, store it in the history array when it's
6097 * new.
6098 */
6099 int
Bram Moolenaar07219f92013-04-14 23:19:36 +02006100read_viminfo_history(virp, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 vir_T *virp;
Bram Moolenaar07219f92013-04-14 23:19:36 +02006102 int writing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103{
6104 int type;
6105 long_u len;
6106 char_u *val;
6107 char_u *p;
6108
6109 type = hist_char2type(virp->vir_line[0]);
6110 if (viminfo_hisidx[type] < viminfo_hislen[type])
6111 {
6112 val = viminfo_readstring(virp, 1, TRUE);
6113 if (val != NULL && *val != NUL)
6114 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006115 int sep = (*val == ' ' ? NUL : *val);
6116
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006118 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {
6120 /* Need to re-allocate to append the separator byte. */
6121 len = STRLEN(val);
6122 p = lalloc(len + 2, TRUE);
6123 if (p != NULL)
6124 {
6125 if (type == HIST_SEARCH)
6126 {
6127 /* Search entry: Move the separator from the first
6128 * column to after the NUL. */
6129 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006130 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 }
6132 else
6133 {
6134 /* Not a search entry: No separator in the viminfo
6135 * file, add a NUL separator. */
6136 mch_memmove(p, val, (size_t)len + 1);
6137 p[len + 1] = NUL;
6138 }
6139 viminfo_history[type][viminfo_hisidx[type]++] = p;
6140 }
6141 }
6142 }
6143 vim_free(val);
6144 }
6145 return viminfo_readline(virp);
6146}
6147
Bram Moolenaar07219f92013-04-14 23:19:36 +02006148/*
6149 * Finish reading history lines from viminfo. Not used when writing viminfo.
6150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 void
6152finish_viminfo_history()
6153{
6154 int idx;
6155 int i;
6156 int type;
6157
6158 for (type = 0; type < HIST_COUNT; ++type)
6159 {
6160 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006161 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 idx = hisidx[type] + viminfo_hisidx[type];
6163 if (idx >= hislen)
6164 idx -= hislen;
6165 else if (idx < 0)
6166 idx = hislen - 1;
6167 if (viminfo_add_at_front)
6168 hisidx[type] = idx;
6169 else
6170 {
6171 if (hisidx[type] == -1)
6172 hisidx[type] = hislen - 1;
6173 do
6174 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006175 if (history[type][idx].hisstr != NULL
6176 || history[type][idx].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 break;
6178 if (++idx == hislen)
6179 idx = 0;
6180 } while (idx != hisidx[type]);
6181 if (idx != hisidx[type] && --idx < 0)
6182 idx = hislen - 1;
6183 }
6184 for (i = 0; i < viminfo_hisidx[type]; i++)
6185 {
6186 vim_free(history[type][idx].hisstr);
6187 history[type][idx].hisstr = viminfo_history[type][i];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006188 history[type][idx].viminfo = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 if (--idx < 0)
6190 idx = hislen - 1;
6191 }
6192 idx += 1;
6193 idx %= hislen;
6194 for (i = 0; i < viminfo_hisidx[type]; i++)
6195 {
6196 history[type][idx++].hisnum = ++hisnum[type];
6197 idx %= hislen;
6198 }
6199 vim_free(viminfo_history[type]);
6200 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006201 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 }
6203}
6204
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006205/*
6206 * Write history to viminfo file in "fp".
6207 * When "merge" is TRUE merge history lines with a previously read viminfo
6208 * file, data is in viminfo_history[].
6209 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 void
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006212write_viminfo_history(fp, merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 FILE *fp;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006214 int merge;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
6216 int i;
6217 int type;
6218 int num_saved;
6219 char_u *p;
6220 int c;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006221 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
6223 init_history();
6224 if (hislen == 0)
6225 return;
6226 for (type = 0; type < HIST_COUNT; ++type)
6227 {
6228 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6229 if (num_saved == 0)
6230 continue;
6231 if (num_saved < 0) /* Use default */
6232 num_saved = hislen;
6233 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6234 type == HIST_CMD ? _("Command Line") :
6235 type == HIST_SEARCH ? _("Search String") :
6236 type == HIST_EXPR ? _("Expression") :
6237 _("Input Line"));
6238 if (num_saved > hislen)
6239 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006240
6241 /*
6242 * Merge typed and viminfo history:
6243 * round 1: history of typed commands.
6244 * round 2: history from recently read viminfo.
6245 */
6246 for (round = 1; round <= 2; ++round)
6247 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006248 if (round == 1)
6249 /* start at newest entry, somewhere in the list */
6250 i = hisidx[type];
6251 else if (viminfo_hisidx[type] > 0)
6252 /* start at newest entry, first in the list */
6253 i = 0;
6254 else
6255 /* empty list */
6256 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006257 if (i >= 0)
6258 while (num_saved > 0
6259 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006261 p = round == 1 ? history[type][i].hisstr
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006262 : viminfo_history[type] == NULL ? NULL
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006263 : viminfo_history[type][i];
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006264 if (p != NULL && (round == 2
6265 || !merge
6266 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006268 --num_saved;
6269 fputc(hist_type2char(type, TRUE), fp);
6270 /* For the search history: put the separator in the
6271 * second column; use a space if there isn't one. */
6272 if (type == HIST_SEARCH)
6273 {
6274 c = p[STRLEN(p) + 1];
6275 putc(c == NUL ? ' ' : c, fp);
6276 }
6277 viminfo_writestring(fp, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006279 if (round == 1)
6280 {
6281 /* Decrement index, loop around and stop when back at
6282 * the start. */
6283 if (--i < 0)
6284 i = hislen - 1;
6285 if (i == hisidx[type])
6286 break;
6287 }
6288 else
6289 {
6290 /* Increment index. Stop at the end in the while. */
6291 ++i;
6292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006294 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006295 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006296 if (viminfo_history[type] != NULL)
6297 vim_free(viminfo_history[type][i]);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006298 vim_free(viminfo_history[type]);
6299 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006300 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 }
6302}
6303#endif /* FEAT_VIMINFO */
6304
6305#if defined(FEAT_FKMAP) || defined(PROTO)
6306/*
6307 * Write a character at the current cursor+offset position.
6308 * It is directly written into the command buffer block.
6309 */
6310 void
6311cmd_pchar(c, offset)
6312 int c, offset;
6313{
6314 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6315 {
6316 EMSG(_("E198: cmd_pchar beyond the command length"));
6317 return;
6318 }
6319 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6320 ccline.cmdbuff[ccline.cmdlen] = NUL;
6321}
6322
6323 int
6324cmd_gchar(offset)
6325 int offset;
6326{
6327 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6328 {
6329 /* EMSG(_("cmd_gchar beyond the command length")); */
6330 return NUL;
6331 }
6332 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6333}
6334#endif
6335
6336#if defined(FEAT_CMDWIN) || defined(PROTO)
6337/*
6338 * Open a window on the current command line and history. Allow editing in
6339 * the window. Returns when the window is closed.
6340 * Returns:
6341 * CR if the command is to be executed
6342 * Ctrl_C if it is to be abandoned
6343 * K_IGNORE if editing continues
6344 */
6345 static int
6346ex_window()
6347{
6348 struct cmdline_info save_ccline;
6349 buf_T *old_curbuf = curbuf;
6350 win_T *old_curwin = curwin;
6351 buf_T *bp;
6352 win_T *wp;
6353 int i;
6354 linenr_T lnum;
6355 int histtype;
6356 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006357#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 int save_restart_edit = restart_edit;
6361 int save_State = State;
6362 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006363#ifdef FEAT_RIGHTLEFT
6364 int save_cmdmsg_rl = cmdmsg_rl;
6365#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006366#ifdef FEAT_FOLDING
6367 int save_KeyTyped;
6368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
6370 /* Can't do this recursively. Can't do it when typing a password. */
6371 if (cmdwin_type != 0
6372# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6373 || cmdline_star > 0
6374# endif
6375 )
6376 {
6377 beep_flush();
6378 return K_IGNORE;
6379 }
6380
6381 /* Save current window sizes. */
6382 win_size_save(&winsizes);
6383
6384# ifdef FEAT_AUTOCMD
6385 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006386 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006388 /* don't use a new tab page */
6389 cmdmod.tab = 0;
6390
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 /* Create a window for the command-line buffer. */
6392 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6393 {
6394 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006395# ifdef FEAT_AUTOCMD
6396 unblock_autocmds();
6397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 return K_IGNORE;
6399 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006400 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006403 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006404 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6406 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6407 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006408#ifdef FEAT_FOLDING
6409 curwin->w_p_fen = FALSE;
6410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006412 curwin->w_p_rl = cmdmsg_rl;
6413 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006415 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416
6417# ifdef FEAT_AUTOCMD
6418 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006419 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420# endif
6421
Bram Moolenaar46152342005-09-07 21:18:43 +00006422 /* Showing the prompt may have set need_wait_return, reset it. */
6423 need_wait_return = FALSE;
6424
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006425 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6427 {
6428 if (p_wc == TAB)
6429 {
6430 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6431 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6432 }
6433 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6434 }
6435
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006436 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6437 * sets 'textwidth' to 78). */
6438 curbuf->b_p_tw = 0;
6439
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 /* Fill the buffer with the history. */
6441 init_history();
6442 if (hislen > 0)
6443 {
6444 i = hisidx[histtype];
6445 if (i >= 0)
6446 {
6447 lnum = 0;
6448 do
6449 {
6450 if (++i == hislen)
6451 i = 0;
6452 if (history[histtype][i].hisstr != NULL)
6453 ml_append(lnum++, history[histtype][i].hisstr,
6454 (colnr_T)0, FALSE);
6455 }
6456 while (i != hisidx[histtype]);
6457 }
6458 }
6459
6460 /* Replace the empty last line with the current command-line and put the
6461 * cursor there. */
6462 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6463 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6464 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006465 changed_line_abv_curs();
6466 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006467 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468
6469 /* Save the command line info, can be used recursively. */
6470 save_ccline = ccline;
6471 ccline.cmdbuff = NULL;
6472 ccline.cmdprompt = NULL;
6473
6474 /* No Ex mode here! */
6475 exmode_active = 0;
6476
6477 State = NORMAL;
6478# ifdef FEAT_MOUSE
6479 setmouse();
6480# endif
6481
6482# ifdef FEAT_AUTOCMD
6483 /* Trigger CmdwinEnter autocommands. */
6484 typestr[0] = cmdwin_type;
6485 typestr[1] = NUL;
6486 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006487 if (restart_edit != 0) /* autocmd with ":startinsert" */
6488 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489# endif
6490
6491 i = RedrawingDisabled;
6492 RedrawingDisabled = 0;
6493
6494 /*
6495 * Call the main loop until <CR> or CTRL-C is typed.
6496 */
6497 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006498 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
6500 RedrawingDisabled = i;
6501
6502# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006503
6504# ifdef FEAT_FOLDING
6505 save_KeyTyped = KeyTyped;
6506# endif
6507
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 /* Trigger CmdwinLeave autocommands. */
6509 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006510
6511# ifdef FEAT_FOLDING
6512 /* Restore KeyTyped in case it is modified by autocommands */
6513 KeyTyped = save_KeyTyped;
6514# endif
6515
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516# endif
6517
Bram Moolenaarccc18222007-05-10 18:25:20 +00006518 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 ccline = save_ccline;
6520 cmdwin_type = 0;
6521
6522 exmode_active = save_exmode;
6523
Bram Moolenaarf9821062008-06-20 16:31:07 +00006524 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 * this happens! */
6526 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6527 {
6528 cmdwin_result = Ctrl_C;
6529 EMSG(_("E199: Active window or buffer deleted"));
6530 }
6531 else
6532 {
6533# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6534 /* autocmds may abort script processing */
6535 if (aborting() && cmdwin_result != K_IGNORE)
6536 cmdwin_result = Ctrl_C;
6537# endif
6538 /* Set the new command line from the cmdline buffer. */
6539 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006540 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006542 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6543
6544 if (histtype == HIST_CMD)
6545 {
6546 /* Execute the command directly. */
6547 ccline.cmdbuff = vim_strsave((char_u *)p);
6548 cmdwin_result = CAR;
6549 }
6550 else
6551 {
6552 /* First need to cancel what we were doing. */
6553 ccline.cmdbuff = NULL;
6554 stuffcharReadbuff(':');
6555 stuffReadbuff((char_u *)p);
6556 stuffcharReadbuff(CAR);
6557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 }
6559 else if (cmdwin_result == K_XF2) /* :qa typed */
6560 {
6561 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6562 cmdwin_result = CAR;
6563 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006564 else if (cmdwin_result == Ctrl_C)
6565 {
6566 /* :q or :close, don't execute any command
6567 * and don't modify the cmd window. */
6568 ccline.cmdbuff = NULL;
6569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 else
6571 ccline.cmdbuff = vim_strsave(ml_get_curline());
6572 if (ccline.cmdbuff == NULL)
6573 cmdwin_result = Ctrl_C;
6574 else
6575 {
6576 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6577 ccline.cmdbufflen = ccline.cmdlen + 1;
6578 ccline.cmdpos = curwin->w_cursor.col;
6579 if (ccline.cmdpos > ccline.cmdlen)
6580 ccline.cmdpos = ccline.cmdlen;
6581 if (cmdwin_result == K_IGNORE)
6582 {
6583 set_cmdspos_cursor();
6584 redrawcmd();
6585 }
6586 }
6587
6588# ifdef FEAT_AUTOCMD
6589 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006590 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591# endif
6592 wp = curwin;
6593 bp = curbuf;
6594 win_goto(old_curwin);
6595 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006596
6597 /* win_close() may have already wiped the buffer when 'bh' is
6598 * set to 'wipe' */
6599 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006600 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601
6602 /* Restore window sizes. */
6603 win_size_restore(&winsizes);
6604
6605# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006606 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607# endif
6608 }
6609
6610 ga_clear(&winsizes);
6611 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006612# ifdef FEAT_RIGHTLEFT
6613 cmdmsg_rl = save_cmdmsg_rl;
6614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615
6616 State = save_State;
6617# ifdef FEAT_MOUSE
6618 setmouse();
6619# endif
6620
6621 return cmdwin_result;
6622}
6623#endif /* FEAT_CMDWIN */
6624
6625/*
6626 * Used for commands that either take a simple command string argument, or:
6627 * cmd << endmarker
6628 * {script}
6629 * endmarker
6630 * Returns a pointer to allocated memory with {script} or NULL.
6631 */
6632 char_u *
6633script_get(eap, cmd)
6634 exarg_T *eap;
6635 char_u *cmd;
6636{
6637 char_u *theline;
6638 char *end_pattern = NULL;
6639 char dot[] = ".";
6640 garray_T ga;
6641
6642 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6643 return NULL;
6644
6645 ga_init2(&ga, 1, 0x400);
6646
6647 if (cmd[2] != NUL)
6648 end_pattern = (char *)skipwhite(cmd + 2);
6649 else
6650 end_pattern = dot;
6651
6652 for (;;)
6653 {
6654 theline = eap->getline(
6655#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006656 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657#endif
6658 NUL, eap->cookie, 0);
6659
6660 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006661 {
6662 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
6666 ga_concat(&ga, theline);
6667 ga_append(&ga, '\n');
6668 vim_free(theline);
6669 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006670 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671
6672 return (char_u *)ga.ga_data;
6673}