|
| 1 | +" Vim auto-load script |
| 2 | +" Author: Peter Odding <peter@peterodding.com> |
| 3 | +" Last Change: June 18, 2011 |
| 4 | +" URL: http://peterodding.com/code/vim/misc/ |
| 5 | + |
| 6 | +if !exists('s:version') |
| 7 | + let s:version = '1.0' |
| 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'] |
| 10 | +endif |
| 11 | + |
| 12 | +function! xolox#misc#open#file(path, ...) |
| 13 | + if xolox#misc#os#is_win() |
| 14 | + try |
| 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)) |
| 19 | + endtry |
| 20 | + return |
| 21 | + elseif has('macunix') |
| 22 | + let cmd = 'open ' . shellescape(a:path) . ' 2>&1' |
| 23 | + call s:handle_error(cmd, system(cmd)) |
| 24 | + return |
| 25 | + else |
| 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)) |
| 31 | + return |
| 32 | + endif |
| 33 | + endfor |
| 34 | + endif |
| 35 | + throw printf(s:enoimpl, s:script, 'xolox#misc#open#file') |
| 36 | +endfunction |
| 37 | + |
| 38 | +function! xolox#misc#open#url(url) |
| 39 | + let url = a:url |
| 40 | + if url !~ '^\w\+://' |
| 41 | + if url !~ '@' |
| 42 | + let url = 'http://' . url |
| 43 | + elseif url !~ '^mailto:' |
| 44 | + let url = 'mailto:' . url |
| 45 | + endif |
| 46 | + endif |
| 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, '') |
| 52 | + return |
| 53 | + endif |
| 54 | + endfor |
| 55 | + endif |
| 56 | + call xolox#misc#open#file(url, 'firefox', 'google-chrome') |
| 57 | +endfunction |
| 58 | + |
| 59 | +function! s:handle_error(cmd, output) |
| 60 | + if v:shell_error |
| 61 | + let message = "open.vim %s: Failed to execute program! (command line: %s%s)" |
| 62 | + let output = strtrans(xolox#misc#str#trim(a:output)) |
| 63 | + if output != '' |
| 64 | + let output = ", output: " . string(output) |
| 65 | + endif |
| 66 | + throw printf(message, s:version, a:cmd, output) |
| 67 | + endif |
| 68 | +endfunction |
| 69 | + |
| 70 | +" vim: et ts=2 sw=2 fdm=marker |
0 commit comments