ad7c083941465fcf892ace31684e2dc8e39054f3
[profile.git] / .vim / script / merge
1 " Define the function we'll use.  It's easier to do this than to faff about 
2 " trying to run a sequence of normal mode commands.
3 fun! P4merge()
4   " There should be four arguments:
5   " 0. The original file (tmp.xxx).
6   " 1. Their version (tmp.yyy).
7   " 2. My version (original filename).
8   " 3. The merged version (tmp.zzz).
9
10   if argc() != 4
11     return
12   endif
13
14   let l:filename = fnameescape(fnamemodify(argv(2), ":h"))
15
16   " Make sure the window is big enough for diffs.
17   if &columns < 165
18     let &columns=(&columns * 2)
19   endif
20
21   " Mark the buffer for the original file.
22   set ro
23   set nomodifiable
24   set nobuflisted
25   set buftype=nowrite
26   set bufhidden=hide
27   setlocal noswapfile
28   exe "file \[ORIG:" . l:filename . "\]"
29   diffthis
30   1
31
32   " Split the buffer for my version.
33   split +buffer\ 3
34   set ro
35   set nomodifiable
36   set nobuflisted
37   set buftype=nowrite
38   set bufhidden=hide
39   setlocal noswapfile
40   exe "file \[YOURS:" . l:filename . "\]"
41   diffthis
42   1
43
44   " Split the buffer for their version and put it below.
45   split +buffer\ 2
46   wincmd J
47   set ro
48   set nomodifiable
49   set nobuflisted
50   set buftype=nowrite
51   set bufhidden=hide
52   setlocal noswapfile
53   exe "file \[THEIRS:" . l:filename . "\]"
54   diffthis
55   1
56
57   " Split the buffer for the merged version and put it at the side.
58   split +buffer\ 4
59   wincmd L
60   se modifiable
61   1
62 endfun
63
64 " Expand the command line window so we don't have to press RETURN later.
65 let s:cmdheight = &cmdheight
66 set cmdheight=3
67
68 " Call and then discard our function.
69 call P4merge()
70 delfunction P4merge
71
72 " Restore the command window.
73 exe "set cmdheight=" . s:cmdheight
74
75 " Close the scratch buffer.
76 if version >= 700
77   " Vim 7 lets us close the buffer by number.  The merge is always in #4.
78   au BufWinLeave <buffer=4> qa!
79 else
80   " Prior versions can only do it by name.  Fall back to a temporary file.
81   au BufWinLeave /tmp/tmp.*.* qa!
82 endif
83