blob: 58cfec8439d805f8fbdbc29faadc4c742883d373 [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
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000106static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
108# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
109static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000110static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111# endif
112#endif
113
114#ifdef FEAT_CMDWIN
115static int ex_window __ARGS((void));
116#endif
117
118/*
119 * getcmdline() - accept a command line starting with firstc.
120 *
121 * firstc == ':' get ":" command line.
122 * firstc == '/' or '?' get search pattern
123 * firstc == '=' get expression
124 * firstc == '@' get text for input() function
125 * firstc == '>' get text for debug mode
126 * firstc == NUL get text for :insert command
127 * firstc == -1 like NUL, and break on CTRL-C
128 *
129 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
130 * command line.
131 *
132 * Careful: getcmdline() can be called recursively!
133 *
134 * Return pointer to allocated string if there is a commandline, NULL
135 * otherwise.
136 */
137/*ARGSUSED*/
138 char_u *
139getcmdline(firstc, count, indent)
140 int firstc;
141 long count; /* only used for incremental search */
142 int indent; /* indent for inside conditionals */
143{
144 int c;
145 int i;
146 int j;
147 int gotesc = FALSE; /* TRUE when <ESC> just typed */
148 int do_abbr; /* when TRUE check for abbr. */
149#ifdef FEAT_CMDHIST
150 char_u *lookfor = NULL; /* string to match */
151 int hiscnt; /* current history line in use */
152 int histype; /* history type to be used */
153#endif
154#ifdef FEAT_SEARCH_EXTRA
155 pos_T old_cursor;
156 colnr_T old_curswant;
157 colnr_T old_leftcol;
158 linenr_T old_topline;
159# ifdef FEAT_DIFF
160 int old_topfill;
161# endif
162 linenr_T old_botline;
163 int did_incsearch = FALSE;
164 int incsearch_postponed = FALSE;
165#endif
166 int did_wild_list = FALSE; /* did wild_list() recently */
167 int wim_index = 0; /* index in wim_flags[] */
168 int res;
169 int save_msg_scroll = msg_scroll;
170 int save_State = State; /* remember State when called */
171 int some_key_typed = FALSE; /* one of the keys was typed */
172#ifdef FEAT_MOUSE
173 /* mouse drag and release events are ignored, unless they are
174 * preceded with a mouse down event */
175 int ignore_drag_release = TRUE;
176#endif
177#ifdef FEAT_EVAL
178 int break_ctrl_c = FALSE;
179#endif
180 expand_T xpc;
181 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000182#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
183 /* Everything that may work recursively should save and restore the
184 * current command line in save_ccline. That includes update_screen(), a
185 * custom status line may invoke ":normal". */
186 struct cmdline_info save_ccline;
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189#ifdef FEAT_SNIFF
190 want_sniff_request = 0;
191#endif
192#ifdef FEAT_EVAL
193 if (firstc == -1)
194 {
195 firstc = NUL;
196 break_ctrl_c = TRUE;
197 }
198#endif
199#ifdef FEAT_RIGHTLEFT
200 /* start without Hebrew mapping for a command line */
201 if (firstc == ':' || firstc == '=' || firstc == '>')
202 cmd_hkmap = 0;
203#endif
204
205 ccline.overstrike = FALSE; /* always start in insert mode */
206#ifdef FEAT_SEARCH_EXTRA
207 old_cursor = curwin->w_cursor; /* needs to be restored later */
208 old_curswant = curwin->w_curswant;
209 old_leftcol = curwin->w_leftcol;
210 old_topline = curwin->w_topline;
211# ifdef FEAT_DIFF
212 old_topfill = curwin->w_topfill;
213# endif
214 old_botline = curwin->w_botline;
215#endif
216
217 /*
218 * set some variables for redrawcmd()
219 */
220 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000221 ccline.cmdindent = (firstc > 0 ? indent : 0);
222
223 /* alloc initial ccline.cmdbuff */
224 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 if (ccline.cmdbuff == NULL)
226 return NULL; /* out of memory */
227 ccline.cmdlen = ccline.cmdpos = 0;
228 ccline.cmdbuff[0] = NUL;
229
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000230 /* autoindent for :insert and :append */
231 if (firstc <= 0)
232 {
233 copy_spaces(ccline.cmdbuff, indent);
234 ccline.cmdbuff[indent] = NUL;
235 ccline.cmdpos = indent;
236 ccline.cmdspos = indent;
237 ccline.cmdlen = indent;
238 }
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 ExpandInit(&xpc);
241
242#ifdef FEAT_RIGHTLEFT
243 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
244 && (firstc == '/' || firstc == '?'))
245 cmdmsg_rl = TRUE;
246 else
247 cmdmsg_rl = FALSE;
248#endif
249
250 redir_off = TRUE; /* don't redirect the typed command */
251 if (!cmd_silent)
252 {
253 i = msg_scrolled;
254 msg_scrolled = 0; /* avoid wait_return message */
255 gotocmdline(TRUE);
256 msg_scrolled += i;
257 redrawcmdprompt(); /* draw prompt or indent */
258 set_cmdspos();
259 }
260 xpc.xp_context = EXPAND_NOTHING;
261 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000262#ifndef BACKSLASH_IN_FILENAME
263 xpc.xp_shell = FALSE;
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000266#if defined(FEAT_EVAL)
267 if (ccline.input_fn)
268 {
269 xpc.xp_context = ccline.xp_context;
270 xpc.xp_pattern = ccline.cmdbuff;
271 xpc.xp_arg = ccline.xp_arg;
272 }
273#endif
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 /*
276 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
277 * doing ":@0" when register 0 doesn't contain a CR.
278 */
279 msg_scroll = FALSE;
280
281 State = CMDLINE;
282
283 if (firstc == '/' || firstc == '?' || firstc == '@')
284 {
285 /* Use ":lmap" mappings for search pattern and input(). */
286 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
287 b_im_ptr = &curbuf->b_p_iminsert;
288 else
289 b_im_ptr = &curbuf->b_p_imsearch;
290 if (*b_im_ptr == B_IMODE_LMAP)
291 State |= LANGMAP;
292#ifdef USE_IM_CONTROL
293 im_set_active(*b_im_ptr == B_IMODE_IM);
294#endif
295 }
296#ifdef USE_IM_CONTROL
297 else if (p_imcmdline)
298 im_set_active(TRUE);
299#endif
300
301#ifdef FEAT_MOUSE
302 setmouse();
303#endif
304#ifdef CURSOR_SHAPE
305 ui_cursor_shape(); /* may show different cursor shape */
306#endif
307
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000308 /* When inside an autocommand for writing "exiting" may be set and
309 * terminal mode set to cooked. Need to set raw mode here then. */
310 settmode(TMODE_RAW);
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#ifdef FEAT_CMDHIST
313 init_history();
314 hiscnt = hislen; /* set hiscnt to impossible history value */
315 histype = hist_char2type(firstc);
316#endif
317
318#ifdef FEAT_DIGRAPHS
319 do_digraph(-1); /* init digraph typahead */
320#endif
321
322 /*
323 * Collect the command string, handling editing keys.
324 */
325 for (;;)
326 {
327#ifdef USE_ON_FLY_SCROLL
328 dont_scroll = FALSE; /* allow scrolling here */
329#endif
330 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
331
332 cursorcmd(); /* set the cursor on the right spot */
333 c = safe_vgetc();
334 if (KeyTyped)
335 {
336 some_key_typed = TRUE;
337#ifdef FEAT_RIGHTLEFT
338 if (cmd_hkmap)
339 c = hkmap(c);
340# ifdef FEAT_FKMAP
341 if (cmd_fkmap)
342 c = cmdl_fkmap(c);
343# endif
344 if (cmdmsg_rl && !KeyStuffed)
345 {
346 /* Invert horizontal movements and operations. Only when
347 * typed by the user directly, not when the result of a
348 * mapping. */
349 switch (c)
350 {
351 case K_RIGHT: c = K_LEFT; break;
352 case K_S_RIGHT: c = K_S_LEFT; break;
353 case K_C_RIGHT: c = K_C_LEFT; break;
354 case K_LEFT: c = K_RIGHT; break;
355 case K_S_LEFT: c = K_S_RIGHT; break;
356 case K_C_LEFT: c = K_C_RIGHT; break;
357 }
358 }
359#endif
360 }
361
362 /*
363 * Ignore got_int when CTRL-C was typed here.
364 * Don't ignore it in :global, we really need to break then, e.g., for
365 * ":g/pat/normal /pat" (without the <CR>).
366 * Don't ignore it for the input() function.
367 */
368 if ((c == Ctrl_C
369#ifdef UNIX
370 || c == intr_char
371#endif
372 )
373#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
374 && firstc != '@'
375#endif
376#ifdef FEAT_EVAL
377 && !break_ctrl_c
378#endif
379 && !global_busy)
380 got_int = FALSE;
381
382#ifdef FEAT_CMDHIST
383 /* free old command line when finished moving around in the history
384 * list */
385 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000386 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000387 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 && c != K_PAGEDOWN && c != K_PAGEUP
389 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000390 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
392 {
393 vim_free(lookfor);
394 lookfor = NULL;
395 }
396#endif
397
398 /*
399 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
400 */
401 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
402 c = Ctrl_P;
403
404#ifdef FEAT_WILDMENU
405 /* Special translations for 'wildmenu' */
406 if (did_wild_list && p_wmnu)
407 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000408 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000410 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 c = Ctrl_N;
412 }
413 /* Hitting CR after "emenu Name.": complete submenu */
414 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
415 && ccline.cmdpos > 1
416 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
417 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
418 && (c == '\n' || c == '\r' || c == K_KENTER))
419 c = K_DOWN;
420#endif
421
422 /* free expanded names when finished walking through matches */
423 if (xpc.xp_numfiles != -1
424 && !(c == p_wc && KeyTyped) && c != p_wcm
425 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
426 && c != Ctrl_L)
427 {
428 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
429 did_wild_list = FALSE;
430#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000431 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#endif
433 xpc.xp_context = EXPAND_NOTHING;
434 wim_index = 0;
435#ifdef FEAT_WILDMENU
436 if (p_wmnu && wild_menu_showing != 0)
437 {
438 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000439 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000440
441 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000442 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
444 if (wild_menu_showing == WM_SCROLLED)
445 {
446 /* Entered command line, move it up */
447 cmdline_row--;
448 redrawcmd();
449 }
450 else if (save_p_ls != -1)
451 {
452 /* restore 'laststatus' and 'winminheight' */
453 p_ls = save_p_ls;
454 p_wmh = save_p_wmh;
455 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000456 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000458 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 redrawcmd();
460 save_p_ls = -1;
461 }
462 else
463 {
464# ifdef FEAT_VERTSPLIT
465 win_redraw_last_status(topframe);
466# else
467 lastwin->w_redr_status = TRUE;
468# endif
469 redraw_statuslines();
470 }
471 KeyTyped = skt;
472 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000473 if (ccline.input_fn)
474 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 }
476#endif
477 }
478
479#ifdef FEAT_WILDMENU
480 /* Special translations for 'wildmenu' */
481 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
482 {
483 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000484 if (ccline.cmdbuff[ccline.cmdpos - 1] == '.' && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000486 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 {
488 /* Hitting <Up>: Remove one submenu name in front of the
489 * cursor */
490 int found = FALSE;
491
492 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
493 i = 0;
494 while (--j > 0)
495 {
496 /* check for start of menu name */
497 if (ccline.cmdbuff[j] == ' '
498 && ccline.cmdbuff[j - 1] != '\\')
499 {
500 i = j + 1;
501 break;
502 }
503 /* check for start of submenu name */
504 if (ccline.cmdbuff[j] == '.'
505 && ccline.cmdbuff[j - 1] != '\\')
506 {
507 if (found)
508 {
509 i = j + 1;
510 break;
511 }
512 else
513 found = TRUE;
514 }
515 }
516 if (i > 0)
517 cmdline_del(i);
518 c = p_wc;
519 xpc.xp_context = EXPAND_NOTHING;
520 }
521 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000522 if ((xpc.xp_context == EXPAND_FILES
523 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
525 char_u upseg[5];
526
527 upseg[0] = PATHSEP;
528 upseg[1] = '.';
529 upseg[2] = '.';
530 upseg[3] = PATHSEP;
531 upseg[4] = NUL;
532
533 if (ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000534 && c == K_DOWN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 && (ccline.cmdbuff[ccline.cmdpos - 2] != '.'
536 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
537 {
538 /* go down a directory */
539 c = p_wc;
540 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000541 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 {
543 /* If in a direct ancestor, strip off one ../ to go down */
544 int found = FALSE;
545
546 j = ccline.cmdpos;
547 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
548 while (--j > i)
549 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000550#ifdef FEAT_MBYTE
551 if (has_mbyte)
552 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 if (vim_ispathsep(ccline.cmdbuff[j]))
555 {
556 found = TRUE;
557 break;
558 }
559 }
560 if (found
561 && ccline.cmdbuff[j - 1] == '.'
562 && ccline.cmdbuff[j - 2] == '.'
563 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
564 {
565 cmdline_del(j - 2);
566 c = p_wc;
567 }
568 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000569 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 {
571 /* go up a directory */
572 int found = FALSE;
573
574 j = ccline.cmdpos - 1;
575 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
576 while (--j > i)
577 {
578#ifdef FEAT_MBYTE
579 if (has_mbyte)
580 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
581#endif
582 if (vim_ispathsep(ccline.cmdbuff[j])
583#ifdef BACKSLASH_IN_FILENAME
584 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
585 == NULL
586#endif
587 )
588 {
589 if (found)
590 {
591 i = j + 1;
592 break;
593 }
594 else
595 found = TRUE;
596 }
597 }
598
599 if (!found)
600 j = i;
601 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
602 j += 4;
603 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
604 && j == i)
605 j += 3;
606 else
607 j = 0;
608 if (j > 0)
609 {
610 /* TODO this is only for DOS/UNIX systems - need to put in
611 * machine-specific stuff here and in upseg init */
612 cmdline_del(j);
613 put_on_cmdline(upseg + 1, 3, FALSE);
614 }
615 else if (ccline.cmdpos > i)
616 cmdline_del(i);
617 c = p_wc;
618 }
619 }
620#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
621 if (p_wmnu
622 && (xpc.xp_context == EXPAND_FILES
623 || xpc.xp_context == EXPAND_MENUNAMES)
624 && (c == K_UP || c == K_DOWN))
625 xpc.xp_context = EXPAND_NOTHING;
626#endif
627
628#endif /* FEAT_WILDMENU */
629
630 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
631 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
632 if (c == Ctrl_BSL)
633 {
634 ++no_mapping;
635 ++allow_keys;
636 c = safe_vgetc();
637 --no_mapping;
638 --allow_keys;
639 /* CTRL-\ e doesn't work when obtaining an expression. */
640 if (c != Ctrl_N && c != Ctrl_G
641 && (c != 'e' || ccline.cmdfirstc == '='))
642 {
643 vungetc(c);
644 c = Ctrl_BSL;
645 }
646#ifdef FEAT_EVAL
647 else if (c == 'e')
648 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000649 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650
651 /*
652 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000653 * Need to save and restore the current command line, to be
654 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 */
656 if (ccline.cmdpos == ccline.cmdlen)
657 new_cmdpos = 99999; /* keep it at the end */
658 else
659 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000660
661 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000663 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 if (c == '=')
665 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000666 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000667 * to avoid nasty things like going to another buffer when
668 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000669 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000670 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000673 restore_cmdline(&save_ccline);
674
675 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
677 ccline.cmdlen = STRLEN(p);
678 STRCPY(ccline.cmdbuff, p);
679 vim_free(p);
680
681 /* Restore the cursor or use the position set with
682 * set_cmdline_pos(). */
683 if (new_cmdpos > ccline.cmdlen)
684 ccline.cmdpos = ccline.cmdlen;
685 else
686 ccline.cmdpos = new_cmdpos;
687
688 KeyTyped = FALSE; /* Don't do p_wc completion. */
689 redrawcmd();
690 goto cmdline_changed;
691 }
692 }
693 beep_flush();
694 c = ESC;
695 }
696#endif
697 else
698 {
699 if (c == Ctrl_G && p_im && restart_edit == 0)
700 restart_edit = 'a';
701 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
702 in history */
703 goto returncmd; /* back to Normal mode */
704 }
705 }
706
707#ifdef FEAT_CMDWIN
708 if (c == cedit_key || c == K_CMDWIN)
709 {
710 /*
711 * Open a window to edit the command line (and history).
712 */
713 c = ex_window();
714 some_key_typed = TRUE;
715 }
716# ifdef FEAT_DIGRAPHS
717 else
718# endif
719#endif
720#ifdef FEAT_DIGRAPHS
721 c = do_digraph(c);
722#endif
723
724 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
725 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
726 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000727 /* In Ex mode a backslash escapes a newline. */
728 if (exmode_active
729 && c != ESC
730 && ccline.cmdpos > 0
731 && ccline.cmdpos == ccline.cmdlen
732 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000734 if (c == K_KENTER)
735 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000737 else
738 {
739 gotesc = FALSE; /* Might have typed ESC previously, don't
740 truncate the cmdline now. */
741 if (ccheck_abbr(c + ABBR_OFF))
742 goto cmdline_changed;
743 if (!cmd_silent)
744 {
745 windgoto(msg_row, 0);
746 out_flush();
747 }
748 break;
749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
751
752 /*
753 * Completion for 'wildchar' or 'wildcharm' key.
754 * - hitting <ESC> twice means: abandon command line.
755 * - wildcard expansion is only done when the 'wildchar' key is really
756 * typed, not when it comes from a macro
757 */
758 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
759 {
760 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
761 {
762 /* if 'wildmode' contains "list" may still need to list */
763 if (xpc.xp_numfiles > 1
764 && !did_wild_list
765 && (wim_flags[wim_index] & WIM_LIST))
766 {
767 (void)showmatches(&xpc, FALSE);
768 redrawcmd();
769 did_wild_list = TRUE;
770 }
771 if (wim_flags[wim_index] & WIM_LONGEST)
772 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
773 else if (wim_flags[wim_index] & WIM_FULL)
774 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
775 else
776 res = OK; /* don't insert 'wildchar' now */
777 }
778 else /* typed p_wc first time */
779 {
780 wim_index = 0;
781 j = ccline.cmdpos;
782 /* if 'wildmode' first contains "longest", get longest
783 * common part */
784 if (wim_flags[0] & WIM_LONGEST)
785 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
786 else
787 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
788
789 /* if interrupted while completing, behave like it failed */
790 if (got_int)
791 {
792 (void)vpeekc(); /* remove <C-C> from input stream */
793 got_int = FALSE; /* don't abandon the command line */
794 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
795#ifdef FEAT_WILDMENU
796 xpc.xp_context = EXPAND_NOTHING;
797#endif
798 goto cmdline_changed;
799 }
800
801 /* when more than one match, and 'wildmode' first contains
802 * "list", or no change and 'wildmode' contains "longest,list",
803 * list all matches */
804 if (res == OK && xpc.xp_numfiles > 1)
805 {
806 /* a "longest" that didn't do anything is skipped (but not
807 * "list:longest") */
808 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
809 wim_index = 1;
810 if ((wim_flags[wim_index] & WIM_LIST)
811#ifdef FEAT_WILDMENU
812 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
813#endif
814 )
815 {
816 if (!(wim_flags[0] & WIM_LONGEST))
817 {
818#ifdef FEAT_WILDMENU
819 int p_wmnu_save = p_wmnu;
820 p_wmnu = 0;
821#endif
822 nextwild(&xpc, WILD_PREV, 0); /* remove match */
823#ifdef FEAT_WILDMENU
824 p_wmnu = p_wmnu_save;
825#endif
826 }
827#ifdef FEAT_WILDMENU
828 (void)showmatches(&xpc, p_wmnu
829 && ((wim_flags[wim_index] & WIM_LIST) == 0));
830#else
831 (void)showmatches(&xpc, FALSE);
832#endif
833 redrawcmd();
834 did_wild_list = TRUE;
835 if (wim_flags[wim_index] & WIM_LONGEST)
836 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
837 else if (wim_flags[wim_index] & WIM_FULL)
838 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
839 }
840 else
841 vim_beep();
842 }
843#ifdef FEAT_WILDMENU
844 else if (xpc.xp_numfiles == -1)
845 xpc.xp_context = EXPAND_NOTHING;
846#endif
847 }
848 if (wim_index < 3)
849 ++wim_index;
850 if (c == ESC)
851 gotesc = TRUE;
852 if (res == OK)
853 goto cmdline_changed;
854 }
855
856 gotesc = FALSE;
857
858 /* <S-Tab> goes to last match, in a clumsy way */
859 if (c == K_S_TAB && KeyTyped)
860 {
861 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
862 && nextwild(&xpc, WILD_PREV, 0) == OK
863 && nextwild(&xpc, WILD_PREV, 0) == OK)
864 goto cmdline_changed;
865 }
866
867 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
868 c = NL;
869
870 do_abbr = TRUE; /* default: check for abbreviation */
871
872 /*
873 * Big switch for a typed command line character.
874 */
875 switch (c)
876 {
877 case K_BS:
878 case Ctrl_H:
879 case K_DEL:
880 case K_KDEL:
881 case Ctrl_W:
882#ifdef FEAT_FKMAP
883 if (cmd_fkmap && c == K_BS)
884 c = K_DEL;
885#endif
886 if (c == K_KDEL)
887 c = K_DEL;
888
889 /*
890 * delete current character is the same as backspace on next
891 * character, except at end of line
892 */
893 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
894 ++ccline.cmdpos;
895#ifdef FEAT_MBYTE
896 if (has_mbyte && c == K_DEL)
897 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
898 ccline.cmdbuff + ccline.cmdpos);
899#endif
900 if (ccline.cmdpos > 0)
901 {
902 char_u *p;
903
904 j = ccline.cmdpos;
905 p = ccline.cmdbuff + j;
906#ifdef FEAT_MBYTE
907 if (has_mbyte)
908 {
909 p = mb_prevptr(ccline.cmdbuff, p);
910 if (c == Ctrl_W)
911 {
912 while (p > ccline.cmdbuff && vim_isspace(*p))
913 p = mb_prevptr(ccline.cmdbuff, p);
914 i = mb_get_class(p);
915 while (p > ccline.cmdbuff && mb_get_class(p) == i)
916 p = mb_prevptr(ccline.cmdbuff, p);
917 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000918 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920 }
921 else
922#endif
923 if (c == Ctrl_W)
924 {
925 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
926 --p;
927 i = vim_iswordc(p[-1]);
928 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
929 && vim_iswordc(p[-1]) == i)
930 --p;
931 }
932 else
933 --p;
934 ccline.cmdpos = (int)(p - ccline.cmdbuff);
935 ccline.cmdlen -= j - ccline.cmdpos;
936 i = ccline.cmdpos;
937 while (i < ccline.cmdlen)
938 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
939
940 /* Truncate at the end, required for multi-byte chars. */
941 ccline.cmdbuff[ccline.cmdlen] = NUL;
942 redrawcmd();
943 }
944 else if (ccline.cmdlen == 0 && c != Ctrl_W
945 && ccline.cmdprompt == NULL && indent == 0)
946 {
947 /* In ex and debug mode it doesn't make sense to return. */
948 if (exmode_active
949#ifdef FEAT_EVAL
950 || ccline.cmdfirstc == '>'
951#endif
952 )
953 goto cmdline_not_changed;
954
955 vim_free(ccline.cmdbuff); /* no commandline to return */
956 ccline.cmdbuff = NULL;
957 if (!cmd_silent)
958 {
959#ifdef FEAT_RIGHTLEFT
960 if (cmdmsg_rl)
961 msg_col = Columns;
962 else
963#endif
964 msg_col = 0;
965 msg_putchar(' '); /* delete ':' */
966 }
967 redraw_cmdline = TRUE;
968 goto returncmd; /* back to cmd mode */
969 }
970 goto cmdline_changed;
971
972 case K_INS:
973 case K_KINS:
974#ifdef FEAT_FKMAP
975 /* if Farsi mode set, we are in reverse insert mode -
976 Do not change the mode */
977 if (cmd_fkmap)
978 beep_flush();
979 else
980#endif
981 ccline.overstrike = !ccline.overstrike;
982#ifdef CURSOR_SHAPE
983 ui_cursor_shape(); /* may show different cursor shape */
984#endif
985 goto cmdline_not_changed;
986
987 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000988 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
990 /* ":lmap" mappings exists, toggle use of mappings. */
991 State ^= LANGMAP;
992#ifdef USE_IM_CONTROL
993 im_set_active(FALSE); /* Disable input method */
994#endif
995 if (b_im_ptr != NULL)
996 {
997 if (State & LANGMAP)
998 *b_im_ptr = B_IMODE_LMAP;
999 else
1000 *b_im_ptr = B_IMODE_NONE;
1001 }
1002 }
1003#ifdef USE_IM_CONTROL
1004 else
1005 {
1006 /* There are no ":lmap" mappings, toggle IM. When
1007 * 'imdisable' is set don't try getting the status, it's
1008 * always off. */
1009 if ((p_imdisable && b_im_ptr != NULL)
1010 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1011 {
1012 im_set_active(FALSE); /* Disable input method */
1013 if (b_im_ptr != NULL)
1014 *b_im_ptr = B_IMODE_NONE;
1015 }
1016 else
1017 {
1018 im_set_active(TRUE); /* Enable input method */
1019 if (b_im_ptr != NULL)
1020 *b_im_ptr = B_IMODE_IM;
1021 }
1022 }
1023#endif
1024 if (b_im_ptr != NULL)
1025 {
1026 if (b_im_ptr == &curbuf->b_p_iminsert)
1027 set_iminsert_global();
1028 else
1029 set_imsearch_global();
1030 }
1031#ifdef CURSOR_SHAPE
1032 ui_cursor_shape(); /* may show different cursor shape */
1033#endif
1034#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1035 /* Show/unshow value of 'keymap' in status lines later. */
1036 status_redraw_curbuf();
1037#endif
1038 goto cmdline_not_changed;
1039
1040/* case '@': only in very old vi */
1041 case Ctrl_U:
1042 /* delete all characters left of the cursor */
1043 j = ccline.cmdpos;
1044 ccline.cmdlen -= j;
1045 i = ccline.cmdpos = 0;
1046 while (i < ccline.cmdlen)
1047 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1048 /* Truncate at the end, required for multi-byte chars. */
1049 ccline.cmdbuff[ccline.cmdlen] = NUL;
1050 redrawcmd();
1051 goto cmdline_changed;
1052
1053#ifdef FEAT_CLIPBOARD
1054 case Ctrl_Y:
1055 /* Copy the modeless selection, if there is one. */
1056 if (clip_star.state != SELECT_CLEARED)
1057 {
1058 if (clip_star.state == SELECT_DONE)
1059 clip_copy_modeless_selection(TRUE);
1060 goto cmdline_not_changed;
1061 }
1062 break;
1063#endif
1064
1065 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1066 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001067 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001068 * ":normal" runs out of characters. */
1069 if (exmode_active
1070#ifdef FEAT_EX_EXTRA
1071 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1072#endif
1073 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 goto cmdline_not_changed;
1075
1076 gotesc = TRUE; /* will free ccline.cmdbuff after
1077 putting it in history */
1078 goto returncmd; /* back to cmd mode */
1079
1080 case Ctrl_R: /* insert register */
1081#ifdef USE_ON_FLY_SCROLL
1082 dont_scroll = TRUE; /* disallow scrolling here */
1083#endif
1084 putcmdline('"', TRUE);
1085 ++no_mapping;
1086 i = c = safe_vgetc(); /* CTRL-R <char> */
1087 if (i == Ctrl_O)
1088 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1089 if (i == Ctrl_R)
1090 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1091 --no_mapping;
1092#ifdef FEAT_EVAL
1093 /*
1094 * Insert the result of an expression.
1095 * Need to save the current command line, to be able to enter
1096 * a new one...
1097 */
1098 new_cmdpos = -1;
1099 if (c == '=')
1100 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1102 {
1103 beep_flush();
1104 c = ESC;
1105 }
1106 else
1107 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001108 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001110 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112 }
1113#endif
1114 if (c != ESC) /* use ESC to cancel inserting register */
1115 {
1116 cmdline_paste(c, i == Ctrl_R);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001117
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001118#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001119 /* When there was a serious error abort getting the
1120 * command line. */
1121 if (aborting())
1122 {
1123 gotesc = TRUE; /* will free ccline.cmdbuff after
1124 putting it in history */
1125 goto returncmd; /* back to cmd mode */
1126 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 KeyTyped = FALSE; /* Don't do p_wc completion. */
1129#ifdef FEAT_EVAL
1130 if (new_cmdpos >= 0)
1131 {
1132 /* set_cmdline_pos() was used */
1133 if (new_cmdpos > ccline.cmdlen)
1134 ccline.cmdpos = ccline.cmdlen;
1135 else
1136 ccline.cmdpos = new_cmdpos;
1137 }
1138#endif
1139 }
1140 redrawcmd();
1141 goto cmdline_changed;
1142
1143 case Ctrl_D:
1144 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1145 break; /* Use ^D as normal char instead */
1146
1147 redrawcmd();
1148 continue; /* don't do incremental search now */
1149
1150 case K_RIGHT:
1151 case K_S_RIGHT:
1152 case K_C_RIGHT:
1153 do
1154 {
1155 if (ccline.cmdpos >= ccline.cmdlen)
1156 break;
1157 i = cmdline_charsize(ccline.cmdpos);
1158 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1159 break;
1160 ccline.cmdspos += i;
1161#ifdef FEAT_MBYTE
1162 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001163 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 + ccline.cmdpos);
1165 else
1166#endif
1167 ++ccline.cmdpos;
1168 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001169 while ((c == K_S_RIGHT || c == K_C_RIGHT
1170 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1172#ifdef FEAT_MBYTE
1173 if (has_mbyte)
1174 set_cmdspos_cursor();
1175#endif
1176 goto cmdline_not_changed;
1177
1178 case K_LEFT:
1179 case K_S_LEFT:
1180 case K_C_LEFT:
1181 do
1182 {
1183 if (ccline.cmdpos == 0)
1184 break;
1185 --ccline.cmdpos;
1186#ifdef FEAT_MBYTE
1187 if (has_mbyte) /* move to first byte of char */
1188 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1189 ccline.cmdbuff + ccline.cmdpos);
1190#endif
1191 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1192 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001193 while ((c == K_S_LEFT || c == K_C_LEFT
1194 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1196#ifdef FEAT_MBYTE
1197 if (has_mbyte)
1198 set_cmdspos_cursor();
1199#endif
1200 goto cmdline_not_changed;
1201
1202 case K_IGNORE:
1203 goto cmdline_not_changed; /* Ignore mouse */
1204
Bram Moolenaar4770d092006-01-12 23:22:24 +00001205#ifdef FEAT_GUI_W32
1206 /* On Win32 ignore <M-F4>, we get it when closing the window was
1207 * cancelled. */
1208 case K_F4:
1209 if (mod_mask == MOD_MASK_ALT)
1210 {
1211 redrawcmd(); /* somehow the cmdline is cleared */
1212 goto cmdline_not_changed;
1213 }
1214 break;
1215#endif
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#ifdef FEAT_MOUSE
1218 case K_MIDDLEDRAG:
1219 case K_MIDDLERELEASE:
1220 goto cmdline_not_changed; /* Ignore mouse */
1221
1222 case K_MIDDLEMOUSE:
1223# ifdef FEAT_GUI
1224 /* When GUI is active, also paste when 'mouse' is empty */
1225 if (!gui.in_use)
1226# endif
1227 if (!mouse_has(MOUSE_COMMAND))
1228 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001229# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 if (clip_star.available)
1231 cmdline_paste('*', TRUE);
1232 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 cmdline_paste(0, TRUE);
1235 redrawcmd();
1236 goto cmdline_changed;
1237
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001238# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 case K_DROP:
1240 cmdline_paste('~', TRUE);
1241 redrawcmd();
1242 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001243# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
1245 case K_LEFTDRAG:
1246 case K_LEFTRELEASE:
1247 case K_RIGHTDRAG:
1248 case K_RIGHTRELEASE:
1249 /* Ignore drag and release events when the button-down wasn't
1250 * seen before. */
1251 if (ignore_drag_release)
1252 goto cmdline_not_changed;
1253 /* FALLTHROUGH */
1254 case K_LEFTMOUSE:
1255 case K_RIGHTMOUSE:
1256 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1257 ignore_drag_release = TRUE;
1258 else
1259 ignore_drag_release = FALSE;
1260# ifdef FEAT_GUI
1261 /* When GUI is active, also move when 'mouse' is empty */
1262 if (!gui.in_use)
1263# endif
1264 if (!mouse_has(MOUSE_COMMAND))
1265 goto cmdline_not_changed; /* Ignore mouse */
1266# ifdef FEAT_CLIPBOARD
1267 if (mouse_row < cmdline_row && clip_star.available)
1268 {
1269 int button, is_click, is_drag;
1270
1271 /*
1272 * Handle modeless selection.
1273 */
1274 button = get_mouse_button(KEY2TERMCAP1(c),
1275 &is_click, &is_drag);
1276 if (mouse_model_popup() && button == MOUSE_LEFT
1277 && (mod_mask & MOD_MASK_SHIFT))
1278 {
1279 /* Translate shift-left to right button. */
1280 button = MOUSE_RIGHT;
1281 mod_mask &= ~MOD_MASK_SHIFT;
1282 }
1283 clip_modeless(button, is_click, is_drag);
1284 goto cmdline_not_changed;
1285 }
1286# endif
1287
1288 set_cmdspos();
1289 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1290 ++ccline.cmdpos)
1291 {
1292 i = cmdline_charsize(ccline.cmdpos);
1293 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1294 && mouse_col < ccline.cmdspos % Columns + i)
1295 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001296# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 if (has_mbyte)
1298 {
1299 /* Count ">" for double-wide char that doesn't fit. */
1300 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001301 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 + ccline.cmdpos) - 1;
1303 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 ccline.cmdspos += i;
1306 }
1307 goto cmdline_not_changed;
1308
1309 /* Mouse scroll wheel: ignored here */
1310 case K_MOUSEDOWN:
1311 case K_MOUSEUP:
1312 /* Alternate buttons ignored here */
1313 case K_X1MOUSE:
1314 case K_X1DRAG:
1315 case K_X1RELEASE:
1316 case K_X2MOUSE:
1317 case K_X2DRAG:
1318 case K_X2RELEASE:
1319 goto cmdline_not_changed;
1320
1321#endif /* FEAT_MOUSE */
1322
1323#ifdef FEAT_GUI
1324 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1325 case K_LEFTRELEASE_NM:
1326 goto cmdline_not_changed;
1327
1328 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001329 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 {
1331 gui_do_scroll();
1332 redrawcmd();
1333 }
1334 goto cmdline_not_changed;
1335
1336 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001337 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 {
1339 gui_do_horiz_scroll();
1340 redrawcmd();
1341 }
1342 goto cmdline_not_changed;
1343#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001344#ifdef FEAT_GUI_TABLINE
1345 case K_TABLINE:
1346 case K_TABMENU:
1347 /* Don't want to change any tabs here. Make sure the same tab
1348 * is still selected. */
1349 if (gui_use_tabline())
1350 gui_mch_set_curtab(tabpage_index(curtab));
1351 goto cmdline_not_changed;
1352#endif
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 case K_SELECT: /* end of Select mode mapping - ignore */
1355 goto cmdline_not_changed;
1356
1357 case Ctrl_B: /* begin of command line */
1358 case K_HOME:
1359 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 case K_S_HOME:
1361 case K_C_HOME:
1362 ccline.cmdpos = 0;
1363 set_cmdspos();
1364 goto cmdline_not_changed;
1365
1366 case Ctrl_E: /* end of command line */
1367 case K_END:
1368 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 case K_S_END:
1370 case K_C_END:
1371 ccline.cmdpos = ccline.cmdlen;
1372 set_cmdspos_cursor();
1373 goto cmdline_not_changed;
1374
1375 case Ctrl_A: /* all matches */
1376 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1377 break;
1378 goto cmdline_changed;
1379
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001380 case Ctrl_L:
1381#ifdef FEAT_SEARCH_EXTRA
1382 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1383 {
1384 /* Add a character from under the cursor for 'incsearch' */
1385 if (did_incsearch
1386 && !equalpos(curwin->w_cursor, old_cursor))
1387 {
1388 c = gchar_cursor();
1389 if (c != NUL)
1390 break;
1391 }
1392 goto cmdline_not_changed;
1393 }
1394#endif
1395
1396 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1398 break;
1399 goto cmdline_changed;
1400
1401 case Ctrl_N: /* next match */
1402 case Ctrl_P: /* previous match */
1403 if (xpc.xp_numfiles > 0)
1404 {
1405 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1406 == FAIL)
1407 break;
1408 goto cmdline_changed;
1409 }
1410
1411#ifdef FEAT_CMDHIST
1412 case K_UP:
1413 case K_DOWN:
1414 case K_S_UP:
1415 case K_S_DOWN:
1416 case K_PAGEUP:
1417 case K_KPAGEUP:
1418 case K_PAGEDOWN:
1419 case K_KPAGEDOWN:
1420 if (hislen == 0 || firstc == NUL) /* no history */
1421 goto cmdline_not_changed;
1422
1423 i = hiscnt;
1424
1425 /* save current command string so it can be restored later */
1426 if (lookfor == NULL)
1427 {
1428 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1429 goto cmdline_not_changed;
1430 lookfor[ccline.cmdpos] = NUL;
1431 }
1432
1433 j = (int)STRLEN(lookfor);
1434 for (;;)
1435 {
1436 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001437 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001438 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 {
1440 if (hiscnt == hislen) /* first time */
1441 hiscnt = hisidx[histype];
1442 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1443 hiscnt = hislen - 1;
1444 else if (hiscnt != hisidx[histype] + 1)
1445 --hiscnt;
1446 else /* at top of list */
1447 {
1448 hiscnt = i;
1449 break;
1450 }
1451 }
1452 else /* one step forwards */
1453 {
1454 /* on last entry, clear the line */
1455 if (hiscnt == hisidx[histype])
1456 {
1457 hiscnt = hislen;
1458 break;
1459 }
1460
1461 /* not on a history line, nothing to do */
1462 if (hiscnt == hislen)
1463 break;
1464 if (hiscnt == hislen - 1) /* wrap around */
1465 hiscnt = 0;
1466 else
1467 ++hiscnt;
1468 }
1469 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1470 {
1471 hiscnt = i;
1472 break;
1473 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001474 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001475 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 || STRNCMP(history[histype][hiscnt].hisstr,
1477 lookfor, (size_t)j) == 0)
1478 break;
1479 }
1480
1481 if (hiscnt != i) /* jumped to other entry */
1482 {
1483 char_u *p;
1484 int len;
1485 int old_firstc;
1486
1487 vim_free(ccline.cmdbuff);
1488 if (hiscnt == hislen)
1489 p = lookfor; /* back to the old one */
1490 else
1491 p = history[histype][hiscnt].hisstr;
1492
1493 if (histype == HIST_SEARCH
1494 && p != lookfor
1495 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1496 {
1497 /* Correct for the separator character used when
1498 * adding the history entry vs the one used now.
1499 * First loop: count length.
1500 * Second loop: copy the characters. */
1501 for (i = 0; i <= 1; ++i)
1502 {
1503 len = 0;
1504 for (j = 0; p[j] != NUL; ++j)
1505 {
1506 /* Replace old sep with new sep, unless it is
1507 * escaped. */
1508 if (p[j] == old_firstc
1509 && (j == 0 || p[j - 1] != '\\'))
1510 {
1511 if (i > 0)
1512 ccline.cmdbuff[len] = firstc;
1513 }
1514 else
1515 {
1516 /* Escape new sep, unless it is already
1517 * escaped. */
1518 if (p[j] == firstc
1519 && (j == 0 || p[j - 1] != '\\'))
1520 {
1521 if (i > 0)
1522 ccline.cmdbuff[len] = '\\';
1523 ++len;
1524 }
1525 if (i > 0)
1526 ccline.cmdbuff[len] = p[j];
1527 }
1528 ++len;
1529 }
1530 if (i == 0)
1531 {
1532 alloc_cmdbuff(len);
1533 if (ccline.cmdbuff == NULL)
1534 goto returncmd;
1535 }
1536 }
1537 ccline.cmdbuff[len] = NUL;
1538 }
1539 else
1540 {
1541 alloc_cmdbuff((int)STRLEN(p));
1542 if (ccline.cmdbuff == NULL)
1543 goto returncmd;
1544 STRCPY(ccline.cmdbuff, p);
1545 }
1546
1547 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1548 redrawcmd();
1549 goto cmdline_changed;
1550 }
1551 beep_flush();
1552 goto cmdline_not_changed;
1553#endif
1554
1555 case Ctrl_V:
1556 case Ctrl_Q:
1557#ifdef FEAT_MOUSE
1558 ignore_drag_release = TRUE;
1559#endif
1560 putcmdline('^', TRUE);
1561 c = get_literal(); /* get next (two) character(s) */
1562 do_abbr = FALSE; /* don't do abbreviation now */
1563#ifdef FEAT_MBYTE
1564 /* may need to remove ^ when composing char was typed */
1565 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1566 {
1567 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1568 msg_putchar(' ');
1569 cursorcmd();
1570 }
1571#endif
1572 break;
1573
1574#ifdef FEAT_DIGRAPHS
1575 case Ctrl_K:
1576#ifdef FEAT_MOUSE
1577 ignore_drag_release = TRUE;
1578#endif
1579 putcmdline('?', TRUE);
1580#ifdef USE_ON_FLY_SCROLL
1581 dont_scroll = TRUE; /* disallow scrolling here */
1582#endif
1583 c = get_digraph(TRUE);
1584 if (c != NUL)
1585 break;
1586
1587 redrawcmd();
1588 goto cmdline_not_changed;
1589#endif /* FEAT_DIGRAPHS */
1590
1591#ifdef FEAT_RIGHTLEFT
1592 case Ctrl__: /* CTRL-_: switch language mode */
1593 if (!p_ari)
1594 break;
1595#ifdef FEAT_FKMAP
1596 if (p_altkeymap)
1597 {
1598 cmd_fkmap = !cmd_fkmap;
1599 if (cmd_fkmap) /* in Farsi always in Insert mode */
1600 ccline.overstrike = FALSE;
1601 }
1602 else /* Hebrew is default */
1603#endif
1604 cmd_hkmap = !cmd_hkmap;
1605 goto cmdline_not_changed;
1606#endif
1607
1608 default:
1609#ifdef UNIX
1610 if (c == intr_char)
1611 {
1612 gotesc = TRUE; /* will free ccline.cmdbuff after
1613 putting it in history */
1614 goto returncmd; /* back to Normal mode */
1615 }
1616#endif
1617 /*
1618 * Normal character with no special meaning. Just set mod_mask
1619 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1620 * the string <S-Space>. This should only happen after ^V.
1621 */
1622 if (!IS_SPECIAL(c))
1623 mod_mask = 0x0;
1624 break;
1625 }
1626 /*
1627 * End of switch on command line character.
1628 * We come here if we have a normal character.
1629 */
1630
1631 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1632#ifdef FEAT_MBYTE
1633 /* Add ABBR_OFF for characters above 0x100, this is
1634 * what check_abbr() expects. */
1635 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1636#endif
1637 c))
1638 goto cmdline_changed;
1639
1640 /*
1641 * put the character in the command line
1642 */
1643 if (IS_SPECIAL(c) || mod_mask != 0)
1644 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1645 else
1646 {
1647#ifdef FEAT_MBYTE
1648 if (has_mbyte)
1649 {
1650 j = (*mb_char2bytes)(c, IObuff);
1651 IObuff[j] = NUL; /* exclude composing chars */
1652 put_on_cmdline(IObuff, j, TRUE);
1653 }
1654 else
1655#endif
1656 {
1657 IObuff[0] = c;
1658 put_on_cmdline(IObuff, 1, TRUE);
1659 }
1660 }
1661 goto cmdline_changed;
1662
1663/*
1664 * This part implements incremental searches for "/" and "?"
1665 * Jump to cmdline_not_changed when a character has been read but the command
1666 * line did not change. Then we only search and redraw if something changed in
1667 * the past.
1668 * Jump to cmdline_changed when the command line did change.
1669 * (Sorry for the goto's, I know it is ugly).
1670 */
1671cmdline_not_changed:
1672#ifdef FEAT_SEARCH_EXTRA
1673 if (!incsearch_postponed)
1674 continue;
1675#endif
1676
1677cmdline_changed:
1678#ifdef FEAT_SEARCH_EXTRA
1679 /*
1680 * 'incsearch' highlighting.
1681 */
1682 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1683 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001684 pos_T end_pos;
1685
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 /* if there is a character waiting, search and redraw later */
1687 if (char_avail())
1688 {
1689 incsearch_postponed = TRUE;
1690 continue;
1691 }
1692 incsearch_postponed = FALSE;
1693 curwin->w_cursor = old_cursor; /* start at old position */
1694
1695 /* If there is no command line, don't do anything */
1696 if (ccline.cmdlen == 0)
1697 i = 0;
1698 else
1699 {
1700 cursor_off(); /* so the user knows we're busy */
1701 out_flush();
1702 ++emsg_off; /* So it doesn't beep if bad expr */
1703 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1704 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1705 --emsg_off;
1706 /* if interrupted while searching, behave like it failed */
1707 if (got_int)
1708 {
1709 (void)vpeekc(); /* remove <C-C> from input stream */
1710 got_int = FALSE; /* don't abandon the command line */
1711 i = 0;
1712 }
1713 else if (char_avail())
1714 /* cancelled searching because a char was typed */
1715 incsearch_postponed = TRUE;
1716 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001717 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 highlight_match = TRUE; /* highlight position */
1719 else
1720 highlight_match = FALSE; /* remove highlight */
1721
1722 /* first restore the old curwin values, so the screen is
1723 * positioned in the same way as the actual search command */
1724 curwin->w_leftcol = old_leftcol;
1725 curwin->w_topline = old_topline;
1726# ifdef FEAT_DIFF
1727 curwin->w_topfill = old_topfill;
1728# endif
1729 curwin->w_botline = old_botline;
1730 changed_cline_bef_curs();
1731 update_topline();
1732
1733 if (i != 0)
1734 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001735 pos_T save_pos = curwin->w_cursor;
1736
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001738 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 * moves the whole match onto the screen when 'nowrap' is set.
1740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 curwin->w_cursor.lnum += search_match_lines;
1742 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001743 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1744 {
1745 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1746 coladvance((colnr_T)MAXCOL);
1747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001749 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001750 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 validate_cursor();
1754
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001755 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001756 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001757 restore_cmdline(&save_ccline);
1758
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001759 /* Leave it at the end to make CTRL-R CTRL-W work. */
1760 if (i != 0)
1761 curwin->w_cursor = end_pos;
1762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 msg_starthere();
1764 redrawcmdline();
1765 did_incsearch = TRUE;
1766 }
1767#else /* FEAT_SEARCH_EXTRA */
1768 ;
1769#endif
1770
1771#ifdef FEAT_RIGHTLEFT
1772 if (cmdmsg_rl
1773# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001774 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775# endif
1776 )
1777 /* Always redraw the whole command line to fix shaping and
1778 * right-left typing. Not efficient, but it works. */
1779 redrawcmd();
1780#endif
1781 }
1782
1783returncmd:
1784
1785#ifdef FEAT_RIGHTLEFT
1786 cmdmsg_rl = FALSE;
1787#endif
1788
1789#ifdef FEAT_FKMAP
1790 cmd_fkmap = 0;
1791#endif
1792
1793 ExpandCleanup(&xpc);
1794
1795#ifdef FEAT_SEARCH_EXTRA
1796 if (did_incsearch)
1797 {
1798 curwin->w_cursor = old_cursor;
1799 curwin->w_curswant = old_curswant;
1800 curwin->w_leftcol = old_leftcol;
1801 curwin->w_topline = old_topline;
1802# ifdef FEAT_DIFF
1803 curwin->w_topfill = old_topfill;
1804# endif
1805 curwin->w_botline = old_botline;
1806 highlight_match = FALSE;
1807 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001808 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 }
1810#endif
1811
1812 if (ccline.cmdbuff != NULL)
1813 {
1814 /*
1815 * Put line in history buffer (":" and "=" only when it was typed).
1816 */
1817#ifdef FEAT_CMDHIST
1818 if (ccline.cmdlen && firstc != NUL
1819 && (some_key_typed || histype == HIST_SEARCH))
1820 {
1821 add_to_history(histype, ccline.cmdbuff, TRUE,
1822 histype == HIST_SEARCH ? firstc : NUL);
1823 if (firstc == ':')
1824 {
1825 vim_free(new_last_cmdline);
1826 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1827 }
1828 }
1829#endif
1830
1831 if (gotesc) /* abandon command line */
1832 {
1833 vim_free(ccline.cmdbuff);
1834 ccline.cmdbuff = NULL;
1835 if (msg_scrolled == 0)
1836 compute_cmdrow();
1837 MSG("");
1838 redraw_cmdline = TRUE;
1839 }
1840 }
1841
1842 /*
1843 * If the screen was shifted up, redraw the whole screen (later).
1844 * If the line is too long, clear it, so ruler and shown command do
1845 * not get printed in the middle of it.
1846 */
1847 msg_check();
1848 msg_scroll = save_msg_scroll;
1849 redir_off = FALSE;
1850
1851 /* When the command line was typed, no need for a wait-return prompt. */
1852 if (some_key_typed)
1853 need_wait_return = FALSE;
1854
1855 State = save_State;
1856#ifdef USE_IM_CONTROL
1857 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1858 im_save_status(b_im_ptr);
1859 im_set_active(FALSE);
1860#endif
1861#ifdef FEAT_MOUSE
1862 setmouse();
1863#endif
1864#ifdef CURSOR_SHAPE
1865 ui_cursor_shape(); /* may show different cursor shape */
1866#endif
1867
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001868 {
1869 char_u *p = ccline.cmdbuff;
1870
1871 /* Make ccline empty, getcmdline() may try to use it. */
1872 ccline.cmdbuff = NULL;
1873 return p;
1874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875}
1876
1877#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1878/*
1879 * Get a command line with a prompt.
1880 * This is prepared to be called recursively from getcmdline() (e.g. by
1881 * f_input() when evaluating an expression from CTRL-R =).
1882 * Returns the command line in allocated memory, or NULL.
1883 */
1884 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001885getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 int firstc;
1887 char_u *prompt; /* command line prompt */
1888 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001889 int xp_context; /* type of expansion */
1890 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 char_u *s;
1893 struct cmdline_info save_ccline;
1894 int msg_col_save = msg_col;
1895
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001896 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 ccline.cmdprompt = prompt;
1898 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001899# ifdef FEAT_EVAL
1900 ccline.xp_context = xp_context;
1901 ccline.xp_arg = xp_arg;
1902 ccline.input_fn = (firstc == '@');
1903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001905 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 /* Restore msg_col, the prompt from input() may have changed it. */
1907 msg_col = msg_col_save;
1908
1909 return s;
1910}
1911#endif
1912
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001913/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001914 * Return TRUE when the text must not be changed and we can't switch to
1915 * another window or buffer. Used when editing the command line, evaluating
1916 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001917 */
1918 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001919text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001920{
1921#ifdef FEAT_CMDWIN
1922 if (cmdwin_type != 0)
1923 return TRUE;
1924#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001925 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001926}
1927
1928/*
1929 * Give an error message for a command that isn't allowed while the cmdline
1930 * window is open or editing the cmdline in another way.
1931 */
1932 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001933text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001934{
1935#ifdef FEAT_CMDWIN
1936 if (cmdwin_type != 0)
1937 EMSG(_(e_cmdwin));
1938 else
1939#endif
1940 EMSG(_(e_secure));
1941}
1942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 static int
1944cmdline_charsize(idx)
1945 int idx;
1946{
1947#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1948 if (cmdline_star > 0) /* showing '*', always 1 position */
1949 return 1;
1950#endif
1951 return ptr2cells(ccline.cmdbuff + idx);
1952}
1953
1954/*
1955 * Compute the offset of the cursor on the command line for the prompt and
1956 * indent.
1957 */
1958 static void
1959set_cmdspos()
1960{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001961 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 ccline.cmdspos = 1 + ccline.cmdindent;
1963 else
1964 ccline.cmdspos = 0 + ccline.cmdindent;
1965}
1966
1967/*
1968 * Compute the screen position for the cursor on the command line.
1969 */
1970 static void
1971set_cmdspos_cursor()
1972{
1973 int i, m, c;
1974
1975 set_cmdspos();
1976 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001979 if (m < 0) /* overflow, Columns or Rows at weird value */
1980 m = MAXCOL;
1981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 else
1983 m = MAXCOL;
1984 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1985 {
1986 c = cmdline_charsize(i);
1987#ifdef FEAT_MBYTE
1988 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1989 if (has_mbyte)
1990 correct_cmdspos(i, c);
1991#endif
1992 /* If the cmdline doesn't fit, put cursor on last visible char. */
1993 if ((ccline.cmdspos += c) >= m)
1994 {
1995 ccline.cmdpos = i - 1;
1996 ccline.cmdspos -= c;
1997 break;
1998 }
1999#ifdef FEAT_MBYTE
2000 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002001 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002#endif
2003 }
2004}
2005
2006#ifdef FEAT_MBYTE
2007/*
2008 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2009 * character that doesn't fit, so that a ">" must be displayed.
2010 */
2011 static void
2012correct_cmdspos(idx, cells)
2013 int idx;
2014 int cells;
2015{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002016 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2018 && ccline.cmdspos % Columns + cells > Columns)
2019 ccline.cmdspos++;
2020}
2021#endif
2022
2023/*
2024 * Get an Ex command line for the ":" command.
2025 */
2026/* ARGSUSED */
2027 char_u *
2028getexline(c, dummy, indent)
2029 int c; /* normally ':', NUL for ":append" */
2030 void *dummy; /* cookie not used */
2031 int indent; /* indent for inside conditionals */
2032{
2033 /* When executing a register, remove ':' that's in front of each line. */
2034 if (exec_from_reg && vpeekc() == ':')
2035 (void)vgetc();
2036 return getcmdline(c, 1L, indent);
2037}
2038
2039/*
2040 * Get an Ex command line for Ex mode.
2041 * In Ex mode we only use the OS supplied line editing features and no
2042 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002043 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 */
2045/* ARGSUSED */
2046 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002047getexmodeline(promptc, dummy, indent)
2048 int promptc; /* normally ':', NUL for ":append" and '?' for
2049 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 void *dummy; /* cookie not used */
2051 int indent; /* indent for inside conditionals */
2052{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002053 garray_T line_ga;
2054 char_u *pend;
2055 int startcol = 0;
2056 int c1;
2057 int escaped = FALSE; /* CTRL-V typed */
2058 int vcol = 0;
2059 char_u *p;
2060 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062 /* Switch cursor on now. This avoids that it happens after the "\n", which
2063 * confuses the system function that computes tabstops. */
2064 cursor_on();
2065
2066 /* always start in column 0; write a newline if necessary */
2067 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002068 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002070 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002072 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002073 if (p_prompt)
2074 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 while (indent-- > 0)
2076 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079
2080 ga_init2(&line_ga, 1, 30);
2081
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002082 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002083 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002084 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002085 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002086 while (indent >= 8)
2087 {
2088 ga_append(&line_ga, TAB);
2089 msg_puts((char_u *)" ");
2090 indent -= 8;
2091 }
2092 while (indent-- > 0)
2093 {
2094 ga_append(&line_ga, ' ');
2095 msg_putchar(' ');
2096 }
2097 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002098 ++no_mapping;
2099 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 /*
2102 * Get the line, one character at a time.
2103 */
2104 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002105 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
2107 if (ga_grow(&line_ga, 40) == FAIL)
2108 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002109 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002111 /* Get one character at a time. Don't use inchar(), it can't handle
2112 * special characters. */
2113 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114
2115 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002116 * Handle line editing.
2117 * Previously this was left to the system, putting the terminal in
2118 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002120 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002122 msg_putchar('\n');
2123 break;
2124 }
2125
2126 if (!escaped)
2127 {
2128 /* CR typed means "enter", which is NL */
2129 if (c1 == '\r')
2130 c1 = '\n';
2131
2132 if (c1 == BS || c1 == K_BS
2133 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002135 if (line_ga.ga_len > 0)
2136 {
2137 --line_ga.ga_len;
2138 goto redraw;
2139 }
2140 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 }
2142
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002143 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002145 msg_col = startcol;
2146 msg_clr_eos();
2147 line_ga.ga_len = 0;
2148 continue;
2149 }
2150
2151 if (c1 == Ctrl_T)
2152 {
2153 p = (char_u *)line_ga.ga_data;
2154 p[line_ga.ga_len] = NUL;
2155 indent = get_indent_str(p, 8);
2156 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2157add_indent:
2158 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002160 char_u *s = skipwhite(p);
2161
2162 ga_grow(&line_ga, 1);
2163 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2164 *s = ' ';
2165 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002167redraw:
2168 /* redraw the line */
2169 msg_col = startcol;
2170 windgoto(msg_row, msg_col);
2171 vcol = 0;
2172 for (p = (char_u *)line_ga.ga_data;
2173 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002175 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002177 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002179 msg_putchar(' ');
2180 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002184 msg_outtrans_len(p, 1);
2185 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002188 msg_clr_eos();
2189 continue;
2190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002192 if (c1 == Ctrl_D)
2193 {
2194 /* Delete one shiftwidth. */
2195 p = (char_u *)line_ga.ga_data;
2196 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002198 if (prev_char == '^')
2199 ex_keep_indent = TRUE;
2200 indent = 0;
2201 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 }
2203 else
2204 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002205 p[line_ga.ga_len] = NUL;
2206 indent = get_indent_str(p, 8);
2207 --indent;
2208 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002210 while (get_indent_str(p, 8) > indent)
2211 {
2212 char_u *s = skipwhite(p);
2213
2214 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2215 --line_ga.ga_len;
2216 }
2217 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219
2220 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2221 {
2222 escaped = TRUE;
2223 continue;
2224 }
2225
2226 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2227 if (IS_SPECIAL(c1))
2228 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002230
2231 if (IS_SPECIAL(c1))
2232 c1 = '?';
2233 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2234 prev_char = c1;
2235 if (c1 == '\n')
2236 msg_putchar('\n');
2237 else if (c1 == TAB)
2238 {
2239 /* Don't use chartabsize(), 'ts' can be different */
2240 do
2241 {
2242 msg_putchar(' ');
2243 } while (++vcol % 8);
2244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002247 msg_outtrans_len(
2248 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2249 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002251 ++line_ga.ga_len;
2252 escaped = FALSE;
2253
2254 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002255 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002256
2257 /* we are done when a NL is entered, but not when it comes after a
2258 * backslash */
2259 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2260 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002263 --pend;
2264 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 }
2267 }
2268
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002269 --no_mapping;
2270 --allow_keys;
2271
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 /* make following messages go to the next line */
2273 msg_didout = FALSE;
2274 msg_col = 0;
2275 if (msg_row < Rows - 1)
2276 ++msg_row;
2277 emsg_on_display = FALSE; /* don't want ui_delay() */
2278
2279 if (got_int)
2280 ga_clear(&line_ga);
2281
2282 return (char_u *)line_ga.ga_data;
2283}
2284
Bram Moolenaare344bea2005-09-01 20:46:49 +00002285# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2286 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287/*
2288 * Return TRUE if ccline.overstrike is on.
2289 */
2290 int
2291cmdline_overstrike()
2292{
2293 return ccline.overstrike;
2294}
2295
2296/*
2297 * Return TRUE if the cursor is at the end of the cmdline.
2298 */
2299 int
2300cmdline_at_end()
2301{
2302 return (ccline.cmdpos >= ccline.cmdlen);
2303}
2304#endif
2305
Bram Moolenaar9372a112005-12-06 19:59:18 +00002306#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307/*
2308 * Return the virtual column number at the current cursor position.
2309 * This is used by the IM code to obtain the start of the preedit string.
2310 */
2311 colnr_T
2312cmdline_getvcol_cursor()
2313{
2314 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2315 return MAXCOL;
2316
2317# ifdef FEAT_MBYTE
2318 if (has_mbyte)
2319 {
2320 colnr_T col;
2321 int i = 0;
2322
2323 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002324 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326 return col;
2327 }
2328 else
2329# endif
2330 return ccline.cmdpos;
2331}
2332#endif
2333
2334#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2335/*
2336 * If part of the command line is an IM preedit string, redraw it with
2337 * IM feedback attributes. The cursor position is restored after drawing.
2338 */
2339 static void
2340redrawcmd_preedit()
2341{
2342 if ((State & CMDLINE)
2343 && xic != NULL
2344 && im_get_status()
2345 && !p_imdisable
2346 && im_is_preediting())
2347 {
2348 int cmdpos = 0;
2349 int cmdspos;
2350 int old_row;
2351 int old_col;
2352 colnr_T col;
2353
2354 old_row = msg_row;
2355 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002356 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358# ifdef FEAT_MBYTE
2359 if (has_mbyte)
2360 {
2361 for (col = 0; col < preedit_start_col
2362 && cmdpos < ccline.cmdlen; ++col)
2363 {
2364 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002365 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
2367 }
2368 else
2369# endif
2370 {
2371 cmdspos += preedit_start_col;
2372 cmdpos += preedit_start_col;
2373 }
2374
2375 msg_row = cmdline_row + (cmdspos / (int)Columns);
2376 msg_col = cmdspos % (int)Columns;
2377 if (msg_row >= Rows)
2378 msg_row = Rows - 1;
2379
2380 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2381 {
2382 int char_len;
2383 int char_attr;
2384
2385 char_attr = im_get_feedback_attr(col);
2386 if (char_attr < 0)
2387 break; /* end of preedit string */
2388
2389# ifdef FEAT_MBYTE
2390 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002391 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 else
2393# endif
2394 char_len = 1;
2395
2396 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2397 cmdpos += char_len;
2398 }
2399
2400 msg_row = old_row;
2401 msg_col = old_col;
2402 }
2403}
2404#endif /* FEAT_XIM && FEAT_GUI_GTK */
2405
2406/*
2407 * Allocate a new command line buffer.
2408 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2409 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2410 */
2411 static void
2412alloc_cmdbuff(len)
2413 int len;
2414{
2415 /*
2416 * give some extra space to avoid having to allocate all the time
2417 */
2418 if (len < 80)
2419 len = 100;
2420 else
2421 len += 20;
2422
2423 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2424 ccline.cmdbufflen = len;
2425}
2426
2427/*
2428 * Re-allocate the command line to length len + something extra.
2429 * return FAIL for failure, OK otherwise
2430 */
2431 static int
2432realloc_cmdbuff(len)
2433 int len;
2434{
2435 char_u *p;
2436
2437 p = ccline.cmdbuff;
2438 alloc_cmdbuff(len); /* will get some more */
2439 if (ccline.cmdbuff == NULL) /* out of memory */
2440 {
2441 ccline.cmdbuff = p; /* keep the old one */
2442 return FAIL;
2443 }
2444 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2445 vim_free(p);
2446 return OK;
2447}
2448
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002449#if defined(FEAT_ARABIC) || defined(PROTO)
2450static char_u *arshape_buf = NULL;
2451
2452# if defined(EXITFREE) || defined(PROTO)
2453 void
2454free_cmdline_buf()
2455{
2456 vim_free(arshape_buf);
2457}
2458# endif
2459#endif
2460
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461/*
2462 * Draw part of the cmdline at the current cursor position. But draw stars
2463 * when cmdline_star is TRUE.
2464 */
2465 static void
2466draw_cmdline(start, len)
2467 int start;
2468 int len;
2469{
2470#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2471 int i;
2472
2473 if (cmdline_star > 0)
2474 for (i = 0; i < len; ++i)
2475 {
2476 msg_putchar('*');
2477# ifdef FEAT_MBYTE
2478 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002479 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480# endif
2481 }
2482 else
2483#endif
2484#ifdef FEAT_ARABIC
2485 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 static int buflen = 0;
2488 char_u *p;
2489 int j;
2490 int newlen = 0;
2491 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002492 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 int prev_c = 0;
2494 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002495 int u8c;
2496 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
2499 /*
2500 * Do arabic shaping into a temporary buffer. This is very
2501 * inefficient!
2502 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002503 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 /* Re-allocate the buffer. We keep it around to avoid a lot of
2506 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002507 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002508 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002509 arshape_buf = alloc(buflen);
2510 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 return; /* out of memory */
2512 }
2513
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002514 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2515 {
2516 /* Prepend a space to draw the leading composing char on. */
2517 arshape_buf[0] = ' ';
2518 newlen = 1;
2519 }
2520
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 for (j = start; j < start + len; j += mb_l)
2522 {
2523 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002524 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002525 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (ARABIC_CHAR(u8c))
2527 {
2528 /* Do Arabic shaping. */
2529 if (cmdmsg_rl)
2530 {
2531 /* displaying from right to left */
2532 pc = prev_c;
2533 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002534 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (j + mb_l >= start + len)
2536 nc = NUL;
2537 else
2538 nc = utf_ptr2char(p + mb_l);
2539 }
2540 else
2541 {
2542 /* displaying from left to right */
2543 if (j + mb_l >= start + len)
2544 pc = NUL;
2545 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002546 {
2547 int pcc[MAX_MCO];
2548
2549 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002551 pc1 = pcc[0];
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 nc = prev_c;
2554 }
2555 prev_c = u8c;
2556
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002557 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002559 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002560 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002562 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2563 if (u8cc[1] != 0)
2564 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002565 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567 }
2568 else
2569 {
2570 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002571 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 newlen += mb_l;
2573 }
2574 }
2575
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002576 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 }
2578 else
2579#endif
2580 msg_outtrans_len(ccline.cmdbuff + start, len);
2581}
2582
2583/*
2584 * Put a character on the command line. Shifts the following text to the
2585 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2586 * "c" must be printable (fit in one display cell)!
2587 */
2588 void
2589putcmdline(c, shift)
2590 int c;
2591 int shift;
2592{
2593 if (cmd_silent)
2594 return;
2595 msg_no_more = TRUE;
2596 msg_putchar(c);
2597 if (shift)
2598 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2599 msg_no_more = FALSE;
2600 cursorcmd();
2601}
2602
2603/*
2604 * Undo a putcmdline(c, FALSE).
2605 */
2606 void
2607unputcmdline()
2608{
2609 if (cmd_silent)
2610 return;
2611 msg_no_more = TRUE;
2612 if (ccline.cmdlen == ccline.cmdpos)
2613 msg_putchar(' ');
2614 else
2615 draw_cmdline(ccline.cmdpos, 1);
2616 msg_no_more = FALSE;
2617 cursorcmd();
2618}
2619
2620/*
2621 * Put the given string, of the given length, onto the command line.
2622 * If len is -1, then STRLEN() is used to calculate the length.
2623 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2624 * part will be redrawn, otherwise it will not. If this function is called
2625 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2626 * called afterwards.
2627 */
2628 int
2629put_on_cmdline(str, len, redraw)
2630 char_u *str;
2631 int len;
2632 int redraw;
2633{
2634 int retval;
2635 int i;
2636 int m;
2637 int c;
2638
2639 if (len < 0)
2640 len = (int)STRLEN(str);
2641
2642 /* Check if ccline.cmdbuff needs to be longer */
2643 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2644 retval = realloc_cmdbuff(ccline.cmdlen + len);
2645 else
2646 retval = OK;
2647 if (retval == OK)
2648 {
2649 if (!ccline.overstrike)
2650 {
2651 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2652 ccline.cmdbuff + ccline.cmdpos,
2653 (size_t)(ccline.cmdlen - ccline.cmdpos));
2654 ccline.cmdlen += len;
2655 }
2656 else
2657 {
2658#ifdef FEAT_MBYTE
2659 if (has_mbyte)
2660 {
2661 /* Count nr of characters in the new string. */
2662 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002663 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 ++m;
2665 /* Count nr of bytes in cmdline that are overwritten by these
2666 * characters. */
2667 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002668 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 --m;
2670 if (i < ccline.cmdlen)
2671 {
2672 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2673 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2674 ccline.cmdlen += ccline.cmdpos + len - i;
2675 }
2676 else
2677 ccline.cmdlen = ccline.cmdpos + len;
2678 }
2679 else
2680#endif
2681 if (ccline.cmdpos + len > ccline.cmdlen)
2682 ccline.cmdlen = ccline.cmdpos + len;
2683 }
2684 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2685 ccline.cmdbuff[ccline.cmdlen] = NUL;
2686
2687#ifdef FEAT_MBYTE
2688 if (enc_utf8)
2689 {
2690 /* When the inserted text starts with a composing character,
2691 * backup to the character before it. There could be two of them.
2692 */
2693 i = 0;
2694 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2695 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2696 {
2697 i = (*mb_head_off)(ccline.cmdbuff,
2698 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2699 ccline.cmdpos -= i;
2700 len += i;
2701 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2702 }
2703# ifdef FEAT_ARABIC
2704 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2705 {
2706 /* Check the previous character for Arabic combining pair. */
2707 i = (*mb_head_off)(ccline.cmdbuff,
2708 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2709 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2710 + ccline.cmdpos - i), c))
2711 {
2712 ccline.cmdpos -= i;
2713 len += i;
2714 }
2715 else
2716 i = 0;
2717 }
2718# endif
2719 if (i != 0)
2720 {
2721 /* Also backup the cursor position. */
2722 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2723 ccline.cmdspos -= i;
2724 msg_col -= i;
2725 if (msg_col < 0)
2726 {
2727 msg_col += Columns;
2728 --msg_row;
2729 }
2730 }
2731 }
2732#endif
2733
2734 if (redraw && !cmd_silent)
2735 {
2736 msg_no_more = TRUE;
2737 i = cmdline_row;
2738 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2739 /* Avoid clearing the rest of the line too often. */
2740 if (cmdline_row != i || ccline.overstrike)
2741 msg_clr_eos();
2742 msg_no_more = FALSE;
2743 }
2744#ifdef FEAT_FKMAP
2745 /*
2746 * If we are in Farsi command mode, the character input must be in
2747 * Insert mode. So do not advance the cmdpos.
2748 */
2749 if (!cmd_fkmap)
2750#endif
2751 {
2752 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002753 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002755 if (m < 0) /* overflow, Columns or Rows at weird value */
2756 m = MAXCOL;
2757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 else
2759 m = MAXCOL;
2760 for (i = 0; i < len; ++i)
2761 {
2762 c = cmdline_charsize(ccline.cmdpos);
2763#ifdef FEAT_MBYTE
2764 /* count ">" for a double-wide char that doesn't fit. */
2765 if (has_mbyte)
2766 correct_cmdspos(ccline.cmdpos, c);
2767#endif
2768 /* Stop cursor at the end of the screen */
2769 if (ccline.cmdspos + c >= m)
2770 break;
2771 ccline.cmdspos += c;
2772#ifdef FEAT_MBYTE
2773 if (has_mbyte)
2774 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002775 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 if (c > len - i - 1)
2777 c = len - i - 1;
2778 ccline.cmdpos += c;
2779 i += c;
2780 }
2781#endif
2782 ++ccline.cmdpos;
2783 }
2784 }
2785 }
2786 if (redraw)
2787 msg_check();
2788 return retval;
2789}
2790
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002791static struct cmdline_info prev_ccline;
2792static int prev_ccline_used = FALSE;
2793
2794/*
2795 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2796 * and overwrite it. But get_cmdline_str() may need it, thus make it
2797 * available globally in prev_ccline.
2798 */
2799 static void
2800save_cmdline(ccp)
2801 struct cmdline_info *ccp;
2802{
2803 if (!prev_ccline_used)
2804 {
2805 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2806 prev_ccline_used = TRUE;
2807 }
2808 *ccp = prev_ccline;
2809 prev_ccline = ccline;
2810 ccline.cmdbuff = NULL;
2811 ccline.cmdprompt = NULL;
2812}
2813
2814/*
2815 * Resture ccline after it has been saved with save_cmdline().
2816 */
2817 static void
2818restore_cmdline(ccp)
2819 struct cmdline_info *ccp;
2820{
2821 ccline = prev_ccline;
2822 prev_ccline = *ccp;
2823}
2824
Bram Moolenaar8299df92004-07-10 09:47:34 +00002825/*
2826 * paste a yank register into the command line.
2827 * used by CTRL-R command in command-line mode
2828 * insert_reg() can't be used here, because special characters from the
2829 * register contents will be interpreted as commands.
2830 *
2831 * return FAIL for failure, OK otherwise
2832 */
2833 static int
2834cmdline_paste(regname, literally)
2835 int regname;
2836 int literally; /* Insert text literally instead of "as typed" */
2837{
2838 long i;
2839 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002840 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002841 int allocated;
2842 struct cmdline_info save_ccline;
2843
2844 /* check for valid regname; also accept special characters for CTRL-R in
2845 * the command line */
2846 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2847 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2848 return FAIL;
2849
2850 /* A register containing CTRL-R can cause an endless loop. Allow using
2851 * CTRL-C to break the loop. */
2852 line_breakcheck();
2853 if (got_int)
2854 return FAIL;
2855
2856#ifdef FEAT_CLIPBOARD
2857 regname = may_get_selection(regname);
2858#endif
2859
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002860 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002861 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002862 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002863 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002864 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002865 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002866 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002867
2868 if (i)
2869 {
2870 /* Got the value of a special register in "arg". */
2871 if (arg == NULL)
2872 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002873
2874 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2875 * part of the word. */
2876 p = arg;
2877 if (p_is && regname == Ctrl_W)
2878 {
2879 char_u *w;
2880 int len;
2881
2882 /* Locate start of last word in the cmd buffer. */
2883 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
2884 {
2885#ifdef FEAT_MBYTE
2886 if (has_mbyte)
2887 {
2888 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
2889 if (!vim_iswordc(mb_ptr2char(w - len)))
2890 break;
2891 w -= len;
2892 }
2893 else
2894#endif
2895 {
2896 if (!vim_iswordc(w[-1]))
2897 break;
2898 --w;
2899 }
2900 }
2901 len = (ccline.cmdbuff + ccline.cmdlen) - w;
2902 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
2903 p += len;
2904 }
2905
2906 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002907 if (allocated)
2908 vim_free(arg);
2909 return OK;
2910 }
2911
2912 return cmdline_paste_reg(regname, literally);
2913}
2914
2915/*
2916 * Put a string on the command line.
2917 * When "literally" is TRUE, insert literally.
2918 * When "literally" is FALSE, insert as typed, but don't leave the command
2919 * line.
2920 */
2921 void
2922cmdline_paste_str(s, literally)
2923 char_u *s;
2924 int literally;
2925{
2926 int c, cv;
2927
2928 if (literally)
2929 put_on_cmdline(s, -1, TRUE);
2930 else
2931 while (*s != NUL)
2932 {
2933 cv = *s;
2934 if (cv == Ctrl_V && s[1])
2935 ++s;
2936#ifdef FEAT_MBYTE
2937 if (has_mbyte)
2938 {
2939 c = mb_ptr2char(s);
2940 s += mb_char2len(c);
2941 }
2942 else
2943#endif
2944 c = *s++;
2945 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2946#ifdef UNIX
2947 || c == intr_char
2948#endif
2949 || (c == Ctrl_BSL && *s == Ctrl_N))
2950 stuffcharReadbuff(Ctrl_V);
2951 stuffcharReadbuff(c);
2952 }
2953}
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955#ifdef FEAT_WILDMENU
2956/*
2957 * Delete characters on the command line, from "from" to the current
2958 * position.
2959 */
2960 static void
2961cmdline_del(from)
2962 int from;
2963{
2964 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2965 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2966 ccline.cmdlen -= ccline.cmdpos - from;
2967 ccline.cmdpos = from;
2968}
2969#endif
2970
2971/*
2972 * this fuction is called when the screen size changes and with incremental
2973 * search
2974 */
2975 void
2976redrawcmdline()
2977{
2978 if (cmd_silent)
2979 return;
2980 need_wait_return = FALSE;
2981 compute_cmdrow();
2982 redrawcmd();
2983 cursorcmd();
2984}
2985
2986 static void
2987redrawcmdprompt()
2988{
2989 int i;
2990
2991 if (cmd_silent)
2992 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002993 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 msg_putchar(ccline.cmdfirstc);
2995 if (ccline.cmdprompt != NULL)
2996 {
2997 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2998 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2999 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003000 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 --ccline.cmdindent;
3002 }
3003 else
3004 for (i = ccline.cmdindent; i > 0; --i)
3005 msg_putchar(' ');
3006}
3007
3008/*
3009 * Redraw what is currently on the command line.
3010 */
3011 void
3012redrawcmd()
3013{
3014 if (cmd_silent)
3015 return;
3016
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003017 /* when 'incsearch' is set there may be no command line while redrawing */
3018 if (ccline.cmdbuff == NULL)
3019 {
3020 windgoto(cmdline_row, 0);
3021 msg_clr_eos();
3022 return;
3023 }
3024
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 msg_start();
3026 redrawcmdprompt();
3027
3028 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3029 msg_no_more = TRUE;
3030 draw_cmdline(0, ccline.cmdlen);
3031 msg_clr_eos();
3032 msg_no_more = FALSE;
3033
3034 set_cmdspos_cursor();
3035
3036 /*
3037 * An emsg() before may have set msg_scroll. This is used in normal mode,
3038 * in cmdline mode we can reset them now.
3039 */
3040 msg_scroll = FALSE; /* next message overwrites cmdline */
3041
3042 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3043 * in cmdline mode */
3044 skip_redraw = FALSE;
3045}
3046
3047 void
3048compute_cmdrow()
3049{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003050 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 cmdline_row = Rows - 1;
3052 else
3053 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3054 + W_STATUS_HEIGHT(lastwin);
3055}
3056
3057 static void
3058cursorcmd()
3059{
3060 if (cmd_silent)
3061 return;
3062
3063#ifdef FEAT_RIGHTLEFT
3064 if (cmdmsg_rl)
3065 {
3066 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3067 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3068 if (msg_row <= 0)
3069 msg_row = Rows - 1;
3070 }
3071 else
3072#endif
3073 {
3074 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3075 msg_col = ccline.cmdspos % (int)Columns;
3076 if (msg_row >= Rows)
3077 msg_row = Rows - 1;
3078 }
3079
3080 windgoto(msg_row, msg_col);
3081#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3082 redrawcmd_preedit();
3083#endif
3084#ifdef MCH_CURSOR_SHAPE
3085 mch_update_cursor();
3086#endif
3087}
3088
3089 void
3090gotocmdline(clr)
3091 int clr;
3092{
3093 msg_start();
3094#ifdef FEAT_RIGHTLEFT
3095 if (cmdmsg_rl)
3096 msg_col = Columns - 1;
3097 else
3098#endif
3099 msg_col = 0; /* always start in column 0 */
3100 if (clr) /* clear the bottom line(s) */
3101 msg_clr_eos(); /* will reset clear_cmdline */
3102 windgoto(cmdline_row, 0);
3103}
3104
3105/*
3106 * Check the word in front of the cursor for an abbreviation.
3107 * Called when the non-id character "c" has been entered.
3108 * When an abbreviation is recognized it is removed from the text with
3109 * backspaces and the replacement string is inserted, followed by "c".
3110 */
3111 static int
3112ccheck_abbr(c)
3113 int c;
3114{
3115 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3116 return FALSE;
3117
3118 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3119}
3120
3121/*
3122 * Return FAIL if this is not an appropriate context in which to do
3123 * completion of anything, return OK if it is (even if there are no matches).
3124 * For the caller, this means that the character is just passed through like a
3125 * normal character (instead of being expanded). This allows :s/^I^D etc.
3126 */
3127 static int
3128nextwild(xp, type, options)
3129 expand_T *xp;
3130 int type;
3131 int options; /* extra options for ExpandOne() */
3132{
3133 int i, j;
3134 char_u *p1;
3135 char_u *p2;
3136 int oldlen;
3137 int difflen;
3138 int v;
3139
3140 if (xp->xp_numfiles == -1)
3141 {
3142 set_expand_context(xp);
3143 cmd_showtail = expand_showtail(xp);
3144 }
3145
3146 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3147 {
3148 beep_flush();
3149 return OK; /* Something illegal on command line */
3150 }
3151 if (xp->xp_context == EXPAND_NOTHING)
3152 {
3153 /* Caller can use the character as a normal char instead */
3154 return FAIL;
3155 }
3156
3157 MSG_PUTS("..."); /* show that we are busy */
3158 out_flush();
3159
3160 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3161 oldlen = ccline.cmdpos - i;
3162
3163 if (type == WILD_NEXT || type == WILD_PREV)
3164 {
3165 /*
3166 * Get next/previous match for a previous expanded pattern.
3167 */
3168 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3169 }
3170 else
3171 {
3172 /*
3173 * Translate string into pattern and expand it.
3174 */
3175 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3176 p2 = NULL;
3177 else
3178 {
3179 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3180 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3181 |options, type);
3182 vim_free(p1);
3183 /* longest match: make sure it is not shorter (happens with :help */
3184 if (p2 != NULL && type == WILD_LONGEST)
3185 {
3186 for (j = 0; j < oldlen; ++j)
3187 if (ccline.cmdbuff[i + j] == '*'
3188 || ccline.cmdbuff[i + j] == '?')
3189 break;
3190 if ((int)STRLEN(p2) < j)
3191 {
3192 vim_free(p2);
3193 p2 = NULL;
3194 }
3195 }
3196 }
3197 }
3198
3199 if (p2 != NULL && !got_int)
3200 {
3201 difflen = (int)STRLEN(p2) - oldlen;
3202 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3203 {
3204 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3205 xp->xp_pattern = ccline.cmdbuff + i;
3206 }
3207 else
3208 v = OK;
3209 if (v == OK)
3210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003211 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3212 &ccline.cmdbuff[ccline.cmdpos],
3213 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3214 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 ccline.cmdlen += difflen;
3216 ccline.cmdpos += difflen;
3217 }
3218 }
3219 vim_free(p2);
3220
3221 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003222 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
3224 /* When expanding a ":map" command and no matches are found, assume that
3225 * the key is supposed to be inserted literally */
3226 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3227 return FAIL;
3228
3229 if (xp->xp_numfiles <= 0 && p2 == NULL)
3230 beep_flush();
3231 else if (xp->xp_numfiles == 1)
3232 /* free expanded pattern */
3233 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3234
3235 return OK;
3236}
3237
3238/*
3239 * Do wildcard expansion on the string 'str'.
3240 * Chars that should not be expanded must be preceded with a backslash.
3241 * Return a pointer to alloced memory containing the new string.
3242 * Return NULL for failure.
3243 *
3244 * Results are cached in xp->xp_files and xp->xp_numfiles.
3245 *
3246 * mode = WILD_FREE: just free previously expanded matches
3247 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3248 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3249 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3250 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3251 * mode = WILD_ALL: return all matches concatenated
3252 * mode = WILD_LONGEST: return longest matched part
3253 *
3254 * options = WILD_LIST_NOTFOUND: list entries without a match
3255 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3256 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3257 * options = WILD_NO_BEEP: Don't beep for multiple matches
3258 * options = WILD_ADD_SLASH: add a slash after directory names
3259 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3260 * options = WILD_SILENT: don't print warning messages
3261 * options = WILD_ESCAPE: put backslash before special chars
3262 *
3263 * The variables xp->xp_context and xp->xp_backslash must have been set!
3264 */
3265 char_u *
3266ExpandOne(xp, str, orig, options, mode)
3267 expand_T *xp;
3268 char_u *str;
3269 char_u *orig; /* allocated copy of original of expanded string */
3270 int options;
3271 int mode;
3272{
3273 char_u *ss = NULL;
3274 static int findex;
3275 static char_u *orig_save = NULL; /* kept value of orig */
3276 int i;
3277 long_u len;
3278 int non_suf_match; /* number without matching suffix */
3279
3280 /*
3281 * first handle the case of using an old match
3282 */
3283 if (mode == WILD_NEXT || mode == WILD_PREV)
3284 {
3285 if (xp->xp_numfiles > 0)
3286 {
3287 if (mode == WILD_PREV)
3288 {
3289 if (findex == -1)
3290 findex = xp->xp_numfiles;
3291 --findex;
3292 }
3293 else /* mode == WILD_NEXT */
3294 ++findex;
3295
3296 /*
3297 * When wrapping around, return the original string, set findex to
3298 * -1.
3299 */
3300 if (findex < 0)
3301 {
3302 if (orig_save == NULL)
3303 findex = xp->xp_numfiles - 1;
3304 else
3305 findex = -1;
3306 }
3307 if (findex >= xp->xp_numfiles)
3308 {
3309 if (orig_save == NULL)
3310 findex = 0;
3311 else
3312 findex = -1;
3313 }
3314#ifdef FEAT_WILDMENU
3315 if (p_wmnu)
3316 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3317 findex, cmd_showtail);
3318#endif
3319 if (findex == -1)
3320 return vim_strsave(orig_save);
3321 return vim_strsave(xp->xp_files[findex]);
3322 }
3323 else
3324 return NULL;
3325 }
3326
3327/* free old names */
3328 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3329 {
3330 FreeWild(xp->xp_numfiles, xp->xp_files);
3331 xp->xp_numfiles = -1;
3332 vim_free(orig_save);
3333 orig_save = NULL;
3334 }
3335 findex = 0;
3336
3337 if (mode == WILD_FREE) /* only release file name */
3338 return NULL;
3339
3340 if (xp->xp_numfiles == -1)
3341 {
3342 vim_free(orig_save);
3343 orig_save = orig;
3344
3345 /*
3346 * Do the expansion.
3347 */
3348 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3349 options) == FAIL)
3350 {
3351#ifdef FNAME_ILLEGAL
3352 /* Illegal file name has been silently skipped. But when there
3353 * are wildcards, the real problem is that there was no match,
3354 * causing the pattern to be added, which has illegal characters.
3355 */
3356 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3357 EMSG2(_(e_nomatch2), str);
3358#endif
3359 }
3360 else if (xp->xp_numfiles == 0)
3361 {
3362 if (!(options & WILD_SILENT))
3363 EMSG2(_(e_nomatch2), str);
3364 }
3365 else
3366 {
3367 /* Escape the matches for use on the command line. */
3368 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3369
3370 /*
3371 * Check for matching suffixes in file names.
3372 */
3373 if (mode != WILD_ALL && mode != WILD_LONGEST)
3374 {
3375 if (xp->xp_numfiles)
3376 non_suf_match = xp->xp_numfiles;
3377 else
3378 non_suf_match = 1;
3379 if ((xp->xp_context == EXPAND_FILES
3380 || xp->xp_context == EXPAND_DIRECTORIES)
3381 && xp->xp_numfiles > 1)
3382 {
3383 /*
3384 * More than one match; check suffix.
3385 * The files will have been sorted on matching suffix in
3386 * expand_wildcards, only need to check the first two.
3387 */
3388 non_suf_match = 0;
3389 for (i = 0; i < 2; ++i)
3390 if (match_suffix(xp->xp_files[i]))
3391 ++non_suf_match;
3392 }
3393 if (non_suf_match != 1)
3394 {
3395 /* Can we ever get here unless it's while expanding
3396 * interactively? If not, we can get rid of this all
3397 * together. Don't really want to wait for this message
3398 * (and possibly have to hit return to continue!).
3399 */
3400 if (!(options & WILD_SILENT))
3401 EMSG(_(e_toomany));
3402 else if (!(options & WILD_NO_BEEP))
3403 beep_flush();
3404 }
3405 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3406 ss = vim_strsave(xp->xp_files[0]);
3407 }
3408 }
3409 }
3410
3411 /* Find longest common part */
3412 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3413 {
3414 for (len = 0; xp->xp_files[0][len]; ++len)
3415 {
3416 for (i = 0; i < xp->xp_numfiles; ++i)
3417 {
3418#ifdef CASE_INSENSITIVE_FILENAME
3419 if (xp->xp_context == EXPAND_DIRECTORIES
3420 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003421 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 || xp->xp_context == EXPAND_BUFFERS)
3423 {
3424 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3425 TOLOWER_LOC(xp->xp_files[0][len]))
3426 break;
3427 }
3428 else
3429#endif
3430 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3431 break;
3432 }
3433 if (i < xp->xp_numfiles)
3434 {
3435 if (!(options & WILD_NO_BEEP))
3436 vim_beep();
3437 break;
3438 }
3439 }
3440 ss = alloc((unsigned)len + 1);
3441 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003442 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 findex = -1; /* next p_wc gets first one */
3444 }
3445
3446 /* Concatenate all matching names */
3447 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3448 {
3449 len = 0;
3450 for (i = 0; i < xp->xp_numfiles; ++i)
3451 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3452 ss = lalloc(len, TRUE);
3453 if (ss != NULL)
3454 {
3455 *ss = NUL;
3456 for (i = 0; i < xp->xp_numfiles; ++i)
3457 {
3458 STRCAT(ss, xp->xp_files[i]);
3459 if (i != xp->xp_numfiles - 1)
3460 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3461 }
3462 }
3463 }
3464
3465 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3466 ExpandCleanup(xp);
3467
3468 return ss;
3469}
3470
3471/*
3472 * Prepare an expand structure for use.
3473 */
3474 void
3475ExpandInit(xp)
3476 expand_T *xp;
3477{
3478 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003479#ifndef BACKSLASH_IN_FILENAME
3480 xp->xp_shell = FALSE;
3481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 xp->xp_numfiles = -1;
3483 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003484#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3485 xp->xp_arg = NULL;
3486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487}
3488
3489/*
3490 * Cleanup an expand structure after use.
3491 */
3492 void
3493ExpandCleanup(xp)
3494 expand_T *xp;
3495{
3496 if (xp->xp_numfiles >= 0)
3497 {
3498 FreeWild(xp->xp_numfiles, xp->xp_files);
3499 xp->xp_numfiles = -1;
3500 }
3501}
3502
3503 void
3504ExpandEscape(xp, str, numfiles, files, options)
3505 expand_T *xp;
3506 char_u *str;
3507 int numfiles;
3508 char_u **files;
3509 int options;
3510{
3511 int i;
3512 char_u *p;
3513
3514 /*
3515 * May change home directory back to "~"
3516 */
3517 if (options & WILD_HOME_REPLACE)
3518 tilde_replace(str, numfiles, files);
3519
3520 if (options & WILD_ESCAPE)
3521 {
3522 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003523 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 || xp->xp_context == EXPAND_BUFFERS
3525 || xp->xp_context == EXPAND_DIRECTORIES)
3526 {
3527 /*
3528 * Insert a backslash into a file name before a space, \, %, #
3529 * and wildmatch characters, except '~'.
3530 */
3531 for (i = 0; i < numfiles; ++i)
3532 {
3533 /* for ":set path=" we need to escape spaces twice */
3534 if (xp->xp_backslash == XP_BS_THREE)
3535 {
3536 p = vim_strsave_escaped(files[i], (char_u *)" ");
3537 if (p != NULL)
3538 {
3539 vim_free(files[i]);
3540 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003541#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 p = vim_strsave_escaped(files[i], (char_u *)" ");
3543 if (p != NULL)
3544 {
3545 vim_free(files[i]);
3546 files[i] = p;
3547 }
3548#endif
3549 }
3550 }
3551#ifdef BACKSLASH_IN_FILENAME
3552 {
3553 char_u buf[20];
3554 int j = 0;
3555
3556 /* Don't escape '[' and '{' if they are in 'isfname'. */
3557 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3558 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3559 buf[j++] = *p;
3560 buf[j] = NUL;
3561 p = vim_strsave_escaped(files[i], buf);
3562 }
3563#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003564 p = vim_strsave_escaped(files[i],
3565 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566#endif
3567 if (p != NULL)
3568 {
3569 vim_free(files[i]);
3570 files[i] = p;
3571 }
3572
3573 /* If 'str' starts with "\~", replace "~" at start of
3574 * files[i] with "\~". */
3575 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003576 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 }
3578 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003579
3580 /* If the first file starts with a '+' escape it. Otherwise it
3581 * could be seen as "+cmd". */
3582 if (*files[0] == '+')
3583 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
3585 else if (xp->xp_context == EXPAND_TAGS)
3586 {
3587 /*
3588 * Insert a backslash before characters in a tag name that
3589 * would terminate the ":tag" command.
3590 */
3591 for (i = 0; i < numfiles; ++i)
3592 {
3593 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3594 if (p != NULL)
3595 {
3596 vim_free(files[i]);
3597 files[i] = p;
3598 }
3599 }
3600 }
3601 }
3602}
3603
3604/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003605 * Put a backslash before the file name in "pp", which is in allocated memory.
3606 */
3607 static void
3608escape_fname(pp)
3609 char_u **pp;
3610{
3611 char_u *p;
3612
3613 p = alloc((unsigned)(STRLEN(*pp) + 2));
3614 if (p != NULL)
3615 {
3616 p[0] = '\\';
3617 STRCPY(p + 1, *pp);
3618 vim_free(*pp);
3619 *pp = p;
3620 }
3621}
3622
3623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 * For each file name in files[num_files]:
3625 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3626 */
3627 void
3628tilde_replace(orig_pat, num_files, files)
3629 char_u *orig_pat;
3630 int num_files;
3631 char_u **files;
3632{
3633 int i;
3634 char_u *p;
3635
3636 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3637 {
3638 for (i = 0; i < num_files; ++i)
3639 {
3640 p = home_replace_save(NULL, files[i]);
3641 if (p != NULL)
3642 {
3643 vim_free(files[i]);
3644 files[i] = p;
3645 }
3646 }
3647 }
3648}
3649
3650/*
3651 * Show all matches for completion on the command line.
3652 * Returns EXPAND_NOTHING when the character that triggered expansion should
3653 * be inserted like a normal character.
3654 */
3655/*ARGSUSED*/
3656 static int
3657showmatches(xp, wildmenu)
3658 expand_T *xp;
3659 int wildmenu;
3660{
3661#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3662 int num_files;
3663 char_u **files_found;
3664 int i, j, k;
3665 int maxlen;
3666 int lines;
3667 int columns;
3668 char_u *p;
3669 int lastlen;
3670 int attr;
3671 int showtail;
3672
3673 if (xp->xp_numfiles == -1)
3674 {
3675 set_expand_context(xp);
3676 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3677 &num_files, &files_found);
3678 showtail = expand_showtail(xp);
3679 if (i != EXPAND_OK)
3680 return i;
3681
3682 }
3683 else
3684 {
3685 num_files = xp->xp_numfiles;
3686 files_found = xp->xp_files;
3687 showtail = cmd_showtail;
3688 }
3689
3690#ifdef FEAT_WILDMENU
3691 if (!wildmenu)
3692 {
3693#endif
3694 msg_didany = FALSE; /* lines_left will be set */
3695 msg_start(); /* prepare for paging */
3696 msg_putchar('\n');
3697 out_flush();
3698 cmdline_row = msg_row;
3699 msg_didany = FALSE; /* lines_left will be set again */
3700 msg_start(); /* prepare for paging */
3701#ifdef FEAT_WILDMENU
3702 }
3703#endif
3704
3705 if (got_int)
3706 got_int = FALSE; /* only int. the completion, not the cmd line */
3707#ifdef FEAT_WILDMENU
3708 else if (wildmenu)
3709 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3710#endif
3711 else
3712 {
3713 /* find the length of the longest file name */
3714 maxlen = 0;
3715 for (i = 0; i < num_files; ++i)
3716 {
3717 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003718 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 || xp->xp_context == EXPAND_BUFFERS))
3720 {
3721 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3722 j = vim_strsize(NameBuff);
3723 }
3724 else
3725 j = vim_strsize(L_SHOWFILE(i));
3726 if (j > maxlen)
3727 maxlen = j;
3728 }
3729
3730 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3731 lines = num_files;
3732 else
3733 {
3734 /* compute the number of columns and lines for the listing */
3735 maxlen += 2; /* two spaces between file names */
3736 columns = ((int)Columns + 2) / maxlen;
3737 if (columns < 1)
3738 columns = 1;
3739 lines = (num_files + columns - 1) / columns;
3740 }
3741
3742 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3743
3744 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3745 {
3746 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3747 msg_clr_eos();
3748 msg_advance(maxlen - 3);
3749 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3750 }
3751
3752 /* list the files line by line */
3753 for (i = 0; i < lines; ++i)
3754 {
3755 lastlen = 999;
3756 for (k = i; k < num_files; k += lines)
3757 {
3758 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3759 {
3760 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3761 p = files_found[k] + STRLEN(files_found[k]) + 1;
3762 msg_advance(maxlen + 1);
3763 msg_puts(p);
3764 msg_advance(maxlen + 3);
3765 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3766 break;
3767 }
3768 for (j = maxlen - lastlen; --j >= 0; )
3769 msg_putchar(' ');
3770 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003771 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 || xp->xp_context == EXPAND_BUFFERS)
3773 {
3774 /* highlight directories */
3775 j = (mch_isdir(files_found[k]));
3776 if (showtail)
3777 p = L_SHOWFILE(k);
3778 else
3779 {
3780 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3781 TRUE);
3782 p = NameBuff;
3783 }
3784 }
3785 else
3786 {
3787 j = FALSE;
3788 p = L_SHOWFILE(k);
3789 }
3790 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3791 }
3792 if (msg_col > 0) /* when not wrapped around */
3793 {
3794 msg_clr_eos();
3795 msg_putchar('\n');
3796 }
3797 out_flush(); /* show one line at a time */
3798 if (got_int)
3799 {
3800 got_int = FALSE;
3801 break;
3802 }
3803 }
3804
3805 /*
3806 * we redraw the command below the lines that we have just listed
3807 * This is a bit tricky, but it saves a lot of screen updating.
3808 */
3809 cmdline_row = msg_row; /* will put it back later */
3810 }
3811
3812 if (xp->xp_numfiles == -1)
3813 FreeWild(num_files, files_found);
3814
3815 return EXPAND_OK;
3816}
3817
3818/*
3819 * Private gettail for showmatches() (and win_redr_status_matches()):
3820 * Find tail of file name path, but ignore trailing "/".
3821 */
3822 char_u *
3823sm_gettail(s)
3824 char_u *s;
3825{
3826 char_u *p;
3827 char_u *t = s;
3828 int had_sep = FALSE;
3829
3830 for (p = s; *p != NUL; )
3831 {
3832 if (vim_ispathsep(*p)
3833#ifdef BACKSLASH_IN_FILENAME
3834 && !rem_backslash(p)
3835#endif
3836 )
3837 had_sep = TRUE;
3838 else if (had_sep)
3839 {
3840 t = p;
3841 had_sep = FALSE;
3842 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003843 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 return t;
3846}
3847
3848/*
3849 * Return TRUE if we only need to show the tail of completion matches.
3850 * When not completing file names or there is a wildcard in the path FALSE is
3851 * returned.
3852 */
3853 static int
3854expand_showtail(xp)
3855 expand_T *xp;
3856{
3857 char_u *s;
3858 char_u *end;
3859
3860 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003861 if (xp->xp_context != EXPAND_FILES
3862 && xp->xp_context != EXPAND_SHELLCMD
3863 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 return FALSE;
3865
3866 end = gettail(xp->xp_pattern);
3867 if (end == xp->xp_pattern) /* there is no path separator */
3868 return FALSE;
3869
3870 for (s = xp->xp_pattern; s < end; s++)
3871 {
3872 /* Skip escaped wildcards. Only when the backslash is not a path
3873 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3874 if (rem_backslash(s))
3875 ++s;
3876 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3877 return FALSE;
3878 }
3879 return TRUE;
3880}
3881
3882/*
3883 * Prepare a string for expansion.
3884 * When expanding file names: The string will be used with expand_wildcards().
3885 * Copy the file name into allocated memory and add a '*' at the end.
3886 * When expanding other names: The string will be used with regcomp(). Copy
3887 * the name into allocated memory and prepend "^".
3888 */
3889 char_u *
3890addstar(fname, len, context)
3891 char_u *fname;
3892 int len;
3893 int context; /* EXPAND_FILES etc. */
3894{
3895 char_u *retval;
3896 int i, j;
3897 int new_len;
3898 char_u *tail;
3899
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003900 if (context != EXPAND_FILES
3901 && context != EXPAND_SHELLCMD
3902 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
3904 /*
3905 * Matching will be done internally (on something other than files).
3906 * So we convert the file-matching-type wildcards into our kind for
3907 * use with vim_regcomp(). First work out how long it will be:
3908 */
3909
3910 /* For help tags the translation is done in find_help_tags().
3911 * For a tag pattern starting with "/" no translation is needed. */
3912 if (context == EXPAND_HELP
3913 || context == EXPAND_COLORS
3914 || context == EXPAND_COMPILER
3915 || (context == EXPAND_TAGS && fname[0] == '/'))
3916 retval = vim_strnsave(fname, len);
3917 else
3918 {
3919 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3920 for (i = 0; i < len; i++)
3921 {
3922 if (fname[i] == '*' || fname[i] == '~')
3923 new_len++; /* '*' needs to be replaced by ".*"
3924 '~' needs to be replaced by "\~" */
3925
3926 /* Buffer names are like file names. "." should be literal */
3927 if (context == EXPAND_BUFFERS && fname[i] == '.')
3928 new_len++; /* "." becomes "\." */
3929
3930 /* Custom expansion takes care of special things, match
3931 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003932 if ((context == EXPAND_USER_DEFINED
3933 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 new_len++; /* '\' becomes "\\" */
3935 }
3936 retval = alloc(new_len);
3937 if (retval != NULL)
3938 {
3939 retval[0] = '^';
3940 j = 1;
3941 for (i = 0; i < len; i++, j++)
3942 {
3943 /* Skip backslash. But why? At least keep it for custom
3944 * expansion. */
3945 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003946 && context != EXPAND_USER_LIST
3947 && fname[i] == '\\'
3948 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 break;
3950
3951 switch (fname[i])
3952 {
3953 case '*': retval[j++] = '.';
3954 break;
3955 case '~': retval[j++] = '\\';
3956 break;
3957 case '?': retval[j] = '.';
3958 continue;
3959 case '.': if (context == EXPAND_BUFFERS)
3960 retval[j++] = '\\';
3961 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003962 case '\\': if (context == EXPAND_USER_DEFINED
3963 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 retval[j++] = '\\';
3965 break;
3966 }
3967 retval[j] = fname[i];
3968 }
3969 retval[j] = NUL;
3970 }
3971 }
3972 }
3973 else
3974 {
3975 retval = alloc(len + 4);
3976 if (retval != NULL)
3977 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003978 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
3980 /*
3981 * Don't add a star to ~, ~user, $var or `cmd`.
3982 * ~ would be at the start of the file name, but not the tail.
3983 * $ could be anywhere in the tail.
3984 * ` could be anywhere in the file name.
3985 */
3986 tail = gettail(retval);
3987 if ((*retval != '~' || tail != retval)
3988 && vim_strchr(tail, '$') == NULL
3989 && vim_strchr(retval, '`') == NULL)
3990 retval[len++] = '*';
3991 retval[len] = NUL;
3992 }
3993 }
3994 return retval;
3995}
3996
3997/*
3998 * Must parse the command line so far to work out what context we are in.
3999 * Completion can then be done based on that context.
4000 * This routine sets the variables:
4001 * xp->xp_pattern The start of the pattern to be expanded within
4002 * the command line (ends at the cursor).
4003 * xp->xp_context The type of thing to expand. Will be one of:
4004 *
4005 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4006 * the command line, like an unknown command. Caller
4007 * should beep.
4008 * EXPAND_NOTHING Unrecognised context for completion, use char like
4009 * a normal char, rather than for completion. eg
4010 * :s/^I/
4011 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4012 * it.
4013 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4014 * EXPAND_FILES After command with XFILE set, or after setting
4015 * with P_EXPAND set. eg :e ^I, :w>>^I
4016 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4017 * when we know only directories are of interest. eg
4018 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004019 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4021 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4022 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4023 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4024 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4025 * EXPAND_EVENTS Complete event names
4026 * EXPAND_SYNTAX Complete :syntax command arguments
4027 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4028 * EXPAND_AUGROUP Complete autocommand group names
4029 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4030 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4031 * eg :unmap a^I , :cunab x^I
4032 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4033 * eg :call sub^I
4034 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4035 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4036 * names in expressions, eg :while s^I
4037 * EXPAND_ENV_VARS Complete environment variable names
4038 */
4039 static void
4040set_expand_context(xp)
4041 expand_T *xp;
4042{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004043 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (ccline.cmdfirstc != ':'
4045#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004046 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004047 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048#endif
4049 )
4050 {
4051 xp->xp_context = EXPAND_NOTHING;
4052 return;
4053 }
4054 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4055}
4056
4057 void
4058set_cmd_context(xp, str, len, col)
4059 expand_T *xp;
4060 char_u *str; /* start of command line */
4061 int len; /* length of command line (excl. NUL) */
4062 int col; /* position of cursor */
4063{
4064 int old_char = NUL;
4065 char_u *nextcomm;
4066
4067 /*
4068 * Avoid a UMR warning from Purify, only save the character if it has been
4069 * written before.
4070 */
4071 if (col < len)
4072 old_char = str[col];
4073 str[col] = NUL;
4074 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004075
4076#ifdef FEAT_EVAL
4077 if (ccline.cmdfirstc == '=')
4078 /* pass CMD_SIZE because there is no real command */
4079 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004080 else if (ccline.input_fn)
4081 {
4082 xp->xp_context = ccline.xp_context;
4083 xp->xp_pattern = ccline.cmdbuff;
4084 xp->xp_arg = ccline.xp_arg;
4085 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004086 else
4087#endif
4088 while (nextcomm != NULL)
4089 nextcomm = set_one_cmd_context(xp, nextcomm);
4090
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 str[col] = old_char;
4092}
4093
4094/*
4095 * Expand the command line "str" from context "xp".
4096 * "xp" must have been set by set_cmd_context().
4097 * xp->xp_pattern points into "str", to where the text that is to be expanded
4098 * starts.
4099 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4100 * cursor.
4101 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4102 * key that triggered expansion literally.
4103 * Returns EXPAND_OK otherwise.
4104 */
4105 int
4106expand_cmdline(xp, str, col, matchcount, matches)
4107 expand_T *xp;
4108 char_u *str; /* start of command line */
4109 int col; /* position of cursor */
4110 int *matchcount; /* return: nr of matches */
4111 char_u ***matches; /* return: array of pointers to matches */
4112{
4113 char_u *file_str = NULL;
4114
4115 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4116 {
4117 beep_flush();
4118 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4119 }
4120 if (xp->xp_context == EXPAND_NOTHING)
4121 {
4122 /* Caller can use the character as a normal char instead */
4123 return EXPAND_NOTHING;
4124 }
4125
4126 /* add star to file name, or convert to regexp if not exp. files. */
4127 file_str = addstar(xp->xp_pattern,
4128 (int)(str + col - xp->xp_pattern), xp->xp_context);
4129 if (file_str == NULL)
4130 return EXPAND_UNSUCCESSFUL;
4131
4132 /* find all files that match the description */
4133 if (ExpandFromContext(xp, file_str, matchcount, matches,
4134 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4135 {
4136 *matchcount = 0;
4137 *matches = NULL;
4138 }
4139 vim_free(file_str);
4140
4141 return EXPAND_OK;
4142}
4143
4144#ifdef FEAT_MULTI_LANG
4145/*
4146 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4147 */
4148static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4149
4150 static void
4151cleanup_help_tags(num_file, file)
4152 int num_file;
4153 char_u **file;
4154{
4155 int i, j;
4156 int len;
4157
4158 for (i = 0; i < num_file; ++i)
4159 {
4160 len = (int)STRLEN(file[i]) - 3;
4161 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4162 {
4163 /* Sorting on priority means the same item in another language may
4164 * be anywhere. Search all items for a match up to the "@en". */
4165 for (j = 0; j < num_file; ++j)
4166 if (j != i
4167 && (int)STRLEN(file[j]) == len + 3
4168 && STRNCMP(file[i], file[j], len + 1) == 0)
4169 break;
4170 if (j == num_file)
4171 file[i][len] = NUL;
4172 }
4173 }
4174}
4175#endif
4176
4177/*
4178 * Do the expansion based on xp->xp_context and "pat".
4179 */
4180 static int
4181ExpandFromContext(xp, pat, num_file, file, options)
4182 expand_T *xp;
4183 char_u *pat;
4184 int *num_file;
4185 char_u ***file;
4186 int options;
4187{
4188#ifdef FEAT_CMDL_COMPL
4189 regmatch_T regmatch;
4190#endif
4191 int ret;
4192 int flags;
4193
4194 flags = EW_DIR; /* include directories */
4195 if (options & WILD_LIST_NOTFOUND)
4196 flags |= EW_NOTFOUND;
4197 if (options & WILD_ADD_SLASH)
4198 flags |= EW_ADDSLASH;
4199 if (options & WILD_KEEP_ALL)
4200 flags |= EW_KEEPALL;
4201 if (options & WILD_SILENT)
4202 flags |= EW_SILENT;
4203
4204 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4205 {
4206 /*
4207 * Expand file or directory names.
4208 */
4209 int free_pat = FALSE;
4210 int i;
4211
4212 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4213 * space */
4214 if (xp->xp_backslash != XP_BS_NONE)
4215 {
4216 free_pat = TRUE;
4217 pat = vim_strsave(pat);
4218 for (i = 0; pat[i]; ++i)
4219 if (pat[i] == '\\')
4220 {
4221 if (xp->xp_backslash == XP_BS_THREE
4222 && pat[i + 1] == '\\'
4223 && pat[i + 2] == '\\'
4224 && pat[i + 3] == ' ')
4225 STRCPY(pat + i, pat + i + 3);
4226 if (xp->xp_backslash == XP_BS_ONE
4227 && pat[i + 1] == ' ')
4228 STRCPY(pat + i, pat + i + 1);
4229 }
4230 }
4231
4232 if (xp->xp_context == EXPAND_FILES)
4233 flags |= EW_FILE;
4234 else
4235 flags = (flags | EW_DIR) & ~EW_FILE;
4236 ret = expand_wildcards(1, &pat, num_file, file, flags);
4237 if (free_pat)
4238 vim_free(pat);
4239 return ret;
4240 }
4241
4242 *file = (char_u **)"";
4243 *num_file = 0;
4244 if (xp->xp_context == EXPAND_HELP)
4245 {
4246 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4247 {
4248#ifdef FEAT_MULTI_LANG
4249 cleanup_help_tags(*num_file, *file);
4250#endif
4251 return OK;
4252 }
4253 return FAIL;
4254 }
4255
4256#ifndef FEAT_CMDL_COMPL
4257 return FAIL;
4258#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004259 if (xp->xp_context == EXPAND_SHELLCMD)
4260 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 if (xp->xp_context == EXPAND_OLD_SETTING)
4262 return ExpandOldSetting(num_file, file);
4263 if (xp->xp_context == EXPAND_BUFFERS)
4264 return ExpandBufnames(pat, num_file, file, options);
4265 if (xp->xp_context == EXPAND_TAGS
4266 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4267 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4268 if (xp->xp_context == EXPAND_COLORS)
4269 return ExpandRTDir(pat, num_file, file, "colors");
4270 if (xp->xp_context == EXPAND_COMPILER)
4271 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004272# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4273 if (xp->xp_context == EXPAND_USER_LIST)
4274 return ExpandUserList(xp, num_file, file);
4275# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276
4277 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4278 if (regmatch.regprog == NULL)
4279 return FAIL;
4280
4281 /* set ignore-case according to p_ic, p_scs and pat */
4282 regmatch.rm_ic = ignorecase(pat);
4283
4284 if (xp->xp_context == EXPAND_SETTINGS
4285 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4286 ret = ExpandSettings(xp, &regmatch, num_file, file);
4287 else if (xp->xp_context == EXPAND_MAPPINGS)
4288 ret = ExpandMappings(&regmatch, num_file, file);
4289# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4290 else if (xp->xp_context == EXPAND_USER_DEFINED)
4291 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4292# endif
4293 else
4294 {
4295 static struct expgen
4296 {
4297 int context;
4298 char_u *((*func)__ARGS((expand_T *, int)));
4299 int ic;
4300 } tab[] =
4301 {
4302 {EXPAND_COMMANDS, get_command_name, FALSE},
4303#ifdef FEAT_USR_CMDS
4304 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4305 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4306 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4307 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4308#endif
4309#ifdef FEAT_EVAL
4310 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4311 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4312 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4313 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4314#endif
4315#ifdef FEAT_MENU
4316 {EXPAND_MENUS, get_menu_name, FALSE},
4317 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4318#endif
4319#ifdef FEAT_SYN_HL
4320 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4321#endif
4322 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4323#ifdef FEAT_AUTOCMD
4324 {EXPAND_EVENTS, get_event_name, TRUE},
4325 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4326#endif
4327#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4328 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4329 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4330#endif
4331 {EXPAND_ENV_VARS, get_env_name, TRUE},
4332 };
4333 int i;
4334
4335 /*
4336 * Find a context in the table and call the ExpandGeneric() with the
4337 * right function to do the expansion.
4338 */
4339 ret = FAIL;
4340 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4341 if (xp->xp_context == tab[i].context)
4342 {
4343 if (tab[i].ic)
4344 regmatch.rm_ic = TRUE;
4345 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4346 break;
4347 }
4348 }
4349
4350 vim_free(regmatch.regprog);
4351
4352 return ret;
4353#endif /* FEAT_CMDL_COMPL */
4354}
4355
4356#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4357/*
4358 * Expand a list of names.
4359 *
4360 * Generic function for command line completion. It calls a function to
4361 * obtain strings, one by one. The strings are matched against a regexp
4362 * program. Matching strings are copied into an array, which is returned.
4363 *
4364 * Returns OK when no problems encountered, FAIL for error (out of memory).
4365 */
4366 int
4367ExpandGeneric(xp, regmatch, num_file, file, func)
4368 expand_T *xp;
4369 regmatch_T *regmatch;
4370 int *num_file;
4371 char_u ***file;
4372 char_u *((*func)__ARGS((expand_T *, int)));
4373 /* returns a string from the list */
4374{
4375 int i;
4376 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004377 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_u *str;
4379
4380 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004381 * round == 0: count the number of matching names
4382 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004384 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 for (i = 0; ; ++i)
4387 {
4388 str = (*func)(xp, i);
4389 if (str == NULL) /* end of list */
4390 break;
4391 if (*str == NUL) /* skip empty strings */
4392 continue;
4393
4394 if (vim_regexec(regmatch, str, (colnr_T)0))
4395 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004396 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
4398 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4399 (*file)[count] = str;
4400#ifdef FEAT_MENU
4401 if (func == get_menu_names && str != NULL)
4402 {
4403 /* test for separator added by get_menu_names() */
4404 str += STRLEN(str) - 1;
4405 if (*str == '\001')
4406 *str = '.';
4407 }
4408#endif
4409 }
4410 ++count;
4411 }
4412 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004413 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
4415 if (count == 0)
4416 return OK;
4417 *num_file = count;
4418 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4419 if (*file == NULL)
4420 {
4421 *file = (char_u **)"";
4422 return FAIL;
4423 }
4424 count = 0;
4425 }
4426 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004427
4428 /* Sort the results. */
4429 sort_strings(*file, *num_file);
4430
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 return OK;
4432}
4433
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004434/*
4435 * Complete a shell command.
4436 * Returns FAIL or OK;
4437 */
4438 static int
4439expand_shellcmd(filepat, num_file, file, flagsarg)
4440 char_u *filepat; /* pattern to match with command names */
4441 int *num_file; /* return: number of matches */
4442 char_u ***file; /* return: array with matches */
4443 int flagsarg; /* EW_ flags */
4444{
4445 char_u *pat;
4446 int i;
4447 char_u *path;
4448 int mustfree = FALSE;
4449 garray_T ga;
4450 char_u *buf = alloc(MAXPATHL);
4451 size_t l;
4452 char_u *s, *e;
4453 int flags = flagsarg;
4454 int ret;
4455
4456 if (buf == NULL)
4457 return FAIL;
4458
4459 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4460 * space */
4461 pat = vim_strsave(filepat);
4462 for (i = 0; pat[i]; ++i)
4463 if (pat[i] == '\\' && pat[i + 1] == ' ')
4464 STRCPY(pat + i, pat + i + 1);
4465
4466 flags |= EW_FILE | EW_EXEC;
4467
4468 /* For an absolute name we don't use $PATH. */
4469 if ((pat[0] == '.' && (vim_ispathsep(pat[1])
4470 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4471 path = (char_u *)".";
4472 else
4473 path = vim_getenv((char_u *)"PATH", &mustfree);
4474
4475 /*
4476 * Go over all directories in $PATH. Expand matches in that directory and
4477 * collect them in "ga".
4478 */
4479 ga_init2(&ga, (int)sizeof(char *), 10);
4480 for (s = path; *s != NUL; s = e)
4481 {
4482#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4483 e = vim_strchr(s, ';');
4484#else
4485 e = vim_strchr(s, ':');
4486#endif
4487 if (e == NULL)
4488 e = s + STRLEN(s);
4489
4490 l = e - s;
4491 if (l > MAXPATHL - 5)
4492 break;
4493 vim_strncpy(buf, s, l);
4494 add_pathsep(buf);
4495 l = STRLEN(buf);
4496 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4497
4498 /* Expand matches in one directory of $PATH. */
4499 ret = expand_wildcards(1, &buf, num_file, file, flags);
4500 if (ret == OK)
4501 {
4502 if (ga_grow(&ga, *num_file) == FAIL)
4503 FreeWild(*num_file, *file);
4504 else
4505 {
4506 for (i = 0; i < *num_file; ++i)
4507 {
4508 s = (*file)[i];
4509 if (STRLEN(s) > l)
4510 {
4511 /* Remove the path again. */
4512 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4513 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4514 }
4515 else
4516 vim_free(s);
4517 }
4518 vim_free(*file);
4519 }
4520 }
4521 if (*e != NUL)
4522 ++e;
4523 }
4524 *file = ga.ga_data;
4525 *num_file = ga.ga_len;
4526
4527 vim_free(buf);
4528 vim_free(pat);
4529 if (mustfree)
4530 vim_free(path);
4531 return OK;
4532}
4533
4534
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004536static 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));
4537
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004539 * call "user_expand_func()" to invoke a user defined VimL function and return
4540 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004542 static void *
4543call_user_expand_func(user_expand_func, xp, num_file, file)
4544 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 int *num_file;
4547 char_u ***file;
4548{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004549 char_u keep;
4550 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004553 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004554 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555
4556 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004557 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 *num_file = 0;
4559 *file = NULL;
4560
4561 keep = ccline.cmdbuff[ccline.cmdlen];
4562 ccline.cmdbuff[ccline.cmdlen] = 0;
4563 sprintf((char *)num, "%d", ccline.cmdpos);
4564 args[0] = xp->xp_pattern;
4565 args[1] = ccline.cmdbuff;
4566 args[2] = num;
4567
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004568 /* Save the cmdline, we don't know what the function may do. */
4569 save_ccline = ccline;
4570 ccline.cmdbuff = NULL;
4571 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004573
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004574 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004575
4576 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004580
4581 return ret;
4582}
4583
4584/*
4585 * Expand names with a function defined by the user.
4586 */
4587 static int
4588ExpandUserDefined(xp, regmatch, num_file, file)
4589 expand_T *xp;
4590 regmatch_T *regmatch;
4591 int *num_file;
4592 char_u ***file;
4593{
4594 char_u *retstr;
4595 char_u *s;
4596 char_u *e;
4597 char_u keep;
4598 garray_T ga;
4599
4600 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4601 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 return FAIL;
4603
4604 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004605 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 e = vim_strchr(s, '\n');
4608 if (e == NULL)
4609 e = s + STRLEN(s);
4610 keep = *e;
4611 *e = 0;
4612
4613 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4614 {
4615 *e = keep;
4616 if (*e != NUL)
4617 ++e;
4618 continue;
4619 }
4620
4621 if (ga_grow(&ga, 1) == FAIL)
4622 break;
4623
4624 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4625 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
4627 *e = keep;
4628 if (*e != NUL)
4629 ++e;
4630 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004631 vim_free(retstr);
4632 *file = ga.ga_data;
4633 *num_file = ga.ga_len;
4634 return OK;
4635}
4636
4637/*
4638 * Expand names with a list returned by a function defined by the user.
4639 */
4640 static int
4641ExpandUserList(xp, num_file, file)
4642 expand_T *xp;
4643 int *num_file;
4644 char_u ***file;
4645{
4646 list_T *retlist;
4647 listitem_T *li;
4648 garray_T ga;
4649
4650 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4651 if (retlist == NULL)
4652 return FAIL;
4653
4654 ga_init2(&ga, (int)sizeof(char *), 3);
4655 /* Loop over the items in the list. */
4656 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4657 {
4658 if (li->li_tv.v_type != VAR_STRING)
4659 continue; /* Skip non-string items */
4660
4661 if (ga_grow(&ga, 1) == FAIL)
4662 break;
4663
4664 ((char_u **)ga.ga_data)[ga.ga_len] =
4665 vim_strsave(li->li_tv.vval.v_string);
4666 ++ga.ga_len;
4667 }
4668 list_unref(retlist);
4669
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 *file = ga.ga_data;
4671 *num_file = ga.ga_len;
4672 return OK;
4673}
4674#endif
4675
4676/*
4677 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4678 * or compiler names.
4679 */
4680 static int
4681ExpandRTDir(pat, num_file, file, dirname)
4682 char_u *pat;
4683 int *num_file;
4684 char_u ***file;
4685 char *dirname; /* "colors" or "compiler" */
4686{
4687 char_u *all;
4688 char_u *s;
4689 char_u *e;
4690 garray_T ga;
4691
4692 *num_file = 0;
4693 *file = NULL;
4694 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4695 if (s == NULL)
4696 return FAIL;
4697 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4698 all = globpath(p_rtp, s);
4699 vim_free(s);
4700 if (all == NULL)
4701 return FAIL;
4702
4703 ga_init2(&ga, (int)sizeof(char *), 3);
4704 for (s = all; *s != NUL; s = e)
4705 {
4706 e = vim_strchr(s, '\n');
4707 if (e == NULL)
4708 e = s + STRLEN(s);
4709 if (ga_grow(&ga, 1) == FAIL)
4710 break;
4711 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4712 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004713 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (*s == '\n' || vim_ispathsep(*s))
4715 break;
4716 ++s;
4717 ((char_u **)ga.ga_data)[ga.ga_len] =
4718 vim_strnsave(s, (int)(e - s - 4));
4719 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
4721 if (*e != NUL)
4722 ++e;
4723 }
4724 vim_free(all);
4725 *file = ga.ga_data;
4726 *num_file = ga.ga_len;
4727 return OK;
4728}
4729
4730#endif
4731
4732#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4733/*
4734 * Expand "file" for all comma-separated directories in "path".
4735 * Returns an allocated string with all matches concatenated, separated by
4736 * newlines. Returns NULL for an error or no matches.
4737 */
4738 char_u *
4739globpath(path, file)
4740 char_u *path;
4741 char_u *file;
4742{
4743 expand_T xpc;
4744 char_u *buf;
4745 garray_T ga;
4746 int i;
4747 int len;
4748 int num_p;
4749 char_u **p;
4750 char_u *cur = NULL;
4751
4752 buf = alloc(MAXPATHL);
4753 if (buf == NULL)
4754 return NULL;
4755
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004756 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 ga_init2(&ga, 1, 100);
4760
4761 /* Loop over all entries in {path}. */
4762 while (*path != NUL)
4763 {
4764 /* Copy one item of the path to buf[] and concatenate the file name. */
4765 copy_option_part(&path, buf, MAXPATHL, ",");
4766 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4767 {
4768 add_pathsep(buf);
4769 STRCAT(buf, file);
4770 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4771 && num_p > 0)
4772 {
4773 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4774 for (len = 0, i = 0; i < num_p; ++i)
4775 len += (long_u)STRLEN(p[i]) + 1;
4776
4777 /* Concatenate new results to previous ones. */
4778 if (ga_grow(&ga, len) == OK)
4779 {
4780 cur = (char_u *)ga.ga_data + ga.ga_len;
4781 for (i = 0; i < num_p; ++i)
4782 {
4783 STRCPY(cur, p[i]);
4784 cur += STRLEN(p[i]);
4785 *cur++ = '\n';
4786 }
4787 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 FreeWild(num_p, p);
4790 }
4791 }
4792 }
4793 if (cur != NULL)
4794 *--cur = 0; /* Replace trailing newline with NUL */
4795
4796 vim_free(buf);
4797 return (char_u *)ga.ga_data;
4798}
4799
4800#endif
4801
4802#if defined(FEAT_CMDHIST) || defined(PROTO)
4803
4804/*********************************
4805 * Command line history stuff *
4806 *********************************/
4807
4808/*
4809 * Translate a history character to the associated type number.
4810 */
4811 static int
4812hist_char2type(c)
4813 int c;
4814{
4815 if (c == ':')
4816 return HIST_CMD;
4817 if (c == '=')
4818 return HIST_EXPR;
4819 if (c == '@')
4820 return HIST_INPUT;
4821 if (c == '>')
4822 return HIST_DEBUG;
4823 return HIST_SEARCH; /* must be '?' or '/' */
4824}
4825
4826/*
4827 * Table of history names.
4828 * These names are used in :history and various hist...() functions.
4829 * It is sufficient to give the significant prefix of a history name.
4830 */
4831
4832static char *(history_names[]) =
4833{
4834 "cmd",
4835 "search",
4836 "expr",
4837 "input",
4838 "debug",
4839 NULL
4840};
4841
4842/*
4843 * init_history() - Initialize the command line history.
4844 * Also used to re-allocate the history when the size changes.
4845 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004846 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847init_history()
4848{
4849 int newlen; /* new length of history table */
4850 histentry_T *temp;
4851 int i;
4852 int j;
4853 int type;
4854
4855 /*
4856 * If size of history table changed, reallocate it
4857 */
4858 newlen = (int)p_hi;
4859 if (newlen != hislen) /* history length changed */
4860 {
4861 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4862 {
4863 if (newlen)
4864 {
4865 temp = (histentry_T *)lalloc(
4866 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4867 if (temp == NULL) /* out of memory! */
4868 {
4869 if (type == 0) /* first one: just keep the old length */
4870 {
4871 newlen = hislen;
4872 break;
4873 }
4874 /* Already changed one table, now we can only have zero
4875 * length for all tables. */
4876 newlen = 0;
4877 type = -1;
4878 continue;
4879 }
4880 }
4881 else
4882 temp = NULL;
4883 if (newlen == 0 || temp != NULL)
4884 {
4885 if (hisidx[type] < 0) /* there are no entries yet */
4886 {
4887 for (i = 0; i < newlen; ++i)
4888 {
4889 temp[i].hisnum = 0;
4890 temp[i].hisstr = NULL;
4891 }
4892 }
4893 else if (newlen > hislen) /* array becomes bigger */
4894 {
4895 for (i = 0; i <= hisidx[type]; ++i)
4896 temp[i] = history[type][i];
4897 j = i;
4898 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4899 {
4900 temp[i].hisnum = 0;
4901 temp[i].hisstr = NULL;
4902 }
4903 for ( ; j < hislen; ++i, ++j)
4904 temp[i] = history[type][j];
4905 }
4906 else /* array becomes smaller or 0 */
4907 {
4908 j = hisidx[type];
4909 for (i = newlen - 1; ; --i)
4910 {
4911 if (i >= 0) /* copy newest entries */
4912 temp[i] = history[type][j];
4913 else /* remove older entries */
4914 vim_free(history[type][j].hisstr);
4915 if (--j < 0)
4916 j = hislen - 1;
4917 if (j == hisidx[type])
4918 break;
4919 }
4920 hisidx[type] = newlen - 1;
4921 }
4922 vim_free(history[type]);
4923 history[type] = temp;
4924 }
4925 }
4926 hislen = newlen;
4927 }
4928}
4929
4930/*
4931 * Check if command line 'str' is already in history.
4932 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4933 */
4934 static int
4935in_history(type, str, move_to_front)
4936 int type;
4937 char_u *str;
4938 int move_to_front; /* Move the entry to the front if it exists */
4939{
4940 int i;
4941 int last_i = -1;
4942
4943 if (hisidx[type] < 0)
4944 return FALSE;
4945 i = hisidx[type];
4946 do
4947 {
4948 if (history[type][i].hisstr == NULL)
4949 return FALSE;
4950 if (STRCMP(str, history[type][i].hisstr) == 0)
4951 {
4952 if (!move_to_front)
4953 return TRUE;
4954 last_i = i;
4955 break;
4956 }
4957 if (--i < 0)
4958 i = hislen - 1;
4959 } while (i != hisidx[type]);
4960
4961 if (last_i >= 0)
4962 {
4963 str = history[type][i].hisstr;
4964 while (i != hisidx[type])
4965 {
4966 if (++i >= hislen)
4967 i = 0;
4968 history[type][last_i] = history[type][i];
4969 last_i = i;
4970 }
4971 history[type][i].hisstr = str;
4972 history[type][i].hisnum = ++hisnum[type];
4973 return TRUE;
4974 }
4975 return FALSE;
4976}
4977
4978/*
4979 * Convert history name (from table above) to its HIST_ equivalent.
4980 * When "name" is empty, return "cmd" history.
4981 * Returns -1 for unknown history name.
4982 */
4983 int
4984get_histtype(name)
4985 char_u *name;
4986{
4987 int i;
4988 int len = (int)STRLEN(name);
4989
4990 /* No argument: use current history. */
4991 if (len == 0)
4992 return hist_char2type(ccline.cmdfirstc);
4993
4994 for (i = 0; history_names[i] != NULL; ++i)
4995 if (STRNICMP(name, history_names[i], len) == 0)
4996 return i;
4997
4998 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4999 return hist_char2type(name[0]);
5000
5001 return -1;
5002}
5003
5004static int last_maptick = -1; /* last seen maptick */
5005
5006/*
5007 * Add the given string to the given history. If the string is already in the
5008 * history then it is moved to the front. "histype" may be one of he HIST_
5009 * values.
5010 */
5011 void
5012add_to_history(histype, new_entry, in_map, sep)
5013 int histype;
5014 char_u *new_entry;
5015 int in_map; /* consider maptick when inside a mapping */
5016 int sep; /* separator character used (search hist) */
5017{
5018 histentry_T *hisptr;
5019 int len;
5020
5021 if (hislen == 0) /* no history */
5022 return;
5023
5024 /*
5025 * Searches inside the same mapping overwrite each other, so that only
5026 * the last line is kept. Be careful not to remove a line that was moved
5027 * down, only lines that were added.
5028 */
5029 if (histype == HIST_SEARCH && in_map)
5030 {
5031 if (maptick == last_maptick)
5032 {
5033 /* Current line is from the same mapping, remove it */
5034 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5035 vim_free(hisptr->hisstr);
5036 hisptr->hisstr = NULL;
5037 hisptr->hisnum = 0;
5038 --hisnum[histype];
5039 if (--hisidx[HIST_SEARCH] < 0)
5040 hisidx[HIST_SEARCH] = hislen - 1;
5041 }
5042 last_maptick = -1;
5043 }
5044 if (!in_history(histype, new_entry, TRUE))
5045 {
5046 if (++hisidx[histype] == hislen)
5047 hisidx[histype] = 0;
5048 hisptr = &history[histype][hisidx[histype]];
5049 vim_free(hisptr->hisstr);
5050
5051 /* Store the separator after the NUL of the string. */
5052 len = STRLEN(new_entry);
5053 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5054 if (hisptr->hisstr != NULL)
5055 hisptr->hisstr[len + 1] = sep;
5056
5057 hisptr->hisnum = ++hisnum[histype];
5058 if (histype == HIST_SEARCH && in_map)
5059 last_maptick = maptick;
5060 }
5061}
5062
5063#if defined(FEAT_EVAL) || defined(PROTO)
5064
5065/*
5066 * Get identifier of newest history entry.
5067 * "histype" may be one of the HIST_ values.
5068 */
5069 int
5070get_history_idx(histype)
5071 int histype;
5072{
5073 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5074 || hisidx[histype] < 0)
5075 return -1;
5076
5077 return history[histype][hisidx[histype]].hisnum;
5078}
5079
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005080static struct cmdline_info *get_ccline_ptr __ARGS((void));
5081
5082/*
5083 * Get pointer to the command line info to use. cmdline_paste() may clear
5084 * ccline and put the previous value in prev_ccline.
5085 */
5086 static struct cmdline_info *
5087get_ccline_ptr()
5088{
5089 if ((State & CMDLINE) == 0)
5090 return NULL;
5091 if (ccline.cmdbuff != NULL)
5092 return &ccline;
5093 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5094 return &prev_ccline;
5095 return NULL;
5096}
5097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098/*
5099 * Get the current command line in allocated memory.
5100 * Only works when the command line is being edited.
5101 * Returns NULL when something is wrong.
5102 */
5103 char_u *
5104get_cmdline_str()
5105{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005106 struct cmdline_info *p = get_ccline_ptr();
5107
5108 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005110 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111}
5112
5113/*
5114 * Get the current command line position, counted in bytes.
5115 * Zero is the first position.
5116 * Only works when the command line is being edited.
5117 * Returns -1 when something is wrong.
5118 */
5119 int
5120get_cmdline_pos()
5121{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005122 struct cmdline_info *p = get_ccline_ptr();
5123
5124 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005126 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127}
5128
5129/*
5130 * Set the command line byte position to "pos". Zero is the first position.
5131 * Only works when the command line is being edited.
5132 * Returns 1 when failed, 0 when OK.
5133 */
5134 int
5135set_cmdline_pos(pos)
5136 int pos;
5137{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005138 struct cmdline_info *p = get_ccline_ptr();
5139
5140 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 return 1;
5142
5143 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5144 * changed the command line. */
5145 if (pos < 0)
5146 new_cmdpos = 0;
5147 else
5148 new_cmdpos = pos;
5149 return 0;
5150}
5151
5152/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005153 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005154 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005155 * Only works when the command line is being edited.
5156 * Returns NUL when something is wrong.
5157 */
5158 int
5159get_cmdline_type()
5160{
5161 struct cmdline_info *p = get_ccline_ptr();
5162
5163 if (p == NULL)
5164 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005165 if (p->cmdfirstc == NUL)
5166 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005167 return p->cmdfirstc;
5168}
5169
5170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 * Calculate history index from a number:
5172 * num > 0: seen as identifying number of a history entry
5173 * num < 0: relative position in history wrt newest entry
5174 * "histype" may be one of the HIST_ values.
5175 */
5176 static int
5177calc_hist_idx(histype, num)
5178 int histype;
5179 int num;
5180{
5181 int i;
5182 histentry_T *hist;
5183 int wrapped = FALSE;
5184
5185 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5186 || (i = hisidx[histype]) < 0 || num == 0)
5187 return -1;
5188
5189 hist = history[histype];
5190 if (num > 0)
5191 {
5192 while (hist[i].hisnum > num)
5193 if (--i < 0)
5194 {
5195 if (wrapped)
5196 break;
5197 i += hislen;
5198 wrapped = TRUE;
5199 }
5200 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5201 return i;
5202 }
5203 else if (-num <= hislen)
5204 {
5205 i += num + 1;
5206 if (i < 0)
5207 i += hislen;
5208 if (hist[i].hisstr != NULL)
5209 return i;
5210 }
5211 return -1;
5212}
5213
5214/*
5215 * Get a history entry by its index.
5216 * "histype" may be one of the HIST_ values.
5217 */
5218 char_u *
5219get_history_entry(histype, idx)
5220 int histype;
5221 int idx;
5222{
5223 idx = calc_hist_idx(histype, idx);
5224 if (idx >= 0)
5225 return history[histype][idx].hisstr;
5226 else
5227 return (char_u *)"";
5228}
5229
5230/*
5231 * Clear all entries of a history.
5232 * "histype" may be one of the HIST_ values.
5233 */
5234 int
5235clr_history(histype)
5236 int histype;
5237{
5238 int i;
5239 histentry_T *hisptr;
5240
5241 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5242 {
5243 hisptr = history[histype];
5244 for (i = hislen; i--;)
5245 {
5246 vim_free(hisptr->hisstr);
5247 hisptr->hisnum = 0;
5248 hisptr++->hisstr = NULL;
5249 }
5250 hisidx[histype] = -1; /* mark history as cleared */
5251 hisnum[histype] = 0; /* reset identifier counter */
5252 return OK;
5253 }
5254 return FAIL;
5255}
5256
5257/*
5258 * Remove all entries matching {str} from a history.
5259 * "histype" may be one of the HIST_ values.
5260 */
5261 int
5262del_history_entry(histype, str)
5263 int histype;
5264 char_u *str;
5265{
5266 regmatch_T regmatch;
5267 histentry_T *hisptr;
5268 int idx;
5269 int i;
5270 int last;
5271 int found = FALSE;
5272
5273 regmatch.regprog = NULL;
5274 regmatch.rm_ic = FALSE; /* always match case */
5275 if (hislen != 0
5276 && histype >= 0
5277 && histype < HIST_COUNT
5278 && *str != NUL
5279 && (idx = hisidx[histype]) >= 0
5280 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5281 != NULL)
5282 {
5283 i = last = idx;
5284 do
5285 {
5286 hisptr = &history[histype][i];
5287 if (hisptr->hisstr == NULL)
5288 break;
5289 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5290 {
5291 found = TRUE;
5292 vim_free(hisptr->hisstr);
5293 hisptr->hisstr = NULL;
5294 hisptr->hisnum = 0;
5295 }
5296 else
5297 {
5298 if (i != last)
5299 {
5300 history[histype][last] = *hisptr;
5301 hisptr->hisstr = NULL;
5302 hisptr->hisnum = 0;
5303 }
5304 if (--last < 0)
5305 last += hislen;
5306 }
5307 if (--i < 0)
5308 i += hislen;
5309 } while (i != idx);
5310 if (history[histype][idx].hisstr == NULL)
5311 hisidx[histype] = -1;
5312 }
5313 vim_free(regmatch.regprog);
5314 return found;
5315}
5316
5317/*
5318 * Remove an indexed entry from a history.
5319 * "histype" may be one of the HIST_ values.
5320 */
5321 int
5322del_history_idx(histype, idx)
5323 int histype;
5324 int idx;
5325{
5326 int i, j;
5327
5328 i = calc_hist_idx(histype, idx);
5329 if (i < 0)
5330 return FALSE;
5331 idx = hisidx[histype];
5332 vim_free(history[histype][i].hisstr);
5333
5334 /* When deleting the last added search string in a mapping, reset
5335 * last_maptick, so that the last added search string isn't deleted again.
5336 */
5337 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5338 last_maptick = -1;
5339
5340 while (i != idx)
5341 {
5342 j = (i + 1) % hislen;
5343 history[histype][i] = history[histype][j];
5344 i = j;
5345 }
5346 history[histype][i].hisstr = NULL;
5347 history[histype][i].hisnum = 0;
5348 if (--i < 0)
5349 i += hislen;
5350 hisidx[histype] = i;
5351 return TRUE;
5352}
5353
5354#endif /* FEAT_EVAL */
5355
5356#if defined(FEAT_CRYPT) || defined(PROTO)
5357/*
5358 * Very specific function to remove the value in ":set key=val" from the
5359 * history.
5360 */
5361 void
5362remove_key_from_history()
5363{
5364 char_u *p;
5365 int i;
5366
5367 i = hisidx[HIST_CMD];
5368 if (i < 0)
5369 return;
5370 p = history[HIST_CMD][i].hisstr;
5371 if (p != NULL)
5372 for ( ; *p; ++p)
5373 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5374 {
5375 p = vim_strchr(p + 3, '=');
5376 if (p == NULL)
5377 break;
5378 ++p;
5379 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5380 if (p[i] == '\\' && p[i + 1])
5381 ++i;
5382 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5383 --p;
5384 }
5385}
5386#endif
5387
5388#endif /* FEAT_CMDHIST */
5389
5390#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5391/*
5392 * Get indices "num1,num2" that specify a range within a list (not a range of
5393 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5394 * Returns OK if parsed successfully, otherwise FAIL.
5395 */
5396 int
5397get_list_range(str, num1, num2)
5398 char_u **str;
5399 int *num1;
5400 int *num2;
5401{
5402 int len;
5403 int first = FALSE;
5404 long num;
5405
5406 *str = skipwhite(*str);
5407 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5408 {
5409 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5410 *str += len;
5411 *num1 = (int)num;
5412 first = TRUE;
5413 }
5414 *str = skipwhite(*str);
5415 if (**str == ',') /* parse "to" part of range */
5416 {
5417 *str = skipwhite(*str + 1);
5418 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5419 if (len > 0)
5420 {
5421 *num2 = (int)num;
5422 *str = skipwhite(*str + len);
5423 }
5424 else if (!first) /* no number given at all */
5425 return FAIL;
5426 }
5427 else if (first) /* only one number given */
5428 *num2 = *num1;
5429 return OK;
5430}
5431#endif
5432
5433#if defined(FEAT_CMDHIST) || defined(PROTO)
5434/*
5435 * :history command - print a history
5436 */
5437 void
5438ex_history(eap)
5439 exarg_T *eap;
5440{
5441 histentry_T *hist;
5442 int histype1 = HIST_CMD;
5443 int histype2 = HIST_CMD;
5444 int hisidx1 = 1;
5445 int hisidx2 = -1;
5446 int idx;
5447 int i, j, k;
5448 char_u *end;
5449 char_u *arg = eap->arg;
5450
5451 if (hislen == 0)
5452 {
5453 MSG(_("'history' option is zero"));
5454 return;
5455 }
5456
5457 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5458 {
5459 end = arg;
5460 while (ASCII_ISALPHA(*end)
5461 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5462 end++;
5463 i = *end;
5464 *end = NUL;
5465 histype1 = get_histtype(arg);
5466 if (histype1 == -1)
5467 {
5468 if (STRICMP(arg, "all") == 0)
5469 {
5470 histype1 = 0;
5471 histype2 = HIST_COUNT-1;
5472 }
5473 else
5474 {
5475 *end = i;
5476 EMSG(_(e_trailing));
5477 return;
5478 }
5479 }
5480 else
5481 histype2 = histype1;
5482 *end = i;
5483 }
5484 else
5485 end = arg;
5486 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5487 {
5488 EMSG(_(e_trailing));
5489 return;
5490 }
5491
5492 for (; !got_int && histype1 <= histype2; ++histype1)
5493 {
5494 STRCPY(IObuff, "\n # ");
5495 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5496 MSG_PUTS_TITLE(IObuff);
5497 idx = hisidx[histype1];
5498 hist = history[histype1];
5499 j = hisidx1;
5500 k = hisidx2;
5501 if (j < 0)
5502 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5503 if (k < 0)
5504 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5505 if (idx >= 0 && j <= k)
5506 for (i = idx + 1; !got_int; ++i)
5507 {
5508 if (i == hislen)
5509 i = 0;
5510 if (hist[i].hisstr != NULL
5511 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5512 {
5513 msg_putchar('\n');
5514 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5515 hist[i].hisnum);
5516 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5517 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5518 (int)Columns - 10);
5519 else
5520 STRCAT(IObuff, hist[i].hisstr);
5521 msg_outtrans(IObuff);
5522 out_flush();
5523 }
5524 if (i == idx)
5525 break;
5526 }
5527 }
5528}
5529#endif
5530
5531#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5532static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5533static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5534static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5535static int viminfo_add_at_front = FALSE;
5536
5537static int hist_type2char __ARGS((int type, int use_question));
5538
5539/*
5540 * Translate a history type number to the associated character.
5541 */
5542 static int
5543hist_type2char(type, use_question)
5544 int type;
5545 int use_question; /* use '?' instead of '/' */
5546{
5547 if (type == HIST_CMD)
5548 return ':';
5549 if (type == HIST_SEARCH)
5550 {
5551 if (use_question)
5552 return '?';
5553 else
5554 return '/';
5555 }
5556 if (type == HIST_EXPR)
5557 return '=';
5558 return '@';
5559}
5560
5561/*
5562 * Prepare for reading the history from the viminfo file.
5563 * This allocates history arrays to store the read history lines.
5564 */
5565 void
5566prepare_viminfo_history(asklen)
5567 int asklen;
5568{
5569 int i;
5570 int num;
5571 int type;
5572 int len;
5573
5574 init_history();
5575 viminfo_add_at_front = (asklen != 0);
5576 if (asklen > hislen)
5577 asklen = hislen;
5578
5579 for (type = 0; type < HIST_COUNT; ++type)
5580 {
5581 /*
5582 * Count the number of empty spaces in the history list. If there are
5583 * more spaces available than we request, then fill them up.
5584 */
5585 for (i = 0, num = 0; i < hislen; i++)
5586 if (history[type][i].hisstr == NULL)
5587 num++;
5588 len = asklen;
5589 if (num > len)
5590 len = num;
5591 if (len <= 0)
5592 viminfo_history[type] = NULL;
5593 else
5594 viminfo_history[type] =
5595 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5596 if (viminfo_history[type] == NULL)
5597 len = 0;
5598 viminfo_hislen[type] = len;
5599 viminfo_hisidx[type] = 0;
5600 }
5601}
5602
5603/*
5604 * Accept a line from the viminfo, store it in the history array when it's
5605 * new.
5606 */
5607 int
5608read_viminfo_history(virp)
5609 vir_T *virp;
5610{
5611 int type;
5612 long_u len;
5613 char_u *val;
5614 char_u *p;
5615
5616 type = hist_char2type(virp->vir_line[0]);
5617 if (viminfo_hisidx[type] < viminfo_hislen[type])
5618 {
5619 val = viminfo_readstring(virp, 1, TRUE);
5620 if (val != NULL && *val != NUL)
5621 {
5622 if (!in_history(type, val + (type == HIST_SEARCH),
5623 viminfo_add_at_front))
5624 {
5625 /* Need to re-allocate to append the separator byte. */
5626 len = STRLEN(val);
5627 p = lalloc(len + 2, TRUE);
5628 if (p != NULL)
5629 {
5630 if (type == HIST_SEARCH)
5631 {
5632 /* Search entry: Move the separator from the first
5633 * column to after the NUL. */
5634 mch_memmove(p, val + 1, (size_t)len);
5635 p[len] = (*val == ' ' ? NUL : *val);
5636 }
5637 else
5638 {
5639 /* Not a search entry: No separator in the viminfo
5640 * file, add a NUL separator. */
5641 mch_memmove(p, val, (size_t)len + 1);
5642 p[len + 1] = NUL;
5643 }
5644 viminfo_history[type][viminfo_hisidx[type]++] = p;
5645 }
5646 }
5647 }
5648 vim_free(val);
5649 }
5650 return viminfo_readline(virp);
5651}
5652
5653 void
5654finish_viminfo_history()
5655{
5656 int idx;
5657 int i;
5658 int type;
5659
5660 for (type = 0; type < HIST_COUNT; ++type)
5661 {
5662 if (history[type] == NULL)
5663 return;
5664 idx = hisidx[type] + viminfo_hisidx[type];
5665 if (idx >= hislen)
5666 idx -= hislen;
5667 else if (idx < 0)
5668 idx = hislen - 1;
5669 if (viminfo_add_at_front)
5670 hisidx[type] = idx;
5671 else
5672 {
5673 if (hisidx[type] == -1)
5674 hisidx[type] = hislen - 1;
5675 do
5676 {
5677 if (history[type][idx].hisstr != NULL)
5678 break;
5679 if (++idx == hislen)
5680 idx = 0;
5681 } while (idx != hisidx[type]);
5682 if (idx != hisidx[type] && --idx < 0)
5683 idx = hislen - 1;
5684 }
5685 for (i = 0; i < viminfo_hisidx[type]; i++)
5686 {
5687 vim_free(history[type][idx].hisstr);
5688 history[type][idx].hisstr = viminfo_history[type][i];
5689 if (--idx < 0)
5690 idx = hislen - 1;
5691 }
5692 idx += 1;
5693 idx %= hislen;
5694 for (i = 0; i < viminfo_hisidx[type]; i++)
5695 {
5696 history[type][idx++].hisnum = ++hisnum[type];
5697 idx %= hislen;
5698 }
5699 vim_free(viminfo_history[type]);
5700 viminfo_history[type] = NULL;
5701 }
5702}
5703
5704 void
5705write_viminfo_history(fp)
5706 FILE *fp;
5707{
5708 int i;
5709 int type;
5710 int num_saved;
5711 char_u *p;
5712 int c;
5713
5714 init_history();
5715 if (hislen == 0)
5716 return;
5717 for (type = 0; type < HIST_COUNT; ++type)
5718 {
5719 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5720 if (num_saved == 0)
5721 continue;
5722 if (num_saved < 0) /* Use default */
5723 num_saved = hislen;
5724 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5725 type == HIST_CMD ? _("Command Line") :
5726 type == HIST_SEARCH ? _("Search String") :
5727 type == HIST_EXPR ? _("Expression") :
5728 _("Input Line"));
5729 if (num_saved > hislen)
5730 num_saved = hislen;
5731 i = hisidx[type];
5732 if (i >= 0)
5733 while (num_saved--)
5734 {
5735 p = history[type][i].hisstr;
5736 if (p != NULL)
5737 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005738 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 /* For the search history: put the separator in the second
5740 * column; use a space if there isn't one. */
5741 if (type == HIST_SEARCH)
5742 {
5743 c = p[STRLEN(p) + 1];
5744 putc(c == NUL ? ' ' : c, fp);
5745 }
5746 viminfo_writestring(fp, p);
5747 }
5748 if (--i < 0)
5749 i = hislen - 1;
5750 }
5751 }
5752}
5753#endif /* FEAT_VIMINFO */
5754
5755#if defined(FEAT_FKMAP) || defined(PROTO)
5756/*
5757 * Write a character at the current cursor+offset position.
5758 * It is directly written into the command buffer block.
5759 */
5760 void
5761cmd_pchar(c, offset)
5762 int c, offset;
5763{
5764 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5765 {
5766 EMSG(_("E198: cmd_pchar beyond the command length"));
5767 return;
5768 }
5769 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5770 ccline.cmdbuff[ccline.cmdlen] = NUL;
5771}
5772
5773 int
5774cmd_gchar(offset)
5775 int offset;
5776{
5777 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5778 {
5779 /* EMSG(_("cmd_gchar beyond the command length")); */
5780 return NUL;
5781 }
5782 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5783}
5784#endif
5785
5786#if defined(FEAT_CMDWIN) || defined(PROTO)
5787/*
5788 * Open a window on the current command line and history. Allow editing in
5789 * the window. Returns when the window is closed.
5790 * Returns:
5791 * CR if the command is to be executed
5792 * Ctrl_C if it is to be abandoned
5793 * K_IGNORE if editing continues
5794 */
5795 static int
5796ex_window()
5797{
5798 struct cmdline_info save_ccline;
5799 buf_T *old_curbuf = curbuf;
5800 win_T *old_curwin = curwin;
5801 buf_T *bp;
5802 win_T *wp;
5803 int i;
5804 linenr_T lnum;
5805 int histtype;
5806 garray_T winsizes;
5807 char_u typestr[2];
5808 int save_restart_edit = restart_edit;
5809 int save_State = State;
5810 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005811#ifdef FEAT_RIGHTLEFT
5812 int save_cmdmsg_rl = cmdmsg_rl;
5813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
5815 /* Can't do this recursively. Can't do it when typing a password. */
5816 if (cmdwin_type != 0
5817# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5818 || cmdline_star > 0
5819# endif
5820 )
5821 {
5822 beep_flush();
5823 return K_IGNORE;
5824 }
5825
5826 /* Save current window sizes. */
5827 win_size_save(&winsizes);
5828
5829# ifdef FEAT_AUTOCMD
5830 /* Don't execute autocommands while creating the window. */
5831 ++autocmd_block;
5832# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005833 /* don't use a new tab page */
5834 cmdmod.tab = 0;
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 /* Create a window for the command-line buffer. */
5837 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5838 {
5839 beep_flush();
5840 return K_IGNORE;
5841 }
5842 cmdwin_type = ccline.cmdfirstc;
5843 if (cmdwin_type == NUL)
5844 cmdwin_type = '-';
5845
5846 /* Create the command-line buffer empty. */
5847 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5848 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5849 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5850 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5851 curbuf->b_p_ma = TRUE;
5852# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005853 curwin->w_p_rl = cmdmsg_rl;
5854 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855# endif
5856# ifdef FEAT_SCROLLBIND
5857 curwin->w_p_scb = FALSE;
5858# endif
5859
5860# ifdef FEAT_AUTOCMD
5861 /* Do execute autocommands for setting the filetype (load syntax). */
5862 --autocmd_block;
5863# endif
5864
Bram Moolenaar46152342005-09-07 21:18:43 +00005865 /* Showing the prompt may have set need_wait_return, reset it. */
5866 need_wait_return = FALSE;
5867
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 histtype = hist_char2type(ccline.cmdfirstc);
5869 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5870 {
5871 if (p_wc == TAB)
5872 {
5873 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5874 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5875 }
5876 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5877 }
5878
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005879 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5880 * sets 'textwidth' to 78). */
5881 curbuf->b_p_tw = 0;
5882
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 /* Fill the buffer with the history. */
5884 init_history();
5885 if (hislen > 0)
5886 {
5887 i = hisidx[histtype];
5888 if (i >= 0)
5889 {
5890 lnum = 0;
5891 do
5892 {
5893 if (++i == hislen)
5894 i = 0;
5895 if (history[histtype][i].hisstr != NULL)
5896 ml_append(lnum++, history[histtype][i].hisstr,
5897 (colnr_T)0, FALSE);
5898 }
5899 while (i != hisidx[histtype]);
5900 }
5901 }
5902
5903 /* Replace the empty last line with the current command-line and put the
5904 * cursor there. */
5905 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5906 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5907 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005908 changed_line_abv_curs();
5909 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005910 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
5912 /* Save the command line info, can be used recursively. */
5913 save_ccline = ccline;
5914 ccline.cmdbuff = NULL;
5915 ccline.cmdprompt = NULL;
5916
5917 /* No Ex mode here! */
5918 exmode_active = 0;
5919
5920 State = NORMAL;
5921# ifdef FEAT_MOUSE
5922 setmouse();
5923# endif
5924
5925# ifdef FEAT_AUTOCMD
5926 /* Trigger CmdwinEnter autocommands. */
5927 typestr[0] = cmdwin_type;
5928 typestr[1] = NUL;
5929 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5930# endif
5931
5932 i = RedrawingDisabled;
5933 RedrawingDisabled = 0;
5934
5935 /*
5936 * Call the main loop until <CR> or CTRL-C is typed.
5937 */
5938 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005939 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940
5941 RedrawingDisabled = i;
5942
5943# ifdef FEAT_AUTOCMD
5944 /* Trigger CmdwinLeave autocommands. */
5945 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5946# endif
5947
5948 /* Restore the comand line info. */
5949 ccline = save_ccline;
5950 cmdwin_type = 0;
5951
5952 exmode_active = save_exmode;
5953
5954 /* Safety check: The old window or buffer was deleted: It's a a bug when
5955 * this happens! */
5956 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5957 {
5958 cmdwin_result = Ctrl_C;
5959 EMSG(_("E199: Active window or buffer deleted"));
5960 }
5961 else
5962 {
5963# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5964 /* autocmds may abort script processing */
5965 if (aborting() && cmdwin_result != K_IGNORE)
5966 cmdwin_result = Ctrl_C;
5967# endif
5968 /* Set the new command line from the cmdline buffer. */
5969 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00005970 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 {
Bram Moolenaar46152342005-09-07 21:18:43 +00005972 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
5973
5974 if (histtype == HIST_CMD)
5975 {
5976 /* Execute the command directly. */
5977 ccline.cmdbuff = vim_strsave((char_u *)p);
5978 cmdwin_result = CAR;
5979 }
5980 else
5981 {
5982 /* First need to cancel what we were doing. */
5983 ccline.cmdbuff = NULL;
5984 stuffcharReadbuff(':');
5985 stuffReadbuff((char_u *)p);
5986 stuffcharReadbuff(CAR);
5987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 }
5989 else if (cmdwin_result == K_XF2) /* :qa typed */
5990 {
5991 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5992 cmdwin_result = CAR;
5993 }
5994 else
5995 ccline.cmdbuff = vim_strsave(ml_get_curline());
5996 if (ccline.cmdbuff == NULL)
5997 cmdwin_result = Ctrl_C;
5998 else
5999 {
6000 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6001 ccline.cmdbufflen = ccline.cmdlen + 1;
6002 ccline.cmdpos = curwin->w_cursor.col;
6003 if (ccline.cmdpos > ccline.cmdlen)
6004 ccline.cmdpos = ccline.cmdlen;
6005 if (cmdwin_result == K_IGNORE)
6006 {
6007 set_cmdspos_cursor();
6008 redrawcmd();
6009 }
6010 }
6011
6012# ifdef FEAT_AUTOCMD
6013 /* Don't execute autocommands while deleting the window. */
6014 ++autocmd_block;
6015# endif
6016 wp = curwin;
6017 bp = curbuf;
6018 win_goto(old_curwin);
6019 win_close(wp, TRUE);
6020 close_buffer(NULL, bp, DOBUF_WIPE);
6021
6022 /* Restore window sizes. */
6023 win_size_restore(&winsizes);
6024
6025# ifdef FEAT_AUTOCMD
6026 --autocmd_block;
6027# endif
6028 }
6029
6030 ga_clear(&winsizes);
6031 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006032# ifdef FEAT_RIGHTLEFT
6033 cmdmsg_rl = save_cmdmsg_rl;
6034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035
6036 State = save_State;
6037# ifdef FEAT_MOUSE
6038 setmouse();
6039# endif
6040
6041 return cmdwin_result;
6042}
6043#endif /* FEAT_CMDWIN */
6044
6045/*
6046 * Used for commands that either take a simple command string argument, or:
6047 * cmd << endmarker
6048 * {script}
6049 * endmarker
6050 * Returns a pointer to allocated memory with {script} or NULL.
6051 */
6052 char_u *
6053script_get(eap, cmd)
6054 exarg_T *eap;
6055 char_u *cmd;
6056{
6057 char_u *theline;
6058 char *end_pattern = NULL;
6059 char dot[] = ".";
6060 garray_T ga;
6061
6062 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6063 return NULL;
6064
6065 ga_init2(&ga, 1, 0x400);
6066
6067 if (cmd[2] != NUL)
6068 end_pattern = (char *)skipwhite(cmd + 2);
6069 else
6070 end_pattern = dot;
6071
6072 for (;;)
6073 {
6074 theline = eap->getline(
6075#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006076 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077#endif
6078 NUL, eap->cookie, 0);
6079
6080 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6081 break;
6082
6083 ga_concat(&ga, theline);
6084 ga_append(&ga, '\n');
6085 vim_free(theline);
6086 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006087 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088
6089 return (char_u *)ga.ga_data;
6090}