##// END OF EJS Templates
Merge pull request #11006 from Carreau/fix-ipython-spelling...
Merge pull request #11006 from Carreau/fix-ipython-spelling Fix IPython spelling

File last commit:

r23582:bbb0bc29 merge
r24144:cc353b25 merge
Show More
shortcuts.py
256 lines | 8.4 KiB | text/x-python | PythonLexer
Matthias Bussonnier
Improve indentation of next line when using CTRL-o in terminal....
r23334 """
Module to define and register Terminal IPython shortcuts with
Thomas Kluyver
Fix a couple of rst roles causing Sphinx warnings
r23428 :mod:`prompt_toolkit`
Matthias Bussonnier
Improve indentation of next line when using CTRL-o in terminal....
r23334 """
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import warnings
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642 import signal
import sys
Matthias Bussonnier
Improve indentation of next line when using CTRL-o in terminal....
r23334 from typing import Callable
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642
from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER
from prompt_toolkit.filters import (HasFocus, HasSelection, Condition,
ViInsertMode, EmacsInsertMode, HasCompletions)
Antony Lee
Add 'v' binding in vi-navigation-mode to %edit.
r23282 from prompt_toolkit.filters.cli import ViMode, ViNavigationMode
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642 from prompt_toolkit.keys import Keys
from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline
Fernando Perez
Tag windows-only function so it doesn't get picked up on *nix doc builds.
r22670 from IPython.utils.decorators import undoc
Matthias Bussonnier
rst fixes
r23465 @undoc
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642 @Condition
def cursor_in_leading_ws(cli):
before = cli.application.buffer.document.current_line_before_cursor
return (not before) or before.isspace()
def register_ipython_shortcuts(registry, shell):
"""Set up the prompt_toolkit keyboard shortcuts for IPython"""
insert_mode = ViInsertMode() | EmacsInsertMode()
Thomas Kluyver
Allow configuring a function to handle return in the terminal...
r23580 if getattr(shell, 'handle_return', None):
return_handler = shell.handle_return(shell)
else:
return_handler = newline_or_execute_outer(shell)
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642 # Ctrl+J == Enter, seemingly
registry.add_binding(Keys.ControlJ,
filter=(HasFocus(DEFAULT_BUFFER)
& ~HasSelection()
& insert_mode
Thomas Kluyver
Allow configuring a function to handle return in the terminal...
r23580 ))(return_handler)
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642
Matthias Bussonnier
Quit IPython on Ctrl-\ (SIGQUIT)...
r22739 registry.add_binding(Keys.ControlBackslash)(force_exit)
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642 registry.add_binding(Keys.ControlP,
filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER)
))(previous_history_or_previous_completion)
registry.add_binding(Keys.ControlN,
filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER)
))(next_history_or_next_completion)
registry.add_binding(Keys.ControlG,
filter=(HasFocus(DEFAULT_BUFFER) & HasCompletions()
))(dismiss_completion)
registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER)
)(reset_buffer)
registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER)
)(reset_search_buffer)
supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP'))
registry.add_binding(Keys.ControlZ, filter=supports_suspend
)(suspend_to_bg)
# Ctrl+I == Tab
registry.add_binding(Keys.ControlI,
filter=(HasFocus(DEFAULT_BUFFER)
& ~HasSelection()
& insert_mode
& cursor_in_leading_ws
))(indent_buffer)
Gil Forsyth
preserve margins when using Ctrl-O for newline...
r22695 registry.add_binding(Keys.ControlO,
filter=(HasFocus(DEFAULT_BUFFER)
Matthias Bussonnier
Improve indentation of next line when using CTRL-o in terminal....
r23334 & EmacsInsertMode()))(newline_autoindent_outer(shell.input_splitter))
Gil Forsyth
preserve margins when using Ctrl-O for newline...
r22695
Carl Smith
Add shortcut to edit cell
r22851 registry.add_binding(Keys.F2,
filter=HasFocus(DEFAULT_BUFFER)
)(open_input_in_editor)
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642 if shell.display_completions == 'readlinelike':
registry.add_binding(Keys.ControlI,
filter=(HasFocus(DEFAULT_BUFFER)
& ~HasSelection()
& insert_mode
& ~cursor_in_leading_ws
))(display_completions_like_readline)
if sys.platform == 'win32':
registry.add_binding(Keys.ControlV,
filter=(
HasFocus(
DEFAULT_BUFFER) & ~ViMode()
))(win_paste)
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
Thomas Kluyver
Prefer execution when there's only a single line entered...
r23577 # 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]
status, indent = shell.input_splitter.check_complete(check_text + '\n')
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642
Matthias Bussonnier
Indent on new line by looking at the text before the cursor....
r23323 if not (d.on_last_line or
d.cursor_position_row >= d.line_count - d.empty_line_count_at_the_end()
):
b.insert_text('\n' + (' ' * (indent or 0)))
return
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642
if (status != 'incomplete') and b.accept_action.is_returnable:
b.accept_action.validate_and_handle(event.cli, b)
else:
b.insert_text('\n' + (' ' * (indent or 0)))
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:
event.cli.push_focus(DEFAULT_BUFFER)
def suspend_to_bg(event):
event.cli.suspend_to_background()
Matthias Bussonnier
Quit IPython on Ctrl-\ (SIGQUIT)...
r22739 def force_exit(event):
"""
Force exit (with a non-zero return value)
"""
sys.exit("Quit")
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642 def indent_buffer(event):
event.current_buffer.insert_text(' ' * 4)
Matthias Bussonnier
Improve indentation of next line when using CTRL-o in terminal....
r23334 @undoc
Gil Forsyth
preserve margins when using Ctrl-O for newline...
r22695 def newline_with_copy_margin(event):
"""
Matthias Bussonnier
Improve indentation of next line when using CTRL-o in terminal....
r23334 DEPRECATED since IPython 6.0
See :any:`newline_autoindent_outer` for a replacement.
Gil Forsyth
preserve margins when using Ctrl-O for newline...
r22695 Preserve margin and cursor position when using
Control-O to insert a newline in EMACS mode
"""
Matthias Bussonnier
Improve indentation of next line when using CTRL-o in terminal....
r23334 warnings.warn("`newline_with_copy_margin(event)` is deprecated since IPython 6.0. "
"see `newline_autoindent_outer(shell)(event)` for a replacement.",
DeprecationWarning, stacklevel=2)
Gil Forsyth
preserve margins when using Ctrl-O for newline...
r22695 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)
Matthias Bussonnier
Improve indentation of next line when using CTRL-o in terminal....
r23334 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
Carl Smith
Add shortcut to edit cell
r22851 def open_input_in_editor(event):
event.cli.current_buffer.tempfile_suffix = ".py"
event.cli.current_buffer.open_in_editor(event.cli)
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642
if sys.platform == 'win32':
from IPython.core.error import TryNext
from IPython.lib.clipboard import (ClipboardEmpty,
win32_clipboard_get,
tkinter_clipboard_get)
Fernando Perez
Tag windows-only function so it doesn't get picked up on *nix doc builds.
r22670 @undoc
Thomas Kluyver
Pull shortcut definitions out to a separate module
r22642 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))