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