Say that I want to define my own autocmd as it follows:
autocmd User Foo echom "Here I am!"
And then I have a function that at some point trigger that autocmd
, like the following:
def Func()
# .. do something
var user_autocmds = execute('autocmd User')
if stridx(user_autocmds, "Foo") != -1
doautocmd User Foo
endif
# ... do something else
enddef
I am wondering if the way of "guarding" it described above is the best I can do.
My thinking is that the doautocmd User Foo
shall be executed only if the autocmd Foo
is defined. If not, then the doautocmd
shall not be executed. This because I don't want to get the message No matching autocommand Foo
every time I call Func()
if Foo
autocommand is not defined.
Surely I can use silent! doautocmd ...
but then I would miss ALL the error message coming from the invocation of doautocmd
Can I do better?
:help exists()
.