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