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