Support :GBrowse {url} outside of Git repository

This commit is contained in:
Tim Pope
2021-04-04 18:20:36 -04:00
parent 81074db0ee
commit f29c9e5079
2 changed files with 52 additions and 51 deletions

View File

@@ -6051,9 +6051,34 @@ augroup END
" Section: :GBrowse
function! s:BrowserOpen(url, mods, echo_copy) abort
let url = substitute(a:url, '[ <>\|"]', '\="%".printf("%02X",char2nr(submatch(0)))', 'g')
let mods = s:Mods(a:mods)
if a:echo_copy
if has('clipboard')
let @+ = url
endif
return 'echo '.string(url)
elseif exists(':Browse') == 2
return 'echo '.string(url).'|' . mods . 'Browse '.url
elseif exists(':OpenBrowser') == 2
return 'echo '.string(url).'|' . mods . 'OpenBrowser '.url
else
if !exists('g:loaded_netrw')
runtime! autoload/netrw.vim
endif
if exists('*netrw#BrowseX')
return 'echo '.string(url).'|' . mods . 'call netrw#BrowseX('.string(url).', 0)'
elseif exists('*netrw#NetrwBrowseX')
return 'echo '.string(url).'|' . mods . 'call netrw#NetrwBrowseX('.string(url).', 0)'
else
return 'echoerr ' . string('Netrw not found. Define your own :Browse to use :GBrowse')
endif
endif
endfunction
function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abort
let dir = s:Dir()
exe s:DirCheck(dir)
try
let arg = a:arg
if arg =~# '^++remote='
@@ -6085,12 +6110,18 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
if rev ==# ''
let rev = s:DirRev(@%)[1]
endif
if rev =~# '^:\=$'
let expanded = s:Relative()
elseif rev =~? '^\a\a\+:[\/][\/]' && rev !~? '^fugitive:'
let expanded = s:Expand(substitute(rev, '\\\@<!#\|\\\@<!%\ze\w', '\\&', 'g'))
else
let expanded = s:Expand(rev)
if rev =~? '^\a\a\+:[\/][\/]' && rev !~? '^fugitive:'
let rev = substitute(rev, '\\\@<![#!]\|\\\@<!%\ze\w', '\\&', 'g')
elseif rev ==# ':'
let rev = ''
endif
let expanded = s:Expand(rev)
if expanded =~? '^\a\a\+:[\/][\/]' && expanded !~? '^fugitive:'
return s:BrowserOpen(s:Slash(expanded), a:mods, a:bang)
endif
exe s:DirCheck(dir)
if empty(expanded)
let expanded = s:Relative(':(top)', dir)
endif
let cdir = FugitiveVimPath(fugitive#CommonDir(dir))
for subdir in ['tags/', 'heads/', 'remotes/']
@@ -6112,9 +6143,6 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
let type = 'blob'
endif
let path = path[1:-1]
elseif full =~? '^\a\a\+:[\/][\/]'
let path = s:Slash(full)
let type = 'url'
elseif empty(s:Tree(dir))
let path = '.git/' . full[strlen(dir)+1:-1]
let type = ''
@@ -6243,45 +6271,19 @@ function! fugitive#BrowseCommand(line1, count, range, bang, mods, arg, args) abo
\ 'line1': line1,
\ 'line2': line2}
if type ==# 'url'
let url = path
else
let url = ''
for Handler in get(g:, 'fugitive_browse_handlers', [])
let url = call(Handler, [copy(opts)])
if !empty(url)
break
endif
endfor
endif
let url = ''
for Handler in get(g:, 'fugitive_browse_handlers', [])
let url = call(Handler, [copy(opts)])
if !empty(url)
break
endif
endfor
if empty(url)
call s:throw("No GBrowse handler installed for '".raw."'")
endif
let url = substitute(url, '[ <>\|"]', '\="%".printf("%02X",char2nr(submatch(0)))', 'g')
let mods = s:Mods(a:mods)
if a:bang
if has('clipboard')
let @+ = url
endif
return 'echo '.string(url)
elseif exists(':Browse') == 2
return 'echo '.string(url).'|' . mods . 'Browse '.url
elseif exists(':OpenBrowser') == 2
return 'echo '.string(url).'|' . mods . 'OpenBrowser '.url
else
if !exists('g:loaded_netrw')
runtime! autoload/netrw.vim
endif
if exists('*netrw#BrowseX')
return 'echo '.string(url).'|' . mods . 'call netrw#BrowseX('.string(url).', 0)'
elseif exists('*netrw#NetrwBrowseX')
return 'echo '.string(url).'|' . mods . 'call netrw#NetrwBrowseX('.string(url).', 0)'
else
return 'echoerr ' . string('Netrw not found. Define your own :Browse to use :GBrowse')
endif
endif
return s:BrowserOpen(url, a:mods, a:bang)
catch /^fugitive:/
return 'echoerr ' . string(v:exception)
endtry