blob: d8a4a67f4d7477fe5321923d462d05375146530d [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 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003656 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003658 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003659 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
3661 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3662 TOLOWER_LOC(xp->xp_files[0][len]))
3663 break;
3664 }
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003665 else if (xp->xp_files[i][len] != xp->xp_files[0][len])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 break;
3667 }
3668 if (i < xp->xp_numfiles)
3669 {
3670 if (!(options & WILD_NO_BEEP))
3671 vim_beep();
3672 break;
3673 }
3674 }
3675 ss = alloc((unsigned)len + 1);
3676 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003677 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 findex = -1; /* next p_wc gets first one */
3679 }
3680
3681 /* Concatenate all matching names */
3682 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3683 {
3684 len = 0;
3685 for (i = 0; i < xp->xp_numfiles; ++i)
3686 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3687 ss = lalloc(len, TRUE);
3688 if (ss != NULL)
3689 {
3690 *ss = NUL;
3691 for (i = 0; i < xp->xp_numfiles; ++i)
3692 {
3693 STRCAT(ss, xp->xp_files[i]);
3694 if (i != xp->xp_numfiles - 1)
3695 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3696 }
3697 }
3698 }
3699
3700 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3701 ExpandCleanup(xp);
3702
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003703 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003704 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003705 vim_free(orig);
3706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 return ss;
3708}
3709
3710/*
3711 * Prepare an expand structure for use.
3712 */
3713 void
3714ExpandInit(xp)
3715 expand_T *xp;
3716{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003717 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003718 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003720#ifndef BACKSLASH_IN_FILENAME
3721 xp->xp_shell = FALSE;
3722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 xp->xp_numfiles = -1;
3724 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003725#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3726 xp->xp_arg = NULL;
3727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728}
3729
3730/*
3731 * Cleanup an expand structure after use.
3732 */
3733 void
3734ExpandCleanup(xp)
3735 expand_T *xp;
3736{
3737 if (xp->xp_numfiles >= 0)
3738 {
3739 FreeWild(xp->xp_numfiles, xp->xp_files);
3740 xp->xp_numfiles = -1;
3741 }
3742}
3743
3744 void
3745ExpandEscape(xp, str, numfiles, files, options)
3746 expand_T *xp;
3747 char_u *str;
3748 int numfiles;
3749 char_u **files;
3750 int options;
3751{
3752 int i;
3753 char_u *p;
3754
3755 /*
3756 * May change home directory back to "~"
3757 */
3758 if (options & WILD_HOME_REPLACE)
3759 tilde_replace(str, numfiles, files);
3760
3761 if (options & WILD_ESCAPE)
3762 {
3763 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003764 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003765 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 || xp->xp_context == EXPAND_BUFFERS
3767 || xp->xp_context == EXPAND_DIRECTORIES)
3768 {
3769 /*
3770 * Insert a backslash into a file name before a space, \, %, #
3771 * and wildmatch characters, except '~'.
3772 */
3773 for (i = 0; i < numfiles; ++i)
3774 {
3775 /* for ":set path=" we need to escape spaces twice */
3776 if (xp->xp_backslash == XP_BS_THREE)
3777 {
3778 p = vim_strsave_escaped(files[i], (char_u *)" ");
3779 if (p != NULL)
3780 {
3781 vim_free(files[i]);
3782 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003783#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 p = vim_strsave_escaped(files[i], (char_u *)" ");
3785 if (p != NULL)
3786 {
3787 vim_free(files[i]);
3788 files[i] = p;
3789 }
3790#endif
3791 }
3792 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003793#ifdef BACKSLASH_IN_FILENAME
3794 p = vim_strsave_fnameescape(files[i], FALSE);
3795#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003796 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (p != NULL)
3799 {
3800 vim_free(files[i]);
3801 files[i] = p;
3802 }
3803
3804 /* If 'str' starts with "\~", replace "~" at start of
3805 * files[i] with "\~". */
3806 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003807 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003810
3811 /* If the first file starts with a '+' escape it. Otherwise it
3812 * could be seen as "+cmd". */
3813 if (*files[0] == '+')
3814 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 else if (xp->xp_context == EXPAND_TAGS)
3817 {
3818 /*
3819 * Insert a backslash before characters in a tag name that
3820 * would terminate the ":tag" command.
3821 */
3822 for (i = 0; i < numfiles; ++i)
3823 {
3824 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3825 if (p != NULL)
3826 {
3827 vim_free(files[i]);
3828 files[i] = p;
3829 }
3830 }
3831 }
3832 }
3833}
3834
3835/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003836 * Escape special characters in "fname" for when used as a file name argument
3837 * after a Vim command, or, when "shell" is non-zero, a shell command.
3838 * Returns the result in allocated memory.
3839 */
3840 char_u *
3841vim_strsave_fnameescape(fname, shell)
3842 char_u *fname;
3843 int shell;
3844{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003845 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003846#ifdef BACKSLASH_IN_FILENAME
3847 char_u buf[20];
3848 int j = 0;
3849
3850 /* Don't escape '[' and '{' if they are in 'isfname'. */
3851 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3852 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3853 buf[j++] = *p;
3854 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003855 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003856#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003857 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3858 if (shell && csh_like_shell() && p != NULL)
3859 {
3860 char_u *s;
3861
3862 /* For csh and similar shells need to put two backslashes before '!'.
3863 * One is taken by Vim, one by the shell. */
3864 s = vim_strsave_escaped(p, (char_u *)"!");
3865 vim_free(p);
3866 p = s;
3867 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003868#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003869
3870 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3871 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003872 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003873 escape_fname(&p);
3874
3875 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003876}
3877
3878/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003879 * Put a backslash before the file name in "pp", which is in allocated memory.
3880 */
3881 static void
3882escape_fname(pp)
3883 char_u **pp;
3884{
3885 char_u *p;
3886
3887 p = alloc((unsigned)(STRLEN(*pp) + 2));
3888 if (p != NULL)
3889 {
3890 p[0] = '\\';
3891 STRCPY(p + 1, *pp);
3892 vim_free(*pp);
3893 *pp = p;
3894 }
3895}
3896
3897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 * For each file name in files[num_files]:
3899 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3900 */
3901 void
3902tilde_replace(orig_pat, num_files, files)
3903 char_u *orig_pat;
3904 int num_files;
3905 char_u **files;
3906{
3907 int i;
3908 char_u *p;
3909
3910 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3911 {
3912 for (i = 0; i < num_files; ++i)
3913 {
3914 p = home_replace_save(NULL, files[i]);
3915 if (p != NULL)
3916 {
3917 vim_free(files[i]);
3918 files[i] = p;
3919 }
3920 }
3921 }
3922}
3923
3924/*
3925 * Show all matches for completion on the command line.
3926 * Returns EXPAND_NOTHING when the character that triggered expansion should
3927 * be inserted like a normal character.
3928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 static int
3930showmatches(xp, wildmenu)
3931 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003932 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
3934#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3935 int num_files;
3936 char_u **files_found;
3937 int i, j, k;
3938 int maxlen;
3939 int lines;
3940 int columns;
3941 char_u *p;
3942 int lastlen;
3943 int attr;
3944 int showtail;
3945
3946 if (xp->xp_numfiles == -1)
3947 {
3948 set_expand_context(xp);
3949 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3950 &num_files, &files_found);
3951 showtail = expand_showtail(xp);
3952 if (i != EXPAND_OK)
3953 return i;
3954
3955 }
3956 else
3957 {
3958 num_files = xp->xp_numfiles;
3959 files_found = xp->xp_files;
3960 showtail = cmd_showtail;
3961 }
3962
3963#ifdef FEAT_WILDMENU
3964 if (!wildmenu)
3965 {
3966#endif
3967 msg_didany = FALSE; /* lines_left will be set */
3968 msg_start(); /* prepare for paging */
3969 msg_putchar('\n');
3970 out_flush();
3971 cmdline_row = msg_row;
3972 msg_didany = FALSE; /* lines_left will be set again */
3973 msg_start(); /* prepare for paging */
3974#ifdef FEAT_WILDMENU
3975 }
3976#endif
3977
3978 if (got_int)
3979 got_int = FALSE; /* only int. the completion, not the cmd line */
3980#ifdef FEAT_WILDMENU
3981 else if (wildmenu)
3982 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3983#endif
3984 else
3985 {
3986 /* find the length of the longest file name */
3987 maxlen = 0;
3988 for (i = 0; i < num_files; ++i)
3989 {
3990 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003991 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 || xp->xp_context == EXPAND_BUFFERS))
3993 {
3994 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3995 j = vim_strsize(NameBuff);
3996 }
3997 else
3998 j = vim_strsize(L_SHOWFILE(i));
3999 if (j > maxlen)
4000 maxlen = j;
4001 }
4002
4003 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4004 lines = num_files;
4005 else
4006 {
4007 /* compute the number of columns and lines for the listing */
4008 maxlen += 2; /* two spaces between file names */
4009 columns = ((int)Columns + 2) / maxlen;
4010 if (columns < 1)
4011 columns = 1;
4012 lines = (num_files + columns - 1) / columns;
4013 }
4014
4015 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4016
4017 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4018 {
4019 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4020 msg_clr_eos();
4021 msg_advance(maxlen - 3);
4022 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4023 }
4024
4025 /* list the files line by line */
4026 for (i = 0; i < lines; ++i)
4027 {
4028 lastlen = 999;
4029 for (k = i; k < num_files; k += lines)
4030 {
4031 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4032 {
4033 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4034 p = files_found[k] + STRLEN(files_found[k]) + 1;
4035 msg_advance(maxlen + 1);
4036 msg_puts(p);
4037 msg_advance(maxlen + 3);
4038 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4039 break;
4040 }
4041 for (j = maxlen - lastlen; --j >= 0; )
4042 msg_putchar(' ');
4043 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004044 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 || xp->xp_context == EXPAND_BUFFERS)
4046 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004047 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004048 if (xp->xp_numfiles != -1)
4049 {
4050 char_u *halved_slash;
4051 char_u *exp_path;
4052
4053 /* Expansion was done before and special characters
4054 * were escaped, need to halve backslashes. Also
4055 * $HOME has been replaced with ~/. */
4056 exp_path = expand_env_save_opt(files_found[k], TRUE);
4057 halved_slash = backslash_halve_save(
4058 exp_path != NULL ? exp_path : files_found[k]);
4059 j = mch_isdir(halved_slash != NULL ? halved_slash
4060 : files_found[k]);
4061 vim_free(exp_path);
4062 vim_free(halved_slash);
4063 }
4064 else
4065 /* Expansion was done here, file names are literal. */
4066 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 if (showtail)
4068 p = L_SHOWFILE(k);
4069 else
4070 {
4071 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4072 TRUE);
4073 p = NameBuff;
4074 }
4075 }
4076 else
4077 {
4078 j = FALSE;
4079 p = L_SHOWFILE(k);
4080 }
4081 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4082 }
4083 if (msg_col > 0) /* when not wrapped around */
4084 {
4085 msg_clr_eos();
4086 msg_putchar('\n');
4087 }
4088 out_flush(); /* show one line at a time */
4089 if (got_int)
4090 {
4091 got_int = FALSE;
4092 break;
4093 }
4094 }
4095
4096 /*
4097 * we redraw the command below the lines that we have just listed
4098 * This is a bit tricky, but it saves a lot of screen updating.
4099 */
4100 cmdline_row = msg_row; /* will put it back later */
4101 }
4102
4103 if (xp->xp_numfiles == -1)
4104 FreeWild(num_files, files_found);
4105
4106 return EXPAND_OK;
4107}
4108
4109/*
4110 * Private gettail for showmatches() (and win_redr_status_matches()):
4111 * Find tail of file name path, but ignore trailing "/".
4112 */
4113 char_u *
4114sm_gettail(s)
4115 char_u *s;
4116{
4117 char_u *p;
4118 char_u *t = s;
4119 int had_sep = FALSE;
4120
4121 for (p = s; *p != NUL; )
4122 {
4123 if (vim_ispathsep(*p)
4124#ifdef BACKSLASH_IN_FILENAME
4125 && !rem_backslash(p)
4126#endif
4127 )
4128 had_sep = TRUE;
4129 else if (had_sep)
4130 {
4131 t = p;
4132 had_sep = FALSE;
4133 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004134 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 return t;
4137}
4138
4139/*
4140 * Return TRUE if we only need to show the tail of completion matches.
4141 * When not completing file names or there is a wildcard in the path FALSE is
4142 * returned.
4143 */
4144 static int
4145expand_showtail(xp)
4146 expand_T *xp;
4147{
4148 char_u *s;
4149 char_u *end;
4150
4151 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004152 if (xp->xp_context != EXPAND_FILES
4153 && xp->xp_context != EXPAND_SHELLCMD
4154 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 return FALSE;
4156
4157 end = gettail(xp->xp_pattern);
4158 if (end == xp->xp_pattern) /* there is no path separator */
4159 return FALSE;
4160
4161 for (s = xp->xp_pattern; s < end; s++)
4162 {
4163 /* Skip escaped wildcards. Only when the backslash is not a path
4164 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4165 if (rem_backslash(s))
4166 ++s;
4167 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4168 return FALSE;
4169 }
4170 return TRUE;
4171}
4172
4173/*
4174 * Prepare a string for expansion.
4175 * When expanding file names: The string will be used with expand_wildcards().
4176 * Copy the file name into allocated memory and add a '*' at the end.
4177 * When expanding other names: The string will be used with regcomp(). Copy
4178 * the name into allocated memory and prepend "^".
4179 */
4180 char_u *
4181addstar(fname, len, context)
4182 char_u *fname;
4183 int len;
4184 int context; /* EXPAND_FILES etc. */
4185{
4186 char_u *retval;
4187 int i, j;
4188 int new_len;
4189 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004190 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004192 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004193 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004194 && context != EXPAND_SHELLCMD
4195 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
4197 /*
4198 * Matching will be done internally (on something other than files).
4199 * So we convert the file-matching-type wildcards into our kind for
4200 * use with vim_regcomp(). First work out how long it will be:
4201 */
4202
4203 /* For help tags the translation is done in find_help_tags().
4204 * For a tag pattern starting with "/" no translation is needed. */
4205 if (context == EXPAND_HELP
4206 || context == EXPAND_COLORS
4207 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004208 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004209 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 || (context == EXPAND_TAGS && fname[0] == '/'))
4211 retval = vim_strnsave(fname, len);
4212 else
4213 {
4214 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4215 for (i = 0; i < len; i++)
4216 {
4217 if (fname[i] == '*' || fname[i] == '~')
4218 new_len++; /* '*' needs to be replaced by ".*"
4219 '~' needs to be replaced by "\~" */
4220
4221 /* Buffer names are like file names. "." should be literal */
4222 if (context == EXPAND_BUFFERS && fname[i] == '.')
4223 new_len++; /* "." becomes "\." */
4224
4225 /* Custom expansion takes care of special things, match
4226 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004227 if ((context == EXPAND_USER_DEFINED
4228 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 new_len++; /* '\' becomes "\\" */
4230 }
4231 retval = alloc(new_len);
4232 if (retval != NULL)
4233 {
4234 retval[0] = '^';
4235 j = 1;
4236 for (i = 0; i < len; i++, j++)
4237 {
4238 /* Skip backslash. But why? At least keep it for custom
4239 * expansion. */
4240 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004241 && context != EXPAND_USER_LIST
4242 && fname[i] == '\\'
4243 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 break;
4245
4246 switch (fname[i])
4247 {
4248 case '*': retval[j++] = '.';
4249 break;
4250 case '~': retval[j++] = '\\';
4251 break;
4252 case '?': retval[j] = '.';
4253 continue;
4254 case '.': if (context == EXPAND_BUFFERS)
4255 retval[j++] = '\\';
4256 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004257 case '\\': if (context == EXPAND_USER_DEFINED
4258 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 retval[j++] = '\\';
4260 break;
4261 }
4262 retval[j] = fname[i];
4263 }
4264 retval[j] = NUL;
4265 }
4266 }
4267 }
4268 else
4269 {
4270 retval = alloc(len + 4);
4271 if (retval != NULL)
4272 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004273 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
4275 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004276 * Don't add a star to *, ~, ~user, $var or `cmd`.
4277 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 * ~ would be at the start of the file name, but not the tail.
4279 * $ could be anywhere in the tail.
4280 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004281 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 */
4283 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004284 ends_in_star = (len > 0 && retval[len - 1] == '*');
4285#ifndef BACKSLASH_IN_FILENAME
4286 for (i = len - 2; i >= 0; --i)
4287 {
4288 if (retval[i] != '\\')
4289 break;
4290 ends_in_star = !ends_in_star;
4291 }
4292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004294 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 && vim_strchr(tail, '$') == NULL
4296 && vim_strchr(retval, '`') == NULL)
4297 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004298 else if (len > 0 && retval[len - 1] == '$')
4299 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 retval[len] = NUL;
4301 }
4302 }
4303 return retval;
4304}
4305
4306/*
4307 * Must parse the command line so far to work out what context we are in.
4308 * Completion can then be done based on that context.
4309 * This routine sets the variables:
4310 * xp->xp_pattern The start of the pattern to be expanded within
4311 * the command line (ends at the cursor).
4312 * xp->xp_context The type of thing to expand. Will be one of:
4313 *
4314 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4315 * the command line, like an unknown command. Caller
4316 * should beep.
4317 * EXPAND_NOTHING Unrecognised context for completion, use char like
4318 * a normal char, rather than for completion. eg
4319 * :s/^I/
4320 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4321 * it.
4322 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4323 * EXPAND_FILES After command with XFILE set, or after setting
4324 * with P_EXPAND set. eg :e ^I, :w>>^I
4325 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4326 * when we know only directories are of interest. eg
4327 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004328 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4330 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4331 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4332 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4333 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4334 * EXPAND_EVENTS Complete event names
4335 * EXPAND_SYNTAX Complete :syntax command arguments
4336 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4337 * EXPAND_AUGROUP Complete autocommand group names
4338 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4339 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4340 * eg :unmap a^I , :cunab x^I
4341 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4342 * eg :call sub^I
4343 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4344 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4345 * names in expressions, eg :while s^I
4346 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004347 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 */
4349 static void
4350set_expand_context(xp)
4351 expand_T *xp;
4352{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004353 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (ccline.cmdfirstc != ':'
4355#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004356 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004357 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358#endif
4359 )
4360 {
4361 xp->xp_context = EXPAND_NOTHING;
4362 return;
4363 }
4364 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4365}
4366
4367 void
4368set_cmd_context(xp, str, len, col)
4369 expand_T *xp;
4370 char_u *str; /* start of command line */
4371 int len; /* length of command line (excl. NUL) */
4372 int col; /* position of cursor */
4373{
4374 int old_char = NUL;
4375 char_u *nextcomm;
4376
4377 /*
4378 * Avoid a UMR warning from Purify, only save the character if it has been
4379 * written before.
4380 */
4381 if (col < len)
4382 old_char = str[col];
4383 str[col] = NUL;
4384 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004385
4386#ifdef FEAT_EVAL
4387 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004388 {
4389# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004390 /* pass CMD_SIZE because there is no real command */
4391 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004392# endif
4393 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004394 else if (ccline.input_fn)
4395 {
4396 xp->xp_context = ccline.xp_context;
4397 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004398# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004399 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004400# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004401 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004402 else
4403#endif
4404 while (nextcomm != NULL)
4405 nextcomm = set_one_cmd_context(xp, nextcomm);
4406
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 str[col] = old_char;
4408}
4409
4410/*
4411 * Expand the command line "str" from context "xp".
4412 * "xp" must have been set by set_cmd_context().
4413 * xp->xp_pattern points into "str", to where the text that is to be expanded
4414 * starts.
4415 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4416 * cursor.
4417 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4418 * key that triggered expansion literally.
4419 * Returns EXPAND_OK otherwise.
4420 */
4421 int
4422expand_cmdline(xp, str, col, matchcount, matches)
4423 expand_T *xp;
4424 char_u *str; /* start of command line */
4425 int col; /* position of cursor */
4426 int *matchcount; /* return: nr of matches */
4427 char_u ***matches; /* return: array of pointers to matches */
4428{
4429 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004430 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4433 {
4434 beep_flush();
4435 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4436 }
4437 if (xp->xp_context == EXPAND_NOTHING)
4438 {
4439 /* Caller can use the character as a normal char instead */
4440 return EXPAND_NOTHING;
4441 }
4442
4443 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004444 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4445 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 if (file_str == NULL)
4447 return EXPAND_UNSUCCESSFUL;
4448
Bram Moolenaar94950a92010-12-02 16:01:29 +01004449 if (p_wic)
4450 options += WILD_ICASE;
4451
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004453 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
4455 *matchcount = 0;
4456 *matches = NULL;
4457 }
4458 vim_free(file_str);
4459
4460 return EXPAND_OK;
4461}
4462
4463#ifdef FEAT_MULTI_LANG
4464/*
4465 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4466 */
4467static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4468
4469 static void
4470cleanup_help_tags(num_file, file)
4471 int num_file;
4472 char_u **file;
4473{
4474 int i, j;
4475 int len;
4476
4477 for (i = 0; i < num_file; ++i)
4478 {
4479 len = (int)STRLEN(file[i]) - 3;
4480 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4481 {
4482 /* Sorting on priority means the same item in another language may
4483 * be anywhere. Search all items for a match up to the "@en". */
4484 for (j = 0; j < num_file; ++j)
4485 if (j != i
4486 && (int)STRLEN(file[j]) == len + 3
4487 && STRNCMP(file[i], file[j], len + 1) == 0)
4488 break;
4489 if (j == num_file)
4490 file[i][len] = NUL;
4491 }
4492 }
4493}
4494#endif
4495
4496/*
4497 * Do the expansion based on xp->xp_context and "pat".
4498 */
4499 static int
4500ExpandFromContext(xp, pat, num_file, file, options)
4501 expand_T *xp;
4502 char_u *pat;
4503 int *num_file;
4504 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004505 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506{
4507#ifdef FEAT_CMDL_COMPL
4508 regmatch_T regmatch;
4509#endif
4510 int ret;
4511 int flags;
4512
4513 flags = EW_DIR; /* include directories */
4514 if (options & WILD_LIST_NOTFOUND)
4515 flags |= EW_NOTFOUND;
4516 if (options & WILD_ADD_SLASH)
4517 flags |= EW_ADDSLASH;
4518 if (options & WILD_KEEP_ALL)
4519 flags |= EW_KEEPALL;
4520 if (options & WILD_SILENT)
4521 flags |= EW_SILENT;
4522
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004523 if (xp->xp_context == EXPAND_FILES
4524 || xp->xp_context == EXPAND_DIRECTORIES
4525 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
4527 /*
4528 * Expand file or directory names.
4529 */
4530 int free_pat = FALSE;
4531 int i;
4532
4533 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4534 * space */
4535 if (xp->xp_backslash != XP_BS_NONE)
4536 {
4537 free_pat = TRUE;
4538 pat = vim_strsave(pat);
4539 for (i = 0; pat[i]; ++i)
4540 if (pat[i] == '\\')
4541 {
4542 if (xp->xp_backslash == XP_BS_THREE
4543 && pat[i + 1] == '\\'
4544 && pat[i + 2] == '\\'
4545 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004546 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (xp->xp_backslash == XP_BS_ONE
4548 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004549 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 }
4551 }
4552
4553 if (xp->xp_context == EXPAND_FILES)
4554 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004555 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4556 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 else
4558 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004559 if (options & WILD_ICASE)
4560 flags |= EW_ICASE;
4561
Bram Moolenaard7834d32009-12-02 16:14:36 +00004562 /* Expand wildcards, supporting %:h and the like. */
4563 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 if (free_pat)
4565 vim_free(pat);
4566 return ret;
4567 }
4568
4569 *file = (char_u **)"";
4570 *num_file = 0;
4571 if (xp->xp_context == EXPAND_HELP)
4572 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004573 /* With an empty argument we would get all the help tags, which is
4574 * very slow. Get matches for "help" instead. */
4575 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4576 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 {
4578#ifdef FEAT_MULTI_LANG
4579 cleanup_help_tags(*num_file, *file);
4580#endif
4581 return OK;
4582 }
4583 return FAIL;
4584 }
4585
4586#ifndef FEAT_CMDL_COMPL
4587 return FAIL;
4588#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004589 if (xp->xp_context == EXPAND_SHELLCMD)
4590 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 if (xp->xp_context == EXPAND_OLD_SETTING)
4592 return ExpandOldSetting(num_file, file);
4593 if (xp->xp_context == EXPAND_BUFFERS)
4594 return ExpandBufnames(pat, num_file, file, options);
4595 if (xp->xp_context == EXPAND_TAGS
4596 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4597 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4598 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004599 {
4600 char *directories[] = {"colors", NULL};
4601 return ExpandRTDir(pat, num_file, file, directories);
4602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004604 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004605 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004606 return ExpandRTDir(pat, num_file, file, directories);
4607 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004608 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004609 {
4610 char *directories[] = {"syntax", NULL};
4611 return ExpandRTDir(pat, num_file, file, directories);
4612 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004613 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004614 {
4615 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4616 return ExpandRTDir(pat, num_file, file, directories);
4617 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004618# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4619 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004620 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4624 if (regmatch.regprog == NULL)
4625 return FAIL;
4626
4627 /* set ignore-case according to p_ic, p_scs and pat */
4628 regmatch.rm_ic = ignorecase(pat);
4629
4630 if (xp->xp_context == EXPAND_SETTINGS
4631 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4632 ret = ExpandSettings(xp, &regmatch, num_file, file);
4633 else if (xp->xp_context == EXPAND_MAPPINGS)
4634 ret = ExpandMappings(&regmatch, num_file, file);
4635# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4636 else if (xp->xp_context == EXPAND_USER_DEFINED)
4637 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4638# endif
4639 else
4640 {
4641 static struct expgen
4642 {
4643 int context;
4644 char_u *((*func)__ARGS((expand_T *, int)));
4645 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004646 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 } tab[] =
4648 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004649 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4650 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004651#ifdef FEAT_CMDHIST
4652 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004655 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4656 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4657 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4658 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659#endif
4660#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004661 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4662 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4663 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4664 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#endif
4666#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004667 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4668 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669#endif
4670#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004671 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004673 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004675 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4676 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004678#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004679 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004680#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004681#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004682 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004683#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004684#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004685 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4688 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004689 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4690 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004692 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004693 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 };
4695 int i;
4696
4697 /*
4698 * Find a context in the table and call the ExpandGeneric() with the
4699 * right function to do the expansion.
4700 */
4701 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004702 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 if (xp->xp_context == tab[i].context)
4704 {
4705 if (tab[i].ic)
4706 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004707 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004708 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 break;
4710 }
4711 }
4712
4713 vim_free(regmatch.regprog);
4714
4715 return ret;
4716#endif /* FEAT_CMDL_COMPL */
4717}
4718
4719#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4720/*
4721 * Expand a list of names.
4722 *
4723 * Generic function for command line completion. It calls a function to
4724 * obtain strings, one by one. The strings are matched against a regexp
4725 * program. Matching strings are copied into an array, which is returned.
4726 *
4727 * Returns OK when no problems encountered, FAIL for error (out of memory).
4728 */
4729 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004730ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 expand_T *xp;
4732 regmatch_T *regmatch;
4733 int *num_file;
4734 char_u ***file;
4735 char_u *((*func)__ARGS((expand_T *, int)));
4736 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004737 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
4739 int i;
4740 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004741 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 char_u *str;
4743
4744 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004745 * round == 0: count the number of matching names
4746 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004748 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
4750 for (i = 0; ; ++i)
4751 {
4752 str = (*func)(xp, i);
4753 if (str == NULL) /* end of list */
4754 break;
4755 if (*str == NUL) /* skip empty strings */
4756 continue;
4757
4758 if (vim_regexec(regmatch, str, (colnr_T)0))
4759 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004760 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004762 if (escaped)
4763 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4764 else
4765 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 (*file)[count] = str;
4767#ifdef FEAT_MENU
4768 if (func == get_menu_names && str != NULL)
4769 {
4770 /* test for separator added by get_menu_names() */
4771 str += STRLEN(str) - 1;
4772 if (*str == '\001')
4773 *str = '.';
4774 }
4775#endif
4776 }
4777 ++count;
4778 }
4779 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004780 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 {
4782 if (count == 0)
4783 return OK;
4784 *num_file = count;
4785 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4786 if (*file == NULL)
4787 {
4788 *file = (char_u **)"";
4789 return FAIL;
4790 }
4791 count = 0;
4792 }
4793 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004794
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004795 /* Sort the results. Keep menu's in the specified order. */
4796 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004797 {
4798 if (xp->xp_context == EXPAND_EXPRESSION
4799 || xp->xp_context == EXPAND_FUNCTIONS
4800 || xp->xp_context == EXPAND_USER_FUNC)
4801 /* <SNR> functions should be sorted to the end. */
4802 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4803 sort_func_compare);
4804 else
4805 sort_strings(*file, *num_file);
4806 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004807
Bram Moolenaar4f688582007-07-24 12:34:30 +00004808#ifdef FEAT_CMDL_COMPL
4809 /* Reset the variables used for special highlight names expansion, so that
4810 * they don't show up when getting normal highlight names by ID. */
4811 reset_expand_highlight();
4812#endif
4813
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 return OK;
4815}
4816
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004817/*
4818 * Complete a shell command.
4819 * Returns FAIL or OK;
4820 */
4821 static int
4822expand_shellcmd(filepat, num_file, file, flagsarg)
4823 char_u *filepat; /* pattern to match with command names */
4824 int *num_file; /* return: number of matches */
4825 char_u ***file; /* return: array with matches */
4826 int flagsarg; /* EW_ flags */
4827{
4828 char_u *pat;
4829 int i;
4830 char_u *path;
4831 int mustfree = FALSE;
4832 garray_T ga;
4833 char_u *buf = alloc(MAXPATHL);
4834 size_t l;
4835 char_u *s, *e;
4836 int flags = flagsarg;
4837 int ret;
4838
4839 if (buf == NULL)
4840 return FAIL;
4841
4842 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4843 * space */
4844 pat = vim_strsave(filepat);
4845 for (i = 0; pat[i]; ++i)
4846 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004847 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004848
4849 flags |= EW_FILE | EW_EXEC;
4850
4851 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004852 if (mch_isFullName(pat))
4853 path = (char_u *)" ";
4854 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004855 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4856 path = (char_u *)".";
4857 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004858 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004859 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004860 if (path == NULL)
4861 path = (char_u *)"";
4862 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004863
4864 /*
4865 * Go over all directories in $PATH. Expand matches in that directory and
4866 * collect them in "ga".
4867 */
4868 ga_init2(&ga, (int)sizeof(char *), 10);
4869 for (s = path; *s != NUL; s = e)
4870 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004871 if (*s == ' ')
4872 ++s; /* Skip space used for absolute path name. */
4873
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004874#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4875 e = vim_strchr(s, ';');
4876#else
4877 e = vim_strchr(s, ':');
4878#endif
4879 if (e == NULL)
4880 e = s + STRLEN(s);
4881
4882 l = e - s;
4883 if (l > MAXPATHL - 5)
4884 break;
4885 vim_strncpy(buf, s, l);
4886 add_pathsep(buf);
4887 l = STRLEN(buf);
4888 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4889
4890 /* Expand matches in one directory of $PATH. */
4891 ret = expand_wildcards(1, &buf, num_file, file, flags);
4892 if (ret == OK)
4893 {
4894 if (ga_grow(&ga, *num_file) == FAIL)
4895 FreeWild(*num_file, *file);
4896 else
4897 {
4898 for (i = 0; i < *num_file; ++i)
4899 {
4900 s = (*file)[i];
4901 if (STRLEN(s) > l)
4902 {
4903 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004904 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004905 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4906 }
4907 else
4908 vim_free(s);
4909 }
4910 vim_free(*file);
4911 }
4912 }
4913 if (*e != NUL)
4914 ++e;
4915 }
4916 *file = ga.ga_data;
4917 *num_file = ga.ga_len;
4918
4919 vim_free(buf);
4920 vim_free(pat);
4921 if (mustfree)
4922 vim_free(path);
4923 return OK;
4924}
4925
4926
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004928static 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));
4929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004931 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004932 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004934 static void *
4935call_user_expand_func(user_expand_func, xp, num_file, file)
4936 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 int *num_file;
4939 char_u ***file;
4940{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004941 char_u keep;
4942 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004945 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004946 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
4948 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004949 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 *num_file = 0;
4951 *file = NULL;
4952
Bram Moolenaar21669c02008-01-18 12:16:16 +00004953 if (ccline.cmdbuff == NULL)
4954 {
4955 /* Completion from Insert mode, pass fake arguments. */
4956 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004957 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004958 args[1] = xp->xp_pattern;
4959 }
4960 else
4961 {
4962 /* Completion on the command line, pass real arguments. */
4963 keep = ccline.cmdbuff[ccline.cmdlen];
4964 ccline.cmdbuff[ccline.cmdlen] = 0;
4965 sprintf((char *)num, "%d", ccline.cmdpos);
4966 args[1] = ccline.cmdbuff;
4967 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004968 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 args[2] = num;
4970
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004971 /* Save the cmdline, we don't know what the function may do. */
4972 save_ccline = ccline;
4973 ccline.cmdbuff = NULL;
4974 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004976
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004977 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004978
4979 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004981 if (ccline.cmdbuff != NULL)
4982 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004983
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004984 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004985 return ret;
4986}
4987
4988/*
4989 * Expand names with a function defined by the user.
4990 */
4991 static int
4992ExpandUserDefined(xp, regmatch, num_file, file)
4993 expand_T *xp;
4994 regmatch_T *regmatch;
4995 int *num_file;
4996 char_u ***file;
4997{
4998 char_u *retstr;
4999 char_u *s;
5000 char_u *e;
5001 char_u keep;
5002 garray_T ga;
5003
5004 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5005 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 return FAIL;
5007
5008 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005009 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
5011 e = vim_strchr(s, '\n');
5012 if (e == NULL)
5013 e = s + STRLEN(s);
5014 keep = *e;
5015 *e = 0;
5016
5017 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5018 {
5019 *e = keep;
5020 if (*e != NUL)
5021 ++e;
5022 continue;
5023 }
5024
5025 if (ga_grow(&ga, 1) == FAIL)
5026 break;
5027
5028 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5029 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030
5031 *e = keep;
5032 if (*e != NUL)
5033 ++e;
5034 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005035 vim_free(retstr);
5036 *file = ga.ga_data;
5037 *num_file = ga.ga_len;
5038 return OK;
5039}
5040
5041/*
5042 * Expand names with a list returned by a function defined by the user.
5043 */
5044 static int
5045ExpandUserList(xp, num_file, file)
5046 expand_T *xp;
5047 int *num_file;
5048 char_u ***file;
5049{
5050 list_T *retlist;
5051 listitem_T *li;
5052 garray_T ga;
5053
5054 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5055 if (retlist == NULL)
5056 return FAIL;
5057
5058 ga_init2(&ga, (int)sizeof(char *), 3);
5059 /* Loop over the items in the list. */
5060 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5061 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005062 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5063 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005064
5065 if (ga_grow(&ga, 1) == FAIL)
5066 break;
5067
5068 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005069 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005070 ++ga.ga_len;
5071 }
5072 list_unref(retlist);
5073
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 *file = ga.ga_data;
5075 *num_file = ga.ga_len;
5076 return OK;
5077}
5078#endif
5079
5080/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005081 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005082 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005083 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 */
5085 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005086ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 char_u *pat;
5088 int *num_file;
5089 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005090 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005092 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 char_u *s;
5094 char_u *e;
5095 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005096 int i;
5097 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
5099 *num_file = 0;
5100 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005101 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005102 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005104 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005106 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5107 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005109 ga_clear_strings(&ga);
5110 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005112 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5113 matches = globpath(p_rtp, s, 0);
5114 vim_free(s);
5115 if (matches == NULL)
5116 continue;
5117
5118 for (s = matches; *s != NUL; s = e)
5119 {
5120 e = vim_strchr(s, '\n');
5121 if (e == NULL)
5122 e = s + STRLEN(s);
5123 if (ga_grow(&ga, 1) == FAIL)
5124 break;
5125 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5126 {
5127 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5128 if (*s == '\n' || vim_ispathsep(*s))
5129 break;
5130 ++s;
5131 ((char_u **)ga.ga_data)[ga.ga_len] =
5132 vim_strnsave(s, (int)(e - s - 4));
5133 ++ga.ga_len;
5134 }
5135 if (*e != NUL)
5136 ++e;
5137 }
5138 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005140 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005141 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005142
5143 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005144 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005145 remove_duplicates(&ga);
5146
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 *file = ga.ga_data;
5148 *num_file = ga.ga_len;
5149 return OK;
5150}
5151
5152#endif
5153
5154#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5155/*
5156 * Expand "file" for all comma-separated directories in "path".
5157 * Returns an allocated string with all matches concatenated, separated by
5158 * newlines. Returns NULL for an error or no matches.
5159 */
5160 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005161globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 char_u *path;
5163 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005164 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165{
5166 expand_T xpc;
5167 char_u *buf;
5168 garray_T ga;
5169 int i;
5170 int len;
5171 int num_p;
5172 char_u **p;
5173 char_u *cur = NULL;
5174
5175 buf = alloc(MAXPATHL);
5176 if (buf == NULL)
5177 return NULL;
5178
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005179 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 ga_init2(&ga, 1, 100);
5183
5184 /* Loop over all entries in {path}. */
5185 while (*path != NUL)
5186 {
5187 /* Copy one item of the path to buf[] and concatenate the file name. */
5188 copy_option_part(&path, buf, MAXPATHL, ",");
5189 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5190 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005191# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005192 /* Using the platform's path separator (\) makes vim incorrectly
5193 * treat it as an escape character, use '/' instead. */
5194 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5195 STRCAT(buf, "/");
5196# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005200 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5201 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005203 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005205 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
5207 /* Concatenate new results to previous ones. */
5208 if (ga_grow(&ga, len) == OK)
5209 {
5210 cur = (char_u *)ga.ga_data + ga.ga_len;
5211 for (i = 0; i < num_p; ++i)
5212 {
5213 STRCPY(cur, p[i]);
5214 cur += STRLEN(p[i]);
5215 *cur++ = '\n';
5216 }
5217 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219 FreeWild(num_p, p);
5220 }
5221 }
5222 }
5223 if (cur != NULL)
5224 *--cur = 0; /* Replace trailing newline with NUL */
5225
5226 vim_free(buf);
5227 return (char_u *)ga.ga_data;
5228}
5229
5230#endif
5231
5232#if defined(FEAT_CMDHIST) || defined(PROTO)
5233
5234/*********************************
5235 * Command line history stuff *
5236 *********************************/
5237
5238/*
5239 * Translate a history character to the associated type number.
5240 */
5241 static int
5242hist_char2type(c)
5243 int c;
5244{
5245 if (c == ':')
5246 return HIST_CMD;
5247 if (c == '=')
5248 return HIST_EXPR;
5249 if (c == '@')
5250 return HIST_INPUT;
5251 if (c == '>')
5252 return HIST_DEBUG;
5253 return HIST_SEARCH; /* must be '?' or '/' */
5254}
5255
5256/*
5257 * Table of history names.
5258 * These names are used in :history and various hist...() functions.
5259 * It is sufficient to give the significant prefix of a history name.
5260 */
5261
5262static char *(history_names[]) =
5263{
5264 "cmd",
5265 "search",
5266 "expr",
5267 "input",
5268 "debug",
5269 NULL
5270};
5271
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005272#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5273/*
5274 * Function given to ExpandGeneric() to obtain the possible first
5275 * arguments of the ":history command.
5276 */
5277 static char_u *
5278get_history_arg(xp, idx)
5279 expand_T *xp UNUSED;
5280 int idx;
5281{
5282 static char_u compl[2] = { NUL, NUL };
5283 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005284 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005285 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5286
5287 if (idx < short_names_count)
5288 {
5289 compl[0] = (char_u)short_names[idx];
5290 return compl;
5291 }
5292 if (idx < short_names_count + history_name_count)
5293 return (char_u *)history_names[idx - short_names_count];
5294 if (idx == short_names_count + history_name_count)
5295 return (char_u *)"all";
5296 return NULL;
5297}
5298#endif
5299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300/*
5301 * init_history() - Initialize the command line history.
5302 * Also used to re-allocate the history when the size changes.
5303 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005304 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305init_history()
5306{
5307 int newlen; /* new length of history table */
5308 histentry_T *temp;
5309 int i;
5310 int j;
5311 int type;
5312
5313 /*
5314 * If size of history table changed, reallocate it
5315 */
5316 newlen = (int)p_hi;
5317 if (newlen != hislen) /* history length changed */
5318 {
5319 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5320 {
5321 if (newlen)
5322 {
5323 temp = (histentry_T *)lalloc(
5324 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5325 if (temp == NULL) /* out of memory! */
5326 {
5327 if (type == 0) /* first one: just keep the old length */
5328 {
5329 newlen = hislen;
5330 break;
5331 }
5332 /* Already changed one table, now we can only have zero
5333 * length for all tables. */
5334 newlen = 0;
5335 type = -1;
5336 continue;
5337 }
5338 }
5339 else
5340 temp = NULL;
5341 if (newlen == 0 || temp != NULL)
5342 {
5343 if (hisidx[type] < 0) /* there are no entries yet */
5344 {
5345 for (i = 0; i < newlen; ++i)
5346 {
5347 temp[i].hisnum = 0;
5348 temp[i].hisstr = NULL;
5349 }
5350 }
5351 else if (newlen > hislen) /* array becomes bigger */
5352 {
5353 for (i = 0; i <= hisidx[type]; ++i)
5354 temp[i] = history[type][i];
5355 j = i;
5356 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5357 {
5358 temp[i].hisnum = 0;
5359 temp[i].hisstr = NULL;
5360 }
5361 for ( ; j < hislen; ++i, ++j)
5362 temp[i] = history[type][j];
5363 }
5364 else /* array becomes smaller or 0 */
5365 {
5366 j = hisidx[type];
5367 for (i = newlen - 1; ; --i)
5368 {
5369 if (i >= 0) /* copy newest entries */
5370 temp[i] = history[type][j];
5371 else /* remove older entries */
5372 vim_free(history[type][j].hisstr);
5373 if (--j < 0)
5374 j = hislen - 1;
5375 if (j == hisidx[type])
5376 break;
5377 }
5378 hisidx[type] = newlen - 1;
5379 }
5380 vim_free(history[type]);
5381 history[type] = temp;
5382 }
5383 }
5384 hislen = newlen;
5385 }
5386}
5387
5388/*
5389 * Check if command line 'str' is already in history.
5390 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5391 */
5392 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005393in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 int type;
5395 char_u *str;
5396 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005397 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 int i;
5400 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005401 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
5403 if (hisidx[type] < 0)
5404 return FALSE;
5405 i = hisidx[type];
5406 do
5407 {
5408 if (history[type][i].hisstr == NULL)
5409 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005410
5411 /* For search history, check that the separator character matches as
5412 * well. */
5413 p = history[type][i].hisstr;
5414 if (STRCMP(str, p) == 0
5415 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 if (!move_to_front)
5418 return TRUE;
5419 last_i = i;
5420 break;
5421 }
5422 if (--i < 0)
5423 i = hislen - 1;
5424 } while (i != hisidx[type]);
5425
5426 if (last_i >= 0)
5427 {
5428 str = history[type][i].hisstr;
5429 while (i != hisidx[type])
5430 {
5431 if (++i >= hislen)
5432 i = 0;
5433 history[type][last_i] = history[type][i];
5434 last_i = i;
5435 }
5436 history[type][i].hisstr = str;
5437 history[type][i].hisnum = ++hisnum[type];
5438 return TRUE;
5439 }
5440 return FALSE;
5441}
5442
5443/*
5444 * Convert history name (from table above) to its HIST_ equivalent.
5445 * When "name" is empty, return "cmd" history.
5446 * Returns -1 for unknown history name.
5447 */
5448 int
5449get_histtype(name)
5450 char_u *name;
5451{
5452 int i;
5453 int len = (int)STRLEN(name);
5454
5455 /* No argument: use current history. */
5456 if (len == 0)
5457 return hist_char2type(ccline.cmdfirstc);
5458
5459 for (i = 0; history_names[i] != NULL; ++i)
5460 if (STRNICMP(name, history_names[i], len) == 0)
5461 return i;
5462
5463 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5464 return hist_char2type(name[0]);
5465
5466 return -1;
5467}
5468
5469static int last_maptick = -1; /* last seen maptick */
5470
5471/*
5472 * Add the given string to the given history. If the string is already in the
5473 * history then it is moved to the front. "histype" may be one of he HIST_
5474 * values.
5475 */
5476 void
5477add_to_history(histype, new_entry, in_map, sep)
5478 int histype;
5479 char_u *new_entry;
5480 int in_map; /* consider maptick when inside a mapping */
5481 int sep; /* separator character used (search hist) */
5482{
5483 histentry_T *hisptr;
5484 int len;
5485
5486 if (hislen == 0) /* no history */
5487 return;
5488
5489 /*
5490 * Searches inside the same mapping overwrite each other, so that only
5491 * the last line is kept. Be careful not to remove a line that was moved
5492 * down, only lines that were added.
5493 */
5494 if (histype == HIST_SEARCH && in_map)
5495 {
5496 if (maptick == last_maptick)
5497 {
5498 /* Current line is from the same mapping, remove it */
5499 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5500 vim_free(hisptr->hisstr);
5501 hisptr->hisstr = NULL;
5502 hisptr->hisnum = 0;
5503 --hisnum[histype];
5504 if (--hisidx[HIST_SEARCH] < 0)
5505 hisidx[HIST_SEARCH] = hislen - 1;
5506 }
5507 last_maptick = -1;
5508 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005509 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 {
5511 if (++hisidx[histype] == hislen)
5512 hisidx[histype] = 0;
5513 hisptr = &history[histype][hisidx[histype]];
5514 vim_free(hisptr->hisstr);
5515
5516 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005517 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5519 if (hisptr->hisstr != NULL)
5520 hisptr->hisstr[len + 1] = sep;
5521
5522 hisptr->hisnum = ++hisnum[histype];
5523 if (histype == HIST_SEARCH && in_map)
5524 last_maptick = maptick;
5525 }
5526}
5527
5528#if defined(FEAT_EVAL) || defined(PROTO)
5529
5530/*
5531 * Get identifier of newest history entry.
5532 * "histype" may be one of the HIST_ values.
5533 */
5534 int
5535get_history_idx(histype)
5536 int histype;
5537{
5538 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5539 || hisidx[histype] < 0)
5540 return -1;
5541
5542 return history[histype][hisidx[histype]].hisnum;
5543}
5544
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005545static struct cmdline_info *get_ccline_ptr __ARGS((void));
5546
5547/*
5548 * Get pointer to the command line info to use. cmdline_paste() may clear
5549 * ccline and put the previous value in prev_ccline.
5550 */
5551 static struct cmdline_info *
5552get_ccline_ptr()
5553{
5554 if ((State & CMDLINE) == 0)
5555 return NULL;
5556 if (ccline.cmdbuff != NULL)
5557 return &ccline;
5558 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5559 return &prev_ccline;
5560 return NULL;
5561}
5562
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563/*
5564 * Get the current command line in allocated memory.
5565 * Only works when the command line is being edited.
5566 * Returns NULL when something is wrong.
5567 */
5568 char_u *
5569get_cmdline_str()
5570{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005571 struct cmdline_info *p = get_ccline_ptr();
5572
5573 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005575 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576}
5577
5578/*
5579 * Get the current command line position, counted in bytes.
5580 * Zero is the first position.
5581 * Only works when the command line is being edited.
5582 * Returns -1 when something is wrong.
5583 */
5584 int
5585get_cmdline_pos()
5586{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005587 struct cmdline_info *p = get_ccline_ptr();
5588
5589 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005591 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592}
5593
5594/*
5595 * Set the command line byte position to "pos". Zero is the first position.
5596 * Only works when the command line is being edited.
5597 * Returns 1 when failed, 0 when OK.
5598 */
5599 int
5600set_cmdline_pos(pos)
5601 int pos;
5602{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005603 struct cmdline_info *p = get_ccline_ptr();
5604
5605 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 return 1;
5607
5608 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5609 * changed the command line. */
5610 if (pos < 0)
5611 new_cmdpos = 0;
5612 else
5613 new_cmdpos = pos;
5614 return 0;
5615}
5616
5617/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005618 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005619 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005620 * Only works when the command line is being edited.
5621 * Returns NUL when something is wrong.
5622 */
5623 int
5624get_cmdline_type()
5625{
5626 struct cmdline_info *p = get_ccline_ptr();
5627
5628 if (p == NULL)
5629 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005630 if (p->cmdfirstc == NUL)
5631 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005632 return p->cmdfirstc;
5633}
5634
5635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 * Calculate history index from a number:
5637 * num > 0: seen as identifying number of a history entry
5638 * num < 0: relative position in history wrt newest entry
5639 * "histype" may be one of the HIST_ values.
5640 */
5641 static int
5642calc_hist_idx(histype, num)
5643 int histype;
5644 int num;
5645{
5646 int i;
5647 histentry_T *hist;
5648 int wrapped = FALSE;
5649
5650 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5651 || (i = hisidx[histype]) < 0 || num == 0)
5652 return -1;
5653
5654 hist = history[histype];
5655 if (num > 0)
5656 {
5657 while (hist[i].hisnum > num)
5658 if (--i < 0)
5659 {
5660 if (wrapped)
5661 break;
5662 i += hislen;
5663 wrapped = TRUE;
5664 }
5665 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5666 return i;
5667 }
5668 else if (-num <= hislen)
5669 {
5670 i += num + 1;
5671 if (i < 0)
5672 i += hislen;
5673 if (hist[i].hisstr != NULL)
5674 return i;
5675 }
5676 return -1;
5677}
5678
5679/*
5680 * Get a history entry by its index.
5681 * "histype" may be one of the HIST_ values.
5682 */
5683 char_u *
5684get_history_entry(histype, idx)
5685 int histype;
5686 int idx;
5687{
5688 idx = calc_hist_idx(histype, idx);
5689 if (idx >= 0)
5690 return history[histype][idx].hisstr;
5691 else
5692 return (char_u *)"";
5693}
5694
5695/*
5696 * Clear all entries of a history.
5697 * "histype" may be one of the HIST_ values.
5698 */
5699 int
5700clr_history(histype)
5701 int histype;
5702{
5703 int i;
5704 histentry_T *hisptr;
5705
5706 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5707 {
5708 hisptr = history[histype];
5709 for (i = hislen; i--;)
5710 {
5711 vim_free(hisptr->hisstr);
5712 hisptr->hisnum = 0;
5713 hisptr++->hisstr = NULL;
5714 }
5715 hisidx[histype] = -1; /* mark history as cleared */
5716 hisnum[histype] = 0; /* reset identifier counter */
5717 return OK;
5718 }
5719 return FAIL;
5720}
5721
5722/*
5723 * Remove all entries matching {str} from a history.
5724 * "histype" may be one of the HIST_ values.
5725 */
5726 int
5727del_history_entry(histype, str)
5728 int histype;
5729 char_u *str;
5730{
5731 regmatch_T regmatch;
5732 histentry_T *hisptr;
5733 int idx;
5734 int i;
5735 int last;
5736 int found = FALSE;
5737
5738 regmatch.regprog = NULL;
5739 regmatch.rm_ic = FALSE; /* always match case */
5740 if (hislen != 0
5741 && histype >= 0
5742 && histype < HIST_COUNT
5743 && *str != NUL
5744 && (idx = hisidx[histype]) >= 0
5745 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5746 != NULL)
5747 {
5748 i = last = idx;
5749 do
5750 {
5751 hisptr = &history[histype][i];
5752 if (hisptr->hisstr == NULL)
5753 break;
5754 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5755 {
5756 found = TRUE;
5757 vim_free(hisptr->hisstr);
5758 hisptr->hisstr = NULL;
5759 hisptr->hisnum = 0;
5760 }
5761 else
5762 {
5763 if (i != last)
5764 {
5765 history[histype][last] = *hisptr;
5766 hisptr->hisstr = NULL;
5767 hisptr->hisnum = 0;
5768 }
5769 if (--last < 0)
5770 last += hislen;
5771 }
5772 if (--i < 0)
5773 i += hislen;
5774 } while (i != idx);
5775 if (history[histype][idx].hisstr == NULL)
5776 hisidx[histype] = -1;
5777 }
5778 vim_free(regmatch.regprog);
5779 return found;
5780}
5781
5782/*
5783 * Remove an indexed entry from a history.
5784 * "histype" may be one of the HIST_ values.
5785 */
5786 int
5787del_history_idx(histype, idx)
5788 int histype;
5789 int idx;
5790{
5791 int i, j;
5792
5793 i = calc_hist_idx(histype, idx);
5794 if (i < 0)
5795 return FALSE;
5796 idx = hisidx[histype];
5797 vim_free(history[histype][i].hisstr);
5798
5799 /* When deleting the last added search string in a mapping, reset
5800 * last_maptick, so that the last added search string isn't deleted again.
5801 */
5802 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5803 last_maptick = -1;
5804
5805 while (i != idx)
5806 {
5807 j = (i + 1) % hislen;
5808 history[histype][i] = history[histype][j];
5809 i = j;
5810 }
5811 history[histype][i].hisstr = NULL;
5812 history[histype][i].hisnum = 0;
5813 if (--i < 0)
5814 i += hislen;
5815 hisidx[histype] = i;
5816 return TRUE;
5817}
5818
5819#endif /* FEAT_EVAL */
5820
5821#if defined(FEAT_CRYPT) || defined(PROTO)
5822/*
5823 * Very specific function to remove the value in ":set key=val" from the
5824 * history.
5825 */
5826 void
5827remove_key_from_history()
5828{
5829 char_u *p;
5830 int i;
5831
5832 i = hisidx[HIST_CMD];
5833 if (i < 0)
5834 return;
5835 p = history[HIST_CMD][i].hisstr;
5836 if (p != NULL)
5837 for ( ; *p; ++p)
5838 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5839 {
5840 p = vim_strchr(p + 3, '=');
5841 if (p == NULL)
5842 break;
5843 ++p;
5844 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5845 if (p[i] == '\\' && p[i + 1])
5846 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005847 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 --p;
5849 }
5850}
5851#endif
5852
5853#endif /* FEAT_CMDHIST */
5854
5855#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5856/*
5857 * Get indices "num1,num2" that specify a range within a list (not a range of
5858 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5859 * Returns OK if parsed successfully, otherwise FAIL.
5860 */
5861 int
5862get_list_range(str, num1, num2)
5863 char_u **str;
5864 int *num1;
5865 int *num2;
5866{
5867 int len;
5868 int first = FALSE;
5869 long num;
5870
5871 *str = skipwhite(*str);
5872 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5873 {
5874 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5875 *str += len;
5876 *num1 = (int)num;
5877 first = TRUE;
5878 }
5879 *str = skipwhite(*str);
5880 if (**str == ',') /* parse "to" part of range */
5881 {
5882 *str = skipwhite(*str + 1);
5883 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5884 if (len > 0)
5885 {
5886 *num2 = (int)num;
5887 *str = skipwhite(*str + len);
5888 }
5889 else if (!first) /* no number given at all */
5890 return FAIL;
5891 }
5892 else if (first) /* only one number given */
5893 *num2 = *num1;
5894 return OK;
5895}
5896#endif
5897
5898#if defined(FEAT_CMDHIST) || defined(PROTO)
5899/*
5900 * :history command - print a history
5901 */
5902 void
5903ex_history(eap)
5904 exarg_T *eap;
5905{
5906 histentry_T *hist;
5907 int histype1 = HIST_CMD;
5908 int histype2 = HIST_CMD;
5909 int hisidx1 = 1;
5910 int hisidx2 = -1;
5911 int idx;
5912 int i, j, k;
5913 char_u *end;
5914 char_u *arg = eap->arg;
5915
5916 if (hislen == 0)
5917 {
5918 MSG(_("'history' option is zero"));
5919 return;
5920 }
5921
5922 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5923 {
5924 end = arg;
5925 while (ASCII_ISALPHA(*end)
5926 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5927 end++;
5928 i = *end;
5929 *end = NUL;
5930 histype1 = get_histtype(arg);
5931 if (histype1 == -1)
5932 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005933 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {
5935 histype1 = 0;
5936 histype2 = HIST_COUNT-1;
5937 }
5938 else
5939 {
5940 *end = i;
5941 EMSG(_(e_trailing));
5942 return;
5943 }
5944 }
5945 else
5946 histype2 = histype1;
5947 *end = i;
5948 }
5949 else
5950 end = arg;
5951 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5952 {
5953 EMSG(_(e_trailing));
5954 return;
5955 }
5956
5957 for (; !got_int && histype1 <= histype2; ++histype1)
5958 {
5959 STRCPY(IObuff, "\n # ");
5960 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5961 MSG_PUTS_TITLE(IObuff);
5962 idx = hisidx[histype1];
5963 hist = history[histype1];
5964 j = hisidx1;
5965 k = hisidx2;
5966 if (j < 0)
5967 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5968 if (k < 0)
5969 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5970 if (idx >= 0 && j <= k)
5971 for (i = idx + 1; !got_int; ++i)
5972 {
5973 if (i == hislen)
5974 i = 0;
5975 if (hist[i].hisstr != NULL
5976 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5977 {
5978 msg_putchar('\n');
5979 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5980 hist[i].hisnum);
5981 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5982 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005983 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 else
5985 STRCAT(IObuff, hist[i].hisstr);
5986 msg_outtrans(IObuff);
5987 out_flush();
5988 }
5989 if (i == idx)
5990 break;
5991 }
5992 }
5993}
5994#endif
5995
5996#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5997static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5998static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5999static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
6000static int viminfo_add_at_front = FALSE;
6001
6002static int hist_type2char __ARGS((int type, int use_question));
6003
6004/*
6005 * Translate a history type number to the associated character.
6006 */
6007 static int
6008hist_type2char(type, use_question)
6009 int type;
6010 int use_question; /* use '?' instead of '/' */
6011{
6012 if (type == HIST_CMD)
6013 return ':';
6014 if (type == HIST_SEARCH)
6015 {
6016 if (use_question)
6017 return '?';
6018 else
6019 return '/';
6020 }
6021 if (type == HIST_EXPR)
6022 return '=';
6023 return '@';
6024}
6025
6026/*
6027 * Prepare for reading the history from the viminfo file.
6028 * This allocates history arrays to store the read history lines.
6029 */
6030 void
6031prepare_viminfo_history(asklen)
6032 int asklen;
6033{
6034 int i;
6035 int num;
6036 int type;
6037 int len;
6038
6039 init_history();
6040 viminfo_add_at_front = (asklen != 0);
6041 if (asklen > hislen)
6042 asklen = hislen;
6043
6044 for (type = 0; type < HIST_COUNT; ++type)
6045 {
6046 /*
6047 * Count the number of empty spaces in the history list. If there are
6048 * more spaces available than we request, then fill them up.
6049 */
6050 for (i = 0, num = 0; i < hislen; i++)
6051 if (history[type][i].hisstr == NULL)
6052 num++;
6053 len = asklen;
6054 if (num > len)
6055 len = num;
6056 if (len <= 0)
6057 viminfo_history[type] = NULL;
6058 else
6059 viminfo_history[type] =
6060 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6061 if (viminfo_history[type] == NULL)
6062 len = 0;
6063 viminfo_hislen[type] = len;
6064 viminfo_hisidx[type] = 0;
6065 }
6066}
6067
6068/*
6069 * Accept a line from the viminfo, store it in the history array when it's
6070 * new.
6071 */
6072 int
6073read_viminfo_history(virp)
6074 vir_T *virp;
6075{
6076 int type;
6077 long_u len;
6078 char_u *val;
6079 char_u *p;
6080
6081 type = hist_char2type(virp->vir_line[0]);
6082 if (viminfo_hisidx[type] < viminfo_hislen[type])
6083 {
6084 val = viminfo_readstring(virp, 1, TRUE);
6085 if (val != NULL && *val != NUL)
6086 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006087 int sep = (*val == ' ' ? NUL : *val);
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006090 viminfo_add_at_front, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 {
6092 /* Need to re-allocate to append the separator byte. */
6093 len = STRLEN(val);
6094 p = lalloc(len + 2, TRUE);
6095 if (p != NULL)
6096 {
6097 if (type == HIST_SEARCH)
6098 {
6099 /* Search entry: Move the separator from the first
6100 * column to after the NUL. */
6101 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006102 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 }
6104 else
6105 {
6106 /* Not a search entry: No separator in the viminfo
6107 * file, add a NUL separator. */
6108 mch_memmove(p, val, (size_t)len + 1);
6109 p[len + 1] = NUL;
6110 }
6111 viminfo_history[type][viminfo_hisidx[type]++] = p;
6112 }
6113 }
6114 }
6115 vim_free(val);
6116 }
6117 return viminfo_readline(virp);
6118}
6119
6120 void
6121finish_viminfo_history()
6122{
6123 int idx;
6124 int i;
6125 int type;
6126
6127 for (type = 0; type < HIST_COUNT; ++type)
6128 {
6129 if (history[type] == NULL)
6130 return;
6131 idx = hisidx[type] + viminfo_hisidx[type];
6132 if (idx >= hislen)
6133 idx -= hislen;
6134 else if (idx < 0)
6135 idx = hislen - 1;
6136 if (viminfo_add_at_front)
6137 hisidx[type] = idx;
6138 else
6139 {
6140 if (hisidx[type] == -1)
6141 hisidx[type] = hislen - 1;
6142 do
6143 {
6144 if (history[type][idx].hisstr != NULL)
6145 break;
6146 if (++idx == hislen)
6147 idx = 0;
6148 } while (idx != hisidx[type]);
6149 if (idx != hisidx[type] && --idx < 0)
6150 idx = hislen - 1;
6151 }
6152 for (i = 0; i < viminfo_hisidx[type]; i++)
6153 {
6154 vim_free(history[type][idx].hisstr);
6155 history[type][idx].hisstr = viminfo_history[type][i];
6156 if (--idx < 0)
6157 idx = hislen - 1;
6158 }
6159 idx += 1;
6160 idx %= hislen;
6161 for (i = 0; i < viminfo_hisidx[type]; i++)
6162 {
6163 history[type][idx++].hisnum = ++hisnum[type];
6164 idx %= hislen;
6165 }
6166 vim_free(viminfo_history[type]);
6167 viminfo_history[type] = NULL;
6168 }
6169}
6170
6171 void
6172write_viminfo_history(fp)
6173 FILE *fp;
6174{
6175 int i;
6176 int type;
6177 int num_saved;
6178 char_u *p;
6179 int c;
6180
6181 init_history();
6182 if (hislen == 0)
6183 return;
6184 for (type = 0; type < HIST_COUNT; ++type)
6185 {
6186 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6187 if (num_saved == 0)
6188 continue;
6189 if (num_saved < 0) /* Use default */
6190 num_saved = hislen;
6191 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6192 type == HIST_CMD ? _("Command Line") :
6193 type == HIST_SEARCH ? _("Search String") :
6194 type == HIST_EXPR ? _("Expression") :
6195 _("Input Line"));
6196 if (num_saved > hislen)
6197 num_saved = hislen;
6198 i = hisidx[type];
6199 if (i >= 0)
6200 while (num_saved--)
6201 {
6202 p = history[type][i].hisstr;
6203 if (p != NULL)
6204 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006205 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 /* For the search history: put the separator in the second
6207 * column; use a space if there isn't one. */
6208 if (type == HIST_SEARCH)
6209 {
6210 c = p[STRLEN(p) + 1];
6211 putc(c == NUL ? ' ' : c, fp);
6212 }
6213 viminfo_writestring(fp, p);
6214 }
6215 if (--i < 0)
6216 i = hislen - 1;
6217 }
6218 }
6219}
6220#endif /* FEAT_VIMINFO */
6221
6222#if defined(FEAT_FKMAP) || defined(PROTO)
6223/*
6224 * Write a character at the current cursor+offset position.
6225 * It is directly written into the command buffer block.
6226 */
6227 void
6228cmd_pchar(c, offset)
6229 int c, offset;
6230{
6231 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6232 {
6233 EMSG(_("E198: cmd_pchar beyond the command length"));
6234 return;
6235 }
6236 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6237 ccline.cmdbuff[ccline.cmdlen] = NUL;
6238}
6239
6240 int
6241cmd_gchar(offset)
6242 int offset;
6243{
6244 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6245 {
6246 /* EMSG(_("cmd_gchar beyond the command length")); */
6247 return NUL;
6248 }
6249 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6250}
6251#endif
6252
6253#if defined(FEAT_CMDWIN) || defined(PROTO)
6254/*
6255 * Open a window on the current command line and history. Allow editing in
6256 * the window. Returns when the window is closed.
6257 * Returns:
6258 * CR if the command is to be executed
6259 * Ctrl_C if it is to be abandoned
6260 * K_IGNORE if editing continues
6261 */
6262 static int
6263ex_window()
6264{
6265 struct cmdline_info save_ccline;
6266 buf_T *old_curbuf = curbuf;
6267 win_T *old_curwin = curwin;
6268 buf_T *bp;
6269 win_T *wp;
6270 int i;
6271 linenr_T lnum;
6272 int histtype;
6273 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006274#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 int save_restart_edit = restart_edit;
6278 int save_State = State;
6279 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006280#ifdef FEAT_RIGHTLEFT
6281 int save_cmdmsg_rl = cmdmsg_rl;
6282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283
6284 /* Can't do this recursively. Can't do it when typing a password. */
6285 if (cmdwin_type != 0
6286# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6287 || cmdline_star > 0
6288# endif
6289 )
6290 {
6291 beep_flush();
6292 return K_IGNORE;
6293 }
6294
6295 /* Save current window sizes. */
6296 win_size_save(&winsizes);
6297
6298# ifdef FEAT_AUTOCMD
6299 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006300 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006302 /* don't use a new tab page */
6303 cmdmod.tab = 0;
6304
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 /* Create a window for the command-line buffer. */
6306 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6307 {
6308 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006309# ifdef FEAT_AUTOCMD
6310 unblock_autocmds();
6311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 return K_IGNORE;
6313 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006314 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
6316 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006317 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006318 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6320 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6321 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006322#ifdef FEAT_FOLDING
6323 curwin->w_p_fen = FALSE;
6324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006326 curwin->w_p_rl = cmdmsg_rl;
6327 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006329 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330
6331# ifdef FEAT_AUTOCMD
6332 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006333 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334# endif
6335
Bram Moolenaar46152342005-09-07 21:18:43 +00006336 /* Showing the prompt may have set need_wait_return, reset it. */
6337 need_wait_return = FALSE;
6338
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006339 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6341 {
6342 if (p_wc == TAB)
6343 {
6344 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6345 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6346 }
6347 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6348 }
6349
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006350 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6351 * sets 'textwidth' to 78). */
6352 curbuf->b_p_tw = 0;
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 /* Fill the buffer with the history. */
6355 init_history();
6356 if (hislen > 0)
6357 {
6358 i = hisidx[histtype];
6359 if (i >= 0)
6360 {
6361 lnum = 0;
6362 do
6363 {
6364 if (++i == hislen)
6365 i = 0;
6366 if (history[histtype][i].hisstr != NULL)
6367 ml_append(lnum++, history[histtype][i].hisstr,
6368 (colnr_T)0, FALSE);
6369 }
6370 while (i != hisidx[histtype]);
6371 }
6372 }
6373
6374 /* Replace the empty last line with the current command-line and put the
6375 * cursor there. */
6376 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6377 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6378 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006379 changed_line_abv_curs();
6380 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006381 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
6383 /* Save the command line info, can be used recursively. */
6384 save_ccline = ccline;
6385 ccline.cmdbuff = NULL;
6386 ccline.cmdprompt = NULL;
6387
6388 /* No Ex mode here! */
6389 exmode_active = 0;
6390
6391 State = NORMAL;
6392# ifdef FEAT_MOUSE
6393 setmouse();
6394# endif
6395
6396# ifdef FEAT_AUTOCMD
6397 /* Trigger CmdwinEnter autocommands. */
6398 typestr[0] = cmdwin_type;
6399 typestr[1] = NUL;
6400 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006401 if (restart_edit != 0) /* autocmd with ":startinsert" */
6402 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403# endif
6404
6405 i = RedrawingDisabled;
6406 RedrawingDisabled = 0;
6407
6408 /*
6409 * Call the main loop until <CR> or CTRL-C is typed.
6410 */
6411 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006412 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413
6414 RedrawingDisabled = i;
6415
6416# ifdef FEAT_AUTOCMD
6417 /* Trigger CmdwinLeave autocommands. */
6418 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6419# endif
6420
Bram Moolenaarccc18222007-05-10 18:25:20 +00006421 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 ccline = save_ccline;
6423 cmdwin_type = 0;
6424
6425 exmode_active = save_exmode;
6426
Bram Moolenaarf9821062008-06-20 16:31:07 +00006427 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 * this happens! */
6429 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6430 {
6431 cmdwin_result = Ctrl_C;
6432 EMSG(_("E199: Active window or buffer deleted"));
6433 }
6434 else
6435 {
6436# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6437 /* autocmds may abort script processing */
6438 if (aborting() && cmdwin_result != K_IGNORE)
6439 cmdwin_result = Ctrl_C;
6440# endif
6441 /* Set the new command line from the cmdline buffer. */
6442 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006443 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006445 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6446
6447 if (histtype == HIST_CMD)
6448 {
6449 /* Execute the command directly. */
6450 ccline.cmdbuff = vim_strsave((char_u *)p);
6451 cmdwin_result = CAR;
6452 }
6453 else
6454 {
6455 /* First need to cancel what we were doing. */
6456 ccline.cmdbuff = NULL;
6457 stuffcharReadbuff(':');
6458 stuffReadbuff((char_u *)p);
6459 stuffcharReadbuff(CAR);
6460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 }
6462 else if (cmdwin_result == K_XF2) /* :qa typed */
6463 {
6464 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6465 cmdwin_result = CAR;
6466 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006467 else if (cmdwin_result == Ctrl_C)
6468 {
6469 /* :q or :close, don't execute any command
6470 * and don't modify the cmd window. */
6471 ccline.cmdbuff = NULL;
6472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 else
6474 ccline.cmdbuff = vim_strsave(ml_get_curline());
6475 if (ccline.cmdbuff == NULL)
6476 cmdwin_result = Ctrl_C;
6477 else
6478 {
6479 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6480 ccline.cmdbufflen = ccline.cmdlen + 1;
6481 ccline.cmdpos = curwin->w_cursor.col;
6482 if (ccline.cmdpos > ccline.cmdlen)
6483 ccline.cmdpos = ccline.cmdlen;
6484 if (cmdwin_result == K_IGNORE)
6485 {
6486 set_cmdspos_cursor();
6487 redrawcmd();
6488 }
6489 }
6490
6491# ifdef FEAT_AUTOCMD
6492 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006493 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494# endif
6495 wp = curwin;
6496 bp = curbuf;
6497 win_goto(old_curwin);
6498 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006499
6500 /* win_close() may have already wiped the buffer when 'bh' is
6501 * set to 'wipe' */
6502 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006503 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
6505 /* Restore window sizes. */
6506 win_size_restore(&winsizes);
6507
6508# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006509 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510# endif
6511 }
6512
6513 ga_clear(&winsizes);
6514 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006515# ifdef FEAT_RIGHTLEFT
6516 cmdmsg_rl = save_cmdmsg_rl;
6517# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
6519 State = save_State;
6520# ifdef FEAT_MOUSE
6521 setmouse();
6522# endif
6523
6524 return cmdwin_result;
6525}
6526#endif /* FEAT_CMDWIN */
6527
6528/*
6529 * Used for commands that either take a simple command string argument, or:
6530 * cmd << endmarker
6531 * {script}
6532 * endmarker
6533 * Returns a pointer to allocated memory with {script} or NULL.
6534 */
6535 char_u *
6536script_get(eap, cmd)
6537 exarg_T *eap;
6538 char_u *cmd;
6539{
6540 char_u *theline;
6541 char *end_pattern = NULL;
6542 char dot[] = ".";
6543 garray_T ga;
6544
6545 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6546 return NULL;
6547
6548 ga_init2(&ga, 1, 0x400);
6549
6550 if (cmd[2] != NUL)
6551 end_pattern = (char *)skipwhite(cmd + 2);
6552 else
6553 end_pattern = dot;
6554
6555 for (;;)
6556 {
6557 theline = eap->getline(
6558#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006559 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560#endif
6561 NUL, eap->cookie, 0);
6562
6563 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006564 {
6565 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569 ga_concat(&ga, theline);
6570 ga_append(&ga, '\n');
6571 vim_free(theline);
6572 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006573 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
6575 return (char_u *)ga.ga_data;
6576}