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