From f29c9e50795cdfcc2b31b1e76ab6bd202e476298 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Sun, 4 Apr 2021 18:20:36 -0400 Subject: [PATCH] Support :GBrowse {url} outside of Git repository --- autoload/fugitive.vim | 90 ++++++++++++++++++++++--------------------- doc/fugitive.txt | 13 +++---- 2 files changed, 52 insertions(+), 51 deletions(-) diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index 72e329c..6fd0f12 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -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, '\\\@\|"]', '\="%".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 diff --git a/doc/fugitive.txt b/doc/fugitive.txt index ac20d21..d5c6afe 100644 --- a/doc/fugitive.txt +++ b/doc/fugitive.txt @@ -212,9 +212,6 @@ that are part of Git repositories). *:GBrowse* :GBrowse Open the current file, blob, tree, commit, or tag in your browser at the upstream hosting provider. - If a range is given, it is appropriately appended to - the URL as an anchor. - Upstream providers can be added by installing an appropriate Vim plugin. For example, GitHub can be supported by installing rhubarb.vim, available at @@ -222,16 +219,18 @@ that are part of Git repositories). :GBrowse {object} Like :GBrowse, but for a given |fugitive-object|. -:GBrowse [...]@{remote} Force using the given remote rather than the remote - for the current branch. The remote is used to - determine which upstream repository to link to. - :{range}GBrowse [args] Appends an anchor to the URL that emphasizes the selected lines. This also forces the URL to include a commit rather than a branch name so it remains valid if the file changes. You can give a range of "0" to force this behavior without including an anchor. +:GBrowse [...]@{remote} Force using the given remote rather than the remote + for the current branch. The remote is used to + determine which upstream repository to link to. + +:GBrowse {url} Open an arbitrary URL in your browser. + :[range]GBrowse! [args] Like :GBrowse, but put the URL on the clipboard rather than opening it.