Show More
@@ -1,50 +1,13 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Word completion for IPython. |
|
2 | """Word completion for IPython. | |
3 |
|
3 | |||
4 |
This module |
|
4 | This module stared as 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, |
|
6 | upstream and were accepted as of Python 2.3, | |
7 | functionality specific to IPython, so this module will continue to live as an |
|
|||
8 | IPython-specific utility. |
|
|||
9 |
|
7 | |||
10 | Original rlcompleter documentation: |
|
|||
11 |
|
8 | |||
12 | This requires the latest extension to the readline module (the |
|
9 | This is now mostly Completer implementation made to function with | |
13 | completes keywords, built-ins and globals in __main__; when completing |
|
10 | prompt_toolkit, but that should still work with readline. | |
14 | NAME.NAME..., it evaluates (!) the expression up to the last dot and |
|
|||
15 | completes its attributes. |
|
|||
16 |
|
||||
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 |
|
|||
19 | string module! |
|
|||
20 |
|
||||
21 | Tip: to use the tab key as the completion key, call |
|
|||
22 |
|
||||
23 | readline.parse_and_bind("tab: complete") |
|
|||
24 |
|
||||
25 | Notes: |
|
|||
26 |
|
||||
27 | - Exceptions raised by the completer function are *ignored* (and |
|
|||
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 |
|
|||
30 | traceback wouldn't work well without some complicated hoopla to save, |
|
|||
31 | reset and restore the tty state. |
|
|||
32 |
|
||||
33 | - The evaluation of the NAME.NAME... form may cause arbitrary |
|
|||
34 | application defined code to be executed if an object with a |
|
|||
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 |
|
|||
37 | acceptable risk. More complicated expressions (e.g. function calls or |
|
|||
38 | indexing operations) are *not* evaluated. |
|
|||
39 |
|
||||
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 |
|
|||
42 | features. Clearly an interactive application can benefit by |
|
|||
43 | specifying its own completer function and using raw_input() for all |
|
|||
44 | its input. |
|
|||
45 |
|
||||
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. |
|
|||
48 | """ |
|
11 | """ | |
49 |
|
12 | |||
50 | # Copyright (c) IPython Development Team. |
|
13 | # Copyright (c) IPython Development Team. | |
@@ -1211,66 +1174,3 b' class IPCompleter(Completer):' | |||||
1211 | self.matches = sorted(set(self.matches), key=completions_sorting_key) |
|
1174 | self.matches = sorted(set(self.matches), key=completions_sorting_key) | |
1212 |
|
1175 | |||
1213 | return text, self.matches |
|
1176 | return text, self.matches | |
1214 |
|
||||
1215 | def rlcomplete(self, text, state): |
|
|||
1216 | """Return the state-th possible completion for 'text'. |
|
|||
1217 |
|
||||
1218 | This is called successively with state == 0, 1, 2, ... until it |
|
|||
1219 | returns None. The completion should begin with 'text'. |
|
|||
1220 |
|
||||
1221 | Parameters |
|
|||
1222 | ---------- |
|
|||
1223 | text : string |
|
|||
1224 | Text to perform the completion on. |
|
|||
1225 |
|
||||
1226 | state : int |
|
|||
1227 | Counter used by readline. |
|
|||
1228 | """ |
|
|||
1229 | if state==0: |
|
|||
1230 |
|
||||
1231 | self.line_buffer = line_buffer = self.readline.get_line_buffer() |
|
|||
1232 | cursor_pos = self.readline.get_endidx() |
|
|||
1233 |
|
||||
1234 | #io.rprint("\nRLCOMPLETE: %r %r %r" % |
|
|||
1235 | # (text, line_buffer, cursor_pos) ) # dbg |
|
|||
1236 |
|
||||
1237 | # if there is only a tab on a line with only whitespace, instead of |
|
|||
1238 | # the mostly useless 'do you want to see all million completions' |
|
|||
1239 | # message, just do the right thing and give the user his tab! |
|
|||
1240 | # Incidentally, this enables pasting of tabbed text from an editor |
|
|||
1241 | # (as long as autoindent is off). |
|
|||
1242 |
|
||||
1243 | # It should be noted that at least pyreadline still shows file |
|
|||
1244 | # completions - is there a way around it? |
|
|||
1245 |
|
||||
1246 | # don't apply this on 'dumb' terminals, such as emacs buffers, so |
|
|||
1247 | # we don't interfere with their own tab-completion mechanism. |
|
|||
1248 | if not (self.dumb_terminal or line_buffer.strip()): |
|
|||
1249 | self.readline.insert_text('\t') |
|
|||
1250 | sys.stdout.flush() |
|
|||
1251 | return None |
|
|||
1252 |
|
||||
1253 | # Note: debugging exceptions that may occur in completion is very |
|
|||
1254 | # tricky, because readline unconditionally silences them. So if |
|
|||
1255 | # during development you suspect a bug in the completion code, turn |
|
|||
1256 | # this flag on temporarily by uncommenting the second form (don't |
|
|||
1257 | # flip the value in the first line, as the '# dbg' marker can be |
|
|||
1258 | # automatically detected and is used elsewhere). |
|
|||
1259 | DEBUG = False |
|
|||
1260 | #DEBUG = True # dbg |
|
|||
1261 | if DEBUG: |
|
|||
1262 | try: |
|
|||
1263 | self.complete(text, line_buffer, cursor_pos) |
|
|||
1264 | except: |
|
|||
1265 | import traceback; traceback.print_exc() |
|
|||
1266 | else: |
|
|||
1267 | # The normal production version is here |
|
|||
1268 |
|
||||
1269 | # This method computes the self.matches array |
|
|||
1270 | self.complete(text, line_buffer, cursor_pos) |
|
|||
1271 |
|
||||
1272 | try: |
|
|||
1273 | return self.matches[state] |
|
|||
1274 | except IndexError: |
|
|||
1275 | return None |
|
|||
1276 |
|
@@ -343,9 +343,6 b' class InteractiveShell(SingletonConfigurable):' | |||||
343 | Automatically call the pdb debugger after every exception. |
|
343 | Automatically call the pdb debugger after every exception. | |
344 | """ |
|
344 | """ | |
345 | ).tag(config=True) |
|
345 | ).tag(config=True) | |
346 | multiline_history = Bool(sys.platform != 'win32', |
|
|||
347 | help="Save multi-line entries as one entry in readline history" |
|
|||
348 | ).tag(config=True) |
|
|||
349 | display_page = Bool(False, |
|
346 | display_page = Bool(False, | |
350 | help="""If True, anything that would be passed to the pager |
|
347 | help="""If True, anything that would be passed to the pager | |
351 | will be displayed as regular output instead.""" |
|
348 | will be displayed as regular output instead.""" | |
@@ -387,40 +384,10 b' class InteractiveShell(SingletonConfigurable):' | |||||
387 | history_load_length = Integer(1000, help= |
|
384 | history_load_length = Integer(1000, help= | |
388 | """ |
|
385 | """ | |
389 | The number of saved history entries to be loaded |
|
386 | The number of saved history entries to be loaded | |
390 |
into the |
|
387 | into the history buffer at startup. | |
391 | """ |
|
388 | """ | |
392 | ).tag(config=True) |
|
389 | ).tag(config=True) | |
393 |
|
390 | |||
394 | # The readline stuff will eventually be moved to the terminal subclass |
|
|||
395 | # but for now, we can't do that as readline is welded in everywhere. |
|
|||
396 | readline_use = Bool(True).tag(config=True) |
|
|||
397 | readline_remove_delims = Unicode('-/~').tag(config=True) |
|
|||
398 | readline_delims = Unicode() # set by init_readline() |
|
|||
399 | # don't use \M- bindings by default, because they |
|
|||
400 | # conflict with 8-bit encodings. See gh-58,gh-88 |
|
|||
401 | readline_parse_and_bind = List([ |
|
|||
402 | 'tab: complete', |
|
|||
403 | '"\C-l": clear-screen', |
|
|||
404 | 'set show-all-if-ambiguous on', |
|
|||
405 | '"\C-o": tab-insert', |
|
|||
406 | '"\C-r": reverse-search-history', |
|
|||
407 | '"\C-s": forward-search-history', |
|
|||
408 | '"\C-p": history-search-backward', |
|
|||
409 | '"\C-n": history-search-forward', |
|
|||
410 | '"\e[A": history-search-backward', |
|
|||
411 | '"\e[B": history-search-forward', |
|
|||
412 | '"\C-k": kill-line', |
|
|||
413 | '"\C-u": unix-line-discard', |
|
|||
414 | ]).tag(config=True) |
|
|||
415 |
|
||||
416 | _custom_readline_config = False |
|
|||
417 |
|
||||
418 | @observe('readline_parse_and_bind') |
|
|||
419 | def _readline_parse_and_bind_changed(self, change): |
|
|||
420 | # notice that readline config is customized |
|
|||
421 | # indicates that it should have higher priority than inputrc |
|
|||
422 | self._custom_readline_config = True |
|
|||
423 |
|
||||
424 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], |
|
391 | ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none'], | |
425 | default_value='last_expr', |
|
392 | default_value='last_expr', | |
426 | help=""" |
|
393 | help=""" |
@@ -109,12 +109,9 b' MAIN FEATURES' | |||||
109 | variable names, and show you a list of the possible completions if there's |
|
109 | variable names, and show you a list of the possible completions if there's | |
110 | no unambiguous one. It will also complete filenames in the current directory. |
|
110 | no unambiguous one. It will also complete filenames in the current directory. | |
111 |
|
111 | |||
112 | This feature requires the readline and rlcomplete modules, so it won't work |
|
112 | * Search previous command history in two ways: | |
113 | if your Python lacks readline support (such as under Windows). |
|
|||
114 |
|
113 | |||
115 | * Search previous command history in two ways (also requires readline): |
|
114 | - Start typing, and then use Ctrl-p (previous, up) and Ctrl-n (next,down) to | |
116 |
|
||||
117 | - Start typing, and then use Ctrl-p (previous,up) and Ctrl-n (next,down) to |
|
|||
118 | search through only the history items that match what you've typed so |
|
115 | search through only the history items that match what you've typed so | |
119 | far. If you use Ctrl-p/Ctrl-n at a blank prompt, they just behave like |
|
116 | far. If you use Ctrl-p/Ctrl-n at a blank prompt, they just behave like | |
120 | normal arrow keys. |
|
117 | normal arrow keys. | |
@@ -123,7 +120,7 b' MAIN FEATURES' | |||||
123 | your history for lines that match what you've typed so far, completing as |
|
120 | your history for lines that match what you've typed so far, completing as | |
124 | much as it can. |
|
121 | much as it can. | |
125 |
|
122 | |||
126 |
- %hist: search history by index |
|
123 | - %hist: search history by index. | |
127 |
|
124 | |||
128 | * Persistent command history across sessions. |
|
125 | * Persistent command history across sessions. | |
129 |
|
126 | |||
@@ -255,7 +252,6 b' MAIN FEATURES' | |||||
255 | interactive_usage_min = """\ |
|
252 | interactive_usage_min = """\ | |
256 | An enhanced console for Python. |
|
253 | An enhanced console for Python. | |
257 | Some of its features are: |
|
254 | Some of its features are: | |
258 | - Readline support if the readline library is present. |
|
|||
259 | - Tab completion in the local namespace. |
|
255 | - Tab completion in the local namespace. | |
260 | - Logging of input, see command-line options. |
|
256 | - Logging of input, see command-line options. | |
261 | - System shell escape via ! , eg !ls. |
|
257 | - System shell escape via ! , eg !ls. |
@@ -220,20 +220,8 b" if not any(arg.startswith('bdist') for arg in sys.argv):" | |||||
220 |
|
220 | |||
221 | if sys.platform == 'darwin': |
|
221 | if sys.platform == 'darwin': | |
222 | install_requires.extend(['appnope']) |
|
222 | install_requires.extend(['appnope']) | |
223 | have_readline = False |
|
223 | ||
224 | try: |
|
224 | if not sys.platform.startswith('win'): | |
225 | import readline |
|
|||
226 | except ImportError: |
|
|||
227 | pass |
|
|||
228 | else: |
|
|||
229 | if 'libedit' not in readline.__doc__: |
|
|||
230 | have_readline = True |
|
|||
231 | if not have_readline: |
|
|||
232 | install_requires.extend(['gnureadline']) |
|
|||
233 |
|
||||
234 | if sys.platform.startswith('win'): |
|
|||
235 | extras_require['terminal'].append('pyreadline>=2.0') |
|
|||
236 | else: |
|
|||
237 | install_requires.append('pexpect') |
|
225 | install_requires.append('pexpect') | |
238 |
|
226 | |||
239 | # workaround pypa/setuptools#147, where setuptools misspells |
|
227 | # workaround pypa/setuptools#147, where setuptools misspells |
General Comments 0
You need to be logged in to leave comments.
Login now