Mercurial > vim
annotate src/ex_getln.c @ 3848:a2fe24ec6a0d v7.3.681
updated for version 7.3.681
Problem: List of distributed files picks up backup files.
Solution: Make tutor patterns more specific.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 03 Oct 2012 21:48:43 +0200 |
parents | 99f076ca8d84 |
children | ba9c9d0c4ff4 |
rev | line source |
---|---|
7 | 1 /* 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 */ | |
21 struct 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 */ | |
3503 | 28 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */ |
7 | 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(). */ | |
1718 | 34 expand_T *xpc; /* struct being used for expansion, xp_pattern |
35 may point into cmdbuff */ | |
531 | 36 int xp_context; /* type of expansion */ |
37 # ifdef FEAT_EVAL | |
38 char_u *xp_arg; /* user-defined expansion arg */ | |
1039 | 39 int input_fn; /* when TRUE Invoked for input() function */ |
531 | 40 # endif |
7 | 41 }; |
42 | |
1718 | 43 /* The current cmdline_info. It is initialized in getcmdline() and after that |
44 * used by other functions. When invoking getcmdline() recursively it needs | |
45 * to be saved with save_cmdline() and restored with restore_cmdline(). | |
46 * TODO: make it local to getcmdline() and pass it around. */ | |
47 static struct cmdline_info ccline; | |
7 | 48 |
49 static int cmd_showtail; /* Only show path tail in lists ? */ | |
50 | |
51 #ifdef FEAT_EVAL | |
52 static int new_cmdpos; /* position set by set_cmdline_pos() */ | |
53 #endif | |
54 | |
55 #ifdef FEAT_CMDHIST | |
56 typedef struct hist_entry | |
57 { | |
58 int hisnum; /* identifying number */ | |
59 char_u *hisstr; /* actual entry, separator char after the NUL */ | |
60 } histentry_T; | |
61 | |
62 static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; | |
63 static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */ | |
64 static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; | |
65 /* identifying (unique) number of newest history entry */ | |
66 static int hislen = 0; /* actual length of history tables */ | |
67 | |
68 static int hist_char2type __ARGS((int c)); | |
69 | |
2986 | 70 static int in_history __ARGS((int, char_u *, int, int)); |
7 | 71 # ifdef FEAT_EVAL |
72 static int calc_hist_idx __ARGS((int histype, int num)); | |
73 # endif | |
74 #endif | |
75 | |
76 #ifdef FEAT_RIGHTLEFT | |
77 static int cmd_hkmap = 0; /* Hebrew mapping during command line */ | |
78 #endif | |
79 | |
80 #ifdef FEAT_FKMAP | |
81 static int cmd_fkmap = 0; /* Farsi mapping during command line */ | |
82 #endif | |
83 | |
84 static int cmdline_charsize __ARGS((int idx)); | |
85 static void set_cmdspos __ARGS((void)); | |
86 static void set_cmdspos_cursor __ARGS((void)); | |
87 #ifdef FEAT_MBYTE | |
88 static void correct_cmdspos __ARGS((int idx, int cells)); | |
89 #endif | |
90 static void alloc_cmdbuff __ARGS((int len)); | |
91 static int realloc_cmdbuff __ARGS((int len)); | |
92 static void draw_cmdline __ARGS((int start, int len)); | |
95 | 93 static void save_cmdline __ARGS((struct cmdline_info *ccp)); |
94 static void restore_cmdline __ARGS((struct cmdline_info *ccp)); | |
1015 | 95 static int cmdline_paste __ARGS((int regname, int literally, int remcr)); |
7 | 96 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
97 static void redrawcmd_preedit __ARGS((void)); | |
98 #endif | |
99 #ifdef FEAT_WILDMENU | |
100 static void cmdline_del __ARGS((int from)); | |
101 #endif | |
102 static void redrawcmdprompt __ARGS((void)); | |
103 static void cursorcmd __ARGS((void)); | |
104 static int ccheck_abbr __ARGS((int)); | |
105 static int nextwild __ARGS((expand_T *xp, int type, int options)); | |
435 | 106 static void escape_fname __ARGS((char_u **pp)); |
7 | 107 static int showmatches __ARGS((expand_T *xp, int wildmenu)); |
108 static void set_expand_context __ARGS((expand_T *xp)); | |
109 static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int)); | |
110 static int expand_showtail __ARGS((expand_T *xp)); | |
111 #ifdef FEAT_CMDL_COMPL | |
716 | 112 static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg)); |
2929 | 113 static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname[])); |
3503 | 114 # ifdef FEAT_CMDHIST |
115 static char_u *get_history_arg __ARGS((expand_T *xp, int idx)); | |
116 # endif | |
7 | 117 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
118 static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file)); | |
407 | 119 static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file)); |
7 | 120 # endif |
121 #endif | |
122 | |
123 #ifdef FEAT_CMDWIN | |
124 static int ex_window __ARGS((void)); | |
125 #endif | |
126 | |
3164 | 127 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
128 static int | |
129 #ifdef __BORLANDC__ | |
130 _RTLENTRYF | |
131 #endif | |
132 sort_func_compare __ARGS((const void *s1, const void *s2)); | |
133 #endif | |
134 | |
7 | 135 /* |
136 * getcmdline() - accept a command line starting with firstc. | |
137 * | |
138 * firstc == ':' get ":" command line. | |
139 * firstc == '/' or '?' get search pattern | |
140 * firstc == '=' get expression | |
141 * firstc == '@' get text for input() function | |
142 * firstc == '>' get text for debug mode | |
143 * firstc == NUL get text for :insert command | |
144 * firstc == -1 like NUL, and break on CTRL-C | |
145 * | |
146 * The line is collected in ccline.cmdbuff, which is reallocated to fit the | |
147 * command line. | |
148 * | |
149 * Careful: getcmdline() can be called recursively! | |
150 * | |
151 * Return pointer to allocated string if there is a commandline, NULL | |
152 * otherwise. | |
153 */ | |
154 char_u * | |
155 getcmdline(firstc, count, indent) | |
156 int firstc; | |
1877 | 157 long count UNUSED; /* only used for incremental search */ |
7 | 158 int indent; /* indent for inside conditionals */ |
159 { | |
160 int c; | |
161 int i; | |
162 int j; | |
163 int gotesc = FALSE; /* TRUE when <ESC> just typed */ | |
164 int do_abbr; /* when TRUE check for abbr. */ | |
165 #ifdef FEAT_CMDHIST | |
166 char_u *lookfor = NULL; /* string to match */ | |
167 int hiscnt; /* current history line in use */ | |
168 int histype; /* history type to be used */ | |
169 #endif | |
170 #ifdef FEAT_SEARCH_EXTRA | |
171 pos_T old_cursor; | |
172 colnr_T old_curswant; | |
173 colnr_T old_leftcol; | |
174 linenr_T old_topline; | |
175 # ifdef FEAT_DIFF | |
176 int old_topfill; | |
177 # endif | |
178 linenr_T old_botline; | |
179 int did_incsearch = FALSE; | |
180 int incsearch_postponed = FALSE; | |
181 #endif | |
182 int did_wild_list = FALSE; /* did wild_list() recently */ | |
183 int wim_index = 0; /* index in wim_flags[] */ | |
184 int res; | |
185 int save_msg_scroll = msg_scroll; | |
186 int save_State = State; /* remember State when called */ | |
187 int some_key_typed = FALSE; /* one of the keys was typed */ | |
188 #ifdef FEAT_MOUSE | |
189 /* mouse drag and release events are ignored, unless they are | |
190 * preceded with a mouse down event */ | |
191 int ignore_drag_release = TRUE; | |
192 #endif | |
193 #ifdef FEAT_EVAL | |
194 int break_ctrl_c = FALSE; | |
195 #endif | |
196 expand_T xpc; | |
197 long *b_im_ptr = NULL; | |
195 | 198 #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA) |
199 /* Everything that may work recursively should save and restore the | |
200 * current command line in save_ccline. That includes update_screen(), a | |
201 * custom status line may invoke ":normal". */ | |
202 struct cmdline_info save_ccline; | |
203 #endif | |
7 | 204 |
205 #ifdef FEAT_SNIFF | |
206 want_sniff_request = 0; | |
207 #endif | |
208 #ifdef FEAT_EVAL | |
209 if (firstc == -1) | |
210 { | |
211 firstc = NUL; | |
212 break_ctrl_c = TRUE; | |
213 } | |
214 #endif | |
215 #ifdef FEAT_RIGHTLEFT | |
216 /* start without Hebrew mapping for a command line */ | |
217 if (firstc == ':' || firstc == '=' || firstc == '>') | |
218 cmd_hkmap = 0; | |
219 #endif | |
220 | |
221 ccline.overstrike = FALSE; /* always start in insert mode */ | |
222 #ifdef FEAT_SEARCH_EXTRA | |
223 old_cursor = curwin->w_cursor; /* needs to be restored later */ | |
224 old_curswant = curwin->w_curswant; | |
225 old_leftcol = curwin->w_leftcol; | |
226 old_topline = curwin->w_topline; | |
227 # ifdef FEAT_DIFF | |
228 old_topfill = curwin->w_topfill; | |
229 # endif | |
230 old_botline = curwin->w_botline; | |
231 #endif | |
232 | |
233 /* | |
234 * set some variables for redrawcmd() | |
235 */ | |
236 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); | |
164 | 237 ccline.cmdindent = (firstc > 0 ? indent : 0); |
238 | |
239 /* alloc initial ccline.cmdbuff */ | |
240 alloc_cmdbuff(exmode_active ? 250 : indent + 1); | |
7 | 241 if (ccline.cmdbuff == NULL) |
242 return NULL; /* out of memory */ | |
243 ccline.cmdlen = ccline.cmdpos = 0; | |
244 ccline.cmdbuff[0] = NUL; | |
245 | |
164 | 246 /* autoindent for :insert and :append */ |
247 if (firstc <= 0) | |
248 { | |
249 copy_spaces(ccline.cmdbuff, indent); | |
250 ccline.cmdbuff[indent] = NUL; | |
251 ccline.cmdpos = indent; | |
252 ccline.cmdspos = indent; | |
253 ccline.cmdlen = indent; | |
254 } | |
255 | |
7 | 256 ExpandInit(&xpc); |
1718 | 257 ccline.xpc = &xpc; |
7 | 258 |
259 #ifdef FEAT_RIGHTLEFT | |
260 if (curwin->w_p_rl && *curwin->w_p_rlc == 's' | |
261 && (firstc == '/' || firstc == '?')) | |
262 cmdmsg_rl = TRUE; | |
263 else | |
264 cmdmsg_rl = FALSE; | |
265 #endif | |
266 | |
267 redir_off = TRUE; /* don't redirect the typed command */ | |
268 if (!cmd_silent) | |
269 { | |
270 i = msg_scrolled; | |
271 msg_scrolled = 0; /* avoid wait_return message */ | |
272 gotocmdline(TRUE); | |
273 msg_scrolled += i; | |
274 redrawcmdprompt(); /* draw prompt or indent */ | |
275 set_cmdspos(); | |
276 } | |
277 xpc.xp_context = EXPAND_NOTHING; | |
278 xpc.xp_backslash = XP_BS_NONE; | |
632 | 279 #ifndef BACKSLASH_IN_FILENAME |
280 xpc.xp_shell = FALSE; | |
281 #endif | |
7 | 282 |
531 | 283 #if defined(FEAT_EVAL) |
284 if (ccline.input_fn) | |
285 { | |
286 xpc.xp_context = ccline.xp_context; | |
287 xpc.xp_pattern = ccline.cmdbuff; | |
1322 | 288 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
531 | 289 xpc.xp_arg = ccline.xp_arg; |
1322 | 290 # endif |
531 | 291 } |
292 #endif | |
293 | |
7 | 294 /* |
295 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when | |
296 * doing ":@0" when register 0 doesn't contain a CR. | |
297 */ | |
298 msg_scroll = FALSE; | |
299 | |
300 State = CMDLINE; | |
301 | |
302 if (firstc == '/' || firstc == '?' || firstc == '@') | |
303 { | |
304 /* Use ":lmap" mappings for search pattern and input(). */ | |
305 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) | |
306 b_im_ptr = &curbuf->b_p_iminsert; | |
307 else | |
308 b_im_ptr = &curbuf->b_p_imsearch; | |
309 if (*b_im_ptr == B_IMODE_LMAP) | |
310 State |= LANGMAP; | |
311 #ifdef USE_IM_CONTROL | |
312 im_set_active(*b_im_ptr == B_IMODE_IM); | |
313 #endif | |
314 } | |
315 #ifdef USE_IM_CONTROL | |
316 else if (p_imcmdline) | |
317 im_set_active(TRUE); | |
318 #endif | |
319 | |
320 #ifdef FEAT_MOUSE | |
321 setmouse(); | |
322 #endif | |
323 #ifdef CURSOR_SHAPE | |
324 ui_cursor_shape(); /* may show different cursor shape */ | |
325 #endif | |
326 | |
571 | 327 /* When inside an autocommand for writing "exiting" may be set and |
328 * terminal mode set to cooked. Need to set raw mode here then. */ | |
329 settmode(TMODE_RAW); | |
330 | |
7 | 331 #ifdef FEAT_CMDHIST |
332 init_history(); | |
333 hiscnt = hislen; /* set hiscnt to impossible history value */ | |
334 histype = hist_char2type(firstc); | |
335 #endif | |
336 | |
337 #ifdef FEAT_DIGRAPHS | |
1868 | 338 do_digraph(-1); /* init digraph typeahead */ |
7 | 339 #endif |
340 | |
341 /* | |
342 * Collect the command string, handling editing keys. | |
343 */ | |
344 for (;;) | |
345 { | |
972 | 346 redir_off = TRUE; /* Don't redirect the typed command. |
347 Repeated, because a ":redir" inside | |
348 completion may switch it on. */ | |
7 | 349 #ifdef USE_ON_FLY_SCROLL |
350 dont_scroll = FALSE; /* allow scrolling here */ | |
351 #endif | |
352 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ | |
353 | |
354 cursorcmd(); /* set the cursor on the right spot */ | |
1472 | 355 |
356 /* Get a character. Ignore K_IGNORE, it should not do anything, such | |
357 * as stop completion. */ | |
358 do | |
359 { | |
360 c = safe_vgetc(); | |
361 } while (c == K_IGNORE); | |
362 | |
7 | 363 if (KeyTyped) |
364 { | |
365 some_key_typed = TRUE; | |
366 #ifdef FEAT_RIGHTLEFT | |
367 if (cmd_hkmap) | |
368 c = hkmap(c); | |
369 # ifdef FEAT_FKMAP | |
370 if (cmd_fkmap) | |
371 c = cmdl_fkmap(c); | |
372 # endif | |
373 if (cmdmsg_rl && !KeyStuffed) | |
374 { | |
375 /* Invert horizontal movements and operations. Only when | |
376 * typed by the user directly, not when the result of a | |
377 * mapping. */ | |
378 switch (c) | |
379 { | |
380 case K_RIGHT: c = K_LEFT; break; | |
381 case K_S_RIGHT: c = K_S_LEFT; break; | |
382 case K_C_RIGHT: c = K_C_LEFT; break; | |
383 case K_LEFT: c = K_RIGHT; break; | |
384 case K_S_LEFT: c = K_S_RIGHT; break; | |
385 case K_C_LEFT: c = K_C_RIGHT; break; | |
386 } | |
387 } | |
388 #endif | |
389 } | |
390 | |
391 /* | |
392 * Ignore got_int when CTRL-C was typed here. | |
393 * Don't ignore it in :global, we really need to break then, e.g., for | |
394 * ":g/pat/normal /pat" (without the <CR>). | |
395 * Don't ignore it for the input() function. | |
396 */ | |
397 if ((c == Ctrl_C | |
398 #ifdef UNIX | |
399 || c == intr_char | |
400 #endif | |
401 ) | |
402 #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) | |
403 && firstc != '@' | |
404 #endif | |
405 #ifdef FEAT_EVAL | |
406 && !break_ctrl_c | |
407 #endif | |
408 && !global_busy) | |
409 got_int = FALSE; | |
410 | |
411 #ifdef FEAT_CMDHIST | |
412 /* free old command line when finished moving around in the history | |
413 * list */ | |
414 if (lookfor != NULL | |
180 | 415 && c != K_S_DOWN && c != K_S_UP |
230 | 416 && c != K_DOWN && c != K_UP |
7 | 417 && c != K_PAGEDOWN && c != K_PAGEUP |
418 && c != K_KPAGEDOWN && c != K_KPAGEUP | |
230 | 419 && c != K_LEFT && c != K_RIGHT |
7 | 420 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
421 { | |
422 vim_free(lookfor); | |
423 lookfor = NULL; | |
424 } | |
425 #endif | |
426 | |
427 /* | |
1718 | 428 * When there are matching completions to select <S-Tab> works like |
429 * CTRL-P (unless 'wc' is <S-Tab>). | |
7 | 430 */ |
1718 | 431 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
7 | 432 c = Ctrl_P; |
433 | |
434 #ifdef FEAT_WILDMENU | |
435 /* Special translations for 'wildmenu' */ | |
436 if (did_wild_list && p_wmnu) | |
437 { | |
230 | 438 if (c == K_LEFT) |
7 | 439 c = Ctrl_P; |
230 | 440 else if (c == K_RIGHT) |
7 | 441 c = Ctrl_N; |
442 } | |
443 /* Hitting CR after "emenu Name.": complete submenu */ | |
444 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu | |
445 && ccline.cmdpos > 1 | |
446 && ccline.cmdbuff[ccline.cmdpos - 1] == '.' | |
447 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' | |
448 && (c == '\n' || c == '\r' || c == K_KENTER)) | |
449 c = K_DOWN; | |
450 #endif | |
451 | |
452 /* free expanded names when finished walking through matches */ | |
453 if (xpc.xp_numfiles != -1 | |
454 && !(c == p_wc && KeyTyped) && c != p_wcm | |
455 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A | |
456 && c != Ctrl_L) | |
457 { | |
458 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); | |
459 did_wild_list = FALSE; | |
460 #ifdef FEAT_WILDMENU | |
230 | 461 if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
7 | 462 #endif |
463 xpc.xp_context = EXPAND_NOTHING; | |
464 wim_index = 0; | |
465 #ifdef FEAT_WILDMENU | |
466 if (p_wmnu && wild_menu_showing != 0) | |
467 { | |
468 int skt = KeyTyped; | |
532 | 469 int old_RedrawingDisabled = RedrawingDisabled; |
531 | 470 |
471 if (ccline.input_fn) | |
472 RedrawingDisabled = 0; | |
7 | 473 |
474 if (wild_menu_showing == WM_SCROLLED) | |
475 { | |
476 /* Entered command line, move it up */ | |
477 cmdline_row--; | |
478 redrawcmd(); | |
479 } | |
480 else if (save_p_ls != -1) | |
481 { | |
482 /* restore 'laststatus' and 'winminheight' */ | |
483 p_ls = save_p_ls; | |
484 p_wmh = save_p_wmh; | |
485 last_status(FALSE); | |
195 | 486 save_cmdline(&save_ccline); |
7 | 487 update_screen(VALID); /* redraw the screen NOW */ |
195 | 488 restore_cmdline(&save_ccline); |
7 | 489 redrawcmd(); |
490 save_p_ls = -1; | |
491 } | |
492 else | |
493 { | |
494 # ifdef FEAT_VERTSPLIT | |
495 win_redraw_last_status(topframe); | |
496 # else | |
497 lastwin->w_redr_status = TRUE; | |
498 # endif | |
499 redraw_statuslines(); | |
500 } | |
532 | 501 KeyTyped = skt; |
502 wild_menu_showing = 0; | |
531 | 503 if (ccline.input_fn) |
504 RedrawingDisabled = old_RedrawingDisabled; | |
7 | 505 } |
506 #endif | |
507 } | |
508 | |
509 #ifdef FEAT_WILDMENU | |
510 /* Special translations for 'wildmenu' */ | |
511 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) | |
512 { | |
513 /* Hitting <Down> after "emenu Name.": complete submenu */ | |
1318 | 514 if (c == K_DOWN && ccline.cmdpos > 0 |
515 && ccline.cmdbuff[ccline.cmdpos - 1] == '.') | |
7 | 516 c = p_wc; |
230 | 517 else if (c == K_UP) |
7 | 518 { |
519 /* Hitting <Up>: Remove one submenu name in front of the | |
520 * cursor */ | |
521 int found = FALSE; | |
522 | |
523 j = (int)(xpc.xp_pattern - ccline.cmdbuff); | |
524 i = 0; | |
525 while (--j > 0) | |
526 { | |
527 /* check for start of menu name */ | |
528 if (ccline.cmdbuff[j] == ' ' | |
529 && ccline.cmdbuff[j - 1] != '\\') | |
530 { | |
531 i = j + 1; | |
532 break; | |
533 } | |
534 /* check for start of submenu name */ | |
535 if (ccline.cmdbuff[j] == '.' | |
536 && ccline.cmdbuff[j - 1] != '\\') | |
537 { | |
538 if (found) | |
539 { | |
540 i = j + 1; | |
541 break; | |
542 } | |
543 else | |
544 found = TRUE; | |
545 } | |
546 } | |
547 if (i > 0) | |
548 cmdline_del(i); | |
549 c = p_wc; | |
550 xpc.xp_context = EXPAND_NOTHING; | |
551 } | |
552 } | |
714 | 553 if ((xpc.xp_context == EXPAND_FILES |
1621 | 554 || xpc.xp_context == EXPAND_DIRECTORIES |
714 | 555 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
7 | 556 { |
557 char_u upseg[5]; | |
558 | |
559 upseg[0] = PATHSEP; | |
560 upseg[1] = '.'; | |
561 upseg[2] = '.'; | |
562 upseg[3] = PATHSEP; | |
563 upseg[4] = NUL; | |
564 | |
1318 | 565 if (c == K_DOWN |
566 && ccline.cmdpos > 0 | |
567 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP | |
568 && (ccline.cmdpos < 3 | |
569 || ccline.cmdbuff[ccline.cmdpos - 2] != '.' | |
7 | 570 || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
571 { | |
572 /* go down a directory */ | |
573 c = p_wc; | |
574 } | |
230 | 575 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN) |
7 | 576 { |
577 /* If in a direct ancestor, strip off one ../ to go down */ | |
578 int found = FALSE; | |
579 | |
580 j = ccline.cmdpos; | |
581 i = (int)(xpc.xp_pattern - ccline.cmdbuff); | |
582 while (--j > i) | |
583 { | |
39 | 584 #ifdef FEAT_MBYTE |
585 if (has_mbyte) | |
586 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); | |
587 #endif | |
7 | 588 if (vim_ispathsep(ccline.cmdbuff[j])) |
589 { | |
590 found = TRUE; | |
591 break; | |
592 } | |
593 } | |
594 if (found | |
595 && ccline.cmdbuff[j - 1] == '.' | |
596 && ccline.cmdbuff[j - 2] == '.' | |
597 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) | |
598 { | |
599 cmdline_del(j - 2); | |
600 c = p_wc; | |
601 } | |
602 } | |
230 | 603 else if (c == K_UP) |
7 | 604 { |
605 /* go up a directory */ | |
606 int found = FALSE; | |
607 | |
608 j = ccline.cmdpos - 1; | |
609 i = (int)(xpc.xp_pattern - ccline.cmdbuff); | |
610 while (--j > i) | |
611 { | |
612 #ifdef FEAT_MBYTE | |
613 if (has_mbyte) | |
614 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); | |
615 #endif | |
616 if (vim_ispathsep(ccline.cmdbuff[j]) | |
617 #ifdef BACKSLASH_IN_FILENAME | |
618 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1]) | |
619 == NULL | |
620 #endif | |
621 ) | |
622 { | |
623 if (found) | |
624 { | |
625 i = j + 1; | |
626 break; | |
627 } | |
628 else | |
629 found = TRUE; | |
630 } | |
631 } | |
632 | |
633 if (!found) | |
634 j = i; | |
635 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) | |
636 j += 4; | |
637 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 | |
638 && j == i) | |
639 j += 3; | |
640 else | |
641 j = 0; | |
642 if (j > 0) | |
643 { | |
644 /* TODO this is only for DOS/UNIX systems - need to put in | |
645 * machine-specific stuff here and in upseg init */ | |
646 cmdline_del(j); | |
647 put_on_cmdline(upseg + 1, 3, FALSE); | |
648 } | |
649 else if (ccline.cmdpos > i) | |
650 cmdline_del(i); | |
3204 | 651 |
652 /* Now complete in the new directory. Set KeyTyped in case the | |
653 * Up key came from a mapping. */ | |
7 | 654 c = p_wc; |
3204 | 655 KeyTyped = TRUE; |
7 | 656 } |
657 } | |
658 | |
659 #endif /* FEAT_WILDMENU */ | |
660 | |
661 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert | |
662 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ | |
663 if (c == Ctrl_BSL) | |
664 { | |
665 ++no_mapping; | |
666 ++allow_keys; | |
1389 | 667 c = plain_vgetc(); |
7 | 668 --no_mapping; |
669 --allow_keys; | |
670 /* CTRL-\ e doesn't work when obtaining an expression. */ | |
671 if (c != Ctrl_N && c != Ctrl_G | |
672 && (c != 'e' || ccline.cmdfirstc == '=')) | |
673 { | |
674 vungetc(c); | |
675 c = Ctrl_BSL; | |
676 } | |
677 #ifdef FEAT_EVAL | |
678 else if (c == 'e') | |
679 { | |
2557
029ace8dff7d
Now really fix using expressions in the command line (hopefully).
Bram Moolenaar <bram@vim.org>
parents:
2556
diff
changeset
|
680 char_u *p = NULL; |
029ace8dff7d
Now really fix using expressions in the command line (hopefully).
Bram Moolenaar <bram@vim.org>
parents:
2556
diff
changeset
|
681 int len; |
7 | 682 |
683 /* | |
684 * Replace the command line with the result of an expression. | |
625 | 685 * Need to save and restore the current command line, to be |
686 * able to enter a new one... | |
7 | 687 */ |
688 if (ccline.cmdpos == ccline.cmdlen) | |
689 new_cmdpos = 99999; /* keep it at the end */ | |
690 else | |
691 new_cmdpos = ccline.cmdpos; | |
95 | 692 |
693 save_cmdline(&save_ccline); | |
7 | 694 c = get_expr_register(); |
95 | 695 restore_cmdline(&save_ccline); |
7 | 696 if (c == '=') |
697 { | |
634 | 698 /* Need to save and restore ccline. And set "textlock" |
632 | 699 * to avoid nasty things like going to another buffer when |
700 * evaluating an expression. */ | |
95 | 701 save_cmdline(&save_ccline); |
634 | 702 ++textlock; |
7 | 703 p = get_expr_line(); |
634 | 704 --textlock; |
95 | 705 restore_cmdline(&save_ccline); |
2617 | 706 |
707 if (p != NULL) | |
7 | 708 { |
2617 | 709 len = (int)STRLEN(p); |
710 if (realloc_cmdbuff(len + 1) == OK) | |
711 { | |
712 ccline.cmdlen = len; | |
713 STRCPY(ccline.cmdbuff, p); | |
714 vim_free(p); | |
715 | |
716 /* Restore the cursor or use the position set with | |
717 * set_cmdline_pos(). */ | |
718 if (new_cmdpos > ccline.cmdlen) | |
719 ccline.cmdpos = ccline.cmdlen; | |
720 else | |
721 ccline.cmdpos = new_cmdpos; | |
722 | |
723 KeyTyped = FALSE; /* Don't do p_wc completion. */ | |
724 redrawcmd(); | |
725 goto cmdline_changed; | |
726 } | |
7 | 727 } |
728 } | |
729 beep_flush(); | |
2636 | 730 got_int = FALSE; /* don't abandon the command line */ |
731 did_emsg = FALSE; | |
732 emsg_on_display = FALSE; | |
733 redrawcmd(); | |
734 goto cmdline_not_changed; | |
7 | 735 } |
736 #endif | |
737 else | |
738 { | |
739 if (c == Ctrl_G && p_im && restart_edit == 0) | |
740 restart_edit = 'a'; | |
741 gotesc = TRUE; /* will free ccline.cmdbuff after putting it | |
742 in history */ | |
743 goto returncmd; /* back to Normal mode */ | |
744 } | |
745 } | |
746 | |
747 #ifdef FEAT_CMDWIN | |
748 if (c == cedit_key || c == K_CMDWIN) | |
749 { | |
750 /* | |
751 * Open a window to edit the command line (and history). | |
752 */ | |
753 c = ex_window(); | |
754 some_key_typed = TRUE; | |
755 } | |
756 # ifdef FEAT_DIGRAPHS | |
757 else | |
758 # endif | |
759 #endif | |
760 #ifdef FEAT_DIGRAPHS | |
761 c = do_digraph(c); | |
762 #endif | |
763 | |
764 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC | |
765 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) | |
766 { | |
168 | 767 /* In Ex mode a backslash escapes a newline. */ |
768 if (exmode_active | |
769 && c != ESC | |
1318 | 770 && ccline.cmdpos == ccline.cmdlen |
168 | 771 && ccline.cmdpos > 0 |
772 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') | |
773 { | |
774 if (c == K_KENTER) | |
775 c = '\n'; | |
776 } | |
777 else | |
7 | 778 { |
168 | 779 gotesc = FALSE; /* Might have typed ESC previously, don't |
780 truncate the cmdline now. */ | |
781 if (ccheck_abbr(c + ABBR_OFF)) | |
782 goto cmdline_changed; | |
783 if (!cmd_silent) | |
784 { | |
785 windgoto(msg_row, 0); | |
786 out_flush(); | |
787 } | |
788 break; | |
7 | 789 } |
790 } | |
791 | |
792 /* | |
793 * Completion for 'wildchar' or 'wildcharm' key. | |
794 * - hitting <ESC> twice means: abandon command line. | |
795 * - wildcard expansion is only done when the 'wildchar' key is really | |
796 * typed, not when it comes from a macro | |
797 */ | |
798 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) | |
799 { | |
800 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ | |
801 { | |
802 /* if 'wildmode' contains "list" may still need to list */ | |
803 if (xpc.xp_numfiles > 1 | |
804 && !did_wild_list | |
805 && (wim_flags[wim_index] & WIM_LIST)) | |
806 { | |
807 (void)showmatches(&xpc, FALSE); | |
808 redrawcmd(); | |
809 did_wild_list = TRUE; | |
810 } | |
811 if (wim_flags[wim_index] & WIM_LONGEST) | |
812 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP); | |
813 else if (wim_flags[wim_index] & WIM_FULL) | |
814 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP); | |
815 else | |
816 res = OK; /* don't insert 'wildchar' now */ | |
817 } | |
818 else /* typed p_wc first time */ | |
819 { | |
820 wim_index = 0; | |
821 j = ccline.cmdpos; | |
822 /* if 'wildmode' first contains "longest", get longest | |
823 * common part */ | |
824 if (wim_flags[0] & WIM_LONGEST) | |
825 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP); | |
826 else | |
827 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP); | |
828 | |
829 /* if interrupted while completing, behave like it failed */ | |
830 if (got_int) | |
831 { | |
832 (void)vpeekc(); /* remove <C-C> from input stream */ | |
833 got_int = FALSE; /* don't abandon the command line */ | |
834 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); | |
835 #ifdef FEAT_WILDMENU | |
836 xpc.xp_context = EXPAND_NOTHING; | |
837 #endif | |
838 goto cmdline_changed; | |
839 } | |
840 | |
841 /* when more than one match, and 'wildmode' first contains | |
842 * "list", or no change and 'wildmode' contains "longest,list", | |
843 * list all matches */ | |
844 if (res == OK && xpc.xp_numfiles > 1) | |
845 { | |
846 /* a "longest" that didn't do anything is skipped (but not | |
847 * "list:longest") */ | |
848 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) | |
849 wim_index = 1; | |
850 if ((wim_flags[wim_index] & WIM_LIST) | |
851 #ifdef FEAT_WILDMENU | |
852 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) | |
853 #endif | |
854 ) | |
855 { | |
856 if (!(wim_flags[0] & WIM_LONGEST)) | |
857 { | |
858 #ifdef FEAT_WILDMENU | |
859 int p_wmnu_save = p_wmnu; | |
860 p_wmnu = 0; | |
861 #endif | |
862 nextwild(&xpc, WILD_PREV, 0); /* remove match */ | |
863 #ifdef FEAT_WILDMENU | |
864 p_wmnu = p_wmnu_save; | |
865 #endif | |
866 } | |
867 #ifdef FEAT_WILDMENU | |
868 (void)showmatches(&xpc, p_wmnu | |
869 && ((wim_flags[wim_index] & WIM_LIST) == 0)); | |
870 #else | |
871 (void)showmatches(&xpc, FALSE); | |
872 #endif | |
873 redrawcmd(); | |
874 did_wild_list = TRUE; | |
875 if (wim_flags[wim_index] & WIM_LONGEST) | |
876 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP); | |
877 else if (wim_flags[wim_index] & WIM_FULL) | |
878 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP); | |
879 } | |
880 else | |
881 vim_beep(); | |
882 } | |
883 #ifdef FEAT_WILDMENU | |
884 else if (xpc.xp_numfiles == -1) | |
885 xpc.xp_context = EXPAND_NOTHING; | |
886 #endif | |
887 } | |
888 if (wim_index < 3) | |
889 ++wim_index; | |
890 if (c == ESC) | |
891 gotesc = TRUE; | |
892 if (res == OK) | |
893 goto cmdline_changed; | |
894 } | |
895 | |
896 gotesc = FALSE; | |
897 | |
898 /* <S-Tab> goes to last match, in a clumsy way */ | |
899 if (c == K_S_TAB && KeyTyped) | |
900 { | |
901 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK | |
902 && nextwild(&xpc, WILD_PREV, 0) == OK | |
903 && nextwild(&xpc, WILD_PREV, 0) == OK) | |
904 goto cmdline_changed; | |
905 } | |
906 | |
907 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ | |
908 c = NL; | |
909 | |
910 do_abbr = TRUE; /* default: check for abbreviation */ | |
911 | |
912 /* | |
913 * Big switch for a typed command line character. | |
914 */ | |
915 switch (c) | |
916 { | |
917 case K_BS: | |
918 case Ctrl_H: | |
919 case K_DEL: | |
920 case K_KDEL: | |
921 case Ctrl_W: | |
922 #ifdef FEAT_FKMAP | |
923 if (cmd_fkmap && c == K_BS) | |
924 c = K_DEL; | |
925 #endif | |
926 if (c == K_KDEL) | |
927 c = K_DEL; | |
928 | |
929 /* | |
930 * delete current character is the same as backspace on next | |
931 * character, except at end of line | |
932 */ | |
933 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) | |
934 ++ccline.cmdpos; | |
935 #ifdef FEAT_MBYTE | |
936 if (has_mbyte && c == K_DEL) | |
937 ccline.cmdpos += mb_off_next(ccline.cmdbuff, | |
938 ccline.cmdbuff + ccline.cmdpos); | |
939 #endif | |
940 if (ccline.cmdpos > 0) | |
941 { | |
942 char_u *p; | |
943 | |
944 j = ccline.cmdpos; | |
945 p = ccline.cmdbuff + j; | |
946 #ifdef FEAT_MBYTE | |
947 if (has_mbyte) | |
948 { | |
949 p = mb_prevptr(ccline.cmdbuff, p); | |
950 if (c == Ctrl_W) | |
951 { | |
952 while (p > ccline.cmdbuff && vim_isspace(*p)) | |
953 p = mb_prevptr(ccline.cmdbuff, p); | |
954 i = mb_get_class(p); | |
955 while (p > ccline.cmdbuff && mb_get_class(p) == i) | |
956 p = mb_prevptr(ccline.cmdbuff, p); | |
957 if (mb_get_class(p) != i) | |
474 | 958 p += (*mb_ptr2len)(p); |
7 | 959 } |
960 } | |
961 else | |
962 #endif | |
963 if (c == Ctrl_W) | |
964 { | |
965 while (p > ccline.cmdbuff && vim_isspace(p[-1])) | |
966 --p; | |
967 i = vim_iswordc(p[-1]); | |
968 while (p > ccline.cmdbuff && !vim_isspace(p[-1]) | |
969 && vim_iswordc(p[-1]) == i) | |
970 --p; | |
971 } | |
972 else | |
973 --p; | |
974 ccline.cmdpos = (int)(p - ccline.cmdbuff); | |
975 ccline.cmdlen -= j - ccline.cmdpos; | |
976 i = ccline.cmdpos; | |
977 while (i < ccline.cmdlen) | |
978 ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; | |
979 | |
980 /* Truncate at the end, required for multi-byte chars. */ | |
981 ccline.cmdbuff[ccline.cmdlen] = NUL; | |
982 redrawcmd(); | |
983 } | |
984 else if (ccline.cmdlen == 0 && c != Ctrl_W | |
985 && ccline.cmdprompt == NULL && indent == 0) | |
986 { | |
987 /* In ex and debug mode it doesn't make sense to return. */ | |
988 if (exmode_active | |
989 #ifdef FEAT_EVAL | |
990 || ccline.cmdfirstc == '>' | |
991 #endif | |
992 ) | |
993 goto cmdline_not_changed; | |
994 | |
995 vim_free(ccline.cmdbuff); /* no commandline to return */ | |
996 ccline.cmdbuff = NULL; | |
997 if (!cmd_silent) | |
998 { | |
999 #ifdef FEAT_RIGHTLEFT | |
1000 if (cmdmsg_rl) | |
1001 msg_col = Columns; | |
1002 else | |
1003 #endif | |
1004 msg_col = 0; | |
1005 msg_putchar(' '); /* delete ':' */ | |
1006 } | |
1007 redraw_cmdline = TRUE; | |
1008 goto returncmd; /* back to cmd mode */ | |
1009 } | |
1010 goto cmdline_changed; | |
1011 | |
1012 case K_INS: | |
1013 case K_KINS: | |
1014 #ifdef FEAT_FKMAP | |
1015 /* if Farsi mode set, we are in reverse insert mode - | |
1016 Do not change the mode */ | |
1017 if (cmd_fkmap) | |
1018 beep_flush(); | |
1019 else | |
1020 #endif | |
1021 ccline.overstrike = !ccline.overstrike; | |
1022 #ifdef CURSOR_SHAPE | |
1023 ui_cursor_shape(); /* may show different cursor shape */ | |
1024 #endif | |
1025 goto cmdline_not_changed; | |
1026 | |
1027 case Ctrl_HAT: | |
782 | 1028 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
7 | 1029 { |
1030 /* ":lmap" mappings exists, toggle use of mappings. */ | |
1031 State ^= LANGMAP; | |
1032 #ifdef USE_IM_CONTROL | |
1033 im_set_active(FALSE); /* Disable input method */ | |
1034 #endif | |
1035 if (b_im_ptr != NULL) | |
1036 { | |
1037 if (State & LANGMAP) | |
1038 *b_im_ptr = B_IMODE_LMAP; | |
1039 else | |
1040 *b_im_ptr = B_IMODE_NONE; | |
1041 } | |
1042 } | |
1043 #ifdef USE_IM_CONTROL | |
1044 else | |
1045 { | |
1046 /* There are no ":lmap" mappings, toggle IM. When | |
1047 * 'imdisable' is set don't try getting the status, it's | |
1048 * always off. */ | |
1049 if ((p_imdisable && b_im_ptr != NULL) | |
1050 ? *b_im_ptr == B_IMODE_IM : im_get_status()) | |
1051 { | |
1052 im_set_active(FALSE); /* Disable input method */ | |
1053 if (b_im_ptr != NULL) | |
1054 *b_im_ptr = B_IMODE_NONE; | |
1055 } | |
1056 else | |
1057 { | |
1058 im_set_active(TRUE); /* Enable input method */ | |
1059 if (b_im_ptr != NULL) | |
1060 *b_im_ptr = B_IMODE_IM; | |
1061 } | |
1062 } | |
1063 #endif | |
1064 if (b_im_ptr != NULL) | |
1065 { | |
1066 if (b_im_ptr == &curbuf->b_p_iminsert) | |
1067 set_iminsert_global(); | |
1068 else | |
1069 set_imsearch_global(); | |
1070 } | |
1071 #ifdef CURSOR_SHAPE | |
1072 ui_cursor_shape(); /* may show different cursor shape */ | |
1073 #endif | |
1074 #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) | |
1075 /* Show/unshow value of 'keymap' in status lines later. */ | |
1076 status_redraw_curbuf(); | |
1077 #endif | |
1078 goto cmdline_not_changed; | |
1079 | |
1080 /* case '@': only in very old vi */ | |
1081 case Ctrl_U: | |
1082 /* delete all characters left of the cursor */ | |
1083 j = ccline.cmdpos; | |
1084 ccline.cmdlen -= j; | |
1085 i = ccline.cmdpos = 0; | |
1086 while (i < ccline.cmdlen) | |
1087 ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; | |
1088 /* Truncate at the end, required for multi-byte chars. */ | |
1089 ccline.cmdbuff[ccline.cmdlen] = NUL; | |
1090 redrawcmd(); | |
1091 goto cmdline_changed; | |
1092 | |
1093 #ifdef FEAT_CLIPBOARD | |
1094 case Ctrl_Y: | |
1095 /* Copy the modeless selection, if there is one. */ | |
1096 if (clip_star.state != SELECT_CLEARED) | |
1097 { | |
1098 if (clip_star.state == SELECT_DONE) | |
1099 clip_copy_modeless_selection(TRUE); | |
1100 goto cmdline_not_changed; | |
1101 } | |
1102 break; | |
1103 #endif | |
1104 | |
1105 case ESC: /* get here if p_wc != ESC or when ESC typed twice */ | |
1106 case Ctrl_C: | |
571 | 1107 /* In exmode it doesn't make sense to return. Except when |
161 | 1108 * ":normal" runs out of characters. */ |
1109 if (exmode_active | |
1110 #ifdef FEAT_EX_EXTRA | |
1111 && (ex_normal_busy == 0 || typebuf.tb_len > 0) | |
1112 #endif | |
1113 ) | |
7 | 1114 goto cmdline_not_changed; |
1115 | |
1116 gotesc = TRUE; /* will free ccline.cmdbuff after | |
1117 putting it in history */ | |
1118 goto returncmd; /* back to cmd mode */ | |
1119 | |
1120 case Ctrl_R: /* insert register */ | |
1121 #ifdef USE_ON_FLY_SCROLL | |
1122 dont_scroll = TRUE; /* disallow scrolling here */ | |
1123 #endif | |
1124 putcmdline('"', TRUE); | |
1125 ++no_mapping; | |
1389 | 1126 i = c = plain_vgetc(); /* CTRL-R <char> */ |
7 | 1127 if (i == Ctrl_O) |
1128 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ | |
1129 if (i == Ctrl_R) | |
1389 | 1130 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
7 | 1131 --no_mapping; |
1132 #ifdef FEAT_EVAL | |
1133 /* | |
1134 * Insert the result of an expression. | |
1135 * Need to save the current command line, to be able to enter | |
1136 * a new one... | |
1137 */ | |
1138 new_cmdpos = -1; | |
1139 if (c == '=') | |
1140 { | |
1141 if (ccline.cmdfirstc == '=')/* can't do this recursively */ | |
1142 { | |
1143 beep_flush(); | |
1144 c = ESC; | |
1145 } | |
1146 else | |
1147 { | |
95 | 1148 save_cmdline(&save_ccline); |
7 | 1149 c = get_expr_register(); |
95 | 1150 restore_cmdline(&save_ccline); |
7 | 1151 } |
1152 } | |
1153 #endif | |
1154 if (c != ESC) /* use ESC to cancel inserting register */ | |
1155 { | |
1015 | 1156 cmdline_paste(c, i == Ctrl_R, FALSE); |
606 | 1157 |
613 | 1158 #ifdef FEAT_EVAL |
606 | 1159 /* When there was a serious error abort getting the |
1160 * command line. */ | |
1161 if (aborting()) | |
1162 { | |
1163 gotesc = TRUE; /* will free ccline.cmdbuff after | |
1164 putting it in history */ | |
1165 goto returncmd; /* back to cmd mode */ | |
1166 } | |
613 | 1167 #endif |
7 | 1168 KeyTyped = FALSE; /* Don't do p_wc completion. */ |
1169 #ifdef FEAT_EVAL | |
1170 if (new_cmdpos >= 0) | |
1171 { | |
1172 /* set_cmdline_pos() was used */ | |
1173 if (new_cmdpos > ccline.cmdlen) | |
1174 ccline.cmdpos = ccline.cmdlen; | |
1175 else | |
1176 ccline.cmdpos = new_cmdpos; | |
1177 } | |
1178 #endif | |
1179 } | |
1180 redrawcmd(); | |
1181 goto cmdline_changed; | |
1182 | |
1183 case Ctrl_D: | |
1184 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) | |
1185 break; /* Use ^D as normal char instead */ | |
1186 | |
1187 redrawcmd(); | |
1188 continue; /* don't do incremental search now */ | |
1189 | |
1190 case K_RIGHT: | |
1191 case K_S_RIGHT: | |
1192 case K_C_RIGHT: | |
1193 do | |
1194 { | |
1195 if (ccline.cmdpos >= ccline.cmdlen) | |
1196 break; | |
1197 i = cmdline_charsize(ccline.cmdpos); | |
1198 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) | |
1199 break; | |
1200 ccline.cmdspos += i; | |
1201 #ifdef FEAT_MBYTE | |
1202 if (has_mbyte) | |
474 | 1203 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
7 | 1204 + ccline.cmdpos); |
1205 else | |
1206 #endif | |
1207 ++ccline.cmdpos; | |
1208 } | |
180 | 1209 while ((c == K_S_RIGHT || c == K_C_RIGHT |
1210 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) | |
7 | 1211 && ccline.cmdbuff[ccline.cmdpos] != ' '); |
1212 #ifdef FEAT_MBYTE | |
1213 if (has_mbyte) | |
1214 set_cmdspos_cursor(); | |
1215 #endif | |
1216 goto cmdline_not_changed; | |
1217 | |
1218 case K_LEFT: | |
1219 case K_S_LEFT: | |
1220 case K_C_LEFT: | |
1456 | 1221 if (ccline.cmdpos == 0) |
1222 goto cmdline_not_changed; | |
7 | 1223 do |
1224 { | |
1225 --ccline.cmdpos; | |
1226 #ifdef FEAT_MBYTE | |
1227 if (has_mbyte) /* move to first byte of char */ | |
1228 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, | |
1229 ccline.cmdbuff + ccline.cmdpos); | |
1230 #endif | |
1231 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); | |
1232 } | |
1456 | 1233 while (ccline.cmdpos > 0 |
1234 && (c == K_S_LEFT || c == K_C_LEFT | |
180 | 1235 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
7 | 1236 && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
1237 #ifdef FEAT_MBYTE | |
1238 if (has_mbyte) | |
1239 set_cmdspos_cursor(); | |
1240 #endif | |
1241 goto cmdline_not_changed; | |
1242 | |
1243 case K_IGNORE: | |
1472 | 1244 /* Ignore mouse event or ex_window() result. */ |
1245 goto cmdline_not_changed; | |
7 | 1246 |
625 | 1247 #ifdef FEAT_GUI_W32 |
1248 /* On Win32 ignore <M-F4>, we get it when closing the window was | |
1249 * cancelled. */ | |
1250 case K_F4: | |
1251 if (mod_mask == MOD_MASK_ALT) | |
1252 { | |
1253 redrawcmd(); /* somehow the cmdline is cleared */ | |
1254 goto cmdline_not_changed; | |
1255 } | |
1256 break; | |
1257 #endif | |
1258 | |
7 | 1259 #ifdef FEAT_MOUSE |
1260 case K_MIDDLEDRAG: | |
1261 case K_MIDDLERELEASE: | |
1262 goto cmdline_not_changed; /* Ignore mouse */ | |
1263 | |
1264 case K_MIDDLEMOUSE: | |
1265 # ifdef FEAT_GUI | |
1266 /* When GUI is active, also paste when 'mouse' is empty */ | |
1267 if (!gui.in_use) | |
1268 # endif | |
1269 if (!mouse_has(MOUSE_COMMAND)) | |
1270 goto cmdline_not_changed; /* Ignore mouse */ | |
692 | 1271 # ifdef FEAT_CLIPBOARD |
7 | 1272 if (clip_star.available) |
1015 | 1273 cmdline_paste('*', TRUE, TRUE); |
7 | 1274 else |
692 | 1275 # endif |
1015 | 1276 cmdline_paste(0, TRUE, TRUE); |
7 | 1277 redrawcmd(); |
1278 goto cmdline_changed; | |
1279 | |
692 | 1280 # ifdef FEAT_DND |
7 | 1281 case K_DROP: |
1015 | 1282 cmdline_paste('~', TRUE, FALSE); |
7 | 1283 redrawcmd(); |
1284 goto cmdline_changed; | |
692 | 1285 # endif |
7 | 1286 |
1287 case K_LEFTDRAG: | |
1288 case K_LEFTRELEASE: | |
1289 case K_RIGHTDRAG: | |
1290 case K_RIGHTRELEASE: | |
1291 /* Ignore drag and release events when the button-down wasn't | |
1292 * seen before. */ | |
1293 if (ignore_drag_release) | |
1294 goto cmdline_not_changed; | |
1295 /* FALLTHROUGH */ | |
1296 case K_LEFTMOUSE: | |
1297 case K_RIGHTMOUSE: | |
1298 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) | |
1299 ignore_drag_release = TRUE; | |
1300 else | |
1301 ignore_drag_release = FALSE; | |
1302 # ifdef FEAT_GUI | |
1303 /* When GUI is active, also move when 'mouse' is empty */ | |
1304 if (!gui.in_use) | |
1305 # endif | |
1306 if (!mouse_has(MOUSE_COMMAND)) | |
1307 goto cmdline_not_changed; /* Ignore mouse */ | |
1308 # ifdef FEAT_CLIPBOARD | |
1309 if (mouse_row < cmdline_row && clip_star.available) | |
1310 { | |
1311 int button, is_click, is_drag; | |
1312 | |
1313 /* | |
1314 * Handle modeless selection. | |
1315 */ | |
1316 button = get_mouse_button(KEY2TERMCAP1(c), | |
1317 &is_click, &is_drag); | |
1318 if (mouse_model_popup() && button == MOUSE_LEFT | |
1319 && (mod_mask & MOD_MASK_SHIFT)) | |
1320 { | |
1321 /* Translate shift-left to right button. */ | |
1322 button = MOUSE_RIGHT; | |
1323 mod_mask &= ~MOD_MASK_SHIFT; | |
1324 } | |
1325 clip_modeless(button, is_click, is_drag); | |
1326 goto cmdline_not_changed; | |
1327 } | |
1328 # endif | |
1329 | |
1330 set_cmdspos(); | |
1331 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; | |
1332 ++ccline.cmdpos) | |
1333 { | |
1334 i = cmdline_charsize(ccline.cmdpos); | |
1335 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns | |
1336 && mouse_col < ccline.cmdspos % Columns + i) | |
1337 break; | |
692 | 1338 # ifdef FEAT_MBYTE |
7 | 1339 if (has_mbyte) |
1340 { | |
1341 /* Count ">" for double-wide char that doesn't fit. */ | |
1342 correct_cmdspos(ccline.cmdpos, i); | |
474 | 1343 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
7 | 1344 + ccline.cmdpos) - 1; |
1345 } | |
692 | 1346 # endif |
7 | 1347 ccline.cmdspos += i; |
1348 } | |
1349 goto cmdline_not_changed; | |
1350 | |
1351 /* Mouse scroll wheel: ignored here */ | |
1352 case K_MOUSEDOWN: | |
1353 case K_MOUSEUP: | |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1354 case K_MOUSELEFT: |
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1355 case K_MOUSERIGHT: |
7 | 1356 /* Alternate buttons ignored here */ |
1357 case K_X1MOUSE: | |
1358 case K_X1DRAG: | |
1359 case K_X1RELEASE: | |
1360 case K_X2MOUSE: | |
1361 case K_X2DRAG: | |
1362 case K_X2RELEASE: | |
1363 goto cmdline_not_changed; | |
1364 | |
1365 #endif /* FEAT_MOUSE */ | |
1366 | |
1367 #ifdef FEAT_GUI | |
1368 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ | |
1369 case K_LEFTRELEASE_NM: | |
1370 goto cmdline_not_changed; | |
1371 | |
1372 case K_VER_SCROLLBAR: | |
540 | 1373 if (msg_scrolled == 0) |
7 | 1374 { |
1375 gui_do_scroll(); | |
1376 redrawcmd(); | |
1377 } | |
1378 goto cmdline_not_changed; | |
1379 | |
1380 case K_HOR_SCROLLBAR: | |
540 | 1381 if (msg_scrolled == 0) |
7 | 1382 { |
2409
0ca06a92adfb
Add support for horizontal scroll wheel. (Bjorn Winckler)
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
1383 gui_do_horiz_scroll(scrollbar_value, FALSE); |
7 | 1384 redrawcmd(); |
1385 } | |
1386 goto cmdline_not_changed; | |
1387 #endif | |
692 | 1388 #ifdef FEAT_GUI_TABLINE |
1389 case K_TABLINE: | |
1390 case K_TABMENU: | |
1391 /* Don't want to change any tabs here. Make sure the same tab | |
1392 * is still selected. */ | |
1393 if (gui_use_tabline()) | |
1394 gui_mch_set_curtab(tabpage_index(curtab)); | |
1395 goto cmdline_not_changed; | |
1396 #endif | |
1397 | |
7 | 1398 case K_SELECT: /* end of Select mode mapping - ignore */ |
1399 goto cmdline_not_changed; | |
1400 | |
1401 case Ctrl_B: /* begin of command line */ | |
1402 case K_HOME: | |
1403 case K_KHOME: | |
1404 case K_S_HOME: | |
1405 case K_C_HOME: | |
1406 ccline.cmdpos = 0; | |
1407 set_cmdspos(); | |
1408 goto cmdline_not_changed; | |
1409 | |
1410 case Ctrl_E: /* end of command line */ | |
1411 case K_END: | |
1412 case K_KEND: | |
1413 case K_S_END: | |
1414 case K_C_END: | |
1415 ccline.cmdpos = ccline.cmdlen; | |
1416 set_cmdspos_cursor(); | |
1417 goto cmdline_not_changed; | |
1418 | |
1419 case Ctrl_A: /* all matches */ | |
1420 if (nextwild(&xpc, WILD_ALL, 0) == FAIL) | |
1421 break; | |
1422 goto cmdline_changed; | |
1423 | |
772 | 1424 case Ctrl_L: |
1425 #ifdef FEAT_SEARCH_EXTRA | |
1426 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) | |
1427 { | |
1428 /* Add a character from under the cursor for 'incsearch' */ | |
1429 if (did_incsearch | |
1430 && !equalpos(curwin->w_cursor, old_cursor)) | |
1431 { | |
1432 c = gchar_cursor(); | |
2302
488be8cbe19c
Make CTRL-L in command line mode respect 'ignorecase' and 'smartcase'. (Martin
Bram Moolenaar <bram@vim.org>
parents:
2268
diff
changeset
|
1433 /* If 'ignorecase' and 'smartcase' are set and the |
488be8cbe19c
Make CTRL-L in command line mode respect 'ignorecase' and 'smartcase'. (Martin
Bram Moolenaar <bram@vim.org>
parents:
2268
diff
changeset
|
1434 * command line has no uppercase characters, convert |
488be8cbe19c
Make CTRL-L in command line mode respect 'ignorecase' and 'smartcase'. (Martin
Bram Moolenaar <bram@vim.org>
parents:
2268
diff
changeset
|
1435 * the character to lowercase */ |
488be8cbe19c
Make CTRL-L in command line mode respect 'ignorecase' and 'smartcase'. (Martin
Bram Moolenaar <bram@vim.org>
parents:
2268
diff
changeset
|
1436 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff)) |
488be8cbe19c
Make CTRL-L in command line mode respect 'ignorecase' and 'smartcase'. (Martin
Bram Moolenaar <bram@vim.org>
parents:
2268
diff
changeset
|
1437 c = MB_TOLOWER(c); |
772 | 1438 if (c != NUL) |
1039 | 1439 { |
1440 if (c == firstc || vim_strchr((char_u *)( | |
1441 p_magic ? "\\^$.*[" : "\\^$"), c) | |
1442 != NULL) | |
1443 { | |
1444 /* put a backslash before special characters */ | |
1445 stuffcharReadbuff(c); | |
1446 c = '\\'; | |
1447 } | |
772 | 1448 break; |
1039 | 1449 } |
772 | 1450 } |
1451 goto cmdline_not_changed; | |
1452 } | |
1453 #endif | |
1454 | |
1455 /* completion: longest common part */ | |
7 | 1456 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL) |
1457 break; | |
1458 goto cmdline_changed; | |
1459 | |
1460 case Ctrl_N: /* next match */ | |
1461 case Ctrl_P: /* previous match */ | |
1462 if (xpc.xp_numfiles > 0) | |
1463 { | |
1464 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0) | |
1465 == FAIL) | |
1466 break; | |
1467 goto cmdline_changed; | |
1468 } | |
1469 | |
1470 #ifdef FEAT_CMDHIST | |
1471 case K_UP: | |
1472 case K_DOWN: | |
1473 case K_S_UP: | |
1474 case K_S_DOWN: | |
1475 case K_PAGEUP: | |
1476 case K_KPAGEUP: | |
1477 case K_PAGEDOWN: | |
1478 case K_KPAGEDOWN: | |
1479 if (hislen == 0 || firstc == NUL) /* no history */ | |
1480 goto cmdline_not_changed; | |
1481 | |
1482 i = hiscnt; | |
1483 | |
1484 /* save current command string so it can be restored later */ | |
1485 if (lookfor == NULL) | |
1486 { | |
1487 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) | |
1488 goto cmdline_not_changed; | |
1489 lookfor[ccline.cmdpos] = NUL; | |
1490 } | |
1491 | |
1492 j = (int)STRLEN(lookfor); | |
1493 for (;;) | |
1494 { | |
1495 /* one step backwards */ | |
230 | 1496 if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
180 | 1497 || c == K_PAGEUP || c == K_KPAGEUP) |
7 | 1498 { |
1499 if (hiscnt == hislen) /* first time */ | |
1500 hiscnt = hisidx[histype]; | |
1501 else if (hiscnt == 0 && hisidx[histype] != hislen - 1) | |
1502 hiscnt = hislen - 1; | |
1503 else if (hiscnt != hisidx[histype] + 1) | |
1504 --hiscnt; | |
1505 else /* at top of list */ | |
1506 { | |
1507 hiscnt = i; | |
1508 break; | |
1509 } | |
1510 } | |
1511 else /* one step forwards */ | |
1512 { | |
1513 /* on last entry, clear the line */ | |
1514 if (hiscnt == hisidx[histype]) | |
1515 { | |
1516 hiscnt = hislen; | |
1517 break; | |
1518 } | |
1519 | |
1520 /* not on a history line, nothing to do */ | |
1521 if (hiscnt == hislen) | |
1522 break; | |
1523 if (hiscnt == hislen - 1) /* wrap around */ | |
1524 hiscnt = 0; | |
1525 else | |
1526 ++hiscnt; | |
1527 } | |
1528 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) | |
1529 { | |
1530 hiscnt = i; | |
1531 break; | |
1532 } | |
230 | 1533 if ((c != K_UP && c != K_DOWN) |
180 | 1534 || hiscnt == i |
7 | 1535 || STRNCMP(history[histype][hiscnt].hisstr, |
1536 lookfor, (size_t)j) == 0) | |
1537 break; | |
1538 } | |
1539 | |
1540 if (hiscnt != i) /* jumped to other entry */ | |
1541 { | |
1542 char_u *p; | |
1543 int len; | |
1544 int old_firstc; | |
1545 | |
1546 vim_free(ccline.cmdbuff); | |
1718 | 1547 xpc.xp_context = EXPAND_NOTHING; |
7 | 1548 if (hiscnt == hislen) |
1549 p = lookfor; /* back to the old one */ | |
1550 else | |
1551 p = history[histype][hiscnt].hisstr; | |
1552 | |
1553 if (histype == HIST_SEARCH | |
1554 && p != lookfor | |
1555 && (old_firstc = p[STRLEN(p) + 1]) != firstc) | |
1556 { | |
1557 /* Correct for the separator character used when | |
1558 * adding the history entry vs the one used now. | |
1559 * First loop: count length. | |
1560 * Second loop: copy the characters. */ | |
1561 for (i = 0; i <= 1; ++i) | |
1562 { | |
1563 len = 0; | |
1564 for (j = 0; p[j] != NUL; ++j) | |
1565 { | |
1566 /* Replace old sep with new sep, unless it is | |
1567 * escaped. */ | |
1568 if (p[j] == old_firstc | |
1569 && (j == 0 || p[j - 1] != '\\')) | |
1570 { | |
1571 if (i > 0) | |
1572 ccline.cmdbuff[len] = firstc; | |
1573 } | |
1574 else | |
1575 { | |
1576 /* Escape new sep, unless it is already | |
1577 * escaped. */ | |
1578 if (p[j] == firstc | |
1579 && (j == 0 || p[j - 1] != '\\')) | |
1580 { | |
1581 if (i > 0) | |
1582 ccline.cmdbuff[len] = '\\'; | |
1583 ++len; | |
1584 } | |
1585 if (i > 0) | |
1586 ccline.cmdbuff[len] = p[j]; | |
1587 } | |
1588 ++len; | |
1589 } | |
1590 if (i == 0) | |
1591 { | |
1592 alloc_cmdbuff(len); | |
1593 if (ccline.cmdbuff == NULL) | |
1594 goto returncmd; | |
1595 } | |
1596 } | |
1597 ccline.cmdbuff[len] = NUL; | |
1598 } | |
1599 else | |
1600 { | |
1601 alloc_cmdbuff((int)STRLEN(p)); | |
1602 if (ccline.cmdbuff == NULL) | |
1603 goto returncmd; | |
1604 STRCPY(ccline.cmdbuff, p); | |
1605 } | |
1606 | |
1607 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); | |
1608 redrawcmd(); | |
1609 goto cmdline_changed; | |
1610 } | |
1611 beep_flush(); | |
1612 goto cmdline_not_changed; | |
1613 #endif | |
1614 | |
1615 case Ctrl_V: | |
1616 case Ctrl_Q: | |
1617 #ifdef FEAT_MOUSE | |
1618 ignore_drag_release = TRUE; | |
1619 #endif | |
1620 putcmdline('^', TRUE); | |
1621 c = get_literal(); /* get next (two) character(s) */ | |
1622 do_abbr = FALSE; /* don't do abbreviation now */ | |
1623 #ifdef FEAT_MBYTE | |
1624 /* may need to remove ^ when composing char was typed */ | |
1625 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) | |
1626 { | |
1627 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); | |
1628 msg_putchar(' '); | |
1629 cursorcmd(); | |
1630 } | |
1631 #endif | |
1632 break; | |
1633 | |
1634 #ifdef FEAT_DIGRAPHS | |
1635 case Ctrl_K: | |
1636 #ifdef FEAT_MOUSE | |
1637 ignore_drag_release = TRUE; | |
1638 #endif | |
1639 putcmdline('?', TRUE); | |
1640 #ifdef USE_ON_FLY_SCROLL | |
1641 dont_scroll = TRUE; /* disallow scrolling here */ | |
1642 #endif | |
1643 c = get_digraph(TRUE); | |
1644 if (c != NUL) | |
1645 break; | |
1646 | |
1647 redrawcmd(); | |
1648 goto cmdline_not_changed; | |
1649 #endif /* FEAT_DIGRAPHS */ | |
1650 | |
1651 #ifdef FEAT_RIGHTLEFT | |
1652 case Ctrl__: /* CTRL-_: switch language mode */ | |
1653 if (!p_ari) | |
1654 break; | |
1655 #ifdef FEAT_FKMAP | |
1656 if (p_altkeymap) | |
1657 { | |
1658 cmd_fkmap = !cmd_fkmap; | |
1659 if (cmd_fkmap) /* in Farsi always in Insert mode */ | |
1660 ccline.overstrike = FALSE; | |
1661 } | |
1662 else /* Hebrew is default */ | |
1663 #endif | |
1664 cmd_hkmap = !cmd_hkmap; | |
1665 goto cmdline_not_changed; | |
1666 #endif | |
1667 | |
1668 default: | |
1669 #ifdef UNIX | |
1670 if (c == intr_char) | |
1671 { | |
1672 gotesc = TRUE; /* will free ccline.cmdbuff after | |
1673 putting it in history */ | |
1674 goto returncmd; /* back to Normal mode */ | |
1675 } | |
1676 #endif | |
1677 /* | |
1678 * Normal character with no special meaning. Just set mod_mask | |
1679 * to 0x0 so that typing Shift-Space in the GUI doesn't enter | |
1680 * the string <S-Space>. This should only happen after ^V. | |
1681 */ | |
1682 if (!IS_SPECIAL(c)) | |
1683 mod_mask = 0x0; | |
1684 break; | |
1685 } | |
1686 /* | |
1687 * End of switch on command line character. | |
1688 * We come here if we have a normal character. | |
1689 */ | |
1690 | |
1691 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr( | |
1692 #ifdef FEAT_MBYTE | |
1693 /* Add ABBR_OFF for characters above 0x100, this is | |
1694 * what check_abbr() expects. */ | |
1695 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : | |
1696 #endif | |
1697 c)) | |
1698 goto cmdline_changed; | |
1699 | |
1700 /* | |
1701 * put the character in the command line | |
1702 */ | |
1703 if (IS_SPECIAL(c) || mod_mask != 0) | |
1704 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); | |
1705 else | |
1706 { | |
1707 #ifdef FEAT_MBYTE | |
1708 if (has_mbyte) | |
1709 { | |
1710 j = (*mb_char2bytes)(c, IObuff); | |
1711 IObuff[j] = NUL; /* exclude composing chars */ | |
1712 put_on_cmdline(IObuff, j, TRUE); | |
1713 } | |
1714 else | |
1715 #endif | |
1716 { | |
1717 IObuff[0] = c; | |
1718 put_on_cmdline(IObuff, 1, TRUE); | |
1719 } | |
1720 } | |
1721 goto cmdline_changed; | |
1722 | |
1723 /* | |
1724 * This part implements incremental searches for "/" and "?" | |
1725 * Jump to cmdline_not_changed when a character has been read but the command | |
1726 * line did not change. Then we only search and redraw if something changed in | |
1727 * the past. | |
1728 * Jump to cmdline_changed when the command line did change. | |
1729 * (Sorry for the goto's, I know it is ugly). | |
1730 */ | |
1731 cmdline_not_changed: | |
1732 #ifdef FEAT_SEARCH_EXTRA | |
1733 if (!incsearch_postponed) | |
1734 continue; | |
1735 #endif | |
1736 | |
1737 cmdline_changed: | |
1738 #ifdef FEAT_SEARCH_EXTRA | |
1739 /* | |
1740 * 'incsearch' highlighting. | |
1741 */ | |
1742 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) | |
1743 { | |
772 | 1744 pos_T end_pos; |
1521 | 1745 #ifdef FEAT_RELTIME |
1746 proftime_T tm; | |
1747 #endif | |
772 | 1748 |
7 | 1749 /* if there is a character waiting, search and redraw later */ |
1750 if (char_avail()) | |
1751 { | |
1752 incsearch_postponed = TRUE; | |
1753 continue; | |
1754 } | |
1755 incsearch_postponed = FALSE; | |
1756 curwin->w_cursor = old_cursor; /* start at old position */ | |
1757 | |
1758 /* If there is no command line, don't do anything */ | |
1759 if (ccline.cmdlen == 0) | |
1760 i = 0; | |
1761 else | |
1762 { | |
1763 cursor_off(); /* so the user knows we're busy */ | |
1764 out_flush(); | |
1765 ++emsg_off; /* So it doesn't beep if bad expr */ | |
1521 | 1766 #ifdef FEAT_RELTIME |
1767 /* Set the time limit to half a second. */ | |
1768 profile_setlimit(500L, &tm); | |
1769 #endif | |
7 | 1770 i = do_search(NULL, firstc, ccline.cmdbuff, count, |
1521 | 1771 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK, |
1772 #ifdef FEAT_RELTIME | |
1773 &tm | |
1774 #else | |
1775 NULL | |
1776 #endif | |
1777 ); | |
7 | 1778 --emsg_off; |
1779 /* if interrupted while searching, behave like it failed */ | |
1780 if (got_int) | |
1781 { | |
1782 (void)vpeekc(); /* remove <C-C> from input stream */ | |
1783 got_int = FALSE; /* don't abandon the command line */ | |
1784 i = 0; | |
1785 } | |
1786 else if (char_avail()) | |
1787 /* cancelled searching because a char was typed */ | |
1788 incsearch_postponed = TRUE; | |
1789 } | |
772 | 1790 if (i != 0) |
7 | 1791 highlight_match = TRUE; /* highlight position */ |
1792 else | |
1793 highlight_match = FALSE; /* remove highlight */ | |
1794 | |
1795 /* first restore the old curwin values, so the screen is | |
1796 * positioned in the same way as the actual search command */ | |
1797 curwin->w_leftcol = old_leftcol; | |
1798 curwin->w_topline = old_topline; | |
1799 # ifdef FEAT_DIFF | |
1800 curwin->w_topfill = old_topfill; | |
1801 # endif | |
1802 curwin->w_botline = old_botline; | |
1803 changed_cline_bef_curs(); | |
1804 update_topline(); | |
1805 | |
1806 if (i != 0) | |
1807 { | |
481 | 1808 pos_T save_pos = curwin->w_cursor; |
1809 | |
7 | 1810 /* |
772 | 1811 * First move cursor to end of match, then to the start. This |
7 | 1812 * moves the whole match onto the screen when 'nowrap' is set. |
1813 */ | |
1814 curwin->w_cursor.lnum += search_match_lines; | |
1815 curwin->w_cursor.col = search_match_endcol; | |
481 | 1816 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
1817 { | |
1818 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; | |
1819 coladvance((colnr_T)MAXCOL); | |
1820 } | |
7 | 1821 validate_cursor(); |
772 | 1822 end_pos = curwin->w_cursor; |
481 | 1823 curwin->w_cursor = save_pos; |
7 | 1824 } |
798 | 1825 else |
1826 end_pos = curwin->w_cursor; /* shutup gcc 4 */ | |
772 | 1827 |
7 | 1828 validate_cursor(); |
979 | 1829 # ifdef FEAT_WINDOWS |
1830 /* May redraw the status line to show the cursor position. */ | |
1831 if (p_ru && curwin->w_status_height > 0) | |
1832 curwin->w_redr_status = TRUE; | |
1833 # endif | |
7 | 1834 |
195 | 1835 save_cmdline(&save_ccline); |
743 | 1836 update_screen(SOME_VALID); |
195 | 1837 restore_cmdline(&save_ccline); |
1838 | |
772 | 1839 /* Leave it at the end to make CTRL-R CTRL-W work. */ |
1840 if (i != 0) | |
1841 curwin->w_cursor = end_pos; | |
1842 | |
7 | 1843 msg_starthere(); |
1844 redrawcmdline(); | |
1845 did_incsearch = TRUE; | |
1846 } | |
1847 #else /* FEAT_SEARCH_EXTRA */ | |
1848 ; | |
1849 #endif | |
1850 | |
1851 #ifdef FEAT_RIGHTLEFT | |
1852 if (cmdmsg_rl | |
1853 # ifdef FEAT_ARABIC | |
534 | 1854 || (p_arshape && !p_tbidi && enc_utf8) |
7 | 1855 # endif |
1856 ) | |
1857 /* Always redraw the whole command line to fix shaping and | |
3374 | 1858 * right-left typing. Not efficient, but it works. |
1859 * Do it only when there are no characters left to read | |
1860 * to avoid useless intermediate redraws. */ | |
1861 if (vpeekc() == NUL) | |
1862 redrawcmd(); | |
7 | 1863 #endif |
1864 } | |
1865 | |
1866 returncmd: | |
1867 | |
1868 #ifdef FEAT_RIGHTLEFT | |
1869 cmdmsg_rl = FALSE; | |
1870 #endif | |
1871 | |
1872 #ifdef FEAT_FKMAP | |
1873 cmd_fkmap = 0; | |
1874 #endif | |
1875 | |
1876 ExpandCleanup(&xpc); | |
1718 | 1877 ccline.xpc = NULL; |
7 | 1878 |
1879 #ifdef FEAT_SEARCH_EXTRA | |
1880 if (did_incsearch) | |
1881 { | |
1882 curwin->w_cursor = old_cursor; | |
1883 curwin->w_curswant = old_curswant; | |
1884 curwin->w_leftcol = old_leftcol; | |
1885 curwin->w_topline = old_topline; | |
1886 # ifdef FEAT_DIFF | |
1887 curwin->w_topfill = old_topfill; | |
1888 # endif | |
1889 curwin->w_botline = old_botline; | |
1890 highlight_match = FALSE; | |
1891 validate_cursor(); /* needed for TAB */ | |
743 | 1892 redraw_later(SOME_VALID); |
7 | 1893 } |
1894 #endif | |
1895 | |
1896 if (ccline.cmdbuff != NULL) | |
1897 { | |
1898 /* | |
1899 * Put line in history buffer (":" and "=" only when it was typed). | |
1900 */ | |
1901 #ifdef FEAT_CMDHIST | |
1902 if (ccline.cmdlen && firstc != NUL | |
1903 && (some_key_typed || histype == HIST_SEARCH)) | |
1904 { | |
1905 add_to_history(histype, ccline.cmdbuff, TRUE, | |
1906 histype == HIST_SEARCH ? firstc : NUL); | |
1907 if (firstc == ':') | |
1908 { | |
1909 vim_free(new_last_cmdline); | |
1910 new_last_cmdline = vim_strsave(ccline.cmdbuff); | |
1911 } | |
1912 } | |
1913 #endif | |
1914 | |
1915 if (gotesc) /* abandon command line */ | |
1916 { | |
1917 vim_free(ccline.cmdbuff); | |
1918 ccline.cmdbuff = NULL; | |
1919 if (msg_scrolled == 0) | |
1920 compute_cmdrow(); | |
1921 MSG(""); | |
1922 redraw_cmdline = TRUE; | |
1923 } | |
1924 } | |
1925 | |
1926 /* | |
1927 * If the screen was shifted up, redraw the whole screen (later). | |
1928 * If the line is too long, clear it, so ruler and shown command do | |
1929 * not get printed in the middle of it. | |
1930 */ | |
1931 msg_check(); | |
1932 msg_scroll = save_msg_scroll; | |
1933 redir_off = FALSE; | |
1934 | |
1935 /* When the command line was typed, no need for a wait-return prompt. */ | |
1936 if (some_key_typed) | |
1937 need_wait_return = FALSE; | |
1938 | |
1939 State = save_State; | |
1940 #ifdef USE_IM_CONTROL | |
1941 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) | |
1942 im_save_status(b_im_ptr); | |
1943 im_set_active(FALSE); | |
1944 #endif | |
1945 #ifdef FEAT_MOUSE | |
1946 setmouse(); | |
1947 #endif | |
1948 #ifdef CURSOR_SHAPE | |
1949 ui_cursor_shape(); /* may show different cursor shape */ | |
1950 #endif | |
1951 | |
95 | 1952 { |
1953 char_u *p = ccline.cmdbuff; | |
1954 | |
1955 /* Make ccline empty, getcmdline() may try to use it. */ | |
1956 ccline.cmdbuff = NULL; | |
1957 return p; | |
1958 } | |
7 | 1959 } |
1960 | |
1961 #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) | |
1962 /* | |
1963 * Get a command line with a prompt. | |
1964 * This is prepared to be called recursively from getcmdline() (e.g. by | |
1965 * f_input() when evaluating an expression from CTRL-R =). | |
1966 * Returns the command line in allocated memory, or NULL. | |
1967 */ | |
1968 char_u * | |
531 | 1969 getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg) |
7 | 1970 int firstc; |
1971 char_u *prompt; /* command line prompt */ | |
1972 int attr; /* attributes for prompt */ | |
531 | 1973 int xp_context; /* type of expansion */ |
1974 char_u *xp_arg; /* user-defined expansion argument */ | |
7 | 1975 { |
1976 char_u *s; | |
1977 struct cmdline_info save_ccline; | |
1978 int msg_col_save = msg_col; | |
1979 | |
95 | 1980 save_cmdline(&save_ccline); |
7 | 1981 ccline.cmdprompt = prompt; |
1982 ccline.cmdattr = attr; | |
531 | 1983 # ifdef FEAT_EVAL |
1984 ccline.xp_context = xp_context; | |
1985 ccline.xp_arg = xp_arg; | |
1986 ccline.input_fn = (firstc == '@'); | |
1987 # endif | |
7 | 1988 s = getcmdline(firstc, 1L, 0); |
95 | 1989 restore_cmdline(&save_ccline); |
3020 | 1990 /* Restore msg_col, the prompt from input() may have changed it. |
1991 * But only if called recursively and the commandline is therefore being | |
1992 * restored to an old one; if not, the input() prompt stays on the screen, | |
1993 * so we need its modified msg_col left intact. */ | |
1994 if (ccline.cmdbuff != NULL) | |
1995 msg_col = msg_col_save; | |
7 | 1996 |
1997 return s; | |
1998 } | |
1999 #endif | |
2000 | |
632 | 2001 /* |
634 | 2002 * Return TRUE when the text must not be changed and we can't switch to |
2003 * another window or buffer. Used when editing the command line, evaluating | |
2004 * 'balloonexpr', etc. | |
632 | 2005 */ |
2006 int | |
634 | 2007 text_locked() |
632 | 2008 { |
2009 #ifdef FEAT_CMDWIN | |
2010 if (cmdwin_type != 0) | |
2011 return TRUE; | |
2012 #endif | |
634 | 2013 return textlock != 0; |
632 | 2014 } |
2015 | |
2016 /* | |
2017 * Give an error message for a command that isn't allowed while the cmdline | |
2018 * window is open or editing the cmdline in another way. | |
2019 */ | |
2020 void | |
634 | 2021 text_locked_msg() |
632 | 2022 { |
2023 #ifdef FEAT_CMDWIN | |
2024 if (cmdwin_type != 0) | |
2025 EMSG(_(e_cmdwin)); | |
2026 else | |
2027 #endif | |
2028 EMSG(_(e_secure)); | |
2029 } | |
2030 | |
819 | 2031 #if defined(FEAT_AUTOCMD) || defined(PROTO) |
2032 /* | |
1834 | 2033 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
2034 * and give an error message. | |
819 | 2035 */ |
2036 int | |
2037 curbuf_locked() | |
2038 { | |
2039 if (curbuf_lock > 0) | |
2040 { | |
2041 EMSG(_("E788: Not allowed to edit another buffer now")); | |
2042 return TRUE; | |
2043 } | |
1834 | 2044 return allbuf_locked(); |
2045 } | |
2046 | |
2047 /* | |
2048 * Check if "allbuf_lock" is set and return TRUE when it is and give an error | |
2049 * message. | |
2050 */ | |
2051 int | |
2052 allbuf_locked() | |
2053 { | |
2054 if (allbuf_lock > 0) | |
2055 { | |
2056 EMSG(_("E811: Not allowed to change buffer information now")); | |
2057 return TRUE; | |
2058 } | |
819 | 2059 return FALSE; |
2060 } | |
2061 #endif | |
2062 | |
7 | 2063 static int |
2064 cmdline_charsize(idx) | |
2065 int idx; | |
2066 { | |
2067 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) | |
2068 if (cmdline_star > 0) /* showing '*', always 1 position */ | |
2069 return 1; | |
2070 #endif | |
2071 return ptr2cells(ccline.cmdbuff + idx); | |
2072 } | |
2073 | |
2074 /* | |
2075 * Compute the offset of the cursor on the command line for the prompt and | |
2076 * indent. | |
2077 */ | |
2078 static void | |
2079 set_cmdspos() | |
2080 { | |
531 | 2081 if (ccline.cmdfirstc != NUL) |
7 | 2082 ccline.cmdspos = 1 + ccline.cmdindent; |
2083 else | |
2084 ccline.cmdspos = 0 + ccline.cmdindent; | |
2085 } | |
2086 | |
2087 /* | |
2088 * Compute the screen position for the cursor on the command line. | |
2089 */ | |
2090 static void | |
2091 set_cmdspos_cursor() | |
2092 { | |
2093 int i, m, c; | |
2094 | |
2095 set_cmdspos(); | |
2096 if (KeyTyped) | |
534 | 2097 { |
7 | 2098 m = Columns * Rows; |
534 | 2099 if (m < 0) /* overflow, Columns or Rows at weird value */ |
2100 m = MAXCOL; | |
2101 } | |
7 | 2102 else |
2103 m = MAXCOL; | |
2104 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) | |
2105 { | |
2106 c = cmdline_charsize(i); | |
2107 #ifdef FEAT_MBYTE | |
2108 /* Count ">" for double-wide multi-byte char that doesn't fit. */ | |
2109 if (has_mbyte) | |
2110 correct_cmdspos(i, c); | |
2111 #endif | |
1612 | 2112 /* If the cmdline doesn't fit, show cursor on last visible char. |
2113 * Don't move the cursor itself, so we can still append. */ | |
7 | 2114 if ((ccline.cmdspos += c) >= m) |
2115 { | |
2116 ccline.cmdspos -= c; | |
2117 break; | |
2118 } | |
2119 #ifdef FEAT_MBYTE | |
2120 if (has_mbyte) | |
474 | 2121 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
7 | 2122 #endif |
2123 } | |
2124 } | |
2125 | |
2126 #ifdef FEAT_MBYTE | |
2127 /* | |
2128 * Check if the character at "idx", which is "cells" wide, is a multi-byte | |
2129 * character that doesn't fit, so that a ">" must be displayed. | |
2130 */ | |
2131 static void | |
2132 correct_cmdspos(idx, cells) | |
2133 int idx; | |
2134 int cells; | |
2135 { | |
474 | 2136 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
7 | 2137 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
2138 && ccline.cmdspos % Columns + cells > Columns) | |
2139 ccline.cmdspos++; | |
2140 } | |
2141 #endif | |
2142 | |
2143 /* | |
2144 * Get an Ex command line for the ":" command. | |
2145 */ | |
2146 char_u * | |
1877 | 2147 getexline(c, cookie, indent) |
7 | 2148 int c; /* normally ':', NUL for ":append" */ |
1877 | 2149 void *cookie UNUSED; |
7 | 2150 int indent; /* indent for inside conditionals */ |
2151 { | |
2152 /* When executing a register, remove ':' that's in front of each line. */ | |
2153 if (exec_from_reg && vpeekc() == ':') | |
2154 (void)vgetc(); | |
2155 return getcmdline(c, 1L, indent); | |
2156 } | |
2157 | |
2158 /* | |
2159 * Get an Ex command line for Ex mode. | |
2160 * In Ex mode we only use the OS supplied line editing features and no | |
2161 * mappings or abbreviations. | |
168 | 2162 * Returns a string in allocated memory or NULL. |
7 | 2163 */ |
2164 char_u * | |
1877 | 2165 getexmodeline(promptc, cookie, indent) |
168 | 2166 int promptc; /* normally ':', NUL for ":append" and '?' for |
2167 :s prompt */ | |
1877 | 2168 void *cookie UNUSED; |
7 | 2169 int indent; /* indent for inside conditionals */ |
2170 { | |
168 | 2171 garray_T line_ga; |
2172 char_u *pend; | |
2173 int startcol = 0; | |
1329 | 2174 int c1 = 0; |
168 | 2175 int escaped = FALSE; /* CTRL-V typed */ |
2176 int vcol = 0; | |
2177 char_u *p; | |
1329 | 2178 int prev_char; |
7 | 2179 |
2180 /* Switch cursor on now. This avoids that it happens after the "\n", which | |
2181 * confuses the system function that computes tabstops. */ | |
2182 cursor_on(); | |
2183 | |
2184 /* always start in column 0; write a newline if necessary */ | |
2185 compute_cmdrow(); | |
168 | 2186 if ((msg_col || msg_didout) && promptc != '?') |
7 | 2187 msg_putchar('\n'); |
168 | 2188 if (promptc == ':') |
7 | 2189 { |
164 | 2190 /* indent that is only displayed, not in the line itself */ |
168 | 2191 if (p_prompt) |
2192 msg_putchar(':'); | |
7 | 2193 while (indent-- > 0) |
2194 msg_putchar(' '); | |
2195 startcol = msg_col; | |
2196 } | |
2197 | |
2198 ga_init2(&line_ga, 1, 30); | |
2199 | |
164 | 2200 /* autoindent for :insert and :append is in the line itself */ |
168 | 2201 if (promptc <= 0) |
164 | 2202 { |
2203 vcol = indent; | |
2204 while (indent >= 8) | |
2205 { | |
2206 ga_append(&line_ga, TAB); | |
2207 msg_puts((char_u *)" "); | |
2208 indent -= 8; | |
2209 } | |
2210 while (indent-- > 0) | |
2211 { | |
2212 ga_append(&line_ga, ' '); | |
2213 msg_putchar(' '); | |
2214 } | |
2215 } | |
168 | 2216 ++no_mapping; |
2217 ++allow_keys; | |
164 | 2218 |
7 | 2219 /* |
2220 * Get the line, one character at a time. | |
2221 */ | |
2222 got_int = FALSE; | |
168 | 2223 while (!got_int) |
7 | 2224 { |
2225 if (ga_grow(&line_ga, 40) == FAIL) | |
2226 break; | |
2227 | |
168 | 2228 /* Get one character at a time. Don't use inchar(), it can't handle |
2229 * special characters. */ | |
1329 | 2230 prev_char = c1; |
168 | 2231 c1 = vgetc(); |
7 | 2232 |
2233 /* | |
168 | 2234 * Handle line editing. |
2235 * Previously this was left to the system, putting the terminal in | |
2236 * cooked mode, but then CTRL-D and CTRL-T can't be used properly. | |
7 | 2237 */ |
168 | 2238 if (got_int) |
2239 { | |
2240 msg_putchar('\n'); | |
2241 break; | |
2242 } | |
2243 | |
2244 if (!escaped) | |
7 | 2245 { |
168 | 2246 /* CR typed means "enter", which is NL */ |
2247 if (c1 == '\r') | |
2248 c1 = '\n'; | |
2249 | |
2250 if (c1 == BS || c1 == K_BS | |
2251 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) | |
7 | 2252 { |
168 | 2253 if (line_ga.ga_len > 0) |
2254 { | |
2255 --line_ga.ga_len; | |
2256 goto redraw; | |
2257 } | |
2258 continue; | |
7 | 2259 } |
2260 | |
168 | 2261 if (c1 == Ctrl_U) |
7 | 2262 { |
168 | 2263 msg_col = startcol; |
2264 msg_clr_eos(); | |
2265 line_ga.ga_len = 0; | |
2266 continue; | |
2267 } | |
2268 | |
2269 if (c1 == Ctrl_T) | |
2270 { | |
3740 | 2271 long sw = get_sw_value(); |
2272 | |
168 | 2273 p = (char_u *)line_ga.ga_data; |
2274 p[line_ga.ga_len] = NUL; | |
2275 indent = get_indent_str(p, 8); | |
3740 | 2276 indent += sw - indent % sw; |
168 | 2277 add_indent: |
2278 while (get_indent_str(p, 8) < indent) | |
7 | 2279 { |
168 | 2280 char_u *s = skipwhite(p); |
2281 | |
2282 ga_grow(&line_ga, 1); | |
2283 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); | |
2284 *s = ' '; | |
2285 ++line_ga.ga_len; | |
7 | 2286 } |
168 | 2287 redraw: |
2288 /* redraw the line */ | |
2289 msg_col = startcol; | |
2290 vcol = 0; | |
2291 for (p = (char_u *)line_ga.ga_data; | |
2292 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p) | |
7 | 2293 { |
168 | 2294 if (*p == TAB) |
7 | 2295 { |
168 | 2296 do |
7 | 2297 { |
168 | 2298 msg_putchar(' '); |
2299 } while (++vcol % 8); | |
7 | 2300 } |
168 | 2301 else |
164 | 2302 { |
168 | 2303 msg_outtrans_len(p, 1); |
2304 vcol += char2cells(*p); | |
7 | 2305 } |
2306 } | |
168 | 2307 msg_clr_eos(); |
1329 | 2308 windgoto(msg_row, msg_col); |
168 | 2309 continue; |
2310 } | |
2311 | |
2312 if (c1 == Ctrl_D) | |
2313 { | |
2314 /* Delete one shiftwidth. */ | |
2315 p = (char_u *)line_ga.ga_data; | |
2316 if (prev_char == '0' || prev_char == '^') | |
7 | 2317 { |
168 | 2318 if (prev_char == '^') |
2319 ex_keep_indent = TRUE; | |
2320 indent = 0; | |
2321 p[--line_ga.ga_len] = NUL; | |
7 | 2322 } |
2323 else | |
2324 { | |
168 | 2325 p[line_ga.ga_len] = NUL; |
2326 indent = get_indent_str(p, 8); | |
2327 --indent; | |
3740 | 2328 indent -= indent % get_sw_value(); |
168 | 2329 } |
2330 while (get_indent_str(p, 8) > indent) | |
2331 { | |
2332 char_u *s = skipwhite(p); | |
2333 | |
2334 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); | |
2335 --line_ga.ga_len; | |
7 | 2336 } |
168 | 2337 goto add_indent; |
2338 } | |
2339 | |
2340 if (c1 == Ctrl_V || c1 == Ctrl_Q) | |
2341 { | |
2342 escaped = TRUE; | |
2343 continue; | |
7 | 2344 } |
168 | 2345 |
2346 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ | |
2347 if (IS_SPECIAL(c1)) | |
2348 continue; | |
7 | 2349 } |
168 | 2350 |
2351 if (IS_SPECIAL(c1)) | |
2352 c1 = '?'; | |
2353 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; | |
2354 if (c1 == '\n') | |
2355 msg_putchar('\n'); | |
2356 else if (c1 == TAB) | |
2357 { | |
2358 /* Don't use chartabsize(), 'ts' can be different */ | |
2359 do | |
2360 { | |
2361 msg_putchar(' '); | |
2362 } while (++vcol % 8); | |
2363 } | |
7 | 2364 else |
2365 { | |
168 | 2366 msg_outtrans_len( |
2367 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1); | |
2368 vcol += char2cells(c1); | |
7 | 2369 } |
168 | 2370 ++line_ga.ga_len; |
2371 escaped = FALSE; | |
2372 | |
2373 windgoto(msg_row, msg_col); | |
164 | 2374 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
168 | 2375 |
2590 | 2376 /* We are done when a NL is entered, but not when it comes after an |
2377 * odd number of backslashes, that results in a NUL. */ | |
2378 if (line_ga.ga_len > 0 && pend[-1] == '\n') | |
7 | 2379 { |
2590 | 2380 int bcount = 0; |
2381 | |
2382 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') | |
2383 ++bcount; | |
2384 | |
2385 if (bcount > 0) | |
2386 { | |
2387 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> | |
2388 * "\NL", etc. */ | |
2389 line_ga.ga_len -= (bcount + 1) / 2; | |
2390 pend -= (bcount + 1) / 2; | |
2391 pend[-1] = '\n'; | |
2392 } | |
2393 | |
2394 if ((bcount & 1) == 0) | |
2395 { | |
2396 --line_ga.ga_len; | |
2397 --pend; | |
2398 *pend = NUL; | |
2399 break; | |
2400 } | |
7 | 2401 } |
2402 } | |
2403 | |
168 | 2404 --no_mapping; |
2405 --allow_keys; | |
2406 | |
7 | 2407 /* make following messages go to the next line */ |
2408 msg_didout = FALSE; | |
2409 msg_col = 0; | |
2410 if (msg_row < Rows - 1) | |
2411 ++msg_row; | |
2412 emsg_on_display = FALSE; /* don't want ui_delay() */ | |
2413 | |
2414 if (got_int) | |
2415 ga_clear(&line_ga); | |
2416 | |
2417 return (char_u *)line_ga.ga_data; | |
2418 } | |
2419 | |
502 | 2420 # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
2421 || defined(FEAT_MOUSESHAPE) || defined(PROTO) | |
7 | 2422 /* |
2423 * Return TRUE if ccline.overstrike is on. | |
2424 */ | |
2425 int | |
2426 cmdline_overstrike() | |
2427 { | |
2428 return ccline.overstrike; | |
2429 } | |
2430 | |
2431 /* | |
2432 * Return TRUE if the cursor is at the end of the cmdline. | |
2433 */ | |
2434 int | |
2435 cmdline_at_end() | |
2436 { | |
2437 return (ccline.cmdpos >= ccline.cmdlen); | |
2438 } | |
2439 #endif | |
2440 | |
574 | 2441 #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
7 | 2442 /* |
2443 * Return the virtual column number at the current cursor position. | |
2444 * This is used by the IM code to obtain the start of the preedit string. | |
2445 */ | |
2446 colnr_T | |
2447 cmdline_getvcol_cursor() | |
2448 { | |
2449 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) | |
2450 return MAXCOL; | |
2451 | |
2452 # ifdef FEAT_MBYTE | |
2453 if (has_mbyte) | |
2454 { | |
2455 colnr_T col; | |
2456 int i = 0; | |
2457 | |
2458 for (col = 0; i < ccline.cmdpos; ++col) | |
474 | 2459 i += (*mb_ptr2len)(ccline.cmdbuff + i); |
7 | 2460 |
2461 return col; | |
2462 } | |
2463 else | |
2464 # endif | |
2465 return ccline.cmdpos; | |
2466 } | |
2467 #endif | |
2468 | |
2469 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) | |
2470 /* | |
2471 * If part of the command line is an IM preedit string, redraw it with | |
2472 * IM feedback attributes. The cursor position is restored after drawing. | |
2473 */ | |
2474 static void | |
2475 redrawcmd_preedit() | |
2476 { | |
2477 if ((State & CMDLINE) | |
2478 && xic != NULL | |
976 | 2479 /* && im_get_status() doesn't work when using SCIM */ |
7 | 2480 && !p_imdisable |
2481 && im_is_preediting()) | |
2482 { | |
2483 int cmdpos = 0; | |
2484 int cmdspos; | |
2485 int old_row; | |
2486 int old_col; | |
2487 colnr_T col; | |
2488 | |
2489 old_row = msg_row; | |
2490 old_col = msg_col; | |
531 | 2491 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
7 | 2492 |
2493 # ifdef FEAT_MBYTE | |
2494 if (has_mbyte) | |
2495 { | |
2496 for (col = 0; col < preedit_start_col | |
2497 && cmdpos < ccline.cmdlen; ++col) | |
2498 { | |
2499 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); | |
474 | 2500 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
7 | 2501 } |
2502 } | |
2503 else | |
2504 # endif | |
2505 { | |
2506 cmdspos += preedit_start_col; | |
2507 cmdpos += preedit_start_col; | |
2508 } | |
2509 | |
2510 msg_row = cmdline_row + (cmdspos / (int)Columns); | |
2511 msg_col = cmdspos % (int)Columns; | |
2512 if (msg_row >= Rows) | |
2513 msg_row = Rows - 1; | |
2514 | |
2515 for (col = 0; cmdpos < ccline.cmdlen; ++col) | |
2516 { | |
2517 int char_len; | |
2518 int char_attr; | |
2519 | |
2520 char_attr = im_get_feedback_attr(col); | |
2521 if (char_attr < 0) | |
2522 break; /* end of preedit string */ | |
2523 | |
2524 # ifdef FEAT_MBYTE | |
2525 if (has_mbyte) | |
474 | 2526 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
7 | 2527 else |
2528 # endif | |
2529 char_len = 1; | |
2530 | |
2531 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); | |
2532 cmdpos += char_len; | |
2533 } | |
2534 | |
2535 msg_row = old_row; | |
2536 msg_col = old_col; | |
2537 } | |
2538 } | |
2539 #endif /* FEAT_XIM && FEAT_GUI_GTK */ | |
2540 | |
2541 /* | |
2542 * Allocate a new command line buffer. | |
2543 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. | |
2544 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. | |
2545 */ | |
2546 static void | |
2547 alloc_cmdbuff(len) | |
2548 int len; | |
2549 { | |
2550 /* | |
2551 * give some extra space to avoid having to allocate all the time | |
2552 */ | |
2553 if (len < 80) | |
2554 len = 100; | |
2555 else | |
2556 len += 20; | |
2557 | |
2558 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ | |
2559 ccline.cmdbufflen = len; | |
2560 } | |
2561 | |
2562 /* | |
2563 * Re-allocate the command line to length len + something extra. | |
2564 * return FAIL for failure, OK otherwise | |
2565 */ | |
2566 static int | |
2567 realloc_cmdbuff(len) | |
2568 int len; | |
2569 { | |
2570 char_u *p; | |
2571 | |
2557
029ace8dff7d
Now really fix using expressions in the command line (hopefully).
Bram Moolenaar <bram@vim.org>
parents:
2556
diff
changeset
|
2572 if (len < ccline.cmdbufflen) |
029ace8dff7d
Now really fix using expressions in the command line (hopefully).
Bram Moolenaar <bram@vim.org>
parents:
2556
diff
changeset
|
2573 return OK; /* no need to resize */ |
029ace8dff7d
Now really fix using expressions in the command line (hopefully).
Bram Moolenaar <bram@vim.org>
parents:
2556
diff
changeset
|
2574 |
7 | 2575 p = ccline.cmdbuff; |
2576 alloc_cmdbuff(len); /* will get some more */ | |
2577 if (ccline.cmdbuff == NULL) /* out of memory */ | |
2578 { | |
2579 ccline.cmdbuff = p; /* keep the old one */ | |
2580 return FAIL; | |
2581 } | |
2556
e065501c703a
Fix illegal memory access when using expressions in the command line.
Bram Moolenaar <bram@vim.org>
parents:
2534
diff
changeset
|
2582 /* There isn't always a NUL after the command, but it may need to be |
e065501c703a
Fix illegal memory access when using expressions in the command line.
Bram Moolenaar <bram@vim.org>
parents:
2534
diff
changeset
|
2583 * there, thus copy up to the NUL and add a NUL. */ |
e065501c703a
Fix illegal memory access when using expressions in the command line.
Bram Moolenaar <bram@vim.org>
parents:
2534
diff
changeset
|
2584 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
e065501c703a
Fix illegal memory access when using expressions in the command line.
Bram Moolenaar <bram@vim.org>
parents:
2534
diff
changeset
|
2585 ccline.cmdbuff[ccline.cmdlen] = NUL; |
7 | 2586 vim_free(p); |
1718 | 2587 |
2588 if (ccline.xpc != NULL | |
2589 && ccline.xpc->xp_pattern != NULL | |
2590 && ccline.xpc->xp_context != EXPAND_NOTHING | |
2591 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) | |
2592 { | |
1754 | 2593 int i = (int)(ccline.xpc->xp_pattern - p); |
1718 | 2594 |
2595 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted | |
2596 * to point into the newly allocated memory. */ | |
2597 if (i >= 0 && i <= ccline.cmdlen) | |
2598 ccline.xpc->xp_pattern = ccline.cmdbuff + i; | |
2599 } | |
2600 | |
7 | 2601 return OK; |
2602 } | |
2603 | |
359 | 2604 #if defined(FEAT_ARABIC) || defined(PROTO) |
2605 static char_u *arshape_buf = NULL; | |
2606 | |
2607 # if defined(EXITFREE) || defined(PROTO) | |
2608 void | |
2609 free_cmdline_buf() | |
2610 { | |
2611 vim_free(arshape_buf); | |
2612 } | |
2613 # endif | |
2614 #endif | |
2615 | |
7 | 2616 /* |
2617 * Draw part of the cmdline at the current cursor position. But draw stars | |
2618 * when cmdline_star is TRUE. | |
2619 */ | |
2620 static void | |
2621 draw_cmdline(start, len) | |
2622 int start; | |
2623 int len; | |
2624 { | |
2625 #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) | |
2626 int i; | |
2627 | |
2628 if (cmdline_star > 0) | |
2629 for (i = 0; i < len; ++i) | |
2630 { | |
2631 msg_putchar('*'); | |
2632 # ifdef FEAT_MBYTE | |
2633 if (has_mbyte) | |
474 | 2634 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
7 | 2635 # endif |
2636 } | |
2637 else | |
2638 #endif | |
2639 #ifdef FEAT_ARABIC | |
2640 if (p_arshape && !p_tbidi && enc_utf8 && len > 0) | |
2641 { | |
2642 static int buflen = 0; | |
2643 char_u *p; | |
2644 int j; | |
2645 int newlen = 0; | |
2646 int mb_l; | |
719 | 2647 int pc, pc1 = 0; |
7 | 2648 int prev_c = 0; |
2649 int prev_c1 = 0; | |
714 | 2650 int u8c; |
2651 int u8cc[MAX_MCO]; | |
7 | 2652 int nc = 0; |
2653 | |
2654 /* | |
2655 * Do arabic shaping into a temporary buffer. This is very | |
2656 * inefficient! | |
2657 */ | |
507 | 2658 if (len * 2 + 2 > buflen) |
7 | 2659 { |
2660 /* Re-allocate the buffer. We keep it around to avoid a lot of | |
2661 * alloc()/free() calls. */ | |
359 | 2662 vim_free(arshape_buf); |
507 | 2663 buflen = len * 2 + 2; |
359 | 2664 arshape_buf = alloc(buflen); |
2665 if (arshape_buf == NULL) | |
7 | 2666 return; /* out of memory */ |
2667 } | |
2668 | |
507 | 2669 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
2670 { | |
2671 /* Prepend a space to draw the leading composing char on. */ | |
2672 arshape_buf[0] = ' '; | |
2673 newlen = 1; | |
2674 } | |
2675 | |
7 | 2676 for (j = start; j < start + len; j += mb_l) |
2677 { | |
2678 p = ccline.cmdbuff + j; | |
714 | 2679 u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
474 | 2680 mb_l = utfc_ptr2len_len(p, start + len - j); |
7 | 2681 if (ARABIC_CHAR(u8c)) |
2682 { | |
2683 /* Do Arabic shaping. */ | |
2684 if (cmdmsg_rl) | |
2685 { | |
2686 /* displaying from right to left */ | |
2687 pc = prev_c; | |
2688 pc1 = prev_c1; | |
714 | 2689 prev_c1 = u8cc[0]; |
7 | 2690 if (j + mb_l >= start + len) |
2691 nc = NUL; | |
2692 else | |
2693 nc = utf_ptr2char(p + mb_l); | |
2694 } | |
2695 else | |
2696 { | |
2697 /* displaying from left to right */ | |
2698 if (j + mb_l >= start + len) | |
2699 pc = NUL; | |
2700 else | |
714 | 2701 { |
2702 int pcc[MAX_MCO]; | |
2703 | |
2704 pc = utfc_ptr2char_len(p + mb_l, pcc, | |
7 | 2705 start + len - j - mb_l); |
714 | 2706 pc1 = pcc[0]; |
2707 } | |
7 | 2708 nc = prev_c; |
2709 } | |
2710 prev_c = u8c; | |
2711 | |
714 | 2712 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
7 | 2713 |
359 | 2714 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
714 | 2715 if (u8cc[0] != 0) |
7 | 2716 { |
714 | 2717 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
2718 if (u8cc[1] != 0) | |
2719 newlen += (*mb_char2bytes)(u8cc[1], | |
359 | 2720 arshape_buf + newlen); |
7 | 2721 } |
2722 } | |
2723 else | |
2724 { | |
2725 prev_c = u8c; | |
359 | 2726 mch_memmove(arshape_buf + newlen, p, mb_l); |
7 | 2727 newlen += mb_l; |
2728 } | |
2729 } | |
2730 | |
359 | 2731 msg_outtrans_len(arshape_buf, newlen); |
7 | 2732 } |
2733 else | |
2734 #endif | |
2735 msg_outtrans_len(ccline.cmdbuff + start, len); | |
2736 } | |
2737 | |
2738 /* | |
2739 * Put a character on the command line. Shifts the following text to the | |
2740 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. | |
2741 * "c" must be printable (fit in one display cell)! | |
2742 */ | |
2743 void | |
2744 putcmdline(c, shift) | |
2745 int c; | |
2746 int shift; | |
2747 { | |
2748 if (cmd_silent) | |
2749 return; | |
2750 msg_no_more = TRUE; | |
2751 msg_putchar(c); | |
2752 if (shift) | |
2753 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); | |
2754 msg_no_more = FALSE; | |
2755 cursorcmd(); | |
2756 } | |
2757 | |
2758 /* | |
2759 * Undo a putcmdline(c, FALSE). | |
2760 */ | |
2761 void | |
2762 unputcmdline() | |
2763 { | |
2764 if (cmd_silent) | |
2765 return; | |
2766 msg_no_more = TRUE; | |
2767 if (ccline.cmdlen == ccline.cmdpos) | |
2768 msg_putchar(' '); | |
3558 | 2769 #ifdef FEAT_MBYTE |
2770 else if (has_mbyte) | |
2771 draw_cmdline(ccline.cmdpos, | |
2772 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); | |
2773 #endif | |
7 | 2774 else |
2775 draw_cmdline(ccline.cmdpos, 1); | |
2776 msg_no_more = FALSE; | |
2777 cursorcmd(); | |
2778 } | |
2779 | |
2780 /* | |
2781 * Put the given string, of the given length, onto the command line. | |
2782 * If len is -1, then STRLEN() is used to calculate the length. | |
2783 * If 'redraw' is TRUE then the new part of the command line, and the remaining | |
2784 * part will be redrawn, otherwise it will not. If this function is called | |
2785 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be | |
2786 * called afterwards. | |
2787 */ | |
2788 int | |
2789 put_on_cmdline(str, len, redraw) | |
2790 char_u *str; | |
2791 int len; | |
2792 int redraw; | |
2793 { | |
2794 int retval; | |
2795 int i; | |
2796 int m; | |
2797 int c; | |
2798 | |
2799 if (len < 0) | |
2800 len = (int)STRLEN(str); | |
2801 | |
2802 /* Check if ccline.cmdbuff needs to be longer */ | |
2803 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) | |
2557
029ace8dff7d
Now really fix using expressions in the command line (hopefully).
Bram Moolenaar <bram@vim.org>
parents:
2556
diff
changeset
|
2804 retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
7 | 2805 else |
2806 retval = OK; | |
2807 if (retval == OK) | |
2808 { | |
2809 if (!ccline.overstrike) | |
2810 { | |
2811 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, | |
2812 ccline.cmdbuff + ccline.cmdpos, | |
2813 (size_t)(ccline.cmdlen - ccline.cmdpos)); | |
2814 ccline.cmdlen += len; | |
2815 } | |
2816 else | |
2817 { | |
2818 #ifdef FEAT_MBYTE | |
2819 if (has_mbyte) | |
2820 { | |
2821 /* Count nr of characters in the new string. */ | |
2822 m = 0; | |
474 | 2823 for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
7 | 2824 ++m; |
2825 /* Count nr of bytes in cmdline that are overwritten by these | |
2826 * characters. */ | |
2827 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; | |
474 | 2828 i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
7 | 2829 --m; |
2830 if (i < ccline.cmdlen) | |
2831 { | |
2832 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, | |
2833 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); | |
2834 ccline.cmdlen += ccline.cmdpos + len - i; | |
2835 } | |
2836 else | |
2837 ccline.cmdlen = ccline.cmdpos + len; | |
2838 } | |
2839 else | |
2840 #endif | |
2841 if (ccline.cmdpos + len > ccline.cmdlen) | |
2842 ccline.cmdlen = ccline.cmdpos + len; | |
2843 } | |
2844 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); | |
2845 ccline.cmdbuff[ccline.cmdlen] = NUL; | |
2846 | |
2847 #ifdef FEAT_MBYTE | |
2848 if (enc_utf8) | |
2849 { | |
2850 /* When the inserted text starts with a composing character, | |
2851 * backup to the character before it. There could be two of them. | |
2852 */ | |
2853 i = 0; | |
2854 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); | |
2855 while (ccline.cmdpos > 0 && utf_iscomposing(c)) | |
2856 { | |
2857 i = (*mb_head_off)(ccline.cmdbuff, | |
2858 ccline.cmdbuff + ccline.cmdpos - 1) + 1; | |
2859 ccline.cmdpos -= i; | |
2860 len += i; | |
2861 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); | |
2862 } | |
2863 # ifdef FEAT_ARABIC | |
2864 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) | |
2865 { | |
2866 /* Check the previous character for Arabic combining pair. */ | |
2867 i = (*mb_head_off)(ccline.cmdbuff, | |
2868 ccline.cmdbuff + ccline.cmdpos - 1) + 1; | |
2869 if (arabic_combine(utf_ptr2char(ccline.cmdbuff | |
2870 + ccline.cmdpos - i), c)) | |
2871 { | |
2872 ccline.cmdpos -= i; | |
2873 len += i; | |
2874 } | |
2875 else | |
2876 i = 0; | |
2877 } | |
2878 # endif | |
2879 if (i != 0) | |
2880 { | |
2881 /* Also backup the cursor position. */ | |
2882 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); | |
2883 ccline.cmdspos -= i; | |
2884 msg_col -= i; | |
2885 if (msg_col < 0) | |
2886 { | |
2887 msg_col += Columns; | |
2888 --msg_row; | |
2889 } | |
2890 } | |
2891 } | |
2892 #endif | |
2893 | |
2894 if (redraw && !cmd_silent) | |
2895 { | |
2896 msg_no_more = TRUE; | |
2897 i = cmdline_row; | |
3114 | 2898 cursorcmd(); |
7 | 2899 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
2900 /* Avoid clearing the rest of the line too often. */ | |
2901 if (cmdline_row != i || ccline.overstrike) | |
2902 msg_clr_eos(); | |
2903 msg_no_more = FALSE; | |
2904 } | |
2905 #ifdef FEAT_FKMAP | |
2906 /* | |
2907 * If we are in Farsi command mode, the character input must be in | |
2908 * Insert mode. So do not advance the cmdpos. | |
2909 */ | |
2910 if (!cmd_fkmap) | |
2911 #endif | |
2912 { | |
2913 if (KeyTyped) | |
534 | 2914 { |
7 | 2915 m = Columns * Rows; |
534 | 2916 if (m < 0) /* overflow, Columns or Rows at weird value */ |
2917 m = MAXCOL; | |
2918 } | |
7 | 2919 else |
2920 m = MAXCOL; | |
2921 for (i = 0; i < len; ++i) | |
2922 { | |
2923 c = cmdline_charsize(ccline.cmdpos); | |
2924 #ifdef FEAT_MBYTE | |
2925 /* count ">" for a double-wide char that doesn't fit. */ | |
2926 if (has_mbyte) | |
2927 correct_cmdspos(ccline.cmdpos, c); | |
2928 #endif | |
1612 | 2929 /* Stop cursor at the end of the screen, but do increment the |
2930 * insert position, so that entering a very long command | |
2931 * works, even though you can't see it. */ | |
2932 if (ccline.cmdspos + c < m) | |
2933 ccline.cmdspos += c; | |
7 | 2934 #ifdef FEAT_MBYTE |
2935 if (has_mbyte) | |
2936 { | |
474 | 2937 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
7 | 2938 if (c > len - i - 1) |
2939 c = len - i - 1; | |
2940 ccline.cmdpos += c; | |
2941 i += c; | |
2942 } | |
2943 #endif | |
2944 ++ccline.cmdpos; | |
2945 } | |
2946 } | |
2947 } | |
2948 if (redraw) | |
2949 msg_check(); | |
2950 return retval; | |
2951 } | |
2952 | |
95 | 2953 static struct cmdline_info prev_ccline; |
2954 static int prev_ccline_used = FALSE; | |
2955 | |
2956 /* | |
2957 * Save ccline, because obtaining the "=" register may execute "normal :cmd" | |
2958 * and overwrite it. But get_cmdline_str() may need it, thus make it | |
2959 * available globally in prev_ccline. | |
2960 */ | |
2961 static void | |
2962 save_cmdline(ccp) | |
2963 struct cmdline_info *ccp; | |
2964 { | |
2965 if (!prev_ccline_used) | |
2966 { | |
2967 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); | |
2968 prev_ccline_used = TRUE; | |
2969 } | |
2970 *ccp = prev_ccline; | |
2971 prev_ccline = ccline; | |
2972 ccline.cmdbuff = NULL; | |
2973 ccline.cmdprompt = NULL; | |
1718 | 2974 ccline.xpc = NULL; |
95 | 2975 } |
2976 | |
2977 /* | |
1214 | 2978 * Restore ccline after it has been saved with save_cmdline(). |
95 | 2979 */ |
2980 static void | |
2981 restore_cmdline(ccp) | |
2982 struct cmdline_info *ccp; | |
2983 { | |
2984 ccline = prev_ccline; | |
2985 prev_ccline = *ccp; | |
2986 } | |
2987 | |
849 | 2988 #if defined(FEAT_EVAL) || defined(PROTO) |
2989 /* | |
2990 * Save the command line into allocated memory. Returns a pointer to be | |
2991 * passed to restore_cmdline_alloc() later. | |
2992 * Returns NULL when failed. | |
2993 */ | |
2994 char_u * | |
2995 save_cmdline_alloc() | |
2996 { | |
2997 struct cmdline_info *p; | |
2998 | |
2999 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); | |
3000 if (p != NULL) | |
3001 save_cmdline(p); | |
3002 return (char_u *)p; | |
3003 } | |
3004 | |
3005 /* | |
3006 * Restore the command line from the return value of save_cmdline_alloc(). | |
3007 */ | |
3008 void | |
3009 restore_cmdline_alloc(p) | |
3010 char_u *p; | |
3011 { | |
3012 if (p != NULL) | |
3013 { | |
3014 restore_cmdline((struct cmdline_info *)p); | |
3015 vim_free(p); | |
3016 } | |
3017 } | |
3018 #endif | |
3019 | |
15 | 3020 /* |
3021 * paste a yank register into the command line. | |
3022 * used by CTRL-R command in command-line mode | |
3023 * insert_reg() can't be used here, because special characters from the | |
3024 * register contents will be interpreted as commands. | |
3025 * | |
3026 * return FAIL for failure, OK otherwise | |
3027 */ | |
3028 static int | |
1015 | 3029 cmdline_paste(regname, literally, remcr) |
15 | 3030 int regname; |
3031 int literally; /* Insert text literally instead of "as typed" */ | |
1015 | 3032 int remcr; /* remove trailing CR */ |
15 | 3033 { |
3034 long i; | |
3035 char_u *arg; | |
772 | 3036 char_u *p; |
15 | 3037 int allocated; |
3038 struct cmdline_info save_ccline; | |
3039 | |
3040 /* check for valid regname; also accept special characters for CTRL-R in | |
3041 * the command line */ | |
3042 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W | |
3043 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE)) | |
3044 return FAIL; | |
3045 | |
3046 /* A register containing CTRL-R can cause an endless loop. Allow using | |
3047 * CTRL-C to break the loop. */ | |
3048 line_breakcheck(); | |
3049 if (got_int) | |
3050 return FAIL; | |
3051 | |
3052 #ifdef FEAT_CLIPBOARD | |
3053 regname = may_get_selection(regname); | |
3054 #endif | |
3055 | |
634 | 3056 /* Need to save and restore ccline. And set "textlock" to avoid nasty |
632 | 3057 * things like going to another buffer when evaluating an expression. */ |
95 | 3058 save_cmdline(&save_ccline); |
634 | 3059 ++textlock; |
15 | 3060 i = get_spec_reg(regname, &arg, &allocated, TRUE); |
634 | 3061 --textlock; |
95 | 3062 restore_cmdline(&save_ccline); |
15 | 3063 |
3064 if (i) | |
3065 { | |
3066 /* Got the value of a special register in "arg". */ | |
3067 if (arg == NULL) | |
3068 return FAIL; | |
772 | 3069 |
3070 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate | |
3071 * part of the word. */ | |
3072 p = arg; | |
3073 if (p_is && regname == Ctrl_W) | |
3074 { | |
3075 char_u *w; | |
3076 int len; | |
3077 | |
3078 /* Locate start of last word in the cmd buffer. */ | |
2937 | 3079 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
772 | 3080 { |
3081 #ifdef FEAT_MBYTE | |
3082 if (has_mbyte) | |
3083 { | |
3084 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; | |
3085 if (!vim_iswordc(mb_ptr2char(w - len))) | |
3086 break; | |
3087 w -= len; | |
3088 } | |
3089 else | |
3090 #endif | |
3091 { | |
3092 if (!vim_iswordc(w[-1])) | |
3093 break; | |
3094 --w; | |
3095 } | |
3096 } | |
2937 | 3097 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
772 | 3098 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
3099 p += len; | |
3100 } | |
3101 | |
3102 cmdline_paste_str(p, literally); | |
15 | 3103 if (allocated) |
3104 vim_free(arg); | |
3105 return OK; | |
3106 } | |
3107 | |
1015 | 3108 return cmdline_paste_reg(regname, literally, remcr); |
15 | 3109 } |
3110 | |
3111 /* | |
3112 * Put a string on the command line. | |
3113 * When "literally" is TRUE, insert literally. | |
3114 * When "literally" is FALSE, insert as typed, but don't leave the command | |
3115 * line. | |
3116 */ | |
3117 void | |
3118 cmdline_paste_str(s, literally) | |
3119 char_u *s; | |
3120 int literally; | |
3121 { | |
3122 int c, cv; | |
3123 | |
3124 if (literally) | |
3125 put_on_cmdline(s, -1, TRUE); | |
3126 else | |
3127 while (*s != NUL) | |
3128 { | |
3129 cv = *s; | |
3130 if (cv == Ctrl_V && s[1]) | |
3131 ++s; | |
3132 #ifdef FEAT_MBYTE | |
3133 if (has_mbyte) | |
1606 | 3134 c = mb_cptr2char_adv(&s); |
15 | 3135 else |
3136 #endif | |
3137 c = *s++; | |
3628 | 3138 if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
3139 || c == CAR || c == NL || c == Ctrl_L | |
15 | 3140 #ifdef UNIX |
3141 || c == intr_char | |
3142 #endif | |
3143 || (c == Ctrl_BSL && *s == Ctrl_N)) | |
3144 stuffcharReadbuff(Ctrl_V); | |
3145 stuffcharReadbuff(c); | |
3146 } | |
3147 } | |
3148 | |
7 | 3149 #ifdef FEAT_WILDMENU |
3150 /* | |
3151 * Delete characters on the command line, from "from" to the current | |
3152 * position. | |
3153 */ | |
3154 static void | |
3155 cmdline_del(from) | |
3156 int from; | |
3157 { | |
3158 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, | |
3159 (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); | |
3160 ccline.cmdlen -= ccline.cmdpos - from; | |
3161 ccline.cmdpos = from; | |
3162 } | |
3163 #endif | |
3164 | |
3165 /* | |
1214 | 3166 * this function is called when the screen size changes and with incremental |
7 | 3167 * search |
3168 */ | |
3169 void | |
3170 redrawcmdline() | |
3171 { | |
3172 if (cmd_silent) | |
3173 return; | |
3174 need_wait_return = FALSE; | |
3175 compute_cmdrow(); | |
3176 redrawcmd(); | |
3177 cursorcmd(); | |
3178 } | |
3179 | |
3180 static void | |
3181 redrawcmdprompt() | |
3182 { | |
3183 int i; | |
3184 | |
3185 if (cmd_silent) | |
3186 return; | |
531 | 3187 if (ccline.cmdfirstc != NUL) |
7 | 3188 msg_putchar(ccline.cmdfirstc); |
3189 if (ccline.cmdprompt != NULL) | |
3190 { | |
3191 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); | |
3192 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; | |
3193 /* do the reverse of set_cmdspos() */ | |
531 | 3194 if (ccline.cmdfirstc != NUL) |
7 | 3195 --ccline.cmdindent; |
3196 } | |
3197 else | |
3198 for (i = ccline.cmdindent; i > 0; --i) | |
3199 msg_putchar(' '); | |
3200 } | |
3201 | |
3202 /* | |
3203 * Redraw what is currently on the command line. | |
3204 */ | |
3205 void | |
3206 redrawcmd() | |
3207 { | |
3208 if (cmd_silent) | |
3209 return; | |
3210 | |
683 | 3211 /* when 'incsearch' is set there may be no command line while redrawing */ |
3212 if (ccline.cmdbuff == NULL) | |
3213 { | |
3214 windgoto(cmdline_row, 0); | |
3215 msg_clr_eos(); | |
3216 return; | |
3217 } | |
3218 | |
7 | 3219 msg_start(); |
3220 redrawcmdprompt(); | |
3221 | |
3222 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ | |
3223 msg_no_more = TRUE; | |
3224 draw_cmdline(0, ccline.cmdlen); | |
3225 msg_clr_eos(); | |
3226 msg_no_more = FALSE; | |
3227 | |
3228 set_cmdspos_cursor(); | |
3229 | |
3230 /* | |
3231 * An emsg() before may have set msg_scroll. This is used in normal mode, | |
3232 * in cmdline mode we can reset them now. | |
3233 */ | |
3234 msg_scroll = FALSE; /* next message overwrites cmdline */ | |
3235 | |
3236 /* Typing ':' at the more prompt may set skip_redraw. We don't want this | |
3237 * in cmdline mode */ | |
3238 skip_redraw = FALSE; | |
3239 } | |
3240 | |
3241 void | |
3242 compute_cmdrow() | |
3243 { | |
540 | 3244 if (exmode_active || msg_scrolled != 0) |
7 | 3245 cmdline_row = Rows - 1; |
3246 else | |
3247 cmdline_row = W_WINROW(lastwin) + lastwin->w_height | |
3248 + W_STATUS_HEIGHT(lastwin); | |
3249 } | |
3250 | |
3251 static void | |
3252 cursorcmd() | |
3253 { | |
3254 if (cmd_silent) | |
3255 return; | |
3256 | |
3257 #ifdef FEAT_RIGHTLEFT | |
3258 if (cmdmsg_rl) | |
3259 { | |
3260 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); | |
3261 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; | |
3262 if (msg_row <= 0) | |
3263 msg_row = Rows - 1; | |
3264 } | |
3265 else | |
3266 #endif | |
3267 { | |
3268 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); | |
3269 msg_col = ccline.cmdspos % (int)Columns; | |
3270 if (msg_row >= Rows) | |
3271 msg_row = Rows - 1; | |
3272 } | |
3273 | |
3274 windgoto(msg_row, msg_col); | |
3275 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) | |
3276 redrawcmd_preedit(); | |
3277 #endif | |
3278 #ifdef MCH_CURSOR_SHAPE | |
3279 mch_update_cursor(); | |
3280 #endif | |
3281 } | |
3282 | |
3283 void | |
3284 gotocmdline(clr) | |
3285 int clr; | |
3286 { | |
3287 msg_start(); | |
3288 #ifdef FEAT_RIGHTLEFT | |
3289 if (cmdmsg_rl) | |
3290 msg_col = Columns - 1; | |
3291 else | |
3292 #endif | |
3293 msg_col = 0; /* always start in column 0 */ | |
3294 if (clr) /* clear the bottom line(s) */ | |
3295 msg_clr_eos(); /* will reset clear_cmdline */ | |
3296 windgoto(cmdline_row, 0); | |
3297 } | |
3298 | |
3299 /* | |
3300 * Check the word in front of the cursor for an abbreviation. | |
3301 * Called when the non-id character "c" has been entered. | |
3302 * When an abbreviation is recognized it is removed from the text with | |
3303 * backspaces and the replacement string is inserted, followed by "c". | |
3304 */ | |
3305 static int | |
3306 ccheck_abbr(c) | |
3307 int c; | |
3308 { | |
3309 if (p_paste || no_abbr) /* no abbreviations or in paste mode */ | |
3310 return FALSE; | |
3311 | |
3312 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0); | |
3313 } | |
3314 | |
3164 | 3315 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
3316 static int | |
3317 #ifdef __BORLANDC__ | |
3318 _RTLENTRYF | |
3319 #endif | |
3320 sort_func_compare(s1, s2) | |
3321 const void *s1; | |
3322 const void *s2; | |
3323 { | |
3324 char_u *p1 = *(char_u **)s1; | |
3325 char_u *p2 = *(char_u **)s2; | |
3326 | |
3327 if (*p1 != '<' && *p2 == '<') return -1; | |
3328 if (*p1 == '<' && *p2 != '<') return 1; | |
3329 return STRCMP(p1, p2); | |
3330 } | |
3331 #endif | |
3332 | |
7 | 3333 /* |
3334 * Return FAIL if this is not an appropriate context in which to do | |
3335 * completion of anything, return OK if it is (even if there are no matches). | |
3336 * For the caller, this means that the character is just passed through like a | |
3337 * normal character (instead of being expanded). This allows :s/^I^D etc. | |
3338 */ | |
3339 static int | |
3340 nextwild(xp, type, options) | |
3341 expand_T *xp; | |
3342 int type; | |
3343 int options; /* extra options for ExpandOne() */ | |
3344 { | |
3345 int i, j; | |
3346 char_u *p1; | |
3347 char_u *p2; | |
3348 int difflen; | |
3349 int v; | |
3350 | |
3351 if (xp->xp_numfiles == -1) | |
3352 { | |
3353 set_expand_context(xp); | |
3354 cmd_showtail = expand_showtail(xp); | |
3355 } | |
3356 | |
3357 if (xp->xp_context == EXPAND_UNSUCCESSFUL) | |
3358 { | |
3359 beep_flush(); | |
3360 return OK; /* Something illegal on command line */ | |
3361 } | |
3362 if (xp->xp_context == EXPAND_NOTHING) | |
3363 { | |
3364 /* Caller can use the character as a normal char instead */ | |
3365 return FAIL; | |
3366 } | |
3367 | |
3368 MSG_PUTS("..."); /* show that we are busy */ | |
3369 out_flush(); | |
3370 | |
3371 i = (int)(xp->xp_pattern - ccline.cmdbuff); | |
1965 | 3372 xp->xp_pattern_len = ccline.cmdpos - i; |
7 | 3373 |
3374 if (type == WILD_NEXT || type == WILD_PREV) | |
3375 { | |
3376 /* | |
3377 * Get next/previous match for a previous expanded pattern. | |
3378 */ | |
3379 p2 = ExpandOne(xp, NULL, NULL, 0, type); | |
3380 } | |
3381 else | |
3382 { | |
3383 /* | |
3384 * Translate string into pattern and expand it. | |
3385 */ | |
1965 | 3386 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
3387 xp->xp_context)) == NULL) | |
7 | 3388 p2 = NULL; |
3389 else | |
3390 { | |
2652 | 3391 int use_options = options | |
3392 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE; | |
3393 | |
3394 if (p_wic) | |
3395 use_options += WILD_ICASE; | |
1965 | 3396 p2 = ExpandOne(xp, p1, |
3397 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), | |
2652 | 3398 use_options, type); |
7 | 3399 vim_free(p1); |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2016
diff
changeset
|
3400 /* longest match: make sure it is not shorter, happens with :help */ |
7 | 3401 if (p2 != NULL && type == WILD_LONGEST) |
3402 { | |
1965 | 3403 for (j = 0; j < xp->xp_pattern_len; ++j) |
7 | 3404 if (ccline.cmdbuff[i + j] == '*' |
3405 || ccline.cmdbuff[i + j] == '?') | |
3406 break; | |
3407 if ((int)STRLEN(p2) < j) | |
3408 { | |
3409 vim_free(p2); | |
3410 p2 = NULL; | |
3411 } | |
3412 } | |
3413 } | |
3414 } | |
3415 | |
3416 if (p2 != NULL && !got_int) | |
3417 { | |
1965 | 3418 difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
2557
029ace8dff7d
Now really fix using expressions in the command line (hopefully).
Bram Moolenaar <bram@vim.org>
parents:
2556
diff
changeset
|
3419 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
7 | 3420 { |
2557
029ace8dff7d
Now really fix using expressions in the command line (hopefully).
Bram Moolenaar <bram@vim.org>
parents:
2556
diff
changeset
|
3421 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
7 | 3422 xp->xp_pattern = ccline.cmdbuff + i; |
3423 } | |
3424 else | |
3425 v = OK; | |
3426 if (v == OK) | |
3427 { | |
323 | 3428 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
3429 &ccline.cmdbuff[ccline.cmdpos], | |
3430 (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); | |
3431 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); | |
7 | 3432 ccline.cmdlen += difflen; |
3433 ccline.cmdpos += difflen; | |
3434 } | |
3435 } | |
3436 vim_free(p2); | |
3437 | |
3438 redrawcmd(); | |
33 | 3439 cursorcmd(); |
7 | 3440 |
3441 /* When expanding a ":map" command and no matches are found, assume that | |
3442 * the key is supposed to be inserted literally */ | |
3443 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) | |
3444 return FAIL; | |
3445 | |
3446 if (xp->xp_numfiles <= 0 && p2 == NULL) | |
3447 beep_flush(); | |
3448 else if (xp->xp_numfiles == 1) | |
3449 /* free expanded pattern */ | |
3450 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); | |
3451 | |
3452 return OK; | |
3453 } | |
3454 | |
3455 /* | |
3456 * Do wildcard expansion on the string 'str'. | |
3457 * Chars that should not be expanded must be preceded with a backslash. | |
1612 | 3458 * Return a pointer to allocated memory containing the new string. |
7 | 3459 * Return NULL for failure. |
3460 * | |
1412 | 3461 * "orig" is the originally expanded string, copied to allocated memory. It |
3462 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or | |
3463 * WILD_PREV "orig" should be NULL. | |
3464 * | |
838 | 3465 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
3466 * is WILD_EXPAND_FREE or WILD_ALL. | |
7 | 3467 * |
3468 * mode = WILD_FREE: just free previously expanded matches | |
3469 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches | |
3470 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches | |
3471 * mode = WILD_NEXT: use next match in multiple match, wrap to first | |
3472 * mode = WILD_PREV: use previous match in multiple match, wrap to first | |
3473 * mode = WILD_ALL: return all matches concatenated | |
3474 * mode = WILD_LONGEST: return longest matched part | |
3398 | 3475 * mode = WILD_ALL_KEEP: get all matches, keep matches |
7 | 3476 * |
3477 * options = WILD_LIST_NOTFOUND: list entries without a match | |
3478 * options = WILD_HOME_REPLACE: do home_replace() for buffer names | |
3479 * options = WILD_USE_NL: Use '\n' for WILD_ALL | |
3480 * options = WILD_NO_BEEP: Don't beep for multiple matches | |
3481 * options = WILD_ADD_SLASH: add a slash after directory names | |
3482 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries | |
3483 * options = WILD_SILENT: don't print warning messages | |
3484 * options = WILD_ESCAPE: put backslash before special chars | |
2652 | 3485 * options = WILD_ICASE: ignore case for files |
7 | 3486 * |
3487 * The variables xp->xp_context and xp->xp_backslash must have been set! | |
3488 */ | |
3489 char_u * | |
3490 ExpandOne(xp, str, orig, options, mode) | |
3491 expand_T *xp; | |
3492 char_u *str; | |
3493 char_u *orig; /* allocated copy of original of expanded string */ | |
3494 int options; | |
3495 int mode; | |
3496 { | |
3497 char_u *ss = NULL; | |
3498 static int findex; | |
3499 static char_u *orig_save = NULL; /* kept value of orig */ | |
1432 | 3500 int orig_saved = FALSE; |
7 | 3501 int i; |
3502 long_u len; | |
3503 int non_suf_match; /* number without matching suffix */ | |
3504 | |
3505 /* | |
3506 * first handle the case of using an old match | |
3507 */ | |
3508 if (mode == WILD_NEXT || mode == WILD_PREV) | |
3509 { | |
3510 if (xp->xp_numfiles > 0) | |
3511 { | |
3512 if (mode == WILD_PREV) | |
3513 { | |
3514 if (findex == -1) | |
3515 findex = xp->xp_numfiles; | |
3516 --findex; | |
3517 } | |
3518 else /* mode == WILD_NEXT */ | |
3519 ++findex; | |
3520 | |
3521 /* | |
3522 * When wrapping around, return the original string, set findex to | |
3523 * -1. | |
3524 */ | |
3525 if (findex < 0) | |
3526 { | |
3527 if (orig_save == NULL) | |
3528 findex = xp->xp_numfiles - 1; | |
3529 else | |
3530 findex = -1; | |
3531 } | |
3532 if (findex >= xp->xp_numfiles) | |
3533 { | |
3534 if (orig_save == NULL) | |
3535 findex = 0; | |
3536 else | |
3537 findex = -1; | |
3538 } | |
3539 #ifdef FEAT_WILDMENU | |
3540 if (p_wmnu) | |
3541 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, | |
3542 findex, cmd_showtail); | |
3543 #endif | |
3544 if (findex == -1) | |
3545 return vim_strsave(orig_save); | |
3546 return vim_strsave(xp->xp_files[findex]); | |
3547 } | |
3548 else | |
3549 return NULL; | |
3550 } | |
3551 | |
1412 | 3552 /* free old names */ |
7 | 3553 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
3554 { | |
3555 FreeWild(xp->xp_numfiles, xp->xp_files); | |
3556 xp->xp_numfiles = -1; | |
3557 vim_free(orig_save); | |
3558 orig_save = NULL; | |
3559 } | |
3560 findex = 0; | |
3561 | |
3562 if (mode == WILD_FREE) /* only release file name */ | |
3563 return NULL; | |
3564 | |
3565 if (xp->xp_numfiles == -1) | |
3566 { | |
3567 vim_free(orig_save); | |
3568 orig_save = orig; | |
1432 | 3569 orig_saved = TRUE; |
7 | 3570 |
3571 /* | |
3572 * Do the expansion. | |
3573 */ | |
3574 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, | |
3575 options) == FAIL) | |
3576 { | |
3577 #ifdef FNAME_ILLEGAL | |
3578 /* Illegal file name has been silently skipped. But when there | |
3579 * are wildcards, the real problem is that there was no match, | |
3580 * causing the pattern to be added, which has illegal characters. | |
3581 */ | |
3582 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) | |
3583 EMSG2(_(e_nomatch2), str); | |
3584 #endif | |
3585 } | |
3586 else if (xp->xp_numfiles == 0) | |
3587 { | |
3588 if (!(options & WILD_SILENT)) | |
3589 EMSG2(_(e_nomatch2), str); | |
3590 } | |
3591 else | |
3592 { | |
3593 /* Escape the matches for use on the command line. */ | |
3594 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); | |
3595 | |
3596 /* | |
3597 * Check for matching suffixes in file names. | |
3598 */ | |
3398 | 3599 if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
3600 && mode != WILD_LONGEST) | |
7 | 3601 { |
3602 if (xp->xp_numfiles) | |
3603 non_suf_match = xp->xp_numfiles; | |
3604 else | |
3605 non_suf_match = 1; | |
3606 if ((xp->xp_context == EXPAND_FILES | |
3607 || xp->xp_context == EXPAND_DIRECTORIES) | |
3608 && xp->xp_numfiles > 1) | |
3609 { | |
3610 /* | |
3611 * More than one match; check suffix. | |
3612 * The files will have been sorted on matching suffix in | |
3613 * expand_wildcards, only need to check the first two. | |
3614 */ | |
3615 non_suf_match = 0; | |
3616 for (i = 0; i < 2; ++i) | |
3617 if (match_suffix(xp->xp_files[i])) | |
3618 ++non_suf_match; | |
3619 } | |
3620 if (non_suf_match != 1) | |
3621 { | |
3622 /* Can we ever get here unless it's while expanding | |
3623 * interactively? If not, we can get rid of this all | |
3624 * together. Don't really want to wait for this message | |
3625 * (and possibly have to hit return to continue!). | |
3626 */ | |
3627 if (!(options & WILD_SILENT)) | |
3628 EMSG(_(e_toomany)); | |
3629 else if (!(options & WILD_NO_BEEP)) | |
3630 beep_flush(); | |
3631 } | |
3632 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) | |
3633 ss = vim_strsave(xp->xp_files[0]); | |
3634 } | |
3635 } | |
3636 } | |
3637 | |
3638 /* Find longest common part */ | |
3639 if (mode == WILD_LONGEST && xp->xp_numfiles > 0) | |
3640 { | |
3641 for (len = 0; xp->xp_files[0][len]; ++len) | |
3642 { | |
3643 for (i = 0; i < xp->xp_numfiles; ++i) | |
3644 { | |
3645 #ifdef CASE_INSENSITIVE_FILENAME | |
3646 if (xp->xp_context == EXPAND_DIRECTORIES | |
3647 || xp->xp_context == EXPAND_FILES | |
714 | 3648 || xp->xp_context == EXPAND_SHELLCMD |
7 | 3649 || xp->xp_context == EXPAND_BUFFERS) |
3650 { | |
3651 if (TOLOWER_LOC(xp->xp_files[i][len]) != | |
3652 TOLOWER_LOC(xp->xp_files[0][len])) | |
3653 break; | |
3654 } | |
3655 else | |
3656 #endif | |
3657 if (xp->xp_files[i][len] != xp->xp_files[0][len]) | |
3658 break; | |
3659 } | |
3660 if (i < xp->xp_numfiles) | |
3661 { | |
3662 if (!(options & WILD_NO_BEEP)) | |
3663 vim_beep(); | |
3664 break; | |
3665 } | |
3666 } | |
3667 ss = alloc((unsigned)len + 1); | |
3668 if (ss) | |
419 | 3669 vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
7 | 3670 findex = -1; /* next p_wc gets first one */ |
3671 } | |
3672 | |
3673 /* Concatenate all matching names */ | |
3674 if (mode == WILD_ALL && xp->xp_numfiles > 0) | |
3675 { | |
3676 len = 0; | |
3677 for (i = 0; i < xp->xp_numfiles; ++i) | |
3678 len += (long_u)STRLEN(xp->xp_files[i]) + 1; | |
3679 ss = lalloc(len, TRUE); | |
3680 if (ss != NULL) | |
3681 { | |
3682 *ss = NUL; | |
3683 for (i = 0; i < xp->xp_numfiles; ++i) | |
3684 { | |
3685 STRCAT(ss, xp->xp_files[i]); | |
3686 if (i != xp->xp_numfiles - 1) | |
3687 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); | |
3688 } | |
3689 } | |
3690 } | |
3691 | |
3692 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) | |
3693 ExpandCleanup(xp); | |
3694 | |
1412 | 3695 /* Free "orig" if it wasn't stored in "orig_save". */ |
1432 | 3696 if (!orig_saved) |
1412 | 3697 vim_free(orig); |
3698 | |
7 | 3699 return ss; |
3700 } | |
3701 | |
3702 /* | |
3703 * Prepare an expand structure for use. | |
3704 */ | |
3705 void | |
3706 ExpandInit(xp) | |
3707 expand_T *xp; | |
3708 { | |
1718 | 3709 xp->xp_pattern = NULL; |
1965 | 3710 xp->xp_pattern_len = 0; |
7 | 3711 xp->xp_backslash = XP_BS_NONE; |
632 | 3712 #ifndef BACKSLASH_IN_FILENAME |
3713 xp->xp_shell = FALSE; | |
3714 #endif | |
7 | 3715 xp->xp_numfiles = -1; |
3716 xp->xp_files = NULL; | |
632 | 3717 #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
3718 xp->xp_arg = NULL; | |
3719 #endif | |
7 | 3720 } |
3721 | |
3722 /* | |
3723 * Cleanup an expand structure after use. | |
3724 */ | |
3725 void | |
3726 ExpandCleanup(xp) | |
3727 expand_T *xp; | |
3728 { | |
3729 if (xp->xp_numfiles >= 0) | |
3730 { | |
3731 FreeWild(xp->xp_numfiles, xp->xp_files); | |
3732 xp->xp_numfiles = -1; | |
3733 } | |
3734 } | |
3735 | |
3736 void | |
3737 ExpandEscape(xp, str, numfiles, files, options) | |
3738 expand_T *xp; | |
3739 char_u *str; | |
3740 int numfiles; | |
3741 char_u **files; | |
3742 int options; | |
3743 { | |
3744 int i; | |
3745 char_u *p; | |
3746 | |
3747 /* | |
3748 * May change home directory back to "~" | |
3749 */ | |
3750 if (options & WILD_HOME_REPLACE) | |
3751 tilde_replace(str, numfiles, files); | |
3752 | |
3753 if (options & WILD_ESCAPE) | |
3754 { | |
3755 if (xp->xp_context == EXPAND_FILES | |
2778 | 3756 || xp->xp_context == EXPAND_FILES_IN_PATH |
714 | 3757 || xp->xp_context == EXPAND_SHELLCMD |
7 | 3758 || xp->xp_context == EXPAND_BUFFERS |
3759 || xp->xp_context == EXPAND_DIRECTORIES) | |
3760 { | |
3761 /* | |
3762 * Insert a backslash into a file name before a space, \, %, # | |
3763 * and wildmatch characters, except '~'. | |
3764 */ | |
3765 for (i = 0; i < numfiles; ++i) | |
3766 { | |
3767 /* for ":set path=" we need to escape spaces twice */ | |
3768 if (xp->xp_backslash == XP_BS_THREE) | |
3769 { | |
3770 p = vim_strsave_escaped(files[i], (char_u *)" "); | |
3771 if (p != NULL) | |
3772 { | |
3773 vim_free(files[i]); | |
3774 files[i] = p; | |
719 | 3775 #if defined(BACKSLASH_IN_FILENAME) |
7 | 3776 p = vim_strsave_escaped(files[i], (char_u *)" "); |
3777 if (p != NULL) | |
3778 { | |
3779 vim_free(files[i]); | |
3780 files[i] = p; | |
3781 } | |
3782 #endif | |
3783 } | |
3784 } | |
1589 | 3785 #ifdef BACKSLASH_IN_FILENAME |
3786 p = vim_strsave_fnameescape(files[i], FALSE); | |
3787 #else | |
1586 | 3788 p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
1589 | 3789 #endif |
7 | 3790 if (p != NULL) |
3791 { | |
3792 vim_free(files[i]); | |
3793 files[i] = p; | |
3794 } | |
3795 | |
3796 /* If 'str' starts with "\~", replace "~" at start of | |
3797 * files[i] with "\~". */ | |
3798 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') | |
435 | 3799 escape_fname(&files[i]); |
7 | 3800 } |
3801 xp->xp_backslash = XP_BS_NONE; | |
435 | 3802 |
3803 /* If the first file starts with a '+' escape it. Otherwise it | |
3804 * could be seen as "+cmd". */ | |
3805 if (*files[0] == '+') | |
3806 escape_fname(&files[0]); | |
7 | 3807 } |
3808 else if (xp->xp_context == EXPAND_TAGS) | |
3809 { | |
3810 /* | |
3811 * Insert a backslash before characters in a tag name that | |
3812 * would terminate the ":tag" command. | |
3813 */ | |
3814 for (i = 0; i < numfiles; ++i) | |
3815 { | |
3816 p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); | |
3817 if (p != NULL) | |
3818 { | |
3819 vim_free(files[i]); | |
3820 files[i] = p; | |
3821 } | |
3822 } | |
3823 } | |
3824 } | |
3825 } | |
3826 | |
3827 /* | |
1586 | 3828 * Escape special characters in "fname" for when used as a file name argument |
3829 * after a Vim command, or, when "shell" is non-zero, a shell command. | |
3830 * Returns the result in allocated memory. | |
3831 */ | |
3832 char_u * | |
3833 vim_strsave_fnameescape(fname, shell) | |
3834 char_u *fname; | |
3835 int shell; | |
3836 { | |
1685 | 3837 char_u *p; |
1586 | 3838 #ifdef BACKSLASH_IN_FILENAME |
3839 char_u buf[20]; | |
3840 int j = 0; | |
3841 | |
3842 /* Don't escape '[' and '{' if they are in 'isfname'. */ | |
3843 for (p = PATH_ESC_CHARS; *p != NUL; ++p) | |
3844 if ((*p != '[' && *p != '{') || !vim_isfilec(*p)) | |
3845 buf[j++] = *p; | |
3846 buf[j] = NUL; | |
1700 | 3847 p = vim_strsave_escaped(fname, buf); |
1586 | 3848 #else |
1685 | 3849 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
3850 if (shell && csh_like_shell() && p != NULL) | |
3851 { | |
3852 char_u *s; | |
3853 | |
3854 /* For csh and similar shells need to put two backslashes before '!'. | |
3855 * One is taken by Vim, one by the shell. */ | |
3856 s = vim_strsave_escaped(p, (char_u *)"!"); | |
3857 vim_free(p); | |
3858 p = s; | |
3859 } | |
1700 | 3860 #endif |
3861 | |
3862 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and | |
3863 * ":write". "cd -" has a special meaning. */ | |
2445
04dae202d316
Fixes for coverity warnings.
Bram Moolenaar <bram@vim.org>
parents:
2433
diff
changeset
|
3864 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
1700 | 3865 escape_fname(&p); |
3866 | |
1685 | 3867 return p; |
1586 | 3868 } |
3869 | |
3870 /* | |
435 | 3871 * Put a backslash before the file name in "pp", which is in allocated memory. |
3872 */ | |
3873 static void | |
3874 escape_fname(pp) | |
3875 char_u **pp; | |
3876 { | |
3877 char_u *p; | |
3878 | |
3879 p = alloc((unsigned)(STRLEN(*pp) + 2)); | |
3880 if (p != NULL) | |
3881 { | |
3882 p[0] = '\\'; | |
3883 STRCPY(p + 1, *pp); | |
3884 vim_free(*pp); | |
3885 *pp = p; | |
3886 } | |
3887 } | |
3888 | |
3889 /* | |
7 | 3890 * For each file name in files[num_files]: |
3891 * If 'orig_pat' starts with "~/", replace the home directory with "~". | |
3892 */ | |
3893 void | |
3894 tilde_replace(orig_pat, num_files, files) | |
3895 char_u *orig_pat; | |
3896 int num_files; | |
3897 char_u **files; | |
3898 { | |
3899 int i; | |
3900 char_u *p; | |
3901 | |
3902 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) | |
3903 { | |
3904 for (i = 0; i < num_files; ++i) | |
3905 { | |
3906 p = home_replace_save(NULL, files[i]); | |
3907 if (p != NULL) | |
3908 { | |
3909 vim_free(files[i]); | |
3910 files[i] = p; | |
3911 } | |
3912 } | |
3913 } | |
3914 } | |
3915 | |
3916 /* | |
3917 * Show all matches for completion on the command line. | |
3918 * Returns EXPAND_NOTHING when the character that triggered expansion should | |
3919 * be inserted like a normal character. | |
3920 */ | |
3921 static int | |
3922 showmatches(xp, wildmenu) | |
3923 expand_T *xp; | |
1877 | 3924 int wildmenu UNUSED; |
7 | 3925 { |
3926 #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) | |
3927 int num_files; | |
3928 char_u **files_found; | |
3929 int i, j, k; | |
3930 int maxlen; | |
3931 int lines; | |
3932 int columns; | |
3933 char_u *p; | |
3934 int lastlen; | |
3935 int attr; | |
3936 int showtail; | |
3937 | |
3938 if (xp->xp_numfiles == -1) | |
3939 { | |
3940 set_expand_context(xp); | |
3941 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, | |
3942 &num_files, &files_found); | |
3943 showtail = expand_showtail(xp); | |
3944 if (i != EXPAND_OK) | |
3945 return i; | |
3946 | |
3947 } | |
3948 else | |
3949 { | |
3950 num_files = xp->xp_numfiles; | |
3951 files_found = xp->xp_files; | |
3952 showtail = cmd_showtail; | |
3953 } | |
3954 | |
3955 #ifdef FEAT_WILDMENU | |
3956 if (!wildmenu) | |
3957 { | |
3958 #endif | |
3959 msg_didany = FALSE; /* lines_left will be set */ | |
3960 msg_start(); /* prepare for paging */ | |
3961 msg_putchar('\n'); | |
3962 out_flush(); | |
3963 cmdline_row = msg_row; | |
3964 msg_didany = FALSE; /* lines_left will be set again */ | |
3965 msg_start(); /* prepare for paging */ | |
3966 #ifdef FEAT_WILDMENU | |
3967 } | |
3968 #endif | |
3969 | |
3970 if (got_int) | |
3971 got_int = FALSE; /* only int. the completion, not the cmd line */ | |
3972 #ifdef FEAT_WILDMENU | |
3973 else if (wildmenu) | |
3974 win_redr_status_matches(xp, num_files, files_found, 0, showtail); | |
3975 #endif | |
3976 else | |
3977 { | |
3978 /* find the length of the longest file name */ | |
3979 maxlen = 0; | |
3980 for (i = 0; i < num_files; ++i) | |
3981 { | |
3982 if (!showtail && (xp->xp_context == EXPAND_FILES | |
714 | 3983 || xp->xp_context == EXPAND_SHELLCMD |
7 | 3984 || xp->xp_context == EXPAND_BUFFERS)) |
3985 { | |
3986 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); | |
3987 j = vim_strsize(NameBuff); | |
3988 } | |
3989 else | |
3990 j = vim_strsize(L_SHOWFILE(i)); | |
3991 if (j > maxlen) | |
3992 maxlen = j; | |
3993 } | |
3994 | |
3995 if (xp->xp_context == EXPAND_TAGS_LISTFILES) | |
3996 lines = num_files; | |
3997 else | |
3998 { | |
3999 /* compute the number of columns and lines for the listing */ | |
4000 maxlen += 2; /* two spaces between file names */ | |
4001 columns = ((int)Columns + 2) / maxlen; | |
4002 if (columns < 1) | |
4003 columns = 1; | |
4004 lines = (num_files + columns - 1) / columns; | |
4005 } | |
4006 | |
4007 attr = hl_attr(HLF_D); /* find out highlighting for directories */ | |
4008 | |
4009 if (xp->xp_context == EXPAND_TAGS_LISTFILES) | |
4010 { | |
4011 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T)); | |
4012 msg_clr_eos(); | |
4013 msg_advance(maxlen - 3); | |
4014 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T)); | |
4015 } | |
4016 | |
4017 /* list the files line by line */ | |
4018 for (i = 0; i < lines; ++i) | |
4019 { | |
4020 lastlen = 999; | |
4021 for (k = i; k < num_files; k += lines) | |
4022 { | |
4023 if (xp->xp_context == EXPAND_TAGS_LISTFILES) | |
4024 { | |
4025 msg_outtrans_attr(files_found[k], hl_attr(HLF_D)); | |
4026 p = files_found[k] + STRLEN(files_found[k]) + 1; | |
4027 msg_advance(maxlen + 1); | |
4028 msg_puts(p); | |
4029 msg_advance(maxlen + 3); | |
4030 msg_puts_long_attr(p + 2, hl_attr(HLF_D)); | |
4031 break; | |
4032 } | |
4033 for (j = maxlen - lastlen; --j >= 0; ) | |
4034 msg_putchar(' '); | |
4035 if (xp->xp_context == EXPAND_FILES | |
714 | 4036 || xp->xp_context == EXPAND_SHELLCMD |
7 | 4037 || xp->xp_context == EXPAND_BUFFERS) |
4038 { | |
2118
63bf37c1e7a2
updated for version 7.2.401
Bram Moolenaar <bram@zimbu.org>
parents:
2099
diff
changeset
|
4039 /* highlight directories */ |
2128
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4040 if (xp->xp_numfiles != -1) |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4041 { |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4042 char_u *halved_slash; |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4043 char_u *exp_path; |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4044 |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4045 /* Expansion was done before and special characters |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4046 * were escaped, need to halve backslashes. Also |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4047 * $HOME has been replaced with ~/. */ |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4048 exp_path = expand_env_save_opt(files_found[k], TRUE); |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4049 halved_slash = backslash_halve_save( |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4050 exp_path != NULL ? exp_path : files_found[k]); |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4051 j = mch_isdir(halved_slash != NULL ? halved_slash |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4052 : files_found[k]); |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4053 vim_free(exp_path); |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4054 vim_free(halved_slash); |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4055 } |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4056 else |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4057 /* Expansion was done here, file names are literal. */ |
a16af0072ea8
updated for version 7.2.410
Bram Moolenaar <bram@zimbu.org>
parents:
2118
diff
changeset
|
4058 j = mch_isdir(files_found[k]); |
7 | 4059 if (showtail) |
4060 p = L_SHOWFILE(k); | |
4061 else | |
4062 { | |
4063 home_replace(NULL, files_found[k], NameBuff, MAXPATHL, | |
4064 TRUE); | |
4065 p = NameBuff; | |
4066 } | |
4067 } | |
4068 else | |
4069 { | |
4070 j = FALSE; | |
4071 p = L_SHOWFILE(k); | |
4072 } | |
4073 lastlen = msg_outtrans_attr(p, j ? attr : 0); | |
4074 } | |
4075 if (msg_col > 0) /* when not wrapped around */ | |
4076 { | |
4077 msg_clr_eos(); | |
4078 msg_putchar('\n'); | |
4079 } | |
4080 out_flush(); /* show one line at a time */ | |
4081 if (got_int) | |
4082 { | |
4083 got_int = FALSE; | |
4084 break; | |
4085 } | |
4086 } | |
4087 | |
4088 /* | |
4089 * we redraw the command below the lines that we have just listed | |
4090 * This is a bit tricky, but it saves a lot of screen updating. | |
4091 */ | |
4092 cmdline_row = msg_row; /* will put it back later */ | |
4093 } | |
4094 | |
4095 if (xp->xp_numfiles == -1) | |
4096 FreeWild(num_files, files_found); | |
4097 | |
4098 return EXPAND_OK; | |
4099 } | |
4100 | |
4101 /* | |
4102 * Private gettail for showmatches() (and win_redr_status_matches()): | |
4103 * Find tail of file name path, but ignore trailing "/". | |
4104 */ | |
4105 char_u * | |
4106 sm_gettail(s) | |
4107 char_u *s; | |
4108 { | |
4109 char_u *p; | |
4110 char_u *t = s; | |
4111 int had_sep = FALSE; | |
4112 | |
4113 for (p = s; *p != NUL; ) | |
4114 { | |
4115 if (vim_ispathsep(*p) | |
4116 #ifdef BACKSLASH_IN_FILENAME | |
4117 && !rem_backslash(p) | |
4118 #endif | |
4119 ) | |
4120 had_sep = TRUE; | |
4121 else if (had_sep) | |
4122 { | |
4123 t = p; | |
4124 had_sep = FALSE; | |
4125 } | |
39 | 4126 mb_ptr_adv(p); |
7 | 4127 } |
4128 return t; | |
4129 } | |
4130 | |
4131 /* | |
4132 * Return TRUE if we only need to show the tail of completion matches. | |
4133 * When not completing file names or there is a wildcard in the path FALSE is | |
4134 * returned. | |
4135 */ | |
4136 static int | |
4137 expand_showtail(xp) | |
4138 expand_T *xp; | |
4139 { | |
4140 char_u *s; | |
4141 char_u *end; | |
4142 | |
4143 /* When not completing file names a "/" may mean something different. */ | |
714 | 4144 if (xp->xp_context != EXPAND_FILES |
4145 && xp->xp_context != EXPAND_SHELLCMD | |
4146 && xp->xp_context != EXPAND_DIRECTORIES) | |
7 | 4147 return FALSE; |
4148 | |
4149 end = gettail(xp->xp_pattern); | |
4150 if (end == xp->xp_pattern) /* there is no path separator */ | |
4151 return FALSE; | |
4152 | |
4153 for (s = xp->xp_pattern; s < end; s++) | |
4154 { | |
4155 /* Skip escaped wildcards. Only when the backslash is not a path | |
4156 * separator, on DOS the '*' "path\*\file" must not be skipped. */ | |
4157 if (rem_backslash(s)) | |
4158 ++s; | |
4159 else if (vim_strchr((char_u *)"*?[", *s) != NULL) | |
4160 return FALSE; | |
4161 } | |
4162 return TRUE; | |
4163 } | |
4164 | |
4165 /* | |
4166 * Prepare a string for expansion. | |
4167 * When expanding file names: The string will be used with expand_wildcards(). | |
4168 * Copy the file name into allocated memory and add a '*' at the end. | |
4169 * When expanding other names: The string will be used with regcomp(). Copy | |
4170 * the name into allocated memory and prepend "^". | |
4171 */ | |
4172 char_u * | |
4173 addstar(fname, len, context) | |
4174 char_u *fname; | |
4175 int len; | |
4176 int context; /* EXPAND_FILES etc. */ | |
4177 { | |
4178 char_u *retval; | |
4179 int i, j; | |
4180 int new_len; | |
4181 char_u *tail; | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4182 int ends_in_star; |
7 | 4183 |
714 | 4184 if (context != EXPAND_FILES |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4185 && context != EXPAND_FILES_IN_PATH |
714 | 4186 && context != EXPAND_SHELLCMD |
4187 && context != EXPAND_DIRECTORIES) | |
7 | 4188 { |
4189 /* | |
4190 * Matching will be done internally (on something other than files). | |
4191 * So we convert the file-matching-type wildcards into our kind for | |
4192 * use with vim_regcomp(). First work out how long it will be: | |
4193 */ | |
4194 | |
4195 /* For help tags the translation is done in find_help_tags(). | |
4196 * For a tag pattern starting with "/" no translation is needed. */ | |
4197 if (context == EXPAND_HELP | |
4198 || context == EXPAND_COLORS | |
4199 || context == EXPAND_COMPILER | |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
4200 || context == EXPAND_OWNSYNTAX |
2268
aafed4a4866f
Command line completion for :ownsyntax. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2243
diff
changeset
|
4201 || context == EXPAND_FILETYPE |
7 | 4202 || (context == EXPAND_TAGS && fname[0] == '/')) |
4203 retval = vim_strnsave(fname, len); | |
4204 else | |
4205 { | |
4206 new_len = len + 2; /* +2 for '^' at start, NUL at end */ | |
4207 for (i = 0; i < len; i++) | |
4208 { | |
4209 if (fname[i] == '*' || fname[i] == '~') | |
4210 new_len++; /* '*' needs to be replaced by ".*" | |
4211 '~' needs to be replaced by "\~" */ | |
4212 | |
4213 /* Buffer names are like file names. "." should be literal */ | |
4214 if (context == EXPAND_BUFFERS && fname[i] == '.') | |
4215 new_len++; /* "." becomes "\." */ | |
4216 | |
4217 /* Custom expansion takes care of special things, match | |
4218 * backslashes literally (perhaps also for other types?) */ | |
634 | 4219 if ((context == EXPAND_USER_DEFINED |
4220 || context == EXPAND_USER_LIST) && fname[i] == '\\') | |
7 | 4221 new_len++; /* '\' becomes "\\" */ |
4222 } | |
4223 retval = alloc(new_len); | |
4224 if (retval != NULL) | |
4225 { | |
4226 retval[0] = '^'; | |
4227 j = 1; | |
4228 for (i = 0; i < len; i++, j++) | |
4229 { | |
4230 /* Skip backslash. But why? At least keep it for custom | |
4231 * expansion. */ | |
4232 if (context != EXPAND_USER_DEFINED | |
407 | 4233 && context != EXPAND_USER_LIST |
4234 && fname[i] == '\\' | |
4235 && ++i == len) | |
7 | 4236 break; |
4237 | |
4238 switch (fname[i]) | |
4239 { | |
4240 case '*': retval[j++] = '.'; | |
4241 break; | |
4242 case '~': retval[j++] = '\\'; | |
4243 break; | |
4244 case '?': retval[j] = '.'; | |
4245 continue; | |
4246 case '.': if (context == EXPAND_BUFFERS) | |
4247 retval[j++] = '\\'; | |
4248 break; | |
407 | 4249 case '\\': if (context == EXPAND_USER_DEFINED |
4250 || context == EXPAND_USER_LIST) | |
7 | 4251 retval[j++] = '\\'; |
4252 break; | |
4253 } | |
4254 retval[j] = fname[i]; | |
4255 } | |
4256 retval[j] = NUL; | |
4257 } | |
4258 } | |
4259 } | |
4260 else | |
4261 { | |
4262 retval = alloc(len + 4); | |
4263 if (retval != NULL) | |
4264 { | |
419 | 4265 vim_strncpy(retval, fname, len); |
7 | 4266 |
4267 /* | |
831 | 4268 * Don't add a star to *, ~, ~user, $var or `cmd`. |
4269 * * would become **, which walks the whole tree. | |
7 | 4270 * ~ would be at the start of the file name, but not the tail. |
4271 * $ could be anywhere in the tail. | |
4272 * ` could be anywhere in the file name. | |
1484 | 4273 * When the name ends in '$' don't add a star, remove the '$'. |
7 | 4274 */ |
4275 tail = gettail(retval); | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4276 ends_in_star = (len > 0 && retval[len - 1] == '*'); |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4277 #ifndef BACKSLASH_IN_FILENAME |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4278 for (i = len - 2; i >= 0; --i) |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4279 { |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4280 if (retval[i] != '\\') |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4281 break; |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4282 ends_in_star = !ends_in_star; |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4283 } |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4284 #endif |
7 | 4285 if ((*retval != '~' || tail != retval) |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2128
diff
changeset
|
4286 && !ends_in_star |
7 | 4287 && vim_strchr(tail, '$') == NULL |
4288 && vim_strchr(retval, '`') == NULL) | |
4289 retval[len++] = '*'; | |
1484 | 4290 else if (len > 0 && retval[len - 1] == '$') |
4291 --len; | |
7 | 4292 retval[len] = NUL; |
4293 } | |
4294 } | |
4295 return retval; | |
4296 } | |
4297 | |
4298 /* | |
4299 * Must parse the command line so far to work out what context we are in. | |
4300 * Completion can then be done based on that context. | |
4301 * This routine sets the variables: | |
4302 * xp->xp_pattern The start of the pattern to be expanded within | |
4303 * the command line (ends at the cursor). | |
4304 * xp->xp_context The type of thing to expand. Will be one of: | |
4305 * | |
4306 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on | |
4307 * the command line, like an unknown command. Caller | |
4308 * should beep. | |
4309 * EXPAND_NOTHING Unrecognised context for completion, use char like | |
4310 * a normal char, rather than for completion. eg | |
4311 * :s/^I/ | |
4312 * EXPAND_COMMANDS Cursor is still touching the command, so complete | |
4313 * it. | |
4314 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. | |
4315 * EXPAND_FILES After command with XFILE set, or after setting | |
4316 * with P_EXPAND set. eg :e ^I, :w>>^I | |
4317 * EXPAND_DIRECTORIES In some cases this is used instead of the latter | |
4318 * when we know only directories are of interest. eg | |
4319 * :set dir=^I | |
714 | 4320 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
7 | 4321 * EXPAND_SETTINGS Complete variable names. eg :set d^I |
4322 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I | |
4323 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I | |
4324 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect | |
4325 * EXPAND_HELP Complete tags from the file 'helpfile'/tags | |
4326 * EXPAND_EVENTS Complete event names | |
4327 * EXPAND_SYNTAX Complete :syntax command arguments | |
4328 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names | |
4329 * EXPAND_AUGROUP Complete autocommand group names | |
4330 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I | |
4331 * EXPAND_MAPPINGS Complete mapping and abbreviation names, | |
4332 * eg :unmap a^I , :cunab x^I | |
4333 * EXPAND_FUNCTIONS Complete internal or user defined function names, | |
4334 * eg :call sub^I | |
4335 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I | |
4336 * EXPAND_EXPRESSION Complete internal or user defined function/variable | |
4337 * names in expressions, eg :while s^I | |
4338 * EXPAND_ENV_VARS Complete environment variable names | |
3744 | 4339 * EXPAND_USER Complete user names |
7 | 4340 */ |
4341 static void | |
4342 set_expand_context(xp) | |
4343 expand_T *xp; | |
4344 { | |
168 | 4345 /* only expansion for ':', '>' and '=' command-lines */ |
7 | 4346 if (ccline.cmdfirstc != ':' |
4347 #ifdef FEAT_EVAL | |
168 | 4348 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
531 | 4349 && !ccline.input_fn |
7 | 4350 #endif |
4351 ) | |
4352 { | |
4353 xp->xp_context = EXPAND_NOTHING; | |
4354 return; | |
4355 } | |
4356 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos); | |
4357 } | |
4358 | |
4359 void | |
4360 set_cmd_context(xp, str, len, col) | |
4361 expand_T *xp; | |
4362 char_u *str; /* start of command line */ | |
4363 int len; /* length of command line (excl. NUL) */ | |
4364 int col; /* position of cursor */ | |
4365 { | |
4366 int old_char = NUL; | |
4367 char_u *nextcomm; | |
4368 | |
4369 /* | |
4370 * Avoid a UMR warning from Purify, only save the character if it has been | |
4371 * written before. | |
4372 */ | |
4373 if (col < len) | |
4374 old_char = str[col]; | |
4375 str[col] = NUL; | |
4376 nextcomm = str; | |
168 | 4377 |
4378 #ifdef FEAT_EVAL | |
4379 if (ccline.cmdfirstc == '=') | |
1322 | 4380 { |
4381 # ifdef FEAT_CMDL_COMPL | |
168 | 4382 /* pass CMD_SIZE because there is no real command */ |
4383 set_context_for_expression(xp, str, CMD_SIZE); | |
1322 | 4384 # endif |
4385 } | |
531 | 4386 else if (ccline.input_fn) |
4387 { | |
4388 xp->xp_context = ccline.xp_context; | |
4389 xp->xp_pattern = ccline.cmdbuff; | |
1322 | 4390 # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
531 | 4391 xp->xp_arg = ccline.xp_arg; |
1322 | 4392 # endif |
531 | 4393 } |
168 | 4394 else |
4395 #endif | |
4396 while (nextcomm != NULL) | |
4397 nextcomm = set_one_cmd_context(xp, nextcomm); | |
4398 | |
7 | 4399 str[col] = old_char; |
4400 } | |
4401 | |
4402 /* | |
4403 * Expand the command line "str" from context "xp". | |
4404 * "xp" must have been set by set_cmd_context(). | |
4405 * xp->xp_pattern points into "str", to where the text that is to be expanded | |
4406 * starts. | |
4407 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the | |
4408 * cursor. | |
4409 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the | |
4410 * key that triggered expansion literally. | |
4411 * Returns EXPAND_OK otherwise. | |
4412 */ | |
4413 int | |
4414 expand_cmdline(xp, str, col, matchcount, matches) | |
4415 expand_T *xp; | |
4416 char_u *str; /* start of command line */ | |
4417 int col; /* position of cursor */ | |
4418 int *matchcount; /* return: nr of matches */ | |
4419 char_u ***matches; /* return: array of pointers to matches */ | |
4420 { | |
4421 char_u *file_str = NULL; | |
2652 | 4422 int options = WILD_ADD_SLASH|WILD_SILENT; |
7 | 4423 |
4424 if (xp->xp_context == EXPAND_UNSUCCESSFUL) | |
4425 { | |
4426 beep_flush(); | |
4427 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ | |
4428 } | |
4429 if (xp->xp_context == EXPAND_NOTHING) | |
4430 { | |
4431 /* Caller can use the character as a normal char instead */ | |
4432 return EXPAND_NOTHING; | |
4433 } | |
4434 | |
4435 /* add star to file name, or convert to regexp if not exp. files. */ | |
1965 | 4436 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
4437 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context); | |
7 | 4438 if (file_str == NULL) |
4439 return EXPAND_UNSUCCESSFUL; | |
4440 | |
2652 | 4441 if (p_wic) |
4442 options += WILD_ICASE; | |
4443 | |
7 | 4444 /* find all files that match the description */ |
2652 | 4445 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
7 | 4446 { |
4447 *matchcount = 0; | |
4448 *matches = NULL; | |
4449 } | |
4450 vim_free(file_str); | |
4451 | |
4452 return EXPAND_OK; | |
4453 } | |
4454 | |
4455 #ifdef FEAT_MULTI_LANG | |
4456 /* | |
4457 * Cleanup matches for help tags: remove "@en" if "en" is the only language. | |
4458 */ | |
4459 static void cleanup_help_tags __ARGS((int num_file, char_u **file)); | |
4460 | |
4461 static void | |
4462 cleanup_help_tags(num_file, file) | |
4463 int num_file; | |
4464 char_u **file; | |
4465 { | |
4466 int i, j; | |
4467 int len; | |
4468 | |
4469 for (i = 0; i < num_file; ++i) | |
4470 { | |
4471 len = (int)STRLEN(file[i]) - 3; | |
4472 if (len > 0 && STRCMP(file[i] + len, "@en") == 0) | |
4473 { | |
4474 /* Sorting on priority means the same item in another language may | |
4475 * be anywhere. Search all items for a match up to the "@en". */ | |
4476 for (j = 0; j < num_file; ++j) | |
4477 if (j != i | |
4478 && (int)STRLEN(file[j]) == len + 3 | |
4479 && STRNCMP(file[i], file[j], len + 1) == 0) | |
4480 break; | |
4481 if (j == num_file) | |
4482 file[i][len] = NUL; | |
4483 } | |
4484 } | |
4485 } | |
4486 #endif | |
4487 | |
4488 /* | |
4489 * Do the expansion based on xp->xp_context and "pat". | |
4490 */ | |
4491 static int | |
4492 ExpandFromContext(xp, pat, num_file, file, options) | |
4493 expand_T *xp; | |
4494 char_u *pat; | |
4495 int *num_file; | |
4496 char_u ***file; | |
2652 | 4497 int options; /* EW_ flags */ |
7 | 4498 { |
4499 #ifdef FEAT_CMDL_COMPL | |
4500 regmatch_T regmatch; | |
4501 #endif | |
4502 int ret; | |
4503 int flags; | |
4504 | |
4505 flags = EW_DIR; /* include directories */ | |
4506 if (options & WILD_LIST_NOTFOUND) | |
4507 flags |= EW_NOTFOUND; | |
4508 if (options & WILD_ADD_SLASH) | |
4509 flags |= EW_ADDSLASH; | |
4510 if (options & WILD_KEEP_ALL) | |
4511 flags |= EW_KEEPALL; | |
4512 if (options & WILD_SILENT) | |
4513 flags |= EW_SILENT; | |
4514 | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4515 if (xp->xp_context == EXPAND_FILES |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4516 || xp->xp_context == EXPAND_DIRECTORIES |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4517 || xp->xp_context == EXPAND_FILES_IN_PATH) |
7 | 4518 { |
4519 /* | |
4520 * Expand file or directory names. | |
4521 */ | |
4522 int free_pat = FALSE; | |
4523 int i; | |
4524 | |
4525 /* for ":set path=" and ":set tags=" halve backslashes for escaped | |
4526 * space */ | |
4527 if (xp->xp_backslash != XP_BS_NONE) | |
4528 { | |
4529 free_pat = TRUE; | |
4530 pat = vim_strsave(pat); | |
4531 for (i = 0; pat[i]; ++i) | |
4532 if (pat[i] == '\\') | |
4533 { | |
4534 if (xp->xp_backslash == XP_BS_THREE | |
4535 && pat[i + 1] == '\\' | |
4536 && pat[i + 2] == '\\' | |
4537 && pat[i + 3] == ' ') | |
1621 | 4538 STRMOVE(pat + i, pat + i + 3); |
7 | 4539 if (xp->xp_backslash == XP_BS_ONE |
4540 && pat[i + 1] == ' ') | |
1621 | 4541 STRMOVE(pat + i, pat + i + 1); |
7 | 4542 } |
4543 } | |
4544 | |
4545 if (xp->xp_context == EXPAND_FILES) | |
4546 flags |= EW_FILE; | |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4547 else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2302
diff
changeset
|
4548 flags |= (EW_FILE | EW_PATH); |
7 | 4549 else |
4550 flags = (flags | EW_DIR) & ~EW_FILE; | |
2652 | 4551 if (options & WILD_ICASE) |
4552 flags |= EW_ICASE; | |
4553 | |
2016 | 4554 /* Expand wildcards, supporting %:h and the like. */ |
4555 ret = expand_wildcards_eval(&pat, num_file, file, flags); | |
7 | 4556 if (free_pat) |
4557 vim_free(pat); | |
4558 return ret; | |
4559 } | |
4560 | |
4561 *file = (char_u **)""; | |
4562 *num_file = 0; | |
4563 if (xp->xp_context == EXPAND_HELP) | |
4564 { | |
1696 | 4565 /* With an empty argument we would get all the help tags, which is |
4566 * very slow. Get matches for "help" instead. */ | |
4567 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, | |
4568 num_file, file, FALSE) == OK) | |
7 | 4569 { |
4570 #ifdef FEAT_MULTI_LANG | |
4571 cleanup_help_tags(*num_file, *file); | |
4572 #endif | |
4573 return OK; | |
4574 } | |
4575 return FAIL; | |
4576 } | |
4577 | |
4578 #ifndef FEAT_CMDL_COMPL | |
4579 return FAIL; | |
4580 #else | |
716 | 4581 if (xp->xp_context == EXPAND_SHELLCMD) |
4582 return expand_shellcmd(pat, num_file, file, flags); | |
7 | 4583 if (xp->xp_context == EXPAND_OLD_SETTING) |
4584 return ExpandOldSetting(num_file, file); | |
4585 if (xp->xp_context == EXPAND_BUFFERS) | |
4586 return ExpandBufnames(pat, num_file, file, options); | |
4587 if (xp->xp_context == EXPAND_TAGS | |
4588 || xp->xp_context == EXPAND_TAGS_LISTFILES) | |
4589 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); | |
4590 if (xp->xp_context == EXPAND_COLORS) | |
2929 | 4591 { |
4592 char *directories[] = {"colors", NULL}; | |
4593 return ExpandRTDir(pat, num_file, file, directories); | |
4594 } | |
7 | 4595 if (xp->xp_context == EXPAND_COMPILER) |
2929 | 4596 { |
3106 | 4597 char *directories[] = {"compiler", NULL}; |
2929 | 4598 return ExpandRTDir(pat, num_file, file, directories); |
4599 } | |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
4600 if (xp->xp_context == EXPAND_OWNSYNTAX) |
2929 | 4601 { |
4602 char *directories[] = {"syntax", NULL}; | |
4603 return ExpandRTDir(pat, num_file, file, directories); | |
4604 } | |
2268
aafed4a4866f
Command line completion for :ownsyntax. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2243
diff
changeset
|
4605 if (xp->xp_context == EXPAND_FILETYPE) |
2929 | 4606 { |
4607 char *directories[] = {"syntax", "indent", "ftplugin", NULL}; | |
4608 return ExpandRTDir(pat, num_file, file, directories); | |
4609 } | |
407 | 4610 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
4611 if (xp->xp_context == EXPAND_USER_LIST) | |
856 | 4612 return ExpandUserList(xp, num_file, file); |
407 | 4613 # endif |
7 | 4614 |
4615 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); | |
4616 if (regmatch.regprog == NULL) | |
4617 return FAIL; | |
4618 | |
4619 /* set ignore-case according to p_ic, p_scs and pat */ | |
4620 regmatch.rm_ic = ignorecase(pat); | |
4621 | |
4622 if (xp->xp_context == EXPAND_SETTINGS | |
4623 || xp->xp_context == EXPAND_BOOL_SETTINGS) | |
4624 ret = ExpandSettings(xp, ®match, num_file, file); | |
4625 else if (xp->xp_context == EXPAND_MAPPINGS) | |
4626 ret = ExpandMappings(®match, num_file, file); | |
4627 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) | |
4628 else if (xp->xp_context == EXPAND_USER_DEFINED) | |
4629 ret = ExpandUserDefined(xp, ®match, num_file, file); | |
4630 # endif | |
4631 else | |
4632 { | |
4633 static struct expgen | |
4634 { | |
4635 int context; | |
4636 char_u *((*func)__ARGS((expand_T *, int))); | |
4637 int ic; | |
2849 | 4638 int escaped; |
7 | 4639 } tab[] = |
4640 { | |
2849 | 4641 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
4642 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, | |
3503 | 4643 #ifdef FEAT_CMDHIST |
4644 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, | |
4645 #endif | |
7 | 4646 #ifdef FEAT_USR_CMDS |
2849 | 4647 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
4648 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, | |
4649 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, | |
4650 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, | |
7 | 4651 #endif |
4652 #ifdef FEAT_EVAL | |
2849 | 4653 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
4654 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, | |
4655 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, | |
4656 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, | |
7 | 4657 #endif |
4658 #ifdef FEAT_MENU | |
2849 | 4659 {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
4660 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, | |
7 | 4661 #endif |
4662 #ifdef FEAT_SYN_HL | |
2849 | 4663 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
4664 #endif | |
4665 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, | |
7 | 4666 #ifdef FEAT_AUTOCMD |
2849 | 4667 {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
4668 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, | |
7 | 4669 #endif |
1845 | 4670 #ifdef FEAT_CSCOPE |
2849 | 4671 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
1845 | 4672 #endif |
1868 | 4673 #ifdef FEAT_SIGNS |
2849 | 4674 {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
1868 | 4675 #endif |
2068
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
4676 #ifdef FEAT_PROFILE |
2849 | 4677 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
2068
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
4678 #endif |
7 | 4679 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
4680 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) | |
2849 | 4681 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
4682 {EXPAND_LOCALES, get_locales, TRUE, FALSE}, | |
4683 #endif | |
4684 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, | |
3744 | 4685 {EXPAND_USER, get_users, TRUE, FALSE}, |
7 | 4686 }; |
4687 int i; | |
4688 | |
4689 /* | |
4690 * Find a context in the table and call the ExpandGeneric() with the | |
4691 * right function to do the expansion. | |
4692 */ | |
4693 ret = FAIL; | |
1880 | 4694 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
7 | 4695 if (xp->xp_context == tab[i].context) |
4696 { | |
4697 if (tab[i].ic) | |
4698 regmatch.rm_ic = TRUE; | |
2849 | 4699 ret = ExpandGeneric(xp, ®match, num_file, file, |
3628 | 4700 tab[i].func, tab[i].escaped); |
7 | 4701 break; |
4702 } | |
4703 } | |
4704 | |
4705 vim_free(regmatch.regprog); | |
4706 | |
4707 return ret; | |
4708 #endif /* FEAT_CMDL_COMPL */ | |
4709 } | |
4710 | |
4711 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
4712 /* | |
4713 * Expand a list of names. | |
4714 * | |
4715 * Generic function for command line completion. It calls a function to | |
4716 * obtain strings, one by one. The strings are matched against a regexp | |
4717 * program. Matching strings are copied into an array, which is returned. | |
4718 * | |
4719 * Returns OK when no problems encountered, FAIL for error (out of memory). | |
4720 */ | |
4721 int | |
2849 | 4722 ExpandGeneric(xp, regmatch, num_file, file, func, escaped) |
7 | 4723 expand_T *xp; |
4724 regmatch_T *regmatch; | |
4725 int *num_file; | |
4726 char_u ***file; | |
4727 char_u *((*func)__ARGS((expand_T *, int))); | |
4728 /* returns a string from the list */ | |
2849 | 4729 int escaped; |
7 | 4730 { |
4731 int i; | |
4732 int count = 0; | |
480 | 4733 int round; |
7 | 4734 char_u *str; |
4735 | |
4736 /* do this loop twice: | |
480 | 4737 * round == 0: count the number of matching names |
4738 * round == 1: copy the matching names into allocated memory | |
7 | 4739 */ |
480 | 4740 for (round = 0; round <= 1; ++round) |
7 | 4741 { |
4742 for (i = 0; ; ++i) | |
4743 { | |
4744 str = (*func)(xp, i); | |
4745 if (str == NULL) /* end of list */ | |
4746 break; | |
4747 if (*str == NUL) /* skip empty strings */ | |
4748 continue; | |
4749 | |
4750 if (vim_regexec(regmatch, str, (colnr_T)0)) | |
4751 { | |
480 | 4752 if (round) |
7 | 4753 { |
2849 | 4754 if (escaped) |
4755 str = vim_strsave_escaped(str, (char_u *)" \t\\."); | |
4756 else | |
4757 str = vim_strsave(str); | |
7 | 4758 (*file)[count] = str; |
4759 #ifdef FEAT_MENU | |
4760 if (func == get_menu_names && str != NULL) | |
4761 { | |
4762 /* test for separator added by get_menu_names() */ | |
4763 str += STRLEN(str) - 1; | |
4764 if (*str == '\001') | |
4765 *str = '.'; | |
4766 } | |
4767 #endif | |
4768 } | |
4769 ++count; | |
4770 } | |
4771 } | |
480 | 4772 if (round == 0) |
7 | 4773 { |
4774 if (count == 0) | |
4775 return OK; | |
4776 *num_file = count; | |
4777 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); | |
4778 if (*file == NULL) | |
4779 { | |
4780 *file = (char_u **)""; | |
4781 return FAIL; | |
4782 } | |
4783 count = 0; | |
4784 } | |
4785 } | |
480 | 4786 |
828 | 4787 /* Sort the results. Keep menu's in the specified order. */ |
4788 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) | |
3164 | 4789 { |
4790 if (xp->xp_context == EXPAND_EXPRESSION | |
4791 || xp->xp_context == EXPAND_FUNCTIONS | |
4792 || xp->xp_context == EXPAND_USER_FUNC) | |
4793 /* <SNR> functions should be sorted to the end. */ | |
4794 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), | |
4795 sort_func_compare); | |
4796 else | |
4797 sort_strings(*file, *num_file); | |
4798 } | |
480 | 4799 |
1322 | 4800 #ifdef FEAT_CMDL_COMPL |
4801 /* Reset the variables used for special highlight names expansion, so that | |
4802 * they don't show up when getting normal highlight names by ID. */ | |
4803 reset_expand_highlight(); | |
4804 #endif | |
4805 | |
7 | 4806 return OK; |
4807 } | |
4808 | |
716 | 4809 /* |
4810 * Complete a shell command. | |
4811 * Returns FAIL or OK; | |
4812 */ | |
4813 static int | |
4814 expand_shellcmd(filepat, num_file, file, flagsarg) | |
4815 char_u *filepat; /* pattern to match with command names */ | |
4816 int *num_file; /* return: number of matches */ | |
4817 char_u ***file; /* return: array with matches */ | |
4818 int flagsarg; /* EW_ flags */ | |
4819 { | |
4820 char_u *pat; | |
4821 int i; | |
4822 char_u *path; | |
4823 int mustfree = FALSE; | |
4824 garray_T ga; | |
4825 char_u *buf = alloc(MAXPATHL); | |
4826 size_t l; | |
4827 char_u *s, *e; | |
4828 int flags = flagsarg; | |
4829 int ret; | |
4830 | |
4831 if (buf == NULL) | |
4832 return FAIL; | |
4833 | |
4834 /* for ":set path=" and ":set tags=" halve backslashes for escaped | |
4835 * space */ | |
4836 pat = vim_strsave(filepat); | |
4837 for (i = 0; pat[i]; ++i) | |
4838 if (pat[i] == '\\' && pat[i + 1] == ' ') | |
1621 | 4839 STRMOVE(pat + i, pat + i + 1); |
716 | 4840 |
4841 flags |= EW_FILE | EW_EXEC; | |
4842 | |
4843 /* For an absolute name we don't use $PATH. */ | |
955 | 4844 if (mch_isFullName(pat)) |
4845 path = (char_u *)" "; | |
4846 else if ((pat[0] == '.' && (vim_ispathsep(pat[1]) | |
716 | 4847 || (pat[1] == '.' && vim_ispathsep(pat[2]))))) |
4848 path = (char_u *)"."; | |
4849 else | |
2630 | 4850 { |
716 | 4851 path = vim_getenv((char_u *)"PATH", &mustfree); |
2630 | 4852 if (path == NULL) |
4853 path = (char_u *)""; | |
4854 } | |
716 | 4855 |
4856 /* | |
4857 * Go over all directories in $PATH. Expand matches in that directory and | |
4858 * collect them in "ga". | |
4859 */ | |
4860 ga_init2(&ga, (int)sizeof(char *), 10); | |
4861 for (s = path; *s != NUL; s = e) | |
4862 { | |
955 | 4863 if (*s == ' ') |
4864 ++s; /* Skip space used for absolute path name. */ | |
4865 | |
716 | 4866 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
4867 e = vim_strchr(s, ';'); | |
4868 #else | |
4869 e = vim_strchr(s, ':'); | |
4870 #endif | |
4871 if (e == NULL) | |
4872 e = s + STRLEN(s); | |
4873 | |
4874 l = e - s; | |
4875 if (l > MAXPATHL - 5) | |
4876 break; | |
4877 vim_strncpy(buf, s, l); | |
4878 add_pathsep(buf); | |
4879 l = STRLEN(buf); | |
4880 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); | |
4881 | |
4882 /* Expand matches in one directory of $PATH. */ | |
4883 ret = expand_wildcards(1, &buf, num_file, file, flags); | |
4884 if (ret == OK) | |
4885 { | |
4886 if (ga_grow(&ga, *num_file) == FAIL) | |
4887 FreeWild(*num_file, *file); | |
4888 else | |
4889 { | |
4890 for (i = 0; i < *num_file; ++i) | |
4891 { | |
4892 s = (*file)[i]; | |
4893 if (STRLEN(s) > l) | |
4894 { | |
4895 /* Remove the path again. */ | |
1621 | 4896 STRMOVE(s, s + l); |
716 | 4897 ((char_u **)ga.ga_data)[ga.ga_len++] = s; |
4898 } | |
4899 else | |
4900 vim_free(s); | |
4901 } | |
4902 vim_free(*file); | |
4903 } | |
4904 } | |
4905 if (*e != NUL) | |
4906 ++e; | |
4907 } | |
4908 *file = ga.ga_data; | |
4909 *num_file = ga.ga_len; | |
4910 | |
4911 vim_free(buf); | |
4912 vim_free(pat); | |
4913 if (mustfree) | |
4914 vim_free(path); | |
4915 return OK; | |
4916 } | |
4917 | |
4918 | |
7 | 4919 # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
410 | 4920 static 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)); |
4921 | |
7 | 4922 /* |
1518 | 4923 * Call "user_expand_func()" to invoke a user defined VimL function and return |
407 | 4924 * the result (either a string or a List). |
7 | 4925 */ |
407 | 4926 static void * |
4927 call_user_expand_func(user_expand_func, xp, num_file, file) | |
4928 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int)); | |
7 | 4929 expand_T *xp; |
4930 int *num_file; | |
4931 char_u ***file; | |
4932 { | |
407 | 4933 char_u keep; |
4934 char_u num[50]; | |
7 | 4935 char_u *args[3]; |
4936 int save_current_SID = current_SID; | |
407 | 4937 void *ret; |
13 | 4938 struct cmdline_info save_ccline; |
7 | 4939 |
4940 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0') | |
407 | 4941 return NULL; |
7 | 4942 *num_file = 0; |
4943 *file = NULL; | |
4944 | |
1518 | 4945 if (ccline.cmdbuff == NULL) |
4946 { | |
4947 /* Completion from Insert mode, pass fake arguments. */ | |
4948 keep = 0; | |
1524 | 4949 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern)); |
1518 | 4950 args[1] = xp->xp_pattern; |
4951 } | |
4952 else | |
4953 { | |
4954 /* Completion on the command line, pass real arguments. */ | |
4955 keep = ccline.cmdbuff[ccline.cmdlen]; | |
4956 ccline.cmdbuff[ccline.cmdlen] = 0; | |
4957 sprintf((char *)num, "%d", ccline.cmdpos); | |
4958 args[1] = ccline.cmdbuff; | |
4959 } | |
1965 | 4960 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
7 | 4961 args[2] = num; |
4962 | |
13 | 4963 /* Save the cmdline, we don't know what the function may do. */ |
4964 save_ccline = ccline; | |
4965 ccline.cmdbuff = NULL; | |
4966 ccline.cmdprompt = NULL; | |
7 | 4967 current_SID = xp->xp_scriptID; |
13 | 4968 |
407 | 4969 ret = user_expand_func(xp->xp_arg, 3, args, FALSE); |
13 | 4970 |
4971 ccline = save_ccline; | |
7 | 4972 current_SID = save_current_SID; |
1518 | 4973 if (ccline.cmdbuff != NULL) |
4974 ccline.cmdbuff[ccline.cmdlen] = keep; | |
407 | 4975 |
1965 | 4976 vim_free(args[0]); |
407 | 4977 return ret; |
4978 } | |
4979 | |
4980 /* | |
4981 * Expand names with a function defined by the user. | |
4982 */ | |
4983 static int | |
4984 ExpandUserDefined(xp, regmatch, num_file, file) | |
4985 expand_T *xp; | |
4986 regmatch_T *regmatch; | |
4987 int *num_file; | |
4988 char_u ***file; | |
4989 { | |
4990 char_u *retstr; | |
4991 char_u *s; | |
4992 char_u *e; | |
4993 char_u keep; | |
4994 garray_T ga; | |
4995 | |
4996 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); | |
4997 if (retstr == NULL) | |
7 | 4998 return FAIL; |
4999 | |
5000 ga_init2(&ga, (int)sizeof(char *), 3); | |
407 | 5001 for (s = retstr; *s != NUL; s = e) |
7 | 5002 { |
5003 e = vim_strchr(s, '\n'); | |
5004 if (e == NULL) | |
5005 e = s + STRLEN(s); | |
5006 keep = *e; | |
5007 *e = 0; | |
5008 | |
5009 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0) | |
5010 { | |
5011 *e = keep; | |
5012 if (*e != NUL) | |
5013 ++e; | |
5014 continue; | |
5015 } | |
5016 | |
5017 if (ga_grow(&ga, 1) == FAIL) | |
5018 break; | |
5019 | |
5020 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); | |
5021 ++ga.ga_len; | |
5022 | |
5023 *e = keep; | |
5024 if (*e != NUL) | |
5025 ++e; | |
5026 } | |
407 | 5027 vim_free(retstr); |
5028 *file = ga.ga_data; | |
5029 *num_file = ga.ga_len; | |
5030 return OK; | |
5031 } | |
5032 | |
5033 /* | |
5034 * Expand names with a list returned by a function defined by the user. | |
5035 */ | |
5036 static int | |
5037 ExpandUserList(xp, num_file, file) | |
5038 expand_T *xp; | |
5039 int *num_file; | |
5040 char_u ***file; | |
5041 { | |
5042 list_T *retlist; | |
5043 listitem_T *li; | |
5044 garray_T ga; | |
5045 | |
5046 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); | |
5047 if (retlist == NULL) | |
5048 return FAIL; | |
5049 | |
5050 ga_init2(&ga, (int)sizeof(char *), 3); | |
5051 /* Loop over the items in the list. */ | |
5052 for (li = retlist->lv_first; li != NULL; li = li->li_next) | |
5053 { | |
1917 | 5054 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
5055 continue; /* Skip non-string items and empty strings */ | |
407 | 5056 |
5057 if (ga_grow(&ga, 1) == FAIL) | |
5058 break; | |
5059 | |
5060 ((char_u **)ga.ga_data)[ga.ga_len] = | |
1917 | 5061 vim_strsave(li->li_tv.vval.v_string); |
407 | 5062 ++ga.ga_len; |
5063 } | |
5064 list_unref(retlist); | |
5065 | |
7 | 5066 *file = ga.ga_data; |
5067 *num_file = ga.ga_len; | |
5068 return OK; | |
5069 } | |
5070 #endif | |
5071 | |
5072 /* | |
2268
aafed4a4866f
Command line completion for :ownsyntax. (Dominique Pelle)
Bram Moolenaar <bram@vim.org>
parents:
2243
diff
changeset
|
5073 * Expand color scheme, compiler or filetype names: |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
5074 * 'runtimepath'/{dirnames}/{pat}.vim |
2929 | 5075 * "dirnames" is an array with one or more directory names. |
7 | 5076 */ |
5077 static int | |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
5078 ExpandRTDir(pat, num_file, file, dirnames) |
7 | 5079 char_u *pat; |
5080 int *num_file; | |
5081 char_u ***file; | |
2929 | 5082 char *dirnames[]; |
7 | 5083 { |
2929 | 5084 char_u *matches; |
7 | 5085 char_u *s; |
5086 char_u *e; | |
5087 garray_T ga; | |
2929 | 5088 int i; |
5089 int pat_len; | |
7 | 5090 |
5091 *num_file = 0; | |
5092 *file = NULL; | |
2931 | 5093 pat_len = (int)STRLEN(pat); |
2929 | 5094 ga_init2(&ga, (int)sizeof(char *), 10); |
5095 | |
5096 for (i = 0; dirnames[i] != NULL; ++i) | |
7 | 5097 { |
2929 | 5098 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
5099 if (s == NULL) | |
5100 { | |
5101 ga_clear_strings(&ga); | |
5102 return FAIL; | |
5103 } | |
5104 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); | |
5105 matches = globpath(p_rtp, s, 0); | |
5106 vim_free(s); | |
5107 if (matches == NULL) | |
5108 continue; | |
5109 | |
5110 for (s = matches; *s != NUL; s = e) | |
7 | 5111 { |
2929 | 5112 e = vim_strchr(s, '\n'); |
5113 if (e == NULL) | |
5114 e = s + STRLEN(s); | |
5115 if (ga_grow(&ga, 1) == FAIL) | |
5116 break; | |
5117 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) | |
5118 { | |
5119 for (s = e - 4; s > matches; mb_ptr_back(matches, s)) | |
5120 if (*s == '\n' || vim_ispathsep(*s)) | |
5121 break; | |
5122 ++s; | |
5123 ((char_u **)ga.ga_data)[ga.ga_len] = | |
7 | 5124 vim_strnsave(s, (int)(e - s - 4)); |
2929 | 5125 ++ga.ga_len; |
5126 } | |
5127 if (*e != NUL) | |
5128 ++e; | |
7 | 5129 } |
2929 | 5130 vim_free(matches); |
7 | 5131 } |
2929 | 5132 if (ga.ga_len == 0) |
3628 | 5133 return FAIL; |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
5134 |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
5135 /* Sort and remove duplicates which can happen when specifying multiple |
2929 | 5136 * directories in dirnames. */ |
2433
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
5137 remove_duplicates(&ga); |
98b9a6b9e7d5
Add completion for ":ownsyntax" and improve completion for ":filetype".
Bram Moolenaar <bram@vim.org>
parents:
2429
diff
changeset
|
5138 |
7 | 5139 *file = ga.ga_data; |
5140 *num_file = ga.ga_len; | |
5141 return OK; | |
5142 } | |
5143 | |
5144 #endif | |
5145 | |
5146 #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) | |
5147 /* | |
5148 * Expand "file" for all comma-separated directories in "path". | |
5149 * Returns an allocated string with all matches concatenated, separated by | |
5150 * newlines. Returns NULL for an error or no matches. | |
5151 */ | |
5152 char_u * | |
1754 | 5153 globpath(path, file, expand_options) |
7 | 5154 char_u *path; |
5155 char_u *file; | |
1754 | 5156 int expand_options; |
7 | 5157 { |
5158 expand_T xpc; | |
5159 char_u *buf; | |
5160 garray_T ga; | |
5161 int i; | |
5162 int len; | |
5163 int num_p; | |
5164 char_u **p; | |
5165 char_u *cur = NULL; | |
5166 | |
5167 buf = alloc(MAXPATHL); | |
5168 if (buf == NULL) | |
5169 return NULL; | |
5170 | |
632 | 5171 ExpandInit(&xpc); |
7 | 5172 xpc.xp_context = EXPAND_FILES; |
632 | 5173 |
7 | 5174 ga_init2(&ga, 1, 100); |
5175 | |
5176 /* Loop over all entries in {path}. */ | |
5177 while (*path != NUL) | |
5178 { | |
5179 /* Copy one item of the path to buf[] and concatenate the file name. */ | |
5180 copy_option_part(&path, buf, MAXPATHL, ","); | |
5181 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) | |
5182 { | |
2534
e27def2ed69b
Make :find completion work better with the DJGPP build. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2520
diff
changeset
|
5183 # if defined(MSWIN) || defined(MSDOS) |
2495
a847363bf06e
Fix a few problems for :find completion. Test much more. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
5184 /* Using the platform's path separator (\) makes vim incorrectly |
a847363bf06e
Fix a few problems for :find completion. Test much more. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
5185 * treat it as an escape character, use '/' instead. */ |
a847363bf06e
Fix a few problems for :find completion. Test much more. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
5186 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
a847363bf06e
Fix a few problems for :find completion. Test much more. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
5187 STRCAT(buf, "/"); |
a847363bf06e
Fix a few problems for :find completion. Test much more. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
5188 # else |
7 | 5189 add_pathsep(buf); |
2495
a847363bf06e
Fix a few problems for :find completion. Test much more. (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2445
diff
changeset
|
5190 # endif |
7 | 5191 STRCAT(buf, file); |
1754 | 5192 if (ExpandFromContext(&xpc, buf, &num_p, &p, |
5193 WILD_SILENT|expand_options) != FAIL && num_p > 0) | |
7 | 5194 { |
1754 | 5195 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
7 | 5196 for (len = 0, i = 0; i < num_p; ++i) |
835 | 5197 len += (int)STRLEN(p[i]) + 1; |
7 | 5198 |
5199 /* Concatenate new results to previous ones. */ | |
5200 if (ga_grow(&ga, len) == OK) | |
5201 { | |
5202 cur = (char_u *)ga.ga_data + ga.ga_len; | |
5203 for (i = 0; i < num_p; ++i) | |
5204 { | |
5205 STRCPY(cur, p[i]); | |
5206 cur += STRLEN(p[i]); | |
5207 *cur++ = '\n'; | |
5208 } | |
5209 ga.ga_len += len; | |
5210 } | |
5211 FreeWild(num_p, p); | |
5212 } | |
5213 } | |
5214 } | |
5215 if (cur != NULL) | |
5216 *--cur = 0; /* Replace trailing newline with NUL */ | |
5217 | |
5218 vim_free(buf); | |
5219 return (char_u *)ga.ga_data; | |
5220 } | |
5221 | |
5222 #endif | |
5223 | |
5224 #if defined(FEAT_CMDHIST) || defined(PROTO) | |
5225 | |
5226 /********************************* | |
5227 * Command line history stuff * | |
5228 *********************************/ | |
5229 | |
5230 /* | |
5231 * Translate a history character to the associated type number. | |
5232 */ | |
5233 static int | |
5234 hist_char2type(c) | |
5235 int c; | |
5236 { | |
5237 if (c == ':') | |
5238 return HIST_CMD; | |
5239 if (c == '=') | |
5240 return HIST_EXPR; | |
5241 if (c == '@') | |
5242 return HIST_INPUT; | |
5243 if (c == '>') | |
5244 return HIST_DEBUG; | |
5245 return HIST_SEARCH; /* must be '?' or '/' */ | |
5246 } | |
5247 | |
5248 /* | |
5249 * Table of history names. | |
5250 * These names are used in :history and various hist...() functions. | |
5251 * It is sufficient to give the significant prefix of a history name. | |
5252 */ | |
5253 | |
5254 static char *(history_names[]) = | |
5255 { | |
5256 "cmd", | |
5257 "search", | |
5258 "expr", | |
5259 "input", | |
5260 "debug", | |
5261 NULL | |
5262 }; | |
5263 | |
3503 | 5264 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
5265 /* | |
5266 * Function given to ExpandGeneric() to obtain the possible first | |
5267 * arguments of the ":history command. | |
5268 */ | |
5269 static char_u * | |
5270 get_history_arg(xp, idx) | |
5271 expand_T *xp UNUSED; | |
5272 int idx; | |
5273 { | |
5274 static char_u compl[2] = { NUL, NUL }; | |
5275 char *short_names = ":=@>?/"; | |
3529 | 5276 int short_names_count = (int)STRLEN(short_names); |
3503 | 5277 int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
5278 | |
5279 if (idx < short_names_count) | |
5280 { | |
5281 compl[0] = (char_u)short_names[idx]; | |
5282 return compl; | |
5283 } | |
5284 if (idx < short_names_count + history_name_count) | |
5285 return (char_u *)history_names[idx - short_names_count]; | |
5286 if (idx == short_names_count + history_name_count) | |
5287 return (char_u *)"all"; | |
5288 return NULL; | |
5289 } | |
5290 #endif | |
5291 | |
7 | 5292 /* |
5293 * init_history() - Initialize the command line history. | |
5294 * Also used to re-allocate the history when the size changes. | |
5295 */ | |
356 | 5296 void |
7 | 5297 init_history() |
5298 { | |
5299 int newlen; /* new length of history table */ | |
5300 histentry_T *temp; | |
5301 int i; | |
5302 int j; | |
5303 int type; | |
5304 | |
5305 /* | |
5306 * If size of history table changed, reallocate it | |
5307 */ | |
5308 newlen = (int)p_hi; | |
5309 if (newlen != hislen) /* history length changed */ | |
5310 { | |
5311 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ | |
5312 { | |
5313 if (newlen) | |
5314 { | |
5315 temp = (histentry_T *)lalloc( | |
5316 (long_u)(newlen * sizeof(histentry_T)), TRUE); | |
5317 if (temp == NULL) /* out of memory! */ | |
5318 { | |
5319 if (type == 0) /* first one: just keep the old length */ | |
5320 { | |
5321 newlen = hislen; | |
5322 break; | |
5323 } | |
5324 /* Already changed one table, now we can only have zero | |
5325 * length for all tables. */ | |
5326 newlen = 0; | |
5327 type = -1; | |
5328 continue; | |
5329 } | |
5330 } | |
5331 else | |
5332 temp = NULL; | |
5333 if (newlen == 0 || temp != NULL) | |
5334 { | |
5335 if (hisidx[type] < 0) /* there are no entries yet */ | |
5336 { | |
5337 for (i = 0; i < newlen; ++i) | |
5338 { | |
5339 temp[i].hisnum = 0; | |
5340 temp[i].hisstr = NULL; | |
5341 } | |
5342 } | |
5343 else if (newlen > hislen) /* array becomes bigger */ | |
5344 { | |
5345 for (i = 0; i <= hisidx[type]; ++i) | |
5346 temp[i] = history[type][i]; | |
5347 j = i; | |
5348 for ( ; i <= newlen - (hislen - hisidx[type]); ++i) | |
5349 { | |
5350 temp[i].hisnum = 0; | |
5351 temp[i].hisstr = NULL; | |
5352 } | |
5353 for ( ; j < hislen; ++i, ++j) | |
5354 temp[i] = history[type][j]; | |
5355 } | |
5356 else /* array becomes smaller or 0 */ | |
5357 { | |
5358 j = hisidx[type]; | |
5359 for (i = newlen - 1; ; --i) | |
5360 { | |
5361 if (i >= 0) /* copy newest entries */ | |
5362 temp[i] = history[type][j]; | |
5363 else /* remove older entries */ | |
5364 vim_free(history[type][j].hisstr); | |
5365 if (--j < 0) | |
5366 j = hislen - 1; | |
5367 if (j == hisidx[type]) | |
5368 break; | |
5369 } | |
5370 hisidx[type] = newlen - 1; | |
5371 } | |
5372 vim_free(history[type]); | |
5373 history[type] = temp; | |
5374 } | |
5375 } | |
5376 hislen = newlen; | |
5377 } | |
5378 } | |
5379 | |
5380 /* | |
5381 * Check if command line 'str' is already in history. | |
5382 * If 'move_to_front' is TRUE, matching entry is moved to end of history. | |
5383 */ | |
5384 static int | |
2986 | 5385 in_history(type, str, move_to_front, sep) |
7 | 5386 int type; |
5387 char_u *str; | |
5388 int move_to_front; /* Move the entry to the front if it exists */ | |
2986 | 5389 int sep; |
7 | 5390 { |
5391 int i; | |
5392 int last_i = -1; | |
2986 | 5393 char_u *p; |
7 | 5394 |
5395 if (hisidx[type] < 0) | |
5396 return FALSE; | |
5397 i = hisidx[type]; | |
5398 do | |
5399 { | |
5400 if (history[type][i].hisstr == NULL) | |
5401 return FALSE; | |
2986 | 5402 |
5403 /* For search history, check that the separator character matches as | |
5404 * well. */ | |
5405 p = history[type][i].hisstr; | |
5406 if (STRCMP(str, p) == 0 | |
5407 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) | |
7 | 5408 { |
5409 if (!move_to_front) | |
5410 return TRUE; | |
5411 last_i = i; | |
5412 break; | |
5413 } | |
5414 if (--i < 0) | |
5415 i = hislen - 1; | |
5416 } while (i != hisidx[type]); | |
5417 | |
5418 if (last_i >= 0) | |
5419 { | |
5420 str = history[type][i].hisstr; | |
5421 while (i != hisidx[type]) | |
5422 { | |
5423 if (++i >= hislen) | |
5424 i = 0; | |
5425 history[type][last_i] = history[type][i]; | |
5426 last_i = i; | |
5427 } | |
5428 history[type][i].hisstr = str; | |
5429 history[type][i].hisnum = ++hisnum[type]; | |
5430 return TRUE; | |
5431 } | |
5432 return FALSE; | |
5433 } | |
5434 | |
5435 /* | |
5436 * Convert history name (from table above) to its HIST_ equivalent. | |
5437 * When "name" is empty, return "cmd" history. | |
5438 * Returns -1 for unknown history name. | |
5439 */ | |
5440 int | |
5441 get_histtype(name) | |
5442 char_u *name; | |
5443 { | |
5444 int i; | |
5445 int len = (int)STRLEN(name); | |
5446 | |
5447 /* No argument: use current history. */ | |
5448 if (len == 0) | |
5449 return hist_char2type(ccline.cmdfirstc); | |
5450 | |
5451 for (i = 0; history_names[i] != NULL; ++i) | |
5452 if (STRNICMP(name, history_names[i], len) == 0) | |
5453 return i; | |
5454 | |
5455 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) | |
5456 return hist_char2type(name[0]); | |
5457 | |
5458 return -1; | |
5459 } | |
5460 | |
5461 static int last_maptick = -1; /* last seen maptick */ | |
5462 | |
5463 /* | |
5464 * Add the given string to the given history. If the string is already in the | |
5465 * history then it is moved to the front. "histype" may be one of he HIST_ | |
5466 * values. | |
5467 */ | |
5468 void | |
5469 add_to_history(histype, new_entry, in_map, sep) | |
5470 int histype; | |
5471 char_u *new_entry; | |
5472 int in_map; /* consider maptick when inside a mapping */ | |
5473 int sep; /* separator character used (search hist) */ | |
5474 { | |
5475 histentry_T *hisptr; | |
5476 int len; | |
5477 | |
5478 if (hislen == 0) /* no history */ | |
5479 return; | |
5480 | |
5481 /* | |
5482 * Searches inside the same mapping overwrite each other, so that only | |
5483 * the last line is kept. Be careful not to remove a line that was moved | |
5484 * down, only lines that were added. | |
5485 */ | |
5486 if (histype == HIST_SEARCH && in_map) | |
5487 { | |
5488 if (maptick == last_maptick) | |
5489 { | |
5490 /* Current line is from the same mapping, remove it */ | |
5491 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; | |
5492 vim_free(hisptr->hisstr); | |
5493 hisptr->hisstr = NULL; | |
5494 hisptr->hisnum = 0; | |
5495 --hisnum[histype]; | |
5496 if (--hisidx[HIST_SEARCH] < 0) | |
5497 hisidx[HIST_SEARCH] = hislen - 1; | |
5498 } | |
5499 last_maptick = -1; | |
5500 } | |
2986 | 5501 if (!in_history(histype, new_entry, TRUE, sep)) |
7 | 5502 { |
5503 if (++hisidx[histype] == hislen) | |
5504 hisidx[histype] = 0; | |
5505 hisptr = &history[histype][hisidx[histype]]; | |
5506 vim_free(hisptr->hisstr); | |
5507 | |
5508 /* Store the separator after the NUL of the string. */ | |
835 | 5509 len = (int)STRLEN(new_entry); |
7 | 5510 hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
5511 if (hisptr->hisstr != NULL) | |
5512 hisptr->hisstr[len + 1] = sep; | |
5513 | |
5514 hisptr->hisnum = ++hisnum[histype]; | |
5515 if (histype == HIST_SEARCH && in_map) | |
5516 last_maptick = maptick; | |
5517 } | |
5518 } | |
5519 | |
5520 #if defined(FEAT_EVAL) || defined(PROTO) | |
5521 | |
5522 /* | |
5523 * Get identifier of newest history entry. | |
5524 * "histype" may be one of the HIST_ values. | |
5525 */ | |
5526 int | |
5527 get_history_idx(histype) | |
5528 int histype; | |
5529 { | |
5530 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT | |
5531 || hisidx[histype] < 0) | |
5532 return -1; | |
5533 | |
5534 return history[histype][hisidx[histype]].hisnum; | |
5535 } | |
5536 | |
95 | 5537 static struct cmdline_info *get_ccline_ptr __ARGS((void)); |
5538 | |
5539 /* | |
5540 * Get pointer to the command line info to use. cmdline_paste() may clear | |
5541 * ccline and put the previous value in prev_ccline. | |
5542 */ | |
5543 static struct cmdline_info * | |
5544 get_ccline_ptr() | |
5545 { | |
5546 if ((State & CMDLINE) == 0) | |
5547 return NULL; | |
5548 if (ccline.cmdbuff != NULL) | |
5549 return &ccline; | |
5550 if (prev_ccline_used && prev_ccline.cmdbuff != NULL) | |
5551 return &prev_ccline; | |
5552 return NULL; | |
5553 } | |
5554 | |
7 | 5555 /* |
5556 * Get the current command line in allocated memory. | |
5557 * Only works when the command line is being edited. | |
5558 * Returns NULL when something is wrong. | |
5559 */ | |
5560 char_u * | |
5561 get_cmdline_str() | |
5562 { | |
95 | 5563 struct cmdline_info *p = get_ccline_ptr(); |
5564 | |
5565 if (p == NULL) | |
7 | 5566 return NULL; |
95 | 5567 return vim_strnsave(p->cmdbuff, p->cmdlen); |
7 | 5568 } |
5569 | |
5570 /* | |
5571 * Get the current command line position, counted in bytes. | |
5572 * Zero is the first position. | |
5573 * Only works when the command line is being edited. | |
5574 * Returns -1 when something is wrong. | |
5575 */ | |
5576 int | |
5577 get_cmdline_pos() | |
5578 { | |
95 | 5579 struct cmdline_info *p = get_ccline_ptr(); |
5580 | |
5581 if (p == NULL) | |
7 | 5582 return -1; |
95 | 5583 return p->cmdpos; |
7 | 5584 } |
5585 | |
5586 /* | |
5587 * Set the command line byte position to "pos". Zero is the first position. | |
5588 * Only works when the command line is being edited. | |
5589 * Returns 1 when failed, 0 when OK. | |
5590 */ | |
5591 int | |
5592 set_cmdline_pos(pos) | |
5593 int pos; | |
5594 { | |
95 | 5595 struct cmdline_info *p = get_ccline_ptr(); |
5596 | |
5597 if (p == NULL) | |
7 | 5598 return 1; |
5599 | |
5600 /* The position is not set directly but after CTRL-\ e or CTRL-R = has | |
5601 * changed the command line. */ | |
5602 if (pos < 0) | |
5603 new_cmdpos = 0; | |
5604 else | |
5605 new_cmdpos = pos; | |
5606 return 0; | |
5607 } | |
5608 | |
5609 /* | |
531 | 5610 * Get the current command-line type. |
532 | 5611 * Returns ':' or '/' or '?' or '@' or '>' or '-' |
531 | 5612 * Only works when the command line is being edited. |
5613 * Returns NUL when something is wrong. | |
5614 */ | |
5615 int | |
5616 get_cmdline_type() | |
5617 { | |
5618 struct cmdline_info *p = get_ccline_ptr(); | |
5619 | |
5620 if (p == NULL) | |
5621 return NUL; | |
532 | 5622 if (p->cmdfirstc == NUL) |
5623 return (p->input_fn) ? '@' : '-'; | |
531 | 5624 return p->cmdfirstc; |
5625 } | |
5626 | |
5627 /* | |
7 | 5628 * Calculate history index from a number: |
5629 * num > 0: seen as identifying number of a history entry | |
5630 * num < 0: relative position in history wrt newest entry | |
5631 * "histype" may be one of the HIST_ values. | |
5632 */ | |
5633 static int | |
5634 calc_hist_idx(histype, num) | |
5635 int histype; | |
5636 int num; | |
5637 { | |
5638 int i; | |
5639 histentry_T *hist; | |
5640 int wrapped = FALSE; | |
5641 | |
5642 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT | |
5643 || (i = hisidx[histype]) < 0 || num == 0) | |
5644 return -1; | |
5645 | |
5646 hist = history[histype]; | |
5647 if (num > 0) | |
5648 { | |
5649 while (hist[i].hisnum > num) | |
5650 if (--i < 0) | |
5651 { | |
5652 if (wrapped) | |
5653 break; | |
5654 i += hislen; | |
5655 wrapped = TRUE; | |
5656 } | |
5657 if (hist[i].hisnum == num && hist[i].hisstr != NULL) | |
5658 return i; | |
5659 } | |
5660 else if (-num <= hislen) | |
5661 { | |
5662 i += num + 1; | |
5663 if (i < 0) | |
5664 i += hislen; | |
5665 if (hist[i].hisstr != NULL) | |
5666 return i; | |
5667 } | |
5668 return -1; | |
5669 } | |
5670 | |
5671 /* | |
5672 * Get a history entry by its index. | |
5673 * "histype" may be one of the HIST_ values. | |
5674 */ | |
5675 char_u * | |
5676 get_history_entry(histype, idx) | |
5677 int histype; | |
5678 int idx; | |
5679 { | |
5680 idx = calc_hist_idx(histype, idx); | |
5681 if (idx >= 0) | |
5682 return history[histype][idx].hisstr; | |
5683 else | |
5684 return (char_u *)""; | |
5685 } | |
5686 | |
5687 /* | |
5688 * Clear all entries of a history. | |
5689 * "histype" may be one of the HIST_ values. | |
5690 */ | |
5691 int | |
5692 clr_history(histype) | |
5693 int histype; | |
5694 { | |
5695 int i; | |
5696 histentry_T *hisptr; | |
5697 | |
5698 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) | |
5699 { | |
5700 hisptr = history[histype]; | |
5701 for (i = hislen; i--;) | |
5702 { | |
5703 vim_free(hisptr->hisstr); | |
5704 hisptr->hisnum = 0; | |
5705 hisptr++->hisstr = NULL; | |
5706 } | |
5707 hisidx[histype] = -1; /* mark history as cleared */ | |
5708 hisnum[histype] = 0; /* reset identifier counter */ | |
5709 return OK; | |
5710 } | |
5711 return FAIL; | |
5712 } | |
5713 | |
5714 /* | |
5715 * Remove all entries matching {str} from a history. | |
5716 * "histype" may be one of the HIST_ values. | |
5717 */ | |
5718 int | |
5719 del_history_entry(histype, str) | |
5720 int histype; | |
5721 char_u *str; | |
5722 { | |
5723 regmatch_T regmatch; | |
5724 histentry_T *hisptr; | |
5725 int idx; | |
5726 int i; | |
5727 int last; | |
5728 int found = FALSE; | |
5729 | |
5730 regmatch.regprog = NULL; | |
5731 regmatch.rm_ic = FALSE; /* always match case */ | |
5732 if (hislen != 0 | |
5733 && histype >= 0 | |
5734 && histype < HIST_COUNT | |
5735 && *str != NUL | |
5736 && (idx = hisidx[histype]) >= 0 | |
5737 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) | |
5738 != NULL) | |
5739 { | |
5740 i = last = idx; | |
5741 do | |
5742 { | |
5743 hisptr = &history[histype][i]; | |
5744 if (hisptr->hisstr == NULL) | |
5745 break; | |
5746 if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) | |
5747 { | |
5748 found = TRUE; | |
5749 vim_free(hisptr->hisstr); | |
5750 hisptr->hisstr = NULL; | |
5751 hisptr->hisnum = 0; | |
5752 } | |
5753 else | |
5754 { | |
5755 if (i != last) | |
5756 { | |
5757 history[histype][last] = *hisptr; | |
5758 hisptr->hisstr = NULL; | |
5759 hisptr->hisnum = 0; | |
5760 } | |
5761 if (--last < 0) | |
5762 last += hislen; | |
5763 } | |
5764 if (--i < 0) | |
5765 i += hislen; | |
5766 } while (i != idx); | |
5767 if (history[histype][idx].hisstr == NULL) | |
5768 hisidx[histype] = -1; | |
5769 } | |
5770 vim_free(regmatch.regprog); | |
5771 return found; | |
5772 } | |
5773 | |
5774 /* | |
5775 * Remove an indexed entry from a history. | |
5776 * "histype" may be one of the HIST_ values. | |
5777 */ | |
5778 int | |
5779 del_history_idx(histype, idx) | |
5780 int histype; | |
5781 int idx; | |
5782 { | |
5783 int i, j; | |
5784 | |
5785 i = calc_hist_idx(histype, idx); | |
5786 if (i < 0) | |
5787 return FALSE; | |
5788 idx = hisidx[histype]; | |
5789 vim_free(history[histype][i].hisstr); | |
5790 | |
5791 /* When deleting the last added search string in a mapping, reset | |
5792 * last_maptick, so that the last added search string isn't deleted again. | |
5793 */ | |
5794 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) | |
5795 last_maptick = -1; | |
5796 | |
5797 while (i != idx) | |
5798 { | |
5799 j = (i + 1) % hislen; | |
5800 history[histype][i] = history[histype][j]; | |
5801 i = j; | |
5802 } | |
5803 history[histype][i].hisstr = NULL; | |
5804 history[histype][i].hisnum = 0; | |
5805 if (--i < 0) | |
5806 i += hislen; | |
5807 hisidx[histype] = i; | |
5808 return TRUE; | |
5809 } | |
5810 | |
5811 #endif /* FEAT_EVAL */ | |
5812 | |
5813 #if defined(FEAT_CRYPT) || defined(PROTO) | |
5814 /* | |
5815 * Very specific function to remove the value in ":set key=val" from the | |
5816 * history. | |
5817 */ | |
5818 void | |
5819 remove_key_from_history() | |
5820 { | |
5821 char_u *p; | |
5822 int i; | |
5823 | |
5824 i = hisidx[HIST_CMD]; | |
5825 if (i < 0) | |
5826 return; | |
5827 p = history[HIST_CMD][i].hisstr; | |
5828 if (p != NULL) | |
5829 for ( ; *p; ++p) | |
5830 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) | |
5831 { | |
5832 p = vim_strchr(p + 3, '='); | |
5833 if (p == NULL) | |
5834 break; | |
5835 ++p; | |
5836 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i) | |
5837 if (p[i] == '\\' && p[i + 1]) | |
5838 ++i; | |
1621 | 5839 STRMOVE(p, p + i); |
7 | 5840 --p; |
5841 } | |
5842 } | |
5843 #endif | |
5844 | |
5845 #endif /* FEAT_CMDHIST */ | |
5846 | |
5847 #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) | |
5848 /* | |
5849 * Get indices "num1,num2" that specify a range within a list (not a range of | |
5850 * text lines in a buffer!) from a string. Used for ":history" and ":clist". | |
5851 * Returns OK if parsed successfully, otherwise FAIL. | |
5852 */ | |
5853 int | |
5854 get_list_range(str, num1, num2) | |
5855 char_u **str; | |
5856 int *num1; | |
5857 int *num2; | |
5858 { | |
5859 int len; | |
5860 int first = FALSE; | |
5861 long num; | |
5862 | |
5863 *str = skipwhite(*str); | |
5864 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ | |
5865 { | |
5866 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL); | |
5867 *str += len; | |
5868 *num1 = (int)num; | |
5869 first = TRUE; | |
5870 } | |
5871 *str = skipwhite(*str); | |
5872 if (**str == ',') /* parse "to" part of range */ | |
5873 { | |
5874 *str = skipwhite(*str + 1); | |
5875 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL); | |
5876 if (len > 0) | |
5877 { | |
5878 *num2 = (int)num; | |
5879 *str = skipwhite(*str + len); | |
5880 } | |
5881 else if (!first) /* no number given at all */ | |
5882 return FAIL; | |
5883 } | |
5884 else if (first) /* only one number given */ | |
5885 *num2 = *num1; | |
5886 return OK; | |
5887 } | |
5888 #endif | |
5889 | |
5890 #if defined(FEAT_CMDHIST) || defined(PROTO) | |
5891 /* | |
5892 * :history command - print a history | |
5893 */ | |
5894 void | |
5895 ex_history(eap) | |
5896 exarg_T *eap; | |
5897 { | |
5898 histentry_T *hist; | |
5899 int histype1 = HIST_CMD; | |
5900 int histype2 = HIST_CMD; | |
5901 int hisidx1 = 1; | |
5902 int hisidx2 = -1; | |
5903 int idx; | |
5904 int i, j, k; | |
5905 char_u *end; | |
5906 char_u *arg = eap->arg; | |
5907 | |
5908 if (hislen == 0) | |
5909 { | |
5910 MSG(_("'history' option is zero")); | |
5911 return; | |
5912 } | |
5913 | |
5914 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) | |
5915 { | |
5916 end = arg; | |
5917 while (ASCII_ISALPHA(*end) | |
5918 || vim_strchr((char_u *)":=@>/?", *end) != NULL) | |
5919 end++; | |
5920 i = *end; | |
5921 *end = NUL; | |
5922 histype1 = get_histtype(arg); | |
5923 if (histype1 == -1) | |
5924 { | |
1853 | 5925 if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
7 | 5926 { |
5927 histype1 = 0; | |
5928 histype2 = HIST_COUNT-1; | |
5929 } | |
5930 else | |
5931 { | |
5932 *end = i; | |
5933 EMSG(_(e_trailing)); | |
5934 return; | |
5935 } | |
5936 } | |
5937 else | |
5938 histype2 = histype1; | |
5939 *end = i; | |
5940 } | |
5941 else | |
5942 end = arg; | |
5943 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) | |
5944 { | |
5945 EMSG(_(e_trailing)); | |
5946 return; | |
5947 } | |
5948 | |
5949 for (; !got_int && histype1 <= histype2; ++histype1) | |
5950 { | |
5951 STRCPY(IObuff, "\n # "); | |
5952 STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); | |
5953 MSG_PUTS_TITLE(IObuff); | |
5954 idx = hisidx[histype1]; | |
5955 hist = history[histype1]; | |
5956 j = hisidx1; | |
5957 k = hisidx2; | |
5958 if (j < 0) | |
5959 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; | |
5960 if (k < 0) | |
5961 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; | |
5962 if (idx >= 0 && j <= k) | |
5963 for (i = idx + 1; !got_int; ++i) | |
5964 { | |
5965 if (i == hislen) | |
5966 i = 0; | |
5967 if (hist[i].hisstr != NULL | |
5968 && hist[i].hisnum >= j && hist[i].hisnum <= k) | |
5969 { | |
5970 msg_putchar('\n'); | |
5971 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', | |
5972 hist[i].hisnum); | |
5973 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) | |
5974 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), | |
3290 | 5975 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
7 | 5976 else |
5977 STRCAT(IObuff, hist[i].hisstr); | |
5978 msg_outtrans(IObuff); | |
5979 out_flush(); | |
5980 } | |
5981 if (i == idx) | |
5982 break; | |
5983 } | |
5984 } | |
5985 } | |
5986 #endif | |
5987 | |
5988 #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) | |
5989 static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL}; | |
5990 static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0}; | |
5991 static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0}; | |
5992 static int viminfo_add_at_front = FALSE; | |
5993 | |
5994 static int hist_type2char __ARGS((int type, int use_question)); | |
5995 | |
5996 /* | |
5997 * Translate a history type number to the associated character. | |
5998 */ | |
5999 static int | |
6000 hist_type2char(type, use_question) | |
6001 int type; | |
6002 int use_question; /* use '?' instead of '/' */ | |
6003 { | |
6004 if (type == HIST_CMD) | |
6005 return ':'; | |
6006 if (type == HIST_SEARCH) | |
6007 { | |
6008 if (use_question) | |
6009 return '?'; | |
6010 else | |
6011 return '/'; | |
6012 } | |
6013 if (type == HIST_EXPR) | |
6014 return '='; | |
6015 return '@'; | |
6016 } | |
6017 | |
6018 /* | |
6019 * Prepare for reading the history from the viminfo file. | |
6020 * This allocates history arrays to store the read history lines. | |
6021 */ | |
6022 void | |
6023 prepare_viminfo_history(asklen) | |
6024 int asklen; | |
6025 { | |
6026 int i; | |
6027 int num; | |
6028 int type; | |
6029 int len; | |
6030 | |
6031 init_history(); | |
6032 viminfo_add_at_front = (asklen != 0); | |
6033 if (asklen > hislen) | |
6034 asklen = hislen; | |
6035 | |
6036 for (type = 0; type < HIST_COUNT; ++type) | |
6037 { | |
6038 /* | |
6039 * Count the number of empty spaces in the history list. If there are | |
6040 * more spaces available than we request, then fill them up. | |
6041 */ | |
6042 for (i = 0, num = 0; i < hislen; i++) | |
6043 if (history[type][i].hisstr == NULL) | |
6044 num++; | |
6045 len = asklen; | |
6046 if (num > len) | |
6047 len = num; | |
6048 if (len <= 0) | |
6049 viminfo_history[type] = NULL; | |
6050 else | |
6051 viminfo_history[type] = | |
6052 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE); | |
6053 if (viminfo_history[type] == NULL) | |
6054 len = 0; | |
6055 viminfo_hislen[type] = len; | |
6056 viminfo_hisidx[type] = 0; | |
6057 } | |
6058 } | |
6059 | |
6060 /* | |
6061 * Accept a line from the viminfo, store it in the history array when it's | |
6062 * new. | |
6063 */ | |
6064 int | |
6065 read_viminfo_history(virp) | |
6066 vir_T *virp; | |
6067 { | |
6068 int type; | |
6069 long_u len; | |
6070 char_u *val; | |
6071 char_u *p; | |
6072 | |
6073 type = hist_char2type(virp->vir_line[0]); | |
6074 if (viminfo_hisidx[type] < viminfo_hislen[type]) | |
6075 { | |
6076 val = viminfo_readstring(virp, 1, TRUE); | |
6077 if (val != NULL && *val != NUL) | |
6078 { | |
3316 | 6079 int sep = (*val == ' ' ? NUL : *val); |
6080 | |
7 | 6081 if (!in_history(type, val + (type == HIST_SEARCH), |
3316 | 6082 viminfo_add_at_front, sep)) |
7 | 6083 { |
6084 /* Need to re-allocate to append the separator byte. */ | |
6085 len = STRLEN(val); | |
6086 p = lalloc(len + 2, TRUE); | |
6087 if (p != NULL) | |
6088 { | |
6089 if (type == HIST_SEARCH) | |
6090 { | |
6091 /* Search entry: Move the separator from the first | |
6092 * column to after the NUL. */ | |
6093 mch_memmove(p, val + 1, (size_t)len); | |
3316 | 6094 p[len] = sep; |
7 | 6095 } |
6096 else | |
6097 { | |
6098 /* Not a search entry: No separator in the viminfo | |
6099 * file, add a NUL separator. */ | |
6100 mch_memmove(p, val, (size_t)len + 1); | |
6101 p[len + 1] = NUL; | |
6102 } | |
6103 viminfo_history[type][viminfo_hisidx[type]++] = p; | |
6104 } | |
6105 } | |
6106 } | |
6107 vim_free(val); | |
6108 } | |
6109 return viminfo_readline(virp); | |
6110 } | |
6111 | |
6112 void | |
6113 finish_viminfo_history() | |
6114 { | |
6115 int idx; | |
6116 int i; | |
6117 int type; | |
6118 | |
6119 for (type = 0; type < HIST_COUNT; ++type) | |
6120 { | |
6121 if (history[type] == NULL) | |
6122 return; | |
6123 idx = hisidx[type] + viminfo_hisidx[type]; | |
6124 if (idx >= hislen) | |
6125 idx -= hislen; | |
6126 else if (idx < 0) | |
6127 idx = hislen - 1; | |
6128 if (viminfo_add_at_front) | |
6129 hisidx[type] = idx; | |
6130 else | |
6131 { | |
6132 if (hisidx[type] == -1) | |
6133 hisidx[type] = hislen - 1; | |
6134 do | |
6135 { | |
6136 if (history[type][idx].hisstr != NULL) | |
6137 break; | |
6138 if (++idx == hislen) | |
6139 idx = 0; | |
6140 } while (idx != hisidx[type]); | |
6141 if (idx != hisidx[type] && --idx < 0) | |
6142 idx = hislen - 1; | |
6143 } | |
6144 for (i = 0; i < viminfo_hisidx[type]; i++) | |
6145 { | |
6146 vim_free(history[type][idx].hisstr); | |
6147 history[type][idx].hisstr = viminfo_history[type][i]; | |
6148 if (--idx < 0) | |
6149 idx = hislen - 1; | |
6150 } | |
6151 idx += 1; | |
6152 idx %= hislen; | |
6153 for (i = 0; i < viminfo_hisidx[type]; i++) | |
6154 { | |
6155 history[type][idx++].hisnum = ++hisnum[type]; | |
6156 idx %= hislen; | |
6157 } | |
6158 vim_free(viminfo_history[type]); | |
6159 viminfo_history[type] = NULL; | |
6160 } | |
6161 } | |
6162 | |
6163 void | |
6164 write_viminfo_history(fp) | |
6165 FILE *fp; | |
6166 { | |
6167 int i; | |
6168 int type; | |
6169 int num_saved; | |
6170 char_u *p; | |
6171 int c; | |
6172 | |
6173 init_history(); | |
6174 if (hislen == 0) | |
6175 return; | |
6176 for (type = 0; type < HIST_COUNT; ++type) | |
6177 { | |
6178 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); | |
6179 if (num_saved == 0) | |
6180 continue; | |
6181 if (num_saved < 0) /* Use default */ | |
6182 num_saved = hislen; | |
6183 fprintf(fp, _("\n# %s History (newest to oldest):\n"), | |
6184 type == HIST_CMD ? _("Command Line") : | |
6185 type == HIST_SEARCH ? _("Search String") : | |
6186 type == HIST_EXPR ? _("Expression") : | |
6187 _("Input Line")); | |
6188 if (num_saved > hislen) | |
6189 num_saved = hislen; | |
6190 i = hisidx[type]; | |
6191 if (i >= 0) | |
6192 while (num_saved--) | |
6193 { | |
6194 p = history[type][i].hisstr; | |
6195 if (p != NULL) | |
6196 { | |
301 | 6197 fputc(hist_type2char(type, TRUE), fp); |
7 | 6198 /* For the search history: put the separator in the second |
6199 * column; use a space if there isn't one. */ | |
6200 if (type == HIST_SEARCH) | |
6201 { | |
6202 c = p[STRLEN(p) + 1]; | |
6203 putc(c == NUL ? ' ' : c, fp); | |
6204 } | |
6205 viminfo_writestring(fp, p); | |
6206 } | |
6207 if (--i < 0) | |
6208 i = hislen - 1; | |
6209 } | |
6210 } | |
6211 } | |
6212 #endif /* FEAT_VIMINFO */ | |
6213 | |
6214 #if defined(FEAT_FKMAP) || defined(PROTO) | |
6215 /* | |
6216 * Write a character at the current cursor+offset position. | |
6217 * It is directly written into the command buffer block. | |
6218 */ | |
6219 void | |
6220 cmd_pchar(c, offset) | |
6221 int c, offset; | |
6222 { | |
6223 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) | |
6224 { | |
6225 EMSG(_("E198: cmd_pchar beyond the command length")); | |
6226 return; | |
6227 } | |
6228 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; | |
6229 ccline.cmdbuff[ccline.cmdlen] = NUL; | |
6230 } | |
6231 | |
6232 int | |
6233 cmd_gchar(offset) | |
6234 int offset; | |
6235 { | |
6236 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) | |
6237 { | |
6238 /* EMSG(_("cmd_gchar beyond the command length")); */ | |
6239 return NUL; | |
6240 } | |
6241 return (int)ccline.cmdbuff[ccline.cmdpos + offset]; | |
6242 } | |
6243 #endif | |
6244 | |
6245 #if defined(FEAT_CMDWIN) || defined(PROTO) | |
6246 /* | |
6247 * Open a window on the current command line and history. Allow editing in | |
6248 * the window. Returns when the window is closed. | |
6249 * Returns: | |
6250 * CR if the command is to be executed | |
6251 * Ctrl_C if it is to be abandoned | |
6252 * K_IGNORE if editing continues | |
6253 */ | |
6254 static int | |
6255 ex_window() | |
6256 { | |
6257 struct cmdline_info save_ccline; | |
6258 buf_T *old_curbuf = curbuf; | |
6259 win_T *old_curwin = curwin; | |
6260 buf_T *bp; | |
6261 win_T *wp; | |
6262 int i; | |
6263 linenr_T lnum; | |
6264 int histtype; | |
6265 garray_T winsizes; | |
1685 | 6266 #ifdef FEAT_AUTOCMD |
7 | 6267 char_u typestr[2]; |
1685 | 6268 #endif |
7 | 6269 int save_restart_edit = restart_edit; |
6270 int save_State = State; | |
6271 int save_exmode = exmode_active; | |
510 | 6272 #ifdef FEAT_RIGHTLEFT |
6273 int save_cmdmsg_rl = cmdmsg_rl; | |
6274 #endif | |
7 | 6275 |
6276 /* Can't do this recursively. Can't do it when typing a password. */ | |
6277 if (cmdwin_type != 0 | |
6278 # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) | |
6279 || cmdline_star > 0 | |
6280 # endif | |
6281 ) | |
6282 { | |
6283 beep_flush(); | |
6284 return K_IGNORE; | |
6285 } | |
6286 | |
6287 /* Save current window sizes. */ | |
6288 win_size_save(&winsizes); | |
6289 | |
6290 # ifdef FEAT_AUTOCMD | |
6291 /* Don't execute autocommands while creating the window. */ | |
1410 | 6292 block_autocmds(); |
7 | 6293 # endif |
683 | 6294 /* don't use a new tab page */ |
6295 cmdmod.tab = 0; | |
6296 | |
7 | 6297 /* Create a window for the command-line buffer. */ |
6298 if (win_split((int)p_cwh, WSP_BOT) == FAIL) | |
6299 { | |
6300 beep_flush(); | |
1410 | 6301 # ifdef FEAT_AUTOCMD |
6302 unblock_autocmds(); | |
6303 # endif | |
7 | 6304 return K_IGNORE; |
6305 } | |
1831 | 6306 cmdwin_type = get_cmdline_type(); |
7 | 6307 |
6308 /* Create the command-line buffer empty. */ | |
1743 | 6309 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
1621 | 6310 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
7 | 6311 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
6312 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); | |
6313 curbuf->b_p_ma = TRUE; | |
1865 | 6314 #ifdef FEAT_FOLDING |
6315 curwin->w_p_fen = FALSE; | |
6316 #endif | |
7 | 6317 # ifdef FEAT_RIGHTLEFT |
510 | 6318 curwin->w_p_rl = cmdmsg_rl; |
6319 cmdmsg_rl = FALSE; | |
7 | 6320 # endif |
2583 | 6321 RESET_BINDING(curwin); |
7 | 6322 |
6323 # ifdef FEAT_AUTOCMD | |
6324 /* Do execute autocommands for setting the filetype (load syntax). */ | |
1410 | 6325 unblock_autocmds(); |
7 | 6326 # endif |
6327 | |
510 | 6328 /* Showing the prompt may have set need_wait_return, reset it. */ |
6329 need_wait_return = FALSE; | |
6330 | |
1831 | 6331 histtype = hist_char2type(cmdwin_type); |
7 | 6332 if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
6333 { | |
6334 if (p_wc == TAB) | |
6335 { | |
6336 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); | |
6337 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); | |
6338 } | |
6339 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); | |
6340 } | |
6341 | |
10 | 6342 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
6343 * sets 'textwidth' to 78). */ | |
6344 curbuf->b_p_tw = 0; | |
6345 | |
7 | 6346 /* Fill the buffer with the history. */ |
6347 init_history(); | |
6348 if (hislen > 0) | |
6349 { | |
6350 i = hisidx[histtype]; | |
6351 if (i >= 0) | |
6352 { | |
6353 lnum = 0; | |
6354 do | |
6355 { | |
6356 if (++i == hislen) | |
6357 i = 0; | |
6358 if (history[histtype][i].hisstr != NULL) | |
6359 ml_append(lnum++, history[histtype][i].hisstr, | |
6360 (colnr_T)0, FALSE); | |
6361 } | |
6362 while (i != hisidx[histtype]); | |
6363 } | |
6364 } | |
6365 | |
6366 /* Replace the empty last line with the current command-line and put the | |
6367 * cursor there. */ | |
6368 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); | |
6369 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; | |
6370 curwin->w_cursor.col = ccline.cmdpos; | |
510 | 6371 changed_line_abv_curs(); |
6372 invalidate_botline(); | |
743 | 6373 redraw_later(SOME_VALID); |
7 | 6374 |
6375 /* Save the command line info, can be used recursively. */ | |
6376 save_ccline = ccline; | |
6377 ccline.cmdbuff = NULL; | |
6378 ccline.cmdprompt = NULL; | |
6379 | |
6380 /* No Ex mode here! */ | |
6381 exmode_active = 0; | |
6382 | |
6383 State = NORMAL; | |
6384 # ifdef FEAT_MOUSE | |
6385 setmouse(); | |
6386 # endif | |
6387 | |
6388 # ifdef FEAT_AUTOCMD | |
6389 /* Trigger CmdwinEnter autocommands. */ | |
6390 typestr[0] = cmdwin_type; | |
6391 typestr[1] = NUL; | |
6392 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf); | |
929 | 6393 if (restart_edit != 0) /* autocmd with ":startinsert" */ |
6394 stuffcharReadbuff(K_NOP); | |
7 | 6395 # endif |
6396 | |
6397 i = RedrawingDisabled; | |
6398 RedrawingDisabled = 0; | |
6399 | |
6400 /* | |
6401 * Call the main loop until <CR> or CTRL-C is typed. | |
6402 */ | |
6403 cmdwin_result = 0; | |
168 | 6404 main_loop(TRUE, FALSE); |
7 | 6405 |
6406 RedrawingDisabled = i; | |
6407 | |
6408 # ifdef FEAT_AUTOCMD | |
6409 /* Trigger CmdwinLeave autocommands. */ | |
6410 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf); | |
6411 # endif | |
6412 | |
1214 | 6413 /* Restore the command line info. */ |
7 | 6414 ccline = save_ccline; |
6415 cmdwin_type = 0; | |
6416 | |
6417 exmode_active = save_exmode; | |
6418 | |
1612 | 6419 /* Safety check: The old window or buffer was deleted: It's a bug when |
7 | 6420 * this happens! */ |
6421 if (!win_valid(old_curwin) || !buf_valid(old_curbuf)) | |
6422 { | |
6423 cmdwin_result = Ctrl_C; | |
6424 EMSG(_("E199: Active window or buffer deleted")); | |
6425 } | |
6426 else | |
6427 { | |
6428 # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
6429 /* autocmds may abort script processing */ | |
6430 if (aborting() && cmdwin_result != K_IGNORE) | |
6431 cmdwin_result = Ctrl_C; | |
6432 # endif | |
6433 /* Set the new command line from the cmdline buffer. */ | |
6434 vim_free(ccline.cmdbuff); | |
510 | 6435 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
7 | 6436 { |
510 | 6437 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
6438 | |
6439 if (histtype == HIST_CMD) | |
6440 { | |
6441 /* Execute the command directly. */ | |
6442 ccline.cmdbuff = vim_strsave((char_u *)p); | |
6443 cmdwin_result = CAR; | |
6444 } | |
6445 else | |
6446 { | |
6447 /* First need to cancel what we were doing. */ | |
6448 ccline.cmdbuff = NULL; | |
6449 stuffcharReadbuff(':'); | |
6450 stuffReadbuff((char_u *)p); | |
6451 stuffcharReadbuff(CAR); | |
6452 } | |
7 | 6453 } |
6454 else if (cmdwin_result == K_XF2) /* :qa typed */ | |
6455 { | |
6456 ccline.cmdbuff = vim_strsave((char_u *)"qa"); | |
6457 cmdwin_result = CAR; | |
6458 } | |
2839 | 6459 else if (cmdwin_result == Ctrl_C) |
6460 { | |
6461 /* :q or :close, don't execute any command | |
6462 * and don't modify the cmd window. */ | |
6463 ccline.cmdbuff = NULL; | |
6464 } | |
7 | 6465 else |
6466 ccline.cmdbuff = vim_strsave(ml_get_curline()); | |
6467 if (ccline.cmdbuff == NULL) | |
6468 cmdwin_result = Ctrl_C; | |
6469 else | |
6470 { | |
6471 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); | |
6472 ccline.cmdbufflen = ccline.cmdlen + 1; | |
6473 ccline.cmdpos = curwin->w_cursor.col; | |
6474 if (ccline.cmdpos > ccline.cmdlen) | |
6475 ccline.cmdpos = ccline.cmdlen; | |
6476 if (cmdwin_result == K_IGNORE) | |
6477 { | |
6478 set_cmdspos_cursor(); | |
6479 redrawcmd(); | |
6480 } | |
6481 } | |
6482 | |
6483 # ifdef FEAT_AUTOCMD | |
6484 /* Don't execute autocommands while deleting the window. */ | |
1410 | 6485 block_autocmds(); |
7 | 6486 # endif |
6487 wp = curwin; | |
6488 bp = curbuf; | |
6489 win_goto(old_curwin); | |
6490 win_close(wp, TRUE); | |
2099
c1f67ce5740a
updated for version 7.2.382
Bram Moolenaar <bram@zimbu.org>
parents:
2097
diff
changeset
|
6491 |
c1f67ce5740a
updated for version 7.2.382
Bram Moolenaar <bram@zimbu.org>
parents:
2097
diff
changeset
|
6492 /* win_close() may have already wiped the buffer when 'bh' is |
c1f67ce5740a
updated for version 7.2.382
Bram Moolenaar <bram@zimbu.org>
parents:
2097
diff
changeset
|
6493 * set to 'wipe' */ |
c1f67ce5740a
updated for version 7.2.382
Bram Moolenaar <bram@zimbu.org>
parents:
2097
diff
changeset
|
6494 if (buf_valid(bp)) |
3365 | 6495 close_buffer(NULL, bp, DOBUF_WIPE, FALSE); |
7 | 6496 |
6497 /* Restore window sizes. */ | |
6498 win_size_restore(&winsizes); | |
6499 | |
6500 # ifdef FEAT_AUTOCMD | |
1410 | 6501 unblock_autocmds(); |
7 | 6502 # endif |
6503 } | |
6504 | |
6505 ga_clear(&winsizes); | |
6506 restart_edit = save_restart_edit; | |
510 | 6507 # ifdef FEAT_RIGHTLEFT |
6508 cmdmsg_rl = save_cmdmsg_rl; | |
6509 # endif | |
7 | 6510 |
6511 State = save_State; | |
6512 # ifdef FEAT_MOUSE | |
6513 setmouse(); | |
6514 # endif | |
6515 | |
6516 return cmdwin_result; | |
6517 } | |
6518 #endif /* FEAT_CMDWIN */ | |
6519 | |
6520 /* | |
6521 * Used for commands that either take a simple command string argument, or: | |
6522 * cmd << endmarker | |
6523 * {script} | |
6524 * endmarker | |
6525 * Returns a pointer to allocated memory with {script} or NULL. | |
6526 */ | |
6527 char_u * | |
6528 script_get(eap, cmd) | |
6529 exarg_T *eap; | |
6530 char_u *cmd; | |
6531 { | |
6532 char_u *theline; | |
6533 char *end_pattern = NULL; | |
6534 char dot[] = "."; | |
6535 garray_T ga; | |
6536 | |
6537 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) | |
6538 return NULL; | |
6539 | |
6540 ga_init2(&ga, 1, 0x400); | |
6541 | |
6542 if (cmd[2] != NUL) | |
6543 end_pattern = (char *)skipwhite(cmd + 2); | |
6544 else | |
6545 end_pattern = dot; | |
6546 | |
6547 for (;;) | |
6548 { | |
6549 theline = eap->getline( | |
6550 #ifdef FEAT_EVAL | |
75 | 6551 eap->cstack->cs_looplevel > 0 ? -1 : |
7 | 6552 #endif |
6553 NUL, eap->cookie, 0); | |
6554 | |
6555 if (theline == NULL || STRCMP(end_pattern, theline) == 0) | |
1694 | 6556 { |
6557 vim_free(theline); | |
7 | 6558 break; |
1694 | 6559 } |
7 | 6560 |
6561 ga_concat(&ga, theline); | |
6562 ga_append(&ga, '\n'); | |
6563 vim_free(theline); | |
6564 } | |
21 | 6565 ga_append(&ga, NUL); |
7 | 6566 |
6567 return (char_u *)ga.ga_data; | |
6568 } |