Added 'options' Dict to VCSCommandDoCommand for tweaking framework behavior.
[vcscommand.git] / plugin / vcssvn.vim
blobe81485e05ca54844702636b33c11c33aee5353ac
1 " vim600: set foldmethod=marker:
3 " SVN extension for VCSCommand.
5 " Version:       VCS development
6 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
7 " License:
8 " Copyright (c) 2007 Bob Hiestand
10 " Permission is hereby granted, free of charge, to any person obtaining a copy
11 " of this software and associated documentation files (the "Software"), to
12 " deal in the Software without restriction, including without limitation the
13 " rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14 " sell copies of the Software, and to permit persons to whom the Software is
15 " furnished to do so, subject to the following conditions:
17 " The above copyright notice and this permission notice shall be included in
18 " all copies or substantial portions of the Software.
20 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 " IN THE SOFTWARE.
28 " Section: Documentation {{{1
30 " Options documentation: {{{2
32 " VCSCommandSVNExec
33 "   This variable specifies the SVN executable.  If not set, it defaults to
34 "   'svn' executed from the user's executable path.
36 " VCSCommandSVNDiffExt
37 "   This variable, if set, sets the external diff program used by Subversion.
39 " VCSCommandSVNDiffOpt
40 "   This variable, if set, determines the options passed to the svn diff
41 "   command (such as 'u', 'w', or 'b').
43 " Section: Plugin header {{{1
45 if v:version < 700
46   echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
47   finish
48 endif
50 runtime plugin/vcscommand.vim
52 if !executable(VCSCommandGetOption('VCSCommandSVNExec', 'svn'))
53   " SVN is not installed
54   finish
55 endif
57 let s:save_cpo=&cpo
58 set cpo&vim
60 " Section: Variable initialization {{{1
62 let s:svnFunctions = {}
64 " Section: Utility functions {{{1
66 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
67 " Wrapper to VCSCommandDoCommand to add the name of the SVN executable to the
68 " command argument.
69 function! s:DoCommand(cmd, cmdName, statusText)
70   if VCSCommandGetVCSType(expand('%')) == 'SVN'
71     let fullCmd = VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' ' . a:cmd
72     return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
73   else
74     throw 'SVN VCSCommand plugin called on non-SVN item.'
75   endif
76 endfunction
78 " Section: VCS function implementations {{{1
80 " Function: s:svnFunctions.Identify(buffer) {{{2
81 function! s:svnFunctions.Identify(buffer)
82   let fileName = resolve(bufname(a:buffer))
83   if isdirectory(fileName)
84     let directoryName = fileName
85   else
86     let directoryName = fnamemodify(fileName, ':h')
87   endif
88   if strlen(directoryName) > 0
89     let svnDir = directoryName . '/.svn'
90   else
91     let svnDir = '.svn'
92   endif
93   if isdirectory(svnDir)
94     return 1
95   else
96     return 0
97   endif
98 endfunction
100 " Function: s:svnFunctions.Add() {{{2
101 function! s:svnFunctions.Add(argList)
102   return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
103 endfunction
105 " Function: s:svnFunctions.Annotate(argList) {{{2
106 function! s:svnFunctions.Annotate(argList)
107   if len(a:argList) == 0
108     if &filetype == 'SVNAnnotate'
109       " Perform annotation of the version indicated by the current line.
110       let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
111       let options = ' -r' . caption
112     else
113       let caption = ''
114       let options = ''
115     endif
116   elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
117     let caption = a:argList[0]
118     let options = ' -r' . caption
119   else
120     let caption = join(a:argList, ' ')
121     let options = ' ' . caption
122   endif
124   let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
125   if resultBuffer > 0
126     set filetype=SVNAnnotate
127   endif
128   return resultBuffer
129 endfunction
131 " Function: s:svnFunctions.Commit(argList) {{{2
132 function! s:svnFunctions.Commit(argList)
133   let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
134   if resultBuffer == 0
135     echomsg 'No commit needed.'
136   endif
137 endfunction
139 " Function: s:svnFunctions.Delete() {{{2
140 function! s:svnFunctions.Delete(argList)
141   return s:DoCommand(join(['delete'] + a:argList, ' '), 'delete', join(a:argList, ' '), {})
142 endfunction
144 " Function: s:svnFunctions.Diff(argList) {{{2
145 function! s:svnFunctions.Diff(argList)
146   if len(a:argList) == 0
147     let revOptions = [] 
148     let caption = ''
149   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
150     let revOptions = ['-r' . join(a:argList, ':')]
151     let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
152   else
153     " Pass-through
154     let caption = join(a:argList, ' ')
155     let revOptions = a:argList
156   endif
158   let svnDiffExt = VCSCommandGetOption('VCSCommandSVNDiffExt', '')
159   if svnDiffExt == ''
160     let diffExt = []
161   else
162     let diffExt = ['--diff-cmd ' . svnDiffExt]
163   endif
165   let svnDiffOpt = VCSCommandGetOption('VCSCommandSVNDiffOpt', '')
166   if svnDiffOpt == ''
167     let diffOptions = []
168   else
169     let diffOptions = ['-x -' . svnDiffOpt]
170   endif
172   let resultBuffer = s:DoCommand(join(['diff'] + diffExt + diffOptions + revOptions), 'diff', caption, {})
173   if resultBuffer > 0
174     set filetype=diff
175   else
176     if svnDiffExt == ''
177       echomsg 'No differences found'
178     endif
179   endif
180   return resultBuffer
181 endfunction
183 " Function: s:svnFunctions.GetBufferInfo() {{{2
184 " Provides version control details for the current file.  Current version
185 " number and current repository version number are required to be returned by
186 " the vcscommand plugin.
187 " Returns: List of results:  [revision, repository, branch]
189 function! s:svnFunctions.GetBufferInfo()
190   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
191   let fileName = bufname(originalBuffer)
192   let statusText = system(VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' status -vu "' . fileName . '"')
193   if(v:shell_error)
194     return []
195   endif
197   " File not under SVN control.
198   if statusText =~ '^?'
199     return ['Unknown']
200   endif
202   let [flags, revision, repository] = matchlist(statusText, '^\(.\{8}\)\s\+\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)\s')[1:3]
203   if revision == ''
204     " Error
205     return ['Unknown']
206   elseif flags =~ '^A'
207     return ['New', 'New']
208   else
209     return [revision, repository]
210   endif
211 endfunction
213 " Function: s:svnFunctions.Info(argList) {{{2
214 function! s:svnFunctions.Info(argList)
215   return s:DoCommand(join(['info'] + a:argList, ' '), 'info', join(a:argList, ' '), {})
216 endfunction
218 " Function: s:svnFunctions.Lock(argList) {{{2
219 function! s:svnFunctions.Lock(argList)
220   return s:DoCommand(join(['lock'] + a:argList, ' '), 'lock', join(a:argList, ' '), {})
221 endfunction
223 " Function: s:svnFunctions.Log(argList) {{{2
224 function! s:svnFunctions.Log(argList)
225   if len(a:argList) == 0
226     let options = []
227     let caption = ''
228   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
229     let options = ['-r' . join(a:argList, ':')]
230     let caption = options[0]
231   else
232     " Pass-through
233     let options = a:argList
234     let caption = join(a:argList, ' ')
235   endif
237   let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
238   return resultBuffer
239 endfunction
241 " Function: s:svnFunctions.Revert(argList) {{{2
242 function! s:svnFunctions.Revert(argList)
243   return s:DoCommand('revert', 'revert', '', {})
244 endfunction
246 " Function: s:svnFunctions.Review(argList) {{{2
247 function! s:svnFunctions.Review(argList)
248   if len(a:argList) == 0
249     let versiontag = '(current)'
250     let versionOption = ''
251   else
252     let versiontag = a:argList[0]
253     let versionOption = ' -r ' . versiontag . ' '
254   endif
256   let resultBuffer = s:DoCommand('cat' . versionOption, 'review', versiontag, {})
257   if resultBuffer > 0
258     let &filetype = getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
259   endif
260   return resultBuffer
261 endfunction
263 " Function: s:svnFunctions.Status(argList) {{{2
264 function! s:svnFunctions.Status(argList)
265   let options = ['-u', '-v']
266   if len(a:argList) == 0
267     let options = a:argList
268   endif
269   return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
270 endfunction
272 " Function: s:svnFunctions.Unlock(argList) {{{2
273 function! s:svnFunctions.Unlock(argList)
274   return s:DoCommand(join(['unlock'] + a:argList, ' '), 'unlock', join(a:argList, ' '), {})
275 endfunction
276 " Function: s:svnFunctions.Update(argList) {{{2
277 function! s:svnFunctions.Update(argList)
278   return s:DoCommand('update', 'update', '', {})
279 endfunction
281 " Section: Plugin Registration {{{1
282 call VCSCommandRegisterModule('SVN', expand('<sfile>'), s:svnFunctions, [])
284 let &cpo = s:save_cpo