Vim logo vim online Vim Book Ad

 Tip #176: Autocheckout from perforce

 tip karma   Rating 4/4, Viewed by 494 

created:   December 8, 2001 12:05      complexity:   intermediate
author:   Steven Grady      as of Vim:   6.0

The following code automatically checks out files from perforce when the user modifies them. It first confirms the check-out with the user.

(Perforce is a commercial version control system.  I imagine this could be modified for RCS, CVS, etc., but I don't use those.)

I'm a vim newbie -- I've used vi since 1984, but just started with vim a couple days ago.  Color me impressed!  Please excuse any stupidity in the code..

Note that this function needs the "P4HOME" environment variable to be set.  I could extract it by invoking "p4 client", but I don't want to invoke p4 every time I start vim.  So I assume the user sets it in the environment.

" Set a buffer-local variable to the perforce path, if this file is under the perforce root.
function IsUnderPerforce()
    if exists("$P4HOME")
        if expand("%:p") =~ ("^" . $P4HOME)
            let b:p4path = substitute(expand("%:p"), $P4HOME, "//depot", "")
        endif
    endif
endfunction
" Confirm with the user, then checkout a file from perforce.
function P4Checkout()
   if exists("b:p4path")
        if (confirm("Checkout from Perforce?", "&Yes;\n&No;", 1) == 1)
            call system("p4 edit " . b:p4path . " > /dev/null")
            if v:shell_error == 0
                set noreadonly
            endif
        endif
    endif
endfunction

if !exists("au_p4_cmd")
    let au_p4_cmd=1

    au BufEnter * call IsUnderPerforce()
    au FileChangedRO * call P4Checkout()
endif

 rate this tip  Life Changing Helpful Unfulfilling 

<<how to make VIM as ur default editor even without root ac. | Highlight matching brackets as one moves in normal mode (plugin) >>

Additional Notes

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo