blob: 9cf1287a4847334474f835261fb8b771bde084f9 [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 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02002271 long sw = get_sw_value();
2272
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002273 p = (char_u *)line_ga.ga_data;
2274 p[line_ga.ga_len] = NUL;
2275 indent = get_indent_str(p, 8);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002276 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002277add_indent:
2278 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002280 char_u *s = skipwhite(p);
2281
2282 ga_grow(&line_ga, 1);
2283 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2284 *s = ' ';
2285 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002287redraw:
2288 /* redraw the line */
2289 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002290 vcol = 0;
2291 for (p = (char_u *)line_ga.ga_data;
2292 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002294 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002296 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002298 msg_putchar(' ');
2299 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002301 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002303 msg_outtrans_len(p, 1);
2304 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
2306 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002307 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002308 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002309 continue;
2310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002312 if (c1 == Ctrl_D)
2313 {
2314 /* Delete one shiftwidth. */
2315 p = (char_u *)line_ga.ga_data;
2316 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002318 if (prev_char == '^')
2319 ex_keep_indent = TRUE;
2320 indent = 0;
2321 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 }
2323 else
2324 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002325 p[line_ga.ga_len] = NUL;
2326 indent = get_indent_str(p, 8);
2327 --indent;
Bram Moolenaar14f24742012-08-08 18:01:05 +02002328 indent -= indent % get_sw_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002330 while (get_indent_str(p, 8) > indent)
2331 {
2332 char_u *s = skipwhite(p);
2333
2334 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2335 --line_ga.ga_len;
2336 }
2337 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002339
2340 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2341 {
2342 escaped = TRUE;
2343 continue;
2344 }
2345
2346 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2347 if (IS_SPECIAL(c1))
2348 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002350
2351 if (IS_SPECIAL(c1))
2352 c1 = '?';
2353 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002354 if (c1 == '\n')
2355 msg_putchar('\n');
2356 else if (c1 == TAB)
2357 {
2358 /* Don't use chartabsize(), 'ts' can be different */
2359 do
2360 {
2361 msg_putchar(' ');
2362 } while (++vcol % 8);
2363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002366 msg_outtrans_len(
2367 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2368 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002370 ++line_ga.ga_len;
2371 escaped = FALSE;
2372
2373 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002374 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002375
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002376 /* We are done when a NL is entered, but not when it comes after an
2377 * odd number of backslashes, that results in a NUL. */
2378 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002380 int bcount = 0;
2381
2382 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2383 ++bcount;
2384
2385 if (bcount > 0)
2386 {
2387 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2388 * "\NL", etc. */
2389 line_ga.ga_len -= (bcount + 1) / 2;
2390 pend -= (bcount + 1) / 2;
2391 pend[-1] = '\n';
2392 }
2393
2394 if ((bcount & 1) == 0)
2395 {
2396 --line_ga.ga_len;
2397 --pend;
2398 *pend = NUL;
2399 break;
2400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402 }
2403
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002404 --no_mapping;
2405 --allow_keys;
2406
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 /* make following messages go to the next line */
2408 msg_didout = FALSE;
2409 msg_col = 0;
2410 if (msg_row < Rows - 1)
2411 ++msg_row;
2412 emsg_on_display = FALSE; /* don't want ui_delay() */
2413
2414 if (got_int)
2415 ga_clear(&line_ga);
2416
2417 return (char_u *)line_ga.ga_data;
2418}
2419
Bram Moolenaare344bea2005-09-01 20:46:49 +00002420# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2421 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422/*
2423 * Return TRUE if ccline.overstrike is on.
2424 */
2425 int
2426cmdline_overstrike()
2427{
2428 return ccline.overstrike;
2429}
2430
2431/*
2432 * Return TRUE if the cursor is at the end of the cmdline.
2433 */
2434 int
2435cmdline_at_end()
2436{
2437 return (ccline.cmdpos >= ccline.cmdlen);
2438}
2439#endif
2440
Bram Moolenaar9372a112005-12-06 19:59:18 +00002441#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442/*
2443 * Return the virtual column number at the current cursor position.
2444 * This is used by the IM code to obtain the start of the preedit string.
2445 */
2446 colnr_T
2447cmdline_getvcol_cursor()
2448{
2449 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2450 return MAXCOL;
2451
2452# ifdef FEAT_MBYTE
2453 if (has_mbyte)
2454 {
2455 colnr_T col;
2456 int i = 0;
2457
2458 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002459 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460
2461 return col;
2462 }
2463 else
2464# endif
2465 return ccline.cmdpos;
2466}
2467#endif
2468
2469#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2470/*
2471 * If part of the command line is an IM preedit string, redraw it with
2472 * IM feedback attributes. The cursor position is restored after drawing.
2473 */
2474 static void
2475redrawcmd_preedit()
2476{
2477 if ((State & CMDLINE)
2478 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002479 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 && !p_imdisable
2481 && im_is_preediting())
2482 {
2483 int cmdpos = 0;
2484 int cmdspos;
2485 int old_row;
2486 int old_col;
2487 colnr_T col;
2488
2489 old_row = msg_row;
2490 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002491 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493# ifdef FEAT_MBYTE
2494 if (has_mbyte)
2495 {
2496 for (col = 0; col < preedit_start_col
2497 && cmdpos < ccline.cmdlen; ++col)
2498 {
2499 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002500 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 }
2502 }
2503 else
2504# endif
2505 {
2506 cmdspos += preedit_start_col;
2507 cmdpos += preedit_start_col;
2508 }
2509
2510 msg_row = cmdline_row + (cmdspos / (int)Columns);
2511 msg_col = cmdspos % (int)Columns;
2512 if (msg_row >= Rows)
2513 msg_row = Rows - 1;
2514
2515 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2516 {
2517 int char_len;
2518 int char_attr;
2519
2520 char_attr = im_get_feedback_attr(col);
2521 if (char_attr < 0)
2522 break; /* end of preedit string */
2523
2524# ifdef FEAT_MBYTE
2525 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002526 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 else
2528# endif
2529 char_len = 1;
2530
2531 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2532 cmdpos += char_len;
2533 }
2534
2535 msg_row = old_row;
2536 msg_col = old_col;
2537 }
2538}
2539#endif /* FEAT_XIM && FEAT_GUI_GTK */
2540
2541/*
2542 * Allocate a new command line buffer.
2543 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2544 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2545 */
2546 static void
2547alloc_cmdbuff(len)
2548 int len;
2549{
2550 /*
2551 * give some extra space to avoid having to allocate all the time
2552 */
2553 if (len < 80)
2554 len = 100;
2555 else
2556 len += 20;
2557
2558 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2559 ccline.cmdbufflen = len;
2560}
2561
2562/*
2563 * Re-allocate the command line to length len + something extra.
2564 * return FAIL for failure, OK otherwise
2565 */
2566 static int
2567realloc_cmdbuff(len)
2568 int len;
2569{
2570 char_u *p;
2571
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002572 if (len < ccline.cmdbufflen)
2573 return OK; /* no need to resize */
2574
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 p = ccline.cmdbuff;
2576 alloc_cmdbuff(len); /* will get some more */
2577 if (ccline.cmdbuff == NULL) /* out of memory */
2578 {
2579 ccline.cmdbuff = p; /* keep the old one */
2580 return FAIL;
2581 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002582 /* There isn't always a NUL after the command, but it may need to be
2583 * there, thus copy up to the NUL and add a NUL. */
2584 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2585 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002587
2588 if (ccline.xpc != NULL
2589 && ccline.xpc->xp_pattern != NULL
2590 && ccline.xpc->xp_context != EXPAND_NOTHING
2591 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2592 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002593 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002594
2595 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2596 * to point into the newly allocated memory. */
2597 if (i >= 0 && i <= ccline.cmdlen)
2598 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2599 }
2600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 return OK;
2602}
2603
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002604#if defined(FEAT_ARABIC) || defined(PROTO)
2605static char_u *arshape_buf = NULL;
2606
2607# if defined(EXITFREE) || defined(PROTO)
2608 void
2609free_cmdline_buf()
2610{
2611 vim_free(arshape_buf);
2612}
2613# endif
2614#endif
2615
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616/*
2617 * Draw part of the cmdline at the current cursor position. But draw stars
2618 * when cmdline_star is TRUE.
2619 */
2620 static void
2621draw_cmdline(start, len)
2622 int start;
2623 int len;
2624{
2625#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2626 int i;
2627
2628 if (cmdline_star > 0)
2629 for (i = 0; i < len; ++i)
2630 {
2631 msg_putchar('*');
2632# ifdef FEAT_MBYTE
2633 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002634 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635# endif
2636 }
2637 else
2638#endif
2639#ifdef FEAT_ARABIC
2640 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2641 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 static int buflen = 0;
2643 char_u *p;
2644 int j;
2645 int newlen = 0;
2646 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002647 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 int prev_c = 0;
2649 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002650 int u8c;
2651 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 /*
2655 * Do arabic shaping into a temporary buffer. This is very
2656 * inefficient!
2657 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002658 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {
2660 /* Re-allocate the buffer. We keep it around to avoid a lot of
2661 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002662 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002663 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002664 arshape_buf = alloc(buflen);
2665 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 return; /* out of memory */
2667 }
2668
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002669 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2670 {
2671 /* Prepend a space to draw the leading composing char on. */
2672 arshape_buf[0] = ' ';
2673 newlen = 1;
2674 }
2675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 for (j = start; j < start + len; j += mb_l)
2677 {
2678 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002679 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002680 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 if (ARABIC_CHAR(u8c))
2682 {
2683 /* Do Arabic shaping. */
2684 if (cmdmsg_rl)
2685 {
2686 /* displaying from right to left */
2687 pc = prev_c;
2688 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002689 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 if (j + mb_l >= start + len)
2691 nc = NUL;
2692 else
2693 nc = utf_ptr2char(p + mb_l);
2694 }
2695 else
2696 {
2697 /* displaying from left to right */
2698 if (j + mb_l >= start + len)
2699 pc = NUL;
2700 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002701 {
2702 int pcc[MAX_MCO];
2703
2704 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002706 pc1 = pcc[0];
2707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 nc = prev_c;
2709 }
2710 prev_c = u8c;
2711
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002712 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002714 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002715 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002717 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2718 if (u8cc[1] != 0)
2719 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002720 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 }
2722 }
2723 else
2724 {
2725 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002726 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 newlen += mb_l;
2728 }
2729 }
2730
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002731 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733 else
2734#endif
2735 msg_outtrans_len(ccline.cmdbuff + start, len);
2736}
2737
2738/*
2739 * Put a character on the command line. Shifts the following text to the
2740 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2741 * "c" must be printable (fit in one display cell)!
2742 */
2743 void
2744putcmdline(c, shift)
2745 int c;
2746 int shift;
2747{
2748 if (cmd_silent)
2749 return;
2750 msg_no_more = TRUE;
2751 msg_putchar(c);
2752 if (shift)
2753 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2754 msg_no_more = FALSE;
2755 cursorcmd();
2756}
2757
2758/*
2759 * Undo a putcmdline(c, FALSE).
2760 */
2761 void
2762unputcmdline()
2763{
2764 if (cmd_silent)
2765 return;
2766 msg_no_more = TRUE;
2767 if (ccline.cmdlen == ccline.cmdpos)
2768 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002769#ifdef FEAT_MBYTE
2770 else if (has_mbyte)
2771 draw_cmdline(ccline.cmdpos,
2772 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 else
2775 draw_cmdline(ccline.cmdpos, 1);
2776 msg_no_more = FALSE;
2777 cursorcmd();
2778}
2779
2780/*
2781 * Put the given string, of the given length, onto the command line.
2782 * If len is -1, then STRLEN() is used to calculate the length.
2783 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2784 * part will be redrawn, otherwise it will not. If this function is called
2785 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2786 * called afterwards.
2787 */
2788 int
2789put_on_cmdline(str, len, redraw)
2790 char_u *str;
2791 int len;
2792 int redraw;
2793{
2794 int retval;
2795 int i;
2796 int m;
2797 int c;
2798
2799 if (len < 0)
2800 len = (int)STRLEN(str);
2801
2802 /* Check if ccline.cmdbuff needs to be longer */
2803 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002804 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 else
2806 retval = OK;
2807 if (retval == OK)
2808 {
2809 if (!ccline.overstrike)
2810 {
2811 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2812 ccline.cmdbuff + ccline.cmdpos,
2813 (size_t)(ccline.cmdlen - ccline.cmdpos));
2814 ccline.cmdlen += len;
2815 }
2816 else
2817 {
2818#ifdef FEAT_MBYTE
2819 if (has_mbyte)
2820 {
2821 /* Count nr of characters in the new string. */
2822 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002823 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 ++m;
2825 /* Count nr of bytes in cmdline that are overwritten by these
2826 * characters. */
2827 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002828 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 --m;
2830 if (i < ccline.cmdlen)
2831 {
2832 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2833 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2834 ccline.cmdlen += ccline.cmdpos + len - i;
2835 }
2836 else
2837 ccline.cmdlen = ccline.cmdpos + len;
2838 }
2839 else
2840#endif
2841 if (ccline.cmdpos + len > ccline.cmdlen)
2842 ccline.cmdlen = ccline.cmdpos + len;
2843 }
2844 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2845 ccline.cmdbuff[ccline.cmdlen] = NUL;
2846
2847#ifdef FEAT_MBYTE
2848 if (enc_utf8)
2849 {
2850 /* When the inserted text starts with a composing character,
2851 * backup to the character before it. There could be two of them.
2852 */
2853 i = 0;
2854 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2855 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2856 {
2857 i = (*mb_head_off)(ccline.cmdbuff,
2858 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2859 ccline.cmdpos -= i;
2860 len += i;
2861 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2862 }
2863# ifdef FEAT_ARABIC
2864 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2865 {
2866 /* Check the previous character for Arabic combining pair. */
2867 i = (*mb_head_off)(ccline.cmdbuff,
2868 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2869 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2870 + ccline.cmdpos - i), c))
2871 {
2872 ccline.cmdpos -= i;
2873 len += i;
2874 }
2875 else
2876 i = 0;
2877 }
2878# endif
2879 if (i != 0)
2880 {
2881 /* Also backup the cursor position. */
2882 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2883 ccline.cmdspos -= i;
2884 msg_col -= i;
2885 if (msg_col < 0)
2886 {
2887 msg_col += Columns;
2888 --msg_row;
2889 }
2890 }
2891 }
2892#endif
2893
2894 if (redraw && !cmd_silent)
2895 {
2896 msg_no_more = TRUE;
2897 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002898 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2900 /* Avoid clearing the rest of the line too often. */
2901 if (cmdline_row != i || ccline.overstrike)
2902 msg_clr_eos();
2903 msg_no_more = FALSE;
2904 }
2905#ifdef FEAT_FKMAP
2906 /*
2907 * If we are in Farsi command mode, the character input must be in
2908 * Insert mode. So do not advance the cmdpos.
2909 */
2910 if (!cmd_fkmap)
2911#endif
2912 {
2913 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002916 if (m < 0) /* overflow, Columns or Rows at weird value */
2917 m = MAXCOL;
2918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 else
2920 m = MAXCOL;
2921 for (i = 0; i < len; ++i)
2922 {
2923 c = cmdline_charsize(ccline.cmdpos);
2924#ifdef FEAT_MBYTE
2925 /* count ">" for a double-wide char that doesn't fit. */
2926 if (has_mbyte)
2927 correct_cmdspos(ccline.cmdpos, c);
2928#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002929 /* Stop cursor at the end of the screen, but do increment the
2930 * insert position, so that entering a very long command
2931 * works, even though you can't see it. */
2932 if (ccline.cmdspos + c < m)
2933 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934#ifdef FEAT_MBYTE
2935 if (has_mbyte)
2936 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002937 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (c > len - i - 1)
2939 c = len - i - 1;
2940 ccline.cmdpos += c;
2941 i += c;
2942 }
2943#endif
2944 ++ccline.cmdpos;
2945 }
2946 }
2947 }
2948 if (redraw)
2949 msg_check();
2950 return retval;
2951}
2952
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002953static struct cmdline_info prev_ccline;
2954static int prev_ccline_used = FALSE;
2955
2956/*
2957 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2958 * and overwrite it. But get_cmdline_str() may need it, thus make it
2959 * available globally in prev_ccline.
2960 */
2961 static void
2962save_cmdline(ccp)
2963 struct cmdline_info *ccp;
2964{
2965 if (!prev_ccline_used)
2966 {
2967 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2968 prev_ccline_used = TRUE;
2969 }
2970 *ccp = prev_ccline;
2971 prev_ccline = ccline;
2972 ccline.cmdbuff = NULL;
2973 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002974 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002975}
2976
2977/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002978 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002979 */
2980 static void
2981restore_cmdline(ccp)
2982 struct cmdline_info *ccp;
2983{
2984 ccline = prev_ccline;
2985 prev_ccline = *ccp;
2986}
2987
Bram Moolenaar5a305422006-04-28 22:38:25 +00002988#if defined(FEAT_EVAL) || defined(PROTO)
2989/*
2990 * Save the command line into allocated memory. Returns a pointer to be
2991 * passed to restore_cmdline_alloc() later.
2992 * Returns NULL when failed.
2993 */
2994 char_u *
2995save_cmdline_alloc()
2996{
2997 struct cmdline_info *p;
2998
2999 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3000 if (p != NULL)
3001 save_cmdline(p);
3002 return (char_u *)p;
3003}
3004
3005/*
3006 * Restore the command line from the return value of save_cmdline_alloc().
3007 */
3008 void
3009restore_cmdline_alloc(p)
3010 char_u *p;
3011{
3012 if (p != NULL)
3013 {
3014 restore_cmdline((struct cmdline_info *)p);
3015 vim_free(p);
3016 }
3017}
3018#endif
3019
Bram Moolenaar8299df92004-07-10 09:47:34 +00003020/*
3021 * paste a yank register into the command line.
3022 * used by CTRL-R command in command-line mode
3023 * insert_reg() can't be used here, because special characters from the
3024 * register contents will be interpreted as commands.
3025 *
3026 * return FAIL for failure, OK otherwise
3027 */
3028 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003029cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003030 int regname;
3031 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003032 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003033{
3034 long i;
3035 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003036 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003037 int allocated;
3038 struct cmdline_info save_ccline;
3039
3040 /* check for valid regname; also accept special characters for CTRL-R in
3041 * the command line */
3042 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3043 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3044 return FAIL;
3045
3046 /* A register containing CTRL-R can cause an endless loop. Allow using
3047 * CTRL-C to break the loop. */
3048 line_breakcheck();
3049 if (got_int)
3050 return FAIL;
3051
3052#ifdef FEAT_CLIPBOARD
3053 regname = may_get_selection(regname);
3054#endif
3055
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003056 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003057 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003058 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003059 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003060 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003061 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003062 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003063
3064 if (i)
3065 {
3066 /* Got the value of a special register in "arg". */
3067 if (arg == NULL)
3068 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003069
3070 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3071 * part of the word. */
3072 p = arg;
3073 if (p_is && regname == Ctrl_W)
3074 {
3075 char_u *w;
3076 int len;
3077
3078 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003079 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003080 {
3081#ifdef FEAT_MBYTE
3082 if (has_mbyte)
3083 {
3084 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3085 if (!vim_iswordc(mb_ptr2char(w - len)))
3086 break;
3087 w -= len;
3088 }
3089 else
3090#endif
3091 {
3092 if (!vim_iswordc(w[-1]))
3093 break;
3094 --w;
3095 }
3096 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003097 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003098 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3099 p += len;
3100 }
3101
3102 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003103 if (allocated)
3104 vim_free(arg);
3105 return OK;
3106 }
3107
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003108 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003109}
3110
3111/*
3112 * Put a string on the command line.
3113 * When "literally" is TRUE, insert literally.
3114 * When "literally" is FALSE, insert as typed, but don't leave the command
3115 * line.
3116 */
3117 void
3118cmdline_paste_str(s, literally)
3119 char_u *s;
3120 int literally;
3121{
3122 int c, cv;
3123
3124 if (literally)
3125 put_on_cmdline(s, -1, TRUE);
3126 else
3127 while (*s != NUL)
3128 {
3129 cv = *s;
3130 if (cv == Ctrl_V && s[1])
3131 ++s;
3132#ifdef FEAT_MBYTE
3133 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003134 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003135 else
3136#endif
3137 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003138 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3139 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003140#ifdef UNIX
3141 || c == intr_char
3142#endif
3143 || (c == Ctrl_BSL && *s == Ctrl_N))
3144 stuffcharReadbuff(Ctrl_V);
3145 stuffcharReadbuff(c);
3146 }
3147}
3148
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149#ifdef FEAT_WILDMENU
3150/*
3151 * Delete characters on the command line, from "from" to the current
3152 * position.
3153 */
3154 static void
3155cmdline_del(from)
3156 int from;
3157{
3158 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3159 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3160 ccline.cmdlen -= ccline.cmdpos - from;
3161 ccline.cmdpos = from;
3162}
3163#endif
3164
3165/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003166 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 * search
3168 */
3169 void
3170redrawcmdline()
3171{
3172 if (cmd_silent)
3173 return;
3174 need_wait_return = FALSE;
3175 compute_cmdrow();
3176 redrawcmd();
3177 cursorcmd();
3178}
3179
3180 static void
3181redrawcmdprompt()
3182{
3183 int i;
3184
3185 if (cmd_silent)
3186 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003187 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 msg_putchar(ccline.cmdfirstc);
3189 if (ccline.cmdprompt != NULL)
3190 {
3191 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3192 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3193 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003194 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 --ccline.cmdindent;
3196 }
3197 else
3198 for (i = ccline.cmdindent; i > 0; --i)
3199 msg_putchar(' ');
3200}
3201
3202/*
3203 * Redraw what is currently on the command line.
3204 */
3205 void
3206redrawcmd()
3207{
3208 if (cmd_silent)
3209 return;
3210
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003211 /* when 'incsearch' is set there may be no command line while redrawing */
3212 if (ccline.cmdbuff == NULL)
3213 {
3214 windgoto(cmdline_row, 0);
3215 msg_clr_eos();
3216 return;
3217 }
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 msg_start();
3220 redrawcmdprompt();
3221
3222 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3223 msg_no_more = TRUE;
3224 draw_cmdline(0, ccline.cmdlen);
3225 msg_clr_eos();
3226 msg_no_more = FALSE;
3227
3228 set_cmdspos_cursor();
3229
3230 /*
3231 * An emsg() before may have set msg_scroll. This is used in normal mode,
3232 * in cmdline mode we can reset them now.
3233 */
3234 msg_scroll = FALSE; /* next message overwrites cmdline */
3235
3236 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3237 * in cmdline mode */
3238 skip_redraw = FALSE;
3239}
3240
3241 void
3242compute_cmdrow()
3243{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003244 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 cmdline_row = Rows - 1;
3246 else
3247 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3248 + W_STATUS_HEIGHT(lastwin);
3249}
3250
3251 static void
3252cursorcmd()
3253{
3254 if (cmd_silent)
3255 return;
3256
3257#ifdef FEAT_RIGHTLEFT
3258 if (cmdmsg_rl)
3259 {
3260 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3261 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3262 if (msg_row <= 0)
3263 msg_row = Rows - 1;
3264 }
3265 else
3266#endif
3267 {
3268 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3269 msg_col = ccline.cmdspos % (int)Columns;
3270 if (msg_row >= Rows)
3271 msg_row = Rows - 1;
3272 }
3273
3274 windgoto(msg_row, msg_col);
3275#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3276 redrawcmd_preedit();
3277#endif
3278#ifdef MCH_CURSOR_SHAPE
3279 mch_update_cursor();
3280#endif
3281}
3282
3283 void
3284gotocmdline(clr)
3285 int clr;
3286{
3287 msg_start();
3288#ifdef FEAT_RIGHTLEFT
3289 if (cmdmsg_rl)
3290 msg_col = Columns - 1;
3291 else
3292#endif
3293 msg_col = 0; /* always start in column 0 */
3294 if (clr) /* clear the bottom line(s) */
3295 msg_clr_eos(); /* will reset clear_cmdline */
3296 windgoto(cmdline_row, 0);
3297}
3298
3299/*
3300 * Check the word in front of the cursor for an abbreviation.
3301 * Called when the non-id character "c" has been entered.
3302 * When an abbreviation is recognized it is removed from the text with
3303 * backspaces and the replacement string is inserted, followed by "c".
3304 */
3305 static int
3306ccheck_abbr(c)
3307 int c;
3308{
3309 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3310 return FALSE;
3311
3312 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3313}
3314
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003315#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3316 static int
3317#ifdef __BORLANDC__
3318_RTLENTRYF
3319#endif
3320sort_func_compare(s1, s2)
3321 const void *s1;
3322 const void *s2;
3323{
3324 char_u *p1 = *(char_u **)s1;
3325 char_u *p2 = *(char_u **)s2;
3326
3327 if (*p1 != '<' && *p2 == '<') return -1;
3328 if (*p1 == '<' && *p2 != '<') return 1;
3329 return STRCMP(p1, p2);
3330}
3331#endif
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333/*
3334 * Return FAIL if this is not an appropriate context in which to do
3335 * completion of anything, return OK if it is (even if there are no matches).
3336 * For the caller, this means that the character is just passed through like a
3337 * normal character (instead of being expanded). This allows :s/^I^D etc.
3338 */
3339 static int
3340nextwild(xp, type, options)
3341 expand_T *xp;
3342 int type;
3343 int options; /* extra options for ExpandOne() */
3344{
3345 int i, j;
3346 char_u *p1;
3347 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 int difflen;
3349 int v;
3350
3351 if (xp->xp_numfiles == -1)
3352 {
3353 set_expand_context(xp);
3354 cmd_showtail = expand_showtail(xp);
3355 }
3356
3357 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3358 {
3359 beep_flush();
3360 return OK; /* Something illegal on command line */
3361 }
3362 if (xp->xp_context == EXPAND_NOTHING)
3363 {
3364 /* Caller can use the character as a normal char instead */
3365 return FAIL;
3366 }
3367
3368 MSG_PUTS("..."); /* show that we are busy */
3369 out_flush();
3370
3371 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003372 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
3374 if (type == WILD_NEXT || type == WILD_PREV)
3375 {
3376 /*
3377 * Get next/previous match for a previous expanded pattern.
3378 */
3379 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3380 }
3381 else
3382 {
3383 /*
3384 * Translate string into pattern and expand it.
3385 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003386 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3387 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 p2 = NULL;
3389 else
3390 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003391 int use_options = options |
3392 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE;
3393
3394 if (p_wic)
3395 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003396 p2 = ExpandOne(xp, p1,
3397 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003398 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003400 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (p2 != NULL && type == WILD_LONGEST)
3402 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003403 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (ccline.cmdbuff[i + j] == '*'
3405 || ccline.cmdbuff[i + j] == '?')
3406 break;
3407 if ((int)STRLEN(p2) < j)
3408 {
3409 vim_free(p2);
3410 p2 = NULL;
3411 }
3412 }
3413 }
3414 }
3415
3416 if (p2 != NULL && !got_int)
3417 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003418 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003419 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003421 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 xp->xp_pattern = ccline.cmdbuff + i;
3423 }
3424 else
3425 v = OK;
3426 if (v == OK)
3427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003428 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3429 &ccline.cmdbuff[ccline.cmdpos],
3430 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3431 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 ccline.cmdlen += difflen;
3433 ccline.cmdpos += difflen;
3434 }
3435 }
3436 vim_free(p2);
3437
3438 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003439 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440
3441 /* When expanding a ":map" command and no matches are found, assume that
3442 * the key is supposed to be inserted literally */
3443 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3444 return FAIL;
3445
3446 if (xp->xp_numfiles <= 0 && p2 == NULL)
3447 beep_flush();
3448 else if (xp->xp_numfiles == 1)
3449 /* free expanded pattern */
3450 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3451
3452 return OK;
3453}
3454
3455/*
3456 * Do wildcard expansion on the string 'str'.
3457 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003458 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 * Return NULL for failure.
3460 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003461 * "orig" is the originally expanded string, copied to allocated memory. It
3462 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3463 * WILD_PREV "orig" should be NULL.
3464 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003465 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3466 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 *
3468 * mode = WILD_FREE: just free previously expanded matches
3469 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3470 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3471 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3472 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3473 * mode = WILD_ALL: return all matches concatenated
3474 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003475 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 *
3477 * options = WILD_LIST_NOTFOUND: list entries without a match
3478 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3479 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3480 * options = WILD_NO_BEEP: Don't beep for multiple matches
3481 * options = WILD_ADD_SLASH: add a slash after directory names
3482 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3483 * options = WILD_SILENT: don't print warning messages
3484 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003485 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 *
3487 * The variables xp->xp_context and xp->xp_backslash must have been set!
3488 */
3489 char_u *
3490ExpandOne(xp, str, orig, options, mode)
3491 expand_T *xp;
3492 char_u *str;
3493 char_u *orig; /* allocated copy of original of expanded string */
3494 int options;
3495 int mode;
3496{
3497 char_u *ss = NULL;
3498 static int findex;
3499 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003500 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 int i;
3502 long_u len;
3503 int non_suf_match; /* number without matching suffix */
3504
3505 /*
3506 * first handle the case of using an old match
3507 */
3508 if (mode == WILD_NEXT || mode == WILD_PREV)
3509 {
3510 if (xp->xp_numfiles > 0)
3511 {
3512 if (mode == WILD_PREV)
3513 {
3514 if (findex == -1)
3515 findex = xp->xp_numfiles;
3516 --findex;
3517 }
3518 else /* mode == WILD_NEXT */
3519 ++findex;
3520
3521 /*
3522 * When wrapping around, return the original string, set findex to
3523 * -1.
3524 */
3525 if (findex < 0)
3526 {
3527 if (orig_save == NULL)
3528 findex = xp->xp_numfiles - 1;
3529 else
3530 findex = -1;
3531 }
3532 if (findex >= xp->xp_numfiles)
3533 {
3534 if (orig_save == NULL)
3535 findex = 0;
3536 else
3537 findex = -1;
3538 }
3539#ifdef FEAT_WILDMENU
3540 if (p_wmnu)
3541 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3542 findex, cmd_showtail);
3543#endif
3544 if (findex == -1)
3545 return vim_strsave(orig_save);
3546 return vim_strsave(xp->xp_files[findex]);
3547 }
3548 else
3549 return NULL;
3550 }
3551
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003552 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3554 {
3555 FreeWild(xp->xp_numfiles, xp->xp_files);
3556 xp->xp_numfiles = -1;
3557 vim_free(orig_save);
3558 orig_save = NULL;
3559 }
3560 findex = 0;
3561
3562 if (mode == WILD_FREE) /* only release file name */
3563 return NULL;
3564
3565 if (xp->xp_numfiles == -1)
3566 {
3567 vim_free(orig_save);
3568 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003569 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
3571 /*
3572 * Do the expansion.
3573 */
3574 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3575 options) == FAIL)
3576 {
3577#ifdef FNAME_ILLEGAL
3578 /* Illegal file name has been silently skipped. But when there
3579 * are wildcards, the real problem is that there was no match,
3580 * causing the pattern to be added, which has illegal characters.
3581 */
3582 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3583 EMSG2(_(e_nomatch2), str);
3584#endif
3585 }
3586 else if (xp->xp_numfiles == 0)
3587 {
3588 if (!(options & WILD_SILENT))
3589 EMSG2(_(e_nomatch2), str);
3590 }
3591 else
3592 {
3593 /* Escape the matches for use on the command line. */
3594 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3595
3596 /*
3597 * Check for matching suffixes in file names.
3598 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003599 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3600 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
3602 if (xp->xp_numfiles)
3603 non_suf_match = xp->xp_numfiles;
3604 else
3605 non_suf_match = 1;
3606 if ((xp->xp_context == EXPAND_FILES
3607 || xp->xp_context == EXPAND_DIRECTORIES)
3608 && xp->xp_numfiles > 1)
3609 {
3610 /*
3611 * More than one match; check suffix.
3612 * The files will have been sorted on matching suffix in
3613 * expand_wildcards, only need to check the first two.
3614 */
3615 non_suf_match = 0;
3616 for (i = 0; i < 2; ++i)
3617 if (match_suffix(xp->xp_files[i]))
3618 ++non_suf_match;
3619 }
3620 if (non_suf_match != 1)
3621 {
3622 /* Can we ever get here unless it's while expanding
3623 * interactively? If not, we can get rid of this all
3624 * together. Don't really want to wait for this message
3625 * (and possibly have to hit return to continue!).
3626 */
3627 if (!(options & WILD_SILENT))
3628 EMSG(_(e_toomany));
3629 else if (!(options & WILD_NO_BEEP))
3630 beep_flush();
3631 }
3632 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3633 ss = vim_strsave(xp->xp_files[0]);
3634 }
3635 }
3636 }
3637
3638 /* Find longest common part */
3639 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3640 {
3641 for (len = 0; xp->xp_files[0][len]; ++len)
3642 {
3643 for (i = 0; i < xp->xp_numfiles; ++i)
3644 {
3645#ifdef CASE_INSENSITIVE_FILENAME
3646 if (xp->xp_context == EXPAND_DIRECTORIES
3647 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003648 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 || xp->xp_context == EXPAND_BUFFERS)
3650 {
3651 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3652 TOLOWER_LOC(xp->xp_files[0][len]))
3653 break;
3654 }
3655 else
3656#endif
3657 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3658 break;
3659 }
3660 if (i < xp->xp_numfiles)
3661 {
3662 if (!(options & WILD_NO_BEEP))
3663 vim_beep();
3664 break;
3665 }
3666 }
3667 ss = alloc((unsigned)len + 1);
3668 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003669 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 findex = -1; /* next p_wc gets first one */
3671 }
3672
3673 /* Concatenate all matching names */
3674 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3675 {
3676 len = 0;
3677 for (i = 0; i < xp->xp_numfiles; ++i)
3678 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3679 ss = lalloc(len, TRUE);
3680 if (ss != NULL)
3681 {
3682 *ss = NUL;
3683 for (i = 0; i < xp->xp_numfiles; ++i)
3684 {
3685 STRCAT(ss, xp->xp_files[i]);
3686 if (i != xp->xp_numfiles - 1)
3687 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3688 }
3689 }
3690 }
3691
3692 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3693 ExpandCleanup(xp);
3694
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003695 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003696 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003697 vim_free(orig);
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 return ss;
3700}
3701
3702/*
3703 * Prepare an expand structure for use.
3704 */
3705 void
3706ExpandInit(xp)
3707 expand_T *xp;
3708{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003709 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003710 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003712#ifndef BACKSLASH_IN_FILENAME
3713 xp->xp_shell = FALSE;
3714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 xp->xp_numfiles = -1;
3716 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003717#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3718 xp->xp_arg = NULL;
3719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720}
3721
3722/*
3723 * Cleanup an expand structure after use.
3724 */
3725 void
3726ExpandCleanup(xp)
3727 expand_T *xp;
3728{
3729 if (xp->xp_numfiles >= 0)
3730 {
3731 FreeWild(xp->xp_numfiles, xp->xp_files);
3732 xp->xp_numfiles = -1;
3733 }
3734}
3735
3736 void
3737ExpandEscape(xp, str, numfiles, files, options)
3738 expand_T *xp;
3739 char_u *str;
3740 int numfiles;
3741 char_u **files;
3742 int options;
3743{
3744 int i;
3745 char_u *p;
3746
3747 /*
3748 * May change home directory back to "~"
3749 */
3750 if (options & WILD_HOME_REPLACE)
3751 tilde_replace(str, numfiles, files);
3752
3753 if (options & WILD_ESCAPE)
3754 {
3755 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003756 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003757 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 || xp->xp_context == EXPAND_BUFFERS
3759 || xp->xp_context == EXPAND_DIRECTORIES)
3760 {
3761 /*
3762 * Insert a backslash into a file name before a space, \, %, #
3763 * and wildmatch characters, except '~'.
3764 */
3765 for (i = 0; i < numfiles; ++i)
3766 {
3767 /* for ":set path=" we need to escape spaces twice */
3768 if (xp->xp_backslash == XP_BS_THREE)
3769 {
3770 p = vim_strsave_escaped(files[i], (char_u *)" ");
3771 if (p != NULL)
3772 {
3773 vim_free(files[i]);
3774 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003775#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 p = vim_strsave_escaped(files[i], (char_u *)" ");
3777 if (p != NULL)
3778 {
3779 vim_free(files[i]);
3780 files[i] = p;
3781 }
3782#endif
3783 }
3784 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003785#ifdef BACKSLASH_IN_FILENAME
3786 p = vim_strsave_fnameescape(files[i], FALSE);
3787#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003788 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (p != NULL)
3791 {
3792 vim_free(files[i]);
3793 files[i] = p;
3794 }
3795
3796 /* If 'str' starts with "\~", replace "~" at start of
3797 * files[i] with "\~". */
3798 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003799 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 }
3801 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003802
3803 /* If the first file starts with a '+' escape it. Otherwise it
3804 * could be seen as "+cmd". */
3805 if (*files[0] == '+')
3806 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
3808 else if (xp->xp_context == EXPAND_TAGS)
3809 {
3810 /*
3811 * Insert a backslash before characters in a tag name that
3812 * would terminate the ":tag" command.
3813 */
3814 for (i = 0; i < numfiles; ++i)
3815 {
3816 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3817 if (p != NULL)
3818 {
3819 vim_free(files[i]);
3820 files[i] = p;
3821 }
3822 }
3823 }
3824 }
3825}
3826
3827/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003828 * Escape special characters in "fname" for when used as a file name argument
3829 * after a Vim command, or, when "shell" is non-zero, a shell command.
3830 * Returns the result in allocated memory.
3831 */
3832 char_u *
3833vim_strsave_fnameescape(fname, shell)
3834 char_u *fname;
3835 int shell;
3836{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003837 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003838#ifdef BACKSLASH_IN_FILENAME
3839 char_u buf[20];
3840 int j = 0;
3841
3842 /* Don't escape '[' and '{' if they are in 'isfname'. */
3843 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3844 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3845 buf[j++] = *p;
3846 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003847 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003848#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003849 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3850 if (shell && csh_like_shell() && p != NULL)
3851 {
3852 char_u *s;
3853
3854 /* For csh and similar shells need to put two backslashes before '!'.
3855 * One is taken by Vim, one by the shell. */
3856 s = vim_strsave_escaped(p, (char_u *)"!");
3857 vim_free(p);
3858 p = s;
3859 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003860#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003861
3862 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3863 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003864 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003865 escape_fname(&p);
3866
3867 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003868}
3869
3870/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003871 * Put a backslash before the file name in "pp", which is in allocated memory.
3872 */
3873 static void
3874escape_fname(pp)
3875 char_u **pp;
3876{
3877 char_u *p;
3878
3879 p = alloc((unsigned)(STRLEN(*pp) + 2));
3880 if (p != NULL)
3881 {
3882 p[0] = '\\';
3883 STRCPY(p + 1, *pp);
3884 vim_free(*pp);
3885 *pp = p;
3886 }
3887}
3888
3889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * For each file name in files[num_files]:
3891 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3892 */
3893 void
3894tilde_replace(orig_pat, num_files, files)
3895 char_u *orig_pat;
3896 int num_files;
3897 char_u **files;
3898{
3899 int i;
3900 char_u *p;
3901
3902 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3903 {
3904 for (i = 0; i < num_files; ++i)
3905 {
3906 p = home_replace_save(NULL, files[i]);
3907 if (p != NULL)
3908 {
3909 vim_free(files[i]);
3910 files[i] = p;
3911 }
3912 }
3913 }
3914}
3915
3916/*
3917 * Show all matches for completion on the command line.
3918 * Returns EXPAND_NOTHING when the character that triggered expansion should
3919 * be inserted like a normal character.
3920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 static int
3922showmatches(xp, wildmenu)
3923 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003924 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
3926#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3927 int num_files;
3928 char_u **files_found;
3929 int i, j, k;
3930 int maxlen;
3931 int lines;
3932 int columns;
3933 char_u *p;
3934 int lastlen;
3935 int attr;
3936 int showtail;
3937
3938 if (xp->xp_numfiles == -1)
3939 {
3940 set_expand_context(xp);
3941 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3942 &num_files, &files_found);
3943 showtail = expand_showtail(xp);
3944 if (i != EXPAND_OK)
3945 return i;
3946
3947 }
3948 else
3949 {
3950 num_files = xp->xp_numfiles;
3951 files_found = xp->xp_files;
3952 showtail = cmd_showtail;
3953 }
3954
3955#ifdef FEAT_WILDMENU
3956 if (!wildmenu)
3957 {
3958#endif
3959 msg_didany = FALSE; /* lines_left will be set */
3960 msg_start(); /* prepare for paging */
3961 msg_putchar('\n');
3962 out_flush();
3963 cmdline_row = msg_row;
3964 msg_didany = FALSE; /* lines_left will be set again */
3965 msg_start(); /* prepare for paging */
3966#ifdef FEAT_WILDMENU
3967 }
3968#endif
3969
3970 if (got_int)
3971 got_int = FALSE; /* only int. the completion, not the cmd line */
3972#ifdef FEAT_WILDMENU
3973 else if (wildmenu)
3974 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3975#endif
3976 else
3977 {
3978 /* find the length of the longest file name */
3979 maxlen = 0;
3980 for (i = 0; i < num_files; ++i)
3981 {
3982 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003983 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 || xp->xp_context == EXPAND_BUFFERS))
3985 {
3986 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3987 j = vim_strsize(NameBuff);
3988 }
3989 else
3990 j = vim_strsize(L_SHOWFILE(i));
3991 if (j > maxlen)
3992 maxlen = j;
3993 }
3994
3995 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3996 lines = num_files;
3997 else
3998 {
3999 /* compute the number of columns and lines for the listing */
4000 maxlen += 2; /* two spaces between file names */
4001 columns = ((int)Columns + 2) / maxlen;
4002 if (columns < 1)
4003 columns = 1;
4004 lines = (num_files + columns - 1) / columns;
4005 }
4006
4007 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4008
4009 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4010 {
4011 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4012 msg_clr_eos();
4013 msg_advance(maxlen - 3);
4014 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4015 }
4016
4017 /* list the files line by line */
4018 for (i = 0; i < lines; ++i)
4019 {
4020 lastlen = 999;
4021 for (k = i; k < num_files; k += lines)
4022 {
4023 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4024 {
4025 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4026 p = files_found[k] + STRLEN(files_found[k]) + 1;
4027 msg_advance(maxlen + 1);
4028 msg_puts(p);
4029 msg_advance(maxlen + 3);
4030 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4031 break;
4032 }
4033 for (j = maxlen - lastlen; --j >= 0; )
4034 msg_putchar(' ');
4035 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004036 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 || xp->xp_context == EXPAND_BUFFERS)
4038 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004039 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004040 if (xp->xp_numfiles != -1)
4041 {
4042 char_u *halved_slash;
4043 char_u *exp_path;
4044
4045 /* Expansion was done before and special characters
4046 * were escaped, need to halve backslashes. Also
4047 * $HOME has been replaced with ~/. */
4048 exp_path = expand_env_save_opt(files_found[k], TRUE);
4049 halved_slash = backslash_halve_save(
4050 exp_path != NULL ? exp_path : files_found[k]);
4051 j = mch_isdir(halved_slash != NULL ? halved_slash
4052 : files_found[k]);
4053 vim_free(exp_path);
4054 vim_free(halved_slash);
4055 }
4056 else
4057 /* Expansion was done here, file names are literal. */
4058 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (showtail)
4060 p = L_SHOWFILE(k);
4061 else
4062 {
4063 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4064 TRUE);
4065 p = NameBuff;
4066 }
4067 }
4068 else
4069 {
4070 j = FALSE;
4071 p = L_SHOWFILE(k);
4072 }
4073 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4074 }
4075 if (msg_col > 0) /* when not wrapped around */
4076 {
4077 msg_clr_eos();
4078 msg_putchar('\n');
4079 }
4080 out_flush(); /* show one line at a time */
4081 if (got_int)
4082 {
4083 got_int = FALSE;
4084 break;
4085 }
4086 }
4087
4088 /*
4089 * we redraw the command below the lines that we have just listed
4090 * This is a bit tricky, but it saves a lot of screen updating.
4091 */
4092 cmdline_row = msg_row; /* will put it back later */
4093 }
4094
4095 if (xp->xp_numfiles == -1)
4096 FreeWild(num_files, files_found);
4097
4098 return EXPAND_OK;
4099}
4100
4101/*
4102 * Private gettail for showmatches() (and win_redr_status_matches()):
4103 * Find tail of file name path, but ignore trailing "/".
4104 */
4105 char_u *
4106sm_gettail(s)
4107 char_u *s;
4108{
4109 char_u *p;
4110 char_u *t = s;
4111 int had_sep = FALSE;
4112
4113 for (p = s; *p != NUL; )
4114 {
4115 if (vim_ispathsep(*p)
4116#ifdef BACKSLASH_IN_FILENAME
4117 && !rem_backslash(p)
4118#endif
4119 )
4120 had_sep = TRUE;
4121 else if (had_sep)
4122 {
4123 t = p;
4124 had_sep = FALSE;
4125 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004126 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128 return t;
4129}
4130
4131/*
4132 * Return TRUE if we only need to show the tail of completion matches.
4133 * When not completing file names or there is a wildcard in the path FALSE is
4134 * returned.
4135 */
4136 static int
4137expand_showtail(xp)
4138 expand_T *xp;
4139{
4140 char_u *s;
4141 char_u *end;
4142
4143 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004144 if (xp->xp_context != EXPAND_FILES
4145 && xp->xp_context != EXPAND_SHELLCMD
4146 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 return FALSE;
4148
4149 end = gettail(xp->xp_pattern);
4150 if (end == xp->xp_pattern) /* there is no path separator */
4151 return FALSE;
4152
4153 for (s = xp->xp_pattern; s < end; s++)
4154 {
4155 /* Skip escaped wildcards. Only when the backslash is not a path
4156 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4157 if (rem_backslash(s))
4158 ++s;
4159 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4160 return FALSE;
4161 }
4162 return TRUE;
4163}
4164
4165/*
4166 * Prepare a string for expansion.
4167 * When expanding file names: The string will be used with expand_wildcards().
4168 * Copy the file name into allocated memory and add a '*' at the end.
4169 * When expanding other names: The string will be used with regcomp(). Copy
4170 * the name into allocated memory and prepend "^".
4171 */
4172 char_u *
4173addstar(fname, len, context)
4174 char_u *fname;
4175 int len;
4176 int context; /* EXPAND_FILES etc. */
4177{
4178 char_u *retval;
4179 int i, j;
4180 int new_len;
4181 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004182 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004184 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004185 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004186 && context != EXPAND_SHELLCMD
4187 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
4189 /*
4190 * Matching will be done internally (on something other than files).
4191 * So we convert the file-matching-type wildcards into our kind for
4192 * use with vim_regcomp(). First work out how long it will be:
4193 */
4194
4195 /* For help tags the translation is done in find_help_tags().
4196 * For a tag pattern starting with "/" no translation is needed. */
4197 if (context == EXPAND_HELP
4198 || context == EXPAND_COLORS
4199 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004200 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004201 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 || (context == EXPAND_TAGS && fname[0] == '/'))
4203 retval = vim_strnsave(fname, len);
4204 else
4205 {
4206 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4207 for (i = 0; i < len; i++)
4208 {
4209 if (fname[i] == '*' || fname[i] == '~')
4210 new_len++; /* '*' needs to be replaced by ".*"
4211 '~' needs to be replaced by "\~" */
4212
4213 /* Buffer names are like file names. "." should be literal */
4214 if (context == EXPAND_BUFFERS && fname[i] == '.')
4215 new_len++; /* "." becomes "\." */
4216
4217 /* Custom expansion takes care of special things, match
4218 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004219 if ((context == EXPAND_USER_DEFINED
4220 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 new_len++; /* '\' becomes "\\" */
4222 }
4223 retval = alloc(new_len);
4224 if (retval != NULL)
4225 {
4226 retval[0] = '^';
4227 j = 1;
4228 for (i = 0; i < len; i++, j++)
4229 {
4230 /* Skip backslash. But why? At least keep it for custom
4231 * expansion. */
4232 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004233 && context != EXPAND_USER_LIST
4234 && fname[i] == '\\'
4235 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 break;
4237
4238 switch (fname[i])
4239 {
4240 case '*': retval[j++] = '.';
4241 break;
4242 case '~': retval[j++] = '\\';
4243 break;
4244 case '?': retval[j] = '.';
4245 continue;
4246 case '.': if (context == EXPAND_BUFFERS)
4247 retval[j++] = '\\';
4248 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004249 case '\\': if (context == EXPAND_USER_DEFINED
4250 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 retval[j++] = '\\';
4252 break;
4253 }
4254 retval[j] = fname[i];
4255 }
4256 retval[j] = NUL;
4257 }
4258 }
4259 }
4260 else
4261 {
4262 retval = alloc(len + 4);
4263 if (retval != NULL)
4264 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004265 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266
4267 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004268 * Don't add a star to *, ~, ~user, $var or `cmd`.
4269 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 * ~ would be at the start of the file name, but not the tail.
4271 * $ could be anywhere in the tail.
4272 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004273 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 */
4275 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004276 ends_in_star = (len > 0 && retval[len - 1] == '*');
4277#ifndef BACKSLASH_IN_FILENAME
4278 for (i = len - 2; i >= 0; --i)
4279 {
4280 if (retval[i] != '\\')
4281 break;
4282 ends_in_star = !ends_in_star;
4283 }
4284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004286 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 && vim_strchr(tail, '$') == NULL
4288 && vim_strchr(retval, '`') == NULL)
4289 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004290 else if (len > 0 && retval[len - 1] == '$')
4291 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 retval[len] = NUL;
4293 }
4294 }
4295 return retval;
4296}
4297
4298/*
4299 * Must parse the command line so far to work out what context we are in.
4300 * Completion can then be done based on that context.
4301 * This routine sets the variables:
4302 * xp->xp_pattern The start of the pattern to be expanded within
4303 * the command line (ends at the cursor).
4304 * xp->xp_context The type of thing to expand. Will be one of:
4305 *
4306 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4307 * the command line, like an unknown command. Caller
4308 * should beep.
4309 * EXPAND_NOTHING Unrecognised context for completion, use char like
4310 * a normal char, rather than for completion. eg
4311 * :s/^I/
4312 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4313 * it.
4314 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4315 * EXPAND_FILES After command with XFILE set, or after setting
4316 * with P_EXPAND set. eg :e ^I, :w>>^I
4317 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4318 * when we know only directories are of interest. eg
4319 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004320 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4322 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4323 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4324 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4325 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4326 * EXPAND_EVENTS Complete event names
4327 * EXPAND_SYNTAX Complete :syntax command arguments
4328 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4329 * EXPAND_AUGROUP Complete autocommand group names
4330 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4331 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4332 * eg :unmap a^I , :cunab x^I
4333 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4334 * eg :call sub^I
4335 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4336 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4337 * names in expressions, eg :while s^I
4338 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004339 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 */
4341 static void
4342set_expand_context(xp)
4343 expand_T *xp;
4344{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004345 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if (ccline.cmdfirstc != ':'
4347#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004348 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004349 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350#endif
4351 )
4352 {
4353 xp->xp_context = EXPAND_NOTHING;
4354 return;
4355 }
4356 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4357}
4358
4359 void
4360set_cmd_context(xp, str, len, col)
4361 expand_T *xp;
4362 char_u *str; /* start of command line */
4363 int len; /* length of command line (excl. NUL) */
4364 int col; /* position of cursor */
4365{
4366 int old_char = NUL;
4367 char_u *nextcomm;
4368
4369 /*
4370 * Avoid a UMR warning from Purify, only save the character if it has been
4371 * written before.
4372 */
4373 if (col < len)
4374 old_char = str[col];
4375 str[col] = NUL;
4376 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004377
4378#ifdef FEAT_EVAL
4379 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004380 {
4381# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004382 /* pass CMD_SIZE because there is no real command */
4383 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004384# endif
4385 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004386 else if (ccline.input_fn)
4387 {
4388 xp->xp_context = ccline.xp_context;
4389 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004390# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004391 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004392# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004393 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004394 else
4395#endif
4396 while (nextcomm != NULL)
4397 nextcomm = set_one_cmd_context(xp, nextcomm);
4398
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 str[col] = old_char;
4400}
4401
4402/*
4403 * Expand the command line "str" from context "xp".
4404 * "xp" must have been set by set_cmd_context().
4405 * xp->xp_pattern points into "str", to where the text that is to be expanded
4406 * starts.
4407 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4408 * cursor.
4409 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4410 * key that triggered expansion literally.
4411 * Returns EXPAND_OK otherwise.
4412 */
4413 int
4414expand_cmdline(xp, str, col, matchcount, matches)
4415 expand_T *xp;
4416 char_u *str; /* start of command line */
4417 int col; /* position of cursor */
4418 int *matchcount; /* return: nr of matches */
4419 char_u ***matches; /* return: array of pointers to matches */
4420{
4421 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004422 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
4424 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4425 {
4426 beep_flush();
4427 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4428 }
4429 if (xp->xp_context == EXPAND_NOTHING)
4430 {
4431 /* Caller can use the character as a normal char instead */
4432 return EXPAND_NOTHING;
4433 }
4434
4435 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004436 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4437 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 if (file_str == NULL)
4439 return EXPAND_UNSUCCESSFUL;
4440
Bram Moolenaar94950a92010-12-02 16:01:29 +01004441 if (p_wic)
4442 options += WILD_ICASE;
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004445 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
4447 *matchcount = 0;
4448 *matches = NULL;
4449 }
4450 vim_free(file_str);
4451
4452 return EXPAND_OK;
4453}
4454
4455#ifdef FEAT_MULTI_LANG
4456/*
4457 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4458 */
4459static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4460
4461 static void
4462cleanup_help_tags(num_file, file)
4463 int num_file;
4464 char_u **file;
4465{
4466 int i, j;
4467 int len;
4468
4469 for (i = 0; i < num_file; ++i)
4470 {
4471 len = (int)STRLEN(file[i]) - 3;
4472 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4473 {
4474 /* Sorting on priority means the same item in another language may
4475 * be anywhere. Search all items for a match up to the "@en". */
4476 for (j = 0; j < num_file; ++j)
4477 if (j != i
4478 && (int)STRLEN(file[j]) == len + 3
4479 && STRNCMP(file[i], file[j], len + 1) == 0)
4480 break;
4481 if (j == num_file)
4482 file[i][len] = NUL;
4483 }
4484 }
4485}
4486#endif
4487
4488/*
4489 * Do the expansion based on xp->xp_context and "pat".
4490 */
4491 static int
4492ExpandFromContext(xp, pat, num_file, file, options)
4493 expand_T *xp;
4494 char_u *pat;
4495 int *num_file;
4496 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004497 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498{
4499#ifdef FEAT_CMDL_COMPL
4500 regmatch_T regmatch;
4501#endif
4502 int ret;
4503 int flags;
4504
4505 flags = EW_DIR; /* include directories */
4506 if (options & WILD_LIST_NOTFOUND)
4507 flags |= EW_NOTFOUND;
4508 if (options & WILD_ADD_SLASH)
4509 flags |= EW_ADDSLASH;
4510 if (options & WILD_KEEP_ALL)
4511 flags |= EW_KEEPALL;
4512 if (options & WILD_SILENT)
4513 flags |= EW_SILENT;
4514
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004515 if (xp->xp_context == EXPAND_FILES
4516 || xp->xp_context == EXPAND_DIRECTORIES
4517 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 {
4519 /*
4520 * Expand file or directory names.
4521 */
4522 int free_pat = FALSE;
4523 int i;
4524
4525 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4526 * space */
4527 if (xp->xp_backslash != XP_BS_NONE)
4528 {
4529 free_pat = TRUE;
4530 pat = vim_strsave(pat);
4531 for (i = 0; pat[i]; ++i)
4532 if (pat[i] == '\\')
4533 {
4534 if (xp->xp_backslash == XP_BS_THREE
4535 && pat[i + 1] == '\\'
4536 && pat[i + 2] == '\\'
4537 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004538 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (xp->xp_backslash == XP_BS_ONE
4540 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004541 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 }
4543 }
4544
4545 if (xp->xp_context == EXPAND_FILES)
4546 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004547 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4548 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 else
4550 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004551 if (options & WILD_ICASE)
4552 flags |= EW_ICASE;
4553
Bram Moolenaard7834d32009-12-02 16:14:36 +00004554 /* Expand wildcards, supporting %:h and the like. */
4555 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (free_pat)
4557 vim_free(pat);
4558 return ret;
4559 }
4560
4561 *file = (char_u **)"";
4562 *num_file = 0;
4563 if (xp->xp_context == EXPAND_HELP)
4564 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004565 /* With an empty argument we would get all the help tags, which is
4566 * very slow. Get matches for "help" instead. */
4567 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4568 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
4570#ifdef FEAT_MULTI_LANG
4571 cleanup_help_tags(*num_file, *file);
4572#endif
4573 return OK;
4574 }
4575 return FAIL;
4576 }
4577
4578#ifndef FEAT_CMDL_COMPL
4579 return FAIL;
4580#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004581 if (xp->xp_context == EXPAND_SHELLCMD)
4582 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (xp->xp_context == EXPAND_OLD_SETTING)
4584 return ExpandOldSetting(num_file, file);
4585 if (xp->xp_context == EXPAND_BUFFERS)
4586 return ExpandBufnames(pat, num_file, file, options);
4587 if (xp->xp_context == EXPAND_TAGS
4588 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4589 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4590 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004591 {
4592 char *directories[] = {"colors", NULL};
4593 return ExpandRTDir(pat, num_file, file, directories);
4594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004596 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004597 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004598 return ExpandRTDir(pat, num_file, file, directories);
4599 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004600 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004601 {
4602 char *directories[] = {"syntax", NULL};
4603 return ExpandRTDir(pat, num_file, file, directories);
4604 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004605 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004606 {
4607 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4608 return ExpandRTDir(pat, num_file, file, directories);
4609 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004610# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4611 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004612 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004613# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4616 if (regmatch.regprog == NULL)
4617 return FAIL;
4618
4619 /* set ignore-case according to p_ic, p_scs and pat */
4620 regmatch.rm_ic = ignorecase(pat);
4621
4622 if (xp->xp_context == EXPAND_SETTINGS
4623 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4624 ret = ExpandSettings(xp, &regmatch, num_file, file);
4625 else if (xp->xp_context == EXPAND_MAPPINGS)
4626 ret = ExpandMappings(&regmatch, num_file, file);
4627# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4628 else if (xp->xp_context == EXPAND_USER_DEFINED)
4629 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4630# endif
4631 else
4632 {
4633 static struct expgen
4634 {
4635 int context;
4636 char_u *((*func)__ARGS((expand_T *, int)));
4637 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004638 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 } tab[] =
4640 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004641 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4642 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004643#ifdef FEAT_CMDHIST
4644 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004647 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4648 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4649 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4650 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651#endif
4652#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004653 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4654 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4655 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4656 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657#endif
4658#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004659 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4660 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661#endif
4662#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004663 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004665 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004667 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4668 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004670#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004671 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004672#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004673#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004674 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004675#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004676#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004677 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4680 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004681 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4682 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004684 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004685 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 };
4687 int i;
4688
4689 /*
4690 * Find a context in the table and call the ExpandGeneric() with the
4691 * right function to do the expansion.
4692 */
4693 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004694 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 if (xp->xp_context == tab[i].context)
4696 {
4697 if (tab[i].ic)
4698 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004699 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004700 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 break;
4702 }
4703 }
4704
4705 vim_free(regmatch.regprog);
4706
4707 return ret;
4708#endif /* FEAT_CMDL_COMPL */
4709}
4710
4711#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4712/*
4713 * Expand a list of names.
4714 *
4715 * Generic function for command line completion. It calls a function to
4716 * obtain strings, one by one. The strings are matched against a regexp
4717 * program. Matching strings are copied into an array, which is returned.
4718 *
4719 * Returns OK when no problems encountered, FAIL for error (out of memory).
4720 */
4721 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004722ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 expand_T *xp;
4724 regmatch_T *regmatch;
4725 int *num_file;
4726 char_u ***file;
4727 char_u *((*func)__ARGS((expand_T *, int)));
4728 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004729 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
4731 int i;
4732 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004733 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 char_u *str;
4735
4736 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004737 * round == 0: count the number of matching names
4738 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004740 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
4742 for (i = 0; ; ++i)
4743 {
4744 str = (*func)(xp, i);
4745 if (str == NULL) /* end of list */
4746 break;
4747 if (*str == NUL) /* skip empty strings */
4748 continue;
4749
4750 if (vim_regexec(regmatch, str, (colnr_T)0))
4751 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004752 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004754 if (escaped)
4755 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4756 else
4757 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 (*file)[count] = str;
4759#ifdef FEAT_MENU
4760 if (func == get_menu_names && str != NULL)
4761 {
4762 /* test for separator added by get_menu_names() */
4763 str += STRLEN(str) - 1;
4764 if (*str == '\001')
4765 *str = '.';
4766 }
4767#endif
4768 }
4769 ++count;
4770 }
4771 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004772 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
4774 if (count == 0)
4775 return OK;
4776 *num_file = count;
4777 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4778 if (*file == NULL)
4779 {
4780 *file = (char_u **)"";
4781 return FAIL;
4782 }
4783 count = 0;
4784 }
4785 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004786
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004787 /* Sort the results. Keep menu's in the specified order. */
4788 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004789 {
4790 if (xp->xp_context == EXPAND_EXPRESSION
4791 || xp->xp_context == EXPAND_FUNCTIONS
4792 || xp->xp_context == EXPAND_USER_FUNC)
4793 /* <SNR> functions should be sorted to the end. */
4794 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4795 sort_func_compare);
4796 else
4797 sort_strings(*file, *num_file);
4798 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004799
Bram Moolenaar4f688582007-07-24 12:34:30 +00004800#ifdef FEAT_CMDL_COMPL
4801 /* Reset the variables used for special highlight names expansion, so that
4802 * they don't show up when getting normal highlight names by ID. */
4803 reset_expand_highlight();
4804#endif
4805
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return OK;
4807}
4808
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004809/*
4810 * Complete a shell command.
4811 * Returns FAIL or OK;
4812 */
4813 static int
4814expand_shellcmd(filepat, num_file, file, flagsarg)
4815 char_u *filepat; /* pattern to match with command names */
4816 int *num_file; /* return: number of matches */
4817 char_u ***file; /* return: array with matches */
4818 int flagsarg; /* EW_ flags */
4819{
4820 char_u *pat;
4821 int i;
4822 char_u *path;
4823 int mustfree = FALSE;
4824 garray_T ga;
4825 char_u *buf = alloc(MAXPATHL);
4826 size_t l;
4827 char_u *s, *e;
4828 int flags = flagsarg;
4829 int ret;
4830
4831 if (buf == NULL)
4832 return FAIL;
4833
4834 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4835 * space */
4836 pat = vim_strsave(filepat);
4837 for (i = 0; pat[i]; ++i)
4838 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004839 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004840
4841 flags |= EW_FILE | EW_EXEC;
4842
4843 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004844 if (mch_isFullName(pat))
4845 path = (char_u *)" ";
4846 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004847 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4848 path = (char_u *)".";
4849 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004850 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004851 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004852 if (path == NULL)
4853 path = (char_u *)"";
4854 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004855
4856 /*
4857 * Go over all directories in $PATH. Expand matches in that directory and
4858 * collect them in "ga".
4859 */
4860 ga_init2(&ga, (int)sizeof(char *), 10);
4861 for (s = path; *s != NUL; s = e)
4862 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004863 if (*s == ' ')
4864 ++s; /* Skip space used for absolute path name. */
4865
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004866#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4867 e = vim_strchr(s, ';');
4868#else
4869 e = vim_strchr(s, ':');
4870#endif
4871 if (e == NULL)
4872 e = s + STRLEN(s);
4873
4874 l = e - s;
4875 if (l > MAXPATHL - 5)
4876 break;
4877 vim_strncpy(buf, s, l);
4878 add_pathsep(buf);
4879 l = STRLEN(buf);
4880 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4881
4882 /* Expand matches in one directory of $PATH. */
4883 ret = expand_wildcards(1, &buf, num_file, file, flags);
4884 if (ret == OK)
4885 {
4886 if (ga_grow(&ga, *num_file) == FAIL)
4887 FreeWild(*num_file, *file);
4888 else
4889 {
4890 for (i = 0; i < *num_file; ++i)
4891 {
4892 s = (*file)[i];
4893 if (STRLEN(s) > l)
4894 {
4895 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004896 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004897 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4898 }
4899 else
4900 vim_free(s);
4901 }
4902 vim_free(*file);
4903 }
4904 }
4905 if (*e != NUL)
4906 ++e;
4907 }
4908 *file = ga.ga_data;
4909 *num_file = ga.ga_len;
4910
4911 vim_free(buf);
4912 vim_free(pat);
4913 if (mustfree)
4914 vim_free(path);
4915 return OK;
4916}
4917
4918
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004920static 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));
4921
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004923 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004924 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004926 static void *
4927call_user_expand_func(user_expand_func, xp, num_file, file)
4928 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 int *num_file;
4931 char_u ***file;
4932{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004933 char_u keep;
4934 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004937 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004938 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004941 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 *num_file = 0;
4943 *file = NULL;
4944
Bram Moolenaar21669c02008-01-18 12:16:16 +00004945 if (ccline.cmdbuff == NULL)
4946 {
4947 /* Completion from Insert mode, pass fake arguments. */
4948 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004949 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004950 args[1] = xp->xp_pattern;
4951 }
4952 else
4953 {
4954 /* Completion on the command line, pass real arguments. */
4955 keep = ccline.cmdbuff[ccline.cmdlen];
4956 ccline.cmdbuff[ccline.cmdlen] = 0;
4957 sprintf((char *)num, "%d", ccline.cmdpos);
4958 args[1] = ccline.cmdbuff;
4959 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004960 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 args[2] = num;
4962
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004963 /* Save the cmdline, we don't know what the function may do. */
4964 save_ccline = ccline;
4965 ccline.cmdbuff = NULL;
4966 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004968
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004969 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004970
4971 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004973 if (ccline.cmdbuff != NULL)
4974 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004975
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004976 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004977 return ret;
4978}
4979
4980/*
4981 * Expand names with a function defined by the user.
4982 */
4983 static int
4984ExpandUserDefined(xp, regmatch, num_file, file)
4985 expand_T *xp;
4986 regmatch_T *regmatch;
4987 int *num_file;
4988 char_u ***file;
4989{
4990 char_u *retstr;
4991 char_u *s;
4992 char_u *e;
4993 char_u keep;
4994 garray_T ga;
4995
4996 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4997 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 return FAIL;
4999
5000 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005001 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
5003 e = vim_strchr(s, '\n');
5004 if (e == NULL)
5005 e = s + STRLEN(s);
5006 keep = *e;
5007 *e = 0;
5008
5009 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5010 {
5011 *e = keep;
5012 if (*e != NUL)
5013 ++e;
5014 continue;
5015 }
5016
5017 if (ga_grow(&ga, 1) == FAIL)
5018 break;
5019
5020 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5021 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022
5023 *e = keep;
5024 if (*e != NUL)
5025 ++e;
5026 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005027 vim_free(retstr);
5028 *file = ga.ga_data;
5029 *num_file = ga.ga_len;
5030 return OK;
5031}
5032
5033/*
5034 * Expand names with a list returned by a function defined by the user.
5035 */
5036 static int
5037ExpandUserList(xp, num_file, file)
5038 expand_T *xp;
5039 int *num_file;
5040 char_u ***file;
5041{
5042 list_T *retlist;
5043 listitem_T *li;
5044 garray_T ga;
5045
5046 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5047 if (retlist == NULL)
5048 return FAIL;
5049
5050 ga_init2(&ga, (int)sizeof(char *), 3);
5051 /* Loop over the items in the list. */
5052 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5053 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005054 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5055 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005056
5057 if (ga_grow(&ga, 1) == FAIL)
5058 break;
5059
5060 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005061 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005062 ++ga.ga_len;
5063 }
5064 list_unref(retlist);
5065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 *file = ga.ga_data;
5067 *num_file = ga.ga_len;
5068 return OK;
5069}
5070#endif
5071
5072/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005073 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005074 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005075 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 */
5077 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005078ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 char_u *pat;
5080 int *num_file;
5081 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005082 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005084 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 char_u *s;
5086 char_u *e;
5087 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005088 int i;
5089 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090
5091 *num_file = 0;
5092 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005093 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005094 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005096 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005098 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5099 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005101 ga_clear_strings(&ga);
5102 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005104 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5105 matches = globpath(p_rtp, s, 0);
5106 vim_free(s);
5107 if (matches == NULL)
5108 continue;
5109
5110 for (s = matches; *s != NUL; s = e)
5111 {
5112 e = vim_strchr(s, '\n');
5113 if (e == NULL)
5114 e = s + STRLEN(s);
5115 if (ga_grow(&ga, 1) == FAIL)
5116 break;
5117 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5118 {
5119 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5120 if (*s == '\n' || vim_ispathsep(*s))
5121 break;
5122 ++s;
5123 ((char_u **)ga.ga_data)[ga.ga_len] =
5124 vim_strnsave(s, (int)(e - s - 4));
5125 ++ga.ga_len;
5126 }
5127 if (*e != NUL)
5128 ++e;
5129 }
5130 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005132 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005133 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005134
5135 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005136 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005137 remove_duplicates(&ga);
5138
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 *file = ga.ga_data;
5140 *num_file = ga.ga_len;
5141 return OK;
5142}
5143
5144#endif
5145
5146#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5147/*
5148 * Expand "file" for all comma-separated directories in "path".
5149 * Returns an allocated string with all matches concatenated, separated by
5150 * newlines. Returns NULL for an error or no matches.
5151 */
5152 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005153globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 char_u *path;
5155 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005156 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157{
5158 expand_T xpc;
5159 char_u *buf;
5160 garray_T ga;
5161 int i;
5162 int len;
5163 int num_p;
5164 char_u **p;
5165 char_u *cur = NULL;
5166
5167 buf = alloc(MAXPATHL);
5168 if (buf == NULL)
5169 return NULL;
5170
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005171 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005173
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 ga_init2(&ga, 1, 100);
5175
5176 /* Loop over all entries in {path}. */
5177 while (*path != NUL)
5178 {
5179 /* Copy one item of the path to buf[] and concatenate the file name. */
5180 copy_option_part(&path, buf, MAXPATHL, ",");
5181 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5182 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005183# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005184 /* Using the platform's path separator (\) makes vim incorrectly
5185 * treat it as an escape character, use '/' instead. */
5186 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5187 STRCAT(buf, "/");
5188# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005192 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5193 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005195 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005197 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199 /* Concatenate new results to previous ones. */
5200 if (ga_grow(&ga, len) == OK)
5201 {
5202 cur = (char_u *)ga.ga_data + ga.ga_len;
5203 for (i = 0; i < num_p; ++i)
5204 {
5205 STRCPY(cur, p[i]);
5206 cur += STRLEN(p[i]);
5207 *cur++ = '\n';
5208 }
5209 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
5211 FreeWild(num_p, p);
5212 }
5213 }
5214 }
5215 if (cur != NULL)
5216 *--cur = 0; /* Replace trailing newline with NUL */
5217
5218 vim_free(buf);
5219 return (char_u *)ga.ga_data;
5220}
5221
5222#endif
5223
5224#if defined(FEAT_CMDHIST) || defined(PROTO)
5225
5226/*********************************
5227 * Command line history stuff *
5228 *********************************/
5229
5230/*
5231 * Translate a history character to the associated type number.
5232 */
5233 static int
5234hist_char2type(c)
5235 int c;
5236{
5237 if (c == ':')
5238 return HIST_CMD;
5239 if (c == '=')
5240 return HIST_EXPR;
5241 if (c == '@')
5242 return HIST_INPUT;
5243 if (c == '>')
5244 return HIST_DEBUG;
5245 return HIST_SEARCH; /* must be '?' or '/' */
5246}
5247
5248/*
5249 * Table of history names.
5250 * These names are used in :history and various hist...() functions.
5251 * It is sufficient to give the significant prefix of a history name.
5252 */
5253
5254static char *(history_names[]) =
5255{
5256 "cmd",
5257 "search",
5258 "expr",
5259 "input",
5260 "debug",
5261 NULL
5262};
5263
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005264#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5265/*
5266 * Function given to ExpandGeneric() to obtain the possible first
5267 * arguments of the ":history command.
5268 */
5269 static char_u *
5270get_history_arg(xp, idx)
5271 expand_T *xp UNUSED;
5272 int idx;
5273{
5274 static char_u compl[2] = { NUL, NUL };
5275 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005276 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005277 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5278
5279 if (idx < short_names_count)
5280 {
5281 compl[0] = (char_u)short_names[idx];
5282 return compl;
5283 }
5284 if (idx < short_names_count + history_name_count)
5285 return (char_u *)history_names[idx - short_names_count];
5286 if (idx == short_names_count + history_name_count)
5287 return (char_u *)"all";
5288 return NULL;
5289}
5290#endif
5291
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292/*
5293 * init_history() - Initialize the command line history.
5294 * Also used to re-allocate the history when the size changes.
5295 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005296 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297init_history()
5298{
5299 int newlen; /* new length of history table */
5300 histentry_T *temp;
5301 int i;
5302 int j;
5303 int type;
5304
5305 /*
5306 * If size of history table changed, reallocate it
5307 */
5308 newlen = (int)p_hi;
5309 if (newlen != hislen) /* history length changed */
5310 {
5311 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5312 {
5313 if (newlen)
5314 {
5315 temp = (histentry_T *)lalloc(
5316 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5317 if (temp == NULL) /* out of memory! */
5318 {
5319 if (type == 0) /* first one: just keep the old length */
5320 {
5321 newlen = hislen;
5322 break;
5323 }
5324 /* Already changed one table, now we can only have zero
5325 * length for all tables. */
5326 newlen = 0;
5327 type = -1;
5328 continue;
5329 }
5330 }
5331 else
5332 temp = NULL;
5333 if (newlen == 0 || temp != NULL)
5334 {
5335 if (hisidx[type] < 0) /* there are no entries yet */
5336 {
5337 for (i = 0; i < newlen; ++i)
5338 {
5339 temp[i].hisnum = 0;
5340 temp[i].hisstr = NULL;
5341 }
5342 }
5343 else if (newlen > hislen) /* array becomes bigger */
5344 {
5345 for (i = 0; i <= hisidx[type]; ++i)
5346 temp[i] = history[type][i];
5347 j = i;
5348 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5349 {
5350 temp[i].hisnum = 0;
5351 temp[i].hisstr = NULL;
5352 }
5353 for ( ; j < hislen; ++i, ++j)
5354 temp[i] = history[type][j];
5355 }
5356 else /* array becomes smaller or 0 */
5357 {
5358 j = hisidx[type];
5359 for (i = newlen - 1; ; --i)
5360 {
5361 if (i >= 0) /* copy newest entries */
5362 temp[i] = history[type][j];
5363 else /* remove older entries */
5364 vim_free(history[type][j].hisstr);
5365 if (--j < 0)
5366 j = hislen - 1;
5367 if (j == hisidx[type])
5368 break;
5369 }
5370 hisidx[type] = newlen - 1;
5371 }
5372 vim_free(history[type]);
5373 history[type] = temp;
5374 }
5375 }
5376 hislen = newlen;
5377 }
5378}
5379
5380/*
5381 * Check if command line 'str' is already in history.
5382 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5383 */
5384 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005385in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 int type;
5387 char_u *str;
5388 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005389 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
5391 int i;
5392 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005393 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394
5395 if (hisidx[type] < 0)
5396 return FALSE;
5397 i = hisidx[type];
5398 do
5399 {
5400 if (history[type][i].hisstr == NULL)
5401 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005402
5403 /* For search history, check that the separator character matches as
5404 * well. */
5405 p = history[type][i].hisstr;
5406 if (STRCMP(str, p) == 0
5407 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 if (!move_to_front)
5410 return TRUE;
5411 last_i = i;
5412 break;
5413 }
5414 if (--i < 0)
5415 i = hislen - 1;
5416 } while (i != hisidx[type]);
5417
5418 if (last_i >= 0)
5419 {
5420 str = history[type][i].hisstr;
5421 while (i != hisidx[type])
5422 {
5423 if (++i >= hislen)
5424 i = 0;
5425 history[type][last_i] = history[type][i];
5426 last_i = i;
5427 }
5428 history[type][i].hisstr = str;
5429 history[type][i].hisnum = ++hisnum[type];
5430 return TRUE;
5431 }
5432 return FALSE;
5433}
5434
5435/*
5436 * Convert history name (from table above) to its HIST_ equivalent.
5437 * When "name" is empty, return "cmd" history.
5438 * Returns -1 for unknown history name.
5439 */
5440 int
5441get_histtype(name)
5442 char_u *name;
5443{
5444 int i;
5445 int len = (int)STRLEN(name);
5446
5447 /* No argument: use current history. */
5448 if (len == 0)
5449 return hist_char2type(ccline.cmdfirstc);
5450
5451 for (i = 0; history_names[i] != NULL; ++i)
5452 if (STRNICMP(name, history_names[i], len) == 0)
5453 return i;
5454
5455 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5456 return hist_char2type(name[0]);
5457
5458 return -1;
5459}
5460
5461static int last_maptick = -1; /* last seen maptick */
5462
5463/*
5464 * Add the given string to the given history. If the string is already in the
5465 * history then it is moved to the front. "histype" may be one of he HIST_
5466 * values.
5467 */
5468 void
5469add_to_history(histype, new_entry, in_map, sep)
5470 int histype;
5471 char_u *new_entry;
5472 int in_map; /* consider maptick when inside a mapping */
5473 int sep; /* separator character used (search hist) */
5474{
5475 histentry_T *hisptr;
5476 int len;
5477
5478 if (hislen == 0) /* no history */
5479 return;
5480
5481 /*
5482 * Searches inside the same mapping overwrite each other, so that only
5483 * the last line is kept. Be careful not to remove a line that was moved
5484 * down, only lines that were added.
5485 */
5486 if (histype == HIST_SEARCH && in_map)
5487 {
5488 if (maptick == last_maptick)
5489 {
5490 /* Current line is from the same mapping, remove it */
5491 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5492 vim_free(hisptr->hisstr);
5493 hisptr->hisstr = NULL;
5494 hisptr->hisnum = 0;
5495 --hisnum[histype];
5496 if (--hisidx[HIST_SEARCH] < 0)
5497 hisidx[HIST_SEARCH] = hislen - 1;
5498 }
5499 last_maptick = -1;
5500 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005501 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 if (++hisidx[histype] == hislen)
5504 hisidx[histype] = 0;
5505 hisptr = &history[histype][hisidx[histype]];
5506 vim_free(hisptr->hisstr);
5507
5508 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005509 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5511 if (hisptr->hisstr != NULL)
5512 hisptr->hisstr[len + 1] = sep;
5513
5514 hisptr->hisnum = ++hisnum[histype];
5515 if (histype == HIST_SEARCH && in_map)
5516 last_maptick = maptick;
5517 }
5518}
5519
5520#if defined(FEAT_EVAL) || defined(PROTO)
5521
5522/*
5523 * Get identifier of newest history entry.
5524 * "histype" may be one of the HIST_ values.
5525 */
5526 int
5527get_history_idx(histype)
5528 int histype;
5529{
5530 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5531 || hisidx[histype] < 0)
5532 return -1;
5533
5534 return history[histype][hisidx[histype]].hisnum;
5535}
5536
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005537static struct cmdline_info *get_ccline_ptr __ARGS((void));
5538
5539/*
5540 * Get pointer to the command line info to use. cmdline_paste() may clear
5541 * ccline and put the previous value in prev_ccline.
5542 */
5543 static struct cmdline_info *
5544get_ccline_ptr()
5545{
5546 if ((State & CMDLINE) == 0)
5547 return NULL;
5548 if (ccline.cmdbuff != NULL)
5549 return &ccline;
5550 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5551 return &prev_ccline;
5552 return NULL;
5553}
5554
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555/*
5556 * Get the current command line in allocated memory.
5557 * Only works when the command line is being edited.
5558 * Returns NULL when something is wrong.
5559 */
5560 char_u *
5561get_cmdline_str()
5562{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005563 struct cmdline_info *p = get_ccline_ptr();
5564
5565 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005567 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568}
5569
5570/*
5571 * Get the current command line position, counted in bytes.
5572 * Zero is the first position.
5573 * Only works when the command line is being edited.
5574 * Returns -1 when something is wrong.
5575 */
5576 int
5577get_cmdline_pos()
5578{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005579 struct cmdline_info *p = get_ccline_ptr();
5580
5581 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005583 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584}
5585
5586/*
5587 * Set the command line byte position to "pos". Zero is the first position.
5588 * Only works when the command line is being edited.
5589 * Returns 1 when failed, 0 when OK.
5590 */
5591 int
5592set_cmdline_pos(pos)
5593 int pos;
5594{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005595 struct cmdline_info *p = get_ccline_ptr();
5596
5597 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 return 1;
5599
5600 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5601 * changed the command line. */
5602 if (pos < 0)
5603 new_cmdpos = 0;
5604 else
5605 new_cmdpos = pos;
5606 return 0;
5607}
5608
5609/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005610 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005611 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005612 * Only works when the command line is being edited.
5613 * Returns NUL when something is wrong.
5614 */
5615 int
5616get_cmdline_type()
5617{
5618 struct cmdline_info *p = get_ccline_ptr();
5619
5620 if (p == NULL)
5621 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005622 if (p->cmdfirstc == NUL)
5623 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005624 return p->cmdfirstc;
5625}
5626
5627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 * Calculate history index from a number:
5629 * num > 0: seen as identifying number of a history entry
5630 * num < 0: relative position in history wrt newest entry
5631 * "histype" may be one of the HIST_ values.
5632 */
5633 static int
5634calc_hist_idx(histype, num)
5635 int histype;
5636 int num;
5637{
5638 int i;
5639 histentry_T *hist;
5640 int wrapped = FALSE;
5641
5642 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5643 || (i = hisidx[histype]) < 0 || num == 0)
5644 return -1;
5645
5646 hist = history[histype];
5647 if (num > 0)
5648 {
5649 while (hist[i].hisnum > num)
5650 if (--i < 0)
5651 {
5652 if (wrapped)
5653 break;
5654 i += hislen;
5655 wrapped = TRUE;
5656 }
5657 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5658 return i;
5659 }
5660 else if (-num <= hislen)
5661 {
5662 i += num + 1;
5663 if (i < 0)
5664 i += hislen;
5665 if (hist[i].hisstr != NULL)
5666 return i;
5667 }
5668 return -1;
5669}
5670
5671/*
5672 * Get a history entry by its index.
5673 * "histype" may be one of the HIST_ values.
5674 */
5675 char_u *
5676get_history_entry(histype, idx)
5677 int histype;
5678 int idx;
5679{
5680 idx = calc_hist_idx(histype, idx);
5681 if (idx >= 0)
5682 return history[histype][idx].hisstr;
5683 else
5684 return (char_u *)"";
5685}
5686
5687/*
5688 * Clear all entries of a history.
5689 * "histype" may be one of the HIST_ values.
5690 */
5691 int
5692clr_history(histype)
5693 int histype;
5694{
5695 int i;
5696 histentry_T *hisptr;
5697
5698 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5699 {
5700 hisptr = history[histype];
5701 for (i = hislen; i--;)
5702 {
5703 vim_free(hisptr->hisstr);
5704 hisptr->hisnum = 0;
5705 hisptr++->hisstr = NULL;
5706 }
5707 hisidx[histype] = -1; /* mark history as cleared */
5708 hisnum[histype] = 0; /* reset identifier counter */
5709 return OK;
5710 }
5711 return FAIL;
5712}
5713
5714/*
5715 * Remove all entries matching {str} from a history.
5716 * "histype" may be one of the HIST_ values.
5717 */
5718 int
5719del_history_entry(histype, str)
5720 int histype;
5721 char_u *str;
5722{
5723 regmatch_T regmatch;
5724 histentry_T *hisptr;
5725 int idx;
5726 int i;
5727 int last;
5728 int found = FALSE;
5729
5730 regmatch.regprog = NULL;
5731 regmatch.rm_ic = FALSE; /* always match case */
5732 if (hislen != 0
5733 && histype >= 0
5734 && histype < HIST_COUNT
5735 && *str != NUL
5736 && (idx = hisidx[histype]) >= 0
5737 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5738 != NULL)
5739 {
5740 i = last = idx;
5741 do
5742 {
5743 hisptr = &history[histype][i];
5744 if (hisptr->hisstr == NULL)
5745 break;
5746 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5747 {
5748 found = TRUE;
5749 vim_free(hisptr->hisstr);
5750 hisptr->hisstr = NULL;
5751 hisptr->hisnum = 0;
5752 }
5753 else
5754 {
5755 if (i != last)
5756 {
5757 history[histype][last] = *hisptr;
5758 hisptr->hisstr = NULL;
5759 hisptr->hisnum = 0;
5760 }
5761 if (--last < 0)
5762 last += hislen;
5763 }
5764 if (--i < 0)
5765 i += hislen;
5766 } while (i != idx);
5767 if (history[histype][idx].hisstr == NULL)
5768 hisidx[histype] = -1;
5769 }
5770 vim_free(regmatch.regprog);
5771 return found;
5772}
5773
5774/*
5775 * Remove an indexed entry from a history.
5776 * "histype" may be one of the HIST_ values.
5777 */
5778 int
5779del_history_idx(histype, idx)
5780 int histype;
5781 int idx;
5782{
5783 int i, j;
5784
5785 i = calc_hist_idx(histype, idx);
5786 if (i < 0)
5787 return FALSE;
5788 idx = hisidx[histype];
5789 vim_free(history[histype][i].hisstr);
5790
5791 /* When deleting the last added search string in a mapping, reset
5792 * last_maptick, so that the last added search string isn't deleted again.
5793 */
5794 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5795 last_maptick = -1;
5796
5797 while (i != idx)
5798 {
5799 j = (i + 1) % hislen;
5800 history[histype][i] = history[histype][j];
5801 i = j;
5802 }
5803 history[histype][i].hisstr = NULL;
5804 history[histype][i].hisnum = 0;
5805 if (--i < 0)
5806 i += hislen;
5807 hisidx[histype] = i;
5808 return TRUE;
5809}
5810
5811#endif /* FEAT_EVAL */
5812
5813#if defined(FEAT_CRYPT) || defined(PROTO)
5814/*
5815 * Very specific function to remove the value in ":set key=val" from the
5816 * history.
5817 */
5818 void
5819remove_key_from_history()
5820{
5821 char_u *p;
5822 int i;
5823
5824 i = hisidx[HIST_CMD];
5825 if (i < 0)
5826 return;
5827 p = history[HIST_CMD][i].hisstr;
5828 if (p != NULL)
5829 for ( ; *p; ++p)
5830 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5831 {
5832 p = vim_strchr(p + 3, '=');
5833 if (p == NULL)
5834 break;
5835 ++p;
5836 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5837 if (p[i] == '\\' && p[i + 1])
5838 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005839 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 --p;
5841 }
5842}
5843#endif
5844
5845#endif /* FEAT_CMDHIST */
5846
5847#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5848/*
5849 * Get indices "num1,num2" that specify a range within a list (not a range of
5850 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5851 * Returns OK if parsed successfully, otherwise FAIL.
5852 */
5853 int
5854get_list_range(str, num1, num2)
5855 char_u **str;
5856 int *num1;
5857 int *num2;
5858{
5859 int len;
5860 int first = FALSE;
5861 long num;
5862
5863 *str = skipwhite(*str);
5864 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5865 {
5866 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5867 *str += len;
5868 *num1 = (int)num;
5869 first = TRUE;
5870 }
5871 *str = skipwhite(*str);
5872 if (**str == ',') /* parse "to" part of range */
5873 {
5874 *str = skipwhite(*str + 1);
5875 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5876 if (len > 0)
5877 {
5878 *num2 = (int)num;
5879 *str = skipwhite(*str + len);
5880 }
5881 else if (!first) /* no number given at all */
5882 return FAIL;
5883 }
5884 else if (first) /* only one number given */
5885 *num2 = *num1;
5886 return OK;
5887}
5888#endif
5889
5890#if defined(FEAT_CMDHIST) || defined(PROTO)
5891/*
5892 * :history command - print a history
5893 */
5894 void
5895ex_history(eap)
5896 exarg_T *eap;
5897{
5898 histentry_T *hist;
5899 int histype1 = HIST_CMD;
5900 int histype2 = HIST_CMD;
5901 int hisidx1 = 1;
5902 int hisidx2 = -1;
5903 int idx;
5904 int i, j, k;
5905 char_u *end;
5906 char_u *arg = eap->arg;
5907
5908 if (hislen == 0)
5909 {
5910 MSG(_("'history' option is zero"));
5911 return;
5912 }
5913
5914 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5915 {
5916 end = arg;
5917 while (ASCII_ISALPHA(*end)
5918 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5919 end++;
5920 i = *end;
5921 *end = NUL;
5922 histype1 = get_histtype(arg);
5923 if (histype1 == -1)
5924 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005925 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
5927 histype1 = 0;
5928 histype2 = HIST_COUNT-1;
5929 }
5930 else
5931 {
5932 *end = i;
5933 EMSG(_(e_trailing));
5934 return;
5935 }
5936 }
5937 else
5938 histype2 = histype1;
5939 *end = i;
5940 }
5941 else
5942 end = arg;
5943 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5944 {
5945 EMSG(_(e_trailing));
5946 return;
5947 }
5948
5949 for (; !got_int && histype1 <= histype2; ++histype1)
5950 {
5951 STRCPY(IObuff, "\n # ");
5952 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5953 MSG_PUTS_TITLE(IObuff);
5954 idx = hisidx[histype1];
5955 hist = history[histype1];
5956 j = hisidx1;
5957 k = hisidx2;
5958 if (j < 0)
5959 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5960 if (k < 0)
5961 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5962 if (idx >= 0 && j <= k)
5963 for (i = idx + 1; !got_int; ++i)
5964 {
5965 if (i == hislen)
5966 i = 0;
5967 if (hist[i].hisstr != NULL
5968 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5969 {
5970 msg_putchar('\n');
5971 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5972 hist[i].hisnum);
5973 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5974 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005975 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 else
5977 STRCAT(IObuff, hist[i].hisstr);
5978 msg_outtrans(IObuff);
5979 out_flush();
5980 }
5981 if (i == idx)
5982 break;
5983 }
5984 }
5985}
5986#endif
5987
5988#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5989static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5990static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5991static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5992static int viminfo_add_at_front = FALSE;
5993
5994static int hist_type2char __ARGS((int type, int use_question));
5995
5996/*
5997 * Translate a history type number to the associated character.
5998 */
5999 static int
6000hist_type2char(type, use_question)
6001 int type;
6002 int use_question; /* use '?' instead of '/' */
6003{
6004 if (type == HIST_CMD)
6005 return ':';
6006 if (type == HIST_SEARCH)
6007 {
6008 if (use_question)
6009 return '?';
6010 else
6011 return '/';
6012 }
6013 if (type == HIST_EXPR)
6014 return '=';
6015 return '@';
6016}
6017
6018/*
6019 * Prepare for reading the history from the viminfo file.
6020 * This allocates history arrays to store the read history lines.
6021 */
6022 void
6023prepare_viminfo_history(asklen)
6024 int asklen;
6025{
6026 int i;
6027 int num;
6028 int type;
6029 int len;
6030
6031 init_history();
6032 viminfo_add_at_front = (asklen != 0);
6033 if (asklen > hislen)
6034 asklen = hislen;
6035
6036 for (type = 0; type < HIST_COUNT; ++type)
6037 {
6038 /*
6039 * Count the number of empty spaces in the history list. If there are
6040 * more spaces available than we request, then fill them up.
6041 */
6042 for (i = 0, num = 0; i < hislen; i++)
6043 if (history[type][i].hisstr == NULL)
6044 num++;
6045 len = asklen;
6046 if (num > len)
6047 len = num;
6048 if (len <= 0)
6049 viminfo_history[type] = NULL;
6050 else
6051 viminfo_history[type] =
6052 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6053 if (viminfo_history[type] == NULL)
6054 len = 0;
6055 viminfo_hislen[type] = len;
6056 viminfo_hisidx[type] = 0;
6057 }
6058}
6059
6060/*
6061 * Accept a line from the viminfo, store it in the history array when it's
6062 * new.
6063 */
6064 int
6065read_viminfo_history(virp)
6066 vir_T *virp;
6067{
6068 int type;
6069 long_u len;
6070 char_u *val;
6071 char_u *p;
6072
6073 type = hist_char2type(virp->vir_line[0]);
6074 if (viminfo_hisidx[type] < viminfo_hislen[type])
6075 {
6076 val = viminfo_readstring(virp, 1, TRUE);
6077 if (val != NULL && *val != NUL)
6078 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006079 int sep = (*val == ' ' ? NUL : *val);
6080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006082 viminfo_add_at_front, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
6084 /* Need to re-allocate to append the separator byte. */
6085 len = STRLEN(val);
6086 p = lalloc(len + 2, TRUE);
6087 if (p != NULL)
6088 {
6089 if (type == HIST_SEARCH)
6090 {
6091 /* Search entry: Move the separator from the first
6092 * column to after the NUL. */
6093 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006094 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 }
6096 else
6097 {
6098 /* Not a search entry: No separator in the viminfo
6099 * file, add a NUL separator. */
6100 mch_memmove(p, val, (size_t)len + 1);
6101 p[len + 1] = NUL;
6102 }
6103 viminfo_history[type][viminfo_hisidx[type]++] = p;
6104 }
6105 }
6106 }
6107 vim_free(val);
6108 }
6109 return viminfo_readline(virp);
6110}
6111
6112 void
6113finish_viminfo_history()
6114{
6115 int idx;
6116 int i;
6117 int type;
6118
6119 for (type = 0; type < HIST_COUNT; ++type)
6120 {
6121 if (history[type] == NULL)
6122 return;
6123 idx = hisidx[type] + viminfo_hisidx[type];
6124 if (idx >= hislen)
6125 idx -= hislen;
6126 else if (idx < 0)
6127 idx = hislen - 1;
6128 if (viminfo_add_at_front)
6129 hisidx[type] = idx;
6130 else
6131 {
6132 if (hisidx[type] == -1)
6133 hisidx[type] = hislen - 1;
6134 do
6135 {
6136 if (history[type][idx].hisstr != NULL)
6137 break;
6138 if (++idx == hislen)
6139 idx = 0;
6140 } while (idx != hisidx[type]);
6141 if (idx != hisidx[type] && --idx < 0)
6142 idx = hislen - 1;
6143 }
6144 for (i = 0; i < viminfo_hisidx[type]; i++)
6145 {
6146 vim_free(history[type][idx].hisstr);
6147 history[type][idx].hisstr = viminfo_history[type][i];
6148 if (--idx < 0)
6149 idx = hislen - 1;
6150 }
6151 idx += 1;
6152 idx %= hislen;
6153 for (i = 0; i < viminfo_hisidx[type]; i++)
6154 {
6155 history[type][idx++].hisnum = ++hisnum[type];
6156 idx %= hislen;
6157 }
6158 vim_free(viminfo_history[type]);
6159 viminfo_history[type] = NULL;
6160 }
6161}
6162
6163 void
6164write_viminfo_history(fp)
6165 FILE *fp;
6166{
6167 int i;
6168 int type;
6169 int num_saved;
6170 char_u *p;
6171 int c;
6172
6173 init_history();
6174 if (hislen == 0)
6175 return;
6176 for (type = 0; type < HIST_COUNT; ++type)
6177 {
6178 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6179 if (num_saved == 0)
6180 continue;
6181 if (num_saved < 0) /* Use default */
6182 num_saved = hislen;
6183 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6184 type == HIST_CMD ? _("Command Line") :
6185 type == HIST_SEARCH ? _("Search String") :
6186 type == HIST_EXPR ? _("Expression") :
6187 _("Input Line"));
6188 if (num_saved > hislen)
6189 num_saved = hislen;
6190 i = hisidx[type];
6191 if (i >= 0)
6192 while (num_saved--)
6193 {
6194 p = history[type][i].hisstr;
6195 if (p != NULL)
6196 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006197 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 /* For the search history: put the separator in the second
6199 * column; use a space if there isn't one. */
6200 if (type == HIST_SEARCH)
6201 {
6202 c = p[STRLEN(p) + 1];
6203 putc(c == NUL ? ' ' : c, fp);
6204 }
6205 viminfo_writestring(fp, p);
6206 }
6207 if (--i < 0)
6208 i = hislen - 1;
6209 }
6210 }
6211}
6212#endif /* FEAT_VIMINFO */
6213
6214#if defined(FEAT_FKMAP) || defined(PROTO)
6215/*
6216 * Write a character at the current cursor+offset position.
6217 * It is directly written into the command buffer block.
6218 */
6219 void
6220cmd_pchar(c, offset)
6221 int c, offset;
6222{
6223 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6224 {
6225 EMSG(_("E198: cmd_pchar beyond the command length"));
6226 return;
6227 }
6228 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6229 ccline.cmdbuff[ccline.cmdlen] = NUL;
6230}
6231
6232 int
6233cmd_gchar(offset)
6234 int offset;
6235{
6236 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6237 {
6238 /* EMSG(_("cmd_gchar beyond the command length")); */
6239 return NUL;
6240 }
6241 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6242}
6243#endif
6244
6245#if defined(FEAT_CMDWIN) || defined(PROTO)
6246/*
6247 * Open a window on the current command line and history. Allow editing in
6248 * the window. Returns when the window is closed.
6249 * Returns:
6250 * CR if the command is to be executed
6251 * Ctrl_C if it is to be abandoned
6252 * K_IGNORE if editing continues
6253 */
6254 static int
6255ex_window()
6256{
6257 struct cmdline_info save_ccline;
6258 buf_T *old_curbuf = curbuf;
6259 win_T *old_curwin = curwin;
6260 buf_T *bp;
6261 win_T *wp;
6262 int i;
6263 linenr_T lnum;
6264 int histtype;
6265 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006266#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 int save_restart_edit = restart_edit;
6270 int save_State = State;
6271 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006272#ifdef FEAT_RIGHTLEFT
6273 int save_cmdmsg_rl = cmdmsg_rl;
6274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
6276 /* Can't do this recursively. Can't do it when typing a password. */
6277 if (cmdwin_type != 0
6278# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6279 || cmdline_star > 0
6280# endif
6281 )
6282 {
6283 beep_flush();
6284 return K_IGNORE;
6285 }
6286
6287 /* Save current window sizes. */
6288 win_size_save(&winsizes);
6289
6290# ifdef FEAT_AUTOCMD
6291 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006292 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006294 /* don't use a new tab page */
6295 cmdmod.tab = 0;
6296
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 /* Create a window for the command-line buffer. */
6298 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6299 {
6300 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006301# ifdef FEAT_AUTOCMD
6302 unblock_autocmds();
6303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 return K_IGNORE;
6305 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006306 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
6308 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006309 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006310 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6312 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6313 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006314#ifdef FEAT_FOLDING
6315 curwin->w_p_fen = FALSE;
6316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006318 curwin->w_p_rl = cmdmsg_rl;
6319 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006321 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322
6323# ifdef FEAT_AUTOCMD
6324 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006325 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326# endif
6327
Bram Moolenaar46152342005-09-07 21:18:43 +00006328 /* Showing the prompt may have set need_wait_return, reset it. */
6329 need_wait_return = FALSE;
6330
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006331 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6333 {
6334 if (p_wc == TAB)
6335 {
6336 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6337 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6338 }
6339 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6340 }
6341
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006342 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6343 * sets 'textwidth' to 78). */
6344 curbuf->b_p_tw = 0;
6345
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 /* Fill the buffer with the history. */
6347 init_history();
6348 if (hislen > 0)
6349 {
6350 i = hisidx[histtype];
6351 if (i >= 0)
6352 {
6353 lnum = 0;
6354 do
6355 {
6356 if (++i == hislen)
6357 i = 0;
6358 if (history[histtype][i].hisstr != NULL)
6359 ml_append(lnum++, history[histtype][i].hisstr,
6360 (colnr_T)0, FALSE);
6361 }
6362 while (i != hisidx[histtype]);
6363 }
6364 }
6365
6366 /* Replace the empty last line with the current command-line and put the
6367 * cursor there. */
6368 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6369 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6370 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006371 changed_line_abv_curs();
6372 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006373 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
6375 /* Save the command line info, can be used recursively. */
6376 save_ccline = ccline;
6377 ccline.cmdbuff = NULL;
6378 ccline.cmdprompt = NULL;
6379
6380 /* No Ex mode here! */
6381 exmode_active = 0;
6382
6383 State = NORMAL;
6384# ifdef FEAT_MOUSE
6385 setmouse();
6386# endif
6387
6388# ifdef FEAT_AUTOCMD
6389 /* Trigger CmdwinEnter autocommands. */
6390 typestr[0] = cmdwin_type;
6391 typestr[1] = NUL;
6392 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006393 if (restart_edit != 0) /* autocmd with ":startinsert" */
6394 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395# endif
6396
6397 i = RedrawingDisabled;
6398 RedrawingDisabled = 0;
6399
6400 /*
6401 * Call the main loop until <CR> or CTRL-C is typed.
6402 */
6403 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006404 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
6406 RedrawingDisabled = i;
6407
6408# ifdef FEAT_AUTOCMD
6409 /* Trigger CmdwinLeave autocommands. */
6410 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6411# endif
6412
Bram Moolenaarccc18222007-05-10 18:25:20 +00006413 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 ccline = save_ccline;
6415 cmdwin_type = 0;
6416
6417 exmode_active = save_exmode;
6418
Bram Moolenaarf9821062008-06-20 16:31:07 +00006419 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 * this happens! */
6421 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6422 {
6423 cmdwin_result = Ctrl_C;
6424 EMSG(_("E199: Active window or buffer deleted"));
6425 }
6426 else
6427 {
6428# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6429 /* autocmds may abort script processing */
6430 if (aborting() && cmdwin_result != K_IGNORE)
6431 cmdwin_result = Ctrl_C;
6432# endif
6433 /* Set the new command line from the cmdline buffer. */
6434 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006435 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006437 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6438
6439 if (histtype == HIST_CMD)
6440 {
6441 /* Execute the command directly. */
6442 ccline.cmdbuff = vim_strsave((char_u *)p);
6443 cmdwin_result = CAR;
6444 }
6445 else
6446 {
6447 /* First need to cancel what we were doing. */
6448 ccline.cmdbuff = NULL;
6449 stuffcharReadbuff(':');
6450 stuffReadbuff((char_u *)p);
6451 stuffcharReadbuff(CAR);
6452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 }
6454 else if (cmdwin_result == K_XF2) /* :qa typed */
6455 {
6456 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6457 cmdwin_result = CAR;
6458 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006459 else if (cmdwin_result == Ctrl_C)
6460 {
6461 /* :q or :close, don't execute any command
6462 * and don't modify the cmd window. */
6463 ccline.cmdbuff = NULL;
6464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 else
6466 ccline.cmdbuff = vim_strsave(ml_get_curline());
6467 if (ccline.cmdbuff == NULL)
6468 cmdwin_result = Ctrl_C;
6469 else
6470 {
6471 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6472 ccline.cmdbufflen = ccline.cmdlen + 1;
6473 ccline.cmdpos = curwin->w_cursor.col;
6474 if (ccline.cmdpos > ccline.cmdlen)
6475 ccline.cmdpos = ccline.cmdlen;
6476 if (cmdwin_result == K_IGNORE)
6477 {
6478 set_cmdspos_cursor();
6479 redrawcmd();
6480 }
6481 }
6482
6483# ifdef FEAT_AUTOCMD
6484 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006485 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486# endif
6487 wp = curwin;
6488 bp = curbuf;
6489 win_goto(old_curwin);
6490 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006491
6492 /* win_close() may have already wiped the buffer when 'bh' is
6493 * set to 'wipe' */
6494 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006495 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496
6497 /* Restore window sizes. */
6498 win_size_restore(&winsizes);
6499
6500# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006501 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502# endif
6503 }
6504
6505 ga_clear(&winsizes);
6506 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006507# ifdef FEAT_RIGHTLEFT
6508 cmdmsg_rl = save_cmdmsg_rl;
6509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511 State = save_State;
6512# ifdef FEAT_MOUSE
6513 setmouse();
6514# endif
6515
6516 return cmdwin_result;
6517}
6518#endif /* FEAT_CMDWIN */
6519
6520/*
6521 * Used for commands that either take a simple command string argument, or:
6522 * cmd << endmarker
6523 * {script}
6524 * endmarker
6525 * Returns a pointer to allocated memory with {script} or NULL.
6526 */
6527 char_u *
6528script_get(eap, cmd)
6529 exarg_T *eap;
6530 char_u *cmd;
6531{
6532 char_u *theline;
6533 char *end_pattern = NULL;
6534 char dot[] = ".";
6535 garray_T ga;
6536
6537 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6538 return NULL;
6539
6540 ga_init2(&ga, 1, 0x400);
6541
6542 if (cmd[2] != NUL)
6543 end_pattern = (char *)skipwhite(cmd + 2);
6544 else
6545 end_pattern = dot;
6546
6547 for (;;)
6548 {
6549 theline = eap->getline(
6550#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006551 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552#endif
6553 NUL, eap->cookie, 0);
6554
6555 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006556 {
6557 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
6561 ga_concat(&ga, theline);
6562 ga_append(&ga, '\n');
6563 vim_free(theline);
6564 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006565 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
6567 return (char_u *)ga.ga_data;
6568}