blob: e59eb1b29787bb82386d97cba24a7fe14235b528 [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 */
Bram Moolenaar93db9752006-11-21 10:29:45 +000037 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000038# 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 Moolenaar1769d5a2006-10-17 14:25:24 +000089static int cmdline_paste __ARGS((int regname, int literally, int remcr));
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 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000327 redir_off = TRUE; /* Don't redirect the typed command.
328 Repeated, because a ":redir" inside
329 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef USE_ON_FLY_SCROLL
331 dont_scroll = FALSE; /* allow scrolling here */
332#endif
333 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
334
335 cursorcmd(); /* set the cursor on the right spot */
336 c = safe_vgetc();
337 if (KeyTyped)
338 {
339 some_key_typed = TRUE;
340#ifdef FEAT_RIGHTLEFT
341 if (cmd_hkmap)
342 c = hkmap(c);
343# ifdef FEAT_FKMAP
344 if (cmd_fkmap)
345 c = cmdl_fkmap(c);
346# endif
347 if (cmdmsg_rl && !KeyStuffed)
348 {
349 /* Invert horizontal movements and operations. Only when
350 * typed by the user directly, not when the result of a
351 * mapping. */
352 switch (c)
353 {
354 case K_RIGHT: c = K_LEFT; break;
355 case K_S_RIGHT: c = K_S_LEFT; break;
356 case K_C_RIGHT: c = K_C_LEFT; break;
357 case K_LEFT: c = K_RIGHT; break;
358 case K_S_LEFT: c = K_S_RIGHT; break;
359 case K_C_LEFT: c = K_C_RIGHT; break;
360 }
361 }
362#endif
363 }
364
365 /*
366 * Ignore got_int when CTRL-C was typed here.
367 * Don't ignore it in :global, we really need to break then, e.g., for
368 * ":g/pat/normal /pat" (without the <CR>).
369 * Don't ignore it for the input() function.
370 */
371 if ((c == Ctrl_C
372#ifdef UNIX
373 || c == intr_char
374#endif
375 )
376#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
377 && firstc != '@'
378#endif
379#ifdef FEAT_EVAL
380 && !break_ctrl_c
381#endif
382 && !global_busy)
383 got_int = FALSE;
384
385#ifdef FEAT_CMDHIST
386 /* free old command line when finished moving around in the history
387 * list */
388 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000389 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000390 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 && c != K_PAGEDOWN && c != K_PAGEUP
392 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000393 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
395 {
396 vim_free(lookfor);
397 lookfor = NULL;
398 }
399#endif
400
401 /*
402 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
403 */
404 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
405 c = Ctrl_P;
406
407#ifdef FEAT_WILDMENU
408 /* Special translations for 'wildmenu' */
409 if (did_wild_list && p_wmnu)
410 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000411 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000413 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 c = Ctrl_N;
415 }
416 /* Hitting CR after "emenu Name.": complete submenu */
417 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
418 && ccline.cmdpos > 1
419 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
420 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
421 && (c == '\n' || c == '\r' || c == K_KENTER))
422 c = K_DOWN;
423#endif
424
425 /* free expanded names when finished walking through matches */
426 if (xpc.xp_numfiles != -1
427 && !(c == p_wc && KeyTyped) && c != p_wcm
428 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
429 && c != Ctrl_L)
430 {
431 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
432 did_wild_list = FALSE;
433#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000434 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435#endif
436 xpc.xp_context = EXPAND_NOTHING;
437 wim_index = 0;
438#ifdef FEAT_WILDMENU
439 if (p_wmnu && wild_menu_showing != 0)
440 {
441 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000442 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000443
444 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000445 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447 if (wild_menu_showing == WM_SCROLLED)
448 {
449 /* Entered command line, move it up */
450 cmdline_row--;
451 redrawcmd();
452 }
453 else if (save_p_ls != -1)
454 {
455 /* restore 'laststatus' and 'winminheight' */
456 p_ls = save_p_ls;
457 p_wmh = save_p_wmh;
458 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000459 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000461 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 redrawcmd();
463 save_p_ls = -1;
464 }
465 else
466 {
467# ifdef FEAT_VERTSPLIT
468 win_redraw_last_status(topframe);
469# else
470 lastwin->w_redr_status = TRUE;
471# endif
472 redraw_statuslines();
473 }
474 KeyTyped = skt;
475 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000476 if (ccline.input_fn)
477 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 }
479#endif
480 }
481
482#ifdef FEAT_WILDMENU
483 /* Special translations for 'wildmenu' */
484 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
485 {
486 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000487 if (c == K_DOWN && ccline.cmdpos > 0
488 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000490 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 {
492 /* Hitting <Up>: Remove one submenu name in front of the
493 * cursor */
494 int found = FALSE;
495
496 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
497 i = 0;
498 while (--j > 0)
499 {
500 /* check for start of menu name */
501 if (ccline.cmdbuff[j] == ' '
502 && ccline.cmdbuff[j - 1] != '\\')
503 {
504 i = j + 1;
505 break;
506 }
507 /* check for start of submenu name */
508 if (ccline.cmdbuff[j] == '.'
509 && ccline.cmdbuff[j - 1] != '\\')
510 {
511 if (found)
512 {
513 i = j + 1;
514 break;
515 }
516 else
517 found = TRUE;
518 }
519 }
520 if (i > 0)
521 cmdline_del(i);
522 c = p_wc;
523 xpc.xp_context = EXPAND_NOTHING;
524 }
525 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000526 if ((xpc.xp_context == EXPAND_FILES
527 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
529 char_u upseg[5];
530
531 upseg[0] = PATHSEP;
532 upseg[1] = '.';
533 upseg[2] = '.';
534 upseg[3] = PATHSEP;
535 upseg[4] = NUL;
536
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000537 if (c == K_DOWN
538 && ccline.cmdpos > 0
539 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
540 && (ccline.cmdpos < 3
541 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
543 {
544 /* go down a directory */
545 c = p_wc;
546 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000547 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 {
549 /* If in a direct ancestor, strip off one ../ to go down */
550 int found = FALSE;
551
552 j = ccline.cmdpos;
553 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
554 while (--j > i)
555 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000556#ifdef FEAT_MBYTE
557 if (has_mbyte)
558 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 if (vim_ispathsep(ccline.cmdbuff[j]))
561 {
562 found = TRUE;
563 break;
564 }
565 }
566 if (found
567 && ccline.cmdbuff[j - 1] == '.'
568 && ccline.cmdbuff[j - 2] == '.'
569 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
570 {
571 cmdline_del(j - 2);
572 c = p_wc;
573 }
574 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000575 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 {
577 /* go up a directory */
578 int found = FALSE;
579
580 j = ccline.cmdpos - 1;
581 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
582 while (--j > i)
583 {
584#ifdef FEAT_MBYTE
585 if (has_mbyte)
586 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
587#endif
588 if (vim_ispathsep(ccline.cmdbuff[j])
589#ifdef BACKSLASH_IN_FILENAME
590 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
591 == NULL
592#endif
593 )
594 {
595 if (found)
596 {
597 i = j + 1;
598 break;
599 }
600 else
601 found = TRUE;
602 }
603 }
604
605 if (!found)
606 j = i;
607 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
608 j += 4;
609 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
610 && j == i)
611 j += 3;
612 else
613 j = 0;
614 if (j > 0)
615 {
616 /* TODO this is only for DOS/UNIX systems - need to put in
617 * machine-specific stuff here and in upseg init */
618 cmdline_del(j);
619 put_on_cmdline(upseg + 1, 3, FALSE);
620 }
621 else if (ccline.cmdpos > i)
622 cmdline_del(i);
623 c = p_wc;
624 }
625 }
626#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
627 if (p_wmnu
628 && (xpc.xp_context == EXPAND_FILES
629 || xpc.xp_context == EXPAND_MENUNAMES)
630 && (c == K_UP || c == K_DOWN))
631 xpc.xp_context = EXPAND_NOTHING;
632#endif
633
634#endif /* FEAT_WILDMENU */
635
636 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
637 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
638 if (c == Ctrl_BSL)
639 {
640 ++no_mapping;
641 ++allow_keys;
642 c = safe_vgetc();
643 --no_mapping;
644 --allow_keys;
645 /* CTRL-\ e doesn't work when obtaining an expression. */
646 if (c != Ctrl_N && c != Ctrl_G
647 && (c != 'e' || ccline.cmdfirstc == '='))
648 {
649 vungetc(c);
650 c = Ctrl_BSL;
651 }
652#ifdef FEAT_EVAL
653 else if (c == 'e')
654 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000655 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
657 /*
658 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000659 * Need to save and restore the current command line, to be
660 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 */
662 if (ccline.cmdpos == ccline.cmdlen)
663 new_cmdpos = 99999; /* keep it at the end */
664 else
665 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000666
667 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000669 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 if (c == '=')
671 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000672 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000673 * to avoid nasty things like going to another buffer when
674 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000675 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000676 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000678 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000679 restore_cmdline(&save_ccline);
680
681 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000683 ccline.cmdlen = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 STRCPY(ccline.cmdbuff, p);
685 vim_free(p);
686
687 /* Restore the cursor or use the position set with
688 * set_cmdline_pos(). */
689 if (new_cmdpos > ccline.cmdlen)
690 ccline.cmdpos = ccline.cmdlen;
691 else
692 ccline.cmdpos = new_cmdpos;
693
694 KeyTyped = FALSE; /* Don't do p_wc completion. */
695 redrawcmd();
696 goto cmdline_changed;
697 }
698 }
699 beep_flush();
700 c = ESC;
701 }
702#endif
703 else
704 {
705 if (c == Ctrl_G && p_im && restart_edit == 0)
706 restart_edit = 'a';
707 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
708 in history */
709 goto returncmd; /* back to Normal mode */
710 }
711 }
712
713#ifdef FEAT_CMDWIN
714 if (c == cedit_key || c == K_CMDWIN)
715 {
716 /*
717 * Open a window to edit the command line (and history).
718 */
719 c = ex_window();
720 some_key_typed = TRUE;
721 }
722# ifdef FEAT_DIGRAPHS
723 else
724# endif
725#endif
726#ifdef FEAT_DIGRAPHS
727 c = do_digraph(c);
728#endif
729
730 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
731 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
732 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000733 /* In Ex mode a backslash escapes a newline. */
734 if (exmode_active
735 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000736 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000737 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000738 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000740 if (c == K_KENTER)
741 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000743 else
744 {
745 gotesc = FALSE; /* Might have typed ESC previously, don't
746 truncate the cmdline now. */
747 if (ccheck_abbr(c + ABBR_OFF))
748 goto cmdline_changed;
749 if (!cmd_silent)
750 {
751 windgoto(msg_row, 0);
752 out_flush();
753 }
754 break;
755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 }
757
758 /*
759 * Completion for 'wildchar' or 'wildcharm' key.
760 * - hitting <ESC> twice means: abandon command line.
761 * - wildcard expansion is only done when the 'wildchar' key is really
762 * typed, not when it comes from a macro
763 */
764 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
765 {
766 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
767 {
768 /* if 'wildmode' contains "list" may still need to list */
769 if (xpc.xp_numfiles > 1
770 && !did_wild_list
771 && (wim_flags[wim_index] & WIM_LIST))
772 {
773 (void)showmatches(&xpc, FALSE);
774 redrawcmd();
775 did_wild_list = TRUE;
776 }
777 if (wim_flags[wim_index] & WIM_LONGEST)
778 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
779 else if (wim_flags[wim_index] & WIM_FULL)
780 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
781 else
782 res = OK; /* don't insert 'wildchar' now */
783 }
784 else /* typed p_wc first time */
785 {
786 wim_index = 0;
787 j = ccline.cmdpos;
788 /* if 'wildmode' first contains "longest", get longest
789 * common part */
790 if (wim_flags[0] & WIM_LONGEST)
791 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
792 else
793 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
794
795 /* if interrupted while completing, behave like it failed */
796 if (got_int)
797 {
798 (void)vpeekc(); /* remove <C-C> from input stream */
799 got_int = FALSE; /* don't abandon the command line */
800 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
801#ifdef FEAT_WILDMENU
802 xpc.xp_context = EXPAND_NOTHING;
803#endif
804 goto cmdline_changed;
805 }
806
807 /* when more than one match, and 'wildmode' first contains
808 * "list", or no change and 'wildmode' contains "longest,list",
809 * list all matches */
810 if (res == OK && xpc.xp_numfiles > 1)
811 {
812 /* a "longest" that didn't do anything is skipped (but not
813 * "list:longest") */
814 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
815 wim_index = 1;
816 if ((wim_flags[wim_index] & WIM_LIST)
817#ifdef FEAT_WILDMENU
818 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
819#endif
820 )
821 {
822 if (!(wim_flags[0] & WIM_LONGEST))
823 {
824#ifdef FEAT_WILDMENU
825 int p_wmnu_save = p_wmnu;
826 p_wmnu = 0;
827#endif
828 nextwild(&xpc, WILD_PREV, 0); /* remove match */
829#ifdef FEAT_WILDMENU
830 p_wmnu = p_wmnu_save;
831#endif
832 }
833#ifdef FEAT_WILDMENU
834 (void)showmatches(&xpc, p_wmnu
835 && ((wim_flags[wim_index] & WIM_LIST) == 0));
836#else
837 (void)showmatches(&xpc, FALSE);
838#endif
839 redrawcmd();
840 did_wild_list = TRUE;
841 if (wim_flags[wim_index] & WIM_LONGEST)
842 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
843 else if (wim_flags[wim_index] & WIM_FULL)
844 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
845 }
846 else
847 vim_beep();
848 }
849#ifdef FEAT_WILDMENU
850 else if (xpc.xp_numfiles == -1)
851 xpc.xp_context = EXPAND_NOTHING;
852#endif
853 }
854 if (wim_index < 3)
855 ++wim_index;
856 if (c == ESC)
857 gotesc = TRUE;
858 if (res == OK)
859 goto cmdline_changed;
860 }
861
862 gotesc = FALSE;
863
864 /* <S-Tab> goes to last match, in a clumsy way */
865 if (c == K_S_TAB && KeyTyped)
866 {
867 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
868 && nextwild(&xpc, WILD_PREV, 0) == OK
869 && nextwild(&xpc, WILD_PREV, 0) == OK)
870 goto cmdline_changed;
871 }
872
873 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
874 c = NL;
875
876 do_abbr = TRUE; /* default: check for abbreviation */
877
878 /*
879 * Big switch for a typed command line character.
880 */
881 switch (c)
882 {
883 case K_BS:
884 case Ctrl_H:
885 case K_DEL:
886 case K_KDEL:
887 case Ctrl_W:
888#ifdef FEAT_FKMAP
889 if (cmd_fkmap && c == K_BS)
890 c = K_DEL;
891#endif
892 if (c == K_KDEL)
893 c = K_DEL;
894
895 /*
896 * delete current character is the same as backspace on next
897 * character, except at end of line
898 */
899 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
900 ++ccline.cmdpos;
901#ifdef FEAT_MBYTE
902 if (has_mbyte && c == K_DEL)
903 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
904 ccline.cmdbuff + ccline.cmdpos);
905#endif
906 if (ccline.cmdpos > 0)
907 {
908 char_u *p;
909
910 j = ccline.cmdpos;
911 p = ccline.cmdbuff + j;
912#ifdef FEAT_MBYTE
913 if (has_mbyte)
914 {
915 p = mb_prevptr(ccline.cmdbuff, p);
916 if (c == Ctrl_W)
917 {
918 while (p > ccline.cmdbuff && vim_isspace(*p))
919 p = mb_prevptr(ccline.cmdbuff, p);
920 i = mb_get_class(p);
921 while (p > ccline.cmdbuff && mb_get_class(p) == i)
922 p = mb_prevptr(ccline.cmdbuff, p);
923 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000924 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 }
926 }
927 else
928#endif
929 if (c == Ctrl_W)
930 {
931 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
932 --p;
933 i = vim_iswordc(p[-1]);
934 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
935 && vim_iswordc(p[-1]) == i)
936 --p;
937 }
938 else
939 --p;
940 ccline.cmdpos = (int)(p - ccline.cmdbuff);
941 ccline.cmdlen -= j - ccline.cmdpos;
942 i = ccline.cmdpos;
943 while (i < ccline.cmdlen)
944 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
945
946 /* Truncate at the end, required for multi-byte chars. */
947 ccline.cmdbuff[ccline.cmdlen] = NUL;
948 redrawcmd();
949 }
950 else if (ccline.cmdlen == 0 && c != Ctrl_W
951 && ccline.cmdprompt == NULL && indent == 0)
952 {
953 /* In ex and debug mode it doesn't make sense to return. */
954 if (exmode_active
955#ifdef FEAT_EVAL
956 || ccline.cmdfirstc == '>'
957#endif
958 )
959 goto cmdline_not_changed;
960
961 vim_free(ccline.cmdbuff); /* no commandline to return */
962 ccline.cmdbuff = NULL;
963 if (!cmd_silent)
964 {
965#ifdef FEAT_RIGHTLEFT
966 if (cmdmsg_rl)
967 msg_col = Columns;
968 else
969#endif
970 msg_col = 0;
971 msg_putchar(' '); /* delete ':' */
972 }
973 redraw_cmdline = TRUE;
974 goto returncmd; /* back to cmd mode */
975 }
976 goto cmdline_changed;
977
978 case K_INS:
979 case K_KINS:
980#ifdef FEAT_FKMAP
981 /* if Farsi mode set, we are in reverse insert mode -
982 Do not change the mode */
983 if (cmd_fkmap)
984 beep_flush();
985 else
986#endif
987 ccline.overstrike = !ccline.overstrike;
988#ifdef CURSOR_SHAPE
989 ui_cursor_shape(); /* may show different cursor shape */
990#endif
991 goto cmdline_not_changed;
992
993 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000994 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 {
996 /* ":lmap" mappings exists, toggle use of mappings. */
997 State ^= LANGMAP;
998#ifdef USE_IM_CONTROL
999 im_set_active(FALSE); /* Disable input method */
1000#endif
1001 if (b_im_ptr != NULL)
1002 {
1003 if (State & LANGMAP)
1004 *b_im_ptr = B_IMODE_LMAP;
1005 else
1006 *b_im_ptr = B_IMODE_NONE;
1007 }
1008 }
1009#ifdef USE_IM_CONTROL
1010 else
1011 {
1012 /* There are no ":lmap" mappings, toggle IM. When
1013 * 'imdisable' is set don't try getting the status, it's
1014 * always off. */
1015 if ((p_imdisable && b_im_ptr != NULL)
1016 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1017 {
1018 im_set_active(FALSE); /* Disable input method */
1019 if (b_im_ptr != NULL)
1020 *b_im_ptr = B_IMODE_NONE;
1021 }
1022 else
1023 {
1024 im_set_active(TRUE); /* Enable input method */
1025 if (b_im_ptr != NULL)
1026 *b_im_ptr = B_IMODE_IM;
1027 }
1028 }
1029#endif
1030 if (b_im_ptr != NULL)
1031 {
1032 if (b_im_ptr == &curbuf->b_p_iminsert)
1033 set_iminsert_global();
1034 else
1035 set_imsearch_global();
1036 }
1037#ifdef CURSOR_SHAPE
1038 ui_cursor_shape(); /* may show different cursor shape */
1039#endif
1040#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1041 /* Show/unshow value of 'keymap' in status lines later. */
1042 status_redraw_curbuf();
1043#endif
1044 goto cmdline_not_changed;
1045
1046/* case '@': only in very old vi */
1047 case Ctrl_U:
1048 /* delete all characters left of the cursor */
1049 j = ccline.cmdpos;
1050 ccline.cmdlen -= j;
1051 i = ccline.cmdpos = 0;
1052 while (i < ccline.cmdlen)
1053 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1054 /* Truncate at the end, required for multi-byte chars. */
1055 ccline.cmdbuff[ccline.cmdlen] = NUL;
1056 redrawcmd();
1057 goto cmdline_changed;
1058
1059#ifdef FEAT_CLIPBOARD
1060 case Ctrl_Y:
1061 /* Copy the modeless selection, if there is one. */
1062 if (clip_star.state != SELECT_CLEARED)
1063 {
1064 if (clip_star.state == SELECT_DONE)
1065 clip_copy_modeless_selection(TRUE);
1066 goto cmdline_not_changed;
1067 }
1068 break;
1069#endif
1070
1071 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1072 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001073 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001074 * ":normal" runs out of characters. */
1075 if (exmode_active
1076#ifdef FEAT_EX_EXTRA
1077 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1078#endif
1079 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 goto cmdline_not_changed;
1081
1082 gotesc = TRUE; /* will free ccline.cmdbuff after
1083 putting it in history */
1084 goto returncmd; /* back to cmd mode */
1085
1086 case Ctrl_R: /* insert register */
1087#ifdef USE_ON_FLY_SCROLL
1088 dont_scroll = TRUE; /* disallow scrolling here */
1089#endif
1090 putcmdline('"', TRUE);
1091 ++no_mapping;
1092 i = c = safe_vgetc(); /* CTRL-R <char> */
1093 if (i == Ctrl_O)
1094 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1095 if (i == Ctrl_R)
1096 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1097 --no_mapping;
1098#ifdef FEAT_EVAL
1099 /*
1100 * Insert the result of an expression.
1101 * Need to save the current command line, to be able to enter
1102 * a new one...
1103 */
1104 new_cmdpos = -1;
1105 if (c == '=')
1106 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1108 {
1109 beep_flush();
1110 c = ESC;
1111 }
1112 else
1113 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001114 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001116 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 }
1118 }
1119#endif
1120 if (c != ESC) /* use ESC to cancel inserting register */
1121 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001122 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001123
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001124#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001125 /* When there was a serious error abort getting the
1126 * command line. */
1127 if (aborting())
1128 {
1129 gotesc = TRUE; /* will free ccline.cmdbuff after
1130 putting it in history */
1131 goto returncmd; /* back to cmd mode */
1132 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 KeyTyped = FALSE; /* Don't do p_wc completion. */
1135#ifdef FEAT_EVAL
1136 if (new_cmdpos >= 0)
1137 {
1138 /* set_cmdline_pos() was used */
1139 if (new_cmdpos > ccline.cmdlen)
1140 ccline.cmdpos = ccline.cmdlen;
1141 else
1142 ccline.cmdpos = new_cmdpos;
1143 }
1144#endif
1145 }
1146 redrawcmd();
1147 goto cmdline_changed;
1148
1149 case Ctrl_D:
1150 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1151 break; /* Use ^D as normal char instead */
1152
1153 redrawcmd();
1154 continue; /* don't do incremental search now */
1155
1156 case K_RIGHT:
1157 case K_S_RIGHT:
1158 case K_C_RIGHT:
1159 do
1160 {
1161 if (ccline.cmdpos >= ccline.cmdlen)
1162 break;
1163 i = cmdline_charsize(ccline.cmdpos);
1164 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1165 break;
1166 ccline.cmdspos += i;
1167#ifdef FEAT_MBYTE
1168 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001169 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 + ccline.cmdpos);
1171 else
1172#endif
1173 ++ccline.cmdpos;
1174 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001175 while ((c == K_S_RIGHT || c == K_C_RIGHT
1176 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1178#ifdef FEAT_MBYTE
1179 if (has_mbyte)
1180 set_cmdspos_cursor();
1181#endif
1182 goto cmdline_not_changed;
1183
1184 case K_LEFT:
1185 case K_S_LEFT:
1186 case K_C_LEFT:
1187 do
1188 {
1189 if (ccline.cmdpos == 0)
1190 break;
1191 --ccline.cmdpos;
1192#ifdef FEAT_MBYTE
1193 if (has_mbyte) /* move to first byte of char */
1194 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1195 ccline.cmdbuff + ccline.cmdpos);
1196#endif
1197 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1198 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001199 while ((c == K_S_LEFT || c == K_C_LEFT
1200 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1202#ifdef FEAT_MBYTE
1203 if (has_mbyte)
1204 set_cmdspos_cursor();
1205#endif
1206 goto cmdline_not_changed;
1207
1208 case K_IGNORE:
1209 goto cmdline_not_changed; /* Ignore mouse */
1210
Bram Moolenaar4770d092006-01-12 23:22:24 +00001211#ifdef FEAT_GUI_W32
1212 /* On Win32 ignore <M-F4>, we get it when closing the window was
1213 * cancelled. */
1214 case K_F4:
1215 if (mod_mask == MOD_MASK_ALT)
1216 {
1217 redrawcmd(); /* somehow the cmdline is cleared */
1218 goto cmdline_not_changed;
1219 }
1220 break;
1221#endif
1222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223#ifdef FEAT_MOUSE
1224 case K_MIDDLEDRAG:
1225 case K_MIDDLERELEASE:
1226 goto cmdline_not_changed; /* Ignore mouse */
1227
1228 case K_MIDDLEMOUSE:
1229# ifdef FEAT_GUI
1230 /* When GUI is active, also paste when 'mouse' is empty */
1231 if (!gui.in_use)
1232# endif
1233 if (!mouse_has(MOUSE_COMMAND))
1234 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001235# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001237 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001239# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001240 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 redrawcmd();
1242 goto cmdline_changed;
1243
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001244# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001246 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 redrawcmd();
1248 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001249# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251 case K_LEFTDRAG:
1252 case K_LEFTRELEASE:
1253 case K_RIGHTDRAG:
1254 case K_RIGHTRELEASE:
1255 /* Ignore drag and release events when the button-down wasn't
1256 * seen before. */
1257 if (ignore_drag_release)
1258 goto cmdline_not_changed;
1259 /* FALLTHROUGH */
1260 case K_LEFTMOUSE:
1261 case K_RIGHTMOUSE:
1262 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1263 ignore_drag_release = TRUE;
1264 else
1265 ignore_drag_release = FALSE;
1266# ifdef FEAT_GUI
1267 /* When GUI is active, also move when 'mouse' is empty */
1268 if (!gui.in_use)
1269# endif
1270 if (!mouse_has(MOUSE_COMMAND))
1271 goto cmdline_not_changed; /* Ignore mouse */
1272# ifdef FEAT_CLIPBOARD
1273 if (mouse_row < cmdline_row && clip_star.available)
1274 {
1275 int button, is_click, is_drag;
1276
1277 /*
1278 * Handle modeless selection.
1279 */
1280 button = get_mouse_button(KEY2TERMCAP1(c),
1281 &is_click, &is_drag);
1282 if (mouse_model_popup() && button == MOUSE_LEFT
1283 && (mod_mask & MOD_MASK_SHIFT))
1284 {
1285 /* Translate shift-left to right button. */
1286 button = MOUSE_RIGHT;
1287 mod_mask &= ~MOD_MASK_SHIFT;
1288 }
1289 clip_modeless(button, is_click, is_drag);
1290 goto cmdline_not_changed;
1291 }
1292# endif
1293
1294 set_cmdspos();
1295 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1296 ++ccline.cmdpos)
1297 {
1298 i = cmdline_charsize(ccline.cmdpos);
1299 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1300 && mouse_col < ccline.cmdspos % Columns + i)
1301 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001302# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (has_mbyte)
1304 {
1305 /* Count ">" for double-wide char that doesn't fit. */
1306 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001307 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 + ccline.cmdpos) - 1;
1309 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 ccline.cmdspos += i;
1312 }
1313 goto cmdline_not_changed;
1314
1315 /* Mouse scroll wheel: ignored here */
1316 case K_MOUSEDOWN:
1317 case K_MOUSEUP:
1318 /* Alternate buttons ignored here */
1319 case K_X1MOUSE:
1320 case K_X1DRAG:
1321 case K_X1RELEASE:
1322 case K_X2MOUSE:
1323 case K_X2DRAG:
1324 case K_X2RELEASE:
1325 goto cmdline_not_changed;
1326
1327#endif /* FEAT_MOUSE */
1328
1329#ifdef FEAT_GUI
1330 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1331 case K_LEFTRELEASE_NM:
1332 goto cmdline_not_changed;
1333
1334 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001335 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 {
1337 gui_do_scroll();
1338 redrawcmd();
1339 }
1340 goto cmdline_not_changed;
1341
1342 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001343 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 {
1345 gui_do_horiz_scroll();
1346 redrawcmd();
1347 }
1348 goto cmdline_not_changed;
1349#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001350#ifdef FEAT_GUI_TABLINE
1351 case K_TABLINE:
1352 case K_TABMENU:
1353 /* Don't want to change any tabs here. Make sure the same tab
1354 * is still selected. */
1355 if (gui_use_tabline())
1356 gui_mch_set_curtab(tabpage_index(curtab));
1357 goto cmdline_not_changed;
1358#endif
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 case K_SELECT: /* end of Select mode mapping - ignore */
1361 goto cmdline_not_changed;
1362
1363 case Ctrl_B: /* begin of command line */
1364 case K_HOME:
1365 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 case K_S_HOME:
1367 case K_C_HOME:
1368 ccline.cmdpos = 0;
1369 set_cmdspos();
1370 goto cmdline_not_changed;
1371
1372 case Ctrl_E: /* end of command line */
1373 case K_END:
1374 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 case K_S_END:
1376 case K_C_END:
1377 ccline.cmdpos = ccline.cmdlen;
1378 set_cmdspos_cursor();
1379 goto cmdline_not_changed;
1380
1381 case Ctrl_A: /* all matches */
1382 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1383 break;
1384 goto cmdline_changed;
1385
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001386 case Ctrl_L:
1387#ifdef FEAT_SEARCH_EXTRA
1388 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1389 {
1390 /* Add a character from under the cursor for 'incsearch' */
1391 if (did_incsearch
1392 && !equalpos(curwin->w_cursor, old_cursor))
1393 {
1394 c = gchar_cursor();
1395 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001396 {
1397 if (c == firstc || vim_strchr((char_u *)(
1398 p_magic ? "\\^$.*[" : "\\^$"), c)
1399 != NULL)
1400 {
1401 /* put a backslash before special characters */
1402 stuffcharReadbuff(c);
1403 c = '\\';
1404 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001405 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001406 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001407 }
1408 goto cmdline_not_changed;
1409 }
1410#endif
1411
1412 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1414 break;
1415 goto cmdline_changed;
1416
1417 case Ctrl_N: /* next match */
1418 case Ctrl_P: /* previous match */
1419 if (xpc.xp_numfiles > 0)
1420 {
1421 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1422 == FAIL)
1423 break;
1424 goto cmdline_changed;
1425 }
1426
1427#ifdef FEAT_CMDHIST
1428 case K_UP:
1429 case K_DOWN:
1430 case K_S_UP:
1431 case K_S_DOWN:
1432 case K_PAGEUP:
1433 case K_KPAGEUP:
1434 case K_PAGEDOWN:
1435 case K_KPAGEDOWN:
1436 if (hislen == 0 || firstc == NUL) /* no history */
1437 goto cmdline_not_changed;
1438
1439 i = hiscnt;
1440
1441 /* save current command string so it can be restored later */
1442 if (lookfor == NULL)
1443 {
1444 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1445 goto cmdline_not_changed;
1446 lookfor[ccline.cmdpos] = NUL;
1447 }
1448
1449 j = (int)STRLEN(lookfor);
1450 for (;;)
1451 {
1452 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001453 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001454 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 {
1456 if (hiscnt == hislen) /* first time */
1457 hiscnt = hisidx[histype];
1458 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1459 hiscnt = hislen - 1;
1460 else if (hiscnt != hisidx[histype] + 1)
1461 --hiscnt;
1462 else /* at top of list */
1463 {
1464 hiscnt = i;
1465 break;
1466 }
1467 }
1468 else /* one step forwards */
1469 {
1470 /* on last entry, clear the line */
1471 if (hiscnt == hisidx[histype])
1472 {
1473 hiscnt = hislen;
1474 break;
1475 }
1476
1477 /* not on a history line, nothing to do */
1478 if (hiscnt == hislen)
1479 break;
1480 if (hiscnt == hislen - 1) /* wrap around */
1481 hiscnt = 0;
1482 else
1483 ++hiscnt;
1484 }
1485 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1486 {
1487 hiscnt = i;
1488 break;
1489 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001490 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001491 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 || STRNCMP(history[histype][hiscnt].hisstr,
1493 lookfor, (size_t)j) == 0)
1494 break;
1495 }
1496
1497 if (hiscnt != i) /* jumped to other entry */
1498 {
1499 char_u *p;
1500 int len;
1501 int old_firstc;
1502
1503 vim_free(ccline.cmdbuff);
1504 if (hiscnt == hislen)
1505 p = lookfor; /* back to the old one */
1506 else
1507 p = history[histype][hiscnt].hisstr;
1508
1509 if (histype == HIST_SEARCH
1510 && p != lookfor
1511 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1512 {
1513 /* Correct for the separator character used when
1514 * adding the history entry vs the one used now.
1515 * First loop: count length.
1516 * Second loop: copy the characters. */
1517 for (i = 0; i <= 1; ++i)
1518 {
1519 len = 0;
1520 for (j = 0; p[j] != NUL; ++j)
1521 {
1522 /* Replace old sep with new sep, unless it is
1523 * escaped. */
1524 if (p[j] == old_firstc
1525 && (j == 0 || p[j - 1] != '\\'))
1526 {
1527 if (i > 0)
1528 ccline.cmdbuff[len] = firstc;
1529 }
1530 else
1531 {
1532 /* Escape new sep, unless it is already
1533 * escaped. */
1534 if (p[j] == firstc
1535 && (j == 0 || p[j - 1] != '\\'))
1536 {
1537 if (i > 0)
1538 ccline.cmdbuff[len] = '\\';
1539 ++len;
1540 }
1541 if (i > 0)
1542 ccline.cmdbuff[len] = p[j];
1543 }
1544 ++len;
1545 }
1546 if (i == 0)
1547 {
1548 alloc_cmdbuff(len);
1549 if (ccline.cmdbuff == NULL)
1550 goto returncmd;
1551 }
1552 }
1553 ccline.cmdbuff[len] = NUL;
1554 }
1555 else
1556 {
1557 alloc_cmdbuff((int)STRLEN(p));
1558 if (ccline.cmdbuff == NULL)
1559 goto returncmd;
1560 STRCPY(ccline.cmdbuff, p);
1561 }
1562
1563 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1564 redrawcmd();
1565 goto cmdline_changed;
1566 }
1567 beep_flush();
1568 goto cmdline_not_changed;
1569#endif
1570
1571 case Ctrl_V:
1572 case Ctrl_Q:
1573#ifdef FEAT_MOUSE
1574 ignore_drag_release = TRUE;
1575#endif
1576 putcmdline('^', TRUE);
1577 c = get_literal(); /* get next (two) character(s) */
1578 do_abbr = FALSE; /* don't do abbreviation now */
1579#ifdef FEAT_MBYTE
1580 /* may need to remove ^ when composing char was typed */
1581 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1582 {
1583 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1584 msg_putchar(' ');
1585 cursorcmd();
1586 }
1587#endif
1588 break;
1589
1590#ifdef FEAT_DIGRAPHS
1591 case Ctrl_K:
1592#ifdef FEAT_MOUSE
1593 ignore_drag_release = TRUE;
1594#endif
1595 putcmdline('?', TRUE);
1596#ifdef USE_ON_FLY_SCROLL
1597 dont_scroll = TRUE; /* disallow scrolling here */
1598#endif
1599 c = get_digraph(TRUE);
1600 if (c != NUL)
1601 break;
1602
1603 redrawcmd();
1604 goto cmdline_not_changed;
1605#endif /* FEAT_DIGRAPHS */
1606
1607#ifdef FEAT_RIGHTLEFT
1608 case Ctrl__: /* CTRL-_: switch language mode */
1609 if (!p_ari)
1610 break;
1611#ifdef FEAT_FKMAP
1612 if (p_altkeymap)
1613 {
1614 cmd_fkmap = !cmd_fkmap;
1615 if (cmd_fkmap) /* in Farsi always in Insert mode */
1616 ccline.overstrike = FALSE;
1617 }
1618 else /* Hebrew is default */
1619#endif
1620 cmd_hkmap = !cmd_hkmap;
1621 goto cmdline_not_changed;
1622#endif
1623
1624 default:
1625#ifdef UNIX
1626 if (c == intr_char)
1627 {
1628 gotesc = TRUE; /* will free ccline.cmdbuff after
1629 putting it in history */
1630 goto returncmd; /* back to Normal mode */
1631 }
1632#endif
1633 /*
1634 * Normal character with no special meaning. Just set mod_mask
1635 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1636 * the string <S-Space>. This should only happen after ^V.
1637 */
1638 if (!IS_SPECIAL(c))
1639 mod_mask = 0x0;
1640 break;
1641 }
1642 /*
1643 * End of switch on command line character.
1644 * We come here if we have a normal character.
1645 */
1646
1647 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1648#ifdef FEAT_MBYTE
1649 /* Add ABBR_OFF for characters above 0x100, this is
1650 * what check_abbr() expects. */
1651 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1652#endif
1653 c))
1654 goto cmdline_changed;
1655
1656 /*
1657 * put the character in the command line
1658 */
1659 if (IS_SPECIAL(c) || mod_mask != 0)
1660 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1661 else
1662 {
1663#ifdef FEAT_MBYTE
1664 if (has_mbyte)
1665 {
1666 j = (*mb_char2bytes)(c, IObuff);
1667 IObuff[j] = NUL; /* exclude composing chars */
1668 put_on_cmdline(IObuff, j, TRUE);
1669 }
1670 else
1671#endif
1672 {
1673 IObuff[0] = c;
1674 put_on_cmdline(IObuff, 1, TRUE);
1675 }
1676 }
1677 goto cmdline_changed;
1678
1679/*
1680 * This part implements incremental searches for "/" and "?"
1681 * Jump to cmdline_not_changed when a character has been read but the command
1682 * line did not change. Then we only search and redraw if something changed in
1683 * the past.
1684 * Jump to cmdline_changed when the command line did change.
1685 * (Sorry for the goto's, I know it is ugly).
1686 */
1687cmdline_not_changed:
1688#ifdef FEAT_SEARCH_EXTRA
1689 if (!incsearch_postponed)
1690 continue;
1691#endif
1692
1693cmdline_changed:
1694#ifdef FEAT_SEARCH_EXTRA
1695 /*
1696 * 'incsearch' highlighting.
1697 */
1698 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1699 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001700 pos_T end_pos;
1701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 /* if there is a character waiting, search and redraw later */
1703 if (char_avail())
1704 {
1705 incsearch_postponed = TRUE;
1706 continue;
1707 }
1708 incsearch_postponed = FALSE;
1709 curwin->w_cursor = old_cursor; /* start at old position */
1710
1711 /* If there is no command line, don't do anything */
1712 if (ccline.cmdlen == 0)
1713 i = 0;
1714 else
1715 {
1716 cursor_off(); /* so the user knows we're busy */
1717 out_flush();
1718 ++emsg_off; /* So it doesn't beep if bad expr */
1719 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1720 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1721 --emsg_off;
1722 /* if interrupted while searching, behave like it failed */
1723 if (got_int)
1724 {
1725 (void)vpeekc(); /* remove <C-C> from input stream */
1726 got_int = FALSE; /* don't abandon the command line */
1727 i = 0;
1728 }
1729 else if (char_avail())
1730 /* cancelled searching because a char was typed */
1731 incsearch_postponed = TRUE;
1732 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001733 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 highlight_match = TRUE; /* highlight position */
1735 else
1736 highlight_match = FALSE; /* remove highlight */
1737
1738 /* first restore the old curwin values, so the screen is
1739 * positioned in the same way as the actual search command */
1740 curwin->w_leftcol = old_leftcol;
1741 curwin->w_topline = old_topline;
1742# ifdef FEAT_DIFF
1743 curwin->w_topfill = old_topfill;
1744# endif
1745 curwin->w_botline = old_botline;
1746 changed_cline_bef_curs();
1747 update_topline();
1748
1749 if (i != 0)
1750 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001751 pos_T save_pos = curwin->w_cursor;
1752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001754 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 * moves the whole match onto the screen when 'nowrap' is set.
1756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 curwin->w_cursor.lnum += search_match_lines;
1758 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001759 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1760 {
1761 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1762 coladvance((colnr_T)MAXCOL);
1763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001765 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001766 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001768 else
1769 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001772# ifdef FEAT_WINDOWS
1773 /* May redraw the status line to show the cursor position. */
1774 if (p_ru && curwin->w_status_height > 0)
1775 curwin->w_redr_status = TRUE;
1776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001778 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001779 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001780 restore_cmdline(&save_ccline);
1781
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001782 /* Leave it at the end to make CTRL-R CTRL-W work. */
1783 if (i != 0)
1784 curwin->w_cursor = end_pos;
1785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 msg_starthere();
1787 redrawcmdline();
1788 did_incsearch = TRUE;
1789 }
1790#else /* FEAT_SEARCH_EXTRA */
1791 ;
1792#endif
1793
1794#ifdef FEAT_RIGHTLEFT
1795 if (cmdmsg_rl
1796# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001797 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798# endif
1799 )
1800 /* Always redraw the whole command line to fix shaping and
1801 * right-left typing. Not efficient, but it works. */
1802 redrawcmd();
1803#endif
1804 }
1805
1806returncmd:
1807
1808#ifdef FEAT_RIGHTLEFT
1809 cmdmsg_rl = FALSE;
1810#endif
1811
1812#ifdef FEAT_FKMAP
1813 cmd_fkmap = 0;
1814#endif
1815
1816 ExpandCleanup(&xpc);
1817
1818#ifdef FEAT_SEARCH_EXTRA
1819 if (did_incsearch)
1820 {
1821 curwin->w_cursor = old_cursor;
1822 curwin->w_curswant = old_curswant;
1823 curwin->w_leftcol = old_leftcol;
1824 curwin->w_topline = old_topline;
1825# ifdef FEAT_DIFF
1826 curwin->w_topfill = old_topfill;
1827# endif
1828 curwin->w_botline = old_botline;
1829 highlight_match = FALSE;
1830 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001831 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833#endif
1834
1835 if (ccline.cmdbuff != NULL)
1836 {
1837 /*
1838 * Put line in history buffer (":" and "=" only when it was typed).
1839 */
1840#ifdef FEAT_CMDHIST
1841 if (ccline.cmdlen && firstc != NUL
1842 && (some_key_typed || histype == HIST_SEARCH))
1843 {
1844 add_to_history(histype, ccline.cmdbuff, TRUE,
1845 histype == HIST_SEARCH ? firstc : NUL);
1846 if (firstc == ':')
1847 {
1848 vim_free(new_last_cmdline);
1849 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1850 }
1851 }
1852#endif
1853
1854 if (gotesc) /* abandon command line */
1855 {
1856 vim_free(ccline.cmdbuff);
1857 ccline.cmdbuff = NULL;
1858 if (msg_scrolled == 0)
1859 compute_cmdrow();
1860 MSG("");
1861 redraw_cmdline = TRUE;
1862 }
1863 }
1864
1865 /*
1866 * If the screen was shifted up, redraw the whole screen (later).
1867 * If the line is too long, clear it, so ruler and shown command do
1868 * not get printed in the middle of it.
1869 */
1870 msg_check();
1871 msg_scroll = save_msg_scroll;
1872 redir_off = FALSE;
1873
1874 /* When the command line was typed, no need for a wait-return prompt. */
1875 if (some_key_typed)
1876 need_wait_return = FALSE;
1877
1878 State = save_State;
1879#ifdef USE_IM_CONTROL
1880 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1881 im_save_status(b_im_ptr);
1882 im_set_active(FALSE);
1883#endif
1884#ifdef FEAT_MOUSE
1885 setmouse();
1886#endif
1887#ifdef CURSOR_SHAPE
1888 ui_cursor_shape(); /* may show different cursor shape */
1889#endif
1890
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001891 {
1892 char_u *p = ccline.cmdbuff;
1893
1894 /* Make ccline empty, getcmdline() may try to use it. */
1895 ccline.cmdbuff = NULL;
1896 return p;
1897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898}
1899
1900#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1901/*
1902 * Get a command line with a prompt.
1903 * This is prepared to be called recursively from getcmdline() (e.g. by
1904 * f_input() when evaluating an expression from CTRL-R =).
1905 * Returns the command line in allocated memory, or NULL.
1906 */
1907 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001908getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 int firstc;
1910 char_u *prompt; /* command line prompt */
1911 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001912 int xp_context; /* type of expansion */
1913 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914{
1915 char_u *s;
1916 struct cmdline_info save_ccline;
1917 int msg_col_save = msg_col;
1918
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001919 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 ccline.cmdprompt = prompt;
1921 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001922# ifdef FEAT_EVAL
1923 ccline.xp_context = xp_context;
1924 ccline.xp_arg = xp_arg;
1925 ccline.input_fn = (firstc == '@');
1926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001928 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 /* Restore msg_col, the prompt from input() may have changed it. */
1930 msg_col = msg_col_save;
1931
1932 return s;
1933}
1934#endif
1935
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001936/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001937 * Return TRUE when the text must not be changed and we can't switch to
1938 * another window or buffer. Used when editing the command line, evaluating
1939 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001940 */
1941 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001942text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001943{
1944#ifdef FEAT_CMDWIN
1945 if (cmdwin_type != 0)
1946 return TRUE;
1947#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001948 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001949}
1950
1951/*
1952 * Give an error message for a command that isn't allowed while the cmdline
1953 * window is open or editing the cmdline in another way.
1954 */
1955 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001956text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001957{
1958#ifdef FEAT_CMDWIN
1959 if (cmdwin_type != 0)
1960 EMSG(_(e_cmdwin));
1961 else
1962#endif
1963 EMSG(_(e_secure));
1964}
1965
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001966#if defined(FEAT_AUTOCMD) || defined(PROTO)
1967/*
1968 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
1969 * message.
1970 */
1971 int
1972curbuf_locked()
1973{
1974 if (curbuf_lock > 0)
1975 {
1976 EMSG(_("E788: Not allowed to edit another buffer now"));
1977 return TRUE;
1978 }
1979 return FALSE;
1980}
1981#endif
1982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 static int
1984cmdline_charsize(idx)
1985 int idx;
1986{
1987#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1988 if (cmdline_star > 0) /* showing '*', always 1 position */
1989 return 1;
1990#endif
1991 return ptr2cells(ccline.cmdbuff + idx);
1992}
1993
1994/*
1995 * Compute the offset of the cursor on the command line for the prompt and
1996 * indent.
1997 */
1998 static void
1999set_cmdspos()
2000{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002001 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 ccline.cmdspos = 1 + ccline.cmdindent;
2003 else
2004 ccline.cmdspos = 0 + ccline.cmdindent;
2005}
2006
2007/*
2008 * Compute the screen position for the cursor on the command line.
2009 */
2010 static void
2011set_cmdspos_cursor()
2012{
2013 int i, m, c;
2014
2015 set_cmdspos();
2016 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002019 if (m < 0) /* overflow, Columns or Rows at weird value */
2020 m = MAXCOL;
2021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 else
2023 m = MAXCOL;
2024 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2025 {
2026 c = cmdline_charsize(i);
2027#ifdef FEAT_MBYTE
2028 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2029 if (has_mbyte)
2030 correct_cmdspos(i, c);
2031#endif
2032 /* If the cmdline doesn't fit, put cursor on last visible char. */
2033 if ((ccline.cmdspos += c) >= m)
2034 {
2035 ccline.cmdpos = i - 1;
2036 ccline.cmdspos -= c;
2037 break;
2038 }
2039#ifdef FEAT_MBYTE
2040 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002041 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042#endif
2043 }
2044}
2045
2046#ifdef FEAT_MBYTE
2047/*
2048 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2049 * character that doesn't fit, so that a ">" must be displayed.
2050 */
2051 static void
2052correct_cmdspos(idx, cells)
2053 int idx;
2054 int cells;
2055{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002056 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2058 && ccline.cmdspos % Columns + cells > Columns)
2059 ccline.cmdspos++;
2060}
2061#endif
2062
2063/*
2064 * Get an Ex command line for the ":" command.
2065 */
2066/* ARGSUSED */
2067 char_u *
2068getexline(c, dummy, indent)
2069 int c; /* normally ':', NUL for ":append" */
2070 void *dummy; /* cookie not used */
2071 int indent; /* indent for inside conditionals */
2072{
2073 /* When executing a register, remove ':' that's in front of each line. */
2074 if (exec_from_reg && vpeekc() == ':')
2075 (void)vgetc();
2076 return getcmdline(c, 1L, indent);
2077}
2078
2079/*
2080 * Get an Ex command line for Ex mode.
2081 * In Ex mode we only use the OS supplied line editing features and no
2082 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002083 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 */
2085/* ARGSUSED */
2086 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002087getexmodeline(promptc, dummy, indent)
2088 int promptc; /* normally ':', NUL for ":append" and '?' for
2089 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 void *dummy; /* cookie not used */
2091 int indent; /* indent for inside conditionals */
2092{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002093 garray_T line_ga;
2094 char_u *pend;
2095 int startcol = 0;
2096 int c1;
2097 int escaped = FALSE; /* CTRL-V typed */
2098 int vcol = 0;
2099 char_u *p;
2100 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
2102 /* Switch cursor on now. This avoids that it happens after the "\n", which
2103 * confuses the system function that computes tabstops. */
2104 cursor_on();
2105
2106 /* always start in column 0; write a newline if necessary */
2107 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002108 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002112 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002113 if (p_prompt)
2114 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 while (indent-- > 0)
2116 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 }
2119
2120 ga_init2(&line_ga, 1, 30);
2121
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002122 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002123 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002124 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002125 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002126 while (indent >= 8)
2127 {
2128 ga_append(&line_ga, TAB);
2129 msg_puts((char_u *)" ");
2130 indent -= 8;
2131 }
2132 while (indent-- > 0)
2133 {
2134 ga_append(&line_ga, ' ');
2135 msg_putchar(' ');
2136 }
2137 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002138 ++no_mapping;
2139 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002140
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 /*
2142 * Get the line, one character at a time.
2143 */
2144 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002145 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
2147 if (ga_grow(&line_ga, 40) == FAIL)
2148 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002149 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002151 /* Get one character at a time. Don't use inchar(), it can't handle
2152 * special characters. */
2153 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
2155 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002156 * Handle line editing.
2157 * Previously this was left to the system, putting the terminal in
2158 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002160 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002162 msg_putchar('\n');
2163 break;
2164 }
2165
2166 if (!escaped)
2167 {
2168 /* CR typed means "enter", which is NL */
2169 if (c1 == '\r')
2170 c1 = '\n';
2171
2172 if (c1 == BS || c1 == K_BS
2173 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002175 if (line_ga.ga_len > 0)
2176 {
2177 --line_ga.ga_len;
2178 goto redraw;
2179 }
2180 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 }
2182
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002183 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 msg_col = startcol;
2186 msg_clr_eos();
2187 line_ga.ga_len = 0;
2188 continue;
2189 }
2190
2191 if (c1 == Ctrl_T)
2192 {
2193 p = (char_u *)line_ga.ga_data;
2194 p[line_ga.ga_len] = NUL;
2195 indent = get_indent_str(p, 8);
2196 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2197add_indent:
2198 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002200 char_u *s = skipwhite(p);
2201
2202 ga_grow(&line_ga, 1);
2203 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2204 *s = ' ';
2205 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002207redraw:
2208 /* redraw the line */
2209 msg_col = startcol;
2210 windgoto(msg_row, msg_col);
2211 vcol = 0;
2212 for (p = (char_u *)line_ga.ga_data;
2213 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002217 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219 msg_putchar(' ');
2220 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002222 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002224 msg_outtrans_len(p, 1);
2225 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
2227 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002228 msg_clr_eos();
2229 continue;
2230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232 if (c1 == Ctrl_D)
2233 {
2234 /* Delete one shiftwidth. */
2235 p = (char_u *)line_ga.ga_data;
2236 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002238 if (prev_char == '^')
2239 ex_keep_indent = TRUE;
2240 indent = 0;
2241 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243 else
2244 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002245 p[line_ga.ga_len] = NUL;
2246 indent = get_indent_str(p, 8);
2247 --indent;
2248 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002250 while (get_indent_str(p, 8) > indent)
2251 {
2252 char_u *s = skipwhite(p);
2253
2254 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2255 --line_ga.ga_len;
2256 }
2257 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002259
2260 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2261 {
2262 escaped = TRUE;
2263 continue;
2264 }
2265
2266 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2267 if (IS_SPECIAL(c1))
2268 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002270
2271 if (IS_SPECIAL(c1))
2272 c1 = '?';
2273 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2274 prev_char = c1;
2275 if (c1 == '\n')
2276 msg_putchar('\n');
2277 else if (c1 == TAB)
2278 {
2279 /* Don't use chartabsize(), 'ts' can be different */
2280 do
2281 {
2282 msg_putchar(' ');
2283 } while (++vcol % 8);
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002287 msg_outtrans_len(
2288 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2289 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002291 ++line_ga.ga_len;
2292 escaped = FALSE;
2293
2294 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002295 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002296
2297 /* we are done when a NL is entered, but not when it comes after a
2298 * backslash */
2299 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2300 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002303 --pend;
2304 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 }
2307 }
2308
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002309 --no_mapping;
2310 --allow_keys;
2311
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 /* make following messages go to the next line */
2313 msg_didout = FALSE;
2314 msg_col = 0;
2315 if (msg_row < Rows - 1)
2316 ++msg_row;
2317 emsg_on_display = FALSE; /* don't want ui_delay() */
2318
2319 if (got_int)
2320 ga_clear(&line_ga);
2321
2322 return (char_u *)line_ga.ga_data;
2323}
2324
Bram Moolenaare344bea2005-09-01 20:46:49 +00002325# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2326 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327/*
2328 * Return TRUE if ccline.overstrike is on.
2329 */
2330 int
2331cmdline_overstrike()
2332{
2333 return ccline.overstrike;
2334}
2335
2336/*
2337 * Return TRUE if the cursor is at the end of the cmdline.
2338 */
2339 int
2340cmdline_at_end()
2341{
2342 return (ccline.cmdpos >= ccline.cmdlen);
2343}
2344#endif
2345
Bram Moolenaar9372a112005-12-06 19:59:18 +00002346#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347/*
2348 * Return the virtual column number at the current cursor position.
2349 * This is used by the IM code to obtain the start of the preedit string.
2350 */
2351 colnr_T
2352cmdline_getvcol_cursor()
2353{
2354 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2355 return MAXCOL;
2356
2357# ifdef FEAT_MBYTE
2358 if (has_mbyte)
2359 {
2360 colnr_T col;
2361 int i = 0;
2362
2363 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002364 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366 return col;
2367 }
2368 else
2369# endif
2370 return ccline.cmdpos;
2371}
2372#endif
2373
2374#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2375/*
2376 * If part of the command line is an IM preedit string, redraw it with
2377 * IM feedback attributes. The cursor position is restored after drawing.
2378 */
2379 static void
2380redrawcmd_preedit()
2381{
2382 if ((State & CMDLINE)
2383 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002384 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 && !p_imdisable
2386 && im_is_preediting())
2387 {
2388 int cmdpos = 0;
2389 int cmdspos;
2390 int old_row;
2391 int old_col;
2392 colnr_T col;
2393
2394 old_row = msg_row;
2395 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002396 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398# ifdef FEAT_MBYTE
2399 if (has_mbyte)
2400 {
2401 for (col = 0; col < preedit_start_col
2402 && cmdpos < ccline.cmdlen; ++col)
2403 {
2404 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002405 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407 }
2408 else
2409# endif
2410 {
2411 cmdspos += preedit_start_col;
2412 cmdpos += preedit_start_col;
2413 }
2414
2415 msg_row = cmdline_row + (cmdspos / (int)Columns);
2416 msg_col = cmdspos % (int)Columns;
2417 if (msg_row >= Rows)
2418 msg_row = Rows - 1;
2419
2420 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2421 {
2422 int char_len;
2423 int char_attr;
2424
2425 char_attr = im_get_feedback_attr(col);
2426 if (char_attr < 0)
2427 break; /* end of preedit string */
2428
2429# ifdef FEAT_MBYTE
2430 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002431 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 else
2433# endif
2434 char_len = 1;
2435
2436 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2437 cmdpos += char_len;
2438 }
2439
2440 msg_row = old_row;
2441 msg_col = old_col;
2442 }
2443}
2444#endif /* FEAT_XIM && FEAT_GUI_GTK */
2445
2446/*
2447 * Allocate a new command line buffer.
2448 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2449 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2450 */
2451 static void
2452alloc_cmdbuff(len)
2453 int len;
2454{
2455 /*
2456 * give some extra space to avoid having to allocate all the time
2457 */
2458 if (len < 80)
2459 len = 100;
2460 else
2461 len += 20;
2462
2463 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2464 ccline.cmdbufflen = len;
2465}
2466
2467/*
2468 * Re-allocate the command line to length len + something extra.
2469 * return FAIL for failure, OK otherwise
2470 */
2471 static int
2472realloc_cmdbuff(len)
2473 int len;
2474{
2475 char_u *p;
2476
2477 p = ccline.cmdbuff;
2478 alloc_cmdbuff(len); /* will get some more */
2479 if (ccline.cmdbuff == NULL) /* out of memory */
2480 {
2481 ccline.cmdbuff = p; /* keep the old one */
2482 return FAIL;
2483 }
2484 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2485 vim_free(p);
2486 return OK;
2487}
2488
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002489#if defined(FEAT_ARABIC) || defined(PROTO)
2490static char_u *arshape_buf = NULL;
2491
2492# if defined(EXITFREE) || defined(PROTO)
2493 void
2494free_cmdline_buf()
2495{
2496 vim_free(arshape_buf);
2497}
2498# endif
2499#endif
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501/*
2502 * Draw part of the cmdline at the current cursor position. But draw stars
2503 * when cmdline_star is TRUE.
2504 */
2505 static void
2506draw_cmdline(start, len)
2507 int start;
2508 int len;
2509{
2510#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2511 int i;
2512
2513 if (cmdline_star > 0)
2514 for (i = 0; i < len; ++i)
2515 {
2516 msg_putchar('*');
2517# ifdef FEAT_MBYTE
2518 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002519 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520# endif
2521 }
2522 else
2523#endif
2524#ifdef FEAT_ARABIC
2525 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 static int buflen = 0;
2528 char_u *p;
2529 int j;
2530 int newlen = 0;
2531 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002532 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 int prev_c = 0;
2534 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002535 int u8c;
2536 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
2539 /*
2540 * Do arabic shaping into a temporary buffer. This is very
2541 * inefficient!
2542 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002543 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 /* Re-allocate the buffer. We keep it around to avoid a lot of
2546 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002547 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002548 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002549 arshape_buf = alloc(buflen);
2550 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 return; /* out of memory */
2552 }
2553
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002554 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2555 {
2556 /* Prepend a space to draw the leading composing char on. */
2557 arshape_buf[0] = ' ';
2558 newlen = 1;
2559 }
2560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 for (j = start; j < start + len; j += mb_l)
2562 {
2563 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002564 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002565 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (ARABIC_CHAR(u8c))
2567 {
2568 /* Do Arabic shaping. */
2569 if (cmdmsg_rl)
2570 {
2571 /* displaying from right to left */
2572 pc = prev_c;
2573 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002574 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (j + mb_l >= start + len)
2576 nc = NUL;
2577 else
2578 nc = utf_ptr2char(p + mb_l);
2579 }
2580 else
2581 {
2582 /* displaying from left to right */
2583 if (j + mb_l >= start + len)
2584 pc = NUL;
2585 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002586 {
2587 int pcc[MAX_MCO];
2588
2589 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002591 pc1 = pcc[0];
2592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 nc = prev_c;
2594 }
2595 prev_c = u8c;
2596
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002597 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002599 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002600 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002602 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2603 if (u8cc[1] != 0)
2604 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002605 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
2607 }
2608 else
2609 {
2610 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002611 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 newlen += mb_l;
2613 }
2614 }
2615
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002616 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 }
2618 else
2619#endif
2620 msg_outtrans_len(ccline.cmdbuff + start, len);
2621}
2622
2623/*
2624 * Put a character on the command line. Shifts the following text to the
2625 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2626 * "c" must be printable (fit in one display cell)!
2627 */
2628 void
2629putcmdline(c, shift)
2630 int c;
2631 int shift;
2632{
2633 if (cmd_silent)
2634 return;
2635 msg_no_more = TRUE;
2636 msg_putchar(c);
2637 if (shift)
2638 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2639 msg_no_more = FALSE;
2640 cursorcmd();
2641}
2642
2643/*
2644 * Undo a putcmdline(c, FALSE).
2645 */
2646 void
2647unputcmdline()
2648{
2649 if (cmd_silent)
2650 return;
2651 msg_no_more = TRUE;
2652 if (ccline.cmdlen == ccline.cmdpos)
2653 msg_putchar(' ');
2654 else
2655 draw_cmdline(ccline.cmdpos, 1);
2656 msg_no_more = FALSE;
2657 cursorcmd();
2658}
2659
2660/*
2661 * Put the given string, of the given length, onto the command line.
2662 * If len is -1, then STRLEN() is used to calculate the length.
2663 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2664 * part will be redrawn, otherwise it will not. If this function is called
2665 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2666 * called afterwards.
2667 */
2668 int
2669put_on_cmdline(str, len, redraw)
2670 char_u *str;
2671 int len;
2672 int redraw;
2673{
2674 int retval;
2675 int i;
2676 int m;
2677 int c;
2678
2679 if (len < 0)
2680 len = (int)STRLEN(str);
2681
2682 /* Check if ccline.cmdbuff needs to be longer */
2683 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2684 retval = realloc_cmdbuff(ccline.cmdlen + len);
2685 else
2686 retval = OK;
2687 if (retval == OK)
2688 {
2689 if (!ccline.overstrike)
2690 {
2691 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2692 ccline.cmdbuff + ccline.cmdpos,
2693 (size_t)(ccline.cmdlen - ccline.cmdpos));
2694 ccline.cmdlen += len;
2695 }
2696 else
2697 {
2698#ifdef FEAT_MBYTE
2699 if (has_mbyte)
2700 {
2701 /* Count nr of characters in the new string. */
2702 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002703 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 ++m;
2705 /* Count nr of bytes in cmdline that are overwritten by these
2706 * characters. */
2707 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002708 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 --m;
2710 if (i < ccline.cmdlen)
2711 {
2712 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2713 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2714 ccline.cmdlen += ccline.cmdpos + len - i;
2715 }
2716 else
2717 ccline.cmdlen = ccline.cmdpos + len;
2718 }
2719 else
2720#endif
2721 if (ccline.cmdpos + len > ccline.cmdlen)
2722 ccline.cmdlen = ccline.cmdpos + len;
2723 }
2724 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2725 ccline.cmdbuff[ccline.cmdlen] = NUL;
2726
2727#ifdef FEAT_MBYTE
2728 if (enc_utf8)
2729 {
2730 /* When the inserted text starts with a composing character,
2731 * backup to the character before it. There could be two of them.
2732 */
2733 i = 0;
2734 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2735 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2736 {
2737 i = (*mb_head_off)(ccline.cmdbuff,
2738 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2739 ccline.cmdpos -= i;
2740 len += i;
2741 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2742 }
2743# ifdef FEAT_ARABIC
2744 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2745 {
2746 /* Check the previous character for Arabic combining pair. */
2747 i = (*mb_head_off)(ccline.cmdbuff,
2748 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2749 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2750 + ccline.cmdpos - i), c))
2751 {
2752 ccline.cmdpos -= i;
2753 len += i;
2754 }
2755 else
2756 i = 0;
2757 }
2758# endif
2759 if (i != 0)
2760 {
2761 /* Also backup the cursor position. */
2762 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2763 ccline.cmdspos -= i;
2764 msg_col -= i;
2765 if (msg_col < 0)
2766 {
2767 msg_col += Columns;
2768 --msg_row;
2769 }
2770 }
2771 }
2772#endif
2773
2774 if (redraw && !cmd_silent)
2775 {
2776 msg_no_more = TRUE;
2777 i = cmdline_row;
2778 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2779 /* Avoid clearing the rest of the line too often. */
2780 if (cmdline_row != i || ccline.overstrike)
2781 msg_clr_eos();
2782 msg_no_more = FALSE;
2783 }
2784#ifdef FEAT_FKMAP
2785 /*
2786 * If we are in Farsi command mode, the character input must be in
2787 * Insert mode. So do not advance the cmdpos.
2788 */
2789 if (!cmd_fkmap)
2790#endif
2791 {
2792 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002795 if (m < 0) /* overflow, Columns or Rows at weird value */
2796 m = MAXCOL;
2797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 else
2799 m = MAXCOL;
2800 for (i = 0; i < len; ++i)
2801 {
2802 c = cmdline_charsize(ccline.cmdpos);
2803#ifdef FEAT_MBYTE
2804 /* count ">" for a double-wide char that doesn't fit. */
2805 if (has_mbyte)
2806 correct_cmdspos(ccline.cmdpos, c);
2807#endif
2808 /* Stop cursor at the end of the screen */
2809 if (ccline.cmdspos + c >= m)
2810 break;
2811 ccline.cmdspos += c;
2812#ifdef FEAT_MBYTE
2813 if (has_mbyte)
2814 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002815 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 if (c > len - i - 1)
2817 c = len - i - 1;
2818 ccline.cmdpos += c;
2819 i += c;
2820 }
2821#endif
2822 ++ccline.cmdpos;
2823 }
2824 }
2825 }
2826 if (redraw)
2827 msg_check();
2828 return retval;
2829}
2830
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002831static struct cmdline_info prev_ccline;
2832static int prev_ccline_used = FALSE;
2833
2834/*
2835 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2836 * and overwrite it. But get_cmdline_str() may need it, thus make it
2837 * available globally in prev_ccline.
2838 */
2839 static void
2840save_cmdline(ccp)
2841 struct cmdline_info *ccp;
2842{
2843 if (!prev_ccline_used)
2844 {
2845 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2846 prev_ccline_used = TRUE;
2847 }
2848 *ccp = prev_ccline;
2849 prev_ccline = ccline;
2850 ccline.cmdbuff = NULL;
2851 ccline.cmdprompt = NULL;
2852}
2853
2854/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002855 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002856 */
2857 static void
2858restore_cmdline(ccp)
2859 struct cmdline_info *ccp;
2860{
2861 ccline = prev_ccline;
2862 prev_ccline = *ccp;
2863}
2864
Bram Moolenaar5a305422006-04-28 22:38:25 +00002865#if defined(FEAT_EVAL) || defined(PROTO)
2866/*
2867 * Save the command line into allocated memory. Returns a pointer to be
2868 * passed to restore_cmdline_alloc() later.
2869 * Returns NULL when failed.
2870 */
2871 char_u *
2872save_cmdline_alloc()
2873{
2874 struct cmdline_info *p;
2875
2876 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2877 if (p != NULL)
2878 save_cmdline(p);
2879 return (char_u *)p;
2880}
2881
2882/*
2883 * Restore the command line from the return value of save_cmdline_alloc().
2884 */
2885 void
2886restore_cmdline_alloc(p)
2887 char_u *p;
2888{
2889 if (p != NULL)
2890 {
2891 restore_cmdline((struct cmdline_info *)p);
2892 vim_free(p);
2893 }
2894}
2895#endif
2896
Bram Moolenaar8299df92004-07-10 09:47:34 +00002897/*
2898 * paste a yank register into the command line.
2899 * used by CTRL-R command in command-line mode
2900 * insert_reg() can't be used here, because special characters from the
2901 * register contents will be interpreted as commands.
2902 *
2903 * return FAIL for failure, OK otherwise
2904 */
2905 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002906cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00002907 int regname;
2908 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002909 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00002910{
2911 long i;
2912 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002913 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002914 int allocated;
2915 struct cmdline_info save_ccline;
2916
2917 /* check for valid regname; also accept special characters for CTRL-R in
2918 * the command line */
2919 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2920 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2921 return FAIL;
2922
2923 /* A register containing CTRL-R can cause an endless loop. Allow using
2924 * CTRL-C to break the loop. */
2925 line_breakcheck();
2926 if (got_int)
2927 return FAIL;
2928
2929#ifdef FEAT_CLIPBOARD
2930 regname = may_get_selection(regname);
2931#endif
2932
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002933 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002934 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002935 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002936 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002937 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002938 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002939 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002940
2941 if (i)
2942 {
2943 /* Got the value of a special register in "arg". */
2944 if (arg == NULL)
2945 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002946
2947 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2948 * part of the word. */
2949 p = arg;
2950 if (p_is && regname == Ctrl_W)
2951 {
2952 char_u *w;
2953 int len;
2954
2955 /* Locate start of last word in the cmd buffer. */
2956 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
2957 {
2958#ifdef FEAT_MBYTE
2959 if (has_mbyte)
2960 {
2961 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
2962 if (!vim_iswordc(mb_ptr2char(w - len)))
2963 break;
2964 w -= len;
2965 }
2966 else
2967#endif
2968 {
2969 if (!vim_iswordc(w[-1]))
2970 break;
2971 --w;
2972 }
2973 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002974 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002975 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
2976 p += len;
2977 }
2978
2979 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002980 if (allocated)
2981 vim_free(arg);
2982 return OK;
2983 }
2984
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002985 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002986}
2987
2988/*
2989 * Put a string on the command line.
2990 * When "literally" is TRUE, insert literally.
2991 * When "literally" is FALSE, insert as typed, but don't leave the command
2992 * line.
2993 */
2994 void
2995cmdline_paste_str(s, literally)
2996 char_u *s;
2997 int literally;
2998{
2999 int c, cv;
3000
3001 if (literally)
3002 put_on_cmdline(s, -1, TRUE);
3003 else
3004 while (*s != NUL)
3005 {
3006 cv = *s;
3007 if (cv == Ctrl_V && s[1])
3008 ++s;
3009#ifdef FEAT_MBYTE
3010 if (has_mbyte)
3011 {
3012 c = mb_ptr2char(s);
3013 s += mb_char2len(c);
3014 }
3015 else
3016#endif
3017 c = *s++;
3018 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3019#ifdef UNIX
3020 || c == intr_char
3021#endif
3022 || (c == Ctrl_BSL && *s == Ctrl_N))
3023 stuffcharReadbuff(Ctrl_V);
3024 stuffcharReadbuff(c);
3025 }
3026}
3027
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028#ifdef FEAT_WILDMENU
3029/*
3030 * Delete characters on the command line, from "from" to the current
3031 * position.
3032 */
3033 static void
3034cmdline_del(from)
3035 int from;
3036{
3037 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3038 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3039 ccline.cmdlen -= ccline.cmdpos - from;
3040 ccline.cmdpos = from;
3041}
3042#endif
3043
3044/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003045 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 * search
3047 */
3048 void
3049redrawcmdline()
3050{
3051 if (cmd_silent)
3052 return;
3053 need_wait_return = FALSE;
3054 compute_cmdrow();
3055 redrawcmd();
3056 cursorcmd();
3057}
3058
3059 static void
3060redrawcmdprompt()
3061{
3062 int i;
3063
3064 if (cmd_silent)
3065 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003066 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 msg_putchar(ccline.cmdfirstc);
3068 if (ccline.cmdprompt != NULL)
3069 {
3070 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3071 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3072 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003073 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 --ccline.cmdindent;
3075 }
3076 else
3077 for (i = ccline.cmdindent; i > 0; --i)
3078 msg_putchar(' ');
3079}
3080
3081/*
3082 * Redraw what is currently on the command line.
3083 */
3084 void
3085redrawcmd()
3086{
3087 if (cmd_silent)
3088 return;
3089
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003090 /* when 'incsearch' is set there may be no command line while redrawing */
3091 if (ccline.cmdbuff == NULL)
3092 {
3093 windgoto(cmdline_row, 0);
3094 msg_clr_eos();
3095 return;
3096 }
3097
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 msg_start();
3099 redrawcmdprompt();
3100
3101 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3102 msg_no_more = TRUE;
3103 draw_cmdline(0, ccline.cmdlen);
3104 msg_clr_eos();
3105 msg_no_more = FALSE;
3106
3107 set_cmdspos_cursor();
3108
3109 /*
3110 * An emsg() before may have set msg_scroll. This is used in normal mode,
3111 * in cmdline mode we can reset them now.
3112 */
3113 msg_scroll = FALSE; /* next message overwrites cmdline */
3114
3115 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3116 * in cmdline mode */
3117 skip_redraw = FALSE;
3118}
3119
3120 void
3121compute_cmdrow()
3122{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003123 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 cmdline_row = Rows - 1;
3125 else
3126 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3127 + W_STATUS_HEIGHT(lastwin);
3128}
3129
3130 static void
3131cursorcmd()
3132{
3133 if (cmd_silent)
3134 return;
3135
3136#ifdef FEAT_RIGHTLEFT
3137 if (cmdmsg_rl)
3138 {
3139 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3140 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3141 if (msg_row <= 0)
3142 msg_row = Rows - 1;
3143 }
3144 else
3145#endif
3146 {
3147 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3148 msg_col = ccline.cmdspos % (int)Columns;
3149 if (msg_row >= Rows)
3150 msg_row = Rows - 1;
3151 }
3152
3153 windgoto(msg_row, msg_col);
3154#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3155 redrawcmd_preedit();
3156#endif
3157#ifdef MCH_CURSOR_SHAPE
3158 mch_update_cursor();
3159#endif
3160}
3161
3162 void
3163gotocmdline(clr)
3164 int clr;
3165{
3166 msg_start();
3167#ifdef FEAT_RIGHTLEFT
3168 if (cmdmsg_rl)
3169 msg_col = Columns - 1;
3170 else
3171#endif
3172 msg_col = 0; /* always start in column 0 */
3173 if (clr) /* clear the bottom line(s) */
3174 msg_clr_eos(); /* will reset clear_cmdline */
3175 windgoto(cmdline_row, 0);
3176}
3177
3178/*
3179 * Check the word in front of the cursor for an abbreviation.
3180 * Called when the non-id character "c" has been entered.
3181 * When an abbreviation is recognized it is removed from the text with
3182 * backspaces and the replacement string is inserted, followed by "c".
3183 */
3184 static int
3185ccheck_abbr(c)
3186 int c;
3187{
3188 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3189 return FALSE;
3190
3191 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3192}
3193
3194/*
3195 * Return FAIL if this is not an appropriate context in which to do
3196 * completion of anything, return OK if it is (even if there are no matches).
3197 * For the caller, this means that the character is just passed through like a
3198 * normal character (instead of being expanded). This allows :s/^I^D etc.
3199 */
3200 static int
3201nextwild(xp, type, options)
3202 expand_T *xp;
3203 int type;
3204 int options; /* extra options for ExpandOne() */
3205{
3206 int i, j;
3207 char_u *p1;
3208 char_u *p2;
3209 int oldlen;
3210 int difflen;
3211 int v;
3212
3213 if (xp->xp_numfiles == -1)
3214 {
3215 set_expand_context(xp);
3216 cmd_showtail = expand_showtail(xp);
3217 }
3218
3219 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3220 {
3221 beep_flush();
3222 return OK; /* Something illegal on command line */
3223 }
3224 if (xp->xp_context == EXPAND_NOTHING)
3225 {
3226 /* Caller can use the character as a normal char instead */
3227 return FAIL;
3228 }
3229
3230 MSG_PUTS("..."); /* show that we are busy */
3231 out_flush();
3232
3233 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3234 oldlen = ccline.cmdpos - i;
3235
3236 if (type == WILD_NEXT || type == WILD_PREV)
3237 {
3238 /*
3239 * Get next/previous match for a previous expanded pattern.
3240 */
3241 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3242 }
3243 else
3244 {
3245 /*
3246 * Translate string into pattern and expand it.
3247 */
3248 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3249 p2 = NULL;
3250 else
3251 {
3252 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3253 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3254 |options, type);
3255 vim_free(p1);
3256 /* longest match: make sure it is not shorter (happens with :help */
3257 if (p2 != NULL && type == WILD_LONGEST)
3258 {
3259 for (j = 0; j < oldlen; ++j)
3260 if (ccline.cmdbuff[i + j] == '*'
3261 || ccline.cmdbuff[i + j] == '?')
3262 break;
3263 if ((int)STRLEN(p2) < j)
3264 {
3265 vim_free(p2);
3266 p2 = NULL;
3267 }
3268 }
3269 }
3270 }
3271
3272 if (p2 != NULL && !got_int)
3273 {
3274 difflen = (int)STRLEN(p2) - oldlen;
3275 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3276 {
3277 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3278 xp->xp_pattern = ccline.cmdbuff + i;
3279 }
3280 else
3281 v = OK;
3282 if (v == OK)
3283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003284 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3285 &ccline.cmdbuff[ccline.cmdpos],
3286 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3287 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 ccline.cmdlen += difflen;
3289 ccline.cmdpos += difflen;
3290 }
3291 }
3292 vim_free(p2);
3293
3294 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003295 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
3297 /* When expanding a ":map" command and no matches are found, assume that
3298 * the key is supposed to be inserted literally */
3299 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3300 return FAIL;
3301
3302 if (xp->xp_numfiles <= 0 && p2 == NULL)
3303 beep_flush();
3304 else if (xp->xp_numfiles == 1)
3305 /* free expanded pattern */
3306 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3307
3308 return OK;
3309}
3310
3311/*
3312 * Do wildcard expansion on the string 'str'.
3313 * Chars that should not be expanded must be preceded with a backslash.
3314 * Return a pointer to alloced memory containing the new string.
3315 * Return NULL for failure.
3316 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003317 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3318 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 *
3320 * mode = WILD_FREE: just free previously expanded matches
3321 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3322 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3323 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3324 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3325 * mode = WILD_ALL: return all matches concatenated
3326 * mode = WILD_LONGEST: return longest matched part
3327 *
3328 * options = WILD_LIST_NOTFOUND: list entries without a match
3329 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3330 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3331 * options = WILD_NO_BEEP: Don't beep for multiple matches
3332 * options = WILD_ADD_SLASH: add a slash after directory names
3333 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3334 * options = WILD_SILENT: don't print warning messages
3335 * options = WILD_ESCAPE: put backslash before special chars
3336 *
3337 * The variables xp->xp_context and xp->xp_backslash must have been set!
3338 */
3339 char_u *
3340ExpandOne(xp, str, orig, options, mode)
3341 expand_T *xp;
3342 char_u *str;
3343 char_u *orig; /* allocated copy of original of expanded string */
3344 int options;
3345 int mode;
3346{
3347 char_u *ss = NULL;
3348 static int findex;
3349 static char_u *orig_save = NULL; /* kept value of orig */
3350 int i;
3351 long_u len;
3352 int non_suf_match; /* number without matching suffix */
3353
3354 /*
3355 * first handle the case of using an old match
3356 */
3357 if (mode == WILD_NEXT || mode == WILD_PREV)
3358 {
3359 if (xp->xp_numfiles > 0)
3360 {
3361 if (mode == WILD_PREV)
3362 {
3363 if (findex == -1)
3364 findex = xp->xp_numfiles;
3365 --findex;
3366 }
3367 else /* mode == WILD_NEXT */
3368 ++findex;
3369
3370 /*
3371 * When wrapping around, return the original string, set findex to
3372 * -1.
3373 */
3374 if (findex < 0)
3375 {
3376 if (orig_save == NULL)
3377 findex = xp->xp_numfiles - 1;
3378 else
3379 findex = -1;
3380 }
3381 if (findex >= xp->xp_numfiles)
3382 {
3383 if (orig_save == NULL)
3384 findex = 0;
3385 else
3386 findex = -1;
3387 }
3388#ifdef FEAT_WILDMENU
3389 if (p_wmnu)
3390 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3391 findex, cmd_showtail);
3392#endif
3393 if (findex == -1)
3394 return vim_strsave(orig_save);
3395 return vim_strsave(xp->xp_files[findex]);
3396 }
3397 else
3398 return NULL;
3399 }
3400
3401/* free old names */
3402 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3403 {
3404 FreeWild(xp->xp_numfiles, xp->xp_files);
3405 xp->xp_numfiles = -1;
3406 vim_free(orig_save);
3407 orig_save = NULL;
3408 }
3409 findex = 0;
3410
3411 if (mode == WILD_FREE) /* only release file name */
3412 return NULL;
3413
3414 if (xp->xp_numfiles == -1)
3415 {
3416 vim_free(orig_save);
3417 orig_save = orig;
3418
3419 /*
3420 * Do the expansion.
3421 */
3422 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3423 options) == FAIL)
3424 {
3425#ifdef FNAME_ILLEGAL
3426 /* Illegal file name has been silently skipped. But when there
3427 * are wildcards, the real problem is that there was no match,
3428 * causing the pattern to be added, which has illegal characters.
3429 */
3430 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3431 EMSG2(_(e_nomatch2), str);
3432#endif
3433 }
3434 else if (xp->xp_numfiles == 0)
3435 {
3436 if (!(options & WILD_SILENT))
3437 EMSG2(_(e_nomatch2), str);
3438 }
3439 else
3440 {
3441 /* Escape the matches for use on the command line. */
3442 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3443
3444 /*
3445 * Check for matching suffixes in file names.
3446 */
3447 if (mode != WILD_ALL && mode != WILD_LONGEST)
3448 {
3449 if (xp->xp_numfiles)
3450 non_suf_match = xp->xp_numfiles;
3451 else
3452 non_suf_match = 1;
3453 if ((xp->xp_context == EXPAND_FILES
3454 || xp->xp_context == EXPAND_DIRECTORIES)
3455 && xp->xp_numfiles > 1)
3456 {
3457 /*
3458 * More than one match; check suffix.
3459 * The files will have been sorted on matching suffix in
3460 * expand_wildcards, only need to check the first two.
3461 */
3462 non_suf_match = 0;
3463 for (i = 0; i < 2; ++i)
3464 if (match_suffix(xp->xp_files[i]))
3465 ++non_suf_match;
3466 }
3467 if (non_suf_match != 1)
3468 {
3469 /* Can we ever get here unless it's while expanding
3470 * interactively? If not, we can get rid of this all
3471 * together. Don't really want to wait for this message
3472 * (and possibly have to hit return to continue!).
3473 */
3474 if (!(options & WILD_SILENT))
3475 EMSG(_(e_toomany));
3476 else if (!(options & WILD_NO_BEEP))
3477 beep_flush();
3478 }
3479 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3480 ss = vim_strsave(xp->xp_files[0]);
3481 }
3482 }
3483 }
3484
3485 /* Find longest common part */
3486 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3487 {
3488 for (len = 0; xp->xp_files[0][len]; ++len)
3489 {
3490 for (i = 0; i < xp->xp_numfiles; ++i)
3491 {
3492#ifdef CASE_INSENSITIVE_FILENAME
3493 if (xp->xp_context == EXPAND_DIRECTORIES
3494 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003495 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 || xp->xp_context == EXPAND_BUFFERS)
3497 {
3498 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3499 TOLOWER_LOC(xp->xp_files[0][len]))
3500 break;
3501 }
3502 else
3503#endif
3504 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3505 break;
3506 }
3507 if (i < xp->xp_numfiles)
3508 {
3509 if (!(options & WILD_NO_BEEP))
3510 vim_beep();
3511 break;
3512 }
3513 }
3514 ss = alloc((unsigned)len + 1);
3515 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003516 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 findex = -1; /* next p_wc gets first one */
3518 }
3519
3520 /* Concatenate all matching names */
3521 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3522 {
3523 len = 0;
3524 for (i = 0; i < xp->xp_numfiles; ++i)
3525 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3526 ss = lalloc(len, TRUE);
3527 if (ss != NULL)
3528 {
3529 *ss = NUL;
3530 for (i = 0; i < xp->xp_numfiles; ++i)
3531 {
3532 STRCAT(ss, xp->xp_files[i]);
3533 if (i != xp->xp_numfiles - 1)
3534 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3535 }
3536 }
3537 }
3538
3539 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3540 ExpandCleanup(xp);
3541
3542 return ss;
3543}
3544
3545/*
3546 * Prepare an expand structure for use.
3547 */
3548 void
3549ExpandInit(xp)
3550 expand_T *xp;
3551{
3552 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003553#ifndef BACKSLASH_IN_FILENAME
3554 xp->xp_shell = FALSE;
3555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 xp->xp_numfiles = -1;
3557 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003558#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3559 xp->xp_arg = NULL;
3560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561}
3562
3563/*
3564 * Cleanup an expand structure after use.
3565 */
3566 void
3567ExpandCleanup(xp)
3568 expand_T *xp;
3569{
3570 if (xp->xp_numfiles >= 0)
3571 {
3572 FreeWild(xp->xp_numfiles, xp->xp_files);
3573 xp->xp_numfiles = -1;
3574 }
3575}
3576
3577 void
3578ExpandEscape(xp, str, numfiles, files, options)
3579 expand_T *xp;
3580 char_u *str;
3581 int numfiles;
3582 char_u **files;
3583 int options;
3584{
3585 int i;
3586 char_u *p;
3587
3588 /*
3589 * May change home directory back to "~"
3590 */
3591 if (options & WILD_HOME_REPLACE)
3592 tilde_replace(str, numfiles, files);
3593
3594 if (options & WILD_ESCAPE)
3595 {
3596 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003597 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 || xp->xp_context == EXPAND_BUFFERS
3599 || xp->xp_context == EXPAND_DIRECTORIES)
3600 {
3601 /*
3602 * Insert a backslash into a file name before a space, \, %, #
3603 * and wildmatch characters, except '~'.
3604 */
3605 for (i = 0; i < numfiles; ++i)
3606 {
3607 /* for ":set path=" we need to escape spaces twice */
3608 if (xp->xp_backslash == XP_BS_THREE)
3609 {
3610 p = vim_strsave_escaped(files[i], (char_u *)" ");
3611 if (p != NULL)
3612 {
3613 vim_free(files[i]);
3614 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003615#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 p = vim_strsave_escaped(files[i], (char_u *)" ");
3617 if (p != NULL)
3618 {
3619 vim_free(files[i]);
3620 files[i] = p;
3621 }
3622#endif
3623 }
3624 }
3625#ifdef BACKSLASH_IN_FILENAME
3626 {
3627 char_u buf[20];
3628 int j = 0;
3629
3630 /* Don't escape '[' and '{' if they are in 'isfname'. */
3631 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3632 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3633 buf[j++] = *p;
3634 buf[j] = NUL;
3635 p = vim_strsave_escaped(files[i], buf);
3636 }
3637#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003638 p = vim_strsave_escaped(files[i],
3639 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640#endif
3641 if (p != NULL)
3642 {
3643 vim_free(files[i]);
3644 files[i] = p;
3645 }
3646
3647 /* If 'str' starts with "\~", replace "~" at start of
3648 * files[i] with "\~". */
3649 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003650 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 }
3652 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003653
3654 /* If the first file starts with a '+' escape it. Otherwise it
3655 * could be seen as "+cmd". */
3656 if (*files[0] == '+')
3657 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659 else if (xp->xp_context == EXPAND_TAGS)
3660 {
3661 /*
3662 * Insert a backslash before characters in a tag name that
3663 * would terminate the ":tag" command.
3664 */
3665 for (i = 0; i < numfiles; ++i)
3666 {
3667 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3668 if (p != NULL)
3669 {
3670 vim_free(files[i]);
3671 files[i] = p;
3672 }
3673 }
3674 }
3675 }
3676}
3677
3678/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003679 * Put a backslash before the file name in "pp", which is in allocated memory.
3680 */
3681 static void
3682escape_fname(pp)
3683 char_u **pp;
3684{
3685 char_u *p;
3686
3687 p = alloc((unsigned)(STRLEN(*pp) + 2));
3688 if (p != NULL)
3689 {
3690 p[0] = '\\';
3691 STRCPY(p + 1, *pp);
3692 vim_free(*pp);
3693 *pp = p;
3694 }
3695}
3696
3697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 * For each file name in files[num_files]:
3699 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3700 */
3701 void
3702tilde_replace(orig_pat, num_files, files)
3703 char_u *orig_pat;
3704 int num_files;
3705 char_u **files;
3706{
3707 int i;
3708 char_u *p;
3709
3710 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3711 {
3712 for (i = 0; i < num_files; ++i)
3713 {
3714 p = home_replace_save(NULL, files[i]);
3715 if (p != NULL)
3716 {
3717 vim_free(files[i]);
3718 files[i] = p;
3719 }
3720 }
3721 }
3722}
3723
3724/*
3725 * Show all matches for completion on the command line.
3726 * Returns EXPAND_NOTHING when the character that triggered expansion should
3727 * be inserted like a normal character.
3728 */
3729/*ARGSUSED*/
3730 static int
3731showmatches(xp, wildmenu)
3732 expand_T *xp;
3733 int wildmenu;
3734{
3735#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3736 int num_files;
3737 char_u **files_found;
3738 int i, j, k;
3739 int maxlen;
3740 int lines;
3741 int columns;
3742 char_u *p;
3743 int lastlen;
3744 int attr;
3745 int showtail;
3746
3747 if (xp->xp_numfiles == -1)
3748 {
3749 set_expand_context(xp);
3750 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3751 &num_files, &files_found);
3752 showtail = expand_showtail(xp);
3753 if (i != EXPAND_OK)
3754 return i;
3755
3756 }
3757 else
3758 {
3759 num_files = xp->xp_numfiles;
3760 files_found = xp->xp_files;
3761 showtail = cmd_showtail;
3762 }
3763
3764#ifdef FEAT_WILDMENU
3765 if (!wildmenu)
3766 {
3767#endif
3768 msg_didany = FALSE; /* lines_left will be set */
3769 msg_start(); /* prepare for paging */
3770 msg_putchar('\n');
3771 out_flush();
3772 cmdline_row = msg_row;
3773 msg_didany = FALSE; /* lines_left will be set again */
3774 msg_start(); /* prepare for paging */
3775#ifdef FEAT_WILDMENU
3776 }
3777#endif
3778
3779 if (got_int)
3780 got_int = FALSE; /* only int. the completion, not the cmd line */
3781#ifdef FEAT_WILDMENU
3782 else if (wildmenu)
3783 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3784#endif
3785 else
3786 {
3787 /* find the length of the longest file name */
3788 maxlen = 0;
3789 for (i = 0; i < num_files; ++i)
3790 {
3791 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003792 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 || xp->xp_context == EXPAND_BUFFERS))
3794 {
3795 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3796 j = vim_strsize(NameBuff);
3797 }
3798 else
3799 j = vim_strsize(L_SHOWFILE(i));
3800 if (j > maxlen)
3801 maxlen = j;
3802 }
3803
3804 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3805 lines = num_files;
3806 else
3807 {
3808 /* compute the number of columns and lines for the listing */
3809 maxlen += 2; /* two spaces between file names */
3810 columns = ((int)Columns + 2) / maxlen;
3811 if (columns < 1)
3812 columns = 1;
3813 lines = (num_files + columns - 1) / columns;
3814 }
3815
3816 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3817
3818 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3819 {
3820 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3821 msg_clr_eos();
3822 msg_advance(maxlen - 3);
3823 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3824 }
3825
3826 /* list the files line by line */
3827 for (i = 0; i < lines; ++i)
3828 {
3829 lastlen = 999;
3830 for (k = i; k < num_files; k += lines)
3831 {
3832 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3833 {
3834 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3835 p = files_found[k] + STRLEN(files_found[k]) + 1;
3836 msg_advance(maxlen + 1);
3837 msg_puts(p);
3838 msg_advance(maxlen + 3);
3839 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3840 break;
3841 }
3842 for (j = maxlen - lastlen; --j >= 0; )
3843 msg_putchar(' ');
3844 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003845 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 || xp->xp_context == EXPAND_BUFFERS)
3847 {
3848 /* highlight directories */
3849 j = (mch_isdir(files_found[k]));
3850 if (showtail)
3851 p = L_SHOWFILE(k);
3852 else
3853 {
3854 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3855 TRUE);
3856 p = NameBuff;
3857 }
3858 }
3859 else
3860 {
3861 j = FALSE;
3862 p = L_SHOWFILE(k);
3863 }
3864 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3865 }
3866 if (msg_col > 0) /* when not wrapped around */
3867 {
3868 msg_clr_eos();
3869 msg_putchar('\n');
3870 }
3871 out_flush(); /* show one line at a time */
3872 if (got_int)
3873 {
3874 got_int = FALSE;
3875 break;
3876 }
3877 }
3878
3879 /*
3880 * we redraw the command below the lines that we have just listed
3881 * This is a bit tricky, but it saves a lot of screen updating.
3882 */
3883 cmdline_row = msg_row; /* will put it back later */
3884 }
3885
3886 if (xp->xp_numfiles == -1)
3887 FreeWild(num_files, files_found);
3888
3889 return EXPAND_OK;
3890}
3891
3892/*
3893 * Private gettail for showmatches() (and win_redr_status_matches()):
3894 * Find tail of file name path, but ignore trailing "/".
3895 */
3896 char_u *
3897sm_gettail(s)
3898 char_u *s;
3899{
3900 char_u *p;
3901 char_u *t = s;
3902 int had_sep = FALSE;
3903
3904 for (p = s; *p != NUL; )
3905 {
3906 if (vim_ispathsep(*p)
3907#ifdef BACKSLASH_IN_FILENAME
3908 && !rem_backslash(p)
3909#endif
3910 )
3911 had_sep = TRUE;
3912 else if (had_sep)
3913 {
3914 t = p;
3915 had_sep = FALSE;
3916 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003917 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919 return t;
3920}
3921
3922/*
3923 * Return TRUE if we only need to show the tail of completion matches.
3924 * When not completing file names or there is a wildcard in the path FALSE is
3925 * returned.
3926 */
3927 static int
3928expand_showtail(xp)
3929 expand_T *xp;
3930{
3931 char_u *s;
3932 char_u *end;
3933
3934 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003935 if (xp->xp_context != EXPAND_FILES
3936 && xp->xp_context != EXPAND_SHELLCMD
3937 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 return FALSE;
3939
3940 end = gettail(xp->xp_pattern);
3941 if (end == xp->xp_pattern) /* there is no path separator */
3942 return FALSE;
3943
3944 for (s = xp->xp_pattern; s < end; s++)
3945 {
3946 /* Skip escaped wildcards. Only when the backslash is not a path
3947 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3948 if (rem_backslash(s))
3949 ++s;
3950 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3951 return FALSE;
3952 }
3953 return TRUE;
3954}
3955
3956/*
3957 * Prepare a string for expansion.
3958 * When expanding file names: The string will be used with expand_wildcards().
3959 * Copy the file name into allocated memory and add a '*' at the end.
3960 * When expanding other names: The string will be used with regcomp(). Copy
3961 * the name into allocated memory and prepend "^".
3962 */
3963 char_u *
3964addstar(fname, len, context)
3965 char_u *fname;
3966 int len;
3967 int context; /* EXPAND_FILES etc. */
3968{
3969 char_u *retval;
3970 int i, j;
3971 int new_len;
3972 char_u *tail;
3973
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003974 if (context != EXPAND_FILES
3975 && context != EXPAND_SHELLCMD
3976 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
3978 /*
3979 * Matching will be done internally (on something other than files).
3980 * So we convert the file-matching-type wildcards into our kind for
3981 * use with vim_regcomp(). First work out how long it will be:
3982 */
3983
3984 /* For help tags the translation is done in find_help_tags().
3985 * For a tag pattern starting with "/" no translation is needed. */
3986 if (context == EXPAND_HELP
3987 || context == EXPAND_COLORS
3988 || context == EXPAND_COMPILER
3989 || (context == EXPAND_TAGS && fname[0] == '/'))
3990 retval = vim_strnsave(fname, len);
3991 else
3992 {
3993 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3994 for (i = 0; i < len; i++)
3995 {
3996 if (fname[i] == '*' || fname[i] == '~')
3997 new_len++; /* '*' needs to be replaced by ".*"
3998 '~' needs to be replaced by "\~" */
3999
4000 /* Buffer names are like file names. "." should be literal */
4001 if (context == EXPAND_BUFFERS && fname[i] == '.')
4002 new_len++; /* "." becomes "\." */
4003
4004 /* Custom expansion takes care of special things, match
4005 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004006 if ((context == EXPAND_USER_DEFINED
4007 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 new_len++; /* '\' becomes "\\" */
4009 }
4010 retval = alloc(new_len);
4011 if (retval != NULL)
4012 {
4013 retval[0] = '^';
4014 j = 1;
4015 for (i = 0; i < len; i++, j++)
4016 {
4017 /* Skip backslash. But why? At least keep it for custom
4018 * expansion. */
4019 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004020 && context != EXPAND_USER_LIST
4021 && fname[i] == '\\'
4022 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 break;
4024
4025 switch (fname[i])
4026 {
4027 case '*': retval[j++] = '.';
4028 break;
4029 case '~': retval[j++] = '\\';
4030 break;
4031 case '?': retval[j] = '.';
4032 continue;
4033 case '.': if (context == EXPAND_BUFFERS)
4034 retval[j++] = '\\';
4035 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004036 case '\\': if (context == EXPAND_USER_DEFINED
4037 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 retval[j++] = '\\';
4039 break;
4040 }
4041 retval[j] = fname[i];
4042 }
4043 retval[j] = NUL;
4044 }
4045 }
4046 }
4047 else
4048 {
4049 retval = alloc(len + 4);
4050 if (retval != NULL)
4051 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004052 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004055 * Don't add a star to *, ~, ~user, $var or `cmd`.
4056 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 * ~ would be at the start of the file name, but not the tail.
4058 * $ could be anywhere in the tail.
4059 * ` could be anywhere in the file name.
4060 */
4061 tail = gettail(retval);
4062 if ((*retval != '~' || tail != retval)
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004063 && (len == 0 || retval[len - 1] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 && vim_strchr(tail, '$') == NULL
4065 && vim_strchr(retval, '`') == NULL)
4066 retval[len++] = '*';
4067 retval[len] = NUL;
4068 }
4069 }
4070 return retval;
4071}
4072
4073/*
4074 * Must parse the command line so far to work out what context we are in.
4075 * Completion can then be done based on that context.
4076 * This routine sets the variables:
4077 * xp->xp_pattern The start of the pattern to be expanded within
4078 * the command line (ends at the cursor).
4079 * xp->xp_context The type of thing to expand. Will be one of:
4080 *
4081 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4082 * the command line, like an unknown command. Caller
4083 * should beep.
4084 * EXPAND_NOTHING Unrecognised context for completion, use char like
4085 * a normal char, rather than for completion. eg
4086 * :s/^I/
4087 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4088 * it.
4089 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4090 * EXPAND_FILES After command with XFILE set, or after setting
4091 * with P_EXPAND set. eg :e ^I, :w>>^I
4092 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4093 * when we know only directories are of interest. eg
4094 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004095 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4097 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4098 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4099 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4100 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4101 * EXPAND_EVENTS Complete event names
4102 * EXPAND_SYNTAX Complete :syntax command arguments
4103 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4104 * EXPAND_AUGROUP Complete autocommand group names
4105 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4106 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4107 * eg :unmap a^I , :cunab x^I
4108 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4109 * eg :call sub^I
4110 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4111 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4112 * names in expressions, eg :while s^I
4113 * EXPAND_ENV_VARS Complete environment variable names
4114 */
4115 static void
4116set_expand_context(xp)
4117 expand_T *xp;
4118{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004119 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 if (ccline.cmdfirstc != ':'
4121#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004122 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004123 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124#endif
4125 )
4126 {
4127 xp->xp_context = EXPAND_NOTHING;
4128 return;
4129 }
4130 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4131}
4132
4133 void
4134set_cmd_context(xp, str, len, col)
4135 expand_T *xp;
4136 char_u *str; /* start of command line */
4137 int len; /* length of command line (excl. NUL) */
4138 int col; /* position of cursor */
4139{
4140 int old_char = NUL;
4141 char_u *nextcomm;
4142
4143 /*
4144 * Avoid a UMR warning from Purify, only save the character if it has been
4145 * written before.
4146 */
4147 if (col < len)
4148 old_char = str[col];
4149 str[col] = NUL;
4150 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004151
4152#ifdef FEAT_EVAL
4153 if (ccline.cmdfirstc == '=')
4154 /* pass CMD_SIZE because there is no real command */
4155 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004156 else if (ccline.input_fn)
4157 {
4158 xp->xp_context = ccline.xp_context;
4159 xp->xp_pattern = ccline.cmdbuff;
4160 xp->xp_arg = ccline.xp_arg;
4161 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004162 else
4163#endif
4164 while (nextcomm != NULL)
4165 nextcomm = set_one_cmd_context(xp, nextcomm);
4166
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 str[col] = old_char;
4168}
4169
4170/*
4171 * Expand the command line "str" from context "xp".
4172 * "xp" must have been set by set_cmd_context().
4173 * xp->xp_pattern points into "str", to where the text that is to be expanded
4174 * starts.
4175 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4176 * cursor.
4177 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4178 * key that triggered expansion literally.
4179 * Returns EXPAND_OK otherwise.
4180 */
4181 int
4182expand_cmdline(xp, str, col, matchcount, matches)
4183 expand_T *xp;
4184 char_u *str; /* start of command line */
4185 int col; /* position of cursor */
4186 int *matchcount; /* return: nr of matches */
4187 char_u ***matches; /* return: array of pointers to matches */
4188{
4189 char_u *file_str = NULL;
4190
4191 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4192 {
4193 beep_flush();
4194 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4195 }
4196 if (xp->xp_context == EXPAND_NOTHING)
4197 {
4198 /* Caller can use the character as a normal char instead */
4199 return EXPAND_NOTHING;
4200 }
4201
4202 /* add star to file name, or convert to regexp if not exp. files. */
4203 file_str = addstar(xp->xp_pattern,
4204 (int)(str + col - xp->xp_pattern), xp->xp_context);
4205 if (file_str == NULL)
4206 return EXPAND_UNSUCCESSFUL;
4207
4208 /* find all files that match the description */
4209 if (ExpandFromContext(xp, file_str, matchcount, matches,
4210 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4211 {
4212 *matchcount = 0;
4213 *matches = NULL;
4214 }
4215 vim_free(file_str);
4216
4217 return EXPAND_OK;
4218}
4219
4220#ifdef FEAT_MULTI_LANG
4221/*
4222 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4223 */
4224static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4225
4226 static void
4227cleanup_help_tags(num_file, file)
4228 int num_file;
4229 char_u **file;
4230{
4231 int i, j;
4232 int len;
4233
4234 for (i = 0; i < num_file; ++i)
4235 {
4236 len = (int)STRLEN(file[i]) - 3;
4237 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4238 {
4239 /* Sorting on priority means the same item in another language may
4240 * be anywhere. Search all items for a match up to the "@en". */
4241 for (j = 0; j < num_file; ++j)
4242 if (j != i
4243 && (int)STRLEN(file[j]) == len + 3
4244 && STRNCMP(file[i], file[j], len + 1) == 0)
4245 break;
4246 if (j == num_file)
4247 file[i][len] = NUL;
4248 }
4249 }
4250}
4251#endif
4252
4253/*
4254 * Do the expansion based on xp->xp_context and "pat".
4255 */
4256 static int
4257ExpandFromContext(xp, pat, num_file, file, options)
4258 expand_T *xp;
4259 char_u *pat;
4260 int *num_file;
4261 char_u ***file;
4262 int options;
4263{
4264#ifdef FEAT_CMDL_COMPL
4265 regmatch_T regmatch;
4266#endif
4267 int ret;
4268 int flags;
4269
4270 flags = EW_DIR; /* include directories */
4271 if (options & WILD_LIST_NOTFOUND)
4272 flags |= EW_NOTFOUND;
4273 if (options & WILD_ADD_SLASH)
4274 flags |= EW_ADDSLASH;
4275 if (options & WILD_KEEP_ALL)
4276 flags |= EW_KEEPALL;
4277 if (options & WILD_SILENT)
4278 flags |= EW_SILENT;
4279
4280 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4281 {
4282 /*
4283 * Expand file or directory names.
4284 */
4285 int free_pat = FALSE;
4286 int i;
4287
4288 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4289 * space */
4290 if (xp->xp_backslash != XP_BS_NONE)
4291 {
4292 free_pat = TRUE;
4293 pat = vim_strsave(pat);
4294 for (i = 0; pat[i]; ++i)
4295 if (pat[i] == '\\')
4296 {
4297 if (xp->xp_backslash == XP_BS_THREE
4298 && pat[i + 1] == '\\'
4299 && pat[i + 2] == '\\'
4300 && pat[i + 3] == ' ')
4301 STRCPY(pat + i, pat + i + 3);
4302 if (xp->xp_backslash == XP_BS_ONE
4303 && pat[i + 1] == ' ')
4304 STRCPY(pat + i, pat + i + 1);
4305 }
4306 }
4307
4308 if (xp->xp_context == EXPAND_FILES)
4309 flags |= EW_FILE;
4310 else
4311 flags = (flags | EW_DIR) & ~EW_FILE;
4312 ret = expand_wildcards(1, &pat, num_file, file, flags);
4313 if (free_pat)
4314 vim_free(pat);
4315 return ret;
4316 }
4317
4318 *file = (char_u **)"";
4319 *num_file = 0;
4320 if (xp->xp_context == EXPAND_HELP)
4321 {
4322 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4323 {
4324#ifdef FEAT_MULTI_LANG
4325 cleanup_help_tags(*num_file, *file);
4326#endif
4327 return OK;
4328 }
4329 return FAIL;
4330 }
4331
4332#ifndef FEAT_CMDL_COMPL
4333 return FAIL;
4334#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004335 if (xp->xp_context == EXPAND_SHELLCMD)
4336 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (xp->xp_context == EXPAND_OLD_SETTING)
4338 return ExpandOldSetting(num_file, file);
4339 if (xp->xp_context == EXPAND_BUFFERS)
4340 return ExpandBufnames(pat, num_file, file, options);
4341 if (xp->xp_context == EXPAND_TAGS
4342 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4343 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4344 if (xp->xp_context == EXPAND_COLORS)
4345 return ExpandRTDir(pat, num_file, file, "colors");
4346 if (xp->xp_context == EXPAND_COMPILER)
4347 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004348# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4349 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004350 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
4353 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4354 if (regmatch.regprog == NULL)
4355 return FAIL;
4356
4357 /* set ignore-case according to p_ic, p_scs and pat */
4358 regmatch.rm_ic = ignorecase(pat);
4359
4360 if (xp->xp_context == EXPAND_SETTINGS
4361 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4362 ret = ExpandSettings(xp, &regmatch, num_file, file);
4363 else if (xp->xp_context == EXPAND_MAPPINGS)
4364 ret = ExpandMappings(&regmatch, num_file, file);
4365# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4366 else if (xp->xp_context == EXPAND_USER_DEFINED)
4367 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4368# endif
4369 else
4370 {
4371 static struct expgen
4372 {
4373 int context;
4374 char_u *((*func)__ARGS((expand_T *, int)));
4375 int ic;
4376 } tab[] =
4377 {
4378 {EXPAND_COMMANDS, get_command_name, FALSE},
4379#ifdef FEAT_USR_CMDS
4380 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4381 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4382 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4383 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4384#endif
4385#ifdef FEAT_EVAL
4386 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4387 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4388 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4389 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4390#endif
4391#ifdef FEAT_MENU
4392 {EXPAND_MENUS, get_menu_name, FALSE},
4393 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4394#endif
4395#ifdef FEAT_SYN_HL
4396 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4397#endif
4398 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4399#ifdef FEAT_AUTOCMD
4400 {EXPAND_EVENTS, get_event_name, TRUE},
4401 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4402#endif
4403#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4404 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4405 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4406#endif
4407 {EXPAND_ENV_VARS, get_env_name, TRUE},
4408 };
4409 int i;
4410
4411 /*
4412 * Find a context in the table and call the ExpandGeneric() with the
4413 * right function to do the expansion.
4414 */
4415 ret = FAIL;
4416 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4417 if (xp->xp_context == tab[i].context)
4418 {
4419 if (tab[i].ic)
4420 regmatch.rm_ic = TRUE;
4421 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4422 break;
4423 }
4424 }
4425
4426 vim_free(regmatch.regprog);
4427
4428 return ret;
4429#endif /* FEAT_CMDL_COMPL */
4430}
4431
4432#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4433/*
4434 * Expand a list of names.
4435 *
4436 * Generic function for command line completion. It calls a function to
4437 * obtain strings, one by one. The strings are matched against a regexp
4438 * program. Matching strings are copied into an array, which is returned.
4439 *
4440 * Returns OK when no problems encountered, FAIL for error (out of memory).
4441 */
4442 int
4443ExpandGeneric(xp, regmatch, num_file, file, func)
4444 expand_T *xp;
4445 regmatch_T *regmatch;
4446 int *num_file;
4447 char_u ***file;
4448 char_u *((*func)__ARGS((expand_T *, int)));
4449 /* returns a string from the list */
4450{
4451 int i;
4452 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004453 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 char_u *str;
4455
4456 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004457 * round == 0: count the number of matching names
4458 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004460 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
4462 for (i = 0; ; ++i)
4463 {
4464 str = (*func)(xp, i);
4465 if (str == NULL) /* end of list */
4466 break;
4467 if (*str == NUL) /* skip empty strings */
4468 continue;
4469
4470 if (vim_regexec(regmatch, str, (colnr_T)0))
4471 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004472 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
4474 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4475 (*file)[count] = str;
4476#ifdef FEAT_MENU
4477 if (func == get_menu_names && str != NULL)
4478 {
4479 /* test for separator added by get_menu_names() */
4480 str += STRLEN(str) - 1;
4481 if (*str == '\001')
4482 *str = '.';
4483 }
4484#endif
4485 }
4486 ++count;
4487 }
4488 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004489 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
4491 if (count == 0)
4492 return OK;
4493 *num_file = count;
4494 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4495 if (*file == NULL)
4496 {
4497 *file = (char_u **)"";
4498 return FAIL;
4499 }
4500 count = 0;
4501 }
4502 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004503
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004504 /* Sort the results. Keep menu's in the specified order. */
4505 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4506 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004507
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 return OK;
4509}
4510
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004511/*
4512 * Complete a shell command.
4513 * Returns FAIL or OK;
4514 */
4515 static int
4516expand_shellcmd(filepat, num_file, file, flagsarg)
4517 char_u *filepat; /* pattern to match with command names */
4518 int *num_file; /* return: number of matches */
4519 char_u ***file; /* return: array with matches */
4520 int flagsarg; /* EW_ flags */
4521{
4522 char_u *pat;
4523 int i;
4524 char_u *path;
4525 int mustfree = FALSE;
4526 garray_T ga;
4527 char_u *buf = alloc(MAXPATHL);
4528 size_t l;
4529 char_u *s, *e;
4530 int flags = flagsarg;
4531 int ret;
4532
4533 if (buf == NULL)
4534 return FAIL;
4535
4536 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4537 * space */
4538 pat = vim_strsave(filepat);
4539 for (i = 0; pat[i]; ++i)
4540 if (pat[i] == '\\' && pat[i + 1] == ' ')
4541 STRCPY(pat + i, pat + i + 1);
4542
4543 flags |= EW_FILE | EW_EXEC;
4544
4545 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004546 if (mch_isFullName(pat))
4547 path = (char_u *)" ";
4548 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004549 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4550 path = (char_u *)".";
4551 else
4552 path = vim_getenv((char_u *)"PATH", &mustfree);
4553
4554 /*
4555 * Go over all directories in $PATH. Expand matches in that directory and
4556 * collect them in "ga".
4557 */
4558 ga_init2(&ga, (int)sizeof(char *), 10);
4559 for (s = path; *s != NUL; s = e)
4560 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004561 if (*s == ' ')
4562 ++s; /* Skip space used for absolute path name. */
4563
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004564#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4565 e = vim_strchr(s, ';');
4566#else
4567 e = vim_strchr(s, ':');
4568#endif
4569 if (e == NULL)
4570 e = s + STRLEN(s);
4571
4572 l = e - s;
4573 if (l > MAXPATHL - 5)
4574 break;
4575 vim_strncpy(buf, s, l);
4576 add_pathsep(buf);
4577 l = STRLEN(buf);
4578 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4579
4580 /* Expand matches in one directory of $PATH. */
4581 ret = expand_wildcards(1, &buf, num_file, file, flags);
4582 if (ret == OK)
4583 {
4584 if (ga_grow(&ga, *num_file) == FAIL)
4585 FreeWild(*num_file, *file);
4586 else
4587 {
4588 for (i = 0; i < *num_file; ++i)
4589 {
4590 s = (*file)[i];
4591 if (STRLEN(s) > l)
4592 {
4593 /* Remove the path again. */
4594 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4595 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4596 }
4597 else
4598 vim_free(s);
4599 }
4600 vim_free(*file);
4601 }
4602 }
4603 if (*e != NUL)
4604 ++e;
4605 }
4606 *file = ga.ga_data;
4607 *num_file = ga.ga_len;
4608
4609 vim_free(buf);
4610 vim_free(pat);
4611 if (mustfree)
4612 vim_free(path);
4613 return OK;
4614}
4615
4616
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004618static 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));
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004621 * call "user_expand_func()" to invoke a user defined VimL function and return
4622 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004624 static void *
4625call_user_expand_func(user_expand_func, xp, num_file, file)
4626 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 int *num_file;
4629 char_u ***file;
4630{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004631 char_u keep;
4632 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004635 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004636 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637
4638 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004639 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 *num_file = 0;
4641 *file = NULL;
4642
4643 keep = ccline.cmdbuff[ccline.cmdlen];
4644 ccline.cmdbuff[ccline.cmdlen] = 0;
4645 sprintf((char *)num, "%d", ccline.cmdpos);
4646 args[0] = xp->xp_pattern;
4647 args[1] = ccline.cmdbuff;
4648 args[2] = num;
4649
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004650 /* Save the cmdline, we don't know what the function may do. */
4651 save_ccline = ccline;
4652 ccline.cmdbuff = NULL;
4653 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004655
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004656 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004657
4658 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004660
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004662
4663 return ret;
4664}
4665
4666/*
4667 * Expand names with a function defined by the user.
4668 */
4669 static int
4670ExpandUserDefined(xp, regmatch, num_file, file)
4671 expand_T *xp;
4672 regmatch_T *regmatch;
4673 int *num_file;
4674 char_u ***file;
4675{
4676 char_u *retstr;
4677 char_u *s;
4678 char_u *e;
4679 char_u keep;
4680 garray_T ga;
4681
4682 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4683 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 return FAIL;
4685
4686 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004687 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
4689 e = vim_strchr(s, '\n');
4690 if (e == NULL)
4691 e = s + STRLEN(s);
4692 keep = *e;
4693 *e = 0;
4694
4695 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4696 {
4697 *e = keep;
4698 if (*e != NUL)
4699 ++e;
4700 continue;
4701 }
4702
4703 if (ga_grow(&ga, 1) == FAIL)
4704 break;
4705
4706 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4707 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709 *e = keep;
4710 if (*e != NUL)
4711 ++e;
4712 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004713 vim_free(retstr);
4714 *file = ga.ga_data;
4715 *num_file = ga.ga_len;
4716 return OK;
4717}
4718
4719/*
4720 * Expand names with a list returned by a function defined by the user.
4721 */
4722 static int
4723ExpandUserList(xp, num_file, file)
4724 expand_T *xp;
4725 int *num_file;
4726 char_u ***file;
4727{
4728 list_T *retlist;
4729 listitem_T *li;
4730 garray_T ga;
4731
4732 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4733 if (retlist == NULL)
4734 return FAIL;
4735
4736 ga_init2(&ga, (int)sizeof(char *), 3);
4737 /* Loop over the items in the list. */
4738 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4739 {
4740 if (li->li_tv.v_type != VAR_STRING)
4741 continue; /* Skip non-string items */
4742
4743 if (ga_grow(&ga, 1) == FAIL)
4744 break;
4745
4746 ((char_u **)ga.ga_data)[ga.ga_len] =
4747 vim_strsave(li->li_tv.vval.v_string);
4748 ++ga.ga_len;
4749 }
4750 list_unref(retlist);
4751
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 *file = ga.ga_data;
4753 *num_file = ga.ga_len;
4754 return OK;
4755}
4756#endif
4757
4758/*
4759 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4760 * or compiler names.
4761 */
4762 static int
4763ExpandRTDir(pat, num_file, file, dirname)
4764 char_u *pat;
4765 int *num_file;
4766 char_u ***file;
4767 char *dirname; /* "colors" or "compiler" */
4768{
4769 char_u *all;
4770 char_u *s;
4771 char_u *e;
4772 garray_T ga;
4773
4774 *num_file = 0;
4775 *file = NULL;
4776 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4777 if (s == NULL)
4778 return FAIL;
4779 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4780 all = globpath(p_rtp, s);
4781 vim_free(s);
4782 if (all == NULL)
4783 return FAIL;
4784
4785 ga_init2(&ga, (int)sizeof(char *), 3);
4786 for (s = all; *s != NUL; s = e)
4787 {
4788 e = vim_strchr(s, '\n');
4789 if (e == NULL)
4790 e = s + STRLEN(s);
4791 if (ga_grow(&ga, 1) == FAIL)
4792 break;
4793 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4794 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004795 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 if (*s == '\n' || vim_ispathsep(*s))
4797 break;
4798 ++s;
4799 ((char_u **)ga.ga_data)[ga.ga_len] =
4800 vim_strnsave(s, (int)(e - s - 4));
4801 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
4803 if (*e != NUL)
4804 ++e;
4805 }
4806 vim_free(all);
4807 *file = ga.ga_data;
4808 *num_file = ga.ga_len;
4809 return OK;
4810}
4811
4812#endif
4813
4814#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4815/*
4816 * Expand "file" for all comma-separated directories in "path".
4817 * Returns an allocated string with all matches concatenated, separated by
4818 * newlines. Returns NULL for an error or no matches.
4819 */
4820 char_u *
4821globpath(path, file)
4822 char_u *path;
4823 char_u *file;
4824{
4825 expand_T xpc;
4826 char_u *buf;
4827 garray_T ga;
4828 int i;
4829 int len;
4830 int num_p;
4831 char_u **p;
4832 char_u *cur = NULL;
4833
4834 buf = alloc(MAXPATHL);
4835 if (buf == NULL)
4836 return NULL;
4837
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004838 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 ga_init2(&ga, 1, 100);
4842
4843 /* Loop over all entries in {path}. */
4844 while (*path != NUL)
4845 {
4846 /* Copy one item of the path to buf[] and concatenate the file name. */
4847 copy_option_part(&path, buf, MAXPATHL, ",");
4848 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4849 {
4850 add_pathsep(buf);
4851 STRCAT(buf, file);
4852 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4853 && num_p > 0)
4854 {
4855 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4856 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004857 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859 /* Concatenate new results to previous ones. */
4860 if (ga_grow(&ga, len) == OK)
4861 {
4862 cur = (char_u *)ga.ga_data + ga.ga_len;
4863 for (i = 0; i < num_p; ++i)
4864 {
4865 STRCPY(cur, p[i]);
4866 cur += STRLEN(p[i]);
4867 *cur++ = '\n';
4868 }
4869 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
4871 FreeWild(num_p, p);
4872 }
4873 }
4874 }
4875 if (cur != NULL)
4876 *--cur = 0; /* Replace trailing newline with NUL */
4877
4878 vim_free(buf);
4879 return (char_u *)ga.ga_data;
4880}
4881
4882#endif
4883
4884#if defined(FEAT_CMDHIST) || defined(PROTO)
4885
4886/*********************************
4887 * Command line history stuff *
4888 *********************************/
4889
4890/*
4891 * Translate a history character to the associated type number.
4892 */
4893 static int
4894hist_char2type(c)
4895 int c;
4896{
4897 if (c == ':')
4898 return HIST_CMD;
4899 if (c == '=')
4900 return HIST_EXPR;
4901 if (c == '@')
4902 return HIST_INPUT;
4903 if (c == '>')
4904 return HIST_DEBUG;
4905 return HIST_SEARCH; /* must be '?' or '/' */
4906}
4907
4908/*
4909 * Table of history names.
4910 * These names are used in :history and various hist...() functions.
4911 * It is sufficient to give the significant prefix of a history name.
4912 */
4913
4914static char *(history_names[]) =
4915{
4916 "cmd",
4917 "search",
4918 "expr",
4919 "input",
4920 "debug",
4921 NULL
4922};
4923
4924/*
4925 * init_history() - Initialize the command line history.
4926 * Also used to re-allocate the history when the size changes.
4927 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004928 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929init_history()
4930{
4931 int newlen; /* new length of history table */
4932 histentry_T *temp;
4933 int i;
4934 int j;
4935 int type;
4936
4937 /*
4938 * If size of history table changed, reallocate it
4939 */
4940 newlen = (int)p_hi;
4941 if (newlen != hislen) /* history length changed */
4942 {
4943 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4944 {
4945 if (newlen)
4946 {
4947 temp = (histentry_T *)lalloc(
4948 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4949 if (temp == NULL) /* out of memory! */
4950 {
4951 if (type == 0) /* first one: just keep the old length */
4952 {
4953 newlen = hislen;
4954 break;
4955 }
4956 /* Already changed one table, now we can only have zero
4957 * length for all tables. */
4958 newlen = 0;
4959 type = -1;
4960 continue;
4961 }
4962 }
4963 else
4964 temp = NULL;
4965 if (newlen == 0 || temp != NULL)
4966 {
4967 if (hisidx[type] < 0) /* there are no entries yet */
4968 {
4969 for (i = 0; i < newlen; ++i)
4970 {
4971 temp[i].hisnum = 0;
4972 temp[i].hisstr = NULL;
4973 }
4974 }
4975 else if (newlen > hislen) /* array becomes bigger */
4976 {
4977 for (i = 0; i <= hisidx[type]; ++i)
4978 temp[i] = history[type][i];
4979 j = i;
4980 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4981 {
4982 temp[i].hisnum = 0;
4983 temp[i].hisstr = NULL;
4984 }
4985 for ( ; j < hislen; ++i, ++j)
4986 temp[i] = history[type][j];
4987 }
4988 else /* array becomes smaller or 0 */
4989 {
4990 j = hisidx[type];
4991 for (i = newlen - 1; ; --i)
4992 {
4993 if (i >= 0) /* copy newest entries */
4994 temp[i] = history[type][j];
4995 else /* remove older entries */
4996 vim_free(history[type][j].hisstr);
4997 if (--j < 0)
4998 j = hislen - 1;
4999 if (j == hisidx[type])
5000 break;
5001 }
5002 hisidx[type] = newlen - 1;
5003 }
5004 vim_free(history[type]);
5005 history[type] = temp;
5006 }
5007 }
5008 hislen = newlen;
5009 }
5010}
5011
5012/*
5013 * Check if command line 'str' is already in history.
5014 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5015 */
5016 static int
5017in_history(type, str, move_to_front)
5018 int type;
5019 char_u *str;
5020 int move_to_front; /* Move the entry to the front if it exists */
5021{
5022 int i;
5023 int last_i = -1;
5024
5025 if (hisidx[type] < 0)
5026 return FALSE;
5027 i = hisidx[type];
5028 do
5029 {
5030 if (history[type][i].hisstr == NULL)
5031 return FALSE;
5032 if (STRCMP(str, history[type][i].hisstr) == 0)
5033 {
5034 if (!move_to_front)
5035 return TRUE;
5036 last_i = i;
5037 break;
5038 }
5039 if (--i < 0)
5040 i = hislen - 1;
5041 } while (i != hisidx[type]);
5042
5043 if (last_i >= 0)
5044 {
5045 str = history[type][i].hisstr;
5046 while (i != hisidx[type])
5047 {
5048 if (++i >= hislen)
5049 i = 0;
5050 history[type][last_i] = history[type][i];
5051 last_i = i;
5052 }
5053 history[type][i].hisstr = str;
5054 history[type][i].hisnum = ++hisnum[type];
5055 return TRUE;
5056 }
5057 return FALSE;
5058}
5059
5060/*
5061 * Convert history name (from table above) to its HIST_ equivalent.
5062 * When "name" is empty, return "cmd" history.
5063 * Returns -1 for unknown history name.
5064 */
5065 int
5066get_histtype(name)
5067 char_u *name;
5068{
5069 int i;
5070 int len = (int)STRLEN(name);
5071
5072 /* No argument: use current history. */
5073 if (len == 0)
5074 return hist_char2type(ccline.cmdfirstc);
5075
5076 for (i = 0; history_names[i] != NULL; ++i)
5077 if (STRNICMP(name, history_names[i], len) == 0)
5078 return i;
5079
5080 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5081 return hist_char2type(name[0]);
5082
5083 return -1;
5084}
5085
5086static int last_maptick = -1; /* last seen maptick */
5087
5088/*
5089 * Add the given string to the given history. If the string is already in the
5090 * history then it is moved to the front. "histype" may be one of he HIST_
5091 * values.
5092 */
5093 void
5094add_to_history(histype, new_entry, in_map, sep)
5095 int histype;
5096 char_u *new_entry;
5097 int in_map; /* consider maptick when inside a mapping */
5098 int sep; /* separator character used (search hist) */
5099{
5100 histentry_T *hisptr;
5101 int len;
5102
5103 if (hislen == 0) /* no history */
5104 return;
5105
5106 /*
5107 * Searches inside the same mapping overwrite each other, so that only
5108 * the last line is kept. Be careful not to remove a line that was moved
5109 * down, only lines that were added.
5110 */
5111 if (histype == HIST_SEARCH && in_map)
5112 {
5113 if (maptick == last_maptick)
5114 {
5115 /* Current line is from the same mapping, remove it */
5116 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5117 vim_free(hisptr->hisstr);
5118 hisptr->hisstr = NULL;
5119 hisptr->hisnum = 0;
5120 --hisnum[histype];
5121 if (--hisidx[HIST_SEARCH] < 0)
5122 hisidx[HIST_SEARCH] = hislen - 1;
5123 }
5124 last_maptick = -1;
5125 }
5126 if (!in_history(histype, new_entry, TRUE))
5127 {
5128 if (++hisidx[histype] == hislen)
5129 hisidx[histype] = 0;
5130 hisptr = &history[histype][hisidx[histype]];
5131 vim_free(hisptr->hisstr);
5132
5133 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005134 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5136 if (hisptr->hisstr != NULL)
5137 hisptr->hisstr[len + 1] = sep;
5138
5139 hisptr->hisnum = ++hisnum[histype];
5140 if (histype == HIST_SEARCH && in_map)
5141 last_maptick = maptick;
5142 }
5143}
5144
5145#if defined(FEAT_EVAL) || defined(PROTO)
5146
5147/*
5148 * Get identifier of newest history entry.
5149 * "histype" may be one of the HIST_ values.
5150 */
5151 int
5152get_history_idx(histype)
5153 int histype;
5154{
5155 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5156 || hisidx[histype] < 0)
5157 return -1;
5158
5159 return history[histype][hisidx[histype]].hisnum;
5160}
5161
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005162static struct cmdline_info *get_ccline_ptr __ARGS((void));
5163
5164/*
5165 * Get pointer to the command line info to use. cmdline_paste() may clear
5166 * ccline and put the previous value in prev_ccline.
5167 */
5168 static struct cmdline_info *
5169get_ccline_ptr()
5170{
5171 if ((State & CMDLINE) == 0)
5172 return NULL;
5173 if (ccline.cmdbuff != NULL)
5174 return &ccline;
5175 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5176 return &prev_ccline;
5177 return NULL;
5178}
5179
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180/*
5181 * Get the current command line in allocated memory.
5182 * Only works when the command line is being edited.
5183 * Returns NULL when something is wrong.
5184 */
5185 char_u *
5186get_cmdline_str()
5187{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005188 struct cmdline_info *p = get_ccline_ptr();
5189
5190 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005192 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193}
5194
5195/*
5196 * Get the current command line position, counted in bytes.
5197 * Zero is the first position.
5198 * Only works when the command line is being edited.
5199 * Returns -1 when something is wrong.
5200 */
5201 int
5202get_cmdline_pos()
5203{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005204 struct cmdline_info *p = get_ccline_ptr();
5205
5206 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005208 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209}
5210
5211/*
5212 * Set the command line byte position to "pos". Zero is the first position.
5213 * Only works when the command line is being edited.
5214 * Returns 1 when failed, 0 when OK.
5215 */
5216 int
5217set_cmdline_pos(pos)
5218 int pos;
5219{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005220 struct cmdline_info *p = get_ccline_ptr();
5221
5222 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 return 1;
5224
5225 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5226 * changed the command line. */
5227 if (pos < 0)
5228 new_cmdpos = 0;
5229 else
5230 new_cmdpos = pos;
5231 return 0;
5232}
5233
5234/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005235 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005236 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005237 * Only works when the command line is being edited.
5238 * Returns NUL when something is wrong.
5239 */
5240 int
5241get_cmdline_type()
5242{
5243 struct cmdline_info *p = get_ccline_ptr();
5244
5245 if (p == NULL)
5246 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005247 if (p->cmdfirstc == NUL)
5248 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005249 return p->cmdfirstc;
5250}
5251
5252/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 * Calculate history index from a number:
5254 * num > 0: seen as identifying number of a history entry
5255 * num < 0: relative position in history wrt newest entry
5256 * "histype" may be one of the HIST_ values.
5257 */
5258 static int
5259calc_hist_idx(histype, num)
5260 int histype;
5261 int num;
5262{
5263 int i;
5264 histentry_T *hist;
5265 int wrapped = FALSE;
5266
5267 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5268 || (i = hisidx[histype]) < 0 || num == 0)
5269 return -1;
5270
5271 hist = history[histype];
5272 if (num > 0)
5273 {
5274 while (hist[i].hisnum > num)
5275 if (--i < 0)
5276 {
5277 if (wrapped)
5278 break;
5279 i += hislen;
5280 wrapped = TRUE;
5281 }
5282 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5283 return i;
5284 }
5285 else if (-num <= hislen)
5286 {
5287 i += num + 1;
5288 if (i < 0)
5289 i += hislen;
5290 if (hist[i].hisstr != NULL)
5291 return i;
5292 }
5293 return -1;
5294}
5295
5296/*
5297 * Get a history entry by its index.
5298 * "histype" may be one of the HIST_ values.
5299 */
5300 char_u *
5301get_history_entry(histype, idx)
5302 int histype;
5303 int idx;
5304{
5305 idx = calc_hist_idx(histype, idx);
5306 if (idx >= 0)
5307 return history[histype][idx].hisstr;
5308 else
5309 return (char_u *)"";
5310}
5311
5312/*
5313 * Clear all entries of a history.
5314 * "histype" may be one of the HIST_ values.
5315 */
5316 int
5317clr_history(histype)
5318 int histype;
5319{
5320 int i;
5321 histentry_T *hisptr;
5322
5323 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5324 {
5325 hisptr = history[histype];
5326 for (i = hislen; i--;)
5327 {
5328 vim_free(hisptr->hisstr);
5329 hisptr->hisnum = 0;
5330 hisptr++->hisstr = NULL;
5331 }
5332 hisidx[histype] = -1; /* mark history as cleared */
5333 hisnum[histype] = 0; /* reset identifier counter */
5334 return OK;
5335 }
5336 return FAIL;
5337}
5338
5339/*
5340 * Remove all entries matching {str} from a history.
5341 * "histype" may be one of the HIST_ values.
5342 */
5343 int
5344del_history_entry(histype, str)
5345 int histype;
5346 char_u *str;
5347{
5348 regmatch_T regmatch;
5349 histentry_T *hisptr;
5350 int idx;
5351 int i;
5352 int last;
5353 int found = FALSE;
5354
5355 regmatch.regprog = NULL;
5356 regmatch.rm_ic = FALSE; /* always match case */
5357 if (hislen != 0
5358 && histype >= 0
5359 && histype < HIST_COUNT
5360 && *str != NUL
5361 && (idx = hisidx[histype]) >= 0
5362 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5363 != NULL)
5364 {
5365 i = last = idx;
5366 do
5367 {
5368 hisptr = &history[histype][i];
5369 if (hisptr->hisstr == NULL)
5370 break;
5371 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5372 {
5373 found = TRUE;
5374 vim_free(hisptr->hisstr);
5375 hisptr->hisstr = NULL;
5376 hisptr->hisnum = 0;
5377 }
5378 else
5379 {
5380 if (i != last)
5381 {
5382 history[histype][last] = *hisptr;
5383 hisptr->hisstr = NULL;
5384 hisptr->hisnum = 0;
5385 }
5386 if (--last < 0)
5387 last += hislen;
5388 }
5389 if (--i < 0)
5390 i += hislen;
5391 } while (i != idx);
5392 if (history[histype][idx].hisstr == NULL)
5393 hisidx[histype] = -1;
5394 }
5395 vim_free(regmatch.regprog);
5396 return found;
5397}
5398
5399/*
5400 * Remove an indexed entry from a history.
5401 * "histype" may be one of the HIST_ values.
5402 */
5403 int
5404del_history_idx(histype, idx)
5405 int histype;
5406 int idx;
5407{
5408 int i, j;
5409
5410 i = calc_hist_idx(histype, idx);
5411 if (i < 0)
5412 return FALSE;
5413 idx = hisidx[histype];
5414 vim_free(history[histype][i].hisstr);
5415
5416 /* When deleting the last added search string in a mapping, reset
5417 * last_maptick, so that the last added search string isn't deleted again.
5418 */
5419 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5420 last_maptick = -1;
5421
5422 while (i != idx)
5423 {
5424 j = (i + 1) % hislen;
5425 history[histype][i] = history[histype][j];
5426 i = j;
5427 }
5428 history[histype][i].hisstr = NULL;
5429 history[histype][i].hisnum = 0;
5430 if (--i < 0)
5431 i += hislen;
5432 hisidx[histype] = i;
5433 return TRUE;
5434}
5435
5436#endif /* FEAT_EVAL */
5437
5438#if defined(FEAT_CRYPT) || defined(PROTO)
5439/*
5440 * Very specific function to remove the value in ":set key=val" from the
5441 * history.
5442 */
5443 void
5444remove_key_from_history()
5445{
5446 char_u *p;
5447 int i;
5448
5449 i = hisidx[HIST_CMD];
5450 if (i < 0)
5451 return;
5452 p = history[HIST_CMD][i].hisstr;
5453 if (p != NULL)
5454 for ( ; *p; ++p)
5455 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5456 {
5457 p = vim_strchr(p + 3, '=');
5458 if (p == NULL)
5459 break;
5460 ++p;
5461 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5462 if (p[i] == '\\' && p[i + 1])
5463 ++i;
5464 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5465 --p;
5466 }
5467}
5468#endif
5469
5470#endif /* FEAT_CMDHIST */
5471
5472#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5473/*
5474 * Get indices "num1,num2" that specify a range within a list (not a range of
5475 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5476 * Returns OK if parsed successfully, otherwise FAIL.
5477 */
5478 int
5479get_list_range(str, num1, num2)
5480 char_u **str;
5481 int *num1;
5482 int *num2;
5483{
5484 int len;
5485 int first = FALSE;
5486 long num;
5487
5488 *str = skipwhite(*str);
5489 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5490 {
5491 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5492 *str += len;
5493 *num1 = (int)num;
5494 first = TRUE;
5495 }
5496 *str = skipwhite(*str);
5497 if (**str == ',') /* parse "to" part of range */
5498 {
5499 *str = skipwhite(*str + 1);
5500 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5501 if (len > 0)
5502 {
5503 *num2 = (int)num;
5504 *str = skipwhite(*str + len);
5505 }
5506 else if (!first) /* no number given at all */
5507 return FAIL;
5508 }
5509 else if (first) /* only one number given */
5510 *num2 = *num1;
5511 return OK;
5512}
5513#endif
5514
5515#if defined(FEAT_CMDHIST) || defined(PROTO)
5516/*
5517 * :history command - print a history
5518 */
5519 void
5520ex_history(eap)
5521 exarg_T *eap;
5522{
5523 histentry_T *hist;
5524 int histype1 = HIST_CMD;
5525 int histype2 = HIST_CMD;
5526 int hisidx1 = 1;
5527 int hisidx2 = -1;
5528 int idx;
5529 int i, j, k;
5530 char_u *end;
5531 char_u *arg = eap->arg;
5532
5533 if (hislen == 0)
5534 {
5535 MSG(_("'history' option is zero"));
5536 return;
5537 }
5538
5539 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5540 {
5541 end = arg;
5542 while (ASCII_ISALPHA(*end)
5543 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5544 end++;
5545 i = *end;
5546 *end = NUL;
5547 histype1 = get_histtype(arg);
5548 if (histype1 == -1)
5549 {
5550 if (STRICMP(arg, "all") == 0)
5551 {
5552 histype1 = 0;
5553 histype2 = HIST_COUNT-1;
5554 }
5555 else
5556 {
5557 *end = i;
5558 EMSG(_(e_trailing));
5559 return;
5560 }
5561 }
5562 else
5563 histype2 = histype1;
5564 *end = i;
5565 }
5566 else
5567 end = arg;
5568 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5569 {
5570 EMSG(_(e_trailing));
5571 return;
5572 }
5573
5574 for (; !got_int && histype1 <= histype2; ++histype1)
5575 {
5576 STRCPY(IObuff, "\n # ");
5577 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5578 MSG_PUTS_TITLE(IObuff);
5579 idx = hisidx[histype1];
5580 hist = history[histype1];
5581 j = hisidx1;
5582 k = hisidx2;
5583 if (j < 0)
5584 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5585 if (k < 0)
5586 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5587 if (idx >= 0 && j <= k)
5588 for (i = idx + 1; !got_int; ++i)
5589 {
5590 if (i == hislen)
5591 i = 0;
5592 if (hist[i].hisstr != NULL
5593 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5594 {
5595 msg_putchar('\n');
5596 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5597 hist[i].hisnum);
5598 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5599 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5600 (int)Columns - 10);
5601 else
5602 STRCAT(IObuff, hist[i].hisstr);
5603 msg_outtrans(IObuff);
5604 out_flush();
5605 }
5606 if (i == idx)
5607 break;
5608 }
5609 }
5610}
5611#endif
5612
5613#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5614static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5615static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5616static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5617static int viminfo_add_at_front = FALSE;
5618
5619static int hist_type2char __ARGS((int type, int use_question));
5620
5621/*
5622 * Translate a history type number to the associated character.
5623 */
5624 static int
5625hist_type2char(type, use_question)
5626 int type;
5627 int use_question; /* use '?' instead of '/' */
5628{
5629 if (type == HIST_CMD)
5630 return ':';
5631 if (type == HIST_SEARCH)
5632 {
5633 if (use_question)
5634 return '?';
5635 else
5636 return '/';
5637 }
5638 if (type == HIST_EXPR)
5639 return '=';
5640 return '@';
5641}
5642
5643/*
5644 * Prepare for reading the history from the viminfo file.
5645 * This allocates history arrays to store the read history lines.
5646 */
5647 void
5648prepare_viminfo_history(asklen)
5649 int asklen;
5650{
5651 int i;
5652 int num;
5653 int type;
5654 int len;
5655
5656 init_history();
5657 viminfo_add_at_front = (asklen != 0);
5658 if (asklen > hislen)
5659 asklen = hislen;
5660
5661 for (type = 0; type < HIST_COUNT; ++type)
5662 {
5663 /*
5664 * Count the number of empty spaces in the history list. If there are
5665 * more spaces available than we request, then fill them up.
5666 */
5667 for (i = 0, num = 0; i < hislen; i++)
5668 if (history[type][i].hisstr == NULL)
5669 num++;
5670 len = asklen;
5671 if (num > len)
5672 len = num;
5673 if (len <= 0)
5674 viminfo_history[type] = NULL;
5675 else
5676 viminfo_history[type] =
5677 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5678 if (viminfo_history[type] == NULL)
5679 len = 0;
5680 viminfo_hislen[type] = len;
5681 viminfo_hisidx[type] = 0;
5682 }
5683}
5684
5685/*
5686 * Accept a line from the viminfo, store it in the history array when it's
5687 * new.
5688 */
5689 int
5690read_viminfo_history(virp)
5691 vir_T *virp;
5692{
5693 int type;
5694 long_u len;
5695 char_u *val;
5696 char_u *p;
5697
5698 type = hist_char2type(virp->vir_line[0]);
5699 if (viminfo_hisidx[type] < viminfo_hislen[type])
5700 {
5701 val = viminfo_readstring(virp, 1, TRUE);
5702 if (val != NULL && *val != NUL)
5703 {
5704 if (!in_history(type, val + (type == HIST_SEARCH),
5705 viminfo_add_at_front))
5706 {
5707 /* Need to re-allocate to append the separator byte. */
5708 len = STRLEN(val);
5709 p = lalloc(len + 2, TRUE);
5710 if (p != NULL)
5711 {
5712 if (type == HIST_SEARCH)
5713 {
5714 /* Search entry: Move the separator from the first
5715 * column to after the NUL. */
5716 mch_memmove(p, val + 1, (size_t)len);
5717 p[len] = (*val == ' ' ? NUL : *val);
5718 }
5719 else
5720 {
5721 /* Not a search entry: No separator in the viminfo
5722 * file, add a NUL separator. */
5723 mch_memmove(p, val, (size_t)len + 1);
5724 p[len + 1] = NUL;
5725 }
5726 viminfo_history[type][viminfo_hisidx[type]++] = p;
5727 }
5728 }
5729 }
5730 vim_free(val);
5731 }
5732 return viminfo_readline(virp);
5733}
5734
5735 void
5736finish_viminfo_history()
5737{
5738 int idx;
5739 int i;
5740 int type;
5741
5742 for (type = 0; type < HIST_COUNT; ++type)
5743 {
5744 if (history[type] == NULL)
5745 return;
5746 idx = hisidx[type] + viminfo_hisidx[type];
5747 if (idx >= hislen)
5748 idx -= hislen;
5749 else if (idx < 0)
5750 idx = hislen - 1;
5751 if (viminfo_add_at_front)
5752 hisidx[type] = idx;
5753 else
5754 {
5755 if (hisidx[type] == -1)
5756 hisidx[type] = hislen - 1;
5757 do
5758 {
5759 if (history[type][idx].hisstr != NULL)
5760 break;
5761 if (++idx == hislen)
5762 idx = 0;
5763 } while (idx != hisidx[type]);
5764 if (idx != hisidx[type] && --idx < 0)
5765 idx = hislen - 1;
5766 }
5767 for (i = 0; i < viminfo_hisidx[type]; i++)
5768 {
5769 vim_free(history[type][idx].hisstr);
5770 history[type][idx].hisstr = viminfo_history[type][i];
5771 if (--idx < 0)
5772 idx = hislen - 1;
5773 }
5774 idx += 1;
5775 idx %= hislen;
5776 for (i = 0; i < viminfo_hisidx[type]; i++)
5777 {
5778 history[type][idx++].hisnum = ++hisnum[type];
5779 idx %= hislen;
5780 }
5781 vim_free(viminfo_history[type]);
5782 viminfo_history[type] = NULL;
5783 }
5784}
5785
5786 void
5787write_viminfo_history(fp)
5788 FILE *fp;
5789{
5790 int i;
5791 int type;
5792 int num_saved;
5793 char_u *p;
5794 int c;
5795
5796 init_history();
5797 if (hislen == 0)
5798 return;
5799 for (type = 0; type < HIST_COUNT; ++type)
5800 {
5801 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5802 if (num_saved == 0)
5803 continue;
5804 if (num_saved < 0) /* Use default */
5805 num_saved = hislen;
5806 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5807 type == HIST_CMD ? _("Command Line") :
5808 type == HIST_SEARCH ? _("Search String") :
5809 type == HIST_EXPR ? _("Expression") :
5810 _("Input Line"));
5811 if (num_saved > hislen)
5812 num_saved = hislen;
5813 i = hisidx[type];
5814 if (i >= 0)
5815 while (num_saved--)
5816 {
5817 p = history[type][i].hisstr;
5818 if (p != NULL)
5819 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005820 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 /* For the search history: put the separator in the second
5822 * column; use a space if there isn't one. */
5823 if (type == HIST_SEARCH)
5824 {
5825 c = p[STRLEN(p) + 1];
5826 putc(c == NUL ? ' ' : c, fp);
5827 }
5828 viminfo_writestring(fp, p);
5829 }
5830 if (--i < 0)
5831 i = hislen - 1;
5832 }
5833 }
5834}
5835#endif /* FEAT_VIMINFO */
5836
5837#if defined(FEAT_FKMAP) || defined(PROTO)
5838/*
5839 * Write a character at the current cursor+offset position.
5840 * It is directly written into the command buffer block.
5841 */
5842 void
5843cmd_pchar(c, offset)
5844 int c, offset;
5845{
5846 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5847 {
5848 EMSG(_("E198: cmd_pchar beyond the command length"));
5849 return;
5850 }
5851 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5852 ccline.cmdbuff[ccline.cmdlen] = NUL;
5853}
5854
5855 int
5856cmd_gchar(offset)
5857 int offset;
5858{
5859 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5860 {
5861 /* EMSG(_("cmd_gchar beyond the command length")); */
5862 return NUL;
5863 }
5864 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5865}
5866#endif
5867
5868#if defined(FEAT_CMDWIN) || defined(PROTO)
5869/*
5870 * Open a window on the current command line and history. Allow editing in
5871 * the window. Returns when the window is closed.
5872 * Returns:
5873 * CR if the command is to be executed
5874 * Ctrl_C if it is to be abandoned
5875 * K_IGNORE if editing continues
5876 */
5877 static int
5878ex_window()
5879{
5880 struct cmdline_info save_ccline;
5881 buf_T *old_curbuf = curbuf;
5882 win_T *old_curwin = curwin;
5883 buf_T *bp;
5884 win_T *wp;
5885 int i;
5886 linenr_T lnum;
5887 int histtype;
5888 garray_T winsizes;
5889 char_u typestr[2];
5890 int save_restart_edit = restart_edit;
5891 int save_State = State;
5892 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005893#ifdef FEAT_RIGHTLEFT
5894 int save_cmdmsg_rl = cmdmsg_rl;
5895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896
5897 /* Can't do this recursively. Can't do it when typing a password. */
5898 if (cmdwin_type != 0
5899# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5900 || cmdline_star > 0
5901# endif
5902 )
5903 {
5904 beep_flush();
5905 return K_IGNORE;
5906 }
5907
5908 /* Save current window sizes. */
5909 win_size_save(&winsizes);
5910
5911# ifdef FEAT_AUTOCMD
5912 /* Don't execute autocommands while creating the window. */
5913 ++autocmd_block;
5914# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005915 /* don't use a new tab page */
5916 cmdmod.tab = 0;
5917
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 /* Create a window for the command-line buffer. */
5919 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5920 {
5921 beep_flush();
5922 return K_IGNORE;
5923 }
5924 cmdwin_type = ccline.cmdfirstc;
5925 if (cmdwin_type == NUL)
5926 cmdwin_type = '-';
5927
5928 /* Create the command-line buffer empty. */
5929 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5930 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5931 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5932 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5933 curbuf->b_p_ma = TRUE;
5934# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005935 curwin->w_p_rl = cmdmsg_rl;
5936 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937# endif
5938# ifdef FEAT_SCROLLBIND
5939 curwin->w_p_scb = FALSE;
5940# endif
5941
5942# ifdef FEAT_AUTOCMD
5943 /* Do execute autocommands for setting the filetype (load syntax). */
5944 --autocmd_block;
5945# endif
5946
Bram Moolenaar46152342005-09-07 21:18:43 +00005947 /* Showing the prompt may have set need_wait_return, reset it. */
5948 need_wait_return = FALSE;
5949
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 histtype = hist_char2type(ccline.cmdfirstc);
5951 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5952 {
5953 if (p_wc == TAB)
5954 {
5955 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5956 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5957 }
5958 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5959 }
5960
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005961 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5962 * sets 'textwidth' to 78). */
5963 curbuf->b_p_tw = 0;
5964
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 /* Fill the buffer with the history. */
5966 init_history();
5967 if (hislen > 0)
5968 {
5969 i = hisidx[histtype];
5970 if (i >= 0)
5971 {
5972 lnum = 0;
5973 do
5974 {
5975 if (++i == hislen)
5976 i = 0;
5977 if (history[histtype][i].hisstr != NULL)
5978 ml_append(lnum++, history[histtype][i].hisstr,
5979 (colnr_T)0, FALSE);
5980 }
5981 while (i != hisidx[histtype]);
5982 }
5983 }
5984
5985 /* Replace the empty last line with the current command-line and put the
5986 * cursor there. */
5987 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5988 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5989 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005990 changed_line_abv_curs();
5991 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005992 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993
5994 /* Save the command line info, can be used recursively. */
5995 save_ccline = ccline;
5996 ccline.cmdbuff = NULL;
5997 ccline.cmdprompt = NULL;
5998
5999 /* No Ex mode here! */
6000 exmode_active = 0;
6001
6002 State = NORMAL;
6003# ifdef FEAT_MOUSE
6004 setmouse();
6005# endif
6006
6007# ifdef FEAT_AUTOCMD
6008 /* Trigger CmdwinEnter autocommands. */
6009 typestr[0] = cmdwin_type;
6010 typestr[1] = NUL;
6011 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006012 if (restart_edit != 0) /* autocmd with ":startinsert" */
6013 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014# endif
6015
6016 i = RedrawingDisabled;
6017 RedrawingDisabled = 0;
6018
6019 /*
6020 * Call the main loop until <CR> or CTRL-C is typed.
6021 */
6022 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006023 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024
6025 RedrawingDisabled = i;
6026
6027# ifdef FEAT_AUTOCMD
6028 /* Trigger CmdwinLeave autocommands. */
6029 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6030# endif
6031
Bram Moolenaarccc18222007-05-10 18:25:20 +00006032 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 ccline = save_ccline;
6034 cmdwin_type = 0;
6035
6036 exmode_active = save_exmode;
6037
6038 /* Safety check: The old window or buffer was deleted: It's a a bug when
6039 * this happens! */
6040 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6041 {
6042 cmdwin_result = Ctrl_C;
6043 EMSG(_("E199: Active window or buffer deleted"));
6044 }
6045 else
6046 {
6047# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6048 /* autocmds may abort script processing */
6049 if (aborting() && cmdwin_result != K_IGNORE)
6050 cmdwin_result = Ctrl_C;
6051# endif
6052 /* Set the new command line from the cmdline buffer. */
6053 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006054 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006056 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6057
6058 if (histtype == HIST_CMD)
6059 {
6060 /* Execute the command directly. */
6061 ccline.cmdbuff = vim_strsave((char_u *)p);
6062 cmdwin_result = CAR;
6063 }
6064 else
6065 {
6066 /* First need to cancel what we were doing. */
6067 ccline.cmdbuff = NULL;
6068 stuffcharReadbuff(':');
6069 stuffReadbuff((char_u *)p);
6070 stuffcharReadbuff(CAR);
6071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 }
6073 else if (cmdwin_result == K_XF2) /* :qa typed */
6074 {
6075 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6076 cmdwin_result = CAR;
6077 }
6078 else
6079 ccline.cmdbuff = vim_strsave(ml_get_curline());
6080 if (ccline.cmdbuff == NULL)
6081 cmdwin_result = Ctrl_C;
6082 else
6083 {
6084 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6085 ccline.cmdbufflen = ccline.cmdlen + 1;
6086 ccline.cmdpos = curwin->w_cursor.col;
6087 if (ccline.cmdpos > ccline.cmdlen)
6088 ccline.cmdpos = ccline.cmdlen;
6089 if (cmdwin_result == K_IGNORE)
6090 {
6091 set_cmdspos_cursor();
6092 redrawcmd();
6093 }
6094 }
6095
6096# ifdef FEAT_AUTOCMD
6097 /* Don't execute autocommands while deleting the window. */
6098 ++autocmd_block;
6099# endif
6100 wp = curwin;
6101 bp = curbuf;
6102 win_goto(old_curwin);
6103 win_close(wp, TRUE);
6104 close_buffer(NULL, bp, DOBUF_WIPE);
6105
6106 /* Restore window sizes. */
6107 win_size_restore(&winsizes);
6108
6109# ifdef FEAT_AUTOCMD
6110 --autocmd_block;
6111# endif
6112 }
6113
6114 ga_clear(&winsizes);
6115 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006116# ifdef FEAT_RIGHTLEFT
6117 cmdmsg_rl = save_cmdmsg_rl;
6118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
6120 State = save_State;
6121# ifdef FEAT_MOUSE
6122 setmouse();
6123# endif
6124
6125 return cmdwin_result;
6126}
6127#endif /* FEAT_CMDWIN */
6128
6129/*
6130 * Used for commands that either take a simple command string argument, or:
6131 * cmd << endmarker
6132 * {script}
6133 * endmarker
6134 * Returns a pointer to allocated memory with {script} or NULL.
6135 */
6136 char_u *
6137script_get(eap, cmd)
6138 exarg_T *eap;
6139 char_u *cmd;
6140{
6141 char_u *theline;
6142 char *end_pattern = NULL;
6143 char dot[] = ".";
6144 garray_T ga;
6145
6146 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6147 return NULL;
6148
6149 ga_init2(&ga, 1, 0x400);
6150
6151 if (cmd[2] != NUL)
6152 end_pattern = (char *)skipwhite(cmd + 2);
6153 else
6154 end_pattern = dot;
6155
6156 for (;;)
6157 {
6158 theline = eap->getline(
6159#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006160 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161#endif
6162 NUL, eap->cookie, 0);
6163
6164 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6165 break;
6166
6167 ga_concat(&ga, theline);
6168 ga_append(&ga, '\n');
6169 vim_free(theline);
6170 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006171 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172
6173 return (char_u *)ga.ga_data;
6174}