Mercurial > vim-p44vim
annotate autoload/p44vim.vim @ 3:14a272d72b0a
Only open file for edit if it's readonly just before writing to it.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 24 Sep 2020 22:50:09 -0700 |
parents | 74b2ef146e82 |
children | 52e1502091e0 |
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 | |
3
14a272d72b0a
Only open file for edit if it's readonly just before writing to it.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
51 function! s:maybe_auto_edit_buffer() abort |
14a272d72b0a
Only open file for edit if it's readonly just before writing to it.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
52 if &readonly |
14a272d72b0a
Only open file for edit if it's readonly just before writing to it.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
53 call p44vim#p4edit() |
14a272d72b0a
Only open file for edit if it's readonly just before writing to it.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
54 endif |
0 | 55 endfunction |
56 | |
57 function! s:maybe_ignore_w12() abort | |
58 if s:ignore_next_w12 | |
59 let v:fcs_choice = '' " Ignore the warning, keep the file. | |
60 let s:ignore_next_w12 = 0 | |
61 endif | |
62 endfunction | |
63 | |
64 function! p44vim#install_p4_auto_commands() abort | |
65 call s:trace("Scanning buffer '".bufname('%')."' for Perforce setup...") | |
66 try | |
67 let l:repo_root = s:get_p4_depot_root(expand('%:h')) | |
68 catch /^p44vim\:/ | |
69 return | |
70 endtry | |
71 | |
72 let b:p44v_repo_root = l:repo_root | |
73 call s:trace("Setting up P4 auto-commands for: ".bufname('%')) | |
74 | |
75 augroup p44v_auto | |
76 autocmd! | |
3
14a272d72b0a
Only open file for edit if it's readonly just before writing to it.
Ludovic Chabant <ludovic@chabant.com>
parents:
2
diff
changeset
|
77 autocmd BufWritePre * call <SID>maybe_auto_edit_buffer() |
0 | 78 autocmd FileChangedShell * call <SID>maybe_ignore_w12() |
79 augroup END | |
80 endfunction | |
81 | |
82 " }}} | |
83 | |
84 " Commands {{{ | |
85 | |
86 function! p44vim#p4sync(...) abort | |
87 let l:cmd = ['sync'] + a:000 | |
88 call s:run_perforce_command(l:cmd) | |
89 endfunction | |
90 | |
91 function! p44vim#p4edit(...) abort | |
92 if a:0 | |
93 let l:filenames = a:000 | |
94 else | |
95 let l:filenames = [expand('%:p')] | |
96 endif | |
97 let l:cmd = ['edit'] + l:filenames | |
98 let l:ignore_next_w12 = 1 | |
99 call s:run_perforce_command(l:cmd) | |
100 set noreadonly | |
101 endfunction | |
102 | |
103 function! p44vim#p4revert(...) abort | |
104 if a:0 | |
105 let l:filenames = a:000 | |
106 else | |
107 let l:filenames = [expand('%:p')] | |
108 endif | |
109 let l:cmd = ['revert'] + l:filenames | |
110 call s:run_perforce_command(l:cmd) | |
111 silent edit | |
112 endfunction | |
113 | |
114 " }}} | |
115 |