1 /* vi:set ts=8 sts=4 sw=4: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * ex_docmd.c: functions for executing an Ex command line. 12 */ 13 14 #include "vim.h" 15 16 #ifdef HAVE_FCNTL_H 17 # include <fcntl.h> /* for chdir() */ 18 #endif 19 20 static int quitmore = 0; 21 static int ex_pressedreturn = FALSE; 22 #ifndef FEAT_PRINTER 23 # define ex_hardcopy ex_ni 24 #endif 25 26 #ifdef FEAT_USR_CMDS 27 typedef struct ucmd 28 { 29 char_u *uc_name; /* The command name */ 30 long_u uc_argt; /* The argument type */ 31 char_u *uc_rep; /* The command's replacement string */ 32 long uc_def; /* The default value for a range/count */ 33 scid_T uc_scriptID; /* SID where the command was defined */ 34 int uc_compl; /* completion type */ 35 # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 36 char_u *uc_compl_arg; /* completion argument if any */ 37 # endif 38 } ucmd_T; 39 40 #define UC_BUFFER 1 /* -buffer: local to current buffer */ 41 42 garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL}; 43 44 #define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i]) 45 #define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i]) 46 47 static void do_ucmd __ARGS((exarg_T *eap)); 48 static void ex_command __ARGS((exarg_T *eap)); 49 static void ex_comclear __ARGS((exarg_T *eap)); 50 static void ex_delcommand __ARGS((exarg_T *eap)); 51 # ifdef FEAT_CMDL_COMPL 52 static char_u *get_user_command_name __ARGS((int idx)); 53 # endif 54 55 #else 56 # define ex_command ex_ni 57 # define ex_comclear ex_ni 58 # define ex_delcommand ex_ni 59 #endif 60 61 #ifdef FEAT_EVAL 62 static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*getline)(int, void *, int), void *cookie)); 63 #else 64 static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*getline)(int, void *, int), void *cookie)); 65 static int if_level = 0; /* depth in :if */ 66 #endif 67 static int checkforcmd __ARGS((char_u **pp, char *cmd, int len)); 68 static char_u *find_command __ARGS((exarg_T *eap, int *full)); 69 70 static void ex_abbreviate __ARGS((exarg_T *eap)); 71 static void ex_map __ARGS((exarg_T *eap)); 72 static void ex_unmap __ARGS((exarg_T *eap)); 73 static void ex_mapclear __ARGS((exarg_T *eap)); 74 static void ex_abclear __ARGS((exarg_T *eap)); 75 #ifndef FEAT_MENU 76 # define ex_emenu ex_ni 77 # define ex_menu ex_ni 78 # define ex_menutranslate ex_ni 79 #endif 80 #ifdef FEAT_AUTOCMD 81 static void ex_autocmd __ARGS((exarg_T *eap)); 82 static void ex_doautocmd __ARGS((exarg_T *eap)); 83 #else 84 # define ex_autocmd ex_ni 85 # define ex_doautocmd ex_ni 86 # define ex_doautoall ex_ni 87 #endif 88 #ifdef FEAT_LISTCMDS 89 static void ex_bunload __ARGS((exarg_T *eap)); 90 static void ex_buffer __ARGS((exarg_T *eap)); 91 static void ex_bmodified __ARGS((exarg_T *eap)); 92 static void ex_bnext __ARGS((exarg_T *eap)); 93 static void ex_bprevious __ARGS((exarg_T *eap)); 94 static void ex_brewind __ARGS((exarg_T *eap)); 95 static void ex_blast __ARGS((exarg_T *eap)); 96 #else 97 # define ex_bunload ex_ni 98 # define ex_buffer ex_ni 99 # define ex_bmodified ex_ni 100 # define ex_bnext ex_ni 101 # define ex_bprevious ex_ni 102 # define ex_brewind ex_ni 103 # define ex_blast ex_ni 104 # define buflist_list ex_ni 105 # define ex_checktime ex_ni 106 #endif 107 #if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS) 108 # define ex_buffer_all ex_ni 109 #endif 110 static char_u *getargcmd __ARGS((char_u **)); 111 static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs)); 112 static int getargopt __ARGS((exarg_T *eap)); 113 #ifndef FEAT_QUICKFIX 114 # define ex_make ex_ni 115 # define ex_cc ex_ni 116 # define ex_cnext ex_ni 117 # define ex_cfile ex_ni 118 # define qf_list ex_ni 119 # define qf_age ex_ni 120 # define ex_helpgrep ex_ni 121 #endif 122 #if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS) 123 # define ex_cclose ex_ni 124 # define ex_copen ex_ni 125 # define ex_cwindow ex_ni 126 #endif 127 128 static int check_more __ARGS((int, int)); 129 static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file)); 130 #if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \ 131 || !defined(FEAT_RUBY) 132 static void ex_script_ni __ARGS((exarg_T *eap)); 133 #endif 134 static char_u *invalid_range __ARGS((exarg_T *eap)); 135 static void correct_range __ARGS((exarg_T *eap)); 136 static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep)); 137 static void ex_highlight __ARGS((exarg_T *eap)); 138 static void ex_colorscheme __ARGS((exarg_T *eap)); 139 static void ex_quit __ARGS((exarg_T *eap)); 140 static void ex_cquit __ARGS((exarg_T *eap)); 141 static void ex_quit_all __ARGS((exarg_T *eap)); 142 #ifdef FEAT_WINDOWS 143 static void ex_close __ARGS((exarg_T *eap)); 144 static void ex_win_close __ARGS((exarg_T *eap, win_T *win)); 145 static void ex_only __ARGS((exarg_T *eap)); 146 static void ex_all __ARGS((exarg_T *eap)); 147 static void ex_resize __ARGS((exarg_T *eap)); 148 static void ex_stag __ARGS((exarg_T *eap)); 149 #else 150 # define ex_close ex_ni 151 # define ex_only ex_ni 152 # define ex_all ex_ni 153 # define ex_resize ex_ni 154 # define ex_splitview ex_ni 155 # define ex_stag ex_ni 156 #endif 157 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) 158 static void ex_pclose __ARGS((exarg_T *eap)); 159 static void ex_ptag __ARGS((exarg_T *eap)); 160 static void ex_pedit __ARGS((exarg_T *eap)); 161 #else 162 # define ex_pclose ex_ni 163 # define ex_ptag ex_ni 164 # define ex_pedit ex_ni 165 #endif 166 static void ex_hide __ARGS((exarg_T *eap)); 167 static void ex_stop __ARGS((exarg_T *eap)); 168 static void ex_exit __ARGS((exarg_T *eap)); 169 static void ex_print __ARGS((exarg_T *eap)); 170 #ifdef FEAT_BYTEOFF 171 static void ex_goto __ARGS((exarg_T *eap)); 172 #else 173 # define ex_goto ex_ni 174 #endif 175 static void ex_shell __ARGS((exarg_T *eap)); 176 static void ex_preserve __ARGS((exarg_T *eap)); 177 static void ex_recover __ARGS((exarg_T *eap)); 178 #ifndef FEAT_LISTCMDS 179 # define ex_argedit ex_ni 180 # define ex_argadd ex_ni 181 # define ex_argdelete ex_ni 182 # define ex_listdo ex_ni 183 #endif 184 static void ex_mode __ARGS((exarg_T *eap)); 185 static void ex_wrongmodifier __ARGS((exarg_T *eap)); 186 static void ex_find __ARGS((exarg_T *eap)); 187 static void ex_edit __ARGS((exarg_T *eap)); 188 #if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER) 189 # define ex_drop ex_ni 190 #endif 191 #ifndef FEAT_GUI 192 # define ex_gui ex_nogui 193 static void ex_nogui __ARGS((exarg_T *eap)); 194 #endif 195 #if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) 196 static void ex_tearoff __ARGS((exarg_T *eap)); 197 #else 198 # define ex_tearoff ex_ni 199 #endif 200 #if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU) 201 static void ex_popup __ARGS((exarg_T *eap)); 202 #else 203 # define ex_popup ex_ni 204 #endif 205 #ifndef FEAT_GUI_MSWIN 206 # define ex_simalt ex_ni 207 #endif 208 #if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) 209 # define gui_mch_find_dialog ex_ni 210 # define gui_mch_replace_dialog ex_ni 211 #endif 212 #ifndef FEAT_GUI_GTK 213 # define ex_helpfind ex_ni 214 #endif 215 #ifndef FEAT_CSCOPE 216 # define do_cscope ex_ni 217 # define do_scscope ex_ni 218 # define do_cstag ex_ni 219 #endif 220 #ifndef FEAT_SYN_HL 221 # define ex_syntax ex_ni 222 #endif 223 #ifndef FEAT_PERL 224 # define ex_perl ex_script_ni 225 # define ex_perldo ex_ni 226 #endif 227 #ifndef FEAT_PYTHON 228 # define ex_python ex_script_ni 229 # define ex_pyfile ex_ni 230 #endif 231 #ifndef FEAT_TCL 232 # define ex_tcl ex_script_ni 233 # define ex_tcldo ex_ni 234 # define ex_tclfile ex_ni 235 #endif 236 #ifndef FEAT_RUBY 237 # define ex_ruby ex_script_ni 238 # define ex_rubydo ex_ni 239 # define ex_rubyfile ex_ni 240 #endif 241 #ifndef FEAT_SNIFF 242 # define ex_sniff ex_ni 243 #endif 244 #ifndef FEAT_KEYMAP 245 # define ex_loadkeymap ex_ni 246 #endif 247 static void ex_swapname __ARGS((exarg_T *eap)); 248 static void ex_syncbind __ARGS((exarg_T *eap)); 249 static void ex_read __ARGS((exarg_T *eap)); 250 static void ex_cd __ARGS((exarg_T *eap)); 251 static void ex_pwd __ARGS((exarg_T *eap)); 252 static void ex_equal __ARGS((exarg_T *eap)); 253 static void ex_sleep __ARGS((exarg_T *eap)); 254 static void do_exmap __ARGS((exarg_T *eap, int isabbrev)); 255 static void ex_winsize __ARGS((exarg_T *eap)); 256 #ifdef FEAT_WINDOWS 257 static void ex_wincmd __ARGS((exarg_T *eap)); 258 #else 259 # define ex_wincmd ex_ni 260 #endif 261 #if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) 262 static void ex_winpos __ARGS((exarg_T *eap)); 263 #else 264 # define ex_winpos ex_ni 265 #endif 266 static void ex_operators __ARGS((exarg_T *eap)); 267 static void ex_put __ARGS((exarg_T *eap)); 268 static void ex_copymove __ARGS((exarg_T *eap)); 269 static void ex_submagic __ARGS((exarg_T *eap)); 270 static void ex_join __ARGS((exarg_T *eap)); 271 static void ex_at __ARGS((exarg_T *eap)); 272 static void ex_bang __ARGS((exarg_T *eap)); 273 static void ex_undo __ARGS((exarg_T *eap)); 274 static void ex_redo __ARGS((exarg_T *eap)); 275 static void ex_redir __ARGS((exarg_T *eap)); 276 static void ex_redraw __ARGS((exarg_T *eap)); 277 static void ex_redrawstatus __ARGS((exarg_T *eap)); 278 static void close_redir __ARGS((void)); 279 static void ex_mkrc __ARGS((exarg_T *eap)); 280 static void ex_mark __ARGS((exarg_T *eap)); 281 #ifdef FEAT_USR_CMDS 282 static char_u *uc_fun_cmd __ARGS((void)); 283 #endif 284 #ifdef FEAT_EX_EXTRA 285 static void ex_normal __ARGS((exarg_T *eap)); 286 static void ex_startinsert __ARGS((exarg_T *eap)); 287 static void ex_stopinsert __ARGS((exarg_T *eap)); 288 #else 289 # define ex_normal ex_ni 290 # define ex_align ex_ni 291 # define ex_retab ex_ni 292 # define ex_startinsert ex_ni 293 # define ex_stopinsert ex_ni 294 # define ex_helptags ex_ni 295 #endif 296 #ifdef FEAT_FIND_ID 297 static void ex_checkpath __ARGS((exarg_T *eap)); 298 static void ex_findpat __ARGS((exarg_T *eap)); 299 #else 300 # define ex_findpat ex_ni 301 # define ex_checkpath ex_ni 302 #endif 303 #if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) 304 static void ex_psearch __ARGS((exarg_T *eap)); 305 #else 306 # define ex_psearch ex_ni 307 #endif 308 static void ex_tag __ARGS((exarg_T *eap)); 309 static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name)); 310 #ifndef FEAT_EVAL 311 # define ex_scriptnames ex_ni 312 # define ex_finish ex_ni 313 # define ex_echo ex_ni 314 # define ex_echohl ex_ni 315 # define ex_execute ex_ni 316 # define ex_call ex_ni 317 # define ex_if ex_ni 318 # define ex_endif ex_ni 319 # define ex_else ex_ni 320 # define ex_while ex_ni 321 # define ex_continue ex_ni 322 # define ex_break ex_ni 323 # define ex_endwhile ex_ni 324 # define ex_throw ex_ni 325 # define ex_try ex_ni 326 # define ex_catch ex_ni 327 # define ex_finally ex_ni 328 # define ex_endtry ex_ni 329 # define ex_endfunction ex_ni 330 # define ex_let ex_ni 331 # define ex_unlet ex_ni 332 # define ex_function ex_ni 333 # define ex_delfunction ex_ni 334 # define ex_return ex_ni 335 #endif 336 static char_u *arg_all __ARGS((void)); 337 #ifdef FEAT_SESSION 338 static int makeopens __ARGS((FILE *fd, char_u *dirnow)); 339 static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp)); 340 static void ex_loadview __ARGS((exarg_T *eap)); 341 static char_u *get_view_file __ARGS((int c)); 342 #else 343 # define ex_loadview ex_ni 344 #endif 345 #ifndef FEAT_EVAL 346 # define ex_compiler ex_ni 347 #endif 348 #ifdef FEAT_VIMINFO 349 static void ex_viminfo __ARGS((exarg_T *eap)); 350 #else 351 # define ex_viminfo ex_ni 352 #endif 353 static void ex_behave __ARGS((exarg_T *eap)); 354 #ifdef FEAT_AUTOCMD 355 static void ex_filetype __ARGS((exarg_T *eap)); 356 static void ex_setfiletype __ARGS((exarg_T *eap)); 357 #else 358 # define ex_filetype ex_ni 359 # define ex_setfiletype ex_ni 360 #endif 361 #ifndef FEAT_DIFF 362 # define ex_diffpatch ex_ni 363 # define ex_diffgetput ex_ni 364 # define ex_diffsplit ex_ni 365 # define ex_diffthis ex_ni 366 # define ex_diffupdate ex_ni 367 #endif 368 static void ex_digraphs __ARGS((exarg_T *eap)); 369 static void ex_set __ARGS((exarg_T *eap)); 370 #if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD) 371 # define ex_options ex_ni 372 #endif 373 #ifdef FEAT_SEARCH_EXTRA 374 static void ex_nohlsearch __ARGS((exarg_T *eap)); 375 static void ex_match __ARGS((exarg_T *eap)); 376 #else 377 # define ex_nohlsearch ex_ni 378 # define ex_match ex_ni 379 #endif 380 #ifdef FEAT_CRYPT 381 static void ex_X __ARGS((exarg_T *eap)); 382 #else 383 # define ex_X ex_ni 384 #endif 385 #ifdef FEAT_FOLDING 386 static void ex_fold __ARGS((exarg_T *eap)); 387 static void ex_foldopen __ARGS((exarg_T *eap)); 388 static void ex_folddo __ARGS((exarg_T *eap)); 389 #else 390 # define ex_fold ex_ni 391 # define ex_foldopen ex_ni 392 # define ex_folddo ex_ni 393 #endif 394 #if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ 395 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))) 396 # define ex_language ex_ni 397 #endif 398 #ifndef FEAT_SIGNS 399 # define ex_sign ex_ni 400 #endif 401 #ifndef FEAT_SUN_WORKSHOP 402 # define ex_wsverb ex_ni 403 #endif 404 405 #ifndef FEAT_EVAL 406 # define ex_debug ex_ni 407 # define ex_breakadd ex_ni 408 # define ex_debuggreedy ex_ni 409 # define ex_breakdel ex_ni 410 # define ex_breaklist ex_ni 411 #endif 412 413 #ifndef FEAT_CMDHIST 414 # define ex_history ex_ni 415 #endif 416 #ifndef FEAT_JUMPLIST 417 # define ex_jumps ex_ni 418 # define ex_changes ex_ni 419 #endif 420 421 /* 422 * Declare cmdnames[]. 423 */ 424 #define DO_DECLARE_EXCMD 425 #include "ex_cmds.h" 426 427 /* 428 * Table used to quickly search for a command, based on its first character. 429 */ 430 cmdidx_T cmdidxs[27] = 431 { 432 CMD_append, 433 CMD_buffer, 434 CMD_change, 435 CMD_delete, 436 CMD_edit, 437 CMD_file, 438 CMD_global, 439 CMD_help, 440 CMD_insert, 441 CMD_join, 442 CMD_k, 443 CMD_list, 444 CMD_move, 445 CMD_next, 446 CMD_open, 447 CMD_print, 448 CMD_quit, 449 CMD_read, 450 CMD_substitute, 451 CMD_t, 452 CMD_undo, 453 CMD_vglobal, 454 CMD_write, 455 CMD_xit, 456 CMD_yank, 457 CMD_z, 458 CMD_bang 459 }; 460 461 static char_u dollar_command[2] = {'$', 0}; 462 463 464 #ifdef FEAT_EVAL 465 /* Struct for storing a line inside a while loop */ 466 typedef struct 467 { 468 char_u *line; /* command line */ 469 linenr_T lnum; /* sourcing_lnum of the line */ 470 } wcmd_T; 471 472 /* 473 * Structure used to store info for line position in a while loop. 474 * This is required, because do_one_cmd() may invoke ex_function(), which 475 * reads more lines that may come from the while loop. 476 */ 477 struct while_cookie 478 { 479 garray_T *lines_gap; /* growarray with line info */ 480 int current_line; /* last read line from growarray */ 481 int repeating; /* TRUE when looping a second time */ 482 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */ 483 char_u *(*getline) __ARGS((int, void *, int)); 484 void *cookie; 485 }; 486 487 static char_u *get_while_line __ARGS((int c, void *cookie, int indent)); 488 static int store_while_line __ARGS((garray_T *gap, char_u *line)); 489 static void free_cmdlines __ARGS((garray_T *gap)); 490 491 /* Struct to save a few things while debugging. Used in do_cmdline() only. */ 492 struct dbg_stuff 493 { 494 int trylevel; 495 int force_abort; 496 except_T *caught_stack; 497 char_u *vv_exception; 498 char_u *vv_throwpoint; 499 int did_emsg; 500 int got_int; 501 int did_throw; 502 int need_rethrow; 503 int check_cstack; 504 except_T *current_exception; 505 }; 506 507 static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp)); 508 static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp)); 509 510 static void 511 save_dbg_stuff(dsp) 512 struct dbg_stuff *dsp; 513 { 514 dsp->trylevel = trylevel; trylevel = 0; 515 dsp->force_abort = force_abort; force_abort = FALSE; 516 dsp->caught_stack = caught_stack; caught_stack = NULL; 517 dsp->vv_exception = v_exception(NULL); 518 dsp->vv_throwpoint = v_throwpoint(NULL); 519 520 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */ 521 dsp->did_emsg = did_emsg; did_emsg = FALSE; 522 dsp->got_int = got_int; got_int = FALSE; 523 dsp->did_throw = did_throw; did_throw = FALSE; 524 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE; 525 dsp->check_cstack = check_cstack; check_cstack = FALSE; 526 dsp->current_exception = current_exception; current_exception = NULL; 527 } 528 529 static void 530 restore_dbg_stuff(dsp) 531 struct dbg_stuff *dsp; 532 { 533 suppress_errthrow = FALSE; 534 trylevel = dsp->trylevel; 535 force_abort = dsp->force_abort; 536 caught_stack = dsp->caught_stack; 537 (void)v_exception(dsp->vv_exception); 538 (void)v_throwpoint(dsp->vv_throwpoint); 539 did_emsg = dsp->did_emsg; 540 got_int = dsp->got_int; 541 did_throw = dsp->did_throw; 542 need_rethrow = dsp->need_rethrow; 543 check_cstack = dsp->check_cstack; 544 current_exception = dsp->current_exception; 545 } 546 #endif 547 548 549 /* 550 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi" 551 * command is given. 552 */ 553 void 554 do_exmode(improved) 555 int improved; /* TRUE for "improved Ex" mode */ 556 { 557 int save_msg_scroll; 558 int prev_msg_row; 559 linenr_T prev_line; 560 561 save_msg_scroll = msg_scroll; 562 ++RedrawingDisabled; /* don't redisplay the window */ 563 ++no_wait_return; /* don't wait for return */ 564 if (improved) 565 exmode_active = EXMODE_VIM; 566 else 567 { 568 settmode(TMODE_COOK); 569 exmode_active = EXMODE_NORMAL; 570 } 571 572 State = NORMAL; 573 #ifdef FEAT_GUI 574 /* Ignore scrollbar and mouse events in Ex mode */ 575 ++hold_gui_events; 576 #endif 577 #ifdef FEAT_SNIFF 578 want_sniff_request = 0; /* No K_SNIFF wanted */ 579 #endif 580 581 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode.")); 582 while (exmode_active) 583 { 584 msg_scroll = TRUE; 585 need_wait_return = FALSE; 586 ex_pressedreturn = FALSE; 587 ex_no_reprint = FALSE; 588 prev_msg_row = msg_row; 589 prev_line = curwin->w_cursor.lnum; 590 #ifdef FEAT_SNIFF 591 ProcessSniffRequests(); 592 #endif 593 if (improved) 594 { 595 cmdline_row = msg_row; 596 do_cmdline(NULL, getexline, NULL, 0); 597 } 598 else 599 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT); 600 lines_left = Rows - 1; 601 602 if (prev_line != curwin->w_cursor.lnum && !ex_no_reprint) 603 { 604 if (ex_pressedreturn) 605 { 606 /* go up one line, to overwrite the ":<CR>" line, so the 607 * output doensn't contain empty lines. */ 608 msg_row = prev_msg_row; 609 if (prev_msg_row == Rows - 1) 610 msg_row--; 611 } 612 msg_col = 0; 613 print_line_no_prefix(curwin->w_cursor.lnum, FALSE); 614 msg_clr_eos(); 615 } 616 else if (ex_pressedreturn) /* must be at EOF */ 617 EMSG(_("E501: At end-of-file")); 618 } 619 620 #ifdef FEAT_GUI 621 --hold_gui_events; 622 #endif 623 if (!improved) 624 settmode(TMODE_RAW); 625 --RedrawingDisabled; 626 --no_wait_return; 627 update_screen(CLEAR); 628 need_wait_return = FALSE; 629 msg_scroll = save_msg_scroll; 630 } 631 632 /* 633 * Execute a simple command line. Used for translated commands like "*". 634 */ 635 int 636 do_cmdline_cmd(cmd) 637 char_u *cmd; 638 { 639 return do_cmdline(cmd, NULL, NULL, 640 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); 641 } 642 643 /* 644 * do_cmdline(): execute one Ex command line 645 * 646 * 1. Execute "cmdline" when it is not NULL. 647 * If "cmdline" is NULL, or more lines are needed, getline() is used. 648 * 2. Split up in parts separated with '|'. 649 * 650 * This function can be called recursively! 651 * 652 * flags: 653 * DOCMD_VERBOSE - The command will be included in the error message. 654 * DOCMD_NOWAIT - Don't call wait_return() and friends. 655 * DOCMD_REPEAT - Repeat execution until getline() returns NULL. 656 * DOCMD_KEYTYPED - Don't reset KeyTyped. 657 * DOCMD_EXCRESET - Reset the exception environment (used for debugging). 658 * DOCMD_KEEPLINE - Store first typed line (for repeating with "."). 659 * 660 * return FAIL if cmdline could not be executed, OK otherwise 661 */ 662 int 663 do_cmdline(cmdline, getline, cookie, flags) 664 char_u *cmdline; 665 char_u *(*getline) __ARGS((int, void *, int)); 666 void *cookie; /* argument for getline() */ 667 int flags; 668 { 669 char_u *next_cmdline; /* next cmd to execute */ 670 char_u *cmdline_copy = NULL; /* copy of cmd line */ 671 int used_getline = FALSE; /* used "getline" to obtain command */ 672 static int recursive = 0; /* recursive depth */ 673 int msg_didout_before_start = 0; 674 int count = 0; /* line number count */ 675 int did_inc = FALSE; /* incremented RedrawingDisabled */ 676 int retval = OK; 677 #ifdef FEAT_EVAL 678 struct condstack cstack; /* conditional stack */ 679 garray_T lines_ga; /* keep lines for ":while" */ 680 int current_line = 0; /* active line in lines_ga */ 681 char_u *fname = NULL; /* function or script name */ 682 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */ 683 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */ 684 struct dbg_stuff debug_saved; /* saved things for debug mode */ 685 int initial_trylevel; 686 struct msglist **saved_msg_list = NULL; 687 struct msglist *private_msg_list; 688 689 /* "getline" and "cookie" passed to do_one_cmd() */ 690 char_u *(*cmd_getline) __ARGS((int, void *, int)); 691 void *cmd_cookie; 692 struct while_cookie cmd_while_cookie; 693 void *real_cookie; 694 #else 695 # define cmd_getline getline 696 # define cmd_cookie cookie 697 #endif 698 static int call_depth = 0; /* recursiveness */ 699 700 #ifdef FEAT_EVAL 701 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory 702 * location for storing error messages to be converted to an exception. 703 * This ensures that the do_errthrow() call in do_one_cmd() does not combine 704 * the messages stored by an earlier invocation of do_one_cmd() with the 705 * command name of the later one. This would happen when BufWritePost 706 * autocommands are executed after a write error. */ 707 saved_msg_list = msg_list; 708 msg_list = &private_msg_list; 709 private_msg_list = NULL; 710 #endif 711 712 /* It's possible to create an endless loop with ":execute", catch that 713 * here. The value of 200 allows nested function calls, ":source", etc. */ 714 if (call_depth == 200) 715 { 716 EMSG(_("E169: Command too recursive")); 717 #ifdef FEAT_EVAL 718 /* When converting to an exception, we do not include the command name 719 * since this is not an error of the specific command. */ 720 do_errthrow((struct condstack *)NULL, (char_u *)NULL); 721 msg_list = saved_msg_list; 722 #endif 723 return FAIL; 724 } 725 ++call_depth; 726 727 #ifdef FEAT_EVAL 728 cstack.cs_idx = -1; 729 cstack.cs_whilelevel = 0; 730 cstack.cs_trylevel = 0; 731 cstack.cs_emsg_silent_list = NULL; 732 cstack.cs_had_while = FALSE; 733 cstack.cs_had_endwhile = FALSE; 734 cstack.cs_had_continue = FALSE; 735 cstack.cs_had_finally = FALSE; 736 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10); 737 738 real_cookie = getline_cookie(getline, cookie); 739 740 /* Inside a function use a higher nesting level. */ 741 if (getline_equal(getline, cookie, get_func_line) 742 && ex_nesting_level == func_level(real_cookie)) 743 ++ex_nesting_level; 744 745 /* Get the function or script name and the address where the next breakpoint 746 * line and the debug tick for a function or script are stored. */ 747 if (getline_equal(getline, cookie, get_func_line)) 748 { 749 fname = func_name(real_cookie); 750 breakpoint = func_breakpoint(real_cookie); 751 dbg_tick = func_dbg_tick(real_cookie); 752 } 753 else if (getline_equal(getline, cookie, getsourceline)) 754 { 755 fname = sourcing_name; 756 breakpoint = source_breakpoint(real_cookie); 757 dbg_tick = source_dbg_tick(real_cookie); 758 } 759 760 /* 761 * Initialize "force_abort" and "suppress_errthrow" at the top level. 762 */ 763 if (!recursive) 764 { 765 force_abort = FALSE; 766 suppress_errthrow = FALSE; 767 } 768 769 /* 770 * If requested, store and reset the global values controlling the 771 * exception handling (used when debugging). 772 */ 773 else if (flags & DOCMD_EXCRESET) 774 save_dbg_stuff(&debug_saved); 775 776 initial_trylevel = trylevel; 777 778 /* 779 * "did_throw" will be set to TRUE when an exception is being thrown. 780 */ 781 did_throw = FALSE; 782 #endif 783 /* 784 * "did_emsg" will be set to TRUE when emsg() is used, in which case we 785 * cancel the whole command line, and any if/endif while/endwhile loop. 786 * If force_abort is set, we cancel everything. 787 */ 788 did_emsg = FALSE; 789 790 /* 791 * KeyTyped is only set when calling vgetc(). Reset it here when not 792 * calling vgetc() (sourced command lines). 793 */ 794 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline)) 795 KeyTyped = FALSE; 796 797 /* 798 * Continue executing command lines: 799 * - when inside an ":if" or ":while" 800 * - for multiple commands on one line, separated with '|' 801 * - when repeating until there are no more lines (for ":source") 802 */ 803 next_cmdline = cmdline; 804 do 805 { 806 /* stop skipping cmds for an error msg after all endifs and endwhiles */ 807 if (next_cmdline == NULL 808 #ifdef FEAT_EVAL 809 && !force_abort 810 && cstack.cs_idx < 0 811 && !(getline_equal(getline, cookie, get_func_line) 812 && func_has_abort(real_cookie)) 813 #endif 814 ) 815 did_emsg = FALSE; 816 817 /* 818 * 1. If repeating a line with ":while", get a line from lines_ga. 819 * 2. If no line given: Get an allocated line with getline(). 820 * 3. If a line is given: Make a copy, so we can mess with it. 821 */ 822 823 #ifdef FEAT_EVAL 824 /* 1. If repeating, get a previous line from lines_ga. */ 825 if (cstack.cs_whilelevel && current_line < lines_ga.ga_len) 826 { 827 /* Each '|' separated command is stored separately in lines_ga, to 828 * be able to jump to it. Don't use next_cmdline now. */ 829 vim_free(cmdline_copy); 830 cmdline_copy = NULL; 831 832 /* Check if a function has returned or, unless it has an unclosed 833 * try conditional, aborted. */ 834 if (getline_equal(getline, cookie, get_func_line) 835 && func_has_ended(real_cookie)) 836 { 837 retval = FAIL; 838 break; 839 } 840 841 /* Check if a sourced file hit a ":finish" command. */ 842 if (source_finished(getline, cookie)) 843 { 844 retval = FAIL; 845 break; 846 } 847 848 /* If breakpoints have been added/deleted need to check for it. */ 849 if (breakpoint != NULL && dbg_tick != NULL 850 && *dbg_tick != debug_tick) 851 { 852 *breakpoint = dbg_find_breakpoint( 853 getline_equal(getline, cookie, getsourceline), 854 fname, sourcing_lnum); 855 *dbg_tick = debug_tick; 856 } 857 858 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line; 859 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum; 860 861 /* Did we encounter a breakpoint? */ 862 if (breakpoint != NULL && *breakpoint != 0 863 && *breakpoint <= sourcing_lnum) 864 { 865 dbg_breakpoint(fname, sourcing_lnum); 866 /* Find next breakpoint. */ 867 *breakpoint = dbg_find_breakpoint( 868 getline_equal(getline, cookie, getsourceline), 869 fname, sourcing_lnum); 870 *dbg_tick = debug_tick; 871 } 872 } 873 874 if (cstack.cs_whilelevel) 875 { 876 /* Inside a while loop we need to store the lines and use them 877 * again. Pass a different "getline" function to do_one_cmd() 878 * below, so that it stores lines in or reads them from 879 * "lines_ga". Makes it possible to define a function inside a 880 * while loop. */ 881 cmd_getline = get_while_line; 882 cmd_cookie = (void *)&cmd_while_cookie; 883 cmd_while_cookie.lines_gap = &lines_ga; 884 cmd_while_cookie.current_line = current_line; 885 cmd_while_cookie.getline = getline; 886 cmd_while_cookie.cookie = cookie; 887 cmd_while_cookie.repeating = (current_line < lines_ga.ga_len); 888 } 889 else 890 { 891 cmd_getline = getline; 892 cmd_cookie = cookie; 893 } 894 #endif 895 896 /* 2. If no line given, get an allocated line with getline(). */ 897 if (next_cmdline == NULL) 898 { 899 /* 900 * Need to set msg_didout for the first line after an ":if", 901 * otherwise the ":if" will be overwritten. 902 */ 903 if (count == 1 && getline_equal(getline, cookie, getexline)) 904 msg_didout = TRUE; 905 if (getline == NULL || (next_cmdline = getline(':', cookie, 906 #ifdef FEAT_EVAL 907 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2 908 #else 909 0 910 #endif 911 )) == NULL) 912 { 913 /* Don't call wait_return for aborted command line. The NULL 914 * returned for the end of a sourced file or executed function 915 * doesn't do this. */ 916 if (KeyTyped && !(flags & DOCMD_REPEAT)) 917 need_wait_return = FALSE; 918 retval = FAIL; 919 break; 920 } 921 used_getline = TRUE; 922 923 /* 924 * Keep the first typed line. Clear it when more lines are typed. 925 */ 926 if (flags & DOCMD_KEEPLINE) 927 { 928 vim_free(repeat_cmdline); 929 if (count == 0) 930 repeat_cmdline = vim_strsave(next_cmdline); 931 else 932 repeat_cmdline = NULL; 933 } 934 } 935 936 /* 3. Make a copy of the command so we can mess with it. */ 937 else if (cmdline_copy == NULL) 938 { 939 next_cmdline = vim_strsave(next_cmdline); 940 if (next_cmdline == NULL) 941 { 942 EMSG(_(e_outofmem)); 943 retval = FAIL; 944 break; 945 } 946 } 947 cmdline_copy = next_cmdline; 948 949 #ifdef FEAT_EVAL 950 /* 951 * Save the current line when inside a ":while", and when the command 952 * looks like a ":while", because we may need it later. 953 * When there is a '|' and another command, it is stored separately, 954 * because we need to be able to jump back to it from an :endwhile. 955 */ 956 if ( current_line == lines_ga.ga_len 957 && (cstack.cs_whilelevel || has_while_cmd(next_cmdline))) 958 { 959 if (store_while_line(&lines_ga, next_cmdline) == FAIL) 960 { 961 retval = FAIL; 962 break; 963 } 964 } 965 did_endif = FALSE; 966 #endif 967 968 if (count++ == 0) 969 { 970 /* 971 * All output from the commands is put below each other, without 972 * waiting for a return. Don't do this when executing commands 973 * from a script or when being called recursive (e.g. for ":e 974 * +command file"). 975 */ 976 if (!(flags & DOCMD_NOWAIT) && !recursive) 977 { 978 msg_didout_before_start = msg_didout; 979 msg_didany = FALSE; /* no output yet */ 980 msg_start(); 981 msg_scroll = TRUE; /* put messages below each other */ 982 ++no_wait_return; /* dont wait for return until finished */ 983 ++RedrawingDisabled; 984 did_inc = TRUE; 985 } 986 } 987 988 if (p_verbose >= 15 && sourcing_name != NULL) 989 { 990 int c = -1; 991 992 ++no_wait_return; 993 msg_scroll = TRUE; /* always scroll up, don't overwrite */ 994 /* Truncate long lines, smsg() can't handle that. */ 995 if (STRLEN(cmdline_copy) > 200) 996 { 997 c = cmdline_copy[200]; 998 cmdline_copy[200] = NUL; 999 } 1000 smsg((char_u *)_("line %ld: %s"), 1001 (long)sourcing_lnum, cmdline_copy); 1002 msg_puts((char_u *)"\n"); /* don't overwrite this either */ 1003 if (c >= 0) 1004 cmdline_copy[200] = c; 1005 cmdline_row = msg_row; 1006 --no_wait_return; 1007 } 1008 1009 /* 1010 * 2. Execute one '|' separated command. 1011 * do_one_cmd() will return NULL if there is no trailing '|'. 1012 * "cmdline_copy" can change, e.g. for '%' and '#' expansion. 1013 */ 1014 ++recursive; 1015 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE, 1016 #ifdef FEAT_EVAL 1017 &cstack, 1018 #endif 1019 cmd_getline, cmd_cookie); 1020 --recursive; 1021 1022 #ifdef FEAT_EVAL 1023 if (cmd_cookie == (void *)&cmd_while_cookie) 1024 /* Use "current_line" from "cmd_while_cookie", it may have been 1025 * incremented when defining a function. */ 1026 current_line = cmd_while_cookie.current_line; 1027 #endif 1028 1029 if (next_cmdline == NULL) 1030 { 1031 vim_free(cmdline_copy); 1032 cmdline_copy = NULL; 1033 #ifdef FEAT_CMDHIST 1034 /* 1035 * If the command was typed, remember it for the ':' register. 1036 * Do this AFTER executing the command to make :@: work. 1037 */ 1038 if (getline_equal(getline, cookie, getexline) 1039 && new_last_cmdline != NULL) 1040 { 1041 vim_free(last_cmdline); 1042 last_cmdline = new_last_cmdline; 1043 new_last_cmdline = NULL; 1044 } 1045 #endif 1046 } 1047 else 1048 { 1049 /* need to copy the command after the '|' to cmdline_copy, for the 1050 * next do_one_cmd() */ 1051 mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1); 1052 next_cmdline = cmdline_copy; 1053 } 1054 1055 1056 #ifdef FEAT_EVAL 1057 /* reset did_emsg for a function that is not aborted by an error */ 1058 if (did_emsg && !force_abort 1059 && getline_equal(getline, cookie, get_func_line) 1060 && !func_has_abort(real_cookie)) 1061 did_emsg = FALSE; 1062 1063 if (cstack.cs_whilelevel) 1064 { 1065 ++current_line; 1066 1067 /* 1068 * An ":endwhile" and ":continue" is handled here. 1069 * If we were executing commands, jump back to the ":while". 1070 * If we were not executing commands, decrement whilelevel. 1071 */ 1072 if (cstack.cs_had_endwhile || cstack.cs_had_continue) 1073 { 1074 if (cstack.cs_had_endwhile) 1075 cstack.cs_had_endwhile = FALSE; 1076 else 1077 cstack.cs_had_continue = FALSE; 1078 1079 /* Jump back to the matching ":while". Be careful not to use 1080 * a cs_line[] from an entry that isn't a ":while": It would 1081 * make "current_line" invalid and can cause a crash. */ 1082 if (!did_emsg && !got_int && !did_throw 1083 && cstack.cs_idx >= 0 1084 && (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE) 1085 && cstack.cs_line[cstack.cs_idx] >= 0 1086 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE)) 1087 { 1088 current_line = cstack.cs_line[cstack.cs_idx]; 1089 cstack.cs_had_while = TRUE; /* note we jumped there */ 1090 line_breakcheck(); /* check if CTRL-C typed */ 1091 1092 /* Check for the next breakpoint at or after the ":while".*/ 1093 if (breakpoint != NULL) 1094 { 1095 *breakpoint = dbg_find_breakpoint( 1096 getline_equal(getline, cookie, getsourceline), 1097 fname, 1098 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1); 1099 *dbg_tick = debug_tick; 1100 } 1101 } 1102 else /* can only get here with ":endwhile" */ 1103 { 1104 --cstack.cs_whilelevel; 1105 if (cstack.cs_idx >= 0) 1106 --cstack.cs_idx; 1107 } 1108 } 1109 1110 /* 1111 * For a ":while" we need to remember the line number. 1112 */ 1113 else if (cstack.cs_had_while) 1114 { 1115 cstack.cs_had_while = FALSE; 1116 cstack.cs_line[cstack.cs_idx] = current_line - 1; 1117 } 1118 } 1119 1120 /* 1121 * When not inside any ":while" loop, clear remembered lines. 1122 */ 1123 if (!cstack.cs_whilelevel) 1124 { 1125 if (lines_ga.ga_len > 0) 1126 { 1127 sourcing_lnum = 1128 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum; 1129 free_cmdlines(&lines_ga); 1130 } 1131 current_line = 0; 1132 } 1133 1134 /* 1135 * A ":finally" makes did_emsg, got_int, and did_throw pending for being 1136 * restored at the ":endtry". Reset them here and set the ACTIVE and 1137 * FINALLY flags, so that the finally clause gets executed. This 1138 * includes the case where a missing ":endif" or ":endwhile" was 1139 * detected by the ":finally" itself. 1140 */ 1141 if (cstack.cs_had_finally) 1142 { 1143 cstack.cs_had_finally = FALSE; 1144 report_make_pending(cstack.cs_pending[cstack.cs_idx] & 1145 (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW), 1146 did_throw ? (void *)current_exception : NULL); 1147 did_emsg = got_int = did_throw = FALSE; 1148 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY; 1149 } 1150 1151 /* Update global "trylevel" for recursive calls to do_cmdline() from 1152 * within this loop. */ 1153 trylevel = initial_trylevel + cstack.cs_trylevel; 1154 1155 /* 1156 * If the outermost try conditional (accross function calls and sourced 1157 * files) is aborted because of an error, an interrupt, or an uncaught 1158 * exception, cancel everything. If it is left normally, reset 1159 * force_abort to get the non-EH compatible abortion behavior for 1160 * the rest of the script. 1161 */ 1162 if (trylevel == 0 && !did_emsg && !got_int && !did_throw) 1163 force_abort = FALSE; 1164 1165 /* Convert an interrupt to an exception if appropriate. */ 1166 (void)do_intthrow(&cstack); 1167 #endif /* FEAT_EVAL */ 1168 1169 } 1170 /* 1171 * Continue executing command lines when: 1172 * - no CTRL-C typed, no aborting error, no exception thrown or try 1173 * conditionals need to be checked for executing finally clauses or 1174 * catching an interrupt exception 1175 * - didn't get an error message or lines are not typed 1176 * - there is a command after '|', inside a :if, :while, or :try, or 1177 * looping for ":source" command or function call. 1178 */ 1179 while (!((got_int 1180 #ifdef FEAT_EVAL 1181 || (did_emsg && force_abort) || did_throw 1182 #endif 1183 ) 1184 #ifdef FEAT_EVAL 1185 && cstack.cs_trylevel == 0 1186 #endif 1187 ) 1188 && !(did_emsg && used_getline 1189 && (getline_equal(getline, cookie, getexmodeline) 1190 || getline_equal(getline, cookie, getexline))) 1191 && (next_cmdline != NULL 1192 #ifdef FEAT_EVAL 1193 || cstack.cs_idx >= 0 1194 #endif 1195 || (flags & DOCMD_REPEAT))); 1196 1197 vim_free(cmdline_copy); 1198 #ifdef FEAT_EVAL 1199 free_cmdlines(&lines_ga); 1200 ga_clear(&lines_ga); 1201 1202 if (cstack.cs_idx >= 0) 1203 { 1204 /* 1205 * If a sourced file or executed function ran to its end, report the 1206 * unclosed conditional. 1207 */ 1208 if (!got_int && !did_throw 1209 && ((getline_equal(getline, cookie, getsourceline) 1210 && !source_finished(getline, cookie)) 1211 || (getline_equal(getline, cookie, get_func_line) 1212 && !func_has_ended(real_cookie)))) 1213 { 1214 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY) 1215 EMSG(_(e_endtry)); 1216 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE) 1217 EMSG(_(e_endwhile)); 1218 else 1219 EMSG(_(e_endif)); 1220 } 1221 1222 /* 1223 * Reset "trylevel" in case of a ":finish" or ":return" or a missing 1224 * ":endtry" in a sourced file or executed function. If the try 1225 * conditional is in its finally clause, ignore anything pending. 1226 * If it is in a catch clause, finish the caught exception. 1227 */ 1228 do 1229 cstack.cs_idx = cleanup_conditionals(&cstack, 0, TRUE); 1230 while (--cstack.cs_idx >= 0); 1231 trylevel = initial_trylevel; 1232 } 1233 1234 /* If a missing ":endtry", ":endwhile", or ":endif" or a memory lack 1235 * was reported above and the error message is to be converted to an 1236 * exception, do this now after rewinding the cstack. */ 1237 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line) 1238 ? (char_u *)"endfunction" : (char_u *)NULL); 1239 1240 if (trylevel == 0) 1241 { 1242 /* 1243 * When an exception is being thrown out of the outermost try 1244 * conditional, discard the uncaught exception, disable the conversion 1245 * of interrupts or errors to exceptions, and ensure that no more 1246 * commands are executed. 1247 */ 1248 if (did_throw) 1249 { 1250 void *p = NULL; 1251 char_u *saved_sourcing_name; 1252 int saved_sourcing_lnum; 1253 struct msglist *messages = NULL, *next; 1254 1255 /* 1256 * If the uncaught exception is a user exception, report it as an 1257 * error. If it is an error exception, display the saved error 1258 * message now. For an interrupt exception, do nothing; the 1259 * interrupt message is given elsewhere. 1260 */ 1261 switch (current_exception->type) 1262 { 1263 case ET_USER: 1264 sprintf((char *)IObuff, _("E605: Exception not caught: %s"), 1265 current_exception->value); 1266 p = vim_strsave(IObuff); 1267 break; 1268 case ET_ERROR: 1269 messages = current_exception->messages; 1270 current_exception->messages = NULL; 1271 break; 1272 case ET_INTERRUPT: 1273 break; 1274 default: 1275 p = vim_strsave((char_u *)_(e_internal)); 1276 } 1277 1278 saved_sourcing_name = sourcing_name; 1279 saved_sourcing_lnum = sourcing_lnum; 1280 sourcing_name = current_exception->throw_name; 1281 sourcing_lnum = current_exception->throw_lnum; 1282 current_exception->throw_name = NULL; 1283 1284 discard_current_exception(); /* uses IObuff if 'verbose' */ 1285 suppress_errthrow = TRUE; 1286 force_abort = TRUE; 1287 1288 if (messages != NULL) 1289 { 1290 do 1291 { 1292 next = messages->next; 1293 emsg(messages->msg); 1294 vim_free(messages->msg); 1295 vim_free(messages); 1296 messages = next; 1297 } 1298 while (messages != NULL); 1299 } 1300 else if (p != NULL) 1301 { 1302 emsg(p); 1303 vim_free(p); 1304 } 1305 vim_free(sourcing_name); 1306 sourcing_name = saved_sourcing_name; 1307 sourcing_lnum = saved_sourcing_lnum; 1308 } 1309 1310 /* 1311 * On an interrupt or an aborting error not converted to an exception, 1312 * disable the conversion of errors to exceptions. (Interrupts are not 1313 * converted any more, here.) This enables also the interrupt message 1314 * when force_abort is set and did_emsg unset in case of an interrupt 1315 * from a finally clause after an error. 1316 */ 1317 else if (got_int || (did_emsg && force_abort)) 1318 suppress_errthrow = TRUE; 1319 } 1320 1321 /* 1322 * The current cstack will be freed when do_cmdline() returns. An uncaught 1323 * exception will have to be rethrown in the previous cstack. If a function 1324 * has just returned or a script file was just finished and the previous 1325 * cstack belongs to the same function or, respectively, script file, it 1326 * will have to be checked for finally clauses to be executed due to the 1327 * ":return" or ":finish". This is done in do_one_cmd(). 1328 */ 1329 if (did_throw) 1330 need_rethrow = TRUE; 1331 if ((getline_equal(getline, cookie, getsourceline) 1332 && ex_nesting_level > source_level(real_cookie)) 1333 || (getline_equal(getline, cookie, get_func_line) 1334 && ex_nesting_level > func_level(real_cookie) + 1)) 1335 { 1336 if (!did_throw) 1337 check_cstack = TRUE; 1338 } 1339 else 1340 { 1341 /* When leaving a function, reduce nesting level. */ 1342 if (getline_equal(getline, cookie, get_func_line)) 1343 --ex_nesting_level; 1344 /* 1345 * Go to debug mode when returning from a function in which we are 1346 * single-stepping. 1347 */ 1348 if ((getline_equal(getline, cookie, getsourceline) 1349 || getline_equal(getline, cookie, get_func_line)) 1350 && ex_nesting_level + 1 <= debug_break_level) 1351 do_debug(getline_equal(getline, cookie, getsourceline) 1352 ? (char_u *)_("End of sourced file") 1353 : (char_u *)_("End of function")); 1354 } 1355 1356 /* 1357 * Restore the exception environment (done after returning from the 1358 * debugger). 1359 */ 1360 if (flags & DOCMD_EXCRESET) 1361 restore_dbg_stuff(&debug_saved); 1362 1363 msg_list = saved_msg_list; 1364 #endif /* FEAT_EVAL */ 1365 1366 /* 1367 * If there was too much output to fit on the command line, ask the user to 1368 * hit return before redrawing the screen. With the ":global" command we do 1369 * this only once after the command is finished. 1370 */ 1371 if (did_inc) 1372 { 1373 --RedrawingDisabled; 1374 --no_wait_return; 1375 msg_scroll = FALSE; 1376 1377 /* 1378 * When just finished an ":if"-":else" which was typed, no need to 1379 * wait for hit-return. Also for an error situation. 1380 */ 1381 if (retval == FAIL 1382 #ifdef FEAT_EVAL 1383 || (did_endif && KeyTyped && !did_emsg) 1384 #endif 1385 ) 1386 { 1387 need_wait_return = FALSE; 1388 msg_didany = FALSE; /* don't wait when restarting edit */ 1389 } 1390 else if (need_wait_return) 1391 { 1392 /* 1393 * The msg_start() above clears msg_didout. The wait_return we do 1394 * here should not overwrite the command that may be shown before 1395 * doing that. 1396 */ 1397 msg_didout |= msg_didout_before_start; 1398 wait_return(FALSE); 1399 } 1400 } 1401 1402 #ifndef FEAT_EVAL 1403 /* 1404 * Reset if_level, in case a sourced script file contains more ":if" than 1405 * ":endif" (could be ":if x | foo | endif"). 1406 */ 1407 if_level = 0; 1408 #endif 1409 1410 --call_depth; 1411 return retval; 1412 } 1413 1414 #ifdef FEAT_EVAL 1415 /* 1416 * Obtain a line when inside a ":while" loop. 1417 */ 1418 static char_u * 1419 get_while_line(c, cookie, indent) 1420 int c; 1421 void *cookie; 1422 int indent; 1423 { 1424 struct while_cookie *cp = (struct while_cookie *)cookie; 1425 wcmd_T *wp; 1426 char_u *line; 1427 1428 if (cp->current_line + 1 >= cp->lines_gap->ga_len) 1429 { 1430 if (cp->repeating) 1431 return NULL; /* trying to read past the ":endwhile" */ 1432 1433 /* First time inside the ":while": get line normally. */ 1434 if (cp->getline == NULL) 1435 line = getcmdline(c, 0L, indent); 1436 else 1437 line = cp->getline(c, cp->cookie, indent); 1438 if (store_while_line(cp->lines_gap, line) == OK) 1439 ++cp->current_line; 1440 1441 return line; 1442 } 1443 1444 KeyTyped = FALSE; 1445 ++cp->current_line; 1446 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line; 1447 sourcing_lnum = wp->lnum; 1448 return vim_strsave(wp->line); 1449 } 1450 1451 /* 1452 * Store a line in "gap" so that a ":while" loop can execute it again. 1453 */ 1454 static int 1455 store_while_line(gap, line) 1456 garray_T *gap; 1457 char_u *line; 1458 { 1459 if (ga_grow(gap, 1) == FAIL) 1460 return FAIL; 1461 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line); 1462 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum; 1463 ++gap->ga_len; 1464 --gap->ga_room; 1465 return OK; 1466 } 1467 1468 /* 1469 * Free the lines stored for a ":while" loop. 1470 */ 1471 static void 1472 free_cmdlines(gap) 1473 garray_T *gap; 1474 { 1475 while (gap->ga_len > 0) 1476 { 1477 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line); 1478 --gap->ga_len; 1479 ++gap->ga_room; 1480 } 1481 } 1482 #endif 1483 1484 /* 1485 * If "getline" is get_while_line(), return TRUE if the getline it uses equals 1486 * "func". * Otherwise return TRUE when "getline" equals "func". 1487 */ 1488 /*ARGSUSED*/ 1489 int 1490 getline_equal(getline, cookie, func) 1491 char_u *(*getline) __ARGS((int, void *, int)); 1492 void *cookie; /* argument for getline() */ 1493 char_u *(*func) __ARGS((int, void *, int)); 1494 { 1495 #ifdef FEAT_EVAL 1496 char_u *(*gp) __ARGS((int, void *, int)); 1497 struct while_cookie *cp; 1498 1499 /* When "getline" is "get_while_line()" use the "cookie" to find the 1500 * function that's orignally used to obtain the lines. This may be nested 1501 * several levels. */ 1502 gp = getline; 1503 cp = (struct while_cookie *)cookie; 1504 while (gp == get_while_line) 1505 { 1506 gp = cp->getline; 1507 cp = cp->cookie; 1508 } 1509 return gp == func; 1510 #else 1511 return getline == func; 1512 #endif 1513 } 1514 1515 #if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO) 1516 /* 1517 * If "getline" is get_while_line(), return the cookie used by the original 1518 * getline function. Otherwise return "cookie". 1519 */ 1520 /*ARGSUSED*/ 1521 void * 1522 getline_cookie(getline, cookie) 1523 char_u *(*getline) __ARGS((int, void *, int)); 1524 void *cookie; /* argument for getline() */ 1525 { 1526 # ifdef FEAT_EVAL 1527 char_u *(*gp) __ARGS((int, void *, int)); 1528 struct while_cookie *cp; 1529 1530 /* When "getline" is "get_while_line()" use the "cookie" to find the 1531 * cookie that's orignally used to obtain the lines. This may be nested 1532 * several levels. */ 1533 gp = getline; 1534 cp = (struct while_cookie *)cookie; 1535 while (gp == get_while_line) 1536 { 1537 gp = cp->getline; 1538 cp = cp->cookie; 1539 } 1540 return cp; 1541 # else 1542 return cookie; 1543 # endif 1544 } 1545 #endif 1546 1547 /* 1548 * Execute one Ex command. 1549 * 1550 * If 'sourcing' is TRUE, the command will be included in the error message. 1551 * 1552 * 1. skip comment lines and leading space 1553 * 2. handle command modifiers 1554 * 3. parse range 1555 * 4. parse command 1556 * 5. parse arguments 1557 * 6. switch on command name 1558 * 1559 * Note: "getline" can be NULL. 1560 * 1561 * This function may be called recursively! 1562 */ 1563 #if (_MSC_VER == 1200) 1564 /* 1565 * Avoid optimisation bug in VC++ version 6.0 1566 */ 1567 # pragma optimize( "g", off ) 1568 #endif 1569 static char_u * 1570 do_one_cmd(cmdlinep, sourcing, 1571 #ifdef FEAT_EVAL 1572 cstack, 1573 #endif 1574 getline, cookie) 1575 char_u **cmdlinep; 1576 int sourcing; 1577 #ifdef FEAT_EVAL 1578 struct condstack *cstack; 1579 #endif 1580 char_u *(*getline) __ARGS((int, void *, int)); 1581 void *cookie; /* argument for getline() */ 1582 { 1583 char_u *p; 1584 linenr_T lnum; 1585 long n; 1586 char_u *errormsg = NULL; /* error message */ 1587 exarg_T ea; /* Ex command arguments */ 1588 long verbose_save = -1; 1589 int save_msg_scroll = 0; 1590 int did_silent = 0; 1591 int did_esilent = 0; 1592 cmdmod_T save_cmdmod; 1593 int ni; /* set when Not Implemented */ 1594 1595 vim_memset(&ea, 0, sizeof(ea)); 1596 ea.line1 = 1; 1597 ea.line2 = 1; 1598 #ifdef FEAT_EVAL 1599 ++ex_nesting_level; 1600 #endif 1601 1602 /* when not editing the last file :q has to be typed twice */ 1603 if (quitmore 1604 #ifdef FEAT_EVAL 1605 /* avoid that a function call in 'statusline' does this */ 1606 && !getline_equal(getline, cookie, get_func_line) 1607 #endif 1608 ) 1609 --quitmore; 1610 1611 /* 1612 * Reset browse, confirm, etc.. They are restored when returning, for 1613 * recursive calls. 1614 */ 1615 save_cmdmod = cmdmod; 1616 vim_memset(&cmdmod, 0, sizeof(cmdmod)); 1617 1618 /* 1619 * Repeat until no more command modifiers are found. 1620 */ 1621 ea.cmd = *cmdlinep; 1622 for (;;) 1623 { 1624 /* 1625 * 1. skip comment lines and leading white space and colons 1626 */ 1627 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':') 1628 ++ea.cmd; 1629 1630 /* in ex mode, an empty line works like :+ */ 1631 if (*ea.cmd == NUL && exmode_active 1632 && (getline_equal(getline, cookie, getexmodeline) 1633 || getline_equal(getline, cookie, getexline))) 1634 { 1635 ea.cmd = (char_u *)"+"; 1636 ex_pressedreturn = TRUE; 1637 } 1638 1639 /* ignore comment and empty lines */ 1640 if (*ea.cmd == '"' || *ea.cmd == NUL) 1641 goto doend; 1642 1643 /* 1644 * 2. handle command modifiers. 1645 */ 1646 p = ea.cmd; 1647 if (VIM_ISDIGIT(*ea.cmd)) 1648 p = skipwhite(skipdigits(ea.cmd)); 1649 switch (*p) 1650 { 1651 /* When adding an entry, also modify cmd_exists(). */ 1652 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3)) 1653 break; 1654 #ifdef FEAT_WINDOWS 1655 cmdmod.split |= WSP_ABOVE; 1656 #endif 1657 continue; 1658 1659 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3)) 1660 { 1661 #ifdef FEAT_WINDOWS 1662 cmdmod.split |= WSP_BELOW; 1663 #endif 1664 continue; 1665 } 1666 if (checkforcmd(&ea.cmd, "browse", 3)) 1667 { 1668 #ifdef FEAT_BROWSE 1669 cmdmod.browse = TRUE; 1670 #endif 1671 continue; 1672 } 1673 if (!checkforcmd(&ea.cmd, "botright", 2)) 1674 break; 1675 #ifdef FEAT_WINDOWS 1676 cmdmod.split |= WSP_BOT; 1677 #endif 1678 continue; 1679 1680 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4)) 1681 break; 1682 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 1683 cmdmod.confirm = TRUE; 1684 #endif 1685 continue; 1686 1687 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3)) 1688 { 1689 cmdmod.keepmarks = TRUE; 1690 continue; 1691 } 1692 if (!checkforcmd(&ea.cmd, "keepjumps", 5)) 1693 break; 1694 cmdmod.keepjumps = TRUE; 1695 continue; 1696 1697 /* ":hide" and ":hide | cmd" are not modifiers */ 1698 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3) 1699 || *p == NUL || ends_excmd(*p)) 1700 break; 1701 ea.cmd = p; 1702 cmdmod.hide = TRUE; 1703 continue; 1704 1705 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3)) 1706 { 1707 cmdmod.lockmarks = TRUE; 1708 continue; 1709 } 1710 1711 if (!checkforcmd(&ea.cmd, "leftabove", 5)) 1712 break; 1713 #ifdef FEAT_WINDOWS 1714 cmdmod.split |= WSP_ABOVE; 1715 #endif 1716 continue; 1717 1718 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6)) 1719 break; 1720 #ifdef FEAT_WINDOWS 1721 cmdmod.split |= WSP_BELOW; 1722 #endif 1723 continue; 1724 1725 case 's': if (!checkforcmd(&ea.cmd, "silent", 3)) 1726 break; 1727 ++did_silent; 1728 ++msg_silent; 1729 save_msg_scroll = msg_scroll; 1730 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1])) 1731 { 1732 /* ":silent!", but not "silent !cmd" */ 1733 ea.cmd = skipwhite(ea.cmd + 1); 1734 ++emsg_silent; 1735 ++did_esilent; 1736 } 1737 continue; 1738 1739 case 't': if (!checkforcmd(&ea.cmd, "topleft", 2)) 1740 break; 1741 #ifdef FEAT_WINDOWS 1742 cmdmod.split |= WSP_TOP; 1743 #endif 1744 continue; 1745 1746 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4)) 1747 { 1748 #ifdef FEAT_VERTSPLIT 1749 cmdmod.split |= WSP_VERT; 1750 #endif 1751 continue; 1752 } 1753 if (!checkforcmd(&p, "verbose", 4)) 1754 break; 1755 if (verbose_save < 0) 1756 verbose_save = p_verbose; 1757 if (vim_isdigit(*ea.cmd)) 1758 p_verbose = atoi((char *)ea.cmd); 1759 else 1760 p_verbose = 1; 1761 ea.cmd = p; 1762 continue; 1763 } 1764 break; 1765 } 1766 1767 #ifdef FEAT_EVAL 1768 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0 1769 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)); 1770 #else 1771 ea.skip = (if_level > 0); 1772 #endif 1773 1774 #ifdef FEAT_EVAL 1775 /* May go to debug mode. If this happens and the ">quit" debug command is 1776 * used, throw an interrupt exception and skip the next command. */ 1777 dbg_check_breakpoint(&ea); 1778 if (!ea.skip && got_int) 1779 { 1780 ea.skip = TRUE; 1781 (void)do_intthrow(cstack); 1782 } 1783 #endif 1784 1785 /* 1786 * 3. parse a range specifier of the form: addr [,addr] [;addr] .. 1787 * 1788 * where 'addr' is: 1789 * 1790 * % (entire file) 1791 * $ [+-NUM] 1792 * 'x [+-NUM] (where x denotes a currently defined mark) 1793 * . [+-NUM] 1794 * [+-NUM].. 1795 * NUM 1796 * 1797 * The ea.cmd pointer is updated to point to the first character following the 1798 * range spec. If an initial address is found, but no second, the upper bound 1799 * is equal to the lower. 1800 */ 1801 1802 /* repeat for all ',' or ';' separated addresses */ 1803 for (;;) 1804 { 1805 ea.line1 = ea.line2; 1806 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */ 1807 ea.cmd = skipwhite(ea.cmd); 1808 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0); 1809 if (ea.cmd == NULL) /* error detected */ 1810 goto doend; 1811 if (lnum == MAXLNUM) 1812 { 1813 if (*ea.cmd == '%') /* '%' - all lines */ 1814 { 1815 ++ea.cmd; 1816 ea.line1 = 1; 1817 ea.line2 = curbuf->b_ml.ml_line_count; 1818 ++ea.addr_count; 1819 } 1820 /* '*' - visual area */ 1821 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL) 1822 { 1823 pos_T *fp; 1824 1825 ++ea.cmd; 1826 if (!ea.skip) 1827 { 1828 fp = getmark('<', FALSE); 1829 if (check_mark(fp) == FAIL) 1830 goto doend; 1831 ea.line1 = fp->lnum; 1832 fp = getmark('>', FALSE); 1833 if (check_mark(fp) == FAIL) 1834 goto doend; 1835 ea.line2 = fp->lnum; 1836 ++ea.addr_count; 1837 } 1838 } 1839 } 1840 else 1841 ea.line2 = lnum; 1842 ea.addr_count++; 1843 1844 if (*ea.cmd == ';') 1845 { 1846 if (!ea.skip) 1847 curwin->w_cursor.lnum = ea.line2; 1848 } 1849 else if (*ea.cmd != ',') 1850 break; 1851 ++ea.cmd; 1852 } 1853 1854 /* One address given: set start and end lines */ 1855 if (ea.addr_count == 1) 1856 { 1857 ea.line1 = ea.line2; 1858 /* ... but only implicit: really no address given */ 1859 if (lnum == MAXLNUM) 1860 ea.addr_count = 0; 1861 } 1862 1863 /* Don't leave the cursor on an illegal line (caused by ';') */ 1864 check_cursor_lnum(); 1865 1866 /* 1867 * 4. parse command 1868 */ 1869 1870 /* 1871 * Skip ':' and any white space 1872 */ 1873 ea.cmd = skipwhite(ea.cmd); 1874 while (*ea.cmd == ':') 1875 ea.cmd = skipwhite(ea.cmd + 1); 1876 1877 /* 1878 * If we got a line, but no command, then go to the line. 1879 * If we find a '|' or '\n' we set ea.nextcmd. 1880 */ 1881 if (*ea.cmd == NUL || *ea.cmd == '"' || 1882 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL) 1883 { 1884 /* 1885 * strange vi behaviour: 1886 * ":3" jumps to line 3 1887 * ":3|..." prints line 3 1888 * ":|" prints current line 1889 */ 1890 if (ea.skip) /* skip this if inside :if */ 1891 goto doend; 1892 if (*ea.cmd == '|') 1893 { 1894 ea.cmdidx = CMD_print; 1895 ea.argt = RANGE+COUNT+TRLBAR; 1896 if ((errormsg = invalid_range(&ea)) == NULL) 1897 { 1898 correct_range(&ea); 1899 ex_print(&ea); 1900 } 1901 } 1902 else if (ea.addr_count != 0) 1903 { 1904 if (ea.line2 < 0) 1905 errormsg = invalid_range(&ea); 1906 else 1907 { 1908 if (ea.line2 == 0) 1909 curwin->w_cursor.lnum = 1; 1910 else if (ea.line2 > curbuf->b_ml.ml_line_count) 1911 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; 1912 else 1913 curwin->w_cursor.lnum = ea.line2; 1914 beginline(BL_SOL | BL_FIX); 1915 } 1916 } 1917 goto doend; 1918 } 1919 1920 /* Find the command and let "p" point to after it. */ 1921 p = find_command(&ea, NULL); 1922 1923 #ifdef FEAT_USR_CMDS 1924 if (p == NULL) 1925 { 1926 if (!ea.skip) 1927 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command"); 1928 goto doend; 1929 } 1930 /* Check for wrong commands. */ 1931 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78) 1932 { 1933 errormsg = uc_fun_cmd(); 1934 goto doend; 1935 } 1936 #endif 1937 if (ea.cmdidx == CMD_SIZE) 1938 { 1939 if (!ea.skip) 1940 { 1941 STRCPY(IObuff, _("E492: Not an editor command")); 1942 if (!sourcing) 1943 { 1944 STRCAT(IObuff, ": "); 1945 STRNCAT(IObuff, *cmdlinep, 40); 1946 } 1947 errormsg = IObuff; 1948 } 1949 goto doend; 1950 } 1951 1952 ni = ( 1953 #ifdef FEAT_USR_CMDS 1954 !USER_CMDIDX(ea.cmdidx) && 1955 #endif 1956 cmdnames[ea.cmdidx].cmd_func == ex_ni); 1957 1958 #ifndef FEAT_EVAL 1959 /* 1960 * When the expression evaluation is disabled, recognize the ":if" and 1961 * ":endif" commands and ignore everything in between it. 1962 */ 1963 if (ea.cmdidx == CMD_if) 1964 ++if_level; 1965 if (if_level) 1966 { 1967 if (ea.cmdidx == CMD_endif) 1968 --if_level; 1969 goto doend; 1970 } 1971 1972 #endif 1973 1974 if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */ 1975 { 1976 ++p; 1977 ea.forceit = TRUE; 1978 } 1979 else 1980 ea.forceit = FALSE; 1981 1982 /* 1983 * 5. parse arguments 1984 */ 1985 #ifdef FEAT_USR_CMDS 1986 if (!USER_CMDIDX(ea.cmdidx)) 1987 #endif 1988 ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt; 1989 1990 if (!ea.skip) 1991 { 1992 #ifdef HAVE_SANDBOX 1993 if (sandbox != 0 && !(ea.argt & SBOXOK)) 1994 { 1995 /* Command not allowed in sandbox. */ 1996 errormsg = (char_u *)_(e_sandbox); 1997 goto doend; 1998 } 1999 #endif 2000 if (!curbuf->b_p_ma && (ea.argt & MODIFY)) 2001 { 2002 /* Command not allowed in non-'modifiable' buffer */ 2003 errormsg = (char_u *)_(e_modifiable); 2004 goto doend; 2005 } 2006 #ifdef FEAT_CMDWIN 2007 if (cmdwin_type != 0 && !(ea.argt & CMDWIN) 2008 # ifdef FEAT_USR_CMDS 2009 && !USER_CMDIDX(ea.cmdidx) 2010 # endif 2011 ) 2012 { 2013 /* Command not allowed in cmdline window. */ 2014 errormsg = (char_u *)_(e_cmdwin); 2015 goto doend; 2016 } 2017 #endif 2018 2019 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0) 2020 { 2021 /* no range allowed */ 2022 errormsg = (char_u *)_(e_norange); 2023 goto doend; 2024 } 2025 } 2026 2027 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */ 2028 { 2029 errormsg = (char_u *)_(e_nobang); 2030 goto doend; 2031 } 2032 2033 /* 2034 * Don't complain about the range if it is not used 2035 * (could happen if line_count is accidentally set to 0). 2036 */ 2037 if (!ea.skip && !ni) 2038 { 2039 /* 2040 * If the range is backwards, ask for confirmation and, if given, swap 2041 * ea.line1 & ea.line2 so it's forwards again. 2042 * When global command is busy, don't ask, will fail below. 2043 */ 2044 if (!global_busy && ea.line1 > ea.line2) 2045 { 2046 if (sourcing) 2047 { 2048 errormsg = (char_u *)_("E493: Backwards range given"); 2049 goto doend; 2050 } 2051 else 2052 { 2053 int msg_silent_save = msg_silent; 2054 2055 msg_silent = 0; 2056 if (ask_yesno((char_u *) 2057 _("Backwards range given, OK to swap"), FALSE) != 'y') 2058 goto doend; 2059 msg_silent = msg_silent_save; 2060 } 2061 lnum = ea.line1; 2062 ea.line1 = ea.line2; 2063 ea.line2 = lnum; 2064 } 2065 if ((errormsg = invalid_range(&ea)) != NULL) 2066 goto doend; 2067 } 2068 2069 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */ 2070 ea.line2 = 1; 2071 2072 correct_range(&ea); 2073 2074 #ifdef FEAT_FOLDING 2075 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy) 2076 { 2077 /* Put the first line at the start of a closed fold, put the last line 2078 * at the end of a closed fold. */ 2079 (void)hasFolding(ea.line1, &ea.line1, NULL); 2080 (void)hasFolding(ea.line2, NULL, &ea.line2); 2081 } 2082 #endif 2083 2084 #ifdef FEAT_QUICKFIX 2085 /* 2086 * For the :make and :grep commands we insert the 'makeprg'/'grepprg' 2087 * option here, so things like % get expanded. 2088 */ 2089 if (ea.cmdidx == CMD_make || ea.cmdidx == CMD_grep 2090 || ea.cmdidx == CMD_grepadd) 2091 { 2092 char_u *new_cmdline; 2093 char_u *program; 2094 char_u *pos; 2095 char_u *ptr; 2096 int len; 2097 int i; 2098 2099 if (ea.cmdidx == CMD_grep || ea.cmdidx == CMD_grepadd) 2100 { 2101 if (*curbuf->b_p_gp == NUL) 2102 program = p_gp; 2103 else 2104 program = curbuf->b_p_gp; 2105 } 2106 else 2107 { 2108 if (*curbuf->b_p_mp == NUL) 2109 program = p_mp; 2110 else 2111 program = curbuf->b_p_mp; 2112 } 2113 2114 p = skipwhite(p); 2115 2116 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) 2117 { /* replace $* by given arguments */ 2118 i = 1; 2119 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL) 2120 ++i; 2121 len = (int)STRLEN(p); 2122 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1)); 2123 if (new_cmdline == NULL) 2124 goto doend; /* out of memory */ 2125 ptr = new_cmdline; 2126 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) 2127 { 2128 i = (int)(pos - program); 2129 STRNCPY(ptr, program, i); 2130 STRCPY(ptr += i, p); 2131 ptr += len; 2132 program = pos + 2; 2133 } 2134 STRCPY(ptr, program); 2135 } 2136 else 2137 { 2138 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2)); 2139 if (new_cmdline == NULL) 2140 goto doend; /* out of memory */ 2141 STRCPY(new_cmdline, program); 2142 STRCAT(new_cmdline, " "); 2143 STRCAT(new_cmdline, p); 2144 } 2145 msg_make(p); 2146 /* 'ea.cmd' is not set here, because it is not used at CMD_make */ 2147 vim_free(*cmdlinep); 2148 *cmdlinep = new_cmdline; 2149 p = new_cmdline; 2150 } 2151 #endif 2152 2153 /* 2154 * Skip to start of argument. 2155 * Don't do this for the ":!" command, because ":!! -l" needs the space. 2156 */ 2157 if (ea.cmdidx == CMD_bang) 2158 ea.arg = p; 2159 else 2160 ea.arg = skipwhite(p); 2161 2162 /* 2163 * Check for "++opt=val" argument. 2164 * Must be first, allow ":w ++enc=utf8 !cmd" 2165 */ 2166 if (ea.argt & ARGOPT) 2167 while (ea.arg[0] == '+' && ea.arg[1] == '+') 2168 if (getargopt(&ea) == FAIL && !ni) 2169 { 2170 errormsg = (char_u *)_(e_invarg); 2171 goto doend; 2172 } 2173 2174 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update) 2175 { 2176 if (*ea.arg == '>') /* append */ 2177 { 2178 if (*++ea.arg != '>') /* typed wrong */ 2179 { 2180 errormsg = (char_u *)_("E494: Use w or w>>"); 2181 goto doend; 2182 } 2183 ea.arg = skipwhite(ea.arg + 1); 2184 ea.append = TRUE; 2185 } 2186 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */ 2187 { 2188 ++ea.arg; 2189 ea.usefilter = TRUE; 2190 } 2191 } 2192 2193 if (ea.cmdidx == CMD_read) 2194 { 2195 if (ea.forceit) 2196 { 2197 ea.usefilter = TRUE; /* :r! filter if ea.forceit */ 2198 ea.forceit = FALSE; 2199 } 2200 else if (*ea.arg == '!') /* :r !filter */ 2201 { 2202 ++ea.arg; 2203 ea.usefilter = TRUE; 2204 } 2205 } 2206 2207 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift) 2208 { 2209 ea.amount = 1; 2210 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */ 2211 { 2212 ++ea.arg; 2213 ++ea.amount; 2214 } 2215 ea.arg = skipwhite(ea.arg); 2216 } 2217 2218 /* 2219 * Check for "+command" argument, before checking for next command. 2220 * Don't do this for ":read !cmd" and ":write !cmd". 2221 */ 2222 if ((ea.argt & EDITCMD) && !ea.usefilter) 2223 ea.do_ecmd_cmd = getargcmd(&ea.arg); 2224 2225 /* 2226 * Check for '|' to separate commands and '"' to start comments. 2227 * Don't do this for ":read !cmd" and ":write !cmd". 2228 */ 2229 if ((ea.argt & TRLBAR) && !ea.usefilter) 2230 separate_nextcmd(&ea); 2231 2232 /* 2233 * Check for <newline> to end a shell command. 2234 * Also do this for ":read !cmd" and ":write !cmd". 2235 */ 2236 else if (ea.cmdidx == CMD_bang || ea.usefilter) 2237 { 2238 for (p = ea.arg; *p; ++p) 2239 { 2240 /* Remove one backslash before a newline, so that it's possible to 2241 * pass a newline to the shell and also a newline that is preceded 2242 * with a backslash. This makes it impossible to end a shell 2243 * command in a backslash, but that doesn't appear useful. 2244 * Halving the number of backslashes is incompatible with previous 2245 * versions. */ 2246 if (*p == '\\' && p[1] == '\n') 2247 mch_memmove(p, p + 1, STRLEN(p)); 2248 else if (*p == '\n') 2249 { 2250 ea.nextcmd = p + 1; 2251 *p = NUL; 2252 break; 2253 } 2254 } 2255 } 2256 2257 if ((ea.argt & DFLALL) && ea.addr_count == 0) 2258 { 2259 ea.line1 = 1; 2260 ea.line2 = curbuf->b_ml.ml_line_count; 2261 } 2262 2263 /* accept numbered register only when no count allowed (:put) */ 2264 if ( (ea.argt & REGSTR) 2265 && *ea.arg != NUL 2266 #ifdef FEAT_USR_CMDS 2267 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put 2268 && USER_CMDIDX(ea.cmdidx))) 2269 /* Do not allow register = for user commands */ 2270 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=') 2271 #else 2272 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put) 2273 #endif 2274 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg))) 2275 { 2276 ea.regname = *ea.arg++; 2277 #ifdef FEAT_EVAL 2278 /* for '=' register: accept the rest of the line as an expression */ 2279 if (ea.arg[-1] == '=' && ea.arg[0] != NUL) 2280 { 2281 set_expr_line(vim_strsave(ea.arg)); 2282 ea.arg += STRLEN(ea.arg); 2283 } 2284 #endif 2285 ea.arg = skipwhite(ea.arg); 2286 } 2287 2288 /* 2289 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a 2290 * count, it's a buffer name. 2291 */ 2292 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg) 2293 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL 2294 || vim_iswhite(*p))) 2295 { 2296 n = getdigits(&ea.arg); 2297 ea.arg = skipwhite(ea.arg); 2298 if (n <= 0 && !ni) 2299 { 2300 errormsg = (char_u *)_(e_zerocount); 2301 goto doend; 2302 } 2303 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */ 2304 { 2305 ea.line2 = n; 2306 if (ea.addr_count == 0) 2307 ea.addr_count = 1; 2308 } 2309 else 2310 { 2311 ea.line1 = ea.line2; 2312 ea.line2 += n - 1; 2313 ++ea.addr_count; 2314 /* 2315 * Be vi compatible: no error message for out of range. 2316 */ 2317 if (ea.line2 > curbuf->b_ml.ml_line_count) 2318 ea.line2 = curbuf->b_ml.ml_line_count; 2319 } 2320 } 2321 /* no arguments allowed */ 2322 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL && 2323 vim_strchr((char_u *)"|\"", *ea.arg) == NULL) 2324 { 2325 errormsg = (char_u *)_(e_trailing); 2326 goto doend; 2327 } 2328 2329 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL) 2330 { 2331 errormsg = (char_u *)_(e_argreq); 2332 goto doend; 2333 } 2334 2335 #ifdef FEAT_EVAL 2336 /* 2337 * Skip the command when it's not going to be executed. 2338 * The commands like :if, :endif, etc. always need to be executed. 2339 * Also make an exception for commands that handle a trailing command 2340 * themselves. 2341 */ 2342 if (ea.skip) 2343 { 2344 switch (ea.cmdidx) 2345 { 2346 /* commands that need evaluation */ 2347 case CMD_while: 2348 case CMD_endwhile: 2349 case CMD_if: 2350 case CMD_elseif: 2351 case CMD_else: 2352 case CMD_endif: 2353 case CMD_try: 2354 case CMD_catch: 2355 case CMD_finally: 2356 case CMD_endtry: 2357 case CMD_function: 2358 break; 2359 2360 /* Commands that handle '|' themselves. Check: A command should 2361 * either have the TRLBAR flag, appear in this list or appear in 2362 * the list at ":help :bar". */ 2363 case CMD_aboveleft: 2364 case CMD_and: 2365 case CMD_belowright: 2366 case CMD_botright: 2367 case CMD_browse: 2368 case CMD_call: 2369 case CMD_confirm: 2370 case CMD_delfunction: 2371 case CMD_djump: 2372 case CMD_dlist: 2373 case CMD_dsearch: 2374 case CMD_dsplit: 2375 case CMD_echo: 2376 case CMD_echoerr: 2377 case CMD_echomsg: 2378 case CMD_echon: 2379 case CMD_execute: 2380 case CMD_help: 2381 case CMD_hide: 2382 case CMD_ijump: 2383 case CMD_ilist: 2384 case CMD_isearch: 2385 case CMD_isplit: 2386 case CMD_keepjumps: 2387 case CMD_keepmarks: 2388 case CMD_leftabove: 2389 case CMD_let: 2390 case CMD_lockmarks: 2391 case CMD_match: 2392 case CMD_perl: 2393 case CMD_psearch: 2394 case CMD_python: 2395 case CMD_return: 2396 case CMD_rightbelow: 2397 case CMD_ruby: 2398 case CMD_silent: 2399 case CMD_smagic: 2400 case CMD_snomagic: 2401 case CMD_substitute: 2402 case CMD_syntax: 2403 case CMD_tcl: 2404 case CMD_throw: 2405 case CMD_tilde: 2406 case CMD_topleft: 2407 case CMD_unlet: 2408 case CMD_verbose: 2409 case CMD_vertical: 2410 break; 2411 2412 default: goto doend; 2413 } 2414 } 2415 #endif 2416 2417 if (ea.argt & XFILE) 2418 { 2419 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL) 2420 goto doend; 2421 } 2422 2423 #ifdef FEAT_LISTCMDS 2424 /* 2425 * Accept buffer name. Cannot be used at the same time with a buffer 2426 * number. Don't do this for a user command. 2427 */ 2428 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0 2429 # ifdef FEAT_USR_CMDS 2430 && !USER_CMDIDX(ea.cmdidx) 2431 # endif 2432 ) 2433 { 2434 /* 2435 * :bdelete, :bwipeout and :bunload take several arguments, separated 2436 * by spaces: find next space (skipping over escaped characters). 2437 * The others take one argument: ignore trailing spaces. 2438 */ 2439 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout 2440 || ea.cmdidx == CMD_bunload) 2441 p = skiptowhite_esc(ea.arg); 2442 else 2443 { 2444 p = ea.arg + STRLEN(ea.arg); 2445 while (p > ea.arg && vim_iswhite(p[-1])) 2446 --p; 2447 } 2448 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE); 2449 if (ea.line2 < 0) /* failed */ 2450 goto doend; 2451 ea.addr_count = 1; 2452 ea.arg = skipwhite(p); 2453 } 2454 #endif 2455 2456 /* 2457 * 6. switch on command name 2458 * 2459 * The "ea" structure holds the arguments that can be used. 2460 */ 2461 ea.cmdlinep = cmdlinep; 2462 ea.getline = getline; 2463 ea.cookie = cookie; 2464 #ifdef FEAT_EVAL 2465 ea.cstack = cstack; 2466 #endif 2467 2468 #ifdef FEAT_USR_CMDS 2469 if (USER_CMDIDX(ea.cmdidx)) 2470 { 2471 /* 2472 * Execute a user-defined command. 2473 */ 2474 do_ucmd(&ea); 2475 } 2476 else 2477 #endif 2478 { 2479 /* 2480 * Call the function to execute the command. 2481 */ 2482 ea.errmsg = NULL; 2483 (cmdnames[ea.cmdidx].cmd_func)(&ea); 2484 if (ea.errmsg != NULL) 2485 errormsg = (char_u *)_(ea.errmsg); 2486 } 2487 2488 #ifdef FEAT_EVAL 2489 /* 2490 * If the command just executed called do_cmdline(), any throw or ":return" 2491 * or ":finish" encountered there must also check the cstack of the still 2492 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught 2493 * exception, or reanimate a returned function or finished script file and 2494 * return or finish it again. 2495 */ 2496 if (need_rethrow) 2497 do_throw(cstack); 2498 else if (check_cstack) 2499 { 2500 if (source_finished(getline, cookie)) 2501 do_finish(&ea, TRUE); 2502 else if (getline_equal(getline, cookie, get_func_line) 2503 && current_func_returned()) 2504 do_return(&ea, TRUE, FALSE, NULL); 2505 } 2506 need_rethrow = check_cstack = FALSE; 2507 #endif 2508 2509 doend: 2510 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */ 2511 curwin->w_cursor.lnum = 1; 2512 2513 if (errormsg != NULL && *errormsg != NUL && !did_emsg) 2514 { 2515 if (sourcing) 2516 { 2517 if (errormsg != IObuff) 2518 { 2519 STRCPY(IObuff, errormsg); 2520 errormsg = IObuff; 2521 } 2522 STRCAT(errormsg, ": "); 2523 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff)); 2524 } 2525 emsg(errormsg); 2526 } 2527 #ifdef FEAT_EVAL 2528 do_errthrow(cstack, 2529 (ea.cmdidx != CMD_SIZE 2530 # ifdef FEAT_USR_CMDS 2531 && !USER_CMDIDX(ea.cmdidx) 2532 # endif 2533 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL); 2534 #endif 2535 2536 if (verbose_save >= 0) 2537 p_verbose = verbose_save; 2538 2539 cmdmod = save_cmdmod; 2540 2541 if (did_silent > 0) 2542 { 2543 /* messages could be enabled for a serious error, need to check if the 2544 * counters don't become negative */ 2545 msg_silent -= did_silent; 2546 if (msg_silent < 0) 2547 msg_silent = 0; 2548 emsg_silent -= did_esilent; 2549 if (emsg_silent < 0) 2550 emsg_silent = 0; 2551 /* Restore msg_scroll, it's set by file I/O commands, even when no 2552 * message is actually displayed. */ 2553 msg_scroll = save_msg_scroll; 2554 } 2555 2556 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */ 2557 ea.nextcmd = NULL; 2558 2559 #ifdef FEAT_EVAL 2560 --ex_nesting_level; 2561 #endif 2562 2563 return ea.nextcmd; 2564 } 2565 #if (_MSC_VER == 1200) 2566 # pragma optimize( "", on ) 2567 #endif 2568 2569 /* 2570 * Check for a command modifier command with optional tail. 2571 * If there is a match advance "pp" to the argument and return TRUE. 2572 */ 2573 static int 2574 checkforcmd(pp, cmd, len) 2575 char_u **pp; /* start of command line */ 2576 char *cmd; /* name of command */ 2577 int len; /* required length */ 2578 { 2579 int i; 2580 2581 for (i = 0; cmd[i] != NUL; ++i) 2582 if (cmd[i] != (*pp)[i]) 2583 break; 2584 if (i >= len && !isalpha((*pp)[i])) 2585 { 2586 *pp = skipwhite(*pp + i); 2587 return TRUE; 2588 } 2589 return FALSE; 2590 } 2591 2592 /* 2593 * Find an Ex command by its name, either built-in or user. 2594 * Name can be found at eap->cmd. 2595 * Returns pointer to char after the command name. 2596 * Returns NULL for an ambiguous user command. 2597 */ 2598 /*ARGSUSED*/ 2599 static char_u * 2600 find_command(eap, full) 2601 exarg_T *eap; 2602 int *full; 2603 { 2604 int len; 2605 char_u *p; 2606 2607 /* 2608 * Isolate the command and search for it in the command table. 2609 * Exeptions: 2610 * - the 'k' command can directly be followed by any character. 2611 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' 2612 * but :sre[wind] is another command, as are :scrip[tnames], 2613 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent]. 2614 */ 2615 p = eap->cmd; 2616 if (*p == 'k') 2617 { 2618 eap->cmdidx = CMD_k; 2619 ++p; 2620 } 2621 else if (p[0] == 's' 2622 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r' && p[3] != 'i' && p[4] != 'p') 2623 || p[1] == 'g' 2624 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g') 2625 || p[1] == 'I' 2626 || (p[1] == 'r' && p[2] != 'e'))) 2627 { 2628 eap->cmdidx = CMD_substitute; 2629 ++p; 2630 } 2631 else 2632 { 2633 while (ASCII_ISALPHA(*p)) 2634 ++p; 2635 /* check for non-alpha command */ 2636 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL) 2637 ++p; 2638 len = (int)(p - eap->cmd); 2639 2640 if (ASCII_ISLOWER(*eap->cmd)) 2641 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)]; 2642 else 2643 eap->cmdidx = cmdidxs[26]; 2644 2645 for ( ; (int)eap->cmdidx < (int)CMD_SIZE; 2646 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1)) 2647 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd, 2648 (size_t)len) == 0) 2649 { 2650 #ifdef FEAT_EVAL 2651 if (full != NULL 2652 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL) 2653 *full = TRUE; 2654 #endif 2655 break; 2656 } 2657 2658 #ifdef FEAT_USR_CMDS 2659 /* Look for a user defined command as a last resort */ 2660 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z') 2661 { 2662 ucmd_T *cmd; 2663 int j, k, matchlen = 0; 2664 int found = FALSE, possible = FALSE; 2665 char_u *cp, *np; /* Point into typed cmd and test name */ 2666 garray_T *gap; 2667 int amb_local = FALSE; /* Found ambiguous buffer-local 2668 command, only full match global 2669 is accepted. */ 2670 2671 /* User defined commands may contain numbers */ 2672 while (ASCII_ISALNUM(*p)) 2673 ++p; 2674 len = (int)(p - eap->cmd); 2675 2676 /* 2677 * Look for buffer-local user commands first, then global ones. 2678 */ 2679 gap = &curbuf->b_ucmds; 2680 for (;;) 2681 { 2682 for (j = 0; j < gap->ga_len; ++j) 2683 { 2684 cmd = USER_CMD_GA(gap, j); 2685 cp = eap->cmd; 2686 np = cmd->uc_name; 2687 k = 0; 2688 while (k < len && *np != NUL && *cp++ == *np++) 2689 k++; 2690 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k]))) 2691 { 2692 /* If finding a second match, the command is 2693 * ambiguous. But not if a buffer-local command 2694 * wasn't a full match and a global command is a full 2695 * match. */ 2696 if (k == len && found && *np != NUL) 2697 { 2698 if (gap == &ucmds) 2699 return NULL; 2700 amb_local = TRUE; 2701 } 2702 2703 if (!found || (k == len && *np == NUL)) 2704 { 2705 /* If we matched up to a digit, then there could 2706 * be another command including the digit that we 2707 * should use instead. 2708 */ 2709 if (k == len) 2710 found = TRUE; 2711 else 2712 possible = TRUE; 2713 2714 if (gap == &ucmds) 2715 eap->cmdidx = CMD_USER; 2716 else 2717 eap->cmdidx = CMD_USER_BUF; 2718 eap->argt = cmd->uc_argt; 2719 eap->useridx = j; 2720 2721 /* Do not search for further abbreviations 2722 * if this is an exact match. */ 2723 matchlen = k; 2724 if (k == len && *np == NUL) 2725 { 2726 if (full != NULL) 2727 *full = TRUE; 2728 amb_local = FALSE; 2729 break; 2730 } 2731 } 2732 } 2733 } 2734 2735 /* Stop if we found a full match or searched all. */ 2736 if (j < gap->ga_len || gap == &ucmds) 2737 break; 2738 gap = &ucmds; 2739 } 2740 2741 /* Only found ambiguous matches. */ 2742 if (amb_local) 2743 return NULL; 2744 2745 /* The match we found may be followed immediately by a 2746 * number. Move *p back to point to it. 2747 */ 2748 if (found || possible) 2749 p += matchlen - len; 2750 } 2751 #endif 2752 2753 if (len == 0) 2754 eap->cmdidx = CMD_SIZE; 2755 } 2756 2757 return p; 2758 } 2759 2760 #if defined(FEAT_EVAL) || defined(PROTO) 2761 /* 2762 * Return > 0 if an Ex command "name" exists. 2763 * Return 2 if there is an exact match. 2764 * Return 3 if there is an ambiguous match. 2765 */ 2766 int 2767 cmd_exists(name) 2768 char_u *name; 2769 { 2770 exarg_T ea; 2771 int full = FALSE; 2772 int i; 2773 int j; 2774 static struct cmdmod 2775 { 2776 char *name; 2777 int minlen; 2778 } cmdmods[] = { 2779 {"aboveleft", 3}, 2780 {"belowright", 3}, 2781 {"botright", 2}, 2782 {"browse", 3}, 2783 {"confirm", 4}, 2784 {"hide", 3}, 2785 {"keepjumps", 5}, 2786 {"keepmarks", 3}, 2787 {"leftabove", 5}, 2788 {"lockmarks", 3}, 2789 {"rightbelow", 6}, 2790 {"silent", 3}, 2791 {"topleft", 2}, 2792 {"verbose", 4}, 2793 {"vertical", 4}, 2794 }; 2795 2796 /* Check command modifiers. */ 2797 for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i) 2798 { 2799 for (j = 0; name[j] != NUL; ++j) 2800 if (name[j] != cmdmods[i].name[j]) 2801 break; 2802 if (name[j] == NUL && j >= cmdmods[i].minlen) 2803 return (cmdmods[i].name[j] == NUL ? 2 : 1); 2804 } 2805 2806 /* Check built-in commands and user defined commands. */ 2807 ea.cmd = name; 2808 ea.cmdidx = (cmdidx_T)0; 2809 if (find_command(&ea, &full) == NULL) 2810 return 3; 2811 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1)); 2812 } 2813 #endif 2814 2815 /* 2816 * This is all pretty much copied from do_one_cmd(), with all the extra stuff 2817 * we don't need/want deleted. Maybe this could be done better if we didn't 2818 * repeat all this stuff. The only problem is that they may not stay 2819 * perfectly compatible with each other, but then the command line syntax 2820 * probably won't change that much -- webb. 2821 */ 2822 char_u * 2823 set_one_cmd_context(xp, buff) 2824 expand_T *xp; 2825 char_u *buff; /* buffer for command string */ 2826 { 2827 char_u *p; 2828 char_u *cmd, *arg; 2829 int i = 0; 2830 cmdidx_T cmdidx; 2831 long_u argt = 0; 2832 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) 2833 int compl = EXPAND_NOTHING; 2834 #endif 2835 #ifdef FEAT_CMDL_COMPL 2836 int delim; 2837 #endif 2838 int forceit = FALSE; 2839 int usefilter = FALSE; /* filter instead of file name */ 2840 2841 xp->xp_pattern = buff; 2842 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */ 2843 xp->xp_backslash = XP_BS_NONE; 2844 #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 2845 xp->xp_arg = NULL; 2846 #endif 2847 2848 /* 2849 * 2. skip comment lines and leading space, colons or bars 2850 */ 2851 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++) 2852 ; 2853 xp->xp_pattern = cmd; 2854 2855 if (*cmd == NUL) 2856 return NULL; 2857 if (*cmd == '"') /* ignore comment lines */ 2858 { 2859 xp->xp_context = EXPAND_NOTHING; 2860 return NULL; 2861 } 2862 2863 /* 2864 * 3. parse a range specifier of the form: addr [,addr] [;addr] .. 2865 */ 2866 cmd = skip_range(cmd, &xp->xp_context); 2867 2868 /* 2869 * 4. parse command 2870 */ 2871 2872 cmd = skipwhite(cmd); 2873 xp->xp_pattern = cmd; 2874 if (*cmd == NUL) 2875 return NULL; 2876 if (*cmd == '"') 2877 { 2878 xp->xp_context = EXPAND_NOTHING; 2879 return NULL; 2880 } 2881 2882 if (*cmd == '|' || *cmd == '\n') 2883 return cmd + 1; /* There's another command */ 2884 2885 /* 2886 * Isolate the command and search for it in the command table. 2887 * Exceptions: 2888 * - the 'k' command can directly be followed by any character, but 2889 * do accept "keepmarks" and "keepjumps". 2890 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' 2891 */ 2892 if (*cmd == 'k' && cmd[1] != 'e') 2893 { 2894 cmdidx = CMD_k; 2895 p = cmd + 1; 2896 } 2897 else 2898 { 2899 p = cmd; 2900 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */ 2901 ++p; 2902 /* check for non-alpha command */ 2903 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL) 2904 ++p; 2905 i = (int)(p - cmd); 2906 2907 if (i == 0) 2908 { 2909 xp->xp_context = EXPAND_UNSUCCESSFUL; 2910 return NULL; 2911 } 2912 for (cmdidx = (cmdidx_T)0; (int)cmdidx < (int)CMD_SIZE; 2913 cmdidx = (cmdidx_T)((int)cmdidx + 1)) 2914 if (STRNCMP(cmdnames[(int)cmdidx].cmd_name, cmd, (size_t)i) == 0) 2915 break; 2916 2917 #ifdef FEAT_USR_CMDS 2918 if (cmd[0] >= 'A' && cmd[0] <= 'Z') 2919 { 2920 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */ 2921 ++p; 2922 i = (int)(p - cmd); 2923 } 2924 #endif 2925 } 2926 2927 /* 2928 * If the cursor is touching the command, and it ends in an alpha-numeric 2929 * character, complete the command name. 2930 */ 2931 if (*p == NUL && ASCII_ISALNUM(p[-1])) 2932 return NULL; 2933 2934 if (cmdidx == CMD_SIZE) 2935 { 2936 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL) 2937 { 2938 cmdidx = CMD_substitute; 2939 p = cmd + 1; 2940 } 2941 #ifdef FEAT_USR_CMDS 2942 else if (cmd[0] >= 'A' && cmd[0] <= 'Z') 2943 { 2944 /* Look for a user defined command as a last resort */ 2945 ucmd_T *uc; 2946 int j, k, matchlen = 0; 2947 int found = FALSE, possible = FALSE; 2948 char_u *cp, *np; /* Point into typed cmd and test name */ 2949 garray_T *gap; 2950 2951 gap = &curbuf->b_ucmds; 2952 for (;;) 2953 { 2954 uc = USER_CMD_GA(gap, 0); 2955 for (j = 0; j < gap->ga_len; ++j, ++uc) 2956 { 2957 cp = cmd; 2958 np = uc->uc_name; 2959 k = 0; 2960 while (k < i && *np != NUL && *cp++ == *np++) 2961 k++; 2962 if (k == i || (*np == NUL && VIM_ISDIGIT(cmd[k]))) 2963 { 2964 if (k == i && found) 2965 { 2966 /* Ambiguous abbreviation */ 2967 xp->xp_context = EXPAND_UNSUCCESSFUL; 2968 return NULL; 2969 } 2970 if (!found) 2971 { 2972 /* If we matched up to a digit, then there could 2973 * be another command including the digit that we 2974 * should use instead. 2975 */ 2976 if (k == i) 2977 found = TRUE; 2978 else 2979 possible = TRUE; 2980 2981 if (gap == &ucmds) 2982 cmdidx = CMD_USER; 2983 else 2984 cmdidx = CMD_USER_BUF; 2985 argt = uc->uc_argt; 2986 #ifdef FEAT_CMDL_COMPL 2987 compl = uc->uc_compl; 2988 # ifdef FEAT_EVAL 2989 xp->xp_arg = uc->uc_compl_arg; 2990 xp->xp_scriptID = uc->uc_scriptID; 2991 # endif 2992 #endif 2993 /* Do not search for further abbreviations 2994 * if this is an exact match 2995 */ 2996 matchlen = k; 2997 if (k == i && *np == NUL) 2998 break; 2999 } 3000 } 3001 } 3002 if (gap == &ucmds || j < gap->ga_len) 3003 break; 3004 gap = &ucmds; 3005 } 3006 3007 /* The match we found may be followed immediately by a 3008 * number. Move *p back to point to it. 3009 */ 3010 if (found || possible) 3011 p += matchlen - i; 3012 } 3013 #endif 3014 } 3015 if (cmdidx == CMD_SIZE) 3016 { 3017 /* Not still touching the command and it was an illegal one */ 3018 xp->xp_context = EXPAND_UNSUCCESSFUL; 3019 return NULL; 3020 } 3021 3022 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */ 3023 3024 if (*p == '!') /* forced commands */ 3025 { 3026 forceit = TRUE; 3027 ++p; 3028 } 3029 3030 /* 3031 * 5. parse arguments 3032 */ 3033 #ifdef FEAT_USR_CMDS 3034 if (!USER_CMDIDX(cmdidx)) 3035 #endif 3036 argt = cmdnames[(int)cmdidx].cmd_argt; 3037 3038 arg = skipwhite(p); 3039 3040 if (cmdidx == CMD_write || cmdidx == CMD_update) 3041 { 3042 if (*arg == '>') /* append */ 3043 { 3044 if (*++arg == '>') 3045 ++arg; 3046 arg = skipwhite(arg); 3047 } 3048 else if (*arg == '!' && cmdidx == CMD_write) /* :w !filter */ 3049 { 3050 ++arg; 3051 usefilter = TRUE; 3052 } 3053 } 3054 3055 if (cmdidx == CMD_read) 3056 { 3057 usefilter = forceit; /* :r! filter if forced */ 3058 if (*arg == '!') /* :r !filter */ 3059 { 3060 ++arg; 3061 usefilter = TRUE; 3062 } 3063 } 3064 3065 if (cmdidx == CMD_lshift || cmdidx == CMD_rshift) 3066 { 3067 while (*arg == *cmd) /* allow any number of '>' or '<' */ 3068 ++arg; 3069 arg = skipwhite(arg); 3070 } 3071 3072 /* Does command allow "+command"? */ 3073 if ((argt & EDITCMD) && !usefilter && *arg == '+') 3074 { 3075 /* Check if we're in the +command */ 3076 p = arg + 1; 3077 arg = skip_cmd_arg(arg, FALSE); 3078 3079 /* Still touching the command after '+'? */ 3080 if (*arg == NUL) 3081 return p; 3082 3083 /* Skip space(s) after +command to get to the real argument */ 3084 arg = skipwhite(arg); 3085 } 3086 3087 /* 3088 * Check for '|' to separate commands and '"' to start comments. 3089 * Don't do this for ":read !cmd" and ":write !cmd". 3090 */ 3091 if ((argt & TRLBAR) && !usefilter) 3092 { 3093 p = arg; 3094 /* ":redir @" is not the start of a comment */ 3095 if (cmdidx == CMD_redir && p[0] == '@' && p[1] == '"') 3096 p += 2; 3097 while (*p) 3098 { 3099 if (*p == Ctrl_V) 3100 { 3101 if (p[1] != NUL) 3102 ++p; 3103 } 3104 else if ( (*p == '"' && !(argt & NOTRLCOM)) 3105 || *p == '|' || *p == '\n') 3106 { 3107 if (*(p - 1) != '\\') 3108 { 3109 if (*p == '|' || *p == '\n') 3110 return p + 1; 3111 return NULL; /* It's a comment */ 3112 } 3113 } 3114 #ifdef FEAT_MBYTE 3115 if (has_mbyte) 3116 p += (*mb_ptr2len_check)(p); 3117 else 3118 #endif 3119 ++p; 3120 } 3121 } 3122 3123 /* no arguments allowed */ 3124 if (!(argt & EXTRA) && *arg != NUL && 3125 vim_strchr((char_u *)"|\"", *arg) == NULL) 3126 return NULL; 3127 3128 /* Find start of last argument (argument just before cursor): */ 3129 p = buff + STRLEN(buff); 3130 while (p != arg && *p != ' ' && *p != TAB) 3131 p--; 3132 if (*p == ' ' || *p == TAB) 3133 p++; 3134 xp->xp_pattern = p; 3135 3136 if (argt & XFILE) 3137 { 3138 int in_quote = FALSE; 3139 char_u *bow = NULL; /* Beginning of word */ 3140 3141 /* 3142 * Allow spaces within back-quotes to count as part of the argument 3143 * being expanded. 3144 */ 3145 xp->xp_pattern = skipwhite(arg); 3146 for (p = xp->xp_pattern; *p; ) 3147 { 3148 if (*p == '\\' && p[1]) 3149 ++p; 3150 #ifdef SPACE_IN_FILENAME 3151 else if (vim_iswhite(*p) && (!(argt & NOSPC) || usefilter)) 3152 #else 3153 else if (vim_iswhite(*p)) 3154 #endif 3155 { 3156 p = skipwhite(p); 3157 if (in_quote) 3158 bow = p; 3159 else 3160 xp->xp_pattern = p; 3161 --p; 3162 } 3163 else if (*p == '`') 3164 { 3165 if (!in_quote) 3166 { 3167 xp->xp_pattern = p; 3168 bow = p + 1; 3169 } 3170 in_quote = !in_quote; 3171 } 3172 #ifdef FEAT_MBYTE 3173 if (has_mbyte) 3174 p += (*mb_ptr2len_check)(p); 3175 else 3176 #endif 3177 ++p; 3178 } 3179 3180 /* 3181 * If we are still inside the quotes, and we passed a space, just 3182 * expand from there. 3183 */ 3184 if (bow != NULL && in_quote) 3185 xp->xp_pattern = bow; 3186 xp->xp_context = EXPAND_FILES; 3187 3188 /* Check for environment variable */ 3189 if (*xp->xp_pattern == '$' 3190 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) 3191 || *xp->xp_pattern == '%' 3192 #endif 3193 ) 3194 { 3195 for (p = xp->xp_pattern + 1; *p != NUL; ++p) 3196 if (!vim_isIDc(*p)) 3197 break; 3198 if (*p == NUL) 3199 { 3200 xp->xp_context = EXPAND_ENV_VARS; 3201 ++xp->xp_pattern; 3202 } 3203 } 3204 } 3205 3206 /* 3207 * 6. switch on command name 3208 */ 3209 switch (cmdidx) 3210 { 3211 case CMD_cd: 3212 case CMD_chdir: 3213 case CMD_lcd: 3214 case CMD_lchdir: 3215 if (xp->xp_context == EXPAND_FILES) 3216 xp->xp_context = EXPAND_DIRECTORIES; 3217 break; 3218 case CMD_help: 3219 xp->xp_context = EXPAND_HELP; 3220 xp->xp_pattern = arg; 3221 break; 3222 3223 /* Command modifiers: return the argument. */ 3224 case CMD_aboveleft: 3225 case CMD_belowright: 3226 case CMD_botright: 3227 case CMD_browse: 3228 case CMD_confirm: 3229 case CMD_folddoclosed: 3230 case CMD_folddoopen: 3231 case CMD_hide: 3232 case CMD_keepjumps: 3233 case CMD_keepmarks: 3234 case CMD_leftabove: 3235 case CMD_lockmarks: 3236 case CMD_rightbelow: 3237 case CMD_silent: 3238 case CMD_topleft: 3239 case CMD_verbose: 3240 case CMD_vertical: 3241 return arg; 3242 3243 #ifdef FEAT_SEARCH_EXTRA 3244 case CMD_match: 3245 if (*arg == NUL || !ends_excmd(*arg)) 3246 { 3247 /* Dummy call to clear variables. */ 3248 set_context_in_highlight_cmd(xp, (char_u *)"link n"); 3249 xp->xp_context = EXPAND_HIGHLIGHT; 3250 xp->xp_pattern = arg; 3251 arg = skipwhite(skiptowhite(arg)); 3252 if (*arg != NUL) 3253 { 3254 xp->xp_context = EXPAND_NOTHING; 3255 arg = skip_regexp(arg + 1, *arg, p_magic, NULL); 3256 } 3257 } 3258 return find_nextcmd(arg); 3259 #endif 3260 3261 #ifdef FEAT_CMDL_COMPL 3262 /* 3263 * All completion for the +cmdline_compl feature goes here. 3264 */ 3265 3266 # ifdef FEAT_USR_CMDS 3267 case CMD_command: 3268 /* Check for attributes */ 3269 while (*arg == '-') 3270 { 3271 arg++; /* Skip "-" */ 3272 p = skiptowhite(arg); 3273 if (*p == NUL) 3274 { 3275 /* Cursor is still in the attribute */ 3276 p = vim_strchr(arg, '='); 3277 if (p == NULL) 3278 { 3279 /* No "=", so complete attribute names */ 3280 xp->xp_context = EXPAND_USER_CMD_FLAGS; 3281 xp->xp_pattern = arg; 3282 return NULL; 3283 } 3284 3285 /* For the -complete and -nargs attributes, we complete 3286 * their arguments as well. 3287 */ 3288 if (STRNICMP(arg, "complete", p - arg) == 0) 3289 { 3290 xp->xp_context = EXPAND_USER_COMPLETE; 3291 xp->xp_pattern = p + 1; 3292 return NULL; 3293 } 3294 else if (STRNICMP(arg, "nargs", p - arg) == 0) 3295 { 3296 xp->xp_context = EXPAND_USER_NARGS; 3297 xp->xp_pattern = p + 1; 3298 return NULL; 3299 } 3300 return NULL; 3301 } 3302 arg = skipwhite(p); 3303 } 3304 3305 /* After the attributes comes the new command name */ 3306 p = skiptowhite(arg); 3307 if (*p == NUL) 3308 { 3309 xp->xp_context = EXPAND_USER_COMMANDS; 3310 xp->xp_pattern = arg; 3311 break; 3312 } 3313 3314 /* And finally comes a normal command */ 3315 return skipwhite(p); 3316 3317 case CMD_delcommand: 3318 xp->xp_context = EXPAND_USER_COMMANDS; 3319 xp->xp_pattern = arg; 3320 break; 3321 # endif 3322 3323 case CMD_global: 3324 case CMD_vglobal: 3325 delim = *arg; /* get the delimiter */ 3326 if (delim) 3327 ++arg; /* skip delimiter if there is one */ 3328 3329 while (arg[0] != NUL && arg[0] != delim) 3330 { 3331 if (arg[0] == '\\' && arg[1] != NUL) 3332 ++arg; 3333 ++arg; 3334 } 3335 if (arg[0] != NUL) 3336 return arg + 1; 3337 break; 3338 case CMD_and: 3339 case CMD_substitute: 3340 delim = *arg; 3341 if (delim) 3342 { 3343 /* skip "from" part */ 3344 ++arg; 3345 arg = skip_regexp(arg, delim, p_magic, NULL); 3346 } 3347 /* skip "to" part */ 3348 while (arg[0] != NUL && arg[0] != delim) 3349 { 3350 if (arg[0] == '\\' && arg[1] != NUL) 3351 ++arg; 3352 ++arg; 3353 } 3354 if (arg[0] != NUL) /* skip delimiter */ 3355 ++arg; 3356 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL) 3357 ++arg; 3358 if (arg[0] != NUL) 3359 return arg; 3360 break; 3361 case CMD_isearch: 3362 case CMD_dsearch: 3363 case CMD_ilist: 3364 case CMD_dlist: 3365 case CMD_ijump: 3366 case CMD_psearch: 3367 case CMD_djump: 3368 case CMD_isplit: 3369 case CMD_dsplit: 3370 arg = skipwhite(skipdigits(arg)); /* skip count */ 3371 if (*arg == '/') /* Match regexp, not just whole words */ 3372 { 3373 for (++arg; *arg && *arg != '/'; arg++) 3374 if (*arg == '\\' && arg[1] != NUL) 3375 arg++; 3376 if (*arg) 3377 { 3378 arg = skipwhite(arg + 1); 3379 3380 /* Check for trailing illegal characters */ 3381 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL) 3382 xp->xp_context = EXPAND_NOTHING; 3383 else 3384 return arg; 3385 } 3386 } 3387 break; 3388 #ifdef FEAT_AUTOCMD 3389 case CMD_autocmd: 3390 return set_context_in_autocmd(xp, arg, FALSE); 3391 3392 case CMD_doautocmd: 3393 return set_context_in_autocmd(xp, arg, TRUE); 3394 #endif 3395 case CMD_set: 3396 set_context_in_set_cmd(xp, arg, 0); 3397 break; 3398 case CMD_setglobal: 3399 set_context_in_set_cmd(xp, arg, OPT_GLOBAL); 3400 break; 3401 case CMD_setlocal: 3402 set_context_in_set_cmd(xp, arg, OPT_LOCAL); 3403 break; 3404 case CMD_tag: 3405 case CMD_stag: 3406 case CMD_ptag: 3407 case CMD_tselect: 3408 case CMD_stselect: 3409 case CMD_ptselect: 3410 case CMD_tjump: 3411 case CMD_stjump: 3412 case CMD_ptjump: 3413 xp->xp_context = EXPAND_TAGS; 3414 xp->xp_pattern = arg; 3415 break; 3416 case CMD_augroup: 3417 xp->xp_context = EXPAND_AUGROUP; 3418 xp->xp_pattern = arg; 3419 break; 3420 #ifdef FEAT_SYN_HL 3421 case CMD_syntax: 3422 set_context_in_syntax_cmd(xp, arg); 3423 break; 3424 #endif 3425 #ifdef FEAT_EVAL 3426 case CMD_let: 3427 case CMD_if: 3428 case CMD_elseif: 3429 case CMD_while: 3430 case CMD_echo: 3431 case CMD_echon: 3432 case CMD_execute: 3433 case CMD_echomsg: 3434 case CMD_echoerr: 3435 case CMD_call: 3436 case CMD_return: 3437 set_context_for_expression(xp, arg, cmdidx); 3438 break; 3439 3440 case CMD_unlet: 3441 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) 3442 arg = xp->xp_pattern + 1; 3443 xp->xp_context = EXPAND_USER_VARS; 3444 xp->xp_pattern = arg; 3445 break; 3446 3447 case CMD_function: 3448 case CMD_delfunction: 3449 xp->xp_context = EXPAND_USER_FUNC; 3450 xp->xp_pattern = arg; 3451 break; 3452 3453 case CMD_echohl: 3454 xp->xp_context = EXPAND_HIGHLIGHT; 3455 xp->xp_pattern = arg; 3456 break; 3457 #endif 3458 case CMD_highlight: 3459 set_context_in_highlight_cmd(xp, arg); 3460 break; 3461 #ifdef FEAT_LISTCMDS 3462 case CMD_bdelete: 3463 case CMD_bwipeout: 3464 case CMD_bunload: 3465 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) 3466 arg = xp->xp_pattern + 1; 3467 /*FALLTHROUGH*/ 3468 case CMD_buffer: 3469 case CMD_sbuffer: 3470 case CMD_checktime: 3471 xp->xp_context = EXPAND_BUFFERS; 3472 xp->xp_pattern = arg; 3473 break; 3474 #endif 3475 #ifdef FEAT_USR_CMDS 3476 case CMD_USER: 3477 case CMD_USER_BUF: 3478 if (compl != EXPAND_NOTHING) 3479 { 3480 /* XFILE: file names are handled above */ 3481 if (!(argt & XFILE)) 3482 { 3483 # ifdef FEAT_MENU 3484 if (compl == EXPAND_MENUS) 3485 return set_context_in_menu_cmd(xp, cmd, arg, forceit); 3486 # endif 3487 if (compl == EXPAND_COMMANDS) 3488 return arg; 3489 if (compl == EXPAND_MAPPINGS) 3490 return set_context_in_map_cmd(xp, (char_u *)"map", 3491 arg, forceit, FALSE, FALSE, CMD_map); 3492 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) 3493 arg = xp->xp_pattern + 1; 3494 xp->xp_pattern = arg; 3495 } 3496 xp->xp_context = compl; 3497 } 3498 break; 3499 #endif 3500 case CMD_map: case CMD_noremap: 3501 case CMD_nmap: case CMD_nnoremap: 3502 case CMD_vmap: case CMD_vnoremap: 3503 case CMD_omap: case CMD_onoremap: 3504 case CMD_imap: case CMD_inoremap: 3505 case CMD_cmap: case CMD_cnoremap: 3506 return set_context_in_map_cmd(xp, cmd, arg, forceit, 3507 FALSE, FALSE, cmdidx); 3508 case CMD_unmap: 3509 case CMD_nunmap: 3510 case CMD_vunmap: 3511 case CMD_ounmap: 3512 case CMD_iunmap: 3513 case CMD_cunmap: 3514 return set_context_in_map_cmd(xp, cmd, arg, forceit, 3515 FALSE, TRUE, cmdidx); 3516 case CMD_abbreviate: case CMD_noreabbrev: 3517 case CMD_cabbrev: case CMD_cnoreabbrev: 3518 case CMD_iabbrev: case CMD_inoreabbrev: 3519 return set_context_in_map_cmd(xp, cmd, arg, forceit, 3520 TRUE, FALSE, cmdidx); 3521 case CMD_unabbreviate: 3522 case CMD_cunabbrev: 3523 case CMD_iunabbrev: 3524 return set_context_in_map_cmd(xp, cmd, arg, forceit, 3525 TRUE, TRUE, cmdidx); 3526 #ifdef FEAT_MENU 3527 case CMD_menu: case CMD_noremenu: case CMD_unmenu: 3528 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu: 3529 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu: 3530 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu: 3531 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu: 3532 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu: 3533 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu: 3534 case CMD_tmenu: case CMD_tunmenu: 3535 case CMD_popup: case CMD_tearoff: case CMD_emenu: 3536 return set_context_in_menu_cmd(xp, cmd, arg, forceit); 3537 #endif 3538 3539 case CMD_colorscheme: 3540 xp->xp_context = EXPAND_COLORS; 3541 xp->xp_pattern = arg; 3542 break; 3543 3544 case CMD_compiler: 3545 xp->xp_context = EXPAND_COMPILER; 3546 xp->xp_pattern = arg; 3547 break; 3548 3549 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ 3550 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) 3551 case CMD_language: 3552 if (*skiptowhite(arg) == NUL) 3553 { 3554 xp->xp_context = EXPAND_LANGUAGE; 3555 xp->xp_pattern = arg; 3556 } 3557 else 3558 xp->xp_context = EXPAND_NOTHING; 3559 break; 3560 #endif 3561 3562 #endif /* FEAT_CMDL_COMPL */ 3563 3564 default: 3565 break; 3566 } 3567 return NULL; 3568 } 3569 3570 /* 3571 * skip a range specifier of the form: addr [,addr] [;addr] .. 3572 * 3573 * Backslashed delimiters after / or ? will be skipped, and commands will 3574 * not be expanded between /'s and ?'s or after "'". 3575 * 3576 * Returns the "cmd" pointer advanced to beyond the range. 3577 */ 3578 char_u * 3579 skip_range(cmd, ctx) 3580 char_u *cmd; 3581 int *ctx; /* pointer to xp_context or NULL */ 3582 { 3583 int delim; 3584 3585 while (*cmd != NUL && (vim_isspace(*cmd) || VIM_ISDIGIT(*cmd) || 3586 vim_strchr((char_u *)".$%'/?-+,;", *cmd) != NULL)) 3587 { 3588 if (*cmd == '\'') 3589 { 3590 if (*++cmd == NUL && ctx != NULL) 3591 *ctx = EXPAND_NOTHING; 3592 } 3593 else if (*cmd == '/' || *cmd == '?') 3594 { 3595 delim = *cmd++; 3596 while (*cmd != NUL && *cmd != delim) 3597 if (*cmd++ == '\\' && *cmd != NUL) 3598 ++cmd; 3599 if (*cmd == NUL && ctx != NULL) 3600 *ctx = EXPAND_NOTHING; 3601 } 3602 if (*cmd != NUL) 3603 ++cmd; 3604 } 3605 return cmd; 3606 } 3607 3608 /* 3609 * get a single EX address 3610 * 3611 * Set ptr to the next character after the part that was interpreted. 3612 * Set ptr to NULL when an error is encountered. 3613 * 3614 * Return MAXLNUM when no Ex address was found. 3615 */ 3616 static linenr_T 3617 get_address(ptr, skip, to_other_file) 3618 char_u **ptr; 3619 int skip; /* only skip the address, don't use it */ 3620 int to_other_file; /* flag: may jump to other file */ 3621 { 3622 int c; 3623 int i; 3624 long n; 3625 char_u *cmd; 3626 pos_T pos; 3627 pos_T *fp; 3628 linenr_T lnum; 3629 3630 cmd = skipwhite(*ptr); 3631 lnum = MAXLNUM; 3632 do 3633 { 3634 switch (*cmd) 3635 { 3636 case '.': /* '.' - Cursor position */ 3637 ++cmd; 3638 lnum = curwin->w_cursor.lnum; 3639 break; 3640 3641 case '$': /* '$' - last line */ 3642 ++cmd; 3643 lnum = curbuf->b_ml.ml_line_count; 3644 break; 3645 3646 case '\'': /* ''' - mark */ 3647 if (*++cmd == NUL) 3648 { 3649 cmd = NULL; 3650 goto error; 3651 } 3652 if (skip) 3653 ++cmd; 3654 else 3655 { 3656 /* Only accept a mark in another file when it is 3657 * used by itself: ":'M". */ 3658 fp = getmark(*cmd, to_other_file && cmd[1] == NUL); 3659 ++cmd; 3660 if (fp == (pos_T *)-1) 3661 /* Jumped to another file. */ 3662 lnum = curwin->w_cursor.lnum; 3663 else 3664 { 3665 if (check_mark(fp) == FAIL) 3666 { 3667 cmd = NULL; 3668 goto error; 3669 } 3670 lnum = fp->lnum; 3671 } 3672 } 3673 break; 3674 3675 case '/': 3676 case '?': /* '/' or '?' - search */ 3677 c = *cmd++; 3678 if (skip) /* skip "/pat/" */ 3679 { 3680 cmd = skip_regexp(cmd, c, (int)p_magic, NULL); 3681 if (*cmd == c) 3682 ++cmd; 3683 } 3684 else 3685 { 3686 pos = curwin->w_cursor; /* save curwin->w_cursor */ 3687 /* 3688 * When '/' or '?' follows another address, start 3689 * from there. 3690 */ 3691 if (lnum != MAXLNUM) 3692 curwin->w_cursor.lnum = lnum; 3693 /* 3694 * Start a forward search at the end of the line. 3695 * Start a backward search at the start of the line. 3696 * This makes sure we never match in the current 3697 * line, and can match anywhere in the 3698 * next/previous line. 3699 */ 3700 if (c == '/') 3701 curwin->w_cursor.col = MAXCOL; 3702 else 3703 curwin->w_cursor.col = 0; 3704 searchcmdlen = 0; 3705 if (!do_search(NULL, c, cmd, 1L, 3706 SEARCH_HIS + SEARCH_MSG + SEARCH_START)) 3707 { 3708 curwin->w_cursor = pos; 3709 cmd = NULL; 3710 goto error; 3711 } 3712 lnum = curwin->w_cursor.lnum; 3713 curwin->w_cursor = pos; 3714 /* adjust command string pointer */ 3715 cmd += searchcmdlen; 3716 } 3717 break; 3718 3719 case '\\': /* "\?", "\/" or "\&", repeat search */ 3720 ++cmd; 3721 if (*cmd == '&') 3722 i = RE_SUBST; 3723 else if (*cmd == '?' || *cmd == '/') 3724 i = RE_SEARCH; 3725 else 3726 { 3727 EMSG(_(e_backslash)); 3728 cmd = NULL; 3729 goto error; 3730 } 3731 3732 if (!skip) 3733 { 3734 /* 3735 * When search follows another address, start from 3736 * there. 3737 */ 3738 if (lnum != MAXLNUM) 3739 pos.lnum = lnum; 3740 else 3741 pos.lnum = curwin->w_cursor.lnum; 3742 3743 /* 3744 * Start the search just like for the above 3745 * do_search(). 3746 */ 3747 if (*cmd != '?') 3748 pos.col = MAXCOL; 3749 else 3750 pos.col = 0; 3751 if (searchit(curwin, curbuf, &pos, 3752 *cmd == '?' ? BACKWARD : FORWARD, 3753 (char_u *)"", 1L, 3754 SEARCH_MSG + SEARCH_START, i) != FAIL) 3755 lnum = pos.lnum; 3756 else 3757 { 3758 cmd = NULL; 3759 goto error; 3760 } 3761 } 3762 ++cmd; 3763 break; 3764 3765 default: 3766 if (VIM_ISDIGIT(*cmd)) /* absolute line number */ 3767 lnum = getdigits(&cmd); 3768 } 3769 3770 for (;;) 3771 { 3772 cmd = skipwhite(cmd); 3773 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd)) 3774 break; 3775 3776 if (lnum == MAXLNUM) 3777 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */ 3778 if (VIM_ISDIGIT(*cmd)) 3779 i = '+'; /* "number" is same as "+number" */ 3780 else 3781 i = *cmd++; 3782 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */ 3783 n = 1; 3784 else 3785 n = getdigits(&cmd); 3786 if (i == '-') 3787 lnum -= n; 3788 else 3789 lnum += n; 3790 } 3791 } while (*cmd == '/' || *cmd == '?'); 3792 3793 error: 3794 *ptr = cmd; 3795 return lnum; 3796 } 3797 3798 /* 3799 * Function called for command which is Not Implemented. NI! 3800 */ 3801 void 3802 ex_ni(eap) 3803 exarg_T *eap; 3804 { 3805 if (!eap->skip) 3806 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version"); 3807 } 3808 3809 #if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \ 3810 || !defined(FEAT_RUBY) 3811 /* 3812 * Function called for script command which is Not Implemented. NI! 3813 * Skips over ":perl <<EOF" constructs. 3814 */ 3815 static void 3816 ex_script_ni(eap) 3817 exarg_T *eap; 3818 { 3819 if (!eap->skip) 3820 ex_ni(eap); 3821 else 3822 vim_free(script_get(eap, eap->arg)); 3823 } 3824 #endif 3825 3826 /* 3827 * Check range in Ex command for validity. 3828 * Return NULL when valid, error message when invalid. 3829 */ 3830 static char_u * 3831 invalid_range(eap) 3832 exarg_T *eap; 3833 { 3834 if ( eap->line1 < 0 3835 || eap->line2 < 0 3836 || eap->line1 > eap->line2 3837 || ((eap->argt & RANGE) 3838 && !(eap->argt & NOTADR) 3839 && eap->line2 > curbuf->b_ml.ml_line_count 3840 #ifdef FEAT_DIFF 3841 + (eap->cmdidx == CMD_diffget) 3842 #endif 3843 )) 3844 return (char_u *)_(e_invrange); 3845 return NULL; 3846 } 3847 3848 /* 3849 * Correct the range for zero line number, if required. 3850 */ 3851 static void 3852 correct_range(eap) 3853 exarg_T *eap; 3854 { 3855 if (!(eap->argt & ZEROR)) /* zero in range not allowed */ 3856 { 3857 if (eap->line1 == 0) 3858 eap->line1 = 1; 3859 if (eap->line2 == 0) 3860 eap->line2 = 1; 3861 } 3862 } 3863 3864 /* 3865 * Expand file name in Ex command argument. 3866 * Return FAIL for failure, OK otherwise. 3867 */ 3868 int 3869 expand_filename(eap, cmdlinep, errormsgp) 3870 exarg_T *eap; 3871 char_u **cmdlinep; 3872 char_u **errormsgp; 3873 { 3874 int has_wildcards; /* need to expand wildcards */ 3875 char_u *repl; 3876 int srclen; 3877 char_u *p; 3878 int n; 3879 3880 /* 3881 * Decide to expand wildcards *before* replacing '%', '#', etc. If 3882 * the file name contains a wildcard it should not cause expanding. 3883 * (it will be expanded anyway if there is a wildcard before replacing). 3884 */ 3885 has_wildcards = mch_has_wildcard(eap->arg); 3886 for (p = eap->arg; *p; ) 3887 { 3888 /* 3889 * Quick check if this cannot be the start of a special string. 3890 * Also removes backslash before '%', '#' and '<'. 3891 */ 3892 if (vim_strchr((char_u *)"%#<", *p) == NULL) 3893 { 3894 ++p; 3895 continue; 3896 } 3897 3898 /* 3899 * Try to find a match at this position. 3900 */ 3901 repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg); 3902 if (*errormsgp != NULL) /* error detected */ 3903 return FAIL; 3904 if (repl == NULL) /* no match found */ 3905 { 3906 p += srclen; 3907 continue; 3908 } 3909 3910 /* Need to escape white space et al. with a backslash. Don't do this 3911 * for shell commands (may have to use quotes instead). Don't do this 3912 * for non-unix systems when there is a single argument (spaces don't 3913 * separate arguments then). */ 3914 if (!eap->usefilter 3915 && eap->cmdidx != CMD_bang 3916 && eap->cmdidx != CMD_make 3917 && eap->cmdidx != CMD_grep 3918 && eap->cmdidx != CMD_grepadd 3919 #ifndef UNIX 3920 && !(eap->argt & NOSPC) 3921 #endif 3922 ) 3923 { 3924 char_u *l; 3925 #ifdef BACKSLASH_IN_FILENAME 3926 /* Don't escape a backslash here, because rem_backslash() doesn't 3927 * remove it later. */ 3928 static char_u *nobslash = (char_u *)" \t\"|"; 3929 # define ESCAPE_CHARS nobslash 3930 #else 3931 # define ESCAPE_CHARS escape_chars 3932 #endif 3933 3934 for (l = repl; *l; ++l) 3935 if (vim_strchr(ESCAPE_CHARS, *l) != NULL) 3936 { 3937 l = vim_strsave_escaped(repl, ESCAPE_CHARS); 3938 if (l != NULL) 3939 { 3940 vim_free(repl); 3941 repl = l; 3942 } 3943 break; 3944 } 3945 } 3946 3947 /* For a shell command a '!' must be escaped. */ 3948 if ((eap->usefilter || eap->cmdidx == CMD_bang) 3949 && vim_strchr(repl, '!') != NULL) 3950 { 3951 char_u *l; 3952 3953 l = vim_strsave_escaped(repl, (char_u *)"!"); 3954 if (l != NULL) 3955 { 3956 vim_free(repl); 3957 repl = l; 3958 /* For a sh-like shell escape it another time. */ 3959 if (strstr((char *)p_sh, "sh") != NULL) 3960 { 3961 l = vim_strsave_escaped(repl, (char_u *)"!"); 3962 if (l != NULL) 3963 { 3964 vim_free(repl); 3965 repl = l; 3966 } 3967 } 3968 } 3969 } 3970 3971 p = repl_cmdline(eap, p, srclen, repl, cmdlinep); 3972 vim_free(repl); 3973 if (p == NULL) 3974 return FAIL; 3975 } 3976 3977 /* 3978 * One file argument: Expand wildcards. 3979 * Don't do this with ":r !command" or ":w !command". 3980 */ 3981 if ((eap->argt & NOSPC) && !eap->usefilter) 3982 { 3983 /* 3984 * May do this twice: 3985 * 1. Replace environment variables. 3986 * 2. Replace any other wildcards, remove backslashes. 3987 */ 3988 for (n = 1; n <= 2; ++n) 3989 { 3990 if (n == 2) 3991 { 3992 #ifdef UNIX 3993 /* 3994 * Only for Unix we check for more than one file name. 3995 * For other systems spaces are considered to be part 3996 * of the file name. 3997 * Only check here if there is no wildcard, otherwise 3998 * ExpandOne() will check for errors. This allows 3999 * ":e `ls ve*.c`" on Unix. 4000 */ 4001 if (!has_wildcards) 4002 for (p = eap->arg; *p; ++p) 4003 { 4004 /* skip escaped characters */ 4005 if (p[1] && (*p == '\\' || *p == Ctrl_V)) 4006 ++p; 4007 else if (vim_iswhite(*p)) 4008 { 4009 *errormsgp = (char_u *)_("E172: Only one file name allowed"); 4010 return FAIL; 4011 } 4012 } 4013 #endif 4014 4015 /* 4016 * Halve the number of backslashes (this is Vi compatible). 4017 * For Unix and OS/2, when wildcards are expanded, this is 4018 * done by ExpandOne() below. 4019 */ 4020 #if defined(UNIX) || defined(OS2) 4021 if (!has_wildcards) 4022 #endif 4023 backslash_halve(eap->arg); 4024 #ifdef MACOS_CLASSIC 4025 /* 4026 * translate unix-like path components 4027 */ 4028 slash_n_colon_adjust(eap->arg); 4029 #endif 4030 } 4031 4032 if (has_wildcards) 4033 { 4034 if (n == 1) 4035 { 4036 /* 4037 * First loop: May expand environment variables. This 4038 * can be done much faster with expand_env() than with 4039 * something else (e.g., calling a shell). 4040 * After expanding environment variables, check again 4041 * if there are still wildcards present. 4042 */ 4043 if (vim_strchr(eap->arg, '$') != NULL 4044 || vim_strchr(eap->arg, '~') != NULL) 4045 { 4046 expand_env_esc(eap->arg, NameBuff, MAXPATHL, TRUE); 4047 has_wildcards = mch_has_wildcard(NameBuff); 4048 p = NameBuff; 4049 } 4050 else 4051 p = NULL; 4052 } 4053 else /* n == 2 */ 4054 { 4055 expand_T xpc; 4056 4057 ExpandInit(&xpc); 4058 xpc.xp_context = EXPAND_FILES; 4059 p = ExpandOne(&xpc, eap->arg, NULL, 4060 WILD_LIST_NOTFOUND|WILD_ADD_SLASH, 4061 WILD_EXPAND_FREE); 4062 ExpandCleanup(&xpc); 4063 if (p == NULL) 4064 return FAIL; 4065 } 4066 if (p != NULL) 4067 { 4068 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg), 4069 p, cmdlinep); 4070 if (n == 2) /* p came from ExpandOne() */ 4071 vim_free(p); 4072 } 4073 } 4074 } 4075 } 4076 return OK; 4077 } 4078 4079 /* 4080 * Replace part of the command line, keeping eap->cmd, eap->arg and 4081 * eap->nextcmd correct. 4082 * "src" points to the part that is to be replaced, of length "srclen". 4083 * "repl" is the replacement string. 4084 * Returns a pointer to the character after the replaced string. 4085 * Returns NULL for failure. 4086 */ 4087 static char_u * 4088 repl_cmdline(eap, src, srclen, repl, cmdlinep) 4089 exarg_T *eap; 4090 char_u *src; 4091 int srclen; 4092 char_u *repl; 4093 char_u **cmdlinep; 4094 { 4095 int len; 4096 int i; 4097 char_u *new_cmdline; 4098 4099 /* 4100 * The new command line is build in new_cmdline[]. 4101 * First allocate it. 4102 * Careful: a "+cmd" argument may have been NUL terminated. 4103 */ 4104 len = (int)STRLEN(repl); 4105 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3; 4106 if (eap->nextcmd) 4107 i += (int)STRLEN(eap->nextcmd);/* add space for next command */ 4108 if ((new_cmdline = alloc((unsigned)i)) == NULL) 4109 return NULL; /* out of memory! */ 4110 4111 /* 4112 * Copy the stuff before the expanded part. 4113 * Copy the expanded stuff. 4114 * Copy what came after the expanded part. 4115 * Copy the next commands, if there are any. 4116 */ 4117 i = (int)(src - *cmdlinep); /* length of part before match */ 4118 mch_memmove(new_cmdline, *cmdlinep, (size_t)i); 4119 mch_memmove(new_cmdline + i, repl, (size_t)len); 4120 i += len; /* remember the end of the string */ 4121 STRCPY(new_cmdline + i, src + srclen); 4122 src = new_cmdline + i; /* remember where to continue */ 4123 4124 if (eap->nextcmd) /* append next command */ 4125 { 4126 i = (int)STRLEN(new_cmdline) + 1; 4127 STRCPY(new_cmdline + i, eap->nextcmd); 4128 eap->nextcmd = new_cmdline + i; 4129 } 4130 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep); 4131 eap->arg = new_cmdline + (eap->arg - *cmdlinep); 4132 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command) 4133 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep); 4134 vim_free(*cmdlinep); 4135 *cmdlinep = new_cmdline; 4136 4137 return src; 4138 } 4139 4140 /* 4141 * Check for '|' to separate commands and '"' to start comments. 4142 */ 4143 void 4144 separate_nextcmd(eap) 4145 exarg_T *eap; 4146 { 4147 char_u *p; 4148 4149 for (p = eap->arg; *p; ++p) 4150 { 4151 if (*p == Ctrl_V) 4152 { 4153 if (eap->argt & (USECTRLV | XFILE)) 4154 ++p; /* skip CTRL-V and next char */ 4155 else 4156 STRCPY(p, p + 1); /* remove CTRL-V and skip next char */ 4157 if (*p == NUL) /* stop at NUL after CTRL-V */ 4158 break; 4159 } 4160 /* Check for '"': start of comment or '|': next command */ 4161 /* :@" and :*" do not start a comment! 4162 * :redir @" doesn't either. */ 4163 else if ((*p == '"' && !(eap->argt & NOTRLCOM) 4164 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star) 4165 || p != eap->arg) 4166 && (eap->cmdidx != CMD_redir 4167 || p != eap->arg + 1 || p[-1] != '@')) 4168 || *p == '|' || *p == '\n') 4169 { 4170 /* 4171 * We remove the '\' before the '|', unless USECTRLV is used 4172 * AND 'b' is present in 'cpoptions'. 4173 */ 4174 if ((vim_strchr(p_cpo, CPO_BAR) == NULL 4175 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\') 4176 { 4177 mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */ 4178 --p; 4179 } 4180 else 4181 { 4182 eap->nextcmd = check_nextcmd(p); 4183 *p = NUL; 4184 break; 4185 } 4186 } 4187 #ifdef FEAT_MBYTE 4188 else if (has_mbyte) 4189 p += (*mb_ptr2len_check)(p) - 1; /* skip bytes of multi-byte char */ 4190 #endif 4191 } 4192 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */ 4193 del_trailing_spaces(eap->arg); 4194 } 4195 4196 /* 4197 * get + command from ex argument 4198 */ 4199 static char_u * 4200 getargcmd(argp) 4201 char_u **argp; 4202 { 4203 char_u *arg = *argp; 4204 char_u *command = NULL; 4205 4206 if (*arg == '+') /* +[command] */ 4207 { 4208 ++arg; 4209 if (vim_isspace(*arg)) 4210 command = dollar_command; 4211 else 4212 { 4213 command = arg; 4214 arg = skip_cmd_arg(command, TRUE); 4215 if (*arg != NUL) 4216 *arg++ = NUL; /* terminate command with NUL */ 4217 } 4218 4219 arg = skipwhite(arg); /* skip over spaces */ 4220 *argp = arg; 4221 } 4222 return command; 4223 } 4224 4225 /* 4226 * Find end of "+command" argument. Skip over "\ " and "\\". 4227 */ 4228 static char_u * 4229 skip_cmd_arg(p, rembs) 4230 char_u *p; 4231 int rembs; /* TRUE to halve the number of backslashes */ 4232 { 4233 while (*p && !vim_isspace(*p)) 4234 { 4235 if (*p == '\\' && p[1] != NUL) 4236 { 4237 if (rembs) 4238 mch_memmove(p, p + 1, STRLEN(p)); 4239 else 4240 ++p; 4241 } 4242 #ifdef FEAT_MBYTE 4243 if (has_mbyte) 4244 p += (*mb_ptr2len_check)(p); 4245 else 4246 #endif 4247 ++p; 4248 } 4249 return p; 4250 } 4251 4252 /* 4253 * Get "++opt=arg" argument. 4254 * Return FAIL or OK. 4255 */ 4256 static int 4257 getargopt(eap) 4258 exarg_T *eap; 4259 { 4260 char_u *arg = eap->arg + 2; 4261 int *pp = NULL; 4262 #ifdef FEAT_MBYTE 4263 char_u *p; 4264 #endif 4265 4266 /* ":edit ++[no]bin[ary] file" */ 4267 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0) 4268 { 4269 if (*arg == 'n') 4270 { 4271 arg += 2; 4272 eap->force_bin = FORCE_NOBIN; 4273 } 4274 else 4275 eap->force_bin = FORCE_BIN; 4276 if (!checkforcmd(&arg, "binary", 3)) 4277 return FAIL; 4278 eap->arg = skipwhite(arg); 4279 return OK; 4280 } 4281 4282 if (STRNCMP(arg, "ff", 2) == 0) 4283 { 4284 arg += 2; 4285 pp = &eap->force_ff; 4286 } 4287 else if (STRNCMP(arg, "fileformat", 10) == 0) 4288 { 4289 arg += 10; 4290 pp = &eap->force_ff; 4291 } 4292 #ifdef FEAT_MBYTE 4293 else if (STRNCMP(arg, "enc", 3) == 0) 4294 { 4295 arg += 3; 4296 pp = &eap->force_enc; 4297 } 4298 else if (STRNCMP(arg, "encoding", 8) == 0) 4299 { 4300 arg += 8; 4301 pp = &eap->force_enc; 4302 } 4303 #endif 4304 4305 if (pp == NULL || *arg != '=') 4306 return FAIL; 4307 4308 ++arg; 4309 *pp = (int)(arg - eap->cmd); 4310 arg = skip_cmd_arg(arg, FALSE); 4311 eap->arg = skipwhite(arg); 4312 *arg = NUL; 4313 4314 #ifdef FEAT_MBYTE 4315 if (pp == &eap->force_ff) 4316 { 4317 #endif 4318 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL) 4319 return FAIL; 4320 #ifdef FEAT_MBYTE 4321 } 4322 else 4323 { 4324 /* Make 'fileencoding' lower case. */ 4325 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p) 4326 *p = TOLOWER_ASC(*p); 4327 } 4328 #endif 4329 4330 return OK; 4331 } 4332 4333 /* 4334 * ":abbreviate" and friends. 4335 */ 4336 static void 4337 ex_abbreviate(eap) 4338 exarg_T *eap; 4339 { 4340 do_exmap(eap, TRUE); /* almost the same as mapping */ 4341 } 4342 4343 /* 4344 * ":map" and friends. 4345 */ 4346 static void 4347 ex_map(eap) 4348 exarg_T *eap; 4349 { 4350 /* 4351 * If we are sourcing .exrc or .vimrc in current directory we 4352 * print the mappings for security reasons. 4353 */ 4354 if (secure) 4355 { 4356 secure = 2; 4357 msg_outtrans(eap->cmd); 4358 msg_putchar('\n'); 4359 } 4360 do_exmap(eap, FALSE); 4361 } 4362 4363 /* 4364 * ":unmap" and friends. 4365 */ 4366 static void 4367 ex_unmap(eap) 4368 exarg_T *eap; 4369 { 4370 do_exmap(eap, FALSE); 4371 } 4372 4373 /* 4374 * ":mapclear" and friends. 4375 */ 4376 static void 4377 ex_mapclear(eap) 4378 exarg_T *eap; 4379 { 4380 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE); 4381 } 4382 4383 /* 4384 * ":abclear" and friends. 4385 */ 4386 static void 4387 ex_abclear(eap) 4388 exarg_T *eap; 4389 { 4390 map_clear(eap->cmd, eap->arg, TRUE, TRUE); 4391 } 4392 4393 #ifdef FEAT_AUTOCMD 4394 static void 4395 ex_autocmd(eap) 4396 exarg_T *eap; 4397 { 4398 /* 4399 * Disallow auto commands from .exrc and .vimrc in current 4400 * directory for security reasons. 4401 */ 4402 if (secure) 4403 { 4404 secure = 2; 4405 eap->errmsg = e_curdir; 4406 } 4407 else if (eap->cmdidx == CMD_autocmd) 4408 do_autocmd(eap->arg, eap->forceit); 4409 else 4410 do_augroup(eap->arg, eap->forceit); 4411 } 4412 4413 /* 4414 * ":doautocmd": Apply the automatic commands to the current buffer. 4415 */ 4416 static void 4417 ex_doautocmd(eap) 4418 exarg_T *eap; 4419 { 4420 (void)do_doautocmd(eap->arg, TRUE); 4421 do_modelines(); 4422 } 4423 #endif 4424 4425 #ifdef FEAT_LISTCMDS 4426 /* 4427 * :[N]bunload[!] [N] [bufname] unload buffer 4428 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list 4429 * :[N]bwipeout[!] [N] [bufname] delete buffer really 4430 */ 4431 static void 4432 ex_bunload(eap) 4433 exarg_T *eap; 4434 { 4435 eap->errmsg = do_bufdel( 4436 eap->cmdidx == CMD_bdelete ? DOBUF_DEL 4437 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE 4438 : DOBUF_UNLOAD, eap->arg, 4439 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit); 4440 } 4441 4442 /* 4443 * :[N]buffer [N] to buffer N 4444 * :[N]sbuffer [N] to buffer N 4445 */ 4446 static void 4447 ex_buffer(eap) 4448 exarg_T *eap; 4449 { 4450 if (*eap->arg) 4451 eap->errmsg = e_trailing; 4452 else 4453 { 4454 if (eap->addr_count == 0) /* default is current buffer */ 4455 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0); 4456 else 4457 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2); 4458 } 4459 } 4460 4461 /* 4462 * :[N]bmodified [N] to next mod. buffer 4463 * :[N]sbmodified [N] to next mod. buffer 4464 */ 4465 static void 4466 ex_bmodified(eap) 4467 exarg_T *eap; 4468 { 4469 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2); 4470 } 4471 4472 /* 4473 * :[N]bnext [N] to next buffer 4474 * :[N]sbnext [N] split and to next buffer 4475 */ 4476 static void 4477 ex_bnext(eap) 4478 exarg_T *eap; 4479 { 4480 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2); 4481 } 4482 4483 /* 4484 * :[N]bNext [N] to previous buffer 4485 * :[N]bprevious [N] to previous buffer 4486 * :[N]sbNext [N] split and to previous buffer 4487 * :[N]sbprevious [N] split and to previous buffer 4488 */ 4489 static void 4490 ex_bprevious(eap) 4491 exarg_T *eap; 4492 { 4493 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2); 4494 } 4495 4496 /* 4497 * :brewind to first buffer 4498 * :bfirst to first buffer 4499 * :sbrewind split and to first buffer 4500 * :sbfirst split and to first buffer 4501 */ 4502 static void 4503 ex_brewind(eap) 4504 exarg_T *eap; 4505 { 4506 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0); 4507 } 4508 4509 /* 4510 * :blast to last buffer 4511 * :sblast split and to last buffer 4512 */ 4513 static void 4514 ex_blast(eap) 4515 exarg_T *eap; 4516 { 4517 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0); 4518 } 4519 #endif 4520 4521 int 4522 ends_excmd(c) 4523 int c; 4524 { 4525 return (c == NUL || c == '|' || c == '"' || c == '\n'); 4526 } 4527 4528 #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \ 4529 || defined(PROTO) 4530 /* 4531 * Return the next command, after the first '|' or '\n'. 4532 * Return NULL if not found. 4533 */ 4534 char_u * 4535 find_nextcmd(p) 4536 char_u *p; 4537 { 4538 while (*p != '|' && *p != '\n') 4539 { 4540 if (*p == NUL) 4541 return NULL; 4542 ++p; 4543 } 4544 return (p + 1); 4545 } 4546 #endif 4547 4548 /* 4549 * Check if *p is a separator between Ex commands. 4550 * Return NULL if it isn't, (p + 1) if it is. 4551 */ 4552 char_u * 4553 check_nextcmd(p) 4554 char_u *p; 4555 { 4556 p = skipwhite(p); 4557 if (*p == '|' || *p == '\n') 4558 return (p + 1); 4559 else 4560 return NULL; 4561 } 4562 4563 /* 4564 * - if there are more files to edit 4565 * - and this is the last window 4566 * - and forceit not used 4567 * - and not repeated twice on a row 4568 * return FAIL and give error message if 'message' TRUE 4569 * return OK otherwise 4570 */ 4571 static int 4572 check_more(message, forceit) 4573 int message; /* when FALSE check only, no messages */ 4574 int forceit; 4575 { 4576 int n = ARGCOUNT - curwin->w_arg_idx - 1; 4577 4578 if (!forceit && only_one_window() && ARGCOUNT > 1 && !arg_had_last 4579 && n >= 0 && quitmore == 0) 4580 { 4581 if (message) 4582 { 4583 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 4584 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL) 4585 { 4586 char_u buff[IOSIZE]; 4587 4588 if (n == 1) 4589 STRCPY(buff, _("1 more file to edit. Quit anyway?")); 4590 else 4591 sprintf((char *)buff, 4592 _("%d more files to edit. Quit anyway?"), n); 4593 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES) 4594 return OK; 4595 return FAIL; 4596 } 4597 #endif 4598 if (n == 1) 4599 EMSG(_("E173: 1 more file to edit")); 4600 else 4601 EMSGN(_("E173: %ld more files to edit"), n); 4602 quitmore = 2; /* next try to quit is allowed */ 4603 } 4604 return FAIL; 4605 } 4606 return OK; 4607 } 4608 4609 #ifdef FEAT_CMDL_COMPL 4610 /* 4611 * Function given to ExpandGeneric() to obtain the list of command names. 4612 */ 4613 /*ARGSUSED*/ 4614 char_u * 4615 get_command_name(xp, idx) 4616 expand_T *xp; 4617 int idx; 4618 { 4619 if (idx >= (int)CMD_SIZE) 4620 # ifdef FEAT_USR_CMDS 4621 return get_user_command_name(idx); 4622 # else 4623 return NULL; 4624 # endif 4625 return cmdnames[idx].cmd_name; 4626 } 4627 #endif 4628 4629 #if defined(FEAT_USR_CMDS) || defined(PROTO) 4630 static int uc_add_command __ARGS((char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int force)); 4631 static void uc_list __ARGS((char_u *name, size_t name_len)); 4632 static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg)); 4633 static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp)); 4634 static size_t uc_check_code __ARGS((char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len)); 4635 4636 static int 4637 uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force) 4638 char_u *name; 4639 size_t name_len; 4640 char_u *rep; 4641 long argt; 4642 long def; 4643 int flags; 4644 int compl; 4645 char_u *compl_arg; 4646 int force; 4647 { 4648 ucmd_T *cmd = NULL; 4649 char_u *p; 4650 int i; 4651 int cmp = 1; 4652 char_u *rep_buf = NULL; 4653 garray_T *gap; 4654 4655 replace_termcodes(rep, &rep_buf, FALSE, FALSE); 4656 if (rep_buf == NULL) 4657 { 4658 /* Can't replace termcodes - try using the string as is */ 4659 rep_buf = vim_strsave(rep); 4660 4661 /* Give up if out of memory */ 4662 if (rep_buf == NULL) 4663 return FAIL; 4664 } 4665 4666 /* get address of growarray: global or in curbuf */ 4667 if (flags & UC_BUFFER) 4668 { 4669 gap = &curbuf->b_ucmds; 4670 if (gap->ga_itemsize == 0) 4671 ga_init2(gap, (int)sizeof(ucmd_T), 4); 4672 } 4673 else 4674 gap = &ucmds; 4675 4676 /* Search for the command in the already defined commands. */ 4677 for (i = 0; i < gap->ga_len; ++i) 4678 { 4679 size_t len; 4680 4681 cmd = USER_CMD_GA(gap, i); 4682 len = STRLEN(cmd->uc_name); 4683 cmp = STRNCMP(name, cmd->uc_name, name_len); 4684 if (cmp == 0) 4685 { 4686 if (name_len < len) 4687 cmp = -1; 4688 else if (name_len > len) 4689 cmp = 1; 4690 } 4691 4692 if (cmp == 0) 4693 { 4694 if (!force) 4695 { 4696 EMSG(_("E174: Command already exists: add ! to replace it")); 4697 goto fail; 4698 } 4699 4700 vim_free(cmd->uc_rep); 4701 cmd->uc_rep = 0; 4702 break; 4703 } 4704 4705 /* Stop as soon as we pass the name to add */ 4706 if (cmp < 0) 4707 break; 4708 } 4709 4710 /* Extend the array unless we're replacing an existing command */ 4711 if (cmp != 0) 4712 { 4713 if (ga_grow(gap, 1) != OK) 4714 goto fail; 4715 if ((p = vim_strnsave(name, (int)name_len)) == NULL) 4716 goto fail; 4717 4718 cmd = USER_CMD_GA(gap, i); 4719 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T)); 4720 4721 ++gap->ga_len; 4722 --gap->ga_room; 4723 4724 cmd->uc_name = p; 4725 } 4726 4727 cmd->uc_rep = rep_buf; 4728 cmd->uc_argt = argt; 4729 cmd->uc_def = def; 4730 cmd->uc_compl = compl; 4731 #ifdef FEAT_EVAL 4732 cmd->uc_scriptID = current_SID; 4733 # ifdef FEAT_CMDL_COMPL 4734 cmd->uc_compl_arg = compl_arg; 4735 # endif 4736 #endif 4737 4738 return OK; 4739 4740 fail: 4741 vim_free(rep_buf); 4742 #if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 4743 vim_free(compl_arg); 4744 #endif 4745 return FAIL; 4746 } 4747 4748 /* 4749 * List of names for completion for ":command" with the EXPAND_ flag. 4750 * Must be alphabetical for completion. 4751 */ 4752 static struct 4753 { 4754 int expand; 4755 char *name; 4756 } command_complete[] = 4757 { 4758 {EXPAND_AUGROUP, "augroup"}, 4759 {EXPAND_BUFFERS, "buffer"}, 4760 {EXPAND_COMMANDS, "command"}, 4761 #if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 4762 {EXPAND_USER_DEFINED, "custom"}, 4763 #endif 4764 {EXPAND_DIRECTORIES, "dir"}, 4765 {EXPAND_ENV_VARS, "environment"}, 4766 {EXPAND_EVENTS, "event"}, 4767 {EXPAND_EXPRESSION, "expression"}, 4768 {EXPAND_FILES, "file"}, 4769 {EXPAND_FUNCTIONS, "function"}, 4770 {EXPAND_HELP, "help"}, 4771 {EXPAND_HIGHLIGHT, "highlight"}, 4772 {EXPAND_MAPPINGS, "mapping"}, 4773 {EXPAND_MENUS, "menu"}, 4774 {EXPAND_SETTINGS, "option"}, 4775 {EXPAND_TAGS, "tag"}, 4776 {EXPAND_TAGS_LISTFILES, "tag_listfiles"}, 4777 {EXPAND_USER_VARS, "var"}, 4778 {0, NULL} 4779 }; 4780 4781 static void 4782 uc_list(name, name_len) 4783 char_u *name; 4784 size_t name_len; 4785 { 4786 int i, j; 4787 int found = FALSE; 4788 ucmd_T *cmd; 4789 int len; 4790 long a; 4791 garray_T *gap; 4792 4793 gap = &curbuf->b_ucmds; 4794 for (;;) 4795 { 4796 for (i = 0; i < gap->ga_len; ++i) 4797 { 4798 cmd = USER_CMD_GA(gap, i); 4799 a = cmd->uc_argt; 4800 4801 /* Skip commands which don't match the requested prefix */ 4802 if (STRNCMP(name, cmd->uc_name, name_len) != 0) 4803 continue; 4804 4805 /* Put out the title first time */ 4806 if (!found) 4807 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition")); 4808 found = TRUE; 4809 msg_putchar('\n'); 4810 if (got_int) 4811 break; 4812 4813 /* Special cases */ 4814 msg_putchar(a & BANG ? '!' : ' '); 4815 msg_putchar(a & REGSTR ? '"' : ' '); 4816 msg_putchar(gap != &ucmds ? 'b' : ' '); 4817 msg_putchar(' '); 4818 4819 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D)); 4820 len = (int)STRLEN(cmd->uc_name) + 4; 4821 4822 do { 4823 msg_putchar(' '); 4824 ++len; 4825 } while (len < 16); 4826 4827 len = 0; 4828 4829 /* Arguments */ 4830 switch ((int)(a & (EXTRA|NOSPC|NEEDARG))) 4831 { 4832 case 0: IObuff[len++] = '0'; break; 4833 case (EXTRA): IObuff[len++] = '*'; break; 4834 case (EXTRA|NOSPC): IObuff[len++] = '?'; break; 4835 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break; 4836 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break; 4837 } 4838 4839 do { 4840 IObuff[len++] = ' '; 4841 } while (len < 5); 4842 4843 /* Range */ 4844 if (a & (RANGE|COUNT)) 4845 { 4846 if (a & COUNT) 4847 { 4848 /* -count=N */ 4849 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def); 4850 len += (int)STRLEN(IObuff + len); 4851 } 4852 else if (a & DFLALL) 4853 IObuff[len++] = '%'; 4854 else if (cmd->uc_def >= 0) 4855 { 4856 /* -range=N */ 4857 sprintf((char *)IObuff + len, "%ld", cmd->uc_def); 4858 len += (int)STRLEN(IObuff + len); 4859 } 4860 else 4861 IObuff[len++] = '.'; 4862 } 4863 4864 do { 4865 IObuff[len++] = ' '; 4866 } while (len < 11); 4867 4868 /* Completion */ 4869 for (j = 0; command_complete[j].expand != 0; ++j) 4870 if (command_complete[j].expand == cmd->uc_compl) 4871 { 4872 STRCPY(IObuff + len, command_complete[j].name); 4873 len += (int)STRLEN(IObuff + len); 4874 break; 4875 } 4876 4877 do { 4878 IObuff[len++] = ' '; 4879 } while (len < 21); 4880 4881 IObuff[len] = '\0'; 4882 msg_outtrans(IObuff); 4883 4884 msg_outtrans_special(cmd->uc_rep, FALSE); 4885 out_flush(); 4886 ui_breakcheck(); 4887 if (got_int) 4888 break; 4889 } 4890 if (gap == &ucmds || i < gap->ga_len) 4891 break; 4892 gap = &ucmds; 4893 } 4894 4895 if (!found) 4896 MSG(_("No user-defined commands found")); 4897 } 4898 4899 static char_u * 4900 uc_fun_cmd() 4901 { 4902 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4, 4903 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60, 4904 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2, 4905 0xb9, 0x7f, 0}; 4906 int i; 4907 4908 for (i = 0; fcmd[i]; ++i) 4909 IObuff[i] = fcmd[i] - 0x40; 4910 IObuff[i] = 0; 4911 return IObuff; 4912 } 4913 4914 static int 4915 uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg) 4916 char_u *attr; 4917 size_t len; 4918 long *argt; 4919 long *def; 4920 int *flags; 4921 int *compl; 4922 char_u **compl_arg; 4923 { 4924 char_u *p; 4925 4926 if (len == 0) 4927 { 4928 EMSG(_("E175: No attribute specified")); 4929 return FAIL; 4930 } 4931 4932 /* First, try the simple attributes (no arguments) */ 4933 if (STRNICMP(attr, "bang", len) == 0) 4934 *argt |= BANG; 4935 else if (STRNICMP(attr, "buffer", len) == 0) 4936 *flags |= UC_BUFFER; 4937 else if (STRNICMP(attr, "register", len) == 0) 4938 *argt |= REGSTR; 4939 else if (STRNICMP(attr, "bar", len) == 0) 4940 *argt |= TRLBAR; 4941 else 4942 { 4943 int i; 4944 char_u *val = NULL; 4945 size_t vallen = 0; 4946 size_t attrlen = len; 4947 4948 /* Look for the attribute name - which is the part before any '=' */ 4949 for (i = 0; i < (int)len; ++i) 4950 { 4951 if (attr[i] == '=') 4952 { 4953 val = &attr[i + 1]; 4954 vallen = len - i - 1; 4955 attrlen = i; 4956 break; 4957 } 4958 } 4959 4960 if (STRNICMP(attr, "nargs", attrlen) == 0) 4961 { 4962 if (vallen == 1) 4963 { 4964 if (*val == '0') 4965 /* Do nothing - this is the default */; 4966 else if (*val == '1') 4967 *argt |= (EXTRA | NOSPC | NEEDARG); 4968 else if (*val == '*') 4969 *argt |= EXTRA; 4970 else if (*val == '?') 4971 *argt |= (EXTRA | NOSPC); 4972 else if (*val == '+') 4973 *argt |= (EXTRA | NEEDARG); 4974 else 4975 goto wrong_nargs; 4976 } 4977 else 4978 { 4979 wrong_nargs: 4980 EMSG(_("E176: Invalid number of arguments")); 4981 return FAIL; 4982 } 4983 } 4984 else if (STRNICMP(attr, "range", attrlen) == 0) 4985 { 4986 *argt |= RANGE; 4987 if (vallen == 1 && *val == '%') 4988 *argt |= DFLALL; 4989 else if (val != NULL) 4990 { 4991 p = val; 4992 if (*def >= 0) 4993 { 4994 two_count: 4995 EMSG(_("E177: Count cannot be specified twice")); 4996 return FAIL; 4997 } 4998 4999 *def = getdigits(&p); 5000 *argt |= (ZEROR | NOTADR); 5001 5002 if (p != val + vallen || vallen == 0) 5003 { 5004 invalid_count: 5005 EMSG(_("E178: Invalid default value for count")); 5006 return FAIL; 5007 } 5008 } 5009 } 5010 else if (STRNICMP(attr, "count", attrlen) == 0) 5011 { 5012 *argt |= (COUNT | ZEROR | NOTADR); 5013 5014 if (val != NULL) 5015 { 5016 p = val; 5017 if (*def >= 0) 5018 goto two_count; 5019 5020 *def = getdigits(&p); 5021 5022 if (p != val + vallen) 5023 goto invalid_count; 5024 } 5025 5026 if (*def < 0) 5027 *def = 0; 5028 } 5029 else if (STRNICMP(attr, "complete", attrlen) == 0) 5030 { 5031 char_u *arg = NULL; 5032 size_t arglen = 0; 5033 5034 if (val == NULL) 5035 { 5036 EMSG(_("E179: argument required for complete")); 5037 return FAIL; 5038 } 5039 /* Look for any argument part - which is the part after any ',' */ 5040 for (i = 0; i < (int)vallen; ++i) 5041 { 5042 if (val[i] == ',') 5043 { 5044 arg = &val[i + 1]; 5045 arglen = vallen - i - 1; 5046 vallen = i; 5047 break; 5048 } 5049 } 5050 5051 for (i = 0; command_complete[i].expand != 0; ++i) 5052 { 5053 if (STRLEN(command_complete[i].name) == vallen 5054 && STRNCMP(val, command_complete[i].name, vallen) == 0) 5055 { 5056 *compl = command_complete[i].expand; 5057 if (command_complete[i].expand == EXPAND_BUFFERS) 5058 *argt |= BUFNAME; 5059 else if (command_complete[i].expand == EXPAND_DIRECTORIES 5060 || command_complete[i].expand == EXPAND_FILES) 5061 *argt |= XFILE; 5062 break; 5063 } 5064 } 5065 5066 if (command_complete[i].expand == 0) 5067 { 5068 EMSG2(_("E180: Invalid complete value: %s"), val); 5069 return FAIL; 5070 } 5071 #if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 5072 if (*compl != EXPAND_USER_DEFINED && arg != NULL) 5073 #else 5074 if (arg != NULL) 5075 #endif 5076 { 5077 EMSG(_("E468: Completion argument only allowed for custom completion")); 5078 return FAIL; 5079 } 5080 #if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 5081 if (*compl == EXPAND_USER_DEFINED && arg == NULL) 5082 { 5083 EMSG(_("E467: Custom completion requires a function argument")); 5084 return FAIL; 5085 } 5086 if (arg != NULL) 5087 *compl_arg = vim_strnsave(arg, (int)arglen); 5088 #endif 5089 } 5090 else 5091 { 5092 char_u ch = attr[len]; 5093 attr[len] = '\0'; 5094 EMSG2(_("E181: Invalid attribute: %s"), attr); 5095 attr[len] = ch; 5096 return FAIL; 5097 } 5098 } 5099 5100 return OK; 5101 } 5102 5103 static void 5104 ex_command(eap) 5105 exarg_T *eap; 5106 { 5107 char_u *name; 5108 char_u *end; 5109 char_u *p; 5110 long argt = 0; 5111 long def = -1; 5112 int flags = 0; 5113 int compl = EXPAND_NOTHING; 5114 char_u *compl_arg = NULL; 5115 int has_attr = (eap->arg[0] == '-'); 5116 5117 p = eap->arg; 5118 5119 /* Check for attributes */ 5120 while (*p == '-') 5121 { 5122 ++p; 5123 end = skiptowhite(p); 5124 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg) 5125 == FAIL) 5126 return; 5127 p = skipwhite(end); 5128 } 5129 5130 /* Get the name (if any) and skip to the following argument */ 5131 name = p; 5132 if (ASCII_ISALPHA(*p)) 5133 while (ASCII_ISALNUM(*p)) 5134 ++p; 5135 if (!ends_excmd(*p) && !vim_iswhite(*p)) 5136 { 5137 EMSG(_("E182: Invalid command name")); 5138 return; 5139 } 5140 end = p; 5141 5142 /* If there is nothing after the name, and no attributes were specified, 5143 * we are listing commands 5144 */ 5145 p = skipwhite(end); 5146 if (!has_attr && ends_excmd(*p)) 5147 { 5148 uc_list(name, end - name); 5149 } 5150 else if (!ASCII_ISUPPER(*name)) 5151 { 5152 EMSG(_("E183: User defined commands must start with an uppercase letter")); 5153 return; 5154 } 5155 else 5156 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg, 5157 eap->forceit); 5158 } 5159 5160 /* 5161 * ":comclear" 5162 */ 5163 /*ARGSUSED*/ 5164 /* 5165 * Clear all user commands, global and for current buffer. 5166 */ 5167 static void 5168 ex_comclear(eap) 5169 exarg_T *eap; 5170 { 5171 uc_clear(&ucmds); 5172 uc_clear(&curbuf->b_ucmds); 5173 } 5174 5175 /* 5176 * Clear all user commands for "gap". 5177 */ 5178 void 5179 uc_clear(gap) 5180 garray_T *gap; 5181 { 5182 int i; 5183 ucmd_T *cmd; 5184 5185 for (i = 0; i < gap->ga_len; ++i) 5186 { 5187 cmd = USER_CMD_GA(gap, i); 5188 vim_free(cmd->uc_name); 5189 vim_free(cmd->uc_rep); 5190 # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 5191 vim_free(cmd->uc_compl_arg); 5192 # endif 5193 } 5194 ga_clear(gap); 5195 } 5196 5197 static void 5198 ex_delcommand(eap) 5199 exarg_T *eap; 5200 { 5201 int i = 0; 5202 ucmd_T *cmd = NULL; 5203 int cmp = -1; 5204 garray_T *gap; 5205 5206 gap = &curbuf->b_ucmds; 5207 for (;;) 5208 { 5209 for (i = 0; i < gap->ga_len; ++i) 5210 { 5211 cmd = USER_CMD_GA(gap, i); 5212 cmp = STRCMP(eap->arg, cmd->uc_name); 5213 if (cmp <= 0) 5214 break; 5215 } 5216 if (gap == &ucmds || cmp == 0) 5217 break; 5218 gap = &ucmds; 5219 } 5220 5221 if (cmp != 0) 5222 { 5223 EMSG2(_("E184: No such user-defined command: %s"), eap->arg); 5224 return; 5225 } 5226 5227 vim_free(cmd->uc_name); 5228 vim_free(cmd->uc_rep); 5229 # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) 5230 vim_free(cmd->uc_compl_arg); 5231 # endif 5232 5233 --gap->ga_len; 5234 ++gap->ga_room; 5235 5236 if (i < gap->ga_len) 5237 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T)); 5238 } 5239 5240 static char_u * 5241 uc_split_args(arg, lenp) 5242 char_u *arg; 5243 size_t *lenp; 5244 { 5245 char_u *buf; 5246 char_u *p; 5247 char_u *q; 5248 int len; 5249 5250 /* Precalculate length */ 5251 p = arg; 5252 len = 2; /* Initial and final quotes */ 5253 5254 while (*p) 5255 { 5256 if (p[0] == '\\' && vim_iswhite(p[1])) 5257 { 5258 len += 1; 5259 p += 2; 5260 } 5261 else if (*p == '\\' || *p == '"') 5262 { 5263 len += 2; 5264 p += 1; 5265 } 5266 else if (vim_iswhite(*p)) 5267 { 5268 p = skipwhite(p); 5269 if (*p == NUL) 5270 break; 5271 len += 3; /* "," */ 5272 } 5273 else 5274 { 5275 ++len; 5276 ++p; 5277 } 5278 } 5279 5280 buf = alloc(len + 1); 5281 if (buf == NULL) 5282 { 5283 *lenp = 0; 5284 return buf; 5285 } 5286 5287 p = arg; 5288 q = buf; 5289 *q++ = '"'; 5290 while (*p) 5291 { 5292 if (p[0] == '\\' && vim_iswhite(p[1])) 5293 { 5294 *q++ = p[1]; 5295 p += 2; 5296 } 5297 else if (*p == '\\' || *p == '"') 5298 { 5299 *q++ = '\\'; 5300 *q++ = *p++; 5301 } 5302 else if (vim_iswhite(*p)) 5303 { 5304 p = skipwhite(p); 5305 if (*p == NUL) 5306 break; 5307 *q++ = '"'; 5308 *q++ = ','; 5309 *q++ = '"'; 5310 } 5311 else 5312 { 5313 *q++ = *p++; 5314 } 5315 } 5316 *q++ = '"'; 5317 *q = 0; 5318 5319 *lenp = len; 5320 return buf; 5321 } 5322 5323 /* 5324 * Check for a <> code in a user command. 5325 * "code" points to the '<'. "len" the length of the <> (inclusive). 5326 * "buf" is where the result is to be added. 5327 * "split_buf" points to a buffer used for splitting, caller should free it. 5328 * "split_len" is the length of what "split_buf" contains. 5329 * Returns the length of the replacement, which has been added to "buf". 5330 * Returns -1 if there was no match, and only the "<" has been copied. 5331 */ 5332 static size_t 5333 uc_check_code(code, len, buf, cmd, eap, split_buf, split_len) 5334 char_u *code; 5335 size_t len; 5336 char_u *buf; 5337 ucmd_T *cmd; /* the user command we're expanding */ 5338 exarg_T *eap; /* ex arguments */ 5339 char_u **split_buf; 5340 size_t *split_len; 5341 { 5342 size_t result = 0; 5343 char_u *p = code + 1; 5344 size_t l = len - 2; 5345 int quote = 0; 5346 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER, 5347 ct_LT, ct_NONE } type = ct_NONE; 5348 5349 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-') 5350 { 5351 quote = (*p == 'q' || *p == 'Q') ? 1 : 2; 5352 p += 2; 5353 l -= 2; 5354 } 5355 5356 if (l < 1) 5357 type = ct_NONE; 5358 else if (STRNICMP(p, "args", l) == 0) 5359 type = ct_ARGS; 5360 else if (STRNICMP(p, "bang", l) == 0) 5361 type = ct_BANG; 5362 else if (STRNICMP(p, "count", l) == 0) 5363 type = ct_COUNT; 5364 else if (STRNICMP(p, "line1", l) == 0) 5365 type = ct_LINE1; 5366 else if (STRNICMP(p, "line2", l) == 0) 5367 type = ct_LINE2; 5368 else if (STRNICMP(p, "lt", l) == 0) 5369 type = ct_LT; 5370 else if (STRNICMP(p, "register", l) == 0) 5371 type = ct_REGISTER; 5372 5373 switch (type) 5374 { 5375 case ct_ARGS: 5376 /* Simple case first */ 5377 if (*eap->arg == NUL) 5378 { 5379 if (quote == 1) 5380 { 5381 result = 2; 5382 if (buf != NULL) 5383 STRCPY(buf, "''"); 5384 } 5385 else 5386 result = 0; 5387 break; 5388 } 5389 5390 /* When specified there is a single argument don't split it. 5391 * Works for ":Cmd %" when % is "a b c". */ 5392 if ((eap->argt & NOSPC) && quote == 2) 5393 quote = 1; 5394 5395 switch (quote) 5396 { 5397 case 0: /* No quoting, no splitting */ 5398 result = STRLEN(eap->arg); 5399 if (buf != NULL) 5400 STRCPY(buf, eap->arg); 5401 break; 5402 case 1: /* Quote, but don't split */ 5403 result = STRLEN(eap->arg) + 2; 5404 for (p = eap->arg; *p; ++p) 5405 { 5406 if (*p == '\\' || *p == '"') 5407 ++result; 5408 } 5409 5410 if (buf != NULL) 5411 { 5412 *buf++ = '"'; 5413 for (p = eap->arg; *p; ++p) 5414 { 5415 if (*p == '\\' || *p == '"') 5416 *buf++ = '\\'; 5417 *buf++ = *p; 5418 } 5419 *buf = '"'; 5420 } 5421 5422 break; 5423 case 2: /* Quote and split */ 5424 /* This is hard, so only do it once, and cache the result */ 5425 if (*split_buf == NULL) 5426 *split_buf = uc_split_args(eap->arg, split_len); 5427 5428 result = *split_len; 5429 if (buf != NULL && result != 0) 5430 STRCPY(buf, *split_buf); 5431 5432 break; 5433 } 5434 break; 5435 5436 case ct_BANG: 5437 result = eap->forceit ? 1 : 0; 5438 if (quote) 5439 result += 2; 5440 if (buf != NULL) 5441 { 5442 if (quote) 5443 *buf++ = '"'; 5444 if (eap->forceit) 5445 *buf++ = '!'; 5446 if (quote) 5447 *buf = '"'; 5448 } 5449 break; 5450 5451 case ct_LINE1: 5452 case ct_LINE2: 5453 case ct_COUNT: 5454 { 5455 char num_buf[20]; 5456 long num = (type == ct_LINE1) ? eap->line1 : 5457 (type == ct_LINE2) ? eap->line2 : 5458 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def; 5459 size_t num_len; 5460 5461 sprintf(num_buf, "%ld", num); 5462 num_len = STRLEN(num_buf); 5463 result = num_len; 5464 5465 if (quote) 5466 result += 2; 5467 5468 if (buf != NULL) 5469 { 5470 if (quote) 5471 *buf++ = '"'; 5472 STRCPY(buf, num_buf); 5473 buf += num_len; 5474 if (quote) 5475 *buf = '"'; 5476 } 5477 5478 break; 5479 } 5480 5481 case ct_REGISTER: 5482 result = eap->regname ? 1 : 0; 5483 if (quote) 5484 result += 2; 5485 if (buf != NULL) 5486 { 5487 if (quote) 5488 *buf++ = '\''; 5489 if (eap->regname) 5490 *buf++ = eap->regname; 5491 if (quote) 5492 *buf = '\''; 5493 } 5494 break; 5495 5496 case ct_LT: 5497 result = 1; 5498 if (buf != NULL) 5499 *buf = '<'; 5500 break; 5501 5502 default: 5503 /* Not recognized: just copy the '<' and return -1. */ 5504 result = (size_t)-1; 5505 if (buf != NULL) 5506 *buf = '<'; 5507 break; 5508 } 5509 5510 return result; 5511 } 5512 5513 static void 5514 do_ucmd(eap) 5515 exarg_T *eap; 5516 { 5517 char_u *buf; 5518 char_u *p; 5519 char_u *q; 5520 5521 char_u *start; 5522 char_u *end; 5523 size_t len, totlen; 5524 5525 size_t split_len = 0; 5526 char_u *split_buf = NULL; 5527 ucmd_T *cmd; 5528 #ifdef FEAT_EVAL 5529 scid_T save_current_SID = current_SID; 5530 #endif 5531 5532 if (eap->cmdidx == CMD_USER) 5533 cmd = USER_CMD(eap->useridx); 5534 else 5535 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx); 5536 5537 /* 5538 * Replace <> in the command by the arguments. 5539 */ 5540 buf = NULL; 5541 for (;;) 5542 { 5543 p = cmd->uc_rep; 5544 q = buf; 5545 totlen = 0; 5546 while ((start = vim_strchr(p, '<')) != NULL 5547 && (end = vim_strchr(start + 1, '>')) != NULL) 5548 { 5549 /* Include the '>' */ 5550 ++end; 5551 5552 /* Take everything up to the '<' */ 5553 len = start - p; 5554 if (buf == NULL) 5555 totlen += len; 5556 else 5557 { 5558 mch_memmove(q, p, len); 5559 q += len; 5560 } 5561 5562 len = uc_check_code(start, end - start, q, cmd, eap, 5563 &split_buf, &split_len); 5564 if (len == (size_t)-1) 5565 { 5566 /* no match, continue after '<' */ 5567 p = start + 1; 5568 len = 1; 5569 } 5570 else 5571 p = end; 5572 if (buf == NULL) 5573 totlen += len; 5574 else 5575 q += len; 5576 } 5577 if (buf != NULL) /* second time here, finished */ 5578 { 5579 STRCPY(q, p); 5580 break; 5581 } 5582 5583 totlen += STRLEN(p); /* Add on the trailing characters */ 5584 buf = alloc((unsigned)(totlen + 1)); 5585 if (buf == NULL) 5586 { 5587 vim_free(split_buf); 5588 return; 5589 } 5590 } 5591 5592 #ifdef FEAT_EVAL 5593 current_SID = cmd->uc_scriptID; 5594 #endif 5595 (void)do_cmdline(buf, eap->getline, eap->cookie, 5596 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); 5597 #ifdef FEAT_EVAL 5598 current_SID = save_current_SID; 5599 #endif 5600 vim_free(buf); 5601 vim_free(split_buf); 5602 } 5603 5604 # if defined(FEAT_CMDL_COMPL) || defined(PROTO) 5605 static char_u * 5606 get_user_command_name(idx) 5607 int idx; 5608 { 5609 return get_user_commands(NULL, idx - (int)CMD_SIZE); 5610 } 5611 5612 /* 5613 * Function given to ExpandGeneric() to obtain the list of user command names. 5614 */ 5615 /*ARGSUSED*/ 5616 char_u * 5617 get_user_commands(xp, idx) 5618 expand_T *xp; 5619 int idx; 5620 { 5621 if (idx < curbuf->b_ucmds.ga_len) 5622 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name; 5623 idx -= curbuf->b_ucmds.ga_len; 5624 if (idx < ucmds.ga_len) 5625 return USER_CMD(idx)->uc_name; 5626 return NULL; 5627 } 5628 5629 /* 5630 * Function given to ExpandGeneric() to obtain the list of user command 5631 * attributes. 5632 */ 5633 /*ARGSUSED*/ 5634 char_u * 5635 get_user_cmd_flags(xp, idx) 5636 expand_T *xp; 5637 int idx; 5638 { 5639 static char *user_cmd_flags[] = 5640 {"bang", "bar", "buffer", "complete", "count", 5641 "nargs", "range", "register"}; 5642 5643 if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])) 5644 return NULL; 5645 return (char_u *)user_cmd_flags[idx]; 5646 } 5647 5648 /* 5649 * Function given to ExpandGeneric() to obtain the list of values for -nargs. 5650 */ 5651 /*ARGSUSED*/ 5652 char_u * 5653 get_user_cmd_nargs(xp, idx) 5654 expand_T *xp; 5655 int idx; 5656 { 5657 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"}; 5658 5659 if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])) 5660 return NULL; 5661 return (char_u *)user_cmd_nargs[idx]; 5662 } 5663 5664 /* 5665 * Function given to ExpandGeneric() to obtain the list of values for -complete. 5666 */ 5667 /*ARGSUSED*/ 5668 char_u * 5669 get_user_cmd_complete(xp, idx) 5670 expand_T *xp; 5671 int idx; 5672 { 5673 return (char_u *)command_complete[idx].name; 5674 } 5675 # endif /* FEAT_CMDL_COMPL */ 5676 5677 #endif /* FEAT_USR_CMDS */ 5678 5679 static void 5680 ex_colorscheme(eap) 5681 exarg_T *eap; 5682 { 5683 if (load_colors(eap->arg) == FAIL) 5684 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg); 5685 } 5686 5687 static void 5688 ex_highlight(eap) 5689 exarg_T *eap; 5690 { 5691 if (*eap->arg == NUL && eap->cmd[2] == '!') 5692 MSG(_("Greetings, Vim user!")); 5693 do_highlight(eap->arg, eap->forceit, FALSE); 5694 } 5695 5696 5697 /* 5698 * Call this function if we thought we were going to exit, but we won't 5699 * (because of an error). May need to restore the terminal mode. 5700 */ 5701 void 5702 not_exiting() 5703 { 5704 exiting = FALSE; 5705 settmode(TMODE_RAW); 5706 } 5707 5708 /* 5709 * ":quit": quit current window, quit Vim if closed the last window. 5710 */ 5711 static void 5712 ex_quit(eap) 5713 exarg_T *eap; 5714 { 5715 #ifdef FEAT_CMDWIN 5716 if (cmdwin_type != 0) 5717 { 5718 cmdwin_result = Ctrl_C; 5719 return; 5720 } 5721 #endif 5722 5723 #ifdef FEAT_NETBEANS_INTG 5724 netbeansForcedQuit = eap->forceit; 5725 #endif 5726 5727 /* 5728 * If there are more files or windows we won't exit. 5729 */ 5730 if (check_more(FALSE, eap->forceit) == OK && only_one_window()) 5731 exiting = TRUE; 5732 if ((!P_HID(curbuf) 5733 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE)) 5734 || check_more(TRUE, eap->forceit) == FAIL 5735 || (only_one_window() && check_changed_any(eap->forceit))) 5736 { 5737 not_exiting(); 5738 } 5739 else 5740 { 5741 #ifdef FEAT_WINDOWS 5742 if (only_one_window()) /* quit last window */ 5743 #endif 5744 getout(0); 5745 #ifdef FEAT_WINDOWS 5746 # ifdef FEAT_GUI 5747 need_mouse_correct = TRUE; 5748 # endif 5749 /* close window; may free buffer */ 5750 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit); 5751 #endif 5752 } 5753 } 5754 5755 /* 5756 * ":cquit". 5757 */ 5758 /*ARGSUSED*/ 5759 static void 5760 ex_cquit(eap) 5761 exarg_T *eap; 5762 { 5763 getout(1); /* this does not always pass on the exit code to the Manx 5764 compiler. why? */ 5765 } 5766 5767 /* 5768 * ":qall": try to quit all windows 5769 */ 5770 static void 5771 ex_quit_all(eap) 5772 exarg_T *eap; 5773 { 5774 # ifdef FEAT_CMDWIN 5775 if (cmdwin_type != 0) 5776 { 5777 if (eap->forceit) 5778 cmdwin_result = K_XF1; /* ex_window() takes care of this */ 5779 else 5780 cmdwin_result = K_XF2; 5781 return; 5782 } 5783 # endif 5784 exiting = TRUE; 5785 if (eap->forceit || !check_changed_any(FALSE)) 5786 getout(0); 5787 not_exiting(); 5788 } 5789 5790 #ifdef FEAT_WINDOWS 5791 /* 5792 * ":close": close current window, unless it is the last one 5793 */ 5794 static void 5795 ex_close(eap) 5796 exarg_T *eap; 5797 { 5798 # ifdef FEAT_CMDWIN 5799 if (cmdwin_type != 0) 5800 cmdwin_result = K_IGNORE; 5801 else 5802 # endif 5803 ex_win_close(eap, curwin); 5804 } 5805 5806 static void 5807 ex_win_close(eap, win) 5808 exarg_T *eap; 5809 win_T *win; 5810 { 5811 int need_hide; 5812 buf_T *buf = win->w_buffer; 5813 5814 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1); 5815 if (need_hide && !P_HID(buf) && !eap->forceit) 5816 { 5817 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) 5818 if ((p_confirm || cmdmod.confirm) && p_write) 5819 { 5820 dialog_changed(buf, FALSE); 5821 if (buf_valid(buf) && bufIsChanged(buf)) 5822 return; 5823 need_hide = FALSE; 5824 } 5825 else 5826 #endif 5827 { 5828 EMSG(_(e_nowrtmsg)); 5829 return; 5830 } 5831 } 5832 5833 #ifdef FEAT_GUI 5834 need_mouse_correct = TRUE; 5835 #endif 5836 /* free buffer when not hiding it or when it's a scratch buffer */ 5837 win_close(win, !need_hide && !P_HID(buf)); 5838 } 5839 5840 #ifdef FEAT_QUICKFIX 5841 /* 5842 * ":pclose": Close any preview window. 5843 */ 5844 static void 5845 ex_pclose(eap) 5846 exarg_T *eap; 5847 { 5848 win_T *win; 5849 5850 for (win = firstwin; win != NULL; win = win->w_next) 5851 if (win->w_p_pvw) 5852 { 5853 ex_win_close(eap, win); 5854 break; 5855 } 5856 } 5857 #endif 5858 5859 /* 5860 * ":only". 5861 */ 5862 static void 5863 ex_only(eap) 5864 exarg_T *eap; 5865 { 5866 # ifdef FEAT_GUI 5867 need_mouse_correct = TRUE; 5868 # endif 5869 close_others(TRUE, eap->forceit); 5870 } 5871 5872 /* 5873 * ":all" and ":sall". 5874 */ 5875 static void 5876 ex_all(eap) 5877 exarg_T *eap; 5878 { 5879 if (eap->addr_count == 0) 5880 eap->line2 = 9999; 5881 do_arg_all((int)eap->line2, eap->forceit); 5882 } 5883 #endif /* FEAT_WINDOWS */ 5884 5885 static void 5886 ex_hide(eap) 5887 exarg_T *eap; 5888 { 5889 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL) 5890 eap->errmsg = e_invarg; 5891 else 5892 { 5893 /* ":hide" or ":hide | cmd": hide current window */ 5894 eap->nextcmd = check_nextcmd(eap->arg); 5895 #ifdef FEAT_WINDOWS 5896 if (!eap->skip) 5897 { 5898 # ifdef FEAT_GUI 5899 need_mouse_correct = TRUE; 5900 # endif 5901 win_close(curwin, FALSE); /* don't free buffer */ 5902 } 5903 #endif 5904 } 5905 } 5906 5907 /* 5908 * ":stop" and ":suspend": Suspend Vim. 5909 */ 5910 static void 5911 ex_stop(eap) 5912 exarg_T *eap; 5913 { 5914 /* 5915 * Disallow suspending for "rvim". 5916 */ 5917 if (!check_restricted() 5918 #ifdef WIN3264 5919 /* 5920 * Check if external commands are allowed now. 5921 */ 5922 && can_end_termcap_mode(TRUE) 5923 #endif 5924 ) 5925 { 5926 if (!eap->forceit) 5927 autowrite_all(); 5928 windgoto((int)Rows - 1, 0); 5929 out_char('\n'); 5930 out_flush(); 5931 stoptermcap(); 5932 out_flush(); /* needed for SUN to restore xterm buffer */ 5933 #ifdef FEAT_TITLE 5934 mch_restore_title(3); /* restore window titles */ 5935 #endif 5936 ui_suspend(); /* call machine specific function */ 5937 #ifdef FEAT_TITLE 5938 maketitle(); 5939 resettitle(); /* force updating the title */ 5940 #endif 5941 starttermcap(); 5942 scroll_start(); /* scroll screen before redrawing */ 5943 redraw_later_clear(); 5944 shell_resized(); /* may have resized window */ 5945 } 5946 } 5947 5948 /* 5949 * ":exit", ":xit" and ":wq": Write file and exit Vim. 5950 */ 5951 static void 5952 ex_exit(eap) 5953 exarg_T *eap; 5954 { 5955 #ifdef FEAT_CMDWIN 5956 if (cmdwin_type != 0) 5957 { 5958 cmdwin_result = Ctrl_C; 5959 return; 5960 } 5961 #endif 5962 5963 /* 5964 * if more files or windows we won't exit 5965 */ 5966 if (check_more(FALSE, eap->forceit) == OK && only_one_window()) 5967 exiting = TRUE; 5968 if ( ((eap->cmdidx == CMD_wq 5969 || curbufIsChanged()) 5970 && do_write(eap) == FAIL) 5971 || check_more(TRUE, eap->forceit) == FAIL 5972 || (only_one_window() && check_changed_any(eap->forceit))) 5973 { 5974 not_exiting(); 5975 } 5976 else 5977 { 5978 #ifdef FEAT_WINDOWS 5979 if (only_one_window()) /* quit last window, exit Vim */ 5980 #endif 5981 getout(0); 5982 #ifdef FEAT_WINDOWS 5983 # ifdef FEAT_GUI 5984 need_mouse_correct = TRUE; 5985 # endif 5986 /* quit current window, may free buffer */ 5987 win_close(curwin, !P_HID(curwin->w_buffer)); 5988 #endif 5989 } 5990 } 5991 5992 /* 5993 * ":print", ":list", ":number". 5994 */ 5995 static void 5996 ex_print(eap) 5997 exarg_T *eap; 5998 { 5999 int save_list = 0; /* init for GCC */ 6000 6001 if (eap->cmdidx == CMD_list) 6002 { 6003 save_list = curwin->w_p_list; 6004 curwin->w_p_list = 1; 6005 } 6006 6007 for ( ;!got_int; ui_breakcheck()) 6008 { 6009 print_line(eap->line1, 6010 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound)); 6011 if (++eap->line1 > eap->line2) 6012 break; 6013 out_flush(); /* show one line at a time */ 6014 } 6015 setpcmark(); 6016 /* put cursor at last line */ 6017 curwin->w_cursor.lnum = eap->line2; 6018 beginline(BL_SOL | BL_FIX); 6019 6020 ex_no_reprint = TRUE; 6021 6022 if (eap->cmdidx == CMD_list) 6023 curwin->w_p_list = save_list; 6024 } 6025 6026 #ifdef FEAT_BYTEOFF 6027 static void 6028 ex_goto(eap) 6029 exarg_T *eap; 6030 { 6031 goto_byte(eap->line2); 6032 } 6033 #endif 6034 6035 /* 6036 * ":shell". 6037 */ 6038 /*ARGSUSED*/ 6039 static void 6040 ex_shell(eap) 6041 exarg_T *eap; 6042 { 6043 do_shell(NULL, 0); 6044 } 6045 6046 #if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \ 6047 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \ 6048 || defined(PROTO) 6049 6050 /* 6051 * Handle a file drop. The code is here because a drop is *nearly* like an 6052 * :args command, but not quite (we have a list of exact filenames, so we 6053 * don't want to (a) parse a command line, or (b) expand wildcards. So the 6054 * code is very similar to :args and hence needs access to a lot of the static 6055 * functions in this file. 6056 * 6057 * The list should be allocated using alloc(), as should each item in the 6058 * list. This function takes over responsibility for freeing the list. 6059 * 6060 * XXX The list is made into the arggument list. This is freed using 6061 * FreeWild(), which does a series of vim_free() calls, unless the two defines 6062 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a 6063 * routine _fnexplodefree() is used. This may cause problems, but as the drop 6064 * file functionality is (currently) not in EMX this is not presently a 6065 * problem. 6066 */ 6067 void 6068 handle_drop(filec, filev, split) 6069 int filec; /* the number of files dropped */ 6070 char_u **filev; /* the list of files dropped */ 6071 int split; /* force splitting the window */ 6072 { 6073 exarg_T ea; 6074 int save_msg_scroll = msg_scroll; 6075 6076 # ifdef FEAT_CMDWIN 6077 if (cmdwin_type != 0) 6078 return; 6079 # endif 6080 6081 /* Check whether the current buffer is changed. If so, we will need 6082 * to split the current window or data could be lost. 6083 * We don't need to check if the 'hidden' option is set, as in this 6084 * case the buffer won't be lost. 6085 */ 6086 if (!P_HID(curbuf) && !split) 6087 { 6088 ++emsg_off; 6089 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE); 6090 --emsg_off; 6091 } 6092 if (split) 6093 { 6094 # ifdef FEAT_WINDOWS 6095 if (win_split(0, 0) == FAIL) 6096 return; 6097 # ifdef FEAT_SCROLLBIND 6098 curwin->w_p_scb = FALSE; 6099 # endif 6100 6101 /* When splitting the window, create a new alist. Otherwise the 6102 * existing one is overwritten. */ 6103 alist_unlink(curwin->w_alist); 6104 alist_new(); 6105 # else 6106 return; /* can't split, always fail */ 6107 # endif 6108 } 6109 6110 /* 6111 * Set up the new argument list. 6112 */ 6113 alist_set(ALIST(curwin), filec, filev, FALSE); 6114 6115 /* 6116 * Move to the first file. 6117 */ 6118 /* Fake up a minimal "next" command for do_argfile() */ 6119 vim_memset(&ea, 0, sizeof(ea)); 6120 ea.cmd = (char_u *)"next"; 6121 do_argfile(&ea, 0); 6122 6123 /* do_ecmd() may set need_start_insertmode, but since we never left Insert 6124 * mode that is not needed here. */ 6125 need_start_insertmode = FALSE; 6126 6127 /* Restore msg_scroll, otherwise a following command may cause scrolling 6128 * unexpectedly. The screen will be redrawn by the caller, thus 6129 * msg_scroll being set by displaying a message is irrelevant. */ 6130 msg_scroll = save_msg_scroll; 6131 } 6132 #endif 6133 6134 static void alist_clear __ARGS((alist_T *al)); 6135 /* 6136 * Clear an argument list: free all file names and reset it to zero entries. 6137 */ 6138 static void 6139 alist_clear(al) 6140 alist_T *al; 6141 { 6142 while (--al->al_ga.ga_len >= 0) 6143 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname); 6144 ga_clear(&al->al_ga); 6145 } 6146 6147 /* 6148 * Init an argument list. 6149 */ 6150 void 6151 alist_init(al) 6152 alist_T *al; 6153 { 6154 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5); 6155 } 6156 6157 #if defined(FEAT_WINDOWS) || defined(PROTO) 6158 6159 /* 6160 * Remove a reference from an argument list. 6161 * Ignored when the argument list is the global one. 6162 * If the argument list is no longer used by any window, free it. 6163 */ 6164 void 6165 alist_unlink(al) 6166 alist_T *al; 6167 { 6168 if (al != &global_alist && --al->al_refcount <= 0) 6169 { 6170 alist_clear(al); 6171 vim_free(al); 6172 } 6173 } 6174 6175 # if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO) 6176 /* 6177 * Create a new argument list and use it for the current window. 6178 */ 6179 void 6180 alist_new() 6181 { 6182 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T)); 6183 if (curwin->w_alist == NULL) 6184 { 6185 curwin->w_alist = &global_alist; 6186 ++global_alist.al_refcount; 6187 } 6188 else 6189 { 6190 curwin->w_alist->al_refcount = 1; 6191 alist_init(curwin->w_alist); 6192 } 6193 } 6194 # endif 6195 #endif 6196 6197 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO) 6198 /* 6199 * Expand the file names in the global argument list. 6200 */ 6201 void 6202 alist_expand() 6203 { 6204 char_u **old_arg_files; 6205 char_u **new_arg_files; 6206 int new_arg_file_count; 6207 char_u *save_p_su = p_su; 6208 int i; 6209 6210 /* Don't use 'suffixes' here. This should work like the shell did the 6211 * expansion. Also, the vimrc file isn't read yet, thus the user 6212 * can't set the options. */ 6213 p_su = empty_option; 6214 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT)); 6215 if (old_arg_files != NULL) 6216 { 6217 for (i = 0; i < GARGCOUNT; ++i) 6218 old_arg_files[i] = GARGLIST[i].ae_fname; 6219 if (expand_wildcards(GARGCOUNT, old_arg_files, 6220 &new_arg_file_count, &new_arg_files, 6221 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK 6222 && new_arg_file_count > 0) 6223 { 6224 alist_set(&global_alist, new_arg_file_count, new_arg_files, TRUE); 6225 } 6226 vim_free(old_arg_files); 6227 } 6228 p_su = save_p_su; 6229 } 6230 #endif 6231 6232 /* 6233 * Set the argument list for the current window. 6234 * Takes over the allocated files[] and the allocated fnames in it. 6235 */ 6236 void 6237 alist_set(al, count, files, use_curbuf) 6238 alist_T *al; 6239 int count; 6240 char_u **files; 6241 int use_curbuf; 6242 { 6243 int i; 6244 6245 alist_clear(al); 6246 if (ga_grow(&al->al_ga, count) == OK) 6247 { 6248 for (i = 0; i < count; ++i) 6249 alist_add(al, files[i], use_curbuf ? 2 : 1); 6250 vim_free(files); 6251 } 6252 else 6253 FreeWild(count, files); 6254 #ifdef FEAT_WINDOWS 6255 if (al == &global_alist) 6256 #endif 6257 arg_had_last = FALSE; 6258 } 6259 6260 /* 6261 * Add file "fname" to argument list "al". 6262 * "fname" must have been allocated and "al" must have been checked for room. 6263 */ 6264 void 6265 alist_add(al, fname, set_fnum) 6266 alist_T *al; 6267 char_u *fname; 6268 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */ 6269 { 6270 if (fname == NULL) /* don't add NULL file names */ 6271 return; 6272 #ifdef BACKSLASH_IN_FILENAME 6273 slash_adjust(fname); 6274 #endif 6275 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname; 6276 if (set_fnum > 0) 6277 AARGLIST(al)[al->al_ga.ga_len].ae_fnum = 6278 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0)); 6279 ++al->al_ga.ga_len; 6280 --al->al_ga.ga_room; 6281 } 6282 6283 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) 6284 /* 6285 * Adjust slashes in file names. Called after 'shellslash' was set. 6286 */ 6287 void 6288 alist_slash_adjust() 6289 { 6290 int i; 6291 # ifdef FEAT_WINDOWS 6292 win_T *wp; 6293 # endif 6294 6295 for (i = 0; i < GARGCOUNT; ++i) 6296 if (GARGLIST[i].ae_fname != NULL) 6297 slash_adjust(GARGLIST[i].ae_fname); 6298 # ifdef FEAT_WINDOWS 6299 for (wp = firstwin; wp != NULL; wp = wp->w_next) 6300 if (wp->w_alist != &global_alist) 6301 for (i = 0; i < WARGCOUNT(wp); ++i) 6302 if (WARGLIST(wp)[i].ae_fname != NULL) 6303 slash_adjust(WARGLIST(wp)[i].ae_fname); 6304 # endif 6305 } 6306 #endif 6307 6308 /* 6309 * ":preserve". 6310 */ 6311 /*ARGSUSED*/ 6312 static void 6313 ex_preserve(eap) 6314 exarg_T *eap; 6315 { 6316 ml_preserve(curbuf, TRUE); 6317 } 6318 6319 /* 6320 * ":recover". 6321 */ 6322 static void 6323 ex_recover(eap) 6324 exarg_T *eap; 6325 { 6326 /* Set recoverymode right away to avoid the ATTENTION prompt. */ 6327 recoverymode = TRUE; 6328 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE) 6329 && (*eap->arg == NUL 6330 || setfname(curbuf, eap->arg, NULL, TRUE) == OK)) 6331 ml_recover(); 6332 recoverymode = FALSE; 6333 } 6334 6335 /* 6336 * Command modifier used in a wrong way. 6337 */ 6338 static void 6339 ex_wrongmodifier(eap) 6340 exarg_T *eap; 6341 { 6342 eap->errmsg = e_invcmd; 6343 } 6344 6345 #ifdef FEAT_WINDOWS 6346 /* 6347 * :sview [+command] file split window with new file, read-only 6348 * :split [[+command] file] split window with current or new file 6349 * :vsplit [[+command] file] split window vertically with current or new file 6350 * :new [[+command] file] split window with no or new file 6351 * :vnew [[+command] file] split vertically window with no or new file 6352 * :sfind [+command] file split window with file in 'path' 6353 */ 6354 void 6355 ex_splitview(eap) 6356 exarg_T *eap; 6357 { 6358 win_T *old_curwin; 6359 #if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE) 6360 char_u *fname = NULL; 6361 #endif 6362 #ifdef FEAT_BROWSE 6363 int browse_flag = cmdmod.browse; 6364 #endif 6365 6366 #ifndef FEAT_VERTSPLIT 6367 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew) 6368 { 6369 ex_ni(eap); 6370 return; 6371 } 6372 #endif 6373 6374 old_curwin = curwin; 6375 #ifdef FEAT_GUI 6376 need_mouse_correct = TRUE; 6377 #endif 6378 6379 #ifdef FEAT_QUICKFIX 6380 /* A ":split" in the quickfix window works like ":new". Don't want two 6381 * quickfix windows. */ 6382 if (bt_quickfix(curbuf)) 6383 { 6384 if (eap->cmdidx == CMD_split) 6385 eap->cmdidx = CMD_new; 6386 # ifdef FEAT_VERTSPLIT 6387 if (eap->cmdidx == CMD_vsplit) 6388 eap->cmdidx = CMD_vnew; 6389 # endif 6390 } 6391 #endif 6392 6393 #ifdef FEAT_SEARCHPATH 6394 if (eap->cmdidx == CMD_sfind) 6395 { 6396 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), 6397 FNAME_MESS, TRUE, curbuf->b_ffname); 6398 if (fname == NULL) 6399 goto theend; 6400 eap->arg = fname; 6401 } 6402 # ifdef FEAT_BROWSE 6403 else 6404 # endif 6405 #endif 6406 #ifdef FEAT_BROWSE 6407 if (cmdmod.browse 6408 # ifdef FEAT_VERTSPLIT 6409 && eap->cmdidx != CMD_vnew 6410 #endif 6411 && eap->cmdidx != CMD_new) 6412 { 6413 fname = do_browse(FALSE, (char_u *)_("Edit File in new window"), 6414 eap->arg, NULL, NULL, NULL, curbuf); 6415 if (fname == NULL) 6416 goto theend; 6417 eap->arg = fname; 6418 } 6419 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */ 6420 #endif 6421 6422 if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0, 6423 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL) 6424 { 6425 #ifdef FEAT_SCROLLBIND 6426 /* Reset 'scrollbind' when editing another file, but keep it when 6427 * doing ":split" without arguments. */ 6428 if (*eap->arg != NUL 6429 #ifdef FEAT_BROWSE 6430 || cmdmod.browse 6431 #endif 6432 ) 6433 curwin->w_p_scb = FALSE; 6434 else 6435 do_check_scrollbind(FALSE); 6436 #endif 6437 do_exedit(eap, old_curwin); 6438 } 6439 6440 #ifdef FEAT_BROWSE 6441 cmdmod.browse = browse_flag; 6442 #endif 6443 6444 #if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE) 6445 theend: 6446 vim_free(fname); 6447 #endif 6448 } 6449 #endif 6450 6451 /* 6452 * ":mode": Set screen mode. 6453 * If no argument given, just get the screen size and redraw. 6454 */ 6455 static void 6456 ex_mode(eap) 6457 exarg_T *eap; 6458 { 6459 if (*eap->arg == NUL) 6460 shell_resized(); 6461 else 6462 mch_screenmode(eap->arg); 6463 } 6464 6465 #ifdef FEAT_WINDOWS 6466 /* 6467 * ":resize". 6468 * set, increment or decrement current window height 6469 */ 6470 static void 6471 ex_resize(eap) 6472 exarg_T *eap; 6473 { 6474 int n; 6475 win_T *wp = curwin; 6476 6477 if (eap->addr_count > 0) 6478 { 6479 n = eap->line2; 6480 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next) 6481 ; 6482 } 6483 6484 #ifdef FEAT_GUI 6485 need_mouse_correct = TRUE; 6486 #endif 6487 n = atol((char *)eap->arg); 6488 #ifdef FEAT_VERTSPLIT 6489 if (cmdmod.split & WSP_VERT) 6490 { 6491 if (*eap->arg == '-' || *eap->arg == '+') 6492 n += W_WIDTH(curwin); 6493 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */ 6494 n = 9999; 6495 win_setwidth_win((int)n, wp); 6496 } 6497 else 6498 #endif 6499 { 6500 if (*eap->arg == '-' || *eap->arg == '+') 6501 n += curwin->w_height; 6502 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */ 6503 n = 9999; 6504 win_setheight_win((int)n, wp); 6505 } 6506 } 6507 #endif 6508 6509 /* 6510 * ":find [+command] <file>" command. 6511 */ 6512 static void 6513 ex_find(eap) 6514 exarg_T *eap; 6515 { 6516 #ifdef FEAT_SEARCHPATH 6517 char_u *fname; 6518 int count; 6519 6520 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS, 6521 TRUE, curbuf->b_ffname); 6522 if (eap->addr_count > 0) 6523 { 6524 /* Repeat finding the file "count" times. This matters when it 6525 * appears several times in the path. */ 6526 count = eap->line2; 6527 while (fname != NULL && --count > 0) 6528 { 6529 vim_free(fname); 6530 fname = find_file_in_path(NULL, 0, FNAME_MESS, 6531 FALSE, curbuf->b_ffname); 6532 } 6533 } 6534 6535 if (fname != NULL) 6536 { 6537 eap->arg = fname; 6538 #endif 6539 do_exedit(eap, NULL); 6540 #ifdef FEAT_SEARCHPATH 6541 vim_free(fname); 6542 } 6543 #endif 6544 } 6545 6546 /* 6547 * ":edit", ":badd". 6548 */ 6549 static void 6550 ex_edit(eap) 6551 exarg_T *eap; 6552 { 6553 do_exedit(eap, NULL); 6554 } 6555 6556 /* 6557 * ":edit <file>" command and alikes. 6558 */ 6559 /*ARGSUSED*/ 6560 void 6561 do_exedit(eap, old_curwin) 6562 exarg_T *eap; 6563 win_T *old_curwin; /* curwin before doing a split or NULL */ 6564 { 6565 int n; 6566 #ifdef FEAT_WINDOWS 6567 int need_hide; 6568 #endif 6569 6570 /* 6571 * ":vi" command ends Ex mode. 6572 */ 6573 if (exmode_active && (eap->cmdidx == CMD_visual 6574 || eap->cmdidx == CMD_view)) 6575 { 6576 exmode_active = FALSE; 6577 if (*eap->arg == NUL) 6578 return; 6579 } 6580 6581 if ((eap->cmdidx == CMD_new 6582 #ifdef FEAT_VERTSPLIT 6583 || eap->cmdidx == CMD_vnew 6584 #endif 6585 ) && *eap->arg == NUL) 6586 { 6587 /* ":new" without argument: edit an new empty buffer */ 6588 setpcmark(); 6589 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE, 6590 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0)); 6591 } 6592 else if ((eap->cmdidx != CMD_split 6593 #ifdef FEAT_VERTSPLIT 6594 && eap->cmdidx != CMD_vsplit 6595 #endif 6596 ) 6597 || *eap->arg != NUL 6598 #ifdef FEAT_BROWSE 6599 || cmdmod.browse 6600 #endif 6601 ) 6602 { 6603 n = readonlymode; 6604 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview) 6605 readonlymode = TRUE; 6606 else if (eap->cmdidx == CMD_enew) 6607 readonlymode = FALSE; /* 'readonly' doesn't make sense in an 6608 empty buffer */ 6609 setpcmark(); 6610 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg), 6611 NULL, eap, 6612 /* ":edit" goes to first line if Vi compatible */ 6613 (*eap->arg == NUL && eap->do_ecmd_lnum == 0 6614 && vim_strchr(p_cpo, CPO_GOTO1) != NULL) 6615 ? ECMD_ONE : eap->do_ecmd_lnum, 6616 (P_HID(curbuf) ? ECMD_HIDE : 0) 6617 + (eap->forceit ? ECMD_FORCEIT : 0) 6618 #ifdef FEAT_LISTCMDS 6619 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 ) 6620 #endif 6621 ) == FAIL) 6622 { 6623 /* Editing the file failed. If the window was split, close it. */ 6624 #ifdef FEAT_WINDOWS 6625 if (old_curwin != NULL) 6626 { 6627 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1); 6628 if (!need_hide || P_HID(curbuf)) 6629 { 6630 # ifdef FEAT_GUI 6631 need_mouse_correct = TRUE; 6632 # endif 6633 win_close(curwin, !need_hide && !P_HID(curbuf)); 6634 } 6635 } 6636 #endif 6637 } 6638 else if (readonlymode && curbuf->b_nwindows == 1) 6639 { 6640 /* When editing an already visited buffer, 'readonly' won't be set 6641 * but the previous value is kept. With ":view" and ":sview" we 6642 * want the file to be readonly, except when another window is 6643 * editing the same buffer. */ 6644 curbuf->b_p_ro = TRUE; 6645 } 6646 readonlymode = n; 6647 } 6648 else 6649 { 6650 if (eap->do_ecmd_cmd != NULL) 6651 do_cmdline_cmd(eap->do_ecmd_cmd); 6652 #ifdef FEAT_TITLE 6653 n = curwin->w_arg_idx_invalid; 6654 #endif 6655 check_arg_idx(curwin); 6656 #ifdef FEAT_TITLE 6657 if (n != curwin->w_arg_idx_invalid) 6658 maketitle(); 6659 #endif 6660 } 6661 6662 #ifdef FEAT_WINDOWS 6663 /* 6664 * if ":split file" worked, set alternate file name in old window to new 6665 * file 6666 */ 6667 if (old_curwin != NULL 6668 && *eap->arg != NUL 6669 && curwin != old_curwin 6670 && win_valid(old_curwin) 6671 && old_curwin->w_buffer != curbuf) 6672 old_curwin->w_alt_fnum = curbuf->b_fnum; 6673 #endif 6674 6675 ex_no_reprint = TRUE; 6676 } 6677 6678 #ifndef FEAT_GUI 6679 /* 6680 * ":gui" and ":gvim" when there is no GUI. 6681 */ 6682 static void 6683 ex_nogui(eap) 6684 exarg_T *eap; 6685 { 6686 eap->errmsg = e_nogvim; 6687 } 6688 #endif 6689 6690 #if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) 6691 static void 6692 ex_tearoff(eap) 6693 exarg_T *eap; 6694 { 6695 gui_make_tearoff(eap->arg); 6696 } 6697 #endif 6698 6699 #if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU) 6700 static void 6701 ex_popup(eap) 6702 exarg_T *eap; 6703 { 6704 gui_make_popup(eap->arg); 6705 } 6706 #endif 6707 6708 /*ARGSUSED*/ 6709 static void 6710 ex_swapname(eap) 6711 exarg_T *eap; 6712 { 6713 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL) 6714 MSG(_("No swap file")); 6715 else 6716 msg(curbuf->b_ml.ml_mfp->mf_fname); 6717 } 6718 6719 /* 6720 * ":syncbind" forces all 'scrollbind' windows to have the same relative 6721 * offset. 6722 * (1998-11-02 16:21:01 R. Edward Ralston <[email protected]>) 6723 */ 6724 /*ARGSUSED*/ 6725 static void 6726 ex_syncbind(eap) 6727 exarg_T *eap; 6728 { 6729 #ifdef FEAT_SCROLLBIND 6730 win_T *wp; 6731 long topline; 6732 long y; 6733 linenr_T old_linenr = curwin->w_cursor.lnum; 6734 6735 setpcmark(); 6736 6737 /* 6738 * determine max topline 6739 */ 6740 if (curwin->w_p_scb) 6741 { 6742 topline = curwin->w_topline; 6743 for (wp = firstwin; wp; wp = wp->w_next) 6744 { 6745 if (wp->w_p_scb && wp->w_buffer) 6746 { 6747 y = wp->w_buffer->b_ml.ml_line_count - p_so; 6748 if (topline > y) 6749 topline = y; 6750 } 6751 } 6752 if (topline < 1) 6753 topline = 1; 6754 } 6755 else 6756 { 6757 topline = 1; 6758 } 6759 6760 6761 /* 6762 * set all scrollbind windows to the same topline 6763 */ 6764 wp = curwin; 6765 for (curwin = firstwin; curwin; curwin = curwin->w_next) 6766 { 6767 if (curwin->w_p_scb) 6768 { 6769 y = topline - curwin->w_topline; 6770 if (y > 0) 6771 scrollup(y, TRUE); 6772 else 6773 scrolldown(-y, TRUE); 6774 curwin->w_scbind_pos = topline; 6775 redraw_later(VALID); 6776 cursor_correct(); 6777 #ifdef FEAT_WINDOWS 6778 curwin->w_redr_status = TRUE; 6779 #endif 6780 } 6781 } 6782 curwin = wp; 6783 if (curwin->w_p_scb) 6784 { 6785 did_syncbind = TRUE; 6786 checkpcmark(); 6787 if (old_linenr != curwin->w_cursor.lnum) 6788 { 6789 char_u ctrl_o[2]; 6790 6791 ctrl_o[0] = Ctrl_O; 6792 ctrl_o[1] = 0; 6793 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE); 6794 } 6795 } 6796 #endif 6797 } 6798 6799 6800 static void 6801 ex_read(eap) 6802 exarg_T *eap; 6803 { 6804 int i; 6805 6806 if (eap->usefilter) /* :r!cmd */ 6807 do_bang(1, eap, FALSE, FALSE, TRUE); 6808 else 6809 { 6810 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL) 6811 return; 6812 6813 #ifdef FEAT_BROWSE 6814 if (cmdmod.browse) 6815 { 6816 char_u *browseFile; 6817 6818 browseFile = do_browse(FALSE, (char_u *)_("Append File"), eap->arg, 6819 NULL, NULL, NULL, curbuf); 6820 if (browseFile != NULL) 6821 { 6822 i = readfile(browseFile, NULL, 6823 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); 6824 vim_free(browseFile); 6825 } 6826 else 6827 i = OK; 6828 } 6829 else 6830 #endif 6831 if (*eap->arg == NUL) 6832 { 6833 if (check_fname() == FAIL) /* check for no file name */ 6834 return; 6835 i = readfile(curbuf->b_ffname, curbuf->b_fname, 6836 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); 6837 } 6838 else 6839 { 6840 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL) 6841 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1); 6842 i = readfile(eap->arg, NULL, 6843 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); 6844 6845 } 6846 if (i == FAIL) 6847 { 6848 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 6849 if (!aborting()) 6850 #endif 6851 EMSG2(_(e_notopen), eap->arg); 6852 } 6853 else 6854 redraw_curbuf_later(VALID); 6855 } 6856 } 6857 6858 /* 6859 * ":cd", ":lcd", ":chdir" and ":lchdir". 6860 */ 6861 static void 6862 ex_cd(eap) 6863 exarg_T *eap; 6864 { 6865 static char_u *prev_dir = NULL; 6866 char_u *new_dir; 6867 char_u *tofree; 6868 6869 new_dir = eap->arg; 6870 #if !defined(UNIX) && !defined(VMS) 6871 /* for non-UNIX ":cd" means: print current directory */ 6872 if (*new_dir == NUL) 6873 ex_pwd(NULL); 6874 else 6875 #endif 6876 { 6877 /* ":cd -": Change to previous directory */ 6878 if (STRCMP(new_dir, "-") == 0) 6879 { 6880 if (prev_dir == NULL) 6881 { 6882 EMSG(_("E186: No previous directory")); 6883 return; 6884 } 6885 new_dir = prev_dir; 6886 } 6887 6888 /* Save current directory for next ":cd -" */ 6889 tofree = prev_dir; 6890 if (mch_dirname(NameBuff, MAXPATHL) == OK) 6891 prev_dir = vim_strsave(NameBuff); 6892 else 6893 prev_dir = NULL; 6894 6895 #if defined(UNIX) || defined(VMS) 6896 /* for UNIX ":cd" means: go to home directory */ 6897 if (*new_dir == NUL) 6898 { 6899 /* use NameBuff for home directory name */ 6900 # ifdef VMS 6901 char_u *p; 6902 6903 p = mch_getenv((char_u *)"SYS$LOGIN"); 6904 if (p == NULL || *p == NUL) /* empty is the same as not set */ 6905 NameBuff[0] = NUL; 6906 else 6907 STRNCPY(NameBuff, p, MAXPATHL); 6908 # else 6909 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL); 6910 # endif 6911 new_dir = NameBuff; 6912 } 6913 #endif 6914 if (new_dir == NULL || vim_chdir(new_dir)) 6915 EMSG(_(e_failed)); 6916 else 6917 { 6918 vim_free(curwin->w_localdir); 6919 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir) 6920 { 6921 /* If still in global directory, need to remember current 6922 * directory as global directory. */ 6923 if (globaldir == NULL && prev_dir != NULL) 6924 globaldir = vim_strsave(prev_dir); 6925 /* Remember this local directory for the window. */ 6926 if (mch_dirname(NameBuff, MAXPATHL) == OK) 6927 curwin->w_localdir = vim_strsave(NameBuff); 6928 } 6929 else 6930 { 6931 /* We are now in the global directory, no need to remember its 6932 * name. */ 6933 vim_free(globaldir); 6934 globaldir = NULL; 6935 curwin->w_localdir = NULL; 6936 } 6937 6938 shorten_fnames(TRUE); 6939 6940 /* Echo the new current directory if the command was typed. */ 6941 if (KeyTyped) 6942 ex_pwd(eap); 6943 } 6944 vim_free(tofree); 6945 } 6946 } 6947 6948 /* 6949 * ":pwd". 6950 */ 6951 /*ARGSUSED*/ 6952 static void 6953 ex_pwd(eap) 6954 exarg_T *eap; 6955 { 6956 if (mch_dirname(NameBuff, MAXPATHL) == OK) 6957 { 6958 #ifdef BACKSLASH_IN_FILENAME 6959 slash_adjust(NameBuff); 6960 #endif 6961 msg(NameBuff); 6962 } 6963 else 6964 EMSG(_("E187: Unknown")); 6965 } 6966 6967 /* 6968 * ":=". 6969 */ 6970 static void 6971 ex_equal(eap) 6972 exarg_T *eap; 6973 { 6974 smsg((char_u *)"%ld", (long)eap->line2); 6975 } 6976 6977 static void 6978 ex_sleep(eap) 6979 exarg_T *eap; 6980 { 6981 int n; 6982 6983 if (cursor_valid()) 6984 { 6985 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled; 6986 if (n >= 0) 6987 windgoto((int)n, curwin->w_wcol); 6988 } 6989 do_sleep(eap->line2 * (*eap->arg == 'm' ? 1L : 1000L)); 6990 } 6991 6992 /* 6993 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second. 6994 */ 6995 void 6996 do_sleep(msec) 6997 long msec; 6998 { 6999 long done; 7000 7001 cursor_on(); 7002 out_flush(); 7003 for (done = 0; !got_int && done < msec; done += 1000L) 7004 { 7005 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE); 7006 ui_breakcheck(); 7007 } 7008 } 7009 7010 static void 7011 do_exmap(eap, isabbrev) 7012 exarg_T *eap; 7013 int isabbrev; 7014 { 7015 int mode; 7016 char_u *cmdp; 7017 7018 cmdp = eap->cmd; 7019 mode = get_map_mode(&cmdp, eap->forceit || isabbrev); 7020 7021 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'), 7022 eap->arg, mode, isabbrev)) 7023 { 7024 case 1: EMSG(_(e_invarg)); 7025 break; 7026 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap)); 7027 break; 7028 } 7029 } 7030 7031 /* 7032 * ":winsize" command (obsolete). 7033 */ 7034 static void 7035 ex_winsize(eap) 7036 exarg_T *eap; 7037 { 7038 int w, h; 7039 char_u *arg = eap->arg; 7040 char_u *p; 7041 7042 w = getdigits(&arg); 7043 arg = skipwhite(arg); 7044 p = arg; 7045 h = getdigits(&arg); 7046 if (*p != NUL && *arg == NUL) 7047 set_shellsize(w, h, TRUE); 7048 else 7049 EMSG(_("E465: :winsize requires two number arguments")); 7050 } 7051 7052 #ifdef FEAT_WINDOWS 7053 static void 7054 ex_wincmd(eap) 7055 exarg_T *eap; 7056 { 7057 int xchar = NUL; 7058 char_u *p; 7059 7060 if (*eap->arg == 'g' || *eap->arg == Ctrl_G) 7061 { 7062 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */ 7063 if (eap->arg[1] == NUL) 7064 { 7065 EMSG(_(e_invarg)); 7066 return; 7067 } 7068 xchar = eap->arg[1]; 7069 p = eap->arg + 2; 7070 } 7071 else 7072 p = eap->arg + 1; 7073 7074 eap->nextcmd = check_nextcmd(p); 7075 p = skipwhite(p); 7076 if (*p != NUL && *p != '"' && eap->nextcmd == NULL) 7077 EMSG(_(e_invarg)); 7078 else 7079 { 7080 /* Pass flags on for ":vertical wincmd ]". */ 7081 postponed_split_flags = cmdmod.split; 7082 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar); 7083 postponed_split_flags = 0; 7084 } 7085 } 7086 #endif 7087 7088 #if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) 7089 /* 7090 * ":winpos". 7091 */ 7092 static void 7093 ex_winpos(eap) 7094 exarg_T *eap; 7095 { 7096 int x, y; 7097 char_u *arg = eap->arg; 7098 char_u *p; 7099 7100 if (*arg == NUL) 7101 { 7102 # ifdef FEAT_GUI 7103 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL) 7104 { 7105 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y); 7106 msg(IObuff); 7107 } 7108 else 7109 # endif 7110 EMSG(_("E188: Obtaining window position not implemented for this platform")); 7111 } 7112 else 7113 { 7114 x = getdigits(&arg); 7115 arg = skipwhite(arg); 7116 p = arg; 7117 y = getdigits(&arg); 7118 if (*p == NUL || *arg != NUL) 7119 { 7120 EMSG(_("E466: :winpos requires two number arguments")); 7121 return; 7122 } 7123 # ifdef FEAT_GUI 7124 if (gui.in_use) 7125 gui_mch_set_winpos(x, y); 7126 else if (gui.starting) 7127 { 7128 /* Remember the coordinates for when the window is opened. */ 7129 gui_win_x = x; 7130 gui_win_y = y; 7131 } 7132 # ifdef HAVE_TGETENT 7133 else 7134 # endif 7135 # endif 7136 # ifdef HAVE_TGETENT 7137 if (*T_CWP) 7138 term_set_winpos(x, y); 7139 # endif 7140 } 7141 } 7142 #endif 7143 7144 /* 7145 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<". 7146 */ 7147 static void 7148 ex_operators(eap) 7149 exarg_T *eap; 7150 { 7151 oparg_T oa; 7152 7153 clear_oparg(&oa); 7154 oa.regname = eap->regname; 7155 oa.start.lnum = eap->line1; 7156 oa.end.lnum = eap->line2; 7157 oa.line_count = eap->line2 - eap->line1 + 1; 7158 oa.motion_type = MLINE; 7159 #ifdef FEAT_VIRTUALEDIT 7160 virtual_op = FALSE; 7161 #endif 7162 if (eap->cmdidx != CMD_yank) /* position cursor for undo */ 7163 { 7164 setpcmark(); 7165 curwin->w_cursor.lnum = eap->line1; 7166 beginline(BL_SOL | BL_FIX); 7167 } 7168 7169 switch (eap->cmdidx) 7170 { 7171 case CMD_delete: 7172 oa.op_type = OP_DELETE; 7173 op_delete(&oa); 7174 break; 7175 7176 case CMD_yank: 7177 oa.op_type = OP_YANK; 7178 (void)op_yank(&oa, FALSE, TRUE); 7179 break; 7180 7181 default: /* CMD_rshift or CMD_lshift */ 7182 if ((eap->cmdidx == CMD_rshift) 7183 #ifdef FEAT_RIGHTLEFT 7184 ^ curwin->w_p_rl 7185 #endif 7186 ) 7187 oa.op_type = OP_RSHIFT; 7188 else 7189 oa.op_type = OP_LSHIFT; 7190 op_shift(&oa, FALSE, eap->amount); 7191 break; 7192 } 7193 #ifdef FEAT_VIRTUALEDIT 7194 virtual_op = MAYBE; 7195 #endif 7196 } 7197 7198 /* 7199 * ":put". 7200 */ 7201 static void 7202 ex_put(eap) 7203 exarg_T *eap; 7204 { 7205 /* ":0put" works like ":1put!". */ 7206 if (eap->line2 == 0) 7207 { 7208 eap->line2 = 1; 7209 eap->forceit = TRUE; 7210 } 7211 curwin->w_cursor.lnum = eap->line2; 7212 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L, PUT_LINE); 7213 } 7214 7215 /* 7216 * Handle ":copy" and ":move". 7217 */ 7218 static void 7219 ex_copymove(eap) 7220 exarg_T *eap; 7221 { 7222 long n; 7223 7224 n = get_address(&eap->arg, FALSE, FALSE); 7225 if (eap->arg == NULL) /* error detected */ 7226 { 7227 eap->nextcmd = NULL; 7228 return; 7229 } 7230 7231 /* 7232 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n' 7233 */ 7234 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count) 7235 { 7236 EMSG(_(e_invaddr)); 7237 return; 7238 } 7239 7240 if (eap->cmdidx == CMD_move) 7241 { 7242 if (do_move(eap->line1, eap->line2, n) == FAIL) 7243 return; 7244 } 7245 else 7246 ex_copy(eap->line1, eap->line2, n); 7247 u_clearline(); 7248 beginline(BL_SOL | BL_FIX); 7249 } 7250 7251 /* 7252 * ":smagic" and ":snomagic". 7253 */ 7254 static void 7255 ex_submagic(eap) 7256 exarg_T *eap; 7257 { 7258 int magic_save = p_magic; 7259 7260 p_magic = (eap->cmdidx == CMD_smagic); 7261 do_sub(eap); 7262 p_magic = magic_save; 7263 } 7264 7265 /* 7266 * ":join". 7267 */ 7268 static void 7269 ex_join(eap) 7270 exarg_T *eap; 7271 { 7272 curwin->w_cursor.lnum = eap->line1; 7273 if (eap->line1 == eap->line2) 7274 { 7275 if (eap->addr_count >= 2) /* :2,2join does nothing */ 7276 return; 7277 if (eap->line2 == curbuf->b_ml.ml_line_count) 7278 { 7279 beep_flush(); 7280 return; 7281 } 7282 ++eap->line2; 7283 } 7284 do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit); 7285 beginline(BL_WHITE | BL_FIX); 7286 } 7287 7288 /* 7289 * ":[addr]@r" or ":[addr]*r": execute register 7290 */ 7291 static void 7292 ex_at(eap) 7293 exarg_T *eap; 7294 { 7295 int c; 7296 7297 curwin->w_cursor.lnum = eap->line2; 7298 7299 #ifdef USE_ON_FLY_SCROLL 7300 dont_scroll = TRUE; /* disallow scrolling here */ 7301 #endif 7302 7303 /* get the register name. No name means to use the previous one */ 7304 c = *eap->arg; 7305 if (c == NUL || (c == '*' && *eap->cmd == '*')) 7306 c = '@'; 7307 /* put the register in mapbuf */ 7308 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL) 7309 beep_flush(); 7310 else 7311 { 7312 int save_efr = exec_from_reg; 7313 7314 exec_from_reg = TRUE; 7315 7316 /* 7317 * Execute from the typeahead buffer. 7318 * Originally this didn't check for the typeahead buffer to be empty, 7319 * thus could read more Ex commands from stdin. It's not clear why, 7320 * it is certainly unexpected. 7321 */ 7322 while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':') 7323 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE); 7324 7325 exec_from_reg = save_efr; 7326 } 7327 } 7328 7329 /* 7330 * ":!". 7331 */ 7332 static void 7333 ex_bang(eap) 7334 exarg_T *eap; 7335 { 7336 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE); 7337 } 7338 7339 /* 7340 * ":undo". 7341 */ 7342 /*ARGSUSED*/ 7343 static void 7344 ex_undo(eap) 7345 exarg_T *eap; 7346 { 7347 u_undo(1); 7348 } 7349 7350 /* 7351 * ":redo". 7352 */ 7353 /*ARGSUSED*/ 7354 static void 7355 ex_redo(eap) 7356 exarg_T *eap; 7357 { 7358 u_redo(1); 7359 } 7360 7361 /* 7362 * ":redir": start/stop redirection. 7363 */ 7364 static void 7365 ex_redir(eap) 7366 exarg_T *eap; 7367 { 7368 char *mode; 7369 char_u *fname; 7370 7371 if (STRICMP(eap->arg, "END") == 0) 7372 close_redir(); 7373 else 7374 { 7375 if (*eap->arg == '>') 7376 { 7377 ++eap->arg; 7378 if (*eap->arg == '>') 7379 { 7380 ++eap->arg; 7381 mode = "a"; 7382 } 7383 else 7384 mode = "w"; 7385 eap->arg = skipwhite(eap->arg); 7386 7387 close_redir(); 7388 7389 /* Expand environment variables and "~/". */ 7390 fname = expand_env_save(eap->arg); 7391 if (fname == NULL) 7392 return; 7393 #ifdef FEAT_BROWSE 7394 if (cmdmod.browse) 7395 { 7396 char_u *browseFile; 7397 7398 browseFile = do_browse(TRUE, (char_u *)_("Save Redirection"), 7399 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf); 7400 if (browseFile == NULL) 7401 return; /* operation cancelled */ 7402 vim_free(fname); 7403 fname = browseFile; 7404 eap->forceit = TRUE; /* since dialog already asked */ 7405 } 7406 #endif 7407 7408 redir_fd = open_exfile(fname, eap->forceit, mode); 7409 vim_free(fname); 7410 } 7411 #ifdef FEAT_EVAL 7412 else if (*eap->arg == '@') 7413 { 7414 /* redirect to a register a-z (resp. A-Z for appending) */ 7415 close_redir(); 7416 ++eap->arg; 7417 if (ASCII_ISALPHA(*eap->arg) 7418 # ifdef FEAT_CLIPBOARD 7419 || *eap->arg == '*' 7420 # endif 7421 || *eap->arg == '"') 7422 { 7423 redir_reg = *eap->arg; 7424 if (islower(redir_reg) 7425 # ifdef FEAT_CLIPBOARD 7426 || redir_reg == '*' 7427 # endif 7428 || redir_reg == '"') 7429 { 7430 /* make register empty */ 7431 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE); 7432 } 7433 } 7434 else 7435 EMSG(_(e_invarg)); 7436 } 7437 #endif 7438 7439 /* TODO: redirect to a buffer */ 7440 7441 /* TODO: redirect to an internal variable */ 7442 7443 else 7444 EMSG(_(e_invarg)); 7445 } 7446 } 7447 7448 /* 7449 * ":redraw": force redraw 7450 */ 7451 static void 7452 ex_redraw(eap) 7453 exarg_T *eap; 7454 { 7455 int r = RedrawingDisabled; 7456 int p = p_lz; 7457 7458 RedrawingDisabled = 0; 7459 p_lz = FALSE; 7460 update_topline(); 7461 update_screen(eap->forceit ? CLEAR : 7462 #ifdef FEAT_VISUAL 7463 VIsual_active ? INVERTED : 7464 #endif 7465 0); 7466 #ifdef FEAT_TITLE 7467 if (need_maketitle) 7468 maketitle(); 7469 #endif 7470 RedrawingDisabled = r; 7471 p_lz = p; 7472 7473 /* Reset msg_didout, so that a message that's there is overwritten. */ 7474 msg_didout = FALSE; 7475 msg_col = 0; 7476 7477 out_flush(); 7478 } 7479 7480 /* 7481 * ":redrawstatus": force redraw of status line(s) 7482 */ 7483 /*ARGSUSED*/ 7484 static void 7485 ex_redrawstatus(eap) 7486 exarg_T *eap; 7487 { 7488 #if defined(FEAT_WINDOWS) 7489 int r = RedrawingDisabled; 7490 int p = p_lz; 7491 7492 RedrawingDisabled = 0; 7493 p_lz = FALSE; 7494 if (eap->forceit) 7495 status_redraw_all(); 7496 else 7497 status_redraw_curbuf(); 7498 update_screen( 7499 # ifdef FEAT_VISUAL 7500 VIsual_active ? INVERTED : 7501 # endif 7502 0); 7503 RedrawingDisabled = r; 7504 p_lz = p; 7505 out_flush(); 7506 #endif 7507 } 7508 7509 static void 7510 close_redir() 7511 { 7512 if (redir_fd != NULL) 7513 { 7514 fclose(redir_fd); 7515 redir_fd = NULL; 7516 } 7517 #ifdef FEAT_EVAL 7518 redir_reg = 0; 7519 #endif 7520 } 7521 7522 #if defined(FEAT_SESSION) && defined(USE_CRNL) 7523 # define MKSESSION_NL 7524 static int mksession_nl = FALSE; /* use NL only in put_eol() */ 7525 #endif 7526 7527 /* 7528 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession". 7529 */ 7530 static void 7531 ex_mkrc(eap) 7532 exarg_T *eap; 7533 { 7534 FILE *fd; 7535 int failed = FALSE; 7536 char_u *fname; 7537 #ifdef FEAT_BROWSE 7538 char_u *browseFile = NULL; 7539 #endif 7540 #ifdef FEAT_SESSION 7541 int view_session = FALSE; 7542 int using_vdir = FALSE; /* using 'viewdir'? */ 7543 char_u *viewFile = NULL; 7544 unsigned *flagp; 7545 #endif 7546 7547 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview) 7548 { 7549 #ifdef FEAT_SESSION 7550 view_session = TRUE; 7551 #else 7552 ex_ni(eap); 7553 return; 7554 #endif 7555 } 7556 7557 #ifdef FEAT_SESSION 7558 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */ 7559 if (eap->cmdidx == CMD_mkview 7560 && (*eap->arg == NUL 7561 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL))) 7562 { 7563 eap->forceit = TRUE; 7564 fname = get_view_file(*eap->arg); 7565 if (fname == NULL) 7566 return; 7567 viewFile = fname; 7568 using_vdir = TRUE; 7569 } 7570 else 7571 #endif 7572 if (*eap->arg != NUL) 7573 fname = eap->arg; 7574 else if (eap->cmdidx == CMD_mkvimrc) 7575 fname = (char_u *)VIMRC_FILE; 7576 #ifdef FEAT_SESSION 7577 else if (eap->cmdidx == CMD_mksession) 7578 fname = (char_u *)SESSION_FILE; 7579 #endif 7580 else 7581 fname = (char_u *)EXRC_FILE; 7582 7583 #ifdef FEAT_BROWSE 7584 if (cmdmod.browse) 7585 { 7586 browseFile = do_browse(TRUE, 7587 # ifdef FEAT_SESSION 7588 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") : 7589 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") : 7590 # endif 7591 (char_u *)_("Save Setup"), 7592 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL); 7593 if (browseFile == NULL) 7594 goto theend; 7595 fname = browseFile; 7596 eap->forceit = TRUE; /* since dialog already asked */ 7597 } 7598 #endif 7599 7600 #if defined(FEAT_SESSION) && defined(vim_mkdir) 7601 /* When using 'viewdir' may have to create the directory. */ 7602 if (using_vdir && !mch_isdir(p_vdir)) 7603 vim_mkdir(p_vdir, 0755); /* ignore errors, open_exfile() will fail */ 7604 #endif 7605 7606 fd = open_exfile(fname, eap->forceit, WRITEBIN); 7607 if (fd != NULL) 7608 { 7609 #ifdef FEAT_SESSION 7610 if (eap->cmdidx == CMD_mkview) 7611 flagp = &vop_flags; 7612 else 7613 flagp = &ssop_flags; 7614 #endif 7615 7616 #ifdef MKSESSION_NL 7617 /* "unix" in 'sessionoptions': use NL line separator */ 7618 if (view_session && (*flagp & SSOP_UNIX)) 7619 mksession_nl = TRUE; 7620 #endif 7621 7622 /* Write the version command for :mkvimrc */ 7623 if (eap->cmdidx == CMD_mkvimrc) 7624 (void)put_line(fd, "version 6.0"); 7625 7626 #ifdef FEAT_SESSION 7627 if (eap->cmdidx != CMD_mkview) 7628 #endif 7629 { 7630 /* Write setting 'compatible' first, because it has side effects. 7631 * For that same reason only do it when needed. */ 7632 if (p_cp) 7633 (void)put_line(fd, "if !&cp | set cp | endif"); 7634 else 7635 (void)put_line(fd, "if &cp | set nocp | endif"); 7636 } 7637 7638 #ifdef FEAT_SESSION 7639 if (!view_session 7640 || (eap->cmdidx == CMD_mksession 7641 && (*flagp & SSOP_OPTIONS))) 7642 #endif 7643 failed |= (makemap(fd, NULL) == FAIL 7644 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL); 7645 7646 #ifdef FEAT_SESSION 7647 if (!failed && view_session) 7648 { 7649 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL) 7650 failed = TRUE; 7651 if (eap->cmdidx == CMD_mksession) 7652 { 7653 char_u dirnow[MAXPATHL]; /* current directory */ 7654 7655 /* 7656 * Change to session file's dir. 7657 */ 7658 if (mch_dirname(dirnow, MAXPATHL) == FAIL 7659 || mch_chdir((char *)dirnow) != 0) 7660 *dirnow = NUL; 7661 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR)) 7662 { 7663 if (vim_chdirfile(fname) == OK) 7664 shorten_fnames(TRUE); 7665 } 7666 else if (*dirnow != NUL 7667 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL) 7668 { 7669 (void)mch_chdir((char *)globaldir); 7670 shorten_fnames(TRUE); 7671 } 7672 7673 failed |= (makeopens(fd, dirnow) == FAIL); 7674 7675 /* restore original dir */ 7676 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR) 7677 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL))) 7678 { 7679 if (mch_chdir((char *)dirnow) != 0) 7680 EMSG(_(e_prev_dir)); 7681 shorten_fnames(TRUE); 7682 } 7683 } 7684 else 7685 { 7686 failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL); 7687 } 7688 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save") 7689 == FAIL) 7690 failed = TRUE; 7691 } 7692 #endif 7693 failed |= fclose(fd); 7694 7695 if (failed) 7696 EMSG(_(e_write)); 7697 #if defined(FEAT_EVAL) && defined(FEAT_SESSION) 7698 else if (eap->cmdidx == CMD_mksession) 7699 { 7700 /* successful session write - set this_session var */ 7701 char_u tbuf[MAXPATHL]; 7702 7703 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK) 7704 set_vim_var_string(VV_THIS_SESSION, tbuf, -1); 7705 } 7706 #endif 7707 #ifdef MKSESSION_NL 7708 mksession_nl = FALSE; 7709 #endif 7710 } 7711 7712 #ifdef FEAT_BROWSE 7713 theend: 7714 vim_free(browseFile); 7715 #endif 7716 #ifdef FEAT_SESSION 7717 vim_free(viewFile); 7718 #endif 7719 } 7720 7721 /* 7722 * Open a file for writing for an Ex command, with some checks. 7723 * Return file descriptor, or NULL on failure. 7724 */ 7725 FILE * 7726 open_exfile(fname, forceit, mode) 7727 char_u *fname; 7728 int forceit; 7729 char *mode; /* "w" for create new file or "a" for append */ 7730 { 7731 FILE *fd; 7732 7733 #ifdef UNIX 7734 /* with Unix it is possible to open a directory */ 7735 if (mch_isdir(fname)) 7736 { 7737 EMSG2(_(e_isadir2), fname); 7738 return NULL; 7739 } 7740 #endif 7741 if (!forceit && *mode != 'a' && vim_fexists(fname)) 7742 { 7743 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname); 7744 return NULL; 7745 } 7746 7747 if ((fd = mch_fopen((char *)fname, mode)) == NULL) 7748 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname); 7749 7750 return fd; 7751 } 7752 7753 /* 7754 * ":mark" and ":k". 7755 */ 7756 static void 7757 ex_mark(eap) 7758 exarg_T *eap; 7759 { 7760 pos_T pos; 7761 7762 if (*eap->arg == NUL) /* No argument? */ 7763 EMSG(_(e_argreq)); 7764 else if (eap->arg[1] != NUL) /* more than one character? */ 7765 EMSG(_(e_trailing)); 7766 else 7767 { 7768 pos = curwin->w_cursor; /* save curwin->w_cursor */ 7769 curwin->w_cursor.lnum = eap->line2; 7770 beginline(BL_WHITE | BL_FIX); 7771 if (setmark(*eap->arg) == FAIL) /* set mark */ 7772 EMSG(_("E191: Argument must be a letter or forward/backward quote")); 7773 curwin->w_cursor = pos; /* restore curwin->w_cursor */ 7774 } 7775 } 7776 7777 /* 7778 * Update w_topline, w_leftcol and the cursor position. 7779 */ 7780 void 7781 update_topline_cursor() 7782 { 7783 check_cursor(); /* put cursor on valid line */ 7784 update_topline(); 7785 if (!curwin->w_p_wrap) 7786 validate_cursor(); 7787 update_curswant(); 7788 } 7789 7790 #ifdef FEAT_EX_EXTRA 7791 /* 7792 * ":normal[!] {commands}": Execute normal mode commands. 7793 */ 7794 static void 7795 ex_normal(eap) 7796 exarg_T *eap; 7797 { 7798 oparg_T oa; 7799 int save_msg_scroll = msg_scroll; 7800 int save_restart_edit = restart_edit; 7801 int save_msg_didout = msg_didout; 7802 int save_State = State; 7803 tasave_T tabuf; 7804 int save_insertmode = p_im; 7805 int save_finish_op = finish_op; 7806 #ifdef FEAT_MBYTE 7807 char_u *arg = NULL; 7808 int l; 7809 char_u *p; 7810 #endif 7811 7812 if (ex_normal_busy >= p_mmd) 7813 { 7814 EMSG(_("E192: Recursive use of :normal too deep")); 7815 return; 7816 } 7817 ++ex_normal_busy; 7818 7819 msg_scroll = FALSE; /* no msg scrolling in Normal mode */ 7820 restart_edit = 0; /* don't go to Insert mode */ 7821 p_im = FALSE; /* don't use 'insertmode' */ 7822 7823 #ifdef FEAT_MBYTE 7824 /* 7825 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do 7826 * this for the K_SPECIAL leading byte, otherwise special keys will not 7827 * work. 7828 */ 7829 if (has_mbyte) 7830 { 7831 int len = 0; 7832 7833 /* Count the number of characters to be escaped. */ 7834 for (p = eap->arg; *p != NUL; ++p) 7835 { 7836 # ifdef FEAT_GUI 7837 if (*p == CSI) /* leadbyte CSI */ 7838 len += 2; 7839 # endif 7840 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l) 7841 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */ 7842 # ifdef FEAT_GUI 7843 || *p == CSI 7844 # endif 7845 ) 7846 len += 2; 7847 } 7848 if (len > 0) 7849 { 7850 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1)); 7851 if (arg != NULL) 7852 { 7853 len = 0; 7854 for (p = eap->arg; *p != NUL; ++p) 7855 { 7856 arg[len++] = *p; 7857 # ifdef FEAT_GUI 7858 if (*p == CSI) 7859 { 7860 arg[len++] = KS_EXTRA; 7861 arg[len++] = (int)KE_CSI; 7862 } 7863 # endif 7864 for (l = (*mb_ptr2len_check)(p) - 1; l > 0; --l) 7865 { 7866 arg[len++] = *++p; 7867 if (*p == K_SPECIAL) 7868 { 7869 arg[len++] = KS_SPECIAL; 7870 arg[len++] = KE_FILLER; 7871 } 7872 # ifdef FEAT_GUI 7873 else if (*p == CSI) 7874 { 7875 arg[len++] = KS_EXTRA; 7876 arg[len++] = (int)KE_CSI; 7877 } 7878 # endif 7879 } 7880 arg[len] = NUL; 7881 } 7882 } 7883 } 7884 } 7885 #endif 7886 7887 /* 7888 * Save the current typeahead. This is required to allow using ":normal" 7889 * from an event handler and makes sure we don't hang when the argument 7890 * ends with half a command. 7891 */ 7892 save_typeahead(&tabuf); 7893 if (tabuf.typebuf_valid) 7894 { 7895 /* 7896 * Repeat the :normal command for each line in the range. When no 7897 * range given, execute it just once, without positioning the cursor 7898 * first. 7899 */ 7900 do 7901 { 7902 clear_oparg(&oa); 7903 finish_op = FALSE; 7904 if (eap->addr_count != 0) 7905 { 7906 curwin->w_cursor.lnum = eap->line1++; 7907 curwin->w_cursor.col = 0; 7908 } 7909 7910 /* 7911 * Stuff the argument into the typeahead buffer. 7912 * Execute normal_cmd() until there is no typeahead left. 7913 */ 7914 ins_typebuf( 7915 #ifdef FEAT_MBYTE 7916 arg != NULL ? arg : 7917 #endif 7918 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, 0, 7919 TRUE, FALSE); 7920 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0)) 7921 && !got_int) 7922 { 7923 update_topline_cursor(); 7924 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */ 7925 } 7926 } 7927 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int); 7928 } 7929 7930 /* Might not return to the main loop when in an event handler. */ 7931 update_topline_cursor(); 7932 7933 /* Restore the previous typeahead. */ 7934 restore_typeahead(&tabuf); 7935 7936 --ex_normal_busy; 7937 msg_scroll = save_msg_scroll; 7938 restart_edit = save_restart_edit; 7939 p_im = save_insertmode; 7940 finish_op = save_finish_op; 7941 msg_didout |= save_msg_didout; /* don't reset msg_didout now */ 7942 7943 /* Restore the state (needed when called from a function executed for 7944 * 'indentexpr'). */ 7945 State = save_State; 7946 #ifdef FEAT_MBYTE 7947 vim_free(arg); 7948 #endif 7949 } 7950 7951 /* 7952 * ":startinsert" 7953 */ 7954 static void 7955 ex_startinsert(eap) 7956 exarg_T *eap; 7957 { 7958 if (eap->forceit) 7959 { 7960 coladvance((colnr_T)MAXCOL); 7961 curwin->w_curswant = MAXCOL; 7962 curwin->w_set_curswant = FALSE; 7963 restart_edit = 'a'; 7964 } 7965 else 7966 { 7967 restart_edit = 'i'; 7968 curwin->w_curswant = 0; /* avoid MAXCOL */ 7969 } 7970 } 7971 7972 /* 7973 * ":stopinsert" 7974 */ 7975 /*ARGSUSED*/ 7976 static void 7977 ex_stopinsert(eap) 7978 exarg_T *eap; 7979 { 7980 restart_edit = 0; 7981 stop_insert_mode = TRUE; 7982 } 7983 #endif 7984 7985 #ifdef FEAT_FIND_ID 7986 static void 7987 ex_checkpath(eap) 7988 exarg_T *eap; 7989 { 7990 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L, 7991 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW, 7992 (linenr_T)1, (linenr_T)MAXLNUM); 7993 } 7994 7995 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) 7996 /* 7997 * ":psearch" 7998 */ 7999 static void 8000 ex_psearch(eap) 8001 exarg_T *eap; 8002 { 8003 g_do_tagpreview = p_pvh; 8004 ex_findpat(eap); 8005 g_do_tagpreview = 0; 8006 } 8007 #endif 8008 8009 static void 8010 ex_findpat(eap) 8011 exarg_T *eap; 8012 { 8013 int whole = TRUE; 8014 long n; 8015 char_u *p; 8016 int action; 8017 8018 switch (cmdnames[eap->cmdidx].cmd_name[2]) 8019 { 8020 case 'e': /* ":psearch", ":isearch" and ":dsearch" */ 8021 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p') 8022 action = ACTION_GOTO; 8023 else 8024 action = ACTION_SHOW; 8025 break; 8026 case 'i': /* ":ilist" and ":dlist" */ 8027 action = ACTION_SHOW_ALL; 8028 break; 8029 case 'u': /* ":ijump" and ":djump" */ 8030 action = ACTION_GOTO; 8031 break; 8032 default: /* ":isplit" and ":dsplit" */ 8033 action = ACTION_SPLIT; 8034 break; 8035 } 8036 8037 n = 1; 8038 if (vim_isdigit(*eap->arg)) /* get count */ 8039 { 8040 n = getdigits(&eap->arg); 8041 eap->arg = skipwhite(eap->arg); 8042 } 8043 if (*eap->arg == '/') /* Match regexp, not just whole words */ 8044 { 8045 whole = FALSE; 8046 ++eap->arg; 8047 p = skip_regexp(eap->arg, '/', p_magic, NULL); 8048 if (*p) 8049 { 8050 *p++ = NUL; 8051 p = skipwhite(p); 8052 8053 /* Check for trailing illegal characters */ 8054 if (!ends_excmd(*p)) 8055 eap->errmsg = e_trailing; 8056 else 8057 eap->nextcmd = check_nextcmd(p); 8058 } 8059 } 8060 if (!eap->skip) 8061 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg), 8062 whole, !eap->forceit, 8063 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY, 8064 n, action, eap->line1, eap->line2); 8065 } 8066 #endif 8067 8068 #ifdef FEAT_WINDOWS 8069 8070 # ifdef FEAT_QUICKFIX 8071 /* 8072 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc. 8073 */ 8074 static void 8075 ex_ptag(eap) 8076 exarg_T *eap; 8077 { 8078 g_do_tagpreview = p_pvh; 8079 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1); 8080 } 8081 8082 /* 8083 * ":pedit" 8084 */ 8085 static void 8086 ex_pedit(eap) 8087 exarg_T *eap; 8088 { 8089 win_T *curwin_save = curwin; 8090 8091 g_do_tagpreview = p_pvh; 8092 prepare_tagpreview(); 8093 keep_help_flag = curwin_save->w_buffer->b_help; 8094 do_exedit(eap, NULL); 8095 keep_help_flag = FALSE; 8096 if (curwin != curwin_save && win_valid(curwin_save)) 8097 { 8098 /* Return cursor to where we were */ 8099 validate_cursor(); 8100 redraw_later(VALID); 8101 win_enter(curwin_save, TRUE); 8102 } 8103 g_do_tagpreview = 0; 8104 } 8105 # endif 8106 8107 /* 8108 * ":stag", ":stselect" and ":stjump". 8109 */ 8110 static void 8111 ex_stag(eap) 8112 exarg_T *eap; 8113 { 8114 postponed_split = -1; 8115 postponed_split_flags = cmdmod.split; 8116 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1); 8117 postponed_split_flags = 0; 8118 } 8119 #endif 8120 8121 /* 8122 * ":tag", ":tselect", ":tjump", ":tnext", etc. 8123 */ 8124 static void 8125 ex_tag(eap) 8126 exarg_T *eap; 8127 { 8128 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name); 8129 } 8130 8131 static void 8132 ex_tag_cmd(eap, name) 8133 exarg_T *eap; 8134 char_u *name; 8135 { 8136 int cmd; 8137 8138 switch (name[1]) 8139 { 8140 case 'j': cmd = DT_JUMP; /* ":tjump" */ 8141 break; 8142 case 's': cmd = DT_SELECT; /* ":tselect" */ 8143 break; 8144 case 'p': cmd = DT_PREV; /* ":tprevious" */ 8145 break; 8146 case 'N': cmd = DT_PREV; /* ":tNext" */ 8147 break; 8148 case 'n': cmd = DT_NEXT; /* ":tnext" */ 8149 break; 8150 case 'o': cmd = DT_POP; /* ":pop" */ 8151 break; 8152 case 'f': /* ":tfirst" */ 8153 case 'r': cmd = DT_FIRST; /* ":trewind" */ 8154 break; 8155 case 'l': cmd = DT_LAST; /* ":tlast" */ 8156 break; 8157 default: /* ":tag" */ 8158 #ifdef FEAT_CSCOPE 8159 if (p_cst) 8160 { 8161 do_cstag(eap); 8162 return; 8163 } 8164 #endif 8165 cmd = DT_TAG; 8166 break; 8167 } 8168 8169 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1, 8170 eap->forceit, TRUE); 8171 } 8172 8173 /* 8174 * Evaluate cmdline variables. 8175 * 8176 * change '%' to curbuf->b_ffname 8177 * '#' to curwin->w_altfile 8178 * '<cword>' to word under the cursor 8179 * '<cWORD>' to WORD under the cursor 8180 * '<cfile>' to path name under the cursor 8181 * '<sfile>' to sourced file name 8182 * '<afile>' to file name for autocommand 8183 * '<abuf>' to buffer number for autocommand 8184 * '<amatch>' to matching name for autocommand 8185 * 8186 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be 8187 * "" for error without a message) and NULL is returned. 8188 * Returns an allocated string if a valid match was found. 8189 * Returns NULL if no match was found. "usedlen" then still contains the 8190 * number of characters to skip. 8191 */ 8192 char_u * 8193 eval_vars(src, usedlen, lnump, errormsg, srcstart) 8194 char_u *src; /* pointer into commandline */ 8195 int *usedlen; /* characters after src that are used */ 8196 linenr_T *lnump; /* line number for :e command, or NULL */ 8197 char_u **errormsg; /* pointer to error message */ 8198 char_u *srcstart; /* beginning of valid memory for src */ 8199 { 8200 int i; 8201 char_u *s; 8202 char_u *result; 8203 char_u *resultbuf = NULL; 8204 int resultlen; 8205 buf_T *buf; 8206 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */ 8207 int spec_idx; 8208 #ifdef FEAT_MODIFY_FNAME 8209 int skip_mod = FALSE; 8210 #endif 8211 static char *(spec_str[]) = 8212 { 8213 "%", 8214 #define SPEC_PERC 0 8215 "#", 8216 #define SPEC_HASH 1 8217 "<cword>", /* cursor word */ 8218 #define SPEC_CWORD 2 8219 "<cWORD>", /* cursor WORD */ 8220 #define SPEC_CCWORD 3 8221 "<cfile>", /* cursor path name */ 8222 #define SPEC_CFILE 4 8223 "<sfile>", /* ":so" file name */ 8224 #define SPEC_SFILE 5 8225 #ifdef FEAT_AUTOCMD 8226 "<afile>", /* autocommand file name */ 8227 # define SPEC_AFILE 6 8228 "<abuf>", /* autocommand buffer number */ 8229 # define SPEC_ABUF 7 8230 "<amatch>", /* autocommand match name */ 8231 # define SPEC_AMATCH 8 8232 #endif 8233 #ifdef FEAT_CLIENTSERVER 8234 "<client>" 8235 # define SPEC_CLIENT 9 8236 #endif 8237 }; 8238 #define SPEC_COUNT (sizeof(spec_str) / sizeof(char *)) 8239 8240 #if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER) 8241 char_u strbuf[30]; 8242 #endif 8243 8244 *errormsg = NULL; 8245 8246 /* 8247 * Check if there is something to do. 8248 */ 8249 for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx) 8250 { 8251 *usedlen = (int)STRLEN(spec_str[spec_idx]); 8252 if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0) 8253 break; 8254 } 8255 if (spec_idx == SPEC_COUNT) /* no match */ 8256 { 8257 *usedlen = 1; 8258 return NULL; 8259 } 8260 8261 /* 8262 * Skip when preceded with a backslash "\%" and "\#". 8263 * Note: In "\\%" the % is also not recognized! 8264 */ 8265 if (src > srcstart && src[-1] == '\\') 8266 { 8267 *usedlen = 0; 8268 STRCPY(src - 1, src); /* remove backslash */ 8269 return NULL; 8270 } 8271 8272 /* 8273 * word or WORD under cursor 8274 */ 8275 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD) 8276 { 8277 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ? 8278 (FIND_IDENT|FIND_STRING) : FIND_STRING); 8279 if (resultlen == 0) 8280 { 8281 *errormsg = (char_u *)""; 8282 return NULL; 8283 } 8284 } 8285 8286 /* 8287 * '#': Alternate file name 8288 * '%': Current file name 8289 * File name under the cursor 8290 * File name for autocommand 8291 * and following modifiers 8292 */ 8293 else 8294 { 8295 switch (spec_idx) 8296 { 8297 case SPEC_PERC: /* '%': current file */ 8298 if (curbuf->b_fname == NULL) 8299 { 8300 result = (char_u *)""; 8301 valid = 0; /* Must have ":p:h" to be valid */ 8302 } 8303 else 8304 #ifdef RISCOS 8305 /* Always use the full path for RISC OS if possible. */ 8306 result = curbuf->b_ffname; 8307 if (result == NULL) 8308 result = curbuf->b_fname; 8309 #else 8310 result = curbuf->b_fname; 8311 #endif 8312 break; 8313 8314 case SPEC_HASH: /* '#' or "#99": alternate file */ 8315 if (src[1] == '#') /* "##": the argument list */ 8316 { 8317 result = arg_all(); 8318 resultbuf = result; 8319 *usedlen = 2; 8320 #ifdef FEAT_MODIFY_FNAME 8321 skip_mod = TRUE; 8322 #endif 8323 break; 8324 } 8325 s = src + 1; 8326 i = (int)getdigits(&s); 8327 *usedlen = (int)(s - src); /* length of what we expand */ 8328 8329 buf = buflist_findnr(i); 8330 if (buf == NULL) 8331 { 8332 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'"); 8333 return NULL; 8334 } 8335 if (lnump != NULL) 8336 *lnump = ECMD_LAST; 8337 if (buf->b_fname == NULL) 8338 { 8339 result = (char_u *)""; 8340 valid = 0; /* Must have ":p:h" to be valid */ 8341 } 8342 else 8343 result = buf->b_fname; 8344 break; 8345 8346 #ifdef FEAT_SEARCHPATH 8347 case SPEC_CFILE: /* file name under cursor */ 8348 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L); 8349 if (result == NULL) 8350 { 8351 *errormsg = (char_u *)""; 8352 return NULL; 8353 } 8354 resultbuf = result; /* remember allocated string */ 8355 break; 8356 #endif 8357 8358 #ifdef FEAT_AUTOCMD 8359 case SPEC_AFILE: /* file name for autocommand */ 8360 result = autocmd_fname; 8361 if (result == NULL) 8362 { 8363 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\""); 8364 return NULL; 8365 } 8366 break; 8367 8368 case SPEC_ABUF: /* buffer number for autocommand */ 8369 if (autocmd_bufnr <= 0) 8370 { 8371 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\""); 8372 return NULL; 8373 } 8374 sprintf((char *)strbuf, "%d", autocmd_bufnr); 8375 result = strbuf; 8376 break; 8377 8378 case SPEC_AMATCH: /* match name for autocommand */ 8379 result = autocmd_match; 8380 if (result == NULL) 8381 { 8382 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\""); 8383 return NULL; 8384 } 8385 break; 8386 8387 #endif 8388 case SPEC_SFILE: /* file name for ":so" command */ 8389 result = sourcing_name; 8390 if (result == NULL) 8391 { 8392 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\""); 8393 return NULL; 8394 } 8395 break; 8396 #if defined(FEAT_CLIENTSERVER) 8397 case SPEC_CLIENT: /* Source of last submitted input */ 8398 sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow); 8399 result = strbuf; 8400 break; 8401 #endif 8402 } 8403 8404 resultlen = (int)STRLEN(result); /* length of new string */ 8405 if (src[*usedlen] == '<') /* remove the file name extension */ 8406 { 8407 ++*usedlen; 8408 #ifdef RISCOS 8409 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result)) 8410 #else 8411 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result)) 8412 #endif 8413 resultlen = (int)(s - result); 8414 } 8415 #ifdef FEAT_MODIFY_FNAME 8416 else if (!skip_mod) 8417 { 8418 valid |= modify_fname(src, usedlen, &result, &resultbuf, 8419 &resultlen); 8420 if (result == NULL) 8421 { 8422 *errormsg = (char_u *)""; 8423 return NULL; 8424 } 8425 } 8426 #endif 8427 } 8428 8429 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH) 8430 { 8431 if (valid != VALID_HEAD + VALID_PATH) 8432 /* xgettext:no-c-format */ 8433 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\""); 8434 else 8435 *errormsg = (char_u *)_("E500: Evaluates to an empty string"); 8436 result = NULL; 8437 } 8438 else 8439 result = vim_strnsave(result, resultlen); 8440 vim_free(resultbuf); 8441 return result; 8442 } 8443 8444 /* 8445 * Concatenate all files in the argument list, separated by spaces, and return 8446 * it in one allocated string. 8447 * Spaces and backslashes in the file names are escaped with a backslash. 8448 * Returns NULL when out of memory. 8449 */ 8450 static char_u * 8451 arg_all() 8452 { 8453 int len; 8454 int idx; 8455 char_u *retval = NULL; 8456 char_u *p; 8457 8458 /* 8459 * Do this loop two times: 8460 * first time: compute the total length 8461 * second time: concatenate the names 8462 */ 8463 for (;;) 8464 { 8465 len = 0; 8466 for (idx = 0; idx < ARGCOUNT; ++idx) 8467 { 8468 p = alist_name(&ARGLIST[idx]); 8469 if (p != NULL) 8470 { 8471 if (len > 0) 8472 { 8473 /* insert a space in between names */ 8474 if (retval != NULL) 8475 retval[len] = ' '; 8476 ++len; 8477 } 8478 for ( ; *p != NUL; ++p) 8479 { 8480 if (*p == ' ' || *p == '\\') 8481 { 8482 /* insert a backslash */ 8483 if (retval != NULL) 8484 retval[len] = '\\'; 8485 ++len; 8486 } 8487 if (retval != NULL) 8488 retval[len] = *p; 8489 ++len; 8490 } 8491 } 8492 } 8493 8494 /* second time: break here */ 8495 if (retval != NULL) 8496 { 8497 retval[len] = NUL; 8498 break; 8499 } 8500 8501 /* allocate memory */ 8502 retval = alloc(len + 1); 8503 if (retval == NULL) 8504 break; 8505 } 8506 8507 return retval; 8508 } 8509 8510 #if defined(FEAT_AUTOCMD) || defined(PROTO) 8511 /* 8512 * Expand the <sfile> string in "arg". 8513 * 8514 * Returns an allocated string, or NULL for any error. 8515 */ 8516 char_u * 8517 expand_sfile(arg) 8518 char_u *arg; 8519 { 8520 char_u *errormsg; 8521 int len; 8522 char_u *result; 8523 char_u *newres; 8524 char_u *repl; 8525 int srclen; 8526 char_u *p; 8527 8528 result = vim_strsave(arg); 8529 if (result == NULL) 8530 return NULL; 8531 8532 for (p = result; *p; ) 8533 { 8534 if (STRNCMP(p, "<sfile>", 7) != 0) 8535 ++p; 8536 else 8537 { 8538 /* replace "<sfile>" with the sourced file name, and do ":" stuff */ 8539 repl = eval_vars(p, &srclen, NULL, &errormsg, result); 8540 if (errormsg != NULL) 8541 { 8542 if (*errormsg) 8543 emsg(errormsg); 8544 vim_free(result); 8545 return NULL; 8546 } 8547 if (repl == NULL) /* no match (cannot happen) */ 8548 { 8549 p += srclen; 8550 continue; 8551 } 8552 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1; 8553 newres = alloc(len); 8554 if (newres == NULL) 8555 { 8556 vim_free(repl); 8557 vim_free(result); 8558 return NULL; 8559 } 8560 mch_memmove(newres, result, (size_t)(p - result)); 8561 STRCPY(newres + (p - result), repl); 8562 len = (int)STRLEN(newres); 8563 STRCAT(newres, p + srclen); 8564 vim_free(repl); 8565 vim_free(result); 8566 result = newres; 8567 p = newres + len; /* continue after the match */ 8568 } 8569 } 8570 8571 return result; 8572 } 8573 #endif 8574 8575 #ifdef FEAT_SESSION 8576 static int ses_winsizes __ARGS((FILE *fd, int restore_size)); 8577 static int ses_win_rec __ARGS((FILE *fd, frame_T *fr)); 8578 static frame_T *ses_skipframe __ARGS((frame_T *fr)); 8579 static int ses_do_frame __ARGS((frame_T *fr)); 8580 static int ses_do_win __ARGS((win_T *wp)); 8581 static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp)); 8582 static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp)); 8583 static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp)); 8584 8585 /* 8586 * Write openfile commands for the current buffers to an .exrc file. 8587 * Return FAIL on error, OK otherwise. 8588 */ 8589 static int 8590 makeopens(fd, dirnow) 8591 FILE *fd; 8592 char_u *dirnow; /* Current directory name */ 8593 { 8594 buf_T *buf; 8595 int only_save_windows = TRUE; 8596 int nr; 8597 int cnr = 1; 8598 int restore_size = TRUE; 8599 win_T *wp; 8600 char_u *sname; 8601 win_T *edited_win = NULL; 8602 8603 if (ssop_flags & SSOP_BUFFERS) 8604 only_save_windows = FALSE; /* Save ALL buffers */ 8605 8606 /* 8607 * Begin by setting the this_session variable, and then other 8608 * sessionable variables. 8609 */ 8610 #ifdef FEAT_EVAL 8611 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL) 8612 return FAIL; 8613 if (ssop_flags & SSOP_GLOBALS) 8614 if (store_session_globals(fd) == FAIL) 8615 return FAIL; 8616 #endif 8617 8618 /* 8619 * Close all windows but one. 8620 */ 8621 if (put_line(fd, "silent only") == FAIL) 8622 return FAIL; 8623 8624 /* 8625 * Now a :cd command to the session directory or the current directory 8626 */ 8627 if (ssop_flags & SSOP_SESDIR) 8628 { 8629 if (put_line(fd, "exe \"cd \" . expand(\"<sfile>:p:h\")") == FAIL) 8630 return FAIL; 8631 } 8632 else if (ssop_flags & SSOP_CURDIR) 8633 { 8634 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow); 8635 if (sname == NULL 8636 || fprintf(fd, "cd %s", sname) < 0 || put_eol(fd) == FAIL) 8637 return FAIL; 8638 vim_free(sname); 8639 } 8640 8641 /* 8642 * Now save the current files, current buffer first. 8643 */ 8644 if (put_line(fd, "set shortmess=aoO") == FAIL) 8645 return FAIL; 8646 8647 /* Now put the other buffers into the buffer list */ 8648 for (buf = firstbuf; buf != NULL; buf = buf->b_next) 8649 { 8650 if (!(only_save_windows && buf->b_nwindows == 0) 8651 && !(buf->b_help && !(ssop_flags & SSOP_HELP)) 8652 && buf->b_fname != NULL 8653 && buf->b_p_bl) 8654 { 8655 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L 8656 : buf->b_wininfo->wi_fpos.lnum) < 0 8657 || ses_fname(fd, buf, &ssop_flags) == FAIL) 8658 return FAIL; 8659 } 8660 } 8661 8662 /* the global argument list */ 8663 if (ses_arglist(fd, "args", &global_alist.al_ga, 8664 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL) 8665 return FAIL; 8666 8667 if (ssop_flags & SSOP_RESIZE) 8668 { 8669 /* Note: after the restore we still check it worked!*/ 8670 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0 8671 || put_eol(fd) == FAIL) 8672 return FAIL; 8673 } 8674 8675 #ifdef FEAT_GUI 8676 if (gui.in_use && (ssop_flags & SSOP_WINPOS)) 8677 { 8678 int x, y; 8679 8680 if (gui_mch_get_winpos(&x, &y) == OK) 8681 { 8682 /* Note: after the restore we still check it worked!*/ 8683 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL) 8684 return FAIL; 8685 } 8686 } 8687 #endif 8688 8689 /* 8690 * Before creating the window layout, try loading one file. If this is 8691 * aborted we don't end up with a number of useless windows. 8692 * This may have side effects! (e.g., compressed or network file). 8693 */ 8694 for (wp = firstwin; wp != NULL; wp = wp->w_next) 8695 { 8696 if (ses_do_win(wp) 8697 && wp->w_buffer->b_ffname != NULL 8698 && !wp->w_buffer->b_help 8699 #ifdef FEAT_QUICKFIX 8700 && !bt_nofile(wp->w_buffer) 8701 #endif 8702 ) 8703 { 8704 if (fputs("edit ", fd) < 0 8705 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL) 8706 return FAIL; 8707 if (!wp->w_arg_idx_invalid) 8708 edited_win = wp; 8709 break; 8710 } 8711 } 8712 8713 /* 8714 * Save current window layout. 8715 */ 8716 if (put_line(fd, "set splitbelow splitright") == FAIL) 8717 return FAIL; 8718 if (ses_win_rec(fd, topframe) == FAIL) 8719 return FAIL; 8720 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL) 8721 return FAIL; 8722 if (!p_spr && put_line(fd, "set nosplitright") == FAIL) 8723 return FAIL; 8724 8725 /* 8726 * Check if window sizes can be restored (no windows omitted). 8727 * Remember the window number of the current window after restoring. 8728 */ 8729 nr = 0; 8730 for (wp = firstwin; wp != NULL; wp = W_NEXT(wp)) 8731 { 8732 if (ses_do_win(wp)) 8733 ++nr; 8734 else 8735 restore_size = FALSE; 8736 if (curwin == wp) 8737 cnr = nr; 8738 } 8739 8740 /* Go to the first window. */ 8741 if (put_line(fd, "wincmd t") == FAIL) 8742 return FAIL; 8743 8744 /* 8745 * If more than one window, see if sizes can be restored. 8746 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being 8747 * resized when moving between windows. 8748 * Do this before restoring the view, so that the topline and the cursor 8749 * can be set. This is done again below. 8750 */ 8751 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL) 8752 return FAIL; 8753 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL) 8754 return FAIL; 8755 8756 /* 8757 * Restore the view of the window (options, file, cursor, etc.). 8758 */ 8759 for (wp = firstwin; wp != NULL; wp = wp->w_next) 8760 { 8761 if (!ses_do_win(wp)) 8762 continue; 8763 if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL) 8764 return FAIL; 8765 if (nr > 1 && put_line(fd, "wincmd w") == FAIL) 8766 return FAIL; 8767 } 8768 8769 /* 8770 * Restore cursor to the current window if it's not the first one. 8771 */ 8772 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL)) 8773 return FAIL; 8774 8775 /* 8776 * Restore window sizes again after jumping around in windows, because the 8777 * current window has a minimum size while others may not. 8778 */ 8779 if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL) 8780 return FAIL; 8781 8782 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */ 8783 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s", 8784 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL) 8785 return FAIL; 8786 8787 /* 8788 * Lastly, execute the x.vim file if it exists. 8789 */ 8790 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL 8791 || put_line(fd, "if file_readable(s:sx)") == FAIL 8792 || put_line(fd, " exe \"source \" . s:sx") == FAIL 8793 || put_line(fd, "endif") == FAIL) 8794 return FAIL; 8795 8796 return OK; 8797 } 8798 8799 static int 8800 ses_winsizes(fd, restore_size) 8801 FILE *fd; 8802 int restore_size; 8803 { 8804 int n = 0; 8805 win_T *wp; 8806 8807 if (restore_size && (ssop_flags & SSOP_WINSIZE)) 8808 { 8809 for (wp = firstwin; wp != NULL; wp = wp->w_next) 8810 { 8811 if (!ses_do_win(wp)) 8812 continue; 8813 ++n; 8814 8815 /* restore height when not full height */ 8816 if (wp->w_height + wp->w_status_height < topframe->fr_height 8817 && (fprintf(fd, 8818 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)", 8819 n, (long)wp->w_height, Rows / 2, Rows) < 0 8820 || put_eol(fd) == FAIL)) 8821 return FAIL; 8822 8823 /* restore width when not full width */ 8824 if (wp->w_width < Columns && (fprintf(fd, 8825 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)", 8826 n, (long)wp->w_width, Columns / 2, Columns) < 0 8827 || put_eol(fd) == FAIL)) 8828 return FAIL; 8829 } 8830 } 8831 else 8832 { 8833 /* Just equalise window sizes */ 8834 if (put_line(fd, "wincmd =") == FAIL) 8835 return FAIL; 8836 } 8837 return OK; 8838 } 8839 8840 /* 8841 * Write commands to "fd" to recursively create windows for frame "fr", 8842 * horizontally and vertically split. 8843 * After the commands the last window in the frame is the current window. 8844 * Returns FAIL when writing the commands to "fd" fails. 8845 */ 8846 static int 8847 ses_win_rec(fd, fr) 8848 FILE *fd; 8849 frame_T *fr; 8850 { 8851 frame_T *frc; 8852 int count = 0; 8853 8854 if (fr->fr_layout != FR_LEAF) 8855 { 8856 /* Find first frame that's not skipped and then create a window for 8857 * each following one (first frame is already there). */ 8858 frc = ses_skipframe(fr->fr_child); 8859 if (frc != NULL) 8860 while ((frc = ses_skipframe(frc->fr_next)) != NULL) 8861 { 8862 /* Make window as big as possible so that we have lots of room 8863 * to split. */ 8864 if (put_line(fd, "wincmd _ | wincmd |") == FAIL 8865 || put_line(fd, fr->fr_layout == FR_COL 8866 ? "split" : "vsplit") == FAIL) 8867 return FAIL; 8868 ++count; 8869 } 8870 8871 /* Go back to the first window. */ 8872 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL 8873 ? "%dwincmd k" : "%dwincmd h", count) < 0 8874 || put_eol(fd) == FAIL)) 8875 return FAIL; 8876 8877 /* Recursively create frames/windows in each window of this column or 8878 * row. */ 8879 frc = ses_skipframe(fr->fr_child); 8880 while (frc != NULL) 8881 { 8882 ses_win_rec(fd, frc); 8883 frc = ses_skipframe(frc->fr_next); 8884 /* Go to next window. */ 8885 if (frc != NULL && put_line(fd, "wincmd w") == FAIL) 8886 return FAIL; 8887 } 8888 } 8889 return OK; 8890 } 8891 8892 /* 8893 * Skip frames that don't contain windows we want to save in the Session. 8894 * Returns NULL when there none. 8895 */ 8896 static frame_T * 8897 ses_skipframe(fr) 8898 frame_T *fr; 8899 { 8900 frame_T *frc; 8901 8902 for (frc = fr; frc != NULL; frc = frc->fr_next) 8903 if (ses_do_frame(frc)) 8904 break; 8905 return frc; 8906 } 8907 8908 /* 8909 * Return TRUE if frame "fr" has a window somewhere that we want to save in 8910 * the Session. 8911 */ 8912 static int 8913 ses_do_frame(fr) 8914 frame_T *fr; 8915 { 8916 frame_T *frc; 8917 8918 if (fr->fr_layout == FR_LEAF) 8919 return ses_do_win(fr->fr_win); 8920 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next) 8921 if (ses_do_frame(frc)) 8922 return TRUE; 8923 return FALSE; 8924 } 8925 8926 /* 8927 * Return non-zero if window "wp" is to be stored in the Session. 8928 */ 8929 static int 8930 ses_do_win(wp) 8931 win_T *wp; 8932 { 8933 if (wp->w_buffer->b_fname == NULL 8934 #ifdef FEAT_QUICKFIX 8935 /* When 'buftype' is "nofile" can't restore the window contents. */ 8936 || bt_nofile(wp->w_buffer) 8937 #endif 8938 ) 8939 return (ssop_flags & SSOP_BLANK); 8940 if (wp->w_buffer->b_help) 8941 return (ssop_flags & SSOP_HELP); 8942 return TRUE; 8943 } 8944 8945 /* 8946 * Write commands to "fd" to restore the view of a window. 8947 * Caller must make sure 'scrolloff' is zero. 8948 */ 8949 static int 8950 put_view(fd, wp, add_edit, flagp) 8951 FILE *fd; 8952 win_T *wp; 8953 int add_edit; /* add ":edit" command to view */ 8954 unsigned *flagp; /* vop_flags or ssop_flags */ 8955 { 8956 win_T *save_curwin; 8957 int f; 8958 int do_cursor; 8959 8960 /* Always restore cursor position for ":mksession". For ":mkview" only 8961 * when 'viewoptions' contains "cursor". */ 8962 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR); 8963 8964 /* 8965 * Local argument list. 8966 */ 8967 if (wp->w_alist == &global_alist) 8968 { 8969 if (put_line(fd, "argglobal") == FAIL) 8970 return FAIL; 8971 } 8972 else 8973 { 8974 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga, 8975 flagp == &vop_flags 8976 || !(*flagp & SSOP_CURDIR) 8977 || wp->w_localdir != NULL, flagp) == FAIL) 8978 return FAIL; 8979 } 8980 8981 /* Only when part of a session: restore the argument index. */ 8982 if (wp->w_arg_idx != 0 && flagp == &ssop_flags) 8983 { 8984 if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0 8985 || put_eol(fd) == FAIL) 8986 return FAIL; 8987 } 8988 8989 /* Edit the file. Skip this when ":next" already did it. */ 8990 if (add_edit && (wp->w_arg_idx == 0 || flagp != &ssop_flags 8991 || wp->w_arg_idx_invalid)) 8992 { 8993 /* 8994 * Load the file. 8995 */ 8996 if (wp->w_buffer->b_ffname != NULL 8997 #ifdef FEAT_QUICKFIX 8998 && !bt_nofile(wp->w_buffer) 8999 #endif 9000 ) 9001 { 9002 /* 9003 * Editing a file in this buffer: use ":edit file". 9004 * This may have side effects! (e.g., compressed or network file). 9005 */ 9006 if (fputs("edit ", fd) < 0 9007 || ses_fname(fd, wp->w_buffer, flagp) == FAIL) 9008 return FAIL; 9009 } 9010 else 9011 { 9012 /* No file in this buffer, just make it empty. */ 9013 if (put_line(fd, "enew") == FAIL) 9014 return FAIL; 9015 #ifdef FEAT_QUICKFIX 9016 if (wp->w_buffer->b_ffname != NULL) 9017 { 9018 /* The buffer does have a name, but it's not a file name. */ 9019 if (fputs("file ", fd) < 0 9020 || ses_fname(fd, wp->w_buffer, flagp) == FAIL) 9021 return FAIL; 9022 } 9023 #endif 9024 do_cursor = FALSE; 9025 } 9026 } 9027 9028 /* 9029 * Local mappings and abbreviations. 9030 */ 9031 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS)) 9032 && makemap(fd, wp->w_buffer) == FAIL) 9033 return FAIL; 9034 9035 /* 9036 * Local options. Need to go to the window temporarily. 9037 * Store only local values when using ":mkview" and when ":mksession" is 9038 * used and 'sessionoptions' doesn't include "options". 9039 * Some folding options are always stored when "folds" is included, 9040 * otherwise the folds would not be restored correctly. 9041 */ 9042 save_curwin = curwin; 9043 curwin = wp; 9044 curbuf = curwin->w_buffer; 9045 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS)) 9046 f = makeset(fd, OPT_LOCAL, 9047 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS)); 9048 #ifdef FEAT_FOLDING 9049 else if (*flagp & SSOP_FOLDS) 9050 f = makefoldset(fd); 9051 #endif 9052 else 9053 f = OK; 9054 curwin = save_curwin; 9055 curbuf = curwin->w_buffer; 9056 if (f == FAIL) 9057 return FAIL; 9058 9059 #ifdef FEAT_FOLDING 9060 /* 9061 * Save Folds when 'buftype' is empty and for help files. 9062 */ 9063 if ((*flagp & SSOP_FOLDS) 9064 && wp->w_buffer->b_ffname != NULL 9065 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)) 9066 { 9067 if (put_folds(fd, wp) == FAIL) 9068 return FAIL; 9069 } 9070 #endif 9071 9072 /* 9073 * Set the cursor after creating folds, since that moves the cursor. 9074 */ 9075 if (do_cursor) 9076 { 9077 9078 /* Restore the cursor line in the file and relatively in the 9079 * window. Don't use "G", it changes the jumplist. */ 9080 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)", 9081 (long)wp->w_cursor.lnum, 9082 (long)(wp->w_cursor.lnum - wp->w_topline), 9083 (long)wp->w_height / 2, (long)wp->w_height) < 0 9084 || put_eol(fd) == FAIL 9085 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL 9086 || put_line(fd, "exe s:l") == FAIL 9087 || put_line(fd, "normal! zt") == FAIL 9088 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0 9089 || put_eol(fd) == FAIL) 9090 return FAIL; 9091 /* Restore the cursor column and left offset when not wrapping. */ 9092 if (wp->w_cursor.col == 0) 9093 { 9094 if (put_line(fd, "normal! 0") == FAIL) 9095 return FAIL; 9096 } 9097 else 9098 { 9099 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0) 9100 { 9101 if (fprintf(fd, 9102 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)", 9103 (long)wp->w_cursor.col, 9104 (long)(wp->w_cursor.col - wp->w_leftcol), 9105 (long)wp->w_width / 2, (long)wp->w_width) < 0 9106 || put_eol(fd) == FAIL 9107 || put_line(fd, "if s:c > 0") == FAIL 9108 || fprintf(fd, 9109 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'", 9110 (long)wp->w_cursor.col) < 0 9111 || put_eol(fd) == FAIL 9112 || put_line(fd, "else") == FAIL 9113 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0 9114 || put_eol(fd) == FAIL 9115 || put_line(fd, "endif") == FAIL) 9116 return FAIL; 9117 } 9118 else 9119 { 9120 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0 9121 || put_eol(fd) == FAIL) 9122 return FAIL; 9123 } 9124 } 9125 } 9126 9127 /* 9128 * Local directory. 9129 */ 9130 if (wp->w_localdir != NULL) 9131 { 9132 if (fputs("lcd ", fd) < 0 9133 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL 9134 || put_eol(fd) == FAIL) 9135 return FAIL; 9136 } 9137 9138 return OK; 9139 } 9140 9141 /* 9142 * Write an argument list to the session file. 9143 * Returns FAIL if writing fails. 9144 */ 9145 static int 9146 ses_arglist(fd, cmd, gap, fullname, flagp) 9147 FILE *fd; 9148 char *cmd; 9149 garray_T *gap; 9150 int fullname; /* TRUE: use full path name */ 9151 unsigned *flagp; 9152 { 9153 int i; 9154 char_u buf[MAXPATHL]; 9155 char_u *s; 9156 9157 if (gap->ga_len == 0) 9158 return put_line(fd, "silent! argdel *"); 9159 if (fputs(cmd, fd) < 0) 9160 return FAIL; 9161 for (i = 0; i < gap->ga_len; ++i) 9162 { 9163 /* NULL file names are skipped (only happens when out of memory). */ 9164 s = alist_name(&((aentry_T *)gap->ga_data)[i]); 9165 if (s != NULL) 9166 { 9167 if (fullname) 9168 { 9169 (void)vim_FullName(s, buf, MAXPATHL, FALSE); 9170 s = buf; 9171 } 9172 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL) 9173 return FAIL; 9174 } 9175 } 9176 return put_eol(fd); 9177 } 9178 9179 /* 9180 * Write a buffer name to the session file. 9181 * Also ends the line. 9182 * Returns FAIL if writing fails. 9183 */ 9184 static int 9185 ses_fname(fd, buf, flagp) 9186 FILE *fd; 9187 buf_T *buf; 9188 unsigned *flagp; 9189 { 9190 char_u *name; 9191 9192 /* Use the short file name if the current directory is known at the time 9193 * the session file will be sourced. Don't do this for ":mkview", we 9194 * don't know the current directory. */ 9195 if (buf->b_sfname != NULL 9196 && flagp == &ssop_flags 9197 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))) 9198 name = buf->b_sfname; 9199 else 9200 name = buf->b_ffname; 9201 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL) 9202 return FAIL; 9203 return OK; 9204 } 9205 9206 /* 9207 * Write a file name to the session file. 9208 * Takes care of the "slash" option in 'sessionoptions' and escapes special 9209 * characters. 9210 * Returns FAIL if writing fails. 9211 */ 9212 static int 9213 ses_put_fname(fd, name, flagp) 9214 FILE *fd; 9215 char_u *name; 9216 unsigned *flagp; 9217 { 9218 char_u *sname; 9219 int retval = OK; 9220 int c; 9221 9222 sname = home_replace_save(NULL, name); 9223 if (sname != NULL) 9224 name = sname; 9225 while (*name != NUL) 9226 { 9227 #ifdef FEAT_MBYTE 9228 { 9229 int l; 9230 9231 if (has_mbyte && (l = (*mb_ptr2len_check)(name)) > 1) 9232 { 9233 /* copy a multibyte char */ 9234 while (--l >= 0) 9235 { 9236 if (putc(*name, fd) != *name) 9237 retval = FAIL; 9238 ++name; 9239 } 9240 continue; 9241 } 9242 } 9243 #endif 9244 c = *name++; 9245 if (c == '\\' && (*flagp & SSOP_SLASH)) 9246 /* change a backslash to a forward slash */ 9247 c = '/'; 9248 else if ((vim_strchr(escape_chars, c) != NULL 9249 #ifdef BACKSLASH_IN_FILENAME 9250 && c != '\\' 9251 #endif 9252 ) || c == '#' || c == '%') 9253 { 9254 /* escape a special character with a backslash */ 9255 if (putc('\\', fd) != '\\') 9256 retval = FAIL; 9257 } 9258 if (putc(c, fd) != c) 9259 retval = FAIL; 9260 } 9261 vim_free(sname); 9262 return retval; 9263 } 9264 9265 /* 9266 * ":loadview [nr]" 9267 */ 9268 static void 9269 ex_loadview(eap) 9270 exarg_T *eap; 9271 { 9272 char_u *fname; 9273 9274 fname = get_view_file(*eap->arg); 9275 if (fname != NULL) 9276 { 9277 do_source(fname, FALSE, FALSE); 9278 vim_free(fname); 9279 } 9280 } 9281 9282 /* 9283 * Get the name of the view file for the current buffer. 9284 */ 9285 static char_u * 9286 get_view_file(c) 9287 int c; 9288 { 9289 int len = 0; 9290 char_u *p, *s; 9291 char_u *retval; 9292 char_u *sname; 9293 9294 if (curbuf->b_ffname == NULL) 9295 { 9296 EMSG(_(e_noname)); 9297 return NULL; 9298 } 9299 sname = home_replace_save(NULL, curbuf->b_ffname); 9300 if (sname == NULL) 9301 return NULL; 9302 9303 /* 9304 * We want a file name without separators, because we're not going to make 9305 * a directory. 9306 * "normal" path separator -> "=+" 9307 * "=" -> "==" 9308 * ":" path separator -> "=-" 9309 */ 9310 for (p = sname; *p; ++p) 9311 if (*p == '=' || vim_ispathsep(*p)) 9312 ++len; 9313 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9)); 9314 if (retval != NULL) 9315 { 9316 STRCPY(retval, p_vdir); 9317 add_pathsep(retval); 9318 s = retval + STRLEN(retval); 9319 for (p = sname; *p; ++p) 9320 { 9321 if (*p == '=') 9322 { 9323 *s++ = '='; 9324 *s++ = '='; 9325 } 9326 else if (vim_ispathsep(*p)) 9327 { 9328 *s++ = '='; 9329 #ifdef MACOS_CLASSIC /* TODO: Is it also needed for MACOS_X? (Dany) */ 9330 *s++ = '+'; 9331 #else 9332 # if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \ 9333 || defined(VMS) 9334 if (*p == ':') 9335 *s++ = '-'; 9336 else 9337 # endif 9338 *s++ = '+'; 9339 #endif 9340 } 9341 else 9342 *s++ = *p; 9343 } 9344 *s++ = '='; 9345 *s++ = c; 9346 STRCPY(s, ".vim"); 9347 } 9348 9349 vim_free(sname); 9350 return retval; 9351 } 9352 9353 #endif /* FEAT_SESSION */ 9354 9355 /* 9356 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession". 9357 * Return FAIL for a write error. 9358 */ 9359 int 9360 put_eol(fd) 9361 FILE *fd; 9362 { 9363 if ( 9364 #ifdef USE_CRNL 9365 ( 9366 # ifdef MKSESSION_NL 9367 !mksession_nl && 9368 # endif 9369 (putc('\r', fd) < 0)) || 9370 #endif 9371 (putc('\n', fd) < 0)) 9372 return FAIL; 9373 return OK; 9374 } 9375 9376 /* 9377 * Write a line to "fd". 9378 * Return FAIL for a write error. 9379 */ 9380 int 9381 put_line(fd, s) 9382 FILE *fd; 9383 char *s; 9384 { 9385 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL) 9386 return FAIL; 9387 return OK; 9388 } 9389 9390 #ifdef FEAT_VIMINFO 9391 /* 9392 * ":rviminfo" and ":wviminfo". 9393 */ 9394 static void 9395 ex_viminfo(eap) 9396 exarg_T *eap; 9397 { 9398 char_u *save_viminfo; 9399 9400 save_viminfo = p_viminfo; 9401 if (*p_viminfo == NUL) 9402 p_viminfo = (char_u *)"'100"; 9403 if (eap->cmdidx == CMD_rviminfo) 9404 { 9405 if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL) 9406 EMSG(_("E195: Cannot open viminfo file for reading")); 9407 } 9408 else 9409 write_viminfo(eap->arg, eap->forceit); 9410 p_viminfo = save_viminfo; 9411 } 9412 #endif 9413 9414 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO) 9415 void 9416 dialog_msg(buff, format, fname) 9417 char_u *buff; 9418 char *format; 9419 char_u *fname; 9420 { 9421 int len; 9422 9423 if (fname == NULL) 9424 fname = (char_u *)_("Untitled"); 9425 len = (int)STRLEN(format) + (int)STRLEN(fname); 9426 if (len >= IOSIZE) 9427 sprintf((char *)buff, format, (int)(IOSIZE - STRLEN(format)), fname); 9428 else 9429 sprintf((char *)buff, format, (int)STRLEN(fname), fname); 9430 } 9431 #endif 9432 9433 /* 9434 * ":behave {mswin,xterm}" 9435 */ 9436 static void 9437 ex_behave(eap) 9438 exarg_T *eap; 9439 { 9440 if (STRCMP(eap->arg, "mswin") == 0) 9441 { 9442 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0); 9443 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0); 9444 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0); 9445 set_option_value((char_u *)"keymodel", 0L, 9446 (char_u *)"startsel,stopsel", 0); 9447 } 9448 else if (STRCMP(eap->arg, "xterm") == 0) 9449 { 9450 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0); 9451 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0); 9452 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0); 9453 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0); 9454 } 9455 else 9456 EMSG2(_(e_invarg2), eap->arg); 9457 } 9458 9459 #ifdef FEAT_AUTOCMD 9460 static int filetype_detect = FALSE; 9461 static int filetype_plugin = FALSE; 9462 static int filetype_indent = FALSE; 9463 9464 /* 9465 * ":filetype [plugin] [indent] {on,off,detect}" 9466 * on: Load the filetype.vim file to install autocommands for file types. 9467 * off: Load the ftoff.vim file to remove all autocommands for file types. 9468 * plugin on: load filetype.vim and ftplugin.vim 9469 * plugin off: load ftplugof.vim 9470 * indent on: load filetype.vim and indent.vim 9471 * indent off: load indoff.vim 9472 */ 9473 static void 9474 ex_filetype(eap) 9475 exarg_T *eap; 9476 { 9477 char_u *arg = eap->arg; 9478 int plugin = FALSE; 9479 int indent = FALSE; 9480 9481 if (*eap->arg == NUL) 9482 { 9483 /* Print current status. */ 9484 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s", 9485 filetype_detect ? "ON" : "OFF", 9486 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF", 9487 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF"); 9488 return; 9489 } 9490 9491 /* Accept "plugin" and "indent" in any order. */ 9492 for (;;) 9493 { 9494 if (STRNCMP(arg, "plugin", 6) == 0) 9495 { 9496 plugin = TRUE; 9497 arg = skipwhite(arg + 6); 9498 continue; 9499 } 9500 if (STRNCMP(arg, "indent", 6) == 0) 9501 { 9502 indent = TRUE; 9503 arg = skipwhite(arg + 6); 9504 continue; 9505 } 9506 break; 9507 } 9508 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0) 9509 { 9510 if (*arg == 'o' || !filetype_detect) 9511 { 9512 cmd_runtime((char_u *)FILETYPE_FILE, TRUE); 9513 filetype_detect = TRUE; 9514 if (plugin) 9515 { 9516 cmd_runtime((char_u *)FTPLUGIN_FILE, TRUE); 9517 filetype_plugin = TRUE; 9518 } 9519 if (indent) 9520 { 9521 cmd_runtime((char_u *)INDENT_FILE, TRUE); 9522 filetype_indent = TRUE; 9523 } 9524 } 9525 if (*arg == 'd') 9526 { 9527 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE); 9528 do_modelines(); 9529 } 9530 } 9531 else if (STRCMP(arg, "off") == 0) 9532 { 9533 if (plugin || indent) 9534 { 9535 if (plugin) 9536 { 9537 cmd_runtime((char_u *)FTPLUGOF_FILE, TRUE); 9538 filetype_plugin = FALSE; 9539 } 9540 if (indent) 9541 { 9542 cmd_runtime((char_u *)INDOFF_FILE, TRUE); 9543 filetype_indent = FALSE; 9544 } 9545 } 9546 else 9547 { 9548 cmd_runtime((char_u *)FTOFF_FILE, TRUE); 9549 filetype_detect = FALSE; 9550 } 9551 } 9552 else 9553 EMSG2(_(e_invarg2), arg); 9554 } 9555 9556 /* 9557 * ":setfiletype {name}" 9558 */ 9559 static void 9560 ex_setfiletype(eap) 9561 exarg_T *eap; 9562 { 9563 if (!did_filetype) 9564 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL); 9565 } 9566 #endif 9567 9568 /*ARGSUSED*/ 9569 static void 9570 ex_digraphs(eap) 9571 exarg_T *eap; 9572 { 9573 #ifdef FEAT_DIGRAPHS 9574 if (*eap->arg != NUL) 9575 putdigraph(eap->arg); 9576 else 9577 listdigraphs(); 9578 #else 9579 EMSG(_("E196: No digraphs in this version")); 9580 #endif 9581 } 9582 9583 static void 9584 ex_set(eap) 9585 exarg_T *eap; 9586 { 9587 int flags = 0; 9588 9589 if (eap->cmdidx == CMD_setlocal) 9590 flags = OPT_LOCAL; 9591 else if (eap->cmdidx == CMD_setglobal) 9592 flags = OPT_GLOBAL; 9593 #if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE) 9594 if (cmdmod.browse && flags == 0) 9595 ex_options(eap); 9596 else 9597 #endif 9598 (void)do_set(eap->arg, flags); 9599 } 9600 9601 #ifdef FEAT_SEARCH_EXTRA 9602 /* 9603 * ":nohlsearch" 9604 */ 9605 /*ARGSUSED*/ 9606 static void 9607 ex_nohlsearch(eap) 9608 exarg_T *eap; 9609 { 9610 no_hlsearch = TRUE; 9611 redraw_all_later(NOT_VALID); 9612 } 9613 9614 /* 9615 * ":match {group} {pattern}" 9616 * Sets nextcmd to the start of the next command, if any. Also called when 9617 * skipping commands to find the next command. 9618 */ 9619 static void 9620 ex_match(eap) 9621 exarg_T *eap; 9622 { 9623 char_u *p; 9624 char_u *end; 9625 int c; 9626 9627 /* First clear any old pattern. */ 9628 if (!eap->skip) 9629 { 9630 vim_free(curwin->w_match.regprog); 9631 curwin->w_match.regprog = NULL; 9632 redraw_later(NOT_VALID); /* always need a redraw */ 9633 } 9634 9635 if (ends_excmd(*eap->arg)) 9636 end = eap->arg; 9637 else if ((STRNICMP(eap->arg, "none", 4) == 0 9638 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4])))) 9639 end = eap->arg + 4; 9640 else 9641 { 9642 p = skiptowhite(eap->arg); 9643 if (!eap->skip) 9644 { 9645 curwin->w_match_id = syn_namen2id(eap->arg, (int)(p - eap->arg)); 9646 if (curwin->w_match_id == 0) 9647 { 9648 EMSG2(_(e_nogroup), eap->arg); 9649 return; 9650 } 9651 } 9652 p = skipwhite(p); 9653 if (*p == NUL) 9654 { 9655 /* There must be two arguments. */ 9656 EMSG2(_(e_invarg2), eap->arg); 9657 return; 9658 } 9659 end = skip_regexp(p + 1, *p, TRUE, NULL); 9660 if (!eap->skip) 9661 { 9662 if (*end != NUL && !ends_excmd(*skipwhite(end + 1))) 9663 { 9664 eap->errmsg = e_trailing; 9665 return; 9666 } 9667 9668 c = *end; 9669 *end = NUL; 9670 curwin->w_match.regprog = vim_regcomp(p + 1, RE_MAGIC); 9671 *end = c; 9672 if (curwin->w_match.regprog == NULL) 9673 { 9674 EMSG2(_(e_invarg2), p); 9675 return; 9676 } 9677 } 9678 } 9679 eap->nextcmd = find_nextcmd(end); 9680 } 9681 #endif 9682 9683 #ifdef FEAT_CRYPT 9684 /* 9685 * ":X": Get crypt key 9686 */ 9687 /*ARGSUSED*/ 9688 static void 9689 ex_X(eap) 9690 exarg_T *eap; 9691 { 9692 (void)get_crypt_key(TRUE, TRUE); 9693 } 9694 #endif 9695 9696 #ifdef FEAT_FOLDING 9697 static void 9698 ex_fold(eap) 9699 exarg_T *eap; 9700 { 9701 if (foldManualAllowed(TRUE)) 9702 foldCreate(eap->line1, eap->line2); 9703 } 9704 9705 static void 9706 ex_foldopen(eap) 9707 exarg_T *eap; 9708 { 9709 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen, 9710 eap->forceit, FALSE); 9711 } 9712 9713 static void 9714 ex_folddo(eap) 9715 exarg_T *eap; 9716 { 9717 linenr_T lnum; 9718 9719 /* First set the marks for all lines closed/open. */ 9720 for (lnum = eap->line1; lnum <= eap->line2; ++lnum) 9721 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed)) 9722 ml_setmarked(lnum); 9723 9724 /* Execute the command on the marked lines. */ 9725 global_exe(eap->arg); 9726 ml_clearmarked(); /* clear rest of the marks */ 9727 } 9728 #endif 9729