blob: 35b131d851202dc0280f621a638ac4204c1495b2 [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 */
28 int cmdfirstc; /* ':', '/', '?', '=' or NUL */
29 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 Moolenaarbfd8fc02005-09-20 23:22:24 +000034 int xp_context; /* type of expansion */
35# ifdef FEAT_EVAL
36 char_u *xp_arg; /* user-defined expansion arg */
37 int input_fn; /* Invoked for input() function */
38# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000039};
40
41static struct cmdline_info ccline; /* current cmdline_info */
42
43static int cmd_showtail; /* Only show path tail in lists ? */
44
45#ifdef FEAT_EVAL
46static int new_cmdpos; /* position set by set_cmdline_pos() */
47#endif
48
49#ifdef FEAT_CMDHIST
50typedef struct hist_entry
51{
52 int hisnum; /* identifying number */
53 char_u *hisstr; /* actual entry, separator char after the NUL */
54} histentry_T;
55
56static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
57static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
58static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
59 /* identifying (unique) number of newest history entry */
60static int hislen = 0; /* actual length of history tables */
61
62static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
64static int in_history __ARGS((int, char_u *, int));
65# ifdef FEAT_EVAL
66static int calc_hist_idx __ARGS((int histype, int num));
67# endif
68#endif
69
70#ifdef FEAT_RIGHTLEFT
71static int cmd_hkmap = 0; /* Hebrew mapping during command line */
72#endif
73
74#ifdef FEAT_FKMAP
75static int cmd_fkmap = 0; /* Farsi mapping during command line */
76#endif
77
78static int cmdline_charsize __ARGS((int idx));
79static void set_cmdspos __ARGS((void));
80static void set_cmdspos_cursor __ARGS((void));
81#ifdef FEAT_MBYTE
82static void correct_cmdspos __ARGS((int idx, int cells));
83#endif
84static void alloc_cmdbuff __ARGS((int len));
85static int realloc_cmdbuff __ARGS((int len));
86static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000087static void save_cmdline __ARGS((struct cmdline_info *ccp));
88static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar8299df92004-07-10 09:47:34 +000089static int cmdline_paste __ARGS((int regname, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
91static void redrawcmd_preedit __ARGS((void));
92#endif
93#ifdef FEAT_WILDMENU
94static void cmdline_del __ARGS((int from));
95#endif
96static void redrawcmdprompt __ARGS((void));
97static void cursorcmd __ARGS((void));
98static int ccheck_abbr __ARGS((int));
99static int nextwild __ARGS((expand_T *xp, int type, int options));
Bram Moolenaar45360022005-07-21 21:08:21 +0000100static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static int showmatches __ARGS((expand_T *xp, int wildmenu));
102static void set_expand_context __ARGS((expand_T *xp));
103static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
104static int expand_showtail __ARGS((expand_T *xp));
105#ifdef FEAT_CMDL_COMPL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000106static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
108# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
109static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000110static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111# endif
112#endif
113
114#ifdef FEAT_CMDWIN
115static int ex_window __ARGS((void));
116#endif
117
118/*
119 * getcmdline() - accept a command line starting with firstc.
120 *
121 * firstc == ':' get ":" command line.
122 * firstc == '/' or '?' get search pattern
123 * firstc == '=' get expression
124 * firstc == '@' get text for input() function
125 * firstc == '>' get text for debug mode
126 * firstc == NUL get text for :insert command
127 * firstc == -1 like NUL, and break on CTRL-C
128 *
129 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
130 * command line.
131 *
132 * Careful: getcmdline() can be called recursively!
133 *
134 * Return pointer to allocated string if there is a commandline, NULL
135 * otherwise.
136 */
137/*ARGSUSED*/
138 char_u *
139getcmdline(firstc, count, indent)
140 int firstc;
141 long count; /* only used for incremental search */
142 int indent; /* indent for inside conditionals */
143{
144 int c;
145 int i;
146 int j;
147 int gotesc = FALSE; /* TRUE when <ESC> just typed */
148 int do_abbr; /* when TRUE check for abbr. */
149#ifdef FEAT_CMDHIST
150 char_u *lookfor = NULL; /* string to match */
151 int hiscnt; /* current history line in use */
152 int histype; /* history type to be used */
153#endif
154#ifdef FEAT_SEARCH_EXTRA
155 pos_T old_cursor;
156 colnr_T old_curswant;
157 colnr_T old_leftcol;
158 linenr_T old_topline;
159# ifdef FEAT_DIFF
160 int old_topfill;
161# endif
162 linenr_T old_botline;
163 int did_incsearch = FALSE;
164 int incsearch_postponed = FALSE;
165#endif
166 int did_wild_list = FALSE; /* did wild_list() recently */
167 int wim_index = 0; /* index in wim_flags[] */
168 int res;
169 int save_msg_scroll = msg_scroll;
170 int save_State = State; /* remember State when called */
171 int some_key_typed = FALSE; /* one of the keys was typed */
172#ifdef FEAT_MOUSE
173 /* mouse drag and release events are ignored, unless they are
174 * preceded with a mouse down event */
175 int ignore_drag_release = TRUE;
176#endif
177#ifdef FEAT_EVAL
178 int break_ctrl_c = FALSE;
179#endif
180 expand_T xpc;
181 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000182#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
183 /* Everything that may work recursively should save and restore the
184 * current command line in save_ccline. That includes update_screen(), a
185 * custom status line may invoke ":normal". */
186 struct cmdline_info save_ccline;
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189#ifdef FEAT_SNIFF
190 want_sniff_request = 0;
191#endif
192#ifdef FEAT_EVAL
193 if (firstc == -1)
194 {
195 firstc = NUL;
196 break_ctrl_c = TRUE;
197 }
198#endif
199#ifdef FEAT_RIGHTLEFT
200 /* start without Hebrew mapping for a command line */
201 if (firstc == ':' || firstc == '=' || firstc == '>')
202 cmd_hkmap = 0;
203#endif
204
205 ccline.overstrike = FALSE; /* always start in insert mode */
206#ifdef FEAT_SEARCH_EXTRA
207 old_cursor = curwin->w_cursor; /* needs to be restored later */
208 old_curswant = curwin->w_curswant;
209 old_leftcol = curwin->w_leftcol;
210 old_topline = curwin->w_topline;
211# ifdef FEAT_DIFF
212 old_topfill = curwin->w_topfill;
213# endif
214 old_botline = curwin->w_botline;
215#endif
216
217 /*
218 * set some variables for redrawcmd()
219 */
220 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000221 ccline.cmdindent = (firstc > 0 ? indent : 0);
222
223 /* alloc initial ccline.cmdbuff */
224 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if (ccline.cmdbuff == NULL)
226 return NULL; /* out of memory */
227 ccline.cmdlen = ccline.cmdpos = 0;
228 ccline.cmdbuff[0] = NUL;
229
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000230 /* autoindent for :insert and :append */
231 if (firstc <= 0)
232 {
233 copy_spaces(ccline.cmdbuff, indent);
234 ccline.cmdbuff[indent] = NUL;
235 ccline.cmdpos = indent;
236 ccline.cmdspos = indent;
237 ccline.cmdlen = indent;
238 }
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 ExpandInit(&xpc);
241
242#ifdef FEAT_RIGHTLEFT
243 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
244 && (firstc == '/' || firstc == '?'))
245 cmdmsg_rl = TRUE;
246 else
247 cmdmsg_rl = FALSE;
248#endif
249
250 redir_off = TRUE; /* don't redirect the typed command */
251 if (!cmd_silent)
252 {
253 i = msg_scrolled;
254 msg_scrolled = 0; /* avoid wait_return message */
255 gotocmdline(TRUE);
256 msg_scrolled += i;
257 redrawcmdprompt(); /* draw prompt or indent */
258 set_cmdspos();
259 }
260 xpc.xp_context = EXPAND_NOTHING;
261 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000262#ifndef BACKSLASH_IN_FILENAME
263 xpc.xp_shell = FALSE;
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000266#if defined(FEAT_EVAL)
267 if (ccline.input_fn)
268 {
269 xpc.xp_context = ccline.xp_context;
270 xpc.xp_pattern = ccline.cmdbuff;
271 xpc.xp_arg = ccline.xp_arg;
272 }
273#endif
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 /*
276 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
277 * doing ":@0" when register 0 doesn't contain a CR.
278 */
279 msg_scroll = FALSE;
280
281 State = CMDLINE;
282
283 if (firstc == '/' || firstc == '?' || firstc == '@')
284 {
285 /* Use ":lmap" mappings for search pattern and input(). */
286 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
287 b_im_ptr = &curbuf->b_p_iminsert;
288 else
289 b_im_ptr = &curbuf->b_p_imsearch;
290 if (*b_im_ptr == B_IMODE_LMAP)
291 State |= LANGMAP;
292#ifdef USE_IM_CONTROL
293 im_set_active(*b_im_ptr == B_IMODE_IM);
294#endif
295 }
296#ifdef USE_IM_CONTROL
297 else if (p_imcmdline)
298 im_set_active(TRUE);
299#endif
300
301#ifdef FEAT_MOUSE
302 setmouse();
303#endif
304#ifdef CURSOR_SHAPE
305 ui_cursor_shape(); /* may show different cursor shape */
306#endif
307
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000308 /* When inside an autocommand for writing "exiting" may be set and
309 * terminal mode set to cooked. Need to set raw mode here then. */
310 settmode(TMODE_RAW);
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#ifdef FEAT_CMDHIST
313 init_history();
314 hiscnt = hislen; /* set hiscnt to impossible history value */
315 histype = hist_char2type(firstc);
316#endif
317
318#ifdef FEAT_DIGRAPHS
319 do_digraph(-1); /* init digraph typahead */
320#endif
321
322 /*
323 * Collect the command string, handling editing keys.
324 */
325 for (;;)
326 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000327 redir_off = TRUE; /* Don't redirect the typed command.
328 Repeated, because a ":redir" inside
329 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef USE_ON_FLY_SCROLL
331 dont_scroll = FALSE; /* allow scrolling here */
332#endif
333 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
334
335 cursorcmd(); /* set the cursor on the right spot */
336 c = safe_vgetc();
337 if (KeyTyped)
338 {
339 some_key_typed = TRUE;
340#ifdef FEAT_RIGHTLEFT
341 if (cmd_hkmap)
342 c = hkmap(c);
343# ifdef FEAT_FKMAP
344 if (cmd_fkmap)
345 c = cmdl_fkmap(c);
346# endif
347 if (cmdmsg_rl && !KeyStuffed)
348 {
349 /* Invert horizontal movements and operations. Only when
350 * typed by the user directly, not when the result of a
351 * mapping. */
352 switch (c)
353 {
354 case K_RIGHT: c = K_LEFT; break;
355 case K_S_RIGHT: c = K_S_LEFT; break;
356 case K_C_RIGHT: c = K_C_LEFT; break;
357 case K_LEFT: c = K_RIGHT; break;
358 case K_S_LEFT: c = K_S_RIGHT; break;
359 case K_C_LEFT: c = K_C_RIGHT; break;
360 }
361 }
362#endif
363 }
364
365 /*
366 * Ignore got_int when CTRL-C was typed here.
367 * Don't ignore it in :global, we really need to break then, e.g., for
368 * ":g/pat/normal /pat" (without the <CR>).
369 * Don't ignore it for the input() function.
370 */
371 if ((c == Ctrl_C
372#ifdef UNIX
373 || c == intr_char
374#endif
375 )
376#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
377 && firstc != '@'
378#endif
379#ifdef FEAT_EVAL
380 && !break_ctrl_c
381#endif
382 && !global_busy)
383 got_int = FALSE;
384
385#ifdef FEAT_CMDHIST
386 /* free old command line when finished moving around in the history
387 * list */
388 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000389 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000390 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 && c != K_PAGEDOWN && c != K_PAGEUP
392 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000393 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
395 {
396 vim_free(lookfor);
397 lookfor = NULL;
398 }
399#endif
400
401 /*
402 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
403 */
404 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
405 c = Ctrl_P;
406
407#ifdef FEAT_WILDMENU
408 /* Special translations for 'wildmenu' */
409 if (did_wild_list && p_wmnu)
410 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000411 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000413 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 c = Ctrl_N;
415 }
416 /* Hitting CR after "emenu Name.": complete submenu */
417 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
418 && ccline.cmdpos > 1
419 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
420 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
421 && (c == '\n' || c == '\r' || c == K_KENTER))
422 c = K_DOWN;
423#endif
424
425 /* free expanded names when finished walking through matches */
426 if (xpc.xp_numfiles != -1
427 && !(c == p_wc && KeyTyped) && c != p_wcm
428 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
429 && c != Ctrl_L)
430 {
431 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
432 did_wild_list = FALSE;
433#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000434 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435#endif
436 xpc.xp_context = EXPAND_NOTHING;
437 wim_index = 0;
438#ifdef FEAT_WILDMENU
439 if (p_wmnu && wild_menu_showing != 0)
440 {
441 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000442 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000443
444 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000445 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447 if (wild_menu_showing == WM_SCROLLED)
448 {
449 /* Entered command line, move it up */
450 cmdline_row--;
451 redrawcmd();
452 }
453 else if (save_p_ls != -1)
454 {
455 /* restore 'laststatus' and 'winminheight' */
456 p_ls = save_p_ls;
457 p_wmh = save_p_wmh;
458 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000459 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000461 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 redrawcmd();
463 save_p_ls = -1;
464 }
465 else
466 {
467# ifdef FEAT_VERTSPLIT
468 win_redraw_last_status(topframe);
469# else
470 lastwin->w_redr_status = TRUE;
471# endif
472 redraw_statuslines();
473 }
474 KeyTyped = skt;
475 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000476 if (ccline.input_fn)
477 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 }
479#endif
480 }
481
482#ifdef FEAT_WILDMENU
483 /* Special translations for 'wildmenu' */
484 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
485 {
486 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000487 if (ccline.cmdbuff[ccline.cmdpos - 1] == '.' && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000489 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 {
491 /* Hitting <Up>: Remove one submenu name in front of the
492 * cursor */
493 int found = FALSE;
494
495 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
496 i = 0;
497 while (--j > 0)
498 {
499 /* check for start of menu name */
500 if (ccline.cmdbuff[j] == ' '
501 && ccline.cmdbuff[j - 1] != '\\')
502 {
503 i = j + 1;
504 break;
505 }
506 /* check for start of submenu name */
507 if (ccline.cmdbuff[j] == '.'
508 && ccline.cmdbuff[j - 1] != '\\')
509 {
510 if (found)
511 {
512 i = j + 1;
513 break;
514 }
515 else
516 found = TRUE;
517 }
518 }
519 if (i > 0)
520 cmdline_del(i);
521 c = p_wc;
522 xpc.xp_context = EXPAND_NOTHING;
523 }
524 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000525 if ((xpc.xp_context == EXPAND_FILES
526 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
528 char_u upseg[5];
529
530 upseg[0] = PATHSEP;
531 upseg[1] = '.';
532 upseg[2] = '.';
533 upseg[3] = PATHSEP;
534 upseg[4] = NUL;
535
536 if (ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000537 && c == K_DOWN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 && (ccline.cmdbuff[ccline.cmdpos - 2] != '.'
539 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
540 {
541 /* go down a directory */
542 c = p_wc;
543 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000544 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
546 /* If in a direct ancestor, strip off one ../ to go down */
547 int found = FALSE;
548
549 j = ccline.cmdpos;
550 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
551 while (--j > i)
552 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000553#ifdef FEAT_MBYTE
554 if (has_mbyte)
555 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (vim_ispathsep(ccline.cmdbuff[j]))
558 {
559 found = TRUE;
560 break;
561 }
562 }
563 if (found
564 && ccline.cmdbuff[j - 1] == '.'
565 && ccline.cmdbuff[j - 2] == '.'
566 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
567 {
568 cmdline_del(j - 2);
569 c = p_wc;
570 }
571 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000572 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 /* go up a directory */
575 int found = FALSE;
576
577 j = ccline.cmdpos - 1;
578 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
579 while (--j > i)
580 {
581#ifdef FEAT_MBYTE
582 if (has_mbyte)
583 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
584#endif
585 if (vim_ispathsep(ccline.cmdbuff[j])
586#ifdef BACKSLASH_IN_FILENAME
587 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
588 == NULL
589#endif
590 )
591 {
592 if (found)
593 {
594 i = j + 1;
595 break;
596 }
597 else
598 found = TRUE;
599 }
600 }
601
602 if (!found)
603 j = i;
604 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
605 j += 4;
606 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
607 && j == i)
608 j += 3;
609 else
610 j = 0;
611 if (j > 0)
612 {
613 /* TODO this is only for DOS/UNIX systems - need to put in
614 * machine-specific stuff here and in upseg init */
615 cmdline_del(j);
616 put_on_cmdline(upseg + 1, 3, FALSE);
617 }
618 else if (ccline.cmdpos > i)
619 cmdline_del(i);
620 c = p_wc;
621 }
622 }
623#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
624 if (p_wmnu
625 && (xpc.xp_context == EXPAND_FILES
626 || xpc.xp_context == EXPAND_MENUNAMES)
627 && (c == K_UP || c == K_DOWN))
628 xpc.xp_context = EXPAND_NOTHING;
629#endif
630
631#endif /* FEAT_WILDMENU */
632
633 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
634 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
635 if (c == Ctrl_BSL)
636 {
637 ++no_mapping;
638 ++allow_keys;
639 c = safe_vgetc();
640 --no_mapping;
641 --allow_keys;
642 /* CTRL-\ e doesn't work when obtaining an expression. */
643 if (c != Ctrl_N && c != Ctrl_G
644 && (c != 'e' || ccline.cmdfirstc == '='))
645 {
646 vungetc(c);
647 c = Ctrl_BSL;
648 }
649#ifdef FEAT_EVAL
650 else if (c == 'e')
651 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000652 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
654 /*
655 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000656 * Need to save and restore the current command line, to be
657 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 */
659 if (ccline.cmdpos == ccline.cmdlen)
660 new_cmdpos = 99999; /* keep it at the end */
661 else
662 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000663
664 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000666 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 if (c == '=')
668 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000669 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000670 * to avoid nasty things like going to another buffer when
671 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000672 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000673 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000675 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000676 restore_cmdline(&save_ccline);
677
678 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000680 ccline.cmdlen = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 STRCPY(ccline.cmdbuff, p);
682 vim_free(p);
683
684 /* Restore the cursor or use the position set with
685 * set_cmdline_pos(). */
686 if (new_cmdpos > ccline.cmdlen)
687 ccline.cmdpos = ccline.cmdlen;
688 else
689 ccline.cmdpos = new_cmdpos;
690
691 KeyTyped = FALSE; /* Don't do p_wc completion. */
692 redrawcmd();
693 goto cmdline_changed;
694 }
695 }
696 beep_flush();
697 c = ESC;
698 }
699#endif
700 else
701 {
702 if (c == Ctrl_G && p_im && restart_edit == 0)
703 restart_edit = 'a';
704 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
705 in history */
706 goto returncmd; /* back to Normal mode */
707 }
708 }
709
710#ifdef FEAT_CMDWIN
711 if (c == cedit_key || c == K_CMDWIN)
712 {
713 /*
714 * Open a window to edit the command line (and history).
715 */
716 c = ex_window();
717 some_key_typed = TRUE;
718 }
719# ifdef FEAT_DIGRAPHS
720 else
721# endif
722#endif
723#ifdef FEAT_DIGRAPHS
724 c = do_digraph(c);
725#endif
726
727 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
728 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
729 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000730 /* In Ex mode a backslash escapes a newline. */
731 if (exmode_active
732 && c != ESC
733 && ccline.cmdpos > 0
734 && ccline.cmdpos == ccline.cmdlen
735 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000737 if (c == K_KENTER)
738 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000740 else
741 {
742 gotesc = FALSE; /* Might have typed ESC previously, don't
743 truncate the cmdline now. */
744 if (ccheck_abbr(c + ABBR_OFF))
745 goto cmdline_changed;
746 if (!cmd_silent)
747 {
748 windgoto(msg_row, 0);
749 out_flush();
750 }
751 break;
752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754
755 /*
756 * Completion for 'wildchar' or 'wildcharm' key.
757 * - hitting <ESC> twice means: abandon command line.
758 * - wildcard expansion is only done when the 'wildchar' key is really
759 * typed, not when it comes from a macro
760 */
761 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
762 {
763 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
764 {
765 /* if 'wildmode' contains "list" may still need to list */
766 if (xpc.xp_numfiles > 1
767 && !did_wild_list
768 && (wim_flags[wim_index] & WIM_LIST))
769 {
770 (void)showmatches(&xpc, FALSE);
771 redrawcmd();
772 did_wild_list = TRUE;
773 }
774 if (wim_flags[wim_index] & WIM_LONGEST)
775 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
776 else if (wim_flags[wim_index] & WIM_FULL)
777 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
778 else
779 res = OK; /* don't insert 'wildchar' now */
780 }
781 else /* typed p_wc first time */
782 {
783 wim_index = 0;
784 j = ccline.cmdpos;
785 /* if 'wildmode' first contains "longest", get longest
786 * common part */
787 if (wim_flags[0] & WIM_LONGEST)
788 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
789 else
790 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
791
792 /* if interrupted while completing, behave like it failed */
793 if (got_int)
794 {
795 (void)vpeekc(); /* remove <C-C> from input stream */
796 got_int = FALSE; /* don't abandon the command line */
797 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
798#ifdef FEAT_WILDMENU
799 xpc.xp_context = EXPAND_NOTHING;
800#endif
801 goto cmdline_changed;
802 }
803
804 /* when more than one match, and 'wildmode' first contains
805 * "list", or no change and 'wildmode' contains "longest,list",
806 * list all matches */
807 if (res == OK && xpc.xp_numfiles > 1)
808 {
809 /* a "longest" that didn't do anything is skipped (but not
810 * "list:longest") */
811 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
812 wim_index = 1;
813 if ((wim_flags[wim_index] & WIM_LIST)
814#ifdef FEAT_WILDMENU
815 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
816#endif
817 )
818 {
819 if (!(wim_flags[0] & WIM_LONGEST))
820 {
821#ifdef FEAT_WILDMENU
822 int p_wmnu_save = p_wmnu;
823 p_wmnu = 0;
824#endif
825 nextwild(&xpc, WILD_PREV, 0); /* remove match */
826#ifdef FEAT_WILDMENU
827 p_wmnu = p_wmnu_save;
828#endif
829 }
830#ifdef FEAT_WILDMENU
831 (void)showmatches(&xpc, p_wmnu
832 && ((wim_flags[wim_index] & WIM_LIST) == 0));
833#else
834 (void)showmatches(&xpc, FALSE);
835#endif
836 redrawcmd();
837 did_wild_list = TRUE;
838 if (wim_flags[wim_index] & WIM_LONGEST)
839 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
840 else if (wim_flags[wim_index] & WIM_FULL)
841 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
842 }
843 else
844 vim_beep();
845 }
846#ifdef FEAT_WILDMENU
847 else if (xpc.xp_numfiles == -1)
848 xpc.xp_context = EXPAND_NOTHING;
849#endif
850 }
851 if (wim_index < 3)
852 ++wim_index;
853 if (c == ESC)
854 gotesc = TRUE;
855 if (res == OK)
856 goto cmdline_changed;
857 }
858
859 gotesc = FALSE;
860
861 /* <S-Tab> goes to last match, in a clumsy way */
862 if (c == K_S_TAB && KeyTyped)
863 {
864 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
865 && nextwild(&xpc, WILD_PREV, 0) == OK
866 && nextwild(&xpc, WILD_PREV, 0) == OK)
867 goto cmdline_changed;
868 }
869
870 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
871 c = NL;
872
873 do_abbr = TRUE; /* default: check for abbreviation */
874
875 /*
876 * Big switch for a typed command line character.
877 */
878 switch (c)
879 {
880 case K_BS:
881 case Ctrl_H:
882 case K_DEL:
883 case K_KDEL:
884 case Ctrl_W:
885#ifdef FEAT_FKMAP
886 if (cmd_fkmap && c == K_BS)
887 c = K_DEL;
888#endif
889 if (c == K_KDEL)
890 c = K_DEL;
891
892 /*
893 * delete current character is the same as backspace on next
894 * character, except at end of line
895 */
896 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
897 ++ccline.cmdpos;
898#ifdef FEAT_MBYTE
899 if (has_mbyte && c == K_DEL)
900 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
901 ccline.cmdbuff + ccline.cmdpos);
902#endif
903 if (ccline.cmdpos > 0)
904 {
905 char_u *p;
906
907 j = ccline.cmdpos;
908 p = ccline.cmdbuff + j;
909#ifdef FEAT_MBYTE
910 if (has_mbyte)
911 {
912 p = mb_prevptr(ccline.cmdbuff, p);
913 if (c == Ctrl_W)
914 {
915 while (p > ccline.cmdbuff && vim_isspace(*p))
916 p = mb_prevptr(ccline.cmdbuff, p);
917 i = mb_get_class(p);
918 while (p > ccline.cmdbuff && mb_get_class(p) == i)
919 p = mb_prevptr(ccline.cmdbuff, p);
920 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000921 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923 }
924 else
925#endif
926 if (c == Ctrl_W)
927 {
928 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
929 --p;
930 i = vim_iswordc(p[-1]);
931 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
932 && vim_iswordc(p[-1]) == i)
933 --p;
934 }
935 else
936 --p;
937 ccline.cmdpos = (int)(p - ccline.cmdbuff);
938 ccline.cmdlen -= j - ccline.cmdpos;
939 i = ccline.cmdpos;
940 while (i < ccline.cmdlen)
941 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
942
943 /* Truncate at the end, required for multi-byte chars. */
944 ccline.cmdbuff[ccline.cmdlen] = NUL;
945 redrawcmd();
946 }
947 else if (ccline.cmdlen == 0 && c != Ctrl_W
948 && ccline.cmdprompt == NULL && indent == 0)
949 {
950 /* In ex and debug mode it doesn't make sense to return. */
951 if (exmode_active
952#ifdef FEAT_EVAL
953 || ccline.cmdfirstc == '>'
954#endif
955 )
956 goto cmdline_not_changed;
957
958 vim_free(ccline.cmdbuff); /* no commandline to return */
959 ccline.cmdbuff = NULL;
960 if (!cmd_silent)
961 {
962#ifdef FEAT_RIGHTLEFT
963 if (cmdmsg_rl)
964 msg_col = Columns;
965 else
966#endif
967 msg_col = 0;
968 msg_putchar(' '); /* delete ':' */
969 }
970 redraw_cmdline = TRUE;
971 goto returncmd; /* back to cmd mode */
972 }
973 goto cmdline_changed;
974
975 case K_INS:
976 case K_KINS:
977#ifdef FEAT_FKMAP
978 /* if Farsi mode set, we are in reverse insert mode -
979 Do not change the mode */
980 if (cmd_fkmap)
981 beep_flush();
982 else
983#endif
984 ccline.overstrike = !ccline.overstrike;
985#ifdef CURSOR_SHAPE
986 ui_cursor_shape(); /* may show different cursor shape */
987#endif
988 goto cmdline_not_changed;
989
990 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000991 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
993 /* ":lmap" mappings exists, toggle use of mappings. */
994 State ^= LANGMAP;
995#ifdef USE_IM_CONTROL
996 im_set_active(FALSE); /* Disable input method */
997#endif
998 if (b_im_ptr != NULL)
999 {
1000 if (State & LANGMAP)
1001 *b_im_ptr = B_IMODE_LMAP;
1002 else
1003 *b_im_ptr = B_IMODE_NONE;
1004 }
1005 }
1006#ifdef USE_IM_CONTROL
1007 else
1008 {
1009 /* There are no ":lmap" mappings, toggle IM. When
1010 * 'imdisable' is set don't try getting the status, it's
1011 * always off. */
1012 if ((p_imdisable && b_im_ptr != NULL)
1013 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1014 {
1015 im_set_active(FALSE); /* Disable input method */
1016 if (b_im_ptr != NULL)
1017 *b_im_ptr = B_IMODE_NONE;
1018 }
1019 else
1020 {
1021 im_set_active(TRUE); /* Enable input method */
1022 if (b_im_ptr != NULL)
1023 *b_im_ptr = B_IMODE_IM;
1024 }
1025 }
1026#endif
1027 if (b_im_ptr != NULL)
1028 {
1029 if (b_im_ptr == &curbuf->b_p_iminsert)
1030 set_iminsert_global();
1031 else
1032 set_imsearch_global();
1033 }
1034#ifdef CURSOR_SHAPE
1035 ui_cursor_shape(); /* may show different cursor shape */
1036#endif
1037#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1038 /* Show/unshow value of 'keymap' in status lines later. */
1039 status_redraw_curbuf();
1040#endif
1041 goto cmdline_not_changed;
1042
1043/* case '@': only in very old vi */
1044 case Ctrl_U:
1045 /* delete all characters left of the cursor */
1046 j = ccline.cmdpos;
1047 ccline.cmdlen -= j;
1048 i = ccline.cmdpos = 0;
1049 while (i < ccline.cmdlen)
1050 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1051 /* Truncate at the end, required for multi-byte chars. */
1052 ccline.cmdbuff[ccline.cmdlen] = NUL;
1053 redrawcmd();
1054 goto cmdline_changed;
1055
1056#ifdef FEAT_CLIPBOARD
1057 case Ctrl_Y:
1058 /* Copy the modeless selection, if there is one. */
1059 if (clip_star.state != SELECT_CLEARED)
1060 {
1061 if (clip_star.state == SELECT_DONE)
1062 clip_copy_modeless_selection(TRUE);
1063 goto cmdline_not_changed;
1064 }
1065 break;
1066#endif
1067
1068 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1069 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001070 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001071 * ":normal" runs out of characters. */
1072 if (exmode_active
1073#ifdef FEAT_EX_EXTRA
1074 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1075#endif
1076 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 goto cmdline_not_changed;
1078
1079 gotesc = TRUE; /* will free ccline.cmdbuff after
1080 putting it in history */
1081 goto returncmd; /* back to cmd mode */
1082
1083 case Ctrl_R: /* insert register */
1084#ifdef USE_ON_FLY_SCROLL
1085 dont_scroll = TRUE; /* disallow scrolling here */
1086#endif
1087 putcmdline('"', TRUE);
1088 ++no_mapping;
1089 i = c = safe_vgetc(); /* CTRL-R <char> */
1090 if (i == Ctrl_O)
1091 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1092 if (i == Ctrl_R)
1093 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1094 --no_mapping;
1095#ifdef FEAT_EVAL
1096 /*
1097 * Insert the result of an expression.
1098 * Need to save the current command line, to be able to enter
1099 * a new one...
1100 */
1101 new_cmdpos = -1;
1102 if (c == '=')
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1105 {
1106 beep_flush();
1107 c = ESC;
1108 }
1109 else
1110 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001111 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001113 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
1115 }
1116#endif
1117 if (c != ESC) /* use ESC to cancel inserting register */
1118 {
1119 cmdline_paste(c, i == Ctrl_R);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001120
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001121#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001122 /* When there was a serious error abort getting the
1123 * command line. */
1124 if (aborting())
1125 {
1126 gotesc = TRUE; /* will free ccline.cmdbuff after
1127 putting it in history */
1128 goto returncmd; /* back to cmd mode */
1129 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 KeyTyped = FALSE; /* Don't do p_wc completion. */
1132#ifdef FEAT_EVAL
1133 if (new_cmdpos >= 0)
1134 {
1135 /* set_cmdline_pos() was used */
1136 if (new_cmdpos > ccline.cmdlen)
1137 ccline.cmdpos = ccline.cmdlen;
1138 else
1139 ccline.cmdpos = new_cmdpos;
1140 }
1141#endif
1142 }
1143 redrawcmd();
1144 goto cmdline_changed;
1145
1146 case Ctrl_D:
1147 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1148 break; /* Use ^D as normal char instead */
1149
1150 redrawcmd();
1151 continue; /* don't do incremental search now */
1152
1153 case K_RIGHT:
1154 case K_S_RIGHT:
1155 case K_C_RIGHT:
1156 do
1157 {
1158 if (ccline.cmdpos >= ccline.cmdlen)
1159 break;
1160 i = cmdline_charsize(ccline.cmdpos);
1161 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1162 break;
1163 ccline.cmdspos += i;
1164#ifdef FEAT_MBYTE
1165 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001166 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 + ccline.cmdpos);
1168 else
1169#endif
1170 ++ccline.cmdpos;
1171 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001172 while ((c == K_S_RIGHT || c == K_C_RIGHT
1173 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1175#ifdef FEAT_MBYTE
1176 if (has_mbyte)
1177 set_cmdspos_cursor();
1178#endif
1179 goto cmdline_not_changed;
1180
1181 case K_LEFT:
1182 case K_S_LEFT:
1183 case K_C_LEFT:
1184 do
1185 {
1186 if (ccline.cmdpos == 0)
1187 break;
1188 --ccline.cmdpos;
1189#ifdef FEAT_MBYTE
1190 if (has_mbyte) /* move to first byte of char */
1191 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1192 ccline.cmdbuff + ccline.cmdpos);
1193#endif
1194 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1195 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001196 while ((c == K_S_LEFT || c == K_C_LEFT
1197 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1199#ifdef FEAT_MBYTE
1200 if (has_mbyte)
1201 set_cmdspos_cursor();
1202#endif
1203 goto cmdline_not_changed;
1204
1205 case K_IGNORE:
1206 goto cmdline_not_changed; /* Ignore mouse */
1207
Bram Moolenaar4770d092006-01-12 23:22:24 +00001208#ifdef FEAT_GUI_W32
1209 /* On Win32 ignore <M-F4>, we get it when closing the window was
1210 * cancelled. */
1211 case K_F4:
1212 if (mod_mask == MOD_MASK_ALT)
1213 {
1214 redrawcmd(); /* somehow the cmdline is cleared */
1215 goto cmdline_not_changed;
1216 }
1217 break;
1218#endif
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef FEAT_MOUSE
1221 case K_MIDDLEDRAG:
1222 case K_MIDDLERELEASE:
1223 goto cmdline_not_changed; /* Ignore mouse */
1224
1225 case K_MIDDLEMOUSE:
1226# ifdef FEAT_GUI
1227 /* When GUI is active, also paste when 'mouse' is empty */
1228 if (!gui.in_use)
1229# endif
1230 if (!mouse_has(MOUSE_COMMAND))
1231 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001232# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (clip_star.available)
1234 cmdline_paste('*', TRUE);
1235 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001236# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 cmdline_paste(0, TRUE);
1238 redrawcmd();
1239 goto cmdline_changed;
1240
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001241# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 case K_DROP:
1243 cmdline_paste('~', TRUE);
1244 redrawcmd();
1245 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
1248 case K_LEFTDRAG:
1249 case K_LEFTRELEASE:
1250 case K_RIGHTDRAG:
1251 case K_RIGHTRELEASE:
1252 /* Ignore drag and release events when the button-down wasn't
1253 * seen before. */
1254 if (ignore_drag_release)
1255 goto cmdline_not_changed;
1256 /* FALLTHROUGH */
1257 case K_LEFTMOUSE:
1258 case K_RIGHTMOUSE:
1259 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1260 ignore_drag_release = TRUE;
1261 else
1262 ignore_drag_release = FALSE;
1263# ifdef FEAT_GUI
1264 /* When GUI is active, also move when 'mouse' is empty */
1265 if (!gui.in_use)
1266# endif
1267 if (!mouse_has(MOUSE_COMMAND))
1268 goto cmdline_not_changed; /* Ignore mouse */
1269# ifdef FEAT_CLIPBOARD
1270 if (mouse_row < cmdline_row && clip_star.available)
1271 {
1272 int button, is_click, is_drag;
1273
1274 /*
1275 * Handle modeless selection.
1276 */
1277 button = get_mouse_button(KEY2TERMCAP1(c),
1278 &is_click, &is_drag);
1279 if (mouse_model_popup() && button == MOUSE_LEFT
1280 && (mod_mask & MOD_MASK_SHIFT))
1281 {
1282 /* Translate shift-left to right button. */
1283 button = MOUSE_RIGHT;
1284 mod_mask &= ~MOD_MASK_SHIFT;
1285 }
1286 clip_modeless(button, is_click, is_drag);
1287 goto cmdline_not_changed;
1288 }
1289# endif
1290
1291 set_cmdspos();
1292 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1293 ++ccline.cmdpos)
1294 {
1295 i = cmdline_charsize(ccline.cmdpos);
1296 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1297 && mouse_col < ccline.cmdspos % Columns + i)
1298 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001299# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (has_mbyte)
1301 {
1302 /* Count ">" for double-wide char that doesn't fit. */
1303 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001304 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 + ccline.cmdpos) - 1;
1306 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 ccline.cmdspos += i;
1309 }
1310 goto cmdline_not_changed;
1311
1312 /* Mouse scroll wheel: ignored here */
1313 case K_MOUSEDOWN:
1314 case K_MOUSEUP:
1315 /* Alternate buttons ignored here */
1316 case K_X1MOUSE:
1317 case K_X1DRAG:
1318 case K_X1RELEASE:
1319 case K_X2MOUSE:
1320 case K_X2DRAG:
1321 case K_X2RELEASE:
1322 goto cmdline_not_changed;
1323
1324#endif /* FEAT_MOUSE */
1325
1326#ifdef FEAT_GUI
1327 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1328 case K_LEFTRELEASE_NM:
1329 goto cmdline_not_changed;
1330
1331 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001332 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 gui_do_scroll();
1335 redrawcmd();
1336 }
1337 goto cmdline_not_changed;
1338
1339 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001340 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
1342 gui_do_horiz_scroll();
1343 redrawcmd();
1344 }
1345 goto cmdline_not_changed;
1346#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001347#ifdef FEAT_GUI_TABLINE
1348 case K_TABLINE:
1349 case K_TABMENU:
1350 /* Don't want to change any tabs here. Make sure the same tab
1351 * is still selected. */
1352 if (gui_use_tabline())
1353 gui_mch_set_curtab(tabpage_index(curtab));
1354 goto cmdline_not_changed;
1355#endif
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 case K_SELECT: /* end of Select mode mapping - ignore */
1358 goto cmdline_not_changed;
1359
1360 case Ctrl_B: /* begin of command line */
1361 case K_HOME:
1362 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 case K_S_HOME:
1364 case K_C_HOME:
1365 ccline.cmdpos = 0;
1366 set_cmdspos();
1367 goto cmdline_not_changed;
1368
1369 case Ctrl_E: /* end of command line */
1370 case K_END:
1371 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 case K_S_END:
1373 case K_C_END:
1374 ccline.cmdpos = ccline.cmdlen;
1375 set_cmdspos_cursor();
1376 goto cmdline_not_changed;
1377
1378 case Ctrl_A: /* all matches */
1379 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1380 break;
1381 goto cmdline_changed;
1382
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001383 case Ctrl_L:
1384#ifdef FEAT_SEARCH_EXTRA
1385 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1386 {
1387 /* Add a character from under the cursor for 'incsearch' */
1388 if (did_incsearch
1389 && !equalpos(curwin->w_cursor, old_cursor))
1390 {
1391 c = gchar_cursor();
1392 if (c != NUL)
1393 break;
1394 }
1395 goto cmdline_not_changed;
1396 }
1397#endif
1398
1399 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1401 break;
1402 goto cmdline_changed;
1403
1404 case Ctrl_N: /* next match */
1405 case Ctrl_P: /* previous match */
1406 if (xpc.xp_numfiles > 0)
1407 {
1408 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1409 == FAIL)
1410 break;
1411 goto cmdline_changed;
1412 }
1413
1414#ifdef FEAT_CMDHIST
1415 case K_UP:
1416 case K_DOWN:
1417 case K_S_UP:
1418 case K_S_DOWN:
1419 case K_PAGEUP:
1420 case K_KPAGEUP:
1421 case K_PAGEDOWN:
1422 case K_KPAGEDOWN:
1423 if (hislen == 0 || firstc == NUL) /* no history */
1424 goto cmdline_not_changed;
1425
1426 i = hiscnt;
1427
1428 /* save current command string so it can be restored later */
1429 if (lookfor == NULL)
1430 {
1431 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1432 goto cmdline_not_changed;
1433 lookfor[ccline.cmdpos] = NUL;
1434 }
1435
1436 j = (int)STRLEN(lookfor);
1437 for (;;)
1438 {
1439 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001440 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001441 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 {
1443 if (hiscnt == hislen) /* first time */
1444 hiscnt = hisidx[histype];
1445 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1446 hiscnt = hislen - 1;
1447 else if (hiscnt != hisidx[histype] + 1)
1448 --hiscnt;
1449 else /* at top of list */
1450 {
1451 hiscnt = i;
1452 break;
1453 }
1454 }
1455 else /* one step forwards */
1456 {
1457 /* on last entry, clear the line */
1458 if (hiscnt == hisidx[histype])
1459 {
1460 hiscnt = hislen;
1461 break;
1462 }
1463
1464 /* not on a history line, nothing to do */
1465 if (hiscnt == hislen)
1466 break;
1467 if (hiscnt == hislen - 1) /* wrap around */
1468 hiscnt = 0;
1469 else
1470 ++hiscnt;
1471 }
1472 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1473 {
1474 hiscnt = i;
1475 break;
1476 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001477 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001478 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 || STRNCMP(history[histype][hiscnt].hisstr,
1480 lookfor, (size_t)j) == 0)
1481 break;
1482 }
1483
1484 if (hiscnt != i) /* jumped to other entry */
1485 {
1486 char_u *p;
1487 int len;
1488 int old_firstc;
1489
1490 vim_free(ccline.cmdbuff);
1491 if (hiscnt == hislen)
1492 p = lookfor; /* back to the old one */
1493 else
1494 p = history[histype][hiscnt].hisstr;
1495
1496 if (histype == HIST_SEARCH
1497 && p != lookfor
1498 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1499 {
1500 /* Correct for the separator character used when
1501 * adding the history entry vs the one used now.
1502 * First loop: count length.
1503 * Second loop: copy the characters. */
1504 for (i = 0; i <= 1; ++i)
1505 {
1506 len = 0;
1507 for (j = 0; p[j] != NUL; ++j)
1508 {
1509 /* Replace old sep with new sep, unless it is
1510 * escaped. */
1511 if (p[j] == old_firstc
1512 && (j == 0 || p[j - 1] != '\\'))
1513 {
1514 if (i > 0)
1515 ccline.cmdbuff[len] = firstc;
1516 }
1517 else
1518 {
1519 /* Escape new sep, unless it is already
1520 * escaped. */
1521 if (p[j] == firstc
1522 && (j == 0 || p[j - 1] != '\\'))
1523 {
1524 if (i > 0)
1525 ccline.cmdbuff[len] = '\\';
1526 ++len;
1527 }
1528 if (i > 0)
1529 ccline.cmdbuff[len] = p[j];
1530 }
1531 ++len;
1532 }
1533 if (i == 0)
1534 {
1535 alloc_cmdbuff(len);
1536 if (ccline.cmdbuff == NULL)
1537 goto returncmd;
1538 }
1539 }
1540 ccline.cmdbuff[len] = NUL;
1541 }
1542 else
1543 {
1544 alloc_cmdbuff((int)STRLEN(p));
1545 if (ccline.cmdbuff == NULL)
1546 goto returncmd;
1547 STRCPY(ccline.cmdbuff, p);
1548 }
1549
1550 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1551 redrawcmd();
1552 goto cmdline_changed;
1553 }
1554 beep_flush();
1555 goto cmdline_not_changed;
1556#endif
1557
1558 case Ctrl_V:
1559 case Ctrl_Q:
1560#ifdef FEAT_MOUSE
1561 ignore_drag_release = TRUE;
1562#endif
1563 putcmdline('^', TRUE);
1564 c = get_literal(); /* get next (two) character(s) */
1565 do_abbr = FALSE; /* don't do abbreviation now */
1566#ifdef FEAT_MBYTE
1567 /* may need to remove ^ when composing char was typed */
1568 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1569 {
1570 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1571 msg_putchar(' ');
1572 cursorcmd();
1573 }
1574#endif
1575 break;
1576
1577#ifdef FEAT_DIGRAPHS
1578 case Ctrl_K:
1579#ifdef FEAT_MOUSE
1580 ignore_drag_release = TRUE;
1581#endif
1582 putcmdline('?', TRUE);
1583#ifdef USE_ON_FLY_SCROLL
1584 dont_scroll = TRUE; /* disallow scrolling here */
1585#endif
1586 c = get_digraph(TRUE);
1587 if (c != NUL)
1588 break;
1589
1590 redrawcmd();
1591 goto cmdline_not_changed;
1592#endif /* FEAT_DIGRAPHS */
1593
1594#ifdef FEAT_RIGHTLEFT
1595 case Ctrl__: /* CTRL-_: switch language mode */
1596 if (!p_ari)
1597 break;
1598#ifdef FEAT_FKMAP
1599 if (p_altkeymap)
1600 {
1601 cmd_fkmap = !cmd_fkmap;
1602 if (cmd_fkmap) /* in Farsi always in Insert mode */
1603 ccline.overstrike = FALSE;
1604 }
1605 else /* Hebrew is default */
1606#endif
1607 cmd_hkmap = !cmd_hkmap;
1608 goto cmdline_not_changed;
1609#endif
1610
1611 default:
1612#ifdef UNIX
1613 if (c == intr_char)
1614 {
1615 gotesc = TRUE; /* will free ccline.cmdbuff after
1616 putting it in history */
1617 goto returncmd; /* back to Normal mode */
1618 }
1619#endif
1620 /*
1621 * Normal character with no special meaning. Just set mod_mask
1622 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1623 * the string <S-Space>. This should only happen after ^V.
1624 */
1625 if (!IS_SPECIAL(c))
1626 mod_mask = 0x0;
1627 break;
1628 }
1629 /*
1630 * End of switch on command line character.
1631 * We come here if we have a normal character.
1632 */
1633
1634 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1635#ifdef FEAT_MBYTE
1636 /* Add ABBR_OFF for characters above 0x100, this is
1637 * what check_abbr() expects. */
1638 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1639#endif
1640 c))
1641 goto cmdline_changed;
1642
1643 /*
1644 * put the character in the command line
1645 */
1646 if (IS_SPECIAL(c) || mod_mask != 0)
1647 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1648 else
1649 {
1650#ifdef FEAT_MBYTE
1651 if (has_mbyte)
1652 {
1653 j = (*mb_char2bytes)(c, IObuff);
1654 IObuff[j] = NUL; /* exclude composing chars */
1655 put_on_cmdline(IObuff, j, TRUE);
1656 }
1657 else
1658#endif
1659 {
1660 IObuff[0] = c;
1661 put_on_cmdline(IObuff, 1, TRUE);
1662 }
1663 }
1664 goto cmdline_changed;
1665
1666/*
1667 * This part implements incremental searches for "/" and "?"
1668 * Jump to cmdline_not_changed when a character has been read but the command
1669 * line did not change. Then we only search and redraw if something changed in
1670 * the past.
1671 * Jump to cmdline_changed when the command line did change.
1672 * (Sorry for the goto's, I know it is ugly).
1673 */
1674cmdline_not_changed:
1675#ifdef FEAT_SEARCH_EXTRA
1676 if (!incsearch_postponed)
1677 continue;
1678#endif
1679
1680cmdline_changed:
1681#ifdef FEAT_SEARCH_EXTRA
1682 /*
1683 * 'incsearch' highlighting.
1684 */
1685 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1686 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001687 pos_T end_pos;
1688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 /* if there is a character waiting, search and redraw later */
1690 if (char_avail())
1691 {
1692 incsearch_postponed = TRUE;
1693 continue;
1694 }
1695 incsearch_postponed = FALSE;
1696 curwin->w_cursor = old_cursor; /* start at old position */
1697
1698 /* If there is no command line, don't do anything */
1699 if (ccline.cmdlen == 0)
1700 i = 0;
1701 else
1702 {
1703 cursor_off(); /* so the user knows we're busy */
1704 out_flush();
1705 ++emsg_off; /* So it doesn't beep if bad expr */
1706 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1707 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1708 --emsg_off;
1709 /* if interrupted while searching, behave like it failed */
1710 if (got_int)
1711 {
1712 (void)vpeekc(); /* remove <C-C> from input stream */
1713 got_int = FALSE; /* don't abandon the command line */
1714 i = 0;
1715 }
1716 else if (char_avail())
1717 /* cancelled searching because a char was typed */
1718 incsearch_postponed = TRUE;
1719 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001720 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 highlight_match = TRUE; /* highlight position */
1722 else
1723 highlight_match = FALSE; /* remove highlight */
1724
1725 /* first restore the old curwin values, so the screen is
1726 * positioned in the same way as the actual search command */
1727 curwin->w_leftcol = old_leftcol;
1728 curwin->w_topline = old_topline;
1729# ifdef FEAT_DIFF
1730 curwin->w_topfill = old_topfill;
1731# endif
1732 curwin->w_botline = old_botline;
1733 changed_cline_bef_curs();
1734 update_topline();
1735
1736 if (i != 0)
1737 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001738 pos_T save_pos = curwin->w_cursor;
1739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001741 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 * moves the whole match onto the screen when 'nowrap' is set.
1743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 curwin->w_cursor.lnum += search_match_lines;
1745 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001746 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1747 {
1748 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1749 coladvance((colnr_T)MAXCOL);
1750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001752 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001753 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001755 else
1756 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 validate_cursor();
1759
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001760 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001761 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001762 restore_cmdline(&save_ccline);
1763
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001764 /* Leave it at the end to make CTRL-R CTRL-W work. */
1765 if (i != 0)
1766 curwin->w_cursor = end_pos;
1767
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 msg_starthere();
1769 redrawcmdline();
1770 did_incsearch = TRUE;
1771 }
1772#else /* FEAT_SEARCH_EXTRA */
1773 ;
1774#endif
1775
1776#ifdef FEAT_RIGHTLEFT
1777 if (cmdmsg_rl
1778# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001779 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780# endif
1781 )
1782 /* Always redraw the whole command line to fix shaping and
1783 * right-left typing. Not efficient, but it works. */
1784 redrawcmd();
1785#endif
1786 }
1787
1788returncmd:
1789
1790#ifdef FEAT_RIGHTLEFT
1791 cmdmsg_rl = FALSE;
1792#endif
1793
1794#ifdef FEAT_FKMAP
1795 cmd_fkmap = 0;
1796#endif
1797
1798 ExpandCleanup(&xpc);
1799
1800#ifdef FEAT_SEARCH_EXTRA
1801 if (did_incsearch)
1802 {
1803 curwin->w_cursor = old_cursor;
1804 curwin->w_curswant = old_curswant;
1805 curwin->w_leftcol = old_leftcol;
1806 curwin->w_topline = old_topline;
1807# ifdef FEAT_DIFF
1808 curwin->w_topfill = old_topfill;
1809# endif
1810 curwin->w_botline = old_botline;
1811 highlight_match = FALSE;
1812 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001813 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815#endif
1816
1817 if (ccline.cmdbuff != NULL)
1818 {
1819 /*
1820 * Put line in history buffer (":" and "=" only when it was typed).
1821 */
1822#ifdef FEAT_CMDHIST
1823 if (ccline.cmdlen && firstc != NUL
1824 && (some_key_typed || histype == HIST_SEARCH))
1825 {
1826 add_to_history(histype, ccline.cmdbuff, TRUE,
1827 histype == HIST_SEARCH ? firstc : NUL);
1828 if (firstc == ':')
1829 {
1830 vim_free(new_last_cmdline);
1831 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1832 }
1833 }
1834#endif
1835
1836 if (gotesc) /* abandon command line */
1837 {
1838 vim_free(ccline.cmdbuff);
1839 ccline.cmdbuff = NULL;
1840 if (msg_scrolled == 0)
1841 compute_cmdrow();
1842 MSG("");
1843 redraw_cmdline = TRUE;
1844 }
1845 }
1846
1847 /*
1848 * If the screen was shifted up, redraw the whole screen (later).
1849 * If the line is too long, clear it, so ruler and shown command do
1850 * not get printed in the middle of it.
1851 */
1852 msg_check();
1853 msg_scroll = save_msg_scroll;
1854 redir_off = FALSE;
1855
1856 /* When the command line was typed, no need for a wait-return prompt. */
1857 if (some_key_typed)
1858 need_wait_return = FALSE;
1859
1860 State = save_State;
1861#ifdef USE_IM_CONTROL
1862 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1863 im_save_status(b_im_ptr);
1864 im_set_active(FALSE);
1865#endif
1866#ifdef FEAT_MOUSE
1867 setmouse();
1868#endif
1869#ifdef CURSOR_SHAPE
1870 ui_cursor_shape(); /* may show different cursor shape */
1871#endif
1872
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001873 {
1874 char_u *p = ccline.cmdbuff;
1875
1876 /* Make ccline empty, getcmdline() may try to use it. */
1877 ccline.cmdbuff = NULL;
1878 return p;
1879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880}
1881
1882#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1883/*
1884 * Get a command line with a prompt.
1885 * This is prepared to be called recursively from getcmdline() (e.g. by
1886 * f_input() when evaluating an expression from CTRL-R =).
1887 * Returns the command line in allocated memory, or NULL.
1888 */
1889 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001890getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 int firstc;
1892 char_u *prompt; /* command line prompt */
1893 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001894 int xp_context; /* type of expansion */
1895 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 char_u *s;
1898 struct cmdline_info save_ccline;
1899 int msg_col_save = msg_col;
1900
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001901 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 ccline.cmdprompt = prompt;
1903 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001904# ifdef FEAT_EVAL
1905 ccline.xp_context = xp_context;
1906 ccline.xp_arg = xp_arg;
1907 ccline.input_fn = (firstc == '@');
1908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001910 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 /* Restore msg_col, the prompt from input() may have changed it. */
1912 msg_col = msg_col_save;
1913
1914 return s;
1915}
1916#endif
1917
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001918/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001919 * Return TRUE when the text must not be changed and we can't switch to
1920 * another window or buffer. Used when editing the command line, evaluating
1921 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001922 */
1923 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001924text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001925{
1926#ifdef FEAT_CMDWIN
1927 if (cmdwin_type != 0)
1928 return TRUE;
1929#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001930 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001931}
1932
1933/*
1934 * Give an error message for a command that isn't allowed while the cmdline
1935 * window is open or editing the cmdline in another way.
1936 */
1937 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001938text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001939{
1940#ifdef FEAT_CMDWIN
1941 if (cmdwin_type != 0)
1942 EMSG(_(e_cmdwin));
1943 else
1944#endif
1945 EMSG(_(e_secure));
1946}
1947
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001948#if defined(FEAT_AUTOCMD) || defined(PROTO)
1949/*
1950 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
1951 * message.
1952 */
1953 int
1954curbuf_locked()
1955{
1956 if (curbuf_lock > 0)
1957 {
1958 EMSG(_("E788: Not allowed to edit another buffer now"));
1959 return TRUE;
1960 }
1961 return FALSE;
1962}
1963#endif
1964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 static int
1966cmdline_charsize(idx)
1967 int idx;
1968{
1969#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1970 if (cmdline_star > 0) /* showing '*', always 1 position */
1971 return 1;
1972#endif
1973 return ptr2cells(ccline.cmdbuff + idx);
1974}
1975
1976/*
1977 * Compute the offset of the cursor on the command line for the prompt and
1978 * indent.
1979 */
1980 static void
1981set_cmdspos()
1982{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001983 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 ccline.cmdspos = 1 + ccline.cmdindent;
1985 else
1986 ccline.cmdspos = 0 + ccline.cmdindent;
1987}
1988
1989/*
1990 * Compute the screen position for the cursor on the command line.
1991 */
1992 static void
1993set_cmdspos_cursor()
1994{
1995 int i, m, c;
1996
1997 set_cmdspos();
1998 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002001 if (m < 0) /* overflow, Columns or Rows at weird value */
2002 m = MAXCOL;
2003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 else
2005 m = MAXCOL;
2006 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2007 {
2008 c = cmdline_charsize(i);
2009#ifdef FEAT_MBYTE
2010 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2011 if (has_mbyte)
2012 correct_cmdspos(i, c);
2013#endif
2014 /* If the cmdline doesn't fit, put cursor on last visible char. */
2015 if ((ccline.cmdspos += c) >= m)
2016 {
2017 ccline.cmdpos = i - 1;
2018 ccline.cmdspos -= c;
2019 break;
2020 }
2021#ifdef FEAT_MBYTE
2022 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002023 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024#endif
2025 }
2026}
2027
2028#ifdef FEAT_MBYTE
2029/*
2030 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2031 * character that doesn't fit, so that a ">" must be displayed.
2032 */
2033 static void
2034correct_cmdspos(idx, cells)
2035 int idx;
2036 int cells;
2037{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002038 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2040 && ccline.cmdspos % Columns + cells > Columns)
2041 ccline.cmdspos++;
2042}
2043#endif
2044
2045/*
2046 * Get an Ex command line for the ":" command.
2047 */
2048/* ARGSUSED */
2049 char_u *
2050getexline(c, dummy, indent)
2051 int c; /* normally ':', NUL for ":append" */
2052 void *dummy; /* cookie not used */
2053 int indent; /* indent for inside conditionals */
2054{
2055 /* When executing a register, remove ':' that's in front of each line. */
2056 if (exec_from_reg && vpeekc() == ':')
2057 (void)vgetc();
2058 return getcmdline(c, 1L, indent);
2059}
2060
2061/*
2062 * Get an Ex command line for Ex mode.
2063 * In Ex mode we only use the OS supplied line editing features and no
2064 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002065 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 */
2067/* ARGSUSED */
2068 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002069getexmodeline(promptc, dummy, indent)
2070 int promptc; /* normally ':', NUL for ":append" and '?' for
2071 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 void *dummy; /* cookie not used */
2073 int indent; /* indent for inside conditionals */
2074{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002075 garray_T line_ga;
2076 char_u *pend;
2077 int startcol = 0;
2078 int c1;
2079 int escaped = FALSE; /* CTRL-V typed */
2080 int vcol = 0;
2081 char_u *p;
2082 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083
2084 /* Switch cursor on now. This avoids that it happens after the "\n", which
2085 * confuses the system function that computes tabstops. */
2086 cursor_on();
2087
2088 /* always start in column 0; write a newline if necessary */
2089 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002090 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002092 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002094 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002095 if (p_prompt)
2096 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 while (indent-- > 0)
2098 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
2101
2102 ga_init2(&line_ga, 1, 30);
2103
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002104 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002105 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002106 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002107 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002108 while (indent >= 8)
2109 {
2110 ga_append(&line_ga, TAB);
2111 msg_puts((char_u *)" ");
2112 indent -= 8;
2113 }
2114 while (indent-- > 0)
2115 {
2116 ga_append(&line_ga, ' ');
2117 msg_putchar(' ');
2118 }
2119 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002120 ++no_mapping;
2121 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 /*
2124 * Get the line, one character at a time.
2125 */
2126 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002127 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
2129 if (ga_grow(&line_ga, 40) == FAIL)
2130 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002131 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002133 /* Get one character at a time. Don't use inchar(), it can't handle
2134 * special characters. */
2135 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
2137 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002138 * Handle line editing.
2139 * Previously this was left to the system, putting the terminal in
2140 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002142 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002144 msg_putchar('\n');
2145 break;
2146 }
2147
2148 if (!escaped)
2149 {
2150 /* CR typed means "enter", which is NL */
2151 if (c1 == '\r')
2152 c1 = '\n';
2153
2154 if (c1 == BS || c1 == K_BS
2155 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002157 if (line_ga.ga_len > 0)
2158 {
2159 --line_ga.ga_len;
2160 goto redraw;
2161 }
2162 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002165 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002167 msg_col = startcol;
2168 msg_clr_eos();
2169 line_ga.ga_len = 0;
2170 continue;
2171 }
2172
2173 if (c1 == Ctrl_T)
2174 {
2175 p = (char_u *)line_ga.ga_data;
2176 p[line_ga.ga_len] = NUL;
2177 indent = get_indent_str(p, 8);
2178 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2179add_indent:
2180 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002182 char_u *s = skipwhite(p);
2183
2184 ga_grow(&line_ga, 1);
2185 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2186 *s = ' ';
2187 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002189redraw:
2190 /* redraw the line */
2191 msg_col = startcol;
2192 windgoto(msg_row, msg_col);
2193 vcol = 0;
2194 for (p = (char_u *)line_ga.ga_data;
2195 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002197 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002199 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002201 msg_putchar(' ');
2202 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002204 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002206 msg_outtrans_len(p, 1);
2207 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002210 msg_clr_eos();
2211 continue;
2212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002214 if (c1 == Ctrl_D)
2215 {
2216 /* Delete one shiftwidth. */
2217 p = (char_u *)line_ga.ga_data;
2218 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002220 if (prev_char == '^')
2221 ex_keep_indent = TRUE;
2222 indent = 0;
2223 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225 else
2226 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002227 p[line_ga.ga_len] = NUL;
2228 indent = get_indent_str(p, 8);
2229 --indent;
2230 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232 while (get_indent_str(p, 8) > indent)
2233 {
2234 char_u *s = skipwhite(p);
2235
2236 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2237 --line_ga.ga_len;
2238 }
2239 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002241
2242 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2243 {
2244 escaped = TRUE;
2245 continue;
2246 }
2247
2248 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2249 if (IS_SPECIAL(c1))
2250 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002252
2253 if (IS_SPECIAL(c1))
2254 c1 = '?';
2255 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2256 prev_char = c1;
2257 if (c1 == '\n')
2258 msg_putchar('\n');
2259 else if (c1 == TAB)
2260 {
2261 /* Don't use chartabsize(), 'ts' can be different */
2262 do
2263 {
2264 msg_putchar(' ');
2265 } while (++vcol % 8);
2266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002269 msg_outtrans_len(
2270 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2271 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002273 ++line_ga.ga_len;
2274 escaped = FALSE;
2275
2276 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002277 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002278
2279 /* we are done when a NL is entered, but not when it comes after a
2280 * backslash */
2281 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2282 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002285 --pend;
2286 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002287 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289 }
2290
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002291 --no_mapping;
2292 --allow_keys;
2293
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 /* make following messages go to the next line */
2295 msg_didout = FALSE;
2296 msg_col = 0;
2297 if (msg_row < Rows - 1)
2298 ++msg_row;
2299 emsg_on_display = FALSE; /* don't want ui_delay() */
2300
2301 if (got_int)
2302 ga_clear(&line_ga);
2303
2304 return (char_u *)line_ga.ga_data;
2305}
2306
Bram Moolenaare344bea2005-09-01 20:46:49 +00002307# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2308 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309/*
2310 * Return TRUE if ccline.overstrike is on.
2311 */
2312 int
2313cmdline_overstrike()
2314{
2315 return ccline.overstrike;
2316}
2317
2318/*
2319 * Return TRUE if the cursor is at the end of the cmdline.
2320 */
2321 int
2322cmdline_at_end()
2323{
2324 return (ccline.cmdpos >= ccline.cmdlen);
2325}
2326#endif
2327
Bram Moolenaar9372a112005-12-06 19:59:18 +00002328#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329/*
2330 * Return the virtual column number at the current cursor position.
2331 * This is used by the IM code to obtain the start of the preedit string.
2332 */
2333 colnr_T
2334cmdline_getvcol_cursor()
2335{
2336 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2337 return MAXCOL;
2338
2339# ifdef FEAT_MBYTE
2340 if (has_mbyte)
2341 {
2342 colnr_T col;
2343 int i = 0;
2344
2345 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002346 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
2348 return col;
2349 }
2350 else
2351# endif
2352 return ccline.cmdpos;
2353}
2354#endif
2355
2356#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2357/*
2358 * If part of the command line is an IM preedit string, redraw it with
2359 * IM feedback attributes. The cursor position is restored after drawing.
2360 */
2361 static void
2362redrawcmd_preedit()
2363{
2364 if ((State & CMDLINE)
2365 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002366 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 && !p_imdisable
2368 && im_is_preediting())
2369 {
2370 int cmdpos = 0;
2371 int cmdspos;
2372 int old_row;
2373 int old_col;
2374 colnr_T col;
2375
2376 old_row = msg_row;
2377 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002378 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380# ifdef FEAT_MBYTE
2381 if (has_mbyte)
2382 {
2383 for (col = 0; col < preedit_start_col
2384 && cmdpos < ccline.cmdlen; ++col)
2385 {
2386 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002387 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389 }
2390 else
2391# endif
2392 {
2393 cmdspos += preedit_start_col;
2394 cmdpos += preedit_start_col;
2395 }
2396
2397 msg_row = cmdline_row + (cmdspos / (int)Columns);
2398 msg_col = cmdspos % (int)Columns;
2399 if (msg_row >= Rows)
2400 msg_row = Rows - 1;
2401
2402 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2403 {
2404 int char_len;
2405 int char_attr;
2406
2407 char_attr = im_get_feedback_attr(col);
2408 if (char_attr < 0)
2409 break; /* end of preedit string */
2410
2411# ifdef FEAT_MBYTE
2412 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002413 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 else
2415# endif
2416 char_len = 1;
2417
2418 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2419 cmdpos += char_len;
2420 }
2421
2422 msg_row = old_row;
2423 msg_col = old_col;
2424 }
2425}
2426#endif /* FEAT_XIM && FEAT_GUI_GTK */
2427
2428/*
2429 * Allocate a new command line buffer.
2430 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2431 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2432 */
2433 static void
2434alloc_cmdbuff(len)
2435 int len;
2436{
2437 /*
2438 * give some extra space to avoid having to allocate all the time
2439 */
2440 if (len < 80)
2441 len = 100;
2442 else
2443 len += 20;
2444
2445 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2446 ccline.cmdbufflen = len;
2447}
2448
2449/*
2450 * Re-allocate the command line to length len + something extra.
2451 * return FAIL for failure, OK otherwise
2452 */
2453 static int
2454realloc_cmdbuff(len)
2455 int len;
2456{
2457 char_u *p;
2458
2459 p = ccline.cmdbuff;
2460 alloc_cmdbuff(len); /* will get some more */
2461 if (ccline.cmdbuff == NULL) /* out of memory */
2462 {
2463 ccline.cmdbuff = p; /* keep the old one */
2464 return FAIL;
2465 }
2466 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2467 vim_free(p);
2468 return OK;
2469}
2470
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002471#if defined(FEAT_ARABIC) || defined(PROTO)
2472static char_u *arshape_buf = NULL;
2473
2474# if defined(EXITFREE) || defined(PROTO)
2475 void
2476free_cmdline_buf()
2477{
2478 vim_free(arshape_buf);
2479}
2480# endif
2481#endif
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483/*
2484 * Draw part of the cmdline at the current cursor position. But draw stars
2485 * when cmdline_star is TRUE.
2486 */
2487 static void
2488draw_cmdline(start, len)
2489 int start;
2490 int len;
2491{
2492#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2493 int i;
2494
2495 if (cmdline_star > 0)
2496 for (i = 0; i < len; ++i)
2497 {
2498 msg_putchar('*');
2499# ifdef FEAT_MBYTE
2500 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002501 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502# endif
2503 }
2504 else
2505#endif
2506#ifdef FEAT_ARABIC
2507 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2508 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 static int buflen = 0;
2510 char_u *p;
2511 int j;
2512 int newlen = 0;
2513 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002514 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 int prev_c = 0;
2516 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002517 int u8c;
2518 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
2521 /*
2522 * Do arabic shaping into a temporary buffer. This is very
2523 * inefficient!
2524 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002525 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 /* Re-allocate the buffer. We keep it around to avoid a lot of
2528 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002529 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002530 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002531 arshape_buf = alloc(buflen);
2532 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 return; /* out of memory */
2534 }
2535
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002536 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2537 {
2538 /* Prepend a space to draw the leading composing char on. */
2539 arshape_buf[0] = ' ';
2540 newlen = 1;
2541 }
2542
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 for (j = start; j < start + len; j += mb_l)
2544 {
2545 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002546 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002547 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (ARABIC_CHAR(u8c))
2549 {
2550 /* Do Arabic shaping. */
2551 if (cmdmsg_rl)
2552 {
2553 /* displaying from right to left */
2554 pc = prev_c;
2555 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002556 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 if (j + mb_l >= start + len)
2558 nc = NUL;
2559 else
2560 nc = utf_ptr2char(p + mb_l);
2561 }
2562 else
2563 {
2564 /* displaying from left to right */
2565 if (j + mb_l >= start + len)
2566 pc = NUL;
2567 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002568 {
2569 int pcc[MAX_MCO];
2570
2571 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002573 pc1 = pcc[0];
2574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 nc = prev_c;
2576 }
2577 prev_c = u8c;
2578
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002579 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002581 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002582 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002584 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2585 if (u8cc[1] != 0)
2586 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002587 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
2589 }
2590 else
2591 {
2592 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002593 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 newlen += mb_l;
2595 }
2596 }
2597
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002598 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600 else
2601#endif
2602 msg_outtrans_len(ccline.cmdbuff + start, len);
2603}
2604
2605/*
2606 * Put a character on the command line. Shifts the following text to the
2607 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2608 * "c" must be printable (fit in one display cell)!
2609 */
2610 void
2611putcmdline(c, shift)
2612 int c;
2613 int shift;
2614{
2615 if (cmd_silent)
2616 return;
2617 msg_no_more = TRUE;
2618 msg_putchar(c);
2619 if (shift)
2620 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2621 msg_no_more = FALSE;
2622 cursorcmd();
2623}
2624
2625/*
2626 * Undo a putcmdline(c, FALSE).
2627 */
2628 void
2629unputcmdline()
2630{
2631 if (cmd_silent)
2632 return;
2633 msg_no_more = TRUE;
2634 if (ccline.cmdlen == ccline.cmdpos)
2635 msg_putchar(' ');
2636 else
2637 draw_cmdline(ccline.cmdpos, 1);
2638 msg_no_more = FALSE;
2639 cursorcmd();
2640}
2641
2642/*
2643 * Put the given string, of the given length, onto the command line.
2644 * If len is -1, then STRLEN() is used to calculate the length.
2645 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2646 * part will be redrawn, otherwise it will not. If this function is called
2647 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2648 * called afterwards.
2649 */
2650 int
2651put_on_cmdline(str, len, redraw)
2652 char_u *str;
2653 int len;
2654 int redraw;
2655{
2656 int retval;
2657 int i;
2658 int m;
2659 int c;
2660
2661 if (len < 0)
2662 len = (int)STRLEN(str);
2663
2664 /* Check if ccline.cmdbuff needs to be longer */
2665 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2666 retval = realloc_cmdbuff(ccline.cmdlen + len);
2667 else
2668 retval = OK;
2669 if (retval == OK)
2670 {
2671 if (!ccline.overstrike)
2672 {
2673 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2674 ccline.cmdbuff + ccline.cmdpos,
2675 (size_t)(ccline.cmdlen - ccline.cmdpos));
2676 ccline.cmdlen += len;
2677 }
2678 else
2679 {
2680#ifdef FEAT_MBYTE
2681 if (has_mbyte)
2682 {
2683 /* Count nr of characters in the new string. */
2684 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002685 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 ++m;
2687 /* Count nr of bytes in cmdline that are overwritten by these
2688 * characters. */
2689 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002690 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 --m;
2692 if (i < ccline.cmdlen)
2693 {
2694 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2695 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2696 ccline.cmdlen += ccline.cmdpos + len - i;
2697 }
2698 else
2699 ccline.cmdlen = ccline.cmdpos + len;
2700 }
2701 else
2702#endif
2703 if (ccline.cmdpos + len > ccline.cmdlen)
2704 ccline.cmdlen = ccline.cmdpos + len;
2705 }
2706 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2707 ccline.cmdbuff[ccline.cmdlen] = NUL;
2708
2709#ifdef FEAT_MBYTE
2710 if (enc_utf8)
2711 {
2712 /* When the inserted text starts with a composing character,
2713 * backup to the character before it. There could be two of them.
2714 */
2715 i = 0;
2716 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2717 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2718 {
2719 i = (*mb_head_off)(ccline.cmdbuff,
2720 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2721 ccline.cmdpos -= i;
2722 len += i;
2723 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2724 }
2725# ifdef FEAT_ARABIC
2726 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2727 {
2728 /* Check the previous character for Arabic combining pair. */
2729 i = (*mb_head_off)(ccline.cmdbuff,
2730 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2731 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2732 + ccline.cmdpos - i), c))
2733 {
2734 ccline.cmdpos -= i;
2735 len += i;
2736 }
2737 else
2738 i = 0;
2739 }
2740# endif
2741 if (i != 0)
2742 {
2743 /* Also backup the cursor position. */
2744 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2745 ccline.cmdspos -= i;
2746 msg_col -= i;
2747 if (msg_col < 0)
2748 {
2749 msg_col += Columns;
2750 --msg_row;
2751 }
2752 }
2753 }
2754#endif
2755
2756 if (redraw && !cmd_silent)
2757 {
2758 msg_no_more = TRUE;
2759 i = cmdline_row;
2760 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2761 /* Avoid clearing the rest of the line too often. */
2762 if (cmdline_row != i || ccline.overstrike)
2763 msg_clr_eos();
2764 msg_no_more = FALSE;
2765 }
2766#ifdef FEAT_FKMAP
2767 /*
2768 * If we are in Farsi command mode, the character input must be in
2769 * Insert mode. So do not advance the cmdpos.
2770 */
2771 if (!cmd_fkmap)
2772#endif
2773 {
2774 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002775 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002777 if (m < 0) /* overflow, Columns or Rows at weird value */
2778 m = MAXCOL;
2779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else
2781 m = MAXCOL;
2782 for (i = 0; i < len; ++i)
2783 {
2784 c = cmdline_charsize(ccline.cmdpos);
2785#ifdef FEAT_MBYTE
2786 /* count ">" for a double-wide char that doesn't fit. */
2787 if (has_mbyte)
2788 correct_cmdspos(ccline.cmdpos, c);
2789#endif
2790 /* Stop cursor at the end of the screen */
2791 if (ccline.cmdspos + c >= m)
2792 break;
2793 ccline.cmdspos += c;
2794#ifdef FEAT_MBYTE
2795 if (has_mbyte)
2796 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002797 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 if (c > len - i - 1)
2799 c = len - i - 1;
2800 ccline.cmdpos += c;
2801 i += c;
2802 }
2803#endif
2804 ++ccline.cmdpos;
2805 }
2806 }
2807 }
2808 if (redraw)
2809 msg_check();
2810 return retval;
2811}
2812
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002813static struct cmdline_info prev_ccline;
2814static int prev_ccline_used = FALSE;
2815
2816/*
2817 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2818 * and overwrite it. But get_cmdline_str() may need it, thus make it
2819 * available globally in prev_ccline.
2820 */
2821 static void
2822save_cmdline(ccp)
2823 struct cmdline_info *ccp;
2824{
2825 if (!prev_ccline_used)
2826 {
2827 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2828 prev_ccline_used = TRUE;
2829 }
2830 *ccp = prev_ccline;
2831 prev_ccline = ccline;
2832 ccline.cmdbuff = NULL;
2833 ccline.cmdprompt = NULL;
2834}
2835
2836/*
2837 * Resture ccline after it has been saved with save_cmdline().
2838 */
2839 static void
2840restore_cmdline(ccp)
2841 struct cmdline_info *ccp;
2842{
2843 ccline = prev_ccline;
2844 prev_ccline = *ccp;
2845}
2846
Bram Moolenaar5a305422006-04-28 22:38:25 +00002847#if defined(FEAT_EVAL) || defined(PROTO)
2848/*
2849 * Save the command line into allocated memory. Returns a pointer to be
2850 * passed to restore_cmdline_alloc() later.
2851 * Returns NULL when failed.
2852 */
2853 char_u *
2854save_cmdline_alloc()
2855{
2856 struct cmdline_info *p;
2857
2858 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2859 if (p != NULL)
2860 save_cmdline(p);
2861 return (char_u *)p;
2862}
2863
2864/*
2865 * Restore the command line from the return value of save_cmdline_alloc().
2866 */
2867 void
2868restore_cmdline_alloc(p)
2869 char_u *p;
2870{
2871 if (p != NULL)
2872 {
2873 restore_cmdline((struct cmdline_info *)p);
2874 vim_free(p);
2875 }
2876}
2877#endif
2878
Bram Moolenaar8299df92004-07-10 09:47:34 +00002879/*
2880 * paste a yank register into the command line.
2881 * used by CTRL-R command in command-line mode
2882 * insert_reg() can't be used here, because special characters from the
2883 * register contents will be interpreted as commands.
2884 *
2885 * return FAIL for failure, OK otherwise
2886 */
2887 static int
2888cmdline_paste(regname, literally)
2889 int regname;
2890 int literally; /* Insert text literally instead of "as typed" */
2891{
2892 long i;
2893 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002894 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002895 int allocated;
2896 struct cmdline_info save_ccline;
2897
2898 /* check for valid regname; also accept special characters for CTRL-R in
2899 * the command line */
2900 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2901 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2902 return FAIL;
2903
2904 /* A register containing CTRL-R can cause an endless loop. Allow using
2905 * CTRL-C to break the loop. */
2906 line_breakcheck();
2907 if (got_int)
2908 return FAIL;
2909
2910#ifdef FEAT_CLIPBOARD
2911 regname = may_get_selection(regname);
2912#endif
2913
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002914 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002915 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002916 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002917 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002918 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002919 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002920 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002921
2922 if (i)
2923 {
2924 /* Got the value of a special register in "arg". */
2925 if (arg == NULL)
2926 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002927
2928 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2929 * part of the word. */
2930 p = arg;
2931 if (p_is && regname == Ctrl_W)
2932 {
2933 char_u *w;
2934 int len;
2935
2936 /* Locate start of last word in the cmd buffer. */
2937 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
2938 {
2939#ifdef FEAT_MBYTE
2940 if (has_mbyte)
2941 {
2942 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
2943 if (!vim_iswordc(mb_ptr2char(w - len)))
2944 break;
2945 w -= len;
2946 }
2947 else
2948#endif
2949 {
2950 if (!vim_iswordc(w[-1]))
2951 break;
2952 --w;
2953 }
2954 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002955 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002956 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
2957 p += len;
2958 }
2959
2960 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002961 if (allocated)
2962 vim_free(arg);
2963 return OK;
2964 }
2965
2966 return cmdline_paste_reg(regname, literally);
2967}
2968
2969/*
2970 * Put a string on the command line.
2971 * When "literally" is TRUE, insert literally.
2972 * When "literally" is FALSE, insert as typed, but don't leave the command
2973 * line.
2974 */
2975 void
2976cmdline_paste_str(s, literally)
2977 char_u *s;
2978 int literally;
2979{
2980 int c, cv;
2981
2982 if (literally)
2983 put_on_cmdline(s, -1, TRUE);
2984 else
2985 while (*s != NUL)
2986 {
2987 cv = *s;
2988 if (cv == Ctrl_V && s[1])
2989 ++s;
2990#ifdef FEAT_MBYTE
2991 if (has_mbyte)
2992 {
2993 c = mb_ptr2char(s);
2994 s += mb_char2len(c);
2995 }
2996 else
2997#endif
2998 c = *s++;
2999 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3000#ifdef UNIX
3001 || c == intr_char
3002#endif
3003 || (c == Ctrl_BSL && *s == Ctrl_N))
3004 stuffcharReadbuff(Ctrl_V);
3005 stuffcharReadbuff(c);
3006 }
3007}
3008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009#ifdef FEAT_WILDMENU
3010/*
3011 * Delete characters on the command line, from "from" to the current
3012 * position.
3013 */
3014 static void
3015cmdline_del(from)
3016 int from;
3017{
3018 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3019 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3020 ccline.cmdlen -= ccline.cmdpos - from;
3021 ccline.cmdpos = from;
3022}
3023#endif
3024
3025/*
3026 * this fuction is called when the screen size changes and with incremental
3027 * search
3028 */
3029 void
3030redrawcmdline()
3031{
3032 if (cmd_silent)
3033 return;
3034 need_wait_return = FALSE;
3035 compute_cmdrow();
3036 redrawcmd();
3037 cursorcmd();
3038}
3039
3040 static void
3041redrawcmdprompt()
3042{
3043 int i;
3044
3045 if (cmd_silent)
3046 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003047 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 msg_putchar(ccline.cmdfirstc);
3049 if (ccline.cmdprompt != NULL)
3050 {
3051 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3052 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3053 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003054 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 --ccline.cmdindent;
3056 }
3057 else
3058 for (i = ccline.cmdindent; i > 0; --i)
3059 msg_putchar(' ');
3060}
3061
3062/*
3063 * Redraw what is currently on the command line.
3064 */
3065 void
3066redrawcmd()
3067{
3068 if (cmd_silent)
3069 return;
3070
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003071 /* when 'incsearch' is set there may be no command line while redrawing */
3072 if (ccline.cmdbuff == NULL)
3073 {
3074 windgoto(cmdline_row, 0);
3075 msg_clr_eos();
3076 return;
3077 }
3078
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 msg_start();
3080 redrawcmdprompt();
3081
3082 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3083 msg_no_more = TRUE;
3084 draw_cmdline(0, ccline.cmdlen);
3085 msg_clr_eos();
3086 msg_no_more = FALSE;
3087
3088 set_cmdspos_cursor();
3089
3090 /*
3091 * An emsg() before may have set msg_scroll. This is used in normal mode,
3092 * in cmdline mode we can reset them now.
3093 */
3094 msg_scroll = FALSE; /* next message overwrites cmdline */
3095
3096 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3097 * in cmdline mode */
3098 skip_redraw = FALSE;
3099}
3100
3101 void
3102compute_cmdrow()
3103{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003104 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 cmdline_row = Rows - 1;
3106 else
3107 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3108 + W_STATUS_HEIGHT(lastwin);
3109}
3110
3111 static void
3112cursorcmd()
3113{
3114 if (cmd_silent)
3115 return;
3116
3117#ifdef FEAT_RIGHTLEFT
3118 if (cmdmsg_rl)
3119 {
3120 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3121 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3122 if (msg_row <= 0)
3123 msg_row = Rows - 1;
3124 }
3125 else
3126#endif
3127 {
3128 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3129 msg_col = ccline.cmdspos % (int)Columns;
3130 if (msg_row >= Rows)
3131 msg_row = Rows - 1;
3132 }
3133
3134 windgoto(msg_row, msg_col);
3135#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3136 redrawcmd_preedit();
3137#endif
3138#ifdef MCH_CURSOR_SHAPE
3139 mch_update_cursor();
3140#endif
3141}
3142
3143 void
3144gotocmdline(clr)
3145 int clr;
3146{
3147 msg_start();
3148#ifdef FEAT_RIGHTLEFT
3149 if (cmdmsg_rl)
3150 msg_col = Columns - 1;
3151 else
3152#endif
3153 msg_col = 0; /* always start in column 0 */
3154 if (clr) /* clear the bottom line(s) */
3155 msg_clr_eos(); /* will reset clear_cmdline */
3156 windgoto(cmdline_row, 0);
3157}
3158
3159/*
3160 * Check the word in front of the cursor for an abbreviation.
3161 * Called when the non-id character "c" has been entered.
3162 * When an abbreviation is recognized it is removed from the text with
3163 * backspaces and the replacement string is inserted, followed by "c".
3164 */
3165 static int
3166ccheck_abbr(c)
3167 int c;
3168{
3169 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3170 return FALSE;
3171
3172 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3173}
3174
3175/*
3176 * Return FAIL if this is not an appropriate context in which to do
3177 * completion of anything, return OK if it is (even if there are no matches).
3178 * For the caller, this means that the character is just passed through like a
3179 * normal character (instead of being expanded). This allows :s/^I^D etc.
3180 */
3181 static int
3182nextwild(xp, type, options)
3183 expand_T *xp;
3184 int type;
3185 int options; /* extra options for ExpandOne() */
3186{
3187 int i, j;
3188 char_u *p1;
3189 char_u *p2;
3190 int oldlen;
3191 int difflen;
3192 int v;
3193
3194 if (xp->xp_numfiles == -1)
3195 {
3196 set_expand_context(xp);
3197 cmd_showtail = expand_showtail(xp);
3198 }
3199
3200 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3201 {
3202 beep_flush();
3203 return OK; /* Something illegal on command line */
3204 }
3205 if (xp->xp_context == EXPAND_NOTHING)
3206 {
3207 /* Caller can use the character as a normal char instead */
3208 return FAIL;
3209 }
3210
3211 MSG_PUTS("..."); /* show that we are busy */
3212 out_flush();
3213
3214 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3215 oldlen = ccline.cmdpos - i;
3216
3217 if (type == WILD_NEXT || type == WILD_PREV)
3218 {
3219 /*
3220 * Get next/previous match for a previous expanded pattern.
3221 */
3222 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3223 }
3224 else
3225 {
3226 /*
3227 * Translate string into pattern and expand it.
3228 */
3229 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3230 p2 = NULL;
3231 else
3232 {
3233 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3234 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3235 |options, type);
3236 vim_free(p1);
3237 /* longest match: make sure it is not shorter (happens with :help */
3238 if (p2 != NULL && type == WILD_LONGEST)
3239 {
3240 for (j = 0; j < oldlen; ++j)
3241 if (ccline.cmdbuff[i + j] == '*'
3242 || ccline.cmdbuff[i + j] == '?')
3243 break;
3244 if ((int)STRLEN(p2) < j)
3245 {
3246 vim_free(p2);
3247 p2 = NULL;
3248 }
3249 }
3250 }
3251 }
3252
3253 if (p2 != NULL && !got_int)
3254 {
3255 difflen = (int)STRLEN(p2) - oldlen;
3256 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3257 {
3258 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3259 xp->xp_pattern = ccline.cmdbuff + i;
3260 }
3261 else
3262 v = OK;
3263 if (v == OK)
3264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003265 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3266 &ccline.cmdbuff[ccline.cmdpos],
3267 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3268 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 ccline.cmdlen += difflen;
3270 ccline.cmdpos += difflen;
3271 }
3272 }
3273 vim_free(p2);
3274
3275 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003276 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
3278 /* When expanding a ":map" command and no matches are found, assume that
3279 * the key is supposed to be inserted literally */
3280 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3281 return FAIL;
3282
3283 if (xp->xp_numfiles <= 0 && p2 == NULL)
3284 beep_flush();
3285 else if (xp->xp_numfiles == 1)
3286 /* free expanded pattern */
3287 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3288
3289 return OK;
3290}
3291
3292/*
3293 * Do wildcard expansion on the string 'str'.
3294 * Chars that should not be expanded must be preceded with a backslash.
3295 * Return a pointer to alloced memory containing the new string.
3296 * Return NULL for failure.
3297 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003298 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3299 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 *
3301 * mode = WILD_FREE: just free previously expanded matches
3302 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3303 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3304 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3305 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3306 * mode = WILD_ALL: return all matches concatenated
3307 * mode = WILD_LONGEST: return longest matched part
3308 *
3309 * options = WILD_LIST_NOTFOUND: list entries without a match
3310 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3311 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3312 * options = WILD_NO_BEEP: Don't beep for multiple matches
3313 * options = WILD_ADD_SLASH: add a slash after directory names
3314 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3315 * options = WILD_SILENT: don't print warning messages
3316 * options = WILD_ESCAPE: put backslash before special chars
3317 *
3318 * The variables xp->xp_context and xp->xp_backslash must have been set!
3319 */
3320 char_u *
3321ExpandOne(xp, str, orig, options, mode)
3322 expand_T *xp;
3323 char_u *str;
3324 char_u *orig; /* allocated copy of original of expanded string */
3325 int options;
3326 int mode;
3327{
3328 char_u *ss = NULL;
3329 static int findex;
3330 static char_u *orig_save = NULL; /* kept value of orig */
3331 int i;
3332 long_u len;
3333 int non_suf_match; /* number without matching suffix */
3334
3335 /*
3336 * first handle the case of using an old match
3337 */
3338 if (mode == WILD_NEXT || mode == WILD_PREV)
3339 {
3340 if (xp->xp_numfiles > 0)
3341 {
3342 if (mode == WILD_PREV)
3343 {
3344 if (findex == -1)
3345 findex = xp->xp_numfiles;
3346 --findex;
3347 }
3348 else /* mode == WILD_NEXT */
3349 ++findex;
3350
3351 /*
3352 * When wrapping around, return the original string, set findex to
3353 * -1.
3354 */
3355 if (findex < 0)
3356 {
3357 if (orig_save == NULL)
3358 findex = xp->xp_numfiles - 1;
3359 else
3360 findex = -1;
3361 }
3362 if (findex >= xp->xp_numfiles)
3363 {
3364 if (orig_save == NULL)
3365 findex = 0;
3366 else
3367 findex = -1;
3368 }
3369#ifdef FEAT_WILDMENU
3370 if (p_wmnu)
3371 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3372 findex, cmd_showtail);
3373#endif
3374 if (findex == -1)
3375 return vim_strsave(orig_save);
3376 return vim_strsave(xp->xp_files[findex]);
3377 }
3378 else
3379 return NULL;
3380 }
3381
3382/* free old names */
3383 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3384 {
3385 FreeWild(xp->xp_numfiles, xp->xp_files);
3386 xp->xp_numfiles = -1;
3387 vim_free(orig_save);
3388 orig_save = NULL;
3389 }
3390 findex = 0;
3391
3392 if (mode == WILD_FREE) /* only release file name */
3393 return NULL;
3394
3395 if (xp->xp_numfiles == -1)
3396 {
3397 vim_free(orig_save);
3398 orig_save = orig;
3399
3400 /*
3401 * Do the expansion.
3402 */
3403 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3404 options) == FAIL)
3405 {
3406#ifdef FNAME_ILLEGAL
3407 /* Illegal file name has been silently skipped. But when there
3408 * are wildcards, the real problem is that there was no match,
3409 * causing the pattern to be added, which has illegal characters.
3410 */
3411 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3412 EMSG2(_(e_nomatch2), str);
3413#endif
3414 }
3415 else if (xp->xp_numfiles == 0)
3416 {
3417 if (!(options & WILD_SILENT))
3418 EMSG2(_(e_nomatch2), str);
3419 }
3420 else
3421 {
3422 /* Escape the matches for use on the command line. */
3423 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3424
3425 /*
3426 * Check for matching suffixes in file names.
3427 */
3428 if (mode != WILD_ALL && mode != WILD_LONGEST)
3429 {
3430 if (xp->xp_numfiles)
3431 non_suf_match = xp->xp_numfiles;
3432 else
3433 non_suf_match = 1;
3434 if ((xp->xp_context == EXPAND_FILES
3435 || xp->xp_context == EXPAND_DIRECTORIES)
3436 && xp->xp_numfiles > 1)
3437 {
3438 /*
3439 * More than one match; check suffix.
3440 * The files will have been sorted on matching suffix in
3441 * expand_wildcards, only need to check the first two.
3442 */
3443 non_suf_match = 0;
3444 for (i = 0; i < 2; ++i)
3445 if (match_suffix(xp->xp_files[i]))
3446 ++non_suf_match;
3447 }
3448 if (non_suf_match != 1)
3449 {
3450 /* Can we ever get here unless it's while expanding
3451 * interactively? If not, we can get rid of this all
3452 * together. Don't really want to wait for this message
3453 * (and possibly have to hit return to continue!).
3454 */
3455 if (!(options & WILD_SILENT))
3456 EMSG(_(e_toomany));
3457 else if (!(options & WILD_NO_BEEP))
3458 beep_flush();
3459 }
3460 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3461 ss = vim_strsave(xp->xp_files[0]);
3462 }
3463 }
3464 }
3465
3466 /* Find longest common part */
3467 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3468 {
3469 for (len = 0; xp->xp_files[0][len]; ++len)
3470 {
3471 for (i = 0; i < xp->xp_numfiles; ++i)
3472 {
3473#ifdef CASE_INSENSITIVE_FILENAME
3474 if (xp->xp_context == EXPAND_DIRECTORIES
3475 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003476 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 || xp->xp_context == EXPAND_BUFFERS)
3478 {
3479 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3480 TOLOWER_LOC(xp->xp_files[0][len]))
3481 break;
3482 }
3483 else
3484#endif
3485 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3486 break;
3487 }
3488 if (i < xp->xp_numfiles)
3489 {
3490 if (!(options & WILD_NO_BEEP))
3491 vim_beep();
3492 break;
3493 }
3494 }
3495 ss = alloc((unsigned)len + 1);
3496 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003497 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 findex = -1; /* next p_wc gets first one */
3499 }
3500
3501 /* Concatenate all matching names */
3502 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3503 {
3504 len = 0;
3505 for (i = 0; i < xp->xp_numfiles; ++i)
3506 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3507 ss = lalloc(len, TRUE);
3508 if (ss != NULL)
3509 {
3510 *ss = NUL;
3511 for (i = 0; i < xp->xp_numfiles; ++i)
3512 {
3513 STRCAT(ss, xp->xp_files[i]);
3514 if (i != xp->xp_numfiles - 1)
3515 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3516 }
3517 }
3518 }
3519
3520 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3521 ExpandCleanup(xp);
3522
3523 return ss;
3524}
3525
3526/*
3527 * Prepare an expand structure for use.
3528 */
3529 void
3530ExpandInit(xp)
3531 expand_T *xp;
3532{
3533 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003534#ifndef BACKSLASH_IN_FILENAME
3535 xp->xp_shell = FALSE;
3536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 xp->xp_numfiles = -1;
3538 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003539#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3540 xp->xp_arg = NULL;
3541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542}
3543
3544/*
3545 * Cleanup an expand structure after use.
3546 */
3547 void
3548ExpandCleanup(xp)
3549 expand_T *xp;
3550{
3551 if (xp->xp_numfiles >= 0)
3552 {
3553 FreeWild(xp->xp_numfiles, xp->xp_files);
3554 xp->xp_numfiles = -1;
3555 }
3556}
3557
3558 void
3559ExpandEscape(xp, str, numfiles, files, options)
3560 expand_T *xp;
3561 char_u *str;
3562 int numfiles;
3563 char_u **files;
3564 int options;
3565{
3566 int i;
3567 char_u *p;
3568
3569 /*
3570 * May change home directory back to "~"
3571 */
3572 if (options & WILD_HOME_REPLACE)
3573 tilde_replace(str, numfiles, files);
3574
3575 if (options & WILD_ESCAPE)
3576 {
3577 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003578 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 || xp->xp_context == EXPAND_BUFFERS
3580 || xp->xp_context == EXPAND_DIRECTORIES)
3581 {
3582 /*
3583 * Insert a backslash into a file name before a space, \, %, #
3584 * and wildmatch characters, except '~'.
3585 */
3586 for (i = 0; i < numfiles; ++i)
3587 {
3588 /* for ":set path=" we need to escape spaces twice */
3589 if (xp->xp_backslash == XP_BS_THREE)
3590 {
3591 p = vim_strsave_escaped(files[i], (char_u *)" ");
3592 if (p != NULL)
3593 {
3594 vim_free(files[i]);
3595 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003596#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 p = vim_strsave_escaped(files[i], (char_u *)" ");
3598 if (p != NULL)
3599 {
3600 vim_free(files[i]);
3601 files[i] = p;
3602 }
3603#endif
3604 }
3605 }
3606#ifdef BACKSLASH_IN_FILENAME
3607 {
3608 char_u buf[20];
3609 int j = 0;
3610
3611 /* Don't escape '[' and '{' if they are in 'isfname'. */
3612 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3613 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3614 buf[j++] = *p;
3615 buf[j] = NUL;
3616 p = vim_strsave_escaped(files[i], buf);
3617 }
3618#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003619 p = vim_strsave_escaped(files[i],
3620 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621#endif
3622 if (p != NULL)
3623 {
3624 vim_free(files[i]);
3625 files[i] = p;
3626 }
3627
3628 /* If 'str' starts with "\~", replace "~" at start of
3629 * files[i] with "\~". */
3630 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003631 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003634
3635 /* If the first file starts with a '+' escape it. Otherwise it
3636 * could be seen as "+cmd". */
3637 if (*files[0] == '+')
3638 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 }
3640 else if (xp->xp_context == EXPAND_TAGS)
3641 {
3642 /*
3643 * Insert a backslash before characters in a tag name that
3644 * would terminate the ":tag" command.
3645 */
3646 for (i = 0; i < numfiles; ++i)
3647 {
3648 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3649 if (p != NULL)
3650 {
3651 vim_free(files[i]);
3652 files[i] = p;
3653 }
3654 }
3655 }
3656 }
3657}
3658
3659/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003660 * Put a backslash before the file name in "pp", which is in allocated memory.
3661 */
3662 static void
3663escape_fname(pp)
3664 char_u **pp;
3665{
3666 char_u *p;
3667
3668 p = alloc((unsigned)(STRLEN(*pp) + 2));
3669 if (p != NULL)
3670 {
3671 p[0] = '\\';
3672 STRCPY(p + 1, *pp);
3673 vim_free(*pp);
3674 *pp = p;
3675 }
3676}
3677
3678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 * For each file name in files[num_files]:
3680 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3681 */
3682 void
3683tilde_replace(orig_pat, num_files, files)
3684 char_u *orig_pat;
3685 int num_files;
3686 char_u **files;
3687{
3688 int i;
3689 char_u *p;
3690
3691 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3692 {
3693 for (i = 0; i < num_files; ++i)
3694 {
3695 p = home_replace_save(NULL, files[i]);
3696 if (p != NULL)
3697 {
3698 vim_free(files[i]);
3699 files[i] = p;
3700 }
3701 }
3702 }
3703}
3704
3705/*
3706 * Show all matches for completion on the command line.
3707 * Returns EXPAND_NOTHING when the character that triggered expansion should
3708 * be inserted like a normal character.
3709 */
3710/*ARGSUSED*/
3711 static int
3712showmatches(xp, wildmenu)
3713 expand_T *xp;
3714 int wildmenu;
3715{
3716#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3717 int num_files;
3718 char_u **files_found;
3719 int i, j, k;
3720 int maxlen;
3721 int lines;
3722 int columns;
3723 char_u *p;
3724 int lastlen;
3725 int attr;
3726 int showtail;
3727
3728 if (xp->xp_numfiles == -1)
3729 {
3730 set_expand_context(xp);
3731 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3732 &num_files, &files_found);
3733 showtail = expand_showtail(xp);
3734 if (i != EXPAND_OK)
3735 return i;
3736
3737 }
3738 else
3739 {
3740 num_files = xp->xp_numfiles;
3741 files_found = xp->xp_files;
3742 showtail = cmd_showtail;
3743 }
3744
3745#ifdef FEAT_WILDMENU
3746 if (!wildmenu)
3747 {
3748#endif
3749 msg_didany = FALSE; /* lines_left will be set */
3750 msg_start(); /* prepare for paging */
3751 msg_putchar('\n');
3752 out_flush();
3753 cmdline_row = msg_row;
3754 msg_didany = FALSE; /* lines_left will be set again */
3755 msg_start(); /* prepare for paging */
3756#ifdef FEAT_WILDMENU
3757 }
3758#endif
3759
3760 if (got_int)
3761 got_int = FALSE; /* only int. the completion, not the cmd line */
3762#ifdef FEAT_WILDMENU
3763 else if (wildmenu)
3764 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3765#endif
3766 else
3767 {
3768 /* find the length of the longest file name */
3769 maxlen = 0;
3770 for (i = 0; i < num_files; ++i)
3771 {
3772 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003773 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 || xp->xp_context == EXPAND_BUFFERS))
3775 {
3776 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3777 j = vim_strsize(NameBuff);
3778 }
3779 else
3780 j = vim_strsize(L_SHOWFILE(i));
3781 if (j > maxlen)
3782 maxlen = j;
3783 }
3784
3785 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3786 lines = num_files;
3787 else
3788 {
3789 /* compute the number of columns and lines for the listing */
3790 maxlen += 2; /* two spaces between file names */
3791 columns = ((int)Columns + 2) / maxlen;
3792 if (columns < 1)
3793 columns = 1;
3794 lines = (num_files + columns - 1) / columns;
3795 }
3796
3797 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3798
3799 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3800 {
3801 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3802 msg_clr_eos();
3803 msg_advance(maxlen - 3);
3804 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3805 }
3806
3807 /* list the files line by line */
3808 for (i = 0; i < lines; ++i)
3809 {
3810 lastlen = 999;
3811 for (k = i; k < num_files; k += lines)
3812 {
3813 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3814 {
3815 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3816 p = files_found[k] + STRLEN(files_found[k]) + 1;
3817 msg_advance(maxlen + 1);
3818 msg_puts(p);
3819 msg_advance(maxlen + 3);
3820 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3821 break;
3822 }
3823 for (j = maxlen - lastlen; --j >= 0; )
3824 msg_putchar(' ');
3825 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003826 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 || xp->xp_context == EXPAND_BUFFERS)
3828 {
3829 /* highlight directories */
3830 j = (mch_isdir(files_found[k]));
3831 if (showtail)
3832 p = L_SHOWFILE(k);
3833 else
3834 {
3835 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3836 TRUE);
3837 p = NameBuff;
3838 }
3839 }
3840 else
3841 {
3842 j = FALSE;
3843 p = L_SHOWFILE(k);
3844 }
3845 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3846 }
3847 if (msg_col > 0) /* when not wrapped around */
3848 {
3849 msg_clr_eos();
3850 msg_putchar('\n');
3851 }
3852 out_flush(); /* show one line at a time */
3853 if (got_int)
3854 {
3855 got_int = FALSE;
3856 break;
3857 }
3858 }
3859
3860 /*
3861 * we redraw the command below the lines that we have just listed
3862 * This is a bit tricky, but it saves a lot of screen updating.
3863 */
3864 cmdline_row = msg_row; /* will put it back later */
3865 }
3866
3867 if (xp->xp_numfiles == -1)
3868 FreeWild(num_files, files_found);
3869
3870 return EXPAND_OK;
3871}
3872
3873/*
3874 * Private gettail for showmatches() (and win_redr_status_matches()):
3875 * Find tail of file name path, but ignore trailing "/".
3876 */
3877 char_u *
3878sm_gettail(s)
3879 char_u *s;
3880{
3881 char_u *p;
3882 char_u *t = s;
3883 int had_sep = FALSE;
3884
3885 for (p = s; *p != NUL; )
3886 {
3887 if (vim_ispathsep(*p)
3888#ifdef BACKSLASH_IN_FILENAME
3889 && !rem_backslash(p)
3890#endif
3891 )
3892 had_sep = TRUE;
3893 else if (had_sep)
3894 {
3895 t = p;
3896 had_sep = FALSE;
3897 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003898 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900 return t;
3901}
3902
3903/*
3904 * Return TRUE if we only need to show the tail of completion matches.
3905 * When not completing file names or there is a wildcard in the path FALSE is
3906 * returned.
3907 */
3908 static int
3909expand_showtail(xp)
3910 expand_T *xp;
3911{
3912 char_u *s;
3913 char_u *end;
3914
3915 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003916 if (xp->xp_context != EXPAND_FILES
3917 && xp->xp_context != EXPAND_SHELLCMD
3918 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 return FALSE;
3920
3921 end = gettail(xp->xp_pattern);
3922 if (end == xp->xp_pattern) /* there is no path separator */
3923 return FALSE;
3924
3925 for (s = xp->xp_pattern; s < end; s++)
3926 {
3927 /* Skip escaped wildcards. Only when the backslash is not a path
3928 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3929 if (rem_backslash(s))
3930 ++s;
3931 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3932 return FALSE;
3933 }
3934 return TRUE;
3935}
3936
3937/*
3938 * Prepare a string for expansion.
3939 * When expanding file names: The string will be used with expand_wildcards().
3940 * Copy the file name into allocated memory and add a '*' at the end.
3941 * When expanding other names: The string will be used with regcomp(). Copy
3942 * the name into allocated memory and prepend "^".
3943 */
3944 char_u *
3945addstar(fname, len, context)
3946 char_u *fname;
3947 int len;
3948 int context; /* EXPAND_FILES etc. */
3949{
3950 char_u *retval;
3951 int i, j;
3952 int new_len;
3953 char_u *tail;
3954
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003955 if (context != EXPAND_FILES
3956 && context != EXPAND_SHELLCMD
3957 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
3959 /*
3960 * Matching will be done internally (on something other than files).
3961 * So we convert the file-matching-type wildcards into our kind for
3962 * use with vim_regcomp(). First work out how long it will be:
3963 */
3964
3965 /* For help tags the translation is done in find_help_tags().
3966 * For a tag pattern starting with "/" no translation is needed. */
3967 if (context == EXPAND_HELP
3968 || context == EXPAND_COLORS
3969 || context == EXPAND_COMPILER
3970 || (context == EXPAND_TAGS && fname[0] == '/'))
3971 retval = vim_strnsave(fname, len);
3972 else
3973 {
3974 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3975 for (i = 0; i < len; i++)
3976 {
3977 if (fname[i] == '*' || fname[i] == '~')
3978 new_len++; /* '*' needs to be replaced by ".*"
3979 '~' needs to be replaced by "\~" */
3980
3981 /* Buffer names are like file names. "." should be literal */
3982 if (context == EXPAND_BUFFERS && fname[i] == '.')
3983 new_len++; /* "." becomes "\." */
3984
3985 /* Custom expansion takes care of special things, match
3986 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003987 if ((context == EXPAND_USER_DEFINED
3988 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 new_len++; /* '\' becomes "\\" */
3990 }
3991 retval = alloc(new_len);
3992 if (retval != NULL)
3993 {
3994 retval[0] = '^';
3995 j = 1;
3996 for (i = 0; i < len; i++, j++)
3997 {
3998 /* Skip backslash. But why? At least keep it for custom
3999 * expansion. */
4000 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004001 && context != EXPAND_USER_LIST
4002 && fname[i] == '\\'
4003 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 break;
4005
4006 switch (fname[i])
4007 {
4008 case '*': retval[j++] = '.';
4009 break;
4010 case '~': retval[j++] = '\\';
4011 break;
4012 case '?': retval[j] = '.';
4013 continue;
4014 case '.': if (context == EXPAND_BUFFERS)
4015 retval[j++] = '\\';
4016 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004017 case '\\': if (context == EXPAND_USER_DEFINED
4018 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 retval[j++] = '\\';
4020 break;
4021 }
4022 retval[j] = fname[i];
4023 }
4024 retval[j] = NUL;
4025 }
4026 }
4027 }
4028 else
4029 {
4030 retval = alloc(len + 4);
4031 if (retval != NULL)
4032 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004033 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034
4035 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004036 * Don't add a star to *, ~, ~user, $var or `cmd`.
4037 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 * ~ would be at the start of the file name, but not the tail.
4039 * $ could be anywhere in the tail.
4040 * ` could be anywhere in the file name.
4041 */
4042 tail = gettail(retval);
4043 if ((*retval != '~' || tail != retval)
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004044 && (len == 0 || retval[len - 1] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 && vim_strchr(tail, '$') == NULL
4046 && vim_strchr(retval, '`') == NULL)
4047 retval[len++] = '*';
4048 retval[len] = NUL;
4049 }
4050 }
4051 return retval;
4052}
4053
4054/*
4055 * Must parse the command line so far to work out what context we are in.
4056 * Completion can then be done based on that context.
4057 * This routine sets the variables:
4058 * xp->xp_pattern The start of the pattern to be expanded within
4059 * the command line (ends at the cursor).
4060 * xp->xp_context The type of thing to expand. Will be one of:
4061 *
4062 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4063 * the command line, like an unknown command. Caller
4064 * should beep.
4065 * EXPAND_NOTHING Unrecognised context for completion, use char like
4066 * a normal char, rather than for completion. eg
4067 * :s/^I/
4068 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4069 * it.
4070 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4071 * EXPAND_FILES After command with XFILE set, or after setting
4072 * with P_EXPAND set. eg :e ^I, :w>>^I
4073 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4074 * when we know only directories are of interest. eg
4075 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004076 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4078 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4079 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4080 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4081 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4082 * EXPAND_EVENTS Complete event names
4083 * EXPAND_SYNTAX Complete :syntax command arguments
4084 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4085 * EXPAND_AUGROUP Complete autocommand group names
4086 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4087 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4088 * eg :unmap a^I , :cunab x^I
4089 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4090 * eg :call sub^I
4091 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4092 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4093 * names in expressions, eg :while s^I
4094 * EXPAND_ENV_VARS Complete environment variable names
4095 */
4096 static void
4097set_expand_context(xp)
4098 expand_T *xp;
4099{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004100 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 if (ccline.cmdfirstc != ':'
4102#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004103 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004104 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#endif
4106 )
4107 {
4108 xp->xp_context = EXPAND_NOTHING;
4109 return;
4110 }
4111 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4112}
4113
4114 void
4115set_cmd_context(xp, str, len, col)
4116 expand_T *xp;
4117 char_u *str; /* start of command line */
4118 int len; /* length of command line (excl. NUL) */
4119 int col; /* position of cursor */
4120{
4121 int old_char = NUL;
4122 char_u *nextcomm;
4123
4124 /*
4125 * Avoid a UMR warning from Purify, only save the character if it has been
4126 * written before.
4127 */
4128 if (col < len)
4129 old_char = str[col];
4130 str[col] = NUL;
4131 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004132
4133#ifdef FEAT_EVAL
4134 if (ccline.cmdfirstc == '=')
4135 /* pass CMD_SIZE because there is no real command */
4136 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004137 else if (ccline.input_fn)
4138 {
4139 xp->xp_context = ccline.xp_context;
4140 xp->xp_pattern = ccline.cmdbuff;
4141 xp->xp_arg = ccline.xp_arg;
4142 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004143 else
4144#endif
4145 while (nextcomm != NULL)
4146 nextcomm = set_one_cmd_context(xp, nextcomm);
4147
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 str[col] = old_char;
4149}
4150
4151/*
4152 * Expand the command line "str" from context "xp".
4153 * "xp" must have been set by set_cmd_context().
4154 * xp->xp_pattern points into "str", to where the text that is to be expanded
4155 * starts.
4156 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4157 * cursor.
4158 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4159 * key that triggered expansion literally.
4160 * Returns EXPAND_OK otherwise.
4161 */
4162 int
4163expand_cmdline(xp, str, col, matchcount, matches)
4164 expand_T *xp;
4165 char_u *str; /* start of command line */
4166 int col; /* position of cursor */
4167 int *matchcount; /* return: nr of matches */
4168 char_u ***matches; /* return: array of pointers to matches */
4169{
4170 char_u *file_str = NULL;
4171
4172 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4173 {
4174 beep_flush();
4175 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4176 }
4177 if (xp->xp_context == EXPAND_NOTHING)
4178 {
4179 /* Caller can use the character as a normal char instead */
4180 return EXPAND_NOTHING;
4181 }
4182
4183 /* add star to file name, or convert to regexp if not exp. files. */
4184 file_str = addstar(xp->xp_pattern,
4185 (int)(str + col - xp->xp_pattern), xp->xp_context);
4186 if (file_str == NULL)
4187 return EXPAND_UNSUCCESSFUL;
4188
4189 /* find all files that match the description */
4190 if (ExpandFromContext(xp, file_str, matchcount, matches,
4191 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4192 {
4193 *matchcount = 0;
4194 *matches = NULL;
4195 }
4196 vim_free(file_str);
4197
4198 return EXPAND_OK;
4199}
4200
4201#ifdef FEAT_MULTI_LANG
4202/*
4203 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4204 */
4205static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4206
4207 static void
4208cleanup_help_tags(num_file, file)
4209 int num_file;
4210 char_u **file;
4211{
4212 int i, j;
4213 int len;
4214
4215 for (i = 0; i < num_file; ++i)
4216 {
4217 len = (int)STRLEN(file[i]) - 3;
4218 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4219 {
4220 /* Sorting on priority means the same item in another language may
4221 * be anywhere. Search all items for a match up to the "@en". */
4222 for (j = 0; j < num_file; ++j)
4223 if (j != i
4224 && (int)STRLEN(file[j]) == len + 3
4225 && STRNCMP(file[i], file[j], len + 1) == 0)
4226 break;
4227 if (j == num_file)
4228 file[i][len] = NUL;
4229 }
4230 }
4231}
4232#endif
4233
4234/*
4235 * Do the expansion based on xp->xp_context and "pat".
4236 */
4237 static int
4238ExpandFromContext(xp, pat, num_file, file, options)
4239 expand_T *xp;
4240 char_u *pat;
4241 int *num_file;
4242 char_u ***file;
4243 int options;
4244{
4245#ifdef FEAT_CMDL_COMPL
4246 regmatch_T regmatch;
4247#endif
4248 int ret;
4249 int flags;
4250
4251 flags = EW_DIR; /* include directories */
4252 if (options & WILD_LIST_NOTFOUND)
4253 flags |= EW_NOTFOUND;
4254 if (options & WILD_ADD_SLASH)
4255 flags |= EW_ADDSLASH;
4256 if (options & WILD_KEEP_ALL)
4257 flags |= EW_KEEPALL;
4258 if (options & WILD_SILENT)
4259 flags |= EW_SILENT;
4260
4261 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4262 {
4263 /*
4264 * Expand file or directory names.
4265 */
4266 int free_pat = FALSE;
4267 int i;
4268
4269 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4270 * space */
4271 if (xp->xp_backslash != XP_BS_NONE)
4272 {
4273 free_pat = TRUE;
4274 pat = vim_strsave(pat);
4275 for (i = 0; pat[i]; ++i)
4276 if (pat[i] == '\\')
4277 {
4278 if (xp->xp_backslash == XP_BS_THREE
4279 && pat[i + 1] == '\\'
4280 && pat[i + 2] == '\\'
4281 && pat[i + 3] == ' ')
4282 STRCPY(pat + i, pat + i + 3);
4283 if (xp->xp_backslash == XP_BS_ONE
4284 && pat[i + 1] == ' ')
4285 STRCPY(pat + i, pat + i + 1);
4286 }
4287 }
4288
4289 if (xp->xp_context == EXPAND_FILES)
4290 flags |= EW_FILE;
4291 else
4292 flags = (flags | EW_DIR) & ~EW_FILE;
4293 ret = expand_wildcards(1, &pat, num_file, file, flags);
4294 if (free_pat)
4295 vim_free(pat);
4296 return ret;
4297 }
4298
4299 *file = (char_u **)"";
4300 *num_file = 0;
4301 if (xp->xp_context == EXPAND_HELP)
4302 {
4303 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4304 {
4305#ifdef FEAT_MULTI_LANG
4306 cleanup_help_tags(*num_file, *file);
4307#endif
4308 return OK;
4309 }
4310 return FAIL;
4311 }
4312
4313#ifndef FEAT_CMDL_COMPL
4314 return FAIL;
4315#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004316 if (xp->xp_context == EXPAND_SHELLCMD)
4317 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 if (xp->xp_context == EXPAND_OLD_SETTING)
4319 return ExpandOldSetting(num_file, file);
4320 if (xp->xp_context == EXPAND_BUFFERS)
4321 return ExpandBufnames(pat, num_file, file, options);
4322 if (xp->xp_context == EXPAND_TAGS
4323 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4324 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4325 if (xp->xp_context == EXPAND_COLORS)
4326 return ExpandRTDir(pat, num_file, file, "colors");
4327 if (xp->xp_context == EXPAND_COMPILER)
4328 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004329# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4330 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004331 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004332# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333
4334 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4335 if (regmatch.regprog == NULL)
4336 return FAIL;
4337
4338 /* set ignore-case according to p_ic, p_scs and pat */
4339 regmatch.rm_ic = ignorecase(pat);
4340
4341 if (xp->xp_context == EXPAND_SETTINGS
4342 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4343 ret = ExpandSettings(xp, &regmatch, num_file, file);
4344 else if (xp->xp_context == EXPAND_MAPPINGS)
4345 ret = ExpandMappings(&regmatch, num_file, file);
4346# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4347 else if (xp->xp_context == EXPAND_USER_DEFINED)
4348 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4349# endif
4350 else
4351 {
4352 static struct expgen
4353 {
4354 int context;
4355 char_u *((*func)__ARGS((expand_T *, int)));
4356 int ic;
4357 } tab[] =
4358 {
4359 {EXPAND_COMMANDS, get_command_name, FALSE},
4360#ifdef FEAT_USR_CMDS
4361 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4362 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4363 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4364 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4365#endif
4366#ifdef FEAT_EVAL
4367 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4368 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4369 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4370 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4371#endif
4372#ifdef FEAT_MENU
4373 {EXPAND_MENUS, get_menu_name, FALSE},
4374 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4375#endif
4376#ifdef FEAT_SYN_HL
4377 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4378#endif
4379 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4380#ifdef FEAT_AUTOCMD
4381 {EXPAND_EVENTS, get_event_name, TRUE},
4382 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4383#endif
4384#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4385 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4386 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4387#endif
4388 {EXPAND_ENV_VARS, get_env_name, TRUE},
4389 };
4390 int i;
4391
4392 /*
4393 * Find a context in the table and call the ExpandGeneric() with the
4394 * right function to do the expansion.
4395 */
4396 ret = FAIL;
4397 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4398 if (xp->xp_context == tab[i].context)
4399 {
4400 if (tab[i].ic)
4401 regmatch.rm_ic = TRUE;
4402 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4403 break;
4404 }
4405 }
4406
4407 vim_free(regmatch.regprog);
4408
4409 return ret;
4410#endif /* FEAT_CMDL_COMPL */
4411}
4412
4413#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4414/*
4415 * Expand a list of names.
4416 *
4417 * Generic function for command line completion. It calls a function to
4418 * obtain strings, one by one. The strings are matched against a regexp
4419 * program. Matching strings are copied into an array, which is returned.
4420 *
4421 * Returns OK when no problems encountered, FAIL for error (out of memory).
4422 */
4423 int
4424ExpandGeneric(xp, regmatch, num_file, file, func)
4425 expand_T *xp;
4426 regmatch_T *regmatch;
4427 int *num_file;
4428 char_u ***file;
4429 char_u *((*func)__ARGS((expand_T *, int)));
4430 /* returns a string from the list */
4431{
4432 int i;
4433 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004434 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 char_u *str;
4436
4437 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004438 * round == 0: count the number of matching names
4439 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004441 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
4443 for (i = 0; ; ++i)
4444 {
4445 str = (*func)(xp, i);
4446 if (str == NULL) /* end of list */
4447 break;
4448 if (*str == NUL) /* skip empty strings */
4449 continue;
4450
4451 if (vim_regexec(regmatch, str, (colnr_T)0))
4452 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004453 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 {
4455 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4456 (*file)[count] = str;
4457#ifdef FEAT_MENU
4458 if (func == get_menu_names && str != NULL)
4459 {
4460 /* test for separator added by get_menu_names() */
4461 str += STRLEN(str) - 1;
4462 if (*str == '\001')
4463 *str = '.';
4464 }
4465#endif
4466 }
4467 ++count;
4468 }
4469 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004470 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
4472 if (count == 0)
4473 return OK;
4474 *num_file = count;
4475 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4476 if (*file == NULL)
4477 {
4478 *file = (char_u **)"";
4479 return FAIL;
4480 }
4481 count = 0;
4482 }
4483 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004484
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004485 /* Sort the results. Keep menu's in the specified order. */
4486 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4487 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004488
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 return OK;
4490}
4491
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004492/*
4493 * Complete a shell command.
4494 * Returns FAIL or OK;
4495 */
4496 static int
4497expand_shellcmd(filepat, num_file, file, flagsarg)
4498 char_u *filepat; /* pattern to match with command names */
4499 int *num_file; /* return: number of matches */
4500 char_u ***file; /* return: array with matches */
4501 int flagsarg; /* EW_ flags */
4502{
4503 char_u *pat;
4504 int i;
4505 char_u *path;
4506 int mustfree = FALSE;
4507 garray_T ga;
4508 char_u *buf = alloc(MAXPATHL);
4509 size_t l;
4510 char_u *s, *e;
4511 int flags = flagsarg;
4512 int ret;
4513
4514 if (buf == NULL)
4515 return FAIL;
4516
4517 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4518 * space */
4519 pat = vim_strsave(filepat);
4520 for (i = 0; pat[i]; ++i)
4521 if (pat[i] == '\\' && pat[i + 1] == ' ')
4522 STRCPY(pat + i, pat + i + 1);
4523
4524 flags |= EW_FILE | EW_EXEC;
4525
4526 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004527 if (mch_isFullName(pat))
4528 path = (char_u *)" ";
4529 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004530 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4531 path = (char_u *)".";
4532 else
4533 path = vim_getenv((char_u *)"PATH", &mustfree);
4534
4535 /*
4536 * Go over all directories in $PATH. Expand matches in that directory and
4537 * collect them in "ga".
4538 */
4539 ga_init2(&ga, (int)sizeof(char *), 10);
4540 for (s = path; *s != NUL; s = e)
4541 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004542 if (*s == ' ')
4543 ++s; /* Skip space used for absolute path name. */
4544
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004545#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4546 e = vim_strchr(s, ';');
4547#else
4548 e = vim_strchr(s, ':');
4549#endif
4550 if (e == NULL)
4551 e = s + STRLEN(s);
4552
4553 l = e - s;
4554 if (l > MAXPATHL - 5)
4555 break;
4556 vim_strncpy(buf, s, l);
4557 add_pathsep(buf);
4558 l = STRLEN(buf);
4559 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4560
4561 /* Expand matches in one directory of $PATH. */
4562 ret = expand_wildcards(1, &buf, num_file, file, flags);
4563 if (ret == OK)
4564 {
4565 if (ga_grow(&ga, *num_file) == FAIL)
4566 FreeWild(*num_file, *file);
4567 else
4568 {
4569 for (i = 0; i < *num_file; ++i)
4570 {
4571 s = (*file)[i];
4572 if (STRLEN(s) > l)
4573 {
4574 /* Remove the path again. */
4575 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4576 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4577 }
4578 else
4579 vim_free(s);
4580 }
4581 vim_free(*file);
4582 }
4583 }
4584 if (*e != NUL)
4585 ++e;
4586 }
4587 *file = ga.ga_data;
4588 *num_file = ga.ga_len;
4589
4590 vim_free(buf);
4591 vim_free(pat);
4592 if (mustfree)
4593 vim_free(path);
4594 return OK;
4595}
4596
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004599static 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));
4600
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004602 * call "user_expand_func()" to invoke a user defined VimL function and return
4603 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004605 static void *
4606call_user_expand_func(user_expand_func, xp, num_file, file)
4607 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 int *num_file;
4610 char_u ***file;
4611{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004612 char_u keep;
4613 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004616 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004617 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
4619 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004620 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 *num_file = 0;
4622 *file = NULL;
4623
4624 keep = ccline.cmdbuff[ccline.cmdlen];
4625 ccline.cmdbuff[ccline.cmdlen] = 0;
4626 sprintf((char *)num, "%d", ccline.cmdpos);
4627 args[0] = xp->xp_pattern;
4628 args[1] = ccline.cmdbuff;
4629 args[2] = num;
4630
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004631 /* Save the cmdline, we don't know what the function may do. */
4632 save_ccline = ccline;
4633 ccline.cmdbuff = NULL;
4634 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004636
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004637 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004638
4639 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004641
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004643
4644 return ret;
4645}
4646
4647/*
4648 * Expand names with a function defined by the user.
4649 */
4650 static int
4651ExpandUserDefined(xp, regmatch, num_file, file)
4652 expand_T *xp;
4653 regmatch_T *regmatch;
4654 int *num_file;
4655 char_u ***file;
4656{
4657 char_u *retstr;
4658 char_u *s;
4659 char_u *e;
4660 char_u keep;
4661 garray_T ga;
4662
4663 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4664 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 return FAIL;
4666
4667 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004668 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {
4670 e = vim_strchr(s, '\n');
4671 if (e == NULL)
4672 e = s + STRLEN(s);
4673 keep = *e;
4674 *e = 0;
4675
4676 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4677 {
4678 *e = keep;
4679 if (*e != NUL)
4680 ++e;
4681 continue;
4682 }
4683
4684 if (ga_grow(&ga, 1) == FAIL)
4685 break;
4686
4687 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4688 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
4690 *e = keep;
4691 if (*e != NUL)
4692 ++e;
4693 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004694 vim_free(retstr);
4695 *file = ga.ga_data;
4696 *num_file = ga.ga_len;
4697 return OK;
4698}
4699
4700/*
4701 * Expand names with a list returned by a function defined by the user.
4702 */
4703 static int
4704ExpandUserList(xp, num_file, file)
4705 expand_T *xp;
4706 int *num_file;
4707 char_u ***file;
4708{
4709 list_T *retlist;
4710 listitem_T *li;
4711 garray_T ga;
4712
4713 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4714 if (retlist == NULL)
4715 return FAIL;
4716
4717 ga_init2(&ga, (int)sizeof(char *), 3);
4718 /* Loop over the items in the list. */
4719 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4720 {
4721 if (li->li_tv.v_type != VAR_STRING)
4722 continue; /* Skip non-string items */
4723
4724 if (ga_grow(&ga, 1) == FAIL)
4725 break;
4726
4727 ((char_u **)ga.ga_data)[ga.ga_len] =
4728 vim_strsave(li->li_tv.vval.v_string);
4729 ++ga.ga_len;
4730 }
4731 list_unref(retlist);
4732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 *file = ga.ga_data;
4734 *num_file = ga.ga_len;
4735 return OK;
4736}
4737#endif
4738
4739/*
4740 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4741 * or compiler names.
4742 */
4743 static int
4744ExpandRTDir(pat, num_file, file, dirname)
4745 char_u *pat;
4746 int *num_file;
4747 char_u ***file;
4748 char *dirname; /* "colors" or "compiler" */
4749{
4750 char_u *all;
4751 char_u *s;
4752 char_u *e;
4753 garray_T ga;
4754
4755 *num_file = 0;
4756 *file = NULL;
4757 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4758 if (s == NULL)
4759 return FAIL;
4760 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4761 all = globpath(p_rtp, s);
4762 vim_free(s);
4763 if (all == NULL)
4764 return FAIL;
4765
4766 ga_init2(&ga, (int)sizeof(char *), 3);
4767 for (s = all; *s != NUL; s = e)
4768 {
4769 e = vim_strchr(s, '\n');
4770 if (e == NULL)
4771 e = s + STRLEN(s);
4772 if (ga_grow(&ga, 1) == FAIL)
4773 break;
4774 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4775 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004776 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 if (*s == '\n' || vim_ispathsep(*s))
4778 break;
4779 ++s;
4780 ((char_u **)ga.ga_data)[ga.ga_len] =
4781 vim_strnsave(s, (int)(e - s - 4));
4782 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
4784 if (*e != NUL)
4785 ++e;
4786 }
4787 vim_free(all);
4788 *file = ga.ga_data;
4789 *num_file = ga.ga_len;
4790 return OK;
4791}
4792
4793#endif
4794
4795#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4796/*
4797 * Expand "file" for all comma-separated directories in "path".
4798 * Returns an allocated string with all matches concatenated, separated by
4799 * newlines. Returns NULL for an error or no matches.
4800 */
4801 char_u *
4802globpath(path, file)
4803 char_u *path;
4804 char_u *file;
4805{
4806 expand_T xpc;
4807 char_u *buf;
4808 garray_T ga;
4809 int i;
4810 int len;
4811 int num_p;
4812 char_u **p;
4813 char_u *cur = NULL;
4814
4815 buf = alloc(MAXPATHL);
4816 if (buf == NULL)
4817 return NULL;
4818
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004819 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 ga_init2(&ga, 1, 100);
4823
4824 /* Loop over all entries in {path}. */
4825 while (*path != NUL)
4826 {
4827 /* Copy one item of the path to buf[] and concatenate the file name. */
4828 copy_option_part(&path, buf, MAXPATHL, ",");
4829 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4830 {
4831 add_pathsep(buf);
4832 STRCAT(buf, file);
4833 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4834 && num_p > 0)
4835 {
4836 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4837 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004838 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
4840 /* Concatenate new results to previous ones. */
4841 if (ga_grow(&ga, len) == OK)
4842 {
4843 cur = (char_u *)ga.ga_data + ga.ga_len;
4844 for (i = 0; i < num_p; ++i)
4845 {
4846 STRCPY(cur, p[i]);
4847 cur += STRLEN(p[i]);
4848 *cur++ = '\n';
4849 }
4850 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
4852 FreeWild(num_p, p);
4853 }
4854 }
4855 }
4856 if (cur != NULL)
4857 *--cur = 0; /* Replace trailing newline with NUL */
4858
4859 vim_free(buf);
4860 return (char_u *)ga.ga_data;
4861}
4862
4863#endif
4864
4865#if defined(FEAT_CMDHIST) || defined(PROTO)
4866
4867/*********************************
4868 * Command line history stuff *
4869 *********************************/
4870
4871/*
4872 * Translate a history character to the associated type number.
4873 */
4874 static int
4875hist_char2type(c)
4876 int c;
4877{
4878 if (c == ':')
4879 return HIST_CMD;
4880 if (c == '=')
4881 return HIST_EXPR;
4882 if (c == '@')
4883 return HIST_INPUT;
4884 if (c == '>')
4885 return HIST_DEBUG;
4886 return HIST_SEARCH; /* must be '?' or '/' */
4887}
4888
4889/*
4890 * Table of history names.
4891 * These names are used in :history and various hist...() functions.
4892 * It is sufficient to give the significant prefix of a history name.
4893 */
4894
4895static char *(history_names[]) =
4896{
4897 "cmd",
4898 "search",
4899 "expr",
4900 "input",
4901 "debug",
4902 NULL
4903};
4904
4905/*
4906 * init_history() - Initialize the command line history.
4907 * Also used to re-allocate the history when the size changes.
4908 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004909 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910init_history()
4911{
4912 int newlen; /* new length of history table */
4913 histentry_T *temp;
4914 int i;
4915 int j;
4916 int type;
4917
4918 /*
4919 * If size of history table changed, reallocate it
4920 */
4921 newlen = (int)p_hi;
4922 if (newlen != hislen) /* history length changed */
4923 {
4924 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4925 {
4926 if (newlen)
4927 {
4928 temp = (histentry_T *)lalloc(
4929 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4930 if (temp == NULL) /* out of memory! */
4931 {
4932 if (type == 0) /* first one: just keep the old length */
4933 {
4934 newlen = hislen;
4935 break;
4936 }
4937 /* Already changed one table, now we can only have zero
4938 * length for all tables. */
4939 newlen = 0;
4940 type = -1;
4941 continue;
4942 }
4943 }
4944 else
4945 temp = NULL;
4946 if (newlen == 0 || temp != NULL)
4947 {
4948 if (hisidx[type] < 0) /* there are no entries yet */
4949 {
4950 for (i = 0; i < newlen; ++i)
4951 {
4952 temp[i].hisnum = 0;
4953 temp[i].hisstr = NULL;
4954 }
4955 }
4956 else if (newlen > hislen) /* array becomes bigger */
4957 {
4958 for (i = 0; i <= hisidx[type]; ++i)
4959 temp[i] = history[type][i];
4960 j = i;
4961 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4962 {
4963 temp[i].hisnum = 0;
4964 temp[i].hisstr = NULL;
4965 }
4966 for ( ; j < hislen; ++i, ++j)
4967 temp[i] = history[type][j];
4968 }
4969 else /* array becomes smaller or 0 */
4970 {
4971 j = hisidx[type];
4972 for (i = newlen - 1; ; --i)
4973 {
4974 if (i >= 0) /* copy newest entries */
4975 temp[i] = history[type][j];
4976 else /* remove older entries */
4977 vim_free(history[type][j].hisstr);
4978 if (--j < 0)
4979 j = hislen - 1;
4980 if (j == hisidx[type])
4981 break;
4982 }
4983 hisidx[type] = newlen - 1;
4984 }
4985 vim_free(history[type]);
4986 history[type] = temp;
4987 }
4988 }
4989 hislen = newlen;
4990 }
4991}
4992
4993/*
4994 * Check if command line 'str' is already in history.
4995 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4996 */
4997 static int
4998in_history(type, str, move_to_front)
4999 int type;
5000 char_u *str;
5001 int move_to_front; /* Move the entry to the front if it exists */
5002{
5003 int i;
5004 int last_i = -1;
5005
5006 if (hisidx[type] < 0)
5007 return FALSE;
5008 i = hisidx[type];
5009 do
5010 {
5011 if (history[type][i].hisstr == NULL)
5012 return FALSE;
5013 if (STRCMP(str, history[type][i].hisstr) == 0)
5014 {
5015 if (!move_to_front)
5016 return TRUE;
5017 last_i = i;
5018 break;
5019 }
5020 if (--i < 0)
5021 i = hislen - 1;
5022 } while (i != hisidx[type]);
5023
5024 if (last_i >= 0)
5025 {
5026 str = history[type][i].hisstr;
5027 while (i != hisidx[type])
5028 {
5029 if (++i >= hislen)
5030 i = 0;
5031 history[type][last_i] = history[type][i];
5032 last_i = i;
5033 }
5034 history[type][i].hisstr = str;
5035 history[type][i].hisnum = ++hisnum[type];
5036 return TRUE;
5037 }
5038 return FALSE;
5039}
5040
5041/*
5042 * Convert history name (from table above) to its HIST_ equivalent.
5043 * When "name" is empty, return "cmd" history.
5044 * Returns -1 for unknown history name.
5045 */
5046 int
5047get_histtype(name)
5048 char_u *name;
5049{
5050 int i;
5051 int len = (int)STRLEN(name);
5052
5053 /* No argument: use current history. */
5054 if (len == 0)
5055 return hist_char2type(ccline.cmdfirstc);
5056
5057 for (i = 0; history_names[i] != NULL; ++i)
5058 if (STRNICMP(name, history_names[i], len) == 0)
5059 return i;
5060
5061 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5062 return hist_char2type(name[0]);
5063
5064 return -1;
5065}
5066
5067static int last_maptick = -1; /* last seen maptick */
5068
5069/*
5070 * Add the given string to the given history. If the string is already in the
5071 * history then it is moved to the front. "histype" may be one of he HIST_
5072 * values.
5073 */
5074 void
5075add_to_history(histype, new_entry, in_map, sep)
5076 int histype;
5077 char_u *new_entry;
5078 int in_map; /* consider maptick when inside a mapping */
5079 int sep; /* separator character used (search hist) */
5080{
5081 histentry_T *hisptr;
5082 int len;
5083
5084 if (hislen == 0) /* no history */
5085 return;
5086
5087 /*
5088 * Searches inside the same mapping overwrite each other, so that only
5089 * the last line is kept. Be careful not to remove a line that was moved
5090 * down, only lines that were added.
5091 */
5092 if (histype == HIST_SEARCH && in_map)
5093 {
5094 if (maptick == last_maptick)
5095 {
5096 /* Current line is from the same mapping, remove it */
5097 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5098 vim_free(hisptr->hisstr);
5099 hisptr->hisstr = NULL;
5100 hisptr->hisnum = 0;
5101 --hisnum[histype];
5102 if (--hisidx[HIST_SEARCH] < 0)
5103 hisidx[HIST_SEARCH] = hislen - 1;
5104 }
5105 last_maptick = -1;
5106 }
5107 if (!in_history(histype, new_entry, TRUE))
5108 {
5109 if (++hisidx[histype] == hislen)
5110 hisidx[histype] = 0;
5111 hisptr = &history[histype][hisidx[histype]];
5112 vim_free(hisptr->hisstr);
5113
5114 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005115 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5117 if (hisptr->hisstr != NULL)
5118 hisptr->hisstr[len + 1] = sep;
5119
5120 hisptr->hisnum = ++hisnum[histype];
5121 if (histype == HIST_SEARCH && in_map)
5122 last_maptick = maptick;
5123 }
5124}
5125
5126#if defined(FEAT_EVAL) || defined(PROTO)
5127
5128/*
5129 * Get identifier of newest history entry.
5130 * "histype" may be one of the HIST_ values.
5131 */
5132 int
5133get_history_idx(histype)
5134 int histype;
5135{
5136 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5137 || hisidx[histype] < 0)
5138 return -1;
5139
5140 return history[histype][hisidx[histype]].hisnum;
5141}
5142
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005143static struct cmdline_info *get_ccline_ptr __ARGS((void));
5144
5145/*
5146 * Get pointer to the command line info to use. cmdline_paste() may clear
5147 * ccline and put the previous value in prev_ccline.
5148 */
5149 static struct cmdline_info *
5150get_ccline_ptr()
5151{
5152 if ((State & CMDLINE) == 0)
5153 return NULL;
5154 if (ccline.cmdbuff != NULL)
5155 return &ccline;
5156 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5157 return &prev_ccline;
5158 return NULL;
5159}
5160
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161/*
5162 * Get the current command line in allocated memory.
5163 * Only works when the command line is being edited.
5164 * Returns NULL when something is wrong.
5165 */
5166 char_u *
5167get_cmdline_str()
5168{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005169 struct cmdline_info *p = get_ccline_ptr();
5170
5171 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005173 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174}
5175
5176/*
5177 * Get the current command line position, counted in bytes.
5178 * Zero is the first position.
5179 * Only works when the command line is being edited.
5180 * Returns -1 when something is wrong.
5181 */
5182 int
5183get_cmdline_pos()
5184{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005185 struct cmdline_info *p = get_ccline_ptr();
5186
5187 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005189 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190}
5191
5192/*
5193 * Set the command line byte position to "pos". Zero is the first position.
5194 * Only works when the command line is being edited.
5195 * Returns 1 when failed, 0 when OK.
5196 */
5197 int
5198set_cmdline_pos(pos)
5199 int pos;
5200{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005201 struct cmdline_info *p = get_ccline_ptr();
5202
5203 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 return 1;
5205
5206 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5207 * changed the command line. */
5208 if (pos < 0)
5209 new_cmdpos = 0;
5210 else
5211 new_cmdpos = pos;
5212 return 0;
5213}
5214
5215/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005216 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005217 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005218 * Only works when the command line is being edited.
5219 * Returns NUL when something is wrong.
5220 */
5221 int
5222get_cmdline_type()
5223{
5224 struct cmdline_info *p = get_ccline_ptr();
5225
5226 if (p == NULL)
5227 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005228 if (p->cmdfirstc == NUL)
5229 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005230 return p->cmdfirstc;
5231}
5232
5233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 * Calculate history index from a number:
5235 * num > 0: seen as identifying number of a history entry
5236 * num < 0: relative position in history wrt newest entry
5237 * "histype" may be one of the HIST_ values.
5238 */
5239 static int
5240calc_hist_idx(histype, num)
5241 int histype;
5242 int num;
5243{
5244 int i;
5245 histentry_T *hist;
5246 int wrapped = FALSE;
5247
5248 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5249 || (i = hisidx[histype]) < 0 || num == 0)
5250 return -1;
5251
5252 hist = history[histype];
5253 if (num > 0)
5254 {
5255 while (hist[i].hisnum > num)
5256 if (--i < 0)
5257 {
5258 if (wrapped)
5259 break;
5260 i += hislen;
5261 wrapped = TRUE;
5262 }
5263 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5264 return i;
5265 }
5266 else if (-num <= hislen)
5267 {
5268 i += num + 1;
5269 if (i < 0)
5270 i += hislen;
5271 if (hist[i].hisstr != NULL)
5272 return i;
5273 }
5274 return -1;
5275}
5276
5277/*
5278 * Get a history entry by its index.
5279 * "histype" may be one of the HIST_ values.
5280 */
5281 char_u *
5282get_history_entry(histype, idx)
5283 int histype;
5284 int idx;
5285{
5286 idx = calc_hist_idx(histype, idx);
5287 if (idx >= 0)
5288 return history[histype][idx].hisstr;
5289 else
5290 return (char_u *)"";
5291}
5292
5293/*
5294 * Clear all entries of a history.
5295 * "histype" may be one of the HIST_ values.
5296 */
5297 int
5298clr_history(histype)
5299 int histype;
5300{
5301 int i;
5302 histentry_T *hisptr;
5303
5304 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5305 {
5306 hisptr = history[histype];
5307 for (i = hislen; i--;)
5308 {
5309 vim_free(hisptr->hisstr);
5310 hisptr->hisnum = 0;
5311 hisptr++->hisstr = NULL;
5312 }
5313 hisidx[histype] = -1; /* mark history as cleared */
5314 hisnum[histype] = 0; /* reset identifier counter */
5315 return OK;
5316 }
5317 return FAIL;
5318}
5319
5320/*
5321 * Remove all entries matching {str} from a history.
5322 * "histype" may be one of the HIST_ values.
5323 */
5324 int
5325del_history_entry(histype, str)
5326 int histype;
5327 char_u *str;
5328{
5329 regmatch_T regmatch;
5330 histentry_T *hisptr;
5331 int idx;
5332 int i;
5333 int last;
5334 int found = FALSE;
5335
5336 regmatch.regprog = NULL;
5337 regmatch.rm_ic = FALSE; /* always match case */
5338 if (hislen != 0
5339 && histype >= 0
5340 && histype < HIST_COUNT
5341 && *str != NUL
5342 && (idx = hisidx[histype]) >= 0
5343 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5344 != NULL)
5345 {
5346 i = last = idx;
5347 do
5348 {
5349 hisptr = &history[histype][i];
5350 if (hisptr->hisstr == NULL)
5351 break;
5352 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5353 {
5354 found = TRUE;
5355 vim_free(hisptr->hisstr);
5356 hisptr->hisstr = NULL;
5357 hisptr->hisnum = 0;
5358 }
5359 else
5360 {
5361 if (i != last)
5362 {
5363 history[histype][last] = *hisptr;
5364 hisptr->hisstr = NULL;
5365 hisptr->hisnum = 0;
5366 }
5367 if (--last < 0)
5368 last += hislen;
5369 }
5370 if (--i < 0)
5371 i += hislen;
5372 } while (i != idx);
5373 if (history[histype][idx].hisstr == NULL)
5374 hisidx[histype] = -1;
5375 }
5376 vim_free(regmatch.regprog);
5377 return found;
5378}
5379
5380/*
5381 * Remove an indexed entry from a history.
5382 * "histype" may be one of the HIST_ values.
5383 */
5384 int
5385del_history_idx(histype, idx)
5386 int histype;
5387 int idx;
5388{
5389 int i, j;
5390
5391 i = calc_hist_idx(histype, idx);
5392 if (i < 0)
5393 return FALSE;
5394 idx = hisidx[histype];
5395 vim_free(history[histype][i].hisstr);
5396
5397 /* When deleting the last added search string in a mapping, reset
5398 * last_maptick, so that the last added search string isn't deleted again.
5399 */
5400 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5401 last_maptick = -1;
5402
5403 while (i != idx)
5404 {
5405 j = (i + 1) % hislen;
5406 history[histype][i] = history[histype][j];
5407 i = j;
5408 }
5409 history[histype][i].hisstr = NULL;
5410 history[histype][i].hisnum = 0;
5411 if (--i < 0)
5412 i += hislen;
5413 hisidx[histype] = i;
5414 return TRUE;
5415}
5416
5417#endif /* FEAT_EVAL */
5418
5419#if defined(FEAT_CRYPT) || defined(PROTO)
5420/*
5421 * Very specific function to remove the value in ":set key=val" from the
5422 * history.
5423 */
5424 void
5425remove_key_from_history()
5426{
5427 char_u *p;
5428 int i;
5429
5430 i = hisidx[HIST_CMD];
5431 if (i < 0)
5432 return;
5433 p = history[HIST_CMD][i].hisstr;
5434 if (p != NULL)
5435 for ( ; *p; ++p)
5436 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5437 {
5438 p = vim_strchr(p + 3, '=');
5439 if (p == NULL)
5440 break;
5441 ++p;
5442 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5443 if (p[i] == '\\' && p[i + 1])
5444 ++i;
5445 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5446 --p;
5447 }
5448}
5449#endif
5450
5451#endif /* FEAT_CMDHIST */
5452
5453#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5454/*
5455 * Get indices "num1,num2" that specify a range within a list (not a range of
5456 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5457 * Returns OK if parsed successfully, otherwise FAIL.
5458 */
5459 int
5460get_list_range(str, num1, num2)
5461 char_u **str;
5462 int *num1;
5463 int *num2;
5464{
5465 int len;
5466 int first = FALSE;
5467 long num;
5468
5469 *str = skipwhite(*str);
5470 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5471 {
5472 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5473 *str += len;
5474 *num1 = (int)num;
5475 first = TRUE;
5476 }
5477 *str = skipwhite(*str);
5478 if (**str == ',') /* parse "to" part of range */
5479 {
5480 *str = skipwhite(*str + 1);
5481 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5482 if (len > 0)
5483 {
5484 *num2 = (int)num;
5485 *str = skipwhite(*str + len);
5486 }
5487 else if (!first) /* no number given at all */
5488 return FAIL;
5489 }
5490 else if (first) /* only one number given */
5491 *num2 = *num1;
5492 return OK;
5493}
5494#endif
5495
5496#if defined(FEAT_CMDHIST) || defined(PROTO)
5497/*
5498 * :history command - print a history
5499 */
5500 void
5501ex_history(eap)
5502 exarg_T *eap;
5503{
5504 histentry_T *hist;
5505 int histype1 = HIST_CMD;
5506 int histype2 = HIST_CMD;
5507 int hisidx1 = 1;
5508 int hisidx2 = -1;
5509 int idx;
5510 int i, j, k;
5511 char_u *end;
5512 char_u *arg = eap->arg;
5513
5514 if (hislen == 0)
5515 {
5516 MSG(_("'history' option is zero"));
5517 return;
5518 }
5519
5520 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5521 {
5522 end = arg;
5523 while (ASCII_ISALPHA(*end)
5524 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5525 end++;
5526 i = *end;
5527 *end = NUL;
5528 histype1 = get_histtype(arg);
5529 if (histype1 == -1)
5530 {
5531 if (STRICMP(arg, "all") == 0)
5532 {
5533 histype1 = 0;
5534 histype2 = HIST_COUNT-1;
5535 }
5536 else
5537 {
5538 *end = i;
5539 EMSG(_(e_trailing));
5540 return;
5541 }
5542 }
5543 else
5544 histype2 = histype1;
5545 *end = i;
5546 }
5547 else
5548 end = arg;
5549 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5550 {
5551 EMSG(_(e_trailing));
5552 return;
5553 }
5554
5555 for (; !got_int && histype1 <= histype2; ++histype1)
5556 {
5557 STRCPY(IObuff, "\n # ");
5558 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5559 MSG_PUTS_TITLE(IObuff);
5560 idx = hisidx[histype1];
5561 hist = history[histype1];
5562 j = hisidx1;
5563 k = hisidx2;
5564 if (j < 0)
5565 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5566 if (k < 0)
5567 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5568 if (idx >= 0 && j <= k)
5569 for (i = idx + 1; !got_int; ++i)
5570 {
5571 if (i == hislen)
5572 i = 0;
5573 if (hist[i].hisstr != NULL
5574 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5575 {
5576 msg_putchar('\n');
5577 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5578 hist[i].hisnum);
5579 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5580 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5581 (int)Columns - 10);
5582 else
5583 STRCAT(IObuff, hist[i].hisstr);
5584 msg_outtrans(IObuff);
5585 out_flush();
5586 }
5587 if (i == idx)
5588 break;
5589 }
5590 }
5591}
5592#endif
5593
5594#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5595static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5596static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5597static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5598static int viminfo_add_at_front = FALSE;
5599
5600static int hist_type2char __ARGS((int type, int use_question));
5601
5602/*
5603 * Translate a history type number to the associated character.
5604 */
5605 static int
5606hist_type2char(type, use_question)
5607 int type;
5608 int use_question; /* use '?' instead of '/' */
5609{
5610 if (type == HIST_CMD)
5611 return ':';
5612 if (type == HIST_SEARCH)
5613 {
5614 if (use_question)
5615 return '?';
5616 else
5617 return '/';
5618 }
5619 if (type == HIST_EXPR)
5620 return '=';
5621 return '@';
5622}
5623
5624/*
5625 * Prepare for reading the history from the viminfo file.
5626 * This allocates history arrays to store the read history lines.
5627 */
5628 void
5629prepare_viminfo_history(asklen)
5630 int asklen;
5631{
5632 int i;
5633 int num;
5634 int type;
5635 int len;
5636
5637 init_history();
5638 viminfo_add_at_front = (asklen != 0);
5639 if (asklen > hislen)
5640 asklen = hislen;
5641
5642 for (type = 0; type < HIST_COUNT; ++type)
5643 {
5644 /*
5645 * Count the number of empty spaces in the history list. If there are
5646 * more spaces available than we request, then fill them up.
5647 */
5648 for (i = 0, num = 0; i < hislen; i++)
5649 if (history[type][i].hisstr == NULL)
5650 num++;
5651 len = asklen;
5652 if (num > len)
5653 len = num;
5654 if (len <= 0)
5655 viminfo_history[type] = NULL;
5656 else
5657 viminfo_history[type] =
5658 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5659 if (viminfo_history[type] == NULL)
5660 len = 0;
5661 viminfo_hislen[type] = len;
5662 viminfo_hisidx[type] = 0;
5663 }
5664}
5665
5666/*
5667 * Accept a line from the viminfo, store it in the history array when it's
5668 * new.
5669 */
5670 int
5671read_viminfo_history(virp)
5672 vir_T *virp;
5673{
5674 int type;
5675 long_u len;
5676 char_u *val;
5677 char_u *p;
5678
5679 type = hist_char2type(virp->vir_line[0]);
5680 if (viminfo_hisidx[type] < viminfo_hislen[type])
5681 {
5682 val = viminfo_readstring(virp, 1, TRUE);
5683 if (val != NULL && *val != NUL)
5684 {
5685 if (!in_history(type, val + (type == HIST_SEARCH),
5686 viminfo_add_at_front))
5687 {
5688 /* Need to re-allocate to append the separator byte. */
5689 len = STRLEN(val);
5690 p = lalloc(len + 2, TRUE);
5691 if (p != NULL)
5692 {
5693 if (type == HIST_SEARCH)
5694 {
5695 /* Search entry: Move the separator from the first
5696 * column to after the NUL. */
5697 mch_memmove(p, val + 1, (size_t)len);
5698 p[len] = (*val == ' ' ? NUL : *val);
5699 }
5700 else
5701 {
5702 /* Not a search entry: No separator in the viminfo
5703 * file, add a NUL separator. */
5704 mch_memmove(p, val, (size_t)len + 1);
5705 p[len + 1] = NUL;
5706 }
5707 viminfo_history[type][viminfo_hisidx[type]++] = p;
5708 }
5709 }
5710 }
5711 vim_free(val);
5712 }
5713 return viminfo_readline(virp);
5714}
5715
5716 void
5717finish_viminfo_history()
5718{
5719 int idx;
5720 int i;
5721 int type;
5722
5723 for (type = 0; type < HIST_COUNT; ++type)
5724 {
5725 if (history[type] == NULL)
5726 return;
5727 idx = hisidx[type] + viminfo_hisidx[type];
5728 if (idx >= hislen)
5729 idx -= hislen;
5730 else if (idx < 0)
5731 idx = hislen - 1;
5732 if (viminfo_add_at_front)
5733 hisidx[type] = idx;
5734 else
5735 {
5736 if (hisidx[type] == -1)
5737 hisidx[type] = hislen - 1;
5738 do
5739 {
5740 if (history[type][idx].hisstr != NULL)
5741 break;
5742 if (++idx == hislen)
5743 idx = 0;
5744 } while (idx != hisidx[type]);
5745 if (idx != hisidx[type] && --idx < 0)
5746 idx = hislen - 1;
5747 }
5748 for (i = 0; i < viminfo_hisidx[type]; i++)
5749 {
5750 vim_free(history[type][idx].hisstr);
5751 history[type][idx].hisstr = viminfo_history[type][i];
5752 if (--idx < 0)
5753 idx = hislen - 1;
5754 }
5755 idx += 1;
5756 idx %= hislen;
5757 for (i = 0; i < viminfo_hisidx[type]; i++)
5758 {
5759 history[type][idx++].hisnum = ++hisnum[type];
5760 idx %= hislen;
5761 }
5762 vim_free(viminfo_history[type]);
5763 viminfo_history[type] = NULL;
5764 }
5765}
5766
5767 void
5768write_viminfo_history(fp)
5769 FILE *fp;
5770{
5771 int i;
5772 int type;
5773 int num_saved;
5774 char_u *p;
5775 int c;
5776
5777 init_history();
5778 if (hislen == 0)
5779 return;
5780 for (type = 0; type < HIST_COUNT; ++type)
5781 {
5782 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5783 if (num_saved == 0)
5784 continue;
5785 if (num_saved < 0) /* Use default */
5786 num_saved = hislen;
5787 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5788 type == HIST_CMD ? _("Command Line") :
5789 type == HIST_SEARCH ? _("Search String") :
5790 type == HIST_EXPR ? _("Expression") :
5791 _("Input Line"));
5792 if (num_saved > hislen)
5793 num_saved = hislen;
5794 i = hisidx[type];
5795 if (i >= 0)
5796 while (num_saved--)
5797 {
5798 p = history[type][i].hisstr;
5799 if (p != NULL)
5800 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005801 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 /* For the search history: put the separator in the second
5803 * column; use a space if there isn't one. */
5804 if (type == HIST_SEARCH)
5805 {
5806 c = p[STRLEN(p) + 1];
5807 putc(c == NUL ? ' ' : c, fp);
5808 }
5809 viminfo_writestring(fp, p);
5810 }
5811 if (--i < 0)
5812 i = hislen - 1;
5813 }
5814 }
5815}
5816#endif /* FEAT_VIMINFO */
5817
5818#if defined(FEAT_FKMAP) || defined(PROTO)
5819/*
5820 * Write a character at the current cursor+offset position.
5821 * It is directly written into the command buffer block.
5822 */
5823 void
5824cmd_pchar(c, offset)
5825 int c, offset;
5826{
5827 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5828 {
5829 EMSG(_("E198: cmd_pchar beyond the command length"));
5830 return;
5831 }
5832 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5833 ccline.cmdbuff[ccline.cmdlen] = NUL;
5834}
5835
5836 int
5837cmd_gchar(offset)
5838 int offset;
5839{
5840 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5841 {
5842 /* EMSG(_("cmd_gchar beyond the command length")); */
5843 return NUL;
5844 }
5845 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5846}
5847#endif
5848
5849#if defined(FEAT_CMDWIN) || defined(PROTO)
5850/*
5851 * Open a window on the current command line and history. Allow editing in
5852 * the window. Returns when the window is closed.
5853 * Returns:
5854 * CR if the command is to be executed
5855 * Ctrl_C if it is to be abandoned
5856 * K_IGNORE if editing continues
5857 */
5858 static int
5859ex_window()
5860{
5861 struct cmdline_info save_ccline;
5862 buf_T *old_curbuf = curbuf;
5863 win_T *old_curwin = curwin;
5864 buf_T *bp;
5865 win_T *wp;
5866 int i;
5867 linenr_T lnum;
5868 int histtype;
5869 garray_T winsizes;
5870 char_u typestr[2];
5871 int save_restart_edit = restart_edit;
5872 int save_State = State;
5873 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005874#ifdef FEAT_RIGHTLEFT
5875 int save_cmdmsg_rl = cmdmsg_rl;
5876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
5878 /* Can't do this recursively. Can't do it when typing a password. */
5879 if (cmdwin_type != 0
5880# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5881 || cmdline_star > 0
5882# endif
5883 )
5884 {
5885 beep_flush();
5886 return K_IGNORE;
5887 }
5888
5889 /* Save current window sizes. */
5890 win_size_save(&winsizes);
5891
5892# ifdef FEAT_AUTOCMD
5893 /* Don't execute autocommands while creating the window. */
5894 ++autocmd_block;
5895# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005896 /* don't use a new tab page */
5897 cmdmod.tab = 0;
5898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 /* Create a window for the command-line buffer. */
5900 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5901 {
5902 beep_flush();
5903 return K_IGNORE;
5904 }
5905 cmdwin_type = ccline.cmdfirstc;
5906 if (cmdwin_type == NUL)
5907 cmdwin_type = '-';
5908
5909 /* Create the command-line buffer empty. */
5910 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5911 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5912 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5913 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5914 curbuf->b_p_ma = TRUE;
5915# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005916 curwin->w_p_rl = cmdmsg_rl;
5917 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918# endif
5919# ifdef FEAT_SCROLLBIND
5920 curwin->w_p_scb = FALSE;
5921# endif
5922
5923# ifdef FEAT_AUTOCMD
5924 /* Do execute autocommands for setting the filetype (load syntax). */
5925 --autocmd_block;
5926# endif
5927
Bram Moolenaar46152342005-09-07 21:18:43 +00005928 /* Showing the prompt may have set need_wait_return, reset it. */
5929 need_wait_return = FALSE;
5930
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 histtype = hist_char2type(ccline.cmdfirstc);
5932 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5933 {
5934 if (p_wc == TAB)
5935 {
5936 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5937 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5938 }
5939 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5940 }
5941
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005942 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5943 * sets 'textwidth' to 78). */
5944 curbuf->b_p_tw = 0;
5945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 /* Fill the buffer with the history. */
5947 init_history();
5948 if (hislen > 0)
5949 {
5950 i = hisidx[histtype];
5951 if (i >= 0)
5952 {
5953 lnum = 0;
5954 do
5955 {
5956 if (++i == hislen)
5957 i = 0;
5958 if (history[histtype][i].hisstr != NULL)
5959 ml_append(lnum++, history[histtype][i].hisstr,
5960 (colnr_T)0, FALSE);
5961 }
5962 while (i != hisidx[histtype]);
5963 }
5964 }
5965
5966 /* Replace the empty last line with the current command-line and put the
5967 * cursor there. */
5968 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5969 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5970 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005971 changed_line_abv_curs();
5972 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005973 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974
5975 /* Save the command line info, can be used recursively. */
5976 save_ccline = ccline;
5977 ccline.cmdbuff = NULL;
5978 ccline.cmdprompt = NULL;
5979
5980 /* No Ex mode here! */
5981 exmode_active = 0;
5982
5983 State = NORMAL;
5984# ifdef FEAT_MOUSE
5985 setmouse();
5986# endif
5987
5988# ifdef FEAT_AUTOCMD
5989 /* Trigger CmdwinEnter autocommands. */
5990 typestr[0] = cmdwin_type;
5991 typestr[1] = NUL;
5992 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00005993 if (restart_edit != 0) /* autocmd with ":startinsert" */
5994 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995# endif
5996
5997 i = RedrawingDisabled;
5998 RedrawingDisabled = 0;
5999
6000 /*
6001 * Call the main loop until <CR> or CTRL-C is typed.
6002 */
6003 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006004 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005
6006 RedrawingDisabled = i;
6007
6008# ifdef FEAT_AUTOCMD
6009 /* Trigger CmdwinLeave autocommands. */
6010 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6011# endif
6012
6013 /* Restore the comand line info. */
6014 ccline = save_ccline;
6015 cmdwin_type = 0;
6016
6017 exmode_active = save_exmode;
6018
6019 /* Safety check: The old window or buffer was deleted: It's a a bug when
6020 * this happens! */
6021 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6022 {
6023 cmdwin_result = Ctrl_C;
6024 EMSG(_("E199: Active window or buffer deleted"));
6025 }
6026 else
6027 {
6028# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6029 /* autocmds may abort script processing */
6030 if (aborting() && cmdwin_result != K_IGNORE)
6031 cmdwin_result = Ctrl_C;
6032# endif
6033 /* Set the new command line from the cmdline buffer. */
6034 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006035 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006037 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6038
6039 if (histtype == HIST_CMD)
6040 {
6041 /* Execute the command directly. */
6042 ccline.cmdbuff = vim_strsave((char_u *)p);
6043 cmdwin_result = CAR;
6044 }
6045 else
6046 {
6047 /* First need to cancel what we were doing. */
6048 ccline.cmdbuff = NULL;
6049 stuffcharReadbuff(':');
6050 stuffReadbuff((char_u *)p);
6051 stuffcharReadbuff(CAR);
6052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 }
6054 else if (cmdwin_result == K_XF2) /* :qa typed */
6055 {
6056 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6057 cmdwin_result = CAR;
6058 }
6059 else
6060 ccline.cmdbuff = vim_strsave(ml_get_curline());
6061 if (ccline.cmdbuff == NULL)
6062 cmdwin_result = Ctrl_C;
6063 else
6064 {
6065 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6066 ccline.cmdbufflen = ccline.cmdlen + 1;
6067 ccline.cmdpos = curwin->w_cursor.col;
6068 if (ccline.cmdpos > ccline.cmdlen)
6069 ccline.cmdpos = ccline.cmdlen;
6070 if (cmdwin_result == K_IGNORE)
6071 {
6072 set_cmdspos_cursor();
6073 redrawcmd();
6074 }
6075 }
6076
6077# ifdef FEAT_AUTOCMD
6078 /* Don't execute autocommands while deleting the window. */
6079 ++autocmd_block;
6080# endif
6081 wp = curwin;
6082 bp = curbuf;
6083 win_goto(old_curwin);
6084 win_close(wp, TRUE);
6085 close_buffer(NULL, bp, DOBUF_WIPE);
6086
6087 /* Restore window sizes. */
6088 win_size_restore(&winsizes);
6089
6090# ifdef FEAT_AUTOCMD
6091 --autocmd_block;
6092# endif
6093 }
6094
6095 ga_clear(&winsizes);
6096 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006097# ifdef FEAT_RIGHTLEFT
6098 cmdmsg_rl = save_cmdmsg_rl;
6099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100
6101 State = save_State;
6102# ifdef FEAT_MOUSE
6103 setmouse();
6104# endif
6105
6106 return cmdwin_result;
6107}
6108#endif /* FEAT_CMDWIN */
6109
6110/*
6111 * Used for commands that either take a simple command string argument, or:
6112 * cmd << endmarker
6113 * {script}
6114 * endmarker
6115 * Returns a pointer to allocated memory with {script} or NULL.
6116 */
6117 char_u *
6118script_get(eap, cmd)
6119 exarg_T *eap;
6120 char_u *cmd;
6121{
6122 char_u *theline;
6123 char *end_pattern = NULL;
6124 char dot[] = ".";
6125 garray_T ga;
6126
6127 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6128 return NULL;
6129
6130 ga_init2(&ga, 1, 0x400);
6131
6132 if (cmd[2] != NUL)
6133 end_pattern = (char *)skipwhite(cmd + 2);
6134 else
6135 end_pattern = dot;
6136
6137 for (;;)
6138 {
6139 theline = eap->getline(
6140#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006141 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142#endif
6143 NUL, eap->cookie, 0);
6144
6145 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6146 break;
6147
6148 ga_concat(&ga, theline);
6149 ga_append(&ga, '\n');
6150 vim_free(theline);
6151 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006152 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153
6154 return (char_u *)ga.ga_data;
6155}