blob: 3d0c26f05e55e089c75a632f008ba9b7a0bbfcab [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));
105static int nextwild __ARGS((expand_T *xp, int type, int options));
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;
670 /* CTRL-\ e doesn't work when obtaining an expression. */
671 if (c != Ctrl_N && c != Ctrl_G
672 && (c != 'e' || ccline.cmdfirstc == '='))
673 {
674 vungetc(c);
675 c = Ctrl_BSL;
676 }
677#ifdef FEAT_EVAL
678 else if (c == 'e')
679 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200680 char_u *p = NULL;
681 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682
683 /*
684 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000685 * Need to save and restore the current command line, to be
686 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 */
688 if (ccline.cmdpos == ccline.cmdlen)
689 new_cmdpos = 99999; /* keep it at the end */
690 else
691 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000692
693 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000695 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (c == '=')
697 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000698 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000699 * to avoid nasty things like going to another buffer when
700 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000701 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000702 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000704 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000705 restore_cmdline(&save_ccline);
706
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200707 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200709 len = (int)STRLEN(p);
710 if (realloc_cmdbuff(len + 1) == OK)
711 {
712 ccline.cmdlen = len;
713 STRCPY(ccline.cmdbuff, p);
714 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200716 /* Restore the cursor or use the position set with
717 * set_cmdline_pos(). */
718 if (new_cmdpos > ccline.cmdlen)
719 ccline.cmdpos = ccline.cmdlen;
720 else
721 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200723 KeyTyped = FALSE; /* Don't do p_wc completion. */
724 redrawcmd();
725 goto cmdline_changed;
726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 }
728 }
729 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100730 got_int = FALSE; /* don't abandon the command line */
731 did_emsg = FALSE;
732 emsg_on_display = FALSE;
733 redrawcmd();
734 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 }
736#endif
737 else
738 {
739 if (c == Ctrl_G && p_im && restart_edit == 0)
740 restart_edit = 'a';
741 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
742 in history */
743 goto returncmd; /* back to Normal mode */
744 }
745 }
746
747#ifdef FEAT_CMDWIN
748 if (c == cedit_key || c == K_CMDWIN)
749 {
750 /*
751 * Open a window to edit the command line (and history).
752 */
753 c = ex_window();
754 some_key_typed = TRUE;
755 }
756# ifdef FEAT_DIGRAPHS
757 else
758# endif
759#endif
760#ifdef FEAT_DIGRAPHS
761 c = do_digraph(c);
762#endif
763
764 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
765 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
766 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000767 /* In Ex mode a backslash escapes a newline. */
768 if (exmode_active
769 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000770 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000771 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000772 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000774 if (c == K_KENTER)
775 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000777 else
778 {
779 gotesc = FALSE; /* Might have typed ESC previously, don't
780 truncate the cmdline now. */
781 if (ccheck_abbr(c + ABBR_OFF))
782 goto cmdline_changed;
783 if (!cmd_silent)
784 {
785 windgoto(msg_row, 0);
786 out_flush();
787 }
788 break;
789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791
792 /*
793 * Completion for 'wildchar' or 'wildcharm' key.
794 * - hitting <ESC> twice means: abandon command line.
795 * - wildcard expansion is only done when the 'wildchar' key is really
796 * typed, not when it comes from a macro
797 */
798 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
799 {
800 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
801 {
802 /* if 'wildmode' contains "list" may still need to list */
803 if (xpc.xp_numfiles > 1
804 && !did_wild_list
805 && (wim_flags[wim_index] & WIM_LIST))
806 {
807 (void)showmatches(&xpc, FALSE);
808 redrawcmd();
809 did_wild_list = TRUE;
810 }
811 if (wim_flags[wim_index] & WIM_LONGEST)
812 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
813 else if (wim_flags[wim_index] & WIM_FULL)
814 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
815 else
816 res = OK; /* don't insert 'wildchar' now */
817 }
818 else /* typed p_wc first time */
819 {
820 wim_index = 0;
821 j = ccline.cmdpos;
822 /* if 'wildmode' first contains "longest", get longest
823 * common part */
824 if (wim_flags[0] & WIM_LONGEST)
825 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
826 else
827 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
828
829 /* if interrupted while completing, behave like it failed */
830 if (got_int)
831 {
832 (void)vpeekc(); /* remove <C-C> from input stream */
833 got_int = FALSE; /* don't abandon the command line */
834 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
835#ifdef FEAT_WILDMENU
836 xpc.xp_context = EXPAND_NOTHING;
837#endif
838 goto cmdline_changed;
839 }
840
841 /* when more than one match, and 'wildmode' first contains
842 * "list", or no change and 'wildmode' contains "longest,list",
843 * list all matches */
844 if (res == OK && xpc.xp_numfiles > 1)
845 {
846 /* a "longest" that didn't do anything is skipped (but not
847 * "list:longest") */
848 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
849 wim_index = 1;
850 if ((wim_flags[wim_index] & WIM_LIST)
851#ifdef FEAT_WILDMENU
852 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
853#endif
854 )
855 {
856 if (!(wim_flags[0] & WIM_LONGEST))
857 {
858#ifdef FEAT_WILDMENU
859 int p_wmnu_save = p_wmnu;
860 p_wmnu = 0;
861#endif
862 nextwild(&xpc, WILD_PREV, 0); /* remove match */
863#ifdef FEAT_WILDMENU
864 p_wmnu = p_wmnu_save;
865#endif
866 }
867#ifdef FEAT_WILDMENU
868 (void)showmatches(&xpc, p_wmnu
869 && ((wim_flags[wim_index] & WIM_LIST) == 0));
870#else
871 (void)showmatches(&xpc, FALSE);
872#endif
873 redrawcmd();
874 did_wild_list = TRUE;
875 if (wim_flags[wim_index] & WIM_LONGEST)
876 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
877 else if (wim_flags[wim_index] & WIM_FULL)
878 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
879 }
880 else
881 vim_beep();
882 }
883#ifdef FEAT_WILDMENU
884 else if (xpc.xp_numfiles == -1)
885 xpc.xp_context = EXPAND_NOTHING;
886#endif
887 }
888 if (wim_index < 3)
889 ++wim_index;
890 if (c == ESC)
891 gotesc = TRUE;
892 if (res == OK)
893 goto cmdline_changed;
894 }
895
896 gotesc = FALSE;
897
898 /* <S-Tab> goes to last match, in a clumsy way */
899 if (c == K_S_TAB && KeyTyped)
900 {
901 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
902 && nextwild(&xpc, WILD_PREV, 0) == OK
903 && nextwild(&xpc, WILD_PREV, 0) == OK)
904 goto cmdline_changed;
905 }
906
907 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
908 c = NL;
909
910 do_abbr = TRUE; /* default: check for abbreviation */
911
912 /*
913 * Big switch for a typed command line character.
914 */
915 switch (c)
916 {
917 case K_BS:
918 case Ctrl_H:
919 case K_DEL:
920 case K_KDEL:
921 case Ctrl_W:
922#ifdef FEAT_FKMAP
923 if (cmd_fkmap && c == K_BS)
924 c = K_DEL;
925#endif
926 if (c == K_KDEL)
927 c = K_DEL;
928
929 /*
930 * delete current character is the same as backspace on next
931 * character, except at end of line
932 */
933 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
934 ++ccline.cmdpos;
935#ifdef FEAT_MBYTE
936 if (has_mbyte && c == K_DEL)
937 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
938 ccline.cmdbuff + ccline.cmdpos);
939#endif
940 if (ccline.cmdpos > 0)
941 {
942 char_u *p;
943
944 j = ccline.cmdpos;
945 p = ccline.cmdbuff + j;
946#ifdef FEAT_MBYTE
947 if (has_mbyte)
948 {
949 p = mb_prevptr(ccline.cmdbuff, p);
950 if (c == Ctrl_W)
951 {
952 while (p > ccline.cmdbuff && vim_isspace(*p))
953 p = mb_prevptr(ccline.cmdbuff, p);
954 i = mb_get_class(p);
955 while (p > ccline.cmdbuff && mb_get_class(p) == i)
956 p = mb_prevptr(ccline.cmdbuff, p);
957 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000958 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 }
960 }
961 else
962#endif
963 if (c == Ctrl_W)
964 {
965 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
966 --p;
967 i = vim_iswordc(p[-1]);
968 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
969 && vim_iswordc(p[-1]) == i)
970 --p;
971 }
972 else
973 --p;
974 ccline.cmdpos = (int)(p - ccline.cmdbuff);
975 ccline.cmdlen -= j - ccline.cmdpos;
976 i = ccline.cmdpos;
977 while (i < ccline.cmdlen)
978 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
979
980 /* Truncate at the end, required for multi-byte chars. */
981 ccline.cmdbuff[ccline.cmdlen] = NUL;
982 redrawcmd();
983 }
984 else if (ccline.cmdlen == 0 && c != Ctrl_W
985 && ccline.cmdprompt == NULL && indent == 0)
986 {
987 /* In ex and debug mode it doesn't make sense to return. */
988 if (exmode_active
989#ifdef FEAT_EVAL
990 || ccline.cmdfirstc == '>'
991#endif
992 )
993 goto cmdline_not_changed;
994
995 vim_free(ccline.cmdbuff); /* no commandline to return */
996 ccline.cmdbuff = NULL;
997 if (!cmd_silent)
998 {
999#ifdef FEAT_RIGHTLEFT
1000 if (cmdmsg_rl)
1001 msg_col = Columns;
1002 else
1003#endif
1004 msg_col = 0;
1005 msg_putchar(' '); /* delete ':' */
1006 }
1007 redraw_cmdline = TRUE;
1008 goto returncmd; /* back to cmd mode */
1009 }
1010 goto cmdline_changed;
1011
1012 case K_INS:
1013 case K_KINS:
1014#ifdef FEAT_FKMAP
1015 /* if Farsi mode set, we are in reverse insert mode -
1016 Do not change the mode */
1017 if (cmd_fkmap)
1018 beep_flush();
1019 else
1020#endif
1021 ccline.overstrike = !ccline.overstrike;
1022#ifdef CURSOR_SHAPE
1023 ui_cursor_shape(); /* may show different cursor shape */
1024#endif
1025 goto cmdline_not_changed;
1026
1027 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001028 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 /* ":lmap" mappings exists, toggle use of mappings. */
1031 State ^= LANGMAP;
1032#ifdef USE_IM_CONTROL
1033 im_set_active(FALSE); /* Disable input method */
1034#endif
1035 if (b_im_ptr != NULL)
1036 {
1037 if (State & LANGMAP)
1038 *b_im_ptr = B_IMODE_LMAP;
1039 else
1040 *b_im_ptr = B_IMODE_NONE;
1041 }
1042 }
1043#ifdef USE_IM_CONTROL
1044 else
1045 {
1046 /* There are no ":lmap" mappings, toggle IM. When
1047 * 'imdisable' is set don't try getting the status, it's
1048 * always off. */
1049 if ((p_imdisable && b_im_ptr != NULL)
1050 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1051 {
1052 im_set_active(FALSE); /* Disable input method */
1053 if (b_im_ptr != NULL)
1054 *b_im_ptr = B_IMODE_NONE;
1055 }
1056 else
1057 {
1058 im_set_active(TRUE); /* Enable input method */
1059 if (b_im_ptr != NULL)
1060 *b_im_ptr = B_IMODE_IM;
1061 }
1062 }
1063#endif
1064 if (b_im_ptr != NULL)
1065 {
1066 if (b_im_ptr == &curbuf->b_p_iminsert)
1067 set_iminsert_global();
1068 else
1069 set_imsearch_global();
1070 }
1071#ifdef CURSOR_SHAPE
1072 ui_cursor_shape(); /* may show different cursor shape */
1073#endif
1074#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1075 /* Show/unshow value of 'keymap' in status lines later. */
1076 status_redraw_curbuf();
1077#endif
1078 goto cmdline_not_changed;
1079
1080/* case '@': only in very old vi */
1081 case Ctrl_U:
1082 /* delete all characters left of the cursor */
1083 j = ccline.cmdpos;
1084 ccline.cmdlen -= j;
1085 i = ccline.cmdpos = 0;
1086 while (i < ccline.cmdlen)
1087 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1088 /* Truncate at the end, required for multi-byte chars. */
1089 ccline.cmdbuff[ccline.cmdlen] = NUL;
1090 redrawcmd();
1091 goto cmdline_changed;
1092
1093#ifdef FEAT_CLIPBOARD
1094 case Ctrl_Y:
1095 /* Copy the modeless selection, if there is one. */
1096 if (clip_star.state != SELECT_CLEARED)
1097 {
1098 if (clip_star.state == SELECT_DONE)
1099 clip_copy_modeless_selection(TRUE);
1100 goto cmdline_not_changed;
1101 }
1102 break;
1103#endif
1104
1105 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1106 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001107 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001108 * ":normal" runs out of characters. */
1109 if (exmode_active
1110#ifdef FEAT_EX_EXTRA
1111 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1112#endif
1113 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 goto cmdline_not_changed;
1115
1116 gotesc = TRUE; /* will free ccline.cmdbuff after
1117 putting it in history */
1118 goto returncmd; /* back to cmd mode */
1119
1120 case Ctrl_R: /* insert register */
1121#ifdef USE_ON_FLY_SCROLL
1122 dont_scroll = TRUE; /* disallow scrolling here */
1123#endif
1124 putcmdline('"', TRUE);
1125 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001126 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (i == Ctrl_O)
1128 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1129 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001130 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 --no_mapping;
1132#ifdef FEAT_EVAL
1133 /*
1134 * Insert the result of an expression.
1135 * Need to save the current command line, to be able to enter
1136 * a new one...
1137 */
1138 new_cmdpos = -1;
1139 if (c == '=')
1140 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1142 {
1143 beep_flush();
1144 c = ESC;
1145 }
1146 else
1147 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001148 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001150 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 }
1152 }
1153#endif
1154 if (c != ESC) /* use ESC to cancel inserting register */
1155 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001156 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001157
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001158#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001159 /* When there was a serious error abort getting the
1160 * command line. */
1161 if (aborting())
1162 {
1163 gotesc = TRUE; /* will free ccline.cmdbuff after
1164 putting it in history */
1165 goto returncmd; /* back to cmd mode */
1166 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 KeyTyped = FALSE; /* Don't do p_wc completion. */
1169#ifdef FEAT_EVAL
1170 if (new_cmdpos >= 0)
1171 {
1172 /* set_cmdline_pos() was used */
1173 if (new_cmdpos > ccline.cmdlen)
1174 ccline.cmdpos = ccline.cmdlen;
1175 else
1176 ccline.cmdpos = new_cmdpos;
1177 }
1178#endif
1179 }
1180 redrawcmd();
1181 goto cmdline_changed;
1182
1183 case Ctrl_D:
1184 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1185 break; /* Use ^D as normal char instead */
1186
1187 redrawcmd();
1188 continue; /* don't do incremental search now */
1189
1190 case K_RIGHT:
1191 case K_S_RIGHT:
1192 case K_C_RIGHT:
1193 do
1194 {
1195 if (ccline.cmdpos >= ccline.cmdlen)
1196 break;
1197 i = cmdline_charsize(ccline.cmdpos);
1198 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1199 break;
1200 ccline.cmdspos += i;
1201#ifdef FEAT_MBYTE
1202 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001203 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 + ccline.cmdpos);
1205 else
1206#endif
1207 ++ccline.cmdpos;
1208 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001209 while ((c == K_S_RIGHT || c == K_C_RIGHT
1210 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1212#ifdef FEAT_MBYTE
1213 if (has_mbyte)
1214 set_cmdspos_cursor();
1215#endif
1216 goto cmdline_not_changed;
1217
1218 case K_LEFT:
1219 case K_S_LEFT:
1220 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001221 if (ccline.cmdpos == 0)
1222 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 do
1224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 --ccline.cmdpos;
1226#ifdef FEAT_MBYTE
1227 if (has_mbyte) /* move to first byte of char */
1228 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1229 ccline.cmdbuff + ccline.cmdpos);
1230#endif
1231 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1232 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001233 while (ccline.cmdpos > 0
1234 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001235 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1237#ifdef FEAT_MBYTE
1238 if (has_mbyte)
1239 set_cmdspos_cursor();
1240#endif
1241 goto cmdline_not_changed;
1242
1243 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001244 /* Ignore mouse event or ex_window() result. */
1245 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
Bram Moolenaar4770d092006-01-12 23:22:24 +00001247#ifdef FEAT_GUI_W32
1248 /* On Win32 ignore <M-F4>, we get it when closing the window was
1249 * cancelled. */
1250 case K_F4:
1251 if (mod_mask == MOD_MASK_ALT)
1252 {
1253 redrawcmd(); /* somehow the cmdline is cleared */
1254 goto cmdline_not_changed;
1255 }
1256 break;
1257#endif
1258
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259#ifdef FEAT_MOUSE
1260 case K_MIDDLEDRAG:
1261 case K_MIDDLERELEASE:
1262 goto cmdline_not_changed; /* Ignore mouse */
1263
1264 case K_MIDDLEMOUSE:
1265# ifdef FEAT_GUI
1266 /* When GUI is active, also paste when 'mouse' is empty */
1267 if (!gui.in_use)
1268# endif
1269 if (!mouse_has(MOUSE_COMMAND))
1270 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001271# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001273 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001275# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001276 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 redrawcmd();
1278 goto cmdline_changed;
1279
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001280# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001282 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 redrawcmd();
1284 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286
1287 case K_LEFTDRAG:
1288 case K_LEFTRELEASE:
1289 case K_RIGHTDRAG:
1290 case K_RIGHTRELEASE:
1291 /* Ignore drag and release events when the button-down wasn't
1292 * seen before. */
1293 if (ignore_drag_release)
1294 goto cmdline_not_changed;
1295 /* FALLTHROUGH */
1296 case K_LEFTMOUSE:
1297 case K_RIGHTMOUSE:
1298 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1299 ignore_drag_release = TRUE;
1300 else
1301 ignore_drag_release = FALSE;
1302# ifdef FEAT_GUI
1303 /* When GUI is active, also move when 'mouse' is empty */
1304 if (!gui.in_use)
1305# endif
1306 if (!mouse_has(MOUSE_COMMAND))
1307 goto cmdline_not_changed; /* Ignore mouse */
1308# ifdef FEAT_CLIPBOARD
1309 if (mouse_row < cmdline_row && clip_star.available)
1310 {
1311 int button, is_click, is_drag;
1312
1313 /*
1314 * Handle modeless selection.
1315 */
1316 button = get_mouse_button(KEY2TERMCAP1(c),
1317 &is_click, &is_drag);
1318 if (mouse_model_popup() && button == MOUSE_LEFT
1319 && (mod_mask & MOD_MASK_SHIFT))
1320 {
1321 /* Translate shift-left to right button. */
1322 button = MOUSE_RIGHT;
1323 mod_mask &= ~MOD_MASK_SHIFT;
1324 }
1325 clip_modeless(button, is_click, is_drag);
1326 goto cmdline_not_changed;
1327 }
1328# endif
1329
1330 set_cmdspos();
1331 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1332 ++ccline.cmdpos)
1333 {
1334 i = cmdline_charsize(ccline.cmdpos);
1335 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1336 && mouse_col < ccline.cmdspos % Columns + i)
1337 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001338# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 if (has_mbyte)
1340 {
1341 /* Count ">" for double-wide char that doesn't fit. */
1342 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001343 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 + ccline.cmdpos) - 1;
1345 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 ccline.cmdspos += i;
1348 }
1349 goto cmdline_not_changed;
1350
1351 /* Mouse scroll wheel: ignored here */
1352 case K_MOUSEDOWN:
1353 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001354 case K_MOUSELEFT:
1355 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 /* Alternate buttons ignored here */
1357 case K_X1MOUSE:
1358 case K_X1DRAG:
1359 case K_X1RELEASE:
1360 case K_X2MOUSE:
1361 case K_X2DRAG:
1362 case K_X2RELEASE:
1363 goto cmdline_not_changed;
1364
1365#endif /* FEAT_MOUSE */
1366
1367#ifdef FEAT_GUI
1368 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1369 case K_LEFTRELEASE_NM:
1370 goto cmdline_not_changed;
1371
1372 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001373 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
1375 gui_do_scroll();
1376 redrawcmd();
1377 }
1378 goto cmdline_not_changed;
1379
1380 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001381 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001383 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 redrawcmd();
1385 }
1386 goto cmdline_not_changed;
1387#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001388#ifdef FEAT_GUI_TABLINE
1389 case K_TABLINE:
1390 case K_TABMENU:
1391 /* Don't want to change any tabs here. Make sure the same tab
1392 * is still selected. */
1393 if (gui_use_tabline())
1394 gui_mch_set_curtab(tabpage_index(curtab));
1395 goto cmdline_not_changed;
1396#endif
1397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 case K_SELECT: /* end of Select mode mapping - ignore */
1399 goto cmdline_not_changed;
1400
1401 case Ctrl_B: /* begin of command line */
1402 case K_HOME:
1403 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 case K_S_HOME:
1405 case K_C_HOME:
1406 ccline.cmdpos = 0;
1407 set_cmdspos();
1408 goto cmdline_not_changed;
1409
1410 case Ctrl_E: /* end of command line */
1411 case K_END:
1412 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 case K_S_END:
1414 case K_C_END:
1415 ccline.cmdpos = ccline.cmdlen;
1416 set_cmdspos_cursor();
1417 goto cmdline_not_changed;
1418
1419 case Ctrl_A: /* all matches */
1420 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1421 break;
1422 goto cmdline_changed;
1423
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001424 case Ctrl_L:
1425#ifdef FEAT_SEARCH_EXTRA
1426 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1427 {
1428 /* Add a character from under the cursor for 'incsearch' */
1429 if (did_incsearch
1430 && !equalpos(curwin->w_cursor, old_cursor))
1431 {
1432 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001433 /* If 'ignorecase' and 'smartcase' are set and the
1434 * command line has no uppercase characters, convert
1435 * the character to lowercase */
1436 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1437 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001438 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001439 {
1440 if (c == firstc || vim_strchr((char_u *)(
1441 p_magic ? "\\^$.*[" : "\\^$"), c)
1442 != NULL)
1443 {
1444 /* put a backslash before special characters */
1445 stuffcharReadbuff(c);
1446 c = '\\';
1447 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001448 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001449 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001450 }
1451 goto cmdline_not_changed;
1452 }
1453#endif
1454
1455 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1457 break;
1458 goto cmdline_changed;
1459
1460 case Ctrl_N: /* next match */
1461 case Ctrl_P: /* previous match */
1462 if (xpc.xp_numfiles > 0)
1463 {
1464 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1465 == FAIL)
1466 break;
1467 goto cmdline_changed;
1468 }
1469
1470#ifdef FEAT_CMDHIST
1471 case K_UP:
1472 case K_DOWN:
1473 case K_S_UP:
1474 case K_S_DOWN:
1475 case K_PAGEUP:
1476 case K_KPAGEUP:
1477 case K_PAGEDOWN:
1478 case K_KPAGEDOWN:
1479 if (hislen == 0 || firstc == NUL) /* no history */
1480 goto cmdline_not_changed;
1481
1482 i = hiscnt;
1483
1484 /* save current command string so it can be restored later */
1485 if (lookfor == NULL)
1486 {
1487 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1488 goto cmdline_not_changed;
1489 lookfor[ccline.cmdpos] = NUL;
1490 }
1491
1492 j = (int)STRLEN(lookfor);
1493 for (;;)
1494 {
1495 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001496 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001497 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 {
1499 if (hiscnt == hislen) /* first time */
1500 hiscnt = hisidx[histype];
1501 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1502 hiscnt = hislen - 1;
1503 else if (hiscnt != hisidx[histype] + 1)
1504 --hiscnt;
1505 else /* at top of list */
1506 {
1507 hiscnt = i;
1508 break;
1509 }
1510 }
1511 else /* one step forwards */
1512 {
1513 /* on last entry, clear the line */
1514 if (hiscnt == hisidx[histype])
1515 {
1516 hiscnt = hislen;
1517 break;
1518 }
1519
1520 /* not on a history line, nothing to do */
1521 if (hiscnt == hislen)
1522 break;
1523 if (hiscnt == hislen - 1) /* wrap around */
1524 hiscnt = 0;
1525 else
1526 ++hiscnt;
1527 }
1528 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1529 {
1530 hiscnt = i;
1531 break;
1532 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001533 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001534 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 || STRNCMP(history[histype][hiscnt].hisstr,
1536 lookfor, (size_t)j) == 0)
1537 break;
1538 }
1539
1540 if (hiscnt != i) /* jumped to other entry */
1541 {
1542 char_u *p;
1543 int len;
1544 int old_firstc;
1545
1546 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001547 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (hiscnt == hislen)
1549 p = lookfor; /* back to the old one */
1550 else
1551 p = history[histype][hiscnt].hisstr;
1552
1553 if (histype == HIST_SEARCH
1554 && p != lookfor
1555 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1556 {
1557 /* Correct for the separator character used when
1558 * adding the history entry vs the one used now.
1559 * First loop: count length.
1560 * Second loop: copy the characters. */
1561 for (i = 0; i <= 1; ++i)
1562 {
1563 len = 0;
1564 for (j = 0; p[j] != NUL; ++j)
1565 {
1566 /* Replace old sep with new sep, unless it is
1567 * escaped. */
1568 if (p[j] == old_firstc
1569 && (j == 0 || p[j - 1] != '\\'))
1570 {
1571 if (i > 0)
1572 ccline.cmdbuff[len] = firstc;
1573 }
1574 else
1575 {
1576 /* Escape new sep, unless it is already
1577 * escaped. */
1578 if (p[j] == firstc
1579 && (j == 0 || p[j - 1] != '\\'))
1580 {
1581 if (i > 0)
1582 ccline.cmdbuff[len] = '\\';
1583 ++len;
1584 }
1585 if (i > 0)
1586 ccline.cmdbuff[len] = p[j];
1587 }
1588 ++len;
1589 }
1590 if (i == 0)
1591 {
1592 alloc_cmdbuff(len);
1593 if (ccline.cmdbuff == NULL)
1594 goto returncmd;
1595 }
1596 }
1597 ccline.cmdbuff[len] = NUL;
1598 }
1599 else
1600 {
1601 alloc_cmdbuff((int)STRLEN(p));
1602 if (ccline.cmdbuff == NULL)
1603 goto returncmd;
1604 STRCPY(ccline.cmdbuff, p);
1605 }
1606
1607 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1608 redrawcmd();
1609 goto cmdline_changed;
1610 }
1611 beep_flush();
1612 goto cmdline_not_changed;
1613#endif
1614
1615 case Ctrl_V:
1616 case Ctrl_Q:
1617#ifdef FEAT_MOUSE
1618 ignore_drag_release = TRUE;
1619#endif
1620 putcmdline('^', TRUE);
1621 c = get_literal(); /* get next (two) character(s) */
1622 do_abbr = FALSE; /* don't do abbreviation now */
1623#ifdef FEAT_MBYTE
1624 /* may need to remove ^ when composing char was typed */
1625 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1626 {
1627 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1628 msg_putchar(' ');
1629 cursorcmd();
1630 }
1631#endif
1632 break;
1633
1634#ifdef FEAT_DIGRAPHS
1635 case Ctrl_K:
1636#ifdef FEAT_MOUSE
1637 ignore_drag_release = TRUE;
1638#endif
1639 putcmdline('?', TRUE);
1640#ifdef USE_ON_FLY_SCROLL
1641 dont_scroll = TRUE; /* disallow scrolling here */
1642#endif
1643 c = get_digraph(TRUE);
1644 if (c != NUL)
1645 break;
1646
1647 redrawcmd();
1648 goto cmdline_not_changed;
1649#endif /* FEAT_DIGRAPHS */
1650
1651#ifdef FEAT_RIGHTLEFT
1652 case Ctrl__: /* CTRL-_: switch language mode */
1653 if (!p_ari)
1654 break;
1655#ifdef FEAT_FKMAP
1656 if (p_altkeymap)
1657 {
1658 cmd_fkmap = !cmd_fkmap;
1659 if (cmd_fkmap) /* in Farsi always in Insert mode */
1660 ccline.overstrike = FALSE;
1661 }
1662 else /* Hebrew is default */
1663#endif
1664 cmd_hkmap = !cmd_hkmap;
1665 goto cmdline_not_changed;
1666#endif
1667
1668 default:
1669#ifdef UNIX
1670 if (c == intr_char)
1671 {
1672 gotesc = TRUE; /* will free ccline.cmdbuff after
1673 putting it in history */
1674 goto returncmd; /* back to Normal mode */
1675 }
1676#endif
1677 /*
1678 * Normal character with no special meaning. Just set mod_mask
1679 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1680 * the string <S-Space>. This should only happen after ^V.
1681 */
1682 if (!IS_SPECIAL(c))
1683 mod_mask = 0x0;
1684 break;
1685 }
1686 /*
1687 * End of switch on command line character.
1688 * We come here if we have a normal character.
1689 */
1690
1691 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1692#ifdef FEAT_MBYTE
1693 /* Add ABBR_OFF for characters above 0x100, this is
1694 * what check_abbr() expects. */
1695 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1696#endif
1697 c))
1698 goto cmdline_changed;
1699
1700 /*
1701 * put the character in the command line
1702 */
1703 if (IS_SPECIAL(c) || mod_mask != 0)
1704 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1705 else
1706 {
1707#ifdef FEAT_MBYTE
1708 if (has_mbyte)
1709 {
1710 j = (*mb_char2bytes)(c, IObuff);
1711 IObuff[j] = NUL; /* exclude composing chars */
1712 put_on_cmdline(IObuff, j, TRUE);
1713 }
1714 else
1715#endif
1716 {
1717 IObuff[0] = c;
1718 put_on_cmdline(IObuff, 1, TRUE);
1719 }
1720 }
1721 goto cmdline_changed;
1722
1723/*
1724 * This part implements incremental searches for "/" and "?"
1725 * Jump to cmdline_not_changed when a character has been read but the command
1726 * line did not change. Then we only search and redraw if something changed in
1727 * the past.
1728 * Jump to cmdline_changed when the command line did change.
1729 * (Sorry for the goto's, I know it is ugly).
1730 */
1731cmdline_not_changed:
1732#ifdef FEAT_SEARCH_EXTRA
1733 if (!incsearch_postponed)
1734 continue;
1735#endif
1736
1737cmdline_changed:
1738#ifdef FEAT_SEARCH_EXTRA
1739 /*
1740 * 'incsearch' highlighting.
1741 */
1742 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1743 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001744 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001745#ifdef FEAT_RELTIME
1746 proftime_T tm;
1747#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 /* if there is a character waiting, search and redraw later */
1750 if (char_avail())
1751 {
1752 incsearch_postponed = TRUE;
1753 continue;
1754 }
1755 incsearch_postponed = FALSE;
1756 curwin->w_cursor = old_cursor; /* start at old position */
1757
1758 /* If there is no command line, don't do anything */
1759 if (ccline.cmdlen == 0)
1760 i = 0;
1761 else
1762 {
1763 cursor_off(); /* so the user knows we're busy */
1764 out_flush();
1765 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001766#ifdef FEAT_RELTIME
1767 /* Set the time limit to half a second. */
1768 profile_setlimit(500L, &tm);
1769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001771 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1772#ifdef FEAT_RELTIME
1773 &tm
1774#else
1775 NULL
1776#endif
1777 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 --emsg_off;
1779 /* if interrupted while searching, behave like it failed */
1780 if (got_int)
1781 {
1782 (void)vpeekc(); /* remove <C-C> from input stream */
1783 got_int = FALSE; /* don't abandon the command line */
1784 i = 0;
1785 }
1786 else if (char_avail())
1787 /* cancelled searching because a char was typed */
1788 incsearch_postponed = TRUE;
1789 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001790 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 highlight_match = TRUE; /* highlight position */
1792 else
1793 highlight_match = FALSE; /* remove highlight */
1794
1795 /* first restore the old curwin values, so the screen is
1796 * positioned in the same way as the actual search command */
1797 curwin->w_leftcol = old_leftcol;
1798 curwin->w_topline = old_topline;
1799# ifdef FEAT_DIFF
1800 curwin->w_topfill = old_topfill;
1801# endif
1802 curwin->w_botline = old_botline;
1803 changed_cline_bef_curs();
1804 update_topline();
1805
1806 if (i != 0)
1807 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001808 pos_T save_pos = curwin->w_cursor;
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001811 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 * moves the whole match onto the screen when 'nowrap' is set.
1813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 curwin->w_cursor.lnum += search_match_lines;
1815 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001816 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1817 {
1818 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1819 coladvance((colnr_T)MAXCOL);
1820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001822 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001823 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001825 else
1826 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001829# ifdef FEAT_WINDOWS
1830 /* May redraw the status line to show the cursor position. */
1831 if (p_ru && curwin->w_status_height > 0)
1832 curwin->w_redr_status = TRUE;
1833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001835 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001836 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001837 restore_cmdline(&save_ccline);
1838
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001839 /* Leave it at the end to make CTRL-R CTRL-W work. */
1840 if (i != 0)
1841 curwin->w_cursor = end_pos;
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 msg_starthere();
1844 redrawcmdline();
1845 did_incsearch = TRUE;
1846 }
1847#else /* FEAT_SEARCH_EXTRA */
1848 ;
1849#endif
1850
1851#ifdef FEAT_RIGHTLEFT
1852 if (cmdmsg_rl
1853# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001854 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855# endif
1856 )
1857 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001858 * right-left typing. Not efficient, but it works.
1859 * Do it only when there are no characters left to read
1860 * to avoid useless intermediate redraws. */
1861 if (vpeekc() == NUL)
1862 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863#endif
1864 }
1865
1866returncmd:
1867
1868#ifdef FEAT_RIGHTLEFT
1869 cmdmsg_rl = FALSE;
1870#endif
1871
1872#ifdef FEAT_FKMAP
1873 cmd_fkmap = 0;
1874#endif
1875
1876 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001877 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879#ifdef FEAT_SEARCH_EXTRA
1880 if (did_incsearch)
1881 {
1882 curwin->w_cursor = old_cursor;
1883 curwin->w_curswant = old_curswant;
1884 curwin->w_leftcol = old_leftcol;
1885 curwin->w_topline = old_topline;
1886# ifdef FEAT_DIFF
1887 curwin->w_topfill = old_topfill;
1888# endif
1889 curwin->w_botline = old_botline;
1890 highlight_match = FALSE;
1891 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001892 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 }
1894#endif
1895
1896 if (ccline.cmdbuff != NULL)
1897 {
1898 /*
1899 * Put line in history buffer (":" and "=" only when it was typed).
1900 */
1901#ifdef FEAT_CMDHIST
1902 if (ccline.cmdlen && firstc != NUL
1903 && (some_key_typed || histype == HIST_SEARCH))
1904 {
1905 add_to_history(histype, ccline.cmdbuff, TRUE,
1906 histype == HIST_SEARCH ? firstc : NUL);
1907 if (firstc == ':')
1908 {
1909 vim_free(new_last_cmdline);
1910 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1911 }
1912 }
1913#endif
1914
1915 if (gotesc) /* abandon command line */
1916 {
1917 vim_free(ccline.cmdbuff);
1918 ccline.cmdbuff = NULL;
1919 if (msg_scrolled == 0)
1920 compute_cmdrow();
1921 MSG("");
1922 redraw_cmdline = TRUE;
1923 }
1924 }
1925
1926 /*
1927 * If the screen was shifted up, redraw the whole screen (later).
1928 * If the line is too long, clear it, so ruler and shown command do
1929 * not get printed in the middle of it.
1930 */
1931 msg_check();
1932 msg_scroll = save_msg_scroll;
1933 redir_off = FALSE;
1934
1935 /* When the command line was typed, no need for a wait-return prompt. */
1936 if (some_key_typed)
1937 need_wait_return = FALSE;
1938
1939 State = save_State;
1940#ifdef USE_IM_CONTROL
1941 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1942 im_save_status(b_im_ptr);
1943 im_set_active(FALSE);
1944#endif
1945#ifdef FEAT_MOUSE
1946 setmouse();
1947#endif
1948#ifdef CURSOR_SHAPE
1949 ui_cursor_shape(); /* may show different cursor shape */
1950#endif
1951
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001952 {
1953 char_u *p = ccline.cmdbuff;
1954
1955 /* Make ccline empty, getcmdline() may try to use it. */
1956 ccline.cmdbuff = NULL;
1957 return p;
1958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959}
1960
1961#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1962/*
1963 * Get a command line with a prompt.
1964 * This is prepared to be called recursively from getcmdline() (e.g. by
1965 * f_input() when evaluating an expression from CTRL-R =).
1966 * Returns the command line in allocated memory, or NULL.
1967 */
1968 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001969getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 int firstc;
1971 char_u *prompt; /* command line prompt */
1972 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001973 int xp_context; /* type of expansion */
1974 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976 char_u *s;
1977 struct cmdline_info save_ccline;
1978 int msg_col_save = msg_col;
1979
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001980 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 ccline.cmdprompt = prompt;
1982 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001983# ifdef FEAT_EVAL
1984 ccline.xp_context = xp_context;
1985 ccline.xp_arg = xp_arg;
1986 ccline.input_fn = (firstc == '@');
1987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001989 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02001990 /* Restore msg_col, the prompt from input() may have changed it.
1991 * But only if called recursively and the commandline is therefore being
1992 * restored to an old one; if not, the input() prompt stays on the screen,
1993 * so we need its modified msg_col left intact. */
1994 if (ccline.cmdbuff != NULL)
1995 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
1997 return s;
1998}
1999#endif
2000
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002001/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002002 * Return TRUE when the text must not be changed and we can't switch to
2003 * another window or buffer. Used when editing the command line, evaluating
2004 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002005 */
2006 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002007text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002008{
2009#ifdef FEAT_CMDWIN
2010 if (cmdwin_type != 0)
2011 return TRUE;
2012#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002013 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002014}
2015
2016/*
2017 * Give an error message for a command that isn't allowed while the cmdline
2018 * window is open or editing the cmdline in another way.
2019 */
2020 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002021text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002022{
2023#ifdef FEAT_CMDWIN
2024 if (cmdwin_type != 0)
2025 EMSG(_(e_cmdwin));
2026 else
2027#endif
2028 EMSG(_(e_secure));
2029}
2030
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002031#if defined(FEAT_AUTOCMD) || defined(PROTO)
2032/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002033 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2034 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002035 */
2036 int
2037curbuf_locked()
2038{
2039 if (curbuf_lock > 0)
2040 {
2041 EMSG(_("E788: Not allowed to edit another buffer now"));
2042 return TRUE;
2043 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002044 return allbuf_locked();
2045}
2046
2047/*
2048 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2049 * message.
2050 */
2051 int
2052allbuf_locked()
2053{
2054 if (allbuf_lock > 0)
2055 {
2056 EMSG(_("E811: Not allowed to change buffer information now"));
2057 return TRUE;
2058 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002059 return FALSE;
2060}
2061#endif
2062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 static int
2064cmdline_charsize(idx)
2065 int idx;
2066{
2067#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2068 if (cmdline_star > 0) /* showing '*', always 1 position */
2069 return 1;
2070#endif
2071 return ptr2cells(ccline.cmdbuff + idx);
2072}
2073
2074/*
2075 * Compute the offset of the cursor on the command line for the prompt and
2076 * indent.
2077 */
2078 static void
2079set_cmdspos()
2080{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002081 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 ccline.cmdspos = 1 + ccline.cmdindent;
2083 else
2084 ccline.cmdspos = 0 + ccline.cmdindent;
2085}
2086
2087/*
2088 * Compute the screen position for the cursor on the command line.
2089 */
2090 static void
2091set_cmdspos_cursor()
2092{
2093 int i, m, c;
2094
2095 set_cmdspos();
2096 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002099 if (m < 0) /* overflow, Columns or Rows at weird value */
2100 m = MAXCOL;
2101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 else
2103 m = MAXCOL;
2104 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2105 {
2106 c = cmdline_charsize(i);
2107#ifdef FEAT_MBYTE
2108 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2109 if (has_mbyte)
2110 correct_cmdspos(i, c);
2111#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002112 /* If the cmdline doesn't fit, show cursor on last visible char.
2113 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 if ((ccline.cmdspos += c) >= m)
2115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 ccline.cmdspos -= c;
2117 break;
2118 }
2119#ifdef FEAT_MBYTE
2120 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002121 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#endif
2123 }
2124}
2125
2126#ifdef FEAT_MBYTE
2127/*
2128 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2129 * character that doesn't fit, so that a ">" must be displayed.
2130 */
2131 static void
2132correct_cmdspos(idx, cells)
2133 int idx;
2134 int cells;
2135{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002136 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2138 && ccline.cmdspos % Columns + cells > Columns)
2139 ccline.cmdspos++;
2140}
2141#endif
2142
2143/*
2144 * Get an Ex command line for the ":" command.
2145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002147getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002149 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 int indent; /* indent for inside conditionals */
2151{
2152 /* When executing a register, remove ':' that's in front of each line. */
2153 if (exec_from_reg && vpeekc() == ':')
2154 (void)vgetc();
2155 return getcmdline(c, 1L, indent);
2156}
2157
2158/*
2159 * Get an Ex command line for Ex mode.
2160 * In Ex mode we only use the OS supplied line editing features and no
2161 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002162 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002165getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002166 int promptc; /* normally ':', NUL for ":append" and '?' for
2167 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002168 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 int indent; /* indent for inside conditionals */
2170{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002171 garray_T line_ga;
2172 char_u *pend;
2173 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002174 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002175 int escaped = FALSE; /* CTRL-V typed */
2176 int vcol = 0;
2177 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002178 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
2180 /* Switch cursor on now. This avoids that it happens after the "\n", which
2181 * confuses the system function that computes tabstops. */
2182 cursor_on();
2183
2184 /* always start in column 0; write a newline if necessary */
2185 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002186 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002188 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002190 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002191 if (p_prompt)
2192 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 while (indent-- > 0)
2194 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 }
2197
2198 ga_init2(&line_ga, 1, 30);
2199
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002200 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002201 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002202 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002203 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002204 while (indent >= 8)
2205 {
2206 ga_append(&line_ga, TAB);
2207 msg_puts((char_u *)" ");
2208 indent -= 8;
2209 }
2210 while (indent-- > 0)
2211 {
2212 ga_append(&line_ga, ' ');
2213 msg_putchar(' ');
2214 }
2215 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002216 ++no_mapping;
2217 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 /*
2220 * Get the line, one character at a time.
2221 */
2222 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002223 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
2225 if (ga_grow(&line_ga, 40) == FAIL)
2226 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002228 /* Get one character at a time. Don't use inchar(), it can't handle
2229 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002230 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002231 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002234 * Handle line editing.
2235 * Previously this was left to the system, putting the terminal in
2236 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002238 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002240 msg_putchar('\n');
2241 break;
2242 }
2243
2244 if (!escaped)
2245 {
2246 /* CR typed means "enter", which is NL */
2247 if (c1 == '\r')
2248 c1 = '\n';
2249
2250 if (c1 == BS || c1 == K_BS
2251 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002253 if (line_ga.ga_len > 0)
2254 {
2255 --line_ga.ga_len;
2256 goto redraw;
2257 }
2258 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 }
2260
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002261 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002263 msg_col = startcol;
2264 msg_clr_eos();
2265 line_ga.ga_len = 0;
2266 continue;
2267 }
2268
2269 if (c1 == Ctrl_T)
2270 {
2271 p = (char_u *)line_ga.ga_data;
2272 p[line_ga.ga_len] = NUL;
2273 indent = get_indent_str(p, 8);
2274 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2275add_indent:
2276 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002278 char_u *s = skipwhite(p);
2279
2280 ga_grow(&line_ga, 1);
2281 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2282 *s = ' ';
2283 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002285redraw:
2286 /* redraw the line */
2287 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002288 vcol = 0;
2289 for (p = (char_u *)line_ga.ga_data;
2290 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002292 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002294 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002296 msg_putchar(' ');
2297 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002299 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002301 msg_outtrans_len(p, 1);
2302 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
2304 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002305 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002306 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002307 continue;
2308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 if (c1 == Ctrl_D)
2311 {
2312 /* Delete one shiftwidth. */
2313 p = (char_u *)line_ga.ga_data;
2314 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002316 if (prev_char == '^')
2317 ex_keep_indent = TRUE;
2318 indent = 0;
2319 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 else
2322 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002323 p[line_ga.ga_len] = NUL;
2324 indent = get_indent_str(p, 8);
2325 --indent;
2326 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002328 while (get_indent_str(p, 8) > indent)
2329 {
2330 char_u *s = skipwhite(p);
2331
2332 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2333 --line_ga.ga_len;
2334 }
2335 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002337
2338 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2339 {
2340 escaped = TRUE;
2341 continue;
2342 }
2343
2344 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2345 if (IS_SPECIAL(c1))
2346 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002348
2349 if (IS_SPECIAL(c1))
2350 c1 = '?';
2351 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002352 if (c1 == '\n')
2353 msg_putchar('\n');
2354 else if (c1 == TAB)
2355 {
2356 /* Don't use chartabsize(), 'ts' can be different */
2357 do
2358 {
2359 msg_putchar(' ');
2360 } while (++vcol % 8);
2361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002364 msg_outtrans_len(
2365 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2366 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002368 ++line_ga.ga_len;
2369 escaped = FALSE;
2370
2371 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002372 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002373
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002374 /* We are done when a NL is entered, but not when it comes after an
2375 * odd number of backslashes, that results in a NUL. */
2376 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002378 int bcount = 0;
2379
2380 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2381 ++bcount;
2382
2383 if (bcount > 0)
2384 {
2385 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2386 * "\NL", etc. */
2387 line_ga.ga_len -= (bcount + 1) / 2;
2388 pend -= (bcount + 1) / 2;
2389 pend[-1] = '\n';
2390 }
2391
2392 if ((bcount & 1) == 0)
2393 {
2394 --line_ga.ga_len;
2395 --pend;
2396 *pend = NUL;
2397 break;
2398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400 }
2401
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002402 --no_mapping;
2403 --allow_keys;
2404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 /* make following messages go to the next line */
2406 msg_didout = FALSE;
2407 msg_col = 0;
2408 if (msg_row < Rows - 1)
2409 ++msg_row;
2410 emsg_on_display = FALSE; /* don't want ui_delay() */
2411
2412 if (got_int)
2413 ga_clear(&line_ga);
2414
2415 return (char_u *)line_ga.ga_data;
2416}
2417
Bram Moolenaare344bea2005-09-01 20:46:49 +00002418# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2419 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420/*
2421 * Return TRUE if ccline.overstrike is on.
2422 */
2423 int
2424cmdline_overstrike()
2425{
2426 return ccline.overstrike;
2427}
2428
2429/*
2430 * Return TRUE if the cursor is at the end of the cmdline.
2431 */
2432 int
2433cmdline_at_end()
2434{
2435 return (ccline.cmdpos >= ccline.cmdlen);
2436}
2437#endif
2438
Bram Moolenaar9372a112005-12-06 19:59:18 +00002439#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440/*
2441 * Return the virtual column number at the current cursor position.
2442 * This is used by the IM code to obtain the start of the preedit string.
2443 */
2444 colnr_T
2445cmdline_getvcol_cursor()
2446{
2447 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2448 return MAXCOL;
2449
2450# ifdef FEAT_MBYTE
2451 if (has_mbyte)
2452 {
2453 colnr_T col;
2454 int i = 0;
2455
2456 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002457 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 return col;
2460 }
2461 else
2462# endif
2463 return ccline.cmdpos;
2464}
2465#endif
2466
2467#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2468/*
2469 * If part of the command line is an IM preedit string, redraw it with
2470 * IM feedback attributes. The cursor position is restored after drawing.
2471 */
2472 static void
2473redrawcmd_preedit()
2474{
2475 if ((State & CMDLINE)
2476 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002477 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 && !p_imdisable
2479 && im_is_preediting())
2480 {
2481 int cmdpos = 0;
2482 int cmdspos;
2483 int old_row;
2484 int old_col;
2485 colnr_T col;
2486
2487 old_row = msg_row;
2488 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002489 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
2491# ifdef FEAT_MBYTE
2492 if (has_mbyte)
2493 {
2494 for (col = 0; col < preedit_start_col
2495 && cmdpos < ccline.cmdlen; ++col)
2496 {
2497 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002498 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500 }
2501 else
2502# endif
2503 {
2504 cmdspos += preedit_start_col;
2505 cmdpos += preedit_start_col;
2506 }
2507
2508 msg_row = cmdline_row + (cmdspos / (int)Columns);
2509 msg_col = cmdspos % (int)Columns;
2510 if (msg_row >= Rows)
2511 msg_row = Rows - 1;
2512
2513 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2514 {
2515 int char_len;
2516 int char_attr;
2517
2518 char_attr = im_get_feedback_attr(col);
2519 if (char_attr < 0)
2520 break; /* end of preedit string */
2521
2522# ifdef FEAT_MBYTE
2523 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002524 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 else
2526# endif
2527 char_len = 1;
2528
2529 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2530 cmdpos += char_len;
2531 }
2532
2533 msg_row = old_row;
2534 msg_col = old_col;
2535 }
2536}
2537#endif /* FEAT_XIM && FEAT_GUI_GTK */
2538
2539/*
2540 * Allocate a new command line buffer.
2541 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2542 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2543 */
2544 static void
2545alloc_cmdbuff(len)
2546 int len;
2547{
2548 /*
2549 * give some extra space to avoid having to allocate all the time
2550 */
2551 if (len < 80)
2552 len = 100;
2553 else
2554 len += 20;
2555
2556 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2557 ccline.cmdbufflen = len;
2558}
2559
2560/*
2561 * Re-allocate the command line to length len + something extra.
2562 * return FAIL for failure, OK otherwise
2563 */
2564 static int
2565realloc_cmdbuff(len)
2566 int len;
2567{
2568 char_u *p;
2569
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002570 if (len < ccline.cmdbufflen)
2571 return OK; /* no need to resize */
2572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 p = ccline.cmdbuff;
2574 alloc_cmdbuff(len); /* will get some more */
2575 if (ccline.cmdbuff == NULL) /* out of memory */
2576 {
2577 ccline.cmdbuff = p; /* keep the old one */
2578 return FAIL;
2579 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002580 /* There isn't always a NUL after the command, but it may need to be
2581 * there, thus copy up to the NUL and add a NUL. */
2582 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2583 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002585
2586 if (ccline.xpc != NULL
2587 && ccline.xpc->xp_pattern != NULL
2588 && ccline.xpc->xp_context != EXPAND_NOTHING
2589 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2590 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002591 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002592
2593 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2594 * to point into the newly allocated memory. */
2595 if (i >= 0 && i <= ccline.cmdlen)
2596 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2597 }
2598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 return OK;
2600}
2601
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002602#if defined(FEAT_ARABIC) || defined(PROTO)
2603static char_u *arshape_buf = NULL;
2604
2605# if defined(EXITFREE) || defined(PROTO)
2606 void
2607free_cmdline_buf()
2608{
2609 vim_free(arshape_buf);
2610}
2611# endif
2612#endif
2613
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614/*
2615 * Draw part of the cmdline at the current cursor position. But draw stars
2616 * when cmdline_star is TRUE.
2617 */
2618 static void
2619draw_cmdline(start, len)
2620 int start;
2621 int len;
2622{
2623#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2624 int i;
2625
2626 if (cmdline_star > 0)
2627 for (i = 0; i < len; ++i)
2628 {
2629 msg_putchar('*');
2630# ifdef FEAT_MBYTE
2631 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002632 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633# endif
2634 }
2635 else
2636#endif
2637#ifdef FEAT_ARABIC
2638 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2639 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 static int buflen = 0;
2641 char_u *p;
2642 int j;
2643 int newlen = 0;
2644 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002645 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 int prev_c = 0;
2647 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002648 int u8c;
2649 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652 /*
2653 * Do arabic shaping into a temporary buffer. This is very
2654 * inefficient!
2655 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002656 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
2658 /* Re-allocate the buffer. We keep it around to avoid a lot of
2659 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002660 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002661 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002662 arshape_buf = alloc(buflen);
2663 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 return; /* out of memory */
2665 }
2666
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002667 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2668 {
2669 /* Prepend a space to draw the leading composing char on. */
2670 arshape_buf[0] = ' ';
2671 newlen = 1;
2672 }
2673
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 for (j = start; j < start + len; j += mb_l)
2675 {
2676 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002677 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002678 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 if (ARABIC_CHAR(u8c))
2680 {
2681 /* Do Arabic shaping. */
2682 if (cmdmsg_rl)
2683 {
2684 /* displaying from right to left */
2685 pc = prev_c;
2686 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002687 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 if (j + mb_l >= start + len)
2689 nc = NUL;
2690 else
2691 nc = utf_ptr2char(p + mb_l);
2692 }
2693 else
2694 {
2695 /* displaying from left to right */
2696 if (j + mb_l >= start + len)
2697 pc = NUL;
2698 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002699 {
2700 int pcc[MAX_MCO];
2701
2702 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002704 pc1 = pcc[0];
2705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 nc = prev_c;
2707 }
2708 prev_c = u8c;
2709
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002710 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002712 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002713 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002715 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2716 if (u8cc[1] != 0)
2717 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002718 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 }
2720 }
2721 else
2722 {
2723 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002724 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 newlen += mb_l;
2726 }
2727 }
2728
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002729 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731 else
2732#endif
2733 msg_outtrans_len(ccline.cmdbuff + start, len);
2734}
2735
2736/*
2737 * Put a character on the command line. Shifts the following text to the
2738 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2739 * "c" must be printable (fit in one display cell)!
2740 */
2741 void
2742putcmdline(c, shift)
2743 int c;
2744 int shift;
2745{
2746 if (cmd_silent)
2747 return;
2748 msg_no_more = TRUE;
2749 msg_putchar(c);
2750 if (shift)
2751 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2752 msg_no_more = FALSE;
2753 cursorcmd();
2754}
2755
2756/*
2757 * Undo a putcmdline(c, FALSE).
2758 */
2759 void
2760unputcmdline()
2761{
2762 if (cmd_silent)
2763 return;
2764 msg_no_more = TRUE;
2765 if (ccline.cmdlen == ccline.cmdpos)
2766 msg_putchar(' ');
2767 else
2768 draw_cmdline(ccline.cmdpos, 1);
2769 msg_no_more = FALSE;
2770 cursorcmd();
2771}
2772
2773/*
2774 * Put the given string, of the given length, onto the command line.
2775 * If len is -1, then STRLEN() is used to calculate the length.
2776 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2777 * part will be redrawn, otherwise it will not. If this function is called
2778 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2779 * called afterwards.
2780 */
2781 int
2782put_on_cmdline(str, len, redraw)
2783 char_u *str;
2784 int len;
2785 int redraw;
2786{
2787 int retval;
2788 int i;
2789 int m;
2790 int c;
2791
2792 if (len < 0)
2793 len = (int)STRLEN(str);
2794
2795 /* Check if ccline.cmdbuff needs to be longer */
2796 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002797 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 else
2799 retval = OK;
2800 if (retval == OK)
2801 {
2802 if (!ccline.overstrike)
2803 {
2804 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2805 ccline.cmdbuff + ccline.cmdpos,
2806 (size_t)(ccline.cmdlen - ccline.cmdpos));
2807 ccline.cmdlen += len;
2808 }
2809 else
2810 {
2811#ifdef FEAT_MBYTE
2812 if (has_mbyte)
2813 {
2814 /* Count nr of characters in the new string. */
2815 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002816 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 ++m;
2818 /* Count nr of bytes in cmdline that are overwritten by these
2819 * characters. */
2820 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002821 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 --m;
2823 if (i < ccline.cmdlen)
2824 {
2825 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2826 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2827 ccline.cmdlen += ccline.cmdpos + len - i;
2828 }
2829 else
2830 ccline.cmdlen = ccline.cmdpos + len;
2831 }
2832 else
2833#endif
2834 if (ccline.cmdpos + len > ccline.cmdlen)
2835 ccline.cmdlen = ccline.cmdpos + len;
2836 }
2837 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2838 ccline.cmdbuff[ccline.cmdlen] = NUL;
2839
2840#ifdef FEAT_MBYTE
2841 if (enc_utf8)
2842 {
2843 /* When the inserted text starts with a composing character,
2844 * backup to the character before it. There could be two of them.
2845 */
2846 i = 0;
2847 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2848 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2849 {
2850 i = (*mb_head_off)(ccline.cmdbuff,
2851 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2852 ccline.cmdpos -= i;
2853 len += i;
2854 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2855 }
2856# ifdef FEAT_ARABIC
2857 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2858 {
2859 /* Check the previous character for Arabic combining pair. */
2860 i = (*mb_head_off)(ccline.cmdbuff,
2861 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2862 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2863 + ccline.cmdpos - i), c))
2864 {
2865 ccline.cmdpos -= i;
2866 len += i;
2867 }
2868 else
2869 i = 0;
2870 }
2871# endif
2872 if (i != 0)
2873 {
2874 /* Also backup the cursor position. */
2875 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2876 ccline.cmdspos -= i;
2877 msg_col -= i;
2878 if (msg_col < 0)
2879 {
2880 msg_col += Columns;
2881 --msg_row;
2882 }
2883 }
2884 }
2885#endif
2886
2887 if (redraw && !cmd_silent)
2888 {
2889 msg_no_more = TRUE;
2890 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002891 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2893 /* Avoid clearing the rest of the line too often. */
2894 if (cmdline_row != i || ccline.overstrike)
2895 msg_clr_eos();
2896 msg_no_more = FALSE;
2897 }
2898#ifdef FEAT_FKMAP
2899 /*
2900 * If we are in Farsi command mode, the character input must be in
2901 * Insert mode. So do not advance the cmdpos.
2902 */
2903 if (!cmd_fkmap)
2904#endif
2905 {
2906 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002909 if (m < 0) /* overflow, Columns or Rows at weird value */
2910 m = MAXCOL;
2911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 else
2913 m = MAXCOL;
2914 for (i = 0; i < len; ++i)
2915 {
2916 c = cmdline_charsize(ccline.cmdpos);
2917#ifdef FEAT_MBYTE
2918 /* count ">" for a double-wide char that doesn't fit. */
2919 if (has_mbyte)
2920 correct_cmdspos(ccline.cmdpos, c);
2921#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002922 /* Stop cursor at the end of the screen, but do increment the
2923 * insert position, so that entering a very long command
2924 * works, even though you can't see it. */
2925 if (ccline.cmdspos + c < m)
2926 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927#ifdef FEAT_MBYTE
2928 if (has_mbyte)
2929 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002930 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 if (c > len - i - 1)
2932 c = len - i - 1;
2933 ccline.cmdpos += c;
2934 i += c;
2935 }
2936#endif
2937 ++ccline.cmdpos;
2938 }
2939 }
2940 }
2941 if (redraw)
2942 msg_check();
2943 return retval;
2944}
2945
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002946static struct cmdline_info prev_ccline;
2947static int prev_ccline_used = FALSE;
2948
2949/*
2950 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2951 * and overwrite it. But get_cmdline_str() may need it, thus make it
2952 * available globally in prev_ccline.
2953 */
2954 static void
2955save_cmdline(ccp)
2956 struct cmdline_info *ccp;
2957{
2958 if (!prev_ccline_used)
2959 {
2960 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2961 prev_ccline_used = TRUE;
2962 }
2963 *ccp = prev_ccline;
2964 prev_ccline = ccline;
2965 ccline.cmdbuff = NULL;
2966 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002967 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002968}
2969
2970/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002971 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002972 */
2973 static void
2974restore_cmdline(ccp)
2975 struct cmdline_info *ccp;
2976{
2977 ccline = prev_ccline;
2978 prev_ccline = *ccp;
2979}
2980
Bram Moolenaar5a305422006-04-28 22:38:25 +00002981#if defined(FEAT_EVAL) || defined(PROTO)
2982/*
2983 * Save the command line into allocated memory. Returns a pointer to be
2984 * passed to restore_cmdline_alloc() later.
2985 * Returns NULL when failed.
2986 */
2987 char_u *
2988save_cmdline_alloc()
2989{
2990 struct cmdline_info *p;
2991
2992 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2993 if (p != NULL)
2994 save_cmdline(p);
2995 return (char_u *)p;
2996}
2997
2998/*
2999 * Restore the command line from the return value of save_cmdline_alloc().
3000 */
3001 void
3002restore_cmdline_alloc(p)
3003 char_u *p;
3004{
3005 if (p != NULL)
3006 {
3007 restore_cmdline((struct cmdline_info *)p);
3008 vim_free(p);
3009 }
3010}
3011#endif
3012
Bram Moolenaar8299df92004-07-10 09:47:34 +00003013/*
3014 * paste a yank register into the command line.
3015 * used by CTRL-R command in command-line mode
3016 * insert_reg() can't be used here, because special characters from the
3017 * register contents will be interpreted as commands.
3018 *
3019 * return FAIL for failure, OK otherwise
3020 */
3021 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003022cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003023 int regname;
3024 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003025 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003026{
3027 long i;
3028 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003029 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003030 int allocated;
3031 struct cmdline_info save_ccline;
3032
3033 /* check for valid regname; also accept special characters for CTRL-R in
3034 * the command line */
3035 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3036 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3037 return FAIL;
3038
3039 /* A register containing CTRL-R can cause an endless loop. Allow using
3040 * CTRL-C to break the loop. */
3041 line_breakcheck();
3042 if (got_int)
3043 return FAIL;
3044
3045#ifdef FEAT_CLIPBOARD
3046 regname = may_get_selection(regname);
3047#endif
3048
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003049 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003050 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003051 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003052 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003053 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003054 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003055 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003056
3057 if (i)
3058 {
3059 /* Got the value of a special register in "arg". */
3060 if (arg == NULL)
3061 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003062
3063 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3064 * part of the word. */
3065 p = arg;
3066 if (p_is && regname == Ctrl_W)
3067 {
3068 char_u *w;
3069 int len;
3070
3071 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003072 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003073 {
3074#ifdef FEAT_MBYTE
3075 if (has_mbyte)
3076 {
3077 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3078 if (!vim_iswordc(mb_ptr2char(w - len)))
3079 break;
3080 w -= len;
3081 }
3082 else
3083#endif
3084 {
3085 if (!vim_iswordc(w[-1]))
3086 break;
3087 --w;
3088 }
3089 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003090 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003091 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3092 p += len;
3093 }
3094
3095 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003096 if (allocated)
3097 vim_free(arg);
3098 return OK;
3099 }
3100
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003101 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003102}
3103
3104/*
3105 * Put a string on the command line.
3106 * When "literally" is TRUE, insert literally.
3107 * When "literally" is FALSE, insert as typed, but don't leave the command
3108 * line.
3109 */
3110 void
3111cmdline_paste_str(s, literally)
3112 char_u *s;
3113 int literally;
3114{
3115 int c, cv;
3116
3117 if (literally)
3118 put_on_cmdline(s, -1, TRUE);
3119 else
3120 while (*s != NUL)
3121 {
3122 cv = *s;
3123 if (cv == Ctrl_V && s[1])
3124 ++s;
3125#ifdef FEAT_MBYTE
3126 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003127 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003128 else
3129#endif
3130 c = *s++;
3131 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3132#ifdef UNIX
3133 || c == intr_char
3134#endif
3135 || (c == Ctrl_BSL && *s == Ctrl_N))
3136 stuffcharReadbuff(Ctrl_V);
3137 stuffcharReadbuff(c);
3138 }
3139}
3140
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141#ifdef FEAT_WILDMENU
3142/*
3143 * Delete characters on the command line, from "from" to the current
3144 * position.
3145 */
3146 static void
3147cmdline_del(from)
3148 int from;
3149{
3150 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3151 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3152 ccline.cmdlen -= ccline.cmdpos - from;
3153 ccline.cmdpos = from;
3154}
3155#endif
3156
3157/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003158 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 * search
3160 */
3161 void
3162redrawcmdline()
3163{
3164 if (cmd_silent)
3165 return;
3166 need_wait_return = FALSE;
3167 compute_cmdrow();
3168 redrawcmd();
3169 cursorcmd();
3170}
3171
3172 static void
3173redrawcmdprompt()
3174{
3175 int i;
3176
3177 if (cmd_silent)
3178 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003179 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 msg_putchar(ccline.cmdfirstc);
3181 if (ccline.cmdprompt != NULL)
3182 {
3183 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3184 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3185 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003186 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 --ccline.cmdindent;
3188 }
3189 else
3190 for (i = ccline.cmdindent; i > 0; --i)
3191 msg_putchar(' ');
3192}
3193
3194/*
3195 * Redraw what is currently on the command line.
3196 */
3197 void
3198redrawcmd()
3199{
3200 if (cmd_silent)
3201 return;
3202
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003203 /* when 'incsearch' is set there may be no command line while redrawing */
3204 if (ccline.cmdbuff == NULL)
3205 {
3206 windgoto(cmdline_row, 0);
3207 msg_clr_eos();
3208 return;
3209 }
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 msg_start();
3212 redrawcmdprompt();
3213
3214 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3215 msg_no_more = TRUE;
3216 draw_cmdline(0, ccline.cmdlen);
3217 msg_clr_eos();
3218 msg_no_more = FALSE;
3219
3220 set_cmdspos_cursor();
3221
3222 /*
3223 * An emsg() before may have set msg_scroll. This is used in normal mode,
3224 * in cmdline mode we can reset them now.
3225 */
3226 msg_scroll = FALSE; /* next message overwrites cmdline */
3227
3228 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3229 * in cmdline mode */
3230 skip_redraw = FALSE;
3231}
3232
3233 void
3234compute_cmdrow()
3235{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003236 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 cmdline_row = Rows - 1;
3238 else
3239 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3240 + W_STATUS_HEIGHT(lastwin);
3241}
3242
3243 static void
3244cursorcmd()
3245{
3246 if (cmd_silent)
3247 return;
3248
3249#ifdef FEAT_RIGHTLEFT
3250 if (cmdmsg_rl)
3251 {
3252 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3253 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3254 if (msg_row <= 0)
3255 msg_row = Rows - 1;
3256 }
3257 else
3258#endif
3259 {
3260 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3261 msg_col = ccline.cmdspos % (int)Columns;
3262 if (msg_row >= Rows)
3263 msg_row = Rows - 1;
3264 }
3265
3266 windgoto(msg_row, msg_col);
3267#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3268 redrawcmd_preedit();
3269#endif
3270#ifdef MCH_CURSOR_SHAPE
3271 mch_update_cursor();
3272#endif
3273}
3274
3275 void
3276gotocmdline(clr)
3277 int clr;
3278{
3279 msg_start();
3280#ifdef FEAT_RIGHTLEFT
3281 if (cmdmsg_rl)
3282 msg_col = Columns - 1;
3283 else
3284#endif
3285 msg_col = 0; /* always start in column 0 */
3286 if (clr) /* clear the bottom line(s) */
3287 msg_clr_eos(); /* will reset clear_cmdline */
3288 windgoto(cmdline_row, 0);
3289}
3290
3291/*
3292 * Check the word in front of the cursor for an abbreviation.
3293 * Called when the non-id character "c" has been entered.
3294 * When an abbreviation is recognized it is removed from the text with
3295 * backspaces and the replacement string is inserted, followed by "c".
3296 */
3297 static int
3298ccheck_abbr(c)
3299 int c;
3300{
3301 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3302 return FALSE;
3303
3304 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3305}
3306
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003307#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3308 static int
3309#ifdef __BORLANDC__
3310_RTLENTRYF
3311#endif
3312sort_func_compare(s1, s2)
3313 const void *s1;
3314 const void *s2;
3315{
3316 char_u *p1 = *(char_u **)s1;
3317 char_u *p2 = *(char_u **)s2;
3318
3319 if (*p1 != '<' && *p2 == '<') return -1;
3320 if (*p1 == '<' && *p2 != '<') return 1;
3321 return STRCMP(p1, p2);
3322}
3323#endif
3324
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325/*
3326 * Return FAIL if this is not an appropriate context in which to do
3327 * completion of anything, return OK if it is (even if there are no matches).
3328 * For the caller, this means that the character is just passed through like a
3329 * normal character (instead of being expanded). This allows :s/^I^D etc.
3330 */
3331 static int
3332nextwild(xp, type, options)
3333 expand_T *xp;
3334 int type;
3335 int options; /* extra options for ExpandOne() */
3336{
3337 int i, j;
3338 char_u *p1;
3339 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 int difflen;
3341 int v;
3342
3343 if (xp->xp_numfiles == -1)
3344 {
3345 set_expand_context(xp);
3346 cmd_showtail = expand_showtail(xp);
3347 }
3348
3349 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3350 {
3351 beep_flush();
3352 return OK; /* Something illegal on command line */
3353 }
3354 if (xp->xp_context == EXPAND_NOTHING)
3355 {
3356 /* Caller can use the character as a normal char instead */
3357 return FAIL;
3358 }
3359
3360 MSG_PUTS("..."); /* show that we are busy */
3361 out_flush();
3362
3363 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003364 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
3366 if (type == WILD_NEXT || type == WILD_PREV)
3367 {
3368 /*
3369 * Get next/previous match for a previous expanded pattern.
3370 */
3371 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3372 }
3373 else
3374 {
3375 /*
3376 * Translate string into pattern and expand it.
3377 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003378 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3379 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 p2 = NULL;
3381 else
3382 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003383 int use_options = options |
3384 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE;
3385
3386 if (p_wic)
3387 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003388 p2 = ExpandOne(xp, p1,
3389 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003390 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003392 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 if (p2 != NULL && type == WILD_LONGEST)
3394 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003395 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 if (ccline.cmdbuff[i + j] == '*'
3397 || ccline.cmdbuff[i + j] == '?')
3398 break;
3399 if ((int)STRLEN(p2) < j)
3400 {
3401 vim_free(p2);
3402 p2 = NULL;
3403 }
3404 }
3405 }
3406 }
3407
3408 if (p2 != NULL && !got_int)
3409 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003410 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003411 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003413 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 xp->xp_pattern = ccline.cmdbuff + i;
3415 }
3416 else
3417 v = OK;
3418 if (v == OK)
3419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003420 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3421 &ccline.cmdbuff[ccline.cmdpos],
3422 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3423 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 ccline.cmdlen += difflen;
3425 ccline.cmdpos += difflen;
3426 }
3427 }
3428 vim_free(p2);
3429
3430 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003431 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
3433 /* When expanding a ":map" command and no matches are found, assume that
3434 * the key is supposed to be inserted literally */
3435 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3436 return FAIL;
3437
3438 if (xp->xp_numfiles <= 0 && p2 == NULL)
3439 beep_flush();
3440 else if (xp->xp_numfiles == 1)
3441 /* free expanded pattern */
3442 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3443
3444 return OK;
3445}
3446
3447/*
3448 * Do wildcard expansion on the string 'str'.
3449 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003450 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 * Return NULL for failure.
3452 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003453 * "orig" is the originally expanded string, copied to allocated memory. It
3454 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3455 * WILD_PREV "orig" should be NULL.
3456 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003457 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3458 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 *
3460 * mode = WILD_FREE: just free previously expanded matches
3461 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3462 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3463 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3464 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3465 * mode = WILD_ALL: return all matches concatenated
3466 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003467 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 *
3469 * options = WILD_LIST_NOTFOUND: list entries without a match
3470 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3471 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3472 * options = WILD_NO_BEEP: Don't beep for multiple matches
3473 * options = WILD_ADD_SLASH: add a slash after directory names
3474 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3475 * options = WILD_SILENT: don't print warning messages
3476 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003477 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 *
3479 * The variables xp->xp_context and xp->xp_backslash must have been set!
3480 */
3481 char_u *
3482ExpandOne(xp, str, orig, options, mode)
3483 expand_T *xp;
3484 char_u *str;
3485 char_u *orig; /* allocated copy of original of expanded string */
3486 int options;
3487 int mode;
3488{
3489 char_u *ss = NULL;
3490 static int findex;
3491 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003492 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 int i;
3494 long_u len;
3495 int non_suf_match; /* number without matching suffix */
3496
3497 /*
3498 * first handle the case of using an old match
3499 */
3500 if (mode == WILD_NEXT || mode == WILD_PREV)
3501 {
3502 if (xp->xp_numfiles > 0)
3503 {
3504 if (mode == WILD_PREV)
3505 {
3506 if (findex == -1)
3507 findex = xp->xp_numfiles;
3508 --findex;
3509 }
3510 else /* mode == WILD_NEXT */
3511 ++findex;
3512
3513 /*
3514 * When wrapping around, return the original string, set findex to
3515 * -1.
3516 */
3517 if (findex < 0)
3518 {
3519 if (orig_save == NULL)
3520 findex = xp->xp_numfiles - 1;
3521 else
3522 findex = -1;
3523 }
3524 if (findex >= xp->xp_numfiles)
3525 {
3526 if (orig_save == NULL)
3527 findex = 0;
3528 else
3529 findex = -1;
3530 }
3531#ifdef FEAT_WILDMENU
3532 if (p_wmnu)
3533 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3534 findex, cmd_showtail);
3535#endif
3536 if (findex == -1)
3537 return vim_strsave(orig_save);
3538 return vim_strsave(xp->xp_files[findex]);
3539 }
3540 else
3541 return NULL;
3542 }
3543
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003544 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3546 {
3547 FreeWild(xp->xp_numfiles, xp->xp_files);
3548 xp->xp_numfiles = -1;
3549 vim_free(orig_save);
3550 orig_save = NULL;
3551 }
3552 findex = 0;
3553
3554 if (mode == WILD_FREE) /* only release file name */
3555 return NULL;
3556
3557 if (xp->xp_numfiles == -1)
3558 {
3559 vim_free(orig_save);
3560 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003561 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 /*
3564 * Do the expansion.
3565 */
3566 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3567 options) == FAIL)
3568 {
3569#ifdef FNAME_ILLEGAL
3570 /* Illegal file name has been silently skipped. But when there
3571 * are wildcards, the real problem is that there was no match,
3572 * causing the pattern to be added, which has illegal characters.
3573 */
3574 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3575 EMSG2(_(e_nomatch2), str);
3576#endif
3577 }
3578 else if (xp->xp_numfiles == 0)
3579 {
3580 if (!(options & WILD_SILENT))
3581 EMSG2(_(e_nomatch2), str);
3582 }
3583 else
3584 {
3585 /* Escape the matches for use on the command line. */
3586 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3587
3588 /*
3589 * Check for matching suffixes in file names.
3590 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003591 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3592 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 {
3594 if (xp->xp_numfiles)
3595 non_suf_match = xp->xp_numfiles;
3596 else
3597 non_suf_match = 1;
3598 if ((xp->xp_context == EXPAND_FILES
3599 || xp->xp_context == EXPAND_DIRECTORIES)
3600 && xp->xp_numfiles > 1)
3601 {
3602 /*
3603 * More than one match; check suffix.
3604 * The files will have been sorted on matching suffix in
3605 * expand_wildcards, only need to check the first two.
3606 */
3607 non_suf_match = 0;
3608 for (i = 0; i < 2; ++i)
3609 if (match_suffix(xp->xp_files[i]))
3610 ++non_suf_match;
3611 }
3612 if (non_suf_match != 1)
3613 {
3614 /* Can we ever get here unless it's while expanding
3615 * interactively? If not, we can get rid of this all
3616 * together. Don't really want to wait for this message
3617 * (and possibly have to hit return to continue!).
3618 */
3619 if (!(options & WILD_SILENT))
3620 EMSG(_(e_toomany));
3621 else if (!(options & WILD_NO_BEEP))
3622 beep_flush();
3623 }
3624 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3625 ss = vim_strsave(xp->xp_files[0]);
3626 }
3627 }
3628 }
3629
3630 /* Find longest common part */
3631 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3632 {
3633 for (len = 0; xp->xp_files[0][len]; ++len)
3634 {
3635 for (i = 0; i < xp->xp_numfiles; ++i)
3636 {
3637#ifdef CASE_INSENSITIVE_FILENAME
3638 if (xp->xp_context == EXPAND_DIRECTORIES
3639 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003640 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 || xp->xp_context == EXPAND_BUFFERS)
3642 {
3643 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3644 TOLOWER_LOC(xp->xp_files[0][len]))
3645 break;
3646 }
3647 else
3648#endif
3649 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3650 break;
3651 }
3652 if (i < xp->xp_numfiles)
3653 {
3654 if (!(options & WILD_NO_BEEP))
3655 vim_beep();
3656 break;
3657 }
3658 }
3659 ss = alloc((unsigned)len + 1);
3660 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003661 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 findex = -1; /* next p_wc gets first one */
3663 }
3664
3665 /* Concatenate all matching names */
3666 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3667 {
3668 len = 0;
3669 for (i = 0; i < xp->xp_numfiles; ++i)
3670 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3671 ss = lalloc(len, TRUE);
3672 if (ss != NULL)
3673 {
3674 *ss = NUL;
3675 for (i = 0; i < xp->xp_numfiles; ++i)
3676 {
3677 STRCAT(ss, xp->xp_files[i]);
3678 if (i != xp->xp_numfiles - 1)
3679 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3680 }
3681 }
3682 }
3683
3684 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3685 ExpandCleanup(xp);
3686
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003687 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003688 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003689 vim_free(orig);
3690
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 return ss;
3692}
3693
3694/*
3695 * Prepare an expand structure for use.
3696 */
3697 void
3698ExpandInit(xp)
3699 expand_T *xp;
3700{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003701 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003702 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003704#ifndef BACKSLASH_IN_FILENAME
3705 xp->xp_shell = FALSE;
3706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 xp->xp_numfiles = -1;
3708 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003709#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3710 xp->xp_arg = NULL;
3711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712}
3713
3714/*
3715 * Cleanup an expand structure after use.
3716 */
3717 void
3718ExpandCleanup(xp)
3719 expand_T *xp;
3720{
3721 if (xp->xp_numfiles >= 0)
3722 {
3723 FreeWild(xp->xp_numfiles, xp->xp_files);
3724 xp->xp_numfiles = -1;
3725 }
3726}
3727
3728 void
3729ExpandEscape(xp, str, numfiles, files, options)
3730 expand_T *xp;
3731 char_u *str;
3732 int numfiles;
3733 char_u **files;
3734 int options;
3735{
3736 int i;
3737 char_u *p;
3738
3739 /*
3740 * May change home directory back to "~"
3741 */
3742 if (options & WILD_HOME_REPLACE)
3743 tilde_replace(str, numfiles, files);
3744
3745 if (options & WILD_ESCAPE)
3746 {
3747 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003748 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003749 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 || xp->xp_context == EXPAND_BUFFERS
3751 || xp->xp_context == EXPAND_DIRECTORIES)
3752 {
3753 /*
3754 * Insert a backslash into a file name before a space, \, %, #
3755 * and wildmatch characters, except '~'.
3756 */
3757 for (i = 0; i < numfiles; ++i)
3758 {
3759 /* for ":set path=" we need to escape spaces twice */
3760 if (xp->xp_backslash == XP_BS_THREE)
3761 {
3762 p = vim_strsave_escaped(files[i], (char_u *)" ");
3763 if (p != NULL)
3764 {
3765 vim_free(files[i]);
3766 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003767#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 p = vim_strsave_escaped(files[i], (char_u *)" ");
3769 if (p != NULL)
3770 {
3771 vim_free(files[i]);
3772 files[i] = p;
3773 }
3774#endif
3775 }
3776 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003777#ifdef BACKSLASH_IN_FILENAME
3778 p = vim_strsave_fnameescape(files[i], FALSE);
3779#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003780 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 if (p != NULL)
3783 {
3784 vim_free(files[i]);
3785 files[i] = p;
3786 }
3787
3788 /* If 'str' starts with "\~", replace "~" at start of
3789 * files[i] with "\~". */
3790 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003791 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003794
3795 /* If the first file starts with a '+' escape it. Otherwise it
3796 * could be seen as "+cmd". */
3797 if (*files[0] == '+')
3798 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800 else if (xp->xp_context == EXPAND_TAGS)
3801 {
3802 /*
3803 * Insert a backslash before characters in a tag name that
3804 * would terminate the ":tag" command.
3805 */
3806 for (i = 0; i < numfiles; ++i)
3807 {
3808 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3809 if (p != NULL)
3810 {
3811 vim_free(files[i]);
3812 files[i] = p;
3813 }
3814 }
3815 }
3816 }
3817}
3818
3819/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003820 * Escape special characters in "fname" for when used as a file name argument
3821 * after a Vim command, or, when "shell" is non-zero, a shell command.
3822 * Returns the result in allocated memory.
3823 */
3824 char_u *
3825vim_strsave_fnameescape(fname, shell)
3826 char_u *fname;
3827 int shell;
3828{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003829 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003830#ifdef BACKSLASH_IN_FILENAME
3831 char_u buf[20];
3832 int j = 0;
3833
3834 /* Don't escape '[' and '{' if they are in 'isfname'. */
3835 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3836 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3837 buf[j++] = *p;
3838 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003839 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003840#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003841 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3842 if (shell && csh_like_shell() && p != NULL)
3843 {
3844 char_u *s;
3845
3846 /* For csh and similar shells need to put two backslashes before '!'.
3847 * One is taken by Vim, one by the shell. */
3848 s = vim_strsave_escaped(p, (char_u *)"!");
3849 vim_free(p);
3850 p = s;
3851 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003852#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003853
3854 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3855 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003856 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003857 escape_fname(&p);
3858
3859 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003860}
3861
3862/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003863 * Put a backslash before the file name in "pp", which is in allocated memory.
3864 */
3865 static void
3866escape_fname(pp)
3867 char_u **pp;
3868{
3869 char_u *p;
3870
3871 p = alloc((unsigned)(STRLEN(*pp) + 2));
3872 if (p != NULL)
3873 {
3874 p[0] = '\\';
3875 STRCPY(p + 1, *pp);
3876 vim_free(*pp);
3877 *pp = p;
3878 }
3879}
3880
3881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 * For each file name in files[num_files]:
3883 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3884 */
3885 void
3886tilde_replace(orig_pat, num_files, files)
3887 char_u *orig_pat;
3888 int num_files;
3889 char_u **files;
3890{
3891 int i;
3892 char_u *p;
3893
3894 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3895 {
3896 for (i = 0; i < num_files; ++i)
3897 {
3898 p = home_replace_save(NULL, files[i]);
3899 if (p != NULL)
3900 {
3901 vim_free(files[i]);
3902 files[i] = p;
3903 }
3904 }
3905 }
3906}
3907
3908/*
3909 * Show all matches for completion on the command line.
3910 * Returns EXPAND_NOTHING when the character that triggered expansion should
3911 * be inserted like a normal character.
3912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 static int
3914showmatches(xp, wildmenu)
3915 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003916 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917{
3918#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3919 int num_files;
3920 char_u **files_found;
3921 int i, j, k;
3922 int maxlen;
3923 int lines;
3924 int columns;
3925 char_u *p;
3926 int lastlen;
3927 int attr;
3928 int showtail;
3929
3930 if (xp->xp_numfiles == -1)
3931 {
3932 set_expand_context(xp);
3933 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3934 &num_files, &files_found);
3935 showtail = expand_showtail(xp);
3936 if (i != EXPAND_OK)
3937 return i;
3938
3939 }
3940 else
3941 {
3942 num_files = xp->xp_numfiles;
3943 files_found = xp->xp_files;
3944 showtail = cmd_showtail;
3945 }
3946
3947#ifdef FEAT_WILDMENU
3948 if (!wildmenu)
3949 {
3950#endif
3951 msg_didany = FALSE; /* lines_left will be set */
3952 msg_start(); /* prepare for paging */
3953 msg_putchar('\n');
3954 out_flush();
3955 cmdline_row = msg_row;
3956 msg_didany = FALSE; /* lines_left will be set again */
3957 msg_start(); /* prepare for paging */
3958#ifdef FEAT_WILDMENU
3959 }
3960#endif
3961
3962 if (got_int)
3963 got_int = FALSE; /* only int. the completion, not the cmd line */
3964#ifdef FEAT_WILDMENU
3965 else if (wildmenu)
3966 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3967#endif
3968 else
3969 {
3970 /* find the length of the longest file name */
3971 maxlen = 0;
3972 for (i = 0; i < num_files; ++i)
3973 {
3974 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003975 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 || xp->xp_context == EXPAND_BUFFERS))
3977 {
3978 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3979 j = vim_strsize(NameBuff);
3980 }
3981 else
3982 j = vim_strsize(L_SHOWFILE(i));
3983 if (j > maxlen)
3984 maxlen = j;
3985 }
3986
3987 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3988 lines = num_files;
3989 else
3990 {
3991 /* compute the number of columns and lines for the listing */
3992 maxlen += 2; /* two spaces between file names */
3993 columns = ((int)Columns + 2) / maxlen;
3994 if (columns < 1)
3995 columns = 1;
3996 lines = (num_files + columns - 1) / columns;
3997 }
3998
3999 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4000
4001 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4002 {
4003 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4004 msg_clr_eos();
4005 msg_advance(maxlen - 3);
4006 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4007 }
4008
4009 /* list the files line by line */
4010 for (i = 0; i < lines; ++i)
4011 {
4012 lastlen = 999;
4013 for (k = i; k < num_files; k += lines)
4014 {
4015 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4016 {
4017 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4018 p = files_found[k] + STRLEN(files_found[k]) + 1;
4019 msg_advance(maxlen + 1);
4020 msg_puts(p);
4021 msg_advance(maxlen + 3);
4022 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4023 break;
4024 }
4025 for (j = maxlen - lastlen; --j >= 0; )
4026 msg_putchar(' ');
4027 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004028 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 || xp->xp_context == EXPAND_BUFFERS)
4030 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004031 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004032 if (xp->xp_numfiles != -1)
4033 {
4034 char_u *halved_slash;
4035 char_u *exp_path;
4036
4037 /* Expansion was done before and special characters
4038 * were escaped, need to halve backslashes. Also
4039 * $HOME has been replaced with ~/. */
4040 exp_path = expand_env_save_opt(files_found[k], TRUE);
4041 halved_slash = backslash_halve_save(
4042 exp_path != NULL ? exp_path : files_found[k]);
4043 j = mch_isdir(halved_slash != NULL ? halved_slash
4044 : files_found[k]);
4045 vim_free(exp_path);
4046 vim_free(halved_slash);
4047 }
4048 else
4049 /* Expansion was done here, file names are literal. */
4050 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 if (showtail)
4052 p = L_SHOWFILE(k);
4053 else
4054 {
4055 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4056 TRUE);
4057 p = NameBuff;
4058 }
4059 }
4060 else
4061 {
4062 j = FALSE;
4063 p = L_SHOWFILE(k);
4064 }
4065 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4066 }
4067 if (msg_col > 0) /* when not wrapped around */
4068 {
4069 msg_clr_eos();
4070 msg_putchar('\n');
4071 }
4072 out_flush(); /* show one line at a time */
4073 if (got_int)
4074 {
4075 got_int = FALSE;
4076 break;
4077 }
4078 }
4079
4080 /*
4081 * we redraw the command below the lines that we have just listed
4082 * This is a bit tricky, but it saves a lot of screen updating.
4083 */
4084 cmdline_row = msg_row; /* will put it back later */
4085 }
4086
4087 if (xp->xp_numfiles == -1)
4088 FreeWild(num_files, files_found);
4089
4090 return EXPAND_OK;
4091}
4092
4093/*
4094 * Private gettail for showmatches() (and win_redr_status_matches()):
4095 * Find tail of file name path, but ignore trailing "/".
4096 */
4097 char_u *
4098sm_gettail(s)
4099 char_u *s;
4100{
4101 char_u *p;
4102 char_u *t = s;
4103 int had_sep = FALSE;
4104
4105 for (p = s; *p != NUL; )
4106 {
4107 if (vim_ispathsep(*p)
4108#ifdef BACKSLASH_IN_FILENAME
4109 && !rem_backslash(p)
4110#endif
4111 )
4112 had_sep = TRUE;
4113 else if (had_sep)
4114 {
4115 t = p;
4116 had_sep = FALSE;
4117 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004118 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
4120 return t;
4121}
4122
4123/*
4124 * Return TRUE if we only need to show the tail of completion matches.
4125 * When not completing file names or there is a wildcard in the path FALSE is
4126 * returned.
4127 */
4128 static int
4129expand_showtail(xp)
4130 expand_T *xp;
4131{
4132 char_u *s;
4133 char_u *end;
4134
4135 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004136 if (xp->xp_context != EXPAND_FILES
4137 && xp->xp_context != EXPAND_SHELLCMD
4138 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 return FALSE;
4140
4141 end = gettail(xp->xp_pattern);
4142 if (end == xp->xp_pattern) /* there is no path separator */
4143 return FALSE;
4144
4145 for (s = xp->xp_pattern; s < end; s++)
4146 {
4147 /* Skip escaped wildcards. Only when the backslash is not a path
4148 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4149 if (rem_backslash(s))
4150 ++s;
4151 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4152 return FALSE;
4153 }
4154 return TRUE;
4155}
4156
4157/*
4158 * Prepare a string for expansion.
4159 * When expanding file names: The string will be used with expand_wildcards().
4160 * Copy the file name into allocated memory and add a '*' at the end.
4161 * When expanding other names: The string will be used with regcomp(). Copy
4162 * the name into allocated memory and prepend "^".
4163 */
4164 char_u *
4165addstar(fname, len, context)
4166 char_u *fname;
4167 int len;
4168 int context; /* EXPAND_FILES etc. */
4169{
4170 char_u *retval;
4171 int i, j;
4172 int new_len;
4173 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004174 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004176 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004177 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004178 && context != EXPAND_SHELLCMD
4179 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 /*
4182 * Matching will be done internally (on something other than files).
4183 * So we convert the file-matching-type wildcards into our kind for
4184 * use with vim_regcomp(). First work out how long it will be:
4185 */
4186
4187 /* For help tags the translation is done in find_help_tags().
4188 * For a tag pattern starting with "/" no translation is needed. */
4189 if (context == EXPAND_HELP
4190 || context == EXPAND_COLORS
4191 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004192 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004193 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 || (context == EXPAND_TAGS && fname[0] == '/'))
4195 retval = vim_strnsave(fname, len);
4196 else
4197 {
4198 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4199 for (i = 0; i < len; i++)
4200 {
4201 if (fname[i] == '*' || fname[i] == '~')
4202 new_len++; /* '*' needs to be replaced by ".*"
4203 '~' needs to be replaced by "\~" */
4204
4205 /* Buffer names are like file names. "." should be literal */
4206 if (context == EXPAND_BUFFERS && fname[i] == '.')
4207 new_len++; /* "." becomes "\." */
4208
4209 /* Custom expansion takes care of special things, match
4210 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004211 if ((context == EXPAND_USER_DEFINED
4212 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 new_len++; /* '\' becomes "\\" */
4214 }
4215 retval = alloc(new_len);
4216 if (retval != NULL)
4217 {
4218 retval[0] = '^';
4219 j = 1;
4220 for (i = 0; i < len; i++, j++)
4221 {
4222 /* Skip backslash. But why? At least keep it for custom
4223 * expansion. */
4224 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004225 && context != EXPAND_USER_LIST
4226 && fname[i] == '\\'
4227 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 break;
4229
4230 switch (fname[i])
4231 {
4232 case '*': retval[j++] = '.';
4233 break;
4234 case '~': retval[j++] = '\\';
4235 break;
4236 case '?': retval[j] = '.';
4237 continue;
4238 case '.': if (context == EXPAND_BUFFERS)
4239 retval[j++] = '\\';
4240 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004241 case '\\': if (context == EXPAND_USER_DEFINED
4242 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 retval[j++] = '\\';
4244 break;
4245 }
4246 retval[j] = fname[i];
4247 }
4248 retval[j] = NUL;
4249 }
4250 }
4251 }
4252 else
4253 {
4254 retval = alloc(len + 4);
4255 if (retval != NULL)
4256 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004257 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004260 * Don't add a star to *, ~, ~user, $var or `cmd`.
4261 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 * ~ would be at the start of the file name, but not the tail.
4263 * $ could be anywhere in the tail.
4264 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004265 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 */
4267 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004268 ends_in_star = (len > 0 && retval[len - 1] == '*');
4269#ifndef BACKSLASH_IN_FILENAME
4270 for (i = len - 2; i >= 0; --i)
4271 {
4272 if (retval[i] != '\\')
4273 break;
4274 ends_in_star = !ends_in_star;
4275 }
4276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004278 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 && vim_strchr(tail, '$') == NULL
4280 && vim_strchr(retval, '`') == NULL)
4281 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004282 else if (len > 0 && retval[len - 1] == '$')
4283 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 retval[len] = NUL;
4285 }
4286 }
4287 return retval;
4288}
4289
4290/*
4291 * Must parse the command line so far to work out what context we are in.
4292 * Completion can then be done based on that context.
4293 * This routine sets the variables:
4294 * xp->xp_pattern The start of the pattern to be expanded within
4295 * the command line (ends at the cursor).
4296 * xp->xp_context The type of thing to expand. Will be one of:
4297 *
4298 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4299 * the command line, like an unknown command. Caller
4300 * should beep.
4301 * EXPAND_NOTHING Unrecognised context for completion, use char like
4302 * a normal char, rather than for completion. eg
4303 * :s/^I/
4304 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4305 * it.
4306 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4307 * EXPAND_FILES After command with XFILE set, or after setting
4308 * with P_EXPAND set. eg :e ^I, :w>>^I
4309 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4310 * when we know only directories are of interest. eg
4311 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004312 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4314 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4315 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4316 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4317 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4318 * EXPAND_EVENTS Complete event names
4319 * EXPAND_SYNTAX Complete :syntax command arguments
4320 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4321 * EXPAND_AUGROUP Complete autocommand group names
4322 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4323 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4324 * eg :unmap a^I , :cunab x^I
4325 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4326 * eg :call sub^I
4327 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4328 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4329 * names in expressions, eg :while s^I
4330 * EXPAND_ENV_VARS Complete environment variable names
4331 */
4332 static void
4333set_expand_context(xp)
4334 expand_T *xp;
4335{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004336 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (ccline.cmdfirstc != ':'
4338#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004339 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004340 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#endif
4342 )
4343 {
4344 xp->xp_context = EXPAND_NOTHING;
4345 return;
4346 }
4347 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4348}
4349
4350 void
4351set_cmd_context(xp, str, len, col)
4352 expand_T *xp;
4353 char_u *str; /* start of command line */
4354 int len; /* length of command line (excl. NUL) */
4355 int col; /* position of cursor */
4356{
4357 int old_char = NUL;
4358 char_u *nextcomm;
4359
4360 /*
4361 * Avoid a UMR warning from Purify, only save the character if it has been
4362 * written before.
4363 */
4364 if (col < len)
4365 old_char = str[col];
4366 str[col] = NUL;
4367 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004368
4369#ifdef FEAT_EVAL
4370 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004371 {
4372# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004373 /* pass CMD_SIZE because there is no real command */
4374 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004375# endif
4376 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004377 else if (ccline.input_fn)
4378 {
4379 xp->xp_context = ccline.xp_context;
4380 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004381# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004382 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004383# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004384 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004385 else
4386#endif
4387 while (nextcomm != NULL)
4388 nextcomm = set_one_cmd_context(xp, nextcomm);
4389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 str[col] = old_char;
4391}
4392
4393/*
4394 * Expand the command line "str" from context "xp".
4395 * "xp" must have been set by set_cmd_context().
4396 * xp->xp_pattern points into "str", to where the text that is to be expanded
4397 * starts.
4398 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4399 * cursor.
4400 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4401 * key that triggered expansion literally.
4402 * Returns EXPAND_OK otherwise.
4403 */
4404 int
4405expand_cmdline(xp, str, col, matchcount, matches)
4406 expand_T *xp;
4407 char_u *str; /* start of command line */
4408 int col; /* position of cursor */
4409 int *matchcount; /* return: nr of matches */
4410 char_u ***matches; /* return: array of pointers to matches */
4411{
4412 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004413 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
4415 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4416 {
4417 beep_flush();
4418 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4419 }
4420 if (xp->xp_context == EXPAND_NOTHING)
4421 {
4422 /* Caller can use the character as a normal char instead */
4423 return EXPAND_NOTHING;
4424 }
4425
4426 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004427 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4428 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 if (file_str == NULL)
4430 return EXPAND_UNSUCCESSFUL;
4431
Bram Moolenaar94950a92010-12-02 16:01:29 +01004432 if (p_wic)
4433 options += WILD_ICASE;
4434
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004436 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
4438 *matchcount = 0;
4439 *matches = NULL;
4440 }
4441 vim_free(file_str);
4442
4443 return EXPAND_OK;
4444}
4445
4446#ifdef FEAT_MULTI_LANG
4447/*
4448 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4449 */
4450static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4451
4452 static void
4453cleanup_help_tags(num_file, file)
4454 int num_file;
4455 char_u **file;
4456{
4457 int i, j;
4458 int len;
4459
4460 for (i = 0; i < num_file; ++i)
4461 {
4462 len = (int)STRLEN(file[i]) - 3;
4463 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4464 {
4465 /* Sorting on priority means the same item in another language may
4466 * be anywhere. Search all items for a match up to the "@en". */
4467 for (j = 0; j < num_file; ++j)
4468 if (j != i
4469 && (int)STRLEN(file[j]) == len + 3
4470 && STRNCMP(file[i], file[j], len + 1) == 0)
4471 break;
4472 if (j == num_file)
4473 file[i][len] = NUL;
4474 }
4475 }
4476}
4477#endif
4478
4479/*
4480 * Do the expansion based on xp->xp_context and "pat".
4481 */
4482 static int
4483ExpandFromContext(xp, pat, num_file, file, options)
4484 expand_T *xp;
4485 char_u *pat;
4486 int *num_file;
4487 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004488 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489{
4490#ifdef FEAT_CMDL_COMPL
4491 regmatch_T regmatch;
4492#endif
4493 int ret;
4494 int flags;
4495
4496 flags = EW_DIR; /* include directories */
4497 if (options & WILD_LIST_NOTFOUND)
4498 flags |= EW_NOTFOUND;
4499 if (options & WILD_ADD_SLASH)
4500 flags |= EW_ADDSLASH;
4501 if (options & WILD_KEEP_ALL)
4502 flags |= EW_KEEPALL;
4503 if (options & WILD_SILENT)
4504 flags |= EW_SILENT;
4505
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004506 if (xp->xp_context == EXPAND_FILES
4507 || xp->xp_context == EXPAND_DIRECTORIES
4508 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
4510 /*
4511 * Expand file or directory names.
4512 */
4513 int free_pat = FALSE;
4514 int i;
4515
4516 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4517 * space */
4518 if (xp->xp_backslash != XP_BS_NONE)
4519 {
4520 free_pat = TRUE;
4521 pat = vim_strsave(pat);
4522 for (i = 0; pat[i]; ++i)
4523 if (pat[i] == '\\')
4524 {
4525 if (xp->xp_backslash == XP_BS_THREE
4526 && pat[i + 1] == '\\'
4527 && pat[i + 2] == '\\'
4528 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004529 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 if (xp->xp_backslash == XP_BS_ONE
4531 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004532 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 }
4535
4536 if (xp->xp_context == EXPAND_FILES)
4537 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004538 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4539 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 else
4541 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004542 if (options & WILD_ICASE)
4543 flags |= EW_ICASE;
4544
Bram Moolenaard7834d32009-12-02 16:14:36 +00004545 /* Expand wildcards, supporting %:h and the like. */
4546 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (free_pat)
4548 vim_free(pat);
4549 return ret;
4550 }
4551
4552 *file = (char_u **)"";
4553 *num_file = 0;
4554 if (xp->xp_context == EXPAND_HELP)
4555 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004556 /* With an empty argument we would get all the help tags, which is
4557 * very slow. Get matches for "help" instead. */
4558 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4559 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
4561#ifdef FEAT_MULTI_LANG
4562 cleanup_help_tags(*num_file, *file);
4563#endif
4564 return OK;
4565 }
4566 return FAIL;
4567 }
4568
4569#ifndef FEAT_CMDL_COMPL
4570 return FAIL;
4571#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004572 if (xp->xp_context == EXPAND_SHELLCMD)
4573 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (xp->xp_context == EXPAND_OLD_SETTING)
4575 return ExpandOldSetting(num_file, file);
4576 if (xp->xp_context == EXPAND_BUFFERS)
4577 return ExpandBufnames(pat, num_file, file, options);
4578 if (xp->xp_context == EXPAND_TAGS
4579 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4580 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4581 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004582 {
4583 char *directories[] = {"colors", NULL};
4584 return ExpandRTDir(pat, num_file, file, directories);
4585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004587 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004588 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004589 return ExpandRTDir(pat, num_file, file, directories);
4590 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004591 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004592 {
4593 char *directories[] = {"syntax", NULL};
4594 return ExpandRTDir(pat, num_file, file, directories);
4595 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004596 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004597 {
4598 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4599 return ExpandRTDir(pat, num_file, file, directories);
4600 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004601# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4602 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004603 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004604# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4607 if (regmatch.regprog == NULL)
4608 return FAIL;
4609
4610 /* set ignore-case according to p_ic, p_scs and pat */
4611 regmatch.rm_ic = ignorecase(pat);
4612
4613 if (xp->xp_context == EXPAND_SETTINGS
4614 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4615 ret = ExpandSettings(xp, &regmatch, num_file, file);
4616 else if (xp->xp_context == EXPAND_MAPPINGS)
4617 ret = ExpandMappings(&regmatch, num_file, file);
4618# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4619 else if (xp->xp_context == EXPAND_USER_DEFINED)
4620 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4621# endif
4622 else
4623 {
4624 static struct expgen
4625 {
4626 int context;
4627 char_u *((*func)__ARGS((expand_T *, int)));
4628 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004629 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 } tab[] =
4631 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004632 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4633 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004634#ifdef FEAT_CMDHIST
4635 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004638 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4639 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4640 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4641 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642#endif
4643#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004644 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4645 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4646 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4647 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648#endif
4649#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004650 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4651 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652#endif
4653#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004654 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004656 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004658 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4659 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004661#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004662 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004663#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004664#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004665 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004666#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004667#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004668 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4671 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004672 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4673 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004675 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 };
4677 int i;
4678
4679 /*
4680 * Find a context in the table and call the ExpandGeneric() with the
4681 * right function to do the expansion.
4682 */
4683 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004684 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (xp->xp_context == tab[i].context)
4686 {
4687 if (tab[i].ic)
4688 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004689 ret = ExpandGeneric(xp, &regmatch, num_file, file,
4690 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 break;
4692 }
4693 }
4694
4695 vim_free(regmatch.regprog);
4696
4697 return ret;
4698#endif /* FEAT_CMDL_COMPL */
4699}
4700
4701#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4702/*
4703 * Expand a list of names.
4704 *
4705 * Generic function for command line completion. It calls a function to
4706 * obtain strings, one by one. The strings are matched against a regexp
4707 * program. Matching strings are copied into an array, which is returned.
4708 *
4709 * Returns OK when no problems encountered, FAIL for error (out of memory).
4710 */
4711 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004712ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 expand_T *xp;
4714 regmatch_T *regmatch;
4715 int *num_file;
4716 char_u ***file;
4717 char_u *((*func)__ARGS((expand_T *, int)));
4718 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004719 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
4721 int i;
4722 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004723 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 char_u *str;
4725
4726 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004727 * round == 0: count the number of matching names
4728 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004730 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
4732 for (i = 0; ; ++i)
4733 {
4734 str = (*func)(xp, i);
4735 if (str == NULL) /* end of list */
4736 break;
4737 if (*str == NUL) /* skip empty strings */
4738 continue;
4739
4740 if (vim_regexec(regmatch, str, (colnr_T)0))
4741 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004742 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004744 if (escaped)
4745 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4746 else
4747 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 (*file)[count] = str;
4749#ifdef FEAT_MENU
4750 if (func == get_menu_names && str != NULL)
4751 {
4752 /* test for separator added by get_menu_names() */
4753 str += STRLEN(str) - 1;
4754 if (*str == '\001')
4755 *str = '.';
4756 }
4757#endif
4758 }
4759 ++count;
4760 }
4761 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004762 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
4764 if (count == 0)
4765 return OK;
4766 *num_file = count;
4767 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4768 if (*file == NULL)
4769 {
4770 *file = (char_u **)"";
4771 return FAIL;
4772 }
4773 count = 0;
4774 }
4775 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004776
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004777 /* Sort the results. Keep menu's in the specified order. */
4778 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004779 {
4780 if (xp->xp_context == EXPAND_EXPRESSION
4781 || xp->xp_context == EXPAND_FUNCTIONS
4782 || xp->xp_context == EXPAND_USER_FUNC)
4783 /* <SNR> functions should be sorted to the end. */
4784 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4785 sort_func_compare);
4786 else
4787 sort_strings(*file, *num_file);
4788 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004789
Bram Moolenaar4f688582007-07-24 12:34:30 +00004790#ifdef FEAT_CMDL_COMPL
4791 /* Reset the variables used for special highlight names expansion, so that
4792 * they don't show up when getting normal highlight names by ID. */
4793 reset_expand_highlight();
4794#endif
4795
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 return OK;
4797}
4798
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004799/*
4800 * Complete a shell command.
4801 * Returns FAIL or OK;
4802 */
4803 static int
4804expand_shellcmd(filepat, num_file, file, flagsarg)
4805 char_u *filepat; /* pattern to match with command names */
4806 int *num_file; /* return: number of matches */
4807 char_u ***file; /* return: array with matches */
4808 int flagsarg; /* EW_ flags */
4809{
4810 char_u *pat;
4811 int i;
4812 char_u *path;
4813 int mustfree = FALSE;
4814 garray_T ga;
4815 char_u *buf = alloc(MAXPATHL);
4816 size_t l;
4817 char_u *s, *e;
4818 int flags = flagsarg;
4819 int ret;
4820
4821 if (buf == NULL)
4822 return FAIL;
4823
4824 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4825 * space */
4826 pat = vim_strsave(filepat);
4827 for (i = 0; pat[i]; ++i)
4828 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004829 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004830
4831 flags |= EW_FILE | EW_EXEC;
4832
4833 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004834 if (mch_isFullName(pat))
4835 path = (char_u *)" ";
4836 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004837 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4838 path = (char_u *)".";
4839 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004840 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004841 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004842 if (path == NULL)
4843 path = (char_u *)"";
4844 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004845
4846 /*
4847 * Go over all directories in $PATH. Expand matches in that directory and
4848 * collect them in "ga".
4849 */
4850 ga_init2(&ga, (int)sizeof(char *), 10);
4851 for (s = path; *s != NUL; s = e)
4852 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004853 if (*s == ' ')
4854 ++s; /* Skip space used for absolute path name. */
4855
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004856#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4857 e = vim_strchr(s, ';');
4858#else
4859 e = vim_strchr(s, ':');
4860#endif
4861 if (e == NULL)
4862 e = s + STRLEN(s);
4863
4864 l = e - s;
4865 if (l > MAXPATHL - 5)
4866 break;
4867 vim_strncpy(buf, s, l);
4868 add_pathsep(buf);
4869 l = STRLEN(buf);
4870 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4871
4872 /* Expand matches in one directory of $PATH. */
4873 ret = expand_wildcards(1, &buf, num_file, file, flags);
4874 if (ret == OK)
4875 {
4876 if (ga_grow(&ga, *num_file) == FAIL)
4877 FreeWild(*num_file, *file);
4878 else
4879 {
4880 for (i = 0; i < *num_file; ++i)
4881 {
4882 s = (*file)[i];
4883 if (STRLEN(s) > l)
4884 {
4885 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004886 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004887 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4888 }
4889 else
4890 vim_free(s);
4891 }
4892 vim_free(*file);
4893 }
4894 }
4895 if (*e != NUL)
4896 ++e;
4897 }
4898 *file = ga.ga_data;
4899 *num_file = ga.ga_len;
4900
4901 vim_free(buf);
4902 vim_free(pat);
4903 if (mustfree)
4904 vim_free(path);
4905 return OK;
4906}
4907
4908
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004910static 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));
4911
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004913 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004914 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004916 static void *
4917call_user_expand_func(user_expand_func, xp, num_file, file)
4918 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 int *num_file;
4921 char_u ***file;
4922{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004923 char_u keep;
4924 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004927 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004928 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
4930 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004931 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 *num_file = 0;
4933 *file = NULL;
4934
Bram Moolenaar21669c02008-01-18 12:16:16 +00004935 if (ccline.cmdbuff == NULL)
4936 {
4937 /* Completion from Insert mode, pass fake arguments. */
4938 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004939 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004940 args[1] = xp->xp_pattern;
4941 }
4942 else
4943 {
4944 /* Completion on the command line, pass real arguments. */
4945 keep = ccline.cmdbuff[ccline.cmdlen];
4946 ccline.cmdbuff[ccline.cmdlen] = 0;
4947 sprintf((char *)num, "%d", ccline.cmdpos);
4948 args[1] = ccline.cmdbuff;
4949 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004950 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 args[2] = num;
4952
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004953 /* Save the cmdline, we don't know what the function may do. */
4954 save_ccline = ccline;
4955 ccline.cmdbuff = NULL;
4956 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004958
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004959 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004960
4961 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004963 if (ccline.cmdbuff != NULL)
4964 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004965
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004966 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004967 return ret;
4968}
4969
4970/*
4971 * Expand names with a function defined by the user.
4972 */
4973 static int
4974ExpandUserDefined(xp, regmatch, num_file, file)
4975 expand_T *xp;
4976 regmatch_T *regmatch;
4977 int *num_file;
4978 char_u ***file;
4979{
4980 char_u *retstr;
4981 char_u *s;
4982 char_u *e;
4983 char_u keep;
4984 garray_T ga;
4985
4986 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4987 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 return FAIL;
4989
4990 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004991 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 e = vim_strchr(s, '\n');
4994 if (e == NULL)
4995 e = s + STRLEN(s);
4996 keep = *e;
4997 *e = 0;
4998
4999 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5000 {
5001 *e = keep;
5002 if (*e != NUL)
5003 ++e;
5004 continue;
5005 }
5006
5007 if (ga_grow(&ga, 1) == FAIL)
5008 break;
5009
5010 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5011 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
5013 *e = keep;
5014 if (*e != NUL)
5015 ++e;
5016 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005017 vim_free(retstr);
5018 *file = ga.ga_data;
5019 *num_file = ga.ga_len;
5020 return OK;
5021}
5022
5023/*
5024 * Expand names with a list returned by a function defined by the user.
5025 */
5026 static int
5027ExpandUserList(xp, num_file, file)
5028 expand_T *xp;
5029 int *num_file;
5030 char_u ***file;
5031{
5032 list_T *retlist;
5033 listitem_T *li;
5034 garray_T ga;
5035
5036 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5037 if (retlist == NULL)
5038 return FAIL;
5039
5040 ga_init2(&ga, (int)sizeof(char *), 3);
5041 /* Loop over the items in the list. */
5042 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5043 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005044 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5045 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005046
5047 if (ga_grow(&ga, 1) == FAIL)
5048 break;
5049
5050 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005051 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005052 ++ga.ga_len;
5053 }
5054 list_unref(retlist);
5055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 *file = ga.ga_data;
5057 *num_file = ga.ga_len;
5058 return OK;
5059}
5060#endif
5061
5062/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005063 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005064 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005065 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 */
5067 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005068ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 char_u *pat;
5070 int *num_file;
5071 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005072 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005074 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 char_u *s;
5076 char_u *e;
5077 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005078 int i;
5079 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
5081 *num_file = 0;
5082 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005083 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005084 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005086 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005088 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5089 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005091 ga_clear_strings(&ga);
5092 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005094 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5095 matches = globpath(p_rtp, s, 0);
5096 vim_free(s);
5097 if (matches == NULL)
5098 continue;
5099
5100 for (s = matches; *s != NUL; s = e)
5101 {
5102 e = vim_strchr(s, '\n');
5103 if (e == NULL)
5104 e = s + STRLEN(s);
5105 if (ga_grow(&ga, 1) == FAIL)
5106 break;
5107 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5108 {
5109 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5110 if (*s == '\n' || vim_ispathsep(*s))
5111 break;
5112 ++s;
5113 ((char_u **)ga.ga_data)[ga.ga_len] =
5114 vim_strnsave(s, (int)(e - s - 4));
5115 ++ga.ga_len;
5116 }
5117 if (*e != NUL)
5118 ++e;
5119 }
5120 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005122 if (ga.ga_len == 0)
5123 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005124
5125 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005126 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005127 remove_duplicates(&ga);
5128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 *file = ga.ga_data;
5130 *num_file = ga.ga_len;
5131 return OK;
5132}
5133
5134#endif
5135
5136#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5137/*
5138 * Expand "file" for all comma-separated directories in "path".
5139 * Returns an allocated string with all matches concatenated, separated by
5140 * newlines. Returns NULL for an error or no matches.
5141 */
5142 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005143globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 char_u *path;
5145 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005146 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147{
5148 expand_T xpc;
5149 char_u *buf;
5150 garray_T ga;
5151 int i;
5152 int len;
5153 int num_p;
5154 char_u **p;
5155 char_u *cur = NULL;
5156
5157 buf = alloc(MAXPATHL);
5158 if (buf == NULL)
5159 return NULL;
5160
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005161 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005163
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 ga_init2(&ga, 1, 100);
5165
5166 /* Loop over all entries in {path}. */
5167 while (*path != NUL)
5168 {
5169 /* Copy one item of the path to buf[] and concatenate the file name. */
5170 copy_option_part(&path, buf, MAXPATHL, ",");
5171 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5172 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005173# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005174 /* Using the platform's path separator (\) makes vim incorrectly
5175 * treat it as an escape character, use '/' instead. */
5176 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5177 STRCAT(buf, "/");
5178# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005182 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5183 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005185 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005187 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189 /* Concatenate new results to previous ones. */
5190 if (ga_grow(&ga, len) == OK)
5191 {
5192 cur = (char_u *)ga.ga_data + ga.ga_len;
5193 for (i = 0; i < num_p; ++i)
5194 {
5195 STRCPY(cur, p[i]);
5196 cur += STRLEN(p[i]);
5197 *cur++ = '\n';
5198 }
5199 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 }
5201 FreeWild(num_p, p);
5202 }
5203 }
5204 }
5205 if (cur != NULL)
5206 *--cur = 0; /* Replace trailing newline with NUL */
5207
5208 vim_free(buf);
5209 return (char_u *)ga.ga_data;
5210}
5211
5212#endif
5213
5214#if defined(FEAT_CMDHIST) || defined(PROTO)
5215
5216/*********************************
5217 * Command line history stuff *
5218 *********************************/
5219
5220/*
5221 * Translate a history character to the associated type number.
5222 */
5223 static int
5224hist_char2type(c)
5225 int c;
5226{
5227 if (c == ':')
5228 return HIST_CMD;
5229 if (c == '=')
5230 return HIST_EXPR;
5231 if (c == '@')
5232 return HIST_INPUT;
5233 if (c == '>')
5234 return HIST_DEBUG;
5235 return HIST_SEARCH; /* must be '?' or '/' */
5236}
5237
5238/*
5239 * Table of history names.
5240 * These names are used in :history and various hist...() functions.
5241 * It is sufficient to give the significant prefix of a history name.
5242 */
5243
5244static char *(history_names[]) =
5245{
5246 "cmd",
5247 "search",
5248 "expr",
5249 "input",
5250 "debug",
5251 NULL
5252};
5253
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005254#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5255/*
5256 * Function given to ExpandGeneric() to obtain the possible first
5257 * arguments of the ":history command.
5258 */
5259 static char_u *
5260get_history_arg(xp, idx)
5261 expand_T *xp UNUSED;
5262 int idx;
5263{
5264 static char_u compl[2] = { NUL, NUL };
5265 char *short_names = ":=@>?/";
5266 int short_names_count = STRLEN(short_names);
5267 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5268
5269 if (idx < short_names_count)
5270 {
5271 compl[0] = (char_u)short_names[idx];
5272 return compl;
5273 }
5274 if (idx < short_names_count + history_name_count)
5275 return (char_u *)history_names[idx - short_names_count];
5276 if (idx == short_names_count + history_name_count)
5277 return (char_u *)"all";
5278 return NULL;
5279}
5280#endif
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282/*
5283 * init_history() - Initialize the command line history.
5284 * Also used to re-allocate the history when the size changes.
5285 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005286 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287init_history()
5288{
5289 int newlen; /* new length of history table */
5290 histentry_T *temp;
5291 int i;
5292 int j;
5293 int type;
5294
5295 /*
5296 * If size of history table changed, reallocate it
5297 */
5298 newlen = (int)p_hi;
5299 if (newlen != hislen) /* history length changed */
5300 {
5301 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5302 {
5303 if (newlen)
5304 {
5305 temp = (histentry_T *)lalloc(
5306 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5307 if (temp == NULL) /* out of memory! */
5308 {
5309 if (type == 0) /* first one: just keep the old length */
5310 {
5311 newlen = hislen;
5312 break;
5313 }
5314 /* Already changed one table, now we can only have zero
5315 * length for all tables. */
5316 newlen = 0;
5317 type = -1;
5318 continue;
5319 }
5320 }
5321 else
5322 temp = NULL;
5323 if (newlen == 0 || temp != NULL)
5324 {
5325 if (hisidx[type] < 0) /* there are no entries yet */
5326 {
5327 for (i = 0; i < newlen; ++i)
5328 {
5329 temp[i].hisnum = 0;
5330 temp[i].hisstr = NULL;
5331 }
5332 }
5333 else if (newlen > hislen) /* array becomes bigger */
5334 {
5335 for (i = 0; i <= hisidx[type]; ++i)
5336 temp[i] = history[type][i];
5337 j = i;
5338 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5339 {
5340 temp[i].hisnum = 0;
5341 temp[i].hisstr = NULL;
5342 }
5343 for ( ; j < hislen; ++i, ++j)
5344 temp[i] = history[type][j];
5345 }
5346 else /* array becomes smaller or 0 */
5347 {
5348 j = hisidx[type];
5349 for (i = newlen - 1; ; --i)
5350 {
5351 if (i >= 0) /* copy newest entries */
5352 temp[i] = history[type][j];
5353 else /* remove older entries */
5354 vim_free(history[type][j].hisstr);
5355 if (--j < 0)
5356 j = hislen - 1;
5357 if (j == hisidx[type])
5358 break;
5359 }
5360 hisidx[type] = newlen - 1;
5361 }
5362 vim_free(history[type]);
5363 history[type] = temp;
5364 }
5365 }
5366 hislen = newlen;
5367 }
5368}
5369
5370/*
5371 * Check if command line 'str' is already in history.
5372 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5373 */
5374 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005375in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 int type;
5377 char_u *str;
5378 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005379 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
5381 int i;
5382 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005383 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
5385 if (hisidx[type] < 0)
5386 return FALSE;
5387 i = hisidx[type];
5388 do
5389 {
5390 if (history[type][i].hisstr == NULL)
5391 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005392
5393 /* For search history, check that the separator character matches as
5394 * well. */
5395 p = history[type][i].hisstr;
5396 if (STRCMP(str, p) == 0
5397 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 {
5399 if (!move_to_front)
5400 return TRUE;
5401 last_i = i;
5402 break;
5403 }
5404 if (--i < 0)
5405 i = hislen - 1;
5406 } while (i != hisidx[type]);
5407
5408 if (last_i >= 0)
5409 {
5410 str = history[type][i].hisstr;
5411 while (i != hisidx[type])
5412 {
5413 if (++i >= hislen)
5414 i = 0;
5415 history[type][last_i] = history[type][i];
5416 last_i = i;
5417 }
5418 history[type][i].hisstr = str;
5419 history[type][i].hisnum = ++hisnum[type];
5420 return TRUE;
5421 }
5422 return FALSE;
5423}
5424
5425/*
5426 * Convert history name (from table above) to its HIST_ equivalent.
5427 * When "name" is empty, return "cmd" history.
5428 * Returns -1 for unknown history name.
5429 */
5430 int
5431get_histtype(name)
5432 char_u *name;
5433{
5434 int i;
5435 int len = (int)STRLEN(name);
5436
5437 /* No argument: use current history. */
5438 if (len == 0)
5439 return hist_char2type(ccline.cmdfirstc);
5440
5441 for (i = 0; history_names[i] != NULL; ++i)
5442 if (STRNICMP(name, history_names[i], len) == 0)
5443 return i;
5444
5445 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5446 return hist_char2type(name[0]);
5447
5448 return -1;
5449}
5450
5451static int last_maptick = -1; /* last seen maptick */
5452
5453/*
5454 * Add the given string to the given history. If the string is already in the
5455 * history then it is moved to the front. "histype" may be one of he HIST_
5456 * values.
5457 */
5458 void
5459add_to_history(histype, new_entry, in_map, sep)
5460 int histype;
5461 char_u *new_entry;
5462 int in_map; /* consider maptick when inside a mapping */
5463 int sep; /* separator character used (search hist) */
5464{
5465 histentry_T *hisptr;
5466 int len;
5467
5468 if (hislen == 0) /* no history */
5469 return;
5470
5471 /*
5472 * Searches inside the same mapping overwrite each other, so that only
5473 * the last line is kept. Be careful not to remove a line that was moved
5474 * down, only lines that were added.
5475 */
5476 if (histype == HIST_SEARCH && in_map)
5477 {
5478 if (maptick == last_maptick)
5479 {
5480 /* Current line is from the same mapping, remove it */
5481 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5482 vim_free(hisptr->hisstr);
5483 hisptr->hisstr = NULL;
5484 hisptr->hisnum = 0;
5485 --hisnum[histype];
5486 if (--hisidx[HIST_SEARCH] < 0)
5487 hisidx[HIST_SEARCH] = hislen - 1;
5488 }
5489 last_maptick = -1;
5490 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005491 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 {
5493 if (++hisidx[histype] == hislen)
5494 hisidx[histype] = 0;
5495 hisptr = &history[histype][hisidx[histype]];
5496 vim_free(hisptr->hisstr);
5497
5498 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005499 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5501 if (hisptr->hisstr != NULL)
5502 hisptr->hisstr[len + 1] = sep;
5503
5504 hisptr->hisnum = ++hisnum[histype];
5505 if (histype == HIST_SEARCH && in_map)
5506 last_maptick = maptick;
5507 }
5508}
5509
5510#if defined(FEAT_EVAL) || defined(PROTO)
5511
5512/*
5513 * Get identifier of newest history entry.
5514 * "histype" may be one of the HIST_ values.
5515 */
5516 int
5517get_history_idx(histype)
5518 int histype;
5519{
5520 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5521 || hisidx[histype] < 0)
5522 return -1;
5523
5524 return history[histype][hisidx[histype]].hisnum;
5525}
5526
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005527static struct cmdline_info *get_ccline_ptr __ARGS((void));
5528
5529/*
5530 * Get pointer to the command line info to use. cmdline_paste() may clear
5531 * ccline and put the previous value in prev_ccline.
5532 */
5533 static struct cmdline_info *
5534get_ccline_ptr()
5535{
5536 if ((State & CMDLINE) == 0)
5537 return NULL;
5538 if (ccline.cmdbuff != NULL)
5539 return &ccline;
5540 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5541 return &prev_ccline;
5542 return NULL;
5543}
5544
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545/*
5546 * Get the current command line in allocated memory.
5547 * Only works when the command line is being edited.
5548 * Returns NULL when something is wrong.
5549 */
5550 char_u *
5551get_cmdline_str()
5552{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005553 struct cmdline_info *p = get_ccline_ptr();
5554
5555 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005557 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558}
5559
5560/*
5561 * Get the current command line position, counted in bytes.
5562 * Zero is the first position.
5563 * Only works when the command line is being edited.
5564 * Returns -1 when something is wrong.
5565 */
5566 int
5567get_cmdline_pos()
5568{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005569 struct cmdline_info *p = get_ccline_ptr();
5570
5571 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005573 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574}
5575
5576/*
5577 * Set the command line byte position to "pos". Zero is the first position.
5578 * Only works when the command line is being edited.
5579 * Returns 1 when failed, 0 when OK.
5580 */
5581 int
5582set_cmdline_pos(pos)
5583 int pos;
5584{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005585 struct cmdline_info *p = get_ccline_ptr();
5586
5587 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 return 1;
5589
5590 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5591 * changed the command line. */
5592 if (pos < 0)
5593 new_cmdpos = 0;
5594 else
5595 new_cmdpos = pos;
5596 return 0;
5597}
5598
5599/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005600 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005601 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005602 * Only works when the command line is being edited.
5603 * Returns NUL when something is wrong.
5604 */
5605 int
5606get_cmdline_type()
5607{
5608 struct cmdline_info *p = get_ccline_ptr();
5609
5610 if (p == NULL)
5611 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005612 if (p->cmdfirstc == NUL)
5613 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005614 return p->cmdfirstc;
5615}
5616
5617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 * Calculate history index from a number:
5619 * num > 0: seen as identifying number of a history entry
5620 * num < 0: relative position in history wrt newest entry
5621 * "histype" may be one of the HIST_ values.
5622 */
5623 static int
5624calc_hist_idx(histype, num)
5625 int histype;
5626 int num;
5627{
5628 int i;
5629 histentry_T *hist;
5630 int wrapped = FALSE;
5631
5632 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5633 || (i = hisidx[histype]) < 0 || num == 0)
5634 return -1;
5635
5636 hist = history[histype];
5637 if (num > 0)
5638 {
5639 while (hist[i].hisnum > num)
5640 if (--i < 0)
5641 {
5642 if (wrapped)
5643 break;
5644 i += hislen;
5645 wrapped = TRUE;
5646 }
5647 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5648 return i;
5649 }
5650 else if (-num <= hislen)
5651 {
5652 i += num + 1;
5653 if (i < 0)
5654 i += hislen;
5655 if (hist[i].hisstr != NULL)
5656 return i;
5657 }
5658 return -1;
5659}
5660
5661/*
5662 * Get a history entry by its index.
5663 * "histype" may be one of the HIST_ values.
5664 */
5665 char_u *
5666get_history_entry(histype, idx)
5667 int histype;
5668 int idx;
5669{
5670 idx = calc_hist_idx(histype, idx);
5671 if (idx >= 0)
5672 return history[histype][idx].hisstr;
5673 else
5674 return (char_u *)"";
5675}
5676
5677/*
5678 * Clear all entries of a history.
5679 * "histype" may be one of the HIST_ values.
5680 */
5681 int
5682clr_history(histype)
5683 int histype;
5684{
5685 int i;
5686 histentry_T *hisptr;
5687
5688 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5689 {
5690 hisptr = history[histype];
5691 for (i = hislen; i--;)
5692 {
5693 vim_free(hisptr->hisstr);
5694 hisptr->hisnum = 0;
5695 hisptr++->hisstr = NULL;
5696 }
5697 hisidx[histype] = -1; /* mark history as cleared */
5698 hisnum[histype] = 0; /* reset identifier counter */
5699 return OK;
5700 }
5701 return FAIL;
5702}
5703
5704/*
5705 * Remove all entries matching {str} from a history.
5706 * "histype" may be one of the HIST_ values.
5707 */
5708 int
5709del_history_entry(histype, str)
5710 int histype;
5711 char_u *str;
5712{
5713 regmatch_T regmatch;
5714 histentry_T *hisptr;
5715 int idx;
5716 int i;
5717 int last;
5718 int found = FALSE;
5719
5720 regmatch.regprog = NULL;
5721 regmatch.rm_ic = FALSE; /* always match case */
5722 if (hislen != 0
5723 && histype >= 0
5724 && histype < HIST_COUNT
5725 && *str != NUL
5726 && (idx = hisidx[histype]) >= 0
5727 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5728 != NULL)
5729 {
5730 i = last = idx;
5731 do
5732 {
5733 hisptr = &history[histype][i];
5734 if (hisptr->hisstr == NULL)
5735 break;
5736 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5737 {
5738 found = TRUE;
5739 vim_free(hisptr->hisstr);
5740 hisptr->hisstr = NULL;
5741 hisptr->hisnum = 0;
5742 }
5743 else
5744 {
5745 if (i != last)
5746 {
5747 history[histype][last] = *hisptr;
5748 hisptr->hisstr = NULL;
5749 hisptr->hisnum = 0;
5750 }
5751 if (--last < 0)
5752 last += hislen;
5753 }
5754 if (--i < 0)
5755 i += hislen;
5756 } while (i != idx);
5757 if (history[histype][idx].hisstr == NULL)
5758 hisidx[histype] = -1;
5759 }
5760 vim_free(regmatch.regprog);
5761 return found;
5762}
5763
5764/*
5765 * Remove an indexed entry from a history.
5766 * "histype" may be one of the HIST_ values.
5767 */
5768 int
5769del_history_idx(histype, idx)
5770 int histype;
5771 int idx;
5772{
5773 int i, j;
5774
5775 i = calc_hist_idx(histype, idx);
5776 if (i < 0)
5777 return FALSE;
5778 idx = hisidx[histype];
5779 vim_free(history[histype][i].hisstr);
5780
5781 /* When deleting the last added search string in a mapping, reset
5782 * last_maptick, so that the last added search string isn't deleted again.
5783 */
5784 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5785 last_maptick = -1;
5786
5787 while (i != idx)
5788 {
5789 j = (i + 1) % hislen;
5790 history[histype][i] = history[histype][j];
5791 i = j;
5792 }
5793 history[histype][i].hisstr = NULL;
5794 history[histype][i].hisnum = 0;
5795 if (--i < 0)
5796 i += hislen;
5797 hisidx[histype] = i;
5798 return TRUE;
5799}
5800
5801#endif /* FEAT_EVAL */
5802
5803#if defined(FEAT_CRYPT) || defined(PROTO)
5804/*
5805 * Very specific function to remove the value in ":set key=val" from the
5806 * history.
5807 */
5808 void
5809remove_key_from_history()
5810{
5811 char_u *p;
5812 int i;
5813
5814 i = hisidx[HIST_CMD];
5815 if (i < 0)
5816 return;
5817 p = history[HIST_CMD][i].hisstr;
5818 if (p != NULL)
5819 for ( ; *p; ++p)
5820 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5821 {
5822 p = vim_strchr(p + 3, '=');
5823 if (p == NULL)
5824 break;
5825 ++p;
5826 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5827 if (p[i] == '\\' && p[i + 1])
5828 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005829 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 --p;
5831 }
5832}
5833#endif
5834
5835#endif /* FEAT_CMDHIST */
5836
5837#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5838/*
5839 * Get indices "num1,num2" that specify a range within a list (not a range of
5840 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5841 * Returns OK if parsed successfully, otherwise FAIL.
5842 */
5843 int
5844get_list_range(str, num1, num2)
5845 char_u **str;
5846 int *num1;
5847 int *num2;
5848{
5849 int len;
5850 int first = FALSE;
5851 long num;
5852
5853 *str = skipwhite(*str);
5854 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5855 {
5856 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5857 *str += len;
5858 *num1 = (int)num;
5859 first = TRUE;
5860 }
5861 *str = skipwhite(*str);
5862 if (**str == ',') /* parse "to" part of range */
5863 {
5864 *str = skipwhite(*str + 1);
5865 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5866 if (len > 0)
5867 {
5868 *num2 = (int)num;
5869 *str = skipwhite(*str + len);
5870 }
5871 else if (!first) /* no number given at all */
5872 return FAIL;
5873 }
5874 else if (first) /* only one number given */
5875 *num2 = *num1;
5876 return OK;
5877}
5878#endif
5879
5880#if defined(FEAT_CMDHIST) || defined(PROTO)
5881/*
5882 * :history command - print a history
5883 */
5884 void
5885ex_history(eap)
5886 exarg_T *eap;
5887{
5888 histentry_T *hist;
5889 int histype1 = HIST_CMD;
5890 int histype2 = HIST_CMD;
5891 int hisidx1 = 1;
5892 int hisidx2 = -1;
5893 int idx;
5894 int i, j, k;
5895 char_u *end;
5896 char_u *arg = eap->arg;
5897
5898 if (hislen == 0)
5899 {
5900 MSG(_("'history' option is zero"));
5901 return;
5902 }
5903
5904 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5905 {
5906 end = arg;
5907 while (ASCII_ISALPHA(*end)
5908 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5909 end++;
5910 i = *end;
5911 *end = NUL;
5912 histype1 = get_histtype(arg);
5913 if (histype1 == -1)
5914 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005915 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
5917 histype1 = 0;
5918 histype2 = HIST_COUNT-1;
5919 }
5920 else
5921 {
5922 *end = i;
5923 EMSG(_(e_trailing));
5924 return;
5925 }
5926 }
5927 else
5928 histype2 = histype1;
5929 *end = i;
5930 }
5931 else
5932 end = arg;
5933 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5934 {
5935 EMSG(_(e_trailing));
5936 return;
5937 }
5938
5939 for (; !got_int && histype1 <= histype2; ++histype1)
5940 {
5941 STRCPY(IObuff, "\n # ");
5942 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5943 MSG_PUTS_TITLE(IObuff);
5944 idx = hisidx[histype1];
5945 hist = history[histype1];
5946 j = hisidx1;
5947 k = hisidx2;
5948 if (j < 0)
5949 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5950 if (k < 0)
5951 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5952 if (idx >= 0 && j <= k)
5953 for (i = idx + 1; !got_int; ++i)
5954 {
5955 if (i == hislen)
5956 i = 0;
5957 if (hist[i].hisstr != NULL
5958 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5959 {
5960 msg_putchar('\n');
5961 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5962 hist[i].hisnum);
5963 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5964 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005965 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 else
5967 STRCAT(IObuff, hist[i].hisstr);
5968 msg_outtrans(IObuff);
5969 out_flush();
5970 }
5971 if (i == idx)
5972 break;
5973 }
5974 }
5975}
5976#endif
5977
5978#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5979static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5980static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5981static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5982static int viminfo_add_at_front = FALSE;
5983
5984static int hist_type2char __ARGS((int type, int use_question));
5985
5986/*
5987 * Translate a history type number to the associated character.
5988 */
5989 static int
5990hist_type2char(type, use_question)
5991 int type;
5992 int use_question; /* use '?' instead of '/' */
5993{
5994 if (type == HIST_CMD)
5995 return ':';
5996 if (type == HIST_SEARCH)
5997 {
5998 if (use_question)
5999 return '?';
6000 else
6001 return '/';
6002 }
6003 if (type == HIST_EXPR)
6004 return '=';
6005 return '@';
6006}
6007
6008/*
6009 * Prepare for reading the history from the viminfo file.
6010 * This allocates history arrays to store the read history lines.
6011 */
6012 void
6013prepare_viminfo_history(asklen)
6014 int asklen;
6015{
6016 int i;
6017 int num;
6018 int type;
6019 int len;
6020
6021 init_history();
6022 viminfo_add_at_front = (asklen != 0);
6023 if (asklen > hislen)
6024 asklen = hislen;
6025
6026 for (type = 0; type < HIST_COUNT; ++type)
6027 {
6028 /*
6029 * Count the number of empty spaces in the history list. If there are
6030 * more spaces available than we request, then fill them up.
6031 */
6032 for (i = 0, num = 0; i < hislen; i++)
6033 if (history[type][i].hisstr == NULL)
6034 num++;
6035 len = asklen;
6036 if (num > len)
6037 len = num;
6038 if (len <= 0)
6039 viminfo_history[type] = NULL;
6040 else
6041 viminfo_history[type] =
6042 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6043 if (viminfo_history[type] == NULL)
6044 len = 0;
6045 viminfo_hislen[type] = len;
6046 viminfo_hisidx[type] = 0;
6047 }
6048}
6049
6050/*
6051 * Accept a line from the viminfo, store it in the history array when it's
6052 * new.
6053 */
6054 int
6055read_viminfo_history(virp)
6056 vir_T *virp;
6057{
6058 int type;
6059 long_u len;
6060 char_u *val;
6061 char_u *p;
6062
6063 type = hist_char2type(virp->vir_line[0]);
6064 if (viminfo_hisidx[type] < viminfo_hislen[type])
6065 {
6066 val = viminfo_readstring(virp, 1, TRUE);
6067 if (val != NULL && *val != NUL)
6068 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006069 int sep = (*val == ' ' ? NUL : *val);
6070
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006072 viminfo_add_at_front, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 {
6074 /* Need to re-allocate to append the separator byte. */
6075 len = STRLEN(val);
6076 p = lalloc(len + 2, TRUE);
6077 if (p != NULL)
6078 {
6079 if (type == HIST_SEARCH)
6080 {
6081 /* Search entry: Move the separator from the first
6082 * column to after the NUL. */
6083 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006084 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 }
6086 else
6087 {
6088 /* Not a search entry: No separator in the viminfo
6089 * file, add a NUL separator. */
6090 mch_memmove(p, val, (size_t)len + 1);
6091 p[len + 1] = NUL;
6092 }
6093 viminfo_history[type][viminfo_hisidx[type]++] = p;
6094 }
6095 }
6096 }
6097 vim_free(val);
6098 }
6099 return viminfo_readline(virp);
6100}
6101
6102 void
6103finish_viminfo_history()
6104{
6105 int idx;
6106 int i;
6107 int type;
6108
6109 for (type = 0; type < HIST_COUNT; ++type)
6110 {
6111 if (history[type] == NULL)
6112 return;
6113 idx = hisidx[type] + viminfo_hisidx[type];
6114 if (idx >= hislen)
6115 idx -= hislen;
6116 else if (idx < 0)
6117 idx = hislen - 1;
6118 if (viminfo_add_at_front)
6119 hisidx[type] = idx;
6120 else
6121 {
6122 if (hisidx[type] == -1)
6123 hisidx[type] = hislen - 1;
6124 do
6125 {
6126 if (history[type][idx].hisstr != NULL)
6127 break;
6128 if (++idx == hislen)
6129 idx = 0;
6130 } while (idx != hisidx[type]);
6131 if (idx != hisidx[type] && --idx < 0)
6132 idx = hislen - 1;
6133 }
6134 for (i = 0; i < viminfo_hisidx[type]; i++)
6135 {
6136 vim_free(history[type][idx].hisstr);
6137 history[type][idx].hisstr = viminfo_history[type][i];
6138 if (--idx < 0)
6139 idx = hislen - 1;
6140 }
6141 idx += 1;
6142 idx %= hislen;
6143 for (i = 0; i < viminfo_hisidx[type]; i++)
6144 {
6145 history[type][idx++].hisnum = ++hisnum[type];
6146 idx %= hislen;
6147 }
6148 vim_free(viminfo_history[type]);
6149 viminfo_history[type] = NULL;
6150 }
6151}
6152
6153 void
6154write_viminfo_history(fp)
6155 FILE *fp;
6156{
6157 int i;
6158 int type;
6159 int num_saved;
6160 char_u *p;
6161 int c;
6162
6163 init_history();
6164 if (hislen == 0)
6165 return;
6166 for (type = 0; type < HIST_COUNT; ++type)
6167 {
6168 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6169 if (num_saved == 0)
6170 continue;
6171 if (num_saved < 0) /* Use default */
6172 num_saved = hislen;
6173 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6174 type == HIST_CMD ? _("Command Line") :
6175 type == HIST_SEARCH ? _("Search String") :
6176 type == HIST_EXPR ? _("Expression") :
6177 _("Input Line"));
6178 if (num_saved > hislen)
6179 num_saved = hislen;
6180 i = hisidx[type];
6181 if (i >= 0)
6182 while (num_saved--)
6183 {
6184 p = history[type][i].hisstr;
6185 if (p != NULL)
6186 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006187 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 /* For the search history: put the separator in the second
6189 * column; use a space if there isn't one. */
6190 if (type == HIST_SEARCH)
6191 {
6192 c = p[STRLEN(p) + 1];
6193 putc(c == NUL ? ' ' : c, fp);
6194 }
6195 viminfo_writestring(fp, p);
6196 }
6197 if (--i < 0)
6198 i = hislen - 1;
6199 }
6200 }
6201}
6202#endif /* FEAT_VIMINFO */
6203
6204#if defined(FEAT_FKMAP) || defined(PROTO)
6205/*
6206 * Write a character at the current cursor+offset position.
6207 * It is directly written into the command buffer block.
6208 */
6209 void
6210cmd_pchar(c, offset)
6211 int c, offset;
6212{
6213 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6214 {
6215 EMSG(_("E198: cmd_pchar beyond the command length"));
6216 return;
6217 }
6218 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6219 ccline.cmdbuff[ccline.cmdlen] = NUL;
6220}
6221
6222 int
6223cmd_gchar(offset)
6224 int offset;
6225{
6226 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6227 {
6228 /* EMSG(_("cmd_gchar beyond the command length")); */
6229 return NUL;
6230 }
6231 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6232}
6233#endif
6234
6235#if defined(FEAT_CMDWIN) || defined(PROTO)
6236/*
6237 * Open a window on the current command line and history. Allow editing in
6238 * the window. Returns when the window is closed.
6239 * Returns:
6240 * CR if the command is to be executed
6241 * Ctrl_C if it is to be abandoned
6242 * K_IGNORE if editing continues
6243 */
6244 static int
6245ex_window()
6246{
6247 struct cmdline_info save_ccline;
6248 buf_T *old_curbuf = curbuf;
6249 win_T *old_curwin = curwin;
6250 buf_T *bp;
6251 win_T *wp;
6252 int i;
6253 linenr_T lnum;
6254 int histtype;
6255 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006256#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 int save_restart_edit = restart_edit;
6260 int save_State = State;
6261 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006262#ifdef FEAT_RIGHTLEFT
6263 int save_cmdmsg_rl = cmdmsg_rl;
6264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265
6266 /* Can't do this recursively. Can't do it when typing a password. */
6267 if (cmdwin_type != 0
6268# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6269 || cmdline_star > 0
6270# endif
6271 )
6272 {
6273 beep_flush();
6274 return K_IGNORE;
6275 }
6276
6277 /* Save current window sizes. */
6278 win_size_save(&winsizes);
6279
6280# ifdef FEAT_AUTOCMD
6281 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006282 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006284 /* don't use a new tab page */
6285 cmdmod.tab = 0;
6286
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 /* Create a window for the command-line buffer. */
6288 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6289 {
6290 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006291# ifdef FEAT_AUTOCMD
6292 unblock_autocmds();
6293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 return K_IGNORE;
6295 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006296 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297
6298 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006299 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006300 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6302 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6303 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006304#ifdef FEAT_FOLDING
6305 curwin->w_p_fen = FALSE;
6306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006308 curwin->w_p_rl = cmdmsg_rl;
6309 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006311 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313# ifdef FEAT_AUTOCMD
6314 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006315 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316# endif
6317
Bram Moolenaar46152342005-09-07 21:18:43 +00006318 /* Showing the prompt may have set need_wait_return, reset it. */
6319 need_wait_return = FALSE;
6320
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006321 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6323 {
6324 if (p_wc == TAB)
6325 {
6326 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6327 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6328 }
6329 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6330 }
6331
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006332 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6333 * sets 'textwidth' to 78). */
6334 curbuf->b_p_tw = 0;
6335
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 /* Fill the buffer with the history. */
6337 init_history();
6338 if (hislen > 0)
6339 {
6340 i = hisidx[histtype];
6341 if (i >= 0)
6342 {
6343 lnum = 0;
6344 do
6345 {
6346 if (++i == hislen)
6347 i = 0;
6348 if (history[histtype][i].hisstr != NULL)
6349 ml_append(lnum++, history[histtype][i].hisstr,
6350 (colnr_T)0, FALSE);
6351 }
6352 while (i != hisidx[histtype]);
6353 }
6354 }
6355
6356 /* Replace the empty last line with the current command-line and put the
6357 * cursor there. */
6358 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6359 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6360 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006361 changed_line_abv_curs();
6362 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006363 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365 /* Save the command line info, can be used recursively. */
6366 save_ccline = ccline;
6367 ccline.cmdbuff = NULL;
6368 ccline.cmdprompt = NULL;
6369
6370 /* No Ex mode here! */
6371 exmode_active = 0;
6372
6373 State = NORMAL;
6374# ifdef FEAT_MOUSE
6375 setmouse();
6376# endif
6377
6378# ifdef FEAT_AUTOCMD
6379 /* Trigger CmdwinEnter autocommands. */
6380 typestr[0] = cmdwin_type;
6381 typestr[1] = NUL;
6382 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006383 if (restart_edit != 0) /* autocmd with ":startinsert" */
6384 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385# endif
6386
6387 i = RedrawingDisabled;
6388 RedrawingDisabled = 0;
6389
6390 /*
6391 * Call the main loop until <CR> or CTRL-C is typed.
6392 */
6393 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006394 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395
6396 RedrawingDisabled = i;
6397
6398# ifdef FEAT_AUTOCMD
6399 /* Trigger CmdwinLeave autocommands. */
6400 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6401# endif
6402
Bram Moolenaarccc18222007-05-10 18:25:20 +00006403 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 ccline = save_ccline;
6405 cmdwin_type = 0;
6406
6407 exmode_active = save_exmode;
6408
Bram Moolenaarf9821062008-06-20 16:31:07 +00006409 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 * this happens! */
6411 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6412 {
6413 cmdwin_result = Ctrl_C;
6414 EMSG(_("E199: Active window or buffer deleted"));
6415 }
6416 else
6417 {
6418# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6419 /* autocmds may abort script processing */
6420 if (aborting() && cmdwin_result != K_IGNORE)
6421 cmdwin_result = Ctrl_C;
6422# endif
6423 /* Set the new command line from the cmdline buffer. */
6424 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006425 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006427 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6428
6429 if (histtype == HIST_CMD)
6430 {
6431 /* Execute the command directly. */
6432 ccline.cmdbuff = vim_strsave((char_u *)p);
6433 cmdwin_result = CAR;
6434 }
6435 else
6436 {
6437 /* First need to cancel what we were doing. */
6438 ccline.cmdbuff = NULL;
6439 stuffcharReadbuff(':');
6440 stuffReadbuff((char_u *)p);
6441 stuffcharReadbuff(CAR);
6442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 }
6444 else if (cmdwin_result == K_XF2) /* :qa typed */
6445 {
6446 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6447 cmdwin_result = CAR;
6448 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006449 else if (cmdwin_result == Ctrl_C)
6450 {
6451 /* :q or :close, don't execute any command
6452 * and don't modify the cmd window. */
6453 ccline.cmdbuff = NULL;
6454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 else
6456 ccline.cmdbuff = vim_strsave(ml_get_curline());
6457 if (ccline.cmdbuff == NULL)
6458 cmdwin_result = Ctrl_C;
6459 else
6460 {
6461 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6462 ccline.cmdbufflen = ccline.cmdlen + 1;
6463 ccline.cmdpos = curwin->w_cursor.col;
6464 if (ccline.cmdpos > ccline.cmdlen)
6465 ccline.cmdpos = ccline.cmdlen;
6466 if (cmdwin_result == K_IGNORE)
6467 {
6468 set_cmdspos_cursor();
6469 redrawcmd();
6470 }
6471 }
6472
6473# ifdef FEAT_AUTOCMD
6474 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006475 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476# endif
6477 wp = curwin;
6478 bp = curbuf;
6479 win_goto(old_curwin);
6480 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006481
6482 /* win_close() may have already wiped the buffer when 'bh' is
6483 * set to 'wipe' */
6484 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006485 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
6487 /* Restore window sizes. */
6488 win_size_restore(&winsizes);
6489
6490# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006491 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492# endif
6493 }
6494
6495 ga_clear(&winsizes);
6496 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006497# ifdef FEAT_RIGHTLEFT
6498 cmdmsg_rl = save_cmdmsg_rl;
6499# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
6501 State = save_State;
6502# ifdef FEAT_MOUSE
6503 setmouse();
6504# endif
6505
6506 return cmdwin_result;
6507}
6508#endif /* FEAT_CMDWIN */
6509
6510/*
6511 * Used for commands that either take a simple command string argument, or:
6512 * cmd << endmarker
6513 * {script}
6514 * endmarker
6515 * Returns a pointer to allocated memory with {script} or NULL.
6516 */
6517 char_u *
6518script_get(eap, cmd)
6519 exarg_T *eap;
6520 char_u *cmd;
6521{
6522 char_u *theline;
6523 char *end_pattern = NULL;
6524 char dot[] = ".";
6525 garray_T ga;
6526
6527 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6528 return NULL;
6529
6530 ga_init2(&ga, 1, 0x400);
6531
6532 if (cmd[2] != NUL)
6533 end_pattern = (char *)skipwhite(cmd + 2);
6534 else
6535 end_pattern = dot;
6536
6537 for (;;)
6538 {
6539 theline = eap->getline(
6540#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006541 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542#endif
6543 NUL, eap->cookie, 0);
6544
6545 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006546 {
6547 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550
6551 ga_concat(&ga, theline);
6552 ga_append(&ga, '\n');
6553 vim_free(theline);
6554 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006555 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557 return (char_u *)ga.ga_data;
6558}