use case-insensitive tests for VCS file types
[vcscommand.git] / plugin / vcssvk.vim
blobf20e4545c27fbe5062d4c5c5baf0bbcf51aa79af
1 " vim600: set foldmethod=marker:
3 " SVK 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 " VCSCommandSVKExec
32 "   This variable specifies the SVK executable.  If not set, it defaults to
33 "   'svk' executed from the user's executable path.
35 " Section: Plugin header {{{1
37 if exists('VCSCommandDisableAll')
38         finish
39 endif
41 if v:version < 700
42         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
43         finish
44 endif
46 runtime plugin/vcscommand.vim
48 if !executable(VCSCommandGetOption('VCSCommandSVKExec', 'svk'))
49         " SVK is not installed
50         finish
51 endif
53 let s:save_cpo=&cpo
54 set cpo&vim
56 " Section: Variable initialization {{{1
58 let s:svkFunctions = {}
60 " Section: Utility functions {{{1
62 " Function: s:Executable() {{{2
63 " Returns the executable used to invoke SVK suitable for use in a shell
64 " command.
65 function! s:Executable()
66         return VCSCommandGetOption('VCSCommandSVKExec', 'svk')
67 endfunction
69 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
70 " Wrapper to VCSCommandDoCommand to add the name of the SVK executable to the
71 " command argument.
72 function! s:DoCommand(cmd, cmdName, statusText, options)
73         if VCSCommandGetVCSType(expand('%')) == 'SVK'
74                 let fullCmd = s:Executable() . ' ' . a:cmd
75                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
76         else
77                 throw 'SVK VCSCommand plugin called on non-SVK item.'
78         endif
79 endfunction
81 " Section: VCS function implementations {{{1
83 " Function: s:svkFunctions.Identify(buffer) {{{2
84 function! s:svkFunctions.Identify(buffer)
85         let fileName = resolve(bufname(a:buffer))
86         if isdirectory(fileName)
87                 let directoryName = fileName
88         else
89                 let directoryName = fnamemodify(fileName, ':p:h')
90         endif
91         let statusText = s:VCSCommandUtility.system(s:Executable() . ' info -- "' . directoryName . '"', "no")
92         if(v:shell_error)
93                 return 0
94         else
95                 return 1
96         endif
97 endfunction
99 " Function: s:svkFunctions.Add() {{{2
100 function! s:svkFunctions.Add(argList)
101         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
102 endfunction
104 " Function: s:svkFunctions.Annotate(argList) {{{2
105 function! s:svkFunctions.Annotate(argList)
106         if len(a:argList) == 0
107                 if &filetype ==? 'svkannotate'
108                         " Perform annotation of the version indicated by the current line.
109                         let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
110                         let options = ' -r' . caption
111                 else
112                         let caption = ''
113                         let options = ''
114                 endif
115         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
116                 let caption = a:argList[0]
117                 let options = ' -r' . caption
118         else
119                 let caption = join(a:argList, ' ')
120                 let options = ' ' . caption
121         endif
123         let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
124         if resultBuffer > 0
125                 normal! 1G2dd
126         endif
127         return resultBuffer
128 endfunction
130 " Function: s:svkFunctions.Commit(argList) {{{2
131 function! s:svkFunctions.Commit(argList)
132         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
133         if resultBuffer == 0
134                 echomsg 'No commit needed.'
135         endif
136 endfunction
138 " Function: s:svkFunctions.Delete() {{{2
139 function! s:svkFunctions.Delete(argList)
140         return s:DoCommand(join(['delete'] + a:argList, ' '), 'delete', join(a:argList, ' '), {})
141 endfunction
143 " Function: s:svkFunctions.Diff(argList) {{{2
144 function! s:svkFunctions.Diff(argList)
145         if len(a:argList) == 0
146                 let revOptions = []
147                 let caption = ''
148         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
149                 let revOptions = ['-r' . join(a:argList, ':')]
150                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
151         else
152                 " Pass-through
153                 let caption = join(a:argList, ' ')
154                 let revOptions = a:argList
155         endif
157         return s:DoCommand(join(['diff'] + revOptions), 'diff', caption, {})
158 endfunction
160 " Function: s:svkFunctions.GetBufferInfo() {{{2
161 " Provides version control details for the current file.  Current version
162 " number and current repository version number are required to be returned by
163 " the vcscommand plugin.
164 " Returns: List of results:  [revision, repository]
166 function! s:svkFunctions.GetBufferInfo()
167         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
168         let fileName = resolve(bufname(originalBuffer))
169         let statusText = s:VCSCommandUtility.system(s:Executable() . ' status -v -- "' . fileName . '"')
170         if(v:shell_error)
171                 return []
172         endif
174         " File not under SVK control.
175         if statusText =~ '^?'
176                 return ['Unknown']
177         endif
179         let [flags, revision, repository] = matchlist(statusText, '^\(.\{3}\)\s\+\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)\s')[1:3]
180         if revision == ''
181                 " Error
182                 return ['Unknown']
183         elseif flags =~ '^A'
184                 return ['New', 'New']
185         else
186                 return [revision, repository]
187         endif
188 endfunction
190 " Function: s:svkFunctions.Info(argList) {{{2
191 function! s:svkFunctions.Info(argList)
192         return s:DoCommand(join(['info'] + a:argList, ' '), 'info', join(a:argList, ' '), {})
193 endfunction
195 " Function: s:svkFunctions.Lock(argList) {{{2
196 function! s:svkFunctions.Lock(argList)
197         return s:DoCommand(join(['lock'] + a:argList, ' '), 'lock', join(a:argList, ' '), {})
198 endfunction
200 " Function: s:svkFunctions.Log() {{{2
201 function! s:svkFunctions.Log(argList)
202         if len(a:argList) == 0
203                 let options = []
204                 let caption = ''
205         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
206                 let options = ['-r' . join(a:argList, ':')]
207                 let caption = options[0]
208         else
209                 " Pass-through
210                 let options = a:argList
211                 let caption = join(a:argList, ' ')
212         endif
214         let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
215         return resultBuffer
216 endfunction
218 " Function: s:svkFunctions.Revert(argList) {{{2
219 function! s:svkFunctions.Revert(argList)
220         return s:DoCommand('revert', 'revert', '', {})
221 endfunction
223 " Function: s:svkFunctions.Review(argList) {{{2
224 function! s:svkFunctions.Review(argList)
225         if len(a:argList) == 0
226                 let versiontag = '(current)'
227                 let versionOption = ''
228         else
229                 let versiontag = a:argList[0]
230                 let versionOption = ' -r ' . versiontag . ' '
231         endif
233         return s:DoCommand('cat' . versionOption, 'review', versiontag, {})
234 endfunction
236 " Function: s:svkFunctions.Status(argList) {{{2
237 function! s:svkFunctions.Status(argList)
238         let options = ['-v']
239         if len(a:argList) != 0
240                 let options = a:argList
241         endif
242         return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
243 endfunction
245 " Function: s:svkFunctions.Unlock(argList) {{{2
246 function! s:svkFunctions.Unlock(argList)
247         return s:DoCommand(join(['unlock'] + a:argList, ' '), 'unlock', join(a:argList, ' '), {})
248 endfunction
249 " Function: s:svkFunctions.Update(argList) {{{2
250 function! s:svkFunctions.Update(argList)
251         return s:DoCommand('update', 'update', '', {})
252 endfunction
254 " Section: Plugin Registration {{{1
255 let s:VCSCommandUtility = VCSCommandRegisterModule('SVK', expand('<sfile>'), s:svkFunctions, [])
257 let &cpo = s:save_cpo