use case-insensitive tests for VCS file types
[vcscommand.git] / plugin / vcsbzr.vim
blob51505febccaa0f84d5759d71ae256eb21eda6036
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 runtime plugin/vcscommand.vim
48 if !executable(VCSCommandGetOption('VCSCommandBZRExec', 'bzr'))
49   " BZR 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:bzrFunctions = {}
60 " Section: Utility functions {{{1
62 " Function: s:Executable() {{{2
63 " Returns the executable used to invoke bzr suitable for use in a shell
64 " command.
65 function! s:Executable()
66         return VCSCommandGetOption('VCSCommandBZRExec', 'bzr')
67 endfunction
69 " Function: s:DoCommand(cmd, cmdName, statusText) {{{2
70 " Wrapper to VCSCommandDoCommand to add the name of the BZR executable to the
71 " command argument.
72 function! s:DoCommand(cmd, cmdName, statusText, options)
73   if VCSCommandGetVCSType(expand('%')) == 'BZR'
74     let fullCmd = s:Executable() . ' ' . a:cmd
75     return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
76   else
77     throw 'BZR VCSCommand plugin called on non-BZR item.'
78   endif
79 endfunction
81 " Section: VCS function implementations {{{1
83 " Function: s:bzrFunctions.Identify(buffer) {{{2
84 function! s:bzrFunctions.Identify(buffer)
85   let fileName = resolve(bufname(a:buffer))
86   let l:save_bzr_log=$BZR_LOG
87   try
88     let $BZR_LOG=has("win32") || has("win95") || has("win64") || has("win16") ? "nul" : "/dev/null"
89     let statusText = s:VCSCommandUtility.system(s:Executable() . ' info -- "' . fileName . '"')
90   finally
91     let $BZR_LOG=l:save_bzr_log
92   endtry
93   if(v:shell_error)
94     return 0
95   else
96     return 1
97   endif
98 endfunction
100 " Function: s:bzrFunctions.Add() {{{2
101 function! s:bzrFunctions.Add(argList)
102   return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
103 endfunction
105 " Function: s:bzrFunctions.Annotate(argList) {{{2
106 function! s:bzrFunctions.Annotate(argList)
107   if len(a:argList) == 0
108     if &filetype ==? 'bzrannotate'
109       " Perform annotation of the version indicated by the current line.
110       let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
111       let options = ' -r' . caption
112     else
113       let caption = ''
114       let options = ''
115     endif
116   elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
117     let caption = a:argList[0]
118     let options = ' -r' . caption
119   else
120     let caption = join(a:argList, ' ')
121     let options = ' ' . caption
122   endif
124   let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
125   if resultBuffer > 0
126     normal! 1G2dd
127   endif
128   return resultBuffer
129 endfunction
131 " Function: s:bzrFunctions.Commit(argList) {{{2
132 function! s:bzrFunctions.Commit(argList)
133   let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
134   if resultBuffer == 0
135     echomsg 'No commit needed.'
136   endif
137 endfunction
139 " Function: s:bzrFunctions.Delete() {{{2
140 function! s:bzrFunctions.Delete(argList)
141   return s:DoCommand(join(['rm'] + a:argList, ' '), 'rm', join(a:argList, ' '), {})
142 endfunction
144 " Function: s:bzrFunctions.Diff(argList) {{{2
145 function! s:bzrFunctions.Diff(argList)
146   if len(a:argList) == 0
147     let revOptions = []
148     let caption = ''
149   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
150     let revOptions = ['-r' . join(a:argList, '..')]
151     let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
152   else
153     " Pass-through
154     let caption = join(a:argList, ' ')
155     let revOptions = a:argList
156   endif
158   return s:DoCommand(join(['diff'] + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
159 endfunction
161 " Function: s:bzrFunctions.GetBufferInfo() {{{2
162 " Provides version control details for the current file.  Current version
163 " number and current repository version number are required to be returned by
164 " the vcscommand plugin.
165 " Returns: List of results:  [revision, repository]
167 function! s:bzrFunctions.GetBufferInfo()
168   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
169   let fileName = resolve(bufname(originalBuffer))
170   let statusText = s:VCSCommandUtility.system(s:Executable() . ' status -S -- "' . fileName . '"')
171   let revision = s:VCSCommandUtility.system(s:Executable() . ' revno -- "' . fileName . '"')
172   if(v:shell_error)
173     return []
174   endif
176   " File not under BZR control.
177   if statusText =~ '^?'
178     return ['Unknown']
179   endif
181   let [flags, repository] = matchlist(statusText, '^\(.\{3}\)\s\+\(\S\+\)')[1:2]
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:bzrFunctions.Info(argList) {{{2
193 function! s:bzrFunctions.Info(argList)
194   return s:DoCommand(join(['version-info'] + a:argList, ' '), 'version-info', join(a:argList, ' '), {})
195 endfunction
197 " Function: s:bzrFunctions.Lock(argList) {{{2
198 function! s:bzrFunctions.Lock(argList)
199   echomsg 'bzr lock is not necessary'
200 endfunction
202 " Function: s:bzrFunctions.Log() {{{2
203 function! s:bzrFunctions.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:bzrFunctions.Revert(argList) {{{2
221 function! s:bzrFunctions.Revert(argList)
222   return s:DoCommand('revert', 'revert', '', {})
223 endfunction
225 " Function: s:bzrFunctions.Review(argList) {{{2
226 function! s:bzrFunctions.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:bzrFunctions.Status(argList) {{{2
239 function! s:bzrFunctions.Status(argList)
240   let options = ['-S']
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:bzrFunctions.Unlock(argList) {{{2
248 function! s:bzrFunctions.Unlock(argList)
249   echomsg 'bzr unlock is not necessary'
250 endfunction
251 " Function: s:bzrFunctions.Update(argList) {{{2
252 function! s:bzrFunctions.Update(argList)
253   return s:DoCommand('update', 'update', '', {})
254 endfunction
256 " Annotate setting {{{2
257 let s:bzrFunctions.AnnotateSplitRegex = '^[^|]\+ | '
259 " Section: Plugin Registration {{{1
260 let s:VCSCommandUtility = VCSCommandRegisterModule('BZR', expand('<sfile>'), s:bzrFunctions, [])
262 let &cpo = s:save_cpo