##// END OF EJS Templates
Fix bug in tab completion of filenames when quotes are present....
Fernando Perez -
Show More
@@ -1,849 +1,880 b''
1 """Word completion for IPython.
1 """Word completion for IPython.
2
2
3 This module is a fork of the rlcompleter module in the Python standard
3 This module is a fork of the rlcompleter module in the Python standard
4 library. The original enhancements made to rlcompleter have been sent
4 library. The original enhancements made to rlcompleter have been sent
5 upstream and were accepted as of Python 2.3, but we need a lot more
5 upstream and were accepted as of Python 2.3, but we need a lot more
6 functionality specific to IPython, so this module will continue to live as an
6 functionality specific to IPython, so this module will continue to live as an
7 IPython-specific utility.
7 IPython-specific utility.
8
8
9 Original rlcompleter documentation:
9 Original rlcompleter documentation:
10
10
11 This requires the latest extension to the readline module (the
11 This requires the latest extension to the readline module (the
12 completes keywords, built-ins and globals in __main__; when completing
12 completes keywords, built-ins and globals in __main__; when completing
13 NAME.NAME..., it evaluates (!) the expression up to the last dot and
13 NAME.NAME..., it evaluates (!) the expression up to the last dot and
14 completes its attributes.
14 completes its attributes.
15
15
16 It's very cool to do "import string" type "string.", hit the
16 It's very cool to do "import string" type "string.", hit the
17 completion key (twice), and see the list of names defined by the
17 completion key (twice), and see the list of names defined by the
18 string module!
18 string module!
19
19
20 Tip: to use the tab key as the completion key, call
20 Tip: to use the tab key as the completion key, call
21
21
22 readline.parse_and_bind("tab: complete")
22 readline.parse_and_bind("tab: complete")
23
23
24 Notes:
24 Notes:
25
25
26 - Exceptions raised by the completer function are *ignored* (and
26 - Exceptions raised by the completer function are *ignored* (and
27 generally cause the completion to fail). This is a feature -- since
27 generally cause the completion to fail). This is a feature -- since
28 readline sets the tty device in raw (or cbreak) mode, printing a
28 readline sets the tty device in raw (or cbreak) mode, printing a
29 traceback wouldn't work well without some complicated hoopla to save,
29 traceback wouldn't work well without some complicated hoopla to save,
30 reset and restore the tty state.
30 reset and restore the tty state.
31
31
32 - The evaluation of the NAME.NAME... form may cause arbitrary
32 - The evaluation of the NAME.NAME... form may cause arbitrary
33 application defined code to be executed if an object with a
33 application defined code to be executed if an object with a
34 __getattr__ hook is found. Since it is the responsibility of the
34 __getattr__ hook is found. Since it is the responsibility of the
35 application (or the user) to enable this feature, I consider this an
35 application (or the user) to enable this feature, I consider this an
36 acceptable risk. More complicated expressions (e.g. function calls or
36 acceptable risk. More complicated expressions (e.g. function calls or
37 indexing operations) are *not* evaluated.
37 indexing operations) are *not* evaluated.
38
38
39 - GNU readline is also used by the built-in functions input() and
39 - GNU readline is also used by the built-in functions input() and
40 raw_input(), and thus these also benefit/suffer from the completer
40 raw_input(), and thus these also benefit/suffer from the completer
41 features. Clearly an interactive application can benefit by
41 features. Clearly an interactive application can benefit by
42 specifying its own completer function and using raw_input() for all
42 specifying its own completer function and using raw_input() for all
43 its input.
43 its input.
44
44
45 - When the original stdin is not a tty device, GNU readline is never
45 - When the original stdin is not a tty device, GNU readline is never
46 used, and this module (and the readline module) are silently inactive.
46 used, and this module (and the readline module) are silently inactive.
47 """
47 """
48
48
49 #*****************************************************************************
49 #*****************************************************************************
50 #
50 #
51 # Since this file is essentially a minimally modified copy of the rlcompleter
51 # Since this file is essentially a minimally modified copy of the rlcompleter
52 # module which is part of the standard Python distribution, I assume that the
52 # module which is part of the standard Python distribution, I assume that the
53 # proper procedure is to maintain its copyright as belonging to the Python
53 # proper procedure is to maintain its copyright as belonging to the Python
54 # Software Foundation (in addition to my own, for all new code).
54 # Software Foundation (in addition to my own, for all new code).
55 #
55 #
56 # Copyright (C) 2008-2010 IPython Development Team
56 # Copyright (C) 2008-2010 IPython Development Team
57 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
57 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 #
59 #
60 # Distributed under the terms of the BSD License. The full license is in
60 # Distributed under the terms of the BSD License. The full license is in
61 # the file COPYING, distributed as part of this software.
61 # the file COPYING, distributed as part of this software.
62 #
62 #
63 #*****************************************************************************
63 #*****************************************************************************
64 from __future__ import print_function
64 from __future__ import print_function
65
65
66 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
67 # Imports
67 # Imports
68 #-----------------------------------------------------------------------------
68 #-----------------------------------------------------------------------------
69
69
70 import __builtin__
70 import __builtin__
71 import __main__
71 import __main__
72 import glob
72 import glob
73 import inspect
73 import inspect
74 import itertools
74 import itertools
75 import keyword
75 import keyword
76 import os
76 import os
77 import re
77 import re
78 import shlex
78 import shlex
79 import sys
79 import sys
80
80
81 from IPython.core.error import TryNext
81 from IPython.core.error import TryNext
82 from IPython.core.prefilter import ESC_MAGIC
82 from IPython.core.prefilter import ESC_MAGIC
83 from IPython.utils import generics
83 from IPython.utils import generics
84 from IPython.utils import io
84 from IPython.utils import io
85 from IPython.utils.dir2 import dir2
85 from IPython.utils.dir2 import dir2
86 from IPython.utils.process import arg_split
86 from IPython.utils.process import arg_split
87
87
88 #-----------------------------------------------------------------------------
88 #-----------------------------------------------------------------------------
89 # Globals
89 # Globals
90 #-----------------------------------------------------------------------------
90 #-----------------------------------------------------------------------------
91
91
92 # Public API
92 # Public API
93 __all__ = ['Completer','IPCompleter']
93 __all__ = ['Completer','IPCompleter']
94
94
95 if sys.platform == 'win32':
95 if sys.platform == 'win32':
96 PROTECTABLES = ' '
96 PROTECTABLES = ' '
97 else:
97 else:
98 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
98 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
99
99
100 #-----------------------------------------------------------------------------
100 #-----------------------------------------------------------------------------
101 # Main functions and classes
101 # Main functions and classes
102 #-----------------------------------------------------------------------------
102 #-----------------------------------------------------------------------------
103
103
104 def has_open_quotes(s):
105 """Return whether a string has open quotes.
106
107 This simply counts whether the number of quote characters of either type in
108 the string is odd.
109
110 Returns
111 -------
112 If there is an open quote, the quote character is returned. Else, return
113 False.
114 """
115 # We check " first, then ', so complex cases with nested quotes will get
116 # the " to take precedence.
117 if s.count('"') % 2:
118 return '"'
119 elif s.count("'") % 2:
120 return "'"
121 else:
122 return False
123
124
104 def protect_filename(s):
125 def protect_filename(s):
105 """Escape a string to protect certain characters."""
126 """Escape a string to protect certain characters."""
106
127
107 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
128 return "".join([(ch in PROTECTABLES and '\\' + ch or ch)
108 for ch in s])
129 for ch in s])
109
130
110
131
111 def mark_dirs(matches):
132 def mark_dirs(matches):
112 """Mark directories in input list by appending '/' to their names."""
133 """Mark directories in input list by appending '/' to their names."""
113 out = []
134 out = []
114 isdir = os.path.isdir
135 isdir = os.path.isdir
115 for x in matches:
136 for x in matches:
116 if isdir(x):
137 if isdir(x):
117 out.append(x+'/')
138 out.append(x+'/')
118 else:
139 else:
119 out.append(x)
140 out.append(x)
120 return out
141 return out
121
142
122
143
123 def expand_user(path):
144 def expand_user(path):
124 """Expand '~'-style usernames in strings.
145 """Expand '~'-style usernames in strings.
125
146
126 This is similar to :func:`os.path.expanduser`, but it computes and returns
147 This is similar to :func:`os.path.expanduser`, but it computes and returns
127 extra information that will be useful if the input was being used in
148 extra information that will be useful if the input was being used in
128 computing completions, and you wish to return the completions with the
149 computing completions, and you wish to return the completions with the
129 original '~' instead of its expanded value.
150 original '~' instead of its expanded value.
130
151
131 Parameters
152 Parameters
132 ----------
153 ----------
133 path : str
154 path : str
134 String to be expanded. If no ~ is present, the output is the same as the
155 String to be expanded. If no ~ is present, the output is the same as the
135 input.
156 input.
136
157
137 Returns
158 Returns
138 -------
159 -------
139 newpath : str
160 newpath : str
140 Result of ~ expansion in the input path.
161 Result of ~ expansion in the input path.
141 tilde_expand : bool
162 tilde_expand : bool
142 Whether any expansion was performed or not.
163 Whether any expansion was performed or not.
143 tilde_val : str
164 tilde_val : str
144 The value that ~ was replaced with.
165 The value that ~ was replaced with.
145 """
166 """
146 # Default values
167 # Default values
147 tilde_expand = False
168 tilde_expand = False
148 tilde_val = ''
169 tilde_val = ''
149 newpath = path
170 newpath = path
150
171
151 if path.startswith('~'):
172 if path.startswith('~'):
152 tilde_expand = True
173 tilde_expand = True
153 rest = path[1:]
174 rest = path[1:]
154 newpath = os.path.expanduser(path)
175 newpath = os.path.expanduser(path)
155 tilde_val = newpath.replace(rest, '')
176 tilde_val = newpath.replace(rest, '')
156
177
157 return newpath, tilde_expand, tilde_val
178 return newpath, tilde_expand, tilde_val
158
179
159
180
160 def compress_user(path, tilde_expand, tilde_val):
181 def compress_user(path, tilde_expand, tilde_val):
161 """Does the opposite of expand_user, with its outputs.
182 """Does the opposite of expand_user, with its outputs.
162 """
183 """
163 if tilde_expand:
184 if tilde_expand:
164 return path.replace(tilde_val, '~')
185 return path.replace(tilde_val, '~')
165 else:
186 else:
166 return path
187 return path
167
188
168
189
169 def single_dir_expand(matches):
190 def single_dir_expand(matches):
170 "Recursively expand match lists containing a single dir."
191 "Recursively expand match lists containing a single dir."
171
192
172 if len(matches) == 1 and os.path.isdir(matches[0]):
193 if len(matches) == 1 and os.path.isdir(matches[0]):
173 # Takes care of links to directories also. Use '/'
194 # Takes care of links to directories also. Use '/'
174 # explicitly, even under Windows, so that name completions
195 # explicitly, even under Windows, so that name completions
175 # don't end up escaped.
196 # don't end up escaped.
176 d = matches[0]
197 d = matches[0]
177 if d[-1] in ['/','\\']:
198 if d[-1] in ['/','\\']:
178 d = d[:-1]
199 d = d[:-1]
179
200
180 subdirs = os.listdir(d)
201 subdirs = os.listdir(d)
181 if subdirs:
202 if subdirs:
182 matches = [ (d + '/' + p) for p in subdirs]
203 matches = [ (d + '/' + p) for p in subdirs]
183 return single_dir_expand(matches)
204 return single_dir_expand(matches)
184 else:
205 else:
185 return matches
206 return matches
186 else:
207 else:
187 return matches
208 return matches
188
209
189
210
190 class Bunch(object): pass
211 class Bunch(object): pass
191
212
192
213
193 class CompletionSplitter(object):
214 class CompletionSplitter(object):
194 """An object to split an input line in a manner similar to readline.
215 """An object to split an input line in a manner similar to readline.
195
216
196 By having our own implementation, we can expose readline-like completion in
217 By having our own implementation, we can expose readline-like completion in
197 a uniform manner to all frontends. This object only needs to be given the
218 a uniform manner to all frontends. This object only needs to be given the
198 line of text to be split and the cursor position on said line, and it
219 line of text to be split and the cursor position on said line, and it
199 returns the 'word' to be completed on at the cursor after splitting the
220 returns the 'word' to be completed on at the cursor after splitting the
200 entire line.
221 entire line.
201
222
202 What characters are used as splitting delimiters can be controlled by
223 What characters are used as splitting delimiters can be controlled by
203 setting the `delims` attribute (this is a property that internally
224 setting the `delims` attribute (this is a property that internally
204 automatically builds the necessary """
225 automatically builds the necessary """
205
226
206 # Private interface
227 # Private interface
207
228
208 # A string of delimiter characters. The default value makes sense for
229 # A string of delimiter characters. The default value makes sense for
209 # IPython's most typical usage patterns.
230 # IPython's most typical usage patterns.
210 _delims = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
231 _delims = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
211
232
212 # The expression (a normal string) to be compiled into a regular expression
233 # The expression (a normal string) to be compiled into a regular expression
213 # for actual splitting. We store it as an attribute mostly for ease of
234 # for actual splitting. We store it as an attribute mostly for ease of
214 # debugging, since this type of code can be so tricky to debug.
235 # debugging, since this type of code can be so tricky to debug.
215 _delim_expr = None
236 _delim_expr = None
216
237
217 # The regular expression that does the actual splitting
238 # The regular expression that does the actual splitting
218 _delim_re = None
239 _delim_re = None
219
240
220 def __init__(self, delims=None):
241 def __init__(self, delims=None):
221 delims = CompletionSplitter._delims if delims is None else delims
242 delims = CompletionSplitter._delims if delims is None else delims
222 self.set_delims(delims)
243 self.set_delims(delims)
223
244
224 def set_delims(self, delims):
245 def set_delims(self, delims):
225 """Set the delimiters for line splitting."""
246 """Set the delimiters for line splitting."""
226 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
247 expr = '[' + ''.join('\\'+ c for c in delims) + ']'
227 self._delim_re = re.compile(expr)
248 self._delim_re = re.compile(expr)
228 self._delims = delims
249 self._delims = delims
229 self._delim_expr = expr
250 self._delim_expr = expr
230
251
231 def get_delims(self):
252 def get_delims(self):
232 """Return the string of delimiter characters."""
253 """Return the string of delimiter characters."""
233 return self._delims
254 return self._delims
234
255
235 def split_line(self, line, cursor_pos=None):
256 def split_line(self, line, cursor_pos=None):
236 """Split a line of text with a cursor at the given position.
257 """Split a line of text with a cursor at the given position.
237 """
258 """
238 l = line if cursor_pos is None else line[:cursor_pos]
259 l = line if cursor_pos is None else line[:cursor_pos]
239 return self._delim_re.split(l)[-1]
260 return self._delim_re.split(l)[-1]
240
261
241
262
242 class Completer(object):
263 class Completer(object):
243 def __init__(self, namespace=None, global_namespace=None):
264 def __init__(self, namespace=None, global_namespace=None):
244 """Create a new completer for the command line.
265 """Create a new completer for the command line.
245
266
246 Completer([namespace,global_namespace]) -> completer instance.
267 Completer([namespace,global_namespace]) -> completer instance.
247
268
248 If unspecified, the default namespace where completions are performed
269 If unspecified, the default namespace where completions are performed
249 is __main__ (technically, __main__.__dict__). Namespaces should be
270 is __main__ (technically, __main__.__dict__). Namespaces should be
250 given as dictionaries.
271 given as dictionaries.
251
272
252 An optional second namespace can be given. This allows the completer
273 An optional second namespace can be given. This allows the completer
253 to handle cases where both the local and global scopes need to be
274 to handle cases where both the local and global scopes need to be
254 distinguished.
275 distinguished.
255
276
256 Completer instances should be used as the completion mechanism of
277 Completer instances should be used as the completion mechanism of
257 readline via the set_completer() call:
278 readline via the set_completer() call:
258
279
259 readline.set_completer(Completer(my_namespace).complete)
280 readline.set_completer(Completer(my_namespace).complete)
260 """
281 """
261
282
262 # Don't bind to namespace quite yet, but flag whether the user wants a
283 # Don't bind to namespace quite yet, but flag whether the user wants a
263 # specific namespace or to use __main__.__dict__. This will allow us
284 # specific namespace or to use __main__.__dict__. This will allow us
264 # to bind to __main__.__dict__ at completion time, not now.
285 # to bind to __main__.__dict__ at completion time, not now.
265 if namespace is None:
286 if namespace is None:
266 self.use_main_ns = 1
287 self.use_main_ns = 1
267 else:
288 else:
268 self.use_main_ns = 0
289 self.use_main_ns = 0
269 self.namespace = namespace
290 self.namespace = namespace
270
291
271 # The global namespace, if given, can be bound directly
292 # The global namespace, if given, can be bound directly
272 if global_namespace is None:
293 if global_namespace is None:
273 self.global_namespace = {}
294 self.global_namespace = {}
274 else:
295 else:
275 self.global_namespace = global_namespace
296 self.global_namespace = global_namespace
276
297
277 def complete(self, text, state):
298 def complete(self, text, state):
278 """Return the next possible completion for 'text'.
299 """Return the next possible completion for 'text'.
279
300
280 This is called successively with state == 0, 1, 2, ... until it
301 This is called successively with state == 0, 1, 2, ... until it
281 returns None. The completion should begin with 'text'.
302 returns None. The completion should begin with 'text'.
282
303
283 """
304 """
284 if self.use_main_ns:
305 if self.use_main_ns:
285 self.namespace = __main__.__dict__
306 self.namespace = __main__.__dict__
286
307
287 if state == 0:
308 if state == 0:
288 if "." in text:
309 if "." in text:
289 self.matches = self.attr_matches(text)
310 self.matches = self.attr_matches(text)
290 else:
311 else:
291 self.matches = self.global_matches(text)
312 self.matches = self.global_matches(text)
292 try:
313 try:
293 return self.matches[state]
314 return self.matches[state]
294 except IndexError:
315 except IndexError:
295 return None
316 return None
296
317
297 def global_matches(self, text):
318 def global_matches(self, text):
298 """Compute matches when text is a simple name.
319 """Compute matches when text is a simple name.
299
320
300 Return a list of all keywords, built-in functions and names currently
321 Return a list of all keywords, built-in functions and names currently
301 defined in self.namespace or self.global_namespace that match.
322 defined in self.namespace or self.global_namespace that match.
302
323
303 """
324 """
304 #print 'Completer->global_matches, txt=%r' % text # dbg
325 #print 'Completer->global_matches, txt=%r' % text # dbg
305 matches = []
326 matches = []
306 match_append = matches.append
327 match_append = matches.append
307 n = len(text)
328 n = len(text)
308 for lst in [keyword.kwlist,
329 for lst in [keyword.kwlist,
309 __builtin__.__dict__.keys(),
330 __builtin__.__dict__.keys(),
310 self.namespace.keys(),
331 self.namespace.keys(),
311 self.global_namespace.keys()]:
332 self.global_namespace.keys()]:
312 for word in lst:
333 for word in lst:
313 if word[:n] == text and word != "__builtins__":
334 if word[:n] == text and word != "__builtins__":
314 match_append(word)
335 match_append(word)
315 return matches
336 return matches
316
337
317 def attr_matches(self, text):
338 def attr_matches(self, text):
318 """Compute matches when text contains a dot.
339 """Compute matches when text contains a dot.
319
340
320 Assuming the text is of the form NAME.NAME....[NAME], and is
341 Assuming the text is of the form NAME.NAME....[NAME], and is
321 evaluatable in self.namespace or self.global_namespace, it will be
342 evaluatable in self.namespace or self.global_namespace, it will be
322 evaluated and its attributes (as revealed by dir()) are used as
343 evaluated and its attributes (as revealed by dir()) are used as
323 possible completions. (For class instances, class members are are
344 possible completions. (For class instances, class members are are
324 also considered.)
345 also considered.)
325
346
326 WARNING: this can still invoke arbitrary C code, if an object
347 WARNING: this can still invoke arbitrary C code, if an object
327 with a __getattr__ hook is evaluated.
348 with a __getattr__ hook is evaluated.
328
349
329 """
350 """
330
351
331 #print 'Completer->attr_matches, txt=%r' % text # dbg
352 #print 'Completer->attr_matches, txt=%r' % text # dbg
332 # Another option, seems to work great. Catches things like ''.<tab>
353 # Another option, seems to work great. Catches things like ''.<tab>
333 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
354 m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
334
355
335 if not m:
356 if not m:
336 return []
357 return []
337
358
338 expr, attr = m.group(1, 3)
359 expr, attr = m.group(1, 3)
339 try:
360 try:
340 obj = eval(expr, self.namespace)
361 obj = eval(expr, self.namespace)
341 except:
362 except:
342 try:
363 try:
343 obj = eval(expr, self.global_namespace)
364 obj = eval(expr, self.global_namespace)
344 except:
365 except:
345 return []
366 return []
346
367
347 words = dir2(obj)
368 words = dir2(obj)
348
369
349 try:
370 try:
350 words = generics.complete_object(obj, words)
371 words = generics.complete_object(obj, words)
351 except TryNext:
372 except TryNext:
352 pass
373 pass
353 # Build match list to return
374 # Build match list to return
354 n = len(attr)
375 n = len(attr)
355 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
376 res = ["%s.%s" % (expr, w) for w in words if w[:n] == attr ]
356 return res
377 return res
357
378
358
379
359 class IPCompleter(Completer):
380 class IPCompleter(Completer):
360 """Extension of the completer class with IPython-specific features"""
381 """Extension of the completer class with IPython-specific features"""
361
382
362 def __init__(self, shell, namespace=None, global_namespace=None,
383 def __init__(self, shell, namespace=None, global_namespace=None,
363 omit__names=0, alias_table=None, use_readline=True):
384 omit__names=0, alias_table=None, use_readline=True):
364 """IPCompleter() -> completer
385 """IPCompleter() -> completer
365
386
366 Return a completer object suitable for use by the readline library
387 Return a completer object suitable for use by the readline library
367 via readline.set_completer().
388 via readline.set_completer().
368
389
369 Inputs:
390 Inputs:
370
391
371 - shell: a pointer to the ipython shell itself. This is needed
392 - shell: a pointer to the ipython shell itself. This is needed
372 because this completer knows about magic functions, and those can
393 because this completer knows about magic functions, and those can
373 only be accessed via the ipython instance.
394 only be accessed via the ipython instance.
374
395
375 - namespace: an optional dict where completions are performed.
396 - namespace: an optional dict where completions are performed.
376
397
377 - global_namespace: secondary optional dict for completions, to
398 - global_namespace: secondary optional dict for completions, to
378 handle cases (such as IPython embedded inside functions) where
399 handle cases (such as IPython embedded inside functions) where
379 both Python scopes are visible.
400 both Python scopes are visible.
380
401
381 - The optional omit__names parameter sets the completer to omit the
402 - The optional omit__names parameter sets the completer to omit the
382 'magic' names (__magicname__) for python objects unless the text
403 'magic' names (__magicname__) for python objects unless the text
383 to be completed explicitly starts with one or more underscores.
404 to be completed explicitly starts with one or more underscores.
384
405
385 - If alias_table is supplied, it should be a dictionary of aliases
406 - If alias_table is supplied, it should be a dictionary of aliases
386 to complete.
407 to complete.
387
408
388 use_readline : bool, optional
409 use_readline : bool, optional
389 If true, use the readline library. This completer can still function
410 If true, use the readline library. This completer can still function
390 without readline, though in that case callers must provide some extra
411 without readline, though in that case callers must provide some extra
391 information on each call about the current line."""
412 information on each call about the current line."""
392
413
393 Completer.__init__(self, namespace, global_namespace)
414 Completer.__init__(self, namespace, global_namespace)
394
415
395 self.magic_escape = ESC_MAGIC
416 self.magic_escape = ESC_MAGIC
396 self.splitter = CompletionSplitter()
417 self.splitter = CompletionSplitter()
397
418
398 # Readline configuration, only used by the rlcompleter method.
419 # Readline configuration, only used by the rlcompleter method.
399 if use_readline:
420 if use_readline:
400 # We store the right version of readline so that later code
421 # We store the right version of readline so that later code
401 import IPython.utils.rlineimpl as readline
422 import IPython.utils.rlineimpl as readline
402 self.readline = readline
423 self.readline = readline
403 else:
424 else:
404 self.readline = None
425 self.readline = None
405
426
406 # List where completion matches will be stored
427 # List where completion matches will be stored
407 self.matches = []
428 self.matches = []
408 self.omit__names = omit__names
429 self.omit__names = omit__names
409 self.merge_completions = shell.readline_merge_completions
430 self.merge_completions = shell.readline_merge_completions
410 self.shell = shell.shell
431 self.shell = shell.shell
411 if alias_table is None:
432 if alias_table is None:
412 alias_table = {}
433 alias_table = {}
413 self.alias_table = alias_table
434 self.alias_table = alias_table
414 # Regexp to split filenames with spaces in them
435 # Regexp to split filenames with spaces in them
415 self.space_name_re = re.compile(r'([^\\] )')
436 self.space_name_re = re.compile(r'([^\\] )')
416 # Hold a local ref. to glob.glob for speed
437 # Hold a local ref. to glob.glob for speed
417 self.glob = glob.glob
438 self.glob = glob.glob
418
439
419 # Determine if we are running on 'dumb' terminals, like (X)Emacs
440 # Determine if we are running on 'dumb' terminals, like (X)Emacs
420 # buffers, to avoid completion problems.
441 # buffers, to avoid completion problems.
421 term = os.environ.get('TERM','xterm')
442 term = os.environ.get('TERM','xterm')
422 self.dumb_terminal = term in ['dumb','emacs']
443 self.dumb_terminal = term in ['dumb','emacs']
423
444
424 # Special handling of backslashes needed in win32 platforms
445 # Special handling of backslashes needed in win32 platforms
425 if sys.platform == "win32":
446 if sys.platform == "win32":
426 self.clean_glob = self._clean_glob_win32
447 self.clean_glob = self._clean_glob_win32
427 else:
448 else:
428 self.clean_glob = self._clean_glob
449 self.clean_glob = self._clean_glob
429
450
430 # All active matcher routines for completion
451 # All active matcher routines for completion
431 self.matchers = [self.python_matches,
452 self.matchers = [self.python_matches,
432 self.file_matches,
453 self.file_matches,
433 self.magic_matches,
454 self.magic_matches,
434 self.alias_matches,
455 self.alias_matches,
435 self.python_func_kw_matches,
456 self.python_func_kw_matches,
436 ]
457 ]
437
458
438 # Code contributed by Alex Schmolck, for ipython/emacs integration
459 # Code contributed by Alex Schmolck, for ipython/emacs integration
439 def all_completions(self, text):
460 def all_completions(self, text):
440 """Return all possible completions for the benefit of emacs."""
461 """Return all possible completions for the benefit of emacs."""
441
462
442 completions = []
463 completions = []
443 comp_append = completions.append
464 comp_append = completions.append
444 try:
465 try:
445 for i in xrange(sys.maxint):
466 for i in xrange(sys.maxint):
446 res = self.complete(text, i, text)
467 res = self.complete(text, i, text)
447 if not res:
468 if not res:
448 break
469 break
449 comp_append(res)
470 comp_append(res)
450 #XXX workaround for ``notDefined.<tab>``
471 #XXX workaround for ``notDefined.<tab>``
451 except NameError:
472 except NameError:
452 pass
473 pass
453 return completions
474 return completions
454 # /end Alex Schmolck code.
475 # /end Alex Schmolck code.
455
476
456 def _clean_glob(self,text):
477 def _clean_glob(self,text):
457 return self.glob("%s*" % text)
478 return self.glob("%s*" % text)
458
479
459 def _clean_glob_win32(self,text):
480 def _clean_glob_win32(self,text):
460 return [f.replace("\\","/")
481 return [f.replace("\\","/")
461 for f in self.glob("%s*" % text)]
482 for f in self.glob("%s*" % text)]
462
483
463 def file_matches(self, text):
484 def file_matches(self, text):
464 """Match filenames, expanding ~USER type strings.
485 """Match filenames, expanding ~USER type strings.
465
486
466 Most of the seemingly convoluted logic in this completer is an
487 Most of the seemingly convoluted logic in this completer is an
467 attempt to handle filenames with spaces in them. And yet it's not
488 attempt to handle filenames with spaces in them. And yet it's not
468 quite perfect, because Python's readline doesn't expose all of the
489 quite perfect, because Python's readline doesn't expose all of the
469 GNU readline details needed for this to be done correctly.
490 GNU readline details needed for this to be done correctly.
470
491
471 For a filename with a space in it, the printed completions will be
492 For a filename with a space in it, the printed completions will be
472 only the parts after what's already been typed (instead of the
493 only the parts after what's already been typed (instead of the
473 full completions, as is normally done). I don't think with the
494 full completions, as is normally done). I don't think with the
474 current (as of Python 2.3) Python readline it's possible to do
495 current (as of Python 2.3) Python readline it's possible to do
475 better."""
496 better."""
476
497
477 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
498 #io.rprint('Completer->file_matches: <%r>' % text) # dbg
478
499
479 # chars that require escaping with backslash - i.e. chars
500 # chars that require escaping with backslash - i.e. chars
480 # that readline treats incorrectly as delimiters, but we
501 # that readline treats incorrectly as delimiters, but we
481 # don't want to treat as delimiters in filename matching
502 # don't want to treat as delimiters in filename matching
482 # when escaped with backslash
503 # when escaped with backslash
483 if text.startswith('!'):
504 if text.startswith('!'):
484 text = text[1:]
505 text = text[1:]
485 text_prefix = '!'
506 text_prefix = '!'
486 else:
507 else:
487 text_prefix = ''
508 text_prefix = ''
488
509
489 text_until_cursor = self.text_until_cursor
510 text_until_cursor = self.text_until_cursor
490 open_quotes = 0 # track strings with open quotes
511 # track strings with open quotes
512 open_quotes = has_open_quotes(text_until_cursor)
513
514 if '(' in text_until_cursor or '[' in text_until_cursor:
515 lsplit = text
516 else:
491 try:
517 try:
492 # arg_split ~ shlex.split, but with unicode bugs fixed by us
518 # arg_split ~ shlex.split, but with unicode bugs fixed by us
493 lsplit = arg_split(text_until_cursor)[-1]
519 lsplit = arg_split(text_until_cursor)[-1]
494 except ValueError:
520 except ValueError:
495 # typically an unmatched ", or backslash without escaped char.
521 # typically an unmatched ", or backslash without escaped char.
496 if text_until_cursor.count('"')==1:
522 if open_quotes:
497 open_quotes = 1
523 lsplit = text_until_cursor.split(open_quotes)[-1]
498 lsplit = text_until_cursor.split('"')[-1]
499 elif text_until_cursor.count("'")==1:
500 open_quotes = 1
501 lsplit = text_until_cursor.split("'")[-1]
502 else:
524 else:
503 return []
525 return []
504 except IndexError:
526 except IndexError:
505 # tab pressed on empty line
527 # tab pressed on empty line
506 lsplit = ""
528 lsplit = ""
507
529
508 if not open_quotes and lsplit != protect_filename(lsplit):
530 if not open_quotes and lsplit != protect_filename(lsplit):
509 # if protectables are found, do matching on the whole escaped
531 # if protectables are found, do matching on the whole escaped name
510 # name
532 has_protectables = True
511 has_protectables = 1
512 text0,text = text,lsplit
533 text0,text = text,lsplit
513 else:
534 else:
514 has_protectables = 0
535 has_protectables = False
515 text = os.path.expanduser(text)
536 text = os.path.expanduser(text)
516
537
517 if text == "":
538 if text == "":
518 return [text_prefix + protect_filename(f) for f in self.glob("*")]
539 return [text_prefix + protect_filename(f) for f in self.glob("*")]
519
540
541 # Compute the matches from the filesystem
520 m0 = self.clean_glob(text.replace('\\',''))
542 m0 = self.clean_glob(text.replace('\\',''))
543
521 if has_protectables:
544 if has_protectables:
522 # If we had protectables, we need to revert our changes to the
545 # If we had protectables, we need to revert our changes to the
523 # beginning of filename so that we don't double-write the part
546 # beginning of filename so that we don't double-write the part
524 # of the filename we have so far
547 # of the filename we have so far
525 len_lsplit = len(lsplit)
548 len_lsplit = len(lsplit)
526 matches = [text_prefix + text0 +
549 matches = [text_prefix + text0 +
527 protect_filename(f[len_lsplit:]) for f in m0]
550 protect_filename(f[len_lsplit:]) for f in m0]
528 else:
551 else:
529 if open_quotes:
552 if open_quotes:
530 # if we have a string with an open quote, we don't need to
553 # if we have a string with an open quote, we don't need to
531 # protect the names at all (and we _shouldn't_, as it
554 # protect the names at all (and we _shouldn't_, as it
532 # would cause bugs when the filesystem call is made).
555 # would cause bugs when the filesystem call is made).
533 matches = m0
556 matches = m0
534 else:
557 else:
535 matches = [text_prefix +
558 matches = [text_prefix +
536 protect_filename(f) for f in m0]
559 protect_filename(f) for f in m0]
537
560
538 #io.rprint('mm', matches) # dbg
561 #io.rprint('mm', matches) # dbg
539 return mark_dirs(matches)
562 return mark_dirs(matches)
540
563
541 def magic_matches(self, text):
564 def magic_matches(self, text):
542 """Match magics"""
565 """Match magics"""
543 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
566 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
544 # Get all shell magics now rather than statically, so magics loaded at
567 # Get all shell magics now rather than statically, so magics loaded at
545 # runtime show up too
568 # runtime show up too
546 magics = self.shell.lsmagic()
569 magics = self.shell.lsmagic()
547 pre = self.magic_escape
570 pre = self.magic_escape
548 baretext = text.lstrip(pre)
571 baretext = text.lstrip(pre)
549 return [ pre+m for m in magics if m.startswith(baretext)]
572 return [ pre+m for m in magics if m.startswith(baretext)]
550
573
551 def alias_matches(self, text):
574 def alias_matches(self, text):
552 """Match internal system aliases"""
575 """Match internal system aliases"""
553 #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg
576 #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg
554
577
555 # if we are not in the first 'item', alias matching
578 # if we are not in the first 'item', alias matching
556 # doesn't make sense - unless we are starting with 'sudo' command.
579 # doesn't make sense - unless we are starting with 'sudo' command.
557 main_text = self.text_until_cursor.lstrip()
580 main_text = self.text_until_cursor.lstrip()
558 if ' ' in main_text and not main_text.startswith('sudo'):
581 if ' ' in main_text and not main_text.startswith('sudo'):
559 return []
582 return []
560 text = os.path.expanduser(text)
583 text = os.path.expanduser(text)
561 aliases = self.alias_table.keys()
584 aliases = self.alias_table.keys()
562 if text == '':
585 if text == '':
563 return aliases
586 return aliases
564 else:
587 else:
565 return [a for a in aliases if a.startswith(text)]
588 return [a for a in aliases if a.startswith(text)]
566
589
567 def python_matches(self,text):
590 def python_matches(self,text):
568 """Match attributes or global python names"""
591 """Match attributes or global python names"""
569
592
570 #print 'Completer->python_matches, txt=%r' % text # dbg
593 #print 'Completer->python_matches, txt=%r' % text # dbg
571 if "." in text:
594 if "." in text:
572 try:
595 try:
573 matches = self.attr_matches(text)
596 matches = self.attr_matches(text)
574 if text.endswith('.') and self.omit__names:
597 if text.endswith('.') and self.omit__names:
575 if self.omit__names == 1:
598 if self.omit__names == 1:
576 # true if txt is _not_ a __ name, false otherwise:
599 # true if txt is _not_ a __ name, false otherwise:
577 no__name = (lambda txt:
600 no__name = (lambda txt:
578 re.match(r'.*\.__.*?__',txt) is None)
601 re.match(r'.*\.__.*?__',txt) is None)
579 else:
602 else:
580 # true if txt is _not_ a _ name, false otherwise:
603 # true if txt is _not_ a _ name, false otherwise:
581 no__name = (lambda txt:
604 no__name = (lambda txt:
582 re.match(r'.*\._.*?',txt) is None)
605 re.match(r'.*\._.*?',txt) is None)
583 matches = filter(no__name, matches)
606 matches = filter(no__name, matches)
584 except NameError:
607 except NameError:
585 # catches <undefined attributes>.<tab>
608 # catches <undefined attributes>.<tab>
586 matches = []
609 matches = []
587 else:
610 else:
588 matches = self.global_matches(text)
611 matches = self.global_matches(text)
589
612
590 return matches
613 return matches
591
614
592 def _default_arguments(self, obj):
615 def _default_arguments(self, obj):
593 """Return the list of default arguments of obj if it is callable,
616 """Return the list of default arguments of obj if it is callable,
594 or empty list otherwise."""
617 or empty list otherwise."""
595
618
596 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
619 if not (inspect.isfunction(obj) or inspect.ismethod(obj)):
597 # for classes, check for __init__,__new__
620 # for classes, check for __init__,__new__
598 if inspect.isclass(obj):
621 if inspect.isclass(obj):
599 obj = (getattr(obj,'__init__',None) or
622 obj = (getattr(obj,'__init__',None) or
600 getattr(obj,'__new__',None))
623 getattr(obj,'__new__',None))
601 # for all others, check if they are __call__able
624 # for all others, check if they are __call__able
602 elif hasattr(obj, '__call__'):
625 elif hasattr(obj, '__call__'):
603 obj = obj.__call__
626 obj = obj.__call__
604 # XXX: is there a way to handle the builtins ?
627 # XXX: is there a way to handle the builtins ?
605 try:
628 try:
606 args,_,_1,defaults = inspect.getargspec(obj)
629 args,_,_1,defaults = inspect.getargspec(obj)
607 if defaults:
630 if defaults:
608 return args[-len(defaults):]
631 return args[-len(defaults):]
609 except TypeError: pass
632 except TypeError: pass
610 return []
633 return []
611
634
612 def python_func_kw_matches(self,text):
635 def python_func_kw_matches(self,text):
613 """Match named parameters (kwargs) of the last open function"""
636 """Match named parameters (kwargs) of the last open function"""
614
637
615 if "." in text: # a parameter cannot be dotted
638 if "." in text: # a parameter cannot be dotted
616 return []
639 return []
617 try: regexp = self.__funcParamsRegex
640 try: regexp = self.__funcParamsRegex
618 except AttributeError:
641 except AttributeError:
619 regexp = self.__funcParamsRegex = re.compile(r'''
642 regexp = self.__funcParamsRegex = re.compile(r'''
620 '.*?' | # single quoted strings or
643 '.*?' | # single quoted strings or
621 ".*?" | # double quoted strings or
644 ".*?" | # double quoted strings or
622 \w+ | # identifier
645 \w+ | # identifier
623 \S # other characters
646 \S # other characters
624 ''', re.VERBOSE | re.DOTALL)
647 ''', re.VERBOSE | re.DOTALL)
625 # 1. find the nearest identifier that comes before an unclosed
648 # 1. find the nearest identifier that comes before an unclosed
626 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
649 # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo"
627 tokens = regexp.findall(self.line_buffer)
650 tokens = regexp.findall(self.line_buffer)
628 tokens.reverse()
651 tokens.reverse()
629 iterTokens = iter(tokens); openPar = 0
652 iterTokens = iter(tokens); openPar = 0
630 for token in iterTokens:
653 for token in iterTokens:
631 if token == ')':
654 if token == ')':
632 openPar -= 1
655 openPar -= 1
633 elif token == '(':
656 elif token == '(':
634 openPar += 1
657 openPar += 1
635 if openPar > 0:
658 if openPar > 0:
636 # found the last unclosed parenthesis
659 # found the last unclosed parenthesis
637 break
660 break
638 else:
661 else:
639 return []
662 return []
640 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
663 # 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
641 ids = []
664 ids = []
642 isId = re.compile(r'\w+$').match
665 isId = re.compile(r'\w+$').match
643 while True:
666 while True:
644 try:
667 try:
645 ids.append(iterTokens.next())
668 ids.append(iterTokens.next())
646 if not isId(ids[-1]):
669 if not isId(ids[-1]):
647 ids.pop(); break
670 ids.pop(); break
648 if not iterTokens.next() == '.':
671 if not iterTokens.next() == '.':
649 break
672 break
650 except StopIteration:
673 except StopIteration:
651 break
674 break
652 # lookup the candidate callable matches either using global_matches
675 # lookup the candidate callable matches either using global_matches
653 # or attr_matches for dotted names
676 # or attr_matches for dotted names
654 if len(ids) == 1:
677 if len(ids) == 1:
655 callableMatches = self.global_matches(ids[0])
678 callableMatches = self.global_matches(ids[0])
656 else:
679 else:
657 callableMatches = self.attr_matches('.'.join(ids[::-1]))
680 callableMatches = self.attr_matches('.'.join(ids[::-1]))
658 argMatches = []
681 argMatches = []
659 for callableMatch in callableMatches:
682 for callableMatch in callableMatches:
660 try:
683 try:
661 namedArgs = self._default_arguments(eval(callableMatch,
684 namedArgs = self._default_arguments(eval(callableMatch,
662 self.namespace))
685 self.namespace))
663 except:
686 except:
664 continue
687 continue
665 for namedArg in namedArgs:
688 for namedArg in namedArgs:
666 if namedArg.startswith(text):
689 if namedArg.startswith(text):
667 argMatches.append("%s=" %namedArg)
690 argMatches.append("%s=" %namedArg)
668 return argMatches
691 return argMatches
669
692
670 def dispatch_custom_completer(self, text):
693 def dispatch_custom_completer(self, text):
671 #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg
694 #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg
672 line = self.line_buffer
695 line = self.line_buffer
673 if not line.strip():
696 if not line.strip():
674 return None
697 return None
675
698
676 # Create a little structure to pass all the relevant information about
699 # Create a little structure to pass all the relevant information about
677 # the current completion to any custom completer.
700 # the current completion to any custom completer.
678 event = Bunch()
701 event = Bunch()
679 event.line = line
702 event.line = line
680 event.symbol = text
703 event.symbol = text
681 cmd = line.split(None,1)[0]
704 cmd = line.split(None,1)[0]
682 event.command = cmd
705 event.command = cmd
683 event.text_until_cursor = self.text_until_cursor
706 event.text_until_cursor = self.text_until_cursor
684
707
685 #print "\ncustom:{%s]\n" % event # dbg
708 #print "\ncustom:{%s]\n" % event # dbg
686
709
687 # for foo etc, try also to find completer for %foo
710 # for foo etc, try also to find completer for %foo
688 if not cmd.startswith(self.magic_escape):
711 if not cmd.startswith(self.magic_escape):
689 try_magic = self.custom_completers.s_matches(
712 try_magic = self.custom_completers.s_matches(
690 self.magic_escape + cmd)
713 self.magic_escape + cmd)
691 else:
714 else:
692 try_magic = []
715 try_magic = []
693
716
694 for c in itertools.chain(self.custom_completers.s_matches(cmd),
717 for c in itertools.chain(self.custom_completers.s_matches(cmd),
695 try_magic,
718 try_magic,
696 self.custom_completers.flat_matches(self.text_until_cursor)):
719 self.custom_completers.flat_matches(self.text_until_cursor)):
697 #print "try",c # dbg
720 #print "try",c # dbg
698 try:
721 try:
699 res = c(event)
722 res = c(event)
700 if res:
723 if res:
701 # first, try case sensitive match
724 # first, try case sensitive match
702 withcase = [r for r in res if r.startswith(text)]
725 withcase = [r for r in res if r.startswith(text)]
703 if withcase:
726 if withcase:
704 return withcase
727 return withcase
705 # if none, then case insensitive ones are ok too
728 # if none, then case insensitive ones are ok too
706 text_low = text.lower()
729 text_low = text.lower()
707 return [r for r in res if r.lower().startswith(text_low)]
730 return [r for r in res if r.lower().startswith(text_low)]
708 except TryNext:
731 except TryNext:
709 pass
732 pass
710
733
711 return None
734 return None
712
735
713 def complete(self, text=None, line_buffer=None, cursor_pos=None):
736 def complete(self, text=None, line_buffer=None, cursor_pos=None):
714 """Return the state-th possible completion for 'text'.
737 """Find completions for the given text and line context.
715
738
716 This is called successively with state == 0, 1, 2, ... until it
739 This is called successively with state == 0, 1, 2, ... until it
717 returns None. The completion should begin with 'text'.
740 returns None. The completion should begin with 'text'.
718
741
719 Note that both the text and the line_buffer are optional, but at least
742 Note that both the text and the line_buffer are optional, but at least
720 one of them must be given.
743 one of them must be given.
721
744
722 Parameters
745 Parameters
723 ----------
746 ----------
724 text : string, optional
747 text : string, optional
725 Text to perform the completion on. If not given, the line buffer
748 Text to perform the completion on. If not given, the line buffer
726 is split using the instance's CompletionSplitter object.
749 is split using the instance's CompletionSplitter object.
727
750
728 line_buffer : string, optional
751 line_buffer : string, optional
729 If not given, the completer attempts to obtain the current line
752 If not given, the completer attempts to obtain the current line
730 buffer via readline. This keyword allows clients which are
753 buffer via readline. This keyword allows clients which are
731 requesting for text completions in non-readline contexts to inform
754 requesting for text completions in non-readline contexts to inform
732 the completer of the entire text.
755 the completer of the entire text.
733
756
734 cursor_pos : int, optional
757 cursor_pos : int, optional
735 Index of the cursor in the full line buffer. Should be provided by
758 Index of the cursor in the full line buffer. Should be provided by
736 remote frontends where kernel has no access to frontend state.
759 remote frontends where kernel has no access to frontend state.
760
761 Returns
762 -------
763 text : str
764 Text that was actually used in the completion.
765
766 matches : list
767 A list of completion matches.
737 """
768 """
738 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
769 #io.rprint('\nCOMP1 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
739
770
740 # if the cursor position isn't given, the only sane assumption we can
771 # if the cursor position isn't given, the only sane assumption we can
741 # make is that it's at the end of the line (the common case)
772 # make is that it's at the end of the line (the common case)
742 if cursor_pos is None:
773 if cursor_pos is None:
743 cursor_pos = len(line_buffer) if text is None else len(text)
774 cursor_pos = len(line_buffer) if text is None else len(text)
744
775
745 # if text is either None or an empty string, rely on the line buffer
776 # if text is either None or an empty string, rely on the line buffer
746 if not text:
777 if not text:
747 text = self.splitter.split_line(line_buffer, cursor_pos)
778 text = self.splitter.split_line(line_buffer, cursor_pos)
748
779
749 # If no line buffer is given, assume the input text is all there was
780 # If no line buffer is given, assume the input text is all there was
750 if line_buffer is None:
781 if line_buffer is None:
751 line_buffer = text
782 line_buffer = text
752
783
753 self.line_buffer = line_buffer
784 self.line_buffer = line_buffer
754 self.text_until_cursor = self.line_buffer[:cursor_pos]
785 self.text_until_cursor = self.line_buffer[:cursor_pos]
755 #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
786 #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg
756
787
757 # Start with a clean slate of completions
788 # Start with a clean slate of completions
758 self.matches[:] = []
789 self.matches[:] = []
759 custom_res = self.dispatch_custom_completer(text)
790 custom_res = self.dispatch_custom_completer(text)
760 if custom_res is not None:
791 if custom_res is not None:
761 # did custom completers produce something?
792 # did custom completers produce something?
762 self.matches = custom_res
793 self.matches = custom_res
763 else:
794 else:
764 # Extend the list of completions with the results of each
795 # Extend the list of completions with the results of each
765 # matcher, so we return results to the user from all
796 # matcher, so we return results to the user from all
766 # namespaces.
797 # namespaces.
767 if self.merge_completions:
798 if self.merge_completions:
768 self.matches = []
799 self.matches = []
769 for matcher in self.matchers:
800 for matcher in self.matchers:
770 try:
801 try:
771 self.matches.extend(matcher(text))
802 self.matches.extend(matcher(text))
772 except:
803 except:
773 # Show the ugly traceback if the matcher causes an
804 # Show the ugly traceback if the matcher causes an
774 # exception, but do NOT crash the kernel!
805 # exception, but do NOT crash the kernel!
775 sys.excepthook()
806 sys.excepthook(*sys.exc_info())
776 else:
807 else:
777 for matcher in self.matchers:
808 for matcher in self.matchers:
778 self.matches = matcher(text)
809 self.matches = matcher(text)
779 if self.matches:
810 if self.matches:
780 break
811 break
781 # FIXME: we should extend our api to return a dict with completions for
812 # FIXME: we should extend our api to return a dict with completions for
782 # different types of objects. The rlcomplete() method could then
813 # different types of objects. The rlcomplete() method could then
783 # simply collapse the dict into a list for readline, but we'd have
814 # simply collapse the dict into a list for readline, but we'd have
784 # richer completion semantics in other evironments.
815 # richer completion semantics in other evironments.
785 self.matches = sorted(set(self.matches))
816 self.matches = sorted(set(self.matches))
786 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
817 #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg
787 return text, self.matches
818 return text, self.matches
788
819
789 def rlcomplete(self, text, state):
820 def rlcomplete(self, text, state):
790 """Return the state-th possible completion for 'text'.
821 """Return the state-th possible completion for 'text'.
791
822
792 This is called successively with state == 0, 1, 2, ... until it
823 This is called successively with state == 0, 1, 2, ... until it
793 returns None. The completion should begin with 'text'.
824 returns None. The completion should begin with 'text'.
794
825
795 Parameters
826 Parameters
796 ----------
827 ----------
797 text : string
828 text : string
798 Text to perform the completion on.
829 Text to perform the completion on.
799
830
800 state : int
831 state : int
801 Counter used by readline.
832 Counter used by readline.
802 """
833 """
803 if state==0:
834 if state==0:
804
835
805 self.line_buffer = line_buffer = self.readline.get_line_buffer()
836 self.line_buffer = line_buffer = self.readline.get_line_buffer()
806 cursor_pos = self.readline.get_endidx()
837 cursor_pos = self.readline.get_endidx()
807
838
808 #io.rprint("\nRLCOMPLETE: %r %r %r" %
839 #io.rprint("\nRLCOMPLETE: %r %r %r" %
809 # (text, line_buffer, cursor_pos) ) # dbg
840 # (text, line_buffer, cursor_pos) ) # dbg
810
841
811 # if there is only a tab on a line with only whitespace, instead of
842 # if there is only a tab on a line with only whitespace, instead of
812 # the mostly useless 'do you want to see all million completions'
843 # the mostly useless 'do you want to see all million completions'
813 # message, just do the right thing and give the user his tab!
844 # message, just do the right thing and give the user his tab!
814 # Incidentally, this enables pasting of tabbed text from an editor
845 # Incidentally, this enables pasting of tabbed text from an editor
815 # (as long as autoindent is off).
846 # (as long as autoindent is off).
816
847
817 # It should be noted that at least pyreadline still shows file
848 # It should be noted that at least pyreadline still shows file
818 # completions - is there a way around it?
849 # completions - is there a way around it?
819
850
820 # don't apply this on 'dumb' terminals, such as emacs buffers, so
851 # don't apply this on 'dumb' terminals, such as emacs buffers, so
821 # we don't interfere with their own tab-completion mechanism.
852 # we don't interfere with their own tab-completion mechanism.
822 if not (self.dumb_terminal or line_buffer.strip()):
853 if not (self.dumb_terminal or line_buffer.strip()):
823 self.readline.insert_text('\t')
854 self.readline.insert_text('\t')
824 sys.stdout.flush()
855 sys.stdout.flush()
825 return None
856 return None
826
857
827 # Note: debugging exceptions that may occur in completion is very
858 # Note: debugging exceptions that may occur in completion is very
828 # tricky, because readline unconditionally silences them. So if
859 # tricky, because readline unconditionally silences them. So if
829 # during development you suspect a bug in the completion code, turn
860 # during development you suspect a bug in the completion code, turn
830 # this flag on temporarily by uncommenting the second form (don't
861 # this flag on temporarily by uncommenting the second form (don't
831 # flip the value in the first line, as the '# dbg' marker can be
862 # flip the value in the first line, as the '# dbg' marker can be
832 # automatically detected and is used elsewhere).
863 # automatically detected and is used elsewhere).
833 DEBUG = False
864 DEBUG = False
834 #DEBUG = True # dbg
865 #DEBUG = True # dbg
835 if DEBUG:
866 if DEBUG:
836 try:
867 try:
837 self.complete(text, line_buffer, cursor_pos)
868 self.complete(text, line_buffer, cursor_pos)
838 except:
869 except:
839 import traceback; traceback.print_exc()
870 import traceback; traceback.print_exc()
840 else:
871 else:
841 # The normal production version is here
872 # The normal production version is here
842
873
843 # This method computes the self.matches array
874 # This method computes the self.matches array
844 self.complete(text, line_buffer, cursor_pos)
875 self.complete(text, line_buffer, cursor_pos)
845
876
846 try:
877 try:
847 return self.matches[state]
878 return self.matches[state]
848 except IndexError:
879 except IndexError:
849 return None
880 return None
@@ -1,115 +1,158 b''
1 """Tests for the IPython tab-completion machinery.
1 """Tests for the IPython tab-completion machinery.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Module imports
4 # Module imports
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6
6
7 # stdlib
7 # stdlib
8 import os
8 import sys
9 import sys
9 import unittest
10 import unittest
10
11
11 # third party
12 # third party
12 import nose.tools as nt
13 import nose.tools as nt
13
14
14 # our own packages
15 # our own packages
15 from IPython.core import completer
16 from IPython.core import completer
17 from IPython.utils.tempdir import TemporaryDirectory
16
18
17 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
18 # Test functions
20 # Test functions
19 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
20 def test_protect_filename():
22 def test_protect_filename():
21 pairs = [ ('abc','abc'),
23 pairs = [ ('abc','abc'),
22 (' abc',r'\ abc'),
24 (' abc',r'\ abc'),
23 ('a bc',r'a\ bc'),
25 ('a bc',r'a\ bc'),
24 ('a bc',r'a\ \ bc'),
26 ('a bc',r'a\ \ bc'),
25 (' bc',r'\ \ bc'),
27 (' bc',r'\ \ bc'),
26 ]
28 ]
27 # On posix, we also protect parens and other special characters
29 # On posix, we also protect parens and other special characters
28 if sys.platform != 'win32':
30 if sys.platform != 'win32':
29 pairs.extend( [('a(bc',r'a\(bc'),
31 pairs.extend( [('a(bc',r'a\(bc'),
30 ('a)bc',r'a\)bc'),
32 ('a)bc',r'a\)bc'),
31 ('a( )bc',r'a\(\ \)bc'),
33 ('a( )bc',r'a\(\ \)bc'),
32 ('a[1]bc', r'a\[1\]bc'),
34 ('a[1]bc', r'a\[1\]bc'),
33 ('a{1}bc', r'a\{1\}bc'),
35 ('a{1}bc', r'a\{1\}bc'),
34 ('a#bc', r'a\#bc'),
36 ('a#bc', r'a\#bc'),
35 ('a?bc', r'a\?bc'),
37 ('a?bc', r'a\?bc'),
36 ('a=bc', r'a\=bc'),
38 ('a=bc', r'a\=bc'),
37 ('a\\bc', r'a\\bc'),
39 ('a\\bc', r'a\\bc'),
38 ('a|bc', r'a\|bc'),
40 ('a|bc', r'a\|bc'),
39 ('a;bc', r'a\;bc'),
41 ('a;bc', r'a\;bc'),
40 ('a:bc', r'a\:bc'),
42 ('a:bc', r'a\:bc'),
41 ("a'bc", r"a\'bc"),
43 ("a'bc", r"a\'bc"),
42 ('a*bc', r'a\*bc'),
44 ('a*bc', r'a\*bc'),
43 ('a"bc', r'a\"bc'),
45 ('a"bc', r'a\"bc'),
44 ('a^bc', r'a\^bc'),
46 ('a^bc', r'a\^bc'),
45 ('a&bc', r'a\&bc'),
47 ('a&bc', r'a\&bc'),
46 ] )
48 ] )
47 # run the actual tests
49 # run the actual tests
48 for s1, s2 in pairs:
50 for s1, s2 in pairs:
49 s1p = completer.protect_filename(s1)
51 s1p = completer.protect_filename(s1)
50 nt.assert_equals(s1p, s2)
52 nt.assert_equals(s1p, s2)
51
53
52
54
53 def check_line_split(splitter, test_specs):
55 def check_line_split(splitter, test_specs):
54 for part1, part2, split in test_specs:
56 for part1, part2, split in test_specs:
55 cursor_pos = len(part1)
57 cursor_pos = len(part1)
56 line = part1+part2
58 line = part1+part2
57 out = splitter.split_line(line, cursor_pos)
59 out = splitter.split_line(line, cursor_pos)
58 nt.assert_equal(out, split)
60 nt.assert_equal(out, split)
59
61
60
62
61 def test_line_split():
63 def test_line_split():
62 """Basice line splitter test with default specs."""
64 """Basice line splitter test with default specs."""
63 sp = completer.CompletionSplitter()
65 sp = completer.CompletionSplitter()
64 # The format of the test specs is: part1, part2, expected answer. Parts 1
66 # The format of the test specs is: part1, part2, expected answer. Parts 1
65 # and 2 are joined into the 'line' sent to the splitter, as if the cursor
67 # and 2 are joined into the 'line' sent to the splitter, as if the cursor
66 # was at the end of part1. So an empty part2 represents someone hitting
68 # was at the end of part1. So an empty part2 represents someone hitting
67 # tab at the end of the line, the most common case.
69 # tab at the end of the line, the most common case.
68 t = [('run some/scrip', '', 'some/scrip'),
70 t = [('run some/scrip', '', 'some/scrip'),
69 ('run scripts/er', 'ror.py foo', 'scripts/er'),
71 ('run scripts/er', 'ror.py foo', 'scripts/er'),
70 ('echo $HOM', '', 'HOM'),
72 ('echo $HOM', '', 'HOM'),
71 ('print sys.pa', '', 'sys.pa'),
73 ('print sys.pa', '', 'sys.pa'),
72 ('print(sys.pa', '', 'sys.pa'),
74 ('print(sys.pa', '', 'sys.pa'),
73 ("execfile('scripts/er", '', 'scripts/er'),
75 ("execfile('scripts/er", '', 'scripts/er'),
74 ('a[x.', '', 'x.'),
76 ('a[x.', '', 'x.'),
75 ('a[x.', 'y', 'x.'),
77 ('a[x.', 'y', 'x.'),
76 ('cd "some_file/', '', 'some_file/'),
78 ('cd "some_file/', '', 'some_file/'),
77 ]
79 ]
78 check_line_split(sp, t)
80 check_line_split(sp, t)
79 # Ensure splitting works OK with unicode by re-running the tests with
81 # Ensure splitting works OK with unicode by re-running the tests with
80 # all inputs turned into unicode
82 # all inputs turned into unicode
81 check_line_split(sp, [ map(unicode, p) for p in t] )
83 check_line_split(sp, [ map(unicode, p) for p in t] )
82
84
83
85
84 def test_unicode_completions():
86 def test_unicode_completions():
85 ip = get_ipython()
87 ip = get_ipython()
86 # Some strings that trigger different types of completion. Check them both
88 # Some strings that trigger different types of completion. Check them both
87 # in str and unicode forms
89 # in str and unicode forms
88 s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/']
90 s = ['ru', '%ru', 'cd /', 'floa', 'float(x)/']
89 for t in s + map(unicode, s):
91 for t in s + map(unicode, s):
90 # We don't need to check exact completion values (they may change
92 # We don't need to check exact completion values (they may change
91 # depending on the state of the namespace, but at least no exceptions
93 # depending on the state of the namespace, but at least no exceptions
92 # should be thrown and the return value should be a pair of text, list
94 # should be thrown and the return value should be a pair of text, list
93 # values.
95 # values.
94 text, matches = ip.complete(t)
96 text, matches = ip.complete(t)
95 nt.assert_true(isinstance(text, basestring))
97 nt.assert_true(isinstance(text, basestring))
96 nt.assert_true(isinstance(matches, list))
98 nt.assert_true(isinstance(matches, list))
97
99
98
100
99 class CompletionSplitterTestCase(unittest.TestCase):
101 class CompletionSplitterTestCase(unittest.TestCase):
100 def setUp(self):
102 def setUp(self):
101 self.sp = completer.CompletionSplitter()
103 self.sp = completer.CompletionSplitter()
102
104
103 def test_delim_setting(self):
105 def test_delim_setting(self):
104 self.sp.set_delims(' ')
106 self.sp.set_delims(' ')
105 nt.assert_equal(self.sp.get_delims(), ' ')
107 nt.assert_equal(self.sp.get_delims(), ' ')
106 nt.assert_equal(self.sp._delim_expr, '[\ ]')
108 nt.assert_equal(self.sp._delim_expr, '[\ ]')
107
109
108 def test_spaces(self):
110 def test_spaces(self):
109 """Test with only spaces as split chars."""
111 """Test with only spaces as split chars."""
110 self.sp.delims = ' '
112 self.sp.delims = ' '
111 t = [('foo', '', 'foo'),
113 t = [('foo', '', 'foo'),
112 ('run foo', '', 'foo'),
114 ('run foo', '', 'foo'),
113 ('run foo', 'bar', 'foo'),
115 ('run foo', 'bar', 'foo'),
114 ]
116 ]
115 check_line_split(self.sp, t)
117 check_line_split(self.sp, t)
118
119
120 def test_has_open_quotes1():
121 for s in ["'", "'''", "'hi' '"]:
122 nt.assert_equal(completer.has_open_quotes(s), "'")
123
124
125 def test_has_open_quotes2():
126 for s in ['"', '"""', '"hi" "']:
127 nt.assert_equal(completer.has_open_quotes(s), '"')
128
129
130 def test_has_open_quotes3():
131 for s in ["''", "''' '''", "'hi' 'ipython'"]:
132 nt.assert_false(completer.has_open_quotes(s))
133
134
135 def test_has_open_quotes4():
136 for s in ['""', '""" """', '"hi" "ipython"']:
137 nt.assert_false(completer.has_open_quotes(s))
138
139
140 def test_file_completions():
141
142 ip = get_ipython()
143 with TemporaryDirectory() as tmpdir:
144 prefix = os.path.join(tmpdir, 'foo')
145 suffixes = map(str, [1,2])
146 names = [prefix+s for s in suffixes]
147 for n in names:
148 open(n, 'w').close()
149
150 # Check simple completion
151 c = ip.complete(prefix)[1]
152 nt.assert_equal(c, names)
153
154 # Now check with a function call
155 cmd = 'a = f("%s' % prefix
156 c = ip.complete(prefix, cmd)[1]
157 comp = [prefix+s for s in suffixes]
158 nt.assert_equal(c, comp)
General Comments 0
You need to be logged in to leave comments. Login now