handle &sel of 'exclusive' in split annotation
[vcscommand.git] / plugin / vcssvn.vim
blobf45682937c115b824731789986c45883b00e0914
1 " vim600: set foldmethod=marker:
3 " SVN extension for VCSCommand.
5 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
6 " License:
7 " Copyright (c) Bob Hiestand
9 " Permission is hereby granted, free of charge, to any person obtaining a copy
10 " of this software and associated documentation files (the "Software"), to
11 " deal in the Software without restriction, including without limitation the
12 " rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13 " sell copies of the Software, and to permit persons to whom the Software is
14 " furnished to do so, subject to the following conditions:
16 " The above copyright notice and this permission notice shall be included in
17 " all copies or substantial portions of the Software.
19 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 " IN THE SOFTWARE.
27 " Section: Documentation {{{1
29 " Options documentation: {{{2
31 " VCSCommandSVNExec
32 "   This variable specifies the SVN executable.  If not set, it defaults to
33 "   'svn' executed from the user's executable path.
35 " VCSCommandSVNDiffExt
36 "   This variable, if set, sets the external diff program used by Subversion.
38 " VCSCommandSVNDiffOpt
39 "   This variable, if set, determines the options passed to the svn diff
40 "   command (such as 'u', 'w', or 'b').
42 " Section: Plugin header {{{1
44 if exists('VCSCommandDisableAll')
45         finish
46 endif
48 if v:version < 700
49         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
50         finish
51 endif
53 runtime plugin/vcscommand.vim
55 if !executable(VCSCommandGetOption('VCSCommandSVNExec', 'svn'))
56         " SVN is not installed
57         finish
58 endif
60 let s:save_cpo=&cpo
61 set cpo&vim
63 " Section: Variable initialization {{{1
65 let s:svnFunctions = {}
67 " Section: Utility functions {{{1
69 " Function: s:Executable() {{{2
70 " Returns the executable used to invoke git suitable for use in a shell
71 " command.
72 function! s:Executable()
73         return VCSCommandGetOption('VCSCommandSVNExec', 'svn')
74 endfunction
76 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
77 " Wrapper to VCSCommandDoCommand to add the name of the SVN executable to the
78 " command argument.
79 function! s:DoCommand(cmd, cmdName, statusText, options)
80         if VCSCommandGetVCSType(expand('%')) == 'SVN'
81                 let fullCmd = s:Executable() . ' ' . a:cmd
82                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
83         else
84                 throw 'SVN VCSCommand plugin called on non-SVN item.'
85         endif
86 endfunction
88 " Section: VCS function implementations {{{1
90 " Function: s:svnFunctions.Identify(buffer) {{{2
91 function! s:svnFunctions.Identify(buffer)
92         let fileName = resolve(bufname(a:buffer))
93         if isdirectory(fileName)
94                 let directoryName = fileName
95         else
96                 let directoryName = fnamemodify(fileName, ':h')
97         endif
98         if strlen(directoryName) > 0
99                 let svnDir = directoryName . '/.svn'
100         else
101                 let svnDir = '.svn'
102         endif
103         if isdirectory(svnDir)
104                 return 1
105         else
106                 return 0
107         endif
108 endfunction
110 " Function: s:svnFunctions.Add() {{{2
111 function! s:svnFunctions.Add(argList)
112         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
113 endfunction
115 " Function: s:svnFunctions.Annotate(argList) {{{2
116 function! s:svnFunctions.Annotate(argList)
117         if len(a:argList) == 0
118                 if &filetype ==? 'svnannotate'
119                         " Perform annotation of the version indicated by the current line.
120                         let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
121                         let options = ' -r' . caption
122                 else
123                         let caption = ''
124                         let options = ''
125                 endif
126         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
127                 let caption = a:argList[0]
128                 let options = ' -r' . caption
129         else
130                 let caption = join(a:argList, ' ')
131                 let options = ' ' . caption
132         endif
134         return s:DoCommand('blame --non-interactive' . options, 'annotate', caption, {})
135 endfunction
137 " Function: s:svnFunctions.Commit(argList) {{{2
138 function! s:svnFunctions.Commit(argList)
139         let resultBuffer = s:DoCommand('commit --non-interactive -F "' . a:argList[0] . '"', 'commit', '', {})
140         if resultBuffer == 0
141                 echomsg 'No commit needed.'
142         endif
143 endfunction
145 " Function: s:svnFunctions.Delete() {{{2
146 function! s:svnFunctions.Delete(argList)
147         return s:DoCommand(join(['delete --non-interactive'] + a:argList, ' '), 'delete', join(a:argList, ' '), {})
148 endfunction
150 " Function: s:svnFunctions.Diff(argList) {{{2
151 function! s:svnFunctions.Diff(argList)
152         if len(a:argList) == 0
153                 let revOptions = []
154                 let caption = ''
155         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
156                 let revOptions = ['-r' . join(a:argList, ':')]
157                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
158         else
159                 " Pass-through
160                 let caption = join(a:argList, ' ')
161                 let revOptions = a:argList
162         endif
164         let svnDiffExt = VCSCommandGetOption('VCSCommandSVNDiffExt', '')
165         if svnDiffExt == ''
166                 let diffExt = []
167         else
168                 let diffExt = ['--diff-cmd ' . svnDiffExt]
169         endif
171         let svnDiffOpt = VCSCommandGetOption('VCSCommandSVNDiffOpt', '')
172         if svnDiffOpt == ''
173                 let diffOptions = []
174         else
175                 let diffOptions = ['-x -' . svnDiffOpt]
176         endif
178         return s:DoCommand(join(['diff --non-interactive'] + diffExt + diffOptions + revOptions), 'diff', caption, {})
179 endfunction
181 " Function: s:svnFunctions.GetBufferInfo() {{{2
182 " Provides version control details for the current file.  Current version
183 " number and current repository version number are required to be returned by
184 " the vcscommand plugin.
185 " Returns: List of results:  [revision, repository, branch]
187 function! s:svnFunctions.GetBufferInfo()
188         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
189         let fileName = bufname(originalBuffer)
190         let statusText = s:VCSCommandUtility.system(s:Executable() . ' status --non-interactive -vu -- "' . fileName . '"')
191         if(v:shell_error)
192                 return []
193         endif
195         " File not under SVN control.
196         if statusText =~ '^?'
197                 return ['Unknown']
198         endif
200         let [flags, revision, repository] = matchlist(statusText, '^\(.\{9}\)\s*\(\d\+\)\s\+\(\d\+\)')[1:3]
201         if revision == ''
202                 " Error
203                 return ['Unknown']
204         elseif flags =~ '^A'
205                 return ['New', 'New']
206         elseif flags =~ '*'
207                 return [revision, repository, '*']
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 --non-interactive'] + 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 --non-interactive'] + 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 --non-interactive', '-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         return s:DoCommand('cat --non-interactive' . versionOption, 'review', versiontag, {})
257 endfunction
259 " Function: s:svnFunctions.Status(argList) {{{2
260 function! s:svnFunctions.Status(argList)
261         let options = ['-u', '-v']
262         if len(a:argList) != 0
263                 let options = a:argList
264         endif
265         return s:DoCommand(join(['status --non-interactive'] + options, ' '), 'status', join(options, ' '), {})
266 endfunction
268 " Function: s:svnFunctions.Unlock(argList) {{{2
269 function! s:svnFunctions.Unlock(argList)
270         return s:DoCommand(join(['unlock --non-interactive'] + a:argList, ' '), 'unlock', join(a:argList, ' '), {})
271 endfunction
273 " Function: s:svnFunctions.Update(argList) {{{2
274 function! s:svnFunctions.Update(argList)
275         return s:DoCommand('update --non-interactive', 'update', '', {})
276 endfunction
278 " Annotate setting {{{2
279 let s:svnFunctions.AnnotateSplitRegex = '\s\+\S\+\s\+\S\+ '
281 " Section: Plugin Registration {{{1
282 let s:VCSCommandUtility = VCSCommandRegisterModule('SVN', expand('<sfile>'), s:svnFunctions, [])
284 let &cpo = s:save_cpo