blob: 4244a12eaa72b07f289787f022178847f5e9a934 [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();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001759# ifdef FEAT_WINDOWS
1760 /* May redraw the status line to show the cursor position. */
1761 if (p_ru && curwin->w_status_height > 0)
1762 curwin->w_redr_status = TRUE;
1763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001765 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001766 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001767 restore_cmdline(&save_ccline);
1768
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001769 /* Leave it at the end to make CTRL-R CTRL-W work. */
1770 if (i != 0)
1771 curwin->w_cursor = end_pos;
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 msg_starthere();
1774 redrawcmdline();
1775 did_incsearch = TRUE;
1776 }
1777#else /* FEAT_SEARCH_EXTRA */
1778 ;
1779#endif
1780
1781#ifdef FEAT_RIGHTLEFT
1782 if (cmdmsg_rl
1783# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001784 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785# endif
1786 )
1787 /* Always redraw the whole command line to fix shaping and
1788 * right-left typing. Not efficient, but it works. */
1789 redrawcmd();
1790#endif
1791 }
1792
1793returncmd:
1794
1795#ifdef FEAT_RIGHTLEFT
1796 cmdmsg_rl = FALSE;
1797#endif
1798
1799#ifdef FEAT_FKMAP
1800 cmd_fkmap = 0;
1801#endif
1802
1803 ExpandCleanup(&xpc);
1804
1805#ifdef FEAT_SEARCH_EXTRA
1806 if (did_incsearch)
1807 {
1808 curwin->w_cursor = old_cursor;
1809 curwin->w_curswant = old_curswant;
1810 curwin->w_leftcol = old_leftcol;
1811 curwin->w_topline = old_topline;
1812# ifdef FEAT_DIFF
1813 curwin->w_topfill = old_topfill;
1814# endif
1815 curwin->w_botline = old_botline;
1816 highlight_match = FALSE;
1817 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001818 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820#endif
1821
1822 if (ccline.cmdbuff != NULL)
1823 {
1824 /*
1825 * Put line in history buffer (":" and "=" only when it was typed).
1826 */
1827#ifdef FEAT_CMDHIST
1828 if (ccline.cmdlen && firstc != NUL
1829 && (some_key_typed || histype == HIST_SEARCH))
1830 {
1831 add_to_history(histype, ccline.cmdbuff, TRUE,
1832 histype == HIST_SEARCH ? firstc : NUL);
1833 if (firstc == ':')
1834 {
1835 vim_free(new_last_cmdline);
1836 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1837 }
1838 }
1839#endif
1840
1841 if (gotesc) /* abandon command line */
1842 {
1843 vim_free(ccline.cmdbuff);
1844 ccline.cmdbuff = NULL;
1845 if (msg_scrolled == 0)
1846 compute_cmdrow();
1847 MSG("");
1848 redraw_cmdline = TRUE;
1849 }
1850 }
1851
1852 /*
1853 * If the screen was shifted up, redraw the whole screen (later).
1854 * If the line is too long, clear it, so ruler and shown command do
1855 * not get printed in the middle of it.
1856 */
1857 msg_check();
1858 msg_scroll = save_msg_scroll;
1859 redir_off = FALSE;
1860
1861 /* When the command line was typed, no need for a wait-return prompt. */
1862 if (some_key_typed)
1863 need_wait_return = FALSE;
1864
1865 State = save_State;
1866#ifdef USE_IM_CONTROL
1867 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1868 im_save_status(b_im_ptr);
1869 im_set_active(FALSE);
1870#endif
1871#ifdef FEAT_MOUSE
1872 setmouse();
1873#endif
1874#ifdef CURSOR_SHAPE
1875 ui_cursor_shape(); /* may show different cursor shape */
1876#endif
1877
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001878 {
1879 char_u *p = ccline.cmdbuff;
1880
1881 /* Make ccline empty, getcmdline() may try to use it. */
1882 ccline.cmdbuff = NULL;
1883 return p;
1884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885}
1886
1887#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1888/*
1889 * Get a command line with a prompt.
1890 * This is prepared to be called recursively from getcmdline() (e.g. by
1891 * f_input() when evaluating an expression from CTRL-R =).
1892 * Returns the command line in allocated memory, or NULL.
1893 */
1894 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001895getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 int firstc;
1897 char_u *prompt; /* command line prompt */
1898 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001899 int xp_context; /* type of expansion */
1900 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
1902 char_u *s;
1903 struct cmdline_info save_ccline;
1904 int msg_col_save = msg_col;
1905
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001906 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 ccline.cmdprompt = prompt;
1908 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001909# ifdef FEAT_EVAL
1910 ccline.xp_context = xp_context;
1911 ccline.xp_arg = xp_arg;
1912 ccline.input_fn = (firstc == '@');
1913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001915 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 /* Restore msg_col, the prompt from input() may have changed it. */
1917 msg_col = msg_col_save;
1918
1919 return s;
1920}
1921#endif
1922
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001923/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001924 * Return TRUE when the text must not be changed and we can't switch to
1925 * another window or buffer. Used when editing the command line, evaluating
1926 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001927 */
1928 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001929text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001930{
1931#ifdef FEAT_CMDWIN
1932 if (cmdwin_type != 0)
1933 return TRUE;
1934#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001935 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001936}
1937
1938/*
1939 * Give an error message for a command that isn't allowed while the cmdline
1940 * window is open or editing the cmdline in another way.
1941 */
1942 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001943text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001944{
1945#ifdef FEAT_CMDWIN
1946 if (cmdwin_type != 0)
1947 EMSG(_(e_cmdwin));
1948 else
1949#endif
1950 EMSG(_(e_secure));
1951}
1952
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001953#if defined(FEAT_AUTOCMD) || defined(PROTO)
1954/*
1955 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
1956 * message.
1957 */
1958 int
1959curbuf_locked()
1960{
1961 if (curbuf_lock > 0)
1962 {
1963 EMSG(_("E788: Not allowed to edit another buffer now"));
1964 return TRUE;
1965 }
1966 return FALSE;
1967}
1968#endif
1969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 static int
1971cmdline_charsize(idx)
1972 int idx;
1973{
1974#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1975 if (cmdline_star > 0) /* showing '*', always 1 position */
1976 return 1;
1977#endif
1978 return ptr2cells(ccline.cmdbuff + idx);
1979}
1980
1981/*
1982 * Compute the offset of the cursor on the command line for the prompt and
1983 * indent.
1984 */
1985 static void
1986set_cmdspos()
1987{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001988 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 ccline.cmdspos = 1 + ccline.cmdindent;
1990 else
1991 ccline.cmdspos = 0 + ccline.cmdindent;
1992}
1993
1994/*
1995 * Compute the screen position for the cursor on the command line.
1996 */
1997 static void
1998set_cmdspos_cursor()
1999{
2000 int i, m, c;
2001
2002 set_cmdspos();
2003 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002006 if (m < 0) /* overflow, Columns or Rows at weird value */
2007 m = MAXCOL;
2008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 else
2010 m = MAXCOL;
2011 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2012 {
2013 c = cmdline_charsize(i);
2014#ifdef FEAT_MBYTE
2015 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2016 if (has_mbyte)
2017 correct_cmdspos(i, c);
2018#endif
2019 /* If the cmdline doesn't fit, put cursor on last visible char. */
2020 if ((ccline.cmdspos += c) >= m)
2021 {
2022 ccline.cmdpos = i - 1;
2023 ccline.cmdspos -= c;
2024 break;
2025 }
2026#ifdef FEAT_MBYTE
2027 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002028 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029#endif
2030 }
2031}
2032
2033#ifdef FEAT_MBYTE
2034/*
2035 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2036 * character that doesn't fit, so that a ">" must be displayed.
2037 */
2038 static void
2039correct_cmdspos(idx, cells)
2040 int idx;
2041 int cells;
2042{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002043 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2045 && ccline.cmdspos % Columns + cells > Columns)
2046 ccline.cmdspos++;
2047}
2048#endif
2049
2050/*
2051 * Get an Ex command line for the ":" command.
2052 */
2053/* ARGSUSED */
2054 char_u *
2055getexline(c, dummy, indent)
2056 int c; /* normally ':', NUL for ":append" */
2057 void *dummy; /* cookie not used */
2058 int indent; /* indent for inside conditionals */
2059{
2060 /* When executing a register, remove ':' that's in front of each line. */
2061 if (exec_from_reg && vpeekc() == ':')
2062 (void)vgetc();
2063 return getcmdline(c, 1L, indent);
2064}
2065
2066/*
2067 * Get an Ex command line for Ex mode.
2068 * In Ex mode we only use the OS supplied line editing features and no
2069 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002070 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 */
2072/* ARGSUSED */
2073 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002074getexmodeline(promptc, dummy, indent)
2075 int promptc; /* normally ':', NUL for ":append" and '?' for
2076 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 void *dummy; /* cookie not used */
2078 int indent; /* indent for inside conditionals */
2079{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002080 garray_T line_ga;
2081 char_u *pend;
2082 int startcol = 0;
2083 int c1;
2084 int escaped = FALSE; /* CTRL-V typed */
2085 int vcol = 0;
2086 char_u *p;
2087 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089 /* Switch cursor on now. This avoids that it happens after the "\n", which
2090 * confuses the system function that computes tabstops. */
2091 cursor_on();
2092
2093 /* always start in column 0; write a newline if necessary */
2094 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002095 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002097 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002099 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002100 if (p_prompt)
2101 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 while (indent-- > 0)
2103 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106
2107 ga_init2(&line_ga, 1, 30);
2108
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002109 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002111 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002112 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002113 while (indent >= 8)
2114 {
2115 ga_append(&line_ga, TAB);
2116 msg_puts((char_u *)" ");
2117 indent -= 8;
2118 }
2119 while (indent-- > 0)
2120 {
2121 ga_append(&line_ga, ' ');
2122 msg_putchar(' ');
2123 }
2124 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002125 ++no_mapping;
2126 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 /*
2129 * Get the line, one character at a time.
2130 */
2131 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002132 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
2134 if (ga_grow(&line_ga, 40) == FAIL)
2135 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002136 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002138 /* Get one character at a time. Don't use inchar(), it can't handle
2139 * special characters. */
2140 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002143 * Handle line editing.
2144 * Previously this was left to the system, putting the terminal in
2145 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002147 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002149 msg_putchar('\n');
2150 break;
2151 }
2152
2153 if (!escaped)
2154 {
2155 /* CR typed means "enter", which is NL */
2156 if (c1 == '\r')
2157 c1 = '\n';
2158
2159 if (c1 == BS || c1 == K_BS
2160 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002162 if (line_ga.ga_len > 0)
2163 {
2164 --line_ga.ga_len;
2165 goto redraw;
2166 }
2167 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
2169
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002170 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002172 msg_col = startcol;
2173 msg_clr_eos();
2174 line_ga.ga_len = 0;
2175 continue;
2176 }
2177
2178 if (c1 == Ctrl_T)
2179 {
2180 p = (char_u *)line_ga.ga_data;
2181 p[line_ga.ga_len] = NUL;
2182 indent = get_indent_str(p, 8);
2183 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2184add_indent:
2185 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002187 char_u *s = skipwhite(p);
2188
2189 ga_grow(&line_ga, 1);
2190 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2191 *s = ' ';
2192 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002194redraw:
2195 /* redraw the line */
2196 msg_col = startcol;
2197 windgoto(msg_row, msg_col);
2198 vcol = 0;
2199 for (p = (char_u *)line_ga.ga_data;
2200 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002202 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002204 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002206 msg_putchar(' ');
2207 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211 msg_outtrans_len(p, 1);
2212 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 }
2214 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 msg_clr_eos();
2216 continue;
2217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219 if (c1 == Ctrl_D)
2220 {
2221 /* Delete one shiftwidth. */
2222 p = (char_u *)line_ga.ga_data;
2223 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002225 if (prev_char == '^')
2226 ex_keep_indent = TRUE;
2227 indent = 0;
2228 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230 else
2231 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232 p[line_ga.ga_len] = NUL;
2233 indent = get_indent_str(p, 8);
2234 --indent;
2235 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002237 while (get_indent_str(p, 8) > indent)
2238 {
2239 char_u *s = skipwhite(p);
2240
2241 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2242 --line_ga.ga_len;
2243 }
2244 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002246
2247 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2248 {
2249 escaped = TRUE;
2250 continue;
2251 }
2252
2253 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2254 if (IS_SPECIAL(c1))
2255 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002257
2258 if (IS_SPECIAL(c1))
2259 c1 = '?';
2260 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2261 prev_char = c1;
2262 if (c1 == '\n')
2263 msg_putchar('\n');
2264 else if (c1 == TAB)
2265 {
2266 /* Don't use chartabsize(), 'ts' can be different */
2267 do
2268 {
2269 msg_putchar(' ');
2270 } while (++vcol % 8);
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002274 msg_outtrans_len(
2275 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2276 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002278 ++line_ga.ga_len;
2279 escaped = FALSE;
2280
2281 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002282 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002283
2284 /* we are done when a NL is entered, but not when it comes after a
2285 * backslash */
2286 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2287 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002290 --pend;
2291 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002292 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294 }
2295
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002296 --no_mapping;
2297 --allow_keys;
2298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 /* make following messages go to the next line */
2300 msg_didout = FALSE;
2301 msg_col = 0;
2302 if (msg_row < Rows - 1)
2303 ++msg_row;
2304 emsg_on_display = FALSE; /* don't want ui_delay() */
2305
2306 if (got_int)
2307 ga_clear(&line_ga);
2308
2309 return (char_u *)line_ga.ga_data;
2310}
2311
Bram Moolenaare344bea2005-09-01 20:46:49 +00002312# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2313 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314/*
2315 * Return TRUE if ccline.overstrike is on.
2316 */
2317 int
2318cmdline_overstrike()
2319{
2320 return ccline.overstrike;
2321}
2322
2323/*
2324 * Return TRUE if the cursor is at the end of the cmdline.
2325 */
2326 int
2327cmdline_at_end()
2328{
2329 return (ccline.cmdpos >= ccline.cmdlen);
2330}
2331#endif
2332
Bram Moolenaar9372a112005-12-06 19:59:18 +00002333#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334/*
2335 * Return the virtual column number at the current cursor position.
2336 * This is used by the IM code to obtain the start of the preedit string.
2337 */
2338 colnr_T
2339cmdline_getvcol_cursor()
2340{
2341 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2342 return MAXCOL;
2343
2344# ifdef FEAT_MBYTE
2345 if (has_mbyte)
2346 {
2347 colnr_T col;
2348 int i = 0;
2349
2350 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002351 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353 return col;
2354 }
2355 else
2356# endif
2357 return ccline.cmdpos;
2358}
2359#endif
2360
2361#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2362/*
2363 * If part of the command line is an IM preedit string, redraw it with
2364 * IM feedback attributes. The cursor position is restored after drawing.
2365 */
2366 static void
2367redrawcmd_preedit()
2368{
2369 if ((State & CMDLINE)
2370 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002371 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 && !p_imdisable
2373 && im_is_preediting())
2374 {
2375 int cmdpos = 0;
2376 int cmdspos;
2377 int old_row;
2378 int old_col;
2379 colnr_T col;
2380
2381 old_row = msg_row;
2382 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002383 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385# ifdef FEAT_MBYTE
2386 if (has_mbyte)
2387 {
2388 for (col = 0; col < preedit_start_col
2389 && cmdpos < ccline.cmdlen; ++col)
2390 {
2391 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002392 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394 }
2395 else
2396# endif
2397 {
2398 cmdspos += preedit_start_col;
2399 cmdpos += preedit_start_col;
2400 }
2401
2402 msg_row = cmdline_row + (cmdspos / (int)Columns);
2403 msg_col = cmdspos % (int)Columns;
2404 if (msg_row >= Rows)
2405 msg_row = Rows - 1;
2406
2407 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2408 {
2409 int char_len;
2410 int char_attr;
2411
2412 char_attr = im_get_feedback_attr(col);
2413 if (char_attr < 0)
2414 break; /* end of preedit string */
2415
2416# ifdef FEAT_MBYTE
2417 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002418 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 else
2420# endif
2421 char_len = 1;
2422
2423 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2424 cmdpos += char_len;
2425 }
2426
2427 msg_row = old_row;
2428 msg_col = old_col;
2429 }
2430}
2431#endif /* FEAT_XIM && FEAT_GUI_GTK */
2432
2433/*
2434 * Allocate a new command line buffer.
2435 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2436 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2437 */
2438 static void
2439alloc_cmdbuff(len)
2440 int len;
2441{
2442 /*
2443 * give some extra space to avoid having to allocate all the time
2444 */
2445 if (len < 80)
2446 len = 100;
2447 else
2448 len += 20;
2449
2450 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2451 ccline.cmdbufflen = len;
2452}
2453
2454/*
2455 * Re-allocate the command line to length len + something extra.
2456 * return FAIL for failure, OK otherwise
2457 */
2458 static int
2459realloc_cmdbuff(len)
2460 int len;
2461{
2462 char_u *p;
2463
2464 p = ccline.cmdbuff;
2465 alloc_cmdbuff(len); /* will get some more */
2466 if (ccline.cmdbuff == NULL) /* out of memory */
2467 {
2468 ccline.cmdbuff = p; /* keep the old one */
2469 return FAIL;
2470 }
2471 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2472 vim_free(p);
2473 return OK;
2474}
2475
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002476#if defined(FEAT_ARABIC) || defined(PROTO)
2477static char_u *arshape_buf = NULL;
2478
2479# if defined(EXITFREE) || defined(PROTO)
2480 void
2481free_cmdline_buf()
2482{
2483 vim_free(arshape_buf);
2484}
2485# endif
2486#endif
2487
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488/*
2489 * Draw part of the cmdline at the current cursor position. But draw stars
2490 * when cmdline_star is TRUE.
2491 */
2492 static void
2493draw_cmdline(start, len)
2494 int start;
2495 int len;
2496{
2497#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2498 int i;
2499
2500 if (cmdline_star > 0)
2501 for (i = 0; i < len; ++i)
2502 {
2503 msg_putchar('*');
2504# ifdef FEAT_MBYTE
2505 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002506 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507# endif
2508 }
2509 else
2510#endif
2511#ifdef FEAT_ARABIC
2512 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 static int buflen = 0;
2515 char_u *p;
2516 int j;
2517 int newlen = 0;
2518 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002519 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 int prev_c = 0;
2521 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002522 int u8c;
2523 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
2526 /*
2527 * Do arabic shaping into a temporary buffer. This is very
2528 * inefficient!
2529 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002530 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
2532 /* Re-allocate the buffer. We keep it around to avoid a lot of
2533 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002534 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002535 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002536 arshape_buf = alloc(buflen);
2537 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 return; /* out of memory */
2539 }
2540
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002541 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2542 {
2543 /* Prepend a space to draw the leading composing char on. */
2544 arshape_buf[0] = ' ';
2545 newlen = 1;
2546 }
2547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 for (j = start; j < start + len; j += mb_l)
2549 {
2550 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002551 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002552 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (ARABIC_CHAR(u8c))
2554 {
2555 /* Do Arabic shaping. */
2556 if (cmdmsg_rl)
2557 {
2558 /* displaying from right to left */
2559 pc = prev_c;
2560 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002561 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (j + mb_l >= start + len)
2563 nc = NUL;
2564 else
2565 nc = utf_ptr2char(p + mb_l);
2566 }
2567 else
2568 {
2569 /* displaying from left to right */
2570 if (j + mb_l >= start + len)
2571 pc = NUL;
2572 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002573 {
2574 int pcc[MAX_MCO];
2575
2576 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002578 pc1 = pcc[0];
2579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 nc = prev_c;
2581 }
2582 prev_c = u8c;
2583
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002584 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002586 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002587 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002589 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2590 if (u8cc[1] != 0)
2591 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002592 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594 }
2595 else
2596 {
2597 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002598 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 newlen += mb_l;
2600 }
2601 }
2602
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002603 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605 else
2606#endif
2607 msg_outtrans_len(ccline.cmdbuff + start, len);
2608}
2609
2610/*
2611 * Put a character on the command line. Shifts the following text to the
2612 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2613 * "c" must be printable (fit in one display cell)!
2614 */
2615 void
2616putcmdline(c, shift)
2617 int c;
2618 int shift;
2619{
2620 if (cmd_silent)
2621 return;
2622 msg_no_more = TRUE;
2623 msg_putchar(c);
2624 if (shift)
2625 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2626 msg_no_more = FALSE;
2627 cursorcmd();
2628}
2629
2630/*
2631 * Undo a putcmdline(c, FALSE).
2632 */
2633 void
2634unputcmdline()
2635{
2636 if (cmd_silent)
2637 return;
2638 msg_no_more = TRUE;
2639 if (ccline.cmdlen == ccline.cmdpos)
2640 msg_putchar(' ');
2641 else
2642 draw_cmdline(ccline.cmdpos, 1);
2643 msg_no_more = FALSE;
2644 cursorcmd();
2645}
2646
2647/*
2648 * Put the given string, of the given length, onto the command line.
2649 * If len is -1, then STRLEN() is used to calculate the length.
2650 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2651 * part will be redrawn, otherwise it will not. If this function is called
2652 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2653 * called afterwards.
2654 */
2655 int
2656put_on_cmdline(str, len, redraw)
2657 char_u *str;
2658 int len;
2659 int redraw;
2660{
2661 int retval;
2662 int i;
2663 int m;
2664 int c;
2665
2666 if (len < 0)
2667 len = (int)STRLEN(str);
2668
2669 /* Check if ccline.cmdbuff needs to be longer */
2670 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2671 retval = realloc_cmdbuff(ccline.cmdlen + len);
2672 else
2673 retval = OK;
2674 if (retval == OK)
2675 {
2676 if (!ccline.overstrike)
2677 {
2678 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2679 ccline.cmdbuff + ccline.cmdpos,
2680 (size_t)(ccline.cmdlen - ccline.cmdpos));
2681 ccline.cmdlen += len;
2682 }
2683 else
2684 {
2685#ifdef FEAT_MBYTE
2686 if (has_mbyte)
2687 {
2688 /* Count nr of characters in the new string. */
2689 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002690 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 ++m;
2692 /* Count nr of bytes in cmdline that are overwritten by these
2693 * characters. */
2694 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002695 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 --m;
2697 if (i < ccline.cmdlen)
2698 {
2699 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2700 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2701 ccline.cmdlen += ccline.cmdpos + len - i;
2702 }
2703 else
2704 ccline.cmdlen = ccline.cmdpos + len;
2705 }
2706 else
2707#endif
2708 if (ccline.cmdpos + len > ccline.cmdlen)
2709 ccline.cmdlen = ccline.cmdpos + len;
2710 }
2711 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2712 ccline.cmdbuff[ccline.cmdlen] = NUL;
2713
2714#ifdef FEAT_MBYTE
2715 if (enc_utf8)
2716 {
2717 /* When the inserted text starts with a composing character,
2718 * backup to the character before it. There could be two of them.
2719 */
2720 i = 0;
2721 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2722 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2723 {
2724 i = (*mb_head_off)(ccline.cmdbuff,
2725 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2726 ccline.cmdpos -= i;
2727 len += i;
2728 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2729 }
2730# ifdef FEAT_ARABIC
2731 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2732 {
2733 /* Check the previous character for Arabic combining pair. */
2734 i = (*mb_head_off)(ccline.cmdbuff,
2735 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2736 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2737 + ccline.cmdpos - i), c))
2738 {
2739 ccline.cmdpos -= i;
2740 len += i;
2741 }
2742 else
2743 i = 0;
2744 }
2745# endif
2746 if (i != 0)
2747 {
2748 /* Also backup the cursor position. */
2749 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2750 ccline.cmdspos -= i;
2751 msg_col -= i;
2752 if (msg_col < 0)
2753 {
2754 msg_col += Columns;
2755 --msg_row;
2756 }
2757 }
2758 }
2759#endif
2760
2761 if (redraw && !cmd_silent)
2762 {
2763 msg_no_more = TRUE;
2764 i = cmdline_row;
2765 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2766 /* Avoid clearing the rest of the line too often. */
2767 if (cmdline_row != i || ccline.overstrike)
2768 msg_clr_eos();
2769 msg_no_more = FALSE;
2770 }
2771#ifdef FEAT_FKMAP
2772 /*
2773 * If we are in Farsi command mode, the character input must be in
2774 * Insert mode. So do not advance the cmdpos.
2775 */
2776 if (!cmd_fkmap)
2777#endif
2778 {
2779 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002782 if (m < 0) /* overflow, Columns or Rows at weird value */
2783 m = MAXCOL;
2784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 else
2786 m = MAXCOL;
2787 for (i = 0; i < len; ++i)
2788 {
2789 c = cmdline_charsize(ccline.cmdpos);
2790#ifdef FEAT_MBYTE
2791 /* count ">" for a double-wide char that doesn't fit. */
2792 if (has_mbyte)
2793 correct_cmdspos(ccline.cmdpos, c);
2794#endif
2795 /* Stop cursor at the end of the screen */
2796 if (ccline.cmdspos + c >= m)
2797 break;
2798 ccline.cmdspos += c;
2799#ifdef FEAT_MBYTE
2800 if (has_mbyte)
2801 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002802 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (c > len - i - 1)
2804 c = len - i - 1;
2805 ccline.cmdpos += c;
2806 i += c;
2807 }
2808#endif
2809 ++ccline.cmdpos;
2810 }
2811 }
2812 }
2813 if (redraw)
2814 msg_check();
2815 return retval;
2816}
2817
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002818static struct cmdline_info prev_ccline;
2819static int prev_ccline_used = FALSE;
2820
2821/*
2822 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2823 * and overwrite it. But get_cmdline_str() may need it, thus make it
2824 * available globally in prev_ccline.
2825 */
2826 static void
2827save_cmdline(ccp)
2828 struct cmdline_info *ccp;
2829{
2830 if (!prev_ccline_used)
2831 {
2832 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2833 prev_ccline_used = TRUE;
2834 }
2835 *ccp = prev_ccline;
2836 prev_ccline = ccline;
2837 ccline.cmdbuff = NULL;
2838 ccline.cmdprompt = NULL;
2839}
2840
2841/*
2842 * Resture ccline after it has been saved with save_cmdline().
2843 */
2844 static void
2845restore_cmdline(ccp)
2846 struct cmdline_info *ccp;
2847{
2848 ccline = prev_ccline;
2849 prev_ccline = *ccp;
2850}
2851
Bram Moolenaar5a305422006-04-28 22:38:25 +00002852#if defined(FEAT_EVAL) || defined(PROTO)
2853/*
2854 * Save the command line into allocated memory. Returns a pointer to be
2855 * passed to restore_cmdline_alloc() later.
2856 * Returns NULL when failed.
2857 */
2858 char_u *
2859save_cmdline_alloc()
2860{
2861 struct cmdline_info *p;
2862
2863 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2864 if (p != NULL)
2865 save_cmdline(p);
2866 return (char_u *)p;
2867}
2868
2869/*
2870 * Restore the command line from the return value of save_cmdline_alloc().
2871 */
2872 void
2873restore_cmdline_alloc(p)
2874 char_u *p;
2875{
2876 if (p != NULL)
2877 {
2878 restore_cmdline((struct cmdline_info *)p);
2879 vim_free(p);
2880 }
2881}
2882#endif
2883
Bram Moolenaar8299df92004-07-10 09:47:34 +00002884/*
2885 * paste a yank register into the command line.
2886 * used by CTRL-R command in command-line mode
2887 * insert_reg() can't be used here, because special characters from the
2888 * register contents will be interpreted as commands.
2889 *
2890 * return FAIL for failure, OK otherwise
2891 */
2892 static int
2893cmdline_paste(regname, literally)
2894 int regname;
2895 int literally; /* Insert text literally instead of "as typed" */
2896{
2897 long i;
2898 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002899 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002900 int allocated;
2901 struct cmdline_info save_ccline;
2902
2903 /* check for valid regname; also accept special characters for CTRL-R in
2904 * the command line */
2905 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2906 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2907 return FAIL;
2908
2909 /* A register containing CTRL-R can cause an endless loop. Allow using
2910 * CTRL-C to break the loop. */
2911 line_breakcheck();
2912 if (got_int)
2913 return FAIL;
2914
2915#ifdef FEAT_CLIPBOARD
2916 regname = may_get_selection(regname);
2917#endif
2918
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002919 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002920 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002921 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002922 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002923 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002924 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002925 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002926
2927 if (i)
2928 {
2929 /* Got the value of a special register in "arg". */
2930 if (arg == NULL)
2931 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002932
2933 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2934 * part of the word. */
2935 p = arg;
2936 if (p_is && regname == Ctrl_W)
2937 {
2938 char_u *w;
2939 int len;
2940
2941 /* Locate start of last word in the cmd buffer. */
2942 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
2943 {
2944#ifdef FEAT_MBYTE
2945 if (has_mbyte)
2946 {
2947 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
2948 if (!vim_iswordc(mb_ptr2char(w - len)))
2949 break;
2950 w -= len;
2951 }
2952 else
2953#endif
2954 {
2955 if (!vim_iswordc(w[-1]))
2956 break;
2957 --w;
2958 }
2959 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002960 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002961 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
2962 p += len;
2963 }
2964
2965 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002966 if (allocated)
2967 vim_free(arg);
2968 return OK;
2969 }
2970
2971 return cmdline_paste_reg(regname, literally);
2972}
2973
2974/*
2975 * Put a string on the command line.
2976 * When "literally" is TRUE, insert literally.
2977 * When "literally" is FALSE, insert as typed, but don't leave the command
2978 * line.
2979 */
2980 void
2981cmdline_paste_str(s, literally)
2982 char_u *s;
2983 int literally;
2984{
2985 int c, cv;
2986
2987 if (literally)
2988 put_on_cmdline(s, -1, TRUE);
2989 else
2990 while (*s != NUL)
2991 {
2992 cv = *s;
2993 if (cv == Ctrl_V && s[1])
2994 ++s;
2995#ifdef FEAT_MBYTE
2996 if (has_mbyte)
2997 {
2998 c = mb_ptr2char(s);
2999 s += mb_char2len(c);
3000 }
3001 else
3002#endif
3003 c = *s++;
3004 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3005#ifdef UNIX
3006 || c == intr_char
3007#endif
3008 || (c == Ctrl_BSL && *s == Ctrl_N))
3009 stuffcharReadbuff(Ctrl_V);
3010 stuffcharReadbuff(c);
3011 }
3012}
3013
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014#ifdef FEAT_WILDMENU
3015/*
3016 * Delete characters on the command line, from "from" to the current
3017 * position.
3018 */
3019 static void
3020cmdline_del(from)
3021 int from;
3022{
3023 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3024 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3025 ccline.cmdlen -= ccline.cmdpos - from;
3026 ccline.cmdpos = from;
3027}
3028#endif
3029
3030/*
3031 * this fuction is called when the screen size changes and with incremental
3032 * search
3033 */
3034 void
3035redrawcmdline()
3036{
3037 if (cmd_silent)
3038 return;
3039 need_wait_return = FALSE;
3040 compute_cmdrow();
3041 redrawcmd();
3042 cursorcmd();
3043}
3044
3045 static void
3046redrawcmdprompt()
3047{
3048 int i;
3049
3050 if (cmd_silent)
3051 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003052 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 msg_putchar(ccline.cmdfirstc);
3054 if (ccline.cmdprompt != NULL)
3055 {
3056 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3057 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3058 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003059 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 --ccline.cmdindent;
3061 }
3062 else
3063 for (i = ccline.cmdindent; i > 0; --i)
3064 msg_putchar(' ');
3065}
3066
3067/*
3068 * Redraw what is currently on the command line.
3069 */
3070 void
3071redrawcmd()
3072{
3073 if (cmd_silent)
3074 return;
3075
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003076 /* when 'incsearch' is set there may be no command line while redrawing */
3077 if (ccline.cmdbuff == NULL)
3078 {
3079 windgoto(cmdline_row, 0);
3080 msg_clr_eos();
3081 return;
3082 }
3083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 msg_start();
3085 redrawcmdprompt();
3086
3087 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3088 msg_no_more = TRUE;
3089 draw_cmdline(0, ccline.cmdlen);
3090 msg_clr_eos();
3091 msg_no_more = FALSE;
3092
3093 set_cmdspos_cursor();
3094
3095 /*
3096 * An emsg() before may have set msg_scroll. This is used in normal mode,
3097 * in cmdline mode we can reset them now.
3098 */
3099 msg_scroll = FALSE; /* next message overwrites cmdline */
3100
3101 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3102 * in cmdline mode */
3103 skip_redraw = FALSE;
3104}
3105
3106 void
3107compute_cmdrow()
3108{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003109 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 cmdline_row = Rows - 1;
3111 else
3112 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3113 + W_STATUS_HEIGHT(lastwin);
3114}
3115
3116 static void
3117cursorcmd()
3118{
3119 if (cmd_silent)
3120 return;
3121
3122#ifdef FEAT_RIGHTLEFT
3123 if (cmdmsg_rl)
3124 {
3125 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3126 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3127 if (msg_row <= 0)
3128 msg_row = Rows - 1;
3129 }
3130 else
3131#endif
3132 {
3133 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3134 msg_col = ccline.cmdspos % (int)Columns;
3135 if (msg_row >= Rows)
3136 msg_row = Rows - 1;
3137 }
3138
3139 windgoto(msg_row, msg_col);
3140#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3141 redrawcmd_preedit();
3142#endif
3143#ifdef MCH_CURSOR_SHAPE
3144 mch_update_cursor();
3145#endif
3146}
3147
3148 void
3149gotocmdline(clr)
3150 int clr;
3151{
3152 msg_start();
3153#ifdef FEAT_RIGHTLEFT
3154 if (cmdmsg_rl)
3155 msg_col = Columns - 1;
3156 else
3157#endif
3158 msg_col = 0; /* always start in column 0 */
3159 if (clr) /* clear the bottom line(s) */
3160 msg_clr_eos(); /* will reset clear_cmdline */
3161 windgoto(cmdline_row, 0);
3162}
3163
3164/*
3165 * Check the word in front of the cursor for an abbreviation.
3166 * Called when the non-id character "c" has been entered.
3167 * When an abbreviation is recognized it is removed from the text with
3168 * backspaces and the replacement string is inserted, followed by "c".
3169 */
3170 static int
3171ccheck_abbr(c)
3172 int c;
3173{
3174 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3175 return FALSE;
3176
3177 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3178}
3179
3180/*
3181 * Return FAIL if this is not an appropriate context in which to do
3182 * completion of anything, return OK if it is (even if there are no matches).
3183 * For the caller, this means that the character is just passed through like a
3184 * normal character (instead of being expanded). This allows :s/^I^D etc.
3185 */
3186 static int
3187nextwild(xp, type, options)
3188 expand_T *xp;
3189 int type;
3190 int options; /* extra options for ExpandOne() */
3191{
3192 int i, j;
3193 char_u *p1;
3194 char_u *p2;
3195 int oldlen;
3196 int difflen;
3197 int v;
3198
3199 if (xp->xp_numfiles == -1)
3200 {
3201 set_expand_context(xp);
3202 cmd_showtail = expand_showtail(xp);
3203 }
3204
3205 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3206 {
3207 beep_flush();
3208 return OK; /* Something illegal on command line */
3209 }
3210 if (xp->xp_context == EXPAND_NOTHING)
3211 {
3212 /* Caller can use the character as a normal char instead */
3213 return FAIL;
3214 }
3215
3216 MSG_PUTS("..."); /* show that we are busy */
3217 out_flush();
3218
3219 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3220 oldlen = ccline.cmdpos - i;
3221
3222 if (type == WILD_NEXT || type == WILD_PREV)
3223 {
3224 /*
3225 * Get next/previous match for a previous expanded pattern.
3226 */
3227 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3228 }
3229 else
3230 {
3231 /*
3232 * Translate string into pattern and expand it.
3233 */
3234 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3235 p2 = NULL;
3236 else
3237 {
3238 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3239 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3240 |options, type);
3241 vim_free(p1);
3242 /* longest match: make sure it is not shorter (happens with :help */
3243 if (p2 != NULL && type == WILD_LONGEST)
3244 {
3245 for (j = 0; j < oldlen; ++j)
3246 if (ccline.cmdbuff[i + j] == '*'
3247 || ccline.cmdbuff[i + j] == '?')
3248 break;
3249 if ((int)STRLEN(p2) < j)
3250 {
3251 vim_free(p2);
3252 p2 = NULL;
3253 }
3254 }
3255 }
3256 }
3257
3258 if (p2 != NULL && !got_int)
3259 {
3260 difflen = (int)STRLEN(p2) - oldlen;
3261 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3262 {
3263 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3264 xp->xp_pattern = ccline.cmdbuff + i;
3265 }
3266 else
3267 v = OK;
3268 if (v == OK)
3269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003270 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3271 &ccline.cmdbuff[ccline.cmdpos],
3272 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3273 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 ccline.cmdlen += difflen;
3275 ccline.cmdpos += difflen;
3276 }
3277 }
3278 vim_free(p2);
3279
3280 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003281 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
3283 /* When expanding a ":map" command and no matches are found, assume that
3284 * the key is supposed to be inserted literally */
3285 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3286 return FAIL;
3287
3288 if (xp->xp_numfiles <= 0 && p2 == NULL)
3289 beep_flush();
3290 else if (xp->xp_numfiles == 1)
3291 /* free expanded pattern */
3292 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3293
3294 return OK;
3295}
3296
3297/*
3298 * Do wildcard expansion on the string 'str'.
3299 * Chars that should not be expanded must be preceded with a backslash.
3300 * Return a pointer to alloced memory containing the new string.
3301 * Return NULL for failure.
3302 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003303 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3304 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 *
3306 * mode = WILD_FREE: just free previously expanded matches
3307 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3308 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3309 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3310 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3311 * mode = WILD_ALL: return all matches concatenated
3312 * mode = WILD_LONGEST: return longest matched part
3313 *
3314 * options = WILD_LIST_NOTFOUND: list entries without a match
3315 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3316 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3317 * options = WILD_NO_BEEP: Don't beep for multiple matches
3318 * options = WILD_ADD_SLASH: add a slash after directory names
3319 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3320 * options = WILD_SILENT: don't print warning messages
3321 * options = WILD_ESCAPE: put backslash before special chars
3322 *
3323 * The variables xp->xp_context and xp->xp_backslash must have been set!
3324 */
3325 char_u *
3326ExpandOne(xp, str, orig, options, mode)
3327 expand_T *xp;
3328 char_u *str;
3329 char_u *orig; /* allocated copy of original of expanded string */
3330 int options;
3331 int mode;
3332{
3333 char_u *ss = NULL;
3334 static int findex;
3335 static char_u *orig_save = NULL; /* kept value of orig */
3336 int i;
3337 long_u len;
3338 int non_suf_match; /* number without matching suffix */
3339
3340 /*
3341 * first handle the case of using an old match
3342 */
3343 if (mode == WILD_NEXT || mode == WILD_PREV)
3344 {
3345 if (xp->xp_numfiles > 0)
3346 {
3347 if (mode == WILD_PREV)
3348 {
3349 if (findex == -1)
3350 findex = xp->xp_numfiles;
3351 --findex;
3352 }
3353 else /* mode == WILD_NEXT */
3354 ++findex;
3355
3356 /*
3357 * When wrapping around, return the original string, set findex to
3358 * -1.
3359 */
3360 if (findex < 0)
3361 {
3362 if (orig_save == NULL)
3363 findex = xp->xp_numfiles - 1;
3364 else
3365 findex = -1;
3366 }
3367 if (findex >= xp->xp_numfiles)
3368 {
3369 if (orig_save == NULL)
3370 findex = 0;
3371 else
3372 findex = -1;
3373 }
3374#ifdef FEAT_WILDMENU
3375 if (p_wmnu)
3376 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3377 findex, cmd_showtail);
3378#endif
3379 if (findex == -1)
3380 return vim_strsave(orig_save);
3381 return vim_strsave(xp->xp_files[findex]);
3382 }
3383 else
3384 return NULL;
3385 }
3386
3387/* free old names */
3388 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3389 {
3390 FreeWild(xp->xp_numfiles, xp->xp_files);
3391 xp->xp_numfiles = -1;
3392 vim_free(orig_save);
3393 orig_save = NULL;
3394 }
3395 findex = 0;
3396
3397 if (mode == WILD_FREE) /* only release file name */
3398 return NULL;
3399
3400 if (xp->xp_numfiles == -1)
3401 {
3402 vim_free(orig_save);
3403 orig_save = orig;
3404
3405 /*
3406 * Do the expansion.
3407 */
3408 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3409 options) == FAIL)
3410 {
3411#ifdef FNAME_ILLEGAL
3412 /* Illegal file name has been silently skipped. But when there
3413 * are wildcards, the real problem is that there was no match,
3414 * causing the pattern to be added, which has illegal characters.
3415 */
3416 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3417 EMSG2(_(e_nomatch2), str);
3418#endif
3419 }
3420 else if (xp->xp_numfiles == 0)
3421 {
3422 if (!(options & WILD_SILENT))
3423 EMSG2(_(e_nomatch2), str);
3424 }
3425 else
3426 {
3427 /* Escape the matches for use on the command line. */
3428 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3429
3430 /*
3431 * Check for matching suffixes in file names.
3432 */
3433 if (mode != WILD_ALL && mode != WILD_LONGEST)
3434 {
3435 if (xp->xp_numfiles)
3436 non_suf_match = xp->xp_numfiles;
3437 else
3438 non_suf_match = 1;
3439 if ((xp->xp_context == EXPAND_FILES
3440 || xp->xp_context == EXPAND_DIRECTORIES)
3441 && xp->xp_numfiles > 1)
3442 {
3443 /*
3444 * More than one match; check suffix.
3445 * The files will have been sorted on matching suffix in
3446 * expand_wildcards, only need to check the first two.
3447 */
3448 non_suf_match = 0;
3449 for (i = 0; i < 2; ++i)
3450 if (match_suffix(xp->xp_files[i]))
3451 ++non_suf_match;
3452 }
3453 if (non_suf_match != 1)
3454 {
3455 /* Can we ever get here unless it's while expanding
3456 * interactively? If not, we can get rid of this all
3457 * together. Don't really want to wait for this message
3458 * (and possibly have to hit return to continue!).
3459 */
3460 if (!(options & WILD_SILENT))
3461 EMSG(_(e_toomany));
3462 else if (!(options & WILD_NO_BEEP))
3463 beep_flush();
3464 }
3465 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3466 ss = vim_strsave(xp->xp_files[0]);
3467 }
3468 }
3469 }
3470
3471 /* Find longest common part */
3472 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3473 {
3474 for (len = 0; xp->xp_files[0][len]; ++len)
3475 {
3476 for (i = 0; i < xp->xp_numfiles; ++i)
3477 {
3478#ifdef CASE_INSENSITIVE_FILENAME
3479 if (xp->xp_context == EXPAND_DIRECTORIES
3480 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003481 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 || xp->xp_context == EXPAND_BUFFERS)
3483 {
3484 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3485 TOLOWER_LOC(xp->xp_files[0][len]))
3486 break;
3487 }
3488 else
3489#endif
3490 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3491 break;
3492 }
3493 if (i < xp->xp_numfiles)
3494 {
3495 if (!(options & WILD_NO_BEEP))
3496 vim_beep();
3497 break;
3498 }
3499 }
3500 ss = alloc((unsigned)len + 1);
3501 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003502 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 findex = -1; /* next p_wc gets first one */
3504 }
3505
3506 /* Concatenate all matching names */
3507 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3508 {
3509 len = 0;
3510 for (i = 0; i < xp->xp_numfiles; ++i)
3511 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3512 ss = lalloc(len, TRUE);
3513 if (ss != NULL)
3514 {
3515 *ss = NUL;
3516 for (i = 0; i < xp->xp_numfiles; ++i)
3517 {
3518 STRCAT(ss, xp->xp_files[i]);
3519 if (i != xp->xp_numfiles - 1)
3520 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3521 }
3522 }
3523 }
3524
3525 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3526 ExpandCleanup(xp);
3527
3528 return ss;
3529}
3530
3531/*
3532 * Prepare an expand structure for use.
3533 */
3534 void
3535ExpandInit(xp)
3536 expand_T *xp;
3537{
3538 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003539#ifndef BACKSLASH_IN_FILENAME
3540 xp->xp_shell = FALSE;
3541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 xp->xp_numfiles = -1;
3543 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003544#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3545 xp->xp_arg = NULL;
3546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547}
3548
3549/*
3550 * Cleanup an expand structure after use.
3551 */
3552 void
3553ExpandCleanup(xp)
3554 expand_T *xp;
3555{
3556 if (xp->xp_numfiles >= 0)
3557 {
3558 FreeWild(xp->xp_numfiles, xp->xp_files);
3559 xp->xp_numfiles = -1;
3560 }
3561}
3562
3563 void
3564ExpandEscape(xp, str, numfiles, files, options)
3565 expand_T *xp;
3566 char_u *str;
3567 int numfiles;
3568 char_u **files;
3569 int options;
3570{
3571 int i;
3572 char_u *p;
3573
3574 /*
3575 * May change home directory back to "~"
3576 */
3577 if (options & WILD_HOME_REPLACE)
3578 tilde_replace(str, numfiles, files);
3579
3580 if (options & WILD_ESCAPE)
3581 {
3582 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003583 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 || xp->xp_context == EXPAND_BUFFERS
3585 || xp->xp_context == EXPAND_DIRECTORIES)
3586 {
3587 /*
3588 * Insert a backslash into a file name before a space, \, %, #
3589 * and wildmatch characters, except '~'.
3590 */
3591 for (i = 0; i < numfiles; ++i)
3592 {
3593 /* for ":set path=" we need to escape spaces twice */
3594 if (xp->xp_backslash == XP_BS_THREE)
3595 {
3596 p = vim_strsave_escaped(files[i], (char_u *)" ");
3597 if (p != NULL)
3598 {
3599 vim_free(files[i]);
3600 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003601#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 p = vim_strsave_escaped(files[i], (char_u *)" ");
3603 if (p != NULL)
3604 {
3605 vim_free(files[i]);
3606 files[i] = p;
3607 }
3608#endif
3609 }
3610 }
3611#ifdef BACKSLASH_IN_FILENAME
3612 {
3613 char_u buf[20];
3614 int j = 0;
3615
3616 /* Don't escape '[' and '{' if they are in 'isfname'. */
3617 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3618 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3619 buf[j++] = *p;
3620 buf[j] = NUL;
3621 p = vim_strsave_escaped(files[i], buf);
3622 }
3623#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003624 p = vim_strsave_escaped(files[i],
3625 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626#endif
3627 if (p != NULL)
3628 {
3629 vim_free(files[i]);
3630 files[i] = p;
3631 }
3632
3633 /* If 'str' starts with "\~", replace "~" at start of
3634 * files[i] with "\~". */
3635 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003636 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003639
3640 /* If the first file starts with a '+' escape it. Otherwise it
3641 * could be seen as "+cmd". */
3642 if (*files[0] == '+')
3643 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 }
3645 else if (xp->xp_context == EXPAND_TAGS)
3646 {
3647 /*
3648 * Insert a backslash before characters in a tag name that
3649 * would terminate the ":tag" command.
3650 */
3651 for (i = 0; i < numfiles; ++i)
3652 {
3653 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3654 if (p != NULL)
3655 {
3656 vim_free(files[i]);
3657 files[i] = p;
3658 }
3659 }
3660 }
3661 }
3662}
3663
3664/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003665 * Put a backslash before the file name in "pp", which is in allocated memory.
3666 */
3667 static void
3668escape_fname(pp)
3669 char_u **pp;
3670{
3671 char_u *p;
3672
3673 p = alloc((unsigned)(STRLEN(*pp) + 2));
3674 if (p != NULL)
3675 {
3676 p[0] = '\\';
3677 STRCPY(p + 1, *pp);
3678 vim_free(*pp);
3679 *pp = p;
3680 }
3681}
3682
3683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 * For each file name in files[num_files]:
3685 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3686 */
3687 void
3688tilde_replace(orig_pat, num_files, files)
3689 char_u *orig_pat;
3690 int num_files;
3691 char_u **files;
3692{
3693 int i;
3694 char_u *p;
3695
3696 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3697 {
3698 for (i = 0; i < num_files; ++i)
3699 {
3700 p = home_replace_save(NULL, files[i]);
3701 if (p != NULL)
3702 {
3703 vim_free(files[i]);
3704 files[i] = p;
3705 }
3706 }
3707 }
3708}
3709
3710/*
3711 * Show all matches for completion on the command line.
3712 * Returns EXPAND_NOTHING when the character that triggered expansion should
3713 * be inserted like a normal character.
3714 */
3715/*ARGSUSED*/
3716 static int
3717showmatches(xp, wildmenu)
3718 expand_T *xp;
3719 int wildmenu;
3720{
3721#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3722 int num_files;
3723 char_u **files_found;
3724 int i, j, k;
3725 int maxlen;
3726 int lines;
3727 int columns;
3728 char_u *p;
3729 int lastlen;
3730 int attr;
3731 int showtail;
3732
3733 if (xp->xp_numfiles == -1)
3734 {
3735 set_expand_context(xp);
3736 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3737 &num_files, &files_found);
3738 showtail = expand_showtail(xp);
3739 if (i != EXPAND_OK)
3740 return i;
3741
3742 }
3743 else
3744 {
3745 num_files = xp->xp_numfiles;
3746 files_found = xp->xp_files;
3747 showtail = cmd_showtail;
3748 }
3749
3750#ifdef FEAT_WILDMENU
3751 if (!wildmenu)
3752 {
3753#endif
3754 msg_didany = FALSE; /* lines_left will be set */
3755 msg_start(); /* prepare for paging */
3756 msg_putchar('\n');
3757 out_flush();
3758 cmdline_row = msg_row;
3759 msg_didany = FALSE; /* lines_left will be set again */
3760 msg_start(); /* prepare for paging */
3761#ifdef FEAT_WILDMENU
3762 }
3763#endif
3764
3765 if (got_int)
3766 got_int = FALSE; /* only int. the completion, not the cmd line */
3767#ifdef FEAT_WILDMENU
3768 else if (wildmenu)
3769 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3770#endif
3771 else
3772 {
3773 /* find the length of the longest file name */
3774 maxlen = 0;
3775 for (i = 0; i < num_files; ++i)
3776 {
3777 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003778 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 || xp->xp_context == EXPAND_BUFFERS))
3780 {
3781 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3782 j = vim_strsize(NameBuff);
3783 }
3784 else
3785 j = vim_strsize(L_SHOWFILE(i));
3786 if (j > maxlen)
3787 maxlen = j;
3788 }
3789
3790 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3791 lines = num_files;
3792 else
3793 {
3794 /* compute the number of columns and lines for the listing */
3795 maxlen += 2; /* two spaces between file names */
3796 columns = ((int)Columns + 2) / maxlen;
3797 if (columns < 1)
3798 columns = 1;
3799 lines = (num_files + columns - 1) / columns;
3800 }
3801
3802 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3803
3804 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3805 {
3806 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3807 msg_clr_eos();
3808 msg_advance(maxlen - 3);
3809 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3810 }
3811
3812 /* list the files line by line */
3813 for (i = 0; i < lines; ++i)
3814 {
3815 lastlen = 999;
3816 for (k = i; k < num_files; k += lines)
3817 {
3818 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3819 {
3820 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3821 p = files_found[k] + STRLEN(files_found[k]) + 1;
3822 msg_advance(maxlen + 1);
3823 msg_puts(p);
3824 msg_advance(maxlen + 3);
3825 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3826 break;
3827 }
3828 for (j = maxlen - lastlen; --j >= 0; )
3829 msg_putchar(' ');
3830 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003831 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 || xp->xp_context == EXPAND_BUFFERS)
3833 {
3834 /* highlight directories */
3835 j = (mch_isdir(files_found[k]));
3836 if (showtail)
3837 p = L_SHOWFILE(k);
3838 else
3839 {
3840 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3841 TRUE);
3842 p = NameBuff;
3843 }
3844 }
3845 else
3846 {
3847 j = FALSE;
3848 p = L_SHOWFILE(k);
3849 }
3850 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3851 }
3852 if (msg_col > 0) /* when not wrapped around */
3853 {
3854 msg_clr_eos();
3855 msg_putchar('\n');
3856 }
3857 out_flush(); /* show one line at a time */
3858 if (got_int)
3859 {
3860 got_int = FALSE;
3861 break;
3862 }
3863 }
3864
3865 /*
3866 * we redraw the command below the lines that we have just listed
3867 * This is a bit tricky, but it saves a lot of screen updating.
3868 */
3869 cmdline_row = msg_row; /* will put it back later */
3870 }
3871
3872 if (xp->xp_numfiles == -1)
3873 FreeWild(num_files, files_found);
3874
3875 return EXPAND_OK;
3876}
3877
3878/*
3879 * Private gettail for showmatches() (and win_redr_status_matches()):
3880 * Find tail of file name path, but ignore trailing "/".
3881 */
3882 char_u *
3883sm_gettail(s)
3884 char_u *s;
3885{
3886 char_u *p;
3887 char_u *t = s;
3888 int had_sep = FALSE;
3889
3890 for (p = s; *p != NUL; )
3891 {
3892 if (vim_ispathsep(*p)
3893#ifdef BACKSLASH_IN_FILENAME
3894 && !rem_backslash(p)
3895#endif
3896 )
3897 had_sep = TRUE;
3898 else if (had_sep)
3899 {
3900 t = p;
3901 had_sep = FALSE;
3902 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003903 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 }
3905 return t;
3906}
3907
3908/*
3909 * Return TRUE if we only need to show the tail of completion matches.
3910 * When not completing file names or there is a wildcard in the path FALSE is
3911 * returned.
3912 */
3913 static int
3914expand_showtail(xp)
3915 expand_T *xp;
3916{
3917 char_u *s;
3918 char_u *end;
3919
3920 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003921 if (xp->xp_context != EXPAND_FILES
3922 && xp->xp_context != EXPAND_SHELLCMD
3923 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 return FALSE;
3925
3926 end = gettail(xp->xp_pattern);
3927 if (end == xp->xp_pattern) /* there is no path separator */
3928 return FALSE;
3929
3930 for (s = xp->xp_pattern; s < end; s++)
3931 {
3932 /* Skip escaped wildcards. Only when the backslash is not a path
3933 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3934 if (rem_backslash(s))
3935 ++s;
3936 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3937 return FALSE;
3938 }
3939 return TRUE;
3940}
3941
3942/*
3943 * Prepare a string for expansion.
3944 * When expanding file names: The string will be used with expand_wildcards().
3945 * Copy the file name into allocated memory and add a '*' at the end.
3946 * When expanding other names: The string will be used with regcomp(). Copy
3947 * the name into allocated memory and prepend "^".
3948 */
3949 char_u *
3950addstar(fname, len, context)
3951 char_u *fname;
3952 int len;
3953 int context; /* EXPAND_FILES etc. */
3954{
3955 char_u *retval;
3956 int i, j;
3957 int new_len;
3958 char_u *tail;
3959
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003960 if (context != EXPAND_FILES
3961 && context != EXPAND_SHELLCMD
3962 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
3964 /*
3965 * Matching will be done internally (on something other than files).
3966 * So we convert the file-matching-type wildcards into our kind for
3967 * use with vim_regcomp(). First work out how long it will be:
3968 */
3969
3970 /* For help tags the translation is done in find_help_tags().
3971 * For a tag pattern starting with "/" no translation is needed. */
3972 if (context == EXPAND_HELP
3973 || context == EXPAND_COLORS
3974 || context == EXPAND_COMPILER
3975 || (context == EXPAND_TAGS && fname[0] == '/'))
3976 retval = vim_strnsave(fname, len);
3977 else
3978 {
3979 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3980 for (i = 0; i < len; i++)
3981 {
3982 if (fname[i] == '*' || fname[i] == '~')
3983 new_len++; /* '*' needs to be replaced by ".*"
3984 '~' needs to be replaced by "\~" */
3985
3986 /* Buffer names are like file names. "." should be literal */
3987 if (context == EXPAND_BUFFERS && fname[i] == '.')
3988 new_len++; /* "." becomes "\." */
3989
3990 /* Custom expansion takes care of special things, match
3991 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003992 if ((context == EXPAND_USER_DEFINED
3993 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 new_len++; /* '\' becomes "\\" */
3995 }
3996 retval = alloc(new_len);
3997 if (retval != NULL)
3998 {
3999 retval[0] = '^';
4000 j = 1;
4001 for (i = 0; i < len; i++, j++)
4002 {
4003 /* Skip backslash. But why? At least keep it for custom
4004 * expansion. */
4005 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004006 && context != EXPAND_USER_LIST
4007 && fname[i] == '\\'
4008 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 break;
4010
4011 switch (fname[i])
4012 {
4013 case '*': retval[j++] = '.';
4014 break;
4015 case '~': retval[j++] = '\\';
4016 break;
4017 case '?': retval[j] = '.';
4018 continue;
4019 case '.': if (context == EXPAND_BUFFERS)
4020 retval[j++] = '\\';
4021 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004022 case '\\': if (context == EXPAND_USER_DEFINED
4023 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 retval[j++] = '\\';
4025 break;
4026 }
4027 retval[j] = fname[i];
4028 }
4029 retval[j] = NUL;
4030 }
4031 }
4032 }
4033 else
4034 {
4035 retval = alloc(len + 4);
4036 if (retval != NULL)
4037 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004038 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
4040 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004041 * Don't add a star to *, ~, ~user, $var or `cmd`.
4042 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 * ~ would be at the start of the file name, but not the tail.
4044 * $ could be anywhere in the tail.
4045 * ` could be anywhere in the file name.
4046 */
4047 tail = gettail(retval);
4048 if ((*retval != '~' || tail != retval)
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004049 && (len == 0 || retval[len - 1] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 && vim_strchr(tail, '$') == NULL
4051 && vim_strchr(retval, '`') == NULL)
4052 retval[len++] = '*';
4053 retval[len] = NUL;
4054 }
4055 }
4056 return retval;
4057}
4058
4059/*
4060 * Must parse the command line so far to work out what context we are in.
4061 * Completion can then be done based on that context.
4062 * This routine sets the variables:
4063 * xp->xp_pattern The start of the pattern to be expanded within
4064 * the command line (ends at the cursor).
4065 * xp->xp_context The type of thing to expand. Will be one of:
4066 *
4067 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4068 * the command line, like an unknown command. Caller
4069 * should beep.
4070 * EXPAND_NOTHING Unrecognised context for completion, use char like
4071 * a normal char, rather than for completion. eg
4072 * :s/^I/
4073 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4074 * it.
4075 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4076 * EXPAND_FILES After command with XFILE set, or after setting
4077 * with P_EXPAND set. eg :e ^I, :w>>^I
4078 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4079 * when we know only directories are of interest. eg
4080 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004081 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4083 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4084 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4085 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4086 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4087 * EXPAND_EVENTS Complete event names
4088 * EXPAND_SYNTAX Complete :syntax command arguments
4089 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4090 * EXPAND_AUGROUP Complete autocommand group names
4091 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4092 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4093 * eg :unmap a^I , :cunab x^I
4094 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4095 * eg :call sub^I
4096 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4097 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4098 * names in expressions, eg :while s^I
4099 * EXPAND_ENV_VARS Complete environment variable names
4100 */
4101 static void
4102set_expand_context(xp)
4103 expand_T *xp;
4104{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004105 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 if (ccline.cmdfirstc != ':'
4107#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004108 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004109 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110#endif
4111 )
4112 {
4113 xp->xp_context = EXPAND_NOTHING;
4114 return;
4115 }
4116 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4117}
4118
4119 void
4120set_cmd_context(xp, str, len, col)
4121 expand_T *xp;
4122 char_u *str; /* start of command line */
4123 int len; /* length of command line (excl. NUL) */
4124 int col; /* position of cursor */
4125{
4126 int old_char = NUL;
4127 char_u *nextcomm;
4128
4129 /*
4130 * Avoid a UMR warning from Purify, only save the character if it has been
4131 * written before.
4132 */
4133 if (col < len)
4134 old_char = str[col];
4135 str[col] = NUL;
4136 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004137
4138#ifdef FEAT_EVAL
4139 if (ccline.cmdfirstc == '=')
4140 /* pass CMD_SIZE because there is no real command */
4141 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004142 else if (ccline.input_fn)
4143 {
4144 xp->xp_context = ccline.xp_context;
4145 xp->xp_pattern = ccline.cmdbuff;
4146 xp->xp_arg = ccline.xp_arg;
4147 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004148 else
4149#endif
4150 while (nextcomm != NULL)
4151 nextcomm = set_one_cmd_context(xp, nextcomm);
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 str[col] = old_char;
4154}
4155
4156/*
4157 * Expand the command line "str" from context "xp".
4158 * "xp" must have been set by set_cmd_context().
4159 * xp->xp_pattern points into "str", to where the text that is to be expanded
4160 * starts.
4161 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4162 * cursor.
4163 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4164 * key that triggered expansion literally.
4165 * Returns EXPAND_OK otherwise.
4166 */
4167 int
4168expand_cmdline(xp, str, col, matchcount, matches)
4169 expand_T *xp;
4170 char_u *str; /* start of command line */
4171 int col; /* position of cursor */
4172 int *matchcount; /* return: nr of matches */
4173 char_u ***matches; /* return: array of pointers to matches */
4174{
4175 char_u *file_str = NULL;
4176
4177 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4178 {
4179 beep_flush();
4180 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4181 }
4182 if (xp->xp_context == EXPAND_NOTHING)
4183 {
4184 /* Caller can use the character as a normal char instead */
4185 return EXPAND_NOTHING;
4186 }
4187
4188 /* add star to file name, or convert to regexp if not exp. files. */
4189 file_str = addstar(xp->xp_pattern,
4190 (int)(str + col - xp->xp_pattern), xp->xp_context);
4191 if (file_str == NULL)
4192 return EXPAND_UNSUCCESSFUL;
4193
4194 /* find all files that match the description */
4195 if (ExpandFromContext(xp, file_str, matchcount, matches,
4196 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4197 {
4198 *matchcount = 0;
4199 *matches = NULL;
4200 }
4201 vim_free(file_str);
4202
4203 return EXPAND_OK;
4204}
4205
4206#ifdef FEAT_MULTI_LANG
4207/*
4208 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4209 */
4210static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4211
4212 static void
4213cleanup_help_tags(num_file, file)
4214 int num_file;
4215 char_u **file;
4216{
4217 int i, j;
4218 int len;
4219
4220 for (i = 0; i < num_file; ++i)
4221 {
4222 len = (int)STRLEN(file[i]) - 3;
4223 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4224 {
4225 /* Sorting on priority means the same item in another language may
4226 * be anywhere. Search all items for a match up to the "@en". */
4227 for (j = 0; j < num_file; ++j)
4228 if (j != i
4229 && (int)STRLEN(file[j]) == len + 3
4230 && STRNCMP(file[i], file[j], len + 1) == 0)
4231 break;
4232 if (j == num_file)
4233 file[i][len] = NUL;
4234 }
4235 }
4236}
4237#endif
4238
4239/*
4240 * Do the expansion based on xp->xp_context and "pat".
4241 */
4242 static int
4243ExpandFromContext(xp, pat, num_file, file, options)
4244 expand_T *xp;
4245 char_u *pat;
4246 int *num_file;
4247 char_u ***file;
4248 int options;
4249{
4250#ifdef FEAT_CMDL_COMPL
4251 regmatch_T regmatch;
4252#endif
4253 int ret;
4254 int flags;
4255
4256 flags = EW_DIR; /* include directories */
4257 if (options & WILD_LIST_NOTFOUND)
4258 flags |= EW_NOTFOUND;
4259 if (options & WILD_ADD_SLASH)
4260 flags |= EW_ADDSLASH;
4261 if (options & WILD_KEEP_ALL)
4262 flags |= EW_KEEPALL;
4263 if (options & WILD_SILENT)
4264 flags |= EW_SILENT;
4265
4266 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4267 {
4268 /*
4269 * Expand file or directory names.
4270 */
4271 int free_pat = FALSE;
4272 int i;
4273
4274 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4275 * space */
4276 if (xp->xp_backslash != XP_BS_NONE)
4277 {
4278 free_pat = TRUE;
4279 pat = vim_strsave(pat);
4280 for (i = 0; pat[i]; ++i)
4281 if (pat[i] == '\\')
4282 {
4283 if (xp->xp_backslash == XP_BS_THREE
4284 && pat[i + 1] == '\\'
4285 && pat[i + 2] == '\\'
4286 && pat[i + 3] == ' ')
4287 STRCPY(pat + i, pat + i + 3);
4288 if (xp->xp_backslash == XP_BS_ONE
4289 && pat[i + 1] == ' ')
4290 STRCPY(pat + i, pat + i + 1);
4291 }
4292 }
4293
4294 if (xp->xp_context == EXPAND_FILES)
4295 flags |= EW_FILE;
4296 else
4297 flags = (flags | EW_DIR) & ~EW_FILE;
4298 ret = expand_wildcards(1, &pat, num_file, file, flags);
4299 if (free_pat)
4300 vim_free(pat);
4301 return ret;
4302 }
4303
4304 *file = (char_u **)"";
4305 *num_file = 0;
4306 if (xp->xp_context == EXPAND_HELP)
4307 {
4308 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4309 {
4310#ifdef FEAT_MULTI_LANG
4311 cleanup_help_tags(*num_file, *file);
4312#endif
4313 return OK;
4314 }
4315 return FAIL;
4316 }
4317
4318#ifndef FEAT_CMDL_COMPL
4319 return FAIL;
4320#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004321 if (xp->xp_context == EXPAND_SHELLCMD)
4322 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 if (xp->xp_context == EXPAND_OLD_SETTING)
4324 return ExpandOldSetting(num_file, file);
4325 if (xp->xp_context == EXPAND_BUFFERS)
4326 return ExpandBufnames(pat, num_file, file, options);
4327 if (xp->xp_context == EXPAND_TAGS
4328 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4329 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4330 if (xp->xp_context == EXPAND_COLORS)
4331 return ExpandRTDir(pat, num_file, file, "colors");
4332 if (xp->xp_context == EXPAND_COMPILER)
4333 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004334# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4335 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004336 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338
4339 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4340 if (regmatch.regprog == NULL)
4341 return FAIL;
4342
4343 /* set ignore-case according to p_ic, p_scs and pat */
4344 regmatch.rm_ic = ignorecase(pat);
4345
4346 if (xp->xp_context == EXPAND_SETTINGS
4347 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4348 ret = ExpandSettings(xp, &regmatch, num_file, file);
4349 else if (xp->xp_context == EXPAND_MAPPINGS)
4350 ret = ExpandMappings(&regmatch, num_file, file);
4351# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4352 else if (xp->xp_context == EXPAND_USER_DEFINED)
4353 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4354# endif
4355 else
4356 {
4357 static struct expgen
4358 {
4359 int context;
4360 char_u *((*func)__ARGS((expand_T *, int)));
4361 int ic;
4362 } tab[] =
4363 {
4364 {EXPAND_COMMANDS, get_command_name, FALSE},
4365#ifdef FEAT_USR_CMDS
4366 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4367 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4368 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4369 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4370#endif
4371#ifdef FEAT_EVAL
4372 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4373 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4374 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4375 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4376#endif
4377#ifdef FEAT_MENU
4378 {EXPAND_MENUS, get_menu_name, FALSE},
4379 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4380#endif
4381#ifdef FEAT_SYN_HL
4382 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4383#endif
4384 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4385#ifdef FEAT_AUTOCMD
4386 {EXPAND_EVENTS, get_event_name, TRUE},
4387 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4388#endif
4389#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4390 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4391 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4392#endif
4393 {EXPAND_ENV_VARS, get_env_name, TRUE},
4394 };
4395 int i;
4396
4397 /*
4398 * Find a context in the table and call the ExpandGeneric() with the
4399 * right function to do the expansion.
4400 */
4401 ret = FAIL;
4402 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4403 if (xp->xp_context == tab[i].context)
4404 {
4405 if (tab[i].ic)
4406 regmatch.rm_ic = TRUE;
4407 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4408 break;
4409 }
4410 }
4411
4412 vim_free(regmatch.regprog);
4413
4414 return ret;
4415#endif /* FEAT_CMDL_COMPL */
4416}
4417
4418#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4419/*
4420 * Expand a list of names.
4421 *
4422 * Generic function for command line completion. It calls a function to
4423 * obtain strings, one by one. The strings are matched against a regexp
4424 * program. Matching strings are copied into an array, which is returned.
4425 *
4426 * Returns OK when no problems encountered, FAIL for error (out of memory).
4427 */
4428 int
4429ExpandGeneric(xp, regmatch, num_file, file, func)
4430 expand_T *xp;
4431 regmatch_T *regmatch;
4432 int *num_file;
4433 char_u ***file;
4434 char_u *((*func)__ARGS((expand_T *, int)));
4435 /* returns a string from the list */
4436{
4437 int i;
4438 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004439 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 char_u *str;
4441
4442 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004443 * round == 0: count the number of matching names
4444 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004446 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
4448 for (i = 0; ; ++i)
4449 {
4450 str = (*func)(xp, i);
4451 if (str == NULL) /* end of list */
4452 break;
4453 if (*str == NUL) /* skip empty strings */
4454 continue;
4455
4456 if (vim_regexec(regmatch, str, (colnr_T)0))
4457 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004458 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
4460 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4461 (*file)[count] = str;
4462#ifdef FEAT_MENU
4463 if (func == get_menu_names && str != NULL)
4464 {
4465 /* test for separator added by get_menu_names() */
4466 str += STRLEN(str) - 1;
4467 if (*str == '\001')
4468 *str = '.';
4469 }
4470#endif
4471 }
4472 ++count;
4473 }
4474 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004475 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
4477 if (count == 0)
4478 return OK;
4479 *num_file = count;
4480 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4481 if (*file == NULL)
4482 {
4483 *file = (char_u **)"";
4484 return FAIL;
4485 }
4486 count = 0;
4487 }
4488 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004489
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004490 /* Sort the results. Keep menu's in the specified order. */
4491 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4492 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004493
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 return OK;
4495}
4496
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004497/*
4498 * Complete a shell command.
4499 * Returns FAIL or OK;
4500 */
4501 static int
4502expand_shellcmd(filepat, num_file, file, flagsarg)
4503 char_u *filepat; /* pattern to match with command names */
4504 int *num_file; /* return: number of matches */
4505 char_u ***file; /* return: array with matches */
4506 int flagsarg; /* EW_ flags */
4507{
4508 char_u *pat;
4509 int i;
4510 char_u *path;
4511 int mustfree = FALSE;
4512 garray_T ga;
4513 char_u *buf = alloc(MAXPATHL);
4514 size_t l;
4515 char_u *s, *e;
4516 int flags = flagsarg;
4517 int ret;
4518
4519 if (buf == NULL)
4520 return FAIL;
4521
4522 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4523 * space */
4524 pat = vim_strsave(filepat);
4525 for (i = 0; pat[i]; ++i)
4526 if (pat[i] == '\\' && pat[i + 1] == ' ')
4527 STRCPY(pat + i, pat + i + 1);
4528
4529 flags |= EW_FILE | EW_EXEC;
4530
4531 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004532 if (mch_isFullName(pat))
4533 path = (char_u *)" ";
4534 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004535 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4536 path = (char_u *)".";
4537 else
4538 path = vim_getenv((char_u *)"PATH", &mustfree);
4539
4540 /*
4541 * Go over all directories in $PATH. Expand matches in that directory and
4542 * collect them in "ga".
4543 */
4544 ga_init2(&ga, (int)sizeof(char *), 10);
4545 for (s = path; *s != NUL; s = e)
4546 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004547 if (*s == ' ')
4548 ++s; /* Skip space used for absolute path name. */
4549
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004550#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4551 e = vim_strchr(s, ';');
4552#else
4553 e = vim_strchr(s, ':');
4554#endif
4555 if (e == NULL)
4556 e = s + STRLEN(s);
4557
4558 l = e - s;
4559 if (l > MAXPATHL - 5)
4560 break;
4561 vim_strncpy(buf, s, l);
4562 add_pathsep(buf);
4563 l = STRLEN(buf);
4564 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4565
4566 /* Expand matches in one directory of $PATH. */
4567 ret = expand_wildcards(1, &buf, num_file, file, flags);
4568 if (ret == OK)
4569 {
4570 if (ga_grow(&ga, *num_file) == FAIL)
4571 FreeWild(*num_file, *file);
4572 else
4573 {
4574 for (i = 0; i < *num_file; ++i)
4575 {
4576 s = (*file)[i];
4577 if (STRLEN(s) > l)
4578 {
4579 /* Remove the path again. */
4580 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4581 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4582 }
4583 else
4584 vim_free(s);
4585 }
4586 vim_free(*file);
4587 }
4588 }
4589 if (*e != NUL)
4590 ++e;
4591 }
4592 *file = ga.ga_data;
4593 *num_file = ga.ga_len;
4594
4595 vim_free(buf);
4596 vim_free(pat);
4597 if (mustfree)
4598 vim_free(path);
4599 return OK;
4600}
4601
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004604static 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));
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004607 * call "user_expand_func()" to invoke a user defined VimL function and return
4608 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004610 static void *
4611call_user_expand_func(user_expand_func, xp, num_file, file)
4612 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 int *num_file;
4615 char_u ***file;
4616{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004617 char_u keep;
4618 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004621 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004622 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
4624 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004625 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 *num_file = 0;
4627 *file = NULL;
4628
4629 keep = ccline.cmdbuff[ccline.cmdlen];
4630 ccline.cmdbuff[ccline.cmdlen] = 0;
4631 sprintf((char *)num, "%d", ccline.cmdpos);
4632 args[0] = xp->xp_pattern;
4633 args[1] = ccline.cmdbuff;
4634 args[2] = num;
4635
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004636 /* Save the cmdline, we don't know what the function may do. */
4637 save_ccline = ccline;
4638 ccline.cmdbuff = NULL;
4639 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004641
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004642 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004643
4644 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004646
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004648
4649 return ret;
4650}
4651
4652/*
4653 * Expand names with a function defined by the user.
4654 */
4655 static int
4656ExpandUserDefined(xp, regmatch, num_file, file)
4657 expand_T *xp;
4658 regmatch_T *regmatch;
4659 int *num_file;
4660 char_u ***file;
4661{
4662 char_u *retstr;
4663 char_u *s;
4664 char_u *e;
4665 char_u keep;
4666 garray_T ga;
4667
4668 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4669 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 return FAIL;
4671
4672 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004673 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 {
4675 e = vim_strchr(s, '\n');
4676 if (e == NULL)
4677 e = s + STRLEN(s);
4678 keep = *e;
4679 *e = 0;
4680
4681 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4682 {
4683 *e = keep;
4684 if (*e != NUL)
4685 ++e;
4686 continue;
4687 }
4688
4689 if (ga_grow(&ga, 1) == FAIL)
4690 break;
4691
4692 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4693 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694
4695 *e = keep;
4696 if (*e != NUL)
4697 ++e;
4698 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004699 vim_free(retstr);
4700 *file = ga.ga_data;
4701 *num_file = ga.ga_len;
4702 return OK;
4703}
4704
4705/*
4706 * Expand names with a list returned by a function defined by the user.
4707 */
4708 static int
4709ExpandUserList(xp, num_file, file)
4710 expand_T *xp;
4711 int *num_file;
4712 char_u ***file;
4713{
4714 list_T *retlist;
4715 listitem_T *li;
4716 garray_T ga;
4717
4718 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4719 if (retlist == NULL)
4720 return FAIL;
4721
4722 ga_init2(&ga, (int)sizeof(char *), 3);
4723 /* Loop over the items in the list. */
4724 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4725 {
4726 if (li->li_tv.v_type != VAR_STRING)
4727 continue; /* Skip non-string items */
4728
4729 if (ga_grow(&ga, 1) == FAIL)
4730 break;
4731
4732 ((char_u **)ga.ga_data)[ga.ga_len] =
4733 vim_strsave(li->li_tv.vval.v_string);
4734 ++ga.ga_len;
4735 }
4736 list_unref(retlist);
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 *file = ga.ga_data;
4739 *num_file = ga.ga_len;
4740 return OK;
4741}
4742#endif
4743
4744/*
4745 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4746 * or compiler names.
4747 */
4748 static int
4749ExpandRTDir(pat, num_file, file, dirname)
4750 char_u *pat;
4751 int *num_file;
4752 char_u ***file;
4753 char *dirname; /* "colors" or "compiler" */
4754{
4755 char_u *all;
4756 char_u *s;
4757 char_u *e;
4758 garray_T ga;
4759
4760 *num_file = 0;
4761 *file = NULL;
4762 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4763 if (s == NULL)
4764 return FAIL;
4765 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4766 all = globpath(p_rtp, s);
4767 vim_free(s);
4768 if (all == NULL)
4769 return FAIL;
4770
4771 ga_init2(&ga, (int)sizeof(char *), 3);
4772 for (s = all; *s != NUL; s = e)
4773 {
4774 e = vim_strchr(s, '\n');
4775 if (e == NULL)
4776 e = s + STRLEN(s);
4777 if (ga_grow(&ga, 1) == FAIL)
4778 break;
4779 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4780 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004781 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (*s == '\n' || vim_ispathsep(*s))
4783 break;
4784 ++s;
4785 ((char_u **)ga.ga_data)[ga.ga_len] =
4786 vim_strnsave(s, (int)(e - s - 4));
4787 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 if (*e != NUL)
4790 ++e;
4791 }
4792 vim_free(all);
4793 *file = ga.ga_data;
4794 *num_file = ga.ga_len;
4795 return OK;
4796}
4797
4798#endif
4799
4800#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4801/*
4802 * Expand "file" for all comma-separated directories in "path".
4803 * Returns an allocated string with all matches concatenated, separated by
4804 * newlines. Returns NULL for an error or no matches.
4805 */
4806 char_u *
4807globpath(path, file)
4808 char_u *path;
4809 char_u *file;
4810{
4811 expand_T xpc;
4812 char_u *buf;
4813 garray_T ga;
4814 int i;
4815 int len;
4816 int num_p;
4817 char_u **p;
4818 char_u *cur = NULL;
4819
4820 buf = alloc(MAXPATHL);
4821 if (buf == NULL)
4822 return NULL;
4823
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004824 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004826
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 ga_init2(&ga, 1, 100);
4828
4829 /* Loop over all entries in {path}. */
4830 while (*path != NUL)
4831 {
4832 /* Copy one item of the path to buf[] and concatenate the file name. */
4833 copy_option_part(&path, buf, MAXPATHL, ",");
4834 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4835 {
4836 add_pathsep(buf);
4837 STRCAT(buf, file);
4838 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4839 && num_p > 0)
4840 {
4841 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4842 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004843 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
4845 /* Concatenate new results to previous ones. */
4846 if (ga_grow(&ga, len) == OK)
4847 {
4848 cur = (char_u *)ga.ga_data + ga.ga_len;
4849 for (i = 0; i < num_p; ++i)
4850 {
4851 STRCPY(cur, p[i]);
4852 cur += STRLEN(p[i]);
4853 *cur++ = '\n';
4854 }
4855 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857 FreeWild(num_p, p);
4858 }
4859 }
4860 }
4861 if (cur != NULL)
4862 *--cur = 0; /* Replace trailing newline with NUL */
4863
4864 vim_free(buf);
4865 return (char_u *)ga.ga_data;
4866}
4867
4868#endif
4869
4870#if defined(FEAT_CMDHIST) || defined(PROTO)
4871
4872/*********************************
4873 * Command line history stuff *
4874 *********************************/
4875
4876/*
4877 * Translate a history character to the associated type number.
4878 */
4879 static int
4880hist_char2type(c)
4881 int c;
4882{
4883 if (c == ':')
4884 return HIST_CMD;
4885 if (c == '=')
4886 return HIST_EXPR;
4887 if (c == '@')
4888 return HIST_INPUT;
4889 if (c == '>')
4890 return HIST_DEBUG;
4891 return HIST_SEARCH; /* must be '?' or '/' */
4892}
4893
4894/*
4895 * Table of history names.
4896 * These names are used in :history and various hist...() functions.
4897 * It is sufficient to give the significant prefix of a history name.
4898 */
4899
4900static char *(history_names[]) =
4901{
4902 "cmd",
4903 "search",
4904 "expr",
4905 "input",
4906 "debug",
4907 NULL
4908};
4909
4910/*
4911 * init_history() - Initialize the command line history.
4912 * Also used to re-allocate the history when the size changes.
4913 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004914 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915init_history()
4916{
4917 int newlen; /* new length of history table */
4918 histentry_T *temp;
4919 int i;
4920 int j;
4921 int type;
4922
4923 /*
4924 * If size of history table changed, reallocate it
4925 */
4926 newlen = (int)p_hi;
4927 if (newlen != hislen) /* history length changed */
4928 {
4929 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4930 {
4931 if (newlen)
4932 {
4933 temp = (histentry_T *)lalloc(
4934 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4935 if (temp == NULL) /* out of memory! */
4936 {
4937 if (type == 0) /* first one: just keep the old length */
4938 {
4939 newlen = hislen;
4940 break;
4941 }
4942 /* Already changed one table, now we can only have zero
4943 * length for all tables. */
4944 newlen = 0;
4945 type = -1;
4946 continue;
4947 }
4948 }
4949 else
4950 temp = NULL;
4951 if (newlen == 0 || temp != NULL)
4952 {
4953 if (hisidx[type] < 0) /* there are no entries yet */
4954 {
4955 for (i = 0; i < newlen; ++i)
4956 {
4957 temp[i].hisnum = 0;
4958 temp[i].hisstr = NULL;
4959 }
4960 }
4961 else if (newlen > hislen) /* array becomes bigger */
4962 {
4963 for (i = 0; i <= hisidx[type]; ++i)
4964 temp[i] = history[type][i];
4965 j = i;
4966 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4967 {
4968 temp[i].hisnum = 0;
4969 temp[i].hisstr = NULL;
4970 }
4971 for ( ; j < hislen; ++i, ++j)
4972 temp[i] = history[type][j];
4973 }
4974 else /* array becomes smaller or 0 */
4975 {
4976 j = hisidx[type];
4977 for (i = newlen - 1; ; --i)
4978 {
4979 if (i >= 0) /* copy newest entries */
4980 temp[i] = history[type][j];
4981 else /* remove older entries */
4982 vim_free(history[type][j].hisstr);
4983 if (--j < 0)
4984 j = hislen - 1;
4985 if (j == hisidx[type])
4986 break;
4987 }
4988 hisidx[type] = newlen - 1;
4989 }
4990 vim_free(history[type]);
4991 history[type] = temp;
4992 }
4993 }
4994 hislen = newlen;
4995 }
4996}
4997
4998/*
4999 * Check if command line 'str' is already in history.
5000 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5001 */
5002 static int
5003in_history(type, str, move_to_front)
5004 int type;
5005 char_u *str;
5006 int move_to_front; /* Move the entry to the front if it exists */
5007{
5008 int i;
5009 int last_i = -1;
5010
5011 if (hisidx[type] < 0)
5012 return FALSE;
5013 i = hisidx[type];
5014 do
5015 {
5016 if (history[type][i].hisstr == NULL)
5017 return FALSE;
5018 if (STRCMP(str, history[type][i].hisstr) == 0)
5019 {
5020 if (!move_to_front)
5021 return TRUE;
5022 last_i = i;
5023 break;
5024 }
5025 if (--i < 0)
5026 i = hislen - 1;
5027 } while (i != hisidx[type]);
5028
5029 if (last_i >= 0)
5030 {
5031 str = history[type][i].hisstr;
5032 while (i != hisidx[type])
5033 {
5034 if (++i >= hislen)
5035 i = 0;
5036 history[type][last_i] = history[type][i];
5037 last_i = i;
5038 }
5039 history[type][i].hisstr = str;
5040 history[type][i].hisnum = ++hisnum[type];
5041 return TRUE;
5042 }
5043 return FALSE;
5044}
5045
5046/*
5047 * Convert history name (from table above) to its HIST_ equivalent.
5048 * When "name" is empty, return "cmd" history.
5049 * Returns -1 for unknown history name.
5050 */
5051 int
5052get_histtype(name)
5053 char_u *name;
5054{
5055 int i;
5056 int len = (int)STRLEN(name);
5057
5058 /* No argument: use current history. */
5059 if (len == 0)
5060 return hist_char2type(ccline.cmdfirstc);
5061
5062 for (i = 0; history_names[i] != NULL; ++i)
5063 if (STRNICMP(name, history_names[i], len) == 0)
5064 return i;
5065
5066 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5067 return hist_char2type(name[0]);
5068
5069 return -1;
5070}
5071
5072static int last_maptick = -1; /* last seen maptick */
5073
5074/*
5075 * Add the given string to the given history. If the string is already in the
5076 * history then it is moved to the front. "histype" may be one of he HIST_
5077 * values.
5078 */
5079 void
5080add_to_history(histype, new_entry, in_map, sep)
5081 int histype;
5082 char_u *new_entry;
5083 int in_map; /* consider maptick when inside a mapping */
5084 int sep; /* separator character used (search hist) */
5085{
5086 histentry_T *hisptr;
5087 int len;
5088
5089 if (hislen == 0) /* no history */
5090 return;
5091
5092 /*
5093 * Searches inside the same mapping overwrite each other, so that only
5094 * the last line is kept. Be careful not to remove a line that was moved
5095 * down, only lines that were added.
5096 */
5097 if (histype == HIST_SEARCH && in_map)
5098 {
5099 if (maptick == last_maptick)
5100 {
5101 /* Current line is from the same mapping, remove it */
5102 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5103 vim_free(hisptr->hisstr);
5104 hisptr->hisstr = NULL;
5105 hisptr->hisnum = 0;
5106 --hisnum[histype];
5107 if (--hisidx[HIST_SEARCH] < 0)
5108 hisidx[HIST_SEARCH] = hislen - 1;
5109 }
5110 last_maptick = -1;
5111 }
5112 if (!in_history(histype, new_entry, TRUE))
5113 {
5114 if (++hisidx[histype] == hislen)
5115 hisidx[histype] = 0;
5116 hisptr = &history[histype][hisidx[histype]];
5117 vim_free(hisptr->hisstr);
5118
5119 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005120 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5122 if (hisptr->hisstr != NULL)
5123 hisptr->hisstr[len + 1] = sep;
5124
5125 hisptr->hisnum = ++hisnum[histype];
5126 if (histype == HIST_SEARCH && in_map)
5127 last_maptick = maptick;
5128 }
5129}
5130
5131#if defined(FEAT_EVAL) || defined(PROTO)
5132
5133/*
5134 * Get identifier of newest history entry.
5135 * "histype" may be one of the HIST_ values.
5136 */
5137 int
5138get_history_idx(histype)
5139 int histype;
5140{
5141 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5142 || hisidx[histype] < 0)
5143 return -1;
5144
5145 return history[histype][hisidx[histype]].hisnum;
5146}
5147
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005148static struct cmdline_info *get_ccline_ptr __ARGS((void));
5149
5150/*
5151 * Get pointer to the command line info to use. cmdline_paste() may clear
5152 * ccline and put the previous value in prev_ccline.
5153 */
5154 static struct cmdline_info *
5155get_ccline_ptr()
5156{
5157 if ((State & CMDLINE) == 0)
5158 return NULL;
5159 if (ccline.cmdbuff != NULL)
5160 return &ccline;
5161 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5162 return &prev_ccline;
5163 return NULL;
5164}
5165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166/*
5167 * Get the current command line in allocated memory.
5168 * Only works when the command line is being edited.
5169 * Returns NULL when something is wrong.
5170 */
5171 char_u *
5172get_cmdline_str()
5173{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005174 struct cmdline_info *p = get_ccline_ptr();
5175
5176 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005178 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179}
5180
5181/*
5182 * Get the current command line position, counted in bytes.
5183 * Zero is the first position.
5184 * Only works when the command line is being edited.
5185 * Returns -1 when something is wrong.
5186 */
5187 int
5188get_cmdline_pos()
5189{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005190 struct cmdline_info *p = get_ccline_ptr();
5191
5192 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005194 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195}
5196
5197/*
5198 * Set the command line byte position to "pos". Zero is the first position.
5199 * Only works when the command line is being edited.
5200 * Returns 1 when failed, 0 when OK.
5201 */
5202 int
5203set_cmdline_pos(pos)
5204 int pos;
5205{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005206 struct cmdline_info *p = get_ccline_ptr();
5207
5208 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 return 1;
5210
5211 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5212 * changed the command line. */
5213 if (pos < 0)
5214 new_cmdpos = 0;
5215 else
5216 new_cmdpos = pos;
5217 return 0;
5218}
5219
5220/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005221 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005222 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005223 * Only works when the command line is being edited.
5224 * Returns NUL when something is wrong.
5225 */
5226 int
5227get_cmdline_type()
5228{
5229 struct cmdline_info *p = get_ccline_ptr();
5230
5231 if (p == NULL)
5232 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005233 if (p->cmdfirstc == NUL)
5234 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005235 return p->cmdfirstc;
5236}
5237
5238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 * Calculate history index from a number:
5240 * num > 0: seen as identifying number of a history entry
5241 * num < 0: relative position in history wrt newest entry
5242 * "histype" may be one of the HIST_ values.
5243 */
5244 static int
5245calc_hist_idx(histype, num)
5246 int histype;
5247 int num;
5248{
5249 int i;
5250 histentry_T *hist;
5251 int wrapped = FALSE;
5252
5253 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5254 || (i = hisidx[histype]) < 0 || num == 0)
5255 return -1;
5256
5257 hist = history[histype];
5258 if (num > 0)
5259 {
5260 while (hist[i].hisnum > num)
5261 if (--i < 0)
5262 {
5263 if (wrapped)
5264 break;
5265 i += hislen;
5266 wrapped = TRUE;
5267 }
5268 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5269 return i;
5270 }
5271 else if (-num <= hislen)
5272 {
5273 i += num + 1;
5274 if (i < 0)
5275 i += hislen;
5276 if (hist[i].hisstr != NULL)
5277 return i;
5278 }
5279 return -1;
5280}
5281
5282/*
5283 * Get a history entry by its index.
5284 * "histype" may be one of the HIST_ values.
5285 */
5286 char_u *
5287get_history_entry(histype, idx)
5288 int histype;
5289 int idx;
5290{
5291 idx = calc_hist_idx(histype, idx);
5292 if (idx >= 0)
5293 return history[histype][idx].hisstr;
5294 else
5295 return (char_u *)"";
5296}
5297
5298/*
5299 * Clear all entries of a history.
5300 * "histype" may be one of the HIST_ values.
5301 */
5302 int
5303clr_history(histype)
5304 int histype;
5305{
5306 int i;
5307 histentry_T *hisptr;
5308
5309 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5310 {
5311 hisptr = history[histype];
5312 for (i = hislen; i--;)
5313 {
5314 vim_free(hisptr->hisstr);
5315 hisptr->hisnum = 0;
5316 hisptr++->hisstr = NULL;
5317 }
5318 hisidx[histype] = -1; /* mark history as cleared */
5319 hisnum[histype] = 0; /* reset identifier counter */
5320 return OK;
5321 }
5322 return FAIL;
5323}
5324
5325/*
5326 * Remove all entries matching {str} from a history.
5327 * "histype" may be one of the HIST_ values.
5328 */
5329 int
5330del_history_entry(histype, str)
5331 int histype;
5332 char_u *str;
5333{
5334 regmatch_T regmatch;
5335 histentry_T *hisptr;
5336 int idx;
5337 int i;
5338 int last;
5339 int found = FALSE;
5340
5341 regmatch.regprog = NULL;
5342 regmatch.rm_ic = FALSE; /* always match case */
5343 if (hislen != 0
5344 && histype >= 0
5345 && histype < HIST_COUNT
5346 && *str != NUL
5347 && (idx = hisidx[histype]) >= 0
5348 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5349 != NULL)
5350 {
5351 i = last = idx;
5352 do
5353 {
5354 hisptr = &history[histype][i];
5355 if (hisptr->hisstr == NULL)
5356 break;
5357 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5358 {
5359 found = TRUE;
5360 vim_free(hisptr->hisstr);
5361 hisptr->hisstr = NULL;
5362 hisptr->hisnum = 0;
5363 }
5364 else
5365 {
5366 if (i != last)
5367 {
5368 history[histype][last] = *hisptr;
5369 hisptr->hisstr = NULL;
5370 hisptr->hisnum = 0;
5371 }
5372 if (--last < 0)
5373 last += hislen;
5374 }
5375 if (--i < 0)
5376 i += hislen;
5377 } while (i != idx);
5378 if (history[histype][idx].hisstr == NULL)
5379 hisidx[histype] = -1;
5380 }
5381 vim_free(regmatch.regprog);
5382 return found;
5383}
5384
5385/*
5386 * Remove an indexed entry from a history.
5387 * "histype" may be one of the HIST_ values.
5388 */
5389 int
5390del_history_idx(histype, idx)
5391 int histype;
5392 int idx;
5393{
5394 int i, j;
5395
5396 i = calc_hist_idx(histype, idx);
5397 if (i < 0)
5398 return FALSE;
5399 idx = hisidx[histype];
5400 vim_free(history[histype][i].hisstr);
5401
5402 /* When deleting the last added search string in a mapping, reset
5403 * last_maptick, so that the last added search string isn't deleted again.
5404 */
5405 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5406 last_maptick = -1;
5407
5408 while (i != idx)
5409 {
5410 j = (i + 1) % hislen;
5411 history[histype][i] = history[histype][j];
5412 i = j;
5413 }
5414 history[histype][i].hisstr = NULL;
5415 history[histype][i].hisnum = 0;
5416 if (--i < 0)
5417 i += hislen;
5418 hisidx[histype] = i;
5419 return TRUE;
5420}
5421
5422#endif /* FEAT_EVAL */
5423
5424#if defined(FEAT_CRYPT) || defined(PROTO)
5425/*
5426 * Very specific function to remove the value in ":set key=val" from the
5427 * history.
5428 */
5429 void
5430remove_key_from_history()
5431{
5432 char_u *p;
5433 int i;
5434
5435 i = hisidx[HIST_CMD];
5436 if (i < 0)
5437 return;
5438 p = history[HIST_CMD][i].hisstr;
5439 if (p != NULL)
5440 for ( ; *p; ++p)
5441 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5442 {
5443 p = vim_strchr(p + 3, '=');
5444 if (p == NULL)
5445 break;
5446 ++p;
5447 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5448 if (p[i] == '\\' && p[i + 1])
5449 ++i;
5450 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5451 --p;
5452 }
5453}
5454#endif
5455
5456#endif /* FEAT_CMDHIST */
5457
5458#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5459/*
5460 * Get indices "num1,num2" that specify a range within a list (not a range of
5461 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5462 * Returns OK if parsed successfully, otherwise FAIL.
5463 */
5464 int
5465get_list_range(str, num1, num2)
5466 char_u **str;
5467 int *num1;
5468 int *num2;
5469{
5470 int len;
5471 int first = FALSE;
5472 long num;
5473
5474 *str = skipwhite(*str);
5475 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5476 {
5477 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5478 *str += len;
5479 *num1 = (int)num;
5480 first = TRUE;
5481 }
5482 *str = skipwhite(*str);
5483 if (**str == ',') /* parse "to" part of range */
5484 {
5485 *str = skipwhite(*str + 1);
5486 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5487 if (len > 0)
5488 {
5489 *num2 = (int)num;
5490 *str = skipwhite(*str + len);
5491 }
5492 else if (!first) /* no number given at all */
5493 return FAIL;
5494 }
5495 else if (first) /* only one number given */
5496 *num2 = *num1;
5497 return OK;
5498}
5499#endif
5500
5501#if defined(FEAT_CMDHIST) || defined(PROTO)
5502/*
5503 * :history command - print a history
5504 */
5505 void
5506ex_history(eap)
5507 exarg_T *eap;
5508{
5509 histentry_T *hist;
5510 int histype1 = HIST_CMD;
5511 int histype2 = HIST_CMD;
5512 int hisidx1 = 1;
5513 int hisidx2 = -1;
5514 int idx;
5515 int i, j, k;
5516 char_u *end;
5517 char_u *arg = eap->arg;
5518
5519 if (hislen == 0)
5520 {
5521 MSG(_("'history' option is zero"));
5522 return;
5523 }
5524
5525 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5526 {
5527 end = arg;
5528 while (ASCII_ISALPHA(*end)
5529 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5530 end++;
5531 i = *end;
5532 *end = NUL;
5533 histype1 = get_histtype(arg);
5534 if (histype1 == -1)
5535 {
5536 if (STRICMP(arg, "all") == 0)
5537 {
5538 histype1 = 0;
5539 histype2 = HIST_COUNT-1;
5540 }
5541 else
5542 {
5543 *end = i;
5544 EMSG(_(e_trailing));
5545 return;
5546 }
5547 }
5548 else
5549 histype2 = histype1;
5550 *end = i;
5551 }
5552 else
5553 end = arg;
5554 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5555 {
5556 EMSG(_(e_trailing));
5557 return;
5558 }
5559
5560 for (; !got_int && histype1 <= histype2; ++histype1)
5561 {
5562 STRCPY(IObuff, "\n # ");
5563 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5564 MSG_PUTS_TITLE(IObuff);
5565 idx = hisidx[histype1];
5566 hist = history[histype1];
5567 j = hisidx1;
5568 k = hisidx2;
5569 if (j < 0)
5570 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5571 if (k < 0)
5572 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5573 if (idx >= 0 && j <= k)
5574 for (i = idx + 1; !got_int; ++i)
5575 {
5576 if (i == hislen)
5577 i = 0;
5578 if (hist[i].hisstr != NULL
5579 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5580 {
5581 msg_putchar('\n');
5582 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5583 hist[i].hisnum);
5584 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5585 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5586 (int)Columns - 10);
5587 else
5588 STRCAT(IObuff, hist[i].hisstr);
5589 msg_outtrans(IObuff);
5590 out_flush();
5591 }
5592 if (i == idx)
5593 break;
5594 }
5595 }
5596}
5597#endif
5598
5599#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5600static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5601static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5602static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5603static int viminfo_add_at_front = FALSE;
5604
5605static int hist_type2char __ARGS((int type, int use_question));
5606
5607/*
5608 * Translate a history type number to the associated character.
5609 */
5610 static int
5611hist_type2char(type, use_question)
5612 int type;
5613 int use_question; /* use '?' instead of '/' */
5614{
5615 if (type == HIST_CMD)
5616 return ':';
5617 if (type == HIST_SEARCH)
5618 {
5619 if (use_question)
5620 return '?';
5621 else
5622 return '/';
5623 }
5624 if (type == HIST_EXPR)
5625 return '=';
5626 return '@';
5627}
5628
5629/*
5630 * Prepare for reading the history from the viminfo file.
5631 * This allocates history arrays to store the read history lines.
5632 */
5633 void
5634prepare_viminfo_history(asklen)
5635 int asklen;
5636{
5637 int i;
5638 int num;
5639 int type;
5640 int len;
5641
5642 init_history();
5643 viminfo_add_at_front = (asklen != 0);
5644 if (asklen > hislen)
5645 asklen = hislen;
5646
5647 for (type = 0; type < HIST_COUNT; ++type)
5648 {
5649 /*
5650 * Count the number of empty spaces in the history list. If there are
5651 * more spaces available than we request, then fill them up.
5652 */
5653 for (i = 0, num = 0; i < hislen; i++)
5654 if (history[type][i].hisstr == NULL)
5655 num++;
5656 len = asklen;
5657 if (num > len)
5658 len = num;
5659 if (len <= 0)
5660 viminfo_history[type] = NULL;
5661 else
5662 viminfo_history[type] =
5663 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5664 if (viminfo_history[type] == NULL)
5665 len = 0;
5666 viminfo_hislen[type] = len;
5667 viminfo_hisidx[type] = 0;
5668 }
5669}
5670
5671/*
5672 * Accept a line from the viminfo, store it in the history array when it's
5673 * new.
5674 */
5675 int
5676read_viminfo_history(virp)
5677 vir_T *virp;
5678{
5679 int type;
5680 long_u len;
5681 char_u *val;
5682 char_u *p;
5683
5684 type = hist_char2type(virp->vir_line[0]);
5685 if (viminfo_hisidx[type] < viminfo_hislen[type])
5686 {
5687 val = viminfo_readstring(virp, 1, TRUE);
5688 if (val != NULL && *val != NUL)
5689 {
5690 if (!in_history(type, val + (type == HIST_SEARCH),
5691 viminfo_add_at_front))
5692 {
5693 /* Need to re-allocate to append the separator byte. */
5694 len = STRLEN(val);
5695 p = lalloc(len + 2, TRUE);
5696 if (p != NULL)
5697 {
5698 if (type == HIST_SEARCH)
5699 {
5700 /* Search entry: Move the separator from the first
5701 * column to after the NUL. */
5702 mch_memmove(p, val + 1, (size_t)len);
5703 p[len] = (*val == ' ' ? NUL : *val);
5704 }
5705 else
5706 {
5707 /* Not a search entry: No separator in the viminfo
5708 * file, add a NUL separator. */
5709 mch_memmove(p, val, (size_t)len + 1);
5710 p[len + 1] = NUL;
5711 }
5712 viminfo_history[type][viminfo_hisidx[type]++] = p;
5713 }
5714 }
5715 }
5716 vim_free(val);
5717 }
5718 return viminfo_readline(virp);
5719}
5720
5721 void
5722finish_viminfo_history()
5723{
5724 int idx;
5725 int i;
5726 int type;
5727
5728 for (type = 0; type < HIST_COUNT; ++type)
5729 {
5730 if (history[type] == NULL)
5731 return;
5732 idx = hisidx[type] + viminfo_hisidx[type];
5733 if (idx >= hislen)
5734 idx -= hislen;
5735 else if (idx < 0)
5736 idx = hislen - 1;
5737 if (viminfo_add_at_front)
5738 hisidx[type] = idx;
5739 else
5740 {
5741 if (hisidx[type] == -1)
5742 hisidx[type] = hislen - 1;
5743 do
5744 {
5745 if (history[type][idx].hisstr != NULL)
5746 break;
5747 if (++idx == hislen)
5748 idx = 0;
5749 } while (idx != hisidx[type]);
5750 if (idx != hisidx[type] && --idx < 0)
5751 idx = hislen - 1;
5752 }
5753 for (i = 0; i < viminfo_hisidx[type]; i++)
5754 {
5755 vim_free(history[type][idx].hisstr);
5756 history[type][idx].hisstr = viminfo_history[type][i];
5757 if (--idx < 0)
5758 idx = hislen - 1;
5759 }
5760 idx += 1;
5761 idx %= hislen;
5762 for (i = 0; i < viminfo_hisidx[type]; i++)
5763 {
5764 history[type][idx++].hisnum = ++hisnum[type];
5765 idx %= hislen;
5766 }
5767 vim_free(viminfo_history[type]);
5768 viminfo_history[type] = NULL;
5769 }
5770}
5771
5772 void
5773write_viminfo_history(fp)
5774 FILE *fp;
5775{
5776 int i;
5777 int type;
5778 int num_saved;
5779 char_u *p;
5780 int c;
5781
5782 init_history();
5783 if (hislen == 0)
5784 return;
5785 for (type = 0; type < HIST_COUNT; ++type)
5786 {
5787 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5788 if (num_saved == 0)
5789 continue;
5790 if (num_saved < 0) /* Use default */
5791 num_saved = hislen;
5792 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5793 type == HIST_CMD ? _("Command Line") :
5794 type == HIST_SEARCH ? _("Search String") :
5795 type == HIST_EXPR ? _("Expression") :
5796 _("Input Line"));
5797 if (num_saved > hislen)
5798 num_saved = hislen;
5799 i = hisidx[type];
5800 if (i >= 0)
5801 while (num_saved--)
5802 {
5803 p = history[type][i].hisstr;
5804 if (p != NULL)
5805 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005806 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 /* For the search history: put the separator in the second
5808 * column; use a space if there isn't one. */
5809 if (type == HIST_SEARCH)
5810 {
5811 c = p[STRLEN(p) + 1];
5812 putc(c == NUL ? ' ' : c, fp);
5813 }
5814 viminfo_writestring(fp, p);
5815 }
5816 if (--i < 0)
5817 i = hislen - 1;
5818 }
5819 }
5820}
5821#endif /* FEAT_VIMINFO */
5822
5823#if defined(FEAT_FKMAP) || defined(PROTO)
5824/*
5825 * Write a character at the current cursor+offset position.
5826 * It is directly written into the command buffer block.
5827 */
5828 void
5829cmd_pchar(c, offset)
5830 int c, offset;
5831{
5832 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5833 {
5834 EMSG(_("E198: cmd_pchar beyond the command length"));
5835 return;
5836 }
5837 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5838 ccline.cmdbuff[ccline.cmdlen] = NUL;
5839}
5840
5841 int
5842cmd_gchar(offset)
5843 int offset;
5844{
5845 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5846 {
5847 /* EMSG(_("cmd_gchar beyond the command length")); */
5848 return NUL;
5849 }
5850 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5851}
5852#endif
5853
5854#if defined(FEAT_CMDWIN) || defined(PROTO)
5855/*
5856 * Open a window on the current command line and history. Allow editing in
5857 * the window. Returns when the window is closed.
5858 * Returns:
5859 * CR if the command is to be executed
5860 * Ctrl_C if it is to be abandoned
5861 * K_IGNORE if editing continues
5862 */
5863 static int
5864ex_window()
5865{
5866 struct cmdline_info save_ccline;
5867 buf_T *old_curbuf = curbuf;
5868 win_T *old_curwin = curwin;
5869 buf_T *bp;
5870 win_T *wp;
5871 int i;
5872 linenr_T lnum;
5873 int histtype;
5874 garray_T winsizes;
5875 char_u typestr[2];
5876 int save_restart_edit = restart_edit;
5877 int save_State = State;
5878 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005879#ifdef FEAT_RIGHTLEFT
5880 int save_cmdmsg_rl = cmdmsg_rl;
5881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
5883 /* Can't do this recursively. Can't do it when typing a password. */
5884 if (cmdwin_type != 0
5885# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5886 || cmdline_star > 0
5887# endif
5888 )
5889 {
5890 beep_flush();
5891 return K_IGNORE;
5892 }
5893
5894 /* Save current window sizes. */
5895 win_size_save(&winsizes);
5896
5897# ifdef FEAT_AUTOCMD
5898 /* Don't execute autocommands while creating the window. */
5899 ++autocmd_block;
5900# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005901 /* don't use a new tab page */
5902 cmdmod.tab = 0;
5903
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 /* Create a window for the command-line buffer. */
5905 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5906 {
5907 beep_flush();
5908 return K_IGNORE;
5909 }
5910 cmdwin_type = ccline.cmdfirstc;
5911 if (cmdwin_type == NUL)
5912 cmdwin_type = '-';
5913
5914 /* Create the command-line buffer empty. */
5915 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5916 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5917 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5918 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5919 curbuf->b_p_ma = TRUE;
5920# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005921 curwin->w_p_rl = cmdmsg_rl;
5922 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923# endif
5924# ifdef FEAT_SCROLLBIND
5925 curwin->w_p_scb = FALSE;
5926# endif
5927
5928# ifdef FEAT_AUTOCMD
5929 /* Do execute autocommands for setting the filetype (load syntax). */
5930 --autocmd_block;
5931# endif
5932
Bram Moolenaar46152342005-09-07 21:18:43 +00005933 /* Showing the prompt may have set need_wait_return, reset it. */
5934 need_wait_return = FALSE;
5935
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 histtype = hist_char2type(ccline.cmdfirstc);
5937 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5938 {
5939 if (p_wc == TAB)
5940 {
5941 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5942 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5943 }
5944 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5945 }
5946
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005947 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5948 * sets 'textwidth' to 78). */
5949 curbuf->b_p_tw = 0;
5950
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 /* Fill the buffer with the history. */
5952 init_history();
5953 if (hislen > 0)
5954 {
5955 i = hisidx[histtype];
5956 if (i >= 0)
5957 {
5958 lnum = 0;
5959 do
5960 {
5961 if (++i == hislen)
5962 i = 0;
5963 if (history[histtype][i].hisstr != NULL)
5964 ml_append(lnum++, history[histtype][i].hisstr,
5965 (colnr_T)0, FALSE);
5966 }
5967 while (i != hisidx[histtype]);
5968 }
5969 }
5970
5971 /* Replace the empty last line with the current command-line and put the
5972 * cursor there. */
5973 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5974 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5975 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005976 changed_line_abv_curs();
5977 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005978 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
5980 /* Save the command line info, can be used recursively. */
5981 save_ccline = ccline;
5982 ccline.cmdbuff = NULL;
5983 ccline.cmdprompt = NULL;
5984
5985 /* No Ex mode here! */
5986 exmode_active = 0;
5987
5988 State = NORMAL;
5989# ifdef FEAT_MOUSE
5990 setmouse();
5991# endif
5992
5993# ifdef FEAT_AUTOCMD
5994 /* Trigger CmdwinEnter autocommands. */
5995 typestr[0] = cmdwin_type;
5996 typestr[1] = NUL;
5997 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00005998 if (restart_edit != 0) /* autocmd with ":startinsert" */
5999 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000# endif
6001
6002 i = RedrawingDisabled;
6003 RedrawingDisabled = 0;
6004
6005 /*
6006 * Call the main loop until <CR> or CTRL-C is typed.
6007 */
6008 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006009 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010
6011 RedrawingDisabled = i;
6012
6013# ifdef FEAT_AUTOCMD
6014 /* Trigger CmdwinLeave autocommands. */
6015 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6016# endif
6017
6018 /* Restore the comand line info. */
6019 ccline = save_ccline;
6020 cmdwin_type = 0;
6021
6022 exmode_active = save_exmode;
6023
6024 /* Safety check: The old window or buffer was deleted: It's a a bug when
6025 * this happens! */
6026 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6027 {
6028 cmdwin_result = Ctrl_C;
6029 EMSG(_("E199: Active window or buffer deleted"));
6030 }
6031 else
6032 {
6033# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6034 /* autocmds may abort script processing */
6035 if (aborting() && cmdwin_result != K_IGNORE)
6036 cmdwin_result = Ctrl_C;
6037# endif
6038 /* Set the new command line from the cmdline buffer. */
6039 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006040 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006042 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6043
6044 if (histtype == HIST_CMD)
6045 {
6046 /* Execute the command directly. */
6047 ccline.cmdbuff = vim_strsave((char_u *)p);
6048 cmdwin_result = CAR;
6049 }
6050 else
6051 {
6052 /* First need to cancel what we were doing. */
6053 ccline.cmdbuff = NULL;
6054 stuffcharReadbuff(':');
6055 stuffReadbuff((char_u *)p);
6056 stuffcharReadbuff(CAR);
6057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 }
6059 else if (cmdwin_result == K_XF2) /* :qa typed */
6060 {
6061 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6062 cmdwin_result = CAR;
6063 }
6064 else
6065 ccline.cmdbuff = vim_strsave(ml_get_curline());
6066 if (ccline.cmdbuff == NULL)
6067 cmdwin_result = Ctrl_C;
6068 else
6069 {
6070 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6071 ccline.cmdbufflen = ccline.cmdlen + 1;
6072 ccline.cmdpos = curwin->w_cursor.col;
6073 if (ccline.cmdpos > ccline.cmdlen)
6074 ccline.cmdpos = ccline.cmdlen;
6075 if (cmdwin_result == K_IGNORE)
6076 {
6077 set_cmdspos_cursor();
6078 redrawcmd();
6079 }
6080 }
6081
6082# ifdef FEAT_AUTOCMD
6083 /* Don't execute autocommands while deleting the window. */
6084 ++autocmd_block;
6085# endif
6086 wp = curwin;
6087 bp = curbuf;
6088 win_goto(old_curwin);
6089 win_close(wp, TRUE);
6090 close_buffer(NULL, bp, DOBUF_WIPE);
6091
6092 /* Restore window sizes. */
6093 win_size_restore(&winsizes);
6094
6095# ifdef FEAT_AUTOCMD
6096 --autocmd_block;
6097# endif
6098 }
6099
6100 ga_clear(&winsizes);
6101 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006102# ifdef FEAT_RIGHTLEFT
6103 cmdmsg_rl = save_cmdmsg_rl;
6104# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 State = save_State;
6107# ifdef FEAT_MOUSE
6108 setmouse();
6109# endif
6110
6111 return cmdwin_result;
6112}
6113#endif /* FEAT_CMDWIN */
6114
6115/*
6116 * Used for commands that either take a simple command string argument, or:
6117 * cmd << endmarker
6118 * {script}
6119 * endmarker
6120 * Returns a pointer to allocated memory with {script} or NULL.
6121 */
6122 char_u *
6123script_get(eap, cmd)
6124 exarg_T *eap;
6125 char_u *cmd;
6126{
6127 char_u *theline;
6128 char *end_pattern = NULL;
6129 char dot[] = ".";
6130 garray_T ga;
6131
6132 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6133 return NULL;
6134
6135 ga_init2(&ga, 1, 0x400);
6136
6137 if (cmd[2] != NUL)
6138 end_pattern = (char *)skipwhite(cmd + 2);
6139 else
6140 end_pattern = dot;
6141
6142 for (;;)
6143 {
6144 theline = eap->getline(
6145#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006146 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147#endif
6148 NUL, eap->cookie, 0);
6149
6150 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6151 break;
6152
6153 ga_concat(&ga, theline);
6154 ga_append(&ga, '\n');
6155 vim_free(theline);
6156 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006157 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
6159 return (char_u *)ga.ga_data;
6160}