Format: text/x-python
⬇ Download File (41.0 KB)
   1  """IPython terminal interface using prompt_toolkit"""
   2  
   3  import os
   4  import sys
   5  import inspect
   6  from warnings import warn
   7  from typing import Union as UnionType, Optional
   8  
   9  from IPython.core.async_helpers import get_asyncio_loop
  10  from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
  11  from IPython.utils.py3compat import input
  12  from IPython.utils.terminal import toggle_set_term_title, set_term_title, restore_term_title
  13  from IPython.utils.process import abbrev_cwd
  14  from traitlets import (
  15      Bool,
  16      Unicode,
  17      Dict,
  18      Integer,
  19      List,
  20      observe,
  21      Instance,
  22      Type,
  23      default,
  24      Enum,
  25      Union,
  26      Any,
  27      validate,
  28      Float,
  29      DottedObjectName,
  30  )
  31  from traitlets.utils.importstring import import_item
  32  
  33  
  34  from prompt_toolkit.auto_suggest import AutoSuggestFromHistory
  35  from prompt_toolkit.enums import DEFAULT_BUFFER, EditingMode
  36  from prompt_toolkit.filters import HasFocus, Condition, IsDone
  37  from prompt_toolkit.formatted_text import PygmentsTokens
  38  from prompt_toolkit.history import History
  39  from prompt_toolkit.layout.processors import ConditionalProcessor, HighlightMatchingBracketProcessor
  40  from prompt_toolkit.output import ColorDepth
  41  from prompt_toolkit.patch_stdout import patch_stdout
  42  from prompt_toolkit.shortcuts import PromptSession, CompleteStyle, print_formatted_text
  43  from prompt_toolkit.styles import DynamicStyle, merge_styles
  44  from prompt_toolkit.styles.pygments import style_from_pygments_cls, style_from_pygments_dict
  45  from prompt_toolkit import __version__ as ptk_version
  46  
  47  from pygments.styles import get_style_by_name
  48  from pygments.style import Style
  49  from pygments.token import Token
  50  
  51  from .debugger import TerminalPdb, Pdb
  52  from .magics import TerminalMagics
  53  from .pt_inputhooks import get_inputhook_name_and_func
  54  from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook
  55  from .ptutils import IPythonPTCompleter, IPythonPTLexer
  56  from .shortcuts import (
  57      KEY_BINDINGS,
  58      UNASSIGNED_ALLOWED_COMMANDS,
  59      create_ipython_shortcuts,
  60      create_identifier,
  61      RuntimeBinding,
  62      add_binding,
  63  )
  64  from .shortcuts.filters import KEYBINDING_FILTERS, filter_from_string
  65  from .shortcuts.auto_suggest import (
  66      NavigableAutoSuggestFromHistory,
  67      AppendAutoSuggestionInAnyLine,
  68  )
  69  
  70  PTK3 = ptk_version.startswith('3.')
  71  
  72  
  73  class _NoStyle(Style):
  74      pass
  75  
  76  
  77  _style_overrides_light_bg = {
  78              Token.Prompt: '#ansibrightblue',
  79              Token.PromptNum: '#ansiblue bold',
  80              Token.OutPrompt: '#ansibrightred',
  81              Token.OutPromptNum: '#ansired bold',
  82  }
  83  
  84  _style_overrides_linux = {
  85              Token.Prompt: '#ansibrightgreen',
  86              Token.PromptNum: '#ansigreen bold',
  87              Token.OutPrompt: '#ansibrightred',
  88              Token.OutPromptNum: '#ansired bold',
  89  }
  90  
  91  
  92  def _backward_compat_continuation_prompt_tokens(method, width: int, *, lineno: int):
  93      """
  94      Sagemath use custom prompt and we broke them in 8.19.
  95      """
  96      sig = inspect.signature(method)
  97      if "lineno" in inspect.signature(method).parameters or any(
  98          [p.kind == p.VAR_KEYWORD for p in sig.parameters.values()]
  99      ):
 100          return method(width, lineno=lineno)
 101      else:
 102          return method(width)
 103  
 104  
 105  def get_default_editor():
 106      try:
 107          return os.environ['EDITOR']
 108      except KeyError:
 109          pass
 110      except UnicodeError:
 111          warn("$EDITOR environment variable is not pure ASCII. Using platform "
 112               "default editor.")
 113  
 114      if os.name == 'posix':
 115          return 'vi'  # the only one guaranteed to be there!
 116      else:
 117          return "notepad"  # same in Windows!
 118  
 119  
 120  # conservatively check for tty
 121  # overridden streams can result in things like:
 122  # - sys.stdin = None
 123  # - no isatty method
 124  for _name in ('stdin', 'stdout', 'stderr'):
 125      _stream = getattr(sys, _name)
 126      try:
 127          if not _stream or not hasattr(_stream, "isatty") or not _stream.isatty():
 128              _is_tty = False
 129              break
 130      except ValueError:
 131          # stream is closed
 132          _is_tty = False
 133          break
 134  else:
 135      _is_tty = True
 136  
 137  
 138  _use_simple_prompt = ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or (not _is_tty)
 139  
 140  def black_reformat_handler(text_before_cursor):
 141      """
 142      We do not need to protect against error,
 143      this is taken care at a higher level where any reformat error is ignored.
 144      Indeed we may call reformatting on incomplete code.
 145      """
 146      import black
 147  
 148      formatted_text = black.format_str(text_before_cursor, mode=black.FileMode())
 149      if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"):
 150          formatted_text = formatted_text[:-1]
 151      return formatted_text
 152  
 153  
 154  def yapf_reformat_handler(text_before_cursor):
 155      from yapf.yapflib import file_resources
 156      from yapf.yapflib import yapf_api
 157  
 158      style_config = file_resources.GetDefaultStyleForDir(os.getcwd())
 159      formatted_text, was_formatted = yapf_api.FormatCode(
 160          text_before_cursor, style_config=style_config
 161      )
 162      if was_formatted:
 163          if not text_before_cursor.endswith("\n") and formatted_text.endswith("\n"):
 164              formatted_text = formatted_text[:-1]
 165          return formatted_text
 166      else:
 167          return text_before_cursor
 168  
 169  
 170  class PtkHistoryAdapter(History):
 171      """
 172      Prompt toolkit has it's own way of handling history, Where it assumes it can
 173      Push/pull from history.
 174  
 175      """
 176  
 177      def __init__(self, shell):
 178          super().__init__()
 179          self.shell = shell
 180          self._refresh()
 181  
 182      def append_string(self, string):
 183          # we rely on sql for that.
 184          self._loaded = False
 185          self._refresh()
 186  
 187      def _refresh(self):
 188          if not self._loaded:
 189              self._loaded_strings = list(self.load_history_strings())
 190  
 191      def load_history_strings(self):
 192          last_cell = ""
 193          res = []
 194          for __, ___, cell in self.shell.history_manager.get_tail(
 195              self.shell.history_load_length, include_latest=True
 196          ):
 197              # Ignore blank lines and consecutive duplicates
 198              cell = cell.rstrip()
 199              if cell and (cell != last_cell):
 200                  res.append(cell)
 201                  last_cell = cell
 202          yield from res[::-1]
 203  
 204      def store_string(self, string: str) -> None:
 205          pass
 206  
 207  class TerminalInteractiveShell(InteractiveShell):
 208      mime_renderers = Dict().tag(config=True)
 209  
 210      space_for_menu = Integer(6, help='Number of line at the bottom of the screen '
 211                                       'to reserve for the tab completion menu, '
 212                                       'search history, ...etc, the height of '
 213                                       'these menus will at most this value. '
 214                                       'Increase it is you prefer long and skinny '
 215                                       'menus, decrease for short and wide.'
 216                              ).tag(config=True)
 217  
 218      pt_app: UnionType[PromptSession, None] = None
 219      auto_suggest: UnionType[
 220          AutoSuggestFromHistory,
 221          NavigableAutoSuggestFromHistory,
 222          None,
 223      ] = None
 224      debugger_history = None
 225  
 226      debugger_history_file = Unicode(
 227          "~/.pdbhistory", help="File in which to store and read history"
 228      ).tag(config=True)
 229  
 230      simple_prompt = Bool(_use_simple_prompt,
 231          help="""Use `raw_input` for the REPL, without completion and prompt colors.
 232  
 233              Useful when controlling IPython as a subprocess, and piping
 234              STDIN/OUT/ERR. Known usage are: IPython's own testing machinery,
 235              and emacs' inferior-python subprocess (assuming you have set
 236              `python-shell-interpreter` to "ipython") available through the
 237              built-in `M-x run-python` and third party packages such as elpy.
 238  
 239              This mode default to `True` if the `IPY_TEST_SIMPLE_PROMPT`
 240              environment variable is set, or the current terminal is not a tty.
 241              Thus the Default value reported in --help-all, or config will often
 242              be incorrectly reported.
 243              """,
 244      ).tag(config=True)
 245  
 246      @property
 247      def debugger_cls(self):
 248          return Pdb if self.simple_prompt else TerminalPdb
 249  
 250      confirm_exit = Bool(True,
 251          help="""
 252          Set to confirm when you try to exit IPython with an EOF (Control-D
 253          in Unix, Control-Z/Enter in Windows). By typing 'exit' or 'quit',
 254          you can force a direct exit without any confirmation.""",
 255      ).tag(config=True)
 256  
 257      editing_mode = Unicode('emacs',
 258          help="Shortcut style to use at the prompt. 'vi' or 'emacs'.",
 259      ).tag(config=True)
 260  
 261      emacs_bindings_in_vi_insert_mode = Bool(
 262          True,
 263          help="Add shortcuts from 'emacs' insert mode to 'vi' insert mode.",
 264      ).tag(config=True)
 265  
 266      modal_cursor = Bool(
 267          True,
 268          help="""
 269         Cursor shape changes depending on vi mode: beam in vi insert mode,
 270         block in nav mode, underscore in replace mode.""",
 271      ).tag(config=True)
 272  
 273      ttimeoutlen = Float(
 274          0.01,
 275          help="""The time in milliseconds that is waited for a key code
 276         to complete.""",
 277      ).tag(config=True)
 278  
 279      timeoutlen = Float(
 280          0.5,
 281          help="""The time in milliseconds that is waited for a mapped key
 282         sequence to complete.""",
 283      ).tag(config=True)
 284  
 285      autoformatter = Unicode(
 286          None,
 287          help="Autoformatter to reformat Terminal code. Can be `'black'`, `'yapf'` or `None`",
 288          allow_none=True
 289      ).tag(config=True)
 290  
 291      auto_match = Bool(
 292          False,
 293          help="""
 294          Automatically add/delete closing bracket or quote when opening bracket or quote is entered/deleted.
 295          Brackets: (), [], {}
 296          Quotes: '', \"\"
 297          """,
 298      ).tag(config=True)
 299  
 300      mouse_support = Bool(False,
 301          help="Enable mouse support in the prompt\n(Note: prevents selecting text with the mouse)"
 302      ).tag(config=True)
 303  
 304      # We don't load the list of styles for the help string, because loading
 305      # Pygments plugins takes time and can cause unexpected errors.
 306      highlighting_style = Union([Unicode('legacy'), Type(klass=Style)],
 307          help="""The name or class of a Pygments style to use for syntax
 308          highlighting. To see available styles, run `pygmentize -L styles`."""
 309      ).tag(config=True)
 310  
 311      @validate('editing_mode')
 312      def _validate_editing_mode(self, proposal):
 313          if proposal['value'].lower() == 'vim':
 314              proposal['value']= 'vi'
 315          elif proposal['value'].lower() == 'default':
 316              proposal['value']= 'emacs'
 317  
 318          if hasattr(EditingMode, proposal['value'].upper()):
 319              return proposal['value'].lower()
 320  
 321          return self.editing_mode
 322  
 323      @observe('editing_mode')
 324      def _editing_mode(self, change):
 325          if self.pt_app:
 326              self.pt_app.editing_mode = getattr(EditingMode, change.new.upper())
 327  
 328      def _set_formatter(self, formatter):
 329          if formatter is None:
 330              self.reformat_handler = lambda x:x
 331          elif formatter == 'black':
 332              self.reformat_handler = black_reformat_handler
 333          elif formatter == "yapf":
 334              self.reformat_handler = yapf_reformat_handler
 335          else:
 336              raise ValueError
 337  
 338      @observe("autoformatter")
 339      def _autoformatter_changed(self, change):
 340          formatter = change.new
 341          self._set_formatter(formatter)
 342  
 343      @observe('highlighting_style')
 344      @observe('colors')
 345      def _highlighting_style_changed(self, change):
 346          self.refresh_style()
 347  
 348      def refresh_style(self):
 349          self._style = self._make_style_from_name_or_cls(self.highlighting_style)
 350  
 351      highlighting_style_overrides = Dict(
 352          help="Override highlighting format for specific tokens"
 353      ).tag(config=True)
 354  
 355      true_color = Bool(False,
 356          help="""Use 24bit colors instead of 256 colors in prompt highlighting.
 357          If your terminal supports true color, the following command should
 358          print ``TRUECOLOR`` in orange::
 359  
 360              printf \"\\x1b[38;2;255;100;0mTRUECOLOR\\x1b[0m\\n\"
 361          """,
 362      ).tag(config=True)
 363  
 364      editor = Unicode(get_default_editor(),
 365          help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
 366      ).tag(config=True)
 367  
 368      prompts_class = Type(Prompts, help='Class used to generate Prompt token for prompt_toolkit').tag(config=True)
 369  
 370      prompts = Instance(Prompts)
 371  
 372      @default('prompts')
 373      def _prompts_default(self):
 374          return self.prompts_class(self)
 375  
 376  #    @observe('prompts')
 377  #    def _(self, change):
 378  #        self._update_layout()
 379  
 380      @default('displayhook_class')
 381      def _displayhook_class_default(self):
 382          return RichPromptDisplayHook
 383  
 384      term_title = Bool(True,
 385          help="Automatically set the terminal title"
 386      ).tag(config=True)
 387  
 388      term_title_format = Unicode("IPython: {cwd}",
 389          help="Customize the terminal title format.  This is a python format string. " +
 390               "Available substitutions are: {cwd}."
 391      ).tag(config=True)
 392  
 393      display_completions = Enum(('column', 'multicolumn','readlinelike'),
 394          help= ( "Options for displaying tab completions, 'column', 'multicolumn', and "
 395                  "'readlinelike'. These options are for `prompt_toolkit`, see "
 396                  "`prompt_toolkit` documentation for more information."
 397                  ),
 398          default_value='multicolumn').tag(config=True)
 399  
 400      highlight_matching_brackets = Bool(True,
 401          help="Highlight matching brackets.",
 402      ).tag(config=True)
 403  
 404      extra_open_editor_shortcuts = Bool(False,
 405          help="Enable vi (v) or Emacs (C-X C-E) shortcuts to open an external editor. "
 406               "This is in addition to the F2 binding, which is always enabled."
 407      ).tag(config=True)
 408  
 409      handle_return = Any(None,
 410          help="Provide an alternative handler to be called when the user presses "
 411               "Return. This is an advanced option intended for debugging, which "
 412               "may be changed or removed in later releases."
 413      ).tag(config=True)
 414  
 415      enable_history_search = Bool(True,
 416          help="Allows to enable/disable the prompt toolkit history search"
 417      ).tag(config=True)
 418  
 419      autosuggestions_provider = Unicode(
 420          "NavigableAutoSuggestFromHistory",
 421          help="Specifies from which source automatic suggestions are provided. "
 422          "Can be set to ``'NavigableAutoSuggestFromHistory'`` (:kbd:`up` and "
 423          ":kbd:`down` swap suggestions), ``'AutoSuggestFromHistory'``, "
 424          " or ``None`` to disable automatic suggestions. "
 425          "Default is `'NavigableAutoSuggestFromHistory`'.",
 426          allow_none=True,
 427      ).tag(config=True)
 428      _autosuggestions_provider: Any
 429  
 430      llm_constructor_kwargs = Dict(
 431          {},
 432          help="""
 433          Extra arguments to pass to `llm_provider_class` constructor.
 434  
 435          This is used to – for example – set the `model_id`""",
 436      ).tag(config=True)
 437  
 438      llm_prefix_from_history = DottedObjectName(
 439          "input_history",
 440          help="""\
 441      Fully Qualifed name of a function that takes an IPython history manager and
 442      return a prefix to pass the llm provider in addition to the current buffer
 443      text.
 444  
 445      You can use:
 446  
 447       - no_prefix
 448       - input_history
 449  
 450      As default value. `input_history` (default),  will use all the input history
 451      of current IPython session
 452  
 453      """,
 454      ).tag(config=True)
 455      _llm_prefix_from_history: Any
 456  
 457      @observe("llm_prefix_from_history")
 458      def _llm_prefix_from_history_changed(self, change):
 459          name = change.new
 460          self._llm_prefix_from_history = name
 461          self._set_autosuggestions()
 462  
 463      llm_provider_class = DottedObjectName(
 464          None,
 465          allow_none=True,
 466          help="""\
 467          Provisional:
 468              This is a provisinal API in IPython 8.32, before stabilisation
 469              in 9.0, it may change without warnings.
 470  
 471          class to use for the `NavigableAutoSuggestFromHistory` to request
 472          completions from a LLM, this should inherit from
 473          `jupyter_ai_magics:BaseProvider` and implement
 474          `stream_inline_completions`
 475      """,
 476      ).tag(config=True)
 477      _llm_provider_class: Any = None
 478  
 479      @observe("llm_provider_class")
 480      def _llm_provider_class_changed(self, change):
 481          provider_class = change.new
 482          if provider_class is not None:
 483              warn(
 484                  "TerminalInteractiveShell.llm_provider_class is a provisional"
 485                  "  API as of IPython 8.32, and may change without warnings."
 486              )
 487          self._llm_provider_class = provider_class
 488          self._set_autosuggestions()
 489  
 490      def _set_autosuggestions(self, provider=None):
 491          if provider is None:
 492              provider = self.autosuggestions_provider
 493          # disconnect old handler
 494          if self.auto_suggest and isinstance(
 495              self.auto_suggest, NavigableAutoSuggestFromHistory
 496          ):
 497              self.auto_suggest.disconnect()
 498          if provider is None:
 499              self.auto_suggest = None
 500          elif provider == "AutoSuggestFromHistory":
 501              self.auto_suggest = AutoSuggestFromHistory()
 502          elif provider == "NavigableAutoSuggestFromHistory":
 503              # LLM stuff are all Provisional in 8.32
 504              if self._llm_provider_class:
 505                  llm_provider_constructor = import_item(self._llm_provider_class)
 506                  llm_provider = llm_provider_constructor(**self.llm_constructor_kwargs)
 507              else:
 508                  llm_provider = None
 509              self.auto_suggest = NavigableAutoSuggestFromHistory()
 510              # Provisinal in 8.32
 511              self.auto_suggest._llm_provider = llm_provider
 512  
 513              name = self.llm_prefix_from_history
 514  
 515              if name == "no_prefix":
 516  
 517                  def no_prefix(history_manager):
 518                      return ""
 519  
 520                  fun = no_prefix
 521  
 522              elif name == "input_history":
 523  
 524                  def input_history(history_manager):
 525                      return "\n".join([s[2] for s in history_manager.get_range()]) + "\n"
 526  
 527                  fun = input_history
 528  
 529              else:
 530                  fun = import_item(name)
 531              self.auto_suggest._llm_prefixer = fun
 532          else:
 533              raise ValueError("No valid provider.")
 534          if self.pt_app:
 535              self.pt_app.auto_suggest = self.auto_suggest
 536  
 537      @observe("autosuggestions_provider")
 538      def _autosuggestions_provider_changed(self, change):
 539          provider = change.new
 540          self._set_autosuggestions(provider)
 541  
 542      shortcuts = List(
 543          trait=Dict(
 544              key_trait=Enum(
 545                  [
 546                      "command",
 547                      "match_keys",
 548                      "match_filter",
 549                      "new_keys",
 550                      "new_filter",
 551                      "create",
 552                  ]
 553              ),
 554              per_key_traits={
 555                  "command": Unicode(),
 556                  "match_keys": List(Unicode()),
 557                  "match_filter": Unicode(),
 558                  "new_keys": List(Unicode()),
 559                  "new_filter": Unicode(),
 560                  "create": Bool(False),
 561              },
 562          ),
 563          help="""
 564          Add, disable or modifying shortcuts.
 565  
 566          Each entry on the list should be a dictionary with ``command`` key
 567          identifying the target function executed by the shortcut and at least
 568          one of the following:
 569  
 570            - ``match_keys``: list of keys used to match an existing shortcut,
 571            - ``match_filter``: shortcut filter used to match an existing shortcut,
 572            - ``new_keys``: list of keys to set,
 573            - ``new_filter``: a new shortcut filter to set
 574  
 575          The filters have to be composed of pre-defined verbs and joined by one
 576          of the following conjunctions: ``&`` (and), ``|`` (or), ``~`` (not).
 577          The pre-defined verbs are:
 578  
 579          {filters}
 580  
 581          To disable a shortcut set ``new_keys`` to an empty list.
 582          To add a shortcut add key ``create`` with value ``True``.
 583  
 584          When modifying/disabling shortcuts, ``match_keys``/``match_filter`` can
 585          be omitted if the provided specification uniquely identifies a shortcut
 586          to be modified/disabled. When modifying a shortcut ``new_filter`` or
 587          ``new_keys`` can be omitted which will result in reuse of the existing
 588          filter/keys.
 589  
 590          Only shortcuts defined in IPython (and not default prompt-toolkit
 591          shortcuts) can be modified or disabled. The full list of shortcuts,
 592          command identifiers and filters is available under
 593          :ref:`terminal-shortcuts-list`.
 594  
 595          Here is an example:
 596  
 597          .. code::
 598  
 599              c.TerminalInteractiveShell.shortcuts = [
 600                 {{
 601                     "new_keys": ["c-q"],
 602                     "command": "prompt_toolkit:named_commands.capitalize_word",
 603                     "create": True,
 604                 }},
 605                 {{
 606                     "new_keys": ["c-j"],
 607                     "command": "prompt_toolkit:named_commands.beginning_of_line",
 608                     "create": True,
 609                 }},
 610              ]
 611  
 612  
 613          """.format(
 614              filters="\n        ".join([f"  - ``{k}``" for k in KEYBINDING_FILTERS])
 615          ),
 616      ).tag(config=True)
 617  
 618      @observe("shortcuts")
 619      def _shortcuts_changed(self, change):
 620          if self.pt_app:
 621              self.pt_app.key_bindings = self._merge_shortcuts(user_shortcuts=change.new)
 622  
 623      def _merge_shortcuts(self, user_shortcuts):
 624          # rebuild the bindings list from scratch
 625          key_bindings = create_ipython_shortcuts(self)
 626  
 627          # for now we only allow adding shortcuts for a specific set of
 628          # commands; this is a security precution.
 629          allowed_commands = {
 630              create_identifier(binding.command): binding.command
 631              for binding in KEY_BINDINGS
 632          }
 633          allowed_commands.update(
 634              {
 635                  create_identifier(command): command
 636                  for command in UNASSIGNED_ALLOWED_COMMANDS
 637              }
 638          )
 639          shortcuts_to_skip = []
 640          shortcuts_to_add = []
 641  
 642          for shortcut in user_shortcuts:
 643              command_id = shortcut["command"]
 644              if command_id not in allowed_commands:
 645                  allowed_commands = "\n - ".join(allowed_commands)
 646                  raise ValueError(
 647                      f"{command_id} is not a known shortcut command."
 648                      f" Allowed commands are: \n - {allowed_commands}"
 649                  )
 650              old_keys = shortcut.get("match_keys", None)
 651              old_filter = (
 652                  filter_from_string(shortcut["match_filter"])
 653                  if "match_filter" in shortcut
 654                  else None
 655              )
 656              matching = [
 657                  binding
 658                  for binding in KEY_BINDINGS
 659                  if (
 660                      (old_filter is None or binding.filter == old_filter)
 661                      and (old_keys is None or [k for k in binding.keys] == old_keys)
 662                      and create_identifier(binding.command) == command_id
 663                  )
 664              ]
 665  
 666              new_keys = shortcut.get("new_keys", None)
 667              new_filter = shortcut.get("new_filter", None)
 668  
 669              command = allowed_commands[command_id]
 670  
 671              creating_new = shortcut.get("create", False)
 672              modifying_existing = not creating_new and (
 673                  new_keys is not None or new_filter
 674              )
 675  
 676              if creating_new and new_keys == []:
 677                  raise ValueError("Cannot add a shortcut without keys")
 678  
 679              if modifying_existing:
 680                  specification = {
 681                      key: shortcut[key]
 682                      for key in ["command", "filter"]
 683                      if key in shortcut
 684                  }
 685                  if len(matching) == 0:
 686                      raise ValueError(
 687                          f"No shortcuts matching {specification} found in {KEY_BINDINGS}"
 688                      )
 689                  elif len(matching) > 1:
 690                      raise ValueError(
 691                          f"Multiple shortcuts matching {specification} found,"
 692                          f" please add keys/filter to select one of: {matching}"
 693                      )
 694  
 695                  matched = matching[0]
 696                  old_filter = matched.filter
 697                  old_keys = list(matched.keys)
 698                  shortcuts_to_skip.append(
 699                      RuntimeBinding(
 700                          command,
 701                          keys=old_keys,
 702                          filter=old_filter,
 703                      )
 704                  )
 705  
 706              if new_keys != []:
 707                  shortcuts_to_add.append(
 708                      RuntimeBinding(
 709                          command,
 710                          keys=new_keys or old_keys,
 711                          filter=(
 712                              filter_from_string(new_filter)
 713                              if new_filter is not None
 714                              else (
 715                                  old_filter
 716                                  if old_filter is not None
 717                                  else filter_from_string("always")
 718                              )
 719                          ),
 720                      )
 721                  )
 722  
 723          # rebuild the bindings list from scratch
 724          key_bindings = create_ipython_shortcuts(self, skip=shortcuts_to_skip)
 725          for binding in shortcuts_to_add:
 726              add_binding(key_bindings, binding)
 727  
 728          return key_bindings
 729  
 730      prompt_includes_vi_mode = Bool(True,
 731          help="Display the current vi mode (when using vi editing mode)."
 732      ).tag(config=True)
 733  
 734      prompt_line_number_format = Unicode(
 735          "",
 736          help="The format for line numbering, will be passed `line` (int, 1 based)"
 737          " the current line number and `rel_line` the relative line number."
 738          " for example to display both you can use the following template string :"
 739          " c.TerminalInteractiveShell.prompt_line_number_format='{line: 4d}/{rel_line:+03d} | '"
 740          " This will display the current line number, with leading space and a width of at least 4"
 741          " character, as well as the relative line number 0 padded and always with a + or - sign."
 742          " Note that when using Emacs mode the prompt of the first line may not update.",
 743      ).tag(config=True)
 744  
 745      @observe('term_title')
 746      def init_term_title(self, change=None):
 747          # Enable or disable the terminal title.
 748          if self.term_title and _is_tty:
 749              toggle_set_term_title(True)
 750              set_term_title(self.term_title_format.format(cwd=abbrev_cwd()))
 751          else:
 752              toggle_set_term_title(False)
 753  
 754      def restore_term_title(self):
 755          if self.term_title and _is_tty:
 756              restore_term_title()
 757  
 758      def init_display_formatter(self):
 759          super(TerminalInteractiveShell, self).init_display_formatter()
 760          # terminal only supports plain text
 761          self.display_formatter.active_types = ["text/plain"]
 762  
 763      def init_prompt_toolkit_cli(self):
 764          if self.simple_prompt:
 765              # Fall back to plain non-interactive output for tests.
 766              # This is very limited.
 767              def prompt():
 768                  prompt_text = "".join(x[1] for x in self.prompts.in_prompt_tokens())
 769                  lines = [input(prompt_text)]
 770                  prompt_continuation = "".join(x[1] for x in self.prompts.continuation_prompt_tokens())
 771                  while self.check_complete('\n'.join(lines))[0] == 'incomplete':
 772                      lines.append( input(prompt_continuation) )
 773                  return '\n'.join(lines)
 774              self.prompt_for_code = prompt
 775              return
 776  
 777          # Set up keyboard shortcuts
 778          key_bindings = self._merge_shortcuts(user_shortcuts=self.shortcuts)
 779  
 780          # Pre-populate history from IPython's history database
 781          history = PtkHistoryAdapter(self)
 782  
 783          self._style = self._make_style_from_name_or_cls(self.highlighting_style)
 784          self.style = DynamicStyle(lambda: self._style)
 785  
 786          editing_mode = getattr(EditingMode, self.editing_mode.upper())
 787  
 788          self._use_asyncio_inputhook = False
 789          self.pt_app = PromptSession(
 790              auto_suggest=self.auto_suggest,
 791              editing_mode=editing_mode,
 792              key_bindings=key_bindings,
 793              history=history,
 794              completer=IPythonPTCompleter(shell=self),
 795              enable_history_search=self.enable_history_search,
 796              style=self.style,
 797              include_default_pygments_style=False,
 798              mouse_support=self.mouse_support,
 799              enable_open_in_editor=self.extra_open_editor_shortcuts,
 800              color_depth=self.color_depth,
 801              tempfile_suffix=".py",
 802              **self._extra_prompt_options(),
 803          )
 804          if isinstance(self.auto_suggest, NavigableAutoSuggestFromHistory):
 805              self.auto_suggest.connect(self.pt_app)
 806  
 807      def _make_style_from_name_or_cls(self, name_or_cls):
 808          """
 809          Small wrapper that make an IPython compatible style from a style name
 810  
 811          We need that to add style for prompt ... etc.
 812          """
 813          style_overrides = {}
 814          if name_or_cls == 'legacy':
 815              legacy = self.colors.lower()
 816              if legacy == 'linux':
 817                  style_cls = get_style_by_name('monokai')
 818                  style_overrides = _style_overrides_linux
 819              elif legacy == 'lightbg':
 820                  style_overrides = _style_overrides_light_bg
 821                  style_cls = get_style_by_name('pastie')
 822              elif legacy == 'neutral':
 823                  # The default theme needs to be visible on both a dark background
 824                  # and a light background, because we can't tell what the terminal
 825                  # looks like. These tweaks to the default theme help with that.
 826                  style_cls = get_style_by_name('default')
 827                  style_overrides.update({
 828                      Token.Number: '#ansigreen',
 829                      Token.Operator: 'noinherit',
 830                      Token.String: '#ansiyellow',
 831                      Token.Name.Function: '#ansiblue',
 832                      Token.Name.Class: 'bold #ansiblue',
 833                      Token.Name.Namespace: 'bold #ansiblue',
 834                      Token.Name.Variable.Magic: '#ansiblue',
 835                      Token.Prompt: '#ansigreen',
 836                      Token.PromptNum: '#ansibrightgreen bold',
 837                      Token.OutPrompt: '#ansired',
 838                      Token.OutPromptNum: '#ansibrightred bold',
 839                  })
 840  
 841                  # Hack: Due to limited color support on the Windows console
 842                  # the prompt colors will be wrong without this
 843                  if os.name == 'nt':
 844                      style_overrides.update({
 845                          Token.Prompt: '#ansidarkgreen',
 846                          Token.PromptNum: '#ansigreen bold',
 847                          Token.OutPrompt: '#ansidarkred',
 848                          Token.OutPromptNum: '#ansired bold',
 849                      })
 850              elif legacy =='nocolor':
 851                  style_cls=_NoStyle
 852                  style_overrides = {}
 853              else :
 854                  raise ValueError('Got unknown colors: ', legacy)
 855          else :
 856              if isinstance(name_or_cls, str):
 857                  style_cls = get_style_by_name(name_or_cls)
 858              else:
 859                  style_cls = name_or_cls
 860              style_overrides = {
 861                  Token.Prompt: '#ansigreen',
 862                  Token.PromptNum: '#ansibrightgreen bold',
 863                  Token.OutPrompt: '#ansired',
 864                  Token.OutPromptNum: '#ansibrightred bold',
 865              }
 866          style_overrides.update(self.highlighting_style_overrides)
 867          style = merge_styles([
 868              style_from_pygments_cls(style_cls),
 869              style_from_pygments_dict(style_overrides),
 870          ])
 871  
 872          return style
 873  
 874      @property
 875      def pt_complete_style(self):
 876          return {
 877              'multicolumn': CompleteStyle.MULTI_COLUMN,
 878              'column': CompleteStyle.COLUMN,
 879              'readlinelike': CompleteStyle.READLINE_LIKE,
 880          }[self.display_completions]
 881  
 882      @property
 883      def color_depth(self):
 884          return (ColorDepth.TRUE_COLOR if self.true_color else None)
 885  
 886      def _extra_prompt_options(self):
 887          """
 888          Return the current layout option for the current Terminal InteractiveShell
 889          """
 890          def get_message():
 891              return PygmentsTokens(self.prompts.in_prompt_tokens())
 892  
 893          if self.editing_mode == "emacs" and self.prompt_line_number_format == "":
 894              # with emacs mode the prompt is (usually) static, so we call only
 895              # the function once. With VI mode it can toggle between [ins] and
 896              # [nor] so we can't precompute.
 897              # here I'm going to favor the default keybinding which almost
 898              # everybody uses to decrease CPU usage.
 899              # if we have issues with users with custom Prompts we can see how to
 900              # work around this.
 901              get_message = get_message()
 902  
 903          options = {
 904              "complete_in_thread": False,
 905              "lexer": IPythonPTLexer(),
 906              "reserve_space_for_menu": self.space_for_menu,
 907              "message": get_message,
 908              "prompt_continuation": (
 909                  lambda width, lineno, is_soft_wrap: PygmentsTokens(
 910                      _backward_compat_continuation_prompt_tokens(
 911                          self.prompts.continuation_prompt_tokens, width, lineno=lineno
 912                      )
 913                  )
 914              ),
 915              "multiline": True,
 916              "complete_style": self.pt_complete_style,
 917              "input_processors": [
 918                  # Highlight matching brackets, but only when this setting is
 919                  # enabled, and only when the DEFAULT_BUFFER has the focus.
 920                  ConditionalProcessor(
 921                      processor=HighlightMatchingBracketProcessor(chars="[](){}"),
 922                      filter=HasFocus(DEFAULT_BUFFER)
 923                      & ~IsDone()
 924                      & Condition(lambda: self.highlight_matching_brackets),
 925                  ),
 926                  # Show auto-suggestion in lines other than the last line.
 927                  ConditionalProcessor(
 928                      processor=AppendAutoSuggestionInAnyLine(),
 929                      filter=HasFocus(DEFAULT_BUFFER)
 930                      & ~IsDone()
 931                      & Condition(
 932                          lambda: isinstance(
 933                              self.auto_suggest,
 934                              NavigableAutoSuggestFromHistory,
 935                          )
 936                      ),
 937                  ),
 938              ],
 939          }
 940          if not PTK3:
 941              options['inputhook'] = self.inputhook
 942  
 943          return options
 944  
 945      def prompt_for_code(self):
 946          if self.rl_next_input:
 947              default = self.rl_next_input
 948              self.rl_next_input = None
 949          else:
 950              default = ''
 951  
 952          # In order to make sure that asyncio code written in the
 953          # interactive shell doesn't interfere with the prompt, we run the
 954          # prompt in a different event loop.
 955          # If we don't do this, people could spawn coroutine with a
 956          # while/true inside which will freeze the prompt.
 957  
 958          with patch_stdout(raw=True):
 959              if self._use_asyncio_inputhook:
 960                  # When we integrate the asyncio event loop, run the UI in the
 961                  # same event loop as the rest of the code. don't use an actual
 962                  # input hook. (Asyncio is not made for nesting event loops.)
 963                  asyncio_loop = get_asyncio_loop()
 964                  text = asyncio_loop.run_until_complete(
 965                      self.pt_app.prompt_async(
 966                          default=default, **self._extra_prompt_options()
 967                      )
 968                  )
 969              else:
 970                  text = self.pt_app.prompt(
 971                      default=default,
 972                      inputhook=self._inputhook,
 973                      **self._extra_prompt_options(),
 974                  )
 975  
 976          return text
 977  
 978      def enable_win_unicode_console(self):
 979          # Since IPython 7.10 doesn't support python < 3.6 and PEP 528, Python uses the unicode APIs for the Windows
 980          # console by default, so WUC shouldn't be needed.
 981          warn("`enable_win_unicode_console` is deprecated since IPython 7.10, does not do anything and will be removed in the future",
 982               DeprecationWarning,
 983               stacklevel=2)
 984  
 985      def init_io(self):
 986          if sys.platform not in {'win32', 'cli'}:
 987              return
 988  
 989          import colorama
 990          colorama.init()
 991  
 992      def init_magics(self):
 993          super(TerminalInteractiveShell, self).init_magics()
 994          self.register_magics(TerminalMagics)
 995  
 996      def init_alias(self):
 997          # The parent class defines aliases that can be safely used with any
 998          # frontend.
 999          super(TerminalInteractiveShell, self).init_alias()
1000  
1001          # Now define aliases that only make sense on the terminal, because they
1002          # need direct access to the console in a way that we can't emulate in
1003          # GUI or web frontend
1004          if os.name == 'posix':
1005              for cmd in ('clear', 'more', 'less', 'man'):
1006                  self.alias_manager.soft_define_alias(cmd, cmd)
1007  
1008      def __init__(self, *args, **kwargs) -> None:
1009          super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
1010          self._set_autosuggestions(self.autosuggestions_provider)
1011          self.init_prompt_toolkit_cli()
1012          self.init_term_title()
1013          self.keep_running = True
1014          self._set_formatter(self.autoformatter)
1015  
1016      def ask_exit(self):
1017          self.keep_running = False
1018  
1019      rl_next_input = None
1020  
1021      def interact(self):
1022          self.keep_running = True
1023          while self.keep_running:
1024              print(self.separate_in, end='')
1025  
1026              try:
1027                  code = self.prompt_for_code()
1028              except EOFError:
1029                  if (not self.confirm_exit) \
1030                          or self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
1031                      self.ask_exit()
1032  
1033              else:
1034                  if code:
1035                      self.run_cell(code, store_history=True)
1036  
1037      def mainloop(self):
1038          # An extra layer of protection in case someone mashing Ctrl-C breaks
1039          # out of our internal code.
1040          while True:
1041              try:
1042                  self.interact()
1043                  break
1044              except KeyboardInterrupt as e:
1045                  print("\n%s escaped interact()\n" % type(e).__name__)
1046              finally:
1047                  # An interrupt during the eventloop will mess up the
1048                  # internal state of the prompt_toolkit library.
1049                  # Stopping the eventloop fixes this, see
1050                  # https://github.com/ipython/ipython/pull/9867
1051                  if hasattr(self, '_eventloop'):
1052                      self._eventloop.stop()
1053  
1054                  self.restore_term_title()
1055  
1056          # try to call some at-exit operation optimistically as some things can't
1057          # be done during interpreter shutdown. this is technically inaccurate as
1058          # this make mainlool not re-callable, but that should be a rare if not
1059          # in existent use case.
1060  
1061          self._atexit_once()
1062  
1063      _inputhook = None
1064      def inputhook(self, context):
1065          if self._inputhook is not None:
1066              self._inputhook(context)
1067  
1068      active_eventloop: Optional[str] = None
1069  
1070      def enable_gui(self, gui: Optional[str] = None) -> None:
1071          if gui:
1072              from ..core.pylabtools import _convert_gui_from_matplotlib
1073  
1074              gui = _convert_gui_from_matplotlib(gui)
1075  
1076          if self.simple_prompt is True and gui is not None:
1077              print(
1078                  f'Cannot install event loop hook for "{gui}" when running with `--simple-prompt`.'
1079              )
1080              print(
1081                  "NOTE: Tk is supported natively; use Tk apps and Tk backends with `--simple-prompt`."
1082              )
1083              return
1084  
1085          if self._inputhook is None and gui is None:
1086              print("No event loop hook running.")
1087              return
1088  
1089          if self._inputhook is not None and gui is not None:
1090              newev, newinhook = get_inputhook_name_and_func(gui)
1091              if self._inputhook == newinhook:
1092                  # same inputhook, do nothing
1093                  self.log.info(
1094                      f"Shell is already running the {self.active_eventloop} eventloop. Doing nothing"
1095                  )
1096                  return
1097              self.log.warning(
1098                  f"Shell is already running a different gui event loop for {self.active_eventloop}. "
1099                  "Call with no arguments to disable the current loop."
1100              )
1101              return
1102          if self._inputhook is not None and gui is None:
1103              self.active_eventloop = self._inputhook = None
1104  
1105          if gui and (gui not in {None, "webagg"}):
1106              # This hook runs with each cycle of the `prompt_toolkit`'s event loop.
1107              self.active_eventloop, self._inputhook = get_inputhook_name_and_func(gui)
1108          else:
1109              self.active_eventloop = self._inputhook = None
1110  
1111          self._use_asyncio_inputhook = gui == "asyncio"
1112  
1113      # Run !system commands directly, not through pipes, so terminal programs
1114      # work correctly.
1115      system = InteractiveShell.system_raw
1116  
1117      def auto_rewrite_input(self, cmd):
1118          """Overridden from the parent class to use fancy rewriting prompt"""
1119          if not self.show_rewritten_input:
1120              return
1121  
1122          tokens = self.prompts.rewrite_prompt_tokens()
1123          if self.pt_app:
1124              print_formatted_text(PygmentsTokens(tokens), end='',
1125                                   style=self.pt_app.app.style)
1126              print(cmd)
1127          else:
1128              prompt = ''.join(s for t, s in tokens)
1129              print(prompt, cmd, sep='')
1130  
1131      _prompts_before = None
1132      def switch_doctest_mode(self, mode):
1133          """Switch prompts to classic for %doctest_mode"""
1134          if mode:
1135              self._prompts_before = self.prompts
1136              self.prompts = ClassicPrompts(self)
1137          elif self._prompts_before:
1138              self.prompts = self._prompts_before
1139              self._prompts_before = None
1140  #        self._update_layout()
1141  
1142  
1143  InteractiveShellABC.register(TerminalInteractiveShell)
1144  
1145  if __name__ == '__main__':
1146      TerminalInteractiveShell.instance().interact()
1147