blob: 6a5f035d2e97a6ccbc8ef18e1bee36b5e5cd2759 [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
zeertzjq5717ee32025-05-25 16:59:50 +02001588 int same_buffer = buf == curbuf;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001589
1590 // Find a window that is for the new buffer
zeertzjq5717ee32025-05-25 16:59:50 +02001591 if (same_buffer) // be quick when buf is curbuf
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001592 win = curwin;
1593 else
1594 FOR_ALL_WINDOWS(win)
1595 if (win->w_buffer == buf)
1596 break;
1597
Bram Moolenaare76062c2022-11-28 18:51:43 +00001598 // Allocate a window when needed.
1599 win_T *auc_win = NULL;
1600 int auc_idx = AUCMD_WIN_COUNT;
1601 if (win == NULL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001602 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001603 for (auc_idx = 0; auc_idx < AUCMD_WIN_COUNT; ++auc_idx)
1604 if (!aucmd_win[auc_idx].auc_win_used)
1605 {
Bram Moolenaar84497cd2022-11-28 20:34:52 +00001606 if (aucmd_win[auc_idx].auc_win == NULL)
1607 aucmd_win[auc_idx].auc_win = win_alloc_popup_win();
1608 auc_win = aucmd_win[auc_idx].auc_win;
Bram Moolenaare76062c2022-11-28 18:51:43 +00001609 if (auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00001610 aucmd_win[auc_idx].auc_win_used = TRUE;
Bram Moolenaare76062c2022-11-28 18:51:43 +00001611 break;
1612 }
1613
1614 // If this fails (out of memory or using all AUCMD_WIN_COUNT
1615 // entries) then we can't reliable execute the autocmd, return with
1616 // "curbuf" unequal "buf".
1617 if (auc_win == NULL)
1618 return;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001619 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001620
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001621 aco->save_curwin_id = curwin->w_id;
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001622 aco->save_prevwin_id = prevwin == NULL ? 0 : prevwin->w_id;
Bram Moolenaar05a627c2023-04-09 22:01:31 +01001623 aco->save_State = State;
zeertzjqf2678472024-01-17 21:22:59 +01001624#ifdef FEAT_JOB_CHANNEL
1625 if (bt_prompt(curbuf))
1626 aco->save_prompt_insert = curbuf->b_prompt_insert;
1627#endif
Bram Moolenaar05a627c2023-04-09 22:01:31 +01001628
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001629 if (win != NULL)
1630 {
1631 // There is a window for "buf" in the current tab page, make it the
1632 // curwin. This is preferred, it has the least side effects (esp. if
1633 // "buf" is curbuf).
Bram Moolenaare76062c2022-11-28 18:51:43 +00001634 aco->use_aucmd_win_idx = -1;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001635 curwin = win;
1636 }
1637 else
1638 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001639 // There is no window for "buf", use "auc_win". To minimize the side
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001640 // effects, insert it in the current tab page.
1641 // Anything related to a window (e.g., setting folds) may have
1642 // unexpected results.
Bram Moolenaare76062c2022-11-28 18:51:43 +00001643 aco->use_aucmd_win_idx = auc_idx;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001644
Bram Moolenaare76062c2022-11-28 18:51:43 +00001645 win_init_popup_win(auc_win, buf);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001646
zeertzjq9d956ee2024-04-07 18:16:10 +02001647 // Make sure tp_localdir and globaldir are NULL to avoid a
1648 // chdir() in win_enter_ext().
1649 // win_init_popup_win() has already set w_localdir to NULL.
1650 aco->tp_localdir = curtab->tp_localdir;
1651 curtab->tp_localdir = NULL;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001652 aco->globaldir = globaldir;
1653 globaldir = NULL;
1654
Bram Moolenaare76062c2022-11-28 18:51:43 +00001655 // Split the current window, put the auc_win in the upper half.
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001656 // We don't want the BufEnter or WinEnter autocommands.
1657 block_autocmds();
1658 make_snapshot(SNAP_AUCMD_IDX);
1659 save_ea = p_ea;
1660 p_ea = FALSE;
1661
1662#ifdef FEAT_AUTOCHDIR
1663 // Prevent chdir() call in win_enter_ext(), through do_autochdir().
1664 save_acd = p_acd;
1665 p_acd = FALSE;
1666#endif
1667
Sean Dewar704966c2024-02-20 22:00:33 +01001668 (void)win_split_ins(0, WSP_TOP | WSP_FORCE_ROOM, auc_win, 0, NULL);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001669 (void)win_comp_pos(); // recompute window positions
1670 p_ea = save_ea;
1671#ifdef FEAT_AUTOCHDIR
1672 p_acd = save_acd;
1673#endif
1674 unblock_autocmds();
Bram Moolenaare76062c2022-11-28 18:51:43 +00001675 curwin = auc_win;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001676 }
1677 curbuf = buf;
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001678 aco->new_curwin_id = curwin->w_id;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001679 set_bufref(&aco->new_curbuf, curbuf);
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00001680
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00001681 aco->save_VIsual_active = VIsual_active;
zeertzjq5717ee32025-05-25 16:59:50 +02001682 if (!same_buffer)
1683 // disable the Visual area, position may be invalid in another buffer
1684 VIsual_active = FALSE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001685}
1686
1687/*
1688 * Cleanup after executing autocommands for a (hidden) buffer.
1689 * Restore the window as it was (if possible).
1690 */
1691 void
1692aucmd_restbuf(
1693 aco_save_T *aco) // structure holding saved values
1694{
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001695 int dummy;
1696 win_T *save_curwin;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001697
Bram Moolenaare76062c2022-11-28 18:51:43 +00001698 if (aco->use_aucmd_win_idx >= 0)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001699 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001700 win_T *awp = aucmd_win[aco->use_aucmd_win_idx].auc_win;
1701
Bram Moolenaare76062c2022-11-28 18:51:43 +00001702 // Find "awp", it can't be closed, but it may be in another tab
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001703 // page. Do not trigger autocommands here.
1704 block_autocmds();
Bram Moolenaare76062c2022-11-28 18:51:43 +00001705 if (curwin != awp)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001706 {
1707 tabpage_T *tp;
1708 win_T *wp;
1709
1710 FOR_ALL_TAB_WINDOWS(tp, wp)
1711 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001712 if (wp == awp)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001713 {
1714 if (tp != curtab)
1715 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaare76062c2022-11-28 18:51:43 +00001716 win_goto(awp);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001717 goto win_found;
1718 }
1719 }
1720 }
1721win_found:
zeertzjq46bdae02023-09-24 23:16:08 +02001722 --curbuf->b_nwindows;
orbitalcde8de02023-04-02 22:05:13 +01001723#ifdef FEAT_JOB_CHANNEL
zeertzjqa40c0bc2023-05-27 14:10:08 +01001724 int save_stop_insert_mode = stop_insert_mode;
orbitalcde8de02023-04-02 22:05:13 +01001725 // May need to stop Insert mode if we were in a prompt buffer.
1726 leaving_window(curwin);
Bram Moolenaar05a627c2023-04-09 22:01:31 +01001727 // Do not stop Insert mode when already in Insert mode before.
1728 if (aco->save_State & MODE_INSERT)
zeertzjqa40c0bc2023-05-27 14:10:08 +01001729 stop_insert_mode = save_stop_insert_mode;
orbitalcde8de02023-04-02 22:05:13 +01001730#endif
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001731 // Remove the window and frame from the tree of frames.
Sean Dewar704966c2024-02-20 22:00:33 +01001732 (void)winframe_remove(curwin, &dummy, NULL, NULL);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001733 win_remove(curwin, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00001734
1735 // The window is marked as not used, but it is not freed, it can be
1736 // used again.
Bram Moolenaare76062c2022-11-28 18:51:43 +00001737 aucmd_win[aco->use_aucmd_win_idx].auc_win_used = FALSE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001738 last_status(FALSE); // may need to remove last status line
1739
1740 if (!valid_tabpage_win(curtab))
1741 // no valid window in current tabpage
1742 close_tabpage(curtab);
1743
1744 restore_snapshot(SNAP_AUCMD_IDX, FALSE);
1745 (void)win_comp_pos(); // recompute window positions
1746 unblock_autocmds();
1747
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001748 save_curwin = win_find_by_id(aco->save_curwin_id);
1749 if (save_curwin != NULL)
1750 curwin = save_curwin;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001751 else
1752 // Hmm, original window disappeared. Just use the first one.
1753 curwin = firstwin;
Bram Moolenaarbdf931c2020-10-01 22:37:40 +02001754 curbuf = curwin->w_buffer;
1755#ifdef FEAT_JOB_CHANNEL
1756 // May need to restore insert mode for a prompt buffer.
1757 entering_window(curwin);
zeertzjqf2678472024-01-17 21:22:59 +01001758 if (bt_prompt(curbuf))
1759 curbuf->b_prompt_insert = aco->save_prompt_insert;
Bram Moolenaarbdf931c2020-10-01 22:37:40 +02001760#endif
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001761 prevwin = win_find_by_id(aco->save_prevwin_id);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001762#ifdef FEAT_EVAL
Bram Moolenaare76062c2022-11-28 18:51:43 +00001763 vars_clear(&awp->w_vars->dv_hashtab); // free all w: variables
1764 hash_init(&awp->w_vars->dv_hashtab); // re-use the hashtab
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001765#endif
zeertzjq9d956ee2024-04-07 18:16:10 +02001766 // If :lcd has been used in the autocommand window, correct current
1767 // directory before restoring tp_localdir and globaldir.
1768 if (awp->w_localdir != NULL)
1769 win_fix_current_dir();
1770 vim_free(curtab->tp_localdir);
1771 curtab->tp_localdir = aco->tp_localdir;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001772 vim_free(globaldir);
1773 globaldir = aco->globaldir;
1774
1775 // the buffer contents may have changed
zeertzjq49f05242023-02-04 10:58:34 +00001776 VIsual_active = aco->save_VIsual_active;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001777 check_cursor();
1778 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
1779 {
1780 curwin->w_topline = curbuf->b_ml.ml_line_count;
1781#ifdef FEAT_DIFF
1782 curwin->w_topfill = 0;
1783#endif
1784 }
1785#if defined(FEAT_GUI)
Bram Moolenaard2ff7052021-12-17 16:00:04 +00001786 if (gui.in_use)
1787 {
Bram Moolenaare76062c2022-11-28 18:51:43 +00001788 // Hide the scrollbars from the "awp" and update.
1789 gui_mch_enable_scrollbar(&awp->w_scrollbars[SBAR_LEFT], FALSE);
1790 gui_mch_enable_scrollbar(&awp->w_scrollbars[SBAR_RIGHT], FALSE);
Bram Moolenaard2ff7052021-12-17 16:00:04 +00001791 gui_may_update_scrollbars();
1792 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001793#endif
1794 }
1795 else
1796 {
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001797 // Restore curwin. Use the window ID, a window may have been closed
1798 // and the memory re-used for another one.
1799 save_curwin = win_find_by_id(aco->save_curwin_id);
1800 if (save_curwin != NULL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001801 {
1802 // Restore the buffer which was previously edited by curwin, if
1803 // it was changed, we are still the same window and the buffer is
1804 // valid.
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001805 if (curwin->w_id == aco->new_curwin_id
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001806 && curbuf != aco->new_curbuf.br_buf
1807 && bufref_valid(&aco->new_curbuf)
1808 && aco->new_curbuf.br_buf->b_ml.ml_mfp != NULL)
1809 {
1810# if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
1811 if (curwin->w_s == &curbuf->b_s)
1812 curwin->w_s = &aco->new_curbuf.br_buf->b_s;
1813# endif
1814 --curbuf->b_nwindows;
1815 curbuf = aco->new_curbuf.br_buf;
1816 curwin->w_buffer = curbuf;
1817 ++curbuf->b_nwindows;
1818 }
1819
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001820 curwin = save_curwin;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001821 curbuf = curwin->w_buffer;
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01001822 prevwin = win_find_by_id(aco->save_prevwin_id);
zeertzjq49f05242023-02-04 10:58:34 +00001823
Bram Moolenaar32aa1022019-11-02 22:54:41 +01001824 // In case the autocommand moves the cursor to a position that
1825 // does not exist in curbuf.
zeertzjq49f05242023-02-04 10:58:34 +00001826 VIsual_active = aco->save_VIsual_active;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001827 check_cursor();
1828 }
1829 }
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00001830
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00001831 VIsual_active = aco->save_VIsual_active;
zeertzjq49f05242023-02-04 10:58:34 +00001832 check_cursor(); // just in case lines got deleted
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00001833 if (VIsual_active)
1834 check_pos(curbuf, &VIsual);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001835}
1836
1837static int autocmd_nested = FALSE;
1838
1839/*
1840 * Execute autocommands for "event" and file name "fname".
1841 * Return TRUE if some commands were executed.
1842 */
1843 int
1844apply_autocmds(
1845 event_T event,
1846 char_u *fname, // NULL or empty means use actual file name
1847 char_u *fname_io, // fname to use for <afile> on cmdline
1848 int force, // when TRUE, ignore autocmd_busy
1849 buf_T *buf) // buffer for <abuf>
1850{
1851 return apply_autocmds_group(event, fname, fname_io, force,
1852 AUGROUP_ALL, buf, NULL);
1853}
1854
1855/*
1856 * Like apply_autocmds(), but with extra "eap" argument. This takes care of
1857 * setting v:filearg.
1858 */
1859 int
1860apply_autocmds_exarg(
1861 event_T event,
1862 char_u *fname,
1863 char_u *fname_io,
1864 int force,
1865 buf_T *buf,
1866 exarg_T *eap)
1867{
1868 return apply_autocmds_group(event, fname, fname_io, force,
1869 AUGROUP_ALL, buf, eap);
1870}
1871
1872/*
1873 * Like apply_autocmds(), but handles the caller's retval. If the script
1874 * processing is being aborted or if retval is FAIL when inside a try
1875 * conditional, no autocommands are executed. If otherwise the autocommands
1876 * cause the script to be aborted, retval is set to FAIL.
1877 */
1878 int
1879apply_autocmds_retval(
1880 event_T event,
1881 char_u *fname, // NULL or empty means use actual file name
1882 char_u *fname_io, // fname to use for <afile> on cmdline
1883 int force, // when TRUE, ignore autocmd_busy
1884 buf_T *buf, // buffer for <abuf>
1885 int *retval) // pointer to caller's retval
1886{
1887 int did_cmd;
1888
1889#ifdef FEAT_EVAL
1890 if (should_abort(*retval))
1891 return FALSE;
1892#endif
1893
1894 did_cmd = apply_autocmds_group(event, fname, fname_io, force,
1895 AUGROUP_ALL, buf, NULL);
1896 if (did_cmd
1897#ifdef FEAT_EVAL
1898 && aborting()
1899#endif
1900 )
1901 *retval = FAIL;
1902 return did_cmd;
1903}
1904
1905/*
1906 * Return TRUE when there is a CursorHold autocommand defined.
1907 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001908 static int
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001909has_cursorhold(void)
1910{
Bram Moolenaar24959102022-05-07 20:01:16 +01001911 return (first_autopat[(int)(get_real_state() == MODE_NORMAL_BUSY
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001912 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL);
1913}
1914
1915/*
1916 * Return TRUE if the CursorHold event can be triggered.
1917 */
1918 int
1919trigger_cursorhold(void)
1920{
1921 int state;
1922
1923 if (!did_cursorhold
1924 && has_cursorhold()
1925 && reg_recording == 0
1926 && typebuf.tb_len == 0
Bram Moolenaare2c453d2019-08-21 14:37:09 +02001927 && !ins_compl_active())
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001928 {
1929 state = get_real_state();
Bram Moolenaar24959102022-05-07 20:01:16 +01001930 if (state == MODE_NORMAL_BUSY || (state & MODE_INSERT) != 0)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001931 return TRUE;
1932 }
1933 return FALSE;
1934}
1935
1936/*
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001937 * Return TRUE when there is a WinResized autocommand defined.
1938 */
1939 int
1940has_winresized(void)
1941{
1942 return (first_autopat[(int)EVENT_WINRESIZED] != NULL);
1943}
1944
1945/*
LemonBoy09371822022-04-08 15:18:45 +01001946 * Return TRUE when there is a WinScrolled autocommand defined.
1947 */
1948 int
1949has_winscrolled(void)
1950{
1951 return (first_autopat[(int)EVENT_WINSCROLLED] != NULL);
1952}
1953
1954/*
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001955 * Return TRUE when there is a CursorMoved autocommand defined.
1956 */
1957 int
1958has_cursormoved(void)
1959{
1960 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL);
1961}
1962
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001963/*
1964 * Return TRUE when there is a CursorMovedI autocommand defined.
1965 */
1966 int
1967has_cursormovedI(void)
1968{
1969 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL);
1970}
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001971
1972/*
1973 * Return TRUE when there is a TextChanged autocommand defined.
1974 */
1975 int
1976has_textchanged(void)
1977{
1978 return (first_autopat[(int)EVENT_TEXTCHANGED] != NULL);
1979}
1980
1981/*
1982 * Return TRUE when there is a TextChangedI autocommand defined.
1983 */
1984 int
1985has_textchangedI(void)
1986{
1987 return (first_autopat[(int)EVENT_TEXTCHANGEDI] != NULL);
1988}
1989
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001990/*
1991 * Return TRUE when there is a TextChangedP autocommand defined.
1992 */
1993 int
1994has_textchangedP(void)
1995{
1996 return (first_autopat[(int)EVENT_TEXTCHANGEDP] != NULL);
1997}
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01001998
1999/*
2000 * Return TRUE when there is an InsertCharPre autocommand defined.
2001 */
2002 int
2003has_insertcharpre(void)
2004{
2005 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL);
2006}
2007
2008/*
Shougo Matsushita83678842024-07-11 22:05:12 +02002009 * Return TRUE when there is an KeyInputPre autocommand defined.
2010 */
2011 int
2012has_keyinputpre(void)
2013{
2014 return (first_autopat[(int)EVENT_KEYINPUTPRE] != NULL);
2015}
2016
2017/*
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002018 * Return TRUE when there is an CmdUndefined autocommand defined.
2019 */
2020 int
2021has_cmdundefined(void)
2022{
2023 return (first_autopat[(int)EVENT_CMDUNDEFINED] != NULL);
2024}
2025
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002026#if defined(FEAT_EVAL) || defined(PROTO)
2027/*
2028 * Return TRUE when there is a TextYankPost autocommand defined.
2029 */
2030 int
2031has_textyankpost(void)
2032{
2033 return (first_autopat[(int)EVENT_TEXTYANKPOST] != NULL);
2034}
2035#endif
2036
Bram Moolenaard7f246c2019-04-08 18:15:41 +02002037#if defined(FEAT_EVAL) || defined(PROTO)
2038/*
2039 * Return TRUE when there is a CompleteChanged autocommand defined.
2040 */
2041 int
2042has_completechanged(void)
2043{
2044 return (first_autopat[(int)EVENT_COMPLETECHANGED] != NULL);
2045}
2046#endif
2047
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002048#if defined(FEAT_EVAL) || defined(PROTO)
2049/*
2050 * Return TRUE when there is a ModeChanged autocommand defined.
2051 */
2052 int
2053has_modechanged(void)
2054{
2055 return (first_autopat[(int)EVENT_MODECHANGED] != NULL);
2056}
2057#endif
2058
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002059/*
2060 * Execute autocommands for "event" and file name "fname".
2061 * Return TRUE if some commands were executed.
2062 */
2063 static int
2064apply_autocmds_group(
2065 event_T event,
2066 char_u *fname, // NULL or empty means use actual file name
2067 char_u *fname_io, // fname to use for <afile> on cmdline, NULL means
2068 // use fname
2069 int force, // when TRUE, ignore autocmd_busy
2070 int group, // group ID, or AUGROUP_ALL
2071 buf_T *buf, // buffer for <abuf>
2072 exarg_T *eap UNUSED) // command arguments
2073{
2074 char_u *sfname = NULL; // short file name
2075 char_u *tail;
2076 int save_changed;
2077 buf_T *old_curbuf;
2078 int retval = FALSE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002079 char_u *save_autocmd_fname;
2080 int save_autocmd_fname_full;
2081 int save_autocmd_bufnr;
2082 char_u *save_autocmd_match;
2083 int save_autocmd_busy;
2084 int save_autocmd_nested;
2085 static int nesting = 0;
LemonBoyeca7c602022-04-14 15:39:43 +01002086 AutoPatCmd_T patcmd;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002087 AutoPat *ap;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002088 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002089#ifdef FEAT_EVAL
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002090 funccal_entry_T funccal_entry;
2091 char_u *save_cmdarg;
2092 long save_cmdbang;
2093#endif
2094 static int filechangeshell_busy = FALSE;
2095#ifdef FEAT_PROFILE
2096 proftime_T wait_time;
2097#endif
2098 int did_save_redobuff = FALSE;
2099 save_redo_T save_redo;
2100 int save_KeyTyped = KeyTyped;
ichizok7e5fe382023-04-15 13:17:50 +01002101 ESTACK_CHECK_DECLARATION;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002102
2103 /*
2104 * Quickly return if there are no autocommands for this event or
2105 * autocommands are blocked.
2106 */
2107 if (event == NUM_EVENTS || first_autopat[(int)event] == NULL
2108 || autocmd_blocked > 0)
2109 goto BYPASS_AU;
2110
2111 /*
2112 * When autocommands are busy, new autocommands are only executed when
2113 * explicitly enabled with the "nested" flag.
2114 */
2115 if (autocmd_busy && !(force || autocmd_nested))
2116 goto BYPASS_AU;
2117
2118#ifdef FEAT_EVAL
2119 /*
2120 * Quickly return when immediately aborting on error, or when an interrupt
2121 * occurred or an exception was thrown but not caught.
2122 */
2123 if (aborting())
2124 goto BYPASS_AU;
2125#endif
2126
2127 /*
2128 * FileChangedShell never nests, because it can create an endless loop.
2129 */
2130 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL
2131 || event == EVENT_FILECHANGEDSHELLPOST))
2132 goto BYPASS_AU;
2133
2134 /*
2135 * Ignore events in 'eventignore'.
2136 */
Luuk van Baalb7147f82025-02-08 18:52:39 +01002137 if (event_ignored(event, p_ei))
2138 goto BYPASS_AU;
2139
Luuk van Baalb7147f82025-02-08 18:52:39 +01002140 int win_ignore = FALSE;
2141 // If event is allowed in 'eventignorewin', check if curwin or all windows
2142 // into "buf" are ignoring the event.
2143 if (buf == curbuf && event_tab[event].key <= 0)
2144 win_ignore = event_ignored(event, curwin->w_p_eiw);
Sean Deward4110e02025-05-11 13:45:21 +02002145 else if (buf != NULL && event_tab[event].key <= 0 && buf->b_nwindows > 0)
2146 {
2147 tabpage_T *tp;
2148 win_T *wp;
2149
2150 win_ignore = TRUE;
2151 FOR_ALL_TAB_WINDOWS(tp, wp)
2152 if (wp->w_buffer == buf && !event_ignored(event, wp->w_p_eiw))
2153 {
2154 win_ignore = FALSE;
2155 break;
2156 }
2157 }
Luuk van Baalb7147f82025-02-08 18:52:39 +01002158 if (win_ignore)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002159 goto BYPASS_AU;
2160
2161 /*
2162 * Allow nesting of autocommands, but restrict the depth, because it's
2163 * possible to create an endless loop.
2164 */
2165 if (nesting == 10)
2166 {
Bram Moolenaar6d057012021-12-31 18:49:43 +00002167 emsg(_(e_autocommand_nesting_too_deep));
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002168 goto BYPASS_AU;
2169 }
2170
2171 /*
2172 * Check if these autocommands are disabled. Used when doing ":all" or
2173 * ":ball".
2174 */
2175 if ( (autocmd_no_enter
2176 && (event == EVENT_WINENTER || event == EVENT_BUFENTER))
2177 || (autocmd_no_leave
2178 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE)))
2179 goto BYPASS_AU;
2180
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002181 if (event == EVENT_CMDLINECHANGED)
2182 ++aucmd_cmdline_changed_count;
2183
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002184 /*
2185 * Save the autocmd_* variables and info about the current buffer.
2186 */
2187 save_autocmd_fname = autocmd_fname;
2188 save_autocmd_fname_full = autocmd_fname_full;
2189 save_autocmd_bufnr = autocmd_bufnr;
2190 save_autocmd_match = autocmd_match;
2191 save_autocmd_busy = autocmd_busy;
2192 save_autocmd_nested = autocmd_nested;
2193 save_changed = curbuf->b_changed;
2194 old_curbuf = curbuf;
2195
2196 /*
2197 * Set the file name to be used for <afile>.
2198 * Make a copy to avoid that changing a buffer name or directory makes it
2199 * invalid.
2200 */
2201 if (fname_io == NULL)
2202 {
2203 if (event == EVENT_COLORSCHEME || event == EVENT_COLORSCHEMEPRE
Bram Moolenaarbb393d82022-12-09 12:21:50 +00002204 || event == EVENT_OPTIONSET
Danek Duvalld7d56032024-01-14 20:19:59 +01002205 || event == EVENT_MODECHANGED
2206 || event == EVENT_TERMRESPONSEALL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002207 autocmd_fname = NULL;
2208 else if (fname != NULL && !ends_excmd(*fname))
2209 autocmd_fname = fname;
2210 else if (buf != NULL)
2211 autocmd_fname = buf->b_ffname;
2212 else
2213 autocmd_fname = NULL;
2214 }
2215 else
2216 autocmd_fname = fname_io;
2217 if (autocmd_fname != NULL)
2218 autocmd_fname = vim_strsave(autocmd_fname);
2219 autocmd_fname_full = FALSE; // call FullName_save() later
2220
2221 /*
2222 * Set the buffer number to be used for <abuf>.
2223 */
2224 if (buf == NULL)
2225 autocmd_bufnr = 0;
2226 else
2227 autocmd_bufnr = buf->b_fnum;
2228
2229 /*
2230 * When the file name is NULL or empty, use the file name of buffer "buf".
2231 * Always use the full path of the file name to match with, in case
2232 * "allow_dirs" is set.
2233 */
2234 if (fname == NULL || *fname == NUL)
2235 {
2236 if (buf == NULL)
2237 fname = NULL;
2238 else
2239 {
2240#ifdef FEAT_SYN_HL
2241 if (event == EVENT_SYNTAX)
2242 fname = buf->b_p_syn;
2243 else
2244#endif
2245 if (event == EVENT_FILETYPE)
2246 fname = buf->b_p_ft;
2247 else
2248 {
2249 if (buf->b_sfname != NULL)
2250 sfname = vim_strsave(buf->b_sfname);
2251 fname = buf->b_ffname;
2252 }
2253 }
2254 if (fname == NULL)
2255 fname = (char_u *)"";
2256 fname = vim_strsave(fname); // make a copy, so we can change it
2257 }
2258 else
2259 {
2260 sfname = vim_strsave(fname);
2261 // Don't try expanding FileType, Syntax, FuncUndefined, WindowID,
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002262 // ColorScheme, QuickFixCmd*, DirChanged and similar.
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002263 if (event == EVENT_FILETYPE
2264 || event == EVENT_SYNTAX
2265 || event == EVENT_CMDLINECHANGED
2266 || event == EVENT_CMDLINEENTER
Girish Palya92f68e22025-04-21 11:12:41 +02002267 || event == EVENT_CMDLINELEAVEPRE
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002268 || event == EVENT_CMDLINELEAVE
Shougo Matsushitad0952142024-06-20 22:05:16 +02002269 || event == EVENT_CURSORMOVEDC
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002270 || event == EVENT_CMDWINENTER
2271 || event == EVENT_CMDWINLEAVE
2272 || event == EVENT_CMDUNDEFINED
2273 || event == EVENT_FUNCUNDEFINED
Shougo Matsushita83678842024-07-11 22:05:12 +02002274 || event == EVENT_KEYINPUTPRE
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002275 || event == EVENT_REMOTEREPLY
2276 || event == EVENT_SPELLFILEMISSING
2277 || event == EVENT_QUICKFIXCMDPRE
2278 || event == EVENT_COLORSCHEME
2279 || event == EVENT_COLORSCHEMEPRE
2280 || event == EVENT_OPTIONSET
2281 || event == EVENT_QUICKFIXCMDPOST
=?UTF-8?q?Magnus=20Gro=C3=9F?=f1e88762021-09-12 13:39:55 +02002282 || event == EVENT_DIRCHANGED
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002283 || event == EVENT_DIRCHANGEDPRE
naohiro ono23beefe2021-11-13 12:38:49 +00002284 || event == EVENT_MODECHANGED
zeertzjqc601d982022-10-10 13:46:15 +01002285 || event == EVENT_MENUPOPUP
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002286 || event == EVENT_USER
LemonBoy09371822022-04-08 15:18:45 +01002287 || event == EVENT_WINCLOSED
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00002288 || event == EVENT_WINRESIZED
Danek Duvalld7d56032024-01-14 20:19:59 +01002289 || event == EVENT_WINSCROLLED
2290 || event == EVENT_TERMRESPONSEALL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002291 {
2292 fname = vim_strsave(fname);
2293 autocmd_fname_full = TRUE; // don't expand it later
2294 }
2295 else
2296 fname = FullName_save(fname, FALSE);
2297 }
2298 if (fname == NULL) // out of memory
2299 {
2300 vim_free(sfname);
2301 retval = FALSE;
2302 goto BYPASS_AU;
2303 }
2304
2305#ifdef BACKSLASH_IN_FILENAME
2306 /*
2307 * Replace all backslashes with forward slashes. This makes the
2308 * autocommand patterns portable between Unix and MS-DOS.
2309 */
2310 if (sfname != NULL)
2311 forward_slash(sfname);
2312 forward_slash(fname);
2313#endif
2314
2315#ifdef VMS
2316 // remove version for correct match
2317 if (sfname != NULL)
2318 vms_remove_version(sfname);
2319 vms_remove_version(fname);
2320#endif
2321
2322 /*
2323 * Set the name to be used for <amatch>.
2324 */
2325 autocmd_match = fname;
2326
2327
2328 // Don't redraw while doing autocommands.
2329 ++RedrawingDisabled;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002330
2331 // name and lnum are filled in later
2332 estack_push(ETYPE_AUCMD, NULL, 0);
ichizok7e5fe382023-04-15 13:17:50 +01002333 ESTACK_CHECK_SETUP;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002334
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002335 save_current_sctx = current_sctx;
2336
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002337#ifdef FEAT_EVAL
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002338# ifdef FEAT_PROFILE
2339 if (do_profiling == PROF_YES)
2340 prof_child_enter(&wait_time); // doesn't count for the caller itself
2341# endif
2342
2343 // Don't use local function variables, if called from a function.
2344 save_funccal(&funccal_entry);
2345#endif
2346
2347 /*
2348 * When starting to execute autocommands, save the search patterns.
2349 */
2350 if (!autocmd_busy)
2351 {
2352 save_search_patterns();
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002353 if (!ins_compl_active())
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002354 {
2355 saveRedobuff(&save_redo);
2356 did_save_redobuff = TRUE;
2357 }
zeertzjq5bf6c212024-03-31 18:41:27 +02002358 curbuf->b_did_filetype = curbuf->b_keep_filetype;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002359 }
2360
2361 /*
2362 * Note that we are applying autocmds. Some commands need to know.
2363 */
2364 autocmd_busy = TRUE;
2365 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL);
2366 ++nesting; // see matching decrement below
2367
2368 // Remember that FileType was triggered. Used for did_filetype().
2369 if (event == EVENT_FILETYPE)
zeertzjq5bf6c212024-03-31 18:41:27 +02002370 curbuf->b_did_filetype = TRUE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002371
2372 tail = gettail(fname);
2373
2374 // Find first autocommand that matches
LemonBoyeca7c602022-04-14 15:39:43 +01002375 CLEAR_FIELD(patcmd);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002376 patcmd.curpat = first_autopat[(int)event];
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002377 patcmd.group = group;
2378 patcmd.fname = fname;
2379 patcmd.sfname = sfname;
2380 patcmd.tail = tail;
2381 patcmd.event = event;
2382 patcmd.arg_bufnr = autocmd_bufnr;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002383 auto_next_pat(&patcmd, FALSE);
2384
2385 // found one, start executing the autocommands
2386 if (patcmd.curpat != NULL)
2387 {
2388 // add to active_apc_list
2389 patcmd.next = active_apc_list;
2390 active_apc_list = &patcmd;
2391
2392#ifdef FEAT_EVAL
2393 // set v:cmdarg (only when there is a matching pattern)
2394 save_cmdbang = (long)get_vim_var_nr(VV_CMDBANG);
2395 if (eap != NULL)
2396 {
2397 save_cmdarg = set_cmdarg(eap, NULL);
2398 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit);
2399 }
2400 else
2401 save_cmdarg = NULL; // avoid gcc warning
2402#endif
2403 retval = TRUE;
2404 // mark the last pattern, to avoid an endless loop when more patterns
2405 // are added when executing autocommands
2406 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next)
2407 ap->last = FALSE;
2408 ap->last = TRUE;
Bram Moolenaara68e5952019-04-25 22:22:01 +02002409
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002410 // Make sure cursor and topline are valid. The first time the current
2411 // values are saved, restored by reset_lnums(). When nested only the
2412 // values are corrected when needed.
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002413 if (nesting == 1)
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002414 check_lnums(TRUE);
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002415 else
2416 check_lnums_nested(TRUE);
Bram Moolenaara68e5952019-04-25 22:22:01 +02002417
Christian Brabandt590aae32023-06-25 22:34:22 +01002418 int save_did_emsg = did_emsg;
2419 int save_ex_pressedreturn = get_pressedreturn();
ichizokc3f91c02021-12-17 09:44:33 +00002420
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002421 do_cmdline(NULL, getnextac, (void *)&patcmd,
2422 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
Bram Moolenaara68e5952019-04-25 22:22:01 +02002423
ichizokc3f91c02021-12-17 09:44:33 +00002424 did_emsg += save_did_emsg;
Christian Brabandt590aae32023-06-25 22:34:22 +01002425 set_pressedreturn(save_ex_pressedreturn);
ichizokc3f91c02021-12-17 09:44:33 +00002426
Bram Moolenaar1e6bbfb2021-04-03 13:19:26 +02002427 if (nesting == 1)
2428 // restore cursor and topline, unless they were changed
2429 reset_lnums();
Bram Moolenaara68e5952019-04-25 22:22:01 +02002430
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002431#ifdef FEAT_EVAL
2432 if (eap != NULL)
2433 {
2434 (void)set_cmdarg(NULL, save_cmdarg);
2435 set_vim_var_nr(VV_CMDBANG, save_cmdbang);
2436 }
2437#endif
2438 // delete from active_apc_list
2439 if (active_apc_list == &patcmd) // just in case
2440 active_apc_list = patcmd.next;
2441 }
2442
Bram Moolenaar79cdf022023-05-20 14:07:00 +01002443 if (RedrawingDisabled > 0)
2444 --RedrawingDisabled;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002445 autocmd_busy = save_autocmd_busy;
2446 filechangeshell_busy = FALSE;
2447 autocmd_nested = save_autocmd_nested;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002448 vim_free(SOURCING_NAME);
ichizok7e5fe382023-04-15 13:17:50 +01002449 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002450 estack_pop();
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002451 vim_free(autocmd_fname);
2452 autocmd_fname = save_autocmd_fname;
2453 autocmd_fname_full = save_autocmd_fname_full;
2454 autocmd_bufnr = save_autocmd_bufnr;
2455 autocmd_match = save_autocmd_match;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002456 current_sctx = save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01002457#ifdef FEAT_EVAL
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002458 restore_funccal();
2459# ifdef FEAT_PROFILE
2460 if (do_profiling == PROF_YES)
2461 prof_child_exit(&wait_time);
2462# endif
2463#endif
2464 KeyTyped = save_KeyTyped;
2465 vim_free(fname);
2466 vim_free(sfname);
2467 --nesting; // see matching increment above
2468
2469 /*
2470 * When stopping to execute autocommands, restore the search patterns and
2471 * the redo buffer. Free any buffers in the au_pending_free_buf list and
2472 * free any windows in the au_pending_free_win list.
2473 */
2474 if (!autocmd_busy)
2475 {
2476 restore_search_patterns();
2477 if (did_save_redobuff)
2478 restoreRedobuff(&save_redo);
zeertzjq5bf6c212024-03-31 18:41:27 +02002479 curbuf->b_did_filetype = FALSE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002480 while (au_pending_free_buf != NULL)
2481 {
2482 buf_T *b = au_pending_free_buf->b_next;
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002483
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002484 vim_free(au_pending_free_buf);
2485 au_pending_free_buf = b;
2486 }
2487 while (au_pending_free_win != NULL)
2488 {
2489 win_T *w = au_pending_free_win->w_next;
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002490
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002491 vim_free(au_pending_free_win);
2492 au_pending_free_win = w;
2493 }
2494 }
2495
2496 /*
2497 * Some events don't set or reset the Changed flag.
2498 * Check if still in the same buffer!
2499 */
2500 if (curbuf == old_curbuf
2501 && (event == EVENT_BUFREADPOST
2502 || event == EVENT_BUFWRITEPOST
2503 || event == EVENT_FILEAPPENDPOST
2504 || event == EVENT_VIMLEAVE
2505 || event == EVENT_VIMLEAVEPRE))
2506 {
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002507 if (curbuf->b_changed != save_changed)
2508 need_maketitle = TRUE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002509 curbuf->b_changed = save_changed;
2510 }
2511
2512 au_cleanup(); // may really delete removed patterns/commands now
2513
2514BYPASS_AU:
2515 // When wiping out a buffer make sure all its buffer-local autocommands
2516 // are deleted.
2517 if (event == EVENT_BUFWIPEOUT && buf != NULL)
2518 aubuflocal_remove(buf);
2519
2520 if (retval == OK && event == EVENT_FILETYPE)
zeertzjq5bf6c212024-03-31 18:41:27 +02002521 curbuf->b_au_did_filetype = TRUE;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002522
2523 return retval;
2524}
2525
2526# ifdef FEAT_EVAL
2527static char_u *old_termresponse = NULL;
Danek Duvalld7d56032024-01-14 20:19:59 +01002528static char_u *old_termu7resp = NULL;
2529static char_u *old_termblinkresp = NULL;
2530static char_u *old_termrbgresp = NULL;
2531static char_u *old_termrfgresp = NULL;
2532static char_u *old_termstyleresp = NULL;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002533# endif
2534
2535/*
2536 * Block triggering autocommands until unblock_autocmd() is called.
2537 * Can be used recursively, so long as it's symmetric.
2538 */
2539 void
2540block_autocmds(void)
2541{
2542# ifdef FEAT_EVAL
2543 // Remember the value of v:termresponse.
2544 if (autocmd_blocked == 0)
Danek Duvalld7d56032024-01-14 20:19:59 +01002545 {
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002546 old_termresponse = get_vim_var_str(VV_TERMRESPONSE);
Danek Duvalld7d56032024-01-14 20:19:59 +01002547 old_termu7resp = get_vim_var_str(VV_TERMU7RESP);
2548 old_termblinkresp = get_vim_var_str(VV_TERMBLINKRESP);
2549 old_termrbgresp = get_vim_var_str(VV_TERMRBGRESP);
2550 old_termrfgresp = get_vim_var_str(VV_TERMRFGRESP);
2551 old_termstyleresp = get_vim_var_str(VV_TERMSTYLERESP);
2552 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002553# endif
2554 ++autocmd_blocked;
2555}
2556
2557 void
2558unblock_autocmds(void)
2559{
2560 --autocmd_blocked;
2561
2562# ifdef FEAT_EVAL
Danek Duvalld7d56032024-01-14 20:19:59 +01002563 // When v:termresponse, etc, were set while autocommands were blocked,
2564 // trigger the autocommands now. Esp. useful when executing a shell
2565 // command during startup (vimdiff).
2566 if (autocmd_blocked == 0)
2567 {
2568 if (get_vim_var_str(VV_TERMRESPONSE) != old_termresponse)
2569 {
2570 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf);
2571 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"version", NULL, FALSE, curbuf);
2572 }
2573 if (get_vim_var_str(VV_TERMU7RESP) != old_termu7resp)
2574 {
2575 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"ambiguouswidth", NULL, FALSE, curbuf);
2576 }
2577 if (get_vim_var_str(VV_TERMBLINKRESP) != old_termblinkresp)
2578 {
2579 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"cursorblink", NULL, FALSE, curbuf);
2580 }
2581 if (get_vim_var_str(VV_TERMRBGRESP) != old_termrbgresp)
2582 {
2583 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"background", NULL, FALSE, curbuf);
2584 }
2585 if (get_vim_var_str(VV_TERMRFGRESP) != old_termrfgresp)
2586 {
2587 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"foreground", NULL, FALSE, curbuf);
2588 }
2589 if (get_vim_var_str(VV_TERMSTYLERESP) != old_termstyleresp)
2590 {
2591 apply_autocmds(EVENT_TERMRESPONSEALL, (char_u *)"cursorshape", NULL, FALSE, curbuf);
2592 }
2593 }
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002594# endif
2595}
2596
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002597 int
2598is_autocmd_blocked(void)
2599{
2600 return autocmd_blocked != 0;
2601}
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002602
2603/*
2604 * Find next autocommand pattern that matches.
2605 */
2606 static void
2607auto_next_pat(
LemonBoyeca7c602022-04-14 15:39:43 +01002608 AutoPatCmd_T *apc,
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002609 int stop_at_last) // stop when 'last' flag is set
2610{
2611 AutoPat *ap;
2612 AutoCmd *cp;
2613 char_u *name;
2614 char *s;
LemonBoyeca7c602022-04-14 15:39:43 +01002615 estack_T *entry;
2616 char_u *namep;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002617
LemonBoyeca7c602022-04-14 15:39:43 +01002618 entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1;
2619
2620 // Clear the exestack entry for this ETYPE_AUCMD entry.
2621 VIM_CLEAR(entry->es_name);
2622 entry->es_info.aucmd = NULL;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002623
2624 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next)
2625 {
2626 apc->curpat = NULL;
2627
2628 // Only use a pattern when it has not been removed, has commands and
2629 // the group matches. For buffer-local autocommands only check the
2630 // buffer number.
2631 if (ap->pat != NULL && ap->cmds != NULL
2632 && (apc->group == AUGROUP_ALL || apc->group == ap->group))
2633 {
2634 // execution-condition
2635 if (ap->buflocal_nr == 0
2636 ? (match_file_pat(NULL, &ap->reg_prog, apc->fname,
2637 apc->sfname, apc->tail, ap->allow_dirs))
2638 : ap->buflocal_nr == apc->arg_bufnr)
2639 {
2640 name = event_nr2name(apc->event);
2641 s = _("%s Autocommands for \"%s\"");
LemonBoyeca7c602022-04-14 15:39:43 +01002642 namep = alloc(STRLEN(s) + STRLEN(name) + ap->patlen + 1);
2643 if (namep != NULL)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002644 {
LemonBoyeca7c602022-04-14 15:39:43 +01002645 sprintf((char *)namep, s, (char *)name, (char *)ap->pat);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002646 if (p_verbose >= 8)
2647 {
2648 verbose_enter();
LemonBoyeca7c602022-04-14 15:39:43 +01002649 smsg(_("Executing %s"), namep);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002650 verbose_leave();
2651 }
2652 }
2653
LemonBoyeca7c602022-04-14 15:39:43 +01002654 // Update the exestack entry for this autocmd.
2655 entry->es_name = namep;
2656 entry->es_info.aucmd = apc;
2657
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002658 apc->curpat = ap;
2659 apc->nextcmd = ap->cmds;
2660 // mark last command
2661 for (cp = ap->cmds; cp->next != NULL; cp = cp->next)
2662 cp->last = FALSE;
2663 cp->last = TRUE;
2664 }
2665 line_breakcheck();
2666 if (apc->curpat != NULL) // found a match
2667 break;
2668 }
2669 if (stop_at_last && ap->last)
2670 break;
2671 }
2672}
2673
Dominique Pellee764d1b2023-03-12 21:20:59 +00002674#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002675/*
LemonBoyeca7c602022-04-14 15:39:43 +01002676 * Get the script context where autocommand "acp" is defined.
2677 */
2678 sctx_T *
2679acp_script_ctx(AutoPatCmd_T *acp)
2680{
2681 return &acp->script_ctx;
2682}
Dominique Pellee764d1b2023-03-12 21:20:59 +00002683#endif
LemonBoyeca7c602022-04-14 15:39:43 +01002684
2685/*
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002686 * Get next autocommand command.
2687 * Called by do_cmdline() to get the next line for ":if".
2688 * Returns allocated string, or NULL for end of autocommands.
2689 */
2690 char_u *
Bram Moolenaar66250c92020-08-20 15:02:42 +02002691getnextac(
2692 int c UNUSED,
2693 void *cookie,
2694 int indent UNUSED,
2695 getline_opt_T options UNUSED)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002696{
LemonBoyeca7c602022-04-14 15:39:43 +01002697 AutoPatCmd_T *acp = (AutoPatCmd_T *)cookie;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002698 char_u *retval;
2699 AutoCmd *ac;
2700
2701 // Can be called again after returning the last line.
2702 if (acp->curpat == NULL)
2703 return NULL;
2704
2705 // repeat until we find an autocommand to execute
2706 for (;;)
2707 {
2708 // skip removed commands
2709 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL)
2710 if (acp->nextcmd->last)
2711 acp->nextcmd = NULL;
2712 else
2713 acp->nextcmd = acp->nextcmd->next;
2714
2715 if (acp->nextcmd != NULL)
2716 break;
2717
2718 // at end of commands, find next pattern that matches
2719 if (acp->curpat->last)
2720 acp->curpat = NULL;
2721 else
2722 acp->curpat = acp->curpat->next;
2723 if (acp->curpat != NULL)
2724 auto_next_pat(acp, TRUE);
2725 if (acp->curpat == NULL)
2726 return NULL;
2727 }
2728
2729 ac = acp->nextcmd;
2730
2731 if (p_verbose >= 9)
2732 {
2733 verbose_enter_scroll();
2734 smsg(_("autocommand %s"), ac->cmd);
2735 msg_puts("\n"); // don't overwrite this either
2736 verbose_leave_scroll();
2737 }
2738 retval = vim_strsave(ac->cmd);
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002739 // Remove one-shot ("once") autocmd in anticipation of its execution.
2740 if (ac->once)
2741 au_del_cmd(ac);
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002742 autocmd_nested = ac->nested;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002743 current_sctx = ac->script_ctx;
LemonBoyeca7c602022-04-14 15:39:43 +01002744 acp->script_ctx = current_sctx;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002745 if (ac->last)
2746 acp->nextcmd = NULL;
2747 else
2748 acp->nextcmd = ac->next;
2749 return retval;
2750}
2751
2752/*
2753 * Return TRUE if there is a matching autocommand for "fname".
2754 * To account for buffer-local autocommands, function needs to know
2755 * in which buffer the file will be opened.
2756 */
2757 int
2758has_autocmd(event_T event, char_u *sfname, buf_T *buf)
2759{
2760 AutoPat *ap;
2761 char_u *fname;
2762 char_u *tail = gettail(sfname);
2763 int retval = FALSE;
2764
2765 fname = FullName_save(sfname, FALSE);
2766 if (fname == NULL)
2767 return FALSE;
2768
2769#ifdef BACKSLASH_IN_FILENAME
2770 /*
2771 * Replace all backslashes with forward slashes. This makes the
2772 * autocommand patterns portable between Unix and MS-DOS.
2773 */
2774 sfname = vim_strsave(sfname);
2775 if (sfname != NULL)
2776 forward_slash(sfname);
2777 forward_slash(fname);
2778#endif
2779
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002780 FOR_ALL_AUTOCMD_PATTERNS(event, ap)
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002781 if (ap->pat != NULL && ap->cmds != NULL
2782 && (ap->buflocal_nr == 0
2783 ? match_file_pat(NULL, &ap->reg_prog,
2784 fname, sfname, tail, ap->allow_dirs)
2785 : buf != NULL && ap->buflocal_nr == buf->b_fnum
2786 ))
2787 {
2788 retval = TRUE;
2789 break;
2790 }
2791
2792 vim_free(fname);
2793#ifdef BACKSLASH_IN_FILENAME
2794 vim_free(sfname);
2795#endif
2796
2797 return retval;
2798}
2799
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002800/*
2801 * Function given to ExpandGeneric() to obtain the list of autocommand group
2802 * names.
2803 */
2804 char_u *
2805get_augroup_name(expand_T *xp UNUSED, int idx)
2806{
2807 if (idx == augroups.ga_len) // add "END" add the end
2808 return (char_u *)"END";
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01002809 if (idx < 0 || idx >= augroups.ga_len) // end of list
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002810 return NULL;
2811 if (AUGROUP_NAME(idx) == NULL || AUGROUP_NAME(idx) == get_deleted_augroup())
2812 // skip deleted entries
2813 return (char_u *)"";
2814 return AUGROUP_NAME(idx); // return a name
2815}
2816
2817static int include_groups = FALSE;
2818
2819 char_u *
2820set_context_in_autocmd(
2821 expand_T *xp,
2822 char_u *arg,
2823 int doautocmd) // TRUE for :doauto*, FALSE for :autocmd
2824{
2825 char_u *p;
2826 int group;
2827
2828 // check for a group name, skip it if present
2829 include_groups = FALSE;
2830 p = arg;
2831 group = au_get_grouparg(&arg);
2832 if (group == AUGROUP_ERROR)
2833 return NULL;
2834 // If there only is a group name that's what we expand.
2835 if (*arg == NUL && group != AUGROUP_ALL && !VIM_ISWHITE(arg[-1]))
2836 {
2837 arg = p;
2838 group = AUGROUP_ALL;
2839 }
2840
2841 // skip over event name
2842 for (p = arg; *p != NUL && !VIM_ISWHITE(*p); ++p)
2843 if (*p == ',')
2844 arg = p + 1;
2845 if (*p == NUL)
2846 {
2847 if (group == AUGROUP_ALL)
2848 include_groups = TRUE;
2849 xp->xp_context = EXPAND_EVENTS; // expand event name
2850 xp->xp_pattern = arg;
2851 return NULL;
2852 }
2853
2854 // skip over pattern
2855 arg = skipwhite(p);
2856 while (*arg && (!VIM_ISWHITE(*arg) || arg[-1] == '\\'))
2857 arg++;
2858 if (*arg)
2859 return arg; // expand (next) command
2860
2861 if (doautocmd)
2862 xp->xp_context = EXPAND_FILES; // expand file names
2863 else
2864 xp->xp_context = EXPAND_NOTHING; // pattern is not expanded
2865 return NULL;
2866}
2867
2868/*
2869 * Function given to ExpandGeneric() to obtain the list of event names.
2870 */
2871 char_u *
2872get_event_name(expand_T *xp UNUSED, int idx)
2873{
John Marriott78d742a2024-04-02 20:26:01 +02002874 int i;
2875
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002876 if (idx < augroups.ga_len) // First list group names, if wanted
2877 {
2878 if (!include_groups || AUGROUP_NAME(idx) == NULL
2879 || AUGROUP_NAME(idx) == get_deleted_augroup())
2880 return (char_u *)""; // skip deleted entries
2881 return AUGROUP_NAME(idx); // return a name
2882 }
John Marriott78d742a2024-04-02 20:26:01 +02002883
2884 i = idx - augroups.ga_len;
Luuk van Baalb7147f82025-02-08 18:52:39 +01002885 if (i < 0 || i >= NUM_EVENTS)
John Marriott78d742a2024-04-02 20:26:01 +02002886 return NULL;
2887
John Marriott8d4477e2024-11-02 15:59:01 +01002888 return event_tab[i].value.string;
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002889}
2890
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002891/*
2892 * Function given to ExpandGeneric() to obtain the list of event names. Don't
2893 * include groups.
2894 */
2895 char_u *
Luuk van Baalb7147f82025-02-08 18:52:39 +01002896get_event_name_no_group(expand_T *xp UNUSED, int idx, int win)
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002897{
Luuk van Baalb7147f82025-02-08 18:52:39 +01002898 if (idx < 0 || idx >= NUM_EVENTS)
John Marriott78d742a2024-04-02 20:26:01 +02002899 return NULL;
2900
Luuk van Baalb7147f82025-02-08 18:52:39 +01002901 if (!win)
2902 return event_tab[idx].value.string;
2903
2904 // Need to check subset of allowed values for 'eventignorewin'.
2905 int j = 0;
2906 for (int i = 0; i < NUM_EVENTS; ++i)
2907 {
2908 j += event_tab[i].key <= 0;
2909 if (j == idx + 1)
2910 return event_tab[i].value.string;
2911 }
2912
2913 return NULL;
Yee Cheng Chin900894b2023-09-29 20:42:32 +02002914}
2915
Jim Zhou5606ca52025-03-13 21:58:25 +01002916/*
2917 * Return TRUE when there is a TabClosedPre autocommand defined.
2918 */
2919 int
2920has_tabclosedpre(void)
2921{
2922 return (first_autopat[(int)EVENT_TABCLOSEDPRE] != NULL);
2923}
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01002924
2925#if defined(FEAT_EVAL) || defined(PROTO)
2926/*
2927 * Return TRUE if autocmd is supported.
2928 */
2929 int
2930autocmd_supported(char_u *name)
2931{
2932 char_u *p;
2933
2934 return (event_name2nr(name, &p) != NUM_EVENTS);
2935}
2936
2937/*
2938 * Return TRUE if an autocommand is defined for a group, event and
2939 * pattern: The group can be omitted to accept any group. "event" and "pattern"
2940 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept
2941 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted.
2942 * Used for:
2943 * exists("#Group") or
2944 * exists("#Group#Event") or
2945 * exists("#Group#Event#pat") or
2946 * exists("#Event") or
2947 * exists("#Event#pat")
2948 */
2949 int
2950au_exists(char_u *arg)
2951{
2952 char_u *arg_save;
2953 char_u *pattern = NULL;
2954 char_u *event_name;
2955 char_u *p;
2956 event_T event;
2957 AutoPat *ap;
2958 buf_T *buflocal_buf = NULL;
2959 int group;
2960 int retval = FALSE;
2961
2962 // Make a copy so that we can change the '#' chars to a NUL.
2963 arg_save = vim_strsave(arg);
2964 if (arg_save == NULL)
2965 return FALSE;
2966 p = vim_strchr(arg_save, '#');
2967 if (p != NULL)
2968 *p++ = NUL;
2969
2970 // First, look for an autocmd group name
2971 group = au_find_group(arg_save);
2972 if (group == AUGROUP_ERROR)
2973 {
2974 // Didn't match a group name, assume the first argument is an event.
2975 group = AUGROUP_ALL;
2976 event_name = arg_save;
2977 }
2978 else
2979 {
2980 if (p == NULL)
2981 {
2982 // "Group": group name is present and it's recognized
2983 retval = TRUE;
2984 goto theend;
2985 }
2986
2987 // Must be "Group#Event" or "Group#Event#pat".
2988 event_name = p;
2989 p = vim_strchr(event_name, '#');
2990 if (p != NULL)
2991 *p++ = NUL; // "Group#Event#pat"
2992 }
2993
2994 pattern = p; // "pattern" is NULL when there is no pattern
2995
2996 // find the index (enum) for the event name
2997 event = event_name2nr(event_name, &p);
2998
2999 // return FALSE if the event name is not recognized
3000 if (event == NUM_EVENTS)
3001 goto theend;
3002
3003 // Find the first autocommand for this event.
3004 // If there isn't any, return FALSE;
3005 // If there is one and no pattern given, return TRUE;
3006 ap = first_autopat[(int)event];
3007 if (ap == NULL)
3008 goto theend;
3009
3010 // if pattern is "<buffer>", special handling is needed which uses curbuf
3011 // for pattern "<buffer=N>, fnamecmp() will work fine
3012 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0)
3013 buflocal_buf = curbuf;
3014
3015 // Check if there is an autocommand with the given pattern.
3016 for ( ; ap != NULL; ap = ap->next)
3017 // only use a pattern when it has not been removed and has commands.
3018 // For buffer-local autocommands, fnamecmp() works fine.
3019 if (ap->pat != NULL && ap->cmds != NULL
3020 && (group == AUGROUP_ALL || ap->group == group)
3021 && (pattern == NULL
3022 || (buflocal_buf == NULL
3023 ? fnamecmp(ap->pat, pattern) == 0
3024 : ap->buflocal_nr == buflocal_buf->b_fnum)))
3025 {
3026 retval = TRUE;
3027 break;
3028 }
3029
3030theend:
3031 vim_free(arg_save);
3032 return retval;
3033}
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003034
3035/*
3036 * autocmd_add() and autocmd_delete() functions
3037 */
3038 static void
3039autocmd_add_or_delete(typval_T *argvars, typval_T *rettv, int delete)
3040{
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003041 list_T *aucmd_list;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003042 listitem_T *li;
3043 dict_T *event_dict;
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003044 dictitem_T *di;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003045 char_u *event_name = NULL;
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003046 list_T *event_list;
3047 listitem_T *eli;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003048 event_T event;
3049 char_u *group_name = NULL;
3050 int group;
3051 char_u *pat = NULL;
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003052 list_T *pat_list;
3053 listitem_T *pli;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003054 char_u *cmd = NULL;
3055 char_u *end;
3056 int once;
3057 int nested;
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003058 int replace; // replace the cmd for a group/event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003059 int retval = VVAL_TRUE;
3060 int save_augroup = current_augroup;
3061
3062 rettv->v_type = VAR_BOOL;
3063 rettv->vval.v_number = VVAL_FALSE;
3064
3065 if (check_for_list_arg(argvars, 0) == FAIL)
3066 return;
3067
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003068 aucmd_list = argvars[0].vval.v_list;
3069 if (aucmd_list == NULL)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003070 return;
3071
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003072 FOR_ALL_LIST_ITEMS(aucmd_list, li)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003073 {
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003074 VIM_CLEAR(group_name);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003075 VIM_CLEAR(cmd);
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003076 event_name = NULL;
3077 event_list = NULL;
3078 pat = NULL;
3079 pat_list = NULL;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003080
3081 if (li->li_tv.v_type != VAR_DICT)
3082 continue;
3083
3084 event_dict = li->li_tv.vval.v_dict;
3085 if (event_dict == NULL)
3086 continue;
3087
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003088 di = dict_find(event_dict, (char_u *)"event", -1);
3089 if (di != NULL)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003090 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003091 if (di->di_tv.v_type == VAR_STRING)
3092 {
3093 event_name = di->di_tv.vval.v_string;
3094 if (event_name == NULL)
3095 {
3096 emsg(_(e_string_required));
3097 continue;
3098 }
3099 }
3100 else if (di->di_tv.v_type == VAR_LIST)
3101 {
3102 event_list = di->di_tv.vval.v_list;
3103 if (event_list == NULL)
3104 {
3105 emsg(_(e_list_required));
3106 continue;
3107 }
3108 }
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003109 else
3110 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003111 emsg(_(e_string_or_list_expected));
3112 continue;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003113 }
3114 }
3115
Bram Moolenaard61efa52022-07-23 09:52:04 +01003116 group_name = dict_get_string(event_dict, "group", TRUE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003117 if (group_name == NULL || *group_name == NUL)
3118 // if the autocmd group name is not specified, then use the current
3119 // autocmd group
3120 group = current_augroup;
3121 else
3122 {
3123 group = au_find_group(group_name);
3124 if (group == AUGROUP_ERROR)
3125 {
3126 if (delete)
3127 {
3128 semsg(_(e_no_such_group_str), group_name);
3129 retval = VVAL_FALSE;
3130 break;
3131 }
3132 // group is not found, create it now
3133 group = au_new_group(group_name);
3134 if (group == AUGROUP_ERROR)
3135 {
3136 semsg(_(e_no_such_group_str), group_name);
3137 retval = VVAL_FALSE;
3138 break;
3139 }
3140
3141 current_augroup = group;
3142 }
3143 }
3144
3145 // if a buffer number is specified, then generate a pattern of the form
3146 // "<buffer=n>. Otherwise, use the pattern supplied by the user.
3147 if (dict_has_key(event_dict, "bufnr"))
3148 {
3149 varnumber_T bnum;
3150
Bram Moolenaard61efa52022-07-23 09:52:04 +01003151 bnum = dict_get_number_def(event_dict, "bufnr", -1);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003152 if (bnum == -1)
3153 continue;
3154
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003155 vim_snprintf((char *)IObuff, IOSIZE, "<buffer=%d>", (int)bnum);
3156 pat = IObuff;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003157 }
3158 else
3159 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003160 di = dict_find(event_dict, (char_u *)"pattern", -1);
3161 if (di != NULL)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003162 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003163 if (di->di_tv.v_type == VAR_STRING)
3164 {
3165 pat = di->di_tv.vval.v_string;
3166 if (pat == NULL)
3167 {
3168 emsg(_(e_string_required));
3169 continue;
3170 }
3171 }
3172 else if (di->di_tv.v_type == VAR_LIST)
3173 {
3174 pat_list = di->di_tv.vval.v_list;
3175 if (pat_list == NULL)
3176 {
3177 emsg(_(e_list_required));
3178 continue;
3179 }
3180 }
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003181 else
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003182 {
3183 emsg(_(e_string_or_list_expected));
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003184 continue;
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003185 }
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003186 }
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003187 else if (delete)
3188 pat = (char_u *)"";
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003189 }
3190
Bram Moolenaard61efa52022-07-23 09:52:04 +01003191 once = dict_get_bool(event_dict, "once", FALSE);
3192 nested = dict_get_bool(event_dict, "nested", FALSE);
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01003193 // if 'replace' is true, then remove all the commands associated with
3194 // this autocmd event/group and add the new command.
Bram Moolenaard61efa52022-07-23 09:52:04 +01003195 replace = dict_get_bool(event_dict, "replace", FALSE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003196
Bram Moolenaard61efa52022-07-23 09:52:04 +01003197 cmd = dict_get_string(event_dict, "cmd", TRUE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003198 if (cmd == NULL)
3199 {
3200 if (delete)
3201 cmd = vim_strsave((char_u *)"");
3202 else
3203 continue;
3204 }
3205
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003206 if (delete && (event_name == NULL
3207 || (event_name[0] == '*' && event_name[1] == NUL)))
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003208 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003209 // if the event name is not specified or '*', delete all the events
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003210 for (event = (event_T)0; (int)event < NUM_EVENTS;
3211 event = (event_T)((int)event + 1))
3212 {
3213 if (do_autocmd_event(event, pat, once, nested, cmd, delete,
3214 group, 0) == FAIL)
3215 {
3216 retval = VVAL_FALSE;
3217 break;
3218 }
3219 }
3220 }
3221 else
3222 {
Bram Moolenaar968443e2022-05-27 21:16:34 +01003223 char_u *p = NULL;
3224
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003225 eli = NULL;
3226 end = NULL;
3227 while (TRUE)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003228 {
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003229 if (event_list != NULL)
3230 {
3231 if (eli == NULL)
3232 eli = event_list->lv_first;
3233 else
3234 eli = eli->li_next;
3235 if (eli == NULL)
3236 break;
3237 if (eli->li_tv.v_type != VAR_STRING
Bram Moolenaar882476a2022-06-01 16:02:38 +01003238 || (p = eli->li_tv.vval.v_string) == NULL)
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003239 {
3240 emsg(_(e_string_required));
Bram Moolenaar882476a2022-06-01 16:02:38 +01003241 break;
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003242 }
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003243 }
3244 else
3245 {
Bram Moolenaar882476a2022-06-01 16:02:38 +01003246 if (p == NULL)
3247 p = event_name;
3248 if (p == NULL || *p == NUL)
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003249 break;
3250 }
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003251
3252 event = event_name2nr(p, &end);
3253 if (event == NUM_EVENTS || *end != NUL)
3254 {
Bram Moolenaar882476a2022-06-01 16:02:38 +01003255 // this also catches something following a valid event name
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01003256 semsg(_(e_no_such_event_str), p);
3257 retval = VVAL_FALSE;
3258 break;
3259 }
3260 if (pat != NULL)
3261 {
3262 if (do_autocmd_event(event, pat, once, nested, cmd,
3263 delete | replace, group, 0) == FAIL)
3264 {
3265 retval = VVAL_FALSE;
3266 break;
3267 }
3268 }
3269 else if (pat_list != NULL)
3270 {
3271 FOR_ALL_LIST_ITEMS(pat_list, pli)
3272 {
3273 if (pli->li_tv.v_type != VAR_STRING
3274 || pli->li_tv.vval.v_string == NULL)
3275 {
3276 emsg(_(e_string_required));
3277 continue;
3278 }
3279 if (do_autocmd_event(event,
3280 pli->li_tv.vval.v_string, once, nested,
3281 cmd, delete | replace, group, 0) ==
3282 FAIL)
3283 {
3284 retval = VVAL_FALSE;
3285 break;
3286 }
3287 }
3288 if (retval == VVAL_FALSE)
3289 break;
3290 }
3291 if (event_name != NULL)
3292 p = end;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003293 }
3294 }
3295
3296 // if only the autocmd group name is specified for delete and the
3297 // autocmd event, pattern and cmd are not specified, then delete the
3298 // autocmd group.
3299 if (delete && group_name != NULL &&
3300 (event_name == NULL || event_name[0] == NUL)
3301 && (pat == NULL || pat[0] == NUL)
3302 && (cmd == NULL || cmd[0] == NUL))
3303 au_del_group(group_name);
3304 }
3305
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003306 VIM_CLEAR(group_name);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003307 VIM_CLEAR(cmd);
3308
3309 current_augroup = save_augroup;
3310 rettv->vval.v_number = retval;
3311}
3312
3313/*
3314 * autocmd_add() function
3315 */
3316 void
3317f_autocmd_add(typval_T *argvars, typval_T *rettv)
3318{
3319 autocmd_add_or_delete(argvars, rettv, FALSE);
3320}
3321
3322/*
3323 * autocmd_delete() function
3324 */
3325 void
3326f_autocmd_delete(typval_T *argvars, typval_T *rettv)
3327{
3328 autocmd_add_or_delete(argvars, rettv, TRUE);
3329}
3330
3331/*
3332 * autocmd_get() function
3333 * Returns a List of autocmds.
3334 */
3335 void
3336f_autocmd_get(typval_T *argvars, typval_T *rettv)
3337{
3338 event_T event_arg = NUM_EVENTS;
3339 event_T event;
3340 AutoPat *ap;
3341 AutoCmd *ac;
3342 list_T *event_list;
3343 dict_T *event_dict;
3344 char_u *event_name = NULL;
3345 char_u *pat = NULL;
3346 char_u *name = NULL;
3347 int group = AUGROUP_ALL;
3348
Yegappan Lakshmananca195cc2022-06-14 13:42:26 +01003349 if (rettv_list_alloc(rettv) == FAIL)
3350 return;
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003351 if (check_for_opt_dict_arg(argvars, 0) == FAIL)
3352 return;
3353
3354 if (argvars[0].v_type == VAR_DICT)
3355 {
3356 // return only the autocmds in the specified group
3357 if (dict_has_key(argvars[0].vval.v_dict, "group"))
3358 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003359 name = dict_get_string(argvars[0].vval.v_dict, "group", TRUE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003360 if (name == NULL)
3361 return;
3362
3363 if (*name == NUL)
3364 group = AUGROUP_DEFAULT;
3365 else
3366 {
3367 group = au_find_group(name);
3368 if (group == AUGROUP_ERROR)
3369 {
3370 semsg(_(e_no_such_group_str), name);
3371 vim_free(name);
3372 return;
3373 }
3374 }
3375 vim_free(name);
3376 }
3377
3378 // return only the autocmds for the specified event
3379 if (dict_has_key(argvars[0].vval.v_dict, "event"))
3380 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003381 name = dict_get_string(argvars[0].vval.v_dict, "event", TRUE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003382 if (name == NULL)
3383 return;
3384
3385 if (name[0] == '*' && name[1] == NUL)
3386 event_arg = NUM_EVENTS;
3387 else
3388 {
John Marriott78d742a2024-04-02 20:26:01 +02003389 keyvalue_T target;
3390 keyvalue_T *entry;
3391
3392 target.key = 0;
John Marriott8d4477e2024-11-02 15:59:01 +01003393 target.value.string = name;
3394 target.value.length = STRLEN(target.value.string);
3395 entry = (keyvalue_T *)bsearch(&target, &event_tab,
Luuk van Baalb7147f82025-02-08 18:52:39 +01003396 NUM_EVENTS, sizeof(event_tab[0]), cmp_keyvalue_value_ni);
John Marriott78d742a2024-04-02 20:26:01 +02003397 if (entry == NULL)
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003398 {
3399 semsg(_(e_no_such_event_str), name);
3400 vim_free(name);
3401 return;
3402 }
Luuk van Baalb7147f82025-02-08 18:52:39 +01003403 event_arg = (event_T)abs(entry->key);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003404 }
3405 vim_free(name);
3406 }
3407
3408 // return only the autocmds for the specified pattern
3409 if (dict_has_key(argvars[0].vval.v_dict, "pattern"))
3410 {
Bram Moolenaard61efa52022-07-23 09:52:04 +01003411 pat = dict_get_string(argvars[0].vval.v_dict, "pattern", TRUE);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003412 if (pat == NULL)
3413 return;
3414 }
3415 }
3416
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003417 event_list = rettv->vval.v_list;
3418
3419 // iterate through all the autocmd events
3420 for (event = (event_T)0; (int)event < NUM_EVENTS;
3421 event = (event_T)((int)event + 1))
3422 {
3423 if (event_arg != NUM_EVENTS && event != event_arg)
3424 continue;
3425
3426 event_name = event_nr2name(event);
3427
3428 // iterate through all the patterns for this autocmd event
3429 FOR_ALL_AUTOCMD_PATTERNS(event, ap)
3430 {
3431 char_u *group_name;
3432
zeertzjq2d1d5c62024-06-09 16:44:33 +02003433 if (ap->pat == NULL) // pattern has been removed
3434 continue;
3435
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003436 if (group != AUGROUP_ALL && group != ap->group)
3437 continue;
3438
3439 if (pat != NULL && STRCMP(pat, ap->pat) != 0)
3440 continue;
3441
3442 group_name = get_augroup_name(NULL, ap->group);
3443
3444 // iterate through all the commands for this pattern and add one
3445 // item for each cmd.
3446 for (ac = ap->cmds; ac != NULL; ac = ac->next)
3447 {
3448 event_dict = dict_alloc();
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003449 if (event_dict == NULL
3450 || list_append_dict(event_list, event_dict) == FAIL)
Christian Brabandt29269a72024-04-16 22:44:31 +02003451 {
3452 vim_free(pat);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003453 return;
Christian Brabandt29269a72024-04-16 22:44:31 +02003454 }
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003455
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01003456 if (dict_add_string(event_dict, "event", event_name) == FAIL
3457 || dict_add_string(event_dict, "group",
3458 group_name == NULL ? (char_u *)""
3459 : group_name) == FAIL
3460 || (ap->buflocal_nr != 0
3461 && (dict_add_number(event_dict, "bufnr",
3462 ap->buflocal_nr) == FAIL))
3463 || dict_add_string(event_dict, "pattern",
3464 ap->pat) == FAIL
3465 || dict_add_string(event_dict, "cmd", ac->cmd) == FAIL
3466 || dict_add_bool(event_dict, "once", ac->once) == FAIL
3467 || dict_add_bool(event_dict, "nested",
3468 ac->nested) == FAIL)
Christian Brabandt29269a72024-04-16 22:44:31 +02003469 {
3470 vim_free(pat);
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003471 return;
Christian Brabandt29269a72024-04-16 22:44:31 +02003472 }
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003473 }
3474 }
3475 }
3476
3477 vim_free(pat);
3478}
3479
Bram Moolenaar3e460fd2019-01-26 16:21:07 +01003480#endif