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