Skip to content

Commit

Permalink
Moved OpenBrowser() to external autoload plug-in
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Sep 18, 2010
1 parent 2728e97 commit b059112
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 82 deletions.
25 changes: 6 additions & 19 deletions README.md
Expand Up @@ -8,43 +8,33 @@ The search works by scanning through a special index file with keyword, URL pair

## Install & usage

Unzip the most recent [ZIP archive](http://peterodding.com/code/vim/downloads/pyref) file inside your Vim profile directory (usually this is `~/.vim` on UNIX and `%USERPROFILE%\vimfiles` on Windows), restart Vim and execute the command `:helptags ~/.vim/doc` (use `:helptags ~\vimfiles\doc` instead on Windows). Now try it out: Open a Python script and press the `<F1>` key. If it doesn't work at first, please see the `g:pyref_browser` and `g:pyref_mapping` options below.
Unzip the most recent [ZIP archive](http://peterodding.com/code/vim/downloads/pyref) file inside your Vim profile directory (usually this is `~/.vim` on UNIX and `%USERPROFILE%\vimfiles` on Windows), restart Vim and execute the command `:helptags ~/.vim/doc` (use `:helptags ~\vimfiles\doc` instead on Windows). Now try it out: Open a Python script and press the `<F1>` key.

The following paragraphs explain the available options:

### The `g:pyref_browser` option

If the plug-in doesn't work out of the box or you don't like the default web browser you can change the global variable `g:pyref_browser` to the filename or pathname of your preferred web browser, e.g. inside Vim type:

:let g:pyref_browser = '/usr/bin/konqueror'

The plug-in tries to find a suitable default web browser but that might not always work. To see the currently configured web browser type the following:

:let g:pyref_browser

### The `g:pyref_mapping` option

When you've set `g:pyref_browser` but it still doesn't work you're probably running Vim inside a terminal which doesn't support `<F1>`. In this case you can change the key-mapping by setting the global variable `g:pyref_mapping` according to the syntax expected by Vim's `:imap` and `:nmap` commands:
If you press `<F1>` and nothing happens you're probably using a terminal that doesn't pass `<F1>` through to Vim. In this case you can change the key mapping by setting the global variable `g:pyref_mapping` according to the syntax expected by Vim's `:imap` and `:nmap` commands:

:let g:pyref_mapping = 'K'

Note that setting `g:pyref_mapping` won't change the mapping in existing buffers.
Note that setting `g:pyref_mapping` won't change the key mapping in existing buffers.

### The `g:pyref_mirror` option

The option `g:pyref_mirror` is useful when you don't always have a reliable internet connection available while coding. Most Linux distributions have an installable package containing the Python documentation, for example on Ubuntu and Debian you can execute the following command to install the documentation:
This option is useful when you don't always have a reliable internet connection available while coding. Most Linux distributions have an installable package containing the Python documentation, for example on Ubuntu and Debian you can execute the following command to install the documentation:

$ sudo apt-get install python2.6-doc

The above package puts the documentation in `/usr/share/doc/python2.6/html/` which happens to be the default location checked by the `pyref.vim` script. If you've installed the documentation elsewhere, change the global variable `g:pyref_mirror` accordingly.
The above package puts the documentation in `/usr/share/doc/python2.6/html/` which happens to be the default location checked by the `pyref.vim` script. If you've installed the documentation elsewhere you can change the global variable `g:pyref_mirror` accordingly.

### The `g:pyref_index` option

If you don't like the default location of the index file you can change it by setting the global variable `g:pyref_index`. A leading `~` in the `g:pyref_index` variable is expanded to your current home directory (`$HOME` on UNIX, `%USERPROFILE%` on Windows). Be aware that when you change the `g:pyref_index` option automatic updates using the [getscript plug-in](http://vimdoc.sourceforge.net/htmldoc/pi_getscript.html#getscript) won't update the index file anymore!

### General note about options

You can change any of the above options permanently by putting the relevant `:let` statements in your [vimrc script][vimrc]. If you set `g:pyref_browser` and/or `g:pyref_mirror` in your [vimrc script][vimrc] this can improve Vim's startup speed slightly because the plug-in won't have to query the file system when it's loaded.
You can change the above options permanently by putting the relevant `:let` statements in your [vimrc script](http://vimdoc.sourceforge.net/htmldoc/starting.html#vimrc).

## Contact

Expand All @@ -54,6 +44,3 @@ If you have questions, bug reports, suggestions, etc. the author can be contacte

This software is licensed under the [MIT license](http://en.wikipedia.org/wiki/MIT_License).
© 2010 Peter Odding &lt;<peter@peterodding.com>&gt;.


[vimrc]: http://vimdoc.sourceforge.net/htmldoc/starting.html#vimrc
3 changes: 0 additions & 3 deletions TODO.md
@@ -1,6 +1,3 @@
# To-do list

* Convert `pyref.vim` to an autoload plug-in?
* Outsource `openurl()` to shell.vim?!
* **Pros:** I don't want to implement this functionality twice because getting it right takes a bit of work. I could just include the `shell.vim` plug-in with the ZIP archive for `pyref.vim` and be done with it.
* **Cons:** The `shell.vim` plug-in includes a Windows DLL which means the ZIP archive for `pyref.vim` would also include this DLL, even for users on other platforms. If I were trying a Vim plug-in that did this I might dismiss it out of hand...
67 changes: 7 additions & 60 deletions pyref.vim
@@ -1,6 +1,6 @@
" Vim plug-in
" Author: Peter Odding <peter@peterodding.com>
" Last Change: July 28, 2010
" Last Change: September 18, 2010
" URL: http://peterodding.com/code/vim/pyref/
" License: MIT
" Version: 0.5
Expand Down Expand Up @@ -42,40 +42,6 @@ function! s:CheckOptions()
echoerr printf(msg, g:pyref_index)
return 0 " Initialization failed.
endif
if !exists('g:pyref_browser')
if has('win32') || has('win64')
" On Windows the default web browser is accessible using the START command.
let g:pyref_browser = 'CMD /C START ""'
else
" On UNIX we decide whether to use a CLI or GUI web browser based on
" whether the $DISPLAY environment variable is set.
if $DISPLAY == ''
let known_browsers = ['lynx', 'links', 'w3m']
else
" Note: Don't use `xdg-open' here, it ignores fragment identifiers :-S
let known_browsers = ['gnome-open', 'firefox', 'google-chrome', 'konqueror']
endif
" Otherwise we search for a sensible default browser.
let search_path = substitute(substitute($PATH, ',', '\\,', 'g'), ':', ',', 'g')
for browser in known_browsers
" Use globpath()'s third argument where possible (since Vim 7.3?).
try
let matches = split(globpath(search_path, browser, 1), '\n')
catch
let matches = split(globpath(search_path, browser), '\n')
endtry
if len(matches) > 0
let g:pyref_browser = matches[0]
break
endif
endfor
if !exists('g:pyref_browser')
let msg = "pyref.vim: Failed to find a default web browser!"
echoerr msg . "\nPlease set the global variable `pyref_browser' manually."
return 0 " Initialization failed.
endif
endif
endif
return 1 " Initialization successful.
endfunction

Expand Down Expand Up @@ -118,7 +84,8 @@ function! s:PyRef() " {{{1

" Do something useful when there's nothing at the current position.
if ident == ''
return s:OpenBrowser(g:pyref_mirror . '/contents.html')
call xolox#open#url(g:pyref_mirror . '/contents.html')
return
endif

" Escape any dots in the expression so it can be used as a pattern.
Expand Down Expand Up @@ -149,7 +116,8 @@ function! s:PyRef() " {{{1
if url =~ '%s'
let url = printf(url, method)
endif
return s:OpenBrowser(g:pyref_mirror . '/' . url)
call xolox#open#url(g:pyref_mirror . '/' . url)
return
endif
endfor

Expand All @@ -172,7 +140,7 @@ function! s:PyRef() " {{{1
endwhile

" As a last resort, search all of http://docs.python.org/ using Google.
call s:OpenBrowser('http://google.com/search?btnI&q=inurl:docs.python.org/+' . ident)
call xolox#open#url('http://google.com/search?btnI&q=inurl:docs.python.org/+' . ident)

endfunction

Expand All @@ -192,31 +160,10 @@ function! s:JumpToEntry(lines, pattern) " {{{1
let index = match(a:lines, a:pattern)
if index >= 0
let url = split(a:lines[index], '\t')[1]
call s:OpenBrowser(g:pyref_mirror . '/' . url)
call xolox#open#url(g:pyref_mirror . '/' . url)
return 1
endif
return 0
endfunction

function! s:OpenBrowser(url) " {{{1
let browser = g:pyref_browser
if browser =~ '\<\(lynx\|links\|w3m\)\>'
execute '!' . browser fnameescape(a:url)
else
if browser !~ '^CMD /C START'
let browser = shellescape(browser)
endif
call system(browser . ' ' . shellescape(a:url))
endif
if v:shell_error && browser !~ '^CMD /C START'
" When I tested this on Windows Vista the START command worked just fine
" but it always exited with a status code of 1. Therefor the status code
" of the START command is now ignored.
let message = "pyref.vim: Failed to execute %s! (status code %i)"
echoerr printf(message, browser, v:shell_error)
return 0
endif
return 1
endfunction

" vim: ts=2 sw=2 et nowrap

0 comments on commit b059112

Please sign in to comment.