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