1

有人可以帮我找到满足我要求的解决方案吗?

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

提前致谢。

4

1 回答 1

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被调用,它就不能被中止。

于 2011-08-09T06:52:44.413 回答