New Vim script for P4 merge.
[profile.git] / .vim / script / merge
diff --git a/.vim/script/merge b/.vim/script/merge
new file mode 100644 (file)
index 0000000..ad7c083
--- /dev/null
@@ -0,0 +1,83 @@
+" 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! P4merge()
+  " There should be four arguments:
+  " 0. The original file (tmp.xxx).
+  " 1. Their version (tmp.yyy).
+  " 2. My version (original filename).
+  " 3. The merged version (tmp.zzz).
+
+  if argc() != 4
+    return
+  endif
+
+  let l:filename = fnameescape(fnamemodify(argv(2), ":h"))
+
+  " Make sure the window is big enough for diffs.
+  if &columns < 165
+    let &columns=(&columns * 2)
+  endif
+
+  " Mark the buffer for the original file.
+  set ro
+  set nomodifiable
+  set nobuflisted
+  set buftype=nowrite
+  set bufhidden=hide
+  setlocal noswapfile
+  exe "file \[ORIG:" . l:filename . "\]"
+  diffthis
+  1
+
+  " Split the buffer for my version.
+  split +buffer\ 3
+  set ro
+  set nomodifiable
+  set nobuflisted
+  set buftype=nowrite
+  set bufhidden=hide
+  setlocal noswapfile
+  exe "file \[YOURS:" . l:filename . "\]"
+  diffthis
+  1
+
+  " Split the buffer for their version and put it below.
+  split +buffer\ 2
+  wincmd J
+  set ro
+  set nomodifiable
+  set nobuflisted
+  set buftype=nowrite
+  set bufhidden=hide
+  setlocal noswapfile
+  exe "file \[THEIRS:" . l:filename . "\]"
+  diffthis
+  1
+
+  " Split the buffer for the merged version and put it at the side.
+  split +buffer\ 4
+  wincmd L
+  se modifiable
+  1
+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 P4merge()
+delfunction P4merge
+
+" 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 merge is always in #4.
+  au BufWinLeave <buffer=4> qa!
+else
+  " Prior versions can only do it by name.  Fall back to a temporary file.
+  au BufWinLeave /tmp/tmp.*.* qa!
+endif
+