blob: 57582e84dfa38534bafd179bbc26d1bda7053984 [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 */
59 char_u *hisstr; /* actual entry, separator char after the NUL */
60} histentry_T;
61
62static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
63static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
64static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
65 /* identifying (unique) number of newest history entry */
66static int hislen = 0; /* actual length of history tables */
67
68static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaar4c402232011-07-27 17:58:46 +020070static int in_history __ARGS((int, char_u *, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000071# ifdef FEAT_EVAL
72static int calc_hist_idx __ARGS((int histype, int num));
73# endif
74#endif
75
76#ifdef FEAT_RIGHTLEFT
77static int cmd_hkmap = 0; /* Hebrew mapping during command line */
78#endif
79
80#ifdef FEAT_FKMAP
81static int cmd_fkmap = 0; /* Farsi mapping during command line */
82#endif
83
84static int cmdline_charsize __ARGS((int idx));
85static void set_cmdspos __ARGS((void));
86static void set_cmdspos_cursor __ARGS((void));
87#ifdef FEAT_MBYTE
88static void correct_cmdspos __ARGS((int idx, int cells));
89#endif
90static void alloc_cmdbuff __ARGS((int len));
91static int realloc_cmdbuff __ARGS((int len));
92static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000093static void save_cmdline __ARGS((struct cmdline_info *ccp));
94static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar1769d5a2006-10-17 14:25:24 +000095static int cmdline_paste __ARGS((int regname, int literally, int remcr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000096#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
97static void redrawcmd_preedit __ARGS((void));
98#endif
99#ifdef FEAT_WILDMENU
100static void cmdline_del __ARGS((int from));
101#endif
102static void redrawcmdprompt __ARGS((void));
103static void cursorcmd __ARGS((void));
104static int ccheck_abbr __ARGS((int));
Bram Moolenaarb3479632012-11-28 16:49:58 +0100105static int nextwild __ARGS((expand_T *xp, int type, int options, int escape));
Bram Moolenaar45360022005-07-21 21:08:21 +0000106static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static int showmatches __ARGS((expand_T *xp, int wildmenu));
108static void set_expand_context __ARGS((expand_T *xp));
109static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
110static int expand_showtail __ARGS((expand_T *xp));
111#ifdef FEAT_CMDL_COMPL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000112static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar0c7437a2011-06-26 19:40:23 +0200113static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname[]));
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200114# ifdef FEAT_CMDHIST
115static char_u *get_history_arg __ARGS((expand_T *xp, int idx));
116# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
118static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000119static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# endif
121#endif
122
123#ifdef FEAT_CMDWIN
124static int ex_window __ARGS((void));
125#endif
126
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200127#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
128static int
129#ifdef __BORLANDC__
130_RTLENTRYF
131#endif
132sort_func_compare __ARGS((const void *s1, const void *s2));
133#endif
134
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135/*
136 * getcmdline() - accept a command line starting with firstc.
137 *
138 * firstc == ':' get ":" command line.
139 * firstc == '/' or '?' get search pattern
140 * firstc == '=' get expression
141 * firstc == '@' get text for input() function
142 * firstc == '>' get text for debug mode
143 * firstc == NUL get text for :insert command
144 * firstc == -1 like NUL, and break on CTRL-C
145 *
146 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
147 * command line.
148 *
149 * Careful: getcmdline() can be called recursively!
150 *
151 * Return pointer to allocated string if there is a commandline, NULL
152 * otherwise.
153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 char_u *
155getcmdline(firstc, count, indent)
156 int firstc;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000157 long count UNUSED; /* only used for incremental search */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 int indent; /* indent for inside conditionals */
159{
160 int c;
161 int i;
162 int j;
163 int gotesc = FALSE; /* TRUE when <ESC> just typed */
164 int do_abbr; /* when TRUE check for abbr. */
165#ifdef FEAT_CMDHIST
166 char_u *lookfor = NULL; /* string to match */
167 int hiscnt; /* current history line in use */
168 int histype; /* history type to be used */
169#endif
170#ifdef FEAT_SEARCH_EXTRA
171 pos_T old_cursor;
172 colnr_T old_curswant;
173 colnr_T old_leftcol;
174 linenr_T old_topline;
175# ifdef FEAT_DIFF
176 int old_topfill;
177# endif
178 linenr_T old_botline;
179 int did_incsearch = FALSE;
180 int incsearch_postponed = FALSE;
181#endif
182 int did_wild_list = FALSE; /* did wild_list() recently */
183 int wim_index = 0; /* index in wim_flags[] */
184 int res;
185 int save_msg_scroll = msg_scroll;
186 int save_State = State; /* remember State when called */
187 int some_key_typed = FALSE; /* one of the keys was typed */
188#ifdef FEAT_MOUSE
189 /* mouse drag and release events are ignored, unless they are
190 * preceded with a mouse down event */
191 int ignore_drag_release = TRUE;
192#endif
193#ifdef FEAT_EVAL
194 int break_ctrl_c = FALSE;
195#endif
196 expand_T xpc;
197 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000198#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
199 /* Everything that may work recursively should save and restore the
200 * current command line in save_ccline. That includes update_screen(), a
201 * custom status line may invoke ":normal". */
202 struct cmdline_info save_ccline;
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205#ifdef FEAT_SNIFF
206 want_sniff_request = 0;
207#endif
208#ifdef FEAT_EVAL
209 if (firstc == -1)
210 {
211 firstc = NUL;
212 break_ctrl_c = TRUE;
213 }
214#endif
215#ifdef FEAT_RIGHTLEFT
216 /* start without Hebrew mapping for a command line */
217 if (firstc == ':' || firstc == '=' || firstc == '>')
218 cmd_hkmap = 0;
219#endif
220
221 ccline.overstrike = FALSE; /* always start in insert mode */
222#ifdef FEAT_SEARCH_EXTRA
223 old_cursor = curwin->w_cursor; /* needs to be restored later */
224 old_curswant = curwin->w_curswant;
225 old_leftcol = curwin->w_leftcol;
226 old_topline = curwin->w_topline;
227# ifdef FEAT_DIFF
228 old_topfill = curwin->w_topfill;
229# endif
230 old_botline = curwin->w_botline;
231#endif
232
233 /*
234 * set some variables for redrawcmd()
235 */
236 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000237 ccline.cmdindent = (firstc > 0 ? indent : 0);
238
239 /* alloc initial ccline.cmdbuff */
240 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 if (ccline.cmdbuff == NULL)
242 return NULL; /* out of memory */
243 ccline.cmdlen = ccline.cmdpos = 0;
244 ccline.cmdbuff[0] = NUL;
245
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000246 /* autoindent for :insert and :append */
247 if (firstc <= 0)
248 {
249 copy_spaces(ccline.cmdbuff, indent);
250 ccline.cmdbuff[indent] = NUL;
251 ccline.cmdpos = indent;
252 ccline.cmdspos = indent;
253 ccline.cmdlen = indent;
254 }
255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000257 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259#ifdef FEAT_RIGHTLEFT
260 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
261 && (firstc == '/' || firstc == '?'))
262 cmdmsg_rl = TRUE;
263 else
264 cmdmsg_rl = FALSE;
265#endif
266
267 redir_off = TRUE; /* don't redirect the typed command */
268 if (!cmd_silent)
269 {
270 i = msg_scrolled;
271 msg_scrolled = 0; /* avoid wait_return message */
272 gotocmdline(TRUE);
273 msg_scrolled += i;
274 redrawcmdprompt(); /* draw prompt or indent */
275 set_cmdspos();
276 }
277 xpc.xp_context = EXPAND_NOTHING;
278 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000279#ifndef BACKSLASH_IN_FILENAME
280 xpc.xp_shell = FALSE;
281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000283#if defined(FEAT_EVAL)
284 if (ccline.input_fn)
285 {
286 xpc.xp_context = ccline.xp_context;
287 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000288# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000289 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000290# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000291 }
292#endif
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 /*
295 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
296 * doing ":@0" when register 0 doesn't contain a CR.
297 */
298 msg_scroll = FALSE;
299
300 State = CMDLINE;
301
302 if (firstc == '/' || firstc == '?' || firstc == '@')
303 {
304 /* Use ":lmap" mappings for search pattern and input(). */
305 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
306 b_im_ptr = &curbuf->b_p_iminsert;
307 else
308 b_im_ptr = &curbuf->b_p_imsearch;
309 if (*b_im_ptr == B_IMODE_LMAP)
310 State |= LANGMAP;
311#ifdef USE_IM_CONTROL
312 im_set_active(*b_im_ptr == B_IMODE_IM);
313#endif
314 }
315#ifdef USE_IM_CONTROL
316 else if (p_imcmdline)
317 im_set_active(TRUE);
318#endif
319
320#ifdef FEAT_MOUSE
321 setmouse();
322#endif
323#ifdef CURSOR_SHAPE
324 ui_cursor_shape(); /* may show different cursor shape */
325#endif
326
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000327 /* When inside an autocommand for writing "exiting" may be set and
328 * terminal mode set to cooked. Need to set raw mode here then. */
329 settmode(TMODE_RAW);
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#ifdef FEAT_CMDHIST
332 init_history();
333 hiscnt = hislen; /* set hiscnt to impossible history value */
334 histype = hist_char2type(firstc);
335#endif
336
337#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000338 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#endif
340
341 /*
342 * Collect the command string, handling editing keys.
343 */
344 for (;;)
345 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000346 redir_off = TRUE; /* Don't redirect the typed command.
347 Repeated, because a ":redir" inside
348 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#ifdef USE_ON_FLY_SCROLL
350 dont_scroll = FALSE; /* allow scrolling here */
351#endif
352 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
353
354 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000355
356 /* Get a character. Ignore K_IGNORE, it should not do anything, such
357 * as stop completion. */
358 do
359 {
360 c = safe_vgetc();
361 } while (c == K_IGNORE);
362
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 if (KeyTyped)
364 {
365 some_key_typed = TRUE;
366#ifdef FEAT_RIGHTLEFT
367 if (cmd_hkmap)
368 c = hkmap(c);
369# ifdef FEAT_FKMAP
370 if (cmd_fkmap)
371 c = cmdl_fkmap(c);
372# endif
373 if (cmdmsg_rl && !KeyStuffed)
374 {
375 /* Invert horizontal movements and operations. Only when
376 * typed by the user directly, not when the result of a
377 * mapping. */
378 switch (c)
379 {
380 case K_RIGHT: c = K_LEFT; break;
381 case K_S_RIGHT: c = K_S_LEFT; break;
382 case K_C_RIGHT: c = K_C_LEFT; break;
383 case K_LEFT: c = K_RIGHT; break;
384 case K_S_LEFT: c = K_S_RIGHT; break;
385 case K_C_LEFT: c = K_C_RIGHT; break;
386 }
387 }
388#endif
389 }
390
391 /*
392 * Ignore got_int when CTRL-C was typed here.
393 * Don't ignore it in :global, we really need to break then, e.g., for
394 * ":g/pat/normal /pat" (without the <CR>).
395 * Don't ignore it for the input() function.
396 */
397 if ((c == Ctrl_C
398#ifdef UNIX
399 || c == intr_char
400#endif
401 )
402#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
403 && firstc != '@'
404#endif
405#ifdef FEAT_EVAL
406 && !break_ctrl_c
407#endif
408 && !global_busy)
409 got_int = FALSE;
410
411#ifdef FEAT_CMDHIST
412 /* free old command line when finished moving around in the history
413 * list */
414 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000415 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000416 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 && c != K_PAGEDOWN && c != K_PAGEUP
418 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000419 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
421 {
422 vim_free(lookfor);
423 lookfor = NULL;
424 }
425#endif
426
427 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000428 * When there are matching completions to select <S-Tab> works like
429 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000431 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 c = Ctrl_P;
433
434#ifdef FEAT_WILDMENU
435 /* Special translations for 'wildmenu' */
436 if (did_wild_list && p_wmnu)
437 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000438 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000440 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 c = Ctrl_N;
442 }
443 /* Hitting CR after "emenu Name.": complete submenu */
444 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
445 && ccline.cmdpos > 1
446 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
447 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
448 && (c == '\n' || c == '\r' || c == K_KENTER))
449 c = K_DOWN;
450#endif
451
452 /* free expanded names when finished walking through matches */
453 if (xpc.xp_numfiles != -1
454 && !(c == p_wc && KeyTyped) && c != p_wcm
455 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
456 && c != Ctrl_L)
457 {
458 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
459 did_wild_list = FALSE;
460#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000461 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462#endif
463 xpc.xp_context = EXPAND_NOTHING;
464 wim_index = 0;
465#ifdef FEAT_WILDMENU
466 if (p_wmnu && wild_menu_showing != 0)
467 {
468 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000469 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000470
471 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000472 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473
474 if (wild_menu_showing == WM_SCROLLED)
475 {
476 /* Entered command line, move it up */
477 cmdline_row--;
478 redrawcmd();
479 }
480 else if (save_p_ls != -1)
481 {
482 /* restore 'laststatus' and 'winminheight' */
483 p_ls = save_p_ls;
484 p_wmh = save_p_wmh;
485 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000486 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000488 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 redrawcmd();
490 save_p_ls = -1;
491 }
492 else
493 {
494# ifdef FEAT_VERTSPLIT
495 win_redraw_last_status(topframe);
496# else
497 lastwin->w_redr_status = TRUE;
498# endif
499 redraw_statuslines();
500 }
501 KeyTyped = skt;
502 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000503 if (ccline.input_fn)
504 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 }
506#endif
507 }
508
509#ifdef FEAT_WILDMENU
510 /* Special translations for 'wildmenu' */
511 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
512 {
513 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000514 if (c == K_DOWN && ccline.cmdpos > 0
515 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000517 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 {
519 /* Hitting <Up>: Remove one submenu name in front of the
520 * cursor */
521 int found = FALSE;
522
523 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
524 i = 0;
525 while (--j > 0)
526 {
527 /* check for start of menu name */
528 if (ccline.cmdbuff[j] == ' '
529 && ccline.cmdbuff[j - 1] != '\\')
530 {
531 i = j + 1;
532 break;
533 }
534 /* check for start of submenu name */
535 if (ccline.cmdbuff[j] == '.'
536 && ccline.cmdbuff[j - 1] != '\\')
537 {
538 if (found)
539 {
540 i = j + 1;
541 break;
542 }
543 else
544 found = TRUE;
545 }
546 }
547 if (i > 0)
548 cmdline_del(i);
549 c = p_wc;
550 xpc.xp_context = EXPAND_NOTHING;
551 }
552 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000553 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000554 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000555 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 {
557 char_u upseg[5];
558
559 upseg[0] = PATHSEP;
560 upseg[1] = '.';
561 upseg[2] = '.';
562 upseg[3] = PATHSEP;
563 upseg[4] = NUL;
564
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000565 if (c == K_DOWN
566 && ccline.cmdpos > 0
567 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
568 && (ccline.cmdpos < 3
569 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
571 {
572 /* go down a directory */
573 c = p_wc;
574 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000575 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {
577 /* If in a direct ancestor, strip off one ../ to go down */
578 int found = FALSE;
579
580 j = ccline.cmdpos;
581 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
582 while (--j > i)
583 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000584#ifdef FEAT_MBYTE
585 if (has_mbyte)
586 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 if (vim_ispathsep(ccline.cmdbuff[j]))
589 {
590 found = TRUE;
591 break;
592 }
593 }
594 if (found
595 && ccline.cmdbuff[j - 1] == '.'
596 && ccline.cmdbuff[j - 2] == '.'
597 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
598 {
599 cmdline_del(j - 2);
600 c = p_wc;
601 }
602 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000603 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
605 /* go up a directory */
606 int found = FALSE;
607
608 j = ccline.cmdpos - 1;
609 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
610 while (--j > i)
611 {
612#ifdef FEAT_MBYTE
613 if (has_mbyte)
614 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
615#endif
616 if (vim_ispathsep(ccline.cmdbuff[j])
617#ifdef BACKSLASH_IN_FILENAME
618 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
619 == NULL
620#endif
621 )
622 {
623 if (found)
624 {
625 i = j + 1;
626 break;
627 }
628 else
629 found = TRUE;
630 }
631 }
632
633 if (!found)
634 j = i;
635 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
636 j += 4;
637 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
638 && j == i)
639 j += 3;
640 else
641 j = 0;
642 if (j > 0)
643 {
644 /* TODO this is only for DOS/UNIX systems - need to put in
645 * machine-specific stuff here and in upseg init */
646 cmdline_del(j);
647 put_on_cmdline(upseg + 1, 3, FALSE);
648 }
649 else if (ccline.cmdpos > i)
650 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100651
652 /* Now complete in the new directory. Set KeyTyped in case the
653 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100655 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 }
657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658
659#endif /* FEAT_WILDMENU */
660
661 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
662 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
663 if (c == Ctrl_BSL)
664 {
665 ++no_mapping;
666 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000667 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 --no_mapping;
669 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200670 /* CTRL-\ e doesn't work when obtaining an expression, unless it
671 * is in a mapping. */
672 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
673 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
675 vungetc(c);
676 c = Ctrl_BSL;
677 }
678#ifdef FEAT_EVAL
679 else if (c == 'e')
680 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200681 char_u *p = NULL;
682 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
684 /*
685 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000686 * Need to save and restore the current command line, to be
687 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 */
689 if (ccline.cmdpos == ccline.cmdlen)
690 new_cmdpos = 99999; /* keep it at the end */
691 else
692 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000693
694 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000696 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 if (c == '=')
698 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000699 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000700 * to avoid nasty things like going to another buffer when
701 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000702 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000703 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000705 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000706 restore_cmdline(&save_ccline);
707
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200708 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200710 len = (int)STRLEN(p);
711 if (realloc_cmdbuff(len + 1) == OK)
712 {
713 ccline.cmdlen = len;
714 STRCPY(ccline.cmdbuff, p);
715 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200717 /* Restore the cursor or use the position set with
718 * set_cmdline_pos(). */
719 if (new_cmdpos > ccline.cmdlen)
720 ccline.cmdpos = ccline.cmdlen;
721 else
722 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200724 KeyTyped = FALSE; /* Don't do p_wc completion. */
725 redrawcmd();
726 goto cmdline_changed;
727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 }
729 }
730 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100731 got_int = FALSE; /* don't abandon the command line */
732 did_emsg = FALSE;
733 emsg_on_display = FALSE;
734 redrawcmd();
735 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
737#endif
738 else
739 {
740 if (c == Ctrl_G && p_im && restart_edit == 0)
741 restart_edit = 'a';
742 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
743 in history */
744 goto returncmd; /* back to Normal mode */
745 }
746 }
747
748#ifdef FEAT_CMDWIN
749 if (c == cedit_key || c == K_CMDWIN)
750 {
751 /*
752 * Open a window to edit the command line (and history).
753 */
754 c = ex_window();
755 some_key_typed = TRUE;
756 }
757# ifdef FEAT_DIGRAPHS
758 else
759# endif
760#endif
761#ifdef FEAT_DIGRAPHS
762 c = do_digraph(c);
763#endif
764
765 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
766 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
767 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000768 /* In Ex mode a backslash escapes a newline. */
769 if (exmode_active
770 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000771 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000772 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000773 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000775 if (c == K_KENTER)
776 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000778 else
779 {
780 gotesc = FALSE; /* Might have typed ESC previously, don't
781 truncate the cmdline now. */
782 if (ccheck_abbr(c + ABBR_OFF))
783 goto cmdline_changed;
784 if (!cmd_silent)
785 {
786 windgoto(msg_row, 0);
787 out_flush();
788 }
789 break;
790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
792
793 /*
794 * Completion for 'wildchar' or 'wildcharm' key.
795 * - hitting <ESC> twice means: abandon command line.
796 * - wildcard expansion is only done when the 'wildchar' key is really
797 * typed, not when it comes from a macro
798 */
799 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
800 {
801 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
802 {
803 /* if 'wildmode' contains "list" may still need to list */
804 if (xpc.xp_numfiles > 1
805 && !did_wild_list
806 && (wim_flags[wim_index] & WIM_LIST))
807 {
808 (void)showmatches(&xpc, FALSE);
809 redrawcmd();
810 did_wild_list = TRUE;
811 }
812 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100813 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
814 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100816 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
817 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 else
819 res = OK; /* don't insert 'wildchar' now */
820 }
821 else /* typed p_wc first time */
822 {
823 wim_index = 0;
824 j = ccline.cmdpos;
825 /* if 'wildmode' first contains "longest", get longest
826 * common part */
827 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100828 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
829 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100831 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
832 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834 /* if interrupted while completing, behave like it failed */
835 if (got_int)
836 {
837 (void)vpeekc(); /* remove <C-C> from input stream */
838 got_int = FALSE; /* don't abandon the command line */
839 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
840#ifdef FEAT_WILDMENU
841 xpc.xp_context = EXPAND_NOTHING;
842#endif
843 goto cmdline_changed;
844 }
845
846 /* when more than one match, and 'wildmode' first contains
847 * "list", or no change and 'wildmode' contains "longest,list",
848 * list all matches */
849 if (res == OK && xpc.xp_numfiles > 1)
850 {
851 /* a "longest" that didn't do anything is skipped (but not
852 * "list:longest") */
853 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
854 wim_index = 1;
855 if ((wim_flags[wim_index] & WIM_LIST)
856#ifdef FEAT_WILDMENU
857 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
858#endif
859 )
860 {
861 if (!(wim_flags[0] & WIM_LONGEST))
862 {
863#ifdef FEAT_WILDMENU
864 int p_wmnu_save = p_wmnu;
865 p_wmnu = 0;
866#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100867 /* remove match */
868 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#ifdef FEAT_WILDMENU
870 p_wmnu = p_wmnu_save;
871#endif
872 }
873#ifdef FEAT_WILDMENU
874 (void)showmatches(&xpc, p_wmnu
875 && ((wim_flags[wim_index] & WIM_LIST) == 0));
876#else
877 (void)showmatches(&xpc, FALSE);
878#endif
879 redrawcmd();
880 did_wild_list = TRUE;
881 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100882 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
883 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100885 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
886 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 }
888 else
889 vim_beep();
890 }
891#ifdef FEAT_WILDMENU
892 else if (xpc.xp_numfiles == -1)
893 xpc.xp_context = EXPAND_NOTHING;
894#endif
895 }
896 if (wim_index < 3)
897 ++wim_index;
898 if (c == ESC)
899 gotesc = TRUE;
900 if (res == OK)
901 goto cmdline_changed;
902 }
903
904 gotesc = FALSE;
905
906 /* <S-Tab> goes to last match, in a clumsy way */
907 if (c == K_S_TAB && KeyTyped)
908 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100909 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
910 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
911 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 goto cmdline_changed;
913 }
914
915 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
916 c = NL;
917
918 do_abbr = TRUE; /* default: check for abbreviation */
919
920 /*
921 * Big switch for a typed command line character.
922 */
923 switch (c)
924 {
925 case K_BS:
926 case Ctrl_H:
927 case K_DEL:
928 case K_KDEL:
929 case Ctrl_W:
930#ifdef FEAT_FKMAP
931 if (cmd_fkmap && c == K_BS)
932 c = K_DEL;
933#endif
934 if (c == K_KDEL)
935 c = K_DEL;
936
937 /*
938 * delete current character is the same as backspace on next
939 * character, except at end of line
940 */
941 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
942 ++ccline.cmdpos;
943#ifdef FEAT_MBYTE
944 if (has_mbyte && c == K_DEL)
945 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
946 ccline.cmdbuff + ccline.cmdpos);
947#endif
948 if (ccline.cmdpos > 0)
949 {
950 char_u *p;
951
952 j = ccline.cmdpos;
953 p = ccline.cmdbuff + j;
954#ifdef FEAT_MBYTE
955 if (has_mbyte)
956 {
957 p = mb_prevptr(ccline.cmdbuff, p);
958 if (c == Ctrl_W)
959 {
960 while (p > ccline.cmdbuff && vim_isspace(*p))
961 p = mb_prevptr(ccline.cmdbuff, p);
962 i = mb_get_class(p);
963 while (p > ccline.cmdbuff && mb_get_class(p) == i)
964 p = mb_prevptr(ccline.cmdbuff, p);
965 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000966 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968 }
969 else
970#endif
971 if (c == Ctrl_W)
972 {
973 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
974 --p;
975 i = vim_iswordc(p[-1]);
976 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
977 && vim_iswordc(p[-1]) == i)
978 --p;
979 }
980 else
981 --p;
982 ccline.cmdpos = (int)(p - ccline.cmdbuff);
983 ccline.cmdlen -= j - ccline.cmdpos;
984 i = ccline.cmdpos;
985 while (i < ccline.cmdlen)
986 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
987
988 /* Truncate at the end, required for multi-byte chars. */
989 ccline.cmdbuff[ccline.cmdlen] = NUL;
990 redrawcmd();
991 }
992 else if (ccline.cmdlen == 0 && c != Ctrl_W
993 && ccline.cmdprompt == NULL && indent == 0)
994 {
995 /* In ex and debug mode it doesn't make sense to return. */
996 if (exmode_active
997#ifdef FEAT_EVAL
998 || ccline.cmdfirstc == '>'
999#endif
1000 )
1001 goto cmdline_not_changed;
1002
1003 vim_free(ccline.cmdbuff); /* no commandline to return */
1004 ccline.cmdbuff = NULL;
1005 if (!cmd_silent)
1006 {
1007#ifdef FEAT_RIGHTLEFT
1008 if (cmdmsg_rl)
1009 msg_col = Columns;
1010 else
1011#endif
1012 msg_col = 0;
1013 msg_putchar(' '); /* delete ':' */
1014 }
1015 redraw_cmdline = TRUE;
1016 goto returncmd; /* back to cmd mode */
1017 }
1018 goto cmdline_changed;
1019
1020 case K_INS:
1021 case K_KINS:
1022#ifdef FEAT_FKMAP
1023 /* if Farsi mode set, we are in reverse insert mode -
1024 Do not change the mode */
1025 if (cmd_fkmap)
1026 beep_flush();
1027 else
1028#endif
1029 ccline.overstrike = !ccline.overstrike;
1030#ifdef CURSOR_SHAPE
1031 ui_cursor_shape(); /* may show different cursor shape */
1032#endif
1033 goto cmdline_not_changed;
1034
1035 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001036 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {
1038 /* ":lmap" mappings exists, toggle use of mappings. */
1039 State ^= LANGMAP;
1040#ifdef USE_IM_CONTROL
1041 im_set_active(FALSE); /* Disable input method */
1042#endif
1043 if (b_im_ptr != NULL)
1044 {
1045 if (State & LANGMAP)
1046 *b_im_ptr = B_IMODE_LMAP;
1047 else
1048 *b_im_ptr = B_IMODE_NONE;
1049 }
1050 }
1051#ifdef USE_IM_CONTROL
1052 else
1053 {
1054 /* There are no ":lmap" mappings, toggle IM. When
1055 * 'imdisable' is set don't try getting the status, it's
1056 * always off. */
1057 if ((p_imdisable && b_im_ptr != NULL)
1058 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1059 {
1060 im_set_active(FALSE); /* Disable input method */
1061 if (b_im_ptr != NULL)
1062 *b_im_ptr = B_IMODE_NONE;
1063 }
1064 else
1065 {
1066 im_set_active(TRUE); /* Enable input method */
1067 if (b_im_ptr != NULL)
1068 *b_im_ptr = B_IMODE_IM;
1069 }
1070 }
1071#endif
1072 if (b_im_ptr != NULL)
1073 {
1074 if (b_im_ptr == &curbuf->b_p_iminsert)
1075 set_iminsert_global();
1076 else
1077 set_imsearch_global();
1078 }
1079#ifdef CURSOR_SHAPE
1080 ui_cursor_shape(); /* may show different cursor shape */
1081#endif
1082#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1083 /* Show/unshow value of 'keymap' in status lines later. */
1084 status_redraw_curbuf();
1085#endif
1086 goto cmdline_not_changed;
1087
1088/* case '@': only in very old vi */
1089 case Ctrl_U:
1090 /* delete all characters left of the cursor */
1091 j = ccline.cmdpos;
1092 ccline.cmdlen -= j;
1093 i = ccline.cmdpos = 0;
1094 while (i < ccline.cmdlen)
1095 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1096 /* Truncate at the end, required for multi-byte chars. */
1097 ccline.cmdbuff[ccline.cmdlen] = NUL;
1098 redrawcmd();
1099 goto cmdline_changed;
1100
1101#ifdef FEAT_CLIPBOARD
1102 case Ctrl_Y:
1103 /* Copy the modeless selection, if there is one. */
1104 if (clip_star.state != SELECT_CLEARED)
1105 {
1106 if (clip_star.state == SELECT_DONE)
1107 clip_copy_modeless_selection(TRUE);
1108 goto cmdline_not_changed;
1109 }
1110 break;
1111#endif
1112
1113 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1114 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001115 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001116 * ":normal" runs out of characters. */
1117 if (exmode_active
1118#ifdef FEAT_EX_EXTRA
1119 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1120#endif
1121 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 goto cmdline_not_changed;
1123
1124 gotesc = TRUE; /* will free ccline.cmdbuff after
1125 putting it in history */
1126 goto returncmd; /* back to cmd mode */
1127
1128 case Ctrl_R: /* insert register */
1129#ifdef USE_ON_FLY_SCROLL
1130 dont_scroll = TRUE; /* disallow scrolling here */
1131#endif
1132 putcmdline('"', TRUE);
1133 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001134 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 if (i == Ctrl_O)
1136 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1137 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001138 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 --no_mapping;
1140#ifdef FEAT_EVAL
1141 /*
1142 * Insert the result of an expression.
1143 * Need to save the current command line, to be able to enter
1144 * a new one...
1145 */
1146 new_cmdpos = -1;
1147 if (c == '=')
1148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1150 {
1151 beep_flush();
1152 c = ESC;
1153 }
1154 else
1155 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001156 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001158 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
1160 }
1161#endif
1162 if (c != ESC) /* use ESC to cancel inserting register */
1163 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001164 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001165
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001166#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001167 /* When there was a serious error abort getting the
1168 * command line. */
1169 if (aborting())
1170 {
1171 gotesc = TRUE; /* will free ccline.cmdbuff after
1172 putting it in history */
1173 goto returncmd; /* back to cmd mode */
1174 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 KeyTyped = FALSE; /* Don't do p_wc completion. */
1177#ifdef FEAT_EVAL
1178 if (new_cmdpos >= 0)
1179 {
1180 /* set_cmdline_pos() was used */
1181 if (new_cmdpos > ccline.cmdlen)
1182 ccline.cmdpos = ccline.cmdlen;
1183 else
1184 ccline.cmdpos = new_cmdpos;
1185 }
1186#endif
1187 }
1188 redrawcmd();
1189 goto cmdline_changed;
1190
1191 case Ctrl_D:
1192 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1193 break; /* Use ^D as normal char instead */
1194
1195 redrawcmd();
1196 continue; /* don't do incremental search now */
1197
1198 case K_RIGHT:
1199 case K_S_RIGHT:
1200 case K_C_RIGHT:
1201 do
1202 {
1203 if (ccline.cmdpos >= ccline.cmdlen)
1204 break;
1205 i = cmdline_charsize(ccline.cmdpos);
1206 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1207 break;
1208 ccline.cmdspos += i;
1209#ifdef FEAT_MBYTE
1210 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001211 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 + ccline.cmdpos);
1213 else
1214#endif
1215 ++ccline.cmdpos;
1216 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001217 while ((c == K_S_RIGHT || c == K_C_RIGHT
1218 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1220#ifdef FEAT_MBYTE
1221 if (has_mbyte)
1222 set_cmdspos_cursor();
1223#endif
1224 goto cmdline_not_changed;
1225
1226 case K_LEFT:
1227 case K_S_LEFT:
1228 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001229 if (ccline.cmdpos == 0)
1230 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 do
1232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 --ccline.cmdpos;
1234#ifdef FEAT_MBYTE
1235 if (has_mbyte) /* move to first byte of char */
1236 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1237 ccline.cmdbuff + ccline.cmdpos);
1238#endif
1239 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1240 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001241 while (ccline.cmdpos > 0
1242 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001243 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1245#ifdef FEAT_MBYTE
1246 if (has_mbyte)
1247 set_cmdspos_cursor();
1248#endif
1249 goto cmdline_not_changed;
1250
1251 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001252 /* Ignore mouse event or ex_window() result. */
1253 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254
Bram Moolenaar4770d092006-01-12 23:22:24 +00001255#ifdef FEAT_GUI_W32
1256 /* On Win32 ignore <M-F4>, we get it when closing the window was
1257 * cancelled. */
1258 case K_F4:
1259 if (mod_mask == MOD_MASK_ALT)
1260 {
1261 redrawcmd(); /* somehow the cmdline is cleared */
1262 goto cmdline_not_changed;
1263 }
1264 break;
1265#endif
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267#ifdef FEAT_MOUSE
1268 case K_MIDDLEDRAG:
1269 case K_MIDDLERELEASE:
1270 goto cmdline_not_changed; /* Ignore mouse */
1271
1272 case K_MIDDLEMOUSE:
1273# ifdef FEAT_GUI
1274 /* When GUI is active, also paste when 'mouse' is empty */
1275 if (!gui.in_use)
1276# endif
1277 if (!mouse_has(MOUSE_COMMAND))
1278 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001279# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001281 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001283# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001284 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 redrawcmd();
1286 goto cmdline_changed;
1287
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001288# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001290 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 redrawcmd();
1292 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
1295 case K_LEFTDRAG:
1296 case K_LEFTRELEASE:
1297 case K_RIGHTDRAG:
1298 case K_RIGHTRELEASE:
1299 /* Ignore drag and release events when the button-down wasn't
1300 * seen before. */
1301 if (ignore_drag_release)
1302 goto cmdline_not_changed;
1303 /* FALLTHROUGH */
1304 case K_LEFTMOUSE:
1305 case K_RIGHTMOUSE:
1306 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1307 ignore_drag_release = TRUE;
1308 else
1309 ignore_drag_release = FALSE;
1310# ifdef FEAT_GUI
1311 /* When GUI is active, also move when 'mouse' is empty */
1312 if (!gui.in_use)
1313# endif
1314 if (!mouse_has(MOUSE_COMMAND))
1315 goto cmdline_not_changed; /* Ignore mouse */
1316# ifdef FEAT_CLIPBOARD
1317 if (mouse_row < cmdline_row && clip_star.available)
1318 {
1319 int button, is_click, is_drag;
1320
1321 /*
1322 * Handle modeless selection.
1323 */
1324 button = get_mouse_button(KEY2TERMCAP1(c),
1325 &is_click, &is_drag);
1326 if (mouse_model_popup() && button == MOUSE_LEFT
1327 && (mod_mask & MOD_MASK_SHIFT))
1328 {
1329 /* Translate shift-left to right button. */
1330 button = MOUSE_RIGHT;
1331 mod_mask &= ~MOD_MASK_SHIFT;
1332 }
1333 clip_modeless(button, is_click, is_drag);
1334 goto cmdline_not_changed;
1335 }
1336# endif
1337
1338 set_cmdspos();
1339 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1340 ++ccline.cmdpos)
1341 {
1342 i = cmdline_charsize(ccline.cmdpos);
1343 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1344 && mouse_col < ccline.cmdspos % Columns + i)
1345 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001346# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 if (has_mbyte)
1348 {
1349 /* Count ">" for double-wide char that doesn't fit. */
1350 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001351 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 + ccline.cmdpos) - 1;
1353 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 ccline.cmdspos += i;
1356 }
1357 goto cmdline_not_changed;
1358
1359 /* Mouse scroll wheel: ignored here */
1360 case K_MOUSEDOWN:
1361 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001362 case K_MOUSELEFT:
1363 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 /* Alternate buttons ignored here */
1365 case K_X1MOUSE:
1366 case K_X1DRAG:
1367 case K_X1RELEASE:
1368 case K_X2MOUSE:
1369 case K_X2DRAG:
1370 case K_X2RELEASE:
1371 goto cmdline_not_changed;
1372
1373#endif /* FEAT_MOUSE */
1374
1375#ifdef FEAT_GUI
1376 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1377 case K_LEFTRELEASE_NM:
1378 goto cmdline_not_changed;
1379
1380 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001381 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
1383 gui_do_scroll();
1384 redrawcmd();
1385 }
1386 goto cmdline_not_changed;
1387
1388 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001389 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001391 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 redrawcmd();
1393 }
1394 goto cmdline_not_changed;
1395#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001396#ifdef FEAT_GUI_TABLINE
1397 case K_TABLINE:
1398 case K_TABMENU:
1399 /* Don't want to change any tabs here. Make sure the same tab
1400 * is still selected. */
1401 if (gui_use_tabline())
1402 gui_mch_set_curtab(tabpage_index(curtab));
1403 goto cmdline_not_changed;
1404#endif
1405
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 case K_SELECT: /* end of Select mode mapping - ignore */
1407 goto cmdline_not_changed;
1408
1409 case Ctrl_B: /* begin of command line */
1410 case K_HOME:
1411 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 case K_S_HOME:
1413 case K_C_HOME:
1414 ccline.cmdpos = 0;
1415 set_cmdspos();
1416 goto cmdline_not_changed;
1417
1418 case Ctrl_E: /* end of command line */
1419 case K_END:
1420 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 case K_S_END:
1422 case K_C_END:
1423 ccline.cmdpos = ccline.cmdlen;
1424 set_cmdspos_cursor();
1425 goto cmdline_not_changed;
1426
1427 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001428 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 break;
1430 goto cmdline_changed;
1431
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001432 case Ctrl_L:
1433#ifdef FEAT_SEARCH_EXTRA
1434 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1435 {
1436 /* Add a character from under the cursor for 'incsearch' */
1437 if (did_incsearch
1438 && !equalpos(curwin->w_cursor, old_cursor))
1439 {
1440 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001441 /* If 'ignorecase' and 'smartcase' are set and the
1442 * command line has no uppercase characters, convert
1443 * the character to lowercase */
1444 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1445 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001446 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001447 {
1448 if (c == firstc || vim_strchr((char_u *)(
1449 p_magic ? "\\^$.*[" : "\\^$"), c)
1450 != NULL)
1451 {
1452 /* put a backslash before special characters */
1453 stuffcharReadbuff(c);
1454 c = '\\';
1455 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001456 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001457 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001458 }
1459 goto cmdline_not_changed;
1460 }
1461#endif
1462
1463 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001464 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 break;
1466 goto cmdline_changed;
1467
1468 case Ctrl_N: /* next match */
1469 case Ctrl_P: /* previous match */
1470 if (xpc.xp_numfiles > 0)
1471 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001472 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1473 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 break;
1475 goto cmdline_changed;
1476 }
1477
1478#ifdef FEAT_CMDHIST
1479 case K_UP:
1480 case K_DOWN:
1481 case K_S_UP:
1482 case K_S_DOWN:
1483 case K_PAGEUP:
1484 case K_KPAGEUP:
1485 case K_PAGEDOWN:
1486 case K_KPAGEDOWN:
1487 if (hislen == 0 || firstc == NUL) /* no history */
1488 goto cmdline_not_changed;
1489
1490 i = hiscnt;
1491
1492 /* save current command string so it can be restored later */
1493 if (lookfor == NULL)
1494 {
1495 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1496 goto cmdline_not_changed;
1497 lookfor[ccline.cmdpos] = NUL;
1498 }
1499
1500 j = (int)STRLEN(lookfor);
1501 for (;;)
1502 {
1503 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001504 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001505 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
1507 if (hiscnt == hislen) /* first time */
1508 hiscnt = hisidx[histype];
1509 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1510 hiscnt = hislen - 1;
1511 else if (hiscnt != hisidx[histype] + 1)
1512 --hiscnt;
1513 else /* at top of list */
1514 {
1515 hiscnt = i;
1516 break;
1517 }
1518 }
1519 else /* one step forwards */
1520 {
1521 /* on last entry, clear the line */
1522 if (hiscnt == hisidx[histype])
1523 {
1524 hiscnt = hislen;
1525 break;
1526 }
1527
1528 /* not on a history line, nothing to do */
1529 if (hiscnt == hislen)
1530 break;
1531 if (hiscnt == hislen - 1) /* wrap around */
1532 hiscnt = 0;
1533 else
1534 ++hiscnt;
1535 }
1536 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1537 {
1538 hiscnt = i;
1539 break;
1540 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001541 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001542 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 || STRNCMP(history[histype][hiscnt].hisstr,
1544 lookfor, (size_t)j) == 0)
1545 break;
1546 }
1547
1548 if (hiscnt != i) /* jumped to other entry */
1549 {
1550 char_u *p;
1551 int len;
1552 int old_firstc;
1553
1554 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001555 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 if (hiscnt == hislen)
1557 p = lookfor; /* back to the old one */
1558 else
1559 p = history[histype][hiscnt].hisstr;
1560
1561 if (histype == HIST_SEARCH
1562 && p != lookfor
1563 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1564 {
1565 /* Correct for the separator character used when
1566 * adding the history entry vs the one used now.
1567 * First loop: count length.
1568 * Second loop: copy the characters. */
1569 for (i = 0; i <= 1; ++i)
1570 {
1571 len = 0;
1572 for (j = 0; p[j] != NUL; ++j)
1573 {
1574 /* Replace old sep with new sep, unless it is
1575 * escaped. */
1576 if (p[j] == old_firstc
1577 && (j == 0 || p[j - 1] != '\\'))
1578 {
1579 if (i > 0)
1580 ccline.cmdbuff[len] = firstc;
1581 }
1582 else
1583 {
1584 /* Escape new sep, unless it is already
1585 * escaped. */
1586 if (p[j] == firstc
1587 && (j == 0 || p[j - 1] != '\\'))
1588 {
1589 if (i > 0)
1590 ccline.cmdbuff[len] = '\\';
1591 ++len;
1592 }
1593 if (i > 0)
1594 ccline.cmdbuff[len] = p[j];
1595 }
1596 ++len;
1597 }
1598 if (i == 0)
1599 {
1600 alloc_cmdbuff(len);
1601 if (ccline.cmdbuff == NULL)
1602 goto returncmd;
1603 }
1604 }
1605 ccline.cmdbuff[len] = NUL;
1606 }
1607 else
1608 {
1609 alloc_cmdbuff((int)STRLEN(p));
1610 if (ccline.cmdbuff == NULL)
1611 goto returncmd;
1612 STRCPY(ccline.cmdbuff, p);
1613 }
1614
1615 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1616 redrawcmd();
1617 goto cmdline_changed;
1618 }
1619 beep_flush();
1620 goto cmdline_not_changed;
1621#endif
1622
1623 case Ctrl_V:
1624 case Ctrl_Q:
1625#ifdef FEAT_MOUSE
1626 ignore_drag_release = TRUE;
1627#endif
1628 putcmdline('^', TRUE);
1629 c = get_literal(); /* get next (two) character(s) */
1630 do_abbr = FALSE; /* don't do abbreviation now */
1631#ifdef FEAT_MBYTE
1632 /* may need to remove ^ when composing char was typed */
1633 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1634 {
1635 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1636 msg_putchar(' ');
1637 cursorcmd();
1638 }
1639#endif
1640 break;
1641
1642#ifdef FEAT_DIGRAPHS
1643 case Ctrl_K:
1644#ifdef FEAT_MOUSE
1645 ignore_drag_release = TRUE;
1646#endif
1647 putcmdline('?', TRUE);
1648#ifdef USE_ON_FLY_SCROLL
1649 dont_scroll = TRUE; /* disallow scrolling here */
1650#endif
1651 c = get_digraph(TRUE);
1652 if (c != NUL)
1653 break;
1654
1655 redrawcmd();
1656 goto cmdline_not_changed;
1657#endif /* FEAT_DIGRAPHS */
1658
1659#ifdef FEAT_RIGHTLEFT
1660 case Ctrl__: /* CTRL-_: switch language mode */
1661 if (!p_ari)
1662 break;
1663#ifdef FEAT_FKMAP
1664 if (p_altkeymap)
1665 {
1666 cmd_fkmap = !cmd_fkmap;
1667 if (cmd_fkmap) /* in Farsi always in Insert mode */
1668 ccline.overstrike = FALSE;
1669 }
1670 else /* Hebrew is default */
1671#endif
1672 cmd_hkmap = !cmd_hkmap;
1673 goto cmdline_not_changed;
1674#endif
1675
1676 default:
1677#ifdef UNIX
1678 if (c == intr_char)
1679 {
1680 gotesc = TRUE; /* will free ccline.cmdbuff after
1681 putting it in history */
1682 goto returncmd; /* back to Normal mode */
1683 }
1684#endif
1685 /*
1686 * Normal character with no special meaning. Just set mod_mask
1687 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1688 * the string <S-Space>. This should only happen after ^V.
1689 */
1690 if (!IS_SPECIAL(c))
1691 mod_mask = 0x0;
1692 break;
1693 }
1694 /*
1695 * End of switch on command line character.
1696 * We come here if we have a normal character.
1697 */
1698
1699 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1700#ifdef FEAT_MBYTE
1701 /* Add ABBR_OFF for characters above 0x100, this is
1702 * what check_abbr() expects. */
1703 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1704#endif
1705 c))
1706 goto cmdline_changed;
1707
1708 /*
1709 * put the character in the command line
1710 */
1711 if (IS_SPECIAL(c) || mod_mask != 0)
1712 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1713 else
1714 {
1715#ifdef FEAT_MBYTE
1716 if (has_mbyte)
1717 {
1718 j = (*mb_char2bytes)(c, IObuff);
1719 IObuff[j] = NUL; /* exclude composing chars */
1720 put_on_cmdline(IObuff, j, TRUE);
1721 }
1722 else
1723#endif
1724 {
1725 IObuff[0] = c;
1726 put_on_cmdline(IObuff, 1, TRUE);
1727 }
1728 }
1729 goto cmdline_changed;
1730
1731/*
1732 * This part implements incremental searches for "/" and "?"
1733 * Jump to cmdline_not_changed when a character has been read but the command
1734 * line did not change. Then we only search and redraw if something changed in
1735 * the past.
1736 * Jump to cmdline_changed when the command line did change.
1737 * (Sorry for the goto's, I know it is ugly).
1738 */
1739cmdline_not_changed:
1740#ifdef FEAT_SEARCH_EXTRA
1741 if (!incsearch_postponed)
1742 continue;
1743#endif
1744
1745cmdline_changed:
1746#ifdef FEAT_SEARCH_EXTRA
1747 /*
1748 * 'incsearch' highlighting.
1749 */
1750 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1751 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001752 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001753#ifdef FEAT_RELTIME
1754 proftime_T tm;
1755#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001756
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 /* if there is a character waiting, search and redraw later */
1758 if (char_avail())
1759 {
1760 incsearch_postponed = TRUE;
1761 continue;
1762 }
1763 incsearch_postponed = FALSE;
1764 curwin->w_cursor = old_cursor; /* start at old position */
1765
1766 /* If there is no command line, don't do anything */
1767 if (ccline.cmdlen == 0)
1768 i = 0;
1769 else
1770 {
1771 cursor_off(); /* so the user knows we're busy */
1772 out_flush();
1773 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001774#ifdef FEAT_RELTIME
1775 /* Set the time limit to half a second. */
1776 profile_setlimit(500L, &tm);
1777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001779 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1780#ifdef FEAT_RELTIME
1781 &tm
1782#else
1783 NULL
1784#endif
1785 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 --emsg_off;
1787 /* if interrupted while searching, behave like it failed */
1788 if (got_int)
1789 {
1790 (void)vpeekc(); /* remove <C-C> from input stream */
1791 got_int = FALSE; /* don't abandon the command line */
1792 i = 0;
1793 }
1794 else if (char_avail())
1795 /* cancelled searching because a char was typed */
1796 incsearch_postponed = TRUE;
1797 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001798 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 highlight_match = TRUE; /* highlight position */
1800 else
1801 highlight_match = FALSE; /* remove highlight */
1802
1803 /* first restore the old curwin values, so the screen is
1804 * positioned in the same way as the actual search command */
1805 curwin->w_leftcol = old_leftcol;
1806 curwin->w_topline = old_topline;
1807# ifdef FEAT_DIFF
1808 curwin->w_topfill = old_topfill;
1809# endif
1810 curwin->w_botline = old_botline;
1811 changed_cline_bef_curs();
1812 update_topline();
1813
1814 if (i != 0)
1815 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001816 pos_T save_pos = curwin->w_cursor;
1817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001819 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 * moves the whole match onto the screen when 'nowrap' is set.
1821 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 curwin->w_cursor.lnum += search_match_lines;
1823 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001824 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1825 {
1826 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1827 coladvance((colnr_T)MAXCOL);
1828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001830 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001831 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001833 else
1834 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001837# ifdef FEAT_WINDOWS
1838 /* May redraw the status line to show the cursor position. */
1839 if (p_ru && curwin->w_status_height > 0)
1840 curwin->w_redr_status = TRUE;
1841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001843 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001844 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001845 restore_cmdline(&save_ccline);
1846
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001847 /* Leave it at the end to make CTRL-R CTRL-W work. */
1848 if (i != 0)
1849 curwin->w_cursor = end_pos;
1850
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 msg_starthere();
1852 redrawcmdline();
1853 did_incsearch = TRUE;
1854 }
1855#else /* FEAT_SEARCH_EXTRA */
1856 ;
1857#endif
1858
1859#ifdef FEAT_RIGHTLEFT
1860 if (cmdmsg_rl
1861# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001862 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863# endif
1864 )
1865 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001866 * right-left typing. Not efficient, but it works.
1867 * Do it only when there are no characters left to read
1868 * to avoid useless intermediate redraws. */
1869 if (vpeekc() == NUL)
1870 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871#endif
1872 }
1873
1874returncmd:
1875
1876#ifdef FEAT_RIGHTLEFT
1877 cmdmsg_rl = FALSE;
1878#endif
1879
1880#ifdef FEAT_FKMAP
1881 cmd_fkmap = 0;
1882#endif
1883
1884 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001885 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
1887#ifdef FEAT_SEARCH_EXTRA
1888 if (did_incsearch)
1889 {
1890 curwin->w_cursor = old_cursor;
1891 curwin->w_curswant = old_curswant;
1892 curwin->w_leftcol = old_leftcol;
1893 curwin->w_topline = old_topline;
1894# ifdef FEAT_DIFF
1895 curwin->w_topfill = old_topfill;
1896# endif
1897 curwin->w_botline = old_botline;
1898 highlight_match = FALSE;
1899 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001900 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 }
1902#endif
1903
1904 if (ccline.cmdbuff != NULL)
1905 {
1906 /*
1907 * Put line in history buffer (":" and "=" only when it was typed).
1908 */
1909#ifdef FEAT_CMDHIST
1910 if (ccline.cmdlen && firstc != NUL
1911 && (some_key_typed || histype == HIST_SEARCH))
1912 {
1913 add_to_history(histype, ccline.cmdbuff, TRUE,
1914 histype == HIST_SEARCH ? firstc : NUL);
1915 if (firstc == ':')
1916 {
1917 vim_free(new_last_cmdline);
1918 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1919 }
1920 }
1921#endif
1922
1923 if (gotesc) /* abandon command line */
1924 {
1925 vim_free(ccline.cmdbuff);
1926 ccline.cmdbuff = NULL;
1927 if (msg_scrolled == 0)
1928 compute_cmdrow();
1929 MSG("");
1930 redraw_cmdline = TRUE;
1931 }
1932 }
1933
1934 /*
1935 * If the screen was shifted up, redraw the whole screen (later).
1936 * If the line is too long, clear it, so ruler and shown command do
1937 * not get printed in the middle of it.
1938 */
1939 msg_check();
1940 msg_scroll = save_msg_scroll;
1941 redir_off = FALSE;
1942
1943 /* When the command line was typed, no need for a wait-return prompt. */
1944 if (some_key_typed)
1945 need_wait_return = FALSE;
1946
1947 State = save_State;
1948#ifdef USE_IM_CONTROL
1949 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1950 im_save_status(b_im_ptr);
1951 im_set_active(FALSE);
1952#endif
1953#ifdef FEAT_MOUSE
1954 setmouse();
1955#endif
1956#ifdef CURSOR_SHAPE
1957 ui_cursor_shape(); /* may show different cursor shape */
1958#endif
1959
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001960 {
1961 char_u *p = ccline.cmdbuff;
1962
1963 /* Make ccline empty, getcmdline() may try to use it. */
1964 ccline.cmdbuff = NULL;
1965 return p;
1966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967}
1968
1969#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1970/*
1971 * Get a command line with a prompt.
1972 * This is prepared to be called recursively from getcmdline() (e.g. by
1973 * f_input() when evaluating an expression from CTRL-R =).
1974 * Returns the command line in allocated memory, or NULL.
1975 */
1976 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001977getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 int firstc;
1979 char_u *prompt; /* command line prompt */
1980 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001981 int xp_context; /* type of expansion */
1982 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983{
1984 char_u *s;
1985 struct cmdline_info save_ccline;
1986 int msg_col_save = msg_col;
1987
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001988 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 ccline.cmdprompt = prompt;
1990 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001991# ifdef FEAT_EVAL
1992 ccline.xp_context = xp_context;
1993 ccline.xp_arg = xp_arg;
1994 ccline.input_fn = (firstc == '@');
1995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001997 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02001998 /* Restore msg_col, the prompt from input() may have changed it.
1999 * But only if called recursively and the commandline is therefore being
2000 * restored to an old one; if not, the input() prompt stays on the screen,
2001 * so we need its modified msg_col left intact. */
2002 if (ccline.cmdbuff != NULL)
2003 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
2005 return s;
2006}
2007#endif
2008
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002009/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002010 * Return TRUE when the text must not be changed and we can't switch to
2011 * another window or buffer. Used when editing the command line, evaluating
2012 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002013 */
2014 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002015text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002016{
2017#ifdef FEAT_CMDWIN
2018 if (cmdwin_type != 0)
2019 return TRUE;
2020#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002021 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002022}
2023
2024/*
2025 * Give an error message for a command that isn't allowed while the cmdline
2026 * window is open or editing the cmdline in another way.
2027 */
2028 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002029text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002030{
2031#ifdef FEAT_CMDWIN
2032 if (cmdwin_type != 0)
2033 EMSG(_(e_cmdwin));
2034 else
2035#endif
2036 EMSG(_(e_secure));
2037}
2038
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002039#if defined(FEAT_AUTOCMD) || defined(PROTO)
2040/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002041 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2042 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002043 */
2044 int
2045curbuf_locked()
2046{
2047 if (curbuf_lock > 0)
2048 {
2049 EMSG(_("E788: Not allowed to edit another buffer now"));
2050 return TRUE;
2051 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002052 return allbuf_locked();
2053}
2054
2055/*
2056 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2057 * message.
2058 */
2059 int
2060allbuf_locked()
2061{
2062 if (allbuf_lock > 0)
2063 {
2064 EMSG(_("E811: Not allowed to change buffer information now"));
2065 return TRUE;
2066 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002067 return FALSE;
2068}
2069#endif
2070
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 static int
2072cmdline_charsize(idx)
2073 int idx;
2074{
2075#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2076 if (cmdline_star > 0) /* showing '*', always 1 position */
2077 return 1;
2078#endif
2079 return ptr2cells(ccline.cmdbuff + idx);
2080}
2081
2082/*
2083 * Compute the offset of the cursor on the command line for the prompt and
2084 * indent.
2085 */
2086 static void
2087set_cmdspos()
2088{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002089 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 ccline.cmdspos = 1 + ccline.cmdindent;
2091 else
2092 ccline.cmdspos = 0 + ccline.cmdindent;
2093}
2094
2095/*
2096 * Compute the screen position for the cursor on the command line.
2097 */
2098 static void
2099set_cmdspos_cursor()
2100{
2101 int i, m, c;
2102
2103 set_cmdspos();
2104 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002107 if (m < 0) /* overflow, Columns or Rows at weird value */
2108 m = MAXCOL;
2109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 else
2111 m = MAXCOL;
2112 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2113 {
2114 c = cmdline_charsize(i);
2115#ifdef FEAT_MBYTE
2116 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2117 if (has_mbyte)
2118 correct_cmdspos(i, c);
2119#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002120 /* If the cmdline doesn't fit, show cursor on last visible char.
2121 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 if ((ccline.cmdspos += c) >= m)
2123 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 ccline.cmdspos -= c;
2125 break;
2126 }
2127#ifdef FEAT_MBYTE
2128 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002129 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130#endif
2131 }
2132}
2133
2134#ifdef FEAT_MBYTE
2135/*
2136 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2137 * character that doesn't fit, so that a ">" must be displayed.
2138 */
2139 static void
2140correct_cmdspos(idx, cells)
2141 int idx;
2142 int cells;
2143{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002144 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2146 && ccline.cmdspos % Columns + cells > Columns)
2147 ccline.cmdspos++;
2148}
2149#endif
2150
2151/*
2152 * Get an Ex command line for the ":" command.
2153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002155getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002157 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 int indent; /* indent for inside conditionals */
2159{
2160 /* When executing a register, remove ':' that's in front of each line. */
2161 if (exec_from_reg && vpeekc() == ':')
2162 (void)vgetc();
2163 return getcmdline(c, 1L, indent);
2164}
2165
2166/*
2167 * Get an Ex command line for Ex mode.
2168 * In Ex mode we only use the OS supplied line editing features and no
2169 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002170 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002173getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002174 int promptc; /* normally ':', NUL for ":append" and '?' for
2175 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002176 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 int indent; /* indent for inside conditionals */
2178{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002179 garray_T line_ga;
2180 char_u *pend;
2181 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002182 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002183 int escaped = FALSE; /* CTRL-V typed */
2184 int vcol = 0;
2185 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002186 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 /* Switch cursor on now. This avoids that it happens after the "\n", which
2189 * confuses the system function that computes tabstops. */
2190 cursor_on();
2191
2192 /* always start in column 0; write a newline if necessary */
2193 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002194 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002196 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002198 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002199 if (p_prompt)
2200 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 while (indent-- > 0)
2202 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
2205
2206 ga_init2(&line_ga, 1, 30);
2207
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002208 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002209 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002210 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002211 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002212 while (indent >= 8)
2213 {
2214 ga_append(&line_ga, TAB);
2215 msg_puts((char_u *)" ");
2216 indent -= 8;
2217 }
2218 while (indent-- > 0)
2219 {
2220 ga_append(&line_ga, ' ');
2221 msg_putchar(' ');
2222 }
2223 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002224 ++no_mapping;
2225 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002226
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 /*
2228 * Get the line, one character at a time.
2229 */
2230 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002231 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 {
2233 if (ga_grow(&line_ga, 40) == FAIL)
2234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002236 /* Get one character at a time. Don't use inchar(), it can't handle
2237 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002238 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002239 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
2241 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002242 * Handle line editing.
2243 * Previously this was left to the system, putting the terminal in
2244 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002246 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002248 msg_putchar('\n');
2249 break;
2250 }
2251
2252 if (!escaped)
2253 {
2254 /* CR typed means "enter", which is NL */
2255 if (c1 == '\r')
2256 c1 = '\n';
2257
2258 if (c1 == BS || c1 == K_BS
2259 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002261 if (line_ga.ga_len > 0)
2262 {
2263 --line_ga.ga_len;
2264 goto redraw;
2265 }
2266 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002269 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002271 msg_col = startcol;
2272 msg_clr_eos();
2273 line_ga.ga_len = 0;
2274 continue;
2275 }
2276
2277 if (c1 == Ctrl_T)
2278 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02002279 long sw = get_sw_value();
2280
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002281 p = (char_u *)line_ga.ga_data;
2282 p[line_ga.ga_len] = NUL;
2283 indent = get_indent_str(p, 8);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002284 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002285add_indent:
2286 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002288 char_u *s = skipwhite(p);
2289
2290 ga_grow(&line_ga, 1);
2291 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2292 *s = ' ';
2293 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002295redraw:
2296 /* redraw the line */
2297 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002298 vcol = 0;
2299 for (p = (char_u *)line_ga.ga_data;
2300 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002302 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002304 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306 msg_putchar(' ');
2307 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002309 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002311 msg_outtrans_len(p, 1);
2312 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
2314 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002315 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002316 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002317 continue;
2318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002320 if (c1 == Ctrl_D)
2321 {
2322 /* Delete one shiftwidth. */
2323 p = (char_u *)line_ga.ga_data;
2324 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002326 if (prev_char == '^')
2327 ex_keep_indent = TRUE;
2328 indent = 0;
2329 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331 else
2332 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002333 p[line_ga.ga_len] = NUL;
2334 indent = get_indent_str(p, 8);
2335 --indent;
Bram Moolenaar14f24742012-08-08 18:01:05 +02002336 indent -= indent % get_sw_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002338 while (get_indent_str(p, 8) > indent)
2339 {
2340 char_u *s = skipwhite(p);
2341
2342 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2343 --line_ga.ga_len;
2344 }
2345 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002347
2348 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2349 {
2350 escaped = TRUE;
2351 continue;
2352 }
2353
2354 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2355 if (IS_SPECIAL(c1))
2356 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002358
2359 if (IS_SPECIAL(c1))
2360 c1 = '?';
2361 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002362 if (c1 == '\n')
2363 msg_putchar('\n');
2364 else if (c1 == TAB)
2365 {
2366 /* Don't use chartabsize(), 'ts' can be different */
2367 do
2368 {
2369 msg_putchar(' ');
2370 } while (++vcol % 8);
2371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002374 msg_outtrans_len(
2375 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2376 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002378 ++line_ga.ga_len;
2379 escaped = FALSE;
2380
2381 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002382 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002383
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002384 /* We are done when a NL is entered, but not when it comes after an
2385 * odd number of backslashes, that results in a NUL. */
2386 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002388 int bcount = 0;
2389
2390 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2391 ++bcount;
2392
2393 if (bcount > 0)
2394 {
2395 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2396 * "\NL", etc. */
2397 line_ga.ga_len -= (bcount + 1) / 2;
2398 pend -= (bcount + 1) / 2;
2399 pend[-1] = '\n';
2400 }
2401
2402 if ((bcount & 1) == 0)
2403 {
2404 --line_ga.ga_len;
2405 --pend;
2406 *pend = NUL;
2407 break;
2408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
2410 }
2411
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002412 --no_mapping;
2413 --allow_keys;
2414
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 /* make following messages go to the next line */
2416 msg_didout = FALSE;
2417 msg_col = 0;
2418 if (msg_row < Rows - 1)
2419 ++msg_row;
2420 emsg_on_display = FALSE; /* don't want ui_delay() */
2421
2422 if (got_int)
2423 ga_clear(&line_ga);
2424
2425 return (char_u *)line_ga.ga_data;
2426}
2427
Bram Moolenaare344bea2005-09-01 20:46:49 +00002428# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2429 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430/*
2431 * Return TRUE if ccline.overstrike is on.
2432 */
2433 int
2434cmdline_overstrike()
2435{
2436 return ccline.overstrike;
2437}
2438
2439/*
2440 * Return TRUE if the cursor is at the end of the cmdline.
2441 */
2442 int
2443cmdline_at_end()
2444{
2445 return (ccline.cmdpos >= ccline.cmdlen);
2446}
2447#endif
2448
Bram Moolenaar9372a112005-12-06 19:59:18 +00002449#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450/*
2451 * Return the virtual column number at the current cursor position.
2452 * This is used by the IM code to obtain the start of the preedit string.
2453 */
2454 colnr_T
2455cmdline_getvcol_cursor()
2456{
2457 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2458 return MAXCOL;
2459
2460# ifdef FEAT_MBYTE
2461 if (has_mbyte)
2462 {
2463 colnr_T col;
2464 int i = 0;
2465
2466 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002467 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
2469 return col;
2470 }
2471 else
2472# endif
2473 return ccline.cmdpos;
2474}
2475#endif
2476
2477#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2478/*
2479 * If part of the command line is an IM preedit string, redraw it with
2480 * IM feedback attributes. The cursor position is restored after drawing.
2481 */
2482 static void
2483redrawcmd_preedit()
2484{
2485 if ((State & CMDLINE)
2486 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002487 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 && !p_imdisable
2489 && im_is_preediting())
2490 {
2491 int cmdpos = 0;
2492 int cmdspos;
2493 int old_row;
2494 int old_col;
2495 colnr_T col;
2496
2497 old_row = msg_row;
2498 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002499 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
2501# ifdef FEAT_MBYTE
2502 if (has_mbyte)
2503 {
2504 for (col = 0; col < preedit_start_col
2505 && cmdpos < ccline.cmdlen; ++col)
2506 {
2507 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002508 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510 }
2511 else
2512# endif
2513 {
2514 cmdspos += preedit_start_col;
2515 cmdpos += preedit_start_col;
2516 }
2517
2518 msg_row = cmdline_row + (cmdspos / (int)Columns);
2519 msg_col = cmdspos % (int)Columns;
2520 if (msg_row >= Rows)
2521 msg_row = Rows - 1;
2522
2523 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2524 {
2525 int char_len;
2526 int char_attr;
2527
2528 char_attr = im_get_feedback_attr(col);
2529 if (char_attr < 0)
2530 break; /* end of preedit string */
2531
2532# ifdef FEAT_MBYTE
2533 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002534 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 else
2536# endif
2537 char_len = 1;
2538
2539 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2540 cmdpos += char_len;
2541 }
2542
2543 msg_row = old_row;
2544 msg_col = old_col;
2545 }
2546}
2547#endif /* FEAT_XIM && FEAT_GUI_GTK */
2548
2549/*
2550 * Allocate a new command line buffer.
2551 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2552 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2553 */
2554 static void
2555alloc_cmdbuff(len)
2556 int len;
2557{
2558 /*
2559 * give some extra space to avoid having to allocate all the time
2560 */
2561 if (len < 80)
2562 len = 100;
2563 else
2564 len += 20;
2565
2566 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2567 ccline.cmdbufflen = len;
2568}
2569
2570/*
2571 * Re-allocate the command line to length len + something extra.
2572 * return FAIL for failure, OK otherwise
2573 */
2574 static int
2575realloc_cmdbuff(len)
2576 int len;
2577{
2578 char_u *p;
2579
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002580 if (len < ccline.cmdbufflen)
2581 return OK; /* no need to resize */
2582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 p = ccline.cmdbuff;
2584 alloc_cmdbuff(len); /* will get some more */
2585 if (ccline.cmdbuff == NULL) /* out of memory */
2586 {
2587 ccline.cmdbuff = p; /* keep the old one */
2588 return FAIL;
2589 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002590 /* There isn't always a NUL after the command, but it may need to be
2591 * there, thus copy up to the NUL and add a NUL. */
2592 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2593 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002595
2596 if (ccline.xpc != NULL
2597 && ccline.xpc->xp_pattern != NULL
2598 && ccline.xpc->xp_context != EXPAND_NOTHING
2599 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2600 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002601 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002602
2603 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2604 * to point into the newly allocated memory. */
2605 if (i >= 0 && i <= ccline.cmdlen)
2606 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2607 }
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 return OK;
2610}
2611
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002612#if defined(FEAT_ARABIC) || defined(PROTO)
2613static char_u *arshape_buf = NULL;
2614
2615# if defined(EXITFREE) || defined(PROTO)
2616 void
2617free_cmdline_buf()
2618{
2619 vim_free(arshape_buf);
2620}
2621# endif
2622#endif
2623
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624/*
2625 * Draw part of the cmdline at the current cursor position. But draw stars
2626 * when cmdline_star is TRUE.
2627 */
2628 static void
2629draw_cmdline(start, len)
2630 int start;
2631 int len;
2632{
2633#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2634 int i;
2635
2636 if (cmdline_star > 0)
2637 for (i = 0; i < len; ++i)
2638 {
2639 msg_putchar('*');
2640# ifdef FEAT_MBYTE
2641 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002642 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643# endif
2644 }
2645 else
2646#endif
2647#ifdef FEAT_ARABIC
2648 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 static int buflen = 0;
2651 char_u *p;
2652 int j;
2653 int newlen = 0;
2654 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002655 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 int prev_c = 0;
2657 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002658 int u8c;
2659 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
2662 /*
2663 * Do arabic shaping into a temporary buffer. This is very
2664 * inefficient!
2665 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002666 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
2668 /* Re-allocate the buffer. We keep it around to avoid a lot of
2669 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002670 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002671 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002672 arshape_buf = alloc(buflen);
2673 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 return; /* out of memory */
2675 }
2676
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002677 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2678 {
2679 /* Prepend a space to draw the leading composing char on. */
2680 arshape_buf[0] = ' ';
2681 newlen = 1;
2682 }
2683
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 for (j = start; j < start + len; j += mb_l)
2685 {
2686 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002687 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002688 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 if (ARABIC_CHAR(u8c))
2690 {
2691 /* Do Arabic shaping. */
2692 if (cmdmsg_rl)
2693 {
2694 /* displaying from right to left */
2695 pc = prev_c;
2696 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002697 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (j + mb_l >= start + len)
2699 nc = NUL;
2700 else
2701 nc = utf_ptr2char(p + mb_l);
2702 }
2703 else
2704 {
2705 /* displaying from left to right */
2706 if (j + mb_l >= start + len)
2707 pc = NUL;
2708 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002709 {
2710 int pcc[MAX_MCO];
2711
2712 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002714 pc1 = pcc[0];
2715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 nc = prev_c;
2717 }
2718 prev_c = u8c;
2719
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002720 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002722 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002723 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002725 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2726 if (u8cc[1] != 0)
2727 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002728 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
2730 }
2731 else
2732 {
2733 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002734 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 newlen += mb_l;
2736 }
2737 }
2738
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002739 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741 else
2742#endif
2743 msg_outtrans_len(ccline.cmdbuff + start, len);
2744}
2745
2746/*
2747 * Put a character on the command line. Shifts the following text to the
2748 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2749 * "c" must be printable (fit in one display cell)!
2750 */
2751 void
2752putcmdline(c, shift)
2753 int c;
2754 int shift;
2755{
2756 if (cmd_silent)
2757 return;
2758 msg_no_more = TRUE;
2759 msg_putchar(c);
2760 if (shift)
2761 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2762 msg_no_more = FALSE;
2763 cursorcmd();
2764}
2765
2766/*
2767 * Undo a putcmdline(c, FALSE).
2768 */
2769 void
2770unputcmdline()
2771{
2772 if (cmd_silent)
2773 return;
2774 msg_no_more = TRUE;
2775 if (ccline.cmdlen == ccline.cmdpos)
2776 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002777#ifdef FEAT_MBYTE
2778 else if (has_mbyte)
2779 draw_cmdline(ccline.cmdpos,
2780 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 else
2783 draw_cmdline(ccline.cmdpos, 1);
2784 msg_no_more = FALSE;
2785 cursorcmd();
2786}
2787
2788/*
2789 * Put the given string, of the given length, onto the command line.
2790 * If len is -1, then STRLEN() is used to calculate the length.
2791 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2792 * part will be redrawn, otherwise it will not. If this function is called
2793 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2794 * called afterwards.
2795 */
2796 int
2797put_on_cmdline(str, len, redraw)
2798 char_u *str;
2799 int len;
2800 int redraw;
2801{
2802 int retval;
2803 int i;
2804 int m;
2805 int c;
2806
2807 if (len < 0)
2808 len = (int)STRLEN(str);
2809
2810 /* Check if ccline.cmdbuff needs to be longer */
2811 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002812 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 else
2814 retval = OK;
2815 if (retval == OK)
2816 {
2817 if (!ccline.overstrike)
2818 {
2819 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2820 ccline.cmdbuff + ccline.cmdpos,
2821 (size_t)(ccline.cmdlen - ccline.cmdpos));
2822 ccline.cmdlen += len;
2823 }
2824 else
2825 {
2826#ifdef FEAT_MBYTE
2827 if (has_mbyte)
2828 {
2829 /* Count nr of characters in the new string. */
2830 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002831 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 ++m;
2833 /* Count nr of bytes in cmdline that are overwritten by these
2834 * characters. */
2835 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002836 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 --m;
2838 if (i < ccline.cmdlen)
2839 {
2840 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2841 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2842 ccline.cmdlen += ccline.cmdpos + len - i;
2843 }
2844 else
2845 ccline.cmdlen = ccline.cmdpos + len;
2846 }
2847 else
2848#endif
2849 if (ccline.cmdpos + len > ccline.cmdlen)
2850 ccline.cmdlen = ccline.cmdpos + len;
2851 }
2852 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2853 ccline.cmdbuff[ccline.cmdlen] = NUL;
2854
2855#ifdef FEAT_MBYTE
2856 if (enc_utf8)
2857 {
2858 /* When the inserted text starts with a composing character,
2859 * backup to the character before it. There could be two of them.
2860 */
2861 i = 0;
2862 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2863 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2864 {
2865 i = (*mb_head_off)(ccline.cmdbuff,
2866 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2867 ccline.cmdpos -= i;
2868 len += i;
2869 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2870 }
2871# ifdef FEAT_ARABIC
2872 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2873 {
2874 /* Check the previous character for Arabic combining pair. */
2875 i = (*mb_head_off)(ccline.cmdbuff,
2876 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2877 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2878 + ccline.cmdpos - i), c))
2879 {
2880 ccline.cmdpos -= i;
2881 len += i;
2882 }
2883 else
2884 i = 0;
2885 }
2886# endif
2887 if (i != 0)
2888 {
2889 /* Also backup the cursor position. */
2890 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2891 ccline.cmdspos -= i;
2892 msg_col -= i;
2893 if (msg_col < 0)
2894 {
2895 msg_col += Columns;
2896 --msg_row;
2897 }
2898 }
2899 }
2900#endif
2901
2902 if (redraw && !cmd_silent)
2903 {
2904 msg_no_more = TRUE;
2905 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002906 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2908 /* Avoid clearing the rest of the line too often. */
2909 if (cmdline_row != i || ccline.overstrike)
2910 msg_clr_eos();
2911 msg_no_more = FALSE;
2912 }
2913#ifdef FEAT_FKMAP
2914 /*
2915 * If we are in Farsi command mode, the character input must be in
2916 * Insert mode. So do not advance the cmdpos.
2917 */
2918 if (!cmd_fkmap)
2919#endif
2920 {
2921 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002924 if (m < 0) /* overflow, Columns or Rows at weird value */
2925 m = MAXCOL;
2926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 else
2928 m = MAXCOL;
2929 for (i = 0; i < len; ++i)
2930 {
2931 c = cmdline_charsize(ccline.cmdpos);
2932#ifdef FEAT_MBYTE
2933 /* count ">" for a double-wide char that doesn't fit. */
2934 if (has_mbyte)
2935 correct_cmdspos(ccline.cmdpos, c);
2936#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002937 /* Stop cursor at the end of the screen, but do increment the
2938 * insert position, so that entering a very long command
2939 * works, even though you can't see it. */
2940 if (ccline.cmdspos + c < m)
2941 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942#ifdef FEAT_MBYTE
2943 if (has_mbyte)
2944 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002945 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 if (c > len - i - 1)
2947 c = len - i - 1;
2948 ccline.cmdpos += c;
2949 i += c;
2950 }
2951#endif
2952 ++ccline.cmdpos;
2953 }
2954 }
2955 }
2956 if (redraw)
2957 msg_check();
2958 return retval;
2959}
2960
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002961static struct cmdline_info prev_ccline;
2962static int prev_ccline_used = FALSE;
2963
2964/*
2965 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2966 * and overwrite it. But get_cmdline_str() may need it, thus make it
2967 * available globally in prev_ccline.
2968 */
2969 static void
2970save_cmdline(ccp)
2971 struct cmdline_info *ccp;
2972{
2973 if (!prev_ccline_used)
2974 {
2975 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2976 prev_ccline_used = TRUE;
2977 }
2978 *ccp = prev_ccline;
2979 prev_ccline = ccline;
2980 ccline.cmdbuff = NULL;
2981 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002982 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002983}
2984
2985/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002986 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002987 */
2988 static void
2989restore_cmdline(ccp)
2990 struct cmdline_info *ccp;
2991{
2992 ccline = prev_ccline;
2993 prev_ccline = *ccp;
2994}
2995
Bram Moolenaar5a305422006-04-28 22:38:25 +00002996#if defined(FEAT_EVAL) || defined(PROTO)
2997/*
2998 * Save the command line into allocated memory. Returns a pointer to be
2999 * passed to restore_cmdline_alloc() later.
3000 * Returns NULL when failed.
3001 */
3002 char_u *
3003save_cmdline_alloc()
3004{
3005 struct cmdline_info *p;
3006
3007 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3008 if (p != NULL)
3009 save_cmdline(p);
3010 return (char_u *)p;
3011}
3012
3013/*
3014 * Restore the command line from the return value of save_cmdline_alloc().
3015 */
3016 void
3017restore_cmdline_alloc(p)
3018 char_u *p;
3019{
3020 if (p != NULL)
3021 {
3022 restore_cmdline((struct cmdline_info *)p);
3023 vim_free(p);
3024 }
3025}
3026#endif
3027
Bram Moolenaar8299df92004-07-10 09:47:34 +00003028/*
3029 * paste a yank register into the command line.
3030 * used by CTRL-R command in command-line mode
3031 * insert_reg() can't be used here, because special characters from the
3032 * register contents will be interpreted as commands.
3033 *
3034 * return FAIL for failure, OK otherwise
3035 */
3036 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003037cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003038 int regname;
3039 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003040 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003041{
3042 long i;
3043 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003044 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003045 int allocated;
3046 struct cmdline_info save_ccline;
3047
3048 /* check for valid regname; also accept special characters for CTRL-R in
3049 * the command line */
3050 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3051 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3052 return FAIL;
3053
3054 /* A register containing CTRL-R can cause an endless loop. Allow using
3055 * CTRL-C to break the loop. */
3056 line_breakcheck();
3057 if (got_int)
3058 return FAIL;
3059
3060#ifdef FEAT_CLIPBOARD
3061 regname = may_get_selection(regname);
3062#endif
3063
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003064 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003065 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003066 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003067 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003068 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003069 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003070 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003071
3072 if (i)
3073 {
3074 /* Got the value of a special register in "arg". */
3075 if (arg == NULL)
3076 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003077
3078 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3079 * part of the word. */
3080 p = arg;
3081 if (p_is && regname == Ctrl_W)
3082 {
3083 char_u *w;
3084 int len;
3085
3086 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003087 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003088 {
3089#ifdef FEAT_MBYTE
3090 if (has_mbyte)
3091 {
3092 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3093 if (!vim_iswordc(mb_ptr2char(w - len)))
3094 break;
3095 w -= len;
3096 }
3097 else
3098#endif
3099 {
3100 if (!vim_iswordc(w[-1]))
3101 break;
3102 --w;
3103 }
3104 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003105 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003106 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3107 p += len;
3108 }
3109
3110 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003111 if (allocated)
3112 vim_free(arg);
3113 return OK;
3114 }
3115
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003116 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003117}
3118
3119/*
3120 * Put a string on the command line.
3121 * When "literally" is TRUE, insert literally.
3122 * When "literally" is FALSE, insert as typed, but don't leave the command
3123 * line.
3124 */
3125 void
3126cmdline_paste_str(s, literally)
3127 char_u *s;
3128 int literally;
3129{
3130 int c, cv;
3131
3132 if (literally)
3133 put_on_cmdline(s, -1, TRUE);
3134 else
3135 while (*s != NUL)
3136 {
3137 cv = *s;
3138 if (cv == Ctrl_V && s[1])
3139 ++s;
3140#ifdef FEAT_MBYTE
3141 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003142 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003143 else
3144#endif
3145 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003146 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3147 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003148#ifdef UNIX
3149 || c == intr_char
3150#endif
3151 || (c == Ctrl_BSL && *s == Ctrl_N))
3152 stuffcharReadbuff(Ctrl_V);
3153 stuffcharReadbuff(c);
3154 }
3155}
3156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#ifdef FEAT_WILDMENU
3158/*
3159 * Delete characters on the command line, from "from" to the current
3160 * position.
3161 */
3162 static void
3163cmdline_del(from)
3164 int from;
3165{
3166 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3167 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3168 ccline.cmdlen -= ccline.cmdpos - from;
3169 ccline.cmdpos = from;
3170}
3171#endif
3172
3173/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003174 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 * search
3176 */
3177 void
3178redrawcmdline()
3179{
3180 if (cmd_silent)
3181 return;
3182 need_wait_return = FALSE;
3183 compute_cmdrow();
3184 redrawcmd();
3185 cursorcmd();
3186}
3187
3188 static void
3189redrawcmdprompt()
3190{
3191 int i;
3192
3193 if (cmd_silent)
3194 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003195 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 msg_putchar(ccline.cmdfirstc);
3197 if (ccline.cmdprompt != NULL)
3198 {
3199 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3200 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3201 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003202 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 --ccline.cmdindent;
3204 }
3205 else
3206 for (i = ccline.cmdindent; i > 0; --i)
3207 msg_putchar(' ');
3208}
3209
3210/*
3211 * Redraw what is currently on the command line.
3212 */
3213 void
3214redrawcmd()
3215{
3216 if (cmd_silent)
3217 return;
3218
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003219 /* when 'incsearch' is set there may be no command line while redrawing */
3220 if (ccline.cmdbuff == NULL)
3221 {
3222 windgoto(cmdline_row, 0);
3223 msg_clr_eos();
3224 return;
3225 }
3226
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 msg_start();
3228 redrawcmdprompt();
3229
3230 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3231 msg_no_more = TRUE;
3232 draw_cmdline(0, ccline.cmdlen);
3233 msg_clr_eos();
3234 msg_no_more = FALSE;
3235
3236 set_cmdspos_cursor();
3237
3238 /*
3239 * An emsg() before may have set msg_scroll. This is used in normal mode,
3240 * in cmdline mode we can reset them now.
3241 */
3242 msg_scroll = FALSE; /* next message overwrites cmdline */
3243
3244 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3245 * in cmdline mode */
3246 skip_redraw = FALSE;
3247}
3248
3249 void
3250compute_cmdrow()
3251{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003252 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 cmdline_row = Rows - 1;
3254 else
3255 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3256 + W_STATUS_HEIGHT(lastwin);
3257}
3258
3259 static void
3260cursorcmd()
3261{
3262 if (cmd_silent)
3263 return;
3264
3265#ifdef FEAT_RIGHTLEFT
3266 if (cmdmsg_rl)
3267 {
3268 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3269 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3270 if (msg_row <= 0)
3271 msg_row = Rows - 1;
3272 }
3273 else
3274#endif
3275 {
3276 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3277 msg_col = ccline.cmdspos % (int)Columns;
3278 if (msg_row >= Rows)
3279 msg_row = Rows - 1;
3280 }
3281
3282 windgoto(msg_row, msg_col);
3283#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3284 redrawcmd_preedit();
3285#endif
3286#ifdef MCH_CURSOR_SHAPE
3287 mch_update_cursor();
3288#endif
3289}
3290
3291 void
3292gotocmdline(clr)
3293 int clr;
3294{
3295 msg_start();
3296#ifdef FEAT_RIGHTLEFT
3297 if (cmdmsg_rl)
3298 msg_col = Columns - 1;
3299 else
3300#endif
3301 msg_col = 0; /* always start in column 0 */
3302 if (clr) /* clear the bottom line(s) */
3303 msg_clr_eos(); /* will reset clear_cmdline */
3304 windgoto(cmdline_row, 0);
3305}
3306
3307/*
3308 * Check the word in front of the cursor for an abbreviation.
3309 * Called when the non-id character "c" has been entered.
3310 * When an abbreviation is recognized it is removed from the text with
3311 * backspaces and the replacement string is inserted, followed by "c".
3312 */
3313 static int
3314ccheck_abbr(c)
3315 int c;
3316{
3317 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3318 return FALSE;
3319
3320 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3321}
3322
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003323#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3324 static int
3325#ifdef __BORLANDC__
3326_RTLENTRYF
3327#endif
3328sort_func_compare(s1, s2)
3329 const void *s1;
3330 const void *s2;
3331{
3332 char_u *p1 = *(char_u **)s1;
3333 char_u *p2 = *(char_u **)s2;
3334
3335 if (*p1 != '<' && *p2 == '<') return -1;
3336 if (*p1 == '<' && *p2 != '<') return 1;
3337 return STRCMP(p1, p2);
3338}
3339#endif
3340
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341/*
3342 * Return FAIL if this is not an appropriate context in which to do
3343 * completion of anything, return OK if it is (even if there are no matches).
3344 * For the caller, this means that the character is just passed through like a
3345 * normal character (instead of being expanded). This allows :s/^I^D etc.
3346 */
3347 static int
Bram Moolenaarb3479632012-11-28 16:49:58 +01003348nextwild(xp, type, options, escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 expand_T *xp;
3350 int type;
3351 int options; /* extra options for ExpandOne() */
Bram Moolenaarb3479632012-11-28 16:49:58 +01003352 int escape; /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 int i, j;
3355 char_u *p1;
3356 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 int difflen;
3358 int v;
3359
3360 if (xp->xp_numfiles == -1)
3361 {
3362 set_expand_context(xp);
3363 cmd_showtail = expand_showtail(xp);
3364 }
3365
3366 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3367 {
3368 beep_flush();
3369 return OK; /* Something illegal on command line */
3370 }
3371 if (xp->xp_context == EXPAND_NOTHING)
3372 {
3373 /* Caller can use the character as a normal char instead */
3374 return FAIL;
3375 }
3376
3377 MSG_PUTS("..."); /* show that we are busy */
3378 out_flush();
3379
3380 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003381 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
3383 if (type == WILD_NEXT || type == WILD_PREV)
3384 {
3385 /*
3386 * Get next/previous match for a previous expanded pattern.
3387 */
3388 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3389 }
3390 else
3391 {
3392 /*
3393 * Translate string into pattern and expand it.
3394 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003395 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3396 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 p2 = NULL;
3398 else
3399 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003400 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003401 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3402 if (escape)
3403 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003404
3405 if (p_wic)
3406 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003407 p2 = ExpandOne(xp, p1,
3408 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003409 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003411 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 if (p2 != NULL && type == WILD_LONGEST)
3413 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003414 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (ccline.cmdbuff[i + j] == '*'
3416 || ccline.cmdbuff[i + j] == '?')
3417 break;
3418 if ((int)STRLEN(p2) < j)
3419 {
3420 vim_free(p2);
3421 p2 = NULL;
3422 }
3423 }
3424 }
3425 }
3426
3427 if (p2 != NULL && !got_int)
3428 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003429 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003430 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003432 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 xp->xp_pattern = ccline.cmdbuff + i;
3434 }
3435 else
3436 v = OK;
3437 if (v == OK)
3438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003439 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3440 &ccline.cmdbuff[ccline.cmdpos],
3441 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3442 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 ccline.cmdlen += difflen;
3444 ccline.cmdpos += difflen;
3445 }
3446 }
3447 vim_free(p2);
3448
3449 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003450 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451
3452 /* When expanding a ":map" command and no matches are found, assume that
3453 * the key is supposed to be inserted literally */
3454 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3455 return FAIL;
3456
3457 if (xp->xp_numfiles <= 0 && p2 == NULL)
3458 beep_flush();
3459 else if (xp->xp_numfiles == 1)
3460 /* free expanded pattern */
3461 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3462
3463 return OK;
3464}
3465
3466/*
3467 * Do wildcard expansion on the string 'str'.
3468 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003469 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 * Return NULL for failure.
3471 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003472 * "orig" is the originally expanded string, copied to allocated memory. It
3473 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3474 * WILD_PREV "orig" should be NULL.
3475 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003476 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3477 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 *
3479 * mode = WILD_FREE: just free previously expanded matches
3480 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3481 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3482 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3483 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3484 * mode = WILD_ALL: return all matches concatenated
3485 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003486 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 *
3488 * options = WILD_LIST_NOTFOUND: list entries without a match
3489 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3490 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3491 * options = WILD_NO_BEEP: Don't beep for multiple matches
3492 * options = WILD_ADD_SLASH: add a slash after directory names
3493 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3494 * options = WILD_SILENT: don't print warning messages
3495 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003496 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 *
3498 * The variables xp->xp_context and xp->xp_backslash must have been set!
3499 */
3500 char_u *
3501ExpandOne(xp, str, orig, options, mode)
3502 expand_T *xp;
3503 char_u *str;
3504 char_u *orig; /* allocated copy of original of expanded string */
3505 int options;
3506 int mode;
3507{
3508 char_u *ss = NULL;
3509 static int findex;
3510 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003511 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 int i;
3513 long_u len;
3514 int non_suf_match; /* number without matching suffix */
3515
3516 /*
3517 * first handle the case of using an old match
3518 */
3519 if (mode == WILD_NEXT || mode == WILD_PREV)
3520 {
3521 if (xp->xp_numfiles > 0)
3522 {
3523 if (mode == WILD_PREV)
3524 {
3525 if (findex == -1)
3526 findex = xp->xp_numfiles;
3527 --findex;
3528 }
3529 else /* mode == WILD_NEXT */
3530 ++findex;
3531
3532 /*
3533 * When wrapping around, return the original string, set findex to
3534 * -1.
3535 */
3536 if (findex < 0)
3537 {
3538 if (orig_save == NULL)
3539 findex = xp->xp_numfiles - 1;
3540 else
3541 findex = -1;
3542 }
3543 if (findex >= xp->xp_numfiles)
3544 {
3545 if (orig_save == NULL)
3546 findex = 0;
3547 else
3548 findex = -1;
3549 }
3550#ifdef FEAT_WILDMENU
3551 if (p_wmnu)
3552 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3553 findex, cmd_showtail);
3554#endif
3555 if (findex == -1)
3556 return vim_strsave(orig_save);
3557 return vim_strsave(xp->xp_files[findex]);
3558 }
3559 else
3560 return NULL;
3561 }
3562
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003563 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3565 {
3566 FreeWild(xp->xp_numfiles, xp->xp_files);
3567 xp->xp_numfiles = -1;
3568 vim_free(orig_save);
3569 orig_save = NULL;
3570 }
3571 findex = 0;
3572
3573 if (mode == WILD_FREE) /* only release file name */
3574 return NULL;
3575
3576 if (xp->xp_numfiles == -1)
3577 {
3578 vim_free(orig_save);
3579 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003580 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
3582 /*
3583 * Do the expansion.
3584 */
3585 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3586 options) == FAIL)
3587 {
3588#ifdef FNAME_ILLEGAL
3589 /* Illegal file name has been silently skipped. But when there
3590 * are wildcards, the real problem is that there was no match,
3591 * causing the pattern to be added, which has illegal characters.
3592 */
3593 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3594 EMSG2(_(e_nomatch2), str);
3595#endif
3596 }
3597 else if (xp->xp_numfiles == 0)
3598 {
3599 if (!(options & WILD_SILENT))
3600 EMSG2(_(e_nomatch2), str);
3601 }
3602 else
3603 {
3604 /* Escape the matches for use on the command line. */
3605 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3606
3607 /*
3608 * Check for matching suffixes in file names.
3609 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003610 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3611 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
3613 if (xp->xp_numfiles)
3614 non_suf_match = xp->xp_numfiles;
3615 else
3616 non_suf_match = 1;
3617 if ((xp->xp_context == EXPAND_FILES
3618 || xp->xp_context == EXPAND_DIRECTORIES)
3619 && xp->xp_numfiles > 1)
3620 {
3621 /*
3622 * More than one match; check suffix.
3623 * The files will have been sorted on matching suffix in
3624 * expand_wildcards, only need to check the first two.
3625 */
3626 non_suf_match = 0;
3627 for (i = 0; i < 2; ++i)
3628 if (match_suffix(xp->xp_files[i]))
3629 ++non_suf_match;
3630 }
3631 if (non_suf_match != 1)
3632 {
3633 /* Can we ever get here unless it's while expanding
3634 * interactively? If not, we can get rid of this all
3635 * together. Don't really want to wait for this message
3636 * (and possibly have to hit return to continue!).
3637 */
3638 if (!(options & WILD_SILENT))
3639 EMSG(_(e_toomany));
3640 else if (!(options & WILD_NO_BEEP))
3641 beep_flush();
3642 }
3643 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3644 ss = vim_strsave(xp->xp_files[0]);
3645 }
3646 }
3647 }
3648
3649 /* Find longest common part */
3650 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3651 {
3652 for (len = 0; xp->xp_files[0][len]; ++len)
3653 {
3654 for (i = 0; i < xp->xp_numfiles; ++i)
3655 {
3656#ifdef CASE_INSENSITIVE_FILENAME
3657 if (xp->xp_context == EXPAND_DIRECTORIES
3658 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003659 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 || xp->xp_context == EXPAND_BUFFERS)
3661 {
3662 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3663 TOLOWER_LOC(xp->xp_files[0][len]))
3664 break;
3665 }
3666 else
3667#endif
3668 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3669 break;
3670 }
3671 if (i < xp->xp_numfiles)
3672 {
3673 if (!(options & WILD_NO_BEEP))
3674 vim_beep();
3675 break;
3676 }
3677 }
3678 ss = alloc((unsigned)len + 1);
3679 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003680 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 findex = -1; /* next p_wc gets first one */
3682 }
3683
3684 /* Concatenate all matching names */
3685 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3686 {
3687 len = 0;
3688 for (i = 0; i < xp->xp_numfiles; ++i)
3689 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3690 ss = lalloc(len, TRUE);
3691 if (ss != NULL)
3692 {
3693 *ss = NUL;
3694 for (i = 0; i < xp->xp_numfiles; ++i)
3695 {
3696 STRCAT(ss, xp->xp_files[i]);
3697 if (i != xp->xp_numfiles - 1)
3698 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3699 }
3700 }
3701 }
3702
3703 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3704 ExpandCleanup(xp);
3705
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003706 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003707 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003708 vim_free(orig);
3709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 return ss;
3711}
3712
3713/*
3714 * Prepare an expand structure for use.
3715 */
3716 void
3717ExpandInit(xp)
3718 expand_T *xp;
3719{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003720 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003721 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003723#ifndef BACKSLASH_IN_FILENAME
3724 xp->xp_shell = FALSE;
3725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 xp->xp_numfiles = -1;
3727 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003728#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3729 xp->xp_arg = NULL;
3730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731}
3732
3733/*
3734 * Cleanup an expand structure after use.
3735 */
3736 void
3737ExpandCleanup(xp)
3738 expand_T *xp;
3739{
3740 if (xp->xp_numfiles >= 0)
3741 {
3742 FreeWild(xp->xp_numfiles, xp->xp_files);
3743 xp->xp_numfiles = -1;
3744 }
3745}
3746
3747 void
3748ExpandEscape(xp, str, numfiles, files, options)
3749 expand_T *xp;
3750 char_u *str;
3751 int numfiles;
3752 char_u **files;
3753 int options;
3754{
3755 int i;
3756 char_u *p;
3757
3758 /*
3759 * May change home directory back to "~"
3760 */
3761 if (options & WILD_HOME_REPLACE)
3762 tilde_replace(str, numfiles, files);
3763
3764 if (options & WILD_ESCAPE)
3765 {
3766 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003767 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003768 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 || xp->xp_context == EXPAND_BUFFERS
3770 || xp->xp_context == EXPAND_DIRECTORIES)
3771 {
3772 /*
3773 * Insert a backslash into a file name before a space, \, %, #
3774 * and wildmatch characters, except '~'.
3775 */
3776 for (i = 0; i < numfiles; ++i)
3777 {
3778 /* for ":set path=" we need to escape spaces twice */
3779 if (xp->xp_backslash == XP_BS_THREE)
3780 {
3781 p = vim_strsave_escaped(files[i], (char_u *)" ");
3782 if (p != NULL)
3783 {
3784 vim_free(files[i]);
3785 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003786#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 p = vim_strsave_escaped(files[i], (char_u *)" ");
3788 if (p != NULL)
3789 {
3790 vim_free(files[i]);
3791 files[i] = p;
3792 }
3793#endif
3794 }
3795 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003796#ifdef BACKSLASH_IN_FILENAME
3797 p = vim_strsave_fnameescape(files[i], FALSE);
3798#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003799 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 if (p != NULL)
3802 {
3803 vim_free(files[i]);
3804 files[i] = p;
3805 }
3806
3807 /* If 'str' starts with "\~", replace "~" at start of
3808 * files[i] with "\~". */
3809 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003810 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 }
3812 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003813
3814 /* If the first file starts with a '+' escape it. Otherwise it
3815 * could be seen as "+cmd". */
3816 if (*files[0] == '+')
3817 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819 else if (xp->xp_context == EXPAND_TAGS)
3820 {
3821 /*
3822 * Insert a backslash before characters in a tag name that
3823 * would terminate the ":tag" command.
3824 */
3825 for (i = 0; i < numfiles; ++i)
3826 {
3827 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3828 if (p != NULL)
3829 {
3830 vim_free(files[i]);
3831 files[i] = p;
3832 }
3833 }
3834 }
3835 }
3836}
3837
3838/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003839 * Escape special characters in "fname" for when used as a file name argument
3840 * after a Vim command, or, when "shell" is non-zero, a shell command.
3841 * Returns the result in allocated memory.
3842 */
3843 char_u *
3844vim_strsave_fnameescape(fname, shell)
3845 char_u *fname;
3846 int shell;
3847{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003848 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003849#ifdef BACKSLASH_IN_FILENAME
3850 char_u buf[20];
3851 int j = 0;
3852
3853 /* Don't escape '[' and '{' if they are in 'isfname'. */
3854 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3855 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3856 buf[j++] = *p;
3857 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003858 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003859#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003860 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3861 if (shell && csh_like_shell() && p != NULL)
3862 {
3863 char_u *s;
3864
3865 /* For csh and similar shells need to put two backslashes before '!'.
3866 * One is taken by Vim, one by the shell. */
3867 s = vim_strsave_escaped(p, (char_u *)"!");
3868 vim_free(p);
3869 p = s;
3870 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003871#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003872
3873 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3874 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003875 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003876 escape_fname(&p);
3877
3878 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003879}
3880
3881/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003882 * Put a backslash before the file name in "pp", which is in allocated memory.
3883 */
3884 static void
3885escape_fname(pp)
3886 char_u **pp;
3887{
3888 char_u *p;
3889
3890 p = alloc((unsigned)(STRLEN(*pp) + 2));
3891 if (p != NULL)
3892 {
3893 p[0] = '\\';
3894 STRCPY(p + 1, *pp);
3895 vim_free(*pp);
3896 *pp = p;
3897 }
3898}
3899
3900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 * For each file name in files[num_files]:
3902 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3903 */
3904 void
3905tilde_replace(orig_pat, num_files, files)
3906 char_u *orig_pat;
3907 int num_files;
3908 char_u **files;
3909{
3910 int i;
3911 char_u *p;
3912
3913 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3914 {
3915 for (i = 0; i < num_files; ++i)
3916 {
3917 p = home_replace_save(NULL, files[i]);
3918 if (p != NULL)
3919 {
3920 vim_free(files[i]);
3921 files[i] = p;
3922 }
3923 }
3924 }
3925}
3926
3927/*
3928 * Show all matches for completion on the command line.
3929 * Returns EXPAND_NOTHING when the character that triggered expansion should
3930 * be inserted like a normal character.
3931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 static int
3933showmatches(xp, wildmenu)
3934 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003935 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936{
3937#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3938 int num_files;
3939 char_u **files_found;
3940 int i, j, k;
3941 int maxlen;
3942 int lines;
3943 int columns;
3944 char_u *p;
3945 int lastlen;
3946 int attr;
3947 int showtail;
3948
3949 if (xp->xp_numfiles == -1)
3950 {
3951 set_expand_context(xp);
3952 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3953 &num_files, &files_found);
3954 showtail = expand_showtail(xp);
3955 if (i != EXPAND_OK)
3956 return i;
3957
3958 }
3959 else
3960 {
3961 num_files = xp->xp_numfiles;
3962 files_found = xp->xp_files;
3963 showtail = cmd_showtail;
3964 }
3965
3966#ifdef FEAT_WILDMENU
3967 if (!wildmenu)
3968 {
3969#endif
3970 msg_didany = FALSE; /* lines_left will be set */
3971 msg_start(); /* prepare for paging */
3972 msg_putchar('\n');
3973 out_flush();
3974 cmdline_row = msg_row;
3975 msg_didany = FALSE; /* lines_left will be set again */
3976 msg_start(); /* prepare for paging */
3977#ifdef FEAT_WILDMENU
3978 }
3979#endif
3980
3981 if (got_int)
3982 got_int = FALSE; /* only int. the completion, not the cmd line */
3983#ifdef FEAT_WILDMENU
3984 else if (wildmenu)
3985 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3986#endif
3987 else
3988 {
3989 /* find the length of the longest file name */
3990 maxlen = 0;
3991 for (i = 0; i < num_files; ++i)
3992 {
3993 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003994 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 || xp->xp_context == EXPAND_BUFFERS))
3996 {
3997 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3998 j = vim_strsize(NameBuff);
3999 }
4000 else
4001 j = vim_strsize(L_SHOWFILE(i));
4002 if (j > maxlen)
4003 maxlen = j;
4004 }
4005
4006 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4007 lines = num_files;
4008 else
4009 {
4010 /* compute the number of columns and lines for the listing */
4011 maxlen += 2; /* two spaces between file names */
4012 columns = ((int)Columns + 2) / maxlen;
4013 if (columns < 1)
4014 columns = 1;
4015 lines = (num_files + columns - 1) / columns;
4016 }
4017
4018 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4019
4020 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4021 {
4022 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4023 msg_clr_eos();
4024 msg_advance(maxlen - 3);
4025 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4026 }
4027
4028 /* list the files line by line */
4029 for (i = 0; i < lines; ++i)
4030 {
4031 lastlen = 999;
4032 for (k = i; k < num_files; k += lines)
4033 {
4034 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4035 {
4036 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4037 p = files_found[k] + STRLEN(files_found[k]) + 1;
4038 msg_advance(maxlen + 1);
4039 msg_puts(p);
4040 msg_advance(maxlen + 3);
4041 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4042 break;
4043 }
4044 for (j = maxlen - lastlen; --j >= 0; )
4045 msg_putchar(' ');
4046 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004047 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 || xp->xp_context == EXPAND_BUFFERS)
4049 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004050 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004051 if (xp->xp_numfiles != -1)
4052 {
4053 char_u *halved_slash;
4054 char_u *exp_path;
4055
4056 /* Expansion was done before and special characters
4057 * were escaped, need to halve backslashes. Also
4058 * $HOME has been replaced with ~/. */
4059 exp_path = expand_env_save_opt(files_found[k], TRUE);
4060 halved_slash = backslash_halve_save(
4061 exp_path != NULL ? exp_path : files_found[k]);
4062 j = mch_isdir(halved_slash != NULL ? halved_slash
4063 : files_found[k]);
4064 vim_free(exp_path);
4065 vim_free(halved_slash);
4066 }
4067 else
4068 /* Expansion was done here, file names are literal. */
4069 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 if (showtail)
4071 p = L_SHOWFILE(k);
4072 else
4073 {
4074 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4075 TRUE);
4076 p = NameBuff;
4077 }
4078 }
4079 else
4080 {
4081 j = FALSE;
4082 p = L_SHOWFILE(k);
4083 }
4084 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4085 }
4086 if (msg_col > 0) /* when not wrapped around */
4087 {
4088 msg_clr_eos();
4089 msg_putchar('\n');
4090 }
4091 out_flush(); /* show one line at a time */
4092 if (got_int)
4093 {
4094 got_int = FALSE;
4095 break;
4096 }
4097 }
4098
4099 /*
4100 * we redraw the command below the lines that we have just listed
4101 * This is a bit tricky, but it saves a lot of screen updating.
4102 */
4103 cmdline_row = msg_row; /* will put it back later */
4104 }
4105
4106 if (xp->xp_numfiles == -1)
4107 FreeWild(num_files, files_found);
4108
4109 return EXPAND_OK;
4110}
4111
4112/*
4113 * Private gettail for showmatches() (and win_redr_status_matches()):
4114 * Find tail of file name path, but ignore trailing "/".
4115 */
4116 char_u *
4117sm_gettail(s)
4118 char_u *s;
4119{
4120 char_u *p;
4121 char_u *t = s;
4122 int had_sep = FALSE;
4123
4124 for (p = s; *p != NUL; )
4125 {
4126 if (vim_ispathsep(*p)
4127#ifdef BACKSLASH_IN_FILENAME
4128 && !rem_backslash(p)
4129#endif
4130 )
4131 had_sep = TRUE;
4132 else if (had_sep)
4133 {
4134 t = p;
4135 had_sep = FALSE;
4136 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004137 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139 return t;
4140}
4141
4142/*
4143 * Return TRUE if we only need to show the tail of completion matches.
4144 * When not completing file names or there is a wildcard in the path FALSE is
4145 * returned.
4146 */
4147 static int
4148expand_showtail(xp)
4149 expand_T *xp;
4150{
4151 char_u *s;
4152 char_u *end;
4153
4154 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004155 if (xp->xp_context != EXPAND_FILES
4156 && xp->xp_context != EXPAND_SHELLCMD
4157 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FALSE;
4159
4160 end = gettail(xp->xp_pattern);
4161 if (end == xp->xp_pattern) /* there is no path separator */
4162 return FALSE;
4163
4164 for (s = xp->xp_pattern; s < end; s++)
4165 {
4166 /* Skip escaped wildcards. Only when the backslash is not a path
4167 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4168 if (rem_backslash(s))
4169 ++s;
4170 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4171 return FALSE;
4172 }
4173 return TRUE;
4174}
4175
4176/*
4177 * Prepare a string for expansion.
4178 * When expanding file names: The string will be used with expand_wildcards().
4179 * Copy the file name into allocated memory and add a '*' at the end.
4180 * When expanding other names: The string will be used with regcomp(). Copy
4181 * the name into allocated memory and prepend "^".
4182 */
4183 char_u *
4184addstar(fname, len, context)
4185 char_u *fname;
4186 int len;
4187 int context; /* EXPAND_FILES etc. */
4188{
4189 char_u *retval;
4190 int i, j;
4191 int new_len;
4192 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004193 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004195 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004196 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004197 && context != EXPAND_SHELLCMD
4198 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 {
4200 /*
4201 * Matching will be done internally (on something other than files).
4202 * So we convert the file-matching-type wildcards into our kind for
4203 * use with vim_regcomp(). First work out how long it will be:
4204 */
4205
4206 /* For help tags the translation is done in find_help_tags().
4207 * For a tag pattern starting with "/" no translation is needed. */
4208 if (context == EXPAND_HELP
4209 || context == EXPAND_COLORS
4210 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004211 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004212 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 || (context == EXPAND_TAGS && fname[0] == '/'))
4214 retval = vim_strnsave(fname, len);
4215 else
4216 {
4217 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4218 for (i = 0; i < len; i++)
4219 {
4220 if (fname[i] == '*' || fname[i] == '~')
4221 new_len++; /* '*' needs to be replaced by ".*"
4222 '~' needs to be replaced by "\~" */
4223
4224 /* Buffer names are like file names. "." should be literal */
4225 if (context == EXPAND_BUFFERS && fname[i] == '.')
4226 new_len++; /* "." becomes "\." */
4227
4228 /* Custom expansion takes care of special things, match
4229 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004230 if ((context == EXPAND_USER_DEFINED
4231 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 new_len++; /* '\' becomes "\\" */
4233 }
4234 retval = alloc(new_len);
4235 if (retval != NULL)
4236 {
4237 retval[0] = '^';
4238 j = 1;
4239 for (i = 0; i < len; i++, j++)
4240 {
4241 /* Skip backslash. But why? At least keep it for custom
4242 * expansion. */
4243 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004244 && context != EXPAND_USER_LIST
4245 && fname[i] == '\\'
4246 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 break;
4248
4249 switch (fname[i])
4250 {
4251 case '*': retval[j++] = '.';
4252 break;
4253 case '~': retval[j++] = '\\';
4254 break;
4255 case '?': retval[j] = '.';
4256 continue;
4257 case '.': if (context == EXPAND_BUFFERS)
4258 retval[j++] = '\\';
4259 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004260 case '\\': if (context == EXPAND_USER_DEFINED
4261 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 retval[j++] = '\\';
4263 break;
4264 }
4265 retval[j] = fname[i];
4266 }
4267 retval[j] = NUL;
4268 }
4269 }
4270 }
4271 else
4272 {
4273 retval = alloc(len + 4);
4274 if (retval != NULL)
4275 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004276 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277
4278 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004279 * Don't add a star to *, ~, ~user, $var or `cmd`.
4280 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 * ~ would be at the start of the file name, but not the tail.
4282 * $ could be anywhere in the tail.
4283 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004284 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 */
4286 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004287 ends_in_star = (len > 0 && retval[len - 1] == '*');
4288#ifndef BACKSLASH_IN_FILENAME
4289 for (i = len - 2; i >= 0; --i)
4290 {
4291 if (retval[i] != '\\')
4292 break;
4293 ends_in_star = !ends_in_star;
4294 }
4295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004297 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 && vim_strchr(tail, '$') == NULL
4299 && vim_strchr(retval, '`') == NULL)
4300 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004301 else if (len > 0 && retval[len - 1] == '$')
4302 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 retval[len] = NUL;
4304 }
4305 }
4306 return retval;
4307}
4308
4309/*
4310 * Must parse the command line so far to work out what context we are in.
4311 * Completion can then be done based on that context.
4312 * This routine sets the variables:
4313 * xp->xp_pattern The start of the pattern to be expanded within
4314 * the command line (ends at the cursor).
4315 * xp->xp_context The type of thing to expand. Will be one of:
4316 *
4317 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4318 * the command line, like an unknown command. Caller
4319 * should beep.
4320 * EXPAND_NOTHING Unrecognised context for completion, use char like
4321 * a normal char, rather than for completion. eg
4322 * :s/^I/
4323 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4324 * it.
4325 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4326 * EXPAND_FILES After command with XFILE set, or after setting
4327 * with P_EXPAND set. eg :e ^I, :w>>^I
4328 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4329 * when we know only directories are of interest. eg
4330 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004331 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4333 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4334 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4335 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4336 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4337 * EXPAND_EVENTS Complete event names
4338 * EXPAND_SYNTAX Complete :syntax command arguments
4339 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4340 * EXPAND_AUGROUP Complete autocommand group names
4341 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4342 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4343 * eg :unmap a^I , :cunab x^I
4344 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4345 * eg :call sub^I
4346 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4347 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4348 * names in expressions, eg :while s^I
4349 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004350 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 */
4352 static void
4353set_expand_context(xp)
4354 expand_T *xp;
4355{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004356 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 if (ccline.cmdfirstc != ':'
4358#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004359 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004360 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361#endif
4362 )
4363 {
4364 xp->xp_context = EXPAND_NOTHING;
4365 return;
4366 }
4367 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4368}
4369
4370 void
4371set_cmd_context(xp, str, len, col)
4372 expand_T *xp;
4373 char_u *str; /* start of command line */
4374 int len; /* length of command line (excl. NUL) */
4375 int col; /* position of cursor */
4376{
4377 int old_char = NUL;
4378 char_u *nextcomm;
4379
4380 /*
4381 * Avoid a UMR warning from Purify, only save the character if it has been
4382 * written before.
4383 */
4384 if (col < len)
4385 old_char = str[col];
4386 str[col] = NUL;
4387 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004388
4389#ifdef FEAT_EVAL
4390 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004391 {
4392# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004393 /* pass CMD_SIZE because there is no real command */
4394 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004395# endif
4396 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004397 else if (ccline.input_fn)
4398 {
4399 xp->xp_context = ccline.xp_context;
4400 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004401# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004402 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004403# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004404 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004405 else
4406#endif
4407 while (nextcomm != NULL)
4408 nextcomm = set_one_cmd_context(xp, nextcomm);
4409
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 str[col] = old_char;
4411}
4412
4413/*
4414 * Expand the command line "str" from context "xp".
4415 * "xp" must have been set by set_cmd_context().
4416 * xp->xp_pattern points into "str", to where the text that is to be expanded
4417 * starts.
4418 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4419 * cursor.
4420 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4421 * key that triggered expansion literally.
4422 * Returns EXPAND_OK otherwise.
4423 */
4424 int
4425expand_cmdline(xp, str, col, matchcount, matches)
4426 expand_T *xp;
4427 char_u *str; /* start of command line */
4428 int col; /* position of cursor */
4429 int *matchcount; /* return: nr of matches */
4430 char_u ***matches; /* return: array of pointers to matches */
4431{
4432 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004433 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
4435 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4436 {
4437 beep_flush();
4438 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4439 }
4440 if (xp->xp_context == EXPAND_NOTHING)
4441 {
4442 /* Caller can use the character as a normal char instead */
4443 return EXPAND_NOTHING;
4444 }
4445
4446 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004447 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4448 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 if (file_str == NULL)
4450 return EXPAND_UNSUCCESSFUL;
4451
Bram Moolenaar94950a92010-12-02 16:01:29 +01004452 if (p_wic)
4453 options += WILD_ICASE;
4454
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004456 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
4458 *matchcount = 0;
4459 *matches = NULL;
4460 }
4461 vim_free(file_str);
4462
4463 return EXPAND_OK;
4464}
4465
4466#ifdef FEAT_MULTI_LANG
4467/*
4468 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4469 */
4470static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4471
4472 static void
4473cleanup_help_tags(num_file, file)
4474 int num_file;
4475 char_u **file;
4476{
4477 int i, j;
4478 int len;
4479
4480 for (i = 0; i < num_file; ++i)
4481 {
4482 len = (int)STRLEN(file[i]) - 3;
4483 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4484 {
4485 /* Sorting on priority means the same item in another language may
4486 * be anywhere. Search all items for a match up to the "@en". */
4487 for (j = 0; j < num_file; ++j)
4488 if (j != i
4489 && (int)STRLEN(file[j]) == len + 3
4490 && STRNCMP(file[i], file[j], len + 1) == 0)
4491 break;
4492 if (j == num_file)
4493 file[i][len] = NUL;
4494 }
4495 }
4496}
4497#endif
4498
4499/*
4500 * Do the expansion based on xp->xp_context and "pat".
4501 */
4502 static int
4503ExpandFromContext(xp, pat, num_file, file, options)
4504 expand_T *xp;
4505 char_u *pat;
4506 int *num_file;
4507 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004508 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
4510#ifdef FEAT_CMDL_COMPL
4511 regmatch_T regmatch;
4512#endif
4513 int ret;
4514 int flags;
4515
4516 flags = EW_DIR; /* include directories */
4517 if (options & WILD_LIST_NOTFOUND)
4518 flags |= EW_NOTFOUND;
4519 if (options & WILD_ADD_SLASH)
4520 flags |= EW_ADDSLASH;
4521 if (options & WILD_KEEP_ALL)
4522 flags |= EW_KEEPALL;
4523 if (options & WILD_SILENT)
4524 flags |= EW_SILENT;
4525
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004526 if (xp->xp_context == EXPAND_FILES
4527 || xp->xp_context == EXPAND_DIRECTORIES
4528 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
4530 /*
4531 * Expand file or directory names.
4532 */
4533 int free_pat = FALSE;
4534 int i;
4535
4536 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4537 * space */
4538 if (xp->xp_backslash != XP_BS_NONE)
4539 {
4540 free_pat = TRUE;
4541 pat = vim_strsave(pat);
4542 for (i = 0; pat[i]; ++i)
4543 if (pat[i] == '\\')
4544 {
4545 if (xp->xp_backslash == XP_BS_THREE
4546 && pat[i + 1] == '\\'
4547 && pat[i + 2] == '\\'
4548 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004549 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (xp->xp_backslash == XP_BS_ONE
4551 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004552 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554 }
4555
4556 if (xp->xp_context == EXPAND_FILES)
4557 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004558 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4559 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 else
4561 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004562 if (options & WILD_ICASE)
4563 flags |= EW_ICASE;
4564
Bram Moolenaard7834d32009-12-02 16:14:36 +00004565 /* Expand wildcards, supporting %:h and the like. */
4566 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 if (free_pat)
4568 vim_free(pat);
4569 return ret;
4570 }
4571
4572 *file = (char_u **)"";
4573 *num_file = 0;
4574 if (xp->xp_context == EXPAND_HELP)
4575 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004576 /* With an empty argument we would get all the help tags, which is
4577 * very slow. Get matches for "help" instead. */
4578 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4579 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
4581#ifdef FEAT_MULTI_LANG
4582 cleanup_help_tags(*num_file, *file);
4583#endif
4584 return OK;
4585 }
4586 return FAIL;
4587 }
4588
4589#ifndef FEAT_CMDL_COMPL
4590 return FAIL;
4591#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004592 if (xp->xp_context == EXPAND_SHELLCMD)
4593 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (xp->xp_context == EXPAND_OLD_SETTING)
4595 return ExpandOldSetting(num_file, file);
4596 if (xp->xp_context == EXPAND_BUFFERS)
4597 return ExpandBufnames(pat, num_file, file, options);
4598 if (xp->xp_context == EXPAND_TAGS
4599 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4600 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4601 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004602 {
4603 char *directories[] = {"colors", NULL};
4604 return ExpandRTDir(pat, num_file, file, directories);
4605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004607 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004608 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004609 return ExpandRTDir(pat, num_file, file, directories);
4610 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004611 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004612 {
4613 char *directories[] = {"syntax", NULL};
4614 return ExpandRTDir(pat, num_file, file, directories);
4615 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004616 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004617 {
4618 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4619 return ExpandRTDir(pat, num_file, file, directories);
4620 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004621# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4622 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004623 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004624# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
4626 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4627 if (regmatch.regprog == NULL)
4628 return FAIL;
4629
4630 /* set ignore-case according to p_ic, p_scs and pat */
4631 regmatch.rm_ic = ignorecase(pat);
4632
4633 if (xp->xp_context == EXPAND_SETTINGS
4634 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4635 ret = ExpandSettings(xp, &regmatch, num_file, file);
4636 else if (xp->xp_context == EXPAND_MAPPINGS)
4637 ret = ExpandMappings(&regmatch, num_file, file);
4638# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4639 else if (xp->xp_context == EXPAND_USER_DEFINED)
4640 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4641# endif
4642 else
4643 {
4644 static struct expgen
4645 {
4646 int context;
4647 char_u *((*func)__ARGS((expand_T *, int)));
4648 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004649 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 } tab[] =
4651 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004652 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4653 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004654#ifdef FEAT_CMDHIST
4655 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004658 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4659 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4660 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4661 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662#endif
4663#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004664 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4665 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4666 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4667 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668#endif
4669#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004670 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4671 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672#endif
4673#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004674 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004676 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004678 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4679 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004681#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004682 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004683#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004684#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004685 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004686#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004687#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004688 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4691 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004692 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4693 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004695 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004696 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 };
4698 int i;
4699
4700 /*
4701 * Find a context in the table and call the ExpandGeneric() with the
4702 * right function to do the expansion.
4703 */
4704 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004705 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 if (xp->xp_context == tab[i].context)
4707 {
4708 if (tab[i].ic)
4709 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004710 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004711 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 break;
4713 }
4714 }
4715
4716 vim_free(regmatch.regprog);
4717
4718 return ret;
4719#endif /* FEAT_CMDL_COMPL */
4720}
4721
4722#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4723/*
4724 * Expand a list of names.
4725 *
4726 * Generic function for command line completion. It calls a function to
4727 * obtain strings, one by one. The strings are matched against a regexp
4728 * program. Matching strings are copied into an array, which is returned.
4729 *
4730 * Returns OK when no problems encountered, FAIL for error (out of memory).
4731 */
4732 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004733ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 expand_T *xp;
4735 regmatch_T *regmatch;
4736 int *num_file;
4737 char_u ***file;
4738 char_u *((*func)__ARGS((expand_T *, int)));
4739 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004740 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
4742 int i;
4743 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004744 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 char_u *str;
4746
4747 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004748 * round == 0: count the number of matching names
4749 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004751 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
4753 for (i = 0; ; ++i)
4754 {
4755 str = (*func)(xp, i);
4756 if (str == NULL) /* end of list */
4757 break;
4758 if (*str == NUL) /* skip empty strings */
4759 continue;
4760
4761 if (vim_regexec(regmatch, str, (colnr_T)0))
4762 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004763 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004765 if (escaped)
4766 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4767 else
4768 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 (*file)[count] = str;
4770#ifdef FEAT_MENU
4771 if (func == get_menu_names && str != NULL)
4772 {
4773 /* test for separator added by get_menu_names() */
4774 str += STRLEN(str) - 1;
4775 if (*str == '\001')
4776 *str = '.';
4777 }
4778#endif
4779 }
4780 ++count;
4781 }
4782 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004783 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
4785 if (count == 0)
4786 return OK;
4787 *num_file = count;
4788 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4789 if (*file == NULL)
4790 {
4791 *file = (char_u **)"";
4792 return FAIL;
4793 }
4794 count = 0;
4795 }
4796 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004797
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004798 /* Sort the results. Keep menu's in the specified order. */
4799 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004800 {
4801 if (xp->xp_context == EXPAND_EXPRESSION
4802 || xp->xp_context == EXPAND_FUNCTIONS
4803 || xp->xp_context == EXPAND_USER_FUNC)
4804 /* <SNR> functions should be sorted to the end. */
4805 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4806 sort_func_compare);
4807 else
4808 sort_strings(*file, *num_file);
4809 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004810
Bram Moolenaar4f688582007-07-24 12:34:30 +00004811#ifdef FEAT_CMDL_COMPL
4812 /* Reset the variables used for special highlight names expansion, so that
4813 * they don't show up when getting normal highlight names by ID. */
4814 reset_expand_highlight();
4815#endif
4816
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 return OK;
4818}
4819
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004820/*
4821 * Complete a shell command.
4822 * Returns FAIL or OK;
4823 */
4824 static int
4825expand_shellcmd(filepat, num_file, file, flagsarg)
4826 char_u *filepat; /* pattern to match with command names */
4827 int *num_file; /* return: number of matches */
4828 char_u ***file; /* return: array with matches */
4829 int flagsarg; /* EW_ flags */
4830{
4831 char_u *pat;
4832 int i;
4833 char_u *path;
4834 int mustfree = FALSE;
4835 garray_T ga;
4836 char_u *buf = alloc(MAXPATHL);
4837 size_t l;
4838 char_u *s, *e;
4839 int flags = flagsarg;
4840 int ret;
4841
4842 if (buf == NULL)
4843 return FAIL;
4844
4845 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4846 * space */
4847 pat = vim_strsave(filepat);
4848 for (i = 0; pat[i]; ++i)
4849 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004850 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004851
4852 flags |= EW_FILE | EW_EXEC;
4853
4854 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004855 if (mch_isFullName(pat))
4856 path = (char_u *)" ";
4857 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004858 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4859 path = (char_u *)".";
4860 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004861 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004862 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004863 if (path == NULL)
4864 path = (char_u *)"";
4865 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004866
4867 /*
4868 * Go over all directories in $PATH. Expand matches in that directory and
4869 * collect them in "ga".
4870 */
4871 ga_init2(&ga, (int)sizeof(char *), 10);
4872 for (s = path; *s != NUL; s = e)
4873 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004874 if (*s == ' ')
4875 ++s; /* Skip space used for absolute path name. */
4876
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004877#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4878 e = vim_strchr(s, ';');
4879#else
4880 e = vim_strchr(s, ':');
4881#endif
4882 if (e == NULL)
4883 e = s + STRLEN(s);
4884
4885 l = e - s;
4886 if (l > MAXPATHL - 5)
4887 break;
4888 vim_strncpy(buf, s, l);
4889 add_pathsep(buf);
4890 l = STRLEN(buf);
4891 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4892
4893 /* Expand matches in one directory of $PATH. */
4894 ret = expand_wildcards(1, &buf, num_file, file, flags);
4895 if (ret == OK)
4896 {
4897 if (ga_grow(&ga, *num_file) == FAIL)
4898 FreeWild(*num_file, *file);
4899 else
4900 {
4901 for (i = 0; i < *num_file; ++i)
4902 {
4903 s = (*file)[i];
4904 if (STRLEN(s) > l)
4905 {
4906 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004907 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004908 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4909 }
4910 else
4911 vim_free(s);
4912 }
4913 vim_free(*file);
4914 }
4915 }
4916 if (*e != NUL)
4917 ++e;
4918 }
4919 *file = ga.ga_data;
4920 *num_file = ga.ga_len;
4921
4922 vim_free(buf);
4923 vim_free(pat);
4924 if (mustfree)
4925 vim_free(path);
4926 return OK;
4927}
4928
4929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004931static 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));
4932
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004934 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004935 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004937 static void *
4938call_user_expand_func(user_expand_func, xp, num_file, file)
4939 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 int *num_file;
4942 char_u ***file;
4943{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004944 char_u keep;
4945 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004948 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004949 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950
4951 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004952 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 *num_file = 0;
4954 *file = NULL;
4955
Bram Moolenaar21669c02008-01-18 12:16:16 +00004956 if (ccline.cmdbuff == NULL)
4957 {
4958 /* Completion from Insert mode, pass fake arguments. */
4959 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004960 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004961 args[1] = xp->xp_pattern;
4962 }
4963 else
4964 {
4965 /* Completion on the command line, pass real arguments. */
4966 keep = ccline.cmdbuff[ccline.cmdlen];
4967 ccline.cmdbuff[ccline.cmdlen] = 0;
4968 sprintf((char *)num, "%d", ccline.cmdpos);
4969 args[1] = ccline.cmdbuff;
4970 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004971 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 args[2] = num;
4973
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004974 /* Save the cmdline, we don't know what the function may do. */
4975 save_ccline = ccline;
4976 ccline.cmdbuff = NULL;
4977 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004979
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004980 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004981
4982 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004984 if (ccline.cmdbuff != NULL)
4985 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004986
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004987 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004988 return ret;
4989}
4990
4991/*
4992 * Expand names with a function defined by the user.
4993 */
4994 static int
4995ExpandUserDefined(xp, regmatch, num_file, file)
4996 expand_T *xp;
4997 regmatch_T *regmatch;
4998 int *num_file;
4999 char_u ***file;
5000{
5001 char_u *retstr;
5002 char_u *s;
5003 char_u *e;
5004 char_u keep;
5005 garray_T ga;
5006
5007 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5008 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 return FAIL;
5010
5011 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005012 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
5014 e = vim_strchr(s, '\n');
5015 if (e == NULL)
5016 e = s + STRLEN(s);
5017 keep = *e;
5018 *e = 0;
5019
5020 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5021 {
5022 *e = keep;
5023 if (*e != NUL)
5024 ++e;
5025 continue;
5026 }
5027
5028 if (ga_grow(&ga, 1) == FAIL)
5029 break;
5030
5031 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5032 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033
5034 *e = keep;
5035 if (*e != NUL)
5036 ++e;
5037 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005038 vim_free(retstr);
5039 *file = ga.ga_data;
5040 *num_file = ga.ga_len;
5041 return OK;
5042}
5043
5044/*
5045 * Expand names with a list returned by a function defined by the user.
5046 */
5047 static int
5048ExpandUserList(xp, num_file, file)
5049 expand_T *xp;
5050 int *num_file;
5051 char_u ***file;
5052{
5053 list_T *retlist;
5054 listitem_T *li;
5055 garray_T ga;
5056
5057 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5058 if (retlist == NULL)
5059 return FAIL;
5060
5061 ga_init2(&ga, (int)sizeof(char *), 3);
5062 /* Loop over the items in the list. */
5063 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5064 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005065 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5066 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005067
5068 if (ga_grow(&ga, 1) == FAIL)
5069 break;
5070
5071 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005072 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005073 ++ga.ga_len;
5074 }
5075 list_unref(retlist);
5076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 *file = ga.ga_data;
5078 *num_file = ga.ga_len;
5079 return OK;
5080}
5081#endif
5082
5083/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005084 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005085 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005086 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 */
5088 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005089ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 char_u *pat;
5091 int *num_file;
5092 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005093 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005095 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 char_u *s;
5097 char_u *e;
5098 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005099 int i;
5100 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101
5102 *num_file = 0;
5103 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005104 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005105 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005107 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005109 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5110 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005112 ga_clear_strings(&ga);
5113 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005115 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5116 matches = globpath(p_rtp, s, 0);
5117 vim_free(s);
5118 if (matches == NULL)
5119 continue;
5120
5121 for (s = matches; *s != NUL; s = e)
5122 {
5123 e = vim_strchr(s, '\n');
5124 if (e == NULL)
5125 e = s + STRLEN(s);
5126 if (ga_grow(&ga, 1) == FAIL)
5127 break;
5128 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5129 {
5130 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5131 if (*s == '\n' || vim_ispathsep(*s))
5132 break;
5133 ++s;
5134 ((char_u **)ga.ga_data)[ga.ga_len] =
5135 vim_strnsave(s, (int)(e - s - 4));
5136 ++ga.ga_len;
5137 }
5138 if (*e != NUL)
5139 ++e;
5140 }
5141 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005143 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005144 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005145
5146 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005147 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005148 remove_duplicates(&ga);
5149
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 *file = ga.ga_data;
5151 *num_file = ga.ga_len;
5152 return OK;
5153}
5154
5155#endif
5156
5157#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5158/*
5159 * Expand "file" for all comma-separated directories in "path".
5160 * Returns an allocated string with all matches concatenated, separated by
5161 * newlines. Returns NULL for an error or no matches.
5162 */
5163 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005164globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 char_u *path;
5166 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005167 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
5169 expand_T xpc;
5170 char_u *buf;
5171 garray_T ga;
5172 int i;
5173 int len;
5174 int num_p;
5175 char_u **p;
5176 char_u *cur = NULL;
5177
5178 buf = alloc(MAXPATHL);
5179 if (buf == NULL)
5180 return NULL;
5181
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005182 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 ga_init2(&ga, 1, 100);
5186
5187 /* Loop over all entries in {path}. */
5188 while (*path != NUL)
5189 {
5190 /* Copy one item of the path to buf[] and concatenate the file name. */
5191 copy_option_part(&path, buf, MAXPATHL, ",");
5192 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5193 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005194# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005195 /* Using the platform's path separator (\) makes vim incorrectly
5196 * treat it as an escape character, use '/' instead. */
5197 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5198 STRCAT(buf, "/");
5199# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005201# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005203 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5204 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005206 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005208 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209
5210 /* Concatenate new results to previous ones. */
5211 if (ga_grow(&ga, len) == OK)
5212 {
5213 cur = (char_u *)ga.ga_data + ga.ga_len;
5214 for (i = 0; i < num_p; ++i)
5215 {
5216 STRCPY(cur, p[i]);
5217 cur += STRLEN(p[i]);
5218 *cur++ = '\n';
5219 }
5220 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
5222 FreeWild(num_p, p);
5223 }
5224 }
5225 }
5226 if (cur != NULL)
5227 *--cur = 0; /* Replace trailing newline with NUL */
5228
5229 vim_free(buf);
5230 return (char_u *)ga.ga_data;
5231}
5232
5233#endif
5234
5235#if defined(FEAT_CMDHIST) || defined(PROTO)
5236
5237/*********************************
5238 * Command line history stuff *
5239 *********************************/
5240
5241/*
5242 * Translate a history character to the associated type number.
5243 */
5244 static int
5245hist_char2type(c)
5246 int c;
5247{
5248 if (c == ':')
5249 return HIST_CMD;
5250 if (c == '=')
5251 return HIST_EXPR;
5252 if (c == '@')
5253 return HIST_INPUT;
5254 if (c == '>')
5255 return HIST_DEBUG;
5256 return HIST_SEARCH; /* must be '?' or '/' */
5257}
5258
5259/*
5260 * Table of history names.
5261 * These names are used in :history and various hist...() functions.
5262 * It is sufficient to give the significant prefix of a history name.
5263 */
5264
5265static char *(history_names[]) =
5266{
5267 "cmd",
5268 "search",
5269 "expr",
5270 "input",
5271 "debug",
5272 NULL
5273};
5274
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005275#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5276/*
5277 * Function given to ExpandGeneric() to obtain the possible first
5278 * arguments of the ":history command.
5279 */
5280 static char_u *
5281get_history_arg(xp, idx)
5282 expand_T *xp UNUSED;
5283 int idx;
5284{
5285 static char_u compl[2] = { NUL, NUL };
5286 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005287 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005288 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5289
5290 if (idx < short_names_count)
5291 {
5292 compl[0] = (char_u)short_names[idx];
5293 return compl;
5294 }
5295 if (idx < short_names_count + history_name_count)
5296 return (char_u *)history_names[idx - short_names_count];
5297 if (idx == short_names_count + history_name_count)
5298 return (char_u *)"all";
5299 return NULL;
5300}
5301#endif
5302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303/*
5304 * init_history() - Initialize the command line history.
5305 * Also used to re-allocate the history when the size changes.
5306 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005307 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308init_history()
5309{
5310 int newlen; /* new length of history table */
5311 histentry_T *temp;
5312 int i;
5313 int j;
5314 int type;
5315
5316 /*
5317 * If size of history table changed, reallocate it
5318 */
5319 newlen = (int)p_hi;
5320 if (newlen != hislen) /* history length changed */
5321 {
5322 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5323 {
5324 if (newlen)
5325 {
5326 temp = (histentry_T *)lalloc(
5327 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5328 if (temp == NULL) /* out of memory! */
5329 {
5330 if (type == 0) /* first one: just keep the old length */
5331 {
5332 newlen = hislen;
5333 break;
5334 }
5335 /* Already changed one table, now we can only have zero
5336 * length for all tables. */
5337 newlen = 0;
5338 type = -1;
5339 continue;
5340 }
5341 }
5342 else
5343 temp = NULL;
5344 if (newlen == 0 || temp != NULL)
5345 {
5346 if (hisidx[type] < 0) /* there are no entries yet */
5347 {
5348 for (i = 0; i < newlen; ++i)
5349 {
5350 temp[i].hisnum = 0;
5351 temp[i].hisstr = NULL;
5352 }
5353 }
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)
5360 {
5361 temp[i].hisnum = 0;
5362 temp[i].hisstr = NULL;
5363 }
5364 for ( ; j < hislen; ++i, ++j)
5365 temp[i] = history[type][j];
5366 }
5367 else /* array becomes smaller or 0 */
5368 {
5369 j = hisidx[type];
5370 for (i = newlen - 1; ; --i)
5371 {
5372 if (i >= 0) /* copy newest entries */
5373 temp[i] = history[type][j];
5374 else /* remove older entries */
5375 vim_free(history[type][j].hisstr);
5376 if (--j < 0)
5377 j = hislen - 1;
5378 if (j == hisidx[type])
5379 break;
5380 }
5381 hisidx[type] = newlen - 1;
5382 }
5383 vim_free(history[type]);
5384 history[type] = temp;
5385 }
5386 }
5387 hislen = newlen;
5388 }
5389}
5390
5391/*
5392 * Check if command line 'str' is already in history.
5393 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5394 */
5395 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005396in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 int type;
5398 char_u *str;
5399 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005400 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
5402 int i;
5403 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005404 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
5406 if (hisidx[type] < 0)
5407 return FALSE;
5408 i = hisidx[type];
5409 do
5410 {
5411 if (history[type][i].hisstr == NULL)
5412 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005413
5414 /* For search history, check that the separator character matches as
5415 * well. */
5416 p = history[type][i].hisstr;
5417 if (STRCMP(str, p) == 0
5418 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
5420 if (!move_to_front)
5421 return TRUE;
5422 last_i = i;
5423 break;
5424 }
5425 if (--i < 0)
5426 i = hislen - 1;
5427 } while (i != hisidx[type]);
5428
5429 if (last_i >= 0)
5430 {
5431 str = history[type][i].hisstr;
5432 while (i != hisidx[type])
5433 {
5434 if (++i >= hislen)
5435 i = 0;
5436 history[type][last_i] = history[type][i];
5437 last_i = i;
5438 }
5439 history[type][i].hisstr = str;
5440 history[type][i].hisnum = ++hisnum[type];
5441 return TRUE;
5442 }
5443 return FALSE;
5444}
5445
5446/*
5447 * Convert history name (from table above) to its HIST_ equivalent.
5448 * When "name" is empty, return "cmd" history.
5449 * Returns -1 for unknown history name.
5450 */
5451 int
5452get_histtype(name)
5453 char_u *name;
5454{
5455 int i;
5456 int len = (int)STRLEN(name);
5457
5458 /* No argument: use current history. */
5459 if (len == 0)
5460 return hist_char2type(ccline.cmdfirstc);
5461
5462 for (i = 0; history_names[i] != NULL; ++i)
5463 if (STRNICMP(name, history_names[i], len) == 0)
5464 return i;
5465
5466 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5467 return hist_char2type(name[0]);
5468
5469 return -1;
5470}
5471
5472static int last_maptick = -1; /* last seen maptick */
5473
5474/*
5475 * Add the given string to the given history. If the string is already in the
5476 * history then it is moved to the front. "histype" may be one of he HIST_
5477 * values.
5478 */
5479 void
5480add_to_history(histype, new_entry, in_map, sep)
5481 int histype;
5482 char_u *new_entry;
5483 int in_map; /* consider maptick when inside a mapping */
5484 int sep; /* separator character used (search hist) */
5485{
5486 histentry_T *hisptr;
5487 int len;
5488
5489 if (hislen == 0) /* no history */
5490 return;
5491
5492 /*
5493 * Searches inside the same mapping overwrite each other, so that only
5494 * the last line is kept. Be careful not to remove a line that was moved
5495 * down, only lines that were added.
5496 */
5497 if (histype == HIST_SEARCH && in_map)
5498 {
5499 if (maptick == last_maptick)
5500 {
5501 /* Current line is from the same mapping, remove it */
5502 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5503 vim_free(hisptr->hisstr);
5504 hisptr->hisstr = NULL;
5505 hisptr->hisnum = 0;
5506 --hisnum[histype];
5507 if (--hisidx[HIST_SEARCH] < 0)
5508 hisidx[HIST_SEARCH] = hislen - 1;
5509 }
5510 last_maptick = -1;
5511 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005512 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
5514 if (++hisidx[histype] == hislen)
5515 hisidx[histype] = 0;
5516 hisptr = &history[histype][hisidx[histype]];
5517 vim_free(hisptr->hisstr);
5518
5519 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005520 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5522 if (hisptr->hisstr != NULL)
5523 hisptr->hisstr[len + 1] = sep;
5524
5525 hisptr->hisnum = ++hisnum[histype];
5526 if (histype == HIST_SEARCH && in_map)
5527 last_maptick = maptick;
5528 }
5529}
5530
5531#if defined(FEAT_EVAL) || defined(PROTO)
5532
5533/*
5534 * Get identifier of newest history entry.
5535 * "histype" may be one of the HIST_ values.
5536 */
5537 int
5538get_history_idx(histype)
5539 int histype;
5540{
5541 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5542 || hisidx[histype] < 0)
5543 return -1;
5544
5545 return history[histype][hisidx[histype]].hisnum;
5546}
5547
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005548static struct cmdline_info *get_ccline_ptr __ARGS((void));
5549
5550/*
5551 * Get pointer to the command line info to use. cmdline_paste() may clear
5552 * ccline and put the previous value in prev_ccline.
5553 */
5554 static struct cmdline_info *
5555get_ccline_ptr()
5556{
5557 if ((State & CMDLINE) == 0)
5558 return NULL;
5559 if (ccline.cmdbuff != NULL)
5560 return &ccline;
5561 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5562 return &prev_ccline;
5563 return NULL;
5564}
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566/*
5567 * Get the current command line in allocated memory.
5568 * Only works when the command line is being edited.
5569 * Returns NULL when something is wrong.
5570 */
5571 char_u *
5572get_cmdline_str()
5573{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005574 struct cmdline_info *p = get_ccline_ptr();
5575
5576 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005578 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579}
5580
5581/*
5582 * Get the current command line position, counted in bytes.
5583 * Zero is the first position.
5584 * Only works when the command line is being edited.
5585 * Returns -1 when something is wrong.
5586 */
5587 int
5588get_cmdline_pos()
5589{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005590 struct cmdline_info *p = get_ccline_ptr();
5591
5592 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005594 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595}
5596
5597/*
5598 * Set the command line byte position to "pos". Zero is the first position.
5599 * Only works when the command line is being edited.
5600 * Returns 1 when failed, 0 when OK.
5601 */
5602 int
5603set_cmdline_pos(pos)
5604 int pos;
5605{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005606 struct cmdline_info *p = get_ccline_ptr();
5607
5608 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 return 1;
5610
5611 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5612 * changed the command line. */
5613 if (pos < 0)
5614 new_cmdpos = 0;
5615 else
5616 new_cmdpos = pos;
5617 return 0;
5618}
5619
5620/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005621 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005622 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005623 * Only works when the command line is being edited.
5624 * Returns NUL when something is wrong.
5625 */
5626 int
5627get_cmdline_type()
5628{
5629 struct cmdline_info *p = get_ccline_ptr();
5630
5631 if (p == NULL)
5632 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005633 if (p->cmdfirstc == NUL)
5634 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005635 return p->cmdfirstc;
5636}
5637
5638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 * Calculate history index from a number:
5640 * num > 0: seen as identifying number of a history entry
5641 * num < 0: relative position in history wrt newest entry
5642 * "histype" may be one of the HIST_ values.
5643 */
5644 static int
5645calc_hist_idx(histype, num)
5646 int histype;
5647 int num;
5648{
5649 int i;
5650 histentry_T *hist;
5651 int wrapped = FALSE;
5652
5653 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5654 || (i = hisidx[histype]) < 0 || num == 0)
5655 return -1;
5656
5657 hist = history[histype];
5658 if (num > 0)
5659 {
5660 while (hist[i].hisnum > num)
5661 if (--i < 0)
5662 {
5663 if (wrapped)
5664 break;
5665 i += hislen;
5666 wrapped = TRUE;
5667 }
5668 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5669 return i;
5670 }
5671 else if (-num <= hislen)
5672 {
5673 i += num + 1;
5674 if (i < 0)
5675 i += hislen;
5676 if (hist[i].hisstr != NULL)
5677 return i;
5678 }
5679 return -1;
5680}
5681
5682/*
5683 * Get a history entry by its index.
5684 * "histype" may be one of the HIST_ values.
5685 */
5686 char_u *
5687get_history_entry(histype, idx)
5688 int histype;
5689 int idx;
5690{
5691 idx = calc_hist_idx(histype, idx);
5692 if (idx >= 0)
5693 return history[histype][idx].hisstr;
5694 else
5695 return (char_u *)"";
5696}
5697
5698/*
5699 * Clear all entries of a history.
5700 * "histype" may be one of the HIST_ values.
5701 */
5702 int
5703clr_history(histype)
5704 int histype;
5705{
5706 int i;
5707 histentry_T *hisptr;
5708
5709 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5710 {
5711 hisptr = history[histype];
5712 for (i = hislen; i--;)
5713 {
5714 vim_free(hisptr->hisstr);
5715 hisptr->hisnum = 0;
5716 hisptr++->hisstr = NULL;
5717 }
5718 hisidx[histype] = -1; /* mark history as cleared */
5719 hisnum[histype] = 0; /* reset identifier counter */
5720 return OK;
5721 }
5722 return FAIL;
5723}
5724
5725/*
5726 * Remove all entries matching {str} from a history.
5727 * "histype" may be one of the HIST_ values.
5728 */
5729 int
5730del_history_entry(histype, str)
5731 int histype;
5732 char_u *str;
5733{
5734 regmatch_T regmatch;
5735 histentry_T *hisptr;
5736 int idx;
5737 int i;
5738 int last;
5739 int found = FALSE;
5740
5741 regmatch.regprog = NULL;
5742 regmatch.rm_ic = FALSE; /* always match case */
5743 if (hislen != 0
5744 && histype >= 0
5745 && histype < HIST_COUNT
5746 && *str != NUL
5747 && (idx = hisidx[histype]) >= 0
5748 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5749 != NULL)
5750 {
5751 i = last = idx;
5752 do
5753 {
5754 hisptr = &history[histype][i];
5755 if (hisptr->hisstr == NULL)
5756 break;
5757 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5758 {
5759 found = TRUE;
5760 vim_free(hisptr->hisstr);
5761 hisptr->hisstr = NULL;
5762 hisptr->hisnum = 0;
5763 }
5764 else
5765 {
5766 if (i != last)
5767 {
5768 history[histype][last] = *hisptr;
5769 hisptr->hisstr = NULL;
5770 hisptr->hisnum = 0;
5771 }
5772 if (--last < 0)
5773 last += hislen;
5774 }
5775 if (--i < 0)
5776 i += hislen;
5777 } while (i != idx);
5778 if (history[histype][idx].hisstr == NULL)
5779 hisidx[histype] = -1;
5780 }
5781 vim_free(regmatch.regprog);
5782 return found;
5783}
5784
5785/*
5786 * Remove an indexed entry from a history.
5787 * "histype" may be one of the HIST_ values.
5788 */
5789 int
5790del_history_idx(histype, idx)
5791 int histype;
5792 int idx;
5793{
5794 int i, j;
5795
5796 i = calc_hist_idx(histype, idx);
5797 if (i < 0)
5798 return FALSE;
5799 idx = hisidx[histype];
5800 vim_free(history[histype][i].hisstr);
5801
5802 /* When deleting the last added search string in a mapping, reset
5803 * last_maptick, so that the last added search string isn't deleted again.
5804 */
5805 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5806 last_maptick = -1;
5807
5808 while (i != idx)
5809 {
5810 j = (i + 1) % hislen;
5811 history[histype][i] = history[histype][j];
5812 i = j;
5813 }
5814 history[histype][i].hisstr = NULL;
5815 history[histype][i].hisnum = 0;
5816 if (--i < 0)
5817 i += hislen;
5818 hisidx[histype] = i;
5819 return TRUE;
5820}
5821
5822#endif /* FEAT_EVAL */
5823
5824#if defined(FEAT_CRYPT) || defined(PROTO)
5825/*
5826 * Very specific function to remove the value in ":set key=val" from the
5827 * history.
5828 */
5829 void
5830remove_key_from_history()
5831{
5832 char_u *p;
5833 int i;
5834
5835 i = hisidx[HIST_CMD];
5836 if (i < 0)
5837 return;
5838 p = history[HIST_CMD][i].hisstr;
5839 if (p != NULL)
5840 for ( ; *p; ++p)
5841 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5842 {
5843 p = vim_strchr(p + 3, '=');
5844 if (p == NULL)
5845 break;
5846 ++p;
5847 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5848 if (p[i] == '\\' && p[i + 1])
5849 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005850 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 --p;
5852 }
5853}
5854#endif
5855
5856#endif /* FEAT_CMDHIST */
5857
5858#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5859/*
5860 * Get indices "num1,num2" that specify a range within a list (not a range of
5861 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5862 * Returns OK if parsed successfully, otherwise FAIL.
5863 */
5864 int
5865get_list_range(str, num1, num2)
5866 char_u **str;
5867 int *num1;
5868 int *num2;
5869{
5870 int len;
5871 int first = FALSE;
5872 long num;
5873
5874 *str = skipwhite(*str);
5875 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5876 {
5877 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5878 *str += len;
5879 *num1 = (int)num;
5880 first = TRUE;
5881 }
5882 *str = skipwhite(*str);
5883 if (**str == ',') /* parse "to" part of range */
5884 {
5885 *str = skipwhite(*str + 1);
5886 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5887 if (len > 0)
5888 {
5889 *num2 = (int)num;
5890 *str = skipwhite(*str + len);
5891 }
5892 else if (!first) /* no number given at all */
5893 return FAIL;
5894 }
5895 else if (first) /* only one number given */
5896 *num2 = *num1;
5897 return OK;
5898}
5899#endif
5900
5901#if defined(FEAT_CMDHIST) || defined(PROTO)
5902/*
5903 * :history command - print a history
5904 */
5905 void
5906ex_history(eap)
5907 exarg_T *eap;
5908{
5909 histentry_T *hist;
5910 int histype1 = HIST_CMD;
5911 int histype2 = HIST_CMD;
5912 int hisidx1 = 1;
5913 int hisidx2 = -1;
5914 int idx;
5915 int i, j, k;
5916 char_u *end;
5917 char_u *arg = eap->arg;
5918
5919 if (hislen == 0)
5920 {
5921 MSG(_("'history' option is zero"));
5922 return;
5923 }
5924
5925 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5926 {
5927 end = arg;
5928 while (ASCII_ISALPHA(*end)
5929 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5930 end++;
5931 i = *end;
5932 *end = NUL;
5933 histype1 = get_histtype(arg);
5934 if (histype1 == -1)
5935 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005936 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 {
5938 histype1 = 0;
5939 histype2 = HIST_COUNT-1;
5940 }
5941 else
5942 {
5943 *end = i;
5944 EMSG(_(e_trailing));
5945 return;
5946 }
5947 }
5948 else
5949 histype2 = histype1;
5950 *end = i;
5951 }
5952 else
5953 end = arg;
5954 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5955 {
5956 EMSG(_(e_trailing));
5957 return;
5958 }
5959
5960 for (; !got_int && histype1 <= histype2; ++histype1)
5961 {
5962 STRCPY(IObuff, "\n # ");
5963 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5964 MSG_PUTS_TITLE(IObuff);
5965 idx = hisidx[histype1];
5966 hist = history[histype1];
5967 j = hisidx1;
5968 k = hisidx2;
5969 if (j < 0)
5970 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5971 if (k < 0)
5972 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5973 if (idx >= 0 && j <= k)
5974 for (i = idx + 1; !got_int; ++i)
5975 {
5976 if (i == hislen)
5977 i = 0;
5978 if (hist[i].hisstr != NULL
5979 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5980 {
5981 msg_putchar('\n');
5982 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5983 hist[i].hisnum);
5984 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5985 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005986 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 else
5988 STRCAT(IObuff, hist[i].hisstr);
5989 msg_outtrans(IObuff);
5990 out_flush();
5991 }
5992 if (i == idx)
5993 break;
5994 }
5995 }
5996}
5997#endif
5998
5999#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
6000static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
6001static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
6002static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
6003static int viminfo_add_at_front = FALSE;
6004
6005static int hist_type2char __ARGS((int type, int use_question));
6006
6007/*
6008 * Translate a history type number to the associated character.
6009 */
6010 static int
6011hist_type2char(type, use_question)
6012 int type;
6013 int use_question; /* use '?' instead of '/' */
6014{
6015 if (type == HIST_CMD)
6016 return ':';
6017 if (type == HIST_SEARCH)
6018 {
6019 if (use_question)
6020 return '?';
6021 else
6022 return '/';
6023 }
6024 if (type == HIST_EXPR)
6025 return '=';
6026 return '@';
6027}
6028
6029/*
6030 * Prepare for reading the history from the viminfo file.
6031 * This allocates history arrays to store the read history lines.
6032 */
6033 void
6034prepare_viminfo_history(asklen)
6035 int asklen;
6036{
6037 int i;
6038 int num;
6039 int type;
6040 int len;
6041
6042 init_history();
6043 viminfo_add_at_front = (asklen != 0);
6044 if (asklen > hislen)
6045 asklen = hislen;
6046
6047 for (type = 0; type < HIST_COUNT; ++type)
6048 {
6049 /*
6050 * Count the number of empty spaces in the history list. If there are
6051 * more spaces available than we request, then fill them up.
6052 */
6053 for (i = 0, num = 0; i < hislen; i++)
6054 if (history[type][i].hisstr == NULL)
6055 num++;
6056 len = asklen;
6057 if (num > len)
6058 len = num;
6059 if (len <= 0)
6060 viminfo_history[type] = NULL;
6061 else
6062 viminfo_history[type] =
6063 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6064 if (viminfo_history[type] == NULL)
6065 len = 0;
6066 viminfo_hislen[type] = len;
6067 viminfo_hisidx[type] = 0;
6068 }
6069}
6070
6071/*
6072 * Accept a line from the viminfo, store it in the history array when it's
6073 * new.
6074 */
6075 int
6076read_viminfo_history(virp)
6077 vir_T *virp;
6078{
6079 int type;
6080 long_u len;
6081 char_u *val;
6082 char_u *p;
6083
6084 type = hist_char2type(virp->vir_line[0]);
6085 if (viminfo_hisidx[type] < viminfo_hislen[type])
6086 {
6087 val = viminfo_readstring(virp, 1, TRUE);
6088 if (val != NULL && *val != NUL)
6089 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006090 int sep = (*val == ' ' ? NUL : *val);
6091
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006093 viminfo_add_at_front, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 {
6095 /* Need to re-allocate to append the separator byte. */
6096 len = STRLEN(val);
6097 p = lalloc(len + 2, TRUE);
6098 if (p != NULL)
6099 {
6100 if (type == HIST_SEARCH)
6101 {
6102 /* Search entry: Move the separator from the first
6103 * column to after the NUL. */
6104 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006105 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 }
6107 else
6108 {
6109 /* Not a search entry: No separator in the viminfo
6110 * file, add a NUL separator. */
6111 mch_memmove(p, val, (size_t)len + 1);
6112 p[len + 1] = NUL;
6113 }
6114 viminfo_history[type][viminfo_hisidx[type]++] = p;
6115 }
6116 }
6117 }
6118 vim_free(val);
6119 }
6120 return viminfo_readline(virp);
6121}
6122
6123 void
6124finish_viminfo_history()
6125{
6126 int idx;
6127 int i;
6128 int type;
6129
6130 for (type = 0; type < HIST_COUNT; ++type)
6131 {
6132 if (history[type] == NULL)
6133 return;
6134 idx = hisidx[type] + viminfo_hisidx[type];
6135 if (idx >= hislen)
6136 idx -= hislen;
6137 else if (idx < 0)
6138 idx = hislen - 1;
6139 if (viminfo_add_at_front)
6140 hisidx[type] = idx;
6141 else
6142 {
6143 if (hisidx[type] == -1)
6144 hisidx[type] = hislen - 1;
6145 do
6146 {
6147 if (history[type][idx].hisstr != NULL)
6148 break;
6149 if (++idx == hislen)
6150 idx = 0;
6151 } while (idx != hisidx[type]);
6152 if (idx != hisidx[type] && --idx < 0)
6153 idx = hislen - 1;
6154 }
6155 for (i = 0; i < viminfo_hisidx[type]; i++)
6156 {
6157 vim_free(history[type][idx].hisstr);
6158 history[type][idx].hisstr = viminfo_history[type][i];
6159 if (--idx < 0)
6160 idx = hislen - 1;
6161 }
6162 idx += 1;
6163 idx %= hislen;
6164 for (i = 0; i < viminfo_hisidx[type]; i++)
6165 {
6166 history[type][idx++].hisnum = ++hisnum[type];
6167 idx %= hislen;
6168 }
6169 vim_free(viminfo_history[type]);
6170 viminfo_history[type] = NULL;
6171 }
6172}
6173
6174 void
6175write_viminfo_history(fp)
6176 FILE *fp;
6177{
6178 int i;
6179 int type;
6180 int num_saved;
6181 char_u *p;
6182 int c;
6183
6184 init_history();
6185 if (hislen == 0)
6186 return;
6187 for (type = 0; type < HIST_COUNT; ++type)
6188 {
6189 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6190 if (num_saved == 0)
6191 continue;
6192 if (num_saved < 0) /* Use default */
6193 num_saved = hislen;
6194 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6195 type == HIST_CMD ? _("Command Line") :
6196 type == HIST_SEARCH ? _("Search String") :
6197 type == HIST_EXPR ? _("Expression") :
6198 _("Input Line"));
6199 if (num_saved > hislen)
6200 num_saved = hislen;
6201 i = hisidx[type];
6202 if (i >= 0)
6203 while (num_saved--)
6204 {
6205 p = history[type][i].hisstr;
6206 if (p != NULL)
6207 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006208 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 /* For the search history: put the separator in the second
6210 * column; use a space if there isn't one. */
6211 if (type == HIST_SEARCH)
6212 {
6213 c = p[STRLEN(p) + 1];
6214 putc(c == NUL ? ' ' : c, fp);
6215 }
6216 viminfo_writestring(fp, p);
6217 }
6218 if (--i < 0)
6219 i = hislen - 1;
6220 }
6221 }
6222}
6223#endif /* FEAT_VIMINFO */
6224
6225#if defined(FEAT_FKMAP) || defined(PROTO)
6226/*
6227 * Write a character at the current cursor+offset position.
6228 * It is directly written into the command buffer block.
6229 */
6230 void
6231cmd_pchar(c, offset)
6232 int c, offset;
6233{
6234 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6235 {
6236 EMSG(_("E198: cmd_pchar beyond the command length"));
6237 return;
6238 }
6239 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6240 ccline.cmdbuff[ccline.cmdlen] = NUL;
6241}
6242
6243 int
6244cmd_gchar(offset)
6245 int offset;
6246{
6247 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6248 {
6249 /* EMSG(_("cmd_gchar beyond the command length")); */
6250 return NUL;
6251 }
6252 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6253}
6254#endif
6255
6256#if defined(FEAT_CMDWIN) || defined(PROTO)
6257/*
6258 * Open a window on the current command line and history. Allow editing in
6259 * the window. Returns when the window is closed.
6260 * Returns:
6261 * CR if the command is to be executed
6262 * Ctrl_C if it is to be abandoned
6263 * K_IGNORE if editing continues
6264 */
6265 static int
6266ex_window()
6267{
6268 struct cmdline_info save_ccline;
6269 buf_T *old_curbuf = curbuf;
6270 win_T *old_curwin = curwin;
6271 buf_T *bp;
6272 win_T *wp;
6273 int i;
6274 linenr_T lnum;
6275 int histtype;
6276 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006277#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 int save_restart_edit = restart_edit;
6281 int save_State = State;
6282 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006283#ifdef FEAT_RIGHTLEFT
6284 int save_cmdmsg_rl = cmdmsg_rl;
6285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286
6287 /* Can't do this recursively. Can't do it when typing a password. */
6288 if (cmdwin_type != 0
6289# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6290 || cmdline_star > 0
6291# endif
6292 )
6293 {
6294 beep_flush();
6295 return K_IGNORE;
6296 }
6297
6298 /* Save current window sizes. */
6299 win_size_save(&winsizes);
6300
6301# ifdef FEAT_AUTOCMD
6302 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006303 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006305 /* don't use a new tab page */
6306 cmdmod.tab = 0;
6307
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 /* Create a window for the command-line buffer. */
6309 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6310 {
6311 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006312# ifdef FEAT_AUTOCMD
6313 unblock_autocmds();
6314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 return K_IGNORE;
6316 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006317 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
6319 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006320 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006321 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6323 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6324 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006325#ifdef FEAT_FOLDING
6326 curwin->w_p_fen = FALSE;
6327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006329 curwin->w_p_rl = cmdmsg_rl;
6330 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006332 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334# ifdef FEAT_AUTOCMD
6335 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006336 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337# endif
6338
Bram Moolenaar46152342005-09-07 21:18:43 +00006339 /* Showing the prompt may have set need_wait_return, reset it. */
6340 need_wait_return = FALSE;
6341
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006342 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6344 {
6345 if (p_wc == TAB)
6346 {
6347 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6348 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6349 }
6350 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6351 }
6352
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006353 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6354 * sets 'textwidth' to 78). */
6355 curbuf->b_p_tw = 0;
6356
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 /* Fill the buffer with the history. */
6358 init_history();
6359 if (hislen > 0)
6360 {
6361 i = hisidx[histtype];
6362 if (i >= 0)
6363 {
6364 lnum = 0;
6365 do
6366 {
6367 if (++i == hislen)
6368 i = 0;
6369 if (history[histtype][i].hisstr != NULL)
6370 ml_append(lnum++, history[histtype][i].hisstr,
6371 (colnr_T)0, FALSE);
6372 }
6373 while (i != hisidx[histtype]);
6374 }
6375 }
6376
6377 /* Replace the empty last line with the current command-line and put the
6378 * cursor there. */
6379 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6380 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6381 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006382 changed_line_abv_curs();
6383 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006384 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
6386 /* Save the command line info, can be used recursively. */
6387 save_ccline = ccline;
6388 ccline.cmdbuff = NULL;
6389 ccline.cmdprompt = NULL;
6390
6391 /* No Ex mode here! */
6392 exmode_active = 0;
6393
6394 State = NORMAL;
6395# ifdef FEAT_MOUSE
6396 setmouse();
6397# endif
6398
6399# ifdef FEAT_AUTOCMD
6400 /* Trigger CmdwinEnter autocommands. */
6401 typestr[0] = cmdwin_type;
6402 typestr[1] = NUL;
6403 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006404 if (restart_edit != 0) /* autocmd with ":startinsert" */
6405 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406# endif
6407
6408 i = RedrawingDisabled;
6409 RedrawingDisabled = 0;
6410
6411 /*
6412 * Call the main loop until <CR> or CTRL-C is typed.
6413 */
6414 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006415 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416
6417 RedrawingDisabled = i;
6418
6419# ifdef FEAT_AUTOCMD
6420 /* Trigger CmdwinLeave autocommands. */
6421 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6422# endif
6423
Bram Moolenaarccc18222007-05-10 18:25:20 +00006424 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 ccline = save_ccline;
6426 cmdwin_type = 0;
6427
6428 exmode_active = save_exmode;
6429
Bram Moolenaarf9821062008-06-20 16:31:07 +00006430 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 * this happens! */
6432 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6433 {
6434 cmdwin_result = Ctrl_C;
6435 EMSG(_("E199: Active window or buffer deleted"));
6436 }
6437 else
6438 {
6439# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6440 /* autocmds may abort script processing */
6441 if (aborting() && cmdwin_result != K_IGNORE)
6442 cmdwin_result = Ctrl_C;
6443# endif
6444 /* Set the new command line from the cmdline buffer. */
6445 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006446 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006448 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6449
6450 if (histtype == HIST_CMD)
6451 {
6452 /* Execute the command directly. */
6453 ccline.cmdbuff = vim_strsave((char_u *)p);
6454 cmdwin_result = CAR;
6455 }
6456 else
6457 {
6458 /* First need to cancel what we were doing. */
6459 ccline.cmdbuff = NULL;
6460 stuffcharReadbuff(':');
6461 stuffReadbuff((char_u *)p);
6462 stuffcharReadbuff(CAR);
6463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 }
6465 else if (cmdwin_result == K_XF2) /* :qa typed */
6466 {
6467 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6468 cmdwin_result = CAR;
6469 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006470 else if (cmdwin_result == Ctrl_C)
6471 {
6472 /* :q or :close, don't execute any command
6473 * and don't modify the cmd window. */
6474 ccline.cmdbuff = NULL;
6475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 else
6477 ccline.cmdbuff = vim_strsave(ml_get_curline());
6478 if (ccline.cmdbuff == NULL)
6479 cmdwin_result = Ctrl_C;
6480 else
6481 {
6482 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6483 ccline.cmdbufflen = ccline.cmdlen + 1;
6484 ccline.cmdpos = curwin->w_cursor.col;
6485 if (ccline.cmdpos > ccline.cmdlen)
6486 ccline.cmdpos = ccline.cmdlen;
6487 if (cmdwin_result == K_IGNORE)
6488 {
6489 set_cmdspos_cursor();
6490 redrawcmd();
6491 }
6492 }
6493
6494# ifdef FEAT_AUTOCMD
6495 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006496 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497# endif
6498 wp = curwin;
6499 bp = curbuf;
6500 win_goto(old_curwin);
6501 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006502
6503 /* win_close() may have already wiped the buffer when 'bh' is
6504 * set to 'wipe' */
6505 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006506 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508 /* Restore window sizes. */
6509 win_size_restore(&winsizes);
6510
6511# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006512 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513# endif
6514 }
6515
6516 ga_clear(&winsizes);
6517 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006518# ifdef FEAT_RIGHTLEFT
6519 cmdmsg_rl = save_cmdmsg_rl;
6520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
6522 State = save_State;
6523# ifdef FEAT_MOUSE
6524 setmouse();
6525# endif
6526
6527 return cmdwin_result;
6528}
6529#endif /* FEAT_CMDWIN */
6530
6531/*
6532 * Used for commands that either take a simple command string argument, or:
6533 * cmd << endmarker
6534 * {script}
6535 * endmarker
6536 * Returns a pointer to allocated memory with {script} or NULL.
6537 */
6538 char_u *
6539script_get(eap, cmd)
6540 exarg_T *eap;
6541 char_u *cmd;
6542{
6543 char_u *theline;
6544 char *end_pattern = NULL;
6545 char dot[] = ".";
6546 garray_T ga;
6547
6548 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6549 return NULL;
6550
6551 ga_init2(&ga, 1, 0x400);
6552
6553 if (cmd[2] != NUL)
6554 end_pattern = (char *)skipwhite(cmd + 2);
6555 else
6556 end_pattern = dot;
6557
6558 for (;;)
6559 {
6560 theline = eap->getline(
6561#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006562 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563#endif
6564 NUL, eap->cookie, 0);
6565
6566 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006567 {
6568 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572 ga_concat(&ga, theline);
6573 ga_append(&ga, '\n');
6574 vim_free(theline);
6575 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006576 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578 return (char_u *)ga.ga_data;
6579}