Mercurial > vim-p44vim
diff autoload/p44vim.vim @ 0:4aede5f1af45
Initial commit.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 11 Feb 2015 15:03:01 -0800 |
parents | |
children | 953baa4a16bb |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/autoload/p44vim.vim Wed Feb 11 15:03:01 2015 -0800 @@ -0,0 +1,108 @@ + +" Utilities {{{ + +function! s:trace(msg) abort + if g:p44v_trace + echom "p44vim: ".a:msg + endif +endfunction + +function! s:throw(msg) abort + throw "p44vim: ".a:msg +endfunction + +function! s:run_perforce_command(...) abort + let l:args = a:000 + if a:0 == 1 && type(a:1) == type([]) + let l:args = a:1 + endif + let l:cmd = ['p4'] + call extend(l:cmd, l:args) + let l:strcmd = join(map(l:cmd, 'shellescape(v:val)')) + call s:trace("Running command: ".l:strcmd) + silent call system(l:strcmd) +endfunction + +function! s:get_p4_depot_root(path) abort + let l:cur = a:path + let l:prev_cur = '' + while l:cur != l:prev_cur + if filereadable(l:cur.'/.p4config') || + \filereadable(l:cur.'/.p4ignore') + return l:cur + endif + let l:prev_cur = l:cur + let l:cur = fnamemodify(l:cur, ':h') + endwhile + call s:throw("No p4 depot found at: ".a:path) +endfunction + +" }}} + +" Auto-commands {{{ + +let s:ignore_next_w12 = 0 + +function! s:auto_edit_buffer() abort + call p44vim#p4edit() +endfunction + +function! s:maybe_ignore_w12() abort + if s:ignore_next_w12 + let v:fcs_choice = '' " Ignore the warning, keep the file. + let s:ignore_next_w12 = 0 + endif +endfunction + +function! p44vim#install_p4_auto_commands() abort + call s:trace("Scanning buffer '".bufname('%')."' for Perforce setup...") + try + let l:repo_root = s:get_p4_depot_root(expand('%:h')) + catch /^p44vim\:/ + return + endtry + + let b:p44v_repo_root = l:repo_root + call s:trace("Setting up P4 auto-commands for: ".bufname('%')) + + augroup p44v_auto + autocmd! + autocmd FileChangedRO * call <SID>auto_edit_buffer() + autocmd FileChangedShell * call <SID>maybe_ignore_w12() + augroup END +endfunction + +" }}} + +" Commands {{{ + +function! p44vim#p4sync(...) abort + let l:cmd = ['sync'] + a:000 + call s:run_perforce_command(l:cmd) +endfunction + +function! p44vim#p4edit(...) abort + if a:0 + let l:filenames = a:000 + else + let l:filenames = [expand('%:p')] + endif + let l:cmd = ['edit'] + l:filenames + let l:ignore_next_w12 = 1 + call s:run_perforce_command(l:cmd) + set noreadonly +endfunction + +function! p44vim#p4revert(...) abort + if a:0 + let l:filenames = a:000 + else + let l:filenames = [expand('%:p')] + endif + let l:cmd = ['revert'] + l:filenames + call s:run_perforce_command(l:cmd) + silent edit +endfunction + +" }}} +