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