Merge tag 'v1.99.47' into bh/nerdtree
[vcscommand.git] / plugin / vcsbzr.vim
blob823225411f8d567683e8f0d65ef26689f14f072d
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(path) {{{2
86 function! s:bzrFunctions.Identify(path)
87   let l:save_bzr_log=$BZR_LOG
88   try
89     let $BZR_LOG=has("win32") || has("win95") || has("win64") || has("win16") ? "nul" : "/dev/null"
90     let statusText = s:VCSCommandUtility.system(s:Executable() . ' info -- "' . a:path . '"')
91   finally
92     let $BZR_LOG=l:save_bzr_log
93   endtry
94   if(v:shell_error)
95     return 0
96   else
97     return 1
98   endif
99 endfunction
101 " Function: s:bzrFunctions.Add() {{{2
102 function! s:bzrFunctions.Add(argList)
103   return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
104 endfunction
106 " Function: s:bzrFunctions.Annotate(argList) {{{2
107 function! s:bzrFunctions.Annotate(argList)
108   if len(a:argList) == 0
109     if &filetype ==? 'bzrannotate'
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:bzrFunctions.Commit(argList) {{{2
133 function! s:bzrFunctions.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:bzrFunctions.Delete() {{{2
141 function! s:bzrFunctions.Delete(argList)
142   return s:DoCommand(join(['rm'] + a:argList, ' '), 'rm', join(a:argList, ' '), {})
143 endfunction
145 " Function: s:bzrFunctions.Diff(argList) {{{2
146 function! s:bzrFunctions.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, {'allowNonZeroExit': 1})
160 endfunction
162 " Function: s:bzrFunctions.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:bzrFunctions.GetBufferInfo()
169   let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
170   let fileName = resolve(bufname(originalBuffer))
171   let statusText = s:VCSCommandUtility.system(s:Executable() . ' status -S -- "' . fileName . '"')
172   let revision = s:VCSCommandUtility.system(s:Executable() . ' revno -- "' . fileName . '"')
173   if(v:shell_error)
174     return []
175   endif
177   " File not under BZR control.
178   if statusText =~ '^?'
179     return ['Unknown']
180   endif
182   let [flags, repository] = matchlist(statusText, '^\(.\{3}\)\s\+\(\S\+\)')[1:2]
183   if revision == ''
184     " Error
185     return ['Unknown']
186   elseif flags =~ '^A'
187     return ['New', 'New']
188   else
189     return [revision, repository]
190   endif
191 endfunction
193 " Function: s:bzrFunctions.Info(argList) {{{2
194 function! s:bzrFunctions.Info(argList)
195   return s:DoCommand(join(['version-info'] + a:argList, ' '), 'version-info', join(a:argList, ' '), {})
196 endfunction
198 " Function: s:bzrFunctions.Lock(argList) {{{2
199 function! s:bzrFunctions.Lock(argList)
200   echomsg 'bzr lock is not necessary'
201 endfunction
203 " Function: s:bzrFunctions.Log() {{{2
204 function! s:bzrFunctions.Log(argList)
205   if len(a:argList) == 0
206     let options = []
207     let caption = ''
208   elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
209     let options = ['-r' . join(a:argList, ':')]
210     let caption = options[0]
211   else
212     " Pass-through
213     let options = a:argList
214     let caption = join(a:argList, ' ')
215   endif
217   let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
218   return resultBuffer
219 endfunction
221 " Function: s:bzrFunctions.Revert(argList) {{{2
222 function! s:bzrFunctions.Revert(argList)
223   return s:DoCommand('revert', 'revert', '', {})
224 endfunction
226 " Function: s:bzrFunctions.Review(argList) {{{2
227 function! s:bzrFunctions.Review(argList)
228   if len(a:argList) == 0
229     let versiontag = '(current)'
230     let versionOption = ''
231   else
232     let versiontag = a:argList[0]
233     let versionOption = ' -r ' . versiontag . ' '
234   endif
236   return s:DoCommand('cat' . versionOption, 'review', versiontag, {})
237 endfunction
239 " Function: s:bzrFunctions.Status(argList) {{{2
240 function! s:bzrFunctions.Status(argList)
241   let options = ['-S']
242   if len(a:argList) != 0
243     let options = a:argList
244   endif
245   return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
246 endfunction
248 " Function: s:bzrFunctions.Unlock(argList) {{{2
249 function! s:bzrFunctions.Unlock(argList)
250   echomsg 'bzr unlock is not necessary'
251 endfunction
252 " Function: s:bzrFunctions.Update(argList) {{{2
253 function! s:bzrFunctions.Update(argList)
254   return s:DoCommand('update', 'update', '', {})
255 endfunction
257 " Annotate setting {{{2
258 let s:bzrFunctions.AnnotateSplitRegex = '^[^|]\+ | '
260 " Section: Plugin Registration {{{1
261 let s:VCSCommandUtility = VCSCommandRegisterModule('BZR', expand('<sfile>'), s:bzrFunctions, [])
263 let &cpo = s:save_cpo