##// END OF EJS Templates
Initial implementation of latex->unicode completer.
Brian E. Granger -
Show More
@@ -1,1165 +1,1172 b''
1 # encoding: utf-8
1 # encoding: utf-8
2 """Word completion for IPython.
2 """Word completion for IPython.
3
3
4 This module is a fork of the rlcompleter module in the Python standard
4 This module is a fork of the rlcompleter module in the Python standard
5 library. The original enhancements made to rlcompleter have been sent
5 library. The original enhancements made to rlcompleter have been sent
6 upstream and were accepted as of Python 2.3, but we need a lot more
6 upstream and were accepted as of Python 2.3, but we need a lot more
7 functionality specific to IPython, so this module will continue to live as an
7 functionality specific to IPython, so this module will continue to live as an
8 IPython-specific utility.
8 IPython-specific utility.
9
9
10 Original rlcompleter documentation:
10 Original rlcompleter documentation:
11
11
12 This requires the latest extension to the readline module (the
12 This requires the latest extension to the readline module (the
13 completes keywords, built-ins and globals in __main__; when completing
13 completes keywords, built-ins and globals in __main__; when completing
14 NAME.NAME..., it evaluates (!) the expression up to the last dot and
14 NAME.NAME..., it evaluates (!) the expression up to the last dot and
15 completes its attributes.
15 completes its attributes.
16
16
17 It's very cool to do "import string" type "string.", hit the
17 It's very cool to do "import string" type "string.", hit the
18 completion key (twice), and see the list of names defined by the
18 completion key (twice), and see the list of names defined by the
19 string module!
19 string module!
20
20
21 Tip: to use the tab key as the completion key, call
21 Tip: to use the tab key as the completion key, call
22
22
23 readline.parse_and_bind("tab: complete")
23 readline.parse_and_bind("tab: complete")
24
24
25 Notes:
25 Notes:
26
26
27 - Exceptions raised by the completer function are *ignored* (and
27 - Exceptions raised by the completer function are *ignored* (and
28 generally cause the completion to fail). This is a feature -- since
28 generally cause the completion to fail). This is a feature -- since
29 readline sets the tty device in raw (or cbreak) mode, printing a
29 readline sets the tty device in raw (or cbreak) mode, printing a
30 traceback wouldn't work well without some complicated hoopla to save,
30 traceback wouldn't work well without some complicated hoopla to save,
31 reset and restore the tty state.
31 reset and restore the tty state.
32
32
33 - The evaluation of the NAME.NAME... form may cause arbitrary
33 - The evaluation of the NAME.NAME... form may cause arbitrary
34 application defined code to be executed if an object with a
34 application defined code to be executed if an object with a
35 ``__getattr__`` hook is found. Since it is the responsibility of the
35 ``__getattr__`` hook is found. Since it is the responsibility of the
36 application (or the user) to enable this feature, I consider this an
36 application (or the user) to enable this feature, I consider this an
37 acceptable risk. More complicated expressions (e.g. function calls or
37 acceptable risk. More complicated expressions (e.g. function calls or
38 indexing operations) are *not* evaluated.
38 indexing operations) are *not* evaluated.
39
39
40 - GNU readline is also used by the built-in functions input() and
40 - GNU readline is also used by the built-in functions input() and
41 raw_input(), and thus these also benefit/suffer from the completer
41 raw_input(), and thus these also benefit/suffer from the completer
42 features. Clearly an interactive application can benefit by
42 features. Clearly an interactive application can benefit by
43 specifying its own completer function and using raw_input() for all
43 specifying its own completer function and using raw_input() for all
44 its input.
44 its input.
45
45
46 - When the original stdin is not a tty device, GNU readline is never
46 - When the original stdin is not a tty device, GNU readline is never
47 used, and this module (and the readline module) are silently inactive.
47 used, and this module (and the readline module) are silently inactive.
48 """
48 """
49
49
50 #*****************************************************************************
50 #*****************************************************************************
51 #
51 #
52 # Since this file is essentially a minimally modified copy of the rlcompleter
52 # Since this file is essentially a minimally modified copy of the rlcompleter
53 # module which is part of the standard Python distribution, I assume that the
53 # module which is part of the standard Python distribution, I assume that the
54 # proper procedure is to maintain its copyright as belonging to the Python
54 # proper procedure is to maintain its copyright as belonging to the Python
55 # Software Foundation (in addition to my own, for all new code).
55 # Software Foundation (in addition to my own, for all new code).
56 #
56 #
57 # Copyright (C) 2008 IPython Development Team
57 # Copyright (C) 2008 IPython Development Team
58 # Copyright (C) 2001 Fernando Perez. <fperez@colorado.edu>
58 # Copyright (C) 2001 Fernando Perez. <fperez@colorado.edu>
59 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 # Copyright (C) 2001 Python Software Foundation, www.python.org
60 #
60 #
61 # Distributed under the terms of the BSD License. The full license is in
61 # Distributed under the terms of the BSD License. The full license is in
62 # the file COPYING, distributed as part of this software.
62 # the file COPYING, distributed as part of this software.
63 #
63 #
64 #*****************************************************************************
64 #*****************************************************************************
65
65
66 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
67 # Imports
67 # Imports
68 #-----------------------------------------------------------------------------
68 #-----------------------------------------------------------------------------
69
69
70 import __main__
70 import __main__
71 import glob
71 import glob
72 import inspect
72 import inspect
73 import itertools
73 import itertools
74 import keyword
74 import keyword
75 import os
75 import os
76 import re
76 import re
77 import sys
77 import sys
78
78
79 from IPython.config.configurable import Configurable
79 from IPython.config.configurable import Configurable
80 from IPython.core.error import TryNext
80 from IPython.core.error import TryNext
81 from IPython.core.inputsplitter import ESC_MAGIC
81 from IPython.core.inputsplitter import ESC_MAGIC
82 from IPython.core.latex_symbols import latex_symbols
82 from IPython.utils import generics
83 from IPython.utils import generics
83 from IPython.utils import io
84 from IPython.utils import io
84 from IPython.utils.decorators import undoc
85 from IPython.utils.decorators import undoc
85 from IPython.utils.dir2 import dir2
86 from IPython.utils.dir2 import dir2
86 from IPython.utils.process import arg_split
87 from IPython.utils.process import arg_split
87 from IPython.utils.py3compat import builtin_mod, string_types
88 from IPython.utils.py3compat import builtin_mod, string_types, PY3
88 from IPython.utils.traitlets import CBool, Enum
89 from IPython.utils.traitlets import CBool, Enum
89
90
90 #-----------------------------------------------------------------------------
91 #-----------------------------------------------------------------------------
91 # Globals
92 # Globals
92 #-----------------------------------------------------------------------------
93 #-----------------------------------------------------------------------------
93
94
94 # Public API
95 # Public API
95 __all__ = ['Completer','IPCompleter']
96 __all__ = ['Completer','IPCompleter']
96
97
97 if sys.platform == 'win32':
98 if sys.platform == 'win32':
98 PROTECTABLES = ' '
99 PROTECTABLES = ' '
99 else:
100 else:
100 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
101 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
101
102
102
103
103 #-----------------------------------------------------------------------------
104 #-----------------------------------------------------------------------------
104 # Main functions and classes
105 # Main functions and classes
105 #-----------------------------------------------------------------------------
106 #-----------------------------------------------------------------------------
106
107
107 def has_open_quotes(s):
108 def has_open_quotes(s):
108 """Return whether a string has open quotes.
109 """Return whether a string has open quotes.
109
110
110 This simply counts whether the number of quote characters of either type in
111 This simply counts whether the number of quote characters of either type in
111 the string is odd.
112 the string is odd.
112
113
113 Returns
114 Returns
114 -------
115 -------
115 If there is an open quote, the quote character is returned. Else, return
116 If there is an open quote, the quote character is returned. Else, return
116 False.
117 False.
117 """
118 """
118 # We check " first, then ', so complex cases with nested quotes will get
119 # We check " first, then ', so complex cases with nested quotes will get
119 # the " to take precedence.
120 # the " to take precedence.
120 if s.count('"') % 2:
121 if s.count('"') % 2:
121 return '"'
122 return '"'
122 elif s.count("'") % 2:
123 elif s.count("'") % 2:
123 return "'"
124 return "'"
124 else:
125 else:
125 return False
126 return False
126
127
127
128
128 def protect_filename(s):
129 def protect_filename(s):
129 """Escape a string to protect certain characters."""
130 """Escape a string to protect certain characters."""
130
131
131 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
132 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
132 for ch in s])
133 for ch in s])
133
134
134 def expand_user(path):
135 def expand_user(path):
135 """Expand '~'-style usernames in strings.
136 """Expand '~'-style usernames in strings.
136
137
137 This is similar to :func:`os.path.expanduser`, but it computes and returns
138 This is similar to :func:`os.path.expanduser`, but it computes and returns
138 extra information that will be useful if the input was being used in
139 extra information that will be useful if the input was being used in
139 computing completions, and you wish to return the completions with the
140 computing completions, and you wish to return the completions with the
140 original '~' instead of its expanded value.
141 original '~' instead of its expanded value.
141
142
142 Parameters
143 Parameters
143 ----------
144 ----------
144 path : str
145 path : str
145 String to be expanded. If no ~ is present, the output is the same as the
146 String to be expanded. If no ~ is present, the output is the same as the
146 input.
147 input.
147
148
148 Returns
149 Returns
149 -------
150 -------
150 newpath : str
151 newpath : str
151 Result of ~ expansion in the input path.
152 Result of ~ expansion in the input path.
152 tilde_expand : bool
153 tilde_expand : bool
153 Whether any expansion was performed or not.
154 Whether any expansion was performed or not.
154 tilde_val : str
155 tilde_val : str
155 The value that ~ was replaced with.
156 The value that ~ was replaced with.
156 """
157 """
157 # Default values
158 # Default values
158 tilde_expand = False
159 tilde_expand = False
159 tilde_val = ''
160 tilde_val = ''
160 newpath = path
161 newpath = path
161
162
162 if path.startswith('~'):
163 if path.startswith('~'):
163 tilde_expand = True
164 tilde_expand = True
164 rest = len(path)-1
165 rest = len(path)-1
165 newpath = os.path.expanduser(path)
166 newpath = os.path.expanduser(path)
166 if rest:
167 if rest:
167 tilde_val = newpath[:-rest]
168 tilde_val = newpath[:-rest]
168 else:
169 else:
169 tilde_val = newpath
170 tilde_val = newpath
170
171
171 return newpath, tilde_expand, tilde_val
172 return newpath, tilde_expand, tilde_val
172
173
173
174
174 def compress_user(path, tilde_expand, tilde_val):
175 def compress_user(path, tilde_expand, tilde_val):
175 """Does the opposite of expand_user, with its outputs.
176 """Does the opposite of expand_user, with its outputs.
176 """
177 """
177 if tilde_expand:
178 if tilde_expand:
178 return path.replace(tilde_val, '~')
179 return path.replace(tilde_val, '~')
179 else:
180 else:
180 return path
181 return path
181
182
182
183
183
184
184 def penalize_magics_key(word):
185 def penalize_magics_key(word):
185 """key for sorting that penalizes magic commands in the ordering
186 """key for sorting that penalizes magic commands in the ordering
186
187
187 Normal words are left alone.
188 Normal words are left alone.
188
189
189 Magic commands have the initial % moved to the end, e.g.
190 Magic commands have the initial % moved to the end, e.g.
190 %matplotlib is transformed as follows:
191 %matplotlib is transformed as follows:
191
192
192 %matplotlib -> matplotlib%
193 %matplotlib -> matplotlib%
193
194
194 [The choice of the final % is arbitrary.]
195 [The choice of the final % is arbitrary.]
195
196
196 Since "matplotlib" < "matplotlib%" as strings,
197 Since "matplotlib" < "matplotlib%" as strings,
197 "timeit" will appear before the magic "%timeit" in the ordering
198 "timeit" will appear before the magic "%timeit" in the ordering
198
199
199 For consistency, move "%%" to the end, so cell magics appear *after*
200 For consistency, move "%%" to the end, so cell magics appear *after*
200 line magics with the same name.
201 line magics with the same name.
201
202
202 A check is performed that there are no other "%" in the string;
203 A check is performed that there are no other "%" in the string;
203 if there are, then the string is not a magic command and is left unchanged.
204 if there are, then the string is not a magic command and is left unchanged.
204
205
205 """
206 """
206
207
207 # Move any % signs from start to end of the key
208 # Move any % signs from start to end of the key
208 # provided there are no others elsewhere in the string
209 # provided there are no others elsewhere in the string
209
210
210 if word[:2] == "%%":
211 if word[:2] == "%%":
211 if not "%" in word[2:]:
212 if not "%" in word[2:]:
212 return word[2:] + "%%"
213 return word[2:] + "%%"
213
214
214 if word[:1] == "%":
215 if word[:1] == "%":
215 if not "%" in word[1:]:
216 if not "%" in word[1:]:
216 return word[1:] + "%"
217 return word[1:] + "%"
217
218
218 return word
219 return word
219
220
220
221
221 @undoc
222 @undoc
222 class Bunch(object): pass
223 class Bunch(object): pass
223
224
224
225
225 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
226 DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
226 GREEDY_DELIMS = ' =\r\n'
227 GREEDY_DELIMS = ' =\r\n'
227
228
228
229
229 class CompletionSplitter(object):
230 class CompletionSplitter(object):
230 """An object to split an input line in a manner similar to readline.
231 """An object to split an input line in a manner similar to readline.
231
232
232 By having our own implementation, we can expose readline-like completion in
233 By having our own implementation, we can expose readline-like completion in
233 a uniform manner to all frontends. This object only needs to be given the
234 a uniform manner to all frontends. This object only needs to be given the
234 line of text to be split and the cursor position on said line, and it
235 line of text to be split and the cursor position on said line, and it
235 returns the 'word' to be completed on at the cursor after splitting the
236 returns the 'word' to be completed on at the cursor after splitting the
236 entire line.
237 entire line.
237
238
238 What characters are used as splitting delimiters can be controlled by
239 What characters are used as splitting delimiters can be controlled by
239 setting the `delims` attribute (this is a property that internally
240 setting the `delims` attribute (this is a property that internally
240 automatically builds the necessary regular expression)"""
241 automatically builds the necessary regular expression)"""
241
242
242 # Private interface
243 # Private interface
243
244
244 # A string of delimiter characters. The default value makes sense for
245 # A string of delimiter characters. The default value makes sense for
245 # IPython's most typical usage patterns.
246 # IPython's most typical usage patterns.
246 _delims = DELIMS
247 _delims = DELIMS
247
248
248 # The expression (a normal string) to be compiled into a regular expression
249 # The expression (a normal string) to be compiled into a regular expression
249 # for actual splitting. We store it as an attribute mostly for ease of
250 # for actual splitting. We store it as an attribute mostly for ease of
250 # debugging, since this type of code can be so tricky to debug.
251 # debugging, since this type of code can be so tricky to debug.
251 _delim_expr = None
252 _delim_expr = None
252
253
253 # The regular expression that does the actual splitting
254 # The regular expression that does the actual splitting
254 _delim_re = None
255 _delim_re = None
255
256
256 def __init__(self, delims=None):
257 def __init__(self, delims=None):
257 delims = CompletionSplitter._delims if delims is None else delims
258 delims = CompletionSplitter._delims if delims is None else delims
258 self.delims = delims
259 self.delims = delims
259
260
260 @property
261 @property
261 def delims(self):
262 def delims(self):
262 """Return the string of delimiter characters."""
263 """Return the string of delimiter characters."""
263 return self._delims
264 return self._delims
264
265
265 @delims.setter
266 @delims.setter
266 def delims(self, delims):
267 def delims(self, delims):
267 """Set the delimiters for line splitting."""
268 """Set the delimiters for line splitting."""
268 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
269 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
269 self._delim_re = re.compile(expr)
270 self._delim_re = re.compile(expr)
270 self._delims = delims
271 self._delims = delims
271 self._delim_expr = expr
272 self._delim_expr = expr
272
273
273 def split_line(self, line, cursor_pos=None):
274 def split_line(self, line, cursor_pos=None):
274 """Split a line of text with a cursor at the given position.
275 """Split a line of text with a cursor at the given position.
275 """
276 """
276 l = line if cursor_pos is None else line[:cursor_pos]
277 l = line if cursor_pos is None else line[:cursor_pos]
277 return self._delim_re.split(l)[-1]
278 return self._delim_re.split(l)[-1]
278
279
279
280
280 class Completer(Configurable):
281 class Completer(Configurable):
281
282
282 greedy = CBool(False, config=True,
283 greedy = CBool(False, config=True,
283 help="""Activate greedy completion
284 help="""Activate greedy completion
284
285
285 This will enable completion on elements of lists, results of function calls, etc.,
286 This will enable completion on elements of lists, results of function calls, etc.,
286 but can be unsafe because the code is actually evaluated on TAB.
287 but can be unsafe because the code is actually evaluated on TAB.
287 """
288 """
288 )
289 )
289
290
290
291
291 def __init__(self, namespace=None, global_namespace=None, **kwargs):
292 def __init__(self, namespace=None, global_namespace=None, **kwargs):
292 """Create a new completer for the command line.
293 """Create a new completer for the command line.
293
294
294 Completer(namespace=ns,global_namespace=ns2) -> completer instance.
295 Completer(namespace=ns,global_namespace=ns2) -> completer instance.
295
296
296 If unspecified, the default namespace where completions are performed
297 If unspecified, the default namespace where completions are performed
297 is __main__ (technically, __main__.__dict__). Namespaces should be
298 is __main__ (technically, __main__.__dict__). Namespaces should be
298 given as dictionaries.
299 given as dictionaries.
299
300
300 An optional second namespace can be given. This allows the completer
301 An optional second namespace can be given. This allows the completer
301 to handle cases where both the local and global scopes need to be
302 to handle cases where both the local and global scopes need to be
302 distinguished.
303 distinguished.
303
304
304 Completer instances should be used as the completion mechanism of
305 Completer instances should be used as the completion mechanism of
305 readline via the set_completer() call:
306 readline via the set_completer() call:
306
307
307 readline.set_completer(Completer(my_namespace).complete)
308 readline.set_completer(Completer(my_namespace).complete)
308 """
309 """
309
310
310 # Don't bind to namespace quite yet, but flag whether the user wants a
311 # Don't bind to namespace quite yet, but flag whether the user wants a
311 # specific namespace or to use __main__.__dict__. This will allow us
312 # specific namespace or to use __main__.__dict__. This will allow us
312 # to bind to __main__.__dict__ at completion time, not now.
313 # to bind to __main__.__dict__ at completion time, not now.
313 if namespace is None:
314 if namespace is None:
314 self.use_main_ns = 1
315 self.use_main_ns = 1
315 else:
316 else:
316 self.use_main_ns = 0
317 self.use_main_ns = 0
317 self.namespace = namespace
318 self.namespace = namespace
318
319
319 # The global namespace, if given, can be bound directly
320 # The global namespace, if given, can be bound directly
320 if global_namespace is None:
321 if global_namespace is None:
321 self.global_namespace = {}
322 self.global_namespace = {}
322 else:
323 else:
323 self.global_namespace = global_namespace
324 self.global_namespace = global_namespace
324
325
325 super(Completer, self).__init__(**kwargs)
326 super(Completer, self).__init__(**kwargs)
326
327
327 def complete(self, text, state):
328 def complete(self, text, state):
328 """Return the next possible completion for 'text'.
329 """Return the next possible completion for 'text'.
329
330
330 This is called successively with state == 0, 1, 2, ... until it
331 This is called successively with state == 0, 1, 2, ... until it
331 returns None. The completion should begin with 'text'.
332 returns None. The completion should begin with 'text'.
332
333
333 """
334 """
334 if self.use_main_ns:
335 if self.use_main_ns:
335 self.namespace = __main__.__dict__
336 self.namespace = __main__.__dict__
336
337
337 if state == 0:
338 if state == 0:
338 if "." in text:
339 if "." in text:
339 self.matches = self.attr_matches(text)
340 self.matches = self.attr_matches(text)
340 else:
341 else:
341 self.matches = self.global_matches(text)
342 self.matches = self.global_matches(text)
342 try:
343 try:
343 return self.matches[state]
344 return self.matches[state]
344 except IndexError:
345 except IndexError:
345 return None
346 return None
346
347
347 def global_matches(self, text):
348 def global_matches(self, text):
348 """Compute matches when text is a simple name.
349 """Compute matches when text is a simple name.
349
350
350 Return a list of all keywords, built-in functions and names currently
351 Return a list of all keywords, built-in functions and names currently
351 defined in self.namespace or self.global_namespace that match.
352 defined in self.namespace or self.global_namespace that match.
352
353
353 """
354 """
354 #print 'Completer->global_matches, txt=%r' % text # dbg
355 #print 'Completer->global_matches, txt=%r' % text # dbg
355 matches = []
356 matches = []
356 match_append = matches.append
357 match_append = matches.append
357 n = len(text)
358 n = len(text)
358 for lst in [keyword.kwlist,
359 for lst in [keyword.kwlist,
359 builtin_mod.__dict__.keys(),
360 builtin_mod.__dict__.keys(),
360 self.namespace.keys(),
361 self.namespace.keys(),
361 self.global_namespace.keys()]:
362 self.global_namespace.keys()]:
362 for word in lst:
363 for word in lst:
363 if word[:n] == text and word != "__builtins__":
364 if word[:n] == text and word != "__builtins__":
364 match_append(word)
365 match_append(word)
365 return matches
366 return matches
366
367
367 def attr_matches(self, text):
368 def attr_matches(self, text):
368 """Compute matches when text contains a dot.
369 """Compute matches when text contains a dot.
369
370
370 Assuming the text is of the form NAME.NAME....[NAME], and is
371 Assuming the text is of the form NAME.NAME....[NAME], and is
371 evaluatable in self.namespace or self.global_namespace, it will be
372 evaluatable in self.namespace or self.global_namespace, it will be
372 evaluated and its attributes (as revealed by dir()) are used as
373 evaluated and its attributes (as revealed by dir()) are used as
373 possible completions. (For class instances, class members are are
374 possible completions. (For class instances, class members are are
374 also considered.)
375 also considered.)
375
376
376 WARNING: this can still invoke arbitrary C code, if an object
377 WARNING: this can still invoke arbitrary C code, if an object
377 with a __getattr__ hook is evaluated.
378 with a __getattr__ hook is evaluated.
378
379
379 """
380 """
380
381
381 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
382 #io.rprint('Completer->attr_matches, txt=%r' % text) # dbg
382 # Another option, seems to work great. Catches things like ''.<tab>
383 # Another option, seems to work great. Catches things like ''.<tab>
383 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
384 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
384
385
385 if m:
386 if m:
386 expr, attr = m.group(1, 3)
387 expr, attr = m.group(1, 3)
387 elif self.greedy:
388 elif self.greedy:
388 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
389 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
389 if not m2:
390 if not m2:
390 return []
391 return []
391 expr, attr = m2.group(1,2)
392 expr, attr = m2.group(1,2)
392 else:
393 else:
393 return []
394 return []
394
395
395 try:
396 try:
396 obj = eval(expr, self.namespace)
397 obj = eval(expr, self.namespace)
397 except:
398 except:
398 try:
399 try:
399 obj = eval(expr, self.global_namespace)
400 obj = eval(expr, self.global_namespace)
400 except:
401 except:
401 return []
402 return []
402
403
403 if self.limit_to__all__ and hasattr(obj, '__all__'):
404 if self.limit_to__all__ and hasattr(obj, '__all__'):
404 words = get__all__entries(obj)
405 words = get__all__entries(obj)
405 else:
406 else:
406 words = dir2(obj)
407 words = dir2(obj)
407
408
408 try:
409 try:
409 words = generics.complete_object(obj, words)
410 words = generics.complete_object(obj, words)
410 except TryNext:
411 except TryNext:
411 pass
412 pass
412 except Exception:
413 except Exception:
413 # Silence errors from completion function
414 # Silence errors from completion function
414 #raise # dbg
415 #raise # dbg
415 pass
416 pass
416 # Build match list to return
417 # Build match list to return
417 n = len(attr)
418 n = len(attr)
418 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
419 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
419 return res
420 return res
420
421
421
422
422 def get__all__entries(obj):
423 def get__all__entries(obj):
423 """returns the strings in the __all__ attribute"""
424 """returns the strings in the __all__ attribute"""
424 try:
425 try:
425 words = getattr(obj, '__all__')
426 words = getattr(obj, '__all__')
426 except:
427 except:
427 return []
428 return []
428
429
429 return [w for w in words if isinstance(w, string_types)]
430 return [w for w in words if isinstance(w, string_types)]
430
431
431
432
432 def match_dict_keys(keys, prefix):
433 def match_dict_keys(keys, prefix):
433 """Used by dict_key_matches, matching the prefix to a list of keys"""
434 """Used by dict_key_matches, matching the prefix to a list of keys"""
434 if not prefix:
435 if not prefix:
435 return None, 0, [repr(k) for k in keys
436 return None, 0, [repr(k) for k in keys
436 if isinstance(k, (string_types, bytes))]
437 if isinstance(k, (string_types, bytes))]
437 quote_match = re.search('["\']', prefix)
438 quote_match = re.search('["\']', prefix)
438 quote = quote_match.group()
439 quote = quote_match.group()
439 try:
440 try:
440 prefix_str = eval(prefix + quote, {})
441 prefix_str = eval(prefix + quote, {})
441 except Exception:
442 except Exception:
442 return None, 0, []
443 return None, 0, []
443
444
444 token_match = re.search(r'\w*$', prefix, re.UNICODE)
445 token_match = re.search(r'\w*$', prefix, re.UNICODE)
445 token_start = token_match.start()
446 token_start = token_match.start()
446 token_prefix = token_match.group()
447 token_prefix = token_match.group()
447
448
448 # TODO: support bytes in Py3k
449 # TODO: support bytes in Py3k
449 matched = []
450 matched = []
450 for key in keys:
451 for key in keys:
451 try:
452 try:
452 if not key.startswith(prefix_str):
453 if not key.startswith(prefix_str):
453 continue
454 continue
454 except (AttributeError, TypeError, UnicodeError):
455 except (AttributeError, TypeError, UnicodeError):
455 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
456 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
456 continue
457 continue
457
458
458 # reformat remainder of key to begin with prefix
459 # reformat remainder of key to begin with prefix
459 rem = key[len(prefix_str):]
460 rem = key[len(prefix_str):]
460 # force repr wrapped in '
461 # force repr wrapped in '
461 rem_repr = repr(rem + '"')
462 rem_repr = repr(rem + '"')
462 if rem_repr.startswith('u') and prefix[0] not in 'uU':
463 if rem_repr.startswith('u') and prefix[0] not in 'uU':
463 # Found key is unicode, but prefix is Py2 string.
464 # Found key is unicode, but prefix is Py2 string.
464 # Therefore attempt to interpret key as string.
465 # Therefore attempt to interpret key as string.
465 try:
466 try:
466 rem_repr = repr(rem.encode('ascii') + '"')
467 rem_repr = repr(rem.encode('ascii') + '"')
467 except UnicodeEncodeError:
468 except UnicodeEncodeError:
468 continue
469 continue
469
470
470 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
471 rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
471 if quote == '"':
472 if quote == '"':
472 # The entered prefix is quoted with ",
473 # The entered prefix is quoted with ",
473 # but the match is quoted with '.
474 # but the match is quoted with '.
474 # A contained " hence needs escaping for comparison:
475 # A contained " hence needs escaping for comparison:
475 rem_repr = rem_repr.replace('"', '\\"')
476 rem_repr = rem_repr.replace('"', '\\"')
476
477
477 # then reinsert prefix from start of token
478 # then reinsert prefix from start of token
478 matched.append('%s%s' % (token_prefix, rem_repr))
479 matched.append('%s%s' % (token_prefix, rem_repr))
479 return quote, token_start, matched
480 return quote, token_start, matched
480
481
481
482
482 def _safe_isinstance(obj, module, class_name):
483 def _safe_isinstance(obj, module, class_name):
483 """Checks if obj is an instance of module.class_name if loaded
484 """Checks if obj is an instance of module.class_name if loaded
484 """
485 """
485 return (module in sys.modules and
486 return (module in sys.modules and
486 isinstance(obj, getattr(__import__(module), class_name)))
487 isinstance(obj, getattr(__import__(module), class_name)))
487
488
488
489
489
490
490 class IPCompleter(Completer):
491 class IPCompleter(Completer):
491 """Extension of the completer class with IPython-specific features"""
492 """Extension of the completer class with IPython-specific features"""
492
493
493 def _greedy_changed(self, name, old, new):
494 def _greedy_changed(self, name, old, new):
494 """update the splitter and readline delims when greedy is changed"""
495 """update the splitter and readline delims when greedy is changed"""
495 if new:
496 if new:
496 self.splitter.delims = GREEDY_DELIMS
497 self.splitter.delims = GREEDY_DELIMS
497 else:
498 else:
498 self.splitter.delims = DELIMS
499 self.splitter.delims = DELIMS
499
500
500 if self.readline:
501 if self.readline:
501 self.readline.set_completer_delims(self.splitter.delims)
502 self.readline.set_completer_delims(self.splitter.delims)
502
503
503 merge_completions = CBool(True, config=True,
504 merge_completions = CBool(True, config=True,
504 help="""Whether to merge completion results into a single list
505 help="""Whether to merge completion results into a single list
505
506
506 If False, only the completion results from the first non-empty
507 If False, only the completion results from the first non-empty
507 completer will be returned.
508 completer will be returned.
508 """
509 """
509 )
510 )
510 omit__names = Enum((0,1,2), default_value=2, config=True,
511 omit__names = Enum((0,1,2), default_value=2, config=True,
511 help="""Instruct the completer to omit private method names
512 help="""Instruct the completer to omit private method names
512
513
513 Specifically, when completing on ``object.<tab>``.
514 Specifically, when completing on ``object.<tab>``.
514
515
515 When 2 [default]: all names that start with '_' will be excluded.
516 When 2 [default]: all names that start with '_' will be excluded.
516
517
517 When 1: all 'magic' names (``__foo__``) will be excluded.
518 When 1: all 'magic' names (``__foo__``) will be excluded.
518
519
519 When 0: nothing will be excluded.
520 When 0: nothing will be excluded.
520 """
521 """
521 )
522 )
522 limit_to__all__ = CBool(default_value=False, config=True,
523 limit_to__all__ = CBool(default_value=False, config=True,
523 help="""Instruct the completer to use __all__ for the completion
524 help="""Instruct the completer to use __all__ for the completion
524
525
525 Specifically, when completing on ``object.<tab>``.
526 Specifically, when completing on ``object.<tab>``.
526
527
527 When True: only those names in obj.__all__ will be included.
528 When True: only those names in obj.__all__ will be included.
528
529
529 When False [default]: the __all__ attribute is ignored
530 When False [default]: the __all__ attribute is ignored
530 """
531 """
531 )
532 )
532
533
533 def __init__(self, shell=None, namespace=None, global_namespace=None,
534 def __init__(self, shell=None, namespace=None, global_namespace=None,
534 use_readline=True, config=None, **kwargs):
535 use_readline=True, config=None, **kwargs):
535 """IPCompleter() -> completer
536 """IPCompleter() -> completer
536
537
537 Return a completer object suitable for use by the readline library
538 Return a completer object suitable for use by the readline library
538 via readline.set_completer().
539 via readline.set_completer().
539
540
540 Inputs:
541 Inputs:
541
542
542 - shell: a pointer to the ipython shell itself. This is needed
543 - shell: a pointer to the ipython shell itself. This is needed
543 because this completer knows about magic functions, and those can
544 because this completer knows about magic functions, and those can
544 only be accessed via the ipython instance.
545 only be accessed via the ipython instance.
545
546
546 - namespace: an optional dict where completions are performed.
547 - namespace: an optional dict where completions are performed.
547
548
548 - global_namespace: secondary optional dict for completions, to
549 - global_namespace: secondary optional dict for completions, to
549 handle cases (such as IPython embedded inside functions) where
550 handle cases (such as IPython embedded inside functions) where
550 both Python scopes are visible.
551 both Python scopes are visible.
551
552
552 use_readline : bool, optional
553 use_readline : bool, optional
553 If true, use the readline library. This completer can still function
554 If true, use the readline library. This completer can still function
554 without readline, though in that case callers must provide some extra
555 without readline, though in that case callers must provide some extra
555 information on each call about the current line."""
556 information on each call about the current line."""
556
557
557 self.magic_escape = ESC_MAGIC
558 self.magic_escape = ESC_MAGIC
558 self.splitter = CompletionSplitter()
559 self.splitter = CompletionSplitter()
559
560
560 # Readline configuration, only used by the rlcompleter method.
561 # Readline configuration, only used by the rlcompleter method.
561 if use_readline:
562 if use_readline:
562 # We store the right version of readline so that later code
563 # We store the right version of readline so that later code
563 import IPython.utils.rlineimpl as readline
564 import IPython.utils.rlineimpl as readline
564 self.readline = readline
565 self.readline = readline
565 else:
566 else:
566 self.readline = None
567 self.readline = None
567
568
568 # _greedy_changed() depends on splitter and readline being defined:
569 # _greedy_changed() depends on splitter and readline being defined:
569 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
570 Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
570 config=config, **kwargs)
571 config=config, **kwargs)
571
572
572 # List where completion matches will be stored
573 # List where completion matches will be stored
573 self.matches = []
574 self.matches = []
574 self.shell = shell
575 self.shell = shell
575 # Regexp to split filenames with spaces in them
576 # Regexp to split filenames with spaces in them
576 self.space_name_re = re.compile(r'([^\\] )')
577 self.space_name_re = re.compile(r'([^\\] )')
577 # Hold a local ref. to glob.glob for speed
578 # Hold a local ref. to glob.glob for speed
578 self.glob = glob.glob
579 self.glob = glob.glob
579
580
580 # Determine if we are running on 'dumb' terminals, like (X)Emacs
581 # Determine if we are running on 'dumb' terminals, like (X)Emacs
581 # buffers, to avoid completion problems.
582 # buffers, to avoid completion problems.
582 term = os.environ.get('TERM','xterm')
583 term = os.environ.get('TERM','xterm')
583 self.dumb_terminal = term in ['dumb','emacs']
584 self.dumb_terminal = term in ['dumb','emacs']
584
585
585 # Special handling of backslashes needed in win32 platforms
586 # Special handling of backslashes needed in win32 platforms
586 if sys.platform == "win32":
587 if sys.platform == "win32":
587 self.clean_glob = self._clean_glob_win32
588 self.clean_glob = self._clean_glob_win32
588 else:
589 else:
589 self.clean_glob = self._clean_glob
590 self.clean_glob = self._clean_glob
590
591
591 #regexp to parse docstring for function signature
592 #regexp to parse docstring for function signature
592 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
593 self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
593 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
594 self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
594 #use this if positional argument name is also needed
595 #use this if positional argument name is also needed
595 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
596 #= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
596
597
597 # All active matcher routines for completion
598 # All active matcher routines for completion
598 self.matchers = [
599 self.matchers = [
599 self.python_matches,
600 self.python_matches,
600 self.file_matches,
601 self.file_matches,
601 self.magic_matches,
602 self.magic_matches,
602 self.python_func_kw_matches,
603 self.python_func_kw_matches,
603 self.dict_key_matches,
604 self.dict_key_matches,
604 self.latex_matches
605 ]
605 ]
606
606
607 def all_completions(self, text):
607 def all_completions(self, text):
608 """
608 """
609 Wrapper around the complete method for the benefit of emacs
609 Wrapper around the complete method for the benefit of emacs
610 and pydb.
610 and pydb.
611 """
611 """
612 return self.complete(text)[1]
612 return self.complete(text)[1]
613
613
614 def _clean_glob(self,text):
614 def _clean_glob(self,text):
615 return self.glob("%s*" % text)
615 return self.glob("%s*" % text)
616
616
617 def _clean_glob_win32(self,text):
617 def _clean_glob_win32(self,text):
618 return [f.replace("\\","/")
618 return [f.replace("\\","/")
619 for f in self.glob("%s*" % text)]
619 for f in self.glob("%s*" % text)]
620
620
621 def file_matches(self, text):
621 def file_matches(self, text):
622 """Match filenames, expanding ~USER type strings.
622 """Match filenames, expanding ~USER type strings.
623
623
624 Most of the seemingly convoluted logic in this completer is an
624 Most of the seemingly convoluted logic in this completer is an
625 attempt to handle filenames with spaces in them. And yet it's not
625 attempt to handle filenames with spaces in them. And yet it's not
626 quite perfect, because Python's readline doesn't expose all of the
626 quite perfect, because Python's readline doesn't expose all of the
627 GNU readline details needed for this to be done correctly.
627 GNU readline details needed for this to be done correctly.
628
628
629 For a filename with a space in it, the printed completions will be
629 For a filename with a space in it, the printed completions will be
630 only the parts after what's already been typed (instead of the
630 only the parts after what's already been typed (instead of the
631 full completions, as is normally done). I don't think with the
631 full completions, as is normally done). I don't think with the
632 current (as of Python 2.3) Python readline it's possible to do
632 current (as of Python 2.3) Python readline it's possible to do
633 better."""
633 better."""
634
634
635 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
635 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
636
636
637 # chars that require escaping with backslash - i.e. chars
637 # chars that require escaping with backslash - i.e. chars
638 # that readline treats incorrectly as delimiters, but we
638 # that readline treats incorrectly as delimiters, but we
639 # don't want to treat as delimiters in filename matching
639 # don't want to treat as delimiters in filename matching
640 # when escaped with backslash
640 # when escaped with backslash
641 if text.startswith('!'):
641 if text.startswith('!'):
642 text = text[1:]
642 text = text[1:]
643 text_prefix = '!'
643 text_prefix = '!'
644 else:
644 else:
645 text_prefix = ''
645 text_prefix = ''
646
646
647 text_until_cursor = self.text_until_cursor
647 text_until_cursor = self.text_until_cursor
648 # track strings with open quotes
648 # track strings with open quotes
649 open_quotes = has_open_quotes(text_until_cursor)
649 open_quotes = has_open_quotes(text_until_cursor)
650
650
651 if '(' in text_until_cursor or '[' in text_until_cursor:
651 if '(' in text_until_cursor or '[' in text_until_cursor:
652 lsplit = text
652 lsplit = text
653 else:
653 else:
654 try:
654 try:
655 # arg_split ~ shlex.split, but with unicode bugs fixed by us
655 # arg_split ~ shlex.split, but with unicode bugs fixed by us
656 lsplit = arg_split(text_until_cursor)[-1]
656 lsplit = arg_split(text_until_cursor)[-1]
657 except ValueError:
657 except ValueError:
658 # typically an unmatched ", or backslash without escaped char.
658 # typically an unmatched ", or backslash without escaped char.
659 if open_quotes:
659 if open_quotes:
660 lsplit = text_until_cursor.split(open_quotes)[-1]
660 lsplit = text_until_cursor.split(open_quotes)[-1]
661 else:
661 else:
662 return []
662 return []
663 except IndexError:
663 except IndexError:
664 # tab pressed on empty line
664 # tab pressed on empty line
665 lsplit = ""
665 lsplit = ""
666
666
667 if not open_quotes and lsplit != protect_filename(lsplit):
667 if not open_quotes and lsplit != protect_filename(lsplit):
668 # if protectables are found, do matching on the whole escaped name
668 # if protectables are found, do matching on the whole escaped name
669 has_protectables = True
669 has_protectables = True
670 text0,text = text,lsplit
670 text0,text = text,lsplit
671 else:
671 else:
672 has_protectables = False
672 has_protectables = False
673 text = os.path.expanduser(text)
673 text = os.path.expanduser(text)
674
674
675 if text == "":
675 if text == "":
676 return [text_prefix + protect_filename(f) for f in self.glob("*")]
676 return [text_prefix + protect_filename(f) for f in self.glob("*")]
677
677
678 # Compute the matches from the filesystem
678 # Compute the matches from the filesystem
679 m0 = self.clean_glob(text.replace('\\',''))
679 m0 = self.clean_glob(text.replace('\\',''))
680
680
681 if has_protectables:
681 if has_protectables:
682 # If we had protectables, we need to revert our changes to the
682 # If we had protectables, we need to revert our changes to the
683 # beginning of filename so that we don't double-write the part
683 # beginning of filename so that we don't double-write the part
684 # of the filename we have so far
684 # of the filename we have so far
685 len_lsplit = len(lsplit)
685 len_lsplit = len(lsplit)
686 matches = [text_prefix + text0 +
686 matches = [text_prefix + text0 +
687 protect_filename(f[len_lsplit:]) for f in m0]
687 protect_filename(f[len_lsplit:]) for f in m0]
688 else:
688 else:
689 if open_quotes:
689 if open_quotes:
690 # if we have a string with an open quote, we don't need to
690 # if we have a string with an open quote, we don't need to
691 # protect the names at all (and we _shouldn't_, as it
691 # protect the names at all (and we _shouldn't_, as it
692 # would cause bugs when the filesystem call is made).
692 # would cause bugs when the filesystem call is made).
693 matches = m0
693 matches = m0
694 else:
694 else:
695 matches = [text_prefix +
695 matches = [text_prefix +
696 protect_filename(f) for f in m0]
696 protect_filename(f) for f in m0]
697
697
698 #io.rprint('mm', matches) # dbg
698 #io.rprint('mm', matches) # dbg
699
699
700 # Mark directories in input list by appending '/' to their names.
700 # Mark directories in input list by appending '/' to their names.
701 matches = [x+'/' if os.path.isdir(x) else x for x in matches]
701 matches = [x+'/' if os.path.isdir(x) else x for x in matches]
702 return matches
702 return matches
703
703
704 def magic_matches(self, text):
704 def magic_matches(self, text):
705 """Match magics"""
705 """Match magics"""
706 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
706 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
707 # Get all shell magics now rather than statically, so magics loaded at
707 # Get all shell magics now rather than statically, so magics loaded at
708 # runtime show up too.
708 # runtime show up too.
709 lsm = self.shell.magics_manager.lsmagic()
709 lsm = self.shell.magics_manager.lsmagic()
710 line_magics = lsm['line']
710 line_magics = lsm['line']
711 cell_magics = lsm['cell']
711 cell_magics = lsm['cell']
712 pre = self.magic_escape
712 pre = self.magic_escape
713 pre2 = pre+pre
713 pre2 = pre+pre
714
714
715 # Completion logic:
715 # Completion logic:
716 # - user gives %%: only do cell magics
716 # - user gives %%: only do cell magics
717 # - user gives %: do both line and cell magics
717 # - user gives %: do both line and cell magics
718 # - no prefix: do both
718 # - no prefix: do both
719 # In other words, line magics are skipped if the user gives %% explicitly
719 # In other words, line magics are skipped if the user gives %% explicitly
720 bare_text = text.lstrip(pre)
720 bare_text = text.lstrip(pre)
721 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
721 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
722 if not text.startswith(pre2):
722 if not text.startswith(pre2):
723 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
723 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
724 return comp
724 return comp
725
725
726 def python_matches(self,text):
726 def python_matches(self,text):
727 """Match attributes or global python names"""
727 """Match attributes or global python names"""
728
728
729 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
729 #io.rprint('Completer->python_matches, txt=%r' % text) # dbg
730 if "." in text:
730 if "." in text:
731 try:
731 try:
732 matches = self.attr_matches(text)
732 matches = self.attr_matches(text)
733 if text.endswith('.') and self.omit__names:
733 if text.endswith('.') and self.omit__names:
734 if self.omit__names == 1:
734 if self.omit__names == 1:
735 # true if txt is _not_ a __ name, false otherwise:
735 # true if txt is _not_ a __ name, false otherwise:
736 no__name = (lambda txt:
736 no__name = (lambda txt:
737 re.match(r'.*\.__.*?__',txt) is None)
737 re.match(r'.*\.__.*?__',txt) is None)
738 else:
738 else:
739 # true if txt is _not_ a _ name, false otherwise:
739 # true if txt is _not_ a _ name, false otherwise:
740 no__name = (lambda txt:
740 no__name = (lambda txt:
741 re.match(r'.*\._.*?',txt) is None)
741 re.match(r'.*\._.*?',txt) is None)
742 matches = filter(no__name, matches)
742 matches = filter(no__name, matches)
743 except NameError:
743 except NameError:
744 # catches <undefined attributes>.<tab>
744 # catches <undefined attributes>.<tab>
745 matches = []
745 matches = []
746 else:
746 else:
747 matches = self.global_matches(text)
747 matches = self.global_matches(text)
748
748
749 return matches
749 return matches
750
750
751 def _default_arguments_from_docstring(self, doc):
751 def _default_arguments_from_docstring(self, doc):
752 """Parse the first line of docstring for call signature.
752 """Parse the first line of docstring for call signature.
753
753
754 Docstring should be of the form 'min(iterable[, key=func])\n'.
754 Docstring should be of the form 'min(iterable[, key=func])\n'.
755 It can also parse cython docstring of the form
755 It can also parse cython docstring of the form
756 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
756 'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
757 """
757 """
758 if doc is None:
758 if doc is None:
759 return []
759 return []
760
760
761 #care only the firstline
761 #care only the firstline
762 line = doc.lstrip().splitlines()[0]
762 line = doc.lstrip().splitlines()[0]
763
763
764 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
764 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
765 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
765 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
766 sig = self.docstring_sig_re.search(line)
766 sig = self.docstring_sig_re.search(line)
767 if sig is None:
767 if sig is None:
768 return []
768 return []
769 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
769 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
770 sig = sig.groups()[0].split(',')
770 sig = sig.groups()[0].split(',')
771 ret = []
771 ret = []
772 for s in sig:
772 for s in sig:
773 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
773 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
774 ret += self.docstring_kwd_re.findall(s)
774 ret += self.docstring_kwd_re.findall(s)
775 return ret
775 return ret
776
776
777 def _default_arguments(self, obj):
777 def _default_arguments(self, obj):
778 """Return the list of default arguments of obj if it is callable,
778 """Return the list of default arguments of obj if it is callable,
779 or empty list otherwise."""
779 or empty list otherwise."""
780 call_obj = obj
780 call_obj = obj
781 ret = []
781 ret = []
782 if inspect.isbuiltin(obj):
782 if inspect.isbuiltin(obj):
783 pass
783 pass
784 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
784 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
785 if inspect.isclass(obj):
785 if inspect.isclass(obj):
786 #for cython embededsignature=True the constructor docstring
786 #for cython embededsignature=True the constructor docstring
787 #belongs to the object itself not __init__
787 #belongs to the object itself not __init__
788 ret += self._default_arguments_from_docstring(
788 ret += self._default_arguments_from_docstring(
789 getattr(obj, '__doc__', ''))
789 getattr(obj, '__doc__', ''))
790 # for classes, check for __init__,__new__
790 # for classes, check for __init__,__new__
791 call_obj = (getattr(obj, '__init__', None) or
791 call_obj = (getattr(obj, '__init__', None) or
792 getattr(obj, '__new__', None))
792 getattr(obj, '__new__', None))
793 # for all others, check if they are __call__able
793 # for all others, check if they are __call__able
794 elif hasattr(obj, '__call__'):
794 elif hasattr(obj, '__call__'):
795 call_obj = obj.__call__
795 call_obj = obj.__call__
796
796
797 ret += self._default_arguments_from_docstring(
797 ret += self._default_arguments_from_docstring(
798 getattr(call_obj, '__doc__', ''))
798 getattr(call_obj, '__doc__', ''))
799
799
800 try:
800 try:
801 args,_,_1,defaults = inspect.getargspec(call_obj)
801 args,_,_1,defaults = inspect.getargspec(call_obj)
802 if defaults:
802 if defaults:
803 ret+=args[-len(defaults):]
803 ret+=args[-len(defaults):]
804 except TypeError:
804 except TypeError:
805 pass
805 pass
806
806
807 return list(set(ret))
807 return list(set(ret))
808
808
809 def python_func_kw_matches(self,text):
809 def python_func_kw_matches(self,text):
810 """Match named parameters (kwargs) of the last open function"""
810 """Match named parameters (kwargs) of the last open function"""
811
811
812 if "." in text: # a parameter cannot be dotted
812 if "." in text: # a parameter cannot be dotted
813 return []
813 return []
814 try: regexp = self.__funcParamsRegex
814 try: regexp = self.__funcParamsRegex
815 except AttributeError:
815 except AttributeError:
816 regexp = self.__funcParamsRegex = re.compile(r'''
816 regexp = self.__funcParamsRegex = re.compile(r'''
817 '.*?(?<!\\)' | # single quoted strings or
817 '.*?(?<!\\)' | # single quoted strings or
818 ".*?(?<!\\)" | # double quoted strings or
818 ".*?(?<!\\)" | # double quoted strings or
819 \w+ | # identifier
819 \w+ | # identifier
820 \S # other characters
820 \S # other characters
821 ''', re.VERBOSE | re.DOTALL)
821 ''', re.VERBOSE | re.DOTALL)
822 # 1. find the nearest identifier that comes before an unclosed
822 # 1. find the nearest identifier that comes before an unclosed
823 # parenthesis before the cursor
823 # parenthesis before the cursor
824 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
824 # e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
825 tokens = regexp.findall(self.text_until_cursor)
825 tokens = regexp.findall(self.text_until_cursor)
826 tokens.reverse()
826 tokens.reverse()
827 iterTokens = iter(tokens); openPar = 0
827 iterTokens = iter(tokens); openPar = 0
828
828
829 for token in iterTokens:
829 for token in iterTokens:
830 if token == ')':
830 if token == ')':
831 openPar -= 1
831 openPar -= 1
832 elif token == '(':
832 elif token == '(':
833 openPar += 1
833 openPar += 1
834 if openPar > 0:
834 if openPar > 0:
835 # found the last unclosed parenthesis
835 # found the last unclosed parenthesis
836 break
836 break
837 else:
837 else:
838 return []
838 return []
839 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
839 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
840 ids = []
840 ids = []
841 isId = re.compile(r'\w+$').match
841 isId = re.compile(r'\w+$').match
842
842
843 while True:
843 while True:
844 try:
844 try:
845 ids.append(next(iterTokens))
845 ids.append(next(iterTokens))
846 if not isId(ids[-1]):
846 if not isId(ids[-1]):
847 ids.pop(); break
847 ids.pop(); break
848 if not next(iterTokens) == '.':
848 if not next(iterTokens) == '.':
849 break
849 break
850 except StopIteration:
850 except StopIteration:
851 break
851 break
852 # lookup the candidate callable matches either using global_matches
852 # lookup the candidate callable matches either using global_matches
853 # or attr_matches for dotted names
853 # or attr_matches for dotted names
854 if len(ids) == 1:
854 if len(ids) == 1:
855 callableMatches = self.global_matches(ids[0])
855 callableMatches = self.global_matches(ids[0])
856 else:
856 else:
857 callableMatches = self.attr_matches('.'.join(ids[::-1]))
857 callableMatches = self.attr_matches('.'.join(ids[::-1]))
858 argMatches = []
858 argMatches = []
859 for callableMatch in callableMatches:
859 for callableMatch in callableMatches:
860 try:
860 try:
861 namedArgs = self._default_arguments(eval(callableMatch,
861 namedArgs = self._default_arguments(eval(callableMatch,
862 self.namespace))
862 self.namespace))
863 except:
863 except:
864 continue
864 continue
865
865
866 for namedArg in namedArgs:
866 for namedArg in namedArgs:
867 if namedArg.startswith(text):
867 if namedArg.startswith(text):
868 argMatches.append("%s=" %namedArg)
868 argMatches.append("%s=" %namedArg)
869 return argMatches
869 return argMatches
870
870
871 def dict_key_matches(self, text):
871 def dict_key_matches(self, text):
872 "Match string keys in a dictionary, after e.g. 'foo[' "
872 "Match string keys in a dictionary, after e.g. 'foo[' "
873 def get_keys(obj):
873 def get_keys(obj):
874 # Only allow completion for known in-memory dict-like types
874 # Only allow completion for known in-memory dict-like types
875 if isinstance(obj, dict) or\
875 if isinstance(obj, dict) or\
876 _safe_isinstance(obj, 'pandas', 'DataFrame'):
876 _safe_isinstance(obj, 'pandas', 'DataFrame'):
877 try:
877 try:
878 return list(obj.keys())
878 return list(obj.keys())
879 except Exception:
879 except Exception:
880 return []
880 return []
881 elif _safe_isinstance(obj, 'numpy', 'ndarray'):
881 elif _safe_isinstance(obj, 'numpy', 'ndarray'):
882 return obj.dtype.names or []
882 return obj.dtype.names or []
883 return []
883 return []
884
884
885 try:
885 try:
886 regexps = self.__dict_key_regexps
886 regexps = self.__dict_key_regexps
887 except AttributeError:
887 except AttributeError:
888 dict_key_re_fmt = r'''(?x)
888 dict_key_re_fmt = r'''(?x)
889 ( # match dict-referring expression wrt greedy setting
889 ( # match dict-referring expression wrt greedy setting
890 %s
890 %s
891 )
891 )
892 \[ # open bracket
892 \[ # open bracket
893 \s* # and optional whitespace
893 \s* # and optional whitespace
894 ([uUbB]? # string prefix (r not handled)
894 ([uUbB]? # string prefix (r not handled)
895 (?: # unclosed string
895 (?: # unclosed string
896 '(?:[^']|(?<!\\)\\')*
896 '(?:[^']|(?<!\\)\\')*
897 |
897 |
898 "(?:[^"]|(?<!\\)\\")*
898 "(?:[^"]|(?<!\\)\\")*
899 )
899 )
900 )?
900 )?
901 $
901 $
902 '''
902 '''
903 regexps = self.__dict_key_regexps = {
903 regexps = self.__dict_key_regexps = {
904 False: re.compile(dict_key_re_fmt % '''
904 False: re.compile(dict_key_re_fmt % '''
905 # identifiers separated by .
905 # identifiers separated by .
906 (?!\d)\w+
906 (?!\d)\w+
907 (?:\.(?!\d)\w+)*
907 (?:\.(?!\d)\w+)*
908 '''),
908 '''),
909 True: re.compile(dict_key_re_fmt % '''
909 True: re.compile(dict_key_re_fmt % '''
910 .+
910 .+
911 ''')
911 ''')
912 }
912 }
913
913
914 match = regexps[self.greedy].search(self.text_until_cursor)
914 match = regexps[self.greedy].search(self.text_until_cursor)
915 if match is None:
915 if match is None:
916 return []
916 return []
917
917
918 expr, prefix = match.groups()
918 expr, prefix = match.groups()
919 try:
919 try:
920 obj = eval(expr, self.namespace)
920 obj = eval(expr, self.namespace)
921 except Exception:
921 except Exception:
922 try:
922 try:
923 obj = eval(expr, self.global_namespace)
923 obj = eval(expr, self.global_namespace)
924 except Exception:
924 except Exception:
925 return []
925 return []
926
926
927 keys = get_keys(obj)
927 keys = get_keys(obj)
928 if not keys:
928 if not keys:
929 return keys
929 return keys
930 closing_quote, token_offset, matches = match_dict_keys(keys, prefix)
930 closing_quote, token_offset, matches = match_dict_keys(keys, prefix)
931 if not matches:
931 if not matches:
932 return matches
932 return matches
933
933
934 # get the cursor position of
934 # get the cursor position of
935 # - the text being completed
935 # - the text being completed
936 # - the start of the key text
936 # - the start of the key text
937 # - the start of the completion
937 # - the start of the completion
938 text_start = len(self.text_until_cursor) - len(text)
938 text_start = len(self.text_until_cursor) - len(text)
939 if prefix:
939 if prefix:
940 key_start = match.start(2)
940 key_start = match.start(2)
941 completion_start = key_start + token_offset
941 completion_start = key_start + token_offset
942 else:
942 else:
943 key_start = completion_start = match.end()
943 key_start = completion_start = match.end()
944
944
945 # grab the leading prefix, to make sure all completions start with `text`
945 # grab the leading prefix, to make sure all completions start with `text`
946 if text_start > key_start:
946 if text_start > key_start:
947 leading = ''
947 leading = ''
948 else:
948 else:
949 leading = text[text_start:completion_start]
949 leading = text[text_start:completion_start]
950
950
951 # the index of the `[` character
951 # the index of the `[` character
952 bracket_idx = match.end(1)
952 bracket_idx = match.end(1)
953
953
954 # append closing quote and bracket as appropriate
954 # append closing quote and bracket as appropriate
955 # this is *not* appropriate if the opening quote or bracket is outside
955 # this is *not* appropriate if the opening quote or bracket is outside
956 # the text given to this method
956 # the text given to this method
957 suf = ''
957 suf = ''
958 continuation = self.line_buffer[len(self.text_until_cursor):]
958 continuation = self.line_buffer[len(self.text_until_cursor):]
959 if key_start > text_start and closing_quote:
959 if key_start > text_start and closing_quote:
960 # quotes were opened inside text, maybe close them
960 # quotes were opened inside text, maybe close them
961 if continuation.startswith(closing_quote):
961 if continuation.startswith(closing_quote):
962 continuation = continuation[len(closing_quote):]
962 continuation = continuation[len(closing_quote):]
963 else:
963 else:
964 suf += closing_quote
964 suf += closing_quote
965 if bracket_idx > text_start:
965 if bracket_idx > text_start:
966 # brackets were opened inside text, maybe close them
966 # brackets were opened inside text, maybe close them
967 if not continuation.startswith(']'):
967 if not continuation.startswith(']'):
968 suf += ']'
968 suf += ']'
969
969
970 return [leading + k + suf for k in matches]
970 return [leading + k + suf for k in matches]
971
971
972 def latex_matches(self, text):
972 def latex_matches(self, text):
973 if text.startswith('\\foo'):
973 slashpos = text.rfind('\\')
974 return ['foo']
974 if slashpos > -1:
975 else:
975 s = text[slashpos:]
976 return []
976 if s in latex_symbols:
977 return s, [latex_symbols[s]]
978 return u'', []
977
979
978 def dispatch_custom_completer(self, text):
980 def dispatch_custom_completer(self, text):
979 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
981 #io.rprint("Custom! '%s' %s" % (text, self.custom_completers)) # dbg
980 line = self.line_buffer
982 line = self.line_buffer
981 if not line.strip():
983 if not line.strip():
982 return None
984 return None
983
985
984 # Create a little structure to pass all the relevant information about
986 # Create a little structure to pass all the relevant information about
985 # the current completion to any custom completer.
987 # the current completion to any custom completer.
986 event = Bunch()
988 event = Bunch()
987 event.line = line
989 event.line = line
988 event.symbol = text
990 event.symbol = text
989 cmd = line.split(None,1)[0]
991 cmd = line.split(None,1)[0]
990 event.command = cmd
992 event.command = cmd
991 event.text_until_cursor = self.text_until_cursor
993 event.text_until_cursor = self.text_until_cursor
992
994
993 #print "\ncustom:{%s]\n" % event # dbg
995 #print "\ncustom:{%s]\n" % event # dbg
994
996
995 # for foo etc, try also to find completer for %foo
997 # for foo etc, try also to find completer for %foo
996 if not cmd.startswith(self.magic_escape):
998 if not cmd.startswith(self.magic_escape):
997 try_magic = self.custom_completers.s_matches(
999 try_magic = self.custom_completers.s_matches(
998 self.magic_escape + cmd)
1000 self.magic_escape + cmd)
999 else:
1001 else:
1000 try_magic = []
1002 try_magic = []
1001
1003
1002 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1004 for c in itertools.chain(self.custom_completers.s_matches(cmd),
1003 try_magic,
1005 try_magic,
1004 self.custom_completers.flat_matches(self.text_until_cursor)):
1006 self.custom_completers.flat_matches(self.text_until_cursor)):
1005 #print "try",c # dbg
1007 #print "try",c # dbg
1006 try:
1008 try:
1007 res = c(event)
1009 res = c(event)
1008 if res:
1010 if res:
1009 # first, try case sensitive match
1011 # first, try case sensitive match
1010 withcase = [r for r in res if r.startswith(text)]
1012 withcase = [r for r in res if r.startswith(text)]
1011 if withcase:
1013 if withcase:
1012 return withcase
1014 return withcase
1013 # if none, then case insensitive ones are ok too
1015 # if none, then case insensitive ones are ok too
1014 text_low = text.lower()
1016 text_low = text.lower()
1015 return [r for r in res if r.lower().startswith(text_low)]
1017 return [r for r in res if r.lower().startswith(text_low)]
1016 except TryNext:
1018 except TryNext:
1017 pass
1019 pass
1018
1020
1019 return None
1021 return None
1020
1022
1021 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1023 def complete(self, text=None, line_buffer=None, cursor_pos=None):
1022 """Find completions for the given text and line context.
1024 """Find completions for the given text and line context.
1023
1025
1024 Note that both the text and the line_buffer are optional, but at least
1026 Note that both the text and the line_buffer are optional, but at least
1025 one of them must be given.
1027 one of them must be given.
1026
1028
1027 Parameters
1029 Parameters
1028 ----------
1030 ----------
1029 text : string, optional
1031 text : string, optional
1030 Text to perform the completion on. If not given, the line buffer
1032 Text to perform the completion on. If not given, the line buffer
1031 is split using the instance's CompletionSplitter object.
1033 is split using the instance's CompletionSplitter object.
1032
1034
1033 line_buffer : string, optional
1035 line_buffer : string, optional
1034 If not given, the completer attempts to obtain the current line
1036 If not given, the completer attempts to obtain the current line
1035 buffer via readline. This keyword allows clients which are
1037 buffer via readline. This keyword allows clients which are
1036 requesting for text completions in non-readline contexts to inform
1038 requesting for text completions in non-readline contexts to inform
1037 the completer of the entire text.
1039 the completer of the entire text.
1038
1040
1039 cursor_pos : int, optional
1041 cursor_pos : int, optional
1040 Index of the cursor in the full line buffer. Should be provided by
1042 Index of the cursor in the full line buffer. Should be provided by
1041 remote frontends where kernel has no access to frontend state.
1043 remote frontends where kernel has no access to frontend state.
1042
1044
1043 Returns
1045 Returns
1044 -------
1046 -------
1045 text : str
1047 text : str
1046 Text that was actually used in the completion.
1048 Text that was actually used in the completion.
1047
1049
1048 matches : list
1050 matches : list
1049 A list of completion matches.
1051 A list of completion matches.
1050 """
1052 """
1051 io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1053 # io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1052
1054
1053 # if the cursor position isn't given, the only sane assumption we can
1055 # if the cursor position isn't given, the only sane assumption we can
1054 # make is that it's at the end of the line (the common case)
1056 # make is that it's at the end of the line (the common case)
1055 if cursor_pos is None:
1057 if cursor_pos is None:
1056 cursor_pos = len(line_buffer) if text is None else len(text)
1058 cursor_pos = len(line_buffer) if text is None else len(text)
1057
1059
1060 latex_text = text if not line_buffer else line_buffer[:cursor_pos]
1061 latex_text, latex_matches = self.latex_matches(latex_text)
1062 if latex_matches:
1063 return latex_text, latex_matches
1064
1058 # if text is either None or an empty string, rely on the line buffer
1065 # if text is either None or an empty string, rely on the line buffer
1059 if not text:
1066 if not text:
1060 text = self.splitter.split_line(line_buffer, cursor_pos)
1067 text = self.splitter.split_line(line_buffer, cursor_pos)
1061
1068
1062 # If no line buffer is given, assume the input text is all there was
1069 # If no line buffer is given, assume the input text is all there was
1063 if line_buffer is None:
1070 if line_buffer is None:
1064 line_buffer = text
1071 line_buffer = text
1065
1072
1066 self.line_buffer = line_buffer
1073 self.line_buffer = line_buffer
1067 self.text_until_cursor = self.line_buffer[:cursor_pos]
1074 self.text_until_cursor = self.line_buffer[:cursor_pos]
1068 io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1075 # io.rprint('COMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
1069
1076
1070 # Start with a clean slate of completions
1077 # Start with a clean slate of completions
1071 self.matches[:] = []
1078 self.matches[:] = []
1072 custom_res = self.dispatch_custom_completer(text)
1079 custom_res = self.dispatch_custom_completer(text)
1073 if custom_res is not None:
1080 if custom_res is not None:
1074 # did custom completers produce something?
1081 # did custom completers produce something?
1075 self.matches = custom_res
1082 self.matches = custom_res
1076 else:
1083 else:
1077 # Extend the list of completions with the results of each
1084 # Extend the list of completions with the results of each
1078 # matcher, so we return results to the user from all
1085 # matcher, so we return results to the user from all
1079 # namespaces.
1086 # namespaces.
1080 if self.merge_completions:
1087 if self.merge_completions:
1081 self.matches = []
1088 self.matches = []
1082 for matcher in self.matchers:
1089 for matcher in self.matchers:
1083 try:
1090 try:
1084 self.matches.extend(matcher(text))
1091 self.matches.extend(matcher(text))
1085 except:
1092 except:
1086 # Show the ugly traceback if the matcher causes an
1093 # Show the ugly traceback if the matcher causes an
1087 # exception, but do NOT crash the kernel!
1094 # exception, but do NOT crash the kernel!
1088 sys.excepthook(*sys.exc_info())
1095 sys.excepthook(*sys.exc_info())
1089 else:
1096 else:
1090 for matcher in self.matchers:
1097 for matcher in self.matchers:
1091 self.matches = matcher(text)
1098 self.matches = matcher(text)
1092 if self.matches:
1099 if self.matches:
1093 break
1100 break
1094 # FIXME: we should extend our api to return a dict with completions for
1101 # FIXME: we should extend our api to return a dict with completions for
1095 # different types of objects. The rlcomplete() method could then
1102 # different types of objects. The rlcomplete() method could then
1096 # simply collapse the dict into a list for readline, but we'd have
1103 # simply collapse the dict into a list for readline, but we'd have
1097 # richer completion semantics in other evironments.
1104 # richer completion semantics in other evironments.
1098
1105
1099 # use penalize_magics_key to put magics after variables with same name
1106 # use penalize_magics_key to put magics after variables with same name
1100 self.matches = sorted(set(self.matches), key=penalize_magics_key)
1107 self.matches = sorted(set(self.matches), key=penalize_magics_key)
1101
1108
1102 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
1109 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
1103 return text, self.matches
1110 return text, self.matches
1104
1111
1105 def rlcomplete(self, text, state):
1112 def rlcomplete(self, text, state):
1106 """Return the state-th possible completion for 'text'.
1113 """Return the state-th possible completion for 'text'.
1107
1114
1108 This is called successively with state == 0, 1, 2, ... until it
1115 This is called successively with state == 0, 1, 2, ... until it
1109 returns None. The completion should begin with 'text'.
1116 returns None. The completion should begin with 'text'.
1110
1117
1111 Parameters
1118 Parameters
1112 ----------
1119 ----------
1113 text : string
1120 text : string
1114 Text to perform the completion on.
1121 Text to perform the completion on.
1115
1122
1116 state : int
1123 state : int
1117 Counter used by readline.
1124 Counter used by readline.
1118 """
1125 """
1119 if state==0:
1126 if state==0:
1120
1127
1121 self.line_buffer = line_buffer = self.readline.get_line_buffer()
1128 self.line_buffer = line_buffer = self.readline.get_line_buffer()
1122 cursor_pos = self.readline.get_endidx()
1129 cursor_pos = self.readline.get_endidx()
1123
1130
1124 #io.rprint("\nRLCOMPLETE: %r %r %r" %
1131 #io.rprint("\nRLCOMPLETE: %r %r %r" %
1125 # (text, line_buffer, cursor_pos) ) # dbg
1132 # (text, line_buffer, cursor_pos) ) # dbg
1126
1133
1127 # if there is only a tab on a line with only whitespace, instead of
1134 # if there is only a tab on a line with only whitespace, instead of
1128 # the mostly useless 'do you want to see all million completions'
1135 # the mostly useless 'do you want to see all million completions'
1129 # message, just do the right thing and give the user his tab!
1136 # message, just do the right thing and give the user his tab!
1130 # Incidentally, this enables pasting of tabbed text from an editor
1137 # Incidentally, this enables pasting of tabbed text from an editor
1131 # (as long as autoindent is off).
1138 # (as long as autoindent is off).
1132
1139
1133 # It should be noted that at least pyreadline still shows file
1140 # It should be noted that at least pyreadline still shows file
1134 # completions - is there a way around it?
1141 # completions - is there a way around it?
1135
1142
1136 # don't apply this on 'dumb' terminals, such as emacs buffers, so
1143 # don't apply this on 'dumb' terminals, such as emacs buffers, so
1137 # we don't interfere with their own tab-completion mechanism.
1144 # we don't interfere with their own tab-completion mechanism.
1138 if not (self.dumb_terminal or line_buffer.strip()):
1145 if not (self.dumb_terminal or line_buffer.strip()):
1139 self.readline.insert_text('\t')
1146 self.readline.insert_text('\t')
1140 sys.stdout.flush()
1147 sys.stdout.flush()
1141 return None
1148 return None
1142
1149
1143 # Note: debugging exceptions that may occur in completion is very
1150 # Note: debugging exceptions that may occur in completion is very
1144 # tricky, because readline unconditionally silences them. So if
1151 # tricky, because readline unconditionally silences them. So if
1145 # during development you suspect a bug in the completion code, turn
1152 # during development you suspect a bug in the completion code, turn
1146 # this flag on temporarily by uncommenting the second form (don't
1153 # this flag on temporarily by uncommenting the second form (don't
1147 # flip the value in the first line, as the '# dbg' marker can be
1154 # flip the value in the first line, as the '# dbg' marker can be
1148 # automatically detected and is used elsewhere).
1155 # automatically detected and is used elsewhere).
1149 DEBUG = False
1156 DEBUG = False
1150 #DEBUG = True # dbg
1157 #DEBUG = True # dbg
1151 if DEBUG:
1158 if DEBUG:
1152 try:
1159 try:
1153 self.complete(text, line_buffer, cursor_pos)
1160 self.complete(text, line_buffer, cursor_pos)
1154 except:
1161 except:
1155 import traceback; traceback.print_exc()
1162 import traceback; traceback.print_exc()
1156 else:
1163 else:
1157 # The normal production version is here
1164 # The normal production version is here
1158
1165
1159 # This method computes the self.matches array
1166 # This method computes the self.matches array
1160 self.complete(text, line_buffer, cursor_pos)
1167 self.complete(text, line_buffer, cursor_pos)
1161
1168
1162 try:
1169 try:
1163 return self.matches[state]
1170 return self.matches[state]
1164 except IndexError:
1171 except IndexError:
1165 return None
1172 return None
General Comments 0
You need to be logged in to leave comments. Login now