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