blob: a3fac10ac73fb7674a9f24257ddff164827a54af [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
1703 || p_arshape
1704# 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)
1876 m = Columns * Rows;
1877 else
1878 m = MAXCOL;
1879 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1880 {
1881 c = cmdline_charsize(i);
1882#ifdef FEAT_MBYTE
1883 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1884 if (has_mbyte)
1885 correct_cmdspos(i, c);
1886#endif
1887 /* If the cmdline doesn't fit, put cursor on last visible char. */
1888 if ((ccline.cmdspos += c) >= m)
1889 {
1890 ccline.cmdpos = i - 1;
1891 ccline.cmdspos -= c;
1892 break;
1893 }
1894#ifdef FEAT_MBYTE
1895 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001896 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897#endif
1898 }
1899}
1900
1901#ifdef FEAT_MBYTE
1902/*
1903 * Check if the character at "idx", which is "cells" wide, is a multi-byte
1904 * character that doesn't fit, so that a ">" must be displayed.
1905 */
1906 static void
1907correct_cmdspos(idx, cells)
1908 int idx;
1909 int cells;
1910{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001911 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
1913 && ccline.cmdspos % Columns + cells > Columns)
1914 ccline.cmdspos++;
1915}
1916#endif
1917
1918/*
1919 * Get an Ex command line for the ":" command.
1920 */
1921/* ARGSUSED */
1922 char_u *
1923getexline(c, dummy, indent)
1924 int c; /* normally ':', NUL for ":append" */
1925 void *dummy; /* cookie not used */
1926 int indent; /* indent for inside conditionals */
1927{
1928 /* When executing a register, remove ':' that's in front of each line. */
1929 if (exec_from_reg && vpeekc() == ':')
1930 (void)vgetc();
1931 return getcmdline(c, 1L, indent);
1932}
1933
1934/*
1935 * Get an Ex command line for Ex mode.
1936 * In Ex mode we only use the OS supplied line editing features and no
1937 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001938 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 */
1940/* ARGSUSED */
1941 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001942getexmodeline(promptc, dummy, indent)
1943 int promptc; /* normally ':', NUL for ":append" and '?' for
1944 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 void *dummy; /* cookie not used */
1946 int indent; /* indent for inside conditionals */
1947{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001948 garray_T line_ga;
1949 char_u *pend;
1950 int startcol = 0;
1951 int c1;
1952 int escaped = FALSE; /* CTRL-V typed */
1953 int vcol = 0;
1954 char_u *p;
1955 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956
1957 /* Switch cursor on now. This avoids that it happens after the "\n", which
1958 * confuses the system function that computes tabstops. */
1959 cursor_on();
1960
1961 /* always start in column 0; write a newline if necessary */
1962 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001963 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001965 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001967 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001968 if (p_prompt)
1969 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 while (indent-- > 0)
1971 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 }
1974
1975 ga_init2(&line_ga, 1, 30);
1976
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001977 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001978 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001979 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001980 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001981 while (indent >= 8)
1982 {
1983 ga_append(&line_ga, TAB);
1984 msg_puts((char_u *)" ");
1985 indent -= 8;
1986 }
1987 while (indent-- > 0)
1988 {
1989 ga_append(&line_ga, ' ');
1990 msg_putchar(' ');
1991 }
1992 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001993 ++no_mapping;
1994 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 /*
1997 * Get the line, one character at a time.
1998 */
1999 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002000 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
2002 if (ga_grow(&line_ga, 40) == FAIL)
2003 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002004 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002006 /* Get one character at a time. Don't use inchar(), it can't handle
2007 * special characters. */
2008 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009
2010 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002011 * Handle line editing.
2012 * Previously this was left to the system, putting the terminal in
2013 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002015 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002017 msg_putchar('\n');
2018 break;
2019 }
2020
2021 if (!escaped)
2022 {
2023 /* CR typed means "enter", which is NL */
2024 if (c1 == '\r')
2025 c1 = '\n';
2026
2027 if (c1 == BS || c1 == K_BS
2028 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002030 if (line_ga.ga_len > 0)
2031 {
2032 --line_ga.ga_len;
2033 goto redraw;
2034 }
2035 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
2037
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002038 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002040 msg_col = startcol;
2041 msg_clr_eos();
2042 line_ga.ga_len = 0;
2043 continue;
2044 }
2045
2046 if (c1 == Ctrl_T)
2047 {
2048 p = (char_u *)line_ga.ga_data;
2049 p[line_ga.ga_len] = NUL;
2050 indent = get_indent_str(p, 8);
2051 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2052add_indent:
2053 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002055 char_u *s = skipwhite(p);
2056
2057 ga_grow(&line_ga, 1);
2058 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2059 *s = ' ';
2060 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002062redraw:
2063 /* redraw the line */
2064 msg_col = startcol;
2065 windgoto(msg_row, msg_col);
2066 vcol = 0;
2067 for (p = (char_u *)line_ga.ga_data;
2068 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002070 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002072 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002074 msg_putchar(' ');
2075 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002077 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002079 msg_outtrans_len(p, 1);
2080 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002083 msg_clr_eos();
2084 continue;
2085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002087 if (c1 == Ctrl_D)
2088 {
2089 /* Delete one shiftwidth. */
2090 p = (char_u *)line_ga.ga_data;
2091 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002093 if (prev_char == '^')
2094 ex_keep_indent = TRUE;
2095 indent = 0;
2096 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 }
2098 else
2099 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002100 p[line_ga.ga_len] = NUL;
2101 indent = get_indent_str(p, 8);
2102 --indent;
2103 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002105 while (get_indent_str(p, 8) > indent)
2106 {
2107 char_u *s = skipwhite(p);
2108
2109 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2110 --line_ga.ga_len;
2111 }
2112 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002114
2115 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2116 {
2117 escaped = TRUE;
2118 continue;
2119 }
2120
2121 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2122 if (IS_SPECIAL(c1))
2123 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002125
2126 if (IS_SPECIAL(c1))
2127 c1 = '?';
2128 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2129 prev_char = c1;
2130 if (c1 == '\n')
2131 msg_putchar('\n');
2132 else if (c1 == TAB)
2133 {
2134 /* Don't use chartabsize(), 'ts' can be different */
2135 do
2136 {
2137 msg_putchar(' ');
2138 } while (++vcol % 8);
2139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002142 msg_outtrans_len(
2143 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2144 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002146 ++line_ga.ga_len;
2147 escaped = FALSE;
2148
2149 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002150 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002151
2152 /* we are done when a NL is entered, but not when it comes after a
2153 * backslash */
2154 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2155 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002158 --pend;
2159 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002160 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
2162 }
2163
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002164 --no_mapping;
2165 --allow_keys;
2166
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 /* make following messages go to the next line */
2168 msg_didout = FALSE;
2169 msg_col = 0;
2170 if (msg_row < Rows - 1)
2171 ++msg_row;
2172 emsg_on_display = FALSE; /* don't want ui_delay() */
2173
2174 if (got_int)
2175 ga_clear(&line_ga);
2176
2177 return (char_u *)line_ga.ga_data;
2178}
2179
Bram Moolenaare344bea2005-09-01 20:46:49 +00002180# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2181 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182/*
2183 * Return TRUE if ccline.overstrike is on.
2184 */
2185 int
2186cmdline_overstrike()
2187{
2188 return ccline.overstrike;
2189}
2190
2191/*
2192 * Return TRUE if the cursor is at the end of the cmdline.
2193 */
2194 int
2195cmdline_at_end()
2196{
2197 return (ccline.cmdpos >= ccline.cmdlen);
2198}
2199#endif
2200
Bram Moolenaar81695252004-12-29 20:58:21 +00002201#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE))) \
2202 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203/*
2204 * Return the virtual column number at the current cursor position.
2205 * This is used by the IM code to obtain the start of the preedit string.
2206 */
2207 colnr_T
2208cmdline_getvcol_cursor()
2209{
2210 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2211 return MAXCOL;
2212
2213# ifdef FEAT_MBYTE
2214 if (has_mbyte)
2215 {
2216 colnr_T col;
2217 int i = 0;
2218
2219 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002220 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
2222 return col;
2223 }
2224 else
2225# endif
2226 return ccline.cmdpos;
2227}
2228#endif
2229
2230#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2231/*
2232 * If part of the command line is an IM preedit string, redraw it with
2233 * IM feedback attributes. The cursor position is restored after drawing.
2234 */
2235 static void
2236redrawcmd_preedit()
2237{
2238 if ((State & CMDLINE)
2239 && xic != NULL
2240 && im_get_status()
2241 && !p_imdisable
2242 && im_is_preediting())
2243 {
2244 int cmdpos = 0;
2245 int cmdspos;
2246 int old_row;
2247 int old_col;
2248 colnr_T col;
2249
2250 old_row = msg_row;
2251 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002252 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
2254# ifdef FEAT_MBYTE
2255 if (has_mbyte)
2256 {
2257 for (col = 0; col < preedit_start_col
2258 && cmdpos < ccline.cmdlen; ++col)
2259 {
2260 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002261 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
2263 }
2264 else
2265# endif
2266 {
2267 cmdspos += preedit_start_col;
2268 cmdpos += preedit_start_col;
2269 }
2270
2271 msg_row = cmdline_row + (cmdspos / (int)Columns);
2272 msg_col = cmdspos % (int)Columns;
2273 if (msg_row >= Rows)
2274 msg_row = Rows - 1;
2275
2276 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2277 {
2278 int char_len;
2279 int char_attr;
2280
2281 char_attr = im_get_feedback_attr(col);
2282 if (char_attr < 0)
2283 break; /* end of preedit string */
2284
2285# ifdef FEAT_MBYTE
2286 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002287 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 else
2289# endif
2290 char_len = 1;
2291
2292 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2293 cmdpos += char_len;
2294 }
2295
2296 msg_row = old_row;
2297 msg_col = old_col;
2298 }
2299}
2300#endif /* FEAT_XIM && FEAT_GUI_GTK */
2301
2302/*
2303 * Allocate a new command line buffer.
2304 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2305 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2306 */
2307 static void
2308alloc_cmdbuff(len)
2309 int len;
2310{
2311 /*
2312 * give some extra space to avoid having to allocate all the time
2313 */
2314 if (len < 80)
2315 len = 100;
2316 else
2317 len += 20;
2318
2319 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2320 ccline.cmdbufflen = len;
2321}
2322
2323/*
2324 * Re-allocate the command line to length len + something extra.
2325 * return FAIL for failure, OK otherwise
2326 */
2327 static int
2328realloc_cmdbuff(len)
2329 int len;
2330{
2331 char_u *p;
2332
2333 p = ccline.cmdbuff;
2334 alloc_cmdbuff(len); /* will get some more */
2335 if (ccline.cmdbuff == NULL) /* out of memory */
2336 {
2337 ccline.cmdbuff = p; /* keep the old one */
2338 return FAIL;
2339 }
2340 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2341 vim_free(p);
2342 return OK;
2343}
2344
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002345#if defined(FEAT_ARABIC) || defined(PROTO)
2346static char_u *arshape_buf = NULL;
2347
2348# if defined(EXITFREE) || defined(PROTO)
2349 void
2350free_cmdline_buf()
2351{
2352 vim_free(arshape_buf);
2353}
2354# endif
2355#endif
2356
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357/*
2358 * Draw part of the cmdline at the current cursor position. But draw stars
2359 * when cmdline_star is TRUE.
2360 */
2361 static void
2362draw_cmdline(start, len)
2363 int start;
2364 int len;
2365{
2366#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2367 int i;
2368
2369 if (cmdline_star > 0)
2370 for (i = 0; i < len; ++i)
2371 {
2372 msg_putchar('*');
2373# ifdef FEAT_MBYTE
2374 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002375 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376# endif
2377 }
2378 else
2379#endif
2380#ifdef FEAT_ARABIC
2381 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 static int buflen = 0;
2384 char_u *p;
2385 int j;
2386 int newlen = 0;
2387 int mb_l;
2388 int pc, pc1;
2389 int prev_c = 0;
2390 int prev_c1 = 0;
2391 int u8c, u8c_c1, u8c_c2;
2392 int nc = 0;
2393 int dummy;
2394
2395 /*
2396 * Do arabic shaping into a temporary buffer. This is very
2397 * inefficient!
2398 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002399 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 {
2401 /* Re-allocate the buffer. We keep it around to avoid a lot of
2402 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002403 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002404 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002405 arshape_buf = alloc(buflen);
2406 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 return; /* out of memory */
2408 }
2409
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002410 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2411 {
2412 /* Prepend a space to draw the leading composing char on. */
2413 arshape_buf[0] = ' ';
2414 newlen = 1;
2415 }
2416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 for (j = start; j < start + len; j += mb_l)
2418 {
2419 p = ccline.cmdbuff + j;
2420 u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002421 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 if (ARABIC_CHAR(u8c))
2423 {
2424 /* Do Arabic shaping. */
2425 if (cmdmsg_rl)
2426 {
2427 /* displaying from right to left */
2428 pc = prev_c;
2429 pc1 = prev_c1;
2430 prev_c1 = u8c_c1;
2431 if (j + mb_l >= start + len)
2432 nc = NUL;
2433 else
2434 nc = utf_ptr2char(p + mb_l);
2435 }
2436 else
2437 {
2438 /* displaying from left to right */
2439 if (j + mb_l >= start + len)
2440 pc = NUL;
2441 else
2442 pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
2443 start + len - j - mb_l);
2444 nc = prev_c;
2445 }
2446 prev_c = u8c;
2447
2448 u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
2449
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002450 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 if (u8c_c1 != 0)
2452 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002453 newlen += (*mb_char2bytes)(u8c_c1, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 if (u8c_c2 != 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002455 newlen += (*mb_char2bytes)(u8c_c2,
2456 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
2458 }
2459 else
2460 {
2461 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002462 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 newlen += mb_l;
2464 }
2465 }
2466
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002467 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 }
2469 else
2470#endif
2471 msg_outtrans_len(ccline.cmdbuff + start, len);
2472}
2473
2474/*
2475 * Put a character on the command line. Shifts the following text to the
2476 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2477 * "c" must be printable (fit in one display cell)!
2478 */
2479 void
2480putcmdline(c, shift)
2481 int c;
2482 int shift;
2483{
2484 if (cmd_silent)
2485 return;
2486 msg_no_more = TRUE;
2487 msg_putchar(c);
2488 if (shift)
2489 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2490 msg_no_more = FALSE;
2491 cursorcmd();
2492}
2493
2494/*
2495 * Undo a putcmdline(c, FALSE).
2496 */
2497 void
2498unputcmdline()
2499{
2500 if (cmd_silent)
2501 return;
2502 msg_no_more = TRUE;
2503 if (ccline.cmdlen == ccline.cmdpos)
2504 msg_putchar(' ');
2505 else
2506 draw_cmdline(ccline.cmdpos, 1);
2507 msg_no_more = FALSE;
2508 cursorcmd();
2509}
2510
2511/*
2512 * Put the given string, of the given length, onto the command line.
2513 * If len is -1, then STRLEN() is used to calculate the length.
2514 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2515 * part will be redrawn, otherwise it will not. If this function is called
2516 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2517 * called afterwards.
2518 */
2519 int
2520put_on_cmdline(str, len, redraw)
2521 char_u *str;
2522 int len;
2523 int redraw;
2524{
2525 int retval;
2526 int i;
2527 int m;
2528 int c;
2529
2530 if (len < 0)
2531 len = (int)STRLEN(str);
2532
2533 /* Check if ccline.cmdbuff needs to be longer */
2534 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2535 retval = realloc_cmdbuff(ccline.cmdlen + len);
2536 else
2537 retval = OK;
2538 if (retval == OK)
2539 {
2540 if (!ccline.overstrike)
2541 {
2542 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2543 ccline.cmdbuff + ccline.cmdpos,
2544 (size_t)(ccline.cmdlen - ccline.cmdpos));
2545 ccline.cmdlen += len;
2546 }
2547 else
2548 {
2549#ifdef FEAT_MBYTE
2550 if (has_mbyte)
2551 {
2552 /* Count nr of characters in the new string. */
2553 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002554 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 ++m;
2556 /* Count nr of bytes in cmdline that are overwritten by these
2557 * characters. */
2558 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002559 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 --m;
2561 if (i < ccline.cmdlen)
2562 {
2563 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2564 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2565 ccline.cmdlen += ccline.cmdpos + len - i;
2566 }
2567 else
2568 ccline.cmdlen = ccline.cmdpos + len;
2569 }
2570 else
2571#endif
2572 if (ccline.cmdpos + len > ccline.cmdlen)
2573 ccline.cmdlen = ccline.cmdpos + len;
2574 }
2575 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2576 ccline.cmdbuff[ccline.cmdlen] = NUL;
2577
2578#ifdef FEAT_MBYTE
2579 if (enc_utf8)
2580 {
2581 /* When the inserted text starts with a composing character,
2582 * backup to the character before it. There could be two of them.
2583 */
2584 i = 0;
2585 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2586 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2587 {
2588 i = (*mb_head_off)(ccline.cmdbuff,
2589 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2590 ccline.cmdpos -= i;
2591 len += i;
2592 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2593 }
2594# ifdef FEAT_ARABIC
2595 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2596 {
2597 /* Check the previous character for Arabic combining pair. */
2598 i = (*mb_head_off)(ccline.cmdbuff,
2599 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2600 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2601 + ccline.cmdpos - i), c))
2602 {
2603 ccline.cmdpos -= i;
2604 len += i;
2605 }
2606 else
2607 i = 0;
2608 }
2609# endif
2610 if (i != 0)
2611 {
2612 /* Also backup the cursor position. */
2613 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2614 ccline.cmdspos -= i;
2615 msg_col -= i;
2616 if (msg_col < 0)
2617 {
2618 msg_col += Columns;
2619 --msg_row;
2620 }
2621 }
2622 }
2623#endif
2624
2625 if (redraw && !cmd_silent)
2626 {
2627 msg_no_more = TRUE;
2628 i = cmdline_row;
2629 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2630 /* Avoid clearing the rest of the line too often. */
2631 if (cmdline_row != i || ccline.overstrike)
2632 msg_clr_eos();
2633 msg_no_more = FALSE;
2634 }
2635#ifdef FEAT_FKMAP
2636 /*
2637 * If we are in Farsi command mode, the character input must be in
2638 * Insert mode. So do not advance the cmdpos.
2639 */
2640 if (!cmd_fkmap)
2641#endif
2642 {
2643 if (KeyTyped)
2644 m = Columns * Rows;
2645 else
2646 m = MAXCOL;
2647 for (i = 0; i < len; ++i)
2648 {
2649 c = cmdline_charsize(ccline.cmdpos);
2650#ifdef FEAT_MBYTE
2651 /* count ">" for a double-wide char that doesn't fit. */
2652 if (has_mbyte)
2653 correct_cmdspos(ccline.cmdpos, c);
2654#endif
2655 /* Stop cursor at the end of the screen */
2656 if (ccline.cmdspos + c >= m)
2657 break;
2658 ccline.cmdspos += c;
2659#ifdef FEAT_MBYTE
2660 if (has_mbyte)
2661 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002662 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (c > len - i - 1)
2664 c = len - i - 1;
2665 ccline.cmdpos += c;
2666 i += c;
2667 }
2668#endif
2669 ++ccline.cmdpos;
2670 }
2671 }
2672 }
2673 if (redraw)
2674 msg_check();
2675 return retval;
2676}
2677
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002678static struct cmdline_info prev_ccline;
2679static int prev_ccline_used = FALSE;
2680
2681/*
2682 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2683 * and overwrite it. But get_cmdline_str() may need it, thus make it
2684 * available globally in prev_ccline.
2685 */
2686 static void
2687save_cmdline(ccp)
2688 struct cmdline_info *ccp;
2689{
2690 if (!prev_ccline_used)
2691 {
2692 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2693 prev_ccline_used = TRUE;
2694 }
2695 *ccp = prev_ccline;
2696 prev_ccline = ccline;
2697 ccline.cmdbuff = NULL;
2698 ccline.cmdprompt = NULL;
2699}
2700
2701/*
2702 * Resture ccline after it has been saved with save_cmdline().
2703 */
2704 static void
2705restore_cmdline(ccp)
2706 struct cmdline_info *ccp;
2707{
2708 ccline = prev_ccline;
2709 prev_ccline = *ccp;
2710}
2711
Bram Moolenaar8299df92004-07-10 09:47:34 +00002712/*
2713 * paste a yank register into the command line.
2714 * used by CTRL-R command in command-line mode
2715 * insert_reg() can't be used here, because special characters from the
2716 * register contents will be interpreted as commands.
2717 *
2718 * return FAIL for failure, OK otherwise
2719 */
2720 static int
2721cmdline_paste(regname, literally)
2722 int regname;
2723 int literally; /* Insert text literally instead of "as typed" */
2724{
2725 long i;
2726 char_u *arg;
2727 int allocated;
2728 struct cmdline_info save_ccline;
2729
2730 /* check for valid regname; also accept special characters for CTRL-R in
2731 * the command line */
2732 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2733 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2734 return FAIL;
2735
2736 /* A register containing CTRL-R can cause an endless loop. Allow using
2737 * CTRL-C to break the loop. */
2738 line_breakcheck();
2739 if (got_int)
2740 return FAIL;
2741
2742#ifdef FEAT_CLIPBOARD
2743 regname = may_get_selection(regname);
2744#endif
2745
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002746 /* Need to save and restore ccline. */
2747 save_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002748 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002749 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002750
2751 if (i)
2752 {
2753 /* Got the value of a special register in "arg". */
2754 if (arg == NULL)
2755 return FAIL;
2756 cmdline_paste_str(arg, literally);
2757 if (allocated)
2758 vim_free(arg);
2759 return OK;
2760 }
2761
2762 return cmdline_paste_reg(regname, literally);
2763}
2764
2765/*
2766 * Put a string on the command line.
2767 * When "literally" is TRUE, insert literally.
2768 * When "literally" is FALSE, insert as typed, but don't leave the command
2769 * line.
2770 */
2771 void
2772cmdline_paste_str(s, literally)
2773 char_u *s;
2774 int literally;
2775{
2776 int c, cv;
2777
2778 if (literally)
2779 put_on_cmdline(s, -1, TRUE);
2780 else
2781 while (*s != NUL)
2782 {
2783 cv = *s;
2784 if (cv == Ctrl_V && s[1])
2785 ++s;
2786#ifdef FEAT_MBYTE
2787 if (has_mbyte)
2788 {
2789 c = mb_ptr2char(s);
2790 s += mb_char2len(c);
2791 }
2792 else
2793#endif
2794 c = *s++;
2795 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2796#ifdef UNIX
2797 || c == intr_char
2798#endif
2799 || (c == Ctrl_BSL && *s == Ctrl_N))
2800 stuffcharReadbuff(Ctrl_V);
2801 stuffcharReadbuff(c);
2802 }
2803}
2804
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805#ifdef FEAT_WILDMENU
2806/*
2807 * Delete characters on the command line, from "from" to the current
2808 * position.
2809 */
2810 static void
2811cmdline_del(from)
2812 int from;
2813{
2814 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2815 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2816 ccline.cmdlen -= ccline.cmdpos - from;
2817 ccline.cmdpos = from;
2818}
2819#endif
2820
2821/*
2822 * this fuction is called when the screen size changes and with incremental
2823 * search
2824 */
2825 void
2826redrawcmdline()
2827{
2828 if (cmd_silent)
2829 return;
2830 need_wait_return = FALSE;
2831 compute_cmdrow();
2832 redrawcmd();
2833 cursorcmd();
2834}
2835
2836 static void
2837redrawcmdprompt()
2838{
2839 int i;
2840
2841 if (cmd_silent)
2842 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002843 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 msg_putchar(ccline.cmdfirstc);
2845 if (ccline.cmdprompt != NULL)
2846 {
2847 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2848 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2849 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002850 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 --ccline.cmdindent;
2852 }
2853 else
2854 for (i = ccline.cmdindent; i > 0; --i)
2855 msg_putchar(' ');
2856}
2857
2858/*
2859 * Redraw what is currently on the command line.
2860 */
2861 void
2862redrawcmd()
2863{
2864 if (cmd_silent)
2865 return;
2866
2867 msg_start();
2868 redrawcmdprompt();
2869
2870 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2871 msg_no_more = TRUE;
2872 draw_cmdline(0, ccline.cmdlen);
2873 msg_clr_eos();
2874 msg_no_more = FALSE;
2875
2876 set_cmdspos_cursor();
2877
2878 /*
2879 * An emsg() before may have set msg_scroll. This is used in normal mode,
2880 * in cmdline mode we can reset them now.
2881 */
2882 msg_scroll = FALSE; /* next message overwrites cmdline */
2883
2884 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2885 * in cmdline mode */
2886 skip_redraw = FALSE;
2887}
2888
2889 void
2890compute_cmdrow()
2891{
2892 if (exmode_active || msg_scrolled)
2893 cmdline_row = Rows - 1;
2894 else
2895 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2896 + W_STATUS_HEIGHT(lastwin);
2897}
2898
2899 static void
2900cursorcmd()
2901{
2902 if (cmd_silent)
2903 return;
2904
2905#ifdef FEAT_RIGHTLEFT
2906 if (cmdmsg_rl)
2907 {
2908 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2909 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2910 if (msg_row <= 0)
2911 msg_row = Rows - 1;
2912 }
2913 else
2914#endif
2915 {
2916 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
2917 msg_col = ccline.cmdspos % (int)Columns;
2918 if (msg_row >= Rows)
2919 msg_row = Rows - 1;
2920 }
2921
2922 windgoto(msg_row, msg_col);
2923#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2924 redrawcmd_preedit();
2925#endif
2926#ifdef MCH_CURSOR_SHAPE
2927 mch_update_cursor();
2928#endif
2929}
2930
2931 void
2932gotocmdline(clr)
2933 int clr;
2934{
2935 msg_start();
2936#ifdef FEAT_RIGHTLEFT
2937 if (cmdmsg_rl)
2938 msg_col = Columns - 1;
2939 else
2940#endif
2941 msg_col = 0; /* always start in column 0 */
2942 if (clr) /* clear the bottom line(s) */
2943 msg_clr_eos(); /* will reset clear_cmdline */
2944 windgoto(cmdline_row, 0);
2945}
2946
2947/*
2948 * Check the word in front of the cursor for an abbreviation.
2949 * Called when the non-id character "c" has been entered.
2950 * When an abbreviation is recognized it is removed from the text with
2951 * backspaces and the replacement string is inserted, followed by "c".
2952 */
2953 static int
2954ccheck_abbr(c)
2955 int c;
2956{
2957 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
2958 return FALSE;
2959
2960 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
2961}
2962
2963/*
2964 * Return FAIL if this is not an appropriate context in which to do
2965 * completion of anything, return OK if it is (even if there are no matches).
2966 * For the caller, this means that the character is just passed through like a
2967 * normal character (instead of being expanded). This allows :s/^I^D etc.
2968 */
2969 static int
2970nextwild(xp, type, options)
2971 expand_T *xp;
2972 int type;
2973 int options; /* extra options for ExpandOne() */
2974{
2975 int i, j;
2976 char_u *p1;
2977 char_u *p2;
2978 int oldlen;
2979 int difflen;
2980 int v;
2981
2982 if (xp->xp_numfiles == -1)
2983 {
2984 set_expand_context(xp);
2985 cmd_showtail = expand_showtail(xp);
2986 }
2987
2988 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
2989 {
2990 beep_flush();
2991 return OK; /* Something illegal on command line */
2992 }
2993 if (xp->xp_context == EXPAND_NOTHING)
2994 {
2995 /* Caller can use the character as a normal char instead */
2996 return FAIL;
2997 }
2998
2999 MSG_PUTS("..."); /* show that we are busy */
3000 out_flush();
3001
3002 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3003 oldlen = ccline.cmdpos - i;
3004
3005 if (type == WILD_NEXT || type == WILD_PREV)
3006 {
3007 /*
3008 * Get next/previous match for a previous expanded pattern.
3009 */
3010 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3011 }
3012 else
3013 {
3014 /*
3015 * Translate string into pattern and expand it.
3016 */
3017 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3018 p2 = NULL;
3019 else
3020 {
3021 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3022 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3023 |options, type);
3024 vim_free(p1);
3025 /* longest match: make sure it is not shorter (happens with :help */
3026 if (p2 != NULL && type == WILD_LONGEST)
3027 {
3028 for (j = 0; j < oldlen; ++j)
3029 if (ccline.cmdbuff[i + j] == '*'
3030 || ccline.cmdbuff[i + j] == '?')
3031 break;
3032 if ((int)STRLEN(p2) < j)
3033 {
3034 vim_free(p2);
3035 p2 = NULL;
3036 }
3037 }
3038 }
3039 }
3040
3041 if (p2 != NULL && !got_int)
3042 {
3043 difflen = (int)STRLEN(p2) - oldlen;
3044 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3045 {
3046 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3047 xp->xp_pattern = ccline.cmdbuff + i;
3048 }
3049 else
3050 v = OK;
3051 if (v == OK)
3052 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003053 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3054 &ccline.cmdbuff[ccline.cmdpos],
3055 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3056 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 ccline.cmdlen += difflen;
3058 ccline.cmdpos += difflen;
3059 }
3060 }
3061 vim_free(p2);
3062
3063 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003064 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
3066 /* When expanding a ":map" command and no matches are found, assume that
3067 * the key is supposed to be inserted literally */
3068 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3069 return FAIL;
3070
3071 if (xp->xp_numfiles <= 0 && p2 == NULL)
3072 beep_flush();
3073 else if (xp->xp_numfiles == 1)
3074 /* free expanded pattern */
3075 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3076
3077 return OK;
3078}
3079
3080/*
3081 * Do wildcard expansion on the string 'str'.
3082 * Chars that should not be expanded must be preceded with a backslash.
3083 * Return a pointer to alloced memory containing the new string.
3084 * Return NULL for failure.
3085 *
3086 * Results are cached in xp->xp_files and xp->xp_numfiles.
3087 *
3088 * mode = WILD_FREE: just free previously expanded matches
3089 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3090 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3091 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3092 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3093 * mode = WILD_ALL: return all matches concatenated
3094 * mode = WILD_LONGEST: return longest matched part
3095 *
3096 * options = WILD_LIST_NOTFOUND: list entries without a match
3097 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3098 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3099 * options = WILD_NO_BEEP: Don't beep for multiple matches
3100 * options = WILD_ADD_SLASH: add a slash after directory names
3101 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3102 * options = WILD_SILENT: don't print warning messages
3103 * options = WILD_ESCAPE: put backslash before special chars
3104 *
3105 * The variables xp->xp_context and xp->xp_backslash must have been set!
3106 */
3107 char_u *
3108ExpandOne(xp, str, orig, options, mode)
3109 expand_T *xp;
3110 char_u *str;
3111 char_u *orig; /* allocated copy of original of expanded string */
3112 int options;
3113 int mode;
3114{
3115 char_u *ss = NULL;
3116 static int findex;
3117 static char_u *orig_save = NULL; /* kept value of orig */
3118 int i;
3119 long_u len;
3120 int non_suf_match; /* number without matching suffix */
3121
3122 /*
3123 * first handle the case of using an old match
3124 */
3125 if (mode == WILD_NEXT || mode == WILD_PREV)
3126 {
3127 if (xp->xp_numfiles > 0)
3128 {
3129 if (mode == WILD_PREV)
3130 {
3131 if (findex == -1)
3132 findex = xp->xp_numfiles;
3133 --findex;
3134 }
3135 else /* mode == WILD_NEXT */
3136 ++findex;
3137
3138 /*
3139 * When wrapping around, return the original string, set findex to
3140 * -1.
3141 */
3142 if (findex < 0)
3143 {
3144 if (orig_save == NULL)
3145 findex = xp->xp_numfiles - 1;
3146 else
3147 findex = -1;
3148 }
3149 if (findex >= xp->xp_numfiles)
3150 {
3151 if (orig_save == NULL)
3152 findex = 0;
3153 else
3154 findex = -1;
3155 }
3156#ifdef FEAT_WILDMENU
3157 if (p_wmnu)
3158 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3159 findex, cmd_showtail);
3160#endif
3161 if (findex == -1)
3162 return vim_strsave(orig_save);
3163 return vim_strsave(xp->xp_files[findex]);
3164 }
3165 else
3166 return NULL;
3167 }
3168
3169/* free old names */
3170 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3171 {
3172 FreeWild(xp->xp_numfiles, xp->xp_files);
3173 xp->xp_numfiles = -1;
3174 vim_free(orig_save);
3175 orig_save = NULL;
3176 }
3177 findex = 0;
3178
3179 if (mode == WILD_FREE) /* only release file name */
3180 return NULL;
3181
3182 if (xp->xp_numfiles == -1)
3183 {
3184 vim_free(orig_save);
3185 orig_save = orig;
3186
3187 /*
3188 * Do the expansion.
3189 */
3190 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3191 options) == FAIL)
3192 {
3193#ifdef FNAME_ILLEGAL
3194 /* Illegal file name has been silently skipped. But when there
3195 * are wildcards, the real problem is that there was no match,
3196 * causing the pattern to be added, which has illegal characters.
3197 */
3198 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3199 EMSG2(_(e_nomatch2), str);
3200#endif
3201 }
3202 else if (xp->xp_numfiles == 0)
3203 {
3204 if (!(options & WILD_SILENT))
3205 EMSG2(_(e_nomatch2), str);
3206 }
3207 else
3208 {
3209 /* Escape the matches for use on the command line. */
3210 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3211
3212 /*
3213 * Check for matching suffixes in file names.
3214 */
3215 if (mode != WILD_ALL && mode != WILD_LONGEST)
3216 {
3217 if (xp->xp_numfiles)
3218 non_suf_match = xp->xp_numfiles;
3219 else
3220 non_suf_match = 1;
3221 if ((xp->xp_context == EXPAND_FILES
3222 || xp->xp_context == EXPAND_DIRECTORIES)
3223 && xp->xp_numfiles > 1)
3224 {
3225 /*
3226 * More than one match; check suffix.
3227 * The files will have been sorted on matching suffix in
3228 * expand_wildcards, only need to check the first two.
3229 */
3230 non_suf_match = 0;
3231 for (i = 0; i < 2; ++i)
3232 if (match_suffix(xp->xp_files[i]))
3233 ++non_suf_match;
3234 }
3235 if (non_suf_match != 1)
3236 {
3237 /* Can we ever get here unless it's while expanding
3238 * interactively? If not, we can get rid of this all
3239 * together. Don't really want to wait for this message
3240 * (and possibly have to hit return to continue!).
3241 */
3242 if (!(options & WILD_SILENT))
3243 EMSG(_(e_toomany));
3244 else if (!(options & WILD_NO_BEEP))
3245 beep_flush();
3246 }
3247 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3248 ss = vim_strsave(xp->xp_files[0]);
3249 }
3250 }
3251 }
3252
3253 /* Find longest common part */
3254 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3255 {
3256 for (len = 0; xp->xp_files[0][len]; ++len)
3257 {
3258 for (i = 0; i < xp->xp_numfiles; ++i)
3259 {
3260#ifdef CASE_INSENSITIVE_FILENAME
3261 if (xp->xp_context == EXPAND_DIRECTORIES
3262 || xp->xp_context == EXPAND_FILES
3263 || xp->xp_context == EXPAND_BUFFERS)
3264 {
3265 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3266 TOLOWER_LOC(xp->xp_files[0][len]))
3267 break;
3268 }
3269 else
3270#endif
3271 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3272 break;
3273 }
3274 if (i < xp->xp_numfiles)
3275 {
3276 if (!(options & WILD_NO_BEEP))
3277 vim_beep();
3278 break;
3279 }
3280 }
3281 ss = alloc((unsigned)len + 1);
3282 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003283 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 findex = -1; /* next p_wc gets first one */
3285 }
3286
3287 /* Concatenate all matching names */
3288 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3289 {
3290 len = 0;
3291 for (i = 0; i < xp->xp_numfiles; ++i)
3292 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3293 ss = lalloc(len, TRUE);
3294 if (ss != NULL)
3295 {
3296 *ss = NUL;
3297 for (i = 0; i < xp->xp_numfiles; ++i)
3298 {
3299 STRCAT(ss, xp->xp_files[i]);
3300 if (i != xp->xp_numfiles - 1)
3301 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3302 }
3303 }
3304 }
3305
3306 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3307 ExpandCleanup(xp);
3308
3309 return ss;
3310}
3311
3312/*
3313 * Prepare an expand structure for use.
3314 */
3315 void
3316ExpandInit(xp)
3317 expand_T *xp;
3318{
3319 xp->xp_backslash = XP_BS_NONE;
3320 xp->xp_numfiles = -1;
3321 xp->xp_files = NULL;
3322}
3323
3324/*
3325 * Cleanup an expand structure after use.
3326 */
3327 void
3328ExpandCleanup(xp)
3329 expand_T *xp;
3330{
3331 if (xp->xp_numfiles >= 0)
3332 {
3333 FreeWild(xp->xp_numfiles, xp->xp_files);
3334 xp->xp_numfiles = -1;
3335 }
3336}
3337
3338 void
3339ExpandEscape(xp, str, numfiles, files, options)
3340 expand_T *xp;
3341 char_u *str;
3342 int numfiles;
3343 char_u **files;
3344 int options;
3345{
3346 int i;
3347 char_u *p;
3348
3349 /*
3350 * May change home directory back to "~"
3351 */
3352 if (options & WILD_HOME_REPLACE)
3353 tilde_replace(str, numfiles, files);
3354
3355 if (options & WILD_ESCAPE)
3356 {
3357 if (xp->xp_context == EXPAND_FILES
3358 || xp->xp_context == EXPAND_BUFFERS
3359 || xp->xp_context == EXPAND_DIRECTORIES)
3360 {
3361 /*
3362 * Insert a backslash into a file name before a space, \, %, #
3363 * and wildmatch characters, except '~'.
3364 */
3365 for (i = 0; i < numfiles; ++i)
3366 {
3367 /* for ":set path=" we need to escape spaces twice */
3368 if (xp->xp_backslash == XP_BS_THREE)
3369 {
3370 p = vim_strsave_escaped(files[i], (char_u *)" ");
3371 if (p != NULL)
3372 {
3373 vim_free(files[i]);
3374 files[i] = p;
3375#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3376 p = vim_strsave_escaped(files[i], (char_u *)" ");
3377 if (p != NULL)
3378 {
3379 vim_free(files[i]);
3380 files[i] = p;
3381 }
3382#endif
3383 }
3384 }
3385#ifdef BACKSLASH_IN_FILENAME
3386 {
3387 char_u buf[20];
3388 int j = 0;
3389
3390 /* Don't escape '[' and '{' if they are in 'isfname'. */
3391 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3392 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3393 buf[j++] = *p;
3394 buf[j] = NUL;
3395 p = vim_strsave_escaped(files[i], buf);
3396 }
3397#else
3398 p = vim_strsave_escaped(files[i], PATH_ESC_CHARS);
3399#endif
3400 if (p != NULL)
3401 {
3402 vim_free(files[i]);
3403 files[i] = p;
3404 }
3405
3406 /* If 'str' starts with "\~", replace "~" at start of
3407 * files[i] with "\~". */
3408 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003409 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 }
3411 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003412
3413 /* If the first file starts with a '+' escape it. Otherwise it
3414 * could be seen as "+cmd". */
3415 if (*files[0] == '+')
3416 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 }
3418 else if (xp->xp_context == EXPAND_TAGS)
3419 {
3420 /*
3421 * Insert a backslash before characters in a tag name that
3422 * would terminate the ":tag" command.
3423 */
3424 for (i = 0; i < numfiles; ++i)
3425 {
3426 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3427 if (p != NULL)
3428 {
3429 vim_free(files[i]);
3430 files[i] = p;
3431 }
3432 }
3433 }
3434 }
3435}
3436
3437/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003438 * Put a backslash before the file name in "pp", which is in allocated memory.
3439 */
3440 static void
3441escape_fname(pp)
3442 char_u **pp;
3443{
3444 char_u *p;
3445
3446 p = alloc((unsigned)(STRLEN(*pp) + 2));
3447 if (p != NULL)
3448 {
3449 p[0] = '\\';
3450 STRCPY(p + 1, *pp);
3451 vim_free(*pp);
3452 *pp = p;
3453 }
3454}
3455
3456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 * For each file name in files[num_files]:
3458 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3459 */
3460 void
3461tilde_replace(orig_pat, num_files, files)
3462 char_u *orig_pat;
3463 int num_files;
3464 char_u **files;
3465{
3466 int i;
3467 char_u *p;
3468
3469 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3470 {
3471 for (i = 0; i < num_files; ++i)
3472 {
3473 p = home_replace_save(NULL, files[i]);
3474 if (p != NULL)
3475 {
3476 vim_free(files[i]);
3477 files[i] = p;
3478 }
3479 }
3480 }
3481}
3482
3483/*
3484 * Show all matches for completion on the command line.
3485 * Returns EXPAND_NOTHING when the character that triggered expansion should
3486 * be inserted like a normal character.
3487 */
3488/*ARGSUSED*/
3489 static int
3490showmatches(xp, wildmenu)
3491 expand_T *xp;
3492 int wildmenu;
3493{
3494#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3495 int num_files;
3496 char_u **files_found;
3497 int i, j, k;
3498 int maxlen;
3499 int lines;
3500 int columns;
3501 char_u *p;
3502 int lastlen;
3503 int attr;
3504 int showtail;
3505
3506 if (xp->xp_numfiles == -1)
3507 {
3508 set_expand_context(xp);
3509 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3510 &num_files, &files_found);
3511 showtail = expand_showtail(xp);
3512 if (i != EXPAND_OK)
3513 return i;
3514
3515 }
3516 else
3517 {
3518 num_files = xp->xp_numfiles;
3519 files_found = xp->xp_files;
3520 showtail = cmd_showtail;
3521 }
3522
3523#ifdef FEAT_WILDMENU
3524 if (!wildmenu)
3525 {
3526#endif
3527 msg_didany = FALSE; /* lines_left will be set */
3528 msg_start(); /* prepare for paging */
3529 msg_putchar('\n');
3530 out_flush();
3531 cmdline_row = msg_row;
3532 msg_didany = FALSE; /* lines_left will be set again */
3533 msg_start(); /* prepare for paging */
3534#ifdef FEAT_WILDMENU
3535 }
3536#endif
3537
3538 if (got_int)
3539 got_int = FALSE; /* only int. the completion, not the cmd line */
3540#ifdef FEAT_WILDMENU
3541 else if (wildmenu)
3542 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3543#endif
3544 else
3545 {
3546 /* find the length of the longest file name */
3547 maxlen = 0;
3548 for (i = 0; i < num_files; ++i)
3549 {
3550 if (!showtail && (xp->xp_context == EXPAND_FILES
3551 || xp->xp_context == EXPAND_BUFFERS))
3552 {
3553 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3554 j = vim_strsize(NameBuff);
3555 }
3556 else
3557 j = vim_strsize(L_SHOWFILE(i));
3558 if (j > maxlen)
3559 maxlen = j;
3560 }
3561
3562 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3563 lines = num_files;
3564 else
3565 {
3566 /* compute the number of columns and lines for the listing */
3567 maxlen += 2; /* two spaces between file names */
3568 columns = ((int)Columns + 2) / maxlen;
3569 if (columns < 1)
3570 columns = 1;
3571 lines = (num_files + columns - 1) / columns;
3572 }
3573
3574 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3575
3576 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3577 {
3578 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3579 msg_clr_eos();
3580 msg_advance(maxlen - 3);
3581 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3582 }
3583
3584 /* list the files line by line */
3585 for (i = 0; i < lines; ++i)
3586 {
3587 lastlen = 999;
3588 for (k = i; k < num_files; k += lines)
3589 {
3590 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3591 {
3592 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3593 p = files_found[k] + STRLEN(files_found[k]) + 1;
3594 msg_advance(maxlen + 1);
3595 msg_puts(p);
3596 msg_advance(maxlen + 3);
3597 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3598 break;
3599 }
3600 for (j = maxlen - lastlen; --j >= 0; )
3601 msg_putchar(' ');
3602 if (xp->xp_context == EXPAND_FILES
3603 || xp->xp_context == EXPAND_BUFFERS)
3604 {
3605 /* highlight directories */
3606 j = (mch_isdir(files_found[k]));
3607 if (showtail)
3608 p = L_SHOWFILE(k);
3609 else
3610 {
3611 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3612 TRUE);
3613 p = NameBuff;
3614 }
3615 }
3616 else
3617 {
3618 j = FALSE;
3619 p = L_SHOWFILE(k);
3620 }
3621 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3622 }
3623 if (msg_col > 0) /* when not wrapped around */
3624 {
3625 msg_clr_eos();
3626 msg_putchar('\n');
3627 }
3628 out_flush(); /* show one line at a time */
3629 if (got_int)
3630 {
3631 got_int = FALSE;
3632 break;
3633 }
3634 }
3635
3636 /*
3637 * we redraw the command below the lines that we have just listed
3638 * This is a bit tricky, but it saves a lot of screen updating.
3639 */
3640 cmdline_row = msg_row; /* will put it back later */
3641 }
3642
3643 if (xp->xp_numfiles == -1)
3644 FreeWild(num_files, files_found);
3645
3646 return EXPAND_OK;
3647}
3648
3649/*
3650 * Private gettail for showmatches() (and win_redr_status_matches()):
3651 * Find tail of file name path, but ignore trailing "/".
3652 */
3653 char_u *
3654sm_gettail(s)
3655 char_u *s;
3656{
3657 char_u *p;
3658 char_u *t = s;
3659 int had_sep = FALSE;
3660
3661 for (p = s; *p != NUL; )
3662 {
3663 if (vim_ispathsep(*p)
3664#ifdef BACKSLASH_IN_FILENAME
3665 && !rem_backslash(p)
3666#endif
3667 )
3668 had_sep = TRUE;
3669 else if (had_sep)
3670 {
3671 t = p;
3672 had_sep = FALSE;
3673 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003674 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676 return t;
3677}
3678
3679/*
3680 * Return TRUE if we only need to show the tail of completion matches.
3681 * When not completing file names or there is a wildcard in the path FALSE is
3682 * returned.
3683 */
3684 static int
3685expand_showtail(xp)
3686 expand_T *xp;
3687{
3688 char_u *s;
3689 char_u *end;
3690
3691 /* When not completing file names a "/" may mean something different. */
3692 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3693 return FALSE;
3694
3695 end = gettail(xp->xp_pattern);
3696 if (end == xp->xp_pattern) /* there is no path separator */
3697 return FALSE;
3698
3699 for (s = xp->xp_pattern; s < end; s++)
3700 {
3701 /* Skip escaped wildcards. Only when the backslash is not a path
3702 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3703 if (rem_backslash(s))
3704 ++s;
3705 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3706 return FALSE;
3707 }
3708 return TRUE;
3709}
3710
3711/*
3712 * Prepare a string for expansion.
3713 * When expanding file names: The string will be used with expand_wildcards().
3714 * Copy the file name into allocated memory and add a '*' at the end.
3715 * When expanding other names: The string will be used with regcomp(). Copy
3716 * the name into allocated memory and prepend "^".
3717 */
3718 char_u *
3719addstar(fname, len, context)
3720 char_u *fname;
3721 int len;
3722 int context; /* EXPAND_FILES etc. */
3723{
3724 char_u *retval;
3725 int i, j;
3726 int new_len;
3727 char_u *tail;
3728
3729 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3730 {
3731 /*
3732 * Matching will be done internally (on something other than files).
3733 * So we convert the file-matching-type wildcards into our kind for
3734 * use with vim_regcomp(). First work out how long it will be:
3735 */
3736
3737 /* For help tags the translation is done in find_help_tags().
3738 * For a tag pattern starting with "/" no translation is needed. */
3739 if (context == EXPAND_HELP
3740 || context == EXPAND_COLORS
3741 || context == EXPAND_COMPILER
3742 || (context == EXPAND_TAGS && fname[0] == '/'))
3743 retval = vim_strnsave(fname, len);
3744 else
3745 {
3746 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3747 for (i = 0; i < len; i++)
3748 {
3749 if (fname[i] == '*' || fname[i] == '~')
3750 new_len++; /* '*' needs to be replaced by ".*"
3751 '~' needs to be replaced by "\~" */
3752
3753 /* Buffer names are like file names. "." should be literal */
3754 if (context == EXPAND_BUFFERS && fname[i] == '.')
3755 new_len++; /* "." becomes "\." */
3756
3757 /* Custom expansion takes care of special things, match
3758 * backslashes literally (perhaps also for other types?) */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003759 if ((context == EXPAND_USER_DEFINED ||
3760 context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 new_len++; /* '\' becomes "\\" */
3762 }
3763 retval = alloc(new_len);
3764 if (retval != NULL)
3765 {
3766 retval[0] = '^';
3767 j = 1;
3768 for (i = 0; i < len; i++, j++)
3769 {
3770 /* Skip backslash. But why? At least keep it for custom
3771 * expansion. */
3772 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003773 && context != EXPAND_USER_LIST
3774 && fname[i] == '\\'
3775 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 break;
3777
3778 switch (fname[i])
3779 {
3780 case '*': retval[j++] = '.';
3781 break;
3782 case '~': retval[j++] = '\\';
3783 break;
3784 case '?': retval[j] = '.';
3785 continue;
3786 case '.': if (context == EXPAND_BUFFERS)
3787 retval[j++] = '\\';
3788 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003789 case '\\': if (context == EXPAND_USER_DEFINED
3790 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 retval[j++] = '\\';
3792 break;
3793 }
3794 retval[j] = fname[i];
3795 }
3796 retval[j] = NUL;
3797 }
3798 }
3799 }
3800 else
3801 {
3802 retval = alloc(len + 4);
3803 if (retval != NULL)
3804 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003805 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806
3807 /*
3808 * Don't add a star to ~, ~user, $var or `cmd`.
3809 * ~ would be at the start of the file name, but not the tail.
3810 * $ could be anywhere in the tail.
3811 * ` could be anywhere in the file name.
3812 */
3813 tail = gettail(retval);
3814 if ((*retval != '~' || tail != retval)
3815 && vim_strchr(tail, '$') == NULL
3816 && vim_strchr(retval, '`') == NULL)
3817 retval[len++] = '*';
3818 retval[len] = NUL;
3819 }
3820 }
3821 return retval;
3822}
3823
3824/*
3825 * Must parse the command line so far to work out what context we are in.
3826 * Completion can then be done based on that context.
3827 * This routine sets the variables:
3828 * xp->xp_pattern The start of the pattern to be expanded within
3829 * the command line (ends at the cursor).
3830 * xp->xp_context The type of thing to expand. Will be one of:
3831 *
3832 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3833 * the command line, like an unknown command. Caller
3834 * should beep.
3835 * EXPAND_NOTHING Unrecognised context for completion, use char like
3836 * a normal char, rather than for completion. eg
3837 * :s/^I/
3838 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3839 * it.
3840 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3841 * EXPAND_FILES After command with XFILE set, or after setting
3842 * with P_EXPAND set. eg :e ^I, :w>>^I
3843 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3844 * when we know only directories are of interest. eg
3845 * :set dir=^I
3846 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3847 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3848 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3849 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3850 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3851 * EXPAND_EVENTS Complete event names
3852 * EXPAND_SYNTAX Complete :syntax command arguments
3853 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3854 * EXPAND_AUGROUP Complete autocommand group names
3855 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3856 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3857 * eg :unmap a^I , :cunab x^I
3858 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3859 * eg :call sub^I
3860 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3861 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3862 * names in expressions, eg :while s^I
3863 * EXPAND_ENV_VARS Complete environment variable names
3864 */
3865 static void
3866set_expand_context(xp)
3867 expand_T *xp;
3868{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003869 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 if (ccline.cmdfirstc != ':'
3871#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003872 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003873 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874#endif
3875 )
3876 {
3877 xp->xp_context = EXPAND_NOTHING;
3878 return;
3879 }
3880 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3881}
3882
3883 void
3884set_cmd_context(xp, str, len, col)
3885 expand_T *xp;
3886 char_u *str; /* start of command line */
3887 int len; /* length of command line (excl. NUL) */
3888 int col; /* position of cursor */
3889{
3890 int old_char = NUL;
3891 char_u *nextcomm;
3892
3893 /*
3894 * Avoid a UMR warning from Purify, only save the character if it has been
3895 * written before.
3896 */
3897 if (col < len)
3898 old_char = str[col];
3899 str[col] = NUL;
3900 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003901
3902#ifdef FEAT_EVAL
3903 if (ccline.cmdfirstc == '=')
3904 /* pass CMD_SIZE because there is no real command */
3905 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003906 else if (ccline.input_fn)
3907 {
3908 xp->xp_context = ccline.xp_context;
3909 xp->xp_pattern = ccline.cmdbuff;
3910 xp->xp_arg = ccline.xp_arg;
3911 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003912 else
3913#endif
3914 while (nextcomm != NULL)
3915 nextcomm = set_one_cmd_context(xp, nextcomm);
3916
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 str[col] = old_char;
3918}
3919
3920/*
3921 * Expand the command line "str" from context "xp".
3922 * "xp" must have been set by set_cmd_context().
3923 * xp->xp_pattern points into "str", to where the text that is to be expanded
3924 * starts.
3925 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
3926 * cursor.
3927 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
3928 * key that triggered expansion literally.
3929 * Returns EXPAND_OK otherwise.
3930 */
3931 int
3932expand_cmdline(xp, str, col, matchcount, matches)
3933 expand_T *xp;
3934 char_u *str; /* start of command line */
3935 int col; /* position of cursor */
3936 int *matchcount; /* return: nr of matches */
3937 char_u ***matches; /* return: array of pointers to matches */
3938{
3939 char_u *file_str = NULL;
3940
3941 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3942 {
3943 beep_flush();
3944 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
3945 }
3946 if (xp->xp_context == EXPAND_NOTHING)
3947 {
3948 /* Caller can use the character as a normal char instead */
3949 return EXPAND_NOTHING;
3950 }
3951
3952 /* add star to file name, or convert to regexp if not exp. files. */
3953 file_str = addstar(xp->xp_pattern,
3954 (int)(str + col - xp->xp_pattern), xp->xp_context);
3955 if (file_str == NULL)
3956 return EXPAND_UNSUCCESSFUL;
3957
3958 /* find all files that match the description */
3959 if (ExpandFromContext(xp, file_str, matchcount, matches,
3960 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
3961 {
3962 *matchcount = 0;
3963 *matches = NULL;
3964 }
3965 vim_free(file_str);
3966
3967 return EXPAND_OK;
3968}
3969
3970#ifdef FEAT_MULTI_LANG
3971/*
3972 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
3973 */
3974static void cleanup_help_tags __ARGS((int num_file, char_u **file));
3975
3976 static void
3977cleanup_help_tags(num_file, file)
3978 int num_file;
3979 char_u **file;
3980{
3981 int i, j;
3982 int len;
3983
3984 for (i = 0; i < num_file; ++i)
3985 {
3986 len = (int)STRLEN(file[i]) - 3;
3987 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
3988 {
3989 /* Sorting on priority means the same item in another language may
3990 * be anywhere. Search all items for a match up to the "@en". */
3991 for (j = 0; j < num_file; ++j)
3992 if (j != i
3993 && (int)STRLEN(file[j]) == len + 3
3994 && STRNCMP(file[i], file[j], len + 1) == 0)
3995 break;
3996 if (j == num_file)
3997 file[i][len] = NUL;
3998 }
3999 }
4000}
4001#endif
4002
4003/*
4004 * Do the expansion based on xp->xp_context and "pat".
4005 */
4006 static int
4007ExpandFromContext(xp, pat, num_file, file, options)
4008 expand_T *xp;
4009 char_u *pat;
4010 int *num_file;
4011 char_u ***file;
4012 int options;
4013{
4014#ifdef FEAT_CMDL_COMPL
4015 regmatch_T regmatch;
4016#endif
4017 int ret;
4018 int flags;
4019
4020 flags = EW_DIR; /* include directories */
4021 if (options & WILD_LIST_NOTFOUND)
4022 flags |= EW_NOTFOUND;
4023 if (options & WILD_ADD_SLASH)
4024 flags |= EW_ADDSLASH;
4025 if (options & WILD_KEEP_ALL)
4026 flags |= EW_KEEPALL;
4027 if (options & WILD_SILENT)
4028 flags |= EW_SILENT;
4029
4030 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4031 {
4032 /*
4033 * Expand file or directory names.
4034 */
4035 int free_pat = FALSE;
4036 int i;
4037
4038 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4039 * space */
4040 if (xp->xp_backslash != XP_BS_NONE)
4041 {
4042 free_pat = TRUE;
4043 pat = vim_strsave(pat);
4044 for (i = 0; pat[i]; ++i)
4045 if (pat[i] == '\\')
4046 {
4047 if (xp->xp_backslash == XP_BS_THREE
4048 && pat[i + 1] == '\\'
4049 && pat[i + 2] == '\\'
4050 && pat[i + 3] == ' ')
4051 STRCPY(pat + i, pat + i + 3);
4052 if (xp->xp_backslash == XP_BS_ONE
4053 && pat[i + 1] == ' ')
4054 STRCPY(pat + i, pat + i + 1);
4055 }
4056 }
4057
4058 if (xp->xp_context == EXPAND_FILES)
4059 flags |= EW_FILE;
4060 else
4061 flags = (flags | EW_DIR) & ~EW_FILE;
4062 ret = expand_wildcards(1, &pat, num_file, file, flags);
4063 if (free_pat)
4064 vim_free(pat);
4065 return ret;
4066 }
4067
4068 *file = (char_u **)"";
4069 *num_file = 0;
4070 if (xp->xp_context == EXPAND_HELP)
4071 {
4072 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4073 {
4074#ifdef FEAT_MULTI_LANG
4075 cleanup_help_tags(*num_file, *file);
4076#endif
4077 return OK;
4078 }
4079 return FAIL;
4080 }
4081
4082#ifndef FEAT_CMDL_COMPL
4083 return FAIL;
4084#else
4085 if (xp->xp_context == EXPAND_OLD_SETTING)
4086 return ExpandOldSetting(num_file, file);
4087 if (xp->xp_context == EXPAND_BUFFERS)
4088 return ExpandBufnames(pat, num_file, file, options);
4089 if (xp->xp_context == EXPAND_TAGS
4090 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4091 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4092 if (xp->xp_context == EXPAND_COLORS)
4093 return ExpandRTDir(pat, num_file, file, "colors");
4094 if (xp->xp_context == EXPAND_COMPILER)
4095 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004096# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4097 if (xp->xp_context == EXPAND_USER_LIST)
4098 return ExpandUserList(xp, num_file, file);
4099# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4102 if (regmatch.regprog == NULL)
4103 return FAIL;
4104
4105 /* set ignore-case according to p_ic, p_scs and pat */
4106 regmatch.rm_ic = ignorecase(pat);
4107
4108 if (xp->xp_context == EXPAND_SETTINGS
4109 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4110 ret = ExpandSettings(xp, &regmatch, num_file, file);
4111 else if (xp->xp_context == EXPAND_MAPPINGS)
4112 ret = ExpandMappings(&regmatch, num_file, file);
4113# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4114 else if (xp->xp_context == EXPAND_USER_DEFINED)
4115 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4116# endif
4117 else
4118 {
4119 static struct expgen
4120 {
4121 int context;
4122 char_u *((*func)__ARGS((expand_T *, int)));
4123 int ic;
4124 } tab[] =
4125 {
4126 {EXPAND_COMMANDS, get_command_name, FALSE},
4127#ifdef FEAT_USR_CMDS
4128 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4129 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4130 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4131 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4132#endif
4133#ifdef FEAT_EVAL
4134 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4135 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4136 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4137 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4138#endif
4139#ifdef FEAT_MENU
4140 {EXPAND_MENUS, get_menu_name, FALSE},
4141 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4142#endif
4143#ifdef FEAT_SYN_HL
4144 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4145#endif
4146 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4147#ifdef FEAT_AUTOCMD
4148 {EXPAND_EVENTS, get_event_name, TRUE},
4149 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4150#endif
4151#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4152 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4153 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4154#endif
4155 {EXPAND_ENV_VARS, get_env_name, TRUE},
4156 };
4157 int i;
4158
4159 /*
4160 * Find a context in the table and call the ExpandGeneric() with the
4161 * right function to do the expansion.
4162 */
4163 ret = FAIL;
4164 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4165 if (xp->xp_context == tab[i].context)
4166 {
4167 if (tab[i].ic)
4168 regmatch.rm_ic = TRUE;
4169 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4170 break;
4171 }
4172 }
4173
4174 vim_free(regmatch.regprog);
4175
4176 return ret;
4177#endif /* FEAT_CMDL_COMPL */
4178}
4179
4180#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4181/*
4182 * Expand a list of names.
4183 *
4184 * Generic function for command line completion. It calls a function to
4185 * obtain strings, one by one. The strings are matched against a regexp
4186 * program. Matching strings are copied into an array, which is returned.
4187 *
4188 * Returns OK when no problems encountered, FAIL for error (out of memory).
4189 */
4190 int
4191ExpandGeneric(xp, regmatch, num_file, file, func)
4192 expand_T *xp;
4193 regmatch_T *regmatch;
4194 int *num_file;
4195 char_u ***file;
4196 char_u *((*func)__ARGS((expand_T *, int)));
4197 /* returns a string from the list */
4198{
4199 int i;
4200 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004201 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 char_u *str;
4203
4204 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004205 * round == 0: count the number of matching names
4206 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004208 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 for (i = 0; ; ++i)
4211 {
4212 str = (*func)(xp, i);
4213 if (str == NULL) /* end of list */
4214 break;
4215 if (*str == NUL) /* skip empty strings */
4216 continue;
4217
4218 if (vim_regexec(regmatch, str, (colnr_T)0))
4219 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004220 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
4222 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4223 (*file)[count] = str;
4224#ifdef FEAT_MENU
4225 if (func == get_menu_names && str != NULL)
4226 {
4227 /* test for separator added by get_menu_names() */
4228 str += STRLEN(str) - 1;
4229 if (*str == '\001')
4230 *str = '.';
4231 }
4232#endif
4233 }
4234 ++count;
4235 }
4236 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004237 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
4239 if (count == 0)
4240 return OK;
4241 *num_file = count;
4242 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4243 if (*file == NULL)
4244 {
4245 *file = (char_u **)"";
4246 return FAIL;
4247 }
4248 count = 0;
4249 }
4250 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004251
4252 /* Sort the results. */
4253 sort_strings(*file, *num_file);
4254
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return OK;
4256}
4257
4258# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004259static 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));
4260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004262 * call "user_expand_func()" to invoke a user defined VimL function and return
4263 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004265 static void *
4266call_user_expand_func(user_expand_func, xp, num_file, file)
4267 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 int *num_file;
4270 char_u ***file;
4271{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004272 char_u keep;
4273 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004276 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004277 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004280 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 *num_file = 0;
4282 *file = NULL;
4283
4284 keep = ccline.cmdbuff[ccline.cmdlen];
4285 ccline.cmdbuff[ccline.cmdlen] = 0;
4286 sprintf((char *)num, "%d", ccline.cmdpos);
4287 args[0] = xp->xp_pattern;
4288 args[1] = ccline.cmdbuff;
4289 args[2] = num;
4290
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004291 /* Save the cmdline, we don't know what the function may do. */
4292 save_ccline = ccline;
4293 ccline.cmdbuff = NULL;
4294 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004296
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004297 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004298
4299 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004303
4304 return ret;
4305}
4306
4307/*
4308 * Expand names with a function defined by the user.
4309 */
4310 static int
4311ExpandUserDefined(xp, regmatch, num_file, file)
4312 expand_T *xp;
4313 regmatch_T *regmatch;
4314 int *num_file;
4315 char_u ***file;
4316{
4317 char_u *retstr;
4318 char_u *s;
4319 char_u *e;
4320 char_u keep;
4321 garray_T ga;
4322
4323 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4324 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
4326
4327 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004328 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 e = vim_strchr(s, '\n');
4331 if (e == NULL)
4332 e = s + STRLEN(s);
4333 keep = *e;
4334 *e = 0;
4335
4336 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4337 {
4338 *e = keep;
4339 if (*e != NUL)
4340 ++e;
4341 continue;
4342 }
4343
4344 if (ga_grow(&ga, 1) == FAIL)
4345 break;
4346
4347 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4348 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
4350 *e = keep;
4351 if (*e != NUL)
4352 ++e;
4353 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004354 vim_free(retstr);
4355 *file = ga.ga_data;
4356 *num_file = ga.ga_len;
4357 return OK;
4358}
4359
4360/*
4361 * Expand names with a list returned by a function defined by the user.
4362 */
4363 static int
4364ExpandUserList(xp, num_file, file)
4365 expand_T *xp;
4366 int *num_file;
4367 char_u ***file;
4368{
4369 list_T *retlist;
4370 listitem_T *li;
4371 garray_T ga;
4372
4373 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4374 if (retlist == NULL)
4375 return FAIL;
4376
4377 ga_init2(&ga, (int)sizeof(char *), 3);
4378 /* Loop over the items in the list. */
4379 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4380 {
4381 if (li->li_tv.v_type != VAR_STRING)
4382 continue; /* Skip non-string items */
4383
4384 if (ga_grow(&ga, 1) == FAIL)
4385 break;
4386
4387 ((char_u **)ga.ga_data)[ga.ga_len] =
4388 vim_strsave(li->li_tv.vval.v_string);
4389 ++ga.ga_len;
4390 }
4391 list_unref(retlist);
4392
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 *file = ga.ga_data;
4394 *num_file = ga.ga_len;
4395 return OK;
4396}
4397#endif
4398
4399/*
4400 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4401 * or compiler names.
4402 */
4403 static int
4404ExpandRTDir(pat, num_file, file, dirname)
4405 char_u *pat;
4406 int *num_file;
4407 char_u ***file;
4408 char *dirname; /* "colors" or "compiler" */
4409{
4410 char_u *all;
4411 char_u *s;
4412 char_u *e;
4413 garray_T ga;
4414
4415 *num_file = 0;
4416 *file = NULL;
4417 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4418 if (s == NULL)
4419 return FAIL;
4420 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4421 all = globpath(p_rtp, s);
4422 vim_free(s);
4423 if (all == NULL)
4424 return FAIL;
4425
4426 ga_init2(&ga, (int)sizeof(char *), 3);
4427 for (s = all; *s != NUL; s = e)
4428 {
4429 e = vim_strchr(s, '\n');
4430 if (e == NULL)
4431 e = s + STRLEN(s);
4432 if (ga_grow(&ga, 1) == FAIL)
4433 break;
4434 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4435 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004436 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 if (*s == '\n' || vim_ispathsep(*s))
4438 break;
4439 ++s;
4440 ((char_u **)ga.ga_data)[ga.ga_len] =
4441 vim_strnsave(s, (int)(e - s - 4));
4442 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 if (*e != NUL)
4445 ++e;
4446 }
4447 vim_free(all);
4448 *file = ga.ga_data;
4449 *num_file = ga.ga_len;
4450 return OK;
4451}
4452
4453#endif
4454
4455#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4456/*
4457 * Expand "file" for all comma-separated directories in "path".
4458 * Returns an allocated string with all matches concatenated, separated by
4459 * newlines. Returns NULL for an error or no matches.
4460 */
4461 char_u *
4462globpath(path, file)
4463 char_u *path;
4464 char_u *file;
4465{
4466 expand_T xpc;
4467 char_u *buf;
4468 garray_T ga;
4469 int i;
4470 int len;
4471 int num_p;
4472 char_u **p;
4473 char_u *cur = NULL;
4474
4475 buf = alloc(MAXPATHL);
4476 if (buf == NULL)
4477 return NULL;
4478
4479 xpc.xp_context = EXPAND_FILES;
4480 xpc.xp_backslash = XP_BS_NONE;
4481 ga_init2(&ga, 1, 100);
4482
4483 /* Loop over all entries in {path}. */
4484 while (*path != NUL)
4485 {
4486 /* Copy one item of the path to buf[] and concatenate the file name. */
4487 copy_option_part(&path, buf, MAXPATHL, ",");
4488 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4489 {
4490 add_pathsep(buf);
4491 STRCAT(buf, file);
4492 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4493 && num_p > 0)
4494 {
4495 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4496 for (len = 0, i = 0; i < num_p; ++i)
4497 len += (long_u)STRLEN(p[i]) + 1;
4498
4499 /* Concatenate new results to previous ones. */
4500 if (ga_grow(&ga, len) == OK)
4501 {
4502 cur = (char_u *)ga.ga_data + ga.ga_len;
4503 for (i = 0; i < num_p; ++i)
4504 {
4505 STRCPY(cur, p[i]);
4506 cur += STRLEN(p[i]);
4507 *cur++ = '\n';
4508 }
4509 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511 FreeWild(num_p, p);
4512 }
4513 }
4514 }
4515 if (cur != NULL)
4516 *--cur = 0; /* Replace trailing newline with NUL */
4517
4518 vim_free(buf);
4519 return (char_u *)ga.ga_data;
4520}
4521
4522#endif
4523
4524#if defined(FEAT_CMDHIST) || defined(PROTO)
4525
4526/*********************************
4527 * Command line history stuff *
4528 *********************************/
4529
4530/*
4531 * Translate a history character to the associated type number.
4532 */
4533 static int
4534hist_char2type(c)
4535 int c;
4536{
4537 if (c == ':')
4538 return HIST_CMD;
4539 if (c == '=')
4540 return HIST_EXPR;
4541 if (c == '@')
4542 return HIST_INPUT;
4543 if (c == '>')
4544 return HIST_DEBUG;
4545 return HIST_SEARCH; /* must be '?' or '/' */
4546}
4547
4548/*
4549 * Table of history names.
4550 * These names are used in :history and various hist...() functions.
4551 * It is sufficient to give the significant prefix of a history name.
4552 */
4553
4554static char *(history_names[]) =
4555{
4556 "cmd",
4557 "search",
4558 "expr",
4559 "input",
4560 "debug",
4561 NULL
4562};
4563
4564/*
4565 * init_history() - Initialize the command line history.
4566 * Also used to re-allocate the history when the size changes.
4567 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004568 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569init_history()
4570{
4571 int newlen; /* new length of history table */
4572 histentry_T *temp;
4573 int i;
4574 int j;
4575 int type;
4576
4577 /*
4578 * If size of history table changed, reallocate it
4579 */
4580 newlen = (int)p_hi;
4581 if (newlen != hislen) /* history length changed */
4582 {
4583 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4584 {
4585 if (newlen)
4586 {
4587 temp = (histentry_T *)lalloc(
4588 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4589 if (temp == NULL) /* out of memory! */
4590 {
4591 if (type == 0) /* first one: just keep the old length */
4592 {
4593 newlen = hislen;
4594 break;
4595 }
4596 /* Already changed one table, now we can only have zero
4597 * length for all tables. */
4598 newlen = 0;
4599 type = -1;
4600 continue;
4601 }
4602 }
4603 else
4604 temp = NULL;
4605 if (newlen == 0 || temp != NULL)
4606 {
4607 if (hisidx[type] < 0) /* there are no entries yet */
4608 {
4609 for (i = 0; i < newlen; ++i)
4610 {
4611 temp[i].hisnum = 0;
4612 temp[i].hisstr = NULL;
4613 }
4614 }
4615 else if (newlen > hislen) /* array becomes bigger */
4616 {
4617 for (i = 0; i <= hisidx[type]; ++i)
4618 temp[i] = history[type][i];
4619 j = i;
4620 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4621 {
4622 temp[i].hisnum = 0;
4623 temp[i].hisstr = NULL;
4624 }
4625 for ( ; j < hislen; ++i, ++j)
4626 temp[i] = history[type][j];
4627 }
4628 else /* array becomes smaller or 0 */
4629 {
4630 j = hisidx[type];
4631 for (i = newlen - 1; ; --i)
4632 {
4633 if (i >= 0) /* copy newest entries */
4634 temp[i] = history[type][j];
4635 else /* remove older entries */
4636 vim_free(history[type][j].hisstr);
4637 if (--j < 0)
4638 j = hislen - 1;
4639 if (j == hisidx[type])
4640 break;
4641 }
4642 hisidx[type] = newlen - 1;
4643 }
4644 vim_free(history[type]);
4645 history[type] = temp;
4646 }
4647 }
4648 hislen = newlen;
4649 }
4650}
4651
4652/*
4653 * Check if command line 'str' is already in history.
4654 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4655 */
4656 static int
4657in_history(type, str, move_to_front)
4658 int type;
4659 char_u *str;
4660 int move_to_front; /* Move the entry to the front if it exists */
4661{
4662 int i;
4663 int last_i = -1;
4664
4665 if (hisidx[type] < 0)
4666 return FALSE;
4667 i = hisidx[type];
4668 do
4669 {
4670 if (history[type][i].hisstr == NULL)
4671 return FALSE;
4672 if (STRCMP(str, history[type][i].hisstr) == 0)
4673 {
4674 if (!move_to_front)
4675 return TRUE;
4676 last_i = i;
4677 break;
4678 }
4679 if (--i < 0)
4680 i = hislen - 1;
4681 } while (i != hisidx[type]);
4682
4683 if (last_i >= 0)
4684 {
4685 str = history[type][i].hisstr;
4686 while (i != hisidx[type])
4687 {
4688 if (++i >= hislen)
4689 i = 0;
4690 history[type][last_i] = history[type][i];
4691 last_i = i;
4692 }
4693 history[type][i].hisstr = str;
4694 history[type][i].hisnum = ++hisnum[type];
4695 return TRUE;
4696 }
4697 return FALSE;
4698}
4699
4700/*
4701 * Convert history name (from table above) to its HIST_ equivalent.
4702 * When "name" is empty, return "cmd" history.
4703 * Returns -1 for unknown history name.
4704 */
4705 int
4706get_histtype(name)
4707 char_u *name;
4708{
4709 int i;
4710 int len = (int)STRLEN(name);
4711
4712 /* No argument: use current history. */
4713 if (len == 0)
4714 return hist_char2type(ccline.cmdfirstc);
4715
4716 for (i = 0; history_names[i] != NULL; ++i)
4717 if (STRNICMP(name, history_names[i], len) == 0)
4718 return i;
4719
4720 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4721 return hist_char2type(name[0]);
4722
4723 return -1;
4724}
4725
4726static int last_maptick = -1; /* last seen maptick */
4727
4728/*
4729 * Add the given string to the given history. If the string is already in the
4730 * history then it is moved to the front. "histype" may be one of he HIST_
4731 * values.
4732 */
4733 void
4734add_to_history(histype, new_entry, in_map, sep)
4735 int histype;
4736 char_u *new_entry;
4737 int in_map; /* consider maptick when inside a mapping */
4738 int sep; /* separator character used (search hist) */
4739{
4740 histentry_T *hisptr;
4741 int len;
4742
4743 if (hislen == 0) /* no history */
4744 return;
4745
4746 /*
4747 * Searches inside the same mapping overwrite each other, so that only
4748 * the last line is kept. Be careful not to remove a line that was moved
4749 * down, only lines that were added.
4750 */
4751 if (histype == HIST_SEARCH && in_map)
4752 {
4753 if (maptick == last_maptick)
4754 {
4755 /* Current line is from the same mapping, remove it */
4756 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4757 vim_free(hisptr->hisstr);
4758 hisptr->hisstr = NULL;
4759 hisptr->hisnum = 0;
4760 --hisnum[histype];
4761 if (--hisidx[HIST_SEARCH] < 0)
4762 hisidx[HIST_SEARCH] = hislen - 1;
4763 }
4764 last_maptick = -1;
4765 }
4766 if (!in_history(histype, new_entry, TRUE))
4767 {
4768 if (++hisidx[histype] == hislen)
4769 hisidx[histype] = 0;
4770 hisptr = &history[histype][hisidx[histype]];
4771 vim_free(hisptr->hisstr);
4772
4773 /* Store the separator after the NUL of the string. */
4774 len = STRLEN(new_entry);
4775 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4776 if (hisptr->hisstr != NULL)
4777 hisptr->hisstr[len + 1] = sep;
4778
4779 hisptr->hisnum = ++hisnum[histype];
4780 if (histype == HIST_SEARCH && in_map)
4781 last_maptick = maptick;
4782 }
4783}
4784
4785#if defined(FEAT_EVAL) || defined(PROTO)
4786
4787/*
4788 * Get identifier of newest history entry.
4789 * "histype" may be one of the HIST_ values.
4790 */
4791 int
4792get_history_idx(histype)
4793 int histype;
4794{
4795 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4796 || hisidx[histype] < 0)
4797 return -1;
4798
4799 return history[histype][hisidx[histype]].hisnum;
4800}
4801
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004802static struct cmdline_info *get_ccline_ptr __ARGS((void));
4803
4804/*
4805 * Get pointer to the command line info to use. cmdline_paste() may clear
4806 * ccline and put the previous value in prev_ccline.
4807 */
4808 static struct cmdline_info *
4809get_ccline_ptr()
4810{
4811 if ((State & CMDLINE) == 0)
4812 return NULL;
4813 if (ccline.cmdbuff != NULL)
4814 return &ccline;
4815 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4816 return &prev_ccline;
4817 return NULL;
4818}
4819
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820/*
4821 * Get the current command line in allocated memory.
4822 * Only works when the command line is being edited.
4823 * Returns NULL when something is wrong.
4824 */
4825 char_u *
4826get_cmdline_str()
4827{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004828 struct cmdline_info *p = get_ccline_ptr();
4829
4830 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004832 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833}
4834
4835/*
4836 * Get the current command line position, counted in bytes.
4837 * Zero is the first position.
4838 * Only works when the command line is being edited.
4839 * Returns -1 when something is wrong.
4840 */
4841 int
4842get_cmdline_pos()
4843{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004844 struct cmdline_info *p = get_ccline_ptr();
4845
4846 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004848 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849}
4850
4851/*
4852 * Set the command line byte position to "pos". Zero is the first position.
4853 * Only works when the command line is being edited.
4854 * Returns 1 when failed, 0 when OK.
4855 */
4856 int
4857set_cmdline_pos(pos)
4858 int pos;
4859{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004860 struct cmdline_info *p = get_ccline_ptr();
4861
4862 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 return 1;
4864
4865 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4866 * changed the command line. */
4867 if (pos < 0)
4868 new_cmdpos = 0;
4869 else
4870 new_cmdpos = pos;
4871 return 0;
4872}
4873
4874/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004875 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00004876 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004877 * Only works when the command line is being edited.
4878 * Returns NUL when something is wrong.
4879 */
4880 int
4881get_cmdline_type()
4882{
4883 struct cmdline_info *p = get_ccline_ptr();
4884
4885 if (p == NULL)
4886 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00004887 if (p->cmdfirstc == NUL)
4888 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004889 return p->cmdfirstc;
4890}
4891
4892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 * Calculate history index from a number:
4894 * num > 0: seen as identifying number of a history entry
4895 * num < 0: relative position in history wrt newest entry
4896 * "histype" may be one of the HIST_ values.
4897 */
4898 static int
4899calc_hist_idx(histype, num)
4900 int histype;
4901 int num;
4902{
4903 int i;
4904 histentry_T *hist;
4905 int wrapped = FALSE;
4906
4907 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4908 || (i = hisidx[histype]) < 0 || num == 0)
4909 return -1;
4910
4911 hist = history[histype];
4912 if (num > 0)
4913 {
4914 while (hist[i].hisnum > num)
4915 if (--i < 0)
4916 {
4917 if (wrapped)
4918 break;
4919 i += hislen;
4920 wrapped = TRUE;
4921 }
4922 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
4923 return i;
4924 }
4925 else if (-num <= hislen)
4926 {
4927 i += num + 1;
4928 if (i < 0)
4929 i += hislen;
4930 if (hist[i].hisstr != NULL)
4931 return i;
4932 }
4933 return -1;
4934}
4935
4936/*
4937 * Get a history entry by its index.
4938 * "histype" may be one of the HIST_ values.
4939 */
4940 char_u *
4941get_history_entry(histype, idx)
4942 int histype;
4943 int idx;
4944{
4945 idx = calc_hist_idx(histype, idx);
4946 if (idx >= 0)
4947 return history[histype][idx].hisstr;
4948 else
4949 return (char_u *)"";
4950}
4951
4952/*
4953 * Clear all entries of a history.
4954 * "histype" may be one of the HIST_ values.
4955 */
4956 int
4957clr_history(histype)
4958 int histype;
4959{
4960 int i;
4961 histentry_T *hisptr;
4962
4963 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
4964 {
4965 hisptr = history[histype];
4966 for (i = hislen; i--;)
4967 {
4968 vim_free(hisptr->hisstr);
4969 hisptr->hisnum = 0;
4970 hisptr++->hisstr = NULL;
4971 }
4972 hisidx[histype] = -1; /* mark history as cleared */
4973 hisnum[histype] = 0; /* reset identifier counter */
4974 return OK;
4975 }
4976 return FAIL;
4977}
4978
4979/*
4980 * Remove all entries matching {str} from a history.
4981 * "histype" may be one of the HIST_ values.
4982 */
4983 int
4984del_history_entry(histype, str)
4985 int histype;
4986 char_u *str;
4987{
4988 regmatch_T regmatch;
4989 histentry_T *hisptr;
4990 int idx;
4991 int i;
4992 int last;
4993 int found = FALSE;
4994
4995 regmatch.regprog = NULL;
4996 regmatch.rm_ic = FALSE; /* always match case */
4997 if (hislen != 0
4998 && histype >= 0
4999 && histype < HIST_COUNT
5000 && *str != NUL
5001 && (idx = hisidx[histype]) >= 0
5002 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5003 != NULL)
5004 {
5005 i = last = idx;
5006 do
5007 {
5008 hisptr = &history[histype][i];
5009 if (hisptr->hisstr == NULL)
5010 break;
5011 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5012 {
5013 found = TRUE;
5014 vim_free(hisptr->hisstr);
5015 hisptr->hisstr = NULL;
5016 hisptr->hisnum = 0;
5017 }
5018 else
5019 {
5020 if (i != last)
5021 {
5022 history[histype][last] = *hisptr;
5023 hisptr->hisstr = NULL;
5024 hisptr->hisnum = 0;
5025 }
5026 if (--last < 0)
5027 last += hislen;
5028 }
5029 if (--i < 0)
5030 i += hislen;
5031 } while (i != idx);
5032 if (history[histype][idx].hisstr == NULL)
5033 hisidx[histype] = -1;
5034 }
5035 vim_free(regmatch.regprog);
5036 return found;
5037}
5038
5039/*
5040 * Remove an indexed entry from a history.
5041 * "histype" may be one of the HIST_ values.
5042 */
5043 int
5044del_history_idx(histype, idx)
5045 int histype;
5046 int idx;
5047{
5048 int i, j;
5049
5050 i = calc_hist_idx(histype, idx);
5051 if (i < 0)
5052 return FALSE;
5053 idx = hisidx[histype];
5054 vim_free(history[histype][i].hisstr);
5055
5056 /* When deleting the last added search string in a mapping, reset
5057 * last_maptick, so that the last added search string isn't deleted again.
5058 */
5059 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5060 last_maptick = -1;
5061
5062 while (i != idx)
5063 {
5064 j = (i + 1) % hislen;
5065 history[histype][i] = history[histype][j];
5066 i = j;
5067 }
5068 history[histype][i].hisstr = NULL;
5069 history[histype][i].hisnum = 0;
5070 if (--i < 0)
5071 i += hislen;
5072 hisidx[histype] = i;
5073 return TRUE;
5074}
5075
5076#endif /* FEAT_EVAL */
5077
5078#if defined(FEAT_CRYPT) || defined(PROTO)
5079/*
5080 * Very specific function to remove the value in ":set key=val" from the
5081 * history.
5082 */
5083 void
5084remove_key_from_history()
5085{
5086 char_u *p;
5087 int i;
5088
5089 i = hisidx[HIST_CMD];
5090 if (i < 0)
5091 return;
5092 p = history[HIST_CMD][i].hisstr;
5093 if (p != NULL)
5094 for ( ; *p; ++p)
5095 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5096 {
5097 p = vim_strchr(p + 3, '=');
5098 if (p == NULL)
5099 break;
5100 ++p;
5101 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5102 if (p[i] == '\\' && p[i + 1])
5103 ++i;
5104 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5105 --p;
5106 }
5107}
5108#endif
5109
5110#endif /* FEAT_CMDHIST */
5111
5112#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5113/*
5114 * Get indices "num1,num2" that specify a range within a list (not a range of
5115 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5116 * Returns OK if parsed successfully, otherwise FAIL.
5117 */
5118 int
5119get_list_range(str, num1, num2)
5120 char_u **str;
5121 int *num1;
5122 int *num2;
5123{
5124 int len;
5125 int first = FALSE;
5126 long num;
5127
5128 *str = skipwhite(*str);
5129 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5130 {
5131 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5132 *str += len;
5133 *num1 = (int)num;
5134 first = TRUE;
5135 }
5136 *str = skipwhite(*str);
5137 if (**str == ',') /* parse "to" part of range */
5138 {
5139 *str = skipwhite(*str + 1);
5140 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5141 if (len > 0)
5142 {
5143 *num2 = (int)num;
5144 *str = skipwhite(*str + len);
5145 }
5146 else if (!first) /* no number given at all */
5147 return FAIL;
5148 }
5149 else if (first) /* only one number given */
5150 *num2 = *num1;
5151 return OK;
5152}
5153#endif
5154
5155#if defined(FEAT_CMDHIST) || defined(PROTO)
5156/*
5157 * :history command - print a history
5158 */
5159 void
5160ex_history(eap)
5161 exarg_T *eap;
5162{
5163 histentry_T *hist;
5164 int histype1 = HIST_CMD;
5165 int histype2 = HIST_CMD;
5166 int hisidx1 = 1;
5167 int hisidx2 = -1;
5168 int idx;
5169 int i, j, k;
5170 char_u *end;
5171 char_u *arg = eap->arg;
5172
5173 if (hislen == 0)
5174 {
5175 MSG(_("'history' option is zero"));
5176 return;
5177 }
5178
5179 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5180 {
5181 end = arg;
5182 while (ASCII_ISALPHA(*end)
5183 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5184 end++;
5185 i = *end;
5186 *end = NUL;
5187 histype1 = get_histtype(arg);
5188 if (histype1 == -1)
5189 {
5190 if (STRICMP(arg, "all") == 0)
5191 {
5192 histype1 = 0;
5193 histype2 = HIST_COUNT-1;
5194 }
5195 else
5196 {
5197 *end = i;
5198 EMSG(_(e_trailing));
5199 return;
5200 }
5201 }
5202 else
5203 histype2 = histype1;
5204 *end = i;
5205 }
5206 else
5207 end = arg;
5208 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5209 {
5210 EMSG(_(e_trailing));
5211 return;
5212 }
5213
5214 for (; !got_int && histype1 <= histype2; ++histype1)
5215 {
5216 STRCPY(IObuff, "\n # ");
5217 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5218 MSG_PUTS_TITLE(IObuff);
5219 idx = hisidx[histype1];
5220 hist = history[histype1];
5221 j = hisidx1;
5222 k = hisidx2;
5223 if (j < 0)
5224 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5225 if (k < 0)
5226 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5227 if (idx >= 0 && j <= k)
5228 for (i = idx + 1; !got_int; ++i)
5229 {
5230 if (i == hislen)
5231 i = 0;
5232 if (hist[i].hisstr != NULL
5233 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5234 {
5235 msg_putchar('\n');
5236 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5237 hist[i].hisnum);
5238 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5239 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5240 (int)Columns - 10);
5241 else
5242 STRCAT(IObuff, hist[i].hisstr);
5243 msg_outtrans(IObuff);
5244 out_flush();
5245 }
5246 if (i == idx)
5247 break;
5248 }
5249 }
5250}
5251#endif
5252
5253#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5254static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5255static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5256static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5257static int viminfo_add_at_front = FALSE;
5258
5259static int hist_type2char __ARGS((int type, int use_question));
5260
5261/*
5262 * Translate a history type number to the associated character.
5263 */
5264 static int
5265hist_type2char(type, use_question)
5266 int type;
5267 int use_question; /* use '?' instead of '/' */
5268{
5269 if (type == HIST_CMD)
5270 return ':';
5271 if (type == HIST_SEARCH)
5272 {
5273 if (use_question)
5274 return '?';
5275 else
5276 return '/';
5277 }
5278 if (type == HIST_EXPR)
5279 return '=';
5280 return '@';
5281}
5282
5283/*
5284 * Prepare for reading the history from the viminfo file.
5285 * This allocates history arrays to store the read history lines.
5286 */
5287 void
5288prepare_viminfo_history(asklen)
5289 int asklen;
5290{
5291 int i;
5292 int num;
5293 int type;
5294 int len;
5295
5296 init_history();
5297 viminfo_add_at_front = (asklen != 0);
5298 if (asklen > hislen)
5299 asklen = hislen;
5300
5301 for (type = 0; type < HIST_COUNT; ++type)
5302 {
5303 /*
5304 * Count the number of empty spaces in the history list. If there are
5305 * more spaces available than we request, then fill them up.
5306 */
5307 for (i = 0, num = 0; i < hislen; i++)
5308 if (history[type][i].hisstr == NULL)
5309 num++;
5310 len = asklen;
5311 if (num > len)
5312 len = num;
5313 if (len <= 0)
5314 viminfo_history[type] = NULL;
5315 else
5316 viminfo_history[type] =
5317 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5318 if (viminfo_history[type] == NULL)
5319 len = 0;
5320 viminfo_hislen[type] = len;
5321 viminfo_hisidx[type] = 0;
5322 }
5323}
5324
5325/*
5326 * Accept a line from the viminfo, store it in the history array when it's
5327 * new.
5328 */
5329 int
5330read_viminfo_history(virp)
5331 vir_T *virp;
5332{
5333 int type;
5334 long_u len;
5335 char_u *val;
5336 char_u *p;
5337
5338 type = hist_char2type(virp->vir_line[0]);
5339 if (viminfo_hisidx[type] < viminfo_hislen[type])
5340 {
5341 val = viminfo_readstring(virp, 1, TRUE);
5342 if (val != NULL && *val != NUL)
5343 {
5344 if (!in_history(type, val + (type == HIST_SEARCH),
5345 viminfo_add_at_front))
5346 {
5347 /* Need to re-allocate to append the separator byte. */
5348 len = STRLEN(val);
5349 p = lalloc(len + 2, TRUE);
5350 if (p != NULL)
5351 {
5352 if (type == HIST_SEARCH)
5353 {
5354 /* Search entry: Move the separator from the first
5355 * column to after the NUL. */
5356 mch_memmove(p, val + 1, (size_t)len);
5357 p[len] = (*val == ' ' ? NUL : *val);
5358 }
5359 else
5360 {
5361 /* Not a search entry: No separator in the viminfo
5362 * file, add a NUL separator. */
5363 mch_memmove(p, val, (size_t)len + 1);
5364 p[len + 1] = NUL;
5365 }
5366 viminfo_history[type][viminfo_hisidx[type]++] = p;
5367 }
5368 }
5369 }
5370 vim_free(val);
5371 }
5372 return viminfo_readline(virp);
5373}
5374
5375 void
5376finish_viminfo_history()
5377{
5378 int idx;
5379 int i;
5380 int type;
5381
5382 for (type = 0; type < HIST_COUNT; ++type)
5383 {
5384 if (history[type] == NULL)
5385 return;
5386 idx = hisidx[type] + viminfo_hisidx[type];
5387 if (idx >= hislen)
5388 idx -= hislen;
5389 else if (idx < 0)
5390 idx = hislen - 1;
5391 if (viminfo_add_at_front)
5392 hisidx[type] = idx;
5393 else
5394 {
5395 if (hisidx[type] == -1)
5396 hisidx[type] = hislen - 1;
5397 do
5398 {
5399 if (history[type][idx].hisstr != NULL)
5400 break;
5401 if (++idx == hislen)
5402 idx = 0;
5403 } while (idx != hisidx[type]);
5404 if (idx != hisidx[type] && --idx < 0)
5405 idx = hislen - 1;
5406 }
5407 for (i = 0; i < viminfo_hisidx[type]; i++)
5408 {
5409 vim_free(history[type][idx].hisstr);
5410 history[type][idx].hisstr = viminfo_history[type][i];
5411 if (--idx < 0)
5412 idx = hislen - 1;
5413 }
5414 idx += 1;
5415 idx %= hislen;
5416 for (i = 0; i < viminfo_hisidx[type]; i++)
5417 {
5418 history[type][idx++].hisnum = ++hisnum[type];
5419 idx %= hislen;
5420 }
5421 vim_free(viminfo_history[type]);
5422 viminfo_history[type] = NULL;
5423 }
5424}
5425
5426 void
5427write_viminfo_history(fp)
5428 FILE *fp;
5429{
5430 int i;
5431 int type;
5432 int num_saved;
5433 char_u *p;
5434 int c;
5435
5436 init_history();
5437 if (hislen == 0)
5438 return;
5439 for (type = 0; type < HIST_COUNT; ++type)
5440 {
5441 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5442 if (num_saved == 0)
5443 continue;
5444 if (num_saved < 0) /* Use default */
5445 num_saved = hislen;
5446 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5447 type == HIST_CMD ? _("Command Line") :
5448 type == HIST_SEARCH ? _("Search String") :
5449 type == HIST_EXPR ? _("Expression") :
5450 _("Input Line"));
5451 if (num_saved > hislen)
5452 num_saved = hislen;
5453 i = hisidx[type];
5454 if (i >= 0)
5455 while (num_saved--)
5456 {
5457 p = history[type][i].hisstr;
5458 if (p != NULL)
5459 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005460 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 /* For the search history: put the separator in the second
5462 * column; use a space if there isn't one. */
5463 if (type == HIST_SEARCH)
5464 {
5465 c = p[STRLEN(p) + 1];
5466 putc(c == NUL ? ' ' : c, fp);
5467 }
5468 viminfo_writestring(fp, p);
5469 }
5470 if (--i < 0)
5471 i = hislen - 1;
5472 }
5473 }
5474}
5475#endif /* FEAT_VIMINFO */
5476
5477#if defined(FEAT_FKMAP) || defined(PROTO)
5478/*
5479 * Write a character at the current cursor+offset position.
5480 * It is directly written into the command buffer block.
5481 */
5482 void
5483cmd_pchar(c, offset)
5484 int c, offset;
5485{
5486 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5487 {
5488 EMSG(_("E198: cmd_pchar beyond the command length"));
5489 return;
5490 }
5491 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5492 ccline.cmdbuff[ccline.cmdlen] = NUL;
5493}
5494
5495 int
5496cmd_gchar(offset)
5497 int offset;
5498{
5499 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5500 {
5501 /* EMSG(_("cmd_gchar beyond the command length")); */
5502 return NUL;
5503 }
5504 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5505}
5506#endif
5507
5508#if defined(FEAT_CMDWIN) || defined(PROTO)
5509/*
5510 * Open a window on the current command line and history. Allow editing in
5511 * the window. Returns when the window is closed.
5512 * Returns:
5513 * CR if the command is to be executed
5514 * Ctrl_C if it is to be abandoned
5515 * K_IGNORE if editing continues
5516 */
5517 static int
5518ex_window()
5519{
5520 struct cmdline_info save_ccline;
5521 buf_T *old_curbuf = curbuf;
5522 win_T *old_curwin = curwin;
5523 buf_T *bp;
5524 win_T *wp;
5525 int i;
5526 linenr_T lnum;
5527 int histtype;
5528 garray_T winsizes;
5529 char_u typestr[2];
5530 int save_restart_edit = restart_edit;
5531 int save_State = State;
5532 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005533#ifdef FEAT_RIGHTLEFT
5534 int save_cmdmsg_rl = cmdmsg_rl;
5535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
5537 /* Can't do this recursively. Can't do it when typing a password. */
5538 if (cmdwin_type != 0
5539# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5540 || cmdline_star > 0
5541# endif
5542 )
5543 {
5544 beep_flush();
5545 return K_IGNORE;
5546 }
5547
5548 /* Save current window sizes. */
5549 win_size_save(&winsizes);
5550
5551# ifdef FEAT_AUTOCMD
5552 /* Don't execute autocommands while creating the window. */
5553 ++autocmd_block;
5554# endif
5555 /* Create a window for the command-line buffer. */
5556 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5557 {
5558 beep_flush();
5559 return K_IGNORE;
5560 }
5561 cmdwin_type = ccline.cmdfirstc;
5562 if (cmdwin_type == NUL)
5563 cmdwin_type = '-';
5564
5565 /* Create the command-line buffer empty. */
5566 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5567 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5568 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5569 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5570 curbuf->b_p_ma = TRUE;
5571# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005572 curwin->w_p_rl = cmdmsg_rl;
5573 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574# endif
5575# ifdef FEAT_SCROLLBIND
5576 curwin->w_p_scb = FALSE;
5577# endif
5578
5579# ifdef FEAT_AUTOCMD
5580 /* Do execute autocommands for setting the filetype (load syntax). */
5581 --autocmd_block;
5582# endif
5583
Bram Moolenaar46152342005-09-07 21:18:43 +00005584 /* Showing the prompt may have set need_wait_return, reset it. */
5585 need_wait_return = FALSE;
5586
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 histtype = hist_char2type(ccline.cmdfirstc);
5588 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5589 {
5590 if (p_wc == TAB)
5591 {
5592 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5593 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5594 }
5595 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5596 }
5597
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005598 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5599 * sets 'textwidth' to 78). */
5600 curbuf->b_p_tw = 0;
5601
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 /* Fill the buffer with the history. */
5603 init_history();
5604 if (hislen > 0)
5605 {
5606 i = hisidx[histtype];
5607 if (i >= 0)
5608 {
5609 lnum = 0;
5610 do
5611 {
5612 if (++i == hislen)
5613 i = 0;
5614 if (history[histtype][i].hisstr != NULL)
5615 ml_append(lnum++, history[histtype][i].hisstr,
5616 (colnr_T)0, FALSE);
5617 }
5618 while (i != hisidx[histtype]);
5619 }
5620 }
5621
5622 /* Replace the empty last line with the current command-line and put the
5623 * cursor there. */
5624 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5625 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5626 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005627 changed_line_abv_curs();
5628 invalidate_botline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 redraw_later(NOT_VALID);
5630
5631 /* Save the command line info, can be used recursively. */
5632 save_ccline = ccline;
5633 ccline.cmdbuff = NULL;
5634 ccline.cmdprompt = NULL;
5635
5636 /* No Ex mode here! */
5637 exmode_active = 0;
5638
5639 State = NORMAL;
5640# ifdef FEAT_MOUSE
5641 setmouse();
5642# endif
5643
5644# ifdef FEAT_AUTOCMD
5645 /* Trigger CmdwinEnter autocommands. */
5646 typestr[0] = cmdwin_type;
5647 typestr[1] = NUL;
5648 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5649# endif
5650
5651 i = RedrawingDisabled;
5652 RedrawingDisabled = 0;
5653
5654 /*
5655 * Call the main loop until <CR> or CTRL-C is typed.
5656 */
5657 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005658 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 RedrawingDisabled = i;
5661
5662# ifdef FEAT_AUTOCMD
5663 /* Trigger CmdwinLeave autocommands. */
5664 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5665# endif
5666
5667 /* Restore the comand line info. */
5668 ccline = save_ccline;
5669 cmdwin_type = 0;
5670
5671 exmode_active = save_exmode;
5672
5673 /* Safety check: The old window or buffer was deleted: It's a a bug when
5674 * this happens! */
5675 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5676 {
5677 cmdwin_result = Ctrl_C;
5678 EMSG(_("E199: Active window or buffer deleted"));
5679 }
5680 else
5681 {
5682# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5683 /* autocmds may abort script processing */
5684 if (aborting() && cmdwin_result != K_IGNORE)
5685 cmdwin_result = Ctrl_C;
5686# endif
5687 /* Set the new command line from the cmdline buffer. */
5688 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00005689 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 {
Bram Moolenaar46152342005-09-07 21:18:43 +00005691 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
5692
5693 if (histtype == HIST_CMD)
5694 {
5695 /* Execute the command directly. */
5696 ccline.cmdbuff = vim_strsave((char_u *)p);
5697 cmdwin_result = CAR;
5698 }
5699 else
5700 {
5701 /* First need to cancel what we were doing. */
5702 ccline.cmdbuff = NULL;
5703 stuffcharReadbuff(':');
5704 stuffReadbuff((char_u *)p);
5705 stuffcharReadbuff(CAR);
5706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708 else if (cmdwin_result == K_XF2) /* :qa typed */
5709 {
5710 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5711 cmdwin_result = CAR;
5712 }
5713 else
5714 ccline.cmdbuff = vim_strsave(ml_get_curline());
5715 if (ccline.cmdbuff == NULL)
5716 cmdwin_result = Ctrl_C;
5717 else
5718 {
5719 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5720 ccline.cmdbufflen = ccline.cmdlen + 1;
5721 ccline.cmdpos = curwin->w_cursor.col;
5722 if (ccline.cmdpos > ccline.cmdlen)
5723 ccline.cmdpos = ccline.cmdlen;
5724 if (cmdwin_result == K_IGNORE)
5725 {
5726 set_cmdspos_cursor();
5727 redrawcmd();
5728 }
5729 }
5730
5731# ifdef FEAT_AUTOCMD
5732 /* Don't execute autocommands while deleting the window. */
5733 ++autocmd_block;
5734# endif
5735 wp = curwin;
5736 bp = curbuf;
5737 win_goto(old_curwin);
5738 win_close(wp, TRUE);
5739 close_buffer(NULL, bp, DOBUF_WIPE);
5740
5741 /* Restore window sizes. */
5742 win_size_restore(&winsizes);
5743
5744# ifdef FEAT_AUTOCMD
5745 --autocmd_block;
5746# endif
5747 }
5748
5749 ga_clear(&winsizes);
5750 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00005751# ifdef FEAT_RIGHTLEFT
5752 cmdmsg_rl = save_cmdmsg_rl;
5753# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
5755 State = save_State;
5756# ifdef FEAT_MOUSE
5757 setmouse();
5758# endif
5759
5760 return cmdwin_result;
5761}
5762#endif /* FEAT_CMDWIN */
5763
5764/*
5765 * Used for commands that either take a simple command string argument, or:
5766 * cmd << endmarker
5767 * {script}
5768 * endmarker
5769 * Returns a pointer to allocated memory with {script} or NULL.
5770 */
5771 char_u *
5772script_get(eap, cmd)
5773 exarg_T *eap;
5774 char_u *cmd;
5775{
5776 char_u *theline;
5777 char *end_pattern = NULL;
5778 char dot[] = ".";
5779 garray_T ga;
5780
5781 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5782 return NULL;
5783
5784 ga_init2(&ga, 1, 0x400);
5785
5786 if (cmd[2] != NUL)
5787 end_pattern = (char *)skipwhite(cmd + 2);
5788 else
5789 end_pattern = dot;
5790
5791 for (;;)
5792 {
5793 theline = eap->getline(
5794#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005795 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796#endif
5797 NUL, eap->cookie, 0);
5798
5799 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5800 break;
5801
5802 ga_concat(&ga, theline);
5803 ga_append(&ga, '\n');
5804 vim_free(theline);
5805 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005806 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807
5808 return (char_u *)ga.ga_data;
5809}