Put the cursor over the description placeholder.
[profile.git] / .vim / script / p4
index 2195813..56958f8 100644 (file)
@@ -1,21 +1,93 @@
-:if ! &ro
-:try
-  /^# A Perforce Change Spec/,/^$/d
-
-  :set nomod
-
-  :wincmd n
-  :wincmd J
-  :r!p4 diff
-  :1
-  :delete
-
-  :setf diff
-  :set nomod
-
-  :wincmd k
-  /<enter description here>
-  :let @/ = ""
-:catch
-:endtry
-:endif
+" Define the function we'll use.  It's easier to do this than to faff about 
+" trying to run a sequence of normal mode commands.
+fun! P4submit()
+  " There's no point doing this if the spec is readonly.  That probably means 
+  " it's a submitted change.
+  if ! &ro
+    setf perforce
+
+    " Clear the unnamed register.
+    let @" = ""
+    try
+      " Delete the spec blurb if it exists.
+      /^# A Perforce Change Spec/,/^# *(New changelists only\.)$/d
+      " If the blurb was deleted the text will be in the unnamed register.
+      if @" != ""
+        " Delete the blank line left above the remaining fields.
+        1
+        delete
+
+        " Allow closing an unmodified spec.
+        set nomod
+
+        try
+          /everything below this line is just the diff/
+
+          " We are submitting through git p4 so yank the diff.
+          normal j
+          normal yG
+          " Create a new window below the spec and paste the diff.
+          botright new
+          normal p
+        catch
+          " Get the files in the changelist.
+          let l:files = system("sed -n 's@^    \\(//.*\\)      #.*@\"\\1\"@p' " . bufname(""))
+          " Create a new window below the spec and read in a diff.
+          botright new
+          if l:files != ""
+            exe "r!env P4DIFF= p4 diff -du " .  substitute(l:files, "\n", " ", "g")
+          endif
+        endtry
+
+        setf diff
+
+        " Delete the blank line left above the diff.
+        1
+        delete
+        set nobuflisted
+        set buftype=nowrite
+        set bufhidden=hide
+        setlocal noswapfile
+        file [p4 diff\]
+
+        " Switch back to the top window and put the cursor by the description.
+        wincmd k
+        resize 10
+        normal G
+        ?^Description:
+        let @/ = ""
+        normal j
+        normal ^
+
+        " Set formatting.
+        se noexpandtab
+        se ts=8
+      endif
+    catch
+      " We get here if no blurb was deleted.  This is the case if we aren't 
+      " editing a change.
+    endtry
+  endif
+endfun
+
+" Expand the command line window so we don't have to press RETURN later.
+let s:cmdheight = &cmdheight
+set cmdheight=3
+
+" Call and then discard our function.
+call P4submit()
+delfunction P4submit
+" Don't expand tabs in the spec.
+set noexpandtab
+
+" Restore the command window.
+exe "set cmdheight=" . s:cmdheight
+
+" Close the scratch buffer.
+if version >= 700
+  " Vim 7 lets us close the buffer by number.  The changelist is always in #1.
+  au BufWinLeave <buffer=1> qa!
+else
+  " Prior versions can only do it by name.  Fall back to a temporary file.
+  au BufWinLeave /tmp/tmp.*.* qa!
+endif