Mercurial > vim-p44vim
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4aede5f1af45 |
---|---|
1 | |
2 " Utilities {{{ | |
3 | |
4 function! s:trace(msg) abort | |
5 if g:p44v_trace | |
6 echom "p44vim: ".a:msg | |
7 endif | |
8 endfunction | |
9 | |
10 function! s:throw(msg) abort | |
11 throw "p44vim: ".a:msg | |
12 endfunction | |
13 | |
14 function! s:run_perforce_command(...) abort | |
15 let l:args = a:000 | |
16 if a:0 == 1 && type(a:1) == type([]) | |
17 let l:args = a:1 | |
18 endif | |
19 let l:cmd = ['p4'] | |
20 call extend(l:cmd, l:args) | |
21 let l:strcmd = join(map(l:cmd, 'shellescape(v:val)')) | |
22 call s:trace("Running command: ".l:strcmd) | |
23 silent call system(l:strcmd) | |
24 endfunction | |
25 | |
26 function! s:get_p4_depot_root(path) abort | |
27 let l:cur = a:path | |
28 let l:prev_cur = '' | |
29 while l:cur != l:prev_cur | |
30 if filereadable(l:cur.'/.p4config') || | |
31 \filereadable(l:cur.'/.p4ignore') | |
32 return l:cur | |
33 endif | |
34 let l:prev_cur = l:cur | |
35 let l:cur = fnamemodify(l:cur, ':h') | |
36 endwhile | |
37 call s:throw("No p4 depot found at: ".a:path) | |
38 endfunction | |
39 | |
40 " }}} | |
41 | |
42 " Auto-commands {{{ | |
43 | |
44 let s:ignore_next_w12 = 0 | |
45 | |
46 function! s:auto_edit_buffer() abort | |
47 call p44vim#p4edit() | |
48 endfunction | |
49 | |
50 function! s:maybe_ignore_w12() abort | |
51 if s:ignore_next_w12 | |
52 let v:fcs_choice = '' " Ignore the warning, keep the file. | |
53 let s:ignore_next_w12 = 0 | |
54 endif | |
55 endfunction | |
56 | |
57 function! p44vim#install_p4_auto_commands() abort | |
58 call s:trace("Scanning buffer '".bufname('%')."' for Perforce setup...") | |
59 try | |
60 let l:repo_root = s:get_p4_depot_root(expand('%:h')) | |
61 catch /^p44vim\:/ | |
62 return | |
63 endtry | |
64 | |
65 let b:p44v_repo_root = l:repo_root | |
66 call s:trace("Setting up P4 auto-commands for: ".bufname('%')) | |
67 | |
68 augroup p44v_auto | |
69 autocmd! | |
70 autocmd FileChangedRO * call <SID>auto_edit_buffer() | |
71 autocmd FileChangedShell * call <SID>maybe_ignore_w12() | |
72 augroup END | |
73 endfunction | |
74 | |
75 " }}} | |
76 | |
77 " Commands {{{ | |
78 | |
79 function! p44vim#p4sync(...) abort | |
80 let l:cmd = ['sync'] + a:000 | |
81 call s:run_perforce_command(l:cmd) | |
82 endfunction | |
83 | |
84 function! p44vim#p4edit(...) abort | |
85 if a:0 | |
86 let l:filenames = a:000 | |
87 else | |
88 let l:filenames = [expand('%:p')] | |
89 endif | |
90 let l:cmd = ['edit'] + l:filenames | |
91 let l:ignore_next_w12 = 1 | |
92 call s:run_perforce_command(l:cmd) | |
93 set noreadonly | |
94 endfunction | |
95 | |
96 function! p44vim#p4revert(...) abort | |
97 if a:0 | |
98 let l:filenames = a:000 | |
99 else | |
100 let l:filenames = [expand('%:p')] | |
101 endif | |
102 let l:cmd = ['revert'] + l:filenames | |
103 call s:run_perforce_command(l:cmd) | |
104 silent edit | |
105 endfunction | |
106 | |
107 " }}} | |
108 |