Skip to content

Commit 08be4c9

Browse files
authored
PR: Unassigned allowed commands (ipython#14564)
From git git: > Modify binding mechanism to allow binding to one of a set of whitelisted commands, Add five such commands ("beginning_of_buffer", "end_of_buffer", "end_of_line", "forward_word", "unix_line_discard"). Currently, only commands which are assigned by default are allowed to be reassigned: ```python known_commands = { create_identifier(binding.command): binding.command for binding in KEY_BINDINGS } ... if command_id not in known_commands: ... raise ValueError( f"{command_id} is not a known shortcut command." f" Allowed commands are: \n - {allowed_commands}" ) ``` This PR adds a List UNASSIGNED_ALLOWED_COMMANDS and updates `known_commands` (now renamed to be more clear as `allowed_commands`): ```python allowed_commands.update({ create_identifier(command): command for command in UNASSIGNED_ALLOWED_COMMANDS }) ``` The five unassigned allowed commands are: ```python UNASSIGNED_ALLOWED_COMMANDS = [ nc.beginning_of_buffer, nc.end_of_buffer, nc.end_of_line, nc.forward_word, nc.unix_line_discard, ] ``` Motivation tldr: Until now, it is not possible to bind eg the common keybindng `ctrl+e` to prompt_toolkit's `end_of_line` (because it is not bound by default). Full motivation: ipython#14369
2 parents a963358 + 42e4bd1 commit 08be4c9

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

IPython/terminal/interactiveshell.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
from .ptutils import IPythonPTCompleter, IPythonPTLexer
5353
from .shortcuts import (
5454
KEY_BINDINGS,
55+
UNASSIGNED_ALLOWED_COMMANDS,
5556
create_ipython_shortcuts,
5657
create_identifier,
5758
RuntimeBinding,
@@ -508,19 +509,25 @@ def _merge_shortcuts(self, user_shortcuts):
508509
# rebuild the bindings list from scratch
509510
key_bindings = create_ipython_shortcuts(self)
510511

511-
# for now we only allow adding shortcuts for commands which are already
512-
# registered; this is a security precaution.
513-
known_commands = {
512+
# for now we only allow adding shortcuts for a specific set of
513+
# commands; this is a security precution.
514+
allowed_commands = {
514515
create_identifier(binding.command): binding.command
515516
for binding in KEY_BINDINGS
516517
}
518+
allowed_commands.update(
519+
{
520+
create_identifier(command): command
521+
for command in UNASSIGNED_ALLOWED_COMMANDS
522+
}
523+
)
517524
shortcuts_to_skip = []
518525
shortcuts_to_add = []
519526

520527
for shortcut in user_shortcuts:
521528
command_id = shortcut["command"]
522-
if command_id not in known_commands:
523-
allowed_commands = "\n - ".join(known_commands)
529+
if command_id not in allowed_commands:
530+
allowed_commands = "\n - ".join(allowed_commands)
524531
raise ValueError(
525532
f"{command_id} is not a known shortcut command."
526533
f" Allowed commands are: \n - {allowed_commands}"
@@ -544,7 +551,7 @@ def _merge_shortcuts(self, user_shortcuts):
544551
new_keys = shortcut.get("new_keys", None)
545552
new_filter = shortcut.get("new_filter", None)
546553

547-
command = known_commands[command_id]
554+
command = allowed_commands[command_id]
548555

549556
creating_new = shortcut.get("create", False)
550557
modifying_existing = not creating_new and (
@@ -586,12 +593,14 @@ def _merge_shortcuts(self, user_shortcuts):
586593
RuntimeBinding(
587594
command,
588595
keys=new_keys or old_keys,
589-
filter=filter_from_string(new_filter)
590-
if new_filter is not None
591-
else (
592-
old_filter
593-
if old_filter is not None
594-
else filter_from_string("always")
596+
filter=(
597+
filter_from_string(new_filter)
598+
if new_filter is not None
599+
else (
600+
old_filter
601+
if old_filter is not None
602+
else filter_from_string("always")
603+
)
595604
),
596605
)
597606
)

IPython/terminal/shortcuts/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,11 @@ def win_paste(event):
628628
*SIMPLE_CONTROL_BINDINGS,
629629
*ALT_AND_COMOBO_CONTROL_BINDINGS,
630630
]
631+
632+
UNASSIGNED_ALLOWED_COMMANDS = [
633+
nc.beginning_of_buffer,
634+
nc.end_of_buffer,
635+
nc.end_of_line,
636+
nc.forward_word,
637+
nc.unix_line_discard,
638+
]

0 commit comments

Comments
 (0)