|
11 | 11 | " Don't source the plug-in when its already been loaded or &compatible is set.
|
12 | 12 | if &cp || exists('g:loaded_pyref')
|
13 | 13 | finish
|
14 |
| -endif |
15 |
| - |
16 |
| -" Configuration defaults. {{{1 |
17 |
| - |
18 |
| -" Use a script-local function to define the configuration defaults so that we |
19 |
| -" don't pollute Vim's global scope with temporary variables. |
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 " Initialization failed. |
44 |
| - endif |
45 |
| - return 1 " Initialization successful. |
46 |
| -endfunction |
47 |
| - |
48 |
| -if s:CheckOptions() |
49 |
| - " Don't reload the plug-in once its been successfully initialized. |
50 |
| - let g:loaded_pyref = 1 |
51 | 14 | else
|
52 |
| - " Don't finish sourcing the script when there's no point. |
53 |
| - finish |
| 15 | + let g:loaded_pyref = 1 |
54 | 16 | endif
|
55 | 17 |
|
56 |
| -" Automatic command to define key-mapping. {{{1 |
| 18 | +" Automatic command to enable plug-in for Python buffers only. |
57 | 19 |
|
58 | 20 | augroup PluginPyRef
|
59 |
| - autocmd! FileType python call s:DefineMappings() |
| 21 | + autocmd! FileType python call xolox#pyref#enable() |
60 | 22 | augroup END
|
61 | 23 |
|
62 |
| -function! s:DefineMappings() " {{{1 |
63 |
| - let command = '%s <silent> <buffer> %s %s:call <Sid>PyRef()<CR>' |
64 |
| - " Always define the normal mode mapping. |
65 |
| - execute printf(command, 'nmap', g:pyref_mapping, '') |
66 |
| - " Don't create the insert mode mapping when "g:pyref_mapping" has been |
67 |
| - " changed to something like K because it'll conflict with regular input. |
68 |
| - if g:pyref_mapping =~ '^<[^>]\+>' |
69 |
| - execute printf(command, 'imap', g:pyref_mapping, '<C-O>') |
70 |
| - endif |
71 |
| -endfunction |
72 |
| - |
73 |
| -function! s:PyRef() " {{{1 |
74 |
| - |
75 |
| - " Get the identifier under the cursor including any dots to match |
76 |
| - " identifiers like `os.path.join' instead of single words like `join'. |
77 |
| - try |
78 |
| - let isk_save = &isk |
79 |
| - let &isk = '@,48-57,_,192-255,.' |
80 |
| - let ident = expand('<cword>') |
81 |
| - finally |
82 |
| - let &isk = isk_save |
83 |
| - endtry |
84 |
| - |
85 |
| - " Do something useful when there's nothing at the current position. |
86 |
| - if ident == '' |
87 |
| - call xolox#open#url(g:pyref_mirror . '/contents.html') |
88 |
| - return |
89 |
| - endif |
90 |
| - |
91 |
| - " Escape any dots in the expression so it can be used as a pattern. |
92 |
| - let pattern = substitute(ident, '\.', '\\.', 'g') |
93 |
| - |
94 |
| - " Search for an exact match of a module name or identifier in the index. |
95 |
| - let indexfile = fnamemodify(g:pyref_index, ':p') |
96 |
| - try |
97 |
| - let lines = readfile(indexfile) |
98 |
| - catch |
99 |
| - let lines = [] |
100 |
| - echoerr "pyref.vim: Failed to read index file! (" . indexfile . ")" |
101 |
| - endtry |
102 |
| - if s:JumpToEntry(lines, '^\C\(module-\|exceptions\.\)\?' . pattern . '\t') |
103 |
| - return |
104 |
| - endif |
105 |
| - |
106 |
| - " Search for a substring match on word boundaries. |
107 |
| - if s:JumpToEntry(lines, '\C\<' . pattern . '\>.*\t') |
108 |
| - return |
109 |
| - endif |
110 |
| - |
111 |
| - " Try to match a method name of one of the standard Python types: strings, |
112 |
| - " lists, dictionaries and files (not exactly ideal but better than nothing). |
113 |
| - for [url, method_pattern] in s:object_methods |
114 |
| - let method = matchstr(ident, method_pattern) |
115 |
| - if method != '' |
116 |
| - if url =~ '%s' |
117 |
| - let url = printf(url, method) |
118 |
| - endif |
119 |
| - call xolox#open#url(g:pyref_mirror . '/' . url) |
120 |
| - return |
121 |
| - endif |
122 |
| - endfor |
123 |
| - |
124 |
| - " Search for a substring match in the index. |
125 |
| - if s:JumpToEntry(lines, '\C' . pattern . '.*\t') |
126 |
| - return |
127 |
| - endif |
128 |
| - |
129 |
| - " Split the expression on all dots and search for a progressively smaller |
130 |
| - " suffix to resolve object attributes like "self.parser.add_option" to |
131 |
| - " global identifiers like "optparse.OptionParser.add_option". This relies |
132 |
| - " on the uniqueness of the method names in the standard library. |
133 |
| - let parts = split(ident, '\.') |
134 |
| - while len(parts) > 1 |
135 |
| - call remove(parts, 0) |
136 |
| - let pattern = '\C\<' . join(parts, '\.') . '$' |
137 |
| - if s:JumpToEntry(lines, pattern) |
138 |
| - return |
139 |
| - endif |
140 |
| - endwhile |
141 |
| - |
142 |
| - " As a last resort, search all of http://docs.python.org/ using Google. |
143 |
| - call xolox#open#url('http://google.com/search?btnI&q=inurl:docs.python.org/+' . ident) |
144 |
| - |
145 |
| -endfunction |
146 |
| - |
147 |
| -" This list of lists contains [url_format, method_pattern] pairs that are used |
148 |
| -" to recognize calls to methods of objects that are one of Python's standard |
149 |
| -" types: strings, lists, dictionaries and file handles. |
150 |
| -let s:object_methods = [ |
151 |
| - \ ['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\)$'], |
152 |
| - \ ['tutorial/datastructures.html#more-on-lists', '\C\.\@<=\(append\|count\|extend\|index\|insert\|pop\|remove\|reverse\|sort\)$'], |
153 |
| - \ ['library/stdtypes.html#dict.%s', '\C\.\@<=\(clear\|copy\|fromkeys\|get\|has_key\|items\|iteritems\|iterkeys\|itervalues\|keys\|pop\|popitem\|setdefault\|update\|values\)$'], |
154 |
| - \ ['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\)$']] |
155 |
| - |
156 |
| -function! s:JumpToEntry(lines, pattern) " {{{1 |
157 |
| - if &verbose |
158 |
| - echomsg "pyref.vim: Trying to match" string(a:pattern) |
159 |
| - endif |
160 |
| - let index = match(a:lines, a:pattern) |
161 |
| - if index >= 0 |
162 |
| - let url = split(a:lines[index], '\t')[1] |
163 |
| - call xolox#open#url(g:pyref_mirror . '/' . url) |
164 |
| - return 1 |
165 |
| - endif |
166 |
| - return 0 |
167 |
| -endfunction |
168 |
| - |
169 | 24 | " vim: ts=2 sw=2 et nowrap
|
0 commit comments