interactiveshell.py 39 KB

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