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