Set formatting in changelist window.
[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
53         " Switch back to the top window and put the cursor by the description.
54         wincmd k
55         resize 10
56         normal G
57         ?^Description:
58         let @/ = ""
59
60         " Set formatting.
61         se noexpandtab
62         se ts=8
63       endif
64     catch
65       " We get here if no blurb was deleted.  This is the case if we aren't 
66       " editing a change.
67     endtry
68   endif
69 endfun
70
71 " Expand the command line window so we don't have to press RETURN later.
72 let s:cmdheight = &cmdheight
73 set cmdheight=3
74
75 " Call and then discard our function.
76 call P4submit()
77 delfunction P4submit
78 " Don't expand tabs in the spec.
79 set noexpandtab
80
81 " Restore the command window.
82 exe "set cmdheight=" . s:cmdheight
83
84 " Close the scratch buffer.
85 if version >= 700
86   " Vim 7 lets us close the buffer by number.  The changelist is always in #1.
87   au BufWinLeave <buffer=1> qa!
88 else
89   " Prior versions can only do it by name.  Fall back to a temporary file.
90   au BufWinLeave /tmp/tmp.*.* qa!
91 endif