Show More
@@ -8,15 +8,14 from .ptutils import IPythonPTCompleter | |||
|
8 | 8 | from .shortcuts import suspend_to_bg, cursor_in_leading_ws |
|
9 | 9 | |
|
10 | 10 | from prompt_toolkit.enums import DEFAULT_BUFFER |
|
11 |
from prompt_toolkit.filters import (Condition, |
|
|
12 |
|
|
|
13 |
from prompt_toolkit.key |
|
|
14 | from prompt_toolkit.key_binding.manager import KeyBindingManager | |
|
11 | from prompt_toolkit.filters import (Condition, has_focus, has_selection, | |
|
12 | vi_insert_mode, emacs_insert_mode) | |
|
13 | from prompt_toolkit.key_binding import KeyBindings | |
|
15 | 14 | from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline |
|
16 |
from p |
|
|
17 |
from prompt_toolkit.shortcuts import |
|
|
18 | from prompt_toolkit.interface import CommandLineInterface | |
|
15 | from pygments.token import Token | |
|
16 | from prompt_toolkit.shortcuts.prompt import PromptSession | |
|
19 | 17 | from prompt_toolkit.enums import EditingMode |
|
18 | from prompt_toolkit.formatted_text import PygmentsTokens | |
|
20 | 19 | |
|
21 | 20 | |
|
22 | 21 | class TerminalPdb(Pdb): |
@@ -26,46 +25,40 class TerminalPdb(Pdb): | |||
|
26 | 25 | self.pt_init() |
|
27 | 26 | |
|
28 | 27 | def pt_init(self): |
|
29 |
def get_prompt_tokens( |
|
|
28 | def get_prompt_tokens(): | |
|
30 | 29 | return [(Token.Prompt, self.prompt)] |
|
31 | 30 | |
|
32 | def patch_stdout(**kwargs): | |
|
33 | return self.pt_cli.patch_stdout_context(**kwargs) | |
|
34 | ||
|
35 | 31 | if self._ptcomp is None: |
|
36 | 32 | compl = IPCompleter(shell=self.shell, |
|
37 | 33 | namespace={}, |
|
38 | 34 | global_namespace={}, |
|
39 | 35 | parent=self.shell, |
|
40 | 36 | ) |
|
41 |
self._ptcomp = IPythonPTCompleter(compl |
|
|
37 | self._ptcomp = IPythonPTCompleter(compl) | |
|
42 | 38 | |
|
43 |
kb |
|
|
44 |
supports_suspend = Condition(lambda |
|
|
45 | kbmanager.registry.add_binding(Keys.ControlZ, filter=supports_suspend | |
|
46 | )(suspend_to_bg) | |
|
39 | kb = KeyBindings() | |
|
40 | supports_suspend = Condition(lambda: hasattr(signal, 'SIGTSTP')) | |
|
41 | kb.add('c-z', filter=supports_suspend)(suspend_to_bg) | |
|
47 | 42 | |
|
48 | 43 | if self.shell.display_completions == 'readlinelike': |
|
49 | kbmanager.registry.add_binding(Keys.ControlI, | |
|
50 |
|
|
|
51 |
|
|
|
52 | & ViInsertMode() | EmacsInsertMode() | |
|
53 | & ~cursor_in_leading_ws | |
|
54 | ))(display_completions_like_readline) | |
|
55 | multicolumn = (self.shell.display_completions == 'multicolumn') | |
|
56 | ||
|
57 | self._pt_app = create_prompt_application( | |
|
44 | kb.add('tab', filter=(has_focus(DEFAULT_BUFFER) | |
|
45 | & ~has_selection | |
|
46 | & vi_insert_mode | emacs_insert_mode | |
|
47 | & ~cursor_in_leading_ws | |
|
48 | ))(display_completions_like_readline) | |
|
49 | ||
|
50 | self.pt_app = PromptSession( | |
|
51 | message=(lambda: PygmentsTokens(get_prompt_tokens())), | |
|
58 | 52 | editing_mode=getattr(EditingMode, self.shell.editing_mode.upper()), |
|
59 |
key_bindings |
|
|
53 | key_bindings=kb, | |
|
60 | 54 | history=self.shell.debugger_history, |
|
61 |
completer= |
|
|
55 | completer=self._ptcomp, | |
|
62 | 56 | enable_history_search=True, |
|
63 | 57 | mouse_support=self.shell.mouse_support, |
|
64 |
|
|
|
65 | display_completions_in_columns=multicolumn, | |
|
66 |
|
|
|
58 | complete_style=self.shell.pt_complete_style, | |
|
59 | style=self.shell.style, | |
|
60 | inputhook=self.shell.inputhook, | |
|
67 | 61 | ) |
|
68 | self.pt_cli = CommandLineInterface(self._pt_app, eventloop=self.shell._eventloop) | |
|
69 | 62 | |
|
70 | 63 | def cmdloop(self, intro=None): |
|
71 | 64 | """Repeatedly issue a prompt, accept input, parse an initial prefix |
@@ -92,7 +85,7 class TerminalPdb(Pdb): | |||
|
92 | 85 | self._ptcomp.ipy_completer.namespace = self.curframe_locals |
|
93 | 86 | self._ptcomp.ipy_completer.global_namespace = self.curframe.f_globals |
|
94 | 87 | try: |
|
95 |
line = self.pt_ |
|
|
88 | line = self.pt_app.prompt() # reset_current_buffer=True) | |
|
96 | 89 | except EOFError: |
|
97 | 90 | line = 'EOF' |
|
98 | 91 | line = self.precmd(line) |
@@ -18,12 +18,15 from traitlets import ( | |||
|
18 | 18 | from prompt_toolkit.document import Document |
|
19 | 19 | from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode |
|
20 | 20 | from prompt_toolkit.filters import (HasFocus, Condition, IsDone) |
|
21 | from prompt_toolkit.formatted_text import PygmentsTokens | |
|
21 | 22 | from prompt_toolkit.history import InMemoryHistory |
|
22 |
from prompt_toolkit.shortcuts import |
|
|
23 |
from prompt_toolkit. |
|
|
24 |
from prompt_toolkit.key_binding |
|
|
23 | from prompt_toolkit.shortcuts import PromptSession, CompleteStyle | |
|
24 | from prompt_toolkit.output.defaults import create_output | |
|
25 | from prompt_toolkit.key_binding import KeyBindings | |
|
25 | 26 | from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor |
|
26 |
from prompt_toolkit. |
|
|
27 | from prompt_toolkit.patch_stdout import patch_stdout | |
|
28 | from prompt_toolkit.styles import DynamicStyle, merge_styles | |
|
29 | from prompt_toolkit.styles.pygments import style_from_pygments_cls, style_from_pygments_dict | |
|
27 | 30 | |
|
28 | 31 | from pygments.styles import get_style_by_name |
|
29 | 32 | from pygments.style import Style |
@@ -34,7 +37,7 from .magics import TerminalMagics | |||
|
34 | 37 | from .pt_inputhooks import get_inputhook_name_and_func |
|
35 | 38 | from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook |
|
36 | 39 | from .ptutils import IPythonPTCompleter, IPythonPTLexer |
|
37 |
from .shortcuts import |
|
|
40 | from .shortcuts import create_ipython_shortcuts | |
|
38 | 41 | |
|
39 | 42 | DISPLAY_BANNER_DEPRECATED = object() |
|
40 | 43 | |
@@ -91,12 +94,11 class TerminalInteractiveShell(InteractiveShell): | |||
|
91 | 94 | 'to reserve for the completion menu' |
|
92 | 95 | ).tag(config=True) |
|
93 | 96 | |
|
94 | def _space_for_menu_changed(self, old, new): | |
|
95 | self._update_layout() | |
|
97 | # def _space_for_menu_changed(self, old, new): | |
|
98 | # self._update_layout() | |
|
96 | 99 | |
|
97 |
pt_ |
|
|
100 | pt_app = None | |
|
98 | 101 | debugger_history = None |
|
99 | _pt_app = None | |
|
100 | 102 | |
|
101 | 103 | simple_prompt = Bool(_use_simple_prompt, |
|
102 | 104 | help="""Use `raw_input` for the REPL, without completion and prompt colors. |
@@ -167,9 +169,9 class TerminalInteractiveShell(InteractiveShell): | |||
|
167 | 169 | def _prompts_default(self): |
|
168 | 170 | return self.prompts_class(self) |
|
169 | 171 | |
|
170 | @observe('prompts') | |
|
171 | def _(self, change): | |
|
172 | self._update_layout() | |
|
172 | # @observe('prompts') | |
|
173 | # def _(self, change): | |
|
174 | # self._update_layout() | |
|
173 | 175 | |
|
174 | 176 | @default('displayhook_class') |
|
175 | 177 | def _displayhook_class_default(self): |
@@ -243,10 +245,7 class TerminalInteractiveShell(InteractiveShell): | |||
|
243 | 245 | return |
|
244 | 246 | |
|
245 | 247 | # Set up keyboard shortcuts |
|
246 | kbmanager = KeyBindingManager.for_prompt( | |
|
247 | enable_open_in_editor=self.extra_open_editor_shortcuts, | |
|
248 | ) | |
|
249 | register_ipython_shortcuts(kbmanager.registry, self) | |
|
248 | key_bindings = create_ipython_shortcuts(self) | |
|
250 | 249 | |
|
251 | 250 | # Pre-populate history from IPython's history database |
|
252 | 251 | history = InMemoryHistory() |
@@ -256,7 +255,7 class TerminalInteractiveShell(InteractiveShell): | |||
|
256 | 255 | # Ignore blank lines and consecutive duplicates |
|
257 | 256 | cell = cell.rstrip() |
|
258 | 257 | if cell and (cell != last_cell): |
|
259 | history.append(cell) | |
|
258 | history.append_string(cell) | |
|
260 | 259 | last_cell = cell |
|
261 | 260 | |
|
262 | 261 | self._style = self._make_style_from_name_or_cls(self.highlighting_style) |
@@ -264,24 +263,18 class TerminalInteractiveShell(InteractiveShell): | |||
|
264 | 263 | |
|
265 | 264 | editing_mode = getattr(EditingMode, self.editing_mode.upper()) |
|
266 | 265 | |
|
267 | def patch_stdout(**kwargs): | |
|
268 | return self.pt_cli.patch_stdout_context(**kwargs) | |
|
269 | ||
|
270 | self._pt_app = create_prompt_application( | |
|
266 | self.pt_app = PromptSession( | |
|
271 | 267 | editing_mode=editing_mode, |
|
272 |
key_bindings |
|
|
268 | key_bindings=key_bindings, | |
|
273 | 269 | history=history, |
|
274 | completer=IPythonPTCompleter(shell=self, | |
|
275 | patch_stdout=patch_stdout), | |
|
276 | enable_history_search=self.enable_history_search, | |
|
270 | completer=IPythonPTCompleter(shell=self), | |
|
271 | enable_history_search = self.enable_history_search, | |
|
277 | 272 | style=self.style, |
|
273 | include_default_pygments_style=False, | |
|
278 | 274 | mouse_support=self.mouse_support, |
|
279 | **self._layout_options() | |
|
280 | ) | |
|
281 | self._eventloop = create_eventloop(self.inputhook) | |
|
282 | self.pt_cli = CommandLineInterface( | |
|
283 | self._pt_app, eventloop=self._eventloop, | |
|
284 | output=create_output(true_color=self.true_color)) | |
|
275 | enable_open_in_editor=self.extra_open_editor_shortcuts, | |
|
276 | color_depth=(ColorDepth.TRUE_COLOR if self.true_color else None), | |
|
277 | **self._extra_prompt_options()) | |
|
285 | 278 | |
|
286 | 279 | def _make_style_from_name_or_cls(self, name_or_cls): |
|
287 | 280 | """ |
@@ -342,44 +335,60 class TerminalInteractiveShell(InteractiveShell): | |||
|
342 | 335 | Token.OutPromptNum: '#ff0000 bold', |
|
343 | 336 | } |
|
344 | 337 | style_overrides.update(self.highlighting_style_overrides) |
|
345 | style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls, | |
|
346 | style_dict=style_overrides) | |
|
338 | style = merge_styles([ | |
|
339 | style_from_pygments_cls(style_cls), | |
|
340 | style_from_pygments_dict(style_overrides), | |
|
341 | ]) | |
|
347 | 342 | |
|
348 | 343 | return style |
|
349 | 344 | |
|
350 | def _layout_options(self): | |
|
345 | @property | |
|
346 | def pt_complete_style(self): | |
|
347 | return { | |
|
348 | 'multicolumn': CompleteStyle.MULTI_COLUMN, | |
|
349 | 'column': CompleteStyle.COLUMN, | |
|
350 | 'readlinelike': CompleteStyle.READLINE_LIKE, | |
|
351 | }[self.display_completions], | |
|
352 | ||
|
353 | def _extra_prompt_options(self): | |
|
351 | 354 | """ |
|
352 | 355 | Return the current layout option for the current Terminal InteractiveShell |
|
353 | 356 | """ |
|
357 | def get_message(): | |
|
358 | return PygmentsTokens(self.prompts.in_prompt_tokens()) | |
|
359 | ||
|
354 | 360 | return { |
|
361 | 'complete_in_thread': False, | |
|
355 | 362 | 'lexer':IPythonPTLexer(), |
|
356 | 363 | 'reserve_space_for_menu':self.space_for_menu, |
|
357 | 'get_prompt_tokens':self.prompts.in_prompt_tokens, | |
|
358 | 'get_continuation_tokens':self.prompts.continuation_prompt_tokens, | |
|
359 | 'multiline':True, | |
|
360 | 'display_completions_in_columns': (self.display_completions == 'multicolumn'), | |
|
364 | 'message': get_message, | |
|
365 | 'prompt_continuation': ( | |
|
366 | lambda width, lineno, is_soft_wrap: | |
|
367 | PygmentsTokens(self.prompts.continuation_prompt_tokens(width))), | |
|
368 | 'multiline': True, | |
|
369 | 'complete_style': self.pt_complete_style, | |
|
361 | 370 | |
|
362 | 371 | # Highlight matching brackets, but only when this setting is |
|
363 | 372 | # enabled, and only when the DEFAULT_BUFFER has the focus. |
|
364 |
' |
|
|
373 | 'input_processors': [ConditionalProcessor( | |
|
365 | 374 | processor=HighlightMatchingBracketProcessor(chars='[](){}'), |
|
366 | 375 | filter=HasFocus(DEFAULT_BUFFER) & ~IsDone() & |
|
367 |
Condition(lambda |
|
|
376 | Condition(lambda: self.highlight_matching_brackets))], | |
|
368 | 377 | } |
|
369 | 378 | |
|
370 | def _update_layout(self): | |
|
371 | """ | |
|
372 | Ask for a re computation of the application layout, if for example , | |
|
373 | some configuration options have changed. | |
|
374 | """ | |
|
375 | if self._pt_app: | |
|
376 | self._pt_app.layout = create_prompt_layout(**self._layout_options()) | |
|
377 | ||
|
378 | 379 | def prompt_for_code(self): |
|
379 | with self.pt_cli.patch_stdout_context(raw=True): | |
|
380 |
d |
|
|
381 | pre_run=self.pre_prompt, reset_current_buffer=True) | |
|
382 | return document.text | |
|
380 | if self.rl_next_input: | |
|
381 | default = self.rl_next_input | |
|
382 | self.rl_next_input = None | |
|
383 | else: | |
|
384 | default = '' | |
|
385 | ||
|
386 | with patch_stdout(raw=True): | |
|
387 | text = self.pt_app.prompt( | |
|
388 | default=default, | |
|
389 | # pre_run=self.pre_prompt,# reset_current_buffer=True, | |
|
390 | **self._extra_prompt_options()) | |
|
391 | return text | |
|
383 | 392 | |
|
384 | 393 | def enable_win_unicode_console(self): |
|
385 | 394 | if sys.version_info >= (3, 6): |
@@ -439,22 +448,6 class TerminalInteractiveShell(InteractiveShell): | |||
|
439 | 448 | |
|
440 | 449 | rl_next_input = None |
|
441 | 450 | |
|
442 | def pre_prompt(self): | |
|
443 | if self.rl_next_input: | |
|
444 | # We can't set the buffer here, because it will be reset just after | |
|
445 | # this. Adding a callable to pre_run_callables does what we need | |
|
446 | # after the buffer is reset. | |
|
447 | s = self.rl_next_input | |
|
448 | def set_doc(): | |
|
449 | self.pt_cli.application.buffer.document = Document(s) | |
|
450 | if hasattr(self.pt_cli, 'pre_run_callables'): | |
|
451 | self.pt_cli.pre_run_callables.append(set_doc) | |
|
452 | else: | |
|
453 | # Older version of prompt_toolkit; it's OK to set the document | |
|
454 | # directly here. | |
|
455 | set_doc() | |
|
456 | self.rl_next_input = None | |
|
457 | ||
|
458 | 451 | def interact(self, display_banner=DISPLAY_BANNER_DEPRECATED): |
|
459 | 452 | |
|
460 | 453 | if display_banner is not DISPLAY_BANNER_DEPRECATED: |
@@ -517,8 +510,8 class TerminalInteractiveShell(InteractiveShell): | |||
|
517 | 510 | return |
|
518 | 511 | |
|
519 | 512 | tokens = self.prompts.rewrite_prompt_tokens() |
|
520 |
if self.pt_ |
|
|
521 |
self.pt_ |
|
|
513 | if self.pt_app: | |
|
514 | self.pt_app.print_tokens(tokens) # XXX | |
|
522 | 515 | print(cmd) |
|
523 | 516 | else: |
|
524 | 517 | prompt = ''.join(s for t, s in tokens) |
@@ -533,7 +526,7 class TerminalInteractiveShell(InteractiveShell): | |||
|
533 | 526 | elif self._prompts_before: |
|
534 | 527 | self.prompts = self._prompts_before |
|
535 | 528 | self._prompts_before = None |
|
536 | self._update_layout() | |
|
529 | # self._update_layout() | |
|
537 | 530 | |
|
538 | 531 | |
|
539 | 532 | InteractiveShellABC.register(TerminalInteractiveShell) |
@@ -5,13 +5,15 import sys | |||
|
5 | 5 | |
|
6 | 6 | from IPython.core.displayhook import DisplayHook |
|
7 | 7 | |
|
8 |
from prompt_toolkit. |
|
|
8 | from prompt_toolkit.formatted_text import fragment_list_width, PygmentsTokens | |
|
9 | from prompt_toolkit.shortcuts import print_formatted_text | |
|
10 | ||
|
9 | 11 | |
|
10 | 12 | class Prompts(object): |
|
11 | 13 | def __init__(self, shell): |
|
12 | 14 | self.shell = shell |
|
13 | 15 | |
|
14 |
def in_prompt_tokens(self |
|
|
16 | def in_prompt_tokens(self): | |
|
15 | 17 | return [ |
|
16 | 18 | (Token.Prompt, 'In ['), |
|
17 | 19 | (Token.PromptNum, str(self.shell.execution_count)), |
@@ -19,9 +21,9 class Prompts(object): | |||
|
19 | 21 | ] |
|
20 | 22 | |
|
21 | 23 | def _width(self): |
|
22 |
return |
|
|
24 | return fragment_list_width(self.in_prompt_tokens()) | |
|
23 | 25 | |
|
24 |
def continuation_prompt_tokens(self, |
|
|
26 | def continuation_prompt_tokens(self, width=None): | |
|
25 | 27 | if width is None: |
|
26 | 28 | width = self._width() |
|
27 | 29 | return [ |
@@ -42,12 +44,12 class Prompts(object): | |||
|
42 | 44 | ] |
|
43 | 45 | |
|
44 | 46 | class ClassicPrompts(Prompts): |
|
45 |
def in_prompt_tokens(self |
|
|
47 | def in_prompt_tokens(self): | |
|
46 | 48 | return [ |
|
47 | 49 | (Token.Prompt, '>>> '), |
|
48 | 50 | ] |
|
49 | 51 | |
|
50 |
def continuation_prompt_tokens(self, |
|
|
52 | def continuation_prompt_tokens(self, width=None): | |
|
51 | 53 | return [ |
|
52 | 54 | (Token.Prompt, '... ') |
|
53 | 55 | ] |
@@ -73,7 +75,8 class RichPromptDisplayHook(DisplayHook): | |||
|
73 | 75 | # Ask for a newline before multiline output |
|
74 | 76 | self.prompt_end_newline = False |
|
75 | 77 | |
|
76 |
if self.shell.pt_ |
|
|
77 | self.shell.pt_cli.print_tokens(tokens) | |
|
78 | if self.shell.pt_app: | |
|
79 | print_formatted_text( | |
|
80 | PygmentsTokens(tokens), style=self.shell.pt_app.app.style) | |
|
78 | 81 | else: |
|
79 | 82 | sys.stdout.write(prompt_txt) |
@@ -14,8 +14,9 from IPython.core.completer import ( | |||
|
14 | 14 | provisionalcompleter, cursor_to_position, |
|
15 | 15 | _deduplicate_completions) |
|
16 | 16 | from prompt_toolkit.completion import Completer, Completion |
|
17 |
from prompt_toolkit |
|
|
18 |
from prompt_toolkit |
|
|
17 | from prompt_toolkit.lexers import Lexer | |
|
18 | from prompt_toolkit.lexers import PygmentsLexer | |
|
19 | from prompt_toolkit.patch_stdout import patch_stdout | |
|
19 | 20 | |
|
20 | 21 | import pygments.lexers as pygments_lexers |
|
21 | 22 | |
@@ -52,14 +53,11 def _adjust_completion_text_based_on_context(text, body, offset): | |||
|
52 | 53 | |
|
53 | 54 | class IPythonPTCompleter(Completer): |
|
54 | 55 | """Adaptor to provide IPython completions to prompt_toolkit""" |
|
55 |
def __init__(self, ipy_completer=None, shell=None |
|
|
56 | def __init__(self, ipy_completer=None, shell=None): | |
|
56 | 57 | if shell is None and ipy_completer is None: |
|
57 | 58 | raise TypeError("Please pass shell=an InteractiveShell instance.") |
|
58 | 59 | self._ipy_completer = ipy_completer |
|
59 | 60 | self.shell = shell |
|
60 | if patch_stdout is None: | |
|
61 | raise TypeError("Please pass patch_stdout") | |
|
62 | self.patch_stdout = patch_stdout | |
|
63 | 61 | |
|
64 | 62 | @property |
|
65 | 63 | def ipy_completer(self): |
@@ -75,7 +73,7 class IPythonPTCompleter(Completer): | |||
|
75 | 73 | # is imported). This context manager ensures that doesn't interfere with |
|
76 | 74 | # the prompt. |
|
77 | 75 | |
|
78 |
with |
|
|
76 | with patch_stdout(), provisionalcompleter(): | |
|
79 | 77 | body = document.text |
|
80 | 78 | cursor_row = document.cursor_position_row |
|
81 | 79 | cursor_col = document.cursor_position_col |
@@ -143,7 +141,7 class IPythonPTLexer(Lexer): | |||
|
143 | 141 | 'latex': PygmentsLexer(l.TexLexer), |
|
144 | 142 | } |
|
145 | 143 | |
|
146 |
def lex_document(self, |
|
|
144 | def lex_document(self, document): | |
|
147 | 145 | text = document.text.lstrip() |
|
148 | 146 | |
|
149 | 147 | lexer = self.python_lexer |
@@ -157,4 +155,4 class IPythonPTLexer(Lexer): | |||
|
157 | 155 | lexer = l |
|
158 | 156 | break |
|
159 | 157 | |
|
160 |
return lexer.lex_document( |
|
|
158 | return lexer.lex_document(document) |
@@ -12,91 +12,79 import sys | |||
|
12 | 12 | from typing import Callable |
|
13 | 13 | |
|
14 | 14 | |
|
15 | from prompt_toolkit.application.current import get_app | |
|
15 | 16 | from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER |
|
16 |
from prompt_toolkit.filters import ( |
|
|
17 |
|
|
|
18 | from prompt_toolkit.filters.cli import ViMode, ViNavigationMode | |
|
19 | from prompt_toolkit.keys import Keys | |
|
17 | from prompt_toolkit.filters import (has_focus, has_selection, Condition, | |
|
18 | vi_insert_mode, emacs_insert_mode, has_completions, vi_mode) | |
|
20 | 19 | from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline |
|
20 | from prompt_toolkit.key_binding import KeyBindings | |
|
21 | 21 | |
|
22 | 22 | from IPython.utils.decorators import undoc |
|
23 | 23 | |
|
24 | 24 | @undoc |
|
25 | 25 | @Condition |
|
26 |
def cursor_in_leading_ws( |
|
|
27 |
before = |
|
|
26 | def cursor_in_leading_ws(): | |
|
27 | before = get_app().current_buffer.document.current_line_before_cursor | |
|
28 | 28 | return (not before) or before.isspace() |
|
29 | 29 | |
|
30 | def register_ipython_shortcuts(registry, shell): | |
|
30 | ||
|
31 | def create_ipython_shortcuts(shell): | |
|
31 | 32 | """Set up the prompt_toolkit keyboard shortcuts for IPython""" |
|
32 | insert_mode = ViInsertMode() | EmacsInsertMode() | |
|
33 | ||
|
34 | kb = KeyBindings() | |
|
35 | insert_mode = vi_insert_mode | emacs_insert_mode | |
|
33 | 36 | |
|
34 | 37 | if getattr(shell, 'handle_return', None): |
|
35 | 38 | return_handler = shell.handle_return(shell) |
|
36 | 39 | else: |
|
37 | 40 | return_handler = newline_or_execute_outer(shell) |
|
38 | 41 | |
|
39 | # Ctrl+J == Enter, seemingly | |
|
40 | registry.add_binding(Keys.ControlJ, | |
|
41 | filter=(HasFocus(DEFAULT_BUFFER) | |
|
42 | & ~HasSelection() | |
|
43 | & insert_mode | |
|
42 | kb.add('enter', filter=(has_focus(DEFAULT_BUFFER) | |
|
43 | & ~has_selection | |
|
44 | & insert_mode | |
|
44 | 45 | ))(return_handler) |
|
45 | 46 | |
|
46 | registry.add_binding(Keys.ControlBackslash)(force_exit) | |
|
47 | kb.add('c-\\')(force_exit) | |
|
47 | 48 | |
|
48 | registry.add_binding(Keys.ControlP, | |
|
49 | filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER) | |
|
50 | ))(previous_history_or_previous_completion) | |
|
49 | kb.add('c-p', filter=(vi_insert_mode & has_focus(DEFAULT_BUFFER)) | |
|
50 | )(previous_history_or_previous_completion) | |
|
51 | 51 | |
|
52 | registry.add_binding(Keys.ControlN, | |
|
53 | filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER) | |
|
54 | ))(next_history_or_next_completion) | |
|
52 | kb.add('c-n', filter=(vi_insert_mode & has_focus(DEFAULT_BUFFER)) | |
|
53 | )(next_history_or_next_completion) | |
|
55 | 54 | |
|
56 | registry.add_binding(Keys.ControlG, | |
|
57 | filter=(HasFocus(DEFAULT_BUFFER) & HasCompletions() | |
|
58 | ))(dismiss_completion) | |
|
55 | kb.add('c-g', filter=(has_focus(DEFAULT_BUFFER) & has_completions) | |
|
56 | )(dismiss_completion) | |
|
59 | 57 | |
|
60 | registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER) | |
|
61 | )(reset_buffer) | |
|
58 | kb.add('c-c', filter=has_focus(DEFAULT_BUFFER))(reset_buffer) | |
|
62 | 59 | |
|
63 | registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER) | |
|
64 | )(reset_search_buffer) | |
|
60 | kb.add('c-c', filter=has_focus(SEARCH_BUFFER))(reset_search_buffer) | |
|
65 | 61 | |
|
66 |
supports_suspend = Condition(lambda |
|
|
67 | registry.add_binding(Keys.ControlZ, filter=supports_suspend | |
|
68 | )(suspend_to_bg) | |
|
62 | supports_suspend = Condition(lambda: hasattr(signal, 'SIGTSTP')) | |
|
63 | kb.add('c-z', filter=supports_suspend)(suspend_to_bg) | |
|
69 | 64 | |
|
70 | 65 | # Ctrl+I == Tab |
|
71 | registry.add_binding(Keys.ControlI, | |
|
72 | filter=(HasFocus(DEFAULT_BUFFER) | |
|
73 |
|
|
|
74 |
|
|
|
75 | & cursor_in_leading_ws | |
|
66 | kb.add('tab', filter=(has_focus(DEFAULT_BUFFER) | |
|
67 | & ~has_selection | |
|
68 | & insert_mode | |
|
69 | & cursor_in_leading_ws | |
|
76 | 70 | ))(indent_buffer) |
|
77 | 71 | |
|
78 | registry.add_binding(Keys.ControlO, | |
|
79 | filter=(HasFocus(DEFAULT_BUFFER) | |
|
80 | & EmacsInsertMode()))(newline_autoindent_outer(shell.input_splitter)) | |
|
72 | kb.add('c-o', filter=(has_focus(DEFAULT_BUFFER) | |
|
73 | & emacs_insert_mode))(newline_autoindent_outer(shell.input_splitter)) | |
|
81 | 74 | |
|
82 | registry.add_binding(Keys.F2, | |
|
83 | filter=HasFocus(DEFAULT_BUFFER) | |
|
84 | )(open_input_in_editor) | |
|
75 | kb.add('f2', filter=has_focus(DEFAULT_BUFFER))(open_input_in_editor) | |
|
85 | 76 | |
|
86 | 77 | if shell.display_completions == 'readlinelike': |
|
87 | registry.add_binding(Keys.ControlI, | |
|
88 | filter=(HasFocus(DEFAULT_BUFFER) | |
|
89 |
|
|
|
90 |
|
|
|
91 | & ~cursor_in_leading_ws | |
|
92 | ))(display_completions_like_readline) | |
|
78 | kb.add('c-i', filter=(has_focus(DEFAULT_BUFFER) | |
|
79 | & ~has_selection | |
|
80 | & insert_mode | |
|
81 | & ~cursor_in_leading_ws | |
|
82 | ))(display_completions_like_readline) | |
|
93 | 83 | |
|
94 | 84 | if sys.platform == 'win32': |
|
95 | registry.add_binding(Keys.ControlV, | |
|
96 | filter=( | |
|
97 | HasFocus( | |
|
98 | DEFAULT_BUFFER) & ~ViMode() | |
|
99 | ))(win_paste) | |
|
85 | kb.add('c-v', filter=(has_focus(DEFAULT_BUFFER) & ~vi_mode))(win_paste) | |
|
86 | ||
|
87 | return kb | |
|
100 | 88 | |
|
101 | 89 | |
|
102 | 90 | def newline_or_execute_outer(shell): |
@@ -127,8 +115,8 def newline_or_execute_outer(shell): | |||
|
127 | 115 | b.insert_text('\n' + (' ' * (indent or 0))) |
|
128 | 116 | return |
|
129 | 117 | |
|
130 |
if (status != 'incomplete') and b.accept_ |
|
|
131 |
b. |
|
|
118 | if (status != 'incomplete') and b.accept_handler: | |
|
119 | b.validate_and_handle() | |
|
132 | 120 | else: |
|
133 | 121 | b.insert_text('\n' + (' ' * (indent or 0))) |
|
134 | 122 | return newline_or_execute |
@@ -170,10 +158,10 def reset_search_buffer(event): | |||
|
170 | 158 | if event.current_buffer.document.text: |
|
171 | 159 | event.current_buffer.reset() |
|
172 | 160 | else: |
|
173 |
event. |
|
|
161 | event.app.layout.focus(DEFAULT_BUFFER) | |
|
174 | 162 | |
|
175 | 163 | def suspend_to_bg(event): |
|
176 |
event. |
|
|
164 | event.app.suspend_to_background() | |
|
177 | 165 | |
|
178 | 166 | def force_exit(event): |
|
179 | 167 | """ |
@@ -232,8 +220,8 def newline_autoindent_outer(inputsplitter) -> Callable[..., None]: | |||
|
232 | 220 | |
|
233 | 221 | |
|
234 | 222 | def open_input_in_editor(event): |
|
235 |
event. |
|
|
236 |
event. |
|
|
223 | event.app.current_buffer.tempfile_suffix = ".py" | |
|
224 | event.app.current_buffer.open_in_editor() | |
|
237 | 225 | |
|
238 | 226 | |
|
239 | 227 | if sys.platform == 'win32': |
General Comments 0
You need to be logged in to leave comments.
Login now