Mercurial > vim-p44vim
annotate autoload/p44vim.vim @ 1:953baa4a16bb
Print P4's output when trace is enabled.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 24 Sep 2020 22:48:03 -0700 |
parents | 4aede5f1af45 |
children | 74b2ef146e82 |
rev | line source |
---|---|
0 | 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) | |
1
953baa4a16bb
Print P4's output when trace is enabled.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
23 let l:cmd_out = system(l:strcmd) |
953baa4a16bb
Print P4's output when trace is enabled.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
24 if g:p44v_trace |
953baa4a16bb
Print P4's output when trace is enabled.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
25 call s:trace(l:cmd_out) |
953baa4a16bb
Print P4's output when trace is enabled.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
26 endif |
953baa4a16bb
Print P4's output when trace is enabled.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
27 return l:cmd_out |
0 | 28 endfunction |
29 | |
30 function! s:get_p4_depot_root(path) abort | |
31 let l:cur = a:path | |
32 let l:prev_cur = '' | |
33 while l:cur != l:prev_cur | |
34 if filereadable(l:cur.'/.p4config') || | |
35 \filereadable(l:cur.'/.p4ignore') | |
36 return l:cur | |
37 endif | |
38 let l:prev_cur = l:cur | |
39 let l:cur = fnamemodify(l:cur, ':h') | |
40 endwhile | |
41 call s:throw("No p4 depot found at: ".a:path) | |
42 endfunction | |
43 | |
44 " }}} | |
45 | |
46 " Auto-commands {{{ | |
47 | |
48 let s:ignore_next_w12 = 0 | |
49 | |
50 function! s:auto_edit_buffer() abort | |
51 call p44vim#p4edit() | |
52 endfunction | |
53 | |
54 function! s:maybe_ignore_w12() abort | |
55 if s:ignore_next_w12 | |
56 let v:fcs_choice = '' " Ignore the warning, keep the file. | |
57 let s:ignore_next_w12 = 0 | |
58 endif | |
59 endfunction | |
60 | |
61 function! p44vim#install_p4_auto_commands() abort | |
62 call s:trace("Scanning buffer '".bufname('%')."' for Perforce setup...") | |
63 try | |
64 let l:repo_root = s:get_p4_depot_root(expand('%:h')) | |
65 catch /^p44vim\:/ | |
66 return | |
67 endtry | |
68 | |
69 let b:p44v_repo_root = l:repo_root | |
70 call s:trace("Setting up P4 auto-commands for: ".bufname('%')) | |
71 | |
72 augroup p44v_auto | |
73 autocmd! | |
74 autocmd FileChangedRO * call <SID>auto_edit_buffer() | |
75 autocmd FileChangedShell * call <SID>maybe_ignore_w12() | |
76 augroup END | |
77 endfunction | |
78 | |
79 " }}} | |
80 | |
81 " Commands {{{ | |
82 | |
83 function! p44vim#p4sync(...) abort | |
84 let l:cmd = ['sync'] + a:000 | |
85 call s:run_perforce_command(l:cmd) | |
86 endfunction | |
87 | |
88 function! p44vim#p4edit(...) abort | |
89 if a:0 | |
90 let l:filenames = a:000 | |
91 else | |
92 let l:filenames = [expand('%:p')] | |
93 endif | |
94 let l:cmd = ['edit'] + l:filenames | |
95 let l:ignore_next_w12 = 1 | |
96 call s:run_perforce_command(l:cmd) | |
97 set noreadonly | |
98 endfunction | |
99 | |
100 function! p44vim#p4revert(...) abort | |
101 if a:0 | |
102 let l:filenames = a:000 | |
103 else | |
104 let l:filenames = [expand('%:p')] | |
105 endif | |
106 let l:cmd = ['revert'] + l:filenames | |
107 call s:run_perforce_command(l:cmd) | |
108 silent edit | |
109 endfunction | |
110 | |
111 " }}} | |
112 |