changeset 0:4aede5f1af45

Initial commit.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 11 Feb 2015 15:03:01 -0800
parents
children 953baa4a16bb
files README.md autoload/p44vim.vim plugin/p44vim.vim
diffstat 3 files changed, 154 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Wed Feb 11 15:03:01 2015 -0800
@@ -0,0 +1,7 @@
+
+# P44Vim
+
+P44Vim is a bare-bones, simple Vim plugin that adds support for working in
+Perforce depots. It aims to stay out of your way, not mess up anything that it
+shouldn't, and not do too much.
+
--- /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
+
+" }}}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/plugin/p44vim.vim	Wed Feb 11 15:03:01 2015 -0800
@@ -0,0 +1,39 @@
+
+if exists('g:loaded_p44v') && !exists('g:p44v_debug')
+    finish
+endif
+let g:loaded_p44v = 1
+
+if !exists('g:p44v_trace')
+    let g:p44v_trace = 0
+endif
+
+if !exists('g:p44v_exe')
+    let g:p44v_exe = 'p4'
+endif
+
+if !executable(g:p44v_exe)
+    echoerr "p44vim: '".g:p44v_exe."' is not a known executable. ".
+                \"Perforce commands won't work."
+endif
+
+" Autocommands {{{
+
+" When a new buffer is opened, try to figure out if it's in a P4 depot.
+" If it is, then setup some auto-commands to do stuff like auto-open-for-edit
+" when you start editing the file.
+augroup p44v_auto_detect
+    autocmd!
+    autocmd BufRead * call p44vim#install_p4_auto_commands()
+augroup END
+
+" }}}
+
+" P4 Commands {{{
+
+command! -nargs=* -complete=file P4Sync :call p44vim#p4sync(<f-args>)
+command! -nargs=* -complete=file P4Edit :call p44vim#p4edit(<f-args>)
+command! -nargs=* -complete=file P4Revert :call p44vim#p4revert(<f-args>)
+
+" }}}
+