##// END OF EJS Templates
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").
Hacker-Dom -
Show More
@@ -52,6 +52,7 b' from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook'
52 from .ptutils import IPythonPTCompleter, IPythonPTLexer
52 from .ptutils import IPythonPTCompleter, IPythonPTLexer
53 from .shortcuts import (
53 from .shortcuts import (
54 KEY_BINDINGS,
54 KEY_BINDINGS,
55 UNASSIGNED_ALLOWED_COMMANDS,
55 create_ipython_shortcuts,
56 create_ipython_shortcuts,
56 create_identifier,
57 create_identifier,
57 RuntimeBinding,
58 RuntimeBinding,
@@ -508,19 +509,23 b' class TerminalInteractiveShell(InteractiveShell):'
508 # rebuild the bindings list from scratch
509 # rebuild the bindings list from scratch
509 key_bindings = create_ipython_shortcuts(self)
510 key_bindings = create_ipython_shortcuts(self)
510
511
511 # for now we only allow adding shortcuts for commands which are already
512 # for now we only allow adding shortcuts for a specific set of
512 # registered; this is a security precaution.
513 # commands; this is a security precution.
513 known_commands = {
514 allowed_commands = {
514 create_identifier(binding.command): binding.command
515 create_identifier(binding.command): binding.command
515 for binding in KEY_BINDINGS
516 for binding in KEY_BINDINGS
516 }
517 }
518 allowed_commands.update({
519 create_identifier(command): command
520 for command in UNASSIGNED_ALLOWED_COMMANDS
521 })
517 shortcuts_to_skip = []
522 shortcuts_to_skip = []
518 shortcuts_to_add = []
523 shortcuts_to_add = []
519
524
520 for shortcut in user_shortcuts:
525 for shortcut in user_shortcuts:
521 command_id = shortcut["command"]
526 command_id = shortcut["command"]
522 if command_id not in known_commands:
527 if command_id not in allowed_commands:
523 allowed_commands = "\n - ".join(known_commands)
528 allowed_commands = "\n - ".join(allowed_commands)
524 raise ValueError(
529 raise ValueError(
525 f"{command_id} is not a known shortcut command."
530 f"{command_id} is not a known shortcut command."
526 f" Allowed commands are: \n - {allowed_commands}"
531 f" Allowed commands are: \n - {allowed_commands}"
@@ -544,7 +549,7 b' class TerminalInteractiveShell(InteractiveShell):'
544 new_keys = shortcut.get("new_keys", None)
549 new_keys = shortcut.get("new_keys", None)
545 new_filter = shortcut.get("new_filter", None)
550 new_filter = shortcut.get("new_filter", None)
546
551
547 command = known_commands[command_id]
552 command = allowed_commands[command_id]
548
553
549 creating_new = shortcut.get("create", False)
554 creating_new = shortcut.get("create", False)
550 modifying_existing = not creating_new and (
555 modifying_existing = not creating_new and (
@@ -628,3 +628,11 b' KEY_BINDINGS = ['
628 *SIMPLE_CONTROL_BINDINGS,
628 *SIMPLE_CONTROL_BINDINGS,
629 *ALT_AND_COMOBO_CONTROL_BINDINGS,
629 *ALT_AND_COMOBO_CONTROL_BINDINGS,
630 ]
630 ]
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 ]
General Comments 0
You need to be logged in to leave comments. Login now