1 " vim600: set foldmethod=marker:
3 " SVN extension for VCSCommand.
5 " Version: VCS development
6 " Maintainer: Bob Hiestand <bob.hiestand@gmail.com>
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
28 " Section: Documentation {{{1
30 " Command documentation {{{2
32 " The following command only applies to files under SVN source control.
34 " SVNInfo Performs "svn info" on the current file.
36 " Mapping documentation: {{{2
38 " By default, a mapping is defined for each command. User-provided mappings
39 " can be used instead by mapping to <Plug>CommandName, for instance:
41 " nnoremap ,si <Plug>SVNInfo
43 " The default mappings are as follow:
47 " Options documentation: {{{2
50 " This variable specifies the SVN executable. If not set, it defaults to
51 " 'svn' executed from the user's executable path.
53 " VCSCommandSVNDiffExt
54 " This variable, if set, sets the external diff program used by Subversion.
56 " VCSCommandSVNDiffOpt
57 " This variable, if set, determines the options passed to the svn diff
58 " command (such as 'u', 'w', or 'b').
64 " Section: Variable initialization {{{1
66 let s:svnFunctions = {}
68 " Section: Utility functions {{{1
70 " Function: s:DoCommand(cmd, cmdName, statusText) {{{2
71 " Wrapper to VCSCommandDoCommand to add the name of the SVN executable to the
73 function! s:DoCommand(cmd, cmdName, statusText)
75 if VCSCommandGetVCSType(expand('%')) == 'SVN'
76 let fullCmd = VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' ' . a:cmd
77 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText)
79 throw 'No suitable plugin'
81 catch /No suitable plugin/
82 echohl WarningMsg|echomsg 'Cannot apply SVN commands to this file.'|echohl None
86 " Section: VCS function implementations {{{1
88 " Function: s:svnFunctions.Identify(buffer) {{{2
89 function! s:svnFunctions.Identify(buffer)
90 let fileName = resolve(bufname(a:buffer))
91 if isdirectory(fileName)
92 let directory = fileName
94 let directory = fnamemodify(fileName, ':h')
96 if strlen(directory) > 0
97 let svnDir = directory . '/.svn'
101 if isdirectory(svnDir)
108 " Function: s:svnFunctions.Add() {{{2
109 function! s:svnFunctions.Add(argList)
110 return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '))
113 " Function: s:svnFunctions.Annotate(argList) {{{2
114 function! s:svnFunctions.Annotate(argList)
115 if len(a:argList) == 0
116 if &filetype == 'SVNAnnotate'
117 " Perform annotation of the version indicated by the current line.
118 let revision = matchstr(getline('.'),'\v^\s+\zs\d+')
120 let revision=VCSCommandGetRevision()
122 throw 'Unable to obtain version information.'
123 elseif revision == 'Unknown'
124 throw 'File not under source control'
125 elseif revision == 'New'
126 throw 'No annotatation available for new file.'
130 let revision=a:argList[0]
133 let resultBuffer=s:DoCommand('blame -r' . revision, 'annotate', revision)
135 set filetype=SVNAnnotate
140 " Function: s:svnFunctions.Commit(argList) {{{2
141 function! s:svnFunctions.Commit(argList)
142 let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '')
144 echomsg 'No commit needed.'
148 " Function: s:svnFunctions.Delete() {{{2
149 function! s:svnFunctions.Delete(argList)
150 return s:DoCommand(join(['delete'] + a:argList, ' '), 'delete', join(a:argList, ' '))
153 " Function: s:svnFunctions.Diff(argList) {{{2
154 function! s:svnFunctions.Diff(argList)
155 if len(a:argList) == 1
156 let revOptions = ' -r' . a:argList[0]
157 let caption = '(' . a:argList[0] . ' : current)'
158 elseif len(a:argList) == 2
159 let revOptions = ' -r' . a:argList[0] . ':' . a:argList[1]
160 let caption = '(' . a:argList[0] . ' : ' . a:argList[1] . ')'
166 let svndiffext = VCSCommandGetOption('VCSCommandSVNDiffExt', '')
168 let diffextstring = ''
170 let diffextstring = ' --diff-cmd ' . svndiffext . ' '
173 let svndiffopt = VCSCommandGetOption('VCSCommandSVNDiffOpt', '')
176 let diffoptionstring = ''
178 let diffoptionstring = ' -x -' . svndiffopt . ' '
181 let resultBuffer = s:DoCommand('diff' . diffextstring . diffoptionstring . revOptions , 'diff', caption)
186 echomsg 'No differences found'
192 " Function: s:svnFunctions.GetBufferInfo() {{{2
193 " Provides version control details for the current file. Current version
194 " number and current repository version number are required to be returned by
195 " the vcscommand plugin.
196 " Returns: List of results: [revision, repository, branch]
198 function! s:svnFunctions.GetBufferInfo()
199 let originalBuffer=VCSCommandGetOriginalBuffer(bufnr('%'))
200 let fileName=bufname(originalBuffer)
201 let realFileName = fnamemodify(resolve(fileName), ':t')
202 if !filereadable(fileName)
205 let oldCwd=VCSCommandChangeToCurrentFileDir(fileName)
207 let statusText=system(VCSCommandGetOption('VCSCommandSVNExec', 'svn') . ' status -vu "' . realFileName . '"')
212 " File not under SVN control.
213 if statusText =~ '^?'
217 let [flags, revision, repository] = matchlist(statusText, '^\(.\{8}\)\s\+\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)\s')[1:3]
222 return ['New', 'New']
224 return [revision, repository]
227 execute 'cd' escape(oldCwd, ' ')
231 " Function: s:svnFunctions.Lock(argList) {{{2
232 function! s:svnFunctions.Lock(argList)
233 return s:DoCommand(join(['lock'] + a:argList, ' '), 'lock', join(a:argList, ' '))
236 " Function: s:svnFunctions.Log() {{{2
237 function! s:svnFunctions.Log(argList)
238 if len(a:argList) == 0
239 let versionOption = ''
241 elseif len(a:argList) == 1 && a:argList[0] !~ "^-"
242 let versionOption=' -r' . a:argList[0]
243 let caption = a:argList[0]
245 " Multiple options, or the option starts with '-'
246 let caption = join(a:argList, ' ')
247 let versionOption = ' ' . caption
250 let resultBuffer=s:DoCommand('log -v' . versionOption, 'log', caption)
254 " Function: s:svnFunctions.Revert(argList) {{{2
255 function! s:svnFunctions.Revert(argList)
256 return s:DoCommand('revert', 'revert', '')
259 " Function: s:svnFunctions.Review(argList) {{{2
260 function! s:svnFunctions.Review(argList)
261 if len(a:argList) == 0
262 let versiontag = '(current)'
263 let versionOption = ''
265 let versiontag = a:argList[0]
266 let versionOption = ' -r ' . versiontag . ' '
269 let resultBuffer = s:DoCommand('cat' . versionOption, 'review', versiontag)
271 let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
276 " Function: s:svnFunctions.Status(argList) {{{2
277 function! s:svnFunctions.Status(argList)
278 if len(a:argList) == 0
279 let a:argList = ['-u', '-v']
281 return s:DoCommand(join(['status -u -v'] + a:argList, ' '), 'status', join(a:argList, ' '))
284 " Function: s:svnFunctions.Unlock(argList) {{{2
285 function! s:svnFunctions.Unlock(argList)
286 return s:DoCommand(join(['unlock'] + a:argList, ' '), 'unlock', join(a:argList, ' '))
288 " Function: s:svnFunctions.Update(argList) {{{2
289 function! s:svnFunctions.Update(argList)
290 return s:DoCommand('update', 'update', '')
293 " Section: SVN-specific functions {{{1
295 " Function: s:SVNInfo() {{{2
296 function! s:SVNInfo(argList)
297 return s:DoCommand(join(['info'] + a:argList, ' '), 'svninfo', join(a:argList, ' '))
300 " Section: Command definitions {{{1
301 " Section: Primary commands {{{2
302 com! -nargs=* SVNInfo call s:SVNInfo([<f-args>])
304 " Section: Plugin command mappings {{{1
306 let s:svnExtensionMappings = {}
307 let mappingInfo = [['SVNInfo', 'SVNInfo', 'ci']]
308 for [pluginName, commandText, shortCut] in mappingInfo
309 execute 'nnoremap <silent> <Plug>' . pluginName . ' :' . commandText . '<CR>'
310 if !hasmapto('<Plug>' . pluginName)
311 let s:svnExtensionMappings[shortCut] = commandText
315 " Section: Menu items {{{1
316 amenu <silent> &Plugin.VCS.SVN.&Info <Plug>SVNInfo
318 " Section: Plugin Registration {{{1
319 " If the vcscommand.vim plugin hasn't loaded, delay registration until it
321 if exists('g:loaded_VCSCommand')
322 call VCSCommandRegisterModule('SVN', expand('<sfile>'), s:svnFunctions, s:svnExtensionMappings)
325 au User VCSLoadExtensions call VCSCommandRegisterModule('SVN', expand('<sfile>'), s:svnFunctions, s:svnExtensionMappings)