blob: 8328619073bb36d8ea0a3ac589c7825dde920ff1 [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:
988 if (map_to_exists_mode((char_u *)"", LANGMAP))
989 {
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
1380 case Ctrl_L: /* longest common part */
1381 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1382 break;
1383 goto cmdline_changed;
1384
1385 case Ctrl_N: /* next match */
1386 case Ctrl_P: /* previous match */
1387 if (xpc.xp_numfiles > 0)
1388 {
1389 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1390 == FAIL)
1391 break;
1392 goto cmdline_changed;
1393 }
1394
1395#ifdef FEAT_CMDHIST
1396 case K_UP:
1397 case K_DOWN:
1398 case K_S_UP:
1399 case K_S_DOWN:
1400 case K_PAGEUP:
1401 case K_KPAGEUP:
1402 case K_PAGEDOWN:
1403 case K_KPAGEDOWN:
1404 if (hislen == 0 || firstc == NUL) /* no history */
1405 goto cmdline_not_changed;
1406
1407 i = hiscnt;
1408
1409 /* save current command string so it can be restored later */
1410 if (lookfor == NULL)
1411 {
1412 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1413 goto cmdline_not_changed;
1414 lookfor[ccline.cmdpos] = NUL;
1415 }
1416
1417 j = (int)STRLEN(lookfor);
1418 for (;;)
1419 {
1420 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001421 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001422 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
1424 if (hiscnt == hislen) /* first time */
1425 hiscnt = hisidx[histype];
1426 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1427 hiscnt = hislen - 1;
1428 else if (hiscnt != hisidx[histype] + 1)
1429 --hiscnt;
1430 else /* at top of list */
1431 {
1432 hiscnt = i;
1433 break;
1434 }
1435 }
1436 else /* one step forwards */
1437 {
1438 /* on last entry, clear the line */
1439 if (hiscnt == hisidx[histype])
1440 {
1441 hiscnt = hislen;
1442 break;
1443 }
1444
1445 /* not on a history line, nothing to do */
1446 if (hiscnt == hislen)
1447 break;
1448 if (hiscnt == hislen - 1) /* wrap around */
1449 hiscnt = 0;
1450 else
1451 ++hiscnt;
1452 }
1453 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1454 {
1455 hiscnt = i;
1456 break;
1457 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001458 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001459 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 || STRNCMP(history[histype][hiscnt].hisstr,
1461 lookfor, (size_t)j) == 0)
1462 break;
1463 }
1464
1465 if (hiscnt != i) /* jumped to other entry */
1466 {
1467 char_u *p;
1468 int len;
1469 int old_firstc;
1470
1471 vim_free(ccline.cmdbuff);
1472 if (hiscnt == hislen)
1473 p = lookfor; /* back to the old one */
1474 else
1475 p = history[histype][hiscnt].hisstr;
1476
1477 if (histype == HIST_SEARCH
1478 && p != lookfor
1479 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1480 {
1481 /* Correct for the separator character used when
1482 * adding the history entry vs the one used now.
1483 * First loop: count length.
1484 * Second loop: copy the characters. */
1485 for (i = 0; i <= 1; ++i)
1486 {
1487 len = 0;
1488 for (j = 0; p[j] != NUL; ++j)
1489 {
1490 /* Replace old sep with new sep, unless it is
1491 * escaped. */
1492 if (p[j] == old_firstc
1493 && (j == 0 || p[j - 1] != '\\'))
1494 {
1495 if (i > 0)
1496 ccline.cmdbuff[len] = firstc;
1497 }
1498 else
1499 {
1500 /* Escape new sep, unless it is already
1501 * escaped. */
1502 if (p[j] == firstc
1503 && (j == 0 || p[j - 1] != '\\'))
1504 {
1505 if (i > 0)
1506 ccline.cmdbuff[len] = '\\';
1507 ++len;
1508 }
1509 if (i > 0)
1510 ccline.cmdbuff[len] = p[j];
1511 }
1512 ++len;
1513 }
1514 if (i == 0)
1515 {
1516 alloc_cmdbuff(len);
1517 if (ccline.cmdbuff == NULL)
1518 goto returncmd;
1519 }
1520 }
1521 ccline.cmdbuff[len] = NUL;
1522 }
1523 else
1524 {
1525 alloc_cmdbuff((int)STRLEN(p));
1526 if (ccline.cmdbuff == NULL)
1527 goto returncmd;
1528 STRCPY(ccline.cmdbuff, p);
1529 }
1530
1531 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1532 redrawcmd();
1533 goto cmdline_changed;
1534 }
1535 beep_flush();
1536 goto cmdline_not_changed;
1537#endif
1538
1539 case Ctrl_V:
1540 case Ctrl_Q:
1541#ifdef FEAT_MOUSE
1542 ignore_drag_release = TRUE;
1543#endif
1544 putcmdline('^', TRUE);
1545 c = get_literal(); /* get next (two) character(s) */
1546 do_abbr = FALSE; /* don't do abbreviation now */
1547#ifdef FEAT_MBYTE
1548 /* may need to remove ^ when composing char was typed */
1549 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1550 {
1551 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1552 msg_putchar(' ');
1553 cursorcmd();
1554 }
1555#endif
1556 break;
1557
1558#ifdef FEAT_DIGRAPHS
1559 case Ctrl_K:
1560#ifdef FEAT_MOUSE
1561 ignore_drag_release = TRUE;
1562#endif
1563 putcmdline('?', TRUE);
1564#ifdef USE_ON_FLY_SCROLL
1565 dont_scroll = TRUE; /* disallow scrolling here */
1566#endif
1567 c = get_digraph(TRUE);
1568 if (c != NUL)
1569 break;
1570
1571 redrawcmd();
1572 goto cmdline_not_changed;
1573#endif /* FEAT_DIGRAPHS */
1574
1575#ifdef FEAT_RIGHTLEFT
1576 case Ctrl__: /* CTRL-_: switch language mode */
1577 if (!p_ari)
1578 break;
1579#ifdef FEAT_FKMAP
1580 if (p_altkeymap)
1581 {
1582 cmd_fkmap = !cmd_fkmap;
1583 if (cmd_fkmap) /* in Farsi always in Insert mode */
1584 ccline.overstrike = FALSE;
1585 }
1586 else /* Hebrew is default */
1587#endif
1588 cmd_hkmap = !cmd_hkmap;
1589 goto cmdline_not_changed;
1590#endif
1591
1592 default:
1593#ifdef UNIX
1594 if (c == intr_char)
1595 {
1596 gotesc = TRUE; /* will free ccline.cmdbuff after
1597 putting it in history */
1598 goto returncmd; /* back to Normal mode */
1599 }
1600#endif
1601 /*
1602 * Normal character with no special meaning. Just set mod_mask
1603 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1604 * the string <S-Space>. This should only happen after ^V.
1605 */
1606 if (!IS_SPECIAL(c))
1607 mod_mask = 0x0;
1608 break;
1609 }
1610 /*
1611 * End of switch on command line character.
1612 * We come here if we have a normal character.
1613 */
1614
1615 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1616#ifdef FEAT_MBYTE
1617 /* Add ABBR_OFF for characters above 0x100, this is
1618 * what check_abbr() expects. */
1619 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1620#endif
1621 c))
1622 goto cmdline_changed;
1623
1624 /*
1625 * put the character in the command line
1626 */
1627 if (IS_SPECIAL(c) || mod_mask != 0)
1628 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1629 else
1630 {
1631#ifdef FEAT_MBYTE
1632 if (has_mbyte)
1633 {
1634 j = (*mb_char2bytes)(c, IObuff);
1635 IObuff[j] = NUL; /* exclude composing chars */
1636 put_on_cmdline(IObuff, j, TRUE);
1637 }
1638 else
1639#endif
1640 {
1641 IObuff[0] = c;
1642 put_on_cmdline(IObuff, 1, TRUE);
1643 }
1644 }
1645 goto cmdline_changed;
1646
1647/*
1648 * This part implements incremental searches for "/" and "?"
1649 * Jump to cmdline_not_changed when a character has been read but the command
1650 * line did not change. Then we only search and redraw if something changed in
1651 * the past.
1652 * Jump to cmdline_changed when the command line did change.
1653 * (Sorry for the goto's, I know it is ugly).
1654 */
1655cmdline_not_changed:
1656#ifdef FEAT_SEARCH_EXTRA
1657 if (!incsearch_postponed)
1658 continue;
1659#endif
1660
1661cmdline_changed:
1662#ifdef FEAT_SEARCH_EXTRA
1663 /*
1664 * 'incsearch' highlighting.
1665 */
1666 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1667 {
1668 /* if there is a character waiting, search and redraw later */
1669 if (char_avail())
1670 {
1671 incsearch_postponed = TRUE;
1672 continue;
1673 }
1674 incsearch_postponed = FALSE;
1675 curwin->w_cursor = old_cursor; /* start at old position */
1676
1677 /* If there is no command line, don't do anything */
1678 if (ccline.cmdlen == 0)
1679 i = 0;
1680 else
1681 {
1682 cursor_off(); /* so the user knows we're busy */
1683 out_flush();
1684 ++emsg_off; /* So it doesn't beep if bad expr */
1685 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1686 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1687 --emsg_off;
1688 /* if interrupted while searching, behave like it failed */
1689 if (got_int)
1690 {
1691 (void)vpeekc(); /* remove <C-C> from input stream */
1692 got_int = FALSE; /* don't abandon the command line */
1693 i = 0;
1694 }
1695 else if (char_avail())
1696 /* cancelled searching because a char was typed */
1697 incsearch_postponed = TRUE;
1698 }
1699 if (i)
1700 highlight_match = TRUE; /* highlight position */
1701 else
1702 highlight_match = FALSE; /* remove highlight */
1703
1704 /* first restore the old curwin values, so the screen is
1705 * positioned in the same way as the actual search command */
1706 curwin->w_leftcol = old_leftcol;
1707 curwin->w_topline = old_topline;
1708# ifdef FEAT_DIFF
1709 curwin->w_topfill = old_topfill;
1710# endif
1711 curwin->w_botline = old_botline;
1712 changed_cline_bef_curs();
1713 update_topline();
1714
1715 if (i != 0)
1716 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001717 pos_T save_pos = curwin->w_cursor;
1718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 /*
1720 * First move cursor to end of match, then to start. This
1721 * moves the whole match onto the screen when 'nowrap' is set.
1722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 curwin->w_cursor.lnum += search_match_lines;
1724 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001725 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1726 {
1727 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1728 coladvance((colnr_T)MAXCOL);
1729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 validate_cursor();
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001731 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 }
1733 validate_cursor();
1734
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001735 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 update_screen(NOT_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001737 restore_cmdline(&save_ccline);
1738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 msg_starthere();
1740 redrawcmdline();
1741 did_incsearch = TRUE;
1742 }
1743#else /* FEAT_SEARCH_EXTRA */
1744 ;
1745#endif
1746
1747#ifdef FEAT_RIGHTLEFT
1748 if (cmdmsg_rl
1749# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001750 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751# endif
1752 )
1753 /* Always redraw the whole command line to fix shaping and
1754 * right-left typing. Not efficient, but it works. */
1755 redrawcmd();
1756#endif
1757 }
1758
1759returncmd:
1760
1761#ifdef FEAT_RIGHTLEFT
1762 cmdmsg_rl = FALSE;
1763#endif
1764
1765#ifdef FEAT_FKMAP
1766 cmd_fkmap = 0;
1767#endif
1768
1769 ExpandCleanup(&xpc);
1770
1771#ifdef FEAT_SEARCH_EXTRA
1772 if (did_incsearch)
1773 {
1774 curwin->w_cursor = old_cursor;
1775 curwin->w_curswant = old_curswant;
1776 curwin->w_leftcol = old_leftcol;
1777 curwin->w_topline = old_topline;
1778# ifdef FEAT_DIFF
1779 curwin->w_topfill = old_topfill;
1780# endif
1781 curwin->w_botline = old_botline;
1782 highlight_match = FALSE;
1783 validate_cursor(); /* needed for TAB */
1784 redraw_later(NOT_VALID);
1785 }
1786#endif
1787
1788 if (ccline.cmdbuff != NULL)
1789 {
1790 /*
1791 * Put line in history buffer (":" and "=" only when it was typed).
1792 */
1793#ifdef FEAT_CMDHIST
1794 if (ccline.cmdlen && firstc != NUL
1795 && (some_key_typed || histype == HIST_SEARCH))
1796 {
1797 add_to_history(histype, ccline.cmdbuff, TRUE,
1798 histype == HIST_SEARCH ? firstc : NUL);
1799 if (firstc == ':')
1800 {
1801 vim_free(new_last_cmdline);
1802 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1803 }
1804 }
1805#endif
1806
1807 if (gotesc) /* abandon command line */
1808 {
1809 vim_free(ccline.cmdbuff);
1810 ccline.cmdbuff = NULL;
1811 if (msg_scrolled == 0)
1812 compute_cmdrow();
1813 MSG("");
1814 redraw_cmdline = TRUE;
1815 }
1816 }
1817
1818 /*
1819 * If the screen was shifted up, redraw the whole screen (later).
1820 * If the line is too long, clear it, so ruler and shown command do
1821 * not get printed in the middle of it.
1822 */
1823 msg_check();
1824 msg_scroll = save_msg_scroll;
1825 redir_off = FALSE;
1826
1827 /* When the command line was typed, no need for a wait-return prompt. */
1828 if (some_key_typed)
1829 need_wait_return = FALSE;
1830
1831 State = save_State;
1832#ifdef USE_IM_CONTROL
1833 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1834 im_save_status(b_im_ptr);
1835 im_set_active(FALSE);
1836#endif
1837#ifdef FEAT_MOUSE
1838 setmouse();
1839#endif
1840#ifdef CURSOR_SHAPE
1841 ui_cursor_shape(); /* may show different cursor shape */
1842#endif
1843
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001844 {
1845 char_u *p = ccline.cmdbuff;
1846
1847 /* Make ccline empty, getcmdline() may try to use it. */
1848 ccline.cmdbuff = NULL;
1849 return p;
1850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851}
1852
1853#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1854/*
1855 * Get a command line with a prompt.
1856 * This is prepared to be called recursively from getcmdline() (e.g. by
1857 * f_input() when evaluating an expression from CTRL-R =).
1858 * Returns the command line in allocated memory, or NULL.
1859 */
1860 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001861getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 int firstc;
1863 char_u *prompt; /* command line prompt */
1864 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001865 int xp_context; /* type of expansion */
1866 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867{
1868 char_u *s;
1869 struct cmdline_info save_ccline;
1870 int msg_col_save = msg_col;
1871
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001872 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 ccline.cmdprompt = prompt;
1874 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001875# ifdef FEAT_EVAL
1876 ccline.xp_context = xp_context;
1877 ccline.xp_arg = xp_arg;
1878 ccline.input_fn = (firstc == '@');
1879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001881 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 /* Restore msg_col, the prompt from input() may have changed it. */
1883 msg_col = msg_col_save;
1884
1885 return s;
1886}
1887#endif
1888
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001889/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001890 * Return TRUE when the text must not be changed and we can't switch to
1891 * another window or buffer. Used when editing the command line, evaluating
1892 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001893 */
1894 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001895text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001896{
1897#ifdef FEAT_CMDWIN
1898 if (cmdwin_type != 0)
1899 return TRUE;
1900#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001901 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001902}
1903
1904/*
1905 * Give an error message for a command that isn't allowed while the cmdline
1906 * window is open or editing the cmdline in another way.
1907 */
1908 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001909text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001910{
1911#ifdef FEAT_CMDWIN
1912 if (cmdwin_type != 0)
1913 EMSG(_(e_cmdwin));
1914 else
1915#endif
1916 EMSG(_(e_secure));
1917}
1918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 static int
1920cmdline_charsize(idx)
1921 int idx;
1922{
1923#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1924 if (cmdline_star > 0) /* showing '*', always 1 position */
1925 return 1;
1926#endif
1927 return ptr2cells(ccline.cmdbuff + idx);
1928}
1929
1930/*
1931 * Compute the offset of the cursor on the command line for the prompt and
1932 * indent.
1933 */
1934 static void
1935set_cmdspos()
1936{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001937 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 ccline.cmdspos = 1 + ccline.cmdindent;
1939 else
1940 ccline.cmdspos = 0 + ccline.cmdindent;
1941}
1942
1943/*
1944 * Compute the screen position for the cursor on the command line.
1945 */
1946 static void
1947set_cmdspos_cursor()
1948{
1949 int i, m, c;
1950
1951 set_cmdspos();
1952 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001955 if (m < 0) /* overflow, Columns or Rows at weird value */
1956 m = MAXCOL;
1957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 else
1959 m = MAXCOL;
1960 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1961 {
1962 c = cmdline_charsize(i);
1963#ifdef FEAT_MBYTE
1964 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1965 if (has_mbyte)
1966 correct_cmdspos(i, c);
1967#endif
1968 /* If the cmdline doesn't fit, put cursor on last visible char. */
1969 if ((ccline.cmdspos += c) >= m)
1970 {
1971 ccline.cmdpos = i - 1;
1972 ccline.cmdspos -= c;
1973 break;
1974 }
1975#ifdef FEAT_MBYTE
1976 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001977 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978#endif
1979 }
1980}
1981
1982#ifdef FEAT_MBYTE
1983/*
1984 * Check if the character at "idx", which is "cells" wide, is a multi-byte
1985 * character that doesn't fit, so that a ">" must be displayed.
1986 */
1987 static void
1988correct_cmdspos(idx, cells)
1989 int idx;
1990 int cells;
1991{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001992 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
1994 && ccline.cmdspos % Columns + cells > Columns)
1995 ccline.cmdspos++;
1996}
1997#endif
1998
1999/*
2000 * Get an Ex command line for the ":" command.
2001 */
2002/* ARGSUSED */
2003 char_u *
2004getexline(c, dummy, indent)
2005 int c; /* normally ':', NUL for ":append" */
2006 void *dummy; /* cookie not used */
2007 int indent; /* indent for inside conditionals */
2008{
2009 /* When executing a register, remove ':' that's in front of each line. */
2010 if (exec_from_reg && vpeekc() == ':')
2011 (void)vgetc();
2012 return getcmdline(c, 1L, indent);
2013}
2014
2015/*
2016 * Get an Ex command line for Ex mode.
2017 * In Ex mode we only use the OS supplied line editing features and no
2018 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002019 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 */
2021/* ARGSUSED */
2022 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002023getexmodeline(promptc, dummy, indent)
2024 int promptc; /* normally ':', NUL for ":append" and '?' for
2025 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 void *dummy; /* cookie not used */
2027 int indent; /* indent for inside conditionals */
2028{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002029 garray_T line_ga;
2030 char_u *pend;
2031 int startcol = 0;
2032 int c1;
2033 int escaped = FALSE; /* CTRL-V typed */
2034 int vcol = 0;
2035 char_u *p;
2036 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038 /* Switch cursor on now. This avoids that it happens after the "\n", which
2039 * confuses the system function that computes tabstops. */
2040 cursor_on();
2041
2042 /* always start in column 0; write a newline if necessary */
2043 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002044 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002046 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002048 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002049 if (p_prompt)
2050 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 while (indent-- > 0)
2052 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055
2056 ga_init2(&line_ga, 1, 30);
2057
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002058 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002059 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002060 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002061 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002062 while (indent >= 8)
2063 {
2064 ga_append(&line_ga, TAB);
2065 msg_puts((char_u *)" ");
2066 indent -= 8;
2067 }
2068 while (indent-- > 0)
2069 {
2070 ga_append(&line_ga, ' ');
2071 msg_putchar(' ');
2072 }
2073 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002074 ++no_mapping;
2075 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002076
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 /*
2078 * Get the line, one character at a time.
2079 */
2080 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002081 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
2083 if (ga_grow(&line_ga, 40) == FAIL)
2084 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002085 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002087 /* Get one character at a time. Don't use inchar(), it can't handle
2088 * special characters. */
2089 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002092 * Handle line editing.
2093 * Previously this was left to the system, putting the terminal in
2094 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002096 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002098 msg_putchar('\n');
2099 break;
2100 }
2101
2102 if (!escaped)
2103 {
2104 /* CR typed means "enter", which is NL */
2105 if (c1 == '\r')
2106 c1 = '\n';
2107
2108 if (c1 == BS || c1 == K_BS
2109 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002111 if (line_ga.ga_len > 0)
2112 {
2113 --line_ga.ga_len;
2114 goto redraw;
2115 }
2116 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
2118
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002119 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002121 msg_col = startcol;
2122 msg_clr_eos();
2123 line_ga.ga_len = 0;
2124 continue;
2125 }
2126
2127 if (c1 == Ctrl_T)
2128 {
2129 p = (char_u *)line_ga.ga_data;
2130 p[line_ga.ga_len] = NUL;
2131 indent = get_indent_str(p, 8);
2132 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2133add_indent:
2134 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002136 char_u *s = skipwhite(p);
2137
2138 ga_grow(&line_ga, 1);
2139 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2140 *s = ' ';
2141 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002143redraw:
2144 /* redraw the line */
2145 msg_col = startcol;
2146 windgoto(msg_row, msg_col);
2147 vcol = 0;
2148 for (p = (char_u *)line_ga.ga_data;
2149 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002151 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002153 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002155 msg_putchar(' ');
2156 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002158 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002160 msg_outtrans_len(p, 1);
2161 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
2163 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002164 msg_clr_eos();
2165 continue;
2166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002168 if (c1 == Ctrl_D)
2169 {
2170 /* Delete one shiftwidth. */
2171 p = (char_u *)line_ga.ga_data;
2172 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002174 if (prev_char == '^')
2175 ex_keep_indent = TRUE;
2176 indent = 0;
2177 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 }
2179 else
2180 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002181 p[line_ga.ga_len] = NUL;
2182 indent = get_indent_str(p, 8);
2183 --indent;
2184 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002186 while (get_indent_str(p, 8) > indent)
2187 {
2188 char_u *s = skipwhite(p);
2189
2190 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2191 --line_ga.ga_len;
2192 }
2193 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002195
2196 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2197 {
2198 escaped = TRUE;
2199 continue;
2200 }
2201
2202 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2203 if (IS_SPECIAL(c1))
2204 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002206
2207 if (IS_SPECIAL(c1))
2208 c1 = '?';
2209 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2210 prev_char = c1;
2211 if (c1 == '\n')
2212 msg_putchar('\n');
2213 else if (c1 == TAB)
2214 {
2215 /* Don't use chartabsize(), 'ts' can be different */
2216 do
2217 {
2218 msg_putchar(' ');
2219 } while (++vcol % 8);
2220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002223 msg_outtrans_len(
2224 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2225 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002227 ++line_ga.ga_len;
2228 escaped = FALSE;
2229
2230 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002231 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232
2233 /* we are done when a NL is entered, but not when it comes after a
2234 * backslash */
2235 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2236 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002239 --pend;
2240 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243 }
2244
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002245 --no_mapping;
2246 --allow_keys;
2247
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 /* make following messages go to the next line */
2249 msg_didout = FALSE;
2250 msg_col = 0;
2251 if (msg_row < Rows - 1)
2252 ++msg_row;
2253 emsg_on_display = FALSE; /* don't want ui_delay() */
2254
2255 if (got_int)
2256 ga_clear(&line_ga);
2257
2258 return (char_u *)line_ga.ga_data;
2259}
2260
Bram Moolenaare344bea2005-09-01 20:46:49 +00002261# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2262 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263/*
2264 * Return TRUE if ccline.overstrike is on.
2265 */
2266 int
2267cmdline_overstrike()
2268{
2269 return ccline.overstrike;
2270}
2271
2272/*
2273 * Return TRUE if the cursor is at the end of the cmdline.
2274 */
2275 int
2276cmdline_at_end()
2277{
2278 return (ccline.cmdpos >= ccline.cmdlen);
2279}
2280#endif
2281
Bram Moolenaar9372a112005-12-06 19:59:18 +00002282#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283/*
2284 * Return the virtual column number at the current cursor position.
2285 * This is used by the IM code to obtain the start of the preedit string.
2286 */
2287 colnr_T
2288cmdline_getvcol_cursor()
2289{
2290 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2291 return MAXCOL;
2292
2293# ifdef FEAT_MBYTE
2294 if (has_mbyte)
2295 {
2296 colnr_T col;
2297 int i = 0;
2298
2299 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002300 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
2302 return col;
2303 }
2304 else
2305# endif
2306 return ccline.cmdpos;
2307}
2308#endif
2309
2310#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2311/*
2312 * If part of the command line is an IM preedit string, redraw it with
2313 * IM feedback attributes. The cursor position is restored after drawing.
2314 */
2315 static void
2316redrawcmd_preedit()
2317{
2318 if ((State & CMDLINE)
2319 && xic != NULL
2320 && im_get_status()
2321 && !p_imdisable
2322 && im_is_preediting())
2323 {
2324 int cmdpos = 0;
2325 int cmdspos;
2326 int old_row;
2327 int old_col;
2328 colnr_T col;
2329
2330 old_row = msg_row;
2331 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002332 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
2334# ifdef FEAT_MBYTE
2335 if (has_mbyte)
2336 {
2337 for (col = 0; col < preedit_start_col
2338 && cmdpos < ccline.cmdlen; ++col)
2339 {
2340 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002341 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
2343 }
2344 else
2345# endif
2346 {
2347 cmdspos += preedit_start_col;
2348 cmdpos += preedit_start_col;
2349 }
2350
2351 msg_row = cmdline_row + (cmdspos / (int)Columns);
2352 msg_col = cmdspos % (int)Columns;
2353 if (msg_row >= Rows)
2354 msg_row = Rows - 1;
2355
2356 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2357 {
2358 int char_len;
2359 int char_attr;
2360
2361 char_attr = im_get_feedback_attr(col);
2362 if (char_attr < 0)
2363 break; /* end of preedit string */
2364
2365# ifdef FEAT_MBYTE
2366 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002367 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 else
2369# endif
2370 char_len = 1;
2371
2372 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2373 cmdpos += char_len;
2374 }
2375
2376 msg_row = old_row;
2377 msg_col = old_col;
2378 }
2379}
2380#endif /* FEAT_XIM && FEAT_GUI_GTK */
2381
2382/*
2383 * Allocate a new command line buffer.
2384 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2385 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2386 */
2387 static void
2388alloc_cmdbuff(len)
2389 int len;
2390{
2391 /*
2392 * give some extra space to avoid having to allocate all the time
2393 */
2394 if (len < 80)
2395 len = 100;
2396 else
2397 len += 20;
2398
2399 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2400 ccline.cmdbufflen = len;
2401}
2402
2403/*
2404 * Re-allocate the command line to length len + something extra.
2405 * return FAIL for failure, OK otherwise
2406 */
2407 static int
2408realloc_cmdbuff(len)
2409 int len;
2410{
2411 char_u *p;
2412
2413 p = ccline.cmdbuff;
2414 alloc_cmdbuff(len); /* will get some more */
2415 if (ccline.cmdbuff == NULL) /* out of memory */
2416 {
2417 ccline.cmdbuff = p; /* keep the old one */
2418 return FAIL;
2419 }
2420 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2421 vim_free(p);
2422 return OK;
2423}
2424
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002425#if defined(FEAT_ARABIC) || defined(PROTO)
2426static char_u *arshape_buf = NULL;
2427
2428# if defined(EXITFREE) || defined(PROTO)
2429 void
2430free_cmdline_buf()
2431{
2432 vim_free(arshape_buf);
2433}
2434# endif
2435#endif
2436
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437/*
2438 * Draw part of the cmdline at the current cursor position. But draw stars
2439 * when cmdline_star is TRUE.
2440 */
2441 static void
2442draw_cmdline(start, len)
2443 int start;
2444 int len;
2445{
2446#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2447 int i;
2448
2449 if (cmdline_star > 0)
2450 for (i = 0; i < len; ++i)
2451 {
2452 msg_putchar('*');
2453# ifdef FEAT_MBYTE
2454 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002455 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456# endif
2457 }
2458 else
2459#endif
2460#ifdef FEAT_ARABIC
2461 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2462 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 static int buflen = 0;
2464 char_u *p;
2465 int j;
2466 int newlen = 0;
2467 int mb_l;
2468 int pc, pc1;
2469 int prev_c = 0;
2470 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002471 int u8c;
2472 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 /*
2476 * Do arabic shaping into a temporary buffer. This is very
2477 * inefficient!
2478 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002479 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
2481 /* Re-allocate the buffer. We keep it around to avoid a lot of
2482 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002483 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002484 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002485 arshape_buf = alloc(buflen);
2486 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 return; /* out of memory */
2488 }
2489
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002490 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2491 {
2492 /* Prepend a space to draw the leading composing char on. */
2493 arshape_buf[0] = ' ';
2494 newlen = 1;
2495 }
2496
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 for (j = start; j < start + len; j += mb_l)
2498 {
2499 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002500 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002501 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 if (ARABIC_CHAR(u8c))
2503 {
2504 /* Do Arabic shaping. */
2505 if (cmdmsg_rl)
2506 {
2507 /* displaying from right to left */
2508 pc = prev_c;
2509 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002510 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 if (j + mb_l >= start + len)
2512 nc = NUL;
2513 else
2514 nc = utf_ptr2char(p + mb_l);
2515 }
2516 else
2517 {
2518 /* displaying from left to right */
2519 if (j + mb_l >= start + len)
2520 pc = NUL;
2521 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002522 {
2523 int pcc[MAX_MCO];
2524
2525 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002527 pc1 = pcc[0];
2528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 nc = prev_c;
2530 }
2531 prev_c = u8c;
2532
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002533 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002535 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002536 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002538 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2539 if (u8cc[1] != 0)
2540 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002541 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
2543 }
2544 else
2545 {
2546 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002547 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 newlen += mb_l;
2549 }
2550 }
2551
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002552 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554 else
2555#endif
2556 msg_outtrans_len(ccline.cmdbuff + start, len);
2557}
2558
2559/*
2560 * Put a character on the command line. Shifts the following text to the
2561 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2562 * "c" must be printable (fit in one display cell)!
2563 */
2564 void
2565putcmdline(c, shift)
2566 int c;
2567 int shift;
2568{
2569 if (cmd_silent)
2570 return;
2571 msg_no_more = TRUE;
2572 msg_putchar(c);
2573 if (shift)
2574 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2575 msg_no_more = FALSE;
2576 cursorcmd();
2577}
2578
2579/*
2580 * Undo a putcmdline(c, FALSE).
2581 */
2582 void
2583unputcmdline()
2584{
2585 if (cmd_silent)
2586 return;
2587 msg_no_more = TRUE;
2588 if (ccline.cmdlen == ccline.cmdpos)
2589 msg_putchar(' ');
2590 else
2591 draw_cmdline(ccline.cmdpos, 1);
2592 msg_no_more = FALSE;
2593 cursorcmd();
2594}
2595
2596/*
2597 * Put the given string, of the given length, onto the command line.
2598 * If len is -1, then STRLEN() is used to calculate the length.
2599 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2600 * part will be redrawn, otherwise it will not. If this function is called
2601 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2602 * called afterwards.
2603 */
2604 int
2605put_on_cmdline(str, len, redraw)
2606 char_u *str;
2607 int len;
2608 int redraw;
2609{
2610 int retval;
2611 int i;
2612 int m;
2613 int c;
2614
2615 if (len < 0)
2616 len = (int)STRLEN(str);
2617
2618 /* Check if ccline.cmdbuff needs to be longer */
2619 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2620 retval = realloc_cmdbuff(ccline.cmdlen + len);
2621 else
2622 retval = OK;
2623 if (retval == OK)
2624 {
2625 if (!ccline.overstrike)
2626 {
2627 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2628 ccline.cmdbuff + ccline.cmdpos,
2629 (size_t)(ccline.cmdlen - ccline.cmdpos));
2630 ccline.cmdlen += len;
2631 }
2632 else
2633 {
2634#ifdef FEAT_MBYTE
2635 if (has_mbyte)
2636 {
2637 /* Count nr of characters in the new string. */
2638 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002639 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 ++m;
2641 /* Count nr of bytes in cmdline that are overwritten by these
2642 * characters. */
2643 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002644 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 --m;
2646 if (i < ccline.cmdlen)
2647 {
2648 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2649 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2650 ccline.cmdlen += ccline.cmdpos + len - i;
2651 }
2652 else
2653 ccline.cmdlen = ccline.cmdpos + len;
2654 }
2655 else
2656#endif
2657 if (ccline.cmdpos + len > ccline.cmdlen)
2658 ccline.cmdlen = ccline.cmdpos + len;
2659 }
2660 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2661 ccline.cmdbuff[ccline.cmdlen] = NUL;
2662
2663#ifdef FEAT_MBYTE
2664 if (enc_utf8)
2665 {
2666 /* When the inserted text starts with a composing character,
2667 * backup to the character before it. There could be two of them.
2668 */
2669 i = 0;
2670 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2671 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2672 {
2673 i = (*mb_head_off)(ccline.cmdbuff,
2674 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2675 ccline.cmdpos -= i;
2676 len += i;
2677 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2678 }
2679# ifdef FEAT_ARABIC
2680 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2681 {
2682 /* Check the previous character for Arabic combining pair. */
2683 i = (*mb_head_off)(ccline.cmdbuff,
2684 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2685 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2686 + ccline.cmdpos - i), c))
2687 {
2688 ccline.cmdpos -= i;
2689 len += i;
2690 }
2691 else
2692 i = 0;
2693 }
2694# endif
2695 if (i != 0)
2696 {
2697 /* Also backup the cursor position. */
2698 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2699 ccline.cmdspos -= i;
2700 msg_col -= i;
2701 if (msg_col < 0)
2702 {
2703 msg_col += Columns;
2704 --msg_row;
2705 }
2706 }
2707 }
2708#endif
2709
2710 if (redraw && !cmd_silent)
2711 {
2712 msg_no_more = TRUE;
2713 i = cmdline_row;
2714 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2715 /* Avoid clearing the rest of the line too often. */
2716 if (cmdline_row != i || ccline.overstrike)
2717 msg_clr_eos();
2718 msg_no_more = FALSE;
2719 }
2720#ifdef FEAT_FKMAP
2721 /*
2722 * If we are in Farsi command mode, the character input must be in
2723 * Insert mode. So do not advance the cmdpos.
2724 */
2725 if (!cmd_fkmap)
2726#endif
2727 {
2728 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002731 if (m < 0) /* overflow, Columns or Rows at weird value */
2732 m = MAXCOL;
2733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 else
2735 m = MAXCOL;
2736 for (i = 0; i < len; ++i)
2737 {
2738 c = cmdline_charsize(ccline.cmdpos);
2739#ifdef FEAT_MBYTE
2740 /* count ">" for a double-wide char that doesn't fit. */
2741 if (has_mbyte)
2742 correct_cmdspos(ccline.cmdpos, c);
2743#endif
2744 /* Stop cursor at the end of the screen */
2745 if (ccline.cmdspos + c >= m)
2746 break;
2747 ccline.cmdspos += c;
2748#ifdef FEAT_MBYTE
2749 if (has_mbyte)
2750 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002751 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 if (c > len - i - 1)
2753 c = len - i - 1;
2754 ccline.cmdpos += c;
2755 i += c;
2756 }
2757#endif
2758 ++ccline.cmdpos;
2759 }
2760 }
2761 }
2762 if (redraw)
2763 msg_check();
2764 return retval;
2765}
2766
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002767static struct cmdline_info prev_ccline;
2768static int prev_ccline_used = FALSE;
2769
2770/*
2771 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2772 * and overwrite it. But get_cmdline_str() may need it, thus make it
2773 * available globally in prev_ccline.
2774 */
2775 static void
2776save_cmdline(ccp)
2777 struct cmdline_info *ccp;
2778{
2779 if (!prev_ccline_used)
2780 {
2781 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2782 prev_ccline_used = TRUE;
2783 }
2784 *ccp = prev_ccline;
2785 prev_ccline = ccline;
2786 ccline.cmdbuff = NULL;
2787 ccline.cmdprompt = NULL;
2788}
2789
2790/*
2791 * Resture ccline after it has been saved with save_cmdline().
2792 */
2793 static void
2794restore_cmdline(ccp)
2795 struct cmdline_info *ccp;
2796{
2797 ccline = prev_ccline;
2798 prev_ccline = *ccp;
2799}
2800
Bram Moolenaar8299df92004-07-10 09:47:34 +00002801/*
2802 * paste a yank register into the command line.
2803 * used by CTRL-R command in command-line mode
2804 * insert_reg() can't be used here, because special characters from the
2805 * register contents will be interpreted as commands.
2806 *
2807 * return FAIL for failure, OK otherwise
2808 */
2809 static int
2810cmdline_paste(regname, literally)
2811 int regname;
2812 int literally; /* Insert text literally instead of "as typed" */
2813{
2814 long i;
2815 char_u *arg;
2816 int allocated;
2817 struct cmdline_info save_ccline;
2818
2819 /* check for valid regname; also accept special characters for CTRL-R in
2820 * the command line */
2821 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2822 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2823 return FAIL;
2824
2825 /* A register containing CTRL-R can cause an endless loop. Allow using
2826 * CTRL-C to break the loop. */
2827 line_breakcheck();
2828 if (got_int)
2829 return FAIL;
2830
2831#ifdef FEAT_CLIPBOARD
2832 regname = may_get_selection(regname);
2833#endif
2834
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002835 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002836 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002837 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002838 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002839 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002840 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002841 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002842
2843 if (i)
2844 {
2845 /* Got the value of a special register in "arg". */
2846 if (arg == NULL)
2847 return FAIL;
2848 cmdline_paste_str(arg, literally);
2849 if (allocated)
2850 vim_free(arg);
2851 return OK;
2852 }
2853
2854 return cmdline_paste_reg(regname, literally);
2855}
2856
2857/*
2858 * Put a string on the command line.
2859 * When "literally" is TRUE, insert literally.
2860 * When "literally" is FALSE, insert as typed, but don't leave the command
2861 * line.
2862 */
2863 void
2864cmdline_paste_str(s, literally)
2865 char_u *s;
2866 int literally;
2867{
2868 int c, cv;
2869
2870 if (literally)
2871 put_on_cmdline(s, -1, TRUE);
2872 else
2873 while (*s != NUL)
2874 {
2875 cv = *s;
2876 if (cv == Ctrl_V && s[1])
2877 ++s;
2878#ifdef FEAT_MBYTE
2879 if (has_mbyte)
2880 {
2881 c = mb_ptr2char(s);
2882 s += mb_char2len(c);
2883 }
2884 else
2885#endif
2886 c = *s++;
2887 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2888#ifdef UNIX
2889 || c == intr_char
2890#endif
2891 || (c == Ctrl_BSL && *s == Ctrl_N))
2892 stuffcharReadbuff(Ctrl_V);
2893 stuffcharReadbuff(c);
2894 }
2895}
2896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897#ifdef FEAT_WILDMENU
2898/*
2899 * Delete characters on the command line, from "from" to the current
2900 * position.
2901 */
2902 static void
2903cmdline_del(from)
2904 int from;
2905{
2906 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2907 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2908 ccline.cmdlen -= ccline.cmdpos - from;
2909 ccline.cmdpos = from;
2910}
2911#endif
2912
2913/*
2914 * this fuction is called when the screen size changes and with incremental
2915 * search
2916 */
2917 void
2918redrawcmdline()
2919{
2920 if (cmd_silent)
2921 return;
2922 need_wait_return = FALSE;
2923 compute_cmdrow();
2924 redrawcmd();
2925 cursorcmd();
2926}
2927
2928 static void
2929redrawcmdprompt()
2930{
2931 int i;
2932
2933 if (cmd_silent)
2934 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002935 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 msg_putchar(ccline.cmdfirstc);
2937 if (ccline.cmdprompt != NULL)
2938 {
2939 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2940 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2941 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002942 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 --ccline.cmdindent;
2944 }
2945 else
2946 for (i = ccline.cmdindent; i > 0; --i)
2947 msg_putchar(' ');
2948}
2949
2950/*
2951 * Redraw what is currently on the command line.
2952 */
2953 void
2954redrawcmd()
2955{
2956 if (cmd_silent)
2957 return;
2958
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00002959 /* when 'incsearch' is set there may be no command line while redrawing */
2960 if (ccline.cmdbuff == NULL)
2961 {
2962 windgoto(cmdline_row, 0);
2963 msg_clr_eos();
2964 return;
2965 }
2966
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 msg_start();
2968 redrawcmdprompt();
2969
2970 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2971 msg_no_more = TRUE;
2972 draw_cmdline(0, ccline.cmdlen);
2973 msg_clr_eos();
2974 msg_no_more = FALSE;
2975
2976 set_cmdspos_cursor();
2977
2978 /*
2979 * An emsg() before may have set msg_scroll. This is used in normal mode,
2980 * in cmdline mode we can reset them now.
2981 */
2982 msg_scroll = FALSE; /* next message overwrites cmdline */
2983
2984 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2985 * in cmdline mode */
2986 skip_redraw = FALSE;
2987}
2988
2989 void
2990compute_cmdrow()
2991{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002992 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 cmdline_row = Rows - 1;
2994 else
2995 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2996 + W_STATUS_HEIGHT(lastwin);
2997}
2998
2999 static void
3000cursorcmd()
3001{
3002 if (cmd_silent)
3003 return;
3004
3005#ifdef FEAT_RIGHTLEFT
3006 if (cmdmsg_rl)
3007 {
3008 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3009 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3010 if (msg_row <= 0)
3011 msg_row = Rows - 1;
3012 }
3013 else
3014#endif
3015 {
3016 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3017 msg_col = ccline.cmdspos % (int)Columns;
3018 if (msg_row >= Rows)
3019 msg_row = Rows - 1;
3020 }
3021
3022 windgoto(msg_row, msg_col);
3023#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3024 redrawcmd_preedit();
3025#endif
3026#ifdef MCH_CURSOR_SHAPE
3027 mch_update_cursor();
3028#endif
3029}
3030
3031 void
3032gotocmdline(clr)
3033 int clr;
3034{
3035 msg_start();
3036#ifdef FEAT_RIGHTLEFT
3037 if (cmdmsg_rl)
3038 msg_col = Columns - 1;
3039 else
3040#endif
3041 msg_col = 0; /* always start in column 0 */
3042 if (clr) /* clear the bottom line(s) */
3043 msg_clr_eos(); /* will reset clear_cmdline */
3044 windgoto(cmdline_row, 0);
3045}
3046
3047/*
3048 * Check the word in front of the cursor for an abbreviation.
3049 * Called when the non-id character "c" has been entered.
3050 * When an abbreviation is recognized it is removed from the text with
3051 * backspaces and the replacement string is inserted, followed by "c".
3052 */
3053 static int
3054ccheck_abbr(c)
3055 int c;
3056{
3057 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3058 return FALSE;
3059
3060 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3061}
3062
3063/*
3064 * Return FAIL if this is not an appropriate context in which to do
3065 * completion of anything, return OK if it is (even if there are no matches).
3066 * For the caller, this means that the character is just passed through like a
3067 * normal character (instead of being expanded). This allows :s/^I^D etc.
3068 */
3069 static int
3070nextwild(xp, type, options)
3071 expand_T *xp;
3072 int type;
3073 int options; /* extra options for ExpandOne() */
3074{
3075 int i, j;
3076 char_u *p1;
3077 char_u *p2;
3078 int oldlen;
3079 int difflen;
3080 int v;
3081
3082 if (xp->xp_numfiles == -1)
3083 {
3084 set_expand_context(xp);
3085 cmd_showtail = expand_showtail(xp);
3086 }
3087
3088 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3089 {
3090 beep_flush();
3091 return OK; /* Something illegal on command line */
3092 }
3093 if (xp->xp_context == EXPAND_NOTHING)
3094 {
3095 /* Caller can use the character as a normal char instead */
3096 return FAIL;
3097 }
3098
3099 MSG_PUTS("..."); /* show that we are busy */
3100 out_flush();
3101
3102 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3103 oldlen = ccline.cmdpos - i;
3104
3105 if (type == WILD_NEXT || type == WILD_PREV)
3106 {
3107 /*
3108 * Get next/previous match for a previous expanded pattern.
3109 */
3110 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3111 }
3112 else
3113 {
3114 /*
3115 * Translate string into pattern and expand it.
3116 */
3117 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3118 p2 = NULL;
3119 else
3120 {
3121 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3122 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3123 |options, type);
3124 vim_free(p1);
3125 /* longest match: make sure it is not shorter (happens with :help */
3126 if (p2 != NULL && type == WILD_LONGEST)
3127 {
3128 for (j = 0; j < oldlen; ++j)
3129 if (ccline.cmdbuff[i + j] == '*'
3130 || ccline.cmdbuff[i + j] == '?')
3131 break;
3132 if ((int)STRLEN(p2) < j)
3133 {
3134 vim_free(p2);
3135 p2 = NULL;
3136 }
3137 }
3138 }
3139 }
3140
3141 if (p2 != NULL && !got_int)
3142 {
3143 difflen = (int)STRLEN(p2) - oldlen;
3144 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3145 {
3146 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3147 xp->xp_pattern = ccline.cmdbuff + i;
3148 }
3149 else
3150 v = OK;
3151 if (v == OK)
3152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003153 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3154 &ccline.cmdbuff[ccline.cmdpos],
3155 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3156 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 ccline.cmdlen += difflen;
3158 ccline.cmdpos += difflen;
3159 }
3160 }
3161 vim_free(p2);
3162
3163 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003164 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
3166 /* When expanding a ":map" command and no matches are found, assume that
3167 * the key is supposed to be inserted literally */
3168 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3169 return FAIL;
3170
3171 if (xp->xp_numfiles <= 0 && p2 == NULL)
3172 beep_flush();
3173 else if (xp->xp_numfiles == 1)
3174 /* free expanded pattern */
3175 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3176
3177 return OK;
3178}
3179
3180/*
3181 * Do wildcard expansion on the string 'str'.
3182 * Chars that should not be expanded must be preceded with a backslash.
3183 * Return a pointer to alloced memory containing the new string.
3184 * Return NULL for failure.
3185 *
3186 * Results are cached in xp->xp_files and xp->xp_numfiles.
3187 *
3188 * mode = WILD_FREE: just free previously expanded matches
3189 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3190 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3191 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3192 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3193 * mode = WILD_ALL: return all matches concatenated
3194 * mode = WILD_LONGEST: return longest matched part
3195 *
3196 * options = WILD_LIST_NOTFOUND: list entries without a match
3197 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3198 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3199 * options = WILD_NO_BEEP: Don't beep for multiple matches
3200 * options = WILD_ADD_SLASH: add a slash after directory names
3201 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3202 * options = WILD_SILENT: don't print warning messages
3203 * options = WILD_ESCAPE: put backslash before special chars
3204 *
3205 * The variables xp->xp_context and xp->xp_backslash must have been set!
3206 */
3207 char_u *
3208ExpandOne(xp, str, orig, options, mode)
3209 expand_T *xp;
3210 char_u *str;
3211 char_u *orig; /* allocated copy of original of expanded string */
3212 int options;
3213 int mode;
3214{
3215 char_u *ss = NULL;
3216 static int findex;
3217 static char_u *orig_save = NULL; /* kept value of orig */
3218 int i;
3219 long_u len;
3220 int non_suf_match; /* number without matching suffix */
3221
3222 /*
3223 * first handle the case of using an old match
3224 */
3225 if (mode == WILD_NEXT || mode == WILD_PREV)
3226 {
3227 if (xp->xp_numfiles > 0)
3228 {
3229 if (mode == WILD_PREV)
3230 {
3231 if (findex == -1)
3232 findex = xp->xp_numfiles;
3233 --findex;
3234 }
3235 else /* mode == WILD_NEXT */
3236 ++findex;
3237
3238 /*
3239 * When wrapping around, return the original string, set findex to
3240 * -1.
3241 */
3242 if (findex < 0)
3243 {
3244 if (orig_save == NULL)
3245 findex = xp->xp_numfiles - 1;
3246 else
3247 findex = -1;
3248 }
3249 if (findex >= xp->xp_numfiles)
3250 {
3251 if (orig_save == NULL)
3252 findex = 0;
3253 else
3254 findex = -1;
3255 }
3256#ifdef FEAT_WILDMENU
3257 if (p_wmnu)
3258 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3259 findex, cmd_showtail);
3260#endif
3261 if (findex == -1)
3262 return vim_strsave(orig_save);
3263 return vim_strsave(xp->xp_files[findex]);
3264 }
3265 else
3266 return NULL;
3267 }
3268
3269/* free old names */
3270 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3271 {
3272 FreeWild(xp->xp_numfiles, xp->xp_files);
3273 xp->xp_numfiles = -1;
3274 vim_free(orig_save);
3275 orig_save = NULL;
3276 }
3277 findex = 0;
3278
3279 if (mode == WILD_FREE) /* only release file name */
3280 return NULL;
3281
3282 if (xp->xp_numfiles == -1)
3283 {
3284 vim_free(orig_save);
3285 orig_save = orig;
3286
3287 /*
3288 * Do the expansion.
3289 */
3290 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3291 options) == FAIL)
3292 {
3293#ifdef FNAME_ILLEGAL
3294 /* Illegal file name has been silently skipped. But when there
3295 * are wildcards, the real problem is that there was no match,
3296 * causing the pattern to be added, which has illegal characters.
3297 */
3298 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3299 EMSG2(_(e_nomatch2), str);
3300#endif
3301 }
3302 else if (xp->xp_numfiles == 0)
3303 {
3304 if (!(options & WILD_SILENT))
3305 EMSG2(_(e_nomatch2), str);
3306 }
3307 else
3308 {
3309 /* Escape the matches for use on the command line. */
3310 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3311
3312 /*
3313 * Check for matching suffixes in file names.
3314 */
3315 if (mode != WILD_ALL && mode != WILD_LONGEST)
3316 {
3317 if (xp->xp_numfiles)
3318 non_suf_match = xp->xp_numfiles;
3319 else
3320 non_suf_match = 1;
3321 if ((xp->xp_context == EXPAND_FILES
3322 || xp->xp_context == EXPAND_DIRECTORIES)
3323 && xp->xp_numfiles > 1)
3324 {
3325 /*
3326 * More than one match; check suffix.
3327 * The files will have been sorted on matching suffix in
3328 * expand_wildcards, only need to check the first two.
3329 */
3330 non_suf_match = 0;
3331 for (i = 0; i < 2; ++i)
3332 if (match_suffix(xp->xp_files[i]))
3333 ++non_suf_match;
3334 }
3335 if (non_suf_match != 1)
3336 {
3337 /* Can we ever get here unless it's while expanding
3338 * interactively? If not, we can get rid of this all
3339 * together. Don't really want to wait for this message
3340 * (and possibly have to hit return to continue!).
3341 */
3342 if (!(options & WILD_SILENT))
3343 EMSG(_(e_toomany));
3344 else if (!(options & WILD_NO_BEEP))
3345 beep_flush();
3346 }
3347 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3348 ss = vim_strsave(xp->xp_files[0]);
3349 }
3350 }
3351 }
3352
3353 /* Find longest common part */
3354 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3355 {
3356 for (len = 0; xp->xp_files[0][len]; ++len)
3357 {
3358 for (i = 0; i < xp->xp_numfiles; ++i)
3359 {
3360#ifdef CASE_INSENSITIVE_FILENAME
3361 if (xp->xp_context == EXPAND_DIRECTORIES
3362 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003363 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 || xp->xp_context == EXPAND_BUFFERS)
3365 {
3366 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3367 TOLOWER_LOC(xp->xp_files[0][len]))
3368 break;
3369 }
3370 else
3371#endif
3372 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3373 break;
3374 }
3375 if (i < xp->xp_numfiles)
3376 {
3377 if (!(options & WILD_NO_BEEP))
3378 vim_beep();
3379 break;
3380 }
3381 }
3382 ss = alloc((unsigned)len + 1);
3383 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003384 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 findex = -1; /* next p_wc gets first one */
3386 }
3387
3388 /* Concatenate all matching names */
3389 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3390 {
3391 len = 0;
3392 for (i = 0; i < xp->xp_numfiles; ++i)
3393 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3394 ss = lalloc(len, TRUE);
3395 if (ss != NULL)
3396 {
3397 *ss = NUL;
3398 for (i = 0; i < xp->xp_numfiles; ++i)
3399 {
3400 STRCAT(ss, xp->xp_files[i]);
3401 if (i != xp->xp_numfiles - 1)
3402 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3403 }
3404 }
3405 }
3406
3407 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3408 ExpandCleanup(xp);
3409
3410 return ss;
3411}
3412
3413/*
3414 * Prepare an expand structure for use.
3415 */
3416 void
3417ExpandInit(xp)
3418 expand_T *xp;
3419{
3420 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003421#ifndef BACKSLASH_IN_FILENAME
3422 xp->xp_shell = FALSE;
3423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 xp->xp_numfiles = -1;
3425 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003426#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3427 xp->xp_arg = NULL;
3428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429}
3430
3431/*
3432 * Cleanup an expand structure after use.
3433 */
3434 void
3435ExpandCleanup(xp)
3436 expand_T *xp;
3437{
3438 if (xp->xp_numfiles >= 0)
3439 {
3440 FreeWild(xp->xp_numfiles, xp->xp_files);
3441 xp->xp_numfiles = -1;
3442 }
3443}
3444
3445 void
3446ExpandEscape(xp, str, numfiles, files, options)
3447 expand_T *xp;
3448 char_u *str;
3449 int numfiles;
3450 char_u **files;
3451 int options;
3452{
3453 int i;
3454 char_u *p;
3455
3456 /*
3457 * May change home directory back to "~"
3458 */
3459 if (options & WILD_HOME_REPLACE)
3460 tilde_replace(str, numfiles, files);
3461
3462 if (options & WILD_ESCAPE)
3463 {
3464 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003465 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 || xp->xp_context == EXPAND_BUFFERS
3467 || xp->xp_context == EXPAND_DIRECTORIES)
3468 {
3469 /*
3470 * Insert a backslash into a file name before a space, \, %, #
3471 * and wildmatch characters, except '~'.
3472 */
3473 for (i = 0; i < numfiles; ++i)
3474 {
3475 /* for ":set path=" we need to escape spaces twice */
3476 if (xp->xp_backslash == XP_BS_THREE)
3477 {
3478 p = vim_strsave_escaped(files[i], (char_u *)" ");
3479 if (p != NULL)
3480 {
3481 vim_free(files[i]);
3482 files[i] = p;
3483#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3484 p = vim_strsave_escaped(files[i], (char_u *)" ");
3485 if (p != NULL)
3486 {
3487 vim_free(files[i]);
3488 files[i] = p;
3489 }
3490#endif
3491 }
3492 }
3493#ifdef BACKSLASH_IN_FILENAME
3494 {
3495 char_u buf[20];
3496 int j = 0;
3497
3498 /* Don't escape '[' and '{' if they are in 'isfname'. */
3499 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3500 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3501 buf[j++] = *p;
3502 buf[j] = NUL;
3503 p = vim_strsave_escaped(files[i], buf);
3504 }
3505#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003506 p = vim_strsave_escaped(files[i],
3507 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508#endif
3509 if (p != NULL)
3510 {
3511 vim_free(files[i]);
3512 files[i] = p;
3513 }
3514
3515 /* If 'str' starts with "\~", replace "~" at start of
3516 * files[i] with "\~". */
3517 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003518 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 }
3520 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003521
3522 /* If the first file starts with a '+' escape it. Otherwise it
3523 * could be seen as "+cmd". */
3524 if (*files[0] == '+')
3525 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527 else if (xp->xp_context == EXPAND_TAGS)
3528 {
3529 /*
3530 * Insert a backslash before characters in a tag name that
3531 * would terminate the ":tag" command.
3532 */
3533 for (i = 0; i < numfiles; ++i)
3534 {
3535 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3536 if (p != NULL)
3537 {
3538 vim_free(files[i]);
3539 files[i] = p;
3540 }
3541 }
3542 }
3543 }
3544}
3545
3546/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003547 * Put a backslash before the file name in "pp", which is in allocated memory.
3548 */
3549 static void
3550escape_fname(pp)
3551 char_u **pp;
3552{
3553 char_u *p;
3554
3555 p = alloc((unsigned)(STRLEN(*pp) + 2));
3556 if (p != NULL)
3557 {
3558 p[0] = '\\';
3559 STRCPY(p + 1, *pp);
3560 vim_free(*pp);
3561 *pp = p;
3562 }
3563}
3564
3565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 * For each file name in files[num_files]:
3567 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3568 */
3569 void
3570tilde_replace(orig_pat, num_files, files)
3571 char_u *orig_pat;
3572 int num_files;
3573 char_u **files;
3574{
3575 int i;
3576 char_u *p;
3577
3578 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3579 {
3580 for (i = 0; i < num_files; ++i)
3581 {
3582 p = home_replace_save(NULL, files[i]);
3583 if (p != NULL)
3584 {
3585 vim_free(files[i]);
3586 files[i] = p;
3587 }
3588 }
3589 }
3590}
3591
3592/*
3593 * Show all matches for completion on the command line.
3594 * Returns EXPAND_NOTHING when the character that triggered expansion should
3595 * be inserted like a normal character.
3596 */
3597/*ARGSUSED*/
3598 static int
3599showmatches(xp, wildmenu)
3600 expand_T *xp;
3601 int wildmenu;
3602{
3603#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3604 int num_files;
3605 char_u **files_found;
3606 int i, j, k;
3607 int maxlen;
3608 int lines;
3609 int columns;
3610 char_u *p;
3611 int lastlen;
3612 int attr;
3613 int showtail;
3614
3615 if (xp->xp_numfiles == -1)
3616 {
3617 set_expand_context(xp);
3618 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3619 &num_files, &files_found);
3620 showtail = expand_showtail(xp);
3621 if (i != EXPAND_OK)
3622 return i;
3623
3624 }
3625 else
3626 {
3627 num_files = xp->xp_numfiles;
3628 files_found = xp->xp_files;
3629 showtail = cmd_showtail;
3630 }
3631
3632#ifdef FEAT_WILDMENU
3633 if (!wildmenu)
3634 {
3635#endif
3636 msg_didany = FALSE; /* lines_left will be set */
3637 msg_start(); /* prepare for paging */
3638 msg_putchar('\n');
3639 out_flush();
3640 cmdline_row = msg_row;
3641 msg_didany = FALSE; /* lines_left will be set again */
3642 msg_start(); /* prepare for paging */
3643#ifdef FEAT_WILDMENU
3644 }
3645#endif
3646
3647 if (got_int)
3648 got_int = FALSE; /* only int. the completion, not the cmd line */
3649#ifdef FEAT_WILDMENU
3650 else if (wildmenu)
3651 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3652#endif
3653 else
3654 {
3655 /* find the length of the longest file name */
3656 maxlen = 0;
3657 for (i = 0; i < num_files; ++i)
3658 {
3659 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003660 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 || xp->xp_context == EXPAND_BUFFERS))
3662 {
3663 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3664 j = vim_strsize(NameBuff);
3665 }
3666 else
3667 j = vim_strsize(L_SHOWFILE(i));
3668 if (j > maxlen)
3669 maxlen = j;
3670 }
3671
3672 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3673 lines = num_files;
3674 else
3675 {
3676 /* compute the number of columns and lines for the listing */
3677 maxlen += 2; /* two spaces between file names */
3678 columns = ((int)Columns + 2) / maxlen;
3679 if (columns < 1)
3680 columns = 1;
3681 lines = (num_files + columns - 1) / columns;
3682 }
3683
3684 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3685
3686 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3687 {
3688 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3689 msg_clr_eos();
3690 msg_advance(maxlen - 3);
3691 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3692 }
3693
3694 /* list the files line by line */
3695 for (i = 0; i < lines; ++i)
3696 {
3697 lastlen = 999;
3698 for (k = i; k < num_files; k += lines)
3699 {
3700 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3701 {
3702 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3703 p = files_found[k] + STRLEN(files_found[k]) + 1;
3704 msg_advance(maxlen + 1);
3705 msg_puts(p);
3706 msg_advance(maxlen + 3);
3707 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3708 break;
3709 }
3710 for (j = maxlen - lastlen; --j >= 0; )
3711 msg_putchar(' ');
3712 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003713 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 || xp->xp_context == EXPAND_BUFFERS)
3715 {
3716 /* highlight directories */
3717 j = (mch_isdir(files_found[k]));
3718 if (showtail)
3719 p = L_SHOWFILE(k);
3720 else
3721 {
3722 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3723 TRUE);
3724 p = NameBuff;
3725 }
3726 }
3727 else
3728 {
3729 j = FALSE;
3730 p = L_SHOWFILE(k);
3731 }
3732 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3733 }
3734 if (msg_col > 0) /* when not wrapped around */
3735 {
3736 msg_clr_eos();
3737 msg_putchar('\n');
3738 }
3739 out_flush(); /* show one line at a time */
3740 if (got_int)
3741 {
3742 got_int = FALSE;
3743 break;
3744 }
3745 }
3746
3747 /*
3748 * we redraw the command below the lines that we have just listed
3749 * This is a bit tricky, but it saves a lot of screen updating.
3750 */
3751 cmdline_row = msg_row; /* will put it back later */
3752 }
3753
3754 if (xp->xp_numfiles == -1)
3755 FreeWild(num_files, files_found);
3756
3757 return EXPAND_OK;
3758}
3759
3760/*
3761 * Private gettail for showmatches() (and win_redr_status_matches()):
3762 * Find tail of file name path, but ignore trailing "/".
3763 */
3764 char_u *
3765sm_gettail(s)
3766 char_u *s;
3767{
3768 char_u *p;
3769 char_u *t = s;
3770 int had_sep = FALSE;
3771
3772 for (p = s; *p != NUL; )
3773 {
3774 if (vim_ispathsep(*p)
3775#ifdef BACKSLASH_IN_FILENAME
3776 && !rem_backslash(p)
3777#endif
3778 )
3779 had_sep = TRUE;
3780 else if (had_sep)
3781 {
3782 t = p;
3783 had_sep = FALSE;
3784 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003785 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787 return t;
3788}
3789
3790/*
3791 * Return TRUE if we only need to show the tail of completion matches.
3792 * When not completing file names or there is a wildcard in the path FALSE is
3793 * returned.
3794 */
3795 static int
3796expand_showtail(xp)
3797 expand_T *xp;
3798{
3799 char_u *s;
3800 char_u *end;
3801
3802 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003803 if (xp->xp_context != EXPAND_FILES
3804 && xp->xp_context != EXPAND_SHELLCMD
3805 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 return FALSE;
3807
3808 end = gettail(xp->xp_pattern);
3809 if (end == xp->xp_pattern) /* there is no path separator */
3810 return FALSE;
3811
3812 for (s = xp->xp_pattern; s < end; s++)
3813 {
3814 /* Skip escaped wildcards. Only when the backslash is not a path
3815 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3816 if (rem_backslash(s))
3817 ++s;
3818 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3819 return FALSE;
3820 }
3821 return TRUE;
3822}
3823
3824/*
3825 * Prepare a string for expansion.
3826 * When expanding file names: The string will be used with expand_wildcards().
3827 * Copy the file name into allocated memory and add a '*' at the end.
3828 * When expanding other names: The string will be used with regcomp(). Copy
3829 * the name into allocated memory and prepend "^".
3830 */
3831 char_u *
3832addstar(fname, len, context)
3833 char_u *fname;
3834 int len;
3835 int context; /* EXPAND_FILES etc. */
3836{
3837 char_u *retval;
3838 int i, j;
3839 int new_len;
3840 char_u *tail;
3841
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003842 if (context != EXPAND_FILES
3843 && context != EXPAND_SHELLCMD
3844 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 {
3846 /*
3847 * Matching will be done internally (on something other than files).
3848 * So we convert the file-matching-type wildcards into our kind for
3849 * use with vim_regcomp(). First work out how long it will be:
3850 */
3851
3852 /* For help tags the translation is done in find_help_tags().
3853 * For a tag pattern starting with "/" no translation is needed. */
3854 if (context == EXPAND_HELP
3855 || context == EXPAND_COLORS
3856 || context == EXPAND_COMPILER
3857 || (context == EXPAND_TAGS && fname[0] == '/'))
3858 retval = vim_strnsave(fname, len);
3859 else
3860 {
3861 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3862 for (i = 0; i < len; i++)
3863 {
3864 if (fname[i] == '*' || fname[i] == '~')
3865 new_len++; /* '*' needs to be replaced by ".*"
3866 '~' needs to be replaced by "\~" */
3867
3868 /* Buffer names are like file names. "." should be literal */
3869 if (context == EXPAND_BUFFERS && fname[i] == '.')
3870 new_len++; /* "." becomes "\." */
3871
3872 /* Custom expansion takes care of special things, match
3873 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003874 if ((context == EXPAND_USER_DEFINED
3875 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 new_len++; /* '\' becomes "\\" */
3877 }
3878 retval = alloc(new_len);
3879 if (retval != NULL)
3880 {
3881 retval[0] = '^';
3882 j = 1;
3883 for (i = 0; i < len; i++, j++)
3884 {
3885 /* Skip backslash. But why? At least keep it for custom
3886 * expansion. */
3887 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003888 && context != EXPAND_USER_LIST
3889 && fname[i] == '\\'
3890 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 break;
3892
3893 switch (fname[i])
3894 {
3895 case '*': retval[j++] = '.';
3896 break;
3897 case '~': retval[j++] = '\\';
3898 break;
3899 case '?': retval[j] = '.';
3900 continue;
3901 case '.': if (context == EXPAND_BUFFERS)
3902 retval[j++] = '\\';
3903 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003904 case '\\': if (context == EXPAND_USER_DEFINED
3905 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 retval[j++] = '\\';
3907 break;
3908 }
3909 retval[j] = fname[i];
3910 }
3911 retval[j] = NUL;
3912 }
3913 }
3914 }
3915 else
3916 {
3917 retval = alloc(len + 4);
3918 if (retval != NULL)
3919 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003920 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
3922 /*
3923 * Don't add a star to ~, ~user, $var or `cmd`.
3924 * ~ would be at the start of the file name, but not the tail.
3925 * $ could be anywhere in the tail.
3926 * ` could be anywhere in the file name.
3927 */
3928 tail = gettail(retval);
3929 if ((*retval != '~' || tail != retval)
3930 && vim_strchr(tail, '$') == NULL
3931 && vim_strchr(retval, '`') == NULL)
3932 retval[len++] = '*';
3933 retval[len] = NUL;
3934 }
3935 }
3936 return retval;
3937}
3938
3939/*
3940 * Must parse the command line so far to work out what context we are in.
3941 * Completion can then be done based on that context.
3942 * This routine sets the variables:
3943 * xp->xp_pattern The start of the pattern to be expanded within
3944 * the command line (ends at the cursor).
3945 * xp->xp_context The type of thing to expand. Will be one of:
3946 *
3947 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3948 * the command line, like an unknown command. Caller
3949 * should beep.
3950 * EXPAND_NOTHING Unrecognised context for completion, use char like
3951 * a normal char, rather than for completion. eg
3952 * :s/^I/
3953 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3954 * it.
3955 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3956 * EXPAND_FILES After command with XFILE set, or after setting
3957 * with P_EXPAND set. eg :e ^I, :w>>^I
3958 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3959 * when we know only directories are of interest. eg
3960 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003961 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3963 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3964 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3965 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3966 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3967 * EXPAND_EVENTS Complete event names
3968 * EXPAND_SYNTAX Complete :syntax command arguments
3969 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3970 * EXPAND_AUGROUP Complete autocommand group names
3971 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3972 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3973 * eg :unmap a^I , :cunab x^I
3974 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3975 * eg :call sub^I
3976 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3977 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3978 * names in expressions, eg :while s^I
3979 * EXPAND_ENV_VARS Complete environment variable names
3980 */
3981 static void
3982set_expand_context(xp)
3983 expand_T *xp;
3984{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003985 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 if (ccline.cmdfirstc != ':'
3987#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003988 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003989 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990#endif
3991 )
3992 {
3993 xp->xp_context = EXPAND_NOTHING;
3994 return;
3995 }
3996 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3997}
3998
3999 void
4000set_cmd_context(xp, str, len, col)
4001 expand_T *xp;
4002 char_u *str; /* start of command line */
4003 int len; /* length of command line (excl. NUL) */
4004 int col; /* position of cursor */
4005{
4006 int old_char = NUL;
4007 char_u *nextcomm;
4008
4009 /*
4010 * Avoid a UMR warning from Purify, only save the character if it has been
4011 * written before.
4012 */
4013 if (col < len)
4014 old_char = str[col];
4015 str[col] = NUL;
4016 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004017
4018#ifdef FEAT_EVAL
4019 if (ccline.cmdfirstc == '=')
4020 /* pass CMD_SIZE because there is no real command */
4021 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004022 else if (ccline.input_fn)
4023 {
4024 xp->xp_context = ccline.xp_context;
4025 xp->xp_pattern = ccline.cmdbuff;
4026 xp->xp_arg = ccline.xp_arg;
4027 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004028 else
4029#endif
4030 while (nextcomm != NULL)
4031 nextcomm = set_one_cmd_context(xp, nextcomm);
4032
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 str[col] = old_char;
4034}
4035
4036/*
4037 * Expand the command line "str" from context "xp".
4038 * "xp" must have been set by set_cmd_context().
4039 * xp->xp_pattern points into "str", to where the text that is to be expanded
4040 * starts.
4041 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4042 * cursor.
4043 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4044 * key that triggered expansion literally.
4045 * Returns EXPAND_OK otherwise.
4046 */
4047 int
4048expand_cmdline(xp, str, col, matchcount, matches)
4049 expand_T *xp;
4050 char_u *str; /* start of command line */
4051 int col; /* position of cursor */
4052 int *matchcount; /* return: nr of matches */
4053 char_u ***matches; /* return: array of pointers to matches */
4054{
4055 char_u *file_str = NULL;
4056
4057 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4058 {
4059 beep_flush();
4060 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4061 }
4062 if (xp->xp_context == EXPAND_NOTHING)
4063 {
4064 /* Caller can use the character as a normal char instead */
4065 return EXPAND_NOTHING;
4066 }
4067
4068 /* add star to file name, or convert to regexp if not exp. files. */
4069 file_str = addstar(xp->xp_pattern,
4070 (int)(str + col - xp->xp_pattern), xp->xp_context);
4071 if (file_str == NULL)
4072 return EXPAND_UNSUCCESSFUL;
4073
4074 /* find all files that match the description */
4075 if (ExpandFromContext(xp, file_str, matchcount, matches,
4076 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4077 {
4078 *matchcount = 0;
4079 *matches = NULL;
4080 }
4081 vim_free(file_str);
4082
4083 return EXPAND_OK;
4084}
4085
4086#ifdef FEAT_MULTI_LANG
4087/*
4088 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4089 */
4090static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4091
4092 static void
4093cleanup_help_tags(num_file, file)
4094 int num_file;
4095 char_u **file;
4096{
4097 int i, j;
4098 int len;
4099
4100 for (i = 0; i < num_file; ++i)
4101 {
4102 len = (int)STRLEN(file[i]) - 3;
4103 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4104 {
4105 /* Sorting on priority means the same item in another language may
4106 * be anywhere. Search all items for a match up to the "@en". */
4107 for (j = 0; j < num_file; ++j)
4108 if (j != i
4109 && (int)STRLEN(file[j]) == len + 3
4110 && STRNCMP(file[i], file[j], len + 1) == 0)
4111 break;
4112 if (j == num_file)
4113 file[i][len] = NUL;
4114 }
4115 }
4116}
4117#endif
4118
4119/*
4120 * Do the expansion based on xp->xp_context and "pat".
4121 */
4122 static int
4123ExpandFromContext(xp, pat, num_file, file, options)
4124 expand_T *xp;
4125 char_u *pat;
4126 int *num_file;
4127 char_u ***file;
4128 int options;
4129{
4130#ifdef FEAT_CMDL_COMPL
4131 regmatch_T regmatch;
4132#endif
4133 int ret;
4134 int flags;
4135
4136 flags = EW_DIR; /* include directories */
4137 if (options & WILD_LIST_NOTFOUND)
4138 flags |= EW_NOTFOUND;
4139 if (options & WILD_ADD_SLASH)
4140 flags |= EW_ADDSLASH;
4141 if (options & WILD_KEEP_ALL)
4142 flags |= EW_KEEPALL;
4143 if (options & WILD_SILENT)
4144 flags |= EW_SILENT;
4145
4146 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4147 {
4148 /*
4149 * Expand file or directory names.
4150 */
4151 int free_pat = FALSE;
4152 int i;
4153
4154 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4155 * space */
4156 if (xp->xp_backslash != XP_BS_NONE)
4157 {
4158 free_pat = TRUE;
4159 pat = vim_strsave(pat);
4160 for (i = 0; pat[i]; ++i)
4161 if (pat[i] == '\\')
4162 {
4163 if (xp->xp_backslash == XP_BS_THREE
4164 && pat[i + 1] == '\\'
4165 && pat[i + 2] == '\\'
4166 && pat[i + 3] == ' ')
4167 STRCPY(pat + i, pat + i + 3);
4168 if (xp->xp_backslash == XP_BS_ONE
4169 && pat[i + 1] == ' ')
4170 STRCPY(pat + i, pat + i + 1);
4171 }
4172 }
4173
4174 if (xp->xp_context == EXPAND_FILES)
4175 flags |= EW_FILE;
4176 else
4177 flags = (flags | EW_DIR) & ~EW_FILE;
4178 ret = expand_wildcards(1, &pat, num_file, file, flags);
4179 if (free_pat)
4180 vim_free(pat);
4181 return ret;
4182 }
4183
4184 *file = (char_u **)"";
4185 *num_file = 0;
4186 if (xp->xp_context == EXPAND_HELP)
4187 {
4188 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4189 {
4190#ifdef FEAT_MULTI_LANG
4191 cleanup_help_tags(*num_file, *file);
4192#endif
4193 return OK;
4194 }
4195 return FAIL;
4196 }
4197
4198#ifndef FEAT_CMDL_COMPL
4199 return FAIL;
4200#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004201 if (xp->xp_context == EXPAND_SHELLCMD)
4202 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (xp->xp_context == EXPAND_OLD_SETTING)
4204 return ExpandOldSetting(num_file, file);
4205 if (xp->xp_context == EXPAND_BUFFERS)
4206 return ExpandBufnames(pat, num_file, file, options);
4207 if (xp->xp_context == EXPAND_TAGS
4208 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4209 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4210 if (xp->xp_context == EXPAND_COLORS)
4211 return ExpandRTDir(pat, num_file, file, "colors");
4212 if (xp->xp_context == EXPAND_COMPILER)
4213 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004214# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4215 if (xp->xp_context == EXPAND_USER_LIST)
4216 return ExpandUserList(xp, num_file, file);
4217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
4219 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4220 if (regmatch.regprog == NULL)
4221 return FAIL;
4222
4223 /* set ignore-case according to p_ic, p_scs and pat */
4224 regmatch.rm_ic = ignorecase(pat);
4225
4226 if (xp->xp_context == EXPAND_SETTINGS
4227 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4228 ret = ExpandSettings(xp, &regmatch, num_file, file);
4229 else if (xp->xp_context == EXPAND_MAPPINGS)
4230 ret = ExpandMappings(&regmatch, num_file, file);
4231# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4232 else if (xp->xp_context == EXPAND_USER_DEFINED)
4233 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4234# endif
4235 else
4236 {
4237 static struct expgen
4238 {
4239 int context;
4240 char_u *((*func)__ARGS((expand_T *, int)));
4241 int ic;
4242 } tab[] =
4243 {
4244 {EXPAND_COMMANDS, get_command_name, FALSE},
4245#ifdef FEAT_USR_CMDS
4246 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4247 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4248 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4249 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4250#endif
4251#ifdef FEAT_EVAL
4252 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4253 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4254 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4255 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4256#endif
4257#ifdef FEAT_MENU
4258 {EXPAND_MENUS, get_menu_name, FALSE},
4259 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4260#endif
4261#ifdef FEAT_SYN_HL
4262 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4263#endif
4264 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4265#ifdef FEAT_AUTOCMD
4266 {EXPAND_EVENTS, get_event_name, TRUE},
4267 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4268#endif
4269#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4270 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4271 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4272#endif
4273 {EXPAND_ENV_VARS, get_env_name, TRUE},
4274 };
4275 int i;
4276
4277 /*
4278 * Find a context in the table and call the ExpandGeneric() with the
4279 * right function to do the expansion.
4280 */
4281 ret = FAIL;
4282 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4283 if (xp->xp_context == tab[i].context)
4284 {
4285 if (tab[i].ic)
4286 regmatch.rm_ic = TRUE;
4287 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4288 break;
4289 }
4290 }
4291
4292 vim_free(regmatch.regprog);
4293
4294 return ret;
4295#endif /* FEAT_CMDL_COMPL */
4296}
4297
4298#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4299/*
4300 * Expand a list of names.
4301 *
4302 * Generic function for command line completion. It calls a function to
4303 * obtain strings, one by one. The strings are matched against a regexp
4304 * program. Matching strings are copied into an array, which is returned.
4305 *
4306 * Returns OK when no problems encountered, FAIL for error (out of memory).
4307 */
4308 int
4309ExpandGeneric(xp, regmatch, num_file, file, func)
4310 expand_T *xp;
4311 regmatch_T *regmatch;
4312 int *num_file;
4313 char_u ***file;
4314 char_u *((*func)__ARGS((expand_T *, int)));
4315 /* returns a string from the list */
4316{
4317 int i;
4318 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004319 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 char_u *str;
4321
4322 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004323 * round == 0: count the number of matching names
4324 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004326 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
4328 for (i = 0; ; ++i)
4329 {
4330 str = (*func)(xp, i);
4331 if (str == NULL) /* end of list */
4332 break;
4333 if (*str == NUL) /* skip empty strings */
4334 continue;
4335
4336 if (vim_regexec(regmatch, str, (colnr_T)0))
4337 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004338 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 {
4340 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4341 (*file)[count] = str;
4342#ifdef FEAT_MENU
4343 if (func == get_menu_names && str != NULL)
4344 {
4345 /* test for separator added by get_menu_names() */
4346 str += STRLEN(str) - 1;
4347 if (*str == '\001')
4348 *str = '.';
4349 }
4350#endif
4351 }
4352 ++count;
4353 }
4354 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004355 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
4357 if (count == 0)
4358 return OK;
4359 *num_file = count;
4360 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4361 if (*file == NULL)
4362 {
4363 *file = (char_u **)"";
4364 return FAIL;
4365 }
4366 count = 0;
4367 }
4368 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004369
4370 /* Sort the results. */
4371 sort_strings(*file, *num_file);
4372
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 return OK;
4374}
4375
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004376/*
4377 * Complete a shell command.
4378 * Returns FAIL or OK;
4379 */
4380 static int
4381expand_shellcmd(filepat, num_file, file, flagsarg)
4382 char_u *filepat; /* pattern to match with command names */
4383 int *num_file; /* return: number of matches */
4384 char_u ***file; /* return: array with matches */
4385 int flagsarg; /* EW_ flags */
4386{
4387 char_u *pat;
4388 int i;
4389 char_u *path;
4390 int mustfree = FALSE;
4391 garray_T ga;
4392 char_u *buf = alloc(MAXPATHL);
4393 size_t l;
4394 char_u *s, *e;
4395 int flags = flagsarg;
4396 int ret;
4397
4398 if (buf == NULL)
4399 return FAIL;
4400
4401 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4402 * space */
4403 pat = vim_strsave(filepat);
4404 for (i = 0; pat[i]; ++i)
4405 if (pat[i] == '\\' && pat[i + 1] == ' ')
4406 STRCPY(pat + i, pat + i + 1);
4407
4408 flags |= EW_FILE | EW_EXEC;
4409
4410 /* For an absolute name we don't use $PATH. */
4411 if ((pat[0] == '.' && (vim_ispathsep(pat[1])
4412 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4413 path = (char_u *)".";
4414 else
4415 path = vim_getenv((char_u *)"PATH", &mustfree);
4416
4417 /*
4418 * Go over all directories in $PATH. Expand matches in that directory and
4419 * collect them in "ga".
4420 */
4421 ga_init2(&ga, (int)sizeof(char *), 10);
4422 for (s = path; *s != NUL; s = e)
4423 {
4424#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4425 e = vim_strchr(s, ';');
4426#else
4427 e = vim_strchr(s, ':');
4428#endif
4429 if (e == NULL)
4430 e = s + STRLEN(s);
4431
4432 l = e - s;
4433 if (l > MAXPATHL - 5)
4434 break;
4435 vim_strncpy(buf, s, l);
4436 add_pathsep(buf);
4437 l = STRLEN(buf);
4438 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4439
4440 /* Expand matches in one directory of $PATH. */
4441 ret = expand_wildcards(1, &buf, num_file, file, flags);
4442 if (ret == OK)
4443 {
4444 if (ga_grow(&ga, *num_file) == FAIL)
4445 FreeWild(*num_file, *file);
4446 else
4447 {
4448 for (i = 0; i < *num_file; ++i)
4449 {
4450 s = (*file)[i];
4451 if (STRLEN(s) > l)
4452 {
4453 /* Remove the path again. */
4454 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4455 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4456 }
4457 else
4458 vim_free(s);
4459 }
4460 vim_free(*file);
4461 }
4462 }
4463 if (*e != NUL)
4464 ++e;
4465 }
4466 *file = ga.ga_data;
4467 *num_file = ga.ga_len;
4468
4469 vim_free(buf);
4470 vim_free(pat);
4471 if (mustfree)
4472 vim_free(path);
4473 return OK;
4474}
4475
4476
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004478static 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));
4479
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004481 * call "user_expand_func()" to invoke a user defined VimL function and return
4482 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004484 static void *
4485call_user_expand_func(user_expand_func, xp, num_file, file)
4486 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 int *num_file;
4489 char_u ***file;
4490{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004491 char_u keep;
4492 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004495 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004496 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004499 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 *num_file = 0;
4501 *file = NULL;
4502
4503 keep = ccline.cmdbuff[ccline.cmdlen];
4504 ccline.cmdbuff[ccline.cmdlen] = 0;
4505 sprintf((char *)num, "%d", ccline.cmdpos);
4506 args[0] = xp->xp_pattern;
4507 args[1] = ccline.cmdbuff;
4508 args[2] = num;
4509
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004510 /* Save the cmdline, we don't know what the function may do. */
4511 save_ccline = ccline;
4512 ccline.cmdbuff = NULL;
4513 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004515
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004516 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004517
4518 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004520
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004522
4523 return ret;
4524}
4525
4526/*
4527 * Expand names with a function defined by the user.
4528 */
4529 static int
4530ExpandUserDefined(xp, regmatch, num_file, file)
4531 expand_T *xp;
4532 regmatch_T *regmatch;
4533 int *num_file;
4534 char_u ***file;
4535{
4536 char_u *retstr;
4537 char_u *s;
4538 char_u *e;
4539 char_u keep;
4540 garray_T ga;
4541
4542 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4543 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 return FAIL;
4545
4546 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004547 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
4549 e = vim_strchr(s, '\n');
4550 if (e == NULL)
4551 e = s + STRLEN(s);
4552 keep = *e;
4553 *e = 0;
4554
4555 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4556 {
4557 *e = keep;
4558 if (*e != NUL)
4559 ++e;
4560 continue;
4561 }
4562
4563 if (ga_grow(&ga, 1) == FAIL)
4564 break;
4565
4566 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4567 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
4569 *e = keep;
4570 if (*e != NUL)
4571 ++e;
4572 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004573 vim_free(retstr);
4574 *file = ga.ga_data;
4575 *num_file = ga.ga_len;
4576 return OK;
4577}
4578
4579/*
4580 * Expand names with a list returned by a function defined by the user.
4581 */
4582 static int
4583ExpandUserList(xp, num_file, file)
4584 expand_T *xp;
4585 int *num_file;
4586 char_u ***file;
4587{
4588 list_T *retlist;
4589 listitem_T *li;
4590 garray_T ga;
4591
4592 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4593 if (retlist == NULL)
4594 return FAIL;
4595
4596 ga_init2(&ga, (int)sizeof(char *), 3);
4597 /* Loop over the items in the list. */
4598 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4599 {
4600 if (li->li_tv.v_type != VAR_STRING)
4601 continue; /* Skip non-string items */
4602
4603 if (ga_grow(&ga, 1) == FAIL)
4604 break;
4605
4606 ((char_u **)ga.ga_data)[ga.ga_len] =
4607 vim_strsave(li->li_tv.vval.v_string);
4608 ++ga.ga_len;
4609 }
4610 list_unref(retlist);
4611
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 *file = ga.ga_data;
4613 *num_file = ga.ga_len;
4614 return OK;
4615}
4616#endif
4617
4618/*
4619 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4620 * or compiler names.
4621 */
4622 static int
4623ExpandRTDir(pat, num_file, file, dirname)
4624 char_u *pat;
4625 int *num_file;
4626 char_u ***file;
4627 char *dirname; /* "colors" or "compiler" */
4628{
4629 char_u *all;
4630 char_u *s;
4631 char_u *e;
4632 garray_T ga;
4633
4634 *num_file = 0;
4635 *file = NULL;
4636 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4637 if (s == NULL)
4638 return FAIL;
4639 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4640 all = globpath(p_rtp, s);
4641 vim_free(s);
4642 if (all == NULL)
4643 return FAIL;
4644
4645 ga_init2(&ga, (int)sizeof(char *), 3);
4646 for (s = all; *s != NUL; s = e)
4647 {
4648 e = vim_strchr(s, '\n');
4649 if (e == NULL)
4650 e = s + STRLEN(s);
4651 if (ga_grow(&ga, 1) == FAIL)
4652 break;
4653 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4654 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004655 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (*s == '\n' || vim_ispathsep(*s))
4657 break;
4658 ++s;
4659 ((char_u **)ga.ga_data)[ga.ga_len] =
4660 vim_strnsave(s, (int)(e - s - 4));
4661 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 }
4663 if (*e != NUL)
4664 ++e;
4665 }
4666 vim_free(all);
4667 *file = ga.ga_data;
4668 *num_file = ga.ga_len;
4669 return OK;
4670}
4671
4672#endif
4673
4674#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4675/*
4676 * Expand "file" for all comma-separated directories in "path".
4677 * Returns an allocated string with all matches concatenated, separated by
4678 * newlines. Returns NULL for an error or no matches.
4679 */
4680 char_u *
4681globpath(path, file)
4682 char_u *path;
4683 char_u *file;
4684{
4685 expand_T xpc;
4686 char_u *buf;
4687 garray_T ga;
4688 int i;
4689 int len;
4690 int num_p;
4691 char_u **p;
4692 char_u *cur = NULL;
4693
4694 buf = alloc(MAXPATHL);
4695 if (buf == NULL)
4696 return NULL;
4697
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004698 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004700
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 ga_init2(&ga, 1, 100);
4702
4703 /* Loop over all entries in {path}. */
4704 while (*path != NUL)
4705 {
4706 /* Copy one item of the path to buf[] and concatenate the file name. */
4707 copy_option_part(&path, buf, MAXPATHL, ",");
4708 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4709 {
4710 add_pathsep(buf);
4711 STRCAT(buf, file);
4712 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4713 && num_p > 0)
4714 {
4715 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4716 for (len = 0, i = 0; i < num_p; ++i)
4717 len += (long_u)STRLEN(p[i]) + 1;
4718
4719 /* Concatenate new results to previous ones. */
4720 if (ga_grow(&ga, len) == OK)
4721 {
4722 cur = (char_u *)ga.ga_data + ga.ga_len;
4723 for (i = 0; i < num_p; ++i)
4724 {
4725 STRCPY(cur, p[i]);
4726 cur += STRLEN(p[i]);
4727 *cur++ = '\n';
4728 }
4729 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731 FreeWild(num_p, p);
4732 }
4733 }
4734 }
4735 if (cur != NULL)
4736 *--cur = 0; /* Replace trailing newline with NUL */
4737
4738 vim_free(buf);
4739 return (char_u *)ga.ga_data;
4740}
4741
4742#endif
4743
4744#if defined(FEAT_CMDHIST) || defined(PROTO)
4745
4746/*********************************
4747 * Command line history stuff *
4748 *********************************/
4749
4750/*
4751 * Translate a history character to the associated type number.
4752 */
4753 static int
4754hist_char2type(c)
4755 int c;
4756{
4757 if (c == ':')
4758 return HIST_CMD;
4759 if (c == '=')
4760 return HIST_EXPR;
4761 if (c == '@')
4762 return HIST_INPUT;
4763 if (c == '>')
4764 return HIST_DEBUG;
4765 return HIST_SEARCH; /* must be '?' or '/' */
4766}
4767
4768/*
4769 * Table of history names.
4770 * These names are used in :history and various hist...() functions.
4771 * It is sufficient to give the significant prefix of a history name.
4772 */
4773
4774static char *(history_names[]) =
4775{
4776 "cmd",
4777 "search",
4778 "expr",
4779 "input",
4780 "debug",
4781 NULL
4782};
4783
4784/*
4785 * init_history() - Initialize the command line history.
4786 * Also used to re-allocate the history when the size changes.
4787 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004788 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789init_history()
4790{
4791 int newlen; /* new length of history table */
4792 histentry_T *temp;
4793 int i;
4794 int j;
4795 int type;
4796
4797 /*
4798 * If size of history table changed, reallocate it
4799 */
4800 newlen = (int)p_hi;
4801 if (newlen != hislen) /* history length changed */
4802 {
4803 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4804 {
4805 if (newlen)
4806 {
4807 temp = (histentry_T *)lalloc(
4808 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4809 if (temp == NULL) /* out of memory! */
4810 {
4811 if (type == 0) /* first one: just keep the old length */
4812 {
4813 newlen = hislen;
4814 break;
4815 }
4816 /* Already changed one table, now we can only have zero
4817 * length for all tables. */
4818 newlen = 0;
4819 type = -1;
4820 continue;
4821 }
4822 }
4823 else
4824 temp = NULL;
4825 if (newlen == 0 || temp != NULL)
4826 {
4827 if (hisidx[type] < 0) /* there are no entries yet */
4828 {
4829 for (i = 0; i < newlen; ++i)
4830 {
4831 temp[i].hisnum = 0;
4832 temp[i].hisstr = NULL;
4833 }
4834 }
4835 else if (newlen > hislen) /* array becomes bigger */
4836 {
4837 for (i = 0; i <= hisidx[type]; ++i)
4838 temp[i] = history[type][i];
4839 j = i;
4840 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4841 {
4842 temp[i].hisnum = 0;
4843 temp[i].hisstr = NULL;
4844 }
4845 for ( ; j < hislen; ++i, ++j)
4846 temp[i] = history[type][j];
4847 }
4848 else /* array becomes smaller or 0 */
4849 {
4850 j = hisidx[type];
4851 for (i = newlen - 1; ; --i)
4852 {
4853 if (i >= 0) /* copy newest entries */
4854 temp[i] = history[type][j];
4855 else /* remove older entries */
4856 vim_free(history[type][j].hisstr);
4857 if (--j < 0)
4858 j = hislen - 1;
4859 if (j == hisidx[type])
4860 break;
4861 }
4862 hisidx[type] = newlen - 1;
4863 }
4864 vim_free(history[type]);
4865 history[type] = temp;
4866 }
4867 }
4868 hislen = newlen;
4869 }
4870}
4871
4872/*
4873 * Check if command line 'str' is already in history.
4874 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4875 */
4876 static int
4877in_history(type, str, move_to_front)
4878 int type;
4879 char_u *str;
4880 int move_to_front; /* Move the entry to the front if it exists */
4881{
4882 int i;
4883 int last_i = -1;
4884
4885 if (hisidx[type] < 0)
4886 return FALSE;
4887 i = hisidx[type];
4888 do
4889 {
4890 if (history[type][i].hisstr == NULL)
4891 return FALSE;
4892 if (STRCMP(str, history[type][i].hisstr) == 0)
4893 {
4894 if (!move_to_front)
4895 return TRUE;
4896 last_i = i;
4897 break;
4898 }
4899 if (--i < 0)
4900 i = hislen - 1;
4901 } while (i != hisidx[type]);
4902
4903 if (last_i >= 0)
4904 {
4905 str = history[type][i].hisstr;
4906 while (i != hisidx[type])
4907 {
4908 if (++i >= hislen)
4909 i = 0;
4910 history[type][last_i] = history[type][i];
4911 last_i = i;
4912 }
4913 history[type][i].hisstr = str;
4914 history[type][i].hisnum = ++hisnum[type];
4915 return TRUE;
4916 }
4917 return FALSE;
4918}
4919
4920/*
4921 * Convert history name (from table above) to its HIST_ equivalent.
4922 * When "name" is empty, return "cmd" history.
4923 * Returns -1 for unknown history name.
4924 */
4925 int
4926get_histtype(name)
4927 char_u *name;
4928{
4929 int i;
4930 int len = (int)STRLEN(name);
4931
4932 /* No argument: use current history. */
4933 if (len == 0)
4934 return hist_char2type(ccline.cmdfirstc);
4935
4936 for (i = 0; history_names[i] != NULL; ++i)
4937 if (STRNICMP(name, history_names[i], len) == 0)
4938 return i;
4939
4940 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4941 return hist_char2type(name[0]);
4942
4943 return -1;
4944}
4945
4946static int last_maptick = -1; /* last seen maptick */
4947
4948/*
4949 * Add the given string to the given history. If the string is already in the
4950 * history then it is moved to the front. "histype" may be one of he HIST_
4951 * values.
4952 */
4953 void
4954add_to_history(histype, new_entry, in_map, sep)
4955 int histype;
4956 char_u *new_entry;
4957 int in_map; /* consider maptick when inside a mapping */
4958 int sep; /* separator character used (search hist) */
4959{
4960 histentry_T *hisptr;
4961 int len;
4962
4963 if (hislen == 0) /* no history */
4964 return;
4965
4966 /*
4967 * Searches inside the same mapping overwrite each other, so that only
4968 * the last line is kept. Be careful not to remove a line that was moved
4969 * down, only lines that were added.
4970 */
4971 if (histype == HIST_SEARCH && in_map)
4972 {
4973 if (maptick == last_maptick)
4974 {
4975 /* Current line is from the same mapping, remove it */
4976 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4977 vim_free(hisptr->hisstr);
4978 hisptr->hisstr = NULL;
4979 hisptr->hisnum = 0;
4980 --hisnum[histype];
4981 if (--hisidx[HIST_SEARCH] < 0)
4982 hisidx[HIST_SEARCH] = hislen - 1;
4983 }
4984 last_maptick = -1;
4985 }
4986 if (!in_history(histype, new_entry, TRUE))
4987 {
4988 if (++hisidx[histype] == hislen)
4989 hisidx[histype] = 0;
4990 hisptr = &history[histype][hisidx[histype]];
4991 vim_free(hisptr->hisstr);
4992
4993 /* Store the separator after the NUL of the string. */
4994 len = STRLEN(new_entry);
4995 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4996 if (hisptr->hisstr != NULL)
4997 hisptr->hisstr[len + 1] = sep;
4998
4999 hisptr->hisnum = ++hisnum[histype];
5000 if (histype == HIST_SEARCH && in_map)
5001 last_maptick = maptick;
5002 }
5003}
5004
5005#if defined(FEAT_EVAL) || defined(PROTO)
5006
5007/*
5008 * Get identifier of newest history entry.
5009 * "histype" may be one of the HIST_ values.
5010 */
5011 int
5012get_history_idx(histype)
5013 int histype;
5014{
5015 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5016 || hisidx[histype] < 0)
5017 return -1;
5018
5019 return history[histype][hisidx[histype]].hisnum;
5020}
5021
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005022static struct cmdline_info *get_ccline_ptr __ARGS((void));
5023
5024/*
5025 * Get pointer to the command line info to use. cmdline_paste() may clear
5026 * ccline and put the previous value in prev_ccline.
5027 */
5028 static struct cmdline_info *
5029get_ccline_ptr()
5030{
5031 if ((State & CMDLINE) == 0)
5032 return NULL;
5033 if (ccline.cmdbuff != NULL)
5034 return &ccline;
5035 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5036 return &prev_ccline;
5037 return NULL;
5038}
5039
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040/*
5041 * Get the current command line in allocated memory.
5042 * Only works when the command line is being edited.
5043 * Returns NULL when something is wrong.
5044 */
5045 char_u *
5046get_cmdline_str()
5047{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005048 struct cmdline_info *p = get_ccline_ptr();
5049
5050 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005052 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053}
5054
5055/*
5056 * Get the current command line position, counted in bytes.
5057 * Zero is the first position.
5058 * Only works when the command line is being edited.
5059 * Returns -1 when something is wrong.
5060 */
5061 int
5062get_cmdline_pos()
5063{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005064 struct cmdline_info *p = get_ccline_ptr();
5065
5066 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005068 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069}
5070
5071/*
5072 * Set the command line byte position to "pos". Zero is the first position.
5073 * Only works when the command line is being edited.
5074 * Returns 1 when failed, 0 when OK.
5075 */
5076 int
5077set_cmdline_pos(pos)
5078 int pos;
5079{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005080 struct cmdline_info *p = get_ccline_ptr();
5081
5082 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 return 1;
5084
5085 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5086 * changed the command line. */
5087 if (pos < 0)
5088 new_cmdpos = 0;
5089 else
5090 new_cmdpos = pos;
5091 return 0;
5092}
5093
5094/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005095 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005096 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005097 * Only works when the command line is being edited.
5098 * Returns NUL when something is wrong.
5099 */
5100 int
5101get_cmdline_type()
5102{
5103 struct cmdline_info *p = get_ccline_ptr();
5104
5105 if (p == NULL)
5106 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005107 if (p->cmdfirstc == NUL)
5108 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005109 return p->cmdfirstc;
5110}
5111
5112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 * Calculate history index from a number:
5114 * num > 0: seen as identifying number of a history entry
5115 * num < 0: relative position in history wrt newest entry
5116 * "histype" may be one of the HIST_ values.
5117 */
5118 static int
5119calc_hist_idx(histype, num)
5120 int histype;
5121 int num;
5122{
5123 int i;
5124 histentry_T *hist;
5125 int wrapped = FALSE;
5126
5127 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5128 || (i = hisidx[histype]) < 0 || num == 0)
5129 return -1;
5130
5131 hist = history[histype];
5132 if (num > 0)
5133 {
5134 while (hist[i].hisnum > num)
5135 if (--i < 0)
5136 {
5137 if (wrapped)
5138 break;
5139 i += hislen;
5140 wrapped = TRUE;
5141 }
5142 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5143 return i;
5144 }
5145 else if (-num <= hislen)
5146 {
5147 i += num + 1;
5148 if (i < 0)
5149 i += hislen;
5150 if (hist[i].hisstr != NULL)
5151 return i;
5152 }
5153 return -1;
5154}
5155
5156/*
5157 * Get a history entry by its index.
5158 * "histype" may be one of the HIST_ values.
5159 */
5160 char_u *
5161get_history_entry(histype, idx)
5162 int histype;
5163 int idx;
5164{
5165 idx = calc_hist_idx(histype, idx);
5166 if (idx >= 0)
5167 return history[histype][idx].hisstr;
5168 else
5169 return (char_u *)"";
5170}
5171
5172/*
5173 * Clear all entries of a history.
5174 * "histype" may be one of the HIST_ values.
5175 */
5176 int
5177clr_history(histype)
5178 int histype;
5179{
5180 int i;
5181 histentry_T *hisptr;
5182
5183 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5184 {
5185 hisptr = history[histype];
5186 for (i = hislen; i--;)
5187 {
5188 vim_free(hisptr->hisstr);
5189 hisptr->hisnum = 0;
5190 hisptr++->hisstr = NULL;
5191 }
5192 hisidx[histype] = -1; /* mark history as cleared */
5193 hisnum[histype] = 0; /* reset identifier counter */
5194 return OK;
5195 }
5196 return FAIL;
5197}
5198
5199/*
5200 * Remove all entries matching {str} from a history.
5201 * "histype" may be one of the HIST_ values.
5202 */
5203 int
5204del_history_entry(histype, str)
5205 int histype;
5206 char_u *str;
5207{
5208 regmatch_T regmatch;
5209 histentry_T *hisptr;
5210 int idx;
5211 int i;
5212 int last;
5213 int found = FALSE;
5214
5215 regmatch.regprog = NULL;
5216 regmatch.rm_ic = FALSE; /* always match case */
5217 if (hislen != 0
5218 && histype >= 0
5219 && histype < HIST_COUNT
5220 && *str != NUL
5221 && (idx = hisidx[histype]) >= 0
5222 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5223 != NULL)
5224 {
5225 i = last = idx;
5226 do
5227 {
5228 hisptr = &history[histype][i];
5229 if (hisptr->hisstr == NULL)
5230 break;
5231 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5232 {
5233 found = TRUE;
5234 vim_free(hisptr->hisstr);
5235 hisptr->hisstr = NULL;
5236 hisptr->hisnum = 0;
5237 }
5238 else
5239 {
5240 if (i != last)
5241 {
5242 history[histype][last] = *hisptr;
5243 hisptr->hisstr = NULL;
5244 hisptr->hisnum = 0;
5245 }
5246 if (--last < 0)
5247 last += hislen;
5248 }
5249 if (--i < 0)
5250 i += hislen;
5251 } while (i != idx);
5252 if (history[histype][idx].hisstr == NULL)
5253 hisidx[histype] = -1;
5254 }
5255 vim_free(regmatch.regprog);
5256 return found;
5257}
5258
5259/*
5260 * Remove an indexed entry from a history.
5261 * "histype" may be one of the HIST_ values.
5262 */
5263 int
5264del_history_idx(histype, idx)
5265 int histype;
5266 int idx;
5267{
5268 int i, j;
5269
5270 i = calc_hist_idx(histype, idx);
5271 if (i < 0)
5272 return FALSE;
5273 idx = hisidx[histype];
5274 vim_free(history[histype][i].hisstr);
5275
5276 /* When deleting the last added search string in a mapping, reset
5277 * last_maptick, so that the last added search string isn't deleted again.
5278 */
5279 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5280 last_maptick = -1;
5281
5282 while (i != idx)
5283 {
5284 j = (i + 1) % hislen;
5285 history[histype][i] = history[histype][j];
5286 i = j;
5287 }
5288 history[histype][i].hisstr = NULL;
5289 history[histype][i].hisnum = 0;
5290 if (--i < 0)
5291 i += hislen;
5292 hisidx[histype] = i;
5293 return TRUE;
5294}
5295
5296#endif /* FEAT_EVAL */
5297
5298#if defined(FEAT_CRYPT) || defined(PROTO)
5299/*
5300 * Very specific function to remove the value in ":set key=val" from the
5301 * history.
5302 */
5303 void
5304remove_key_from_history()
5305{
5306 char_u *p;
5307 int i;
5308
5309 i = hisidx[HIST_CMD];
5310 if (i < 0)
5311 return;
5312 p = history[HIST_CMD][i].hisstr;
5313 if (p != NULL)
5314 for ( ; *p; ++p)
5315 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5316 {
5317 p = vim_strchr(p + 3, '=');
5318 if (p == NULL)
5319 break;
5320 ++p;
5321 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5322 if (p[i] == '\\' && p[i + 1])
5323 ++i;
5324 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5325 --p;
5326 }
5327}
5328#endif
5329
5330#endif /* FEAT_CMDHIST */
5331
5332#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5333/*
5334 * Get indices "num1,num2" that specify a range within a list (not a range of
5335 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5336 * Returns OK if parsed successfully, otherwise FAIL.
5337 */
5338 int
5339get_list_range(str, num1, num2)
5340 char_u **str;
5341 int *num1;
5342 int *num2;
5343{
5344 int len;
5345 int first = FALSE;
5346 long num;
5347
5348 *str = skipwhite(*str);
5349 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5350 {
5351 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5352 *str += len;
5353 *num1 = (int)num;
5354 first = TRUE;
5355 }
5356 *str = skipwhite(*str);
5357 if (**str == ',') /* parse "to" part of range */
5358 {
5359 *str = skipwhite(*str + 1);
5360 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5361 if (len > 0)
5362 {
5363 *num2 = (int)num;
5364 *str = skipwhite(*str + len);
5365 }
5366 else if (!first) /* no number given at all */
5367 return FAIL;
5368 }
5369 else if (first) /* only one number given */
5370 *num2 = *num1;
5371 return OK;
5372}
5373#endif
5374
5375#if defined(FEAT_CMDHIST) || defined(PROTO)
5376/*
5377 * :history command - print a history
5378 */
5379 void
5380ex_history(eap)
5381 exarg_T *eap;
5382{
5383 histentry_T *hist;
5384 int histype1 = HIST_CMD;
5385 int histype2 = HIST_CMD;
5386 int hisidx1 = 1;
5387 int hisidx2 = -1;
5388 int idx;
5389 int i, j, k;
5390 char_u *end;
5391 char_u *arg = eap->arg;
5392
5393 if (hislen == 0)
5394 {
5395 MSG(_("'history' option is zero"));
5396 return;
5397 }
5398
5399 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5400 {
5401 end = arg;
5402 while (ASCII_ISALPHA(*end)
5403 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5404 end++;
5405 i = *end;
5406 *end = NUL;
5407 histype1 = get_histtype(arg);
5408 if (histype1 == -1)
5409 {
5410 if (STRICMP(arg, "all") == 0)
5411 {
5412 histype1 = 0;
5413 histype2 = HIST_COUNT-1;
5414 }
5415 else
5416 {
5417 *end = i;
5418 EMSG(_(e_trailing));
5419 return;
5420 }
5421 }
5422 else
5423 histype2 = histype1;
5424 *end = i;
5425 }
5426 else
5427 end = arg;
5428 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5429 {
5430 EMSG(_(e_trailing));
5431 return;
5432 }
5433
5434 for (; !got_int && histype1 <= histype2; ++histype1)
5435 {
5436 STRCPY(IObuff, "\n # ");
5437 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5438 MSG_PUTS_TITLE(IObuff);
5439 idx = hisidx[histype1];
5440 hist = history[histype1];
5441 j = hisidx1;
5442 k = hisidx2;
5443 if (j < 0)
5444 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5445 if (k < 0)
5446 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5447 if (idx >= 0 && j <= k)
5448 for (i = idx + 1; !got_int; ++i)
5449 {
5450 if (i == hislen)
5451 i = 0;
5452 if (hist[i].hisstr != NULL
5453 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5454 {
5455 msg_putchar('\n');
5456 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5457 hist[i].hisnum);
5458 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5459 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5460 (int)Columns - 10);
5461 else
5462 STRCAT(IObuff, hist[i].hisstr);
5463 msg_outtrans(IObuff);
5464 out_flush();
5465 }
5466 if (i == idx)
5467 break;
5468 }
5469 }
5470}
5471#endif
5472
5473#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5474static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5475static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5476static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5477static int viminfo_add_at_front = FALSE;
5478
5479static int hist_type2char __ARGS((int type, int use_question));
5480
5481/*
5482 * Translate a history type number to the associated character.
5483 */
5484 static int
5485hist_type2char(type, use_question)
5486 int type;
5487 int use_question; /* use '?' instead of '/' */
5488{
5489 if (type == HIST_CMD)
5490 return ':';
5491 if (type == HIST_SEARCH)
5492 {
5493 if (use_question)
5494 return '?';
5495 else
5496 return '/';
5497 }
5498 if (type == HIST_EXPR)
5499 return '=';
5500 return '@';
5501}
5502
5503/*
5504 * Prepare for reading the history from the viminfo file.
5505 * This allocates history arrays to store the read history lines.
5506 */
5507 void
5508prepare_viminfo_history(asklen)
5509 int asklen;
5510{
5511 int i;
5512 int num;
5513 int type;
5514 int len;
5515
5516 init_history();
5517 viminfo_add_at_front = (asklen != 0);
5518 if (asklen > hislen)
5519 asklen = hislen;
5520
5521 for (type = 0; type < HIST_COUNT; ++type)
5522 {
5523 /*
5524 * Count the number of empty spaces in the history list. If there are
5525 * more spaces available than we request, then fill them up.
5526 */
5527 for (i = 0, num = 0; i < hislen; i++)
5528 if (history[type][i].hisstr == NULL)
5529 num++;
5530 len = asklen;
5531 if (num > len)
5532 len = num;
5533 if (len <= 0)
5534 viminfo_history[type] = NULL;
5535 else
5536 viminfo_history[type] =
5537 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5538 if (viminfo_history[type] == NULL)
5539 len = 0;
5540 viminfo_hislen[type] = len;
5541 viminfo_hisidx[type] = 0;
5542 }
5543}
5544
5545/*
5546 * Accept a line from the viminfo, store it in the history array when it's
5547 * new.
5548 */
5549 int
5550read_viminfo_history(virp)
5551 vir_T *virp;
5552{
5553 int type;
5554 long_u len;
5555 char_u *val;
5556 char_u *p;
5557
5558 type = hist_char2type(virp->vir_line[0]);
5559 if (viminfo_hisidx[type] < viminfo_hislen[type])
5560 {
5561 val = viminfo_readstring(virp, 1, TRUE);
5562 if (val != NULL && *val != NUL)
5563 {
5564 if (!in_history(type, val + (type == HIST_SEARCH),
5565 viminfo_add_at_front))
5566 {
5567 /* Need to re-allocate to append the separator byte. */
5568 len = STRLEN(val);
5569 p = lalloc(len + 2, TRUE);
5570 if (p != NULL)
5571 {
5572 if (type == HIST_SEARCH)
5573 {
5574 /* Search entry: Move the separator from the first
5575 * column to after the NUL. */
5576 mch_memmove(p, val + 1, (size_t)len);
5577 p[len] = (*val == ' ' ? NUL : *val);
5578 }
5579 else
5580 {
5581 /* Not a search entry: No separator in the viminfo
5582 * file, add a NUL separator. */
5583 mch_memmove(p, val, (size_t)len + 1);
5584 p[len + 1] = NUL;
5585 }
5586 viminfo_history[type][viminfo_hisidx[type]++] = p;
5587 }
5588 }
5589 }
5590 vim_free(val);
5591 }
5592 return viminfo_readline(virp);
5593}
5594
5595 void
5596finish_viminfo_history()
5597{
5598 int idx;
5599 int i;
5600 int type;
5601
5602 for (type = 0; type < HIST_COUNT; ++type)
5603 {
5604 if (history[type] == NULL)
5605 return;
5606 idx = hisidx[type] + viminfo_hisidx[type];
5607 if (idx >= hislen)
5608 idx -= hislen;
5609 else if (idx < 0)
5610 idx = hislen - 1;
5611 if (viminfo_add_at_front)
5612 hisidx[type] = idx;
5613 else
5614 {
5615 if (hisidx[type] == -1)
5616 hisidx[type] = hislen - 1;
5617 do
5618 {
5619 if (history[type][idx].hisstr != NULL)
5620 break;
5621 if (++idx == hislen)
5622 idx = 0;
5623 } while (idx != hisidx[type]);
5624 if (idx != hisidx[type] && --idx < 0)
5625 idx = hislen - 1;
5626 }
5627 for (i = 0; i < viminfo_hisidx[type]; i++)
5628 {
5629 vim_free(history[type][idx].hisstr);
5630 history[type][idx].hisstr = viminfo_history[type][i];
5631 if (--idx < 0)
5632 idx = hislen - 1;
5633 }
5634 idx += 1;
5635 idx %= hislen;
5636 for (i = 0; i < viminfo_hisidx[type]; i++)
5637 {
5638 history[type][idx++].hisnum = ++hisnum[type];
5639 idx %= hislen;
5640 }
5641 vim_free(viminfo_history[type]);
5642 viminfo_history[type] = NULL;
5643 }
5644}
5645
5646 void
5647write_viminfo_history(fp)
5648 FILE *fp;
5649{
5650 int i;
5651 int type;
5652 int num_saved;
5653 char_u *p;
5654 int c;
5655
5656 init_history();
5657 if (hislen == 0)
5658 return;
5659 for (type = 0; type < HIST_COUNT; ++type)
5660 {
5661 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5662 if (num_saved == 0)
5663 continue;
5664 if (num_saved < 0) /* Use default */
5665 num_saved = hislen;
5666 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5667 type == HIST_CMD ? _("Command Line") :
5668 type == HIST_SEARCH ? _("Search String") :
5669 type == HIST_EXPR ? _("Expression") :
5670 _("Input Line"));
5671 if (num_saved > hislen)
5672 num_saved = hislen;
5673 i = hisidx[type];
5674 if (i >= 0)
5675 while (num_saved--)
5676 {
5677 p = history[type][i].hisstr;
5678 if (p != NULL)
5679 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005680 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 /* For the search history: put the separator in the second
5682 * column; use a space if there isn't one. */
5683 if (type == HIST_SEARCH)
5684 {
5685 c = p[STRLEN(p) + 1];
5686 putc(c == NUL ? ' ' : c, fp);
5687 }
5688 viminfo_writestring(fp, p);
5689 }
5690 if (--i < 0)
5691 i = hislen - 1;
5692 }
5693 }
5694}
5695#endif /* FEAT_VIMINFO */
5696
5697#if defined(FEAT_FKMAP) || defined(PROTO)
5698/*
5699 * Write a character at the current cursor+offset position.
5700 * It is directly written into the command buffer block.
5701 */
5702 void
5703cmd_pchar(c, offset)
5704 int c, offset;
5705{
5706 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5707 {
5708 EMSG(_("E198: cmd_pchar beyond the command length"));
5709 return;
5710 }
5711 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5712 ccline.cmdbuff[ccline.cmdlen] = NUL;
5713}
5714
5715 int
5716cmd_gchar(offset)
5717 int offset;
5718{
5719 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5720 {
5721 /* EMSG(_("cmd_gchar beyond the command length")); */
5722 return NUL;
5723 }
5724 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5725}
5726#endif
5727
5728#if defined(FEAT_CMDWIN) || defined(PROTO)
5729/*
5730 * Open a window on the current command line and history. Allow editing in
5731 * the window. Returns when the window is closed.
5732 * Returns:
5733 * CR if the command is to be executed
5734 * Ctrl_C if it is to be abandoned
5735 * K_IGNORE if editing continues
5736 */
5737 static int
5738ex_window()
5739{
5740 struct cmdline_info save_ccline;
5741 buf_T *old_curbuf = curbuf;
5742 win_T *old_curwin = curwin;
5743 buf_T *bp;
5744 win_T *wp;
5745 int i;
5746 linenr_T lnum;
5747 int histtype;
5748 garray_T winsizes;
5749 char_u typestr[2];
5750 int save_restart_edit = restart_edit;
5751 int save_State = State;
5752 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005753#ifdef FEAT_RIGHTLEFT
5754 int save_cmdmsg_rl = cmdmsg_rl;
5755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756
5757 /* Can't do this recursively. Can't do it when typing a password. */
5758 if (cmdwin_type != 0
5759# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5760 || cmdline_star > 0
5761# endif
5762 )
5763 {
5764 beep_flush();
5765 return K_IGNORE;
5766 }
5767
5768 /* Save current window sizes. */
5769 win_size_save(&winsizes);
5770
5771# ifdef FEAT_AUTOCMD
5772 /* Don't execute autocommands while creating the window. */
5773 ++autocmd_block;
5774# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005775 /* don't use a new tab page */
5776 cmdmod.tab = 0;
5777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 /* Create a window for the command-line buffer. */
5779 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5780 {
5781 beep_flush();
5782 return K_IGNORE;
5783 }
5784 cmdwin_type = ccline.cmdfirstc;
5785 if (cmdwin_type == NUL)
5786 cmdwin_type = '-';
5787
5788 /* Create the command-line buffer empty. */
5789 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5790 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5791 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5792 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5793 curbuf->b_p_ma = TRUE;
5794# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005795 curwin->w_p_rl = cmdmsg_rl;
5796 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797# endif
5798# ifdef FEAT_SCROLLBIND
5799 curwin->w_p_scb = FALSE;
5800# endif
5801
5802# ifdef FEAT_AUTOCMD
5803 /* Do execute autocommands for setting the filetype (load syntax). */
5804 --autocmd_block;
5805# endif
5806
Bram Moolenaar46152342005-09-07 21:18:43 +00005807 /* Showing the prompt may have set need_wait_return, reset it. */
5808 need_wait_return = FALSE;
5809
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 histtype = hist_char2type(ccline.cmdfirstc);
5811 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5812 {
5813 if (p_wc == TAB)
5814 {
5815 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5816 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5817 }
5818 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5819 }
5820
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005821 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5822 * sets 'textwidth' to 78). */
5823 curbuf->b_p_tw = 0;
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 /* Fill the buffer with the history. */
5826 init_history();
5827 if (hislen > 0)
5828 {
5829 i = hisidx[histtype];
5830 if (i >= 0)
5831 {
5832 lnum = 0;
5833 do
5834 {
5835 if (++i == hislen)
5836 i = 0;
5837 if (history[histtype][i].hisstr != NULL)
5838 ml_append(lnum++, history[histtype][i].hisstr,
5839 (colnr_T)0, FALSE);
5840 }
5841 while (i != hisidx[histtype]);
5842 }
5843 }
5844
5845 /* Replace the empty last line with the current command-line and put the
5846 * cursor there. */
5847 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5848 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5849 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005850 changed_line_abv_curs();
5851 invalidate_botline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 redraw_later(NOT_VALID);
5853
5854 /* Save the command line info, can be used recursively. */
5855 save_ccline = ccline;
5856 ccline.cmdbuff = NULL;
5857 ccline.cmdprompt = NULL;
5858
5859 /* No Ex mode here! */
5860 exmode_active = 0;
5861
5862 State = NORMAL;
5863# ifdef FEAT_MOUSE
5864 setmouse();
5865# endif
5866
5867# ifdef FEAT_AUTOCMD
5868 /* Trigger CmdwinEnter autocommands. */
5869 typestr[0] = cmdwin_type;
5870 typestr[1] = NUL;
5871 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5872# endif
5873
5874 i = RedrawingDisabled;
5875 RedrawingDisabled = 0;
5876
5877 /*
5878 * Call the main loop until <CR> or CTRL-C is typed.
5879 */
5880 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005881 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
5883 RedrawingDisabled = i;
5884
5885# ifdef FEAT_AUTOCMD
5886 /* Trigger CmdwinLeave autocommands. */
5887 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5888# endif
5889
5890 /* Restore the comand line info. */
5891 ccline = save_ccline;
5892 cmdwin_type = 0;
5893
5894 exmode_active = save_exmode;
5895
5896 /* Safety check: The old window or buffer was deleted: It's a a bug when
5897 * this happens! */
5898 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5899 {
5900 cmdwin_result = Ctrl_C;
5901 EMSG(_("E199: Active window or buffer deleted"));
5902 }
5903 else
5904 {
5905# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5906 /* autocmds may abort script processing */
5907 if (aborting() && cmdwin_result != K_IGNORE)
5908 cmdwin_result = Ctrl_C;
5909# endif
5910 /* Set the new command line from the cmdline buffer. */
5911 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00005912 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 {
Bram Moolenaar46152342005-09-07 21:18:43 +00005914 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
5915
5916 if (histtype == HIST_CMD)
5917 {
5918 /* Execute the command directly. */
5919 ccline.cmdbuff = vim_strsave((char_u *)p);
5920 cmdwin_result = CAR;
5921 }
5922 else
5923 {
5924 /* First need to cancel what we were doing. */
5925 ccline.cmdbuff = NULL;
5926 stuffcharReadbuff(':');
5927 stuffReadbuff((char_u *)p);
5928 stuffcharReadbuff(CAR);
5929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
5931 else if (cmdwin_result == K_XF2) /* :qa typed */
5932 {
5933 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5934 cmdwin_result = CAR;
5935 }
5936 else
5937 ccline.cmdbuff = vim_strsave(ml_get_curline());
5938 if (ccline.cmdbuff == NULL)
5939 cmdwin_result = Ctrl_C;
5940 else
5941 {
5942 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5943 ccline.cmdbufflen = ccline.cmdlen + 1;
5944 ccline.cmdpos = curwin->w_cursor.col;
5945 if (ccline.cmdpos > ccline.cmdlen)
5946 ccline.cmdpos = ccline.cmdlen;
5947 if (cmdwin_result == K_IGNORE)
5948 {
5949 set_cmdspos_cursor();
5950 redrawcmd();
5951 }
5952 }
5953
5954# ifdef FEAT_AUTOCMD
5955 /* Don't execute autocommands while deleting the window. */
5956 ++autocmd_block;
5957# endif
5958 wp = curwin;
5959 bp = curbuf;
5960 win_goto(old_curwin);
5961 win_close(wp, TRUE);
5962 close_buffer(NULL, bp, DOBUF_WIPE);
5963
5964 /* Restore window sizes. */
5965 win_size_restore(&winsizes);
5966
5967# ifdef FEAT_AUTOCMD
5968 --autocmd_block;
5969# endif
5970 }
5971
5972 ga_clear(&winsizes);
5973 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00005974# ifdef FEAT_RIGHTLEFT
5975 cmdmsg_rl = save_cmdmsg_rl;
5976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
5978 State = save_State;
5979# ifdef FEAT_MOUSE
5980 setmouse();
5981# endif
5982
5983 return cmdwin_result;
5984}
5985#endif /* FEAT_CMDWIN */
5986
5987/*
5988 * Used for commands that either take a simple command string argument, or:
5989 * cmd << endmarker
5990 * {script}
5991 * endmarker
5992 * Returns a pointer to allocated memory with {script} or NULL.
5993 */
5994 char_u *
5995script_get(eap, cmd)
5996 exarg_T *eap;
5997 char_u *cmd;
5998{
5999 char_u *theline;
6000 char *end_pattern = NULL;
6001 char dot[] = ".";
6002 garray_T ga;
6003
6004 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6005 return NULL;
6006
6007 ga_init2(&ga, 1, 0x400);
6008
6009 if (cmd[2] != NUL)
6010 end_pattern = (char *)skipwhite(cmd + 2);
6011 else
6012 end_pattern = dot;
6013
6014 for (;;)
6015 {
6016 theline = eap->getline(
6017#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006018 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019#endif
6020 NUL, eap->cookie, 0);
6021
6022 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6023 break;
6024
6025 ga_concat(&ga, theline);
6026 ga_append(&ga, '\n');
6027 vim_free(theline);
6028 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006029 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030
6031 return (char_u *)ga.ga_data;
6032}