blob: cd40a689dd6aee7b2a95a5785c1d9a418787bc3e [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(). */
34};
35
36static struct cmdline_info ccline; /* current cmdline_info */
37
38static int cmd_showtail; /* Only show path tail in lists ? */
39
40#ifdef FEAT_EVAL
41static int new_cmdpos; /* position set by set_cmdline_pos() */
42#endif
43
44#ifdef FEAT_CMDHIST
45typedef struct hist_entry
46{
47 int hisnum; /* identifying number */
48 char_u *hisstr; /* actual entry, separator char after the NUL */
49} histentry_T;
50
51static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
52static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
53static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
54 /* identifying (unique) number of newest history entry */
55static int hislen = 0; /* actual length of history tables */
56
57static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
59static int in_history __ARGS((int, char_u *, int));
60# ifdef FEAT_EVAL
61static int calc_hist_idx __ARGS((int histype, int num));
62# endif
63#endif
64
65#ifdef FEAT_RIGHTLEFT
66static int cmd_hkmap = 0; /* Hebrew mapping during command line */
67#endif
68
69#ifdef FEAT_FKMAP
70static int cmd_fkmap = 0; /* Farsi mapping during command line */
71#endif
72
73static int cmdline_charsize __ARGS((int idx));
74static void set_cmdspos __ARGS((void));
75static void set_cmdspos_cursor __ARGS((void));
76#ifdef FEAT_MBYTE
77static void correct_cmdspos __ARGS((int idx, int cells));
78#endif
79static void alloc_cmdbuff __ARGS((int len));
80static int realloc_cmdbuff __ARGS((int len));
81static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000082static void save_cmdline __ARGS((struct cmdline_info *ccp));
83static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar8299df92004-07-10 09:47:34 +000084static int cmdline_paste __ARGS((int regname, int literally));
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
86static void redrawcmd_preedit __ARGS((void));
87#endif
88#ifdef FEAT_WILDMENU
89static void cmdline_del __ARGS((int from));
90#endif
91static void redrawcmdprompt __ARGS((void));
92static void cursorcmd __ARGS((void));
93static int ccheck_abbr __ARGS((int));
94static int nextwild __ARGS((expand_T *xp, int type, int options));
Bram Moolenaar45360022005-07-21 21:08:21 +000095static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000096static int showmatches __ARGS((expand_T *xp, int wildmenu));
97static void set_expand_context __ARGS((expand_T *xp));
98static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
99static int expand_showtail __ARGS((expand_T *xp));
100#ifdef FEAT_CMDL_COMPL
101static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
102# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
103static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000104static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105# endif
106#endif
107
108#ifdef FEAT_CMDWIN
109static int ex_window __ARGS((void));
110#endif
111
112/*
113 * getcmdline() - accept a command line starting with firstc.
114 *
115 * firstc == ':' get ":" command line.
116 * firstc == '/' or '?' get search pattern
117 * firstc == '=' get expression
118 * firstc == '@' get text for input() function
119 * firstc == '>' get text for debug mode
120 * firstc == NUL get text for :insert command
121 * firstc == -1 like NUL, and break on CTRL-C
122 *
123 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
124 * command line.
125 *
126 * Careful: getcmdline() can be called recursively!
127 *
128 * Return pointer to allocated string if there is a commandline, NULL
129 * otherwise.
130 */
131/*ARGSUSED*/
132 char_u *
133getcmdline(firstc, count, indent)
134 int firstc;
135 long count; /* only used for incremental search */
136 int indent; /* indent for inside conditionals */
137{
138 int c;
139 int i;
140 int j;
141 int gotesc = FALSE; /* TRUE when <ESC> just typed */
142 int do_abbr; /* when TRUE check for abbr. */
143#ifdef FEAT_CMDHIST
144 char_u *lookfor = NULL; /* string to match */
145 int hiscnt; /* current history line in use */
146 int histype; /* history type to be used */
147#endif
148#ifdef FEAT_SEARCH_EXTRA
149 pos_T old_cursor;
150 colnr_T old_curswant;
151 colnr_T old_leftcol;
152 linenr_T old_topline;
153# ifdef FEAT_DIFF
154 int old_topfill;
155# endif
156 linenr_T old_botline;
157 int did_incsearch = FALSE;
158 int incsearch_postponed = FALSE;
159#endif
160 int did_wild_list = FALSE; /* did wild_list() recently */
161 int wim_index = 0; /* index in wim_flags[] */
162 int res;
163 int save_msg_scroll = msg_scroll;
164 int save_State = State; /* remember State when called */
165 int some_key_typed = FALSE; /* one of the keys was typed */
166#ifdef FEAT_MOUSE
167 /* mouse drag and release events are ignored, unless they are
168 * preceded with a mouse down event */
169 int ignore_drag_release = TRUE;
170#endif
171#ifdef FEAT_EVAL
172 int break_ctrl_c = FALSE;
173#endif
174 expand_T xpc;
175 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000176#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
177 /* Everything that may work recursively should save and restore the
178 * current command line in save_ccline. That includes update_screen(), a
179 * custom status line may invoke ":normal". */
180 struct cmdline_info save_ccline;
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182
183#ifdef FEAT_SNIFF
184 want_sniff_request = 0;
185#endif
186#ifdef FEAT_EVAL
187 if (firstc == -1)
188 {
189 firstc = NUL;
190 break_ctrl_c = TRUE;
191 }
192#endif
193#ifdef FEAT_RIGHTLEFT
194 /* start without Hebrew mapping for a command line */
195 if (firstc == ':' || firstc == '=' || firstc == '>')
196 cmd_hkmap = 0;
197#endif
198
199 ccline.overstrike = FALSE; /* always start in insert mode */
200#ifdef FEAT_SEARCH_EXTRA
201 old_cursor = curwin->w_cursor; /* needs to be restored later */
202 old_curswant = curwin->w_curswant;
203 old_leftcol = curwin->w_leftcol;
204 old_topline = curwin->w_topline;
205# ifdef FEAT_DIFF
206 old_topfill = curwin->w_topfill;
207# endif
208 old_botline = curwin->w_botline;
209#endif
210
211 /*
212 * set some variables for redrawcmd()
213 */
214 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000215 ccline.cmdindent = (firstc > 0 ? indent : 0);
216
217 /* alloc initial ccline.cmdbuff */
218 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 if (ccline.cmdbuff == NULL)
220 return NULL; /* out of memory */
221 ccline.cmdlen = ccline.cmdpos = 0;
222 ccline.cmdbuff[0] = NUL;
223
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000224 /* autoindent for :insert and :append */
225 if (firstc <= 0)
226 {
227 copy_spaces(ccline.cmdbuff, indent);
228 ccline.cmdbuff[indent] = NUL;
229 ccline.cmdpos = indent;
230 ccline.cmdspos = indent;
231 ccline.cmdlen = indent;
232 }
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 ExpandInit(&xpc);
235
236#ifdef FEAT_RIGHTLEFT
237 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
238 && (firstc == '/' || firstc == '?'))
239 cmdmsg_rl = TRUE;
240 else
241 cmdmsg_rl = FALSE;
242#endif
243
244 redir_off = TRUE; /* don't redirect the typed command */
245 if (!cmd_silent)
246 {
247 i = msg_scrolled;
248 msg_scrolled = 0; /* avoid wait_return message */
249 gotocmdline(TRUE);
250 msg_scrolled += i;
251 redrawcmdprompt(); /* draw prompt or indent */
252 set_cmdspos();
253 }
254 xpc.xp_context = EXPAND_NOTHING;
255 xpc.xp_backslash = XP_BS_NONE;
256
257 /*
258 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
259 * doing ":@0" when register 0 doesn't contain a CR.
260 */
261 msg_scroll = FALSE;
262
263 State = CMDLINE;
264
265 if (firstc == '/' || firstc == '?' || firstc == '@')
266 {
267 /* Use ":lmap" mappings for search pattern and input(). */
268 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
269 b_im_ptr = &curbuf->b_p_iminsert;
270 else
271 b_im_ptr = &curbuf->b_p_imsearch;
272 if (*b_im_ptr == B_IMODE_LMAP)
273 State |= LANGMAP;
274#ifdef USE_IM_CONTROL
275 im_set_active(*b_im_ptr == B_IMODE_IM);
276#endif
277 }
278#ifdef USE_IM_CONTROL
279 else if (p_imcmdline)
280 im_set_active(TRUE);
281#endif
282
283#ifdef FEAT_MOUSE
284 setmouse();
285#endif
286#ifdef CURSOR_SHAPE
287 ui_cursor_shape(); /* may show different cursor shape */
288#endif
289
290#ifdef FEAT_CMDHIST
291 init_history();
292 hiscnt = hislen; /* set hiscnt to impossible history value */
293 histype = hist_char2type(firstc);
294#endif
295
296#ifdef FEAT_DIGRAPHS
297 do_digraph(-1); /* init digraph typahead */
298#endif
299
300 /*
301 * Collect the command string, handling editing keys.
302 */
303 for (;;)
304 {
305#ifdef USE_ON_FLY_SCROLL
306 dont_scroll = FALSE; /* allow scrolling here */
307#endif
308 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
309
310 cursorcmd(); /* set the cursor on the right spot */
311 c = safe_vgetc();
312 if (KeyTyped)
313 {
314 some_key_typed = TRUE;
315#ifdef FEAT_RIGHTLEFT
316 if (cmd_hkmap)
317 c = hkmap(c);
318# ifdef FEAT_FKMAP
319 if (cmd_fkmap)
320 c = cmdl_fkmap(c);
321# endif
322 if (cmdmsg_rl && !KeyStuffed)
323 {
324 /* Invert horizontal movements and operations. Only when
325 * typed by the user directly, not when the result of a
326 * mapping. */
327 switch (c)
328 {
329 case K_RIGHT: c = K_LEFT; break;
330 case K_S_RIGHT: c = K_S_LEFT; break;
331 case K_C_RIGHT: c = K_C_LEFT; break;
332 case K_LEFT: c = K_RIGHT; break;
333 case K_S_LEFT: c = K_S_RIGHT; break;
334 case K_C_LEFT: c = K_C_RIGHT; break;
335 }
336 }
337#endif
338 }
339
340 /*
341 * Ignore got_int when CTRL-C was typed here.
342 * Don't ignore it in :global, we really need to break then, e.g., for
343 * ":g/pat/normal /pat" (without the <CR>).
344 * Don't ignore it for the input() function.
345 */
346 if ((c == Ctrl_C
347#ifdef UNIX
348 || c == intr_char
349#endif
350 )
351#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
352 && firstc != '@'
353#endif
354#ifdef FEAT_EVAL
355 && !break_ctrl_c
356#endif
357 && !global_busy)
358 got_int = FALSE;
359
360#ifdef FEAT_CMDHIST
361 /* free old command line when finished moving around in the history
362 * list */
363 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000364 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000365 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 && c != K_PAGEDOWN && c != K_PAGEUP
367 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000368 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
370 {
371 vim_free(lookfor);
372 lookfor = NULL;
373 }
374#endif
375
376 /*
377 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
378 */
379 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
380 c = Ctrl_P;
381
382#ifdef FEAT_WILDMENU
383 /* Special translations for 'wildmenu' */
384 if (did_wild_list && p_wmnu)
385 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000386 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000388 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 c = Ctrl_N;
390 }
391 /* Hitting CR after "emenu Name.": complete submenu */
392 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
393 && ccline.cmdpos > 1
394 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
395 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
396 && (c == '\n' || c == '\r' || c == K_KENTER))
397 c = K_DOWN;
398#endif
399
400 /* free expanded names when finished walking through matches */
401 if (xpc.xp_numfiles != -1
402 && !(c == p_wc && KeyTyped) && c != p_wcm
403 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
404 && c != Ctrl_L)
405 {
406 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
407 did_wild_list = FALSE;
408#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000409 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410#endif
411 xpc.xp_context = EXPAND_NOTHING;
412 wim_index = 0;
413#ifdef FEAT_WILDMENU
414 if (p_wmnu && wild_menu_showing != 0)
415 {
416 int skt = KeyTyped;
417
418 if (wild_menu_showing == WM_SCROLLED)
419 {
420 /* Entered command line, move it up */
421 cmdline_row--;
422 redrawcmd();
423 }
424 else if (save_p_ls != -1)
425 {
426 /* restore 'laststatus' and 'winminheight' */
427 p_ls = save_p_ls;
428 p_wmh = save_p_wmh;
429 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000430 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000432 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 redrawcmd();
434 save_p_ls = -1;
435 }
436 else
437 {
438# ifdef FEAT_VERTSPLIT
439 win_redraw_last_status(topframe);
440# else
441 lastwin->w_redr_status = TRUE;
442# endif
443 redraw_statuslines();
444 }
445 KeyTyped = skt;
446 wild_menu_showing = 0;
447 }
448#endif
449 }
450
451#ifdef FEAT_WILDMENU
452 /* Special translations for 'wildmenu' */
453 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
454 {
455 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000456 if (ccline.cmdbuff[ccline.cmdpos - 1] == '.' && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000458 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 {
460 /* Hitting <Up>: Remove one submenu name in front of the
461 * cursor */
462 int found = FALSE;
463
464 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
465 i = 0;
466 while (--j > 0)
467 {
468 /* check for start of menu name */
469 if (ccline.cmdbuff[j] == ' '
470 && ccline.cmdbuff[j - 1] != '\\')
471 {
472 i = j + 1;
473 break;
474 }
475 /* check for start of submenu name */
476 if (ccline.cmdbuff[j] == '.'
477 && ccline.cmdbuff[j - 1] != '\\')
478 {
479 if (found)
480 {
481 i = j + 1;
482 break;
483 }
484 else
485 found = TRUE;
486 }
487 }
488 if (i > 0)
489 cmdline_del(i);
490 c = p_wc;
491 xpc.xp_context = EXPAND_NOTHING;
492 }
493 }
494 if (xpc.xp_context == EXPAND_FILES && p_wmnu)
495 {
496 char_u upseg[5];
497
498 upseg[0] = PATHSEP;
499 upseg[1] = '.';
500 upseg[2] = '.';
501 upseg[3] = PATHSEP;
502 upseg[4] = NUL;
503
504 if (ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000505 && c == K_DOWN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 && (ccline.cmdbuff[ccline.cmdpos - 2] != '.'
507 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
508 {
509 /* go down a directory */
510 c = p_wc;
511 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000512 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 {
514 /* If in a direct ancestor, strip off one ../ to go down */
515 int found = FALSE;
516
517 j = ccline.cmdpos;
518 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
519 while (--j > i)
520 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000521#ifdef FEAT_MBYTE
522 if (has_mbyte)
523 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 if (vim_ispathsep(ccline.cmdbuff[j]))
526 {
527 found = TRUE;
528 break;
529 }
530 }
531 if (found
532 && ccline.cmdbuff[j - 1] == '.'
533 && ccline.cmdbuff[j - 2] == '.'
534 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
535 {
536 cmdline_del(j - 2);
537 c = p_wc;
538 }
539 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000540 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {
542 /* go up a directory */
543 int found = FALSE;
544
545 j = ccline.cmdpos - 1;
546 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
547 while (--j > i)
548 {
549#ifdef FEAT_MBYTE
550 if (has_mbyte)
551 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
552#endif
553 if (vim_ispathsep(ccline.cmdbuff[j])
554#ifdef BACKSLASH_IN_FILENAME
555 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
556 == NULL
557#endif
558 )
559 {
560 if (found)
561 {
562 i = j + 1;
563 break;
564 }
565 else
566 found = TRUE;
567 }
568 }
569
570 if (!found)
571 j = i;
572 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
573 j += 4;
574 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
575 && j == i)
576 j += 3;
577 else
578 j = 0;
579 if (j > 0)
580 {
581 /* TODO this is only for DOS/UNIX systems - need to put in
582 * machine-specific stuff here and in upseg init */
583 cmdline_del(j);
584 put_on_cmdline(upseg + 1, 3, FALSE);
585 }
586 else if (ccline.cmdpos > i)
587 cmdline_del(i);
588 c = p_wc;
589 }
590 }
591#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
592 if (p_wmnu
593 && (xpc.xp_context == EXPAND_FILES
594 || xpc.xp_context == EXPAND_MENUNAMES)
595 && (c == K_UP || c == K_DOWN))
596 xpc.xp_context = EXPAND_NOTHING;
597#endif
598
599#endif /* FEAT_WILDMENU */
600
601 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
602 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
603 if (c == Ctrl_BSL)
604 {
605 ++no_mapping;
606 ++allow_keys;
607 c = safe_vgetc();
608 --no_mapping;
609 --allow_keys;
610 /* CTRL-\ e doesn't work when obtaining an expression. */
611 if (c != Ctrl_N && c != Ctrl_G
612 && (c != 'e' || ccline.cmdfirstc == '='))
613 {
614 vungetc(c);
615 c = Ctrl_BSL;
616 }
617#ifdef FEAT_EVAL
618 else if (c == 'e')
619 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000620 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621
622 /*
623 * Replace the command line with the result of an expression.
624 * Need to save the current command line, to be able to enter
625 * a new one...
626 */
627 if (ccline.cmdpos == ccline.cmdlen)
628 new_cmdpos = 99999; /* keep it at the end */
629 else
630 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000631
632 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000634 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (c == '=')
636 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000637 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 p = get_expr_line();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000639 restore_cmdline(&save_ccline);
640
641 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 {
643 ccline.cmdlen = STRLEN(p);
644 STRCPY(ccline.cmdbuff, p);
645 vim_free(p);
646
647 /* Restore the cursor or use the position set with
648 * set_cmdline_pos(). */
649 if (new_cmdpos > ccline.cmdlen)
650 ccline.cmdpos = ccline.cmdlen;
651 else
652 ccline.cmdpos = new_cmdpos;
653
654 KeyTyped = FALSE; /* Don't do p_wc completion. */
655 redrawcmd();
656 goto cmdline_changed;
657 }
658 }
659 beep_flush();
660 c = ESC;
661 }
662#endif
663 else
664 {
665 if (c == Ctrl_G && p_im && restart_edit == 0)
666 restart_edit = 'a';
667 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
668 in history */
669 goto returncmd; /* back to Normal mode */
670 }
671 }
672
673#ifdef FEAT_CMDWIN
674 if (c == cedit_key || c == K_CMDWIN)
675 {
676 /*
677 * Open a window to edit the command line (and history).
678 */
679 c = ex_window();
680 some_key_typed = TRUE;
681 }
682# ifdef FEAT_DIGRAPHS
683 else
684# endif
685#endif
686#ifdef FEAT_DIGRAPHS
687 c = do_digraph(c);
688#endif
689
690 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
691 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
692 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000693 /* In Ex mode a backslash escapes a newline. */
694 if (exmode_active
695 && c != ESC
696 && ccline.cmdpos > 0
697 && ccline.cmdpos == ccline.cmdlen
698 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000700 if (c == K_KENTER)
701 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000703 else
704 {
705 gotesc = FALSE; /* Might have typed ESC previously, don't
706 truncate the cmdline now. */
707 if (ccheck_abbr(c + ABBR_OFF))
708 goto cmdline_changed;
709 if (!cmd_silent)
710 {
711 windgoto(msg_row, 0);
712 out_flush();
713 }
714 break;
715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717
718 /*
719 * Completion for 'wildchar' or 'wildcharm' key.
720 * - hitting <ESC> twice means: abandon command line.
721 * - wildcard expansion is only done when the 'wildchar' key is really
722 * typed, not when it comes from a macro
723 */
724 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
725 {
726 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
727 {
728 /* if 'wildmode' contains "list" may still need to list */
729 if (xpc.xp_numfiles > 1
730 && !did_wild_list
731 && (wim_flags[wim_index] & WIM_LIST))
732 {
733 (void)showmatches(&xpc, FALSE);
734 redrawcmd();
735 did_wild_list = TRUE;
736 }
737 if (wim_flags[wim_index] & WIM_LONGEST)
738 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
739 else if (wim_flags[wim_index] & WIM_FULL)
740 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
741 else
742 res = OK; /* don't insert 'wildchar' now */
743 }
744 else /* typed p_wc first time */
745 {
746 wim_index = 0;
747 j = ccline.cmdpos;
748 /* if 'wildmode' first contains "longest", get longest
749 * common part */
750 if (wim_flags[0] & WIM_LONGEST)
751 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
752 else
753 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
754
755 /* if interrupted while completing, behave like it failed */
756 if (got_int)
757 {
758 (void)vpeekc(); /* remove <C-C> from input stream */
759 got_int = FALSE; /* don't abandon the command line */
760 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
761#ifdef FEAT_WILDMENU
762 xpc.xp_context = EXPAND_NOTHING;
763#endif
764 goto cmdline_changed;
765 }
766
767 /* when more than one match, and 'wildmode' first contains
768 * "list", or no change and 'wildmode' contains "longest,list",
769 * list all matches */
770 if (res == OK && xpc.xp_numfiles > 1)
771 {
772 /* a "longest" that didn't do anything is skipped (but not
773 * "list:longest") */
774 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
775 wim_index = 1;
776 if ((wim_flags[wim_index] & WIM_LIST)
777#ifdef FEAT_WILDMENU
778 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
779#endif
780 )
781 {
782 if (!(wim_flags[0] & WIM_LONGEST))
783 {
784#ifdef FEAT_WILDMENU
785 int p_wmnu_save = p_wmnu;
786 p_wmnu = 0;
787#endif
788 nextwild(&xpc, WILD_PREV, 0); /* remove match */
789#ifdef FEAT_WILDMENU
790 p_wmnu = p_wmnu_save;
791#endif
792 }
793#ifdef FEAT_WILDMENU
794 (void)showmatches(&xpc, p_wmnu
795 && ((wim_flags[wim_index] & WIM_LIST) == 0));
796#else
797 (void)showmatches(&xpc, FALSE);
798#endif
799 redrawcmd();
800 did_wild_list = TRUE;
801 if (wim_flags[wim_index] & WIM_LONGEST)
802 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
803 else if (wim_flags[wim_index] & WIM_FULL)
804 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
805 }
806 else
807 vim_beep();
808 }
809#ifdef FEAT_WILDMENU
810 else if (xpc.xp_numfiles == -1)
811 xpc.xp_context = EXPAND_NOTHING;
812#endif
813 }
814 if (wim_index < 3)
815 ++wim_index;
816 if (c == ESC)
817 gotesc = TRUE;
818 if (res == OK)
819 goto cmdline_changed;
820 }
821
822 gotesc = FALSE;
823
824 /* <S-Tab> goes to last match, in a clumsy way */
825 if (c == K_S_TAB && KeyTyped)
826 {
827 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
828 && nextwild(&xpc, WILD_PREV, 0) == OK
829 && nextwild(&xpc, WILD_PREV, 0) == OK)
830 goto cmdline_changed;
831 }
832
833 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
834 c = NL;
835
836 do_abbr = TRUE; /* default: check for abbreviation */
837
838 /*
839 * Big switch for a typed command line character.
840 */
841 switch (c)
842 {
843 case K_BS:
844 case Ctrl_H:
845 case K_DEL:
846 case K_KDEL:
847 case Ctrl_W:
848#ifdef FEAT_FKMAP
849 if (cmd_fkmap && c == K_BS)
850 c = K_DEL;
851#endif
852 if (c == K_KDEL)
853 c = K_DEL;
854
855 /*
856 * delete current character is the same as backspace on next
857 * character, except at end of line
858 */
859 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
860 ++ccline.cmdpos;
861#ifdef FEAT_MBYTE
862 if (has_mbyte && c == K_DEL)
863 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
864 ccline.cmdbuff + ccline.cmdpos);
865#endif
866 if (ccline.cmdpos > 0)
867 {
868 char_u *p;
869
870 j = ccline.cmdpos;
871 p = ccline.cmdbuff + j;
872#ifdef FEAT_MBYTE
873 if (has_mbyte)
874 {
875 p = mb_prevptr(ccline.cmdbuff, p);
876 if (c == Ctrl_W)
877 {
878 while (p > ccline.cmdbuff && vim_isspace(*p))
879 p = mb_prevptr(ccline.cmdbuff, p);
880 i = mb_get_class(p);
881 while (p > ccline.cmdbuff && mb_get_class(p) == i)
882 p = mb_prevptr(ccline.cmdbuff, p);
883 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000884 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886 }
887 else
888#endif
889 if (c == Ctrl_W)
890 {
891 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
892 --p;
893 i = vim_iswordc(p[-1]);
894 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
895 && vim_iswordc(p[-1]) == i)
896 --p;
897 }
898 else
899 --p;
900 ccline.cmdpos = (int)(p - ccline.cmdbuff);
901 ccline.cmdlen -= j - ccline.cmdpos;
902 i = ccline.cmdpos;
903 while (i < ccline.cmdlen)
904 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
905
906 /* Truncate at the end, required for multi-byte chars. */
907 ccline.cmdbuff[ccline.cmdlen] = NUL;
908 redrawcmd();
909 }
910 else if (ccline.cmdlen == 0 && c != Ctrl_W
911 && ccline.cmdprompt == NULL && indent == 0)
912 {
913 /* In ex and debug mode it doesn't make sense to return. */
914 if (exmode_active
915#ifdef FEAT_EVAL
916 || ccline.cmdfirstc == '>'
917#endif
918 )
919 goto cmdline_not_changed;
920
921 vim_free(ccline.cmdbuff); /* no commandline to return */
922 ccline.cmdbuff = NULL;
923 if (!cmd_silent)
924 {
925#ifdef FEAT_RIGHTLEFT
926 if (cmdmsg_rl)
927 msg_col = Columns;
928 else
929#endif
930 msg_col = 0;
931 msg_putchar(' '); /* delete ':' */
932 }
933 redraw_cmdline = TRUE;
934 goto returncmd; /* back to cmd mode */
935 }
936 goto cmdline_changed;
937
938 case K_INS:
939 case K_KINS:
940#ifdef FEAT_FKMAP
941 /* if Farsi mode set, we are in reverse insert mode -
942 Do not change the mode */
943 if (cmd_fkmap)
944 beep_flush();
945 else
946#endif
947 ccline.overstrike = !ccline.overstrike;
948#ifdef CURSOR_SHAPE
949 ui_cursor_shape(); /* may show different cursor shape */
950#endif
951 goto cmdline_not_changed;
952
953 case Ctrl_HAT:
954 if (map_to_exists_mode((char_u *)"", LANGMAP))
955 {
956 /* ":lmap" mappings exists, toggle use of mappings. */
957 State ^= LANGMAP;
958#ifdef USE_IM_CONTROL
959 im_set_active(FALSE); /* Disable input method */
960#endif
961 if (b_im_ptr != NULL)
962 {
963 if (State & LANGMAP)
964 *b_im_ptr = B_IMODE_LMAP;
965 else
966 *b_im_ptr = B_IMODE_NONE;
967 }
968 }
969#ifdef USE_IM_CONTROL
970 else
971 {
972 /* There are no ":lmap" mappings, toggle IM. When
973 * 'imdisable' is set don't try getting the status, it's
974 * always off. */
975 if ((p_imdisable && b_im_ptr != NULL)
976 ? *b_im_ptr == B_IMODE_IM : im_get_status())
977 {
978 im_set_active(FALSE); /* Disable input method */
979 if (b_im_ptr != NULL)
980 *b_im_ptr = B_IMODE_NONE;
981 }
982 else
983 {
984 im_set_active(TRUE); /* Enable input method */
985 if (b_im_ptr != NULL)
986 *b_im_ptr = B_IMODE_IM;
987 }
988 }
989#endif
990 if (b_im_ptr != NULL)
991 {
992 if (b_im_ptr == &curbuf->b_p_iminsert)
993 set_iminsert_global();
994 else
995 set_imsearch_global();
996 }
997#ifdef CURSOR_SHAPE
998 ui_cursor_shape(); /* may show different cursor shape */
999#endif
1000#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1001 /* Show/unshow value of 'keymap' in status lines later. */
1002 status_redraw_curbuf();
1003#endif
1004 goto cmdline_not_changed;
1005
1006/* case '@': only in very old vi */
1007 case Ctrl_U:
1008 /* delete all characters left of the cursor */
1009 j = ccline.cmdpos;
1010 ccline.cmdlen -= j;
1011 i = ccline.cmdpos = 0;
1012 while (i < ccline.cmdlen)
1013 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1014 /* Truncate at the end, required for multi-byte chars. */
1015 ccline.cmdbuff[ccline.cmdlen] = NUL;
1016 redrawcmd();
1017 goto cmdline_changed;
1018
1019#ifdef FEAT_CLIPBOARD
1020 case Ctrl_Y:
1021 /* Copy the modeless selection, if there is one. */
1022 if (clip_star.state != SELECT_CLEARED)
1023 {
1024 if (clip_star.state == SELECT_DONE)
1025 clip_copy_modeless_selection(TRUE);
1026 goto cmdline_not_changed;
1027 }
1028 break;
1029#endif
1030
1031 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1032 case Ctrl_C:
Bram Moolenaar7c626922005-02-07 22:01:03 +00001033 /* In exmode it doesn't make sense to return. Except when
1034 * ":normal" runs out of characters. */
1035 if (exmode_active
1036#ifdef FEAT_EX_EXTRA
1037 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1038#endif
1039 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 goto cmdline_not_changed;
1041
1042 gotesc = TRUE; /* will free ccline.cmdbuff after
1043 putting it in history */
1044 goto returncmd; /* back to cmd mode */
1045
1046 case Ctrl_R: /* insert register */
1047#ifdef USE_ON_FLY_SCROLL
1048 dont_scroll = TRUE; /* disallow scrolling here */
1049#endif
1050 putcmdline('"', TRUE);
1051 ++no_mapping;
1052 i = c = safe_vgetc(); /* CTRL-R <char> */
1053 if (i == Ctrl_O)
1054 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1055 if (i == Ctrl_R)
1056 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1057 --no_mapping;
1058#ifdef FEAT_EVAL
1059 /*
1060 * Insert the result of an expression.
1061 * Need to save the current command line, to be able to enter
1062 * a new one...
1063 */
1064 new_cmdpos = -1;
1065 if (c == '=')
1066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1068 {
1069 beep_flush();
1070 c = ESC;
1071 }
1072 else
1073 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001074 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001076 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 }
1079#endif
1080 if (c != ESC) /* use ESC to cancel inserting register */
1081 {
1082 cmdline_paste(c, i == Ctrl_R);
1083 KeyTyped = FALSE; /* Don't do p_wc completion. */
1084#ifdef FEAT_EVAL
1085 if (new_cmdpos >= 0)
1086 {
1087 /* set_cmdline_pos() was used */
1088 if (new_cmdpos > ccline.cmdlen)
1089 ccline.cmdpos = ccline.cmdlen;
1090 else
1091 ccline.cmdpos = new_cmdpos;
1092 }
1093#endif
1094 }
1095 redrawcmd();
1096 goto cmdline_changed;
1097
1098 case Ctrl_D:
1099 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1100 break; /* Use ^D as normal char instead */
1101
1102 redrawcmd();
1103 continue; /* don't do incremental search now */
1104
1105 case K_RIGHT:
1106 case K_S_RIGHT:
1107 case K_C_RIGHT:
1108 do
1109 {
1110 if (ccline.cmdpos >= ccline.cmdlen)
1111 break;
1112 i = cmdline_charsize(ccline.cmdpos);
1113 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1114 break;
1115 ccline.cmdspos += i;
1116#ifdef FEAT_MBYTE
1117 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001118 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 + ccline.cmdpos);
1120 else
1121#endif
1122 ++ccline.cmdpos;
1123 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001124 while ((c == K_S_RIGHT || c == K_C_RIGHT
1125 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1127#ifdef FEAT_MBYTE
1128 if (has_mbyte)
1129 set_cmdspos_cursor();
1130#endif
1131 goto cmdline_not_changed;
1132
1133 case K_LEFT:
1134 case K_S_LEFT:
1135 case K_C_LEFT:
1136 do
1137 {
1138 if (ccline.cmdpos == 0)
1139 break;
1140 --ccline.cmdpos;
1141#ifdef FEAT_MBYTE
1142 if (has_mbyte) /* move to first byte of char */
1143 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1144 ccline.cmdbuff + ccline.cmdpos);
1145#endif
1146 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1147 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001148 while ((c == K_S_LEFT || c == K_C_LEFT
1149 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1151#ifdef FEAT_MBYTE
1152 if (has_mbyte)
1153 set_cmdspos_cursor();
1154#endif
1155 goto cmdline_not_changed;
1156
1157 case K_IGNORE:
1158 goto cmdline_not_changed; /* Ignore mouse */
1159
1160#ifdef FEAT_MOUSE
1161 case K_MIDDLEDRAG:
1162 case K_MIDDLERELEASE:
1163 goto cmdline_not_changed; /* Ignore mouse */
1164
1165 case K_MIDDLEMOUSE:
1166# ifdef FEAT_GUI
1167 /* When GUI is active, also paste when 'mouse' is empty */
1168 if (!gui.in_use)
1169# endif
1170 if (!mouse_has(MOUSE_COMMAND))
1171 goto cmdline_not_changed; /* Ignore mouse */
1172#ifdef FEAT_CLIPBOARD
1173 if (clip_star.available)
1174 cmdline_paste('*', TRUE);
1175 else
1176#endif
1177 cmdline_paste(0, TRUE);
1178 redrawcmd();
1179 goto cmdline_changed;
1180
1181#ifdef FEAT_DND
1182 case K_DROP:
1183 cmdline_paste('~', TRUE);
1184 redrawcmd();
1185 goto cmdline_changed;
1186#endif
1187
1188 case K_LEFTDRAG:
1189 case K_LEFTRELEASE:
1190 case K_RIGHTDRAG:
1191 case K_RIGHTRELEASE:
1192 /* Ignore drag and release events when the button-down wasn't
1193 * seen before. */
1194 if (ignore_drag_release)
1195 goto cmdline_not_changed;
1196 /* FALLTHROUGH */
1197 case K_LEFTMOUSE:
1198 case K_RIGHTMOUSE:
1199 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1200 ignore_drag_release = TRUE;
1201 else
1202 ignore_drag_release = FALSE;
1203# ifdef FEAT_GUI
1204 /* When GUI is active, also move when 'mouse' is empty */
1205 if (!gui.in_use)
1206# endif
1207 if (!mouse_has(MOUSE_COMMAND))
1208 goto cmdline_not_changed; /* Ignore mouse */
1209# ifdef FEAT_CLIPBOARD
1210 if (mouse_row < cmdline_row && clip_star.available)
1211 {
1212 int button, is_click, is_drag;
1213
1214 /*
1215 * Handle modeless selection.
1216 */
1217 button = get_mouse_button(KEY2TERMCAP1(c),
1218 &is_click, &is_drag);
1219 if (mouse_model_popup() && button == MOUSE_LEFT
1220 && (mod_mask & MOD_MASK_SHIFT))
1221 {
1222 /* Translate shift-left to right button. */
1223 button = MOUSE_RIGHT;
1224 mod_mask &= ~MOD_MASK_SHIFT;
1225 }
1226 clip_modeless(button, is_click, is_drag);
1227 goto cmdline_not_changed;
1228 }
1229# endif
1230
1231 set_cmdspos();
1232 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1233 ++ccline.cmdpos)
1234 {
1235 i = cmdline_charsize(ccline.cmdpos);
1236 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1237 && mouse_col < ccline.cmdspos % Columns + i)
1238 break;
1239#ifdef FEAT_MBYTE
1240 if (has_mbyte)
1241 {
1242 /* Count ">" for double-wide char that doesn't fit. */
1243 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001244 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 + ccline.cmdpos) - 1;
1246 }
1247#endif
1248 ccline.cmdspos += i;
1249 }
1250 goto cmdline_not_changed;
1251
1252 /* Mouse scroll wheel: ignored here */
1253 case K_MOUSEDOWN:
1254 case K_MOUSEUP:
1255 /* Alternate buttons ignored here */
1256 case K_X1MOUSE:
1257 case K_X1DRAG:
1258 case K_X1RELEASE:
1259 case K_X2MOUSE:
1260 case K_X2DRAG:
1261 case K_X2RELEASE:
1262 goto cmdline_not_changed;
1263
1264#endif /* FEAT_MOUSE */
1265
1266#ifdef FEAT_GUI
1267 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1268 case K_LEFTRELEASE_NM:
1269 goto cmdline_not_changed;
1270
1271 case K_VER_SCROLLBAR:
1272 if (!msg_scrolled)
1273 {
1274 gui_do_scroll();
1275 redrawcmd();
1276 }
1277 goto cmdline_not_changed;
1278
1279 case K_HOR_SCROLLBAR:
1280 if (!msg_scrolled)
1281 {
1282 gui_do_horiz_scroll();
1283 redrawcmd();
1284 }
1285 goto cmdline_not_changed;
1286#endif
1287 case K_SELECT: /* end of Select mode mapping - ignore */
1288 goto cmdline_not_changed;
1289
1290 case Ctrl_B: /* begin of command line */
1291 case K_HOME:
1292 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 case K_S_HOME:
1294 case K_C_HOME:
1295 ccline.cmdpos = 0;
1296 set_cmdspos();
1297 goto cmdline_not_changed;
1298
1299 case Ctrl_E: /* end of command line */
1300 case K_END:
1301 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 case K_S_END:
1303 case K_C_END:
1304 ccline.cmdpos = ccline.cmdlen;
1305 set_cmdspos_cursor();
1306 goto cmdline_not_changed;
1307
1308 case Ctrl_A: /* all matches */
1309 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1310 break;
1311 goto cmdline_changed;
1312
1313 case Ctrl_L: /* longest common part */
1314 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1315 break;
1316 goto cmdline_changed;
1317
1318 case Ctrl_N: /* next match */
1319 case Ctrl_P: /* previous match */
1320 if (xpc.xp_numfiles > 0)
1321 {
1322 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1323 == FAIL)
1324 break;
1325 goto cmdline_changed;
1326 }
1327
1328#ifdef FEAT_CMDHIST
1329 case K_UP:
1330 case K_DOWN:
1331 case K_S_UP:
1332 case K_S_DOWN:
1333 case K_PAGEUP:
1334 case K_KPAGEUP:
1335 case K_PAGEDOWN:
1336 case K_KPAGEDOWN:
1337 if (hislen == 0 || firstc == NUL) /* no history */
1338 goto cmdline_not_changed;
1339
1340 i = hiscnt;
1341
1342 /* save current command string so it can be restored later */
1343 if (lookfor == NULL)
1344 {
1345 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1346 goto cmdline_not_changed;
1347 lookfor[ccline.cmdpos] = NUL;
1348 }
1349
1350 j = (int)STRLEN(lookfor);
1351 for (;;)
1352 {
1353 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001354 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001355 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
1357 if (hiscnt == hislen) /* first time */
1358 hiscnt = hisidx[histype];
1359 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1360 hiscnt = hislen - 1;
1361 else if (hiscnt != hisidx[histype] + 1)
1362 --hiscnt;
1363 else /* at top of list */
1364 {
1365 hiscnt = i;
1366 break;
1367 }
1368 }
1369 else /* one step forwards */
1370 {
1371 /* on last entry, clear the line */
1372 if (hiscnt == hisidx[histype])
1373 {
1374 hiscnt = hislen;
1375 break;
1376 }
1377
1378 /* not on a history line, nothing to do */
1379 if (hiscnt == hislen)
1380 break;
1381 if (hiscnt == hislen - 1) /* wrap around */
1382 hiscnt = 0;
1383 else
1384 ++hiscnt;
1385 }
1386 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1387 {
1388 hiscnt = i;
1389 break;
1390 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001391 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001392 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 || STRNCMP(history[histype][hiscnt].hisstr,
1394 lookfor, (size_t)j) == 0)
1395 break;
1396 }
1397
1398 if (hiscnt != i) /* jumped to other entry */
1399 {
1400 char_u *p;
1401 int len;
1402 int old_firstc;
1403
1404 vim_free(ccline.cmdbuff);
1405 if (hiscnt == hislen)
1406 p = lookfor; /* back to the old one */
1407 else
1408 p = history[histype][hiscnt].hisstr;
1409
1410 if (histype == HIST_SEARCH
1411 && p != lookfor
1412 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1413 {
1414 /* Correct for the separator character used when
1415 * adding the history entry vs the one used now.
1416 * First loop: count length.
1417 * Second loop: copy the characters. */
1418 for (i = 0; i <= 1; ++i)
1419 {
1420 len = 0;
1421 for (j = 0; p[j] != NUL; ++j)
1422 {
1423 /* Replace old sep with new sep, unless it is
1424 * escaped. */
1425 if (p[j] == old_firstc
1426 && (j == 0 || p[j - 1] != '\\'))
1427 {
1428 if (i > 0)
1429 ccline.cmdbuff[len] = firstc;
1430 }
1431 else
1432 {
1433 /* Escape new sep, unless it is already
1434 * escaped. */
1435 if (p[j] == firstc
1436 && (j == 0 || p[j - 1] != '\\'))
1437 {
1438 if (i > 0)
1439 ccline.cmdbuff[len] = '\\';
1440 ++len;
1441 }
1442 if (i > 0)
1443 ccline.cmdbuff[len] = p[j];
1444 }
1445 ++len;
1446 }
1447 if (i == 0)
1448 {
1449 alloc_cmdbuff(len);
1450 if (ccline.cmdbuff == NULL)
1451 goto returncmd;
1452 }
1453 }
1454 ccline.cmdbuff[len] = NUL;
1455 }
1456 else
1457 {
1458 alloc_cmdbuff((int)STRLEN(p));
1459 if (ccline.cmdbuff == NULL)
1460 goto returncmd;
1461 STRCPY(ccline.cmdbuff, p);
1462 }
1463
1464 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1465 redrawcmd();
1466 goto cmdline_changed;
1467 }
1468 beep_flush();
1469 goto cmdline_not_changed;
1470#endif
1471
1472 case Ctrl_V:
1473 case Ctrl_Q:
1474#ifdef FEAT_MOUSE
1475 ignore_drag_release = TRUE;
1476#endif
1477 putcmdline('^', TRUE);
1478 c = get_literal(); /* get next (two) character(s) */
1479 do_abbr = FALSE; /* don't do abbreviation now */
1480#ifdef FEAT_MBYTE
1481 /* may need to remove ^ when composing char was typed */
1482 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1483 {
1484 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1485 msg_putchar(' ');
1486 cursorcmd();
1487 }
1488#endif
1489 break;
1490
1491#ifdef FEAT_DIGRAPHS
1492 case Ctrl_K:
1493#ifdef FEAT_MOUSE
1494 ignore_drag_release = TRUE;
1495#endif
1496 putcmdline('?', TRUE);
1497#ifdef USE_ON_FLY_SCROLL
1498 dont_scroll = TRUE; /* disallow scrolling here */
1499#endif
1500 c = get_digraph(TRUE);
1501 if (c != NUL)
1502 break;
1503
1504 redrawcmd();
1505 goto cmdline_not_changed;
1506#endif /* FEAT_DIGRAPHS */
1507
1508#ifdef FEAT_RIGHTLEFT
1509 case Ctrl__: /* CTRL-_: switch language mode */
1510 if (!p_ari)
1511 break;
1512#ifdef FEAT_FKMAP
1513 if (p_altkeymap)
1514 {
1515 cmd_fkmap = !cmd_fkmap;
1516 if (cmd_fkmap) /* in Farsi always in Insert mode */
1517 ccline.overstrike = FALSE;
1518 }
1519 else /* Hebrew is default */
1520#endif
1521 cmd_hkmap = !cmd_hkmap;
1522 goto cmdline_not_changed;
1523#endif
1524
1525 default:
1526#ifdef UNIX
1527 if (c == intr_char)
1528 {
1529 gotesc = TRUE; /* will free ccline.cmdbuff after
1530 putting it in history */
1531 goto returncmd; /* back to Normal mode */
1532 }
1533#endif
1534 /*
1535 * Normal character with no special meaning. Just set mod_mask
1536 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1537 * the string <S-Space>. This should only happen after ^V.
1538 */
1539 if (!IS_SPECIAL(c))
1540 mod_mask = 0x0;
1541 break;
1542 }
1543 /*
1544 * End of switch on command line character.
1545 * We come here if we have a normal character.
1546 */
1547
1548 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1549#ifdef FEAT_MBYTE
1550 /* Add ABBR_OFF for characters above 0x100, this is
1551 * what check_abbr() expects. */
1552 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1553#endif
1554 c))
1555 goto cmdline_changed;
1556
1557 /*
1558 * put the character in the command line
1559 */
1560 if (IS_SPECIAL(c) || mod_mask != 0)
1561 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1562 else
1563 {
1564#ifdef FEAT_MBYTE
1565 if (has_mbyte)
1566 {
1567 j = (*mb_char2bytes)(c, IObuff);
1568 IObuff[j] = NUL; /* exclude composing chars */
1569 put_on_cmdline(IObuff, j, TRUE);
1570 }
1571 else
1572#endif
1573 {
1574 IObuff[0] = c;
1575 put_on_cmdline(IObuff, 1, TRUE);
1576 }
1577 }
1578 goto cmdline_changed;
1579
1580/*
1581 * This part implements incremental searches for "/" and "?"
1582 * Jump to cmdline_not_changed when a character has been read but the command
1583 * line did not change. Then we only search and redraw if something changed in
1584 * the past.
1585 * Jump to cmdline_changed when the command line did change.
1586 * (Sorry for the goto's, I know it is ugly).
1587 */
1588cmdline_not_changed:
1589#ifdef FEAT_SEARCH_EXTRA
1590 if (!incsearch_postponed)
1591 continue;
1592#endif
1593
1594cmdline_changed:
1595#ifdef FEAT_SEARCH_EXTRA
1596 /*
1597 * 'incsearch' highlighting.
1598 */
1599 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1600 {
1601 /* if there is a character waiting, search and redraw later */
1602 if (char_avail())
1603 {
1604 incsearch_postponed = TRUE;
1605 continue;
1606 }
1607 incsearch_postponed = FALSE;
1608 curwin->w_cursor = old_cursor; /* start at old position */
1609
1610 /* If there is no command line, don't do anything */
1611 if (ccline.cmdlen == 0)
1612 i = 0;
1613 else
1614 {
1615 cursor_off(); /* so the user knows we're busy */
1616 out_flush();
1617 ++emsg_off; /* So it doesn't beep if bad expr */
1618 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1619 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1620 --emsg_off;
1621 /* if interrupted while searching, behave like it failed */
1622 if (got_int)
1623 {
1624 (void)vpeekc(); /* remove <C-C> from input stream */
1625 got_int = FALSE; /* don't abandon the command line */
1626 i = 0;
1627 }
1628 else if (char_avail())
1629 /* cancelled searching because a char was typed */
1630 incsearch_postponed = TRUE;
1631 }
1632 if (i)
1633 highlight_match = TRUE; /* highlight position */
1634 else
1635 highlight_match = FALSE; /* remove highlight */
1636
1637 /* first restore the old curwin values, so the screen is
1638 * positioned in the same way as the actual search command */
1639 curwin->w_leftcol = old_leftcol;
1640 curwin->w_topline = old_topline;
1641# ifdef FEAT_DIFF
1642 curwin->w_topfill = old_topfill;
1643# endif
1644 curwin->w_botline = old_botline;
1645 changed_cline_bef_curs();
1646 update_topline();
1647
1648 if (i != 0)
1649 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001650 pos_T save_pos = curwin->w_cursor;
1651
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 /*
1653 * First move cursor to end of match, then to start. This
1654 * moves the whole match onto the screen when 'nowrap' is set.
1655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 curwin->w_cursor.lnum += search_match_lines;
1657 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001658 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1659 {
1660 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1661 coladvance((colnr_T)MAXCOL);
1662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 validate_cursor();
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001664 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 validate_cursor();
1667
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001668 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 update_screen(NOT_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001670 restore_cmdline(&save_ccline);
1671
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 msg_starthere();
1673 redrawcmdline();
1674 did_incsearch = TRUE;
1675 }
1676#else /* FEAT_SEARCH_EXTRA */
1677 ;
1678#endif
1679
1680#ifdef FEAT_RIGHTLEFT
1681 if (cmdmsg_rl
1682# ifdef FEAT_ARABIC
1683 || p_arshape
1684# endif
1685 )
1686 /* Always redraw the whole command line to fix shaping and
1687 * right-left typing. Not efficient, but it works. */
1688 redrawcmd();
1689#endif
1690 }
1691
1692returncmd:
1693
1694#ifdef FEAT_RIGHTLEFT
1695 cmdmsg_rl = FALSE;
1696#endif
1697
1698#ifdef FEAT_FKMAP
1699 cmd_fkmap = 0;
1700#endif
1701
1702 ExpandCleanup(&xpc);
1703
1704#ifdef FEAT_SEARCH_EXTRA
1705 if (did_incsearch)
1706 {
1707 curwin->w_cursor = old_cursor;
1708 curwin->w_curswant = old_curswant;
1709 curwin->w_leftcol = old_leftcol;
1710 curwin->w_topline = old_topline;
1711# ifdef FEAT_DIFF
1712 curwin->w_topfill = old_topfill;
1713# endif
1714 curwin->w_botline = old_botline;
1715 highlight_match = FALSE;
1716 validate_cursor(); /* needed for TAB */
1717 redraw_later(NOT_VALID);
1718 }
1719#endif
1720
1721 if (ccline.cmdbuff != NULL)
1722 {
1723 /*
1724 * Put line in history buffer (":" and "=" only when it was typed).
1725 */
1726#ifdef FEAT_CMDHIST
1727 if (ccline.cmdlen && firstc != NUL
1728 && (some_key_typed || histype == HIST_SEARCH))
1729 {
1730 add_to_history(histype, ccline.cmdbuff, TRUE,
1731 histype == HIST_SEARCH ? firstc : NUL);
1732 if (firstc == ':')
1733 {
1734 vim_free(new_last_cmdline);
1735 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1736 }
1737 }
1738#endif
1739
1740 if (gotesc) /* abandon command line */
1741 {
1742 vim_free(ccline.cmdbuff);
1743 ccline.cmdbuff = NULL;
1744 if (msg_scrolled == 0)
1745 compute_cmdrow();
1746 MSG("");
1747 redraw_cmdline = TRUE;
1748 }
1749 }
1750
1751 /*
1752 * If the screen was shifted up, redraw the whole screen (later).
1753 * If the line is too long, clear it, so ruler and shown command do
1754 * not get printed in the middle of it.
1755 */
1756 msg_check();
1757 msg_scroll = save_msg_scroll;
1758 redir_off = FALSE;
1759
1760 /* When the command line was typed, no need for a wait-return prompt. */
1761 if (some_key_typed)
1762 need_wait_return = FALSE;
1763
1764 State = save_State;
1765#ifdef USE_IM_CONTROL
1766 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1767 im_save_status(b_im_ptr);
1768 im_set_active(FALSE);
1769#endif
1770#ifdef FEAT_MOUSE
1771 setmouse();
1772#endif
1773#ifdef CURSOR_SHAPE
1774 ui_cursor_shape(); /* may show different cursor shape */
1775#endif
1776
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001777 {
1778 char_u *p = ccline.cmdbuff;
1779
1780 /* Make ccline empty, getcmdline() may try to use it. */
1781 ccline.cmdbuff = NULL;
1782 return p;
1783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784}
1785
1786#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1787/*
1788 * Get a command line with a prompt.
1789 * This is prepared to be called recursively from getcmdline() (e.g. by
1790 * f_input() when evaluating an expression from CTRL-R =).
1791 * Returns the command line in allocated memory, or NULL.
1792 */
1793 char_u *
1794getcmdline_prompt(firstc, prompt, attr)
1795 int firstc;
1796 char_u *prompt; /* command line prompt */
1797 int attr; /* attributes for prompt */
1798{
1799 char_u *s;
1800 struct cmdline_info save_ccline;
1801 int msg_col_save = msg_col;
1802
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001803 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 ccline.cmdprompt = prompt;
1805 ccline.cmdattr = attr;
1806 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001807 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 /* Restore msg_col, the prompt from input() may have changed it. */
1809 msg_col = msg_col_save;
1810
1811 return s;
1812}
1813#endif
1814
1815 static int
1816cmdline_charsize(idx)
1817 int idx;
1818{
1819#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1820 if (cmdline_star > 0) /* showing '*', always 1 position */
1821 return 1;
1822#endif
1823 return ptr2cells(ccline.cmdbuff + idx);
1824}
1825
1826/*
1827 * Compute the offset of the cursor on the command line for the prompt and
1828 * indent.
1829 */
1830 static void
1831set_cmdspos()
1832{
1833 if (ccline.cmdfirstc)
1834 ccline.cmdspos = 1 + ccline.cmdindent;
1835 else
1836 ccline.cmdspos = 0 + ccline.cmdindent;
1837}
1838
1839/*
1840 * Compute the screen position for the cursor on the command line.
1841 */
1842 static void
1843set_cmdspos_cursor()
1844{
1845 int i, m, c;
1846
1847 set_cmdspos();
1848 if (KeyTyped)
1849 m = Columns * Rows;
1850 else
1851 m = MAXCOL;
1852 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1853 {
1854 c = cmdline_charsize(i);
1855#ifdef FEAT_MBYTE
1856 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1857 if (has_mbyte)
1858 correct_cmdspos(i, c);
1859#endif
1860 /* If the cmdline doesn't fit, put cursor on last visible char. */
1861 if ((ccline.cmdspos += c) >= m)
1862 {
1863 ccline.cmdpos = i - 1;
1864 ccline.cmdspos -= c;
1865 break;
1866 }
1867#ifdef FEAT_MBYTE
1868 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001869 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#endif
1871 }
1872}
1873
1874#ifdef FEAT_MBYTE
1875/*
1876 * Check if the character at "idx", which is "cells" wide, is a multi-byte
1877 * character that doesn't fit, so that a ">" must be displayed.
1878 */
1879 static void
1880correct_cmdspos(idx, cells)
1881 int idx;
1882 int cells;
1883{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001884 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
1886 && ccline.cmdspos % Columns + cells > Columns)
1887 ccline.cmdspos++;
1888}
1889#endif
1890
1891/*
1892 * Get an Ex command line for the ":" command.
1893 */
1894/* ARGSUSED */
1895 char_u *
1896getexline(c, dummy, indent)
1897 int c; /* normally ':', NUL for ":append" */
1898 void *dummy; /* cookie not used */
1899 int indent; /* indent for inside conditionals */
1900{
1901 /* When executing a register, remove ':' that's in front of each line. */
1902 if (exec_from_reg && vpeekc() == ':')
1903 (void)vgetc();
1904 return getcmdline(c, 1L, indent);
1905}
1906
1907/*
1908 * Get an Ex command line for Ex mode.
1909 * In Ex mode we only use the OS supplied line editing features and no
1910 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001911 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 */
1913/* ARGSUSED */
1914 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001915getexmodeline(promptc, dummy, indent)
1916 int promptc; /* normally ':', NUL for ":append" and '?' for
1917 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 void *dummy; /* cookie not used */
1919 int indent; /* indent for inside conditionals */
1920{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001921 garray_T line_ga;
1922 char_u *pend;
1923 int startcol = 0;
1924 int c1;
1925 int escaped = FALSE; /* CTRL-V typed */
1926 int vcol = 0;
1927 char_u *p;
1928 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
1930 /* Switch cursor on now. This avoids that it happens after the "\n", which
1931 * confuses the system function that computes tabstops. */
1932 cursor_on();
1933
1934 /* always start in column 0; write a newline if necessary */
1935 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001936 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001938 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001940 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001941 if (p_prompt)
1942 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 while (indent-- > 0)
1944 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 }
1947
1948 ga_init2(&line_ga, 1, 30);
1949
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001950 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001951 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001952 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001953 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001954 while (indent >= 8)
1955 {
1956 ga_append(&line_ga, TAB);
1957 msg_puts((char_u *)" ");
1958 indent -= 8;
1959 }
1960 while (indent-- > 0)
1961 {
1962 ga_append(&line_ga, ' ');
1963 msg_putchar(' ');
1964 }
1965 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001966 ++no_mapping;
1967 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 /*
1970 * Get the line, one character at a time.
1971 */
1972 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001973 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 if (ga_grow(&line_ga, 40) == FAIL)
1976 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001977 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001979 /* Get one character at a time. Don't use inchar(), it can't handle
1980 * special characters. */
1981 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
1983 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001984 * Handle line editing.
1985 * Previously this was left to the system, putting the terminal in
1986 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001988 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001990 msg_putchar('\n');
1991 break;
1992 }
1993
1994 if (!escaped)
1995 {
1996 /* CR typed means "enter", which is NL */
1997 if (c1 == '\r')
1998 c1 = '\n';
1999
2000 if (c1 == BS || c1 == K_BS
2001 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002003 if (line_ga.ga_len > 0)
2004 {
2005 --line_ga.ga_len;
2006 goto redraw;
2007 }
2008 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002011 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002013 msg_col = startcol;
2014 msg_clr_eos();
2015 line_ga.ga_len = 0;
2016 continue;
2017 }
2018
2019 if (c1 == Ctrl_T)
2020 {
2021 p = (char_u *)line_ga.ga_data;
2022 p[line_ga.ga_len] = NUL;
2023 indent = get_indent_str(p, 8);
2024 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2025add_indent:
2026 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002028 char_u *s = skipwhite(p);
2029
2030 ga_grow(&line_ga, 1);
2031 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2032 *s = ' ';
2033 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002035redraw:
2036 /* redraw the line */
2037 msg_col = startcol;
2038 windgoto(msg_row, msg_col);
2039 vcol = 0;
2040 for (p = (char_u *)line_ga.ga_data;
2041 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002043 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002045 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002047 msg_putchar(' ');
2048 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002050 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002052 msg_outtrans_len(p, 1);
2053 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002056 msg_clr_eos();
2057 continue;
2058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002060 if (c1 == Ctrl_D)
2061 {
2062 /* Delete one shiftwidth. */
2063 p = (char_u *)line_ga.ga_data;
2064 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002066 if (prev_char == '^')
2067 ex_keep_indent = TRUE;
2068 indent = 0;
2069 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 }
2071 else
2072 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002073 p[line_ga.ga_len] = NUL;
2074 indent = get_indent_str(p, 8);
2075 --indent;
2076 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002078 while (get_indent_str(p, 8) > indent)
2079 {
2080 char_u *s = skipwhite(p);
2081
2082 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2083 --line_ga.ga_len;
2084 }
2085 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002087
2088 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2089 {
2090 escaped = TRUE;
2091 continue;
2092 }
2093
2094 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2095 if (IS_SPECIAL(c1))
2096 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002098
2099 if (IS_SPECIAL(c1))
2100 c1 = '?';
2101 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2102 prev_char = c1;
2103 if (c1 == '\n')
2104 msg_putchar('\n');
2105 else if (c1 == TAB)
2106 {
2107 /* Don't use chartabsize(), 'ts' can be different */
2108 do
2109 {
2110 msg_putchar(' ');
2111 } while (++vcol % 8);
2112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002115 msg_outtrans_len(
2116 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2117 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002119 ++line_ga.ga_len;
2120 escaped = FALSE;
2121
2122 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002123 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002124
2125 /* we are done when a NL is entered, but not when it comes after a
2126 * backslash */
2127 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2128 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002131 --pend;
2132 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002133 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 }
2135 }
2136
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002137 --no_mapping;
2138 --allow_keys;
2139
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 /* make following messages go to the next line */
2141 msg_didout = FALSE;
2142 msg_col = 0;
2143 if (msg_row < Rows - 1)
2144 ++msg_row;
2145 emsg_on_display = FALSE; /* don't want ui_delay() */
2146
2147 if (got_int)
2148 ga_clear(&line_ga);
2149
2150 return (char_u *)line_ga.ga_data;
2151}
2152
2153#ifdef CURSOR_SHAPE
2154/*
2155 * Return TRUE if ccline.overstrike is on.
2156 */
2157 int
2158cmdline_overstrike()
2159{
2160 return ccline.overstrike;
2161}
2162
2163/*
2164 * Return TRUE if the cursor is at the end of the cmdline.
2165 */
2166 int
2167cmdline_at_end()
2168{
2169 return (ccline.cmdpos >= ccline.cmdlen);
2170}
2171#endif
2172
Bram Moolenaar81695252004-12-29 20:58:21 +00002173#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE))) \
2174 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175/*
2176 * Return the virtual column number at the current cursor position.
2177 * This is used by the IM code to obtain the start of the preedit string.
2178 */
2179 colnr_T
2180cmdline_getvcol_cursor()
2181{
2182 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2183 return MAXCOL;
2184
2185# ifdef FEAT_MBYTE
2186 if (has_mbyte)
2187 {
2188 colnr_T col;
2189 int i = 0;
2190
2191 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002192 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 return col;
2195 }
2196 else
2197# endif
2198 return ccline.cmdpos;
2199}
2200#endif
2201
2202#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2203/*
2204 * If part of the command line is an IM preedit string, redraw it with
2205 * IM feedback attributes. The cursor position is restored after drawing.
2206 */
2207 static void
2208redrawcmd_preedit()
2209{
2210 if ((State & CMDLINE)
2211 && xic != NULL
2212 && im_get_status()
2213 && !p_imdisable
2214 && im_is_preediting())
2215 {
2216 int cmdpos = 0;
2217 int cmdspos;
2218 int old_row;
2219 int old_col;
2220 colnr_T col;
2221
2222 old_row = msg_row;
2223 old_col = msg_col;
2224 cmdspos = ((ccline.cmdfirstc) ? 1 : 0) + ccline.cmdindent;
2225
2226# ifdef FEAT_MBYTE
2227 if (has_mbyte)
2228 {
2229 for (col = 0; col < preedit_start_col
2230 && cmdpos < ccline.cmdlen; ++col)
2231 {
2232 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002233 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
2235 }
2236 else
2237# endif
2238 {
2239 cmdspos += preedit_start_col;
2240 cmdpos += preedit_start_col;
2241 }
2242
2243 msg_row = cmdline_row + (cmdspos / (int)Columns);
2244 msg_col = cmdspos % (int)Columns;
2245 if (msg_row >= Rows)
2246 msg_row = Rows - 1;
2247
2248 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2249 {
2250 int char_len;
2251 int char_attr;
2252
2253 char_attr = im_get_feedback_attr(col);
2254 if (char_attr < 0)
2255 break; /* end of preedit string */
2256
2257# ifdef FEAT_MBYTE
2258 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002259 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 else
2261# endif
2262 char_len = 1;
2263
2264 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2265 cmdpos += char_len;
2266 }
2267
2268 msg_row = old_row;
2269 msg_col = old_col;
2270 }
2271}
2272#endif /* FEAT_XIM && FEAT_GUI_GTK */
2273
2274/*
2275 * Allocate a new command line buffer.
2276 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2277 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2278 */
2279 static void
2280alloc_cmdbuff(len)
2281 int len;
2282{
2283 /*
2284 * give some extra space to avoid having to allocate all the time
2285 */
2286 if (len < 80)
2287 len = 100;
2288 else
2289 len += 20;
2290
2291 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2292 ccline.cmdbufflen = len;
2293}
2294
2295/*
2296 * Re-allocate the command line to length len + something extra.
2297 * return FAIL for failure, OK otherwise
2298 */
2299 static int
2300realloc_cmdbuff(len)
2301 int len;
2302{
2303 char_u *p;
2304
2305 p = ccline.cmdbuff;
2306 alloc_cmdbuff(len); /* will get some more */
2307 if (ccline.cmdbuff == NULL) /* out of memory */
2308 {
2309 ccline.cmdbuff = p; /* keep the old one */
2310 return FAIL;
2311 }
2312 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2313 vim_free(p);
2314 return OK;
2315}
2316
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002317#if defined(FEAT_ARABIC) || defined(PROTO)
2318static char_u *arshape_buf = NULL;
2319
2320# if defined(EXITFREE) || defined(PROTO)
2321 void
2322free_cmdline_buf()
2323{
2324 vim_free(arshape_buf);
2325}
2326# endif
2327#endif
2328
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329/*
2330 * Draw part of the cmdline at the current cursor position. But draw stars
2331 * when cmdline_star is TRUE.
2332 */
2333 static void
2334draw_cmdline(start, len)
2335 int start;
2336 int len;
2337{
2338#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2339 int i;
2340
2341 if (cmdline_star > 0)
2342 for (i = 0; i < len; ++i)
2343 {
2344 msg_putchar('*');
2345# ifdef FEAT_MBYTE
2346 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002347 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348# endif
2349 }
2350 else
2351#endif
2352#ifdef FEAT_ARABIC
2353 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 static int buflen = 0;
2356 char_u *p;
2357 int j;
2358 int newlen = 0;
2359 int mb_l;
2360 int pc, pc1;
2361 int prev_c = 0;
2362 int prev_c1 = 0;
2363 int u8c, u8c_c1, u8c_c2;
2364 int nc = 0;
2365 int dummy;
2366
2367 /*
2368 * Do arabic shaping into a temporary buffer. This is very
2369 * inefficient!
2370 */
2371 if (len * 2 > buflen)
2372 {
2373 /* Re-allocate the buffer. We keep it around to avoid a lot of
2374 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002375 vim_free(arshape_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 buflen = len * 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002377 arshape_buf = alloc(buflen);
2378 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 return; /* out of memory */
2380 }
2381
2382 for (j = start; j < start + len; j += mb_l)
2383 {
2384 p = ccline.cmdbuff + j;
2385 u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002386 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (ARABIC_CHAR(u8c))
2388 {
2389 /* Do Arabic shaping. */
2390 if (cmdmsg_rl)
2391 {
2392 /* displaying from right to left */
2393 pc = prev_c;
2394 pc1 = prev_c1;
2395 prev_c1 = u8c_c1;
2396 if (j + mb_l >= start + len)
2397 nc = NUL;
2398 else
2399 nc = utf_ptr2char(p + mb_l);
2400 }
2401 else
2402 {
2403 /* displaying from left to right */
2404 if (j + mb_l >= start + len)
2405 pc = NUL;
2406 else
2407 pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
2408 start + len - j - mb_l);
2409 nc = prev_c;
2410 }
2411 prev_c = u8c;
2412
2413 u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
2414
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002415 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (u8c_c1 != 0)
2417 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002418 newlen += (*mb_char2bytes)(u8c_c1, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 if (u8c_c2 != 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002420 newlen += (*mb_char2bytes)(u8c_c2,
2421 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423 }
2424 else
2425 {
2426 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002427 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 newlen += mb_l;
2429 }
2430 }
2431
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002432 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 }
2434 else
2435#endif
2436 msg_outtrans_len(ccline.cmdbuff + start, len);
2437}
2438
2439/*
2440 * Put a character on the command line. Shifts the following text to the
2441 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2442 * "c" must be printable (fit in one display cell)!
2443 */
2444 void
2445putcmdline(c, shift)
2446 int c;
2447 int shift;
2448{
2449 if (cmd_silent)
2450 return;
2451 msg_no_more = TRUE;
2452 msg_putchar(c);
2453 if (shift)
2454 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2455 msg_no_more = FALSE;
2456 cursorcmd();
2457}
2458
2459/*
2460 * Undo a putcmdline(c, FALSE).
2461 */
2462 void
2463unputcmdline()
2464{
2465 if (cmd_silent)
2466 return;
2467 msg_no_more = TRUE;
2468 if (ccline.cmdlen == ccline.cmdpos)
2469 msg_putchar(' ');
2470 else
2471 draw_cmdline(ccline.cmdpos, 1);
2472 msg_no_more = FALSE;
2473 cursorcmd();
2474}
2475
2476/*
2477 * Put the given string, of the given length, onto the command line.
2478 * If len is -1, then STRLEN() is used to calculate the length.
2479 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2480 * part will be redrawn, otherwise it will not. If this function is called
2481 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2482 * called afterwards.
2483 */
2484 int
2485put_on_cmdline(str, len, redraw)
2486 char_u *str;
2487 int len;
2488 int redraw;
2489{
2490 int retval;
2491 int i;
2492 int m;
2493 int c;
2494
2495 if (len < 0)
2496 len = (int)STRLEN(str);
2497
2498 /* Check if ccline.cmdbuff needs to be longer */
2499 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2500 retval = realloc_cmdbuff(ccline.cmdlen + len);
2501 else
2502 retval = OK;
2503 if (retval == OK)
2504 {
2505 if (!ccline.overstrike)
2506 {
2507 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2508 ccline.cmdbuff + ccline.cmdpos,
2509 (size_t)(ccline.cmdlen - ccline.cmdpos));
2510 ccline.cmdlen += len;
2511 }
2512 else
2513 {
2514#ifdef FEAT_MBYTE
2515 if (has_mbyte)
2516 {
2517 /* Count nr of characters in the new string. */
2518 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002519 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 ++m;
2521 /* Count nr of bytes in cmdline that are overwritten by these
2522 * characters. */
2523 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002524 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 --m;
2526 if (i < ccline.cmdlen)
2527 {
2528 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2529 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2530 ccline.cmdlen += ccline.cmdpos + len - i;
2531 }
2532 else
2533 ccline.cmdlen = ccline.cmdpos + len;
2534 }
2535 else
2536#endif
2537 if (ccline.cmdpos + len > ccline.cmdlen)
2538 ccline.cmdlen = ccline.cmdpos + len;
2539 }
2540 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2541 ccline.cmdbuff[ccline.cmdlen] = NUL;
2542
2543#ifdef FEAT_MBYTE
2544 if (enc_utf8)
2545 {
2546 /* When the inserted text starts with a composing character,
2547 * backup to the character before it. There could be two of them.
2548 */
2549 i = 0;
2550 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2551 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2552 {
2553 i = (*mb_head_off)(ccline.cmdbuff,
2554 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2555 ccline.cmdpos -= i;
2556 len += i;
2557 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2558 }
2559# ifdef FEAT_ARABIC
2560 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2561 {
2562 /* Check the previous character for Arabic combining pair. */
2563 i = (*mb_head_off)(ccline.cmdbuff,
2564 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2565 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2566 + ccline.cmdpos - i), c))
2567 {
2568 ccline.cmdpos -= i;
2569 len += i;
2570 }
2571 else
2572 i = 0;
2573 }
2574# endif
2575 if (i != 0)
2576 {
2577 /* Also backup the cursor position. */
2578 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2579 ccline.cmdspos -= i;
2580 msg_col -= i;
2581 if (msg_col < 0)
2582 {
2583 msg_col += Columns;
2584 --msg_row;
2585 }
2586 }
2587 }
2588#endif
2589
2590 if (redraw && !cmd_silent)
2591 {
2592 msg_no_more = TRUE;
2593 i = cmdline_row;
2594 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2595 /* Avoid clearing the rest of the line too often. */
2596 if (cmdline_row != i || ccline.overstrike)
2597 msg_clr_eos();
2598 msg_no_more = FALSE;
2599 }
2600#ifdef FEAT_FKMAP
2601 /*
2602 * If we are in Farsi command mode, the character input must be in
2603 * Insert mode. So do not advance the cmdpos.
2604 */
2605 if (!cmd_fkmap)
2606#endif
2607 {
2608 if (KeyTyped)
2609 m = Columns * Rows;
2610 else
2611 m = MAXCOL;
2612 for (i = 0; i < len; ++i)
2613 {
2614 c = cmdline_charsize(ccline.cmdpos);
2615#ifdef FEAT_MBYTE
2616 /* count ">" for a double-wide char that doesn't fit. */
2617 if (has_mbyte)
2618 correct_cmdspos(ccline.cmdpos, c);
2619#endif
2620 /* Stop cursor at the end of the screen */
2621 if (ccline.cmdspos + c >= m)
2622 break;
2623 ccline.cmdspos += c;
2624#ifdef FEAT_MBYTE
2625 if (has_mbyte)
2626 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002627 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (c > len - i - 1)
2629 c = len - i - 1;
2630 ccline.cmdpos += c;
2631 i += c;
2632 }
2633#endif
2634 ++ccline.cmdpos;
2635 }
2636 }
2637 }
2638 if (redraw)
2639 msg_check();
2640 return retval;
2641}
2642
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002643static struct cmdline_info prev_ccline;
2644static int prev_ccline_used = FALSE;
2645
2646/*
2647 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2648 * and overwrite it. But get_cmdline_str() may need it, thus make it
2649 * available globally in prev_ccline.
2650 */
2651 static void
2652save_cmdline(ccp)
2653 struct cmdline_info *ccp;
2654{
2655 if (!prev_ccline_used)
2656 {
2657 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2658 prev_ccline_used = TRUE;
2659 }
2660 *ccp = prev_ccline;
2661 prev_ccline = ccline;
2662 ccline.cmdbuff = NULL;
2663 ccline.cmdprompt = NULL;
2664}
2665
2666/*
2667 * Resture ccline after it has been saved with save_cmdline().
2668 */
2669 static void
2670restore_cmdline(ccp)
2671 struct cmdline_info *ccp;
2672{
2673 ccline = prev_ccline;
2674 prev_ccline = *ccp;
2675}
2676
Bram Moolenaar8299df92004-07-10 09:47:34 +00002677/*
2678 * paste a yank register into the command line.
2679 * used by CTRL-R command in command-line mode
2680 * insert_reg() can't be used here, because special characters from the
2681 * register contents will be interpreted as commands.
2682 *
2683 * return FAIL for failure, OK otherwise
2684 */
2685 static int
2686cmdline_paste(regname, literally)
2687 int regname;
2688 int literally; /* Insert text literally instead of "as typed" */
2689{
2690 long i;
2691 char_u *arg;
2692 int allocated;
2693 struct cmdline_info save_ccline;
2694
2695 /* check for valid regname; also accept special characters for CTRL-R in
2696 * the command line */
2697 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2698 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2699 return FAIL;
2700
2701 /* A register containing CTRL-R can cause an endless loop. Allow using
2702 * CTRL-C to break the loop. */
2703 line_breakcheck();
2704 if (got_int)
2705 return FAIL;
2706
2707#ifdef FEAT_CLIPBOARD
2708 regname = may_get_selection(regname);
2709#endif
2710
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002711 /* Need to save and restore ccline. */
2712 save_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002713 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002714 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002715
2716 if (i)
2717 {
2718 /* Got the value of a special register in "arg". */
2719 if (arg == NULL)
2720 return FAIL;
2721 cmdline_paste_str(arg, literally);
2722 if (allocated)
2723 vim_free(arg);
2724 return OK;
2725 }
2726
2727 return cmdline_paste_reg(regname, literally);
2728}
2729
2730/*
2731 * Put a string on the command line.
2732 * When "literally" is TRUE, insert literally.
2733 * When "literally" is FALSE, insert as typed, but don't leave the command
2734 * line.
2735 */
2736 void
2737cmdline_paste_str(s, literally)
2738 char_u *s;
2739 int literally;
2740{
2741 int c, cv;
2742
2743 if (literally)
2744 put_on_cmdline(s, -1, TRUE);
2745 else
2746 while (*s != NUL)
2747 {
2748 cv = *s;
2749 if (cv == Ctrl_V && s[1])
2750 ++s;
2751#ifdef FEAT_MBYTE
2752 if (has_mbyte)
2753 {
2754 c = mb_ptr2char(s);
2755 s += mb_char2len(c);
2756 }
2757 else
2758#endif
2759 c = *s++;
2760 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2761#ifdef UNIX
2762 || c == intr_char
2763#endif
2764 || (c == Ctrl_BSL && *s == Ctrl_N))
2765 stuffcharReadbuff(Ctrl_V);
2766 stuffcharReadbuff(c);
2767 }
2768}
2769
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770#ifdef FEAT_WILDMENU
2771/*
2772 * Delete characters on the command line, from "from" to the current
2773 * position.
2774 */
2775 static void
2776cmdline_del(from)
2777 int from;
2778{
2779 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2780 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2781 ccline.cmdlen -= ccline.cmdpos - from;
2782 ccline.cmdpos = from;
2783}
2784#endif
2785
2786/*
2787 * this fuction is called when the screen size changes and with incremental
2788 * search
2789 */
2790 void
2791redrawcmdline()
2792{
2793 if (cmd_silent)
2794 return;
2795 need_wait_return = FALSE;
2796 compute_cmdrow();
2797 redrawcmd();
2798 cursorcmd();
2799}
2800
2801 static void
2802redrawcmdprompt()
2803{
2804 int i;
2805
2806 if (cmd_silent)
2807 return;
2808 if (ccline.cmdfirstc)
2809 msg_putchar(ccline.cmdfirstc);
2810 if (ccline.cmdprompt != NULL)
2811 {
2812 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2813 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2814 /* do the reverse of set_cmdspos() */
2815 if (ccline.cmdfirstc)
2816 --ccline.cmdindent;
2817 }
2818 else
2819 for (i = ccline.cmdindent; i > 0; --i)
2820 msg_putchar(' ');
2821}
2822
2823/*
2824 * Redraw what is currently on the command line.
2825 */
2826 void
2827redrawcmd()
2828{
2829 if (cmd_silent)
2830 return;
2831
2832 msg_start();
2833 redrawcmdprompt();
2834
2835 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2836 msg_no_more = TRUE;
2837 draw_cmdline(0, ccline.cmdlen);
2838 msg_clr_eos();
2839 msg_no_more = FALSE;
2840
2841 set_cmdspos_cursor();
2842
2843 /*
2844 * An emsg() before may have set msg_scroll. This is used in normal mode,
2845 * in cmdline mode we can reset them now.
2846 */
2847 msg_scroll = FALSE; /* next message overwrites cmdline */
2848
2849 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2850 * in cmdline mode */
2851 skip_redraw = FALSE;
2852}
2853
2854 void
2855compute_cmdrow()
2856{
2857 if (exmode_active || msg_scrolled)
2858 cmdline_row = Rows - 1;
2859 else
2860 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2861 + W_STATUS_HEIGHT(lastwin);
2862}
2863
2864 static void
2865cursorcmd()
2866{
2867 if (cmd_silent)
2868 return;
2869
2870#ifdef FEAT_RIGHTLEFT
2871 if (cmdmsg_rl)
2872 {
2873 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2874 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2875 if (msg_row <= 0)
2876 msg_row = Rows - 1;
2877 }
2878 else
2879#endif
2880 {
2881 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
2882 msg_col = ccline.cmdspos % (int)Columns;
2883 if (msg_row >= Rows)
2884 msg_row = Rows - 1;
2885 }
2886
2887 windgoto(msg_row, msg_col);
2888#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2889 redrawcmd_preedit();
2890#endif
2891#ifdef MCH_CURSOR_SHAPE
2892 mch_update_cursor();
2893#endif
2894}
2895
2896 void
2897gotocmdline(clr)
2898 int clr;
2899{
2900 msg_start();
2901#ifdef FEAT_RIGHTLEFT
2902 if (cmdmsg_rl)
2903 msg_col = Columns - 1;
2904 else
2905#endif
2906 msg_col = 0; /* always start in column 0 */
2907 if (clr) /* clear the bottom line(s) */
2908 msg_clr_eos(); /* will reset clear_cmdline */
2909 windgoto(cmdline_row, 0);
2910}
2911
2912/*
2913 * Check the word in front of the cursor for an abbreviation.
2914 * Called when the non-id character "c" has been entered.
2915 * When an abbreviation is recognized it is removed from the text with
2916 * backspaces and the replacement string is inserted, followed by "c".
2917 */
2918 static int
2919ccheck_abbr(c)
2920 int c;
2921{
2922 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
2923 return FALSE;
2924
2925 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
2926}
2927
2928/*
2929 * Return FAIL if this is not an appropriate context in which to do
2930 * completion of anything, return OK if it is (even if there are no matches).
2931 * For the caller, this means that the character is just passed through like a
2932 * normal character (instead of being expanded). This allows :s/^I^D etc.
2933 */
2934 static int
2935nextwild(xp, type, options)
2936 expand_T *xp;
2937 int type;
2938 int options; /* extra options for ExpandOne() */
2939{
2940 int i, j;
2941 char_u *p1;
2942 char_u *p2;
2943 int oldlen;
2944 int difflen;
2945 int v;
2946
2947 if (xp->xp_numfiles == -1)
2948 {
2949 set_expand_context(xp);
2950 cmd_showtail = expand_showtail(xp);
2951 }
2952
2953 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2954 {
2955 beep_flush();
2956 return OK; /* Something illegal on command line */
2957 }
2958 if (xp->xp_context == EXPAND_NOTHING)
2959 {
2960 /* Caller can use the character as a normal char instead */
2961 return FAIL;
2962 }
2963
2964 MSG_PUTS("..."); /* show that we are busy */
2965 out_flush();
2966
2967 i = (int)(xp->xp_pattern - ccline.cmdbuff);
2968 oldlen = ccline.cmdpos - i;
2969
2970 if (type == WILD_NEXT || type == WILD_PREV)
2971 {
2972 /*
2973 * Get next/previous match for a previous expanded pattern.
2974 */
2975 p2 = ExpandOne(xp, NULL, NULL, 0, type);
2976 }
2977 else
2978 {
2979 /*
2980 * Translate string into pattern and expand it.
2981 */
2982 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
2983 p2 = NULL;
2984 else
2985 {
2986 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
2987 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
2988 |options, type);
2989 vim_free(p1);
2990 /* longest match: make sure it is not shorter (happens with :help */
2991 if (p2 != NULL && type == WILD_LONGEST)
2992 {
2993 for (j = 0; j < oldlen; ++j)
2994 if (ccline.cmdbuff[i + j] == '*'
2995 || ccline.cmdbuff[i + j] == '?')
2996 break;
2997 if ((int)STRLEN(p2) < j)
2998 {
2999 vim_free(p2);
3000 p2 = NULL;
3001 }
3002 }
3003 }
3004 }
3005
3006 if (p2 != NULL && !got_int)
3007 {
3008 difflen = (int)STRLEN(p2) - oldlen;
3009 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3010 {
3011 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3012 xp->xp_pattern = ccline.cmdbuff + i;
3013 }
3014 else
3015 v = OK;
3016 if (v == OK)
3017 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003018 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3019 &ccline.cmdbuff[ccline.cmdpos],
3020 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3021 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 ccline.cmdlen += difflen;
3023 ccline.cmdpos += difflen;
3024 }
3025 }
3026 vim_free(p2);
3027
3028 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003029 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
3031 /* When expanding a ":map" command and no matches are found, assume that
3032 * the key is supposed to be inserted literally */
3033 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3034 return FAIL;
3035
3036 if (xp->xp_numfiles <= 0 && p2 == NULL)
3037 beep_flush();
3038 else if (xp->xp_numfiles == 1)
3039 /* free expanded pattern */
3040 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3041
3042 return OK;
3043}
3044
3045/*
3046 * Do wildcard expansion on the string 'str'.
3047 * Chars that should not be expanded must be preceded with a backslash.
3048 * Return a pointer to alloced memory containing the new string.
3049 * Return NULL for failure.
3050 *
3051 * Results are cached in xp->xp_files and xp->xp_numfiles.
3052 *
3053 * mode = WILD_FREE: just free previously expanded matches
3054 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3055 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3056 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3057 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3058 * mode = WILD_ALL: return all matches concatenated
3059 * mode = WILD_LONGEST: return longest matched part
3060 *
3061 * options = WILD_LIST_NOTFOUND: list entries without a match
3062 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3063 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3064 * options = WILD_NO_BEEP: Don't beep for multiple matches
3065 * options = WILD_ADD_SLASH: add a slash after directory names
3066 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3067 * options = WILD_SILENT: don't print warning messages
3068 * options = WILD_ESCAPE: put backslash before special chars
3069 *
3070 * The variables xp->xp_context and xp->xp_backslash must have been set!
3071 */
3072 char_u *
3073ExpandOne(xp, str, orig, options, mode)
3074 expand_T *xp;
3075 char_u *str;
3076 char_u *orig; /* allocated copy of original of expanded string */
3077 int options;
3078 int mode;
3079{
3080 char_u *ss = NULL;
3081 static int findex;
3082 static char_u *orig_save = NULL; /* kept value of orig */
3083 int i;
3084 long_u len;
3085 int non_suf_match; /* number without matching suffix */
3086
3087 /*
3088 * first handle the case of using an old match
3089 */
3090 if (mode == WILD_NEXT || mode == WILD_PREV)
3091 {
3092 if (xp->xp_numfiles > 0)
3093 {
3094 if (mode == WILD_PREV)
3095 {
3096 if (findex == -1)
3097 findex = xp->xp_numfiles;
3098 --findex;
3099 }
3100 else /* mode == WILD_NEXT */
3101 ++findex;
3102
3103 /*
3104 * When wrapping around, return the original string, set findex to
3105 * -1.
3106 */
3107 if (findex < 0)
3108 {
3109 if (orig_save == NULL)
3110 findex = xp->xp_numfiles - 1;
3111 else
3112 findex = -1;
3113 }
3114 if (findex >= xp->xp_numfiles)
3115 {
3116 if (orig_save == NULL)
3117 findex = 0;
3118 else
3119 findex = -1;
3120 }
3121#ifdef FEAT_WILDMENU
3122 if (p_wmnu)
3123 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3124 findex, cmd_showtail);
3125#endif
3126 if (findex == -1)
3127 return vim_strsave(orig_save);
3128 return vim_strsave(xp->xp_files[findex]);
3129 }
3130 else
3131 return NULL;
3132 }
3133
3134/* free old names */
3135 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3136 {
3137 FreeWild(xp->xp_numfiles, xp->xp_files);
3138 xp->xp_numfiles = -1;
3139 vim_free(orig_save);
3140 orig_save = NULL;
3141 }
3142 findex = 0;
3143
3144 if (mode == WILD_FREE) /* only release file name */
3145 return NULL;
3146
3147 if (xp->xp_numfiles == -1)
3148 {
3149 vim_free(orig_save);
3150 orig_save = orig;
3151
3152 /*
3153 * Do the expansion.
3154 */
3155 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3156 options) == FAIL)
3157 {
3158#ifdef FNAME_ILLEGAL
3159 /* Illegal file name has been silently skipped. But when there
3160 * are wildcards, the real problem is that there was no match,
3161 * causing the pattern to be added, which has illegal characters.
3162 */
3163 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3164 EMSG2(_(e_nomatch2), str);
3165#endif
3166 }
3167 else if (xp->xp_numfiles == 0)
3168 {
3169 if (!(options & WILD_SILENT))
3170 EMSG2(_(e_nomatch2), str);
3171 }
3172 else
3173 {
3174 /* Escape the matches for use on the command line. */
3175 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3176
3177 /*
3178 * Check for matching suffixes in file names.
3179 */
3180 if (mode != WILD_ALL && mode != WILD_LONGEST)
3181 {
3182 if (xp->xp_numfiles)
3183 non_suf_match = xp->xp_numfiles;
3184 else
3185 non_suf_match = 1;
3186 if ((xp->xp_context == EXPAND_FILES
3187 || xp->xp_context == EXPAND_DIRECTORIES)
3188 && xp->xp_numfiles > 1)
3189 {
3190 /*
3191 * More than one match; check suffix.
3192 * The files will have been sorted on matching suffix in
3193 * expand_wildcards, only need to check the first two.
3194 */
3195 non_suf_match = 0;
3196 for (i = 0; i < 2; ++i)
3197 if (match_suffix(xp->xp_files[i]))
3198 ++non_suf_match;
3199 }
3200 if (non_suf_match != 1)
3201 {
3202 /* Can we ever get here unless it's while expanding
3203 * interactively? If not, we can get rid of this all
3204 * together. Don't really want to wait for this message
3205 * (and possibly have to hit return to continue!).
3206 */
3207 if (!(options & WILD_SILENT))
3208 EMSG(_(e_toomany));
3209 else if (!(options & WILD_NO_BEEP))
3210 beep_flush();
3211 }
3212 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3213 ss = vim_strsave(xp->xp_files[0]);
3214 }
3215 }
3216 }
3217
3218 /* Find longest common part */
3219 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3220 {
3221 for (len = 0; xp->xp_files[0][len]; ++len)
3222 {
3223 for (i = 0; i < xp->xp_numfiles; ++i)
3224 {
3225#ifdef CASE_INSENSITIVE_FILENAME
3226 if (xp->xp_context == EXPAND_DIRECTORIES
3227 || xp->xp_context == EXPAND_FILES
3228 || xp->xp_context == EXPAND_BUFFERS)
3229 {
3230 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3231 TOLOWER_LOC(xp->xp_files[0][len]))
3232 break;
3233 }
3234 else
3235#endif
3236 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3237 break;
3238 }
3239 if (i < xp->xp_numfiles)
3240 {
3241 if (!(options & WILD_NO_BEEP))
3242 vim_beep();
3243 break;
3244 }
3245 }
3246 ss = alloc((unsigned)len + 1);
3247 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003248 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 findex = -1; /* next p_wc gets first one */
3250 }
3251
3252 /* Concatenate all matching names */
3253 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3254 {
3255 len = 0;
3256 for (i = 0; i < xp->xp_numfiles; ++i)
3257 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3258 ss = lalloc(len, TRUE);
3259 if (ss != NULL)
3260 {
3261 *ss = NUL;
3262 for (i = 0; i < xp->xp_numfiles; ++i)
3263 {
3264 STRCAT(ss, xp->xp_files[i]);
3265 if (i != xp->xp_numfiles - 1)
3266 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3267 }
3268 }
3269 }
3270
3271 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3272 ExpandCleanup(xp);
3273
3274 return ss;
3275}
3276
3277/*
3278 * Prepare an expand structure for use.
3279 */
3280 void
3281ExpandInit(xp)
3282 expand_T *xp;
3283{
3284 xp->xp_backslash = XP_BS_NONE;
3285 xp->xp_numfiles = -1;
3286 xp->xp_files = NULL;
3287}
3288
3289/*
3290 * Cleanup an expand structure after use.
3291 */
3292 void
3293ExpandCleanup(xp)
3294 expand_T *xp;
3295{
3296 if (xp->xp_numfiles >= 0)
3297 {
3298 FreeWild(xp->xp_numfiles, xp->xp_files);
3299 xp->xp_numfiles = -1;
3300 }
3301}
3302
3303 void
3304ExpandEscape(xp, str, numfiles, files, options)
3305 expand_T *xp;
3306 char_u *str;
3307 int numfiles;
3308 char_u **files;
3309 int options;
3310{
3311 int i;
3312 char_u *p;
3313
3314 /*
3315 * May change home directory back to "~"
3316 */
3317 if (options & WILD_HOME_REPLACE)
3318 tilde_replace(str, numfiles, files);
3319
3320 if (options & WILD_ESCAPE)
3321 {
3322 if (xp->xp_context == EXPAND_FILES
3323 || xp->xp_context == EXPAND_BUFFERS
3324 || xp->xp_context == EXPAND_DIRECTORIES)
3325 {
3326 /*
3327 * Insert a backslash into a file name before a space, \, %, #
3328 * and wildmatch characters, except '~'.
3329 */
3330 for (i = 0; i < numfiles; ++i)
3331 {
3332 /* for ":set path=" we need to escape spaces twice */
3333 if (xp->xp_backslash == XP_BS_THREE)
3334 {
3335 p = vim_strsave_escaped(files[i], (char_u *)" ");
3336 if (p != NULL)
3337 {
3338 vim_free(files[i]);
3339 files[i] = p;
3340#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3341 p = vim_strsave_escaped(files[i], (char_u *)" ");
3342 if (p != NULL)
3343 {
3344 vim_free(files[i]);
3345 files[i] = p;
3346 }
3347#endif
3348 }
3349 }
3350#ifdef BACKSLASH_IN_FILENAME
3351 {
3352 char_u buf[20];
3353 int j = 0;
3354
3355 /* Don't escape '[' and '{' if they are in 'isfname'. */
3356 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3357 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3358 buf[j++] = *p;
3359 buf[j] = NUL;
3360 p = vim_strsave_escaped(files[i], buf);
3361 }
3362#else
3363 p = vim_strsave_escaped(files[i], PATH_ESC_CHARS);
3364#endif
3365 if (p != NULL)
3366 {
3367 vim_free(files[i]);
3368 files[i] = p;
3369 }
3370
3371 /* If 'str' starts with "\~", replace "~" at start of
3372 * files[i] with "\~". */
3373 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003374 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003377
3378 /* If the first file starts with a '+' escape it. Otherwise it
3379 * could be seen as "+cmd". */
3380 if (*files[0] == '+')
3381 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383 else if (xp->xp_context == EXPAND_TAGS)
3384 {
3385 /*
3386 * Insert a backslash before characters in a tag name that
3387 * would terminate the ":tag" command.
3388 */
3389 for (i = 0; i < numfiles; ++i)
3390 {
3391 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3392 if (p != NULL)
3393 {
3394 vim_free(files[i]);
3395 files[i] = p;
3396 }
3397 }
3398 }
3399 }
3400}
3401
3402/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003403 * Put a backslash before the file name in "pp", which is in allocated memory.
3404 */
3405 static void
3406escape_fname(pp)
3407 char_u **pp;
3408{
3409 char_u *p;
3410
3411 p = alloc((unsigned)(STRLEN(*pp) + 2));
3412 if (p != NULL)
3413 {
3414 p[0] = '\\';
3415 STRCPY(p + 1, *pp);
3416 vim_free(*pp);
3417 *pp = p;
3418 }
3419}
3420
3421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 * For each file name in files[num_files]:
3423 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3424 */
3425 void
3426tilde_replace(orig_pat, num_files, files)
3427 char_u *orig_pat;
3428 int num_files;
3429 char_u **files;
3430{
3431 int i;
3432 char_u *p;
3433
3434 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3435 {
3436 for (i = 0; i < num_files; ++i)
3437 {
3438 p = home_replace_save(NULL, files[i]);
3439 if (p != NULL)
3440 {
3441 vim_free(files[i]);
3442 files[i] = p;
3443 }
3444 }
3445 }
3446}
3447
3448/*
3449 * Show all matches for completion on the command line.
3450 * Returns EXPAND_NOTHING when the character that triggered expansion should
3451 * be inserted like a normal character.
3452 */
3453/*ARGSUSED*/
3454 static int
3455showmatches(xp, wildmenu)
3456 expand_T *xp;
3457 int wildmenu;
3458{
3459#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3460 int num_files;
3461 char_u **files_found;
3462 int i, j, k;
3463 int maxlen;
3464 int lines;
3465 int columns;
3466 char_u *p;
3467 int lastlen;
3468 int attr;
3469 int showtail;
3470
3471 if (xp->xp_numfiles == -1)
3472 {
3473 set_expand_context(xp);
3474 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3475 &num_files, &files_found);
3476 showtail = expand_showtail(xp);
3477 if (i != EXPAND_OK)
3478 return i;
3479
3480 }
3481 else
3482 {
3483 num_files = xp->xp_numfiles;
3484 files_found = xp->xp_files;
3485 showtail = cmd_showtail;
3486 }
3487
3488#ifdef FEAT_WILDMENU
3489 if (!wildmenu)
3490 {
3491#endif
3492 msg_didany = FALSE; /* lines_left will be set */
3493 msg_start(); /* prepare for paging */
3494 msg_putchar('\n');
3495 out_flush();
3496 cmdline_row = msg_row;
3497 msg_didany = FALSE; /* lines_left will be set again */
3498 msg_start(); /* prepare for paging */
3499#ifdef FEAT_WILDMENU
3500 }
3501#endif
3502
3503 if (got_int)
3504 got_int = FALSE; /* only int. the completion, not the cmd line */
3505#ifdef FEAT_WILDMENU
3506 else if (wildmenu)
3507 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3508#endif
3509 else
3510 {
3511 /* find the length of the longest file name */
3512 maxlen = 0;
3513 for (i = 0; i < num_files; ++i)
3514 {
3515 if (!showtail && (xp->xp_context == EXPAND_FILES
3516 || xp->xp_context == EXPAND_BUFFERS))
3517 {
3518 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3519 j = vim_strsize(NameBuff);
3520 }
3521 else
3522 j = vim_strsize(L_SHOWFILE(i));
3523 if (j > maxlen)
3524 maxlen = j;
3525 }
3526
3527 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3528 lines = num_files;
3529 else
3530 {
3531 /* compute the number of columns and lines for the listing */
3532 maxlen += 2; /* two spaces between file names */
3533 columns = ((int)Columns + 2) / maxlen;
3534 if (columns < 1)
3535 columns = 1;
3536 lines = (num_files + columns - 1) / columns;
3537 }
3538
3539 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3540
3541 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3542 {
3543 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3544 msg_clr_eos();
3545 msg_advance(maxlen - 3);
3546 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3547 }
3548
3549 /* list the files line by line */
3550 for (i = 0; i < lines; ++i)
3551 {
3552 lastlen = 999;
3553 for (k = i; k < num_files; k += lines)
3554 {
3555 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3556 {
3557 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3558 p = files_found[k] + STRLEN(files_found[k]) + 1;
3559 msg_advance(maxlen + 1);
3560 msg_puts(p);
3561 msg_advance(maxlen + 3);
3562 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3563 break;
3564 }
3565 for (j = maxlen - lastlen; --j >= 0; )
3566 msg_putchar(' ');
3567 if (xp->xp_context == EXPAND_FILES
3568 || xp->xp_context == EXPAND_BUFFERS)
3569 {
3570 /* highlight directories */
3571 j = (mch_isdir(files_found[k]));
3572 if (showtail)
3573 p = L_SHOWFILE(k);
3574 else
3575 {
3576 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3577 TRUE);
3578 p = NameBuff;
3579 }
3580 }
3581 else
3582 {
3583 j = FALSE;
3584 p = L_SHOWFILE(k);
3585 }
3586 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3587 }
3588 if (msg_col > 0) /* when not wrapped around */
3589 {
3590 msg_clr_eos();
3591 msg_putchar('\n');
3592 }
3593 out_flush(); /* show one line at a time */
3594 if (got_int)
3595 {
3596 got_int = FALSE;
3597 break;
3598 }
3599 }
3600
3601 /*
3602 * we redraw the command below the lines that we have just listed
3603 * This is a bit tricky, but it saves a lot of screen updating.
3604 */
3605 cmdline_row = msg_row; /* will put it back later */
3606 }
3607
3608 if (xp->xp_numfiles == -1)
3609 FreeWild(num_files, files_found);
3610
3611 return EXPAND_OK;
3612}
3613
3614/*
3615 * Private gettail for showmatches() (and win_redr_status_matches()):
3616 * Find tail of file name path, but ignore trailing "/".
3617 */
3618 char_u *
3619sm_gettail(s)
3620 char_u *s;
3621{
3622 char_u *p;
3623 char_u *t = s;
3624 int had_sep = FALSE;
3625
3626 for (p = s; *p != NUL; )
3627 {
3628 if (vim_ispathsep(*p)
3629#ifdef BACKSLASH_IN_FILENAME
3630 && !rem_backslash(p)
3631#endif
3632 )
3633 had_sep = TRUE;
3634 else if (had_sep)
3635 {
3636 t = p;
3637 had_sep = FALSE;
3638 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003639 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
3641 return t;
3642}
3643
3644/*
3645 * Return TRUE if we only need to show the tail of completion matches.
3646 * When not completing file names or there is a wildcard in the path FALSE is
3647 * returned.
3648 */
3649 static int
3650expand_showtail(xp)
3651 expand_T *xp;
3652{
3653 char_u *s;
3654 char_u *end;
3655
3656 /* When not completing file names a "/" may mean something different. */
3657 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3658 return FALSE;
3659
3660 end = gettail(xp->xp_pattern);
3661 if (end == xp->xp_pattern) /* there is no path separator */
3662 return FALSE;
3663
3664 for (s = xp->xp_pattern; s < end; s++)
3665 {
3666 /* Skip escaped wildcards. Only when the backslash is not a path
3667 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3668 if (rem_backslash(s))
3669 ++s;
3670 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3671 return FALSE;
3672 }
3673 return TRUE;
3674}
3675
3676/*
3677 * Prepare a string for expansion.
3678 * When expanding file names: The string will be used with expand_wildcards().
3679 * Copy the file name into allocated memory and add a '*' at the end.
3680 * When expanding other names: The string will be used with regcomp(). Copy
3681 * the name into allocated memory and prepend "^".
3682 */
3683 char_u *
3684addstar(fname, len, context)
3685 char_u *fname;
3686 int len;
3687 int context; /* EXPAND_FILES etc. */
3688{
3689 char_u *retval;
3690 int i, j;
3691 int new_len;
3692 char_u *tail;
3693
3694 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3695 {
3696 /*
3697 * Matching will be done internally (on something other than files).
3698 * So we convert the file-matching-type wildcards into our kind for
3699 * use with vim_regcomp(). First work out how long it will be:
3700 */
3701
3702 /* For help tags the translation is done in find_help_tags().
3703 * For a tag pattern starting with "/" no translation is needed. */
3704 if (context == EXPAND_HELP
3705 || context == EXPAND_COLORS
3706 || context == EXPAND_COMPILER
3707 || (context == EXPAND_TAGS && fname[0] == '/'))
3708 retval = vim_strnsave(fname, len);
3709 else
3710 {
3711 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3712 for (i = 0; i < len; i++)
3713 {
3714 if (fname[i] == '*' || fname[i] == '~')
3715 new_len++; /* '*' needs to be replaced by ".*"
3716 '~' needs to be replaced by "\~" */
3717
3718 /* Buffer names are like file names. "." should be literal */
3719 if (context == EXPAND_BUFFERS && fname[i] == '.')
3720 new_len++; /* "." becomes "\." */
3721
3722 /* Custom expansion takes care of special things, match
3723 * backslashes literally (perhaps also for other types?) */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003724 if ((context == EXPAND_USER_DEFINED ||
3725 context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 new_len++; /* '\' becomes "\\" */
3727 }
3728 retval = alloc(new_len);
3729 if (retval != NULL)
3730 {
3731 retval[0] = '^';
3732 j = 1;
3733 for (i = 0; i < len; i++, j++)
3734 {
3735 /* Skip backslash. But why? At least keep it for custom
3736 * expansion. */
3737 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003738 && context != EXPAND_USER_LIST
3739 && fname[i] == '\\'
3740 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 break;
3742
3743 switch (fname[i])
3744 {
3745 case '*': retval[j++] = '.';
3746 break;
3747 case '~': retval[j++] = '\\';
3748 break;
3749 case '?': retval[j] = '.';
3750 continue;
3751 case '.': if (context == EXPAND_BUFFERS)
3752 retval[j++] = '\\';
3753 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003754 case '\\': if (context == EXPAND_USER_DEFINED
3755 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 retval[j++] = '\\';
3757 break;
3758 }
3759 retval[j] = fname[i];
3760 }
3761 retval[j] = NUL;
3762 }
3763 }
3764 }
3765 else
3766 {
3767 retval = alloc(len + 4);
3768 if (retval != NULL)
3769 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003770 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
3772 /*
3773 * Don't add a star to ~, ~user, $var or `cmd`.
3774 * ~ would be at the start of the file name, but not the tail.
3775 * $ could be anywhere in the tail.
3776 * ` could be anywhere in the file name.
3777 */
3778 tail = gettail(retval);
3779 if ((*retval != '~' || tail != retval)
3780 && vim_strchr(tail, '$') == NULL
3781 && vim_strchr(retval, '`') == NULL)
3782 retval[len++] = '*';
3783 retval[len] = NUL;
3784 }
3785 }
3786 return retval;
3787}
3788
3789/*
3790 * Must parse the command line so far to work out what context we are in.
3791 * Completion can then be done based on that context.
3792 * This routine sets the variables:
3793 * xp->xp_pattern The start of the pattern to be expanded within
3794 * the command line (ends at the cursor).
3795 * xp->xp_context The type of thing to expand. Will be one of:
3796 *
3797 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3798 * the command line, like an unknown command. Caller
3799 * should beep.
3800 * EXPAND_NOTHING Unrecognised context for completion, use char like
3801 * a normal char, rather than for completion. eg
3802 * :s/^I/
3803 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3804 * it.
3805 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3806 * EXPAND_FILES After command with XFILE set, or after setting
3807 * with P_EXPAND set. eg :e ^I, :w>>^I
3808 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3809 * when we know only directories are of interest. eg
3810 * :set dir=^I
3811 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3812 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3813 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3814 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3815 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3816 * EXPAND_EVENTS Complete event names
3817 * EXPAND_SYNTAX Complete :syntax command arguments
3818 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3819 * EXPAND_AUGROUP Complete autocommand group names
3820 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3821 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3822 * eg :unmap a^I , :cunab x^I
3823 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3824 * eg :call sub^I
3825 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3826 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3827 * names in expressions, eg :while s^I
3828 * EXPAND_ENV_VARS Complete environment variable names
3829 */
3830 static void
3831set_expand_context(xp)
3832 expand_T *xp;
3833{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003834 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (ccline.cmdfirstc != ':'
3836#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003837 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838#endif
3839 )
3840 {
3841 xp->xp_context = EXPAND_NOTHING;
3842 return;
3843 }
3844 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3845}
3846
3847 void
3848set_cmd_context(xp, str, len, col)
3849 expand_T *xp;
3850 char_u *str; /* start of command line */
3851 int len; /* length of command line (excl. NUL) */
3852 int col; /* position of cursor */
3853{
3854 int old_char = NUL;
3855 char_u *nextcomm;
3856
3857 /*
3858 * Avoid a UMR warning from Purify, only save the character if it has been
3859 * written before.
3860 */
3861 if (col < len)
3862 old_char = str[col];
3863 str[col] = NUL;
3864 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003865
3866#ifdef FEAT_EVAL
3867 if (ccline.cmdfirstc == '=')
3868 /* pass CMD_SIZE because there is no real command */
3869 set_context_for_expression(xp, str, CMD_SIZE);
3870 else
3871#endif
3872 while (nextcomm != NULL)
3873 nextcomm = set_one_cmd_context(xp, nextcomm);
3874
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 str[col] = old_char;
3876}
3877
3878/*
3879 * Expand the command line "str" from context "xp".
3880 * "xp" must have been set by set_cmd_context().
3881 * xp->xp_pattern points into "str", to where the text that is to be expanded
3882 * starts.
3883 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
3884 * cursor.
3885 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
3886 * key that triggered expansion literally.
3887 * Returns EXPAND_OK otherwise.
3888 */
3889 int
3890expand_cmdline(xp, str, col, matchcount, matches)
3891 expand_T *xp;
3892 char_u *str; /* start of command line */
3893 int col; /* position of cursor */
3894 int *matchcount; /* return: nr of matches */
3895 char_u ***matches; /* return: array of pointers to matches */
3896{
3897 char_u *file_str = NULL;
3898
3899 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3900 {
3901 beep_flush();
3902 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
3903 }
3904 if (xp->xp_context == EXPAND_NOTHING)
3905 {
3906 /* Caller can use the character as a normal char instead */
3907 return EXPAND_NOTHING;
3908 }
3909
3910 /* add star to file name, or convert to regexp if not exp. files. */
3911 file_str = addstar(xp->xp_pattern,
3912 (int)(str + col - xp->xp_pattern), xp->xp_context);
3913 if (file_str == NULL)
3914 return EXPAND_UNSUCCESSFUL;
3915
3916 /* find all files that match the description */
3917 if (ExpandFromContext(xp, file_str, matchcount, matches,
3918 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
3919 {
3920 *matchcount = 0;
3921 *matches = NULL;
3922 }
3923 vim_free(file_str);
3924
3925 return EXPAND_OK;
3926}
3927
3928#ifdef FEAT_MULTI_LANG
3929/*
3930 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
3931 */
3932static void cleanup_help_tags __ARGS((int num_file, char_u **file));
3933
3934 static void
3935cleanup_help_tags(num_file, file)
3936 int num_file;
3937 char_u **file;
3938{
3939 int i, j;
3940 int len;
3941
3942 for (i = 0; i < num_file; ++i)
3943 {
3944 len = (int)STRLEN(file[i]) - 3;
3945 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
3946 {
3947 /* Sorting on priority means the same item in another language may
3948 * be anywhere. Search all items for a match up to the "@en". */
3949 for (j = 0; j < num_file; ++j)
3950 if (j != i
3951 && (int)STRLEN(file[j]) == len + 3
3952 && STRNCMP(file[i], file[j], len + 1) == 0)
3953 break;
3954 if (j == num_file)
3955 file[i][len] = NUL;
3956 }
3957 }
3958}
3959#endif
3960
3961/*
3962 * Do the expansion based on xp->xp_context and "pat".
3963 */
3964 static int
3965ExpandFromContext(xp, pat, num_file, file, options)
3966 expand_T *xp;
3967 char_u *pat;
3968 int *num_file;
3969 char_u ***file;
3970 int options;
3971{
3972#ifdef FEAT_CMDL_COMPL
3973 regmatch_T regmatch;
3974#endif
3975 int ret;
3976 int flags;
3977
3978 flags = EW_DIR; /* include directories */
3979 if (options & WILD_LIST_NOTFOUND)
3980 flags |= EW_NOTFOUND;
3981 if (options & WILD_ADD_SLASH)
3982 flags |= EW_ADDSLASH;
3983 if (options & WILD_KEEP_ALL)
3984 flags |= EW_KEEPALL;
3985 if (options & WILD_SILENT)
3986 flags |= EW_SILENT;
3987
3988 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
3989 {
3990 /*
3991 * Expand file or directory names.
3992 */
3993 int free_pat = FALSE;
3994 int i;
3995
3996 /* for ":set path=" and ":set tags=" halve backslashes for escaped
3997 * space */
3998 if (xp->xp_backslash != XP_BS_NONE)
3999 {
4000 free_pat = TRUE;
4001 pat = vim_strsave(pat);
4002 for (i = 0; pat[i]; ++i)
4003 if (pat[i] == '\\')
4004 {
4005 if (xp->xp_backslash == XP_BS_THREE
4006 && pat[i + 1] == '\\'
4007 && pat[i + 2] == '\\'
4008 && pat[i + 3] == ' ')
4009 STRCPY(pat + i, pat + i + 3);
4010 if (xp->xp_backslash == XP_BS_ONE
4011 && pat[i + 1] == ' ')
4012 STRCPY(pat + i, pat + i + 1);
4013 }
4014 }
4015
4016 if (xp->xp_context == EXPAND_FILES)
4017 flags |= EW_FILE;
4018 else
4019 flags = (flags | EW_DIR) & ~EW_FILE;
4020 ret = expand_wildcards(1, &pat, num_file, file, flags);
4021 if (free_pat)
4022 vim_free(pat);
4023 return ret;
4024 }
4025
4026 *file = (char_u **)"";
4027 *num_file = 0;
4028 if (xp->xp_context == EXPAND_HELP)
4029 {
4030 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4031 {
4032#ifdef FEAT_MULTI_LANG
4033 cleanup_help_tags(*num_file, *file);
4034#endif
4035 return OK;
4036 }
4037 return FAIL;
4038 }
4039
4040#ifndef FEAT_CMDL_COMPL
4041 return FAIL;
4042#else
4043 if (xp->xp_context == EXPAND_OLD_SETTING)
4044 return ExpandOldSetting(num_file, file);
4045 if (xp->xp_context == EXPAND_BUFFERS)
4046 return ExpandBufnames(pat, num_file, file, options);
4047 if (xp->xp_context == EXPAND_TAGS
4048 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4049 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4050 if (xp->xp_context == EXPAND_COLORS)
4051 return ExpandRTDir(pat, num_file, file, "colors");
4052 if (xp->xp_context == EXPAND_COMPILER)
4053 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004054# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4055 if (xp->xp_context == EXPAND_USER_LIST)
4056 return ExpandUserList(xp, num_file, file);
4057# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4060 if (regmatch.regprog == NULL)
4061 return FAIL;
4062
4063 /* set ignore-case according to p_ic, p_scs and pat */
4064 regmatch.rm_ic = ignorecase(pat);
4065
4066 if (xp->xp_context == EXPAND_SETTINGS
4067 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4068 ret = ExpandSettings(xp, &regmatch, num_file, file);
4069 else if (xp->xp_context == EXPAND_MAPPINGS)
4070 ret = ExpandMappings(&regmatch, num_file, file);
4071# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4072 else if (xp->xp_context == EXPAND_USER_DEFINED)
4073 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4074# endif
4075 else
4076 {
4077 static struct expgen
4078 {
4079 int context;
4080 char_u *((*func)__ARGS((expand_T *, int)));
4081 int ic;
4082 } tab[] =
4083 {
4084 {EXPAND_COMMANDS, get_command_name, FALSE},
4085#ifdef FEAT_USR_CMDS
4086 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4087 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4088 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4089 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4090#endif
4091#ifdef FEAT_EVAL
4092 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4093 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4094 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4095 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4096#endif
4097#ifdef FEAT_MENU
4098 {EXPAND_MENUS, get_menu_name, FALSE},
4099 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4100#endif
4101#ifdef FEAT_SYN_HL
4102 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4103#endif
4104 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4105#ifdef FEAT_AUTOCMD
4106 {EXPAND_EVENTS, get_event_name, TRUE},
4107 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4108#endif
4109#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4110 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4111 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4112#endif
4113 {EXPAND_ENV_VARS, get_env_name, TRUE},
4114 };
4115 int i;
4116
4117 /*
4118 * Find a context in the table and call the ExpandGeneric() with the
4119 * right function to do the expansion.
4120 */
4121 ret = FAIL;
4122 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4123 if (xp->xp_context == tab[i].context)
4124 {
4125 if (tab[i].ic)
4126 regmatch.rm_ic = TRUE;
4127 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4128 break;
4129 }
4130 }
4131
4132 vim_free(regmatch.regprog);
4133
4134 return ret;
4135#endif /* FEAT_CMDL_COMPL */
4136}
4137
4138#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4139/*
4140 * Expand a list of names.
4141 *
4142 * Generic function for command line completion. It calls a function to
4143 * obtain strings, one by one. The strings are matched against a regexp
4144 * program. Matching strings are copied into an array, which is returned.
4145 *
4146 * Returns OK when no problems encountered, FAIL for error (out of memory).
4147 */
4148 int
4149ExpandGeneric(xp, regmatch, num_file, file, func)
4150 expand_T *xp;
4151 regmatch_T *regmatch;
4152 int *num_file;
4153 char_u ***file;
4154 char_u *((*func)__ARGS((expand_T *, int)));
4155 /* returns a string from the list */
4156{
4157 int i;
4158 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004159 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 char_u *str;
4161
4162 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004163 * round == 0: count the number of matching names
4164 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004166 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
4168 for (i = 0; ; ++i)
4169 {
4170 str = (*func)(xp, i);
4171 if (str == NULL) /* end of list */
4172 break;
4173 if (*str == NUL) /* skip empty strings */
4174 continue;
4175
4176 if (vim_regexec(regmatch, str, (colnr_T)0))
4177 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004178 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
4180 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4181 (*file)[count] = str;
4182#ifdef FEAT_MENU
4183 if (func == get_menu_names && str != NULL)
4184 {
4185 /* test for separator added by get_menu_names() */
4186 str += STRLEN(str) - 1;
4187 if (*str == '\001')
4188 *str = '.';
4189 }
4190#endif
4191 }
4192 ++count;
4193 }
4194 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004195 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 {
4197 if (count == 0)
4198 return OK;
4199 *num_file = count;
4200 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4201 if (*file == NULL)
4202 {
4203 *file = (char_u **)"";
4204 return FAIL;
4205 }
4206 count = 0;
4207 }
4208 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004209
4210 /* Sort the results. */
4211 sort_strings(*file, *num_file);
4212
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return OK;
4214}
4215
4216# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004217static 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));
4218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004220 * call "user_expand_func()" to invoke a user defined VimL function and return
4221 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004223 static void *
4224call_user_expand_func(user_expand_func, xp, num_file, file)
4225 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 int *num_file;
4228 char_u ***file;
4229{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004230 char_u keep;
4231 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004234 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004235 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
4237 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004238 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 *num_file = 0;
4240 *file = NULL;
4241
4242 keep = ccline.cmdbuff[ccline.cmdlen];
4243 ccline.cmdbuff[ccline.cmdlen] = 0;
4244 sprintf((char *)num, "%d", ccline.cmdpos);
4245 args[0] = xp->xp_pattern;
4246 args[1] = ccline.cmdbuff;
4247 args[2] = num;
4248
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004249 /* Save the cmdline, we don't know what the function may do. */
4250 save_ccline = ccline;
4251 ccline.cmdbuff = NULL;
4252 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004254
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004255 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004256
4257 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004261
4262 return ret;
4263}
4264
4265/*
4266 * Expand names with a function defined by the user.
4267 */
4268 static int
4269ExpandUserDefined(xp, regmatch, num_file, file)
4270 expand_T *xp;
4271 regmatch_T *regmatch;
4272 int *num_file;
4273 char_u ***file;
4274{
4275 char_u *retstr;
4276 char_u *s;
4277 char_u *e;
4278 char_u keep;
4279 garray_T ga;
4280
4281 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4282 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 return FAIL;
4284
4285 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004286 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
4288 e = vim_strchr(s, '\n');
4289 if (e == NULL)
4290 e = s + STRLEN(s);
4291 keep = *e;
4292 *e = 0;
4293
4294 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4295 {
4296 *e = keep;
4297 if (*e != NUL)
4298 ++e;
4299 continue;
4300 }
4301
4302 if (ga_grow(&ga, 1) == FAIL)
4303 break;
4304
4305 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4306 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 *e = keep;
4309 if (*e != NUL)
4310 ++e;
4311 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004312 vim_free(retstr);
4313 *file = ga.ga_data;
4314 *num_file = ga.ga_len;
4315 return OK;
4316}
4317
4318/*
4319 * Expand names with a list returned by a function defined by the user.
4320 */
4321 static int
4322ExpandUserList(xp, num_file, file)
4323 expand_T *xp;
4324 int *num_file;
4325 char_u ***file;
4326{
4327 list_T *retlist;
4328 listitem_T *li;
4329 garray_T ga;
4330
4331 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4332 if (retlist == NULL)
4333 return FAIL;
4334
4335 ga_init2(&ga, (int)sizeof(char *), 3);
4336 /* Loop over the items in the list. */
4337 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4338 {
4339 if (li->li_tv.v_type != VAR_STRING)
4340 continue; /* Skip non-string items */
4341
4342 if (ga_grow(&ga, 1) == FAIL)
4343 break;
4344
4345 ((char_u **)ga.ga_data)[ga.ga_len] =
4346 vim_strsave(li->li_tv.vval.v_string);
4347 ++ga.ga_len;
4348 }
4349 list_unref(retlist);
4350
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 *file = ga.ga_data;
4352 *num_file = ga.ga_len;
4353 return OK;
4354}
4355#endif
4356
4357/*
4358 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4359 * or compiler names.
4360 */
4361 static int
4362ExpandRTDir(pat, num_file, file, dirname)
4363 char_u *pat;
4364 int *num_file;
4365 char_u ***file;
4366 char *dirname; /* "colors" or "compiler" */
4367{
4368 char_u *all;
4369 char_u *s;
4370 char_u *e;
4371 garray_T ga;
4372
4373 *num_file = 0;
4374 *file = NULL;
4375 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4376 if (s == NULL)
4377 return FAIL;
4378 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4379 all = globpath(p_rtp, s);
4380 vim_free(s);
4381 if (all == NULL)
4382 return FAIL;
4383
4384 ga_init2(&ga, (int)sizeof(char *), 3);
4385 for (s = all; *s != NUL; s = e)
4386 {
4387 e = vim_strchr(s, '\n');
4388 if (e == NULL)
4389 e = s + STRLEN(s);
4390 if (ga_grow(&ga, 1) == FAIL)
4391 break;
4392 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4393 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004394 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 if (*s == '\n' || vim_ispathsep(*s))
4396 break;
4397 ++s;
4398 ((char_u **)ga.ga_data)[ga.ga_len] =
4399 vim_strnsave(s, (int)(e - s - 4));
4400 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402 if (*e != NUL)
4403 ++e;
4404 }
4405 vim_free(all);
4406 *file = ga.ga_data;
4407 *num_file = ga.ga_len;
4408 return OK;
4409}
4410
4411#endif
4412
4413#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4414/*
4415 * Expand "file" for all comma-separated directories in "path".
4416 * Returns an allocated string with all matches concatenated, separated by
4417 * newlines. Returns NULL for an error or no matches.
4418 */
4419 char_u *
4420globpath(path, file)
4421 char_u *path;
4422 char_u *file;
4423{
4424 expand_T xpc;
4425 char_u *buf;
4426 garray_T ga;
4427 int i;
4428 int len;
4429 int num_p;
4430 char_u **p;
4431 char_u *cur = NULL;
4432
4433 buf = alloc(MAXPATHL);
4434 if (buf == NULL)
4435 return NULL;
4436
4437 xpc.xp_context = EXPAND_FILES;
4438 xpc.xp_backslash = XP_BS_NONE;
4439 ga_init2(&ga, 1, 100);
4440
4441 /* Loop over all entries in {path}. */
4442 while (*path != NUL)
4443 {
4444 /* Copy one item of the path to buf[] and concatenate the file name. */
4445 copy_option_part(&path, buf, MAXPATHL, ",");
4446 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4447 {
4448 add_pathsep(buf);
4449 STRCAT(buf, file);
4450 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4451 && num_p > 0)
4452 {
4453 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4454 for (len = 0, i = 0; i < num_p; ++i)
4455 len += (long_u)STRLEN(p[i]) + 1;
4456
4457 /* Concatenate new results to previous ones. */
4458 if (ga_grow(&ga, len) == OK)
4459 {
4460 cur = (char_u *)ga.ga_data + ga.ga_len;
4461 for (i = 0; i < num_p; ++i)
4462 {
4463 STRCPY(cur, p[i]);
4464 cur += STRLEN(p[i]);
4465 *cur++ = '\n';
4466 }
4467 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 FreeWild(num_p, p);
4470 }
4471 }
4472 }
4473 if (cur != NULL)
4474 *--cur = 0; /* Replace trailing newline with NUL */
4475
4476 vim_free(buf);
4477 return (char_u *)ga.ga_data;
4478}
4479
4480#endif
4481
4482#if defined(FEAT_CMDHIST) || defined(PROTO)
4483
4484/*********************************
4485 * Command line history stuff *
4486 *********************************/
4487
4488/*
4489 * Translate a history character to the associated type number.
4490 */
4491 static int
4492hist_char2type(c)
4493 int c;
4494{
4495 if (c == ':')
4496 return HIST_CMD;
4497 if (c == '=')
4498 return HIST_EXPR;
4499 if (c == '@')
4500 return HIST_INPUT;
4501 if (c == '>')
4502 return HIST_DEBUG;
4503 return HIST_SEARCH; /* must be '?' or '/' */
4504}
4505
4506/*
4507 * Table of history names.
4508 * These names are used in :history and various hist...() functions.
4509 * It is sufficient to give the significant prefix of a history name.
4510 */
4511
4512static char *(history_names[]) =
4513{
4514 "cmd",
4515 "search",
4516 "expr",
4517 "input",
4518 "debug",
4519 NULL
4520};
4521
4522/*
4523 * init_history() - Initialize the command line history.
4524 * Also used to re-allocate the history when the size changes.
4525 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004526 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527init_history()
4528{
4529 int newlen; /* new length of history table */
4530 histentry_T *temp;
4531 int i;
4532 int j;
4533 int type;
4534
4535 /*
4536 * If size of history table changed, reallocate it
4537 */
4538 newlen = (int)p_hi;
4539 if (newlen != hislen) /* history length changed */
4540 {
4541 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4542 {
4543 if (newlen)
4544 {
4545 temp = (histentry_T *)lalloc(
4546 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4547 if (temp == NULL) /* out of memory! */
4548 {
4549 if (type == 0) /* first one: just keep the old length */
4550 {
4551 newlen = hislen;
4552 break;
4553 }
4554 /* Already changed one table, now we can only have zero
4555 * length for all tables. */
4556 newlen = 0;
4557 type = -1;
4558 continue;
4559 }
4560 }
4561 else
4562 temp = NULL;
4563 if (newlen == 0 || temp != NULL)
4564 {
4565 if (hisidx[type] < 0) /* there are no entries yet */
4566 {
4567 for (i = 0; i < newlen; ++i)
4568 {
4569 temp[i].hisnum = 0;
4570 temp[i].hisstr = NULL;
4571 }
4572 }
4573 else if (newlen > hislen) /* array becomes bigger */
4574 {
4575 for (i = 0; i <= hisidx[type]; ++i)
4576 temp[i] = history[type][i];
4577 j = i;
4578 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4579 {
4580 temp[i].hisnum = 0;
4581 temp[i].hisstr = NULL;
4582 }
4583 for ( ; j < hislen; ++i, ++j)
4584 temp[i] = history[type][j];
4585 }
4586 else /* array becomes smaller or 0 */
4587 {
4588 j = hisidx[type];
4589 for (i = newlen - 1; ; --i)
4590 {
4591 if (i >= 0) /* copy newest entries */
4592 temp[i] = history[type][j];
4593 else /* remove older entries */
4594 vim_free(history[type][j].hisstr);
4595 if (--j < 0)
4596 j = hislen - 1;
4597 if (j == hisidx[type])
4598 break;
4599 }
4600 hisidx[type] = newlen - 1;
4601 }
4602 vim_free(history[type]);
4603 history[type] = temp;
4604 }
4605 }
4606 hislen = newlen;
4607 }
4608}
4609
4610/*
4611 * Check if command line 'str' is already in history.
4612 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4613 */
4614 static int
4615in_history(type, str, move_to_front)
4616 int type;
4617 char_u *str;
4618 int move_to_front; /* Move the entry to the front if it exists */
4619{
4620 int i;
4621 int last_i = -1;
4622
4623 if (hisidx[type] < 0)
4624 return FALSE;
4625 i = hisidx[type];
4626 do
4627 {
4628 if (history[type][i].hisstr == NULL)
4629 return FALSE;
4630 if (STRCMP(str, history[type][i].hisstr) == 0)
4631 {
4632 if (!move_to_front)
4633 return TRUE;
4634 last_i = i;
4635 break;
4636 }
4637 if (--i < 0)
4638 i = hislen - 1;
4639 } while (i != hisidx[type]);
4640
4641 if (last_i >= 0)
4642 {
4643 str = history[type][i].hisstr;
4644 while (i != hisidx[type])
4645 {
4646 if (++i >= hislen)
4647 i = 0;
4648 history[type][last_i] = history[type][i];
4649 last_i = i;
4650 }
4651 history[type][i].hisstr = str;
4652 history[type][i].hisnum = ++hisnum[type];
4653 return TRUE;
4654 }
4655 return FALSE;
4656}
4657
4658/*
4659 * Convert history name (from table above) to its HIST_ equivalent.
4660 * When "name" is empty, return "cmd" history.
4661 * Returns -1 for unknown history name.
4662 */
4663 int
4664get_histtype(name)
4665 char_u *name;
4666{
4667 int i;
4668 int len = (int)STRLEN(name);
4669
4670 /* No argument: use current history. */
4671 if (len == 0)
4672 return hist_char2type(ccline.cmdfirstc);
4673
4674 for (i = 0; history_names[i] != NULL; ++i)
4675 if (STRNICMP(name, history_names[i], len) == 0)
4676 return i;
4677
4678 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4679 return hist_char2type(name[0]);
4680
4681 return -1;
4682}
4683
4684static int last_maptick = -1; /* last seen maptick */
4685
4686/*
4687 * Add the given string to the given history. If the string is already in the
4688 * history then it is moved to the front. "histype" may be one of he HIST_
4689 * values.
4690 */
4691 void
4692add_to_history(histype, new_entry, in_map, sep)
4693 int histype;
4694 char_u *new_entry;
4695 int in_map; /* consider maptick when inside a mapping */
4696 int sep; /* separator character used (search hist) */
4697{
4698 histentry_T *hisptr;
4699 int len;
4700
4701 if (hislen == 0) /* no history */
4702 return;
4703
4704 /*
4705 * Searches inside the same mapping overwrite each other, so that only
4706 * the last line is kept. Be careful not to remove a line that was moved
4707 * down, only lines that were added.
4708 */
4709 if (histype == HIST_SEARCH && in_map)
4710 {
4711 if (maptick == last_maptick)
4712 {
4713 /* Current line is from the same mapping, remove it */
4714 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4715 vim_free(hisptr->hisstr);
4716 hisptr->hisstr = NULL;
4717 hisptr->hisnum = 0;
4718 --hisnum[histype];
4719 if (--hisidx[HIST_SEARCH] < 0)
4720 hisidx[HIST_SEARCH] = hislen - 1;
4721 }
4722 last_maptick = -1;
4723 }
4724 if (!in_history(histype, new_entry, TRUE))
4725 {
4726 if (++hisidx[histype] == hislen)
4727 hisidx[histype] = 0;
4728 hisptr = &history[histype][hisidx[histype]];
4729 vim_free(hisptr->hisstr);
4730
4731 /* Store the separator after the NUL of the string. */
4732 len = STRLEN(new_entry);
4733 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4734 if (hisptr->hisstr != NULL)
4735 hisptr->hisstr[len + 1] = sep;
4736
4737 hisptr->hisnum = ++hisnum[histype];
4738 if (histype == HIST_SEARCH && in_map)
4739 last_maptick = maptick;
4740 }
4741}
4742
4743#if defined(FEAT_EVAL) || defined(PROTO)
4744
4745/*
4746 * Get identifier of newest history entry.
4747 * "histype" may be one of the HIST_ values.
4748 */
4749 int
4750get_history_idx(histype)
4751 int histype;
4752{
4753 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4754 || hisidx[histype] < 0)
4755 return -1;
4756
4757 return history[histype][hisidx[histype]].hisnum;
4758}
4759
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004760static struct cmdline_info *get_ccline_ptr __ARGS((void));
4761
4762/*
4763 * Get pointer to the command line info to use. cmdline_paste() may clear
4764 * ccline and put the previous value in prev_ccline.
4765 */
4766 static struct cmdline_info *
4767get_ccline_ptr()
4768{
4769 if ((State & CMDLINE) == 0)
4770 return NULL;
4771 if (ccline.cmdbuff != NULL)
4772 return &ccline;
4773 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4774 return &prev_ccline;
4775 return NULL;
4776}
4777
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778/*
4779 * Get the current command line in allocated memory.
4780 * Only works when the command line is being edited.
4781 * Returns NULL when something is wrong.
4782 */
4783 char_u *
4784get_cmdline_str()
4785{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004786 struct cmdline_info *p = get_ccline_ptr();
4787
4788 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004790 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791}
4792
4793/*
4794 * Get the current command line position, counted in bytes.
4795 * Zero is the first position.
4796 * Only works when the command line is being edited.
4797 * Returns -1 when something is wrong.
4798 */
4799 int
4800get_cmdline_pos()
4801{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004802 struct cmdline_info *p = get_ccline_ptr();
4803
4804 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004806 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807}
4808
4809/*
4810 * Set the command line byte position to "pos". Zero is the first position.
4811 * Only works when the command line is being edited.
4812 * Returns 1 when failed, 0 when OK.
4813 */
4814 int
4815set_cmdline_pos(pos)
4816 int pos;
4817{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004818 struct cmdline_info *p = get_ccline_ptr();
4819
4820 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 return 1;
4822
4823 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4824 * changed the command line. */
4825 if (pos < 0)
4826 new_cmdpos = 0;
4827 else
4828 new_cmdpos = pos;
4829 return 0;
4830}
4831
4832/*
4833 * Calculate history index from a number:
4834 * num > 0: seen as identifying number of a history entry
4835 * num < 0: relative position in history wrt newest entry
4836 * "histype" may be one of the HIST_ values.
4837 */
4838 static int
4839calc_hist_idx(histype, num)
4840 int histype;
4841 int num;
4842{
4843 int i;
4844 histentry_T *hist;
4845 int wrapped = FALSE;
4846
4847 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4848 || (i = hisidx[histype]) < 0 || num == 0)
4849 return -1;
4850
4851 hist = history[histype];
4852 if (num > 0)
4853 {
4854 while (hist[i].hisnum > num)
4855 if (--i < 0)
4856 {
4857 if (wrapped)
4858 break;
4859 i += hislen;
4860 wrapped = TRUE;
4861 }
4862 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
4863 return i;
4864 }
4865 else if (-num <= hislen)
4866 {
4867 i += num + 1;
4868 if (i < 0)
4869 i += hislen;
4870 if (hist[i].hisstr != NULL)
4871 return i;
4872 }
4873 return -1;
4874}
4875
4876/*
4877 * Get a history entry by its index.
4878 * "histype" may be one of the HIST_ values.
4879 */
4880 char_u *
4881get_history_entry(histype, idx)
4882 int histype;
4883 int idx;
4884{
4885 idx = calc_hist_idx(histype, idx);
4886 if (idx >= 0)
4887 return history[histype][idx].hisstr;
4888 else
4889 return (char_u *)"";
4890}
4891
4892/*
4893 * Clear all entries of a history.
4894 * "histype" may be one of the HIST_ values.
4895 */
4896 int
4897clr_history(histype)
4898 int histype;
4899{
4900 int i;
4901 histentry_T *hisptr;
4902
4903 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
4904 {
4905 hisptr = history[histype];
4906 for (i = hislen; i--;)
4907 {
4908 vim_free(hisptr->hisstr);
4909 hisptr->hisnum = 0;
4910 hisptr++->hisstr = NULL;
4911 }
4912 hisidx[histype] = -1; /* mark history as cleared */
4913 hisnum[histype] = 0; /* reset identifier counter */
4914 return OK;
4915 }
4916 return FAIL;
4917}
4918
4919/*
4920 * Remove all entries matching {str} from a history.
4921 * "histype" may be one of the HIST_ values.
4922 */
4923 int
4924del_history_entry(histype, str)
4925 int histype;
4926 char_u *str;
4927{
4928 regmatch_T regmatch;
4929 histentry_T *hisptr;
4930 int idx;
4931 int i;
4932 int last;
4933 int found = FALSE;
4934
4935 regmatch.regprog = NULL;
4936 regmatch.rm_ic = FALSE; /* always match case */
4937 if (hislen != 0
4938 && histype >= 0
4939 && histype < HIST_COUNT
4940 && *str != NUL
4941 && (idx = hisidx[histype]) >= 0
4942 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
4943 != NULL)
4944 {
4945 i = last = idx;
4946 do
4947 {
4948 hisptr = &history[histype][i];
4949 if (hisptr->hisstr == NULL)
4950 break;
4951 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
4952 {
4953 found = TRUE;
4954 vim_free(hisptr->hisstr);
4955 hisptr->hisstr = NULL;
4956 hisptr->hisnum = 0;
4957 }
4958 else
4959 {
4960 if (i != last)
4961 {
4962 history[histype][last] = *hisptr;
4963 hisptr->hisstr = NULL;
4964 hisptr->hisnum = 0;
4965 }
4966 if (--last < 0)
4967 last += hislen;
4968 }
4969 if (--i < 0)
4970 i += hislen;
4971 } while (i != idx);
4972 if (history[histype][idx].hisstr == NULL)
4973 hisidx[histype] = -1;
4974 }
4975 vim_free(regmatch.regprog);
4976 return found;
4977}
4978
4979/*
4980 * Remove an indexed entry from a history.
4981 * "histype" may be one of the HIST_ values.
4982 */
4983 int
4984del_history_idx(histype, idx)
4985 int histype;
4986 int idx;
4987{
4988 int i, j;
4989
4990 i = calc_hist_idx(histype, idx);
4991 if (i < 0)
4992 return FALSE;
4993 idx = hisidx[histype];
4994 vim_free(history[histype][i].hisstr);
4995
4996 /* When deleting the last added search string in a mapping, reset
4997 * last_maptick, so that the last added search string isn't deleted again.
4998 */
4999 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5000 last_maptick = -1;
5001
5002 while (i != idx)
5003 {
5004 j = (i + 1) % hislen;
5005 history[histype][i] = history[histype][j];
5006 i = j;
5007 }
5008 history[histype][i].hisstr = NULL;
5009 history[histype][i].hisnum = 0;
5010 if (--i < 0)
5011 i += hislen;
5012 hisidx[histype] = i;
5013 return TRUE;
5014}
5015
5016#endif /* FEAT_EVAL */
5017
5018#if defined(FEAT_CRYPT) || defined(PROTO)
5019/*
5020 * Very specific function to remove the value in ":set key=val" from the
5021 * history.
5022 */
5023 void
5024remove_key_from_history()
5025{
5026 char_u *p;
5027 int i;
5028
5029 i = hisidx[HIST_CMD];
5030 if (i < 0)
5031 return;
5032 p = history[HIST_CMD][i].hisstr;
5033 if (p != NULL)
5034 for ( ; *p; ++p)
5035 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5036 {
5037 p = vim_strchr(p + 3, '=');
5038 if (p == NULL)
5039 break;
5040 ++p;
5041 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5042 if (p[i] == '\\' && p[i + 1])
5043 ++i;
5044 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5045 --p;
5046 }
5047}
5048#endif
5049
5050#endif /* FEAT_CMDHIST */
5051
5052#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5053/*
5054 * Get indices "num1,num2" that specify a range within a list (not a range of
5055 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5056 * Returns OK if parsed successfully, otherwise FAIL.
5057 */
5058 int
5059get_list_range(str, num1, num2)
5060 char_u **str;
5061 int *num1;
5062 int *num2;
5063{
5064 int len;
5065 int first = FALSE;
5066 long num;
5067
5068 *str = skipwhite(*str);
5069 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5070 {
5071 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5072 *str += len;
5073 *num1 = (int)num;
5074 first = TRUE;
5075 }
5076 *str = skipwhite(*str);
5077 if (**str == ',') /* parse "to" part of range */
5078 {
5079 *str = skipwhite(*str + 1);
5080 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5081 if (len > 0)
5082 {
5083 *num2 = (int)num;
5084 *str = skipwhite(*str + len);
5085 }
5086 else if (!first) /* no number given at all */
5087 return FAIL;
5088 }
5089 else if (first) /* only one number given */
5090 *num2 = *num1;
5091 return OK;
5092}
5093#endif
5094
5095#if defined(FEAT_CMDHIST) || defined(PROTO)
5096/*
5097 * :history command - print a history
5098 */
5099 void
5100ex_history(eap)
5101 exarg_T *eap;
5102{
5103 histentry_T *hist;
5104 int histype1 = HIST_CMD;
5105 int histype2 = HIST_CMD;
5106 int hisidx1 = 1;
5107 int hisidx2 = -1;
5108 int idx;
5109 int i, j, k;
5110 char_u *end;
5111 char_u *arg = eap->arg;
5112
5113 if (hislen == 0)
5114 {
5115 MSG(_("'history' option is zero"));
5116 return;
5117 }
5118
5119 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5120 {
5121 end = arg;
5122 while (ASCII_ISALPHA(*end)
5123 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5124 end++;
5125 i = *end;
5126 *end = NUL;
5127 histype1 = get_histtype(arg);
5128 if (histype1 == -1)
5129 {
5130 if (STRICMP(arg, "all") == 0)
5131 {
5132 histype1 = 0;
5133 histype2 = HIST_COUNT-1;
5134 }
5135 else
5136 {
5137 *end = i;
5138 EMSG(_(e_trailing));
5139 return;
5140 }
5141 }
5142 else
5143 histype2 = histype1;
5144 *end = i;
5145 }
5146 else
5147 end = arg;
5148 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5149 {
5150 EMSG(_(e_trailing));
5151 return;
5152 }
5153
5154 for (; !got_int && histype1 <= histype2; ++histype1)
5155 {
5156 STRCPY(IObuff, "\n # ");
5157 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5158 MSG_PUTS_TITLE(IObuff);
5159 idx = hisidx[histype1];
5160 hist = history[histype1];
5161 j = hisidx1;
5162 k = hisidx2;
5163 if (j < 0)
5164 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5165 if (k < 0)
5166 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5167 if (idx >= 0 && j <= k)
5168 for (i = idx + 1; !got_int; ++i)
5169 {
5170 if (i == hislen)
5171 i = 0;
5172 if (hist[i].hisstr != NULL
5173 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5174 {
5175 msg_putchar('\n');
5176 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5177 hist[i].hisnum);
5178 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5179 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5180 (int)Columns - 10);
5181 else
5182 STRCAT(IObuff, hist[i].hisstr);
5183 msg_outtrans(IObuff);
5184 out_flush();
5185 }
5186 if (i == idx)
5187 break;
5188 }
5189 }
5190}
5191#endif
5192
5193#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5194static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5195static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5196static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5197static int viminfo_add_at_front = FALSE;
5198
5199static int hist_type2char __ARGS((int type, int use_question));
5200
5201/*
5202 * Translate a history type number to the associated character.
5203 */
5204 static int
5205hist_type2char(type, use_question)
5206 int type;
5207 int use_question; /* use '?' instead of '/' */
5208{
5209 if (type == HIST_CMD)
5210 return ':';
5211 if (type == HIST_SEARCH)
5212 {
5213 if (use_question)
5214 return '?';
5215 else
5216 return '/';
5217 }
5218 if (type == HIST_EXPR)
5219 return '=';
5220 return '@';
5221}
5222
5223/*
5224 * Prepare for reading the history from the viminfo file.
5225 * This allocates history arrays to store the read history lines.
5226 */
5227 void
5228prepare_viminfo_history(asklen)
5229 int asklen;
5230{
5231 int i;
5232 int num;
5233 int type;
5234 int len;
5235
5236 init_history();
5237 viminfo_add_at_front = (asklen != 0);
5238 if (asklen > hislen)
5239 asklen = hislen;
5240
5241 for (type = 0; type < HIST_COUNT; ++type)
5242 {
5243 /*
5244 * Count the number of empty spaces in the history list. If there are
5245 * more spaces available than we request, then fill them up.
5246 */
5247 for (i = 0, num = 0; i < hislen; i++)
5248 if (history[type][i].hisstr == NULL)
5249 num++;
5250 len = asklen;
5251 if (num > len)
5252 len = num;
5253 if (len <= 0)
5254 viminfo_history[type] = NULL;
5255 else
5256 viminfo_history[type] =
5257 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5258 if (viminfo_history[type] == NULL)
5259 len = 0;
5260 viminfo_hislen[type] = len;
5261 viminfo_hisidx[type] = 0;
5262 }
5263}
5264
5265/*
5266 * Accept a line from the viminfo, store it in the history array when it's
5267 * new.
5268 */
5269 int
5270read_viminfo_history(virp)
5271 vir_T *virp;
5272{
5273 int type;
5274 long_u len;
5275 char_u *val;
5276 char_u *p;
5277
5278 type = hist_char2type(virp->vir_line[0]);
5279 if (viminfo_hisidx[type] < viminfo_hislen[type])
5280 {
5281 val = viminfo_readstring(virp, 1, TRUE);
5282 if (val != NULL && *val != NUL)
5283 {
5284 if (!in_history(type, val + (type == HIST_SEARCH),
5285 viminfo_add_at_front))
5286 {
5287 /* Need to re-allocate to append the separator byte. */
5288 len = STRLEN(val);
5289 p = lalloc(len + 2, TRUE);
5290 if (p != NULL)
5291 {
5292 if (type == HIST_SEARCH)
5293 {
5294 /* Search entry: Move the separator from the first
5295 * column to after the NUL. */
5296 mch_memmove(p, val + 1, (size_t)len);
5297 p[len] = (*val == ' ' ? NUL : *val);
5298 }
5299 else
5300 {
5301 /* Not a search entry: No separator in the viminfo
5302 * file, add a NUL separator. */
5303 mch_memmove(p, val, (size_t)len + 1);
5304 p[len + 1] = NUL;
5305 }
5306 viminfo_history[type][viminfo_hisidx[type]++] = p;
5307 }
5308 }
5309 }
5310 vim_free(val);
5311 }
5312 return viminfo_readline(virp);
5313}
5314
5315 void
5316finish_viminfo_history()
5317{
5318 int idx;
5319 int i;
5320 int type;
5321
5322 for (type = 0; type < HIST_COUNT; ++type)
5323 {
5324 if (history[type] == NULL)
5325 return;
5326 idx = hisidx[type] + viminfo_hisidx[type];
5327 if (idx >= hislen)
5328 idx -= hislen;
5329 else if (idx < 0)
5330 idx = hislen - 1;
5331 if (viminfo_add_at_front)
5332 hisidx[type] = idx;
5333 else
5334 {
5335 if (hisidx[type] == -1)
5336 hisidx[type] = hislen - 1;
5337 do
5338 {
5339 if (history[type][idx].hisstr != NULL)
5340 break;
5341 if (++idx == hislen)
5342 idx = 0;
5343 } while (idx != hisidx[type]);
5344 if (idx != hisidx[type] && --idx < 0)
5345 idx = hislen - 1;
5346 }
5347 for (i = 0; i < viminfo_hisidx[type]; i++)
5348 {
5349 vim_free(history[type][idx].hisstr);
5350 history[type][idx].hisstr = viminfo_history[type][i];
5351 if (--idx < 0)
5352 idx = hislen - 1;
5353 }
5354 idx += 1;
5355 idx %= hislen;
5356 for (i = 0; i < viminfo_hisidx[type]; i++)
5357 {
5358 history[type][idx++].hisnum = ++hisnum[type];
5359 idx %= hislen;
5360 }
5361 vim_free(viminfo_history[type]);
5362 viminfo_history[type] = NULL;
5363 }
5364}
5365
5366 void
5367write_viminfo_history(fp)
5368 FILE *fp;
5369{
5370 int i;
5371 int type;
5372 int num_saved;
5373 char_u *p;
5374 int c;
5375
5376 init_history();
5377 if (hislen == 0)
5378 return;
5379 for (type = 0; type < HIST_COUNT; ++type)
5380 {
5381 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5382 if (num_saved == 0)
5383 continue;
5384 if (num_saved < 0) /* Use default */
5385 num_saved = hislen;
5386 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5387 type == HIST_CMD ? _("Command Line") :
5388 type == HIST_SEARCH ? _("Search String") :
5389 type == HIST_EXPR ? _("Expression") :
5390 _("Input Line"));
5391 if (num_saved > hislen)
5392 num_saved = hislen;
5393 i = hisidx[type];
5394 if (i >= 0)
5395 while (num_saved--)
5396 {
5397 p = history[type][i].hisstr;
5398 if (p != NULL)
5399 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005400 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 /* For the search history: put the separator in the second
5402 * column; use a space if there isn't one. */
5403 if (type == HIST_SEARCH)
5404 {
5405 c = p[STRLEN(p) + 1];
5406 putc(c == NUL ? ' ' : c, fp);
5407 }
5408 viminfo_writestring(fp, p);
5409 }
5410 if (--i < 0)
5411 i = hislen - 1;
5412 }
5413 }
5414}
5415#endif /* FEAT_VIMINFO */
5416
5417#if defined(FEAT_FKMAP) || defined(PROTO)
5418/*
5419 * Write a character at the current cursor+offset position.
5420 * It is directly written into the command buffer block.
5421 */
5422 void
5423cmd_pchar(c, offset)
5424 int c, offset;
5425{
5426 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5427 {
5428 EMSG(_("E198: cmd_pchar beyond the command length"));
5429 return;
5430 }
5431 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5432 ccline.cmdbuff[ccline.cmdlen] = NUL;
5433}
5434
5435 int
5436cmd_gchar(offset)
5437 int offset;
5438{
5439 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5440 {
5441 /* EMSG(_("cmd_gchar beyond the command length")); */
5442 return NUL;
5443 }
5444 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5445}
5446#endif
5447
5448#if defined(FEAT_CMDWIN) || defined(PROTO)
5449/*
5450 * Open a window on the current command line and history. Allow editing in
5451 * the window. Returns when the window is closed.
5452 * Returns:
5453 * CR if the command is to be executed
5454 * Ctrl_C if it is to be abandoned
5455 * K_IGNORE if editing continues
5456 */
5457 static int
5458ex_window()
5459{
5460 struct cmdline_info save_ccline;
5461 buf_T *old_curbuf = curbuf;
5462 win_T *old_curwin = curwin;
5463 buf_T *bp;
5464 win_T *wp;
5465 int i;
5466 linenr_T lnum;
5467 int histtype;
5468 garray_T winsizes;
5469 char_u typestr[2];
5470 int save_restart_edit = restart_edit;
5471 int save_State = State;
5472 int save_exmode = exmode_active;
5473
5474 /* Can't do this recursively. Can't do it when typing a password. */
5475 if (cmdwin_type != 0
5476# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5477 || cmdline_star > 0
5478# endif
5479 )
5480 {
5481 beep_flush();
5482 return K_IGNORE;
5483 }
5484
5485 /* Save current window sizes. */
5486 win_size_save(&winsizes);
5487
5488# ifdef FEAT_AUTOCMD
5489 /* Don't execute autocommands while creating the window. */
5490 ++autocmd_block;
5491# endif
5492 /* Create a window for the command-line buffer. */
5493 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5494 {
5495 beep_flush();
5496 return K_IGNORE;
5497 }
5498 cmdwin_type = ccline.cmdfirstc;
5499 if (cmdwin_type == NUL)
5500 cmdwin_type = '-';
5501
5502 /* Create the command-line buffer empty. */
5503 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5504 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5505 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5506 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5507 curbuf->b_p_ma = TRUE;
5508# ifdef FEAT_RIGHTLEFT
5509 curwin->w_p_rl = FALSE;
5510# endif
5511# ifdef FEAT_SCROLLBIND
5512 curwin->w_p_scb = FALSE;
5513# endif
5514
5515# ifdef FEAT_AUTOCMD
5516 /* Do execute autocommands for setting the filetype (load syntax). */
5517 --autocmd_block;
5518# endif
5519
5520 histtype = hist_char2type(ccline.cmdfirstc);
5521 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5522 {
5523 if (p_wc == TAB)
5524 {
5525 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5526 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5527 }
5528 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5529 }
5530
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005531 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5532 * sets 'textwidth' to 78). */
5533 curbuf->b_p_tw = 0;
5534
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 /* Fill the buffer with the history. */
5536 init_history();
5537 if (hislen > 0)
5538 {
5539 i = hisidx[histtype];
5540 if (i >= 0)
5541 {
5542 lnum = 0;
5543 do
5544 {
5545 if (++i == hislen)
5546 i = 0;
5547 if (history[histtype][i].hisstr != NULL)
5548 ml_append(lnum++, history[histtype][i].hisstr,
5549 (colnr_T)0, FALSE);
5550 }
5551 while (i != hisidx[histtype]);
5552 }
5553 }
5554
5555 /* Replace the empty last line with the current command-line and put the
5556 * cursor there. */
5557 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5558 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5559 curwin->w_cursor.col = ccline.cmdpos;
5560 redraw_later(NOT_VALID);
5561
5562 /* Save the command line info, can be used recursively. */
5563 save_ccline = ccline;
5564 ccline.cmdbuff = NULL;
5565 ccline.cmdprompt = NULL;
5566
5567 /* No Ex mode here! */
5568 exmode_active = 0;
5569
5570 State = NORMAL;
5571# ifdef FEAT_MOUSE
5572 setmouse();
5573# endif
5574
5575# ifdef FEAT_AUTOCMD
5576 /* Trigger CmdwinEnter autocommands. */
5577 typestr[0] = cmdwin_type;
5578 typestr[1] = NUL;
5579 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5580# endif
5581
5582 i = RedrawingDisabled;
5583 RedrawingDisabled = 0;
5584
5585 /*
5586 * Call the main loop until <CR> or CTRL-C is typed.
5587 */
5588 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005589 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
5591 RedrawingDisabled = i;
5592
5593# ifdef FEAT_AUTOCMD
5594 /* Trigger CmdwinLeave autocommands. */
5595 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5596# endif
5597
5598 /* Restore the comand line info. */
5599 ccline = save_ccline;
5600 cmdwin_type = 0;
5601
5602 exmode_active = save_exmode;
5603
5604 /* Safety check: The old window or buffer was deleted: It's a a bug when
5605 * this happens! */
5606 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5607 {
5608 cmdwin_result = Ctrl_C;
5609 EMSG(_("E199: Active window or buffer deleted"));
5610 }
5611 else
5612 {
5613# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5614 /* autocmds may abort script processing */
5615 if (aborting() && cmdwin_result != K_IGNORE)
5616 cmdwin_result = Ctrl_C;
5617# endif
5618 /* Set the new command line from the cmdline buffer. */
5619 vim_free(ccline.cmdbuff);
5620 if (cmdwin_result == K_XF1) /* :qa! typed */
5621 {
5622 ccline.cmdbuff = vim_strsave((char_u *)"qa!");
5623 cmdwin_result = CAR;
5624 }
5625 else if (cmdwin_result == K_XF2) /* :qa typed */
5626 {
5627 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5628 cmdwin_result = CAR;
5629 }
5630 else
5631 ccline.cmdbuff = vim_strsave(ml_get_curline());
5632 if (ccline.cmdbuff == NULL)
5633 cmdwin_result = Ctrl_C;
5634 else
5635 {
5636 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5637 ccline.cmdbufflen = ccline.cmdlen + 1;
5638 ccline.cmdpos = curwin->w_cursor.col;
5639 if (ccline.cmdpos > ccline.cmdlen)
5640 ccline.cmdpos = ccline.cmdlen;
5641 if (cmdwin_result == K_IGNORE)
5642 {
5643 set_cmdspos_cursor();
5644 redrawcmd();
5645 }
5646 }
5647
5648# ifdef FEAT_AUTOCMD
5649 /* Don't execute autocommands while deleting the window. */
5650 ++autocmd_block;
5651# endif
5652 wp = curwin;
5653 bp = curbuf;
5654 win_goto(old_curwin);
5655 win_close(wp, TRUE);
5656 close_buffer(NULL, bp, DOBUF_WIPE);
5657
5658 /* Restore window sizes. */
5659 win_size_restore(&winsizes);
5660
5661# ifdef FEAT_AUTOCMD
5662 --autocmd_block;
5663# endif
5664 }
5665
5666 ga_clear(&winsizes);
5667 restart_edit = save_restart_edit;
5668
5669 State = save_State;
5670# ifdef FEAT_MOUSE
5671 setmouse();
5672# endif
5673
5674 return cmdwin_result;
5675}
5676#endif /* FEAT_CMDWIN */
5677
5678/*
5679 * Used for commands that either take a simple command string argument, or:
5680 * cmd << endmarker
5681 * {script}
5682 * endmarker
5683 * Returns a pointer to allocated memory with {script} or NULL.
5684 */
5685 char_u *
5686script_get(eap, cmd)
5687 exarg_T *eap;
5688 char_u *cmd;
5689{
5690 char_u *theline;
5691 char *end_pattern = NULL;
5692 char dot[] = ".";
5693 garray_T ga;
5694
5695 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5696 return NULL;
5697
5698 ga_init2(&ga, 1, 0x400);
5699
5700 if (cmd[2] != NUL)
5701 end_pattern = (char *)skipwhite(cmd + 2);
5702 else
5703 end_pattern = dot;
5704
5705 for (;;)
5706 {
5707 theline = eap->getline(
5708#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005709 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710#endif
5711 NUL, eap->cookie, 0);
5712
5713 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5714 break;
5715
5716 ga_concat(&ga, theline);
5717 ga_append(&ga, '\n');
5718 vim_free(theline);
5719 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005720 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721
5722 return (char_u *)ga.ga_data;
5723}