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