blob: d221249e7eba4208bdb28106b6ed3ae6e0b402cb [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
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002310#if defined(FEAT_ARABIC) || defined(PROTO)
2311static char_u *arshape_buf = NULL;
2312
2313# if defined(EXITFREE) || defined(PROTO)
2314 void
2315free_cmdline_buf()
2316{
2317 vim_free(arshape_buf);
2318}
2319# endif
2320#endif
2321
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322/*
2323 * Draw part of the cmdline at the current cursor position. But draw stars
2324 * when cmdline_star is TRUE.
2325 */
2326 static void
2327draw_cmdline(start, len)
2328 int start;
2329 int len;
2330{
2331#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2332 int i;
2333
2334 if (cmdline_star > 0)
2335 for (i = 0; i < len; ++i)
2336 {
2337 msg_putchar('*');
2338# ifdef FEAT_MBYTE
2339 if (has_mbyte)
2340 i += (*mb_ptr2len_check)(ccline.cmdbuff + start + i) - 1;
2341# endif
2342 }
2343 else
2344#endif
2345#ifdef FEAT_ARABIC
2346 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 static int buflen = 0;
2349 char_u *p;
2350 int j;
2351 int newlen = 0;
2352 int mb_l;
2353 int pc, pc1;
2354 int prev_c = 0;
2355 int prev_c1 = 0;
2356 int u8c, u8c_c1, u8c_c2;
2357 int nc = 0;
2358 int dummy;
2359
2360 /*
2361 * Do arabic shaping into a temporary buffer. This is very
2362 * inefficient!
2363 */
2364 if (len * 2 > buflen)
2365 {
2366 /* Re-allocate the buffer. We keep it around to avoid a lot of
2367 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002368 vim_free(arshape_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 buflen = len * 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002370 arshape_buf = alloc(buflen);
2371 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 return; /* out of memory */
2373 }
2374
2375 for (j = start; j < start + len; j += mb_l)
2376 {
2377 p = ccline.cmdbuff + j;
2378 u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
2379 mb_l = utfc_ptr2len_check_len(p, start + len - j);
2380 if (ARABIC_CHAR(u8c))
2381 {
2382 /* Do Arabic shaping. */
2383 if (cmdmsg_rl)
2384 {
2385 /* displaying from right to left */
2386 pc = prev_c;
2387 pc1 = prev_c1;
2388 prev_c1 = u8c_c1;
2389 if (j + mb_l >= start + len)
2390 nc = NUL;
2391 else
2392 nc = utf_ptr2char(p + mb_l);
2393 }
2394 else
2395 {
2396 /* displaying from left to right */
2397 if (j + mb_l >= start + len)
2398 pc = NUL;
2399 else
2400 pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
2401 start + len - j - mb_l);
2402 nc = prev_c;
2403 }
2404 prev_c = u8c;
2405
2406 u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
2407
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002408 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 if (u8c_c1 != 0)
2410 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002411 newlen += (*mb_char2bytes)(u8c_c1, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (u8c_c2 != 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002413 newlen += (*mb_char2bytes)(u8c_c2,
2414 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 }
2417 else
2418 {
2419 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002420 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 newlen += mb_l;
2422 }
2423 }
2424
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002425 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 }
2427 else
2428#endif
2429 msg_outtrans_len(ccline.cmdbuff + start, len);
2430}
2431
2432/*
2433 * Put a character on the command line. Shifts the following text to the
2434 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2435 * "c" must be printable (fit in one display cell)!
2436 */
2437 void
2438putcmdline(c, shift)
2439 int c;
2440 int shift;
2441{
2442 if (cmd_silent)
2443 return;
2444 msg_no_more = TRUE;
2445 msg_putchar(c);
2446 if (shift)
2447 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2448 msg_no_more = FALSE;
2449 cursorcmd();
2450}
2451
2452/*
2453 * Undo a putcmdline(c, FALSE).
2454 */
2455 void
2456unputcmdline()
2457{
2458 if (cmd_silent)
2459 return;
2460 msg_no_more = TRUE;
2461 if (ccline.cmdlen == ccline.cmdpos)
2462 msg_putchar(' ');
2463 else
2464 draw_cmdline(ccline.cmdpos, 1);
2465 msg_no_more = FALSE;
2466 cursorcmd();
2467}
2468
2469/*
2470 * Put the given string, of the given length, onto the command line.
2471 * If len is -1, then STRLEN() is used to calculate the length.
2472 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2473 * part will be redrawn, otherwise it will not. If this function is called
2474 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2475 * called afterwards.
2476 */
2477 int
2478put_on_cmdline(str, len, redraw)
2479 char_u *str;
2480 int len;
2481 int redraw;
2482{
2483 int retval;
2484 int i;
2485 int m;
2486 int c;
2487
2488 if (len < 0)
2489 len = (int)STRLEN(str);
2490
2491 /* Check if ccline.cmdbuff needs to be longer */
2492 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2493 retval = realloc_cmdbuff(ccline.cmdlen + len);
2494 else
2495 retval = OK;
2496 if (retval == OK)
2497 {
2498 if (!ccline.overstrike)
2499 {
2500 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2501 ccline.cmdbuff + ccline.cmdpos,
2502 (size_t)(ccline.cmdlen - ccline.cmdpos));
2503 ccline.cmdlen += len;
2504 }
2505 else
2506 {
2507#ifdef FEAT_MBYTE
2508 if (has_mbyte)
2509 {
2510 /* Count nr of characters in the new string. */
2511 m = 0;
2512 for (i = 0; i < len; i += (*mb_ptr2len_check)(str + i))
2513 ++m;
2514 /* Count nr of bytes in cmdline that are overwritten by these
2515 * characters. */
2516 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
2517 i += (*mb_ptr2len_check)(ccline.cmdbuff + i))
2518 --m;
2519 if (i < ccline.cmdlen)
2520 {
2521 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2522 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2523 ccline.cmdlen += ccline.cmdpos + len - i;
2524 }
2525 else
2526 ccline.cmdlen = ccline.cmdpos + len;
2527 }
2528 else
2529#endif
2530 if (ccline.cmdpos + len > ccline.cmdlen)
2531 ccline.cmdlen = ccline.cmdpos + len;
2532 }
2533 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2534 ccline.cmdbuff[ccline.cmdlen] = NUL;
2535
2536#ifdef FEAT_MBYTE
2537 if (enc_utf8)
2538 {
2539 /* When the inserted text starts with a composing character,
2540 * backup to the character before it. There could be two of them.
2541 */
2542 i = 0;
2543 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2544 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2545 {
2546 i = (*mb_head_off)(ccline.cmdbuff,
2547 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2548 ccline.cmdpos -= i;
2549 len += i;
2550 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2551 }
2552# ifdef FEAT_ARABIC
2553 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2554 {
2555 /* Check the previous character for Arabic combining pair. */
2556 i = (*mb_head_off)(ccline.cmdbuff,
2557 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2558 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2559 + ccline.cmdpos - i), c))
2560 {
2561 ccline.cmdpos -= i;
2562 len += i;
2563 }
2564 else
2565 i = 0;
2566 }
2567# endif
2568 if (i != 0)
2569 {
2570 /* Also backup the cursor position. */
2571 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2572 ccline.cmdspos -= i;
2573 msg_col -= i;
2574 if (msg_col < 0)
2575 {
2576 msg_col += Columns;
2577 --msg_row;
2578 }
2579 }
2580 }
2581#endif
2582
2583 if (redraw && !cmd_silent)
2584 {
2585 msg_no_more = TRUE;
2586 i = cmdline_row;
2587 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2588 /* Avoid clearing the rest of the line too often. */
2589 if (cmdline_row != i || ccline.overstrike)
2590 msg_clr_eos();
2591 msg_no_more = FALSE;
2592 }
2593#ifdef FEAT_FKMAP
2594 /*
2595 * If we are in Farsi command mode, the character input must be in
2596 * Insert mode. So do not advance the cmdpos.
2597 */
2598 if (!cmd_fkmap)
2599#endif
2600 {
2601 if (KeyTyped)
2602 m = Columns * Rows;
2603 else
2604 m = MAXCOL;
2605 for (i = 0; i < len; ++i)
2606 {
2607 c = cmdline_charsize(ccline.cmdpos);
2608#ifdef FEAT_MBYTE
2609 /* count ">" for a double-wide char that doesn't fit. */
2610 if (has_mbyte)
2611 correct_cmdspos(ccline.cmdpos, c);
2612#endif
2613 /* Stop cursor at the end of the screen */
2614 if (ccline.cmdspos + c >= m)
2615 break;
2616 ccline.cmdspos += c;
2617#ifdef FEAT_MBYTE
2618 if (has_mbyte)
2619 {
2620 c = (*mb_ptr2len_check)(ccline.cmdbuff + ccline.cmdpos) - 1;
2621 if (c > len - i - 1)
2622 c = len - i - 1;
2623 ccline.cmdpos += c;
2624 i += c;
2625 }
2626#endif
2627 ++ccline.cmdpos;
2628 }
2629 }
2630 }
2631 if (redraw)
2632 msg_check();
2633 return retval;
2634}
2635
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002636static struct cmdline_info prev_ccline;
2637static int prev_ccline_used = FALSE;
2638
2639/*
2640 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2641 * and overwrite it. But get_cmdline_str() may need it, thus make it
2642 * available globally in prev_ccline.
2643 */
2644 static void
2645save_cmdline(ccp)
2646 struct cmdline_info *ccp;
2647{
2648 if (!prev_ccline_used)
2649 {
2650 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2651 prev_ccline_used = TRUE;
2652 }
2653 *ccp = prev_ccline;
2654 prev_ccline = ccline;
2655 ccline.cmdbuff = NULL;
2656 ccline.cmdprompt = NULL;
2657}
2658
2659/*
2660 * Resture ccline after it has been saved with save_cmdline().
2661 */
2662 static void
2663restore_cmdline(ccp)
2664 struct cmdline_info *ccp;
2665{
2666 ccline = prev_ccline;
2667 prev_ccline = *ccp;
2668}
2669
Bram Moolenaar8299df92004-07-10 09:47:34 +00002670/*
2671 * paste a yank register into the command line.
2672 * used by CTRL-R command in command-line mode
2673 * insert_reg() can't be used here, because special characters from the
2674 * register contents will be interpreted as commands.
2675 *
2676 * return FAIL for failure, OK otherwise
2677 */
2678 static int
2679cmdline_paste(regname, literally)
2680 int regname;
2681 int literally; /* Insert text literally instead of "as typed" */
2682{
2683 long i;
2684 char_u *arg;
2685 int allocated;
2686 struct cmdline_info save_ccline;
2687
2688 /* check for valid regname; also accept special characters for CTRL-R in
2689 * the command line */
2690 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2691 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2692 return FAIL;
2693
2694 /* A register containing CTRL-R can cause an endless loop. Allow using
2695 * CTRL-C to break the loop. */
2696 line_breakcheck();
2697 if (got_int)
2698 return FAIL;
2699
2700#ifdef FEAT_CLIPBOARD
2701 regname = may_get_selection(regname);
2702#endif
2703
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002704 /* Need to save and restore ccline. */
2705 save_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002706 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002707 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002708
2709 if (i)
2710 {
2711 /* Got the value of a special register in "arg". */
2712 if (arg == NULL)
2713 return FAIL;
2714 cmdline_paste_str(arg, literally);
2715 if (allocated)
2716 vim_free(arg);
2717 return OK;
2718 }
2719
2720 return cmdline_paste_reg(regname, literally);
2721}
2722
2723/*
2724 * Put a string on the command line.
2725 * When "literally" is TRUE, insert literally.
2726 * When "literally" is FALSE, insert as typed, but don't leave the command
2727 * line.
2728 */
2729 void
2730cmdline_paste_str(s, literally)
2731 char_u *s;
2732 int literally;
2733{
2734 int c, cv;
2735
2736 if (literally)
2737 put_on_cmdline(s, -1, TRUE);
2738 else
2739 while (*s != NUL)
2740 {
2741 cv = *s;
2742 if (cv == Ctrl_V && s[1])
2743 ++s;
2744#ifdef FEAT_MBYTE
2745 if (has_mbyte)
2746 {
2747 c = mb_ptr2char(s);
2748 s += mb_char2len(c);
2749 }
2750 else
2751#endif
2752 c = *s++;
2753 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2754#ifdef UNIX
2755 || c == intr_char
2756#endif
2757 || (c == Ctrl_BSL && *s == Ctrl_N))
2758 stuffcharReadbuff(Ctrl_V);
2759 stuffcharReadbuff(c);
2760 }
2761}
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763#ifdef FEAT_WILDMENU
2764/*
2765 * Delete characters on the command line, from "from" to the current
2766 * position.
2767 */
2768 static void
2769cmdline_del(from)
2770 int from;
2771{
2772 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2773 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2774 ccline.cmdlen -= ccline.cmdpos - from;
2775 ccline.cmdpos = from;
2776}
2777#endif
2778
2779/*
2780 * this fuction is called when the screen size changes and with incremental
2781 * search
2782 */
2783 void
2784redrawcmdline()
2785{
2786 if (cmd_silent)
2787 return;
2788 need_wait_return = FALSE;
2789 compute_cmdrow();
2790 redrawcmd();
2791 cursorcmd();
2792}
2793
2794 static void
2795redrawcmdprompt()
2796{
2797 int i;
2798
2799 if (cmd_silent)
2800 return;
2801 if (ccline.cmdfirstc)
2802 msg_putchar(ccline.cmdfirstc);
2803 if (ccline.cmdprompt != NULL)
2804 {
2805 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2806 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2807 /* do the reverse of set_cmdspos() */
2808 if (ccline.cmdfirstc)
2809 --ccline.cmdindent;
2810 }
2811 else
2812 for (i = ccline.cmdindent; i > 0; --i)
2813 msg_putchar(' ');
2814}
2815
2816/*
2817 * Redraw what is currently on the command line.
2818 */
2819 void
2820redrawcmd()
2821{
2822 if (cmd_silent)
2823 return;
2824
2825 msg_start();
2826 redrawcmdprompt();
2827
2828 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2829 msg_no_more = TRUE;
2830 draw_cmdline(0, ccline.cmdlen);
2831 msg_clr_eos();
2832 msg_no_more = FALSE;
2833
2834 set_cmdspos_cursor();
2835
2836 /*
2837 * An emsg() before may have set msg_scroll. This is used in normal mode,
2838 * in cmdline mode we can reset them now.
2839 */
2840 msg_scroll = FALSE; /* next message overwrites cmdline */
2841
2842 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2843 * in cmdline mode */
2844 skip_redraw = FALSE;
2845}
2846
2847 void
2848compute_cmdrow()
2849{
2850 if (exmode_active || msg_scrolled)
2851 cmdline_row = Rows - 1;
2852 else
2853 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2854 + W_STATUS_HEIGHT(lastwin);
2855}
2856
2857 static void
2858cursorcmd()
2859{
2860 if (cmd_silent)
2861 return;
2862
2863#ifdef FEAT_RIGHTLEFT
2864 if (cmdmsg_rl)
2865 {
2866 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2867 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2868 if (msg_row <= 0)
2869 msg_row = Rows - 1;
2870 }
2871 else
2872#endif
2873 {
2874 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
2875 msg_col = ccline.cmdspos % (int)Columns;
2876 if (msg_row >= Rows)
2877 msg_row = Rows - 1;
2878 }
2879
2880 windgoto(msg_row, msg_col);
2881#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2882 redrawcmd_preedit();
2883#endif
2884#ifdef MCH_CURSOR_SHAPE
2885 mch_update_cursor();
2886#endif
2887}
2888
2889 void
2890gotocmdline(clr)
2891 int clr;
2892{
2893 msg_start();
2894#ifdef FEAT_RIGHTLEFT
2895 if (cmdmsg_rl)
2896 msg_col = Columns - 1;
2897 else
2898#endif
2899 msg_col = 0; /* always start in column 0 */
2900 if (clr) /* clear the bottom line(s) */
2901 msg_clr_eos(); /* will reset clear_cmdline */
2902 windgoto(cmdline_row, 0);
2903}
2904
2905/*
2906 * Check the word in front of the cursor for an abbreviation.
2907 * Called when the non-id character "c" has been entered.
2908 * When an abbreviation is recognized it is removed from the text with
2909 * backspaces and the replacement string is inserted, followed by "c".
2910 */
2911 static int
2912ccheck_abbr(c)
2913 int c;
2914{
2915 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
2916 return FALSE;
2917
2918 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
2919}
2920
2921/*
2922 * Return FAIL if this is not an appropriate context in which to do
2923 * completion of anything, return OK if it is (even if there are no matches).
2924 * For the caller, this means that the character is just passed through like a
2925 * normal character (instead of being expanded). This allows :s/^I^D etc.
2926 */
2927 static int
2928nextwild(xp, type, options)
2929 expand_T *xp;
2930 int type;
2931 int options; /* extra options for ExpandOne() */
2932{
2933 int i, j;
2934 char_u *p1;
2935 char_u *p2;
2936 int oldlen;
2937 int difflen;
2938 int v;
2939
2940 if (xp->xp_numfiles == -1)
2941 {
2942 set_expand_context(xp);
2943 cmd_showtail = expand_showtail(xp);
2944 }
2945
2946 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2947 {
2948 beep_flush();
2949 return OK; /* Something illegal on command line */
2950 }
2951 if (xp->xp_context == EXPAND_NOTHING)
2952 {
2953 /* Caller can use the character as a normal char instead */
2954 return FAIL;
2955 }
2956
2957 MSG_PUTS("..."); /* show that we are busy */
2958 out_flush();
2959
2960 i = (int)(xp->xp_pattern - ccline.cmdbuff);
2961 oldlen = ccline.cmdpos - i;
2962
2963 if (type == WILD_NEXT || type == WILD_PREV)
2964 {
2965 /*
2966 * Get next/previous match for a previous expanded pattern.
2967 */
2968 p2 = ExpandOne(xp, NULL, NULL, 0, type);
2969 }
2970 else
2971 {
2972 /*
2973 * Translate string into pattern and expand it.
2974 */
2975 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
2976 p2 = NULL;
2977 else
2978 {
2979 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
2980 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
2981 |options, type);
2982 vim_free(p1);
2983 /* longest match: make sure it is not shorter (happens with :help */
2984 if (p2 != NULL && type == WILD_LONGEST)
2985 {
2986 for (j = 0; j < oldlen; ++j)
2987 if (ccline.cmdbuff[i + j] == '*'
2988 || ccline.cmdbuff[i + j] == '?')
2989 break;
2990 if ((int)STRLEN(p2) < j)
2991 {
2992 vim_free(p2);
2993 p2 = NULL;
2994 }
2995 }
2996 }
2997 }
2998
2999 if (p2 != NULL && !got_int)
3000 {
3001 difflen = (int)STRLEN(p2) - oldlen;
3002 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3003 {
3004 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3005 xp->xp_pattern = ccline.cmdbuff + i;
3006 }
3007 else
3008 v = OK;
3009 if (v == OK)
3010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003011 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3012 &ccline.cmdbuff[ccline.cmdpos],
3013 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3014 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 ccline.cmdlen += difflen;
3016 ccline.cmdpos += difflen;
3017 }
3018 }
3019 vim_free(p2);
3020
3021 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003022 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
3024 /* When expanding a ":map" command and no matches are found, assume that
3025 * the key is supposed to be inserted literally */
3026 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3027 return FAIL;
3028
3029 if (xp->xp_numfiles <= 0 && p2 == NULL)
3030 beep_flush();
3031 else if (xp->xp_numfiles == 1)
3032 /* free expanded pattern */
3033 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3034
3035 return OK;
3036}
3037
3038/*
3039 * Do wildcard expansion on the string 'str'.
3040 * Chars that should not be expanded must be preceded with a backslash.
3041 * Return a pointer to alloced memory containing the new string.
3042 * Return NULL for failure.
3043 *
3044 * Results are cached in xp->xp_files and xp->xp_numfiles.
3045 *
3046 * mode = WILD_FREE: just free previously expanded matches
3047 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3048 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3049 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3050 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3051 * mode = WILD_ALL: return all matches concatenated
3052 * mode = WILD_LONGEST: return longest matched part
3053 *
3054 * options = WILD_LIST_NOTFOUND: list entries without a match
3055 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3056 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3057 * options = WILD_NO_BEEP: Don't beep for multiple matches
3058 * options = WILD_ADD_SLASH: add a slash after directory names
3059 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3060 * options = WILD_SILENT: don't print warning messages
3061 * options = WILD_ESCAPE: put backslash before special chars
3062 *
3063 * The variables xp->xp_context and xp->xp_backslash must have been set!
3064 */
3065 char_u *
3066ExpandOne(xp, str, orig, options, mode)
3067 expand_T *xp;
3068 char_u *str;
3069 char_u *orig; /* allocated copy of original of expanded string */
3070 int options;
3071 int mode;
3072{
3073 char_u *ss = NULL;
3074 static int findex;
3075 static char_u *orig_save = NULL; /* kept value of orig */
3076 int i;
3077 long_u len;
3078 int non_suf_match; /* number without matching suffix */
3079
3080 /*
3081 * first handle the case of using an old match
3082 */
3083 if (mode == WILD_NEXT || mode == WILD_PREV)
3084 {
3085 if (xp->xp_numfiles > 0)
3086 {
3087 if (mode == WILD_PREV)
3088 {
3089 if (findex == -1)
3090 findex = xp->xp_numfiles;
3091 --findex;
3092 }
3093 else /* mode == WILD_NEXT */
3094 ++findex;
3095
3096 /*
3097 * When wrapping around, return the original string, set findex to
3098 * -1.
3099 */
3100 if (findex < 0)
3101 {
3102 if (orig_save == NULL)
3103 findex = xp->xp_numfiles - 1;
3104 else
3105 findex = -1;
3106 }
3107 if (findex >= xp->xp_numfiles)
3108 {
3109 if (orig_save == NULL)
3110 findex = 0;
3111 else
3112 findex = -1;
3113 }
3114#ifdef FEAT_WILDMENU
3115 if (p_wmnu)
3116 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3117 findex, cmd_showtail);
3118#endif
3119 if (findex == -1)
3120 return vim_strsave(orig_save);
3121 return vim_strsave(xp->xp_files[findex]);
3122 }
3123 else
3124 return NULL;
3125 }
3126
3127/* free old names */
3128 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3129 {
3130 FreeWild(xp->xp_numfiles, xp->xp_files);
3131 xp->xp_numfiles = -1;
3132 vim_free(orig_save);
3133 orig_save = NULL;
3134 }
3135 findex = 0;
3136
3137 if (mode == WILD_FREE) /* only release file name */
3138 return NULL;
3139
3140 if (xp->xp_numfiles == -1)
3141 {
3142 vim_free(orig_save);
3143 orig_save = orig;
3144
3145 /*
3146 * Do the expansion.
3147 */
3148 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3149 options) == FAIL)
3150 {
3151#ifdef FNAME_ILLEGAL
3152 /* Illegal file name has been silently skipped. But when there
3153 * are wildcards, the real problem is that there was no match,
3154 * causing the pattern to be added, which has illegal characters.
3155 */
3156 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3157 EMSG2(_(e_nomatch2), str);
3158#endif
3159 }
3160 else if (xp->xp_numfiles == 0)
3161 {
3162 if (!(options & WILD_SILENT))
3163 EMSG2(_(e_nomatch2), str);
3164 }
3165 else
3166 {
3167 /* Escape the matches for use on the command line. */
3168 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3169
3170 /*
3171 * Check for matching suffixes in file names.
3172 */
3173 if (mode != WILD_ALL && mode != WILD_LONGEST)
3174 {
3175 if (xp->xp_numfiles)
3176 non_suf_match = xp->xp_numfiles;
3177 else
3178 non_suf_match = 1;
3179 if ((xp->xp_context == EXPAND_FILES
3180 || xp->xp_context == EXPAND_DIRECTORIES)
3181 && xp->xp_numfiles > 1)
3182 {
3183 /*
3184 * More than one match; check suffix.
3185 * The files will have been sorted on matching suffix in
3186 * expand_wildcards, only need to check the first two.
3187 */
3188 non_suf_match = 0;
3189 for (i = 0; i < 2; ++i)
3190 if (match_suffix(xp->xp_files[i]))
3191 ++non_suf_match;
3192 }
3193 if (non_suf_match != 1)
3194 {
3195 /* Can we ever get here unless it's while expanding
3196 * interactively? If not, we can get rid of this all
3197 * together. Don't really want to wait for this message
3198 * (and possibly have to hit return to continue!).
3199 */
3200 if (!(options & WILD_SILENT))
3201 EMSG(_(e_toomany));
3202 else if (!(options & WILD_NO_BEEP))
3203 beep_flush();
3204 }
3205 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3206 ss = vim_strsave(xp->xp_files[0]);
3207 }
3208 }
3209 }
3210
3211 /* Find longest common part */
3212 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3213 {
3214 for (len = 0; xp->xp_files[0][len]; ++len)
3215 {
3216 for (i = 0; i < xp->xp_numfiles; ++i)
3217 {
3218#ifdef CASE_INSENSITIVE_FILENAME
3219 if (xp->xp_context == EXPAND_DIRECTORIES
3220 || xp->xp_context == EXPAND_FILES
3221 || xp->xp_context == EXPAND_BUFFERS)
3222 {
3223 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3224 TOLOWER_LOC(xp->xp_files[0][len]))
3225 break;
3226 }
3227 else
3228#endif
3229 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3230 break;
3231 }
3232 if (i < xp->xp_numfiles)
3233 {
3234 if (!(options & WILD_NO_BEEP))
3235 vim_beep();
3236 break;
3237 }
3238 }
3239 ss = alloc((unsigned)len + 1);
3240 if (ss)
3241 {
3242 STRNCPY(ss, xp->xp_files[0], len);
3243 ss[len] = NUL;
3244 }
3245 findex = -1; /* next p_wc gets first one */
3246 }
3247
3248 /* Concatenate all matching names */
3249 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3250 {
3251 len = 0;
3252 for (i = 0; i < xp->xp_numfiles; ++i)
3253 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3254 ss = lalloc(len, TRUE);
3255 if (ss != NULL)
3256 {
3257 *ss = NUL;
3258 for (i = 0; i < xp->xp_numfiles; ++i)
3259 {
3260 STRCAT(ss, xp->xp_files[i]);
3261 if (i != xp->xp_numfiles - 1)
3262 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3263 }
3264 }
3265 }
3266
3267 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3268 ExpandCleanup(xp);
3269
3270 return ss;
3271}
3272
3273/*
3274 * Prepare an expand structure for use.
3275 */
3276 void
3277ExpandInit(xp)
3278 expand_T *xp;
3279{
3280 xp->xp_backslash = XP_BS_NONE;
3281 xp->xp_numfiles = -1;
3282 xp->xp_files = NULL;
3283}
3284
3285/*
3286 * Cleanup an expand structure after use.
3287 */
3288 void
3289ExpandCleanup(xp)
3290 expand_T *xp;
3291{
3292 if (xp->xp_numfiles >= 0)
3293 {
3294 FreeWild(xp->xp_numfiles, xp->xp_files);
3295 xp->xp_numfiles = -1;
3296 }
3297}
3298
3299 void
3300ExpandEscape(xp, str, numfiles, files, options)
3301 expand_T *xp;
3302 char_u *str;
3303 int numfiles;
3304 char_u **files;
3305 int options;
3306{
3307 int i;
3308 char_u *p;
3309
3310 /*
3311 * May change home directory back to "~"
3312 */
3313 if (options & WILD_HOME_REPLACE)
3314 tilde_replace(str, numfiles, files);
3315
3316 if (options & WILD_ESCAPE)
3317 {
3318 if (xp->xp_context == EXPAND_FILES
3319 || xp->xp_context == EXPAND_BUFFERS
3320 || xp->xp_context == EXPAND_DIRECTORIES)
3321 {
3322 /*
3323 * Insert a backslash into a file name before a space, \, %, #
3324 * and wildmatch characters, except '~'.
3325 */
3326 for (i = 0; i < numfiles; ++i)
3327 {
3328 /* for ":set path=" we need to escape spaces twice */
3329 if (xp->xp_backslash == XP_BS_THREE)
3330 {
3331 p = vim_strsave_escaped(files[i], (char_u *)" ");
3332 if (p != NULL)
3333 {
3334 vim_free(files[i]);
3335 files[i] = p;
3336#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3337 p = vim_strsave_escaped(files[i], (char_u *)" ");
3338 if (p != NULL)
3339 {
3340 vim_free(files[i]);
3341 files[i] = p;
3342 }
3343#endif
3344 }
3345 }
3346#ifdef BACKSLASH_IN_FILENAME
3347 {
3348 char_u buf[20];
3349 int j = 0;
3350
3351 /* Don't escape '[' and '{' if they are in 'isfname'. */
3352 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3353 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3354 buf[j++] = *p;
3355 buf[j] = NUL;
3356 p = vim_strsave_escaped(files[i], buf);
3357 }
3358#else
3359 p = vim_strsave_escaped(files[i], PATH_ESC_CHARS);
3360#endif
3361 if (p != NULL)
3362 {
3363 vim_free(files[i]);
3364 files[i] = p;
3365 }
3366
3367 /* If 'str' starts with "\~", replace "~" at start of
3368 * files[i] with "\~". */
3369 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
3370 {
3371 p = alloc((unsigned)(STRLEN(files[i]) + 2));
3372 if (p != NULL)
3373 {
3374 p[0] = '\\';
3375 STRCPY(p + 1, files[i]);
3376 vim_free(files[i]);
3377 files[i] = p;
3378 }
3379 }
3380 }
3381 xp->xp_backslash = XP_BS_NONE;
3382 }
3383 else if (xp->xp_context == EXPAND_TAGS)
3384 {
3385 /*
3386 * Insert a backslash before characters in a tag name that
3387 * would terminate the ":tag" command.
3388 */
3389 for (i = 0; i < numfiles; ++i)
3390 {
3391 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3392 if (p != NULL)
3393 {
3394 vim_free(files[i]);
3395 files[i] = p;
3396 }
3397 }
3398 }
3399 }
3400}
3401
3402/*
3403 * For each file name in files[num_files]:
3404 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3405 */
3406 void
3407tilde_replace(orig_pat, num_files, files)
3408 char_u *orig_pat;
3409 int num_files;
3410 char_u **files;
3411{
3412 int i;
3413 char_u *p;
3414
3415 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3416 {
3417 for (i = 0; i < num_files; ++i)
3418 {
3419 p = home_replace_save(NULL, files[i]);
3420 if (p != NULL)
3421 {
3422 vim_free(files[i]);
3423 files[i] = p;
3424 }
3425 }
3426 }
3427}
3428
3429/*
3430 * Show all matches for completion on the command line.
3431 * Returns EXPAND_NOTHING when the character that triggered expansion should
3432 * be inserted like a normal character.
3433 */
3434/*ARGSUSED*/
3435 static int
3436showmatches(xp, wildmenu)
3437 expand_T *xp;
3438 int wildmenu;
3439{
3440#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3441 int num_files;
3442 char_u **files_found;
3443 int i, j, k;
3444 int maxlen;
3445 int lines;
3446 int columns;
3447 char_u *p;
3448 int lastlen;
3449 int attr;
3450 int showtail;
3451
3452 if (xp->xp_numfiles == -1)
3453 {
3454 set_expand_context(xp);
3455 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3456 &num_files, &files_found);
3457 showtail = expand_showtail(xp);
3458 if (i != EXPAND_OK)
3459 return i;
3460
3461 }
3462 else
3463 {
3464 num_files = xp->xp_numfiles;
3465 files_found = xp->xp_files;
3466 showtail = cmd_showtail;
3467 }
3468
3469#ifdef FEAT_WILDMENU
3470 if (!wildmenu)
3471 {
3472#endif
3473 msg_didany = FALSE; /* lines_left will be set */
3474 msg_start(); /* prepare for paging */
3475 msg_putchar('\n');
3476 out_flush();
3477 cmdline_row = msg_row;
3478 msg_didany = FALSE; /* lines_left will be set again */
3479 msg_start(); /* prepare for paging */
3480#ifdef FEAT_WILDMENU
3481 }
3482#endif
3483
3484 if (got_int)
3485 got_int = FALSE; /* only int. the completion, not the cmd line */
3486#ifdef FEAT_WILDMENU
3487 else if (wildmenu)
3488 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3489#endif
3490 else
3491 {
3492 /* find the length of the longest file name */
3493 maxlen = 0;
3494 for (i = 0; i < num_files; ++i)
3495 {
3496 if (!showtail && (xp->xp_context == EXPAND_FILES
3497 || xp->xp_context == EXPAND_BUFFERS))
3498 {
3499 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3500 j = vim_strsize(NameBuff);
3501 }
3502 else
3503 j = vim_strsize(L_SHOWFILE(i));
3504 if (j > maxlen)
3505 maxlen = j;
3506 }
3507
3508 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3509 lines = num_files;
3510 else
3511 {
3512 /* compute the number of columns and lines for the listing */
3513 maxlen += 2; /* two spaces between file names */
3514 columns = ((int)Columns + 2) / maxlen;
3515 if (columns < 1)
3516 columns = 1;
3517 lines = (num_files + columns - 1) / columns;
3518 }
3519
3520 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3521
3522 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3523 {
3524 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3525 msg_clr_eos();
3526 msg_advance(maxlen - 3);
3527 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3528 }
3529
3530 /* list the files line by line */
3531 for (i = 0; i < lines; ++i)
3532 {
3533 lastlen = 999;
3534 for (k = i; k < num_files; k += lines)
3535 {
3536 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3537 {
3538 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3539 p = files_found[k] + STRLEN(files_found[k]) + 1;
3540 msg_advance(maxlen + 1);
3541 msg_puts(p);
3542 msg_advance(maxlen + 3);
3543 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3544 break;
3545 }
3546 for (j = maxlen - lastlen; --j >= 0; )
3547 msg_putchar(' ');
3548 if (xp->xp_context == EXPAND_FILES
3549 || xp->xp_context == EXPAND_BUFFERS)
3550 {
3551 /* highlight directories */
3552 j = (mch_isdir(files_found[k]));
3553 if (showtail)
3554 p = L_SHOWFILE(k);
3555 else
3556 {
3557 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3558 TRUE);
3559 p = NameBuff;
3560 }
3561 }
3562 else
3563 {
3564 j = FALSE;
3565 p = L_SHOWFILE(k);
3566 }
3567 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3568 }
3569 if (msg_col > 0) /* when not wrapped around */
3570 {
3571 msg_clr_eos();
3572 msg_putchar('\n');
3573 }
3574 out_flush(); /* show one line at a time */
3575 if (got_int)
3576 {
3577 got_int = FALSE;
3578 break;
3579 }
3580 }
3581
3582 /*
3583 * we redraw the command below the lines that we have just listed
3584 * This is a bit tricky, but it saves a lot of screen updating.
3585 */
3586 cmdline_row = msg_row; /* will put it back later */
3587 }
3588
3589 if (xp->xp_numfiles == -1)
3590 FreeWild(num_files, files_found);
3591
3592 return EXPAND_OK;
3593}
3594
3595/*
3596 * Private gettail for showmatches() (and win_redr_status_matches()):
3597 * Find tail of file name path, but ignore trailing "/".
3598 */
3599 char_u *
3600sm_gettail(s)
3601 char_u *s;
3602{
3603 char_u *p;
3604 char_u *t = s;
3605 int had_sep = FALSE;
3606
3607 for (p = s; *p != NUL; )
3608 {
3609 if (vim_ispathsep(*p)
3610#ifdef BACKSLASH_IN_FILENAME
3611 && !rem_backslash(p)
3612#endif
3613 )
3614 had_sep = TRUE;
3615 else if (had_sep)
3616 {
3617 t = p;
3618 had_sep = FALSE;
3619 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003620 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622 return t;
3623}
3624
3625/*
3626 * Return TRUE if we only need to show the tail of completion matches.
3627 * When not completing file names or there is a wildcard in the path FALSE is
3628 * returned.
3629 */
3630 static int
3631expand_showtail(xp)
3632 expand_T *xp;
3633{
3634 char_u *s;
3635 char_u *end;
3636
3637 /* When not completing file names a "/" may mean something different. */
3638 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3639 return FALSE;
3640
3641 end = gettail(xp->xp_pattern);
3642 if (end == xp->xp_pattern) /* there is no path separator */
3643 return FALSE;
3644
3645 for (s = xp->xp_pattern; s < end; s++)
3646 {
3647 /* Skip escaped wildcards. Only when the backslash is not a path
3648 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3649 if (rem_backslash(s))
3650 ++s;
3651 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3652 return FALSE;
3653 }
3654 return TRUE;
3655}
3656
3657/*
3658 * Prepare a string for expansion.
3659 * When expanding file names: The string will be used with expand_wildcards().
3660 * Copy the file name into allocated memory and add a '*' at the end.
3661 * When expanding other names: The string will be used with regcomp(). Copy
3662 * the name into allocated memory and prepend "^".
3663 */
3664 char_u *
3665addstar(fname, len, context)
3666 char_u *fname;
3667 int len;
3668 int context; /* EXPAND_FILES etc. */
3669{
3670 char_u *retval;
3671 int i, j;
3672 int new_len;
3673 char_u *tail;
3674
3675 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3676 {
3677 /*
3678 * Matching will be done internally (on something other than files).
3679 * So we convert the file-matching-type wildcards into our kind for
3680 * use with vim_regcomp(). First work out how long it will be:
3681 */
3682
3683 /* For help tags the translation is done in find_help_tags().
3684 * For a tag pattern starting with "/" no translation is needed. */
3685 if (context == EXPAND_HELP
3686 || context == EXPAND_COLORS
3687 || context == EXPAND_COMPILER
3688 || (context == EXPAND_TAGS && fname[0] == '/'))
3689 retval = vim_strnsave(fname, len);
3690 else
3691 {
3692 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3693 for (i = 0; i < len; i++)
3694 {
3695 if (fname[i] == '*' || fname[i] == '~')
3696 new_len++; /* '*' needs to be replaced by ".*"
3697 '~' needs to be replaced by "\~" */
3698
3699 /* Buffer names are like file names. "." should be literal */
3700 if (context == EXPAND_BUFFERS && fname[i] == '.')
3701 new_len++; /* "." becomes "\." */
3702
3703 /* Custom expansion takes care of special things, match
3704 * backslashes literally (perhaps also for other types?) */
3705 if (context == EXPAND_USER_DEFINED && fname[i] == '\\')
3706 new_len++; /* '\' becomes "\\" */
3707 }
3708 retval = alloc(new_len);
3709 if (retval != NULL)
3710 {
3711 retval[0] = '^';
3712 j = 1;
3713 for (i = 0; i < len; i++, j++)
3714 {
3715 /* Skip backslash. But why? At least keep it for custom
3716 * expansion. */
3717 if (context != EXPAND_USER_DEFINED
3718 && fname[i] == '\\' && ++i == len)
3719 break;
3720
3721 switch (fname[i])
3722 {
3723 case '*': retval[j++] = '.';
3724 break;
3725 case '~': retval[j++] = '\\';
3726 break;
3727 case '?': retval[j] = '.';
3728 continue;
3729 case '.': if (context == EXPAND_BUFFERS)
3730 retval[j++] = '\\';
3731 break;
3732 case '\\': if (context == EXPAND_USER_DEFINED)
3733 retval[j++] = '\\';
3734 break;
3735 }
3736 retval[j] = fname[i];
3737 }
3738 retval[j] = NUL;
3739 }
3740 }
3741 }
3742 else
3743 {
3744 retval = alloc(len + 4);
3745 if (retval != NULL)
3746 {
3747 STRNCPY(retval, fname, len);
3748 retval[len] = NUL;
3749
3750 /*
3751 * Don't add a star to ~, ~user, $var or `cmd`.
3752 * ~ would be at the start of the file name, but not the tail.
3753 * $ could be anywhere in the tail.
3754 * ` could be anywhere in the file name.
3755 */
3756 tail = gettail(retval);
3757 if ((*retval != '~' || tail != retval)
3758 && vim_strchr(tail, '$') == NULL
3759 && vim_strchr(retval, '`') == NULL)
3760 retval[len++] = '*';
3761 retval[len] = NUL;
3762 }
3763 }
3764 return retval;
3765}
3766
3767/*
3768 * Must parse the command line so far to work out what context we are in.
3769 * Completion can then be done based on that context.
3770 * This routine sets the variables:
3771 * xp->xp_pattern The start of the pattern to be expanded within
3772 * the command line (ends at the cursor).
3773 * xp->xp_context The type of thing to expand. Will be one of:
3774 *
3775 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3776 * the command line, like an unknown command. Caller
3777 * should beep.
3778 * EXPAND_NOTHING Unrecognised context for completion, use char like
3779 * a normal char, rather than for completion. eg
3780 * :s/^I/
3781 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3782 * it.
3783 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3784 * EXPAND_FILES After command with XFILE set, or after setting
3785 * with P_EXPAND set. eg :e ^I, :w>>^I
3786 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3787 * when we know only directories are of interest. eg
3788 * :set dir=^I
3789 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3790 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3791 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3792 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3793 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3794 * EXPAND_EVENTS Complete event names
3795 * EXPAND_SYNTAX Complete :syntax command arguments
3796 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3797 * EXPAND_AUGROUP Complete autocommand group names
3798 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3799 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3800 * eg :unmap a^I , :cunab x^I
3801 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3802 * eg :call sub^I
3803 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3804 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3805 * names in expressions, eg :while s^I
3806 * EXPAND_ENV_VARS Complete environment variable names
3807 */
3808 static void
3809set_expand_context(xp)
3810 expand_T *xp;
3811{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003812 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (ccline.cmdfirstc != ':'
3814#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003815 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816#endif
3817 )
3818 {
3819 xp->xp_context = EXPAND_NOTHING;
3820 return;
3821 }
3822 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3823}
3824
3825 void
3826set_cmd_context(xp, str, len, col)
3827 expand_T *xp;
3828 char_u *str; /* start of command line */
3829 int len; /* length of command line (excl. NUL) */
3830 int col; /* position of cursor */
3831{
3832 int old_char = NUL;
3833 char_u *nextcomm;
3834
3835 /*
3836 * Avoid a UMR warning from Purify, only save the character if it has been
3837 * written before.
3838 */
3839 if (col < len)
3840 old_char = str[col];
3841 str[col] = NUL;
3842 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003843
3844#ifdef FEAT_EVAL
3845 if (ccline.cmdfirstc == '=')
3846 /* pass CMD_SIZE because there is no real command */
3847 set_context_for_expression(xp, str, CMD_SIZE);
3848 else
3849#endif
3850 while (nextcomm != NULL)
3851 nextcomm = set_one_cmd_context(xp, nextcomm);
3852
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 str[col] = old_char;
3854}
3855
3856/*
3857 * Expand the command line "str" from context "xp".
3858 * "xp" must have been set by set_cmd_context().
3859 * xp->xp_pattern points into "str", to where the text that is to be expanded
3860 * starts.
3861 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
3862 * cursor.
3863 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
3864 * key that triggered expansion literally.
3865 * Returns EXPAND_OK otherwise.
3866 */
3867 int
3868expand_cmdline(xp, str, col, matchcount, matches)
3869 expand_T *xp;
3870 char_u *str; /* start of command line */
3871 int col; /* position of cursor */
3872 int *matchcount; /* return: nr of matches */
3873 char_u ***matches; /* return: array of pointers to matches */
3874{
3875 char_u *file_str = NULL;
3876
3877 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3878 {
3879 beep_flush();
3880 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
3881 }
3882 if (xp->xp_context == EXPAND_NOTHING)
3883 {
3884 /* Caller can use the character as a normal char instead */
3885 return EXPAND_NOTHING;
3886 }
3887
3888 /* add star to file name, or convert to regexp if not exp. files. */
3889 file_str = addstar(xp->xp_pattern,
3890 (int)(str + col - xp->xp_pattern), xp->xp_context);
3891 if (file_str == NULL)
3892 return EXPAND_UNSUCCESSFUL;
3893
3894 /* find all files that match the description */
3895 if (ExpandFromContext(xp, file_str, matchcount, matches,
3896 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
3897 {
3898 *matchcount = 0;
3899 *matches = NULL;
3900 }
3901 vim_free(file_str);
3902
3903 return EXPAND_OK;
3904}
3905
3906#ifdef FEAT_MULTI_LANG
3907/*
3908 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
3909 */
3910static void cleanup_help_tags __ARGS((int num_file, char_u **file));
3911
3912 static void
3913cleanup_help_tags(num_file, file)
3914 int num_file;
3915 char_u **file;
3916{
3917 int i, j;
3918 int len;
3919
3920 for (i = 0; i < num_file; ++i)
3921 {
3922 len = (int)STRLEN(file[i]) - 3;
3923 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
3924 {
3925 /* Sorting on priority means the same item in another language may
3926 * be anywhere. Search all items for a match up to the "@en". */
3927 for (j = 0; j < num_file; ++j)
3928 if (j != i
3929 && (int)STRLEN(file[j]) == len + 3
3930 && STRNCMP(file[i], file[j], len + 1) == 0)
3931 break;
3932 if (j == num_file)
3933 file[i][len] = NUL;
3934 }
3935 }
3936}
3937#endif
3938
3939/*
3940 * Do the expansion based on xp->xp_context and "pat".
3941 */
3942 static int
3943ExpandFromContext(xp, pat, num_file, file, options)
3944 expand_T *xp;
3945 char_u *pat;
3946 int *num_file;
3947 char_u ***file;
3948 int options;
3949{
3950#ifdef FEAT_CMDL_COMPL
3951 regmatch_T regmatch;
3952#endif
3953 int ret;
3954 int flags;
3955
3956 flags = EW_DIR; /* include directories */
3957 if (options & WILD_LIST_NOTFOUND)
3958 flags |= EW_NOTFOUND;
3959 if (options & WILD_ADD_SLASH)
3960 flags |= EW_ADDSLASH;
3961 if (options & WILD_KEEP_ALL)
3962 flags |= EW_KEEPALL;
3963 if (options & WILD_SILENT)
3964 flags |= EW_SILENT;
3965
3966 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
3967 {
3968 /*
3969 * Expand file or directory names.
3970 */
3971 int free_pat = FALSE;
3972 int i;
3973
3974 /* for ":set path=" and ":set tags=" halve backslashes for escaped
3975 * space */
3976 if (xp->xp_backslash != XP_BS_NONE)
3977 {
3978 free_pat = TRUE;
3979 pat = vim_strsave(pat);
3980 for (i = 0; pat[i]; ++i)
3981 if (pat[i] == '\\')
3982 {
3983 if (xp->xp_backslash == XP_BS_THREE
3984 && pat[i + 1] == '\\'
3985 && pat[i + 2] == '\\'
3986 && pat[i + 3] == ' ')
3987 STRCPY(pat + i, pat + i + 3);
3988 if (xp->xp_backslash == XP_BS_ONE
3989 && pat[i + 1] == ' ')
3990 STRCPY(pat + i, pat + i + 1);
3991 }
3992 }
3993
3994 if (xp->xp_context == EXPAND_FILES)
3995 flags |= EW_FILE;
3996 else
3997 flags = (flags | EW_DIR) & ~EW_FILE;
3998 ret = expand_wildcards(1, &pat, num_file, file, flags);
3999 if (free_pat)
4000 vim_free(pat);
4001 return ret;
4002 }
4003
4004 *file = (char_u **)"";
4005 *num_file = 0;
4006 if (xp->xp_context == EXPAND_HELP)
4007 {
4008 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4009 {
4010#ifdef FEAT_MULTI_LANG
4011 cleanup_help_tags(*num_file, *file);
4012#endif
4013 return OK;
4014 }
4015 return FAIL;
4016 }
4017
4018#ifndef FEAT_CMDL_COMPL
4019 return FAIL;
4020#else
4021 if (xp->xp_context == EXPAND_OLD_SETTING)
4022 return ExpandOldSetting(num_file, file);
4023 if (xp->xp_context == EXPAND_BUFFERS)
4024 return ExpandBufnames(pat, num_file, file, options);
4025 if (xp->xp_context == EXPAND_TAGS
4026 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4027 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4028 if (xp->xp_context == EXPAND_COLORS)
4029 return ExpandRTDir(pat, num_file, file, "colors");
4030 if (xp->xp_context == EXPAND_COMPILER)
4031 return ExpandRTDir(pat, num_file, file, "compiler");
4032
4033 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4034 if (regmatch.regprog == NULL)
4035 return FAIL;
4036
4037 /* set ignore-case according to p_ic, p_scs and pat */
4038 regmatch.rm_ic = ignorecase(pat);
4039
4040 if (xp->xp_context == EXPAND_SETTINGS
4041 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4042 ret = ExpandSettings(xp, &regmatch, num_file, file);
4043 else if (xp->xp_context == EXPAND_MAPPINGS)
4044 ret = ExpandMappings(&regmatch, num_file, file);
4045# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4046 else if (xp->xp_context == EXPAND_USER_DEFINED)
4047 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4048# endif
4049 else
4050 {
4051 static struct expgen
4052 {
4053 int context;
4054 char_u *((*func)__ARGS((expand_T *, int)));
4055 int ic;
4056 } tab[] =
4057 {
4058 {EXPAND_COMMANDS, get_command_name, FALSE},
4059#ifdef FEAT_USR_CMDS
4060 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4061 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4062 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4063 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4064#endif
4065#ifdef FEAT_EVAL
4066 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4067 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4068 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4069 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4070#endif
4071#ifdef FEAT_MENU
4072 {EXPAND_MENUS, get_menu_name, FALSE},
4073 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4074#endif
4075#ifdef FEAT_SYN_HL
4076 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4077#endif
4078 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4079#ifdef FEAT_AUTOCMD
4080 {EXPAND_EVENTS, get_event_name, TRUE},
4081 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4082#endif
4083#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4084 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4085 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4086#endif
4087 {EXPAND_ENV_VARS, get_env_name, TRUE},
4088 };
4089 int i;
4090
4091 /*
4092 * Find a context in the table and call the ExpandGeneric() with the
4093 * right function to do the expansion.
4094 */
4095 ret = FAIL;
4096 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4097 if (xp->xp_context == tab[i].context)
4098 {
4099 if (tab[i].ic)
4100 regmatch.rm_ic = TRUE;
4101 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4102 break;
4103 }
4104 }
4105
4106 vim_free(regmatch.regprog);
4107
4108 return ret;
4109#endif /* FEAT_CMDL_COMPL */
4110}
4111
4112#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4113/*
4114 * Expand a list of names.
4115 *
4116 * Generic function for command line completion. It calls a function to
4117 * obtain strings, one by one. The strings are matched against a regexp
4118 * program. Matching strings are copied into an array, which is returned.
4119 *
4120 * Returns OK when no problems encountered, FAIL for error (out of memory).
4121 */
4122 int
4123ExpandGeneric(xp, regmatch, num_file, file, func)
4124 expand_T *xp;
4125 regmatch_T *regmatch;
4126 int *num_file;
4127 char_u ***file;
4128 char_u *((*func)__ARGS((expand_T *, int)));
4129 /* returns a string from the list */
4130{
4131 int i;
4132 int count = 0;
4133 int loop;
4134 char_u *str;
4135
4136 /* do this loop twice:
4137 * loop == 0: count the number of matching names
4138 * loop == 1: copy the matching names into allocated memory
4139 */
4140 for (loop = 0; loop <= 1; ++loop)
4141 {
4142 for (i = 0; ; ++i)
4143 {
4144 str = (*func)(xp, i);
4145 if (str == NULL) /* end of list */
4146 break;
4147 if (*str == NUL) /* skip empty strings */
4148 continue;
4149
4150 if (vim_regexec(regmatch, str, (colnr_T)0))
4151 {
4152 if (loop)
4153 {
4154 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4155 (*file)[count] = str;
4156#ifdef FEAT_MENU
4157 if (func == get_menu_names && str != NULL)
4158 {
4159 /* test for separator added by get_menu_names() */
4160 str += STRLEN(str) - 1;
4161 if (*str == '\001')
4162 *str = '.';
4163 }
4164#endif
4165 }
4166 ++count;
4167 }
4168 }
4169 if (loop == 0)
4170 {
4171 if (count == 0)
4172 return OK;
4173 *num_file = count;
4174 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4175 if (*file == NULL)
4176 {
4177 *file = (char_u **)"";
4178 return FAIL;
4179 }
4180 count = 0;
4181 }
4182 }
4183 return OK;
4184}
4185
4186# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4187/*
4188 * Expand names with a function defined by the user.
4189 */
4190 static int
4191ExpandUserDefined(xp, regmatch, num_file, file)
4192 expand_T *xp;
4193 regmatch_T *regmatch;
4194 int *num_file;
4195 char_u ***file;
4196{
4197 char_u *args[3];
4198 char_u *all;
4199 char_u *s;
4200 char_u *e;
4201 char_u keep;
4202 char_u num[50];
4203 garray_T ga;
4204 int save_current_SID = current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004205 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
4207 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
4208 return FAIL;
4209 *num_file = 0;
4210 *file = NULL;
4211
4212 keep = ccline.cmdbuff[ccline.cmdlen];
4213 ccline.cmdbuff[ccline.cmdlen] = 0;
4214 sprintf((char *)num, "%d", ccline.cmdpos);
4215 args[0] = xp->xp_pattern;
4216 args[1] = ccline.cmdbuff;
4217 args[2] = num;
4218
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004219 /* Save the cmdline, we don't know what the function may do. */
4220 save_ccline = ccline;
4221 ccline.cmdbuff = NULL;
4222 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004224
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 all = call_vim_function(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004226
4227 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004229
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 ccline.cmdbuff[ccline.cmdlen] = keep;
4231 if (all == NULL)
4232 return FAIL;
4233
4234 ga_init2(&ga, (int)sizeof(char *), 3);
4235 for (s = all; *s != NUL; s = e)
4236 {
4237 e = vim_strchr(s, '\n');
4238 if (e == NULL)
4239 e = s + STRLEN(s);
4240 keep = *e;
4241 *e = 0;
4242
4243 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4244 {
4245 *e = keep;
4246 if (*e != NUL)
4247 ++e;
4248 continue;
4249 }
4250
4251 if (ga_grow(&ga, 1) == FAIL)
4252 break;
4253
4254 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4255 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 *e = keep;
4258 if (*e != NUL)
4259 ++e;
4260 }
4261 vim_free(all);
4262 *file = ga.ga_data;
4263 *num_file = ga.ga_len;
4264 return OK;
4265}
4266#endif
4267
4268/*
4269 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4270 * or compiler names.
4271 */
4272 static int
4273ExpandRTDir(pat, num_file, file, dirname)
4274 char_u *pat;
4275 int *num_file;
4276 char_u ***file;
4277 char *dirname; /* "colors" or "compiler" */
4278{
4279 char_u *all;
4280 char_u *s;
4281 char_u *e;
4282 garray_T ga;
4283
4284 *num_file = 0;
4285 *file = NULL;
4286 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4287 if (s == NULL)
4288 return FAIL;
4289 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4290 all = globpath(p_rtp, s);
4291 vim_free(s);
4292 if (all == NULL)
4293 return FAIL;
4294
4295 ga_init2(&ga, (int)sizeof(char *), 3);
4296 for (s = all; *s != NUL; s = e)
4297 {
4298 e = vim_strchr(s, '\n');
4299 if (e == NULL)
4300 e = s + STRLEN(s);
4301 if (ga_grow(&ga, 1) == FAIL)
4302 break;
4303 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4304 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004305 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (*s == '\n' || vim_ispathsep(*s))
4307 break;
4308 ++s;
4309 ((char_u **)ga.ga_data)[ga.ga_len] =
4310 vim_strnsave(s, (int)(e - s - 4));
4311 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 }
4313 if (*e != NUL)
4314 ++e;
4315 }
4316 vim_free(all);
4317 *file = ga.ga_data;
4318 *num_file = ga.ga_len;
4319 return OK;
4320}
4321
4322#endif
4323
4324#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4325/*
4326 * Expand "file" for all comma-separated directories in "path".
4327 * Returns an allocated string with all matches concatenated, separated by
4328 * newlines. Returns NULL for an error or no matches.
4329 */
4330 char_u *
4331globpath(path, file)
4332 char_u *path;
4333 char_u *file;
4334{
4335 expand_T xpc;
4336 char_u *buf;
4337 garray_T ga;
4338 int i;
4339 int len;
4340 int num_p;
4341 char_u **p;
4342 char_u *cur = NULL;
4343
4344 buf = alloc(MAXPATHL);
4345 if (buf == NULL)
4346 return NULL;
4347
4348 xpc.xp_context = EXPAND_FILES;
4349 xpc.xp_backslash = XP_BS_NONE;
4350 ga_init2(&ga, 1, 100);
4351
4352 /* Loop over all entries in {path}. */
4353 while (*path != NUL)
4354 {
4355 /* Copy one item of the path to buf[] and concatenate the file name. */
4356 copy_option_part(&path, buf, MAXPATHL, ",");
4357 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4358 {
4359 add_pathsep(buf);
4360 STRCAT(buf, file);
4361 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4362 && num_p > 0)
4363 {
4364 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4365 for (len = 0, i = 0; i < num_p; ++i)
4366 len += (long_u)STRLEN(p[i]) + 1;
4367
4368 /* Concatenate new results to previous ones. */
4369 if (ga_grow(&ga, len) == OK)
4370 {
4371 cur = (char_u *)ga.ga_data + ga.ga_len;
4372 for (i = 0; i < num_p; ++i)
4373 {
4374 STRCPY(cur, p[i]);
4375 cur += STRLEN(p[i]);
4376 *cur++ = '\n';
4377 }
4378 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 }
4380 FreeWild(num_p, p);
4381 }
4382 }
4383 }
4384 if (cur != NULL)
4385 *--cur = 0; /* Replace trailing newline with NUL */
4386
4387 vim_free(buf);
4388 return (char_u *)ga.ga_data;
4389}
4390
4391#endif
4392
4393#if defined(FEAT_CMDHIST) || defined(PROTO)
4394
4395/*********************************
4396 * Command line history stuff *
4397 *********************************/
4398
4399/*
4400 * Translate a history character to the associated type number.
4401 */
4402 static int
4403hist_char2type(c)
4404 int c;
4405{
4406 if (c == ':')
4407 return HIST_CMD;
4408 if (c == '=')
4409 return HIST_EXPR;
4410 if (c == '@')
4411 return HIST_INPUT;
4412 if (c == '>')
4413 return HIST_DEBUG;
4414 return HIST_SEARCH; /* must be '?' or '/' */
4415}
4416
4417/*
4418 * Table of history names.
4419 * These names are used in :history and various hist...() functions.
4420 * It is sufficient to give the significant prefix of a history name.
4421 */
4422
4423static char *(history_names[]) =
4424{
4425 "cmd",
4426 "search",
4427 "expr",
4428 "input",
4429 "debug",
4430 NULL
4431};
4432
4433/*
4434 * init_history() - Initialize the command line history.
4435 * Also used to re-allocate the history when the size changes.
4436 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004437 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438init_history()
4439{
4440 int newlen; /* new length of history table */
4441 histentry_T *temp;
4442 int i;
4443 int j;
4444 int type;
4445
4446 /*
4447 * If size of history table changed, reallocate it
4448 */
4449 newlen = (int)p_hi;
4450 if (newlen != hislen) /* history length changed */
4451 {
4452 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4453 {
4454 if (newlen)
4455 {
4456 temp = (histentry_T *)lalloc(
4457 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4458 if (temp == NULL) /* out of memory! */
4459 {
4460 if (type == 0) /* first one: just keep the old length */
4461 {
4462 newlen = hislen;
4463 break;
4464 }
4465 /* Already changed one table, now we can only have zero
4466 * length for all tables. */
4467 newlen = 0;
4468 type = -1;
4469 continue;
4470 }
4471 }
4472 else
4473 temp = NULL;
4474 if (newlen == 0 || temp != NULL)
4475 {
4476 if (hisidx[type] < 0) /* there are no entries yet */
4477 {
4478 for (i = 0; i < newlen; ++i)
4479 {
4480 temp[i].hisnum = 0;
4481 temp[i].hisstr = NULL;
4482 }
4483 }
4484 else if (newlen > hislen) /* array becomes bigger */
4485 {
4486 for (i = 0; i <= hisidx[type]; ++i)
4487 temp[i] = history[type][i];
4488 j = i;
4489 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4490 {
4491 temp[i].hisnum = 0;
4492 temp[i].hisstr = NULL;
4493 }
4494 for ( ; j < hislen; ++i, ++j)
4495 temp[i] = history[type][j];
4496 }
4497 else /* array becomes smaller or 0 */
4498 {
4499 j = hisidx[type];
4500 for (i = newlen - 1; ; --i)
4501 {
4502 if (i >= 0) /* copy newest entries */
4503 temp[i] = history[type][j];
4504 else /* remove older entries */
4505 vim_free(history[type][j].hisstr);
4506 if (--j < 0)
4507 j = hislen - 1;
4508 if (j == hisidx[type])
4509 break;
4510 }
4511 hisidx[type] = newlen - 1;
4512 }
4513 vim_free(history[type]);
4514 history[type] = temp;
4515 }
4516 }
4517 hislen = newlen;
4518 }
4519}
4520
4521/*
4522 * Check if command line 'str' is already in history.
4523 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4524 */
4525 static int
4526in_history(type, str, move_to_front)
4527 int type;
4528 char_u *str;
4529 int move_to_front; /* Move the entry to the front if it exists */
4530{
4531 int i;
4532 int last_i = -1;
4533
4534 if (hisidx[type] < 0)
4535 return FALSE;
4536 i = hisidx[type];
4537 do
4538 {
4539 if (history[type][i].hisstr == NULL)
4540 return FALSE;
4541 if (STRCMP(str, history[type][i].hisstr) == 0)
4542 {
4543 if (!move_to_front)
4544 return TRUE;
4545 last_i = i;
4546 break;
4547 }
4548 if (--i < 0)
4549 i = hislen - 1;
4550 } while (i != hisidx[type]);
4551
4552 if (last_i >= 0)
4553 {
4554 str = history[type][i].hisstr;
4555 while (i != hisidx[type])
4556 {
4557 if (++i >= hislen)
4558 i = 0;
4559 history[type][last_i] = history[type][i];
4560 last_i = i;
4561 }
4562 history[type][i].hisstr = str;
4563 history[type][i].hisnum = ++hisnum[type];
4564 return TRUE;
4565 }
4566 return FALSE;
4567}
4568
4569/*
4570 * Convert history name (from table above) to its HIST_ equivalent.
4571 * When "name" is empty, return "cmd" history.
4572 * Returns -1 for unknown history name.
4573 */
4574 int
4575get_histtype(name)
4576 char_u *name;
4577{
4578 int i;
4579 int len = (int)STRLEN(name);
4580
4581 /* No argument: use current history. */
4582 if (len == 0)
4583 return hist_char2type(ccline.cmdfirstc);
4584
4585 for (i = 0; history_names[i] != NULL; ++i)
4586 if (STRNICMP(name, history_names[i], len) == 0)
4587 return i;
4588
4589 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4590 return hist_char2type(name[0]);
4591
4592 return -1;
4593}
4594
4595static int last_maptick = -1; /* last seen maptick */
4596
4597/*
4598 * Add the given string to the given history. If the string is already in the
4599 * history then it is moved to the front. "histype" may be one of he HIST_
4600 * values.
4601 */
4602 void
4603add_to_history(histype, new_entry, in_map, sep)
4604 int histype;
4605 char_u *new_entry;
4606 int in_map; /* consider maptick when inside a mapping */
4607 int sep; /* separator character used (search hist) */
4608{
4609 histentry_T *hisptr;
4610 int len;
4611
4612 if (hislen == 0) /* no history */
4613 return;
4614
4615 /*
4616 * Searches inside the same mapping overwrite each other, so that only
4617 * the last line is kept. Be careful not to remove a line that was moved
4618 * down, only lines that were added.
4619 */
4620 if (histype == HIST_SEARCH && in_map)
4621 {
4622 if (maptick == last_maptick)
4623 {
4624 /* Current line is from the same mapping, remove it */
4625 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4626 vim_free(hisptr->hisstr);
4627 hisptr->hisstr = NULL;
4628 hisptr->hisnum = 0;
4629 --hisnum[histype];
4630 if (--hisidx[HIST_SEARCH] < 0)
4631 hisidx[HIST_SEARCH] = hislen - 1;
4632 }
4633 last_maptick = -1;
4634 }
4635 if (!in_history(histype, new_entry, TRUE))
4636 {
4637 if (++hisidx[histype] == hislen)
4638 hisidx[histype] = 0;
4639 hisptr = &history[histype][hisidx[histype]];
4640 vim_free(hisptr->hisstr);
4641
4642 /* Store the separator after the NUL of the string. */
4643 len = STRLEN(new_entry);
4644 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4645 if (hisptr->hisstr != NULL)
4646 hisptr->hisstr[len + 1] = sep;
4647
4648 hisptr->hisnum = ++hisnum[histype];
4649 if (histype == HIST_SEARCH && in_map)
4650 last_maptick = maptick;
4651 }
4652}
4653
4654#if defined(FEAT_EVAL) || defined(PROTO)
4655
4656/*
4657 * Get identifier of newest history entry.
4658 * "histype" may be one of the HIST_ values.
4659 */
4660 int
4661get_history_idx(histype)
4662 int histype;
4663{
4664 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4665 || hisidx[histype] < 0)
4666 return -1;
4667
4668 return history[histype][hisidx[histype]].hisnum;
4669}
4670
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004671static struct cmdline_info *get_ccline_ptr __ARGS((void));
4672
4673/*
4674 * Get pointer to the command line info to use. cmdline_paste() may clear
4675 * ccline and put the previous value in prev_ccline.
4676 */
4677 static struct cmdline_info *
4678get_ccline_ptr()
4679{
4680 if ((State & CMDLINE) == 0)
4681 return NULL;
4682 if (ccline.cmdbuff != NULL)
4683 return &ccline;
4684 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4685 return &prev_ccline;
4686 return NULL;
4687}
4688
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689/*
4690 * Get the current command line in allocated memory.
4691 * Only works when the command line is being edited.
4692 * Returns NULL when something is wrong.
4693 */
4694 char_u *
4695get_cmdline_str()
4696{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004697 struct cmdline_info *p = get_ccline_ptr();
4698
4699 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004701 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702}
4703
4704/*
4705 * Get the current command line position, counted in bytes.
4706 * Zero is the first position.
4707 * Only works when the command line is being edited.
4708 * Returns -1 when something is wrong.
4709 */
4710 int
4711get_cmdline_pos()
4712{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004713 struct cmdline_info *p = get_ccline_ptr();
4714
4715 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004717 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718}
4719
4720/*
4721 * Set the command line byte position to "pos". Zero is the first position.
4722 * Only works when the command line is being edited.
4723 * Returns 1 when failed, 0 when OK.
4724 */
4725 int
4726set_cmdline_pos(pos)
4727 int pos;
4728{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004729 struct cmdline_info *p = get_ccline_ptr();
4730
4731 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return 1;
4733
4734 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4735 * changed the command line. */
4736 if (pos < 0)
4737 new_cmdpos = 0;
4738 else
4739 new_cmdpos = pos;
4740 return 0;
4741}
4742
4743/*
4744 * Calculate history index from a number:
4745 * num > 0: seen as identifying number of a history entry
4746 * num < 0: relative position in history wrt newest entry
4747 * "histype" may be one of the HIST_ values.
4748 */
4749 static int
4750calc_hist_idx(histype, num)
4751 int histype;
4752 int num;
4753{
4754 int i;
4755 histentry_T *hist;
4756 int wrapped = FALSE;
4757
4758 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4759 || (i = hisidx[histype]) < 0 || num == 0)
4760 return -1;
4761
4762 hist = history[histype];
4763 if (num > 0)
4764 {
4765 while (hist[i].hisnum > num)
4766 if (--i < 0)
4767 {
4768 if (wrapped)
4769 break;
4770 i += hislen;
4771 wrapped = TRUE;
4772 }
4773 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
4774 return i;
4775 }
4776 else if (-num <= hislen)
4777 {
4778 i += num + 1;
4779 if (i < 0)
4780 i += hislen;
4781 if (hist[i].hisstr != NULL)
4782 return i;
4783 }
4784 return -1;
4785}
4786
4787/*
4788 * Get a history entry by its index.
4789 * "histype" may be one of the HIST_ values.
4790 */
4791 char_u *
4792get_history_entry(histype, idx)
4793 int histype;
4794 int idx;
4795{
4796 idx = calc_hist_idx(histype, idx);
4797 if (idx >= 0)
4798 return history[histype][idx].hisstr;
4799 else
4800 return (char_u *)"";
4801}
4802
4803/*
4804 * Clear all entries of a history.
4805 * "histype" may be one of the HIST_ values.
4806 */
4807 int
4808clr_history(histype)
4809 int histype;
4810{
4811 int i;
4812 histentry_T *hisptr;
4813
4814 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
4815 {
4816 hisptr = history[histype];
4817 for (i = hislen; i--;)
4818 {
4819 vim_free(hisptr->hisstr);
4820 hisptr->hisnum = 0;
4821 hisptr++->hisstr = NULL;
4822 }
4823 hisidx[histype] = -1; /* mark history as cleared */
4824 hisnum[histype] = 0; /* reset identifier counter */
4825 return OK;
4826 }
4827 return FAIL;
4828}
4829
4830/*
4831 * Remove all entries matching {str} from a history.
4832 * "histype" may be one of the HIST_ values.
4833 */
4834 int
4835del_history_entry(histype, str)
4836 int histype;
4837 char_u *str;
4838{
4839 regmatch_T regmatch;
4840 histentry_T *hisptr;
4841 int idx;
4842 int i;
4843 int last;
4844 int found = FALSE;
4845
4846 regmatch.regprog = NULL;
4847 regmatch.rm_ic = FALSE; /* always match case */
4848 if (hislen != 0
4849 && histype >= 0
4850 && histype < HIST_COUNT
4851 && *str != NUL
4852 && (idx = hisidx[histype]) >= 0
4853 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
4854 != NULL)
4855 {
4856 i = last = idx;
4857 do
4858 {
4859 hisptr = &history[histype][i];
4860 if (hisptr->hisstr == NULL)
4861 break;
4862 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
4863 {
4864 found = TRUE;
4865 vim_free(hisptr->hisstr);
4866 hisptr->hisstr = NULL;
4867 hisptr->hisnum = 0;
4868 }
4869 else
4870 {
4871 if (i != last)
4872 {
4873 history[histype][last] = *hisptr;
4874 hisptr->hisstr = NULL;
4875 hisptr->hisnum = 0;
4876 }
4877 if (--last < 0)
4878 last += hislen;
4879 }
4880 if (--i < 0)
4881 i += hislen;
4882 } while (i != idx);
4883 if (history[histype][idx].hisstr == NULL)
4884 hisidx[histype] = -1;
4885 }
4886 vim_free(regmatch.regprog);
4887 return found;
4888}
4889
4890/*
4891 * Remove an indexed entry from a history.
4892 * "histype" may be one of the HIST_ values.
4893 */
4894 int
4895del_history_idx(histype, idx)
4896 int histype;
4897 int idx;
4898{
4899 int i, j;
4900
4901 i = calc_hist_idx(histype, idx);
4902 if (i < 0)
4903 return FALSE;
4904 idx = hisidx[histype];
4905 vim_free(history[histype][i].hisstr);
4906
4907 /* When deleting the last added search string in a mapping, reset
4908 * last_maptick, so that the last added search string isn't deleted again.
4909 */
4910 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
4911 last_maptick = -1;
4912
4913 while (i != idx)
4914 {
4915 j = (i + 1) % hislen;
4916 history[histype][i] = history[histype][j];
4917 i = j;
4918 }
4919 history[histype][i].hisstr = NULL;
4920 history[histype][i].hisnum = 0;
4921 if (--i < 0)
4922 i += hislen;
4923 hisidx[histype] = i;
4924 return TRUE;
4925}
4926
4927#endif /* FEAT_EVAL */
4928
4929#if defined(FEAT_CRYPT) || defined(PROTO)
4930/*
4931 * Very specific function to remove the value in ":set key=val" from the
4932 * history.
4933 */
4934 void
4935remove_key_from_history()
4936{
4937 char_u *p;
4938 int i;
4939
4940 i = hisidx[HIST_CMD];
4941 if (i < 0)
4942 return;
4943 p = history[HIST_CMD][i].hisstr;
4944 if (p != NULL)
4945 for ( ; *p; ++p)
4946 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
4947 {
4948 p = vim_strchr(p + 3, '=');
4949 if (p == NULL)
4950 break;
4951 ++p;
4952 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
4953 if (p[i] == '\\' && p[i + 1])
4954 ++i;
4955 mch_memmove(p, p + i, STRLEN(p + i) + 1);
4956 --p;
4957 }
4958}
4959#endif
4960
4961#endif /* FEAT_CMDHIST */
4962
4963#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
4964/*
4965 * Get indices "num1,num2" that specify a range within a list (not a range of
4966 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
4967 * Returns OK if parsed successfully, otherwise FAIL.
4968 */
4969 int
4970get_list_range(str, num1, num2)
4971 char_u **str;
4972 int *num1;
4973 int *num2;
4974{
4975 int len;
4976 int first = FALSE;
4977 long num;
4978
4979 *str = skipwhite(*str);
4980 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
4981 {
4982 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
4983 *str += len;
4984 *num1 = (int)num;
4985 first = TRUE;
4986 }
4987 *str = skipwhite(*str);
4988 if (**str == ',') /* parse "to" part of range */
4989 {
4990 *str = skipwhite(*str + 1);
4991 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
4992 if (len > 0)
4993 {
4994 *num2 = (int)num;
4995 *str = skipwhite(*str + len);
4996 }
4997 else if (!first) /* no number given at all */
4998 return FAIL;
4999 }
5000 else if (first) /* only one number given */
5001 *num2 = *num1;
5002 return OK;
5003}
5004#endif
5005
5006#if defined(FEAT_CMDHIST) || defined(PROTO)
5007/*
5008 * :history command - print a history
5009 */
5010 void
5011ex_history(eap)
5012 exarg_T *eap;
5013{
5014 histentry_T *hist;
5015 int histype1 = HIST_CMD;
5016 int histype2 = HIST_CMD;
5017 int hisidx1 = 1;
5018 int hisidx2 = -1;
5019 int idx;
5020 int i, j, k;
5021 char_u *end;
5022 char_u *arg = eap->arg;
5023
5024 if (hislen == 0)
5025 {
5026 MSG(_("'history' option is zero"));
5027 return;
5028 }
5029
5030 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5031 {
5032 end = arg;
5033 while (ASCII_ISALPHA(*end)
5034 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5035 end++;
5036 i = *end;
5037 *end = NUL;
5038 histype1 = get_histtype(arg);
5039 if (histype1 == -1)
5040 {
5041 if (STRICMP(arg, "all") == 0)
5042 {
5043 histype1 = 0;
5044 histype2 = HIST_COUNT-1;
5045 }
5046 else
5047 {
5048 *end = i;
5049 EMSG(_(e_trailing));
5050 return;
5051 }
5052 }
5053 else
5054 histype2 = histype1;
5055 *end = i;
5056 }
5057 else
5058 end = arg;
5059 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5060 {
5061 EMSG(_(e_trailing));
5062 return;
5063 }
5064
5065 for (; !got_int && histype1 <= histype2; ++histype1)
5066 {
5067 STRCPY(IObuff, "\n # ");
5068 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5069 MSG_PUTS_TITLE(IObuff);
5070 idx = hisidx[histype1];
5071 hist = history[histype1];
5072 j = hisidx1;
5073 k = hisidx2;
5074 if (j < 0)
5075 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5076 if (k < 0)
5077 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5078 if (idx >= 0 && j <= k)
5079 for (i = idx + 1; !got_int; ++i)
5080 {
5081 if (i == hislen)
5082 i = 0;
5083 if (hist[i].hisstr != NULL
5084 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5085 {
5086 msg_putchar('\n');
5087 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5088 hist[i].hisnum);
5089 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5090 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5091 (int)Columns - 10);
5092 else
5093 STRCAT(IObuff, hist[i].hisstr);
5094 msg_outtrans(IObuff);
5095 out_flush();
5096 }
5097 if (i == idx)
5098 break;
5099 }
5100 }
5101}
5102#endif
5103
5104#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5105static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5106static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5107static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5108static int viminfo_add_at_front = FALSE;
5109
5110static int hist_type2char __ARGS((int type, int use_question));
5111
5112/*
5113 * Translate a history type number to the associated character.
5114 */
5115 static int
5116hist_type2char(type, use_question)
5117 int type;
5118 int use_question; /* use '?' instead of '/' */
5119{
5120 if (type == HIST_CMD)
5121 return ':';
5122 if (type == HIST_SEARCH)
5123 {
5124 if (use_question)
5125 return '?';
5126 else
5127 return '/';
5128 }
5129 if (type == HIST_EXPR)
5130 return '=';
5131 return '@';
5132}
5133
5134/*
5135 * Prepare for reading the history from the viminfo file.
5136 * This allocates history arrays to store the read history lines.
5137 */
5138 void
5139prepare_viminfo_history(asklen)
5140 int asklen;
5141{
5142 int i;
5143 int num;
5144 int type;
5145 int len;
5146
5147 init_history();
5148 viminfo_add_at_front = (asklen != 0);
5149 if (asklen > hislen)
5150 asklen = hislen;
5151
5152 for (type = 0; type < HIST_COUNT; ++type)
5153 {
5154 /*
5155 * Count the number of empty spaces in the history list. If there are
5156 * more spaces available than we request, then fill them up.
5157 */
5158 for (i = 0, num = 0; i < hislen; i++)
5159 if (history[type][i].hisstr == NULL)
5160 num++;
5161 len = asklen;
5162 if (num > len)
5163 len = num;
5164 if (len <= 0)
5165 viminfo_history[type] = NULL;
5166 else
5167 viminfo_history[type] =
5168 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5169 if (viminfo_history[type] == NULL)
5170 len = 0;
5171 viminfo_hislen[type] = len;
5172 viminfo_hisidx[type] = 0;
5173 }
5174}
5175
5176/*
5177 * Accept a line from the viminfo, store it in the history array when it's
5178 * new.
5179 */
5180 int
5181read_viminfo_history(virp)
5182 vir_T *virp;
5183{
5184 int type;
5185 long_u len;
5186 char_u *val;
5187 char_u *p;
5188
5189 type = hist_char2type(virp->vir_line[0]);
5190 if (viminfo_hisidx[type] < viminfo_hislen[type])
5191 {
5192 val = viminfo_readstring(virp, 1, TRUE);
5193 if (val != NULL && *val != NUL)
5194 {
5195 if (!in_history(type, val + (type == HIST_SEARCH),
5196 viminfo_add_at_front))
5197 {
5198 /* Need to re-allocate to append the separator byte. */
5199 len = STRLEN(val);
5200 p = lalloc(len + 2, TRUE);
5201 if (p != NULL)
5202 {
5203 if (type == HIST_SEARCH)
5204 {
5205 /* Search entry: Move the separator from the first
5206 * column to after the NUL. */
5207 mch_memmove(p, val + 1, (size_t)len);
5208 p[len] = (*val == ' ' ? NUL : *val);
5209 }
5210 else
5211 {
5212 /* Not a search entry: No separator in the viminfo
5213 * file, add a NUL separator. */
5214 mch_memmove(p, val, (size_t)len + 1);
5215 p[len + 1] = NUL;
5216 }
5217 viminfo_history[type][viminfo_hisidx[type]++] = p;
5218 }
5219 }
5220 }
5221 vim_free(val);
5222 }
5223 return viminfo_readline(virp);
5224}
5225
5226 void
5227finish_viminfo_history()
5228{
5229 int idx;
5230 int i;
5231 int type;
5232
5233 for (type = 0; type < HIST_COUNT; ++type)
5234 {
5235 if (history[type] == NULL)
5236 return;
5237 idx = hisidx[type] + viminfo_hisidx[type];
5238 if (idx >= hislen)
5239 idx -= hislen;
5240 else if (idx < 0)
5241 idx = hislen - 1;
5242 if (viminfo_add_at_front)
5243 hisidx[type] = idx;
5244 else
5245 {
5246 if (hisidx[type] == -1)
5247 hisidx[type] = hislen - 1;
5248 do
5249 {
5250 if (history[type][idx].hisstr != NULL)
5251 break;
5252 if (++idx == hislen)
5253 idx = 0;
5254 } while (idx != hisidx[type]);
5255 if (idx != hisidx[type] && --idx < 0)
5256 idx = hislen - 1;
5257 }
5258 for (i = 0; i < viminfo_hisidx[type]; i++)
5259 {
5260 vim_free(history[type][idx].hisstr);
5261 history[type][idx].hisstr = viminfo_history[type][i];
5262 if (--idx < 0)
5263 idx = hislen - 1;
5264 }
5265 idx += 1;
5266 idx %= hislen;
5267 for (i = 0; i < viminfo_hisidx[type]; i++)
5268 {
5269 history[type][idx++].hisnum = ++hisnum[type];
5270 idx %= hislen;
5271 }
5272 vim_free(viminfo_history[type]);
5273 viminfo_history[type] = NULL;
5274 }
5275}
5276
5277 void
5278write_viminfo_history(fp)
5279 FILE *fp;
5280{
5281 int i;
5282 int type;
5283 int num_saved;
5284 char_u *p;
5285 int c;
5286
5287 init_history();
5288 if (hislen == 0)
5289 return;
5290 for (type = 0; type < HIST_COUNT; ++type)
5291 {
5292 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5293 if (num_saved == 0)
5294 continue;
5295 if (num_saved < 0) /* Use default */
5296 num_saved = hislen;
5297 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5298 type == HIST_CMD ? _("Command Line") :
5299 type == HIST_SEARCH ? _("Search String") :
5300 type == HIST_EXPR ? _("Expression") :
5301 _("Input Line"));
5302 if (num_saved > hislen)
5303 num_saved = hislen;
5304 i = hisidx[type];
5305 if (i >= 0)
5306 while (num_saved--)
5307 {
5308 p = history[type][i].hisstr;
5309 if (p != NULL)
5310 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005311 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 /* For the search history: put the separator in the second
5313 * column; use a space if there isn't one. */
5314 if (type == HIST_SEARCH)
5315 {
5316 c = p[STRLEN(p) + 1];
5317 putc(c == NUL ? ' ' : c, fp);
5318 }
5319 viminfo_writestring(fp, p);
5320 }
5321 if (--i < 0)
5322 i = hislen - 1;
5323 }
5324 }
5325}
5326#endif /* FEAT_VIMINFO */
5327
5328#if defined(FEAT_FKMAP) || defined(PROTO)
5329/*
5330 * Write a character at the current cursor+offset position.
5331 * It is directly written into the command buffer block.
5332 */
5333 void
5334cmd_pchar(c, offset)
5335 int c, offset;
5336{
5337 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5338 {
5339 EMSG(_("E198: cmd_pchar beyond the command length"));
5340 return;
5341 }
5342 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5343 ccline.cmdbuff[ccline.cmdlen] = NUL;
5344}
5345
5346 int
5347cmd_gchar(offset)
5348 int offset;
5349{
5350 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5351 {
5352 /* EMSG(_("cmd_gchar beyond the command length")); */
5353 return NUL;
5354 }
5355 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5356}
5357#endif
5358
5359#if defined(FEAT_CMDWIN) || defined(PROTO)
5360/*
5361 * Open a window on the current command line and history. Allow editing in
5362 * the window. Returns when the window is closed.
5363 * Returns:
5364 * CR if the command is to be executed
5365 * Ctrl_C if it is to be abandoned
5366 * K_IGNORE if editing continues
5367 */
5368 static int
5369ex_window()
5370{
5371 struct cmdline_info save_ccline;
5372 buf_T *old_curbuf = curbuf;
5373 win_T *old_curwin = curwin;
5374 buf_T *bp;
5375 win_T *wp;
5376 int i;
5377 linenr_T lnum;
5378 int histtype;
5379 garray_T winsizes;
5380 char_u typestr[2];
5381 int save_restart_edit = restart_edit;
5382 int save_State = State;
5383 int save_exmode = exmode_active;
5384
5385 /* Can't do this recursively. Can't do it when typing a password. */
5386 if (cmdwin_type != 0
5387# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5388 || cmdline_star > 0
5389# endif
5390 )
5391 {
5392 beep_flush();
5393 return K_IGNORE;
5394 }
5395
5396 /* Save current window sizes. */
5397 win_size_save(&winsizes);
5398
5399# ifdef FEAT_AUTOCMD
5400 /* Don't execute autocommands while creating the window. */
5401 ++autocmd_block;
5402# endif
5403 /* Create a window for the command-line buffer. */
5404 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5405 {
5406 beep_flush();
5407 return K_IGNORE;
5408 }
5409 cmdwin_type = ccline.cmdfirstc;
5410 if (cmdwin_type == NUL)
5411 cmdwin_type = '-';
5412
5413 /* Create the command-line buffer empty. */
5414 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5415 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5416 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5417 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5418 curbuf->b_p_ma = TRUE;
5419# ifdef FEAT_RIGHTLEFT
5420 curwin->w_p_rl = FALSE;
5421# endif
5422# ifdef FEAT_SCROLLBIND
5423 curwin->w_p_scb = FALSE;
5424# endif
5425
5426# ifdef FEAT_AUTOCMD
5427 /* Do execute autocommands for setting the filetype (load syntax). */
5428 --autocmd_block;
5429# endif
5430
5431 histtype = hist_char2type(ccline.cmdfirstc);
5432 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5433 {
5434 if (p_wc == TAB)
5435 {
5436 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5437 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5438 }
5439 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5440 }
5441
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005442 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5443 * sets 'textwidth' to 78). */
5444 curbuf->b_p_tw = 0;
5445
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 /* Fill the buffer with the history. */
5447 init_history();
5448 if (hislen > 0)
5449 {
5450 i = hisidx[histtype];
5451 if (i >= 0)
5452 {
5453 lnum = 0;
5454 do
5455 {
5456 if (++i == hislen)
5457 i = 0;
5458 if (history[histtype][i].hisstr != NULL)
5459 ml_append(lnum++, history[histtype][i].hisstr,
5460 (colnr_T)0, FALSE);
5461 }
5462 while (i != hisidx[histtype]);
5463 }
5464 }
5465
5466 /* Replace the empty last line with the current command-line and put the
5467 * cursor there. */
5468 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5469 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5470 curwin->w_cursor.col = ccline.cmdpos;
5471 redraw_later(NOT_VALID);
5472
5473 /* Save the command line info, can be used recursively. */
5474 save_ccline = ccline;
5475 ccline.cmdbuff = NULL;
5476 ccline.cmdprompt = NULL;
5477
5478 /* No Ex mode here! */
5479 exmode_active = 0;
5480
5481 State = NORMAL;
5482# ifdef FEAT_MOUSE
5483 setmouse();
5484# endif
5485
5486# ifdef FEAT_AUTOCMD
5487 /* Trigger CmdwinEnter autocommands. */
5488 typestr[0] = cmdwin_type;
5489 typestr[1] = NUL;
5490 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5491# endif
5492
5493 i = RedrawingDisabled;
5494 RedrawingDisabled = 0;
5495
5496 /*
5497 * Call the main loop until <CR> or CTRL-C is typed.
5498 */
5499 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005500 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
5502 RedrawingDisabled = i;
5503
5504# ifdef FEAT_AUTOCMD
5505 /* Trigger CmdwinLeave autocommands. */
5506 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5507# endif
5508
5509 /* Restore the comand line info. */
5510 ccline = save_ccline;
5511 cmdwin_type = 0;
5512
5513 exmode_active = save_exmode;
5514
5515 /* Safety check: The old window or buffer was deleted: It's a a bug when
5516 * this happens! */
5517 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5518 {
5519 cmdwin_result = Ctrl_C;
5520 EMSG(_("E199: Active window or buffer deleted"));
5521 }
5522 else
5523 {
5524# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5525 /* autocmds may abort script processing */
5526 if (aborting() && cmdwin_result != K_IGNORE)
5527 cmdwin_result = Ctrl_C;
5528# endif
5529 /* Set the new command line from the cmdline buffer. */
5530 vim_free(ccline.cmdbuff);
5531 if (cmdwin_result == K_XF1) /* :qa! typed */
5532 {
5533 ccline.cmdbuff = vim_strsave((char_u *)"qa!");
5534 cmdwin_result = CAR;
5535 }
5536 else if (cmdwin_result == K_XF2) /* :qa typed */
5537 {
5538 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5539 cmdwin_result = CAR;
5540 }
5541 else
5542 ccline.cmdbuff = vim_strsave(ml_get_curline());
5543 if (ccline.cmdbuff == NULL)
5544 cmdwin_result = Ctrl_C;
5545 else
5546 {
5547 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5548 ccline.cmdbufflen = ccline.cmdlen + 1;
5549 ccline.cmdpos = curwin->w_cursor.col;
5550 if (ccline.cmdpos > ccline.cmdlen)
5551 ccline.cmdpos = ccline.cmdlen;
5552 if (cmdwin_result == K_IGNORE)
5553 {
5554 set_cmdspos_cursor();
5555 redrawcmd();
5556 }
5557 }
5558
5559# ifdef FEAT_AUTOCMD
5560 /* Don't execute autocommands while deleting the window. */
5561 ++autocmd_block;
5562# endif
5563 wp = curwin;
5564 bp = curbuf;
5565 win_goto(old_curwin);
5566 win_close(wp, TRUE);
5567 close_buffer(NULL, bp, DOBUF_WIPE);
5568
5569 /* Restore window sizes. */
5570 win_size_restore(&winsizes);
5571
5572# ifdef FEAT_AUTOCMD
5573 --autocmd_block;
5574# endif
5575 }
5576
5577 ga_clear(&winsizes);
5578 restart_edit = save_restart_edit;
5579
5580 State = save_State;
5581# ifdef FEAT_MOUSE
5582 setmouse();
5583# endif
5584
5585 return cmdwin_result;
5586}
5587#endif /* FEAT_CMDWIN */
5588
5589/*
5590 * Used for commands that either take a simple command string argument, or:
5591 * cmd << endmarker
5592 * {script}
5593 * endmarker
5594 * Returns a pointer to allocated memory with {script} or NULL.
5595 */
5596 char_u *
5597script_get(eap, cmd)
5598 exarg_T *eap;
5599 char_u *cmd;
5600{
5601 char_u *theline;
5602 char *end_pattern = NULL;
5603 char dot[] = ".";
5604 garray_T ga;
5605
5606 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5607 return NULL;
5608
5609 ga_init2(&ga, 1, 0x400);
5610
5611 if (cmd[2] != NUL)
5612 end_pattern = (char *)skipwhite(cmd + 2);
5613 else
5614 end_pattern = dot;
5615
5616 for (;;)
5617 {
5618 theline = eap->getline(
5619#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005620 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621#endif
5622 NUL, eap->cookie, 0);
5623
5624 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5625 break;
5626
5627 ga_concat(&ga, theline);
5628 ga_append(&ga, '\n');
5629 vim_free(theline);
5630 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005631 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
5633 return (char_u *)ga.ga_data;
5634}