Well, :silent!
suppresses errors and neutralizes exceptions. If the netrw
code is indeed throwing exceptions (see below) then removing it, or at least dropping the !
, should clear things up. (Note: I'm interpreting what you said about that error message you saw as if that occurred in a later/different context than this code with :silent!
. Accurate?)
If you need :silent!
for some other reason you could check for failure by checking whether the variable v:errmsg
is non-empty/non-null. With the code below we will see v:errmsg is bzzzt
.
func! DoError()
echoerr("bzzzt")
endfunc
func! HandleError()
echom("got it")
endfunc
func! ErrmsgTest()
try
let v:errmsg = ""
silent! :call DoError()
echom("v:errmsg is " . v:errmsg)
catch
call HandleError()
endtry
endfunc
Update: saw some netrw documentation that says "messages from netrw will use echoerr" so the evidence indicates you can solve your issue by removing :silent!
or using v:errmsg
.
Update 2: Well, silly me, believing the documentation is accurate. My findings after a deeper dive...
There is a flag g:netrw_use_errorwindow
that when set to 0 (as OP has tried) is supposed to result in, per the docs: messages from netrw will use echoerr.
But at the point in the code where the flag comes into play a comment says netrw will show messages using echomsg, completely contradicting the docs. And indeed echomsg
is used there not echoerr
. On top of that I initially overlooked that the message is a warning and rarely will you find exceptions thrown as a result of a warning condition (at least not in quality code).
So basically there's nothing you can do to directly detect this state.
How about a hack? One that comes to mind is to see if the message in question shows up in the message history. You could do something like
mess clear
Rex
if match(execute("1mess"), "not a former netrw window") >= 0
Ex " or whatever you want to do to handle the warn state
endif
Like I said, hack, but it should work if there's nothing else going on in parallel that can add to message history.
As a side note, the key echomsg
in the netrw code is preceded by unsilent
which explains why you're seeing the warning despite silent
. (unsilent
nullifes the message suppression caused by any silent
command set in the surrounding code.)
:Rexplore
doesn’t throw an error of the kind catch can handle, but I may be wrong.