1 " diffwin.vim: a simple way to use diff to compare two source files
2 " and to synchronize the three windows to the current,
3 " next, and previous difference blocks.
5 " Author : Charles E. Campbell, Jr. (Charles.E.Campbell.1@gsfc.nasa.gov)
8 " To enable: put this file into your <.vimrc> or source it from there.
9 " You may wish to modify the maps' temporary directory;
10 " its easiest to use vms's version: copy it, then :s/tmp:/newpath/g
12 " To use: start Vim as shown below, use \df to generate differences,
13 " and then hit the <F8> key:
15 " vim -o newfile oldfile
19 " The resulting three windows will look like this:
23 " |diff| *** oldfilename date
24 " +----+ --- newfilename date
25 " |new | ***************
27 " |old | how to convert new -> old (shows new stuff)
29 " how to convert old -> new (shows old stuff)
32 " You can synchronize the files in the new&old windows to the current
33 " difference-block being considered: just move the cursor in the diff
34 " window to the difference of interest and hit the "\dc". Use "\dn"
35 " (or "F8") and "\dp" to navigate to the next/previous difference block,
39 " \df : opens a third window on top with the diff file.
40 " \dc : synchronize windows to current diff, cursor at new->old diff section
41 " \dC : synchronize windows to current diff, cursor at old->new diff section
42 " \dn : synchronize windows to next diff
43 " \dp : synchronize windows to previous diff
44 " \ds : reSet diff (re-runs diff on new/old/files)
45 " \du : apply patch from down->up (old->new)
46 " \db : apply patch from up->bottom (new->old)
50 map \df :let lzs1=&lz<CR><C-W>k:let tmpfile=tempname()<CR>:exe "!diff -c ".expand("#1")." ".expand("#2").">".tmpfile<CR><C-W>s:exe "e ".tmpfile<CR>:exe "!/bin/rm -f ".tmpfile<CR>:unlet tmpfile<CR>:set ft=diff<CR>gg:let &lz=lzs1<CR>\dn
52 map \df :let lzs1=&lz<CR><C-W>k:let tmpfile=tempname()<CR>:exe "!diff -c ".expand("#1")." ".expand("#2").">".tmpfile<CR><C-W>s:exe "e ".tmpfile<CR>:exe "!erase ".tmpfile<CR>:unlet tmpfile<CR>:set ft=diff<CR>gg:let &lz=lzs1<CR>\dn
54 map \df :let lzs1=&lz<CR><C-W>k:let tmpfile=tempname()<CR>:exe "!diff -c ".expand("#1")." ".expand("#2").">".tmpfile<CR><C-W>s:exe "e ".tmpfile<CR>:exe "!del ".tmpfile.";*"<CR>:unlet tmpfile<CR>:set ft=diff<CR>gg:let &lz=lzs1<CR>\dn
57 map \df :let lzs1=&lz<CR><C-W>k<C-W>s:ene<CR>:exe "0r !diff -c ".expand("#1")." ".expand("#2")<CR>:set nomod<CR>:set ft=diff<CR>gg:let &lz=lzs1<CR>\dn
59 map \dc :let lzs3=&lz<CR><C-W>k<C-W>k?^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*$<CR>jYpdwf,DAGz<C-V><CR><Esc>"bYdd<C-W>j@b<C-W>k?^\*\*\*\*\*<CR>/^--- <CR>Ypdwf,DAGz<C-V><CR><Esc>"aYdd2<C-W>j@a2<C-W>k?^\*\*\* <CR>z<CR>:set nomod<CR>:let &lz=lzs3<CR>:echo "diff converts middle window to lower window"<CR>
60 map \dC \dc/^--- <CR>z<CR>:echo "diff converts lower window to middle window"<CR>
61 map \dn :let lzs4=&lz<CR><C-W>k<C-W>k/^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*$<CR>j\dc:let &lz=lzs4<CR>
62 map \dp :let lzs5=&lz<CR><C-W>k<C-W>k?^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*$<CR>?<CR>j\dc:let &lz=lzs5<CR>
64 map \ds :let lzs2=&lz<CR>:wa<CR>2<C-W>k:exe "0r !diff -c ".expand("#1")." ".expand("#2")<CR>:set nomod<CR>:set ft=diff<CR>gg:let &lz=lzs2<CR><CR>\dn
66 map \ds :let lzs2=&lz<CR>:wa<CR>2<C-W>k:exe "0r !diff -c ".expand("#1")." ".expand("#2")<CR>:set nomod<CR>gg:let &lz=lzs2<CR><CR>\dn
70 " ---------------------------------------------------------------------
72 " Functions didn't enter vim until Version 5.2
74 map \db :call DiffPatch(0)<CR>\ds
75 map \du :call DiffPatch(1)<CR>\ds
76 " DiffPatch: applies current patch section to newfile/oldfile
77 " Uses anonymous register
79 " old2new: =1 DiffPatch being used to convert old -> new
80 " =0 DiffPatch being used to convert new -> old
81 fu! DiffPatch(old2new)
87 exe "norm 2\<c-w>k?\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*$\<CR>j"
88 let newpat='\*\*\* \(\d\+\),\(\d\+\).*$'
89 let new1=substitute(getline("."),newpat,'\1',"")
90 let new2=substitute(getline("."),newpat,'\2',"")
92 exe "norm /^--- \<CR>"
93 let oldpat='--- \(\d\+\),\(\d\+\).*$'
94 let old1=substitute(getline("."),oldpat,'\1',"")
95 let old2=substitute(getline("."),oldpat,'\2',"")
98 exe "norm \<c-w>j:".new1.",".new2."d\<CR>"
99 exe "norm \<c-w>j:".old1.",".old2."y\<CR>"
101 exe "norm \<c-w>k".new1."Gp"
103 exe "norm 2\<c-w>j:".old1.",".old2."d\<CR>"
104 exe "norm \<c-w>k:".new1.",".new2."y\<CR>"
106 exe "norm \<c-w>j".old1."Gp"
113 " ---------------------------------------------------------------------