blob: 249dafb5868ce423e28d6e76405a5da831b0d334 [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;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200670 /* CTRL-\ e doesn't work when obtaining an expression, unless it
671 * is in a mapping. */
672 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
673 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
675 vungetc(c);
676 c = Ctrl_BSL;
677 }
678#ifdef FEAT_EVAL
679 else if (c == 'e')
680 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200681 char_u *p = NULL;
682 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
684 /*
685 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000686 * Need to save and restore the current command line, to be
687 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 */
689 if (ccline.cmdpos == ccline.cmdlen)
690 new_cmdpos = 99999; /* keep it at the end */
691 else
692 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000693
694 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000696 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 if (c == '=')
698 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000699 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000700 * to avoid nasty things like going to another buffer when
701 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000702 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000703 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000705 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000706 restore_cmdline(&save_ccline);
707
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200708 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200710 len = (int)STRLEN(p);
711 if (realloc_cmdbuff(len + 1) == OK)
712 {
713 ccline.cmdlen = len;
714 STRCPY(ccline.cmdbuff, p);
715 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200717 /* Restore the cursor or use the position set with
718 * set_cmdline_pos(). */
719 if (new_cmdpos > ccline.cmdlen)
720 ccline.cmdpos = ccline.cmdlen;
721 else
722 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200724 KeyTyped = FALSE; /* Don't do p_wc completion. */
725 redrawcmd();
726 goto cmdline_changed;
727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 }
729 }
730 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100731 got_int = FALSE; /* don't abandon the command line */
732 did_emsg = FALSE;
733 emsg_on_display = FALSE;
734 redrawcmd();
735 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
737#endif
738 else
739 {
740 if (c == Ctrl_G && p_im && restart_edit == 0)
741 restart_edit = 'a';
742 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
743 in history */
744 goto returncmd; /* back to Normal mode */
745 }
746 }
747
748#ifdef FEAT_CMDWIN
749 if (c == cedit_key || c == K_CMDWIN)
750 {
751 /*
752 * Open a window to edit the command line (and history).
753 */
754 c = ex_window();
755 some_key_typed = TRUE;
756 }
757# ifdef FEAT_DIGRAPHS
758 else
759# endif
760#endif
761#ifdef FEAT_DIGRAPHS
762 c = do_digraph(c);
763#endif
764
765 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
766 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
767 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000768 /* In Ex mode a backslash escapes a newline. */
769 if (exmode_active
770 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000771 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000772 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000773 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000775 if (c == K_KENTER)
776 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000778 else
779 {
780 gotesc = FALSE; /* Might have typed ESC previously, don't
781 truncate the cmdline now. */
782 if (ccheck_abbr(c + ABBR_OFF))
783 goto cmdline_changed;
784 if (!cmd_silent)
785 {
786 windgoto(msg_row, 0);
787 out_flush();
788 }
789 break;
790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 }
792
793 /*
794 * Completion for 'wildchar' or 'wildcharm' key.
795 * - hitting <ESC> twice means: abandon command line.
796 * - wildcard expansion is only done when the 'wildchar' key is really
797 * typed, not when it comes from a macro
798 */
799 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
800 {
801 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
802 {
803 /* if 'wildmode' contains "list" may still need to list */
804 if (xpc.xp_numfiles > 1
805 && !did_wild_list
806 && (wim_flags[wim_index] & WIM_LIST))
807 {
808 (void)showmatches(&xpc, FALSE);
809 redrawcmd();
810 did_wild_list = TRUE;
811 }
812 if (wim_flags[wim_index] & WIM_LONGEST)
813 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
814 else if (wim_flags[wim_index] & WIM_FULL)
815 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
816 else
817 res = OK; /* don't insert 'wildchar' now */
818 }
819 else /* typed p_wc first time */
820 {
821 wim_index = 0;
822 j = ccline.cmdpos;
823 /* if 'wildmode' first contains "longest", get longest
824 * common part */
825 if (wim_flags[0] & WIM_LONGEST)
826 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
827 else
828 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
829
830 /* if interrupted while completing, behave like it failed */
831 if (got_int)
832 {
833 (void)vpeekc(); /* remove <C-C> from input stream */
834 got_int = FALSE; /* don't abandon the command line */
835 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
836#ifdef FEAT_WILDMENU
837 xpc.xp_context = EXPAND_NOTHING;
838#endif
839 goto cmdline_changed;
840 }
841
842 /* when more than one match, and 'wildmode' first contains
843 * "list", or no change and 'wildmode' contains "longest,list",
844 * list all matches */
845 if (res == OK && xpc.xp_numfiles > 1)
846 {
847 /* a "longest" that didn't do anything is skipped (but not
848 * "list:longest") */
849 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
850 wim_index = 1;
851 if ((wim_flags[wim_index] & WIM_LIST)
852#ifdef FEAT_WILDMENU
853 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
854#endif
855 )
856 {
857 if (!(wim_flags[0] & WIM_LONGEST))
858 {
859#ifdef FEAT_WILDMENU
860 int p_wmnu_save = p_wmnu;
861 p_wmnu = 0;
862#endif
863 nextwild(&xpc, WILD_PREV, 0); /* remove match */
864#ifdef FEAT_WILDMENU
865 p_wmnu = p_wmnu_save;
866#endif
867 }
868#ifdef FEAT_WILDMENU
869 (void)showmatches(&xpc, p_wmnu
870 && ((wim_flags[wim_index] & WIM_LIST) == 0));
871#else
872 (void)showmatches(&xpc, FALSE);
873#endif
874 redrawcmd();
875 did_wild_list = TRUE;
876 if (wim_flags[wim_index] & WIM_LONGEST)
877 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
878 else if (wim_flags[wim_index] & WIM_FULL)
879 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
880 }
881 else
882 vim_beep();
883 }
884#ifdef FEAT_WILDMENU
885 else if (xpc.xp_numfiles == -1)
886 xpc.xp_context = EXPAND_NOTHING;
887#endif
888 }
889 if (wim_index < 3)
890 ++wim_index;
891 if (c == ESC)
892 gotesc = TRUE;
893 if (res == OK)
894 goto cmdline_changed;
895 }
896
897 gotesc = FALSE;
898
899 /* <S-Tab> goes to last match, in a clumsy way */
900 if (c == K_S_TAB && KeyTyped)
901 {
902 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
903 && nextwild(&xpc, WILD_PREV, 0) == OK
904 && nextwild(&xpc, WILD_PREV, 0) == OK)
905 goto cmdline_changed;
906 }
907
908 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
909 c = NL;
910
911 do_abbr = TRUE; /* default: check for abbreviation */
912
913 /*
914 * Big switch for a typed command line character.
915 */
916 switch (c)
917 {
918 case K_BS:
919 case Ctrl_H:
920 case K_DEL:
921 case K_KDEL:
922 case Ctrl_W:
923#ifdef FEAT_FKMAP
924 if (cmd_fkmap && c == K_BS)
925 c = K_DEL;
926#endif
927 if (c == K_KDEL)
928 c = K_DEL;
929
930 /*
931 * delete current character is the same as backspace on next
932 * character, except at end of line
933 */
934 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
935 ++ccline.cmdpos;
936#ifdef FEAT_MBYTE
937 if (has_mbyte && c == K_DEL)
938 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
939 ccline.cmdbuff + ccline.cmdpos);
940#endif
941 if (ccline.cmdpos > 0)
942 {
943 char_u *p;
944
945 j = ccline.cmdpos;
946 p = ccline.cmdbuff + j;
947#ifdef FEAT_MBYTE
948 if (has_mbyte)
949 {
950 p = mb_prevptr(ccline.cmdbuff, p);
951 if (c == Ctrl_W)
952 {
953 while (p > ccline.cmdbuff && vim_isspace(*p))
954 p = mb_prevptr(ccline.cmdbuff, p);
955 i = mb_get_class(p);
956 while (p > ccline.cmdbuff && mb_get_class(p) == i)
957 p = mb_prevptr(ccline.cmdbuff, p);
958 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000959 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 }
961 }
962 else
963#endif
964 if (c == Ctrl_W)
965 {
966 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
967 --p;
968 i = vim_iswordc(p[-1]);
969 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
970 && vim_iswordc(p[-1]) == i)
971 --p;
972 }
973 else
974 --p;
975 ccline.cmdpos = (int)(p - ccline.cmdbuff);
976 ccline.cmdlen -= j - ccline.cmdpos;
977 i = ccline.cmdpos;
978 while (i < ccline.cmdlen)
979 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
980
981 /* Truncate at the end, required for multi-byte chars. */
982 ccline.cmdbuff[ccline.cmdlen] = NUL;
983 redrawcmd();
984 }
985 else if (ccline.cmdlen == 0 && c != Ctrl_W
986 && ccline.cmdprompt == NULL && indent == 0)
987 {
988 /* In ex and debug mode it doesn't make sense to return. */
989 if (exmode_active
990#ifdef FEAT_EVAL
991 || ccline.cmdfirstc == '>'
992#endif
993 )
994 goto cmdline_not_changed;
995
996 vim_free(ccline.cmdbuff); /* no commandline to return */
997 ccline.cmdbuff = NULL;
998 if (!cmd_silent)
999 {
1000#ifdef FEAT_RIGHTLEFT
1001 if (cmdmsg_rl)
1002 msg_col = Columns;
1003 else
1004#endif
1005 msg_col = 0;
1006 msg_putchar(' '); /* delete ':' */
1007 }
1008 redraw_cmdline = TRUE;
1009 goto returncmd; /* back to cmd mode */
1010 }
1011 goto cmdline_changed;
1012
1013 case K_INS:
1014 case K_KINS:
1015#ifdef FEAT_FKMAP
1016 /* if Farsi mode set, we are in reverse insert mode -
1017 Do not change the mode */
1018 if (cmd_fkmap)
1019 beep_flush();
1020 else
1021#endif
1022 ccline.overstrike = !ccline.overstrike;
1023#ifdef CURSOR_SHAPE
1024 ui_cursor_shape(); /* may show different cursor shape */
1025#endif
1026 goto cmdline_not_changed;
1027
1028 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001029 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 {
1031 /* ":lmap" mappings exists, toggle use of mappings. */
1032 State ^= LANGMAP;
1033#ifdef USE_IM_CONTROL
1034 im_set_active(FALSE); /* Disable input method */
1035#endif
1036 if (b_im_ptr != NULL)
1037 {
1038 if (State & LANGMAP)
1039 *b_im_ptr = B_IMODE_LMAP;
1040 else
1041 *b_im_ptr = B_IMODE_NONE;
1042 }
1043 }
1044#ifdef USE_IM_CONTROL
1045 else
1046 {
1047 /* There are no ":lmap" mappings, toggle IM. When
1048 * 'imdisable' is set don't try getting the status, it's
1049 * always off. */
1050 if ((p_imdisable && b_im_ptr != NULL)
1051 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1052 {
1053 im_set_active(FALSE); /* Disable input method */
1054 if (b_im_ptr != NULL)
1055 *b_im_ptr = B_IMODE_NONE;
1056 }
1057 else
1058 {
1059 im_set_active(TRUE); /* Enable input method */
1060 if (b_im_ptr != NULL)
1061 *b_im_ptr = B_IMODE_IM;
1062 }
1063 }
1064#endif
1065 if (b_im_ptr != NULL)
1066 {
1067 if (b_im_ptr == &curbuf->b_p_iminsert)
1068 set_iminsert_global();
1069 else
1070 set_imsearch_global();
1071 }
1072#ifdef CURSOR_SHAPE
1073 ui_cursor_shape(); /* may show different cursor shape */
1074#endif
1075#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1076 /* Show/unshow value of 'keymap' in status lines later. */
1077 status_redraw_curbuf();
1078#endif
1079 goto cmdline_not_changed;
1080
1081/* case '@': only in very old vi */
1082 case Ctrl_U:
1083 /* delete all characters left of the cursor */
1084 j = ccline.cmdpos;
1085 ccline.cmdlen -= j;
1086 i = ccline.cmdpos = 0;
1087 while (i < ccline.cmdlen)
1088 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1089 /* Truncate at the end, required for multi-byte chars. */
1090 ccline.cmdbuff[ccline.cmdlen] = NUL;
1091 redrawcmd();
1092 goto cmdline_changed;
1093
1094#ifdef FEAT_CLIPBOARD
1095 case Ctrl_Y:
1096 /* Copy the modeless selection, if there is one. */
1097 if (clip_star.state != SELECT_CLEARED)
1098 {
1099 if (clip_star.state == SELECT_DONE)
1100 clip_copy_modeless_selection(TRUE);
1101 goto cmdline_not_changed;
1102 }
1103 break;
1104#endif
1105
1106 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1107 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001108 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001109 * ":normal" runs out of characters. */
1110 if (exmode_active
1111#ifdef FEAT_EX_EXTRA
1112 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1113#endif
1114 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 goto cmdline_not_changed;
1116
1117 gotesc = TRUE; /* will free ccline.cmdbuff after
1118 putting it in history */
1119 goto returncmd; /* back to cmd mode */
1120
1121 case Ctrl_R: /* insert register */
1122#ifdef USE_ON_FLY_SCROLL
1123 dont_scroll = TRUE; /* disallow scrolling here */
1124#endif
1125 putcmdline('"', TRUE);
1126 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001127 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 if (i == Ctrl_O)
1129 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1130 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001131 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 --no_mapping;
1133#ifdef FEAT_EVAL
1134 /*
1135 * Insert the result of an expression.
1136 * Need to save the current command line, to be able to enter
1137 * a new one...
1138 */
1139 new_cmdpos = -1;
1140 if (c == '=')
1141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1143 {
1144 beep_flush();
1145 c = ESC;
1146 }
1147 else
1148 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001149 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001151 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 }
1153 }
1154#endif
1155 if (c != ESC) /* use ESC to cancel inserting register */
1156 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001157 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001158
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001159#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001160 /* When there was a serious error abort getting the
1161 * command line. */
1162 if (aborting())
1163 {
1164 gotesc = TRUE; /* will free ccline.cmdbuff after
1165 putting it in history */
1166 goto returncmd; /* back to cmd mode */
1167 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 KeyTyped = FALSE; /* Don't do p_wc completion. */
1170#ifdef FEAT_EVAL
1171 if (new_cmdpos >= 0)
1172 {
1173 /* set_cmdline_pos() was used */
1174 if (new_cmdpos > ccline.cmdlen)
1175 ccline.cmdpos = ccline.cmdlen;
1176 else
1177 ccline.cmdpos = new_cmdpos;
1178 }
1179#endif
1180 }
1181 redrawcmd();
1182 goto cmdline_changed;
1183
1184 case Ctrl_D:
1185 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1186 break; /* Use ^D as normal char instead */
1187
1188 redrawcmd();
1189 continue; /* don't do incremental search now */
1190
1191 case K_RIGHT:
1192 case K_S_RIGHT:
1193 case K_C_RIGHT:
1194 do
1195 {
1196 if (ccline.cmdpos >= ccline.cmdlen)
1197 break;
1198 i = cmdline_charsize(ccline.cmdpos);
1199 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1200 break;
1201 ccline.cmdspos += i;
1202#ifdef FEAT_MBYTE
1203 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001204 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 + ccline.cmdpos);
1206 else
1207#endif
1208 ++ccline.cmdpos;
1209 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001210 while ((c == K_S_RIGHT || c == K_C_RIGHT
1211 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1213#ifdef FEAT_MBYTE
1214 if (has_mbyte)
1215 set_cmdspos_cursor();
1216#endif
1217 goto cmdline_not_changed;
1218
1219 case K_LEFT:
1220 case K_S_LEFT:
1221 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001222 if (ccline.cmdpos == 0)
1223 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 do
1225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 --ccline.cmdpos;
1227#ifdef FEAT_MBYTE
1228 if (has_mbyte) /* move to first byte of char */
1229 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1230 ccline.cmdbuff + ccline.cmdpos);
1231#endif
1232 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1233 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001234 while (ccline.cmdpos > 0
1235 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001236 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1238#ifdef FEAT_MBYTE
1239 if (has_mbyte)
1240 set_cmdspos_cursor();
1241#endif
1242 goto cmdline_not_changed;
1243
1244 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001245 /* Ignore mouse event or ex_window() result. */
1246 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
Bram Moolenaar4770d092006-01-12 23:22:24 +00001248#ifdef FEAT_GUI_W32
1249 /* On Win32 ignore <M-F4>, we get it when closing the window was
1250 * cancelled. */
1251 case K_F4:
1252 if (mod_mask == MOD_MASK_ALT)
1253 {
1254 redrawcmd(); /* somehow the cmdline is cleared */
1255 goto cmdline_not_changed;
1256 }
1257 break;
1258#endif
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260#ifdef FEAT_MOUSE
1261 case K_MIDDLEDRAG:
1262 case K_MIDDLERELEASE:
1263 goto cmdline_not_changed; /* Ignore mouse */
1264
1265 case K_MIDDLEMOUSE:
1266# ifdef FEAT_GUI
1267 /* When GUI is active, also paste when 'mouse' is empty */
1268 if (!gui.in_use)
1269# endif
1270 if (!mouse_has(MOUSE_COMMAND))
1271 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001272# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001274 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001276# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001277 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 redrawcmd();
1279 goto cmdline_changed;
1280
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001281# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001283 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 redrawcmd();
1285 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001286# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287
1288 case K_LEFTDRAG:
1289 case K_LEFTRELEASE:
1290 case K_RIGHTDRAG:
1291 case K_RIGHTRELEASE:
1292 /* Ignore drag and release events when the button-down wasn't
1293 * seen before. */
1294 if (ignore_drag_release)
1295 goto cmdline_not_changed;
1296 /* FALLTHROUGH */
1297 case K_LEFTMOUSE:
1298 case K_RIGHTMOUSE:
1299 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1300 ignore_drag_release = TRUE;
1301 else
1302 ignore_drag_release = FALSE;
1303# ifdef FEAT_GUI
1304 /* When GUI is active, also move when 'mouse' is empty */
1305 if (!gui.in_use)
1306# endif
1307 if (!mouse_has(MOUSE_COMMAND))
1308 goto cmdline_not_changed; /* Ignore mouse */
1309# ifdef FEAT_CLIPBOARD
1310 if (mouse_row < cmdline_row && clip_star.available)
1311 {
1312 int button, is_click, is_drag;
1313
1314 /*
1315 * Handle modeless selection.
1316 */
1317 button = get_mouse_button(KEY2TERMCAP1(c),
1318 &is_click, &is_drag);
1319 if (mouse_model_popup() && button == MOUSE_LEFT
1320 && (mod_mask & MOD_MASK_SHIFT))
1321 {
1322 /* Translate shift-left to right button. */
1323 button = MOUSE_RIGHT;
1324 mod_mask &= ~MOD_MASK_SHIFT;
1325 }
1326 clip_modeless(button, is_click, is_drag);
1327 goto cmdline_not_changed;
1328 }
1329# endif
1330
1331 set_cmdspos();
1332 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1333 ++ccline.cmdpos)
1334 {
1335 i = cmdline_charsize(ccline.cmdpos);
1336 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1337 && mouse_col < ccline.cmdspos % Columns + i)
1338 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001339# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (has_mbyte)
1341 {
1342 /* Count ">" for double-wide char that doesn't fit. */
1343 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001344 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 + ccline.cmdpos) - 1;
1346 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 ccline.cmdspos += i;
1349 }
1350 goto cmdline_not_changed;
1351
1352 /* Mouse scroll wheel: ignored here */
1353 case K_MOUSEDOWN:
1354 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001355 case K_MOUSELEFT:
1356 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 /* Alternate buttons ignored here */
1358 case K_X1MOUSE:
1359 case K_X1DRAG:
1360 case K_X1RELEASE:
1361 case K_X2MOUSE:
1362 case K_X2DRAG:
1363 case K_X2RELEASE:
1364 goto cmdline_not_changed;
1365
1366#endif /* FEAT_MOUSE */
1367
1368#ifdef FEAT_GUI
1369 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1370 case K_LEFTRELEASE_NM:
1371 goto cmdline_not_changed;
1372
1373 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001374 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 {
1376 gui_do_scroll();
1377 redrawcmd();
1378 }
1379 goto cmdline_not_changed;
1380
1381 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001382 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001384 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 redrawcmd();
1386 }
1387 goto cmdline_not_changed;
1388#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001389#ifdef FEAT_GUI_TABLINE
1390 case K_TABLINE:
1391 case K_TABMENU:
1392 /* Don't want to change any tabs here. Make sure the same tab
1393 * is still selected. */
1394 if (gui_use_tabline())
1395 gui_mch_set_curtab(tabpage_index(curtab));
1396 goto cmdline_not_changed;
1397#endif
1398
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 case K_SELECT: /* end of Select mode mapping - ignore */
1400 goto cmdline_not_changed;
1401
1402 case Ctrl_B: /* begin of command line */
1403 case K_HOME:
1404 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 case K_S_HOME:
1406 case K_C_HOME:
1407 ccline.cmdpos = 0;
1408 set_cmdspos();
1409 goto cmdline_not_changed;
1410
1411 case Ctrl_E: /* end of command line */
1412 case K_END:
1413 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 case K_S_END:
1415 case K_C_END:
1416 ccline.cmdpos = ccline.cmdlen;
1417 set_cmdspos_cursor();
1418 goto cmdline_not_changed;
1419
1420 case Ctrl_A: /* all matches */
1421 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1422 break;
1423 goto cmdline_changed;
1424
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001425 case Ctrl_L:
1426#ifdef FEAT_SEARCH_EXTRA
1427 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1428 {
1429 /* Add a character from under the cursor for 'incsearch' */
1430 if (did_incsearch
1431 && !equalpos(curwin->w_cursor, old_cursor))
1432 {
1433 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001434 /* If 'ignorecase' and 'smartcase' are set and the
1435 * command line has no uppercase characters, convert
1436 * the character to lowercase */
1437 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1438 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001439 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001440 {
1441 if (c == firstc || vim_strchr((char_u *)(
1442 p_magic ? "\\^$.*[" : "\\^$"), c)
1443 != NULL)
1444 {
1445 /* put a backslash before special characters */
1446 stuffcharReadbuff(c);
1447 c = '\\';
1448 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001449 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001450 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001451 }
1452 goto cmdline_not_changed;
1453 }
1454#endif
1455
1456 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1458 break;
1459 goto cmdline_changed;
1460
1461 case Ctrl_N: /* next match */
1462 case Ctrl_P: /* previous match */
1463 if (xpc.xp_numfiles > 0)
1464 {
1465 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1466 == FAIL)
1467 break;
1468 goto cmdline_changed;
1469 }
1470
1471#ifdef FEAT_CMDHIST
1472 case K_UP:
1473 case K_DOWN:
1474 case K_S_UP:
1475 case K_S_DOWN:
1476 case K_PAGEUP:
1477 case K_KPAGEUP:
1478 case K_PAGEDOWN:
1479 case K_KPAGEDOWN:
1480 if (hislen == 0 || firstc == NUL) /* no history */
1481 goto cmdline_not_changed;
1482
1483 i = hiscnt;
1484
1485 /* save current command string so it can be restored later */
1486 if (lookfor == NULL)
1487 {
1488 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1489 goto cmdline_not_changed;
1490 lookfor[ccline.cmdpos] = NUL;
1491 }
1492
1493 j = (int)STRLEN(lookfor);
1494 for (;;)
1495 {
1496 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001497 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001498 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 {
1500 if (hiscnt == hislen) /* first time */
1501 hiscnt = hisidx[histype];
1502 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1503 hiscnt = hislen - 1;
1504 else if (hiscnt != hisidx[histype] + 1)
1505 --hiscnt;
1506 else /* at top of list */
1507 {
1508 hiscnt = i;
1509 break;
1510 }
1511 }
1512 else /* one step forwards */
1513 {
1514 /* on last entry, clear the line */
1515 if (hiscnt == hisidx[histype])
1516 {
1517 hiscnt = hislen;
1518 break;
1519 }
1520
1521 /* not on a history line, nothing to do */
1522 if (hiscnt == hislen)
1523 break;
1524 if (hiscnt == hislen - 1) /* wrap around */
1525 hiscnt = 0;
1526 else
1527 ++hiscnt;
1528 }
1529 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1530 {
1531 hiscnt = i;
1532 break;
1533 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001534 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001535 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 || STRNCMP(history[histype][hiscnt].hisstr,
1537 lookfor, (size_t)j) == 0)
1538 break;
1539 }
1540
1541 if (hiscnt != i) /* jumped to other entry */
1542 {
1543 char_u *p;
1544 int len;
1545 int old_firstc;
1546
1547 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001548 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (hiscnt == hislen)
1550 p = lookfor; /* back to the old one */
1551 else
1552 p = history[histype][hiscnt].hisstr;
1553
1554 if (histype == HIST_SEARCH
1555 && p != lookfor
1556 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1557 {
1558 /* Correct for the separator character used when
1559 * adding the history entry vs the one used now.
1560 * First loop: count length.
1561 * Second loop: copy the characters. */
1562 for (i = 0; i <= 1; ++i)
1563 {
1564 len = 0;
1565 for (j = 0; p[j] != NUL; ++j)
1566 {
1567 /* Replace old sep with new sep, unless it is
1568 * escaped. */
1569 if (p[j] == old_firstc
1570 && (j == 0 || p[j - 1] != '\\'))
1571 {
1572 if (i > 0)
1573 ccline.cmdbuff[len] = firstc;
1574 }
1575 else
1576 {
1577 /* Escape new sep, unless it is already
1578 * escaped. */
1579 if (p[j] == firstc
1580 && (j == 0 || p[j - 1] != '\\'))
1581 {
1582 if (i > 0)
1583 ccline.cmdbuff[len] = '\\';
1584 ++len;
1585 }
1586 if (i > 0)
1587 ccline.cmdbuff[len] = p[j];
1588 }
1589 ++len;
1590 }
1591 if (i == 0)
1592 {
1593 alloc_cmdbuff(len);
1594 if (ccline.cmdbuff == NULL)
1595 goto returncmd;
1596 }
1597 }
1598 ccline.cmdbuff[len] = NUL;
1599 }
1600 else
1601 {
1602 alloc_cmdbuff((int)STRLEN(p));
1603 if (ccline.cmdbuff == NULL)
1604 goto returncmd;
1605 STRCPY(ccline.cmdbuff, p);
1606 }
1607
1608 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1609 redrawcmd();
1610 goto cmdline_changed;
1611 }
1612 beep_flush();
1613 goto cmdline_not_changed;
1614#endif
1615
1616 case Ctrl_V:
1617 case Ctrl_Q:
1618#ifdef FEAT_MOUSE
1619 ignore_drag_release = TRUE;
1620#endif
1621 putcmdline('^', TRUE);
1622 c = get_literal(); /* get next (two) character(s) */
1623 do_abbr = FALSE; /* don't do abbreviation now */
1624#ifdef FEAT_MBYTE
1625 /* may need to remove ^ when composing char was typed */
1626 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1627 {
1628 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1629 msg_putchar(' ');
1630 cursorcmd();
1631 }
1632#endif
1633 break;
1634
1635#ifdef FEAT_DIGRAPHS
1636 case Ctrl_K:
1637#ifdef FEAT_MOUSE
1638 ignore_drag_release = TRUE;
1639#endif
1640 putcmdline('?', TRUE);
1641#ifdef USE_ON_FLY_SCROLL
1642 dont_scroll = TRUE; /* disallow scrolling here */
1643#endif
1644 c = get_digraph(TRUE);
1645 if (c != NUL)
1646 break;
1647
1648 redrawcmd();
1649 goto cmdline_not_changed;
1650#endif /* FEAT_DIGRAPHS */
1651
1652#ifdef FEAT_RIGHTLEFT
1653 case Ctrl__: /* CTRL-_: switch language mode */
1654 if (!p_ari)
1655 break;
1656#ifdef FEAT_FKMAP
1657 if (p_altkeymap)
1658 {
1659 cmd_fkmap = !cmd_fkmap;
1660 if (cmd_fkmap) /* in Farsi always in Insert mode */
1661 ccline.overstrike = FALSE;
1662 }
1663 else /* Hebrew is default */
1664#endif
1665 cmd_hkmap = !cmd_hkmap;
1666 goto cmdline_not_changed;
1667#endif
1668
1669 default:
1670#ifdef UNIX
1671 if (c == intr_char)
1672 {
1673 gotesc = TRUE; /* will free ccline.cmdbuff after
1674 putting it in history */
1675 goto returncmd; /* back to Normal mode */
1676 }
1677#endif
1678 /*
1679 * Normal character with no special meaning. Just set mod_mask
1680 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1681 * the string <S-Space>. This should only happen after ^V.
1682 */
1683 if (!IS_SPECIAL(c))
1684 mod_mask = 0x0;
1685 break;
1686 }
1687 /*
1688 * End of switch on command line character.
1689 * We come here if we have a normal character.
1690 */
1691
1692 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1693#ifdef FEAT_MBYTE
1694 /* Add ABBR_OFF for characters above 0x100, this is
1695 * what check_abbr() expects. */
1696 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1697#endif
1698 c))
1699 goto cmdline_changed;
1700
1701 /*
1702 * put the character in the command line
1703 */
1704 if (IS_SPECIAL(c) || mod_mask != 0)
1705 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1706 else
1707 {
1708#ifdef FEAT_MBYTE
1709 if (has_mbyte)
1710 {
1711 j = (*mb_char2bytes)(c, IObuff);
1712 IObuff[j] = NUL; /* exclude composing chars */
1713 put_on_cmdline(IObuff, j, TRUE);
1714 }
1715 else
1716#endif
1717 {
1718 IObuff[0] = c;
1719 put_on_cmdline(IObuff, 1, TRUE);
1720 }
1721 }
1722 goto cmdline_changed;
1723
1724/*
1725 * This part implements incremental searches for "/" and "?"
1726 * Jump to cmdline_not_changed when a character has been read but the command
1727 * line did not change. Then we only search and redraw if something changed in
1728 * the past.
1729 * Jump to cmdline_changed when the command line did change.
1730 * (Sorry for the goto's, I know it is ugly).
1731 */
1732cmdline_not_changed:
1733#ifdef FEAT_SEARCH_EXTRA
1734 if (!incsearch_postponed)
1735 continue;
1736#endif
1737
1738cmdline_changed:
1739#ifdef FEAT_SEARCH_EXTRA
1740 /*
1741 * 'incsearch' highlighting.
1742 */
1743 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1744 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001745 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001746#ifdef FEAT_RELTIME
1747 proftime_T tm;
1748#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001749
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 /* if there is a character waiting, search and redraw later */
1751 if (char_avail())
1752 {
1753 incsearch_postponed = TRUE;
1754 continue;
1755 }
1756 incsearch_postponed = FALSE;
1757 curwin->w_cursor = old_cursor; /* start at old position */
1758
1759 /* If there is no command line, don't do anything */
1760 if (ccline.cmdlen == 0)
1761 i = 0;
1762 else
1763 {
1764 cursor_off(); /* so the user knows we're busy */
1765 out_flush();
1766 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001767#ifdef FEAT_RELTIME
1768 /* Set the time limit to half a second. */
1769 profile_setlimit(500L, &tm);
1770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001772 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1773#ifdef FEAT_RELTIME
1774 &tm
1775#else
1776 NULL
1777#endif
1778 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 --emsg_off;
1780 /* if interrupted while searching, behave like it failed */
1781 if (got_int)
1782 {
1783 (void)vpeekc(); /* remove <C-C> from input stream */
1784 got_int = FALSE; /* don't abandon the command line */
1785 i = 0;
1786 }
1787 else if (char_avail())
1788 /* cancelled searching because a char was typed */
1789 incsearch_postponed = TRUE;
1790 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001791 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 highlight_match = TRUE; /* highlight position */
1793 else
1794 highlight_match = FALSE; /* remove highlight */
1795
1796 /* first restore the old curwin values, so the screen is
1797 * positioned in the same way as the actual search command */
1798 curwin->w_leftcol = old_leftcol;
1799 curwin->w_topline = old_topline;
1800# ifdef FEAT_DIFF
1801 curwin->w_topfill = old_topfill;
1802# endif
1803 curwin->w_botline = old_botline;
1804 changed_cline_bef_curs();
1805 update_topline();
1806
1807 if (i != 0)
1808 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001809 pos_T save_pos = curwin->w_cursor;
1810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001812 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 * moves the whole match onto the screen when 'nowrap' is set.
1814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 curwin->w_cursor.lnum += search_match_lines;
1816 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001817 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1818 {
1819 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1820 coladvance((colnr_T)MAXCOL);
1821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001823 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001824 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001826 else
1827 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001828
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001830# ifdef FEAT_WINDOWS
1831 /* May redraw the status line to show the cursor position. */
1832 if (p_ru && curwin->w_status_height > 0)
1833 curwin->w_redr_status = TRUE;
1834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001836 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001837 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001838 restore_cmdline(&save_ccline);
1839
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001840 /* Leave it at the end to make CTRL-R CTRL-W work. */
1841 if (i != 0)
1842 curwin->w_cursor = end_pos;
1843
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 msg_starthere();
1845 redrawcmdline();
1846 did_incsearch = TRUE;
1847 }
1848#else /* FEAT_SEARCH_EXTRA */
1849 ;
1850#endif
1851
1852#ifdef FEAT_RIGHTLEFT
1853 if (cmdmsg_rl
1854# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001855 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856# endif
1857 )
1858 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001859 * right-left typing. Not efficient, but it works.
1860 * Do it only when there are no characters left to read
1861 * to avoid useless intermediate redraws. */
1862 if (vpeekc() == NUL)
1863 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864#endif
1865 }
1866
1867returncmd:
1868
1869#ifdef FEAT_RIGHTLEFT
1870 cmdmsg_rl = FALSE;
1871#endif
1872
1873#ifdef FEAT_FKMAP
1874 cmd_fkmap = 0;
1875#endif
1876
1877 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001878 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
1880#ifdef FEAT_SEARCH_EXTRA
1881 if (did_incsearch)
1882 {
1883 curwin->w_cursor = old_cursor;
1884 curwin->w_curswant = old_curswant;
1885 curwin->w_leftcol = old_leftcol;
1886 curwin->w_topline = old_topline;
1887# ifdef FEAT_DIFF
1888 curwin->w_topfill = old_topfill;
1889# endif
1890 curwin->w_botline = old_botline;
1891 highlight_match = FALSE;
1892 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001893 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
1895#endif
1896
1897 if (ccline.cmdbuff != NULL)
1898 {
1899 /*
1900 * Put line in history buffer (":" and "=" only when it was typed).
1901 */
1902#ifdef FEAT_CMDHIST
1903 if (ccline.cmdlen && firstc != NUL
1904 && (some_key_typed || histype == HIST_SEARCH))
1905 {
1906 add_to_history(histype, ccline.cmdbuff, TRUE,
1907 histype == HIST_SEARCH ? firstc : NUL);
1908 if (firstc == ':')
1909 {
1910 vim_free(new_last_cmdline);
1911 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1912 }
1913 }
1914#endif
1915
1916 if (gotesc) /* abandon command line */
1917 {
1918 vim_free(ccline.cmdbuff);
1919 ccline.cmdbuff = NULL;
1920 if (msg_scrolled == 0)
1921 compute_cmdrow();
1922 MSG("");
1923 redraw_cmdline = TRUE;
1924 }
1925 }
1926
1927 /*
1928 * If the screen was shifted up, redraw the whole screen (later).
1929 * If the line is too long, clear it, so ruler and shown command do
1930 * not get printed in the middle of it.
1931 */
1932 msg_check();
1933 msg_scroll = save_msg_scroll;
1934 redir_off = FALSE;
1935
1936 /* When the command line was typed, no need for a wait-return prompt. */
1937 if (some_key_typed)
1938 need_wait_return = FALSE;
1939
1940 State = save_State;
1941#ifdef USE_IM_CONTROL
1942 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1943 im_save_status(b_im_ptr);
1944 im_set_active(FALSE);
1945#endif
1946#ifdef FEAT_MOUSE
1947 setmouse();
1948#endif
1949#ifdef CURSOR_SHAPE
1950 ui_cursor_shape(); /* may show different cursor shape */
1951#endif
1952
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001953 {
1954 char_u *p = ccline.cmdbuff;
1955
1956 /* Make ccline empty, getcmdline() may try to use it. */
1957 ccline.cmdbuff = NULL;
1958 return p;
1959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960}
1961
1962#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1963/*
1964 * Get a command line with a prompt.
1965 * This is prepared to be called recursively from getcmdline() (e.g. by
1966 * f_input() when evaluating an expression from CTRL-R =).
1967 * Returns the command line in allocated memory, or NULL.
1968 */
1969 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001970getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 int firstc;
1972 char_u *prompt; /* command line prompt */
1973 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001974 int xp_context; /* type of expansion */
1975 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976{
1977 char_u *s;
1978 struct cmdline_info save_ccline;
1979 int msg_col_save = msg_col;
1980
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001981 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 ccline.cmdprompt = prompt;
1983 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001984# ifdef FEAT_EVAL
1985 ccline.xp_context = xp_context;
1986 ccline.xp_arg = xp_arg;
1987 ccline.input_fn = (firstc == '@');
1988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001990 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02001991 /* Restore msg_col, the prompt from input() may have changed it.
1992 * But only if called recursively and the commandline is therefore being
1993 * restored to an old one; if not, the input() prompt stays on the screen,
1994 * so we need its modified msg_col left intact. */
1995 if (ccline.cmdbuff != NULL)
1996 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
1998 return s;
1999}
2000#endif
2001
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002002/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002003 * Return TRUE when the text must not be changed and we can't switch to
2004 * another window or buffer. Used when editing the command line, evaluating
2005 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002006 */
2007 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002008text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002009{
2010#ifdef FEAT_CMDWIN
2011 if (cmdwin_type != 0)
2012 return TRUE;
2013#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002014 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002015}
2016
2017/*
2018 * Give an error message for a command that isn't allowed while the cmdline
2019 * window is open or editing the cmdline in another way.
2020 */
2021 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002022text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002023{
2024#ifdef FEAT_CMDWIN
2025 if (cmdwin_type != 0)
2026 EMSG(_(e_cmdwin));
2027 else
2028#endif
2029 EMSG(_(e_secure));
2030}
2031
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002032#if defined(FEAT_AUTOCMD) || defined(PROTO)
2033/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002034 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2035 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002036 */
2037 int
2038curbuf_locked()
2039{
2040 if (curbuf_lock > 0)
2041 {
2042 EMSG(_("E788: Not allowed to edit another buffer now"));
2043 return TRUE;
2044 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002045 return allbuf_locked();
2046}
2047
2048/*
2049 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2050 * message.
2051 */
2052 int
2053allbuf_locked()
2054{
2055 if (allbuf_lock > 0)
2056 {
2057 EMSG(_("E811: Not allowed to change buffer information now"));
2058 return TRUE;
2059 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002060 return FALSE;
2061}
2062#endif
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 static int
2065cmdline_charsize(idx)
2066 int idx;
2067{
2068#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2069 if (cmdline_star > 0) /* showing '*', always 1 position */
2070 return 1;
2071#endif
2072 return ptr2cells(ccline.cmdbuff + idx);
2073}
2074
2075/*
2076 * Compute the offset of the cursor on the command line for the prompt and
2077 * indent.
2078 */
2079 static void
2080set_cmdspos()
2081{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002082 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 ccline.cmdspos = 1 + ccline.cmdindent;
2084 else
2085 ccline.cmdspos = 0 + ccline.cmdindent;
2086}
2087
2088/*
2089 * Compute the screen position for the cursor on the command line.
2090 */
2091 static void
2092set_cmdspos_cursor()
2093{
2094 int i, m, c;
2095
2096 set_cmdspos();
2097 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002100 if (m < 0) /* overflow, Columns or Rows at weird value */
2101 m = MAXCOL;
2102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 else
2104 m = MAXCOL;
2105 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2106 {
2107 c = cmdline_charsize(i);
2108#ifdef FEAT_MBYTE
2109 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2110 if (has_mbyte)
2111 correct_cmdspos(i, c);
2112#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002113 /* If the cmdline doesn't fit, show cursor on last visible char.
2114 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 if ((ccline.cmdspos += c) >= m)
2116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 ccline.cmdspos -= c;
2118 break;
2119 }
2120#ifdef FEAT_MBYTE
2121 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002122 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123#endif
2124 }
2125}
2126
2127#ifdef FEAT_MBYTE
2128/*
2129 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2130 * character that doesn't fit, so that a ">" must be displayed.
2131 */
2132 static void
2133correct_cmdspos(idx, cells)
2134 int idx;
2135 int cells;
2136{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002137 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2139 && ccline.cmdspos % Columns + cells > Columns)
2140 ccline.cmdspos++;
2141}
2142#endif
2143
2144/*
2145 * Get an Ex command line for the ":" command.
2146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002148getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002150 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 int indent; /* indent for inside conditionals */
2152{
2153 /* When executing a register, remove ':' that's in front of each line. */
2154 if (exec_from_reg && vpeekc() == ':')
2155 (void)vgetc();
2156 return getcmdline(c, 1L, indent);
2157}
2158
2159/*
2160 * Get an Ex command line for Ex mode.
2161 * In Ex mode we only use the OS supplied line editing features and no
2162 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002163 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002166getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002167 int promptc; /* normally ':', NUL for ":append" and '?' for
2168 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002169 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 int indent; /* indent for inside conditionals */
2171{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002172 garray_T line_ga;
2173 char_u *pend;
2174 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002175 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002176 int escaped = FALSE; /* CTRL-V typed */
2177 int vcol = 0;
2178 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002179 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180
2181 /* Switch cursor on now. This avoids that it happens after the "\n", which
2182 * confuses the system function that computes tabstops. */
2183 cursor_on();
2184
2185 /* always start in column 0; write a newline if necessary */
2186 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002187 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002189 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002191 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002192 if (p_prompt)
2193 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 while (indent-- > 0)
2195 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 }
2198
2199 ga_init2(&line_ga, 1, 30);
2200
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002201 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002202 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002203 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002204 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002205 while (indent >= 8)
2206 {
2207 ga_append(&line_ga, TAB);
2208 msg_puts((char_u *)" ");
2209 indent -= 8;
2210 }
2211 while (indent-- > 0)
2212 {
2213 ga_append(&line_ga, ' ');
2214 msg_putchar(' ');
2215 }
2216 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002217 ++no_mapping;
2218 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 /*
2221 * Get the line, one character at a time.
2222 */
2223 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002224 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 if (ga_grow(&line_ga, 40) == FAIL)
2227 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002229 /* Get one character at a time. Don't use inchar(), it can't handle
2230 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002231 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233
2234 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002235 * Handle line editing.
2236 * Previously this was left to the system, putting the terminal in
2237 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002239 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002241 msg_putchar('\n');
2242 break;
2243 }
2244
2245 if (!escaped)
2246 {
2247 /* CR typed means "enter", which is NL */
2248 if (c1 == '\r')
2249 c1 = '\n';
2250
2251 if (c1 == BS || c1 == K_BS
2252 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002254 if (line_ga.ga_len > 0)
2255 {
2256 --line_ga.ga_len;
2257 goto redraw;
2258 }
2259 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002262 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002264 msg_col = startcol;
2265 msg_clr_eos();
2266 line_ga.ga_len = 0;
2267 continue;
2268 }
2269
2270 if (c1 == Ctrl_T)
2271 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02002272 long sw = get_sw_value();
2273
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002274 p = (char_u *)line_ga.ga_data;
2275 p[line_ga.ga_len] = NUL;
2276 indent = get_indent_str(p, 8);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002277 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002278add_indent:
2279 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002281 char_u *s = skipwhite(p);
2282
2283 ga_grow(&line_ga, 1);
2284 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2285 *s = ' ';
2286 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002288redraw:
2289 /* redraw the line */
2290 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002291 vcol = 0;
2292 for (p = (char_u *)line_ga.ga_data;
2293 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002295 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002297 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002299 msg_putchar(' ');
2300 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002302 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002304 msg_outtrans_len(p, 1);
2305 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 }
2307 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002308 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002309 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 continue;
2311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002313 if (c1 == Ctrl_D)
2314 {
2315 /* Delete one shiftwidth. */
2316 p = (char_u *)line_ga.ga_data;
2317 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002319 if (prev_char == '^')
2320 ex_keep_indent = TRUE;
2321 indent = 0;
2322 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324 else
2325 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002326 p[line_ga.ga_len] = NUL;
2327 indent = get_indent_str(p, 8);
2328 --indent;
Bram Moolenaar14f24742012-08-08 18:01:05 +02002329 indent -= indent % get_sw_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002331 while (get_indent_str(p, 8) > indent)
2332 {
2333 char_u *s = skipwhite(p);
2334
2335 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2336 --line_ga.ga_len;
2337 }
2338 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002340
2341 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2342 {
2343 escaped = TRUE;
2344 continue;
2345 }
2346
2347 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2348 if (IS_SPECIAL(c1))
2349 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002351
2352 if (IS_SPECIAL(c1))
2353 c1 = '?';
2354 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002355 if (c1 == '\n')
2356 msg_putchar('\n');
2357 else if (c1 == TAB)
2358 {
2359 /* Don't use chartabsize(), 'ts' can be different */
2360 do
2361 {
2362 msg_putchar(' ');
2363 } while (++vcol % 8);
2364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002367 msg_outtrans_len(
2368 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2369 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002371 ++line_ga.ga_len;
2372 escaped = FALSE;
2373
2374 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002375 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002376
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002377 /* We are done when a NL is entered, but not when it comes after an
2378 * odd number of backslashes, that results in a NUL. */
2379 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002381 int bcount = 0;
2382
2383 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2384 ++bcount;
2385
2386 if (bcount > 0)
2387 {
2388 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2389 * "\NL", etc. */
2390 line_ga.ga_len -= (bcount + 1) / 2;
2391 pend -= (bcount + 1) / 2;
2392 pend[-1] = '\n';
2393 }
2394
2395 if ((bcount & 1) == 0)
2396 {
2397 --line_ga.ga_len;
2398 --pend;
2399 *pend = NUL;
2400 break;
2401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 }
2403 }
2404
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002405 --no_mapping;
2406 --allow_keys;
2407
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 /* make following messages go to the next line */
2409 msg_didout = FALSE;
2410 msg_col = 0;
2411 if (msg_row < Rows - 1)
2412 ++msg_row;
2413 emsg_on_display = FALSE; /* don't want ui_delay() */
2414
2415 if (got_int)
2416 ga_clear(&line_ga);
2417
2418 return (char_u *)line_ga.ga_data;
2419}
2420
Bram Moolenaare344bea2005-09-01 20:46:49 +00002421# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2422 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423/*
2424 * Return TRUE if ccline.overstrike is on.
2425 */
2426 int
2427cmdline_overstrike()
2428{
2429 return ccline.overstrike;
2430}
2431
2432/*
2433 * Return TRUE if the cursor is at the end of the cmdline.
2434 */
2435 int
2436cmdline_at_end()
2437{
2438 return (ccline.cmdpos >= ccline.cmdlen);
2439}
2440#endif
2441
Bram Moolenaar9372a112005-12-06 19:59:18 +00002442#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443/*
2444 * Return the virtual column number at the current cursor position.
2445 * This is used by the IM code to obtain the start of the preedit string.
2446 */
2447 colnr_T
2448cmdline_getvcol_cursor()
2449{
2450 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2451 return MAXCOL;
2452
2453# ifdef FEAT_MBYTE
2454 if (has_mbyte)
2455 {
2456 colnr_T col;
2457 int i = 0;
2458
2459 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002460 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 return col;
2463 }
2464 else
2465# endif
2466 return ccline.cmdpos;
2467}
2468#endif
2469
2470#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2471/*
2472 * If part of the command line is an IM preedit string, redraw it with
2473 * IM feedback attributes. The cursor position is restored after drawing.
2474 */
2475 static void
2476redrawcmd_preedit()
2477{
2478 if ((State & CMDLINE)
2479 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002480 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 && !p_imdisable
2482 && im_is_preediting())
2483 {
2484 int cmdpos = 0;
2485 int cmdspos;
2486 int old_row;
2487 int old_col;
2488 colnr_T col;
2489
2490 old_row = msg_row;
2491 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002492 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494# ifdef FEAT_MBYTE
2495 if (has_mbyte)
2496 {
2497 for (col = 0; col < preedit_start_col
2498 && cmdpos < ccline.cmdlen; ++col)
2499 {
2500 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002501 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 }
2503 }
2504 else
2505# endif
2506 {
2507 cmdspos += preedit_start_col;
2508 cmdpos += preedit_start_col;
2509 }
2510
2511 msg_row = cmdline_row + (cmdspos / (int)Columns);
2512 msg_col = cmdspos % (int)Columns;
2513 if (msg_row >= Rows)
2514 msg_row = Rows - 1;
2515
2516 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2517 {
2518 int char_len;
2519 int char_attr;
2520
2521 char_attr = im_get_feedback_attr(col);
2522 if (char_attr < 0)
2523 break; /* end of preedit string */
2524
2525# ifdef FEAT_MBYTE
2526 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002527 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 else
2529# endif
2530 char_len = 1;
2531
2532 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2533 cmdpos += char_len;
2534 }
2535
2536 msg_row = old_row;
2537 msg_col = old_col;
2538 }
2539}
2540#endif /* FEAT_XIM && FEAT_GUI_GTK */
2541
2542/*
2543 * Allocate a new command line buffer.
2544 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2545 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2546 */
2547 static void
2548alloc_cmdbuff(len)
2549 int len;
2550{
2551 /*
2552 * give some extra space to avoid having to allocate all the time
2553 */
2554 if (len < 80)
2555 len = 100;
2556 else
2557 len += 20;
2558
2559 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2560 ccline.cmdbufflen = len;
2561}
2562
2563/*
2564 * Re-allocate the command line to length len + something extra.
2565 * return FAIL for failure, OK otherwise
2566 */
2567 static int
2568realloc_cmdbuff(len)
2569 int len;
2570{
2571 char_u *p;
2572
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002573 if (len < ccline.cmdbufflen)
2574 return OK; /* no need to resize */
2575
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 p = ccline.cmdbuff;
2577 alloc_cmdbuff(len); /* will get some more */
2578 if (ccline.cmdbuff == NULL) /* out of memory */
2579 {
2580 ccline.cmdbuff = p; /* keep the old one */
2581 return FAIL;
2582 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002583 /* There isn't always a NUL after the command, but it may need to be
2584 * there, thus copy up to the NUL and add a NUL. */
2585 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2586 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002588
2589 if (ccline.xpc != NULL
2590 && ccline.xpc->xp_pattern != NULL
2591 && ccline.xpc->xp_context != EXPAND_NOTHING
2592 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2593 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002594 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002595
2596 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2597 * to point into the newly allocated memory. */
2598 if (i >= 0 && i <= ccline.cmdlen)
2599 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2600 }
2601
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 return OK;
2603}
2604
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002605#if defined(FEAT_ARABIC) || defined(PROTO)
2606static char_u *arshape_buf = NULL;
2607
2608# if defined(EXITFREE) || defined(PROTO)
2609 void
2610free_cmdline_buf()
2611{
2612 vim_free(arshape_buf);
2613}
2614# endif
2615#endif
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617/*
2618 * Draw part of the cmdline at the current cursor position. But draw stars
2619 * when cmdline_star is TRUE.
2620 */
2621 static void
2622draw_cmdline(start, len)
2623 int start;
2624 int len;
2625{
2626#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2627 int i;
2628
2629 if (cmdline_star > 0)
2630 for (i = 0; i < len; ++i)
2631 {
2632 msg_putchar('*');
2633# ifdef FEAT_MBYTE
2634 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002635 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636# endif
2637 }
2638 else
2639#endif
2640#ifdef FEAT_ARABIC
2641 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 static int buflen = 0;
2644 char_u *p;
2645 int j;
2646 int newlen = 0;
2647 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002648 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 int prev_c = 0;
2650 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002651 int u8c;
2652 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
2655 /*
2656 * Do arabic shaping into a temporary buffer. This is very
2657 * inefficient!
2658 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002659 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
2661 /* Re-allocate the buffer. We keep it around to avoid a lot of
2662 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002663 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002664 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002665 arshape_buf = alloc(buflen);
2666 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 return; /* out of memory */
2668 }
2669
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002670 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2671 {
2672 /* Prepend a space to draw the leading composing char on. */
2673 arshape_buf[0] = ' ';
2674 newlen = 1;
2675 }
2676
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 for (j = start; j < start + len; j += mb_l)
2678 {
2679 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002680 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002681 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if (ARABIC_CHAR(u8c))
2683 {
2684 /* Do Arabic shaping. */
2685 if (cmdmsg_rl)
2686 {
2687 /* displaying from right to left */
2688 pc = prev_c;
2689 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002690 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (j + mb_l >= start + len)
2692 nc = NUL;
2693 else
2694 nc = utf_ptr2char(p + mb_l);
2695 }
2696 else
2697 {
2698 /* displaying from left to right */
2699 if (j + mb_l >= start + len)
2700 pc = NUL;
2701 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002702 {
2703 int pcc[MAX_MCO];
2704
2705 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002707 pc1 = pcc[0];
2708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 nc = prev_c;
2710 }
2711 prev_c = u8c;
2712
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002713 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002715 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002716 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002718 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2719 if (u8cc[1] != 0)
2720 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002721 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
2723 }
2724 else
2725 {
2726 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002727 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 newlen += mb_l;
2729 }
2730 }
2731
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002732 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734 else
2735#endif
2736 msg_outtrans_len(ccline.cmdbuff + start, len);
2737}
2738
2739/*
2740 * Put a character on the command line. Shifts the following text to the
2741 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2742 * "c" must be printable (fit in one display cell)!
2743 */
2744 void
2745putcmdline(c, shift)
2746 int c;
2747 int shift;
2748{
2749 if (cmd_silent)
2750 return;
2751 msg_no_more = TRUE;
2752 msg_putchar(c);
2753 if (shift)
2754 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2755 msg_no_more = FALSE;
2756 cursorcmd();
2757}
2758
2759/*
2760 * Undo a putcmdline(c, FALSE).
2761 */
2762 void
2763unputcmdline()
2764{
2765 if (cmd_silent)
2766 return;
2767 msg_no_more = TRUE;
2768 if (ccline.cmdlen == ccline.cmdpos)
2769 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002770#ifdef FEAT_MBYTE
2771 else if (has_mbyte)
2772 draw_cmdline(ccline.cmdpos,
2773 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 else
2776 draw_cmdline(ccline.cmdpos, 1);
2777 msg_no_more = FALSE;
2778 cursorcmd();
2779}
2780
2781/*
2782 * Put the given string, of the given length, onto the command line.
2783 * If len is -1, then STRLEN() is used to calculate the length.
2784 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2785 * part will be redrawn, otherwise it will not. If this function is called
2786 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2787 * called afterwards.
2788 */
2789 int
2790put_on_cmdline(str, len, redraw)
2791 char_u *str;
2792 int len;
2793 int redraw;
2794{
2795 int retval;
2796 int i;
2797 int m;
2798 int c;
2799
2800 if (len < 0)
2801 len = (int)STRLEN(str);
2802
2803 /* Check if ccline.cmdbuff needs to be longer */
2804 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002805 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 else
2807 retval = OK;
2808 if (retval == OK)
2809 {
2810 if (!ccline.overstrike)
2811 {
2812 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2813 ccline.cmdbuff + ccline.cmdpos,
2814 (size_t)(ccline.cmdlen - ccline.cmdpos));
2815 ccline.cmdlen += len;
2816 }
2817 else
2818 {
2819#ifdef FEAT_MBYTE
2820 if (has_mbyte)
2821 {
2822 /* Count nr of characters in the new string. */
2823 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002824 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 ++m;
2826 /* Count nr of bytes in cmdline that are overwritten by these
2827 * characters. */
2828 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002829 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 --m;
2831 if (i < ccline.cmdlen)
2832 {
2833 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2834 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2835 ccline.cmdlen += ccline.cmdpos + len - i;
2836 }
2837 else
2838 ccline.cmdlen = ccline.cmdpos + len;
2839 }
2840 else
2841#endif
2842 if (ccline.cmdpos + len > ccline.cmdlen)
2843 ccline.cmdlen = ccline.cmdpos + len;
2844 }
2845 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2846 ccline.cmdbuff[ccline.cmdlen] = NUL;
2847
2848#ifdef FEAT_MBYTE
2849 if (enc_utf8)
2850 {
2851 /* When the inserted text starts with a composing character,
2852 * backup to the character before it. There could be two of them.
2853 */
2854 i = 0;
2855 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2856 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2857 {
2858 i = (*mb_head_off)(ccline.cmdbuff,
2859 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2860 ccline.cmdpos -= i;
2861 len += i;
2862 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2863 }
2864# ifdef FEAT_ARABIC
2865 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2866 {
2867 /* Check the previous character for Arabic combining pair. */
2868 i = (*mb_head_off)(ccline.cmdbuff,
2869 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2870 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2871 + ccline.cmdpos - i), c))
2872 {
2873 ccline.cmdpos -= i;
2874 len += i;
2875 }
2876 else
2877 i = 0;
2878 }
2879# endif
2880 if (i != 0)
2881 {
2882 /* Also backup the cursor position. */
2883 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2884 ccline.cmdspos -= i;
2885 msg_col -= i;
2886 if (msg_col < 0)
2887 {
2888 msg_col += Columns;
2889 --msg_row;
2890 }
2891 }
2892 }
2893#endif
2894
2895 if (redraw && !cmd_silent)
2896 {
2897 msg_no_more = TRUE;
2898 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002899 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2901 /* Avoid clearing the rest of the line too often. */
2902 if (cmdline_row != i || ccline.overstrike)
2903 msg_clr_eos();
2904 msg_no_more = FALSE;
2905 }
2906#ifdef FEAT_FKMAP
2907 /*
2908 * If we are in Farsi command mode, the character input must be in
2909 * Insert mode. So do not advance the cmdpos.
2910 */
2911 if (!cmd_fkmap)
2912#endif
2913 {
2914 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002917 if (m < 0) /* overflow, Columns or Rows at weird value */
2918 m = MAXCOL;
2919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 else
2921 m = MAXCOL;
2922 for (i = 0; i < len; ++i)
2923 {
2924 c = cmdline_charsize(ccline.cmdpos);
2925#ifdef FEAT_MBYTE
2926 /* count ">" for a double-wide char that doesn't fit. */
2927 if (has_mbyte)
2928 correct_cmdspos(ccline.cmdpos, c);
2929#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002930 /* Stop cursor at the end of the screen, but do increment the
2931 * insert position, so that entering a very long command
2932 * works, even though you can't see it. */
2933 if (ccline.cmdspos + c < m)
2934 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935#ifdef FEAT_MBYTE
2936 if (has_mbyte)
2937 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002938 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 if (c > len - i - 1)
2940 c = len - i - 1;
2941 ccline.cmdpos += c;
2942 i += c;
2943 }
2944#endif
2945 ++ccline.cmdpos;
2946 }
2947 }
2948 }
2949 if (redraw)
2950 msg_check();
2951 return retval;
2952}
2953
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002954static struct cmdline_info prev_ccline;
2955static int prev_ccline_used = FALSE;
2956
2957/*
2958 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2959 * and overwrite it. But get_cmdline_str() may need it, thus make it
2960 * available globally in prev_ccline.
2961 */
2962 static void
2963save_cmdline(ccp)
2964 struct cmdline_info *ccp;
2965{
2966 if (!prev_ccline_used)
2967 {
2968 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2969 prev_ccline_used = TRUE;
2970 }
2971 *ccp = prev_ccline;
2972 prev_ccline = ccline;
2973 ccline.cmdbuff = NULL;
2974 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002975 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002976}
2977
2978/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002979 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002980 */
2981 static void
2982restore_cmdline(ccp)
2983 struct cmdline_info *ccp;
2984{
2985 ccline = prev_ccline;
2986 prev_ccline = *ccp;
2987}
2988
Bram Moolenaar5a305422006-04-28 22:38:25 +00002989#if defined(FEAT_EVAL) || defined(PROTO)
2990/*
2991 * Save the command line into allocated memory. Returns a pointer to be
2992 * passed to restore_cmdline_alloc() later.
2993 * Returns NULL when failed.
2994 */
2995 char_u *
2996save_cmdline_alloc()
2997{
2998 struct cmdline_info *p;
2999
3000 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3001 if (p != NULL)
3002 save_cmdline(p);
3003 return (char_u *)p;
3004}
3005
3006/*
3007 * Restore the command line from the return value of save_cmdline_alloc().
3008 */
3009 void
3010restore_cmdline_alloc(p)
3011 char_u *p;
3012{
3013 if (p != NULL)
3014 {
3015 restore_cmdline((struct cmdline_info *)p);
3016 vim_free(p);
3017 }
3018}
3019#endif
3020
Bram Moolenaar8299df92004-07-10 09:47:34 +00003021/*
3022 * paste a yank register into the command line.
3023 * used by CTRL-R command in command-line mode
3024 * insert_reg() can't be used here, because special characters from the
3025 * register contents will be interpreted as commands.
3026 *
3027 * return FAIL for failure, OK otherwise
3028 */
3029 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003030cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003031 int regname;
3032 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003033 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003034{
3035 long i;
3036 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003037 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003038 int allocated;
3039 struct cmdline_info save_ccline;
3040
3041 /* check for valid regname; also accept special characters for CTRL-R in
3042 * the command line */
3043 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3044 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3045 return FAIL;
3046
3047 /* A register containing CTRL-R can cause an endless loop. Allow using
3048 * CTRL-C to break the loop. */
3049 line_breakcheck();
3050 if (got_int)
3051 return FAIL;
3052
3053#ifdef FEAT_CLIPBOARD
3054 regname = may_get_selection(regname);
3055#endif
3056
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003057 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003058 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003059 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003060 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003061 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003062 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003063 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003064
3065 if (i)
3066 {
3067 /* Got the value of a special register in "arg". */
3068 if (arg == NULL)
3069 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003070
3071 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3072 * part of the word. */
3073 p = arg;
3074 if (p_is && regname == Ctrl_W)
3075 {
3076 char_u *w;
3077 int len;
3078
3079 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003080 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003081 {
3082#ifdef FEAT_MBYTE
3083 if (has_mbyte)
3084 {
3085 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3086 if (!vim_iswordc(mb_ptr2char(w - len)))
3087 break;
3088 w -= len;
3089 }
3090 else
3091#endif
3092 {
3093 if (!vim_iswordc(w[-1]))
3094 break;
3095 --w;
3096 }
3097 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003098 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003099 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3100 p += len;
3101 }
3102
3103 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003104 if (allocated)
3105 vim_free(arg);
3106 return OK;
3107 }
3108
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003109 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003110}
3111
3112/*
3113 * Put a string on the command line.
3114 * When "literally" is TRUE, insert literally.
3115 * When "literally" is FALSE, insert as typed, but don't leave the command
3116 * line.
3117 */
3118 void
3119cmdline_paste_str(s, literally)
3120 char_u *s;
3121 int literally;
3122{
3123 int c, cv;
3124
3125 if (literally)
3126 put_on_cmdline(s, -1, TRUE);
3127 else
3128 while (*s != NUL)
3129 {
3130 cv = *s;
3131 if (cv == Ctrl_V && s[1])
3132 ++s;
3133#ifdef FEAT_MBYTE
3134 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003135 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003136 else
3137#endif
3138 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003139 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3140 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003141#ifdef UNIX
3142 || c == intr_char
3143#endif
3144 || (c == Ctrl_BSL && *s == Ctrl_N))
3145 stuffcharReadbuff(Ctrl_V);
3146 stuffcharReadbuff(c);
3147 }
3148}
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150#ifdef FEAT_WILDMENU
3151/*
3152 * Delete characters on the command line, from "from" to the current
3153 * position.
3154 */
3155 static void
3156cmdline_del(from)
3157 int from;
3158{
3159 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3160 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3161 ccline.cmdlen -= ccline.cmdpos - from;
3162 ccline.cmdpos = from;
3163}
3164#endif
3165
3166/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003167 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 * search
3169 */
3170 void
3171redrawcmdline()
3172{
3173 if (cmd_silent)
3174 return;
3175 need_wait_return = FALSE;
3176 compute_cmdrow();
3177 redrawcmd();
3178 cursorcmd();
3179}
3180
3181 static void
3182redrawcmdprompt()
3183{
3184 int i;
3185
3186 if (cmd_silent)
3187 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003188 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 msg_putchar(ccline.cmdfirstc);
3190 if (ccline.cmdprompt != NULL)
3191 {
3192 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3193 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3194 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003195 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 --ccline.cmdindent;
3197 }
3198 else
3199 for (i = ccline.cmdindent; i > 0; --i)
3200 msg_putchar(' ');
3201}
3202
3203/*
3204 * Redraw what is currently on the command line.
3205 */
3206 void
3207redrawcmd()
3208{
3209 if (cmd_silent)
3210 return;
3211
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003212 /* when 'incsearch' is set there may be no command line while redrawing */
3213 if (ccline.cmdbuff == NULL)
3214 {
3215 windgoto(cmdline_row, 0);
3216 msg_clr_eos();
3217 return;
3218 }
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 msg_start();
3221 redrawcmdprompt();
3222
3223 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3224 msg_no_more = TRUE;
3225 draw_cmdline(0, ccline.cmdlen);
3226 msg_clr_eos();
3227 msg_no_more = FALSE;
3228
3229 set_cmdspos_cursor();
3230
3231 /*
3232 * An emsg() before may have set msg_scroll. This is used in normal mode,
3233 * in cmdline mode we can reset them now.
3234 */
3235 msg_scroll = FALSE; /* next message overwrites cmdline */
3236
3237 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3238 * in cmdline mode */
3239 skip_redraw = FALSE;
3240}
3241
3242 void
3243compute_cmdrow()
3244{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003245 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 cmdline_row = Rows - 1;
3247 else
3248 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3249 + W_STATUS_HEIGHT(lastwin);
3250}
3251
3252 static void
3253cursorcmd()
3254{
3255 if (cmd_silent)
3256 return;
3257
3258#ifdef FEAT_RIGHTLEFT
3259 if (cmdmsg_rl)
3260 {
3261 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3262 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3263 if (msg_row <= 0)
3264 msg_row = Rows - 1;
3265 }
3266 else
3267#endif
3268 {
3269 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3270 msg_col = ccline.cmdspos % (int)Columns;
3271 if (msg_row >= Rows)
3272 msg_row = Rows - 1;
3273 }
3274
3275 windgoto(msg_row, msg_col);
3276#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3277 redrawcmd_preedit();
3278#endif
3279#ifdef MCH_CURSOR_SHAPE
3280 mch_update_cursor();
3281#endif
3282}
3283
3284 void
3285gotocmdline(clr)
3286 int clr;
3287{
3288 msg_start();
3289#ifdef FEAT_RIGHTLEFT
3290 if (cmdmsg_rl)
3291 msg_col = Columns - 1;
3292 else
3293#endif
3294 msg_col = 0; /* always start in column 0 */
3295 if (clr) /* clear the bottom line(s) */
3296 msg_clr_eos(); /* will reset clear_cmdline */
3297 windgoto(cmdline_row, 0);
3298}
3299
3300/*
3301 * Check the word in front of the cursor for an abbreviation.
3302 * Called when the non-id character "c" has been entered.
3303 * When an abbreviation is recognized it is removed from the text with
3304 * backspaces and the replacement string is inserted, followed by "c".
3305 */
3306 static int
3307ccheck_abbr(c)
3308 int c;
3309{
3310 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3311 return FALSE;
3312
3313 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3314}
3315
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003316#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3317 static int
3318#ifdef __BORLANDC__
3319_RTLENTRYF
3320#endif
3321sort_func_compare(s1, s2)
3322 const void *s1;
3323 const void *s2;
3324{
3325 char_u *p1 = *(char_u **)s1;
3326 char_u *p2 = *(char_u **)s2;
3327
3328 if (*p1 != '<' && *p2 == '<') return -1;
3329 if (*p1 == '<' && *p2 != '<') return 1;
3330 return STRCMP(p1, p2);
3331}
3332#endif
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334/*
3335 * Return FAIL if this is not an appropriate context in which to do
3336 * completion of anything, return OK if it is (even if there are no matches).
3337 * For the caller, this means that the character is just passed through like a
3338 * normal character (instead of being expanded). This allows :s/^I^D etc.
3339 */
3340 static int
3341nextwild(xp, type, options)
3342 expand_T *xp;
3343 int type;
3344 int options; /* extra options for ExpandOne() */
3345{
3346 int i, j;
3347 char_u *p1;
3348 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 int difflen;
3350 int v;
3351
3352 if (xp->xp_numfiles == -1)
3353 {
3354 set_expand_context(xp);
3355 cmd_showtail = expand_showtail(xp);
3356 }
3357
3358 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3359 {
3360 beep_flush();
3361 return OK; /* Something illegal on command line */
3362 }
3363 if (xp->xp_context == EXPAND_NOTHING)
3364 {
3365 /* Caller can use the character as a normal char instead */
3366 return FAIL;
3367 }
3368
3369 MSG_PUTS("..."); /* show that we are busy */
3370 out_flush();
3371
3372 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003373 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
3375 if (type == WILD_NEXT || type == WILD_PREV)
3376 {
3377 /*
3378 * Get next/previous match for a previous expanded pattern.
3379 */
3380 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3381 }
3382 else
3383 {
3384 /*
3385 * Translate string into pattern and expand it.
3386 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003387 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3388 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 p2 = NULL;
3390 else
3391 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003392 int use_options = options |
3393 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE;
3394
3395 if (p_wic)
3396 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003397 p2 = ExpandOne(xp, p1,
3398 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003399 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003401 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (p2 != NULL && type == WILD_LONGEST)
3403 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003404 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (ccline.cmdbuff[i + j] == '*'
3406 || ccline.cmdbuff[i + j] == '?')
3407 break;
3408 if ((int)STRLEN(p2) < j)
3409 {
3410 vim_free(p2);
3411 p2 = NULL;
3412 }
3413 }
3414 }
3415 }
3416
3417 if (p2 != NULL && !got_int)
3418 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003419 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003420 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003422 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 xp->xp_pattern = ccline.cmdbuff + i;
3424 }
3425 else
3426 v = OK;
3427 if (v == OK)
3428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003429 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3430 &ccline.cmdbuff[ccline.cmdpos],
3431 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3432 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 ccline.cmdlen += difflen;
3434 ccline.cmdpos += difflen;
3435 }
3436 }
3437 vim_free(p2);
3438
3439 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003440 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
3442 /* When expanding a ":map" command and no matches are found, assume that
3443 * the key is supposed to be inserted literally */
3444 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3445 return FAIL;
3446
3447 if (xp->xp_numfiles <= 0 && p2 == NULL)
3448 beep_flush();
3449 else if (xp->xp_numfiles == 1)
3450 /* free expanded pattern */
3451 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3452
3453 return OK;
3454}
3455
3456/*
3457 * Do wildcard expansion on the string 'str'.
3458 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003459 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 * Return NULL for failure.
3461 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003462 * "orig" is the originally expanded string, copied to allocated memory. It
3463 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3464 * WILD_PREV "orig" should be NULL.
3465 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003466 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3467 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 *
3469 * mode = WILD_FREE: just free previously expanded matches
3470 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3471 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3472 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3473 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3474 * mode = WILD_ALL: return all matches concatenated
3475 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003476 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 *
3478 * options = WILD_LIST_NOTFOUND: list entries without a match
3479 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3480 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3481 * options = WILD_NO_BEEP: Don't beep for multiple matches
3482 * options = WILD_ADD_SLASH: add a slash after directory names
3483 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3484 * options = WILD_SILENT: don't print warning messages
3485 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003486 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 *
3488 * The variables xp->xp_context and xp->xp_backslash must have been set!
3489 */
3490 char_u *
3491ExpandOne(xp, str, orig, options, mode)
3492 expand_T *xp;
3493 char_u *str;
3494 char_u *orig; /* allocated copy of original of expanded string */
3495 int options;
3496 int mode;
3497{
3498 char_u *ss = NULL;
3499 static int findex;
3500 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003501 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 int i;
3503 long_u len;
3504 int non_suf_match; /* number without matching suffix */
3505
3506 /*
3507 * first handle the case of using an old match
3508 */
3509 if (mode == WILD_NEXT || mode == WILD_PREV)
3510 {
3511 if (xp->xp_numfiles > 0)
3512 {
3513 if (mode == WILD_PREV)
3514 {
3515 if (findex == -1)
3516 findex = xp->xp_numfiles;
3517 --findex;
3518 }
3519 else /* mode == WILD_NEXT */
3520 ++findex;
3521
3522 /*
3523 * When wrapping around, return the original string, set findex to
3524 * -1.
3525 */
3526 if (findex < 0)
3527 {
3528 if (orig_save == NULL)
3529 findex = xp->xp_numfiles - 1;
3530 else
3531 findex = -1;
3532 }
3533 if (findex >= xp->xp_numfiles)
3534 {
3535 if (orig_save == NULL)
3536 findex = 0;
3537 else
3538 findex = -1;
3539 }
3540#ifdef FEAT_WILDMENU
3541 if (p_wmnu)
3542 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3543 findex, cmd_showtail);
3544#endif
3545 if (findex == -1)
3546 return vim_strsave(orig_save);
3547 return vim_strsave(xp->xp_files[findex]);
3548 }
3549 else
3550 return NULL;
3551 }
3552
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003553 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3555 {
3556 FreeWild(xp->xp_numfiles, xp->xp_files);
3557 xp->xp_numfiles = -1;
3558 vim_free(orig_save);
3559 orig_save = NULL;
3560 }
3561 findex = 0;
3562
3563 if (mode == WILD_FREE) /* only release file name */
3564 return NULL;
3565
3566 if (xp->xp_numfiles == -1)
3567 {
3568 vim_free(orig_save);
3569 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003570 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
3572 /*
3573 * Do the expansion.
3574 */
3575 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3576 options) == FAIL)
3577 {
3578#ifdef FNAME_ILLEGAL
3579 /* Illegal file name has been silently skipped. But when there
3580 * are wildcards, the real problem is that there was no match,
3581 * causing the pattern to be added, which has illegal characters.
3582 */
3583 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3584 EMSG2(_(e_nomatch2), str);
3585#endif
3586 }
3587 else if (xp->xp_numfiles == 0)
3588 {
3589 if (!(options & WILD_SILENT))
3590 EMSG2(_(e_nomatch2), str);
3591 }
3592 else
3593 {
3594 /* Escape the matches for use on the command line. */
3595 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3596
3597 /*
3598 * Check for matching suffixes in file names.
3599 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003600 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3601 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 {
3603 if (xp->xp_numfiles)
3604 non_suf_match = xp->xp_numfiles;
3605 else
3606 non_suf_match = 1;
3607 if ((xp->xp_context == EXPAND_FILES
3608 || xp->xp_context == EXPAND_DIRECTORIES)
3609 && xp->xp_numfiles > 1)
3610 {
3611 /*
3612 * More than one match; check suffix.
3613 * The files will have been sorted on matching suffix in
3614 * expand_wildcards, only need to check the first two.
3615 */
3616 non_suf_match = 0;
3617 for (i = 0; i < 2; ++i)
3618 if (match_suffix(xp->xp_files[i]))
3619 ++non_suf_match;
3620 }
3621 if (non_suf_match != 1)
3622 {
3623 /* Can we ever get here unless it's while expanding
3624 * interactively? If not, we can get rid of this all
3625 * together. Don't really want to wait for this message
3626 * (and possibly have to hit return to continue!).
3627 */
3628 if (!(options & WILD_SILENT))
3629 EMSG(_(e_toomany));
3630 else if (!(options & WILD_NO_BEEP))
3631 beep_flush();
3632 }
3633 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3634 ss = vim_strsave(xp->xp_files[0]);
3635 }
3636 }
3637 }
3638
3639 /* Find longest common part */
3640 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3641 {
3642 for (len = 0; xp->xp_files[0][len]; ++len)
3643 {
3644 for (i = 0; i < xp->xp_numfiles; ++i)
3645 {
3646#ifdef CASE_INSENSITIVE_FILENAME
3647 if (xp->xp_context == EXPAND_DIRECTORIES
3648 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003649 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 || xp->xp_context == EXPAND_BUFFERS)
3651 {
3652 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3653 TOLOWER_LOC(xp->xp_files[0][len]))
3654 break;
3655 }
3656 else
3657#endif
3658 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3659 break;
3660 }
3661 if (i < xp->xp_numfiles)
3662 {
3663 if (!(options & WILD_NO_BEEP))
3664 vim_beep();
3665 break;
3666 }
3667 }
3668 ss = alloc((unsigned)len + 1);
3669 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003670 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 findex = -1; /* next p_wc gets first one */
3672 }
3673
3674 /* Concatenate all matching names */
3675 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3676 {
3677 len = 0;
3678 for (i = 0; i < xp->xp_numfiles; ++i)
3679 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3680 ss = lalloc(len, TRUE);
3681 if (ss != NULL)
3682 {
3683 *ss = NUL;
3684 for (i = 0; i < xp->xp_numfiles; ++i)
3685 {
3686 STRCAT(ss, xp->xp_files[i]);
3687 if (i != xp->xp_numfiles - 1)
3688 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3689 }
3690 }
3691 }
3692
3693 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3694 ExpandCleanup(xp);
3695
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003696 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003697 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003698 vim_free(orig);
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 return ss;
3701}
3702
3703/*
3704 * Prepare an expand structure for use.
3705 */
3706 void
3707ExpandInit(xp)
3708 expand_T *xp;
3709{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003710 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003711 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003713#ifndef BACKSLASH_IN_FILENAME
3714 xp->xp_shell = FALSE;
3715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 xp->xp_numfiles = -1;
3717 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003718#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3719 xp->xp_arg = NULL;
3720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721}
3722
3723/*
3724 * Cleanup an expand structure after use.
3725 */
3726 void
3727ExpandCleanup(xp)
3728 expand_T *xp;
3729{
3730 if (xp->xp_numfiles >= 0)
3731 {
3732 FreeWild(xp->xp_numfiles, xp->xp_files);
3733 xp->xp_numfiles = -1;
3734 }
3735}
3736
3737 void
3738ExpandEscape(xp, str, numfiles, files, options)
3739 expand_T *xp;
3740 char_u *str;
3741 int numfiles;
3742 char_u **files;
3743 int options;
3744{
3745 int i;
3746 char_u *p;
3747
3748 /*
3749 * May change home directory back to "~"
3750 */
3751 if (options & WILD_HOME_REPLACE)
3752 tilde_replace(str, numfiles, files);
3753
3754 if (options & WILD_ESCAPE)
3755 {
3756 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003757 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003758 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 || xp->xp_context == EXPAND_BUFFERS
3760 || xp->xp_context == EXPAND_DIRECTORIES)
3761 {
3762 /*
3763 * Insert a backslash into a file name before a space, \, %, #
3764 * and wildmatch characters, except '~'.
3765 */
3766 for (i = 0; i < numfiles; ++i)
3767 {
3768 /* for ":set path=" we need to escape spaces twice */
3769 if (xp->xp_backslash == XP_BS_THREE)
3770 {
3771 p = vim_strsave_escaped(files[i], (char_u *)" ");
3772 if (p != NULL)
3773 {
3774 vim_free(files[i]);
3775 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003776#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 p = vim_strsave_escaped(files[i], (char_u *)" ");
3778 if (p != NULL)
3779 {
3780 vim_free(files[i]);
3781 files[i] = p;
3782 }
3783#endif
3784 }
3785 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003786#ifdef BACKSLASH_IN_FILENAME
3787 p = vim_strsave_fnameescape(files[i], FALSE);
3788#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003789 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 if (p != NULL)
3792 {
3793 vim_free(files[i]);
3794 files[i] = p;
3795 }
3796
3797 /* If 'str' starts with "\~", replace "~" at start of
3798 * files[i] with "\~". */
3799 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003800 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 }
3802 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003803
3804 /* If the first file starts with a '+' escape it. Otherwise it
3805 * could be seen as "+cmd". */
3806 if (*files[0] == '+')
3807 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 else if (xp->xp_context == EXPAND_TAGS)
3810 {
3811 /*
3812 * Insert a backslash before characters in a tag name that
3813 * would terminate the ":tag" command.
3814 */
3815 for (i = 0; i < numfiles; ++i)
3816 {
3817 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3818 if (p != NULL)
3819 {
3820 vim_free(files[i]);
3821 files[i] = p;
3822 }
3823 }
3824 }
3825 }
3826}
3827
3828/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003829 * Escape special characters in "fname" for when used as a file name argument
3830 * after a Vim command, or, when "shell" is non-zero, a shell command.
3831 * Returns the result in allocated memory.
3832 */
3833 char_u *
3834vim_strsave_fnameescape(fname, shell)
3835 char_u *fname;
3836 int shell;
3837{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003838 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003839#ifdef BACKSLASH_IN_FILENAME
3840 char_u buf[20];
3841 int j = 0;
3842
3843 /* Don't escape '[' and '{' if they are in 'isfname'. */
3844 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3845 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3846 buf[j++] = *p;
3847 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003848 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003849#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003850 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3851 if (shell && csh_like_shell() && p != NULL)
3852 {
3853 char_u *s;
3854
3855 /* For csh and similar shells need to put two backslashes before '!'.
3856 * One is taken by Vim, one by the shell. */
3857 s = vim_strsave_escaped(p, (char_u *)"!");
3858 vim_free(p);
3859 p = s;
3860 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003861#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003862
3863 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3864 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003865 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003866 escape_fname(&p);
3867
3868 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003869}
3870
3871/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003872 * Put a backslash before the file name in "pp", which is in allocated memory.
3873 */
3874 static void
3875escape_fname(pp)
3876 char_u **pp;
3877{
3878 char_u *p;
3879
3880 p = alloc((unsigned)(STRLEN(*pp) + 2));
3881 if (p != NULL)
3882 {
3883 p[0] = '\\';
3884 STRCPY(p + 1, *pp);
3885 vim_free(*pp);
3886 *pp = p;
3887 }
3888}
3889
3890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 * For each file name in files[num_files]:
3892 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3893 */
3894 void
3895tilde_replace(orig_pat, num_files, files)
3896 char_u *orig_pat;
3897 int num_files;
3898 char_u **files;
3899{
3900 int i;
3901 char_u *p;
3902
3903 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3904 {
3905 for (i = 0; i < num_files; ++i)
3906 {
3907 p = home_replace_save(NULL, files[i]);
3908 if (p != NULL)
3909 {
3910 vim_free(files[i]);
3911 files[i] = p;
3912 }
3913 }
3914 }
3915}
3916
3917/*
3918 * Show all matches for completion on the command line.
3919 * Returns EXPAND_NOTHING when the character that triggered expansion should
3920 * be inserted like a normal character.
3921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 static int
3923showmatches(xp, wildmenu)
3924 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003925 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926{
3927#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3928 int num_files;
3929 char_u **files_found;
3930 int i, j, k;
3931 int maxlen;
3932 int lines;
3933 int columns;
3934 char_u *p;
3935 int lastlen;
3936 int attr;
3937 int showtail;
3938
3939 if (xp->xp_numfiles == -1)
3940 {
3941 set_expand_context(xp);
3942 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3943 &num_files, &files_found);
3944 showtail = expand_showtail(xp);
3945 if (i != EXPAND_OK)
3946 return i;
3947
3948 }
3949 else
3950 {
3951 num_files = xp->xp_numfiles;
3952 files_found = xp->xp_files;
3953 showtail = cmd_showtail;
3954 }
3955
3956#ifdef FEAT_WILDMENU
3957 if (!wildmenu)
3958 {
3959#endif
3960 msg_didany = FALSE; /* lines_left will be set */
3961 msg_start(); /* prepare for paging */
3962 msg_putchar('\n');
3963 out_flush();
3964 cmdline_row = msg_row;
3965 msg_didany = FALSE; /* lines_left will be set again */
3966 msg_start(); /* prepare for paging */
3967#ifdef FEAT_WILDMENU
3968 }
3969#endif
3970
3971 if (got_int)
3972 got_int = FALSE; /* only int. the completion, not the cmd line */
3973#ifdef FEAT_WILDMENU
3974 else if (wildmenu)
3975 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3976#endif
3977 else
3978 {
3979 /* find the length of the longest file name */
3980 maxlen = 0;
3981 for (i = 0; i < num_files; ++i)
3982 {
3983 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003984 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 || xp->xp_context == EXPAND_BUFFERS))
3986 {
3987 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3988 j = vim_strsize(NameBuff);
3989 }
3990 else
3991 j = vim_strsize(L_SHOWFILE(i));
3992 if (j > maxlen)
3993 maxlen = j;
3994 }
3995
3996 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3997 lines = num_files;
3998 else
3999 {
4000 /* compute the number of columns and lines for the listing */
4001 maxlen += 2; /* two spaces between file names */
4002 columns = ((int)Columns + 2) / maxlen;
4003 if (columns < 1)
4004 columns = 1;
4005 lines = (num_files + columns - 1) / columns;
4006 }
4007
4008 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4009
4010 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4011 {
4012 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4013 msg_clr_eos();
4014 msg_advance(maxlen - 3);
4015 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4016 }
4017
4018 /* list the files line by line */
4019 for (i = 0; i < lines; ++i)
4020 {
4021 lastlen = 999;
4022 for (k = i; k < num_files; k += lines)
4023 {
4024 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4025 {
4026 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4027 p = files_found[k] + STRLEN(files_found[k]) + 1;
4028 msg_advance(maxlen + 1);
4029 msg_puts(p);
4030 msg_advance(maxlen + 3);
4031 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4032 break;
4033 }
4034 for (j = maxlen - lastlen; --j >= 0; )
4035 msg_putchar(' ');
4036 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004037 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 || xp->xp_context == EXPAND_BUFFERS)
4039 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004040 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004041 if (xp->xp_numfiles != -1)
4042 {
4043 char_u *halved_slash;
4044 char_u *exp_path;
4045
4046 /* Expansion was done before and special characters
4047 * were escaped, need to halve backslashes. Also
4048 * $HOME has been replaced with ~/. */
4049 exp_path = expand_env_save_opt(files_found[k], TRUE);
4050 halved_slash = backslash_halve_save(
4051 exp_path != NULL ? exp_path : files_found[k]);
4052 j = mch_isdir(halved_slash != NULL ? halved_slash
4053 : files_found[k]);
4054 vim_free(exp_path);
4055 vim_free(halved_slash);
4056 }
4057 else
4058 /* Expansion was done here, file names are literal. */
4059 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (showtail)
4061 p = L_SHOWFILE(k);
4062 else
4063 {
4064 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4065 TRUE);
4066 p = NameBuff;
4067 }
4068 }
4069 else
4070 {
4071 j = FALSE;
4072 p = L_SHOWFILE(k);
4073 }
4074 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4075 }
4076 if (msg_col > 0) /* when not wrapped around */
4077 {
4078 msg_clr_eos();
4079 msg_putchar('\n');
4080 }
4081 out_flush(); /* show one line at a time */
4082 if (got_int)
4083 {
4084 got_int = FALSE;
4085 break;
4086 }
4087 }
4088
4089 /*
4090 * we redraw the command below the lines that we have just listed
4091 * This is a bit tricky, but it saves a lot of screen updating.
4092 */
4093 cmdline_row = msg_row; /* will put it back later */
4094 }
4095
4096 if (xp->xp_numfiles == -1)
4097 FreeWild(num_files, files_found);
4098
4099 return EXPAND_OK;
4100}
4101
4102/*
4103 * Private gettail for showmatches() (and win_redr_status_matches()):
4104 * Find tail of file name path, but ignore trailing "/".
4105 */
4106 char_u *
4107sm_gettail(s)
4108 char_u *s;
4109{
4110 char_u *p;
4111 char_u *t = s;
4112 int had_sep = FALSE;
4113
4114 for (p = s; *p != NUL; )
4115 {
4116 if (vim_ispathsep(*p)
4117#ifdef BACKSLASH_IN_FILENAME
4118 && !rem_backslash(p)
4119#endif
4120 )
4121 had_sep = TRUE;
4122 else if (had_sep)
4123 {
4124 t = p;
4125 had_sep = FALSE;
4126 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004127 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 return t;
4130}
4131
4132/*
4133 * Return TRUE if we only need to show the tail of completion matches.
4134 * When not completing file names or there is a wildcard in the path FALSE is
4135 * returned.
4136 */
4137 static int
4138expand_showtail(xp)
4139 expand_T *xp;
4140{
4141 char_u *s;
4142 char_u *end;
4143
4144 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004145 if (xp->xp_context != EXPAND_FILES
4146 && xp->xp_context != EXPAND_SHELLCMD
4147 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 return FALSE;
4149
4150 end = gettail(xp->xp_pattern);
4151 if (end == xp->xp_pattern) /* there is no path separator */
4152 return FALSE;
4153
4154 for (s = xp->xp_pattern; s < end; s++)
4155 {
4156 /* Skip escaped wildcards. Only when the backslash is not a path
4157 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4158 if (rem_backslash(s))
4159 ++s;
4160 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4161 return FALSE;
4162 }
4163 return TRUE;
4164}
4165
4166/*
4167 * Prepare a string for expansion.
4168 * When expanding file names: The string will be used with expand_wildcards().
4169 * Copy the file name into allocated memory and add a '*' at the end.
4170 * When expanding other names: The string will be used with regcomp(). Copy
4171 * the name into allocated memory and prepend "^".
4172 */
4173 char_u *
4174addstar(fname, len, context)
4175 char_u *fname;
4176 int len;
4177 int context; /* EXPAND_FILES etc. */
4178{
4179 char_u *retval;
4180 int i, j;
4181 int new_len;
4182 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004183 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004185 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004186 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004187 && context != EXPAND_SHELLCMD
4188 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
4190 /*
4191 * Matching will be done internally (on something other than files).
4192 * So we convert the file-matching-type wildcards into our kind for
4193 * use with vim_regcomp(). First work out how long it will be:
4194 */
4195
4196 /* For help tags the translation is done in find_help_tags().
4197 * For a tag pattern starting with "/" no translation is needed. */
4198 if (context == EXPAND_HELP
4199 || context == EXPAND_COLORS
4200 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004201 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004202 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 || (context == EXPAND_TAGS && fname[0] == '/'))
4204 retval = vim_strnsave(fname, len);
4205 else
4206 {
4207 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4208 for (i = 0; i < len; i++)
4209 {
4210 if (fname[i] == '*' || fname[i] == '~')
4211 new_len++; /* '*' needs to be replaced by ".*"
4212 '~' needs to be replaced by "\~" */
4213
4214 /* Buffer names are like file names. "." should be literal */
4215 if (context == EXPAND_BUFFERS && fname[i] == '.')
4216 new_len++; /* "." becomes "\." */
4217
4218 /* Custom expansion takes care of special things, match
4219 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004220 if ((context == EXPAND_USER_DEFINED
4221 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 new_len++; /* '\' becomes "\\" */
4223 }
4224 retval = alloc(new_len);
4225 if (retval != NULL)
4226 {
4227 retval[0] = '^';
4228 j = 1;
4229 for (i = 0; i < len; i++, j++)
4230 {
4231 /* Skip backslash. But why? At least keep it for custom
4232 * expansion. */
4233 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004234 && context != EXPAND_USER_LIST
4235 && fname[i] == '\\'
4236 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 break;
4238
4239 switch (fname[i])
4240 {
4241 case '*': retval[j++] = '.';
4242 break;
4243 case '~': retval[j++] = '\\';
4244 break;
4245 case '?': retval[j] = '.';
4246 continue;
4247 case '.': if (context == EXPAND_BUFFERS)
4248 retval[j++] = '\\';
4249 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004250 case '\\': if (context == EXPAND_USER_DEFINED
4251 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 retval[j++] = '\\';
4253 break;
4254 }
4255 retval[j] = fname[i];
4256 }
4257 retval[j] = NUL;
4258 }
4259 }
4260 }
4261 else
4262 {
4263 retval = alloc(len + 4);
4264 if (retval != NULL)
4265 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004266 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
4268 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004269 * Don't add a star to *, ~, ~user, $var or `cmd`.
4270 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 * ~ would be at the start of the file name, but not the tail.
4272 * $ could be anywhere in the tail.
4273 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004274 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 */
4276 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004277 ends_in_star = (len > 0 && retval[len - 1] == '*');
4278#ifndef BACKSLASH_IN_FILENAME
4279 for (i = len - 2; i >= 0; --i)
4280 {
4281 if (retval[i] != '\\')
4282 break;
4283 ends_in_star = !ends_in_star;
4284 }
4285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004287 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 && vim_strchr(tail, '$') == NULL
4289 && vim_strchr(retval, '`') == NULL)
4290 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004291 else if (len > 0 && retval[len - 1] == '$')
4292 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 retval[len] = NUL;
4294 }
4295 }
4296 return retval;
4297}
4298
4299/*
4300 * Must parse the command line so far to work out what context we are in.
4301 * Completion can then be done based on that context.
4302 * This routine sets the variables:
4303 * xp->xp_pattern The start of the pattern to be expanded within
4304 * the command line (ends at the cursor).
4305 * xp->xp_context The type of thing to expand. Will be one of:
4306 *
4307 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4308 * the command line, like an unknown command. Caller
4309 * should beep.
4310 * EXPAND_NOTHING Unrecognised context for completion, use char like
4311 * a normal char, rather than for completion. eg
4312 * :s/^I/
4313 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4314 * it.
4315 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4316 * EXPAND_FILES After command with XFILE set, or after setting
4317 * with P_EXPAND set. eg :e ^I, :w>>^I
4318 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4319 * when we know only directories are of interest. eg
4320 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004321 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4323 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4324 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4325 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4326 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4327 * EXPAND_EVENTS Complete event names
4328 * EXPAND_SYNTAX Complete :syntax command arguments
4329 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4330 * EXPAND_AUGROUP Complete autocommand group names
4331 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4332 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4333 * eg :unmap a^I , :cunab x^I
4334 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4335 * eg :call sub^I
4336 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4337 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4338 * names in expressions, eg :while s^I
4339 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004340 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 */
4342 static void
4343set_expand_context(xp)
4344 expand_T *xp;
4345{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004346 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 if (ccline.cmdfirstc != ':'
4348#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004349 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004350 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351#endif
4352 )
4353 {
4354 xp->xp_context = EXPAND_NOTHING;
4355 return;
4356 }
4357 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4358}
4359
4360 void
4361set_cmd_context(xp, str, len, col)
4362 expand_T *xp;
4363 char_u *str; /* start of command line */
4364 int len; /* length of command line (excl. NUL) */
4365 int col; /* position of cursor */
4366{
4367 int old_char = NUL;
4368 char_u *nextcomm;
4369
4370 /*
4371 * Avoid a UMR warning from Purify, only save the character if it has been
4372 * written before.
4373 */
4374 if (col < len)
4375 old_char = str[col];
4376 str[col] = NUL;
4377 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004378
4379#ifdef FEAT_EVAL
4380 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004381 {
4382# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004383 /* pass CMD_SIZE because there is no real command */
4384 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004385# endif
4386 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004387 else if (ccline.input_fn)
4388 {
4389 xp->xp_context = ccline.xp_context;
4390 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004391# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004392 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004393# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004394 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004395 else
4396#endif
4397 while (nextcomm != NULL)
4398 nextcomm = set_one_cmd_context(xp, nextcomm);
4399
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 str[col] = old_char;
4401}
4402
4403/*
4404 * Expand the command line "str" from context "xp".
4405 * "xp" must have been set by set_cmd_context().
4406 * xp->xp_pattern points into "str", to where the text that is to be expanded
4407 * starts.
4408 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4409 * cursor.
4410 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4411 * key that triggered expansion literally.
4412 * Returns EXPAND_OK otherwise.
4413 */
4414 int
4415expand_cmdline(xp, str, col, matchcount, matches)
4416 expand_T *xp;
4417 char_u *str; /* start of command line */
4418 int col; /* position of cursor */
4419 int *matchcount; /* return: nr of matches */
4420 char_u ***matches; /* return: array of pointers to matches */
4421{
4422 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004423 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424
4425 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4426 {
4427 beep_flush();
4428 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4429 }
4430 if (xp->xp_context == EXPAND_NOTHING)
4431 {
4432 /* Caller can use the character as a normal char instead */
4433 return EXPAND_NOTHING;
4434 }
4435
4436 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004437 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4438 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 if (file_str == NULL)
4440 return EXPAND_UNSUCCESSFUL;
4441
Bram Moolenaar94950a92010-12-02 16:01:29 +01004442 if (p_wic)
4443 options += WILD_ICASE;
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004446 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
4448 *matchcount = 0;
4449 *matches = NULL;
4450 }
4451 vim_free(file_str);
4452
4453 return EXPAND_OK;
4454}
4455
4456#ifdef FEAT_MULTI_LANG
4457/*
4458 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4459 */
4460static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4461
4462 static void
4463cleanup_help_tags(num_file, file)
4464 int num_file;
4465 char_u **file;
4466{
4467 int i, j;
4468 int len;
4469
4470 for (i = 0; i < num_file; ++i)
4471 {
4472 len = (int)STRLEN(file[i]) - 3;
4473 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4474 {
4475 /* Sorting on priority means the same item in another language may
4476 * be anywhere. Search all items for a match up to the "@en". */
4477 for (j = 0; j < num_file; ++j)
4478 if (j != i
4479 && (int)STRLEN(file[j]) == len + 3
4480 && STRNCMP(file[i], file[j], len + 1) == 0)
4481 break;
4482 if (j == num_file)
4483 file[i][len] = NUL;
4484 }
4485 }
4486}
4487#endif
4488
4489/*
4490 * Do the expansion based on xp->xp_context and "pat".
4491 */
4492 static int
4493ExpandFromContext(xp, pat, num_file, file, options)
4494 expand_T *xp;
4495 char_u *pat;
4496 int *num_file;
4497 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004498 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499{
4500#ifdef FEAT_CMDL_COMPL
4501 regmatch_T regmatch;
4502#endif
4503 int ret;
4504 int flags;
4505
4506 flags = EW_DIR; /* include directories */
4507 if (options & WILD_LIST_NOTFOUND)
4508 flags |= EW_NOTFOUND;
4509 if (options & WILD_ADD_SLASH)
4510 flags |= EW_ADDSLASH;
4511 if (options & WILD_KEEP_ALL)
4512 flags |= EW_KEEPALL;
4513 if (options & WILD_SILENT)
4514 flags |= EW_SILENT;
4515
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004516 if (xp->xp_context == EXPAND_FILES
4517 || xp->xp_context == EXPAND_DIRECTORIES
4518 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
4520 /*
4521 * Expand file or directory names.
4522 */
4523 int free_pat = FALSE;
4524 int i;
4525
4526 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4527 * space */
4528 if (xp->xp_backslash != XP_BS_NONE)
4529 {
4530 free_pat = TRUE;
4531 pat = vim_strsave(pat);
4532 for (i = 0; pat[i]; ++i)
4533 if (pat[i] == '\\')
4534 {
4535 if (xp->xp_backslash == XP_BS_THREE
4536 && pat[i + 1] == '\\'
4537 && pat[i + 2] == '\\'
4538 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004539 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (xp->xp_backslash == XP_BS_ONE
4541 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004542 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544 }
4545
4546 if (xp->xp_context == EXPAND_FILES)
4547 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004548 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4549 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 else
4551 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004552 if (options & WILD_ICASE)
4553 flags |= EW_ICASE;
4554
Bram Moolenaard7834d32009-12-02 16:14:36 +00004555 /* Expand wildcards, supporting %:h and the like. */
4556 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (free_pat)
4558 vim_free(pat);
4559 return ret;
4560 }
4561
4562 *file = (char_u **)"";
4563 *num_file = 0;
4564 if (xp->xp_context == EXPAND_HELP)
4565 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004566 /* With an empty argument we would get all the help tags, which is
4567 * very slow. Get matches for "help" instead. */
4568 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4569 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
4571#ifdef FEAT_MULTI_LANG
4572 cleanup_help_tags(*num_file, *file);
4573#endif
4574 return OK;
4575 }
4576 return FAIL;
4577 }
4578
4579#ifndef FEAT_CMDL_COMPL
4580 return FAIL;
4581#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004582 if (xp->xp_context == EXPAND_SHELLCMD)
4583 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 if (xp->xp_context == EXPAND_OLD_SETTING)
4585 return ExpandOldSetting(num_file, file);
4586 if (xp->xp_context == EXPAND_BUFFERS)
4587 return ExpandBufnames(pat, num_file, file, options);
4588 if (xp->xp_context == EXPAND_TAGS
4589 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4590 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4591 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004592 {
4593 char *directories[] = {"colors", NULL};
4594 return ExpandRTDir(pat, num_file, file, directories);
4595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004597 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004598 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004599 return ExpandRTDir(pat, num_file, file, directories);
4600 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004601 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004602 {
4603 char *directories[] = {"syntax", NULL};
4604 return ExpandRTDir(pat, num_file, file, directories);
4605 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004606 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004607 {
4608 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4609 return ExpandRTDir(pat, num_file, file, directories);
4610 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004611# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4612 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004613 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615
4616 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4617 if (regmatch.regprog == NULL)
4618 return FAIL;
4619
4620 /* set ignore-case according to p_ic, p_scs and pat */
4621 regmatch.rm_ic = ignorecase(pat);
4622
4623 if (xp->xp_context == EXPAND_SETTINGS
4624 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4625 ret = ExpandSettings(xp, &regmatch, num_file, file);
4626 else if (xp->xp_context == EXPAND_MAPPINGS)
4627 ret = ExpandMappings(&regmatch, num_file, file);
4628# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4629 else if (xp->xp_context == EXPAND_USER_DEFINED)
4630 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4631# endif
4632 else
4633 {
4634 static struct expgen
4635 {
4636 int context;
4637 char_u *((*func)__ARGS((expand_T *, int)));
4638 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004639 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 } tab[] =
4641 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004642 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4643 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004644#ifdef FEAT_CMDHIST
4645 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004648 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4649 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4650 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4651 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652#endif
4653#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004654 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4655 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4656 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4657 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#endif
4659#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004660 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4661 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662#endif
4663#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004664 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004666 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004668 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4669 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004671#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004672 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004673#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004674#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004675 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004676#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004677#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004678 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4681 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004682 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4683 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004685 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004686 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 };
4688 int i;
4689
4690 /*
4691 * Find a context in the table and call the ExpandGeneric() with the
4692 * right function to do the expansion.
4693 */
4694 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004695 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 if (xp->xp_context == tab[i].context)
4697 {
4698 if (tab[i].ic)
4699 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004700 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004701 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 break;
4703 }
4704 }
4705
4706 vim_free(regmatch.regprog);
4707
4708 return ret;
4709#endif /* FEAT_CMDL_COMPL */
4710}
4711
4712#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4713/*
4714 * Expand a list of names.
4715 *
4716 * Generic function for command line completion. It calls a function to
4717 * obtain strings, one by one. The strings are matched against a regexp
4718 * program. Matching strings are copied into an array, which is returned.
4719 *
4720 * Returns OK when no problems encountered, FAIL for error (out of memory).
4721 */
4722 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004723ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 expand_T *xp;
4725 regmatch_T *regmatch;
4726 int *num_file;
4727 char_u ***file;
4728 char_u *((*func)__ARGS((expand_T *, int)));
4729 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004730 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 int i;
4733 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004734 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 char_u *str;
4736
4737 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004738 * round == 0: count the number of matching names
4739 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004741 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
4743 for (i = 0; ; ++i)
4744 {
4745 str = (*func)(xp, i);
4746 if (str == NULL) /* end of list */
4747 break;
4748 if (*str == NUL) /* skip empty strings */
4749 continue;
4750
4751 if (vim_regexec(regmatch, str, (colnr_T)0))
4752 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004753 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004755 if (escaped)
4756 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4757 else
4758 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 (*file)[count] = str;
4760#ifdef FEAT_MENU
4761 if (func == get_menu_names && str != NULL)
4762 {
4763 /* test for separator added by get_menu_names() */
4764 str += STRLEN(str) - 1;
4765 if (*str == '\001')
4766 *str = '.';
4767 }
4768#endif
4769 }
4770 ++count;
4771 }
4772 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004773 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 if (count == 0)
4776 return OK;
4777 *num_file = count;
4778 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4779 if (*file == NULL)
4780 {
4781 *file = (char_u **)"";
4782 return FAIL;
4783 }
4784 count = 0;
4785 }
4786 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004787
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004788 /* Sort the results. Keep menu's in the specified order. */
4789 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004790 {
4791 if (xp->xp_context == EXPAND_EXPRESSION
4792 || xp->xp_context == EXPAND_FUNCTIONS
4793 || xp->xp_context == EXPAND_USER_FUNC)
4794 /* <SNR> functions should be sorted to the end. */
4795 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4796 sort_func_compare);
4797 else
4798 sort_strings(*file, *num_file);
4799 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004800
Bram Moolenaar4f688582007-07-24 12:34:30 +00004801#ifdef FEAT_CMDL_COMPL
4802 /* Reset the variables used for special highlight names expansion, so that
4803 * they don't show up when getting normal highlight names by ID. */
4804 reset_expand_highlight();
4805#endif
4806
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 return OK;
4808}
4809
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004810/*
4811 * Complete a shell command.
4812 * Returns FAIL or OK;
4813 */
4814 static int
4815expand_shellcmd(filepat, num_file, file, flagsarg)
4816 char_u *filepat; /* pattern to match with command names */
4817 int *num_file; /* return: number of matches */
4818 char_u ***file; /* return: array with matches */
4819 int flagsarg; /* EW_ flags */
4820{
4821 char_u *pat;
4822 int i;
4823 char_u *path;
4824 int mustfree = FALSE;
4825 garray_T ga;
4826 char_u *buf = alloc(MAXPATHL);
4827 size_t l;
4828 char_u *s, *e;
4829 int flags = flagsarg;
4830 int ret;
4831
4832 if (buf == NULL)
4833 return FAIL;
4834
4835 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4836 * space */
4837 pat = vim_strsave(filepat);
4838 for (i = 0; pat[i]; ++i)
4839 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004840 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004841
4842 flags |= EW_FILE | EW_EXEC;
4843
4844 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004845 if (mch_isFullName(pat))
4846 path = (char_u *)" ";
4847 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004848 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4849 path = (char_u *)".";
4850 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004851 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004852 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004853 if (path == NULL)
4854 path = (char_u *)"";
4855 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004856
4857 /*
4858 * Go over all directories in $PATH. Expand matches in that directory and
4859 * collect them in "ga".
4860 */
4861 ga_init2(&ga, (int)sizeof(char *), 10);
4862 for (s = path; *s != NUL; s = e)
4863 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004864 if (*s == ' ')
4865 ++s; /* Skip space used for absolute path name. */
4866
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004867#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4868 e = vim_strchr(s, ';');
4869#else
4870 e = vim_strchr(s, ':');
4871#endif
4872 if (e == NULL)
4873 e = s + STRLEN(s);
4874
4875 l = e - s;
4876 if (l > MAXPATHL - 5)
4877 break;
4878 vim_strncpy(buf, s, l);
4879 add_pathsep(buf);
4880 l = STRLEN(buf);
4881 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4882
4883 /* Expand matches in one directory of $PATH. */
4884 ret = expand_wildcards(1, &buf, num_file, file, flags);
4885 if (ret == OK)
4886 {
4887 if (ga_grow(&ga, *num_file) == FAIL)
4888 FreeWild(*num_file, *file);
4889 else
4890 {
4891 for (i = 0; i < *num_file; ++i)
4892 {
4893 s = (*file)[i];
4894 if (STRLEN(s) > l)
4895 {
4896 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004897 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004898 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4899 }
4900 else
4901 vim_free(s);
4902 }
4903 vim_free(*file);
4904 }
4905 }
4906 if (*e != NUL)
4907 ++e;
4908 }
4909 *file = ga.ga_data;
4910 *num_file = ga.ga_len;
4911
4912 vim_free(buf);
4913 vim_free(pat);
4914 if (mustfree)
4915 vim_free(path);
4916 return OK;
4917}
4918
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004921static 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));
4922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004924 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004925 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004927 static void *
4928call_user_expand_func(user_expand_func, xp, num_file, file)
4929 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 int *num_file;
4932 char_u ***file;
4933{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004934 char_u keep;
4935 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004938 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004939 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940
4941 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004942 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 *num_file = 0;
4944 *file = NULL;
4945
Bram Moolenaar21669c02008-01-18 12:16:16 +00004946 if (ccline.cmdbuff == NULL)
4947 {
4948 /* Completion from Insert mode, pass fake arguments. */
4949 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004950 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004951 args[1] = xp->xp_pattern;
4952 }
4953 else
4954 {
4955 /* Completion on the command line, pass real arguments. */
4956 keep = ccline.cmdbuff[ccline.cmdlen];
4957 ccline.cmdbuff[ccline.cmdlen] = 0;
4958 sprintf((char *)num, "%d", ccline.cmdpos);
4959 args[1] = ccline.cmdbuff;
4960 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004961 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 args[2] = num;
4963
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004964 /* Save the cmdline, we don't know what the function may do. */
4965 save_ccline = ccline;
4966 ccline.cmdbuff = NULL;
4967 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004969
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004970 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004971
4972 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004974 if (ccline.cmdbuff != NULL)
4975 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004976
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004977 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004978 return ret;
4979}
4980
4981/*
4982 * Expand names with a function defined by the user.
4983 */
4984 static int
4985ExpandUserDefined(xp, regmatch, num_file, file)
4986 expand_T *xp;
4987 regmatch_T *regmatch;
4988 int *num_file;
4989 char_u ***file;
4990{
4991 char_u *retstr;
4992 char_u *s;
4993 char_u *e;
4994 char_u keep;
4995 garray_T ga;
4996
4997 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4998 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 return FAIL;
5000
5001 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005002 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
5004 e = vim_strchr(s, '\n');
5005 if (e == NULL)
5006 e = s + STRLEN(s);
5007 keep = *e;
5008 *e = 0;
5009
5010 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5011 {
5012 *e = keep;
5013 if (*e != NUL)
5014 ++e;
5015 continue;
5016 }
5017
5018 if (ga_grow(&ga, 1) == FAIL)
5019 break;
5020
5021 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5022 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
5024 *e = keep;
5025 if (*e != NUL)
5026 ++e;
5027 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005028 vim_free(retstr);
5029 *file = ga.ga_data;
5030 *num_file = ga.ga_len;
5031 return OK;
5032}
5033
5034/*
5035 * Expand names with a list returned by a function defined by the user.
5036 */
5037 static int
5038ExpandUserList(xp, num_file, file)
5039 expand_T *xp;
5040 int *num_file;
5041 char_u ***file;
5042{
5043 list_T *retlist;
5044 listitem_T *li;
5045 garray_T ga;
5046
5047 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5048 if (retlist == NULL)
5049 return FAIL;
5050
5051 ga_init2(&ga, (int)sizeof(char *), 3);
5052 /* Loop over the items in the list. */
5053 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5054 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005055 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5056 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005057
5058 if (ga_grow(&ga, 1) == FAIL)
5059 break;
5060
5061 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005062 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005063 ++ga.ga_len;
5064 }
5065 list_unref(retlist);
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 *file = ga.ga_data;
5068 *num_file = ga.ga_len;
5069 return OK;
5070}
5071#endif
5072
5073/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005074 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005075 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005076 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 */
5078 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005079ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 char_u *pat;
5081 int *num_file;
5082 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005083 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005085 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 char_u *s;
5087 char_u *e;
5088 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005089 int i;
5090 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
5092 *num_file = 0;
5093 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005094 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005095 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005097 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005099 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5100 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005102 ga_clear_strings(&ga);
5103 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005105 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5106 matches = globpath(p_rtp, s, 0);
5107 vim_free(s);
5108 if (matches == NULL)
5109 continue;
5110
5111 for (s = matches; *s != NUL; s = e)
5112 {
5113 e = vim_strchr(s, '\n');
5114 if (e == NULL)
5115 e = s + STRLEN(s);
5116 if (ga_grow(&ga, 1) == FAIL)
5117 break;
5118 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5119 {
5120 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5121 if (*s == '\n' || vim_ispathsep(*s))
5122 break;
5123 ++s;
5124 ((char_u **)ga.ga_data)[ga.ga_len] =
5125 vim_strnsave(s, (int)(e - s - 4));
5126 ++ga.ga_len;
5127 }
5128 if (*e != NUL)
5129 ++e;
5130 }
5131 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005133 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005134 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005135
5136 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005137 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005138 remove_duplicates(&ga);
5139
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 *file = ga.ga_data;
5141 *num_file = ga.ga_len;
5142 return OK;
5143}
5144
5145#endif
5146
5147#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5148/*
5149 * Expand "file" for all comma-separated directories in "path".
5150 * Returns an allocated string with all matches concatenated, separated by
5151 * newlines. Returns NULL for an error or no matches.
5152 */
5153 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005154globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 char_u *path;
5156 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005157 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158{
5159 expand_T xpc;
5160 char_u *buf;
5161 garray_T ga;
5162 int i;
5163 int len;
5164 int num_p;
5165 char_u **p;
5166 char_u *cur = NULL;
5167
5168 buf = alloc(MAXPATHL);
5169 if (buf == NULL)
5170 return NULL;
5171
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005172 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 ga_init2(&ga, 1, 100);
5176
5177 /* Loop over all entries in {path}. */
5178 while (*path != NUL)
5179 {
5180 /* Copy one item of the path to buf[] and concatenate the file name. */
5181 copy_option_part(&path, buf, MAXPATHL, ",");
5182 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5183 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005184# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005185 /* Using the platform's path separator (\) makes vim incorrectly
5186 * treat it as an escape character, use '/' instead. */
5187 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5188 STRCAT(buf, "/");
5189# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005193 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5194 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005196 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005198 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
5200 /* Concatenate new results to previous ones. */
5201 if (ga_grow(&ga, len) == OK)
5202 {
5203 cur = (char_u *)ga.ga_data + ga.ga_len;
5204 for (i = 0; i < num_p; ++i)
5205 {
5206 STRCPY(cur, p[i]);
5207 cur += STRLEN(p[i]);
5208 *cur++ = '\n';
5209 }
5210 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
5212 FreeWild(num_p, p);
5213 }
5214 }
5215 }
5216 if (cur != NULL)
5217 *--cur = 0; /* Replace trailing newline with NUL */
5218
5219 vim_free(buf);
5220 return (char_u *)ga.ga_data;
5221}
5222
5223#endif
5224
5225#if defined(FEAT_CMDHIST) || defined(PROTO)
5226
5227/*********************************
5228 * Command line history stuff *
5229 *********************************/
5230
5231/*
5232 * Translate a history character to the associated type number.
5233 */
5234 static int
5235hist_char2type(c)
5236 int c;
5237{
5238 if (c == ':')
5239 return HIST_CMD;
5240 if (c == '=')
5241 return HIST_EXPR;
5242 if (c == '@')
5243 return HIST_INPUT;
5244 if (c == '>')
5245 return HIST_DEBUG;
5246 return HIST_SEARCH; /* must be '?' or '/' */
5247}
5248
5249/*
5250 * Table of history names.
5251 * These names are used in :history and various hist...() functions.
5252 * It is sufficient to give the significant prefix of a history name.
5253 */
5254
5255static char *(history_names[]) =
5256{
5257 "cmd",
5258 "search",
5259 "expr",
5260 "input",
5261 "debug",
5262 NULL
5263};
5264
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005265#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5266/*
5267 * Function given to ExpandGeneric() to obtain the possible first
5268 * arguments of the ":history command.
5269 */
5270 static char_u *
5271get_history_arg(xp, idx)
5272 expand_T *xp UNUSED;
5273 int idx;
5274{
5275 static char_u compl[2] = { NUL, NUL };
5276 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005277 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005278 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5279
5280 if (idx < short_names_count)
5281 {
5282 compl[0] = (char_u)short_names[idx];
5283 return compl;
5284 }
5285 if (idx < short_names_count + history_name_count)
5286 return (char_u *)history_names[idx - short_names_count];
5287 if (idx == short_names_count + history_name_count)
5288 return (char_u *)"all";
5289 return NULL;
5290}
5291#endif
5292
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293/*
5294 * init_history() - Initialize the command line history.
5295 * Also used to re-allocate the history when the size changes.
5296 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005297 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298init_history()
5299{
5300 int newlen; /* new length of history table */
5301 histentry_T *temp;
5302 int i;
5303 int j;
5304 int type;
5305
5306 /*
5307 * If size of history table changed, reallocate it
5308 */
5309 newlen = (int)p_hi;
5310 if (newlen != hislen) /* history length changed */
5311 {
5312 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5313 {
5314 if (newlen)
5315 {
5316 temp = (histentry_T *)lalloc(
5317 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5318 if (temp == NULL) /* out of memory! */
5319 {
5320 if (type == 0) /* first one: just keep the old length */
5321 {
5322 newlen = hislen;
5323 break;
5324 }
5325 /* Already changed one table, now we can only have zero
5326 * length for all tables. */
5327 newlen = 0;
5328 type = -1;
5329 continue;
5330 }
5331 }
5332 else
5333 temp = NULL;
5334 if (newlen == 0 || temp != NULL)
5335 {
5336 if (hisidx[type] < 0) /* there are no entries yet */
5337 {
5338 for (i = 0; i < newlen; ++i)
5339 {
5340 temp[i].hisnum = 0;
5341 temp[i].hisstr = NULL;
5342 }
5343 }
5344 else if (newlen > hislen) /* array becomes bigger */
5345 {
5346 for (i = 0; i <= hisidx[type]; ++i)
5347 temp[i] = history[type][i];
5348 j = i;
5349 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
5350 {
5351 temp[i].hisnum = 0;
5352 temp[i].hisstr = NULL;
5353 }
5354 for ( ; j < hislen; ++i, ++j)
5355 temp[i] = history[type][j];
5356 }
5357 else /* array becomes smaller or 0 */
5358 {
5359 j = hisidx[type];
5360 for (i = newlen - 1; ; --i)
5361 {
5362 if (i >= 0) /* copy newest entries */
5363 temp[i] = history[type][j];
5364 else /* remove older entries */
5365 vim_free(history[type][j].hisstr);
5366 if (--j < 0)
5367 j = hislen - 1;
5368 if (j == hisidx[type])
5369 break;
5370 }
5371 hisidx[type] = newlen - 1;
5372 }
5373 vim_free(history[type]);
5374 history[type] = temp;
5375 }
5376 }
5377 hislen = newlen;
5378 }
5379}
5380
5381/*
5382 * Check if command line 'str' is already in history.
5383 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5384 */
5385 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005386in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 int type;
5388 char_u *str;
5389 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005390 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392 int i;
5393 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005394 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395
5396 if (hisidx[type] < 0)
5397 return FALSE;
5398 i = hisidx[type];
5399 do
5400 {
5401 if (history[type][i].hisstr == NULL)
5402 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005403
5404 /* For search history, check that the separator character matches as
5405 * well. */
5406 p = history[type][i].hisstr;
5407 if (STRCMP(str, p) == 0
5408 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 {
5410 if (!move_to_front)
5411 return TRUE;
5412 last_i = i;
5413 break;
5414 }
5415 if (--i < 0)
5416 i = hislen - 1;
5417 } while (i != hisidx[type]);
5418
5419 if (last_i >= 0)
5420 {
5421 str = history[type][i].hisstr;
5422 while (i != hisidx[type])
5423 {
5424 if (++i >= hislen)
5425 i = 0;
5426 history[type][last_i] = history[type][i];
5427 last_i = i;
5428 }
5429 history[type][i].hisstr = str;
5430 history[type][i].hisnum = ++hisnum[type];
5431 return TRUE;
5432 }
5433 return FALSE;
5434}
5435
5436/*
5437 * Convert history name (from table above) to its HIST_ equivalent.
5438 * When "name" is empty, return "cmd" history.
5439 * Returns -1 for unknown history name.
5440 */
5441 int
5442get_histtype(name)
5443 char_u *name;
5444{
5445 int i;
5446 int len = (int)STRLEN(name);
5447
5448 /* No argument: use current history. */
5449 if (len == 0)
5450 return hist_char2type(ccline.cmdfirstc);
5451
5452 for (i = 0; history_names[i] != NULL; ++i)
5453 if (STRNICMP(name, history_names[i], len) == 0)
5454 return i;
5455
5456 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5457 return hist_char2type(name[0]);
5458
5459 return -1;
5460}
5461
5462static int last_maptick = -1; /* last seen maptick */
5463
5464/*
5465 * Add the given string to the given history. If the string is already in the
5466 * history then it is moved to the front. "histype" may be one of he HIST_
5467 * values.
5468 */
5469 void
5470add_to_history(histype, new_entry, in_map, sep)
5471 int histype;
5472 char_u *new_entry;
5473 int in_map; /* consider maptick when inside a mapping */
5474 int sep; /* separator character used (search hist) */
5475{
5476 histentry_T *hisptr;
5477 int len;
5478
5479 if (hislen == 0) /* no history */
5480 return;
5481
5482 /*
5483 * Searches inside the same mapping overwrite each other, so that only
5484 * the last line is kept. Be careful not to remove a line that was moved
5485 * down, only lines that were added.
5486 */
5487 if (histype == HIST_SEARCH && in_map)
5488 {
5489 if (maptick == last_maptick)
5490 {
5491 /* Current line is from the same mapping, remove it */
5492 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5493 vim_free(hisptr->hisstr);
5494 hisptr->hisstr = NULL;
5495 hisptr->hisnum = 0;
5496 --hisnum[histype];
5497 if (--hisidx[HIST_SEARCH] < 0)
5498 hisidx[HIST_SEARCH] = hislen - 1;
5499 }
5500 last_maptick = -1;
5501 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005502 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 if (++hisidx[histype] == hislen)
5505 hisidx[histype] = 0;
5506 hisptr = &history[histype][hisidx[histype]];
5507 vim_free(hisptr->hisstr);
5508
5509 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005510 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5512 if (hisptr->hisstr != NULL)
5513 hisptr->hisstr[len + 1] = sep;
5514
5515 hisptr->hisnum = ++hisnum[histype];
5516 if (histype == HIST_SEARCH && in_map)
5517 last_maptick = maptick;
5518 }
5519}
5520
5521#if defined(FEAT_EVAL) || defined(PROTO)
5522
5523/*
5524 * Get identifier of newest history entry.
5525 * "histype" may be one of the HIST_ values.
5526 */
5527 int
5528get_history_idx(histype)
5529 int histype;
5530{
5531 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5532 || hisidx[histype] < 0)
5533 return -1;
5534
5535 return history[histype][hisidx[histype]].hisnum;
5536}
5537
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005538static struct cmdline_info *get_ccline_ptr __ARGS((void));
5539
5540/*
5541 * Get pointer to the command line info to use. cmdline_paste() may clear
5542 * ccline and put the previous value in prev_ccline.
5543 */
5544 static struct cmdline_info *
5545get_ccline_ptr()
5546{
5547 if ((State & CMDLINE) == 0)
5548 return NULL;
5549 if (ccline.cmdbuff != NULL)
5550 return &ccline;
5551 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5552 return &prev_ccline;
5553 return NULL;
5554}
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556/*
5557 * Get the current command line in allocated memory.
5558 * Only works when the command line is being edited.
5559 * Returns NULL when something is wrong.
5560 */
5561 char_u *
5562get_cmdline_str()
5563{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005564 struct cmdline_info *p = get_ccline_ptr();
5565
5566 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005568 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569}
5570
5571/*
5572 * Get the current command line position, counted in bytes.
5573 * Zero is the first position.
5574 * Only works when the command line is being edited.
5575 * Returns -1 when something is wrong.
5576 */
5577 int
5578get_cmdline_pos()
5579{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005580 struct cmdline_info *p = get_ccline_ptr();
5581
5582 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005584 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585}
5586
5587/*
5588 * Set the command line byte position to "pos". Zero is the first position.
5589 * Only works when the command line is being edited.
5590 * Returns 1 when failed, 0 when OK.
5591 */
5592 int
5593set_cmdline_pos(pos)
5594 int pos;
5595{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005596 struct cmdline_info *p = get_ccline_ptr();
5597
5598 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 return 1;
5600
5601 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5602 * changed the command line. */
5603 if (pos < 0)
5604 new_cmdpos = 0;
5605 else
5606 new_cmdpos = pos;
5607 return 0;
5608}
5609
5610/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005611 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005612 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005613 * Only works when the command line is being edited.
5614 * Returns NUL when something is wrong.
5615 */
5616 int
5617get_cmdline_type()
5618{
5619 struct cmdline_info *p = get_ccline_ptr();
5620
5621 if (p == NULL)
5622 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005623 if (p->cmdfirstc == NUL)
5624 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005625 return p->cmdfirstc;
5626}
5627
5628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 * Calculate history index from a number:
5630 * num > 0: seen as identifying number of a history entry
5631 * num < 0: relative position in history wrt newest entry
5632 * "histype" may be one of the HIST_ values.
5633 */
5634 static int
5635calc_hist_idx(histype, num)
5636 int histype;
5637 int num;
5638{
5639 int i;
5640 histentry_T *hist;
5641 int wrapped = FALSE;
5642
5643 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5644 || (i = hisidx[histype]) < 0 || num == 0)
5645 return -1;
5646
5647 hist = history[histype];
5648 if (num > 0)
5649 {
5650 while (hist[i].hisnum > num)
5651 if (--i < 0)
5652 {
5653 if (wrapped)
5654 break;
5655 i += hislen;
5656 wrapped = TRUE;
5657 }
5658 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5659 return i;
5660 }
5661 else if (-num <= hislen)
5662 {
5663 i += num + 1;
5664 if (i < 0)
5665 i += hislen;
5666 if (hist[i].hisstr != NULL)
5667 return i;
5668 }
5669 return -1;
5670}
5671
5672/*
5673 * Get a history entry by its index.
5674 * "histype" may be one of the HIST_ values.
5675 */
5676 char_u *
5677get_history_entry(histype, idx)
5678 int histype;
5679 int idx;
5680{
5681 idx = calc_hist_idx(histype, idx);
5682 if (idx >= 0)
5683 return history[histype][idx].hisstr;
5684 else
5685 return (char_u *)"";
5686}
5687
5688/*
5689 * Clear all entries of a history.
5690 * "histype" may be one of the HIST_ values.
5691 */
5692 int
5693clr_history(histype)
5694 int histype;
5695{
5696 int i;
5697 histentry_T *hisptr;
5698
5699 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5700 {
5701 hisptr = history[histype];
5702 for (i = hislen; i--;)
5703 {
5704 vim_free(hisptr->hisstr);
5705 hisptr->hisnum = 0;
5706 hisptr++->hisstr = NULL;
5707 }
5708 hisidx[histype] = -1; /* mark history as cleared */
5709 hisnum[histype] = 0; /* reset identifier counter */
5710 return OK;
5711 }
5712 return FAIL;
5713}
5714
5715/*
5716 * Remove all entries matching {str} from a history.
5717 * "histype" may be one of the HIST_ values.
5718 */
5719 int
5720del_history_entry(histype, str)
5721 int histype;
5722 char_u *str;
5723{
5724 regmatch_T regmatch;
5725 histentry_T *hisptr;
5726 int idx;
5727 int i;
5728 int last;
5729 int found = FALSE;
5730
5731 regmatch.regprog = NULL;
5732 regmatch.rm_ic = FALSE; /* always match case */
5733 if (hislen != 0
5734 && histype >= 0
5735 && histype < HIST_COUNT
5736 && *str != NUL
5737 && (idx = hisidx[histype]) >= 0
5738 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5739 != NULL)
5740 {
5741 i = last = idx;
5742 do
5743 {
5744 hisptr = &history[histype][i];
5745 if (hisptr->hisstr == NULL)
5746 break;
5747 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5748 {
5749 found = TRUE;
5750 vim_free(hisptr->hisstr);
5751 hisptr->hisstr = NULL;
5752 hisptr->hisnum = 0;
5753 }
5754 else
5755 {
5756 if (i != last)
5757 {
5758 history[histype][last] = *hisptr;
5759 hisptr->hisstr = NULL;
5760 hisptr->hisnum = 0;
5761 }
5762 if (--last < 0)
5763 last += hislen;
5764 }
5765 if (--i < 0)
5766 i += hislen;
5767 } while (i != idx);
5768 if (history[histype][idx].hisstr == NULL)
5769 hisidx[histype] = -1;
5770 }
5771 vim_free(regmatch.regprog);
5772 return found;
5773}
5774
5775/*
5776 * Remove an indexed entry from a history.
5777 * "histype" may be one of the HIST_ values.
5778 */
5779 int
5780del_history_idx(histype, idx)
5781 int histype;
5782 int idx;
5783{
5784 int i, j;
5785
5786 i = calc_hist_idx(histype, idx);
5787 if (i < 0)
5788 return FALSE;
5789 idx = hisidx[histype];
5790 vim_free(history[histype][i].hisstr);
5791
5792 /* When deleting the last added search string in a mapping, reset
5793 * last_maptick, so that the last added search string isn't deleted again.
5794 */
5795 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5796 last_maptick = -1;
5797
5798 while (i != idx)
5799 {
5800 j = (i + 1) % hislen;
5801 history[histype][i] = history[histype][j];
5802 i = j;
5803 }
5804 history[histype][i].hisstr = NULL;
5805 history[histype][i].hisnum = 0;
5806 if (--i < 0)
5807 i += hislen;
5808 hisidx[histype] = i;
5809 return TRUE;
5810}
5811
5812#endif /* FEAT_EVAL */
5813
5814#if defined(FEAT_CRYPT) || defined(PROTO)
5815/*
5816 * Very specific function to remove the value in ":set key=val" from the
5817 * history.
5818 */
5819 void
5820remove_key_from_history()
5821{
5822 char_u *p;
5823 int i;
5824
5825 i = hisidx[HIST_CMD];
5826 if (i < 0)
5827 return;
5828 p = history[HIST_CMD][i].hisstr;
5829 if (p != NULL)
5830 for ( ; *p; ++p)
5831 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5832 {
5833 p = vim_strchr(p + 3, '=');
5834 if (p == NULL)
5835 break;
5836 ++p;
5837 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5838 if (p[i] == '\\' && p[i + 1])
5839 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005840 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 --p;
5842 }
5843}
5844#endif
5845
5846#endif /* FEAT_CMDHIST */
5847
5848#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5849/*
5850 * Get indices "num1,num2" that specify a range within a list (not a range of
5851 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5852 * Returns OK if parsed successfully, otherwise FAIL.
5853 */
5854 int
5855get_list_range(str, num1, num2)
5856 char_u **str;
5857 int *num1;
5858 int *num2;
5859{
5860 int len;
5861 int first = FALSE;
5862 long num;
5863
5864 *str = skipwhite(*str);
5865 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5866 {
5867 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5868 *str += len;
5869 *num1 = (int)num;
5870 first = TRUE;
5871 }
5872 *str = skipwhite(*str);
5873 if (**str == ',') /* parse "to" part of range */
5874 {
5875 *str = skipwhite(*str + 1);
5876 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5877 if (len > 0)
5878 {
5879 *num2 = (int)num;
5880 *str = skipwhite(*str + len);
5881 }
5882 else if (!first) /* no number given at all */
5883 return FAIL;
5884 }
5885 else if (first) /* only one number given */
5886 *num2 = *num1;
5887 return OK;
5888}
5889#endif
5890
5891#if defined(FEAT_CMDHIST) || defined(PROTO)
5892/*
5893 * :history command - print a history
5894 */
5895 void
5896ex_history(eap)
5897 exarg_T *eap;
5898{
5899 histentry_T *hist;
5900 int histype1 = HIST_CMD;
5901 int histype2 = HIST_CMD;
5902 int hisidx1 = 1;
5903 int hisidx2 = -1;
5904 int idx;
5905 int i, j, k;
5906 char_u *end;
5907 char_u *arg = eap->arg;
5908
5909 if (hislen == 0)
5910 {
5911 MSG(_("'history' option is zero"));
5912 return;
5913 }
5914
5915 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5916 {
5917 end = arg;
5918 while (ASCII_ISALPHA(*end)
5919 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5920 end++;
5921 i = *end;
5922 *end = NUL;
5923 histype1 = get_histtype(arg);
5924 if (histype1 == -1)
5925 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005926 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {
5928 histype1 = 0;
5929 histype2 = HIST_COUNT-1;
5930 }
5931 else
5932 {
5933 *end = i;
5934 EMSG(_(e_trailing));
5935 return;
5936 }
5937 }
5938 else
5939 histype2 = histype1;
5940 *end = i;
5941 }
5942 else
5943 end = arg;
5944 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5945 {
5946 EMSG(_(e_trailing));
5947 return;
5948 }
5949
5950 for (; !got_int && histype1 <= histype2; ++histype1)
5951 {
5952 STRCPY(IObuff, "\n # ");
5953 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5954 MSG_PUTS_TITLE(IObuff);
5955 idx = hisidx[histype1];
5956 hist = history[histype1];
5957 j = hisidx1;
5958 k = hisidx2;
5959 if (j < 0)
5960 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5961 if (k < 0)
5962 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5963 if (idx >= 0 && j <= k)
5964 for (i = idx + 1; !got_int; ++i)
5965 {
5966 if (i == hislen)
5967 i = 0;
5968 if (hist[i].hisstr != NULL
5969 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5970 {
5971 msg_putchar('\n');
5972 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5973 hist[i].hisnum);
5974 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5975 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005976 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 else
5978 STRCAT(IObuff, hist[i].hisstr);
5979 msg_outtrans(IObuff);
5980 out_flush();
5981 }
5982 if (i == idx)
5983 break;
5984 }
5985 }
5986}
5987#endif
5988
5989#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5990static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5991static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5992static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5993static int viminfo_add_at_front = FALSE;
5994
5995static int hist_type2char __ARGS((int type, int use_question));
5996
5997/*
5998 * Translate a history type number to the associated character.
5999 */
6000 static int
6001hist_type2char(type, use_question)
6002 int type;
6003 int use_question; /* use '?' instead of '/' */
6004{
6005 if (type == HIST_CMD)
6006 return ':';
6007 if (type == HIST_SEARCH)
6008 {
6009 if (use_question)
6010 return '?';
6011 else
6012 return '/';
6013 }
6014 if (type == HIST_EXPR)
6015 return '=';
6016 return '@';
6017}
6018
6019/*
6020 * Prepare for reading the history from the viminfo file.
6021 * This allocates history arrays to store the read history lines.
6022 */
6023 void
6024prepare_viminfo_history(asklen)
6025 int asklen;
6026{
6027 int i;
6028 int num;
6029 int type;
6030 int len;
6031
6032 init_history();
6033 viminfo_add_at_front = (asklen != 0);
6034 if (asklen > hislen)
6035 asklen = hislen;
6036
6037 for (type = 0; type < HIST_COUNT; ++type)
6038 {
6039 /*
6040 * Count the number of empty spaces in the history list. If there are
6041 * more spaces available than we request, then fill them up.
6042 */
6043 for (i = 0, num = 0; i < hislen; i++)
6044 if (history[type][i].hisstr == NULL)
6045 num++;
6046 len = asklen;
6047 if (num > len)
6048 len = num;
6049 if (len <= 0)
6050 viminfo_history[type] = NULL;
6051 else
6052 viminfo_history[type] =
6053 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6054 if (viminfo_history[type] == NULL)
6055 len = 0;
6056 viminfo_hislen[type] = len;
6057 viminfo_hisidx[type] = 0;
6058 }
6059}
6060
6061/*
6062 * Accept a line from the viminfo, store it in the history array when it's
6063 * new.
6064 */
6065 int
6066read_viminfo_history(virp)
6067 vir_T *virp;
6068{
6069 int type;
6070 long_u len;
6071 char_u *val;
6072 char_u *p;
6073
6074 type = hist_char2type(virp->vir_line[0]);
6075 if (viminfo_hisidx[type] < viminfo_hislen[type])
6076 {
6077 val = viminfo_readstring(virp, 1, TRUE);
6078 if (val != NULL && *val != NUL)
6079 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006080 int sep = (*val == ' ' ? NUL : *val);
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006083 viminfo_add_at_front, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 {
6085 /* Need to re-allocate to append the separator byte. */
6086 len = STRLEN(val);
6087 p = lalloc(len + 2, TRUE);
6088 if (p != NULL)
6089 {
6090 if (type == HIST_SEARCH)
6091 {
6092 /* Search entry: Move the separator from the first
6093 * column to after the NUL. */
6094 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006095 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 }
6097 else
6098 {
6099 /* Not a search entry: No separator in the viminfo
6100 * file, add a NUL separator. */
6101 mch_memmove(p, val, (size_t)len + 1);
6102 p[len + 1] = NUL;
6103 }
6104 viminfo_history[type][viminfo_hisidx[type]++] = p;
6105 }
6106 }
6107 }
6108 vim_free(val);
6109 }
6110 return viminfo_readline(virp);
6111}
6112
6113 void
6114finish_viminfo_history()
6115{
6116 int idx;
6117 int i;
6118 int type;
6119
6120 for (type = 0; type < HIST_COUNT; ++type)
6121 {
6122 if (history[type] == NULL)
6123 return;
6124 idx = hisidx[type] + viminfo_hisidx[type];
6125 if (idx >= hislen)
6126 idx -= hislen;
6127 else if (idx < 0)
6128 idx = hislen - 1;
6129 if (viminfo_add_at_front)
6130 hisidx[type] = idx;
6131 else
6132 {
6133 if (hisidx[type] == -1)
6134 hisidx[type] = hislen - 1;
6135 do
6136 {
6137 if (history[type][idx].hisstr != NULL)
6138 break;
6139 if (++idx == hislen)
6140 idx = 0;
6141 } while (idx != hisidx[type]);
6142 if (idx != hisidx[type] && --idx < 0)
6143 idx = hislen - 1;
6144 }
6145 for (i = 0; i < viminfo_hisidx[type]; i++)
6146 {
6147 vim_free(history[type][idx].hisstr);
6148 history[type][idx].hisstr = viminfo_history[type][i];
6149 if (--idx < 0)
6150 idx = hislen - 1;
6151 }
6152 idx += 1;
6153 idx %= hislen;
6154 for (i = 0; i < viminfo_hisidx[type]; i++)
6155 {
6156 history[type][idx++].hisnum = ++hisnum[type];
6157 idx %= hislen;
6158 }
6159 vim_free(viminfo_history[type]);
6160 viminfo_history[type] = NULL;
6161 }
6162}
6163
6164 void
6165write_viminfo_history(fp)
6166 FILE *fp;
6167{
6168 int i;
6169 int type;
6170 int num_saved;
6171 char_u *p;
6172 int c;
6173
6174 init_history();
6175 if (hislen == 0)
6176 return;
6177 for (type = 0; type < HIST_COUNT; ++type)
6178 {
6179 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6180 if (num_saved == 0)
6181 continue;
6182 if (num_saved < 0) /* Use default */
6183 num_saved = hislen;
6184 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6185 type == HIST_CMD ? _("Command Line") :
6186 type == HIST_SEARCH ? _("Search String") :
6187 type == HIST_EXPR ? _("Expression") :
6188 _("Input Line"));
6189 if (num_saved > hislen)
6190 num_saved = hislen;
6191 i = hisidx[type];
6192 if (i >= 0)
6193 while (num_saved--)
6194 {
6195 p = history[type][i].hisstr;
6196 if (p != NULL)
6197 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006198 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 /* For the search history: put the separator in the second
6200 * column; use a space if there isn't one. */
6201 if (type == HIST_SEARCH)
6202 {
6203 c = p[STRLEN(p) + 1];
6204 putc(c == NUL ? ' ' : c, fp);
6205 }
6206 viminfo_writestring(fp, p);
6207 }
6208 if (--i < 0)
6209 i = hislen - 1;
6210 }
6211 }
6212}
6213#endif /* FEAT_VIMINFO */
6214
6215#if defined(FEAT_FKMAP) || defined(PROTO)
6216/*
6217 * Write a character at the current cursor+offset position.
6218 * It is directly written into the command buffer block.
6219 */
6220 void
6221cmd_pchar(c, offset)
6222 int c, offset;
6223{
6224 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6225 {
6226 EMSG(_("E198: cmd_pchar beyond the command length"));
6227 return;
6228 }
6229 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6230 ccline.cmdbuff[ccline.cmdlen] = NUL;
6231}
6232
6233 int
6234cmd_gchar(offset)
6235 int offset;
6236{
6237 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6238 {
6239 /* EMSG(_("cmd_gchar beyond the command length")); */
6240 return NUL;
6241 }
6242 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6243}
6244#endif
6245
6246#if defined(FEAT_CMDWIN) || defined(PROTO)
6247/*
6248 * Open a window on the current command line and history. Allow editing in
6249 * the window. Returns when the window is closed.
6250 * Returns:
6251 * CR if the command is to be executed
6252 * Ctrl_C if it is to be abandoned
6253 * K_IGNORE if editing continues
6254 */
6255 static int
6256ex_window()
6257{
6258 struct cmdline_info save_ccline;
6259 buf_T *old_curbuf = curbuf;
6260 win_T *old_curwin = curwin;
6261 buf_T *bp;
6262 win_T *wp;
6263 int i;
6264 linenr_T lnum;
6265 int histtype;
6266 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006267#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 int save_restart_edit = restart_edit;
6271 int save_State = State;
6272 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006273#ifdef FEAT_RIGHTLEFT
6274 int save_cmdmsg_rl = cmdmsg_rl;
6275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276
6277 /* Can't do this recursively. Can't do it when typing a password. */
6278 if (cmdwin_type != 0
6279# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6280 || cmdline_star > 0
6281# endif
6282 )
6283 {
6284 beep_flush();
6285 return K_IGNORE;
6286 }
6287
6288 /* Save current window sizes. */
6289 win_size_save(&winsizes);
6290
6291# ifdef FEAT_AUTOCMD
6292 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006293 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006295 /* don't use a new tab page */
6296 cmdmod.tab = 0;
6297
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 /* Create a window for the command-line buffer. */
6299 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6300 {
6301 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006302# ifdef FEAT_AUTOCMD
6303 unblock_autocmds();
6304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 return K_IGNORE;
6306 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006307 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
6309 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006310 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006311 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6313 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6314 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006315#ifdef FEAT_FOLDING
6316 curwin->w_p_fen = FALSE;
6317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006319 curwin->w_p_rl = cmdmsg_rl;
6320 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006322 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323
6324# ifdef FEAT_AUTOCMD
6325 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006326 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327# endif
6328
Bram Moolenaar46152342005-09-07 21:18:43 +00006329 /* Showing the prompt may have set need_wait_return, reset it. */
6330 need_wait_return = FALSE;
6331
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006332 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6334 {
6335 if (p_wc == TAB)
6336 {
6337 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6338 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6339 }
6340 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6341 }
6342
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006343 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6344 * sets 'textwidth' to 78). */
6345 curbuf->b_p_tw = 0;
6346
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 /* Fill the buffer with the history. */
6348 init_history();
6349 if (hislen > 0)
6350 {
6351 i = hisidx[histtype];
6352 if (i >= 0)
6353 {
6354 lnum = 0;
6355 do
6356 {
6357 if (++i == hislen)
6358 i = 0;
6359 if (history[histtype][i].hisstr != NULL)
6360 ml_append(lnum++, history[histtype][i].hisstr,
6361 (colnr_T)0, FALSE);
6362 }
6363 while (i != hisidx[histtype]);
6364 }
6365 }
6366
6367 /* Replace the empty last line with the current command-line and put the
6368 * cursor there. */
6369 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6370 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6371 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006372 changed_line_abv_curs();
6373 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006374 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 /* Save the command line info, can be used recursively. */
6377 save_ccline = ccline;
6378 ccline.cmdbuff = NULL;
6379 ccline.cmdprompt = NULL;
6380
6381 /* No Ex mode here! */
6382 exmode_active = 0;
6383
6384 State = NORMAL;
6385# ifdef FEAT_MOUSE
6386 setmouse();
6387# endif
6388
6389# ifdef FEAT_AUTOCMD
6390 /* Trigger CmdwinEnter autocommands. */
6391 typestr[0] = cmdwin_type;
6392 typestr[1] = NUL;
6393 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006394 if (restart_edit != 0) /* autocmd with ":startinsert" */
6395 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396# endif
6397
6398 i = RedrawingDisabled;
6399 RedrawingDisabled = 0;
6400
6401 /*
6402 * Call the main loop until <CR> or CTRL-C is typed.
6403 */
6404 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006405 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407 RedrawingDisabled = i;
6408
6409# ifdef FEAT_AUTOCMD
6410 /* Trigger CmdwinLeave autocommands. */
6411 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6412# endif
6413
Bram Moolenaarccc18222007-05-10 18:25:20 +00006414 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 ccline = save_ccline;
6416 cmdwin_type = 0;
6417
6418 exmode_active = save_exmode;
6419
Bram Moolenaarf9821062008-06-20 16:31:07 +00006420 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 * this happens! */
6422 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6423 {
6424 cmdwin_result = Ctrl_C;
6425 EMSG(_("E199: Active window or buffer deleted"));
6426 }
6427 else
6428 {
6429# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6430 /* autocmds may abort script processing */
6431 if (aborting() && cmdwin_result != K_IGNORE)
6432 cmdwin_result = Ctrl_C;
6433# endif
6434 /* Set the new command line from the cmdline buffer. */
6435 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006436 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006438 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6439
6440 if (histtype == HIST_CMD)
6441 {
6442 /* Execute the command directly. */
6443 ccline.cmdbuff = vim_strsave((char_u *)p);
6444 cmdwin_result = CAR;
6445 }
6446 else
6447 {
6448 /* First need to cancel what we were doing. */
6449 ccline.cmdbuff = NULL;
6450 stuffcharReadbuff(':');
6451 stuffReadbuff((char_u *)p);
6452 stuffcharReadbuff(CAR);
6453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 }
6455 else if (cmdwin_result == K_XF2) /* :qa typed */
6456 {
6457 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6458 cmdwin_result = CAR;
6459 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006460 else if (cmdwin_result == Ctrl_C)
6461 {
6462 /* :q or :close, don't execute any command
6463 * and don't modify the cmd window. */
6464 ccline.cmdbuff = NULL;
6465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 else
6467 ccline.cmdbuff = vim_strsave(ml_get_curline());
6468 if (ccline.cmdbuff == NULL)
6469 cmdwin_result = Ctrl_C;
6470 else
6471 {
6472 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6473 ccline.cmdbufflen = ccline.cmdlen + 1;
6474 ccline.cmdpos = curwin->w_cursor.col;
6475 if (ccline.cmdpos > ccline.cmdlen)
6476 ccline.cmdpos = ccline.cmdlen;
6477 if (cmdwin_result == K_IGNORE)
6478 {
6479 set_cmdspos_cursor();
6480 redrawcmd();
6481 }
6482 }
6483
6484# ifdef FEAT_AUTOCMD
6485 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006486 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487# endif
6488 wp = curwin;
6489 bp = curbuf;
6490 win_goto(old_curwin);
6491 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006492
6493 /* win_close() may have already wiped the buffer when 'bh' is
6494 * set to 'wipe' */
6495 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006496 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
6498 /* Restore window sizes. */
6499 win_size_restore(&winsizes);
6500
6501# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006502 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503# endif
6504 }
6505
6506 ga_clear(&winsizes);
6507 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006508# ifdef FEAT_RIGHTLEFT
6509 cmdmsg_rl = save_cmdmsg_rl;
6510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
6512 State = save_State;
6513# ifdef FEAT_MOUSE
6514 setmouse();
6515# endif
6516
6517 return cmdwin_result;
6518}
6519#endif /* FEAT_CMDWIN */
6520
6521/*
6522 * Used for commands that either take a simple command string argument, or:
6523 * cmd << endmarker
6524 * {script}
6525 * endmarker
6526 * Returns a pointer to allocated memory with {script} or NULL.
6527 */
6528 char_u *
6529script_get(eap, cmd)
6530 exarg_T *eap;
6531 char_u *cmd;
6532{
6533 char_u *theline;
6534 char *end_pattern = NULL;
6535 char dot[] = ".";
6536 garray_T ga;
6537
6538 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6539 return NULL;
6540
6541 ga_init2(&ga, 1, 0x400);
6542
6543 if (cmd[2] != NUL)
6544 end_pattern = (char *)skipwhite(cmd + 2);
6545 else
6546 end_pattern = dot;
6547
6548 for (;;)
6549 {
6550 theline = eap->getline(
6551#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006552 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553#endif
6554 NUL, eap->cookie, 0);
6555
6556 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006557 {
6558 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561
6562 ga_concat(&ga, theline);
6563 ga_append(&ga, '\n');
6564 vim_free(theline);
6565 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006566 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
6568 return (char_u *)ga.ga_data;
6569}