1 /* vi:set ts=8 sts=4 sw=4 noet: 2 * 3 * VIM - Vi IMproved by Bram Moolenaar 4 * 5 * Do ":help uganda" in Vim to read copying and usage conditions. 6 * Do ":help credits" in Vim to see a list of people who contributed. 7 * See README.txt for an overview of the Vim source code. 8 */ 9 10 /* 11 * autocmd.c: Autocommand related functions 12 */ 13 14 #include "vim.h" 15 16 /* 17 * The autocommands are stored in a list for each event. 18 * Autocommands for the same pattern, that are consecutive, are joined 19 * together, to avoid having to match the pattern too often. 20 * The result is an array of Autopat lists, which point to AutoCmd lists: 21 * 22 * last_autopat[0] -----------------------------+ 23 * V 24 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL 25 * Autopat.cmds Autopat.cmds 26 * | | 27 * V V 28 * AutoCmd.next AutoCmd.next 29 * | | 30 * V V 31 * AutoCmd.next NULL 32 * | 33 * V 34 * NULL 35 * 36 * last_autopat[1] --------+ 37 * V 38 * first_autopat[1] --> Autopat.next --> NULL 39 * Autopat.cmds 40 * | 41 * V 42 * AutoCmd.next 43 * | 44 * V 45 * NULL 46 * etc. 47 * 48 * The order of AutoCmds is important, this is the order in which they were 49 * defined and will have to be executed. 50 */ 51 typedef struct AutoCmd 52 { 53 char_u *cmd; // The command to be executed (NULL 54 // when command has been removed). 55 char nested; // If autocommands nest here. 56 char last; // last command in list 57 #ifdef FEAT_EVAL 58 sctx_T script_ctx; // script context where defined 59 #endif 60 struct AutoCmd *next; // next AutoCmd in list 61 } AutoCmd; 62 63 typedef struct AutoPat 64 { 65 struct AutoPat *next; // Next AutoPat in AutoPat list; MUST 66 // be the first entry. 67 char_u *pat; // pattern as typed (NULL when pattern 68 // has been removed) 69 regprog_T *reg_prog; // compiled regprog for pattern 70 AutoCmd *cmds; // list of commands to do 71 int group; // group ID 72 int patlen; // strlen() of pat 73 int buflocal_nr; // !=0 for buffer-local AutoPat 74 char allow_dirs; // Pattern may match whole path 75 char last; // last pattern for apply_autocmds() 76 } AutoPat; 77 78 static struct event_name 79 { 80 char *name; // event name 81 event_T event; // event number 82 } event_names[] = 83 { 84 {"BufAdd", EVENT_BUFADD}, 85 {"BufCreate", EVENT_BUFADD}, 86 {"BufDelete", EVENT_BUFDELETE}, 87 {"BufEnter", EVENT_BUFENTER}, 88 {"BufFilePost", EVENT_BUFFILEPOST}, 89 {"BufFilePre", EVENT_BUFFILEPRE}, 90 {"BufHidden", EVENT_BUFHIDDEN}, 91 {"BufLeave", EVENT_BUFLEAVE}, 92 {"BufNew", EVENT_BUFNEW}, 93 {"BufNewFile", EVENT_BUFNEWFILE}, 94 {"BufRead", EVENT_BUFREADPOST}, 95 {"BufReadCmd", EVENT_BUFREADCMD}, 96 {"BufReadPost", EVENT_BUFREADPOST}, 97 {"BufReadPre", EVENT_BUFREADPRE}, 98 {"BufUnload", EVENT_BUFUNLOAD}, 99 {"BufWinEnter", EVENT_BUFWINENTER}, 100 {"BufWinLeave", EVENT_BUFWINLEAVE}, 101 {"BufWipeout", EVENT_BUFWIPEOUT}, 102 {"BufWrite", EVENT_BUFWRITEPRE}, 103 {"BufWritePost", EVENT_BUFWRITEPOST}, 104 {"BufWritePre", EVENT_BUFWRITEPRE}, 105 {"BufWriteCmd", EVENT_BUFWRITECMD}, 106 {"CmdlineChanged", EVENT_CMDLINECHANGED}, 107 {"CmdlineEnter", EVENT_CMDLINEENTER}, 108 {"CmdlineLeave", EVENT_CMDLINELEAVE}, 109 {"CmdwinEnter", EVENT_CMDWINENTER}, 110 {"CmdwinLeave", EVENT_CMDWINLEAVE}, 111 {"CmdUndefined", EVENT_CMDUNDEFINED}, 112 {"ColorScheme", EVENT_COLORSCHEME}, 113 {"ColorSchemePre", EVENT_COLORSCHEMEPRE}, 114 {"CompleteDone", EVENT_COMPLETEDONE}, 115 {"CursorHold", EVENT_CURSORHOLD}, 116 {"CursorHoldI", EVENT_CURSORHOLDI}, 117 {"CursorMoved", EVENT_CURSORMOVED}, 118 {"CursorMovedI", EVENT_CURSORMOVEDI}, 119 {"DiffUpdated", EVENT_DIFFUPDATED}, 120 {"DirChanged", EVENT_DIRCHANGED}, 121 {"EncodingChanged", EVENT_ENCODINGCHANGED}, 122 {"ExitPre", EVENT_EXITPRE}, 123 {"FileEncoding", EVENT_ENCODINGCHANGED}, 124 {"FileAppendPost", EVENT_FILEAPPENDPOST}, 125 {"FileAppendPre", EVENT_FILEAPPENDPRE}, 126 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, 127 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, 128 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, 129 {"FileChangedRO", EVENT_FILECHANGEDRO}, 130 {"FileReadPost", EVENT_FILEREADPOST}, 131 {"FileReadPre", EVENT_FILEREADPRE}, 132 {"FileReadCmd", EVENT_FILEREADCMD}, 133 {"FileType", EVENT_FILETYPE}, 134 {"FileWritePost", EVENT_FILEWRITEPOST}, 135 {"FileWritePre", EVENT_FILEWRITEPRE}, 136 {"FileWriteCmd", EVENT_FILEWRITECMD}, 137 {"FilterReadPost", EVENT_FILTERREADPOST}, 138 {"FilterReadPre", EVENT_FILTERREADPRE}, 139 {"FilterWritePost", EVENT_FILTERWRITEPOST}, 140 {"FilterWritePre", EVENT_FILTERWRITEPRE}, 141 {"FocusGained", EVENT_FOCUSGAINED}, 142 {"FocusLost", EVENT_FOCUSLOST}, 143 {"FuncUndefined", EVENT_FUNCUNDEFINED}, 144 {"GUIEnter", EVENT_GUIENTER}, 145 {"GUIFailed", EVENT_GUIFAILED}, 146 {"InsertChange", EVENT_INSERTCHANGE}, 147 {"InsertEnter", EVENT_INSERTENTER}, 148 {"InsertLeave", EVENT_INSERTLEAVE}, 149 {"InsertCharPre", EVENT_INSERTCHARPRE}, 150 {"MenuPopup", EVENT_MENUPOPUP}, 151 {"OptionSet", EVENT_OPTIONSET}, 152 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, 153 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, 154 {"QuitPre", EVENT_QUITPRE}, 155 {"RemoteReply", EVENT_REMOTEREPLY}, 156 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, 157 {"ShellCmdPost", EVENT_SHELLCMDPOST}, 158 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, 159 {"SourceCmd", EVENT_SOURCECMD}, 160 {"SourcePre", EVENT_SOURCEPRE}, 161 {"SourcePost", EVENT_SOURCEPOST}, 162 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, 163 {"StdinReadPost", EVENT_STDINREADPOST}, 164 {"StdinReadPre", EVENT_STDINREADPRE}, 165 {"SwapExists", EVENT_SWAPEXISTS}, 166 {"Syntax", EVENT_SYNTAX}, 167 {"TabNew", EVENT_TABNEW}, 168 {"TabClosed", EVENT_TABCLOSED}, 169 {"TabEnter", EVENT_TABENTER}, 170 {"TabLeave", EVENT_TABLEAVE}, 171 {"TermChanged", EVENT_TERMCHANGED}, 172 {"TerminalOpen", EVENT_TERMINALOPEN}, 173 {"TermResponse", EVENT_TERMRESPONSE}, 174 {"TextChanged", EVENT_TEXTCHANGED}, 175 {"TextChangedI", EVENT_TEXTCHANGEDI}, 176 {"TextChangedP", EVENT_TEXTCHANGEDP}, 177 {"User", EVENT_USER}, 178 {"VimEnter", EVENT_VIMENTER}, 179 {"VimLeave", EVENT_VIMLEAVE}, 180 {"VimLeavePre", EVENT_VIMLEAVEPRE}, 181 {"WinNew", EVENT_WINNEW}, 182 {"WinEnter", EVENT_WINENTER}, 183 {"WinLeave", EVENT_WINLEAVE}, 184 {"VimResized", EVENT_VIMRESIZED}, 185 {"TextYankPost", EVENT_TEXTYANKPOST}, 186 {NULL, (event_T)0} 187 }; 188 189 static AutoPat *first_autopat[NUM_EVENTS] = 190 { 191 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 192 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 193 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 194 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 195 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 196 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 197 }; 198 199 static AutoPat *last_autopat[NUM_EVENTS] = 200 { 201 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 202 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 203 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 204 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 205 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 206 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 207 }; 208 209 #define AUGROUP_DEFAULT -1 // default autocmd group 210 #define AUGROUP_ERROR -2 // erroneous autocmd group 211 #define AUGROUP_ALL -3 // all autocmd groups 212 213 /* 214 * struct used to keep status while executing autocommands for an event. 215 */ 216 typedef struct AutoPatCmd 217 { 218 AutoPat *curpat; // next AutoPat to examine 219 AutoCmd *nextcmd; // next AutoCmd to execute 220 int group; // group being used 221 char_u *fname; // fname to match with 222 char_u *sfname; // sfname to match with 223 char_u *tail; // tail of fname 224 event_T event; // current event 225 int arg_bufnr; // Initially equal to <abuf>, set to zero when 226 // buf is deleted. 227 struct AutoPatCmd *next; // chain of active apc-s for auto-invalidation 228 } AutoPatCmd; 229 230 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ 231 232 /* 233 * augroups stores a list of autocmd group names. 234 */ 235 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; 236 #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) 237 /* use get_deleted_augroup() to get this */ 238 static char_u *deleted_augroup = NULL; 239 240 /* 241 * Set by the apply_autocmds_group function if the given event is equal to 242 * EVENT_FILETYPE. Used by the readfile function in order to determine if 243 * EVENT_BUFREADPOST triggered the EVENT_FILETYPE. 244 * 245 * Relying on this value requires one to reset it prior calling 246 * apply_autocmds_group. 247 */ 248 int au_did_filetype INIT(= FALSE); 249 250 /* 251 * The ID of the current group. Group 0 is the default one. 252 */ 253 static int current_augroup = AUGROUP_DEFAULT; 254 255 static int au_need_clean = FALSE; /* need to delete marked patterns */ 256 257 static char_u *event_nr2name(event_T event); 258 static int au_get_grouparg(char_u **argp); 259 static int do_autocmd_event(event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group); 260 static int apply_autocmds_group(event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap); 261 static void auto_next_pat(AutoPatCmd *apc, int stop_at_last); 262 static int au_find_group(char_u *name); 263 264 static event_T last_event; 265 static int last_group; 266 static int autocmd_blocked = 0; /* block all autocmds */ 267 268 static char_u * 269 get_deleted_augroup(void) 270 { 271 if (deleted_augroup == NULL) 272 deleted_augroup = (char_u *)_("--Deleted--"); 273 return deleted_augroup; 274 } 275 276 /* 277 * Show the autocommands for one AutoPat. 278 */ 279 static void 280 show_autocmd(AutoPat *ap, event_T event) 281 { 282 AutoCmd *ac; 283 284 // Check for "got_int" (here and at various places below), which is set 285 // when "q" has been hit for the "--more--" prompt 286 if (got_int) 287 return; 288 if (ap->pat == NULL) // pattern has been removed 289 return; 290 291 msg_putchar('\n'); 292 if (got_int) 293 return; 294 if (event != last_event || ap->group != last_group) 295 { 296 if (ap->group != AUGROUP_DEFAULT) 297 { 298 if (AUGROUP_NAME(ap->group) == NULL) 299 msg_puts_attr((char *)get_deleted_augroup(), HL_ATTR(HLF_E)); 300 else 301 msg_puts_attr((char *)AUGROUP_NAME(ap->group), HL_ATTR(HLF_T)); 302 msg_puts(" "); 303 } 304 msg_puts_attr((char *)event_nr2name(event), HL_ATTR(HLF_T)); 305 last_event = event; 306 last_group = ap->group; 307 msg_putchar('\n'); 308 if (got_int) 309 return; 310 } 311 msg_col = 4; 312 msg_outtrans(ap->pat); 313 314 for (ac = ap->cmds; ac != NULL; ac = ac->next) 315 { 316 if (ac->cmd != NULL) // skip removed commands 317 { 318 if (msg_col >= 14) 319 msg_putchar('\n'); 320 msg_col = 14; 321 if (got_int) 322 return; 323 msg_outtrans(ac->cmd); 324 #ifdef FEAT_EVAL 325 if (p_verbose > 0) 326 last_set_msg(ac->script_ctx); 327 #endif 328 if (got_int) 329 return; 330 if (ac->next != NULL) 331 { 332 msg_putchar('\n'); 333 if (got_int) 334 return; 335 } 336 } 337 } 338 } 339 340 /* 341 * Mark an autocommand pattern for deletion. 342 */ 343 static void 344 au_remove_pat(AutoPat *ap) 345 { 346 VIM_CLEAR(ap->pat); 347 ap->buflocal_nr = -1; 348 au_need_clean = TRUE; 349 } 350 351 /* 352 * Mark all commands for a pattern for deletion. 353 */ 354 static void 355 au_remove_cmds(AutoPat *ap) 356 { 357 AutoCmd *ac; 358 359 for (ac = ap->cmds; ac != NULL; ac = ac->next) 360 VIM_CLEAR(ac->cmd); 361 au_need_clean = TRUE; 362 } 363 364 /* 365 * Cleanup autocommands and patterns that have been deleted. 366 * This is only done when not executing autocommands. 367 */ 368 static void 369 au_cleanup(void) 370 { 371 AutoPat *ap, **prev_ap; 372 AutoCmd *ac, **prev_ac; 373 event_T event; 374 375 if (autocmd_busy || !au_need_clean) 376 return; 377 378 // loop over all events 379 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; 380 event = (event_T)((int)event + 1)) 381 { 382 // loop over all autocommand patterns 383 prev_ap = &(first_autopat[(int)event]); 384 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) 385 { 386 // loop over all commands for this pattern 387 prev_ac = &(ap->cmds); 388 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) 389 { 390 // remove the command if the pattern is to be deleted or when 391 // the command has been marked for deletion 392 if (ap->pat == NULL || ac->cmd == NULL) 393 { 394 *prev_ac = ac->next; 395 vim_free(ac->cmd); 396 vim_free(ac); 397 } 398 else 399 prev_ac = &(ac->next); 400 } 401 402 // remove the pattern if it has been marked for deletion 403 if (ap->pat == NULL) 404 { 405 if (ap->next == NULL) 406 { 407 if (prev_ap == &(first_autopat[(int)event])) 408 last_autopat[(int)event] = NULL; 409 else 410 // this depends on the "next" field being the first in 411 // the struct 412 last_autopat[(int)event] = (AutoPat *)prev_ap; 413 } 414 *prev_ap = ap->next; 415 vim_regfree(ap->reg_prog); 416 vim_free(ap); 417 } 418 else 419 prev_ap = &(ap->next); 420 } 421 } 422 423 au_need_clean = FALSE; 424 } 425 426 /* 427 * Called when buffer is freed, to remove/invalidate related buffer-local 428 * autocmds. 429 */ 430 void 431 aubuflocal_remove(buf_T *buf) 432 { 433 AutoPat *ap; 434 event_T event; 435 AutoPatCmd *apc; 436 437 // invalidate currently executing autocommands 438 for (apc = active_apc_list; apc; apc = apc->next) 439 if (buf->b_fnum == apc->arg_bufnr) 440 apc->arg_bufnr = 0; 441 442 // invalidate buflocals looping through events 443 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; 444 event = (event_T)((int)event + 1)) 445 // loop over all autocommand patterns 446 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) 447 if (ap->buflocal_nr == buf->b_fnum) 448 { 449 au_remove_pat(ap); 450 if (p_verbose >= 6) 451 { 452 verbose_enter(); 453 smsg(_("auto-removing autocommand: %s <buffer=%d>"), 454 event_nr2name(event), buf->b_fnum); 455 verbose_leave(); 456 } 457 } 458 au_cleanup(); 459 } 460 461 /* 462 * Add an autocmd group name. 463 * Return its ID. Returns AUGROUP_ERROR (< 0) for error. 464 */ 465 static int 466 au_new_group(char_u *name) 467 { 468 int i; 469 470 i = au_find_group(name); 471 if (i == AUGROUP_ERROR) // the group doesn't exist yet, add it 472 { 473 // First try using a free entry. 474 for (i = 0; i < augroups.ga_len; ++i) 475 if (AUGROUP_NAME(i) == NULL) 476 break; 477 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) 478 return AUGROUP_ERROR; 479 480 AUGROUP_NAME(i) = vim_strsave(name); 481 if (AUGROUP_NAME(i) == NULL) 482 return AUGROUP_ERROR; 483 if (i == augroups.ga_len) 484 ++augroups.ga_len; 485 } 486 487 return i; 488 } 489 490 static void 491 au_del_group(char_u *name) 492 { 493 int i; 494 495 i = au_find_group(name); 496 if (i == AUGROUP_ERROR) // the group doesn't exist 497 semsg(_("E367: No such group: \"%s\""), name); 498 else if (i == current_augroup) 499 emsg(_("E936: Cannot delete the current group")); 500 else 501 { 502 event_T event; 503 AutoPat *ap; 504 int in_use = FALSE; 505 506 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; 507 event = (event_T)((int)event + 1)) 508 { 509 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) 510 if (ap->group == i && ap->pat != NULL) 511 { 512 give_warning((char_u *)_("W19: Deleting augroup that is still in use"), TRUE); 513 in_use = TRUE; 514 event = NUM_EVENTS; 515 break; 516 } 517 } 518 vim_free(AUGROUP_NAME(i)); 519 if (in_use) 520 { 521 AUGROUP_NAME(i) = get_deleted_augroup(); 522 } 523 else 524 AUGROUP_NAME(i) = NULL; 525 } 526 } 527 528 /* 529 * Find the ID of an autocmd group name. 530 * Return its ID. Returns AUGROUP_ERROR (< 0) for error. 531 */ 532 static int 533 au_find_group(char_u *name) 534 { 535 int i; 536 537 for (i = 0; i < augroups.ga_len; ++i) 538 if (AUGROUP_NAME(i) != NULL && AUGROUP_NAME(i) != get_deleted_augroup() 539 && STRCMP(AUGROUP_NAME(i), name) == 0) 540 return i; 541 return AUGROUP_ERROR; 542 } 543 544 /* 545 * Return TRUE if augroup "name" exists. 546 */ 547 int 548 au_has_group(char_u *name) 549 { 550 return au_find_group(name) != AUGROUP_ERROR; 551 } 552 553 /* 554 * ":augroup {name}". 555 */ 556 void 557 do_augroup(char_u *arg, int del_group) 558 { 559 int i; 560 561 if (del_group) 562 { 563 if (*arg == NUL) 564 emsg(_(e_argreq)); 565 else 566 au_del_group(arg); 567 } 568 else if (STRICMP(arg, "end") == 0) // ":aug end": back to group 0 569 current_augroup = AUGROUP_DEFAULT; 570 else if (*arg) // ":aug xxx": switch to group xxx 571 { 572 i = au_new_group(arg); 573 if (i != AUGROUP_ERROR) 574 current_augroup = i; 575 } 576 else // ":aug": list the group names 577 { 578 msg_start(); 579 for (i = 0; i < augroups.ga_len; ++i) 580 { 581 if (AUGROUP_NAME(i) != NULL) 582 { 583 msg_puts((char *)AUGROUP_NAME(i)); 584 msg_puts(" "); 585 } 586 } 587 msg_clr_eos(); 588 msg_end(); 589 } 590 } 591 592 #if defined(EXITFREE) || defined(PROTO) 593 void 594 free_all_autocmds(void) 595 { 596 int i; 597 char_u *s; 598 599 for (current_augroup = -1; current_augroup < augroups.ga_len; 600 ++current_augroup) 601 do_autocmd((char_u *)"", TRUE); 602 603 for (i = 0; i < augroups.ga_len; ++i) 604 { 605 s = ((char_u **)(augroups.ga_data))[i]; 606 if (s != get_deleted_augroup()) 607 vim_free(s); 608 } 609 ga_clear(&augroups); 610 } 611 #endif 612 613 /* 614 * Return the event number for event name "start". 615 * Return NUM_EVENTS if the event name was not found. 616 * Return a pointer to the next event name in "end". 617 */ 618 static event_T 619 event_name2nr(char_u *start, char_u **end) 620 { 621 char_u *p; 622 int i; 623 int len; 624 625 // the event name ends with end of line, '|', a blank or a comma 626 for (p = start; *p && !VIM_ISWHITE(*p) && *p != ',' && *p != '|'; ++p) 627 ; 628 for (i = 0; event_names[i].name != NULL; ++i) 629 { 630 len = (int)STRLEN(event_names[i].name); 631 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) 632 break; 633 } 634 if (*p == ',') 635 ++p; 636 *end = p; 637 if (event_names[i].name == NULL) 638 return NUM_EVENTS; 639 return event_names[i].event; 640 } 641 642 /* 643 * Return the name for event "event". 644 */ 645 static char_u * 646 event_nr2name(event_T event) 647 { 648 int i; 649 650 for (i = 0; event_names[i].name != NULL; ++i) 651 if (event_names[i].event == event) 652 return (char_u *)event_names[i].name; 653 return (char_u *)"Unknown"; 654 } 655 656 /* 657 * Scan over the events. "*" stands for all events. 658 */ 659 static char_u * 660 find_end_event( 661 char_u *arg, 662 int have_group) // TRUE when group name was found 663 { 664 char_u *pat; 665 char_u *p; 666 667 if (*arg == '*') 668 { 669 if (arg[1] && !VIM_ISWHITE(arg[1])) 670 { 671 semsg(_("E215: Illegal character after *: %s"), arg); 672 return NULL; 673 } 674 pat = arg + 1; 675 } 676 else 677 { 678 for (pat = arg; *pat && *pat != '|' && !VIM_ISWHITE(*pat); pat = p) 679 { 680 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) 681 { 682 if (have_group) 683 semsg(_("E216: No such event: %s"), pat); 684 else 685 semsg(_("E216: No such group or event: %s"), pat); 686 return NULL; 687 } 688 } 689 } 690 return pat; 691 } 692 693 /* 694 * Return TRUE if "event" is included in 'eventignore'. 695 */ 696 static int 697 event_ignored(event_T event) 698 { 699 char_u *p = p_ei; 700 701 while (*p != NUL) 702 { 703 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) 704 return TRUE; 705 if (event_name2nr(p, &p) == event) 706 return TRUE; 707 } 708 709 return FALSE; 710 } 711 712 /* 713 * Return OK when the contents of p_ei is valid, FAIL otherwise. 714 */ 715 int 716 check_ei(void) 717 { 718 char_u *p = p_ei; 719 720 while (*p) 721 { 722 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) 723 { 724 p += 3; 725 if (*p == ',') 726 ++p; 727 } 728 else if (event_name2nr(p, &p) == NUM_EVENTS) 729 return FAIL; 730 } 731 732 return OK; 733 } 734 735 # if defined(FEAT_SYN_HL) || defined(PROTO) 736 737 /* 738 * Add "what" to 'eventignore' to skip loading syntax highlighting for every 739 * buffer loaded into the window. "what" must start with a comma. 740 * Returns the old value of 'eventignore' in allocated memory. 741 */ 742 char_u * 743 au_event_disable(char *what) 744 { 745 char_u *new_ei; 746 char_u *save_ei; 747 748 save_ei = vim_strsave(p_ei); 749 if (save_ei != NULL) 750 { 751 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); 752 if (new_ei != NULL) 753 { 754 if (*what == ',' && *p_ei == NUL) 755 STRCPY(new_ei, what + 1); 756 else 757 STRCAT(new_ei, what); 758 set_string_option_direct((char_u *)"ei", -1, new_ei, 759 OPT_FREE, SID_NONE); 760 vim_free(new_ei); 761 } 762 } 763 return save_ei; 764 } 765 766 void 767 au_event_restore(char_u *old_ei) 768 { 769 if (old_ei != NULL) 770 { 771 set_string_option_direct((char_u *)"ei", -1, old_ei, 772 OPT_FREE, SID_NONE); 773 vim_free(old_ei); 774 } 775 } 776 # endif /* FEAT_SYN_HL */ 777 778 /* 779 * do_autocmd() -- implements the :autocmd command. Can be used in the 780 * following ways: 781 * 782 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that 783 * will be automatically executed for <event> 784 * when editing a file matching <pat>, in 785 * the current group. 786 * :autocmd <event> <pat> Show the autocommands associated with 787 * <event> and <pat>. 788 * :autocmd <event> Show the autocommands associated with 789 * <event>. 790 * :autocmd Show all autocommands. 791 * :autocmd! <event> <pat> <cmd> Remove all autocommands associated with 792 * <event> and <pat>, and add the command 793 * <cmd>, for the current group. 794 * :autocmd! <event> <pat> Remove all autocommands associated with 795 * <event> and <pat> for the current group. 796 * :autocmd! <event> Remove all autocommands associated with 797 * <event> for the current group. 798 * :autocmd! Remove ALL autocommands for the current 799 * group. 800 * 801 * Multiple events and patterns may be given separated by commas. Here are 802 * some examples: 803 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic 804 * :autocmd bufleave * set tw=79 nosmartindent ic infercase 805 * 806 * :autocmd * *.c show all autocommands for *.c files. 807 * 808 * Mostly a {group} argument can optionally appear before <event>. 809 */ 810 void 811 do_autocmd(char_u *arg_in, int forceit) 812 { 813 char_u *arg = arg_in; 814 char_u *pat; 815 char_u *envpat = NULL; 816 char_u *cmd; 817 event_T event; 818 int need_free = FALSE; 819 int nested = FALSE; 820 int group; 821 822 if (*arg == '|') 823 { 824 arg = (char_u *)""; 825 group = AUGROUP_ALL; // no argument, use all groups 826 } 827 else 828 { 829 /* 830 * Check for a legal group name. If not, use AUGROUP_ALL. 831 */ 832 group = au_get_grouparg(&arg); 833 if (arg == NULL) // out of memory 834 return; 835 } 836 837 /* 838 * Scan over the events. 839 * If we find an illegal name, return here, don't do anything. 840 */ 841 pat = find_end_event(arg, group != AUGROUP_ALL); 842 if (pat == NULL) 843 return; 844 845 pat = skipwhite(pat); 846 if (*pat == '|') 847 { 848 pat = (char_u *)""; 849 cmd = (char_u *)""; 850 } 851 else 852 { 853 /* 854 * Scan over the pattern. Put a NUL at the end. 855 */ 856 cmd = pat; 857 while (*cmd && (!VIM_ISWHITE(*cmd) || cmd[-1] == '\\')) 858 cmd++; 859 if (*cmd) 860 *cmd++ = NUL; 861 862 // Expand environment variables in the pattern. Set 'shellslash', we 863 // want forward slashes here. 864 if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) 865 { 866 #ifdef BACKSLASH_IN_FILENAME 867 int p_ssl_save = p_ssl; 868 869 p_ssl = TRUE; 870 #endif 871 envpat = expand_env_save(pat); 872 #ifdef BACKSLASH_IN_FILENAME 873 p_ssl = p_ssl_save; 874 #endif 875 if (envpat != NULL) 876 pat = envpat; 877 } 878 879 /* 880 * Check for "nested" flag. 881 */ 882 cmd = skipwhite(cmd); 883 if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 884 && VIM_ISWHITE(cmd[6])) 885 { 886 nested = TRUE; 887 cmd = skipwhite(cmd + 6); 888 } 889 890 /* 891 * Find the start of the commands. 892 * Expand <sfile> in it. 893 */ 894 if (*cmd != NUL) 895 { 896 cmd = expand_sfile(cmd); 897 if (cmd == NULL) // some error 898 return; 899 need_free = TRUE; 900 } 901 } 902 903 /* 904 * Print header when showing autocommands. 905 */ 906 if (!forceit && *cmd == NUL) 907 // Highlight title 908 msg_puts_title(_("\n--- Autocommands ---")); 909 910 /* 911 * Loop over the events. 912 */ 913 last_event = (event_T)-1; // for listing the event name 914 last_group = AUGROUP_ERROR; // for listing the group name 915 if (*arg == '*' || *arg == NUL || *arg == '|') 916 { 917 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; 918 event = (event_T)((int)event + 1)) 919 if (do_autocmd_event(event, pat, 920 nested, cmd, forceit, group) == FAIL) 921 break; 922 } 923 else 924 { 925 while (*arg && *arg != '|' && !VIM_ISWHITE(*arg)) 926 if (do_autocmd_event(event_name2nr(arg, &arg), pat, 927 nested, cmd, forceit, group) == FAIL) 928 break; 929 } 930 931 if (need_free) 932 vim_free(cmd); 933 vim_free(envpat); 934 } 935 936 /* 937 * Find the group ID in a ":autocmd" or ":doautocmd" argument. 938 * The "argp" argument is advanced to the following argument. 939 * 940 * Returns the group ID, AUGROUP_ERROR for error (out of memory). 941 */ 942 static int 943 au_get_grouparg(char_u **argp) 944 { 945 char_u *group_name; 946 char_u *p; 947 char_u *arg = *argp; 948 int group = AUGROUP_ALL; 949 950 for (p = arg; *p && !VIM_ISWHITE(*p) && *p != '|'; ++p) 951 ; 952 if (p > arg) 953 { 954 group_name = vim_strnsave(arg, (int)(p - arg)); 955 if (group_name == NULL) // out of memory 956 return AUGROUP_ERROR; 957 group = au_find_group(group_name); 958 if (group == AUGROUP_ERROR) 959 group = AUGROUP_ALL; // no match, use all groups 960 else 961 *argp = skipwhite(p); // match, skip over group name 962 vim_free(group_name); 963 } 964 return group; 965 } 966 967 /* 968 * do_autocmd() for one event. 969 * If *pat == NUL do for all patterns. 970 * If *cmd == NUL show entries. 971 * If forceit == TRUE delete entries. 972 * If group is not AUGROUP_ALL, only use this group. 973 */ 974 static int 975 do_autocmd_event( 976 event_T event, 977 char_u *pat, 978 int nested, 979 char_u *cmd, 980 int forceit, 981 int group) 982 { 983 AutoPat *ap; 984 AutoPat **prev_ap; 985 AutoCmd *ac; 986 AutoCmd **prev_ac; 987 int brace_level; 988 char_u *endpat; 989 int findgroup; 990 int allgroups; 991 int patlen; 992 int is_buflocal; 993 int buflocal_nr; 994 char_u buflocal_pat[25]; /* for "<buffer=X>" */ 995 996 if (group == AUGROUP_ALL) 997 findgroup = current_augroup; 998 else 999 findgroup = group; 1000 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); 1001 1002 /* 1003 * Show or delete all patterns for an event. 1004 */ 1005 if (*pat == NUL) 1006 { 1007 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) 1008 { 1009 if (forceit) // delete the AutoPat, if it's in the current group 1010 { 1011 if (ap->group == findgroup) 1012 au_remove_pat(ap); 1013 } 1014 else if (group == AUGROUP_ALL || ap->group == group) 1015 show_autocmd(ap, event); 1016 } 1017 } 1018 1019 /* 1020 * Loop through all the specified patterns. 1021 */ 1022 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) 1023 { 1024 /* 1025 * Find end of the pattern. 1026 * Watch out for a comma in braces, like "*.\{obj,o\}". 1027 */ 1028 brace_level = 0; 1029 for (endpat = pat; *endpat && (*endpat != ',' || brace_level 1030 || (endpat > pat && endpat[-1] == '\\')); ++endpat) 1031 { 1032 if (*endpat == '{') 1033 brace_level++; 1034 else if (*endpat == '}') 1035 brace_level--; 1036 } 1037 if (pat == endpat) // ignore single comma 1038 continue; 1039 patlen = (int)(endpat - pat); 1040 1041 /* 1042 * detect special <buflocal[=X]> buffer-local patterns 1043 */ 1044 is_buflocal = FALSE; 1045 buflocal_nr = 0; 1046 1047 if (patlen >= 8 && STRNCMP(pat, "<buffer", 7) == 0 1048 && pat[patlen - 1] == '>') 1049 { 1050 // "<buffer...>": Error will be printed only for addition. 1051 // printing and removing will proceed silently. 1052 is_buflocal = TRUE; 1053 if (patlen == 8) 1054 // "<buffer>" 1055 buflocal_nr = curbuf->b_fnum; 1056 else if (patlen > 9 && pat[7] == '=') 1057 { 1058 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13) == 0) 1059 // "<buffer=abuf>" 1060 buflocal_nr = autocmd_bufnr; 1061 else if (skipdigits(pat + 8) == pat + patlen - 1) 1062 // "<buffer=123>" 1063 buflocal_nr = atoi((char *)pat + 8); 1064 } 1065 } 1066 1067 if (is_buflocal) 1068 { 1069 // normalize pat into standard "<buffer>#N" form 1070 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); 1071 pat = buflocal_pat; // can modify pat and patlen 1072 patlen = (int)STRLEN(buflocal_pat); // but not endpat 1073 } 1074 1075 /* 1076 * Find AutoPat entries with this pattern. When adding a command it 1077 * always goes at or after the last one, so start at the end. 1078 */ 1079 if (!forceit && *cmd != NUL && last_autopat[(int)event] != NULL) 1080 prev_ap = &last_autopat[(int)event]; 1081 else 1082 prev_ap = &first_autopat[(int)event]; 1083 while ((ap = *prev_ap) != NULL) 1084 { 1085 if (ap->pat != NULL) 1086 { 1087 /* Accept a pattern when: 1088 * - a group was specified and it's that group, or a group was 1089 * not specified and it's the current group, or a group was 1090 * not specified and we are listing 1091 * - the length of the pattern matches 1092 * - the pattern matches. 1093 * For <buffer[=X]>, this condition works because we normalize 1094 * all buffer-local patterns. 1095 */ 1096 if ((allgroups || ap->group == findgroup) 1097 && ap->patlen == patlen 1098 && STRNCMP(pat, ap->pat, patlen) == 0) 1099 { 1100 /* 1101 * Remove existing autocommands. 1102 * If adding any new autocmd's for this AutoPat, don't 1103 * delete the pattern from the autopat list, append to 1104 * this list. 1105 */ 1106 if (forceit) 1107 { 1108 if (*cmd != NUL && ap->next == NULL) 1109 { 1110 au_remove_cmds(ap); 1111 break; 1112 } 1113 au_remove_pat(ap); 1114 } 1115 1116 /* 1117 * Show autocmd's for this autopat, or buflocals <buffer=X> 1118 */ 1119 else if (*cmd == NUL) 1120 show_autocmd(ap, event); 1121 1122 /* 1123 * Add autocmd to this autopat, if it's the last one. 1124 */ 1125 else if (ap->next == NULL) 1126 break; 1127 } 1128 } 1129 prev_ap = &ap->next; 1130 } 1131 1132 /* 1133 * Add a new command. 1134 */ 1135 if (*cmd != NUL) 1136 { 1137 /* 1138 * If the pattern we want to add a command to does appear at the 1139 * end of the list (or not is not in the list at all), add the 1140 * pattern at the end of the list. 1141 */ 1142 if (ap == NULL) 1143 { 1144 /* refuse to add buffer-local ap if buffer number is invalid */ 1145 if (is_buflocal && (buflocal_nr == 0 1146 || buflist_findnr(buflocal_nr) == NULL)) 1147 { 1148 semsg(_("E680: <buffer=%d>: invalid buffer number "), 1149 buflocal_nr); 1150 return FAIL; 1151 } 1152 1153 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); 1154 if (ap == NULL) 1155 return FAIL; 1156 ap->pat = vim_strnsave(pat, patlen); 1157 ap->patlen = patlen; 1158 if (ap->pat == NULL) 1159 { 1160 vim_free(ap); 1161 return FAIL; 1162 } 1163 1164 if (is_buflocal) 1165 { 1166 ap->buflocal_nr = buflocal_nr; 1167 ap->reg_prog = NULL; 1168 } 1169 else 1170 { 1171 char_u *reg_pat; 1172 1173 ap->buflocal_nr = 0; 1174 reg_pat = file_pat_to_reg_pat(pat, endpat, 1175 &ap->allow_dirs, TRUE); 1176 if (reg_pat != NULL) 1177 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); 1178 vim_free(reg_pat); 1179 if (reg_pat == NULL || ap->reg_prog == NULL) 1180 { 1181 vim_free(ap->pat); 1182 vim_free(ap); 1183 return FAIL; 1184 } 1185 } 1186 ap->cmds = NULL; 1187 *prev_ap = ap; 1188 last_autopat[(int)event] = ap; 1189 ap->next = NULL; 1190 if (group == AUGROUP_ALL) 1191 ap->group = current_augroup; 1192 else 1193 ap->group = group; 1194 } 1195 1196 /* 1197 * Add the autocmd at the end of the AutoCmd list. 1198 */ 1199 prev_ac = &(ap->cmds); 1200 while ((ac = *prev_ac) != NULL) 1201 prev_ac = &ac->next; 1202 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); 1203 if (ac == NULL) 1204 return FAIL; 1205 ac->cmd = vim_strsave(cmd); 1206 #ifdef FEAT_EVAL 1207 ac->script_ctx = current_sctx; 1208 ac->script_ctx.sc_lnum += sourcing_lnum; 1209 #endif 1210 if (ac->cmd == NULL) 1211 { 1212 vim_free(ac); 1213 return FAIL; 1214 } 1215 ac->next = NULL; 1216 *prev_ac = ac; 1217 ac->nested = nested; 1218 } 1219 } 1220 1221 au_cleanup(); // may really delete removed patterns/commands now 1222 return OK; 1223 } 1224 1225 /* 1226 * Implementation of ":doautocmd [group] event [fname]". 1227 * Return OK for success, FAIL for failure; 1228 */ 1229 int 1230 do_doautocmd( 1231 char_u *arg, 1232 int do_msg, // give message for no matching autocmds? 1233 int *did_something) 1234 { 1235 char_u *fname; 1236 int nothing_done = TRUE; 1237 int group; 1238 1239 if (did_something != NULL) 1240 *did_something = FALSE; 1241 1242 /* 1243 * Check for a legal group name. If not, use AUGROUP_ALL. 1244 */ 1245 group = au_get_grouparg(&arg); 1246 if (arg == NULL) // out of memory 1247 return FAIL; 1248 1249 if (*arg == '*') 1250 { 1251 emsg(_("E217: Can't execute autocommands for ALL events")); 1252 return FAIL; 1253 } 1254 1255 /* 1256 * Scan over the events. 1257 * If we find an illegal name, return here, don't do anything. 1258 */ 1259 fname = find_end_event(arg, group != AUGROUP_ALL); 1260 if (fname == NULL) 1261 return FAIL; 1262 1263 fname = skipwhite(fname); 1264 1265 /* 1266 * Loop over the events. 1267 */ 1268 while (*arg && !ends_excmd(*arg) && !VIM_ISWHITE(*arg)) 1269 if (apply_autocmds_group(event_name2nr(arg, &arg), 1270 fname, NULL, TRUE, group, curbuf, NULL)) 1271 nothing_done = FALSE; 1272 1273 if (nothing_done && do_msg) 1274 msg(_("No matching autocommands")); 1275 if (did_something != NULL) 1276 *did_something = !nothing_done; 1277 1278 #ifdef FEAT_EVAL 1279 return aborting() ? FAIL : OK; 1280 #else 1281 return OK; 1282 #endif 1283 } 1284 1285 /* 1286 * ":doautoall": execute autocommands for each loaded buffer. 1287 */ 1288 void 1289 ex_doautoall(exarg_T *eap) 1290 { 1291 int retval; 1292 aco_save_T aco; 1293 buf_T *buf; 1294 bufref_T bufref; 1295 char_u *arg = eap->arg; 1296 int call_do_modelines = check_nomodeline(&arg); 1297 int did_aucmd; 1298 1299 /* 1300 * This is a bit tricky: For some commands curwin->w_buffer needs to be 1301 * equal to curbuf, but for some buffers there may not be a window. 1302 * So we change the buffer for the current window for a moment. This 1303 * gives problems when the autocommands make changes to the list of 1304 * buffers or windows... 1305 */ 1306 FOR_ALL_BUFFERS(buf) 1307 { 1308 if (buf->b_ml.ml_mfp != NULL) 1309 { 1310 // find a window for this buffer and save some values 1311 aucmd_prepbuf(&aco, buf); 1312 set_bufref(&bufref, buf); 1313 1314 // execute the autocommands for this buffer 1315 retval = do_doautocmd(arg, FALSE, &did_aucmd); 1316 1317 if (call_do_modelines && did_aucmd) 1318 { 1319 // Execute the modeline settings, but don't set window-local 1320 // options if we are using the current window for another 1321 // buffer. 1322 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); 1323 } 1324 1325 // restore the current window 1326 aucmd_restbuf(&aco); 1327 1328 // stop if there is some error or buffer was deleted 1329 if (retval == FAIL || !bufref_valid(&bufref)) 1330 break; 1331 } 1332 } 1333 1334 check_cursor(); // just in case lines got deleted 1335 } 1336 1337 /* 1338 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise 1339 * return TRUE and advance *argp to after it. 1340 * Thus return TRUE when do_modelines() should be called. 1341 */ 1342 int 1343 check_nomodeline(char_u **argp) 1344 { 1345 if (STRNCMP(*argp, "<nomodeline>", 12) == 0) 1346 { 1347 *argp = skipwhite(*argp + 12); 1348 return FALSE; 1349 } 1350 return TRUE; 1351 } 1352 1353 /* 1354 * Prepare for executing autocommands for (hidden) buffer "buf". 1355 * Search for a visible window containing the current buffer. If there isn't 1356 * one then use "aucmd_win". 1357 * Set "curbuf" and "curwin" to match "buf". 1358 */ 1359 void 1360 aucmd_prepbuf( 1361 aco_save_T *aco, // structure to save values in 1362 buf_T *buf) // new curbuf 1363 { 1364 win_T *win; 1365 int save_ea; 1366 #ifdef FEAT_AUTOCHDIR 1367 int save_acd; 1368 #endif 1369 1370 // Find a window that is for the new buffer 1371 if (buf == curbuf) // be quick when buf is curbuf 1372 win = curwin; 1373 else 1374 FOR_ALL_WINDOWS(win) 1375 if (win->w_buffer == buf) 1376 break; 1377 1378 // Allocate "aucmd_win" when needed. If this fails (out of memory) fall 1379 // back to using the current window. 1380 if (win == NULL && aucmd_win == NULL) 1381 { 1382 win_alloc_aucmd_win(); 1383 if (aucmd_win == NULL) 1384 win = curwin; 1385 } 1386 if (win == NULL && aucmd_win_used) 1387 // Strange recursive autocommand, fall back to using the current 1388 // window. Expect a few side effects... 1389 win = curwin; 1390 1391 aco->save_curwin = curwin; 1392 aco->save_curbuf = curbuf; 1393 aco->save_prevwin = prevwin; 1394 if (win != NULL) 1395 { 1396 // There is a window for "buf" in the current tab page, make it the 1397 // curwin. This is preferred, it has the least side effects (esp. if 1398 // "buf" is curbuf). 1399 aco->use_aucmd_win = FALSE; 1400 curwin = win; 1401 } 1402 else 1403 { 1404 // There is no window for "buf", use "aucmd_win". To minimize the side 1405 // effects, insert it in the current tab page. 1406 // Anything related to a window (e.g., setting folds) may have 1407 // unexpected results. 1408 aco->use_aucmd_win = TRUE; 1409 aucmd_win_used = TRUE; 1410 aucmd_win->w_buffer = buf; 1411 #if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) 1412 aucmd_win->w_s = &buf->b_s; 1413 #endif 1414 ++buf->b_nwindows; 1415 win_init_empty(aucmd_win); // set cursor and topline to safe values 1416 1417 // Make sure w_localdir and globaldir are NULL to avoid a chdir() in 1418 // win_enter_ext(). 1419 VIM_CLEAR(aucmd_win->w_localdir); 1420 aco->globaldir = globaldir; 1421 globaldir = NULL; 1422 1423 1424 // Split the current window, put the aucmd_win in the upper half. 1425 // We don't want the BufEnter or WinEnter autocommands. 1426 block_autocmds(); 1427 make_snapshot(SNAP_AUCMD_IDX); 1428 save_ea = p_ea; 1429 p_ea = FALSE; 1430 1431 #ifdef FEAT_AUTOCHDIR 1432 // Prevent chdir() call in win_enter_ext(), through do_autochdir(). 1433 save_acd = p_acd; 1434 p_acd = FALSE; 1435 #endif 1436 1437 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); 1438 (void)win_comp_pos(); // recompute window positions 1439 p_ea = save_ea; 1440 #ifdef FEAT_AUTOCHDIR 1441 p_acd = save_acd; 1442 #endif 1443 unblock_autocmds(); 1444 curwin = aucmd_win; 1445 } 1446 curbuf = buf; 1447 aco->new_curwin = curwin; 1448 set_bufref(&aco->new_curbuf, curbuf); 1449 } 1450 1451 /* 1452 * Cleanup after executing autocommands for a (hidden) buffer. 1453 * Restore the window as it was (if possible). 1454 */ 1455 void 1456 aucmd_restbuf( 1457 aco_save_T *aco) // structure holding saved values 1458 { 1459 int dummy; 1460 1461 if (aco->use_aucmd_win) 1462 { 1463 --curbuf->b_nwindows; 1464 // Find "aucmd_win", it can't be closed, but it may be in another tab 1465 // page. Do not trigger autocommands here. 1466 block_autocmds(); 1467 if (curwin != aucmd_win) 1468 { 1469 tabpage_T *tp; 1470 win_T *wp; 1471 1472 FOR_ALL_TAB_WINDOWS(tp, wp) 1473 { 1474 if (wp == aucmd_win) 1475 { 1476 if (tp != curtab) 1477 goto_tabpage_tp(tp, TRUE, TRUE); 1478 win_goto(aucmd_win); 1479 goto win_found; 1480 } 1481 } 1482 } 1483 win_found: 1484 1485 // Remove the window and frame from the tree of frames. 1486 (void)winframe_remove(curwin, &dummy, NULL); 1487 win_remove(curwin, NULL); 1488 aucmd_win_used = FALSE; 1489 last_status(FALSE); // may need to remove last status line 1490 1491 if (!valid_tabpage_win(curtab)) 1492 // no valid window in current tabpage 1493 close_tabpage(curtab); 1494 1495 restore_snapshot(SNAP_AUCMD_IDX, FALSE); 1496 (void)win_comp_pos(); // recompute window positions 1497 unblock_autocmds(); 1498 1499 if (win_valid(aco->save_curwin)) 1500 curwin = aco->save_curwin; 1501 else 1502 // Hmm, original window disappeared. Just use the first one. 1503 curwin = firstwin; 1504 if (win_valid(aco->save_prevwin)) 1505 prevwin = aco->save_prevwin; 1506 #ifdef FEAT_EVAL 1507 vars_clear(&aucmd_win->w_vars->dv_hashtab); // free all w: variables 1508 hash_init(&aucmd_win->w_vars->dv_hashtab); // re-use the hashtab 1509 #endif 1510 curbuf = curwin->w_buffer; 1511 1512 vim_free(globaldir); 1513 globaldir = aco->globaldir; 1514 1515 // the buffer contents may have changed 1516 check_cursor(); 1517 if (curwin->w_topline > curbuf->b_ml.ml_line_count) 1518 { 1519 curwin->w_topline = curbuf->b_ml.ml_line_count; 1520 #ifdef FEAT_DIFF 1521 curwin->w_topfill = 0; 1522 #endif 1523 } 1524 #if defined(FEAT_GUI) 1525 // Hide the scrollbars from the aucmd_win and update. 1526 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); 1527 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); 1528 gui_may_update_scrollbars(); 1529 #endif 1530 } 1531 else 1532 { 1533 // restore curwin 1534 if (win_valid(aco->save_curwin)) 1535 { 1536 // Restore the buffer which was previously edited by curwin, if 1537 // it was changed, we are still the same window and the buffer is 1538 // valid. 1539 if (curwin == aco->new_curwin 1540 && curbuf != aco->new_curbuf.br_buf 1541 && bufref_valid(&aco->new_curbuf) 1542 && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL) 1543 { 1544 # if defined(FEAT_SYN_HL) || defined(FEAT_SPELL) 1545 if (curwin->w_s == &curbuf->b_s) 1546 curwin->w_s = &aco->new_curbuf.br_buf->b_s; 1547 # endif 1548 --curbuf->b_nwindows; 1549 curbuf = aco->new_curbuf.br_buf; 1550 curwin->w_buffer = curbuf; 1551 ++curbuf->b_nwindows; 1552 } 1553 1554 curwin = aco->save_curwin; 1555 curbuf = curwin->w_buffer; 1556 if (win_valid(aco->save_prevwin)) 1557 prevwin = aco->save_prevwin; 1558 // In case the autocommand move the cursor to a position that that 1559 // not exist in curbuf. 1560 check_cursor(); 1561 } 1562 } 1563 } 1564 1565 static int autocmd_nested = FALSE; 1566 1567 /* 1568 * Execute autocommands for "event" and file name "fname". 1569 * Return TRUE if some commands were executed. 1570 */ 1571 int 1572 apply_autocmds( 1573 event_T event, 1574 char_u *fname, // NULL or empty means use actual file name 1575 char_u *fname_io, // fname to use for <afile> on cmdline 1576 int force, // when TRUE, ignore autocmd_busy 1577 buf_T *buf) // buffer for <abuf> 1578 { 1579 return apply_autocmds_group(event, fname, fname_io, force, 1580 AUGROUP_ALL, buf, NULL); 1581 } 1582 1583 /* 1584 * Like apply_autocmds(), but with extra "eap" argument. This takes care of 1585 * setting v:filearg. 1586 */ 1587 int 1588 apply_autocmds_exarg( 1589 event_T event, 1590 char_u *fname, 1591 char_u *fname_io, 1592 int force, 1593 buf_T *buf, 1594 exarg_T *eap) 1595 { 1596 return apply_autocmds_group(event, fname, fname_io, force, 1597 AUGROUP_ALL, buf, eap); 1598 } 1599 1600 /* 1601 * Like apply_autocmds(), but handles the caller's retval. If the script 1602 * processing is being aborted or if retval is FAIL when inside a try 1603 * conditional, no autocommands are executed. If otherwise the autocommands 1604 * cause the script to be aborted, retval is set to FAIL. 1605 */ 1606 int 1607 apply_autocmds_retval( 1608 event_T event, 1609 char_u *fname, // NULL or empty means use actual file name 1610 char_u *fname_io, // fname to use for <afile> on cmdline 1611 int force, // when TRUE, ignore autocmd_busy 1612 buf_T *buf, // buffer for <abuf> 1613 int *retval) // pointer to caller's retval 1614 { 1615 int did_cmd; 1616 1617 #ifdef FEAT_EVAL 1618 if (should_abort(*retval)) 1619 return FALSE; 1620 #endif 1621 1622 did_cmd = apply_autocmds_group(event, fname, fname_io, force, 1623 AUGROUP_ALL, buf, NULL); 1624 if (did_cmd 1625 #ifdef FEAT_EVAL 1626 && aborting() 1627 #endif 1628 ) 1629 *retval = FAIL; 1630 return did_cmd; 1631 } 1632 1633 /* 1634 * Return TRUE when there is a CursorHold autocommand defined. 1635 */ 1636 int 1637 has_cursorhold(void) 1638 { 1639 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY 1640 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); 1641 } 1642 1643 /* 1644 * Return TRUE if the CursorHold event can be triggered. 1645 */ 1646 int 1647 trigger_cursorhold(void) 1648 { 1649 int state; 1650 1651 if (!did_cursorhold 1652 && has_cursorhold() 1653 && reg_recording == 0 1654 && typebuf.tb_len == 0 1655 #ifdef FEAT_INS_EXPAND 1656 && !ins_compl_active() 1657 #endif 1658 ) 1659 { 1660 state = get_real_state(); 1661 if (state == NORMAL_BUSY || (state & INSERT) != 0) 1662 return TRUE; 1663 } 1664 return FALSE; 1665 } 1666 1667 /* 1668 * Return TRUE when there is a CursorMoved autocommand defined. 1669 */ 1670 int 1671 has_cursormoved(void) 1672 { 1673 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); 1674 } 1675 1676 #if defined(FEAT_CONCEAL) || defined(PROTO) 1677 /* 1678 * Return TRUE when there is a CursorMovedI autocommand defined. 1679 */ 1680 int 1681 has_cursormovedI(void) 1682 { 1683 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); 1684 } 1685 #endif 1686 1687 /* 1688 * Return TRUE when there is a TextChanged autocommand defined. 1689 */ 1690 int 1691 has_textchanged(void) 1692 { 1693 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL); 1694 } 1695 1696 /* 1697 * Return TRUE when there is a TextChangedI autocommand defined. 1698 */ 1699 int 1700 has_textchangedI(void) 1701 { 1702 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL); 1703 } 1704 1705 #if defined(FEAT_INS_EXPAND) || defined(PROTO) 1706 /* 1707 * Return TRUE when there is a TextChangedP autocommand defined. 1708 */ 1709 int 1710 has_textchangedP(void) 1711 { 1712 return (first_autopat[(int)EVENT_TEXTCHANGEDP] != NULL); 1713 } 1714 #endif 1715 1716 /* 1717 * Return TRUE when there is an InsertCharPre autocommand defined. 1718 */ 1719 int 1720 has_insertcharpre(void) 1721 { 1722 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL); 1723 } 1724 1725 /* 1726 * Return TRUE when there is an CmdUndefined autocommand defined. 1727 */ 1728 int 1729 has_cmdundefined(void) 1730 { 1731 return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL); 1732 } 1733 1734 /* 1735 * Return TRUE when there is an FuncUndefined autocommand defined. 1736 */ 1737 int 1738 has_funcundefined(void) 1739 { 1740 return (first_autopat[(int)EVENT_FUNCUNDEFINED] != NULL); 1741 } 1742 1743 #if defined(FEAT_EVAL) || defined(PROTO) 1744 /* 1745 * Return TRUE when there is a TextYankPost autocommand defined. 1746 */ 1747 int 1748 has_textyankpost(void) 1749 { 1750 return (first_autopat[(int)EVENT_TEXTYANKPOST] != NULL); 1751 } 1752 #endif 1753 1754 /* 1755 * Execute autocommands for "event" and file name "fname". 1756 * Return TRUE if some commands were executed. 1757 */ 1758 static int 1759 apply_autocmds_group( 1760 event_T event, 1761 char_u *fname, // NULL or empty means use actual file name 1762 char_u *fname_io, // fname to use for <afile> on cmdline, NULL means 1763 // use fname 1764 int force, // when TRUE, ignore autocmd_busy 1765 int group, // group ID, or AUGROUP_ALL 1766 buf_T *buf, // buffer for <abuf> 1767 exarg_T *eap UNUSED) // command arguments 1768 { 1769 char_u *sfname = NULL; // short file name 1770 char_u *tail; 1771 int save_changed; 1772 buf_T *old_curbuf; 1773 int retval = FALSE; 1774 char_u *save_sourcing_name; 1775 linenr_T save_sourcing_lnum; 1776 char_u *save_autocmd_fname; 1777 int save_autocmd_fname_full; 1778 int save_autocmd_bufnr; 1779 char_u *save_autocmd_match; 1780 int save_autocmd_busy; 1781 int save_autocmd_nested; 1782 static int nesting = 0; 1783 AutoPatCmd patcmd; 1784 AutoPat *ap; 1785 #ifdef FEAT_EVAL 1786 sctx_T save_current_sctx; 1787 funccal_entry_T funccal_entry; 1788 char_u *save_cmdarg; 1789 long save_cmdbang; 1790 #endif 1791 static int filechangeshell_busy = FALSE; 1792 #ifdef FEAT_PROFILE 1793 proftime_T wait_time; 1794 #endif 1795 int did_save_redobuff = FALSE; 1796 save_redo_T save_redo; 1797 int save_KeyTyped = KeyTyped; 1798 1799 /* 1800 * Quickly return if there are no autocommands for this event or 1801 * autocommands are blocked. 1802 */ 1803 if (event == NUM_EVENTS || first_autopat[(int)event] == NULL 1804 || autocmd_blocked > 0) 1805 goto BYPASS_AU; 1806 1807 /* 1808 * When autocommands are busy, new autocommands are only executed when 1809 * explicitly enabled with the "nested" flag. 1810 */ 1811 if (autocmd_busy && !(force || autocmd_nested)) 1812 goto BYPASS_AU; 1813 1814 #ifdef FEAT_EVAL 1815 /* 1816 * Quickly return when immediately aborting on error, or when an interrupt 1817 * occurred or an exception was thrown but not caught. 1818 */ 1819 if (aborting()) 1820 goto BYPASS_AU; 1821 #endif 1822 1823 /* 1824 * FileChangedShell never nests, because it can create an endless loop. 1825 */ 1826 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL 1827 || event == EVENT_FILECHANGEDSHELLPOST)) 1828 goto BYPASS_AU; 1829 1830 /* 1831 * Ignore events in 'eventignore'. 1832 */ 1833 if (event_ignored(event)) 1834 goto BYPASS_AU; 1835 1836 /* 1837 * Allow nesting of autocommands, but restrict the depth, because it's 1838 * possible to create an endless loop. 1839 */ 1840 if (nesting == 10) 1841 { 1842 emsg(_("E218: autocommand nesting too deep")); 1843 goto BYPASS_AU; 1844 } 1845 1846 /* 1847 * Check if these autocommands are disabled. Used when doing ":all" or 1848 * ":ball". 1849 */ 1850 if ( (autocmd_no_enter 1851 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) 1852 || (autocmd_no_leave 1853 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) 1854 goto BYPASS_AU; 1855 1856 /* 1857 * Save the autocmd_* variables and info about the current buffer. 1858 */ 1859 save_autocmd_fname = autocmd_fname; 1860 save_autocmd_fname_full = autocmd_fname_full; 1861 save_autocmd_bufnr = autocmd_bufnr; 1862 save_autocmd_match = autocmd_match; 1863 save_autocmd_busy = autocmd_busy; 1864 save_autocmd_nested = autocmd_nested; 1865 save_changed = curbuf->b_changed; 1866 old_curbuf = curbuf; 1867 1868 /* 1869 * Set the file name to be used for <afile>. 1870 * Make a copy to avoid that changing a buffer name or directory makes it 1871 * invalid. 1872 */ 1873 if (fname_io == NULL) 1874 { 1875 if (event == EVENT_COLORSCHEME || event == EVENT_COLORSCHEMEPRE 1876 || event == EVENT_OPTIONSET) 1877 autocmd_fname = NULL; 1878 else if (fname != NULL && !ends_excmd(*fname)) 1879 autocmd_fname = fname; 1880 else if (buf != NULL) 1881 autocmd_fname = buf->b_ffname; 1882 else 1883 autocmd_fname = NULL; 1884 } 1885 else 1886 autocmd_fname = fname_io; 1887 if (autocmd_fname != NULL) 1888 autocmd_fname = vim_strsave(autocmd_fname); 1889 autocmd_fname_full = FALSE; // call FullName_save() later 1890 1891 /* 1892 * Set the buffer number to be used for <abuf>. 1893 */ 1894 if (buf == NULL) 1895 autocmd_bufnr = 0; 1896 else 1897 autocmd_bufnr = buf->b_fnum; 1898 1899 /* 1900 * When the file name is NULL or empty, use the file name of buffer "buf". 1901 * Always use the full path of the file name to match with, in case 1902 * "allow_dirs" is set. 1903 */ 1904 if (fname == NULL || *fname == NUL) 1905 { 1906 if (buf == NULL) 1907 fname = NULL; 1908 else 1909 { 1910 #ifdef FEAT_SYN_HL 1911 if (event == EVENT_SYNTAX) 1912 fname = buf->b_p_syn; 1913 else 1914 #endif 1915 if (event == EVENT_FILETYPE) 1916 fname = buf->b_p_ft; 1917 else 1918 { 1919 if (buf->b_sfname != NULL) 1920 sfname = vim_strsave(buf->b_sfname); 1921 fname = buf->b_ffname; 1922 } 1923 } 1924 if (fname == NULL) 1925 fname = (char_u *)""; 1926 fname = vim_strsave(fname); // make a copy, so we can change it 1927 } 1928 else 1929 { 1930 sfname = vim_strsave(fname); 1931 // Don't try expanding FileType, Syntax, FuncUndefined, WindowID, 1932 // ColorScheme, QuickFixCmd* or DirChanged 1933 if (event == EVENT_FILETYPE 1934 || event == EVENT_SYNTAX 1935 || event == EVENT_CMDLINECHANGED 1936 || event == EVENT_CMDLINEENTER 1937 || event == EVENT_CMDLINELEAVE 1938 || event == EVENT_CMDWINENTER 1939 || event == EVENT_CMDWINLEAVE 1940 || event == EVENT_CMDUNDEFINED 1941 || event == EVENT_FUNCUNDEFINED 1942 || event == EVENT_REMOTEREPLY 1943 || event == EVENT_SPELLFILEMISSING 1944 || event == EVENT_QUICKFIXCMDPRE 1945 || event == EVENT_COLORSCHEME 1946 || event == EVENT_COLORSCHEMEPRE 1947 || event == EVENT_OPTIONSET 1948 || event == EVENT_QUICKFIXCMDPOST 1949 || event == EVENT_DIRCHANGED) 1950 { 1951 fname = vim_strsave(fname); 1952 autocmd_fname_full = TRUE; // don't expand it later 1953 } 1954 else 1955 fname = FullName_save(fname, FALSE); 1956 } 1957 if (fname == NULL) // out of memory 1958 { 1959 vim_free(sfname); 1960 retval = FALSE; 1961 goto BYPASS_AU; 1962 } 1963 1964 #ifdef BACKSLASH_IN_FILENAME 1965 /* 1966 * Replace all backslashes with forward slashes. This makes the 1967 * autocommand patterns portable between Unix and MS-DOS. 1968 */ 1969 if (sfname != NULL) 1970 forward_slash(sfname); 1971 forward_slash(fname); 1972 #endif 1973 1974 #ifdef VMS 1975 // remove version for correct match 1976 if (sfname != NULL) 1977 vms_remove_version(sfname); 1978 vms_remove_version(fname); 1979 #endif 1980 1981 /* 1982 * Set the name to be used for <amatch>. 1983 */ 1984 autocmd_match = fname; 1985 1986 1987 // Don't redraw while doing autocommands. 1988 ++RedrawingDisabled; 1989 save_sourcing_name = sourcing_name; 1990 sourcing_name = NULL; // don't free this one 1991 save_sourcing_lnum = sourcing_lnum; 1992 sourcing_lnum = 0; // no line number here 1993 1994 #ifdef FEAT_EVAL 1995 save_current_sctx = current_sctx; 1996 1997 # ifdef FEAT_PROFILE 1998 if (do_profiling == PROF_YES) 1999 prof_child_enter(&wait_time); // doesn't count for the caller itself 2000 # endif 2001 2002 // Don't use local function variables, if called from a function. 2003 save_funccal(&funccal_entry); 2004 #endif 2005 2006 /* 2007 * When starting to execute autocommands, save the search patterns. 2008 */ 2009 if (!autocmd_busy) 2010 { 2011 save_search_patterns(); 2012 #ifdef FEAT_INS_EXPAND 2013 if (!ins_compl_active()) 2014 #endif 2015 { 2016 saveRedobuff(&save_redo); 2017 did_save_redobuff = TRUE; 2018 } 2019 did_filetype = keep_filetype; 2020 } 2021 2022 /* 2023 * Note that we are applying autocmds. Some commands need to know. 2024 */ 2025 autocmd_busy = TRUE; 2026 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); 2027 ++nesting; // see matching decrement below 2028 2029 // Remember that FileType was triggered. Used for did_filetype(). 2030 if (event == EVENT_FILETYPE) 2031 did_filetype = TRUE; 2032 2033 tail = gettail(fname); 2034 2035 // Find first autocommand that matches 2036 patcmd.curpat = first_autopat[(int)event]; 2037 patcmd.nextcmd = NULL; 2038 patcmd.group = group; 2039 patcmd.fname = fname; 2040 patcmd.sfname = sfname; 2041 patcmd.tail = tail; 2042 patcmd.event = event; 2043 patcmd.arg_bufnr = autocmd_bufnr; 2044 patcmd.next = NULL; 2045 auto_next_pat(&patcmd, FALSE); 2046 2047 // found one, start executing the autocommands 2048 if (patcmd.curpat != NULL) 2049 { 2050 // add to active_apc_list 2051 patcmd.next = active_apc_list; 2052 active_apc_list = &patcmd; 2053 2054 #ifdef FEAT_EVAL 2055 // set v:cmdarg (only when there is a matching pattern) 2056 save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG); 2057 if (eap != NULL) 2058 { 2059 save_cmdarg = set_cmdarg(eap, NULL); 2060 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); 2061 } 2062 else 2063 save_cmdarg = NULL; // avoid gcc warning 2064 #endif 2065 retval = TRUE; 2066 // mark the last pattern, to avoid an endless loop when more patterns 2067 // are added when executing autocommands 2068 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) 2069 ap->last = FALSE; 2070 ap->last = TRUE; 2071 check_lnums(TRUE); // make sure cursor and topline are valid 2072 do_cmdline(NULL, getnextac, (void *)&patcmd, 2073 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); 2074 #ifdef FEAT_EVAL 2075 if (eap != NULL) 2076 { 2077 (void)set_cmdarg(NULL, save_cmdarg); 2078 set_vim_var_nr(VV_CMDBANG, save_cmdbang); 2079 } 2080 #endif 2081 // delete from active_apc_list 2082 if (active_apc_list == &patcmd) // just in case 2083 active_apc_list = patcmd.next; 2084 } 2085 2086 --RedrawingDisabled; 2087 autocmd_busy = save_autocmd_busy; 2088 filechangeshell_busy = FALSE; 2089 autocmd_nested = save_autocmd_nested; 2090 vim_free(sourcing_name); 2091 sourcing_name = save_sourcing_name; 2092 sourcing_lnum = save_sourcing_lnum; 2093 vim_free(autocmd_fname); 2094 autocmd_fname = save_autocmd_fname; 2095 autocmd_fname_full = save_autocmd_fname_full; 2096 autocmd_bufnr = save_autocmd_bufnr; 2097 autocmd_match = save_autocmd_match; 2098 #ifdef FEAT_EVAL 2099 current_sctx = save_current_sctx; 2100 restore_funccal(); 2101 # ifdef FEAT_PROFILE 2102 if (do_profiling == PROF_YES) 2103 prof_child_exit(&wait_time); 2104 # endif 2105 #endif 2106 KeyTyped = save_KeyTyped; 2107 vim_free(fname); 2108 vim_free(sfname); 2109 --nesting; // see matching increment above 2110 2111 /* 2112 * When stopping to execute autocommands, restore the search patterns and 2113 * the redo buffer. Free any buffers in the au_pending_free_buf list and 2114 * free any windows in the au_pending_free_win list. 2115 */ 2116 if (!autocmd_busy) 2117 { 2118 restore_search_patterns(); 2119 if (did_save_redobuff) 2120 restoreRedobuff(&save_redo); 2121 did_filetype = FALSE; 2122 while (au_pending_free_buf != NULL) 2123 { 2124 buf_T *b = au_pending_free_buf->b_next; 2125 vim_free(au_pending_free_buf); 2126 au_pending_free_buf = b; 2127 } 2128 while (au_pending_free_win != NULL) 2129 { 2130 win_T *w = au_pending_free_win->w_next; 2131 vim_free(au_pending_free_win); 2132 au_pending_free_win = w; 2133 } 2134 } 2135 2136 /* 2137 * Some events don't set or reset the Changed flag. 2138 * Check if still in the same buffer! 2139 */ 2140 if (curbuf == old_curbuf 2141 && (event == EVENT_BUFREADPOST 2142 || event == EVENT_BUFWRITEPOST 2143 || event == EVENT_FILEAPPENDPOST 2144 || event == EVENT_VIMLEAVE 2145 || event == EVENT_VIMLEAVEPRE)) 2146 { 2147 #ifdef FEAT_TITLE 2148 if (curbuf->b_changed != save_changed) 2149 need_maketitle = TRUE; 2150 #endif 2151 curbuf->b_changed = save_changed; 2152 } 2153 2154 au_cleanup(); // may really delete removed patterns/commands now 2155 2156 BYPASS_AU: 2157 // When wiping out a buffer make sure all its buffer-local autocommands 2158 // are deleted. 2159 if (event == EVENT_BUFWIPEOUT && buf != NULL) 2160 aubuflocal_remove(buf); 2161 2162 if (retval == OK && event == EVENT_FILETYPE) 2163 au_did_filetype = TRUE; 2164 2165 return retval; 2166 } 2167 2168 # ifdef FEAT_EVAL 2169 static char_u *old_termresponse = NULL; 2170 # endif 2171 2172 /* 2173 * Block triggering autocommands until unblock_autocmd() is called. 2174 * Can be used recursively, so long as it's symmetric. 2175 */ 2176 void 2177 block_autocmds(void) 2178 { 2179 # ifdef FEAT_EVAL 2180 // Remember the value of v:termresponse. 2181 if (autocmd_blocked == 0) 2182 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); 2183 # endif 2184 ++autocmd_blocked; 2185 } 2186 2187 void 2188 unblock_autocmds(void) 2189 { 2190 --autocmd_blocked; 2191 2192 # ifdef FEAT_EVAL 2193 // When v:termresponse was set while autocommands were blocked, trigger 2194 // the autocommands now. Esp. useful when executing a shell command 2195 // during startup (vimdiff). 2196 if (autocmd_blocked == 0 2197 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) 2198 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); 2199 # endif 2200 } 2201 2202 #if defined(FEAT_EVAL) && (defined(FEAT_XIM) || defined(IME_WITHOUT_XIM)) \ 2203 || defined(PROTO) 2204 int 2205 is_autocmd_blocked(void) 2206 { 2207 return autocmd_blocked != 0; 2208 } 2209 #endif 2210 2211 /* 2212 * Find next autocommand pattern that matches. 2213 */ 2214 static void 2215 auto_next_pat( 2216 AutoPatCmd *apc, 2217 int stop_at_last) // stop when 'last' flag is set 2218 { 2219 AutoPat *ap; 2220 AutoCmd *cp; 2221 char_u *name; 2222 char *s; 2223 2224 VIM_CLEAR(sourcing_name); 2225 2226 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) 2227 { 2228 apc->curpat = NULL; 2229 2230 // Only use a pattern when it has not been removed, has commands and 2231 // the group matches. For buffer-local autocommands only check the 2232 // buffer number. 2233 if (ap->pat != NULL && ap->cmds != NULL 2234 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) 2235 { 2236 // execution-condition 2237 if (ap->buflocal_nr == 0 2238 ? (match_file_pat(NULL, &ap->reg_prog, apc->fname, 2239 apc->sfname, apc->tail, ap->allow_dirs)) 2240 : ap->buflocal_nr == apc->arg_bufnr) 2241 { 2242 name = event_nr2name(apc->event); 2243 s = _("%s Autocommands for \"%s\""); 2244 sourcing_name = alloc((unsigned)(STRLEN(s) 2245 + STRLEN(name) + ap->patlen + 1)); 2246 if (sourcing_name != NULL) 2247 { 2248 sprintf((char *)sourcing_name, s, 2249 (char *)name, (char *)ap->pat); 2250 if (p_verbose >= 8) 2251 { 2252 verbose_enter(); 2253 smsg(_("Executing %s"), sourcing_name); 2254 verbose_leave(); 2255 } 2256 } 2257 2258 apc->curpat = ap; 2259 apc->nextcmd = ap->cmds; 2260 // mark last command 2261 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) 2262 cp->last = FALSE; 2263 cp->last = TRUE; 2264 } 2265 line_breakcheck(); 2266 if (apc->curpat != NULL) // found a match 2267 break; 2268 } 2269 if (stop_at_last && ap->last) 2270 break; 2271 } 2272 } 2273 2274 /* 2275 * Get next autocommand command. 2276 * Called by do_cmdline() to get the next line for ":if". 2277 * Returns allocated string, or NULL for end of autocommands. 2278 */ 2279 char_u * 2280 getnextac(int c UNUSED, void *cookie, int indent UNUSED) 2281 { 2282 AutoPatCmd *acp = (AutoPatCmd *)cookie; 2283 char_u *retval; 2284 AutoCmd *ac; 2285 2286 // Can be called again after returning the last line. 2287 if (acp->curpat == NULL) 2288 return NULL; 2289 2290 // repeat until we find an autocommand to execute 2291 for (;;) 2292 { 2293 // skip removed commands 2294 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) 2295 if (acp->nextcmd->last) 2296 acp->nextcmd = NULL; 2297 else 2298 acp->nextcmd = acp->nextcmd->next; 2299 2300 if (acp->nextcmd != NULL) 2301 break; 2302 2303 // at end of commands, find next pattern that matches 2304 if (acp->curpat->last) 2305 acp->curpat = NULL; 2306 else 2307 acp->curpat = acp->curpat->next; 2308 if (acp->curpat != NULL) 2309 auto_next_pat(acp, TRUE); 2310 if (acp->curpat == NULL) 2311 return NULL; 2312 } 2313 2314 ac = acp->nextcmd; 2315 2316 if (p_verbose >= 9) 2317 { 2318 verbose_enter_scroll(); 2319 smsg(_("autocommand %s"), ac->cmd); 2320 msg_puts("\n"); // don't overwrite this either 2321 verbose_leave_scroll(); 2322 } 2323 retval = vim_strsave(ac->cmd); 2324 autocmd_nested = ac->nested; 2325 #ifdef FEAT_EVAL 2326 current_sctx = ac->script_ctx; 2327 #endif 2328 if (ac->last) 2329 acp->nextcmd = NULL; 2330 else 2331 acp->nextcmd = ac->next; 2332 return retval; 2333 } 2334 2335 /* 2336 * Return TRUE if there is a matching autocommand for "fname". 2337 * To account for buffer-local autocommands, function needs to know 2338 * in which buffer the file will be opened. 2339 */ 2340 int 2341 has_autocmd(event_T event, char_u *sfname, buf_T *buf) 2342 { 2343 AutoPat *ap; 2344 char_u *fname; 2345 char_u *tail = gettail(sfname); 2346 int retval = FALSE; 2347 2348 fname = FullName_save(sfname, FALSE); 2349 if (fname == NULL) 2350 return FALSE; 2351 2352 #ifdef BACKSLASH_IN_FILENAME 2353 /* 2354 * Replace all backslashes with forward slashes. This makes the 2355 * autocommand patterns portable between Unix and MS-DOS. 2356 */ 2357 sfname = vim_strsave(sfname); 2358 if (sfname != NULL) 2359 forward_slash(sfname); 2360 forward_slash(fname); 2361 #endif 2362 2363 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) 2364 if (ap->pat != NULL && ap->cmds != NULL 2365 && (ap->buflocal_nr == 0 2366 ? match_file_pat(NULL, &ap->reg_prog, 2367 fname, sfname, tail, ap->allow_dirs) 2368 : buf != NULL && ap->buflocal_nr == buf->b_fnum 2369 )) 2370 { 2371 retval = TRUE; 2372 break; 2373 } 2374 2375 vim_free(fname); 2376 #ifdef BACKSLASH_IN_FILENAME 2377 vim_free(sfname); 2378 #endif 2379 2380 return retval; 2381 } 2382 2383 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) 2384 /* 2385 * Function given to ExpandGeneric() to obtain the list of autocommand group 2386 * names. 2387 */ 2388 char_u * 2389 get_augroup_name(expand_T *xp UNUSED, int idx) 2390 { 2391 if (idx == augroups.ga_len) // add "END" add the end 2392 return (char_u *)"END"; 2393 if (idx >= augroups.ga_len) // end of list 2394 return NULL; 2395 if (AUGROUP_NAME(idx) == NULL || AUGROUP_NAME(idx) == get_deleted_augroup()) 2396 // skip deleted entries 2397 return (char_u *)""; 2398 return AUGROUP_NAME(idx); // return a name 2399 } 2400 2401 static int include_groups = FALSE; 2402 2403 char_u * 2404 set_context_in_autocmd( 2405 expand_T *xp, 2406 char_u *arg, 2407 int doautocmd) // TRUE for :doauto*, FALSE for :autocmd 2408 { 2409 char_u *p; 2410 int group; 2411 2412 // check for a group name, skip it if present 2413 include_groups = FALSE; 2414 p = arg; 2415 group = au_get_grouparg(&arg); 2416 if (group == AUGROUP_ERROR) 2417 return NULL; 2418 // If there only is a group name that's what we expand. 2419 if (*arg == NUL && group != AUGROUP_ALL && !VIM_ISWHITE(arg[-1])) 2420 { 2421 arg = p; 2422 group = AUGROUP_ALL; 2423 } 2424 2425 // skip over event name 2426 for (p = arg; *p != NUL && !VIM_ISWHITE(*p); ++p) 2427 if (*p == ',') 2428 arg = p + 1; 2429 if (*p == NUL) 2430 { 2431 if (group == AUGROUP_ALL) 2432 include_groups = TRUE; 2433 xp->xp_context = EXPAND_EVENTS; // expand event name 2434 xp->xp_pattern = arg; 2435 return NULL; 2436 } 2437 2438 // skip over pattern 2439 arg = skipwhite(p); 2440 while (*arg && (!VIM_ISWHITE(*arg) || arg[-1] == '\\')) 2441 arg++; 2442 if (*arg) 2443 return arg; // expand (next) command 2444 2445 if (doautocmd) 2446 xp->xp_context = EXPAND_FILES; // expand file names 2447 else 2448 xp->xp_context = EXPAND_NOTHING; // pattern is not expanded 2449 return NULL; 2450 } 2451 2452 /* 2453 * Function given to ExpandGeneric() to obtain the list of event names. 2454 */ 2455 char_u * 2456 get_event_name(expand_T *xp UNUSED, int idx) 2457 { 2458 if (idx < augroups.ga_len) // First list group names, if wanted 2459 { 2460 if (!include_groups || AUGROUP_NAME(idx) == NULL 2461 || AUGROUP_NAME(idx) == get_deleted_augroup()) 2462 return (char_u *)""; // skip deleted entries 2463 return AUGROUP_NAME(idx); // return a name 2464 } 2465 return (char_u *)event_names[idx - augroups.ga_len].name; 2466 } 2467 2468 #endif // FEAT_CMDL_COMPL 2469 2470 #if defined(FEAT_EVAL) || defined(PROTO) 2471 /* 2472 * Return TRUE if autocmd is supported. 2473 */ 2474 int 2475 autocmd_supported(char_u *name) 2476 { 2477 char_u *p; 2478 2479 return (event_name2nr(name, &p) != NUM_EVENTS); 2480 } 2481 2482 /* 2483 * Return TRUE if an autocommand is defined for a group, event and 2484 * pattern: The group can be omitted to accept any group. "event" and "pattern" 2485 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept 2486 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. 2487 * Used for: 2488 * exists("#Group") or 2489 * exists("#Group#Event") or 2490 * exists("#Group#Event#pat") or 2491 * exists("#Event") or 2492 * exists("#Event#pat") 2493 */ 2494 int 2495 au_exists(char_u *arg) 2496 { 2497 char_u *arg_save; 2498 char_u *pattern = NULL; 2499 char_u *event_name; 2500 char_u *p; 2501 event_T event; 2502 AutoPat *ap; 2503 buf_T *buflocal_buf = NULL; 2504 int group; 2505 int retval = FALSE; 2506 2507 // Make a copy so that we can change the '#' chars to a NUL. 2508 arg_save = vim_strsave(arg); 2509 if (arg_save == NULL) 2510 return FALSE; 2511 p = vim_strchr(arg_save, '#'); 2512 if (p != NULL) 2513 *p++ = NUL; 2514 2515 // First, look for an autocmd group name 2516 group = au_find_group(arg_save); 2517 if (group == AUGROUP_ERROR) 2518 { 2519 // Didn't match a group name, assume the first argument is an event. 2520 group = AUGROUP_ALL; 2521 event_name = arg_save; 2522 } 2523 else 2524 { 2525 if (p == NULL) 2526 { 2527 // "Group": group name is present and it's recognized 2528 retval = TRUE; 2529 goto theend; 2530 } 2531 2532 // Must be "Group#Event" or "Group#Event#pat". 2533 event_name = p; 2534 p = vim_strchr(event_name, '#'); 2535 if (p != NULL) 2536 *p++ = NUL; // "Group#Event#pat" 2537 } 2538 2539 pattern = p; // "pattern" is NULL when there is no pattern 2540 2541 // find the index (enum) for the event name 2542 event = event_name2nr(event_name, &p); 2543 2544 // return FALSE if the event name is not recognized 2545 if (event == NUM_EVENTS) 2546 goto theend; 2547 2548 // Find the first autocommand for this event. 2549 // If there isn't any, return FALSE; 2550 // If there is one and no pattern given, return TRUE; 2551 ap = first_autopat[(int)event]; 2552 if (ap == NULL) 2553 goto theend; 2554 2555 // if pattern is "<buffer>", special handling is needed which uses curbuf 2556 // for pattern "<buffer=N>, fnamecmp() will work fine 2557 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) 2558 buflocal_buf = curbuf; 2559 2560 // Check if there is an autocommand with the given pattern. 2561 for ( ; ap != NULL; ap = ap->next) 2562 // only use a pattern when it has not been removed and has commands. 2563 // For buffer-local autocommands, fnamecmp() works fine. 2564 if (ap->pat != NULL && ap->cmds != NULL 2565 && (group == AUGROUP_ALL || ap->group == group) 2566 && (pattern == NULL 2567 || (buflocal_buf == NULL 2568 ? fnamecmp(ap->pat, pattern) == 0 2569 : ap->buflocal_nr == buflocal_buf->b_fnum))) 2570 { 2571 retval = TRUE; 2572 break; 2573 } 2574 2575 theend: 2576 vim_free(arg_save); 2577 return retval; 2578 } 2579 #endif 2580