9319906d7a7ac4cc9ed21c6a8b7bf731a4cc94b2
[profile.git] / .vim / script / p4
1 " $Id$ vim: se syntax=vim:
2 " Define the function we'll use.  It's easier to do this than to faff about 
3 " trying to run a sequence of normal mode commands.
4 fun! P4submit()
5   " There's no point doing this if the spec is readonly.  That probably means 
6   " it's a submitted change.
7   if ! &ro
8     " Clear the unnamed register.
9     let @" = ""
10     try
11       " Delete the spec blurb if it exists.
12       /^# A Perforce Change Spec/,/^$/d
13       " If the blurb was deleted the text will be in the unnamed register.
14       if @" != ""
15         " Allow closing an unmodified spec.
16         set nomod
17
18         " Get the files in the changelist.
19         let l:files = system("sed -n 's@^       \\(//.*\\)      #.*@\\1@p' " . bufname(""))
20         " Create a new window, move it below the spec and read in a diff.
21         botright new
22         if l:files != ""
23           exe "r!env P4DIFF= p4 diff -du " .  substitute(l:files, "\n", " ", "g")
24         endif
25
26         setf diff
27
28         " Delete the blank line left above the diff.
29         1
30         delete
31         set nobuflisted
32         set buftype=nowrite
33         set bufhidden=hide
34         setlocal noswapfile
35         file [p4 diff]
36
37         " Switch back to the top window and put the cursor by the description.
38         wincmd k
39         resize 10
40         exe "normal G"
41         ?<enter description here>
42         let @/ = ""
43       endif
44     catch
45       " We get here if no blurb was deleted.  This is the case if we aren't 
46       " editing a change.
47     endtry
48   endif
49 endfun
50
51 " Expand the command line window so we don't have to press RETURN later.
52 let s:cmdheight = &cmdheight
53 set cmdheight=3
54
55 " Call and then discard our function.
56 call P4submit()
57 delfunction P4submit
58 " Don't expand tabs in the spec.
59 set noexpandtab
60
61 " Restore the command window.
62 exe "set cmdheight=" . s:cmdheight
63
64 " Close the scratch buffer.
65 if version >= 700
66   " Vim 7 lets us close the buffer by number.  The changelist is always in #1.
67   au BufWinLeave <buffer=1> qa!
68 else
69   " Prior versions can only do it by name.  Fall back to a temporary file.
70   au BufWinLeave /tmp/tmp.*.* qa!
71 endif