blob: 0c7dd8036384dcf014f0599d1717771530c8a746 [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(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002767#ifdef FEAT_MBYTE
2768 else if (has_mbyte)
2769 draw_cmdline(ccline.cmdpos,
2770 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 else
2773 draw_cmdline(ccline.cmdpos, 1);
2774 msg_no_more = FALSE;
2775 cursorcmd();
2776}
2777
2778/*
2779 * Put the given string, of the given length, onto the command line.
2780 * If len is -1, then STRLEN() is used to calculate the length.
2781 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2782 * part will be redrawn, otherwise it will not. If this function is called
2783 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2784 * called afterwards.
2785 */
2786 int
2787put_on_cmdline(str, len, redraw)
2788 char_u *str;
2789 int len;
2790 int redraw;
2791{
2792 int retval;
2793 int i;
2794 int m;
2795 int c;
2796
2797 if (len < 0)
2798 len = (int)STRLEN(str);
2799
2800 /* Check if ccline.cmdbuff needs to be longer */
2801 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002802 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 else
2804 retval = OK;
2805 if (retval == OK)
2806 {
2807 if (!ccline.overstrike)
2808 {
2809 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2810 ccline.cmdbuff + ccline.cmdpos,
2811 (size_t)(ccline.cmdlen - ccline.cmdpos));
2812 ccline.cmdlen += len;
2813 }
2814 else
2815 {
2816#ifdef FEAT_MBYTE
2817 if (has_mbyte)
2818 {
2819 /* Count nr of characters in the new string. */
2820 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002821 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 ++m;
2823 /* Count nr of bytes in cmdline that are overwritten by these
2824 * characters. */
2825 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002826 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 --m;
2828 if (i < ccline.cmdlen)
2829 {
2830 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2831 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2832 ccline.cmdlen += ccline.cmdpos + len - i;
2833 }
2834 else
2835 ccline.cmdlen = ccline.cmdpos + len;
2836 }
2837 else
2838#endif
2839 if (ccline.cmdpos + len > ccline.cmdlen)
2840 ccline.cmdlen = ccline.cmdpos + len;
2841 }
2842 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2843 ccline.cmdbuff[ccline.cmdlen] = NUL;
2844
2845#ifdef FEAT_MBYTE
2846 if (enc_utf8)
2847 {
2848 /* When the inserted text starts with a composing character,
2849 * backup to the character before it. There could be two of them.
2850 */
2851 i = 0;
2852 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2853 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2854 {
2855 i = (*mb_head_off)(ccline.cmdbuff,
2856 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2857 ccline.cmdpos -= i;
2858 len += i;
2859 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2860 }
2861# ifdef FEAT_ARABIC
2862 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2863 {
2864 /* Check the previous character for Arabic combining pair. */
2865 i = (*mb_head_off)(ccline.cmdbuff,
2866 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2867 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2868 + ccline.cmdpos - i), c))
2869 {
2870 ccline.cmdpos -= i;
2871 len += i;
2872 }
2873 else
2874 i = 0;
2875 }
2876# endif
2877 if (i != 0)
2878 {
2879 /* Also backup the cursor position. */
2880 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2881 ccline.cmdspos -= i;
2882 msg_col -= i;
2883 if (msg_col < 0)
2884 {
2885 msg_col += Columns;
2886 --msg_row;
2887 }
2888 }
2889 }
2890#endif
2891
2892 if (redraw && !cmd_silent)
2893 {
2894 msg_no_more = TRUE;
2895 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002896 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2898 /* Avoid clearing the rest of the line too often. */
2899 if (cmdline_row != i || ccline.overstrike)
2900 msg_clr_eos();
2901 msg_no_more = FALSE;
2902 }
2903#ifdef FEAT_FKMAP
2904 /*
2905 * If we are in Farsi command mode, the character input must be in
2906 * Insert mode. So do not advance the cmdpos.
2907 */
2908 if (!cmd_fkmap)
2909#endif
2910 {
2911 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002914 if (m < 0) /* overflow, Columns or Rows at weird value */
2915 m = MAXCOL;
2916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 else
2918 m = MAXCOL;
2919 for (i = 0; i < len; ++i)
2920 {
2921 c = cmdline_charsize(ccline.cmdpos);
2922#ifdef FEAT_MBYTE
2923 /* count ">" for a double-wide char that doesn't fit. */
2924 if (has_mbyte)
2925 correct_cmdspos(ccline.cmdpos, c);
2926#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002927 /* Stop cursor at the end of the screen, but do increment the
2928 * insert position, so that entering a very long command
2929 * works, even though you can't see it. */
2930 if (ccline.cmdspos + c < m)
2931 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932#ifdef FEAT_MBYTE
2933 if (has_mbyte)
2934 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002935 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 if (c > len - i - 1)
2937 c = len - i - 1;
2938 ccline.cmdpos += c;
2939 i += c;
2940 }
2941#endif
2942 ++ccline.cmdpos;
2943 }
2944 }
2945 }
2946 if (redraw)
2947 msg_check();
2948 return retval;
2949}
2950
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002951static struct cmdline_info prev_ccline;
2952static int prev_ccline_used = FALSE;
2953
2954/*
2955 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2956 * and overwrite it. But get_cmdline_str() may need it, thus make it
2957 * available globally in prev_ccline.
2958 */
2959 static void
2960save_cmdline(ccp)
2961 struct cmdline_info *ccp;
2962{
2963 if (!prev_ccline_used)
2964 {
2965 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2966 prev_ccline_used = TRUE;
2967 }
2968 *ccp = prev_ccline;
2969 prev_ccline = ccline;
2970 ccline.cmdbuff = NULL;
2971 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002972 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002973}
2974
2975/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002976 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002977 */
2978 static void
2979restore_cmdline(ccp)
2980 struct cmdline_info *ccp;
2981{
2982 ccline = prev_ccline;
2983 prev_ccline = *ccp;
2984}
2985
Bram Moolenaar5a305422006-04-28 22:38:25 +00002986#if defined(FEAT_EVAL) || defined(PROTO)
2987/*
2988 * Save the command line into allocated memory. Returns a pointer to be
2989 * passed to restore_cmdline_alloc() later.
2990 * Returns NULL when failed.
2991 */
2992 char_u *
2993save_cmdline_alloc()
2994{
2995 struct cmdline_info *p;
2996
2997 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2998 if (p != NULL)
2999 save_cmdline(p);
3000 return (char_u *)p;
3001}
3002
3003/*
3004 * Restore the command line from the return value of save_cmdline_alloc().
3005 */
3006 void
3007restore_cmdline_alloc(p)
3008 char_u *p;
3009{
3010 if (p != NULL)
3011 {
3012 restore_cmdline((struct cmdline_info *)p);
3013 vim_free(p);
3014 }
3015}
3016#endif
3017
Bram Moolenaar8299df92004-07-10 09:47:34 +00003018/*
3019 * paste a yank register into the command line.
3020 * used by CTRL-R command in command-line mode
3021 * insert_reg() can't be used here, because special characters from the
3022 * register contents will be interpreted as commands.
3023 *
3024 * return FAIL for failure, OK otherwise
3025 */
3026 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003027cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003028 int regname;
3029 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003030 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003031{
3032 long i;
3033 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003034 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003035 int allocated;
3036 struct cmdline_info save_ccline;
3037
3038 /* check for valid regname; also accept special characters for CTRL-R in
3039 * the command line */
3040 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3041 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3042 return FAIL;
3043
3044 /* A register containing CTRL-R can cause an endless loop. Allow using
3045 * CTRL-C to break the loop. */
3046 line_breakcheck();
3047 if (got_int)
3048 return FAIL;
3049
3050#ifdef FEAT_CLIPBOARD
3051 regname = may_get_selection(regname);
3052#endif
3053
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003054 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003055 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003056 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003057 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003058 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003059 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003060 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003061
3062 if (i)
3063 {
3064 /* Got the value of a special register in "arg". */
3065 if (arg == NULL)
3066 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003067
3068 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3069 * part of the word. */
3070 p = arg;
3071 if (p_is && regname == Ctrl_W)
3072 {
3073 char_u *w;
3074 int len;
3075
3076 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003077 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003078 {
3079#ifdef FEAT_MBYTE
3080 if (has_mbyte)
3081 {
3082 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3083 if (!vim_iswordc(mb_ptr2char(w - len)))
3084 break;
3085 w -= len;
3086 }
3087 else
3088#endif
3089 {
3090 if (!vim_iswordc(w[-1]))
3091 break;
3092 --w;
3093 }
3094 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003095 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003096 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3097 p += len;
3098 }
3099
3100 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003101 if (allocated)
3102 vim_free(arg);
3103 return OK;
3104 }
3105
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003106 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003107}
3108
3109/*
3110 * Put a string on the command line.
3111 * When "literally" is TRUE, insert literally.
3112 * When "literally" is FALSE, insert as typed, but don't leave the command
3113 * line.
3114 */
3115 void
3116cmdline_paste_str(s, literally)
3117 char_u *s;
3118 int literally;
3119{
3120 int c, cv;
3121
3122 if (literally)
3123 put_on_cmdline(s, -1, TRUE);
3124 else
3125 while (*s != NUL)
3126 {
3127 cv = *s;
3128 if (cv == Ctrl_V && s[1])
3129 ++s;
3130#ifdef FEAT_MBYTE
3131 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003132 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003133 else
3134#endif
3135 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003136 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3137 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003138#ifdef UNIX
3139 || c == intr_char
3140#endif
3141 || (c == Ctrl_BSL && *s == Ctrl_N))
3142 stuffcharReadbuff(Ctrl_V);
3143 stuffcharReadbuff(c);
3144 }
3145}
3146
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147#ifdef FEAT_WILDMENU
3148/*
3149 * Delete characters on the command line, from "from" to the current
3150 * position.
3151 */
3152 static void
3153cmdline_del(from)
3154 int from;
3155{
3156 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3157 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3158 ccline.cmdlen -= ccline.cmdpos - from;
3159 ccline.cmdpos = from;
3160}
3161#endif
3162
3163/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003164 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 * search
3166 */
3167 void
3168redrawcmdline()
3169{
3170 if (cmd_silent)
3171 return;
3172 need_wait_return = FALSE;
3173 compute_cmdrow();
3174 redrawcmd();
3175 cursorcmd();
3176}
3177
3178 static void
3179redrawcmdprompt()
3180{
3181 int i;
3182
3183 if (cmd_silent)
3184 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003185 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 msg_putchar(ccline.cmdfirstc);
3187 if (ccline.cmdprompt != NULL)
3188 {
3189 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3190 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3191 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003192 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 --ccline.cmdindent;
3194 }
3195 else
3196 for (i = ccline.cmdindent; i > 0; --i)
3197 msg_putchar(' ');
3198}
3199
3200/*
3201 * Redraw what is currently on the command line.
3202 */
3203 void
3204redrawcmd()
3205{
3206 if (cmd_silent)
3207 return;
3208
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003209 /* when 'incsearch' is set there may be no command line while redrawing */
3210 if (ccline.cmdbuff == NULL)
3211 {
3212 windgoto(cmdline_row, 0);
3213 msg_clr_eos();
3214 return;
3215 }
3216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 msg_start();
3218 redrawcmdprompt();
3219
3220 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3221 msg_no_more = TRUE;
3222 draw_cmdline(0, ccline.cmdlen);
3223 msg_clr_eos();
3224 msg_no_more = FALSE;
3225
3226 set_cmdspos_cursor();
3227
3228 /*
3229 * An emsg() before may have set msg_scroll. This is used in normal mode,
3230 * in cmdline mode we can reset them now.
3231 */
3232 msg_scroll = FALSE; /* next message overwrites cmdline */
3233
3234 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3235 * in cmdline mode */
3236 skip_redraw = FALSE;
3237}
3238
3239 void
3240compute_cmdrow()
3241{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003242 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 cmdline_row = Rows - 1;
3244 else
3245 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3246 + W_STATUS_HEIGHT(lastwin);
3247}
3248
3249 static void
3250cursorcmd()
3251{
3252 if (cmd_silent)
3253 return;
3254
3255#ifdef FEAT_RIGHTLEFT
3256 if (cmdmsg_rl)
3257 {
3258 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3259 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3260 if (msg_row <= 0)
3261 msg_row = Rows - 1;
3262 }
3263 else
3264#endif
3265 {
3266 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3267 msg_col = ccline.cmdspos % (int)Columns;
3268 if (msg_row >= Rows)
3269 msg_row = Rows - 1;
3270 }
3271
3272 windgoto(msg_row, msg_col);
3273#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3274 redrawcmd_preedit();
3275#endif
3276#ifdef MCH_CURSOR_SHAPE
3277 mch_update_cursor();
3278#endif
3279}
3280
3281 void
3282gotocmdline(clr)
3283 int clr;
3284{
3285 msg_start();
3286#ifdef FEAT_RIGHTLEFT
3287 if (cmdmsg_rl)
3288 msg_col = Columns - 1;
3289 else
3290#endif
3291 msg_col = 0; /* always start in column 0 */
3292 if (clr) /* clear the bottom line(s) */
3293 msg_clr_eos(); /* will reset clear_cmdline */
3294 windgoto(cmdline_row, 0);
3295}
3296
3297/*
3298 * Check the word in front of the cursor for an abbreviation.
3299 * Called when the non-id character "c" has been entered.
3300 * When an abbreviation is recognized it is removed from the text with
3301 * backspaces and the replacement string is inserted, followed by "c".
3302 */
3303 static int
3304ccheck_abbr(c)
3305 int c;
3306{
3307 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3308 return FALSE;
3309
3310 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3311}
3312
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003313#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3314 static int
3315#ifdef __BORLANDC__
3316_RTLENTRYF
3317#endif
3318sort_func_compare(s1, s2)
3319 const void *s1;
3320 const void *s2;
3321{
3322 char_u *p1 = *(char_u **)s1;
3323 char_u *p2 = *(char_u **)s2;
3324
3325 if (*p1 != '<' && *p2 == '<') return -1;
3326 if (*p1 == '<' && *p2 != '<') return 1;
3327 return STRCMP(p1, p2);
3328}
3329#endif
3330
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331/*
3332 * Return FAIL if this is not an appropriate context in which to do
3333 * completion of anything, return OK if it is (even if there are no matches).
3334 * For the caller, this means that the character is just passed through like a
3335 * normal character (instead of being expanded). This allows :s/^I^D etc.
3336 */
3337 static int
3338nextwild(xp, type, options)
3339 expand_T *xp;
3340 int type;
3341 int options; /* extra options for ExpandOne() */
3342{
3343 int i, j;
3344 char_u *p1;
3345 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 int difflen;
3347 int v;
3348
3349 if (xp->xp_numfiles == -1)
3350 {
3351 set_expand_context(xp);
3352 cmd_showtail = expand_showtail(xp);
3353 }
3354
3355 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3356 {
3357 beep_flush();
3358 return OK; /* Something illegal on command line */
3359 }
3360 if (xp->xp_context == EXPAND_NOTHING)
3361 {
3362 /* Caller can use the character as a normal char instead */
3363 return FAIL;
3364 }
3365
3366 MSG_PUTS("..."); /* show that we are busy */
3367 out_flush();
3368
3369 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003370 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
3372 if (type == WILD_NEXT || type == WILD_PREV)
3373 {
3374 /*
3375 * Get next/previous match for a previous expanded pattern.
3376 */
3377 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3378 }
3379 else
3380 {
3381 /*
3382 * Translate string into pattern and expand it.
3383 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003384 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3385 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 p2 = NULL;
3387 else
3388 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003389 int use_options = options |
3390 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE;
3391
3392 if (p_wic)
3393 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003394 p2 = ExpandOne(xp, p1,
3395 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003396 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003398 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 if (p2 != NULL && type == WILD_LONGEST)
3400 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003401 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (ccline.cmdbuff[i + j] == '*'
3403 || ccline.cmdbuff[i + j] == '?')
3404 break;
3405 if ((int)STRLEN(p2) < j)
3406 {
3407 vim_free(p2);
3408 p2 = NULL;
3409 }
3410 }
3411 }
3412 }
3413
3414 if (p2 != NULL && !got_int)
3415 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003416 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003417 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003419 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 xp->xp_pattern = ccline.cmdbuff + i;
3421 }
3422 else
3423 v = OK;
3424 if (v == OK)
3425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003426 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3427 &ccline.cmdbuff[ccline.cmdpos],
3428 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3429 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 ccline.cmdlen += difflen;
3431 ccline.cmdpos += difflen;
3432 }
3433 }
3434 vim_free(p2);
3435
3436 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003437 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
3439 /* When expanding a ":map" command and no matches are found, assume that
3440 * the key is supposed to be inserted literally */
3441 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3442 return FAIL;
3443
3444 if (xp->xp_numfiles <= 0 && p2 == NULL)
3445 beep_flush();
3446 else if (xp->xp_numfiles == 1)
3447 /* free expanded pattern */
3448 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3449
3450 return OK;
3451}
3452
3453/*
3454 * Do wildcard expansion on the string 'str'.
3455 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003456 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 * Return NULL for failure.
3458 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003459 * "orig" is the originally expanded string, copied to allocated memory. It
3460 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3461 * WILD_PREV "orig" should be NULL.
3462 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003463 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3464 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 *
3466 * mode = WILD_FREE: just free previously expanded matches
3467 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3468 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3469 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3470 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3471 * mode = WILD_ALL: return all matches concatenated
3472 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003473 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 *
3475 * options = WILD_LIST_NOTFOUND: list entries without a match
3476 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3477 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3478 * options = WILD_NO_BEEP: Don't beep for multiple matches
3479 * options = WILD_ADD_SLASH: add a slash after directory names
3480 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3481 * options = WILD_SILENT: don't print warning messages
3482 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003483 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 *
3485 * The variables xp->xp_context and xp->xp_backslash must have been set!
3486 */
3487 char_u *
3488ExpandOne(xp, str, orig, options, mode)
3489 expand_T *xp;
3490 char_u *str;
3491 char_u *orig; /* allocated copy of original of expanded string */
3492 int options;
3493 int mode;
3494{
3495 char_u *ss = NULL;
3496 static int findex;
3497 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003498 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 int i;
3500 long_u len;
3501 int non_suf_match; /* number without matching suffix */
3502
3503 /*
3504 * first handle the case of using an old match
3505 */
3506 if (mode == WILD_NEXT || mode == WILD_PREV)
3507 {
3508 if (xp->xp_numfiles > 0)
3509 {
3510 if (mode == WILD_PREV)
3511 {
3512 if (findex == -1)
3513 findex = xp->xp_numfiles;
3514 --findex;
3515 }
3516 else /* mode == WILD_NEXT */
3517 ++findex;
3518
3519 /*
3520 * When wrapping around, return the original string, set findex to
3521 * -1.
3522 */
3523 if (findex < 0)
3524 {
3525 if (orig_save == NULL)
3526 findex = xp->xp_numfiles - 1;
3527 else
3528 findex = -1;
3529 }
3530 if (findex >= xp->xp_numfiles)
3531 {
3532 if (orig_save == NULL)
3533 findex = 0;
3534 else
3535 findex = -1;
3536 }
3537#ifdef FEAT_WILDMENU
3538 if (p_wmnu)
3539 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3540 findex, cmd_showtail);
3541#endif
3542 if (findex == -1)
3543 return vim_strsave(orig_save);
3544 return vim_strsave(xp->xp_files[findex]);
3545 }
3546 else
3547 return NULL;
3548 }
3549
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003550 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3552 {
3553 FreeWild(xp->xp_numfiles, xp->xp_files);
3554 xp->xp_numfiles = -1;
3555 vim_free(orig_save);
3556 orig_save = NULL;
3557 }
3558 findex = 0;
3559
3560 if (mode == WILD_FREE) /* only release file name */
3561 return NULL;
3562
3563 if (xp->xp_numfiles == -1)
3564 {
3565 vim_free(orig_save);
3566 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003567 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
3569 /*
3570 * Do the expansion.
3571 */
3572 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3573 options) == FAIL)
3574 {
3575#ifdef FNAME_ILLEGAL
3576 /* Illegal file name has been silently skipped. But when there
3577 * are wildcards, the real problem is that there was no match,
3578 * causing the pattern to be added, which has illegal characters.
3579 */
3580 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3581 EMSG2(_(e_nomatch2), str);
3582#endif
3583 }
3584 else if (xp->xp_numfiles == 0)
3585 {
3586 if (!(options & WILD_SILENT))
3587 EMSG2(_(e_nomatch2), str);
3588 }
3589 else
3590 {
3591 /* Escape the matches for use on the command line. */
3592 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3593
3594 /*
3595 * Check for matching suffixes in file names.
3596 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003597 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3598 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
3600 if (xp->xp_numfiles)
3601 non_suf_match = xp->xp_numfiles;
3602 else
3603 non_suf_match = 1;
3604 if ((xp->xp_context == EXPAND_FILES
3605 || xp->xp_context == EXPAND_DIRECTORIES)
3606 && xp->xp_numfiles > 1)
3607 {
3608 /*
3609 * More than one match; check suffix.
3610 * The files will have been sorted on matching suffix in
3611 * expand_wildcards, only need to check the first two.
3612 */
3613 non_suf_match = 0;
3614 for (i = 0; i < 2; ++i)
3615 if (match_suffix(xp->xp_files[i]))
3616 ++non_suf_match;
3617 }
3618 if (non_suf_match != 1)
3619 {
3620 /* Can we ever get here unless it's while expanding
3621 * interactively? If not, we can get rid of this all
3622 * together. Don't really want to wait for this message
3623 * (and possibly have to hit return to continue!).
3624 */
3625 if (!(options & WILD_SILENT))
3626 EMSG(_(e_toomany));
3627 else if (!(options & WILD_NO_BEEP))
3628 beep_flush();
3629 }
3630 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3631 ss = vim_strsave(xp->xp_files[0]);
3632 }
3633 }
3634 }
3635
3636 /* Find longest common part */
3637 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3638 {
3639 for (len = 0; xp->xp_files[0][len]; ++len)
3640 {
3641 for (i = 0; i < xp->xp_numfiles; ++i)
3642 {
3643#ifdef CASE_INSENSITIVE_FILENAME
3644 if (xp->xp_context == EXPAND_DIRECTORIES
3645 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003646 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 || xp->xp_context == EXPAND_BUFFERS)
3648 {
3649 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3650 TOLOWER_LOC(xp->xp_files[0][len]))
3651 break;
3652 }
3653 else
3654#endif
3655 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3656 break;
3657 }
3658 if (i < xp->xp_numfiles)
3659 {
3660 if (!(options & WILD_NO_BEEP))
3661 vim_beep();
3662 break;
3663 }
3664 }
3665 ss = alloc((unsigned)len + 1);
3666 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003667 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 findex = -1; /* next p_wc gets first one */
3669 }
3670
3671 /* Concatenate all matching names */
3672 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3673 {
3674 len = 0;
3675 for (i = 0; i < xp->xp_numfiles; ++i)
3676 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3677 ss = lalloc(len, TRUE);
3678 if (ss != NULL)
3679 {
3680 *ss = NUL;
3681 for (i = 0; i < xp->xp_numfiles; ++i)
3682 {
3683 STRCAT(ss, xp->xp_files[i]);
3684 if (i != xp->xp_numfiles - 1)
3685 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3686 }
3687 }
3688 }
3689
3690 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3691 ExpandCleanup(xp);
3692
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003693 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003694 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003695 vim_free(orig);
3696
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 return ss;
3698}
3699
3700/*
3701 * Prepare an expand structure for use.
3702 */
3703 void
3704ExpandInit(xp)
3705 expand_T *xp;
3706{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003707 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003708 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003710#ifndef BACKSLASH_IN_FILENAME
3711 xp->xp_shell = FALSE;
3712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 xp->xp_numfiles = -1;
3714 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003715#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3716 xp->xp_arg = NULL;
3717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718}
3719
3720/*
3721 * Cleanup an expand structure after use.
3722 */
3723 void
3724ExpandCleanup(xp)
3725 expand_T *xp;
3726{
3727 if (xp->xp_numfiles >= 0)
3728 {
3729 FreeWild(xp->xp_numfiles, xp->xp_files);
3730 xp->xp_numfiles = -1;
3731 }
3732}
3733
3734 void
3735ExpandEscape(xp, str, numfiles, files, options)
3736 expand_T *xp;
3737 char_u *str;
3738 int numfiles;
3739 char_u **files;
3740 int options;
3741{
3742 int i;
3743 char_u *p;
3744
3745 /*
3746 * May change home directory back to "~"
3747 */
3748 if (options & WILD_HOME_REPLACE)
3749 tilde_replace(str, numfiles, files);
3750
3751 if (options & WILD_ESCAPE)
3752 {
3753 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003754 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003755 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 || xp->xp_context == EXPAND_BUFFERS
3757 || xp->xp_context == EXPAND_DIRECTORIES)
3758 {
3759 /*
3760 * Insert a backslash into a file name before a space, \, %, #
3761 * and wildmatch characters, except '~'.
3762 */
3763 for (i = 0; i < numfiles; ++i)
3764 {
3765 /* for ":set path=" we need to escape spaces twice */
3766 if (xp->xp_backslash == XP_BS_THREE)
3767 {
3768 p = vim_strsave_escaped(files[i], (char_u *)" ");
3769 if (p != NULL)
3770 {
3771 vim_free(files[i]);
3772 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003773#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 p = vim_strsave_escaped(files[i], (char_u *)" ");
3775 if (p != NULL)
3776 {
3777 vim_free(files[i]);
3778 files[i] = p;
3779 }
3780#endif
3781 }
3782 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003783#ifdef BACKSLASH_IN_FILENAME
3784 p = vim_strsave_fnameescape(files[i], FALSE);
3785#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003786 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 if (p != NULL)
3789 {
3790 vim_free(files[i]);
3791 files[i] = p;
3792 }
3793
3794 /* If 'str' starts with "\~", replace "~" at start of
3795 * files[i] with "\~". */
3796 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003797 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003800
3801 /* If the first file starts with a '+' escape it. Otherwise it
3802 * could be seen as "+cmd". */
3803 if (*files[0] == '+')
3804 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 }
3806 else if (xp->xp_context == EXPAND_TAGS)
3807 {
3808 /*
3809 * Insert a backslash before characters in a tag name that
3810 * would terminate the ":tag" command.
3811 */
3812 for (i = 0; i < numfiles; ++i)
3813 {
3814 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3815 if (p != NULL)
3816 {
3817 vim_free(files[i]);
3818 files[i] = p;
3819 }
3820 }
3821 }
3822 }
3823}
3824
3825/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003826 * Escape special characters in "fname" for when used as a file name argument
3827 * after a Vim command, or, when "shell" is non-zero, a shell command.
3828 * Returns the result in allocated memory.
3829 */
3830 char_u *
3831vim_strsave_fnameescape(fname, shell)
3832 char_u *fname;
3833 int shell;
3834{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003835 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003836#ifdef BACKSLASH_IN_FILENAME
3837 char_u buf[20];
3838 int j = 0;
3839
3840 /* Don't escape '[' and '{' if they are in 'isfname'. */
3841 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3842 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3843 buf[j++] = *p;
3844 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003845 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003846#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003847 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3848 if (shell && csh_like_shell() && p != NULL)
3849 {
3850 char_u *s;
3851
3852 /* For csh and similar shells need to put two backslashes before '!'.
3853 * One is taken by Vim, one by the shell. */
3854 s = vim_strsave_escaped(p, (char_u *)"!");
3855 vim_free(p);
3856 p = s;
3857 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003858#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003859
3860 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3861 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003862 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003863 escape_fname(&p);
3864
3865 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003866}
3867
3868/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003869 * Put a backslash before the file name in "pp", which is in allocated memory.
3870 */
3871 static void
3872escape_fname(pp)
3873 char_u **pp;
3874{
3875 char_u *p;
3876
3877 p = alloc((unsigned)(STRLEN(*pp) + 2));
3878 if (p != NULL)
3879 {
3880 p[0] = '\\';
3881 STRCPY(p + 1, *pp);
3882 vim_free(*pp);
3883 *pp = p;
3884 }
3885}
3886
3887/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 * For each file name in files[num_files]:
3889 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3890 */
3891 void
3892tilde_replace(orig_pat, num_files, files)
3893 char_u *orig_pat;
3894 int num_files;
3895 char_u **files;
3896{
3897 int i;
3898 char_u *p;
3899
3900 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3901 {
3902 for (i = 0; i < num_files; ++i)
3903 {
3904 p = home_replace_save(NULL, files[i]);
3905 if (p != NULL)
3906 {
3907 vim_free(files[i]);
3908 files[i] = p;
3909 }
3910 }
3911 }
3912}
3913
3914/*
3915 * Show all matches for completion on the command line.
3916 * Returns EXPAND_NOTHING when the character that triggered expansion should
3917 * be inserted like a normal character.
3918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 static int
3920showmatches(xp, wildmenu)
3921 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003922 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923{
3924#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3925 int num_files;
3926 char_u **files_found;
3927 int i, j, k;
3928 int maxlen;
3929 int lines;
3930 int columns;
3931 char_u *p;
3932 int lastlen;
3933 int attr;
3934 int showtail;
3935
3936 if (xp->xp_numfiles == -1)
3937 {
3938 set_expand_context(xp);
3939 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3940 &num_files, &files_found);
3941 showtail = expand_showtail(xp);
3942 if (i != EXPAND_OK)
3943 return i;
3944
3945 }
3946 else
3947 {
3948 num_files = xp->xp_numfiles;
3949 files_found = xp->xp_files;
3950 showtail = cmd_showtail;
3951 }
3952
3953#ifdef FEAT_WILDMENU
3954 if (!wildmenu)
3955 {
3956#endif
3957 msg_didany = FALSE; /* lines_left will be set */
3958 msg_start(); /* prepare for paging */
3959 msg_putchar('\n');
3960 out_flush();
3961 cmdline_row = msg_row;
3962 msg_didany = FALSE; /* lines_left will be set again */
3963 msg_start(); /* prepare for paging */
3964#ifdef FEAT_WILDMENU
3965 }
3966#endif
3967
3968 if (got_int)
3969 got_int = FALSE; /* only int. the completion, not the cmd line */
3970#ifdef FEAT_WILDMENU
3971 else if (wildmenu)
3972 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3973#endif
3974 else
3975 {
3976 /* find the length of the longest file name */
3977 maxlen = 0;
3978 for (i = 0; i < num_files; ++i)
3979 {
3980 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003981 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 || xp->xp_context == EXPAND_BUFFERS))
3983 {
3984 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3985 j = vim_strsize(NameBuff);
3986 }
3987 else
3988 j = vim_strsize(L_SHOWFILE(i));
3989 if (j > maxlen)
3990 maxlen = j;
3991 }
3992
3993 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3994 lines = num_files;
3995 else
3996 {
3997 /* compute the number of columns and lines for the listing */
3998 maxlen += 2; /* two spaces between file names */
3999 columns = ((int)Columns + 2) / maxlen;
4000 if (columns < 1)
4001 columns = 1;
4002 lines = (num_files + columns - 1) / columns;
4003 }
4004
4005 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4006
4007 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4008 {
4009 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4010 msg_clr_eos();
4011 msg_advance(maxlen - 3);
4012 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4013 }
4014
4015 /* list the files line by line */
4016 for (i = 0; i < lines; ++i)
4017 {
4018 lastlen = 999;
4019 for (k = i; k < num_files; k += lines)
4020 {
4021 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4022 {
4023 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4024 p = files_found[k] + STRLEN(files_found[k]) + 1;
4025 msg_advance(maxlen + 1);
4026 msg_puts(p);
4027 msg_advance(maxlen + 3);
4028 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4029 break;
4030 }
4031 for (j = maxlen - lastlen; --j >= 0; )
4032 msg_putchar(' ');
4033 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004034 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 || xp->xp_context == EXPAND_BUFFERS)
4036 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004037 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004038 if (xp->xp_numfiles != -1)
4039 {
4040 char_u *halved_slash;
4041 char_u *exp_path;
4042
4043 /* Expansion was done before and special characters
4044 * were escaped, need to halve backslashes. Also
4045 * $HOME has been replaced with ~/. */
4046 exp_path = expand_env_save_opt(files_found[k], TRUE);
4047 halved_slash = backslash_halve_save(
4048 exp_path != NULL ? exp_path : files_found[k]);
4049 j = mch_isdir(halved_slash != NULL ? halved_slash
4050 : files_found[k]);
4051 vim_free(exp_path);
4052 vim_free(halved_slash);
4053 }
4054 else
4055 /* Expansion was done here, file names are literal. */
4056 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 if (showtail)
4058 p = L_SHOWFILE(k);
4059 else
4060 {
4061 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4062 TRUE);
4063 p = NameBuff;
4064 }
4065 }
4066 else
4067 {
4068 j = FALSE;
4069 p = L_SHOWFILE(k);
4070 }
4071 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4072 }
4073 if (msg_col > 0) /* when not wrapped around */
4074 {
4075 msg_clr_eos();
4076 msg_putchar('\n');
4077 }
4078 out_flush(); /* show one line at a time */
4079 if (got_int)
4080 {
4081 got_int = FALSE;
4082 break;
4083 }
4084 }
4085
4086 /*
4087 * we redraw the command below the lines that we have just listed
4088 * This is a bit tricky, but it saves a lot of screen updating.
4089 */
4090 cmdline_row = msg_row; /* will put it back later */
4091 }
4092
4093 if (xp->xp_numfiles == -1)
4094 FreeWild(num_files, files_found);
4095
4096 return EXPAND_OK;
4097}
4098
4099/*
4100 * Private gettail for showmatches() (and win_redr_status_matches()):
4101 * Find tail of file name path, but ignore trailing "/".
4102 */
4103 char_u *
4104sm_gettail(s)
4105 char_u *s;
4106{
4107 char_u *p;
4108 char_u *t = s;
4109 int had_sep = FALSE;
4110
4111 for (p = s; *p != NUL; )
4112 {
4113 if (vim_ispathsep(*p)
4114#ifdef BACKSLASH_IN_FILENAME
4115 && !rem_backslash(p)
4116#endif
4117 )
4118 had_sep = TRUE;
4119 else if (had_sep)
4120 {
4121 t = p;
4122 had_sep = FALSE;
4123 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004124 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 return t;
4127}
4128
4129/*
4130 * Return TRUE if we only need to show the tail of completion matches.
4131 * When not completing file names or there is a wildcard in the path FALSE is
4132 * returned.
4133 */
4134 static int
4135expand_showtail(xp)
4136 expand_T *xp;
4137{
4138 char_u *s;
4139 char_u *end;
4140
4141 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004142 if (xp->xp_context != EXPAND_FILES
4143 && xp->xp_context != EXPAND_SHELLCMD
4144 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 return FALSE;
4146
4147 end = gettail(xp->xp_pattern);
4148 if (end == xp->xp_pattern) /* there is no path separator */
4149 return FALSE;
4150
4151 for (s = xp->xp_pattern; s < end; s++)
4152 {
4153 /* Skip escaped wildcards. Only when the backslash is not a path
4154 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4155 if (rem_backslash(s))
4156 ++s;
4157 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4158 return FALSE;
4159 }
4160 return TRUE;
4161}
4162
4163/*
4164 * Prepare a string for expansion.
4165 * When expanding file names: The string will be used with expand_wildcards().
4166 * Copy the file name into allocated memory and add a '*' at the end.
4167 * When expanding other names: The string will be used with regcomp(). Copy
4168 * the name into allocated memory and prepend "^".
4169 */
4170 char_u *
4171addstar(fname, len, context)
4172 char_u *fname;
4173 int len;
4174 int context; /* EXPAND_FILES etc. */
4175{
4176 char_u *retval;
4177 int i, j;
4178 int new_len;
4179 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004180 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004182 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004183 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004184 && context != EXPAND_SHELLCMD
4185 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
4187 /*
4188 * Matching will be done internally (on something other than files).
4189 * So we convert the file-matching-type wildcards into our kind for
4190 * use with vim_regcomp(). First work out how long it will be:
4191 */
4192
4193 /* For help tags the translation is done in find_help_tags().
4194 * For a tag pattern starting with "/" no translation is needed. */
4195 if (context == EXPAND_HELP
4196 || context == EXPAND_COLORS
4197 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004198 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004199 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 || (context == EXPAND_TAGS && fname[0] == '/'))
4201 retval = vim_strnsave(fname, len);
4202 else
4203 {
4204 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4205 for (i = 0; i < len; i++)
4206 {
4207 if (fname[i] == '*' || fname[i] == '~')
4208 new_len++; /* '*' needs to be replaced by ".*"
4209 '~' needs to be replaced by "\~" */
4210
4211 /* Buffer names are like file names. "." should be literal */
4212 if (context == EXPAND_BUFFERS && fname[i] == '.')
4213 new_len++; /* "." becomes "\." */
4214
4215 /* Custom expansion takes care of special things, match
4216 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004217 if ((context == EXPAND_USER_DEFINED
4218 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 new_len++; /* '\' becomes "\\" */
4220 }
4221 retval = alloc(new_len);
4222 if (retval != NULL)
4223 {
4224 retval[0] = '^';
4225 j = 1;
4226 for (i = 0; i < len; i++, j++)
4227 {
4228 /* Skip backslash. But why? At least keep it for custom
4229 * expansion. */
4230 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004231 && context != EXPAND_USER_LIST
4232 && fname[i] == '\\'
4233 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 break;
4235
4236 switch (fname[i])
4237 {
4238 case '*': retval[j++] = '.';
4239 break;
4240 case '~': retval[j++] = '\\';
4241 break;
4242 case '?': retval[j] = '.';
4243 continue;
4244 case '.': if (context == EXPAND_BUFFERS)
4245 retval[j++] = '\\';
4246 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004247 case '\\': if (context == EXPAND_USER_DEFINED
4248 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 retval[j++] = '\\';
4250 break;
4251 }
4252 retval[j] = fname[i];
4253 }
4254 retval[j] = NUL;
4255 }
4256 }
4257 }
4258 else
4259 {
4260 retval = alloc(len + 4);
4261 if (retval != NULL)
4262 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004263 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264
4265 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004266 * Don't add a star to *, ~, ~user, $var or `cmd`.
4267 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 * ~ would be at the start of the file name, but not the tail.
4269 * $ could be anywhere in the tail.
4270 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004271 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 */
4273 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004274 ends_in_star = (len > 0 && retval[len - 1] == '*');
4275#ifndef BACKSLASH_IN_FILENAME
4276 for (i = len - 2; i >= 0; --i)
4277 {
4278 if (retval[i] != '\\')
4279 break;
4280 ends_in_star = !ends_in_star;
4281 }
4282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004284 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 && vim_strchr(tail, '$') == NULL
4286 && vim_strchr(retval, '`') == NULL)
4287 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004288 else if (len > 0 && retval[len - 1] == '$')
4289 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 retval[len] = NUL;
4291 }
4292 }
4293 return retval;
4294}
4295
4296/*
4297 * Must parse the command line so far to work out what context we are in.
4298 * Completion can then be done based on that context.
4299 * This routine sets the variables:
4300 * xp->xp_pattern The start of the pattern to be expanded within
4301 * the command line (ends at the cursor).
4302 * xp->xp_context The type of thing to expand. Will be one of:
4303 *
4304 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4305 * the command line, like an unknown command. Caller
4306 * should beep.
4307 * EXPAND_NOTHING Unrecognised context for completion, use char like
4308 * a normal char, rather than for completion. eg
4309 * :s/^I/
4310 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4311 * it.
4312 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4313 * EXPAND_FILES After command with XFILE set, or after setting
4314 * with P_EXPAND set. eg :e ^I, :w>>^I
4315 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4316 * when we know only directories are of interest. eg
4317 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004318 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4320 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4321 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4322 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4323 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4324 * EXPAND_EVENTS Complete event names
4325 * EXPAND_SYNTAX Complete :syntax command arguments
4326 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4327 * EXPAND_AUGROUP Complete autocommand group names
4328 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4329 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4330 * eg :unmap a^I , :cunab x^I
4331 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4332 * eg :call sub^I
4333 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4334 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4335 * names in expressions, eg :while s^I
4336 * EXPAND_ENV_VARS Complete environment variable names
4337 */
4338 static void
4339set_expand_context(xp)
4340 expand_T *xp;
4341{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004342 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 if (ccline.cmdfirstc != ':'
4344#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004345 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004346 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347#endif
4348 )
4349 {
4350 xp->xp_context = EXPAND_NOTHING;
4351 return;
4352 }
4353 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4354}
4355
4356 void
4357set_cmd_context(xp, str, len, col)
4358 expand_T *xp;
4359 char_u *str; /* start of command line */
4360 int len; /* length of command line (excl. NUL) */
4361 int col; /* position of cursor */
4362{
4363 int old_char = NUL;
4364 char_u *nextcomm;
4365
4366 /*
4367 * Avoid a UMR warning from Purify, only save the character if it has been
4368 * written before.
4369 */
4370 if (col < len)
4371 old_char = str[col];
4372 str[col] = NUL;
4373 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004374
4375#ifdef FEAT_EVAL
4376 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004377 {
4378# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004379 /* pass CMD_SIZE because there is no real command */
4380 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004381# endif
4382 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004383 else if (ccline.input_fn)
4384 {
4385 xp->xp_context = ccline.xp_context;
4386 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004387# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004388 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004389# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004390 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004391 else
4392#endif
4393 while (nextcomm != NULL)
4394 nextcomm = set_one_cmd_context(xp, nextcomm);
4395
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 str[col] = old_char;
4397}
4398
4399/*
4400 * Expand the command line "str" from context "xp".
4401 * "xp" must have been set by set_cmd_context().
4402 * xp->xp_pattern points into "str", to where the text that is to be expanded
4403 * starts.
4404 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4405 * cursor.
4406 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4407 * key that triggered expansion literally.
4408 * Returns EXPAND_OK otherwise.
4409 */
4410 int
4411expand_cmdline(xp, str, col, matchcount, matches)
4412 expand_T *xp;
4413 char_u *str; /* start of command line */
4414 int col; /* position of cursor */
4415 int *matchcount; /* return: nr of matches */
4416 char_u ***matches; /* return: array of pointers to matches */
4417{
4418 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004419 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420
4421 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4422 {
4423 beep_flush();
4424 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4425 }
4426 if (xp->xp_context == EXPAND_NOTHING)
4427 {
4428 /* Caller can use the character as a normal char instead */
4429 return EXPAND_NOTHING;
4430 }
4431
4432 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004433 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4434 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 if (file_str == NULL)
4436 return EXPAND_UNSUCCESSFUL;
4437
Bram Moolenaar94950a92010-12-02 16:01:29 +01004438 if (p_wic)
4439 options += WILD_ICASE;
4440
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004442 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
4444 *matchcount = 0;
4445 *matches = NULL;
4446 }
4447 vim_free(file_str);
4448
4449 return EXPAND_OK;
4450}
4451
4452#ifdef FEAT_MULTI_LANG
4453/*
4454 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4455 */
4456static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4457
4458 static void
4459cleanup_help_tags(num_file, file)
4460 int num_file;
4461 char_u **file;
4462{
4463 int i, j;
4464 int len;
4465
4466 for (i = 0; i < num_file; ++i)
4467 {
4468 len = (int)STRLEN(file[i]) - 3;
4469 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4470 {
4471 /* Sorting on priority means the same item in another language may
4472 * be anywhere. Search all items for a match up to the "@en". */
4473 for (j = 0; j < num_file; ++j)
4474 if (j != i
4475 && (int)STRLEN(file[j]) == len + 3
4476 && STRNCMP(file[i], file[j], len + 1) == 0)
4477 break;
4478 if (j == num_file)
4479 file[i][len] = NUL;
4480 }
4481 }
4482}
4483#endif
4484
4485/*
4486 * Do the expansion based on xp->xp_context and "pat".
4487 */
4488 static int
4489ExpandFromContext(xp, pat, num_file, file, options)
4490 expand_T *xp;
4491 char_u *pat;
4492 int *num_file;
4493 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004494 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495{
4496#ifdef FEAT_CMDL_COMPL
4497 regmatch_T regmatch;
4498#endif
4499 int ret;
4500 int flags;
4501
4502 flags = EW_DIR; /* include directories */
4503 if (options & WILD_LIST_NOTFOUND)
4504 flags |= EW_NOTFOUND;
4505 if (options & WILD_ADD_SLASH)
4506 flags |= EW_ADDSLASH;
4507 if (options & WILD_KEEP_ALL)
4508 flags |= EW_KEEPALL;
4509 if (options & WILD_SILENT)
4510 flags |= EW_SILENT;
4511
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004512 if (xp->xp_context == EXPAND_FILES
4513 || xp->xp_context == EXPAND_DIRECTORIES
4514 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
4516 /*
4517 * Expand file or directory names.
4518 */
4519 int free_pat = FALSE;
4520 int i;
4521
4522 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4523 * space */
4524 if (xp->xp_backslash != XP_BS_NONE)
4525 {
4526 free_pat = TRUE;
4527 pat = vim_strsave(pat);
4528 for (i = 0; pat[i]; ++i)
4529 if (pat[i] == '\\')
4530 {
4531 if (xp->xp_backslash == XP_BS_THREE
4532 && pat[i + 1] == '\\'
4533 && pat[i + 2] == '\\'
4534 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004535 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 if (xp->xp_backslash == XP_BS_ONE
4537 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004538 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
4540 }
4541
4542 if (xp->xp_context == EXPAND_FILES)
4543 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004544 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4545 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 else
4547 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004548 if (options & WILD_ICASE)
4549 flags |= EW_ICASE;
4550
Bram Moolenaard7834d32009-12-02 16:14:36 +00004551 /* Expand wildcards, supporting %:h and the like. */
4552 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (free_pat)
4554 vim_free(pat);
4555 return ret;
4556 }
4557
4558 *file = (char_u **)"";
4559 *num_file = 0;
4560 if (xp->xp_context == EXPAND_HELP)
4561 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004562 /* With an empty argument we would get all the help tags, which is
4563 * very slow. Get matches for "help" instead. */
4564 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4565 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
4567#ifdef FEAT_MULTI_LANG
4568 cleanup_help_tags(*num_file, *file);
4569#endif
4570 return OK;
4571 }
4572 return FAIL;
4573 }
4574
4575#ifndef FEAT_CMDL_COMPL
4576 return FAIL;
4577#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004578 if (xp->xp_context == EXPAND_SHELLCMD)
4579 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if (xp->xp_context == EXPAND_OLD_SETTING)
4581 return ExpandOldSetting(num_file, file);
4582 if (xp->xp_context == EXPAND_BUFFERS)
4583 return ExpandBufnames(pat, num_file, file, options);
4584 if (xp->xp_context == EXPAND_TAGS
4585 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4586 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4587 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004588 {
4589 char *directories[] = {"colors", NULL};
4590 return ExpandRTDir(pat, num_file, file, directories);
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004593 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004594 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004595 return ExpandRTDir(pat, num_file, file, directories);
4596 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004597 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004598 {
4599 char *directories[] = {"syntax", NULL};
4600 return ExpandRTDir(pat, num_file, file, directories);
4601 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004602 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004603 {
4604 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4605 return ExpandRTDir(pat, num_file, file, directories);
4606 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004607# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4608 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004609 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
4612 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4613 if (regmatch.regprog == NULL)
4614 return FAIL;
4615
4616 /* set ignore-case according to p_ic, p_scs and pat */
4617 regmatch.rm_ic = ignorecase(pat);
4618
4619 if (xp->xp_context == EXPAND_SETTINGS
4620 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4621 ret = ExpandSettings(xp, &regmatch, num_file, file);
4622 else if (xp->xp_context == EXPAND_MAPPINGS)
4623 ret = ExpandMappings(&regmatch, num_file, file);
4624# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4625 else if (xp->xp_context == EXPAND_USER_DEFINED)
4626 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4627# endif
4628 else
4629 {
4630 static struct expgen
4631 {
4632 int context;
4633 char_u *((*func)__ARGS((expand_T *, int)));
4634 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004635 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 } tab[] =
4637 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004638 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4639 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004640#ifdef FEAT_CMDHIST
4641 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004644 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4645 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4646 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4647 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648#endif
4649#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004650 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4651 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4652 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4653 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654#endif
4655#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004656 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4657 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#endif
4659#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004660 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004662 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004664 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4665 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004667#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004668 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004669#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004670#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004671 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004672#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004673#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004674 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4677 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004678 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4679 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004681 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 };
4683 int i;
4684
4685 /*
4686 * Find a context in the table and call the ExpandGeneric() with the
4687 * right function to do the expansion.
4688 */
4689 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004690 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (xp->xp_context == tab[i].context)
4692 {
4693 if (tab[i].ic)
4694 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004695 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004696 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 break;
4698 }
4699 }
4700
4701 vim_free(regmatch.regprog);
4702
4703 return ret;
4704#endif /* FEAT_CMDL_COMPL */
4705}
4706
4707#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4708/*
4709 * Expand a list of names.
4710 *
4711 * Generic function for command line completion. It calls a function to
4712 * obtain strings, one by one. The strings are matched against a regexp
4713 * program. Matching strings are copied into an array, which is returned.
4714 *
4715 * Returns OK when no problems encountered, FAIL for error (out of memory).
4716 */
4717 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004718ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 expand_T *xp;
4720 regmatch_T *regmatch;
4721 int *num_file;
4722 char_u ***file;
4723 char_u *((*func)__ARGS((expand_T *, int)));
4724 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004725 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
4727 int i;
4728 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004729 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 char_u *str;
4731
4732 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004733 * round == 0: count the number of matching names
4734 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004736 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
4738 for (i = 0; ; ++i)
4739 {
4740 str = (*func)(xp, i);
4741 if (str == NULL) /* end of list */
4742 break;
4743 if (*str == NUL) /* skip empty strings */
4744 continue;
4745
4746 if (vim_regexec(regmatch, str, (colnr_T)0))
4747 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004748 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004750 if (escaped)
4751 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4752 else
4753 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 (*file)[count] = str;
4755#ifdef FEAT_MENU
4756 if (func == get_menu_names && str != NULL)
4757 {
4758 /* test for separator added by get_menu_names() */
4759 str += STRLEN(str) - 1;
4760 if (*str == '\001')
4761 *str = '.';
4762 }
4763#endif
4764 }
4765 ++count;
4766 }
4767 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004768 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 if (count == 0)
4771 return OK;
4772 *num_file = count;
4773 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4774 if (*file == NULL)
4775 {
4776 *file = (char_u **)"";
4777 return FAIL;
4778 }
4779 count = 0;
4780 }
4781 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004782
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004783 /* Sort the results. Keep menu's in the specified order. */
4784 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004785 {
4786 if (xp->xp_context == EXPAND_EXPRESSION
4787 || xp->xp_context == EXPAND_FUNCTIONS
4788 || xp->xp_context == EXPAND_USER_FUNC)
4789 /* <SNR> functions should be sorted to the end. */
4790 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4791 sort_func_compare);
4792 else
4793 sort_strings(*file, *num_file);
4794 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004795
Bram Moolenaar4f688582007-07-24 12:34:30 +00004796#ifdef FEAT_CMDL_COMPL
4797 /* Reset the variables used for special highlight names expansion, so that
4798 * they don't show up when getting normal highlight names by ID. */
4799 reset_expand_highlight();
4800#endif
4801
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 return OK;
4803}
4804
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004805/*
4806 * Complete a shell command.
4807 * Returns FAIL or OK;
4808 */
4809 static int
4810expand_shellcmd(filepat, num_file, file, flagsarg)
4811 char_u *filepat; /* pattern to match with command names */
4812 int *num_file; /* return: number of matches */
4813 char_u ***file; /* return: array with matches */
4814 int flagsarg; /* EW_ flags */
4815{
4816 char_u *pat;
4817 int i;
4818 char_u *path;
4819 int mustfree = FALSE;
4820 garray_T ga;
4821 char_u *buf = alloc(MAXPATHL);
4822 size_t l;
4823 char_u *s, *e;
4824 int flags = flagsarg;
4825 int ret;
4826
4827 if (buf == NULL)
4828 return FAIL;
4829
4830 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4831 * space */
4832 pat = vim_strsave(filepat);
4833 for (i = 0; pat[i]; ++i)
4834 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004835 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004836
4837 flags |= EW_FILE | EW_EXEC;
4838
4839 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004840 if (mch_isFullName(pat))
4841 path = (char_u *)" ";
4842 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004843 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4844 path = (char_u *)".";
4845 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004846 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004847 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004848 if (path == NULL)
4849 path = (char_u *)"";
4850 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004851
4852 /*
4853 * Go over all directories in $PATH. Expand matches in that directory and
4854 * collect them in "ga".
4855 */
4856 ga_init2(&ga, (int)sizeof(char *), 10);
4857 for (s = path; *s != NUL; s = e)
4858 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004859 if (*s == ' ')
4860 ++s; /* Skip space used for absolute path name. */
4861
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004862#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4863 e = vim_strchr(s, ';');
4864#else
4865 e = vim_strchr(s, ':');
4866#endif
4867 if (e == NULL)
4868 e = s + STRLEN(s);
4869
4870 l = e - s;
4871 if (l > MAXPATHL - 5)
4872 break;
4873 vim_strncpy(buf, s, l);
4874 add_pathsep(buf);
4875 l = STRLEN(buf);
4876 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4877
4878 /* Expand matches in one directory of $PATH. */
4879 ret = expand_wildcards(1, &buf, num_file, file, flags);
4880 if (ret == OK)
4881 {
4882 if (ga_grow(&ga, *num_file) == FAIL)
4883 FreeWild(*num_file, *file);
4884 else
4885 {
4886 for (i = 0; i < *num_file; ++i)
4887 {
4888 s = (*file)[i];
4889 if (STRLEN(s) > l)
4890 {
4891 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004892 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004893 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4894 }
4895 else
4896 vim_free(s);
4897 }
4898 vim_free(*file);
4899 }
4900 }
4901 if (*e != NUL)
4902 ++e;
4903 }
4904 *file = ga.ga_data;
4905 *num_file = ga.ga_len;
4906
4907 vim_free(buf);
4908 vim_free(pat);
4909 if (mustfree)
4910 vim_free(path);
4911 return OK;
4912}
4913
4914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004916static 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));
4917
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004919 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004920 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004922 static void *
4923call_user_expand_func(user_expand_func, xp, num_file, file)
4924 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 int *num_file;
4927 char_u ***file;
4928{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004929 char_u keep;
4930 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004933 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004934 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
4936 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004937 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 *num_file = 0;
4939 *file = NULL;
4940
Bram Moolenaar21669c02008-01-18 12:16:16 +00004941 if (ccline.cmdbuff == NULL)
4942 {
4943 /* Completion from Insert mode, pass fake arguments. */
4944 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004945 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004946 args[1] = xp->xp_pattern;
4947 }
4948 else
4949 {
4950 /* Completion on the command line, pass real arguments. */
4951 keep = ccline.cmdbuff[ccline.cmdlen];
4952 ccline.cmdbuff[ccline.cmdlen] = 0;
4953 sprintf((char *)num, "%d", ccline.cmdpos);
4954 args[1] = ccline.cmdbuff;
4955 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004956 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 args[2] = num;
4958
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004959 /* Save the cmdline, we don't know what the function may do. */
4960 save_ccline = ccline;
4961 ccline.cmdbuff = NULL;
4962 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004964
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004965 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004966
4967 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004969 if (ccline.cmdbuff != NULL)
4970 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004971
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004972 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004973 return ret;
4974}
4975
4976/*
4977 * Expand names with a function defined by the user.
4978 */
4979 static int
4980ExpandUserDefined(xp, regmatch, num_file, file)
4981 expand_T *xp;
4982 regmatch_T *regmatch;
4983 int *num_file;
4984 char_u ***file;
4985{
4986 char_u *retstr;
4987 char_u *s;
4988 char_u *e;
4989 char_u keep;
4990 garray_T ga;
4991
4992 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4993 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 return FAIL;
4995
4996 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004997 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 {
4999 e = vim_strchr(s, '\n');
5000 if (e == NULL)
5001 e = s + STRLEN(s);
5002 keep = *e;
5003 *e = 0;
5004
5005 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5006 {
5007 *e = keep;
5008 if (*e != NUL)
5009 ++e;
5010 continue;
5011 }
5012
5013 if (ga_grow(&ga, 1) == FAIL)
5014 break;
5015
5016 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5017 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019 *e = keep;
5020 if (*e != NUL)
5021 ++e;
5022 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005023 vim_free(retstr);
5024 *file = ga.ga_data;
5025 *num_file = ga.ga_len;
5026 return OK;
5027}
5028
5029/*
5030 * Expand names with a list returned by a function defined by the user.
5031 */
5032 static int
5033ExpandUserList(xp, num_file, file)
5034 expand_T *xp;
5035 int *num_file;
5036 char_u ***file;
5037{
5038 list_T *retlist;
5039 listitem_T *li;
5040 garray_T ga;
5041
5042 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5043 if (retlist == NULL)
5044 return FAIL;
5045
5046 ga_init2(&ga, (int)sizeof(char *), 3);
5047 /* Loop over the items in the list. */
5048 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5049 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005050 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5051 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005052
5053 if (ga_grow(&ga, 1) == FAIL)
5054 break;
5055
5056 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005057 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005058 ++ga.ga_len;
5059 }
5060 list_unref(retlist);
5061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 *file = ga.ga_data;
5063 *num_file = ga.ga_len;
5064 return OK;
5065}
5066#endif
5067
5068/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005069 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005070 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005071 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 */
5073 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005074ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 char_u *pat;
5076 int *num_file;
5077 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005078 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005080 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 char_u *s;
5082 char_u *e;
5083 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005084 int i;
5085 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
5087 *num_file = 0;
5088 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005089 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005090 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005092 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005094 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5095 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005097 ga_clear_strings(&ga);
5098 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005100 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5101 matches = globpath(p_rtp, s, 0);
5102 vim_free(s);
5103 if (matches == NULL)
5104 continue;
5105
5106 for (s = matches; *s != NUL; s = e)
5107 {
5108 e = vim_strchr(s, '\n');
5109 if (e == NULL)
5110 e = s + STRLEN(s);
5111 if (ga_grow(&ga, 1) == FAIL)
5112 break;
5113 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5114 {
5115 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5116 if (*s == '\n' || vim_ispathsep(*s))
5117 break;
5118 ++s;
5119 ((char_u **)ga.ga_data)[ga.ga_len] =
5120 vim_strnsave(s, (int)(e - s - 4));
5121 ++ga.ga_len;
5122 }
5123 if (*e != NUL)
5124 ++e;
5125 }
5126 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005128 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005129 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005130
5131 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005132 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005133 remove_duplicates(&ga);
5134
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 *file = ga.ga_data;
5136 *num_file = ga.ga_len;
5137 return OK;
5138}
5139
5140#endif
5141
5142#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5143/*
5144 * Expand "file" for all comma-separated directories in "path".
5145 * Returns an allocated string with all matches concatenated, separated by
5146 * newlines. Returns NULL for an error or no matches.
5147 */
5148 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005149globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 char_u *path;
5151 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005152 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
5154 expand_T xpc;
5155 char_u *buf;
5156 garray_T ga;
5157 int i;
5158 int len;
5159 int num_p;
5160 char_u **p;
5161 char_u *cur = NULL;
5162
5163 buf = alloc(MAXPATHL);
5164 if (buf == NULL)
5165 return NULL;
5166
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005167 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005169
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 ga_init2(&ga, 1, 100);
5171
5172 /* Loop over all entries in {path}. */
5173 while (*path != NUL)
5174 {
5175 /* Copy one item of the path to buf[] and concatenate the file name. */
5176 copy_option_part(&path, buf, MAXPATHL, ",");
5177 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5178 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005179# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005180 /* Using the platform's path separator (\) makes vim incorrectly
5181 * treat it as an escape character, use '/' instead. */
5182 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5183 STRCAT(buf, "/");
5184# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005186# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005188 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5189 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005191 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005193 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194
5195 /* Concatenate new results to previous ones. */
5196 if (ga_grow(&ga, len) == OK)
5197 {
5198 cur = (char_u *)ga.ga_data + ga.ga_len;
5199 for (i = 0; i < num_p; ++i)
5200 {
5201 STRCPY(cur, p[i]);
5202 cur += STRLEN(p[i]);
5203 *cur++ = '\n';
5204 }
5205 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
5207 FreeWild(num_p, p);
5208 }
5209 }
5210 }
5211 if (cur != NULL)
5212 *--cur = 0; /* Replace trailing newline with NUL */
5213
5214 vim_free(buf);
5215 return (char_u *)ga.ga_data;
5216}
5217
5218#endif
5219
5220#if defined(FEAT_CMDHIST) || defined(PROTO)
5221
5222/*********************************
5223 * Command line history stuff *
5224 *********************************/
5225
5226/*
5227 * Translate a history character to the associated type number.
5228 */
5229 static int
5230hist_char2type(c)
5231 int c;
5232{
5233 if (c == ':')
5234 return HIST_CMD;
5235 if (c == '=')
5236 return HIST_EXPR;
5237 if (c == '@')
5238 return HIST_INPUT;
5239 if (c == '>')
5240 return HIST_DEBUG;
5241 return HIST_SEARCH; /* must be '?' or '/' */
5242}
5243
5244/*
5245 * Table of history names.
5246 * These names are used in :history and various hist...() functions.
5247 * It is sufficient to give the significant prefix of a history name.
5248 */
5249
5250static char *(history_names[]) =
5251{
5252 "cmd",
5253 "search",
5254 "expr",
5255 "input",
5256 "debug",
5257 NULL
5258};
5259
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005260#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5261/*
5262 * Function given to ExpandGeneric() to obtain the possible first
5263 * arguments of the ":history command.
5264 */
5265 static char_u *
5266get_history_arg(xp, idx)
5267 expand_T *xp UNUSED;
5268 int idx;
5269{
5270 static char_u compl[2] = { NUL, NUL };
5271 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005272 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005273 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5274
5275 if (idx < short_names_count)
5276 {
5277 compl[0] = (char_u)short_names[idx];
5278 return compl;
5279 }
5280 if (idx < short_names_count + history_name_count)
5281 return (char_u *)history_names[idx - short_names_count];
5282 if (idx == short_names_count + history_name_count)
5283 return (char_u *)"all";
5284 return NULL;
5285}
5286#endif
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288/*
5289 * init_history() - Initialize the command line history.
5290 * Also used to re-allocate the history when the size changes.
5291 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005292 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293init_history()
5294{
5295 int newlen; /* new length of history table */
5296 histentry_T *temp;
5297 int i;
5298 int j;
5299 int type;
5300
5301 /*
5302 * If size of history table changed, reallocate it
5303 */
5304 newlen = (int)p_hi;
5305 if (newlen != hislen) /* history length changed */
5306 {
5307 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5308 {
5309 if (newlen)
5310 {
5311 temp = (histentry_T *)lalloc(
5312 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5313 if (temp == NULL) /* out of memory! */
5314 {
5315 if (type == 0) /* first one: just keep the old length */
5316 {
5317 newlen = hislen;
5318 break;
5319 }
5320 /* Already changed one table, now we can only have zero
5321 * length for all tables. */
5322 newlen = 0;
5323 type = -1;
5324 continue;
5325 }
5326 }
5327 else
5328 temp = NULL;
5329 if (newlen == 0 || temp != NULL)
5330 {
5331 if (hisidx[type] < 0) /* there are no entries yet */
5332 {
5333 for (i = 0; i < newlen; ++i)
5334 {
5335 temp[i].hisnum = 0;
5336 temp[i].hisstr = NULL;
5337 }
5338 }
5339 else if (newlen > hislen) /* array becomes bigger */
5340 {
5341 for (i = 0; i <= hisidx[type]; ++i)
5342 temp[i] = history[type][i];
5343 j = i;
5344 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5345 {
5346 temp[i].hisnum = 0;
5347 temp[i].hisstr = NULL;
5348 }
5349 for ( ; j < hislen; ++i, ++j)
5350 temp[i] = history[type][j];
5351 }
5352 else /* array becomes smaller or 0 */
5353 {
5354 j = hisidx[type];
5355 for (i = newlen - 1; ; --i)
5356 {
5357 if (i >= 0) /* copy newest entries */
5358 temp[i] = history[type][j];
5359 else /* remove older entries */
5360 vim_free(history[type][j].hisstr);
5361 if (--j < 0)
5362 j = hislen - 1;
5363 if (j == hisidx[type])
5364 break;
5365 }
5366 hisidx[type] = newlen - 1;
5367 }
5368 vim_free(history[type]);
5369 history[type] = temp;
5370 }
5371 }
5372 hislen = newlen;
5373 }
5374}
5375
5376/*
5377 * Check if command line 'str' is already in history.
5378 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5379 */
5380 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005381in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 int type;
5383 char_u *str;
5384 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005385 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
5387 int i;
5388 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005389 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
5391 if (hisidx[type] < 0)
5392 return FALSE;
5393 i = hisidx[type];
5394 do
5395 {
5396 if (history[type][i].hisstr == NULL)
5397 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005398
5399 /* For search history, check that the separator character matches as
5400 * well. */
5401 p = history[type][i].hisstr;
5402 if (STRCMP(str, p) == 0
5403 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
5405 if (!move_to_front)
5406 return TRUE;
5407 last_i = i;
5408 break;
5409 }
5410 if (--i < 0)
5411 i = hislen - 1;
5412 } while (i != hisidx[type]);
5413
5414 if (last_i >= 0)
5415 {
5416 str = history[type][i].hisstr;
5417 while (i != hisidx[type])
5418 {
5419 if (++i >= hislen)
5420 i = 0;
5421 history[type][last_i] = history[type][i];
5422 last_i = i;
5423 }
5424 history[type][i].hisstr = str;
5425 history[type][i].hisnum = ++hisnum[type];
5426 return TRUE;
5427 }
5428 return FALSE;
5429}
5430
5431/*
5432 * Convert history name (from table above) to its HIST_ equivalent.
5433 * When "name" is empty, return "cmd" history.
5434 * Returns -1 for unknown history name.
5435 */
5436 int
5437get_histtype(name)
5438 char_u *name;
5439{
5440 int i;
5441 int len = (int)STRLEN(name);
5442
5443 /* No argument: use current history. */
5444 if (len == 0)
5445 return hist_char2type(ccline.cmdfirstc);
5446
5447 for (i = 0; history_names[i] != NULL; ++i)
5448 if (STRNICMP(name, history_names[i], len) == 0)
5449 return i;
5450
5451 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5452 return hist_char2type(name[0]);
5453
5454 return -1;
5455}
5456
5457static int last_maptick = -1; /* last seen maptick */
5458
5459/*
5460 * Add the given string to the given history. If the string is already in the
5461 * history then it is moved to the front. "histype" may be one of he HIST_
5462 * values.
5463 */
5464 void
5465add_to_history(histype, new_entry, in_map, sep)
5466 int histype;
5467 char_u *new_entry;
5468 int in_map; /* consider maptick when inside a mapping */
5469 int sep; /* separator character used (search hist) */
5470{
5471 histentry_T *hisptr;
5472 int len;
5473
5474 if (hislen == 0) /* no history */
5475 return;
5476
5477 /*
5478 * Searches inside the same mapping overwrite each other, so that only
5479 * the last line is kept. Be careful not to remove a line that was moved
5480 * down, only lines that were added.
5481 */
5482 if (histype == HIST_SEARCH && in_map)
5483 {
5484 if (maptick == last_maptick)
5485 {
5486 /* Current line is from the same mapping, remove it */
5487 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5488 vim_free(hisptr->hisstr);
5489 hisptr->hisstr = NULL;
5490 hisptr->hisnum = 0;
5491 --hisnum[histype];
5492 if (--hisidx[HIST_SEARCH] < 0)
5493 hisidx[HIST_SEARCH] = hislen - 1;
5494 }
5495 last_maptick = -1;
5496 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005497 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 {
5499 if (++hisidx[histype] == hislen)
5500 hisidx[histype] = 0;
5501 hisptr = &history[histype][hisidx[histype]];
5502 vim_free(hisptr->hisstr);
5503
5504 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005505 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5507 if (hisptr->hisstr != NULL)
5508 hisptr->hisstr[len + 1] = sep;
5509
5510 hisptr->hisnum = ++hisnum[histype];
5511 if (histype == HIST_SEARCH && in_map)
5512 last_maptick = maptick;
5513 }
5514}
5515
5516#if defined(FEAT_EVAL) || defined(PROTO)
5517
5518/*
5519 * Get identifier of newest history entry.
5520 * "histype" may be one of the HIST_ values.
5521 */
5522 int
5523get_history_idx(histype)
5524 int histype;
5525{
5526 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5527 || hisidx[histype] < 0)
5528 return -1;
5529
5530 return history[histype][hisidx[histype]].hisnum;
5531}
5532
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005533static struct cmdline_info *get_ccline_ptr __ARGS((void));
5534
5535/*
5536 * Get pointer to the command line info to use. cmdline_paste() may clear
5537 * ccline and put the previous value in prev_ccline.
5538 */
5539 static struct cmdline_info *
5540get_ccline_ptr()
5541{
5542 if ((State & CMDLINE) == 0)
5543 return NULL;
5544 if (ccline.cmdbuff != NULL)
5545 return &ccline;
5546 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5547 return &prev_ccline;
5548 return NULL;
5549}
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551/*
5552 * Get the current command line in allocated memory.
5553 * Only works when the command line is being edited.
5554 * Returns NULL when something is wrong.
5555 */
5556 char_u *
5557get_cmdline_str()
5558{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005559 struct cmdline_info *p = get_ccline_ptr();
5560
5561 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005563 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564}
5565
5566/*
5567 * Get the current command line position, counted in bytes.
5568 * Zero is the first position.
5569 * Only works when the command line is being edited.
5570 * Returns -1 when something is wrong.
5571 */
5572 int
5573get_cmdline_pos()
5574{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005575 struct cmdline_info *p = get_ccline_ptr();
5576
5577 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005579 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580}
5581
5582/*
5583 * Set the command line byte position to "pos". Zero is the first position.
5584 * Only works when the command line is being edited.
5585 * Returns 1 when failed, 0 when OK.
5586 */
5587 int
5588set_cmdline_pos(pos)
5589 int pos;
5590{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005591 struct cmdline_info *p = get_ccline_ptr();
5592
5593 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 return 1;
5595
5596 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5597 * changed the command line. */
5598 if (pos < 0)
5599 new_cmdpos = 0;
5600 else
5601 new_cmdpos = pos;
5602 return 0;
5603}
5604
5605/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005606 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005607 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005608 * Only works when the command line is being edited.
5609 * Returns NUL when something is wrong.
5610 */
5611 int
5612get_cmdline_type()
5613{
5614 struct cmdline_info *p = get_ccline_ptr();
5615
5616 if (p == NULL)
5617 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005618 if (p->cmdfirstc == NUL)
5619 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005620 return p->cmdfirstc;
5621}
5622
5623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 * Calculate history index from a number:
5625 * num > 0: seen as identifying number of a history entry
5626 * num < 0: relative position in history wrt newest entry
5627 * "histype" may be one of the HIST_ values.
5628 */
5629 static int
5630calc_hist_idx(histype, num)
5631 int histype;
5632 int num;
5633{
5634 int i;
5635 histentry_T *hist;
5636 int wrapped = FALSE;
5637
5638 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5639 || (i = hisidx[histype]) < 0 || num == 0)
5640 return -1;
5641
5642 hist = history[histype];
5643 if (num > 0)
5644 {
5645 while (hist[i].hisnum > num)
5646 if (--i < 0)
5647 {
5648 if (wrapped)
5649 break;
5650 i += hislen;
5651 wrapped = TRUE;
5652 }
5653 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5654 return i;
5655 }
5656 else if (-num <= hislen)
5657 {
5658 i += num + 1;
5659 if (i < 0)
5660 i += hislen;
5661 if (hist[i].hisstr != NULL)
5662 return i;
5663 }
5664 return -1;
5665}
5666
5667/*
5668 * Get a history entry by its index.
5669 * "histype" may be one of the HIST_ values.
5670 */
5671 char_u *
5672get_history_entry(histype, idx)
5673 int histype;
5674 int idx;
5675{
5676 idx = calc_hist_idx(histype, idx);
5677 if (idx >= 0)
5678 return history[histype][idx].hisstr;
5679 else
5680 return (char_u *)"";
5681}
5682
5683/*
5684 * Clear all entries of a history.
5685 * "histype" may be one of the HIST_ values.
5686 */
5687 int
5688clr_history(histype)
5689 int histype;
5690{
5691 int i;
5692 histentry_T *hisptr;
5693
5694 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5695 {
5696 hisptr = history[histype];
5697 for (i = hislen; i--;)
5698 {
5699 vim_free(hisptr->hisstr);
5700 hisptr->hisnum = 0;
5701 hisptr++->hisstr = NULL;
5702 }
5703 hisidx[histype] = -1; /* mark history as cleared */
5704 hisnum[histype] = 0; /* reset identifier counter */
5705 return OK;
5706 }
5707 return FAIL;
5708}
5709
5710/*
5711 * Remove all entries matching {str} from a history.
5712 * "histype" may be one of the HIST_ values.
5713 */
5714 int
5715del_history_entry(histype, str)
5716 int histype;
5717 char_u *str;
5718{
5719 regmatch_T regmatch;
5720 histentry_T *hisptr;
5721 int idx;
5722 int i;
5723 int last;
5724 int found = FALSE;
5725
5726 regmatch.regprog = NULL;
5727 regmatch.rm_ic = FALSE; /* always match case */
5728 if (hislen != 0
5729 && histype >= 0
5730 && histype < HIST_COUNT
5731 && *str != NUL
5732 && (idx = hisidx[histype]) >= 0
5733 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5734 != NULL)
5735 {
5736 i = last = idx;
5737 do
5738 {
5739 hisptr = &history[histype][i];
5740 if (hisptr->hisstr == NULL)
5741 break;
5742 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5743 {
5744 found = TRUE;
5745 vim_free(hisptr->hisstr);
5746 hisptr->hisstr = NULL;
5747 hisptr->hisnum = 0;
5748 }
5749 else
5750 {
5751 if (i != last)
5752 {
5753 history[histype][last] = *hisptr;
5754 hisptr->hisstr = NULL;
5755 hisptr->hisnum = 0;
5756 }
5757 if (--last < 0)
5758 last += hislen;
5759 }
5760 if (--i < 0)
5761 i += hislen;
5762 } while (i != idx);
5763 if (history[histype][idx].hisstr == NULL)
5764 hisidx[histype] = -1;
5765 }
5766 vim_free(regmatch.regprog);
5767 return found;
5768}
5769
5770/*
5771 * Remove an indexed entry from a history.
5772 * "histype" may be one of the HIST_ values.
5773 */
5774 int
5775del_history_idx(histype, idx)
5776 int histype;
5777 int idx;
5778{
5779 int i, j;
5780
5781 i = calc_hist_idx(histype, idx);
5782 if (i < 0)
5783 return FALSE;
5784 idx = hisidx[histype];
5785 vim_free(history[histype][i].hisstr);
5786
5787 /* When deleting the last added search string in a mapping, reset
5788 * last_maptick, so that the last added search string isn't deleted again.
5789 */
5790 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5791 last_maptick = -1;
5792
5793 while (i != idx)
5794 {
5795 j = (i + 1) % hislen;
5796 history[histype][i] = history[histype][j];
5797 i = j;
5798 }
5799 history[histype][i].hisstr = NULL;
5800 history[histype][i].hisnum = 0;
5801 if (--i < 0)
5802 i += hislen;
5803 hisidx[histype] = i;
5804 return TRUE;
5805}
5806
5807#endif /* FEAT_EVAL */
5808
5809#if defined(FEAT_CRYPT) || defined(PROTO)
5810/*
5811 * Very specific function to remove the value in ":set key=val" from the
5812 * history.
5813 */
5814 void
5815remove_key_from_history()
5816{
5817 char_u *p;
5818 int i;
5819
5820 i = hisidx[HIST_CMD];
5821 if (i < 0)
5822 return;
5823 p = history[HIST_CMD][i].hisstr;
5824 if (p != NULL)
5825 for ( ; *p; ++p)
5826 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5827 {
5828 p = vim_strchr(p + 3, '=');
5829 if (p == NULL)
5830 break;
5831 ++p;
5832 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5833 if (p[i] == '\\' && p[i + 1])
5834 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005835 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 --p;
5837 }
5838}
5839#endif
5840
5841#endif /* FEAT_CMDHIST */
5842
5843#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5844/*
5845 * Get indices "num1,num2" that specify a range within a list (not a range of
5846 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5847 * Returns OK if parsed successfully, otherwise FAIL.
5848 */
5849 int
5850get_list_range(str, num1, num2)
5851 char_u **str;
5852 int *num1;
5853 int *num2;
5854{
5855 int len;
5856 int first = FALSE;
5857 long num;
5858
5859 *str = skipwhite(*str);
5860 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5861 {
5862 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5863 *str += len;
5864 *num1 = (int)num;
5865 first = TRUE;
5866 }
5867 *str = skipwhite(*str);
5868 if (**str == ',') /* parse "to" part of range */
5869 {
5870 *str = skipwhite(*str + 1);
5871 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5872 if (len > 0)
5873 {
5874 *num2 = (int)num;
5875 *str = skipwhite(*str + len);
5876 }
5877 else if (!first) /* no number given at all */
5878 return FAIL;
5879 }
5880 else if (first) /* only one number given */
5881 *num2 = *num1;
5882 return OK;
5883}
5884#endif
5885
5886#if defined(FEAT_CMDHIST) || defined(PROTO)
5887/*
5888 * :history command - print a history
5889 */
5890 void
5891ex_history(eap)
5892 exarg_T *eap;
5893{
5894 histentry_T *hist;
5895 int histype1 = HIST_CMD;
5896 int histype2 = HIST_CMD;
5897 int hisidx1 = 1;
5898 int hisidx2 = -1;
5899 int idx;
5900 int i, j, k;
5901 char_u *end;
5902 char_u *arg = eap->arg;
5903
5904 if (hislen == 0)
5905 {
5906 MSG(_("'history' option is zero"));
5907 return;
5908 }
5909
5910 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5911 {
5912 end = arg;
5913 while (ASCII_ISALPHA(*end)
5914 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5915 end++;
5916 i = *end;
5917 *end = NUL;
5918 histype1 = get_histtype(arg);
5919 if (histype1 == -1)
5920 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005921 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
5923 histype1 = 0;
5924 histype2 = HIST_COUNT-1;
5925 }
5926 else
5927 {
5928 *end = i;
5929 EMSG(_(e_trailing));
5930 return;
5931 }
5932 }
5933 else
5934 histype2 = histype1;
5935 *end = i;
5936 }
5937 else
5938 end = arg;
5939 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5940 {
5941 EMSG(_(e_trailing));
5942 return;
5943 }
5944
5945 for (; !got_int && histype1 <= histype2; ++histype1)
5946 {
5947 STRCPY(IObuff, "\n # ");
5948 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5949 MSG_PUTS_TITLE(IObuff);
5950 idx = hisidx[histype1];
5951 hist = history[histype1];
5952 j = hisidx1;
5953 k = hisidx2;
5954 if (j < 0)
5955 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5956 if (k < 0)
5957 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5958 if (idx >= 0 && j <= k)
5959 for (i = idx + 1; !got_int; ++i)
5960 {
5961 if (i == hislen)
5962 i = 0;
5963 if (hist[i].hisstr != NULL
5964 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5965 {
5966 msg_putchar('\n');
5967 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5968 hist[i].hisnum);
5969 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5970 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005971 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 else
5973 STRCAT(IObuff, hist[i].hisstr);
5974 msg_outtrans(IObuff);
5975 out_flush();
5976 }
5977 if (i == idx)
5978 break;
5979 }
5980 }
5981}
5982#endif
5983
5984#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5985static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5986static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5987static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5988static int viminfo_add_at_front = FALSE;
5989
5990static int hist_type2char __ARGS((int type, int use_question));
5991
5992/*
5993 * Translate a history type number to the associated character.
5994 */
5995 static int
5996hist_type2char(type, use_question)
5997 int type;
5998 int use_question; /* use '?' instead of '/' */
5999{
6000 if (type == HIST_CMD)
6001 return ':';
6002 if (type == HIST_SEARCH)
6003 {
6004 if (use_question)
6005 return '?';
6006 else
6007 return '/';
6008 }
6009 if (type == HIST_EXPR)
6010 return '=';
6011 return '@';
6012}
6013
6014/*
6015 * Prepare for reading the history from the viminfo file.
6016 * This allocates history arrays to store the read history lines.
6017 */
6018 void
6019prepare_viminfo_history(asklen)
6020 int asklen;
6021{
6022 int i;
6023 int num;
6024 int type;
6025 int len;
6026
6027 init_history();
6028 viminfo_add_at_front = (asklen != 0);
6029 if (asklen > hislen)
6030 asklen = hislen;
6031
6032 for (type = 0; type < HIST_COUNT; ++type)
6033 {
6034 /*
6035 * Count the number of empty spaces in the history list. If there are
6036 * more spaces available than we request, then fill them up.
6037 */
6038 for (i = 0, num = 0; i < hislen; i++)
6039 if (history[type][i].hisstr == NULL)
6040 num++;
6041 len = asklen;
6042 if (num > len)
6043 len = num;
6044 if (len <= 0)
6045 viminfo_history[type] = NULL;
6046 else
6047 viminfo_history[type] =
6048 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6049 if (viminfo_history[type] == NULL)
6050 len = 0;
6051 viminfo_hislen[type] = len;
6052 viminfo_hisidx[type] = 0;
6053 }
6054}
6055
6056/*
6057 * Accept a line from the viminfo, store it in the history array when it's
6058 * new.
6059 */
6060 int
6061read_viminfo_history(virp)
6062 vir_T *virp;
6063{
6064 int type;
6065 long_u len;
6066 char_u *val;
6067 char_u *p;
6068
6069 type = hist_char2type(virp->vir_line[0]);
6070 if (viminfo_hisidx[type] < viminfo_hislen[type])
6071 {
6072 val = viminfo_readstring(virp, 1, TRUE);
6073 if (val != NULL && *val != NUL)
6074 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006075 int sep = (*val == ' ' ? NUL : *val);
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006078 viminfo_add_at_front, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
6080 /* Need to re-allocate to append the separator byte. */
6081 len = STRLEN(val);
6082 p = lalloc(len + 2, TRUE);
6083 if (p != NULL)
6084 {
6085 if (type == HIST_SEARCH)
6086 {
6087 /* Search entry: Move the separator from the first
6088 * column to after the NUL. */
6089 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006090 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 }
6092 else
6093 {
6094 /* Not a search entry: No separator in the viminfo
6095 * file, add a NUL separator. */
6096 mch_memmove(p, val, (size_t)len + 1);
6097 p[len + 1] = NUL;
6098 }
6099 viminfo_history[type][viminfo_hisidx[type]++] = p;
6100 }
6101 }
6102 }
6103 vim_free(val);
6104 }
6105 return viminfo_readline(virp);
6106}
6107
6108 void
6109finish_viminfo_history()
6110{
6111 int idx;
6112 int i;
6113 int type;
6114
6115 for (type = 0; type < HIST_COUNT; ++type)
6116 {
6117 if (history[type] == NULL)
6118 return;
6119 idx = hisidx[type] + viminfo_hisidx[type];
6120 if (idx >= hislen)
6121 idx -= hislen;
6122 else if (idx < 0)
6123 idx = hislen - 1;
6124 if (viminfo_add_at_front)
6125 hisidx[type] = idx;
6126 else
6127 {
6128 if (hisidx[type] == -1)
6129 hisidx[type] = hislen - 1;
6130 do
6131 {
6132 if (history[type][idx].hisstr != NULL)
6133 break;
6134 if (++idx == hislen)
6135 idx = 0;
6136 } while (idx != hisidx[type]);
6137 if (idx != hisidx[type] && --idx < 0)
6138 idx = hislen - 1;
6139 }
6140 for (i = 0; i < viminfo_hisidx[type]; i++)
6141 {
6142 vim_free(history[type][idx].hisstr);
6143 history[type][idx].hisstr = viminfo_history[type][i];
6144 if (--idx < 0)
6145 idx = hislen - 1;
6146 }
6147 idx += 1;
6148 idx %= hislen;
6149 for (i = 0; i < viminfo_hisidx[type]; i++)
6150 {
6151 history[type][idx++].hisnum = ++hisnum[type];
6152 idx %= hislen;
6153 }
6154 vim_free(viminfo_history[type]);
6155 viminfo_history[type] = NULL;
6156 }
6157}
6158
6159 void
6160write_viminfo_history(fp)
6161 FILE *fp;
6162{
6163 int i;
6164 int type;
6165 int num_saved;
6166 char_u *p;
6167 int c;
6168
6169 init_history();
6170 if (hislen == 0)
6171 return;
6172 for (type = 0; type < HIST_COUNT; ++type)
6173 {
6174 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6175 if (num_saved == 0)
6176 continue;
6177 if (num_saved < 0) /* Use default */
6178 num_saved = hislen;
6179 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6180 type == HIST_CMD ? _("Command Line") :
6181 type == HIST_SEARCH ? _("Search String") :
6182 type == HIST_EXPR ? _("Expression") :
6183 _("Input Line"));
6184 if (num_saved > hislen)
6185 num_saved = hislen;
6186 i = hisidx[type];
6187 if (i >= 0)
6188 while (num_saved--)
6189 {
6190 p = history[type][i].hisstr;
6191 if (p != NULL)
6192 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006193 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 /* For the search history: put the separator in the second
6195 * column; use a space if there isn't one. */
6196 if (type == HIST_SEARCH)
6197 {
6198 c = p[STRLEN(p) + 1];
6199 putc(c == NUL ? ' ' : c, fp);
6200 }
6201 viminfo_writestring(fp, p);
6202 }
6203 if (--i < 0)
6204 i = hislen - 1;
6205 }
6206 }
6207}
6208#endif /* FEAT_VIMINFO */
6209
6210#if defined(FEAT_FKMAP) || defined(PROTO)
6211/*
6212 * Write a character at the current cursor+offset position.
6213 * It is directly written into the command buffer block.
6214 */
6215 void
6216cmd_pchar(c, offset)
6217 int c, offset;
6218{
6219 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6220 {
6221 EMSG(_("E198: cmd_pchar beyond the command length"));
6222 return;
6223 }
6224 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6225 ccline.cmdbuff[ccline.cmdlen] = NUL;
6226}
6227
6228 int
6229cmd_gchar(offset)
6230 int offset;
6231{
6232 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6233 {
6234 /* EMSG(_("cmd_gchar beyond the command length")); */
6235 return NUL;
6236 }
6237 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6238}
6239#endif
6240
6241#if defined(FEAT_CMDWIN) || defined(PROTO)
6242/*
6243 * Open a window on the current command line and history. Allow editing in
6244 * the window. Returns when the window is closed.
6245 * Returns:
6246 * CR if the command is to be executed
6247 * Ctrl_C if it is to be abandoned
6248 * K_IGNORE if editing continues
6249 */
6250 static int
6251ex_window()
6252{
6253 struct cmdline_info save_ccline;
6254 buf_T *old_curbuf = curbuf;
6255 win_T *old_curwin = curwin;
6256 buf_T *bp;
6257 win_T *wp;
6258 int i;
6259 linenr_T lnum;
6260 int histtype;
6261 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006262#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 int save_restart_edit = restart_edit;
6266 int save_State = State;
6267 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006268#ifdef FEAT_RIGHTLEFT
6269 int save_cmdmsg_rl = cmdmsg_rl;
6270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271
6272 /* Can't do this recursively. Can't do it when typing a password. */
6273 if (cmdwin_type != 0
6274# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6275 || cmdline_star > 0
6276# endif
6277 )
6278 {
6279 beep_flush();
6280 return K_IGNORE;
6281 }
6282
6283 /* Save current window sizes. */
6284 win_size_save(&winsizes);
6285
6286# ifdef FEAT_AUTOCMD
6287 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006288 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006290 /* don't use a new tab page */
6291 cmdmod.tab = 0;
6292
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 /* Create a window for the command-line buffer. */
6294 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6295 {
6296 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006297# ifdef FEAT_AUTOCMD
6298 unblock_autocmds();
6299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 return K_IGNORE;
6301 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006302 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303
6304 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006305 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006306 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6308 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6309 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006310#ifdef FEAT_FOLDING
6311 curwin->w_p_fen = FALSE;
6312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006314 curwin->w_p_rl = cmdmsg_rl;
6315 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006317 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
6319# ifdef FEAT_AUTOCMD
6320 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006321 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322# endif
6323
Bram Moolenaar46152342005-09-07 21:18:43 +00006324 /* Showing the prompt may have set need_wait_return, reset it. */
6325 need_wait_return = FALSE;
6326
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006327 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6329 {
6330 if (p_wc == TAB)
6331 {
6332 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6333 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6334 }
6335 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6336 }
6337
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006338 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6339 * sets 'textwidth' to 78). */
6340 curbuf->b_p_tw = 0;
6341
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 /* Fill the buffer with the history. */
6343 init_history();
6344 if (hislen > 0)
6345 {
6346 i = hisidx[histtype];
6347 if (i >= 0)
6348 {
6349 lnum = 0;
6350 do
6351 {
6352 if (++i == hislen)
6353 i = 0;
6354 if (history[histtype][i].hisstr != NULL)
6355 ml_append(lnum++, history[histtype][i].hisstr,
6356 (colnr_T)0, FALSE);
6357 }
6358 while (i != hisidx[histtype]);
6359 }
6360 }
6361
6362 /* Replace the empty last line with the current command-line and put the
6363 * cursor there. */
6364 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6365 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6366 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006367 changed_line_abv_curs();
6368 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006369 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370
6371 /* Save the command line info, can be used recursively. */
6372 save_ccline = ccline;
6373 ccline.cmdbuff = NULL;
6374 ccline.cmdprompt = NULL;
6375
6376 /* No Ex mode here! */
6377 exmode_active = 0;
6378
6379 State = NORMAL;
6380# ifdef FEAT_MOUSE
6381 setmouse();
6382# endif
6383
6384# ifdef FEAT_AUTOCMD
6385 /* Trigger CmdwinEnter autocommands. */
6386 typestr[0] = cmdwin_type;
6387 typestr[1] = NUL;
6388 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006389 if (restart_edit != 0) /* autocmd with ":startinsert" */
6390 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391# endif
6392
6393 i = RedrawingDisabled;
6394 RedrawingDisabled = 0;
6395
6396 /*
6397 * Call the main loop until <CR> or CTRL-C is typed.
6398 */
6399 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006400 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402 RedrawingDisabled = i;
6403
6404# ifdef FEAT_AUTOCMD
6405 /* Trigger CmdwinLeave autocommands. */
6406 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6407# endif
6408
Bram Moolenaarccc18222007-05-10 18:25:20 +00006409 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 ccline = save_ccline;
6411 cmdwin_type = 0;
6412
6413 exmode_active = save_exmode;
6414
Bram Moolenaarf9821062008-06-20 16:31:07 +00006415 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 * this happens! */
6417 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6418 {
6419 cmdwin_result = Ctrl_C;
6420 EMSG(_("E199: Active window or buffer deleted"));
6421 }
6422 else
6423 {
6424# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6425 /* autocmds may abort script processing */
6426 if (aborting() && cmdwin_result != K_IGNORE)
6427 cmdwin_result = Ctrl_C;
6428# endif
6429 /* Set the new command line from the cmdline buffer. */
6430 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006431 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006433 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6434
6435 if (histtype == HIST_CMD)
6436 {
6437 /* Execute the command directly. */
6438 ccline.cmdbuff = vim_strsave((char_u *)p);
6439 cmdwin_result = CAR;
6440 }
6441 else
6442 {
6443 /* First need to cancel what we were doing. */
6444 ccline.cmdbuff = NULL;
6445 stuffcharReadbuff(':');
6446 stuffReadbuff((char_u *)p);
6447 stuffcharReadbuff(CAR);
6448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 }
6450 else if (cmdwin_result == K_XF2) /* :qa typed */
6451 {
6452 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6453 cmdwin_result = CAR;
6454 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006455 else if (cmdwin_result == Ctrl_C)
6456 {
6457 /* :q or :close, don't execute any command
6458 * and don't modify the cmd window. */
6459 ccline.cmdbuff = NULL;
6460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 else
6462 ccline.cmdbuff = vim_strsave(ml_get_curline());
6463 if (ccline.cmdbuff == NULL)
6464 cmdwin_result = Ctrl_C;
6465 else
6466 {
6467 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6468 ccline.cmdbufflen = ccline.cmdlen + 1;
6469 ccline.cmdpos = curwin->w_cursor.col;
6470 if (ccline.cmdpos > ccline.cmdlen)
6471 ccline.cmdpos = ccline.cmdlen;
6472 if (cmdwin_result == K_IGNORE)
6473 {
6474 set_cmdspos_cursor();
6475 redrawcmd();
6476 }
6477 }
6478
6479# ifdef FEAT_AUTOCMD
6480 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006481 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482# endif
6483 wp = curwin;
6484 bp = curbuf;
6485 win_goto(old_curwin);
6486 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006487
6488 /* win_close() may have already wiped the buffer when 'bh' is
6489 * set to 'wipe' */
6490 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006491 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
6493 /* Restore window sizes. */
6494 win_size_restore(&winsizes);
6495
6496# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006497 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498# endif
6499 }
6500
6501 ga_clear(&winsizes);
6502 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006503# ifdef FEAT_RIGHTLEFT
6504 cmdmsg_rl = save_cmdmsg_rl;
6505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507 State = save_State;
6508# ifdef FEAT_MOUSE
6509 setmouse();
6510# endif
6511
6512 return cmdwin_result;
6513}
6514#endif /* FEAT_CMDWIN */
6515
6516/*
6517 * Used for commands that either take a simple command string argument, or:
6518 * cmd << endmarker
6519 * {script}
6520 * endmarker
6521 * Returns a pointer to allocated memory with {script} or NULL.
6522 */
6523 char_u *
6524script_get(eap, cmd)
6525 exarg_T *eap;
6526 char_u *cmd;
6527{
6528 char_u *theline;
6529 char *end_pattern = NULL;
6530 char dot[] = ".";
6531 garray_T ga;
6532
6533 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6534 return NULL;
6535
6536 ga_init2(&ga, 1, 0x400);
6537
6538 if (cmd[2] != NUL)
6539 end_pattern = (char *)skipwhite(cmd + 2);
6540 else
6541 end_pattern = dot;
6542
6543 for (;;)
6544 {
6545 theline = eap->getline(
6546#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006547 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548#endif
6549 NUL, eap->cookie, 0);
6550
6551 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006552 {
6553 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556
6557 ga_concat(&ga, theline);
6558 ga_append(&ga, '\n');
6559 vim_free(theline);
6560 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006561 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562
6563 return (char_u *)ga.ga_data;
6564}