blob: a876508c0b4060fd1eccd28faae167b697f0f6de [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)
884 p += (*mb_ptr2len_check)(p);
885 }
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)
1118 ccline.cmdpos += (*mb_ptr2len_check)(ccline.cmdbuff
1119 + 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);
1244 ccline.cmdpos += (*mb_ptr2len_check)(ccline.cmdbuff
1245 + 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 {
1650 /*
1651 * First move cursor to end of match, then to start. This
1652 * moves the whole match onto the screen when 'nowrap' is set.
1653 */
1654 i = curwin->w_cursor.col;
1655 curwin->w_cursor.lnum += search_match_lines;
1656 curwin->w_cursor.col = search_match_endcol;
1657 validate_cursor();
1658 curwin->w_cursor.lnum -= search_match_lines;
1659 curwin->w_cursor.col = i;
1660 }
1661 validate_cursor();
1662
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001663 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 update_screen(NOT_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001665 restore_cmdline(&save_ccline);
1666
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 msg_starthere();
1668 redrawcmdline();
1669 did_incsearch = TRUE;
1670 }
1671#else /* FEAT_SEARCH_EXTRA */
1672 ;
1673#endif
1674
1675#ifdef FEAT_RIGHTLEFT
1676 if (cmdmsg_rl
1677# ifdef FEAT_ARABIC
1678 || p_arshape
1679# endif
1680 )
1681 /* Always redraw the whole command line to fix shaping and
1682 * right-left typing. Not efficient, but it works. */
1683 redrawcmd();
1684#endif
1685 }
1686
1687returncmd:
1688
1689#ifdef FEAT_RIGHTLEFT
1690 cmdmsg_rl = FALSE;
1691#endif
1692
1693#ifdef FEAT_FKMAP
1694 cmd_fkmap = 0;
1695#endif
1696
1697 ExpandCleanup(&xpc);
1698
1699#ifdef FEAT_SEARCH_EXTRA
1700 if (did_incsearch)
1701 {
1702 curwin->w_cursor = old_cursor;
1703 curwin->w_curswant = old_curswant;
1704 curwin->w_leftcol = old_leftcol;
1705 curwin->w_topline = old_topline;
1706# ifdef FEAT_DIFF
1707 curwin->w_topfill = old_topfill;
1708# endif
1709 curwin->w_botline = old_botline;
1710 highlight_match = FALSE;
1711 validate_cursor(); /* needed for TAB */
1712 redraw_later(NOT_VALID);
1713 }
1714#endif
1715
1716 if (ccline.cmdbuff != NULL)
1717 {
1718 /*
1719 * Put line in history buffer (":" and "=" only when it was typed).
1720 */
1721#ifdef FEAT_CMDHIST
1722 if (ccline.cmdlen && firstc != NUL
1723 && (some_key_typed || histype == HIST_SEARCH))
1724 {
1725 add_to_history(histype, ccline.cmdbuff, TRUE,
1726 histype == HIST_SEARCH ? firstc : NUL);
1727 if (firstc == ':')
1728 {
1729 vim_free(new_last_cmdline);
1730 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1731 }
1732 }
1733#endif
1734
1735 if (gotesc) /* abandon command line */
1736 {
1737 vim_free(ccline.cmdbuff);
1738 ccline.cmdbuff = NULL;
1739 if (msg_scrolled == 0)
1740 compute_cmdrow();
1741 MSG("");
1742 redraw_cmdline = TRUE;
1743 }
1744 }
1745
1746 /*
1747 * If the screen was shifted up, redraw the whole screen (later).
1748 * If the line is too long, clear it, so ruler and shown command do
1749 * not get printed in the middle of it.
1750 */
1751 msg_check();
1752 msg_scroll = save_msg_scroll;
1753 redir_off = FALSE;
1754
1755 /* When the command line was typed, no need for a wait-return prompt. */
1756 if (some_key_typed)
1757 need_wait_return = FALSE;
1758
1759 State = save_State;
1760#ifdef USE_IM_CONTROL
1761 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1762 im_save_status(b_im_ptr);
1763 im_set_active(FALSE);
1764#endif
1765#ifdef FEAT_MOUSE
1766 setmouse();
1767#endif
1768#ifdef CURSOR_SHAPE
1769 ui_cursor_shape(); /* may show different cursor shape */
1770#endif
1771
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001772 {
1773 char_u *p = ccline.cmdbuff;
1774
1775 /* Make ccline empty, getcmdline() may try to use it. */
1776 ccline.cmdbuff = NULL;
1777 return p;
1778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779}
1780
1781#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1782/*
1783 * Get a command line with a prompt.
1784 * This is prepared to be called recursively from getcmdline() (e.g. by
1785 * f_input() when evaluating an expression from CTRL-R =).
1786 * Returns the command line in allocated memory, or NULL.
1787 */
1788 char_u *
1789getcmdline_prompt(firstc, prompt, attr)
1790 int firstc;
1791 char_u *prompt; /* command line prompt */
1792 int attr; /* attributes for prompt */
1793{
1794 char_u *s;
1795 struct cmdline_info save_ccline;
1796 int msg_col_save = msg_col;
1797
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001798 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 ccline.cmdprompt = prompt;
1800 ccline.cmdattr = attr;
1801 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001802 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 /* Restore msg_col, the prompt from input() may have changed it. */
1804 msg_col = msg_col_save;
1805
1806 return s;
1807}
1808#endif
1809
1810 static int
1811cmdline_charsize(idx)
1812 int idx;
1813{
1814#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1815 if (cmdline_star > 0) /* showing '*', always 1 position */
1816 return 1;
1817#endif
1818 return ptr2cells(ccline.cmdbuff + idx);
1819}
1820
1821/*
1822 * Compute the offset of the cursor on the command line for the prompt and
1823 * indent.
1824 */
1825 static void
1826set_cmdspos()
1827{
1828 if (ccline.cmdfirstc)
1829 ccline.cmdspos = 1 + ccline.cmdindent;
1830 else
1831 ccline.cmdspos = 0 + ccline.cmdindent;
1832}
1833
1834/*
1835 * Compute the screen position for the cursor on the command line.
1836 */
1837 static void
1838set_cmdspos_cursor()
1839{
1840 int i, m, c;
1841
1842 set_cmdspos();
1843 if (KeyTyped)
1844 m = Columns * Rows;
1845 else
1846 m = MAXCOL;
1847 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1848 {
1849 c = cmdline_charsize(i);
1850#ifdef FEAT_MBYTE
1851 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1852 if (has_mbyte)
1853 correct_cmdspos(i, c);
1854#endif
1855 /* If the cmdline doesn't fit, put cursor on last visible char. */
1856 if ((ccline.cmdspos += c) >= m)
1857 {
1858 ccline.cmdpos = i - 1;
1859 ccline.cmdspos -= c;
1860 break;
1861 }
1862#ifdef FEAT_MBYTE
1863 if (has_mbyte)
1864 i += (*mb_ptr2len_check)(ccline.cmdbuff + i) - 1;
1865#endif
1866 }
1867}
1868
1869#ifdef FEAT_MBYTE
1870/*
1871 * Check if the character at "idx", which is "cells" wide, is a multi-byte
1872 * character that doesn't fit, so that a ">" must be displayed.
1873 */
1874 static void
1875correct_cmdspos(idx, cells)
1876 int idx;
1877 int cells;
1878{
1879 if ((*mb_ptr2len_check)(ccline.cmdbuff + idx) > 1
1880 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
1881 && ccline.cmdspos % Columns + cells > Columns)
1882 ccline.cmdspos++;
1883}
1884#endif
1885
1886/*
1887 * Get an Ex command line for the ":" command.
1888 */
1889/* ARGSUSED */
1890 char_u *
1891getexline(c, dummy, indent)
1892 int c; /* normally ':', NUL for ":append" */
1893 void *dummy; /* cookie not used */
1894 int indent; /* indent for inside conditionals */
1895{
1896 /* When executing a register, remove ':' that's in front of each line. */
1897 if (exec_from_reg && vpeekc() == ':')
1898 (void)vgetc();
1899 return getcmdline(c, 1L, indent);
1900}
1901
1902/*
1903 * Get an Ex command line for Ex mode.
1904 * In Ex mode we only use the OS supplied line editing features and no
1905 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001906 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 */
1908/* ARGSUSED */
1909 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001910getexmodeline(promptc, dummy, indent)
1911 int promptc; /* normally ':', NUL for ":append" and '?' for
1912 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 void *dummy; /* cookie not used */
1914 int indent; /* indent for inside conditionals */
1915{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001916 garray_T line_ga;
1917 char_u *pend;
1918 int startcol = 0;
1919 int c1;
1920 int escaped = FALSE; /* CTRL-V typed */
1921 int vcol = 0;
1922 char_u *p;
1923 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925 /* Switch cursor on now. This avoids that it happens after the "\n", which
1926 * confuses the system function that computes tabstops. */
1927 cursor_on();
1928
1929 /* always start in column 0; write a newline if necessary */
1930 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001931 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001933 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001935 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001936 if (p_prompt)
1937 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 while (indent-- > 0)
1939 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
1942
1943 ga_init2(&line_ga, 1, 30);
1944
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001945 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001946 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001947 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001948 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001949 while (indent >= 8)
1950 {
1951 ga_append(&line_ga, TAB);
1952 msg_puts((char_u *)" ");
1953 indent -= 8;
1954 }
1955 while (indent-- > 0)
1956 {
1957 ga_append(&line_ga, ' ');
1958 msg_putchar(' ');
1959 }
1960 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001961 ++no_mapping;
1962 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 /*
1965 * Get the line, one character at a time.
1966 */
1967 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001968 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
1970 if (ga_grow(&line_ga, 40) == FAIL)
1971 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001972 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001974 /* Get one character at a time. Don't use inchar(), it can't handle
1975 * special characters. */
1976 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
1978 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001979 * Handle line editing.
1980 * Previously this was left to the system, putting the terminal in
1981 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001983 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001985 msg_putchar('\n');
1986 break;
1987 }
1988
1989 if (!escaped)
1990 {
1991 /* CR typed means "enter", which is NL */
1992 if (c1 == '\r')
1993 c1 = '\n';
1994
1995 if (c1 == BS || c1 == K_BS
1996 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001998 if (line_ga.ga_len > 0)
1999 {
2000 --line_ga.ga_len;
2001 goto redraw;
2002 }
2003 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 }
2005
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002006 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002008 msg_col = startcol;
2009 msg_clr_eos();
2010 line_ga.ga_len = 0;
2011 continue;
2012 }
2013
2014 if (c1 == Ctrl_T)
2015 {
2016 p = (char_u *)line_ga.ga_data;
2017 p[line_ga.ga_len] = NUL;
2018 indent = get_indent_str(p, 8);
2019 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2020add_indent:
2021 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002023 char_u *s = skipwhite(p);
2024
2025 ga_grow(&line_ga, 1);
2026 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2027 *s = ' ';
2028 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002030redraw:
2031 /* redraw the line */
2032 msg_col = startcol;
2033 windgoto(msg_row, msg_col);
2034 vcol = 0;
2035 for (p = (char_u *)line_ga.ga_data;
2036 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002038 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002040 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002042 msg_putchar(' ');
2043 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002045 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002047 msg_outtrans_len(p, 1);
2048 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 }
2050 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002051 msg_clr_eos();
2052 continue;
2053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002055 if (c1 == Ctrl_D)
2056 {
2057 /* Delete one shiftwidth. */
2058 p = (char_u *)line_ga.ga_data;
2059 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002061 if (prev_char == '^')
2062 ex_keep_indent = TRUE;
2063 indent = 0;
2064 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 }
2066 else
2067 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002068 p[line_ga.ga_len] = NUL;
2069 indent = get_indent_str(p, 8);
2070 --indent;
2071 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002073 while (get_indent_str(p, 8) > indent)
2074 {
2075 char_u *s = skipwhite(p);
2076
2077 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2078 --line_ga.ga_len;
2079 }
2080 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002082
2083 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2084 {
2085 escaped = TRUE;
2086 continue;
2087 }
2088
2089 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2090 if (IS_SPECIAL(c1))
2091 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002093
2094 if (IS_SPECIAL(c1))
2095 c1 = '?';
2096 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2097 prev_char = c1;
2098 if (c1 == '\n')
2099 msg_putchar('\n');
2100 else if (c1 == TAB)
2101 {
2102 /* Don't use chartabsize(), 'ts' can be different */
2103 do
2104 {
2105 msg_putchar(' ');
2106 } while (++vcol % 8);
2107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110 msg_outtrans_len(
2111 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2112 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002114 ++line_ga.ga_len;
2115 escaped = FALSE;
2116
2117 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002118 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002119
2120 /* we are done when a NL is entered, but not when it comes after a
2121 * backslash */
2122 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2123 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002126 --pend;
2127 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002128 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
2130 }
2131
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002132 --no_mapping;
2133 --allow_keys;
2134
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 /* make following messages go to the next line */
2136 msg_didout = FALSE;
2137 msg_col = 0;
2138 if (msg_row < Rows - 1)
2139 ++msg_row;
2140 emsg_on_display = FALSE; /* don't want ui_delay() */
2141
2142 if (got_int)
2143 ga_clear(&line_ga);
2144
2145 return (char_u *)line_ga.ga_data;
2146}
2147
2148#ifdef CURSOR_SHAPE
2149/*
2150 * Return TRUE if ccline.overstrike is on.
2151 */
2152 int
2153cmdline_overstrike()
2154{
2155 return ccline.overstrike;
2156}
2157
2158/*
2159 * Return TRUE if the cursor is at the end of the cmdline.
2160 */
2161 int
2162cmdline_at_end()
2163{
2164 return (ccline.cmdpos >= ccline.cmdlen);
2165}
2166#endif
2167
Bram Moolenaar81695252004-12-29 20:58:21 +00002168#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE))) \
2169 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170/*
2171 * Return the virtual column number at the current cursor position.
2172 * This is used by the IM code to obtain the start of the preedit string.
2173 */
2174 colnr_T
2175cmdline_getvcol_cursor()
2176{
2177 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2178 return MAXCOL;
2179
2180# ifdef FEAT_MBYTE
2181 if (has_mbyte)
2182 {
2183 colnr_T col;
2184 int i = 0;
2185
2186 for (col = 0; i < ccline.cmdpos; ++col)
2187 i += (*mb_ptr2len_check)(ccline.cmdbuff + i);
2188
2189 return col;
2190 }
2191 else
2192# endif
2193 return ccline.cmdpos;
2194}
2195#endif
2196
2197#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2198/*
2199 * If part of the command line is an IM preedit string, redraw it with
2200 * IM feedback attributes. The cursor position is restored after drawing.
2201 */
2202 static void
2203redrawcmd_preedit()
2204{
2205 if ((State & CMDLINE)
2206 && xic != NULL
2207 && im_get_status()
2208 && !p_imdisable
2209 && im_is_preediting())
2210 {
2211 int cmdpos = 0;
2212 int cmdspos;
2213 int old_row;
2214 int old_col;
2215 colnr_T col;
2216
2217 old_row = msg_row;
2218 old_col = msg_col;
2219 cmdspos = ((ccline.cmdfirstc) ? 1 : 0) + ccline.cmdindent;
2220
2221# ifdef FEAT_MBYTE
2222 if (has_mbyte)
2223 {
2224 for (col = 0; col < preedit_start_col
2225 && cmdpos < ccline.cmdlen; ++col)
2226 {
2227 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
2228 cmdpos += (*mb_ptr2len_check)(ccline.cmdbuff + cmdpos);
2229 }
2230 }
2231 else
2232# endif
2233 {
2234 cmdspos += preedit_start_col;
2235 cmdpos += preedit_start_col;
2236 }
2237
2238 msg_row = cmdline_row + (cmdspos / (int)Columns);
2239 msg_col = cmdspos % (int)Columns;
2240 if (msg_row >= Rows)
2241 msg_row = Rows - 1;
2242
2243 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2244 {
2245 int char_len;
2246 int char_attr;
2247
2248 char_attr = im_get_feedback_attr(col);
2249 if (char_attr < 0)
2250 break; /* end of preedit string */
2251
2252# ifdef FEAT_MBYTE
2253 if (has_mbyte)
2254 char_len = (*mb_ptr2len_check)(ccline.cmdbuff + cmdpos);
2255 else
2256# endif
2257 char_len = 1;
2258
2259 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2260 cmdpos += char_len;
2261 }
2262
2263 msg_row = old_row;
2264 msg_col = old_col;
2265 }
2266}
2267#endif /* FEAT_XIM && FEAT_GUI_GTK */
2268
2269/*
2270 * Allocate a new command line buffer.
2271 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2272 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2273 */
2274 static void
2275alloc_cmdbuff(len)
2276 int len;
2277{
2278 /*
2279 * give some extra space to avoid having to allocate all the time
2280 */
2281 if (len < 80)
2282 len = 100;
2283 else
2284 len += 20;
2285
2286 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2287 ccline.cmdbufflen = len;
2288}
2289
2290/*
2291 * Re-allocate the command line to length len + something extra.
2292 * return FAIL for failure, OK otherwise
2293 */
2294 static int
2295realloc_cmdbuff(len)
2296 int len;
2297{
2298 char_u *p;
2299
2300 p = ccline.cmdbuff;
2301 alloc_cmdbuff(len); /* will get some more */
2302 if (ccline.cmdbuff == NULL) /* out of memory */
2303 {
2304 ccline.cmdbuff = p; /* keep the old one */
2305 return FAIL;
2306 }
2307 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2308 vim_free(p);
2309 return OK;
2310}
2311
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002312#if defined(FEAT_ARABIC) || defined(PROTO)
2313static char_u *arshape_buf = NULL;
2314
2315# if defined(EXITFREE) || defined(PROTO)
2316 void
2317free_cmdline_buf()
2318{
2319 vim_free(arshape_buf);
2320}
2321# endif
2322#endif
2323
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324/*
2325 * Draw part of the cmdline at the current cursor position. But draw stars
2326 * when cmdline_star is TRUE.
2327 */
2328 static void
2329draw_cmdline(start, len)
2330 int start;
2331 int len;
2332{
2333#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2334 int i;
2335
2336 if (cmdline_star > 0)
2337 for (i = 0; i < len; ++i)
2338 {
2339 msg_putchar('*');
2340# ifdef FEAT_MBYTE
2341 if (has_mbyte)
2342 i += (*mb_ptr2len_check)(ccline.cmdbuff + start + i) - 1;
2343# endif
2344 }
2345 else
2346#endif
2347#ifdef FEAT_ARABIC
2348 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 static int buflen = 0;
2351 char_u *p;
2352 int j;
2353 int newlen = 0;
2354 int mb_l;
2355 int pc, pc1;
2356 int prev_c = 0;
2357 int prev_c1 = 0;
2358 int u8c, u8c_c1, u8c_c2;
2359 int nc = 0;
2360 int dummy;
2361
2362 /*
2363 * Do arabic shaping into a temporary buffer. This is very
2364 * inefficient!
2365 */
2366 if (len * 2 > buflen)
2367 {
2368 /* Re-allocate the buffer. We keep it around to avoid a lot of
2369 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002370 vim_free(arshape_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 buflen = len * 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002372 arshape_buf = alloc(buflen);
2373 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 return; /* out of memory */
2375 }
2376
2377 for (j = start; j < start + len; j += mb_l)
2378 {
2379 p = ccline.cmdbuff + j;
2380 u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
2381 mb_l = utfc_ptr2len_check_len(p, start + len - j);
2382 if (ARABIC_CHAR(u8c))
2383 {
2384 /* Do Arabic shaping. */
2385 if (cmdmsg_rl)
2386 {
2387 /* displaying from right to left */
2388 pc = prev_c;
2389 pc1 = prev_c1;
2390 prev_c1 = u8c_c1;
2391 if (j + mb_l >= start + len)
2392 nc = NUL;
2393 else
2394 nc = utf_ptr2char(p + mb_l);
2395 }
2396 else
2397 {
2398 /* displaying from left to right */
2399 if (j + mb_l >= start + len)
2400 pc = NUL;
2401 else
2402 pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
2403 start + len - j - mb_l);
2404 nc = prev_c;
2405 }
2406 prev_c = u8c;
2407
2408 u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
2409
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002410 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (u8c_c1 != 0)
2412 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002413 newlen += (*mb_char2bytes)(u8c_c1, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (u8c_c2 != 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002415 newlen += (*mb_char2bytes)(u8c_c2,
2416 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418 }
2419 else
2420 {
2421 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002422 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 newlen += mb_l;
2424 }
2425 }
2426
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002427 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 }
2429 else
2430#endif
2431 msg_outtrans_len(ccline.cmdbuff + start, len);
2432}
2433
2434/*
2435 * Put a character on the command line. Shifts the following text to the
2436 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2437 * "c" must be printable (fit in one display cell)!
2438 */
2439 void
2440putcmdline(c, shift)
2441 int c;
2442 int shift;
2443{
2444 if (cmd_silent)
2445 return;
2446 msg_no_more = TRUE;
2447 msg_putchar(c);
2448 if (shift)
2449 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2450 msg_no_more = FALSE;
2451 cursorcmd();
2452}
2453
2454/*
2455 * Undo a putcmdline(c, FALSE).
2456 */
2457 void
2458unputcmdline()
2459{
2460 if (cmd_silent)
2461 return;
2462 msg_no_more = TRUE;
2463 if (ccline.cmdlen == ccline.cmdpos)
2464 msg_putchar(' ');
2465 else
2466 draw_cmdline(ccline.cmdpos, 1);
2467 msg_no_more = FALSE;
2468 cursorcmd();
2469}
2470
2471/*
2472 * Put the given string, of the given length, onto the command line.
2473 * If len is -1, then STRLEN() is used to calculate the length.
2474 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2475 * part will be redrawn, otherwise it will not. If this function is called
2476 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2477 * called afterwards.
2478 */
2479 int
2480put_on_cmdline(str, len, redraw)
2481 char_u *str;
2482 int len;
2483 int redraw;
2484{
2485 int retval;
2486 int i;
2487 int m;
2488 int c;
2489
2490 if (len < 0)
2491 len = (int)STRLEN(str);
2492
2493 /* Check if ccline.cmdbuff needs to be longer */
2494 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2495 retval = realloc_cmdbuff(ccline.cmdlen + len);
2496 else
2497 retval = OK;
2498 if (retval == OK)
2499 {
2500 if (!ccline.overstrike)
2501 {
2502 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2503 ccline.cmdbuff + ccline.cmdpos,
2504 (size_t)(ccline.cmdlen - ccline.cmdpos));
2505 ccline.cmdlen += len;
2506 }
2507 else
2508 {
2509#ifdef FEAT_MBYTE
2510 if (has_mbyte)
2511 {
2512 /* Count nr of characters in the new string. */
2513 m = 0;
2514 for (i = 0; i < len; i += (*mb_ptr2len_check)(str + i))
2515 ++m;
2516 /* Count nr of bytes in cmdline that are overwritten by these
2517 * characters. */
2518 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
2519 i += (*mb_ptr2len_check)(ccline.cmdbuff + i))
2520 --m;
2521 if (i < ccline.cmdlen)
2522 {
2523 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2524 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2525 ccline.cmdlen += ccline.cmdpos + len - i;
2526 }
2527 else
2528 ccline.cmdlen = ccline.cmdpos + len;
2529 }
2530 else
2531#endif
2532 if (ccline.cmdpos + len > ccline.cmdlen)
2533 ccline.cmdlen = ccline.cmdpos + len;
2534 }
2535 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2536 ccline.cmdbuff[ccline.cmdlen] = NUL;
2537
2538#ifdef FEAT_MBYTE
2539 if (enc_utf8)
2540 {
2541 /* When the inserted text starts with a composing character,
2542 * backup to the character before it. There could be two of them.
2543 */
2544 i = 0;
2545 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2546 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2547 {
2548 i = (*mb_head_off)(ccline.cmdbuff,
2549 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2550 ccline.cmdpos -= i;
2551 len += i;
2552 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2553 }
2554# ifdef FEAT_ARABIC
2555 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2556 {
2557 /* Check the previous character for Arabic combining pair. */
2558 i = (*mb_head_off)(ccline.cmdbuff,
2559 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2560 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2561 + ccline.cmdpos - i), c))
2562 {
2563 ccline.cmdpos -= i;
2564 len += i;
2565 }
2566 else
2567 i = 0;
2568 }
2569# endif
2570 if (i != 0)
2571 {
2572 /* Also backup the cursor position. */
2573 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2574 ccline.cmdspos -= i;
2575 msg_col -= i;
2576 if (msg_col < 0)
2577 {
2578 msg_col += Columns;
2579 --msg_row;
2580 }
2581 }
2582 }
2583#endif
2584
2585 if (redraw && !cmd_silent)
2586 {
2587 msg_no_more = TRUE;
2588 i = cmdline_row;
2589 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2590 /* Avoid clearing the rest of the line too often. */
2591 if (cmdline_row != i || ccline.overstrike)
2592 msg_clr_eos();
2593 msg_no_more = FALSE;
2594 }
2595#ifdef FEAT_FKMAP
2596 /*
2597 * If we are in Farsi command mode, the character input must be in
2598 * Insert mode. So do not advance the cmdpos.
2599 */
2600 if (!cmd_fkmap)
2601#endif
2602 {
2603 if (KeyTyped)
2604 m = Columns * Rows;
2605 else
2606 m = MAXCOL;
2607 for (i = 0; i < len; ++i)
2608 {
2609 c = cmdline_charsize(ccline.cmdpos);
2610#ifdef FEAT_MBYTE
2611 /* count ">" for a double-wide char that doesn't fit. */
2612 if (has_mbyte)
2613 correct_cmdspos(ccline.cmdpos, c);
2614#endif
2615 /* Stop cursor at the end of the screen */
2616 if (ccline.cmdspos + c >= m)
2617 break;
2618 ccline.cmdspos += c;
2619#ifdef FEAT_MBYTE
2620 if (has_mbyte)
2621 {
2622 c = (*mb_ptr2len_check)(ccline.cmdbuff + ccline.cmdpos) - 1;
2623 if (c > len - i - 1)
2624 c = len - i - 1;
2625 ccline.cmdpos += c;
2626 i += c;
2627 }
2628#endif
2629 ++ccline.cmdpos;
2630 }
2631 }
2632 }
2633 if (redraw)
2634 msg_check();
2635 return retval;
2636}
2637
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002638static struct cmdline_info prev_ccline;
2639static int prev_ccline_used = FALSE;
2640
2641/*
2642 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2643 * and overwrite it. But get_cmdline_str() may need it, thus make it
2644 * available globally in prev_ccline.
2645 */
2646 static void
2647save_cmdline(ccp)
2648 struct cmdline_info *ccp;
2649{
2650 if (!prev_ccline_used)
2651 {
2652 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2653 prev_ccline_used = TRUE;
2654 }
2655 *ccp = prev_ccline;
2656 prev_ccline = ccline;
2657 ccline.cmdbuff = NULL;
2658 ccline.cmdprompt = NULL;
2659}
2660
2661/*
2662 * Resture ccline after it has been saved with save_cmdline().
2663 */
2664 static void
2665restore_cmdline(ccp)
2666 struct cmdline_info *ccp;
2667{
2668 ccline = prev_ccline;
2669 prev_ccline = *ccp;
2670}
2671
Bram Moolenaar8299df92004-07-10 09:47:34 +00002672/*
2673 * paste a yank register into the command line.
2674 * used by CTRL-R command in command-line mode
2675 * insert_reg() can't be used here, because special characters from the
2676 * register contents will be interpreted as commands.
2677 *
2678 * return FAIL for failure, OK otherwise
2679 */
2680 static int
2681cmdline_paste(regname, literally)
2682 int regname;
2683 int literally; /* Insert text literally instead of "as typed" */
2684{
2685 long i;
2686 char_u *arg;
2687 int allocated;
2688 struct cmdline_info save_ccline;
2689
2690 /* check for valid regname; also accept special characters for CTRL-R in
2691 * the command line */
2692 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2693 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2694 return FAIL;
2695
2696 /* A register containing CTRL-R can cause an endless loop. Allow using
2697 * CTRL-C to break the loop. */
2698 line_breakcheck();
2699 if (got_int)
2700 return FAIL;
2701
2702#ifdef FEAT_CLIPBOARD
2703 regname = may_get_selection(regname);
2704#endif
2705
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002706 /* Need to save and restore ccline. */
2707 save_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002708 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002709 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002710
2711 if (i)
2712 {
2713 /* Got the value of a special register in "arg". */
2714 if (arg == NULL)
2715 return FAIL;
2716 cmdline_paste_str(arg, literally);
2717 if (allocated)
2718 vim_free(arg);
2719 return OK;
2720 }
2721
2722 return cmdline_paste_reg(regname, literally);
2723}
2724
2725/*
2726 * Put a string on the command line.
2727 * When "literally" is TRUE, insert literally.
2728 * When "literally" is FALSE, insert as typed, but don't leave the command
2729 * line.
2730 */
2731 void
2732cmdline_paste_str(s, literally)
2733 char_u *s;
2734 int literally;
2735{
2736 int c, cv;
2737
2738 if (literally)
2739 put_on_cmdline(s, -1, TRUE);
2740 else
2741 while (*s != NUL)
2742 {
2743 cv = *s;
2744 if (cv == Ctrl_V && s[1])
2745 ++s;
2746#ifdef FEAT_MBYTE
2747 if (has_mbyte)
2748 {
2749 c = mb_ptr2char(s);
2750 s += mb_char2len(c);
2751 }
2752 else
2753#endif
2754 c = *s++;
2755 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2756#ifdef UNIX
2757 || c == intr_char
2758#endif
2759 || (c == Ctrl_BSL && *s == Ctrl_N))
2760 stuffcharReadbuff(Ctrl_V);
2761 stuffcharReadbuff(c);
2762 }
2763}
2764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765#ifdef FEAT_WILDMENU
2766/*
2767 * Delete characters on the command line, from "from" to the current
2768 * position.
2769 */
2770 static void
2771cmdline_del(from)
2772 int from;
2773{
2774 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2775 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2776 ccline.cmdlen -= ccline.cmdpos - from;
2777 ccline.cmdpos = from;
2778}
2779#endif
2780
2781/*
2782 * this fuction is called when the screen size changes and with incremental
2783 * search
2784 */
2785 void
2786redrawcmdline()
2787{
2788 if (cmd_silent)
2789 return;
2790 need_wait_return = FALSE;
2791 compute_cmdrow();
2792 redrawcmd();
2793 cursorcmd();
2794}
2795
2796 static void
2797redrawcmdprompt()
2798{
2799 int i;
2800
2801 if (cmd_silent)
2802 return;
2803 if (ccline.cmdfirstc)
2804 msg_putchar(ccline.cmdfirstc);
2805 if (ccline.cmdprompt != NULL)
2806 {
2807 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2808 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2809 /* do the reverse of set_cmdspos() */
2810 if (ccline.cmdfirstc)
2811 --ccline.cmdindent;
2812 }
2813 else
2814 for (i = ccline.cmdindent; i > 0; --i)
2815 msg_putchar(' ');
2816}
2817
2818/*
2819 * Redraw what is currently on the command line.
2820 */
2821 void
2822redrawcmd()
2823{
2824 if (cmd_silent)
2825 return;
2826
2827 msg_start();
2828 redrawcmdprompt();
2829
2830 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2831 msg_no_more = TRUE;
2832 draw_cmdline(0, ccline.cmdlen);
2833 msg_clr_eos();
2834 msg_no_more = FALSE;
2835
2836 set_cmdspos_cursor();
2837
2838 /*
2839 * An emsg() before may have set msg_scroll. This is used in normal mode,
2840 * in cmdline mode we can reset them now.
2841 */
2842 msg_scroll = FALSE; /* next message overwrites cmdline */
2843
2844 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2845 * in cmdline mode */
2846 skip_redraw = FALSE;
2847}
2848
2849 void
2850compute_cmdrow()
2851{
2852 if (exmode_active || msg_scrolled)
2853 cmdline_row = Rows - 1;
2854 else
2855 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2856 + W_STATUS_HEIGHT(lastwin);
2857}
2858
2859 static void
2860cursorcmd()
2861{
2862 if (cmd_silent)
2863 return;
2864
2865#ifdef FEAT_RIGHTLEFT
2866 if (cmdmsg_rl)
2867 {
2868 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2869 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2870 if (msg_row <= 0)
2871 msg_row = Rows - 1;
2872 }
2873 else
2874#endif
2875 {
2876 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
2877 msg_col = ccline.cmdspos % (int)Columns;
2878 if (msg_row >= Rows)
2879 msg_row = Rows - 1;
2880 }
2881
2882 windgoto(msg_row, msg_col);
2883#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2884 redrawcmd_preedit();
2885#endif
2886#ifdef MCH_CURSOR_SHAPE
2887 mch_update_cursor();
2888#endif
2889}
2890
2891 void
2892gotocmdline(clr)
2893 int clr;
2894{
2895 msg_start();
2896#ifdef FEAT_RIGHTLEFT
2897 if (cmdmsg_rl)
2898 msg_col = Columns - 1;
2899 else
2900#endif
2901 msg_col = 0; /* always start in column 0 */
2902 if (clr) /* clear the bottom line(s) */
2903 msg_clr_eos(); /* will reset clear_cmdline */
2904 windgoto(cmdline_row, 0);
2905}
2906
2907/*
2908 * Check the word in front of the cursor for an abbreviation.
2909 * Called when the non-id character "c" has been entered.
2910 * When an abbreviation is recognized it is removed from the text with
2911 * backspaces and the replacement string is inserted, followed by "c".
2912 */
2913 static int
2914ccheck_abbr(c)
2915 int c;
2916{
2917 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
2918 return FALSE;
2919
2920 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
2921}
2922
2923/*
2924 * Return FAIL if this is not an appropriate context in which to do
2925 * completion of anything, return OK if it is (even if there are no matches).
2926 * For the caller, this means that the character is just passed through like a
2927 * normal character (instead of being expanded). This allows :s/^I^D etc.
2928 */
2929 static int
2930nextwild(xp, type, options)
2931 expand_T *xp;
2932 int type;
2933 int options; /* extra options for ExpandOne() */
2934{
2935 int i, j;
2936 char_u *p1;
2937 char_u *p2;
2938 int oldlen;
2939 int difflen;
2940 int v;
2941
2942 if (xp->xp_numfiles == -1)
2943 {
2944 set_expand_context(xp);
2945 cmd_showtail = expand_showtail(xp);
2946 }
2947
2948 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2949 {
2950 beep_flush();
2951 return OK; /* Something illegal on command line */
2952 }
2953 if (xp->xp_context == EXPAND_NOTHING)
2954 {
2955 /* Caller can use the character as a normal char instead */
2956 return FAIL;
2957 }
2958
2959 MSG_PUTS("..."); /* show that we are busy */
2960 out_flush();
2961
2962 i = (int)(xp->xp_pattern - ccline.cmdbuff);
2963 oldlen = ccline.cmdpos - i;
2964
2965 if (type == WILD_NEXT || type == WILD_PREV)
2966 {
2967 /*
2968 * Get next/previous match for a previous expanded pattern.
2969 */
2970 p2 = ExpandOne(xp, NULL, NULL, 0, type);
2971 }
2972 else
2973 {
2974 /*
2975 * Translate string into pattern and expand it.
2976 */
2977 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
2978 p2 = NULL;
2979 else
2980 {
2981 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
2982 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
2983 |options, type);
2984 vim_free(p1);
2985 /* longest match: make sure it is not shorter (happens with :help */
2986 if (p2 != NULL && type == WILD_LONGEST)
2987 {
2988 for (j = 0; j < oldlen; ++j)
2989 if (ccline.cmdbuff[i + j] == '*'
2990 || ccline.cmdbuff[i + j] == '?')
2991 break;
2992 if ((int)STRLEN(p2) < j)
2993 {
2994 vim_free(p2);
2995 p2 = NULL;
2996 }
2997 }
2998 }
2999 }
3000
3001 if (p2 != NULL && !got_int)
3002 {
3003 difflen = (int)STRLEN(p2) - oldlen;
3004 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3005 {
3006 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3007 xp->xp_pattern = ccline.cmdbuff + i;
3008 }
3009 else
3010 v = OK;
3011 if (v == OK)
3012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003013 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3014 &ccline.cmdbuff[ccline.cmdpos],
3015 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3016 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 ccline.cmdlen += difflen;
3018 ccline.cmdpos += difflen;
3019 }
3020 }
3021 vim_free(p2);
3022
3023 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003024 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025
3026 /* When expanding a ":map" command and no matches are found, assume that
3027 * the key is supposed to be inserted literally */
3028 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3029 return FAIL;
3030
3031 if (xp->xp_numfiles <= 0 && p2 == NULL)
3032 beep_flush();
3033 else if (xp->xp_numfiles == 1)
3034 /* free expanded pattern */
3035 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3036
3037 return OK;
3038}
3039
3040/*
3041 * Do wildcard expansion on the string 'str'.
3042 * Chars that should not be expanded must be preceded with a backslash.
3043 * Return a pointer to alloced memory containing the new string.
3044 * Return NULL for failure.
3045 *
3046 * Results are cached in xp->xp_files and xp->xp_numfiles.
3047 *
3048 * mode = WILD_FREE: just free previously expanded matches
3049 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3050 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3051 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3052 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3053 * mode = WILD_ALL: return all matches concatenated
3054 * mode = WILD_LONGEST: return longest matched part
3055 *
3056 * options = WILD_LIST_NOTFOUND: list entries without a match
3057 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3058 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3059 * options = WILD_NO_BEEP: Don't beep for multiple matches
3060 * options = WILD_ADD_SLASH: add a slash after directory names
3061 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3062 * options = WILD_SILENT: don't print warning messages
3063 * options = WILD_ESCAPE: put backslash before special chars
3064 *
3065 * The variables xp->xp_context and xp->xp_backslash must have been set!
3066 */
3067 char_u *
3068ExpandOne(xp, str, orig, options, mode)
3069 expand_T *xp;
3070 char_u *str;
3071 char_u *orig; /* allocated copy of original of expanded string */
3072 int options;
3073 int mode;
3074{
3075 char_u *ss = NULL;
3076 static int findex;
3077 static char_u *orig_save = NULL; /* kept value of orig */
3078 int i;
3079 long_u len;
3080 int non_suf_match; /* number without matching suffix */
3081
3082 /*
3083 * first handle the case of using an old match
3084 */
3085 if (mode == WILD_NEXT || mode == WILD_PREV)
3086 {
3087 if (xp->xp_numfiles > 0)
3088 {
3089 if (mode == WILD_PREV)
3090 {
3091 if (findex == -1)
3092 findex = xp->xp_numfiles;
3093 --findex;
3094 }
3095 else /* mode == WILD_NEXT */
3096 ++findex;
3097
3098 /*
3099 * When wrapping around, return the original string, set findex to
3100 * -1.
3101 */
3102 if (findex < 0)
3103 {
3104 if (orig_save == NULL)
3105 findex = xp->xp_numfiles - 1;
3106 else
3107 findex = -1;
3108 }
3109 if (findex >= xp->xp_numfiles)
3110 {
3111 if (orig_save == NULL)
3112 findex = 0;
3113 else
3114 findex = -1;
3115 }
3116#ifdef FEAT_WILDMENU
3117 if (p_wmnu)
3118 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3119 findex, cmd_showtail);
3120#endif
3121 if (findex == -1)
3122 return vim_strsave(orig_save);
3123 return vim_strsave(xp->xp_files[findex]);
3124 }
3125 else
3126 return NULL;
3127 }
3128
3129/* free old names */
3130 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3131 {
3132 FreeWild(xp->xp_numfiles, xp->xp_files);
3133 xp->xp_numfiles = -1;
3134 vim_free(orig_save);
3135 orig_save = NULL;
3136 }
3137 findex = 0;
3138
3139 if (mode == WILD_FREE) /* only release file name */
3140 return NULL;
3141
3142 if (xp->xp_numfiles == -1)
3143 {
3144 vim_free(orig_save);
3145 orig_save = orig;
3146
3147 /*
3148 * Do the expansion.
3149 */
3150 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3151 options) == FAIL)
3152 {
3153#ifdef FNAME_ILLEGAL
3154 /* Illegal file name has been silently skipped. But when there
3155 * are wildcards, the real problem is that there was no match,
3156 * causing the pattern to be added, which has illegal characters.
3157 */
3158 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3159 EMSG2(_(e_nomatch2), str);
3160#endif
3161 }
3162 else if (xp->xp_numfiles == 0)
3163 {
3164 if (!(options & WILD_SILENT))
3165 EMSG2(_(e_nomatch2), str);
3166 }
3167 else
3168 {
3169 /* Escape the matches for use on the command line. */
3170 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3171
3172 /*
3173 * Check for matching suffixes in file names.
3174 */
3175 if (mode != WILD_ALL && mode != WILD_LONGEST)
3176 {
3177 if (xp->xp_numfiles)
3178 non_suf_match = xp->xp_numfiles;
3179 else
3180 non_suf_match = 1;
3181 if ((xp->xp_context == EXPAND_FILES
3182 || xp->xp_context == EXPAND_DIRECTORIES)
3183 && xp->xp_numfiles > 1)
3184 {
3185 /*
3186 * More than one match; check suffix.
3187 * The files will have been sorted on matching suffix in
3188 * expand_wildcards, only need to check the first two.
3189 */
3190 non_suf_match = 0;
3191 for (i = 0; i < 2; ++i)
3192 if (match_suffix(xp->xp_files[i]))
3193 ++non_suf_match;
3194 }
3195 if (non_suf_match != 1)
3196 {
3197 /* Can we ever get here unless it's while expanding
3198 * interactively? If not, we can get rid of this all
3199 * together. Don't really want to wait for this message
3200 * (and possibly have to hit return to continue!).
3201 */
3202 if (!(options & WILD_SILENT))
3203 EMSG(_(e_toomany));
3204 else if (!(options & WILD_NO_BEEP))
3205 beep_flush();
3206 }
3207 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3208 ss = vim_strsave(xp->xp_files[0]);
3209 }
3210 }
3211 }
3212
3213 /* Find longest common part */
3214 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3215 {
3216 for (len = 0; xp->xp_files[0][len]; ++len)
3217 {
3218 for (i = 0; i < xp->xp_numfiles; ++i)
3219 {
3220#ifdef CASE_INSENSITIVE_FILENAME
3221 if (xp->xp_context == EXPAND_DIRECTORIES
3222 || xp->xp_context == EXPAND_FILES
3223 || xp->xp_context == EXPAND_BUFFERS)
3224 {
3225 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3226 TOLOWER_LOC(xp->xp_files[0][len]))
3227 break;
3228 }
3229 else
3230#endif
3231 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3232 break;
3233 }
3234 if (i < xp->xp_numfiles)
3235 {
3236 if (!(options & WILD_NO_BEEP))
3237 vim_beep();
3238 break;
3239 }
3240 }
3241 ss = alloc((unsigned)len + 1);
3242 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003243 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 findex = -1; /* next p_wc gets first one */
3245 }
3246
3247 /* Concatenate all matching names */
3248 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3249 {
3250 len = 0;
3251 for (i = 0; i < xp->xp_numfiles; ++i)
3252 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3253 ss = lalloc(len, TRUE);
3254 if (ss != NULL)
3255 {
3256 *ss = NUL;
3257 for (i = 0; i < xp->xp_numfiles; ++i)
3258 {
3259 STRCAT(ss, xp->xp_files[i]);
3260 if (i != xp->xp_numfiles - 1)
3261 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3262 }
3263 }
3264 }
3265
3266 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3267 ExpandCleanup(xp);
3268
3269 return ss;
3270}
3271
3272/*
3273 * Prepare an expand structure for use.
3274 */
3275 void
3276ExpandInit(xp)
3277 expand_T *xp;
3278{
3279 xp->xp_backslash = XP_BS_NONE;
3280 xp->xp_numfiles = -1;
3281 xp->xp_files = NULL;
3282}
3283
3284/*
3285 * Cleanup an expand structure after use.
3286 */
3287 void
3288ExpandCleanup(xp)
3289 expand_T *xp;
3290{
3291 if (xp->xp_numfiles >= 0)
3292 {
3293 FreeWild(xp->xp_numfiles, xp->xp_files);
3294 xp->xp_numfiles = -1;
3295 }
3296}
3297
3298 void
3299ExpandEscape(xp, str, numfiles, files, options)
3300 expand_T *xp;
3301 char_u *str;
3302 int numfiles;
3303 char_u **files;
3304 int options;
3305{
3306 int i;
3307 char_u *p;
3308
3309 /*
3310 * May change home directory back to "~"
3311 */
3312 if (options & WILD_HOME_REPLACE)
3313 tilde_replace(str, numfiles, files);
3314
3315 if (options & WILD_ESCAPE)
3316 {
3317 if (xp->xp_context == EXPAND_FILES
3318 || xp->xp_context == EXPAND_BUFFERS
3319 || xp->xp_context == EXPAND_DIRECTORIES)
3320 {
3321 /*
3322 * Insert a backslash into a file name before a space, \, %, #
3323 * and wildmatch characters, except '~'.
3324 */
3325 for (i = 0; i < numfiles; ++i)
3326 {
3327 /* for ":set path=" we need to escape spaces twice */
3328 if (xp->xp_backslash == XP_BS_THREE)
3329 {
3330 p = vim_strsave_escaped(files[i], (char_u *)" ");
3331 if (p != NULL)
3332 {
3333 vim_free(files[i]);
3334 files[i] = p;
3335#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3336 p = vim_strsave_escaped(files[i], (char_u *)" ");
3337 if (p != NULL)
3338 {
3339 vim_free(files[i]);
3340 files[i] = p;
3341 }
3342#endif
3343 }
3344 }
3345#ifdef BACKSLASH_IN_FILENAME
3346 {
3347 char_u buf[20];
3348 int j = 0;
3349
3350 /* Don't escape '[' and '{' if they are in 'isfname'. */
3351 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3352 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3353 buf[j++] = *p;
3354 buf[j] = NUL;
3355 p = vim_strsave_escaped(files[i], buf);
3356 }
3357#else
3358 p = vim_strsave_escaped(files[i], PATH_ESC_CHARS);
3359#endif
3360 if (p != NULL)
3361 {
3362 vim_free(files[i]);
3363 files[i] = p;
3364 }
3365
3366 /* If 'str' starts with "\~", replace "~" at start of
3367 * files[i] with "\~". */
3368 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003369 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 }
3371 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003372
3373 /* If the first file starts with a '+' escape it. Otherwise it
3374 * could be seen as "+cmd". */
3375 if (*files[0] == '+')
3376 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378 else if (xp->xp_context == EXPAND_TAGS)
3379 {
3380 /*
3381 * Insert a backslash before characters in a tag name that
3382 * would terminate the ":tag" command.
3383 */
3384 for (i = 0; i < numfiles; ++i)
3385 {
3386 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3387 if (p != NULL)
3388 {
3389 vim_free(files[i]);
3390 files[i] = p;
3391 }
3392 }
3393 }
3394 }
3395}
3396
3397/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003398 * Put a backslash before the file name in "pp", which is in allocated memory.
3399 */
3400 static void
3401escape_fname(pp)
3402 char_u **pp;
3403{
3404 char_u *p;
3405
3406 p = alloc((unsigned)(STRLEN(*pp) + 2));
3407 if (p != NULL)
3408 {
3409 p[0] = '\\';
3410 STRCPY(p + 1, *pp);
3411 vim_free(*pp);
3412 *pp = p;
3413 }
3414}
3415
3416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 * For each file name in files[num_files]:
3418 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3419 */
3420 void
3421tilde_replace(orig_pat, num_files, files)
3422 char_u *orig_pat;
3423 int num_files;
3424 char_u **files;
3425{
3426 int i;
3427 char_u *p;
3428
3429 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3430 {
3431 for (i = 0; i < num_files; ++i)
3432 {
3433 p = home_replace_save(NULL, files[i]);
3434 if (p != NULL)
3435 {
3436 vim_free(files[i]);
3437 files[i] = p;
3438 }
3439 }
3440 }
3441}
3442
3443/*
3444 * Show all matches for completion on the command line.
3445 * Returns EXPAND_NOTHING when the character that triggered expansion should
3446 * be inserted like a normal character.
3447 */
3448/*ARGSUSED*/
3449 static int
3450showmatches(xp, wildmenu)
3451 expand_T *xp;
3452 int wildmenu;
3453{
3454#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3455 int num_files;
3456 char_u **files_found;
3457 int i, j, k;
3458 int maxlen;
3459 int lines;
3460 int columns;
3461 char_u *p;
3462 int lastlen;
3463 int attr;
3464 int showtail;
3465
3466 if (xp->xp_numfiles == -1)
3467 {
3468 set_expand_context(xp);
3469 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3470 &num_files, &files_found);
3471 showtail = expand_showtail(xp);
3472 if (i != EXPAND_OK)
3473 return i;
3474
3475 }
3476 else
3477 {
3478 num_files = xp->xp_numfiles;
3479 files_found = xp->xp_files;
3480 showtail = cmd_showtail;
3481 }
3482
3483#ifdef FEAT_WILDMENU
3484 if (!wildmenu)
3485 {
3486#endif
3487 msg_didany = FALSE; /* lines_left will be set */
3488 msg_start(); /* prepare for paging */
3489 msg_putchar('\n');
3490 out_flush();
3491 cmdline_row = msg_row;
3492 msg_didany = FALSE; /* lines_left will be set again */
3493 msg_start(); /* prepare for paging */
3494#ifdef FEAT_WILDMENU
3495 }
3496#endif
3497
3498 if (got_int)
3499 got_int = FALSE; /* only int. the completion, not the cmd line */
3500#ifdef FEAT_WILDMENU
3501 else if (wildmenu)
3502 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3503#endif
3504 else
3505 {
3506 /* find the length of the longest file name */
3507 maxlen = 0;
3508 for (i = 0; i < num_files; ++i)
3509 {
3510 if (!showtail && (xp->xp_context == EXPAND_FILES
3511 || xp->xp_context == EXPAND_BUFFERS))
3512 {
3513 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3514 j = vim_strsize(NameBuff);
3515 }
3516 else
3517 j = vim_strsize(L_SHOWFILE(i));
3518 if (j > maxlen)
3519 maxlen = j;
3520 }
3521
3522 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3523 lines = num_files;
3524 else
3525 {
3526 /* compute the number of columns and lines for the listing */
3527 maxlen += 2; /* two spaces between file names */
3528 columns = ((int)Columns + 2) / maxlen;
3529 if (columns < 1)
3530 columns = 1;
3531 lines = (num_files + columns - 1) / columns;
3532 }
3533
3534 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3535
3536 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3537 {
3538 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3539 msg_clr_eos();
3540 msg_advance(maxlen - 3);
3541 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3542 }
3543
3544 /* list the files line by line */
3545 for (i = 0; i < lines; ++i)
3546 {
3547 lastlen = 999;
3548 for (k = i; k < num_files; k += lines)
3549 {
3550 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3551 {
3552 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3553 p = files_found[k] + STRLEN(files_found[k]) + 1;
3554 msg_advance(maxlen + 1);
3555 msg_puts(p);
3556 msg_advance(maxlen + 3);
3557 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3558 break;
3559 }
3560 for (j = maxlen - lastlen; --j >= 0; )
3561 msg_putchar(' ');
3562 if (xp->xp_context == EXPAND_FILES
3563 || xp->xp_context == EXPAND_BUFFERS)
3564 {
3565 /* highlight directories */
3566 j = (mch_isdir(files_found[k]));
3567 if (showtail)
3568 p = L_SHOWFILE(k);
3569 else
3570 {
3571 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3572 TRUE);
3573 p = NameBuff;
3574 }
3575 }
3576 else
3577 {
3578 j = FALSE;
3579 p = L_SHOWFILE(k);
3580 }
3581 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3582 }
3583 if (msg_col > 0) /* when not wrapped around */
3584 {
3585 msg_clr_eos();
3586 msg_putchar('\n');
3587 }
3588 out_flush(); /* show one line at a time */
3589 if (got_int)
3590 {
3591 got_int = FALSE;
3592 break;
3593 }
3594 }
3595
3596 /*
3597 * we redraw the command below the lines that we have just listed
3598 * This is a bit tricky, but it saves a lot of screen updating.
3599 */
3600 cmdline_row = msg_row; /* will put it back later */
3601 }
3602
3603 if (xp->xp_numfiles == -1)
3604 FreeWild(num_files, files_found);
3605
3606 return EXPAND_OK;
3607}
3608
3609/*
3610 * Private gettail for showmatches() (and win_redr_status_matches()):
3611 * Find tail of file name path, but ignore trailing "/".
3612 */
3613 char_u *
3614sm_gettail(s)
3615 char_u *s;
3616{
3617 char_u *p;
3618 char_u *t = s;
3619 int had_sep = FALSE;
3620
3621 for (p = s; *p != NUL; )
3622 {
3623 if (vim_ispathsep(*p)
3624#ifdef BACKSLASH_IN_FILENAME
3625 && !rem_backslash(p)
3626#endif
3627 )
3628 had_sep = TRUE;
3629 else if (had_sep)
3630 {
3631 t = p;
3632 had_sep = FALSE;
3633 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003634 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
3636 return t;
3637}
3638
3639/*
3640 * Return TRUE if we only need to show the tail of completion matches.
3641 * When not completing file names or there is a wildcard in the path FALSE is
3642 * returned.
3643 */
3644 static int
3645expand_showtail(xp)
3646 expand_T *xp;
3647{
3648 char_u *s;
3649 char_u *end;
3650
3651 /* When not completing file names a "/" may mean something different. */
3652 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3653 return FALSE;
3654
3655 end = gettail(xp->xp_pattern);
3656 if (end == xp->xp_pattern) /* there is no path separator */
3657 return FALSE;
3658
3659 for (s = xp->xp_pattern; s < end; s++)
3660 {
3661 /* Skip escaped wildcards. Only when the backslash is not a path
3662 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3663 if (rem_backslash(s))
3664 ++s;
3665 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3666 return FALSE;
3667 }
3668 return TRUE;
3669}
3670
3671/*
3672 * Prepare a string for expansion.
3673 * When expanding file names: The string will be used with expand_wildcards().
3674 * Copy the file name into allocated memory and add a '*' at the end.
3675 * When expanding other names: The string will be used with regcomp(). Copy
3676 * the name into allocated memory and prepend "^".
3677 */
3678 char_u *
3679addstar(fname, len, context)
3680 char_u *fname;
3681 int len;
3682 int context; /* EXPAND_FILES etc. */
3683{
3684 char_u *retval;
3685 int i, j;
3686 int new_len;
3687 char_u *tail;
3688
3689 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3690 {
3691 /*
3692 * Matching will be done internally (on something other than files).
3693 * So we convert the file-matching-type wildcards into our kind for
3694 * use with vim_regcomp(). First work out how long it will be:
3695 */
3696
3697 /* For help tags the translation is done in find_help_tags().
3698 * For a tag pattern starting with "/" no translation is needed. */
3699 if (context == EXPAND_HELP
3700 || context == EXPAND_COLORS
3701 || context == EXPAND_COMPILER
3702 || (context == EXPAND_TAGS && fname[0] == '/'))
3703 retval = vim_strnsave(fname, len);
3704 else
3705 {
3706 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3707 for (i = 0; i < len; i++)
3708 {
3709 if (fname[i] == '*' || fname[i] == '~')
3710 new_len++; /* '*' needs to be replaced by ".*"
3711 '~' needs to be replaced by "\~" */
3712
3713 /* Buffer names are like file names. "." should be literal */
3714 if (context == EXPAND_BUFFERS && fname[i] == '.')
3715 new_len++; /* "." becomes "\." */
3716
3717 /* Custom expansion takes care of special things, match
3718 * backslashes literally (perhaps also for other types?) */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003719 if ((context == EXPAND_USER_DEFINED ||
3720 context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 new_len++; /* '\' becomes "\\" */
3722 }
3723 retval = alloc(new_len);
3724 if (retval != NULL)
3725 {
3726 retval[0] = '^';
3727 j = 1;
3728 for (i = 0; i < len; i++, j++)
3729 {
3730 /* Skip backslash. But why? At least keep it for custom
3731 * expansion. */
3732 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003733 && context != EXPAND_USER_LIST
3734 && fname[i] == '\\'
3735 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 break;
3737
3738 switch (fname[i])
3739 {
3740 case '*': retval[j++] = '.';
3741 break;
3742 case '~': retval[j++] = '\\';
3743 break;
3744 case '?': retval[j] = '.';
3745 continue;
3746 case '.': if (context == EXPAND_BUFFERS)
3747 retval[j++] = '\\';
3748 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003749 case '\\': if (context == EXPAND_USER_DEFINED
3750 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 retval[j++] = '\\';
3752 break;
3753 }
3754 retval[j] = fname[i];
3755 }
3756 retval[j] = NUL;
3757 }
3758 }
3759 }
3760 else
3761 {
3762 retval = alloc(len + 4);
3763 if (retval != NULL)
3764 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003765 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
3767 /*
3768 * Don't add a star to ~, ~user, $var or `cmd`.
3769 * ~ would be at the start of the file name, but not the tail.
3770 * $ could be anywhere in the tail.
3771 * ` could be anywhere in the file name.
3772 */
3773 tail = gettail(retval);
3774 if ((*retval != '~' || tail != retval)
3775 && vim_strchr(tail, '$') == NULL
3776 && vim_strchr(retval, '`') == NULL)
3777 retval[len++] = '*';
3778 retval[len] = NUL;
3779 }
3780 }
3781 return retval;
3782}
3783
3784/*
3785 * Must parse the command line so far to work out what context we are in.
3786 * Completion can then be done based on that context.
3787 * This routine sets the variables:
3788 * xp->xp_pattern The start of the pattern to be expanded within
3789 * the command line (ends at the cursor).
3790 * xp->xp_context The type of thing to expand. Will be one of:
3791 *
3792 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3793 * the command line, like an unknown command. Caller
3794 * should beep.
3795 * EXPAND_NOTHING Unrecognised context for completion, use char like
3796 * a normal char, rather than for completion. eg
3797 * :s/^I/
3798 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3799 * it.
3800 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3801 * EXPAND_FILES After command with XFILE set, or after setting
3802 * with P_EXPAND set. eg :e ^I, :w>>^I
3803 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3804 * when we know only directories are of interest. eg
3805 * :set dir=^I
3806 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3807 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3808 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3809 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3810 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3811 * EXPAND_EVENTS Complete event names
3812 * EXPAND_SYNTAX Complete :syntax command arguments
3813 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3814 * EXPAND_AUGROUP Complete autocommand group names
3815 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3816 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3817 * eg :unmap a^I , :cunab x^I
3818 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3819 * eg :call sub^I
3820 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3821 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3822 * names in expressions, eg :while s^I
3823 * EXPAND_ENV_VARS Complete environment variable names
3824 */
3825 static void
3826set_expand_context(xp)
3827 expand_T *xp;
3828{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003829 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (ccline.cmdfirstc != ':'
3831#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003832 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833#endif
3834 )
3835 {
3836 xp->xp_context = EXPAND_NOTHING;
3837 return;
3838 }
3839 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3840}
3841
3842 void
3843set_cmd_context(xp, str, len, col)
3844 expand_T *xp;
3845 char_u *str; /* start of command line */
3846 int len; /* length of command line (excl. NUL) */
3847 int col; /* position of cursor */
3848{
3849 int old_char = NUL;
3850 char_u *nextcomm;
3851
3852 /*
3853 * Avoid a UMR warning from Purify, only save the character if it has been
3854 * written before.
3855 */
3856 if (col < len)
3857 old_char = str[col];
3858 str[col] = NUL;
3859 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003860
3861#ifdef FEAT_EVAL
3862 if (ccline.cmdfirstc == '=')
3863 /* pass CMD_SIZE because there is no real command */
3864 set_context_for_expression(xp, str, CMD_SIZE);
3865 else
3866#endif
3867 while (nextcomm != NULL)
3868 nextcomm = set_one_cmd_context(xp, nextcomm);
3869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 str[col] = old_char;
3871}
3872
3873/*
3874 * Expand the command line "str" from context "xp".
3875 * "xp" must have been set by set_cmd_context().
3876 * xp->xp_pattern points into "str", to where the text that is to be expanded
3877 * starts.
3878 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
3879 * cursor.
3880 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
3881 * key that triggered expansion literally.
3882 * Returns EXPAND_OK otherwise.
3883 */
3884 int
3885expand_cmdline(xp, str, col, matchcount, matches)
3886 expand_T *xp;
3887 char_u *str; /* start of command line */
3888 int col; /* position of cursor */
3889 int *matchcount; /* return: nr of matches */
3890 char_u ***matches; /* return: array of pointers to matches */
3891{
3892 char_u *file_str = NULL;
3893
3894 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3895 {
3896 beep_flush();
3897 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
3898 }
3899 if (xp->xp_context == EXPAND_NOTHING)
3900 {
3901 /* Caller can use the character as a normal char instead */
3902 return EXPAND_NOTHING;
3903 }
3904
3905 /* add star to file name, or convert to regexp if not exp. files. */
3906 file_str = addstar(xp->xp_pattern,
3907 (int)(str + col - xp->xp_pattern), xp->xp_context);
3908 if (file_str == NULL)
3909 return EXPAND_UNSUCCESSFUL;
3910
3911 /* find all files that match the description */
3912 if (ExpandFromContext(xp, file_str, matchcount, matches,
3913 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
3914 {
3915 *matchcount = 0;
3916 *matches = NULL;
3917 }
3918 vim_free(file_str);
3919
3920 return EXPAND_OK;
3921}
3922
3923#ifdef FEAT_MULTI_LANG
3924/*
3925 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
3926 */
3927static void cleanup_help_tags __ARGS((int num_file, char_u **file));
3928
3929 static void
3930cleanup_help_tags(num_file, file)
3931 int num_file;
3932 char_u **file;
3933{
3934 int i, j;
3935 int len;
3936
3937 for (i = 0; i < num_file; ++i)
3938 {
3939 len = (int)STRLEN(file[i]) - 3;
3940 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
3941 {
3942 /* Sorting on priority means the same item in another language may
3943 * be anywhere. Search all items for a match up to the "@en". */
3944 for (j = 0; j < num_file; ++j)
3945 if (j != i
3946 && (int)STRLEN(file[j]) == len + 3
3947 && STRNCMP(file[i], file[j], len + 1) == 0)
3948 break;
3949 if (j == num_file)
3950 file[i][len] = NUL;
3951 }
3952 }
3953}
3954#endif
3955
3956/*
3957 * Do the expansion based on xp->xp_context and "pat".
3958 */
3959 static int
3960ExpandFromContext(xp, pat, num_file, file, options)
3961 expand_T *xp;
3962 char_u *pat;
3963 int *num_file;
3964 char_u ***file;
3965 int options;
3966{
3967#ifdef FEAT_CMDL_COMPL
3968 regmatch_T regmatch;
3969#endif
3970 int ret;
3971 int flags;
3972
3973 flags = EW_DIR; /* include directories */
3974 if (options & WILD_LIST_NOTFOUND)
3975 flags |= EW_NOTFOUND;
3976 if (options & WILD_ADD_SLASH)
3977 flags |= EW_ADDSLASH;
3978 if (options & WILD_KEEP_ALL)
3979 flags |= EW_KEEPALL;
3980 if (options & WILD_SILENT)
3981 flags |= EW_SILENT;
3982
3983 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
3984 {
3985 /*
3986 * Expand file or directory names.
3987 */
3988 int free_pat = FALSE;
3989 int i;
3990
3991 /* for ":set path=" and ":set tags=" halve backslashes for escaped
3992 * space */
3993 if (xp->xp_backslash != XP_BS_NONE)
3994 {
3995 free_pat = TRUE;
3996 pat = vim_strsave(pat);
3997 for (i = 0; pat[i]; ++i)
3998 if (pat[i] == '\\')
3999 {
4000 if (xp->xp_backslash == XP_BS_THREE
4001 && pat[i + 1] == '\\'
4002 && pat[i + 2] == '\\'
4003 && pat[i + 3] == ' ')
4004 STRCPY(pat + i, pat + i + 3);
4005 if (xp->xp_backslash == XP_BS_ONE
4006 && pat[i + 1] == ' ')
4007 STRCPY(pat + i, pat + i + 1);
4008 }
4009 }
4010
4011 if (xp->xp_context == EXPAND_FILES)
4012 flags |= EW_FILE;
4013 else
4014 flags = (flags | EW_DIR) & ~EW_FILE;
4015 ret = expand_wildcards(1, &pat, num_file, file, flags);
4016 if (free_pat)
4017 vim_free(pat);
4018 return ret;
4019 }
4020
4021 *file = (char_u **)"";
4022 *num_file = 0;
4023 if (xp->xp_context == EXPAND_HELP)
4024 {
4025 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4026 {
4027#ifdef FEAT_MULTI_LANG
4028 cleanup_help_tags(*num_file, *file);
4029#endif
4030 return OK;
4031 }
4032 return FAIL;
4033 }
4034
4035#ifndef FEAT_CMDL_COMPL
4036 return FAIL;
4037#else
4038 if (xp->xp_context == EXPAND_OLD_SETTING)
4039 return ExpandOldSetting(num_file, file);
4040 if (xp->xp_context == EXPAND_BUFFERS)
4041 return ExpandBufnames(pat, num_file, file, options);
4042 if (xp->xp_context == EXPAND_TAGS
4043 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4044 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4045 if (xp->xp_context == EXPAND_COLORS)
4046 return ExpandRTDir(pat, num_file, file, "colors");
4047 if (xp->xp_context == EXPAND_COMPILER)
4048 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004049# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4050 if (xp->xp_context == EXPAND_USER_LIST)
4051 return ExpandUserList(xp, num_file, file);
4052# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4055 if (regmatch.regprog == NULL)
4056 return FAIL;
4057
4058 /* set ignore-case according to p_ic, p_scs and pat */
4059 regmatch.rm_ic = ignorecase(pat);
4060
4061 if (xp->xp_context == EXPAND_SETTINGS
4062 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4063 ret = ExpandSettings(xp, &regmatch, num_file, file);
4064 else if (xp->xp_context == EXPAND_MAPPINGS)
4065 ret = ExpandMappings(&regmatch, num_file, file);
4066# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4067 else if (xp->xp_context == EXPAND_USER_DEFINED)
4068 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4069# endif
4070 else
4071 {
4072 static struct expgen
4073 {
4074 int context;
4075 char_u *((*func)__ARGS((expand_T *, int)));
4076 int ic;
4077 } tab[] =
4078 {
4079 {EXPAND_COMMANDS, get_command_name, FALSE},
4080#ifdef FEAT_USR_CMDS
4081 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4082 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4083 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4084 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4085#endif
4086#ifdef FEAT_EVAL
4087 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4088 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4089 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4090 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4091#endif
4092#ifdef FEAT_MENU
4093 {EXPAND_MENUS, get_menu_name, FALSE},
4094 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4095#endif
4096#ifdef FEAT_SYN_HL
4097 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4098#endif
4099 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4100#ifdef FEAT_AUTOCMD
4101 {EXPAND_EVENTS, get_event_name, TRUE},
4102 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4103#endif
4104#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4105 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4106 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4107#endif
4108 {EXPAND_ENV_VARS, get_env_name, TRUE},
4109 };
4110 int i;
4111
4112 /*
4113 * Find a context in the table and call the ExpandGeneric() with the
4114 * right function to do the expansion.
4115 */
4116 ret = FAIL;
4117 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4118 if (xp->xp_context == tab[i].context)
4119 {
4120 if (tab[i].ic)
4121 regmatch.rm_ic = TRUE;
4122 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4123 break;
4124 }
4125 }
4126
4127 vim_free(regmatch.regprog);
4128
4129 return ret;
4130#endif /* FEAT_CMDL_COMPL */
4131}
4132
4133#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4134/*
4135 * Expand a list of names.
4136 *
4137 * Generic function for command line completion. It calls a function to
4138 * obtain strings, one by one. The strings are matched against a regexp
4139 * program. Matching strings are copied into an array, which is returned.
4140 *
4141 * Returns OK when no problems encountered, FAIL for error (out of memory).
4142 */
4143 int
4144ExpandGeneric(xp, regmatch, num_file, file, func)
4145 expand_T *xp;
4146 regmatch_T *regmatch;
4147 int *num_file;
4148 char_u ***file;
4149 char_u *((*func)__ARGS((expand_T *, int)));
4150 /* returns a string from the list */
4151{
4152 int i;
4153 int count = 0;
4154 int loop;
4155 char_u *str;
4156
4157 /* do this loop twice:
4158 * loop == 0: count the number of matching names
4159 * loop == 1: copy the matching names into allocated memory
4160 */
4161 for (loop = 0; loop <= 1; ++loop)
4162 {
4163 for (i = 0; ; ++i)
4164 {
4165 str = (*func)(xp, i);
4166 if (str == NULL) /* end of list */
4167 break;
4168 if (*str == NUL) /* skip empty strings */
4169 continue;
4170
4171 if (vim_regexec(regmatch, str, (colnr_T)0))
4172 {
4173 if (loop)
4174 {
4175 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4176 (*file)[count] = str;
4177#ifdef FEAT_MENU
4178 if (func == get_menu_names && str != NULL)
4179 {
4180 /* test for separator added by get_menu_names() */
4181 str += STRLEN(str) - 1;
4182 if (*str == '\001')
4183 *str = '.';
4184 }
4185#endif
4186 }
4187 ++count;
4188 }
4189 }
4190 if (loop == 0)
4191 {
4192 if (count == 0)
4193 return OK;
4194 *num_file = count;
4195 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4196 if (*file == NULL)
4197 {
4198 *file = (char_u **)"";
4199 return FAIL;
4200 }
4201 count = 0;
4202 }
4203 }
4204 return OK;
4205}
4206
4207# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004208static 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));
4209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004211 * call "user_expand_func()" to invoke a user defined VimL function and return
4212 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004214 static void *
4215call_user_expand_func(user_expand_func, xp, num_file, file)
4216 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 int *num_file;
4219 char_u ***file;
4220{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004221 char_u keep;
4222 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004225 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004226 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004229 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 *num_file = 0;
4231 *file = NULL;
4232
4233 keep = ccline.cmdbuff[ccline.cmdlen];
4234 ccline.cmdbuff[ccline.cmdlen] = 0;
4235 sprintf((char *)num, "%d", ccline.cmdpos);
4236 args[0] = xp->xp_pattern;
4237 args[1] = ccline.cmdbuff;
4238 args[2] = num;
4239
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004240 /* Save the cmdline, we don't know what the function may do. */
4241 save_ccline = ccline;
4242 ccline.cmdbuff = NULL;
4243 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004245
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004246 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004247
4248 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004250
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004252
4253 return ret;
4254}
4255
4256/*
4257 * Expand names with a function defined by the user.
4258 */
4259 static int
4260ExpandUserDefined(xp, regmatch, num_file, file)
4261 expand_T *xp;
4262 regmatch_T *regmatch;
4263 int *num_file;
4264 char_u ***file;
4265{
4266 char_u *retstr;
4267 char_u *s;
4268 char_u *e;
4269 char_u keep;
4270 garray_T ga;
4271
4272 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4273 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 return FAIL;
4275
4276 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004277 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
4279 e = vim_strchr(s, '\n');
4280 if (e == NULL)
4281 e = s + STRLEN(s);
4282 keep = *e;
4283 *e = 0;
4284
4285 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4286 {
4287 *e = keep;
4288 if (*e != NUL)
4289 ++e;
4290 continue;
4291 }
4292
4293 if (ga_grow(&ga, 1) == FAIL)
4294 break;
4295
4296 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4297 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 *e = keep;
4300 if (*e != NUL)
4301 ++e;
4302 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004303 vim_free(retstr);
4304 *file = ga.ga_data;
4305 *num_file = ga.ga_len;
4306 return OK;
4307}
4308
4309/*
4310 * Expand names with a list returned by a function defined by the user.
4311 */
4312 static int
4313ExpandUserList(xp, num_file, file)
4314 expand_T *xp;
4315 int *num_file;
4316 char_u ***file;
4317{
4318 list_T *retlist;
4319 listitem_T *li;
4320 garray_T ga;
4321
4322 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4323 if (retlist == NULL)
4324 return FAIL;
4325
4326 ga_init2(&ga, (int)sizeof(char *), 3);
4327 /* Loop over the items in the list. */
4328 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4329 {
4330 if (li->li_tv.v_type != VAR_STRING)
4331 continue; /* Skip non-string items */
4332
4333 if (ga_grow(&ga, 1) == FAIL)
4334 break;
4335
4336 ((char_u **)ga.ga_data)[ga.ga_len] =
4337 vim_strsave(li->li_tv.vval.v_string);
4338 ++ga.ga_len;
4339 }
4340 list_unref(retlist);
4341
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 *file = ga.ga_data;
4343 *num_file = ga.ga_len;
4344 return OK;
4345}
4346#endif
4347
4348/*
4349 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4350 * or compiler names.
4351 */
4352 static int
4353ExpandRTDir(pat, num_file, file, dirname)
4354 char_u *pat;
4355 int *num_file;
4356 char_u ***file;
4357 char *dirname; /* "colors" or "compiler" */
4358{
4359 char_u *all;
4360 char_u *s;
4361 char_u *e;
4362 garray_T ga;
4363
4364 *num_file = 0;
4365 *file = NULL;
4366 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4367 if (s == NULL)
4368 return FAIL;
4369 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4370 all = globpath(p_rtp, s);
4371 vim_free(s);
4372 if (all == NULL)
4373 return FAIL;
4374
4375 ga_init2(&ga, (int)sizeof(char *), 3);
4376 for (s = all; *s != NUL; s = e)
4377 {
4378 e = vim_strchr(s, '\n');
4379 if (e == NULL)
4380 e = s + STRLEN(s);
4381 if (ga_grow(&ga, 1) == FAIL)
4382 break;
4383 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4384 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004385 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 if (*s == '\n' || vim_ispathsep(*s))
4387 break;
4388 ++s;
4389 ((char_u **)ga.ga_data)[ga.ga_len] =
4390 vim_strnsave(s, (int)(e - s - 4));
4391 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
4393 if (*e != NUL)
4394 ++e;
4395 }
4396 vim_free(all);
4397 *file = ga.ga_data;
4398 *num_file = ga.ga_len;
4399 return OK;
4400}
4401
4402#endif
4403
4404#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4405/*
4406 * Expand "file" for all comma-separated directories in "path".
4407 * Returns an allocated string with all matches concatenated, separated by
4408 * newlines. Returns NULL for an error or no matches.
4409 */
4410 char_u *
4411globpath(path, file)
4412 char_u *path;
4413 char_u *file;
4414{
4415 expand_T xpc;
4416 char_u *buf;
4417 garray_T ga;
4418 int i;
4419 int len;
4420 int num_p;
4421 char_u **p;
4422 char_u *cur = NULL;
4423
4424 buf = alloc(MAXPATHL);
4425 if (buf == NULL)
4426 return NULL;
4427
4428 xpc.xp_context = EXPAND_FILES;
4429 xpc.xp_backslash = XP_BS_NONE;
4430 ga_init2(&ga, 1, 100);
4431
4432 /* Loop over all entries in {path}. */
4433 while (*path != NUL)
4434 {
4435 /* Copy one item of the path to buf[] and concatenate the file name. */
4436 copy_option_part(&path, buf, MAXPATHL, ",");
4437 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4438 {
4439 add_pathsep(buf);
4440 STRCAT(buf, file);
4441 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4442 && num_p > 0)
4443 {
4444 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4445 for (len = 0, i = 0; i < num_p; ++i)
4446 len += (long_u)STRLEN(p[i]) + 1;
4447
4448 /* Concatenate new results to previous ones. */
4449 if (ga_grow(&ga, len) == OK)
4450 {
4451 cur = (char_u *)ga.ga_data + ga.ga_len;
4452 for (i = 0; i < num_p; ++i)
4453 {
4454 STRCPY(cur, p[i]);
4455 cur += STRLEN(p[i]);
4456 *cur++ = '\n';
4457 }
4458 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460 FreeWild(num_p, p);
4461 }
4462 }
4463 }
4464 if (cur != NULL)
4465 *--cur = 0; /* Replace trailing newline with NUL */
4466
4467 vim_free(buf);
4468 return (char_u *)ga.ga_data;
4469}
4470
4471#endif
4472
4473#if defined(FEAT_CMDHIST) || defined(PROTO)
4474
4475/*********************************
4476 * Command line history stuff *
4477 *********************************/
4478
4479/*
4480 * Translate a history character to the associated type number.
4481 */
4482 static int
4483hist_char2type(c)
4484 int c;
4485{
4486 if (c == ':')
4487 return HIST_CMD;
4488 if (c == '=')
4489 return HIST_EXPR;
4490 if (c == '@')
4491 return HIST_INPUT;
4492 if (c == '>')
4493 return HIST_DEBUG;
4494 return HIST_SEARCH; /* must be '?' or '/' */
4495}
4496
4497/*
4498 * Table of history names.
4499 * These names are used in :history and various hist...() functions.
4500 * It is sufficient to give the significant prefix of a history name.
4501 */
4502
4503static char *(history_names[]) =
4504{
4505 "cmd",
4506 "search",
4507 "expr",
4508 "input",
4509 "debug",
4510 NULL
4511};
4512
4513/*
4514 * init_history() - Initialize the command line history.
4515 * Also used to re-allocate the history when the size changes.
4516 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004517 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518init_history()
4519{
4520 int newlen; /* new length of history table */
4521 histentry_T *temp;
4522 int i;
4523 int j;
4524 int type;
4525
4526 /*
4527 * If size of history table changed, reallocate it
4528 */
4529 newlen = (int)p_hi;
4530 if (newlen != hislen) /* history length changed */
4531 {
4532 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4533 {
4534 if (newlen)
4535 {
4536 temp = (histentry_T *)lalloc(
4537 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4538 if (temp == NULL) /* out of memory! */
4539 {
4540 if (type == 0) /* first one: just keep the old length */
4541 {
4542 newlen = hislen;
4543 break;
4544 }
4545 /* Already changed one table, now we can only have zero
4546 * length for all tables. */
4547 newlen = 0;
4548 type = -1;
4549 continue;
4550 }
4551 }
4552 else
4553 temp = NULL;
4554 if (newlen == 0 || temp != NULL)
4555 {
4556 if (hisidx[type] < 0) /* there are no entries yet */
4557 {
4558 for (i = 0; i < newlen; ++i)
4559 {
4560 temp[i].hisnum = 0;
4561 temp[i].hisstr = NULL;
4562 }
4563 }
4564 else if (newlen > hislen) /* array becomes bigger */
4565 {
4566 for (i = 0; i <= hisidx[type]; ++i)
4567 temp[i] = history[type][i];
4568 j = i;
4569 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4570 {
4571 temp[i].hisnum = 0;
4572 temp[i].hisstr = NULL;
4573 }
4574 for ( ; j < hislen; ++i, ++j)
4575 temp[i] = history[type][j];
4576 }
4577 else /* array becomes smaller or 0 */
4578 {
4579 j = hisidx[type];
4580 for (i = newlen - 1; ; --i)
4581 {
4582 if (i >= 0) /* copy newest entries */
4583 temp[i] = history[type][j];
4584 else /* remove older entries */
4585 vim_free(history[type][j].hisstr);
4586 if (--j < 0)
4587 j = hislen - 1;
4588 if (j == hisidx[type])
4589 break;
4590 }
4591 hisidx[type] = newlen - 1;
4592 }
4593 vim_free(history[type]);
4594 history[type] = temp;
4595 }
4596 }
4597 hislen = newlen;
4598 }
4599}
4600
4601/*
4602 * Check if command line 'str' is already in history.
4603 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4604 */
4605 static int
4606in_history(type, str, move_to_front)
4607 int type;
4608 char_u *str;
4609 int move_to_front; /* Move the entry to the front if it exists */
4610{
4611 int i;
4612 int last_i = -1;
4613
4614 if (hisidx[type] < 0)
4615 return FALSE;
4616 i = hisidx[type];
4617 do
4618 {
4619 if (history[type][i].hisstr == NULL)
4620 return FALSE;
4621 if (STRCMP(str, history[type][i].hisstr) == 0)
4622 {
4623 if (!move_to_front)
4624 return TRUE;
4625 last_i = i;
4626 break;
4627 }
4628 if (--i < 0)
4629 i = hislen - 1;
4630 } while (i != hisidx[type]);
4631
4632 if (last_i >= 0)
4633 {
4634 str = history[type][i].hisstr;
4635 while (i != hisidx[type])
4636 {
4637 if (++i >= hislen)
4638 i = 0;
4639 history[type][last_i] = history[type][i];
4640 last_i = i;
4641 }
4642 history[type][i].hisstr = str;
4643 history[type][i].hisnum = ++hisnum[type];
4644 return TRUE;
4645 }
4646 return FALSE;
4647}
4648
4649/*
4650 * Convert history name (from table above) to its HIST_ equivalent.
4651 * When "name" is empty, return "cmd" history.
4652 * Returns -1 for unknown history name.
4653 */
4654 int
4655get_histtype(name)
4656 char_u *name;
4657{
4658 int i;
4659 int len = (int)STRLEN(name);
4660
4661 /* No argument: use current history. */
4662 if (len == 0)
4663 return hist_char2type(ccline.cmdfirstc);
4664
4665 for (i = 0; history_names[i] != NULL; ++i)
4666 if (STRNICMP(name, history_names[i], len) == 0)
4667 return i;
4668
4669 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4670 return hist_char2type(name[0]);
4671
4672 return -1;
4673}
4674
4675static int last_maptick = -1; /* last seen maptick */
4676
4677/*
4678 * Add the given string to the given history. If the string is already in the
4679 * history then it is moved to the front. "histype" may be one of he HIST_
4680 * values.
4681 */
4682 void
4683add_to_history(histype, new_entry, in_map, sep)
4684 int histype;
4685 char_u *new_entry;
4686 int in_map; /* consider maptick when inside a mapping */
4687 int sep; /* separator character used (search hist) */
4688{
4689 histentry_T *hisptr;
4690 int len;
4691
4692 if (hislen == 0) /* no history */
4693 return;
4694
4695 /*
4696 * Searches inside the same mapping overwrite each other, so that only
4697 * the last line is kept. Be careful not to remove a line that was moved
4698 * down, only lines that were added.
4699 */
4700 if (histype == HIST_SEARCH && in_map)
4701 {
4702 if (maptick == last_maptick)
4703 {
4704 /* Current line is from the same mapping, remove it */
4705 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4706 vim_free(hisptr->hisstr);
4707 hisptr->hisstr = NULL;
4708 hisptr->hisnum = 0;
4709 --hisnum[histype];
4710 if (--hisidx[HIST_SEARCH] < 0)
4711 hisidx[HIST_SEARCH] = hislen - 1;
4712 }
4713 last_maptick = -1;
4714 }
4715 if (!in_history(histype, new_entry, TRUE))
4716 {
4717 if (++hisidx[histype] == hislen)
4718 hisidx[histype] = 0;
4719 hisptr = &history[histype][hisidx[histype]];
4720 vim_free(hisptr->hisstr);
4721
4722 /* Store the separator after the NUL of the string. */
4723 len = STRLEN(new_entry);
4724 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4725 if (hisptr->hisstr != NULL)
4726 hisptr->hisstr[len + 1] = sep;
4727
4728 hisptr->hisnum = ++hisnum[histype];
4729 if (histype == HIST_SEARCH && in_map)
4730 last_maptick = maptick;
4731 }
4732}
4733
4734#if defined(FEAT_EVAL) || defined(PROTO)
4735
4736/*
4737 * Get identifier of newest history entry.
4738 * "histype" may be one of the HIST_ values.
4739 */
4740 int
4741get_history_idx(histype)
4742 int histype;
4743{
4744 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4745 || hisidx[histype] < 0)
4746 return -1;
4747
4748 return history[histype][hisidx[histype]].hisnum;
4749}
4750
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004751static struct cmdline_info *get_ccline_ptr __ARGS((void));
4752
4753/*
4754 * Get pointer to the command line info to use. cmdline_paste() may clear
4755 * ccline and put the previous value in prev_ccline.
4756 */
4757 static struct cmdline_info *
4758get_ccline_ptr()
4759{
4760 if ((State & CMDLINE) == 0)
4761 return NULL;
4762 if (ccline.cmdbuff != NULL)
4763 return &ccline;
4764 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4765 return &prev_ccline;
4766 return NULL;
4767}
4768
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769/*
4770 * Get the current command line in allocated memory.
4771 * Only works when the command line is being edited.
4772 * Returns NULL when something is wrong.
4773 */
4774 char_u *
4775get_cmdline_str()
4776{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004777 struct cmdline_info *p = get_ccline_ptr();
4778
4779 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004781 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782}
4783
4784/*
4785 * Get the current command line position, counted in bytes.
4786 * Zero is the first position.
4787 * Only works when the command line is being edited.
4788 * Returns -1 when something is wrong.
4789 */
4790 int
4791get_cmdline_pos()
4792{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004793 struct cmdline_info *p = get_ccline_ptr();
4794
4795 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004797 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798}
4799
4800/*
4801 * Set the command line byte position to "pos". Zero is the first position.
4802 * Only works when the command line is being edited.
4803 * Returns 1 when failed, 0 when OK.
4804 */
4805 int
4806set_cmdline_pos(pos)
4807 int pos;
4808{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004809 struct cmdline_info *p = get_ccline_ptr();
4810
4811 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 return 1;
4813
4814 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4815 * changed the command line. */
4816 if (pos < 0)
4817 new_cmdpos = 0;
4818 else
4819 new_cmdpos = pos;
4820 return 0;
4821}
4822
4823/*
4824 * Calculate history index from a number:
4825 * num > 0: seen as identifying number of a history entry
4826 * num < 0: relative position in history wrt newest entry
4827 * "histype" may be one of the HIST_ values.
4828 */
4829 static int
4830calc_hist_idx(histype, num)
4831 int histype;
4832 int num;
4833{
4834 int i;
4835 histentry_T *hist;
4836 int wrapped = FALSE;
4837
4838 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4839 || (i = hisidx[histype]) < 0 || num == 0)
4840 return -1;
4841
4842 hist = history[histype];
4843 if (num > 0)
4844 {
4845 while (hist[i].hisnum > num)
4846 if (--i < 0)
4847 {
4848 if (wrapped)
4849 break;
4850 i += hislen;
4851 wrapped = TRUE;
4852 }
4853 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
4854 return i;
4855 }
4856 else if (-num <= hislen)
4857 {
4858 i += num + 1;
4859 if (i < 0)
4860 i += hislen;
4861 if (hist[i].hisstr != NULL)
4862 return i;
4863 }
4864 return -1;
4865}
4866
4867/*
4868 * Get a history entry by its index.
4869 * "histype" may be one of the HIST_ values.
4870 */
4871 char_u *
4872get_history_entry(histype, idx)
4873 int histype;
4874 int idx;
4875{
4876 idx = calc_hist_idx(histype, idx);
4877 if (idx >= 0)
4878 return history[histype][idx].hisstr;
4879 else
4880 return (char_u *)"";
4881}
4882
4883/*
4884 * Clear all entries of a history.
4885 * "histype" may be one of the HIST_ values.
4886 */
4887 int
4888clr_history(histype)
4889 int histype;
4890{
4891 int i;
4892 histentry_T *hisptr;
4893
4894 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
4895 {
4896 hisptr = history[histype];
4897 for (i = hislen; i--;)
4898 {
4899 vim_free(hisptr->hisstr);
4900 hisptr->hisnum = 0;
4901 hisptr++->hisstr = NULL;
4902 }
4903 hisidx[histype] = -1; /* mark history as cleared */
4904 hisnum[histype] = 0; /* reset identifier counter */
4905 return OK;
4906 }
4907 return FAIL;
4908}
4909
4910/*
4911 * Remove all entries matching {str} from a history.
4912 * "histype" may be one of the HIST_ values.
4913 */
4914 int
4915del_history_entry(histype, str)
4916 int histype;
4917 char_u *str;
4918{
4919 regmatch_T regmatch;
4920 histentry_T *hisptr;
4921 int idx;
4922 int i;
4923 int last;
4924 int found = FALSE;
4925
4926 regmatch.regprog = NULL;
4927 regmatch.rm_ic = FALSE; /* always match case */
4928 if (hislen != 0
4929 && histype >= 0
4930 && histype < HIST_COUNT
4931 && *str != NUL
4932 && (idx = hisidx[histype]) >= 0
4933 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
4934 != NULL)
4935 {
4936 i = last = idx;
4937 do
4938 {
4939 hisptr = &history[histype][i];
4940 if (hisptr->hisstr == NULL)
4941 break;
4942 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
4943 {
4944 found = TRUE;
4945 vim_free(hisptr->hisstr);
4946 hisptr->hisstr = NULL;
4947 hisptr->hisnum = 0;
4948 }
4949 else
4950 {
4951 if (i != last)
4952 {
4953 history[histype][last] = *hisptr;
4954 hisptr->hisstr = NULL;
4955 hisptr->hisnum = 0;
4956 }
4957 if (--last < 0)
4958 last += hislen;
4959 }
4960 if (--i < 0)
4961 i += hislen;
4962 } while (i != idx);
4963 if (history[histype][idx].hisstr == NULL)
4964 hisidx[histype] = -1;
4965 }
4966 vim_free(regmatch.regprog);
4967 return found;
4968}
4969
4970/*
4971 * Remove an indexed entry from a history.
4972 * "histype" may be one of the HIST_ values.
4973 */
4974 int
4975del_history_idx(histype, idx)
4976 int histype;
4977 int idx;
4978{
4979 int i, j;
4980
4981 i = calc_hist_idx(histype, idx);
4982 if (i < 0)
4983 return FALSE;
4984 idx = hisidx[histype];
4985 vim_free(history[histype][i].hisstr);
4986
4987 /* When deleting the last added search string in a mapping, reset
4988 * last_maptick, so that the last added search string isn't deleted again.
4989 */
4990 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
4991 last_maptick = -1;
4992
4993 while (i != idx)
4994 {
4995 j = (i + 1) % hislen;
4996 history[histype][i] = history[histype][j];
4997 i = j;
4998 }
4999 history[histype][i].hisstr = NULL;
5000 history[histype][i].hisnum = 0;
5001 if (--i < 0)
5002 i += hislen;
5003 hisidx[histype] = i;
5004 return TRUE;
5005}
5006
5007#endif /* FEAT_EVAL */
5008
5009#if defined(FEAT_CRYPT) || defined(PROTO)
5010/*
5011 * Very specific function to remove the value in ":set key=val" from the
5012 * history.
5013 */
5014 void
5015remove_key_from_history()
5016{
5017 char_u *p;
5018 int i;
5019
5020 i = hisidx[HIST_CMD];
5021 if (i < 0)
5022 return;
5023 p = history[HIST_CMD][i].hisstr;
5024 if (p != NULL)
5025 for ( ; *p; ++p)
5026 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5027 {
5028 p = vim_strchr(p + 3, '=');
5029 if (p == NULL)
5030 break;
5031 ++p;
5032 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5033 if (p[i] == '\\' && p[i + 1])
5034 ++i;
5035 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5036 --p;
5037 }
5038}
5039#endif
5040
5041#endif /* FEAT_CMDHIST */
5042
5043#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5044/*
5045 * Get indices "num1,num2" that specify a range within a list (not a range of
5046 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5047 * Returns OK if parsed successfully, otherwise FAIL.
5048 */
5049 int
5050get_list_range(str, num1, num2)
5051 char_u **str;
5052 int *num1;
5053 int *num2;
5054{
5055 int len;
5056 int first = FALSE;
5057 long num;
5058
5059 *str = skipwhite(*str);
5060 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5061 {
5062 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5063 *str += len;
5064 *num1 = (int)num;
5065 first = TRUE;
5066 }
5067 *str = skipwhite(*str);
5068 if (**str == ',') /* parse "to" part of range */
5069 {
5070 *str = skipwhite(*str + 1);
5071 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5072 if (len > 0)
5073 {
5074 *num2 = (int)num;
5075 *str = skipwhite(*str + len);
5076 }
5077 else if (!first) /* no number given at all */
5078 return FAIL;
5079 }
5080 else if (first) /* only one number given */
5081 *num2 = *num1;
5082 return OK;
5083}
5084#endif
5085
5086#if defined(FEAT_CMDHIST) || defined(PROTO)
5087/*
5088 * :history command - print a history
5089 */
5090 void
5091ex_history(eap)
5092 exarg_T *eap;
5093{
5094 histentry_T *hist;
5095 int histype1 = HIST_CMD;
5096 int histype2 = HIST_CMD;
5097 int hisidx1 = 1;
5098 int hisidx2 = -1;
5099 int idx;
5100 int i, j, k;
5101 char_u *end;
5102 char_u *arg = eap->arg;
5103
5104 if (hislen == 0)
5105 {
5106 MSG(_("'history' option is zero"));
5107 return;
5108 }
5109
5110 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5111 {
5112 end = arg;
5113 while (ASCII_ISALPHA(*end)
5114 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5115 end++;
5116 i = *end;
5117 *end = NUL;
5118 histype1 = get_histtype(arg);
5119 if (histype1 == -1)
5120 {
5121 if (STRICMP(arg, "all") == 0)
5122 {
5123 histype1 = 0;
5124 histype2 = HIST_COUNT-1;
5125 }
5126 else
5127 {
5128 *end = i;
5129 EMSG(_(e_trailing));
5130 return;
5131 }
5132 }
5133 else
5134 histype2 = histype1;
5135 *end = i;
5136 }
5137 else
5138 end = arg;
5139 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5140 {
5141 EMSG(_(e_trailing));
5142 return;
5143 }
5144
5145 for (; !got_int && histype1 <= histype2; ++histype1)
5146 {
5147 STRCPY(IObuff, "\n # ");
5148 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5149 MSG_PUTS_TITLE(IObuff);
5150 idx = hisidx[histype1];
5151 hist = history[histype1];
5152 j = hisidx1;
5153 k = hisidx2;
5154 if (j < 0)
5155 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5156 if (k < 0)
5157 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5158 if (idx >= 0 && j <= k)
5159 for (i = idx + 1; !got_int; ++i)
5160 {
5161 if (i == hislen)
5162 i = 0;
5163 if (hist[i].hisstr != NULL
5164 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5165 {
5166 msg_putchar('\n');
5167 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5168 hist[i].hisnum);
5169 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5170 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5171 (int)Columns - 10);
5172 else
5173 STRCAT(IObuff, hist[i].hisstr);
5174 msg_outtrans(IObuff);
5175 out_flush();
5176 }
5177 if (i == idx)
5178 break;
5179 }
5180 }
5181}
5182#endif
5183
5184#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5185static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5186static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5187static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5188static int viminfo_add_at_front = FALSE;
5189
5190static int hist_type2char __ARGS((int type, int use_question));
5191
5192/*
5193 * Translate a history type number to the associated character.
5194 */
5195 static int
5196hist_type2char(type, use_question)
5197 int type;
5198 int use_question; /* use '?' instead of '/' */
5199{
5200 if (type == HIST_CMD)
5201 return ':';
5202 if (type == HIST_SEARCH)
5203 {
5204 if (use_question)
5205 return '?';
5206 else
5207 return '/';
5208 }
5209 if (type == HIST_EXPR)
5210 return '=';
5211 return '@';
5212}
5213
5214/*
5215 * Prepare for reading the history from the viminfo file.
5216 * This allocates history arrays to store the read history lines.
5217 */
5218 void
5219prepare_viminfo_history(asklen)
5220 int asklen;
5221{
5222 int i;
5223 int num;
5224 int type;
5225 int len;
5226
5227 init_history();
5228 viminfo_add_at_front = (asklen != 0);
5229 if (asklen > hislen)
5230 asklen = hislen;
5231
5232 for (type = 0; type < HIST_COUNT; ++type)
5233 {
5234 /*
5235 * Count the number of empty spaces in the history list. If there are
5236 * more spaces available than we request, then fill them up.
5237 */
5238 for (i = 0, num = 0; i < hislen; i++)
5239 if (history[type][i].hisstr == NULL)
5240 num++;
5241 len = asklen;
5242 if (num > len)
5243 len = num;
5244 if (len <= 0)
5245 viminfo_history[type] = NULL;
5246 else
5247 viminfo_history[type] =
5248 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5249 if (viminfo_history[type] == NULL)
5250 len = 0;
5251 viminfo_hislen[type] = len;
5252 viminfo_hisidx[type] = 0;
5253 }
5254}
5255
5256/*
5257 * Accept a line from the viminfo, store it in the history array when it's
5258 * new.
5259 */
5260 int
5261read_viminfo_history(virp)
5262 vir_T *virp;
5263{
5264 int type;
5265 long_u len;
5266 char_u *val;
5267 char_u *p;
5268
5269 type = hist_char2type(virp->vir_line[0]);
5270 if (viminfo_hisidx[type] < viminfo_hislen[type])
5271 {
5272 val = viminfo_readstring(virp, 1, TRUE);
5273 if (val != NULL && *val != NUL)
5274 {
5275 if (!in_history(type, val + (type == HIST_SEARCH),
5276 viminfo_add_at_front))
5277 {
5278 /* Need to re-allocate to append the separator byte. */
5279 len = STRLEN(val);
5280 p = lalloc(len + 2, TRUE);
5281 if (p != NULL)
5282 {
5283 if (type == HIST_SEARCH)
5284 {
5285 /* Search entry: Move the separator from the first
5286 * column to after the NUL. */
5287 mch_memmove(p, val + 1, (size_t)len);
5288 p[len] = (*val == ' ' ? NUL : *val);
5289 }
5290 else
5291 {
5292 /* Not a search entry: No separator in the viminfo
5293 * file, add a NUL separator. */
5294 mch_memmove(p, val, (size_t)len + 1);
5295 p[len + 1] = NUL;
5296 }
5297 viminfo_history[type][viminfo_hisidx[type]++] = p;
5298 }
5299 }
5300 }
5301 vim_free(val);
5302 }
5303 return viminfo_readline(virp);
5304}
5305
5306 void
5307finish_viminfo_history()
5308{
5309 int idx;
5310 int i;
5311 int type;
5312
5313 for (type = 0; type < HIST_COUNT; ++type)
5314 {
5315 if (history[type] == NULL)
5316 return;
5317 idx = hisidx[type] + viminfo_hisidx[type];
5318 if (idx >= hislen)
5319 idx -= hislen;
5320 else if (idx < 0)
5321 idx = hislen - 1;
5322 if (viminfo_add_at_front)
5323 hisidx[type] = idx;
5324 else
5325 {
5326 if (hisidx[type] == -1)
5327 hisidx[type] = hislen - 1;
5328 do
5329 {
5330 if (history[type][idx].hisstr != NULL)
5331 break;
5332 if (++idx == hislen)
5333 idx = 0;
5334 } while (idx != hisidx[type]);
5335 if (idx != hisidx[type] && --idx < 0)
5336 idx = hislen - 1;
5337 }
5338 for (i = 0; i < viminfo_hisidx[type]; i++)
5339 {
5340 vim_free(history[type][idx].hisstr);
5341 history[type][idx].hisstr = viminfo_history[type][i];
5342 if (--idx < 0)
5343 idx = hislen - 1;
5344 }
5345 idx += 1;
5346 idx %= hislen;
5347 for (i = 0; i < viminfo_hisidx[type]; i++)
5348 {
5349 history[type][idx++].hisnum = ++hisnum[type];
5350 idx %= hislen;
5351 }
5352 vim_free(viminfo_history[type]);
5353 viminfo_history[type] = NULL;
5354 }
5355}
5356
5357 void
5358write_viminfo_history(fp)
5359 FILE *fp;
5360{
5361 int i;
5362 int type;
5363 int num_saved;
5364 char_u *p;
5365 int c;
5366
5367 init_history();
5368 if (hislen == 0)
5369 return;
5370 for (type = 0; type < HIST_COUNT; ++type)
5371 {
5372 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5373 if (num_saved == 0)
5374 continue;
5375 if (num_saved < 0) /* Use default */
5376 num_saved = hislen;
5377 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5378 type == HIST_CMD ? _("Command Line") :
5379 type == HIST_SEARCH ? _("Search String") :
5380 type == HIST_EXPR ? _("Expression") :
5381 _("Input Line"));
5382 if (num_saved > hislen)
5383 num_saved = hislen;
5384 i = hisidx[type];
5385 if (i >= 0)
5386 while (num_saved--)
5387 {
5388 p = history[type][i].hisstr;
5389 if (p != NULL)
5390 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005391 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 /* For the search history: put the separator in the second
5393 * column; use a space if there isn't one. */
5394 if (type == HIST_SEARCH)
5395 {
5396 c = p[STRLEN(p) + 1];
5397 putc(c == NUL ? ' ' : c, fp);
5398 }
5399 viminfo_writestring(fp, p);
5400 }
5401 if (--i < 0)
5402 i = hislen - 1;
5403 }
5404 }
5405}
5406#endif /* FEAT_VIMINFO */
5407
5408#if defined(FEAT_FKMAP) || defined(PROTO)
5409/*
5410 * Write a character at the current cursor+offset position.
5411 * It is directly written into the command buffer block.
5412 */
5413 void
5414cmd_pchar(c, offset)
5415 int c, offset;
5416{
5417 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5418 {
5419 EMSG(_("E198: cmd_pchar beyond the command length"));
5420 return;
5421 }
5422 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5423 ccline.cmdbuff[ccline.cmdlen] = NUL;
5424}
5425
5426 int
5427cmd_gchar(offset)
5428 int offset;
5429{
5430 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5431 {
5432 /* EMSG(_("cmd_gchar beyond the command length")); */
5433 return NUL;
5434 }
5435 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5436}
5437#endif
5438
5439#if defined(FEAT_CMDWIN) || defined(PROTO)
5440/*
5441 * Open a window on the current command line and history. Allow editing in
5442 * the window. Returns when the window is closed.
5443 * Returns:
5444 * CR if the command is to be executed
5445 * Ctrl_C if it is to be abandoned
5446 * K_IGNORE if editing continues
5447 */
5448 static int
5449ex_window()
5450{
5451 struct cmdline_info save_ccline;
5452 buf_T *old_curbuf = curbuf;
5453 win_T *old_curwin = curwin;
5454 buf_T *bp;
5455 win_T *wp;
5456 int i;
5457 linenr_T lnum;
5458 int histtype;
5459 garray_T winsizes;
5460 char_u typestr[2];
5461 int save_restart_edit = restart_edit;
5462 int save_State = State;
5463 int save_exmode = exmode_active;
5464
5465 /* Can't do this recursively. Can't do it when typing a password. */
5466 if (cmdwin_type != 0
5467# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5468 || cmdline_star > 0
5469# endif
5470 )
5471 {
5472 beep_flush();
5473 return K_IGNORE;
5474 }
5475
5476 /* Save current window sizes. */
5477 win_size_save(&winsizes);
5478
5479# ifdef FEAT_AUTOCMD
5480 /* Don't execute autocommands while creating the window. */
5481 ++autocmd_block;
5482# endif
5483 /* Create a window for the command-line buffer. */
5484 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5485 {
5486 beep_flush();
5487 return K_IGNORE;
5488 }
5489 cmdwin_type = ccline.cmdfirstc;
5490 if (cmdwin_type == NUL)
5491 cmdwin_type = '-';
5492
5493 /* Create the command-line buffer empty. */
5494 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5495 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5496 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5497 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5498 curbuf->b_p_ma = TRUE;
5499# ifdef FEAT_RIGHTLEFT
5500 curwin->w_p_rl = FALSE;
5501# endif
5502# ifdef FEAT_SCROLLBIND
5503 curwin->w_p_scb = FALSE;
5504# endif
5505
5506# ifdef FEAT_AUTOCMD
5507 /* Do execute autocommands for setting the filetype (load syntax). */
5508 --autocmd_block;
5509# endif
5510
5511 histtype = hist_char2type(ccline.cmdfirstc);
5512 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5513 {
5514 if (p_wc == TAB)
5515 {
5516 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5517 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5518 }
5519 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5520 }
5521
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005522 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5523 * sets 'textwidth' to 78). */
5524 curbuf->b_p_tw = 0;
5525
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 /* Fill the buffer with the history. */
5527 init_history();
5528 if (hislen > 0)
5529 {
5530 i = hisidx[histtype];
5531 if (i >= 0)
5532 {
5533 lnum = 0;
5534 do
5535 {
5536 if (++i == hislen)
5537 i = 0;
5538 if (history[histtype][i].hisstr != NULL)
5539 ml_append(lnum++, history[histtype][i].hisstr,
5540 (colnr_T)0, FALSE);
5541 }
5542 while (i != hisidx[histtype]);
5543 }
5544 }
5545
5546 /* Replace the empty last line with the current command-line and put the
5547 * cursor there. */
5548 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5549 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5550 curwin->w_cursor.col = ccline.cmdpos;
5551 redraw_later(NOT_VALID);
5552
5553 /* Save the command line info, can be used recursively. */
5554 save_ccline = ccline;
5555 ccline.cmdbuff = NULL;
5556 ccline.cmdprompt = NULL;
5557
5558 /* No Ex mode here! */
5559 exmode_active = 0;
5560
5561 State = NORMAL;
5562# ifdef FEAT_MOUSE
5563 setmouse();
5564# endif
5565
5566# ifdef FEAT_AUTOCMD
5567 /* Trigger CmdwinEnter autocommands. */
5568 typestr[0] = cmdwin_type;
5569 typestr[1] = NUL;
5570 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5571# endif
5572
5573 i = RedrawingDisabled;
5574 RedrawingDisabled = 0;
5575
5576 /*
5577 * Call the main loop until <CR> or CTRL-C is typed.
5578 */
5579 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005580 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581
5582 RedrawingDisabled = i;
5583
5584# ifdef FEAT_AUTOCMD
5585 /* Trigger CmdwinLeave autocommands. */
5586 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5587# endif
5588
5589 /* Restore the comand line info. */
5590 ccline = save_ccline;
5591 cmdwin_type = 0;
5592
5593 exmode_active = save_exmode;
5594
5595 /* Safety check: The old window or buffer was deleted: It's a a bug when
5596 * this happens! */
5597 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5598 {
5599 cmdwin_result = Ctrl_C;
5600 EMSG(_("E199: Active window or buffer deleted"));
5601 }
5602 else
5603 {
5604# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5605 /* autocmds may abort script processing */
5606 if (aborting() && cmdwin_result != K_IGNORE)
5607 cmdwin_result = Ctrl_C;
5608# endif
5609 /* Set the new command line from the cmdline buffer. */
5610 vim_free(ccline.cmdbuff);
5611 if (cmdwin_result == K_XF1) /* :qa! typed */
5612 {
5613 ccline.cmdbuff = vim_strsave((char_u *)"qa!");
5614 cmdwin_result = CAR;
5615 }
5616 else if (cmdwin_result == K_XF2) /* :qa typed */
5617 {
5618 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5619 cmdwin_result = CAR;
5620 }
5621 else
5622 ccline.cmdbuff = vim_strsave(ml_get_curline());
5623 if (ccline.cmdbuff == NULL)
5624 cmdwin_result = Ctrl_C;
5625 else
5626 {
5627 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5628 ccline.cmdbufflen = ccline.cmdlen + 1;
5629 ccline.cmdpos = curwin->w_cursor.col;
5630 if (ccline.cmdpos > ccline.cmdlen)
5631 ccline.cmdpos = ccline.cmdlen;
5632 if (cmdwin_result == K_IGNORE)
5633 {
5634 set_cmdspos_cursor();
5635 redrawcmd();
5636 }
5637 }
5638
5639# ifdef FEAT_AUTOCMD
5640 /* Don't execute autocommands while deleting the window. */
5641 ++autocmd_block;
5642# endif
5643 wp = curwin;
5644 bp = curbuf;
5645 win_goto(old_curwin);
5646 win_close(wp, TRUE);
5647 close_buffer(NULL, bp, DOBUF_WIPE);
5648
5649 /* Restore window sizes. */
5650 win_size_restore(&winsizes);
5651
5652# ifdef FEAT_AUTOCMD
5653 --autocmd_block;
5654# endif
5655 }
5656
5657 ga_clear(&winsizes);
5658 restart_edit = save_restart_edit;
5659
5660 State = save_State;
5661# ifdef FEAT_MOUSE
5662 setmouse();
5663# endif
5664
5665 return cmdwin_result;
5666}
5667#endif /* FEAT_CMDWIN */
5668
5669/*
5670 * Used for commands that either take a simple command string argument, or:
5671 * cmd << endmarker
5672 * {script}
5673 * endmarker
5674 * Returns a pointer to allocated memory with {script} or NULL.
5675 */
5676 char_u *
5677script_get(eap, cmd)
5678 exarg_T *eap;
5679 char_u *cmd;
5680{
5681 char_u *theline;
5682 char *end_pattern = NULL;
5683 char dot[] = ".";
5684 garray_T ga;
5685
5686 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5687 return NULL;
5688
5689 ga_init2(&ga, 1, 0x400);
5690
5691 if (cmd[2] != NUL)
5692 end_pattern = (char *)skipwhite(cmd + 2);
5693 else
5694 end_pattern = dot;
5695
5696 for (;;)
5697 {
5698 theline = eap->getline(
5699#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005700 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701#endif
5702 NUL, eap->cookie, 0);
5703
5704 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5705 break;
5706
5707 ga_concat(&ga, theline);
5708 ga_append(&ga, '\n');
5709 vim_free(theline);
5710 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005711 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
5713 return (char_u *)ga.ga_data;
5714}