当我在Vim编辑器中以交互方式执行c / c ++时,我可以使用cppcheck吗?

时间:2011-08-09 04:37:04

标签: vim cppcheck

有人可以帮我解决我的要求吗?

要求是当用户退出vim时,cppcheck应该发生,如果发生任何警告或错误,则应该提示用户。

提前致谢。

1 个答案:

答案 0 :(得分:1)

我假设您不关心命令是否异步执行,因为您无论如何都要退出缓冲区。您可以使用:!命令运行shell命令,使用:read将输出捕获到新窗口:

function! s:RunShellCommand(cmdline)
    let first = 1
    let words = []
    " Expand and escape cmd arguments.
    " shellescape() should work with '\'
    for part in split(a:cmdline)
        if first
            " skip the cmd. ugly, i know.
            let first = 0
        else
            if part[0] =~ '\v[%#<]'
                let part = expand(part)
            endif
            let part = shellescape(part, 1)
       endif
       call add(words, part)
   endfor
   let expanded_cmdline = join(words)

   " Create the new window
   botright new
   setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
   call setline(1, 'Showing output from cmd:    ' . expanded_cmdline)
   call append(line('$'), substitute(getline(2), '.', '=', 'g'))

   " This is where actual work is getting done :-)
   silent execute '$read !'. expanded_cmdline

   " Uncomment the line below if you want the buffer to be
   " non-modifiable
   " setlocal nomodifiable
   1
endfunction

然后,您可以为缓冲区卸载时定义自动命令:

au BufUnload *.cpp s:RunShellCommand('cppcheck %')

或者您可以随时调用的更通用的命令:

command! -complete=shellcmd -nargs=+ Shell call s:RunShellCommand(<q-args>)

现在,为了防止关闭缓冲区,您必须将:wq:q重新映射到将执行上述操作的函数(加上可能还有一些确认?),因为一旦:quit为调用,它不能被中止。