1 " Filename: diffchanges.vim
2 " Description: Shows the changes made to the current buffer in a diff format
3 " Maintainer: Jeremy Cantrell <jmcantrell@gmail.com>
4 " Last Modified: Fri 2008-02-29 14:17:06 (-0500)
10 if exists("loaded_diffchanges")
14 let loaded_diffchanges = 1
16 let g:diffchanges_diff = []
17 let g:diffchanges_patch = []
18 let s:diffchanges_modes = ['diff', 'patch']
20 if !exists('g:diffchanges_patch_cmd')
21 let g:diffchanges_patch_cmd = 'diff -u'
29 if !hasmapto('<Plug>DiffChangesDiffToggle')
30 nmap <silent> <unique> <leader>dcd <Plug>DiffChangesDiffToggle
33 if !hasmapto('<Plug>DiffChangesPatchToggle')
34 nmap <silent> <unique> <leader>dcp <Plug>DiffChangesPatchToggle
37 nnoremap <unique> <script> <Plug>DiffChangesDiffToggle <SID>DiffChangesDiffToggle
38 nnoremap <unique> <script> <Plug>DiffChangesPatchToggle <SID>DiffChangesPatchToggle
40 nnoremap <SID>DiffChangesDiffToggle :DiffChangesDiffToggle<cr>
41 nnoremap <SID>DiffChangesPatchToggle :DiffChangesPatchToggle<cr>
43 command -bar DiffChangesDiffToggle :call s:DiffChangesToggle('diff')
44 command -bar DiffChangesPatchToggle :call s:DiffChangesToggle('patch')
46 nnoremenu <script> &Plugin.&DiffChanges.&Diff\ Toggle <SID>DiffChangesDiffToggle
47 nnoremenu <script> &Plugin.&DiffChanges.&Patch\ Toggle <SID>DiffChangesPatchToggle
49 function! s:DiffChangesToggle(mode) "{{{1
50 if count(s:diffchanges_modes, a:mode) == 0
51 call s:Warn("Invalid mode '".a:mode."'")
54 if len(expand('%')) == 0
55 call s:Warn("Unable to show changes for unsaved buffer")
58 let [dcm, pair] = s:DiffChangesPair(bufnr('%'))
59 if count(s:diffchanges_modes, dcm) == 0
60 call s:DiffChangesOn(a:mode)
62 call s:DiffChangesOff()
66 function! s:DiffChangesOn(mode) "{{{1
67 if count(s:diffchanges_modes, a:mode) == 0
70 let filename = expand('%')
71 let diffname = tempname()
72 let buforig = bufnr('%')
74 call writefile(readfile(filename, 'b'), diffname, 'b')
75 let b:diffchanges_savefdm = &fdm
76 let b:diffchanges_savefdl = &fdl
81 execute '-read '.filename
83 let bufdiff = bufnr('%')
84 elseif a:mode == 'patch'
85 execute 'silent w! '.diffname
86 let diff = system(g:diffchanges_patch_cmd.' '.filename.' '.diffname)
89 call s:Warn('No changes found')
93 setlocal filetype=diff
94 setlocal foldmethod=manual
99 let bufname = "Changes made to '".filename."'"
100 silent file `=bufname`
101 autocmd BufUnload <buffer> call s:DiffChangesOff()
102 let bufdiff = bufnr('%')
103 call add(g:diffchanges_{a:mode}, [buforig, bufdiff])
106 function! s:DiffChangesOff() "{{{1
107 let [dcm, pair] = s:DiffChangesPair(bufnr('%'))
108 execute 'autocmd! BufUnload <buffer='.pair[1].'>'
109 execute 'bdelete! '.pair[1]
110 execute bufwinnr(pair[0]).'wincmd w'
113 let &fdm = b:diffchanges_savefdm
114 let &fdl = b:diffchanges_savefdl
116 let dcp = g:diffchanges_{dcm}
117 call remove(dcp, index(dcp, pair))
120 function! s:DiffChangesPair(buf_num) "{{{1
121 for dcm in s:diffchanges_modes
122 let pairs = g:diffchanges_{dcm}
124 if count(pair, a:buf_num) > 0
132 function! s:Warn(message) "{{{1
133 echohl WarningMsg | echo a:message | echohl None
136 function! s:Error(message) "{{{1
137 echohl ErrorMsg | echo a:message | echohl None
140 " Restore settings "{{{1
141 let &cpo = s:save_cpo