Currently, I have begun using vim for C++ projects. I use vimspector for debugging help. This offers a command :call vimspector#Launch()
to debug the current successful build. To build the current code, I have the following keybinding:
:nnoremap <F4> :%update \| :make CONF=Debug -C ./.vim \| :cope <CR>
which essentially:
- saves modified source code files,
- compiles and builds the source code files by running make on
.vim/Makefile-Debug.mk
, and, - opens the quick-fix list containing warnings and possible errors.
After a successful build, I separately and manually launch the debugging session. Is there a way to have the above keymapping also execute :call vimspector#Launch()
automatically if make did not return any errors?
This link specifies that make returns a 0 if there were no errors discovered (warnings while compilation seem to be OK and not affect the return value of 0).
So, can I have something like below (in pseudocode) in .vimrc?
:nnoremap <F4> :%update \| :make CONF=Debug -C ./.vim \| if not 0 -> :cope <CR> | if 0 -> :call vimspector#Launch()
where if make returns a 0 the debugging session is launched and if not, the quick fix list is opened?
ETA1: Following Luc Hermitte's suggestion, I had:
function Get_buildmetrics() abort
let qf = getqflist()
let recognized = filter(qf, 'get(v:val, "valid", 1)')
" TODO: support other locales, see lh#po#context().tranlate()
let errors = filter(copy(recognized), 'v:val.type == "E" || v:val.text =~ "\\v^ *(error|erreur)"')
let warnings = filter(copy(recognized), 'v:val.type == "W" || v:val.text =~ "\\v^ *(warning|attention)"')
let res = { 'all': len(qf), 'errors': len(errors), 'warnings': len(warnings) }
return res
endfunction
function Build_and_run() abort
" to make sure the buffer is saved
exe '%update'
exe 'make CONF=Debug -C ./.vim'
if get_buildmetrics().errors
echom "Error detected, execution aborted"
copen
return
endif
exe 'call vimspector#Launch()'
endfunction
in my .vimrc. Then,
:call Build_and_run()
from within Vim does exactly what I want!
Merci beaucoup!