【问题标题】:Can I use cppcheck when I execute :wq in Vim editor for c/c++ in a interactive way当我在 Vim 编辑器中以交互方式执行 c/c++ 中的 wq 时,我可以使用 cppcheck
【发布时间】:2011-08-09 04:37:03
【问题描述】:

谁能帮我找到满足我要求的解决方案?

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

提前致谢。

【问题讨论】:

    标签: vim cppcheck


    【解决方案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,它就不能被中止.

    【讨论】:

    • 如何在 makefile 中或编译时使用 cppcheck
    • 把 make/cc 放到后台,打开一个新的 shell 等等。但这是一个不同的问题,真的,所以请再问一个问题。
    猜你喜欢
    • 1970-01-01
    • 2020-11-11
    • 2012-06-23
    • 2011-07-08
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 2012-12-30
    相关资源
    最近更新 更多