blob: 6ee6c11afcaa4f5ea5b7fcff022a06758c11ee53 [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).
Bram Moolenaareb93f3f2019-04-04 15:04:56 +020055 char once; // "One shot": removed after execution
Bram Moolenaar3e460fd2019-01-26 16:21:07 +010056 char nested; // If autocommands nest here.
57 char last; // last command in list
LemonBoyeca7c602022-04-14 15:39:43 +010058 sctx_T script_ctx; // script context where it is defined
Bram Moolenaar3e460fd2019-01-26 16:21:07 +010059 struct AutoCmd *next; // next AutoCmd in list
60} AutoCmd;
61
62typedef struct AutoPat
63{
64 struct AutoPat *next; // Next AutoPat in AutoPat list; MUST
65 // be the first entry.
66 char_u *pat; // pattern as typed (NULL when pattern
67 // has been removed)
68 regprog_T *reg_prog; // compiled regprog for pattern
69 AutoCmd *cmds; // list of commands to do
70 int group; // group ID
71 int patlen; // strlen() of pat
72 int buflocal_nr; // !=0 for buffer-local AutoPat
73 char allow_dirs; // Pattern may match whole path
74 char last; // last pattern for apply_autocmds()
75} AutoPat;
76
John Marriott78d742a2024-04-02 20:26:01 +020077//
78// special cases:
79// BufNewFile and BufRead are searched for ALOT (especially at startup)
80// so we pre-determine their index into the event_tab[] table for fast access.
81// Keep these values in sync with event_tab[]!
82#define BUFNEWFILE_INDEX 9
83#define BUFREAD_INDEX 10
84
Luuk van Baalb7147f82025-02-08 18:52:39 +010085// Must be sorted by the 'value' field because it is used by bsearch()!
86// Events with positive keys aren't allowed in 'eventignorewin'.
87static keyvalue_T event_tab[NUM_EVENTS] = {
88 KEYVALUE_ENTRY(-EVENT_BUFADD, "BufAdd"),
89 KEYVALUE_ENTRY(-EVENT_BUFADD, "BufCreate"),
90 KEYVALUE_ENTRY(-EVENT_BUFDELETE, "BufDelete"),
91 KEYVALUE_ENTRY(-EVENT_BUFENTER, "BufEnter"),
92 KEYVALUE_ENTRY(-EVENT_BUFFILEPOST, "BufFilePost"),
93 KEYVALUE_ENTRY(-EVENT_BUFFILEPRE, "BufFilePre"),
94 KEYVALUE_ENTRY(-EVENT_BUFHIDDEN, "BufHidden"),
95 KEYVALUE_ENTRY(-EVENT_BUFLEAVE, "BufLeave"),
96 KEYVALUE_ENTRY(-EVENT_BUFNEW, "BufNew"),
97 KEYVALUE_ENTRY(-EVENT_BUFNEWFILE, "BufNewFile"), // BUFNEWFILE_INDEX
98 KEYVALUE_ENTRY(-EVENT_BUFREADPOST, "BufRead"), // BUFREAD_INDEX
99 KEYVALUE_ENTRY(-EVENT_BUFREADCMD, "BufReadCmd"),
100 KEYVALUE_ENTRY(-EVENT_BUFREADPOST, "BufReadPost"),
101 KEYVALUE_ENTRY(-EVENT_BUFREADPRE, "BufReadPre"),
102 KEYVALUE_ENTRY(-EVENT_BUFUNLOAD, "BufUnload"),
103 KEYVALUE_ENTRY(-EVENT_BUFWINENTER, "BufWinEnter"),
104 KEYVALUE_ENTRY(-EVENT_BUFWINLEAVE, "BufWinLeave"),
105 KEYVALUE_ENTRY(-EVENT_BUFWIPEOUT, "BufWipeout"),
106 KEYVALUE_ENTRY(-EVENT_BUFWRITEPRE, "BufWrite"),
107 KEYVALUE_ENTRY(-EVENT_BUFWRITECMD, "BufWriteCmd"),
108 KEYVALUE_ENTRY(-EVENT_BUFWRITEPOST, "BufWritePost"),
109 KEYVALUE_ENTRY(-EVENT_BUFWRITEPRE, "BufWritePre"),
John Marriott78d742a2024-04-02 20:26:01 +0200110 KEYVALUE_ENTRY(EVENT_CMDLINECHANGED, "CmdlineChanged"),
111 KEYVALUE_ENTRY(EVENT_CMDLINEENTER, "CmdlineEnter"),
112 KEYVALUE_ENTRY(EVENT_CMDLINELEAVE, "CmdlineLeave"),
Girish Palya92f68e22025-04-21 11:12:41 +0200113 KEYVALUE_ENTRY(EVENT_CMDLINELEAVEPRE, "CmdlineLeavePre"),
John Marriott78d742a2024-04-02 20:26:01 +0200114 KEYVALUE_ENTRY(EVENT_CMDUNDEFINED, "CmdUndefined"),
115 KEYVALUE_ENTRY(EVENT_CMDWINENTER, "CmdwinEnter"),
116 KEYVALUE_ENTRY(EVENT_CMDWINLEAVE, "CmdwinLeave"),
117 KEYVALUE_ENTRY(EVENT_COLORSCHEME, "ColorScheme"),
118 KEYVALUE_ENTRY(EVENT_COLORSCHEMEPRE, "ColorSchemePre"),
119 KEYVALUE_ENTRY(EVENT_COMPLETECHANGED, "CompleteChanged"),
120 KEYVALUE_ENTRY(EVENT_COMPLETEDONE, "CompleteDone"),
121 KEYVALUE_ENTRY(EVENT_COMPLETEDONEPRE, "CompleteDonePre"),
Luuk van Baalb7147f82025-02-08 18:52:39 +0100122 KEYVALUE_ENTRY(-EVENT_CURSORHOLD, "CursorHold"),
123 KEYVALUE_ENTRY(-EVENT_CURSORHOLDI, "CursorHoldI"),
124 KEYVALUE_ENTRY(-EVENT_CURSORMOVED, "CursorMoved"),
125 KEYVALUE_ENTRY(-EVENT_CURSORMOVEDC, "CursorMovedC"),
126 KEYVALUE_ENTRY(-EVENT_CURSORMOVEDI, "CursorMovedI"),
John Marriott78d742a2024-04-02 20:26:01 +0200127 KEYVALUE_ENTRY(EVENT_DIFFUPDATED, "DiffUpdated"),
128 KEYVALUE_ENTRY(EVENT_DIRCHANGED, "DirChanged"),
129 KEYVALUE_ENTRY(EVENT_DIRCHANGEDPRE, "DirChangedPre"),
130 KEYVALUE_ENTRY(EVENT_ENCODINGCHANGED, "EncodingChanged"),
131 KEYVALUE_ENTRY(EVENT_EXITPRE, "ExitPre"),
Luuk van Baalb7147f82025-02-08 18:52:39 +0100132 KEYVALUE_ENTRY(-EVENT_FILEAPPENDCMD, "FileAppendCmd"),
133 KEYVALUE_ENTRY(-EVENT_FILEAPPENDPOST, "FileAppendPost"),
134 KEYVALUE_ENTRY(-EVENT_FILEAPPENDPRE, "FileAppendPre"),
135 KEYVALUE_ENTRY(-EVENT_FILECHANGEDRO, "FileChangedRO"),
136 KEYVALUE_ENTRY(-EVENT_FILECHANGEDSHELL, "FileChangedShell"),
137 KEYVALUE_ENTRY(-EVENT_FILECHANGEDSHELLPOST, "FileChangedShellPost"),
John Marriott78d742a2024-04-02 20:26:01 +0200138 KEYVALUE_ENTRY(EVENT_ENCODINGCHANGED, "FileEncoding"),
Luuk van Baalb7147f82025-02-08 18:52:39 +0100139 KEYVALUE_ENTRY(-EVENT_FILEREADCMD, "FileReadCmd"),
140 KEYVALUE_ENTRY(-EVENT_FILEREADPOST, "FileReadPost"),
141 KEYVALUE_ENTRY(-EVENT_FILEREADPRE, "FileReadPre"),
142 KEYVALUE_ENTRY(-EVENT_FILETYPE, "FileType"),
143 KEYVALUE_ENTRY(-EVENT_FILEWRITECMD, "FileWriteCmd"),
144 KEYVALUE_ENTRY(-EVENT_FILEWRITEPOST, "FileWritePost"),
145 KEYVALUE_ENTRY(-EVENT_FILEWRITEPRE, "FileWritePre"),
146 KEYVALUE_ENTRY(-EVENT_FILTERREADPOST, "FilterReadPost"),
147 KEYVALUE_ENTRY(-EVENT_FILTERREADPRE, "FilterReadPre"),
148 KEYVALUE_ENTRY(-EVENT_FILTERWRITEPOST, "FilterWritePost"),
149 KEYVALUE_ENTRY(-EVENT_FILTERWRITEPRE, "FilterWritePre"),
John Marriott78d742a2024-04-02 20:26:01 +0200150 KEYVALUE_ENTRY(EVENT_FOCUSGAINED, "FocusGained"),
151 KEYVALUE_ENTRY(EVENT_FOCUSLOST, "FocusLost"),
152 KEYVALUE_ENTRY(EVENT_FUNCUNDEFINED, "FuncUndefined"),
153 KEYVALUE_ENTRY(EVENT_GUIENTER, "GUIEnter"),
154 KEYVALUE_ENTRY(EVENT_GUIFAILED, "GUIFailed"),
Luuk van Baalb7147f82025-02-08 18:52:39 +0100155 KEYVALUE_ENTRY(-EVENT_INSERTCHANGE, "InsertChange"),
156 KEYVALUE_ENTRY(-EVENT_INSERTCHARPRE, "InsertCharPre"),
157 KEYVALUE_ENTRY(-EVENT_INSERTENTER, "InsertEnter"),
158 KEYVALUE_ENTRY(-EVENT_INSERTLEAVE, "InsertLeave"),
159 KEYVALUE_ENTRY(-EVENT_INSERTLEAVEPRE, "InsertLeavePre"),
Shougo Matsushita83678842024-07-11 22:05:12 +0200160 KEYVALUE_ENTRY(EVENT_KEYINPUTPRE, "KeyInputPre"),
John Marriott78d742a2024-04-02 20:26:01 +0200161 KEYVALUE_ENTRY(EVENT_MENUPOPUP, "MenuPopup"),
162 KEYVALUE_ENTRY(EVENT_MODECHANGED, "ModeChanged"),
163 KEYVALUE_ENTRY(EVENT_OPTIONSET, "OptionSet"),
164 KEYVALUE_ENTRY(EVENT_QUICKFIXCMDPOST, "QuickFixCmdPost"),
165 KEYVALUE_ENTRY(EVENT_QUICKFIXCMDPRE, "QuickFixCmdPre"),
166 KEYVALUE_ENTRY(EVENT_QUITPRE, "QuitPre"),
167 KEYVALUE_ENTRY(EVENT_REMOTEREPLY, "RemoteReply"),
168 KEYVALUE_ENTRY(EVENT_SAFESTATE, "SafeState"),
169 KEYVALUE_ENTRY(EVENT_SAFESTATEAGAIN, "SafeStateAgain"),
170 KEYVALUE_ENTRY(EVENT_SESSIONLOADPOST, "SessionLoadPost"),
171 KEYVALUE_ENTRY(EVENT_SESSIONWRITEPOST, "SessionWritePost"),
172 KEYVALUE_ENTRY(EVENT_SHELLCMDPOST, "ShellCmdPost"),
Luuk van Baalb7147f82025-02-08 18:52:39 +0100173 KEYVALUE_ENTRY(-EVENT_SHELLFILTERPOST, "ShellFilterPost"),
John Marriott78d742a2024-04-02 20:26:01 +0200174 KEYVALUE_ENTRY(EVENT_SIGUSR1, "SigUSR1"),
175 KEYVALUE_ENTRY(EVENT_SOURCECMD, "SourceCmd"),
176 KEYVALUE_ENTRY(EVENT_SOURCEPOST, "SourcePost"),
177 KEYVALUE_ENTRY(EVENT_SOURCEPRE, "SourcePre"),
178 KEYVALUE_ENTRY(EVENT_SPELLFILEMISSING, "SpellFileMissing"),
179 KEYVALUE_ENTRY(EVENT_STDINREADPOST, "StdinReadPost"),
180 KEYVALUE_ENTRY(EVENT_STDINREADPRE, "StdinReadPre"),
181 KEYVALUE_ENTRY(EVENT_SWAPEXISTS, "SwapExists"),
182 KEYVALUE_ENTRY(EVENT_SYNTAX, "Syntax"),
183 KEYVALUE_ENTRY(EVENT_TABCLOSED, "TabClosed"),
Jim Zhou5606ca52025-03-13 21:58:25 +0100184 KEYVALUE_ENTRY(EVENT_TABCLOSEDPRE, "TabClosedPre"),
John Marriott78d742a2024-04-02 20:26:01 +0200185 KEYVALUE_ENTRY(EVENT_TABENTER, "TabEnter"),
186 KEYVALUE_ENTRY(EVENT_TABLEAVE, "TabLeave"),
187 KEYVALUE_ENTRY(EVENT_TABNEW, "TabNew"),
188 KEYVALUE_ENTRY(EVENT_TERMCHANGED, "TermChanged"),
189 KEYVALUE_ENTRY(EVENT_TERMINALOPEN, "TerminalOpen"),
190 KEYVALUE_ENTRY(EVENT_TERMINALWINOPEN, "TerminalWinOpen"),
191 KEYVALUE_ENTRY(EVENT_TERMRESPONSE, "TermResponse"),
192 KEYVALUE_ENTRY(EVENT_TERMRESPONSEALL, "TermResponseAll"),
Luuk van Baalb7147f82025-02-08 18:52:39 +0100193 KEYVALUE_ENTRY(-EVENT_TEXTCHANGED, "TextChanged"),
194 KEYVALUE_ENTRY(-EVENT_TEXTCHANGEDI, "TextChangedI"),
195 KEYVALUE_ENTRY(-EVENT_TEXTCHANGEDP, "TextChangedP"),
196 KEYVALUE_ENTRY(-EVENT_TEXTCHANGEDT, "TextChangedT"),
197 KEYVALUE_ENTRY(-EVENT_TEXTYANKPOST, "TextYankPost"),
John Marriott78d742a2024-04-02 20:26:01 +0200198 KEYVALUE_ENTRY(EVENT_USER, "User"),
199 KEYVALUE_ENTRY(EVENT_VIMENTER, "VimEnter"),
200 KEYVALUE_ENTRY(EVENT_VIMLEAVE, "VimLeave"),
201 KEYVALUE_ENTRY(EVENT_VIMLEAVEPRE, "VimLeavePre"),
202 KEYVALUE_ENTRY(EVENT_VIMRESIZED, "VimResized"),
203 KEYVALUE_ENTRY(EVENT_VIMRESUME, "VimResume"),
204 KEYVALUE_ENTRY(EVENT_VIMSUSPEND, "VimSuspend"),
Luuk van Baalb7147f82025-02-08 18:52:39 +0100205 KEYVALUE_ENTRY(-EVENT_WINCLOSED, "WinClosed"),
206 KEYVALUE_ENTRY(-EVENT_WINENTER, "WinEnter"),
207 KEYVALUE_ENTRY(-EVENT_WINLEAVE, "WinLeave"),
John Marriott78d742a2024-04-02 20:26:01 +0200208 KEYVALUE_ENTRY(EVENT_WINNEW, "WinNew"),
209 KEYVALUE_ENTRY(EVENT_WINNEWPRE, "WinNewPre"),
Luuk van Baalb7147f82025-02-08 18:52:39 +0100210 KEYVALUE_ENTRY(-EVENT_WINRESIZED, "WinResized"),
211 KEYVALUE_ENTRY(-EVENT_WINSCROLLED, "WinScrolled"),
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100212};
213
Luuk van Baalb7147f82025-02-08 18:52:39 +0100214static AutoPat *first_autopat[NUM_EVENTS] = { NULL };
215static AutoPat *last_autopat[NUM_EVENTS] = { NULL };
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100216
kylo252ae6f1d82022-02-16 19:24:07 +0000217#define AUGROUP_DEFAULT (-1) // default autocmd group
218#define AUGROUP_ERROR (-2) // erroneous autocmd group
219#define AUGROUP_ALL (-3) // all autocmd groups
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100220
221/*
222 * struct used to keep status while executing autocommands for an event.
223 */
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100224struct AutoPatCmd_S
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100225{
226 AutoPat *curpat; // next AutoPat to examine
227 AutoCmd *nextcmd; // next AutoCmd to execute
228 int group; // group being used
229 char_u *fname; // fname to match with
230 char_u *sfname; // sfname to match with
231 char_u *tail; // tail of fname
232 event_T event; // current event
LemonBoyeca7c602022-04-14 15:39:43 +0100233 sctx_T script_ctx; // script context where it is defined
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100234 int arg_bufnr; // Initially equal to <abuf>, set to zero when
235 // buf is deleted.
LemonBoyeca7c602022-04-14 15:39:43 +0100236 AutoPatCmd_T *next; // chain of active apc-s for auto-invalidation
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100237};
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100238
LemonBoyeca7c602022-04-14 15:39:43 +0100239static AutoPatCmd_T *active_apc_list = NULL; // stack of active autocommands
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100240
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200241// Macro to loop over all the patterns for an autocmd event
242#define FOR_ALL_AUTOCMD_PATTERNS(event, ap) \
243 for ((ap) = first_autopat[(int)(event)]; (ap) != NULL; (ap) = (ap)->next)
244
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100245/*
246 * augroups stores a list of autocmd group names.
247 */
248static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL};
249#define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i])
Bram Moolenaarc667da52019-11-30 20:52:27 +0100250// use get_deleted_augroup() to get this
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100251static char_u *deleted_augroup = NULL;
252
253/*
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100254 * The ID of the current group. Group 0 is the default one.
255 */
256static int current_augroup = AUGROUP_DEFAULT;
257
Bram Moolenaarc667da52019-11-30 20:52:27 +0100258static int au_need_clean = FALSE; // need to delete marked patterns
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100259
John Marriott78d742a2024-04-02 20:26:01 +0200260static event_T event_name2nr(char_u *start, char_u **end);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100261static char_u *event_nr2name(event_T event);
262static int au_get_grouparg(char_u **argp);
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +0200263static int do_autocmd_event(event_T event, char_u *pat, int once, int nested, char_u *cmd, int forceit, int group, int flags);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100264static int apply_autocmds_group(event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap);
LemonBoyeca7c602022-04-14 15:39:43 +0100265static void auto_next_pat(AutoPatCmd_T *apc, int stop_at_last);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100266static int au_find_group(char_u *name);
267
268static event_T last_event;
269static int last_group;
Bram Moolenaarc667da52019-11-30 20:52:27 +0100270static int autocmd_blocked = 0; // block all autocmds
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100271
272 static char_u *
273get_deleted_augroup(void)
274{
275 if (deleted_augroup == NULL)
276 deleted_augroup = (char_u *)_("--Deleted--");
277 return deleted_augroup;
278}
279
280/*
281 * Show the autocommands for one AutoPat.
282 */
283 static void
284show_autocmd(AutoPat *ap, event_T event)
285{
286 AutoCmd *ac;
287
288 // Check for "got_int" (here and at various places below), which is set
289 // when "q" has been hit for the "--more--" prompt
290 if (got_int)
291 return;
292 if (ap->pat == NULL) // pattern has been removed
293 return;
294
Bram Moolenaar3b014be2022-11-13 17:53:46 +0000295 // Make sure no info referenced by "ap" is cleared, e.g. when a timer
296 // clears an augroup. Jump to "theend" after this!
297 // "ap->pat" may be cleared anyway.
298 ++autocmd_busy;
299
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100300 msg_putchar('\n');
301 if (got_int)
Bram Moolenaar3b014be2022-11-13 17:53:46 +0000302 goto theend;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100303 if (event != last_event || ap->group != last_group)
304 {
305 if (ap->group != AUGROUP_DEFAULT)
306 {
307 if (AUGROUP_NAME(ap->group) == NULL)
308 msg_puts_attr((char *)get_deleted_augroup(), HL_ATTR(HLF_E));
309 else
310 msg_puts_attr((char *)AUGROUP_NAME(ap->group), HL_ATTR(HLF_T));
311 msg_puts(" ");
312 }
313 msg_puts_attr((char *)event_nr2name(event), HL_ATTR(HLF_T));
314 last_event = event;
315 last_group = ap->group;
316 msg_putchar('\n');
317 if (got_int)
Bram Moolenaar3b014be2022-11-13 17:53:46 +0000318 goto theend;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100319 }
Bram Moolenaar3b014be2022-11-13 17:53:46 +0000320
321 if (ap->pat == NULL)
322 goto theend; // timer might have cleared the pattern or group
323
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100324 msg_col = 4;
325 msg_outtrans(ap->pat);
326
327 for (ac = ap->cmds; ac != NULL; ac = ac->next)
328 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100329 if (ac->cmd == NULL) // skip removed commands
330 continue;
331
332 if (msg_col >= 14)
333 msg_putchar('\n');
334 msg_col = 14;
335 if (got_int)
Bram Moolenaar3b014be2022-11-13 17:53:46 +0000336 goto theend;
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100337 msg_outtrans(ac->cmd);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100338#ifdef FEAT_EVAL
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100339 if (p_verbose > 0)
340 last_set_msg(ac->script_ctx);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100341#endif
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100342 if (got_int)
Bram Moolenaar3b014be2022-11-13 17:53:46 +0000343 goto theend;
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100344 if (ac->next != NULL)
345 {
346 msg_putchar('\n');
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100347 if (got_int)
Bram Moolenaar3b014be2022-11-13 17:53:46 +0000348 goto theend;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100349 }
350 }
Bram Moolenaar3b014be2022-11-13 17:53:46 +0000351
352theend:
353 --autocmd_busy;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100354}
355
356/*
357 * Mark an autocommand pattern for deletion.
358 */
359 static void
360au_remove_pat(AutoPat *ap)
361{
362 VIM_CLEAR(ap->pat);
363 ap->buflocal_nr = -1;
364 au_need_clean = TRUE;
365}
366
367/*
368 * Mark all commands for a pattern for deletion.
369 */
370 static void
371au_remove_cmds(AutoPat *ap)
372{
373 AutoCmd *ac;
374
375 for (ac = ap->cmds; ac != NULL; ac = ac->next)
376 VIM_CLEAR(ac->cmd);
377 au_need_clean = TRUE;
378}
379
Bram Moolenaareb93f3f2019-04-04 15:04:56 +0200380// Delete one command from an autocmd pattern.
381static void au_del_cmd(AutoCmd *ac)
382{
383 VIM_CLEAR(ac->cmd);
384 au_need_clean = TRUE;
385}
386
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100387/*
388 * Cleanup autocommands and patterns that have been deleted.
389 * This is only done when not executing autocommands.
390 */
391 static void
392au_cleanup(void)
393{
394 AutoPat *ap, **prev_ap;
395 AutoCmd *ac, **prev_ac;
396 event_T event;
397
398 if (autocmd_busy || !au_need_clean)
399 return;
400
401 // loop over all events
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100402 for (event = (event_T)0; (int)event < NUM_EVENTS;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100403 event = (event_T)((int)event + 1))
404 {
405 // loop over all autocommand patterns
406 prev_ap = &(first_autopat[(int)event]);
407 for (ap = *prev_ap; ap != NULL; ap = *prev_ap)
408 {
Bram Moolenaareb93f3f2019-04-04 15:04:56 +0200409 int has_cmd = FALSE;
410
Bram Moolenaar8f4aeb52019-04-04 15:40:56 +0200411 // loop over all commands for this pattern
412 prev_ac = &(ap->cmds);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100413 for (ac = *prev_ac; ac != NULL; ac = *prev_ac)
414 {
415 // remove the command if the pattern is to be deleted or when
416 // the command has been marked for deletion
417 if (ap->pat == NULL || ac->cmd == NULL)
418 {
419 *prev_ac = ac->next;
420 vim_free(ac->cmd);
421 vim_free(ac);
422 }
Bram Moolenaar8f4aeb52019-04-04 15:40:56 +0200423 else
424 {
Bram Moolenaareb93f3f2019-04-04 15:04:56 +0200425 has_cmd = TRUE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100426 prev_ac = &(ac->next);
Bram Moolenaareb93f3f2019-04-04 15:04:56 +0200427 }
428 }
429
Bram Moolenaar8f4aeb52019-04-04 15:40:56 +0200430 if (ap->pat != NULL && !has_cmd)
Bram Moolenaareb93f3f2019-04-04 15:04:56 +0200431 // Pattern was not marked for deletion, but all of its
432 // commands were. So mark the pattern for deletion.
433 au_remove_pat(ap);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100434
435 // remove the pattern if it has been marked for deletion
436 if (ap->pat == NULL)
437 {
438 if (ap->next == NULL)
439 {
440 if (prev_ap == &(first_autopat[(int)event]))
441 last_autopat[(int)event] = NULL;
442 else
443 // this depends on the "next" field being the first in
444 // the struct
445 last_autopat[(int)event] = (AutoPat *)prev_ap;
446 }
447 *prev_ap = ap->next;
448 vim_regfree(ap->reg_prog);
449 vim_free(ap);
450 }
451 else
452 prev_ap = &(ap->next);
453 }
454 }
455
456 au_need_clean = FALSE;
457}
458
459/*
460 * Called when buffer is freed, to remove/invalidate related buffer-local
461 * autocmds.
462 */
463 void
464aubuflocal_remove(buf_T *buf)
465{
LemonBoyeca7c602022-04-14 15:39:43 +0100466 AutoPat *ap;
467 event_T event;
468 AutoPatCmd_T *apc;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100469
470 // invalidate currently executing autocommands
471 for (apc = active_apc_list; apc; apc = apc->next)
472 if (buf->b_fnum == apc->arg_bufnr)
473 apc->arg_bufnr = 0;
474
475 // invalidate buflocals looping through events
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100476 for (event = (event_T)0; (int)event < NUM_EVENTS;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100477 event = (event_T)((int)event + 1))
478 // loop over all autocommand patterns
Bram Moolenaaraeea7212020-04-02 18:50:46 +0200479 FOR_ALL_AUTOCMD_PATTERNS(event, ap)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100480 if (ap->buflocal_nr == buf->b_fnum)
481 {
482 au_remove_pat(ap);
483 if (p_verbose >= 6)
484 {
485 verbose_enter();
486 smsg(_("auto-removing autocommand: %s <buffer=%d>"),
487 event_nr2name(event), buf->b_fnum);
488 verbose_leave();
489 }
490 }
491 au_cleanup();
492}
493
494/*
495 * Add an autocmd group name.
496 * Return its ID. Returns AUGROUP_ERROR (< 0) for error.
497 */
498 static int
499au_new_group(char_u *name)
500{
501 int i;
502
503 i = au_find_group(name);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100504 if (i != AUGROUP_ERROR)
505 return i;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100506
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100507 // the group doesn't exist yet, add it. First try using a free entry.
508 for (i = 0; i < augroups.ga_len; ++i)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100509 if (AUGROUP_NAME(i) == NULL)
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100510 break;
511 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL)
512 return AUGROUP_ERROR;
513
514 AUGROUP_NAME(i) = vim_strsave(name);
515 if (AUGROUP_NAME(i) == NULL)
516 return AUGROUP_ERROR;
517 if (i == augroups.ga_len)
518 ++augroups.ga_len;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100519
520 return i;
521}
522
523 static void
524au_del_group(char_u *name)
525{
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100526 int i;
527 event_T event;
528 AutoPat *ap;
529 int in_use = FALSE;
530
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100531
532 i = au_find_group(name);
533 if (i == AUGROUP_ERROR) // the group doesn't exist
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100534 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100535 semsg(_(e_no_such_group_str), name);
536 return;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100537 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100538 if (i == current_augroup)
539 {
540 emsg(_(e_cannot_delete_current_group));
541 return;
542 }
543
544 for (event = (event_T)0; (int)event < NUM_EVENTS;
545 event = (event_T)((int)event + 1))
546 {
547 FOR_ALL_AUTOCMD_PATTERNS(event, ap)
548 if (ap->group == i && ap->pat != NULL)
549 {
550 give_warning((char_u *)_("W19: Deleting augroup that is still in use"), TRUE);
551 in_use = TRUE;
552 event = NUM_EVENTS;
553 break;
554 }
555 }
556 vim_free(AUGROUP_NAME(i));
557 if (in_use)
558 AUGROUP_NAME(i) = get_deleted_augroup();
559 else
560 AUGROUP_NAME(i) = NULL;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100561}
562
563/*
564 * Find the ID of an autocmd group name.
565 * Return its ID. Returns AUGROUP_ERROR (< 0) for error.
566 */
567 static int
568au_find_group(char_u *name)
569{
570 int i;
571
572 for (i = 0; i < augroups.ga_len; ++i)
573 if (AUGROUP_NAME(i) != NULL && AUGROUP_NAME(i) != get_deleted_augroup()
574 && STRCMP(AUGROUP_NAME(i), name) == 0)
575 return i;
576 return AUGROUP_ERROR;
577}
578
579/*
580 * Return TRUE if augroup "name" exists.
581 */
582 int
583au_has_group(char_u *name)
584{
585 return au_find_group(name) != AUGROUP_ERROR;
586}
587
588/*
589 * ":augroup {name}".
590 */
591 void
592do_augroup(char_u *arg, int del_group)
593{
594 int i;
595
596 if (del_group)
597 {
598 if (*arg == NUL)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000599 emsg(_(e_argument_required));
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100600 else
601 au_del_group(arg);
602 }
603 else if (STRICMP(arg, "end") == 0) // ":aug end": back to group 0
604 current_augroup = AUGROUP_DEFAULT;
605 else if (*arg) // ":aug xxx": switch to group xxx
606 {
607 i = au_new_group(arg);
608 if (i != AUGROUP_ERROR)
609 current_augroup = i;
610 }
611 else // ":aug": list the group names
612 {
613 msg_start();
614 for (i = 0; i < augroups.ga_len; ++i)
615 {
616 if (AUGROUP_NAME(i) != NULL)
617 {
618 msg_puts((char *)AUGROUP_NAME(i));
619 msg_puts(" ");
620 }
621 }
622 msg_clr_eos();
623 msg_end();
624 }
625}
626
Bram Moolenaare76062c2022-11-28 18:51:43 +0000627 void
628autocmd_init(void)
629{
630 CLEAR_FIELD(aucmd_win);
631}
632
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100633#if defined(EXITFREE) || defined(PROTO)
634 void
635free_all_autocmds(void)
636{
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100637 char_u *s;
638
639 for (current_augroup = -1; current_augroup < augroups.ga_len;
640 ++current_augroup)
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +0200641 do_autocmd(NULL, (char_u *)"", TRUE);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100642
Bram Moolenaare76062c2022-11-28 18:51:43 +0000643 for (int i = 0; i < augroups.ga_len; ++i)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100644 {
645 s = ((char_u **)(augroups.ga_data))[i];
646 if (s != get_deleted_augroup())
647 vim_free(s);
648 }
649 ga_clear(&augroups);
Bram Moolenaare76062c2022-11-28 18:51:43 +0000650
Bram Moolenaar84497cd2022-11-28 20:34:52 +0000651 // aucmd_win[] is freed in win_free_all()
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100652}
653#endif
654
655/*
Bram Moolenaare76062c2022-11-28 18:51:43 +0000656 * Return TRUE if "win" is an active entry in aucmd_win[].
657 */
658 int
659is_aucmd_win(win_T *win)
660{
661 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
662 if (aucmd_win[i].auc_win_used && aucmd_win[i].auc_win == win)
663 return TRUE;
664 return FALSE;
665}
666
667/*
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100668 * Return the event number for event name "start".
669 * Return NUM_EVENTS if the event name was not found.
670 * Return a pointer to the next event name in "end".
671 */
672 static event_T
673event_name2nr(char_u *start, char_u **end)
674{
675 char_u *p;
John Marriott78d742a2024-04-02 20:26:01 +0200676 keyvalue_T target;
677 keyvalue_T *entry;
678 static keyvalue_T *bufnewfile = &event_tab[BUFNEWFILE_INDEX];
679 static keyvalue_T *bufread = &event_tab[BUFREAD_INDEX];
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100680
681 // the event name ends with end of line, '|', a blank or a comma
682 for (p = start; *p && !VIM_ISWHITE(*p) && *p != ',' && *p != '|'; ++p)
683 ;
John Marriott78d742a2024-04-02 20:26:01 +0200684
685 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +0100686 target.value.string = start;
687 target.value.length = (size_t)(p - start);
John Marriott78d742a2024-04-02 20:26:01 +0200688
689 // special cases:
690 // BufNewFile and BufRead are searched for ALOT (especially at startup)
691 // so we check for them first.
692 if (cmp_keyvalue_value_ni(&target, bufnewfile) == 0)
693 entry = bufnewfile;
Luuk van Baalb7147f82025-02-08 18:52:39 +0100694 else if (cmp_keyvalue_value_ni(&target, bufread) == 0)
John Marriott78d742a2024-04-02 20:26:01 +0200695 entry = bufread;
696 else
Luuk van Baalb7147f82025-02-08 18:52:39 +0100697 entry = (keyvalue_T *)bsearch(&target, &event_tab, NUM_EVENTS,
698 sizeof(event_tab[0]), cmp_keyvalue_value_ni);
John Marriott78d742a2024-04-02 20:26:01 +0200699
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100700 if (*p == ',')
701 ++p;
702 *end = p;
John Marriott78d742a2024-04-02 20:26:01 +0200703
Luuk van Baalb7147f82025-02-08 18:52:39 +0100704 return (entry == NULL) ? NUM_EVENTS : (event_T)abs(entry->key);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100705}
706
707/*
708 * Return the name for event "event".
709 */
710 static char_u *
711event_nr2name(event_T event)
712{
713 int i;
John Marriott78d742a2024-04-02 20:26:01 +0200714#define CACHE_SIZE 12
715 static int cache_tab[CACHE_SIZE];
716 static int cache_last_index = -1;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100717
John Marriott78d742a2024-04-02 20:26:01 +0200718 if (cache_last_index < 0)
719 {
Luuk van Baalb7147f82025-02-08 18:52:39 +0100720 for (i = 0; i < CACHE_SIZE; ++i)
John Marriott78d742a2024-04-02 20:26:01 +0200721 cache_tab[i] = -1;
Luuk van Baalb7147f82025-02-08 18:52:39 +0100722 cache_last_index = CACHE_SIZE - 1;
John Marriott78d742a2024-04-02 20:26:01 +0200723 }
724
725 // first look in the cache
726 // the cache is circular. to search it we start at the most recent entry
727 // and go backwards wrapping around when we get to index 0.
728 for (i = cache_last_index; cache_tab[i] >= 0; )
729 {
Luuk van Baalb7147f82025-02-08 18:52:39 +0100730 if ((event_T)abs(event_tab[cache_tab[i]].key) == event)
John Marriott8d4477e2024-11-02 15:59:01 +0100731 return event_tab[cache_tab[i]].value.string;
John Marriott78d742a2024-04-02 20:26:01 +0200732
733 if (i == 0)
Luuk van Baalb7147f82025-02-08 18:52:39 +0100734 i = CACHE_SIZE - 1;
John Marriott78d742a2024-04-02 20:26:01 +0200735 else
736 --i;
737
738 // are we back at the start?
739 if (i == cache_last_index)
740 break;
741 }
742
743 // look in the event table itself
Luuk van Baalb7147f82025-02-08 18:52:39 +0100744 for (i = 0; i < NUM_EVENTS; ++i)
John Marriott78d742a2024-04-02 20:26:01 +0200745 {
Luuk van Baalb7147f82025-02-08 18:52:39 +0100746 if ((event_T)abs(event_tab[i].key) == event)
John Marriott78d742a2024-04-02 20:26:01 +0200747 {
748 // store the found entry in the next position in the cache,
749 // wrapping around when we get to the maximum index.
Luuk van Baalb7147f82025-02-08 18:52:39 +0100750 if (cache_last_index == CACHE_SIZE - 1)
John Marriott78d742a2024-04-02 20:26:01 +0200751 cache_last_index = 0;
752 else
753 ++cache_last_index;
754 cache_tab[cache_last_index] = i;
755 break;
756 }
757 }
758
Luuk van Baalb7147f82025-02-08 18:52:39 +0100759 return (i == NUM_EVENTS) ? (char_u *)"Unknown" :
760 event_tab[i].value.string;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100761}
762
763/*
764 * Scan over the events. "*" stands for all events.
765 */
766 static char_u *
767find_end_event(
768 char_u *arg,
769 int have_group) // TRUE when group name was found
770{
771 char_u *pat;
772 char_u *p;
773
774 if (*arg == '*')
775 {
776 if (arg[1] && !VIM_ISWHITE(arg[1]))
777 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000778 semsg(_(e_illegal_character_after_star_str), arg);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100779 return NULL;
780 }
781 pat = arg + 1;
782 }
783 else
784 {
785 for (pat = arg; *pat && *pat != '|' && !VIM_ISWHITE(*pat); pat = p)
786 {
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +0100787 if ((int)event_name2nr(pat, &p) >= NUM_EVENTS)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100788 {
789 if (have_group)
Bram Moolenaar6d057012021-12-31 18:49:43 +0000790 semsg(_(e_no_such_event_str), pat);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100791 else
Bram Moolenaar6d057012021-12-31 18:49:43 +0000792 semsg(_(e_no_such_group_or_event_str), pat);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100793 return NULL;
794 }
795 }
796 }
797 return pat;
798}
799
800/*
Luuk van Baalb7147f82025-02-08 18:52:39 +0100801 * Return TRUE if "event" is included in 'eventignore(win)'.
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100802 */
Luuk van Baalb7147f82025-02-08 18:52:39 +0100803 int
804event_ignored(event_T event, char_u *ei)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100805{
Luuk van Baalb7147f82025-02-08 18:52:39 +0100806 while (*ei != NUL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100807 {
Luuk van Baalb7147f82025-02-08 18:52:39 +0100808 if (STRNICMP(ei, "all", 3) == 0 && (ei[3] == NUL || ei[3] == ',')
809 && (ei == p_ei || (event_tab[event].key <= 0)))
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100810 return TRUE;
Luuk van Baalb7147f82025-02-08 18:52:39 +0100811 if (event_name2nr(ei, &ei) == event)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100812 return TRUE;
813 }
814
815 return FALSE;
816}
817
818/*
Luuk van Baalb7147f82025-02-08 18:52:39 +0100819 * Return OK when the contents of 'eventignore' or 'eventignorewin' is valid,
820 * FAIL otherwise.
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100821 */
822 int
Luuk van Baalb7147f82025-02-08 18:52:39 +0100823check_ei(char_u *ei)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100824{
Luuk van Baalb7147f82025-02-08 18:52:39 +0100825 int win = ei != p_ei;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100826
Luuk van Baalb7147f82025-02-08 18:52:39 +0100827 while (*ei)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100828 {
Luuk van Baalb7147f82025-02-08 18:52:39 +0100829 if (STRNICMP(ei, "all", 3) == 0 && (ei[3] == NUL || ei[3] == ','))
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100830 {
Luuk van Baalb7147f82025-02-08 18:52:39 +0100831 ei += 3;
832 if (*ei == ',')
833 ++ei;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100834 }
Luuk van Baalb7147f82025-02-08 18:52:39 +0100835 else
836 {
837 event_T event = event_name2nr(ei, &ei);
838 if (event == NUM_EVENTS || (win && event_tab[event].key > 0))
839 return FAIL;
840 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100841 }
842
843 return OK;
844}
845
846# if defined(FEAT_SYN_HL) || defined(PROTO)
847
848/*
849 * Add "what" to 'eventignore' to skip loading syntax highlighting for every
850 * buffer loaded into the window. "what" must start with a comma.
851 * Returns the old value of 'eventignore' in allocated memory.
852 */
853 char_u *
854au_event_disable(char *what)
855{
856 char_u *new_ei;
857 char_u *save_ei;
John Marriott78d742a2024-04-02 20:26:01 +0200858 size_t p_ei_len;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100859
John Marriott78d742a2024-04-02 20:26:01 +0200860 p_ei_len = STRLEN(p_ei);
861 save_ei = vim_strnsave(p_ei, p_ei_len);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100862 if (save_ei == NULL)
863 return NULL;
864
John Marriott78d742a2024-04-02 20:26:01 +0200865 new_ei = vim_strnsave(p_ei, p_ei_len + STRLEN(what));
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100866 if (new_ei == NULL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100867 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100868 vim_free(save_ei);
869 return NULL;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100870 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100871
872 if (*what == ',' && *p_ei == NUL)
873 STRCPY(new_ei, what + 1);
874 else
zeertzjq969e11a2025-03-10 21:15:19 +0100875 STRCPY(new_ei + p_ei_len, what);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +0100876 set_string_option_direct((char_u *)"ei", -1, new_ei,
877 OPT_FREE, SID_NONE);
878 vim_free(new_ei);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100879 return save_ei;
880}
881
882 void
883au_event_restore(char_u *old_ei)
884{
885 if (old_ei != NULL)
886 {
887 set_string_option_direct((char_u *)"ei", -1, old_ei,
888 OPT_FREE, SID_NONE);
889 vim_free(old_ei);
890 }
891}
Bram Moolenaarc667da52019-11-30 20:52:27 +0100892# endif // FEAT_SYN_HL
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100893
894/*
895 * do_autocmd() -- implements the :autocmd command. Can be used in the
896 * following ways:
897 *
898 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that
899 * will be automatically executed for <event>
900 * when editing a file matching <pat>, in
901 * the current group.
902 * :autocmd <event> <pat> Show the autocommands associated with
903 * <event> and <pat>.
904 * :autocmd <event> Show the autocommands associated with
905 * <event>.
906 * :autocmd Show all autocommands.
907 * :autocmd! <event> <pat> <cmd> Remove all autocommands associated with
908 * <event> and <pat>, and add the command
909 * <cmd>, for the current group.
910 * :autocmd! <event> <pat> Remove all autocommands associated with
911 * <event> and <pat> for the current group.
912 * :autocmd! <event> Remove all autocommands associated with
913 * <event> for the current group.
914 * :autocmd! Remove ALL autocommands for the current
915 * group.
916 *
917 * Multiple events and patterns may be given separated by commas. Here are
918 * some examples:
919 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic
920 * :autocmd bufleave * set tw=79 nosmartindent ic infercase
921 *
922 * :autocmd * *.c show all autocommands for *.c files.
923 *
924 * Mostly a {group} argument can optionally appear before <event>.
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +0200925 * "eap" can be NULL.
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100926 */
927 void
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +0200928do_autocmd(exarg_T *eap, char_u *arg_in, int forceit)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100929{
930 char_u *arg = arg_in;
931 char_u *pat;
932 char_u *envpat = NULL;
933 char_u *cmd;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +0200934 int cmd_need_free = FALSE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100935 event_T event;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +0200936 char_u *tofree = NULL;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100937 int nested = FALSE;
Bram Moolenaareb93f3f2019-04-04 15:04:56 +0200938 int once = FALSE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100939 int group;
Bram Moolenaareb93f3f2019-04-04 15:04:56 +0200940 int i;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +0200941 int flags = 0;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100942
943 if (*arg == '|')
944 {
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000945 eap->nextcmd = arg + 1;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100946 arg = (char_u *)"";
947 group = AUGROUP_ALL; // no argument, use all groups
948 }
949 else
950 {
951 /*
952 * Check for a legal group name. If not, use AUGROUP_ALL.
953 */
954 group = au_get_grouparg(&arg);
955 if (arg == NULL) // out of memory
956 return;
957 }
958
959 /*
960 * Scan over the events.
961 * If we find an illegal name, return here, don't do anything.
962 */
963 pat = find_end_event(arg, group != AUGROUP_ALL);
964 if (pat == NULL)
965 return;
966
967 pat = skipwhite(pat);
968 if (*pat == '|')
969 {
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000970 eap->nextcmd = pat + 1;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +0100971 pat = (char_u *)"";
972 cmd = (char_u *)"";
973 }
974 else
975 {
976 /*
977 * Scan over the pattern. Put a NUL at the end.
978 */
979 cmd = pat;
980 while (*cmd && (!VIM_ISWHITE(*cmd) || cmd[-1] == '\\'))
981 cmd++;
982 if (*cmd)
983 *cmd++ = NUL;
984
985 // Expand environment variables in the pattern. Set 'shellslash', we
986 // want forward slashes here.
987 if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL)
988 {
989#ifdef BACKSLASH_IN_FILENAME
990 int p_ssl_save = p_ssl;
991
992 p_ssl = TRUE;
993#endif
994 envpat = expand_env_save(pat);
995#ifdef BACKSLASH_IN_FILENAME
996 p_ssl = p_ssl_save;
997#endif
998 if (envpat != NULL)
999 pat = envpat;
1000 }
1001
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001002 cmd = skipwhite(cmd);
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001003 for (i = 0; i < 2; i++)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001004 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001005 if (*cmd == NUL)
1006 continue;
1007
1008 // Check for "++once" flag.
1009 if (STRNCMP(cmd, "++once", 6) == 0 && VIM_ISWHITE(cmd[6]))
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001010 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001011 if (once)
1012 semsg(_(e_duplicate_argument_str), "++once");
1013 once = TRUE;
1014 cmd = skipwhite(cmd + 6);
1015 }
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001016
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001017 // Check for "++nested" flag.
1018 if ((STRNCMP(cmd, "++nested", 8) == 0 && VIM_ISWHITE(cmd[8])))
1019 {
1020 if (nested)
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001021 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001022 semsg(_(e_duplicate_argument_str), "++nested");
1023 return;
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001024 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001025 nested = TRUE;
1026 cmd = skipwhite(cmd + 8);
1027 }
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001028
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001029 // Check for the old "nested" flag in legacy script.
1030 if (STRNCMP(cmd, "nested", 6) == 0 && VIM_ISWHITE(cmd[6]))
1031 {
1032 if (in_vim9script())
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001033 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001034 // If there ever is a :nested command this error should
1035 // be removed and "nested" accepted as the start of the
1036 // command.
1037 emsg(_(e_invalid_command_nested_did_you_mean_plusplus_nested));
1038 return;
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001039 }
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001040 if (nested)
1041 {
1042 semsg(_(e_duplicate_argument_str), "nested");
1043 return;
1044 }
1045 nested = TRUE;
1046 cmd = skipwhite(cmd + 6);
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001047 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001048 }
1049
1050 /*
1051 * Find the start of the commands.
1052 * Expand <sfile> in it.
1053 */
1054 if (*cmd != NUL)
1055 {
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001056 if (eap != NULL)
1057 // Read a {} block if it follows.
1058 cmd = may_get_cmd_block(eap, cmd, &tofree, &flags);
1059
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001060 cmd = expand_sfile(cmd);
1061 if (cmd == NULL) // some error
1062 return;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001063 cmd_need_free = TRUE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001064 }
1065 }
1066
1067 /*
1068 * Print header when showing autocommands.
1069 */
1070 if (!forceit && *cmd == NUL)
1071 // Highlight title
1072 msg_puts_title(_("\n--- Autocommands ---"));
1073
1074 /*
1075 * Loop over the events.
1076 */
1077 last_event = (event_T)-1; // for listing the event name
1078 last_group = AUGROUP_ERROR; // for listing the group name
1079 if (*arg == '*' || *arg == NUL || *arg == '|')
1080 {
Bram Moolenaarb6db1462021-12-24 19:24:47 +00001081 if (*cmd != NUL)
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001082 emsg(_(e_cannot_define_autocommands_for_all_events));
1083 else
=?UTF-8?q?Dundar=20G=C3=B6c?=dfa5e462021-10-02 11:26:51 +01001084 for (event = (event_T)0; (int)event < NUM_EVENTS;
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001085 event = (event_T)((int)event + 1))
1086 if (do_autocmd_event(event, pat,
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001087 once, nested, cmd, forceit, group, flags) == FAIL)
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001088 break;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001089 }
1090 else
1091 {
1092 while (*arg && *arg != '|' && !VIM_ISWHITE(*arg))
1093 if (do_autocmd_event(event_name2nr(arg, &arg), pat,
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001094 once, nested, cmd, forceit, group, flags) == FAIL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001095 break;
1096 }
1097
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001098 if (cmd_need_free)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001099 vim_free(cmd);
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001100 vim_free(tofree);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001101 vim_free(envpat);
1102}
1103
1104/*
1105 * Find the group ID in a ":autocmd" or ":doautocmd" argument.
1106 * The "argp" argument is advanced to the following argument.
1107 *
1108 * Returns the group ID, AUGROUP_ERROR for error (out of memory).
1109 */
1110 static int
1111au_get_grouparg(char_u **argp)
1112{
1113 char_u *group_name;
1114 char_u *p;
1115 char_u *arg = *argp;
1116 int group = AUGROUP_ALL;
1117
1118 for (p = arg; *p && !VIM_ISWHITE(*p) && *p != '|'; ++p)
1119 ;
Yegappan Lakshmanan1cfb14a2023-01-09 19:04:23 +00001120 if (p <= arg)
1121 return AUGROUP_ALL;
1122
1123 group_name = vim_strnsave(arg, p - arg);
1124 if (group_name == NULL) // out of memory
1125 return AUGROUP_ERROR;
1126 group = au_find_group(group_name);
1127 if (group == AUGROUP_ERROR)
1128 group = AUGROUP_ALL; // no match, use all groups
1129 else
1130 *argp = skipwhite(p); // match, skip over group name
1131 vim_free(group_name);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001132 return group;
1133}
1134
1135/*
1136 * do_autocmd() for one event.
1137 * If *pat == NUL do for all patterns.
1138 * If *cmd == NUL show entries.
1139 * If forceit == TRUE delete entries.
1140 * If group is not AUGROUP_ALL, only use this group.
1141 */
1142 static int
1143do_autocmd_event(
1144 event_T event,
1145 char_u *pat,
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001146 int once,
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001147 int nested,
1148 char_u *cmd,
1149 int forceit,
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001150 int group,
1151 int flags)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001152{
1153 AutoPat *ap;
1154 AutoPat **prev_ap;
1155 AutoCmd *ac;
1156 AutoCmd **prev_ac;
1157 int brace_level;
1158 char_u *endpat;
1159 int findgroup;
1160 int allgroups;
1161 int patlen;
1162 int is_buflocal;
1163 int buflocal_nr;
Bram Moolenaarc667da52019-11-30 20:52:27 +01001164 char_u buflocal_pat[25]; // for "<buffer=X>"
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001165
1166 if (group == AUGROUP_ALL)
1167 findgroup = current_augroup;
1168 else
1169 findgroup = group;
1170 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL);
1171
1172 /*
1173 * Show or delete all patterns for an event.
1174 */
1175 if (*pat == NUL)
1176 {
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001177 FOR_ALL_AUTOCMD_PATTERNS(event, ap)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001178 {
1179 if (forceit) // delete the AutoPat, if it's in the current group
1180 {
1181 if (ap->group == findgroup)
1182 au_remove_pat(ap);
1183 }
1184 else if (group == AUGROUP_ALL || ap->group == group)
1185 show_autocmd(ap, event);
1186 }
1187 }
1188
1189 /*
1190 * Loop through all the specified patterns.
1191 */
1192 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat))
1193 {
1194 /*
1195 * Find end of the pattern.
1196 * Watch out for a comma in braces, like "*.\{obj,o\}".
1197 */
1198 brace_level = 0;
1199 for (endpat = pat; *endpat && (*endpat != ',' || brace_level
1200 || (endpat > pat && endpat[-1] == '\\')); ++endpat)
1201 {
1202 if (*endpat == '{')
1203 brace_level++;
1204 else if (*endpat == '}')
1205 brace_level--;
1206 }
1207 if (pat == endpat) // ignore single comma
1208 continue;
1209 patlen = (int)(endpat - pat);
1210
1211 /*
1212 * detect special <buflocal[=X]> buffer-local patterns
1213 */
1214 is_buflocal = FALSE;
1215 buflocal_nr = 0;
1216
1217 if (patlen >= 8 && STRNCMP(pat, "<buffer", 7) == 0
1218 && pat[patlen - 1] == '>')
1219 {
1220 // "<buffer...>": Error will be printed only for addition.
1221 // printing and removing will proceed silently.
1222 is_buflocal = TRUE;
1223 if (patlen == 8)
1224 // "<buffer>"
1225 buflocal_nr = curbuf->b_fnum;
1226 else if (patlen > 9 && pat[7] == '=')
1227 {
1228 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13) == 0)
1229 // "<buffer=abuf>"
1230 buflocal_nr = autocmd_bufnr;
1231 else if (skipdigits(pat + 8) == pat + patlen - 1)
1232 // "<buffer=123>"
1233 buflocal_nr = atoi((char *)pat + 8);
1234 }
1235 }
1236
1237 if (is_buflocal)
1238 {
1239 // normalize pat into standard "<buffer>#N" form
1240 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr);
1241 pat = buflocal_pat; // can modify pat and patlen
1242 patlen = (int)STRLEN(buflocal_pat); // but not endpat
1243 }
1244
1245 /*
1246 * Find AutoPat entries with this pattern. When adding a command it
1247 * always goes at or after the last one, so start at the end.
1248 */
1249 if (!forceit && *cmd != NUL && last_autopat[(int)event] != NULL)
1250 prev_ap = &last_autopat[(int)event];
1251 else
1252 prev_ap = &first_autopat[(int)event];
1253 while ((ap = *prev_ap) != NULL)
1254 {
1255 if (ap->pat != NULL)
1256 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001257 /*
1258 * Accept a pattern when:
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001259 * - a group was specified and it's that group, or a group was
1260 * not specified and it's the current group, or a group was
1261 * not specified and we are listing
1262 * - the length of the pattern matches
1263 * - the pattern matches.
1264 * For <buffer[=X]>, this condition works because we normalize
1265 * all buffer-local patterns.
1266 */
1267 if ((allgroups || ap->group == findgroup)
1268 && ap->patlen == patlen
1269 && STRNCMP(pat, ap->pat, patlen) == 0)
1270 {
1271 /*
1272 * Remove existing autocommands.
1273 * If adding any new autocmd's for this AutoPat, don't
1274 * delete the pattern from the autopat list, append to
1275 * this list.
1276 */
1277 if (forceit)
1278 {
1279 if (*cmd != NUL && ap->next == NULL)
1280 {
1281 au_remove_cmds(ap);
1282 break;
1283 }
1284 au_remove_pat(ap);
1285 }
1286
1287 /*
1288 * Show autocmd's for this autopat, or buflocals <buffer=X>
1289 */
1290 else if (*cmd == NUL)
1291 show_autocmd(ap, event);
1292
1293 /*
1294 * Add autocmd to this autopat, if it's the last one.
1295 */
1296 else if (ap->next == NULL)
1297 break;
1298 }
1299 }
1300 prev_ap = &ap->next;
1301 }
1302
1303 /*
1304 * Add a new command.
1305 */
1306 if (*cmd != NUL)
1307 {
1308 /*
1309 * If the pattern we want to add a command to does appear at the
1310 * end of the list (or not is not in the list at all), add the
1311 * pattern at the end of the list.
1312 */
1313 if (ap == NULL)
1314 {
Bram Moolenaarc667da52019-11-30 20:52:27 +01001315 // refuse to add buffer-local ap if buffer number is invalid
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001316 if (is_buflocal && (buflocal_nr == 0
1317 || buflist_findnr(buflocal_nr) == NULL))
1318 {
Bram Moolenaarf1474d82021-12-31 19:59:55 +00001319 semsg(_(e_buffer_nr_invalid_buffer_number), buflocal_nr);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001320 return FAIL;
1321 }
1322
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001323 ap = ALLOC_ONE(AutoPat);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001324 if (ap == NULL)
1325 return FAIL;
1326 ap->pat = vim_strnsave(pat, patlen);
1327 ap->patlen = patlen;
1328 if (ap->pat == NULL)
1329 {
1330 vim_free(ap);
1331 return FAIL;
1332 }
1333
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001334#ifdef FEAT_EVAL
1335 // need to initialize last_mode for the first ModeChanged
1336 // autocmd
1337 if (event == EVENT_MODECHANGED && !has_modechanged())
LemonBoy2bf52dd2022-04-09 18:17:34 +01001338 get_mode(last_mode);
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001339#endif
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001340 // Initialize the fields checked by the WinScrolled and
1341 // WinResized trigger to prevent them from firing right after
1342 // the first autocmd is defined.
1343 if ((event == EVENT_WINSCROLLED || event == EVENT_WINRESIZED)
1344 && !(has_winscrolled() || has_winresized()))
LemonBoy09371822022-04-08 15:18:45 +01001345 {
Bram Moolenaar29967732022-11-20 12:11:45 +00001346 tabpage_T *save_curtab = curtab;
1347 tabpage_T *tp;
1348 FOR_ALL_TABPAGES(tp)
1349 {
1350 unuse_tabpage(curtab);
1351 use_tabpage(tp);
1352 snapshot_windows_scroll_size();
1353 }
1354 unuse_tabpage(curtab);
1355 use_tabpage(save_curtab);
LemonBoy09371822022-04-08 15:18:45 +01001356 }
=?UTF-8?q?Magnus=20Gro=C3=9F?=25def2c2021-10-22 18:56:39 +01001357
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001358 if (is_buflocal)
1359 {
1360 ap->buflocal_nr = buflocal_nr;
1361 ap->reg_prog = NULL;
1362 }
1363 else
1364 {
1365 char_u *reg_pat;
1366
1367 ap->buflocal_nr = 0;
1368 reg_pat = file_pat_to_reg_pat(pat, endpat,
1369 &ap->allow_dirs, TRUE);
1370 if (reg_pat != NULL)
1371 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC);
1372 vim_free(reg_pat);
1373 if (reg_pat == NULL || ap->reg_prog == NULL)
1374 {
1375 vim_free(ap->pat);
1376 vim_free(ap);
1377 return FAIL;
1378 }
1379 }
1380 ap->cmds = NULL;
1381 *prev_ap = ap;
1382 last_autopat[(int)event] = ap;
1383 ap->next = NULL;
1384 if (group == AUGROUP_ALL)
1385 ap->group = current_augroup;
1386 else
1387 ap->group = group;
1388 }
1389
1390 /*
1391 * Add the autocmd at the end of the AutoCmd list.
1392 */
1393 prev_ac = &(ap->cmds);
1394 while ((ac = *prev_ac) != NULL)
1395 prev_ac = &ac->next;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001396 ac = ALLOC_ONE(AutoCmd);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001397 if (ac == NULL)
1398 return FAIL;
1399 ac->cmd = vim_strsave(cmd);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001400 ac->script_ctx = current_sctx;
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02001401 if (flags & UC_VIM9)
1402 ac->script_ctx.sc_version = SCRIPT_VERSION_VIM9;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01001403#ifdef FEAT_EVAL
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001404 ac->script_ctx.sc_lnum += SOURCING_LNUM;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001405#endif
1406 if (ac->cmd == NULL)
1407 {
1408 vim_free(ac);
1409 return FAIL;
1410 }
1411 ac->next = NULL;
1412 *prev_ac = ac;
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001413 ac->once = once;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001414 ac->nested = nested;
1415 }
1416 }
1417
1418 au_cleanup(); // may really delete removed patterns/commands now
1419 return OK;
1420}
1421
1422/*
1423 * Implementation of ":doautocmd [group] event [fname]".
1424 * Return OK for success, FAIL for failure;
1425 */
1426 int
1427do_doautocmd(
Bram Moolenaar1b154ea2021-08-07 13:59:43 +02001428 char_u *arg_start,
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001429 int do_msg, // give message for no matching autocmds?
1430 int *did_something)
1431{
Bram Moolenaar1b154ea2021-08-07 13:59:43 +02001432 char_u *arg = arg_start;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001433 char_u *fname;
1434 int nothing_done = TRUE;
1435 int group;
1436
1437 if (did_something != NULL)
1438 *did_something = FALSE;
1439
1440 /*
1441 * Check for a legal group name. If not, use AUGROUP_ALL.
1442 */
1443 group = au_get_grouparg(&arg);
1444 if (arg == NULL) // out of memory
1445 return FAIL;
1446
1447 if (*arg == '*')
1448 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00001449 emsg(_(e_cant_execute_autocommands_for_all_events));
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001450 return FAIL;
1451 }
1452
1453 /*
1454 * Scan over the events.
1455 * If we find an illegal name, return here, don't do anything.
1456 */
1457 fname = find_end_event(arg, group != AUGROUP_ALL);
1458 if (fname == NULL)
1459 return FAIL;
1460
1461 fname = skipwhite(fname);
1462
1463 /*
1464 * Loop over the events.
1465 */
1466 while (*arg && !ends_excmd(*arg) && !VIM_ISWHITE(*arg))
1467 if (apply_autocmds_group(event_name2nr(arg, &arg),
1468 fname, NULL, TRUE, group, curbuf, NULL))
1469 nothing_done = FALSE;
1470
Bram Moolenaar1b154ea2021-08-07 13:59:43 +02001471 if (nothing_done && do_msg
1472#ifdef FEAT_EVAL
1473 && !aborting()
1474#endif
1475 )
1476 smsg(_("No matching autocommands: %s"), arg_start);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001477 if (did_something != NULL)
1478 *did_something = !nothing_done;
1479
1480#ifdef FEAT_EVAL
1481 return aborting() ? FAIL : OK;
1482#else
1483 return OK;
1484#endif
1485}
1486
1487/*
1488 * ":doautoall": execute autocommands for each loaded buffer.
1489 */
1490 void
1491ex_doautoall(exarg_T *eap)
1492{
Bram Moolenaar41cd8032021-03-13 15:47:56 +01001493 int retval = OK;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001494 aco_save_T aco;
1495 buf_T *buf;
1496 bufref_T bufref;
1497 char_u *arg = eap->arg;
1498 int call_do_modelines = check_nomodeline(&arg);
1499 int did_aucmd;
1500
1501 /*
1502 * This is a bit tricky: For some commands curwin->w_buffer needs to be
1503 * equal to curbuf, but for some buffers there may not be a window.
1504 * So we change the buffer for the current window for a moment. This
1505 * gives problems when the autocommands make changes to the list of
1506 * buffers or windows...
1507 */
1508 FOR_ALL_BUFFERS(buf)
1509 {
Bram Moolenaar41cd8032021-03-13 15:47:56 +01001510 // Only do loaded buffers and skip the current buffer, it's done last.
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001511 if (buf->b_ml.ml_mfp == NULL || buf == curbuf)
1512 continue;
1513
Bram Moolenaare76062c2022-11-28 18:51:43 +00001514 // Find a window for this buffer and save some values.
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001515 aucmd_prepbuf(&aco, buf);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001516 if (curbuf != buf)
1517 {
1518 // Failed to find a window for this buffer. Better not execute
1519 // autocommands then.
1520 retval = FAIL;
1521 break;
1522 }
1523
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001524 set_bufref(&bufref, buf);
1525
1526 // execute the autocommands for this buffer
1527 retval = do_doautocmd(arg, FALSE, &did_aucmd);
1528
1529 if (call_do_modelines && did_aucmd)
1530 // Execute the modeline settings, but don't set window-local
1531 // options if we are using the current window for another
1532 // buffer.
Bram Moolenaare76062c2022-11-28 18:51:43 +00001533 do_modelines(is_aucmd_win(curwin) ? OPT_NOWIN : 0);
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001534
1535 // restore the current window
1536 aucmd_restbuf(&aco);
1537
1538 // stop if there is some error or buffer was deleted
1539 if (retval == FAIL || !bufref_valid(&bufref))
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001540 {
Yegappan Lakshmanane9dcf132022-09-24 11:30:41 +01001541 retval = FAIL;
1542 break;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001543 }
1544 }
1545
Bram Moolenaar41cd8032021-03-13 15:47:56 +01001546 // Execute autocommands for the current buffer last.
1547 if (retval == OK)
1548 {
1549 do_doautocmd(arg, FALSE, &did_aucmd);
1550 if (call_do_modelines && did_aucmd)
1551 do_modelines(0);
1552 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001553}
1554
1555/*
1556 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise
1557 * return TRUE and advance *argp to after it.
1558 * Thus return TRUE when do_modelines() should be called.
1559 */
1560 int
1561check_nomodeline(char_u **argp)
1562{
1563 if (STRNCMP(*argp, "<nomodeline>", 12) == 0)
1564 {
1565 *argp = skipwhite(*argp + 12);
1566 return FALSE;
1567 }
1568 return TRUE;
1569}
1570
1571/*
1572 * Prepare for executing autocommands for (hidden) buffer "buf".
1573 * Search for a visible window containing the current buffer. If there isn't
Bram Moolenaare76062c2022-11-28 18:51:43 +00001574 * one then use an entry in "aucmd_win[]".
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001575 * Set "curbuf" and "curwin" to match "buf".
Bram Moolenaare76062c2022-11-28 18:51:43 +00001576 * When this fails "curbuf" is not equal "buf".
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001577 */
1578 void
1579aucmd_prepbuf(
1580 aco_save_T *aco, // structure to save values in
1581 buf_T *buf) // new curbuf
1582{
1583 win_T *win;
1584 int save_ea;
1585#ifdef FEAT_AUTOCHDIR
1586 int save_acd;
1587#endif
1588
1589 // Find a window that is for the new buffer
1590 if (buf == curbuf) // be quick when buf is curbuf
1591 win = curwin;
1592 else
1593 FOR_ALL_WINDOWS(win)
1594 if (win->w_buffer == buf)
1595 break;
1596
Bram Moolenaare76062c2022-11-28 18:51:43 +00001597 // Allocate a window when needed.
1598 win_T *auc_win = NULL;
1599 int auc_idx = AUCMD_WIN_COUNT;
1600 if (win == NULL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001601 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001602 for (auc_idx = 0; auc_idx < AUCMD_WIN_COUNT; ++auc_idx)
1603 if (!aucmd_win[auc_idx].auc_win_used)
1604 {
Bram Moolenaar84497cd2022-11-28 20:34:52 +00001605 if (aucmd_win[auc_idx].auc_win == NULL)
1606 aucmd_win[auc_idx].auc_win = win_alloc_popup_win();
1607 auc_win = aucmd_win[auc_idx].auc_win;
Bram Moolenaare76062c2022-11-28 18:51:43 +00001608 if (auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00001609 aucmd_win[auc_idx].auc_win_used = TRUE;
Bram Moolenaare76062c2022-11-28 18:51:43 +00001610 break;
1611 }
1612
1613 // If this fails (out of memory or using all AUCMD_WIN_COUNT
1614 // entries) then we can't reliable execute the autocmd, return with
1615 // "curbuf" unequal "buf".
1616 if (auc_win == NULL)
1617 return;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001618 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001619
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001620 aco->save_curwin_id = curwin->w_id;
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001621 aco->save_prevwin_id = prevwin == NULL ? 0 : prevwin->w_id;
Bram Moolenaar05a627c2023-04-09 22:01:31 +01001622 aco->save_State = State;
zeertzjqf2678472024-01-17 21:22:59 +01001623#ifdef FEAT_JOB_CHANNEL
1624 if (bt_prompt(curbuf))
1625 aco->save_prompt_insert = curbuf->b_prompt_insert;
1626#endif
Bram Moolenaar05a627c2023-04-09 22:01:31 +01001627
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001628 if (win != NULL)
1629 {
1630 // There is a window for "buf" in the current tab page, make it the
1631 // curwin. This is preferred, it has the least side effects (esp. if
1632 // "buf" is curbuf).
Bram Moolenaare76062c2022-11-28 18:51:43 +00001633 aco->use_aucmd_win_idx = -1;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001634 curwin = win;
1635 }
1636 else
1637 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001638 // There is no window for "buf", use "auc_win". To minimize the side
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001639 // effects, insert it in the current tab page.
1640 // Anything related to a window (e.g., setting folds) may have
1641 // unexpected results.
Bram Moolenaare76062c2022-11-28 18:51:43 +00001642 aco->use_aucmd_win_idx = auc_idx;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001643
Bram Moolenaare76062c2022-11-28 18:51:43 +00001644 win_init_popup_win(auc_win, buf);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001645
zeertzjq9d956ee2024-04-07 18:16:10 +02001646 // Make sure tp_localdir and globaldir are NULL to avoid a
1647 // chdir() in win_enter_ext().
1648 // win_init_popup_win() has already set w_localdir to NULL.
1649 aco->tp_localdir = curtab->tp_localdir;
1650 curtab->tp_localdir = NULL;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001651 aco->globaldir = globaldir;
1652 globaldir = NULL;
1653
Bram Moolenaare76062c2022-11-28 18:51:43 +00001654 // Split the current window, put the auc_win in the upper half.
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001655 // We don't want the BufEnter or WinEnter autocommands.
1656 block_autocmds();
1657 make_snapshot(SNAP_AUCMD_IDX);
1658 save_ea = p_ea;
1659 p_ea = FALSE;
1660
1661#ifdef FEAT_AUTOCHDIR
1662 // Prevent chdir() call in win_enter_ext(), through do_autochdir().
1663 save_acd = p_acd;
1664 p_acd = FALSE;
1665#endif
1666
Sean Dewar704966c2024-02-20 22:00:33 +01001667 (void)win_split_ins(0, WSP_TOP | WSP_FORCE_ROOM, auc_win, 0, NULL);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001668 (void)win_comp_pos(); // recompute window positions
1669 p_ea = save_ea;
1670#ifdef FEAT_AUTOCHDIR
1671 p_acd = save_acd;
1672#endif
1673 unblock_autocmds();
Bram Moolenaare76062c2022-11-28 18:51:43 +00001674 curwin = auc_win;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001675 }
1676 curbuf = buf;
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001677 aco->new_curwin_id = curwin->w_id;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001678 set_bufref(&aco->new_curbuf, curbuf);
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00001679
1680 // disable the Visual area, the position may be invalid in another buffer
1681 aco->save_VIsual_active = VIsual_active;
1682 VIsual_active = FALSE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001683}
1684
1685/*
1686 * Cleanup after executing autocommands for a (hidden) buffer.
1687 * Restore the window as it was (if possible).
1688 */
1689 void
1690aucmd_restbuf(
1691 aco_save_T *aco) // structure holding saved values
1692{
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001693 int dummy;
1694 win_T *save_curwin;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001695
Bram Moolenaare76062c2022-11-28 18:51:43 +00001696 if (aco->use_aucmd_win_idx >= 0)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001697 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001698 win_T *awp = aucmd_win[aco->use_aucmd_win_idx].auc_win;
1699
Bram Moolenaare76062c2022-11-28 18:51:43 +00001700 // Find "awp", it can't be closed, but it may be in another tab
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001701 // page. Do not trigger autocommands here.
1702 block_autocmds();
Bram Moolenaare76062c2022-11-28 18:51:43 +00001703 if (curwin != awp)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001704 {
1705 tabpage_T *tp;
1706 win_T *wp;
1707
1708 FOR_ALL_TAB_WINDOWS(tp, wp)
1709 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001710 if (wp == awp)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001711 {
1712 if (tp != curtab)
1713 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001714 win_goto(awp);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001715 goto win_found;
1716 }
1717 }
1718 }
1719win_found:
zeertzjq46bdae02023-09-24 23:16:08 +02001720 --curbuf->b_nwindows;
orbitalcde8de02023-04-02 22:05:13 +01001721#ifdef FEAT_JOB_CHANNEL
zeertzjqa40c0bc2023-05-27 14:10:08 +01001722 int save_stop_insert_mode = stop_insert_mode;
orbitalcde8de02023-04-02 22:05:13 +01001723 // May need to stop Insert mode if we were in a prompt buffer.
1724 leaving_window(curwin);
Bram Moolenaar05a627c2023-04-09 22:01:31 +01001725 // Do not stop Insert mode when already in Insert mode before.
1726 if (aco->save_State & MODE_INSERT)
zeertzjqa40c0bc2023-05-27 14:10:08 +01001727 stop_insert_mode = save_stop_insert_mode;
orbitalcde8de02023-04-02 22:05:13 +01001728#endif
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001729 // Remove the window and frame from the tree of frames.
Sean Dewar704966c2024-02-20 22:00:33 +01001730 (void)winframe_remove(curwin, &dummy, NULL, NULL);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001731 win_remove(curwin, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00001732
1733 // The window is marked as not used, but it is not freed, it can be
1734 // used again.
Bram Moolenaare76062c2022-11-28 18:51:43 +00001735 aucmd_win[aco->use_aucmd_win_idx].auc_win_used = FALSE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001736 last_status(FALSE); // may need to remove last status line
1737
1738 if (!valid_tabpage_win(curtab))
1739 // no valid window in current tabpage
1740 close_tabpage(curtab);
1741
1742 restore_snapshot(SNAP_AUCMD_IDX, FALSE);
1743 (void)win_comp_pos(); // recompute window positions
1744 unblock_autocmds();
1745
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001746 save_curwin = win_find_by_id(aco->save_curwin_id);
1747 if (save_curwin != NULL)
1748 curwin = save_curwin;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001749 else
1750 // Hmm, original window disappeared. Just use the first one.
1751 curwin = firstwin;
Bram Moolenaarbdf931c2020-10-01 22:37:40 +02001752 curbuf = curwin->w_buffer;
1753#ifdef FEAT_JOB_CHANNEL
1754 // May need to restore insert mode for a prompt buffer.
1755 entering_window(curwin);
zeertzjqf2678472024-01-17 21:22:59 +01001756 if (bt_prompt(curbuf))
1757 curbuf->b_prompt_insert = aco->save_prompt_insert;
Bram Moolenaarbdf931c2020-10-01 22:37:40 +02001758#endif
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001759 prevwin = win_find_by_id(aco->save_prevwin_id);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001760#ifdef FEAT_EVAL
Bram Moolenaare76062c2022-11-28 18:51:43 +00001761 vars_clear(&awp->w_vars->dv_hashtab); // free all w: variables
1762 hash_init(&awp->w_vars->dv_hashtab); // re-use the hashtab
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001763#endif
zeertzjq9d956ee2024-04-07 18:16:10 +02001764 // If :lcd has been used in the autocommand window, correct current
1765 // directory before restoring tp_localdir and globaldir.
1766 if (awp->w_localdir != NULL)
1767 win_fix_current_dir();
1768 vim_free(curtab->tp_localdir);
1769 curtab->tp_localdir = aco->tp_localdir;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001770 vim_free(globaldir);
1771 globaldir = aco->globaldir;
1772
1773 // the buffer contents may have changed
zeertzjq49f05242023-02-04 10:58:34 +00001774 VIsual_active = aco->save_VIsual_active;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001775 check_cursor();
1776 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1777 {
1778 curwin->w_topline = curbuf->b_ml.ml_line_count;
1779#ifdef FEAT_DIFF
1780 curwin->w_topfill = 0;
1781#endif
1782 }
1783#if defined(FEAT_GUI)
Bram Moolenaard2ff7052021-12-17 16:00:04 +00001784 if (gui.in_use)
1785 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001786 // Hide the scrollbars from the "awp" and update.
1787 gui_mch_enable_scrollbar(&awp->w_scrollbars[SBAR_LEFT], FALSE);
1788 gui_mch_enable_scrollbar(&awp->w_scrollbars[SBAR_RIGHT], FALSE);
Bram Moolenaard2ff7052021-12-17 16:00:04 +00001789 gui_may_update_scrollbars();
1790 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001791#endif
1792 }
1793 else
1794 {
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001795 // Restore curwin. Use the window ID, a window may have been closed
1796 // and the memory re-used for another one.
1797 save_curwin = win_find_by_id(aco->save_curwin_id);
1798 if (save_curwin != NULL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001799 {
1800 // Restore the buffer which was previously edited by curwin, if
1801 // it was changed, we are still the same window and the buffer is
1802 // valid.
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001803 if (curwin->w_id == aco->new_curwin_id
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001804 && curbuf != aco->new_curbuf.br_buf
1805 && bufref_valid(&aco->new_curbuf)
1806 && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL)
1807 {
1808# if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
1809 if (curwin->w_s == &curbuf->b_s)
1810 curwin->w_s = &aco->new_curbuf.br_buf->b_s;
1811# endif
1812 --curbuf->b_nwindows;
1813 curbuf = aco->new_curbuf.br_buf;
1814 curwin->w_buffer = curbuf;
1815 ++curbuf->b_nwindows;
1816 }
1817
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001818 curwin = save_curwin;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001819 curbuf = curwin->w_buffer;
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001820 prevwin = win_find_by_id(aco->save_prevwin_id);
zeertzjq49f05242023-02-04 10:58:34 +00001821
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001822 // In case the autocommand moves the cursor to a position that
1823 // does not exist in curbuf.
zeertzjq49f05242023-02-04 10:58:34 +00001824 VIsual_active = aco->save_VIsual_active;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001825 check_cursor();
1826 }
1827 }
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00001828
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00001829 VIsual_active = aco->save_VIsual_active;
zeertzjq49f05242023-02-04 10:58:34 +00001830 check_cursor(); // just in case lines got deleted
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00001831 if (VIsual_active)
1832 check_pos(curbuf, &VIsual);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001833}
1834
1835static int autocmd_nested = FALSE;
1836
1837/*
1838 * Execute autocommands for "event" and file name "fname".
1839 * Return TRUE if some commands were executed.
1840 */
1841 int
1842apply_autocmds(
1843 event_T event,
1844 char_u *fname, // NULL or empty means use actual file name
1845 char_u *fname_io, // fname to use for <afile> on cmdline
1846 int force, // when TRUE, ignore autocmd_busy
1847 buf_T *buf) // buffer for <abuf>
1848{
1849 return apply_autocmds_group(event, fname, fname_io, force,
1850 AUGROUP_ALL, buf, NULL);
1851}
1852
1853/*
1854 * Like apply_autocmds(), but with extra "eap" argument. This takes care of
1855 * setting v:filearg.
1856 */
1857 int
1858apply_autocmds_exarg(
1859 event_T event,
1860 char_u *fname,
1861 char_u *fname_io,
1862 int force,
1863 buf_T *buf,
1864 exarg_T *eap)
1865{
1866 return apply_autocmds_group(event, fname, fname_io, force,
1867 AUGROUP_ALL, buf, eap);
1868}
1869
1870/*
1871 * Like apply_autocmds(), but handles the caller's retval. If the script
1872 * processing is being aborted or if retval is FAIL when inside a try
1873 * conditional, no autocommands are executed. If otherwise the autocommands
1874 * cause the script to be aborted, retval is set to FAIL.
1875 */
1876 int
1877apply_autocmds_retval(
1878 event_T event,
1879 char_u *fname, // NULL or empty means use actual file name
1880 char_u *fname_io, // fname to use for <afile> on cmdline
1881 int force, // when TRUE, ignore autocmd_busy
1882 buf_T *buf, // buffer for <abuf>
1883 int *retval) // pointer to caller's retval
1884{
1885 int did_cmd;
1886
1887#ifdef FEAT_EVAL
1888 if (should_abort(*retval))
1889 return FALSE;
1890#endif
1891
1892 did_cmd = apply_autocmds_group(event, fname, fname_io, force,
1893 AUGROUP_ALL, buf, NULL);
1894 if (did_cmd
1895#ifdef FEAT_EVAL
1896 && aborting()
1897#endif
1898 )
1899 *retval = FAIL;
1900 return did_cmd;
1901}
1902
1903/*
1904 * Return TRUE when there is a CursorHold autocommand defined.
1905 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001906 static int
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001907has_cursorhold(void)
1908{
Bram Moolenaar24959102022-05-07 20:01:16 +01001909 return (first_autopat[(int)(get_real_state() == MODE_NORMAL_BUSY
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001910 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL);
1911}
1912
1913/*
1914 * Return TRUE if the CursorHold event can be triggered.
1915 */
1916 int
1917trigger_cursorhold(void)
1918{
1919 int state;
1920
1921 if (!did_cursorhold
1922 && has_cursorhold()
1923 && reg_recording == 0
1924 && typebuf.tb_len == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001925 && !ins_compl_active())
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001926 {
1927 state = get_real_state();
Bram Moolenaar24959102022-05-07 20:01:16 +01001928 if (state == MODE_NORMAL_BUSY || (state & MODE_INSERT) != 0)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001929 return TRUE;
1930 }
1931 return FALSE;
1932}
1933
1934/*
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001935 * Return TRUE when there is a WinResized autocommand defined.
1936 */
1937 int
1938has_winresized(void)
1939{
1940 return (first_autopat[(int)EVENT_WINRESIZED] != NULL);
1941}
1942
1943/*
LemonBoy09371822022-04-08 15:18:45 +01001944 * Return TRUE when there is a WinScrolled autocommand defined.
1945 */
1946 int
1947has_winscrolled(void)
1948{
1949 return (first_autopat[(int)EVENT_WINSCROLLED] != NULL);
1950}
1951
1952/*
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001953 * Return TRUE when there is a CursorMoved autocommand defined.
1954 */
1955 int
1956has_cursormoved(void)
1957{
1958 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL);
1959}
1960
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001961/*
1962 * Return TRUE when there is a CursorMovedI autocommand defined.
1963 */
1964 int
1965has_cursormovedI(void)
1966{
1967 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL);
1968}
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001969
1970/*
1971 * Return TRUE when there is a TextChanged autocommand defined.
1972 */
1973 int
1974has_textchanged(void)
1975{
1976 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL);
1977}
1978
1979/*
1980 * Return TRUE when there is a TextChangedI autocommand defined.
1981 */
1982 int
1983has_textchangedI(void)
1984{
1985 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL);
1986}
1987
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001988/*
1989 * Return TRUE when there is a TextChangedP autocommand defined.
1990 */
1991 int
1992has_textchangedP(void)
1993{
1994 return (first_autopat[(int)EVENT_TEXTCHANGEDP] != NULL);
1995}
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001996
1997/*
1998 * Return TRUE when there is an InsertCharPre autocommand defined.
1999 */
2000 int
2001has_insertcharpre(void)
2002{
2003 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL);
2004}
2005
2006/*
Shougo Matsushita83678842024-07-11 22:05:12 +02002007 * Return TRUE when there is an KeyInputPre autocommand defined.
2008 */
2009 int
2010has_keyinputpre(void)
2011{
2012 return (first_autopat[(int)EVENT_KEYINPUTPRE] != NULL);
2013}
2014
2015/*
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002016 * Return TRUE when there is an CmdUndefined autocommand defined.
2017 */
2018 int
2019has_cmdundefined(void)
2020{
2021 return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL);
2022}
2023
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002024#if defined(FEAT_EVAL) || defined(PROTO)
2025/*
2026 * Return TRUE when there is a TextYankPost autocommand defined.
2027 */
2028 int
2029has_textyankpost(void)
2030{
2031 return (first_autopat[(int)EVENT_TEXTYANKPOST] != NULL);
2032}
2033#endif
2034
Bram Moolenaard7f246c2019-04-08 18:15:41 +02002035#if defined(FEAT_EVAL) || defined(PROTO)
2036/*
2037 * Return TRUE when there is a CompleteChanged autocommand defined.
2038 */
2039 int
2040has_completechanged(void)
2041{
2042 return (first_autopat[(int)EVENT_COMPLETECHANGED] != NULL);
2043}
2044#endif
2045
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002046#if defined(FEAT_EVAL) || defined(PROTO)
2047/*
2048 * Return TRUE when there is a ModeChanged autocommand defined.
2049 */
2050 int
2051has_modechanged(void)
2052{
2053 return (first_autopat[(int)EVENT_MODECHANGED] != NULL);
2054}
2055#endif
2056
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002057/*
2058 * Execute autocommands for "event" and file name "fname".
2059 * Return TRUE if some commands were executed.
2060 */
2061 static int
2062apply_autocmds_group(
2063 event_T event,
2064 char_u *fname, // NULL or empty means use actual file name
2065 char_u *fname_io, // fname to use for <afile> on cmdline, NULL means
2066 // use fname
2067 int force, // when TRUE, ignore autocmd_busy
2068 int group, // group ID, or AUGROUP_ALL
2069 buf_T *buf, // buffer for <abuf>
2070 exarg_T *eap UNUSED) // command arguments
2071{
2072 char_u *sfname = NULL; // short file name
2073 char_u *tail;
2074 int save_changed;
2075 buf_T *old_curbuf;
2076 int retval = FALSE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002077 char_u *save_autocmd_fname;
2078 int save_autocmd_fname_full;
2079 int save_autocmd_bufnr;
2080 char_u *save_autocmd_match;
2081 int save_autocmd_busy;
2082 int save_autocmd_nested;
2083 static int nesting = 0;
LemonBoyeca7c602022-04-14 15:39:43 +01002084 AutoPatCmd_T patcmd;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002085 AutoPat *ap;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002086 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002087#ifdef FEAT_EVAL
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002088 funccal_entry_T funccal_entry;
2089 char_u *save_cmdarg;
2090 long save_cmdbang;
2091#endif
2092 static int filechangeshell_busy = FALSE;
2093#ifdef FEAT_PROFILE
2094 proftime_T wait_time;
2095#endif
2096 int did_save_redobuff = FALSE;
2097 save_redo_T save_redo;
2098 int save_KeyTyped = KeyTyped;
ichizok7e5fe382023-04-15 13:17:50 +01002099 ESTACK_CHECK_DECLARATION;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002100
2101 /*
2102 * Quickly return if there are no autocommands for this event or
2103 * autocommands are blocked.
2104 */
2105 if (event == NUM_EVENTS || first_autopat[(int)event] == NULL
2106 || autocmd_blocked > 0)
2107 goto BYPASS_AU;
2108
2109 /*
2110 * When autocommands are busy, new autocommands are only executed when
2111 * explicitly enabled with the "nested" flag.
2112 */
2113 if (autocmd_busy && !(force || autocmd_nested))
2114 goto BYPASS_AU;
2115
2116#ifdef FEAT_EVAL
2117 /*
2118 * Quickly return when immediately aborting on error, or when an interrupt
2119 * occurred or an exception was thrown but not caught.
2120 */
2121 if (aborting())
2122 goto BYPASS_AU;
2123#endif
2124
2125 /*
2126 * FileChangedShell never nests, because it can create an endless loop.
2127 */
2128 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL
2129 || event == EVENT_FILECHANGEDSHELLPOST))
2130 goto BYPASS_AU;
2131
2132 /*
2133 * Ignore events in 'eventignore'.
2134 */
Luuk van Baalb7147f82025-02-08 18:52:39 +01002135 if (event_ignored(event, p_ei))
2136 goto BYPASS_AU;
2137
2138 wininfo_T *wip;
2139 int win_ignore = FALSE;
2140 // If event is allowed in 'eventignorewin', check if curwin or all windows
2141 // into "buf" are ignoring the event.
2142 if (buf == curbuf && event_tab[event].key <= 0)
2143 win_ignore = event_ignored(event, curwin->w_p_eiw);
2144 else if (buf != NULL && event_tab[event].key <= 0)
2145 FOR_ALL_BUF_WININFO(buf, wip)
2146 if (wip->wi_win != NULL && wip->wi_win->w_buffer == buf)
2147 win_ignore = event_ignored(event, wip->wi_win->w_p_eiw);
2148 if (win_ignore)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002149 goto BYPASS_AU;
2150
2151 /*
2152 * Allow nesting of autocommands, but restrict the depth, because it's
2153 * possible to create an endless loop.
2154 */
2155 if (nesting == 10)
2156 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002157 emsg(_(e_autocommand_nesting_too_deep));
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002158 goto BYPASS_AU;
2159 }
2160
2161 /*
2162 * Check if these autocommands are disabled. Used when doing ":all" or
2163 * ":ball".
2164 */
2165 if ( (autocmd_no_enter
2166 && (event == EVENT_WINENTER || event == EVENT_BUFENTER))
2167 || (autocmd_no_leave
2168 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE)))
2169 goto BYPASS_AU;
2170
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002171 if (event == EVENT_CMDLINECHANGED)
2172 ++aucmd_cmdline_changed_count;
2173
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002174 /*
2175 * Save the autocmd_* variables and info about the current buffer.
2176 */
2177 save_autocmd_fname = autocmd_fname;
2178 save_autocmd_fname_full = autocmd_fname_full;
2179 save_autocmd_bufnr = autocmd_bufnr;
2180 save_autocmd_match = autocmd_match;
2181 save_autocmd_busy = autocmd_busy;
2182 save_autocmd_nested = autocmd_nested;
2183 save_changed = curbuf->b_changed;
2184 old_curbuf = curbuf;
2185
2186 /*
2187 * Set the file name to be used for <afile>.
2188 * Make a copy to avoid that changing a buffer name or directory makes it
2189 * invalid.
2190 */
2191 if (fname_io == NULL)
2192 {
2193 if (event == EVENT_COLORSCHEME || event == EVENT_COLORSCHEMEPRE
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002194 || event == EVENT_OPTIONSET
Danek Duvalld7d56032024-01-14 20:19:59 +01002195 || event == EVENT_MODECHANGED
2196 || event == EVENT_TERMRESPONSEALL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002197 autocmd_fname = NULL;
2198 else if (fname != NULL && !ends_excmd(*fname))
2199 autocmd_fname = fname;
2200 else if (buf != NULL)
2201 autocmd_fname = buf->b_ffname;
2202 else
2203 autocmd_fname = NULL;
2204 }
2205 else
2206 autocmd_fname = fname_io;
2207 if (autocmd_fname != NULL)
2208 autocmd_fname = vim_strsave(autocmd_fname);
2209 autocmd_fname_full = FALSE; // call FullName_save() later
2210
2211 /*
2212 * Set the buffer number to be used for <abuf>.
2213 */
2214 if (buf == NULL)
2215 autocmd_bufnr = 0;
2216 else
2217 autocmd_bufnr = buf->b_fnum;
2218
2219 /*
2220 * When the file name is NULL or empty, use the file name of buffer "buf".
2221 * Always use the full path of the file name to match with, in case
2222 * "allow_dirs" is set.
2223 */
2224 if (fname == NULL || *fname == NUL)
2225 {
2226 if (buf == NULL)
2227 fname = NULL;
2228 else
2229 {
2230#ifdef FEAT_SYN_HL
2231 if (event == EVENT_SYNTAX)
2232 fname = buf->b_p_syn;
2233 else
2234#endif
2235 if (event == EVENT_FILETYPE)
2236 fname = buf->b_p_ft;
2237 else
2238 {
2239 if (buf->b_sfname != NULL)
2240 sfname = vim_strsave(buf->b_sfname);
2241 fname = buf->b_ffname;
2242 }
2243 }
2244 if (fname == NULL)
2245 fname = (char_u *)"";
2246 fname = vim_strsave(fname); // make a copy, so we can change it
2247 }
2248 else
2249 {
2250 sfname = vim_strsave(fname);
2251 // Don't try expanding FileType, Syntax, FuncUndefined, WindowID,
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002252 // ColorScheme, QuickFixCmd*, DirChanged and similar.
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002253 if (event == EVENT_FILETYPE
2254 || event == EVENT_SYNTAX
2255 || event == EVENT_CMDLINECHANGED
2256 || event == EVENT_CMDLINEENTER
Girish Palya92f68e22025-04-21 11:12:41 +02002257 || event == EVENT_CMDLINELEAVEPRE
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002258 || event == EVENT_CMDLINELEAVE
Shougo Matsushitad0952142024-06-20 22:05:16 +02002259 || event == EVENT_CURSORMOVEDC
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002260 || event == EVENT_CMDWINENTER
2261 || event == EVENT_CMDWINLEAVE
2262 || event == EVENT_CMDUNDEFINED
2263 || event == EVENT_FUNCUNDEFINED
Shougo Matsushita83678842024-07-11 22:05:12 +02002264 || event == EVENT_KEYINPUTPRE
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002265 || event == EVENT_REMOTEREPLY
2266 || event == EVENT_SPELLFILEMISSING
2267 || event == EVENT_QUICKFIXCMDPRE
2268 || event == EVENT_COLORSCHEME
2269 || event == EVENT_COLORSCHEMEPRE
2270 || event == EVENT_OPTIONSET
2271 || event == EVENT_QUICKFIXCMDPOST
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002272 || event == EVENT_DIRCHANGED
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002273 || event == EVENT_DIRCHANGEDPRE
naohiro ono23beefe2021-11-13 12:38:49 +00002274 || event == EVENT_MODECHANGED
zeertzjqc601d982022-10-10 13:46:15 +01002275 || event == EVENT_MENUPOPUP
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002276 || event == EVENT_USER
LemonBoy09371822022-04-08 15:18:45 +01002277 || event == EVENT_WINCLOSED
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00002278 || event == EVENT_WINRESIZED
Danek Duvalld7d56032024-01-14 20:19:59 +01002279 || event == EVENT_WINSCROLLED
2280 || event == EVENT_TERMRESPONSEALL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002281 {
2282 fname = vim_strsave(fname);
2283 autocmd_fname_full = TRUE; // don't expand it later
2284 }
2285 else
2286 fname = FullName_save(fname, FALSE);
2287 }
2288 if (fname == NULL) // out of memory
2289 {
2290 vim_free(sfname);
2291 retval = FALSE;
2292 goto BYPASS_AU;
2293 }
2294
2295#ifdef BACKSLASH_IN_FILENAME
2296 /*
2297 * Replace all backslashes with forward slashes. This makes the
2298 * autocommand patterns portable between Unix and MS-DOS.
2299 */
2300 if (sfname != NULL)
2301 forward_slash(sfname);
2302 forward_slash(fname);
2303#endif
2304
2305#ifdef VMS
2306 // remove version for correct match
2307 if (sfname != NULL)
2308 vms_remove_version(sfname);
2309 vms_remove_version(fname);
2310#endif
2311
2312 /*
2313 * Set the name to be used for <amatch>.
2314 */
2315 autocmd_match = fname;
2316
2317
2318 // Don't redraw while doing autocommands.
2319 ++RedrawingDisabled;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002320
2321 // name and lnum are filled in later
2322 estack_push(ETYPE_AUCMD, NULL, 0);
ichizok7e5fe382023-04-15 13:17:50 +01002323 ESTACK_CHECK_SETUP;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002324
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002325 save_current_sctx = current_sctx;
2326
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002327#ifdef FEAT_EVAL
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002328# ifdef FEAT_PROFILE
2329 if (do_profiling == PROF_YES)
2330 prof_child_enter(&wait_time); // doesn't count for the caller itself
2331# endif
2332
2333 // Don't use local function variables, if called from a function.
2334 save_funccal(&funccal_entry);
2335#endif
2336
2337 /*
2338 * When starting to execute autocommands, save the search patterns.
2339 */
2340 if (!autocmd_busy)
2341 {
2342 save_search_patterns();
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002343 if (!ins_compl_active())
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002344 {
2345 saveRedobuff(&save_redo);
2346 did_save_redobuff = TRUE;
2347 }
zeertzjq5bf6c212024-03-31 18:41:27 +02002348 curbuf->b_did_filetype = curbuf->b_keep_filetype;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002349 }
2350
2351 /*
2352 * Note that we are applying autocmds. Some commands need to know.
2353 */
2354 autocmd_busy = TRUE;
2355 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL);
2356 ++nesting; // see matching decrement below
2357
2358 // Remember that FileType was triggered. Used for did_filetype().
2359 if (event == EVENT_FILETYPE)
zeertzjq5bf6c212024-03-31 18:41:27 +02002360 curbuf->b_did_filetype = TRUE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002361
2362 tail = gettail(fname);
2363
2364 // Find first autocommand that matches
LemonBoyeca7c602022-04-14 15:39:43 +01002365 CLEAR_FIELD(patcmd);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002366 patcmd.curpat = first_autopat[(int)event];
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002367 patcmd.group = group;
2368 patcmd.fname = fname;
2369 patcmd.sfname = sfname;
2370 patcmd.tail = tail;
2371 patcmd.event = event;
2372 patcmd.arg_bufnr = autocmd_bufnr;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002373 auto_next_pat(&patcmd, FALSE);
2374
2375 // found one, start executing the autocommands
2376 if (patcmd.curpat != NULL)
2377 {
2378 // add to active_apc_list
2379 patcmd.next = active_apc_list;
2380 active_apc_list = &patcmd;
2381
2382#ifdef FEAT_EVAL
2383 // set v:cmdarg (only when there is a matching pattern)
2384 save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG);
2385 if (eap != NULL)
2386 {
2387 save_cmdarg = set_cmdarg(eap, NULL);
2388 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit);
2389 }
2390 else
2391 save_cmdarg = NULL; // avoid gcc warning
2392#endif
2393 retval = TRUE;
2394 // mark the last pattern, to avoid an endless loop when more patterns
2395 // are added when executing autocommands
2396 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next)
2397 ap->last = FALSE;
2398 ap->last = TRUE;
Bram Moolenaara68e5952019-04-25 22:22:01 +02002399
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002400 // Make sure cursor and topline are valid. The first time the current
2401 // values are saved, restored by reset_lnums(). When nested only the
2402 // values are corrected when needed.
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002403 if (nesting == 1)
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002404 check_lnums(TRUE);
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002405 else
2406 check_lnums_nested(TRUE);
Bram Moolenaara68e5952019-04-25 22:22:01 +02002407
Christian Brabandt590aae32023-06-25 22:34:22 +01002408 int save_did_emsg = did_emsg;
2409 int save_ex_pressedreturn = get_pressedreturn();
ichizokc3f91c02021-12-17 09:44:33 +00002410
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002411 do_cmdline(NULL, getnextac, (void *)&patcmd,
2412 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
Bram Moolenaara68e5952019-04-25 22:22:01 +02002413
ichizokc3f91c02021-12-17 09:44:33 +00002414 did_emsg += save_did_emsg;
Christian Brabandt590aae32023-06-25 22:34:22 +01002415 set_pressedreturn(save_ex_pressedreturn);
ichizokc3f91c02021-12-17 09:44:33 +00002416
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002417 if (nesting == 1)
2418 // restore cursor and topline, unless they were changed
2419 reset_lnums();
Bram Moolenaara68e5952019-04-25 22:22:01 +02002420
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002421#ifdef FEAT_EVAL
2422 if (eap != NULL)
2423 {
2424 (void)set_cmdarg(NULL, save_cmdarg);
2425 set_vim_var_nr(VV_CMDBANG, save_cmdbang);
2426 }
2427#endif
2428 // delete from active_apc_list
2429 if (active_apc_list == &patcmd) // just in case
2430 active_apc_list = patcmd.next;
2431 }
2432
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002433 if (RedrawingDisabled > 0)
2434 --RedrawingDisabled;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002435 autocmd_busy = save_autocmd_busy;
2436 filechangeshell_busy = FALSE;
2437 autocmd_nested = save_autocmd_nested;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002438 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01002439 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002440 estack_pop();
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002441 vim_free(autocmd_fname);
2442 autocmd_fname = save_autocmd_fname;
2443 autocmd_fname_full = save_autocmd_fname_full;
2444 autocmd_bufnr = save_autocmd_bufnr;
2445 autocmd_match = save_autocmd_match;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002446 current_sctx = save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002447#ifdef FEAT_EVAL
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002448 restore_funccal();
2449# ifdef FEAT_PROFILE
2450 if (do_profiling == PROF_YES)
2451 prof_child_exit(&wait_time);
2452# endif
2453#endif
2454 KeyTyped = save_KeyTyped;
2455 vim_free(fname);
2456 vim_free(sfname);
2457 --nesting; // see matching increment above
2458
2459 /*
2460 * When stopping to execute autocommands, restore the search patterns and
2461 * the redo buffer. Free any buffers in the au_pending_free_buf list and
2462 * free any windows in the au_pending_free_win list.
2463 */
2464 if (!autocmd_busy)
2465 {
2466 restore_search_patterns();
2467 if (did_save_redobuff)
2468 restoreRedobuff(&save_redo);
zeertzjq5bf6c212024-03-31 18:41:27 +02002469 curbuf->b_did_filetype = FALSE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002470 while (au_pending_free_buf != NULL)
2471 {
2472 buf_T *b = au_pending_free_buf->b_next;
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002473
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002474 vim_free(au_pending_free_buf);
2475 au_pending_free_buf = b;
2476 }
2477 while (au_pending_free_win != NULL)
2478 {
2479 win_T *w = au_pending_free_win->w_next;
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002480
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002481 vim_free(au_pending_free_win);
2482 au_pending_free_win = w;
2483 }
2484 }
2485
2486 /*
2487 * Some events don't set or reset the Changed flag.
2488 * Check if still in the same buffer!
2489 */
2490 if (curbuf == old_curbuf
2491 && (event == EVENT_BUFREADPOST
2492 || event == EVENT_BUFWRITEPOST
2493 || event == EVENT_FILEAPPENDPOST
2494 || event == EVENT_VIMLEAVE
2495 || event == EVENT_VIMLEAVEPRE))
2496 {
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002497 if (curbuf->b_changed != save_changed)
2498 need_maketitle = TRUE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002499 curbuf->b_changed = save_changed;
2500 }
2501
2502 au_cleanup(); // may really delete removed patterns/commands now
2503
2504BYPASS_AU:
2505 // When wiping out a buffer make sure all its buffer-local autocommands
2506 // are deleted.
2507 if (event == EVENT_BUFWIPEOUT && buf != NULL)
2508 aubuflocal_remove(buf);
2509
2510 if (retval == OK && event == EVENT_FILETYPE)
zeertzjq5bf6c212024-03-31 18:41:27 +02002511 curbuf->b_au_did_filetype = TRUE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002512
2513 return retval;
2514}
2515
2516# ifdef FEAT_EVAL
2517static char_u *old_termresponse = NULL;
Danek Duvalld7d56032024-01-14 20:19:59 +01002518static char_u *old_termu7resp = NULL;
2519static char_u *old_termblinkresp = NULL;
2520static char_u *old_termrbgresp = NULL;
2521static char_u *old_termrfgresp = NULL;
2522static char_u *old_termstyleresp = NULL;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002523# endif
2524
2525/*
2526 * Block triggering autocommands until unblock_autocmd() is called.
2527 * Can be used recursively, so long as it's symmetric.
2528 */
2529 void
2530block_autocmds(void)
2531{
2532# ifdef FEAT_EVAL
2533 // Remember the value of v:termresponse.
2534 if (autocmd_blocked == 0)
Danek Duvalld7d56032024-01-14 20:19:59 +01002535 {
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002536 old_termresponse = get_vim_var_str(VV_TERMRESPONSE);
Danek Duvalld7d56032024-01-14 20:19:59 +01002537 old_termu7resp = get_vim_var_str(VV_TERMU7RESP);
2538 old_termblinkresp = get_vim_var_str(VV_TERMBLINKRESP);
2539 old_termrbgresp = get_vim_var_str(VV_TERMRBGRESP);
2540 old_termrfgresp = get_vim_var_str(VV_TERMRFGRESP);
2541 old_termstyleresp = get_vim_var_str(VV_TERMSTYLERESP);
2542 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002543# endif
2544 ++autocmd_blocked;
2545}
2546
2547 void
2548unblock_autocmds(void)
2549{
2550 --autocmd_blocked;
2551
2552# ifdef FEAT_EVAL
Danek Duvalld7d56032024-01-14 20:19:59 +01002553 // When v:termresponse, etc, were set while autocommands were blocked,
2554 // trigger the autocommands now. Esp. useful when executing a shell
2555 // command during startup (vimdiff).
2556 if (autocmd_blocked == 0)
2557 {
2558 if (get_vim_var_str(VV_TERMRESPONSE) != old_termresponse)
2559 {
2560 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf);
2561 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"version", NULL, FALSE, curbuf);
2562 }
2563 if (get_vim_var_str(VV_TERMU7RESP) != old_termu7resp)
2564 {
2565 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"ambiguouswidth", NULL, FALSE, curbuf);
2566 }
2567 if (get_vim_var_str(VV_TERMBLINKRESP) != old_termblinkresp)
2568 {
2569 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"cursorblink", NULL, FALSE, curbuf);
2570 }
2571 if (get_vim_var_str(VV_TERMRBGRESP) != old_termrbgresp)
2572 {
2573 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"background", NULL, FALSE, curbuf);
2574 }
2575 if (get_vim_var_str(VV_TERMRFGRESP) != old_termrfgresp)
2576 {
2577 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"foreground", NULL, FALSE, curbuf);
2578 }
2579 if (get_vim_var_str(VV_TERMSTYLERESP) != old_termstyleresp)
2580 {
2581 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"cursorshape", NULL, FALSE, curbuf);
2582 }
2583 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002584# endif
2585}
2586
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002587 int
2588is_autocmd_blocked(void)
2589{
2590 return autocmd_blocked != 0;
2591}
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002592
2593/*
2594 * Find next autocommand pattern that matches.
2595 */
2596 static void
2597auto_next_pat(
LemonBoyeca7c602022-04-14 15:39:43 +01002598 AutoPatCmd_T *apc,
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002599 int stop_at_last) // stop when 'last' flag is set
2600{
2601 AutoPat *ap;
2602 AutoCmd *cp;
2603 char_u *name;
2604 char *s;
LemonBoyeca7c602022-04-14 15:39:43 +01002605 estack_T *entry;
2606 char_u *namep;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002607
LemonBoyeca7c602022-04-14 15:39:43 +01002608 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
2609
2610 // Clear the exestack entry for this ETYPE_AUCMD entry.
2611 VIM_CLEAR(entry->es_name);
2612 entry->es_info.aucmd = NULL;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002613
2614 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next)
2615 {
2616 apc->curpat = NULL;
2617
2618 // Only use a pattern when it has not been removed, has commands and
2619 // the group matches. For buffer-local autocommands only check the
2620 // buffer number.
2621 if (ap->pat != NULL && ap->cmds != NULL
2622 && (apc->group == AUGROUP_ALL || apc->group == ap->group))
2623 {
2624 // execution-condition
2625 if (ap->buflocal_nr == 0
2626 ? (match_file_pat(NULL, &ap->reg_prog, apc->fname,
2627 apc->sfname, apc->tail, ap->allow_dirs))
2628 : ap->buflocal_nr == apc->arg_bufnr)
2629 {
2630 name = event_nr2name(apc->event);
2631 s = _("%s Autocommands for \"%s\"");
LemonBoyeca7c602022-04-14 15:39:43 +01002632 namep = alloc(STRLEN(s) + STRLEN(name) + ap->patlen + 1);
2633 if (namep != NULL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002634 {
LemonBoyeca7c602022-04-14 15:39:43 +01002635 sprintf((char *)namep, s, (char *)name, (char *)ap->pat);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002636 if (p_verbose >= 8)
2637 {
2638 verbose_enter();
LemonBoyeca7c602022-04-14 15:39:43 +01002639 smsg(_("Executing %s"), namep);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002640 verbose_leave();
2641 }
2642 }
2643
LemonBoyeca7c602022-04-14 15:39:43 +01002644 // Update the exestack entry for this autocmd.
2645 entry->es_name = namep;
2646 entry->es_info.aucmd = apc;
2647
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002648 apc->curpat = ap;
2649 apc->nextcmd = ap->cmds;
2650 // mark last command
2651 for (cp = ap->cmds; cp->next != NULL; cp = cp->next)
2652 cp->last = FALSE;
2653 cp->last = TRUE;
2654 }
2655 line_breakcheck();
2656 if (apc->curpat != NULL) // found a match
2657 break;
2658 }
2659 if (stop_at_last && ap->last)
2660 break;
2661 }
2662}
2663
Dominique Pellee764d1b2023-03-12 21:20:59 +00002664#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002665/*
LemonBoyeca7c602022-04-14 15:39:43 +01002666 * Get the script context where autocommand "acp" is defined.
2667 */
2668 sctx_T *
2669acp_script_ctx(AutoPatCmd_T *acp)
2670{
2671 return &acp->script_ctx;
2672}
Dominique Pellee764d1b2023-03-12 21:20:59 +00002673#endif
LemonBoyeca7c602022-04-14 15:39:43 +01002674
2675/*
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002676 * Get next autocommand command.
2677 * Called by do_cmdline() to get the next line for ":if".
2678 * Returns allocated string, or NULL for end of autocommands.
2679 */
2680 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002681getnextac(
2682 int c UNUSED,
2683 void *cookie,
2684 int indent UNUSED,
2685 getline_opt_T options UNUSED)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002686{
LemonBoyeca7c602022-04-14 15:39:43 +01002687 AutoPatCmd_T *acp = (AutoPatCmd_T *)cookie;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002688 char_u *retval;
2689 AutoCmd *ac;
2690
2691 // Can be called again after returning the last line.
2692 if (acp->curpat == NULL)
2693 return NULL;
2694
2695 // repeat until we find an autocommand to execute
2696 for (;;)
2697 {
2698 // skip removed commands
2699 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL)
2700 if (acp->nextcmd->last)
2701 acp->nextcmd = NULL;
2702 else
2703 acp->nextcmd = acp->nextcmd->next;
2704
2705 if (acp->nextcmd != NULL)
2706 break;
2707
2708 // at end of commands, find next pattern that matches
2709 if (acp->curpat->last)
2710 acp->curpat = NULL;
2711 else
2712 acp->curpat = acp->curpat->next;
2713 if (acp->curpat != NULL)
2714 auto_next_pat(acp, TRUE);
2715 if (acp->curpat == NULL)
2716 return NULL;
2717 }
2718
2719 ac = acp->nextcmd;
2720
2721 if (p_verbose >= 9)
2722 {
2723 verbose_enter_scroll();
2724 smsg(_("autocommand %s"), ac->cmd);
2725 msg_puts("\n"); // don't overwrite this either
2726 verbose_leave_scroll();
2727 }
2728 retval = vim_strsave(ac->cmd);
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002729 // Remove one-shot ("once") autocmd in anticipation of its execution.
2730 if (ac->once)
2731 au_del_cmd(ac);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002732 autocmd_nested = ac->nested;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002733 current_sctx = ac->script_ctx;
LemonBoyeca7c602022-04-14 15:39:43 +01002734 acp->script_ctx = current_sctx;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002735 if (ac->last)
2736 acp->nextcmd = NULL;
2737 else
2738 acp->nextcmd = ac->next;
2739 return retval;
2740}
2741
2742/*
2743 * Return TRUE if there is a matching autocommand for "fname".
2744 * To account for buffer-local autocommands, function needs to know
2745 * in which buffer the file will be opened.
2746 */
2747 int
2748has_autocmd(event_T event, char_u *sfname, buf_T *buf)
2749{
2750 AutoPat *ap;
2751 char_u *fname;
2752 char_u *tail = gettail(sfname);
2753 int retval = FALSE;
2754
2755 fname = FullName_save(sfname, FALSE);
2756 if (fname == NULL)
2757 return FALSE;
2758
2759#ifdef BACKSLASH_IN_FILENAME
2760 /*
2761 * Replace all backslashes with forward slashes. This makes the
2762 * autocommand patterns portable between Unix and MS-DOS.
2763 */
2764 sfname = vim_strsave(sfname);
2765 if (sfname != NULL)
2766 forward_slash(sfname);
2767 forward_slash(fname);
2768#endif
2769
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002770 FOR_ALL_AUTOCMD_PATTERNS(event, ap)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002771 if (ap->pat != NULL && ap->cmds != NULL
2772 && (ap->buflocal_nr == 0
2773 ? match_file_pat(NULL, &ap->reg_prog,
2774 fname, sfname, tail, ap->allow_dirs)
2775 : buf != NULL && ap->buflocal_nr == buf->b_fnum
2776 ))
2777 {
2778 retval = TRUE;
2779 break;
2780 }
2781
2782 vim_free(fname);
2783#ifdef BACKSLASH_IN_FILENAME
2784 vim_free(sfname);
2785#endif
2786
2787 return retval;
2788}
2789
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002790/*
2791 * Function given to ExpandGeneric() to obtain the list of autocommand group
2792 * names.
2793 */
2794 char_u *
2795get_augroup_name(expand_T *xp UNUSED, int idx)
2796{
2797 if (idx == augroups.ga_len) // add "END" add the end
2798 return (char_u *)"END";
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01002799 if (idx < 0 || idx >= augroups.ga_len) // end of list
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002800 return NULL;
2801 if (AUGROUP_NAME(idx) == NULL || AUGROUP_NAME(idx) == get_deleted_augroup())
2802 // skip deleted entries
2803 return (char_u *)"";
2804 return AUGROUP_NAME(idx); // return a name
2805}
2806
2807static int include_groups = FALSE;
2808
2809 char_u *
2810set_context_in_autocmd(
2811 expand_T *xp,
2812 char_u *arg,
2813 int doautocmd) // TRUE for :doauto*, FALSE for :autocmd
2814{
2815 char_u *p;
2816 int group;
2817
2818 // check for a group name, skip it if present
2819 include_groups = FALSE;
2820 p = arg;
2821 group = au_get_grouparg(&arg);
2822 if (group == AUGROUP_ERROR)
2823 return NULL;
2824 // If there only is a group name that's what we expand.
2825 if (*arg == NUL && group != AUGROUP_ALL && !VIM_ISWHITE(arg[-1]))
2826 {
2827 arg = p;
2828 group = AUGROUP_ALL;
2829 }
2830
2831 // skip over event name
2832 for (p = arg; *p != NUL && !VIM_ISWHITE(*p); ++p)
2833 if (*p == ',')
2834 arg = p + 1;
2835 if (*p == NUL)
2836 {
2837 if (group == AUGROUP_ALL)
2838 include_groups = TRUE;
2839 xp->xp_context = EXPAND_EVENTS; // expand event name
2840 xp->xp_pattern = arg;
2841 return NULL;
2842 }
2843
2844 // skip over pattern
2845 arg = skipwhite(p);
2846 while (*arg && (!VIM_ISWHITE(*arg) || arg[-1] == '\\'))
2847 arg++;
2848 if (*arg)
2849 return arg; // expand (next) command
2850
2851 if (doautocmd)
2852 xp->xp_context = EXPAND_FILES; // expand file names
2853 else
2854 xp->xp_context = EXPAND_NOTHING; // pattern is not expanded
2855 return NULL;
2856}
2857
2858/*
2859 * Function given to ExpandGeneric() to obtain the list of event names.
2860 */
2861 char_u *
2862get_event_name(expand_T *xp UNUSED, int idx)
2863{
John Marriott78d742a2024-04-02 20:26:01 +02002864 int i;
2865
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002866 if (idx < augroups.ga_len) // First list group names, if wanted
2867 {
2868 if (!include_groups || AUGROUP_NAME(idx) == NULL
2869 || AUGROUP_NAME(idx) == get_deleted_augroup())
2870 return (char_u *)""; // skip deleted entries
2871 return AUGROUP_NAME(idx); // return a name
2872 }
John Marriott78d742a2024-04-02 20:26:01 +02002873
2874 i = idx - augroups.ga_len;
Luuk van Baalb7147f82025-02-08 18:52:39 +01002875 if (i < 0 || i >= NUM_EVENTS)
John Marriott78d742a2024-04-02 20:26:01 +02002876 return NULL;
2877
John Marriott8d4477e2024-11-02 15:59:01 +01002878 return event_tab[i].value.string;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002879}
2880
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002881/*
2882 * Function given to ExpandGeneric() to obtain the list of event names. Don't
2883 * include groups.
2884 */
2885 char_u *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002886get_event_name_no_group(expand_T *xp UNUSED, int idx, int win)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002887{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002888 if (idx < 0 || idx >= NUM_EVENTS)
John Marriott78d742a2024-04-02 20:26:01 +02002889 return NULL;
2890
Luuk van Baalb7147f82025-02-08 18:52:39 +01002891 if (!win)
2892 return event_tab[idx].value.string;
2893
2894 // Need to check subset of allowed values for 'eventignorewin'.
2895 int j = 0;
2896 for (int i = 0; i < NUM_EVENTS; ++i)
2897 {
2898 j += event_tab[i].key <= 0;
2899 if (j == idx + 1)
2900 return event_tab[i].value.string;
2901 }
2902
2903 return NULL;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002904}
2905
Jim Zhou5606ca52025-03-13 21:58:25 +01002906/*
2907 * Return TRUE when there is a TabClosedPre autocommand defined.
2908 */
2909 int
2910has_tabclosedpre(void)
2911{
2912 return (first_autopat[(int)EVENT_TABCLOSEDPRE] != NULL);
2913}
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002914
2915#if defined(FEAT_EVAL) || defined(PROTO)
2916/*
2917 * Return TRUE if autocmd is supported.
2918 */
2919 int
2920autocmd_supported(char_u *name)
2921{
2922 char_u *p;
2923
2924 return (event_name2nr(name, &p) != NUM_EVENTS);
2925}
2926
2927/*
2928 * Return TRUE if an autocommand is defined for a group, event and
2929 * pattern: The group can be omitted to accept any group. "event" and "pattern"
2930 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept
2931 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted.
2932 * Used for:
2933 * exists("#Group") or
2934 * exists("#Group#Event") or
2935 * exists("#Group#Event#pat") or
2936 * exists("#Event") or
2937 * exists("#Event#pat")
2938 */
2939 int
2940au_exists(char_u *arg)
2941{
2942 char_u *arg_save;
2943 char_u *pattern = NULL;
2944 char_u *event_name;
2945 char_u *p;
2946 event_T event;
2947 AutoPat *ap;
2948 buf_T *buflocal_buf = NULL;
2949 int group;
2950 int retval = FALSE;
2951
2952 // Make a copy so that we can change the '#' chars to a NUL.
2953 arg_save = vim_strsave(arg);
2954 if (arg_save == NULL)
2955 return FALSE;
2956 p = vim_strchr(arg_save, '#');
2957 if (p != NULL)
2958 *p++ = NUL;
2959
2960 // First, look for an autocmd group name
2961 group = au_find_group(arg_save);
2962 if (group == AUGROUP_ERROR)
2963 {
2964 // Didn't match a group name, assume the first argument is an event.
2965 group = AUGROUP_ALL;
2966 event_name = arg_save;
2967 }
2968 else
2969 {
2970 if (p == NULL)
2971 {
2972 // "Group": group name is present and it's recognized
2973 retval = TRUE;
2974 goto theend;
2975 }
2976
2977 // Must be "Group#Event" or "Group#Event#pat".
2978 event_name = p;
2979 p = vim_strchr(event_name, '#');
2980 if (p != NULL)
2981 *p++ = NUL; // "Group#Event#pat"
2982 }
2983
2984 pattern = p; // "pattern" is NULL when there is no pattern
2985
2986 // find the index (enum) for the event name
2987 event = event_name2nr(event_name, &p);
2988
2989 // return FALSE if the event name is not recognized
2990 if (event == NUM_EVENTS)
2991 goto theend;
2992
2993 // Find the first autocommand for this event.
2994 // If there isn't any, return FALSE;
2995 // If there is one and no pattern given, return TRUE;
2996 ap = first_autopat[(int)event];
2997 if (ap == NULL)
2998 goto theend;
2999
3000 // if pattern is "<buffer>", special handling is needed which uses curbuf
3001 // for pattern "<buffer=N>, fnamecmp() will work fine
3002 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0)
3003 buflocal_buf = curbuf;
3004
3005 // Check if there is an autocommand with the given pattern.
3006 for ( ; ap != NULL; ap = ap->next)
3007 // only use a pattern when it has not been removed and has commands.
3008 // For buffer-local autocommands, fnamecmp() works fine.
3009 if (ap->pat != NULL && ap->cmds != NULL
3010 && (group == AUGROUP_ALL || ap->group == group)
3011 && (pattern == NULL
3012 || (buflocal_buf == NULL
3013 ? fnamecmp(ap->pat, pattern) == 0
3014 : ap->buflocal_nr == buflocal_buf->b_fnum)))
3015 {
3016 retval = TRUE;
3017 break;
3018 }
3019
3020theend:
3021 vim_free(arg_save);
3022 return retval;
3023}
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003024
3025/*
3026 * autocmd_add() and autocmd_delete() functions
3027 */
3028 static void
3029autocmd_add_or_delete(typval_T *argvars, typval_T *rettv, int delete)
3030{
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003031 list_T *aucmd_list;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003032 listitem_T *li;
3033 dict_T *event_dict;
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003034 dictitem_T *di;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003035 char_u *event_name = NULL;
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003036 list_T *event_list;
3037 listitem_T *eli;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003038 event_T event;
3039 char_u *group_name = NULL;
3040 int group;
3041 char_u *pat = NULL;
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003042 list_T *pat_list;
3043 listitem_T *pli;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003044 char_u *cmd = NULL;
3045 char_u *end;
3046 int once;
3047 int nested;
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003048 int replace; // replace the cmd for a group/event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003049 int retval = VVAL_TRUE;
3050 int save_augroup = current_augroup;
3051
3052 rettv->v_type = VAR_BOOL;
3053 rettv->vval.v_number = VVAL_FALSE;
3054
3055 if (check_for_list_arg(argvars, 0) == FAIL)
3056 return;
3057
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003058 aucmd_list = argvars[0].vval.v_list;
3059 if (aucmd_list == NULL)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003060 return;
3061
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003062 FOR_ALL_LIST_ITEMS(aucmd_list, li)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003063 {
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003064 VIM_CLEAR(group_name);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003065 VIM_CLEAR(cmd);
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003066 event_name = NULL;
3067 event_list = NULL;
3068 pat = NULL;
3069 pat_list = NULL;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003070
3071 if (li->li_tv.v_type != VAR_DICT)
3072 continue;
3073
3074 event_dict = li->li_tv.vval.v_dict;
3075 if (event_dict == NULL)
3076 continue;
3077
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003078 di = dict_find(event_dict, (char_u *)"event", -1);
3079 if (di != NULL)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003080 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003081 if (di->di_tv.v_type == VAR_STRING)
3082 {
3083 event_name = di->di_tv.vval.v_string;
3084 if (event_name == NULL)
3085 {
3086 emsg(_(e_string_required));
3087 continue;
3088 }
3089 }
3090 else if (di->di_tv.v_type == VAR_LIST)
3091 {
3092 event_list = di->di_tv.vval.v_list;
3093 if (event_list == NULL)
3094 {
3095 emsg(_(e_list_required));
3096 continue;
3097 }
3098 }
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003099 else
3100 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003101 emsg(_(e_string_or_list_expected));
3102 continue;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003103 }
3104 }
3105
Bram Moolenaard61efa52022-07-23 09:52:04 +01003106 group_name = dict_get_string(event_dict, "group", TRUE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003107 if (group_name == NULL || *group_name == NUL)
3108 // if the autocmd group name is not specified, then use the current
3109 // autocmd group
3110 group = current_augroup;
3111 else
3112 {
3113 group = au_find_group(group_name);
3114 if (group == AUGROUP_ERROR)
3115 {
3116 if (delete)
3117 {
3118 semsg(_(e_no_such_group_str), group_name);
3119 retval = VVAL_FALSE;
3120 break;
3121 }
3122 // group is not found, create it now
3123 group = au_new_group(group_name);
3124 if (group == AUGROUP_ERROR)
3125 {
3126 semsg(_(e_no_such_group_str), group_name);
3127 retval = VVAL_FALSE;
3128 break;
3129 }
3130
3131 current_augroup = group;
3132 }
3133 }
3134
3135 // if a buffer number is specified, then generate a pattern of the form
3136 // "<buffer=n>. Otherwise, use the pattern supplied by the user.
3137 if (dict_has_key(event_dict, "bufnr"))
3138 {
3139 varnumber_T bnum;
3140
Bram Moolenaard61efa52022-07-23 09:52:04 +01003141 bnum = dict_get_number_def(event_dict, "bufnr", -1);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003142 if (bnum == -1)
3143 continue;
3144
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003145 vim_snprintf((char *)IObuff, IOSIZE, "<buffer=%d>", (int)bnum);
3146 pat = IObuff;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003147 }
3148 else
3149 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003150 di = dict_find(event_dict, (char_u *)"pattern", -1);
3151 if (di != NULL)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003152 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003153 if (di->di_tv.v_type == VAR_STRING)
3154 {
3155 pat = di->di_tv.vval.v_string;
3156 if (pat == NULL)
3157 {
3158 emsg(_(e_string_required));
3159 continue;
3160 }
3161 }
3162 else if (di->di_tv.v_type == VAR_LIST)
3163 {
3164 pat_list = di->di_tv.vval.v_list;
3165 if (pat_list == NULL)
3166 {
3167 emsg(_(e_list_required));
3168 continue;
3169 }
3170 }
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003171 else
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003172 {
3173 emsg(_(e_string_or_list_expected));
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003174 continue;
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003175 }
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003176 }
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003177 else if (delete)
3178 pat = (char_u *)"";
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003179 }
3180
Bram Moolenaard61efa52022-07-23 09:52:04 +01003181 once = dict_get_bool(event_dict, "once", FALSE);
3182 nested = dict_get_bool(event_dict, "nested", FALSE);
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003183 // if 'replace' is true, then remove all the commands associated with
3184 // this autocmd event/group and add the new command.
Bram Moolenaard61efa52022-07-23 09:52:04 +01003185 replace = dict_get_bool(event_dict, "replace", FALSE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003186
Bram Moolenaard61efa52022-07-23 09:52:04 +01003187 cmd = dict_get_string(event_dict, "cmd", TRUE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003188 if (cmd == NULL)
3189 {
3190 if (delete)
3191 cmd = vim_strsave((char_u *)"");
3192 else
3193 continue;
3194 }
3195
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003196 if (delete && (event_name == NULL
3197 || (event_name[0] == '*' && event_name[1] == NUL)))
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003198 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003199 // if the event name is not specified or '*', delete all the events
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003200 for (event = (event_T)0; (int)event < NUM_EVENTS;
3201 event = (event_T)((int)event + 1))
3202 {
3203 if (do_autocmd_event(event, pat, once, nested, cmd, delete,
3204 group, 0) == FAIL)
3205 {
3206 retval = VVAL_FALSE;
3207 break;
3208 }
3209 }
3210 }
3211 else
3212 {
Bram Moolenaar968443e2022-05-27 21:16:34 +01003213 char_u *p = NULL;
3214
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003215 eli = NULL;
3216 end = NULL;
3217 while (TRUE)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003218 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003219 if (event_list != NULL)
3220 {
3221 if (eli == NULL)
3222 eli = event_list->lv_first;
3223 else
3224 eli = eli->li_next;
3225 if (eli == NULL)
3226 break;
3227 if (eli->li_tv.v_type != VAR_STRING
Bram Moolenaar882476a2022-06-01 16:02:38 +01003228 || (p = eli->li_tv.vval.v_string) == NULL)
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003229 {
3230 emsg(_(e_string_required));
Bram Moolenaar882476a2022-06-01 16:02:38 +01003231 break;
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003232 }
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003233 }
3234 else
3235 {
Bram Moolenaar882476a2022-06-01 16:02:38 +01003236 if (p == NULL)
3237 p = event_name;
3238 if (p == NULL || *p == NUL)
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003239 break;
3240 }
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003241
3242 event = event_name2nr(p, &end);
3243 if (event == NUM_EVENTS || *end != NUL)
3244 {
Bram Moolenaar882476a2022-06-01 16:02:38 +01003245 // this also catches something following a valid event name
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003246 semsg(_(e_no_such_event_str), p);
3247 retval = VVAL_FALSE;
3248 break;
3249 }
3250 if (pat != NULL)
3251 {
3252 if (do_autocmd_event(event, pat, once, nested, cmd,
3253 delete | replace, group, 0) == FAIL)
3254 {
3255 retval = VVAL_FALSE;
3256 break;
3257 }
3258 }
3259 else if (pat_list != NULL)
3260 {
3261 FOR_ALL_LIST_ITEMS(pat_list, pli)
3262 {
3263 if (pli->li_tv.v_type != VAR_STRING
3264 || pli->li_tv.vval.v_string == NULL)
3265 {
3266 emsg(_(e_string_required));
3267 continue;
3268 }
3269 if (do_autocmd_event(event,
3270 pli->li_tv.vval.v_string, once, nested,
3271 cmd, delete | replace, group, 0) ==
3272 FAIL)
3273 {
3274 retval = VVAL_FALSE;
3275 break;
3276 }
3277 }
3278 if (retval == VVAL_FALSE)
3279 break;
3280 }
3281 if (event_name != NULL)
3282 p = end;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003283 }
3284 }
3285
3286 // if only the autocmd group name is specified for delete and the
3287 // autocmd event, pattern and cmd are not specified, then delete the
3288 // autocmd group.
3289 if (delete && group_name != NULL &&
3290 (event_name == NULL || event_name[0] == NUL)
3291 && (pat == NULL || pat[0] == NUL)
3292 && (cmd == NULL || cmd[0] == NUL))
3293 au_del_group(group_name);
3294 }
3295
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003296 VIM_CLEAR(group_name);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003297 VIM_CLEAR(cmd);
3298
3299 current_augroup = save_augroup;
3300 rettv->vval.v_number = retval;
3301}
3302
3303/*
3304 * autocmd_add() function
3305 */
3306 void
3307f_autocmd_add(typval_T *argvars, typval_T *rettv)
3308{
3309 autocmd_add_or_delete(argvars, rettv, FALSE);
3310}
3311
3312/*
3313 * autocmd_delete() function
3314 */
3315 void
3316f_autocmd_delete(typval_T *argvars, typval_T *rettv)
3317{
3318 autocmd_add_or_delete(argvars, rettv, TRUE);
3319}
3320
3321/*
3322 * autocmd_get() function
3323 * Returns a List of autocmds.
3324 */
3325 void
3326f_autocmd_get(typval_T *argvars, typval_T *rettv)
3327{
3328 event_T event_arg = NUM_EVENTS;
3329 event_T event;
3330 AutoPat *ap;
3331 AutoCmd *ac;
3332 list_T *event_list;
3333 dict_T *event_dict;
3334 char_u *event_name = NULL;
3335 char_u *pat = NULL;
3336 char_u *name = NULL;
3337 int group = AUGROUP_ALL;
3338
Yegappan Lakshmananca195cc2022-06-14 13:42:26 +01003339 if (rettv_list_alloc(rettv) == FAIL)
3340 return;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003341 if (check_for_opt_dict_arg(argvars, 0) == FAIL)
3342 return;
3343
3344 if (argvars[0].v_type == VAR_DICT)
3345 {
3346 // return only the autocmds in the specified group
3347 if (dict_has_key(argvars[0].vval.v_dict, "group"))
3348 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003349 name = dict_get_string(argvars[0].vval.v_dict, "group", TRUE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003350 if (name == NULL)
3351 return;
3352
3353 if (*name == NUL)
3354 group = AUGROUP_DEFAULT;
3355 else
3356 {
3357 group = au_find_group(name);
3358 if (group == AUGROUP_ERROR)
3359 {
3360 semsg(_(e_no_such_group_str), name);
3361 vim_free(name);
3362 return;
3363 }
3364 }
3365 vim_free(name);
3366 }
3367
3368 // return only the autocmds for the specified event
3369 if (dict_has_key(argvars[0].vval.v_dict, "event"))
3370 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003371 name = dict_get_string(argvars[0].vval.v_dict, "event", TRUE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003372 if (name == NULL)
3373 return;
3374
3375 if (name[0] == '*' && name[1] == NUL)
3376 event_arg = NUM_EVENTS;
3377 else
3378 {
John Marriott78d742a2024-04-02 20:26:01 +02003379 keyvalue_T target;
3380 keyvalue_T *entry;
3381
3382 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01003383 target.value.string = name;
3384 target.value.length = STRLEN(target.value.string);
3385 entry = (keyvalue_T *)bsearch(&target, &event_tab,
Luuk van Baalb7147f82025-02-08 18:52:39 +01003386 NUM_EVENTS, sizeof(event_tab[0]), cmp_keyvalue_value_ni);
John Marriott78d742a2024-04-02 20:26:01 +02003387 if (entry == NULL)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003388 {
3389 semsg(_(e_no_such_event_str), name);
3390 vim_free(name);
3391 return;
3392 }
Luuk van Baalb7147f82025-02-08 18:52:39 +01003393 event_arg = (event_T)abs(entry->key);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003394 }
3395 vim_free(name);
3396 }
3397
3398 // return only the autocmds for the specified pattern
3399 if (dict_has_key(argvars[0].vval.v_dict, "pattern"))
3400 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003401 pat = dict_get_string(argvars[0].vval.v_dict, "pattern", TRUE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003402 if (pat == NULL)
3403 return;
3404 }
3405 }
3406
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003407 event_list = rettv->vval.v_list;
3408
3409 // iterate through all the autocmd events
3410 for (event = (event_T)0; (int)event < NUM_EVENTS;
3411 event = (event_T)((int)event + 1))
3412 {
3413 if (event_arg != NUM_EVENTS && event != event_arg)
3414 continue;
3415
3416 event_name = event_nr2name(event);
3417
3418 // iterate through all the patterns for this autocmd event
3419 FOR_ALL_AUTOCMD_PATTERNS(event, ap)
3420 {
3421 char_u *group_name;
3422
zeertzjq2d1d5c62024-06-09 16:44:33 +02003423 if (ap->pat == NULL) // pattern has been removed
3424 continue;
3425
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003426 if (group != AUGROUP_ALL && group != ap->group)
3427 continue;
3428
3429 if (pat != NULL && STRCMP(pat, ap->pat) != 0)
3430 continue;
3431
3432 group_name = get_augroup_name(NULL, ap->group);
3433
3434 // iterate through all the commands for this pattern and add one
3435 // item for each cmd.
3436 for (ac = ap->cmds; ac != NULL; ac = ac->next)
3437 {
3438 event_dict = dict_alloc();
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003439 if (event_dict == NULL
3440 || list_append_dict(event_list, event_dict) == FAIL)
Christian Brabandt29269a72024-04-16 22:44:31 +02003441 {
3442 vim_free(pat);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003443 return;
Christian Brabandt29269a72024-04-16 22:44:31 +02003444 }
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003445
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003446 if (dict_add_string(event_dict, "event", event_name) == FAIL
3447 || dict_add_string(event_dict, "group",
3448 group_name == NULL ? (char_u *)""
3449 : group_name) == FAIL
3450 || (ap->buflocal_nr != 0
3451 && (dict_add_number(event_dict, "bufnr",
3452 ap->buflocal_nr) == FAIL))
3453 || dict_add_string(event_dict, "pattern",
3454 ap->pat) == FAIL
3455 || dict_add_string(event_dict, "cmd", ac->cmd) == FAIL
3456 || dict_add_bool(event_dict, "once", ac->once) == FAIL
3457 || dict_add_bool(event_dict, "nested",
3458 ac->nested) == FAIL)
Christian Brabandt29269a72024-04-16 22:44:31 +02003459 {
3460 vim_free(pat);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003461 return;
Christian Brabandt29269a72024-04-16 22:44:31 +02003462 }
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003463 }
3464 }
3465 }
3466
3467 vim_free(pat);
3468}
3469
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01003470#endif