handle ignored files in GetBufferInfo
[vcscommand.git] / plugin / vcsbzr.vim
blob2e9c53bb79b14a4478cbcf7deb4465c4f0e105a5
1 " vim600: set foldmethod=marker:
3 " BZR 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 " VCSCommandBZRExec
32 "   This variable specifies the BZR executable.  If not set, it defaults to
33 "   'bzr' 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('VCSCommandBZRExec', 'bzr'))
51   " BZR 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:bzrFunctions = {}
62 " Section: Utility functions {{{1
64 " Function: s:Executable() {{{2
65 " Returns the executable used to invoke bzr suitable for use in a shell
66 " command.
67 function! s:Executable()
68         return shellescape(VCSCommandGetOption('VCSCommandBZRExec', 'bzr'))
69 endfunction
71 " Function: s:DoCommand(cmd, cmdName, statusText) {{{2
72 " Wrapper to VCSCommandDoCommand to add the name of the BZR executable to the
73 " command argument.
74 function! s:DoCommand(cmd, cmdName, statusText, options)
75   if VCSCommandGetVCSType(expand('%')) == 'BZR'
76     let fullCmd = s:Executable() . ' ' . a:cmd
77     return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
78   else
79     throw 'BZR VCSCommand plugin called on non-BZR item.'
80   endif
81 endfunction
83 " Section: VCS function implementations {{{1
85 " Function: s:bzrFunctions.Identify(buffer) {{{2
86 function! s:bzrFunctions.Identify(buffer)
87   let fileName = resolve(bufname(a:buffer))
88   let l:save_bzr_log=$BZR_LOG
89   try
90     let $BZR_LOG=has("win32") || has("win95") || has("win64") || has("win16") ? "nul" : "/dev/null"
91     let statusText = s:VCSCommandUtility.system(s:Executable() . ' info -- "' . fileName . '"')
92   finally
93     let $BZR_LOG=l:save_bzr_log
94   endtry
95   if(v:shell_error)
96     return 0
97   else
98     return 1
99   endif
100 endfunction
102 " Function: s:bzrFunctions.Add() {{{2
103 function! s:bzrFunctions.Add(argList)
104   return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
105 endfunction
107 " Function: s:bzrFunctions.Annotate(argList) {{{2
108 function! s:bzrFunctions.Annotate(argList)
109   if len(a:argList) == 0
110     if &filetype ==? 'bzrannotate'
111       " Perform annotation of the version indicated by the current line.
112       let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
113       let options = ' -r' . caption
114     else
115       let caption = ''
116       let options = ''
117     endif
118   elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
119     let caption = a:argList[0]
120     let options = ' -r' . caption
121   else
122     let caption = join(a:argList, ' ')
123     let options = ' ' . caption
124   endif
126   let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
127   if resultBuffer > 0
128     normal! 1G2dd
129   endif
130   return resultBuffer
131 endfunction
133 " Function: s:bzrFunctions.Commit(argList) {{{2
134 function! s:bzrFunctions.Commit(argList)
135   let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
136   if resultBuffer == 0
137     echomsg 'No commit needed.'
138   endif
139 endfunction
141 " Function: s:bzrFunctions.Delete() {{{2
142 function! s:bzrFunctions.Delete(argList)
143   return s:DoCommand(join(['rm'] + a:argList, ' '), 'rm', join(a:argList, ' '), {})
144 endfunction
146 " Function: s:bzrFunctions.Diff(argList) {{{2
147 function! s:bzrFunctions.Diff(argList)
148   if len(a:argList) == 0
149     let revOptions = []
150     let caption = ''
151   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
152     let revOptions = ['-r' . join(a:argList, '..')]
153     let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
154   else
155     " Pass-through
156     let caption = join(a:argList, ' ')
157     let revOptions = a:argList
158   endif
160   return s:DoCommand(join(['diff'] + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
161 endfunction
163 " Function: s:bzrFunctions.GetBufferInfo() {{{2
164 " Provides version control details for the current file.  Current version
165 " number and current repository version number are required to be returned by
166 " the vcscommand plugin.
167 " Returns: List of results:  [revision, repository]
169 function! s:bzrFunctions.GetBufferInfo()
170   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
171   let fileName = resolve(bufname(originalBuffer))
172   let statusText = s:VCSCommandUtility.system(s:Executable() . ' status -S -- "' . fileName . '"')
173   let revision = s:VCSCommandUtility.system(s:Executable() . ' revno -- "' . fileName . '"')
174   if(v:shell_error)
175     return []
176   endif
178   " File not under BZR control.
179   if statusText =~ '^?'
180     return ['Unknown']
181   endif
183   let [flags, repository] = matchlist(statusText, '^\(.\{3}\)\s\+\(\S\+\)')[1:2]
184   if revision == ''
185     " Error
186     return ['Unknown']
187   elseif flags =~ '^A'
188     return ['New', 'New']
189   else
190     return [revision, repository]
191   endif
192 endfunction
194 " Function: s:bzrFunctions.Info(argList) {{{2
195 function! s:bzrFunctions.Info(argList)
196   return s:DoCommand(join(['version-info'] + a:argList, ' '), 'version-info', join(a:argList, ' '), {})
197 endfunction
199 " Function: s:bzrFunctions.Lock(argList) {{{2
200 function! s:bzrFunctions.Lock(argList)
201   echomsg 'bzr lock is not necessary'
202 endfunction
204 " Function: s:bzrFunctions.Log() {{{2
205 function! s:bzrFunctions.Log(argList)
206   if len(a:argList) == 0
207     let options = []
208     let caption = ''
209   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
210     let options = ['-r' . join(a:argList, ':')]
211     let caption = options[0]
212   else
213     " Pass-through
214     let options = a:argList
215     let caption = join(a:argList, ' ')
216   endif
218   let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
219   return resultBuffer
220 endfunction
222 " Function: s:bzrFunctions.Revert(argList) {{{2
223 function! s:bzrFunctions.Revert(argList)
224   return s:DoCommand('revert', 'revert', '', {})
225 endfunction
227 " Function: s:bzrFunctions.Review(argList) {{{2
228 function! s:bzrFunctions.Review(argList)
229   if len(a:argList) == 0
230     let versiontag = '(current)'
231     let versionOption = ''
232   else
233     let versiontag = a:argList[0]
234     let versionOption = ' -r ' . versiontag . ' '
235   endif
237   return s:DoCommand('cat' . versionOption, 'review', versiontag, {})
238 endfunction
240 " Function: s:bzrFunctions.Status(argList) {{{2
241 function! s:bzrFunctions.Status(argList)
242   let options = ['-S']
243   if len(a:argList) != 0
244     let options = a:argList
245   endif
246   return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
247 endfunction
249 " Function: s:bzrFunctions.Unlock(argList) {{{2
250 function! s:bzrFunctions.Unlock(argList)
251   echomsg 'bzr unlock is not necessary'
252 endfunction
253 " Function: s:bzrFunctions.Update(argList) {{{2
254 function! s:bzrFunctions.Update(argList)
255   return s:DoCommand('update', 'update', '', {})
256 endfunction
258 " Annotate setting {{{2
259 let s:bzrFunctions.AnnotateSplitRegex = '^[^|]\+ | '
261 " Section: Plugin Registration {{{1
262 let s:VCSCommandUtility = VCSCommandRegisterModule('BZR', expand('<sfile>'), s:bzrFunctions, [])
264 let &cpo = s:save_cpo