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