4badc84029925984011311b87ccf4034218cd2cf
[profile.git] / .vim / script / p4
1 " Define the function we'll use.  It's easier to do this than to faff about 
2 " trying to run a sequence of normal mode commands.
3 fun! P4submit()
4   " There's no point doing this if the spec is readonly.  That probably means 
5   " it's a submitted change.
6   if ! &ro
7     setf perforce
8
9     " Clear the unnamed register.
10     let @" = ""
11     try
12       " Delete the spec blurb if it exists.
13       /^# A Perforce Change Spec/,/^# *(New changelists only\.)$/d
14       " If the blurb was deleted the text will be in the unnamed register.
15       if @" != ""
16         " Delete the blank line left above the remaining fields.
17         1
18         delete
19
20         " Allow closing an unmodified spec.
21         set nomod
22
23         try
24           /everything below this line is just the diff/
25
26           " We are submitting through git p4 so yank the diff.
27           normal j
28           normal yG
29           " Create a new window below the spec and paste the diff.
30           botright new
31           normal p
32         catch
33           " Get the files in the changelist.
34           let l:files = system("sed -n 's@^     \\(//.*\\)      #.*@\"\\1\"@p' " . bufname(""))
35           " Create a new window below the spec and read in a diff.
36           botright new
37           if l:files != ""
38             exe "r!env P4DIFF= p4 diff -du " .  substitute(l:files, "\n", " ", "g")
39           endif
40         endtry
41
42         setf diff
43
44         " Delete the blank line left above the diff.
45         1
46         delete
47         set nobuflisted
48         set buftype=nowrite
49         set bufhidden=hide
50         setlocal noswapfile
51         file [p4 diff\]
52         " Map q to quit easily.
53         nnoremap <silent> <buffer> q <C-W>q
54
55         " Vim 7.4 really doesn't want us to quit windows from a script.
56         try
57           au QuitPre * qa!
58         catch
59         endtry
60
61         " Switch back to the top window and put the cursor by the description.
62         wincmd k
63         resize 10
64         normal G
65         ?^Description:
66         let @/ = ""
67         normal j
68         normal ^
69
70         " Set formatting.
71         se noexpandtab
72         se ts=8
73       endif
74     catch
75       " We get here if no blurb was deleted.  This is the case if we aren't 
76       " editing a change.
77     endtry
78   endif
79 endfun
80
81 " Expand the command line window so we don't have to press RETURN later.
82 let s:cmdheight = &cmdheight
83 set cmdheight=3
84
85 " Call and then discard our function.
86 call P4submit()
87 delfunction P4submit
88 " Don't expand tabs in the spec.
89 set noexpandtab
90
91 " Restore the command window.
92 exe "set cmdheight=" . s:cmdheight
93
94 " Close the scratch buffer.
95 if version >= 700
96   " Vim 7 lets us close the buffer by number.  The changelist is always in #1.
97   au BufWinLeave <buffer=1> qa!
98 else
99   " Prior versions can only do it by name.  Fall back to a temporary file.
100   au BufWinLeave /tmp/tmp.*.* qa!
101 endif