Set up the prompt_toolkit keyboard shortcuts for IPython
(shell)
| 29 | |
| 30 | |
| 31 | def create_ipython_shortcuts(shell): |
| 32 | """Set up the prompt_toolkit keyboard shortcuts for IPython""" |
| 33 | |
| 34 | kb = KeyBindings() |
| 35 | insert_mode = vi_insert_mode | emacs_insert_mode |
| 36 | |
| 37 | if getattr(shell, 'handle_return', None): |
| 38 | return_handler = shell.handle_return(shell) |
| 39 | else: |
| 40 | return_handler = newline_or_execute_outer(shell) |
| 41 | |
| 42 | kb.add('enter', filter=(has_focus(DEFAULT_BUFFER) |
| 43 | & ~has_selection |
| 44 | & insert_mode |
| 45 | ))(return_handler) |
| 46 | |
| 47 | def reformat_and_execute(event): |
| 48 | reformat_text_before_cursor(event.current_buffer, event.current_buffer.document, shell) |
| 49 | event.current_buffer.validate_and_handle() |
| 50 | |
| 51 | kb.add('escape', 'enter', filter=(has_focus(DEFAULT_BUFFER) |
| 52 | & ~has_selection |
| 53 | & insert_mode |
| 54 | ))(reformat_and_execute) |
| 55 | |
| 56 | kb.add('c-\\')(force_exit) |
| 57 | |
| 58 | kb.add('c-p', filter=(vi_insert_mode & has_focus(DEFAULT_BUFFER)) |
| 59 | )(previous_history_or_previous_completion) |
| 60 | |
| 61 | kb.add('c-n', filter=(vi_insert_mode & has_focus(DEFAULT_BUFFER)) |
| 62 | )(next_history_or_next_completion) |
| 63 | |
| 64 | kb.add('c-g', filter=(has_focus(DEFAULT_BUFFER) & has_completions) |
| 65 | )(dismiss_completion) |
| 66 | |
| 67 | kb.add('c-c', filter=has_focus(DEFAULT_BUFFER))(reset_buffer) |
| 68 | |
| 69 | kb.add('c-c', filter=has_focus(SEARCH_BUFFER))(reset_search_buffer) |
| 70 | |
| 71 | supports_suspend = Condition(lambda: hasattr(signal, 'SIGTSTP')) |
| 72 | kb.add('c-z', filter=supports_suspend)(suspend_to_bg) |
| 73 | |
| 74 | # Ctrl+I == Tab |
| 75 | kb.add('tab', filter=(has_focus(DEFAULT_BUFFER) |
| 76 | & ~has_selection |
| 77 | & insert_mode |
| 78 | & cursor_in_leading_ws |
| 79 | ))(indent_buffer) |
| 80 | kb.add('c-o', filter=(has_focus(DEFAULT_BUFFER) & emacs_insert_mode) |
| 81 | )(newline_autoindent_outer(shell.input_transformer_manager)) |
| 82 | |
| 83 | kb.add('f2', filter=has_focus(DEFAULT_BUFFER))(open_input_in_editor) |
| 84 | |
| 85 | if shell.display_completions == 'readlinelike': |
| 86 | kb.add('c-i', filter=(has_focus(DEFAULT_BUFFER) |
| 87 | & ~has_selection |
| 88 | & insert_mode |
no test coverage detected