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