blob: 4b7a584385fb80da7ea08c9b1608a93f424d6a7c [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;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000261#ifndef BACKSLASH_IN_FILENAME
262 xpc.xp_shell = FALSE;
263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000265#if defined(FEAT_EVAL)
266 if (ccline.input_fn)
267 {
268 xpc.xp_context = ccline.xp_context;
269 xpc.xp_pattern = ccline.cmdbuff;
270 xpc.xp_arg = ccline.xp_arg;
271 }
272#endif
273
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 /*
275 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
276 * doing ":@0" when register 0 doesn't contain a CR.
277 */
278 msg_scroll = FALSE;
279
280 State = CMDLINE;
281
282 if (firstc == '/' || firstc == '?' || firstc == '@')
283 {
284 /* Use ":lmap" mappings for search pattern and input(). */
285 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
286 b_im_ptr = &curbuf->b_p_iminsert;
287 else
288 b_im_ptr = &curbuf->b_p_imsearch;
289 if (*b_im_ptr == B_IMODE_LMAP)
290 State |= LANGMAP;
291#ifdef USE_IM_CONTROL
292 im_set_active(*b_im_ptr == B_IMODE_IM);
293#endif
294 }
295#ifdef USE_IM_CONTROL
296 else if (p_imcmdline)
297 im_set_active(TRUE);
298#endif
299
300#ifdef FEAT_MOUSE
301 setmouse();
302#endif
303#ifdef CURSOR_SHAPE
304 ui_cursor_shape(); /* may show different cursor shape */
305#endif
306
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000307 /* When inside an autocommand for writing "exiting" may be set and
308 * terminal mode set to cooked. Need to set raw mode here then. */
309 settmode(TMODE_RAW);
310
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#ifdef FEAT_CMDHIST
312 init_history();
313 hiscnt = hislen; /* set hiscnt to impossible history value */
314 histype = hist_char2type(firstc);
315#endif
316
317#ifdef FEAT_DIGRAPHS
318 do_digraph(-1); /* init digraph typahead */
319#endif
320
321 /*
322 * Collect the command string, handling editing keys.
323 */
324 for (;;)
325 {
326#ifdef USE_ON_FLY_SCROLL
327 dont_scroll = FALSE; /* allow scrolling here */
328#endif
329 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
330
331 cursorcmd(); /* set the cursor on the right spot */
332 c = safe_vgetc();
333 if (KeyTyped)
334 {
335 some_key_typed = TRUE;
336#ifdef FEAT_RIGHTLEFT
337 if (cmd_hkmap)
338 c = hkmap(c);
339# ifdef FEAT_FKMAP
340 if (cmd_fkmap)
341 c = cmdl_fkmap(c);
342# endif
343 if (cmdmsg_rl && !KeyStuffed)
344 {
345 /* Invert horizontal movements and operations. Only when
346 * typed by the user directly, not when the result of a
347 * mapping. */
348 switch (c)
349 {
350 case K_RIGHT: c = K_LEFT; break;
351 case K_S_RIGHT: c = K_S_LEFT; break;
352 case K_C_RIGHT: c = K_C_LEFT; break;
353 case K_LEFT: c = K_RIGHT; break;
354 case K_S_LEFT: c = K_S_RIGHT; break;
355 case K_C_LEFT: c = K_C_RIGHT; break;
356 }
357 }
358#endif
359 }
360
361 /*
362 * Ignore got_int when CTRL-C was typed here.
363 * Don't ignore it in :global, we really need to break then, e.g., for
364 * ":g/pat/normal /pat" (without the <CR>).
365 * Don't ignore it for the input() function.
366 */
367 if ((c == Ctrl_C
368#ifdef UNIX
369 || c == intr_char
370#endif
371 )
372#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
373 && firstc != '@'
374#endif
375#ifdef FEAT_EVAL
376 && !break_ctrl_c
377#endif
378 && !global_busy)
379 got_int = FALSE;
380
381#ifdef FEAT_CMDHIST
382 /* free old command line when finished moving around in the history
383 * list */
384 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000385 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000386 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 && c != K_PAGEDOWN && c != K_PAGEUP
388 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000389 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
391 {
392 vim_free(lookfor);
393 lookfor = NULL;
394 }
395#endif
396
397 /*
398 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
399 */
400 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
401 c = Ctrl_P;
402
403#ifdef FEAT_WILDMENU
404 /* Special translations for 'wildmenu' */
405 if (did_wild_list && p_wmnu)
406 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000407 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000409 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 c = Ctrl_N;
411 }
412 /* Hitting CR after "emenu Name.": complete submenu */
413 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
414 && ccline.cmdpos > 1
415 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
416 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
417 && (c == '\n' || c == '\r' || c == K_KENTER))
418 c = K_DOWN;
419#endif
420
421 /* free expanded names when finished walking through matches */
422 if (xpc.xp_numfiles != -1
423 && !(c == p_wc && KeyTyped) && c != p_wcm
424 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
425 && c != Ctrl_L)
426 {
427 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
428 did_wild_list = FALSE;
429#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000430 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#endif
432 xpc.xp_context = EXPAND_NOTHING;
433 wim_index = 0;
434#ifdef FEAT_WILDMENU
435 if (p_wmnu && wild_menu_showing != 0)
436 {
437 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000438 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000439
440 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000441 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443 if (wild_menu_showing == WM_SCROLLED)
444 {
445 /* Entered command line, move it up */
446 cmdline_row--;
447 redrawcmd();
448 }
449 else if (save_p_ls != -1)
450 {
451 /* restore 'laststatus' and 'winminheight' */
452 p_ls = save_p_ls;
453 p_wmh = save_p_wmh;
454 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000455 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000457 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 redrawcmd();
459 save_p_ls = -1;
460 }
461 else
462 {
463# ifdef FEAT_VERTSPLIT
464 win_redraw_last_status(topframe);
465# else
466 lastwin->w_redr_status = TRUE;
467# endif
468 redraw_statuslines();
469 }
470 KeyTyped = skt;
471 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000472 if (ccline.input_fn)
473 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 }
475#endif
476 }
477
478#ifdef FEAT_WILDMENU
479 /* Special translations for 'wildmenu' */
480 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
481 {
482 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000483 if (ccline.cmdbuff[ccline.cmdpos - 1] == '.' && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000485 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {
487 /* Hitting <Up>: Remove one submenu name in front of the
488 * cursor */
489 int found = FALSE;
490
491 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
492 i = 0;
493 while (--j > 0)
494 {
495 /* check for start of menu name */
496 if (ccline.cmdbuff[j] == ' '
497 && ccline.cmdbuff[j - 1] != '\\')
498 {
499 i = j + 1;
500 break;
501 }
502 /* check for start of submenu name */
503 if (ccline.cmdbuff[j] == '.'
504 && ccline.cmdbuff[j - 1] != '\\')
505 {
506 if (found)
507 {
508 i = j + 1;
509 break;
510 }
511 else
512 found = TRUE;
513 }
514 }
515 if (i > 0)
516 cmdline_del(i);
517 c = p_wc;
518 xpc.xp_context = EXPAND_NOTHING;
519 }
520 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000521 if ((xpc.xp_context == EXPAND_FILES
522 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {
524 char_u upseg[5];
525
526 upseg[0] = PATHSEP;
527 upseg[1] = '.';
528 upseg[2] = '.';
529 upseg[3] = PATHSEP;
530 upseg[4] = NUL;
531
532 if (ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000533 && c == K_DOWN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 && (ccline.cmdbuff[ccline.cmdpos - 2] != '.'
535 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
536 {
537 /* go down a directory */
538 c = p_wc;
539 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000540 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {
542 /* If in a direct ancestor, strip off one ../ to go down */
543 int found = FALSE;
544
545 j = ccline.cmdpos;
546 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
547 while (--j > i)
548 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000549#ifdef FEAT_MBYTE
550 if (has_mbyte)
551 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 if (vim_ispathsep(ccline.cmdbuff[j]))
554 {
555 found = TRUE;
556 break;
557 }
558 }
559 if (found
560 && ccline.cmdbuff[j - 1] == '.'
561 && ccline.cmdbuff[j - 2] == '.'
562 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
563 {
564 cmdline_del(j - 2);
565 c = p_wc;
566 }
567 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000568 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 {
570 /* go up a directory */
571 int found = FALSE;
572
573 j = ccline.cmdpos - 1;
574 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
575 while (--j > i)
576 {
577#ifdef FEAT_MBYTE
578 if (has_mbyte)
579 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
580#endif
581 if (vim_ispathsep(ccline.cmdbuff[j])
582#ifdef BACKSLASH_IN_FILENAME
583 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
584 == NULL
585#endif
586 )
587 {
588 if (found)
589 {
590 i = j + 1;
591 break;
592 }
593 else
594 found = TRUE;
595 }
596 }
597
598 if (!found)
599 j = i;
600 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
601 j += 4;
602 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
603 && j == i)
604 j += 3;
605 else
606 j = 0;
607 if (j > 0)
608 {
609 /* TODO this is only for DOS/UNIX systems - need to put in
610 * machine-specific stuff here and in upseg init */
611 cmdline_del(j);
612 put_on_cmdline(upseg + 1, 3, FALSE);
613 }
614 else if (ccline.cmdpos > i)
615 cmdline_del(i);
616 c = p_wc;
617 }
618 }
619#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
620 if (p_wmnu
621 && (xpc.xp_context == EXPAND_FILES
622 || xpc.xp_context == EXPAND_MENUNAMES)
623 && (c == K_UP || c == K_DOWN))
624 xpc.xp_context = EXPAND_NOTHING;
625#endif
626
627#endif /* FEAT_WILDMENU */
628
629 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
630 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
631 if (c == Ctrl_BSL)
632 {
633 ++no_mapping;
634 ++allow_keys;
635 c = safe_vgetc();
636 --no_mapping;
637 --allow_keys;
638 /* CTRL-\ e doesn't work when obtaining an expression. */
639 if (c != Ctrl_N && c != Ctrl_G
640 && (c != 'e' || ccline.cmdfirstc == '='))
641 {
642 vungetc(c);
643 c = Ctrl_BSL;
644 }
645#ifdef FEAT_EVAL
646 else if (c == 'e')
647 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000648 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 /*
651 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000652 * Need to save and restore the current command line, to be
653 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 */
655 if (ccline.cmdpos == ccline.cmdlen)
656 new_cmdpos = 99999; /* keep it at the end */
657 else
658 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000659
660 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000662 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 if (c == '=')
664 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000665 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000666 * to avoid nasty things like going to another buffer when
667 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000668 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000669 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000671 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000672 restore_cmdline(&save_ccline);
673
674 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
676 ccline.cmdlen = STRLEN(p);
677 STRCPY(ccline.cmdbuff, p);
678 vim_free(p);
679
680 /* Restore the cursor or use the position set with
681 * set_cmdline_pos(). */
682 if (new_cmdpos > ccline.cmdlen)
683 ccline.cmdpos = ccline.cmdlen;
684 else
685 ccline.cmdpos = new_cmdpos;
686
687 KeyTyped = FALSE; /* Don't do p_wc completion. */
688 redrawcmd();
689 goto cmdline_changed;
690 }
691 }
692 beep_flush();
693 c = ESC;
694 }
695#endif
696 else
697 {
698 if (c == Ctrl_G && p_im && restart_edit == 0)
699 restart_edit = 'a';
700 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
701 in history */
702 goto returncmd; /* back to Normal mode */
703 }
704 }
705
706#ifdef FEAT_CMDWIN
707 if (c == cedit_key || c == K_CMDWIN)
708 {
709 /*
710 * Open a window to edit the command line (and history).
711 */
712 c = ex_window();
713 some_key_typed = TRUE;
714 }
715# ifdef FEAT_DIGRAPHS
716 else
717# endif
718#endif
719#ifdef FEAT_DIGRAPHS
720 c = do_digraph(c);
721#endif
722
723 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
724 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
725 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000726 /* In Ex mode a backslash escapes a newline. */
727 if (exmode_active
728 && c != ESC
729 && ccline.cmdpos > 0
730 && ccline.cmdpos == ccline.cmdlen
731 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000733 if (c == K_KENTER)
734 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000736 else
737 {
738 gotesc = FALSE; /* Might have typed ESC previously, don't
739 truncate the cmdline now. */
740 if (ccheck_abbr(c + ABBR_OFF))
741 goto cmdline_changed;
742 if (!cmd_silent)
743 {
744 windgoto(msg_row, 0);
745 out_flush();
746 }
747 break;
748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 }
750
751 /*
752 * Completion for 'wildchar' or 'wildcharm' key.
753 * - hitting <ESC> twice means: abandon command line.
754 * - wildcard expansion is only done when the 'wildchar' key is really
755 * typed, not when it comes from a macro
756 */
757 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
758 {
759 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
760 {
761 /* if 'wildmode' contains "list" may still need to list */
762 if (xpc.xp_numfiles > 1
763 && !did_wild_list
764 && (wim_flags[wim_index] & WIM_LIST))
765 {
766 (void)showmatches(&xpc, FALSE);
767 redrawcmd();
768 did_wild_list = TRUE;
769 }
770 if (wim_flags[wim_index] & WIM_LONGEST)
771 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
772 else if (wim_flags[wim_index] & WIM_FULL)
773 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
774 else
775 res = OK; /* don't insert 'wildchar' now */
776 }
777 else /* typed p_wc first time */
778 {
779 wim_index = 0;
780 j = ccline.cmdpos;
781 /* if 'wildmode' first contains "longest", get longest
782 * common part */
783 if (wim_flags[0] & WIM_LONGEST)
784 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
785 else
786 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
787
788 /* if interrupted while completing, behave like it failed */
789 if (got_int)
790 {
791 (void)vpeekc(); /* remove <C-C> from input stream */
792 got_int = FALSE; /* don't abandon the command line */
793 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
794#ifdef FEAT_WILDMENU
795 xpc.xp_context = EXPAND_NOTHING;
796#endif
797 goto cmdline_changed;
798 }
799
800 /* when more than one match, and 'wildmode' first contains
801 * "list", or no change and 'wildmode' contains "longest,list",
802 * list all matches */
803 if (res == OK && xpc.xp_numfiles > 1)
804 {
805 /* a "longest" that didn't do anything is skipped (but not
806 * "list:longest") */
807 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
808 wim_index = 1;
809 if ((wim_flags[wim_index] & WIM_LIST)
810#ifdef FEAT_WILDMENU
811 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
812#endif
813 )
814 {
815 if (!(wim_flags[0] & WIM_LONGEST))
816 {
817#ifdef FEAT_WILDMENU
818 int p_wmnu_save = p_wmnu;
819 p_wmnu = 0;
820#endif
821 nextwild(&xpc, WILD_PREV, 0); /* remove match */
822#ifdef FEAT_WILDMENU
823 p_wmnu = p_wmnu_save;
824#endif
825 }
826#ifdef FEAT_WILDMENU
827 (void)showmatches(&xpc, p_wmnu
828 && ((wim_flags[wim_index] & WIM_LIST) == 0));
829#else
830 (void)showmatches(&xpc, FALSE);
831#endif
832 redrawcmd();
833 did_wild_list = TRUE;
834 if (wim_flags[wim_index] & WIM_LONGEST)
835 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
836 else if (wim_flags[wim_index] & WIM_FULL)
837 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
838 }
839 else
840 vim_beep();
841 }
842#ifdef FEAT_WILDMENU
843 else if (xpc.xp_numfiles == -1)
844 xpc.xp_context = EXPAND_NOTHING;
845#endif
846 }
847 if (wim_index < 3)
848 ++wim_index;
849 if (c == ESC)
850 gotesc = TRUE;
851 if (res == OK)
852 goto cmdline_changed;
853 }
854
855 gotesc = FALSE;
856
857 /* <S-Tab> goes to last match, in a clumsy way */
858 if (c == K_S_TAB && KeyTyped)
859 {
860 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
861 && nextwild(&xpc, WILD_PREV, 0) == OK
862 && nextwild(&xpc, WILD_PREV, 0) == OK)
863 goto cmdline_changed;
864 }
865
866 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
867 c = NL;
868
869 do_abbr = TRUE; /* default: check for abbreviation */
870
871 /*
872 * Big switch for a typed command line character.
873 */
874 switch (c)
875 {
876 case K_BS:
877 case Ctrl_H:
878 case K_DEL:
879 case K_KDEL:
880 case Ctrl_W:
881#ifdef FEAT_FKMAP
882 if (cmd_fkmap && c == K_BS)
883 c = K_DEL;
884#endif
885 if (c == K_KDEL)
886 c = K_DEL;
887
888 /*
889 * delete current character is the same as backspace on next
890 * character, except at end of line
891 */
892 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
893 ++ccline.cmdpos;
894#ifdef FEAT_MBYTE
895 if (has_mbyte && c == K_DEL)
896 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
897 ccline.cmdbuff + ccline.cmdpos);
898#endif
899 if (ccline.cmdpos > 0)
900 {
901 char_u *p;
902
903 j = ccline.cmdpos;
904 p = ccline.cmdbuff + j;
905#ifdef FEAT_MBYTE
906 if (has_mbyte)
907 {
908 p = mb_prevptr(ccline.cmdbuff, p);
909 if (c == Ctrl_W)
910 {
911 while (p > ccline.cmdbuff && vim_isspace(*p))
912 p = mb_prevptr(ccline.cmdbuff, p);
913 i = mb_get_class(p);
914 while (p > ccline.cmdbuff && mb_get_class(p) == i)
915 p = mb_prevptr(ccline.cmdbuff, p);
916 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000917 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 }
919 }
920 else
921#endif
922 if (c == Ctrl_W)
923 {
924 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
925 --p;
926 i = vim_iswordc(p[-1]);
927 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
928 && vim_iswordc(p[-1]) == i)
929 --p;
930 }
931 else
932 --p;
933 ccline.cmdpos = (int)(p - ccline.cmdbuff);
934 ccline.cmdlen -= j - ccline.cmdpos;
935 i = ccline.cmdpos;
936 while (i < ccline.cmdlen)
937 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
938
939 /* Truncate at the end, required for multi-byte chars. */
940 ccline.cmdbuff[ccline.cmdlen] = NUL;
941 redrawcmd();
942 }
943 else if (ccline.cmdlen == 0 && c != Ctrl_W
944 && ccline.cmdprompt == NULL && indent == 0)
945 {
946 /* In ex and debug mode it doesn't make sense to return. */
947 if (exmode_active
948#ifdef FEAT_EVAL
949 || ccline.cmdfirstc == '>'
950#endif
951 )
952 goto cmdline_not_changed;
953
954 vim_free(ccline.cmdbuff); /* no commandline to return */
955 ccline.cmdbuff = NULL;
956 if (!cmd_silent)
957 {
958#ifdef FEAT_RIGHTLEFT
959 if (cmdmsg_rl)
960 msg_col = Columns;
961 else
962#endif
963 msg_col = 0;
964 msg_putchar(' '); /* delete ':' */
965 }
966 redraw_cmdline = TRUE;
967 goto returncmd; /* back to cmd mode */
968 }
969 goto cmdline_changed;
970
971 case K_INS:
972 case K_KINS:
973#ifdef FEAT_FKMAP
974 /* if Farsi mode set, we are in reverse insert mode -
975 Do not change the mode */
976 if (cmd_fkmap)
977 beep_flush();
978 else
979#endif
980 ccline.overstrike = !ccline.overstrike;
981#ifdef CURSOR_SHAPE
982 ui_cursor_shape(); /* may show different cursor shape */
983#endif
984 goto cmdline_not_changed;
985
986 case Ctrl_HAT:
987 if (map_to_exists_mode((char_u *)"", LANGMAP))
988 {
989 /* ":lmap" mappings exists, toggle use of mappings. */
990 State ^= LANGMAP;
991#ifdef USE_IM_CONTROL
992 im_set_active(FALSE); /* Disable input method */
993#endif
994 if (b_im_ptr != NULL)
995 {
996 if (State & LANGMAP)
997 *b_im_ptr = B_IMODE_LMAP;
998 else
999 *b_im_ptr = B_IMODE_NONE;
1000 }
1001 }
1002#ifdef USE_IM_CONTROL
1003 else
1004 {
1005 /* There are no ":lmap" mappings, toggle IM. When
1006 * 'imdisable' is set don't try getting the status, it's
1007 * always off. */
1008 if ((p_imdisable && b_im_ptr != NULL)
1009 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1010 {
1011 im_set_active(FALSE); /* Disable input method */
1012 if (b_im_ptr != NULL)
1013 *b_im_ptr = B_IMODE_NONE;
1014 }
1015 else
1016 {
1017 im_set_active(TRUE); /* Enable input method */
1018 if (b_im_ptr != NULL)
1019 *b_im_ptr = B_IMODE_IM;
1020 }
1021 }
1022#endif
1023 if (b_im_ptr != NULL)
1024 {
1025 if (b_im_ptr == &curbuf->b_p_iminsert)
1026 set_iminsert_global();
1027 else
1028 set_imsearch_global();
1029 }
1030#ifdef CURSOR_SHAPE
1031 ui_cursor_shape(); /* may show different cursor shape */
1032#endif
1033#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1034 /* Show/unshow value of 'keymap' in status lines later. */
1035 status_redraw_curbuf();
1036#endif
1037 goto cmdline_not_changed;
1038
1039/* case '@': only in very old vi */
1040 case Ctrl_U:
1041 /* delete all characters left of the cursor */
1042 j = ccline.cmdpos;
1043 ccline.cmdlen -= j;
1044 i = ccline.cmdpos = 0;
1045 while (i < ccline.cmdlen)
1046 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1047 /* Truncate at the end, required for multi-byte chars. */
1048 ccline.cmdbuff[ccline.cmdlen] = NUL;
1049 redrawcmd();
1050 goto cmdline_changed;
1051
1052#ifdef FEAT_CLIPBOARD
1053 case Ctrl_Y:
1054 /* Copy the modeless selection, if there is one. */
1055 if (clip_star.state != SELECT_CLEARED)
1056 {
1057 if (clip_star.state == SELECT_DONE)
1058 clip_copy_modeless_selection(TRUE);
1059 goto cmdline_not_changed;
1060 }
1061 break;
1062#endif
1063
1064 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1065 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001066 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001067 * ":normal" runs out of characters. */
1068 if (exmode_active
1069#ifdef FEAT_EX_EXTRA
1070 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1071#endif
1072 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 goto cmdline_not_changed;
1074
1075 gotesc = TRUE; /* will free ccline.cmdbuff after
1076 putting it in history */
1077 goto returncmd; /* back to cmd mode */
1078
1079 case Ctrl_R: /* insert register */
1080#ifdef USE_ON_FLY_SCROLL
1081 dont_scroll = TRUE; /* disallow scrolling here */
1082#endif
1083 putcmdline('"', TRUE);
1084 ++no_mapping;
1085 i = c = safe_vgetc(); /* CTRL-R <char> */
1086 if (i == Ctrl_O)
1087 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1088 if (i == Ctrl_R)
1089 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1090 --no_mapping;
1091#ifdef FEAT_EVAL
1092 /*
1093 * Insert the result of an expression.
1094 * Need to save the current command line, to be able to enter
1095 * a new one...
1096 */
1097 new_cmdpos = -1;
1098 if (c == '=')
1099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1101 {
1102 beep_flush();
1103 c = ESC;
1104 }
1105 else
1106 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001107 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001109 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
1111 }
1112#endif
1113 if (c != ESC) /* use ESC to cancel inserting register */
1114 {
1115 cmdline_paste(c, i == Ctrl_R);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001116
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001117#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001118 /* When there was a serious error abort getting the
1119 * command line. */
1120 if (aborting())
1121 {
1122 gotesc = TRUE; /* will free ccline.cmdbuff after
1123 putting it in history */
1124 goto returncmd; /* back to cmd mode */
1125 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 KeyTyped = FALSE; /* Don't do p_wc completion. */
1128#ifdef FEAT_EVAL
1129 if (new_cmdpos >= 0)
1130 {
1131 /* set_cmdline_pos() was used */
1132 if (new_cmdpos > ccline.cmdlen)
1133 ccline.cmdpos = ccline.cmdlen;
1134 else
1135 ccline.cmdpos = new_cmdpos;
1136 }
1137#endif
1138 }
1139 redrawcmd();
1140 goto cmdline_changed;
1141
1142 case Ctrl_D:
1143 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1144 break; /* Use ^D as normal char instead */
1145
1146 redrawcmd();
1147 continue; /* don't do incremental search now */
1148
1149 case K_RIGHT:
1150 case K_S_RIGHT:
1151 case K_C_RIGHT:
1152 do
1153 {
1154 if (ccline.cmdpos >= ccline.cmdlen)
1155 break;
1156 i = cmdline_charsize(ccline.cmdpos);
1157 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1158 break;
1159 ccline.cmdspos += i;
1160#ifdef FEAT_MBYTE
1161 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001162 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 + ccline.cmdpos);
1164 else
1165#endif
1166 ++ccline.cmdpos;
1167 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001168 while ((c == K_S_RIGHT || c == K_C_RIGHT
1169 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1171#ifdef FEAT_MBYTE
1172 if (has_mbyte)
1173 set_cmdspos_cursor();
1174#endif
1175 goto cmdline_not_changed;
1176
1177 case K_LEFT:
1178 case K_S_LEFT:
1179 case K_C_LEFT:
1180 do
1181 {
1182 if (ccline.cmdpos == 0)
1183 break;
1184 --ccline.cmdpos;
1185#ifdef FEAT_MBYTE
1186 if (has_mbyte) /* move to first byte of char */
1187 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1188 ccline.cmdbuff + ccline.cmdpos);
1189#endif
1190 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1191 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001192 while ((c == K_S_LEFT || c == K_C_LEFT
1193 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1195#ifdef FEAT_MBYTE
1196 if (has_mbyte)
1197 set_cmdspos_cursor();
1198#endif
1199 goto cmdline_not_changed;
1200
1201 case K_IGNORE:
1202 goto cmdline_not_changed; /* Ignore mouse */
1203
Bram Moolenaar4770d092006-01-12 23:22:24 +00001204#ifdef FEAT_GUI_W32
1205 /* On Win32 ignore <M-F4>, we get it when closing the window was
1206 * cancelled. */
1207 case K_F4:
1208 if (mod_mask == MOD_MASK_ALT)
1209 {
1210 redrawcmd(); /* somehow the cmdline is cleared */
1211 goto cmdline_not_changed;
1212 }
1213 break;
1214#endif
1215
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216#ifdef FEAT_MOUSE
1217 case K_MIDDLEDRAG:
1218 case K_MIDDLERELEASE:
1219 goto cmdline_not_changed; /* Ignore mouse */
1220
1221 case K_MIDDLEMOUSE:
1222# ifdef FEAT_GUI
1223 /* When GUI is active, also paste when 'mouse' is empty */
1224 if (!gui.in_use)
1225# endif
1226 if (!mouse_has(MOUSE_COMMAND))
1227 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001228# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (clip_star.available)
1230 cmdline_paste('*', TRUE);
1231 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 cmdline_paste(0, TRUE);
1234 redrawcmd();
1235 goto cmdline_changed;
1236
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001237# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 case K_DROP:
1239 cmdline_paste('~', TRUE);
1240 redrawcmd();
1241 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243
1244 case K_LEFTDRAG:
1245 case K_LEFTRELEASE:
1246 case K_RIGHTDRAG:
1247 case K_RIGHTRELEASE:
1248 /* Ignore drag and release events when the button-down wasn't
1249 * seen before. */
1250 if (ignore_drag_release)
1251 goto cmdline_not_changed;
1252 /* FALLTHROUGH */
1253 case K_LEFTMOUSE:
1254 case K_RIGHTMOUSE:
1255 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1256 ignore_drag_release = TRUE;
1257 else
1258 ignore_drag_release = FALSE;
1259# ifdef FEAT_GUI
1260 /* When GUI is active, also move when 'mouse' is empty */
1261 if (!gui.in_use)
1262# endif
1263 if (!mouse_has(MOUSE_COMMAND))
1264 goto cmdline_not_changed; /* Ignore mouse */
1265# ifdef FEAT_CLIPBOARD
1266 if (mouse_row < cmdline_row && clip_star.available)
1267 {
1268 int button, is_click, is_drag;
1269
1270 /*
1271 * Handle modeless selection.
1272 */
1273 button = get_mouse_button(KEY2TERMCAP1(c),
1274 &is_click, &is_drag);
1275 if (mouse_model_popup() && button == MOUSE_LEFT
1276 && (mod_mask & MOD_MASK_SHIFT))
1277 {
1278 /* Translate shift-left to right button. */
1279 button = MOUSE_RIGHT;
1280 mod_mask &= ~MOD_MASK_SHIFT;
1281 }
1282 clip_modeless(button, is_click, is_drag);
1283 goto cmdline_not_changed;
1284 }
1285# endif
1286
1287 set_cmdspos();
1288 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1289 ++ccline.cmdpos)
1290 {
1291 i = cmdline_charsize(ccline.cmdpos);
1292 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1293 && mouse_col < ccline.cmdspos % Columns + i)
1294 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001295# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if (has_mbyte)
1297 {
1298 /* Count ">" for double-wide char that doesn't fit. */
1299 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001300 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 + ccline.cmdpos) - 1;
1302 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 ccline.cmdspos += i;
1305 }
1306 goto cmdline_not_changed;
1307
1308 /* Mouse scroll wheel: ignored here */
1309 case K_MOUSEDOWN:
1310 case K_MOUSEUP:
1311 /* Alternate buttons ignored here */
1312 case K_X1MOUSE:
1313 case K_X1DRAG:
1314 case K_X1RELEASE:
1315 case K_X2MOUSE:
1316 case K_X2DRAG:
1317 case K_X2RELEASE:
1318 goto cmdline_not_changed;
1319
1320#endif /* FEAT_MOUSE */
1321
1322#ifdef FEAT_GUI
1323 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1324 case K_LEFTRELEASE_NM:
1325 goto cmdline_not_changed;
1326
1327 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001328 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
1330 gui_do_scroll();
1331 redrawcmd();
1332 }
1333 goto cmdline_not_changed;
1334
1335 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001336 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 {
1338 gui_do_horiz_scroll();
1339 redrawcmd();
1340 }
1341 goto cmdline_not_changed;
1342#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001343#ifdef FEAT_GUI_TABLINE
1344 case K_TABLINE:
1345 case K_TABMENU:
1346 /* Don't want to change any tabs here. Make sure the same tab
1347 * is still selected. */
1348 if (gui_use_tabline())
1349 gui_mch_set_curtab(tabpage_index(curtab));
1350 goto cmdline_not_changed;
1351#endif
1352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 case K_SELECT: /* end of Select mode mapping - ignore */
1354 goto cmdline_not_changed;
1355
1356 case Ctrl_B: /* begin of command line */
1357 case K_HOME:
1358 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 case K_S_HOME:
1360 case K_C_HOME:
1361 ccline.cmdpos = 0;
1362 set_cmdspos();
1363 goto cmdline_not_changed;
1364
1365 case Ctrl_E: /* end of command line */
1366 case K_END:
1367 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 case K_S_END:
1369 case K_C_END:
1370 ccline.cmdpos = ccline.cmdlen;
1371 set_cmdspos_cursor();
1372 goto cmdline_not_changed;
1373
1374 case Ctrl_A: /* all matches */
1375 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1376 break;
1377 goto cmdline_changed;
1378
1379 case Ctrl_L: /* longest common part */
1380 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1381 break;
1382 goto cmdline_changed;
1383
1384 case Ctrl_N: /* next match */
1385 case Ctrl_P: /* previous match */
1386 if (xpc.xp_numfiles > 0)
1387 {
1388 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1389 == FAIL)
1390 break;
1391 goto cmdline_changed;
1392 }
1393
1394#ifdef FEAT_CMDHIST
1395 case K_UP:
1396 case K_DOWN:
1397 case K_S_UP:
1398 case K_S_DOWN:
1399 case K_PAGEUP:
1400 case K_KPAGEUP:
1401 case K_PAGEDOWN:
1402 case K_KPAGEDOWN:
1403 if (hislen == 0 || firstc == NUL) /* no history */
1404 goto cmdline_not_changed;
1405
1406 i = hiscnt;
1407
1408 /* save current command string so it can be restored later */
1409 if (lookfor == NULL)
1410 {
1411 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1412 goto cmdline_not_changed;
1413 lookfor[ccline.cmdpos] = NUL;
1414 }
1415
1416 j = (int)STRLEN(lookfor);
1417 for (;;)
1418 {
1419 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001420 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001421 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 {
1423 if (hiscnt == hislen) /* first time */
1424 hiscnt = hisidx[histype];
1425 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1426 hiscnt = hislen - 1;
1427 else if (hiscnt != hisidx[histype] + 1)
1428 --hiscnt;
1429 else /* at top of list */
1430 {
1431 hiscnt = i;
1432 break;
1433 }
1434 }
1435 else /* one step forwards */
1436 {
1437 /* on last entry, clear the line */
1438 if (hiscnt == hisidx[histype])
1439 {
1440 hiscnt = hislen;
1441 break;
1442 }
1443
1444 /* not on a history line, nothing to do */
1445 if (hiscnt == hislen)
1446 break;
1447 if (hiscnt == hislen - 1) /* wrap around */
1448 hiscnt = 0;
1449 else
1450 ++hiscnt;
1451 }
1452 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1453 {
1454 hiscnt = i;
1455 break;
1456 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001457 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001458 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 || STRNCMP(history[histype][hiscnt].hisstr,
1460 lookfor, (size_t)j) == 0)
1461 break;
1462 }
1463
1464 if (hiscnt != i) /* jumped to other entry */
1465 {
1466 char_u *p;
1467 int len;
1468 int old_firstc;
1469
1470 vim_free(ccline.cmdbuff);
1471 if (hiscnt == hislen)
1472 p = lookfor; /* back to the old one */
1473 else
1474 p = history[histype][hiscnt].hisstr;
1475
1476 if (histype == HIST_SEARCH
1477 && p != lookfor
1478 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1479 {
1480 /* Correct for the separator character used when
1481 * adding the history entry vs the one used now.
1482 * First loop: count length.
1483 * Second loop: copy the characters. */
1484 for (i = 0; i <= 1; ++i)
1485 {
1486 len = 0;
1487 for (j = 0; p[j] != NUL; ++j)
1488 {
1489 /* Replace old sep with new sep, unless it is
1490 * escaped. */
1491 if (p[j] == old_firstc
1492 && (j == 0 || p[j - 1] != '\\'))
1493 {
1494 if (i > 0)
1495 ccline.cmdbuff[len] = firstc;
1496 }
1497 else
1498 {
1499 /* Escape new sep, unless it is already
1500 * escaped. */
1501 if (p[j] == firstc
1502 && (j == 0 || p[j - 1] != '\\'))
1503 {
1504 if (i > 0)
1505 ccline.cmdbuff[len] = '\\';
1506 ++len;
1507 }
1508 if (i > 0)
1509 ccline.cmdbuff[len] = p[j];
1510 }
1511 ++len;
1512 }
1513 if (i == 0)
1514 {
1515 alloc_cmdbuff(len);
1516 if (ccline.cmdbuff == NULL)
1517 goto returncmd;
1518 }
1519 }
1520 ccline.cmdbuff[len] = NUL;
1521 }
1522 else
1523 {
1524 alloc_cmdbuff((int)STRLEN(p));
1525 if (ccline.cmdbuff == NULL)
1526 goto returncmd;
1527 STRCPY(ccline.cmdbuff, p);
1528 }
1529
1530 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1531 redrawcmd();
1532 goto cmdline_changed;
1533 }
1534 beep_flush();
1535 goto cmdline_not_changed;
1536#endif
1537
1538 case Ctrl_V:
1539 case Ctrl_Q:
1540#ifdef FEAT_MOUSE
1541 ignore_drag_release = TRUE;
1542#endif
1543 putcmdline('^', TRUE);
1544 c = get_literal(); /* get next (two) character(s) */
1545 do_abbr = FALSE; /* don't do abbreviation now */
1546#ifdef FEAT_MBYTE
1547 /* may need to remove ^ when composing char was typed */
1548 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1549 {
1550 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1551 msg_putchar(' ');
1552 cursorcmd();
1553 }
1554#endif
1555 break;
1556
1557#ifdef FEAT_DIGRAPHS
1558 case Ctrl_K:
1559#ifdef FEAT_MOUSE
1560 ignore_drag_release = TRUE;
1561#endif
1562 putcmdline('?', TRUE);
1563#ifdef USE_ON_FLY_SCROLL
1564 dont_scroll = TRUE; /* disallow scrolling here */
1565#endif
1566 c = get_digraph(TRUE);
1567 if (c != NUL)
1568 break;
1569
1570 redrawcmd();
1571 goto cmdline_not_changed;
1572#endif /* FEAT_DIGRAPHS */
1573
1574#ifdef FEAT_RIGHTLEFT
1575 case Ctrl__: /* CTRL-_: switch language mode */
1576 if (!p_ari)
1577 break;
1578#ifdef FEAT_FKMAP
1579 if (p_altkeymap)
1580 {
1581 cmd_fkmap = !cmd_fkmap;
1582 if (cmd_fkmap) /* in Farsi always in Insert mode */
1583 ccline.overstrike = FALSE;
1584 }
1585 else /* Hebrew is default */
1586#endif
1587 cmd_hkmap = !cmd_hkmap;
1588 goto cmdline_not_changed;
1589#endif
1590
1591 default:
1592#ifdef UNIX
1593 if (c == intr_char)
1594 {
1595 gotesc = TRUE; /* will free ccline.cmdbuff after
1596 putting it in history */
1597 goto returncmd; /* back to Normal mode */
1598 }
1599#endif
1600 /*
1601 * Normal character with no special meaning. Just set mod_mask
1602 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1603 * the string <S-Space>. This should only happen after ^V.
1604 */
1605 if (!IS_SPECIAL(c))
1606 mod_mask = 0x0;
1607 break;
1608 }
1609 /*
1610 * End of switch on command line character.
1611 * We come here if we have a normal character.
1612 */
1613
1614 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1615#ifdef FEAT_MBYTE
1616 /* Add ABBR_OFF for characters above 0x100, this is
1617 * what check_abbr() expects. */
1618 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1619#endif
1620 c))
1621 goto cmdline_changed;
1622
1623 /*
1624 * put the character in the command line
1625 */
1626 if (IS_SPECIAL(c) || mod_mask != 0)
1627 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1628 else
1629 {
1630#ifdef FEAT_MBYTE
1631 if (has_mbyte)
1632 {
1633 j = (*mb_char2bytes)(c, IObuff);
1634 IObuff[j] = NUL; /* exclude composing chars */
1635 put_on_cmdline(IObuff, j, TRUE);
1636 }
1637 else
1638#endif
1639 {
1640 IObuff[0] = c;
1641 put_on_cmdline(IObuff, 1, TRUE);
1642 }
1643 }
1644 goto cmdline_changed;
1645
1646/*
1647 * This part implements incremental searches for "/" and "?"
1648 * Jump to cmdline_not_changed when a character has been read but the command
1649 * line did not change. Then we only search and redraw if something changed in
1650 * the past.
1651 * Jump to cmdline_changed when the command line did change.
1652 * (Sorry for the goto's, I know it is ugly).
1653 */
1654cmdline_not_changed:
1655#ifdef FEAT_SEARCH_EXTRA
1656 if (!incsearch_postponed)
1657 continue;
1658#endif
1659
1660cmdline_changed:
1661#ifdef FEAT_SEARCH_EXTRA
1662 /*
1663 * 'incsearch' highlighting.
1664 */
1665 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1666 {
1667 /* if there is a character waiting, search and redraw later */
1668 if (char_avail())
1669 {
1670 incsearch_postponed = TRUE;
1671 continue;
1672 }
1673 incsearch_postponed = FALSE;
1674 curwin->w_cursor = old_cursor; /* start at old position */
1675
1676 /* If there is no command line, don't do anything */
1677 if (ccline.cmdlen == 0)
1678 i = 0;
1679 else
1680 {
1681 cursor_off(); /* so the user knows we're busy */
1682 out_flush();
1683 ++emsg_off; /* So it doesn't beep if bad expr */
1684 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1685 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1686 --emsg_off;
1687 /* if interrupted while searching, behave like it failed */
1688 if (got_int)
1689 {
1690 (void)vpeekc(); /* remove <C-C> from input stream */
1691 got_int = FALSE; /* don't abandon the command line */
1692 i = 0;
1693 }
1694 else if (char_avail())
1695 /* cancelled searching because a char was typed */
1696 incsearch_postponed = TRUE;
1697 }
1698 if (i)
1699 highlight_match = TRUE; /* highlight position */
1700 else
1701 highlight_match = FALSE; /* remove highlight */
1702
1703 /* first restore the old curwin values, so the screen is
1704 * positioned in the same way as the actual search command */
1705 curwin->w_leftcol = old_leftcol;
1706 curwin->w_topline = old_topline;
1707# ifdef FEAT_DIFF
1708 curwin->w_topfill = old_topfill;
1709# endif
1710 curwin->w_botline = old_botline;
1711 changed_cline_bef_curs();
1712 update_topline();
1713
1714 if (i != 0)
1715 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001716 pos_T save_pos = curwin->w_cursor;
1717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 /*
1719 * First move cursor to end of match, then to start. This
1720 * moves the whole match onto the screen when 'nowrap' is set.
1721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 curwin->w_cursor.lnum += search_match_lines;
1723 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001724 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1725 {
1726 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1727 coladvance((colnr_T)MAXCOL);
1728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 validate_cursor();
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001730 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 }
1732 validate_cursor();
1733
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001734 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 update_screen(NOT_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001736 restore_cmdline(&save_ccline);
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 msg_starthere();
1739 redrawcmdline();
1740 did_incsearch = TRUE;
1741 }
1742#else /* FEAT_SEARCH_EXTRA */
1743 ;
1744#endif
1745
1746#ifdef FEAT_RIGHTLEFT
1747 if (cmdmsg_rl
1748# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001749 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750# endif
1751 )
1752 /* Always redraw the whole command line to fix shaping and
1753 * right-left typing. Not efficient, but it works. */
1754 redrawcmd();
1755#endif
1756 }
1757
1758returncmd:
1759
1760#ifdef FEAT_RIGHTLEFT
1761 cmdmsg_rl = FALSE;
1762#endif
1763
1764#ifdef FEAT_FKMAP
1765 cmd_fkmap = 0;
1766#endif
1767
1768 ExpandCleanup(&xpc);
1769
1770#ifdef FEAT_SEARCH_EXTRA
1771 if (did_incsearch)
1772 {
1773 curwin->w_cursor = old_cursor;
1774 curwin->w_curswant = old_curswant;
1775 curwin->w_leftcol = old_leftcol;
1776 curwin->w_topline = old_topline;
1777# ifdef FEAT_DIFF
1778 curwin->w_topfill = old_topfill;
1779# endif
1780 curwin->w_botline = old_botline;
1781 highlight_match = FALSE;
1782 validate_cursor(); /* needed for TAB */
1783 redraw_later(NOT_VALID);
1784 }
1785#endif
1786
1787 if (ccline.cmdbuff != NULL)
1788 {
1789 /*
1790 * Put line in history buffer (":" and "=" only when it was typed).
1791 */
1792#ifdef FEAT_CMDHIST
1793 if (ccline.cmdlen && firstc != NUL
1794 && (some_key_typed || histype == HIST_SEARCH))
1795 {
1796 add_to_history(histype, ccline.cmdbuff, TRUE,
1797 histype == HIST_SEARCH ? firstc : NUL);
1798 if (firstc == ':')
1799 {
1800 vim_free(new_last_cmdline);
1801 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1802 }
1803 }
1804#endif
1805
1806 if (gotesc) /* abandon command line */
1807 {
1808 vim_free(ccline.cmdbuff);
1809 ccline.cmdbuff = NULL;
1810 if (msg_scrolled == 0)
1811 compute_cmdrow();
1812 MSG("");
1813 redraw_cmdline = TRUE;
1814 }
1815 }
1816
1817 /*
1818 * If the screen was shifted up, redraw the whole screen (later).
1819 * If the line is too long, clear it, so ruler and shown command do
1820 * not get printed in the middle of it.
1821 */
1822 msg_check();
1823 msg_scroll = save_msg_scroll;
1824 redir_off = FALSE;
1825
1826 /* When the command line was typed, no need for a wait-return prompt. */
1827 if (some_key_typed)
1828 need_wait_return = FALSE;
1829
1830 State = save_State;
1831#ifdef USE_IM_CONTROL
1832 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1833 im_save_status(b_im_ptr);
1834 im_set_active(FALSE);
1835#endif
1836#ifdef FEAT_MOUSE
1837 setmouse();
1838#endif
1839#ifdef CURSOR_SHAPE
1840 ui_cursor_shape(); /* may show different cursor shape */
1841#endif
1842
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001843 {
1844 char_u *p = ccline.cmdbuff;
1845
1846 /* Make ccline empty, getcmdline() may try to use it. */
1847 ccline.cmdbuff = NULL;
1848 return p;
1849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850}
1851
1852#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1853/*
1854 * Get a command line with a prompt.
1855 * This is prepared to be called recursively from getcmdline() (e.g. by
1856 * f_input() when evaluating an expression from CTRL-R =).
1857 * Returns the command line in allocated memory, or NULL.
1858 */
1859 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001860getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 int firstc;
1862 char_u *prompt; /* command line prompt */
1863 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001864 int xp_context; /* type of expansion */
1865 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 char_u *s;
1868 struct cmdline_info save_ccline;
1869 int msg_col_save = msg_col;
1870
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001871 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 ccline.cmdprompt = prompt;
1873 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001874# ifdef FEAT_EVAL
1875 ccline.xp_context = xp_context;
1876 ccline.xp_arg = xp_arg;
1877 ccline.input_fn = (firstc == '@');
1878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001880 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 /* Restore msg_col, the prompt from input() may have changed it. */
1882 msg_col = msg_col_save;
1883
1884 return s;
1885}
1886#endif
1887
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001888/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001889 * Return TRUE when the text must not be changed and we can't switch to
1890 * another window or buffer. Used when editing the command line, evaluating
1891 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001892 */
1893 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001894text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001895{
1896#ifdef FEAT_CMDWIN
1897 if (cmdwin_type != 0)
1898 return TRUE;
1899#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001900 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001901}
1902
1903/*
1904 * Give an error message for a command that isn't allowed while the cmdline
1905 * window is open or editing the cmdline in another way.
1906 */
1907 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001908text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001909{
1910#ifdef FEAT_CMDWIN
1911 if (cmdwin_type != 0)
1912 EMSG(_(e_cmdwin));
1913 else
1914#endif
1915 EMSG(_(e_secure));
1916}
1917
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 static int
1919cmdline_charsize(idx)
1920 int idx;
1921{
1922#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1923 if (cmdline_star > 0) /* showing '*', always 1 position */
1924 return 1;
1925#endif
1926 return ptr2cells(ccline.cmdbuff + idx);
1927}
1928
1929/*
1930 * Compute the offset of the cursor on the command line for the prompt and
1931 * indent.
1932 */
1933 static void
1934set_cmdspos()
1935{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001936 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 ccline.cmdspos = 1 + ccline.cmdindent;
1938 else
1939 ccline.cmdspos = 0 + ccline.cmdindent;
1940}
1941
1942/*
1943 * Compute the screen position for the cursor on the command line.
1944 */
1945 static void
1946set_cmdspos_cursor()
1947{
1948 int i, m, c;
1949
1950 set_cmdspos();
1951 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001954 if (m < 0) /* overflow, Columns or Rows at weird value */
1955 m = MAXCOL;
1956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 else
1958 m = MAXCOL;
1959 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1960 {
1961 c = cmdline_charsize(i);
1962#ifdef FEAT_MBYTE
1963 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1964 if (has_mbyte)
1965 correct_cmdspos(i, c);
1966#endif
1967 /* If the cmdline doesn't fit, put cursor on last visible char. */
1968 if ((ccline.cmdspos += c) >= m)
1969 {
1970 ccline.cmdpos = i - 1;
1971 ccline.cmdspos -= c;
1972 break;
1973 }
1974#ifdef FEAT_MBYTE
1975 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001976 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977#endif
1978 }
1979}
1980
1981#ifdef FEAT_MBYTE
1982/*
1983 * Check if the character at "idx", which is "cells" wide, is a multi-byte
1984 * character that doesn't fit, so that a ">" must be displayed.
1985 */
1986 static void
1987correct_cmdspos(idx, cells)
1988 int idx;
1989 int cells;
1990{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001991 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
1993 && ccline.cmdspos % Columns + cells > Columns)
1994 ccline.cmdspos++;
1995}
1996#endif
1997
1998/*
1999 * Get an Ex command line for the ":" command.
2000 */
2001/* ARGSUSED */
2002 char_u *
2003getexline(c, dummy, indent)
2004 int c; /* normally ':', NUL for ":append" */
2005 void *dummy; /* cookie not used */
2006 int indent; /* indent for inside conditionals */
2007{
2008 /* When executing a register, remove ':' that's in front of each line. */
2009 if (exec_from_reg && vpeekc() == ':')
2010 (void)vgetc();
2011 return getcmdline(c, 1L, indent);
2012}
2013
2014/*
2015 * Get an Ex command line for Ex mode.
2016 * In Ex mode we only use the OS supplied line editing features and no
2017 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002018 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 */
2020/* ARGSUSED */
2021 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002022getexmodeline(promptc, dummy, indent)
2023 int promptc; /* normally ':', NUL for ":append" and '?' for
2024 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 void *dummy; /* cookie not used */
2026 int indent; /* indent for inside conditionals */
2027{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002028 garray_T line_ga;
2029 char_u *pend;
2030 int startcol = 0;
2031 int c1;
2032 int escaped = FALSE; /* CTRL-V typed */
2033 int vcol = 0;
2034 char_u *p;
2035 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
2037 /* Switch cursor on now. This avoids that it happens after the "\n", which
2038 * confuses the system function that computes tabstops. */
2039 cursor_on();
2040
2041 /* always start in column 0; write a newline if necessary */
2042 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002043 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002045 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002047 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002048 if (p_prompt)
2049 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 while (indent-- > 0)
2051 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054
2055 ga_init2(&line_ga, 1, 30);
2056
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002057 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002058 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002059 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002060 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002061 while (indent >= 8)
2062 {
2063 ga_append(&line_ga, TAB);
2064 msg_puts((char_u *)" ");
2065 indent -= 8;
2066 }
2067 while (indent-- > 0)
2068 {
2069 ga_append(&line_ga, ' ');
2070 msg_putchar(' ');
2071 }
2072 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002073 ++no_mapping;
2074 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 /*
2077 * Get the line, one character at a time.
2078 */
2079 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002080 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
2082 if (ga_grow(&line_ga, 40) == FAIL)
2083 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002084 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002086 /* Get one character at a time. Don't use inchar(), it can't handle
2087 * special characters. */
2088 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002091 * Handle line editing.
2092 * Previously this was left to the system, putting the terminal in
2093 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002095 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002097 msg_putchar('\n');
2098 break;
2099 }
2100
2101 if (!escaped)
2102 {
2103 /* CR typed means "enter", which is NL */
2104 if (c1 == '\r')
2105 c1 = '\n';
2106
2107 if (c1 == BS || c1 == K_BS
2108 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110 if (line_ga.ga_len > 0)
2111 {
2112 --line_ga.ga_len;
2113 goto redraw;
2114 }
2115 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002118 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002120 msg_col = startcol;
2121 msg_clr_eos();
2122 line_ga.ga_len = 0;
2123 continue;
2124 }
2125
2126 if (c1 == Ctrl_T)
2127 {
2128 p = (char_u *)line_ga.ga_data;
2129 p[line_ga.ga_len] = NUL;
2130 indent = get_indent_str(p, 8);
2131 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2132add_indent:
2133 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002135 char_u *s = skipwhite(p);
2136
2137 ga_grow(&line_ga, 1);
2138 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2139 *s = ' ';
2140 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002142redraw:
2143 /* redraw the line */
2144 msg_col = startcol;
2145 windgoto(msg_row, msg_col);
2146 vcol = 0;
2147 for (p = (char_u *)line_ga.ga_data;
2148 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002150 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002152 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002154 msg_putchar(' ');
2155 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002157 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002159 msg_outtrans_len(p, 1);
2160 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 }
2162 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002163 msg_clr_eos();
2164 continue;
2165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002167 if (c1 == Ctrl_D)
2168 {
2169 /* Delete one shiftwidth. */
2170 p = (char_u *)line_ga.ga_data;
2171 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002173 if (prev_char == '^')
2174 ex_keep_indent = TRUE;
2175 indent = 0;
2176 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 }
2178 else
2179 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002180 p[line_ga.ga_len] = NUL;
2181 indent = get_indent_str(p, 8);
2182 --indent;
2183 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 while (get_indent_str(p, 8) > indent)
2186 {
2187 char_u *s = skipwhite(p);
2188
2189 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2190 --line_ga.ga_len;
2191 }
2192 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002194
2195 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2196 {
2197 escaped = TRUE;
2198 continue;
2199 }
2200
2201 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2202 if (IS_SPECIAL(c1))
2203 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002205
2206 if (IS_SPECIAL(c1))
2207 c1 = '?';
2208 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2209 prev_char = c1;
2210 if (c1 == '\n')
2211 msg_putchar('\n');
2212 else if (c1 == TAB)
2213 {
2214 /* Don't use chartabsize(), 'ts' can be different */
2215 do
2216 {
2217 msg_putchar(' ');
2218 } while (++vcol % 8);
2219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 msg_outtrans_len(
2223 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2224 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002226 ++line_ga.ga_len;
2227 escaped = FALSE;
2228
2229 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002230 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002231
2232 /* we are done when a NL is entered, but not when it comes after a
2233 * backslash */
2234 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2235 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002238 --pend;
2239 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002240 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 }
2242 }
2243
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002244 --no_mapping;
2245 --allow_keys;
2246
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 /* make following messages go to the next line */
2248 msg_didout = FALSE;
2249 msg_col = 0;
2250 if (msg_row < Rows - 1)
2251 ++msg_row;
2252 emsg_on_display = FALSE; /* don't want ui_delay() */
2253
2254 if (got_int)
2255 ga_clear(&line_ga);
2256
2257 return (char_u *)line_ga.ga_data;
2258}
2259
Bram Moolenaare344bea2005-09-01 20:46:49 +00002260# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2261 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262/*
2263 * Return TRUE if ccline.overstrike is on.
2264 */
2265 int
2266cmdline_overstrike()
2267{
2268 return ccline.overstrike;
2269}
2270
2271/*
2272 * Return TRUE if the cursor is at the end of the cmdline.
2273 */
2274 int
2275cmdline_at_end()
2276{
2277 return (ccline.cmdpos >= ccline.cmdlen);
2278}
2279#endif
2280
Bram Moolenaar9372a112005-12-06 19:59:18 +00002281#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282/*
2283 * Return the virtual column number at the current cursor position.
2284 * This is used by the IM code to obtain the start of the preedit string.
2285 */
2286 colnr_T
2287cmdline_getvcol_cursor()
2288{
2289 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2290 return MAXCOL;
2291
2292# ifdef FEAT_MBYTE
2293 if (has_mbyte)
2294 {
2295 colnr_T col;
2296 int i = 0;
2297
2298 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002299 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301 return col;
2302 }
2303 else
2304# endif
2305 return ccline.cmdpos;
2306}
2307#endif
2308
2309#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2310/*
2311 * If part of the command line is an IM preedit string, redraw it with
2312 * IM feedback attributes. The cursor position is restored after drawing.
2313 */
2314 static void
2315redrawcmd_preedit()
2316{
2317 if ((State & CMDLINE)
2318 && xic != NULL
2319 && im_get_status()
2320 && !p_imdisable
2321 && im_is_preediting())
2322 {
2323 int cmdpos = 0;
2324 int cmdspos;
2325 int old_row;
2326 int old_col;
2327 colnr_T col;
2328
2329 old_row = msg_row;
2330 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002331 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332
2333# ifdef FEAT_MBYTE
2334 if (has_mbyte)
2335 {
2336 for (col = 0; col < preedit_start_col
2337 && cmdpos < ccline.cmdlen; ++col)
2338 {
2339 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002340 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342 }
2343 else
2344# endif
2345 {
2346 cmdspos += preedit_start_col;
2347 cmdpos += preedit_start_col;
2348 }
2349
2350 msg_row = cmdline_row + (cmdspos / (int)Columns);
2351 msg_col = cmdspos % (int)Columns;
2352 if (msg_row >= Rows)
2353 msg_row = Rows - 1;
2354
2355 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2356 {
2357 int char_len;
2358 int char_attr;
2359
2360 char_attr = im_get_feedback_attr(col);
2361 if (char_attr < 0)
2362 break; /* end of preedit string */
2363
2364# ifdef FEAT_MBYTE
2365 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002366 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 else
2368# endif
2369 char_len = 1;
2370
2371 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2372 cmdpos += char_len;
2373 }
2374
2375 msg_row = old_row;
2376 msg_col = old_col;
2377 }
2378}
2379#endif /* FEAT_XIM && FEAT_GUI_GTK */
2380
2381/*
2382 * Allocate a new command line buffer.
2383 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2384 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2385 */
2386 static void
2387alloc_cmdbuff(len)
2388 int len;
2389{
2390 /*
2391 * give some extra space to avoid having to allocate all the time
2392 */
2393 if (len < 80)
2394 len = 100;
2395 else
2396 len += 20;
2397
2398 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2399 ccline.cmdbufflen = len;
2400}
2401
2402/*
2403 * Re-allocate the command line to length len + something extra.
2404 * return FAIL for failure, OK otherwise
2405 */
2406 static int
2407realloc_cmdbuff(len)
2408 int len;
2409{
2410 char_u *p;
2411
2412 p = ccline.cmdbuff;
2413 alloc_cmdbuff(len); /* will get some more */
2414 if (ccline.cmdbuff == NULL) /* out of memory */
2415 {
2416 ccline.cmdbuff = p; /* keep the old one */
2417 return FAIL;
2418 }
2419 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2420 vim_free(p);
2421 return OK;
2422}
2423
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002424#if defined(FEAT_ARABIC) || defined(PROTO)
2425static char_u *arshape_buf = NULL;
2426
2427# if defined(EXITFREE) || defined(PROTO)
2428 void
2429free_cmdline_buf()
2430{
2431 vim_free(arshape_buf);
2432}
2433# endif
2434#endif
2435
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436/*
2437 * Draw part of the cmdline at the current cursor position. But draw stars
2438 * when cmdline_star is TRUE.
2439 */
2440 static void
2441draw_cmdline(start, len)
2442 int start;
2443 int len;
2444{
2445#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2446 int i;
2447
2448 if (cmdline_star > 0)
2449 for (i = 0; i < len; ++i)
2450 {
2451 msg_putchar('*');
2452# ifdef FEAT_MBYTE
2453 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002454 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455# endif
2456 }
2457 else
2458#endif
2459#ifdef FEAT_ARABIC
2460 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2461 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 static int buflen = 0;
2463 char_u *p;
2464 int j;
2465 int newlen = 0;
2466 int mb_l;
2467 int pc, pc1;
2468 int prev_c = 0;
2469 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002470 int u8c;
2471 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 /*
2475 * Do arabic shaping into a temporary buffer. This is very
2476 * inefficient!
2477 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002478 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
2480 /* Re-allocate the buffer. We keep it around to avoid a lot of
2481 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002482 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002483 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002484 arshape_buf = alloc(buflen);
2485 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 return; /* out of memory */
2487 }
2488
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002489 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2490 {
2491 /* Prepend a space to draw the leading composing char on. */
2492 arshape_buf[0] = ' ';
2493 newlen = 1;
2494 }
2495
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 for (j = start; j < start + len; j += mb_l)
2497 {
2498 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002499 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002500 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 if (ARABIC_CHAR(u8c))
2502 {
2503 /* Do Arabic shaping. */
2504 if (cmdmsg_rl)
2505 {
2506 /* displaying from right to left */
2507 pc = prev_c;
2508 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002509 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 if (j + mb_l >= start + len)
2511 nc = NUL;
2512 else
2513 nc = utf_ptr2char(p + mb_l);
2514 }
2515 else
2516 {
2517 /* displaying from left to right */
2518 if (j + mb_l >= start + len)
2519 pc = NUL;
2520 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002521 {
2522 int pcc[MAX_MCO];
2523
2524 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002526 pc1 = pcc[0];
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 nc = prev_c;
2529 }
2530 prev_c = u8c;
2531
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002532 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002534 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002535 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002537 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2538 if (u8cc[1] != 0)
2539 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002540 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
2542 }
2543 else
2544 {
2545 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002546 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 newlen += mb_l;
2548 }
2549 }
2550
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002551 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 }
2553 else
2554#endif
2555 msg_outtrans_len(ccline.cmdbuff + start, len);
2556}
2557
2558/*
2559 * Put a character on the command line. Shifts the following text to the
2560 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2561 * "c" must be printable (fit in one display cell)!
2562 */
2563 void
2564putcmdline(c, shift)
2565 int c;
2566 int shift;
2567{
2568 if (cmd_silent)
2569 return;
2570 msg_no_more = TRUE;
2571 msg_putchar(c);
2572 if (shift)
2573 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2574 msg_no_more = FALSE;
2575 cursorcmd();
2576}
2577
2578/*
2579 * Undo a putcmdline(c, FALSE).
2580 */
2581 void
2582unputcmdline()
2583{
2584 if (cmd_silent)
2585 return;
2586 msg_no_more = TRUE;
2587 if (ccline.cmdlen == ccline.cmdpos)
2588 msg_putchar(' ');
2589 else
2590 draw_cmdline(ccline.cmdpos, 1);
2591 msg_no_more = FALSE;
2592 cursorcmd();
2593}
2594
2595/*
2596 * Put the given string, of the given length, onto the command line.
2597 * If len is -1, then STRLEN() is used to calculate the length.
2598 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2599 * part will be redrawn, otherwise it will not. If this function is called
2600 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2601 * called afterwards.
2602 */
2603 int
2604put_on_cmdline(str, len, redraw)
2605 char_u *str;
2606 int len;
2607 int redraw;
2608{
2609 int retval;
2610 int i;
2611 int m;
2612 int c;
2613
2614 if (len < 0)
2615 len = (int)STRLEN(str);
2616
2617 /* Check if ccline.cmdbuff needs to be longer */
2618 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2619 retval = realloc_cmdbuff(ccline.cmdlen + len);
2620 else
2621 retval = OK;
2622 if (retval == OK)
2623 {
2624 if (!ccline.overstrike)
2625 {
2626 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2627 ccline.cmdbuff + ccline.cmdpos,
2628 (size_t)(ccline.cmdlen - ccline.cmdpos));
2629 ccline.cmdlen += len;
2630 }
2631 else
2632 {
2633#ifdef FEAT_MBYTE
2634 if (has_mbyte)
2635 {
2636 /* Count nr of characters in the new string. */
2637 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002638 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 ++m;
2640 /* Count nr of bytes in cmdline that are overwritten by these
2641 * characters. */
2642 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002643 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 --m;
2645 if (i < ccline.cmdlen)
2646 {
2647 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2648 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2649 ccline.cmdlen += ccline.cmdpos + len - i;
2650 }
2651 else
2652 ccline.cmdlen = ccline.cmdpos + len;
2653 }
2654 else
2655#endif
2656 if (ccline.cmdpos + len > ccline.cmdlen)
2657 ccline.cmdlen = ccline.cmdpos + len;
2658 }
2659 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2660 ccline.cmdbuff[ccline.cmdlen] = NUL;
2661
2662#ifdef FEAT_MBYTE
2663 if (enc_utf8)
2664 {
2665 /* When the inserted text starts with a composing character,
2666 * backup to the character before it. There could be two of them.
2667 */
2668 i = 0;
2669 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2670 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2671 {
2672 i = (*mb_head_off)(ccline.cmdbuff,
2673 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2674 ccline.cmdpos -= i;
2675 len += i;
2676 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2677 }
2678# ifdef FEAT_ARABIC
2679 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2680 {
2681 /* Check the previous character for Arabic combining pair. */
2682 i = (*mb_head_off)(ccline.cmdbuff,
2683 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2684 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2685 + ccline.cmdpos - i), c))
2686 {
2687 ccline.cmdpos -= i;
2688 len += i;
2689 }
2690 else
2691 i = 0;
2692 }
2693# endif
2694 if (i != 0)
2695 {
2696 /* Also backup the cursor position. */
2697 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2698 ccline.cmdspos -= i;
2699 msg_col -= i;
2700 if (msg_col < 0)
2701 {
2702 msg_col += Columns;
2703 --msg_row;
2704 }
2705 }
2706 }
2707#endif
2708
2709 if (redraw && !cmd_silent)
2710 {
2711 msg_no_more = TRUE;
2712 i = cmdline_row;
2713 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2714 /* Avoid clearing the rest of the line too often. */
2715 if (cmdline_row != i || ccline.overstrike)
2716 msg_clr_eos();
2717 msg_no_more = FALSE;
2718 }
2719#ifdef FEAT_FKMAP
2720 /*
2721 * If we are in Farsi command mode, the character input must be in
2722 * Insert mode. So do not advance the cmdpos.
2723 */
2724 if (!cmd_fkmap)
2725#endif
2726 {
2727 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002730 if (m < 0) /* overflow, Columns or Rows at weird value */
2731 m = MAXCOL;
2732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 else
2734 m = MAXCOL;
2735 for (i = 0; i < len; ++i)
2736 {
2737 c = cmdline_charsize(ccline.cmdpos);
2738#ifdef FEAT_MBYTE
2739 /* count ">" for a double-wide char that doesn't fit. */
2740 if (has_mbyte)
2741 correct_cmdspos(ccline.cmdpos, c);
2742#endif
2743 /* Stop cursor at the end of the screen */
2744 if (ccline.cmdspos + c >= m)
2745 break;
2746 ccline.cmdspos += c;
2747#ifdef FEAT_MBYTE
2748 if (has_mbyte)
2749 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002750 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (c > len - i - 1)
2752 c = len - i - 1;
2753 ccline.cmdpos += c;
2754 i += c;
2755 }
2756#endif
2757 ++ccline.cmdpos;
2758 }
2759 }
2760 }
2761 if (redraw)
2762 msg_check();
2763 return retval;
2764}
2765
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002766static struct cmdline_info prev_ccline;
2767static int prev_ccline_used = FALSE;
2768
2769/*
2770 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2771 * and overwrite it. But get_cmdline_str() may need it, thus make it
2772 * available globally in prev_ccline.
2773 */
2774 static void
2775save_cmdline(ccp)
2776 struct cmdline_info *ccp;
2777{
2778 if (!prev_ccline_used)
2779 {
2780 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2781 prev_ccline_used = TRUE;
2782 }
2783 *ccp = prev_ccline;
2784 prev_ccline = ccline;
2785 ccline.cmdbuff = NULL;
2786 ccline.cmdprompt = NULL;
2787}
2788
2789/*
2790 * Resture ccline after it has been saved with save_cmdline().
2791 */
2792 static void
2793restore_cmdline(ccp)
2794 struct cmdline_info *ccp;
2795{
2796 ccline = prev_ccline;
2797 prev_ccline = *ccp;
2798}
2799
Bram Moolenaar8299df92004-07-10 09:47:34 +00002800/*
2801 * paste a yank register into the command line.
2802 * used by CTRL-R command in command-line mode
2803 * insert_reg() can't be used here, because special characters from the
2804 * register contents will be interpreted as commands.
2805 *
2806 * return FAIL for failure, OK otherwise
2807 */
2808 static int
2809cmdline_paste(regname, literally)
2810 int regname;
2811 int literally; /* Insert text literally instead of "as typed" */
2812{
2813 long i;
2814 char_u *arg;
2815 int allocated;
2816 struct cmdline_info save_ccline;
2817
2818 /* check for valid regname; also accept special characters for CTRL-R in
2819 * the command line */
2820 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2821 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2822 return FAIL;
2823
2824 /* A register containing CTRL-R can cause an endless loop. Allow using
2825 * CTRL-C to break the loop. */
2826 line_breakcheck();
2827 if (got_int)
2828 return FAIL;
2829
2830#ifdef FEAT_CLIPBOARD
2831 regname = may_get_selection(regname);
2832#endif
2833
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002834 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002835 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002836 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002837 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002838 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002839 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002840 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002841
2842 if (i)
2843 {
2844 /* Got the value of a special register in "arg". */
2845 if (arg == NULL)
2846 return FAIL;
2847 cmdline_paste_str(arg, literally);
2848 if (allocated)
2849 vim_free(arg);
2850 return OK;
2851 }
2852
2853 return cmdline_paste_reg(regname, literally);
2854}
2855
2856/*
2857 * Put a string on the command line.
2858 * When "literally" is TRUE, insert literally.
2859 * When "literally" is FALSE, insert as typed, but don't leave the command
2860 * line.
2861 */
2862 void
2863cmdline_paste_str(s, literally)
2864 char_u *s;
2865 int literally;
2866{
2867 int c, cv;
2868
2869 if (literally)
2870 put_on_cmdline(s, -1, TRUE);
2871 else
2872 while (*s != NUL)
2873 {
2874 cv = *s;
2875 if (cv == Ctrl_V && s[1])
2876 ++s;
2877#ifdef FEAT_MBYTE
2878 if (has_mbyte)
2879 {
2880 c = mb_ptr2char(s);
2881 s += mb_char2len(c);
2882 }
2883 else
2884#endif
2885 c = *s++;
2886 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2887#ifdef UNIX
2888 || c == intr_char
2889#endif
2890 || (c == Ctrl_BSL && *s == Ctrl_N))
2891 stuffcharReadbuff(Ctrl_V);
2892 stuffcharReadbuff(c);
2893 }
2894}
2895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896#ifdef FEAT_WILDMENU
2897/*
2898 * Delete characters on the command line, from "from" to the current
2899 * position.
2900 */
2901 static void
2902cmdline_del(from)
2903 int from;
2904{
2905 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2906 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2907 ccline.cmdlen -= ccline.cmdpos - from;
2908 ccline.cmdpos = from;
2909}
2910#endif
2911
2912/*
2913 * this fuction is called when the screen size changes and with incremental
2914 * search
2915 */
2916 void
2917redrawcmdline()
2918{
2919 if (cmd_silent)
2920 return;
2921 need_wait_return = FALSE;
2922 compute_cmdrow();
2923 redrawcmd();
2924 cursorcmd();
2925}
2926
2927 static void
2928redrawcmdprompt()
2929{
2930 int i;
2931
2932 if (cmd_silent)
2933 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002934 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 msg_putchar(ccline.cmdfirstc);
2936 if (ccline.cmdprompt != NULL)
2937 {
2938 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2939 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2940 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002941 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 --ccline.cmdindent;
2943 }
2944 else
2945 for (i = ccline.cmdindent; i > 0; --i)
2946 msg_putchar(' ');
2947}
2948
2949/*
2950 * Redraw what is currently on the command line.
2951 */
2952 void
2953redrawcmd()
2954{
2955 if (cmd_silent)
2956 return;
2957
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00002958 /* when 'incsearch' is set there may be no command line while redrawing */
2959 if (ccline.cmdbuff == NULL)
2960 {
2961 windgoto(cmdline_row, 0);
2962 msg_clr_eos();
2963 return;
2964 }
2965
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 msg_start();
2967 redrawcmdprompt();
2968
2969 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2970 msg_no_more = TRUE;
2971 draw_cmdline(0, ccline.cmdlen);
2972 msg_clr_eos();
2973 msg_no_more = FALSE;
2974
2975 set_cmdspos_cursor();
2976
2977 /*
2978 * An emsg() before may have set msg_scroll. This is used in normal mode,
2979 * in cmdline mode we can reset them now.
2980 */
2981 msg_scroll = FALSE; /* next message overwrites cmdline */
2982
2983 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2984 * in cmdline mode */
2985 skip_redraw = FALSE;
2986}
2987
2988 void
2989compute_cmdrow()
2990{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002991 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 cmdline_row = Rows - 1;
2993 else
2994 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2995 + W_STATUS_HEIGHT(lastwin);
2996}
2997
2998 static void
2999cursorcmd()
3000{
3001 if (cmd_silent)
3002 return;
3003
3004#ifdef FEAT_RIGHTLEFT
3005 if (cmdmsg_rl)
3006 {
3007 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3008 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3009 if (msg_row <= 0)
3010 msg_row = Rows - 1;
3011 }
3012 else
3013#endif
3014 {
3015 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3016 msg_col = ccline.cmdspos % (int)Columns;
3017 if (msg_row >= Rows)
3018 msg_row = Rows - 1;
3019 }
3020
3021 windgoto(msg_row, msg_col);
3022#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3023 redrawcmd_preedit();
3024#endif
3025#ifdef MCH_CURSOR_SHAPE
3026 mch_update_cursor();
3027#endif
3028}
3029
3030 void
3031gotocmdline(clr)
3032 int clr;
3033{
3034 msg_start();
3035#ifdef FEAT_RIGHTLEFT
3036 if (cmdmsg_rl)
3037 msg_col = Columns - 1;
3038 else
3039#endif
3040 msg_col = 0; /* always start in column 0 */
3041 if (clr) /* clear the bottom line(s) */
3042 msg_clr_eos(); /* will reset clear_cmdline */
3043 windgoto(cmdline_row, 0);
3044}
3045
3046/*
3047 * Check the word in front of the cursor for an abbreviation.
3048 * Called when the non-id character "c" has been entered.
3049 * When an abbreviation is recognized it is removed from the text with
3050 * backspaces and the replacement string is inserted, followed by "c".
3051 */
3052 static int
3053ccheck_abbr(c)
3054 int c;
3055{
3056 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3057 return FALSE;
3058
3059 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3060}
3061
3062/*
3063 * Return FAIL if this is not an appropriate context in which to do
3064 * completion of anything, return OK if it is (even if there are no matches).
3065 * For the caller, this means that the character is just passed through like a
3066 * normal character (instead of being expanded). This allows :s/^I^D etc.
3067 */
3068 static int
3069nextwild(xp, type, options)
3070 expand_T *xp;
3071 int type;
3072 int options; /* extra options for ExpandOne() */
3073{
3074 int i, j;
3075 char_u *p1;
3076 char_u *p2;
3077 int oldlen;
3078 int difflen;
3079 int v;
3080
3081 if (xp->xp_numfiles == -1)
3082 {
3083 set_expand_context(xp);
3084 cmd_showtail = expand_showtail(xp);
3085 }
3086
3087 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3088 {
3089 beep_flush();
3090 return OK; /* Something illegal on command line */
3091 }
3092 if (xp->xp_context == EXPAND_NOTHING)
3093 {
3094 /* Caller can use the character as a normal char instead */
3095 return FAIL;
3096 }
3097
3098 MSG_PUTS("..."); /* show that we are busy */
3099 out_flush();
3100
3101 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3102 oldlen = ccline.cmdpos - i;
3103
3104 if (type == WILD_NEXT || type == WILD_PREV)
3105 {
3106 /*
3107 * Get next/previous match for a previous expanded pattern.
3108 */
3109 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3110 }
3111 else
3112 {
3113 /*
3114 * Translate string into pattern and expand it.
3115 */
3116 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3117 p2 = NULL;
3118 else
3119 {
3120 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3121 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3122 |options, type);
3123 vim_free(p1);
3124 /* longest match: make sure it is not shorter (happens with :help */
3125 if (p2 != NULL && type == WILD_LONGEST)
3126 {
3127 for (j = 0; j < oldlen; ++j)
3128 if (ccline.cmdbuff[i + j] == '*'
3129 || ccline.cmdbuff[i + j] == '?')
3130 break;
3131 if ((int)STRLEN(p2) < j)
3132 {
3133 vim_free(p2);
3134 p2 = NULL;
3135 }
3136 }
3137 }
3138 }
3139
3140 if (p2 != NULL && !got_int)
3141 {
3142 difflen = (int)STRLEN(p2) - oldlen;
3143 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3144 {
3145 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3146 xp->xp_pattern = ccline.cmdbuff + i;
3147 }
3148 else
3149 v = OK;
3150 if (v == OK)
3151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003152 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3153 &ccline.cmdbuff[ccline.cmdpos],
3154 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3155 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 ccline.cmdlen += difflen;
3157 ccline.cmdpos += difflen;
3158 }
3159 }
3160 vim_free(p2);
3161
3162 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003163 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165 /* When expanding a ":map" command and no matches are found, assume that
3166 * the key is supposed to be inserted literally */
3167 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3168 return FAIL;
3169
3170 if (xp->xp_numfiles <= 0 && p2 == NULL)
3171 beep_flush();
3172 else if (xp->xp_numfiles == 1)
3173 /* free expanded pattern */
3174 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3175
3176 return OK;
3177}
3178
3179/*
3180 * Do wildcard expansion on the string 'str'.
3181 * Chars that should not be expanded must be preceded with a backslash.
3182 * Return a pointer to alloced memory containing the new string.
3183 * Return NULL for failure.
3184 *
3185 * Results are cached in xp->xp_files and xp->xp_numfiles.
3186 *
3187 * mode = WILD_FREE: just free previously expanded matches
3188 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3189 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3190 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3191 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3192 * mode = WILD_ALL: return all matches concatenated
3193 * mode = WILD_LONGEST: return longest matched part
3194 *
3195 * options = WILD_LIST_NOTFOUND: list entries without a match
3196 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3197 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3198 * options = WILD_NO_BEEP: Don't beep for multiple matches
3199 * options = WILD_ADD_SLASH: add a slash after directory names
3200 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3201 * options = WILD_SILENT: don't print warning messages
3202 * options = WILD_ESCAPE: put backslash before special chars
3203 *
3204 * The variables xp->xp_context and xp->xp_backslash must have been set!
3205 */
3206 char_u *
3207ExpandOne(xp, str, orig, options, mode)
3208 expand_T *xp;
3209 char_u *str;
3210 char_u *orig; /* allocated copy of original of expanded string */
3211 int options;
3212 int mode;
3213{
3214 char_u *ss = NULL;
3215 static int findex;
3216 static char_u *orig_save = NULL; /* kept value of orig */
3217 int i;
3218 long_u len;
3219 int non_suf_match; /* number without matching suffix */
3220
3221 /*
3222 * first handle the case of using an old match
3223 */
3224 if (mode == WILD_NEXT || mode == WILD_PREV)
3225 {
3226 if (xp->xp_numfiles > 0)
3227 {
3228 if (mode == WILD_PREV)
3229 {
3230 if (findex == -1)
3231 findex = xp->xp_numfiles;
3232 --findex;
3233 }
3234 else /* mode == WILD_NEXT */
3235 ++findex;
3236
3237 /*
3238 * When wrapping around, return the original string, set findex to
3239 * -1.
3240 */
3241 if (findex < 0)
3242 {
3243 if (orig_save == NULL)
3244 findex = xp->xp_numfiles - 1;
3245 else
3246 findex = -1;
3247 }
3248 if (findex >= xp->xp_numfiles)
3249 {
3250 if (orig_save == NULL)
3251 findex = 0;
3252 else
3253 findex = -1;
3254 }
3255#ifdef FEAT_WILDMENU
3256 if (p_wmnu)
3257 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3258 findex, cmd_showtail);
3259#endif
3260 if (findex == -1)
3261 return vim_strsave(orig_save);
3262 return vim_strsave(xp->xp_files[findex]);
3263 }
3264 else
3265 return NULL;
3266 }
3267
3268/* free old names */
3269 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3270 {
3271 FreeWild(xp->xp_numfiles, xp->xp_files);
3272 xp->xp_numfiles = -1;
3273 vim_free(orig_save);
3274 orig_save = NULL;
3275 }
3276 findex = 0;
3277
3278 if (mode == WILD_FREE) /* only release file name */
3279 return NULL;
3280
3281 if (xp->xp_numfiles == -1)
3282 {
3283 vim_free(orig_save);
3284 orig_save = orig;
3285
3286 /*
3287 * Do the expansion.
3288 */
3289 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3290 options) == FAIL)
3291 {
3292#ifdef FNAME_ILLEGAL
3293 /* Illegal file name has been silently skipped. But when there
3294 * are wildcards, the real problem is that there was no match,
3295 * causing the pattern to be added, which has illegal characters.
3296 */
3297 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3298 EMSG2(_(e_nomatch2), str);
3299#endif
3300 }
3301 else if (xp->xp_numfiles == 0)
3302 {
3303 if (!(options & WILD_SILENT))
3304 EMSG2(_(e_nomatch2), str);
3305 }
3306 else
3307 {
3308 /* Escape the matches for use on the command line. */
3309 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3310
3311 /*
3312 * Check for matching suffixes in file names.
3313 */
3314 if (mode != WILD_ALL && mode != WILD_LONGEST)
3315 {
3316 if (xp->xp_numfiles)
3317 non_suf_match = xp->xp_numfiles;
3318 else
3319 non_suf_match = 1;
3320 if ((xp->xp_context == EXPAND_FILES
3321 || xp->xp_context == EXPAND_DIRECTORIES)
3322 && xp->xp_numfiles > 1)
3323 {
3324 /*
3325 * More than one match; check suffix.
3326 * The files will have been sorted on matching suffix in
3327 * expand_wildcards, only need to check the first two.
3328 */
3329 non_suf_match = 0;
3330 for (i = 0; i < 2; ++i)
3331 if (match_suffix(xp->xp_files[i]))
3332 ++non_suf_match;
3333 }
3334 if (non_suf_match != 1)
3335 {
3336 /* Can we ever get here unless it's while expanding
3337 * interactively? If not, we can get rid of this all
3338 * together. Don't really want to wait for this message
3339 * (and possibly have to hit return to continue!).
3340 */
3341 if (!(options & WILD_SILENT))
3342 EMSG(_(e_toomany));
3343 else if (!(options & WILD_NO_BEEP))
3344 beep_flush();
3345 }
3346 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3347 ss = vim_strsave(xp->xp_files[0]);
3348 }
3349 }
3350 }
3351
3352 /* Find longest common part */
3353 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3354 {
3355 for (len = 0; xp->xp_files[0][len]; ++len)
3356 {
3357 for (i = 0; i < xp->xp_numfiles; ++i)
3358 {
3359#ifdef CASE_INSENSITIVE_FILENAME
3360 if (xp->xp_context == EXPAND_DIRECTORIES
3361 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003362 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 || xp->xp_context == EXPAND_BUFFERS)
3364 {
3365 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3366 TOLOWER_LOC(xp->xp_files[0][len]))
3367 break;
3368 }
3369 else
3370#endif
3371 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3372 break;
3373 }
3374 if (i < xp->xp_numfiles)
3375 {
3376 if (!(options & WILD_NO_BEEP))
3377 vim_beep();
3378 break;
3379 }
3380 }
3381 ss = alloc((unsigned)len + 1);
3382 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003383 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 findex = -1; /* next p_wc gets first one */
3385 }
3386
3387 /* Concatenate all matching names */
3388 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3389 {
3390 len = 0;
3391 for (i = 0; i < xp->xp_numfiles; ++i)
3392 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3393 ss = lalloc(len, TRUE);
3394 if (ss != NULL)
3395 {
3396 *ss = NUL;
3397 for (i = 0; i < xp->xp_numfiles; ++i)
3398 {
3399 STRCAT(ss, xp->xp_files[i]);
3400 if (i != xp->xp_numfiles - 1)
3401 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3402 }
3403 }
3404 }
3405
3406 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3407 ExpandCleanup(xp);
3408
3409 return ss;
3410}
3411
3412/*
3413 * Prepare an expand structure for use.
3414 */
3415 void
3416ExpandInit(xp)
3417 expand_T *xp;
3418{
3419 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003420#ifndef BACKSLASH_IN_FILENAME
3421 xp->xp_shell = FALSE;
3422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 xp->xp_numfiles = -1;
3424 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003425#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3426 xp->xp_arg = NULL;
3427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428}
3429
3430/*
3431 * Cleanup an expand structure after use.
3432 */
3433 void
3434ExpandCleanup(xp)
3435 expand_T *xp;
3436{
3437 if (xp->xp_numfiles >= 0)
3438 {
3439 FreeWild(xp->xp_numfiles, xp->xp_files);
3440 xp->xp_numfiles = -1;
3441 }
3442}
3443
3444 void
3445ExpandEscape(xp, str, numfiles, files, options)
3446 expand_T *xp;
3447 char_u *str;
3448 int numfiles;
3449 char_u **files;
3450 int options;
3451{
3452 int i;
3453 char_u *p;
3454
3455 /*
3456 * May change home directory back to "~"
3457 */
3458 if (options & WILD_HOME_REPLACE)
3459 tilde_replace(str, numfiles, files);
3460
3461 if (options & WILD_ESCAPE)
3462 {
3463 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003464 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 || xp->xp_context == EXPAND_BUFFERS
3466 || xp->xp_context == EXPAND_DIRECTORIES)
3467 {
3468 /*
3469 * Insert a backslash into a file name before a space, \, %, #
3470 * and wildmatch characters, except '~'.
3471 */
3472 for (i = 0; i < numfiles; ++i)
3473 {
3474 /* for ":set path=" we need to escape spaces twice */
3475 if (xp->xp_backslash == XP_BS_THREE)
3476 {
3477 p = vim_strsave_escaped(files[i], (char_u *)" ");
3478 if (p != NULL)
3479 {
3480 vim_free(files[i]);
3481 files[i] = p;
3482#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3483 p = vim_strsave_escaped(files[i], (char_u *)" ");
3484 if (p != NULL)
3485 {
3486 vim_free(files[i]);
3487 files[i] = p;
3488 }
3489#endif
3490 }
3491 }
3492#ifdef BACKSLASH_IN_FILENAME
3493 {
3494 char_u buf[20];
3495 int j = 0;
3496
3497 /* Don't escape '[' and '{' if they are in 'isfname'. */
3498 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3499 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3500 buf[j++] = *p;
3501 buf[j] = NUL;
3502 p = vim_strsave_escaped(files[i], buf);
3503 }
3504#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003505 p = vim_strsave_escaped(files[i],
3506 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507#endif
3508 if (p != NULL)
3509 {
3510 vim_free(files[i]);
3511 files[i] = p;
3512 }
3513
3514 /* If 'str' starts with "\~", replace "~" at start of
3515 * files[i] with "\~". */
3516 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003517 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003520
3521 /* If the first file starts with a '+' escape it. Otherwise it
3522 * could be seen as "+cmd". */
3523 if (*files[0] == '+')
3524 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 else if (xp->xp_context == EXPAND_TAGS)
3527 {
3528 /*
3529 * Insert a backslash before characters in a tag name that
3530 * would terminate the ":tag" command.
3531 */
3532 for (i = 0; i < numfiles; ++i)
3533 {
3534 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3535 if (p != NULL)
3536 {
3537 vim_free(files[i]);
3538 files[i] = p;
3539 }
3540 }
3541 }
3542 }
3543}
3544
3545/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003546 * Put a backslash before the file name in "pp", which is in allocated memory.
3547 */
3548 static void
3549escape_fname(pp)
3550 char_u **pp;
3551{
3552 char_u *p;
3553
3554 p = alloc((unsigned)(STRLEN(*pp) + 2));
3555 if (p != NULL)
3556 {
3557 p[0] = '\\';
3558 STRCPY(p + 1, *pp);
3559 vim_free(*pp);
3560 *pp = p;
3561 }
3562}
3563
3564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 * For each file name in files[num_files]:
3566 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3567 */
3568 void
3569tilde_replace(orig_pat, num_files, files)
3570 char_u *orig_pat;
3571 int num_files;
3572 char_u **files;
3573{
3574 int i;
3575 char_u *p;
3576
3577 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3578 {
3579 for (i = 0; i < num_files; ++i)
3580 {
3581 p = home_replace_save(NULL, files[i]);
3582 if (p != NULL)
3583 {
3584 vim_free(files[i]);
3585 files[i] = p;
3586 }
3587 }
3588 }
3589}
3590
3591/*
3592 * Show all matches for completion on the command line.
3593 * Returns EXPAND_NOTHING when the character that triggered expansion should
3594 * be inserted like a normal character.
3595 */
3596/*ARGSUSED*/
3597 static int
3598showmatches(xp, wildmenu)
3599 expand_T *xp;
3600 int wildmenu;
3601{
3602#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3603 int num_files;
3604 char_u **files_found;
3605 int i, j, k;
3606 int maxlen;
3607 int lines;
3608 int columns;
3609 char_u *p;
3610 int lastlen;
3611 int attr;
3612 int showtail;
3613
3614 if (xp->xp_numfiles == -1)
3615 {
3616 set_expand_context(xp);
3617 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3618 &num_files, &files_found);
3619 showtail = expand_showtail(xp);
3620 if (i != EXPAND_OK)
3621 return i;
3622
3623 }
3624 else
3625 {
3626 num_files = xp->xp_numfiles;
3627 files_found = xp->xp_files;
3628 showtail = cmd_showtail;
3629 }
3630
3631#ifdef FEAT_WILDMENU
3632 if (!wildmenu)
3633 {
3634#endif
3635 msg_didany = FALSE; /* lines_left will be set */
3636 msg_start(); /* prepare for paging */
3637 msg_putchar('\n');
3638 out_flush();
3639 cmdline_row = msg_row;
3640 msg_didany = FALSE; /* lines_left will be set again */
3641 msg_start(); /* prepare for paging */
3642#ifdef FEAT_WILDMENU
3643 }
3644#endif
3645
3646 if (got_int)
3647 got_int = FALSE; /* only int. the completion, not the cmd line */
3648#ifdef FEAT_WILDMENU
3649 else if (wildmenu)
3650 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3651#endif
3652 else
3653 {
3654 /* find the length of the longest file name */
3655 maxlen = 0;
3656 for (i = 0; i < num_files; ++i)
3657 {
3658 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003659 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 || xp->xp_context == EXPAND_BUFFERS))
3661 {
3662 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3663 j = vim_strsize(NameBuff);
3664 }
3665 else
3666 j = vim_strsize(L_SHOWFILE(i));
3667 if (j > maxlen)
3668 maxlen = j;
3669 }
3670
3671 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3672 lines = num_files;
3673 else
3674 {
3675 /* compute the number of columns and lines for the listing */
3676 maxlen += 2; /* two spaces between file names */
3677 columns = ((int)Columns + 2) / maxlen;
3678 if (columns < 1)
3679 columns = 1;
3680 lines = (num_files + columns - 1) / columns;
3681 }
3682
3683 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3684
3685 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3686 {
3687 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3688 msg_clr_eos();
3689 msg_advance(maxlen - 3);
3690 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3691 }
3692
3693 /* list the files line by line */
3694 for (i = 0; i < lines; ++i)
3695 {
3696 lastlen = 999;
3697 for (k = i; k < num_files; k += lines)
3698 {
3699 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3700 {
3701 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3702 p = files_found[k] + STRLEN(files_found[k]) + 1;
3703 msg_advance(maxlen + 1);
3704 msg_puts(p);
3705 msg_advance(maxlen + 3);
3706 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3707 break;
3708 }
3709 for (j = maxlen - lastlen; --j >= 0; )
3710 msg_putchar(' ');
3711 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003712 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 || xp->xp_context == EXPAND_BUFFERS)
3714 {
3715 /* highlight directories */
3716 j = (mch_isdir(files_found[k]));
3717 if (showtail)
3718 p = L_SHOWFILE(k);
3719 else
3720 {
3721 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3722 TRUE);
3723 p = NameBuff;
3724 }
3725 }
3726 else
3727 {
3728 j = FALSE;
3729 p = L_SHOWFILE(k);
3730 }
3731 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3732 }
3733 if (msg_col > 0) /* when not wrapped around */
3734 {
3735 msg_clr_eos();
3736 msg_putchar('\n');
3737 }
3738 out_flush(); /* show one line at a time */
3739 if (got_int)
3740 {
3741 got_int = FALSE;
3742 break;
3743 }
3744 }
3745
3746 /*
3747 * we redraw the command below the lines that we have just listed
3748 * This is a bit tricky, but it saves a lot of screen updating.
3749 */
3750 cmdline_row = msg_row; /* will put it back later */
3751 }
3752
3753 if (xp->xp_numfiles == -1)
3754 FreeWild(num_files, files_found);
3755
3756 return EXPAND_OK;
3757}
3758
3759/*
3760 * Private gettail for showmatches() (and win_redr_status_matches()):
3761 * Find tail of file name path, but ignore trailing "/".
3762 */
3763 char_u *
3764sm_gettail(s)
3765 char_u *s;
3766{
3767 char_u *p;
3768 char_u *t = s;
3769 int had_sep = FALSE;
3770
3771 for (p = s; *p != NUL; )
3772 {
3773 if (vim_ispathsep(*p)
3774#ifdef BACKSLASH_IN_FILENAME
3775 && !rem_backslash(p)
3776#endif
3777 )
3778 had_sep = TRUE;
3779 else if (had_sep)
3780 {
3781 t = p;
3782 had_sep = FALSE;
3783 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003784 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
3786 return t;
3787}
3788
3789/*
3790 * Return TRUE if we only need to show the tail of completion matches.
3791 * When not completing file names or there is a wildcard in the path FALSE is
3792 * returned.
3793 */
3794 static int
3795expand_showtail(xp)
3796 expand_T *xp;
3797{
3798 char_u *s;
3799 char_u *end;
3800
3801 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003802 if (xp->xp_context != EXPAND_FILES
3803 && xp->xp_context != EXPAND_SHELLCMD
3804 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 return FALSE;
3806
3807 end = gettail(xp->xp_pattern);
3808 if (end == xp->xp_pattern) /* there is no path separator */
3809 return FALSE;
3810
3811 for (s = xp->xp_pattern; s < end; s++)
3812 {
3813 /* Skip escaped wildcards. Only when the backslash is not a path
3814 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3815 if (rem_backslash(s))
3816 ++s;
3817 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3818 return FALSE;
3819 }
3820 return TRUE;
3821}
3822
3823/*
3824 * Prepare a string for expansion.
3825 * When expanding file names: The string will be used with expand_wildcards().
3826 * Copy the file name into allocated memory and add a '*' at the end.
3827 * When expanding other names: The string will be used with regcomp(). Copy
3828 * the name into allocated memory and prepend "^".
3829 */
3830 char_u *
3831addstar(fname, len, context)
3832 char_u *fname;
3833 int len;
3834 int context; /* EXPAND_FILES etc. */
3835{
3836 char_u *retval;
3837 int i, j;
3838 int new_len;
3839 char_u *tail;
3840
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003841 if (context != EXPAND_FILES
3842 && context != EXPAND_SHELLCMD
3843 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 {
3845 /*
3846 * Matching will be done internally (on something other than files).
3847 * So we convert the file-matching-type wildcards into our kind for
3848 * use with vim_regcomp(). First work out how long it will be:
3849 */
3850
3851 /* For help tags the translation is done in find_help_tags().
3852 * For a tag pattern starting with "/" no translation is needed. */
3853 if (context == EXPAND_HELP
3854 || context == EXPAND_COLORS
3855 || context == EXPAND_COMPILER
3856 || (context == EXPAND_TAGS && fname[0] == '/'))
3857 retval = vim_strnsave(fname, len);
3858 else
3859 {
3860 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3861 for (i = 0; i < len; i++)
3862 {
3863 if (fname[i] == '*' || fname[i] == '~')
3864 new_len++; /* '*' needs to be replaced by ".*"
3865 '~' needs to be replaced by "\~" */
3866
3867 /* Buffer names are like file names. "." should be literal */
3868 if (context == EXPAND_BUFFERS && fname[i] == '.')
3869 new_len++; /* "." becomes "\." */
3870
3871 /* Custom expansion takes care of special things, match
3872 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003873 if ((context == EXPAND_USER_DEFINED
3874 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 new_len++; /* '\' becomes "\\" */
3876 }
3877 retval = alloc(new_len);
3878 if (retval != NULL)
3879 {
3880 retval[0] = '^';
3881 j = 1;
3882 for (i = 0; i < len; i++, j++)
3883 {
3884 /* Skip backslash. But why? At least keep it for custom
3885 * expansion. */
3886 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003887 && context != EXPAND_USER_LIST
3888 && fname[i] == '\\'
3889 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 break;
3891
3892 switch (fname[i])
3893 {
3894 case '*': retval[j++] = '.';
3895 break;
3896 case '~': retval[j++] = '\\';
3897 break;
3898 case '?': retval[j] = '.';
3899 continue;
3900 case '.': if (context == EXPAND_BUFFERS)
3901 retval[j++] = '\\';
3902 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003903 case '\\': if (context == EXPAND_USER_DEFINED
3904 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 retval[j++] = '\\';
3906 break;
3907 }
3908 retval[j] = fname[i];
3909 }
3910 retval[j] = NUL;
3911 }
3912 }
3913 }
3914 else
3915 {
3916 retval = alloc(len + 4);
3917 if (retval != NULL)
3918 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003919 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 /*
3922 * Don't add a star to ~, ~user, $var or `cmd`.
3923 * ~ would be at the start of the file name, but not the tail.
3924 * $ could be anywhere in the tail.
3925 * ` could be anywhere in the file name.
3926 */
3927 tail = gettail(retval);
3928 if ((*retval != '~' || tail != retval)
3929 && vim_strchr(tail, '$') == NULL
3930 && vim_strchr(retval, '`') == NULL)
3931 retval[len++] = '*';
3932 retval[len] = NUL;
3933 }
3934 }
3935 return retval;
3936}
3937
3938/*
3939 * Must parse the command line so far to work out what context we are in.
3940 * Completion can then be done based on that context.
3941 * This routine sets the variables:
3942 * xp->xp_pattern The start of the pattern to be expanded within
3943 * the command line (ends at the cursor).
3944 * xp->xp_context The type of thing to expand. Will be one of:
3945 *
3946 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3947 * the command line, like an unknown command. Caller
3948 * should beep.
3949 * EXPAND_NOTHING Unrecognised context for completion, use char like
3950 * a normal char, rather than for completion. eg
3951 * :s/^I/
3952 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3953 * it.
3954 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3955 * EXPAND_FILES After command with XFILE set, or after setting
3956 * with P_EXPAND set. eg :e ^I, :w>>^I
3957 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3958 * when we know only directories are of interest. eg
3959 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003960 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3962 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3963 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3964 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3965 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3966 * EXPAND_EVENTS Complete event names
3967 * EXPAND_SYNTAX Complete :syntax command arguments
3968 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3969 * EXPAND_AUGROUP Complete autocommand group names
3970 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3971 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3972 * eg :unmap a^I , :cunab x^I
3973 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3974 * eg :call sub^I
3975 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3976 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3977 * names in expressions, eg :while s^I
3978 * EXPAND_ENV_VARS Complete environment variable names
3979 */
3980 static void
3981set_expand_context(xp)
3982 expand_T *xp;
3983{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003984 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (ccline.cmdfirstc != ':'
3986#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003987 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003988 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989#endif
3990 )
3991 {
3992 xp->xp_context = EXPAND_NOTHING;
3993 return;
3994 }
3995 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3996}
3997
3998 void
3999set_cmd_context(xp, str, len, col)
4000 expand_T *xp;
4001 char_u *str; /* start of command line */
4002 int len; /* length of command line (excl. NUL) */
4003 int col; /* position of cursor */
4004{
4005 int old_char = NUL;
4006 char_u *nextcomm;
4007
4008 /*
4009 * Avoid a UMR warning from Purify, only save the character if it has been
4010 * written before.
4011 */
4012 if (col < len)
4013 old_char = str[col];
4014 str[col] = NUL;
4015 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004016
4017#ifdef FEAT_EVAL
4018 if (ccline.cmdfirstc == '=')
4019 /* pass CMD_SIZE because there is no real command */
4020 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004021 else if (ccline.input_fn)
4022 {
4023 xp->xp_context = ccline.xp_context;
4024 xp->xp_pattern = ccline.cmdbuff;
4025 xp->xp_arg = ccline.xp_arg;
4026 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004027 else
4028#endif
4029 while (nextcomm != NULL)
4030 nextcomm = set_one_cmd_context(xp, nextcomm);
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 str[col] = old_char;
4033}
4034
4035/*
4036 * Expand the command line "str" from context "xp".
4037 * "xp" must have been set by set_cmd_context().
4038 * xp->xp_pattern points into "str", to where the text that is to be expanded
4039 * starts.
4040 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4041 * cursor.
4042 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4043 * key that triggered expansion literally.
4044 * Returns EXPAND_OK otherwise.
4045 */
4046 int
4047expand_cmdline(xp, str, col, matchcount, matches)
4048 expand_T *xp;
4049 char_u *str; /* start of command line */
4050 int col; /* position of cursor */
4051 int *matchcount; /* return: nr of matches */
4052 char_u ***matches; /* return: array of pointers to matches */
4053{
4054 char_u *file_str = NULL;
4055
4056 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4057 {
4058 beep_flush();
4059 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4060 }
4061 if (xp->xp_context == EXPAND_NOTHING)
4062 {
4063 /* Caller can use the character as a normal char instead */
4064 return EXPAND_NOTHING;
4065 }
4066
4067 /* add star to file name, or convert to regexp if not exp. files. */
4068 file_str = addstar(xp->xp_pattern,
4069 (int)(str + col - xp->xp_pattern), xp->xp_context);
4070 if (file_str == NULL)
4071 return EXPAND_UNSUCCESSFUL;
4072
4073 /* find all files that match the description */
4074 if (ExpandFromContext(xp, file_str, matchcount, matches,
4075 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4076 {
4077 *matchcount = 0;
4078 *matches = NULL;
4079 }
4080 vim_free(file_str);
4081
4082 return EXPAND_OK;
4083}
4084
4085#ifdef FEAT_MULTI_LANG
4086/*
4087 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4088 */
4089static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4090
4091 static void
4092cleanup_help_tags(num_file, file)
4093 int num_file;
4094 char_u **file;
4095{
4096 int i, j;
4097 int len;
4098
4099 for (i = 0; i < num_file; ++i)
4100 {
4101 len = (int)STRLEN(file[i]) - 3;
4102 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4103 {
4104 /* Sorting on priority means the same item in another language may
4105 * be anywhere. Search all items for a match up to the "@en". */
4106 for (j = 0; j < num_file; ++j)
4107 if (j != i
4108 && (int)STRLEN(file[j]) == len + 3
4109 && STRNCMP(file[i], file[j], len + 1) == 0)
4110 break;
4111 if (j == num_file)
4112 file[i][len] = NUL;
4113 }
4114 }
4115}
4116#endif
4117
4118/*
4119 * Do the expansion based on xp->xp_context and "pat".
4120 */
4121 static int
4122ExpandFromContext(xp, pat, num_file, file, options)
4123 expand_T *xp;
4124 char_u *pat;
4125 int *num_file;
4126 char_u ***file;
4127 int options;
4128{
4129#ifdef FEAT_CMDL_COMPL
4130 regmatch_T regmatch;
4131#endif
4132 int ret;
4133 int flags;
4134
4135 flags = EW_DIR; /* include directories */
4136 if (options & WILD_LIST_NOTFOUND)
4137 flags |= EW_NOTFOUND;
4138 if (options & WILD_ADD_SLASH)
4139 flags |= EW_ADDSLASH;
4140 if (options & WILD_KEEP_ALL)
4141 flags |= EW_KEEPALL;
4142 if (options & WILD_SILENT)
4143 flags |= EW_SILENT;
4144
4145 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4146 {
4147 /*
4148 * Expand file or directory names.
4149 */
4150 int free_pat = FALSE;
4151 int i;
4152
4153 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4154 * space */
4155 if (xp->xp_backslash != XP_BS_NONE)
4156 {
4157 free_pat = TRUE;
4158 pat = vim_strsave(pat);
4159 for (i = 0; pat[i]; ++i)
4160 if (pat[i] == '\\')
4161 {
4162 if (xp->xp_backslash == XP_BS_THREE
4163 && pat[i + 1] == '\\'
4164 && pat[i + 2] == '\\'
4165 && pat[i + 3] == ' ')
4166 STRCPY(pat + i, pat + i + 3);
4167 if (xp->xp_backslash == XP_BS_ONE
4168 && pat[i + 1] == ' ')
4169 STRCPY(pat + i, pat + i + 1);
4170 }
4171 }
4172
4173 if (xp->xp_context == EXPAND_FILES)
4174 flags |= EW_FILE;
4175 else
4176 flags = (flags | EW_DIR) & ~EW_FILE;
4177 ret = expand_wildcards(1, &pat, num_file, file, flags);
4178 if (free_pat)
4179 vim_free(pat);
4180 return ret;
4181 }
4182
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004183 if (xp->xp_context == EXPAND_SHELLCMD)
4184 {
4185 /*
4186 * Expand shell command.
4187 */
4188 int i;
4189 char_u *path;
4190 int mustfree = FALSE;
4191 garray_T ga;
4192 char_u *buf = alloc(MAXPATHL);
4193 int l;
4194 char_u *s, *e;
4195
4196 if (buf == NULL)
4197 return FAIL;
4198
4199 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4200 * space */
4201 pat = vim_strsave(pat);
4202 for (i = 0; pat[i]; ++i)
4203 if (pat[i] == '\\' && pat[i + 1] == ' ')
4204 STRCPY(pat + i, pat + i + 1);
4205
4206 flags |= EW_FILE | EW_EXEC;
4207 /* For an absolute name we don't use $PATH. */
4208 if ((pat[0] == '.' && (vim_ispathsep(pat[1])
4209 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4210 path = (char_u *)".";
4211 else
4212 path = vim_getenv((char_u *)"PATH", &mustfree);
4213
4214 ga_init2(&ga, (int)sizeof(char *), 10);
4215 for (s = path; *s != NUL; s = e)
4216 {
4217#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4218 e = vim_strchr(s, ';');
4219#else
4220 e = vim_strchr(s, ':');
4221#endif
4222 if (e == NULL)
4223 e = s + STRLEN(s);
4224
4225 l = e - s;
4226 if (l > MAXPATHL - 5)
4227 break;
4228 vim_strncpy(buf, s, l);
4229 add_pathsep(buf);
4230 l = STRLEN(buf);
4231 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4232
4233 /* Expand matches in one directory of $PATH. */
4234 ret = expand_wildcards(1, &buf, num_file, file, flags);
4235 if (ret == OK)
4236 {
4237 if (ga_grow(&ga, *num_file) == FAIL)
4238 FreeWild(*num_file, *file);
4239 else
4240 {
4241 for (i = 0; i < *num_file; ++i)
4242 {
4243 s = (*file)[i];
4244 if (STRLEN(s) > l)
4245 {
4246 /* Remove the path again. */
4247 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4248 ((char_u **)ga.ga_data)[ga.ga_len] = s;
4249 ++ga.ga_len;
4250 }
4251 else
4252 vim_free(s);
4253 }
4254 vim_free(*file);
4255 }
4256 }
4257 if (*e != NUL)
4258 ++e;
4259 }
4260 *file = ga.ga_data;
4261 *num_file = ga.ga_len;
4262
4263 vim_free(buf);
4264 vim_free(pat);
4265 if (mustfree)
4266 vim_free(path);
4267 return ret;
4268 }
4269
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 *file = (char_u **)"";
4271 *num_file = 0;
4272 if (xp->xp_context == EXPAND_HELP)
4273 {
4274 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4275 {
4276#ifdef FEAT_MULTI_LANG
4277 cleanup_help_tags(*num_file, *file);
4278#endif
4279 return OK;
4280 }
4281 return FAIL;
4282 }
4283
4284#ifndef FEAT_CMDL_COMPL
4285 return FAIL;
4286#else
4287 if (xp->xp_context == EXPAND_OLD_SETTING)
4288 return ExpandOldSetting(num_file, file);
4289 if (xp->xp_context == EXPAND_BUFFERS)
4290 return ExpandBufnames(pat, num_file, file, options);
4291 if (xp->xp_context == EXPAND_TAGS
4292 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4293 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4294 if (xp->xp_context == EXPAND_COLORS)
4295 return ExpandRTDir(pat, num_file, file, "colors");
4296 if (xp->xp_context == EXPAND_COMPILER)
4297 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004298# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4299 if (xp->xp_context == EXPAND_USER_LIST)
4300 return ExpandUserList(xp, num_file, file);
4301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302
4303 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4304 if (regmatch.regprog == NULL)
4305 return FAIL;
4306
4307 /* set ignore-case according to p_ic, p_scs and pat */
4308 regmatch.rm_ic = ignorecase(pat);
4309
4310 if (xp->xp_context == EXPAND_SETTINGS
4311 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4312 ret = ExpandSettings(xp, &regmatch, num_file, file);
4313 else if (xp->xp_context == EXPAND_MAPPINGS)
4314 ret = ExpandMappings(&regmatch, num_file, file);
4315# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4316 else if (xp->xp_context == EXPAND_USER_DEFINED)
4317 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4318# endif
4319 else
4320 {
4321 static struct expgen
4322 {
4323 int context;
4324 char_u *((*func)__ARGS((expand_T *, int)));
4325 int ic;
4326 } tab[] =
4327 {
4328 {EXPAND_COMMANDS, get_command_name, FALSE},
4329#ifdef FEAT_USR_CMDS
4330 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4331 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4332 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4333 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4334#endif
4335#ifdef FEAT_EVAL
4336 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4337 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4338 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4339 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4340#endif
4341#ifdef FEAT_MENU
4342 {EXPAND_MENUS, get_menu_name, FALSE},
4343 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4344#endif
4345#ifdef FEAT_SYN_HL
4346 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4347#endif
4348 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4349#ifdef FEAT_AUTOCMD
4350 {EXPAND_EVENTS, get_event_name, TRUE},
4351 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4352#endif
4353#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4354 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4355 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4356#endif
4357 {EXPAND_ENV_VARS, get_env_name, TRUE},
4358 };
4359 int i;
4360
4361 /*
4362 * Find a context in the table and call the ExpandGeneric() with the
4363 * right function to do the expansion.
4364 */
4365 ret = FAIL;
4366 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4367 if (xp->xp_context == tab[i].context)
4368 {
4369 if (tab[i].ic)
4370 regmatch.rm_ic = TRUE;
4371 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4372 break;
4373 }
4374 }
4375
4376 vim_free(regmatch.regprog);
4377
4378 return ret;
4379#endif /* FEAT_CMDL_COMPL */
4380}
4381
4382#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4383/*
4384 * Expand a list of names.
4385 *
4386 * Generic function for command line completion. It calls a function to
4387 * obtain strings, one by one. The strings are matched against a regexp
4388 * program. Matching strings are copied into an array, which is returned.
4389 *
4390 * Returns OK when no problems encountered, FAIL for error (out of memory).
4391 */
4392 int
4393ExpandGeneric(xp, regmatch, num_file, file, func)
4394 expand_T *xp;
4395 regmatch_T *regmatch;
4396 int *num_file;
4397 char_u ***file;
4398 char_u *((*func)__ARGS((expand_T *, int)));
4399 /* returns a string from the list */
4400{
4401 int i;
4402 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004403 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 char_u *str;
4405
4406 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004407 * round == 0: count the number of matching names
4408 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004410 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 {
4412 for (i = 0; ; ++i)
4413 {
4414 str = (*func)(xp, i);
4415 if (str == NULL) /* end of list */
4416 break;
4417 if (*str == NUL) /* skip empty strings */
4418 continue;
4419
4420 if (vim_regexec(regmatch, str, (colnr_T)0))
4421 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004422 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4425 (*file)[count] = str;
4426#ifdef FEAT_MENU
4427 if (func == get_menu_names && str != NULL)
4428 {
4429 /* test for separator added by get_menu_names() */
4430 str += STRLEN(str) - 1;
4431 if (*str == '\001')
4432 *str = '.';
4433 }
4434#endif
4435 }
4436 ++count;
4437 }
4438 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004439 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
4441 if (count == 0)
4442 return OK;
4443 *num_file = count;
4444 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4445 if (*file == NULL)
4446 {
4447 *file = (char_u **)"";
4448 return FAIL;
4449 }
4450 count = 0;
4451 }
4452 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004453
4454 /* Sort the results. */
4455 sort_strings(*file, *num_file);
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 return OK;
4458}
4459
4460# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004461static 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));
4462
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004464 * call "user_expand_func()" to invoke a user defined VimL function and return
4465 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004467 static void *
4468call_user_expand_func(user_expand_func, xp, num_file, file)
4469 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 int *num_file;
4472 char_u ***file;
4473{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004474 char_u keep;
4475 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004478 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004479 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004482 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 *num_file = 0;
4484 *file = NULL;
4485
4486 keep = ccline.cmdbuff[ccline.cmdlen];
4487 ccline.cmdbuff[ccline.cmdlen] = 0;
4488 sprintf((char *)num, "%d", ccline.cmdpos);
4489 args[0] = xp->xp_pattern;
4490 args[1] = ccline.cmdbuff;
4491 args[2] = num;
4492
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004493 /* Save the cmdline, we don't know what the function may do. */
4494 save_ccline = ccline;
4495 ccline.cmdbuff = NULL;
4496 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004498
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004499 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004500
4501 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004505
4506 return ret;
4507}
4508
4509/*
4510 * Expand names with a function defined by the user.
4511 */
4512 static int
4513ExpandUserDefined(xp, regmatch, num_file, file)
4514 expand_T *xp;
4515 regmatch_T *regmatch;
4516 int *num_file;
4517 char_u ***file;
4518{
4519 char_u *retstr;
4520 char_u *s;
4521 char_u *e;
4522 char_u keep;
4523 garray_T ga;
4524
4525 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4526 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 return FAIL;
4528
4529 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004530 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 {
4532 e = vim_strchr(s, '\n');
4533 if (e == NULL)
4534 e = s + STRLEN(s);
4535 keep = *e;
4536 *e = 0;
4537
4538 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4539 {
4540 *e = keep;
4541 if (*e != NUL)
4542 ++e;
4543 continue;
4544 }
4545
4546 if (ga_grow(&ga, 1) == FAIL)
4547 break;
4548
4549 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4550 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 *e = keep;
4553 if (*e != NUL)
4554 ++e;
4555 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004556 vim_free(retstr);
4557 *file = ga.ga_data;
4558 *num_file = ga.ga_len;
4559 return OK;
4560}
4561
4562/*
4563 * Expand names with a list returned by a function defined by the user.
4564 */
4565 static int
4566ExpandUserList(xp, num_file, file)
4567 expand_T *xp;
4568 int *num_file;
4569 char_u ***file;
4570{
4571 list_T *retlist;
4572 listitem_T *li;
4573 garray_T ga;
4574
4575 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4576 if (retlist == NULL)
4577 return FAIL;
4578
4579 ga_init2(&ga, (int)sizeof(char *), 3);
4580 /* Loop over the items in the list. */
4581 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4582 {
4583 if (li->li_tv.v_type != VAR_STRING)
4584 continue; /* Skip non-string items */
4585
4586 if (ga_grow(&ga, 1) == FAIL)
4587 break;
4588
4589 ((char_u **)ga.ga_data)[ga.ga_len] =
4590 vim_strsave(li->li_tv.vval.v_string);
4591 ++ga.ga_len;
4592 }
4593 list_unref(retlist);
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 *file = ga.ga_data;
4596 *num_file = ga.ga_len;
4597 return OK;
4598}
4599#endif
4600
4601/*
4602 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4603 * or compiler names.
4604 */
4605 static int
4606ExpandRTDir(pat, num_file, file, dirname)
4607 char_u *pat;
4608 int *num_file;
4609 char_u ***file;
4610 char *dirname; /* "colors" or "compiler" */
4611{
4612 char_u *all;
4613 char_u *s;
4614 char_u *e;
4615 garray_T ga;
4616
4617 *num_file = 0;
4618 *file = NULL;
4619 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4620 if (s == NULL)
4621 return FAIL;
4622 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4623 all = globpath(p_rtp, s);
4624 vim_free(s);
4625 if (all == NULL)
4626 return FAIL;
4627
4628 ga_init2(&ga, (int)sizeof(char *), 3);
4629 for (s = all; *s != NUL; s = e)
4630 {
4631 e = vim_strchr(s, '\n');
4632 if (e == NULL)
4633 e = s + STRLEN(s);
4634 if (ga_grow(&ga, 1) == FAIL)
4635 break;
4636 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4637 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004638 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (*s == '\n' || vim_ispathsep(*s))
4640 break;
4641 ++s;
4642 ((char_u **)ga.ga_data)[ga.ga_len] =
4643 vim_strnsave(s, (int)(e - s - 4));
4644 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 }
4646 if (*e != NUL)
4647 ++e;
4648 }
4649 vim_free(all);
4650 *file = ga.ga_data;
4651 *num_file = ga.ga_len;
4652 return OK;
4653}
4654
4655#endif
4656
4657#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4658/*
4659 * Expand "file" for all comma-separated directories in "path".
4660 * Returns an allocated string with all matches concatenated, separated by
4661 * newlines. Returns NULL for an error or no matches.
4662 */
4663 char_u *
4664globpath(path, file)
4665 char_u *path;
4666 char_u *file;
4667{
4668 expand_T xpc;
4669 char_u *buf;
4670 garray_T ga;
4671 int i;
4672 int len;
4673 int num_p;
4674 char_u **p;
4675 char_u *cur = NULL;
4676
4677 buf = alloc(MAXPATHL);
4678 if (buf == NULL)
4679 return NULL;
4680
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004681 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004683
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 ga_init2(&ga, 1, 100);
4685
4686 /* Loop over all entries in {path}. */
4687 while (*path != NUL)
4688 {
4689 /* Copy one item of the path to buf[] and concatenate the file name. */
4690 copy_option_part(&path, buf, MAXPATHL, ",");
4691 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4692 {
4693 add_pathsep(buf);
4694 STRCAT(buf, file);
4695 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4696 && num_p > 0)
4697 {
4698 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4699 for (len = 0, i = 0; i < num_p; ++i)
4700 len += (long_u)STRLEN(p[i]) + 1;
4701
4702 /* Concatenate new results to previous ones. */
4703 if (ga_grow(&ga, len) == OK)
4704 {
4705 cur = (char_u *)ga.ga_data + ga.ga_len;
4706 for (i = 0; i < num_p; ++i)
4707 {
4708 STRCPY(cur, p[i]);
4709 cur += STRLEN(p[i]);
4710 *cur++ = '\n';
4711 }
4712 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
4714 FreeWild(num_p, p);
4715 }
4716 }
4717 }
4718 if (cur != NULL)
4719 *--cur = 0; /* Replace trailing newline with NUL */
4720
4721 vim_free(buf);
4722 return (char_u *)ga.ga_data;
4723}
4724
4725#endif
4726
4727#if defined(FEAT_CMDHIST) || defined(PROTO)
4728
4729/*********************************
4730 * Command line history stuff *
4731 *********************************/
4732
4733/*
4734 * Translate a history character to the associated type number.
4735 */
4736 static int
4737hist_char2type(c)
4738 int c;
4739{
4740 if (c == ':')
4741 return HIST_CMD;
4742 if (c == '=')
4743 return HIST_EXPR;
4744 if (c == '@')
4745 return HIST_INPUT;
4746 if (c == '>')
4747 return HIST_DEBUG;
4748 return HIST_SEARCH; /* must be '?' or '/' */
4749}
4750
4751/*
4752 * Table of history names.
4753 * These names are used in :history and various hist...() functions.
4754 * It is sufficient to give the significant prefix of a history name.
4755 */
4756
4757static char *(history_names[]) =
4758{
4759 "cmd",
4760 "search",
4761 "expr",
4762 "input",
4763 "debug",
4764 NULL
4765};
4766
4767/*
4768 * init_history() - Initialize the command line history.
4769 * Also used to re-allocate the history when the size changes.
4770 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004771 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772init_history()
4773{
4774 int newlen; /* new length of history table */
4775 histentry_T *temp;
4776 int i;
4777 int j;
4778 int type;
4779
4780 /*
4781 * If size of history table changed, reallocate it
4782 */
4783 newlen = (int)p_hi;
4784 if (newlen != hislen) /* history length changed */
4785 {
4786 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4787 {
4788 if (newlen)
4789 {
4790 temp = (histentry_T *)lalloc(
4791 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4792 if (temp == NULL) /* out of memory! */
4793 {
4794 if (type == 0) /* first one: just keep the old length */
4795 {
4796 newlen = hislen;
4797 break;
4798 }
4799 /* Already changed one table, now we can only have zero
4800 * length for all tables. */
4801 newlen = 0;
4802 type = -1;
4803 continue;
4804 }
4805 }
4806 else
4807 temp = NULL;
4808 if (newlen == 0 || temp != NULL)
4809 {
4810 if (hisidx[type] < 0) /* there are no entries yet */
4811 {
4812 for (i = 0; i < newlen; ++i)
4813 {
4814 temp[i].hisnum = 0;
4815 temp[i].hisstr = NULL;
4816 }
4817 }
4818 else if (newlen > hislen) /* array becomes bigger */
4819 {
4820 for (i = 0; i <= hisidx[type]; ++i)
4821 temp[i] = history[type][i];
4822 j = i;
4823 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4824 {
4825 temp[i].hisnum = 0;
4826 temp[i].hisstr = NULL;
4827 }
4828 for ( ; j < hislen; ++i, ++j)
4829 temp[i] = history[type][j];
4830 }
4831 else /* array becomes smaller or 0 */
4832 {
4833 j = hisidx[type];
4834 for (i = newlen - 1; ; --i)
4835 {
4836 if (i >= 0) /* copy newest entries */
4837 temp[i] = history[type][j];
4838 else /* remove older entries */
4839 vim_free(history[type][j].hisstr);
4840 if (--j < 0)
4841 j = hislen - 1;
4842 if (j == hisidx[type])
4843 break;
4844 }
4845 hisidx[type] = newlen - 1;
4846 }
4847 vim_free(history[type]);
4848 history[type] = temp;
4849 }
4850 }
4851 hislen = newlen;
4852 }
4853}
4854
4855/*
4856 * Check if command line 'str' is already in history.
4857 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4858 */
4859 static int
4860in_history(type, str, move_to_front)
4861 int type;
4862 char_u *str;
4863 int move_to_front; /* Move the entry to the front if it exists */
4864{
4865 int i;
4866 int last_i = -1;
4867
4868 if (hisidx[type] < 0)
4869 return FALSE;
4870 i = hisidx[type];
4871 do
4872 {
4873 if (history[type][i].hisstr == NULL)
4874 return FALSE;
4875 if (STRCMP(str, history[type][i].hisstr) == 0)
4876 {
4877 if (!move_to_front)
4878 return TRUE;
4879 last_i = i;
4880 break;
4881 }
4882 if (--i < 0)
4883 i = hislen - 1;
4884 } while (i != hisidx[type]);
4885
4886 if (last_i >= 0)
4887 {
4888 str = history[type][i].hisstr;
4889 while (i != hisidx[type])
4890 {
4891 if (++i >= hislen)
4892 i = 0;
4893 history[type][last_i] = history[type][i];
4894 last_i = i;
4895 }
4896 history[type][i].hisstr = str;
4897 history[type][i].hisnum = ++hisnum[type];
4898 return TRUE;
4899 }
4900 return FALSE;
4901}
4902
4903/*
4904 * Convert history name (from table above) to its HIST_ equivalent.
4905 * When "name" is empty, return "cmd" history.
4906 * Returns -1 for unknown history name.
4907 */
4908 int
4909get_histtype(name)
4910 char_u *name;
4911{
4912 int i;
4913 int len = (int)STRLEN(name);
4914
4915 /* No argument: use current history. */
4916 if (len == 0)
4917 return hist_char2type(ccline.cmdfirstc);
4918
4919 for (i = 0; history_names[i] != NULL; ++i)
4920 if (STRNICMP(name, history_names[i], len) == 0)
4921 return i;
4922
4923 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4924 return hist_char2type(name[0]);
4925
4926 return -1;
4927}
4928
4929static int last_maptick = -1; /* last seen maptick */
4930
4931/*
4932 * Add the given string to the given history. If the string is already in the
4933 * history then it is moved to the front. "histype" may be one of he HIST_
4934 * values.
4935 */
4936 void
4937add_to_history(histype, new_entry, in_map, sep)
4938 int histype;
4939 char_u *new_entry;
4940 int in_map; /* consider maptick when inside a mapping */
4941 int sep; /* separator character used (search hist) */
4942{
4943 histentry_T *hisptr;
4944 int len;
4945
4946 if (hislen == 0) /* no history */
4947 return;
4948
4949 /*
4950 * Searches inside the same mapping overwrite each other, so that only
4951 * the last line is kept. Be careful not to remove a line that was moved
4952 * down, only lines that were added.
4953 */
4954 if (histype == HIST_SEARCH && in_map)
4955 {
4956 if (maptick == last_maptick)
4957 {
4958 /* Current line is from the same mapping, remove it */
4959 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4960 vim_free(hisptr->hisstr);
4961 hisptr->hisstr = NULL;
4962 hisptr->hisnum = 0;
4963 --hisnum[histype];
4964 if (--hisidx[HIST_SEARCH] < 0)
4965 hisidx[HIST_SEARCH] = hislen - 1;
4966 }
4967 last_maptick = -1;
4968 }
4969 if (!in_history(histype, new_entry, TRUE))
4970 {
4971 if (++hisidx[histype] == hislen)
4972 hisidx[histype] = 0;
4973 hisptr = &history[histype][hisidx[histype]];
4974 vim_free(hisptr->hisstr);
4975
4976 /* Store the separator after the NUL of the string. */
4977 len = STRLEN(new_entry);
4978 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4979 if (hisptr->hisstr != NULL)
4980 hisptr->hisstr[len + 1] = sep;
4981
4982 hisptr->hisnum = ++hisnum[histype];
4983 if (histype == HIST_SEARCH && in_map)
4984 last_maptick = maptick;
4985 }
4986}
4987
4988#if defined(FEAT_EVAL) || defined(PROTO)
4989
4990/*
4991 * Get identifier of newest history entry.
4992 * "histype" may be one of the HIST_ values.
4993 */
4994 int
4995get_history_idx(histype)
4996 int histype;
4997{
4998 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4999 || hisidx[histype] < 0)
5000 return -1;
5001
5002 return history[histype][hisidx[histype]].hisnum;
5003}
5004
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005005static struct cmdline_info *get_ccline_ptr __ARGS((void));
5006
5007/*
5008 * Get pointer to the command line info to use. cmdline_paste() may clear
5009 * ccline and put the previous value in prev_ccline.
5010 */
5011 static struct cmdline_info *
5012get_ccline_ptr()
5013{
5014 if ((State & CMDLINE) == 0)
5015 return NULL;
5016 if (ccline.cmdbuff != NULL)
5017 return &ccline;
5018 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5019 return &prev_ccline;
5020 return NULL;
5021}
5022
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023/*
5024 * Get the current command line in allocated memory.
5025 * Only works when the command line is being edited.
5026 * Returns NULL when something is wrong.
5027 */
5028 char_u *
5029get_cmdline_str()
5030{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005031 struct cmdline_info *p = get_ccline_ptr();
5032
5033 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005035 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036}
5037
5038/*
5039 * Get the current command line position, counted in bytes.
5040 * Zero is the first position.
5041 * Only works when the command line is being edited.
5042 * Returns -1 when something is wrong.
5043 */
5044 int
5045get_cmdline_pos()
5046{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005047 struct cmdline_info *p = get_ccline_ptr();
5048
5049 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005051 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052}
5053
5054/*
5055 * Set the command line byte position to "pos". Zero is the first position.
5056 * Only works when the command line is being edited.
5057 * Returns 1 when failed, 0 when OK.
5058 */
5059 int
5060set_cmdline_pos(pos)
5061 int pos;
5062{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005063 struct cmdline_info *p = get_ccline_ptr();
5064
5065 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 return 1;
5067
5068 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5069 * changed the command line. */
5070 if (pos < 0)
5071 new_cmdpos = 0;
5072 else
5073 new_cmdpos = pos;
5074 return 0;
5075}
5076
5077/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005078 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005079 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005080 * Only works when the command line is being edited.
5081 * Returns NUL when something is wrong.
5082 */
5083 int
5084get_cmdline_type()
5085{
5086 struct cmdline_info *p = get_ccline_ptr();
5087
5088 if (p == NULL)
5089 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005090 if (p->cmdfirstc == NUL)
5091 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005092 return p->cmdfirstc;
5093}
5094
5095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 * Calculate history index from a number:
5097 * num > 0: seen as identifying number of a history entry
5098 * num < 0: relative position in history wrt newest entry
5099 * "histype" may be one of the HIST_ values.
5100 */
5101 static int
5102calc_hist_idx(histype, num)
5103 int histype;
5104 int num;
5105{
5106 int i;
5107 histentry_T *hist;
5108 int wrapped = FALSE;
5109
5110 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5111 || (i = hisidx[histype]) < 0 || num == 0)
5112 return -1;
5113
5114 hist = history[histype];
5115 if (num > 0)
5116 {
5117 while (hist[i].hisnum > num)
5118 if (--i < 0)
5119 {
5120 if (wrapped)
5121 break;
5122 i += hislen;
5123 wrapped = TRUE;
5124 }
5125 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5126 return i;
5127 }
5128 else if (-num <= hislen)
5129 {
5130 i += num + 1;
5131 if (i < 0)
5132 i += hislen;
5133 if (hist[i].hisstr != NULL)
5134 return i;
5135 }
5136 return -1;
5137}
5138
5139/*
5140 * Get a history entry by its index.
5141 * "histype" may be one of the HIST_ values.
5142 */
5143 char_u *
5144get_history_entry(histype, idx)
5145 int histype;
5146 int idx;
5147{
5148 idx = calc_hist_idx(histype, idx);
5149 if (idx >= 0)
5150 return history[histype][idx].hisstr;
5151 else
5152 return (char_u *)"";
5153}
5154
5155/*
5156 * Clear all entries of a history.
5157 * "histype" may be one of the HIST_ values.
5158 */
5159 int
5160clr_history(histype)
5161 int histype;
5162{
5163 int i;
5164 histentry_T *hisptr;
5165
5166 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5167 {
5168 hisptr = history[histype];
5169 for (i = hislen; i--;)
5170 {
5171 vim_free(hisptr->hisstr);
5172 hisptr->hisnum = 0;
5173 hisptr++->hisstr = NULL;
5174 }
5175 hisidx[histype] = -1; /* mark history as cleared */
5176 hisnum[histype] = 0; /* reset identifier counter */
5177 return OK;
5178 }
5179 return FAIL;
5180}
5181
5182/*
5183 * Remove all entries matching {str} from a history.
5184 * "histype" may be one of the HIST_ values.
5185 */
5186 int
5187del_history_entry(histype, str)
5188 int histype;
5189 char_u *str;
5190{
5191 regmatch_T regmatch;
5192 histentry_T *hisptr;
5193 int idx;
5194 int i;
5195 int last;
5196 int found = FALSE;
5197
5198 regmatch.regprog = NULL;
5199 regmatch.rm_ic = FALSE; /* always match case */
5200 if (hislen != 0
5201 && histype >= 0
5202 && histype < HIST_COUNT
5203 && *str != NUL
5204 && (idx = hisidx[histype]) >= 0
5205 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5206 != NULL)
5207 {
5208 i = last = idx;
5209 do
5210 {
5211 hisptr = &history[histype][i];
5212 if (hisptr->hisstr == NULL)
5213 break;
5214 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5215 {
5216 found = TRUE;
5217 vim_free(hisptr->hisstr);
5218 hisptr->hisstr = NULL;
5219 hisptr->hisnum = 0;
5220 }
5221 else
5222 {
5223 if (i != last)
5224 {
5225 history[histype][last] = *hisptr;
5226 hisptr->hisstr = NULL;
5227 hisptr->hisnum = 0;
5228 }
5229 if (--last < 0)
5230 last += hislen;
5231 }
5232 if (--i < 0)
5233 i += hislen;
5234 } while (i != idx);
5235 if (history[histype][idx].hisstr == NULL)
5236 hisidx[histype] = -1;
5237 }
5238 vim_free(regmatch.regprog);
5239 return found;
5240}
5241
5242/*
5243 * Remove an indexed entry from a history.
5244 * "histype" may be one of the HIST_ values.
5245 */
5246 int
5247del_history_idx(histype, idx)
5248 int histype;
5249 int idx;
5250{
5251 int i, j;
5252
5253 i = calc_hist_idx(histype, idx);
5254 if (i < 0)
5255 return FALSE;
5256 idx = hisidx[histype];
5257 vim_free(history[histype][i].hisstr);
5258
5259 /* When deleting the last added search string in a mapping, reset
5260 * last_maptick, so that the last added search string isn't deleted again.
5261 */
5262 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5263 last_maptick = -1;
5264
5265 while (i != idx)
5266 {
5267 j = (i + 1) % hislen;
5268 history[histype][i] = history[histype][j];
5269 i = j;
5270 }
5271 history[histype][i].hisstr = NULL;
5272 history[histype][i].hisnum = 0;
5273 if (--i < 0)
5274 i += hislen;
5275 hisidx[histype] = i;
5276 return TRUE;
5277}
5278
5279#endif /* FEAT_EVAL */
5280
5281#if defined(FEAT_CRYPT) || defined(PROTO)
5282/*
5283 * Very specific function to remove the value in ":set key=val" from the
5284 * history.
5285 */
5286 void
5287remove_key_from_history()
5288{
5289 char_u *p;
5290 int i;
5291
5292 i = hisidx[HIST_CMD];
5293 if (i < 0)
5294 return;
5295 p = history[HIST_CMD][i].hisstr;
5296 if (p != NULL)
5297 for ( ; *p; ++p)
5298 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5299 {
5300 p = vim_strchr(p + 3, '=');
5301 if (p == NULL)
5302 break;
5303 ++p;
5304 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5305 if (p[i] == '\\' && p[i + 1])
5306 ++i;
5307 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5308 --p;
5309 }
5310}
5311#endif
5312
5313#endif /* FEAT_CMDHIST */
5314
5315#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5316/*
5317 * Get indices "num1,num2" that specify a range within a list (not a range of
5318 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5319 * Returns OK if parsed successfully, otherwise FAIL.
5320 */
5321 int
5322get_list_range(str, num1, num2)
5323 char_u **str;
5324 int *num1;
5325 int *num2;
5326{
5327 int len;
5328 int first = FALSE;
5329 long num;
5330
5331 *str = skipwhite(*str);
5332 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5333 {
5334 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5335 *str += len;
5336 *num1 = (int)num;
5337 first = TRUE;
5338 }
5339 *str = skipwhite(*str);
5340 if (**str == ',') /* parse "to" part of range */
5341 {
5342 *str = skipwhite(*str + 1);
5343 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5344 if (len > 0)
5345 {
5346 *num2 = (int)num;
5347 *str = skipwhite(*str + len);
5348 }
5349 else if (!first) /* no number given at all */
5350 return FAIL;
5351 }
5352 else if (first) /* only one number given */
5353 *num2 = *num1;
5354 return OK;
5355}
5356#endif
5357
5358#if defined(FEAT_CMDHIST) || defined(PROTO)
5359/*
5360 * :history command - print a history
5361 */
5362 void
5363ex_history(eap)
5364 exarg_T *eap;
5365{
5366 histentry_T *hist;
5367 int histype1 = HIST_CMD;
5368 int histype2 = HIST_CMD;
5369 int hisidx1 = 1;
5370 int hisidx2 = -1;
5371 int idx;
5372 int i, j, k;
5373 char_u *end;
5374 char_u *arg = eap->arg;
5375
5376 if (hislen == 0)
5377 {
5378 MSG(_("'history' option is zero"));
5379 return;
5380 }
5381
5382 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5383 {
5384 end = arg;
5385 while (ASCII_ISALPHA(*end)
5386 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5387 end++;
5388 i = *end;
5389 *end = NUL;
5390 histype1 = get_histtype(arg);
5391 if (histype1 == -1)
5392 {
5393 if (STRICMP(arg, "all") == 0)
5394 {
5395 histype1 = 0;
5396 histype2 = HIST_COUNT-1;
5397 }
5398 else
5399 {
5400 *end = i;
5401 EMSG(_(e_trailing));
5402 return;
5403 }
5404 }
5405 else
5406 histype2 = histype1;
5407 *end = i;
5408 }
5409 else
5410 end = arg;
5411 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5412 {
5413 EMSG(_(e_trailing));
5414 return;
5415 }
5416
5417 for (; !got_int && histype1 <= histype2; ++histype1)
5418 {
5419 STRCPY(IObuff, "\n # ");
5420 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5421 MSG_PUTS_TITLE(IObuff);
5422 idx = hisidx[histype1];
5423 hist = history[histype1];
5424 j = hisidx1;
5425 k = hisidx2;
5426 if (j < 0)
5427 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5428 if (k < 0)
5429 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5430 if (idx >= 0 && j <= k)
5431 for (i = idx + 1; !got_int; ++i)
5432 {
5433 if (i == hislen)
5434 i = 0;
5435 if (hist[i].hisstr != NULL
5436 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5437 {
5438 msg_putchar('\n');
5439 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5440 hist[i].hisnum);
5441 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5442 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5443 (int)Columns - 10);
5444 else
5445 STRCAT(IObuff, hist[i].hisstr);
5446 msg_outtrans(IObuff);
5447 out_flush();
5448 }
5449 if (i == idx)
5450 break;
5451 }
5452 }
5453}
5454#endif
5455
5456#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5457static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5458static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5459static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5460static int viminfo_add_at_front = FALSE;
5461
5462static int hist_type2char __ARGS((int type, int use_question));
5463
5464/*
5465 * Translate a history type number to the associated character.
5466 */
5467 static int
5468hist_type2char(type, use_question)
5469 int type;
5470 int use_question; /* use '?' instead of '/' */
5471{
5472 if (type == HIST_CMD)
5473 return ':';
5474 if (type == HIST_SEARCH)
5475 {
5476 if (use_question)
5477 return '?';
5478 else
5479 return '/';
5480 }
5481 if (type == HIST_EXPR)
5482 return '=';
5483 return '@';
5484}
5485
5486/*
5487 * Prepare for reading the history from the viminfo file.
5488 * This allocates history arrays to store the read history lines.
5489 */
5490 void
5491prepare_viminfo_history(asklen)
5492 int asklen;
5493{
5494 int i;
5495 int num;
5496 int type;
5497 int len;
5498
5499 init_history();
5500 viminfo_add_at_front = (asklen != 0);
5501 if (asklen > hislen)
5502 asklen = hislen;
5503
5504 for (type = 0; type < HIST_COUNT; ++type)
5505 {
5506 /*
5507 * Count the number of empty spaces in the history list. If there are
5508 * more spaces available than we request, then fill them up.
5509 */
5510 for (i = 0, num = 0; i < hislen; i++)
5511 if (history[type][i].hisstr == NULL)
5512 num++;
5513 len = asklen;
5514 if (num > len)
5515 len = num;
5516 if (len <= 0)
5517 viminfo_history[type] = NULL;
5518 else
5519 viminfo_history[type] =
5520 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5521 if (viminfo_history[type] == NULL)
5522 len = 0;
5523 viminfo_hislen[type] = len;
5524 viminfo_hisidx[type] = 0;
5525 }
5526}
5527
5528/*
5529 * Accept a line from the viminfo, store it in the history array when it's
5530 * new.
5531 */
5532 int
5533read_viminfo_history(virp)
5534 vir_T *virp;
5535{
5536 int type;
5537 long_u len;
5538 char_u *val;
5539 char_u *p;
5540
5541 type = hist_char2type(virp->vir_line[0]);
5542 if (viminfo_hisidx[type] < viminfo_hislen[type])
5543 {
5544 val = viminfo_readstring(virp, 1, TRUE);
5545 if (val != NULL && *val != NUL)
5546 {
5547 if (!in_history(type, val + (type == HIST_SEARCH),
5548 viminfo_add_at_front))
5549 {
5550 /* Need to re-allocate to append the separator byte. */
5551 len = STRLEN(val);
5552 p = lalloc(len + 2, TRUE);
5553 if (p != NULL)
5554 {
5555 if (type == HIST_SEARCH)
5556 {
5557 /* Search entry: Move the separator from the first
5558 * column to after the NUL. */
5559 mch_memmove(p, val + 1, (size_t)len);
5560 p[len] = (*val == ' ' ? NUL : *val);
5561 }
5562 else
5563 {
5564 /* Not a search entry: No separator in the viminfo
5565 * file, add a NUL separator. */
5566 mch_memmove(p, val, (size_t)len + 1);
5567 p[len + 1] = NUL;
5568 }
5569 viminfo_history[type][viminfo_hisidx[type]++] = p;
5570 }
5571 }
5572 }
5573 vim_free(val);
5574 }
5575 return viminfo_readline(virp);
5576}
5577
5578 void
5579finish_viminfo_history()
5580{
5581 int idx;
5582 int i;
5583 int type;
5584
5585 for (type = 0; type < HIST_COUNT; ++type)
5586 {
5587 if (history[type] == NULL)
5588 return;
5589 idx = hisidx[type] + viminfo_hisidx[type];
5590 if (idx >= hislen)
5591 idx -= hislen;
5592 else if (idx < 0)
5593 idx = hislen - 1;
5594 if (viminfo_add_at_front)
5595 hisidx[type] = idx;
5596 else
5597 {
5598 if (hisidx[type] == -1)
5599 hisidx[type] = hislen - 1;
5600 do
5601 {
5602 if (history[type][idx].hisstr != NULL)
5603 break;
5604 if (++idx == hislen)
5605 idx = 0;
5606 } while (idx != hisidx[type]);
5607 if (idx != hisidx[type] && --idx < 0)
5608 idx = hislen - 1;
5609 }
5610 for (i = 0; i < viminfo_hisidx[type]; i++)
5611 {
5612 vim_free(history[type][idx].hisstr);
5613 history[type][idx].hisstr = viminfo_history[type][i];
5614 if (--idx < 0)
5615 idx = hislen - 1;
5616 }
5617 idx += 1;
5618 idx %= hislen;
5619 for (i = 0; i < viminfo_hisidx[type]; i++)
5620 {
5621 history[type][idx++].hisnum = ++hisnum[type];
5622 idx %= hislen;
5623 }
5624 vim_free(viminfo_history[type]);
5625 viminfo_history[type] = NULL;
5626 }
5627}
5628
5629 void
5630write_viminfo_history(fp)
5631 FILE *fp;
5632{
5633 int i;
5634 int type;
5635 int num_saved;
5636 char_u *p;
5637 int c;
5638
5639 init_history();
5640 if (hislen == 0)
5641 return;
5642 for (type = 0; type < HIST_COUNT; ++type)
5643 {
5644 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5645 if (num_saved == 0)
5646 continue;
5647 if (num_saved < 0) /* Use default */
5648 num_saved = hislen;
5649 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5650 type == HIST_CMD ? _("Command Line") :
5651 type == HIST_SEARCH ? _("Search String") :
5652 type == HIST_EXPR ? _("Expression") :
5653 _("Input Line"));
5654 if (num_saved > hislen)
5655 num_saved = hislen;
5656 i = hisidx[type];
5657 if (i >= 0)
5658 while (num_saved--)
5659 {
5660 p = history[type][i].hisstr;
5661 if (p != NULL)
5662 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005663 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 /* For the search history: put the separator in the second
5665 * column; use a space if there isn't one. */
5666 if (type == HIST_SEARCH)
5667 {
5668 c = p[STRLEN(p) + 1];
5669 putc(c == NUL ? ' ' : c, fp);
5670 }
5671 viminfo_writestring(fp, p);
5672 }
5673 if (--i < 0)
5674 i = hislen - 1;
5675 }
5676 }
5677}
5678#endif /* FEAT_VIMINFO */
5679
5680#if defined(FEAT_FKMAP) || defined(PROTO)
5681/*
5682 * Write a character at the current cursor+offset position.
5683 * It is directly written into the command buffer block.
5684 */
5685 void
5686cmd_pchar(c, offset)
5687 int c, offset;
5688{
5689 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5690 {
5691 EMSG(_("E198: cmd_pchar beyond the command length"));
5692 return;
5693 }
5694 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5695 ccline.cmdbuff[ccline.cmdlen] = NUL;
5696}
5697
5698 int
5699cmd_gchar(offset)
5700 int offset;
5701{
5702 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5703 {
5704 /* EMSG(_("cmd_gchar beyond the command length")); */
5705 return NUL;
5706 }
5707 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5708}
5709#endif
5710
5711#if defined(FEAT_CMDWIN) || defined(PROTO)
5712/*
5713 * Open a window on the current command line and history. Allow editing in
5714 * the window. Returns when the window is closed.
5715 * Returns:
5716 * CR if the command is to be executed
5717 * Ctrl_C if it is to be abandoned
5718 * K_IGNORE if editing continues
5719 */
5720 static int
5721ex_window()
5722{
5723 struct cmdline_info save_ccline;
5724 buf_T *old_curbuf = curbuf;
5725 win_T *old_curwin = curwin;
5726 buf_T *bp;
5727 win_T *wp;
5728 int i;
5729 linenr_T lnum;
5730 int histtype;
5731 garray_T winsizes;
5732 char_u typestr[2];
5733 int save_restart_edit = restart_edit;
5734 int save_State = State;
5735 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005736#ifdef FEAT_RIGHTLEFT
5737 int save_cmdmsg_rl = cmdmsg_rl;
5738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
5740 /* Can't do this recursively. Can't do it when typing a password. */
5741 if (cmdwin_type != 0
5742# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5743 || cmdline_star > 0
5744# endif
5745 )
5746 {
5747 beep_flush();
5748 return K_IGNORE;
5749 }
5750
5751 /* Save current window sizes. */
5752 win_size_save(&winsizes);
5753
5754# ifdef FEAT_AUTOCMD
5755 /* Don't execute autocommands while creating the window. */
5756 ++autocmd_block;
5757# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005758 /* don't use a new tab page */
5759 cmdmod.tab = 0;
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 /* Create a window for the command-line buffer. */
5762 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5763 {
5764 beep_flush();
5765 return K_IGNORE;
5766 }
5767 cmdwin_type = ccline.cmdfirstc;
5768 if (cmdwin_type == NUL)
5769 cmdwin_type = '-';
5770
5771 /* Create the command-line buffer empty. */
5772 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5773 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5774 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5775 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5776 curbuf->b_p_ma = TRUE;
5777# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005778 curwin->w_p_rl = cmdmsg_rl;
5779 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780# endif
5781# ifdef FEAT_SCROLLBIND
5782 curwin->w_p_scb = FALSE;
5783# endif
5784
5785# ifdef FEAT_AUTOCMD
5786 /* Do execute autocommands for setting the filetype (load syntax). */
5787 --autocmd_block;
5788# endif
5789
Bram Moolenaar46152342005-09-07 21:18:43 +00005790 /* Showing the prompt may have set need_wait_return, reset it. */
5791 need_wait_return = FALSE;
5792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 histtype = hist_char2type(ccline.cmdfirstc);
5794 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5795 {
5796 if (p_wc == TAB)
5797 {
5798 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5799 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5800 }
5801 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5802 }
5803
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005804 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5805 * sets 'textwidth' to 78). */
5806 curbuf->b_p_tw = 0;
5807
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 /* Fill the buffer with the history. */
5809 init_history();
5810 if (hislen > 0)
5811 {
5812 i = hisidx[histtype];
5813 if (i >= 0)
5814 {
5815 lnum = 0;
5816 do
5817 {
5818 if (++i == hislen)
5819 i = 0;
5820 if (history[histtype][i].hisstr != NULL)
5821 ml_append(lnum++, history[histtype][i].hisstr,
5822 (colnr_T)0, FALSE);
5823 }
5824 while (i != hisidx[histtype]);
5825 }
5826 }
5827
5828 /* Replace the empty last line with the current command-line and put the
5829 * cursor there. */
5830 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5831 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5832 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005833 changed_line_abv_curs();
5834 invalidate_botline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 redraw_later(NOT_VALID);
5836
5837 /* Save the command line info, can be used recursively. */
5838 save_ccline = ccline;
5839 ccline.cmdbuff = NULL;
5840 ccline.cmdprompt = NULL;
5841
5842 /* No Ex mode here! */
5843 exmode_active = 0;
5844
5845 State = NORMAL;
5846# ifdef FEAT_MOUSE
5847 setmouse();
5848# endif
5849
5850# ifdef FEAT_AUTOCMD
5851 /* Trigger CmdwinEnter autocommands. */
5852 typestr[0] = cmdwin_type;
5853 typestr[1] = NUL;
5854 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5855# endif
5856
5857 i = RedrawingDisabled;
5858 RedrawingDisabled = 0;
5859
5860 /*
5861 * Call the main loop until <CR> or CTRL-C is typed.
5862 */
5863 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005864 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
5866 RedrawingDisabled = i;
5867
5868# ifdef FEAT_AUTOCMD
5869 /* Trigger CmdwinLeave autocommands. */
5870 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5871# endif
5872
5873 /* Restore the comand line info. */
5874 ccline = save_ccline;
5875 cmdwin_type = 0;
5876
5877 exmode_active = save_exmode;
5878
5879 /* Safety check: The old window or buffer was deleted: It's a a bug when
5880 * this happens! */
5881 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5882 {
5883 cmdwin_result = Ctrl_C;
5884 EMSG(_("E199: Active window or buffer deleted"));
5885 }
5886 else
5887 {
5888# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5889 /* autocmds may abort script processing */
5890 if (aborting() && cmdwin_result != K_IGNORE)
5891 cmdwin_result = Ctrl_C;
5892# endif
5893 /* Set the new command line from the cmdline buffer. */
5894 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00005895 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {
Bram Moolenaar46152342005-09-07 21:18:43 +00005897 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
5898
5899 if (histtype == HIST_CMD)
5900 {
5901 /* Execute the command directly. */
5902 ccline.cmdbuff = vim_strsave((char_u *)p);
5903 cmdwin_result = CAR;
5904 }
5905 else
5906 {
5907 /* First need to cancel what we were doing. */
5908 ccline.cmdbuff = NULL;
5909 stuffcharReadbuff(':');
5910 stuffReadbuff((char_u *)p);
5911 stuffcharReadbuff(CAR);
5912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
5914 else if (cmdwin_result == K_XF2) /* :qa typed */
5915 {
5916 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5917 cmdwin_result = CAR;
5918 }
5919 else
5920 ccline.cmdbuff = vim_strsave(ml_get_curline());
5921 if (ccline.cmdbuff == NULL)
5922 cmdwin_result = Ctrl_C;
5923 else
5924 {
5925 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5926 ccline.cmdbufflen = ccline.cmdlen + 1;
5927 ccline.cmdpos = curwin->w_cursor.col;
5928 if (ccline.cmdpos > ccline.cmdlen)
5929 ccline.cmdpos = ccline.cmdlen;
5930 if (cmdwin_result == K_IGNORE)
5931 {
5932 set_cmdspos_cursor();
5933 redrawcmd();
5934 }
5935 }
5936
5937# ifdef FEAT_AUTOCMD
5938 /* Don't execute autocommands while deleting the window. */
5939 ++autocmd_block;
5940# endif
5941 wp = curwin;
5942 bp = curbuf;
5943 win_goto(old_curwin);
5944 win_close(wp, TRUE);
5945 close_buffer(NULL, bp, DOBUF_WIPE);
5946
5947 /* Restore window sizes. */
5948 win_size_restore(&winsizes);
5949
5950# ifdef FEAT_AUTOCMD
5951 --autocmd_block;
5952# endif
5953 }
5954
5955 ga_clear(&winsizes);
5956 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00005957# ifdef FEAT_RIGHTLEFT
5958 cmdmsg_rl = save_cmdmsg_rl;
5959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
5961 State = save_State;
5962# ifdef FEAT_MOUSE
5963 setmouse();
5964# endif
5965
5966 return cmdwin_result;
5967}
5968#endif /* FEAT_CMDWIN */
5969
5970/*
5971 * Used for commands that either take a simple command string argument, or:
5972 * cmd << endmarker
5973 * {script}
5974 * endmarker
5975 * Returns a pointer to allocated memory with {script} or NULL.
5976 */
5977 char_u *
5978script_get(eap, cmd)
5979 exarg_T *eap;
5980 char_u *cmd;
5981{
5982 char_u *theline;
5983 char *end_pattern = NULL;
5984 char dot[] = ".";
5985 garray_T ga;
5986
5987 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5988 return NULL;
5989
5990 ga_init2(&ga, 1, 0x400);
5991
5992 if (cmd[2] != NUL)
5993 end_pattern = (char *)skipwhite(cmd + 2);
5994 else
5995 end_pattern = dot;
5996
5997 for (;;)
5998 {
5999 theline = eap->getline(
6000#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006001 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002#endif
6003 NUL, eap->cookie, 0);
6004
6005 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6006 break;
6007
6008 ga_concat(&ga, theline);
6009 ga_append(&ga, '\n');
6010 vim_free(theline);
6011 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006012 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013
6014 return (char_u *)ga.ga_data;
6015}