Verified Commit 52cfee8c authored by Marco Kellershoff's avatar Marco Kellershoff 🤸
Browse files

Check if it's a valid URL, otherwise (optional) fallback to investigate.vim

parent 912ba389
...@@ -12,6 +12,9 @@ Here's a quick example: ...@@ -12,6 +12,9 @@ Here's a quick example:
```vim ```vim
" OpenUrl.nvim binding " OpenUrl.nvim binding
nnoremap <leader>e :call OpenUrl#UnderCursor()<CR> nnoremap <leader>q :call OpenUrl#UnderCursor()<CR>
" Use keith/investigate.vim as a fallback, if it's not an URL
let g:loaded_OpenUrl_DarkInvestigate = 1
``` ```
...@@ -5,17 +5,24 @@ let g:loaded_OpenUrl = 1 ...@@ -5,17 +5,24 @@ let g:loaded_OpenUrl = 1
function! OpenUrl#UnderCursor() function! OpenUrl#UnderCursor()
let s:url = OpenUrl#GetWordUnderCursor() let s:url = OpenUrl#GetWordUnderCursor()
if has('win16') || has('win32') || has('win64') let s:is_url = OpenUrl#IsUrl(s:url)
exe '!start cmd /cstart /b '.s:url.'' if s:is_url == 1
elseif has('mac') || has('macunix') || has('gui_macvim') call OpenUrl#WithDefaultBrowser(s:url)
exe 'silent !open "'.s:url.'"'
else else
exe 'silent !xdg-open "'.s:url.'"' call OpenUrl#PassToDarkInvestigate()
endif
endfunction
function! OpenUrl#PassToDarkInvestigate()
let s:url = OpenUrl#GetWordUnderCursor()
if exists('g:loaded_OpenUrl_DarkInvestigate')
call investigate#Investigate()
else
echo 'No valid URL found: '.s:url
endif endif
redraw!
endfunction endfunction
function! OpenUrl#Open(url) function! OpenUrl#WithDefaultBrowser(url)
if has('win16') || has('win32') || has('win64') if has('win16') || has('win32') || has('win64')
exe '!start cmd /cstart /b '.a:url.'' exe '!start cmd /cstart /b '.a:url.''
elseif has('mac') || has('macunix') || has('gui_macvim') elseif has('mac') || has('macunix') || has('gui_macvim')
...@@ -36,6 +43,15 @@ function! OpenUrl#OpenWith(app, file) ...@@ -36,6 +43,15 @@ function! OpenUrl#OpenWith(app, file)
endif endif
endfunction endfunction
function! OpenUrl#IsUrl(str)
let s:url = matchstr(a:str, '[a-z]*:\/\/[^ >,;]*')
if empty(s:url)
return 0
else
return 1
endif
endfunction
function! OpenUrl#GetWordUnderCursor() function! OpenUrl#GetWordUnderCursor()
return expand("<cWORD>") return expand("<cWORD>")
endfunction endfunction
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment