blob: 906eb8bb0c72b11b6e1944d2eab0eefd016b828b [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
345 /*
346 * Collect the command string, handling editing keys.
347 */
348 for (;;)
349 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000350 redir_off = TRUE; /* Don't redirect the typed command.
351 Repeated, because a ":redir" inside
352 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#ifdef USE_ON_FLY_SCROLL
354 dont_scroll = FALSE; /* allow scrolling here */
355#endif
356 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
357
358 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000359
360 /* Get a character. Ignore K_IGNORE, it should not do anything, such
361 * as stop completion. */
362 do
363 {
364 c = safe_vgetc();
365 } while (c == K_IGNORE);
366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 if (KeyTyped)
368 {
369 some_key_typed = TRUE;
370#ifdef FEAT_RIGHTLEFT
371 if (cmd_hkmap)
372 c = hkmap(c);
373# ifdef FEAT_FKMAP
374 if (cmd_fkmap)
375 c = cmdl_fkmap(c);
376# endif
377 if (cmdmsg_rl && !KeyStuffed)
378 {
379 /* Invert horizontal movements and operations. Only when
380 * typed by the user directly, not when the result of a
381 * mapping. */
382 switch (c)
383 {
384 case K_RIGHT: c = K_LEFT; break;
385 case K_S_RIGHT: c = K_S_LEFT; break;
386 case K_C_RIGHT: c = K_C_LEFT; break;
387 case K_LEFT: c = K_RIGHT; break;
388 case K_S_LEFT: c = K_S_RIGHT; break;
389 case K_C_LEFT: c = K_C_RIGHT; break;
390 }
391 }
392#endif
393 }
394
395 /*
396 * Ignore got_int when CTRL-C was typed here.
397 * Don't ignore it in :global, we really need to break then, e.g., for
398 * ":g/pat/normal /pat" (without the <CR>).
399 * Don't ignore it for the input() function.
400 */
401 if ((c == Ctrl_C
402#ifdef UNIX
403 || c == intr_char
404#endif
405 )
406#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
407 && firstc != '@'
408#endif
409#ifdef FEAT_EVAL
410 && !break_ctrl_c
411#endif
412 && !global_busy)
413 got_int = FALSE;
414
415#ifdef FEAT_CMDHIST
416 /* free old command line when finished moving around in the history
417 * list */
418 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000419 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000420 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 && c != K_PAGEDOWN && c != K_PAGEUP
422 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000423 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
425 {
426 vim_free(lookfor);
427 lookfor = NULL;
428 }
429#endif
430
431 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000432 * When there are matching completions to select <S-Tab> works like
433 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000435 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 c = Ctrl_P;
437
438#ifdef FEAT_WILDMENU
439 /* Special translations for 'wildmenu' */
440 if (did_wild_list && p_wmnu)
441 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000442 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000444 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 c = Ctrl_N;
446 }
447 /* Hitting CR after "emenu Name.": complete submenu */
448 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
449 && ccline.cmdpos > 1
450 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
451 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
452 && (c == '\n' || c == '\r' || c == K_KENTER))
453 c = K_DOWN;
454#endif
455
456 /* free expanded names when finished walking through matches */
457 if (xpc.xp_numfiles != -1
458 && !(c == p_wc && KeyTyped) && c != p_wcm
459 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
460 && c != Ctrl_L)
461 {
462 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
463 did_wild_list = FALSE;
464#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000465 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467 xpc.xp_context = EXPAND_NOTHING;
468 wim_index = 0;
469#ifdef FEAT_WILDMENU
470 if (p_wmnu && wild_menu_showing != 0)
471 {
472 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000473 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000474
475 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000476 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
478 if (wild_menu_showing == WM_SCROLLED)
479 {
480 /* Entered command line, move it up */
481 cmdline_row--;
482 redrawcmd();
483 }
484 else if (save_p_ls != -1)
485 {
486 /* restore 'laststatus' and 'winminheight' */
487 p_ls = save_p_ls;
488 p_wmh = save_p_wmh;
489 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000490 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000492 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 redrawcmd();
494 save_p_ls = -1;
495 }
496 else
497 {
498# ifdef FEAT_VERTSPLIT
499 win_redraw_last_status(topframe);
500# else
501 lastwin->w_redr_status = TRUE;
502# endif
503 redraw_statuslines();
504 }
505 KeyTyped = skt;
506 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000507 if (ccline.input_fn)
508 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 }
510#endif
511 }
512
513#ifdef FEAT_WILDMENU
514 /* Special translations for 'wildmenu' */
515 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
516 {
517 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000518 if (c == K_DOWN && ccline.cmdpos > 0
519 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000521 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
523 /* Hitting <Up>: Remove one submenu name in front of the
524 * cursor */
525 int found = FALSE;
526
527 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
528 i = 0;
529 while (--j > 0)
530 {
531 /* check for start of menu name */
532 if (ccline.cmdbuff[j] == ' '
533 && ccline.cmdbuff[j - 1] != '\\')
534 {
535 i = j + 1;
536 break;
537 }
538 /* check for start of submenu name */
539 if (ccline.cmdbuff[j] == '.'
540 && ccline.cmdbuff[j - 1] != '\\')
541 {
542 if (found)
543 {
544 i = j + 1;
545 break;
546 }
547 else
548 found = TRUE;
549 }
550 }
551 if (i > 0)
552 cmdline_del(i);
553 c = p_wc;
554 xpc.xp_context = EXPAND_NOTHING;
555 }
556 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000557 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000558 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000559 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
561 char_u upseg[5];
562
563 upseg[0] = PATHSEP;
564 upseg[1] = '.';
565 upseg[2] = '.';
566 upseg[3] = PATHSEP;
567 upseg[4] = NUL;
568
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000569 if (c == K_DOWN
570 && ccline.cmdpos > 0
571 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
572 && (ccline.cmdpos < 3
573 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
575 {
576 /* go down a directory */
577 c = p_wc;
578 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000579 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
581 /* If in a direct ancestor, strip off one ../ to go down */
582 int found = FALSE;
583
584 j = ccline.cmdpos;
585 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
586 while (--j > i)
587 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000588#ifdef FEAT_MBYTE
589 if (has_mbyte)
590 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 if (vim_ispathsep(ccline.cmdbuff[j]))
593 {
594 found = TRUE;
595 break;
596 }
597 }
598 if (found
599 && ccline.cmdbuff[j - 1] == '.'
600 && ccline.cmdbuff[j - 2] == '.'
601 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
602 {
603 cmdline_del(j - 2);
604 c = p_wc;
605 }
606 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000607 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {
609 /* go up a directory */
610 int found = FALSE;
611
612 j = ccline.cmdpos - 1;
613 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
614 while (--j > i)
615 {
616#ifdef FEAT_MBYTE
617 if (has_mbyte)
618 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
619#endif
620 if (vim_ispathsep(ccline.cmdbuff[j])
621#ifdef BACKSLASH_IN_FILENAME
622 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
623 == NULL
624#endif
625 )
626 {
627 if (found)
628 {
629 i = j + 1;
630 break;
631 }
632 else
633 found = TRUE;
634 }
635 }
636
637 if (!found)
638 j = i;
639 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
640 j += 4;
641 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
642 && j == i)
643 j += 3;
644 else
645 j = 0;
646 if (j > 0)
647 {
648 /* TODO this is only for DOS/UNIX systems - need to put in
649 * machine-specific stuff here and in upseg init */
650 cmdline_del(j);
651 put_on_cmdline(upseg + 1, 3, FALSE);
652 }
653 else if (ccline.cmdpos > i)
654 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100655
656 /* Now complete in the new directory. Set KeyTyped in case the
657 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100659 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662
663#endif /* FEAT_WILDMENU */
664
665 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
666 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
667 if (c == Ctrl_BSL)
668 {
669 ++no_mapping;
670 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000671 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 --no_mapping;
673 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200674 /* CTRL-\ e doesn't work when obtaining an expression, unless it
675 * is in a mapping. */
676 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
677 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
679 vungetc(c);
680 c = Ctrl_BSL;
681 }
682#ifdef FEAT_EVAL
683 else if (c == 'e')
684 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200685 char_u *p = NULL;
686 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
688 /*
689 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000690 * Need to save and restore the current command line, to be
691 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 */
693 if (ccline.cmdpos == ccline.cmdlen)
694 new_cmdpos = 99999; /* keep it at the end */
695 else
696 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000697
698 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000700 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 if (c == '=')
702 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000703 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000704 * to avoid nasty things like going to another buffer when
705 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000706 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000707 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000709 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000710 restore_cmdline(&save_ccline);
711
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200712 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200714 len = (int)STRLEN(p);
715 if (realloc_cmdbuff(len + 1) == OK)
716 {
717 ccline.cmdlen = len;
718 STRCPY(ccline.cmdbuff, p);
719 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200721 /* Restore the cursor or use the position set with
722 * set_cmdline_pos(). */
723 if (new_cmdpos > ccline.cmdlen)
724 ccline.cmdpos = ccline.cmdlen;
725 else
726 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200728 KeyTyped = FALSE; /* Don't do p_wc completion. */
729 redrawcmd();
730 goto cmdline_changed;
731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 }
733 }
734 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100735 got_int = FALSE; /* don't abandon the command line */
736 did_emsg = FALSE;
737 emsg_on_display = FALSE;
738 redrawcmd();
739 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 }
741#endif
742 else
743 {
744 if (c == Ctrl_G && p_im && restart_edit == 0)
745 restart_edit = 'a';
746 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
747 in history */
748 goto returncmd; /* back to Normal mode */
749 }
750 }
751
752#ifdef FEAT_CMDWIN
753 if (c == cedit_key || c == K_CMDWIN)
754 {
755 /*
756 * Open a window to edit the command line (and history).
757 */
758 c = ex_window();
759 some_key_typed = TRUE;
760 }
761# ifdef FEAT_DIGRAPHS
762 else
763# endif
764#endif
765#ifdef FEAT_DIGRAPHS
766 c = do_digraph(c);
767#endif
768
769 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
770 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
771 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000772 /* In Ex mode a backslash escapes a newline. */
773 if (exmode_active
774 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000775 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000776 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000777 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000779 if (c == K_KENTER)
780 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000782 else
783 {
784 gotesc = FALSE; /* Might have typed ESC previously, don't
785 truncate the cmdline now. */
786 if (ccheck_abbr(c + ABBR_OFF))
787 goto cmdline_changed;
788 if (!cmd_silent)
789 {
790 windgoto(msg_row, 0);
791 out_flush();
792 }
793 break;
794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
796
797 /*
798 * Completion for 'wildchar' or 'wildcharm' key.
799 * - hitting <ESC> twice means: abandon command line.
800 * - wildcard expansion is only done when the 'wildchar' key is really
801 * typed, not when it comes from a macro
802 */
803 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
804 {
805 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
806 {
807 /* if 'wildmode' contains "list" may still need to list */
808 if (xpc.xp_numfiles > 1
809 && !did_wild_list
810 && (wim_flags[wim_index] & WIM_LIST))
811 {
812 (void)showmatches(&xpc, FALSE);
813 redrawcmd();
814 did_wild_list = TRUE;
815 }
816 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100817 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
818 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100820 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
821 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 else
823 res = OK; /* don't insert 'wildchar' now */
824 }
825 else /* typed p_wc first time */
826 {
827 wim_index = 0;
828 j = ccline.cmdpos;
829 /* if 'wildmode' first contains "longest", get longest
830 * common part */
831 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100832 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
833 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100835 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
836 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
838 /* if interrupted while completing, behave like it failed */
839 if (got_int)
840 {
841 (void)vpeekc(); /* remove <C-C> from input stream */
842 got_int = FALSE; /* don't abandon the command line */
843 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
844#ifdef FEAT_WILDMENU
845 xpc.xp_context = EXPAND_NOTHING;
846#endif
847 goto cmdline_changed;
848 }
849
850 /* when more than one match, and 'wildmode' first contains
851 * "list", or no change and 'wildmode' contains "longest,list",
852 * list all matches */
853 if (res == OK && xpc.xp_numfiles > 1)
854 {
855 /* a "longest" that didn't do anything is skipped (but not
856 * "list:longest") */
857 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
858 wim_index = 1;
859 if ((wim_flags[wim_index] & WIM_LIST)
860#ifdef FEAT_WILDMENU
861 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
862#endif
863 )
864 {
865 if (!(wim_flags[0] & WIM_LONGEST))
866 {
867#ifdef FEAT_WILDMENU
868 int p_wmnu_save = p_wmnu;
869 p_wmnu = 0;
870#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100871 /* remove match */
872 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#ifdef FEAT_WILDMENU
874 p_wmnu = p_wmnu_save;
875#endif
876 }
877#ifdef FEAT_WILDMENU
878 (void)showmatches(&xpc, p_wmnu
879 && ((wim_flags[wim_index] & WIM_LIST) == 0));
880#else
881 (void)showmatches(&xpc, FALSE);
882#endif
883 redrawcmd();
884 did_wild_list = TRUE;
885 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100886 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
887 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100889 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
890 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 }
892 else
893 vim_beep();
894 }
895#ifdef FEAT_WILDMENU
896 else if (xpc.xp_numfiles == -1)
897 xpc.xp_context = EXPAND_NOTHING;
898#endif
899 }
900 if (wim_index < 3)
901 ++wim_index;
902 if (c == ESC)
903 gotesc = TRUE;
904 if (res == OK)
905 goto cmdline_changed;
906 }
907
908 gotesc = FALSE;
909
910 /* <S-Tab> goes to last match, in a clumsy way */
911 if (c == K_S_TAB && KeyTyped)
912 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100913 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
914 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
915 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 goto cmdline_changed;
917 }
918
919 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
920 c = NL;
921
922 do_abbr = TRUE; /* default: check for abbreviation */
923
924 /*
925 * Big switch for a typed command line character.
926 */
927 switch (c)
928 {
929 case K_BS:
930 case Ctrl_H:
931 case K_DEL:
932 case K_KDEL:
933 case Ctrl_W:
934#ifdef FEAT_FKMAP
935 if (cmd_fkmap && c == K_BS)
936 c = K_DEL;
937#endif
938 if (c == K_KDEL)
939 c = K_DEL;
940
941 /*
942 * delete current character is the same as backspace on next
943 * character, except at end of line
944 */
945 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
946 ++ccline.cmdpos;
947#ifdef FEAT_MBYTE
948 if (has_mbyte && c == K_DEL)
949 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
950 ccline.cmdbuff + ccline.cmdpos);
951#endif
952 if (ccline.cmdpos > 0)
953 {
954 char_u *p;
955
956 j = ccline.cmdpos;
957 p = ccline.cmdbuff + j;
958#ifdef FEAT_MBYTE
959 if (has_mbyte)
960 {
961 p = mb_prevptr(ccline.cmdbuff, p);
962 if (c == Ctrl_W)
963 {
964 while (p > ccline.cmdbuff && vim_isspace(*p))
965 p = mb_prevptr(ccline.cmdbuff, p);
966 i = mb_get_class(p);
967 while (p > ccline.cmdbuff && mb_get_class(p) == i)
968 p = mb_prevptr(ccline.cmdbuff, p);
969 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000970 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972 }
973 else
974#endif
975 if (c == Ctrl_W)
976 {
977 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
978 --p;
979 i = vim_iswordc(p[-1]);
980 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
981 && vim_iswordc(p[-1]) == i)
982 --p;
983 }
984 else
985 --p;
986 ccline.cmdpos = (int)(p - ccline.cmdbuff);
987 ccline.cmdlen -= j - ccline.cmdpos;
988 i = ccline.cmdpos;
989 while (i < ccline.cmdlen)
990 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
991
992 /* Truncate at the end, required for multi-byte chars. */
993 ccline.cmdbuff[ccline.cmdlen] = NUL;
994 redrawcmd();
995 }
996 else if (ccline.cmdlen == 0 && c != Ctrl_W
997 && ccline.cmdprompt == NULL && indent == 0)
998 {
999 /* In ex and debug mode it doesn't make sense to return. */
1000 if (exmode_active
1001#ifdef FEAT_EVAL
1002 || ccline.cmdfirstc == '>'
1003#endif
1004 )
1005 goto cmdline_not_changed;
1006
1007 vim_free(ccline.cmdbuff); /* no commandline to return */
1008 ccline.cmdbuff = NULL;
1009 if (!cmd_silent)
1010 {
1011#ifdef FEAT_RIGHTLEFT
1012 if (cmdmsg_rl)
1013 msg_col = Columns;
1014 else
1015#endif
1016 msg_col = 0;
1017 msg_putchar(' '); /* delete ':' */
1018 }
1019 redraw_cmdline = TRUE;
1020 goto returncmd; /* back to cmd mode */
1021 }
1022 goto cmdline_changed;
1023
1024 case K_INS:
1025 case K_KINS:
1026#ifdef FEAT_FKMAP
1027 /* if Farsi mode set, we are in reverse insert mode -
1028 Do not change the mode */
1029 if (cmd_fkmap)
1030 beep_flush();
1031 else
1032#endif
1033 ccline.overstrike = !ccline.overstrike;
1034#ifdef CURSOR_SHAPE
1035 ui_cursor_shape(); /* may show different cursor shape */
1036#endif
1037 goto cmdline_not_changed;
1038
1039 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001040 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
1042 /* ":lmap" mappings exists, toggle use of mappings. */
1043 State ^= LANGMAP;
1044#ifdef USE_IM_CONTROL
1045 im_set_active(FALSE); /* Disable input method */
1046#endif
1047 if (b_im_ptr != NULL)
1048 {
1049 if (State & LANGMAP)
1050 *b_im_ptr = B_IMODE_LMAP;
1051 else
1052 *b_im_ptr = B_IMODE_NONE;
1053 }
1054 }
1055#ifdef USE_IM_CONTROL
1056 else
1057 {
1058 /* There are no ":lmap" mappings, toggle IM. When
1059 * 'imdisable' is set don't try getting the status, it's
1060 * always off. */
1061 if ((p_imdisable && b_im_ptr != NULL)
1062 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1063 {
1064 im_set_active(FALSE); /* Disable input method */
1065 if (b_im_ptr != NULL)
1066 *b_im_ptr = B_IMODE_NONE;
1067 }
1068 else
1069 {
1070 im_set_active(TRUE); /* Enable input method */
1071 if (b_im_ptr != NULL)
1072 *b_im_ptr = B_IMODE_IM;
1073 }
1074 }
1075#endif
1076 if (b_im_ptr != NULL)
1077 {
1078 if (b_im_ptr == &curbuf->b_p_iminsert)
1079 set_iminsert_global();
1080 else
1081 set_imsearch_global();
1082 }
1083#ifdef CURSOR_SHAPE
1084 ui_cursor_shape(); /* may show different cursor shape */
1085#endif
1086#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1087 /* Show/unshow value of 'keymap' in status lines later. */
1088 status_redraw_curbuf();
1089#endif
1090 goto cmdline_not_changed;
1091
1092/* case '@': only in very old vi */
1093 case Ctrl_U:
1094 /* delete all characters left of the cursor */
1095 j = ccline.cmdpos;
1096 ccline.cmdlen -= j;
1097 i = ccline.cmdpos = 0;
1098 while (i < ccline.cmdlen)
1099 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1100 /* Truncate at the end, required for multi-byte chars. */
1101 ccline.cmdbuff[ccline.cmdlen] = NUL;
1102 redrawcmd();
1103 goto cmdline_changed;
1104
1105#ifdef FEAT_CLIPBOARD
1106 case Ctrl_Y:
1107 /* Copy the modeless selection, if there is one. */
1108 if (clip_star.state != SELECT_CLEARED)
1109 {
1110 if (clip_star.state == SELECT_DONE)
1111 clip_copy_modeless_selection(TRUE);
1112 goto cmdline_not_changed;
1113 }
1114 break;
1115#endif
1116
1117 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1118 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001119 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001120 * ":normal" runs out of characters. */
1121 if (exmode_active
1122#ifdef FEAT_EX_EXTRA
1123 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1124#endif
1125 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 goto cmdline_not_changed;
1127
1128 gotesc = TRUE; /* will free ccline.cmdbuff after
1129 putting it in history */
1130 goto returncmd; /* back to cmd mode */
1131
1132 case Ctrl_R: /* insert register */
1133#ifdef USE_ON_FLY_SCROLL
1134 dont_scroll = TRUE; /* disallow scrolling here */
1135#endif
1136 putcmdline('"', TRUE);
1137 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001138 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 if (i == Ctrl_O)
1140 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1141 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001142 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 --no_mapping;
1144#ifdef FEAT_EVAL
1145 /*
1146 * Insert the result of an expression.
1147 * Need to save the current command line, to be able to enter
1148 * a new one...
1149 */
1150 new_cmdpos = -1;
1151 if (c == '=')
1152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1154 {
1155 beep_flush();
1156 c = ESC;
1157 }
1158 else
1159 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001160 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001162 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
1164 }
1165#endif
1166 if (c != ESC) /* use ESC to cancel inserting register */
1167 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001168 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001169
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001170#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001171 /* When there was a serious error abort getting the
1172 * command line. */
1173 if (aborting())
1174 {
1175 gotesc = TRUE; /* will free ccline.cmdbuff after
1176 putting it in history */
1177 goto returncmd; /* back to cmd mode */
1178 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 KeyTyped = FALSE; /* Don't do p_wc completion. */
1181#ifdef FEAT_EVAL
1182 if (new_cmdpos >= 0)
1183 {
1184 /* set_cmdline_pos() was used */
1185 if (new_cmdpos > ccline.cmdlen)
1186 ccline.cmdpos = ccline.cmdlen;
1187 else
1188 ccline.cmdpos = new_cmdpos;
1189 }
1190#endif
1191 }
1192 redrawcmd();
1193 goto cmdline_changed;
1194
1195 case Ctrl_D:
1196 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1197 break; /* Use ^D as normal char instead */
1198
1199 redrawcmd();
1200 continue; /* don't do incremental search now */
1201
1202 case K_RIGHT:
1203 case K_S_RIGHT:
1204 case K_C_RIGHT:
1205 do
1206 {
1207 if (ccline.cmdpos >= ccline.cmdlen)
1208 break;
1209 i = cmdline_charsize(ccline.cmdpos);
1210 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1211 break;
1212 ccline.cmdspos += i;
1213#ifdef FEAT_MBYTE
1214 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001215 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 + ccline.cmdpos);
1217 else
1218#endif
1219 ++ccline.cmdpos;
1220 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001221 while ((c == K_S_RIGHT || c == K_C_RIGHT
1222 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1224#ifdef FEAT_MBYTE
1225 if (has_mbyte)
1226 set_cmdspos_cursor();
1227#endif
1228 goto cmdline_not_changed;
1229
1230 case K_LEFT:
1231 case K_S_LEFT:
1232 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001233 if (ccline.cmdpos == 0)
1234 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 do
1236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 --ccline.cmdpos;
1238#ifdef FEAT_MBYTE
1239 if (has_mbyte) /* move to first byte of char */
1240 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1241 ccline.cmdbuff + ccline.cmdpos);
1242#endif
1243 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1244 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001245 while (ccline.cmdpos > 0
1246 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001247 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1249#ifdef FEAT_MBYTE
1250 if (has_mbyte)
1251 set_cmdspos_cursor();
1252#endif
1253 goto cmdline_not_changed;
1254
1255 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001256 /* Ignore mouse event or ex_window() result. */
1257 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
Bram Moolenaar4770d092006-01-12 23:22:24 +00001259#ifdef FEAT_GUI_W32
1260 /* On Win32 ignore <M-F4>, we get it when closing the window was
1261 * cancelled. */
1262 case K_F4:
1263 if (mod_mask == MOD_MASK_ALT)
1264 {
1265 redrawcmd(); /* somehow the cmdline is cleared */
1266 goto cmdline_not_changed;
1267 }
1268 break;
1269#endif
1270
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271#ifdef FEAT_MOUSE
1272 case K_MIDDLEDRAG:
1273 case K_MIDDLERELEASE:
1274 goto cmdline_not_changed; /* Ignore mouse */
1275
1276 case K_MIDDLEMOUSE:
1277# ifdef FEAT_GUI
1278 /* When GUI is active, also paste when 'mouse' is empty */
1279 if (!gui.in_use)
1280# endif
1281 if (!mouse_has(MOUSE_COMMAND))
1282 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001283# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001285 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001287# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001288 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 redrawcmd();
1290 goto cmdline_changed;
1291
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001292# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001294 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 redrawcmd();
1296 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
1299 case K_LEFTDRAG:
1300 case K_LEFTRELEASE:
1301 case K_RIGHTDRAG:
1302 case K_RIGHTRELEASE:
1303 /* Ignore drag and release events when the button-down wasn't
1304 * seen before. */
1305 if (ignore_drag_release)
1306 goto cmdline_not_changed;
1307 /* FALLTHROUGH */
1308 case K_LEFTMOUSE:
1309 case K_RIGHTMOUSE:
1310 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1311 ignore_drag_release = TRUE;
1312 else
1313 ignore_drag_release = FALSE;
1314# ifdef FEAT_GUI
1315 /* When GUI is active, also move when 'mouse' is empty */
1316 if (!gui.in_use)
1317# endif
1318 if (!mouse_has(MOUSE_COMMAND))
1319 goto cmdline_not_changed; /* Ignore mouse */
1320# ifdef FEAT_CLIPBOARD
1321 if (mouse_row < cmdline_row && clip_star.available)
1322 {
1323 int button, is_click, is_drag;
1324
1325 /*
1326 * Handle modeless selection.
1327 */
1328 button = get_mouse_button(KEY2TERMCAP1(c),
1329 &is_click, &is_drag);
1330 if (mouse_model_popup() && button == MOUSE_LEFT
1331 && (mod_mask & MOD_MASK_SHIFT))
1332 {
1333 /* Translate shift-left to right button. */
1334 button = MOUSE_RIGHT;
1335 mod_mask &= ~MOD_MASK_SHIFT;
1336 }
1337 clip_modeless(button, is_click, is_drag);
1338 goto cmdline_not_changed;
1339 }
1340# endif
1341
1342 set_cmdspos();
1343 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1344 ++ccline.cmdpos)
1345 {
1346 i = cmdline_charsize(ccline.cmdpos);
1347 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1348 && mouse_col < ccline.cmdspos % Columns + i)
1349 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001350# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (has_mbyte)
1352 {
1353 /* Count ">" for double-wide char that doesn't fit. */
1354 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001355 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 + ccline.cmdpos) - 1;
1357 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 ccline.cmdspos += i;
1360 }
1361 goto cmdline_not_changed;
1362
1363 /* Mouse scroll wheel: ignored here */
1364 case K_MOUSEDOWN:
1365 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001366 case K_MOUSELEFT:
1367 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 /* Alternate buttons ignored here */
1369 case K_X1MOUSE:
1370 case K_X1DRAG:
1371 case K_X1RELEASE:
1372 case K_X2MOUSE:
1373 case K_X2DRAG:
1374 case K_X2RELEASE:
1375 goto cmdline_not_changed;
1376
1377#endif /* FEAT_MOUSE */
1378
1379#ifdef FEAT_GUI
1380 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1381 case K_LEFTRELEASE_NM:
1382 goto cmdline_not_changed;
1383
1384 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001385 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 {
1387 gui_do_scroll();
1388 redrawcmd();
1389 }
1390 goto cmdline_not_changed;
1391
1392 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001393 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001395 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 redrawcmd();
1397 }
1398 goto cmdline_not_changed;
1399#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001400#ifdef FEAT_GUI_TABLINE
1401 case K_TABLINE:
1402 case K_TABMENU:
1403 /* Don't want to change any tabs here. Make sure the same tab
1404 * is still selected. */
1405 if (gui_use_tabline())
1406 gui_mch_set_curtab(tabpage_index(curtab));
1407 goto cmdline_not_changed;
1408#endif
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 case K_SELECT: /* end of Select mode mapping - ignore */
1411 goto cmdline_not_changed;
1412
1413 case Ctrl_B: /* begin of command line */
1414 case K_HOME:
1415 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 case K_S_HOME:
1417 case K_C_HOME:
1418 ccline.cmdpos = 0;
1419 set_cmdspos();
1420 goto cmdline_not_changed;
1421
1422 case Ctrl_E: /* end of command line */
1423 case K_END:
1424 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 case K_S_END:
1426 case K_C_END:
1427 ccline.cmdpos = ccline.cmdlen;
1428 set_cmdspos_cursor();
1429 goto cmdline_not_changed;
1430
1431 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001432 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 break;
1434 goto cmdline_changed;
1435
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001436 case Ctrl_L:
1437#ifdef FEAT_SEARCH_EXTRA
1438 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1439 {
1440 /* Add a character from under the cursor for 'incsearch' */
1441 if (did_incsearch
1442 && !equalpos(curwin->w_cursor, old_cursor))
1443 {
1444 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001445 /* If 'ignorecase' and 'smartcase' are set and the
1446 * command line has no uppercase characters, convert
1447 * the character to lowercase */
1448 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1449 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001450 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001451 {
1452 if (c == firstc || vim_strchr((char_u *)(
1453 p_magic ? "\\^$.*[" : "\\^$"), c)
1454 != NULL)
1455 {
1456 /* put a backslash before special characters */
1457 stuffcharReadbuff(c);
1458 c = '\\';
1459 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001460 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001461 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001462 }
1463 goto cmdline_not_changed;
1464 }
1465#endif
1466
1467 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001468 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 break;
1470 goto cmdline_changed;
1471
1472 case Ctrl_N: /* next match */
1473 case Ctrl_P: /* previous match */
1474 if (xpc.xp_numfiles > 0)
1475 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001476 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1477 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 break;
1479 goto cmdline_changed;
1480 }
1481
1482#ifdef FEAT_CMDHIST
1483 case K_UP:
1484 case K_DOWN:
1485 case K_S_UP:
1486 case K_S_DOWN:
1487 case K_PAGEUP:
1488 case K_KPAGEUP:
1489 case K_PAGEDOWN:
1490 case K_KPAGEDOWN:
1491 if (hislen == 0 || firstc == NUL) /* no history */
1492 goto cmdline_not_changed;
1493
1494 i = hiscnt;
1495
1496 /* save current command string so it can be restored later */
1497 if (lookfor == NULL)
1498 {
1499 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1500 goto cmdline_not_changed;
1501 lookfor[ccline.cmdpos] = NUL;
1502 }
1503
1504 j = (int)STRLEN(lookfor);
1505 for (;;)
1506 {
1507 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001508 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001509 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 {
1511 if (hiscnt == hislen) /* first time */
1512 hiscnt = hisidx[histype];
1513 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1514 hiscnt = hislen - 1;
1515 else if (hiscnt != hisidx[histype] + 1)
1516 --hiscnt;
1517 else /* at top of list */
1518 {
1519 hiscnt = i;
1520 break;
1521 }
1522 }
1523 else /* one step forwards */
1524 {
1525 /* on last entry, clear the line */
1526 if (hiscnt == hisidx[histype])
1527 {
1528 hiscnt = hislen;
1529 break;
1530 }
1531
1532 /* not on a history line, nothing to do */
1533 if (hiscnt == hislen)
1534 break;
1535 if (hiscnt == hislen - 1) /* wrap around */
1536 hiscnt = 0;
1537 else
1538 ++hiscnt;
1539 }
1540 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1541 {
1542 hiscnt = i;
1543 break;
1544 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001545 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001546 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 || STRNCMP(history[histype][hiscnt].hisstr,
1548 lookfor, (size_t)j) == 0)
1549 break;
1550 }
1551
1552 if (hiscnt != i) /* jumped to other entry */
1553 {
1554 char_u *p;
1555 int len;
1556 int old_firstc;
1557
1558 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001559 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 if (hiscnt == hislen)
1561 p = lookfor; /* back to the old one */
1562 else
1563 p = history[histype][hiscnt].hisstr;
1564
1565 if (histype == HIST_SEARCH
1566 && p != lookfor
1567 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1568 {
1569 /* Correct for the separator character used when
1570 * adding the history entry vs the one used now.
1571 * First loop: count length.
1572 * Second loop: copy the characters. */
1573 for (i = 0; i <= 1; ++i)
1574 {
1575 len = 0;
1576 for (j = 0; p[j] != NUL; ++j)
1577 {
1578 /* Replace old sep with new sep, unless it is
1579 * escaped. */
1580 if (p[j] == old_firstc
1581 && (j == 0 || p[j - 1] != '\\'))
1582 {
1583 if (i > 0)
1584 ccline.cmdbuff[len] = firstc;
1585 }
1586 else
1587 {
1588 /* Escape new sep, unless it is already
1589 * escaped. */
1590 if (p[j] == firstc
1591 && (j == 0 || p[j - 1] != '\\'))
1592 {
1593 if (i > 0)
1594 ccline.cmdbuff[len] = '\\';
1595 ++len;
1596 }
1597 if (i > 0)
1598 ccline.cmdbuff[len] = p[j];
1599 }
1600 ++len;
1601 }
1602 if (i == 0)
1603 {
1604 alloc_cmdbuff(len);
1605 if (ccline.cmdbuff == NULL)
1606 goto returncmd;
1607 }
1608 }
1609 ccline.cmdbuff[len] = NUL;
1610 }
1611 else
1612 {
1613 alloc_cmdbuff((int)STRLEN(p));
1614 if (ccline.cmdbuff == NULL)
1615 goto returncmd;
1616 STRCPY(ccline.cmdbuff, p);
1617 }
1618
1619 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1620 redrawcmd();
1621 goto cmdline_changed;
1622 }
1623 beep_flush();
1624 goto cmdline_not_changed;
1625#endif
1626
1627 case Ctrl_V:
1628 case Ctrl_Q:
1629#ifdef FEAT_MOUSE
1630 ignore_drag_release = TRUE;
1631#endif
1632 putcmdline('^', TRUE);
1633 c = get_literal(); /* get next (two) character(s) */
1634 do_abbr = FALSE; /* don't do abbreviation now */
1635#ifdef FEAT_MBYTE
1636 /* may need to remove ^ when composing char was typed */
1637 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1638 {
1639 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1640 msg_putchar(' ');
1641 cursorcmd();
1642 }
1643#endif
1644 break;
1645
1646#ifdef FEAT_DIGRAPHS
1647 case Ctrl_K:
1648#ifdef FEAT_MOUSE
1649 ignore_drag_release = TRUE;
1650#endif
1651 putcmdline('?', TRUE);
1652#ifdef USE_ON_FLY_SCROLL
1653 dont_scroll = TRUE; /* disallow scrolling here */
1654#endif
1655 c = get_digraph(TRUE);
1656 if (c != NUL)
1657 break;
1658
1659 redrawcmd();
1660 goto cmdline_not_changed;
1661#endif /* FEAT_DIGRAPHS */
1662
1663#ifdef FEAT_RIGHTLEFT
1664 case Ctrl__: /* CTRL-_: switch language mode */
1665 if (!p_ari)
1666 break;
1667#ifdef FEAT_FKMAP
1668 if (p_altkeymap)
1669 {
1670 cmd_fkmap = !cmd_fkmap;
1671 if (cmd_fkmap) /* in Farsi always in Insert mode */
1672 ccline.overstrike = FALSE;
1673 }
1674 else /* Hebrew is default */
1675#endif
1676 cmd_hkmap = !cmd_hkmap;
1677 goto cmdline_not_changed;
1678#endif
1679
1680 default:
1681#ifdef UNIX
1682 if (c == intr_char)
1683 {
1684 gotesc = TRUE; /* will free ccline.cmdbuff after
1685 putting it in history */
1686 goto returncmd; /* back to Normal mode */
1687 }
1688#endif
1689 /*
1690 * Normal character with no special meaning. Just set mod_mask
1691 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1692 * the string <S-Space>. This should only happen after ^V.
1693 */
1694 if (!IS_SPECIAL(c))
1695 mod_mask = 0x0;
1696 break;
1697 }
1698 /*
1699 * End of switch on command line character.
1700 * We come here if we have a normal character.
1701 */
1702
Bram Moolenaarede3e632013-06-23 16:16:19 +02001703 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#ifdef FEAT_MBYTE
1705 /* Add ABBR_OFF for characters above 0x100, this is
1706 * what check_abbr() expects. */
1707 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1708#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001709 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 goto cmdline_changed;
1711
1712 /*
1713 * put the character in the command line
1714 */
1715 if (IS_SPECIAL(c) || mod_mask != 0)
1716 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1717 else
1718 {
1719#ifdef FEAT_MBYTE
1720 if (has_mbyte)
1721 {
1722 j = (*mb_char2bytes)(c, IObuff);
1723 IObuff[j] = NUL; /* exclude composing chars */
1724 put_on_cmdline(IObuff, j, TRUE);
1725 }
1726 else
1727#endif
1728 {
1729 IObuff[0] = c;
1730 put_on_cmdline(IObuff, 1, TRUE);
1731 }
1732 }
1733 goto cmdline_changed;
1734
1735/*
1736 * This part implements incremental searches for "/" and "?"
1737 * Jump to cmdline_not_changed when a character has been read but the command
1738 * line did not change. Then we only search and redraw if something changed in
1739 * the past.
1740 * Jump to cmdline_changed when the command line did change.
1741 * (Sorry for the goto's, I know it is ugly).
1742 */
1743cmdline_not_changed:
1744#ifdef FEAT_SEARCH_EXTRA
1745 if (!incsearch_postponed)
1746 continue;
1747#endif
1748
1749cmdline_changed:
1750#ifdef FEAT_SEARCH_EXTRA
1751 /*
1752 * 'incsearch' highlighting.
1753 */
1754 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1755 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001756 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001757#ifdef FEAT_RELTIME
1758 proftime_T tm;
1759#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 /* if there is a character waiting, search and redraw later */
1762 if (char_avail())
1763 {
1764 incsearch_postponed = TRUE;
1765 continue;
1766 }
1767 incsearch_postponed = FALSE;
1768 curwin->w_cursor = old_cursor; /* start at old position */
1769
1770 /* If there is no command line, don't do anything */
1771 if (ccline.cmdlen == 0)
1772 i = 0;
1773 else
1774 {
1775 cursor_off(); /* so the user knows we're busy */
1776 out_flush();
1777 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001778#ifdef FEAT_RELTIME
1779 /* Set the time limit to half a second. */
1780 profile_setlimit(500L, &tm);
1781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001783 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1784#ifdef FEAT_RELTIME
1785 &tm
1786#else
1787 NULL
1788#endif
1789 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 --emsg_off;
1791 /* if interrupted while searching, behave like it failed */
1792 if (got_int)
1793 {
1794 (void)vpeekc(); /* remove <C-C> from input stream */
1795 got_int = FALSE; /* don't abandon the command line */
1796 i = 0;
1797 }
1798 else if (char_avail())
1799 /* cancelled searching because a char was typed */
1800 incsearch_postponed = TRUE;
1801 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001802 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 highlight_match = TRUE; /* highlight position */
1804 else
1805 highlight_match = FALSE; /* remove highlight */
1806
1807 /* first restore the old curwin values, so the screen is
1808 * positioned in the same way as the actual search command */
1809 curwin->w_leftcol = old_leftcol;
1810 curwin->w_topline = old_topline;
1811# ifdef FEAT_DIFF
1812 curwin->w_topfill = old_topfill;
1813# endif
1814 curwin->w_botline = old_botline;
1815 changed_cline_bef_curs();
1816 update_topline();
1817
1818 if (i != 0)
1819 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001820 pos_T save_pos = curwin->w_cursor;
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001823 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 * moves the whole match onto the screen when 'nowrap' is set.
1825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 curwin->w_cursor.lnum += search_match_lines;
1827 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001828 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1829 {
1830 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1831 coladvance((colnr_T)MAXCOL);
1832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001834 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001835 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001837 else
1838 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001841# ifdef FEAT_WINDOWS
1842 /* May redraw the status line to show the cursor position. */
1843 if (p_ru && curwin->w_status_height > 0)
1844 curwin->w_redr_status = TRUE;
1845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001847 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001848 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001849 restore_cmdline(&save_ccline);
1850
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001851 /* Leave it at the end to make CTRL-R CTRL-W work. */
1852 if (i != 0)
1853 curwin->w_cursor = end_pos;
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 msg_starthere();
1856 redrawcmdline();
1857 did_incsearch = TRUE;
1858 }
1859#else /* FEAT_SEARCH_EXTRA */
1860 ;
1861#endif
1862
1863#ifdef FEAT_RIGHTLEFT
1864 if (cmdmsg_rl
1865# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001866 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867# endif
1868 )
1869 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001870 * right-left typing. Not efficient, but it works.
1871 * Do it only when there are no characters left to read
1872 * to avoid useless intermediate redraws. */
1873 if (vpeekc() == NUL)
1874 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875#endif
1876 }
1877
1878returncmd:
1879
1880#ifdef FEAT_RIGHTLEFT
1881 cmdmsg_rl = FALSE;
1882#endif
1883
1884#ifdef FEAT_FKMAP
1885 cmd_fkmap = 0;
1886#endif
1887
1888 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001889 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891#ifdef FEAT_SEARCH_EXTRA
1892 if (did_incsearch)
1893 {
1894 curwin->w_cursor = old_cursor;
1895 curwin->w_curswant = old_curswant;
1896 curwin->w_leftcol = old_leftcol;
1897 curwin->w_topline = old_topline;
1898# ifdef FEAT_DIFF
1899 curwin->w_topfill = old_topfill;
1900# endif
1901 curwin->w_botline = old_botline;
1902 highlight_match = FALSE;
1903 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001904 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906#endif
1907
1908 if (ccline.cmdbuff != NULL)
1909 {
1910 /*
1911 * Put line in history buffer (":" and "=" only when it was typed).
1912 */
1913#ifdef FEAT_CMDHIST
1914 if (ccline.cmdlen && firstc != NUL
1915 && (some_key_typed || histype == HIST_SEARCH))
1916 {
1917 add_to_history(histype, ccline.cmdbuff, TRUE,
1918 histype == HIST_SEARCH ? firstc : NUL);
1919 if (firstc == ':')
1920 {
1921 vim_free(new_last_cmdline);
1922 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1923 }
1924 }
1925#endif
1926
1927 if (gotesc) /* abandon command line */
1928 {
1929 vim_free(ccline.cmdbuff);
1930 ccline.cmdbuff = NULL;
1931 if (msg_scrolled == 0)
1932 compute_cmdrow();
1933 MSG("");
1934 redraw_cmdline = TRUE;
1935 }
1936 }
1937
1938 /*
1939 * If the screen was shifted up, redraw the whole screen (later).
1940 * If the line is too long, clear it, so ruler and shown command do
1941 * not get printed in the middle of it.
1942 */
1943 msg_check();
1944 msg_scroll = save_msg_scroll;
1945 redir_off = FALSE;
1946
1947 /* When the command line was typed, no need for a wait-return prompt. */
1948 if (some_key_typed)
1949 need_wait_return = FALSE;
1950
1951 State = save_State;
1952#ifdef USE_IM_CONTROL
1953 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1954 im_save_status(b_im_ptr);
1955 im_set_active(FALSE);
1956#endif
1957#ifdef FEAT_MOUSE
1958 setmouse();
1959#endif
1960#ifdef CURSOR_SHAPE
1961 ui_cursor_shape(); /* may show different cursor shape */
1962#endif
1963
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001964 {
1965 char_u *p = ccline.cmdbuff;
1966
1967 /* Make ccline empty, getcmdline() may try to use it. */
1968 ccline.cmdbuff = NULL;
1969 return p;
1970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971}
1972
1973#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1974/*
1975 * Get a command line with a prompt.
1976 * This is prepared to be called recursively from getcmdline() (e.g. by
1977 * f_input() when evaluating an expression from CTRL-R =).
1978 * Returns the command line in allocated memory, or NULL.
1979 */
1980 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001981getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 int firstc;
1983 char_u *prompt; /* command line prompt */
1984 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001985 int xp_context; /* type of expansion */
1986 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
1988 char_u *s;
1989 struct cmdline_info save_ccline;
1990 int msg_col_save = msg_col;
1991
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001992 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 ccline.cmdprompt = prompt;
1994 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001995# ifdef FEAT_EVAL
1996 ccline.xp_context = xp_context;
1997 ccline.xp_arg = xp_arg;
1998 ccline.input_fn = (firstc == '@');
1999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002001 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002002 /* Restore msg_col, the prompt from input() may have changed it.
2003 * But only if called recursively and the commandline is therefore being
2004 * restored to an old one; if not, the input() prompt stays on the screen,
2005 * so we need its modified msg_col left intact. */
2006 if (ccline.cmdbuff != NULL)
2007 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
2009 return s;
2010}
2011#endif
2012
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002013/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002014 * Return TRUE when the text must not be changed and we can't switch to
2015 * another window or buffer. Used when editing the command line, evaluating
2016 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002017 */
2018 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002019text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002020{
2021#ifdef FEAT_CMDWIN
2022 if (cmdwin_type != 0)
2023 return TRUE;
2024#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002025 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002026}
2027
2028/*
2029 * Give an error message for a command that isn't allowed while the cmdline
2030 * window is open or editing the cmdline in another way.
2031 */
2032 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002033text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002034{
2035#ifdef FEAT_CMDWIN
2036 if (cmdwin_type != 0)
2037 EMSG(_(e_cmdwin));
2038 else
2039#endif
2040 EMSG(_(e_secure));
2041}
2042
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002043#if defined(FEAT_AUTOCMD) || defined(PROTO)
2044/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002045 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2046 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002047 */
2048 int
2049curbuf_locked()
2050{
2051 if (curbuf_lock > 0)
2052 {
2053 EMSG(_("E788: Not allowed to edit another buffer now"));
2054 return TRUE;
2055 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002056 return allbuf_locked();
2057}
2058
2059/*
2060 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2061 * message.
2062 */
2063 int
2064allbuf_locked()
2065{
2066 if (allbuf_lock > 0)
2067 {
2068 EMSG(_("E811: Not allowed to change buffer information now"));
2069 return TRUE;
2070 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002071 return FALSE;
2072}
2073#endif
2074
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 static int
2076cmdline_charsize(idx)
2077 int idx;
2078{
2079#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2080 if (cmdline_star > 0) /* showing '*', always 1 position */
2081 return 1;
2082#endif
2083 return ptr2cells(ccline.cmdbuff + idx);
2084}
2085
2086/*
2087 * Compute the offset of the cursor on the command line for the prompt and
2088 * indent.
2089 */
2090 static void
2091set_cmdspos()
2092{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002093 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 ccline.cmdspos = 1 + ccline.cmdindent;
2095 else
2096 ccline.cmdspos = 0 + ccline.cmdindent;
2097}
2098
2099/*
2100 * Compute the screen position for the cursor on the command line.
2101 */
2102 static void
2103set_cmdspos_cursor()
2104{
2105 int i, m, c;
2106
2107 set_cmdspos();
2108 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002111 if (m < 0) /* overflow, Columns or Rows at weird value */
2112 m = MAXCOL;
2113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 else
2115 m = MAXCOL;
2116 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2117 {
2118 c = cmdline_charsize(i);
2119#ifdef FEAT_MBYTE
2120 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2121 if (has_mbyte)
2122 correct_cmdspos(i, c);
2123#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002124 /* If the cmdline doesn't fit, show cursor on last visible char.
2125 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if ((ccline.cmdspos += c) >= m)
2127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 ccline.cmdspos -= c;
2129 break;
2130 }
2131#ifdef FEAT_MBYTE
2132 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002133 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134#endif
2135 }
2136}
2137
2138#ifdef FEAT_MBYTE
2139/*
2140 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2141 * character that doesn't fit, so that a ">" must be displayed.
2142 */
2143 static void
2144correct_cmdspos(idx, cells)
2145 int idx;
2146 int cells;
2147{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002148 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2150 && ccline.cmdspos % Columns + cells > Columns)
2151 ccline.cmdspos++;
2152}
2153#endif
2154
2155/*
2156 * Get an Ex command line for the ":" command.
2157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002159getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002161 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 int indent; /* indent for inside conditionals */
2163{
2164 /* When executing a register, remove ':' that's in front of each line. */
2165 if (exec_from_reg && vpeekc() == ':')
2166 (void)vgetc();
2167 return getcmdline(c, 1L, indent);
2168}
2169
2170/*
2171 * Get an Ex command line for Ex mode.
2172 * In Ex mode we only use the OS supplied line editing features and no
2173 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002174 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002177getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002178 int promptc; /* normally ':', NUL for ":append" and '?' for
2179 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002180 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 int indent; /* indent for inside conditionals */
2182{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002183 garray_T line_ga;
2184 char_u *pend;
2185 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002186 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002187 int escaped = FALSE; /* CTRL-V typed */
2188 int vcol = 0;
2189 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002190 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 /* Switch cursor on now. This avoids that it happens after the "\n", which
2193 * confuses the system function that computes tabstops. */
2194 cursor_on();
2195
2196 /* always start in column 0; write a newline if necessary */
2197 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002198 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002200 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002202 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002203 if (p_prompt)
2204 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 while (indent-- > 0)
2206 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209
2210 ga_init2(&line_ga, 1, 30);
2211
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002212 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002214 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002215 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002216 while (indent >= 8)
2217 {
2218 ga_append(&line_ga, TAB);
2219 msg_puts((char_u *)" ");
2220 indent -= 8;
2221 }
2222 while (indent-- > 0)
2223 {
2224 ga_append(&line_ga, ' ');
2225 msg_putchar(' ');
2226 }
2227 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002228 ++no_mapping;
2229 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 /*
2232 * Get the line, one character at a time.
2233 */
2234 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002235 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 if (ga_grow(&line_ga, 40) == FAIL)
2238 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002240 /* Get one character at a time. Don't use inchar(), it can't handle
2241 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002242 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002243 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
2245 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002246 * Handle line editing.
2247 * Previously this was left to the system, putting the terminal in
2248 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002250 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002252 msg_putchar('\n');
2253 break;
2254 }
2255
2256 if (!escaped)
2257 {
2258 /* CR typed means "enter", which is NL */
2259 if (c1 == '\r')
2260 c1 = '\n';
2261
2262 if (c1 == BS || c1 == K_BS
2263 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002265 if (line_ga.ga_len > 0)
2266 {
2267 --line_ga.ga_len;
2268 goto redraw;
2269 }
2270 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002273 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002275 msg_col = startcol;
2276 msg_clr_eos();
2277 line_ga.ga_len = 0;
2278 continue;
2279 }
2280
2281 if (c1 == Ctrl_T)
2282 {
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01002283 long sw = get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002284
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002285 p = (char_u *)line_ga.ga_data;
2286 p[line_ga.ga_len] = NUL;
2287 indent = get_indent_str(p, 8);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002288 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002289add_indent:
2290 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002292 char_u *s = skipwhite(p);
2293
2294 ga_grow(&line_ga, 1);
2295 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2296 *s = ' ';
2297 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002299redraw:
2300 /* redraw the line */
2301 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002302 vcol = 0;
2303 for (p = (char_u *)line_ga.ga_data;
2304 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002308 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 msg_putchar(' ');
2311 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002315 msg_outtrans_len(p, 1);
2316 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002319 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002320 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002321 continue;
2322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002324 if (c1 == Ctrl_D)
2325 {
2326 /* Delete one shiftwidth. */
2327 p = (char_u *)line_ga.ga_data;
2328 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002330 if (prev_char == '^')
2331 ex_keep_indent = TRUE;
2332 indent = 0;
2333 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335 else
2336 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002337 p[line_ga.ga_len] = NUL;
2338 indent = get_indent_str(p, 8);
2339 --indent;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01002340 indent -= indent % get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342 while (get_indent_str(p, 8) > indent)
2343 {
2344 char_u *s = skipwhite(p);
2345
2346 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2347 --line_ga.ga_len;
2348 }
2349 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002351
2352 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2353 {
2354 escaped = TRUE;
2355 continue;
2356 }
2357
2358 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2359 if (IS_SPECIAL(c1))
2360 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002362
2363 if (IS_SPECIAL(c1))
2364 c1 = '?';
2365 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002366 if (c1 == '\n')
2367 msg_putchar('\n');
2368 else if (c1 == TAB)
2369 {
2370 /* Don't use chartabsize(), 'ts' can be different */
2371 do
2372 {
2373 msg_putchar(' ');
2374 } while (++vcol % 8);
2375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002378 msg_outtrans_len(
2379 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2380 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002382 ++line_ga.ga_len;
2383 escaped = FALSE;
2384
2385 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002386 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002387
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002388 /* We are done when a NL is entered, but not when it comes after an
2389 * odd number of backslashes, that results in a NUL. */
2390 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002392 int bcount = 0;
2393
2394 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2395 ++bcount;
2396
2397 if (bcount > 0)
2398 {
2399 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2400 * "\NL", etc. */
2401 line_ga.ga_len -= (bcount + 1) / 2;
2402 pend -= (bcount + 1) / 2;
2403 pend[-1] = '\n';
2404 }
2405
2406 if ((bcount & 1) == 0)
2407 {
2408 --line_ga.ga_len;
2409 --pend;
2410 *pend = NUL;
2411 break;
2412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
2414 }
2415
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002416 --no_mapping;
2417 --allow_keys;
2418
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 /* make following messages go to the next line */
2420 msg_didout = FALSE;
2421 msg_col = 0;
2422 if (msg_row < Rows - 1)
2423 ++msg_row;
2424 emsg_on_display = FALSE; /* don't want ui_delay() */
2425
2426 if (got_int)
2427 ga_clear(&line_ga);
2428
2429 return (char_u *)line_ga.ga_data;
2430}
2431
Bram Moolenaare344bea2005-09-01 20:46:49 +00002432# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2433 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434/*
2435 * Return TRUE if ccline.overstrike is on.
2436 */
2437 int
2438cmdline_overstrike()
2439{
2440 return ccline.overstrike;
2441}
2442
2443/*
2444 * Return TRUE if the cursor is at the end of the cmdline.
2445 */
2446 int
2447cmdline_at_end()
2448{
2449 return (ccline.cmdpos >= ccline.cmdlen);
2450}
2451#endif
2452
Bram Moolenaar9372a112005-12-06 19:59:18 +00002453#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454/*
2455 * Return the virtual column number at the current cursor position.
2456 * This is used by the IM code to obtain the start of the preedit string.
2457 */
2458 colnr_T
2459cmdline_getvcol_cursor()
2460{
2461 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2462 return MAXCOL;
2463
2464# ifdef FEAT_MBYTE
2465 if (has_mbyte)
2466 {
2467 colnr_T col;
2468 int i = 0;
2469
2470 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002471 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
2473 return col;
2474 }
2475 else
2476# endif
2477 return ccline.cmdpos;
2478}
2479#endif
2480
2481#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2482/*
2483 * If part of the command line is an IM preedit string, redraw it with
2484 * IM feedback attributes. The cursor position is restored after drawing.
2485 */
2486 static void
2487redrawcmd_preedit()
2488{
2489 if ((State & CMDLINE)
2490 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002491 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 && !p_imdisable
2493 && im_is_preediting())
2494 {
2495 int cmdpos = 0;
2496 int cmdspos;
2497 int old_row;
2498 int old_col;
2499 colnr_T col;
2500
2501 old_row = msg_row;
2502 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002503 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505# ifdef FEAT_MBYTE
2506 if (has_mbyte)
2507 {
2508 for (col = 0; col < preedit_start_col
2509 && cmdpos < ccline.cmdlen; ++col)
2510 {
2511 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002512 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514 }
2515 else
2516# endif
2517 {
2518 cmdspos += preedit_start_col;
2519 cmdpos += preedit_start_col;
2520 }
2521
2522 msg_row = cmdline_row + (cmdspos / (int)Columns);
2523 msg_col = cmdspos % (int)Columns;
2524 if (msg_row >= Rows)
2525 msg_row = Rows - 1;
2526
2527 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2528 {
2529 int char_len;
2530 int char_attr;
2531
2532 char_attr = im_get_feedback_attr(col);
2533 if (char_attr < 0)
2534 break; /* end of preedit string */
2535
2536# ifdef FEAT_MBYTE
2537 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002538 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 else
2540# endif
2541 char_len = 1;
2542
2543 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2544 cmdpos += char_len;
2545 }
2546
2547 msg_row = old_row;
2548 msg_col = old_col;
2549 }
2550}
2551#endif /* FEAT_XIM && FEAT_GUI_GTK */
2552
2553/*
2554 * Allocate a new command line buffer.
2555 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2556 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2557 */
2558 static void
2559alloc_cmdbuff(len)
2560 int len;
2561{
2562 /*
2563 * give some extra space to avoid having to allocate all the time
2564 */
2565 if (len < 80)
2566 len = 100;
2567 else
2568 len += 20;
2569
2570 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2571 ccline.cmdbufflen = len;
2572}
2573
2574/*
2575 * Re-allocate the command line to length len + something extra.
2576 * return FAIL for failure, OK otherwise
2577 */
2578 static int
2579realloc_cmdbuff(len)
2580 int len;
2581{
2582 char_u *p;
2583
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002584 if (len < ccline.cmdbufflen)
2585 return OK; /* no need to resize */
2586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 p = ccline.cmdbuff;
2588 alloc_cmdbuff(len); /* will get some more */
2589 if (ccline.cmdbuff == NULL) /* out of memory */
2590 {
2591 ccline.cmdbuff = p; /* keep the old one */
2592 return FAIL;
2593 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002594 /* There isn't always a NUL after the command, but it may need to be
2595 * there, thus copy up to the NUL and add a NUL. */
2596 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2597 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002599
2600 if (ccline.xpc != NULL
2601 && ccline.xpc->xp_pattern != NULL
2602 && ccline.xpc->xp_context != EXPAND_NOTHING
2603 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2604 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002605 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002606
2607 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2608 * to point into the newly allocated memory. */
2609 if (i >= 0 && i <= ccline.cmdlen)
2610 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2611 }
2612
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 return OK;
2614}
2615
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002616#if defined(FEAT_ARABIC) || defined(PROTO)
2617static char_u *arshape_buf = NULL;
2618
2619# if defined(EXITFREE) || defined(PROTO)
2620 void
2621free_cmdline_buf()
2622{
2623 vim_free(arshape_buf);
2624}
2625# endif
2626#endif
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628/*
2629 * Draw part of the cmdline at the current cursor position. But draw stars
2630 * when cmdline_star is TRUE.
2631 */
2632 static void
2633draw_cmdline(start, len)
2634 int start;
2635 int len;
2636{
2637#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2638 int i;
2639
2640 if (cmdline_star > 0)
2641 for (i = 0; i < len; ++i)
2642 {
2643 msg_putchar('*');
2644# ifdef FEAT_MBYTE
2645 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002646 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647# endif
2648 }
2649 else
2650#endif
2651#ifdef FEAT_ARABIC
2652 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2653 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 static int buflen = 0;
2655 char_u *p;
2656 int j;
2657 int newlen = 0;
2658 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002659 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 int prev_c = 0;
2661 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002662 int u8c;
2663 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 /*
2667 * Do arabic shaping into a temporary buffer. This is very
2668 * inefficient!
2669 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002670 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
2672 /* Re-allocate the buffer. We keep it around to avoid a lot of
2673 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002674 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002675 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002676 arshape_buf = alloc(buflen);
2677 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 return; /* out of memory */
2679 }
2680
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002681 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2682 {
2683 /* Prepend a space to draw the leading composing char on. */
2684 arshape_buf[0] = ' ';
2685 newlen = 1;
2686 }
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 for (j = start; j < start + len; j += mb_l)
2689 {
2690 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002691 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002692 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (ARABIC_CHAR(u8c))
2694 {
2695 /* Do Arabic shaping. */
2696 if (cmdmsg_rl)
2697 {
2698 /* displaying from right to left */
2699 pc = prev_c;
2700 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002701 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (j + mb_l >= start + len)
2703 nc = NUL;
2704 else
2705 nc = utf_ptr2char(p + mb_l);
2706 }
2707 else
2708 {
2709 /* displaying from left to right */
2710 if (j + mb_l >= start + len)
2711 pc = NUL;
2712 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002713 {
2714 int pcc[MAX_MCO];
2715
2716 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002718 pc1 = pcc[0];
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 nc = prev_c;
2721 }
2722 prev_c = u8c;
2723
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002724 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002726 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002727 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002729 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2730 if (u8cc[1] != 0)
2731 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002732 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734 }
2735 else
2736 {
2737 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002738 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 newlen += mb_l;
2740 }
2741 }
2742
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002743 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 }
2745 else
2746#endif
2747 msg_outtrans_len(ccline.cmdbuff + start, len);
2748}
2749
2750/*
2751 * Put a character on the command line. Shifts the following text to the
2752 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2753 * "c" must be printable (fit in one display cell)!
2754 */
2755 void
2756putcmdline(c, shift)
2757 int c;
2758 int shift;
2759{
2760 if (cmd_silent)
2761 return;
2762 msg_no_more = TRUE;
2763 msg_putchar(c);
2764 if (shift)
2765 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2766 msg_no_more = FALSE;
2767 cursorcmd();
2768}
2769
2770/*
2771 * Undo a putcmdline(c, FALSE).
2772 */
2773 void
2774unputcmdline()
2775{
2776 if (cmd_silent)
2777 return;
2778 msg_no_more = TRUE;
2779 if (ccline.cmdlen == ccline.cmdpos)
2780 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002781#ifdef FEAT_MBYTE
2782 else if (has_mbyte)
2783 draw_cmdline(ccline.cmdpos,
2784 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 else
2787 draw_cmdline(ccline.cmdpos, 1);
2788 msg_no_more = FALSE;
2789 cursorcmd();
2790}
2791
2792/*
2793 * Put the given string, of the given length, onto the command line.
2794 * If len is -1, then STRLEN() is used to calculate the length.
2795 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2796 * part will be redrawn, otherwise it will not. If this function is called
2797 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2798 * called afterwards.
2799 */
2800 int
2801put_on_cmdline(str, len, redraw)
2802 char_u *str;
2803 int len;
2804 int redraw;
2805{
2806 int retval;
2807 int i;
2808 int m;
2809 int c;
2810
2811 if (len < 0)
2812 len = (int)STRLEN(str);
2813
2814 /* Check if ccline.cmdbuff needs to be longer */
2815 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002816 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 else
2818 retval = OK;
2819 if (retval == OK)
2820 {
2821 if (!ccline.overstrike)
2822 {
2823 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2824 ccline.cmdbuff + ccline.cmdpos,
2825 (size_t)(ccline.cmdlen - ccline.cmdpos));
2826 ccline.cmdlen += len;
2827 }
2828 else
2829 {
2830#ifdef FEAT_MBYTE
2831 if (has_mbyte)
2832 {
2833 /* Count nr of characters in the new string. */
2834 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002835 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 ++m;
2837 /* Count nr of bytes in cmdline that are overwritten by these
2838 * characters. */
2839 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002840 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 --m;
2842 if (i < ccline.cmdlen)
2843 {
2844 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2845 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2846 ccline.cmdlen += ccline.cmdpos + len - i;
2847 }
2848 else
2849 ccline.cmdlen = ccline.cmdpos + len;
2850 }
2851 else
2852#endif
2853 if (ccline.cmdpos + len > ccline.cmdlen)
2854 ccline.cmdlen = ccline.cmdpos + len;
2855 }
2856 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2857 ccline.cmdbuff[ccline.cmdlen] = NUL;
2858
2859#ifdef FEAT_MBYTE
2860 if (enc_utf8)
2861 {
2862 /* When the inserted text starts with a composing character,
2863 * backup to the character before it. There could be two of them.
2864 */
2865 i = 0;
2866 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2867 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2868 {
2869 i = (*mb_head_off)(ccline.cmdbuff,
2870 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2871 ccline.cmdpos -= i;
2872 len += i;
2873 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2874 }
2875# ifdef FEAT_ARABIC
2876 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2877 {
2878 /* Check the previous character for Arabic combining pair. */
2879 i = (*mb_head_off)(ccline.cmdbuff,
2880 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2881 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2882 + ccline.cmdpos - i), c))
2883 {
2884 ccline.cmdpos -= i;
2885 len += i;
2886 }
2887 else
2888 i = 0;
2889 }
2890# endif
2891 if (i != 0)
2892 {
2893 /* Also backup the cursor position. */
2894 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2895 ccline.cmdspos -= i;
2896 msg_col -= i;
2897 if (msg_col < 0)
2898 {
2899 msg_col += Columns;
2900 --msg_row;
2901 }
2902 }
2903 }
2904#endif
2905
2906 if (redraw && !cmd_silent)
2907 {
2908 msg_no_more = TRUE;
2909 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002910 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2912 /* Avoid clearing the rest of the line too often. */
2913 if (cmdline_row != i || ccline.overstrike)
2914 msg_clr_eos();
2915 msg_no_more = FALSE;
2916 }
2917#ifdef FEAT_FKMAP
2918 /*
2919 * If we are in Farsi command mode, the character input must be in
2920 * Insert mode. So do not advance the cmdpos.
2921 */
2922 if (!cmd_fkmap)
2923#endif
2924 {
2925 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002928 if (m < 0) /* overflow, Columns or Rows at weird value */
2929 m = MAXCOL;
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 else
2932 m = MAXCOL;
2933 for (i = 0; i < len; ++i)
2934 {
2935 c = cmdline_charsize(ccline.cmdpos);
2936#ifdef FEAT_MBYTE
2937 /* count ">" for a double-wide char that doesn't fit. */
2938 if (has_mbyte)
2939 correct_cmdspos(ccline.cmdpos, c);
2940#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002941 /* Stop cursor at the end of the screen, but do increment the
2942 * insert position, so that entering a very long command
2943 * works, even though you can't see it. */
2944 if (ccline.cmdspos + c < m)
2945 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946#ifdef FEAT_MBYTE
2947 if (has_mbyte)
2948 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002949 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 if (c > len - i - 1)
2951 c = len - i - 1;
2952 ccline.cmdpos += c;
2953 i += c;
2954 }
2955#endif
2956 ++ccline.cmdpos;
2957 }
2958 }
2959 }
2960 if (redraw)
2961 msg_check();
2962 return retval;
2963}
2964
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002965static struct cmdline_info prev_ccline;
2966static int prev_ccline_used = FALSE;
2967
2968/*
2969 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2970 * and overwrite it. But get_cmdline_str() may need it, thus make it
2971 * available globally in prev_ccline.
2972 */
2973 static void
2974save_cmdline(ccp)
2975 struct cmdline_info *ccp;
2976{
2977 if (!prev_ccline_used)
2978 {
2979 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2980 prev_ccline_used = TRUE;
2981 }
2982 *ccp = prev_ccline;
2983 prev_ccline = ccline;
2984 ccline.cmdbuff = NULL;
2985 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002986 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002987}
2988
2989/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002990 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002991 */
2992 static void
2993restore_cmdline(ccp)
2994 struct cmdline_info *ccp;
2995{
2996 ccline = prev_ccline;
2997 prev_ccline = *ccp;
2998}
2999
Bram Moolenaar5a305422006-04-28 22:38:25 +00003000#if defined(FEAT_EVAL) || defined(PROTO)
3001/*
3002 * Save the command line into allocated memory. Returns a pointer to be
3003 * passed to restore_cmdline_alloc() later.
3004 * Returns NULL when failed.
3005 */
3006 char_u *
3007save_cmdline_alloc()
3008{
3009 struct cmdline_info *p;
3010
3011 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3012 if (p != NULL)
3013 save_cmdline(p);
3014 return (char_u *)p;
3015}
3016
3017/*
3018 * Restore the command line from the return value of save_cmdline_alloc().
3019 */
3020 void
3021restore_cmdline_alloc(p)
3022 char_u *p;
3023{
3024 if (p != NULL)
3025 {
3026 restore_cmdline((struct cmdline_info *)p);
3027 vim_free(p);
3028 }
3029}
3030#endif
3031
Bram Moolenaar8299df92004-07-10 09:47:34 +00003032/*
3033 * paste a yank register into the command line.
3034 * used by CTRL-R command in command-line mode
3035 * insert_reg() can't be used here, because special characters from the
3036 * register contents will be interpreted as commands.
3037 *
3038 * return FAIL for failure, OK otherwise
3039 */
3040 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003041cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003042 int regname;
3043 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003044 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003045{
3046 long i;
3047 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003048 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003049 int allocated;
3050 struct cmdline_info save_ccline;
3051
3052 /* check for valid regname; also accept special characters for CTRL-R in
3053 * the command line */
3054 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3055 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3056 return FAIL;
3057
3058 /* A register containing CTRL-R can cause an endless loop. Allow using
3059 * CTRL-C to break the loop. */
3060 line_breakcheck();
3061 if (got_int)
3062 return FAIL;
3063
3064#ifdef FEAT_CLIPBOARD
3065 regname = may_get_selection(regname);
3066#endif
3067
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003068 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003069 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003070 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003071 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003072 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003073 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003074 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003075
3076 if (i)
3077 {
3078 /* Got the value of a special register in "arg". */
3079 if (arg == NULL)
3080 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003081
3082 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3083 * part of the word. */
3084 p = arg;
3085 if (p_is && regname == Ctrl_W)
3086 {
3087 char_u *w;
3088 int len;
3089
3090 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003091 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003092 {
3093#ifdef FEAT_MBYTE
3094 if (has_mbyte)
3095 {
3096 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3097 if (!vim_iswordc(mb_ptr2char(w - len)))
3098 break;
3099 w -= len;
3100 }
3101 else
3102#endif
3103 {
3104 if (!vim_iswordc(w[-1]))
3105 break;
3106 --w;
3107 }
3108 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003109 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003110 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3111 p += len;
3112 }
3113
3114 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003115 if (allocated)
3116 vim_free(arg);
3117 return OK;
3118 }
3119
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003120 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003121}
3122
3123/*
3124 * Put a string on the command line.
3125 * When "literally" is TRUE, insert literally.
3126 * When "literally" is FALSE, insert as typed, but don't leave the command
3127 * line.
3128 */
3129 void
3130cmdline_paste_str(s, literally)
3131 char_u *s;
3132 int literally;
3133{
3134 int c, cv;
3135
3136 if (literally)
3137 put_on_cmdline(s, -1, TRUE);
3138 else
3139 while (*s != NUL)
3140 {
3141 cv = *s;
3142 if (cv == Ctrl_V && s[1])
3143 ++s;
3144#ifdef FEAT_MBYTE
3145 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003146 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003147 else
3148#endif
3149 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003150 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3151 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003152#ifdef UNIX
3153 || c == intr_char
3154#endif
3155 || (c == Ctrl_BSL && *s == Ctrl_N))
3156 stuffcharReadbuff(Ctrl_V);
3157 stuffcharReadbuff(c);
3158 }
3159}
3160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#ifdef FEAT_WILDMENU
3162/*
3163 * Delete characters on the command line, from "from" to the current
3164 * position.
3165 */
3166 static void
3167cmdline_del(from)
3168 int from;
3169{
3170 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3171 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3172 ccline.cmdlen -= ccline.cmdpos - from;
3173 ccline.cmdpos = from;
3174}
3175#endif
3176
3177/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003178 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 * search
3180 */
3181 void
3182redrawcmdline()
3183{
3184 if (cmd_silent)
3185 return;
3186 need_wait_return = FALSE;
3187 compute_cmdrow();
3188 redrawcmd();
3189 cursorcmd();
3190}
3191
3192 static void
3193redrawcmdprompt()
3194{
3195 int i;
3196
3197 if (cmd_silent)
3198 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003199 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 msg_putchar(ccline.cmdfirstc);
3201 if (ccline.cmdprompt != NULL)
3202 {
3203 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3204 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3205 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003206 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 --ccline.cmdindent;
3208 }
3209 else
3210 for (i = ccline.cmdindent; i > 0; --i)
3211 msg_putchar(' ');
3212}
3213
3214/*
3215 * Redraw what is currently on the command line.
3216 */
3217 void
3218redrawcmd()
3219{
3220 if (cmd_silent)
3221 return;
3222
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003223 /* when 'incsearch' is set there may be no command line while redrawing */
3224 if (ccline.cmdbuff == NULL)
3225 {
3226 windgoto(cmdline_row, 0);
3227 msg_clr_eos();
3228 return;
3229 }
3230
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 msg_start();
3232 redrawcmdprompt();
3233
3234 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3235 msg_no_more = TRUE;
3236 draw_cmdline(0, ccline.cmdlen);
3237 msg_clr_eos();
3238 msg_no_more = FALSE;
3239
3240 set_cmdspos_cursor();
3241
3242 /*
3243 * An emsg() before may have set msg_scroll. This is used in normal mode,
3244 * in cmdline mode we can reset them now.
3245 */
3246 msg_scroll = FALSE; /* next message overwrites cmdline */
3247
3248 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3249 * in cmdline mode */
3250 skip_redraw = FALSE;
3251}
3252
3253 void
3254compute_cmdrow()
3255{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003256 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 cmdline_row = Rows - 1;
3258 else
3259 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3260 + W_STATUS_HEIGHT(lastwin);
3261}
3262
3263 static void
3264cursorcmd()
3265{
3266 if (cmd_silent)
3267 return;
3268
3269#ifdef FEAT_RIGHTLEFT
3270 if (cmdmsg_rl)
3271 {
3272 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3273 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3274 if (msg_row <= 0)
3275 msg_row = Rows - 1;
3276 }
3277 else
3278#endif
3279 {
3280 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3281 msg_col = ccline.cmdspos % (int)Columns;
3282 if (msg_row >= Rows)
3283 msg_row = Rows - 1;
3284 }
3285
3286 windgoto(msg_row, msg_col);
3287#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3288 redrawcmd_preedit();
3289#endif
3290#ifdef MCH_CURSOR_SHAPE
3291 mch_update_cursor();
3292#endif
3293}
3294
3295 void
3296gotocmdline(clr)
3297 int clr;
3298{
3299 msg_start();
3300#ifdef FEAT_RIGHTLEFT
3301 if (cmdmsg_rl)
3302 msg_col = Columns - 1;
3303 else
3304#endif
3305 msg_col = 0; /* always start in column 0 */
3306 if (clr) /* clear the bottom line(s) */
3307 msg_clr_eos(); /* will reset clear_cmdline */
3308 windgoto(cmdline_row, 0);
3309}
3310
3311/*
3312 * Check the word in front of the cursor for an abbreviation.
3313 * Called when the non-id character "c" has been entered.
3314 * When an abbreviation is recognized it is removed from the text with
3315 * backspaces and the replacement string is inserted, followed by "c".
3316 */
3317 static int
3318ccheck_abbr(c)
3319 int c;
3320{
3321 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3322 return FALSE;
3323
3324 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3325}
3326
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003327#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3328 static int
3329#ifdef __BORLANDC__
3330_RTLENTRYF
3331#endif
3332sort_func_compare(s1, s2)
3333 const void *s1;
3334 const void *s2;
3335{
3336 char_u *p1 = *(char_u **)s1;
3337 char_u *p2 = *(char_u **)s2;
3338
3339 if (*p1 != '<' && *p2 == '<') return -1;
3340 if (*p1 == '<' && *p2 != '<') return 1;
3341 return STRCMP(p1, p2);
3342}
3343#endif
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345/*
3346 * Return FAIL if this is not an appropriate context in which to do
3347 * completion of anything, return OK if it is (even if there are no matches).
3348 * For the caller, this means that the character is just passed through like a
3349 * normal character (instead of being expanded). This allows :s/^I^D etc.
3350 */
3351 static int
Bram Moolenaarb3479632012-11-28 16:49:58 +01003352nextwild(xp, type, options, escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 expand_T *xp;
3354 int type;
3355 int options; /* extra options for ExpandOne() */
Bram Moolenaarb3479632012-11-28 16:49:58 +01003356 int escape; /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 int i, j;
3359 char_u *p1;
3360 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 int difflen;
3362 int v;
3363
3364 if (xp->xp_numfiles == -1)
3365 {
3366 set_expand_context(xp);
3367 cmd_showtail = expand_showtail(xp);
3368 }
3369
3370 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3371 {
3372 beep_flush();
3373 return OK; /* Something illegal on command line */
3374 }
3375 if (xp->xp_context == EXPAND_NOTHING)
3376 {
3377 /* Caller can use the character as a normal char instead */
3378 return FAIL;
3379 }
3380
3381 MSG_PUTS("..."); /* show that we are busy */
3382 out_flush();
3383
3384 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003385 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
3387 if (type == WILD_NEXT || type == WILD_PREV)
3388 {
3389 /*
3390 * Get next/previous match for a previous expanded pattern.
3391 */
3392 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3393 }
3394 else
3395 {
3396 /*
3397 * Translate string into pattern and expand it.
3398 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003399 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3400 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 p2 = NULL;
3402 else
3403 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003404 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003405 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3406 if (escape)
3407 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003408
3409 if (p_wic)
3410 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003411 p2 = ExpandOne(xp, p1,
3412 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003413 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003415 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (p2 != NULL && type == WILD_LONGEST)
3417 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003418 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (ccline.cmdbuff[i + j] == '*'
3420 || ccline.cmdbuff[i + j] == '?')
3421 break;
3422 if ((int)STRLEN(p2) < j)
3423 {
3424 vim_free(p2);
3425 p2 = NULL;
3426 }
3427 }
3428 }
3429 }
3430
3431 if (p2 != NULL && !got_int)
3432 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003433 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003434 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003436 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 xp->xp_pattern = ccline.cmdbuff + i;
3438 }
3439 else
3440 v = OK;
3441 if (v == OK)
3442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003443 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3444 &ccline.cmdbuff[ccline.cmdpos],
3445 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3446 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 ccline.cmdlen += difflen;
3448 ccline.cmdpos += difflen;
3449 }
3450 }
3451 vim_free(p2);
3452
3453 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003454 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
3456 /* When expanding a ":map" command and no matches are found, assume that
3457 * the key is supposed to be inserted literally */
3458 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3459 return FAIL;
3460
3461 if (xp->xp_numfiles <= 0 && p2 == NULL)
3462 beep_flush();
3463 else if (xp->xp_numfiles == 1)
3464 /* free expanded pattern */
3465 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3466
3467 return OK;
3468}
3469
3470/*
3471 * Do wildcard expansion on the string 'str'.
3472 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003473 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 * Return NULL for failure.
3475 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003476 * "orig" is the originally expanded string, copied to allocated memory. It
3477 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3478 * WILD_PREV "orig" should be NULL.
3479 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003480 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3481 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 *
3483 * mode = WILD_FREE: just free previously expanded matches
3484 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3485 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3486 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3487 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3488 * mode = WILD_ALL: return all matches concatenated
3489 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003490 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 *
3492 * options = WILD_LIST_NOTFOUND: list entries without a match
3493 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3494 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3495 * options = WILD_NO_BEEP: Don't beep for multiple matches
3496 * options = WILD_ADD_SLASH: add a slash after directory names
3497 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3498 * options = WILD_SILENT: don't print warning messages
3499 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003500 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 *
3502 * The variables xp->xp_context and xp->xp_backslash must have been set!
3503 */
3504 char_u *
3505ExpandOne(xp, str, orig, options, mode)
3506 expand_T *xp;
3507 char_u *str;
3508 char_u *orig; /* allocated copy of original of expanded string */
3509 int options;
3510 int mode;
3511{
3512 char_u *ss = NULL;
3513 static int findex;
3514 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003515 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 int i;
3517 long_u len;
3518 int non_suf_match; /* number without matching suffix */
3519
3520 /*
3521 * first handle the case of using an old match
3522 */
3523 if (mode == WILD_NEXT || mode == WILD_PREV)
3524 {
3525 if (xp->xp_numfiles > 0)
3526 {
3527 if (mode == WILD_PREV)
3528 {
3529 if (findex == -1)
3530 findex = xp->xp_numfiles;
3531 --findex;
3532 }
3533 else /* mode == WILD_NEXT */
3534 ++findex;
3535
3536 /*
3537 * When wrapping around, return the original string, set findex to
3538 * -1.
3539 */
3540 if (findex < 0)
3541 {
3542 if (orig_save == NULL)
3543 findex = xp->xp_numfiles - 1;
3544 else
3545 findex = -1;
3546 }
3547 if (findex >= xp->xp_numfiles)
3548 {
3549 if (orig_save == NULL)
3550 findex = 0;
3551 else
3552 findex = -1;
3553 }
3554#ifdef FEAT_WILDMENU
3555 if (p_wmnu)
3556 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3557 findex, cmd_showtail);
3558#endif
3559 if (findex == -1)
3560 return vim_strsave(orig_save);
3561 return vim_strsave(xp->xp_files[findex]);
3562 }
3563 else
3564 return NULL;
3565 }
3566
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003567 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3569 {
3570 FreeWild(xp->xp_numfiles, xp->xp_files);
3571 xp->xp_numfiles = -1;
3572 vim_free(orig_save);
3573 orig_save = NULL;
3574 }
3575 findex = 0;
3576
3577 if (mode == WILD_FREE) /* only release file name */
3578 return NULL;
3579
3580 if (xp->xp_numfiles == -1)
3581 {
3582 vim_free(orig_save);
3583 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003584 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 /*
3587 * Do the expansion.
3588 */
3589 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3590 options) == FAIL)
3591 {
3592#ifdef FNAME_ILLEGAL
3593 /* Illegal file name has been silently skipped. But when there
3594 * are wildcards, the real problem is that there was no match,
3595 * causing the pattern to be added, which has illegal characters.
3596 */
3597 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3598 EMSG2(_(e_nomatch2), str);
3599#endif
3600 }
3601 else if (xp->xp_numfiles == 0)
3602 {
3603 if (!(options & WILD_SILENT))
3604 EMSG2(_(e_nomatch2), str);
3605 }
3606 else
3607 {
3608 /* Escape the matches for use on the command line. */
3609 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3610
3611 /*
3612 * Check for matching suffixes in file names.
3613 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003614 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3615 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 if (xp->xp_numfiles)
3618 non_suf_match = xp->xp_numfiles;
3619 else
3620 non_suf_match = 1;
3621 if ((xp->xp_context == EXPAND_FILES
3622 || xp->xp_context == EXPAND_DIRECTORIES)
3623 && xp->xp_numfiles > 1)
3624 {
3625 /*
3626 * More than one match; check suffix.
3627 * The files will have been sorted on matching suffix in
3628 * expand_wildcards, only need to check the first two.
3629 */
3630 non_suf_match = 0;
3631 for (i = 0; i < 2; ++i)
3632 if (match_suffix(xp->xp_files[i]))
3633 ++non_suf_match;
3634 }
3635 if (non_suf_match != 1)
3636 {
3637 /* Can we ever get here unless it's while expanding
3638 * interactively? If not, we can get rid of this all
3639 * together. Don't really want to wait for this message
3640 * (and possibly have to hit return to continue!).
3641 */
3642 if (!(options & WILD_SILENT))
3643 EMSG(_(e_toomany));
3644 else if (!(options & WILD_NO_BEEP))
3645 beep_flush();
3646 }
3647 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3648 ss = vim_strsave(xp->xp_files[0]);
3649 }
3650 }
3651 }
3652
3653 /* Find longest common part */
3654 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3655 {
3656 for (len = 0; xp->xp_files[0][len]; ++len)
3657 {
3658 for (i = 0; i < xp->xp_numfiles; ++i)
3659 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003660 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003662 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003663 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
3665 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3666 TOLOWER_LOC(xp->xp_files[0][len]))
3667 break;
3668 }
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003669 else if (xp->xp_files[i][len] != xp->xp_files[0][len])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 break;
3671 }
3672 if (i < xp->xp_numfiles)
3673 {
3674 if (!(options & WILD_NO_BEEP))
3675 vim_beep();
3676 break;
3677 }
3678 }
3679 ss = alloc((unsigned)len + 1);
3680 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003681 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 findex = -1; /* next p_wc gets first one */
3683 }
3684
3685 /* Concatenate all matching names */
3686 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3687 {
3688 len = 0;
3689 for (i = 0; i < xp->xp_numfiles; ++i)
3690 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3691 ss = lalloc(len, TRUE);
3692 if (ss != NULL)
3693 {
3694 *ss = NUL;
3695 for (i = 0; i < xp->xp_numfiles; ++i)
3696 {
3697 STRCAT(ss, xp->xp_files[i]);
3698 if (i != xp->xp_numfiles - 1)
3699 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3700 }
3701 }
3702 }
3703
3704 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3705 ExpandCleanup(xp);
3706
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003707 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003708 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003709 vim_free(orig);
3710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 return ss;
3712}
3713
3714/*
3715 * Prepare an expand structure for use.
3716 */
3717 void
3718ExpandInit(xp)
3719 expand_T *xp;
3720{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003721 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003722 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003724#ifndef BACKSLASH_IN_FILENAME
3725 xp->xp_shell = FALSE;
3726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 xp->xp_numfiles = -1;
3728 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003729#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3730 xp->xp_arg = NULL;
3731#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003732 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733}
3734
3735/*
3736 * Cleanup an expand structure after use.
3737 */
3738 void
3739ExpandCleanup(xp)
3740 expand_T *xp;
3741{
3742 if (xp->xp_numfiles >= 0)
3743 {
3744 FreeWild(xp->xp_numfiles, xp->xp_files);
3745 xp->xp_numfiles = -1;
3746 }
3747}
3748
3749 void
3750ExpandEscape(xp, str, numfiles, files, options)
3751 expand_T *xp;
3752 char_u *str;
3753 int numfiles;
3754 char_u **files;
3755 int options;
3756{
3757 int i;
3758 char_u *p;
3759
3760 /*
3761 * May change home directory back to "~"
3762 */
3763 if (options & WILD_HOME_REPLACE)
3764 tilde_replace(str, numfiles, files);
3765
3766 if (options & WILD_ESCAPE)
3767 {
3768 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003769 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003770 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 || xp->xp_context == EXPAND_BUFFERS
3772 || xp->xp_context == EXPAND_DIRECTORIES)
3773 {
3774 /*
3775 * Insert a backslash into a file name before a space, \, %, #
3776 * and wildmatch characters, except '~'.
3777 */
3778 for (i = 0; i < numfiles; ++i)
3779 {
3780 /* for ":set path=" we need to escape spaces twice */
3781 if (xp->xp_backslash == XP_BS_THREE)
3782 {
3783 p = vim_strsave_escaped(files[i], (char_u *)" ");
3784 if (p != NULL)
3785 {
3786 vim_free(files[i]);
3787 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003788#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 p = vim_strsave_escaped(files[i], (char_u *)" ");
3790 if (p != NULL)
3791 {
3792 vim_free(files[i]);
3793 files[i] = p;
3794 }
3795#endif
3796 }
3797 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003798#ifdef BACKSLASH_IN_FILENAME
3799 p = vim_strsave_fnameescape(files[i], FALSE);
3800#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003801 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (p != NULL)
3804 {
3805 vim_free(files[i]);
3806 files[i] = p;
3807 }
3808
3809 /* If 'str' starts with "\~", replace "~" at start of
3810 * files[i] with "\~". */
3811 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003812 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 }
3814 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003815
3816 /* If the first file starts with a '+' escape it. Otherwise it
3817 * could be seen as "+cmd". */
3818 if (*files[0] == '+')
3819 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 else if (xp->xp_context == EXPAND_TAGS)
3822 {
3823 /*
3824 * Insert a backslash before characters in a tag name that
3825 * would terminate the ":tag" command.
3826 */
3827 for (i = 0; i < numfiles; ++i)
3828 {
3829 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3830 if (p != NULL)
3831 {
3832 vim_free(files[i]);
3833 files[i] = p;
3834 }
3835 }
3836 }
3837 }
3838}
3839
3840/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003841 * Escape special characters in "fname" for when used as a file name argument
3842 * after a Vim command, or, when "shell" is non-zero, a shell command.
3843 * Returns the result in allocated memory.
3844 */
3845 char_u *
3846vim_strsave_fnameescape(fname, shell)
3847 char_u *fname;
3848 int shell;
3849{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003850 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003851#ifdef BACKSLASH_IN_FILENAME
3852 char_u buf[20];
3853 int j = 0;
3854
3855 /* Don't escape '[' and '{' if they are in 'isfname'. */
3856 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3857 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3858 buf[j++] = *p;
3859 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003860 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003861#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003862 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3863 if (shell && csh_like_shell() && p != NULL)
3864 {
3865 char_u *s;
3866
3867 /* For csh and similar shells need to put two backslashes before '!'.
3868 * One is taken by Vim, one by the shell. */
3869 s = vim_strsave_escaped(p, (char_u *)"!");
3870 vim_free(p);
3871 p = s;
3872 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003873#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003874
3875 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3876 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003877 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003878 escape_fname(&p);
3879
3880 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003881}
3882
3883/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003884 * Put a backslash before the file name in "pp", which is in allocated memory.
3885 */
3886 static void
3887escape_fname(pp)
3888 char_u **pp;
3889{
3890 char_u *p;
3891
3892 p = alloc((unsigned)(STRLEN(*pp) + 2));
3893 if (p != NULL)
3894 {
3895 p[0] = '\\';
3896 STRCPY(p + 1, *pp);
3897 vim_free(*pp);
3898 *pp = p;
3899 }
3900}
3901
3902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 * For each file name in files[num_files]:
3904 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3905 */
3906 void
3907tilde_replace(orig_pat, num_files, files)
3908 char_u *orig_pat;
3909 int num_files;
3910 char_u **files;
3911{
3912 int i;
3913 char_u *p;
3914
3915 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3916 {
3917 for (i = 0; i < num_files; ++i)
3918 {
3919 p = home_replace_save(NULL, files[i]);
3920 if (p != NULL)
3921 {
3922 vim_free(files[i]);
3923 files[i] = p;
3924 }
3925 }
3926 }
3927}
3928
3929/*
3930 * Show all matches for completion on the command line.
3931 * Returns EXPAND_NOTHING when the character that triggered expansion should
3932 * be inserted like a normal character.
3933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 static int
3935showmatches(xp, wildmenu)
3936 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003937 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
3939#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3940 int num_files;
3941 char_u **files_found;
3942 int i, j, k;
3943 int maxlen;
3944 int lines;
3945 int columns;
3946 char_u *p;
3947 int lastlen;
3948 int attr;
3949 int showtail;
3950
3951 if (xp->xp_numfiles == -1)
3952 {
3953 set_expand_context(xp);
3954 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3955 &num_files, &files_found);
3956 showtail = expand_showtail(xp);
3957 if (i != EXPAND_OK)
3958 return i;
3959
3960 }
3961 else
3962 {
3963 num_files = xp->xp_numfiles;
3964 files_found = xp->xp_files;
3965 showtail = cmd_showtail;
3966 }
3967
3968#ifdef FEAT_WILDMENU
3969 if (!wildmenu)
3970 {
3971#endif
3972 msg_didany = FALSE; /* lines_left will be set */
3973 msg_start(); /* prepare for paging */
3974 msg_putchar('\n');
3975 out_flush();
3976 cmdline_row = msg_row;
3977 msg_didany = FALSE; /* lines_left will be set again */
3978 msg_start(); /* prepare for paging */
3979#ifdef FEAT_WILDMENU
3980 }
3981#endif
3982
3983 if (got_int)
3984 got_int = FALSE; /* only int. the completion, not the cmd line */
3985#ifdef FEAT_WILDMENU
3986 else if (wildmenu)
3987 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3988#endif
3989 else
3990 {
3991 /* find the length of the longest file name */
3992 maxlen = 0;
3993 for (i = 0; i < num_files; ++i)
3994 {
3995 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003996 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 || xp->xp_context == EXPAND_BUFFERS))
3998 {
3999 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4000 j = vim_strsize(NameBuff);
4001 }
4002 else
4003 j = vim_strsize(L_SHOWFILE(i));
4004 if (j > maxlen)
4005 maxlen = j;
4006 }
4007
4008 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4009 lines = num_files;
4010 else
4011 {
4012 /* compute the number of columns and lines for the listing */
4013 maxlen += 2; /* two spaces between file names */
4014 columns = ((int)Columns + 2) / maxlen;
4015 if (columns < 1)
4016 columns = 1;
4017 lines = (num_files + columns - 1) / columns;
4018 }
4019
4020 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4021
4022 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4023 {
4024 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4025 msg_clr_eos();
4026 msg_advance(maxlen - 3);
4027 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4028 }
4029
4030 /* list the files line by line */
4031 for (i = 0; i < lines; ++i)
4032 {
4033 lastlen = 999;
4034 for (k = i; k < num_files; k += lines)
4035 {
4036 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4037 {
4038 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4039 p = files_found[k] + STRLEN(files_found[k]) + 1;
4040 msg_advance(maxlen + 1);
4041 msg_puts(p);
4042 msg_advance(maxlen + 3);
4043 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4044 break;
4045 }
4046 for (j = maxlen - lastlen; --j >= 0; )
4047 msg_putchar(' ');
4048 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004049 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 || xp->xp_context == EXPAND_BUFFERS)
4051 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004052 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004053 if (xp->xp_numfiles != -1)
4054 {
4055 char_u *halved_slash;
4056 char_u *exp_path;
4057
4058 /* Expansion was done before and special characters
4059 * were escaped, need to halve backslashes. Also
4060 * $HOME has been replaced with ~/. */
4061 exp_path = expand_env_save_opt(files_found[k], TRUE);
4062 halved_slash = backslash_halve_save(
4063 exp_path != NULL ? exp_path : files_found[k]);
4064 j = mch_isdir(halved_slash != NULL ? halved_slash
4065 : files_found[k]);
4066 vim_free(exp_path);
4067 vim_free(halved_slash);
4068 }
4069 else
4070 /* Expansion was done here, file names are literal. */
4071 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 if (showtail)
4073 p = L_SHOWFILE(k);
4074 else
4075 {
4076 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4077 TRUE);
4078 p = NameBuff;
4079 }
4080 }
4081 else
4082 {
4083 j = FALSE;
4084 p = L_SHOWFILE(k);
4085 }
4086 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4087 }
4088 if (msg_col > 0) /* when not wrapped around */
4089 {
4090 msg_clr_eos();
4091 msg_putchar('\n');
4092 }
4093 out_flush(); /* show one line at a time */
4094 if (got_int)
4095 {
4096 got_int = FALSE;
4097 break;
4098 }
4099 }
4100
4101 /*
4102 * we redraw the command below the lines that we have just listed
4103 * This is a bit tricky, but it saves a lot of screen updating.
4104 */
4105 cmdline_row = msg_row; /* will put it back later */
4106 }
4107
4108 if (xp->xp_numfiles == -1)
4109 FreeWild(num_files, files_found);
4110
4111 return EXPAND_OK;
4112}
4113
4114/*
4115 * Private gettail for showmatches() (and win_redr_status_matches()):
4116 * Find tail of file name path, but ignore trailing "/".
4117 */
4118 char_u *
4119sm_gettail(s)
4120 char_u *s;
4121{
4122 char_u *p;
4123 char_u *t = s;
4124 int had_sep = FALSE;
4125
4126 for (p = s; *p != NUL; )
4127 {
4128 if (vim_ispathsep(*p)
4129#ifdef BACKSLASH_IN_FILENAME
4130 && !rem_backslash(p)
4131#endif
4132 )
4133 had_sep = TRUE;
4134 else if (had_sep)
4135 {
4136 t = p;
4137 had_sep = FALSE;
4138 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004139 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
4141 return t;
4142}
4143
4144/*
4145 * Return TRUE if we only need to show the tail of completion matches.
4146 * When not completing file names or there is a wildcard in the path FALSE is
4147 * returned.
4148 */
4149 static int
4150expand_showtail(xp)
4151 expand_T *xp;
4152{
4153 char_u *s;
4154 char_u *end;
4155
4156 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004157 if (xp->xp_context != EXPAND_FILES
4158 && xp->xp_context != EXPAND_SHELLCMD
4159 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 return FALSE;
4161
4162 end = gettail(xp->xp_pattern);
4163 if (end == xp->xp_pattern) /* there is no path separator */
4164 return FALSE;
4165
4166 for (s = xp->xp_pattern; s < end; s++)
4167 {
4168 /* Skip escaped wildcards. Only when the backslash is not a path
4169 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4170 if (rem_backslash(s))
4171 ++s;
4172 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4173 return FALSE;
4174 }
4175 return TRUE;
4176}
4177
4178/*
4179 * Prepare a string for expansion.
4180 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004181 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 * When expanding other names: The string will be used with regcomp(). Copy
4183 * the name into allocated memory and prepend "^".
4184 */
4185 char_u *
4186addstar(fname, len, context)
4187 char_u *fname;
4188 int len;
4189 int context; /* EXPAND_FILES etc. */
4190{
4191 char_u *retval;
4192 int i, j;
4193 int new_len;
4194 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004195 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004197 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004198 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004199 && context != EXPAND_SHELLCMD
4200 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
4202 /*
4203 * Matching will be done internally (on something other than files).
4204 * So we convert the file-matching-type wildcards into our kind for
4205 * use with vim_regcomp(). First work out how long it will be:
4206 */
4207
4208 /* For help tags the translation is done in find_help_tags().
4209 * For a tag pattern starting with "/" no translation is needed. */
4210 if (context == EXPAND_HELP
4211 || context == EXPAND_COLORS
4212 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004213 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004214 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 || (context == EXPAND_TAGS && fname[0] == '/'))
4216 retval = vim_strnsave(fname, len);
4217 else
4218 {
4219 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4220 for (i = 0; i < len; i++)
4221 {
4222 if (fname[i] == '*' || fname[i] == '~')
4223 new_len++; /* '*' needs to be replaced by ".*"
4224 '~' needs to be replaced by "\~" */
4225
4226 /* Buffer names are like file names. "." should be literal */
4227 if (context == EXPAND_BUFFERS && fname[i] == '.')
4228 new_len++; /* "." becomes "\." */
4229
4230 /* Custom expansion takes care of special things, match
4231 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004232 if ((context == EXPAND_USER_DEFINED
4233 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 new_len++; /* '\' becomes "\\" */
4235 }
4236 retval = alloc(new_len);
4237 if (retval != NULL)
4238 {
4239 retval[0] = '^';
4240 j = 1;
4241 for (i = 0; i < len; i++, j++)
4242 {
4243 /* Skip backslash. But why? At least keep it for custom
4244 * expansion. */
4245 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004246 && context != EXPAND_USER_LIST
4247 && fname[i] == '\\'
4248 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 break;
4250
4251 switch (fname[i])
4252 {
4253 case '*': retval[j++] = '.';
4254 break;
4255 case '~': retval[j++] = '\\';
4256 break;
4257 case '?': retval[j] = '.';
4258 continue;
4259 case '.': if (context == EXPAND_BUFFERS)
4260 retval[j++] = '\\';
4261 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004262 case '\\': if (context == EXPAND_USER_DEFINED
4263 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 retval[j++] = '\\';
4265 break;
4266 }
4267 retval[j] = fname[i];
4268 }
4269 retval[j] = NUL;
4270 }
4271 }
4272 }
4273 else
4274 {
4275 retval = alloc(len + 4);
4276 if (retval != NULL)
4277 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004278 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
4280 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004281 * Don't add a star to *, ~, ~user, $var or `cmd`.
4282 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 * ~ would be at the start of the file name, but not the tail.
4284 * $ could be anywhere in the tail.
4285 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004286 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 */
4288 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004289 ends_in_star = (len > 0 && retval[len - 1] == '*');
4290#ifndef BACKSLASH_IN_FILENAME
4291 for (i = len - 2; i >= 0; --i)
4292 {
4293 if (retval[i] != '\\')
4294 break;
4295 ends_in_star = !ends_in_star;
4296 }
4297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004299 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 && vim_strchr(tail, '$') == NULL
4301 && vim_strchr(retval, '`') == NULL)
4302 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004303 else if (len > 0 && retval[len - 1] == '$')
4304 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 retval[len] = NUL;
4306 }
4307 }
4308 return retval;
4309}
4310
4311/*
4312 * Must parse the command line so far to work out what context we are in.
4313 * Completion can then be done based on that context.
4314 * This routine sets the variables:
4315 * xp->xp_pattern The start of the pattern to be expanded within
4316 * the command line (ends at the cursor).
4317 * xp->xp_context The type of thing to expand. Will be one of:
4318 *
4319 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4320 * the command line, like an unknown command. Caller
4321 * should beep.
4322 * EXPAND_NOTHING Unrecognised context for completion, use char like
4323 * a normal char, rather than for completion. eg
4324 * :s/^I/
4325 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4326 * it.
4327 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4328 * EXPAND_FILES After command with XFILE set, or after setting
4329 * with P_EXPAND set. eg :e ^I, :w>>^I
4330 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4331 * when we know only directories are of interest. eg
4332 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004333 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4335 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4336 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4337 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4338 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4339 * EXPAND_EVENTS Complete event names
4340 * EXPAND_SYNTAX Complete :syntax command arguments
4341 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4342 * EXPAND_AUGROUP Complete autocommand group names
4343 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4344 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4345 * eg :unmap a^I , :cunab x^I
4346 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4347 * eg :call sub^I
4348 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4349 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4350 * names in expressions, eg :while s^I
4351 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004352 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 */
4354 static void
4355set_expand_context(xp)
4356 expand_T *xp;
4357{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004358 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (ccline.cmdfirstc != ':'
4360#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004361 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004362 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363#endif
4364 )
4365 {
4366 xp->xp_context = EXPAND_NOTHING;
4367 return;
4368 }
4369 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4370}
4371
4372 void
4373set_cmd_context(xp, str, len, col)
4374 expand_T *xp;
4375 char_u *str; /* start of command line */
4376 int len; /* length of command line (excl. NUL) */
4377 int col; /* position of cursor */
4378{
4379 int old_char = NUL;
4380 char_u *nextcomm;
4381
4382 /*
4383 * Avoid a UMR warning from Purify, only save the character if it has been
4384 * written before.
4385 */
4386 if (col < len)
4387 old_char = str[col];
4388 str[col] = NUL;
4389 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004390
4391#ifdef FEAT_EVAL
4392 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004393 {
4394# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004395 /* pass CMD_SIZE because there is no real command */
4396 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004397# endif
4398 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004399 else if (ccline.input_fn)
4400 {
4401 xp->xp_context = ccline.xp_context;
4402 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004403# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004404 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004405# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004406 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004407 else
4408#endif
4409 while (nextcomm != NULL)
4410 nextcomm = set_one_cmd_context(xp, nextcomm);
4411
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004412 /* Store the string here so that call_user_expand_func() can get to them
4413 * easily. */
4414 xp->xp_line = str;
4415 xp->xp_col = col;
4416
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 str[col] = old_char;
4418}
4419
4420/*
4421 * Expand the command line "str" from context "xp".
4422 * "xp" must have been set by set_cmd_context().
4423 * xp->xp_pattern points into "str", to where the text that is to be expanded
4424 * starts.
4425 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4426 * cursor.
4427 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4428 * key that triggered expansion literally.
4429 * Returns EXPAND_OK otherwise.
4430 */
4431 int
4432expand_cmdline(xp, str, col, matchcount, matches)
4433 expand_T *xp;
4434 char_u *str; /* start of command line */
4435 int col; /* position of cursor */
4436 int *matchcount; /* return: nr of matches */
4437 char_u ***matches; /* return: array of pointers to matches */
4438{
4439 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004440 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4443 {
4444 beep_flush();
4445 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4446 }
4447 if (xp->xp_context == EXPAND_NOTHING)
4448 {
4449 /* Caller can use the character as a normal char instead */
4450 return EXPAND_NOTHING;
4451 }
4452
4453 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004454 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4455 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 if (file_str == NULL)
4457 return EXPAND_UNSUCCESSFUL;
4458
Bram Moolenaar94950a92010-12-02 16:01:29 +01004459 if (p_wic)
4460 options += WILD_ICASE;
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004463 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
4465 *matchcount = 0;
4466 *matches = NULL;
4467 }
4468 vim_free(file_str);
4469
4470 return EXPAND_OK;
4471}
4472
4473#ifdef FEAT_MULTI_LANG
4474/*
4475 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4476 */
4477static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4478
4479 static void
4480cleanup_help_tags(num_file, file)
4481 int num_file;
4482 char_u **file;
4483{
4484 int i, j;
4485 int len;
4486
4487 for (i = 0; i < num_file; ++i)
4488 {
4489 len = (int)STRLEN(file[i]) - 3;
4490 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4491 {
4492 /* Sorting on priority means the same item in another language may
4493 * be anywhere. Search all items for a match up to the "@en". */
4494 for (j = 0; j < num_file; ++j)
4495 if (j != i
4496 && (int)STRLEN(file[j]) == len + 3
4497 && STRNCMP(file[i], file[j], len + 1) == 0)
4498 break;
4499 if (j == num_file)
4500 file[i][len] = NUL;
4501 }
4502 }
4503}
4504#endif
4505
4506/*
4507 * Do the expansion based on xp->xp_context and "pat".
4508 */
4509 static int
4510ExpandFromContext(xp, pat, num_file, file, options)
4511 expand_T *xp;
4512 char_u *pat;
4513 int *num_file;
4514 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004515 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516{
4517#ifdef FEAT_CMDL_COMPL
4518 regmatch_T regmatch;
4519#endif
4520 int ret;
4521 int flags;
4522
4523 flags = EW_DIR; /* include directories */
4524 if (options & WILD_LIST_NOTFOUND)
4525 flags |= EW_NOTFOUND;
4526 if (options & WILD_ADD_SLASH)
4527 flags |= EW_ADDSLASH;
4528 if (options & WILD_KEEP_ALL)
4529 flags |= EW_KEEPALL;
4530 if (options & WILD_SILENT)
4531 flags |= EW_SILENT;
4532
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004533 if (xp->xp_context == EXPAND_FILES
4534 || xp->xp_context == EXPAND_DIRECTORIES
4535 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
4537 /*
4538 * Expand file or directory names.
4539 */
4540 int free_pat = FALSE;
4541 int i;
4542
4543 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4544 * space */
4545 if (xp->xp_backslash != XP_BS_NONE)
4546 {
4547 free_pat = TRUE;
4548 pat = vim_strsave(pat);
4549 for (i = 0; pat[i]; ++i)
4550 if (pat[i] == '\\')
4551 {
4552 if (xp->xp_backslash == XP_BS_THREE
4553 && pat[i + 1] == '\\'
4554 && pat[i + 2] == '\\'
4555 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004556 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (xp->xp_backslash == XP_BS_ONE
4558 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004559 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
4561 }
4562
4563 if (xp->xp_context == EXPAND_FILES)
4564 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004565 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4566 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 else
4568 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004569 if (options & WILD_ICASE)
4570 flags |= EW_ICASE;
4571
Bram Moolenaard7834d32009-12-02 16:14:36 +00004572 /* Expand wildcards, supporting %:h and the like. */
4573 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (free_pat)
4575 vim_free(pat);
4576 return ret;
4577 }
4578
4579 *file = (char_u **)"";
4580 *num_file = 0;
4581 if (xp->xp_context == EXPAND_HELP)
4582 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004583 /* With an empty argument we would get all the help tags, which is
4584 * very slow. Get matches for "help" instead. */
4585 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4586 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
4588#ifdef FEAT_MULTI_LANG
4589 cleanup_help_tags(*num_file, *file);
4590#endif
4591 return OK;
4592 }
4593 return FAIL;
4594 }
4595
4596#ifndef FEAT_CMDL_COMPL
4597 return FAIL;
4598#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004599 if (xp->xp_context == EXPAND_SHELLCMD)
4600 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (xp->xp_context == EXPAND_OLD_SETTING)
4602 return ExpandOldSetting(num_file, file);
4603 if (xp->xp_context == EXPAND_BUFFERS)
4604 return ExpandBufnames(pat, num_file, file, options);
4605 if (xp->xp_context == EXPAND_TAGS
4606 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4607 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4608 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004609 {
4610 char *directories[] = {"colors", NULL};
4611 return ExpandRTDir(pat, num_file, file, directories);
4612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004614 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004615 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004616 return ExpandRTDir(pat, num_file, file, directories);
4617 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004618 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004619 {
4620 char *directories[] = {"syntax", NULL};
4621 return ExpandRTDir(pat, num_file, file, directories);
4622 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004623 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004624 {
4625 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4626 return ExpandRTDir(pat, num_file, file, directories);
4627 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004628# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4629 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004630 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004631# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632
4633 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4634 if (regmatch.regprog == NULL)
4635 return FAIL;
4636
4637 /* set ignore-case according to p_ic, p_scs and pat */
4638 regmatch.rm_ic = ignorecase(pat);
4639
4640 if (xp->xp_context == EXPAND_SETTINGS
4641 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4642 ret = ExpandSettings(xp, &regmatch, num_file, file);
4643 else if (xp->xp_context == EXPAND_MAPPINGS)
4644 ret = ExpandMappings(&regmatch, num_file, file);
4645# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4646 else if (xp->xp_context == EXPAND_USER_DEFINED)
4647 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4648# endif
4649 else
4650 {
4651 static struct expgen
4652 {
4653 int context;
4654 char_u *((*func)__ARGS((expand_T *, int)));
4655 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004656 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 } tab[] =
4658 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004659 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4660 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004661#ifdef FEAT_CMDHIST
4662 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004665 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4666 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4667 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4668 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669#endif
4670#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004671 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4672 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4673 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4674 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675#endif
4676#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004677 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4678 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#endif
4680#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004681 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004683#ifdef FEAT_PROFILE
4684 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4685#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004686 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004688 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4689 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004691#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004692 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004693#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004694#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004695 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004696#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004697#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004698 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4701 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004702 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4703 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004705 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004706 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 };
4708 int i;
4709
4710 /*
4711 * Find a context in the table and call the ExpandGeneric() with the
4712 * right function to do the expansion.
4713 */
4714 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004715 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (xp->xp_context == tab[i].context)
4717 {
4718 if (tab[i].ic)
4719 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004720 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004721 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 break;
4723 }
4724 }
4725
Bram Moolenaar473de612013-06-08 18:19:48 +02004726 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
4728 return ret;
4729#endif /* FEAT_CMDL_COMPL */
4730}
4731
4732#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4733/*
4734 * Expand a list of names.
4735 *
4736 * Generic function for command line completion. It calls a function to
4737 * obtain strings, one by one. The strings are matched against a regexp
4738 * program. Matching strings are copied into an array, which is returned.
4739 *
4740 * Returns OK when no problems encountered, FAIL for error (out of memory).
4741 */
4742 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004743ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 expand_T *xp;
4745 regmatch_T *regmatch;
4746 int *num_file;
4747 char_u ***file;
4748 char_u *((*func)__ARGS((expand_T *, int)));
4749 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004750 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
4752 int i;
4753 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004754 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 char_u *str;
4756
4757 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004758 * round == 0: count the number of matching names
4759 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004761 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
4763 for (i = 0; ; ++i)
4764 {
4765 str = (*func)(xp, i);
4766 if (str == NULL) /* end of list */
4767 break;
4768 if (*str == NUL) /* skip empty strings */
4769 continue;
4770
4771 if (vim_regexec(regmatch, str, (colnr_T)0))
4772 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004773 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004775 if (escaped)
4776 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4777 else
4778 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 (*file)[count] = str;
4780#ifdef FEAT_MENU
4781 if (func == get_menu_names && str != NULL)
4782 {
4783 /* test for separator added by get_menu_names() */
4784 str += STRLEN(str) - 1;
4785 if (*str == '\001')
4786 *str = '.';
4787 }
4788#endif
4789 }
4790 ++count;
4791 }
4792 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004793 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
4795 if (count == 0)
4796 return OK;
4797 *num_file = count;
4798 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4799 if (*file == NULL)
4800 {
4801 *file = (char_u **)"";
4802 return FAIL;
4803 }
4804 count = 0;
4805 }
4806 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004807
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004808 /* Sort the results. Keep menu's in the specified order. */
4809 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004810 {
4811 if (xp->xp_context == EXPAND_EXPRESSION
4812 || xp->xp_context == EXPAND_FUNCTIONS
4813 || xp->xp_context == EXPAND_USER_FUNC)
4814 /* <SNR> functions should be sorted to the end. */
4815 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4816 sort_func_compare);
4817 else
4818 sort_strings(*file, *num_file);
4819 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004820
Bram Moolenaar4f688582007-07-24 12:34:30 +00004821#ifdef FEAT_CMDL_COMPL
4822 /* Reset the variables used for special highlight names expansion, so that
4823 * they don't show up when getting normal highlight names by ID. */
4824 reset_expand_highlight();
4825#endif
4826
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 return OK;
4828}
4829
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004830/*
4831 * Complete a shell command.
4832 * Returns FAIL or OK;
4833 */
4834 static int
4835expand_shellcmd(filepat, num_file, file, flagsarg)
4836 char_u *filepat; /* pattern to match with command names */
4837 int *num_file; /* return: number of matches */
4838 char_u ***file; /* return: array with matches */
4839 int flagsarg; /* EW_ flags */
4840{
4841 char_u *pat;
4842 int i;
4843 char_u *path;
4844 int mustfree = FALSE;
4845 garray_T ga;
4846 char_u *buf = alloc(MAXPATHL);
4847 size_t l;
4848 char_u *s, *e;
4849 int flags = flagsarg;
4850 int ret;
4851
4852 if (buf == NULL)
4853 return FAIL;
4854
4855 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4856 * space */
4857 pat = vim_strsave(filepat);
4858 for (i = 0; pat[i]; ++i)
4859 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004860 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004861
4862 flags |= EW_FILE | EW_EXEC;
4863
4864 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004865 if (mch_isFullName(pat))
4866 path = (char_u *)" ";
4867 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004868 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4869 path = (char_u *)".";
4870 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004871 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004872 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004873 if (path == NULL)
4874 path = (char_u *)"";
4875 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004876
4877 /*
4878 * Go over all directories in $PATH. Expand matches in that directory and
4879 * collect them in "ga".
4880 */
4881 ga_init2(&ga, (int)sizeof(char *), 10);
4882 for (s = path; *s != NUL; s = e)
4883 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004884 if (*s == ' ')
4885 ++s; /* Skip space used for absolute path name. */
4886
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004887#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4888 e = vim_strchr(s, ';');
4889#else
4890 e = vim_strchr(s, ':');
4891#endif
4892 if (e == NULL)
4893 e = s + STRLEN(s);
4894
4895 l = e - s;
4896 if (l > MAXPATHL - 5)
4897 break;
4898 vim_strncpy(buf, s, l);
4899 add_pathsep(buf);
4900 l = STRLEN(buf);
4901 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4902
4903 /* Expand matches in one directory of $PATH. */
4904 ret = expand_wildcards(1, &buf, num_file, file, flags);
4905 if (ret == OK)
4906 {
4907 if (ga_grow(&ga, *num_file) == FAIL)
4908 FreeWild(*num_file, *file);
4909 else
4910 {
4911 for (i = 0; i < *num_file; ++i)
4912 {
4913 s = (*file)[i];
4914 if (STRLEN(s) > l)
4915 {
4916 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004917 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004918 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4919 }
4920 else
4921 vim_free(s);
4922 }
4923 vim_free(*file);
4924 }
4925 }
4926 if (*e != NUL)
4927 ++e;
4928 }
4929 *file = ga.ga_data;
4930 *num_file = ga.ga_len;
4931
4932 vim_free(buf);
4933 vim_free(pat);
4934 if (mustfree)
4935 vim_free(path);
4936 return OK;
4937}
4938
4939
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004941static 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));
4942
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004944 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004945 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004947 static void *
4948call_user_expand_func(user_expand_func, xp, num_file, file)
4949 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 int *num_file;
4952 char_u ***file;
4953{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02004954 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004955 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004958 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004959 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960
Bram Moolenaarb7515462013-06-29 12:58:33 +02004961 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004962 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 *num_file = 0;
4964 *file = NULL;
4965
Bram Moolenaarb7515462013-06-29 12:58:33 +02004966 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00004967 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00004968 keep = ccline.cmdbuff[ccline.cmdlen];
4969 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004970 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02004971
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004972 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02004973 args[1] = xp->xp_line;
4974 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 args[2] = num;
4976
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004977 /* Save the cmdline, we don't know what the function may do. */
4978 save_ccline = ccline;
4979 ccline.cmdbuff = NULL;
4980 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004982
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004983 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004984
4985 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004987 if (ccline.cmdbuff != NULL)
4988 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004989
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004990 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004991 return ret;
4992}
4993
4994/*
4995 * Expand names with a function defined by the user.
4996 */
4997 static int
4998ExpandUserDefined(xp, regmatch, num_file, file)
4999 expand_T *xp;
5000 regmatch_T *regmatch;
5001 int *num_file;
5002 char_u ***file;
5003{
5004 char_u *retstr;
5005 char_u *s;
5006 char_u *e;
5007 char_u keep;
5008 garray_T ga;
5009
5010 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5011 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 return FAIL;
5013
5014 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005015 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 e = vim_strchr(s, '\n');
5018 if (e == NULL)
5019 e = s + STRLEN(s);
5020 keep = *e;
5021 *e = 0;
5022
5023 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5024 {
5025 *e = keep;
5026 if (*e != NUL)
5027 ++e;
5028 continue;
5029 }
5030
5031 if (ga_grow(&ga, 1) == FAIL)
5032 break;
5033
5034 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5035 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
5037 *e = keep;
5038 if (*e != NUL)
5039 ++e;
5040 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005041 vim_free(retstr);
5042 *file = ga.ga_data;
5043 *num_file = ga.ga_len;
5044 return OK;
5045}
5046
5047/*
5048 * Expand names with a list returned by a function defined by the user.
5049 */
5050 static int
5051ExpandUserList(xp, num_file, file)
5052 expand_T *xp;
5053 int *num_file;
5054 char_u ***file;
5055{
5056 list_T *retlist;
5057 listitem_T *li;
5058 garray_T ga;
5059
5060 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5061 if (retlist == NULL)
5062 return FAIL;
5063
5064 ga_init2(&ga, (int)sizeof(char *), 3);
5065 /* Loop over the items in the list. */
5066 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5067 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005068 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5069 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005070
5071 if (ga_grow(&ga, 1) == FAIL)
5072 break;
5073
5074 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005075 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005076 ++ga.ga_len;
5077 }
5078 list_unref(retlist);
5079
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 *file = ga.ga_data;
5081 *num_file = ga.ga_len;
5082 return OK;
5083}
5084#endif
5085
5086/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005087 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005088 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005089 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 */
5091 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005092ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 char_u *pat;
5094 int *num_file;
5095 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005096 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005098 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 char_u *s;
5100 char_u *e;
5101 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005102 int i;
5103 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104
5105 *num_file = 0;
5106 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005107 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005108 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005110 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005112 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5113 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005115 ga_clear_strings(&ga);
5116 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005118 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5119 matches = globpath(p_rtp, s, 0);
5120 vim_free(s);
5121 if (matches == NULL)
5122 continue;
5123
5124 for (s = matches; *s != NUL; s = e)
5125 {
5126 e = vim_strchr(s, '\n');
5127 if (e == NULL)
5128 e = s + STRLEN(s);
5129 if (ga_grow(&ga, 1) == FAIL)
5130 break;
5131 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5132 {
5133 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5134 if (*s == '\n' || vim_ispathsep(*s))
5135 break;
5136 ++s;
5137 ((char_u **)ga.ga_data)[ga.ga_len] =
5138 vim_strnsave(s, (int)(e - s - 4));
5139 ++ga.ga_len;
5140 }
5141 if (*e != NUL)
5142 ++e;
5143 }
5144 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005146 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005147 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005148
5149 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005150 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005151 remove_duplicates(&ga);
5152
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 *file = ga.ga_data;
5154 *num_file = ga.ga_len;
5155 return OK;
5156}
5157
5158#endif
5159
5160#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5161/*
5162 * Expand "file" for all comma-separated directories in "path".
5163 * Returns an allocated string with all matches concatenated, separated by
5164 * newlines. Returns NULL for an error or no matches.
5165 */
5166 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005167globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 char_u *path;
5169 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005170 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172 expand_T xpc;
5173 char_u *buf;
5174 garray_T ga;
5175 int i;
5176 int len;
5177 int num_p;
5178 char_u **p;
5179 char_u *cur = NULL;
5180
5181 buf = alloc(MAXPATHL);
5182 if (buf == NULL)
5183 return NULL;
5184
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005185 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 ga_init2(&ga, 1, 100);
5189
5190 /* Loop over all entries in {path}. */
5191 while (*path != NUL)
5192 {
5193 /* Copy one item of the path to buf[] and concatenate the file name. */
5194 copy_option_part(&path, buf, MAXPATHL, ",");
5195 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5196 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005197# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005198 /* Using the platform's path separator (\) makes vim incorrectly
5199 * treat it as an escape character, use '/' instead. */
5200 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5201 STRCAT(buf, "/");
5202# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005206 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5207 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005209 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005211 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213 /* Concatenate new results to previous ones. */
5214 if (ga_grow(&ga, len) == OK)
5215 {
5216 cur = (char_u *)ga.ga_data + ga.ga_len;
5217 for (i = 0; i < num_p; ++i)
5218 {
5219 STRCPY(cur, p[i]);
5220 cur += STRLEN(p[i]);
5221 *cur++ = '\n';
5222 }
5223 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
5225 FreeWild(num_p, p);
5226 }
5227 }
5228 }
5229 if (cur != NULL)
5230 *--cur = 0; /* Replace trailing newline with NUL */
5231
5232 vim_free(buf);
5233 return (char_u *)ga.ga_data;
5234}
5235
5236#endif
5237
5238#if defined(FEAT_CMDHIST) || defined(PROTO)
5239
5240/*********************************
5241 * Command line history stuff *
5242 *********************************/
5243
5244/*
5245 * Translate a history character to the associated type number.
5246 */
5247 static int
5248hist_char2type(c)
5249 int c;
5250{
5251 if (c == ':')
5252 return HIST_CMD;
5253 if (c == '=')
5254 return HIST_EXPR;
5255 if (c == '@')
5256 return HIST_INPUT;
5257 if (c == '>')
5258 return HIST_DEBUG;
5259 return HIST_SEARCH; /* must be '?' or '/' */
5260}
5261
5262/*
5263 * Table of history names.
5264 * These names are used in :history and various hist...() functions.
5265 * It is sufficient to give the significant prefix of a history name.
5266 */
5267
5268static char *(history_names[]) =
5269{
5270 "cmd",
5271 "search",
5272 "expr",
5273 "input",
5274 "debug",
5275 NULL
5276};
5277
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005278#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5279/*
5280 * Function given to ExpandGeneric() to obtain the possible first
5281 * arguments of the ":history command.
5282 */
5283 static char_u *
5284get_history_arg(xp, idx)
5285 expand_T *xp UNUSED;
5286 int idx;
5287{
5288 static char_u compl[2] = { NUL, NUL };
5289 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005290 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005291 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5292
5293 if (idx < short_names_count)
5294 {
5295 compl[0] = (char_u)short_names[idx];
5296 return compl;
5297 }
5298 if (idx < short_names_count + history_name_count)
5299 return (char_u *)history_names[idx - short_names_count];
5300 if (idx == short_names_count + history_name_count)
5301 return (char_u *)"all";
5302 return NULL;
5303}
5304#endif
5305
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306/*
5307 * init_history() - Initialize the command line history.
5308 * Also used to re-allocate the history when the size changes.
5309 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005310 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311init_history()
5312{
5313 int newlen; /* new length of history table */
5314 histentry_T *temp;
5315 int i;
5316 int j;
5317 int type;
5318
5319 /*
5320 * If size of history table changed, reallocate it
5321 */
5322 newlen = (int)p_hi;
5323 if (newlen != hislen) /* history length changed */
5324 {
5325 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5326 {
5327 if (newlen)
5328 {
5329 temp = (histentry_T *)lalloc(
5330 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5331 if (temp == NULL) /* out of memory! */
5332 {
5333 if (type == 0) /* first one: just keep the old length */
5334 {
5335 newlen = hislen;
5336 break;
5337 }
5338 /* Already changed one table, now we can only have zero
5339 * length for all tables. */
5340 newlen = 0;
5341 type = -1;
5342 continue;
5343 }
5344 }
5345 else
5346 temp = NULL;
5347 if (newlen == 0 || temp != NULL)
5348 {
5349 if (hisidx[type] < 0) /* there are no entries yet */
5350 {
5351 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005352 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 }
5354 else if (newlen > hislen) /* array becomes bigger */
5355 {
5356 for (i = 0; i <= hisidx[type]; ++i)
5357 temp[i] = history[type][i];
5358 j = i;
5359 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005360 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 for ( ; j < hislen; ++i, ++j)
5362 temp[i] = history[type][j];
5363 }
5364 else /* array becomes smaller or 0 */
5365 {
5366 j = hisidx[type];
5367 for (i = newlen - 1; ; --i)
5368 {
5369 if (i >= 0) /* copy newest entries */
5370 temp[i] = history[type][j];
5371 else /* remove older entries */
5372 vim_free(history[type][j].hisstr);
5373 if (--j < 0)
5374 j = hislen - 1;
5375 if (j == hisidx[type])
5376 break;
5377 }
5378 hisidx[type] = newlen - 1;
5379 }
5380 vim_free(history[type]);
5381 history[type] = temp;
5382 }
5383 }
5384 hislen = newlen;
5385 }
5386}
5387
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005388 static void
5389clear_hist_entry(hisptr)
5390 histentry_T *hisptr;
5391{
5392 hisptr->hisnum = 0;
5393 hisptr->viminfo = FALSE;
5394 hisptr->hisstr = NULL;
5395}
5396
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397/*
5398 * Check if command line 'str' is already in history.
5399 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5400 */
5401 static int
Bram Moolenaar07219f92013-04-14 23:19:36 +02005402in_history(type, str, move_to_front, sep, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 int type;
5404 char_u *str;
5405 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005406 int sep;
Bram Moolenaar07219f92013-04-14 23:19:36 +02005407 int writing; /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
5409 int i;
5410 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005411 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
5413 if (hisidx[type] < 0)
5414 return FALSE;
5415 i = hisidx[type];
5416 do
5417 {
5418 if (history[type][i].hisstr == NULL)
5419 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005420
5421 /* For search history, check that the separator character matches as
5422 * well. */
5423 p = history[type][i].hisstr;
5424 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005425 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005426 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {
5428 if (!move_to_front)
5429 return TRUE;
5430 last_i = i;
5431 break;
5432 }
5433 if (--i < 0)
5434 i = hislen - 1;
5435 } while (i != hisidx[type]);
5436
5437 if (last_i >= 0)
5438 {
5439 str = history[type][i].hisstr;
5440 while (i != hisidx[type])
5441 {
5442 if (++i >= hislen)
5443 i = 0;
5444 history[type][last_i] = history[type][i];
5445 last_i = i;
5446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005448 history[type][i].viminfo = FALSE;
5449 history[type][i].hisstr = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 return TRUE;
5451 }
5452 return FALSE;
5453}
5454
5455/*
5456 * Convert history name (from table above) to its HIST_ equivalent.
5457 * When "name" is empty, return "cmd" history.
5458 * Returns -1 for unknown history name.
5459 */
5460 int
5461get_histtype(name)
5462 char_u *name;
5463{
5464 int i;
5465 int len = (int)STRLEN(name);
5466
5467 /* No argument: use current history. */
5468 if (len == 0)
5469 return hist_char2type(ccline.cmdfirstc);
5470
5471 for (i = 0; history_names[i] != NULL; ++i)
5472 if (STRNICMP(name, history_names[i], len) == 0)
5473 return i;
5474
5475 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5476 return hist_char2type(name[0]);
5477
5478 return -1;
5479}
5480
5481static int last_maptick = -1; /* last seen maptick */
5482
5483/*
5484 * Add the given string to the given history. If the string is already in the
5485 * history then it is moved to the front. "histype" may be one of he HIST_
5486 * values.
5487 */
5488 void
5489add_to_history(histype, new_entry, in_map, sep)
5490 int histype;
5491 char_u *new_entry;
5492 int in_map; /* consider maptick when inside a mapping */
5493 int sep; /* separator character used (search hist) */
5494{
5495 histentry_T *hisptr;
5496 int len;
5497
5498 if (hislen == 0) /* no history */
5499 return;
5500
Bram Moolenaara939e432013-11-09 05:30:26 +01005501 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5502 return;
5503
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 /*
5505 * Searches inside the same mapping overwrite each other, so that only
5506 * the last line is kept. Be careful not to remove a line that was moved
5507 * down, only lines that were added.
5508 */
5509 if (histype == HIST_SEARCH && in_map)
5510 {
5511 if (maptick == last_maptick)
5512 {
5513 /* Current line is from the same mapping, remove it */
5514 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5515 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005516 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 --hisnum[histype];
5518 if (--hisidx[HIST_SEARCH] < 0)
5519 hisidx[HIST_SEARCH] = hislen - 1;
5520 }
5521 last_maptick = -1;
5522 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005523 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
5525 if (++hisidx[histype] == hislen)
5526 hisidx[histype] = 0;
5527 hisptr = &history[histype][hisidx[histype]];
5528 vim_free(hisptr->hisstr);
5529
5530 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005531 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5533 if (hisptr->hisstr != NULL)
5534 hisptr->hisstr[len + 1] = sep;
5535
5536 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005537 hisptr->viminfo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 if (histype == HIST_SEARCH && in_map)
5539 last_maptick = maptick;
5540 }
5541}
5542
5543#if defined(FEAT_EVAL) || defined(PROTO)
5544
5545/*
5546 * Get identifier of newest history entry.
5547 * "histype" may be one of the HIST_ values.
5548 */
5549 int
5550get_history_idx(histype)
5551 int histype;
5552{
5553 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5554 || hisidx[histype] < 0)
5555 return -1;
5556
5557 return history[histype][hisidx[histype]].hisnum;
5558}
5559
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005560static struct cmdline_info *get_ccline_ptr __ARGS((void));
5561
5562/*
5563 * Get pointer to the command line info to use. cmdline_paste() may clear
5564 * ccline and put the previous value in prev_ccline.
5565 */
5566 static struct cmdline_info *
5567get_ccline_ptr()
5568{
5569 if ((State & CMDLINE) == 0)
5570 return NULL;
5571 if (ccline.cmdbuff != NULL)
5572 return &ccline;
5573 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5574 return &prev_ccline;
5575 return NULL;
5576}
5577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578/*
5579 * Get the current command line in allocated memory.
5580 * Only works when the command line is being edited.
5581 * Returns NULL when something is wrong.
5582 */
5583 char_u *
5584get_cmdline_str()
5585{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005586 struct cmdline_info *p = get_ccline_ptr();
5587
5588 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005590 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591}
5592
5593/*
5594 * Get the current command line position, counted in bytes.
5595 * Zero is the first position.
5596 * Only works when the command line is being edited.
5597 * Returns -1 when something is wrong.
5598 */
5599 int
5600get_cmdline_pos()
5601{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005602 struct cmdline_info *p = get_ccline_ptr();
5603
5604 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005606 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607}
5608
5609/*
5610 * Set the command line byte position to "pos". Zero is the first position.
5611 * Only works when the command line is being edited.
5612 * Returns 1 when failed, 0 when OK.
5613 */
5614 int
5615set_cmdline_pos(pos)
5616 int pos;
5617{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005618 struct cmdline_info *p = get_ccline_ptr();
5619
5620 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 return 1;
5622
5623 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5624 * changed the command line. */
5625 if (pos < 0)
5626 new_cmdpos = 0;
5627 else
5628 new_cmdpos = pos;
5629 return 0;
5630}
5631
5632/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005633 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005634 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005635 * Only works when the command line is being edited.
5636 * Returns NUL when something is wrong.
5637 */
5638 int
5639get_cmdline_type()
5640{
5641 struct cmdline_info *p = get_ccline_ptr();
5642
5643 if (p == NULL)
5644 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005645 if (p->cmdfirstc == NUL)
5646 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005647 return p->cmdfirstc;
5648}
5649
5650/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 * Calculate history index from a number:
5652 * num > 0: seen as identifying number of a history entry
5653 * num < 0: relative position in history wrt newest entry
5654 * "histype" may be one of the HIST_ values.
5655 */
5656 static int
5657calc_hist_idx(histype, num)
5658 int histype;
5659 int num;
5660{
5661 int i;
5662 histentry_T *hist;
5663 int wrapped = FALSE;
5664
5665 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5666 || (i = hisidx[histype]) < 0 || num == 0)
5667 return -1;
5668
5669 hist = history[histype];
5670 if (num > 0)
5671 {
5672 while (hist[i].hisnum > num)
5673 if (--i < 0)
5674 {
5675 if (wrapped)
5676 break;
5677 i += hislen;
5678 wrapped = TRUE;
5679 }
5680 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5681 return i;
5682 }
5683 else if (-num <= hislen)
5684 {
5685 i += num + 1;
5686 if (i < 0)
5687 i += hislen;
5688 if (hist[i].hisstr != NULL)
5689 return i;
5690 }
5691 return -1;
5692}
5693
5694/*
5695 * Get a history entry by its index.
5696 * "histype" may be one of the HIST_ values.
5697 */
5698 char_u *
5699get_history_entry(histype, idx)
5700 int histype;
5701 int idx;
5702{
5703 idx = calc_hist_idx(histype, idx);
5704 if (idx >= 0)
5705 return history[histype][idx].hisstr;
5706 else
5707 return (char_u *)"";
5708}
5709
5710/*
5711 * Clear all entries of a history.
5712 * "histype" may be one of the HIST_ values.
5713 */
5714 int
5715clr_history(histype)
5716 int histype;
5717{
5718 int i;
5719 histentry_T *hisptr;
5720
5721 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5722 {
5723 hisptr = history[histype];
5724 for (i = hislen; i--;)
5725 {
5726 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005727 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729 hisidx[histype] = -1; /* mark history as cleared */
5730 hisnum[histype] = 0; /* reset identifier counter */
5731 return OK;
5732 }
5733 return FAIL;
5734}
5735
5736/*
5737 * Remove all entries matching {str} from a history.
5738 * "histype" may be one of the HIST_ values.
5739 */
5740 int
5741del_history_entry(histype, str)
5742 int histype;
5743 char_u *str;
5744{
5745 regmatch_T regmatch;
5746 histentry_T *hisptr;
5747 int idx;
5748 int i;
5749 int last;
5750 int found = FALSE;
5751
5752 regmatch.regprog = NULL;
5753 regmatch.rm_ic = FALSE; /* always match case */
5754 if (hislen != 0
5755 && histype >= 0
5756 && histype < HIST_COUNT
5757 && *str != NUL
5758 && (idx = hisidx[histype]) >= 0
5759 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5760 != NULL)
5761 {
5762 i = last = idx;
5763 do
5764 {
5765 hisptr = &history[histype][i];
5766 if (hisptr->hisstr == NULL)
5767 break;
5768 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5769 {
5770 found = TRUE;
5771 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005772 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
5774 else
5775 {
5776 if (i != last)
5777 {
5778 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005779 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
5781 if (--last < 0)
5782 last += hislen;
5783 }
5784 if (--i < 0)
5785 i += hislen;
5786 } while (i != idx);
5787 if (history[histype][idx].hisstr == NULL)
5788 hisidx[histype] = -1;
5789 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005790 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 return found;
5792}
5793
5794/*
5795 * Remove an indexed entry from a history.
5796 * "histype" may be one of the HIST_ values.
5797 */
5798 int
5799del_history_idx(histype, idx)
5800 int histype;
5801 int idx;
5802{
5803 int i, j;
5804
5805 i = calc_hist_idx(histype, idx);
5806 if (i < 0)
5807 return FALSE;
5808 idx = hisidx[histype];
5809 vim_free(history[histype][i].hisstr);
5810
5811 /* When deleting the last added search string in a mapping, reset
5812 * last_maptick, so that the last added search string isn't deleted again.
5813 */
5814 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5815 last_maptick = -1;
5816
5817 while (i != idx)
5818 {
5819 j = (i + 1) % hislen;
5820 history[histype][i] = history[histype][j];
5821 i = j;
5822 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005823 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 if (--i < 0)
5825 i += hislen;
5826 hisidx[histype] = i;
5827 return TRUE;
5828}
5829
5830#endif /* FEAT_EVAL */
5831
5832#if defined(FEAT_CRYPT) || defined(PROTO)
5833/*
5834 * Very specific function to remove the value in ":set key=val" from the
5835 * history.
5836 */
5837 void
5838remove_key_from_history()
5839{
5840 char_u *p;
5841 int i;
5842
5843 i = hisidx[HIST_CMD];
5844 if (i < 0)
5845 return;
5846 p = history[HIST_CMD][i].hisstr;
5847 if (p != NULL)
5848 for ( ; *p; ++p)
5849 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5850 {
5851 p = vim_strchr(p + 3, '=');
5852 if (p == NULL)
5853 break;
5854 ++p;
5855 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5856 if (p[i] == '\\' && p[i + 1])
5857 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005858 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 --p;
5860 }
5861}
5862#endif
5863
5864#endif /* FEAT_CMDHIST */
5865
5866#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5867/*
5868 * Get indices "num1,num2" that specify a range within a list (not a range of
5869 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5870 * Returns OK if parsed successfully, otherwise FAIL.
5871 */
5872 int
5873get_list_range(str, num1, num2)
5874 char_u **str;
5875 int *num1;
5876 int *num2;
5877{
5878 int len;
5879 int first = FALSE;
5880 long num;
5881
5882 *str = skipwhite(*str);
5883 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5884 {
5885 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5886 *str += len;
5887 *num1 = (int)num;
5888 first = TRUE;
5889 }
5890 *str = skipwhite(*str);
5891 if (**str == ',') /* parse "to" part of range */
5892 {
5893 *str = skipwhite(*str + 1);
5894 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5895 if (len > 0)
5896 {
5897 *num2 = (int)num;
5898 *str = skipwhite(*str + len);
5899 }
5900 else if (!first) /* no number given at all */
5901 return FAIL;
5902 }
5903 else if (first) /* only one number given */
5904 *num2 = *num1;
5905 return OK;
5906}
5907#endif
5908
5909#if defined(FEAT_CMDHIST) || defined(PROTO)
5910/*
5911 * :history command - print a history
5912 */
5913 void
5914ex_history(eap)
5915 exarg_T *eap;
5916{
5917 histentry_T *hist;
5918 int histype1 = HIST_CMD;
5919 int histype2 = HIST_CMD;
5920 int hisidx1 = 1;
5921 int hisidx2 = -1;
5922 int idx;
5923 int i, j, k;
5924 char_u *end;
5925 char_u *arg = eap->arg;
5926
5927 if (hislen == 0)
5928 {
5929 MSG(_("'history' option is zero"));
5930 return;
5931 }
5932
5933 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5934 {
5935 end = arg;
5936 while (ASCII_ISALPHA(*end)
5937 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5938 end++;
5939 i = *end;
5940 *end = NUL;
5941 histype1 = get_histtype(arg);
5942 if (histype1 == -1)
5943 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005944 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 {
5946 histype1 = 0;
5947 histype2 = HIST_COUNT-1;
5948 }
5949 else
5950 {
5951 *end = i;
5952 EMSG(_(e_trailing));
5953 return;
5954 }
5955 }
5956 else
5957 histype2 = histype1;
5958 *end = i;
5959 }
5960 else
5961 end = arg;
5962 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5963 {
5964 EMSG(_(e_trailing));
5965 return;
5966 }
5967
5968 for (; !got_int && histype1 <= histype2; ++histype1)
5969 {
5970 STRCPY(IObuff, "\n # ");
5971 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5972 MSG_PUTS_TITLE(IObuff);
5973 idx = hisidx[histype1];
5974 hist = history[histype1];
5975 j = hisidx1;
5976 k = hisidx2;
5977 if (j < 0)
5978 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5979 if (k < 0)
5980 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5981 if (idx >= 0 && j <= k)
5982 for (i = idx + 1; !got_int; ++i)
5983 {
5984 if (i == hislen)
5985 i = 0;
5986 if (hist[i].hisstr != NULL
5987 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5988 {
5989 msg_putchar('\n');
5990 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5991 hist[i].hisnum);
5992 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5993 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005994 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 else
5996 STRCAT(IObuff, hist[i].hisstr);
5997 msg_outtrans(IObuff);
5998 out_flush();
5999 }
6000 if (i == idx)
6001 break;
6002 }
6003 }
6004}
6005#endif
6006
6007#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006008/*
6009 * Buffers for history read from a viminfo file. Only valid while reading.
6010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
6012static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
6013static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
6014static int viminfo_add_at_front = FALSE;
6015
6016static int hist_type2char __ARGS((int type, int use_question));
6017
6018/*
6019 * Translate a history type number to the associated character.
6020 */
6021 static int
6022hist_type2char(type, use_question)
6023 int type;
6024 int use_question; /* use '?' instead of '/' */
6025{
6026 if (type == HIST_CMD)
6027 return ':';
6028 if (type == HIST_SEARCH)
6029 {
6030 if (use_question)
6031 return '?';
6032 else
6033 return '/';
6034 }
6035 if (type == HIST_EXPR)
6036 return '=';
6037 return '@';
6038}
6039
6040/*
6041 * Prepare for reading the history from the viminfo file.
6042 * This allocates history arrays to store the read history lines.
6043 */
6044 void
Bram Moolenaar07219f92013-04-14 23:19:36 +02006045prepare_viminfo_history(asklen, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 int asklen;
Bram Moolenaar07219f92013-04-14 23:19:36 +02006047 int writing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
6049 int i;
6050 int num;
6051 int type;
6052 int len;
6053
6054 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006055 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 if (asklen > hislen)
6057 asklen = hislen;
6058
6059 for (type = 0; type < HIST_COUNT; ++type)
6060 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006061 /* Count the number of empty spaces in the history list. Entries read
6062 * from viminfo previously are also considered empty. If there are
6063 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006065 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 num++;
6067 len = asklen;
6068 if (num > len)
6069 len = num;
6070 if (len <= 0)
6071 viminfo_history[type] = NULL;
6072 else
6073 viminfo_history[type] =
6074 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6075 if (viminfo_history[type] == NULL)
6076 len = 0;
6077 viminfo_hislen[type] = len;
6078 viminfo_hisidx[type] = 0;
6079 }
6080}
6081
6082/*
6083 * Accept a line from the viminfo, store it in the history array when it's
6084 * new.
6085 */
6086 int
Bram Moolenaar07219f92013-04-14 23:19:36 +02006087read_viminfo_history(virp, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 vir_T *virp;
Bram Moolenaar07219f92013-04-14 23:19:36 +02006089 int writing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090{
6091 int type;
6092 long_u len;
6093 char_u *val;
6094 char_u *p;
6095
6096 type = hist_char2type(virp->vir_line[0]);
6097 if (viminfo_hisidx[type] < viminfo_hislen[type])
6098 {
6099 val = viminfo_readstring(virp, 1, TRUE);
6100 if (val != NULL && *val != NUL)
6101 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006102 int sep = (*val == ' ' ? NUL : *val);
6103
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006105 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
6107 /* Need to re-allocate to append the separator byte. */
6108 len = STRLEN(val);
6109 p = lalloc(len + 2, TRUE);
6110 if (p != NULL)
6111 {
6112 if (type == HIST_SEARCH)
6113 {
6114 /* Search entry: Move the separator from the first
6115 * column to after the NUL. */
6116 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006117 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 }
6119 else
6120 {
6121 /* Not a search entry: No separator in the viminfo
6122 * file, add a NUL separator. */
6123 mch_memmove(p, val, (size_t)len + 1);
6124 p[len + 1] = NUL;
6125 }
6126 viminfo_history[type][viminfo_hisidx[type]++] = p;
6127 }
6128 }
6129 }
6130 vim_free(val);
6131 }
6132 return viminfo_readline(virp);
6133}
6134
Bram Moolenaar07219f92013-04-14 23:19:36 +02006135/*
6136 * Finish reading history lines from viminfo. Not used when writing viminfo.
6137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 void
6139finish_viminfo_history()
6140{
6141 int idx;
6142 int i;
6143 int type;
6144
6145 for (type = 0; type < HIST_COUNT; ++type)
6146 {
6147 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006148 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 idx = hisidx[type] + viminfo_hisidx[type];
6150 if (idx >= hislen)
6151 idx -= hislen;
6152 else if (idx < 0)
6153 idx = hislen - 1;
6154 if (viminfo_add_at_front)
6155 hisidx[type] = idx;
6156 else
6157 {
6158 if (hisidx[type] == -1)
6159 hisidx[type] = hislen - 1;
6160 do
6161 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006162 if (history[type][idx].hisstr != NULL
6163 || history[type][idx].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 break;
6165 if (++idx == hislen)
6166 idx = 0;
6167 } while (idx != hisidx[type]);
6168 if (idx != hisidx[type] && --idx < 0)
6169 idx = hislen - 1;
6170 }
6171 for (i = 0; i < viminfo_hisidx[type]; i++)
6172 {
6173 vim_free(history[type][idx].hisstr);
6174 history[type][idx].hisstr = viminfo_history[type][i];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006175 history[type][idx].viminfo = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 if (--idx < 0)
6177 idx = hislen - 1;
6178 }
6179 idx += 1;
6180 idx %= hislen;
6181 for (i = 0; i < viminfo_hisidx[type]; i++)
6182 {
6183 history[type][idx++].hisnum = ++hisnum[type];
6184 idx %= hislen;
6185 }
6186 vim_free(viminfo_history[type]);
6187 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006188 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
6190}
6191
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006192/*
6193 * Write history to viminfo file in "fp".
6194 * When "merge" is TRUE merge history lines with a previously read viminfo
6195 * file, data is in viminfo_history[].
6196 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 void
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006199write_viminfo_history(fp, merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 FILE *fp;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006201 int merge;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 int i;
6204 int type;
6205 int num_saved;
6206 char_u *p;
6207 int c;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006208 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
6210 init_history();
6211 if (hislen == 0)
6212 return;
6213 for (type = 0; type < HIST_COUNT; ++type)
6214 {
6215 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6216 if (num_saved == 0)
6217 continue;
6218 if (num_saved < 0) /* Use default */
6219 num_saved = hislen;
6220 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6221 type == HIST_CMD ? _("Command Line") :
6222 type == HIST_SEARCH ? _("Search String") :
6223 type == HIST_EXPR ? _("Expression") :
6224 _("Input Line"));
6225 if (num_saved > hislen)
6226 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006227
6228 /*
6229 * Merge typed and viminfo history:
6230 * round 1: history of typed commands.
6231 * round 2: history from recently read viminfo.
6232 */
6233 for (round = 1; round <= 2; ++round)
6234 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006235 if (round == 1)
6236 /* start at newest entry, somewhere in the list */
6237 i = hisidx[type];
6238 else if (viminfo_hisidx[type] > 0)
6239 /* start at newest entry, first in the list */
6240 i = 0;
6241 else
6242 /* empty list */
6243 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006244 if (i >= 0)
6245 while (num_saved > 0
6246 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006248 p = round == 1 ? history[type][i].hisstr
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006249 : viminfo_history[type] == NULL ? NULL
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006250 : viminfo_history[type][i];
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006251 if (p != NULL && (round == 2
6252 || !merge
6253 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006255 --num_saved;
6256 fputc(hist_type2char(type, TRUE), fp);
6257 /* For the search history: put the separator in the
6258 * second column; use a space if there isn't one. */
6259 if (type == HIST_SEARCH)
6260 {
6261 c = p[STRLEN(p) + 1];
6262 putc(c == NUL ? ' ' : c, fp);
6263 }
6264 viminfo_writestring(fp, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006266 if (round == 1)
6267 {
6268 /* Decrement index, loop around and stop when back at
6269 * the start. */
6270 if (--i < 0)
6271 i = hislen - 1;
6272 if (i == hisidx[type])
6273 break;
6274 }
6275 else
6276 {
6277 /* Increment index. Stop at the end in the while. */
6278 ++i;
6279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006281 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006282 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006283 if (viminfo_history[type] != NULL)
6284 vim_free(viminfo_history[type][i]);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006285 vim_free(viminfo_history[type]);
6286 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006287 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289}
6290#endif /* FEAT_VIMINFO */
6291
6292#if defined(FEAT_FKMAP) || defined(PROTO)
6293/*
6294 * Write a character at the current cursor+offset position.
6295 * It is directly written into the command buffer block.
6296 */
6297 void
6298cmd_pchar(c, offset)
6299 int c, offset;
6300{
6301 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6302 {
6303 EMSG(_("E198: cmd_pchar beyond the command length"));
6304 return;
6305 }
6306 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6307 ccline.cmdbuff[ccline.cmdlen] = NUL;
6308}
6309
6310 int
6311cmd_gchar(offset)
6312 int offset;
6313{
6314 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6315 {
6316 /* EMSG(_("cmd_gchar beyond the command length")); */
6317 return NUL;
6318 }
6319 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6320}
6321#endif
6322
6323#if defined(FEAT_CMDWIN) || defined(PROTO)
6324/*
6325 * Open a window on the current command line and history. Allow editing in
6326 * the window. Returns when the window is closed.
6327 * Returns:
6328 * CR if the command is to be executed
6329 * Ctrl_C if it is to be abandoned
6330 * K_IGNORE if editing continues
6331 */
6332 static int
6333ex_window()
6334{
6335 struct cmdline_info save_ccline;
6336 buf_T *old_curbuf = curbuf;
6337 win_T *old_curwin = curwin;
6338 buf_T *bp;
6339 win_T *wp;
6340 int i;
6341 linenr_T lnum;
6342 int histtype;
6343 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006344#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 int save_restart_edit = restart_edit;
6348 int save_State = State;
6349 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006350#ifdef FEAT_RIGHTLEFT
6351 int save_cmdmsg_rl = cmdmsg_rl;
6352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
6354 /* Can't do this recursively. Can't do it when typing a password. */
6355 if (cmdwin_type != 0
6356# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6357 || cmdline_star > 0
6358# endif
6359 )
6360 {
6361 beep_flush();
6362 return K_IGNORE;
6363 }
6364
6365 /* Save current window sizes. */
6366 win_size_save(&winsizes);
6367
6368# ifdef FEAT_AUTOCMD
6369 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006370 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006372 /* don't use a new tab page */
6373 cmdmod.tab = 0;
6374
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 /* Create a window for the command-line buffer. */
6376 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6377 {
6378 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006379# ifdef FEAT_AUTOCMD
6380 unblock_autocmds();
6381# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 return K_IGNORE;
6383 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006384 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
6386 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006387 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006388 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6390 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6391 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006392#ifdef FEAT_FOLDING
6393 curwin->w_p_fen = FALSE;
6394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006396 curwin->w_p_rl = cmdmsg_rl;
6397 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006399 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
6401# ifdef FEAT_AUTOCMD
6402 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006403 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404# endif
6405
Bram Moolenaar46152342005-09-07 21:18:43 +00006406 /* Showing the prompt may have set need_wait_return, reset it. */
6407 need_wait_return = FALSE;
6408
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006409 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6411 {
6412 if (p_wc == TAB)
6413 {
6414 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6415 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6416 }
6417 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6418 }
6419
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006420 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6421 * sets 'textwidth' to 78). */
6422 curbuf->b_p_tw = 0;
6423
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 /* Fill the buffer with the history. */
6425 init_history();
6426 if (hislen > 0)
6427 {
6428 i = hisidx[histtype];
6429 if (i >= 0)
6430 {
6431 lnum = 0;
6432 do
6433 {
6434 if (++i == hislen)
6435 i = 0;
6436 if (history[histtype][i].hisstr != NULL)
6437 ml_append(lnum++, history[histtype][i].hisstr,
6438 (colnr_T)0, FALSE);
6439 }
6440 while (i != hisidx[histtype]);
6441 }
6442 }
6443
6444 /* Replace the empty last line with the current command-line and put the
6445 * cursor there. */
6446 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6447 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6448 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006449 changed_line_abv_curs();
6450 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006451 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452
6453 /* Save the command line info, can be used recursively. */
6454 save_ccline = ccline;
6455 ccline.cmdbuff = NULL;
6456 ccline.cmdprompt = NULL;
6457
6458 /* No Ex mode here! */
6459 exmode_active = 0;
6460
6461 State = NORMAL;
6462# ifdef FEAT_MOUSE
6463 setmouse();
6464# endif
6465
6466# ifdef FEAT_AUTOCMD
6467 /* Trigger CmdwinEnter autocommands. */
6468 typestr[0] = cmdwin_type;
6469 typestr[1] = NUL;
6470 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006471 if (restart_edit != 0) /* autocmd with ":startinsert" */
6472 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473# endif
6474
6475 i = RedrawingDisabled;
6476 RedrawingDisabled = 0;
6477
6478 /*
6479 * Call the main loop until <CR> or CTRL-C is typed.
6480 */
6481 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006482 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483
6484 RedrawingDisabled = i;
6485
6486# ifdef FEAT_AUTOCMD
6487 /* Trigger CmdwinLeave autocommands. */
6488 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6489# endif
6490
Bram Moolenaarccc18222007-05-10 18:25:20 +00006491 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 ccline = save_ccline;
6493 cmdwin_type = 0;
6494
6495 exmode_active = save_exmode;
6496
Bram Moolenaarf9821062008-06-20 16:31:07 +00006497 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 * this happens! */
6499 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6500 {
6501 cmdwin_result = Ctrl_C;
6502 EMSG(_("E199: Active window or buffer deleted"));
6503 }
6504 else
6505 {
6506# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6507 /* autocmds may abort script processing */
6508 if (aborting() && cmdwin_result != K_IGNORE)
6509 cmdwin_result = Ctrl_C;
6510# endif
6511 /* Set the new command line from the cmdline buffer. */
6512 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006513 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006515 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6516
6517 if (histtype == HIST_CMD)
6518 {
6519 /* Execute the command directly. */
6520 ccline.cmdbuff = vim_strsave((char_u *)p);
6521 cmdwin_result = CAR;
6522 }
6523 else
6524 {
6525 /* First need to cancel what we were doing. */
6526 ccline.cmdbuff = NULL;
6527 stuffcharReadbuff(':');
6528 stuffReadbuff((char_u *)p);
6529 stuffcharReadbuff(CAR);
6530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
6532 else if (cmdwin_result == K_XF2) /* :qa typed */
6533 {
6534 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6535 cmdwin_result = CAR;
6536 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006537 else if (cmdwin_result == Ctrl_C)
6538 {
6539 /* :q or :close, don't execute any command
6540 * and don't modify the cmd window. */
6541 ccline.cmdbuff = NULL;
6542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 else
6544 ccline.cmdbuff = vim_strsave(ml_get_curline());
6545 if (ccline.cmdbuff == NULL)
6546 cmdwin_result = Ctrl_C;
6547 else
6548 {
6549 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6550 ccline.cmdbufflen = ccline.cmdlen + 1;
6551 ccline.cmdpos = curwin->w_cursor.col;
6552 if (ccline.cmdpos > ccline.cmdlen)
6553 ccline.cmdpos = ccline.cmdlen;
6554 if (cmdwin_result == K_IGNORE)
6555 {
6556 set_cmdspos_cursor();
6557 redrawcmd();
6558 }
6559 }
6560
6561# ifdef FEAT_AUTOCMD
6562 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006563 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564# endif
6565 wp = curwin;
6566 bp = curbuf;
6567 win_goto(old_curwin);
6568 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006569
6570 /* win_close() may have already wiped the buffer when 'bh' is
6571 * set to 'wipe' */
6572 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006573 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
6575 /* Restore window sizes. */
6576 win_size_restore(&winsizes);
6577
6578# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006579 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580# endif
6581 }
6582
6583 ga_clear(&winsizes);
6584 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006585# ifdef FEAT_RIGHTLEFT
6586 cmdmsg_rl = save_cmdmsg_rl;
6587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588
6589 State = save_State;
6590# ifdef FEAT_MOUSE
6591 setmouse();
6592# endif
6593
6594 return cmdwin_result;
6595}
6596#endif /* FEAT_CMDWIN */
6597
6598/*
6599 * Used for commands that either take a simple command string argument, or:
6600 * cmd << endmarker
6601 * {script}
6602 * endmarker
6603 * Returns a pointer to allocated memory with {script} or NULL.
6604 */
6605 char_u *
6606script_get(eap, cmd)
6607 exarg_T *eap;
6608 char_u *cmd;
6609{
6610 char_u *theline;
6611 char *end_pattern = NULL;
6612 char dot[] = ".";
6613 garray_T ga;
6614
6615 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6616 return NULL;
6617
6618 ga_init2(&ga, 1, 0x400);
6619
6620 if (cmd[2] != NUL)
6621 end_pattern = (char *)skipwhite(cmd + 2);
6622 else
6623 end_pattern = dot;
6624
6625 for (;;)
6626 {
6627 theline = eap->getline(
6628#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006629 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630#endif
6631 NUL, eap->cookie, 0);
6632
6633 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006634 {
6635 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638
6639 ga_concat(&ga, theline);
6640 ga_append(&ga, '\n');
6641 vim_free(theline);
6642 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006643 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645 return (char_u *)ga.ga_data;
6646}