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