blob: c94dd1198e6d42222fab4226fee4732722c767ac [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 */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020059 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 char_u *hisstr; /* actual entry, separator char after the NUL */
61} histentry_T;
62
63static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
64static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
65static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
66 /* identifying (unique) number of newest history entry */
67static int hislen = 0; /* actual length of history tables */
68
69static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaar4c402232011-07-27 17:58:46 +020071static int in_history __ARGS((int, char_u *, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072# ifdef FEAT_EVAL
73static int calc_hist_idx __ARGS((int histype, int num));
74# endif
75#endif
76
77#ifdef FEAT_RIGHTLEFT
78static int cmd_hkmap = 0; /* Hebrew mapping during command line */
79#endif
80
81#ifdef FEAT_FKMAP
82static int cmd_fkmap = 0; /* Farsi mapping during command line */
83#endif
84
85static int cmdline_charsize __ARGS((int idx));
86static void set_cmdspos __ARGS((void));
87static void set_cmdspos_cursor __ARGS((void));
88#ifdef FEAT_MBYTE
89static void correct_cmdspos __ARGS((int idx, int cells));
90#endif
91static void alloc_cmdbuff __ARGS((int len));
92static int realloc_cmdbuff __ARGS((int len));
93static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000094static void save_cmdline __ARGS((struct cmdline_info *ccp));
95static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar1769d5a2006-10-17 14:25:24 +000096static int cmdline_paste __ARGS((int regname, int literally, int remcr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
98static void redrawcmd_preedit __ARGS((void));
99#endif
100#ifdef FEAT_WILDMENU
101static void cmdline_del __ARGS((int from));
102#endif
103static void redrawcmdprompt __ARGS((void));
104static void cursorcmd __ARGS((void));
105static int ccheck_abbr __ARGS((int));
Bram Moolenaarb3479632012-11-28 16:49:58 +0100106static int nextwild __ARGS((expand_T *xp, int type, int options, int escape));
Bram Moolenaar45360022005-07-21 21:08:21 +0000107static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108static int showmatches __ARGS((expand_T *xp, int wildmenu));
109static void set_expand_context __ARGS((expand_T *xp));
110static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
111static int expand_showtail __ARGS((expand_T *xp));
112#ifdef FEAT_CMDL_COMPL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000113static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar0c7437a2011-06-26 19:40:23 +0200114static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname[]));
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200115# ifdef FEAT_CMDHIST
116static char_u *get_history_arg __ARGS((expand_T *xp, int idx));
Bram Moolenaarb3049f42013-04-05 18:58:47 +0200117static void clear_hist_entry __ARGS((histentry_T *hisptr));
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
120static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000121static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122# endif
123#endif
124
125#ifdef FEAT_CMDWIN
126static int ex_window __ARGS((void));
127#endif
128
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200129#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
130static int
131#ifdef __BORLANDC__
132_RTLENTRYF
133#endif
134sort_func_compare __ARGS((const void *s1, const void *s2));
135#endif
136
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137/*
138 * getcmdline() - accept a command line starting with firstc.
139 *
140 * firstc == ':' get ":" command line.
141 * firstc == '/' or '?' get search pattern
142 * firstc == '=' get expression
143 * firstc == '@' get text for input() function
144 * firstc == '>' get text for debug mode
145 * firstc == NUL get text for :insert command
146 * firstc == -1 like NUL, and break on CTRL-C
147 *
148 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
149 * command line.
150 *
151 * Careful: getcmdline() can be called recursively!
152 *
153 * Return pointer to allocated string if there is a commandline, NULL
154 * otherwise.
155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156 char_u *
157getcmdline(firstc, count, indent)
158 int firstc;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000159 long count UNUSED; /* only used for incremental search */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160 int indent; /* indent for inside conditionals */
161{
162 int c;
163 int i;
164 int j;
165 int gotesc = FALSE; /* TRUE when <ESC> just typed */
166 int do_abbr; /* when TRUE check for abbr. */
167#ifdef FEAT_CMDHIST
168 char_u *lookfor = NULL; /* string to match */
169 int hiscnt; /* current history line in use */
170 int histype; /* history type to be used */
171#endif
172#ifdef FEAT_SEARCH_EXTRA
173 pos_T old_cursor;
174 colnr_T old_curswant;
175 colnr_T old_leftcol;
176 linenr_T old_topline;
177# ifdef FEAT_DIFF
178 int old_topfill;
179# endif
180 linenr_T old_botline;
181 int did_incsearch = FALSE;
182 int incsearch_postponed = FALSE;
183#endif
184 int did_wild_list = FALSE; /* did wild_list() recently */
185 int wim_index = 0; /* index in wim_flags[] */
186 int res;
187 int save_msg_scroll = msg_scroll;
188 int save_State = State; /* remember State when called */
189 int some_key_typed = FALSE; /* one of the keys was typed */
190#ifdef FEAT_MOUSE
191 /* mouse drag and release events are ignored, unless they are
192 * preceded with a mouse down event */
193 int ignore_drag_release = TRUE;
194#endif
195#ifdef FEAT_EVAL
196 int break_ctrl_c = FALSE;
197#endif
198 expand_T xpc;
199 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000200#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
201 /* Everything that may work recursively should save and restore the
202 * current command line in save_ccline. That includes update_screen(), a
203 * custom status line may invoke ":normal". */
204 struct cmdline_info save_ccline;
205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207#ifdef FEAT_SNIFF
208 want_sniff_request = 0;
209#endif
210#ifdef FEAT_EVAL
211 if (firstc == -1)
212 {
213 firstc = NUL;
214 break_ctrl_c = TRUE;
215 }
216#endif
217#ifdef FEAT_RIGHTLEFT
218 /* start without Hebrew mapping for a command line */
219 if (firstc == ':' || firstc == '=' || firstc == '>')
220 cmd_hkmap = 0;
221#endif
222
223 ccline.overstrike = FALSE; /* always start in insert mode */
224#ifdef FEAT_SEARCH_EXTRA
225 old_cursor = curwin->w_cursor; /* needs to be restored later */
226 old_curswant = curwin->w_curswant;
227 old_leftcol = curwin->w_leftcol;
228 old_topline = curwin->w_topline;
229# ifdef FEAT_DIFF
230 old_topfill = curwin->w_topfill;
231# endif
232 old_botline = curwin->w_botline;
233#endif
234
235 /*
236 * set some variables for redrawcmd()
237 */
238 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000239 ccline.cmdindent = (firstc > 0 ? indent : 0);
240
241 /* alloc initial ccline.cmdbuff */
242 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 if (ccline.cmdbuff == NULL)
244 return NULL; /* out of memory */
245 ccline.cmdlen = ccline.cmdpos = 0;
246 ccline.cmdbuff[0] = NUL;
247
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000248 /* autoindent for :insert and :append */
249 if (firstc <= 0)
250 {
251 copy_spaces(ccline.cmdbuff, indent);
252 ccline.cmdbuff[indent] = NUL;
253 ccline.cmdpos = indent;
254 ccline.cmdspos = indent;
255 ccline.cmdlen = indent;
256 }
257
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000259 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261#ifdef FEAT_RIGHTLEFT
262 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
263 && (firstc == '/' || firstc == '?'))
264 cmdmsg_rl = TRUE;
265 else
266 cmdmsg_rl = FALSE;
267#endif
268
269 redir_off = TRUE; /* don't redirect the typed command */
270 if (!cmd_silent)
271 {
272 i = msg_scrolled;
273 msg_scrolled = 0; /* avoid wait_return message */
274 gotocmdline(TRUE);
275 msg_scrolled += i;
276 redrawcmdprompt(); /* draw prompt or indent */
277 set_cmdspos();
278 }
279 xpc.xp_context = EXPAND_NOTHING;
280 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000281#ifndef BACKSLASH_IN_FILENAME
282 xpc.xp_shell = FALSE;
283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000285#if defined(FEAT_EVAL)
286 if (ccline.input_fn)
287 {
288 xpc.xp_context = ccline.xp_context;
289 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000290# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000291 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000292# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000293 }
294#endif
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 /*
297 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
298 * doing ":@0" when register 0 doesn't contain a CR.
299 */
300 msg_scroll = FALSE;
301
302 State = CMDLINE;
303
304 if (firstc == '/' || firstc == '?' || firstc == '@')
305 {
306 /* Use ":lmap" mappings for search pattern and input(). */
307 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
308 b_im_ptr = &curbuf->b_p_iminsert;
309 else
310 b_im_ptr = &curbuf->b_p_imsearch;
311 if (*b_im_ptr == B_IMODE_LMAP)
312 State |= LANGMAP;
313#ifdef USE_IM_CONTROL
314 im_set_active(*b_im_ptr == B_IMODE_IM);
315#endif
316 }
317#ifdef USE_IM_CONTROL
318 else if (p_imcmdline)
319 im_set_active(TRUE);
320#endif
321
322#ifdef FEAT_MOUSE
323 setmouse();
324#endif
325#ifdef CURSOR_SHAPE
326 ui_cursor_shape(); /* may show different cursor shape */
327#endif
328
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000329 /* When inside an autocommand for writing "exiting" may be set and
330 * terminal mode set to cooked. Need to set raw mode here then. */
331 settmode(TMODE_RAW);
332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#ifdef FEAT_CMDHIST
334 init_history();
335 hiscnt = hislen; /* set hiscnt to impossible history value */
336 histype = hist_char2type(firstc);
337#endif
338
339#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000340 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#endif
342
343 /*
344 * Collect the command string, handling editing keys.
345 */
346 for (;;)
347 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000348 redir_off = TRUE; /* Don't redirect the typed command.
349 Repeated, because a ":redir" inside
350 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#ifdef USE_ON_FLY_SCROLL
352 dont_scroll = FALSE; /* allow scrolling here */
353#endif
354 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
355
356 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000357
358 /* Get a character. Ignore K_IGNORE, it should not do anything, such
359 * as stop completion. */
360 do
361 {
362 c = safe_vgetc();
363 } while (c == K_IGNORE);
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 if (KeyTyped)
366 {
367 some_key_typed = TRUE;
368#ifdef FEAT_RIGHTLEFT
369 if (cmd_hkmap)
370 c = hkmap(c);
371# ifdef FEAT_FKMAP
372 if (cmd_fkmap)
373 c = cmdl_fkmap(c);
374# endif
375 if (cmdmsg_rl && !KeyStuffed)
376 {
377 /* Invert horizontal movements and operations. Only when
378 * typed by the user directly, not when the result of a
379 * mapping. */
380 switch (c)
381 {
382 case K_RIGHT: c = K_LEFT; break;
383 case K_S_RIGHT: c = K_S_LEFT; break;
384 case K_C_RIGHT: c = K_C_LEFT; break;
385 case K_LEFT: c = K_RIGHT; break;
386 case K_S_LEFT: c = K_S_RIGHT; break;
387 case K_C_LEFT: c = K_C_RIGHT; break;
388 }
389 }
390#endif
391 }
392
393 /*
394 * Ignore got_int when CTRL-C was typed here.
395 * Don't ignore it in :global, we really need to break then, e.g., for
396 * ":g/pat/normal /pat" (without the <CR>).
397 * Don't ignore it for the input() function.
398 */
399 if ((c == Ctrl_C
400#ifdef UNIX
401 || c == intr_char
402#endif
403 )
404#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
405 && firstc != '@'
406#endif
407#ifdef FEAT_EVAL
408 && !break_ctrl_c
409#endif
410 && !global_busy)
411 got_int = FALSE;
412
413#ifdef FEAT_CMDHIST
414 /* free old command line when finished moving around in the history
415 * list */
416 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000417 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000418 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 && c != K_PAGEDOWN && c != K_PAGEUP
420 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000421 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
423 {
424 vim_free(lookfor);
425 lookfor = NULL;
426 }
427#endif
428
429 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000430 * When there are matching completions to select <S-Tab> works like
431 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000433 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 c = Ctrl_P;
435
436#ifdef FEAT_WILDMENU
437 /* Special translations for 'wildmenu' */
438 if (did_wild_list && p_wmnu)
439 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000440 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000442 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 c = Ctrl_N;
444 }
445 /* Hitting CR after "emenu Name.": complete submenu */
446 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
447 && ccline.cmdpos > 1
448 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
449 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
450 && (c == '\n' || c == '\r' || c == K_KENTER))
451 c = K_DOWN;
452#endif
453
454 /* free expanded names when finished walking through matches */
455 if (xpc.xp_numfiles != -1
456 && !(c == p_wc && KeyTyped) && c != p_wcm
457 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
458 && c != Ctrl_L)
459 {
460 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
461 did_wild_list = FALSE;
462#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000463 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465 xpc.xp_context = EXPAND_NOTHING;
466 wim_index = 0;
467#ifdef FEAT_WILDMENU
468 if (p_wmnu && wild_menu_showing != 0)
469 {
470 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000471 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000472
473 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000474 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475
476 if (wild_menu_showing == WM_SCROLLED)
477 {
478 /* Entered command line, move it up */
479 cmdline_row--;
480 redrawcmd();
481 }
482 else if (save_p_ls != -1)
483 {
484 /* restore 'laststatus' and 'winminheight' */
485 p_ls = save_p_ls;
486 p_wmh = save_p_wmh;
487 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000488 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000490 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 redrawcmd();
492 save_p_ls = -1;
493 }
494 else
495 {
496# ifdef FEAT_VERTSPLIT
497 win_redraw_last_status(topframe);
498# else
499 lastwin->w_redr_status = TRUE;
500# endif
501 redraw_statuslines();
502 }
503 KeyTyped = skt;
504 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000505 if (ccline.input_fn)
506 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 }
508#endif
509 }
510
511#ifdef FEAT_WILDMENU
512 /* Special translations for 'wildmenu' */
513 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
514 {
515 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000516 if (c == K_DOWN && ccline.cmdpos > 0
517 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000519 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 {
521 /* Hitting <Up>: Remove one submenu name in front of the
522 * cursor */
523 int found = FALSE;
524
525 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
526 i = 0;
527 while (--j > 0)
528 {
529 /* check for start of menu name */
530 if (ccline.cmdbuff[j] == ' '
531 && ccline.cmdbuff[j - 1] != '\\')
532 {
533 i = j + 1;
534 break;
535 }
536 /* check for start of submenu name */
537 if (ccline.cmdbuff[j] == '.'
538 && ccline.cmdbuff[j - 1] != '\\')
539 {
540 if (found)
541 {
542 i = j + 1;
543 break;
544 }
545 else
546 found = TRUE;
547 }
548 }
549 if (i > 0)
550 cmdline_del(i);
551 c = p_wc;
552 xpc.xp_context = EXPAND_NOTHING;
553 }
554 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000555 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000556 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000557 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 {
559 char_u upseg[5];
560
561 upseg[0] = PATHSEP;
562 upseg[1] = '.';
563 upseg[2] = '.';
564 upseg[3] = PATHSEP;
565 upseg[4] = NUL;
566
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000567 if (c == K_DOWN
568 && ccline.cmdpos > 0
569 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
570 && (ccline.cmdpos < 3
571 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
573 {
574 /* go down a directory */
575 c = p_wc;
576 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000577 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
579 /* If in a direct ancestor, strip off one ../ to go down */
580 int found = FALSE;
581
582 j = ccline.cmdpos;
583 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
584 while (--j > i)
585 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000586#ifdef FEAT_MBYTE
587 if (has_mbyte)
588 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (vim_ispathsep(ccline.cmdbuff[j]))
591 {
592 found = TRUE;
593 break;
594 }
595 }
596 if (found
597 && ccline.cmdbuff[j - 1] == '.'
598 && ccline.cmdbuff[j - 2] == '.'
599 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
600 {
601 cmdline_del(j - 2);
602 c = p_wc;
603 }
604 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000605 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {
607 /* go up a directory */
608 int found = FALSE;
609
610 j = ccline.cmdpos - 1;
611 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
612 while (--j > i)
613 {
614#ifdef FEAT_MBYTE
615 if (has_mbyte)
616 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
617#endif
618 if (vim_ispathsep(ccline.cmdbuff[j])
619#ifdef BACKSLASH_IN_FILENAME
620 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
621 == NULL
622#endif
623 )
624 {
625 if (found)
626 {
627 i = j + 1;
628 break;
629 }
630 else
631 found = TRUE;
632 }
633 }
634
635 if (!found)
636 j = i;
637 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
638 j += 4;
639 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
640 && j == i)
641 j += 3;
642 else
643 j = 0;
644 if (j > 0)
645 {
646 /* TODO this is only for DOS/UNIX systems - need to put in
647 * machine-specific stuff here and in upseg init */
648 cmdline_del(j);
649 put_on_cmdline(upseg + 1, 3, FALSE);
650 }
651 else if (ccline.cmdpos > i)
652 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100653
654 /* Now complete in the new directory. Set KeyTyped in case the
655 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100657 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661#endif /* FEAT_WILDMENU */
662
663 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
664 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
665 if (c == Ctrl_BSL)
666 {
667 ++no_mapping;
668 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000669 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 --no_mapping;
671 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200672 /* CTRL-\ e doesn't work when obtaining an expression, unless it
673 * is in a mapping. */
674 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
675 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
677 vungetc(c);
678 c = Ctrl_BSL;
679 }
680#ifdef FEAT_EVAL
681 else if (c == 'e')
682 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200683 char_u *p = NULL;
684 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685
686 /*
687 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000688 * Need to save and restore the current command line, to be
689 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 */
691 if (ccline.cmdpos == ccline.cmdlen)
692 new_cmdpos = 99999; /* keep it at the end */
693 else
694 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000695
696 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000698 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 if (c == '=')
700 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000701 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000702 * to avoid nasty things like going to another buffer when
703 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000704 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000705 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000707 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000708 restore_cmdline(&save_ccline);
709
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200710 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200712 len = (int)STRLEN(p);
713 if (realloc_cmdbuff(len + 1) == OK)
714 {
715 ccline.cmdlen = len;
716 STRCPY(ccline.cmdbuff, p);
717 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200719 /* Restore the cursor or use the position set with
720 * set_cmdline_pos(). */
721 if (new_cmdpos > ccline.cmdlen)
722 ccline.cmdpos = ccline.cmdlen;
723 else
724 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200726 KeyTyped = FALSE; /* Don't do p_wc completion. */
727 redrawcmd();
728 goto cmdline_changed;
729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 }
731 }
732 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100733 got_int = FALSE; /* don't abandon the command line */
734 did_emsg = FALSE;
735 emsg_on_display = FALSE;
736 redrawcmd();
737 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739#endif
740 else
741 {
742 if (c == Ctrl_G && p_im && restart_edit == 0)
743 restart_edit = 'a';
744 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
745 in history */
746 goto returncmd; /* back to Normal mode */
747 }
748 }
749
750#ifdef FEAT_CMDWIN
751 if (c == cedit_key || c == K_CMDWIN)
752 {
753 /*
754 * Open a window to edit the command line (and history).
755 */
756 c = ex_window();
757 some_key_typed = TRUE;
758 }
759# ifdef FEAT_DIGRAPHS
760 else
761# endif
762#endif
763#ifdef FEAT_DIGRAPHS
764 c = do_digraph(c);
765#endif
766
767 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
768 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
769 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000770 /* In Ex mode a backslash escapes a newline. */
771 if (exmode_active
772 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000773 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000774 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000775 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000777 if (c == K_KENTER)
778 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000780 else
781 {
782 gotesc = FALSE; /* Might have typed ESC previously, don't
783 truncate the cmdline now. */
784 if (ccheck_abbr(c + ABBR_OFF))
785 goto cmdline_changed;
786 if (!cmd_silent)
787 {
788 windgoto(msg_row, 0);
789 out_flush();
790 }
791 break;
792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 }
794
795 /*
796 * Completion for 'wildchar' or 'wildcharm' key.
797 * - hitting <ESC> twice means: abandon command line.
798 * - wildcard expansion is only done when the 'wildchar' key is really
799 * typed, not when it comes from a macro
800 */
801 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
802 {
803 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
804 {
805 /* if 'wildmode' contains "list" may still need to list */
806 if (xpc.xp_numfiles > 1
807 && !did_wild_list
808 && (wim_flags[wim_index] & WIM_LIST))
809 {
810 (void)showmatches(&xpc, FALSE);
811 redrawcmd();
812 did_wild_list = TRUE;
813 }
814 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100815 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
816 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100818 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
819 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 else
821 res = OK; /* don't insert 'wildchar' now */
822 }
823 else /* typed p_wc first time */
824 {
825 wim_index = 0;
826 j = ccline.cmdpos;
827 /* if 'wildmode' first contains "longest", get longest
828 * common part */
829 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100830 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
831 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100833 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
834 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
836 /* if interrupted while completing, behave like it failed */
837 if (got_int)
838 {
839 (void)vpeekc(); /* remove <C-C> from input stream */
840 got_int = FALSE; /* don't abandon the command line */
841 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
842#ifdef FEAT_WILDMENU
843 xpc.xp_context = EXPAND_NOTHING;
844#endif
845 goto cmdline_changed;
846 }
847
848 /* when more than one match, and 'wildmode' first contains
849 * "list", or no change and 'wildmode' contains "longest,list",
850 * list all matches */
851 if (res == OK && xpc.xp_numfiles > 1)
852 {
853 /* a "longest" that didn't do anything is skipped (but not
854 * "list:longest") */
855 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
856 wim_index = 1;
857 if ((wim_flags[wim_index] & WIM_LIST)
858#ifdef FEAT_WILDMENU
859 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
860#endif
861 )
862 {
863 if (!(wim_flags[0] & WIM_LONGEST))
864 {
865#ifdef FEAT_WILDMENU
866 int p_wmnu_save = p_wmnu;
867 p_wmnu = 0;
868#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100869 /* remove match */
870 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#ifdef FEAT_WILDMENU
872 p_wmnu = p_wmnu_save;
873#endif
874 }
875#ifdef FEAT_WILDMENU
876 (void)showmatches(&xpc, p_wmnu
877 && ((wim_flags[wim_index] & WIM_LIST) == 0));
878#else
879 (void)showmatches(&xpc, FALSE);
880#endif
881 redrawcmd();
882 did_wild_list = TRUE;
883 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100884 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
885 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100887 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
888 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 }
890 else
891 vim_beep();
892 }
893#ifdef FEAT_WILDMENU
894 else if (xpc.xp_numfiles == -1)
895 xpc.xp_context = EXPAND_NOTHING;
896#endif
897 }
898 if (wim_index < 3)
899 ++wim_index;
900 if (c == ESC)
901 gotesc = TRUE;
902 if (res == OK)
903 goto cmdline_changed;
904 }
905
906 gotesc = FALSE;
907
908 /* <S-Tab> goes to last match, in a clumsy way */
909 if (c == K_S_TAB && KeyTyped)
910 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100911 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
912 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
913 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 goto cmdline_changed;
915 }
916
917 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
918 c = NL;
919
920 do_abbr = TRUE; /* default: check for abbreviation */
921
922 /*
923 * Big switch for a typed command line character.
924 */
925 switch (c)
926 {
927 case K_BS:
928 case Ctrl_H:
929 case K_DEL:
930 case K_KDEL:
931 case Ctrl_W:
932#ifdef FEAT_FKMAP
933 if (cmd_fkmap && c == K_BS)
934 c = K_DEL;
935#endif
936 if (c == K_KDEL)
937 c = K_DEL;
938
939 /*
940 * delete current character is the same as backspace on next
941 * character, except at end of line
942 */
943 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
944 ++ccline.cmdpos;
945#ifdef FEAT_MBYTE
946 if (has_mbyte && c == K_DEL)
947 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
948 ccline.cmdbuff + ccline.cmdpos);
949#endif
950 if (ccline.cmdpos > 0)
951 {
952 char_u *p;
953
954 j = ccline.cmdpos;
955 p = ccline.cmdbuff + j;
956#ifdef FEAT_MBYTE
957 if (has_mbyte)
958 {
959 p = mb_prevptr(ccline.cmdbuff, p);
960 if (c == Ctrl_W)
961 {
962 while (p > ccline.cmdbuff && vim_isspace(*p))
963 p = mb_prevptr(ccline.cmdbuff, p);
964 i = mb_get_class(p);
965 while (p > ccline.cmdbuff && mb_get_class(p) == i)
966 p = mb_prevptr(ccline.cmdbuff, p);
967 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000968 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 }
970 }
971 else
972#endif
973 if (c == Ctrl_W)
974 {
975 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
976 --p;
977 i = vim_iswordc(p[-1]);
978 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
979 && vim_iswordc(p[-1]) == i)
980 --p;
981 }
982 else
983 --p;
984 ccline.cmdpos = (int)(p - ccline.cmdbuff);
985 ccline.cmdlen -= j - ccline.cmdpos;
986 i = ccline.cmdpos;
987 while (i < ccline.cmdlen)
988 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
989
990 /* Truncate at the end, required for multi-byte chars. */
991 ccline.cmdbuff[ccline.cmdlen] = NUL;
992 redrawcmd();
993 }
994 else if (ccline.cmdlen == 0 && c != Ctrl_W
995 && ccline.cmdprompt == NULL && indent == 0)
996 {
997 /* In ex and debug mode it doesn't make sense to return. */
998 if (exmode_active
999#ifdef FEAT_EVAL
1000 || ccline.cmdfirstc == '>'
1001#endif
1002 )
1003 goto cmdline_not_changed;
1004
1005 vim_free(ccline.cmdbuff); /* no commandline to return */
1006 ccline.cmdbuff = NULL;
1007 if (!cmd_silent)
1008 {
1009#ifdef FEAT_RIGHTLEFT
1010 if (cmdmsg_rl)
1011 msg_col = Columns;
1012 else
1013#endif
1014 msg_col = 0;
1015 msg_putchar(' '); /* delete ':' */
1016 }
1017 redraw_cmdline = TRUE;
1018 goto returncmd; /* back to cmd mode */
1019 }
1020 goto cmdline_changed;
1021
1022 case K_INS:
1023 case K_KINS:
1024#ifdef FEAT_FKMAP
1025 /* if Farsi mode set, we are in reverse insert mode -
1026 Do not change the mode */
1027 if (cmd_fkmap)
1028 beep_flush();
1029 else
1030#endif
1031 ccline.overstrike = !ccline.overstrike;
1032#ifdef CURSOR_SHAPE
1033 ui_cursor_shape(); /* may show different cursor shape */
1034#endif
1035 goto cmdline_not_changed;
1036
1037 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001038 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
1040 /* ":lmap" mappings exists, toggle use of mappings. */
1041 State ^= LANGMAP;
1042#ifdef USE_IM_CONTROL
1043 im_set_active(FALSE); /* Disable input method */
1044#endif
1045 if (b_im_ptr != NULL)
1046 {
1047 if (State & LANGMAP)
1048 *b_im_ptr = B_IMODE_LMAP;
1049 else
1050 *b_im_ptr = B_IMODE_NONE;
1051 }
1052 }
1053#ifdef USE_IM_CONTROL
1054 else
1055 {
1056 /* There are no ":lmap" mappings, toggle IM. When
1057 * 'imdisable' is set don't try getting the status, it's
1058 * always off. */
1059 if ((p_imdisable && b_im_ptr != NULL)
1060 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1061 {
1062 im_set_active(FALSE); /* Disable input method */
1063 if (b_im_ptr != NULL)
1064 *b_im_ptr = B_IMODE_NONE;
1065 }
1066 else
1067 {
1068 im_set_active(TRUE); /* Enable input method */
1069 if (b_im_ptr != NULL)
1070 *b_im_ptr = B_IMODE_IM;
1071 }
1072 }
1073#endif
1074 if (b_im_ptr != NULL)
1075 {
1076 if (b_im_ptr == &curbuf->b_p_iminsert)
1077 set_iminsert_global();
1078 else
1079 set_imsearch_global();
1080 }
1081#ifdef CURSOR_SHAPE
1082 ui_cursor_shape(); /* may show different cursor shape */
1083#endif
1084#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1085 /* Show/unshow value of 'keymap' in status lines later. */
1086 status_redraw_curbuf();
1087#endif
1088 goto cmdline_not_changed;
1089
1090/* case '@': only in very old vi */
1091 case Ctrl_U:
1092 /* delete all characters left of the cursor */
1093 j = ccline.cmdpos;
1094 ccline.cmdlen -= j;
1095 i = ccline.cmdpos = 0;
1096 while (i < ccline.cmdlen)
1097 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1098 /* Truncate at the end, required for multi-byte chars. */
1099 ccline.cmdbuff[ccline.cmdlen] = NUL;
1100 redrawcmd();
1101 goto cmdline_changed;
1102
1103#ifdef FEAT_CLIPBOARD
1104 case Ctrl_Y:
1105 /* Copy the modeless selection, if there is one. */
1106 if (clip_star.state != SELECT_CLEARED)
1107 {
1108 if (clip_star.state == SELECT_DONE)
1109 clip_copy_modeless_selection(TRUE);
1110 goto cmdline_not_changed;
1111 }
1112 break;
1113#endif
1114
1115 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1116 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001117 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001118 * ":normal" runs out of characters. */
1119 if (exmode_active
1120#ifdef FEAT_EX_EXTRA
1121 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1122#endif
1123 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 goto cmdline_not_changed;
1125
1126 gotesc = TRUE; /* will free ccline.cmdbuff after
1127 putting it in history */
1128 goto returncmd; /* back to cmd mode */
1129
1130 case Ctrl_R: /* insert register */
1131#ifdef USE_ON_FLY_SCROLL
1132 dont_scroll = TRUE; /* disallow scrolling here */
1133#endif
1134 putcmdline('"', TRUE);
1135 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001136 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (i == Ctrl_O)
1138 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1139 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001140 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 --no_mapping;
1142#ifdef FEAT_EVAL
1143 /*
1144 * Insert the result of an expression.
1145 * Need to save the current command line, to be able to enter
1146 * a new one...
1147 */
1148 new_cmdpos = -1;
1149 if (c == '=')
1150 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1152 {
1153 beep_flush();
1154 c = ESC;
1155 }
1156 else
1157 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001158 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001160 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162 }
1163#endif
1164 if (c != ESC) /* use ESC to cancel inserting register */
1165 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001166 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001167
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001168#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001169 /* When there was a serious error abort getting the
1170 * command line. */
1171 if (aborting())
1172 {
1173 gotesc = TRUE; /* will free ccline.cmdbuff after
1174 putting it in history */
1175 goto returncmd; /* back to cmd mode */
1176 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 KeyTyped = FALSE; /* Don't do p_wc completion. */
1179#ifdef FEAT_EVAL
1180 if (new_cmdpos >= 0)
1181 {
1182 /* set_cmdline_pos() was used */
1183 if (new_cmdpos > ccline.cmdlen)
1184 ccline.cmdpos = ccline.cmdlen;
1185 else
1186 ccline.cmdpos = new_cmdpos;
1187 }
1188#endif
1189 }
1190 redrawcmd();
1191 goto cmdline_changed;
1192
1193 case Ctrl_D:
1194 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1195 break; /* Use ^D as normal char instead */
1196
1197 redrawcmd();
1198 continue; /* don't do incremental search now */
1199
1200 case K_RIGHT:
1201 case K_S_RIGHT:
1202 case K_C_RIGHT:
1203 do
1204 {
1205 if (ccline.cmdpos >= ccline.cmdlen)
1206 break;
1207 i = cmdline_charsize(ccline.cmdpos);
1208 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1209 break;
1210 ccline.cmdspos += i;
1211#ifdef FEAT_MBYTE
1212 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001213 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 + ccline.cmdpos);
1215 else
1216#endif
1217 ++ccline.cmdpos;
1218 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001219 while ((c == K_S_RIGHT || c == K_C_RIGHT
1220 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1222#ifdef FEAT_MBYTE
1223 if (has_mbyte)
1224 set_cmdspos_cursor();
1225#endif
1226 goto cmdline_not_changed;
1227
1228 case K_LEFT:
1229 case K_S_LEFT:
1230 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001231 if (ccline.cmdpos == 0)
1232 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 do
1234 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 --ccline.cmdpos;
1236#ifdef FEAT_MBYTE
1237 if (has_mbyte) /* move to first byte of char */
1238 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1239 ccline.cmdbuff + ccline.cmdpos);
1240#endif
1241 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1242 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001243 while (ccline.cmdpos > 0
1244 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001245 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1247#ifdef FEAT_MBYTE
1248 if (has_mbyte)
1249 set_cmdspos_cursor();
1250#endif
1251 goto cmdline_not_changed;
1252
1253 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001254 /* Ignore mouse event or ex_window() result. */
1255 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
Bram Moolenaar4770d092006-01-12 23:22:24 +00001257#ifdef FEAT_GUI_W32
1258 /* On Win32 ignore <M-F4>, we get it when closing the window was
1259 * cancelled. */
1260 case K_F4:
1261 if (mod_mask == MOD_MASK_ALT)
1262 {
1263 redrawcmd(); /* somehow the cmdline is cleared */
1264 goto cmdline_not_changed;
1265 }
1266 break;
1267#endif
1268
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269#ifdef FEAT_MOUSE
1270 case K_MIDDLEDRAG:
1271 case K_MIDDLERELEASE:
1272 goto cmdline_not_changed; /* Ignore mouse */
1273
1274 case K_MIDDLEMOUSE:
1275# ifdef FEAT_GUI
1276 /* When GUI is active, also paste when 'mouse' is empty */
1277 if (!gui.in_use)
1278# endif
1279 if (!mouse_has(MOUSE_COMMAND))
1280 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001281# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001283 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001285# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001286 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 redrawcmd();
1288 goto cmdline_changed;
1289
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001290# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001292 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 redrawcmd();
1294 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
1297 case K_LEFTDRAG:
1298 case K_LEFTRELEASE:
1299 case K_RIGHTDRAG:
1300 case K_RIGHTRELEASE:
1301 /* Ignore drag and release events when the button-down wasn't
1302 * seen before. */
1303 if (ignore_drag_release)
1304 goto cmdline_not_changed;
1305 /* FALLTHROUGH */
1306 case K_LEFTMOUSE:
1307 case K_RIGHTMOUSE:
1308 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1309 ignore_drag_release = TRUE;
1310 else
1311 ignore_drag_release = FALSE;
1312# ifdef FEAT_GUI
1313 /* When GUI is active, also move when 'mouse' is empty */
1314 if (!gui.in_use)
1315# endif
1316 if (!mouse_has(MOUSE_COMMAND))
1317 goto cmdline_not_changed; /* Ignore mouse */
1318# ifdef FEAT_CLIPBOARD
1319 if (mouse_row < cmdline_row && clip_star.available)
1320 {
1321 int button, is_click, is_drag;
1322
1323 /*
1324 * Handle modeless selection.
1325 */
1326 button = get_mouse_button(KEY2TERMCAP1(c),
1327 &is_click, &is_drag);
1328 if (mouse_model_popup() && button == MOUSE_LEFT
1329 && (mod_mask & MOD_MASK_SHIFT))
1330 {
1331 /* Translate shift-left to right button. */
1332 button = MOUSE_RIGHT;
1333 mod_mask &= ~MOD_MASK_SHIFT;
1334 }
1335 clip_modeless(button, is_click, is_drag);
1336 goto cmdline_not_changed;
1337 }
1338# endif
1339
1340 set_cmdspos();
1341 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1342 ++ccline.cmdpos)
1343 {
1344 i = cmdline_charsize(ccline.cmdpos);
1345 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1346 && mouse_col < ccline.cmdspos % Columns + i)
1347 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001348# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (has_mbyte)
1350 {
1351 /* Count ">" for double-wide char that doesn't fit. */
1352 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001353 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 + ccline.cmdpos) - 1;
1355 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 ccline.cmdspos += i;
1358 }
1359 goto cmdline_not_changed;
1360
1361 /* Mouse scroll wheel: ignored here */
1362 case K_MOUSEDOWN:
1363 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001364 case K_MOUSELEFT:
1365 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 /* Alternate buttons ignored here */
1367 case K_X1MOUSE:
1368 case K_X1DRAG:
1369 case K_X1RELEASE:
1370 case K_X2MOUSE:
1371 case K_X2DRAG:
1372 case K_X2RELEASE:
1373 goto cmdline_not_changed;
1374
1375#endif /* FEAT_MOUSE */
1376
1377#ifdef FEAT_GUI
1378 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1379 case K_LEFTRELEASE_NM:
1380 goto cmdline_not_changed;
1381
1382 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001383 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
1385 gui_do_scroll();
1386 redrawcmd();
1387 }
1388 goto cmdline_not_changed;
1389
1390 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001391 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001393 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 redrawcmd();
1395 }
1396 goto cmdline_not_changed;
1397#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001398#ifdef FEAT_GUI_TABLINE
1399 case K_TABLINE:
1400 case K_TABMENU:
1401 /* Don't want to change any tabs here. Make sure the same tab
1402 * is still selected. */
1403 if (gui_use_tabline())
1404 gui_mch_set_curtab(tabpage_index(curtab));
1405 goto cmdline_not_changed;
1406#endif
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 case K_SELECT: /* end of Select mode mapping - ignore */
1409 goto cmdline_not_changed;
1410
1411 case Ctrl_B: /* begin of command line */
1412 case K_HOME:
1413 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 case K_S_HOME:
1415 case K_C_HOME:
1416 ccline.cmdpos = 0;
1417 set_cmdspos();
1418 goto cmdline_not_changed;
1419
1420 case Ctrl_E: /* end of command line */
1421 case K_END:
1422 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 case K_S_END:
1424 case K_C_END:
1425 ccline.cmdpos = ccline.cmdlen;
1426 set_cmdspos_cursor();
1427 goto cmdline_not_changed;
1428
1429 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001430 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 break;
1432 goto cmdline_changed;
1433
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001434 case Ctrl_L:
1435#ifdef FEAT_SEARCH_EXTRA
1436 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1437 {
1438 /* Add a character from under the cursor for 'incsearch' */
1439 if (did_incsearch
1440 && !equalpos(curwin->w_cursor, old_cursor))
1441 {
1442 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001443 /* If 'ignorecase' and 'smartcase' are set and the
1444 * command line has no uppercase characters, convert
1445 * the character to lowercase */
1446 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1447 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001448 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001449 {
1450 if (c == firstc || vim_strchr((char_u *)(
1451 p_magic ? "\\^$.*[" : "\\^$"), c)
1452 != NULL)
1453 {
1454 /* put a backslash before special characters */
1455 stuffcharReadbuff(c);
1456 c = '\\';
1457 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001458 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001459 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001460 }
1461 goto cmdline_not_changed;
1462 }
1463#endif
1464
1465 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001466 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 break;
1468 goto cmdline_changed;
1469
1470 case Ctrl_N: /* next match */
1471 case Ctrl_P: /* previous match */
1472 if (xpc.xp_numfiles > 0)
1473 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001474 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1475 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 break;
1477 goto cmdline_changed;
1478 }
1479
1480#ifdef FEAT_CMDHIST
1481 case K_UP:
1482 case K_DOWN:
1483 case K_S_UP:
1484 case K_S_DOWN:
1485 case K_PAGEUP:
1486 case K_KPAGEUP:
1487 case K_PAGEDOWN:
1488 case K_KPAGEDOWN:
1489 if (hislen == 0 || firstc == NUL) /* no history */
1490 goto cmdline_not_changed;
1491
1492 i = hiscnt;
1493
1494 /* save current command string so it can be restored later */
1495 if (lookfor == NULL)
1496 {
1497 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1498 goto cmdline_not_changed;
1499 lookfor[ccline.cmdpos] = NUL;
1500 }
1501
1502 j = (int)STRLEN(lookfor);
1503 for (;;)
1504 {
1505 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001506 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001507 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 {
1509 if (hiscnt == hislen) /* first time */
1510 hiscnt = hisidx[histype];
1511 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1512 hiscnt = hislen - 1;
1513 else if (hiscnt != hisidx[histype] + 1)
1514 --hiscnt;
1515 else /* at top of list */
1516 {
1517 hiscnt = i;
1518 break;
1519 }
1520 }
1521 else /* one step forwards */
1522 {
1523 /* on last entry, clear the line */
1524 if (hiscnt == hisidx[histype])
1525 {
1526 hiscnt = hislen;
1527 break;
1528 }
1529
1530 /* not on a history line, nothing to do */
1531 if (hiscnt == hislen)
1532 break;
1533 if (hiscnt == hislen - 1) /* wrap around */
1534 hiscnt = 0;
1535 else
1536 ++hiscnt;
1537 }
1538 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1539 {
1540 hiscnt = i;
1541 break;
1542 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001543 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001544 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 || STRNCMP(history[histype][hiscnt].hisstr,
1546 lookfor, (size_t)j) == 0)
1547 break;
1548 }
1549
1550 if (hiscnt != i) /* jumped to other entry */
1551 {
1552 char_u *p;
1553 int len;
1554 int old_firstc;
1555
1556 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001557 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 if (hiscnt == hislen)
1559 p = lookfor; /* back to the old one */
1560 else
1561 p = history[histype][hiscnt].hisstr;
1562
1563 if (histype == HIST_SEARCH
1564 && p != lookfor
1565 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1566 {
1567 /* Correct for the separator character used when
1568 * adding the history entry vs the one used now.
1569 * First loop: count length.
1570 * Second loop: copy the characters. */
1571 for (i = 0; i <= 1; ++i)
1572 {
1573 len = 0;
1574 for (j = 0; p[j] != NUL; ++j)
1575 {
1576 /* Replace old sep with new sep, unless it is
1577 * escaped. */
1578 if (p[j] == old_firstc
1579 && (j == 0 || p[j - 1] != '\\'))
1580 {
1581 if (i > 0)
1582 ccline.cmdbuff[len] = firstc;
1583 }
1584 else
1585 {
1586 /* Escape new sep, unless it is already
1587 * escaped. */
1588 if (p[j] == firstc
1589 && (j == 0 || p[j - 1] != '\\'))
1590 {
1591 if (i > 0)
1592 ccline.cmdbuff[len] = '\\';
1593 ++len;
1594 }
1595 if (i > 0)
1596 ccline.cmdbuff[len] = p[j];
1597 }
1598 ++len;
1599 }
1600 if (i == 0)
1601 {
1602 alloc_cmdbuff(len);
1603 if (ccline.cmdbuff == NULL)
1604 goto returncmd;
1605 }
1606 }
1607 ccline.cmdbuff[len] = NUL;
1608 }
1609 else
1610 {
1611 alloc_cmdbuff((int)STRLEN(p));
1612 if (ccline.cmdbuff == NULL)
1613 goto returncmd;
1614 STRCPY(ccline.cmdbuff, p);
1615 }
1616
1617 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1618 redrawcmd();
1619 goto cmdline_changed;
1620 }
1621 beep_flush();
1622 goto cmdline_not_changed;
1623#endif
1624
1625 case Ctrl_V:
1626 case Ctrl_Q:
1627#ifdef FEAT_MOUSE
1628 ignore_drag_release = TRUE;
1629#endif
1630 putcmdline('^', TRUE);
1631 c = get_literal(); /* get next (two) character(s) */
1632 do_abbr = FALSE; /* don't do abbreviation now */
1633#ifdef FEAT_MBYTE
1634 /* may need to remove ^ when composing char was typed */
1635 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1636 {
1637 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1638 msg_putchar(' ');
1639 cursorcmd();
1640 }
1641#endif
1642 break;
1643
1644#ifdef FEAT_DIGRAPHS
1645 case Ctrl_K:
1646#ifdef FEAT_MOUSE
1647 ignore_drag_release = TRUE;
1648#endif
1649 putcmdline('?', TRUE);
1650#ifdef USE_ON_FLY_SCROLL
1651 dont_scroll = TRUE; /* disallow scrolling here */
1652#endif
1653 c = get_digraph(TRUE);
1654 if (c != NUL)
1655 break;
1656
1657 redrawcmd();
1658 goto cmdline_not_changed;
1659#endif /* FEAT_DIGRAPHS */
1660
1661#ifdef FEAT_RIGHTLEFT
1662 case Ctrl__: /* CTRL-_: switch language mode */
1663 if (!p_ari)
1664 break;
1665#ifdef FEAT_FKMAP
1666 if (p_altkeymap)
1667 {
1668 cmd_fkmap = !cmd_fkmap;
1669 if (cmd_fkmap) /* in Farsi always in Insert mode */
1670 ccline.overstrike = FALSE;
1671 }
1672 else /* Hebrew is default */
1673#endif
1674 cmd_hkmap = !cmd_hkmap;
1675 goto cmdline_not_changed;
1676#endif
1677
1678 default:
1679#ifdef UNIX
1680 if (c == intr_char)
1681 {
1682 gotesc = TRUE; /* will free ccline.cmdbuff after
1683 putting it in history */
1684 goto returncmd; /* back to Normal mode */
1685 }
1686#endif
1687 /*
1688 * Normal character with no special meaning. Just set mod_mask
1689 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1690 * the string <S-Space>. This should only happen after ^V.
1691 */
1692 if (!IS_SPECIAL(c))
1693 mod_mask = 0x0;
1694 break;
1695 }
1696 /*
1697 * End of switch on command line character.
1698 * We come here if we have a normal character.
1699 */
1700
1701 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1702#ifdef FEAT_MBYTE
1703 /* Add ABBR_OFF for characters above 0x100, this is
1704 * what check_abbr() expects. */
1705 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1706#endif
1707 c))
1708 goto cmdline_changed;
1709
1710 /*
1711 * put the character in the command line
1712 */
1713 if (IS_SPECIAL(c) || mod_mask != 0)
1714 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1715 else
1716 {
1717#ifdef FEAT_MBYTE
1718 if (has_mbyte)
1719 {
1720 j = (*mb_char2bytes)(c, IObuff);
1721 IObuff[j] = NUL; /* exclude composing chars */
1722 put_on_cmdline(IObuff, j, TRUE);
1723 }
1724 else
1725#endif
1726 {
1727 IObuff[0] = c;
1728 put_on_cmdline(IObuff, 1, TRUE);
1729 }
1730 }
1731 goto cmdline_changed;
1732
1733/*
1734 * This part implements incremental searches for "/" and "?"
1735 * Jump to cmdline_not_changed when a character has been read but the command
1736 * line did not change. Then we only search and redraw if something changed in
1737 * the past.
1738 * Jump to cmdline_changed when the command line did change.
1739 * (Sorry for the goto's, I know it is ugly).
1740 */
1741cmdline_not_changed:
1742#ifdef FEAT_SEARCH_EXTRA
1743 if (!incsearch_postponed)
1744 continue;
1745#endif
1746
1747cmdline_changed:
1748#ifdef FEAT_SEARCH_EXTRA
1749 /*
1750 * 'incsearch' highlighting.
1751 */
1752 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1753 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001754 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001755#ifdef FEAT_RELTIME
1756 proftime_T tm;
1757#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001758
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 /* if there is a character waiting, search and redraw later */
1760 if (char_avail())
1761 {
1762 incsearch_postponed = TRUE;
1763 continue;
1764 }
1765 incsearch_postponed = FALSE;
1766 curwin->w_cursor = old_cursor; /* start at old position */
1767
1768 /* If there is no command line, don't do anything */
1769 if (ccline.cmdlen == 0)
1770 i = 0;
1771 else
1772 {
1773 cursor_off(); /* so the user knows we're busy */
1774 out_flush();
1775 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001776#ifdef FEAT_RELTIME
1777 /* Set the time limit to half a second. */
1778 profile_setlimit(500L, &tm);
1779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001781 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1782#ifdef FEAT_RELTIME
1783 &tm
1784#else
1785 NULL
1786#endif
1787 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 --emsg_off;
1789 /* if interrupted while searching, behave like it failed */
1790 if (got_int)
1791 {
1792 (void)vpeekc(); /* remove <C-C> from input stream */
1793 got_int = FALSE; /* don't abandon the command line */
1794 i = 0;
1795 }
1796 else if (char_avail())
1797 /* cancelled searching because a char was typed */
1798 incsearch_postponed = TRUE;
1799 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001800 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 highlight_match = TRUE; /* highlight position */
1802 else
1803 highlight_match = FALSE; /* remove highlight */
1804
1805 /* first restore the old curwin values, so the screen is
1806 * positioned in the same way as the actual search command */
1807 curwin->w_leftcol = old_leftcol;
1808 curwin->w_topline = old_topline;
1809# ifdef FEAT_DIFF
1810 curwin->w_topfill = old_topfill;
1811# endif
1812 curwin->w_botline = old_botline;
1813 changed_cline_bef_curs();
1814 update_topline();
1815
1816 if (i != 0)
1817 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001818 pos_T save_pos = curwin->w_cursor;
1819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001821 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 * moves the whole match onto the screen when 'nowrap' is set.
1823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 curwin->w_cursor.lnum += search_match_lines;
1825 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001826 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1827 {
1828 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1829 coladvance((colnr_T)MAXCOL);
1830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001832 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001833 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001835 else
1836 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001839# ifdef FEAT_WINDOWS
1840 /* May redraw the status line to show the cursor position. */
1841 if (p_ru && curwin->w_status_height > 0)
1842 curwin->w_redr_status = TRUE;
1843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001845 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001846 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001847 restore_cmdline(&save_ccline);
1848
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001849 /* Leave it at the end to make CTRL-R CTRL-W work. */
1850 if (i != 0)
1851 curwin->w_cursor = end_pos;
1852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 msg_starthere();
1854 redrawcmdline();
1855 did_incsearch = TRUE;
1856 }
1857#else /* FEAT_SEARCH_EXTRA */
1858 ;
1859#endif
1860
1861#ifdef FEAT_RIGHTLEFT
1862 if (cmdmsg_rl
1863# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001864 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865# endif
1866 )
1867 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001868 * right-left typing. Not efficient, but it works.
1869 * Do it only when there are no characters left to read
1870 * to avoid useless intermediate redraws. */
1871 if (vpeekc() == NUL)
1872 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873#endif
1874 }
1875
1876returncmd:
1877
1878#ifdef FEAT_RIGHTLEFT
1879 cmdmsg_rl = FALSE;
1880#endif
1881
1882#ifdef FEAT_FKMAP
1883 cmd_fkmap = 0;
1884#endif
1885
1886 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001887 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889#ifdef FEAT_SEARCH_EXTRA
1890 if (did_incsearch)
1891 {
1892 curwin->w_cursor = old_cursor;
1893 curwin->w_curswant = old_curswant;
1894 curwin->w_leftcol = old_leftcol;
1895 curwin->w_topline = old_topline;
1896# ifdef FEAT_DIFF
1897 curwin->w_topfill = old_topfill;
1898# endif
1899 curwin->w_botline = old_botline;
1900 highlight_match = FALSE;
1901 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001902 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
1904#endif
1905
1906 if (ccline.cmdbuff != NULL)
1907 {
1908 /*
1909 * Put line in history buffer (":" and "=" only when it was typed).
1910 */
1911#ifdef FEAT_CMDHIST
1912 if (ccline.cmdlen && firstc != NUL
1913 && (some_key_typed || histype == HIST_SEARCH))
1914 {
1915 add_to_history(histype, ccline.cmdbuff, TRUE,
1916 histype == HIST_SEARCH ? firstc : NUL);
1917 if (firstc == ':')
1918 {
1919 vim_free(new_last_cmdline);
1920 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1921 }
1922 }
1923#endif
1924
1925 if (gotesc) /* abandon command line */
1926 {
1927 vim_free(ccline.cmdbuff);
1928 ccline.cmdbuff = NULL;
1929 if (msg_scrolled == 0)
1930 compute_cmdrow();
1931 MSG("");
1932 redraw_cmdline = TRUE;
1933 }
1934 }
1935
1936 /*
1937 * If the screen was shifted up, redraw the whole screen (later).
1938 * If the line is too long, clear it, so ruler and shown command do
1939 * not get printed in the middle of it.
1940 */
1941 msg_check();
1942 msg_scroll = save_msg_scroll;
1943 redir_off = FALSE;
1944
1945 /* When the command line was typed, no need for a wait-return prompt. */
1946 if (some_key_typed)
1947 need_wait_return = FALSE;
1948
1949 State = save_State;
1950#ifdef USE_IM_CONTROL
1951 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1952 im_save_status(b_im_ptr);
1953 im_set_active(FALSE);
1954#endif
1955#ifdef FEAT_MOUSE
1956 setmouse();
1957#endif
1958#ifdef CURSOR_SHAPE
1959 ui_cursor_shape(); /* may show different cursor shape */
1960#endif
1961
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001962 {
1963 char_u *p = ccline.cmdbuff;
1964
1965 /* Make ccline empty, getcmdline() may try to use it. */
1966 ccline.cmdbuff = NULL;
1967 return p;
1968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969}
1970
1971#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1972/*
1973 * Get a command line with a prompt.
1974 * This is prepared to be called recursively from getcmdline() (e.g. by
1975 * f_input() when evaluating an expression from CTRL-R =).
1976 * Returns the command line in allocated memory, or NULL.
1977 */
1978 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001979getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 int firstc;
1981 char_u *prompt; /* command line prompt */
1982 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001983 int xp_context; /* type of expansion */
1984 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985{
1986 char_u *s;
1987 struct cmdline_info save_ccline;
1988 int msg_col_save = msg_col;
1989
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001990 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 ccline.cmdprompt = prompt;
1992 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001993# ifdef FEAT_EVAL
1994 ccline.xp_context = xp_context;
1995 ccline.xp_arg = xp_arg;
1996 ccline.input_fn = (firstc == '@');
1997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001999 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002000 /* Restore msg_col, the prompt from input() may have changed it.
2001 * But only if called recursively and the commandline is therefore being
2002 * restored to an old one; if not, the input() prompt stays on the screen,
2003 * so we need its modified msg_col left intact. */
2004 if (ccline.cmdbuff != NULL)
2005 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006
2007 return s;
2008}
2009#endif
2010
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002011/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002012 * Return TRUE when the text must not be changed and we can't switch to
2013 * another window or buffer. Used when editing the command line, evaluating
2014 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002015 */
2016 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002017text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002018{
2019#ifdef FEAT_CMDWIN
2020 if (cmdwin_type != 0)
2021 return TRUE;
2022#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002023 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002024}
2025
2026/*
2027 * Give an error message for a command that isn't allowed while the cmdline
2028 * window is open or editing the cmdline in another way.
2029 */
2030 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002031text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002032{
2033#ifdef FEAT_CMDWIN
2034 if (cmdwin_type != 0)
2035 EMSG(_(e_cmdwin));
2036 else
2037#endif
2038 EMSG(_(e_secure));
2039}
2040
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002041#if defined(FEAT_AUTOCMD) || defined(PROTO)
2042/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002043 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2044 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002045 */
2046 int
2047curbuf_locked()
2048{
2049 if (curbuf_lock > 0)
2050 {
2051 EMSG(_("E788: Not allowed to edit another buffer now"));
2052 return TRUE;
2053 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002054 return allbuf_locked();
2055}
2056
2057/*
2058 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2059 * message.
2060 */
2061 int
2062allbuf_locked()
2063{
2064 if (allbuf_lock > 0)
2065 {
2066 EMSG(_("E811: Not allowed to change buffer information now"));
2067 return TRUE;
2068 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002069 return FALSE;
2070}
2071#endif
2072
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 static int
2074cmdline_charsize(idx)
2075 int idx;
2076{
2077#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2078 if (cmdline_star > 0) /* showing '*', always 1 position */
2079 return 1;
2080#endif
2081 return ptr2cells(ccline.cmdbuff + idx);
2082}
2083
2084/*
2085 * Compute the offset of the cursor on the command line for the prompt and
2086 * indent.
2087 */
2088 static void
2089set_cmdspos()
2090{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002091 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 ccline.cmdspos = 1 + ccline.cmdindent;
2093 else
2094 ccline.cmdspos = 0 + ccline.cmdindent;
2095}
2096
2097/*
2098 * Compute the screen position for the cursor on the command line.
2099 */
2100 static void
2101set_cmdspos_cursor()
2102{
2103 int i, m, c;
2104
2105 set_cmdspos();
2106 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002109 if (m < 0) /* overflow, Columns or Rows at weird value */
2110 m = MAXCOL;
2111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 else
2113 m = MAXCOL;
2114 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2115 {
2116 c = cmdline_charsize(i);
2117#ifdef FEAT_MBYTE
2118 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2119 if (has_mbyte)
2120 correct_cmdspos(i, c);
2121#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002122 /* If the cmdline doesn't fit, show cursor on last visible char.
2123 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 if ((ccline.cmdspos += c) >= m)
2125 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 ccline.cmdspos -= c;
2127 break;
2128 }
2129#ifdef FEAT_MBYTE
2130 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002131 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#endif
2133 }
2134}
2135
2136#ifdef FEAT_MBYTE
2137/*
2138 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2139 * character that doesn't fit, so that a ">" must be displayed.
2140 */
2141 static void
2142correct_cmdspos(idx, cells)
2143 int idx;
2144 int cells;
2145{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002146 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2148 && ccline.cmdspos % Columns + cells > Columns)
2149 ccline.cmdspos++;
2150}
2151#endif
2152
2153/*
2154 * Get an Ex command line for the ":" command.
2155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002157getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002159 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 int indent; /* indent for inside conditionals */
2161{
2162 /* When executing a register, remove ':' that's in front of each line. */
2163 if (exec_from_reg && vpeekc() == ':')
2164 (void)vgetc();
2165 return getcmdline(c, 1L, indent);
2166}
2167
2168/*
2169 * Get an Ex command line for Ex mode.
2170 * In Ex mode we only use the OS supplied line editing features and no
2171 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002172 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002175getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002176 int promptc; /* normally ':', NUL for ":append" and '?' for
2177 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002178 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 int indent; /* indent for inside conditionals */
2180{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002181 garray_T line_ga;
2182 char_u *pend;
2183 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002184 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 int escaped = FALSE; /* CTRL-V typed */
2186 int vcol = 0;
2187 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002188 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189
2190 /* Switch cursor on now. This avoids that it happens after the "\n", which
2191 * confuses the system function that computes tabstops. */
2192 cursor_on();
2193
2194 /* always start in column 0; write a newline if necessary */
2195 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002196 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002198 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002200 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002201 if (p_prompt)
2202 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 while (indent-- > 0)
2204 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207
2208 ga_init2(&line_ga, 1, 30);
2209
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002210 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002212 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002213 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002214 while (indent >= 8)
2215 {
2216 ga_append(&line_ga, TAB);
2217 msg_puts((char_u *)" ");
2218 indent -= 8;
2219 }
2220 while (indent-- > 0)
2221 {
2222 ga_append(&line_ga, ' ');
2223 msg_putchar(' ');
2224 }
2225 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002226 ++no_mapping;
2227 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002228
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 /*
2230 * Get the line, one character at a time.
2231 */
2232 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002233 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
2235 if (ga_grow(&line_ga, 40) == FAIL)
2236 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002238 /* Get one character at a time. Don't use inchar(), it can't handle
2239 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002240 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002241 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242
2243 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002244 * Handle line editing.
2245 * Previously this was left to the system, putting the terminal in
2246 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002248 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002250 msg_putchar('\n');
2251 break;
2252 }
2253
2254 if (!escaped)
2255 {
2256 /* CR typed means "enter", which is NL */
2257 if (c1 == '\r')
2258 c1 = '\n';
2259
2260 if (c1 == BS || c1 == K_BS
2261 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002263 if (line_ga.ga_len > 0)
2264 {
2265 --line_ga.ga_len;
2266 goto redraw;
2267 }
2268 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
2270
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002271 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002273 msg_col = startcol;
2274 msg_clr_eos();
2275 line_ga.ga_len = 0;
2276 continue;
2277 }
2278
2279 if (c1 == Ctrl_T)
2280 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02002281 long sw = get_sw_value();
2282
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002283 p = (char_u *)line_ga.ga_data;
2284 p[line_ga.ga_len] = NUL;
2285 indent = get_indent_str(p, 8);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002286 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002287add_indent:
2288 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002290 char_u *s = skipwhite(p);
2291
2292 ga_grow(&line_ga, 1);
2293 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2294 *s = ' ';
2295 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002297redraw:
2298 /* redraw the line */
2299 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002300 vcol = 0;
2301 for (p = (char_u *)line_ga.ga_data;
2302 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002304 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002308 msg_putchar(' ');
2309 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002313 msg_outtrans_len(p, 1);
2314 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002317 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002318 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002319 continue;
2320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002322 if (c1 == Ctrl_D)
2323 {
2324 /* Delete one shiftwidth. */
2325 p = (char_u *)line_ga.ga_data;
2326 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002328 if (prev_char == '^')
2329 ex_keep_indent = TRUE;
2330 indent = 0;
2331 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333 else
2334 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002335 p[line_ga.ga_len] = NUL;
2336 indent = get_indent_str(p, 8);
2337 --indent;
Bram Moolenaar14f24742012-08-08 18:01:05 +02002338 indent -= indent % get_sw_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002340 while (get_indent_str(p, 8) > indent)
2341 {
2342 char_u *s = skipwhite(p);
2343
2344 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2345 --line_ga.ga_len;
2346 }
2347 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002349
2350 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2351 {
2352 escaped = TRUE;
2353 continue;
2354 }
2355
2356 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2357 if (IS_SPECIAL(c1))
2358 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002360
2361 if (IS_SPECIAL(c1))
2362 c1 = '?';
2363 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002364 if (c1 == '\n')
2365 msg_putchar('\n');
2366 else if (c1 == TAB)
2367 {
2368 /* Don't use chartabsize(), 'ts' can be different */
2369 do
2370 {
2371 msg_putchar(' ');
2372 } while (++vcol % 8);
2373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002376 msg_outtrans_len(
2377 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2378 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002380 ++line_ga.ga_len;
2381 escaped = FALSE;
2382
2383 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002384 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002385
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002386 /* We are done when a NL is entered, but not when it comes after an
2387 * odd number of backslashes, that results in a NUL. */
2388 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002390 int bcount = 0;
2391
2392 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2393 ++bcount;
2394
2395 if (bcount > 0)
2396 {
2397 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2398 * "\NL", etc. */
2399 line_ga.ga_len -= (bcount + 1) / 2;
2400 pend -= (bcount + 1) / 2;
2401 pend[-1] = '\n';
2402 }
2403
2404 if ((bcount & 1) == 0)
2405 {
2406 --line_ga.ga_len;
2407 --pend;
2408 *pend = NUL;
2409 break;
2410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412 }
2413
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002414 --no_mapping;
2415 --allow_keys;
2416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 /* make following messages go to the next line */
2418 msg_didout = FALSE;
2419 msg_col = 0;
2420 if (msg_row < Rows - 1)
2421 ++msg_row;
2422 emsg_on_display = FALSE; /* don't want ui_delay() */
2423
2424 if (got_int)
2425 ga_clear(&line_ga);
2426
2427 return (char_u *)line_ga.ga_data;
2428}
2429
Bram Moolenaare344bea2005-09-01 20:46:49 +00002430# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2431 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432/*
2433 * Return TRUE if ccline.overstrike is on.
2434 */
2435 int
2436cmdline_overstrike()
2437{
2438 return ccline.overstrike;
2439}
2440
2441/*
2442 * Return TRUE if the cursor is at the end of the cmdline.
2443 */
2444 int
2445cmdline_at_end()
2446{
2447 return (ccline.cmdpos >= ccline.cmdlen);
2448}
2449#endif
2450
Bram Moolenaar9372a112005-12-06 19:59:18 +00002451#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452/*
2453 * Return the virtual column number at the current cursor position.
2454 * This is used by the IM code to obtain the start of the preedit string.
2455 */
2456 colnr_T
2457cmdline_getvcol_cursor()
2458{
2459 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2460 return MAXCOL;
2461
2462# ifdef FEAT_MBYTE
2463 if (has_mbyte)
2464 {
2465 colnr_T col;
2466 int i = 0;
2467
2468 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002469 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
2471 return col;
2472 }
2473 else
2474# endif
2475 return ccline.cmdpos;
2476}
2477#endif
2478
2479#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2480/*
2481 * If part of the command line is an IM preedit string, redraw it with
2482 * IM feedback attributes. The cursor position is restored after drawing.
2483 */
2484 static void
2485redrawcmd_preedit()
2486{
2487 if ((State & CMDLINE)
2488 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002489 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 && !p_imdisable
2491 && im_is_preediting())
2492 {
2493 int cmdpos = 0;
2494 int cmdspos;
2495 int old_row;
2496 int old_col;
2497 colnr_T col;
2498
2499 old_row = msg_row;
2500 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002501 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503# ifdef FEAT_MBYTE
2504 if (has_mbyte)
2505 {
2506 for (col = 0; col < preedit_start_col
2507 && cmdpos < ccline.cmdlen; ++col)
2508 {
2509 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002510 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 }
2512 }
2513 else
2514# endif
2515 {
2516 cmdspos += preedit_start_col;
2517 cmdpos += preedit_start_col;
2518 }
2519
2520 msg_row = cmdline_row + (cmdspos / (int)Columns);
2521 msg_col = cmdspos % (int)Columns;
2522 if (msg_row >= Rows)
2523 msg_row = Rows - 1;
2524
2525 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2526 {
2527 int char_len;
2528 int char_attr;
2529
2530 char_attr = im_get_feedback_attr(col);
2531 if (char_attr < 0)
2532 break; /* end of preedit string */
2533
2534# ifdef FEAT_MBYTE
2535 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002536 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 else
2538# endif
2539 char_len = 1;
2540
2541 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2542 cmdpos += char_len;
2543 }
2544
2545 msg_row = old_row;
2546 msg_col = old_col;
2547 }
2548}
2549#endif /* FEAT_XIM && FEAT_GUI_GTK */
2550
2551/*
2552 * Allocate a new command line buffer.
2553 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2554 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2555 */
2556 static void
2557alloc_cmdbuff(len)
2558 int len;
2559{
2560 /*
2561 * give some extra space to avoid having to allocate all the time
2562 */
2563 if (len < 80)
2564 len = 100;
2565 else
2566 len += 20;
2567
2568 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2569 ccline.cmdbufflen = len;
2570}
2571
2572/*
2573 * Re-allocate the command line to length len + something extra.
2574 * return FAIL for failure, OK otherwise
2575 */
2576 static int
2577realloc_cmdbuff(len)
2578 int len;
2579{
2580 char_u *p;
2581
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002582 if (len < ccline.cmdbufflen)
2583 return OK; /* no need to resize */
2584
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 p = ccline.cmdbuff;
2586 alloc_cmdbuff(len); /* will get some more */
2587 if (ccline.cmdbuff == NULL) /* out of memory */
2588 {
2589 ccline.cmdbuff = p; /* keep the old one */
2590 return FAIL;
2591 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002592 /* There isn't always a NUL after the command, but it may need to be
2593 * there, thus copy up to the NUL and add a NUL. */
2594 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2595 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002597
2598 if (ccline.xpc != NULL
2599 && ccline.xpc->xp_pattern != NULL
2600 && ccline.xpc->xp_context != EXPAND_NOTHING
2601 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2602 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002603 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002604
2605 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2606 * to point into the newly allocated memory. */
2607 if (i >= 0 && i <= ccline.cmdlen)
2608 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2609 }
2610
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 return OK;
2612}
2613
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002614#if defined(FEAT_ARABIC) || defined(PROTO)
2615static char_u *arshape_buf = NULL;
2616
2617# if defined(EXITFREE) || defined(PROTO)
2618 void
2619free_cmdline_buf()
2620{
2621 vim_free(arshape_buf);
2622}
2623# endif
2624#endif
2625
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626/*
2627 * Draw part of the cmdline at the current cursor position. But draw stars
2628 * when cmdline_star is TRUE.
2629 */
2630 static void
2631draw_cmdline(start, len)
2632 int start;
2633 int len;
2634{
2635#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2636 int i;
2637
2638 if (cmdline_star > 0)
2639 for (i = 0; i < len; ++i)
2640 {
2641 msg_putchar('*');
2642# ifdef FEAT_MBYTE
2643 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002644 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645# endif
2646 }
2647 else
2648#endif
2649#ifdef FEAT_ARABIC
2650 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 static int buflen = 0;
2653 char_u *p;
2654 int j;
2655 int newlen = 0;
2656 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002657 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 int prev_c = 0;
2659 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002660 int u8c;
2661 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663
2664 /*
2665 * Do arabic shaping into a temporary buffer. This is very
2666 * inefficient!
2667 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002668 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 /* Re-allocate the buffer. We keep it around to avoid a lot of
2671 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002672 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002673 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002674 arshape_buf = alloc(buflen);
2675 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 return; /* out of memory */
2677 }
2678
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002679 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2680 {
2681 /* Prepend a space to draw the leading composing char on. */
2682 arshape_buf[0] = ' ';
2683 newlen = 1;
2684 }
2685
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 for (j = start; j < start + len; j += mb_l)
2687 {
2688 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002689 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002690 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 if (ARABIC_CHAR(u8c))
2692 {
2693 /* Do Arabic shaping. */
2694 if (cmdmsg_rl)
2695 {
2696 /* displaying from right to left */
2697 pc = prev_c;
2698 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002699 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 if (j + mb_l >= start + len)
2701 nc = NUL;
2702 else
2703 nc = utf_ptr2char(p + mb_l);
2704 }
2705 else
2706 {
2707 /* displaying from left to right */
2708 if (j + mb_l >= start + len)
2709 pc = NUL;
2710 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002711 {
2712 int pcc[MAX_MCO];
2713
2714 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002716 pc1 = pcc[0];
2717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 nc = prev_c;
2719 }
2720 prev_c = u8c;
2721
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002722 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002724 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002725 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002727 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2728 if (u8cc[1] != 0)
2729 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002730 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 }
2732 }
2733 else
2734 {
2735 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002736 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 newlen += mb_l;
2738 }
2739 }
2740
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002741 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743 else
2744#endif
2745 msg_outtrans_len(ccline.cmdbuff + start, len);
2746}
2747
2748/*
2749 * Put a character on the command line. Shifts the following text to the
2750 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2751 * "c" must be printable (fit in one display cell)!
2752 */
2753 void
2754putcmdline(c, shift)
2755 int c;
2756 int shift;
2757{
2758 if (cmd_silent)
2759 return;
2760 msg_no_more = TRUE;
2761 msg_putchar(c);
2762 if (shift)
2763 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2764 msg_no_more = FALSE;
2765 cursorcmd();
2766}
2767
2768/*
2769 * Undo a putcmdline(c, FALSE).
2770 */
2771 void
2772unputcmdline()
2773{
2774 if (cmd_silent)
2775 return;
2776 msg_no_more = TRUE;
2777 if (ccline.cmdlen == ccline.cmdpos)
2778 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002779#ifdef FEAT_MBYTE
2780 else if (has_mbyte)
2781 draw_cmdline(ccline.cmdpos,
2782 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 else
2785 draw_cmdline(ccline.cmdpos, 1);
2786 msg_no_more = FALSE;
2787 cursorcmd();
2788}
2789
2790/*
2791 * Put the given string, of the given length, onto the command line.
2792 * If len is -1, then STRLEN() is used to calculate the length.
2793 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2794 * part will be redrawn, otherwise it will not. If this function is called
2795 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2796 * called afterwards.
2797 */
2798 int
2799put_on_cmdline(str, len, redraw)
2800 char_u *str;
2801 int len;
2802 int redraw;
2803{
2804 int retval;
2805 int i;
2806 int m;
2807 int c;
2808
2809 if (len < 0)
2810 len = (int)STRLEN(str);
2811
2812 /* Check if ccline.cmdbuff needs to be longer */
2813 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002814 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 else
2816 retval = OK;
2817 if (retval == OK)
2818 {
2819 if (!ccline.overstrike)
2820 {
2821 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2822 ccline.cmdbuff + ccline.cmdpos,
2823 (size_t)(ccline.cmdlen - ccline.cmdpos));
2824 ccline.cmdlen += len;
2825 }
2826 else
2827 {
2828#ifdef FEAT_MBYTE
2829 if (has_mbyte)
2830 {
2831 /* Count nr of characters in the new string. */
2832 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002833 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 ++m;
2835 /* Count nr of bytes in cmdline that are overwritten by these
2836 * characters. */
2837 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002838 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 --m;
2840 if (i < ccline.cmdlen)
2841 {
2842 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2843 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2844 ccline.cmdlen += ccline.cmdpos + len - i;
2845 }
2846 else
2847 ccline.cmdlen = ccline.cmdpos + len;
2848 }
2849 else
2850#endif
2851 if (ccline.cmdpos + len > ccline.cmdlen)
2852 ccline.cmdlen = ccline.cmdpos + len;
2853 }
2854 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2855 ccline.cmdbuff[ccline.cmdlen] = NUL;
2856
2857#ifdef FEAT_MBYTE
2858 if (enc_utf8)
2859 {
2860 /* When the inserted text starts with a composing character,
2861 * backup to the character before it. There could be two of them.
2862 */
2863 i = 0;
2864 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2865 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2866 {
2867 i = (*mb_head_off)(ccline.cmdbuff,
2868 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2869 ccline.cmdpos -= i;
2870 len += i;
2871 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2872 }
2873# ifdef FEAT_ARABIC
2874 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2875 {
2876 /* Check the previous character for Arabic combining pair. */
2877 i = (*mb_head_off)(ccline.cmdbuff,
2878 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2879 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2880 + ccline.cmdpos - i), c))
2881 {
2882 ccline.cmdpos -= i;
2883 len += i;
2884 }
2885 else
2886 i = 0;
2887 }
2888# endif
2889 if (i != 0)
2890 {
2891 /* Also backup the cursor position. */
2892 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2893 ccline.cmdspos -= i;
2894 msg_col -= i;
2895 if (msg_col < 0)
2896 {
2897 msg_col += Columns;
2898 --msg_row;
2899 }
2900 }
2901 }
2902#endif
2903
2904 if (redraw && !cmd_silent)
2905 {
2906 msg_no_more = TRUE;
2907 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002908 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2910 /* Avoid clearing the rest of the line too often. */
2911 if (cmdline_row != i || ccline.overstrike)
2912 msg_clr_eos();
2913 msg_no_more = FALSE;
2914 }
2915#ifdef FEAT_FKMAP
2916 /*
2917 * If we are in Farsi command mode, the character input must be in
2918 * Insert mode. So do not advance the cmdpos.
2919 */
2920 if (!cmd_fkmap)
2921#endif
2922 {
2923 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002924 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002926 if (m < 0) /* overflow, Columns or Rows at weird value */
2927 m = MAXCOL;
2928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 else
2930 m = MAXCOL;
2931 for (i = 0; i < len; ++i)
2932 {
2933 c = cmdline_charsize(ccline.cmdpos);
2934#ifdef FEAT_MBYTE
2935 /* count ">" for a double-wide char that doesn't fit. */
2936 if (has_mbyte)
2937 correct_cmdspos(ccline.cmdpos, c);
2938#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002939 /* Stop cursor at the end of the screen, but do increment the
2940 * insert position, so that entering a very long command
2941 * works, even though you can't see it. */
2942 if (ccline.cmdspos + c < m)
2943 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944#ifdef FEAT_MBYTE
2945 if (has_mbyte)
2946 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002947 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 if (c > len - i - 1)
2949 c = len - i - 1;
2950 ccline.cmdpos += c;
2951 i += c;
2952 }
2953#endif
2954 ++ccline.cmdpos;
2955 }
2956 }
2957 }
2958 if (redraw)
2959 msg_check();
2960 return retval;
2961}
2962
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002963static struct cmdline_info prev_ccline;
2964static int prev_ccline_used = FALSE;
2965
2966/*
2967 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2968 * and overwrite it. But get_cmdline_str() may need it, thus make it
2969 * available globally in prev_ccline.
2970 */
2971 static void
2972save_cmdline(ccp)
2973 struct cmdline_info *ccp;
2974{
2975 if (!prev_ccline_used)
2976 {
2977 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2978 prev_ccline_used = TRUE;
2979 }
2980 *ccp = prev_ccline;
2981 prev_ccline = ccline;
2982 ccline.cmdbuff = NULL;
2983 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002984 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002985}
2986
2987/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002988 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002989 */
2990 static void
2991restore_cmdline(ccp)
2992 struct cmdline_info *ccp;
2993{
2994 ccline = prev_ccline;
2995 prev_ccline = *ccp;
2996}
2997
Bram Moolenaar5a305422006-04-28 22:38:25 +00002998#if defined(FEAT_EVAL) || defined(PROTO)
2999/*
3000 * Save the command line into allocated memory. Returns a pointer to be
3001 * passed to restore_cmdline_alloc() later.
3002 * Returns NULL when failed.
3003 */
3004 char_u *
3005save_cmdline_alloc()
3006{
3007 struct cmdline_info *p;
3008
3009 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3010 if (p != NULL)
3011 save_cmdline(p);
3012 return (char_u *)p;
3013}
3014
3015/*
3016 * Restore the command line from the return value of save_cmdline_alloc().
3017 */
3018 void
3019restore_cmdline_alloc(p)
3020 char_u *p;
3021{
3022 if (p != NULL)
3023 {
3024 restore_cmdline((struct cmdline_info *)p);
3025 vim_free(p);
3026 }
3027}
3028#endif
3029
Bram Moolenaar8299df92004-07-10 09:47:34 +00003030/*
3031 * paste a yank register into the command line.
3032 * used by CTRL-R command in command-line mode
3033 * insert_reg() can't be used here, because special characters from the
3034 * register contents will be interpreted as commands.
3035 *
3036 * return FAIL for failure, OK otherwise
3037 */
3038 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003039cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003040 int regname;
3041 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003042 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003043{
3044 long i;
3045 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003046 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003047 int allocated;
3048 struct cmdline_info save_ccline;
3049
3050 /* check for valid regname; also accept special characters for CTRL-R in
3051 * the command line */
3052 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3053 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3054 return FAIL;
3055
3056 /* A register containing CTRL-R can cause an endless loop. Allow using
3057 * CTRL-C to break the loop. */
3058 line_breakcheck();
3059 if (got_int)
3060 return FAIL;
3061
3062#ifdef FEAT_CLIPBOARD
3063 regname = may_get_selection(regname);
3064#endif
3065
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003066 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003067 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003068 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003069 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003070 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003071 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003072 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003073
3074 if (i)
3075 {
3076 /* Got the value of a special register in "arg". */
3077 if (arg == NULL)
3078 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003079
3080 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3081 * part of the word. */
3082 p = arg;
3083 if (p_is && regname == Ctrl_W)
3084 {
3085 char_u *w;
3086 int len;
3087
3088 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003089 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003090 {
3091#ifdef FEAT_MBYTE
3092 if (has_mbyte)
3093 {
3094 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3095 if (!vim_iswordc(mb_ptr2char(w - len)))
3096 break;
3097 w -= len;
3098 }
3099 else
3100#endif
3101 {
3102 if (!vim_iswordc(w[-1]))
3103 break;
3104 --w;
3105 }
3106 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003107 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003108 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3109 p += len;
3110 }
3111
3112 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003113 if (allocated)
3114 vim_free(arg);
3115 return OK;
3116 }
3117
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003118 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003119}
3120
3121/*
3122 * Put a string on the command line.
3123 * When "literally" is TRUE, insert literally.
3124 * When "literally" is FALSE, insert as typed, but don't leave the command
3125 * line.
3126 */
3127 void
3128cmdline_paste_str(s, literally)
3129 char_u *s;
3130 int literally;
3131{
3132 int c, cv;
3133
3134 if (literally)
3135 put_on_cmdline(s, -1, TRUE);
3136 else
3137 while (*s != NUL)
3138 {
3139 cv = *s;
3140 if (cv == Ctrl_V && s[1])
3141 ++s;
3142#ifdef FEAT_MBYTE
3143 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003144 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003145 else
3146#endif
3147 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003148 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3149 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003150#ifdef UNIX
3151 || c == intr_char
3152#endif
3153 || (c == Ctrl_BSL && *s == Ctrl_N))
3154 stuffcharReadbuff(Ctrl_V);
3155 stuffcharReadbuff(c);
3156 }
3157}
3158
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159#ifdef FEAT_WILDMENU
3160/*
3161 * Delete characters on the command line, from "from" to the current
3162 * position.
3163 */
3164 static void
3165cmdline_del(from)
3166 int from;
3167{
3168 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3169 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3170 ccline.cmdlen -= ccline.cmdpos - from;
3171 ccline.cmdpos = from;
3172}
3173#endif
3174
3175/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003176 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 * search
3178 */
3179 void
3180redrawcmdline()
3181{
3182 if (cmd_silent)
3183 return;
3184 need_wait_return = FALSE;
3185 compute_cmdrow();
3186 redrawcmd();
3187 cursorcmd();
3188}
3189
3190 static void
3191redrawcmdprompt()
3192{
3193 int i;
3194
3195 if (cmd_silent)
3196 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003197 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 msg_putchar(ccline.cmdfirstc);
3199 if (ccline.cmdprompt != NULL)
3200 {
3201 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3202 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3203 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003204 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 --ccline.cmdindent;
3206 }
3207 else
3208 for (i = ccline.cmdindent; i > 0; --i)
3209 msg_putchar(' ');
3210}
3211
3212/*
3213 * Redraw what is currently on the command line.
3214 */
3215 void
3216redrawcmd()
3217{
3218 if (cmd_silent)
3219 return;
3220
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003221 /* when 'incsearch' is set there may be no command line while redrawing */
3222 if (ccline.cmdbuff == NULL)
3223 {
3224 windgoto(cmdline_row, 0);
3225 msg_clr_eos();
3226 return;
3227 }
3228
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 msg_start();
3230 redrawcmdprompt();
3231
3232 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3233 msg_no_more = TRUE;
3234 draw_cmdline(0, ccline.cmdlen);
3235 msg_clr_eos();
3236 msg_no_more = FALSE;
3237
3238 set_cmdspos_cursor();
3239
3240 /*
3241 * An emsg() before may have set msg_scroll. This is used in normal mode,
3242 * in cmdline mode we can reset them now.
3243 */
3244 msg_scroll = FALSE; /* next message overwrites cmdline */
3245
3246 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3247 * in cmdline mode */
3248 skip_redraw = FALSE;
3249}
3250
3251 void
3252compute_cmdrow()
3253{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003254 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 cmdline_row = Rows - 1;
3256 else
3257 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3258 + W_STATUS_HEIGHT(lastwin);
3259}
3260
3261 static void
3262cursorcmd()
3263{
3264 if (cmd_silent)
3265 return;
3266
3267#ifdef FEAT_RIGHTLEFT
3268 if (cmdmsg_rl)
3269 {
3270 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3271 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3272 if (msg_row <= 0)
3273 msg_row = Rows - 1;
3274 }
3275 else
3276#endif
3277 {
3278 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3279 msg_col = ccline.cmdspos % (int)Columns;
3280 if (msg_row >= Rows)
3281 msg_row = Rows - 1;
3282 }
3283
3284 windgoto(msg_row, msg_col);
3285#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3286 redrawcmd_preedit();
3287#endif
3288#ifdef MCH_CURSOR_SHAPE
3289 mch_update_cursor();
3290#endif
3291}
3292
3293 void
3294gotocmdline(clr)
3295 int clr;
3296{
3297 msg_start();
3298#ifdef FEAT_RIGHTLEFT
3299 if (cmdmsg_rl)
3300 msg_col = Columns - 1;
3301 else
3302#endif
3303 msg_col = 0; /* always start in column 0 */
3304 if (clr) /* clear the bottom line(s) */
3305 msg_clr_eos(); /* will reset clear_cmdline */
3306 windgoto(cmdline_row, 0);
3307}
3308
3309/*
3310 * Check the word in front of the cursor for an abbreviation.
3311 * Called when the non-id character "c" has been entered.
3312 * When an abbreviation is recognized it is removed from the text with
3313 * backspaces and the replacement string is inserted, followed by "c".
3314 */
3315 static int
3316ccheck_abbr(c)
3317 int c;
3318{
3319 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3320 return FALSE;
3321
3322 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3323}
3324
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003325#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3326 static int
3327#ifdef __BORLANDC__
3328_RTLENTRYF
3329#endif
3330sort_func_compare(s1, s2)
3331 const void *s1;
3332 const void *s2;
3333{
3334 char_u *p1 = *(char_u **)s1;
3335 char_u *p2 = *(char_u **)s2;
3336
3337 if (*p1 != '<' && *p2 == '<') return -1;
3338 if (*p1 == '<' && *p2 != '<') return 1;
3339 return STRCMP(p1, p2);
3340}
3341#endif
3342
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343/*
3344 * Return FAIL if this is not an appropriate context in which to do
3345 * completion of anything, return OK if it is (even if there are no matches).
3346 * For the caller, this means that the character is just passed through like a
3347 * normal character (instead of being expanded). This allows :s/^I^D etc.
3348 */
3349 static int
Bram Moolenaarb3479632012-11-28 16:49:58 +01003350nextwild(xp, type, options, escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 expand_T *xp;
3352 int type;
3353 int options; /* extra options for ExpandOne() */
Bram Moolenaarb3479632012-11-28 16:49:58 +01003354 int escape; /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356 int i, j;
3357 char_u *p1;
3358 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 int difflen;
3360 int v;
3361
3362 if (xp->xp_numfiles == -1)
3363 {
3364 set_expand_context(xp);
3365 cmd_showtail = expand_showtail(xp);
3366 }
3367
3368 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3369 {
3370 beep_flush();
3371 return OK; /* Something illegal on command line */
3372 }
3373 if (xp->xp_context == EXPAND_NOTHING)
3374 {
3375 /* Caller can use the character as a normal char instead */
3376 return FAIL;
3377 }
3378
3379 MSG_PUTS("..."); /* show that we are busy */
3380 out_flush();
3381
3382 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003383 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385 if (type == WILD_NEXT || type == WILD_PREV)
3386 {
3387 /*
3388 * Get next/previous match for a previous expanded pattern.
3389 */
3390 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3391 }
3392 else
3393 {
3394 /*
3395 * Translate string into pattern and expand it.
3396 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003397 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3398 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 p2 = NULL;
3400 else
3401 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003402 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003403 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3404 if (escape)
3405 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003406
3407 if (p_wic)
3408 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003409 p2 = ExpandOne(xp, p1,
3410 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003411 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003413 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (p2 != NULL && type == WILD_LONGEST)
3415 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003416 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 if (ccline.cmdbuff[i + j] == '*'
3418 || ccline.cmdbuff[i + j] == '?')
3419 break;
3420 if ((int)STRLEN(p2) < j)
3421 {
3422 vim_free(p2);
3423 p2 = NULL;
3424 }
3425 }
3426 }
3427 }
3428
3429 if (p2 != NULL && !got_int)
3430 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003431 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003432 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003434 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 xp->xp_pattern = ccline.cmdbuff + i;
3436 }
3437 else
3438 v = OK;
3439 if (v == OK)
3440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003441 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3442 &ccline.cmdbuff[ccline.cmdpos],
3443 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3444 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 ccline.cmdlen += difflen;
3446 ccline.cmdpos += difflen;
3447 }
3448 }
3449 vim_free(p2);
3450
3451 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003452 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 /* When expanding a ":map" command and no matches are found, assume that
3455 * the key is supposed to be inserted literally */
3456 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3457 return FAIL;
3458
3459 if (xp->xp_numfiles <= 0 && p2 == NULL)
3460 beep_flush();
3461 else if (xp->xp_numfiles == 1)
3462 /* free expanded pattern */
3463 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3464
3465 return OK;
3466}
3467
3468/*
3469 * Do wildcard expansion on the string 'str'.
3470 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003471 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 * Return NULL for failure.
3473 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003474 * "orig" is the originally expanded string, copied to allocated memory. It
3475 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3476 * WILD_PREV "orig" should be NULL.
3477 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003478 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3479 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 *
3481 * mode = WILD_FREE: just free previously expanded matches
3482 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3483 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3484 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3485 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3486 * mode = WILD_ALL: return all matches concatenated
3487 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003488 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 *
3490 * options = WILD_LIST_NOTFOUND: list entries without a match
3491 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3492 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3493 * options = WILD_NO_BEEP: Don't beep for multiple matches
3494 * options = WILD_ADD_SLASH: add a slash after directory names
3495 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3496 * options = WILD_SILENT: don't print warning messages
3497 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003498 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 *
3500 * The variables xp->xp_context and xp->xp_backslash must have been set!
3501 */
3502 char_u *
3503ExpandOne(xp, str, orig, options, mode)
3504 expand_T *xp;
3505 char_u *str;
3506 char_u *orig; /* allocated copy of original of expanded string */
3507 int options;
3508 int mode;
3509{
3510 char_u *ss = NULL;
3511 static int findex;
3512 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003513 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 int i;
3515 long_u len;
3516 int non_suf_match; /* number without matching suffix */
3517
3518 /*
3519 * first handle the case of using an old match
3520 */
3521 if (mode == WILD_NEXT || mode == WILD_PREV)
3522 {
3523 if (xp->xp_numfiles > 0)
3524 {
3525 if (mode == WILD_PREV)
3526 {
3527 if (findex == -1)
3528 findex = xp->xp_numfiles;
3529 --findex;
3530 }
3531 else /* mode == WILD_NEXT */
3532 ++findex;
3533
3534 /*
3535 * When wrapping around, return the original string, set findex to
3536 * -1.
3537 */
3538 if (findex < 0)
3539 {
3540 if (orig_save == NULL)
3541 findex = xp->xp_numfiles - 1;
3542 else
3543 findex = -1;
3544 }
3545 if (findex >= xp->xp_numfiles)
3546 {
3547 if (orig_save == NULL)
3548 findex = 0;
3549 else
3550 findex = -1;
3551 }
3552#ifdef FEAT_WILDMENU
3553 if (p_wmnu)
3554 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3555 findex, cmd_showtail);
3556#endif
3557 if (findex == -1)
3558 return vim_strsave(orig_save);
3559 return vim_strsave(xp->xp_files[findex]);
3560 }
3561 else
3562 return NULL;
3563 }
3564
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003565 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3567 {
3568 FreeWild(xp->xp_numfiles, xp->xp_files);
3569 xp->xp_numfiles = -1;
3570 vim_free(orig_save);
3571 orig_save = NULL;
3572 }
3573 findex = 0;
3574
3575 if (mode == WILD_FREE) /* only release file name */
3576 return NULL;
3577
3578 if (xp->xp_numfiles == -1)
3579 {
3580 vim_free(orig_save);
3581 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003582 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584 /*
3585 * Do the expansion.
3586 */
3587 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3588 options) == FAIL)
3589 {
3590#ifdef FNAME_ILLEGAL
3591 /* Illegal file name has been silently skipped. But when there
3592 * are wildcards, the real problem is that there was no match,
3593 * causing the pattern to be added, which has illegal characters.
3594 */
3595 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3596 EMSG2(_(e_nomatch2), str);
3597#endif
3598 }
3599 else if (xp->xp_numfiles == 0)
3600 {
3601 if (!(options & WILD_SILENT))
3602 EMSG2(_(e_nomatch2), str);
3603 }
3604 else
3605 {
3606 /* Escape the matches for use on the command line. */
3607 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3608
3609 /*
3610 * Check for matching suffixes in file names.
3611 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003612 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3613 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 if (xp->xp_numfiles)
3616 non_suf_match = xp->xp_numfiles;
3617 else
3618 non_suf_match = 1;
3619 if ((xp->xp_context == EXPAND_FILES
3620 || xp->xp_context == EXPAND_DIRECTORIES)
3621 && xp->xp_numfiles > 1)
3622 {
3623 /*
3624 * More than one match; check suffix.
3625 * The files will have been sorted on matching suffix in
3626 * expand_wildcards, only need to check the first two.
3627 */
3628 non_suf_match = 0;
3629 for (i = 0; i < 2; ++i)
3630 if (match_suffix(xp->xp_files[i]))
3631 ++non_suf_match;
3632 }
3633 if (non_suf_match != 1)
3634 {
3635 /* Can we ever get here unless it's while expanding
3636 * interactively? If not, we can get rid of this all
3637 * together. Don't really want to wait for this message
3638 * (and possibly have to hit return to continue!).
3639 */
3640 if (!(options & WILD_SILENT))
3641 EMSG(_(e_toomany));
3642 else if (!(options & WILD_NO_BEEP))
3643 beep_flush();
3644 }
3645 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3646 ss = vim_strsave(xp->xp_files[0]);
3647 }
3648 }
3649 }
3650
3651 /* Find longest common part */
3652 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3653 {
3654 for (len = 0; xp->xp_files[0][len]; ++len)
3655 {
3656 for (i = 0; i < xp->xp_numfiles; ++i)
3657 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003658 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003660 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003661 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
3663 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3664 TOLOWER_LOC(xp->xp_files[0][len]))
3665 break;
3666 }
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003667 else if (xp->xp_files[i][len] != xp->xp_files[0][len])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 break;
3669 }
3670 if (i < xp->xp_numfiles)
3671 {
3672 if (!(options & WILD_NO_BEEP))
3673 vim_beep();
3674 break;
3675 }
3676 }
3677 ss = alloc((unsigned)len + 1);
3678 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003679 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 findex = -1; /* next p_wc gets first one */
3681 }
3682
3683 /* Concatenate all matching names */
3684 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3685 {
3686 len = 0;
3687 for (i = 0; i < xp->xp_numfiles; ++i)
3688 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3689 ss = lalloc(len, TRUE);
3690 if (ss != NULL)
3691 {
3692 *ss = NUL;
3693 for (i = 0; i < xp->xp_numfiles; ++i)
3694 {
3695 STRCAT(ss, xp->xp_files[i]);
3696 if (i != xp->xp_numfiles - 1)
3697 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3698 }
3699 }
3700 }
3701
3702 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3703 ExpandCleanup(xp);
3704
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003705 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003706 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003707 vim_free(orig);
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 return ss;
3710}
3711
3712/*
3713 * Prepare an expand structure for use.
3714 */
3715 void
3716ExpandInit(xp)
3717 expand_T *xp;
3718{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003719 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003720 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003722#ifndef BACKSLASH_IN_FILENAME
3723 xp->xp_shell = FALSE;
3724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 xp->xp_numfiles = -1;
3726 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003727#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3728 xp->xp_arg = NULL;
3729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730}
3731
3732/*
3733 * Cleanup an expand structure after use.
3734 */
3735 void
3736ExpandCleanup(xp)
3737 expand_T *xp;
3738{
3739 if (xp->xp_numfiles >= 0)
3740 {
3741 FreeWild(xp->xp_numfiles, xp->xp_files);
3742 xp->xp_numfiles = -1;
3743 }
3744}
3745
3746 void
3747ExpandEscape(xp, str, numfiles, files, options)
3748 expand_T *xp;
3749 char_u *str;
3750 int numfiles;
3751 char_u **files;
3752 int options;
3753{
3754 int i;
3755 char_u *p;
3756
3757 /*
3758 * May change home directory back to "~"
3759 */
3760 if (options & WILD_HOME_REPLACE)
3761 tilde_replace(str, numfiles, files);
3762
3763 if (options & WILD_ESCAPE)
3764 {
3765 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003766 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003767 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 || xp->xp_context == EXPAND_BUFFERS
3769 || xp->xp_context == EXPAND_DIRECTORIES)
3770 {
3771 /*
3772 * Insert a backslash into a file name before a space, \, %, #
3773 * and wildmatch characters, except '~'.
3774 */
3775 for (i = 0; i < numfiles; ++i)
3776 {
3777 /* for ":set path=" we need to escape spaces twice */
3778 if (xp->xp_backslash == XP_BS_THREE)
3779 {
3780 p = vim_strsave_escaped(files[i], (char_u *)" ");
3781 if (p != NULL)
3782 {
3783 vim_free(files[i]);
3784 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003785#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 p = vim_strsave_escaped(files[i], (char_u *)" ");
3787 if (p != NULL)
3788 {
3789 vim_free(files[i]);
3790 files[i] = p;
3791 }
3792#endif
3793 }
3794 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003795#ifdef BACKSLASH_IN_FILENAME
3796 p = vim_strsave_fnameescape(files[i], FALSE);
3797#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003798 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 if (p != NULL)
3801 {
3802 vim_free(files[i]);
3803 files[i] = p;
3804 }
3805
3806 /* If 'str' starts with "\~", replace "~" at start of
3807 * files[i] with "\~". */
3808 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003809 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003812
3813 /* If the first file starts with a '+' escape it. Otherwise it
3814 * could be seen as "+cmd". */
3815 if (*files[0] == '+')
3816 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
3818 else if (xp->xp_context == EXPAND_TAGS)
3819 {
3820 /*
3821 * Insert a backslash before characters in a tag name that
3822 * would terminate the ":tag" command.
3823 */
3824 for (i = 0; i < numfiles; ++i)
3825 {
3826 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3827 if (p != NULL)
3828 {
3829 vim_free(files[i]);
3830 files[i] = p;
3831 }
3832 }
3833 }
3834 }
3835}
3836
3837/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003838 * Escape special characters in "fname" for when used as a file name argument
3839 * after a Vim command, or, when "shell" is non-zero, a shell command.
3840 * Returns the result in allocated memory.
3841 */
3842 char_u *
3843vim_strsave_fnameescape(fname, shell)
3844 char_u *fname;
3845 int shell;
3846{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003847 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003848#ifdef BACKSLASH_IN_FILENAME
3849 char_u buf[20];
3850 int j = 0;
3851
3852 /* Don't escape '[' and '{' if they are in 'isfname'. */
3853 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3854 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3855 buf[j++] = *p;
3856 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003857 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003858#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003859 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3860 if (shell && csh_like_shell() && p != NULL)
3861 {
3862 char_u *s;
3863
3864 /* For csh and similar shells need to put two backslashes before '!'.
3865 * One is taken by Vim, one by the shell. */
3866 s = vim_strsave_escaped(p, (char_u *)"!");
3867 vim_free(p);
3868 p = s;
3869 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003870#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003871
3872 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3873 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003874 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003875 escape_fname(&p);
3876
3877 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003878}
3879
3880/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003881 * Put a backslash before the file name in "pp", which is in allocated memory.
3882 */
3883 static void
3884escape_fname(pp)
3885 char_u **pp;
3886{
3887 char_u *p;
3888
3889 p = alloc((unsigned)(STRLEN(*pp) + 2));
3890 if (p != NULL)
3891 {
3892 p[0] = '\\';
3893 STRCPY(p + 1, *pp);
3894 vim_free(*pp);
3895 *pp = p;
3896 }
3897}
3898
3899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 * For each file name in files[num_files]:
3901 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3902 */
3903 void
3904tilde_replace(orig_pat, num_files, files)
3905 char_u *orig_pat;
3906 int num_files;
3907 char_u **files;
3908{
3909 int i;
3910 char_u *p;
3911
3912 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3913 {
3914 for (i = 0; i < num_files; ++i)
3915 {
3916 p = home_replace_save(NULL, files[i]);
3917 if (p != NULL)
3918 {
3919 vim_free(files[i]);
3920 files[i] = p;
3921 }
3922 }
3923 }
3924}
3925
3926/*
3927 * Show all matches for completion on the command line.
3928 * Returns EXPAND_NOTHING when the character that triggered expansion should
3929 * be inserted like a normal character.
3930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 static int
3932showmatches(xp, wildmenu)
3933 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003934 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
3936#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3937 int num_files;
3938 char_u **files_found;
3939 int i, j, k;
3940 int maxlen;
3941 int lines;
3942 int columns;
3943 char_u *p;
3944 int lastlen;
3945 int attr;
3946 int showtail;
3947
3948 if (xp->xp_numfiles == -1)
3949 {
3950 set_expand_context(xp);
3951 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3952 &num_files, &files_found);
3953 showtail = expand_showtail(xp);
3954 if (i != EXPAND_OK)
3955 return i;
3956
3957 }
3958 else
3959 {
3960 num_files = xp->xp_numfiles;
3961 files_found = xp->xp_files;
3962 showtail = cmd_showtail;
3963 }
3964
3965#ifdef FEAT_WILDMENU
3966 if (!wildmenu)
3967 {
3968#endif
3969 msg_didany = FALSE; /* lines_left will be set */
3970 msg_start(); /* prepare for paging */
3971 msg_putchar('\n');
3972 out_flush();
3973 cmdline_row = msg_row;
3974 msg_didany = FALSE; /* lines_left will be set again */
3975 msg_start(); /* prepare for paging */
3976#ifdef FEAT_WILDMENU
3977 }
3978#endif
3979
3980 if (got_int)
3981 got_int = FALSE; /* only int. the completion, not the cmd line */
3982#ifdef FEAT_WILDMENU
3983 else if (wildmenu)
3984 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3985#endif
3986 else
3987 {
3988 /* find the length of the longest file name */
3989 maxlen = 0;
3990 for (i = 0; i < num_files; ++i)
3991 {
3992 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003993 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 || xp->xp_context == EXPAND_BUFFERS))
3995 {
3996 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3997 j = vim_strsize(NameBuff);
3998 }
3999 else
4000 j = vim_strsize(L_SHOWFILE(i));
4001 if (j > maxlen)
4002 maxlen = j;
4003 }
4004
4005 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4006 lines = num_files;
4007 else
4008 {
4009 /* compute the number of columns and lines for the listing */
4010 maxlen += 2; /* two spaces between file names */
4011 columns = ((int)Columns + 2) / maxlen;
4012 if (columns < 1)
4013 columns = 1;
4014 lines = (num_files + columns - 1) / columns;
4015 }
4016
4017 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4018
4019 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4020 {
4021 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4022 msg_clr_eos();
4023 msg_advance(maxlen - 3);
4024 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4025 }
4026
4027 /* list the files line by line */
4028 for (i = 0; i < lines; ++i)
4029 {
4030 lastlen = 999;
4031 for (k = i; k < num_files; k += lines)
4032 {
4033 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4034 {
4035 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4036 p = files_found[k] + STRLEN(files_found[k]) + 1;
4037 msg_advance(maxlen + 1);
4038 msg_puts(p);
4039 msg_advance(maxlen + 3);
4040 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4041 break;
4042 }
4043 for (j = maxlen - lastlen; --j >= 0; )
4044 msg_putchar(' ');
4045 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004046 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 || xp->xp_context == EXPAND_BUFFERS)
4048 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004049 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004050 if (xp->xp_numfiles != -1)
4051 {
4052 char_u *halved_slash;
4053 char_u *exp_path;
4054
4055 /* Expansion was done before and special characters
4056 * were escaped, need to halve backslashes. Also
4057 * $HOME has been replaced with ~/. */
4058 exp_path = expand_env_save_opt(files_found[k], TRUE);
4059 halved_slash = backslash_halve_save(
4060 exp_path != NULL ? exp_path : files_found[k]);
4061 j = mch_isdir(halved_slash != NULL ? halved_slash
4062 : files_found[k]);
4063 vim_free(exp_path);
4064 vim_free(halved_slash);
4065 }
4066 else
4067 /* Expansion was done here, file names are literal. */
4068 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 if (showtail)
4070 p = L_SHOWFILE(k);
4071 else
4072 {
4073 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4074 TRUE);
4075 p = NameBuff;
4076 }
4077 }
4078 else
4079 {
4080 j = FALSE;
4081 p = L_SHOWFILE(k);
4082 }
4083 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4084 }
4085 if (msg_col > 0) /* when not wrapped around */
4086 {
4087 msg_clr_eos();
4088 msg_putchar('\n');
4089 }
4090 out_flush(); /* show one line at a time */
4091 if (got_int)
4092 {
4093 got_int = FALSE;
4094 break;
4095 }
4096 }
4097
4098 /*
4099 * we redraw the command below the lines that we have just listed
4100 * This is a bit tricky, but it saves a lot of screen updating.
4101 */
4102 cmdline_row = msg_row; /* will put it back later */
4103 }
4104
4105 if (xp->xp_numfiles == -1)
4106 FreeWild(num_files, files_found);
4107
4108 return EXPAND_OK;
4109}
4110
4111/*
4112 * Private gettail for showmatches() (and win_redr_status_matches()):
4113 * Find tail of file name path, but ignore trailing "/".
4114 */
4115 char_u *
4116sm_gettail(s)
4117 char_u *s;
4118{
4119 char_u *p;
4120 char_u *t = s;
4121 int had_sep = FALSE;
4122
4123 for (p = s; *p != NUL; )
4124 {
4125 if (vim_ispathsep(*p)
4126#ifdef BACKSLASH_IN_FILENAME
4127 && !rem_backslash(p)
4128#endif
4129 )
4130 had_sep = TRUE;
4131 else if (had_sep)
4132 {
4133 t = p;
4134 had_sep = FALSE;
4135 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004136 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 }
4138 return t;
4139}
4140
4141/*
4142 * Return TRUE if we only need to show the tail of completion matches.
4143 * When not completing file names or there is a wildcard in the path FALSE is
4144 * returned.
4145 */
4146 static int
4147expand_showtail(xp)
4148 expand_T *xp;
4149{
4150 char_u *s;
4151 char_u *end;
4152
4153 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004154 if (xp->xp_context != EXPAND_FILES
4155 && xp->xp_context != EXPAND_SHELLCMD
4156 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FALSE;
4158
4159 end = gettail(xp->xp_pattern);
4160 if (end == xp->xp_pattern) /* there is no path separator */
4161 return FALSE;
4162
4163 for (s = xp->xp_pattern; s < end; s++)
4164 {
4165 /* Skip escaped wildcards. Only when the backslash is not a path
4166 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4167 if (rem_backslash(s))
4168 ++s;
4169 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4170 return FALSE;
4171 }
4172 return TRUE;
4173}
4174
4175/*
4176 * Prepare a string for expansion.
4177 * When expanding file names: The string will be used with expand_wildcards().
4178 * Copy the file name into allocated memory and add a '*' at the end.
4179 * When expanding other names: The string will be used with regcomp(). Copy
4180 * the name into allocated memory and prepend "^".
4181 */
4182 char_u *
4183addstar(fname, len, context)
4184 char_u *fname;
4185 int len;
4186 int context; /* EXPAND_FILES etc. */
4187{
4188 char_u *retval;
4189 int i, j;
4190 int new_len;
4191 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004192 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004194 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004195 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004196 && context != EXPAND_SHELLCMD
4197 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
4199 /*
4200 * Matching will be done internally (on something other than files).
4201 * So we convert the file-matching-type wildcards into our kind for
4202 * use with vim_regcomp(). First work out how long it will be:
4203 */
4204
4205 /* For help tags the translation is done in find_help_tags().
4206 * For a tag pattern starting with "/" no translation is needed. */
4207 if (context == EXPAND_HELP
4208 || context == EXPAND_COLORS
4209 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004210 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004211 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 || (context == EXPAND_TAGS && fname[0] == '/'))
4213 retval = vim_strnsave(fname, len);
4214 else
4215 {
4216 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4217 for (i = 0; i < len; i++)
4218 {
4219 if (fname[i] == '*' || fname[i] == '~')
4220 new_len++; /* '*' needs to be replaced by ".*"
4221 '~' needs to be replaced by "\~" */
4222
4223 /* Buffer names are like file names. "." should be literal */
4224 if (context == EXPAND_BUFFERS && fname[i] == '.')
4225 new_len++; /* "." becomes "\." */
4226
4227 /* Custom expansion takes care of special things, match
4228 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004229 if ((context == EXPAND_USER_DEFINED
4230 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 new_len++; /* '\' becomes "\\" */
4232 }
4233 retval = alloc(new_len);
4234 if (retval != NULL)
4235 {
4236 retval[0] = '^';
4237 j = 1;
4238 for (i = 0; i < len; i++, j++)
4239 {
4240 /* Skip backslash. But why? At least keep it for custom
4241 * expansion. */
4242 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004243 && context != EXPAND_USER_LIST
4244 && fname[i] == '\\'
4245 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 break;
4247
4248 switch (fname[i])
4249 {
4250 case '*': retval[j++] = '.';
4251 break;
4252 case '~': retval[j++] = '\\';
4253 break;
4254 case '?': retval[j] = '.';
4255 continue;
4256 case '.': if (context == EXPAND_BUFFERS)
4257 retval[j++] = '\\';
4258 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004259 case '\\': if (context == EXPAND_USER_DEFINED
4260 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 retval[j++] = '\\';
4262 break;
4263 }
4264 retval[j] = fname[i];
4265 }
4266 retval[j] = NUL;
4267 }
4268 }
4269 }
4270 else
4271 {
4272 retval = alloc(len + 4);
4273 if (retval != NULL)
4274 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004275 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004278 * Don't add a star to *, ~, ~user, $var or `cmd`.
4279 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 * ~ would be at the start of the file name, but not the tail.
4281 * $ could be anywhere in the tail.
4282 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004283 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 */
4285 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004286 ends_in_star = (len > 0 && retval[len - 1] == '*');
4287#ifndef BACKSLASH_IN_FILENAME
4288 for (i = len - 2; i >= 0; --i)
4289 {
4290 if (retval[i] != '\\')
4291 break;
4292 ends_in_star = !ends_in_star;
4293 }
4294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004296 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 && vim_strchr(tail, '$') == NULL
4298 && vim_strchr(retval, '`') == NULL)
4299 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004300 else if (len > 0 && retval[len - 1] == '$')
4301 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 retval[len] = NUL;
4303 }
4304 }
4305 return retval;
4306}
4307
4308/*
4309 * Must parse the command line so far to work out what context we are in.
4310 * Completion can then be done based on that context.
4311 * This routine sets the variables:
4312 * xp->xp_pattern The start of the pattern to be expanded within
4313 * the command line (ends at the cursor).
4314 * xp->xp_context The type of thing to expand. Will be one of:
4315 *
4316 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4317 * the command line, like an unknown command. Caller
4318 * should beep.
4319 * EXPAND_NOTHING Unrecognised context for completion, use char like
4320 * a normal char, rather than for completion. eg
4321 * :s/^I/
4322 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4323 * it.
4324 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4325 * EXPAND_FILES After command with XFILE set, or after setting
4326 * with P_EXPAND set. eg :e ^I, :w>>^I
4327 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4328 * when we know only directories are of interest. eg
4329 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004330 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4332 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4333 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4334 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4335 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4336 * EXPAND_EVENTS Complete event names
4337 * EXPAND_SYNTAX Complete :syntax command arguments
4338 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4339 * EXPAND_AUGROUP Complete autocommand group names
4340 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4341 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4342 * eg :unmap a^I , :cunab x^I
4343 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4344 * eg :call sub^I
4345 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4346 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4347 * names in expressions, eg :while s^I
4348 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004349 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 */
4351 static void
4352set_expand_context(xp)
4353 expand_T *xp;
4354{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004355 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 if (ccline.cmdfirstc != ':'
4357#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004358 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004359 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360#endif
4361 )
4362 {
4363 xp->xp_context = EXPAND_NOTHING;
4364 return;
4365 }
4366 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4367}
4368
4369 void
4370set_cmd_context(xp, str, len, col)
4371 expand_T *xp;
4372 char_u *str; /* start of command line */
4373 int len; /* length of command line (excl. NUL) */
4374 int col; /* position of cursor */
4375{
4376 int old_char = NUL;
4377 char_u *nextcomm;
4378
4379 /*
4380 * Avoid a UMR warning from Purify, only save the character if it has been
4381 * written before.
4382 */
4383 if (col < len)
4384 old_char = str[col];
4385 str[col] = NUL;
4386 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004387
4388#ifdef FEAT_EVAL
4389 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004390 {
4391# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004392 /* pass CMD_SIZE because there is no real command */
4393 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004394# endif
4395 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004396 else if (ccline.input_fn)
4397 {
4398 xp->xp_context = ccline.xp_context;
4399 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004400# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004401 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004402# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004403 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004404 else
4405#endif
4406 while (nextcomm != NULL)
4407 nextcomm = set_one_cmd_context(xp, nextcomm);
4408
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 str[col] = old_char;
4410}
4411
4412/*
4413 * Expand the command line "str" from context "xp".
4414 * "xp" must have been set by set_cmd_context().
4415 * xp->xp_pattern points into "str", to where the text that is to be expanded
4416 * starts.
4417 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4418 * cursor.
4419 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4420 * key that triggered expansion literally.
4421 * Returns EXPAND_OK otherwise.
4422 */
4423 int
4424expand_cmdline(xp, str, col, matchcount, matches)
4425 expand_T *xp;
4426 char_u *str; /* start of command line */
4427 int col; /* position of cursor */
4428 int *matchcount; /* return: nr of matches */
4429 char_u ***matches; /* return: array of pointers to matches */
4430{
4431 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004432 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
4434 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4435 {
4436 beep_flush();
4437 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4438 }
4439 if (xp->xp_context == EXPAND_NOTHING)
4440 {
4441 /* Caller can use the character as a normal char instead */
4442 return EXPAND_NOTHING;
4443 }
4444
4445 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004446 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4447 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 if (file_str == NULL)
4449 return EXPAND_UNSUCCESSFUL;
4450
Bram Moolenaar94950a92010-12-02 16:01:29 +01004451 if (p_wic)
4452 options += WILD_ICASE;
4453
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004455 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
4457 *matchcount = 0;
4458 *matches = NULL;
4459 }
4460 vim_free(file_str);
4461
4462 return EXPAND_OK;
4463}
4464
4465#ifdef FEAT_MULTI_LANG
4466/*
4467 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4468 */
4469static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4470
4471 static void
4472cleanup_help_tags(num_file, file)
4473 int num_file;
4474 char_u **file;
4475{
4476 int i, j;
4477 int len;
4478
4479 for (i = 0; i < num_file; ++i)
4480 {
4481 len = (int)STRLEN(file[i]) - 3;
4482 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4483 {
4484 /* Sorting on priority means the same item in another language may
4485 * be anywhere. Search all items for a match up to the "@en". */
4486 for (j = 0; j < num_file; ++j)
4487 if (j != i
4488 && (int)STRLEN(file[j]) == len + 3
4489 && STRNCMP(file[i], file[j], len + 1) == 0)
4490 break;
4491 if (j == num_file)
4492 file[i][len] = NUL;
4493 }
4494 }
4495}
4496#endif
4497
4498/*
4499 * Do the expansion based on xp->xp_context and "pat".
4500 */
4501 static int
4502ExpandFromContext(xp, pat, num_file, file, options)
4503 expand_T *xp;
4504 char_u *pat;
4505 int *num_file;
4506 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004507 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508{
4509#ifdef FEAT_CMDL_COMPL
4510 regmatch_T regmatch;
4511#endif
4512 int ret;
4513 int flags;
4514
4515 flags = EW_DIR; /* include directories */
4516 if (options & WILD_LIST_NOTFOUND)
4517 flags |= EW_NOTFOUND;
4518 if (options & WILD_ADD_SLASH)
4519 flags |= EW_ADDSLASH;
4520 if (options & WILD_KEEP_ALL)
4521 flags |= EW_KEEPALL;
4522 if (options & WILD_SILENT)
4523 flags |= EW_SILENT;
4524
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004525 if (xp->xp_context == EXPAND_FILES
4526 || xp->xp_context == EXPAND_DIRECTORIES
4527 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
4529 /*
4530 * Expand file or directory names.
4531 */
4532 int free_pat = FALSE;
4533 int i;
4534
4535 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4536 * space */
4537 if (xp->xp_backslash != XP_BS_NONE)
4538 {
4539 free_pat = TRUE;
4540 pat = vim_strsave(pat);
4541 for (i = 0; pat[i]; ++i)
4542 if (pat[i] == '\\')
4543 {
4544 if (xp->xp_backslash == XP_BS_THREE
4545 && pat[i + 1] == '\\'
4546 && pat[i + 2] == '\\'
4547 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004548 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (xp->xp_backslash == XP_BS_ONE
4550 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004551 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 }
4553 }
4554
4555 if (xp->xp_context == EXPAND_FILES)
4556 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004557 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4558 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 else
4560 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004561 if (options & WILD_ICASE)
4562 flags |= EW_ICASE;
4563
Bram Moolenaard7834d32009-12-02 16:14:36 +00004564 /* Expand wildcards, supporting %:h and the like. */
4565 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 if (free_pat)
4567 vim_free(pat);
4568 return ret;
4569 }
4570
4571 *file = (char_u **)"";
4572 *num_file = 0;
4573 if (xp->xp_context == EXPAND_HELP)
4574 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004575 /* With an empty argument we would get all the help tags, which is
4576 * very slow. Get matches for "help" instead. */
4577 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4578 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 {
4580#ifdef FEAT_MULTI_LANG
4581 cleanup_help_tags(*num_file, *file);
4582#endif
4583 return OK;
4584 }
4585 return FAIL;
4586 }
4587
4588#ifndef FEAT_CMDL_COMPL
4589 return FAIL;
4590#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004591 if (xp->xp_context == EXPAND_SHELLCMD)
4592 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (xp->xp_context == EXPAND_OLD_SETTING)
4594 return ExpandOldSetting(num_file, file);
4595 if (xp->xp_context == EXPAND_BUFFERS)
4596 return ExpandBufnames(pat, num_file, file, options);
4597 if (xp->xp_context == EXPAND_TAGS
4598 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4599 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4600 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004601 {
4602 char *directories[] = {"colors", NULL};
4603 return ExpandRTDir(pat, num_file, file, directories);
4604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004606 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004607 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004608 return ExpandRTDir(pat, num_file, file, directories);
4609 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004610 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004611 {
4612 char *directories[] = {"syntax", NULL};
4613 return ExpandRTDir(pat, num_file, file, directories);
4614 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004615 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004616 {
4617 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4618 return ExpandRTDir(pat, num_file, file, directories);
4619 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004620# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4621 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004622 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624
4625 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4626 if (regmatch.regprog == NULL)
4627 return FAIL;
4628
4629 /* set ignore-case according to p_ic, p_scs and pat */
4630 regmatch.rm_ic = ignorecase(pat);
4631
4632 if (xp->xp_context == EXPAND_SETTINGS
4633 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4634 ret = ExpandSettings(xp, &regmatch, num_file, file);
4635 else if (xp->xp_context == EXPAND_MAPPINGS)
4636 ret = ExpandMappings(&regmatch, num_file, file);
4637# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4638 else if (xp->xp_context == EXPAND_USER_DEFINED)
4639 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4640# endif
4641 else
4642 {
4643 static struct expgen
4644 {
4645 int context;
4646 char_u *((*func)__ARGS((expand_T *, int)));
4647 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004648 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 } tab[] =
4650 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004651 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4652 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004653#ifdef FEAT_CMDHIST
4654 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004657 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4658 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4659 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4660 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661#endif
4662#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004663 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4664 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4665 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4666 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667#endif
4668#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004669 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4670 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671#endif
4672#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004673 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004675 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004677 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4678 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004680#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004681 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004682#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004683#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004684 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004685#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004686#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004687 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4690 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004691 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4692 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004694 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004695 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 };
4697 int i;
4698
4699 /*
4700 * Find a context in the table and call the ExpandGeneric() with the
4701 * right function to do the expansion.
4702 */
4703 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004704 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 if (xp->xp_context == tab[i].context)
4706 {
4707 if (tab[i].ic)
4708 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004709 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004710 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 break;
4712 }
4713 }
4714
4715 vim_free(regmatch.regprog);
4716
4717 return ret;
4718#endif /* FEAT_CMDL_COMPL */
4719}
4720
4721#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4722/*
4723 * Expand a list of names.
4724 *
4725 * Generic function for command line completion. It calls a function to
4726 * obtain strings, one by one. The strings are matched against a regexp
4727 * program. Matching strings are copied into an array, which is returned.
4728 *
4729 * Returns OK when no problems encountered, FAIL for error (out of memory).
4730 */
4731 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004732ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 expand_T *xp;
4734 regmatch_T *regmatch;
4735 int *num_file;
4736 char_u ***file;
4737 char_u *((*func)__ARGS((expand_T *, int)));
4738 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004739 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
4741 int i;
4742 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004743 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 char_u *str;
4745
4746 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004747 * round == 0: count the number of matching names
4748 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004750 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
4752 for (i = 0; ; ++i)
4753 {
4754 str = (*func)(xp, i);
4755 if (str == NULL) /* end of list */
4756 break;
4757 if (*str == NUL) /* skip empty strings */
4758 continue;
4759
4760 if (vim_regexec(regmatch, str, (colnr_T)0))
4761 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004762 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004764 if (escaped)
4765 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4766 else
4767 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 (*file)[count] = str;
4769#ifdef FEAT_MENU
4770 if (func == get_menu_names && str != NULL)
4771 {
4772 /* test for separator added by get_menu_names() */
4773 str += STRLEN(str) - 1;
4774 if (*str == '\001')
4775 *str = '.';
4776 }
4777#endif
4778 }
4779 ++count;
4780 }
4781 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004782 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
4784 if (count == 0)
4785 return OK;
4786 *num_file = count;
4787 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4788 if (*file == NULL)
4789 {
4790 *file = (char_u **)"";
4791 return FAIL;
4792 }
4793 count = 0;
4794 }
4795 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004796
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004797 /* Sort the results. Keep menu's in the specified order. */
4798 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004799 {
4800 if (xp->xp_context == EXPAND_EXPRESSION
4801 || xp->xp_context == EXPAND_FUNCTIONS
4802 || xp->xp_context == EXPAND_USER_FUNC)
4803 /* <SNR> functions should be sorted to the end. */
4804 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4805 sort_func_compare);
4806 else
4807 sort_strings(*file, *num_file);
4808 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004809
Bram Moolenaar4f688582007-07-24 12:34:30 +00004810#ifdef FEAT_CMDL_COMPL
4811 /* Reset the variables used for special highlight names expansion, so that
4812 * they don't show up when getting normal highlight names by ID. */
4813 reset_expand_highlight();
4814#endif
4815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 return OK;
4817}
4818
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004819/*
4820 * Complete a shell command.
4821 * Returns FAIL or OK;
4822 */
4823 static int
4824expand_shellcmd(filepat, num_file, file, flagsarg)
4825 char_u *filepat; /* pattern to match with command names */
4826 int *num_file; /* return: number of matches */
4827 char_u ***file; /* return: array with matches */
4828 int flagsarg; /* EW_ flags */
4829{
4830 char_u *pat;
4831 int i;
4832 char_u *path;
4833 int mustfree = FALSE;
4834 garray_T ga;
4835 char_u *buf = alloc(MAXPATHL);
4836 size_t l;
4837 char_u *s, *e;
4838 int flags = flagsarg;
4839 int ret;
4840
4841 if (buf == NULL)
4842 return FAIL;
4843
4844 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4845 * space */
4846 pat = vim_strsave(filepat);
4847 for (i = 0; pat[i]; ++i)
4848 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004849 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004850
4851 flags |= EW_FILE | EW_EXEC;
4852
4853 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004854 if (mch_isFullName(pat))
4855 path = (char_u *)" ";
4856 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004857 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4858 path = (char_u *)".";
4859 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004860 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004861 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004862 if (path == NULL)
4863 path = (char_u *)"";
4864 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004865
4866 /*
4867 * Go over all directories in $PATH. Expand matches in that directory and
4868 * collect them in "ga".
4869 */
4870 ga_init2(&ga, (int)sizeof(char *), 10);
4871 for (s = path; *s != NUL; s = e)
4872 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004873 if (*s == ' ')
4874 ++s; /* Skip space used for absolute path name. */
4875
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004876#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4877 e = vim_strchr(s, ';');
4878#else
4879 e = vim_strchr(s, ':');
4880#endif
4881 if (e == NULL)
4882 e = s + STRLEN(s);
4883
4884 l = e - s;
4885 if (l > MAXPATHL - 5)
4886 break;
4887 vim_strncpy(buf, s, l);
4888 add_pathsep(buf);
4889 l = STRLEN(buf);
4890 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4891
4892 /* Expand matches in one directory of $PATH. */
4893 ret = expand_wildcards(1, &buf, num_file, file, flags);
4894 if (ret == OK)
4895 {
4896 if (ga_grow(&ga, *num_file) == FAIL)
4897 FreeWild(*num_file, *file);
4898 else
4899 {
4900 for (i = 0; i < *num_file; ++i)
4901 {
4902 s = (*file)[i];
4903 if (STRLEN(s) > l)
4904 {
4905 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004906 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004907 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4908 }
4909 else
4910 vim_free(s);
4911 }
4912 vim_free(*file);
4913 }
4914 }
4915 if (*e != NUL)
4916 ++e;
4917 }
4918 *file = ga.ga_data;
4919 *num_file = ga.ga_len;
4920
4921 vim_free(buf);
4922 vim_free(pat);
4923 if (mustfree)
4924 vim_free(path);
4925 return OK;
4926}
4927
4928
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004930static 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));
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004933 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004934 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004936 static void *
4937call_user_expand_func(user_expand_func, xp, num_file, file)
4938 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 int *num_file;
4941 char_u ***file;
4942{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004943 char_u keep;
4944 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004947 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004948 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004951 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 *num_file = 0;
4953 *file = NULL;
4954
Bram Moolenaar21669c02008-01-18 12:16:16 +00004955 if (ccline.cmdbuff == NULL)
4956 {
4957 /* Completion from Insert mode, pass fake arguments. */
4958 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004959 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004960 args[1] = xp->xp_pattern;
4961 }
4962 else
4963 {
4964 /* Completion on the command line, pass real arguments. */
4965 keep = ccline.cmdbuff[ccline.cmdlen];
4966 ccline.cmdbuff[ccline.cmdlen] = 0;
4967 sprintf((char *)num, "%d", ccline.cmdpos);
4968 args[1] = ccline.cmdbuff;
4969 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004970 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 args[2] = num;
4972
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004973 /* Save the cmdline, we don't know what the function may do. */
4974 save_ccline = ccline;
4975 ccline.cmdbuff = NULL;
4976 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004978
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004979 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004980
4981 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004983 if (ccline.cmdbuff != NULL)
4984 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004985
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004986 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004987 return ret;
4988}
4989
4990/*
4991 * Expand names with a function defined by the user.
4992 */
4993 static int
4994ExpandUserDefined(xp, regmatch, num_file, file)
4995 expand_T *xp;
4996 regmatch_T *regmatch;
4997 int *num_file;
4998 char_u ***file;
4999{
5000 char_u *retstr;
5001 char_u *s;
5002 char_u *e;
5003 char_u keep;
5004 garray_T ga;
5005
5006 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5007 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 return FAIL;
5009
5010 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005011 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 e = vim_strchr(s, '\n');
5014 if (e == NULL)
5015 e = s + STRLEN(s);
5016 keep = *e;
5017 *e = 0;
5018
5019 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5020 {
5021 *e = keep;
5022 if (*e != NUL)
5023 ++e;
5024 continue;
5025 }
5026
5027 if (ga_grow(&ga, 1) == FAIL)
5028 break;
5029
5030 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5031 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
5033 *e = keep;
5034 if (*e != NUL)
5035 ++e;
5036 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005037 vim_free(retstr);
5038 *file = ga.ga_data;
5039 *num_file = ga.ga_len;
5040 return OK;
5041}
5042
5043/*
5044 * Expand names with a list returned by a function defined by the user.
5045 */
5046 static int
5047ExpandUserList(xp, num_file, file)
5048 expand_T *xp;
5049 int *num_file;
5050 char_u ***file;
5051{
5052 list_T *retlist;
5053 listitem_T *li;
5054 garray_T ga;
5055
5056 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5057 if (retlist == NULL)
5058 return FAIL;
5059
5060 ga_init2(&ga, (int)sizeof(char *), 3);
5061 /* Loop over the items in the list. */
5062 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5063 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005064 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5065 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005066
5067 if (ga_grow(&ga, 1) == FAIL)
5068 break;
5069
5070 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005071 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005072 ++ga.ga_len;
5073 }
5074 list_unref(retlist);
5075
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 *file = ga.ga_data;
5077 *num_file = ga.ga_len;
5078 return OK;
5079}
5080#endif
5081
5082/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005083 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005084 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005085 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 */
5087 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005088ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 char_u *pat;
5090 int *num_file;
5091 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005092 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005094 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 char_u *s;
5096 char_u *e;
5097 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005098 int i;
5099 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
5101 *num_file = 0;
5102 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005103 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005104 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005106 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005108 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5109 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005111 ga_clear_strings(&ga);
5112 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005114 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5115 matches = globpath(p_rtp, s, 0);
5116 vim_free(s);
5117 if (matches == NULL)
5118 continue;
5119
5120 for (s = matches; *s != NUL; s = e)
5121 {
5122 e = vim_strchr(s, '\n');
5123 if (e == NULL)
5124 e = s + STRLEN(s);
5125 if (ga_grow(&ga, 1) == FAIL)
5126 break;
5127 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5128 {
5129 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5130 if (*s == '\n' || vim_ispathsep(*s))
5131 break;
5132 ++s;
5133 ((char_u **)ga.ga_data)[ga.ga_len] =
5134 vim_strnsave(s, (int)(e - s - 4));
5135 ++ga.ga_len;
5136 }
5137 if (*e != NUL)
5138 ++e;
5139 }
5140 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005142 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005143 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005144
5145 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005146 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005147 remove_duplicates(&ga);
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 *file = ga.ga_data;
5150 *num_file = ga.ga_len;
5151 return OK;
5152}
5153
5154#endif
5155
5156#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5157/*
5158 * Expand "file" for all comma-separated directories in "path".
5159 * Returns an allocated string with all matches concatenated, separated by
5160 * newlines. Returns NULL for an error or no matches.
5161 */
5162 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005163globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 char_u *path;
5165 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005166 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
5168 expand_T xpc;
5169 char_u *buf;
5170 garray_T ga;
5171 int i;
5172 int len;
5173 int num_p;
5174 char_u **p;
5175 char_u *cur = NULL;
5176
5177 buf = alloc(MAXPATHL);
5178 if (buf == NULL)
5179 return NULL;
5180
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005181 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 ga_init2(&ga, 1, 100);
5185
5186 /* Loop over all entries in {path}. */
5187 while (*path != NUL)
5188 {
5189 /* Copy one item of the path to buf[] and concatenate the file name. */
5190 copy_option_part(&path, buf, MAXPATHL, ",");
5191 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5192 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005193# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005194 /* Using the platform's path separator (\) makes vim incorrectly
5195 * treat it as an escape character, use '/' instead. */
5196 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5197 STRCAT(buf, "/");
5198# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005200# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005202 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5203 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005205 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005207 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
5209 /* Concatenate new results to previous ones. */
5210 if (ga_grow(&ga, len) == OK)
5211 {
5212 cur = (char_u *)ga.ga_data + ga.ga_len;
5213 for (i = 0; i < num_p; ++i)
5214 {
5215 STRCPY(cur, p[i]);
5216 cur += STRLEN(p[i]);
5217 *cur++ = '\n';
5218 }
5219 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221 FreeWild(num_p, p);
5222 }
5223 }
5224 }
5225 if (cur != NULL)
5226 *--cur = 0; /* Replace trailing newline with NUL */
5227
5228 vim_free(buf);
5229 return (char_u *)ga.ga_data;
5230}
5231
5232#endif
5233
5234#if defined(FEAT_CMDHIST) || defined(PROTO)
5235
5236/*********************************
5237 * Command line history stuff *
5238 *********************************/
5239
5240/*
5241 * Translate a history character to the associated type number.
5242 */
5243 static int
5244hist_char2type(c)
5245 int c;
5246{
5247 if (c == ':')
5248 return HIST_CMD;
5249 if (c == '=')
5250 return HIST_EXPR;
5251 if (c == '@')
5252 return HIST_INPUT;
5253 if (c == '>')
5254 return HIST_DEBUG;
5255 return HIST_SEARCH; /* must be '?' or '/' */
5256}
5257
5258/*
5259 * Table of history names.
5260 * These names are used in :history and various hist...() functions.
5261 * It is sufficient to give the significant prefix of a history name.
5262 */
5263
5264static char *(history_names[]) =
5265{
5266 "cmd",
5267 "search",
5268 "expr",
5269 "input",
5270 "debug",
5271 NULL
5272};
5273
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005274#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5275/*
5276 * Function given to ExpandGeneric() to obtain the possible first
5277 * arguments of the ":history command.
5278 */
5279 static char_u *
5280get_history_arg(xp, idx)
5281 expand_T *xp UNUSED;
5282 int idx;
5283{
5284 static char_u compl[2] = { NUL, NUL };
5285 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005286 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005287 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5288
5289 if (idx < short_names_count)
5290 {
5291 compl[0] = (char_u)short_names[idx];
5292 return compl;
5293 }
5294 if (idx < short_names_count + history_name_count)
5295 return (char_u *)history_names[idx - short_names_count];
5296 if (idx == short_names_count + history_name_count)
5297 return (char_u *)"all";
5298 return NULL;
5299}
5300#endif
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302/*
5303 * init_history() - Initialize the command line history.
5304 * Also used to re-allocate the history when the size changes.
5305 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005306 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307init_history()
5308{
5309 int newlen; /* new length of history table */
5310 histentry_T *temp;
5311 int i;
5312 int j;
5313 int type;
5314
5315 /*
5316 * If size of history table changed, reallocate it
5317 */
5318 newlen = (int)p_hi;
5319 if (newlen != hislen) /* history length changed */
5320 {
5321 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5322 {
5323 if (newlen)
5324 {
5325 temp = (histentry_T *)lalloc(
5326 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5327 if (temp == NULL) /* out of memory! */
5328 {
5329 if (type == 0) /* first one: just keep the old length */
5330 {
5331 newlen = hislen;
5332 break;
5333 }
5334 /* Already changed one table, now we can only have zero
5335 * length for all tables. */
5336 newlen = 0;
5337 type = -1;
5338 continue;
5339 }
5340 }
5341 else
5342 temp = NULL;
5343 if (newlen == 0 || temp != NULL)
5344 {
5345 if (hisidx[type] < 0) /* there are no entries yet */
5346 {
5347 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005348 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
5350 else if (newlen > hislen) /* array becomes bigger */
5351 {
5352 for (i = 0; i <= hisidx[type]; ++i)
5353 temp[i] = history[type][i];
5354 j = i;
5355 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005356 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 for ( ; j < hislen; ++i, ++j)
5358 temp[i] = history[type][j];
5359 }
5360 else /* array becomes smaller or 0 */
5361 {
5362 j = hisidx[type];
5363 for (i = newlen - 1; ; --i)
5364 {
5365 if (i >= 0) /* copy newest entries */
5366 temp[i] = history[type][j];
5367 else /* remove older entries */
5368 vim_free(history[type][j].hisstr);
5369 if (--j < 0)
5370 j = hislen - 1;
5371 if (j == hisidx[type])
5372 break;
5373 }
5374 hisidx[type] = newlen - 1;
5375 }
5376 vim_free(history[type]);
5377 history[type] = temp;
5378 }
5379 }
5380 hislen = newlen;
5381 }
5382}
5383
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005384 static void
5385clear_hist_entry(hisptr)
5386 histentry_T *hisptr;
5387{
5388 hisptr->hisnum = 0;
5389 hisptr->viminfo = FALSE;
5390 hisptr->hisstr = NULL;
5391}
5392
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393/*
5394 * Check if command line 'str' is already in history.
5395 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5396 */
5397 static int
Bram Moolenaar4c402232011-07-27 17:58:46 +02005398in_history(type, str, move_to_front, sep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 int type;
5400 char_u *str;
5401 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005402 int sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
5404 int i;
5405 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005406 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
5408 if (hisidx[type] < 0)
5409 return FALSE;
5410 i = hisidx[type];
5411 do
5412 {
5413 if (history[type][i].hisstr == NULL)
5414 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005415
5416 /* For search history, check that the separator character matches as
5417 * well. */
5418 p = history[type][i].hisstr;
5419 if (STRCMP(str, p) == 0
5420 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 {
5422 if (!move_to_front)
5423 return TRUE;
5424 last_i = i;
5425 break;
5426 }
5427 if (--i < 0)
5428 i = hislen - 1;
5429 } while (i != hisidx[type]);
5430
5431 if (last_i >= 0)
5432 {
5433 str = history[type][i].hisstr;
5434 while (i != hisidx[type])
5435 {
5436 if (++i >= hislen)
5437 i = 0;
5438 history[type][last_i] = history[type][i];
5439 last_i = i;
5440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005442 history[type][i].viminfo = FALSE;
5443 history[type][i].hisstr = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 return TRUE;
5445 }
5446 return FALSE;
5447}
5448
5449/*
5450 * Convert history name (from table above) to its HIST_ equivalent.
5451 * When "name" is empty, return "cmd" history.
5452 * Returns -1 for unknown history name.
5453 */
5454 int
5455get_histtype(name)
5456 char_u *name;
5457{
5458 int i;
5459 int len = (int)STRLEN(name);
5460
5461 /* No argument: use current history. */
5462 if (len == 0)
5463 return hist_char2type(ccline.cmdfirstc);
5464
5465 for (i = 0; history_names[i] != NULL; ++i)
5466 if (STRNICMP(name, history_names[i], len) == 0)
5467 return i;
5468
5469 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5470 return hist_char2type(name[0]);
5471
5472 return -1;
5473}
5474
5475static int last_maptick = -1; /* last seen maptick */
5476
5477/*
5478 * Add the given string to the given history. If the string is already in the
5479 * history then it is moved to the front. "histype" may be one of he HIST_
5480 * values.
5481 */
5482 void
5483add_to_history(histype, new_entry, in_map, sep)
5484 int histype;
5485 char_u *new_entry;
5486 int in_map; /* consider maptick when inside a mapping */
5487 int sep; /* separator character used (search hist) */
5488{
5489 histentry_T *hisptr;
5490 int len;
5491
5492 if (hislen == 0) /* no history */
5493 return;
5494
5495 /*
5496 * Searches inside the same mapping overwrite each other, so that only
5497 * the last line is kept. Be careful not to remove a line that was moved
5498 * down, only lines that were added.
5499 */
5500 if (histype == HIST_SEARCH && in_map)
5501 {
5502 if (maptick == last_maptick)
5503 {
5504 /* Current line is from the same mapping, remove it */
5505 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5506 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005507 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 --hisnum[histype];
5509 if (--hisidx[HIST_SEARCH] < 0)
5510 hisidx[HIST_SEARCH] = hislen - 1;
5511 }
5512 last_maptick = -1;
5513 }
Bram Moolenaar4c402232011-07-27 17:58:46 +02005514 if (!in_history(histype, new_entry, TRUE, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
5516 if (++hisidx[histype] == hislen)
5517 hisidx[histype] = 0;
5518 hisptr = &history[histype][hisidx[histype]];
5519 vim_free(hisptr->hisstr);
5520
5521 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005522 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5524 if (hisptr->hisstr != NULL)
5525 hisptr->hisstr[len + 1] = sep;
5526
5527 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005528 hisptr->viminfo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 if (histype == HIST_SEARCH && in_map)
5530 last_maptick = maptick;
5531 }
5532}
5533
5534#if defined(FEAT_EVAL) || defined(PROTO)
5535
5536/*
5537 * Get identifier of newest history entry.
5538 * "histype" may be one of the HIST_ values.
5539 */
5540 int
5541get_history_idx(histype)
5542 int histype;
5543{
5544 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5545 || hisidx[histype] < 0)
5546 return -1;
5547
5548 return history[histype][hisidx[histype]].hisnum;
5549}
5550
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005551static struct cmdline_info *get_ccline_ptr __ARGS((void));
5552
5553/*
5554 * Get pointer to the command line info to use. cmdline_paste() may clear
5555 * ccline and put the previous value in prev_ccline.
5556 */
5557 static struct cmdline_info *
5558get_ccline_ptr()
5559{
5560 if ((State & CMDLINE) == 0)
5561 return NULL;
5562 if (ccline.cmdbuff != NULL)
5563 return &ccline;
5564 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5565 return &prev_ccline;
5566 return NULL;
5567}
5568
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569/*
5570 * Get the current command line in allocated memory.
5571 * Only works when the command line is being edited.
5572 * Returns NULL when something is wrong.
5573 */
5574 char_u *
5575get_cmdline_str()
5576{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005577 struct cmdline_info *p = get_ccline_ptr();
5578
5579 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005581 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582}
5583
5584/*
5585 * Get the current command line position, counted in bytes.
5586 * Zero is the first position.
5587 * Only works when the command line is being edited.
5588 * Returns -1 when something is wrong.
5589 */
5590 int
5591get_cmdline_pos()
5592{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005593 struct cmdline_info *p = get_ccline_ptr();
5594
5595 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005597 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598}
5599
5600/*
5601 * Set the command line byte position to "pos". Zero is the first position.
5602 * Only works when the command line is being edited.
5603 * Returns 1 when failed, 0 when OK.
5604 */
5605 int
5606set_cmdline_pos(pos)
5607 int pos;
5608{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005609 struct cmdline_info *p = get_ccline_ptr();
5610
5611 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 return 1;
5613
5614 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5615 * changed the command line. */
5616 if (pos < 0)
5617 new_cmdpos = 0;
5618 else
5619 new_cmdpos = pos;
5620 return 0;
5621}
5622
5623/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005624 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005625 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005626 * Only works when the command line is being edited.
5627 * Returns NUL when something is wrong.
5628 */
5629 int
5630get_cmdline_type()
5631{
5632 struct cmdline_info *p = get_ccline_ptr();
5633
5634 if (p == NULL)
5635 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005636 if (p->cmdfirstc == NUL)
5637 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005638 return p->cmdfirstc;
5639}
5640
5641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 * Calculate history index from a number:
5643 * num > 0: seen as identifying number of a history entry
5644 * num < 0: relative position in history wrt newest entry
5645 * "histype" may be one of the HIST_ values.
5646 */
5647 static int
5648calc_hist_idx(histype, num)
5649 int histype;
5650 int num;
5651{
5652 int i;
5653 histentry_T *hist;
5654 int wrapped = FALSE;
5655
5656 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5657 || (i = hisidx[histype]) < 0 || num == 0)
5658 return -1;
5659
5660 hist = history[histype];
5661 if (num > 0)
5662 {
5663 while (hist[i].hisnum > num)
5664 if (--i < 0)
5665 {
5666 if (wrapped)
5667 break;
5668 i += hislen;
5669 wrapped = TRUE;
5670 }
5671 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5672 return i;
5673 }
5674 else if (-num <= hislen)
5675 {
5676 i += num + 1;
5677 if (i < 0)
5678 i += hislen;
5679 if (hist[i].hisstr != NULL)
5680 return i;
5681 }
5682 return -1;
5683}
5684
5685/*
5686 * Get a history entry by its index.
5687 * "histype" may be one of the HIST_ values.
5688 */
5689 char_u *
5690get_history_entry(histype, idx)
5691 int histype;
5692 int idx;
5693{
5694 idx = calc_hist_idx(histype, idx);
5695 if (idx >= 0)
5696 return history[histype][idx].hisstr;
5697 else
5698 return (char_u *)"";
5699}
5700
5701/*
5702 * Clear all entries of a history.
5703 * "histype" may be one of the HIST_ values.
5704 */
5705 int
5706clr_history(histype)
5707 int histype;
5708{
5709 int i;
5710 histentry_T *hisptr;
5711
5712 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5713 {
5714 hisptr = history[histype];
5715 for (i = hislen; i--;)
5716 {
5717 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005718 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720 hisidx[histype] = -1; /* mark history as cleared */
5721 hisnum[histype] = 0; /* reset identifier counter */
5722 return OK;
5723 }
5724 return FAIL;
5725}
5726
5727/*
5728 * Remove all entries matching {str} from a history.
5729 * "histype" may be one of the HIST_ values.
5730 */
5731 int
5732del_history_entry(histype, str)
5733 int histype;
5734 char_u *str;
5735{
5736 regmatch_T regmatch;
5737 histentry_T *hisptr;
5738 int idx;
5739 int i;
5740 int last;
5741 int found = FALSE;
5742
5743 regmatch.regprog = NULL;
5744 regmatch.rm_ic = FALSE; /* always match case */
5745 if (hislen != 0
5746 && histype >= 0
5747 && histype < HIST_COUNT
5748 && *str != NUL
5749 && (idx = hisidx[histype]) >= 0
5750 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5751 != NULL)
5752 {
5753 i = last = idx;
5754 do
5755 {
5756 hisptr = &history[histype][i];
5757 if (hisptr->hisstr == NULL)
5758 break;
5759 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5760 {
5761 found = TRUE;
5762 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005763 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
5765 else
5766 {
5767 if (i != last)
5768 {
5769 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005770 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 }
5772 if (--last < 0)
5773 last += hislen;
5774 }
5775 if (--i < 0)
5776 i += hislen;
5777 } while (i != idx);
5778 if (history[histype][idx].hisstr == NULL)
5779 hisidx[histype] = -1;
5780 }
5781 vim_free(regmatch.regprog);
5782 return found;
5783}
5784
5785/*
5786 * Remove an indexed entry from a history.
5787 * "histype" may be one of the HIST_ values.
5788 */
5789 int
5790del_history_idx(histype, idx)
5791 int histype;
5792 int idx;
5793{
5794 int i, j;
5795
5796 i = calc_hist_idx(histype, idx);
5797 if (i < 0)
5798 return FALSE;
5799 idx = hisidx[histype];
5800 vim_free(history[histype][i].hisstr);
5801
5802 /* When deleting the last added search string in a mapping, reset
5803 * last_maptick, so that the last added search string isn't deleted again.
5804 */
5805 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5806 last_maptick = -1;
5807
5808 while (i != idx)
5809 {
5810 j = (i + 1) % hislen;
5811 history[histype][i] = history[histype][j];
5812 i = j;
5813 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005814 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 if (--i < 0)
5816 i += hislen;
5817 hisidx[histype] = i;
5818 return TRUE;
5819}
5820
5821#endif /* FEAT_EVAL */
5822
5823#if defined(FEAT_CRYPT) || defined(PROTO)
5824/*
5825 * Very specific function to remove the value in ":set key=val" from the
5826 * history.
5827 */
5828 void
5829remove_key_from_history()
5830{
5831 char_u *p;
5832 int i;
5833
5834 i = hisidx[HIST_CMD];
5835 if (i < 0)
5836 return;
5837 p = history[HIST_CMD][i].hisstr;
5838 if (p != NULL)
5839 for ( ; *p; ++p)
5840 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5841 {
5842 p = vim_strchr(p + 3, '=');
5843 if (p == NULL)
5844 break;
5845 ++p;
5846 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5847 if (p[i] == '\\' && p[i + 1])
5848 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005849 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 --p;
5851 }
5852}
5853#endif
5854
5855#endif /* FEAT_CMDHIST */
5856
5857#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5858/*
5859 * Get indices "num1,num2" that specify a range within a list (not a range of
5860 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5861 * Returns OK if parsed successfully, otherwise FAIL.
5862 */
5863 int
5864get_list_range(str, num1, num2)
5865 char_u **str;
5866 int *num1;
5867 int *num2;
5868{
5869 int len;
5870 int first = FALSE;
5871 long num;
5872
5873 *str = skipwhite(*str);
5874 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5875 {
5876 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5877 *str += len;
5878 *num1 = (int)num;
5879 first = TRUE;
5880 }
5881 *str = skipwhite(*str);
5882 if (**str == ',') /* parse "to" part of range */
5883 {
5884 *str = skipwhite(*str + 1);
5885 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5886 if (len > 0)
5887 {
5888 *num2 = (int)num;
5889 *str = skipwhite(*str + len);
5890 }
5891 else if (!first) /* no number given at all */
5892 return FAIL;
5893 }
5894 else if (first) /* only one number given */
5895 *num2 = *num1;
5896 return OK;
5897}
5898#endif
5899
5900#if defined(FEAT_CMDHIST) || defined(PROTO)
5901/*
5902 * :history command - print a history
5903 */
5904 void
5905ex_history(eap)
5906 exarg_T *eap;
5907{
5908 histentry_T *hist;
5909 int histype1 = HIST_CMD;
5910 int histype2 = HIST_CMD;
5911 int hisidx1 = 1;
5912 int hisidx2 = -1;
5913 int idx;
5914 int i, j, k;
5915 char_u *end;
5916 char_u *arg = eap->arg;
5917
5918 if (hislen == 0)
5919 {
5920 MSG(_("'history' option is zero"));
5921 return;
5922 }
5923
5924 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5925 {
5926 end = arg;
5927 while (ASCII_ISALPHA(*end)
5928 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5929 end++;
5930 i = *end;
5931 *end = NUL;
5932 histype1 = get_histtype(arg);
5933 if (histype1 == -1)
5934 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005935 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {
5937 histype1 = 0;
5938 histype2 = HIST_COUNT-1;
5939 }
5940 else
5941 {
5942 *end = i;
5943 EMSG(_(e_trailing));
5944 return;
5945 }
5946 }
5947 else
5948 histype2 = histype1;
5949 *end = i;
5950 }
5951 else
5952 end = arg;
5953 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5954 {
5955 EMSG(_(e_trailing));
5956 return;
5957 }
5958
5959 for (; !got_int && histype1 <= histype2; ++histype1)
5960 {
5961 STRCPY(IObuff, "\n # ");
5962 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5963 MSG_PUTS_TITLE(IObuff);
5964 idx = hisidx[histype1];
5965 hist = history[histype1];
5966 j = hisidx1;
5967 k = hisidx2;
5968 if (j < 0)
5969 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5970 if (k < 0)
5971 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5972 if (idx >= 0 && j <= k)
5973 for (i = idx + 1; !got_int; ++i)
5974 {
5975 if (i == hislen)
5976 i = 0;
5977 if (hist[i].hisstr != NULL
5978 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5979 {
5980 msg_putchar('\n');
5981 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5982 hist[i].hisnum);
5983 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5984 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005985 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 else
5987 STRCAT(IObuff, hist[i].hisstr);
5988 msg_outtrans(IObuff);
5989 out_flush();
5990 }
5991 if (i == idx)
5992 break;
5993 }
5994 }
5995}
5996#endif
5997
5998#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5999static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
6000static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
6001static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
6002static int viminfo_add_at_front = FALSE;
6003
6004static int hist_type2char __ARGS((int type, int use_question));
6005
6006/*
6007 * Translate a history type number to the associated character.
6008 */
6009 static int
6010hist_type2char(type, use_question)
6011 int type;
6012 int use_question; /* use '?' instead of '/' */
6013{
6014 if (type == HIST_CMD)
6015 return ':';
6016 if (type == HIST_SEARCH)
6017 {
6018 if (use_question)
6019 return '?';
6020 else
6021 return '/';
6022 }
6023 if (type == HIST_EXPR)
6024 return '=';
6025 return '@';
6026}
6027
6028/*
6029 * Prepare for reading the history from the viminfo file.
6030 * This allocates history arrays to store the read history lines.
6031 */
6032 void
6033prepare_viminfo_history(asklen)
6034 int asklen;
6035{
6036 int i;
6037 int num;
6038 int type;
6039 int len;
6040
6041 init_history();
6042 viminfo_add_at_front = (asklen != 0);
6043 if (asklen > hislen)
6044 asklen = hislen;
6045
6046 for (type = 0; type < HIST_COUNT; ++type)
6047 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006048 /* Count the number of empty spaces in the history list. Entries read
6049 * from viminfo previously are also considered empty. If there are
6050 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006052 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 num++;
6054 len = asklen;
6055 if (num > len)
6056 len = num;
6057 if (len <= 0)
6058 viminfo_history[type] = NULL;
6059 else
6060 viminfo_history[type] =
6061 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6062 if (viminfo_history[type] == NULL)
6063 len = 0;
6064 viminfo_hislen[type] = len;
6065 viminfo_hisidx[type] = 0;
6066 }
6067}
6068
6069/*
6070 * Accept a line from the viminfo, store it in the history array when it's
6071 * new.
6072 */
6073 int
6074read_viminfo_history(virp)
6075 vir_T *virp;
6076{
6077 int type;
6078 long_u len;
6079 char_u *val;
6080 char_u *p;
6081
6082 type = hist_char2type(virp->vir_line[0]);
6083 if (viminfo_hisidx[type] < viminfo_hislen[type])
6084 {
6085 val = viminfo_readstring(virp, 1, TRUE);
6086 if (val != NULL && *val != NUL)
6087 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006088 int sep = (*val == ' ' ? NUL : *val);
6089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006091 viminfo_add_at_front, sep))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 {
6093 /* Need to re-allocate to append the separator byte. */
6094 len = STRLEN(val);
6095 p = lalloc(len + 2, TRUE);
6096 if (p != NULL)
6097 {
6098 if (type == HIST_SEARCH)
6099 {
6100 /* Search entry: Move the separator from the first
6101 * column to after the NUL. */
6102 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006103 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 }
6105 else
6106 {
6107 /* Not a search entry: No separator in the viminfo
6108 * file, add a NUL separator. */
6109 mch_memmove(p, val, (size_t)len + 1);
6110 p[len + 1] = NUL;
6111 }
6112 viminfo_history[type][viminfo_hisidx[type]++] = p;
6113 }
6114 }
6115 }
6116 vim_free(val);
6117 }
6118 return viminfo_readline(virp);
6119}
6120
6121 void
6122finish_viminfo_history()
6123{
6124 int idx;
6125 int i;
6126 int type;
6127
6128 for (type = 0; type < HIST_COUNT; ++type)
6129 {
6130 if (history[type] == NULL)
6131 return;
6132 idx = hisidx[type] + viminfo_hisidx[type];
6133 if (idx >= hislen)
6134 idx -= hislen;
6135 else if (idx < 0)
6136 idx = hislen - 1;
6137 if (viminfo_add_at_front)
6138 hisidx[type] = idx;
6139 else
6140 {
6141 if (hisidx[type] == -1)
6142 hisidx[type] = hislen - 1;
6143 do
6144 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006145 if (history[type][idx].hisstr != NULL
6146 || history[type][idx].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 break;
6148 if (++idx == hislen)
6149 idx = 0;
6150 } while (idx != hisidx[type]);
6151 if (idx != hisidx[type] && --idx < 0)
6152 idx = hislen - 1;
6153 }
6154 for (i = 0; i < viminfo_hisidx[type]; i++)
6155 {
6156 vim_free(history[type][idx].hisstr);
6157 history[type][idx].hisstr = viminfo_history[type][i];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006158 history[type][idx].viminfo = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 if (--idx < 0)
6160 idx = hislen - 1;
6161 }
6162 idx += 1;
6163 idx %= hislen;
6164 for (i = 0; i < viminfo_hisidx[type]; i++)
6165 {
6166 history[type][idx++].hisnum = ++hisnum[type];
6167 idx %= hislen;
6168 }
6169 vim_free(viminfo_history[type]);
6170 viminfo_history[type] = NULL;
6171 }
6172}
6173
6174 void
6175write_viminfo_history(fp)
6176 FILE *fp;
6177{
6178 int i;
6179 int type;
6180 int num_saved;
6181 char_u *p;
6182 int c;
6183
6184 init_history();
6185 if (hislen == 0)
6186 return;
6187 for (type = 0; type < HIST_COUNT; ++type)
6188 {
6189 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6190 if (num_saved == 0)
6191 continue;
6192 if (num_saved < 0) /* Use default */
6193 num_saved = hislen;
6194 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6195 type == HIST_CMD ? _("Command Line") :
6196 type == HIST_SEARCH ? _("Search String") :
6197 type == HIST_EXPR ? _("Expression") :
6198 _("Input Line"));
6199 if (num_saved > hislen)
6200 num_saved = hislen;
6201 i = hisidx[type];
6202 if (i >= 0)
6203 while (num_saved--)
6204 {
6205 p = history[type][i].hisstr;
6206 if (p != NULL)
6207 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006208 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 /* For the search history: put the separator in the second
6210 * column; use a space if there isn't one. */
6211 if (type == HIST_SEARCH)
6212 {
6213 c = p[STRLEN(p) + 1];
6214 putc(c == NUL ? ' ' : c, fp);
6215 }
6216 viminfo_writestring(fp, p);
6217 }
6218 if (--i < 0)
6219 i = hislen - 1;
6220 }
6221 }
6222}
6223#endif /* FEAT_VIMINFO */
6224
6225#if defined(FEAT_FKMAP) || defined(PROTO)
6226/*
6227 * Write a character at the current cursor+offset position.
6228 * It is directly written into the command buffer block.
6229 */
6230 void
6231cmd_pchar(c, offset)
6232 int c, offset;
6233{
6234 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6235 {
6236 EMSG(_("E198: cmd_pchar beyond the command length"));
6237 return;
6238 }
6239 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6240 ccline.cmdbuff[ccline.cmdlen] = NUL;
6241}
6242
6243 int
6244cmd_gchar(offset)
6245 int offset;
6246{
6247 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6248 {
6249 /* EMSG(_("cmd_gchar beyond the command length")); */
6250 return NUL;
6251 }
6252 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6253}
6254#endif
6255
6256#if defined(FEAT_CMDWIN) || defined(PROTO)
6257/*
6258 * Open a window on the current command line and history. Allow editing in
6259 * the window. Returns when the window is closed.
6260 * Returns:
6261 * CR if the command is to be executed
6262 * Ctrl_C if it is to be abandoned
6263 * K_IGNORE if editing continues
6264 */
6265 static int
6266ex_window()
6267{
6268 struct cmdline_info save_ccline;
6269 buf_T *old_curbuf = curbuf;
6270 win_T *old_curwin = curwin;
6271 buf_T *bp;
6272 win_T *wp;
6273 int i;
6274 linenr_T lnum;
6275 int histtype;
6276 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006277#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 int save_restart_edit = restart_edit;
6281 int save_State = State;
6282 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006283#ifdef FEAT_RIGHTLEFT
6284 int save_cmdmsg_rl = cmdmsg_rl;
6285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286
6287 /* Can't do this recursively. Can't do it when typing a password. */
6288 if (cmdwin_type != 0
6289# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6290 || cmdline_star > 0
6291# endif
6292 )
6293 {
6294 beep_flush();
6295 return K_IGNORE;
6296 }
6297
6298 /* Save current window sizes. */
6299 win_size_save(&winsizes);
6300
6301# ifdef FEAT_AUTOCMD
6302 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006303 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006305 /* don't use a new tab page */
6306 cmdmod.tab = 0;
6307
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 /* Create a window for the command-line buffer. */
6309 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6310 {
6311 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006312# ifdef FEAT_AUTOCMD
6313 unblock_autocmds();
6314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 return K_IGNORE;
6316 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006317 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318
6319 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006320 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006321 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6323 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6324 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006325#ifdef FEAT_FOLDING
6326 curwin->w_p_fen = FALSE;
6327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006329 curwin->w_p_rl = cmdmsg_rl;
6330 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006332 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334# ifdef FEAT_AUTOCMD
6335 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006336 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337# endif
6338
Bram Moolenaar46152342005-09-07 21:18:43 +00006339 /* Showing the prompt may have set need_wait_return, reset it. */
6340 need_wait_return = FALSE;
6341
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006342 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6344 {
6345 if (p_wc == TAB)
6346 {
6347 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6348 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6349 }
6350 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6351 }
6352
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006353 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6354 * sets 'textwidth' to 78). */
6355 curbuf->b_p_tw = 0;
6356
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 /* Fill the buffer with the history. */
6358 init_history();
6359 if (hislen > 0)
6360 {
6361 i = hisidx[histtype];
6362 if (i >= 0)
6363 {
6364 lnum = 0;
6365 do
6366 {
6367 if (++i == hislen)
6368 i = 0;
6369 if (history[histtype][i].hisstr != NULL)
6370 ml_append(lnum++, history[histtype][i].hisstr,
6371 (colnr_T)0, FALSE);
6372 }
6373 while (i != hisidx[histtype]);
6374 }
6375 }
6376
6377 /* Replace the empty last line with the current command-line and put the
6378 * cursor there. */
6379 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6380 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6381 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006382 changed_line_abv_curs();
6383 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006384 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
6386 /* Save the command line info, can be used recursively. */
6387 save_ccline = ccline;
6388 ccline.cmdbuff = NULL;
6389 ccline.cmdprompt = NULL;
6390
6391 /* No Ex mode here! */
6392 exmode_active = 0;
6393
6394 State = NORMAL;
6395# ifdef FEAT_MOUSE
6396 setmouse();
6397# endif
6398
6399# ifdef FEAT_AUTOCMD
6400 /* Trigger CmdwinEnter autocommands. */
6401 typestr[0] = cmdwin_type;
6402 typestr[1] = NUL;
6403 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006404 if (restart_edit != 0) /* autocmd with ":startinsert" */
6405 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406# endif
6407
6408 i = RedrawingDisabled;
6409 RedrawingDisabled = 0;
6410
6411 /*
6412 * Call the main loop until <CR> or CTRL-C is typed.
6413 */
6414 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006415 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416
6417 RedrawingDisabled = i;
6418
6419# ifdef FEAT_AUTOCMD
6420 /* Trigger CmdwinLeave autocommands. */
6421 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6422# endif
6423
Bram Moolenaarccc18222007-05-10 18:25:20 +00006424 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 ccline = save_ccline;
6426 cmdwin_type = 0;
6427
6428 exmode_active = save_exmode;
6429
Bram Moolenaarf9821062008-06-20 16:31:07 +00006430 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 * this happens! */
6432 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6433 {
6434 cmdwin_result = Ctrl_C;
6435 EMSG(_("E199: Active window or buffer deleted"));
6436 }
6437 else
6438 {
6439# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6440 /* autocmds may abort script processing */
6441 if (aborting() && cmdwin_result != K_IGNORE)
6442 cmdwin_result = Ctrl_C;
6443# endif
6444 /* Set the new command line from the cmdline buffer. */
6445 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006446 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006448 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6449
6450 if (histtype == HIST_CMD)
6451 {
6452 /* Execute the command directly. */
6453 ccline.cmdbuff = vim_strsave((char_u *)p);
6454 cmdwin_result = CAR;
6455 }
6456 else
6457 {
6458 /* First need to cancel what we were doing. */
6459 ccline.cmdbuff = NULL;
6460 stuffcharReadbuff(':');
6461 stuffReadbuff((char_u *)p);
6462 stuffcharReadbuff(CAR);
6463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 }
6465 else if (cmdwin_result == K_XF2) /* :qa typed */
6466 {
6467 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6468 cmdwin_result = CAR;
6469 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006470 else if (cmdwin_result == Ctrl_C)
6471 {
6472 /* :q or :close, don't execute any command
6473 * and don't modify the cmd window. */
6474 ccline.cmdbuff = NULL;
6475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 else
6477 ccline.cmdbuff = vim_strsave(ml_get_curline());
6478 if (ccline.cmdbuff == NULL)
6479 cmdwin_result = Ctrl_C;
6480 else
6481 {
6482 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6483 ccline.cmdbufflen = ccline.cmdlen + 1;
6484 ccline.cmdpos = curwin->w_cursor.col;
6485 if (ccline.cmdpos > ccline.cmdlen)
6486 ccline.cmdpos = ccline.cmdlen;
6487 if (cmdwin_result == K_IGNORE)
6488 {
6489 set_cmdspos_cursor();
6490 redrawcmd();
6491 }
6492 }
6493
6494# ifdef FEAT_AUTOCMD
6495 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006496 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497# endif
6498 wp = curwin;
6499 bp = curbuf;
6500 win_goto(old_curwin);
6501 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006502
6503 /* win_close() may have already wiped the buffer when 'bh' is
6504 * set to 'wipe' */
6505 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006506 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508 /* Restore window sizes. */
6509 win_size_restore(&winsizes);
6510
6511# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006512 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513# endif
6514 }
6515
6516 ga_clear(&winsizes);
6517 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006518# ifdef FEAT_RIGHTLEFT
6519 cmdmsg_rl = save_cmdmsg_rl;
6520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
6522 State = save_State;
6523# ifdef FEAT_MOUSE
6524 setmouse();
6525# endif
6526
6527 return cmdwin_result;
6528}
6529#endif /* FEAT_CMDWIN */
6530
6531/*
6532 * Used for commands that either take a simple command string argument, or:
6533 * cmd << endmarker
6534 * {script}
6535 * endmarker
6536 * Returns a pointer to allocated memory with {script} or NULL.
6537 */
6538 char_u *
6539script_get(eap, cmd)
6540 exarg_T *eap;
6541 char_u *cmd;
6542{
6543 char_u *theline;
6544 char *end_pattern = NULL;
6545 char dot[] = ".";
6546 garray_T ga;
6547
6548 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6549 return NULL;
6550
6551 ga_init2(&ga, 1, 0x400);
6552
6553 if (cmd[2] != NUL)
6554 end_pattern = (char *)skipwhite(cmd + 2);
6555 else
6556 end_pattern = dot;
6557
6558 for (;;)
6559 {
6560 theline = eap->getline(
6561#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006562 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563#endif
6564 NUL, eap->cookie, 0);
6565
6566 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006567 {
6568 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572 ga_concat(&ga, theline);
6573 ga_append(&ga, '\n');
6574 vim_free(theline);
6575 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006576 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578 return (char_u *)ga.ga_data;
6579}