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