2018-01-05 16:57:38 +00:00
|
|
|
"""
|
2018-10-15 13:48:49 +01:00
|
|
|
Module to define and register Terminal IPython shortcuts with
|
2018-01-05 16:57:38 +00:00
|
|
|
:mod:`prompt_toolkit`
|
|
|
|
"""
|
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
# Copyright (c) IPython Development Team.
|
2018-01-05 16:57:38 +00:00
|
|
|
# Distributed under the terms of the Modified BSD License.
|
|
|
|
|
|
|
|
import warnings
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
from typing import Callable
|
|
|
|
|
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
from prompt_toolkit.application.current import get_app
|
2018-01-05 16:57:38 +00:00
|
|
|
from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER
|
2018-10-15 13:48:49 +01:00
|
|
|
from prompt_toolkit.filters import (has_focus, has_selection, Condition,
|
|
|
|
vi_insert_mode, emacs_insert_mode, has_completions, vi_mode)
|
2018-01-05 16:57:38 +00:00
|
|
|
from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline
|
2018-10-15 13:48:49 +01:00
|
|
|
from prompt_toolkit.key_binding import KeyBindings
|
2018-01-05 16:57:38 +00:00
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
from IPython.utils.decorators import undoc
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
@undoc
|
|
|
|
@Condition
|
2018-10-15 13:48:49 +01:00
|
|
|
def cursor_in_leading_ws():
|
|
|
|
before = get_app().current_buffer.document.current_line_before_cursor
|
2018-01-05 16:57:38 +00:00
|
|
|
return (not before) or before.isspace()
|
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
|
|
|
|
def create_ipython_shortcuts(shell):
|
|
|
|
"""Set up the prompt_toolkit keyboard shortcuts for IPython"""
|
|
|
|
|
|
|
|
kb = KeyBindings()
|
|
|
|
insert_mode = vi_insert_mode | emacs_insert_mode
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
if getattr(shell, 'handle_return', None):
|
|
|
|
return_handler = shell.handle_return(shell)
|
|
|
|
else:
|
|
|
|
return_handler = newline_or_execute_outer(shell)
|
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('enter', filter=(has_focus(DEFAULT_BUFFER)
|
|
|
|
& ~has_selection
|
|
|
|
& insert_mode
|
2018-01-05 16:57:38 +00:00
|
|
|
))(return_handler)
|
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('c-\\')(force_exit)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('c-p', filter=(vi_insert_mode & has_focus(DEFAULT_BUFFER))
|
|
|
|
)(previous_history_or_previous_completion)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('c-n', filter=(vi_insert_mode & has_focus(DEFAULT_BUFFER))
|
|
|
|
)(next_history_or_next_completion)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('c-g', filter=(has_focus(DEFAULT_BUFFER) & has_completions)
|
|
|
|
)(dismiss_completion)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('c-c', filter=has_focus(DEFAULT_BUFFER))(reset_buffer)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('c-c', filter=has_focus(SEARCH_BUFFER))(reset_search_buffer)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
supports_suspend = Condition(lambda: hasattr(signal, 'SIGTSTP'))
|
|
|
|
kb.add('c-z', filter=supports_suspend)(suspend_to_bg)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
# Ctrl+I == Tab
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('tab', filter=(has_focus(DEFAULT_BUFFER)
|
|
|
|
& ~has_selection
|
|
|
|
& insert_mode
|
|
|
|
& cursor_in_leading_ws
|
2018-01-05 16:57:38 +00:00
|
|
|
))(indent_buffer)
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('c-o', filter=(has_focus(DEFAULT_BUFFER) & emacs_insert_mode)
|
|
|
|
)(newline_autoindent_outer(shell.input_transformer_manager))
|
2018-01-05 16:57:38 +00:00
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('f2', filter=has_focus(DEFAULT_BUFFER))(open_input_in_editor)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
if shell.display_completions == 'readlinelike':
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('c-i', filter=(has_focus(DEFAULT_BUFFER)
|
|
|
|
& ~has_selection
|
|
|
|
& insert_mode
|
|
|
|
& ~cursor_in_leading_ws
|
|
|
|
))(display_completions_like_readline)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
2018-10-15 13:48:49 +01:00
|
|
|
kb.add('c-v', filter=(has_focus(DEFAULT_BUFFER) & ~vi_mode))(win_paste)
|
|
|
|
|
|
|
|
return kb
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def newline_or_execute_outer(shell):
|
|
|
|
def newline_or_execute(event):
|
|
|
|
"""When the user presses return, insert a newline or execute the code."""
|
|
|
|
b = event.current_buffer
|
|
|
|
d = b.document
|
|
|
|
|
|
|
|
if b.complete_state:
|
|
|
|
cc = b.complete_state.current_completion
|
|
|
|
if cc:
|
|
|
|
b.apply_completion(cc)
|
|
|
|
else:
|
|
|
|
b.cancel_completion()
|
|
|
|
return
|
|
|
|
|
|
|
|
# If there's only one line, treat it as if the cursor is at the end.
|
|
|
|
# See https://github.com/ipython/ipython/issues/10425
|
|
|
|
if d.line_count == 1:
|
|
|
|
check_text = d.text
|
|
|
|
else:
|
|
|
|
check_text = d.text[:d.cursor_position]
|
2018-10-15 13:48:49 +01:00
|
|
|
status, indent = shell.check_complete(check_text)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
if not (d.on_last_line or
|
|
|
|
d.cursor_position_row >= d.line_count - d.empty_line_count_at_the_end()
|
|
|
|
):
|
2018-10-15 13:48:49 +01:00
|
|
|
if shell.autoindent:
|
|
|
|
b.insert_text('\n' + indent)
|
|
|
|
else:
|
|
|
|
b.insert_text('\n')
|
2018-01-05 16:57:38 +00:00
|
|
|
return
|
|
|
|
|
2018-10-15 13:48:49 +01:00
|
|
|
if (status != 'incomplete') and b.accept_handler:
|
|
|
|
b.validate_and_handle()
|
2018-01-05 16:57:38 +00:00
|
|
|
else:
|
2018-10-15 13:48:49 +01:00
|
|
|
if shell.autoindent:
|
|
|
|
b.insert_text('\n' + indent)
|
|
|
|
else:
|
|
|
|
b.insert_text('\n')
|
2018-01-05 16:57:38 +00:00
|
|
|
return newline_or_execute
|
|
|
|
|
|
|
|
|
|
|
|
def previous_history_or_previous_completion(event):
|
|
|
|
"""
|
|
|
|
Control-P in vi edit mode on readline is history next, unlike default prompt toolkit.
|
|
|
|
|
|
|
|
If completer is open this still select previous completion.
|
|
|
|
"""
|
|
|
|
event.current_buffer.auto_up()
|
|
|
|
|
|
|
|
|
|
|
|
def next_history_or_next_completion(event):
|
|
|
|
"""
|
|
|
|
Control-N in vi edit mode on readline is history previous, unlike default prompt toolkit.
|
|
|
|
|
|
|
|
If completer is open this still select next completion.
|
|
|
|
"""
|
|
|
|
event.current_buffer.auto_down()
|
|
|
|
|
|
|
|
|
|
|
|
def dismiss_completion(event):
|
|
|
|
b = event.current_buffer
|
|
|
|
if b.complete_state:
|
|
|
|
b.cancel_completion()
|
|
|
|
|
|
|
|
|
|
|
|
def reset_buffer(event):
|
|
|
|
b = event.current_buffer
|
|
|
|
if b.complete_state:
|
|
|
|
b.cancel_completion()
|
|
|
|
else:
|
|
|
|
b.reset()
|
|
|
|
|
|
|
|
|
|
|
|
def reset_search_buffer(event):
|
|
|
|
if event.current_buffer.document.text:
|
|
|
|
event.current_buffer.reset()
|
|
|
|
else:
|
2018-10-15 13:48:49 +01:00
|
|
|
event.app.layout.focus(DEFAULT_BUFFER)
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
def suspend_to_bg(event):
|
2018-10-15 13:48:49 +01:00
|
|
|
event.app.suspend_to_background()
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
def force_exit(event):
|
|
|
|
"""
|
|
|
|
Force exit (with a non-zero return value)
|
|
|
|
"""
|
|
|
|
sys.exit("Quit")
|
|
|
|
|
|
|
|
def indent_buffer(event):
|
|
|
|
event.current_buffer.insert_text(' ' * 4)
|
|
|
|
|
|
|
|
@undoc
|
|
|
|
def newline_with_copy_margin(event):
|
|
|
|
"""
|
2018-10-15 13:48:49 +01:00
|
|
|
DEPRECATED since IPython 6.0
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
See :any:`newline_autoindent_outer` for a replacement.
|
|
|
|
|
|
|
|
Preserve margin and cursor position when using
|
|
|
|
Control-O to insert a newline in EMACS mode
|
|
|
|
"""
|
2018-10-15 13:48:49 +01:00
|
|
|
warnings.warn("`newline_with_copy_margin(event)` is deprecated since IPython 6.0. "
|
2018-01-05 16:57:38 +00:00
|
|
|
"see `newline_autoindent_outer(shell)(event)` for a replacement.",
|
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
|
|
|
|
|
b = event.current_buffer
|
|
|
|
cursor_start_pos = b.document.cursor_position_col
|
|
|
|
b.newline(copy_margin=True)
|
|
|
|
b.cursor_up(count=1)
|
|
|
|
cursor_end_pos = b.document.cursor_position_col
|
|
|
|
if cursor_start_pos != cursor_end_pos:
|
|
|
|
pos_diff = cursor_start_pos - cursor_end_pos
|
|
|
|
b.cursor_right(count=pos_diff)
|
|
|
|
|
|
|
|
def newline_autoindent_outer(inputsplitter) -> Callable[..., None]:
|
|
|
|
"""
|
|
|
|
Return a function suitable for inserting a indented newline after the cursor.
|
|
|
|
|
|
|
|
Fancier version of deprecated ``newline_with_copy_margin`` which should
|
|
|
|
compute the correct indentation of the inserted line. That is to say, indent
|
|
|
|
by 4 extra space after a function definition, class definition, context
|
|
|
|
manager... And dedent by 4 space after ``pass``, ``return``, ``raise ...``.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def newline_autoindent(event):
|
|
|
|
"""insert a newline after the cursor indented appropriately."""
|
|
|
|
b = event.current_buffer
|
|
|
|
d = b.document
|
|
|
|
|
|
|
|
if b.complete_state:
|
|
|
|
b.cancel_completion()
|
|
|
|
text = d.text[:d.cursor_position] + '\n'
|
|
|
|
_, indent = inputsplitter.check_complete(text)
|
|
|
|
b.insert_text('\n' + (' ' * (indent or 0)), move_cursor=False)
|
|
|
|
|
|
|
|
return newline_autoindent
|
|
|
|
|
|
|
|
|
|
|
|
def open_input_in_editor(event):
|
2018-10-15 13:48:49 +01:00
|
|
|
event.app.current_buffer.tempfile_suffix = ".py"
|
|
|
|
event.app.current_buffer.open_in_editor()
|
2018-01-05 16:57:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
if sys.platform == 'win32':
|
2018-10-15 13:48:49 +01:00
|
|
|
from IPython.core.error import TryNext
|
|
|
|
from IPython.lib.clipboard import (ClipboardEmpty,
|
2018-01-05 16:57:38 +00:00
|
|
|
win32_clipboard_get,
|
|
|
|
tkinter_clipboard_get)
|
|
|
|
|
|
|
|
@undoc
|
|
|
|
def win_paste(event):
|
|
|
|
try:
|
|
|
|
text = win32_clipboard_get()
|
|
|
|
except TryNext:
|
|
|
|
try:
|
|
|
|
text = tkinter_clipboard_get()
|
|
|
|
except (TryNext, ClipboardEmpty):
|
|
|
|
return
|
|
|
|
except ClipboardEmpty:
|
|
|
|
return
|
|
|
|
event.current_buffer.insert_text(text.replace('\t', ' ' * 4))
|