handle ignored files in GetBufferInfo
[vcscommand.git] / plugin / vcssvk.vim
blobf04c9cc81e3f9842056bae6e988c9fef232a3ecf
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 if !exists('g:loaded_VCSCommand')
47         runtime plugin/vcscommand.vim
48 endif
50 if !executable(VCSCommandGetOption('VCSCommandSVKExec', 'svk'))
51         " SVK is not installed
52         finish
53 endif
55 let s:save_cpo=&cpo
56 set cpo&vim
58 " Section: Variable initialization {{{1
60 let s:svkFunctions = {}
62 " Section: Utility functions {{{1
64 " Function: s:Executable() {{{2
65 " Returns the executable used to invoke SVK suitable for use in a shell
66 " command.
67 function! s:Executable()
68         return shellescape(VCSCommandGetOption('VCSCommandSVKExec', 'svk'))
69 endfunction
71 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
72 " Wrapper to VCSCommandDoCommand to add the name of the SVK executable to the
73 " command argument.
74 function! s:DoCommand(cmd, cmdName, statusText, options)
75         if VCSCommandGetVCSType(expand('%')) == 'SVK'
76                 let fullCmd = s:Executable() . ' ' . a:cmd
77                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
78         else
79                 throw 'SVK VCSCommand plugin called on non-SVK item.'
80         endif
81 endfunction
83 " Section: VCS function implementations {{{1
85 " Function: s:svkFunctions.Identify(buffer) {{{2
86 function! s:svkFunctions.Identify(buffer)
87         let fileName = resolve(bufname(a:buffer))
88         if isdirectory(fileName)
89                 let directoryName = fileName
90         else
91                 let directoryName = fnamemodify(fileName, ':p:h')
92         endif
93         let statusText = s:VCSCommandUtility.system(s:Executable() . ' info -- "' . directoryName . '"', "no")
94         if(v:shell_error)
95                 return 0
96         else
97                 return 1
98         endif
99 endfunction
101 " Function: s:svkFunctions.Add() {{{2
102 function! s:svkFunctions.Add(argList)
103         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
104 endfunction
106 " Function: s:svkFunctions.Annotate(argList) {{{2
107 function! s:svkFunctions.Annotate(argList)
108         if len(a:argList) == 0
109                 if &filetype ==? 'svkannotate'
110                         " Perform annotation of the version indicated by the current line.
111                         let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
112                         let options = ' -r' . caption
113                 else
114                         let caption = ''
115                         let options = ''
116                 endif
117         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
118                 let caption = a:argList[0]
119                 let options = ' -r' . caption
120         else
121                 let caption = join(a:argList, ' ')
122                 let options = ' ' . caption
123         endif
125         let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
126         if resultBuffer > 0
127                 normal! 1G2dd
128         endif
129         return resultBuffer
130 endfunction
132 " Function: s:svkFunctions.Commit(argList) {{{2
133 function! s:svkFunctions.Commit(argList)
134         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
135         if resultBuffer == 0
136                 echomsg 'No commit needed.'
137         endif
138 endfunction
140 " Function: s:svkFunctions.Delete() {{{2
141 function! s:svkFunctions.Delete(argList)
142         return s:DoCommand(join(['delete'] + a:argList, ' '), 'delete', join(a:argList, ' '), {})
143 endfunction
145 " Function: s:svkFunctions.Diff(argList) {{{2
146 function! s:svkFunctions.Diff(argList)
147         if len(a:argList) == 0
148                 let revOptions = []
149                 let caption = ''
150         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
151                 let revOptions = ['-r' . join(a:argList, ':')]
152                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
153         else
154                 " Pass-through
155                 let caption = join(a:argList, ' ')
156                 let revOptions = a:argList
157         endif
159         return s:DoCommand(join(['diff'] + revOptions), 'diff', caption, {})
160 endfunction
162 " Function: s:svkFunctions.GetBufferInfo() {{{2
163 " Provides version control details for the current file.  Current version
164 " number and current repository version number are required to be returned by
165 " the vcscommand plugin.
166 " Returns: List of results:  [revision, repository]
168 function! s:svkFunctions.GetBufferInfo()
169         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
170         let fileName = resolve(bufname(originalBuffer))
171         let statusText = s:VCSCommandUtility.system(s:Executable() . ' status -v -- "' . fileName . '"')
172         if(v:shell_error)
173                 return []
174         endif
176         " File not under SVK control.
177         if statusText =~ '^?'
178                 return ['Unknown']
179         endif
181         let [flags, revision, repository] = matchlist(statusText, '^\(.\{3}\)\s\+\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)\s')[1:3]
182         if revision == ''
183                 " Error
184                 return ['Unknown']
185         elseif flags =~ '^A'
186                 return ['New', 'New']
187         else
188                 return [revision, repository]
189         endif
190 endfunction
192 " Function: s:svkFunctions.Info(argList) {{{2
193 function! s:svkFunctions.Info(argList)
194         return s:DoCommand(join(['info'] + a:argList, ' '), 'info', join(a:argList, ' '), {})
195 endfunction
197 " Function: s:svkFunctions.Lock(argList) {{{2
198 function! s:svkFunctions.Lock(argList)
199         return s:DoCommand(join(['lock'] + a:argList, ' '), 'lock', join(a:argList, ' '), {})
200 endfunction
202 " Function: s:svkFunctions.Log() {{{2
203 function! s:svkFunctions.Log(argList)
204         if len(a:argList) == 0
205                 let options = []
206                 let caption = ''
207         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
208                 let options = ['-r' . join(a:argList, ':')]
209                 let caption = options[0]
210         else
211                 " Pass-through
212                 let options = a:argList
213                 let caption = join(a:argList, ' ')
214         endif
216         let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
217         return resultBuffer
218 endfunction
220 " Function: s:svkFunctions.Revert(argList) {{{2
221 function! s:svkFunctions.Revert(argList)
222         return s:DoCommand('revert', 'revert', '', {})
223 endfunction
225 " Function: s:svkFunctions.Review(argList) {{{2
226 function! s:svkFunctions.Review(argList)
227         if len(a:argList) == 0
228                 let versiontag = '(current)'
229                 let versionOption = ''
230         else
231                 let versiontag = a:argList[0]
232                 let versionOption = ' -r ' . versiontag . ' '
233         endif
235         return s:DoCommand('cat' . versionOption, 'review', versiontag, {})
236 endfunction
238 " Function: s:svkFunctions.Status(argList) {{{2
239 function! s:svkFunctions.Status(argList)
240         let options = ['-v']
241         if len(a:argList) != 0
242                 let options = a:argList
243         endif
244         return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
245 endfunction
247 " Function: s:svkFunctions.Unlock(argList) {{{2
248 function! s:svkFunctions.Unlock(argList)
249         return s:DoCommand(join(['unlock'] + a:argList, ' '), 'unlock', join(a:argList, ' '), {})
250 endfunction
251 " Function: s:svkFunctions.Update(argList) {{{2
252 function! s:svkFunctions.Update(argList)
253         return s:DoCommand('update', 'update', '', {})
254 endfunction
256 " Section: Plugin Registration {{{1
257 let s:VCSCommandUtility = VCSCommandRegisterModule('SVK', expand('<sfile>'), s:svkFunctions, [])
259 let &cpo = s:save_cpo