1
2 Author:peter@peterodding.com
3 Last Change:
4 URL:http://peterodding.com/code/vim/pyref/
5 License:
6 Version:
7
8
9 GetLatestVimScripts::AutoInstall
10
11 &compatible
12 if &cp || exists('g:loaded_pyref')
13 finish
14 endif
15
16
17
18
19
20
21 function! s:CheckOptions()
22 if !exists('g:pyref_mapping')
23 let g:pyref_mapping = '<F1>'
24 endif
25 if !exists('g:pyref_mirror')
26 let local_mirror = '/usr/share/doc/python2.6/html'
27 if isdirectory(local_mirror)
28 let g:pyref_mirror = 'file://' . local_mirror
29 else
30 let g:pyref_mirror = 'http://docs.python.org'
31 endif
32 endif
33 if !exists('g:pyref_index')
34 if has('win32') || has('win64')
35 let g:pyref_index = '~/vimfiles/misc/pyref_index'
36 else
37 let g:pyref_index = '~/.vim/misc/pyref_index'
38 endif
39 endif
40 if !filereadable(fnamemodify(g:pyref_index, ':p'))
41 let msg = "pyref.vim: The index file doesn't exist or isn't readable! (%s)"
42 echoerr printf(msg, g:pyref_index)
43 return 0
44 endif
45 if !exists('g:pyref_browser')
46 if has('win32') || has('win64')
47
48 let g:pyref_browser = 'CMD /C START ""'
49 else
50
51 $DISPLAY
52 if $DISPLAY == ''
53 let known_browsers = ['lynx', 'links', 'w3m']
54 else
55
56 let known_browsers = ['gnome-open', 'firefox', 'google-chrome', 'konqueror']
57 endif
58
59 let search_path = substitute(substitute($PATH, ',', '\\,', 'g'), ':', ',', 'g')
60 for browser in known_browsers
61 globpath()
62 try
63 let matches = split(globpath(search_path, browser, 1), '\n')
64 catch
65 let matches = split(globpath(search_path, browser), '\n')
66 endtry
67 if len(matches) > 0
68 let g:pyref_browser = matches[0]
69 break
70 endif
71 endfor
72 if !exists('g:pyref_browser')
73 let msg = "pyref.vim: Failed to find a default web browser!"
74 echoerr msg . "\nPlease set the global variable `pyref_browser' manually."
75 return 0
76 endif
77 endif
78 endif
79 return 1
80 endfunction
81
82 if s:CheckOptions()
83
84 let g:loaded_pyref = 1
85 else
86
87 finish
88 endif
89
90
91
92 augroup PluginPyRef
93 autocmd! FileType python call s:DefineMappings()
94 augroup END
95
96 function! s:DefineMappings()
97 let command = '%s <silent> <buffer> %s %s:call <Sid>PyRef()<CR>'
98
99 execute printf(command, 'nmap', g:pyref_mapping, '')
100 "g:pyref_mapping"
101
102 if g:pyref_mapping =~ '^<[^>]\+>'
103 execute printf(command, 'imap', g:pyref_mapping, '<C-O>')
104 endif
105 endfunction
106
107 function! s:PyRef()
108
109
110
111 try
112 let isk_save = &isk
113 let &isk = '@,48-57,_,192-255,.'
114 let ident = expand('<cword>')
115 finally
116 let &isk = isk_save
117 endtry
118
119
120 if ident == ''
121 return s:OpenBrowser(g:pyref_mirror . '/contents.html')
122 endif
123
124
125 let pattern = substitute(ident, '\.', '\\.', 'g')
126
127
128 let indexfile = fnamemodify(g:pyref_index, ':p')
129 try
130 let lines = readfile(indexfile)
131 catch
132 let lines = []
133 echoerr "pyref.vim: Failed to read index file! (" . indexfile . ")"
134 endtry
135 if s:JumpToEntry(lines, '^\C\(module-\|exceptions\.\)\?' . pattern . '\t')
136 return
137 endif
138
139
140 if s:JumpToEntry(lines, '\C\<' . pattern . '\>.*\t')
141 return
142 endif
143
144
145
146 for [url, method_pattern] in s:object_methods
147 let method = matchstr(ident, method_pattern)
148 if method != ''
149 if url =~ '%s'
150 let url = printf(url, method)
151 endif
152 return s:OpenBrowser(g:pyref_mirror . '/' . url)
153 endif
154 endfor
155
156
157 if s:JumpToEntry(lines, '\C' . pattern . '.*\t')
158 return
159 endif
160
161
162 "self.parser.add_option"
163 "optparse.OptionParser.add_option"
164
165 let parts = split(ident, '\.')
166 while len(parts) > 1
167 call remove(parts, 0)
168 let pattern = '\C\<' . join(parts, '\.') . '$'
169 if s:JumpToEntry(lines, pattern)
170 return
171 endif
172 endwhile
173
174 http://docs.python.org/
175 call s:OpenBrowser('http://google.com/search?btnI&q=inurl:docs.python.org/+' . ident)
176
177 endfunction
178
179
180
181
182 let s:object_methods = [
183 \ ['library/stdtypes.html#str.%s', '\C\.\@<=\(capitalize\|center\|count\|decode\|encode\|endswith\|expandtabs\|find\|format\|index\|isalnum\|isalpha\|isdigit\|islower\|isspace\|istitle\|isupper\|join\|ljust\|lower\|lstrip\|partition\|replace\|rfind\|rindex\|rjust\|rpartition\|rsplit\|rstrip\|split\|splitlines\|startswith\|strip\|swapcase\|title\|translate\|upper\|zfill\)$'],
184 \ ['tutorial/datastructures.html#more-on-lists', '\C\.\@<=\(append\|count\|extend\|index\|insert\|pop\|remove\|reverse\|sort\)$'],
185 \ ['library/stdtypes.html#dict.%s', '\C\.\@<=\(clear\|copy\|fromkeys\|get\|has_key\|items\|iteritems\|iterkeys\|itervalues\|keys\|pop\|popitem\|setdefault\|update\|values\)$'],
186 \ ['library/stdtypes.html#file.%s', '\C\.\@<=\(close\|closed\|encoding\|errors\|fileno\|flush\|isatty\|mode\|name\|newlines\|next\|read\|readinto\|readline\|readlines\|seek\|softspace\|tell\|truncate\|write\|writelines\|xreadlines\)$']]
187
188 function! s:JumpToEntry(lines, pattern)
189 if &verbose
190 echomsg "pyref.vim: Trying to match" string(a:pattern)
191 endif
192 let index = match(a:lines, a:pattern)
193 if index >= 0
194 let url = split(a:lines[index], '\t')[1]
195 call s:OpenBrowser(g:pyref_mirror . '/' . url)
196 return 1
197 endif
198 return 0
199 endfunction
200
201 function! s:OpenBrowser(url)
202 let browser = g:pyref_browser
203 if browser =~ '\<\(lynx\|links\|w3m\)\>'
204 execute '!' . browser fnameescape(a:url)
205 else
206 if browser !~ '^CMD /C START'
207 let browser = shellescape(browser)
208 endif
209 call system(browser . ' ' . shellescape(a:url))
210 endif
211 if v:shell_error && browser !~ '^CMD /C START'
212
213
214
215 let message = "pyref.vim: Failed to execute %s! (status code %i)"
216 echoerr printf(message, browser, v:shell_error)
217 return 0
218 endif
219 return 1
220 endfunction
221
222