开发者

Can I use cppcheck when I execute :wq in Vim editor for c/c++ in a interactive way

开发者 https://www.devze.com 2023-03-26 15:51 出处:网络
Can anybody help me 开发者_JAVA百科to get solution for my requirement? Requirement is when a user exits from vim, cppcheck should happen and if any warning or error occurs then it should be prompted

Can anybody help me 开发者_JAVA百科to get solution for my requirement?

Requirement is when a user exits from vim, cppcheck should happen and if any warning or error occurs then it should be prompted to a user.

Thanks in advance.


I assume you don't care if the command is executed asynchronously, since you're quitting the buffer anyway. You can use the :! command to run shell commands and :read to capture the output to a new window:

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

Then you can define an autocommand for when a buffer is unloading:

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

or a somewhat more generic command which you can call at any time:

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

Now, to prevent closing your buffer, you have to remap :wq or :q to a function that will perform the aforementioned (plus perhaps some confirmation?), since once :quit is invoked, it cannot be aborted.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号