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