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