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