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