FIX: Directly echo previous errors instead of concatenating them

The problem is that :echoerr does not handle newlines (\n -> ^@, \r -> ^M), and as the "echoerr ingo#err#Get()" is used pervasively by clients, it's hard to turn than into a loop, also because of the exception semantics of :echoerr (so subsequent ones might be suppressed).
Instead, if a second (third, ...) error is encountered, :echomsg the previous one with error highlighting, and move the next error into the slot, so that only the last error is used with :echoerr.
This commit is contained in:
Ingo Karkat
2021-06-09 18:12:50 +02:00
parent d3fc7d0fbf
commit a78341e8a5
2 changed files with 26 additions and 31 deletions
+12 -13
View File
@@ -10,8 +10,8 @@
function! ingo#buffers#Delete( buffersToDelete, isForce ) abort
"******************************************************************************
"* PURPOSE:
" Delete (:bdelete) all buffers in a:buffersToDelete, and collect any
" encountered errors.
" Delete (:bdelete) all buffers in a:buffersToDelete, and report any
" encountered errors (with the affected buffer name prepended).
"* ASSUMPTIONS / PRECONDITIONS:
" None.
"* EFFECTS / POSTCONDITIONS:
@@ -20,26 +20,25 @@ function! ingo#buffers#Delete( buffersToDelete, isForce ) abort
" a:buffersToDelete List of buffer numbers.
" a:isForce Force flag; uses :bdelete! if true.
"* RETURN VALUES:
" 1 if complete success, 0 if error(s) / exception(s) occurred. An error
" message is then available from ingo#err#Get().
" 1 if complete success, 0 if error(s) / exception(s) occurred. The last error
" message is then available from ingo#err#Get(); previous errors have already
" been echoed.
"******************************************************************************
call ingo#err#Clear()
let l:errors = []
let l:isSuccess = 1
for l:bufNr in a:buffersToDelete
try
execute l:bufNr . 'bdelete' . (a:isForce ? '!' : '')
catch /^Vim\%((\a\+)\)\=:/
call add(l:errors, printf('%s: %s', ingo#buffer#NameOrDefault(bufname(l:bufNr)), ingo#msg#MsgFromVimException()))
let l:isSuccess = 0
if ingo#err#IsSet()
call ingo#msg#ErrorMsg(ingo#err#Get())
endif
call ingo#err#Set(printf('%s: %s', ingo#buffer#NameOrDefault(bufname(l:bufNr)), ingo#msg#MsgFromVimException()))
endtry
endfor
if empty(l:errors)
let l:isSuccess = 1
else
call ingo#err#Set(join(l:errors))
let l:isSuccess = 0
endif
return l:isSuccess
endfunction
+14 -18
View File
@@ -118,7 +118,12 @@ function! ingo#window#iterate#ActionWithCatch( Action, ... ) abort
execute l:command
endif
catch /^Vim\%((\a\+)\)\=:/
call add(s:errors, printf('%s: %s', ingo#buffer#NameOrDefault(bufname('')), ingo#msg#MsgFromVimException()))
let s:isSuccess = 0
if ingo#err#IsSet()
call ingo#msg#ErrorMsg(ingo#err#Get())
endif
call ingo#err#Set(printf('%s: %s', ingo#buffer#NameOrDefault(bufname('')), ingo#msg#MsgFromVimException()))
endtry
endfunction
@@ -126,8 +131,8 @@ function! ingo#window#iterate#AllWithErrorsEchoed( Action, ... ) abort
"******************************************************************************
"* PURPOSE:
" Execute a:Action in all windows in the current tab page. Errors / exceptions
" do not abort the iteration, but instead :echomsg the error messages (with
" the affected buffer name prepended).
" do not abort the iteration, but are reported (with the affected buffer name
" prepended).
"* ASSUMPTIONS / PRECONDITIONS:
" - a:Action must not remove or add windows, as that will mess with iteration.
"* EFFECTS / POSTCONDITIONS:
@@ -139,23 +144,14 @@ function! ingo#window#iterate#AllWithErrorsEchoed( Action, ... ) abort
" v:val is inserted literally (as a Number, String, List,
" Dict)!
"* RETURN VALUES:
" 1 if complete success, 0 if error(s) / exception(s) occurred. An error
" message is then available from ingo#err#Get().
" 1 if complete success, 0 if error(s) / exception(s) occurred. The last error
" message is then available from ingo#err#Get(); previous errors have already
" been echoed.
"******************************************************************************
call ingo#err#Clear()
let s:errors = []
call call('ingo#window#iterate#All', [function('ingo#window#iterate#ActionWithCatch'), a:Action] + a:000)
if empty(s:errors)
let l:isSuccess = 1
else
call ingo#err#Set(join(s:errors))
let l:isSuccess = 0
endif
unlet! s:errors
return l:isSuccess
let s:isSuccess = 1
call call('ingo#window#iterate#All', [function('ingo#window#iterate#ActionWithCatch'), a:Action] + a:000)
return s:isSuccess
endfunction
let &cpo = s:save_cpo