1 /* vi:set ts=8 sts=4 sw=4 noet: 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_docmd.c: functions for executing an Ex command line. 12 */ 13 14 #include "vim.h" 15 16 static int quitmore = 0; 17 static int ex_pressedreturn = FALSE; 18 #ifndef FEAT_PRINTER 19 # define ex_hardcopy ex_ni 20 #endif 21 22 #ifdef FEAT_USR_CMDS 23 typedef struct ucmd 24 { 25 char_u *uc_name; /* The command name */ 26 long_u uc_argt; /* The argument type */ 27 char_u *uc_rep; /* The command's replacement string */ 28 long uc_def; /* The default value for a range/count */ 29 int uc_compl; /* completion type */ 30 int uc_addr_type; /* The command's address type */ 31 # ifdef FEAT_EVAL 32 sctx_T uc_script_ctx; /* SCTX where the command was defined */ 33 # ifdef FEAT_CMDL_COMPL 34 char_u *uc_compl_arg; /* completion argument if any */ 35 # endif 36 # endif 37 } ucmd_T; 38 39 #define UC_BUFFER 1 /* -buffer: local to current buffer */ 40 41 static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL}; 42 43 #define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i]) 44 #define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i]) 45 46 static void do_ucmd(exarg_T *eap); 47 static void ex_command(exarg_T *eap); 48 static void ex_delcommand(exarg_T *eap); 49 # ifdef FEAT_CMDL_COMPL 50 static char_u *get_user_command_name(int idx); 51 # endif 52 53 /* Wether a command index indicates a user command. */ 54 # define IS_USER_CMDIDX(idx) ((int)(idx) < 0) 55 56 #else 57 # define ex_command ex_ni 58 # define ex_comclear ex_ni 59 # define ex_delcommand ex_ni 60 /* Wether a command index indicates a user command. */ 61 # define IS_USER_CMDIDX(idx) (FALSE) 62 #endif 63 64 #ifdef FEAT_EVAL 65 static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie); 66 #else 67 static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie); 68 static int if_level = 0; /* depth in :if */ 69 #endif 70 static void free_cmdmod(void); 71 static void append_command(char_u *cmd); 72 static char_u *find_command(exarg_T *eap, int *full); 73 74 static void ex_abbreviate(exarg_T *eap); 75 static void ex_map(exarg_T *eap); 76 static void ex_unmap(exarg_T *eap); 77 static void ex_mapclear(exarg_T *eap); 78 static void ex_abclear(exarg_T *eap); 79 #ifndef FEAT_MENU 80 # define ex_emenu ex_ni 81 # define ex_menu ex_ni 82 # define ex_menutranslate ex_ni 83 #endif 84 static void ex_autocmd(exarg_T *eap); 85 static void ex_doautocmd(exarg_T *eap); 86 static void ex_bunload(exarg_T *eap); 87 static void ex_buffer(exarg_T *eap); 88 static void ex_bmodified(exarg_T *eap); 89 static void ex_bnext(exarg_T *eap); 90 static void ex_bprevious(exarg_T *eap); 91 static void ex_brewind(exarg_T *eap); 92 static void ex_blast(exarg_T *eap); 93 static char_u *getargcmd(char_u **); 94 static char_u *skip_cmd_arg(char_u *p, int rembs); 95 static int getargopt(exarg_T *eap); 96 #ifndef FEAT_QUICKFIX 97 # define ex_make ex_ni 98 # define ex_cbuffer ex_ni 99 # define ex_cc ex_ni 100 # define ex_cnext ex_ni 101 # define ex_cfile ex_ni 102 # define qf_list ex_ni 103 # define qf_age ex_ni 104 # define qf_history ex_ni 105 # define ex_helpgrep ex_ni 106 # define ex_vimgrep ex_ni 107 #endif 108 #if !defined(FEAT_QUICKFIX) 109 # define ex_cclose ex_ni 110 # define ex_copen ex_ni 111 # define ex_cwindow ex_ni 112 # define ex_cbottom ex_ni 113 #endif 114 #if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL) 115 # define ex_cexpr ex_ni 116 #endif 117 118 static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int silent, int to_other_file, int address_count); 119 static void get_flags(exarg_T *eap); 120 #if !defined(FEAT_PERL) \ 121 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \ 122 || !defined(FEAT_TCL) \ 123 || !defined(FEAT_RUBY) \ 124 || !defined(FEAT_LUA) \ 125 || !defined(FEAT_MZSCHEME) 126 # define HAVE_EX_SCRIPT_NI 127 static void ex_script_ni(exarg_T *eap); 128 #endif 129 static char *invalid_range(exarg_T *eap); 130 static void correct_range(exarg_T *eap); 131 #ifdef FEAT_QUICKFIX 132 static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep); 133 #endif 134 static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep); 135 static void ex_highlight(exarg_T *eap); 136 static void ex_colorscheme(exarg_T *eap); 137 static void ex_quit(exarg_T *eap); 138 static void ex_cquit(exarg_T *eap); 139 static void ex_quit_all(exarg_T *eap); 140 static void ex_close(exarg_T *eap); 141 static void ex_win_close(int forceit, win_T *win, tabpage_T *tp); 142 static void ex_only(exarg_T *eap); 143 static void ex_resize(exarg_T *eap); 144 static void ex_stag(exarg_T *eap); 145 static void ex_tabclose(exarg_T *eap); 146 static void ex_tabonly(exarg_T *eap); 147 static void ex_tabnext(exarg_T *eap); 148 static void ex_tabmove(exarg_T *eap); 149 static void ex_tabs(exarg_T *eap); 150 #if defined(FEAT_QUICKFIX) 151 static void ex_pclose(exarg_T *eap); 152 static void ex_ptag(exarg_T *eap); 153 static void ex_pedit(exarg_T *eap); 154 #else 155 # define ex_pclose ex_ni 156 # define ex_ptag ex_ni 157 # define ex_pedit ex_ni 158 #endif 159 static void ex_hide(exarg_T *eap); 160 static void ex_stop(exarg_T *eap); 161 static void ex_exit(exarg_T *eap); 162 static void ex_print(exarg_T *eap); 163 #ifdef FEAT_BYTEOFF 164 static void ex_goto(exarg_T *eap); 165 #else 166 # define ex_goto ex_ni 167 #endif 168 static void ex_shell(exarg_T *eap); 169 static void ex_preserve(exarg_T *eap); 170 static void ex_recover(exarg_T *eap); 171 static void ex_mode(exarg_T *eap); 172 static void ex_wrongmodifier(exarg_T *eap); 173 static void ex_find(exarg_T *eap); 174 static void ex_open(exarg_T *eap); 175 static void ex_edit(exarg_T *eap); 176 #ifndef FEAT_GUI 177 # define ex_gui ex_nogui 178 static void ex_nogui(exarg_T *eap); 179 #endif 180 #if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) 181 static void ex_tearoff(exarg_T *eap); 182 #else 183 # define ex_tearoff ex_ni 184 #endif 185 #if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \ 186 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU) 187 static void ex_popup(exarg_T *eap); 188 #else 189 # define ex_popup ex_ni 190 #endif 191 #ifndef FEAT_GUI_MSWIN 192 # define ex_simalt ex_ni 193 #endif 194 #if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) 195 # define gui_mch_find_dialog ex_ni 196 # define gui_mch_replace_dialog ex_ni 197 #endif 198 #if !defined(FEAT_GUI_GTK) 199 # define ex_helpfind ex_ni 200 #endif 201 #ifndef FEAT_CSCOPE 202 # define ex_cscope ex_ni 203 # define ex_scscope ex_ni 204 # define ex_cstag ex_ni 205 #endif 206 #ifndef FEAT_SYN_HL 207 # define ex_syntax ex_ni 208 # define ex_ownsyntax ex_ni 209 #endif 210 #ifndef FEAT_EVAL 211 # define ex_packadd ex_ni 212 # define ex_packloadall ex_ni 213 #endif 214 #if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE) 215 # define ex_syntime ex_ni 216 #endif 217 #ifndef FEAT_SPELL 218 # define ex_spell ex_ni 219 # define ex_mkspell ex_ni 220 # define ex_spelldump ex_ni 221 # define ex_spellinfo ex_ni 222 # define ex_spellrepall ex_ni 223 #endif 224 #ifndef FEAT_PERSISTENT_UNDO 225 # define ex_rundo ex_ni 226 # define ex_wundo ex_ni 227 #endif 228 #ifndef FEAT_LUA 229 # define ex_lua ex_script_ni 230 # define ex_luado ex_ni 231 # define ex_luafile ex_ni 232 #endif 233 #ifndef FEAT_MZSCHEME 234 # define ex_mzscheme ex_script_ni 235 # define ex_mzfile ex_ni 236 #endif 237 #ifndef FEAT_PERL 238 # define ex_perl ex_script_ni 239 # define ex_perldo ex_ni 240 #endif 241 #ifndef FEAT_PYTHON 242 # define ex_python ex_script_ni 243 # define ex_pydo ex_ni 244 # define ex_pyfile ex_ni 245 #endif 246 #ifndef FEAT_PYTHON3 247 # define ex_py3 ex_script_ni 248 # define ex_py3do ex_ni 249 # define ex_py3file ex_ni 250 #endif 251 #if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3) 252 # define ex_pyx ex_script_ni 253 # define ex_pyxdo ex_ni 254 # define ex_pyxfile ex_ni 255 #endif 256 #ifndef FEAT_TCL 257 # define ex_tcl ex_script_ni 258 # define ex_tcldo ex_ni 259 # define ex_tclfile ex_ni 260 #endif 261 #ifndef FEAT_RUBY 262 # define ex_ruby ex_script_ni 263 # define ex_rubydo ex_ni 264 # define ex_rubyfile ex_ni 265 #endif 266 #ifndef FEAT_KEYMAP 267 # define ex_loadkeymap ex_ni 268 #endif 269 static void ex_swapname(exarg_T *eap); 270 static void ex_syncbind(exarg_T *eap); 271 static void ex_read(exarg_T *eap); 272 static void ex_pwd(exarg_T *eap); 273 static void ex_equal(exarg_T *eap); 274 static void ex_sleep(exarg_T *eap); 275 static void do_exmap(exarg_T *eap, int isabbrev); 276 static void ex_winsize(exarg_T *eap); 277 static void ex_wincmd(exarg_T *eap); 278 #if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN) 279 static void ex_winpos(exarg_T *eap); 280 #else 281 # define ex_winpos ex_ni 282 #endif 283 static void ex_operators(exarg_T *eap); 284 static void ex_put(exarg_T *eap); 285 static void ex_copymove(exarg_T *eap); 286 static void ex_submagic(exarg_T *eap); 287 static void ex_join(exarg_T *eap); 288 static void ex_at(exarg_T *eap); 289 static void ex_bang(exarg_T *eap); 290 static void ex_undo(exarg_T *eap); 291 #ifdef FEAT_PERSISTENT_UNDO 292 static void ex_wundo(exarg_T *eap); 293 static void ex_rundo(exarg_T *eap); 294 #endif 295 static void ex_redo(exarg_T *eap); 296 static void ex_later(exarg_T *eap); 297 static void ex_redir(exarg_T *eap); 298 static void ex_redrawstatus(exarg_T *eap); 299 static void ex_redrawtabline(exarg_T *eap); 300 static void close_redir(void); 301 static void ex_mkrc(exarg_T *eap); 302 static void ex_mark(exarg_T *eap); 303 #ifdef FEAT_USR_CMDS 304 static char *uc_fun_cmd(void); 305 static char_u *find_ucmd(exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl); 306 #endif 307 static void ex_startinsert(exarg_T *eap); 308 static void ex_stopinsert(exarg_T *eap); 309 #ifdef FEAT_FIND_ID 310 static void ex_checkpath(exarg_T *eap); 311 static void ex_findpat(exarg_T *eap); 312 #else 313 # define ex_findpat ex_ni 314 # define ex_checkpath ex_ni 315 #endif 316 #if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX) 317 static void ex_psearch(exarg_T *eap); 318 #else 319 # define ex_psearch ex_ni 320 #endif 321 static void ex_tag(exarg_T *eap); 322 static void ex_tag_cmd(exarg_T *eap, char_u *name); 323 #ifndef FEAT_EVAL 324 # define ex_scriptnames ex_ni 325 # define ex_finish ex_ni 326 # define ex_echo ex_ni 327 # define ex_echohl ex_ni 328 # define ex_execute ex_ni 329 # define ex_call ex_ni 330 # define ex_if ex_ni 331 # define ex_endif ex_ni 332 # define ex_else ex_ni 333 # define ex_while ex_ni 334 # define ex_continue ex_ni 335 # define ex_break ex_ni 336 # define ex_endwhile ex_ni 337 # define ex_throw ex_ni 338 # define ex_try ex_ni 339 # define ex_catch ex_ni 340 # define ex_finally ex_ni 341 # define ex_endtry ex_ni 342 # define ex_endfunction ex_ni 343 # define ex_let ex_ni 344 # define ex_unlet ex_ni 345 # define ex_lockvar ex_ni 346 # define ex_unlockvar ex_ni 347 # define ex_function ex_ni 348 # define ex_delfunction ex_ni 349 # define ex_return ex_ni 350 # define ex_oldfiles ex_ni 351 #endif 352 static char_u *arg_all(void); 353 #ifdef FEAT_SESSION 354 static int makeopens(FILE *fd, char_u *dirnow); 355 static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx); 356 static void ex_loadview(exarg_T *eap); 357 static char_u *get_view_file(int c); 358 static int did_lcd; /* whether ":lcd" was produced for a session */ 359 #else 360 # define ex_loadview ex_ni 361 #endif 362 #ifndef FEAT_EVAL 363 # define ex_compiler ex_ni 364 #endif 365 #ifdef FEAT_VIMINFO 366 static void ex_viminfo(exarg_T *eap); 367 #else 368 # define ex_viminfo ex_ni 369 #endif 370 static void ex_behave(exarg_T *eap); 371 static void ex_filetype(exarg_T *eap); 372 static void ex_setfiletype(exarg_T *eap); 373 #ifndef FEAT_DIFF 374 # define ex_diffoff ex_ni 375 # define ex_diffpatch ex_ni 376 # define ex_diffgetput ex_ni 377 # define ex_diffsplit ex_ni 378 # define ex_diffthis ex_ni 379 # define ex_diffupdate ex_ni 380 #endif 381 static void ex_digraphs(exarg_T *eap); 382 static void ex_set(exarg_T *eap); 383 #if !defined(FEAT_EVAL) 384 # define ex_options ex_ni 385 #endif 386 #ifdef FEAT_SEARCH_EXTRA 387 static void ex_nohlsearch(exarg_T *eap); 388 static void ex_match(exarg_T *eap); 389 #else 390 # define ex_nohlsearch ex_ni 391 # define ex_match ex_ni 392 #endif 393 #ifdef FEAT_CRYPT 394 static void ex_X(exarg_T *eap); 395 #else 396 # define ex_X ex_ni 397 #endif 398 #ifdef FEAT_FOLDING 399 static void ex_fold(exarg_T *eap); 400 static void ex_foldopen(exarg_T *eap); 401 static void ex_folddo(exarg_T *eap); 402 #else 403 # define ex_fold ex_ni 404 # define ex_foldopen ex_ni 405 # define ex_folddo ex_ni 406 #endif 407 #if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE)) 408 # define ex_language ex_ni 409 #endif 410 #ifndef FEAT_SIGNS 411 # define ex_sign ex_ni 412 #endif 413 #ifndef FEAT_NETBEANS_INTG 414 # define ex_nbclose ex_ni 415 # define ex_nbkey ex_ni 416 # define ex_nbstart ex_ni 417 #endif 418 419 #ifndef FEAT_EVAL 420 # define ex_debug ex_ni 421 # define ex_breakadd ex_ni 422 # define ex_debuggreedy ex_ni 423 # define ex_breakdel ex_ni 424 # define ex_breaklist ex_ni 425 #endif 426 427 #ifndef FEAT_CMDHIST 428 # define ex_history ex_ni 429 #endif 430 #ifndef FEAT_JUMPLIST 431 # define ex_jumps ex_ni 432 # define ex_clearjumps ex_ni 433 # define ex_changes ex_ni 434 #endif 435 436 #ifndef FEAT_PROFILE 437 # define ex_profile ex_ni 438 #endif 439 #ifndef FEAT_TERMINAL 440 # define ex_terminal ex_ni 441 #endif 442 443 /* 444 * Declare cmdnames[]. 445 */ 446 #define DO_DECLARE_EXCMD 447 #include "ex_cmds.h" 448 #include "ex_cmdidxs.h" 449 450 static char_u dollar_command[2] = {'$', 0}; 451 452 453 #ifdef FEAT_EVAL 454 /* Struct for storing a line inside a while/for loop */ 455 typedef struct 456 { 457 char_u *line; /* command line */ 458 linenr_T lnum; /* sourcing_lnum of the line */ 459 } wcmd_T; 460 461 /* 462 * Structure used to store info for line position in a while or for loop. 463 * This is required, because do_one_cmd() may invoke ex_function(), which 464 * reads more lines that may come from the while/for loop. 465 */ 466 struct loop_cookie 467 { 468 garray_T *lines_gap; /* growarray with line info */ 469 int current_line; /* last read line from growarray */ 470 int repeating; /* TRUE when looping a second time */ 471 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */ 472 char_u *(*getline)(int, void *, int); 473 void *cookie; 474 }; 475 476 static char_u *get_loop_line(int c, void *cookie, int indent); 477 static int store_loop_line(garray_T *gap, char_u *line); 478 static void free_cmdlines(garray_T *gap); 479 480 /* Struct to save a few things while debugging. Used in do_cmdline() only. */ 481 struct dbg_stuff 482 { 483 int trylevel; 484 int force_abort; 485 except_T *caught_stack; 486 char_u *vv_exception; 487 char_u *vv_throwpoint; 488 int did_emsg; 489 int got_int; 490 int did_throw; 491 int need_rethrow; 492 int check_cstack; 493 except_T *current_exception; 494 }; 495 496 static void 497 save_dbg_stuff(struct dbg_stuff *dsp) 498 { 499 dsp->trylevel = trylevel; trylevel = 0; 500 dsp->force_abort = force_abort; force_abort = FALSE; 501 dsp->caught_stack = caught_stack; caught_stack = NULL; 502 dsp->vv_exception = v_exception(NULL); 503 dsp->vv_throwpoint = v_throwpoint(NULL); 504 505 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */ 506 dsp->did_emsg = did_emsg; did_emsg = FALSE; 507 dsp->got_int = got_int; got_int = FALSE; 508 dsp->did_throw = did_throw; did_throw = FALSE; 509 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE; 510 dsp->check_cstack = check_cstack; check_cstack = FALSE; 511 dsp->current_exception = current_exception; current_exception = NULL; 512 } 513 514 static void 515 restore_dbg_stuff(struct dbg_stuff *dsp) 516 { 517 suppress_errthrow = FALSE; 518 trylevel = dsp->trylevel; 519 force_abort = dsp->force_abort; 520 caught_stack = dsp->caught_stack; 521 (void)v_exception(dsp->vv_exception); 522 (void)v_throwpoint(dsp->vv_throwpoint); 523 did_emsg = dsp->did_emsg; 524 got_int = dsp->got_int; 525 did_throw = dsp->did_throw; 526 need_rethrow = dsp->need_rethrow; 527 check_cstack = dsp->check_cstack; 528 current_exception = dsp->current_exception; 529 } 530 #endif 531 532 /* 533 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi" 534 * command is given. 535 */ 536 void 537 do_exmode( 538 int improved) /* TRUE for "improved Ex" mode */ 539 { 540 int save_msg_scroll; 541 int prev_msg_row; 542 linenr_T prev_line; 543 varnumber_T changedtick; 544 545 if (improved) 546 exmode_active = EXMODE_VIM; 547 else 548 exmode_active = EXMODE_NORMAL; 549 State = NORMAL; 550 551 /* When using ":global /pat/ visual" and then "Q" we return to continue 552 * the :global command. */ 553 if (global_busy) 554 return; 555 556 save_msg_scroll = msg_scroll; 557 ++RedrawingDisabled; /* don't redisplay the window */ 558 ++no_wait_return; /* don't wait for return */ 559 #ifdef FEAT_GUI 560 /* Ignore scrollbar and mouse events in Ex mode */ 561 ++hold_gui_events; 562 #endif 563 564 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode.")); 565 while (exmode_active) 566 { 567 /* Check for a ":normal" command and no more characters left. */ 568 if (ex_normal_busy > 0 && typebuf.tb_len == 0) 569 { 570 exmode_active = FALSE; 571 break; 572 } 573 msg_scroll = TRUE; 574 need_wait_return = FALSE; 575 ex_pressedreturn = FALSE; 576 ex_no_reprint = FALSE; 577 changedtick = CHANGEDTICK(curbuf); 578 prev_msg_row = msg_row; 579 prev_line = curwin->w_cursor.lnum; 580 if (improved) 581 { 582 cmdline_row = msg_row; 583 do_cmdline(NULL, getexline, NULL, 0); 584 } 585 else 586 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT); 587 lines_left = Rows - 1; 588 589 if ((prev_line != curwin->w_cursor.lnum 590 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint) 591 { 592 if (curbuf->b_ml.ml_flags & ML_EMPTY) 593 emsg(_(e_emptybuf)); 594 else 595 { 596 if (ex_pressedreturn) 597 { 598 /* go up one line, to overwrite the ":<CR>" line, so the 599 * output doesn't contain empty lines. */ 600 msg_row = prev_msg_row; 601 if (prev_msg_row == Rows - 1) 602 msg_row--; 603 } 604 msg_col = 0; 605 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE); 606 msg_clr_eos(); 607 } 608 } 609 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */ 610 { 611 if (curbuf->b_ml.ml_flags & ML_EMPTY) 612 emsg(_(e_emptybuf)); 613 else 614 emsg(_("E501: At end-of-file")); 615 } 616 } 617 618 #ifdef FEAT_GUI 619 --hold_gui_events; 620 #endif 621 --RedrawingDisabled; 622 --no_wait_return; 623 update_screen(CLEAR); 624 need_wait_return = FALSE; 625 msg_scroll = save_msg_scroll; 626 } 627 628 /* 629 * Execute a simple command line. Used for translated commands like "*". 630 */ 631 int 632 do_cmdline_cmd(char_u *cmd) 633 { 634 return do_cmdline(cmd, NULL, NULL, 635 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); 636 } 637 638 /* 639 * do_cmdline(): execute one Ex command line 640 * 641 * 1. Execute "cmdline" when it is not NULL. 642 * If "cmdline" is NULL, or more lines are needed, fgetline() is used. 643 * 2. Split up in parts separated with '|'. 644 * 645 * This function can be called recursively! 646 * 647 * flags: 648 * DOCMD_VERBOSE - The command will be included in the error message. 649 * DOCMD_NOWAIT - Don't call wait_return() and friends. 650 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL. 651 * DOCMD_KEYTYPED - Don't reset KeyTyped. 652 * DOCMD_EXCRESET - Reset the exception environment (used for debugging). 653 * DOCMD_KEEPLINE - Store first typed line (for repeating with "."). 654 * 655 * return FAIL if cmdline could not be executed, OK otherwise 656 */ 657 int 658 do_cmdline( 659 char_u *cmdline, 660 char_u *(*fgetline)(int, void *, int), 661 void *cookie, /* argument for fgetline() */ 662 int flags) 663 { 664 char_u *next_cmdline; /* next cmd to execute */ 665 char_u *cmdline_copy = NULL; /* copy of cmd line */ 666 int used_getline = FALSE; /* used "fgetline" to obtain command */ 667 static int recursive = 0; /* recursive depth */ 668 int msg_didout_before_start = 0; 669 int count = 0; /* line number count */ 670 int did_inc = FALSE; /* incremented RedrawingDisabled */ 671 int retval = OK; 672 #ifdef FEAT_EVAL 673 struct condstack cstack; /* conditional stack */ 674 garray_T lines_ga; /* keep lines for ":while"/":for" */ 675 int current_line = 0; /* active line in lines_ga */ 676 char_u *fname = NULL; /* function or script name */ 677 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */ 678 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */ 679 struct dbg_stuff debug_saved; /* saved things for debug mode */ 680 int initial_trylevel; 681 struct msglist **saved_msg_list = NULL; 682 struct msglist *private_msg_list; 683 684 /* "fgetline" and "cookie" passed to do_one_cmd() */ 685 char_u *(*cmd_getline)(int, void *, int); 686 void *cmd_cookie; 687 struct loop_cookie cmd_loop_cookie; 688 void *real_cookie; 689 int getline_is_func; 690 #else 691 # define cmd_getline fgetline 692 # define cmd_cookie cookie 693 #endif 694 static int call_depth = 0; /* recursiveness */ 695 696 #ifdef FEAT_EVAL 697 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory 698 * location for storing error messages to be converted to an exception. 699 * This ensures that the do_errthrow() call in do_one_cmd() does not 700 * combine the messages stored by an earlier invocation of do_one_cmd() 701 * with the command name of the later one. This would happen when 702 * BufWritePost autocommands are executed after a write error. */ 703 saved_msg_list = msg_list; 704 msg_list = &private_msg_list; 705 private_msg_list = NULL; 706 #endif 707 708 /* It's possible to create an endless loop with ":execute", catch that 709 * here. The value of 200 allows nested function calls, ":source", etc. 710 * Allow 200 or 'maxfuncdepth', whatever is larger. */ 711 if (call_depth >= 200 712 #ifdef FEAT_EVAL 713 && call_depth >= p_mfd 714 #endif 715 ) 716 { 717 emsg(_("E169: Command too recursive")); 718 #ifdef FEAT_EVAL 719 /* When converting to an exception, we do not include the command name 720 * since this is not an error of the specific command. */ 721 do_errthrow((struct condstack *)NULL, (char_u *)NULL); 722 msg_list = saved_msg_list; 723 #endif 724 return FAIL; 725 } 726 ++call_depth; 727 728 #ifdef FEAT_EVAL 729 cstack.cs_idx = -1; 730 cstack.cs_looplevel = 0; 731 cstack.cs_trylevel = 0; 732 cstack.cs_emsg_silent_list = NULL; 733 cstack.cs_lflags = 0; 734 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10); 735 736 real_cookie = getline_cookie(fgetline, cookie); 737 738 /* Inside a function use a higher nesting level. */ 739 getline_is_func = getline_equal(fgetline, cookie, get_func_line); 740 if (getline_is_func && ex_nesting_level == func_level(real_cookie)) 741 ++ex_nesting_level; 742 743 /* Get the function or script name and the address where the next breakpoint 744 * line and the debug tick for a function or script are stored. */ 745 if (getline_is_func) 746 { 747 fname = func_name(real_cookie); 748 breakpoint = func_breakpoint(real_cookie); 749 dbg_tick = func_dbg_tick(real_cookie); 750 } 751 else if (getline_equal(fgetline, cookie, getsourceline)) 752 { 753 fname = sourcing_name; 754 breakpoint = source_breakpoint(real_cookie); 755 dbg_tick = source_dbg_tick(real_cookie); 756 } 757 758 /* 759 * Initialize "force_abort" and "suppress_errthrow" at the top level. 760 */ 761 if (!recursive) 762 { 763 force_abort = FALSE; 764 suppress_errthrow = FALSE; 765 } 766 767 /* 768 * If requested, store and reset the global values controlling the 769 * exception handling (used when debugging). Otherwise clear it to avoid 770 * a bogus compiler warning when the optimizer uses inline functions... 771 */ 772 if (flags & DOCMD_EXCRESET) 773 save_dbg_stuff(&debug_saved); 774 else 775 vim_memset(&debug_saved, 0, sizeof(debug_saved)); 776 777 initial_trylevel = trylevel; 778 779 /* 780 * "did_throw" will be set to TRUE when an exception is being thrown. 781 */ 782 did_throw = FALSE; 783 #endif 784 /* 785 * "did_emsg" will be set to TRUE when emsg() is used, in which case we 786 * cancel the whole command line, and any if/endif or loop. 787 * If force_abort is set, we cancel everything. 788 */ 789 did_emsg = FALSE; 790 791 /* 792 * KeyTyped is only set when calling vgetc(). Reset it here when not 793 * calling vgetc() (sourced command lines). 794 */ 795 if (!(flags & DOCMD_KEYTYPED) 796 && !getline_equal(fgetline, cookie, getexline)) 797 KeyTyped = FALSE; 798 799 /* 800 * Continue executing command lines: 801 * - when inside an ":if", ":while" or ":for" 802 * - for multiple commands on one line, separated with '|' 803 * - when repeating until there are no more lines (for ":source") 804 */ 805 next_cmdline = cmdline; 806 do 807 { 808 #ifdef FEAT_EVAL 809 getline_is_func = getline_equal(fgetline, cookie, get_func_line); 810 #endif 811 812 /* stop skipping cmds for an error msg after all endif/while/for */ 813 if (next_cmdline == NULL 814 #ifdef FEAT_EVAL 815 && !force_abort 816 && cstack.cs_idx < 0 817 && !(getline_is_func && func_has_abort(real_cookie)) 818 #endif 819 ) 820 did_emsg = FALSE; 821 822 /* 823 * 1. If repeating a line in a loop, get a line from lines_ga. 824 * 2. If no line given: Get an allocated line with fgetline(). 825 * 3. If a line is given: Make a copy, so we can mess with it. 826 */ 827 828 #ifdef FEAT_EVAL 829 /* 1. If repeating, get a previous line from lines_ga. */ 830 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len) 831 { 832 /* Each '|' separated command is stored separately in lines_ga, to 833 * be able to jump to it. Don't use next_cmdline now. */ 834 VIM_CLEAR(cmdline_copy); 835 836 /* Check if a function has returned or, unless it has an unclosed 837 * try conditional, aborted. */ 838 if (getline_is_func) 839 { 840 # ifdef FEAT_PROFILE 841 if (do_profiling == PROF_YES) 842 func_line_end(real_cookie); 843 # endif 844 if (func_has_ended(real_cookie)) 845 { 846 retval = FAIL; 847 break; 848 } 849 } 850 #ifdef FEAT_PROFILE 851 else if (do_profiling == PROF_YES 852 && getline_equal(fgetline, cookie, getsourceline)) 853 script_line_end(); 854 #endif 855 856 /* Check if a sourced file hit a ":finish" command. */ 857 if (source_finished(fgetline, cookie)) 858 { 859 retval = FAIL; 860 break; 861 } 862 863 /* If breakpoints have been added/deleted need to check for it. */ 864 if (breakpoint != NULL && dbg_tick != NULL 865 && *dbg_tick != debug_tick) 866 { 867 *breakpoint = dbg_find_breakpoint( 868 getline_equal(fgetline, cookie, getsourceline), 869 fname, sourcing_lnum); 870 *dbg_tick = debug_tick; 871 } 872 873 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line; 874 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum; 875 876 /* Did we encounter a breakpoint? */ 877 if (breakpoint != NULL && *breakpoint != 0 878 && *breakpoint <= sourcing_lnum) 879 { 880 dbg_breakpoint(fname, sourcing_lnum); 881 /* Find next breakpoint. */ 882 *breakpoint = dbg_find_breakpoint( 883 getline_equal(fgetline, cookie, getsourceline), 884 fname, sourcing_lnum); 885 *dbg_tick = debug_tick; 886 } 887 # ifdef FEAT_PROFILE 888 if (do_profiling == PROF_YES) 889 { 890 if (getline_is_func) 891 func_line_start(real_cookie); 892 else if (getline_equal(fgetline, cookie, getsourceline)) 893 script_line_start(); 894 } 895 # endif 896 } 897 898 if (cstack.cs_looplevel > 0) 899 { 900 /* Inside a while/for loop we need to store the lines and use them 901 * again. Pass a different "fgetline" function to do_one_cmd() 902 * below, so that it stores lines in or reads them from 903 * "lines_ga". Makes it possible to define a function inside a 904 * while/for loop. */ 905 cmd_getline = get_loop_line; 906 cmd_cookie = (void *)&cmd_loop_cookie; 907 cmd_loop_cookie.lines_gap = &lines_ga; 908 cmd_loop_cookie.current_line = current_line; 909 cmd_loop_cookie.getline = fgetline; 910 cmd_loop_cookie.cookie = cookie; 911 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len); 912 } 913 else 914 { 915 cmd_getline = fgetline; 916 cmd_cookie = cookie; 917 } 918 #endif 919 920 /* 2. If no line given, get an allocated line with fgetline(). */ 921 if (next_cmdline == NULL) 922 { 923 /* 924 * Need to set msg_didout for the first line after an ":if", 925 * otherwise the ":if" will be overwritten. 926 */ 927 if (count == 1 && getline_equal(fgetline, cookie, getexline)) 928 msg_didout = TRUE; 929 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie, 930 #ifdef FEAT_EVAL 931 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2 932 #else 933 0 934 #endif 935 )) == NULL) 936 { 937 /* Don't call wait_return for aborted command line. The NULL 938 * returned for the end of a sourced file or executed function 939 * doesn't do this. */ 940 if (KeyTyped && !(flags & DOCMD_REPEAT)) 941 need_wait_return = FALSE; 942 retval = FAIL; 943 break; 944 } 945 used_getline = TRUE; 946 947 /* 948 * Keep the first typed line. Clear it when more lines are typed. 949 */ 950 if (flags & DOCMD_KEEPLINE) 951 { 952 vim_free(repeat_cmdline); 953 if (count == 0) 954 repeat_cmdline = vim_strsave(next_cmdline); 955 else 956 repeat_cmdline = NULL; 957 } 958 } 959 960 /* 3. Make a copy of the command so we can mess with it. */ 961 else if (cmdline_copy == NULL) 962 { 963 next_cmdline = vim_strsave(next_cmdline); 964 if (next_cmdline == NULL) 965 { 966 emsg(_(e_outofmem)); 967 retval = FAIL; 968 break; 969 } 970 } 971 cmdline_copy = next_cmdline; 972 973 #ifdef FEAT_EVAL 974 /* 975 * Save the current line when inside a ":while" or ":for", and when 976 * the command looks like a ":while" or ":for", because we may need it 977 * later. When there is a '|' and another command, it is stored 978 * separately, because we need to be able to jump back to it from an 979 * :endwhile/:endfor. 980 */ 981 if (current_line == lines_ga.ga_len 982 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline))) 983 { 984 if (store_loop_line(&lines_ga, next_cmdline) == FAIL) 985 { 986 retval = FAIL; 987 break; 988 } 989 } 990 did_endif = FALSE; 991 #endif 992 993 if (count++ == 0) 994 { 995 /* 996 * All output from the commands is put below each other, without 997 * waiting for a return. Don't do this when executing commands 998 * from a script or when being called recursive (e.g. for ":e 999 * +command file"). 1000 */ 1001 if (!(flags & DOCMD_NOWAIT) && !recursive) 1002 { 1003 msg_didout_before_start = msg_didout; 1004 msg_didany = FALSE; /* no output yet */ 1005 msg_start(); 1006 msg_scroll = TRUE; /* put messages below each other */ 1007 ++no_wait_return; /* don't wait for return until finished */ 1008 ++RedrawingDisabled; 1009 did_inc = TRUE; 1010 } 1011 } 1012 1013 if (p_verbose >= 15 && sourcing_name != NULL) 1014 { 1015 ++no_wait_return; 1016 verbose_enter_scroll(); 1017 1018 smsg(_("line %ld: %s"), 1019 (long)sourcing_lnum, cmdline_copy); 1020 if (msg_silent == 0) 1021 msg_puts("\n"); /* don't overwrite this */ 1022 1023 verbose_leave_scroll(); 1024 --no_wait_return; 1025 } 1026 1027 /* 1028 * 2. Execute one '|' separated command. 1029 * do_one_cmd() will return NULL if there is no trailing '|'. 1030 * "cmdline_copy" can change, e.g. for '%' and '#' expansion. 1031 */ 1032 ++recursive; 1033 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE, 1034 #ifdef FEAT_EVAL 1035 &cstack, 1036 #endif 1037 cmd_getline, cmd_cookie); 1038 --recursive; 1039 1040 #ifdef FEAT_EVAL 1041 if (cmd_cookie == (void *)&cmd_loop_cookie) 1042 /* Use "current_line" from "cmd_loop_cookie", it may have been 1043 * incremented when defining a function. */ 1044 current_line = cmd_loop_cookie.current_line; 1045 #endif 1046 1047 if (next_cmdline == NULL) 1048 { 1049 VIM_CLEAR(cmdline_copy); 1050 #ifdef FEAT_CMDHIST 1051 /* 1052 * If the command was typed, remember it for the ':' register. 1053 * Do this AFTER executing the command to make :@: work. 1054 */ 1055 if (getline_equal(fgetline, cookie, getexline) 1056 && new_last_cmdline != NULL) 1057 { 1058 vim_free(last_cmdline); 1059 last_cmdline = new_last_cmdline; 1060 new_last_cmdline = NULL; 1061 } 1062 #endif 1063 } 1064 else 1065 { 1066 /* need to copy the command after the '|' to cmdline_copy, for the 1067 * next do_one_cmd() */ 1068 STRMOVE(cmdline_copy, next_cmdline); 1069 next_cmdline = cmdline_copy; 1070 } 1071 1072 1073 #ifdef FEAT_EVAL 1074 /* reset did_emsg for a function that is not aborted by an error */ 1075 if (did_emsg && !force_abort 1076 && getline_equal(fgetline, cookie, get_func_line) 1077 && !func_has_abort(real_cookie)) 1078 did_emsg = FALSE; 1079 1080 if (cstack.cs_looplevel > 0) 1081 { 1082 ++current_line; 1083 1084 /* 1085 * An ":endwhile", ":endfor" and ":continue" is handled here. 1086 * If we were executing commands, jump back to the ":while" or 1087 * ":for". 1088 * If we were not executing commands, decrement cs_looplevel. 1089 */ 1090 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP)) 1091 { 1092 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP); 1093 1094 /* Jump back to the matching ":while" or ":for". Be careful 1095 * not to use a cs_line[] from an entry that isn't a ":while" 1096 * or ":for": It would make "current_line" invalid and can 1097 * cause a crash. */ 1098 if (!did_emsg && !got_int && !did_throw 1099 && cstack.cs_idx >= 0 1100 && (cstack.cs_flags[cstack.cs_idx] 1101 & (CSF_WHILE | CSF_FOR)) 1102 && cstack.cs_line[cstack.cs_idx] >= 0 1103 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE)) 1104 { 1105 current_line = cstack.cs_line[cstack.cs_idx]; 1106 /* remember we jumped there */ 1107 cstack.cs_lflags |= CSL_HAD_LOOP; 1108 line_breakcheck(); /* check if CTRL-C typed */ 1109 1110 /* Check for the next breakpoint at or after the ":while" 1111 * or ":for". */ 1112 if (breakpoint != NULL) 1113 { 1114 *breakpoint = dbg_find_breakpoint( 1115 getline_equal(fgetline, cookie, getsourceline), 1116 fname, 1117 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1); 1118 *dbg_tick = debug_tick; 1119 } 1120 } 1121 else 1122 { 1123 /* can only get here with ":endwhile" or ":endfor" */ 1124 if (cstack.cs_idx >= 0) 1125 rewind_conditionals(&cstack, cstack.cs_idx - 1, 1126 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel); 1127 } 1128 } 1129 1130 /* 1131 * For a ":while" or ":for" we need to remember the line number. 1132 */ 1133 else if (cstack.cs_lflags & CSL_HAD_LOOP) 1134 { 1135 cstack.cs_lflags &= ~CSL_HAD_LOOP; 1136 cstack.cs_line[cstack.cs_idx] = current_line - 1; 1137 } 1138 } 1139 1140 /* Check for the next breakpoint after a watchexpression */ 1141 if (breakpoint != NULL && has_watchexpr()) 1142 { 1143 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum); 1144 *dbg_tick = debug_tick; 1145 } 1146 1147 /* 1148 * When not inside any ":while" loop, clear remembered lines. 1149 */ 1150 if (cstack.cs_looplevel == 0) 1151 { 1152 if (lines_ga.ga_len > 0) 1153 { 1154 sourcing_lnum = 1155 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum; 1156 free_cmdlines(&lines_ga); 1157 } 1158 current_line = 0; 1159 } 1160 1161 /* 1162 * A ":finally" makes did_emsg, got_int, and did_throw pending for 1163 * being restored at the ":endtry". Reset them here and set the 1164 * ACTIVE and FINALLY flags, so that the finally clause gets executed. 1165 * This includes the case where a missing ":endif", ":endwhile" or 1166 * ":endfor" was detected by the ":finally" itself. 1167 */ 1168 if (cstack.cs_lflags & CSL_HAD_FINA) 1169 { 1170 cstack.cs_lflags &= ~CSL_HAD_FINA; 1171 report_make_pending(cstack.cs_pending[cstack.cs_idx] 1172 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW), 1173 did_throw ? (void *)current_exception : NULL); 1174 did_emsg = got_int = did_throw = FALSE; 1175 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY; 1176 } 1177 1178 /* Update global "trylevel" for recursive calls to do_cmdline() from 1179 * within this loop. */ 1180 trylevel = initial_trylevel + cstack.cs_trylevel; 1181 1182 /* 1183 * If the outermost try conditional (across function calls and sourced 1184 * files) is aborted because of an error, an interrupt, or an uncaught 1185 * exception, cancel everything. If it is left normally, reset 1186 * force_abort to get the non-EH compatible abortion behavior for 1187 * the rest of the script. 1188 */ 1189 if (trylevel == 0 && !did_emsg && !got_int && !did_throw) 1190 force_abort = FALSE; 1191 1192 /* Convert an interrupt to an exception if appropriate. */ 1193 (void)do_intthrow(&cstack); 1194 #endif /* FEAT_EVAL */ 1195 1196 } 1197 /* 1198 * Continue executing command lines when: 1199 * - no CTRL-C typed, no aborting error, no exception thrown or try 1200 * conditionals need to be checked for executing finally clauses or 1201 * catching an interrupt exception 1202 * - didn't get an error message or lines are not typed 1203 * - there is a command after '|', inside a :if, :while, :for or :try, or 1204 * looping for ":source" command or function call. 1205 */ 1206 while (!((got_int 1207 #ifdef FEAT_EVAL 1208 || (did_emsg && force_abort) || did_throw 1209 #endif 1210 ) 1211 #ifdef FEAT_EVAL 1212 && cstack.cs_trylevel == 0 1213 #endif 1214 ) 1215 && !(did_emsg 1216 #ifdef FEAT_EVAL 1217 /* Keep going when inside try/catch, so that the error can be 1218 * deal with, except when it is a syntax error, it may cause 1219 * the :endtry to be missed. */ 1220 && (cstack.cs_trylevel == 0 || did_emsg_syntax) 1221 #endif 1222 && used_getline 1223 && (getline_equal(fgetline, cookie, getexmodeline) 1224 || getline_equal(fgetline, cookie, getexline))) 1225 && (next_cmdline != NULL 1226 #ifdef FEAT_EVAL 1227 || cstack.cs_idx >= 0 1228 #endif 1229 || (flags & DOCMD_REPEAT))); 1230 1231 vim_free(cmdline_copy); 1232 did_emsg_syntax = FALSE; 1233 #ifdef FEAT_EVAL 1234 free_cmdlines(&lines_ga); 1235 ga_clear(&lines_ga); 1236 1237 if (cstack.cs_idx >= 0) 1238 { 1239 /* 1240 * If a sourced file or executed function ran to its end, report the 1241 * unclosed conditional. 1242 */ 1243 if (!got_int && !did_throw 1244 && ((getline_equal(fgetline, cookie, getsourceline) 1245 && !source_finished(fgetline, cookie)) 1246 || (getline_equal(fgetline, cookie, get_func_line) 1247 && !func_has_ended(real_cookie)))) 1248 { 1249 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY) 1250 emsg(_(e_endtry)); 1251 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE) 1252 emsg(_(e_endwhile)); 1253 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR) 1254 emsg(_(e_endfor)); 1255 else 1256 emsg(_(e_endif)); 1257 } 1258 1259 /* 1260 * Reset "trylevel" in case of a ":finish" or ":return" or a missing 1261 * ":endtry" in a sourced file or executed function. If the try 1262 * conditional is in its finally clause, ignore anything pending. 1263 * If it is in a catch clause, finish the caught exception. 1264 * Also cleanup any "cs_forinfo" structures. 1265 */ 1266 do 1267 { 1268 int idx = cleanup_conditionals(&cstack, 0, TRUE); 1269 1270 if (idx >= 0) 1271 --idx; /* remove try block not in its finally clause */ 1272 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR, 1273 &cstack.cs_looplevel); 1274 } 1275 while (cstack.cs_idx >= 0); 1276 trylevel = initial_trylevel; 1277 } 1278 1279 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory 1280 * lack was reported above and the error message is to be converted to an 1281 * exception, do this now after rewinding the cstack. */ 1282 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line) 1283 ? (char_u *)"endfunction" : (char_u *)NULL); 1284 1285 if (trylevel == 0) 1286 { 1287 /* 1288 * When an exception is being thrown out of the outermost try 1289 * conditional, discard the uncaught exception, disable the conversion 1290 * of interrupts or errors to exceptions, and ensure that no more 1291 * commands are executed. 1292 */ 1293 if (did_throw) 1294 { 1295 void *p = NULL; 1296 char_u *saved_sourcing_name; 1297 int saved_sourcing_lnum; 1298 struct msglist *messages = NULL, *next; 1299 1300 /* 1301 * If the uncaught exception is a user exception, report it as an 1302 * error. If it is an error exception, display the saved error 1303 * message now. For an interrupt exception, do nothing; the 1304 * interrupt message is given elsewhere. 1305 */ 1306 switch (current_exception->type) 1307 { 1308 case ET_USER: 1309 vim_snprintf((char *)IObuff, IOSIZE, 1310 _("E605: Exception not caught: %s"), 1311 current_exception->value); 1312 p = vim_strsave(IObuff); 1313 break; 1314 case ET_ERROR: 1315 messages = current_exception->messages; 1316 current_exception->messages = NULL; 1317 break; 1318 case ET_INTERRUPT: 1319 break; 1320 } 1321 1322 saved_sourcing_name = sourcing_name; 1323 saved_sourcing_lnum = sourcing_lnum; 1324 sourcing_name = current_exception->throw_name; 1325 sourcing_lnum = current_exception->throw_lnum; 1326 current_exception->throw_name = NULL; 1327 1328 discard_current_exception(); /* uses IObuff if 'verbose' */ 1329 suppress_errthrow = TRUE; 1330 force_abort = TRUE; 1331 1332 if (messages != NULL) 1333 { 1334 do 1335 { 1336 next = messages->next; 1337 emsg(messages->msg); 1338 vim_free(messages->msg); 1339 vim_free(messages); 1340 messages = next; 1341 } 1342 while (messages != NULL); 1343 } 1344 else if (p != NULL) 1345 { 1346 emsg(p); 1347 vim_free(p); 1348 } 1349 vim_free(sourcing_name); 1350 sourcing_name = saved_sourcing_name; 1351 sourcing_lnum = saved_sourcing_lnum; 1352 } 1353 1354 /* 1355 * On an interrupt or an aborting error not converted to an exception, 1356 * disable the conversion of errors to exceptions. (Interrupts are not 1357 * converted any more, here.) This enables also the interrupt message 1358 * when force_abort is set and did_emsg unset in case of an interrupt 1359 * from a finally clause after an error. 1360 */ 1361 else if (got_int || (did_emsg && force_abort)) 1362 suppress_errthrow = TRUE; 1363 } 1364 1365 /* 1366 * The current cstack will be freed when do_cmdline() returns. An uncaught 1367 * exception will have to be rethrown in the previous cstack. If a function 1368 * has just returned or a script file was just finished and the previous 1369 * cstack belongs to the same function or, respectively, script file, it 1370 * will have to be checked for finally clauses to be executed due to the 1371 * ":return" or ":finish". This is done in do_one_cmd(). 1372 */ 1373 if (did_throw) 1374 need_rethrow = TRUE; 1375 if ((getline_equal(fgetline, cookie, getsourceline) 1376 && ex_nesting_level > source_level(real_cookie)) 1377 || (getline_equal(fgetline, cookie, get_func_line) 1378 && ex_nesting_level > func_level(real_cookie) + 1)) 1379 { 1380 if (!did_throw) 1381 check_cstack = TRUE; 1382 } 1383 else 1384 { 1385 /* When leaving a function, reduce nesting level. */ 1386 if (getline_equal(fgetline, cookie, get_func_line)) 1387 --ex_nesting_level; 1388 /* 1389 * Go to debug mode when returning from a function in which we are 1390 * single-stepping. 1391 */ 1392 if ((getline_equal(fgetline, cookie, getsourceline) 1393 || getline_equal(fgetline, cookie, get_func_line)) 1394 && ex_nesting_level + 1 <= debug_break_level) 1395 do_debug(getline_equal(fgetline, cookie, getsourceline) 1396 ? (char_u *)_("End of sourced file") 1397 : (char_u *)_("End of function")); 1398 } 1399 1400 /* 1401 * Restore the exception environment (done after returning from the 1402 * debugger). 1403 */ 1404 if (flags & DOCMD_EXCRESET) 1405 restore_dbg_stuff(&debug_saved); 1406 1407 msg_list = saved_msg_list; 1408 #endif /* FEAT_EVAL */ 1409 1410 /* 1411 * If there was too much output to fit on the command line, ask the user to 1412 * hit return before redrawing the screen. With the ":global" command we do 1413 * this only once after the command is finished. 1414 */ 1415 if (did_inc) 1416 { 1417 --RedrawingDisabled; 1418 --no_wait_return; 1419 msg_scroll = FALSE; 1420 1421 /* 1422 * When just finished an ":if"-":else" which was typed, no need to 1423 * wait for hit-return. Also for an error situation. 1424 */ 1425 if (retval == FAIL 1426 #ifdef FEAT_EVAL 1427 || (did_endif && KeyTyped && !did_emsg) 1428 #endif 1429 ) 1430 { 1431 need_wait_return = FALSE; 1432 msg_didany = FALSE; /* don't wait when restarting edit */ 1433 } 1434 else if (need_wait_return) 1435 { 1436 /* 1437 * The msg_start() above clears msg_didout. The wait_return we do 1438 * here should not overwrite the command that may be shown before 1439 * doing that. 1440 */ 1441 msg_didout |= msg_didout_before_start; 1442 wait_return(FALSE); 1443 } 1444 } 1445 1446 #ifdef FEAT_EVAL 1447 did_endif = FALSE; /* in case do_cmdline used recursively */ 1448 #else 1449 /* 1450 * Reset if_level, in case a sourced script file contains more ":if" than 1451 * ":endif" (could be ":if x | foo | endif"). 1452 */ 1453 if_level = 0; 1454 #endif 1455 1456 --call_depth; 1457 return retval; 1458 } 1459 1460 #ifdef FEAT_EVAL 1461 /* 1462 * Obtain a line when inside a ":while" or ":for" loop. 1463 */ 1464 static char_u * 1465 get_loop_line(int c, void *cookie, int indent) 1466 { 1467 struct loop_cookie *cp = (struct loop_cookie *)cookie; 1468 wcmd_T *wp; 1469 char_u *line; 1470 1471 if (cp->current_line + 1 >= cp->lines_gap->ga_len) 1472 { 1473 if (cp->repeating) 1474 return NULL; /* trying to read past ":endwhile"/":endfor" */ 1475 1476 /* First time inside the ":while"/":for": get line normally. */ 1477 if (cp->getline == NULL) 1478 line = getcmdline(c, 0L, indent); 1479 else 1480 line = cp->getline(c, cp->cookie, indent); 1481 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK) 1482 ++cp->current_line; 1483 1484 return line; 1485 } 1486 1487 KeyTyped = FALSE; 1488 ++cp->current_line; 1489 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line; 1490 sourcing_lnum = wp->lnum; 1491 return vim_strsave(wp->line); 1492 } 1493 1494 /* 1495 * Store a line in "gap" so that a ":while" loop can execute it again. 1496 */ 1497 static int 1498 store_loop_line(garray_T *gap, char_u *line) 1499 { 1500 if (ga_grow(gap, 1) == FAIL) 1501 return FAIL; 1502 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line); 1503 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum; 1504 ++gap->ga_len; 1505 return OK; 1506 } 1507 1508 /* 1509 * Free the lines stored for a ":while" or ":for" loop. 1510 */ 1511 static void 1512 free_cmdlines(garray_T *gap) 1513 { 1514 while (gap->ga_len > 0) 1515 { 1516 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line); 1517 --gap->ga_len; 1518 } 1519 } 1520 #endif 1521 1522 /* 1523 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals 1524 * "func". * Otherwise return TRUE when "fgetline" equals "func". 1525 */ 1526 int 1527 getline_equal( 1528 char_u *(*fgetline)(int, void *, int), 1529 void *cookie UNUSED, /* argument for fgetline() */ 1530 char_u *(*func)(int, void *, int)) 1531 { 1532 #ifdef FEAT_EVAL 1533 char_u *(*gp)(int, void *, int); 1534 struct loop_cookie *cp; 1535 1536 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the 1537 * function that's originally used to obtain the lines. This may be 1538 * nested several levels. */ 1539 gp = fgetline; 1540 cp = (struct loop_cookie *)cookie; 1541 while (gp == get_loop_line) 1542 { 1543 gp = cp->getline; 1544 cp = cp->cookie; 1545 } 1546 return gp == func; 1547 #else 1548 return fgetline == func; 1549 #endif 1550 } 1551 1552 /* 1553 * If "fgetline" is get_loop_line(), return the cookie used by the original 1554 * getline function. Otherwise return "cookie". 1555 */ 1556 void * 1557 getline_cookie( 1558 char_u *(*fgetline)(int, void *, int) UNUSED, 1559 void *cookie) /* argument for fgetline() */ 1560 { 1561 #ifdef FEAT_EVAL 1562 char_u *(*gp)(int, void *, int); 1563 struct loop_cookie *cp; 1564 1565 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the 1566 * cookie that's originally used to obtain the lines. This may be nested 1567 * several levels. */ 1568 gp = fgetline; 1569 cp = (struct loop_cookie *)cookie; 1570 while (gp == get_loop_line) 1571 { 1572 gp = cp->getline; 1573 cp = cp->cookie; 1574 } 1575 return cp; 1576 #else 1577 return cookie; 1578 #endif 1579 } 1580 1581 1582 /* 1583 * Helper function to apply an offset for buffer commands, i.e. ":bdelete", 1584 * ":bwipeout", etc. 1585 * Returns the buffer number. 1586 */ 1587 static int 1588 compute_buffer_local_count(int addr_type, int lnum, int offset) 1589 { 1590 buf_T *buf; 1591 buf_T *nextbuf; 1592 int count = offset; 1593 1594 buf = firstbuf; 1595 while (buf->b_next != NULL && buf->b_fnum < lnum) 1596 buf = buf->b_next; 1597 while (count != 0) 1598 { 1599 count += (offset < 0) ? 1 : -1; 1600 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next; 1601 if (nextbuf == NULL) 1602 break; 1603 buf = nextbuf; 1604 if (addr_type == ADDR_LOADED_BUFFERS) 1605 /* skip over unloaded buffers */ 1606 while (buf->b_ml.ml_mfp == NULL) 1607 { 1608 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next; 1609 if (nextbuf == NULL) 1610 break; 1611 buf = nextbuf; 1612 } 1613 } 1614 /* we might have gone too far, last buffer is not loadedd */ 1615 if (addr_type == ADDR_LOADED_BUFFERS) 1616 while (buf->b_ml.ml_mfp == NULL) 1617 { 1618 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next; 1619 if (nextbuf == NULL) 1620 break; 1621 buf = nextbuf; 1622 } 1623 return buf->b_fnum; 1624 } 1625 1626 static int 1627 current_win_nr(win_T *win) 1628 { 1629 win_T *wp; 1630 int nr = 0; 1631 1632 FOR_ALL_WINDOWS(wp) 1633 { 1634 ++nr; 1635 if (wp == win) 1636 break; 1637 } 1638 return nr; 1639 } 1640 1641 static int 1642 current_tab_nr(tabpage_T *tab) 1643 { 1644 tabpage_T *tp; 1645 int nr = 0; 1646 1647 FOR_ALL_TABPAGES(tp) 1648 { 1649 ++nr; 1650 if (tp == tab) 1651 break; 1652 } 1653 return nr; 1654 } 1655 1656 # define CURRENT_WIN_NR current_win_nr(curwin) 1657 # define LAST_WIN_NR current_win_nr(NULL) 1658 # define CURRENT_TAB_NR current_tab_nr(curtab) 1659 # define LAST_TAB_NR current_tab_nr(NULL) 1660 1661 /* 1662 * Execute one Ex command. 1663 * 1664 * If 'sourcing' is TRUE, the command will be included in the error message. 1665 * 1666 * 1. skip comment lines and leading space 1667 * 2. handle command modifiers 1668 * 3. find the command 1669 * 4. parse range 1670 * 5. Parse the command. 1671 * 6. parse arguments 1672 * 7. switch on command name 1673 * 1674 * Note: "fgetline" can be NULL. 1675 * 1676 * This function may be called recursively! 1677 */ 1678 #if (_MSC_VER == 1200) 1679 /* 1680 * Avoid optimisation bug in VC++ version 6.0 1681 */ 1682 #pragma optimize( "g", off ) 1683 #endif 1684 static char_u * 1685 do_one_cmd( 1686 char_u **cmdlinep, 1687 int sourcing, 1688 #ifdef FEAT_EVAL 1689 struct condstack *cstack, 1690 #endif 1691 char_u *(*fgetline)(int, void *, int), 1692 void *cookie) /* argument for fgetline() */ 1693 { 1694 char_u *p; 1695 linenr_T lnum; 1696 long n; 1697 char *errormsg = NULL; /* error message */ 1698 char_u *after_modifier = NULL; 1699 exarg_T ea; /* Ex command arguments */ 1700 int save_msg_scroll = msg_scroll; 1701 cmdmod_T save_cmdmod; 1702 int ni; /* set when Not Implemented */ 1703 char_u *cmd; 1704 1705 vim_memset(&ea, 0, sizeof(ea)); 1706 ea.line1 = 1; 1707 ea.line2 = 1; 1708 #ifdef FEAT_EVAL 1709 ++ex_nesting_level; 1710 #endif 1711 1712 /* When the last file has not been edited :q has to be typed twice. */ 1713 if (quitmore 1714 #ifdef FEAT_EVAL 1715 /* avoid that a function call in 'statusline' does this */ 1716 && !getline_equal(fgetline, cookie, get_func_line) 1717 #endif 1718 /* avoid that an autocommand, e.g. QuitPre, does this */ 1719 && !getline_equal(fgetline, cookie, getnextac)) 1720 --quitmore; 1721 1722 /* 1723 * Reset browse, confirm, etc.. They are restored when returning, for 1724 * recursive calls. 1725 */ 1726 save_cmdmod = cmdmod; 1727 1728 /* "#!anything" is handled like a comment. */ 1729 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!') 1730 goto doend; 1731 1732 /* 1733 * 1. Skip comment lines and leading white space and colons. 1734 * 2. Handle command modifiers. 1735 */ 1736 // The "ea" structure holds the arguments that can be used. 1737 ea.cmd = *cmdlinep; 1738 ea.cmdlinep = cmdlinep; 1739 ea.getline = fgetline; 1740 ea.cookie = cookie; 1741 #ifdef FEAT_EVAL 1742 ea.cstack = cstack; 1743 #endif 1744 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) 1745 goto doend; 1746 1747 after_modifier = ea.cmd; 1748 1749 #ifdef FEAT_EVAL 1750 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0 1751 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)); 1752 #else 1753 ea.skip = (if_level > 0); 1754 #endif 1755 1756 /* 1757 * 3. Skip over the range to find the command. Let "p" point to after it. 1758 * 1759 * We need the command to know what kind of range it uses. 1760 */ 1761 cmd = ea.cmd; 1762 ea.cmd = skip_range(ea.cmd, NULL); 1763 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL) 1764 ea.cmd = skipwhite(ea.cmd + 1); 1765 p = find_command(&ea, NULL); 1766 1767 #ifdef FEAT_EVAL 1768 # ifdef FEAT_PROFILE 1769 // Count this line for profiling if skip is TRUE. 1770 if (do_profiling == PROF_YES 1771 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0 1772 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE)))) 1773 { 1774 int skip = did_emsg || got_int || did_throw; 1775 1776 if (ea.cmdidx == CMD_catch) 1777 skip = !skip && !(cstack->cs_idx >= 0 1778 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN) 1779 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT)); 1780 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif) 1781 skip = skip || !(cstack->cs_idx >= 0 1782 && !(cstack->cs_flags[cstack->cs_idx] 1783 & (CSF_ACTIVE | CSF_TRUE))); 1784 else if (ea.cmdidx == CMD_finally) 1785 skip = FALSE; 1786 else if (ea.cmdidx != CMD_endif 1787 && ea.cmdidx != CMD_endfor 1788 && ea.cmdidx != CMD_endtry 1789 && ea.cmdidx != CMD_endwhile) 1790 skip = ea.skip; 1791 1792 if (!skip) 1793 { 1794 if (getline_equal(fgetline, cookie, get_func_line)) 1795 func_line_exec(getline_cookie(fgetline, cookie)); 1796 else if (getline_equal(fgetline, cookie, getsourceline)) 1797 script_line_exec(); 1798 } 1799 } 1800 # endif 1801 1802 /* May go to debug mode. If this happens and the ">quit" debug command is 1803 * used, throw an interrupt exception and skip the next command. */ 1804 dbg_check_breakpoint(&ea); 1805 if (!ea.skip && got_int) 1806 { 1807 ea.skip = TRUE; 1808 (void)do_intthrow(cstack); 1809 } 1810 #endif 1811 1812 /* 1813 * 4. parse a range specifier of the form: addr [,addr] [;addr] .. 1814 * 1815 * where 'addr' is: 1816 * 1817 * % (entire file) 1818 * $ [+-NUM] 1819 * 'x [+-NUM] (where x denotes a currently defined mark) 1820 * . [+-NUM] 1821 * [+-NUM].. 1822 * NUM 1823 * 1824 * The ea.cmd pointer is updated to point to the first character following the 1825 * range spec. If an initial address is found, but no second, the upper bound 1826 * is equal to the lower. 1827 */ 1828 1829 /* ea.addr_type for user commands is set by find_ucmd */ 1830 if (!IS_USER_CMDIDX(ea.cmdidx)) 1831 { 1832 if (ea.cmdidx != CMD_SIZE) 1833 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type; 1834 else 1835 ea.addr_type = ADDR_LINES; 1836 1837 /* :wincmd range depends on the argument. */ 1838 if (ea.cmdidx == CMD_wincmd && p != NULL) 1839 get_wincmd_addr_type(skipwhite(p), &ea); 1840 } 1841 1842 ea.cmd = cmd; 1843 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL) 1844 goto doend; 1845 1846 /* 1847 * 5. Parse the command. 1848 */ 1849 1850 /* 1851 * Skip ':' and any white space 1852 */ 1853 ea.cmd = skipwhite(ea.cmd); 1854 while (*ea.cmd == ':') 1855 ea.cmd = skipwhite(ea.cmd + 1); 1856 1857 /* 1858 * If we got a line, but no command, then go to the line. 1859 * If we find a '|' or '\n' we set ea.nextcmd. 1860 */ 1861 if (*ea.cmd == NUL || *ea.cmd == '"' 1862 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL) 1863 { 1864 /* 1865 * strange vi behaviour: 1866 * ":3" jumps to line 3 1867 * ":3|..." prints line 3 1868 * ":|" prints current line 1869 */ 1870 if (ea.skip) /* skip this if inside :if */ 1871 goto doend; 1872 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2)) 1873 { 1874 ea.cmdidx = CMD_print; 1875 ea.argt = RANGE+COUNT+TRLBAR; 1876 if ((errormsg = invalid_range(&ea)) == NULL) 1877 { 1878 correct_range(&ea); 1879 ex_print(&ea); 1880 } 1881 } 1882 else if (ea.addr_count != 0) 1883 { 1884 if (ea.line2 > curbuf->b_ml.ml_line_count) 1885 { 1886 /* With '-' in 'cpoptions' a line number past the file is an 1887 * error, otherwise put it at the end of the file. */ 1888 if (vim_strchr(p_cpo, CPO_MINUS) != NULL) 1889 ea.line2 = -1; 1890 else 1891 ea.line2 = curbuf->b_ml.ml_line_count; 1892 } 1893 1894 if (ea.line2 < 0) 1895 errormsg = _(e_invrange); 1896 else 1897 { 1898 if (ea.line2 == 0) 1899 curwin->w_cursor.lnum = 1; 1900 else 1901 curwin->w_cursor.lnum = ea.line2; 1902 beginline(BL_SOL | BL_FIX); 1903 } 1904 } 1905 goto doend; 1906 } 1907 1908 /* If this looks like an undefined user command and there are CmdUndefined 1909 * autocommands defined, trigger the matching autocommands. */ 1910 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip 1911 && ASCII_ISUPPER(*ea.cmd) 1912 && has_cmdundefined()) 1913 { 1914 int ret; 1915 1916 p = ea.cmd; 1917 while (ASCII_ISALNUM(*p)) 1918 ++p; 1919 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd)); 1920 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL); 1921 vim_free(p); 1922 /* If the autocommands did something and didn't cause an error, try 1923 * finding the command again. */ 1924 p = (ret 1925 #ifdef FEAT_EVAL 1926 && !aborting() 1927 #endif 1928 ) ? find_command(&ea, NULL) : ea.cmd; 1929 } 1930 1931 #ifdef FEAT_USR_CMDS 1932 if (p == NULL) 1933 { 1934 if (!ea.skip) 1935 errormsg = _("E464: Ambiguous use of user-defined command"); 1936 goto doend; 1937 } 1938 /* Check for wrong commands. */ 1939 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78 1940 && !IS_USER_CMDIDX(ea.cmdidx)) 1941 { 1942 errormsg = uc_fun_cmd(); 1943 goto doend; 1944 } 1945 #endif 1946 if (ea.cmdidx == CMD_SIZE) 1947 { 1948 if (!ea.skip) 1949 { 1950 STRCPY(IObuff, _("E492: Not an editor command")); 1951 if (!sourcing) 1952 { 1953 /* If the modifier was parsed OK the error must be in the 1954 * following command */ 1955 if (after_modifier != NULL) 1956 append_command(after_modifier); 1957 else 1958 append_command(*cmdlinep); 1959 } 1960 errormsg = (char *)IObuff; 1961 did_emsg_syntax = TRUE; 1962 } 1963 goto doend; 1964 } 1965 1966 ni = (!IS_USER_CMDIDX(ea.cmdidx) 1967 && (cmdnames[ea.cmdidx].cmd_func == ex_ni 1968 #ifdef HAVE_EX_SCRIPT_NI 1969 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni 1970 #endif 1971 )); 1972 1973 #ifndef FEAT_EVAL 1974 /* 1975 * When the expression evaluation is disabled, recognize the ":if" and 1976 * ":endif" commands and ignore everything in between it. 1977 */ 1978 if (ea.cmdidx == CMD_if) 1979 ++if_level; 1980 if (if_level) 1981 { 1982 if (ea.cmdidx == CMD_endif) 1983 --if_level; 1984 goto doend; 1985 } 1986 1987 #endif 1988 1989 /* forced commands */ 1990 if (*p == '!' && ea.cmdidx != CMD_substitute 1991 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic) 1992 { 1993 ++p; 1994 ea.forceit = TRUE; 1995 } 1996 else 1997 ea.forceit = FALSE; 1998 1999 /* 2000 * 6. Parse arguments. 2001 */ 2002 if (!IS_USER_CMDIDX(ea.cmdidx)) 2003 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt; 2004 2005 if (!ea.skip) 2006 { 2007 #ifdef HAVE_SANDBOX 2008 if (sandbox != 0 && !(ea.argt & SBOXOK)) 2009 { 2010 // Command not allowed in sandbox. 2011 errormsg = _(e_sandbox); 2012 goto doend; 2013 } 2014 #endif 2015 if (restricted != 0 && (ea.argt & RESTRICT)) 2016 { 2017 errormsg = _("E981: Command not allowed in rvim"); 2018 goto doend; 2019 } 2020 if (!curbuf->b_p_ma && (ea.argt & MODIFY)) 2021 { 2022 /* Command not allowed in non-'modifiable' buffer */ 2023 errormsg = _(e_modifiable); 2024 goto doend; 2025 } 2026 2027 if (text_locked() && !(ea.argt & CMDWIN) 2028 && !IS_USER_CMDIDX(ea.cmdidx)) 2029 { 2030 /* Command not allowed when editing the command line. */ 2031 errormsg = _(get_text_locked_msg()); 2032 goto doend; 2033 } 2034 2035 /* Disallow editing another buffer when "curbuf_lock" is set. 2036 * Do allow ":checktime" (it is postponed). 2037 * Do allow ":edit" (check for an argument later). 2038 * Do allow ":file" with no arguments (check for an argument later). */ 2039 if (!(ea.argt & CMDWIN) 2040 && ea.cmdidx != CMD_checktime 2041 && ea.cmdidx != CMD_edit 2042 && ea.cmdidx != CMD_file 2043 && !IS_USER_CMDIDX(ea.cmdidx) 2044 && curbuf_locked()) 2045 goto doend; 2046 2047 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0) 2048 { 2049 /* no range allowed */ 2050 errormsg = _(e_norange); 2051 goto doend; 2052 } 2053 } 2054 2055 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */ 2056 { 2057 errormsg = _(e_nobang); 2058 goto doend; 2059 } 2060 2061 /* 2062 * Don't complain about the range if it is not used 2063 * (could happen if line_count is accidentally set to 0). 2064 */ 2065 if (!ea.skip && !ni) 2066 { 2067 /* 2068 * If the range is backwards, ask for confirmation and, if given, swap 2069 * ea.line1 & ea.line2 so it's forwards again. 2070 * When global command is busy, don't ask, will fail below. 2071 */ 2072 if (!global_busy && ea.line1 > ea.line2) 2073 { 2074 if (msg_silent == 0) 2075 { 2076 if (sourcing || exmode_active) 2077 { 2078 errormsg = _("E493: Backwards range given"); 2079 goto doend; 2080 } 2081 if (ask_yesno((char_u *) 2082 _("Backwards range given, OK to swap"), FALSE) != 'y') 2083 goto doend; 2084 } 2085 lnum = ea.line1; 2086 ea.line1 = ea.line2; 2087 ea.line2 = lnum; 2088 } 2089 if ((errormsg = invalid_range(&ea)) != NULL) 2090 goto doend; 2091 } 2092 2093 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */ 2094 ea.line2 = 1; 2095 2096 correct_range(&ea); 2097 2098 #ifdef FEAT_FOLDING 2099 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy 2100 && ea.addr_type == ADDR_LINES) 2101 { 2102 /* Put the first line at the start of a closed fold, put the last line 2103 * at the end of a closed fold. */ 2104 (void)hasFolding(ea.line1, &ea.line1, NULL); 2105 (void)hasFolding(ea.line2, NULL, &ea.line2); 2106 } 2107 #endif 2108 2109 #ifdef FEAT_QUICKFIX 2110 /* 2111 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg' 2112 * option here, so things like % get expanded. 2113 */ 2114 p = replace_makeprg(&ea, p, cmdlinep); 2115 if (p == NULL) 2116 goto doend; 2117 #endif 2118 2119 /* 2120 * Skip to start of argument. 2121 * Don't do this for the ":!" command, because ":!! -l" needs the space. 2122 */ 2123 if (ea.cmdidx == CMD_bang) 2124 ea.arg = p; 2125 else 2126 ea.arg = skipwhite(p); 2127 2128 // ":file" cannot be run with an argument when "curbuf_lock" is set 2129 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked()) 2130 goto doend; 2131 2132 /* 2133 * Check for "++opt=val" argument. 2134 * Must be first, allow ":w ++enc=utf8 !cmd" 2135 */ 2136 if (ea.argt & ARGOPT) 2137 while (ea.arg[0] == '+' && ea.arg[1] == '+') 2138 if (getargopt(&ea) == FAIL && !ni) 2139 { 2140 errormsg = _(e_invarg); 2141 goto doend; 2142 } 2143 2144 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update) 2145 { 2146 if (*ea.arg == '>') /* append */ 2147 { 2148 if (*++ea.arg != '>') /* typed wrong */ 2149 { 2150 errormsg = _("E494: Use w or w>>"); 2151 goto doend; 2152 } 2153 ea.arg = skipwhite(ea.arg + 1); 2154 ea.append = TRUE; 2155 } 2156 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */ 2157 { 2158 ++ea.arg; 2159 ea.usefilter = TRUE; 2160 } 2161 } 2162 2163 if (ea.cmdidx == CMD_read) 2164 { 2165 if (ea.forceit) 2166 { 2167 ea.usefilter = TRUE; /* :r! filter if ea.forceit */ 2168 ea.forceit = FALSE; 2169 } 2170 else if (*ea.arg == '!') /* :r !filter */ 2171 { 2172 ++ea.arg; 2173 ea.usefilter = TRUE; 2174 } 2175 } 2176 2177 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift) 2178 { 2179 ea.amount = 1; 2180 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */ 2181 { 2182 ++ea.arg; 2183 ++ea.amount; 2184 } 2185 ea.arg = skipwhite(ea.arg); 2186 } 2187 2188 /* 2189 * Check for "+command" argument, before checking for next command. 2190 * Don't do this for ":read !cmd" and ":write !cmd". 2191 */ 2192 if ((ea.argt & EDITCMD) && !ea.usefilter) 2193 ea.do_ecmd_cmd = getargcmd(&ea.arg); 2194 2195 /* 2196 * Check for '|' to separate commands and '"' to start comments. 2197 * Don't do this for ":read !cmd" and ":write !cmd". 2198 */ 2199 if ((ea.argt & TRLBAR) && !ea.usefilter) 2200 separate_nextcmd(&ea); 2201 2202 /* 2203 * Check for <newline> to end a shell command. 2204 * Also do this for ":read !cmd", ":write !cmd" and ":global". 2205 * Any others? 2206 */ 2207 else if (ea.cmdidx == CMD_bang 2208 || ea.cmdidx == CMD_terminal 2209 || ea.cmdidx == CMD_global 2210 || ea.cmdidx == CMD_vglobal 2211 || ea.usefilter) 2212 { 2213 for (p = ea.arg; *p; ++p) 2214 { 2215 /* Remove one backslash before a newline, so that it's possible to 2216 * pass a newline to the shell and also a newline that is preceded 2217 * with a backslash. This makes it impossible to end a shell 2218 * command in a backslash, but that doesn't appear useful. 2219 * Halving the number of backslashes is incompatible with previous 2220 * versions. */ 2221 if (*p == '\\' && p[1] == '\n') 2222 STRMOVE(p, p + 1); 2223 else if (*p == '\n') 2224 { 2225 ea.nextcmd = p + 1; 2226 *p = NUL; 2227 break; 2228 } 2229 } 2230 } 2231 2232 if ((ea.argt & DFLALL) && ea.addr_count == 0) 2233 { 2234 buf_T *buf; 2235 2236 ea.line1 = 1; 2237 switch (ea.addr_type) 2238 { 2239 case ADDR_LINES: 2240 ea.line2 = curbuf->b_ml.ml_line_count; 2241 break; 2242 case ADDR_LOADED_BUFFERS: 2243 buf = firstbuf; 2244 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL) 2245 buf = buf->b_next; 2246 ea.line1 = buf->b_fnum; 2247 buf = lastbuf; 2248 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL) 2249 buf = buf->b_prev; 2250 ea.line2 = buf->b_fnum; 2251 break; 2252 case ADDR_BUFFERS: 2253 ea.line1 = firstbuf->b_fnum; 2254 ea.line2 = lastbuf->b_fnum; 2255 break; 2256 case ADDR_WINDOWS: 2257 ea.line2 = LAST_WIN_NR; 2258 break; 2259 case ADDR_TABS: 2260 ea.line2 = LAST_TAB_NR; 2261 break; 2262 case ADDR_TABS_RELATIVE: 2263 ea.line2 = 1; 2264 break; 2265 case ADDR_ARGUMENTS: 2266 if (ARGCOUNT == 0) 2267 ea.line1 = ea.line2 = 0; 2268 else 2269 ea.line2 = ARGCOUNT; 2270 break; 2271 #ifdef FEAT_QUICKFIX 2272 case ADDR_QUICKFIX: 2273 ea.line2 = qf_get_size(&ea); 2274 if (ea.line2 == 0) 2275 ea.line2 = 1; 2276 break; 2277 #endif 2278 } 2279 } 2280 2281 /* accept numbered register only when no count allowed (:put) */ 2282 if ( (ea.argt & REGSTR) 2283 && *ea.arg != NUL 2284 /* Do not allow register = for user commands */ 2285 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=') 2286 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg))) 2287 { 2288 #ifndef FEAT_CLIPBOARD 2289 /* check these explicitly for a more specific error message */ 2290 if (*ea.arg == '*' || *ea.arg == '+') 2291 { 2292 errormsg = _(e_invalidreg); 2293 goto doend; 2294 } 2295 #endif 2296 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put 2297 && !IS_USER_CMDIDX(ea.cmdidx)))) 2298 { 2299 ea.regname = *ea.arg++; 2300 #ifdef FEAT_EVAL 2301 /* for '=' register: accept the rest of the line as an expression */ 2302 if (ea.arg[-1] == '=' && ea.arg[0] != NUL) 2303 { 2304 set_expr_line(vim_strsave(ea.arg)); 2305 ea.arg += STRLEN(ea.arg); 2306 } 2307 #endif 2308 ea.arg = skipwhite(ea.arg); 2309 } 2310 } 2311 2312 /* 2313 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a 2314 * count, it's a buffer name. 2315 */ 2316 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg) 2317 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL 2318 || VIM_ISWHITE(*p))) 2319 { 2320 n = getdigits(&ea.arg); 2321 ea.arg = skipwhite(ea.arg); 2322 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0) 2323 { 2324 errormsg = _(e_zerocount); 2325 goto doend; 2326 } 2327 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */ 2328 { 2329 ea.line2 = n; 2330 if (ea.addr_count == 0) 2331 ea.addr_count = 1; 2332 } 2333 else 2334 { 2335 ea.line1 = ea.line2; 2336 ea.line2 += n - 1; 2337 ++ea.addr_count; 2338 /* 2339 * Be vi compatible: no error message for out of range. 2340 */ 2341 if (ea.addr_type == ADDR_LINES 2342 && ea.line2 > curbuf->b_ml.ml_line_count) 2343 ea.line2 = curbuf->b_ml.ml_line_count; 2344 } 2345 } 2346 2347 /* 2348 * Check for flags: 'l', 'p' and '#'. 2349 */ 2350 if (ea.argt & EXFLAGS) 2351 get_flags(&ea); 2352 /* no arguments allowed */ 2353 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL 2354 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0)) 2355 { 2356 errormsg = _(e_trailing); 2357 goto doend; 2358 } 2359 2360 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL) 2361 { 2362 errormsg = _(e_argreq); 2363 goto doend; 2364 } 2365 2366 #ifdef FEAT_EVAL 2367 /* 2368 * Skip the command when it's not going to be executed. 2369 * The commands like :if, :endif, etc. always need to be executed. 2370 * Also make an exception for commands that handle a trailing command 2371 * themselves. 2372 */ 2373 if (ea.skip) 2374 { 2375 switch (ea.cmdidx) 2376 { 2377 /* commands that need evaluation */ 2378 case CMD_while: 2379 case CMD_endwhile: 2380 case CMD_for: 2381 case CMD_endfor: 2382 case CMD_if: 2383 case CMD_elseif: 2384 case CMD_else: 2385 case CMD_endif: 2386 case CMD_try: 2387 case CMD_catch: 2388 case CMD_finally: 2389 case CMD_endtry: 2390 case CMD_function: 2391 break; 2392 2393 /* Commands that handle '|' themselves. Check: A command should 2394 * either have the TRLBAR flag, appear in this list or appear in 2395 * the list at ":help :bar". */ 2396 case CMD_aboveleft: 2397 case CMD_and: 2398 case CMD_belowright: 2399 case CMD_botright: 2400 case CMD_browse: 2401 case CMD_call: 2402 case CMD_confirm: 2403 case CMD_delfunction: 2404 case CMD_djump: 2405 case CMD_dlist: 2406 case CMD_dsearch: 2407 case CMD_dsplit: 2408 case CMD_echo: 2409 case CMD_echoerr: 2410 case CMD_echomsg: 2411 case CMD_echon: 2412 case CMD_execute: 2413 case CMD_filter: 2414 case CMD_help: 2415 case CMD_hide: 2416 case CMD_ijump: 2417 case CMD_ilist: 2418 case CMD_isearch: 2419 case CMD_isplit: 2420 case CMD_keepalt: 2421 case CMD_keepjumps: 2422 case CMD_keepmarks: 2423 case CMD_keeppatterns: 2424 case CMD_leftabove: 2425 case CMD_let: 2426 case CMD_lockmarks: 2427 case CMD_lua: 2428 case CMD_match: 2429 case CMD_mzscheme: 2430 case CMD_noautocmd: 2431 case CMD_noswapfile: 2432 case CMD_perl: 2433 case CMD_psearch: 2434 case CMD_python: 2435 case CMD_py3: 2436 case CMD_python3: 2437 case CMD_return: 2438 case CMD_rightbelow: 2439 case CMD_ruby: 2440 case CMD_silent: 2441 case CMD_smagic: 2442 case CMD_snomagic: 2443 case CMD_substitute: 2444 case CMD_syntax: 2445 case CMD_tab: 2446 case CMD_tcl: 2447 case CMD_throw: 2448 case CMD_tilde: 2449 case CMD_topleft: 2450 case CMD_unlet: 2451 case CMD_verbose: 2452 case CMD_vertical: 2453 case CMD_wincmd: 2454 break; 2455 2456 default: goto doend; 2457 } 2458 } 2459 #endif 2460 2461 if (ea.argt & XFILE) 2462 { 2463 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL) 2464 goto doend; 2465 } 2466 2467 /* 2468 * Accept buffer name. Cannot be used at the same time with a buffer 2469 * number. Don't do this for a user command. 2470 */ 2471 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0 2472 && !IS_USER_CMDIDX(ea.cmdidx)) 2473 { 2474 /* 2475 * :bdelete, :bwipeout and :bunload take several arguments, separated 2476 * by spaces: find next space (skipping over escaped characters). 2477 * The others take one argument: ignore trailing spaces. 2478 */ 2479 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout 2480 || ea.cmdidx == CMD_bunload) 2481 p = skiptowhite_esc(ea.arg); 2482 else 2483 { 2484 p = ea.arg + STRLEN(ea.arg); 2485 while (p > ea.arg && VIM_ISWHITE(p[-1])) 2486 --p; 2487 } 2488 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, 2489 FALSE, FALSE); 2490 if (ea.line2 < 0) /* failed */ 2491 goto doend; 2492 ea.addr_count = 1; 2493 ea.arg = skipwhite(p); 2494 } 2495 2496 /* The :try command saves the emsg_silent flag, reset it here when 2497 * ":silent! try" was used, it should only apply to :try itself. */ 2498 if (ea.cmdidx == CMD_try && ea.did_esilent > 0) 2499 { 2500 emsg_silent -= ea.did_esilent; 2501 if (emsg_silent < 0) 2502 emsg_silent = 0; 2503 ea.did_esilent = 0; 2504 } 2505 2506 /* 2507 * 7. Execute the command. 2508 */ 2509 2510 #ifdef FEAT_USR_CMDS 2511 if (IS_USER_CMDIDX(ea.cmdidx)) 2512 { 2513 /* 2514 * Execute a user-defined command. 2515 */ 2516 do_ucmd(&ea); 2517 } 2518 else 2519 #endif 2520 { 2521 /* 2522 * Call the function to execute the command. 2523 */ 2524 ea.errmsg = NULL; 2525 (cmdnames[ea.cmdidx].cmd_func)(&ea); 2526 if (ea.errmsg != NULL) 2527 errormsg = _(ea.errmsg); 2528 } 2529 2530 #ifdef FEAT_EVAL 2531 /* 2532 * If the command just executed called do_cmdline(), any throw or ":return" 2533 * or ":finish" encountered there must also check the cstack of the still 2534 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught 2535 * exception, or reanimate a returned function or finished script file and 2536 * return or finish it again. 2537 */ 2538 if (need_rethrow) 2539 do_throw(cstack); 2540 else if (check_cstack) 2541 { 2542 if (source_finished(fgetline, cookie)) 2543 do_finish(&ea, TRUE); 2544 else if (getline_equal(fgetline, cookie, get_func_line) 2545 && current_func_returned()) 2546 do_return(&ea, TRUE, FALSE, NULL); 2547 } 2548 need_rethrow = check_cstack = FALSE; 2549 #endif 2550 2551 doend: 2552 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */ 2553 { 2554 curwin->w_cursor.lnum = 1; 2555 curwin->w_cursor.col = 0; 2556 } 2557 2558 if (errormsg != NULL && *errormsg != NUL && !did_emsg) 2559 { 2560 if (sourcing) 2561 { 2562 if (errormsg != (char *)IObuff) 2563 { 2564 STRCPY(IObuff, errormsg); 2565 errormsg = (char *)IObuff; 2566 } 2567 append_command(*cmdlinep); 2568 } 2569 emsg(errormsg); 2570 } 2571 #ifdef FEAT_EVAL 2572 do_errthrow(cstack, 2573 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx)) 2574 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL); 2575 #endif 2576 2577 if (ea.verbose_save >= 0) 2578 p_verbose = ea.verbose_save; 2579 2580 free_cmdmod(); 2581 cmdmod = save_cmdmod; 2582 2583 if (ea.save_msg_silent != -1) 2584 { 2585 /* messages could be enabled for a serious error, need to check if the 2586 * counters don't become negative */ 2587 if (!did_emsg || msg_silent > ea.save_msg_silent) 2588 msg_silent = ea.save_msg_silent; 2589 emsg_silent -= ea.did_esilent; 2590 if (emsg_silent < 0) 2591 emsg_silent = 0; 2592 /* Restore msg_scroll, it's set by file I/O commands, even when no 2593 * message is actually displayed. */ 2594 msg_scroll = save_msg_scroll; 2595 2596 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col 2597 * somewhere in the line. Put it back in the first column. */ 2598 if (redirecting()) 2599 msg_col = 0; 2600 } 2601 2602 #ifdef HAVE_SANDBOX 2603 if (ea.did_sandbox) 2604 --sandbox; 2605 #endif 2606 2607 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */ 2608 ea.nextcmd = NULL; 2609 2610 #ifdef FEAT_EVAL 2611 --ex_nesting_level; 2612 #endif 2613 2614 return ea.nextcmd; 2615 } 2616 #if (_MSC_VER == 1200) 2617 #pragma optimize( "", on ) 2618 #endif 2619 2620 /* 2621 * Parse and skip over command modifiers: 2622 * - update eap->cmd 2623 * - store flags in "cmdmod". 2624 * - Set ex_pressedreturn for an empty command line. 2625 * - set msg_silent for ":silent" 2626 * - set 'eventignore' to "all" for ":noautocmd" 2627 * - set p_verbose for ":verbose" 2628 * - Increment "sandbox" for ":sandbox" 2629 * When "skip_only" is TRUE the global variables are not changed, except for 2630 * "cmdmod". 2631 * Return FAIL when the command is not to be executed. 2632 * May set "errormsg" to an error message. 2633 */ 2634 int 2635 parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only) 2636 { 2637 char_u *p; 2638 2639 vim_memset(&cmdmod, 0, sizeof(cmdmod)); 2640 eap->verbose_save = -1; 2641 eap->save_msg_silent = -1; 2642 2643 // Repeat until no more command modifiers are found. 2644 for (;;) 2645 { 2646 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':') 2647 ++eap->cmd; 2648 2649 /* in ex mode, an empty line works like :+ */ 2650 if (*eap->cmd == NUL && exmode_active 2651 && (getline_equal(eap->getline, eap->cookie, getexmodeline) 2652 || getline_equal(eap->getline, eap->cookie, getexline)) 2653 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) 2654 { 2655 eap->cmd = (char_u *)"+"; 2656 if (!skip_only) 2657 ex_pressedreturn = TRUE; 2658 } 2659 2660 /* ignore comment and empty lines */ 2661 if (*eap->cmd == '"') 2662 return FAIL; 2663 if (*eap->cmd == NUL) 2664 { 2665 if (!skip_only) 2666 ex_pressedreturn = TRUE; 2667 return FAIL; 2668 } 2669 2670 p = skip_range(eap->cmd, NULL); 2671 switch (*p) 2672 { 2673 /* When adding an entry, also modify cmd_exists(). */ 2674 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3)) 2675 break; 2676 cmdmod.split |= WSP_ABOVE; 2677 continue; 2678 2679 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3)) 2680 { 2681 cmdmod.split |= WSP_BELOW; 2682 continue; 2683 } 2684 if (checkforcmd(&eap->cmd, "browse", 3)) 2685 { 2686 #ifdef FEAT_BROWSE_CMD 2687 cmdmod.browse = TRUE; 2688 #endif 2689 continue; 2690 } 2691 if (!checkforcmd(&eap->cmd, "botright", 2)) 2692 break; 2693 cmdmod.split |= WSP_BOT; 2694 continue; 2695 2696 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4)) 2697 break; 2698 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 2699 cmdmod.confirm = TRUE; 2700 #endif 2701 continue; 2702 2703 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3)) 2704 { 2705 cmdmod.keepmarks = TRUE; 2706 continue; 2707 } 2708 if (checkforcmd(&eap->cmd, "keepalt", 5)) 2709 { 2710 cmdmod.keepalt = TRUE; 2711 continue; 2712 } 2713 if (checkforcmd(&eap->cmd, "keeppatterns", 5)) 2714 { 2715 cmdmod.keeppatterns = TRUE; 2716 continue; 2717 } 2718 if (!checkforcmd(&eap->cmd, "keepjumps", 5)) 2719 break; 2720 cmdmod.keepjumps = TRUE; 2721 continue; 2722 2723 case 'f': /* only accept ":filter {pat} cmd" */ 2724 { 2725 char_u *reg_pat; 2726 2727 if (!checkforcmd(&p, "filter", 4) 2728 || *p == NUL || ends_excmd(*p)) 2729 break; 2730 if (*p == '!') 2731 { 2732 cmdmod.filter_force = TRUE; 2733 p = skipwhite(p + 1); 2734 if (*p == NUL || ends_excmd(*p)) 2735 break; 2736 } 2737 if (skip_only) 2738 p = skip_vimgrep_pat(p, NULL, NULL); 2739 else 2740 // NOTE: This puts a NUL after the pattern. 2741 p = skip_vimgrep_pat(p, ®_pat, NULL); 2742 if (p == NULL || *p == NUL) 2743 break; 2744 if (!skip_only) 2745 { 2746 cmdmod.filter_regmatch.regprog = 2747 vim_regcomp(reg_pat, RE_MAGIC); 2748 if (cmdmod.filter_regmatch.regprog == NULL) 2749 break; 2750 } 2751 eap->cmd = p; 2752 continue; 2753 } 2754 2755 /* ":hide" and ":hide | cmd" are not modifiers */ 2756 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3) 2757 || *p == NUL || ends_excmd(*p)) 2758 break; 2759 eap->cmd = p; 2760 cmdmod.hide = TRUE; 2761 continue; 2762 2763 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3)) 2764 { 2765 cmdmod.lockmarks = TRUE; 2766 continue; 2767 } 2768 2769 if (!checkforcmd(&eap->cmd, "leftabove", 5)) 2770 break; 2771 cmdmod.split |= WSP_ABOVE; 2772 continue; 2773 2774 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3)) 2775 { 2776 if (cmdmod.save_ei == NULL && !skip_only) 2777 { 2778 /* Set 'eventignore' to "all". Restore the 2779 * existing option value later. */ 2780 cmdmod.save_ei = vim_strsave(p_ei); 2781 set_string_option_direct((char_u *)"ei", -1, 2782 (char_u *)"all", OPT_FREE, SID_NONE); 2783 } 2784 continue; 2785 } 2786 if (!checkforcmd(&eap->cmd, "noswapfile", 3)) 2787 break; 2788 cmdmod.noswapfile = TRUE; 2789 continue; 2790 2791 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6)) 2792 break; 2793 cmdmod.split |= WSP_BELOW; 2794 continue; 2795 2796 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3)) 2797 { 2798 #ifdef HAVE_SANDBOX 2799 if (!skip_only) 2800 { 2801 if (!eap->did_sandbox) 2802 ++sandbox; 2803 eap->did_sandbox = TRUE; 2804 } 2805 #endif 2806 continue; 2807 } 2808 if (!checkforcmd(&eap->cmd, "silent", 3)) 2809 break; 2810 if (!skip_only) 2811 { 2812 if (eap->save_msg_silent == -1) 2813 eap->save_msg_silent = msg_silent; 2814 ++msg_silent; 2815 } 2816 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1])) 2817 { 2818 /* ":silent!", but not "silent !cmd" */ 2819 eap->cmd = skipwhite(eap->cmd + 1); 2820 if (!skip_only) 2821 { 2822 ++emsg_silent; 2823 ++eap->did_esilent; 2824 } 2825 } 2826 continue; 2827 2828 case 't': if (checkforcmd(&p, "tab", 3)) 2829 { 2830 if (!skip_only) 2831 { 2832 long tabnr = get_address(eap, &eap->cmd, 2833 ADDR_TABS, eap->skip, 2834 skip_only, FALSE, 1); 2835 if (tabnr == MAXLNUM) 2836 cmdmod.tab = tabpage_index(curtab) + 1; 2837 else 2838 { 2839 if (tabnr < 0 || tabnr > LAST_TAB_NR) 2840 { 2841 *errormsg = _(e_invrange); 2842 return FAIL; 2843 } 2844 cmdmod.tab = tabnr + 1; 2845 } 2846 } 2847 eap->cmd = p; 2848 continue; 2849 } 2850 if (!checkforcmd(&eap->cmd, "topleft", 2)) 2851 break; 2852 cmdmod.split |= WSP_TOP; 2853 continue; 2854 2855 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3)) 2856 break; 2857 if (!skip_only) 2858 { 2859 if (eap->save_msg_silent == -1) 2860 eap->save_msg_silent = msg_silent; 2861 msg_silent = 0; 2862 } 2863 continue; 2864 2865 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4)) 2866 { 2867 cmdmod.split |= WSP_VERT; 2868 continue; 2869 } 2870 if (!checkforcmd(&p, "verbose", 4)) 2871 break; 2872 if (!skip_only) 2873 { 2874 if (eap->verbose_save < 0) 2875 eap->verbose_save = p_verbose; 2876 if (vim_isdigit(*eap->cmd)) 2877 p_verbose = atoi((char *)eap->cmd); 2878 else 2879 p_verbose = 1; 2880 } 2881 eap->cmd = p; 2882 continue; 2883 } 2884 break; 2885 } 2886 2887 return OK; 2888 } 2889 2890 /* 2891 * Free contents of "cmdmod". 2892 */ 2893 static void 2894 free_cmdmod(void) 2895 { 2896 if (cmdmod.save_ei != NULL) 2897 { 2898 /* Restore 'eventignore' to the value before ":noautocmd". */ 2899 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei, 2900 OPT_FREE, SID_NONE); 2901 free_string_option(cmdmod.save_ei); 2902 } 2903 2904 if (cmdmod.filter_regmatch.regprog != NULL) 2905 vim_regfree(cmdmod.filter_regmatch.regprog); 2906 } 2907 2908 /* 2909 * Parse the address range, if any, in "eap". 2910 * May set the last search pattern, unless "silent" is TRUE. 2911 * Return FAIL and set "errormsg" or return OK. 2912 */ 2913 int 2914 parse_cmd_address(exarg_T *eap, char **errormsg, int silent) 2915 { 2916 int address_count = 1; 2917 linenr_T lnum; 2918 2919 // Repeat for all ',' or ';' separated addresses. 2920 for (;;) 2921 { 2922 eap->line1 = eap->line2; 2923 switch (eap->addr_type) 2924 { 2925 case ADDR_LINES: 2926 // default is current line number 2927 eap->line2 = curwin->w_cursor.lnum; 2928 break; 2929 case ADDR_WINDOWS: 2930 eap->line2 = CURRENT_WIN_NR; 2931 break; 2932 case ADDR_ARGUMENTS: 2933 eap->line2 = curwin->w_arg_idx + 1; 2934 if (eap->line2 > ARGCOUNT) 2935 eap->line2 = ARGCOUNT; 2936 break; 2937 case ADDR_LOADED_BUFFERS: 2938 case ADDR_BUFFERS: 2939 eap->line2 = curbuf->b_fnum; 2940 break; 2941 case ADDR_TABS: 2942 eap->line2 = CURRENT_TAB_NR; 2943 break; 2944 case ADDR_TABS_RELATIVE: 2945 eap->line2 = 1; 2946 break; 2947 #ifdef FEAT_QUICKFIX 2948 case ADDR_QUICKFIX: 2949 eap->line2 = qf_get_cur_valid_idx(eap); 2950 break; 2951 #endif 2952 } 2953 eap->cmd = skipwhite(eap->cmd); 2954 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent, 2955 eap->addr_count == 0, address_count++); 2956 if (eap->cmd == NULL) // error detected 2957 return FAIL; 2958 if (lnum == MAXLNUM) 2959 { 2960 if (*eap->cmd == '%') // '%' - all lines 2961 { 2962 ++eap->cmd; 2963 switch (eap->addr_type) 2964 { 2965 case ADDR_LINES: 2966 eap->line1 = 1; 2967 eap->line2 = curbuf->b_ml.ml_line_count; 2968 break; 2969 case ADDR_LOADED_BUFFERS: 2970 { 2971 buf_T *buf = firstbuf; 2972 2973 while (buf->b_next != NULL 2974 && buf->b_ml.ml_mfp == NULL) 2975 buf = buf->b_next; 2976 eap->line1 = buf->b_fnum; 2977 buf = lastbuf; 2978 while (buf->b_prev != NULL 2979 && buf->b_ml.ml_mfp == NULL) 2980 buf = buf->b_prev; 2981 eap->line2 = buf->b_fnum; 2982 break; 2983 } 2984 case ADDR_BUFFERS: 2985 eap->line1 = firstbuf->b_fnum; 2986 eap->line2 = lastbuf->b_fnum; 2987 break; 2988 case ADDR_WINDOWS: 2989 case ADDR_TABS: 2990 if (IS_USER_CMDIDX(eap->cmdidx)) 2991 { 2992 eap->line1 = 1; 2993 eap->line2 = eap->addr_type == ADDR_WINDOWS 2994 ? LAST_WIN_NR : LAST_TAB_NR; 2995 } 2996 else 2997 { 2998 // there is no Vim command which uses '%' and 2999 // ADDR_WINDOWS or ADDR_TABS 3000 *errormsg = _(e_invrange); 3001 return FAIL; 3002 } 3003 break; 3004 case ADDR_TABS_RELATIVE: 3005 case ADDR_OTHER: 3006 *errormsg = _(e_invrange); 3007 return FAIL; 3008 case ADDR_ARGUMENTS: 3009 if (ARGCOUNT == 0) 3010 eap->line1 = eap->line2 = 0; 3011 else 3012 { 3013 eap->line1 = 1; 3014 eap->line2 = ARGCOUNT; 3015 } 3016 break; 3017 #ifdef FEAT_QUICKFIX 3018 case ADDR_QUICKFIX: 3019 eap->line1 = 1; 3020 eap->line2 = qf_get_size(eap); 3021 if (eap->line2 == 0) 3022 eap->line2 = 1; 3023 break; 3024 #endif 3025 } 3026 ++eap->addr_count; 3027 } 3028 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL) 3029 { 3030 pos_T *fp; 3031 3032 // '*' - visual area 3033 if (eap->addr_type != ADDR_LINES) 3034 { 3035 *errormsg = _(e_invrange); 3036 return FAIL; 3037 } 3038 3039 ++eap->cmd; 3040 if (!eap->skip) 3041 { 3042 fp = getmark('<', FALSE); 3043 if (check_mark(fp) == FAIL) 3044 return FAIL; 3045 eap->line1 = fp->lnum; 3046 fp = getmark('>', FALSE); 3047 if (check_mark(fp) == FAIL) 3048 return FAIL; 3049 eap->line2 = fp->lnum; 3050 ++eap->addr_count; 3051 } 3052 } 3053 } 3054 else 3055 eap->line2 = lnum; 3056 eap->addr_count++; 3057 3058 if (*eap->cmd == ';') 3059 { 3060 if (!eap->skip) 3061 { 3062 curwin->w_cursor.lnum = eap->line2; 3063 // don't leave the cursor on an illegal line or column 3064 check_cursor(); 3065 } 3066 } 3067 else if (*eap->cmd != ',') 3068 break; 3069 ++eap->cmd; 3070 } 3071 3072 // One address given: set start and end lines. 3073 if (eap->addr_count == 1) 3074 { 3075 eap->line1 = eap->line2; 3076 // ... but only implicit: really no address given 3077 if (lnum == MAXLNUM) 3078 eap->addr_count = 0; 3079 } 3080 return OK; 3081 } 3082 3083 /* 3084 * Check for an Ex command with optional tail. 3085 * If there is a match advance "pp" to the argument and return TRUE. 3086 */ 3087 int 3088 checkforcmd( 3089 char_u **pp, /* start of command */ 3090 char *cmd, /* name of command */ 3091 int len) /* required length */ 3092 { 3093 int i; 3094 3095 for (i = 0; cmd[i] != NUL; ++i) 3096 if (((char_u *)cmd)[i] != (*pp)[i]) 3097 break; 3098 if (i >= len && !isalpha((*pp)[i])) 3099 { 3100 *pp = skipwhite(*pp + i); 3101 return TRUE; 3102 } 3103 return FALSE; 3104 } 3105 3106 /* 3107 * Append "cmd" to the error message in IObuff. 3108 * Takes care of limiting the length and handling 0xa0, which would be 3109 * invisible otherwise. 3110 */ 3111 static void 3112 append_command(char_u *cmd) 3113 { 3114 char_u *s = cmd; 3115 char_u *d; 3116 3117 STRCAT(IObuff, ": "); 3118 d = IObuff + STRLEN(IObuff); 3119 while (*s != NUL && d - IObuff < IOSIZE - 7) 3120 { 3121 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0) 3122 { 3123 s += enc_utf8 ? 2 : 1; 3124 STRCPY(d, "<a0>"); 3125 d += 4; 3126 } 3127 else 3128 MB_COPY_CHAR(s, d); 3129 } 3130 *d = NUL; 3131 } 3132 3133 /* 3134 * Find an Ex command by its name, either built-in or user. 3135 * Start of the name can be found at eap->cmd. 3136 * Returns pointer to char after the command name. 3137 * "full" is set to TRUE if the whole command name matched. 3138 * Returns NULL for an ambiguous user command. 3139 */ 3140 static char_u * 3141 find_command(exarg_T *eap, int *full UNUSED) 3142 { 3143 int len; 3144 char_u *p; 3145 int i; 3146 3147 /* 3148 * Isolate the command and search for it in the command table. 3149 * Exceptions: 3150 * - the 'k' command can directly be followed by any character. 3151 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' 3152 * but :sre[wind] is another command, as are :scr[iptnames], 3153 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent]. 3154 * - the "d" command can directly be followed by 'l' or 'p' flag. 3155 */ 3156 p = eap->cmd; 3157 if (*p == 'k') 3158 { 3159 eap->cmdidx = CMD_k; 3160 ++p; 3161 } 3162 else if (p[0] == 's' 3163 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r' 3164 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p'))))) 3165 || p[1] == 'g' 3166 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g') 3167 || p[1] == 'I' 3168 || (p[1] == 'r' && p[2] != 'e'))) 3169 { 3170 eap->cmdidx = CMD_substitute; 3171 ++p; 3172 } 3173 else 3174 { 3175 while (ASCII_ISALPHA(*p)) 3176 ++p; 3177 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */ 3178 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y') 3179 while (ASCII_ISALNUM(*p)) 3180 ++p; 3181 3182 /* check for non-alpha command */ 3183 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL) 3184 ++p; 3185 len = (int)(p - eap->cmd); 3186 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p')) 3187 { 3188 /* Check for ":dl", ":dell", etc. to ":deletel": that's 3189 * :delete with the 'l' flag. Same for 'p'. */ 3190 for (i = 0; i < len; ++i) 3191 if (eap->cmd[i] != ((char_u *)"delete")[i]) 3192 break; 3193 if (i == len - 1) 3194 { 3195 --len; 3196 if (p[-1] == 'l') 3197 eap->flags |= EXFLAG_LIST; 3198 else 3199 eap->flags |= EXFLAG_PRINT; 3200 } 3201 } 3202 3203 if (ASCII_ISLOWER(eap->cmd[0])) 3204 { 3205 int c1 = eap->cmd[0]; 3206 int c2 = eap->cmd[1]; 3207 3208 if (command_count != (int)CMD_SIZE) 3209 { 3210 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'")); 3211 getout(1); 3212 } 3213 3214 /* Use a precomputed index for fast look-up in cmdnames[] 3215 * taking into account the first 2 letters of eap->cmd. */ 3216 eap->cmdidx = cmdidxs1[CharOrdLow(c1)]; 3217 if (ASCII_ISLOWER(c2)) 3218 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)]; 3219 } 3220 else 3221 eap->cmdidx = CMD_bang; 3222 3223 for ( ; (int)eap->cmdidx < (int)CMD_SIZE; 3224 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1)) 3225 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd, 3226 (size_t)len) == 0) 3227 { 3228 #ifdef FEAT_EVAL 3229 if (full != NULL 3230 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL) 3231 *full = TRUE; 3232 #endif 3233 break; 3234 } 3235 3236 #ifdef FEAT_USR_CMDS 3237 /* Look for a user defined command as a last resort. Let ":Print" be 3238 * overruled by a user defined command. */ 3239 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print) 3240 && *eap->cmd >= 'A' && *eap->cmd <= 'Z') 3241 { 3242 /* User defined commands may contain digits. */ 3243 while (ASCII_ISALNUM(*p)) 3244 ++p; 3245 p = find_ucmd(eap, p, full, NULL, NULL); 3246 } 3247 #endif 3248 if (p == eap->cmd) 3249 eap->cmdidx = CMD_SIZE; 3250 } 3251 3252 return p; 3253 } 3254 3255 #ifdef FEAT_USR_CMDS 3256 /* 3257 * Search for a user command that matches "eap->cmd". 3258 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx". 3259 * Return a pointer to just after the command. 3260 * Return NULL if there is no matching command. 3261 */ 3262 static char_u * 3263 find_ucmd( 3264 exarg_T *eap, 3265 char_u *p, /* end of the command (possibly including count) */ 3266 int *full, /* set to TRUE for a full match */ 3267 expand_T *xp, /* used for completion, NULL otherwise */ 3268 int *compl) /* completion flags or NULL */ 3269 { 3270 int len = (int)(p - eap->cmd); 3271 int j, k, matchlen = 0; 3272 ucmd_T *uc; 3273 int found = FALSE; 3274 int possible = FALSE; 3275 char_u *cp, *np; /* Point into typed cmd and test name */ 3276 garray_T *gap; 3277 int amb_local = FALSE; /* Found ambiguous buffer-local command, 3278 only full match global is accepted. */ 3279 3280 /* 3281 * Look for buffer-local user commands first, then global ones. 3282 */ 3283 gap = &curbuf->b_ucmds; 3284 for (;;) 3285 { 3286 for (j = 0; j < gap->ga_len; ++j) 3287 { 3288 uc = USER_CMD_GA(gap, j); 3289 cp = eap->cmd; 3290 np = uc->uc_name; 3291 k = 0; 3292 while (k < len && *np != NUL && *cp++ == *np++) 3293 k++; 3294 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k]))) 3295 { 3296 /* If finding a second match, the command is ambiguous. But 3297 * not if a buffer-local command wasn't a full match and a 3298 * global command is a full match. */ 3299 if (k == len && found && *np != NUL) 3300 { 3301 if (gap == &ucmds) 3302 return NULL; 3303 amb_local = TRUE; 3304 } 3305 3306 if (!found || (k == len && *np == NUL)) 3307 { 3308 /* If we matched up to a digit, then there could 3309 * be another command including the digit that we 3310 * should use instead. 3311 */ 3312 if (k == len) 3313 found = TRUE; 3314 else 3315 possible = TRUE; 3316 3317 if (gap == &ucmds) 3318 eap->cmdidx = CMD_USER; 3319 else 3320 eap->cmdidx = CMD_USER_BUF; 3321 eap->argt = (long)uc->uc_argt; 3322 eap->useridx = j; 3323 eap->addr_type = uc->uc_addr_type; 3324 3325 # ifdef FEAT_CMDL_COMPL 3326 if (compl != NULL) 3327 *compl = uc->uc_compl; 3328 # ifdef FEAT_EVAL 3329 if (xp != NULL) 3330 { 3331 xp->xp_arg = uc->uc_compl_arg; 3332 xp->xp_script_ctx = uc->uc_script_ctx; 3333 xp->xp_script_ctx.sc_lnum += sourcing_lnum; 3334 } 3335 # endif 3336 # endif 3337 /* Do not search for further abbreviations 3338 * if this is an exact match. */ 3339 matchlen = k; 3340 if (k == len && *np == NUL) 3341 { 3342 if (full != NULL) 3343 *full = TRUE; 3344 amb_local = FALSE; 3345 break; 3346 } 3347 } 3348 } 3349 } 3350 3351 /* Stop if we found a full match or searched all. */ 3352 if (j < gap->ga_len || gap == &ucmds) 3353 break; 3354 gap = &ucmds; 3355 } 3356 3357 /* Only found ambiguous matches. */ 3358 if (amb_local) 3359 { 3360 if (xp != NULL) 3361 xp->xp_context = EXPAND_UNSUCCESSFUL; 3362 return NULL; 3363 } 3364 3365 /* The match we found may be followed immediately by a number. Move "p" 3366 * back to point to it. */ 3367 if (found || possible) 3368 return p + (matchlen - len); 3369 return p; 3370 } 3371 #endif 3372 3373 #if defined(FEAT_EVAL) || defined(PROTO) 3374 static struct cmdmod 3375 { 3376 char *name; 3377 int minlen; 3378 int has_count; /* :123verbose :3tab */ 3379 } cmdmods[] = { 3380 {"aboveleft", 3, FALSE}, 3381 {"belowright", 3, FALSE}, 3382 {"botright", 2, FALSE}, 3383 {"browse", 3, FALSE}, 3384 {"confirm", 4, FALSE}, 3385 {"filter", 4, FALSE}, 3386 {"hide", 3, FALSE}, 3387 {"keepalt", 5, FALSE}, 3388 {"keepjumps", 5, FALSE}, 3389 {"keepmarks", 3, FALSE}, 3390 {"keeppatterns", 5, FALSE}, 3391 {"leftabove", 5, FALSE}, 3392 {"lockmarks", 3, FALSE}, 3393 {"noautocmd", 3, FALSE}, 3394 {"noswapfile", 3, FALSE}, 3395 {"rightbelow", 6, FALSE}, 3396 {"sandbox", 3, FALSE}, 3397 {"silent", 3, FALSE}, 3398 {"tab", 3, TRUE}, 3399 {"topleft", 2, FALSE}, 3400 {"unsilent", 3, FALSE}, 3401 {"verbose", 4, TRUE}, 3402 {"vertical", 4, FALSE}, 3403 }; 3404 3405 /* 3406 * Return length of a command modifier (including optional count). 3407 * Return zero when it's not a modifier. 3408 */ 3409 int 3410 modifier_len(char_u *cmd) 3411 { 3412 int i, j; 3413 char_u *p = cmd; 3414 3415 if (VIM_ISDIGIT(*cmd)) 3416 p = skipwhite(skipdigits(cmd)); 3417 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i) 3418 { 3419 for (j = 0; p[j] != NUL; ++j) 3420 if (p[j] != cmdmods[i].name[j]) 3421 break; 3422 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen 3423 && (p == cmd || cmdmods[i].has_count)) 3424 return j + (int)(p - cmd); 3425 } 3426 return 0; 3427 } 3428 3429 /* 3430 * Return > 0 if an Ex command "name" exists. 3431 * Return 2 if there is an exact match. 3432 * Return 3 if there is an ambiguous match. 3433 */ 3434 int 3435 cmd_exists(char_u *name) 3436 { 3437 exarg_T ea; 3438 int full = FALSE; 3439 int i; 3440 int j; 3441 char_u *p; 3442 3443 /* Check command modifiers. */ 3444 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i) 3445 { 3446 for (j = 0; name[j] != NUL; ++j) 3447 if (name[j] != cmdmods[i].name[j]) 3448 break; 3449 if (name[j] == NUL && j >= cmdmods[i].minlen) 3450 return (cmdmods[i].name[j] == NUL ? 2 : 1); 3451 } 3452 3453 /* Check built-in commands and user defined commands. 3454 * For ":2match" and ":3match" we need to skip the number. */ 3455 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name; 3456 ea.cmdidx = (cmdidx_T)0; 3457 p = find_command(&ea, &full); 3458 if (p == NULL) 3459 return 3; 3460 if (vim_isdigit(*name) && ea.cmdidx != CMD_match) 3461 return 0; 3462 if (*skipwhite(p) != NUL) 3463 return 0; /* trailing garbage */ 3464 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1)); 3465 } 3466 #endif 3467 3468 /* 3469 * This is all pretty much copied from do_one_cmd(), with all the extra stuff 3470 * we don't need/want deleted. Maybe this could be done better if we didn't 3471 * repeat all this stuff. The only problem is that they may not stay 3472 * perfectly compatible with each other, but then the command line syntax 3473 * probably won't change that much -- webb. 3474 */ 3475 char_u * 3476 set_one_cmd_context( 3477 expand_T *xp, 3478 char_u *buff) /* buffer for command string */ 3479 { 3480 char_u *p; 3481 char_u *cmd, *arg; 3482 int len = 0; 3483 exarg_T ea; 3484 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) 3485 int compl = EXPAND_NOTHING; 3486 #endif 3487 #ifdef FEAT_CMDL_COMPL 3488 int delim; 3489 #endif 3490 int forceit = FALSE; 3491 int usefilter = FALSE; /* filter instead of file name */ 3492 3493 ExpandInit(xp); 3494 xp->xp_pattern = buff; 3495 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */ 3496 ea.argt = 0; 3497 3498 /* 3499 * 1. skip comment lines and leading space, colons or bars 3500 */ 3501 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++) 3502 ; 3503 xp->xp_pattern = cmd; 3504 3505 if (*cmd == NUL) 3506 return NULL; 3507 if (*cmd == '"') /* ignore comment lines */ 3508 { 3509 xp->xp_context = EXPAND_NOTHING; 3510 return NULL; 3511 } 3512 3513 /* 3514 * 3. Skip over the range to find the command. 3515 */ 3516 cmd = skip_range(cmd, &xp->xp_context); 3517 xp->xp_pattern = cmd; 3518 if (*cmd == NUL) 3519 return NULL; 3520 if (*cmd == '"') 3521 { 3522 xp->xp_context = EXPAND_NOTHING; 3523 return NULL; 3524 } 3525 3526 if (*cmd == '|' || *cmd == '\n') 3527 return cmd + 1; /* There's another command */ 3528 3529 /* 3530 * Isolate the command and search for it in the command table. 3531 * Exceptions: 3532 * - the 'k' command can directly be followed by any character, but 3533 * do accept "keepmarks", "keepalt" and "keepjumps". 3534 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' 3535 */ 3536 if (*cmd == 'k' && cmd[1] != 'e') 3537 { 3538 ea.cmdidx = CMD_k; 3539 p = cmd + 1; 3540 } 3541 else 3542 { 3543 p = cmd; 3544 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */ 3545 ++p; 3546 /* a user command may contain digits */ 3547 if (ASCII_ISUPPER(cmd[0])) 3548 while (ASCII_ISALNUM(*p) || *p == '*') 3549 ++p; 3550 /* for python 3.x: ":py3*" commands completion */ 3551 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3') 3552 { 3553 ++p; 3554 while (ASCII_ISALPHA(*p) || *p == '*') 3555 ++p; 3556 } 3557 /* check for non-alpha command */ 3558 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL) 3559 ++p; 3560 len = (int)(p - cmd); 3561 3562 if (len == 0) 3563 { 3564 xp->xp_context = EXPAND_UNSUCCESSFUL; 3565 return NULL; 3566 } 3567 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE; 3568 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1)) 3569 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, 3570 (size_t)len) == 0) 3571 break; 3572 3573 #ifdef FEAT_USR_CMDS 3574 if (cmd[0] >= 'A' && cmd[0] <= 'Z') 3575 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */ 3576 ++p; 3577 #endif 3578 } 3579 3580 /* 3581 * If the cursor is touching the command, and it ends in an alpha-numeric 3582 * character, complete the command name. 3583 */ 3584 if (*p == NUL && ASCII_ISALNUM(p[-1])) 3585 return NULL; 3586 3587 if (ea.cmdidx == CMD_SIZE) 3588 { 3589 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL) 3590 { 3591 ea.cmdidx = CMD_substitute; 3592 p = cmd + 1; 3593 } 3594 #ifdef FEAT_USR_CMDS 3595 else if (cmd[0] >= 'A' && cmd[0] <= 'Z') 3596 { 3597 ea.cmd = cmd; 3598 p = find_ucmd(&ea, p, NULL, xp, 3599 # if defined(FEAT_CMDL_COMPL) 3600 &compl 3601 # else 3602 NULL 3603 # endif 3604 ); 3605 if (p == NULL) 3606 ea.cmdidx = CMD_SIZE; /* ambiguous user command */ 3607 } 3608 #endif 3609 } 3610 if (ea.cmdidx == CMD_SIZE) 3611 { 3612 /* Not still touching the command and it was an illegal one */ 3613 xp->xp_context = EXPAND_UNSUCCESSFUL; 3614 return NULL; 3615 } 3616 3617 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */ 3618 3619 if (*p == '!') /* forced commands */ 3620 { 3621 forceit = TRUE; 3622 ++p; 3623 } 3624 3625 /* 3626 * 6. parse arguments 3627 */ 3628 if (!IS_USER_CMDIDX(ea.cmdidx)) 3629 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt; 3630 3631 arg = skipwhite(p); 3632 3633 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update) 3634 { 3635 if (*arg == '>') /* append */ 3636 { 3637 if (*++arg == '>') 3638 ++arg; 3639 arg = skipwhite(arg); 3640 } 3641 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */ 3642 { 3643 ++arg; 3644 usefilter = TRUE; 3645 } 3646 } 3647 3648 if (ea.cmdidx == CMD_read) 3649 { 3650 usefilter = forceit; /* :r! filter if forced */ 3651 if (*arg == '!') /* :r !filter */ 3652 { 3653 ++arg; 3654 usefilter = TRUE; 3655 } 3656 } 3657 3658 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift) 3659 { 3660 while (*arg == *cmd) /* allow any number of '>' or '<' */ 3661 ++arg; 3662 arg = skipwhite(arg); 3663 } 3664 3665 /* Does command allow "+command"? */ 3666 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+') 3667 { 3668 /* Check if we're in the +command */ 3669 p = arg + 1; 3670 arg = skip_cmd_arg(arg, FALSE); 3671 3672 /* Still touching the command after '+'? */ 3673 if (*arg == NUL) 3674 return p; 3675 3676 /* Skip space(s) after +command to get to the real argument */ 3677 arg = skipwhite(arg); 3678 } 3679 3680 /* 3681 * Check for '|' to separate commands and '"' to start comments. 3682 * Don't do this for ":read !cmd" and ":write !cmd". 3683 */ 3684 if ((ea.argt & TRLBAR) && !usefilter) 3685 { 3686 p = arg; 3687 /* ":redir @" is not the start of a comment */ 3688 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"') 3689 p += 2; 3690 while (*p) 3691 { 3692 if (*p == Ctrl_V) 3693 { 3694 if (p[1] != NUL) 3695 ++p; 3696 } 3697 else if ( (*p == '"' && !(ea.argt & NOTRLCOM)) 3698 || *p == '|' || *p == '\n') 3699 { 3700 if (*(p - 1) != '\\') 3701 { 3702 if (*p == '|' || *p == '\n') 3703 return p + 1; 3704 return NULL; /* It's a comment */ 3705 } 3706 } 3707 MB_PTR_ADV(p); 3708 } 3709 } 3710 3711 /* no arguments allowed */ 3712 if (!(ea.argt & EXTRA) && *arg != NUL && 3713 vim_strchr((char_u *)"|\"", *arg) == NULL) 3714 return NULL; 3715 3716 /* Find start of last argument (argument just before cursor): */ 3717 p = buff; 3718 xp->xp_pattern = p; 3719 len = (int)STRLEN(buff); 3720 while (*p && p < buff + len) 3721 { 3722 if (*p == ' ' || *p == TAB) 3723 { 3724 /* argument starts after a space */ 3725 xp->xp_pattern = ++p; 3726 } 3727 else 3728 { 3729 if (*p == '\\' && *(p + 1) != NUL) 3730 ++p; /* skip over escaped character */ 3731 MB_PTR_ADV(p); 3732 } 3733 } 3734 3735 if (ea.argt & XFILE) 3736 { 3737 int c; 3738 int in_quote = FALSE; 3739 char_u *bow = NULL; /* Beginning of word */ 3740 3741 /* 3742 * Allow spaces within back-quotes to count as part of the argument 3743 * being expanded. 3744 */ 3745 xp->xp_pattern = skipwhite(arg); 3746 p = xp->xp_pattern; 3747 while (*p != NUL) 3748 { 3749 if (has_mbyte) 3750 c = mb_ptr2char(p); 3751 else 3752 c = *p; 3753 if (c == '\\' && p[1] != NUL) 3754 ++p; 3755 else if (c == '`') 3756 { 3757 if (!in_quote) 3758 { 3759 xp->xp_pattern = p; 3760 bow = p + 1; 3761 } 3762 in_quote = !in_quote; 3763 } 3764 /* An argument can contain just about everything, except 3765 * characters that end the command and white space. */ 3766 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c) 3767 #ifdef SPACE_IN_FILENAME 3768 && (!(ea.argt & NOSPC) || usefilter) 3769 #endif 3770 )) 3771 { 3772 len = 0; /* avoid getting stuck when space is in 'isfname' */ 3773 while (*p != NUL) 3774 { 3775 if (has_mbyte) 3776 c = mb_ptr2char(p); 3777 else 3778 c = *p; 3779 if (c == '`' || vim_isfilec_or_wc(c)) 3780 break; 3781 if (has_mbyte) 3782 len = (*mb_ptr2len)(p); 3783 else 3784 len = 1; 3785 MB_PTR_ADV(p); 3786 } 3787 if (in_quote) 3788 bow = p; 3789 else 3790 xp->xp_pattern = p; 3791 p -= len; 3792 } 3793 MB_PTR_ADV(p); 3794 } 3795 3796 /* 3797 * If we are still inside the quotes, and we passed a space, just 3798 * expand from there. 3799 */ 3800 if (bow != NULL && in_quote) 3801 xp->xp_pattern = bow; 3802 xp->xp_context = EXPAND_FILES; 3803 3804 /* For a shell command more chars need to be escaped. */ 3805 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal) 3806 { 3807 #ifndef BACKSLASH_IN_FILENAME 3808 xp->xp_shell = TRUE; 3809 #endif 3810 /* When still after the command name expand executables. */ 3811 if (xp->xp_pattern == skipwhite(arg)) 3812 xp->xp_context = EXPAND_SHELLCMD; 3813 } 3814 3815 /* Check for environment variable */ 3816 if (*xp->xp_pattern == '$' 3817 #if defined(MSWIN) 3818 || *xp->xp_pattern == '%' 3819 #endif 3820 ) 3821 { 3822 for (p = xp->xp_pattern + 1; *p != NUL; ++p) 3823 if (!vim_isIDc(*p)) 3824 break; 3825 if (*p == NUL) 3826 { 3827 xp->xp_context = EXPAND_ENV_VARS; 3828 ++xp->xp_pattern; 3829 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) 3830 /* Avoid that the assignment uses EXPAND_FILES again. */ 3831 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST) 3832 compl = EXPAND_ENV_VARS; 3833 #endif 3834 } 3835 } 3836 #if defined(FEAT_CMDL_COMPL) 3837 /* Check for user names */ 3838 if (*xp->xp_pattern == '~') 3839 { 3840 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p) 3841 ; 3842 /* Complete ~user only if it partially matches a user name. 3843 * A full match ~user<Tab> will be replaced by user's home 3844 * directory i.e. something like ~user<Tab> -> /home/user/ */ 3845 if (*p == NUL && p > xp->xp_pattern + 1 3846 && match_user(xp->xp_pattern + 1) >= 1) 3847 { 3848 xp->xp_context = EXPAND_USER; 3849 ++xp->xp_pattern; 3850 } 3851 } 3852 #endif 3853 } 3854 3855 /* 3856 * 6. Switch on command name. 3857 */ 3858 switch (ea.cmdidx) 3859 { 3860 case CMD_find: 3861 case CMD_sfind: 3862 case CMD_tabfind: 3863 if (xp->xp_context == EXPAND_FILES) 3864 xp->xp_context = EXPAND_FILES_IN_PATH; 3865 break; 3866 case CMD_cd: 3867 case CMD_chdir: 3868 case CMD_lcd: 3869 case CMD_lchdir: 3870 if (xp->xp_context == EXPAND_FILES) 3871 xp->xp_context = EXPAND_DIRECTORIES; 3872 break; 3873 case CMD_help: 3874 xp->xp_context = EXPAND_HELP; 3875 xp->xp_pattern = arg; 3876 break; 3877 3878 /* Command modifiers: return the argument. 3879 * Also for commands with an argument that is a command. */ 3880 case CMD_aboveleft: 3881 case CMD_argdo: 3882 case CMD_belowright: 3883 case CMD_botright: 3884 case CMD_browse: 3885 case CMD_bufdo: 3886 case CMD_cdo: 3887 case CMD_cfdo: 3888 case CMD_confirm: 3889 case CMD_debug: 3890 case CMD_folddoclosed: 3891 case CMD_folddoopen: 3892 case CMD_hide: 3893 case CMD_keepalt: 3894 case CMD_keepjumps: 3895 case CMD_keepmarks: 3896 case CMD_keeppatterns: 3897 case CMD_ldo: 3898 case CMD_leftabove: 3899 case CMD_lfdo: 3900 case CMD_lockmarks: 3901 case CMD_noautocmd: 3902 case CMD_noswapfile: 3903 case CMD_rightbelow: 3904 case CMD_sandbox: 3905 case CMD_silent: 3906 case CMD_tab: 3907 case CMD_tabdo: 3908 case CMD_topleft: 3909 case CMD_verbose: 3910 case CMD_vertical: 3911 case CMD_windo: 3912 return arg; 3913 3914 case CMD_filter: 3915 if (*arg != NUL) 3916 arg = skip_vimgrep_pat(arg, NULL, NULL); 3917 if (arg == NULL || *arg == NUL) 3918 { 3919 xp->xp_context = EXPAND_NOTHING; 3920 return NULL; 3921 } 3922 return skipwhite(arg); 3923 3924 #ifdef FEAT_CMDL_COMPL 3925 # ifdef FEAT_SEARCH_EXTRA 3926 case CMD_match: 3927 if (*arg == NUL || !ends_excmd(*arg)) 3928 { 3929 /* also complete "None" */ 3930 set_context_in_echohl_cmd(xp, arg); 3931 arg = skipwhite(skiptowhite(arg)); 3932 if (*arg != NUL) 3933 { 3934 xp->xp_context = EXPAND_NOTHING; 3935 arg = skip_regexp(arg + 1, *arg, p_magic, NULL); 3936 } 3937 } 3938 return find_nextcmd(arg); 3939 # endif 3940 3941 /* 3942 * All completion for the +cmdline_compl feature goes here. 3943 */ 3944 3945 # ifdef FEAT_USR_CMDS 3946 case CMD_command: 3947 /* Check for attributes */ 3948 while (*arg == '-') 3949 { 3950 arg++; /* Skip "-" */ 3951 p = skiptowhite(arg); 3952 if (*p == NUL) 3953 { 3954 /* Cursor is still in the attribute */ 3955 p = vim_strchr(arg, '='); 3956 if (p == NULL) 3957 { 3958 /* No "=", so complete attribute names */ 3959 xp->xp_context = EXPAND_USER_CMD_FLAGS; 3960 xp->xp_pattern = arg; 3961 return NULL; 3962 } 3963 3964 /* For the -complete, -nargs and -addr attributes, we complete 3965 * their arguments as well. 3966 */ 3967 if (STRNICMP(arg, "complete", p - arg) == 0) 3968 { 3969 xp->xp_context = EXPAND_USER_COMPLETE; 3970 xp->xp_pattern = p + 1; 3971 return NULL; 3972 } 3973 else if (STRNICMP(arg, "nargs", p - arg) == 0) 3974 { 3975 xp->xp_context = EXPAND_USER_NARGS; 3976 xp->xp_pattern = p + 1; 3977 return NULL; 3978 } 3979 else if (STRNICMP(arg, "addr", p - arg) == 0) 3980 { 3981 xp->xp_context = EXPAND_USER_ADDR_TYPE; 3982 xp->xp_pattern = p + 1; 3983 return NULL; 3984 } 3985 return NULL; 3986 } 3987 arg = skipwhite(p); 3988 } 3989 3990 /* After the attributes comes the new command name */ 3991 p = skiptowhite(arg); 3992 if (*p == NUL) 3993 { 3994 xp->xp_context = EXPAND_USER_COMMANDS; 3995 xp->xp_pattern = arg; 3996 break; 3997 } 3998 3999 /* And finally comes a normal command */ 4000 return skipwhite(p); 4001 4002 case CMD_delcommand: 4003 xp->xp_context = EXPAND_USER_COMMANDS; 4004 xp->xp_pattern = arg; 4005 break; 4006 # endif 4007 4008 case CMD_global: 4009 case CMD_vglobal: 4010 delim = *arg; /* get the delimiter */ 4011 if (delim) 4012 ++arg; /* skip delimiter if there is one */ 4013 4014 while (arg[0] != NUL && arg[0] != delim) 4015 { 4016 if (arg[0] == '\\' && arg[1] != NUL) 4017 ++arg; 4018 ++arg; 4019 } 4020 if (arg[0] != NUL) 4021 return arg + 1; 4022 break; 4023 case CMD_and: 4024 case CMD_substitute: 4025 delim = *arg; 4026 if (delim) 4027 { 4028 /* skip "from" part */ 4029 ++arg; 4030 arg = skip_regexp(arg, delim, p_magic, NULL); 4031 } 4032 /* skip "to" part */ 4033 while (arg[0] != NUL && arg[0] != delim) 4034 { 4035 if (arg[0] == '\\' && arg[1] != NUL) 4036 ++arg; 4037 ++arg; 4038 } 4039 if (arg[0] != NUL) /* skip delimiter */ 4040 ++arg; 4041 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL) 4042 ++arg; 4043 if (arg[0] != NUL) 4044 return arg; 4045 break; 4046 case CMD_isearch: 4047 case CMD_dsearch: 4048 case CMD_ilist: 4049 case CMD_dlist: 4050 case CMD_ijump: 4051 case CMD_psearch: 4052 case CMD_djump: 4053 case CMD_isplit: 4054 case CMD_dsplit: 4055 arg = skipwhite(skipdigits(arg)); /* skip count */ 4056 if (*arg == '/') /* Match regexp, not just whole words */ 4057 { 4058 for (++arg; *arg && *arg != '/'; arg++) 4059 if (*arg == '\\' && arg[1] != NUL) 4060 arg++; 4061 if (*arg) 4062 { 4063 arg = skipwhite(arg + 1); 4064 4065 /* Check for trailing illegal characters */ 4066 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL) 4067 xp->xp_context = EXPAND_NOTHING; 4068 else 4069 return arg; 4070 } 4071 } 4072 break; 4073 4074 case CMD_autocmd: 4075 return set_context_in_autocmd(xp, arg, FALSE); 4076 case CMD_doautocmd: 4077 case CMD_doautoall: 4078 return set_context_in_autocmd(xp, arg, TRUE); 4079 case CMD_set: 4080 set_context_in_set_cmd(xp, arg, 0); 4081 break; 4082 case CMD_setglobal: 4083 set_context_in_set_cmd(xp, arg, OPT_GLOBAL); 4084 break; 4085 case CMD_setlocal: 4086 set_context_in_set_cmd(xp, arg, OPT_LOCAL); 4087 break; 4088 case CMD_tag: 4089 case CMD_stag: 4090 case CMD_ptag: 4091 case CMD_ltag: 4092 case CMD_tselect: 4093 case CMD_stselect: 4094 case CMD_ptselect: 4095 case CMD_tjump: 4096 case CMD_stjump: 4097 case CMD_ptjump: 4098 if (*p_wop != NUL) 4099 xp->xp_context = EXPAND_TAGS_LISTFILES; 4100 else 4101 xp->xp_context = EXPAND_TAGS; 4102 xp->xp_pattern = arg; 4103 break; 4104 case CMD_augroup: 4105 xp->xp_context = EXPAND_AUGROUP; 4106 xp->xp_pattern = arg; 4107 break; 4108 #ifdef FEAT_SYN_HL 4109 case CMD_syntax: 4110 set_context_in_syntax_cmd(xp, arg); 4111 break; 4112 #endif 4113 #ifdef FEAT_EVAL 4114 case CMD_let: 4115 case CMD_if: 4116 case CMD_elseif: 4117 case CMD_while: 4118 case CMD_for: 4119 case CMD_echo: 4120 case CMD_echon: 4121 case CMD_execute: 4122 case CMD_echomsg: 4123 case CMD_echoerr: 4124 case CMD_call: 4125 case CMD_return: 4126 case CMD_cexpr: 4127 case CMD_caddexpr: 4128 case CMD_cgetexpr: 4129 case CMD_lexpr: 4130 case CMD_laddexpr: 4131 case CMD_lgetexpr: 4132 set_context_for_expression(xp, arg, ea.cmdidx); 4133 break; 4134 4135 case CMD_unlet: 4136 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) 4137 arg = xp->xp_pattern + 1; 4138 4139 xp->xp_context = EXPAND_USER_VARS; 4140 xp->xp_pattern = arg; 4141 4142 if (*xp->xp_pattern == '$') 4143 { 4144 xp->xp_context = EXPAND_ENV_VARS; 4145 ++xp->xp_pattern; 4146 } 4147 4148 break; 4149 4150 case CMD_function: 4151 case CMD_delfunction: 4152 xp->xp_context = EXPAND_USER_FUNC; 4153 xp->xp_pattern = arg; 4154 break; 4155 4156 case CMD_echohl: 4157 set_context_in_echohl_cmd(xp, arg); 4158 break; 4159 #endif 4160 case CMD_highlight: 4161 set_context_in_highlight_cmd(xp, arg); 4162 break; 4163 #ifdef FEAT_CSCOPE 4164 case CMD_cscope: 4165 case CMD_lcscope: 4166 case CMD_scscope: 4167 set_context_in_cscope_cmd(xp, arg, ea.cmdidx); 4168 break; 4169 #endif 4170 #ifdef FEAT_SIGNS 4171 case CMD_sign: 4172 set_context_in_sign_cmd(xp, arg); 4173 break; 4174 #endif 4175 case CMD_bdelete: 4176 case CMD_bwipeout: 4177 case CMD_bunload: 4178 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) 4179 arg = xp->xp_pattern + 1; 4180 /* FALLTHROUGH */ 4181 case CMD_buffer: 4182 case CMD_sbuffer: 4183 case CMD_checktime: 4184 xp->xp_context = EXPAND_BUFFERS; 4185 xp->xp_pattern = arg; 4186 break; 4187 #ifdef FEAT_USR_CMDS 4188 case CMD_USER: 4189 case CMD_USER_BUF: 4190 if (compl != EXPAND_NOTHING) 4191 { 4192 /* XFILE: file names are handled above */ 4193 if (!(ea.argt & XFILE)) 4194 { 4195 # ifdef FEAT_MENU 4196 if (compl == EXPAND_MENUS) 4197 return set_context_in_menu_cmd(xp, cmd, arg, forceit); 4198 # endif 4199 if (compl == EXPAND_COMMANDS) 4200 return arg; 4201 if (compl == EXPAND_MAPPINGS) 4202 return set_context_in_map_cmd(xp, (char_u *)"map", 4203 arg, forceit, FALSE, FALSE, CMD_map); 4204 /* Find start of last argument. */ 4205 p = arg; 4206 while (*p) 4207 { 4208 if (*p == ' ') 4209 /* argument starts after a space */ 4210 arg = p + 1; 4211 else if (*p == '\\' && *(p + 1) != NUL) 4212 ++p; /* skip over escaped character */ 4213 MB_PTR_ADV(p); 4214 } 4215 xp->xp_pattern = arg; 4216 } 4217 xp->xp_context = compl; 4218 } 4219 break; 4220 #endif 4221 case CMD_map: case CMD_noremap: 4222 case CMD_nmap: case CMD_nnoremap: 4223 case CMD_vmap: case CMD_vnoremap: 4224 case CMD_omap: case CMD_onoremap: 4225 case CMD_imap: case CMD_inoremap: 4226 case CMD_cmap: case CMD_cnoremap: 4227 case CMD_lmap: case CMD_lnoremap: 4228 case CMD_smap: case CMD_snoremap: 4229 case CMD_tmap: case CMD_tnoremap: 4230 case CMD_xmap: case CMD_xnoremap: 4231 return set_context_in_map_cmd(xp, cmd, arg, forceit, 4232 FALSE, FALSE, ea.cmdidx); 4233 case CMD_unmap: 4234 case CMD_nunmap: 4235 case CMD_vunmap: 4236 case CMD_ounmap: 4237 case CMD_iunmap: 4238 case CMD_cunmap: 4239 case CMD_lunmap: 4240 case CMD_sunmap: 4241 case CMD_tunmap: 4242 case CMD_xunmap: 4243 return set_context_in_map_cmd(xp, cmd, arg, forceit, 4244 FALSE, TRUE, ea.cmdidx); 4245 case CMD_mapclear: 4246 case CMD_nmapclear: 4247 case CMD_vmapclear: 4248 case CMD_omapclear: 4249 case CMD_imapclear: 4250 case CMD_cmapclear: 4251 case CMD_lmapclear: 4252 case CMD_smapclear: 4253 case CMD_tmapclear: 4254 case CMD_xmapclear: 4255 xp->xp_context = EXPAND_MAPCLEAR; 4256 xp->xp_pattern = arg; 4257 break; 4258 4259 case CMD_abbreviate: case CMD_noreabbrev: 4260 case CMD_cabbrev: case CMD_cnoreabbrev: 4261 case CMD_iabbrev: case CMD_inoreabbrev: 4262 return set_context_in_map_cmd(xp, cmd, arg, forceit, 4263 TRUE, FALSE, ea.cmdidx); 4264 case CMD_unabbreviate: 4265 case CMD_cunabbrev: 4266 case CMD_iunabbrev: 4267 return set_context_in_map_cmd(xp, cmd, arg, forceit, 4268 TRUE, TRUE, ea.cmdidx); 4269 #ifdef FEAT_MENU 4270 case CMD_menu: case CMD_noremenu: case CMD_unmenu: 4271 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu: 4272 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu: 4273 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu: 4274 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu: 4275 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu: 4276 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu: 4277 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu: 4278 case CMD_tmenu: case CMD_tunmenu: 4279 case CMD_popup: case CMD_tearoff: case CMD_emenu: 4280 return set_context_in_menu_cmd(xp, cmd, arg, forceit); 4281 #endif 4282 4283 case CMD_colorscheme: 4284 xp->xp_context = EXPAND_COLORS; 4285 xp->xp_pattern = arg; 4286 break; 4287 4288 case CMD_compiler: 4289 xp->xp_context = EXPAND_COMPILER; 4290 xp->xp_pattern = arg; 4291 break; 4292 4293 case CMD_ownsyntax: 4294 xp->xp_context = EXPAND_OWNSYNTAX; 4295 xp->xp_pattern = arg; 4296 break; 4297 4298 case CMD_setfiletype: 4299 xp->xp_context = EXPAND_FILETYPE; 4300 xp->xp_pattern = arg; 4301 break; 4302 4303 case CMD_packadd: 4304 xp->xp_context = EXPAND_PACKADD; 4305 xp->xp_pattern = arg; 4306 break; 4307 4308 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 4309 case CMD_language: 4310 p = skiptowhite(arg); 4311 if (*p == NUL) 4312 { 4313 xp->xp_context = EXPAND_LANGUAGE; 4314 xp->xp_pattern = arg; 4315 } 4316 else 4317 { 4318 if ( STRNCMP(arg, "messages", p - arg) == 0 4319 || STRNCMP(arg, "ctype", p - arg) == 0 4320 || STRNCMP(arg, "time", p - arg) == 0) 4321 { 4322 xp->xp_context = EXPAND_LOCALES; 4323 xp->xp_pattern = skipwhite(p); 4324 } 4325 else 4326 xp->xp_context = EXPAND_NOTHING; 4327 } 4328 break; 4329 #endif 4330 #if defined(FEAT_PROFILE) 4331 case CMD_profile: 4332 set_context_in_profile_cmd(xp, arg); 4333 break; 4334 #endif 4335 case CMD_behave: 4336 xp->xp_context = EXPAND_BEHAVE; 4337 xp->xp_pattern = arg; 4338 break; 4339 4340 case CMD_messages: 4341 xp->xp_context = EXPAND_MESSAGES; 4342 xp->xp_pattern = arg; 4343 break; 4344 4345 #if defined(FEAT_CMDHIST) 4346 case CMD_history: 4347 xp->xp_context = EXPAND_HISTORY; 4348 xp->xp_pattern = arg; 4349 break; 4350 #endif 4351 #if defined(FEAT_PROFILE) 4352 case CMD_syntime: 4353 xp->xp_context = EXPAND_SYNTIME; 4354 xp->xp_pattern = arg; 4355 break; 4356 #endif 4357 4358 case CMD_argdelete: 4359 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) 4360 arg = xp->xp_pattern + 1; 4361 xp->xp_context = EXPAND_ARGLIST; 4362 xp->xp_pattern = arg; 4363 break; 4364 4365 #endif /* FEAT_CMDL_COMPL */ 4366 4367 default: 4368 break; 4369 } 4370 return NULL; 4371 } 4372 4373 /* 4374 * Skip a range specifier of the form: addr [,addr] [;addr] .. 4375 * 4376 * Backslashed delimiters after / or ? will be skipped, and commands will 4377 * not be expanded between /'s and ?'s or after "'". 4378 * 4379 * Also skip white space and ":" characters. 4380 * Returns the "cmd" pointer advanced to beyond the range. 4381 */ 4382 char_u * 4383 skip_range( 4384 char_u *cmd, 4385 int *ctx) /* pointer to xp_context or NULL */ 4386 { 4387 unsigned delim; 4388 4389 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL) 4390 { 4391 if (*cmd == '\\') 4392 { 4393 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&') 4394 ++cmd; 4395 else 4396 break; 4397 } 4398 else if (*cmd == '\'') 4399 { 4400 if (*++cmd == NUL && ctx != NULL) 4401 *ctx = EXPAND_NOTHING; 4402 } 4403 else if (*cmd == '/' || *cmd == '?') 4404 { 4405 delim = *cmd++; 4406 while (*cmd != NUL && *cmd != delim) 4407 if (*cmd++ == '\\' && *cmd != NUL) 4408 ++cmd; 4409 if (*cmd == NUL && ctx != NULL) 4410 *ctx = EXPAND_NOTHING; 4411 } 4412 if (*cmd != NUL) 4413 ++cmd; 4414 } 4415 4416 /* Skip ":" and white space. */ 4417 while (*cmd == ':') 4418 cmd = skipwhite(cmd + 1); 4419 4420 return cmd; 4421 } 4422 4423 /* 4424 * Get a single EX address. 4425 * 4426 * Set ptr to the next character after the part that was interpreted. 4427 * Set ptr to NULL when an error is encountered. 4428 * This may set the last used search pattern. 4429 * 4430 * Return MAXLNUM when no Ex address was found. 4431 */ 4432 static linenr_T 4433 get_address( 4434 exarg_T *eap UNUSED, 4435 char_u **ptr, 4436 int addr_type, // flag: one of ADDR_LINES, ... 4437 int skip, // only skip the address, don't use it 4438 int silent, // no errors or side effects 4439 int to_other_file, // flag: may jump to other file 4440 int address_count UNUSED) // 1 for first address, >1 after comma 4441 { 4442 int c; 4443 int i; 4444 long n; 4445 char_u *cmd; 4446 pos_T pos; 4447 pos_T *fp; 4448 linenr_T lnum; 4449 buf_T *buf; 4450 4451 cmd = skipwhite(*ptr); 4452 lnum = MAXLNUM; 4453 do 4454 { 4455 switch (*cmd) 4456 { 4457 case '.': /* '.' - Cursor position */ 4458 ++cmd; 4459 switch (addr_type) 4460 { 4461 case ADDR_LINES: 4462 lnum = curwin->w_cursor.lnum; 4463 break; 4464 case ADDR_WINDOWS: 4465 lnum = CURRENT_WIN_NR; 4466 break; 4467 case ADDR_ARGUMENTS: 4468 lnum = curwin->w_arg_idx + 1; 4469 break; 4470 case ADDR_LOADED_BUFFERS: 4471 case ADDR_BUFFERS: 4472 lnum = curbuf->b_fnum; 4473 break; 4474 case ADDR_TABS: 4475 lnum = CURRENT_TAB_NR; 4476 break; 4477 case ADDR_TABS_RELATIVE: 4478 emsg(_(e_invrange)); 4479 cmd = NULL; 4480 goto error; 4481 break; 4482 #ifdef FEAT_QUICKFIX 4483 case ADDR_QUICKFIX: 4484 lnum = qf_get_cur_valid_idx(eap); 4485 break; 4486 #endif 4487 } 4488 break; 4489 4490 case '$': /* '$' - last line */ 4491 ++cmd; 4492 switch (addr_type) 4493 { 4494 case ADDR_LINES: 4495 lnum = curbuf->b_ml.ml_line_count; 4496 break; 4497 case ADDR_WINDOWS: 4498 lnum = LAST_WIN_NR; 4499 break; 4500 case ADDR_ARGUMENTS: 4501 lnum = ARGCOUNT; 4502 break; 4503 case ADDR_LOADED_BUFFERS: 4504 buf = lastbuf; 4505 while (buf->b_ml.ml_mfp == NULL) 4506 { 4507 if (buf->b_prev == NULL) 4508 break; 4509 buf = buf->b_prev; 4510 } 4511 lnum = buf->b_fnum; 4512 break; 4513 case ADDR_BUFFERS: 4514 lnum = lastbuf->b_fnum; 4515 break; 4516 case ADDR_TABS: 4517 lnum = LAST_TAB_NR; 4518 break; 4519 case ADDR_TABS_RELATIVE: 4520 emsg(_(e_invrange)); 4521 cmd = NULL; 4522 goto error; 4523 break; 4524 #ifdef FEAT_QUICKFIX 4525 case ADDR_QUICKFIX: 4526 lnum = qf_get_size(eap); 4527 if (lnum == 0) 4528 lnum = 1; 4529 break; 4530 #endif 4531 } 4532 break; 4533 4534 case '\'': /* ''' - mark */ 4535 if (*++cmd == NUL) 4536 { 4537 cmd = NULL; 4538 goto error; 4539 } 4540 if (addr_type != ADDR_LINES) 4541 { 4542 emsg(_(e_invaddr)); 4543 cmd = NULL; 4544 goto error; 4545 } 4546 if (skip) 4547 ++cmd; 4548 else 4549 { 4550 /* Only accept a mark in another file when it is 4551 * used by itself: ":'M". */ 4552 fp = getmark(*cmd, to_other_file && cmd[1] == NUL); 4553 ++cmd; 4554 if (fp == (pos_T *)-1) 4555 /* Jumped to another file. */ 4556 lnum = curwin->w_cursor.lnum; 4557 else 4558 { 4559 if (check_mark(fp) == FAIL) 4560 { 4561 cmd = NULL; 4562 goto error; 4563 } 4564 lnum = fp->lnum; 4565 } 4566 } 4567 break; 4568 4569 case '/': 4570 case '?': /* '/' or '?' - search */ 4571 c = *cmd++; 4572 if (addr_type != ADDR_LINES) 4573 { 4574 emsg(_(e_invaddr)); 4575 cmd = NULL; 4576 goto error; 4577 } 4578 if (skip) /* skip "/pat/" */ 4579 { 4580 cmd = skip_regexp(cmd, c, (int)p_magic, NULL); 4581 if (*cmd == c) 4582 ++cmd; 4583 } 4584 else 4585 { 4586 int flags; 4587 4588 pos = curwin->w_cursor; // save curwin->w_cursor 4589 4590 // When '/' or '?' follows another address, start from 4591 // there. 4592 if (lnum != MAXLNUM) 4593 curwin->w_cursor.lnum = lnum; 4594 4595 // Start a forward search at the end of the line (unless 4596 // before the first line). 4597 // Start a backward search at the start of the line. 4598 // This makes sure we never match in the current 4599 // line, and can match anywhere in the 4600 // next/previous line. 4601 if (c == '/' && curwin->w_cursor.lnum > 0) 4602 curwin->w_cursor.col = MAXCOL; 4603 else 4604 curwin->w_cursor.col = 0; 4605 searchcmdlen = 0; 4606 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG; 4607 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL)) 4608 { 4609 curwin->w_cursor = pos; 4610 cmd = NULL; 4611 goto error; 4612 } 4613 lnum = curwin->w_cursor.lnum; 4614 curwin->w_cursor = pos; 4615 /* adjust command string pointer */ 4616 cmd += searchcmdlen; 4617 } 4618 break; 4619 4620 case '\\': /* "\?", "\/" or "\&", repeat search */ 4621 ++cmd; 4622 if (addr_type != ADDR_LINES) 4623 { 4624 emsg(_(e_invaddr)); 4625 cmd = NULL; 4626 goto error; 4627 } 4628 if (*cmd == '&') 4629 i = RE_SUBST; 4630 else if (*cmd == '?' || *cmd == '/') 4631 i = RE_SEARCH; 4632 else 4633 { 4634 emsg(_(e_backslash)); 4635 cmd = NULL; 4636 goto error; 4637 } 4638 4639 if (!skip) 4640 { 4641 /* 4642 * When search follows another address, start from 4643 * there. 4644 */ 4645 if (lnum != MAXLNUM) 4646 pos.lnum = lnum; 4647 else 4648 pos.lnum = curwin->w_cursor.lnum; 4649 4650 /* 4651 * Start the search just like for the above 4652 * do_search(). 4653 */ 4654 if (*cmd != '?') 4655 pos.col = MAXCOL; 4656 else 4657 pos.col = 0; 4658 pos.coladd = 0; 4659 if (searchit(curwin, curbuf, &pos, NULL, 4660 *cmd == '?' ? BACKWARD : FORWARD, 4661 (char_u *)"", 1L, SEARCH_MSG, 4662 i, (linenr_T)0, NULL, NULL) != FAIL) 4663 lnum = pos.lnum; 4664 else 4665 { 4666 cmd = NULL; 4667 goto error; 4668 } 4669 } 4670 ++cmd; 4671 break; 4672 4673 default: 4674 if (VIM_ISDIGIT(*cmd)) /* absolute line number */ 4675 lnum = getdigits(&cmd); 4676 } 4677 4678 for (;;) 4679 { 4680 cmd = skipwhite(cmd); 4681 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd)) 4682 break; 4683 4684 if (lnum == MAXLNUM) 4685 { 4686 switch (addr_type) 4687 { 4688 case ADDR_LINES: 4689 /* "+1" is same as ".+1" */ 4690 lnum = curwin->w_cursor.lnum; 4691 break; 4692 case ADDR_WINDOWS: 4693 lnum = CURRENT_WIN_NR; 4694 break; 4695 case ADDR_ARGUMENTS: 4696 lnum = curwin->w_arg_idx + 1; 4697 break; 4698 case ADDR_LOADED_BUFFERS: 4699 case ADDR_BUFFERS: 4700 lnum = curbuf->b_fnum; 4701 break; 4702 case ADDR_TABS: 4703 lnum = CURRENT_TAB_NR; 4704 break; 4705 case ADDR_TABS_RELATIVE: 4706 lnum = 1; 4707 break; 4708 #ifdef FEAT_QUICKFIX 4709 case ADDR_QUICKFIX: 4710 lnum = qf_get_cur_valid_idx(eap); 4711 break; 4712 #endif 4713 } 4714 } 4715 4716 if (VIM_ISDIGIT(*cmd)) 4717 i = '+'; /* "number" is same as "+number" */ 4718 else 4719 i = *cmd++; 4720 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */ 4721 n = 1; 4722 else 4723 n = getdigits(&cmd); 4724 4725 if (addr_type == ADDR_TABS_RELATIVE) 4726 { 4727 emsg(_(e_invrange)); 4728 cmd = NULL; 4729 goto error; 4730 } 4731 else if (addr_type == ADDR_LOADED_BUFFERS 4732 || addr_type == ADDR_BUFFERS) 4733 lnum = compute_buffer_local_count( 4734 addr_type, lnum, (i == '-') ? -1 * n : n); 4735 else 4736 { 4737 #ifdef FEAT_FOLDING 4738 /* Relative line addressing, need to adjust for folded lines 4739 * now, but only do it after the first address. */ 4740 if (addr_type == ADDR_LINES && (i == '-' || i == '+') 4741 && address_count >= 2) 4742 (void)hasFolding(lnum, NULL, &lnum); 4743 #endif 4744 if (i == '-') 4745 lnum -= n; 4746 else 4747 lnum += n; 4748 } 4749 } 4750 } while (*cmd == '/' || *cmd == '?'); 4751 4752 error: 4753 *ptr = cmd; 4754 return lnum; 4755 } 4756 4757 /* 4758 * Get flags from an Ex command argument. 4759 */ 4760 static void 4761 get_flags(exarg_T *eap) 4762 { 4763 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL) 4764 { 4765 if (*eap->arg == 'l') 4766 eap->flags |= EXFLAG_LIST; 4767 else if (*eap->arg == 'p') 4768 eap->flags |= EXFLAG_PRINT; 4769 else 4770 eap->flags |= EXFLAG_NR; 4771 eap->arg = skipwhite(eap->arg + 1); 4772 } 4773 } 4774 4775 /* 4776 * Function called for command which is Not Implemented. NI! 4777 */ 4778 void 4779 ex_ni(exarg_T *eap) 4780 { 4781 if (!eap->skip) 4782 eap->errmsg = N_("E319: Sorry, the command is not available in this version"); 4783 } 4784 4785 #ifdef HAVE_EX_SCRIPT_NI 4786 /* 4787 * Function called for script command which is Not Implemented. NI! 4788 * Skips over ":perl <<EOF" constructs. 4789 */ 4790 static void 4791 ex_script_ni(exarg_T *eap) 4792 { 4793 if (!eap->skip) 4794 ex_ni(eap); 4795 else 4796 vim_free(script_get(eap, eap->arg)); 4797 } 4798 #endif 4799 4800 /* 4801 * Check range in Ex command for validity. 4802 * Return NULL when valid, error message when invalid. 4803 */ 4804 static char * 4805 invalid_range(exarg_T *eap) 4806 { 4807 buf_T *buf; 4808 if ( eap->line1 < 0 4809 || eap->line2 < 0 4810 || eap->line1 > eap->line2) 4811 return _(e_invrange); 4812 4813 if (eap->argt & RANGE) 4814 { 4815 switch(eap->addr_type) 4816 { 4817 case ADDR_LINES: 4818 if (!(eap->argt & NOTADR) 4819 && eap->line2 > curbuf->b_ml.ml_line_count 4820 #ifdef FEAT_DIFF 4821 + (eap->cmdidx == CMD_diffget) 4822 #endif 4823 ) 4824 return _(e_invrange); 4825 break; 4826 case ADDR_ARGUMENTS: 4827 /* add 1 if ARGCOUNT is 0 */ 4828 if (eap->line2 > ARGCOUNT + (!ARGCOUNT)) 4829 return _(e_invrange); 4830 break; 4831 case ADDR_BUFFERS: 4832 if (eap->line1 < firstbuf->b_fnum 4833 || eap->line2 > lastbuf->b_fnum) 4834 return _(e_invrange); 4835 break; 4836 case ADDR_LOADED_BUFFERS: 4837 buf = firstbuf; 4838 while (buf->b_ml.ml_mfp == NULL) 4839 { 4840 if (buf->b_next == NULL) 4841 return _(e_invrange); 4842 buf = buf->b_next; 4843 } 4844 if (eap->line1 < buf->b_fnum) 4845 return _(e_invrange); 4846 buf = lastbuf; 4847 while (buf->b_ml.ml_mfp == NULL) 4848 { 4849 if (buf->b_prev == NULL) 4850 return _(e_invrange); 4851 buf = buf->b_prev; 4852 } 4853 if (eap->line2 > buf->b_fnum) 4854 return _(e_invrange); 4855 break; 4856 case ADDR_WINDOWS: 4857 if (eap->line2 > LAST_WIN_NR) 4858 return _(e_invrange); 4859 break; 4860 case ADDR_TABS: 4861 if (eap->line2 > LAST_TAB_NR) 4862 return _(e_invrange); 4863 break; 4864 case ADDR_TABS_RELATIVE: 4865 /* Do nothing */ 4866 break; 4867 #ifdef FEAT_QUICKFIX 4868 case ADDR_QUICKFIX: 4869 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap)) 4870 return _(e_invrange); 4871 break; 4872 #endif 4873 } 4874 } 4875 return NULL; 4876 } 4877 4878 /* 4879 * Correct the range for zero line number, if required. 4880 */ 4881 static void 4882 correct_range(exarg_T *eap) 4883 { 4884 if (!(eap->argt & ZEROR)) /* zero in range not allowed */ 4885 { 4886 if (eap->line1 == 0) 4887 eap->line1 = 1; 4888 if (eap->line2 == 0) 4889 eap->line2 = 1; 4890 } 4891 } 4892 4893 #ifdef FEAT_QUICKFIX 4894 /* 4895 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the 4896 * pattern. Otherwise return eap->arg. 4897 */ 4898 static char_u * 4899 skip_grep_pat(exarg_T *eap) 4900 { 4901 char_u *p = eap->arg; 4902 4903 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep 4904 || eap->cmdidx == CMD_vimgrepadd 4905 || eap->cmdidx == CMD_lvimgrepadd 4906 || grep_internal(eap->cmdidx))) 4907 { 4908 p = skip_vimgrep_pat(p, NULL, NULL); 4909 if (p == NULL) 4910 p = eap->arg; 4911 } 4912 return p; 4913 } 4914 4915 /* 4916 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option 4917 * in the command line, so that things like % get expanded. 4918 */ 4919 static char_u * 4920 replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep) 4921 { 4922 char_u *new_cmdline; 4923 char_u *program; 4924 char_u *pos; 4925 char_u *ptr; 4926 int len; 4927 int i; 4928 4929 /* 4930 * Don't do it when ":vimgrep" is used for ":grep". 4931 */ 4932 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake 4933 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep 4934 || eap->cmdidx == CMD_grepadd 4935 || eap->cmdidx == CMD_lgrepadd) 4936 && !grep_internal(eap->cmdidx)) 4937 { 4938 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep 4939 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd) 4940 { 4941 if (*curbuf->b_p_gp == NUL) 4942 program = p_gp; 4943 else 4944 program = curbuf->b_p_gp; 4945 } 4946 else 4947 { 4948 if (*curbuf->b_p_mp == NUL) 4949 program = p_mp; 4950 else 4951 program = curbuf->b_p_mp; 4952 } 4953 4954 p = skipwhite(p); 4955 4956 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) 4957 { 4958 /* replace $* by given arguments */ 4959 i = 1; 4960 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL) 4961 ++i; 4962 len = (int)STRLEN(p); 4963 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1)); 4964 if (new_cmdline == NULL) 4965 return NULL; /* out of memory */ 4966 ptr = new_cmdline; 4967 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) 4968 { 4969 i = (int)(pos - program); 4970 STRNCPY(ptr, program, i); 4971 STRCPY(ptr += i, p); 4972 ptr += len; 4973 program = pos + 2; 4974 } 4975 STRCPY(ptr, program); 4976 } 4977 else 4978 { 4979 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2)); 4980 if (new_cmdline == NULL) 4981 return NULL; /* out of memory */ 4982 STRCPY(new_cmdline, program); 4983 STRCAT(new_cmdline, " "); 4984 STRCAT(new_cmdline, p); 4985 } 4986 msg_make(p); 4987 4988 /* 'eap->cmd' is not set here, because it is not used at CMD_make */ 4989 vim_free(*cmdlinep); 4990 *cmdlinep = new_cmdline; 4991 p = new_cmdline; 4992 } 4993 return p; 4994 } 4995 #endif 4996 4997 /* 4998 * Expand file name in Ex command argument. 4999 * Return FAIL for failure, OK otherwise. 5000 */ 5001 int 5002 expand_filename( 5003 exarg_T *eap, 5004 char_u **cmdlinep, 5005 char **errormsgp) 5006 { 5007 int has_wildcards; /* need to expand wildcards */ 5008 char_u *repl; 5009 int srclen; 5010 char_u *p; 5011 int n; 5012 int escaped; 5013 5014 #ifdef FEAT_QUICKFIX 5015 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */ 5016 p = skip_grep_pat(eap); 5017 #else 5018 p = eap->arg; 5019 #endif 5020 5021 /* 5022 * Decide to expand wildcards *before* replacing '%', '#', etc. If 5023 * the file name contains a wildcard it should not cause expanding. 5024 * (it will be expanded anyway if there is a wildcard before replacing). 5025 */ 5026 has_wildcards = mch_has_wildcard(p); 5027 while (*p != NUL) 5028 { 5029 #ifdef FEAT_EVAL 5030 /* Skip over `=expr`, wildcards in it are not expanded. */ 5031 if (p[0] == '`' && p[1] == '=') 5032 { 5033 p += 2; 5034 (void)skip_expr(&p); 5035 if (*p == '`') 5036 ++p; 5037 continue; 5038 } 5039 #endif 5040 /* 5041 * Quick check if this cannot be the start of a special string. 5042 * Also removes backslash before '%', '#' and '<'. 5043 */ 5044 if (vim_strchr((char_u *)"%#<", *p) == NULL) 5045 { 5046 ++p; 5047 continue; 5048 } 5049 5050 /* 5051 * Try to find a match at this position. 5052 */ 5053 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum), 5054 errormsgp, &escaped); 5055 if (*errormsgp != NULL) /* error detected */ 5056 return FAIL; 5057 if (repl == NULL) /* no match found */ 5058 { 5059 p += srclen; 5060 continue; 5061 } 5062 5063 /* Wildcards won't be expanded below, the replacement is taken 5064 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */ 5065 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL) 5066 { 5067 char_u *l = repl; 5068 5069 repl = expand_env_save(repl); 5070 vim_free(l); 5071 } 5072 5073 /* Need to escape white space et al. with a backslash. 5074 * Don't do this for: 5075 * - replacement that already has been escaped: "##" 5076 * - shell commands (may have to use quotes instead). 5077 * - non-unix systems when there is a single argument (spaces don't 5078 * separate arguments then). 5079 */ 5080 if (!eap->usefilter 5081 && !escaped 5082 && eap->cmdidx != CMD_bang 5083 && eap->cmdidx != CMD_grep 5084 && eap->cmdidx != CMD_grepadd 5085 && eap->cmdidx != CMD_hardcopy 5086 && eap->cmdidx != CMD_lgrep 5087 && eap->cmdidx != CMD_lgrepadd 5088 && eap->cmdidx != CMD_lmake 5089 && eap->cmdidx != CMD_make 5090 && eap->cmdidx != CMD_terminal 5091 #ifndef UNIX 5092 && !(eap->argt & NOSPC) 5093 #endif 5094 ) 5095 { 5096 char_u *l; 5097 #ifdef BACKSLASH_IN_FILENAME 5098 /* Don't escape a backslash here, because rem_backslash() doesn't 5099 * remove it later. */ 5100 static char_u *nobslash = (char_u *)" \t\"|"; 5101 # define ESCAPE_CHARS nobslash 5102 #else 5103 # define ESCAPE_CHARS escape_chars 5104 #endif 5105 5106 for (l = repl; *l; ++l) 5107 if (vim_strchr(ESCAPE_CHARS, *l) != NULL) 5108 { 5109 l = vim_strsave_escaped(repl, ESCAPE_CHARS); 5110 if (l != NULL) 5111 { 5112 vim_free(repl); 5113 repl = l; 5114 } 5115 break; 5116 } 5117 } 5118 5119 /* For a shell command a '!' must be escaped. */ 5120 if ((eap->usefilter || eap->cmdidx == CMD_bang 5121 || eap->cmdidx == CMD_terminal) 5122 && vim_strpbrk(repl, (char_u *)"!") != NULL) 5123 { 5124 char_u *l; 5125 5126 l = vim_strsave_escaped(repl, (char_u *)"!"); 5127 if (l != NULL) 5128 { 5129 vim_free(repl); 5130 repl = l; 5131 } 5132 } 5133 5134 p = repl_cmdline(eap, p, srclen, repl, cmdlinep); 5135 vim_free(repl); 5136 if (p == NULL) 5137 return FAIL; 5138 } 5139 5140 /* 5141 * One file argument: Expand wildcards. 5142 * Don't do this with ":r !command" or ":w !command". 5143 */ 5144 if ((eap->argt & NOSPC) && !eap->usefilter) 5145 { 5146 /* 5147 * May do this twice: 5148 * 1. Replace environment variables. 5149 * 2. Replace any other wildcards, remove backslashes. 5150 */ 5151 for (n = 1; n <= 2; ++n) 5152 { 5153 if (n == 2) 5154 { 5155 /* 5156 * Halve the number of backslashes (this is Vi compatible). 5157 * For Unix and OS/2, when wildcards are expanded, this is 5158 * done by ExpandOne() below. 5159 */ 5160 #if defined(UNIX) 5161 if (!has_wildcards) 5162 #endif 5163 backslash_halve(eap->arg); 5164 } 5165 5166 if (has_wildcards) 5167 { 5168 if (n == 1) 5169 { 5170 /* 5171 * First loop: May expand environment variables. This 5172 * can be done much faster with expand_env() than with 5173 * something else (e.g., calling a shell). 5174 * After expanding environment variables, check again 5175 * if there are still wildcards present. 5176 */ 5177 if (vim_strchr(eap->arg, '$') != NULL 5178 || vim_strchr(eap->arg, '~') != NULL) 5179 { 5180 expand_env_esc(eap->arg, NameBuff, MAXPATHL, 5181 TRUE, TRUE, NULL); 5182 has_wildcards = mch_has_wildcard(NameBuff); 5183 p = NameBuff; 5184 } 5185 else 5186 p = NULL; 5187 } 5188 else /* n == 2 */ 5189 { 5190 expand_T xpc; 5191 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH; 5192 5193 ExpandInit(&xpc); 5194 xpc.xp_context = EXPAND_FILES; 5195 if (p_wic) 5196 options += WILD_ICASE; 5197 p = ExpandOne(&xpc, eap->arg, NULL, 5198 options, WILD_EXPAND_FREE); 5199 if (p == NULL) 5200 return FAIL; 5201 } 5202 if (p != NULL) 5203 { 5204 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg), 5205 p, cmdlinep); 5206 if (n == 2) /* p came from ExpandOne() */ 5207 vim_free(p); 5208 } 5209 } 5210 } 5211 } 5212 return OK; 5213 } 5214 5215 /* 5216 * Replace part of the command line, keeping eap->cmd, eap->arg and 5217 * eap->nextcmd correct. 5218 * "src" points to the part that is to be replaced, of length "srclen". 5219 * "repl" is the replacement string. 5220 * Returns a pointer to the character after the replaced string. 5221 * Returns NULL for failure. 5222 */ 5223 static char_u * 5224 repl_cmdline( 5225 exarg_T *eap, 5226 char_u *src, 5227 int srclen, 5228 char_u *repl, 5229 char_u **cmdlinep) 5230 { 5231 int len; 5232 int i; 5233 char_u *new_cmdline; 5234 5235 /* 5236 * The new command line is build in new_cmdline[]. 5237 * First allocate it. 5238 * Careful: a "+cmd" argument may have been NUL terminated. 5239 */ 5240 len = (int)STRLEN(repl); 5241 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3; 5242 if (eap->nextcmd != NULL) 5243 i += (int)STRLEN(eap->nextcmd);/* add space for next command */ 5244 if ((new_cmdline = alloc((unsigned)i)) == NULL) 5245 return NULL; /* out of memory! */ 5246 5247 /* 5248 * Copy the stuff before the expanded part. 5249 * Copy the expanded stuff. 5250 * Copy what came after the expanded part. 5251 * Copy the next commands, if there are any. 5252 */ 5253 i = (int)(src - *cmdlinep); /* length of part before match */ 5254 mch_memmove(new_cmdline, *cmdlinep, (size_t)i); 5255 5256 mch_memmove(new_cmdline + i, repl, (size_t)len); 5257 i += len; /* remember the end of the string */ 5258 STRCPY(new_cmdline + i, src + srclen); 5259 src = new_cmdline + i; /* remember where to continue */ 5260 5261 if (eap->nextcmd != NULL) /* append next command */ 5262 { 5263 i = (int)STRLEN(new_cmdline) + 1; 5264 STRCPY(new_cmdline + i, eap->nextcmd); 5265 eap->nextcmd = new_cmdline + i; 5266 } 5267 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep); 5268 eap->arg = new_cmdline + (eap->arg - *cmdlinep); 5269 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command) 5270 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep); 5271 vim_free(*cmdlinep); 5272 *cmdlinep = new_cmdline; 5273 5274 return src; 5275 } 5276 5277 /* 5278 * Check for '|' to separate commands and '"' to start comments. 5279 */ 5280 void 5281 separate_nextcmd(exarg_T *eap) 5282 { 5283 char_u *p; 5284 5285 #ifdef FEAT_QUICKFIX 5286 p = skip_grep_pat(eap); 5287 #else 5288 p = eap->arg; 5289 #endif 5290 5291 for ( ; *p; MB_PTR_ADV(p)) 5292 { 5293 if (*p == Ctrl_V) 5294 { 5295 if (eap->argt & (USECTRLV | XFILE)) 5296 ++p; /* skip CTRL-V and next char */ 5297 else 5298 /* remove CTRL-V and skip next char */ 5299 STRMOVE(p, p + 1); 5300 if (*p == NUL) /* stop at NUL after CTRL-V */ 5301 break; 5302 } 5303 5304 #ifdef FEAT_EVAL 5305 /* Skip over `=expr` when wildcards are expanded. */ 5306 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE)) 5307 { 5308 p += 2; 5309 (void)skip_expr(&p); 5310 } 5311 #endif 5312 5313 /* Check for '"': start of comment or '|': next command */ 5314 /* :@" and :*" do not start a comment! 5315 * :redir @" doesn't either. */ 5316 else if ((*p == '"' && !(eap->argt & NOTRLCOM) 5317 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star) 5318 || p != eap->arg) 5319 && (eap->cmdidx != CMD_redir 5320 || p != eap->arg + 1 || p[-1] != '@')) 5321 || *p == '|' || *p == '\n') 5322 { 5323 /* 5324 * We remove the '\' before the '|', unless USECTRLV is used 5325 * AND 'b' is present in 'cpoptions'. 5326 */ 5327 if ((vim_strchr(p_cpo, CPO_BAR) == NULL 5328 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\') 5329 { 5330 STRMOVE(p - 1, p); /* remove the '\' */ 5331 --p; 5332 } 5333 else 5334 { 5335 eap->nextcmd = check_nextcmd(p); 5336 *p = NUL; 5337 break; 5338 } 5339 } 5340 } 5341 5342 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */ 5343 del_trailing_spaces(eap->arg); 5344 } 5345 5346 /* 5347 * get + command from ex argument 5348 */ 5349 static char_u * 5350 getargcmd(char_u **argp) 5351 { 5352 char_u *arg = *argp; 5353 char_u *command = NULL; 5354 5355 if (*arg == '+') /* +[command] */ 5356 { 5357 ++arg; 5358 if (vim_isspace(*arg) || *arg == NUL) 5359 command = dollar_command; 5360 else 5361 { 5362 command = arg; 5363 arg = skip_cmd_arg(command, TRUE); 5364 if (*arg != NUL) 5365 *arg++ = NUL; /* terminate command with NUL */ 5366 } 5367 5368 arg = skipwhite(arg); /* skip over spaces */ 5369 *argp = arg; 5370 } 5371 return command; 5372 } 5373 5374 /* 5375 * Find end of "+command" argument. Skip over "\ " and "\\". 5376 */ 5377 static char_u * 5378 skip_cmd_arg( 5379 char_u *p, 5380 int rembs) /* TRUE to halve the number of backslashes */ 5381 { 5382 while (*p && !vim_isspace(*p)) 5383 { 5384 if (*p == '\\' && p[1] != NUL) 5385 { 5386 if (rembs) 5387 STRMOVE(p, p + 1); 5388 else 5389 ++p; 5390 } 5391 MB_PTR_ADV(p); 5392 } 5393 return p; 5394 } 5395 5396 int 5397 get_bad_opt(char_u *p, exarg_T *eap) 5398 { 5399 if (STRICMP(p, "keep") == 0) 5400 eap->bad_char = BAD_KEEP; 5401 else if (STRICMP(p, "drop") == 0) 5402 eap->bad_char = BAD_DROP; 5403 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL) 5404 eap->bad_char = *p; 5405 else 5406 return FAIL; 5407 return OK; 5408 } 5409 5410 /* 5411 * Get "++opt=arg" argument. 5412 * Return FAIL or OK. 5413 */ 5414 static int 5415 getargopt(exarg_T *eap) 5416 { 5417 char_u *arg = eap->arg + 2; 5418 int *pp = NULL; 5419 int bad_char_idx; 5420 char_u *p; 5421 5422 /* ":edit ++[no]bin[ary] file" */ 5423 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0) 5424 { 5425 if (*arg == 'n') 5426 { 5427 arg += 2; 5428 eap->force_bin = FORCE_NOBIN; 5429 } 5430 else 5431 eap->force_bin = FORCE_BIN; 5432 if (!checkforcmd(&arg, "binary", 3)) 5433 return FAIL; 5434 eap->arg = skipwhite(arg); 5435 return OK; 5436 } 5437 5438 /* ":read ++edit file" */ 5439 if (STRNCMP(arg, "edit", 4) == 0) 5440 { 5441 eap->read_edit = TRUE; 5442 eap->arg = skipwhite(arg + 4); 5443 return OK; 5444 } 5445 5446 if (STRNCMP(arg, "ff", 2) == 0) 5447 { 5448 arg += 2; 5449 pp = &eap->force_ff; 5450 } 5451 else if (STRNCMP(arg, "fileformat", 10) == 0) 5452 { 5453 arg += 10; 5454 pp = &eap->force_ff; 5455 } 5456 else if (STRNCMP(arg, "enc", 3) == 0) 5457 { 5458 if (STRNCMP(arg, "encoding", 8) == 0) 5459 arg += 8; 5460 else 5461 arg += 3; 5462 pp = &eap->force_enc; 5463 } 5464 else if (STRNCMP(arg, "bad", 3) == 0) 5465 { 5466 arg += 3; 5467 pp = &bad_char_idx; 5468 } 5469 5470 if (pp == NULL || *arg != '=') 5471 return FAIL; 5472 5473 ++arg; 5474 *pp = (int)(arg - eap->cmd); 5475 arg = skip_cmd_arg(arg, FALSE); 5476 eap->arg = skipwhite(arg); 5477 *arg = NUL; 5478 5479 if (pp == &eap->force_ff) 5480 { 5481 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL) 5482 return FAIL; 5483 eap->force_ff = eap->cmd[eap->force_ff]; 5484 } 5485 else if (pp == &eap->force_enc) 5486 { 5487 /* Make 'fileencoding' lower case. */ 5488 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p) 5489 *p = TOLOWER_ASC(*p); 5490 } 5491 else 5492 { 5493 /* Check ++bad= argument. Must be a single-byte character, "keep" or 5494 * "drop". */ 5495 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL) 5496 return FAIL; 5497 } 5498 5499 return OK; 5500 } 5501 5502 /* 5503 * ":abbreviate" and friends. 5504 */ 5505 static void 5506 ex_abbreviate(exarg_T *eap) 5507 { 5508 do_exmap(eap, TRUE); /* almost the same as mapping */ 5509 } 5510 5511 /* 5512 * ":map" and friends. 5513 */ 5514 static void 5515 ex_map(exarg_T *eap) 5516 { 5517 /* 5518 * If we are sourcing .exrc or .vimrc in current directory we 5519 * print the mappings for security reasons. 5520 */ 5521 if (secure) 5522 { 5523 secure = 2; 5524 msg_outtrans(eap->cmd); 5525 msg_putchar('\n'); 5526 } 5527 do_exmap(eap, FALSE); 5528 } 5529 5530 /* 5531 * ":unmap" and friends. 5532 */ 5533 static void 5534 ex_unmap(exarg_T *eap) 5535 { 5536 do_exmap(eap, FALSE); 5537 } 5538 5539 /* 5540 * ":mapclear" and friends. 5541 */ 5542 static void 5543 ex_mapclear(exarg_T *eap) 5544 { 5545 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE); 5546 } 5547 5548 /* 5549 * ":abclear" and friends. 5550 */ 5551 static void 5552 ex_abclear(exarg_T *eap) 5553 { 5554 map_clear(eap->cmd, eap->arg, TRUE, TRUE); 5555 } 5556 5557 static void 5558 ex_autocmd(exarg_T *eap) 5559 { 5560 /* 5561 * Disallow autocommands from .exrc and .vimrc in current 5562 * directory for security reasons. 5563 */ 5564 if (secure) 5565 { 5566 secure = 2; 5567 eap->errmsg = e_curdir; 5568 } 5569 else if (eap->cmdidx == CMD_autocmd) 5570 do_autocmd(eap->arg, eap->forceit); 5571 else 5572 do_augroup(eap->arg, eap->forceit); 5573 } 5574 5575 /* 5576 * ":doautocmd": Apply the automatic commands to the current buffer. 5577 */ 5578 static void 5579 ex_doautocmd(exarg_T *eap) 5580 { 5581 char_u *arg = eap->arg; 5582 int call_do_modelines = check_nomodeline(&arg); 5583 int did_aucmd; 5584 5585 (void)do_doautocmd(arg, TRUE, &did_aucmd); 5586 /* Only when there is no <nomodeline>. */ 5587 if (call_do_modelines && did_aucmd) 5588 do_modelines(0); 5589 } 5590 5591 /* 5592 * :[N]bunload[!] [N] [bufname] unload buffer 5593 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list 5594 * :[N]bwipeout[!] [N] [bufname] delete buffer really 5595 */ 5596 static void 5597 ex_bunload(exarg_T *eap) 5598 { 5599 eap->errmsg = do_bufdel( 5600 eap->cmdidx == CMD_bdelete ? DOBUF_DEL 5601 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE 5602 : DOBUF_UNLOAD, eap->arg, 5603 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit); 5604 } 5605 5606 /* 5607 * :[N]buffer [N] to buffer N 5608 * :[N]sbuffer [N] to buffer N 5609 */ 5610 static void 5611 ex_buffer(exarg_T *eap) 5612 { 5613 if (*eap->arg) 5614 eap->errmsg = e_trailing; 5615 else 5616 { 5617 if (eap->addr_count == 0) /* default is current buffer */ 5618 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0); 5619 else 5620 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2); 5621 if (eap->do_ecmd_cmd != NULL) 5622 do_cmdline_cmd(eap->do_ecmd_cmd); 5623 } 5624 } 5625 5626 /* 5627 * :[N]bmodified [N] to next mod. buffer 5628 * :[N]sbmodified [N] to next mod. buffer 5629 */ 5630 static void 5631 ex_bmodified(exarg_T *eap) 5632 { 5633 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2); 5634 if (eap->do_ecmd_cmd != NULL) 5635 do_cmdline_cmd(eap->do_ecmd_cmd); 5636 } 5637 5638 /* 5639 * :[N]bnext [N] to next buffer 5640 * :[N]sbnext [N] split and to next buffer 5641 */ 5642 static void 5643 ex_bnext(exarg_T *eap) 5644 { 5645 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2); 5646 if (eap->do_ecmd_cmd != NULL) 5647 do_cmdline_cmd(eap->do_ecmd_cmd); 5648 } 5649 5650 /* 5651 * :[N]bNext [N] to previous buffer 5652 * :[N]bprevious [N] to previous buffer 5653 * :[N]sbNext [N] split and to previous buffer 5654 * :[N]sbprevious [N] split and to previous buffer 5655 */ 5656 static void 5657 ex_bprevious(exarg_T *eap) 5658 { 5659 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2); 5660 if (eap->do_ecmd_cmd != NULL) 5661 do_cmdline_cmd(eap->do_ecmd_cmd); 5662 } 5663 5664 /* 5665 * :brewind to first buffer 5666 * :bfirst to first buffer 5667 * :sbrewind split and to first buffer 5668 * :sbfirst split and to first buffer 5669 */ 5670 static void 5671 ex_brewind(exarg_T *eap) 5672 { 5673 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0); 5674 if (eap->do_ecmd_cmd != NULL) 5675 do_cmdline_cmd(eap->do_ecmd_cmd); 5676 } 5677 5678 /* 5679 * :blast to last buffer 5680 * :sblast split and to last buffer 5681 */ 5682 static void 5683 ex_blast(exarg_T *eap) 5684 { 5685 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0); 5686 if (eap->do_ecmd_cmd != NULL) 5687 do_cmdline_cmd(eap->do_ecmd_cmd); 5688 } 5689 5690 int 5691 ends_excmd(int c) 5692 { 5693 return (c == NUL || c == '|' || c == '"' || c == '\n'); 5694 } 5695 5696 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \ 5697 || defined(PROTO) 5698 /* 5699 * Return the next command, after the first '|' or '\n'. 5700 * Return NULL if not found. 5701 */ 5702 char_u * 5703 find_nextcmd(char_u *p) 5704 { 5705 while (*p != '|' && *p != '\n') 5706 { 5707 if (*p == NUL) 5708 return NULL; 5709 ++p; 5710 } 5711 return (p + 1); 5712 } 5713 #endif 5714 5715 /* 5716 * Check if *p is a separator between Ex commands, skipping over white space. 5717 * Return NULL if it isn't, the following character if it is. 5718 */ 5719 char_u * 5720 check_nextcmd(char_u *p) 5721 { 5722 char_u *s = skipwhite(p); 5723 5724 if (*s == '|' || *s == '\n') 5725 return (s + 1); 5726 else 5727 return NULL; 5728 } 5729 5730 /* 5731 * - if there are more files to edit 5732 * - and this is the last window 5733 * - and forceit not used 5734 * - and not repeated twice on a row 5735 * return FAIL and give error message if 'message' TRUE 5736 * return OK otherwise 5737 */ 5738 static int 5739 check_more( 5740 int message, /* when FALSE check only, no messages */ 5741 int forceit) 5742 { 5743 int n = ARGCOUNT - curwin->w_arg_idx - 1; 5744 5745 if (!forceit && only_one_window() 5746 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0) 5747 { 5748 if (message) 5749 { 5750 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 5751 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL) 5752 { 5753 char_u buff[DIALOG_MSG_SIZE]; 5754 5755 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, 5756 NGETTEXT("%d more file to edit. Quit anyway?", 5757 "%d more files to edit. Quit anyway?", n), n); 5758 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES) 5759 return OK; 5760 return FAIL; 5761 } 5762 #endif 5763 semsg(NGETTEXT("E173: %d more file to edit", 5764 "E173: %d more files to edit", n), n); 5765 quitmore = 2; /* next try to quit is allowed */ 5766 } 5767 return FAIL; 5768 } 5769 return OK; 5770 } 5771 5772 #ifdef FEAT_CMDL_COMPL 5773 /* 5774 * Function given to ExpandGeneric() to obtain the list of command names. 5775 */ 5776 char_u * 5777 get_command_name(expand_T *xp UNUSED, int idx) 5778 { 5779 if (idx >= (int)CMD_SIZE) 5780 # ifdef FEAT_USR_CMDS 5781 return get_user_command_name(idx); 5782 # else 5783 return NULL; 5784 # endif 5785 return cmdnames[idx].cmd_name; 5786 } 5787 #endif 5788 5789 #if defined(FEAT_USR_CMDS) || defined(PROTO) 5790 static int 5791 uc_add_command( 5792 char_u *name, 5793 size_t name_len, 5794 char_u *rep, 5795 long argt, 5796 long def, 5797 int flags, 5798 int compl, 5799 char_u *compl_arg, 5800 int addr_type, 5801 int force) 5802 { 5803 ucmd_T *cmd = NULL; 5804 char_u *p; 5805 int i; 5806 int cmp = 1; 5807 char_u *rep_buf = NULL; 5808 garray_T *gap; 5809 5810 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE); 5811 if (rep_buf == NULL) 5812 { 5813 /* Can't replace termcodes - try using the string as is */ 5814 rep_buf = vim_strsave(rep); 5815 5816 /* Give up if out of memory */ 5817 if (rep_buf == NULL) 5818 return FAIL; 5819 } 5820 5821 /* get address of growarray: global or in curbuf */ 5822 if (flags & UC_BUFFER) 5823 { 5824 gap = &curbuf->b_ucmds; 5825 if (gap->ga_itemsize == 0) 5826 ga_init2(gap, (int)sizeof(ucmd_T), 4); 5827 } 5828 else 5829 gap = &ucmds; 5830 5831 /* Search for the command in the already defined commands. */ 5832 for (i = 0; i < gap->ga_len; ++i) 5833 { 5834 size_t len; 5835 5836 cmd = USER_CMD_GA(gap, i); 5837 len = STRLEN(cmd->uc_name); 5838 cmp = STRNCMP(name, cmd->uc_name, name_len); 5839 if (cmp == 0) 5840 { 5841 if (name_len < len) 5842 cmp = -1; 5843 else if (name_len > len) 5844 cmp = 1; 5845 } 5846 5847 if (cmp == 0) 5848 { 5849 // Command can be replaced with "command!" and when sourcing the 5850 // same script again, but only once. 5851 if (!force && (cmd->uc_script_ctx.sc_sid != current_sctx.sc_sid 5852 || cmd->uc_script_ctx.sc_seq == current_sctx.sc_seq)) 5853 { 5854 semsg(_("E174: Command already exists: add ! to replace it: %s"), 5855 name); 5856 goto fail; 5857 } 5858 5859 VIM_CLEAR(cmd->uc_rep); 5860 #if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 5861 VIM_CLEAR(cmd->uc_compl_arg); 5862 #endif 5863 break; 5864 } 5865 5866 /* Stop as soon as we pass the name to add */ 5867 if (cmp < 0) 5868 break; 5869 } 5870 5871 /* Extend the array unless we're replacing an existing command */ 5872 if (cmp != 0) 5873 { 5874 if (ga_grow(gap, 1) != OK) 5875 goto fail; 5876 if ((p = vim_strnsave(name, (int)name_len)) == NULL) 5877 goto fail; 5878 5879 cmd = USER_CMD_GA(gap, i); 5880 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T)); 5881 5882 ++gap->ga_len; 5883 5884 cmd->uc_name = p; 5885 } 5886 5887 cmd->uc_rep = rep_buf; 5888 cmd->uc_argt = argt; 5889 cmd->uc_def = def; 5890 cmd->uc_compl = compl; 5891 #ifdef FEAT_EVAL 5892 cmd->uc_script_ctx = current_sctx; 5893 cmd->uc_script_ctx.sc_lnum += sourcing_lnum; 5894 # ifdef FEAT_CMDL_COMPL 5895 cmd->uc_compl_arg = compl_arg; 5896 # endif 5897 #endif 5898 cmd->uc_addr_type = addr_type; 5899 5900 return OK; 5901 5902 fail: 5903 vim_free(rep_buf); 5904 #if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 5905 vim_free(compl_arg); 5906 #endif 5907 return FAIL; 5908 } 5909 #endif 5910 5911 #if defined(FEAT_USR_CMDS) 5912 static struct 5913 { 5914 int expand; 5915 char *name; 5916 } addr_type_complete[] = 5917 { 5918 {ADDR_ARGUMENTS, "arguments"}, 5919 {ADDR_LINES, "lines"}, 5920 {ADDR_LOADED_BUFFERS, "loaded_buffers"}, 5921 {ADDR_TABS, "tabs"}, 5922 {ADDR_BUFFERS, "buffers"}, 5923 {ADDR_WINDOWS, "windows"}, 5924 {ADDR_QUICKFIX, "quickfix"}, 5925 {ADDR_OTHER, "other"}, 5926 {-1, NULL} 5927 }; 5928 #endif 5929 5930 #if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO) 5931 /* 5932 * List of names for completion for ":command" with the EXPAND_ flag. 5933 * Must be alphabetical for completion. 5934 */ 5935 static struct 5936 { 5937 int expand; 5938 char *name; 5939 } command_complete[] = 5940 { 5941 {EXPAND_ARGLIST, "arglist"}, 5942 {EXPAND_AUGROUP, "augroup"}, 5943 {EXPAND_BEHAVE, "behave"}, 5944 {EXPAND_BUFFERS, "buffer"}, 5945 {EXPAND_COLORS, "color"}, 5946 {EXPAND_COMMANDS, "command"}, 5947 {EXPAND_COMPILER, "compiler"}, 5948 #if defined(FEAT_CSCOPE) 5949 {EXPAND_CSCOPE, "cscope"}, 5950 #endif 5951 #if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 5952 {EXPAND_USER_DEFINED, "custom"}, 5953 {EXPAND_USER_LIST, "customlist"}, 5954 #endif 5955 {EXPAND_DIRECTORIES, "dir"}, 5956 {EXPAND_ENV_VARS, "environment"}, 5957 {EXPAND_EVENTS, "event"}, 5958 {EXPAND_EXPRESSION, "expression"}, 5959 {EXPAND_FILES, "file"}, 5960 {EXPAND_FILES_IN_PATH, "file_in_path"}, 5961 {EXPAND_FILETYPE, "filetype"}, 5962 {EXPAND_FUNCTIONS, "function"}, 5963 {EXPAND_HELP, "help"}, 5964 {EXPAND_HIGHLIGHT, "highlight"}, 5965 #if defined(FEAT_CMDHIST) 5966 {EXPAND_HISTORY, "history"}, 5967 #endif 5968 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE) 5969 {EXPAND_LOCALES, "locale"}, 5970 #endif 5971 {EXPAND_MAPCLEAR, "mapclear"}, 5972 {EXPAND_MAPPINGS, "mapping"}, 5973 {EXPAND_MENUS, "menu"}, 5974 {EXPAND_MESSAGES, "messages"}, 5975 {EXPAND_OWNSYNTAX, "syntax"}, 5976 #if defined(FEAT_PROFILE) 5977 {EXPAND_SYNTIME, "syntime"}, 5978 #endif 5979 {EXPAND_SETTINGS, "option"}, 5980 {EXPAND_PACKADD, "packadd"}, 5981 {EXPAND_SHELLCMD, "shellcmd"}, 5982 #if defined(FEAT_SIGNS) 5983 {EXPAND_SIGN, "sign"}, 5984 #endif 5985 {EXPAND_TAGS, "tag"}, 5986 {EXPAND_TAGS_LISTFILES, "tag_listfiles"}, 5987 {EXPAND_USER, "user"}, 5988 {EXPAND_USER_VARS, "var"}, 5989 {0, NULL} 5990 }; 5991 #endif 5992 5993 #if defined(FEAT_USR_CMDS) || defined(PROTO) 5994 static void 5995 uc_list(char_u *name, size_t name_len) 5996 { 5997 int i, j; 5998 int found = FALSE; 5999 ucmd_T *cmd; 6000 int len; 6001 long a; 6002 garray_T *gap; 6003 6004 gap = &curbuf->b_ucmds; 6005 for (;;) 6006 { 6007 for (i = 0; i < gap->ga_len; ++i) 6008 { 6009 cmd = USER_CMD_GA(gap, i); 6010 a = (long)cmd->uc_argt; 6011 6012 /* Skip commands which don't match the requested prefix and 6013 * commands filtered out. */ 6014 if (STRNCMP(name, cmd->uc_name, name_len) != 0 6015 || message_filtered(cmd->uc_name)) 6016 continue; 6017 6018 /* Put out the title first time */ 6019 if (!found) 6020 msg_puts_title(_("\n Name Args Address Complete Definition")); 6021 found = TRUE; 6022 msg_putchar('\n'); 6023 if (got_int) 6024 break; 6025 6026 /* Special cases */ 6027 msg_putchar(a & BANG ? '!' : ' '); 6028 msg_putchar(a & REGSTR ? '"' : ' '); 6029 msg_putchar(gap != &ucmds ? 'b' : ' '); 6030 msg_putchar(' '); 6031 6032 msg_outtrans_attr(cmd->uc_name, HL_ATTR(HLF_D)); 6033 len = (int)STRLEN(cmd->uc_name) + 4; 6034 6035 do { 6036 msg_putchar(' '); 6037 ++len; 6038 } while (len < 16); 6039 6040 len = 0; 6041 6042 /* Arguments */ 6043 switch ((int)(a & (EXTRA|NOSPC|NEEDARG))) 6044 { 6045 case 0: IObuff[len++] = '0'; break; 6046 case (EXTRA): IObuff[len++] = '*'; break; 6047 case (EXTRA|NOSPC): IObuff[len++] = '?'; break; 6048 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break; 6049 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break; 6050 } 6051 6052 do { 6053 IObuff[len++] = ' '; 6054 } while (len < 5); 6055 6056 /* Range */ 6057 if (a & (RANGE|COUNT)) 6058 { 6059 if (a & COUNT) 6060 { 6061 /* -count=N */ 6062 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def); 6063 len += (int)STRLEN(IObuff + len); 6064 } 6065 else if (a & DFLALL) 6066 IObuff[len++] = '%'; 6067 else if (cmd->uc_def >= 0) 6068 { 6069 /* -range=N */ 6070 sprintf((char *)IObuff + len, "%ld", cmd->uc_def); 6071 len += (int)STRLEN(IObuff + len); 6072 } 6073 else 6074 IObuff[len++] = '.'; 6075 } 6076 6077 do { 6078 IObuff[len++] = ' '; 6079 } while (len < 11); 6080 6081 /* Address Type */ 6082 for (j = 0; addr_type_complete[j].expand != -1; ++j) 6083 if (addr_type_complete[j].expand != ADDR_LINES 6084 && addr_type_complete[j].expand == cmd->uc_addr_type) 6085 { 6086 STRCPY(IObuff + len, addr_type_complete[j].name); 6087 len += (int)STRLEN(IObuff + len); 6088 break; 6089 } 6090 6091 do { 6092 IObuff[len++] = ' '; 6093 } while (len < 21); 6094 6095 /* Completion */ 6096 for (j = 0; command_complete[j].expand != 0; ++j) 6097 if (command_complete[j].expand == cmd->uc_compl) 6098 { 6099 STRCPY(IObuff + len, command_complete[j].name); 6100 len += (int)STRLEN(IObuff + len); 6101 break; 6102 } 6103 6104 do { 6105 IObuff[len++] = ' '; 6106 } while (len < 35); 6107 6108 IObuff[len] = '\0'; 6109 msg_outtrans(IObuff); 6110 6111 msg_outtrans_special(cmd->uc_rep, FALSE); 6112 #ifdef FEAT_EVAL 6113 if (p_verbose > 0) 6114 last_set_msg(cmd->uc_script_ctx); 6115 #endif 6116 out_flush(); 6117 ui_breakcheck(); 6118 if (got_int) 6119 break; 6120 } 6121 if (gap == &ucmds || i < gap->ga_len) 6122 break; 6123 gap = &ucmds; 6124 } 6125 6126 if (!found) 6127 msg(_("No user-defined commands found")); 6128 } 6129 6130 static char * 6131 uc_fun_cmd(void) 6132 { 6133 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4, 6134 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60, 6135 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2, 6136 0xb9, 0x7f, 0}; 6137 int i; 6138 6139 for (i = 0; fcmd[i]; ++i) 6140 IObuff[i] = fcmd[i] - 0x40; 6141 IObuff[i] = 0; 6142 return (char *)IObuff; 6143 } 6144 6145 static int 6146 uc_scan_attr( 6147 char_u *attr, 6148 size_t len, 6149 long *argt, 6150 long *def, 6151 int *flags, 6152 int *compl, 6153 char_u **compl_arg, 6154 int *addr_type_arg) 6155 { 6156 char_u *p; 6157 6158 if (len == 0) 6159 { 6160 emsg(_("E175: No attribute specified")); 6161 return FAIL; 6162 } 6163 6164 /* First, try the simple attributes (no arguments) */ 6165 if (STRNICMP(attr, "bang", len) == 0) 6166 *argt |= BANG; 6167 else if (STRNICMP(attr, "buffer", len) == 0) 6168 *flags |= UC_BUFFER; 6169 else if (STRNICMP(attr, "register", len) == 0) 6170 *argt |= REGSTR; 6171 else if (STRNICMP(attr, "bar", len) == 0) 6172 *argt |= TRLBAR; 6173 else 6174 { 6175 int i; 6176 char_u *val = NULL; 6177 size_t vallen = 0; 6178 size_t attrlen = len; 6179 6180 /* Look for the attribute name - which is the part before any '=' */ 6181 for (i = 0; i < (int)len; ++i) 6182 { 6183 if (attr[i] == '=') 6184 { 6185 val = &attr[i + 1]; 6186 vallen = len - i - 1; 6187 attrlen = i; 6188 break; 6189 } 6190 } 6191 6192 if (STRNICMP(attr, "nargs", attrlen) == 0) 6193 { 6194 if (vallen == 1) 6195 { 6196 if (*val == '0') 6197 /* Do nothing - this is the default */; 6198 else if (*val == '1') 6199 *argt |= (EXTRA | NOSPC | NEEDARG); 6200 else if (*val == '*') 6201 *argt |= EXTRA; 6202 else if (*val == '?') 6203 *argt |= (EXTRA | NOSPC); 6204 else if (*val == '+') 6205 *argt |= (EXTRA | NEEDARG); 6206 else 6207 goto wrong_nargs; 6208 } 6209 else 6210 { 6211 wrong_nargs: 6212 emsg(_("E176: Invalid number of arguments")); 6213 return FAIL; 6214 } 6215 } 6216 else if (STRNICMP(attr, "range", attrlen) == 0) 6217 { 6218 *argt |= RANGE; 6219 if (vallen == 1 && *val == '%') 6220 *argt |= DFLALL; 6221 else if (val != NULL) 6222 { 6223 p = val; 6224 if (*def >= 0) 6225 { 6226 two_count: 6227 emsg(_("E177: Count cannot be specified twice")); 6228 return FAIL; 6229 } 6230 6231 *def = getdigits(&p); 6232 *argt |= (ZEROR | NOTADR); 6233 6234 if (p != val + vallen || vallen == 0) 6235 { 6236 invalid_count: 6237 emsg(_("E178: Invalid default value for count")); 6238 return FAIL; 6239 } 6240 } 6241 } 6242 else if (STRNICMP(attr, "count", attrlen) == 0) 6243 { 6244 *argt |= (COUNT | ZEROR | RANGE | NOTADR); 6245 6246 if (val != NULL) 6247 { 6248 p = val; 6249 if (*def >= 0) 6250 goto two_count; 6251 6252 *def = getdigits(&p); 6253 6254 if (p != val + vallen) 6255 goto invalid_count; 6256 } 6257 6258 if (*def < 0) 6259 *def = 0; 6260 } 6261 else if (STRNICMP(attr, "complete", attrlen) == 0) 6262 { 6263 if (val == NULL) 6264 { 6265 emsg(_("E179: argument required for -complete")); 6266 return FAIL; 6267 } 6268 6269 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg) 6270 == FAIL) 6271 return FAIL; 6272 } 6273 else if (STRNICMP(attr, "addr", attrlen) == 0) 6274 { 6275 *argt |= RANGE; 6276 if (val == NULL) 6277 { 6278 emsg(_("E179: argument required for -addr")); 6279 return FAIL; 6280 } 6281 if (parse_addr_type_arg(val, (int)vallen, argt, addr_type_arg) 6282 == FAIL) 6283 return FAIL; 6284 if (addr_type_arg != ADDR_LINES) 6285 *argt |= (ZEROR | NOTADR) ; 6286 } 6287 else 6288 { 6289 char_u ch = attr[len]; 6290 attr[len] = '\0'; 6291 semsg(_("E181: Invalid attribute: %s"), attr); 6292 attr[len] = ch; 6293 return FAIL; 6294 } 6295 } 6296 6297 return OK; 6298 } 6299 6300 /* 6301 * ":command ..." 6302 */ 6303 static void 6304 ex_command(exarg_T *eap) 6305 { 6306 char_u *name; 6307 char_u *end; 6308 char_u *p; 6309 long argt = 0; 6310 long def = -1; 6311 int flags = 0; 6312 int compl = EXPAND_NOTHING; 6313 char_u *compl_arg = NULL; 6314 int addr_type_arg = ADDR_LINES; 6315 int has_attr = (eap->arg[0] == '-'); 6316 int name_len; 6317 6318 p = eap->arg; 6319 6320 /* Check for attributes */ 6321 while (*p == '-') 6322 { 6323 ++p; 6324 end = skiptowhite(p); 6325 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, 6326 &compl_arg, &addr_type_arg) 6327 == FAIL) 6328 return; 6329 p = skipwhite(end); 6330 } 6331 6332 /* Get the name (if any) and skip to the following argument */ 6333 name = p; 6334 if (ASCII_ISALPHA(*p)) 6335 while (ASCII_ISALNUM(*p)) 6336 ++p; 6337 if (!ends_excmd(*p) && !VIM_ISWHITE(*p)) 6338 { 6339 emsg(_("E182: Invalid command name")); 6340 return; 6341 } 6342 end = p; 6343 name_len = (int)(end - name); 6344 6345 /* If there is nothing after the name, and no attributes were specified, 6346 * we are listing commands 6347 */ 6348 p = skipwhite(end); 6349 if (!has_attr && ends_excmd(*p)) 6350 { 6351 uc_list(name, end - name); 6352 } 6353 else if (!ASCII_ISUPPER(*name)) 6354 { 6355 emsg(_("E183: User defined commands must start with an uppercase letter")); 6356 return; 6357 } 6358 else if ((name_len == 1 && *name == 'X') 6359 || (name_len <= 4 6360 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0)) 6361 { 6362 emsg(_("E841: Reserved name, cannot be used for user defined command")); 6363 return; 6364 } 6365 else 6366 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg, 6367 addr_type_arg, eap->forceit); 6368 } 6369 6370 /* 6371 * ":comclear" 6372 * Clear all user commands, global and for current buffer. 6373 */ 6374 void 6375 ex_comclear(exarg_T *eap UNUSED) 6376 { 6377 uc_clear(&ucmds); 6378 uc_clear(&curbuf->b_ucmds); 6379 } 6380 6381 /* 6382 * Clear all user commands for "gap". 6383 */ 6384 void 6385 uc_clear(garray_T *gap) 6386 { 6387 int i; 6388 ucmd_T *cmd; 6389 6390 for (i = 0; i < gap->ga_len; ++i) 6391 { 6392 cmd = USER_CMD_GA(gap, i); 6393 vim_free(cmd->uc_name); 6394 vim_free(cmd->uc_rep); 6395 # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 6396 vim_free(cmd->uc_compl_arg); 6397 # endif 6398 } 6399 ga_clear(gap); 6400 } 6401 6402 static void 6403 ex_delcommand(exarg_T *eap) 6404 { 6405 int i = 0; 6406 ucmd_T *cmd = NULL; 6407 int cmp = -1; 6408 garray_T *gap; 6409 6410 gap = &curbuf->b_ucmds; 6411 for (;;) 6412 { 6413 for (i = 0; i < gap->ga_len; ++i) 6414 { 6415 cmd = USER_CMD_GA(gap, i); 6416 cmp = STRCMP(eap->arg, cmd->uc_name); 6417 if (cmp <= 0) 6418 break; 6419 } 6420 if (gap == &ucmds || cmp == 0) 6421 break; 6422 gap = &ucmds; 6423 } 6424 6425 if (cmp != 0) 6426 { 6427 semsg(_("E184: No such user-defined command: %s"), eap->arg); 6428 return; 6429 } 6430 6431 vim_free(cmd->uc_name); 6432 vim_free(cmd->uc_rep); 6433 # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 6434 vim_free(cmd->uc_compl_arg); 6435 # endif 6436 6437 --gap->ga_len; 6438 6439 if (i < gap->ga_len) 6440 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T)); 6441 } 6442 6443 /* 6444 * split and quote args for <f-args> 6445 */ 6446 static char_u * 6447 uc_split_args(char_u *arg, size_t *lenp) 6448 { 6449 char_u *buf; 6450 char_u *p; 6451 char_u *q; 6452 int len; 6453 6454 /* Precalculate length */ 6455 p = arg; 6456 len = 2; /* Initial and final quotes */ 6457 6458 while (*p) 6459 { 6460 if (p[0] == '\\' && p[1] == '\\') 6461 { 6462 len += 2; 6463 p += 2; 6464 } 6465 else if (p[0] == '\\' && VIM_ISWHITE(p[1])) 6466 { 6467 len += 1; 6468 p += 2; 6469 } 6470 else if (*p == '\\' || *p == '"') 6471 { 6472 len += 2; 6473 p += 1; 6474 } 6475 else if (VIM_ISWHITE(*p)) 6476 { 6477 p = skipwhite(p); 6478 if (*p == NUL) 6479 break; 6480 len += 3; /* "," */ 6481 } 6482 else 6483 { 6484 int charlen = (*mb_ptr2len)(p); 6485 6486 len += charlen; 6487 p += charlen; 6488 } 6489 } 6490 6491 buf = alloc(len + 1); 6492 if (buf == NULL) 6493 { 6494 *lenp = 0; 6495 return buf; 6496 } 6497 6498 p = arg; 6499 q = buf; 6500 *q++ = '"'; 6501 while (*p) 6502 { 6503 if (p[0] == '\\' && p[1] == '\\') 6504 { 6505 *q++ = '\\'; 6506 *q++ = '\\'; 6507 p += 2; 6508 } 6509 else if (p[0] == '\\' && VIM_ISWHITE(p[1])) 6510 { 6511 *q++ = p[1]; 6512 p += 2; 6513 } 6514 else if (*p == '\\' || *p == '"') 6515 { 6516 *q++ = '\\'; 6517 *q++ = *p++; 6518 } 6519 else if (VIM_ISWHITE(*p)) 6520 { 6521 p = skipwhite(p); 6522 if (*p == NUL) 6523 break; 6524 *q++ = '"'; 6525 *q++ = ','; 6526 *q++ = '"'; 6527 } 6528 else 6529 { 6530 MB_COPY_CHAR(p, q); 6531 } 6532 } 6533 *q++ = '"'; 6534 *q = 0; 6535 6536 *lenp = len; 6537 return buf; 6538 } 6539 6540 static size_t 6541 add_cmd_modifier(char_u *buf, char *mod_str, int *multi_mods) 6542 { 6543 size_t result; 6544 6545 result = STRLEN(mod_str); 6546 if (*multi_mods) 6547 result += 1; 6548 if (buf != NULL) 6549 { 6550 if (*multi_mods) 6551 STRCAT(buf, " "); 6552 STRCAT(buf, mod_str); 6553 } 6554 6555 *multi_mods = 1; 6556 6557 return result; 6558 } 6559 6560 /* 6561 * Check for a <> code in a user command. 6562 * "code" points to the '<'. "len" the length of the <> (inclusive). 6563 * "buf" is where the result is to be added. 6564 * "split_buf" points to a buffer used for splitting, caller should free it. 6565 * "split_len" is the length of what "split_buf" contains. 6566 * Returns the length of the replacement, which has been added to "buf". 6567 * Returns -1 if there was no match, and only the "<" has been copied. 6568 */ 6569 static size_t 6570 uc_check_code( 6571 char_u *code, 6572 size_t len, 6573 char_u *buf, 6574 ucmd_T *cmd, /* the user command we're expanding */ 6575 exarg_T *eap, /* ex arguments */ 6576 char_u **split_buf, 6577 size_t *split_len) 6578 { 6579 size_t result = 0; 6580 char_u *p = code + 1; 6581 size_t l = len - 2; 6582 int quote = 0; 6583 enum { 6584 ct_ARGS, 6585 ct_BANG, 6586 ct_COUNT, 6587 ct_LINE1, 6588 ct_LINE2, 6589 ct_RANGE, 6590 ct_MODS, 6591 ct_REGISTER, 6592 ct_LT, 6593 ct_NONE 6594 } type = ct_NONE; 6595 6596 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-') 6597 { 6598 quote = (*p == 'q' || *p == 'Q') ? 1 : 2; 6599 p += 2; 6600 l -= 2; 6601 } 6602 6603 ++l; 6604 if (l <= 1) 6605 type = ct_NONE; 6606 else if (STRNICMP(p, "args>", l) == 0) 6607 type = ct_ARGS; 6608 else if (STRNICMP(p, "bang>", l) == 0) 6609 type = ct_BANG; 6610 else if (STRNICMP(p, "count>", l) == 0) 6611 type = ct_COUNT; 6612 else if (STRNICMP(p, "line1>", l) == 0) 6613 type = ct_LINE1; 6614 else if (STRNICMP(p, "line2>", l) == 0) 6615 type = ct_LINE2; 6616 else if (STRNICMP(p, "range>", l) == 0) 6617 type = ct_RANGE; 6618 else if (STRNICMP(p, "lt>", l) == 0) 6619 type = ct_LT; 6620 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0) 6621 type = ct_REGISTER; 6622 else if (STRNICMP(p, "mods>", l) == 0) 6623 type = ct_MODS; 6624 6625 switch (type) 6626 { 6627 case ct_ARGS: 6628 /* Simple case first */ 6629 if (*eap->arg == NUL) 6630 { 6631 if (quote == 1) 6632 { 6633 result = 2; 6634 if (buf != NULL) 6635 STRCPY(buf, "''"); 6636 } 6637 else 6638 result = 0; 6639 break; 6640 } 6641 6642 /* When specified there is a single argument don't split it. 6643 * Works for ":Cmd %" when % is "a b c". */ 6644 if ((eap->argt & NOSPC) && quote == 2) 6645 quote = 1; 6646 6647 switch (quote) 6648 { 6649 case 0: /* No quoting, no splitting */ 6650 result = STRLEN(eap->arg); 6651 if (buf != NULL) 6652 STRCPY(buf, eap->arg); 6653 break; 6654 case 1: /* Quote, but don't split */ 6655 result = STRLEN(eap->arg) + 2; 6656 for (p = eap->arg; *p; ++p) 6657 { 6658 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2) 6659 /* DBCS can contain \ in a trail byte, skip the 6660 * double-byte character. */ 6661 ++p; 6662 else 6663 if (*p == '\\' || *p == '"') 6664 ++result; 6665 } 6666 6667 if (buf != NULL) 6668 { 6669 *buf++ = '"'; 6670 for (p = eap->arg; *p; ++p) 6671 { 6672 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2) 6673 /* DBCS can contain \ in a trail byte, copy the 6674 * double-byte character to avoid escaping. */ 6675 *buf++ = *p++; 6676 else 6677 if (*p == '\\' || *p == '"') 6678 *buf++ = '\\'; 6679 *buf++ = *p; 6680 } 6681 *buf = '"'; 6682 } 6683 6684 break; 6685 case 2: /* Quote and split (<f-args>) */ 6686 /* This is hard, so only do it once, and cache the result */ 6687 if (*split_buf == NULL) 6688 *split_buf = uc_split_args(eap->arg, split_len); 6689 6690 result = *split_len; 6691 if (buf != NULL && result != 0) 6692 STRCPY(buf, *split_buf); 6693 6694 break; 6695 } 6696 break; 6697 6698 case ct_BANG: 6699 result = eap->forceit ? 1 : 0; 6700 if (quote) 6701 result += 2; 6702 if (buf != NULL) 6703 { 6704 if (quote) 6705 *buf++ = '"'; 6706 if (eap->forceit) 6707 *buf++ = '!'; 6708 if (quote) 6709 *buf = '"'; 6710 } 6711 break; 6712 6713 case ct_LINE1: 6714 case ct_LINE2: 6715 case ct_RANGE: 6716 case ct_COUNT: 6717 { 6718 char num_buf[20]; 6719 long num = (type == ct_LINE1) ? eap->line1 : 6720 (type == ct_LINE2) ? eap->line2 : 6721 (type == ct_RANGE) ? eap->addr_count : 6722 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def; 6723 size_t num_len; 6724 6725 sprintf(num_buf, "%ld", num); 6726 num_len = STRLEN(num_buf); 6727 result = num_len; 6728 6729 if (quote) 6730 result += 2; 6731 6732 if (buf != NULL) 6733 { 6734 if (quote) 6735 *buf++ = '"'; 6736 STRCPY(buf, num_buf); 6737 buf += num_len; 6738 if (quote) 6739 *buf = '"'; 6740 } 6741 6742 break; 6743 } 6744 6745 case ct_MODS: 6746 { 6747 int multi_mods = 0; 6748 typedef struct { 6749 int *varp; 6750 char *name; 6751 } mod_entry_T; 6752 static mod_entry_T mod_entries[] = { 6753 #ifdef FEAT_BROWSE_CMD 6754 {&cmdmod.browse, "browse"}, 6755 #endif 6756 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 6757 {&cmdmod.confirm, "confirm"}, 6758 #endif 6759 {&cmdmod.hide, "hide"}, 6760 {&cmdmod.keepalt, "keepalt"}, 6761 {&cmdmod.keepjumps, "keepjumps"}, 6762 {&cmdmod.keepmarks, "keepmarks"}, 6763 {&cmdmod.keeppatterns, "keeppatterns"}, 6764 {&cmdmod.lockmarks, "lockmarks"}, 6765 {&cmdmod.noswapfile, "noswapfile"}, 6766 {NULL, NULL} 6767 }; 6768 int i; 6769 6770 result = quote ? 2 : 0; 6771 if (buf != NULL) 6772 { 6773 if (quote) 6774 *buf++ = '"'; 6775 *buf = '\0'; 6776 } 6777 6778 /* :aboveleft and :leftabove */ 6779 if (cmdmod.split & WSP_ABOVE) 6780 result += add_cmd_modifier(buf, "aboveleft", &multi_mods); 6781 /* :belowright and :rightbelow */ 6782 if (cmdmod.split & WSP_BELOW) 6783 result += add_cmd_modifier(buf, "belowright", &multi_mods); 6784 /* :botright */ 6785 if (cmdmod.split & WSP_BOT) 6786 result += add_cmd_modifier(buf, "botright", &multi_mods); 6787 6788 /* the modifiers that are simple flags */ 6789 for (i = 0; mod_entries[i].varp != NULL; ++i) 6790 if (*mod_entries[i].varp) 6791 result += add_cmd_modifier(buf, mod_entries[i].name, 6792 &multi_mods); 6793 6794 /* TODO: How to support :noautocmd? */ 6795 #ifdef HAVE_SANDBOX 6796 /* TODO: How to support :sandbox?*/ 6797 #endif 6798 /* :silent */ 6799 if (msg_silent > 0) 6800 result += add_cmd_modifier(buf, 6801 emsg_silent > 0 ? "silent!" : "silent", &multi_mods); 6802 /* :tab */ 6803 if (cmdmod.tab > 0) 6804 result += add_cmd_modifier(buf, "tab", &multi_mods); 6805 /* :topleft */ 6806 if (cmdmod.split & WSP_TOP) 6807 result += add_cmd_modifier(buf, "topleft", &multi_mods); 6808 /* TODO: How to support :unsilent?*/ 6809 /* :verbose */ 6810 if (p_verbose > 0) 6811 result += add_cmd_modifier(buf, "verbose", &multi_mods); 6812 /* :vertical */ 6813 if (cmdmod.split & WSP_VERT) 6814 result += add_cmd_modifier(buf, "vertical", &multi_mods); 6815 if (quote && buf != NULL) 6816 { 6817 buf += result - 2; 6818 *buf = '"'; 6819 } 6820 break; 6821 } 6822 6823 case ct_REGISTER: 6824 result = eap->regname ? 1 : 0; 6825 if (quote) 6826 result += 2; 6827 if (buf != NULL) 6828 { 6829 if (quote) 6830 *buf++ = '\''; 6831 if (eap->regname) 6832 *buf++ = eap->regname; 6833 if (quote) 6834 *buf = '\''; 6835 } 6836 break; 6837 6838 case ct_LT: 6839 result = 1; 6840 if (buf != NULL) 6841 *buf = '<'; 6842 break; 6843 6844 default: 6845 /* Not recognized: just copy the '<' and return -1. */ 6846 result = (size_t)-1; 6847 if (buf != NULL) 6848 *buf = '<'; 6849 break; 6850 } 6851 6852 return result; 6853 } 6854 6855 static void 6856 do_ucmd(exarg_T *eap) 6857 { 6858 char_u *buf; 6859 char_u *p; 6860 char_u *q; 6861 6862 char_u *start; 6863 char_u *end = NULL; 6864 char_u *ksp; 6865 size_t len, totlen; 6866 6867 size_t split_len = 0; 6868 char_u *split_buf = NULL; 6869 ucmd_T *cmd; 6870 #ifdef FEAT_EVAL 6871 sctx_T save_current_sctx = current_sctx; 6872 #endif 6873 6874 if (eap->cmdidx == CMD_USER) 6875 cmd = USER_CMD(eap->useridx); 6876 else 6877 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx); 6878 6879 /* 6880 * Replace <> in the command by the arguments. 6881 * First round: "buf" is NULL, compute length, allocate "buf". 6882 * Second round: copy result into "buf". 6883 */ 6884 buf = NULL; 6885 for (;;) 6886 { 6887 p = cmd->uc_rep; /* source */ 6888 q = buf; /* destination */ 6889 totlen = 0; 6890 6891 for (;;) 6892 { 6893 start = vim_strchr(p, '<'); 6894 if (start != NULL) 6895 end = vim_strchr(start + 1, '>'); 6896 if (buf != NULL) 6897 { 6898 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp) 6899 ; 6900 if (*ksp == K_SPECIAL 6901 && (start == NULL || ksp < start || end == NULL) 6902 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER) 6903 # ifdef FEAT_GUI 6904 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI) 6905 # endif 6906 )) 6907 { 6908 /* K_SPECIAL has been put in the buffer as K_SPECIAL 6909 * KS_SPECIAL KE_FILLER, like for mappings, but 6910 * do_cmdline() doesn't handle that, so convert it back. 6911 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */ 6912 len = ksp - p; 6913 if (len > 0) 6914 { 6915 mch_memmove(q, p, len); 6916 q += len; 6917 } 6918 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI; 6919 p = ksp + 3; 6920 continue; 6921 } 6922 } 6923 6924 /* break if no <item> is found */ 6925 if (start == NULL || end == NULL) 6926 break; 6927 6928 /* Include the '>' */ 6929 ++end; 6930 6931 /* Take everything up to the '<' */ 6932 len = start - p; 6933 if (buf == NULL) 6934 totlen += len; 6935 else 6936 { 6937 mch_memmove(q, p, len); 6938 q += len; 6939 } 6940 6941 len = uc_check_code(start, end - start, q, cmd, eap, 6942 &split_buf, &split_len); 6943 if (len == (size_t)-1) 6944 { 6945 /* no match, continue after '<' */ 6946 p = start + 1; 6947 len = 1; 6948 } 6949 else 6950 p = end; 6951 if (buf == NULL) 6952 totlen += len; 6953 else 6954 q += len; 6955 } 6956 if (buf != NULL) /* second time here, finished */ 6957 { 6958 STRCPY(q, p); 6959 break; 6960 } 6961 6962 totlen += STRLEN(p); /* Add on the trailing characters */ 6963 buf = alloc((unsigned)(totlen + 1)); 6964 if (buf == NULL) 6965 { 6966 vim_free(split_buf); 6967 return; 6968 } 6969 } 6970 6971 #ifdef FEAT_EVAL 6972 current_sctx.sc_sid = cmd->uc_script_ctx.sc_sid; 6973 #endif 6974 (void)do_cmdline(buf, eap->getline, eap->cookie, 6975 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); 6976 #ifdef FEAT_EVAL 6977 current_sctx = save_current_sctx; 6978 #endif 6979 vim_free(buf); 6980 vim_free(split_buf); 6981 } 6982 6983 # if defined(FEAT_CMDL_COMPL) || defined(PROTO) 6984 static char_u * 6985 get_user_command_name(int idx) 6986 { 6987 return get_user_commands(NULL, idx - (int)CMD_SIZE); 6988 } 6989 6990 /* 6991 * Function given to ExpandGeneric() to obtain the list of user command names. 6992 */ 6993 char_u * 6994 get_user_commands(expand_T *xp UNUSED, int idx) 6995 { 6996 if (idx < curbuf->b_ucmds.ga_len) 6997 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name; 6998 idx -= curbuf->b_ucmds.ga_len; 6999 if (idx < ucmds.ga_len) 7000 return USER_CMD(idx)->uc_name; 7001 return NULL; 7002 } 7003 7004 /* 7005 * Function given to ExpandGeneric() to obtain the list of user address type names. 7006 */ 7007 char_u * 7008 get_user_cmd_addr_type(expand_T *xp UNUSED, int idx) 7009 { 7010 return (char_u *)addr_type_complete[idx].name; 7011 } 7012 7013 /* 7014 * Function given to ExpandGeneric() to obtain the list of user command 7015 * attributes. 7016 */ 7017 char_u * 7018 get_user_cmd_flags(expand_T *xp UNUSED, int idx) 7019 { 7020 static char *user_cmd_flags[] = 7021 {"addr", "bang", "bar", "buffer", "complete", 7022 "count", "nargs", "range", "register"}; 7023 7024 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0]))) 7025 return NULL; 7026 return (char_u *)user_cmd_flags[idx]; 7027 } 7028 7029 /* 7030 * Function given to ExpandGeneric() to obtain the list of values for -nargs. 7031 */ 7032 char_u * 7033 get_user_cmd_nargs(expand_T *xp UNUSED, int idx) 7034 { 7035 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"}; 7036 7037 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0]))) 7038 return NULL; 7039 return (char_u *)user_cmd_nargs[idx]; 7040 } 7041 7042 /* 7043 * Function given to ExpandGeneric() to obtain the list of values for -complete. 7044 */ 7045 char_u * 7046 get_user_cmd_complete(expand_T *xp UNUSED, int idx) 7047 { 7048 return (char_u *)command_complete[idx].name; 7049 } 7050 # endif /* FEAT_CMDL_COMPL */ 7051 7052 /* 7053 * Parse address type argument 7054 */ 7055 int 7056 parse_addr_type_arg( 7057 char_u *value, 7058 int vallen, 7059 long *argt, 7060 int *addr_type_arg) 7061 { 7062 int i, a, b; 7063 7064 for (i = 0; addr_type_complete[i].expand != -1; ++i) 7065 { 7066 a = (int)STRLEN(addr_type_complete[i].name) == vallen; 7067 b = STRNCMP(value, addr_type_complete[i].name, vallen) == 0; 7068 if (a && b) 7069 { 7070 *addr_type_arg = addr_type_complete[i].expand; 7071 break; 7072 } 7073 } 7074 7075 if (addr_type_complete[i].expand == -1) 7076 { 7077 char_u *err = value; 7078 7079 for (i = 0; err[i] != NUL && !VIM_ISWHITE(err[i]); i++) 7080 ; 7081 err[i] = NUL; 7082 semsg(_("E180: Invalid address type value: %s"), err); 7083 return FAIL; 7084 } 7085 7086 if (*addr_type_arg != ADDR_LINES) 7087 *argt |= NOTADR; 7088 7089 return OK; 7090 } 7091 7092 #endif /* FEAT_USR_CMDS */ 7093 7094 #if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO) 7095 /* 7096 * Parse a completion argument "value[vallen]". 7097 * The detected completion goes in "*complp", argument type in "*argt". 7098 * When there is an argument, for function and user defined completion, it's 7099 * copied to allocated memory and stored in "*compl_arg". 7100 * Returns FAIL if something is wrong. 7101 */ 7102 int 7103 parse_compl_arg( 7104 char_u *value, 7105 int vallen, 7106 int *complp, 7107 long *argt, 7108 char_u **compl_arg UNUSED) 7109 { 7110 char_u *arg = NULL; 7111 # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 7112 size_t arglen = 0; 7113 # endif 7114 int i; 7115 int valend = vallen; 7116 7117 /* Look for any argument part - which is the part after any ',' */ 7118 for (i = 0; i < vallen; ++i) 7119 { 7120 if (value[i] == ',') 7121 { 7122 arg = &value[i + 1]; 7123 # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 7124 arglen = vallen - i - 1; 7125 # endif 7126 valend = i; 7127 break; 7128 } 7129 } 7130 7131 for (i = 0; command_complete[i].expand != 0; ++i) 7132 { 7133 if ((int)STRLEN(command_complete[i].name) == valend 7134 && STRNCMP(value, command_complete[i].name, valend) == 0) 7135 { 7136 *complp = command_complete[i].expand; 7137 if (command_complete[i].expand == EXPAND_BUFFERS) 7138 *argt |= BUFNAME; 7139 else if (command_complete[i].expand == EXPAND_DIRECTORIES 7140 || command_complete[i].expand == EXPAND_FILES) 7141 *argt |= XFILE; 7142 break; 7143 } 7144 } 7145 7146 if (command_complete[i].expand == 0) 7147 { 7148 semsg(_("E180: Invalid complete value: %s"), value); 7149 return FAIL; 7150 } 7151 7152 # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 7153 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST 7154 && arg != NULL) 7155 # else 7156 if (arg != NULL) 7157 # endif 7158 { 7159 emsg(_("E468: Completion argument only allowed for custom completion")); 7160 return FAIL; 7161 } 7162 7163 # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 7164 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST) 7165 && arg == NULL) 7166 { 7167 emsg(_("E467: Custom completion requires a function argument")); 7168 return FAIL; 7169 } 7170 7171 if (arg != NULL) 7172 *compl_arg = vim_strnsave(arg, (int)arglen); 7173 # endif 7174 return OK; 7175 } 7176 7177 int 7178 cmdcomplete_str_to_type(char_u *complete_str) 7179 { 7180 int i; 7181 7182 for (i = 0; command_complete[i].expand != 0; ++i) 7183 if (STRCMP(complete_str, command_complete[i].name) == 0) 7184 return command_complete[i].expand; 7185 7186 return EXPAND_NOTHING; 7187 } 7188 #endif 7189 7190 static void 7191 ex_colorscheme(exarg_T *eap) 7192 { 7193 if (*eap->arg == NUL) 7194 { 7195 #ifdef FEAT_EVAL 7196 char_u *expr = vim_strsave((char_u *)"g:colors_name"); 7197 char_u *p = NULL; 7198 7199 if (expr != NULL) 7200 { 7201 ++emsg_off; 7202 p = eval_to_string(expr, NULL, FALSE); 7203 --emsg_off; 7204 vim_free(expr); 7205 } 7206 if (p != NULL) 7207 { 7208 msg((char *)p); 7209 vim_free(p); 7210 } 7211 else 7212 msg("default"); 7213 #else 7214 msg(_("unknown")); 7215 #endif 7216 } 7217 else if (load_colors(eap->arg) == FAIL) 7218 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg); 7219 7220 #ifdef FEAT_VTP 7221 else if (has_vtp_working()) 7222 { 7223 // background color change requires clear + redraw 7224 update_screen(CLEAR); 7225 redrawcmd(); 7226 } 7227 #endif 7228 } 7229 7230 static void 7231 ex_highlight(exarg_T *eap) 7232 { 7233 if (*eap->arg == NUL && eap->cmd[2] == '!') 7234 msg(_("Greetings, Vim user!")); 7235 do_highlight(eap->arg, eap->forceit, FALSE); 7236 } 7237 7238 7239 /* 7240 * Call this function if we thought we were going to exit, but we won't 7241 * (because of an error). May need to restore the terminal mode. 7242 */ 7243 void 7244 not_exiting(void) 7245 { 7246 exiting = FALSE; 7247 settmode(TMODE_RAW); 7248 } 7249 7250 static int 7251 before_quit_autocmds(win_T *wp, int quit_all, int forceit) 7252 { 7253 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer); 7254 7255 /* Bail out when autocommands closed the window. 7256 * Refuse to quit when the buffer in the last window is being closed (can 7257 * only happen in autocommands). */ 7258 if (!win_valid(wp) 7259 || curbuf_locked() 7260 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0)) 7261 return TRUE; 7262 7263 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window())) 7264 { 7265 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf); 7266 /* Refuse to quit when locked or when the buffer in the last window is 7267 * being closed (can only happen in autocommands). */ 7268 if (curbuf_locked() 7269 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0)) 7270 return TRUE; 7271 } 7272 7273 return FALSE; 7274 } 7275 7276 /* 7277 * ":quit": quit current window, quit Vim if the last window is closed. 7278 * ":{nr}quit": quit window {nr} 7279 */ 7280 static void 7281 ex_quit(exarg_T *eap) 7282 { 7283 win_T *wp; 7284 7285 #ifdef FEAT_CMDWIN 7286 if (cmdwin_type != 0) 7287 { 7288 cmdwin_result = Ctrl_C; 7289 return; 7290 } 7291 #endif 7292 /* Don't quit while editing the command line. */ 7293 if (text_locked()) 7294 { 7295 text_locked_msg(); 7296 return; 7297 } 7298 if (eap->addr_count > 0) 7299 { 7300 int wnr = eap->line2; 7301 7302 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next) 7303 if (--wnr <= 0) 7304 break; 7305 } 7306 else 7307 wp = curwin; 7308 7309 /* Refuse to quit when locked. */ 7310 if (curbuf_locked()) 7311 return; 7312 7313 /* Trigger QuitPre and maybe ExitPre */ 7314 if (before_quit_autocmds(wp, FALSE, eap->forceit)) 7315 return; 7316 7317 #ifdef FEAT_NETBEANS_INTG 7318 netbeansForcedQuit = eap->forceit; 7319 #endif 7320 7321 /* 7322 * If there are more files or windows we won't exit. 7323 */ 7324 if (check_more(FALSE, eap->forceit) == OK && only_one_window()) 7325 exiting = TRUE; 7326 if ((!buf_hide(wp->w_buffer) 7327 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0) 7328 | (eap->forceit ? CCGD_FORCEIT : 0) 7329 | CCGD_EXCMD)) 7330 || check_more(TRUE, eap->forceit) == FAIL 7331 || (only_one_window() && check_changed_any(eap->forceit, TRUE))) 7332 { 7333 not_exiting(); 7334 } 7335 else 7336 { 7337 /* quit last window 7338 * Note: only_one_window() returns true, even so a help window is 7339 * still open. In that case only quit, if no address has been 7340 * specified. Example: 7341 * :h|wincmd w|1q - don't quit 7342 * :h|wincmd w|q - quit 7343 */ 7344 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0)) 7345 getout(0); 7346 not_exiting(); 7347 #ifdef FEAT_GUI 7348 need_mouse_correct = TRUE; 7349 #endif 7350 /* close window; may free buffer */ 7351 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit); 7352 } 7353 } 7354 7355 /* 7356 * ":cquit". 7357 */ 7358 static void 7359 ex_cquit(exarg_T *eap UNUSED) 7360 { 7361 getout(1); /* this does not always pass on the exit code to the Manx 7362 compiler. why? */ 7363 } 7364 7365 /* 7366 * ":qall": try to quit all windows 7367 */ 7368 static void 7369 ex_quit_all(exarg_T *eap) 7370 { 7371 # ifdef FEAT_CMDWIN 7372 if (cmdwin_type != 0) 7373 { 7374 if (eap->forceit) 7375 cmdwin_result = K_XF1; /* ex_window() takes care of this */ 7376 else 7377 cmdwin_result = K_XF2; 7378 return; 7379 } 7380 # endif 7381 7382 /* Don't quit while editing the command line. */ 7383 if (text_locked()) 7384 { 7385 text_locked_msg(); 7386 return; 7387 } 7388 7389 if (before_quit_autocmds(curwin, TRUE, eap->forceit)) 7390 return; 7391 7392 exiting = TRUE; 7393 if (eap->forceit || !check_changed_any(FALSE, FALSE)) 7394 getout(0); 7395 not_exiting(); 7396 } 7397 7398 /* 7399 * ":close": close current window, unless it is the last one 7400 */ 7401 static void 7402 ex_close(exarg_T *eap) 7403 { 7404 win_T *win; 7405 int winnr = 0; 7406 #ifdef FEAT_CMDWIN 7407 if (cmdwin_type != 0) 7408 cmdwin_result = Ctrl_C; 7409 else 7410 #endif 7411 if (!text_locked() && !curbuf_locked()) 7412 { 7413 if (eap->addr_count == 0) 7414 ex_win_close(eap->forceit, curwin, NULL); 7415 else 7416 { 7417 FOR_ALL_WINDOWS(win) 7418 { 7419 winnr++; 7420 if (winnr == eap->line2) 7421 break; 7422 } 7423 if (win == NULL) 7424 win = lastwin; 7425 ex_win_close(eap->forceit, win, NULL); 7426 } 7427 } 7428 } 7429 7430 #ifdef FEAT_QUICKFIX 7431 /* 7432 * ":pclose": Close any preview window. 7433 */ 7434 static void 7435 ex_pclose(exarg_T *eap) 7436 { 7437 win_T *win; 7438 7439 FOR_ALL_WINDOWS(win) 7440 if (win->w_p_pvw) 7441 { 7442 ex_win_close(eap->forceit, win, NULL); 7443 break; 7444 } 7445 } 7446 #endif 7447 7448 /* 7449 * Close window "win" and take care of handling closing the last window for a 7450 * modified buffer. 7451 */ 7452 static void 7453 ex_win_close( 7454 int forceit, 7455 win_T *win, 7456 tabpage_T *tp) /* NULL or the tab page "win" is in */ 7457 { 7458 int need_hide; 7459 buf_T *buf = win->w_buffer; 7460 7461 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1); 7462 if (need_hide && !buf_hide(buf) && !forceit) 7463 { 7464 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 7465 if ((p_confirm || cmdmod.confirm) && p_write) 7466 { 7467 bufref_T bufref; 7468 7469 set_bufref(&bufref, buf); 7470 dialog_changed(buf, FALSE); 7471 if (bufref_valid(&bufref) && bufIsChanged(buf)) 7472 return; 7473 need_hide = FALSE; 7474 } 7475 else 7476 #endif 7477 { 7478 no_write_message(); 7479 return; 7480 } 7481 } 7482 7483 #ifdef FEAT_GUI 7484 need_mouse_correct = TRUE; 7485 #endif 7486 7487 /* free buffer when not hiding it or when it's a scratch buffer */ 7488 if (tp == NULL) 7489 win_close(win, !need_hide && !buf_hide(buf)); 7490 else 7491 win_close_othertab(win, !need_hide && !buf_hide(buf), tp); 7492 } 7493 7494 /* 7495 * Handle the argument for a tabpage related ex command. 7496 * Returns a tabpage number. 7497 * When an error is encountered then eap->errmsg is set. 7498 */ 7499 static int 7500 get_tabpage_arg(exarg_T *eap) 7501 { 7502 int tab_number; 7503 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1; 7504 7505 if (eap->arg && *eap->arg != NUL) 7506 { 7507 char_u *p = eap->arg; 7508 char_u *p_save; 7509 int relative = 0; /* argument +N/-N means: go to N places to the 7510 * right/left relative to the current position. */ 7511 7512 if (*p == '-') 7513 { 7514 relative = -1; 7515 p++; 7516 } 7517 else if (*p == '+') 7518 { 7519 relative = 1; 7520 p++; 7521 } 7522 7523 p_save = p; 7524 tab_number = getdigits(&p); 7525 7526 if (relative == 0) 7527 { 7528 if (STRCMP(p, "$") == 0) 7529 tab_number = LAST_TAB_NR; 7530 else if (p == p_save || *p_save == '-' || *p != NUL 7531 || tab_number > LAST_TAB_NR) 7532 { 7533 /* No numbers as argument. */ 7534 eap->errmsg = e_invarg; 7535 goto theend; 7536 } 7537 } 7538 else 7539 { 7540 if (*p_save == NUL) 7541 tab_number = 1; 7542 else if (p == p_save || *p_save == '-' || *p != NUL 7543 || tab_number == 0) 7544 { 7545 /* No numbers as argument. */ 7546 eap->errmsg = e_invarg; 7547 goto theend; 7548 } 7549 tab_number = tab_number * relative + tabpage_index(curtab); 7550 if (!unaccept_arg0 && relative == -1) 7551 --tab_number; 7552 } 7553 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR) 7554 eap->errmsg = e_invarg; 7555 } 7556 else if (eap->addr_count > 0) 7557 { 7558 if (unaccept_arg0 && eap->line2 == 0) 7559 { 7560 eap->errmsg = e_invrange; 7561 tab_number = 0; 7562 } 7563 else 7564 { 7565 tab_number = eap->line2; 7566 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-') 7567 { 7568 --tab_number; 7569 if (tab_number < unaccept_arg0) 7570 eap->errmsg = e_invarg; 7571 } 7572 } 7573 } 7574 else 7575 { 7576 switch (eap->cmdidx) 7577 { 7578 case CMD_tabnext: 7579 tab_number = tabpage_index(curtab) + 1; 7580 if (tab_number > LAST_TAB_NR) 7581 tab_number = 1; 7582 break; 7583 case CMD_tabmove: 7584 tab_number = LAST_TAB_NR; 7585 break; 7586 default: 7587 tab_number = tabpage_index(curtab); 7588 } 7589 } 7590 7591 theend: 7592 return tab_number; 7593 } 7594 7595 /* 7596 * ":tabclose": close current tab page, unless it is the last one. 7597 * ":tabclose N": close tab page N. 7598 */ 7599 static void 7600 ex_tabclose(exarg_T *eap) 7601 { 7602 tabpage_T *tp; 7603 int tab_number; 7604 7605 # ifdef FEAT_CMDWIN 7606 if (cmdwin_type != 0) 7607 cmdwin_result = K_IGNORE; 7608 else 7609 # endif 7610 if (first_tabpage->tp_next == NULL) 7611 emsg(_("E784: Cannot close last tab page")); 7612 else 7613 { 7614 tab_number = get_tabpage_arg(eap); 7615 if (eap->errmsg == NULL) 7616 { 7617 tp = find_tabpage(tab_number); 7618 if (tp == NULL) 7619 { 7620 beep_flush(); 7621 return; 7622 } 7623 if (tp != curtab) 7624 { 7625 tabpage_close_other(tp, eap->forceit); 7626 return; 7627 } 7628 else if (!text_locked() && !curbuf_locked()) 7629 tabpage_close(eap->forceit); 7630 } 7631 } 7632 } 7633 7634 /* 7635 * ":tabonly": close all tab pages except the current one 7636 */ 7637 static void 7638 ex_tabonly(exarg_T *eap) 7639 { 7640 tabpage_T *tp; 7641 int done; 7642 int tab_number; 7643 7644 # ifdef FEAT_CMDWIN 7645 if (cmdwin_type != 0) 7646 cmdwin_result = K_IGNORE; 7647 else 7648 # endif 7649 if (first_tabpage->tp_next == NULL) 7650 msg(_("Already only one tab page")); 7651 else 7652 { 7653 tab_number = get_tabpage_arg(eap); 7654 if (eap->errmsg == NULL) 7655 { 7656 goto_tabpage(tab_number); 7657 /* Repeat this up to a 1000 times, because autocommands may 7658 * mess up the lists. */ 7659 for (done = 0; done < 1000; ++done) 7660 { 7661 FOR_ALL_TABPAGES(tp) 7662 if (tp->tp_topframe != topframe) 7663 { 7664 tabpage_close_other(tp, eap->forceit); 7665 /* if we failed to close it quit */ 7666 if (valid_tabpage(tp)) 7667 done = 1000; 7668 /* start over, "tp" is now invalid */ 7669 break; 7670 } 7671 if (first_tabpage->tp_next == NULL) 7672 break; 7673 } 7674 } 7675 } 7676 } 7677 7678 /* 7679 * Close the current tab page. 7680 */ 7681 void 7682 tabpage_close(int forceit) 7683 { 7684 /* First close all the windows but the current one. If that worked then 7685 * close the last window in this tab, that will close it. */ 7686 if (!ONE_WINDOW) 7687 close_others(TRUE, forceit); 7688 if (ONE_WINDOW) 7689 ex_win_close(forceit, curwin, NULL); 7690 # ifdef FEAT_GUI 7691 need_mouse_correct = TRUE; 7692 # endif 7693 } 7694 7695 /* 7696 * Close tab page "tp", which is not the current tab page. 7697 * Note that autocommands may make "tp" invalid. 7698 * Also takes care of the tab pages line disappearing when closing the 7699 * last-but-one tab page. 7700 */ 7701 void 7702 tabpage_close_other(tabpage_T *tp, int forceit) 7703 { 7704 int done = 0; 7705 win_T *wp; 7706 int h = tabline_height(); 7707 7708 /* Limit to 1000 windows, autocommands may add a window while we close 7709 * one. OK, so I'm paranoid... */ 7710 while (++done < 1000) 7711 { 7712 wp = tp->tp_firstwin; 7713 ex_win_close(forceit, wp, tp); 7714 7715 /* Autocommands may delete the tab page under our fingers and we may 7716 * fail to close a window with a modified buffer. */ 7717 if (!valid_tabpage(tp) || tp->tp_firstwin == wp) 7718 break; 7719 } 7720 7721 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf); 7722 7723 redraw_tabline = TRUE; 7724 if (h != tabline_height()) 7725 shell_new_rows(); 7726 } 7727 7728 /* 7729 * ":only". 7730 */ 7731 static void 7732 ex_only(exarg_T *eap) 7733 { 7734 win_T *wp; 7735 int wnr; 7736 # ifdef FEAT_GUI 7737 need_mouse_correct = TRUE; 7738 # endif 7739 if (eap->addr_count > 0) 7740 { 7741 wnr = eap->line2; 7742 for (wp = firstwin; --wnr > 0; ) 7743 { 7744 if (wp->w_next == NULL) 7745 break; 7746 else 7747 wp = wp->w_next; 7748 } 7749 win_goto(wp); 7750 } 7751 close_others(TRUE, eap->forceit); 7752 } 7753 7754 /* 7755 * ":all" and ":sall". 7756 * Also used for ":tab drop file ..." after setting the argument list. 7757 */ 7758 void 7759 ex_all(exarg_T *eap) 7760 { 7761 if (eap->addr_count == 0) 7762 eap->line2 = 9999; 7763 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop); 7764 } 7765 7766 static void 7767 ex_hide(exarg_T *eap UNUSED) 7768 { 7769 /* ":hide" or ":hide | cmd": hide current window */ 7770 if (!eap->skip) 7771 { 7772 #ifdef FEAT_GUI 7773 need_mouse_correct = TRUE; 7774 #endif 7775 if (eap->addr_count == 0) 7776 win_close(curwin, FALSE); /* don't free buffer */ 7777 else 7778 { 7779 int winnr = 0; 7780 win_T *win; 7781 7782 FOR_ALL_WINDOWS(win) 7783 { 7784 winnr++; 7785 if (winnr == eap->line2) 7786 break; 7787 } 7788 if (win == NULL) 7789 win = lastwin; 7790 win_close(win, FALSE); 7791 } 7792 } 7793 } 7794 7795 /* 7796 * ":stop" and ":suspend": Suspend Vim. 7797 */ 7798 static void 7799 ex_stop(exarg_T *eap) 7800 { 7801 /* 7802 * Disallow suspending for "rvim". 7803 */ 7804 if (!check_restricted()) 7805 { 7806 if (!eap->forceit) 7807 autowrite_all(); 7808 windgoto((int)Rows - 1, 0); 7809 out_char('\n'); 7810 out_flush(); 7811 stoptermcap(); 7812 out_flush(); /* needed for SUN to restore xterm buffer */ 7813 #ifdef FEAT_TITLE 7814 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */ 7815 #endif 7816 ui_suspend(); /* call machine specific function */ 7817 #ifdef FEAT_TITLE 7818 maketitle(); 7819 resettitle(); /* force updating the title */ 7820 #endif 7821 starttermcap(); 7822 scroll_start(); /* scroll screen before redrawing */ 7823 redraw_later_clear(); 7824 shell_resized(); /* may have resized window */ 7825 } 7826 } 7827 7828 /* 7829 * ":exit", ":xit" and ":wq": Write file and quite the current window. 7830 */ 7831 static void 7832 ex_exit(exarg_T *eap) 7833 { 7834 #ifdef FEAT_CMDWIN 7835 if (cmdwin_type != 0) 7836 { 7837 cmdwin_result = Ctrl_C; 7838 return; 7839 } 7840 #endif 7841 /* Don't quit while editing the command line. */ 7842 if (text_locked()) 7843 { 7844 text_locked_msg(); 7845 return; 7846 } 7847 7848 if (before_quit_autocmds(curwin, FALSE, eap->forceit)) 7849 return; 7850 7851 /* 7852 * if more files or windows we won't exit 7853 */ 7854 if (check_more(FALSE, eap->forceit) == OK && only_one_window()) 7855 exiting = TRUE; 7856 if ( ((eap->cmdidx == CMD_wq 7857 || curbufIsChanged()) 7858 && do_write(eap) == FAIL) 7859 || check_more(TRUE, eap->forceit) == FAIL 7860 || (only_one_window() && check_changed_any(eap->forceit, FALSE))) 7861 { 7862 not_exiting(); 7863 } 7864 else 7865 { 7866 if (only_one_window()) /* quit last window, exit Vim */ 7867 getout(0); 7868 not_exiting(); 7869 # ifdef FEAT_GUI 7870 need_mouse_correct = TRUE; 7871 # endif 7872 /* Quit current window, may free the buffer. */ 7873 win_close(curwin, !buf_hide(curwin->w_buffer)); 7874 } 7875 } 7876 7877 /* 7878 * ":print", ":list", ":number". 7879 */ 7880 static void 7881 ex_print(exarg_T *eap) 7882 { 7883 if (curbuf->b_ml.ml_flags & ML_EMPTY) 7884 emsg(_(e_emptybuf)); 7885 else 7886 { 7887 for ( ;!got_int; ui_breakcheck()) 7888 { 7889 print_line(eap->line1, 7890 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound 7891 || (eap->flags & EXFLAG_NR)), 7892 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST)); 7893 if (++eap->line1 > eap->line2) 7894 break; 7895 out_flush(); /* show one line at a time */ 7896 } 7897 setpcmark(); 7898 /* put cursor at last line */ 7899 curwin->w_cursor.lnum = eap->line2; 7900 beginline(BL_SOL | BL_FIX); 7901 } 7902 7903 ex_no_reprint = TRUE; 7904 } 7905 7906 #ifdef FEAT_BYTEOFF 7907 static void 7908 ex_goto(exarg_T *eap) 7909 { 7910 goto_byte(eap->line2); 7911 } 7912 #endif 7913 7914 /* 7915 * ":shell". 7916 */ 7917 static void 7918 ex_shell(exarg_T *eap UNUSED) 7919 { 7920 do_shell(NULL, 0); 7921 } 7922 7923 #if defined(HAVE_DROP_FILE) || defined(PROTO) 7924 7925 static int drop_busy = FALSE; 7926 static int drop_filec; 7927 static char_u **drop_filev = NULL; 7928 static int drop_split; 7929 static void (*drop_callback)(void *); 7930 static void *drop_cookie; 7931 7932 static void 7933 handle_drop_internal(void) 7934 { 7935 exarg_T ea; 7936 int save_msg_scroll = msg_scroll; 7937 7938 // Setting the argument list may cause screen updates and being called 7939 // recursively. Avoid that by setting drop_busy. 7940 drop_busy = TRUE; 7941 7942 /* Check whether the current buffer is changed. If so, we will need 7943 * to split the current window or data could be lost. 7944 * We don't need to check if the 'hidden' option is set, as in this 7945 * case the buffer won't be lost. 7946 */ 7947 if (!buf_hide(curbuf) && !drop_split) 7948 { 7949 ++emsg_off; 7950 drop_split = check_changed(curbuf, CCGD_AW); 7951 --emsg_off; 7952 } 7953 if (drop_split) 7954 { 7955 if (win_split(0, 0) == FAIL) 7956 return; 7957 RESET_BINDING(curwin); 7958 7959 /* When splitting the window, create a new alist. Otherwise the 7960 * existing one is overwritten. */ 7961 alist_unlink(curwin->w_alist); 7962 alist_new(); 7963 } 7964 7965 /* 7966 * Set up the new argument list. 7967 */ 7968 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0); 7969 7970 /* 7971 * Move to the first file. 7972 */ 7973 /* Fake up a minimal "next" command for do_argfile() */ 7974 vim_memset(&ea, 0, sizeof(ea)); 7975 ea.cmd = (char_u *)"next"; 7976 do_argfile(&ea, 0); 7977 7978 /* do_ecmd() may set need_start_insertmode, but since we never left Insert 7979 * mode that is not needed here. */ 7980 need_start_insertmode = FALSE; 7981 7982 /* Restore msg_scroll, otherwise a following command may cause scrolling 7983 * unexpectedly. The screen will be redrawn by the caller, thus 7984 * msg_scroll being set by displaying a message is irrelevant. */ 7985 msg_scroll = save_msg_scroll; 7986 7987 if (drop_callback != NULL) 7988 drop_callback(drop_cookie); 7989 7990 drop_filev = NULL; 7991 drop_busy = FALSE; 7992 } 7993 7994 /* 7995 * Handle a file drop. The code is here because a drop is *nearly* like an 7996 * :args command, but not quite (we have a list of exact filenames, so we 7997 * don't want to (a) parse a command line, or (b) expand wildcards). So the 7998 * code is very similar to :args and hence needs access to a lot of the static 7999 * functions in this file. 8000 * 8001 * The "filev" list must have been allocated using alloc(), as should each item 8002 * in the list. This function takes over responsibility for freeing the "filev" 8003 * list. 8004 */ 8005 void 8006 handle_drop( 8007 int filec, // the number of files dropped 8008 char_u **filev, // the list of files dropped 8009 int split, // force splitting the window 8010 void (*callback)(void *), // to be called after setting the argument 8011 // list 8012 void *cookie) // argument for "callback" (allocated) 8013 { 8014 // Cannot handle recursive drops, finish the pending one. 8015 if (drop_busy) 8016 { 8017 FreeWild(filec, filev); 8018 vim_free(cookie); 8019 return; 8020 } 8021 8022 // When calling handle_drop() more than once in a row we only use the last 8023 // one. 8024 if (drop_filev != NULL) 8025 { 8026 FreeWild(drop_filec, drop_filev); 8027 vim_free(drop_cookie); 8028 } 8029 8030 drop_filec = filec; 8031 drop_filev = filev; 8032 drop_split = split; 8033 drop_callback = callback; 8034 drop_cookie = cookie; 8035 8036 // Postpone this when: 8037 // - editing the command line 8038 // - not possible to change the current buffer 8039 // - updating the screen 8040 // As it may change buffers and window structures that are in use and cause 8041 // freed memory to be used. 8042 if (text_locked() || curbuf_locked() || updating_screen) 8043 return; 8044 8045 handle_drop_internal(); 8046 } 8047 8048 /* 8049 * To be called when text is unlocked, curbuf is unlocked or updating_screen is 8050 * reset: Handle a postponed drop. 8051 */ 8052 void 8053 handle_any_postponed_drop(void) 8054 { 8055 if (!drop_busy && drop_filev != NULL 8056 && !text_locked() && !curbuf_locked() && !updating_screen) 8057 handle_drop_internal(); 8058 } 8059 #endif 8060 8061 /* 8062 * Clear an argument list: free all file names and reset it to zero entries. 8063 */ 8064 void 8065 alist_clear(alist_T *al) 8066 { 8067 while (--al->al_ga.ga_len >= 0) 8068 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname); 8069 ga_clear(&al->al_ga); 8070 } 8071 8072 /* 8073 * Init an argument list. 8074 */ 8075 void 8076 alist_init(alist_T *al) 8077 { 8078 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5); 8079 } 8080 8081 /* 8082 * Remove a reference from an argument list. 8083 * Ignored when the argument list is the global one. 8084 * If the argument list is no longer used by any window, free it. 8085 */ 8086 void 8087 alist_unlink(alist_T *al) 8088 { 8089 if (al != &global_alist && --al->al_refcount <= 0) 8090 { 8091 alist_clear(al); 8092 vim_free(al); 8093 } 8094 } 8095 8096 /* 8097 * Create a new argument list and use it for the current window. 8098 */ 8099 void 8100 alist_new(void) 8101 { 8102 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T)); 8103 if (curwin->w_alist == NULL) 8104 { 8105 curwin->w_alist = &global_alist; 8106 ++global_alist.al_refcount; 8107 } 8108 else 8109 { 8110 curwin->w_alist->al_refcount = 1; 8111 curwin->w_alist->id = ++max_alist_id; 8112 alist_init(curwin->w_alist); 8113 } 8114 } 8115 8116 #if !defined(UNIX) || defined(PROTO) 8117 /* 8118 * Expand the file names in the global argument list. 8119 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer 8120 * numbers to be re-used. 8121 */ 8122 void 8123 alist_expand(int *fnum_list, int fnum_len) 8124 { 8125 char_u **old_arg_files; 8126 int old_arg_count; 8127 char_u **new_arg_files; 8128 int new_arg_file_count; 8129 char_u *save_p_su = p_su; 8130 int i; 8131 8132 /* Don't use 'suffixes' here. This should work like the shell did the 8133 * expansion. Also, the vimrc file isn't read yet, thus the user 8134 * can't set the options. */ 8135 p_su = empty_option; 8136 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT)); 8137 if (old_arg_files != NULL) 8138 { 8139 for (i = 0; i < GARGCOUNT; ++i) 8140 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname); 8141 old_arg_count = GARGCOUNT; 8142 if (expand_wildcards(old_arg_count, old_arg_files, 8143 &new_arg_file_count, &new_arg_files, 8144 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK 8145 && new_arg_file_count > 0) 8146 { 8147 alist_set(&global_alist, new_arg_file_count, new_arg_files, 8148 TRUE, fnum_list, fnum_len); 8149 FreeWild(old_arg_count, old_arg_files); 8150 } 8151 } 8152 p_su = save_p_su; 8153 } 8154 #endif 8155 8156 /* 8157 * Set the argument list for the current window. 8158 * Takes over the allocated files[] and the allocated fnames in it. 8159 */ 8160 void 8161 alist_set( 8162 alist_T *al, 8163 int count, 8164 char_u **files, 8165 int use_curbuf, 8166 int *fnum_list, 8167 int fnum_len) 8168 { 8169 int i; 8170 static int recursive = 0; 8171 8172 if (recursive) 8173 { 8174 emsg(_(e_au_recursive)); 8175 return; 8176 } 8177 ++recursive; 8178 8179 alist_clear(al); 8180 if (ga_grow(&al->al_ga, count) == OK) 8181 { 8182 for (i = 0; i < count; ++i) 8183 { 8184 if (got_int) 8185 { 8186 /* When adding many buffers this can take a long time. Allow 8187 * interrupting here. */ 8188 while (i < count) 8189 vim_free(files[i++]); 8190 break; 8191 } 8192 8193 /* May set buffer name of a buffer previously used for the 8194 * argument list, so that it's re-used by alist_add. */ 8195 if (fnum_list != NULL && i < fnum_len) 8196 buf_set_name(fnum_list[i], files[i]); 8197 8198 alist_add(al, files[i], use_curbuf ? 2 : 1); 8199 ui_breakcheck(); 8200 } 8201 vim_free(files); 8202 } 8203 else 8204 FreeWild(count, files); 8205 if (al == &global_alist) 8206 arg_had_last = FALSE; 8207 8208 --recursive; 8209 } 8210 8211 /* 8212 * Add file "fname" to argument list "al". 8213 * "fname" must have been allocated and "al" must have been checked for room. 8214 */ 8215 void 8216 alist_add( 8217 alist_T *al, 8218 char_u *fname, 8219 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */ 8220 { 8221 if (fname == NULL) /* don't add NULL file names */ 8222 return; 8223 #ifdef BACKSLASH_IN_FILENAME 8224 slash_adjust(fname); 8225 #endif 8226 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname; 8227 if (set_fnum > 0) 8228 AARGLIST(al)[al->al_ga.ga_len].ae_fnum = 8229 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0)); 8230 ++al->al_ga.ga_len; 8231 } 8232 8233 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) 8234 /* 8235 * Adjust slashes in file names. Called after 'shellslash' was set. 8236 */ 8237 void 8238 alist_slash_adjust(void) 8239 { 8240 int i; 8241 win_T *wp; 8242 tabpage_T *tp; 8243 8244 for (i = 0; i < GARGCOUNT; ++i) 8245 if (GARGLIST[i].ae_fname != NULL) 8246 slash_adjust(GARGLIST[i].ae_fname); 8247 FOR_ALL_TAB_WINDOWS(tp, wp) 8248 if (wp->w_alist != &global_alist) 8249 for (i = 0; i < WARGCOUNT(wp); ++i) 8250 if (WARGLIST(wp)[i].ae_fname != NULL) 8251 slash_adjust(WARGLIST(wp)[i].ae_fname); 8252 } 8253 #endif 8254 8255 /* 8256 * ":preserve". 8257 */ 8258 static void 8259 ex_preserve(exarg_T *eap UNUSED) 8260 { 8261 curbuf->b_flags |= BF_PRESERVED; 8262 ml_preserve(curbuf, TRUE); 8263 } 8264 8265 /* 8266 * ":recover". 8267 */ 8268 static void 8269 ex_recover(exarg_T *eap) 8270 { 8271 /* Set recoverymode right away to avoid the ATTENTION prompt. */ 8272 recoverymode = TRUE; 8273 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0) 8274 | CCGD_MULTWIN 8275 | (eap->forceit ? CCGD_FORCEIT : 0) 8276 | CCGD_EXCMD) 8277 8278 && (*eap->arg == NUL 8279 || setfname(curbuf, eap->arg, NULL, TRUE) == OK)) 8280 ml_recover(); 8281 recoverymode = FALSE; 8282 } 8283 8284 /* 8285 * Command modifier used in a wrong way. 8286 */ 8287 static void 8288 ex_wrongmodifier(exarg_T *eap) 8289 { 8290 eap->errmsg = e_invcmd; 8291 } 8292 8293 /* 8294 * :sview [+command] file split window with new file, read-only 8295 * :split [[+command] file] split window with current or new file 8296 * :vsplit [[+command] file] split window vertically with current or new file 8297 * :new [[+command] file] split window with no or new file 8298 * :vnew [[+command] file] split vertically window with no or new file 8299 * :sfind [+command] file split window with file in 'path' 8300 * 8301 * :tabedit open new Tab page with empty window 8302 * :tabedit [+command] file open new Tab page and edit "file" 8303 * :tabnew [[+command] file] just like :tabedit 8304 * :tabfind [+command] file open new Tab page and find "file" 8305 */ 8306 void 8307 ex_splitview(exarg_T *eap) 8308 { 8309 win_T *old_curwin = curwin; 8310 #if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE) 8311 char_u *fname = NULL; 8312 #endif 8313 #ifdef FEAT_BROWSE 8314 int browse_flag = cmdmod.browse; 8315 #endif 8316 int use_tab = eap->cmdidx == CMD_tabedit 8317 || eap->cmdidx == CMD_tabfind 8318 || eap->cmdidx == CMD_tabnew; 8319 8320 #ifdef FEAT_GUI 8321 need_mouse_correct = TRUE; 8322 #endif 8323 8324 #ifdef FEAT_QUICKFIX 8325 /* A ":split" in the quickfix window works like ":new". Don't want two 8326 * quickfix windows. But it's OK when doing ":tab split". */ 8327 if (bt_quickfix(curbuf) && cmdmod.tab == 0) 8328 { 8329 if (eap->cmdidx == CMD_split) 8330 eap->cmdidx = CMD_new; 8331 if (eap->cmdidx == CMD_vsplit) 8332 eap->cmdidx = CMD_vnew; 8333 } 8334 #endif 8335 8336 #ifdef FEAT_SEARCHPATH 8337 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind) 8338 { 8339 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), 8340 FNAME_MESS, TRUE, curbuf->b_ffname); 8341 if (fname == NULL) 8342 goto theend; 8343 eap->arg = fname; 8344 } 8345 # ifdef FEAT_BROWSE 8346 else 8347 # endif 8348 #endif 8349 #ifdef FEAT_BROWSE 8350 if (cmdmod.browse 8351 && eap->cmdidx != CMD_vnew 8352 && eap->cmdidx != CMD_new) 8353 { 8354 if ( 8355 # ifdef FEAT_GUI 8356 !gui.in_use && 8357 # endif 8358 au_has_group((char_u *)"FileExplorer")) 8359 { 8360 /* No browsing supported but we do have the file explorer: 8361 * Edit the directory. */ 8362 if (*eap->arg == NUL || !mch_isdir(eap->arg)) 8363 eap->arg = (char_u *)"."; 8364 } 8365 else 8366 { 8367 fname = do_browse(0, (char_u *)(use_tab 8368 ? _("Edit File in new tab page") 8369 : _("Edit File in new window")), 8370 eap->arg, NULL, NULL, NULL, curbuf); 8371 if (fname == NULL) 8372 goto theend; 8373 eap->arg = fname; 8374 } 8375 } 8376 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */ 8377 #endif 8378 8379 /* 8380 * Either open new tab page or split the window. 8381 */ 8382 if (use_tab) 8383 { 8384 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab 8385 : eap->addr_count == 0 ? 0 8386 : (int)eap->line2 + 1) != FAIL) 8387 { 8388 do_exedit(eap, old_curwin); 8389 8390 /* set the alternate buffer for the window we came from */ 8391 if (curwin != old_curwin 8392 && win_valid(old_curwin) 8393 && old_curwin->w_buffer != curbuf 8394 && !cmdmod.keepalt) 8395 old_curwin->w_alt_fnum = curbuf->b_fnum; 8396 } 8397 } 8398 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0, 8399 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL) 8400 { 8401 /* Reset 'scrollbind' when editing another file, but keep it when 8402 * doing ":split" without arguments. */ 8403 if (*eap->arg != NUL 8404 # ifdef FEAT_BROWSE 8405 || cmdmod.browse 8406 # endif 8407 ) 8408 { 8409 RESET_BINDING(curwin); 8410 } 8411 else 8412 do_check_scrollbind(FALSE); 8413 do_exedit(eap, old_curwin); 8414 } 8415 8416 # ifdef FEAT_BROWSE 8417 cmdmod.browse = browse_flag; 8418 # endif 8419 8420 # if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE) 8421 theend: 8422 vim_free(fname); 8423 # endif 8424 } 8425 8426 /* 8427 * Open a new tab page. 8428 */ 8429 void 8430 tabpage_new(void) 8431 { 8432 exarg_T ea; 8433 8434 vim_memset(&ea, 0, sizeof(ea)); 8435 ea.cmdidx = CMD_tabnew; 8436 ea.cmd = (char_u *)"tabn"; 8437 ea.arg = (char_u *)""; 8438 ex_splitview(&ea); 8439 } 8440 8441 /* 8442 * :tabnext command 8443 */ 8444 static void 8445 ex_tabnext(exarg_T *eap) 8446 { 8447 int tab_number; 8448 8449 switch (eap->cmdidx) 8450 { 8451 case CMD_tabfirst: 8452 case CMD_tabrewind: 8453 goto_tabpage(1); 8454 break; 8455 case CMD_tablast: 8456 goto_tabpage(9999); 8457 break; 8458 case CMD_tabprevious: 8459 case CMD_tabNext: 8460 if (eap->arg && *eap->arg != NUL) 8461 { 8462 char_u *p = eap->arg; 8463 char_u *p_save = p; 8464 8465 tab_number = getdigits(&p); 8466 if (p == p_save || *p_save == '-' || *p != NUL 8467 || tab_number == 0) 8468 { 8469 /* No numbers as argument. */ 8470 eap->errmsg = e_invarg; 8471 return; 8472 } 8473 } 8474 else 8475 { 8476 if (eap->addr_count == 0) 8477 tab_number = 1; 8478 else 8479 { 8480 tab_number = eap->line2; 8481 if (tab_number < 1) 8482 { 8483 eap->errmsg = e_invrange; 8484 return; 8485 } 8486 } 8487 } 8488 goto_tabpage(-tab_number); 8489 break; 8490 default: /* CMD_tabnext */ 8491 tab_number = get_tabpage_arg(eap); 8492 if (eap->errmsg == NULL) 8493 goto_tabpage(tab_number); 8494 break; 8495 } 8496 } 8497 8498 /* 8499 * :tabmove command 8500 */ 8501 static void 8502 ex_tabmove(exarg_T *eap) 8503 { 8504 int tab_number; 8505 8506 tab_number = get_tabpage_arg(eap); 8507 if (eap->errmsg == NULL) 8508 tabpage_move(tab_number); 8509 } 8510 8511 /* 8512 * :tabs command: List tabs and their contents. 8513 */ 8514 static void 8515 ex_tabs(exarg_T *eap UNUSED) 8516 { 8517 tabpage_T *tp; 8518 win_T *wp; 8519 int tabcount = 1; 8520 8521 msg_start(); 8522 msg_scroll = TRUE; 8523 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next) 8524 { 8525 msg_putchar('\n'); 8526 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++); 8527 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T)); 8528 out_flush(); /* output one line at a time */ 8529 ui_breakcheck(); 8530 8531 if (tp == curtab) 8532 wp = firstwin; 8533 else 8534 wp = tp->tp_firstwin; 8535 for ( ; wp != NULL && !got_int; wp = wp->w_next) 8536 { 8537 msg_putchar('\n'); 8538 msg_putchar(wp == curwin ? '>' : ' '); 8539 msg_putchar(' '); 8540 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' '); 8541 msg_putchar(' '); 8542 if (buf_spname(wp->w_buffer) != NULL) 8543 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1); 8544 else 8545 home_replace(wp->w_buffer, wp->w_buffer->b_fname, 8546 IObuff, IOSIZE, TRUE); 8547 msg_outtrans(IObuff); 8548 out_flush(); /* output one line at a time */ 8549 ui_breakcheck(); 8550 } 8551 } 8552 } 8553 8554 /* 8555 * ":mode": Set screen mode. 8556 * If no argument given, just get the screen size and redraw. 8557 */ 8558 static void 8559 ex_mode(exarg_T *eap) 8560 { 8561 if (*eap->arg == NUL) 8562 shell_resized(); 8563 else 8564 mch_screenmode(eap->arg); 8565 } 8566 8567 /* 8568 * ":resize". 8569 * set, increment or decrement current window height 8570 */ 8571 static void 8572 ex_resize(exarg_T *eap) 8573 { 8574 int n; 8575 win_T *wp = curwin; 8576 8577 if (eap->addr_count > 0) 8578 { 8579 n = eap->line2; 8580 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next) 8581 ; 8582 } 8583 8584 # ifdef FEAT_GUI 8585 need_mouse_correct = TRUE; 8586 # endif 8587 n = atol((char *)eap->arg); 8588 if (cmdmod.split & WSP_VERT) 8589 { 8590 if (*eap->arg == '-' || *eap->arg == '+') 8591 n += curwin->w_width; 8592 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */ 8593 n = 9999; 8594 win_setwidth_win((int)n, wp); 8595 } 8596 else 8597 { 8598 if (*eap->arg == '-' || *eap->arg == '+') 8599 n += curwin->w_height; 8600 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */ 8601 n = 9999; 8602 win_setheight_win((int)n, wp); 8603 } 8604 } 8605 8606 /* 8607 * ":find [+command] <file>" command. 8608 */ 8609 static void 8610 ex_find(exarg_T *eap) 8611 { 8612 #ifdef FEAT_SEARCHPATH 8613 char_u *fname; 8614 int count; 8615 8616 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS, 8617 TRUE, curbuf->b_ffname); 8618 if (eap->addr_count > 0) 8619 { 8620 /* Repeat finding the file "count" times. This matters when it 8621 * appears several times in the path. */ 8622 count = eap->line2; 8623 while (fname != NULL && --count > 0) 8624 { 8625 vim_free(fname); 8626 fname = find_file_in_path(NULL, 0, FNAME_MESS, 8627 FALSE, curbuf->b_ffname); 8628 } 8629 } 8630 8631 if (fname != NULL) 8632 { 8633 eap->arg = fname; 8634 #endif 8635 do_exedit(eap, NULL); 8636 #ifdef FEAT_SEARCHPATH 8637 vim_free(fname); 8638 } 8639 #endif 8640 } 8641 8642 /* 8643 * ":open" simulation: for now just work like ":visual". 8644 */ 8645 static void 8646 ex_open(exarg_T *eap) 8647 { 8648 regmatch_T regmatch; 8649 char_u *p; 8650 8651 curwin->w_cursor.lnum = eap->line2; 8652 beginline(BL_SOL | BL_FIX); 8653 if (*eap->arg == '/') 8654 { 8655 /* ":open /pattern/": put cursor in column found with pattern */ 8656 ++eap->arg; 8657 p = skip_regexp(eap->arg, '/', p_magic, NULL); 8658 *p = NUL; 8659 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0); 8660 if (regmatch.regprog != NULL) 8661 { 8662 regmatch.rm_ic = p_ic; 8663 p = ml_get_curline(); 8664 if (vim_regexec(®match, p, (colnr_T)0)) 8665 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p); 8666 else 8667 emsg(_(e_nomatch)); 8668 vim_regfree(regmatch.regprog); 8669 } 8670 /* Move to the NUL, ignore any other arguments. */ 8671 eap->arg += STRLEN(eap->arg); 8672 } 8673 check_cursor(); 8674 8675 eap->cmdidx = CMD_visual; 8676 do_exedit(eap, NULL); 8677 } 8678 8679 /* 8680 * ":edit", ":badd", ":visual". 8681 */ 8682 static void 8683 ex_edit(exarg_T *eap) 8684 { 8685 do_exedit(eap, NULL); 8686 } 8687 8688 /* 8689 * ":edit <file>" command and alikes. 8690 */ 8691 void 8692 do_exedit( 8693 exarg_T *eap, 8694 win_T *old_curwin) /* curwin before doing a split or NULL */ 8695 { 8696 int n; 8697 int need_hide; 8698 int exmode_was = exmode_active; 8699 8700 /* 8701 * ":vi" command ends Ex mode. 8702 */ 8703 if (exmode_active && (eap->cmdidx == CMD_visual 8704 || eap->cmdidx == CMD_view)) 8705 { 8706 exmode_active = FALSE; 8707 if (*eap->arg == NUL) 8708 { 8709 /* Special case: ":global/pat/visual\NLvi-commands" */ 8710 if (global_busy) 8711 { 8712 int rd = RedrawingDisabled; 8713 int nwr = no_wait_return; 8714 int ms = msg_scroll; 8715 #ifdef FEAT_GUI 8716 int he = hold_gui_events; 8717 #endif 8718 8719 if (eap->nextcmd != NULL) 8720 { 8721 stuffReadbuff(eap->nextcmd); 8722 eap->nextcmd = NULL; 8723 } 8724 8725 if (exmode_was != EXMODE_VIM) 8726 settmode(TMODE_RAW); 8727 RedrawingDisabled = 0; 8728 no_wait_return = 0; 8729 need_wait_return = FALSE; 8730 msg_scroll = 0; 8731 #ifdef FEAT_GUI 8732 hold_gui_events = 0; 8733 #endif 8734 must_redraw = CLEAR; 8735 8736 main_loop(FALSE, TRUE); 8737 8738 RedrawingDisabled = rd; 8739 no_wait_return = nwr; 8740 msg_scroll = ms; 8741 #ifdef FEAT_GUI 8742 hold_gui_events = he; 8743 #endif 8744 } 8745 return; 8746 } 8747 } 8748 8749 if ((eap->cmdidx == CMD_new 8750 || eap->cmdidx == CMD_tabnew 8751 || eap->cmdidx == CMD_tabedit 8752 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL) 8753 { 8754 /* ":new" or ":tabnew" without argument: edit an new empty buffer */ 8755 setpcmark(); 8756 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE, 8757 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0), 8758 old_curwin == NULL ? curwin : NULL); 8759 } 8760 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit) 8761 || *eap->arg != NUL 8762 #ifdef FEAT_BROWSE 8763 || cmdmod.browse 8764 #endif 8765 ) 8766 { 8767 /* Can't edit another file when "curbuf_lock" is set. Only ":edit" 8768 * can bring us here, others are stopped earlier. */ 8769 if (*eap->arg != NUL && curbuf_locked()) 8770 return; 8771 8772 n = readonlymode; 8773 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview) 8774 readonlymode = TRUE; 8775 else if (eap->cmdidx == CMD_enew) 8776 readonlymode = FALSE; /* 'readonly' doesn't make sense in an 8777 empty buffer */ 8778 setpcmark(); 8779 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg), 8780 NULL, eap, 8781 /* ":edit" goes to first line if Vi compatible */ 8782 (*eap->arg == NUL && eap->do_ecmd_lnum == 0 8783 && vim_strchr(p_cpo, CPO_GOTO1) != NULL) 8784 ? ECMD_ONE : eap->do_ecmd_lnum, 8785 (buf_hide(curbuf) ? ECMD_HIDE : 0) 8786 + (eap->forceit ? ECMD_FORCEIT : 0) 8787 /* after a split we can use an existing buffer */ 8788 + (old_curwin != NULL ? ECMD_OLDBUF : 0) 8789 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 ) 8790 , old_curwin == NULL ? curwin : NULL) == FAIL) 8791 { 8792 /* Editing the file failed. If the window was split, close it. */ 8793 if (old_curwin != NULL) 8794 { 8795 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1); 8796 if (!need_hide || buf_hide(curbuf)) 8797 { 8798 #if defined(FEAT_EVAL) 8799 cleanup_T cs; 8800 8801 /* Reset the error/interrupt/exception state here so that 8802 * aborting() returns FALSE when closing a window. */ 8803 enter_cleanup(&cs); 8804 #endif 8805 #ifdef FEAT_GUI 8806 need_mouse_correct = TRUE; 8807 #endif 8808 win_close(curwin, !need_hide && !buf_hide(curbuf)); 8809 8810 #if defined(FEAT_EVAL) 8811 /* Restore the error/interrupt/exception state if not 8812 * discarded by a new aborting error, interrupt, or 8813 * uncaught exception. */ 8814 leave_cleanup(&cs); 8815 #endif 8816 } 8817 } 8818 } 8819 else if (readonlymode && curbuf->b_nwindows == 1) 8820 { 8821 /* When editing an already visited buffer, 'readonly' won't be set 8822 * but the previous value is kept. With ":view" and ":sview" we 8823 * want the file to be readonly, except when another window is 8824 * editing the same buffer. */ 8825 curbuf->b_p_ro = TRUE; 8826 } 8827 readonlymode = n; 8828 } 8829 else 8830 { 8831 if (eap->do_ecmd_cmd != NULL) 8832 do_cmdline_cmd(eap->do_ecmd_cmd); 8833 #ifdef FEAT_TITLE 8834 n = curwin->w_arg_idx_invalid; 8835 #endif 8836 check_arg_idx(curwin); 8837 #ifdef FEAT_TITLE 8838 if (n != curwin->w_arg_idx_invalid) 8839 maketitle(); 8840 #endif 8841 } 8842 8843 /* 8844 * if ":split file" worked, set alternate file name in old window to new 8845 * file 8846 */ 8847 if (old_curwin != NULL 8848 && *eap->arg != NUL 8849 && curwin != old_curwin 8850 && win_valid(old_curwin) 8851 && old_curwin->w_buffer != curbuf 8852 && !cmdmod.keepalt) 8853 old_curwin->w_alt_fnum = curbuf->b_fnum; 8854 8855 ex_no_reprint = TRUE; 8856 } 8857 8858 #ifndef FEAT_GUI 8859 /* 8860 * ":gui" and ":gvim" when there is no GUI. 8861 */ 8862 static void 8863 ex_nogui(exarg_T *eap) 8864 { 8865 eap->errmsg = e_nogvim; 8866 } 8867 #endif 8868 8869 #if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) 8870 static void 8871 ex_tearoff(exarg_T *eap) 8872 { 8873 gui_make_tearoff(eap->arg); 8874 } 8875 #endif 8876 8877 #if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \ 8878 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU) 8879 static void 8880 ex_popup(exarg_T *eap) 8881 { 8882 # if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) 8883 if (gui.in_use) 8884 gui_make_popup(eap->arg, eap->forceit); 8885 # ifdef FEAT_TERM_POPUP_MENU 8886 else 8887 # endif 8888 # endif 8889 # ifdef FEAT_TERM_POPUP_MENU 8890 pum_make_popup(eap->arg, eap->forceit); 8891 # endif 8892 } 8893 #endif 8894 8895 static void 8896 ex_swapname(exarg_T *eap UNUSED) 8897 { 8898 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL) 8899 msg(_("No swap file")); 8900 else 8901 msg((char *)curbuf->b_ml.ml_mfp->mf_fname); 8902 } 8903 8904 /* 8905 * ":syncbind" forces all 'scrollbind' windows to have the same relative 8906 * offset. 8907 * (1998-11-02 16:21:01 R. Edward Ralston <[email protected]>) 8908 */ 8909 static void 8910 ex_syncbind(exarg_T *eap UNUSED) 8911 { 8912 win_T *wp; 8913 win_T *save_curwin = curwin; 8914 buf_T *save_curbuf = curbuf; 8915 long topline; 8916 long y; 8917 linenr_T old_linenr = curwin->w_cursor.lnum; 8918 8919 setpcmark(); 8920 8921 /* 8922 * determine max topline 8923 */ 8924 if (curwin->w_p_scb) 8925 { 8926 topline = curwin->w_topline; 8927 FOR_ALL_WINDOWS(wp) 8928 { 8929 if (wp->w_p_scb && wp->w_buffer) 8930 { 8931 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value(); 8932 if (topline > y) 8933 topline = y; 8934 } 8935 } 8936 if (topline < 1) 8937 topline = 1; 8938 } 8939 else 8940 { 8941 topline = 1; 8942 } 8943 8944 8945 /* 8946 * Set all scrollbind windows to the same topline. 8947 */ 8948 FOR_ALL_WINDOWS(curwin) 8949 { 8950 if (curwin->w_p_scb) 8951 { 8952 curbuf = curwin->w_buffer; 8953 y = topline - curwin->w_topline; 8954 if (y > 0) 8955 scrollup(y, TRUE); 8956 else 8957 scrolldown(-y, TRUE); 8958 curwin->w_scbind_pos = topline; 8959 redraw_later(VALID); 8960 cursor_correct(); 8961 curwin->w_redr_status = TRUE; 8962 } 8963 } 8964 curwin = save_curwin; 8965 curbuf = save_curbuf; 8966 if (curwin->w_p_scb) 8967 { 8968 did_syncbind = TRUE; 8969 checkpcmark(); 8970 if (old_linenr != curwin->w_cursor.lnum) 8971 { 8972 char_u ctrl_o[2]; 8973 8974 ctrl_o[0] = Ctrl_O; 8975 ctrl_o[1] = 0; 8976 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE); 8977 } 8978 } 8979 } 8980 8981 8982 static void 8983 ex_read(exarg_T *eap) 8984 { 8985 int i; 8986 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY); 8987 linenr_T lnum; 8988 8989 if (eap->usefilter) /* :r!cmd */ 8990 do_bang(1, eap, FALSE, FALSE, TRUE); 8991 else 8992 { 8993 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL) 8994 return; 8995 8996 #ifdef FEAT_BROWSE 8997 if (cmdmod.browse) 8998 { 8999 char_u *browseFile; 9000 9001 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg, 9002 NULL, NULL, NULL, curbuf); 9003 if (browseFile != NULL) 9004 { 9005 i = readfile(browseFile, NULL, 9006 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); 9007 vim_free(browseFile); 9008 } 9009 else 9010 i = OK; 9011 } 9012 else 9013 #endif 9014 if (*eap->arg == NUL) 9015 { 9016 if (check_fname() == FAIL) /* check for no file name */ 9017 return; 9018 i = readfile(curbuf->b_ffname, curbuf->b_fname, 9019 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); 9020 } 9021 else 9022 { 9023 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL) 9024 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1); 9025 i = readfile(eap->arg, NULL, 9026 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); 9027 9028 } 9029 if (i != OK) 9030 { 9031 #if defined(FEAT_EVAL) 9032 if (!aborting()) 9033 #endif 9034 semsg(_(e_notopen), eap->arg); 9035 } 9036 else 9037 { 9038 if (empty && exmode_active) 9039 { 9040 /* Delete the empty line that remains. Historically ex does 9041 * this but vi doesn't. */ 9042 if (eap->line2 == 0) 9043 lnum = curbuf->b_ml.ml_line_count; 9044 else 9045 lnum = 1; 9046 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK) 9047 { 9048 ml_delete(lnum, FALSE); 9049 if (curwin->w_cursor.lnum > 1 9050 && curwin->w_cursor.lnum >= lnum) 9051 --curwin->w_cursor.lnum; 9052 deleted_lines_mark(lnum, 1L); 9053 } 9054 } 9055 redraw_curbuf_later(VALID); 9056 } 9057 } 9058 } 9059 9060 static char_u *prev_dir = NULL; 9061 9062 #if defined(EXITFREE) || defined(PROTO) 9063 void 9064 free_cd_dir(void) 9065 { 9066 VIM_CLEAR(prev_dir); 9067 VIM_CLEAR(globaldir); 9068 } 9069 #endif 9070 9071 /* 9072 * Deal with the side effects of changing the current directory. 9073 * When "local" is TRUE then this was after an ":lcd" command. 9074 */ 9075 void 9076 post_chdir(int local) 9077 { 9078 VIM_CLEAR(curwin->w_localdir); 9079 if (local) 9080 { 9081 /* If still in global directory, need to remember current 9082 * directory as global directory. */ 9083 if (globaldir == NULL && prev_dir != NULL) 9084 globaldir = vim_strsave(prev_dir); 9085 /* Remember this local directory for the window. */ 9086 if (mch_dirname(NameBuff, MAXPATHL) == OK) 9087 curwin->w_localdir = vim_strsave(NameBuff); 9088 } 9089 else 9090 { 9091 /* We are now in the global directory, no need to remember its 9092 * name. */ 9093 VIM_CLEAR(globaldir); 9094 } 9095 9096 shorten_fnames(TRUE); 9097 } 9098 9099 9100 /* 9101 * ":cd", ":lcd", ":chdir" and ":lchdir". 9102 */ 9103 void 9104 ex_cd(exarg_T *eap) 9105 { 9106 char_u *new_dir; 9107 char_u *tofree; 9108 int dir_differs; 9109 9110 new_dir = eap->arg; 9111 #if !defined(UNIX) && !defined(VMS) 9112 /* for non-UNIX ":cd" means: print current directory */ 9113 if (*new_dir == NUL) 9114 ex_pwd(NULL); 9115 else 9116 #endif 9117 { 9118 if (allbuf_locked()) 9119 return; 9120 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() 9121 && !eap->forceit) 9122 { 9123 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)")); 9124 return; 9125 } 9126 9127 /* ":cd -": Change to previous directory */ 9128 if (STRCMP(new_dir, "-") == 0) 9129 { 9130 if (prev_dir == NULL) 9131 { 9132 emsg(_("E186: No previous directory")); 9133 return; 9134 } 9135 new_dir = prev_dir; 9136 } 9137 9138 /* Save current directory for next ":cd -" */ 9139 tofree = prev_dir; 9140 if (mch_dirname(NameBuff, MAXPATHL) == OK) 9141 prev_dir = vim_strsave(NameBuff); 9142 else 9143 prev_dir = NULL; 9144 9145 #if defined(UNIX) || defined(VMS) 9146 /* for UNIX ":cd" means: go to home directory */ 9147 if (*new_dir == NUL) 9148 { 9149 /* use NameBuff for home directory name */ 9150 # ifdef VMS 9151 char_u *p; 9152 9153 p = mch_getenv((char_u *)"SYS$LOGIN"); 9154 if (p == NULL || *p == NUL) /* empty is the same as not set */ 9155 NameBuff[0] = NUL; 9156 else 9157 vim_strncpy(NameBuff, p, MAXPATHL - 1); 9158 # else 9159 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL); 9160 # endif 9161 new_dir = NameBuff; 9162 } 9163 #endif 9164 dir_differs = new_dir == NULL || prev_dir == NULL 9165 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0; 9166 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir))) 9167 emsg(_(e_failed)); 9168 else 9169 { 9170 int is_local_chdir = eap->cmdidx == CMD_lcd 9171 || eap->cmdidx == CMD_lchdir; 9172 9173 post_chdir(is_local_chdir); 9174 9175 /* Echo the new current directory if the command was typed. */ 9176 if (KeyTyped || p_verbose >= 5) 9177 ex_pwd(eap); 9178 9179 if (dir_differs) 9180 apply_autocmds(EVENT_DIRCHANGED, 9181 is_local_chdir ? (char_u *)"window" : (char_u *)"global", 9182 new_dir, FALSE, curbuf); 9183 } 9184 vim_free(tofree); 9185 } 9186 } 9187 9188 /* 9189 * ":pwd". 9190 */ 9191 static void 9192 ex_pwd(exarg_T *eap UNUSED) 9193 { 9194 if (mch_dirname(NameBuff, MAXPATHL) == OK) 9195 { 9196 #ifdef BACKSLASH_IN_FILENAME 9197 slash_adjust(NameBuff); 9198 #endif 9199 msg((char *)NameBuff); 9200 } 9201 else 9202 emsg(_("E187: Unknown")); 9203 } 9204 9205 /* 9206 * ":=". 9207 */ 9208 static void 9209 ex_equal(exarg_T *eap) 9210 { 9211 smsg("%ld", (long)eap->line2); 9212 ex_may_print(eap); 9213 } 9214 9215 static void 9216 ex_sleep(exarg_T *eap) 9217 { 9218 int n; 9219 long len; 9220 9221 if (cursor_valid()) 9222 { 9223 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled; 9224 if (n >= 0) 9225 windgoto((int)n, curwin->w_wincol + curwin->w_wcol); 9226 } 9227 9228 len = eap->line2; 9229 switch (*eap->arg) 9230 { 9231 case 'm': break; 9232 case NUL: len *= 1000L; break; 9233 default: semsg(_(e_invarg2), eap->arg); return; 9234 } 9235 do_sleep(len); 9236 } 9237 9238 /* 9239 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second. 9240 */ 9241 void 9242 do_sleep(long msec) 9243 { 9244 long done; 9245 long wait_now; 9246 9247 cursor_on(); 9248 out_flush_cursor(FALSE, FALSE); 9249 for (done = 0; !got_int && done < msec; done += wait_now) 9250 { 9251 wait_now = msec - done > 1000L ? 1000L : msec - done; 9252 #ifdef FEAT_TIMERS 9253 { 9254 long due_time = check_due_timer(); 9255 9256 if (due_time > 0 && due_time < wait_now) 9257 wait_now = due_time; 9258 } 9259 #endif 9260 #ifdef FEAT_JOB_CHANNEL 9261 if (has_any_channel() && wait_now > 100L) 9262 wait_now = 100L; 9263 #endif 9264 ui_delay(wait_now, TRUE); 9265 #ifdef FEAT_JOB_CHANNEL 9266 if (has_any_channel()) 9267 ui_breakcheck_force(TRUE); 9268 else 9269 #endif 9270 ui_breakcheck(); 9271 #ifdef MESSAGE_QUEUE 9272 /* Process the netbeans and clientserver messages that may have been 9273 * received in the call to ui_breakcheck() when the GUI is in use. This 9274 * may occur when running a test case. */ 9275 parse_queued_messages(); 9276 #endif 9277 } 9278 9279 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the 9280 // input buffer, otherwise a following call to input() fails. 9281 if (got_int) 9282 (void)vpeekc(); 9283 } 9284 9285 static void 9286 do_exmap(exarg_T *eap, int isabbrev) 9287 { 9288 int mode; 9289 char_u *cmdp; 9290 9291 cmdp = eap->cmd; 9292 mode = get_map_mode(&cmdp, eap->forceit || isabbrev); 9293 9294 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'), 9295 eap->arg, mode, isabbrev)) 9296 { 9297 case 1: emsg(_(e_invarg)); 9298 break; 9299 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap))); 9300 break; 9301 } 9302 } 9303 9304 /* 9305 * ":winsize" command (obsolete). 9306 */ 9307 static void 9308 ex_winsize(exarg_T *eap) 9309 { 9310 int w, h; 9311 char_u *arg = eap->arg; 9312 char_u *p; 9313 9314 w = getdigits(&arg); 9315 arg = skipwhite(arg); 9316 p = arg; 9317 h = getdigits(&arg); 9318 if (*p != NUL && *arg == NUL) 9319 set_shellsize(w, h, TRUE); 9320 else 9321 emsg(_("E465: :winsize requires two number arguments")); 9322 } 9323 9324 static void 9325 ex_wincmd(exarg_T *eap) 9326 { 9327 int xchar = NUL; 9328 char_u *p; 9329 9330 if (*eap->arg == 'g' || *eap->arg == Ctrl_G) 9331 { 9332 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */ 9333 if (eap->arg[1] == NUL) 9334 { 9335 emsg(_(e_invarg)); 9336 return; 9337 } 9338 xchar = eap->arg[1]; 9339 p = eap->arg + 2; 9340 } 9341 else 9342 p = eap->arg + 1; 9343 9344 eap->nextcmd = check_nextcmd(p); 9345 p = skipwhite(p); 9346 if (*p != NUL && *p != '"' && eap->nextcmd == NULL) 9347 emsg(_(e_invarg)); 9348 else if (!eap->skip) 9349 { 9350 /* Pass flags on for ":vertical wincmd ]". */ 9351 postponed_split_flags = cmdmod.split; 9352 postponed_split_tab = cmdmod.tab; 9353 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar); 9354 postponed_split_flags = 0; 9355 postponed_split_tab = 0; 9356 } 9357 } 9358 9359 #if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN) 9360 /* 9361 * ":winpos". 9362 */ 9363 static void 9364 ex_winpos(exarg_T *eap) 9365 { 9366 int x, y; 9367 char_u *arg = eap->arg; 9368 char_u *p; 9369 9370 if (*arg == NUL) 9371 { 9372 # if defined(FEAT_GUI) || defined(MSWIN) 9373 # ifdef FEAT_GUI 9374 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL) 9375 # else 9376 if (mch_get_winpos(&x, &y) != FAIL) 9377 # endif 9378 { 9379 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y); 9380 msg((char *)IObuff); 9381 } 9382 else 9383 # endif 9384 emsg(_("E188: Obtaining window position not implemented for this platform")); 9385 } 9386 else 9387 { 9388 x = getdigits(&arg); 9389 arg = skipwhite(arg); 9390 p = arg; 9391 y = getdigits(&arg); 9392 if (*p == NUL || *arg != NUL) 9393 { 9394 emsg(_("E466: :winpos requires two number arguments")); 9395 return; 9396 } 9397 # ifdef FEAT_GUI 9398 if (gui.in_use) 9399 gui_mch_set_winpos(x, y); 9400 else if (gui.starting) 9401 { 9402 /* Remember the coordinates for when the window is opened. */ 9403 gui_win_x = x; 9404 gui_win_y = y; 9405 } 9406 # ifdef HAVE_TGETENT 9407 else 9408 # endif 9409 # else 9410 # ifdef MSWIN 9411 mch_set_winpos(x, y); 9412 # endif 9413 # endif 9414 # ifdef HAVE_TGETENT 9415 if (*T_CWP) 9416 term_set_winpos(x, y); 9417 # endif 9418 } 9419 } 9420 #endif 9421 9422 /* 9423 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<". 9424 */ 9425 static void 9426 ex_operators(exarg_T *eap) 9427 { 9428 oparg_T oa; 9429 9430 clear_oparg(&oa); 9431 oa.regname = eap->regname; 9432 oa.start.lnum = eap->line1; 9433 oa.end.lnum = eap->line2; 9434 oa.line_count = eap->line2 - eap->line1 + 1; 9435 oa.motion_type = MLINE; 9436 virtual_op = FALSE; 9437 if (eap->cmdidx != CMD_yank) /* position cursor for undo */ 9438 { 9439 setpcmark(); 9440 curwin->w_cursor.lnum = eap->line1; 9441 beginline(BL_SOL | BL_FIX); 9442 } 9443 9444 if (VIsual_active) 9445 end_visual_mode(); 9446 9447 switch (eap->cmdidx) 9448 { 9449 case CMD_delete: 9450 oa.op_type = OP_DELETE; 9451 op_delete(&oa); 9452 break; 9453 9454 case CMD_yank: 9455 oa.op_type = OP_YANK; 9456 (void)op_yank(&oa, FALSE, TRUE); 9457 break; 9458 9459 default: /* CMD_rshift or CMD_lshift */ 9460 if ( 9461 #ifdef FEAT_RIGHTLEFT 9462 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl 9463 #else 9464 eap->cmdidx == CMD_rshift 9465 #endif 9466 ) 9467 oa.op_type = OP_RSHIFT; 9468 else 9469 oa.op_type = OP_LSHIFT; 9470 op_shift(&oa, FALSE, eap->amount); 9471 break; 9472 } 9473 virtual_op = MAYBE; 9474 ex_may_print(eap); 9475 } 9476 9477 /* 9478 * ":put". 9479 */ 9480 static void 9481 ex_put(exarg_T *eap) 9482 { 9483 /* ":0put" works like ":1put!". */ 9484 if (eap->line2 == 0) 9485 { 9486 eap->line2 = 1; 9487 eap->forceit = TRUE; 9488 } 9489 curwin->w_cursor.lnum = eap->line2; 9490 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L, 9491 PUT_LINE|PUT_CURSLINE); 9492 } 9493 9494 /* 9495 * Handle ":copy" and ":move". 9496 */ 9497 static void 9498 ex_copymove(exarg_T *eap) 9499 { 9500 long n; 9501 9502 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1); 9503 if (eap->arg == NULL) /* error detected */ 9504 { 9505 eap->nextcmd = NULL; 9506 return; 9507 } 9508 get_flags(eap); 9509 9510 /* 9511 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n' 9512 */ 9513 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count) 9514 { 9515 emsg(_(e_invaddr)); 9516 return; 9517 } 9518 9519 if (eap->cmdidx == CMD_move) 9520 { 9521 if (do_move(eap->line1, eap->line2, n) == FAIL) 9522 return; 9523 } 9524 else 9525 ex_copy(eap->line1, eap->line2, n); 9526 u_clearline(); 9527 beginline(BL_SOL | BL_FIX); 9528 ex_may_print(eap); 9529 } 9530 9531 /* 9532 * Print the current line if flags were given to the Ex command. 9533 */ 9534 void 9535 ex_may_print(exarg_T *eap) 9536 { 9537 if (eap->flags != 0) 9538 { 9539 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR), 9540 (eap->flags & EXFLAG_LIST)); 9541 ex_no_reprint = TRUE; 9542 } 9543 } 9544 9545 /* 9546 * ":smagic" and ":snomagic". 9547 */ 9548 static void 9549 ex_submagic(exarg_T *eap) 9550 { 9551 int magic_save = p_magic; 9552 9553 p_magic = (eap->cmdidx == CMD_smagic); 9554 do_sub(eap); 9555 p_magic = magic_save; 9556 } 9557 9558 /* 9559 * ":join". 9560 */ 9561 static void 9562 ex_join(exarg_T *eap) 9563 { 9564 curwin->w_cursor.lnum = eap->line1; 9565 if (eap->line1 == eap->line2) 9566 { 9567 if (eap->addr_count >= 2) /* :2,2join does nothing */ 9568 return; 9569 if (eap->line2 == curbuf->b_ml.ml_line_count) 9570 { 9571 beep_flush(); 9572 return; 9573 } 9574 ++eap->line2; 9575 } 9576 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE); 9577 beginline(BL_WHITE | BL_FIX); 9578 ex_may_print(eap); 9579 } 9580 9581 /* 9582 * ":[addr]@r" or ":[addr]*r": execute register 9583 */ 9584 static void 9585 ex_at(exarg_T *eap) 9586 { 9587 int c; 9588 int prev_len = typebuf.tb_len; 9589 9590 curwin->w_cursor.lnum = eap->line2; 9591 check_cursor_col(); 9592 9593 #ifdef USE_ON_FLY_SCROLL 9594 dont_scroll = TRUE; /* disallow scrolling here */ 9595 #endif 9596 9597 /* get the register name. No name means to use the previous one */ 9598 c = *eap->arg; 9599 if (c == NUL || (c == '*' && *eap->cmd == '*')) 9600 c = '@'; 9601 /* Put the register in the typeahead buffer with the "silent" flag. */ 9602 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE) 9603 == FAIL) 9604 { 9605 beep_flush(); 9606 } 9607 else 9608 { 9609 int save_efr = exec_from_reg; 9610 9611 exec_from_reg = TRUE; 9612 9613 /* 9614 * Execute from the typeahead buffer. 9615 * Continue until the stuff buffer is empty and all added characters 9616 * have been consumed. 9617 */ 9618 while (!stuff_empty() || typebuf.tb_len > prev_len) 9619 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE); 9620 9621 exec_from_reg = save_efr; 9622 } 9623 } 9624 9625 /* 9626 * ":!". 9627 */ 9628 static void 9629 ex_bang(exarg_T *eap) 9630 { 9631 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE); 9632 } 9633 9634 /* 9635 * ":undo". 9636 */ 9637 static void 9638 ex_undo(exarg_T *eap) 9639 { 9640 if (eap->addr_count == 1) /* :undo 123 */ 9641 undo_time(eap->line2, FALSE, FALSE, TRUE); 9642 else 9643 u_undo(1); 9644 } 9645 9646 #ifdef FEAT_PERSISTENT_UNDO 9647 static void 9648 ex_wundo(exarg_T *eap) 9649 { 9650 char_u hash[UNDO_HASH_SIZE]; 9651 9652 u_compute_hash(hash); 9653 u_write_undo(eap->arg, eap->forceit, curbuf, hash); 9654 } 9655 9656 static void 9657 ex_rundo(exarg_T *eap) 9658 { 9659 char_u hash[UNDO_HASH_SIZE]; 9660 9661 u_compute_hash(hash); 9662 u_read_undo(eap->arg, hash, NULL); 9663 } 9664 #endif 9665 9666 /* 9667 * ":redo". 9668 */ 9669 static void 9670 ex_redo(exarg_T *eap UNUSED) 9671 { 9672 u_redo(1); 9673 } 9674 9675 /* 9676 * ":earlier" and ":later". 9677 */ 9678 static void 9679 ex_later(exarg_T *eap) 9680 { 9681 long count = 0; 9682 int sec = FALSE; 9683 int file = FALSE; 9684 char_u *p = eap->arg; 9685 9686 if (*p == NUL) 9687 count = 1; 9688 else if (isdigit(*p)) 9689 { 9690 count = getdigits(&p); 9691 switch (*p) 9692 { 9693 case 's': ++p; sec = TRUE; break; 9694 case 'm': ++p; sec = TRUE; count *= 60; break; 9695 case 'h': ++p; sec = TRUE; count *= 60 * 60; break; 9696 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break; 9697 case 'f': ++p; file = TRUE; break; 9698 } 9699 } 9700 9701 if (*p != NUL) 9702 semsg(_(e_invarg2), eap->arg); 9703 else 9704 undo_time(eap->cmdidx == CMD_earlier ? -count : count, 9705 sec, file, FALSE); 9706 } 9707 9708 /* 9709 * ":redir": start/stop redirection. 9710 */ 9711 static void 9712 ex_redir(exarg_T *eap) 9713 { 9714 char *mode; 9715 char_u *fname; 9716 char_u *arg = eap->arg; 9717 9718 #ifdef FEAT_EVAL 9719 if (redir_execute) 9720 { 9721 emsg(_("E930: Cannot use :redir inside execute()")); 9722 return; 9723 } 9724 #endif 9725 9726 if (STRICMP(eap->arg, "END") == 0) 9727 close_redir(); 9728 else 9729 { 9730 if (*arg == '>') 9731 { 9732 ++arg; 9733 if (*arg == '>') 9734 { 9735 ++arg; 9736 mode = "a"; 9737 } 9738 else 9739 mode = "w"; 9740 arg = skipwhite(arg); 9741 9742 close_redir(); 9743 9744 /* Expand environment variables and "~/". */ 9745 fname = expand_env_save(arg); 9746 if (fname == NULL) 9747 return; 9748 #ifdef FEAT_BROWSE 9749 if (cmdmod.browse) 9750 { 9751 char_u *browseFile; 9752 9753 browseFile = do_browse(BROWSE_SAVE, 9754 (char_u *)_("Save Redirection"), 9755 fname, NULL, NULL, 9756 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf); 9757 if (browseFile == NULL) 9758 return; /* operation cancelled */ 9759 vim_free(fname); 9760 fname = browseFile; 9761 eap->forceit = TRUE; /* since dialog already asked */ 9762 } 9763 #endif 9764 9765 redir_fd = open_exfile(fname, eap->forceit, mode); 9766 vim_free(fname); 9767 } 9768 #ifdef FEAT_EVAL 9769 else if (*arg == '@') 9770 { 9771 /* redirect to a register a-z (resp. A-Z for appending) */ 9772 close_redir(); 9773 ++arg; 9774 if (ASCII_ISALPHA(*arg) 9775 # ifdef FEAT_CLIPBOARD 9776 || *arg == '*' 9777 || *arg == '+' 9778 # endif 9779 || *arg == '"') 9780 { 9781 redir_reg = *arg++; 9782 if (*arg == '>' && arg[1] == '>') /* append */ 9783 arg += 2; 9784 else 9785 { 9786 /* Can use both "@a" and "@a>". */ 9787 if (*arg == '>') 9788 arg++; 9789 /* Make register empty when not using @A-@Z and the 9790 * command is valid. */ 9791 if (*arg == NUL && !isupper(redir_reg)) 9792 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE); 9793 } 9794 } 9795 if (*arg != NUL) 9796 { 9797 redir_reg = 0; 9798 semsg(_(e_invarg2), eap->arg); 9799 } 9800 } 9801 else if (*arg == '=' && arg[1] == '>') 9802 { 9803 int append; 9804 9805 /* redirect to a variable */ 9806 close_redir(); 9807 arg += 2; 9808 9809 if (*arg == '>') 9810 { 9811 ++arg; 9812 append = TRUE; 9813 } 9814 else 9815 append = FALSE; 9816 9817 if (var_redir_start(skipwhite(arg), append) == OK) 9818 redir_vname = 1; 9819 } 9820 #endif 9821 9822 /* TODO: redirect to a buffer */ 9823 9824 else 9825 semsg(_(e_invarg2), eap->arg); 9826 } 9827 9828 /* Make sure redirection is not off. Can happen for cmdline completion 9829 * that indirectly invokes a command to catch its output. */ 9830 if (redir_fd != NULL 9831 #ifdef FEAT_EVAL 9832 || redir_reg || redir_vname 9833 #endif 9834 ) 9835 redir_off = FALSE; 9836 } 9837 9838 /* 9839 * ":redraw": force redraw 9840 */ 9841 void 9842 ex_redraw(exarg_T *eap) 9843 { 9844 int r = RedrawingDisabled; 9845 int p = p_lz; 9846 9847 RedrawingDisabled = 0; 9848 p_lz = FALSE; 9849 validate_cursor(); 9850 update_topline(); 9851 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0); 9852 #ifdef FEAT_TITLE 9853 if (need_maketitle) 9854 maketitle(); 9855 #endif 9856 #if defined(MSWIN) && !defined(FEAT_GUI_MSWIN) 9857 resize_console_buf(); 9858 #endif 9859 RedrawingDisabled = r; 9860 p_lz = p; 9861 9862 /* Reset msg_didout, so that a message that's there is overwritten. */ 9863 msg_didout = FALSE; 9864 msg_col = 0; 9865 9866 /* No need to wait after an intentional redraw. */ 9867 need_wait_return = FALSE; 9868 9869 out_flush(); 9870 } 9871 9872 /* 9873 * ":redrawstatus": force redraw of status line(s) 9874 */ 9875 static void 9876 ex_redrawstatus(exarg_T *eap UNUSED) 9877 { 9878 int r = RedrawingDisabled; 9879 int p = p_lz; 9880 9881 RedrawingDisabled = 0; 9882 p_lz = FALSE; 9883 if (eap->forceit) 9884 status_redraw_all(); 9885 else 9886 status_redraw_curbuf(); 9887 update_screen(VIsual_active ? INVERTED : 0); 9888 RedrawingDisabled = r; 9889 p_lz = p; 9890 out_flush(); 9891 } 9892 9893 /* 9894 * ":redrawtabline": force redraw of the tabline 9895 */ 9896 static void 9897 ex_redrawtabline(exarg_T *eap UNUSED) 9898 { 9899 int r = RedrawingDisabled; 9900 int p = p_lz; 9901 9902 RedrawingDisabled = 0; 9903 p_lz = FALSE; 9904 9905 draw_tabline(); 9906 9907 RedrawingDisabled = r; 9908 p_lz = p; 9909 out_flush(); 9910 } 9911 9912 static void 9913 close_redir(void) 9914 { 9915 if (redir_fd != NULL) 9916 { 9917 fclose(redir_fd); 9918 redir_fd = NULL; 9919 } 9920 #ifdef FEAT_EVAL 9921 redir_reg = 0; 9922 if (redir_vname) 9923 { 9924 var_redir_stop(); 9925 redir_vname = 0; 9926 } 9927 #endif 9928 } 9929 9930 #if defined(FEAT_SESSION) && defined(USE_CRNL) 9931 # define MKSESSION_NL 9932 static int mksession_nl = FALSE; /* use NL only in put_eol() */ 9933 #endif 9934 9935 /* 9936 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession". 9937 */ 9938 static void 9939 ex_mkrc( 9940 exarg_T *eap) 9941 { 9942 FILE *fd; 9943 int failed = FALSE; 9944 char_u *fname; 9945 #ifdef FEAT_BROWSE 9946 char_u *browseFile = NULL; 9947 #endif 9948 #ifdef FEAT_SESSION 9949 int view_session = FALSE; 9950 int using_vdir = FALSE; /* using 'viewdir'? */ 9951 char_u *viewFile = NULL; 9952 unsigned *flagp; 9953 #endif 9954 9955 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview) 9956 { 9957 #ifdef FEAT_SESSION 9958 view_session = TRUE; 9959 #else 9960 ex_ni(eap); 9961 return; 9962 #endif 9963 } 9964 9965 #ifdef FEAT_SESSION 9966 /* Use the short file name until ":lcd" is used. We also don't use the 9967 * short file name when 'acd' is set, that is checked later. */ 9968 did_lcd = FALSE; 9969 9970 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */ 9971 if (eap->cmdidx == CMD_mkview 9972 && (*eap->arg == NUL 9973 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL))) 9974 { 9975 eap->forceit = TRUE; 9976 fname = get_view_file(*eap->arg); 9977 if (fname == NULL) 9978 return; 9979 viewFile = fname; 9980 using_vdir = TRUE; 9981 } 9982 else 9983 #endif 9984 if (*eap->arg != NUL) 9985 fname = eap->arg; 9986 else if (eap->cmdidx == CMD_mkvimrc) 9987 fname = (char_u *)VIMRC_FILE; 9988 #ifdef FEAT_SESSION 9989 else if (eap->cmdidx == CMD_mksession) 9990 fname = (char_u *)SESSION_FILE; 9991 #endif 9992 else 9993 fname = (char_u *)EXRC_FILE; 9994 9995 #ifdef FEAT_BROWSE 9996 if (cmdmod.browse) 9997 { 9998 browseFile = do_browse(BROWSE_SAVE, 9999 # ifdef FEAT_SESSION 10000 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") : 10001 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") : 10002 # endif 10003 (char_u *)_("Save Setup"), 10004 fname, (char_u *)"vim", NULL, 10005 (char_u *)_(BROWSE_FILTER_MACROS), NULL); 10006 if (browseFile == NULL) 10007 goto theend; 10008 fname = browseFile; 10009 eap->forceit = TRUE; /* since dialog already asked */ 10010 } 10011 #endif 10012 10013 #if defined(FEAT_SESSION) && defined(vim_mkdir) 10014 /* When using 'viewdir' may have to create the directory. */ 10015 if (using_vdir && !mch_isdir(p_vdir)) 10016 vim_mkdir_emsg(p_vdir, 0755); 10017 #endif 10018 10019 fd = open_exfile(fname, eap->forceit, WRITEBIN); 10020 if (fd != NULL) 10021 { 10022 #ifdef FEAT_SESSION 10023 if (eap->cmdidx == CMD_mkview) 10024 flagp = &vop_flags; 10025 else 10026 flagp = &ssop_flags; 10027 #endif 10028 10029 #ifdef MKSESSION_NL 10030 /* "unix" in 'sessionoptions': use NL line separator */ 10031 if (view_session && (*flagp & SSOP_UNIX)) 10032 mksession_nl = TRUE; 10033 #endif 10034 10035 /* Write the version command for :mkvimrc */ 10036 if (eap->cmdidx == CMD_mkvimrc) 10037 (void)put_line(fd, "version 6.0"); 10038 10039 #ifdef FEAT_SESSION 10040 if (eap->cmdidx == CMD_mksession) 10041 { 10042 if (put_line(fd, "let SessionLoad = 1") == FAIL) 10043 failed = TRUE; 10044 } 10045 10046 if (eap->cmdidx != CMD_mkview) 10047 #endif 10048 { 10049 /* Write setting 'compatible' first, because it has side effects. 10050 * For that same reason only do it when needed. */ 10051 if (p_cp) 10052 (void)put_line(fd, "if !&cp | set cp | endif"); 10053 else 10054 (void)put_line(fd, "if &cp | set nocp | endif"); 10055 } 10056 10057 #ifdef FEAT_SESSION 10058 if (!view_session 10059 || (eap->cmdidx == CMD_mksession 10060 && (*flagp & SSOP_OPTIONS))) 10061 #endif 10062 failed |= (makemap(fd, NULL) == FAIL 10063 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL); 10064 10065 #ifdef FEAT_SESSION 10066 if (!failed && view_session) 10067 { 10068 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL) 10069 failed = TRUE; 10070 if (eap->cmdidx == CMD_mksession) 10071 { 10072 char_u *dirnow; /* current directory */ 10073 10074 dirnow = alloc(MAXPATHL); 10075 if (dirnow == NULL) 10076 failed = TRUE; 10077 else 10078 { 10079 /* 10080 * Change to session file's dir. 10081 */ 10082 if (mch_dirname(dirnow, MAXPATHL) == FAIL 10083 || mch_chdir((char *)dirnow) != 0) 10084 *dirnow = NUL; 10085 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR)) 10086 { 10087 if (vim_chdirfile(fname, NULL) == OK) 10088 shorten_fnames(TRUE); 10089 } 10090 else if (*dirnow != NUL 10091 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL) 10092 { 10093 if (mch_chdir((char *)globaldir) == 0) 10094 shorten_fnames(TRUE); 10095 } 10096 10097 failed |= (makeopens(fd, dirnow) == FAIL); 10098 10099 /* restore original dir */ 10100 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR) 10101 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL))) 10102 { 10103 if (mch_chdir((char *)dirnow) != 0) 10104 emsg(_(e_prev_dir)); 10105 shorten_fnames(TRUE); 10106 } 10107 vim_free(dirnow); 10108 } 10109 } 10110 else 10111 { 10112 failed |= (put_view(fd, curwin, !using_vdir, flagp, 10113 -1) == FAIL); 10114 } 10115 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save") 10116 == FAIL) 10117 failed = TRUE; 10118 # ifdef FEAT_SEARCH_EXTRA 10119 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL) 10120 failed = TRUE; 10121 # endif 10122 if (put_line(fd, "doautoall SessionLoadPost") == FAIL) 10123 failed = TRUE; 10124 if (eap->cmdidx == CMD_mksession) 10125 { 10126 if (put_line(fd, "unlet SessionLoad") == FAIL) 10127 failed = TRUE; 10128 } 10129 } 10130 #endif 10131 if (put_line(fd, "\" vim: set ft=vim :") == FAIL) 10132 failed = TRUE; 10133 10134 failed |= fclose(fd); 10135 10136 if (failed) 10137 emsg(_(e_write)); 10138 #if defined(FEAT_EVAL) && defined(FEAT_SESSION) 10139 else if (eap->cmdidx == CMD_mksession) 10140 { 10141 /* successful session write - set this_session var */ 10142 char_u *tbuf; 10143 10144 tbuf = alloc(MAXPATHL); 10145 if (tbuf != NULL) 10146 { 10147 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK) 10148 set_vim_var_string(VV_THIS_SESSION, tbuf, -1); 10149 vim_free(tbuf); 10150 } 10151 } 10152 #endif 10153 #ifdef MKSESSION_NL 10154 mksession_nl = FALSE; 10155 #endif 10156 } 10157 10158 #ifdef FEAT_BROWSE 10159 theend: 10160 vim_free(browseFile); 10161 #endif 10162 #ifdef FEAT_SESSION 10163 vim_free(viewFile); 10164 #endif 10165 } 10166 10167 #if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \ 10168 || defined(PROTO) 10169 int 10170 vim_mkdir_emsg(char_u *name, int prot) 10171 { 10172 if (vim_mkdir(name, prot) != 0) 10173 { 10174 semsg(_("E739: Cannot create directory: %s"), name); 10175 return FAIL; 10176 } 10177 return OK; 10178 } 10179 #endif 10180 10181 /* 10182 * Open a file for writing for an Ex command, with some checks. 10183 * Return file descriptor, or NULL on failure. 10184 */ 10185 FILE * 10186 open_exfile( 10187 char_u *fname, 10188 int forceit, 10189 char *mode) /* "w" for create new file or "a" for append */ 10190 { 10191 FILE *fd; 10192 10193 #ifdef UNIX 10194 /* with Unix it is possible to open a directory */ 10195 if (mch_isdir(fname)) 10196 { 10197 semsg(_(e_isadir2), fname); 10198 return NULL; 10199 } 10200 #endif 10201 if (!forceit && *mode != 'a' && vim_fexists(fname)) 10202 { 10203 semsg(_("E189: \"%s\" exists (add ! to override)"), fname); 10204 return NULL; 10205 } 10206 10207 if ((fd = mch_fopen((char *)fname, mode)) == NULL) 10208 semsg(_("E190: Cannot open \"%s\" for writing"), fname); 10209 10210 return fd; 10211 } 10212 10213 /* 10214 * ":mark" and ":k". 10215 */ 10216 static void 10217 ex_mark(exarg_T *eap) 10218 { 10219 pos_T pos; 10220 10221 if (*eap->arg == NUL) /* No argument? */ 10222 emsg(_(e_argreq)); 10223 else if (eap->arg[1] != NUL) /* more than one character? */ 10224 emsg(_(e_trailing)); 10225 else 10226 { 10227 pos = curwin->w_cursor; /* save curwin->w_cursor */ 10228 curwin->w_cursor.lnum = eap->line2; 10229 beginline(BL_WHITE | BL_FIX); 10230 if (setmark(*eap->arg) == FAIL) /* set mark */ 10231 emsg(_("E191: Argument must be a letter or forward/backward quote")); 10232 curwin->w_cursor = pos; /* restore curwin->w_cursor */ 10233 } 10234 } 10235 10236 /* 10237 * Update w_topline, w_leftcol and the cursor position. 10238 */ 10239 void 10240 update_topline_cursor(void) 10241 { 10242 check_cursor(); /* put cursor on valid line */ 10243 update_topline(); 10244 if (!curwin->w_p_wrap) 10245 validate_cursor(); 10246 update_curswant(); 10247 } 10248 10249 /* 10250 * Save the current State and go to Normal mode. 10251 * Return TRUE if the typeahead could be saved. 10252 */ 10253 int 10254 save_current_state(save_state_T *sst) 10255 { 10256 sst->save_msg_scroll = msg_scroll; 10257 sst->save_restart_edit = restart_edit; 10258 sst->save_msg_didout = msg_didout; 10259 sst->save_State = State; 10260 sst->save_insertmode = p_im; 10261 sst->save_finish_op = finish_op; 10262 sst->save_opcount = opcount; 10263 sst->save_reg_executing = reg_executing; 10264 10265 msg_scroll = FALSE; /* no msg scrolling in Normal mode */ 10266 restart_edit = 0; /* don't go to Insert mode */ 10267 p_im = FALSE; /* don't use 'insertmode' */ 10268 10269 /* 10270 * Save the current typeahead. This is required to allow using ":normal" 10271 * from an event handler and makes sure we don't hang when the argument 10272 * ends with half a command. 10273 */ 10274 save_typeahead(&sst->tabuf); 10275 return sst->tabuf.typebuf_valid; 10276 } 10277 10278 void 10279 restore_current_state(save_state_T *sst) 10280 { 10281 /* Restore the previous typeahead. */ 10282 restore_typeahead(&sst->tabuf); 10283 10284 msg_scroll = sst->save_msg_scroll; 10285 restart_edit = sst->save_restart_edit; 10286 p_im = sst->save_insertmode; 10287 finish_op = sst->save_finish_op; 10288 opcount = sst->save_opcount; 10289 reg_executing = sst->save_reg_executing; 10290 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */ 10291 10292 /* Restore the state (needed when called from a function executed for 10293 * 'indentexpr'). Update the mouse and cursor, they may have changed. */ 10294 State = sst->save_State; 10295 #ifdef CURSOR_SHAPE 10296 ui_cursor_shape(); /* may show different cursor shape */ 10297 #endif 10298 } 10299 10300 /* 10301 * ":normal[!] {commands}": Execute normal mode commands. 10302 */ 10303 void 10304 ex_normal(exarg_T *eap) 10305 { 10306 save_state_T save_state; 10307 char_u *arg = NULL; 10308 int l; 10309 char_u *p; 10310 10311 if (ex_normal_lock > 0) 10312 { 10313 emsg(_(e_secure)); 10314 return; 10315 } 10316 if (ex_normal_busy >= p_mmd) 10317 { 10318 emsg(_("E192: Recursive use of :normal too deep")); 10319 return; 10320 } 10321 10322 /* 10323 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do 10324 * this for the K_SPECIAL leading byte, otherwise special keys will not 10325 * work. 10326 */ 10327 if (has_mbyte) 10328 { 10329 int len = 0; 10330 10331 /* Count the number of characters to be escaped. */ 10332 for (p = eap->arg; *p != NUL; ++p) 10333 { 10334 #ifdef FEAT_GUI 10335 if (*p == CSI) /* leadbyte CSI */ 10336 len += 2; 10337 #endif 10338 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l) 10339 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */ 10340 #ifdef FEAT_GUI 10341 || *p == CSI 10342 #endif 10343 ) 10344 len += 2; 10345 } 10346 if (len > 0) 10347 { 10348 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1)); 10349 if (arg != NULL) 10350 { 10351 len = 0; 10352 for (p = eap->arg; *p != NUL; ++p) 10353 { 10354 arg[len++] = *p; 10355 #ifdef FEAT_GUI 10356 if (*p == CSI) 10357 { 10358 arg[len++] = KS_EXTRA; 10359 arg[len++] = (int)KE_CSI; 10360 } 10361 #endif 10362 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l) 10363 { 10364 arg[len++] = *++p; 10365 if (*p == K_SPECIAL) 10366 { 10367 arg[len++] = KS_SPECIAL; 10368 arg[len++] = KE_FILLER; 10369 } 10370 #ifdef FEAT_GUI 10371 else if (*p == CSI) 10372 { 10373 arg[len++] = KS_EXTRA; 10374 arg[len++] = (int)KE_CSI; 10375 } 10376 #endif 10377 } 10378 arg[len] = NUL; 10379 } 10380 } 10381 } 10382 } 10383 10384 ++ex_normal_busy; 10385 if (save_current_state(&save_state)) 10386 { 10387 /* 10388 * Repeat the :normal command for each line in the range. When no 10389 * range given, execute it just once, without positioning the cursor 10390 * first. 10391 */ 10392 do 10393 { 10394 if (eap->addr_count != 0) 10395 { 10396 curwin->w_cursor.lnum = eap->line1++; 10397 curwin->w_cursor.col = 0; 10398 check_cursor_moved(curwin); 10399 } 10400 10401 exec_normal_cmd(arg != NULL 10402 ? arg 10403 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE); 10404 } 10405 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int); 10406 } 10407 10408 /* Might not return to the main loop when in an event handler. */ 10409 update_topline_cursor(); 10410 10411 restore_current_state(&save_state); 10412 --ex_normal_busy; 10413 #ifdef FEAT_MOUSE 10414 setmouse(); 10415 #endif 10416 #ifdef CURSOR_SHAPE 10417 ui_cursor_shape(); /* may show different cursor shape */ 10418 #endif 10419 10420 vim_free(arg); 10421 } 10422 10423 /* 10424 * ":startinsert", ":startreplace" and ":startgreplace" 10425 */ 10426 static void 10427 ex_startinsert(exarg_T *eap) 10428 { 10429 if (eap->forceit) 10430 { 10431 /* cursor line can be zero on startup */ 10432 if (!curwin->w_cursor.lnum) 10433 curwin->w_cursor.lnum = 1; 10434 coladvance((colnr_T)MAXCOL); 10435 curwin->w_curswant = MAXCOL; 10436 curwin->w_set_curswant = FALSE; 10437 } 10438 10439 /* Ignore the command when already in Insert mode. Inserting an 10440 * expression register that invokes a function can do this. */ 10441 if (State & INSERT) 10442 return; 10443 10444 if (eap->cmdidx == CMD_startinsert) 10445 restart_edit = 'a'; 10446 else if (eap->cmdidx == CMD_startreplace) 10447 restart_edit = 'R'; 10448 else 10449 restart_edit = 'V'; 10450 10451 if (!eap->forceit) 10452 { 10453 if (eap->cmdidx == CMD_startinsert) 10454 restart_edit = 'i'; 10455 curwin->w_curswant = 0; /* avoid MAXCOL */ 10456 } 10457 } 10458 10459 /* 10460 * ":stopinsert" 10461 */ 10462 static void 10463 ex_stopinsert(exarg_T *eap UNUSED) 10464 { 10465 restart_edit = 0; 10466 stop_insert_mode = TRUE; 10467 clearmode(); 10468 } 10469 10470 /* 10471 * Execute normal mode command "cmd". 10472 * "remap" can be REMAP_NONE or REMAP_YES. 10473 */ 10474 void 10475 exec_normal_cmd(char_u *cmd, int remap, int silent) 10476 { 10477 /* Stuff the argument into the typeahead buffer. */ 10478 ins_typebuf(cmd, remap, 0, TRUE, silent); 10479 exec_normal(FALSE, FALSE, FALSE); 10480 } 10481 10482 /* 10483 * Execute normal_cmd() until there is no typeahead left. 10484 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars. 10485 */ 10486 void 10487 exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED) 10488 { 10489 oparg_T oa; 10490 10491 clear_oparg(&oa); 10492 finish_op = FALSE; 10493 while ((!stuff_empty() 10494 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0) 10495 || (use_vpeekc && vpeekc() != NUL)) 10496 && !got_int) 10497 { 10498 update_topline_cursor(); 10499 #ifdef FEAT_TERMINAL 10500 if (may_use_terminal_loop && term_use_loop() 10501 && oa.op_type == OP_NOP && oa.regname == NUL 10502 && !VIsual_active) 10503 { 10504 /* If terminal_loop() returns OK we got a key that is handled 10505 * in Normal model. With FAIL we first need to position the 10506 * cursor and the screen needs to be redrawn. */ 10507 if (terminal_loop(TRUE) == OK) 10508 normal_cmd(&oa, TRUE); 10509 } 10510 else 10511 #endif 10512 /* execute a Normal mode cmd */ 10513 normal_cmd(&oa, TRUE); 10514 } 10515 } 10516 10517 #ifdef FEAT_FIND_ID 10518 static void 10519 ex_checkpath(exarg_T *eap) 10520 { 10521 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L, 10522 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW, 10523 (linenr_T)1, (linenr_T)MAXLNUM); 10524 } 10525 10526 #if defined(FEAT_QUICKFIX) 10527 /* 10528 * ":psearch" 10529 */ 10530 static void 10531 ex_psearch(exarg_T *eap) 10532 { 10533 g_do_tagpreview = p_pvh; 10534 ex_findpat(eap); 10535 g_do_tagpreview = 0; 10536 } 10537 #endif 10538 10539 static void 10540 ex_findpat(exarg_T *eap) 10541 { 10542 int whole = TRUE; 10543 long n; 10544 char_u *p; 10545 int action; 10546 10547 switch (cmdnames[eap->cmdidx].cmd_name[2]) 10548 { 10549 case 'e': /* ":psearch", ":isearch" and ":dsearch" */ 10550 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p') 10551 action = ACTION_GOTO; 10552 else 10553 action = ACTION_SHOW; 10554 break; 10555 case 'i': /* ":ilist" and ":dlist" */ 10556 action = ACTION_SHOW_ALL; 10557 break; 10558 case 'u': /* ":ijump" and ":djump" */ 10559 action = ACTION_GOTO; 10560 break; 10561 default: /* ":isplit" and ":dsplit" */ 10562 action = ACTION_SPLIT; 10563 break; 10564 } 10565 10566 n = 1; 10567 if (vim_isdigit(*eap->arg)) /* get count */ 10568 { 10569 n = getdigits(&eap->arg); 10570 eap->arg = skipwhite(eap->arg); 10571 } 10572 if (*eap->arg == '/') /* Match regexp, not just whole words */ 10573 { 10574 whole = FALSE; 10575 ++eap->arg; 10576 p = skip_regexp(eap->arg, '/', p_magic, NULL); 10577 if (*p) 10578 { 10579 *p++ = NUL; 10580 p = skipwhite(p); 10581 10582 /* Check for trailing illegal characters */ 10583 if (!ends_excmd(*p)) 10584 eap->errmsg = e_trailing; 10585 else 10586 eap->nextcmd = check_nextcmd(p); 10587 } 10588 } 10589 if (!eap->skip) 10590 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg), 10591 whole, !eap->forceit, 10592 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY, 10593 n, action, eap->line1, eap->line2); 10594 } 10595 #endif 10596 10597 10598 #ifdef FEAT_QUICKFIX 10599 /* 10600 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc. 10601 */ 10602 static void 10603 ex_ptag(exarg_T *eap) 10604 { 10605 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */ 10606 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1); 10607 } 10608 10609 /* 10610 * ":pedit" 10611 */ 10612 static void 10613 ex_pedit(exarg_T *eap) 10614 { 10615 win_T *curwin_save = curwin; 10616 10617 g_do_tagpreview = p_pvh; 10618 prepare_tagpreview(TRUE); 10619 keep_help_flag = bt_help(curwin_save->w_buffer); 10620 do_exedit(eap, NULL); 10621 keep_help_flag = FALSE; 10622 if (curwin != curwin_save && win_valid(curwin_save)) 10623 { 10624 /* Return cursor to where we were */ 10625 validate_cursor(); 10626 redraw_later(VALID); 10627 win_enter(curwin_save, TRUE); 10628 } 10629 g_do_tagpreview = 0; 10630 } 10631 #endif 10632 10633 /* 10634 * ":stag", ":stselect" and ":stjump". 10635 */ 10636 static void 10637 ex_stag(exarg_T *eap) 10638 { 10639 postponed_split = -1; 10640 postponed_split_flags = cmdmod.split; 10641 postponed_split_tab = cmdmod.tab; 10642 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1); 10643 postponed_split_flags = 0; 10644 postponed_split_tab = 0; 10645 } 10646 10647 /* 10648 * ":tag", ":tselect", ":tjump", ":tnext", etc. 10649 */ 10650 static void 10651 ex_tag(exarg_T *eap) 10652 { 10653 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name); 10654 } 10655 10656 static void 10657 ex_tag_cmd(exarg_T *eap, char_u *name) 10658 { 10659 int cmd; 10660 10661 switch (name[1]) 10662 { 10663 case 'j': cmd = DT_JUMP; /* ":tjump" */ 10664 break; 10665 case 's': cmd = DT_SELECT; /* ":tselect" */ 10666 break; 10667 case 'p': cmd = DT_PREV; /* ":tprevious" */ 10668 break; 10669 case 'N': cmd = DT_PREV; /* ":tNext" */ 10670 break; 10671 case 'n': cmd = DT_NEXT; /* ":tnext" */ 10672 break; 10673 case 'o': cmd = DT_POP; /* ":pop" */ 10674 break; 10675 case 'f': /* ":tfirst" */ 10676 case 'r': cmd = DT_FIRST; /* ":trewind" */ 10677 break; 10678 case 'l': cmd = DT_LAST; /* ":tlast" */ 10679 break; 10680 default: /* ":tag" */ 10681 #ifdef FEAT_CSCOPE 10682 if (p_cst && *eap->arg != NUL) 10683 { 10684 ex_cstag(eap); 10685 return; 10686 } 10687 #endif 10688 cmd = DT_TAG; 10689 break; 10690 } 10691 10692 if (name[0] == 'l') 10693 { 10694 #ifndef FEAT_QUICKFIX 10695 ex_ni(eap); 10696 return; 10697 #else 10698 cmd = DT_LTAG; 10699 #endif 10700 } 10701 10702 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1, 10703 eap->forceit, TRUE); 10704 } 10705 10706 /* 10707 * Check "str" for starting with a special cmdline variable. 10708 * If found return one of the SPEC_ values and set "*usedlen" to the length of 10709 * the variable. Otherwise return -1 and "*usedlen" is unchanged. 10710 */ 10711 int 10712 find_cmdline_var(char_u *src, int *usedlen) 10713 { 10714 int len; 10715 int i; 10716 static char *(spec_str[]) = { 10717 "%", 10718 #define SPEC_PERC 0 10719 "#", 10720 #define SPEC_HASH (SPEC_PERC + 1) 10721 "<cword>", /* cursor word */ 10722 #define SPEC_CWORD (SPEC_HASH + 1) 10723 "<cWORD>", /* cursor WORD */ 10724 #define SPEC_CCWORD (SPEC_CWORD + 1) 10725 "<cexpr>", /* expr under cursor */ 10726 #define SPEC_CEXPR (SPEC_CCWORD + 1) 10727 "<cfile>", /* cursor path name */ 10728 #define SPEC_CFILE (SPEC_CEXPR + 1) 10729 "<sfile>", /* ":so" file name */ 10730 #define SPEC_SFILE (SPEC_CFILE + 1) 10731 "<slnum>", /* ":so" file line number */ 10732 #define SPEC_SLNUM (SPEC_SFILE + 1) 10733 "<afile>", /* autocommand file name */ 10734 #define SPEC_AFILE (SPEC_SLNUM + 1) 10735 "<abuf>", /* autocommand buffer number */ 10736 #define SPEC_ABUF (SPEC_AFILE + 1) 10737 "<amatch>", /* autocommand match name */ 10738 #define SPEC_AMATCH (SPEC_ABUF + 1) 10739 "<sflnum>", /* script file line number */ 10740 #define SPEC_SFLNUM (SPEC_AMATCH + 1) 10741 #ifdef FEAT_CLIENTSERVER 10742 "<client>" 10743 # define SPEC_CLIENT (SPEC_SFLNUM + 1) 10744 #endif 10745 }; 10746 10747 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i) 10748 { 10749 len = (int)STRLEN(spec_str[i]); 10750 if (STRNCMP(src, spec_str[i], len) == 0) 10751 { 10752 *usedlen = len; 10753 return i; 10754 } 10755 } 10756 return -1; 10757 } 10758 10759 /* 10760 * Evaluate cmdline variables. 10761 * 10762 * change '%' to curbuf->b_ffname 10763 * '#' to curwin->w_altfile 10764 * '<cword>' to word under the cursor 10765 * '<cWORD>' to WORD under the cursor 10766 * '<cfile>' to path name under the cursor 10767 * '<sfile>' to sourced file name 10768 * '<slnum>' to sourced file line number 10769 * '<afile>' to file name for autocommand 10770 * '<abuf>' to buffer number for autocommand 10771 * '<amatch>' to matching name for autocommand 10772 * 10773 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be 10774 * "" for error without a message) and NULL is returned. 10775 * Returns an allocated string if a valid match was found. 10776 * Returns NULL if no match was found. "usedlen" then still contains the 10777 * number of characters to skip. 10778 */ 10779 char_u * 10780 eval_vars( 10781 char_u *src, /* pointer into commandline */ 10782 char_u *srcstart, /* beginning of valid memory for src */ 10783 int *usedlen, /* characters after src that are used */ 10784 linenr_T *lnump, /* line number for :e command, or NULL */ 10785 char **errormsg, /* pointer to error message */ 10786 int *escaped) /* return value has escaped white space (can 10787 * be NULL) */ 10788 { 10789 int i; 10790 char_u *s; 10791 char_u *result; 10792 char_u *resultbuf = NULL; 10793 int resultlen; 10794 buf_T *buf; 10795 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */ 10796 int spec_idx; 10797 #ifdef FEAT_MODIFY_FNAME 10798 int tilde_file = FALSE; 10799 int skip_mod = FALSE; 10800 #endif 10801 char_u strbuf[30]; 10802 10803 *errormsg = NULL; 10804 if (escaped != NULL) 10805 *escaped = FALSE; 10806 10807 /* 10808 * Check if there is something to do. 10809 */ 10810 spec_idx = find_cmdline_var(src, usedlen); 10811 if (spec_idx < 0) /* no match */ 10812 { 10813 *usedlen = 1; 10814 return NULL; 10815 } 10816 10817 /* 10818 * Skip when preceded with a backslash "\%" and "\#". 10819 * Note: In "\\%" the % is also not recognized! 10820 */ 10821 if (src > srcstart && src[-1] == '\\') 10822 { 10823 *usedlen = 0; 10824 STRMOVE(src - 1, src); /* remove backslash */ 10825 return NULL; 10826 } 10827 10828 /* 10829 * word or WORD under cursor 10830 */ 10831 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD 10832 || spec_idx == SPEC_CEXPR) 10833 { 10834 resultlen = find_ident_under_cursor(&result, 10835 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING) 10836 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL) 10837 : FIND_STRING); 10838 if (resultlen == 0) 10839 { 10840 *errormsg = ""; 10841 return NULL; 10842 } 10843 } 10844 10845 /* 10846 * '#': Alternate file name 10847 * '%': Current file name 10848 * File name under the cursor 10849 * File name for autocommand 10850 * and following modifiers 10851 */ 10852 else 10853 { 10854 switch (spec_idx) 10855 { 10856 case SPEC_PERC: /* '%': current file */ 10857 if (curbuf->b_fname == NULL) 10858 { 10859 result = (char_u *)""; 10860 valid = 0; /* Must have ":p:h" to be valid */ 10861 } 10862 else 10863 { 10864 result = curbuf->b_fname; 10865 #ifdef FEAT_MODIFY_FNAME 10866 tilde_file = STRCMP(result, "~") == 0; 10867 #endif 10868 } 10869 break; 10870 10871 case SPEC_HASH: /* '#' or "#99": alternate file */ 10872 if (src[1] == '#') /* "##": the argument list */ 10873 { 10874 result = arg_all(); 10875 resultbuf = result; 10876 *usedlen = 2; 10877 if (escaped != NULL) 10878 *escaped = TRUE; 10879 #ifdef FEAT_MODIFY_FNAME 10880 skip_mod = TRUE; 10881 #endif 10882 break; 10883 } 10884 s = src + 1; 10885 if (*s == '<') /* "#<99" uses v:oldfiles */ 10886 ++s; 10887 i = (int)getdigits(&s); 10888 if (s == src + 2 && src[1] == '-') 10889 /* just a minus sign, don't skip over it */ 10890 s--; 10891 *usedlen = (int)(s - src); /* length of what we expand */ 10892 10893 if (src[1] == '<' && i != 0) 10894 { 10895 if (*usedlen < 2) 10896 { 10897 /* Should we give an error message for #<text? */ 10898 *usedlen = 1; 10899 return NULL; 10900 } 10901 #ifdef FEAT_EVAL 10902 result = list_find_str(get_vim_var_list(VV_OLDFILES), 10903 (long)i); 10904 if (result == NULL) 10905 { 10906 *errormsg = ""; 10907 return NULL; 10908 } 10909 #else 10910 *errormsg = _("E809: #< is not available without the +eval feature"); 10911 return NULL; 10912 #endif 10913 } 10914 else 10915 { 10916 if (i == 0 && src[1] == '<' && *usedlen > 1) 10917 *usedlen = 1; 10918 buf = buflist_findnr(i); 10919 if (buf == NULL) 10920 { 10921 *errormsg = _("E194: No alternate file name to substitute for '#'"); 10922 return NULL; 10923 } 10924 if (lnump != NULL) 10925 *lnump = ECMD_LAST; 10926 if (buf->b_fname == NULL) 10927 { 10928 result = (char_u *)""; 10929 valid = 0; /* Must have ":p:h" to be valid */ 10930 } 10931 else 10932 { 10933 result = buf->b_fname; 10934 #ifdef FEAT_MODIFY_FNAME 10935 tilde_file = STRCMP(result, "~") == 0; 10936 #endif 10937 } 10938 } 10939 break; 10940 10941 #ifdef FEAT_SEARCHPATH 10942 case SPEC_CFILE: /* file name under cursor */ 10943 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL); 10944 if (result == NULL) 10945 { 10946 *errormsg = ""; 10947 return NULL; 10948 } 10949 resultbuf = result; /* remember allocated string */ 10950 break; 10951 #endif 10952 10953 case SPEC_AFILE: /* file name for autocommand */ 10954 result = autocmd_fname; 10955 if (result != NULL && !autocmd_fname_full) 10956 { 10957 /* Still need to turn the fname into a full path. It is 10958 * postponed to avoid a delay when <afile> is not used. */ 10959 autocmd_fname_full = TRUE; 10960 result = FullName_save(autocmd_fname, FALSE); 10961 vim_free(autocmd_fname); 10962 autocmd_fname = result; 10963 } 10964 if (result == NULL) 10965 { 10966 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\""); 10967 return NULL; 10968 } 10969 result = shorten_fname1(result); 10970 break; 10971 10972 case SPEC_ABUF: /* buffer number for autocommand */ 10973 if (autocmd_bufnr <= 0) 10974 { 10975 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\""); 10976 return NULL; 10977 } 10978 sprintf((char *)strbuf, "%d", autocmd_bufnr); 10979 result = strbuf; 10980 break; 10981 10982 case SPEC_AMATCH: /* match name for autocommand */ 10983 result = autocmd_match; 10984 if (result == NULL) 10985 { 10986 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\""); 10987 return NULL; 10988 } 10989 break; 10990 10991 case SPEC_SFILE: /* file name for ":so" command */ 10992 result = sourcing_name; 10993 if (result == NULL) 10994 { 10995 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\""); 10996 return NULL; 10997 } 10998 break; 10999 11000 case SPEC_SLNUM: /* line in file for ":so" command */ 11001 if (sourcing_name == NULL || sourcing_lnum == 0) 11002 { 11003 *errormsg = _("E842: no line number to use for \"<slnum>\""); 11004 return NULL; 11005 } 11006 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum); 11007 result = strbuf; 11008 break; 11009 11010 #ifdef FEAT_EVAL 11011 case SPEC_SFLNUM: /* line in script file */ 11012 if (current_sctx.sc_lnum + sourcing_lnum == 0) 11013 { 11014 *errormsg = _("E961: no line number to use for \"<sflnum>\""); 11015 return NULL; 11016 } 11017 sprintf((char *)strbuf, "%ld", 11018 (long)(current_sctx.sc_lnum + sourcing_lnum)); 11019 result = strbuf; 11020 break; 11021 #endif 11022 11023 #ifdef FEAT_CLIENTSERVER 11024 case SPEC_CLIENT: /* Source of last submitted input */ 11025 sprintf((char *)strbuf, PRINTF_HEX_LONG_U, 11026 (long_u)clientWindow); 11027 result = strbuf; 11028 break; 11029 #endif 11030 11031 default: 11032 result = (char_u *)""; /* avoid gcc warning */ 11033 break; 11034 } 11035 11036 resultlen = (int)STRLEN(result); /* length of new string */ 11037 if (src[*usedlen] == '<') /* remove the file name extension */ 11038 { 11039 ++*usedlen; 11040 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result)) 11041 resultlen = (int)(s - result); 11042 } 11043 #ifdef FEAT_MODIFY_FNAME 11044 else if (!skip_mod) 11045 { 11046 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf, 11047 &resultlen); 11048 if (result == NULL) 11049 { 11050 *errormsg = ""; 11051 return NULL; 11052 } 11053 } 11054 #endif 11055 } 11056 11057 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH) 11058 { 11059 if (valid != VALID_HEAD + VALID_PATH) 11060 /* xgettext:no-c-format */ 11061 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\""); 11062 else 11063 *errormsg = _("E500: Evaluates to an empty string"); 11064 result = NULL; 11065 } 11066 else 11067 result = vim_strnsave(result, resultlen); 11068 vim_free(resultbuf); 11069 return result; 11070 } 11071 11072 /* 11073 * Concatenate all files in the argument list, separated by spaces, and return 11074 * it in one allocated string. 11075 * Spaces and backslashes in the file names are escaped with a backslash. 11076 * Returns NULL when out of memory. 11077 */ 11078 static char_u * 11079 arg_all(void) 11080 { 11081 int len; 11082 int idx; 11083 char_u *retval = NULL; 11084 char_u *p; 11085 11086 /* 11087 * Do this loop two times: 11088 * first time: compute the total length 11089 * second time: concatenate the names 11090 */ 11091 for (;;) 11092 { 11093 len = 0; 11094 for (idx = 0; idx < ARGCOUNT; ++idx) 11095 { 11096 p = alist_name(&ARGLIST[idx]); 11097 if (p != NULL) 11098 { 11099 if (len > 0) 11100 { 11101 /* insert a space in between names */ 11102 if (retval != NULL) 11103 retval[len] = ' '; 11104 ++len; 11105 } 11106 for ( ; *p != NUL; ++p) 11107 { 11108 if (*p == ' ' 11109 #ifndef BACKSLASH_IN_FILENAME 11110 || *p == '\\' 11111 #endif 11112 || *p == '`') 11113 { 11114 /* insert a backslash */ 11115 if (retval != NULL) 11116 retval[len] = '\\'; 11117 ++len; 11118 } 11119 if (retval != NULL) 11120 retval[len] = *p; 11121 ++len; 11122 } 11123 } 11124 } 11125 11126 /* second time: break here */ 11127 if (retval != NULL) 11128 { 11129 retval[len] = NUL; 11130 break; 11131 } 11132 11133 /* allocate memory */ 11134 retval = alloc((unsigned)len + 1); 11135 if (retval == NULL) 11136 break; 11137 } 11138 11139 return retval; 11140 } 11141 11142 /* 11143 * Expand the <sfile> string in "arg". 11144 * 11145 * Returns an allocated string, or NULL for any error. 11146 */ 11147 char_u * 11148 expand_sfile(char_u *arg) 11149 { 11150 char *errormsg; 11151 int len; 11152 char_u *result; 11153 char_u *newres; 11154 char_u *repl; 11155 int srclen; 11156 char_u *p; 11157 11158 result = vim_strsave(arg); 11159 if (result == NULL) 11160 return NULL; 11161 11162 for (p = result; *p; ) 11163 { 11164 if (STRNCMP(p, "<sfile>", 7) != 0) 11165 ++p; 11166 else 11167 { 11168 /* replace "<sfile>" with the sourced file name, and do ":" stuff */ 11169 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL); 11170 if (errormsg != NULL) 11171 { 11172 if (*errormsg) 11173 emsg(errormsg); 11174 vim_free(result); 11175 return NULL; 11176 } 11177 if (repl == NULL) /* no match (cannot happen) */ 11178 { 11179 p += srclen; 11180 continue; 11181 } 11182 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1; 11183 newres = alloc(len); 11184 if (newres == NULL) 11185 { 11186 vim_free(repl); 11187 vim_free(result); 11188 return NULL; 11189 } 11190 mch_memmove(newres, result, (size_t)(p - result)); 11191 STRCPY(newres + (p - result), repl); 11192 len = (int)STRLEN(newres); 11193 STRCAT(newres, p + srclen); 11194 vim_free(repl); 11195 vim_free(result); 11196 result = newres; 11197 p = newres + len; /* continue after the match */ 11198 } 11199 } 11200 11201 return result; 11202 } 11203 11204 #ifdef FEAT_SESSION 11205 static int ses_winsizes(FILE *fd, int restore_size, 11206 win_T *tab_firstwin); 11207 static int ses_win_rec(FILE *fd, frame_T *fr); 11208 static frame_T *ses_skipframe(frame_T *fr); 11209 static int ses_do_frame(frame_T *fr); 11210 static int ses_do_win(win_T *wp); 11211 static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp); 11212 static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp); 11213 static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol); 11214 11215 /* 11216 * Write openfile commands for the current buffers to an .exrc file. 11217 * Return FAIL on error, OK otherwise. 11218 */ 11219 static int 11220 makeopens( 11221 FILE *fd, 11222 char_u *dirnow) /* Current directory name */ 11223 { 11224 buf_T *buf; 11225 int only_save_windows = TRUE; 11226 int nr; 11227 int restore_size = TRUE; 11228 win_T *wp; 11229 char_u *sname; 11230 win_T *edited_win = NULL; 11231 int tabnr; 11232 int restore_stal = FALSE; 11233 win_T *tab_firstwin; 11234 frame_T *tab_topframe; 11235 int cur_arg_idx = 0; 11236 int next_arg_idx = 0; 11237 11238 if (ssop_flags & SSOP_BUFFERS) 11239 only_save_windows = FALSE; /* Save ALL buffers */ 11240 11241 /* 11242 * Begin by setting the this_session variable, and then other 11243 * sessionable variables. 11244 */ 11245 #ifdef FEAT_EVAL 11246 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL) 11247 return FAIL; 11248 if (ssop_flags & SSOP_GLOBALS) 11249 if (store_session_globals(fd) == FAIL) 11250 return FAIL; 11251 #endif 11252 11253 /* 11254 * Close all windows and tabs but one. 11255 */ 11256 if (put_line(fd, "silent only") == FAIL) 11257 return FAIL; 11258 if ((ssop_flags & SSOP_TABPAGES) 11259 && put_line(fd, "silent tabonly") == FAIL) 11260 return FAIL; 11261 11262 /* 11263 * Now a :cd command to the session directory or the current directory 11264 */ 11265 if (ssop_flags & SSOP_SESDIR) 11266 { 11267 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')") 11268 == FAIL) 11269 return FAIL; 11270 } 11271 else if (ssop_flags & SSOP_CURDIR) 11272 { 11273 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow); 11274 if (sname == NULL 11275 || fputs("cd ", fd) < 0 11276 || ses_put_fname(fd, sname, &ssop_flags) == FAIL 11277 || put_eol(fd) == FAIL) 11278 { 11279 vim_free(sname); 11280 return FAIL; 11281 } 11282 vim_free(sname); 11283 } 11284 11285 /* 11286 * If there is an empty, unnamed buffer we will wipe it out later. 11287 * Remember the buffer number. 11288 */ 11289 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL) 11290 return FAIL; 11291 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL) 11292 return FAIL; 11293 if (put_line(fd, "endif") == FAIL) 11294 return FAIL; 11295 11296 /* 11297 * Now save the current files, current buffer first. 11298 */ 11299 if (put_line(fd, "set shortmess=aoO") == FAIL) 11300 return FAIL; 11301 11302 /* the global argument list */ 11303 if (ses_arglist(fd, "argglobal", &global_alist.al_ga, 11304 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL) 11305 return FAIL; 11306 11307 if (ssop_flags & SSOP_RESIZE) 11308 { 11309 /* Note: after the restore we still check it worked!*/ 11310 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0 11311 || put_eol(fd) == FAIL) 11312 return FAIL; 11313 } 11314 11315 #ifdef FEAT_GUI 11316 if (gui.in_use && (ssop_flags & SSOP_WINPOS)) 11317 { 11318 int x, y; 11319 11320 if (gui_mch_get_winpos(&x, &y) == OK) 11321 { 11322 /* Note: after the restore we still check it worked!*/ 11323 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL) 11324 return FAIL; 11325 } 11326 } 11327 #endif 11328 11329 /* 11330 * When there are two or more tabpages and 'showtabline' is 1 the tabline 11331 * will be displayed when creating the next tab. That resizes the windows 11332 * in the first tab, which may cause problems. Set 'showtabline' to 2 11333 * temporarily to avoid that. 11334 */ 11335 if (p_stal == 1 && first_tabpage->tp_next != NULL) 11336 { 11337 if (put_line(fd, "set stal=2") == FAIL) 11338 return FAIL; 11339 restore_stal = TRUE; 11340 } 11341 11342 /* 11343 * May repeat putting Windows for each tab, when "tabpages" is in 11344 * 'sessionoptions'. 11345 * Don't use goto_tabpage(), it may change directory and trigger 11346 * autocommands. 11347 */ 11348 tab_firstwin = firstwin; /* first window in tab page "tabnr" */ 11349 tab_topframe = topframe; 11350 if ((ssop_flags & SSOP_TABPAGES)) 11351 { 11352 tabpage_T *tp; 11353 11354 // Similar to ses_win_rec() below, populate the tab pages first so 11355 // later local options won't be copied to the new tabs. 11356 FOR_ALL_TABPAGES(tp) 11357 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL) 11358 return FAIL; 11359 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL) 11360 return FAIL; 11361 } 11362 for (tabnr = 1; ; ++tabnr) 11363 { 11364 int need_tabnext = FALSE; 11365 int cnr = 1; 11366 11367 if ((ssop_flags & SSOP_TABPAGES)) 11368 { 11369 tabpage_T *tp = find_tabpage(tabnr); 11370 11371 if (tp == NULL) 11372 break; /* done all tab pages */ 11373 if (tp == curtab) 11374 { 11375 tab_firstwin = firstwin; 11376 tab_topframe = topframe; 11377 } 11378 else 11379 { 11380 tab_firstwin = tp->tp_firstwin; 11381 tab_topframe = tp->tp_topframe; 11382 } 11383 if (tabnr > 1) 11384 need_tabnext = TRUE; 11385 } 11386 11387 /* 11388 * Before creating the window layout, try loading one file. If this 11389 * is aborted we don't end up with a number of useless windows. 11390 * This may have side effects! (e.g., compressed or network file). 11391 */ 11392 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next) 11393 { 11394 if (ses_do_win(wp) 11395 && wp->w_buffer->b_ffname != NULL 11396 && !bt_help(wp->w_buffer) 11397 #ifdef FEAT_QUICKFIX 11398 && !bt_nofile(wp->w_buffer) 11399 #endif 11400 ) 11401 { 11402 if (need_tabnext && put_line(fd, "tabnext") == FAIL) 11403 return FAIL; 11404 need_tabnext = FALSE; 11405 11406 if (fputs("edit ", fd) < 0 11407 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE) 11408 == FAIL) 11409 return FAIL; 11410 if (!wp->w_arg_idx_invalid) 11411 edited_win = wp; 11412 break; 11413 } 11414 } 11415 11416 /* If no file got edited create an empty tab page. */ 11417 if (need_tabnext && put_line(fd, "tabnext") == FAIL) 11418 return FAIL; 11419 11420 /* 11421 * Save current window layout. 11422 */ 11423 if (put_line(fd, "set splitbelow splitright") == FAIL) 11424 return FAIL; 11425 if (ses_win_rec(fd, tab_topframe) == FAIL) 11426 return FAIL; 11427 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL) 11428 return FAIL; 11429 if (!p_spr && put_line(fd, "set nosplitright") == FAIL) 11430 return FAIL; 11431 11432 /* 11433 * Check if window sizes can be restored (no windows omitted). 11434 * Remember the window number of the current window after restoring. 11435 */ 11436 nr = 0; 11437 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp)) 11438 { 11439 if (ses_do_win(wp)) 11440 ++nr; 11441 else 11442 restore_size = FALSE; 11443 if (curwin == wp) 11444 cnr = nr; 11445 } 11446 11447 /* Go to the first window. */ 11448 if (put_line(fd, "wincmd t") == FAIL) 11449 return FAIL; 11450 11451 /* 11452 * If more than one window, see if sizes can be restored. 11453 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being 11454 * resized when moving between windows. 11455 * Do this before restoring the view, so that the topline and the 11456 * cursor can be set. This is done again below. 11457 * winminheight and winminwidth need to be set to avoid an error if the 11458 * user has set winheight or winwidth. 11459 */ 11460 if (put_line(fd, "set winminheight=0") == FAIL 11461 || put_line(fd, "set winheight=1") == FAIL 11462 || put_line(fd, "set winminwidth=0") == FAIL 11463 || put_line(fd, "set winwidth=1") == FAIL) 11464 return FAIL; 11465 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL) 11466 return FAIL; 11467 11468 /* 11469 * Restore the view of the window (options, file, cursor, etc.). 11470 */ 11471 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next) 11472 { 11473 if (!ses_do_win(wp)) 11474 continue; 11475 if (put_view(fd, wp, wp != edited_win, &ssop_flags, 11476 cur_arg_idx) == FAIL) 11477 return FAIL; 11478 if (nr > 1 && put_line(fd, "wincmd w") == FAIL) 11479 return FAIL; 11480 next_arg_idx = wp->w_arg_idx; 11481 } 11482 11483 /* The argument index in the first tab page is zero, need to set it in 11484 * each window. For further tab pages it's the window where we do 11485 * "tabedit". */ 11486 cur_arg_idx = next_arg_idx; 11487 11488 /* 11489 * Restore cursor to the current window if it's not the first one. 11490 */ 11491 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 11492 || put_eol(fd) == FAIL)) 11493 return FAIL; 11494 11495 /* 11496 * Restore window sizes again after jumping around in windows, because 11497 * the current window has a minimum size while others may not. 11498 */ 11499 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL) 11500 return FAIL; 11501 11502 /* Don't continue in another tab page when doing only the current one 11503 * or when at the last tab page. */ 11504 if (!(ssop_flags & SSOP_TABPAGES)) 11505 break; 11506 } 11507 11508 if (ssop_flags & SSOP_TABPAGES) 11509 { 11510 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0 11511 || put_eol(fd) == FAIL) 11512 return FAIL; 11513 } 11514 if (restore_stal && put_line(fd, "set stal=1") == FAIL) 11515 return FAIL; 11516 11517 // Now put the remaining buffers into the buffer list. 11518 // This is near the end, so that when 'hidden' is set we don't create extra 11519 // buffers. If the buffer was already created with another command the 11520 // ":badd" will have no effect. 11521 FOR_ALL_BUFFERS(buf) 11522 { 11523 if (!(only_save_windows && buf->b_nwindows == 0) 11524 && !(buf->b_help && !(ssop_flags & SSOP_HELP)) 11525 #ifdef FEAT_TERMINAL 11526 // Skip terminal buffers: finished ones are not useful, others 11527 // will be resurrected and result in a new buffer. 11528 && !bt_terminal(buf) 11529 #endif 11530 && buf->b_fname != NULL 11531 && buf->b_p_bl) 11532 { 11533 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L 11534 : buf->b_wininfo->wi_fpos.lnum) < 0 11535 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL) 11536 return FAIL; 11537 } 11538 } 11539 11540 /* 11541 * Wipe out an empty unnamed buffer we started in. 11542 */ 11543 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0") 11544 == FAIL) 11545 return FAIL; 11546 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL) 11547 return FAIL; 11548 if (put_line(fd, "endif") == FAIL) 11549 return FAIL; 11550 if (put_line(fd, "unlet! s:wipebuf") == FAIL) 11551 return FAIL; 11552 11553 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */ 11554 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s", 11555 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL) 11556 return FAIL; 11557 /* Re-apply 'winminheight' and 'winminwidth'. */ 11558 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld", 11559 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL) 11560 return FAIL; 11561 11562 /* 11563 * Lastly, execute the x.vim file if it exists. 11564 */ 11565 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL 11566 || put_line(fd, "if file_readable(s:sx)") == FAIL 11567 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL 11568 || put_line(fd, "endif") == FAIL) 11569 return FAIL; 11570 11571 return OK; 11572 } 11573 11574 static int 11575 ses_winsizes( 11576 FILE *fd, 11577 int restore_size, 11578 win_T *tab_firstwin) 11579 { 11580 int n = 0; 11581 win_T *wp; 11582 11583 if (restore_size && (ssop_flags & SSOP_WINSIZE)) 11584 { 11585 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next) 11586 { 11587 if (!ses_do_win(wp)) 11588 continue; 11589 ++n; 11590 11591 /* restore height when not full height */ 11592 if (wp->w_height + wp->w_status_height < topframe->fr_height 11593 && (fprintf(fd, 11594 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)", 11595 n, (long)wp->w_height, Rows / 2, Rows) < 0 11596 || put_eol(fd) == FAIL)) 11597 return FAIL; 11598 11599 /* restore width when not full width */ 11600 if (wp->w_width < Columns && (fprintf(fd, 11601 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)", 11602 n, (long)wp->w_width, Columns / 2, Columns) < 0 11603 || put_eol(fd) == FAIL)) 11604 return FAIL; 11605 } 11606 } 11607 else 11608 { 11609 /* Just equalise window sizes */ 11610 if (put_line(fd, "wincmd =") == FAIL) 11611 return FAIL; 11612 } 11613 return OK; 11614 } 11615 11616 /* 11617 * Write commands to "fd" to recursively create windows for frame "fr", 11618 * horizontally and vertically split. 11619 * After the commands the last window in the frame is the current window. 11620 * Returns FAIL when writing the commands to "fd" fails. 11621 */ 11622 static int 11623 ses_win_rec(FILE *fd, frame_T *fr) 11624 { 11625 frame_T *frc; 11626 int count = 0; 11627 11628 if (fr->fr_layout != FR_LEAF) 11629 { 11630 /* Find first frame that's not skipped and then create a window for 11631 * each following one (first frame is already there). */ 11632 frc = ses_skipframe(fr->fr_child); 11633 if (frc != NULL) 11634 while ((frc = ses_skipframe(frc->fr_next)) != NULL) 11635 { 11636 /* Make window as big as possible so that we have lots of room 11637 * to split. */ 11638 if (put_line(fd, "wincmd _ | wincmd |") == FAIL 11639 || put_line(fd, fr->fr_layout == FR_COL 11640 ? "split" : "vsplit") == FAIL) 11641 return FAIL; 11642 ++count; 11643 } 11644 11645 /* Go back to the first window. */ 11646 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL 11647 ? "%dwincmd k" : "%dwincmd h", count) < 0 11648 || put_eol(fd) == FAIL)) 11649 return FAIL; 11650 11651 /* Recursively create frames/windows in each window of this column or 11652 * row. */ 11653 frc = ses_skipframe(fr->fr_child); 11654 while (frc != NULL) 11655 { 11656 ses_win_rec(fd, frc); 11657 frc = ses_skipframe(frc->fr_next); 11658 /* Go to next window. */ 11659 if (frc != NULL && put_line(fd, "wincmd w") == FAIL) 11660 return FAIL; 11661 } 11662 } 11663 return OK; 11664 } 11665 11666 /* 11667 * Skip frames that don't contain windows we want to save in the Session. 11668 * Returns NULL when there none. 11669 */ 11670 static frame_T * 11671 ses_skipframe(frame_T *fr) 11672 { 11673 frame_T *frc; 11674 11675 FOR_ALL_FRAMES(frc, fr) 11676 if (ses_do_frame(frc)) 11677 break; 11678 return frc; 11679 } 11680 11681 /* 11682 * Return TRUE if frame "fr" has a window somewhere that we want to save in 11683 * the Session. 11684 */ 11685 static int 11686 ses_do_frame(frame_T *fr) 11687 { 11688 frame_T *frc; 11689 11690 if (fr->fr_layout == FR_LEAF) 11691 return ses_do_win(fr->fr_win); 11692 FOR_ALL_FRAMES(frc, fr->fr_child) 11693 if (ses_do_frame(frc)) 11694 return TRUE; 11695 return FALSE; 11696 } 11697 11698 /* 11699 * Return non-zero if window "wp" is to be stored in the Session. 11700 */ 11701 static int 11702 ses_do_win(win_T *wp) 11703 { 11704 #ifdef FEAT_TERMINAL 11705 if (bt_terminal(wp->w_buffer)) 11706 return !term_is_finished(wp->w_buffer) 11707 && (ssop_flags & SSOP_TERMINAL) 11708 && term_should_restore(wp->w_buffer); 11709 #endif 11710 if (wp->w_buffer->b_fname == NULL 11711 #ifdef FEAT_QUICKFIX 11712 /* When 'buftype' is "nofile" can't restore the window contents. */ 11713 || bt_nofile(wp->w_buffer) 11714 #endif 11715 ) 11716 return (ssop_flags & SSOP_BLANK); 11717 if (bt_help(wp->w_buffer)) 11718 return (ssop_flags & SSOP_HELP); 11719 return TRUE; 11720 } 11721 11722 static int 11723 put_view_curpos(FILE *fd, win_T *wp, char *spaces) 11724 { 11725 int r; 11726 11727 if (wp->w_curswant == MAXCOL) 11728 r = fprintf(fd, "%snormal! $", spaces); 11729 else 11730 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1); 11731 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK; 11732 } 11733 11734 /* 11735 * Write commands to "fd" to restore the view of a window. 11736 * Caller must make sure 'scrolloff' is zero. 11737 */ 11738 static int 11739 put_view( 11740 FILE *fd, 11741 win_T *wp, 11742 int add_edit, /* add ":edit" command to view */ 11743 unsigned *flagp, /* vop_flags or ssop_flags */ 11744 int current_arg_idx) /* current argument index of the window, use 11745 * -1 if unknown */ 11746 { 11747 win_T *save_curwin; 11748 int f; 11749 int do_cursor; 11750 int did_next = FALSE; 11751 11752 /* Always restore cursor position for ":mksession". For ":mkview" only 11753 * when 'viewoptions' contains "cursor". */ 11754 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR); 11755 11756 /* 11757 * Local argument list. 11758 */ 11759 if (wp->w_alist == &global_alist) 11760 { 11761 if (put_line(fd, "argglobal") == FAIL) 11762 return FAIL; 11763 } 11764 else 11765 { 11766 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga, 11767 flagp == &vop_flags 11768 || !(*flagp & SSOP_CURDIR) 11769 || wp->w_localdir != NULL, flagp) == FAIL) 11770 return FAIL; 11771 } 11772 11773 /* Only when part of a session: restore the argument index. Some 11774 * arguments may have been deleted, check if the index is valid. */ 11775 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp) 11776 && flagp == &ssop_flags) 11777 { 11778 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0 11779 || put_eol(fd) == FAIL) 11780 return FAIL; 11781 did_next = TRUE; 11782 } 11783 11784 /* Edit the file. Skip this when ":next" already did it. */ 11785 if (add_edit && (!did_next || wp->w_arg_idx_invalid)) 11786 { 11787 # ifdef FEAT_TERMINAL 11788 if (bt_terminal(wp->w_buffer)) 11789 { 11790 if (term_write_session(fd, wp) == FAIL) 11791 return FAIL; 11792 } 11793 else 11794 # endif 11795 /* 11796 * Load the file. 11797 */ 11798 if (wp->w_buffer->b_ffname != NULL 11799 # ifdef FEAT_QUICKFIX 11800 && !bt_nofile(wp->w_buffer) 11801 # endif 11802 ) 11803 { 11804 /* 11805 * Editing a file in this buffer: use ":edit file". 11806 * This may have side effects! (e.g., compressed or network file). 11807 * 11808 * Note, if a buffer for that file already exists, use :badd to 11809 * edit that buffer, to not lose folding information (:edit resets 11810 * folds in other buffers) 11811 */ 11812 if (fputs("if bufexists(\"", fd) < 0 11813 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL 11814 || fputs("\") | buffer ", fd) < 0 11815 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL 11816 || fputs(" | else | edit ", fd) < 0 11817 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL 11818 || fputs(" | endif", fd) < 0 11819 || put_eol(fd) == FAIL) 11820 return FAIL; 11821 } 11822 else 11823 { 11824 /* No file in this buffer, just make it empty. */ 11825 if (put_line(fd, "enew") == FAIL) 11826 return FAIL; 11827 #ifdef FEAT_QUICKFIX 11828 if (wp->w_buffer->b_ffname != NULL) 11829 { 11830 /* The buffer does have a name, but it's not a file name. */ 11831 if (fputs("file ", fd) < 0 11832 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL) 11833 return FAIL; 11834 } 11835 #endif 11836 do_cursor = FALSE; 11837 } 11838 } 11839 11840 /* 11841 * Local mappings and abbreviations. 11842 */ 11843 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS)) 11844 && makemap(fd, wp->w_buffer) == FAIL) 11845 return FAIL; 11846 11847 /* 11848 * Local options. Need to go to the window temporarily. 11849 * Store only local values when using ":mkview" and when ":mksession" is 11850 * used and 'sessionoptions' doesn't include "options". 11851 * Some folding options are always stored when "folds" is included, 11852 * otherwise the folds would not be restored correctly. 11853 */ 11854 save_curwin = curwin; 11855 curwin = wp; 11856 curbuf = curwin->w_buffer; 11857 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS)) 11858 f = makeset(fd, OPT_LOCAL, 11859 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS)); 11860 #ifdef FEAT_FOLDING 11861 else if (*flagp & SSOP_FOLDS) 11862 f = makefoldset(fd); 11863 #endif 11864 else 11865 f = OK; 11866 curwin = save_curwin; 11867 curbuf = curwin->w_buffer; 11868 if (f == FAIL) 11869 return FAIL; 11870 11871 #ifdef FEAT_FOLDING 11872 /* 11873 * Save Folds when 'buftype' is empty and for help files. 11874 */ 11875 if ((*flagp & SSOP_FOLDS) 11876 && wp->w_buffer->b_ffname != NULL 11877 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer))) 11878 { 11879 if (put_folds(fd, wp) == FAIL) 11880 return FAIL; 11881 } 11882 #endif 11883 11884 /* 11885 * Set the cursor after creating folds, since that moves the cursor. 11886 */ 11887 if (do_cursor) 11888 { 11889 11890 /* Restore the cursor line in the file and relatively in the 11891 * window. Don't use "G", it changes the jumplist. */ 11892 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)", 11893 (long)wp->w_cursor.lnum, 11894 (long)(wp->w_cursor.lnum - wp->w_topline), 11895 (long)wp->w_height / 2, (long)wp->w_height) < 0 11896 || put_eol(fd) == FAIL 11897 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL 11898 || put_line(fd, "exe s:l") == FAIL 11899 || put_line(fd, "normal! zt") == FAIL 11900 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0 11901 || put_eol(fd) == FAIL) 11902 return FAIL; 11903 /* Restore the cursor column and left offset when not wrapping. */ 11904 if (wp->w_cursor.col == 0) 11905 { 11906 if (put_line(fd, "normal! 0") == FAIL) 11907 return FAIL; 11908 } 11909 else 11910 { 11911 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0) 11912 { 11913 if (fprintf(fd, 11914 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)", 11915 (long)wp->w_virtcol + 1, 11916 (long)(wp->w_virtcol - wp->w_leftcol), 11917 (long)wp->w_width / 2, (long)wp->w_width) < 0 11918 || put_eol(fd) == FAIL 11919 || put_line(fd, "if s:c > 0") == FAIL 11920 || fprintf(fd, 11921 " exe 'normal! ' . s:c . '|zs' . %ld . '|'", 11922 (long)wp->w_virtcol + 1) < 0 11923 || put_eol(fd) == FAIL 11924 || put_line(fd, "else") == FAIL 11925 || put_view_curpos(fd, wp, " ") == FAIL 11926 || put_line(fd, "endif") == FAIL) 11927 return FAIL; 11928 } 11929 else if (put_view_curpos(fd, wp, "") == FAIL) 11930 return FAIL; 11931 } 11932 } 11933 11934 /* 11935 * Local directory, if the current flag is not view options or the "curdir" 11936 * option is included. 11937 */ 11938 if (wp->w_localdir != NULL 11939 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR))) 11940 { 11941 if (fputs("lcd ", fd) < 0 11942 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL 11943 || put_eol(fd) == FAIL) 11944 return FAIL; 11945 did_lcd = TRUE; 11946 } 11947 11948 return OK; 11949 } 11950 11951 /* 11952 * Write an argument list to the session file. 11953 * Returns FAIL if writing fails. 11954 */ 11955 static int 11956 ses_arglist( 11957 FILE *fd, 11958 char *cmd, 11959 garray_T *gap, 11960 int fullname, /* TRUE: use full path name */ 11961 unsigned *flagp) 11962 { 11963 int i; 11964 char_u *buf = NULL; 11965 char_u *s; 11966 11967 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL) 11968 return FAIL; 11969 if (put_line(fd, "%argdel") == FAIL) 11970 return FAIL; 11971 for (i = 0; i < gap->ga_len; ++i) 11972 { 11973 /* NULL file names are skipped (only happens when out of memory). */ 11974 s = alist_name(&((aentry_T *)gap->ga_data)[i]); 11975 if (s != NULL) 11976 { 11977 if (fullname) 11978 { 11979 buf = alloc(MAXPATHL); 11980 if (buf != NULL) 11981 { 11982 (void)vim_FullName(s, buf, MAXPATHL, FALSE); 11983 s = buf; 11984 } 11985 } 11986 if (fputs("$argadd ", fd) < 0 11987 || ses_put_fname(fd, s, flagp) == FAIL 11988 || put_eol(fd) == FAIL) 11989 { 11990 vim_free(buf); 11991 return FAIL; 11992 } 11993 vim_free(buf); 11994 } 11995 } 11996 return OK; 11997 } 11998 11999 /* 12000 * Write a buffer name to the session file. 12001 * Also ends the line, if "add_eol" is TRUE. 12002 * Returns FAIL if writing fails. 12003 */ 12004 static int 12005 ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol) 12006 { 12007 char_u *name; 12008 12009 /* Use the short file name if the current directory is known at the time 12010 * the session file will be sourced. 12011 * Don't do this for ":mkview", we don't know the current directory. 12012 * Don't do this after ":lcd", we don't keep track of what the current 12013 * directory is. */ 12014 if (buf->b_sfname != NULL 12015 && flagp == &ssop_flags 12016 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR)) 12017 #ifdef FEAT_AUTOCHDIR 12018 && !p_acd 12019 #endif 12020 && !did_lcd) 12021 name = buf->b_sfname; 12022 else 12023 name = buf->b_ffname; 12024 if (ses_put_fname(fd, name, flagp) == FAIL 12025 || (add_eol && put_eol(fd) == FAIL)) 12026 return FAIL; 12027 return OK; 12028 } 12029 12030 /* 12031 * Write a file name to the session file. 12032 * Takes care of the "slash" option in 'sessionoptions' and escapes special 12033 * characters. 12034 * Returns FAIL if writing fails or out of memory. 12035 */ 12036 static int 12037 ses_put_fname(FILE *fd, char_u *name, unsigned *flagp) 12038 { 12039 char_u *sname; 12040 char_u *p; 12041 int retval = OK; 12042 12043 sname = home_replace_save(NULL, name); 12044 if (sname == NULL) 12045 return FAIL; 12046 12047 if (*flagp & SSOP_SLASH) 12048 { 12049 /* change all backslashes to forward slashes */ 12050 for (p = sname; *p != NUL; MB_PTR_ADV(p)) 12051 if (*p == '\\') 12052 *p = '/'; 12053 } 12054 12055 /* escape special characters */ 12056 p = vim_strsave_fnameescape(sname, FALSE); 12057 vim_free(sname); 12058 if (p == NULL) 12059 return FAIL; 12060 12061 /* write the result */ 12062 if (fputs((char *)p, fd) < 0) 12063 retval = FAIL; 12064 12065 vim_free(p); 12066 return retval; 12067 } 12068 12069 /* 12070 * ":loadview [nr]" 12071 */ 12072 static void 12073 ex_loadview(exarg_T *eap) 12074 { 12075 char_u *fname; 12076 12077 fname = get_view_file(*eap->arg); 12078 if (fname != NULL) 12079 { 12080 do_source(fname, FALSE, DOSO_NONE); 12081 vim_free(fname); 12082 } 12083 } 12084 12085 /* 12086 * Get the name of the view file for the current buffer. 12087 */ 12088 static char_u * 12089 get_view_file(int c) 12090 { 12091 int len = 0; 12092 char_u *p, *s; 12093 char_u *retval; 12094 char_u *sname; 12095 12096 if (curbuf->b_ffname == NULL) 12097 { 12098 emsg(_(e_noname)); 12099 return NULL; 12100 } 12101 sname = home_replace_save(NULL, curbuf->b_ffname); 12102 if (sname == NULL) 12103 return NULL; 12104 12105 /* 12106 * We want a file name without separators, because we're not going to make 12107 * a directory. 12108 * "normal" path separator -> "=+" 12109 * "=" -> "==" 12110 * ":" path separator -> "=-" 12111 */ 12112 for (p = sname; *p; ++p) 12113 if (*p == '=' || vim_ispathsep(*p)) 12114 ++len; 12115 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9)); 12116 if (retval != NULL) 12117 { 12118 STRCPY(retval, p_vdir); 12119 add_pathsep(retval); 12120 s = retval + STRLEN(retval); 12121 for (p = sname; *p; ++p) 12122 { 12123 if (*p == '=') 12124 { 12125 *s++ = '='; 12126 *s++ = '='; 12127 } 12128 else if (vim_ispathsep(*p)) 12129 { 12130 *s++ = '='; 12131 #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS) 12132 if (*p == ':') 12133 *s++ = '-'; 12134 else 12135 #endif 12136 *s++ = '+'; 12137 } 12138 else 12139 *s++ = *p; 12140 } 12141 *s++ = '='; 12142 *s++ = c; 12143 STRCPY(s, ".vim"); 12144 } 12145 12146 vim_free(sname); 12147 return retval; 12148 } 12149 12150 #endif /* FEAT_SESSION */ 12151 12152 /* 12153 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession". 12154 * Return FAIL for a write error. 12155 */ 12156 int 12157 put_eol(FILE *fd) 12158 { 12159 if ( 12160 #ifdef USE_CRNL 12161 ( 12162 # ifdef MKSESSION_NL 12163 !mksession_nl && 12164 # endif 12165 (putc('\r', fd) < 0)) || 12166 #endif 12167 (putc('\n', fd) < 0)) 12168 return FAIL; 12169 return OK; 12170 } 12171 12172 /* 12173 * Write a line to "fd". 12174 * Return FAIL for a write error. 12175 */ 12176 int 12177 put_line(FILE *fd, char *s) 12178 { 12179 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL) 12180 return FAIL; 12181 return OK; 12182 } 12183 12184 #ifdef FEAT_VIMINFO 12185 /* 12186 * ":rviminfo" and ":wviminfo". 12187 */ 12188 static void 12189 ex_viminfo( 12190 exarg_T *eap) 12191 { 12192 char_u *save_viminfo; 12193 12194 save_viminfo = p_viminfo; 12195 if (*p_viminfo == NUL) 12196 p_viminfo = (char_u *)"'100"; 12197 if (eap->cmdidx == CMD_rviminfo) 12198 { 12199 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS 12200 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL) 12201 emsg(_("E195: Cannot open viminfo file for reading")); 12202 } 12203 else 12204 write_viminfo(eap->arg, eap->forceit); 12205 p_viminfo = save_viminfo; 12206 } 12207 #endif 12208 12209 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO) 12210 /* 12211 * Make a dialog message in "buff[DIALOG_MSG_SIZE]". 12212 * "format" must contain "%s". 12213 */ 12214 void 12215 dialog_msg(char_u *buff, char *format, char_u *fname) 12216 { 12217 if (fname == NULL) 12218 fname = (char_u *)_("Untitled"); 12219 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname); 12220 } 12221 #endif 12222 12223 /* 12224 * ":behave {mswin,xterm}" 12225 */ 12226 static void 12227 ex_behave(exarg_T *eap) 12228 { 12229 if (STRCMP(eap->arg, "mswin") == 0) 12230 { 12231 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0); 12232 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0); 12233 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0); 12234 set_option_value((char_u *)"keymodel", 0L, 12235 (char_u *)"startsel,stopsel", 0); 12236 } 12237 else if (STRCMP(eap->arg, "xterm") == 0) 12238 { 12239 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0); 12240 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0); 12241 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0); 12242 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0); 12243 } 12244 else 12245 semsg(_(e_invarg2), eap->arg); 12246 } 12247 12248 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 12249 /* 12250 * Function given to ExpandGeneric() to obtain the possible arguments of the 12251 * ":behave {mswin,xterm}" command. 12252 */ 12253 char_u * 12254 get_behave_arg(expand_T *xp UNUSED, int idx) 12255 { 12256 if (idx == 0) 12257 return (char_u *)"mswin"; 12258 if (idx == 1) 12259 return (char_u *)"xterm"; 12260 return NULL; 12261 } 12262 12263 /* 12264 * Function given to ExpandGeneric() to obtain the possible arguments of the 12265 * ":messages {clear}" command. 12266 */ 12267 char_u * 12268 get_messages_arg(expand_T *xp UNUSED, int idx) 12269 { 12270 if (idx == 0) 12271 return (char_u *)"clear"; 12272 return NULL; 12273 } 12274 #endif 12275 12276 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 12277 char_u * 12278 get_mapclear_arg(expand_T *xp UNUSED, int idx) 12279 { 12280 if (idx == 0) 12281 return (char_u *)"<buffer>"; 12282 return NULL; 12283 } 12284 #endif 12285 12286 static int filetype_detect = FALSE; 12287 static int filetype_plugin = FALSE; 12288 static int filetype_indent = FALSE; 12289 12290 /* 12291 * ":filetype [plugin] [indent] {on,off,detect}" 12292 * on: Load the filetype.vim file to install autocommands for file types. 12293 * off: Load the ftoff.vim file to remove all autocommands for file types. 12294 * plugin on: load filetype.vim and ftplugin.vim 12295 * plugin off: load ftplugof.vim 12296 * indent on: load filetype.vim and indent.vim 12297 * indent off: load indoff.vim 12298 */ 12299 static void 12300 ex_filetype(exarg_T *eap) 12301 { 12302 char_u *arg = eap->arg; 12303 int plugin = FALSE; 12304 int indent = FALSE; 12305 12306 if (*eap->arg == NUL) 12307 { 12308 /* Print current status. */ 12309 smsg("filetype detection:%s plugin:%s indent:%s", 12310 filetype_detect ? "ON" : "OFF", 12311 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF", 12312 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF"); 12313 return; 12314 } 12315 12316 /* Accept "plugin" and "indent" in any order. */ 12317 for (;;) 12318 { 12319 if (STRNCMP(arg, "plugin", 6) == 0) 12320 { 12321 plugin = TRUE; 12322 arg = skipwhite(arg + 6); 12323 continue; 12324 } 12325 if (STRNCMP(arg, "indent", 6) == 0) 12326 { 12327 indent = TRUE; 12328 arg = skipwhite(arg + 6); 12329 continue; 12330 } 12331 break; 12332 } 12333 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0) 12334 { 12335 if (*arg == 'o' || !filetype_detect) 12336 { 12337 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL); 12338 filetype_detect = TRUE; 12339 if (plugin) 12340 { 12341 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL); 12342 filetype_plugin = TRUE; 12343 } 12344 if (indent) 12345 { 12346 source_runtime((char_u *)INDENT_FILE, DIP_ALL); 12347 filetype_indent = TRUE; 12348 } 12349 } 12350 if (*arg == 'd') 12351 { 12352 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL); 12353 do_modelines(0); 12354 } 12355 } 12356 else if (STRCMP(arg, "off") == 0) 12357 { 12358 if (plugin || indent) 12359 { 12360 if (plugin) 12361 { 12362 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL); 12363 filetype_plugin = FALSE; 12364 } 12365 if (indent) 12366 { 12367 source_runtime((char_u *)INDOFF_FILE, DIP_ALL); 12368 filetype_indent = FALSE; 12369 } 12370 } 12371 else 12372 { 12373 source_runtime((char_u *)FTOFF_FILE, DIP_ALL); 12374 filetype_detect = FALSE; 12375 } 12376 } 12377 else 12378 semsg(_(e_invarg2), arg); 12379 } 12380 12381 /* 12382 * ":setfiletype [FALLBACK] {name}" 12383 */ 12384 static void 12385 ex_setfiletype(exarg_T *eap) 12386 { 12387 if (!did_filetype) 12388 { 12389 char_u *arg = eap->arg; 12390 12391 if (STRNCMP(arg, "FALLBACK ", 9) == 0) 12392 arg += 9; 12393 12394 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL); 12395 if (arg != eap->arg) 12396 did_filetype = FALSE; 12397 } 12398 } 12399 12400 static void 12401 ex_digraphs(exarg_T *eap UNUSED) 12402 { 12403 #ifdef FEAT_DIGRAPHS 12404 if (*eap->arg != NUL) 12405 putdigraph(eap->arg); 12406 else 12407 listdigraphs(eap->forceit); 12408 #else 12409 emsg(_("E196: No digraphs in this version")); 12410 #endif 12411 } 12412 12413 static void 12414 ex_set(exarg_T *eap) 12415 { 12416 int flags = 0; 12417 12418 if (eap->cmdidx == CMD_setlocal) 12419 flags = OPT_LOCAL; 12420 else if (eap->cmdidx == CMD_setglobal) 12421 flags = OPT_GLOBAL; 12422 #if defined(FEAT_EVAL) && defined(FEAT_BROWSE) 12423 if (cmdmod.browse && flags == 0) 12424 ex_options(eap); 12425 else 12426 #endif 12427 (void)do_set(eap->arg, flags); 12428 } 12429 12430 #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO) 12431 void 12432 set_no_hlsearch(int flag) 12433 { 12434 no_hlsearch = flag; 12435 # ifdef FEAT_EVAL 12436 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls); 12437 # endif 12438 } 12439 12440 /* 12441 * ":nohlsearch" 12442 */ 12443 static void 12444 ex_nohlsearch(exarg_T *eap UNUSED) 12445 { 12446 set_no_hlsearch(TRUE); 12447 redraw_all_later(SOME_VALID); 12448 } 12449 12450 /* 12451 * ":[N]match {group} {pattern}" 12452 * Sets nextcmd to the start of the next command, if any. Also called when 12453 * skipping commands to find the next command. 12454 */ 12455 static void 12456 ex_match(exarg_T *eap) 12457 { 12458 char_u *p; 12459 char_u *g = NULL; 12460 char_u *end; 12461 int c; 12462 int id; 12463 12464 if (eap->line2 <= 3) 12465 id = eap->line2; 12466 else 12467 { 12468 emsg(_(e_invcmd)); 12469 return; 12470 } 12471 12472 /* First clear any old pattern. */ 12473 if (!eap->skip) 12474 match_delete(curwin, id, FALSE); 12475 12476 if (ends_excmd(*eap->arg)) 12477 end = eap->arg; 12478 else if ((STRNICMP(eap->arg, "none", 4) == 0 12479 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4])))) 12480 end = eap->arg + 4; 12481 else 12482 { 12483 p = skiptowhite(eap->arg); 12484 if (!eap->skip) 12485 g = vim_strnsave(eap->arg, (int)(p - eap->arg)); 12486 p = skipwhite(p); 12487 if (*p == NUL) 12488 { 12489 /* There must be two arguments. */ 12490 vim_free(g); 12491 semsg(_(e_invarg2), eap->arg); 12492 return; 12493 } 12494 end = skip_regexp(p + 1, *p, TRUE, NULL); 12495 if (!eap->skip) 12496 { 12497 if (*end != NUL && !ends_excmd(*skipwhite(end + 1))) 12498 { 12499 vim_free(g); 12500 eap->errmsg = e_trailing; 12501 return; 12502 } 12503 if (*end != *p) 12504 { 12505 vim_free(g); 12506 semsg(_(e_invarg2), p); 12507 return; 12508 } 12509 12510 c = *end; 12511 *end = NUL; 12512 match_add(curwin, g, p + 1, 10, id, NULL, NULL); 12513 vim_free(g); 12514 *end = c; 12515 } 12516 } 12517 eap->nextcmd = find_nextcmd(end); 12518 } 12519 #endif 12520 12521 #ifdef FEAT_CRYPT 12522 /* 12523 * ":X": Get crypt key 12524 */ 12525 static void 12526 ex_X(exarg_T *eap UNUSED) 12527 { 12528 crypt_check_current_method(); 12529 (void)crypt_get_key(TRUE, TRUE); 12530 } 12531 #endif 12532 12533 #ifdef FEAT_FOLDING 12534 static void 12535 ex_fold(exarg_T *eap) 12536 { 12537 if (foldManualAllowed(TRUE)) 12538 foldCreate(eap->line1, eap->line2); 12539 } 12540 12541 static void 12542 ex_foldopen(exarg_T *eap) 12543 { 12544 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen, 12545 eap->forceit, FALSE); 12546 } 12547 12548 static void 12549 ex_folddo(exarg_T *eap) 12550 { 12551 linenr_T lnum; 12552 12553 #ifdef FEAT_CLIPBOARD 12554 start_global_changes(); 12555 #endif 12556 12557 /* First set the marks for all lines closed/open. */ 12558 for (lnum = eap->line1; lnum <= eap->line2; ++lnum) 12559 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed)) 12560 ml_setmarked(lnum); 12561 12562 /* Execute the command on the marked lines. */ 12563 global_exe(eap->arg); 12564 ml_clearmarked(); /* clear rest of the marks */ 12565 #ifdef FEAT_CLIPBOARD 12566 end_global_changes(); 12567 #endif 12568 } 12569 #endif 12570 12571 #ifdef FEAT_QUICKFIX 12572 /* 12573 * Returns TRUE if the supplied Ex cmdidx is for a location list command 12574 * instead of a quickfix command. 12575 */ 12576 int 12577 is_loclist_cmd(int cmdidx) 12578 { 12579 if (cmdidx < 0 || cmdidx >= CMD_SIZE) 12580 return FALSE; 12581 return cmdnames[cmdidx].cmd_name[0] == 'l'; 12582 } 12583 #endif 12584 12585 # if defined(FEAT_TIMERS) || defined(PROTO) 12586 int 12587 get_pressedreturn(void) 12588 { 12589 return ex_pressedreturn; 12590 } 12591 12592 void 12593 set_pressedreturn(int val) 12594 { 12595 ex_pressedreturn = val; 12596 } 12597 #endif 12598