2 " Author: Peter Odding <peter@peterodding.com>
3 " Last Change: June 18, 2011
4 " URL: http://peterodding.com/code/vim/misc/
6 if !exists('s:version')
8 let s:enoimpl = "open.vim %s: %s() hasn't been implemented for your platform! If you have suggestions, please contact peter@peterodding.com."
9 let s:handlers = ['gnome-open', 'kde-open', 'exo-open', 'xdg-open']
12 function! xolox#misc#open#file(path, ...)
13 if xolox#misc#os#is_win()
15 call xolox#shell#open_with_windows_shell(a:path)
16 catch /^Vim\%((\a\+)\)\=:E117/
17 let command = '!start CMD /C START "" %s'
18 silent execute printf(command, shellescape(a:path))
22 let cmd = 'open ' . shellescape(a:path) . ' 2>&1'
23 call s:handle_error(cmd, system(cmd))
26 for handler in s:handlers + a:000
27 if executable(handler)
28 call xolox#misc#msg#debug("open.vim %s: Using '%s' to open '%s'.", s:version, handler, a:path)
29 let cmd = shellescape(handler) . ' ' . shellescape(a:path) . ' 2>&1'
30 call s:handle_error(cmd, system(cmd))
35 throw printf(s:enoimpl, s:script, 'xolox#misc#open#file')
38 function! xolox#misc#open#url(url)
42 let url = 'http://' . url
43 elseif url !~ '^mailto:'
44 let url = 'mailto:' . url
47 if has('unix') && !has('gui_running') && $DISPLAY == ''
48 for browser in ['lynx', 'links', 'w3m']
49 if executable(browser)
50 execute '!' . browser fnameescape(url)
51 call s:handle_error(browser . ' ' . url, '')
56 call xolox#misc#open#file(url, 'firefox', 'google-chrome')
59 function! s:handle_error(cmd, output)
61 let message = "open.vim %s: Failed to execute program! (command line: %s%s)"
62 let output = strtrans(xolox#misc#str#trim(a:output))
64 let output = ", output: " . string(output)
66 throw printf(message, s:version, a:cmd, output)
70 " vim: et ts=2 sw=2 fdm=marker