##// END OF EJS Templates
Only handle Ctrl-C when in main input buffer...
Thomas Kluyver -
Show More
@@ -1,289 +1,289 b''
1 """IPython terminal interface using prompt_toolkit in place of readline"""
1 """IPython terminal interface using prompt_toolkit in place of readline"""
2 from __future__ import print_function
2 from __future__ import print_function
3
3
4 import os
4 import os
5 import sys
5 import sys
6
6
7 from IPython.core.interactiveshell import InteractiveShell
7 from IPython.core.interactiveshell import InteractiveShell
8 from IPython.utils.py3compat import PY3, cast_unicode_py2, input
8 from IPython.utils.py3compat import PY3, cast_unicode_py2, input
9 from IPython.utils.terminal import toggle_set_term_title, set_term_title
9 from IPython.utils.terminal import toggle_set_term_title, set_term_title
10 from IPython.utils.process import abbrev_cwd
10 from IPython.utils.process import abbrev_cwd
11 from traitlets import Bool, Unicode, Dict
11 from traitlets import Bool, Unicode, Dict
12
12
13 from prompt_toolkit.completion import Completer, Completion
13 from prompt_toolkit.completion import Completer, Completion
14 from prompt_toolkit.enums import DEFAULT_BUFFER
14 from prompt_toolkit.enums import DEFAULT_BUFFER
15 from prompt_toolkit.filters import HasFocus, HasSelection, Condition
15 from prompt_toolkit.filters import HasFocus, HasSelection, Condition
16 from prompt_toolkit.history import InMemoryHistory
16 from prompt_toolkit.history import InMemoryHistory
17 from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop
17 from prompt_toolkit.shortcuts import create_prompt_application, create_eventloop
18 from prompt_toolkit.interface import CommandLineInterface
18 from prompt_toolkit.interface import CommandLineInterface
19 from prompt_toolkit.key_binding.manager import KeyBindingManager
19 from prompt_toolkit.key_binding.manager import KeyBindingManager
20 from prompt_toolkit.key_binding.vi_state import InputMode
20 from prompt_toolkit.key_binding.vi_state import InputMode
21 from prompt_toolkit.key_binding.bindings.vi import ViStateFilter
21 from prompt_toolkit.key_binding.bindings.vi import ViStateFilter
22 from prompt_toolkit.keys import Keys
22 from prompt_toolkit.keys import Keys
23 from prompt_toolkit.layout.lexers import PygmentsLexer
23 from prompt_toolkit.layout.lexers import PygmentsLexer
24 from prompt_toolkit.styles import PygmentsStyle
24 from prompt_toolkit.styles import PygmentsStyle
25
25
26 from pygments.styles import get_style_by_name
26 from pygments.styles import get_style_by_name
27 from pygments.lexers import Python3Lexer, PythonLexer
27 from pygments.lexers import Python3Lexer, PythonLexer
28 from pygments.token import Token
28 from pygments.token import Token
29
29
30 from .pt_inputhooks import get_inputhook_func
30 from .pt_inputhooks import get_inputhook_func
31 from .interactiveshell import get_default_editor, TerminalMagics
31 from .interactiveshell import get_default_editor, TerminalMagics
32
32
33
33
34
34
35 class IPythonPTCompleter(Completer):
35 class IPythonPTCompleter(Completer):
36 """Adaptor to provide IPython completions to prompt_toolkit"""
36 """Adaptor to provide IPython completions to prompt_toolkit"""
37 def __init__(self, ipy_completer):
37 def __init__(self, ipy_completer):
38 self.ipy_completer = ipy_completer
38 self.ipy_completer = ipy_completer
39
39
40 def get_completions(self, document, complete_event):
40 def get_completions(self, document, complete_event):
41 if not document.current_line.strip():
41 if not document.current_line.strip():
42 return
42 return
43
43
44 used, matches = self.ipy_completer.complete(
44 used, matches = self.ipy_completer.complete(
45 line_buffer=document.current_line,
45 line_buffer=document.current_line,
46 cursor_pos=document.cursor_position_col
46 cursor_pos=document.cursor_position_col
47 )
47 )
48 start_pos = -len(used)
48 start_pos = -len(used)
49 for m in matches:
49 for m in matches:
50 yield Completion(m, start_position=start_pos)
50 yield Completion(m, start_position=start_pos)
51
51
52 class TerminalInteractiveShell(InteractiveShell):
52 class TerminalInteractiveShell(InteractiveShell):
53 colors_force = True
53 colors_force = True
54
54
55 pt_cli = None
55 pt_cli = None
56
56
57 vi_mode = Bool(False, config=True,
57 vi_mode = Bool(False, config=True,
58 help="Use vi style keybindings at the prompt",
58 help="Use vi style keybindings at the prompt",
59 )
59 )
60
60
61 mouse_support = Bool(False, config=True,
61 mouse_support = Bool(False, config=True,
62 help="Enable mouse support in the prompt"
62 help="Enable mouse support in the prompt"
63 )
63 )
64
64
65 highlighting_style = Unicode('', config=True,
65 highlighting_style = Unicode('', config=True,
66 help="The name of a Pygments style to use for syntax highlighting"
66 help="The name of a Pygments style to use for syntax highlighting"
67 )
67 )
68
68
69 highlighting_style_overrides = Dict(config=True,
69 highlighting_style_overrides = Dict(config=True,
70 help="Override highlighting format for specific tokens"
70 help="Override highlighting format for specific tokens"
71 )
71 )
72
72
73 editor = Unicode(get_default_editor(), config=True,
73 editor = Unicode(get_default_editor(), config=True,
74 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
74 help="Set the editor used by IPython (default to $EDITOR/vi/notepad)."
75 )
75 )
76
76
77 term_title = Bool(True, config=True,
77 term_title = Bool(True, config=True,
78 help="Automatically set the terminal title"
78 help="Automatically set the terminal title"
79 )
79 )
80 def _term_title_changed(self, name, new_value):
80 def _term_title_changed(self, name, new_value):
81 self.init_term_title()
81 self.init_term_title()
82
82
83 def init_term_title(self):
83 def init_term_title(self):
84 # Enable or disable the terminal title.
84 # Enable or disable the terminal title.
85 if self.term_title:
85 if self.term_title:
86 toggle_set_term_title(True)
86 toggle_set_term_title(True)
87 set_term_title('IPython: ' + abbrev_cwd())
87 set_term_title('IPython: ' + abbrev_cwd())
88 else:
88 else:
89 toggle_set_term_title(False)
89 toggle_set_term_title(False)
90
90
91 def get_prompt_tokens(self, cli):
91 def get_prompt_tokens(self, cli):
92 return [
92 return [
93 (Token.Prompt, 'In ['),
93 (Token.Prompt, 'In ['),
94 (Token.PromptNum, str(self.execution_count)),
94 (Token.PromptNum, str(self.execution_count)),
95 (Token.Prompt, ']: '),
95 (Token.Prompt, ']: '),
96 ]
96 ]
97
97
98 def get_continuation_tokens(self, cli, width):
98 def get_continuation_tokens(self, cli, width):
99 return [
99 return [
100 (Token.Prompt, (' ' * (width - 5)) + '...: '),
100 (Token.Prompt, (' ' * (width - 5)) + '...: '),
101 ]
101 ]
102
102
103 def init_prompt_toolkit_cli(self):
103 def init_prompt_toolkit_cli(self):
104 if ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or not sys.stdin.isatty():
104 if ('IPY_TEST_SIMPLE_PROMPT' in os.environ) or not sys.stdin.isatty():
105 # Fall back to plain non-interactive output for tests.
105 # Fall back to plain non-interactive output for tests.
106 # This is very limited, and only accepts a single line.
106 # This is very limited, and only accepts a single line.
107 def prompt():
107 def prompt():
108 return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
108 return cast_unicode_py2(input('In [%d]: ' % self.execution_count))
109 self.prompt_for_code = prompt
109 self.prompt_for_code = prompt
110 return
110 return
111
111
112 kbmanager = KeyBindingManager.for_prompt(enable_vi_mode=self.vi_mode)
112 kbmanager = KeyBindingManager.for_prompt(enable_vi_mode=self.vi_mode)
113 insert_mode = ViStateFilter(kbmanager.get_vi_state, InputMode.INSERT)
113 insert_mode = ViStateFilter(kbmanager.get_vi_state, InputMode.INSERT)
114 # Ctrl+J == Enter, seemingly
114 # Ctrl+J == Enter, seemingly
115 @kbmanager.registry.add_binding(Keys.ControlJ,
115 @kbmanager.registry.add_binding(Keys.ControlJ,
116 filter=(HasFocus(DEFAULT_BUFFER)
116 filter=(HasFocus(DEFAULT_BUFFER)
117 & ~HasSelection()
117 & ~HasSelection()
118 & insert_mode
118 & insert_mode
119 ))
119 ))
120 def _(event):
120 def _(event):
121 b = event.current_buffer
121 b = event.current_buffer
122 d = b.document
122 d = b.document
123 if not (d.on_last_line or d.cursor_position_row >= d.line_count
123 if not (d.on_last_line or d.cursor_position_row >= d.line_count
124 - d.empty_line_count_at_the_end()):
124 - d.empty_line_count_at_the_end()):
125 b.newline()
125 b.newline()
126 return
126 return
127
127
128 status, indent = self.input_splitter.check_complete(d.text)
128 status, indent = self.input_splitter.check_complete(d.text)
129
129
130 if (status != 'incomplete') and b.accept_action.is_returnable:
130 if (status != 'incomplete') and b.accept_action.is_returnable:
131 b.accept_action.validate_and_handle(event.cli, b)
131 b.accept_action.validate_and_handle(event.cli, b)
132 else:
132 else:
133 b.insert_text('\n' + (' ' * (indent or 0)))
133 b.insert_text('\n' + (' ' * (indent or 0)))
134
134
135 @kbmanager.registry.add_binding(Keys.ControlC)
135 @kbmanager.registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER))
136 def _(event):
136 def _(event):
137 event.current_buffer.reset()
137 event.current_buffer.reset()
138
138
139 @Condition
139 @Condition
140 def cursor_in_leading_ws(cli):
140 def cursor_in_leading_ws(cli):
141 before = cli.application.buffer.document.current_line_before_cursor
141 before = cli.application.buffer.document.current_line_before_cursor
142 return (not before) or before.isspace()
142 return (not before) or before.isspace()
143
143
144 # Ctrl+I == Tab
144 # Ctrl+I == Tab
145 @kbmanager.registry.add_binding(Keys.ControlI,
145 @kbmanager.registry.add_binding(Keys.ControlI,
146 filter=(HasFocus(DEFAULT_BUFFER)
146 filter=(HasFocus(DEFAULT_BUFFER)
147 & ~HasSelection()
147 & ~HasSelection()
148 & insert_mode
148 & insert_mode
149 & cursor_in_leading_ws
149 & cursor_in_leading_ws
150 ))
150 ))
151 def _(event):
151 def _(event):
152 event.current_buffer.insert_text(' ' * 4)
152 event.current_buffer.insert_text(' ' * 4)
153
153
154 # Pre-populate history from IPython's history database
154 # Pre-populate history from IPython's history database
155 history = InMemoryHistory()
155 history = InMemoryHistory()
156 last_cell = u""
156 last_cell = u""
157 for _, _, cell in self.history_manager.get_tail(self.history_load_length,
157 for _, _, cell in self.history_manager.get_tail(self.history_load_length,
158 include_latest=True):
158 include_latest=True):
159 # Ignore blank lines and consecutive duplicates
159 # Ignore blank lines and consecutive duplicates
160 cell = cell.rstrip()
160 cell = cell.rstrip()
161 if cell and (cell != last_cell):
161 if cell and (cell != last_cell):
162 history.append(cell)
162 history.append(cell)
163
163
164 style_overrides = {
164 style_overrides = {
165 Token.Prompt: '#009900',
165 Token.Prompt: '#009900',
166 Token.PromptNum: '#00ff00 bold',
166 Token.PromptNum: '#00ff00 bold',
167 }
167 }
168 if self.highlighting_style:
168 if self.highlighting_style:
169 style_cls = get_style_by_name(self.highlighting_style)
169 style_cls = get_style_by_name(self.highlighting_style)
170 else:
170 else:
171 style_cls = get_style_by_name('default')
171 style_cls = get_style_by_name('default')
172 # The default theme needs to be visible on both a dark background
172 # The default theme needs to be visible on both a dark background
173 # and a light background, because we can't tell what the terminal
173 # and a light background, because we can't tell what the terminal
174 # looks like. These tweaks to the default theme help with that.
174 # looks like. These tweaks to the default theme help with that.
175 style_overrides.update({
175 style_overrides.update({
176 Token.Number: '#007700',
176 Token.Number: '#007700',
177 Token.Operator: 'noinherit',
177 Token.Operator: 'noinherit',
178 Token.String: '#BB6622',
178 Token.String: '#BB6622',
179 Token.Name.Function: '#2080D0',
179 Token.Name.Function: '#2080D0',
180 Token.Name.Class: 'bold #2080D0',
180 Token.Name.Class: 'bold #2080D0',
181 Token.Name.Namespace: 'bold #2080D0',
181 Token.Name.Namespace: 'bold #2080D0',
182 })
182 })
183 style_overrides.update(self.highlighting_style_overrides)
183 style_overrides.update(self.highlighting_style_overrides)
184 style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
184 style = PygmentsStyle.from_defaults(pygments_style_cls=style_cls,
185 style_dict=style_overrides)
185 style_dict=style_overrides)
186
186
187 app = create_prompt_application(multiline=True,
187 app = create_prompt_application(multiline=True,
188 lexer=PygmentsLexer(Python3Lexer if PY3 else PythonLexer),
188 lexer=PygmentsLexer(Python3Lexer if PY3 else PythonLexer),
189 get_prompt_tokens=self.get_prompt_tokens,
189 get_prompt_tokens=self.get_prompt_tokens,
190 get_continuation_tokens=self.get_continuation_tokens,
190 get_continuation_tokens=self.get_continuation_tokens,
191 key_bindings_registry=kbmanager.registry,
191 key_bindings_registry=kbmanager.registry,
192 history=history,
192 history=history,
193 completer=IPythonPTCompleter(self.Completer),
193 completer=IPythonPTCompleter(self.Completer),
194 enable_history_search=True,
194 enable_history_search=True,
195 style=style,
195 style=style,
196 mouse_support=self.mouse_support,
196 mouse_support=self.mouse_support,
197 )
197 )
198
198
199 self.pt_cli = CommandLineInterface(app,
199 self.pt_cli = CommandLineInterface(app,
200 eventloop=create_eventloop(self.inputhook))
200 eventloop=create_eventloop(self.inputhook))
201
201
202 def prompt_for_code(self):
202 def prompt_for_code(self):
203 document = self.pt_cli.run(pre_run=self.pre_prompt)
203 document = self.pt_cli.run(pre_run=self.pre_prompt)
204 return document.text
204 return document.text
205
205
206 def init_io(self):
206 def init_io(self):
207 if sys.platform not in {'win32', 'cli'}:
207 if sys.platform not in {'win32', 'cli'}:
208 return
208 return
209
209
210 import colorama
210 import colorama
211 colorama.init()
211 colorama.init()
212
212
213 # For some reason we make these wrappers around stdout/stderr.
213 # For some reason we make these wrappers around stdout/stderr.
214 # For now, we need to reset them so all output gets coloured.
214 # For now, we need to reset them so all output gets coloured.
215 # https://github.com/ipython/ipython/issues/8669
215 # https://github.com/ipython/ipython/issues/8669
216 from IPython.utils import io
216 from IPython.utils import io
217 io.stdout = io.IOStream(sys.stdout)
217 io.stdout = io.IOStream(sys.stdout)
218 io.stderr = io.IOStream(sys.stderr)
218 io.stderr = io.IOStream(sys.stderr)
219
219
220 def init_magics(self):
220 def init_magics(self):
221 super(TerminalInteractiveShell, self).init_magics()
221 super(TerminalInteractiveShell, self).init_magics()
222 self.register_magics(TerminalMagics)
222 self.register_magics(TerminalMagics)
223
223
224 def init_alias(self):
224 def init_alias(self):
225 # The parent class defines aliases that can be safely used with any
225 # The parent class defines aliases that can be safely used with any
226 # frontend.
226 # frontend.
227 super(TerminalInteractiveShell, self).init_alias()
227 super(TerminalInteractiveShell, self).init_alias()
228
228
229 # Now define aliases that only make sense on the terminal, because they
229 # Now define aliases that only make sense on the terminal, because they
230 # need direct access to the console in a way that we can't emulate in
230 # need direct access to the console in a way that we can't emulate in
231 # GUI or web frontend
231 # GUI or web frontend
232 if os.name == 'posix':
232 if os.name == 'posix':
233 for cmd in ['clear', 'more', 'less', 'man']:
233 for cmd in ['clear', 'more', 'less', 'man']:
234 self.alias_manager.soft_define_alias(cmd, cmd)
234 self.alias_manager.soft_define_alias(cmd, cmd)
235
235
236
236
237 def __init__(self, *args, **kwargs):
237 def __init__(self, *args, **kwargs):
238 super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
238 super(TerminalInteractiveShell, self).__init__(*args, **kwargs)
239 self.init_prompt_toolkit_cli()
239 self.init_prompt_toolkit_cli()
240 self.init_term_title()
240 self.init_term_title()
241 self.keep_running = True
241 self.keep_running = True
242
242
243 def ask_exit(self):
243 def ask_exit(self):
244 self.keep_running = False
244 self.keep_running = False
245
245
246 rl_next_input = None
246 rl_next_input = None
247
247
248 def pre_prompt(self):
248 def pre_prompt(self):
249 if self.rl_next_input:
249 if self.rl_next_input:
250 self.pt_cli.application.buffer.text = cast_unicode_py2(self.rl_next_input)
250 self.pt_cli.application.buffer.text = cast_unicode_py2(self.rl_next_input)
251 self.rl_next_input = None
251 self.rl_next_input = None
252
252
253 def interact(self):
253 def interact(self):
254 while self.keep_running:
254 while self.keep_running:
255 print(self.separate_in, end='')
255 print(self.separate_in, end='')
256
256
257 try:
257 try:
258 code = self.prompt_for_code()
258 code = self.prompt_for_code()
259 except EOFError:
259 except EOFError:
260 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
260 if self.ask_yes_no('Do you really want to exit ([y]/n)?','y','n'):
261 self.ask_exit()
261 self.ask_exit()
262
262
263 else:
263 else:
264 if code:
264 if code:
265 self.run_cell(code, store_history=True)
265 self.run_cell(code, store_history=True)
266
266
267 def mainloop(self):
267 def mainloop(self):
268 # An extra layer of protection in case someone mashing Ctrl-C breaks
268 # An extra layer of protection in case someone mashing Ctrl-C breaks
269 # out of our internal code.
269 # out of our internal code.
270 while True:
270 while True:
271 try:
271 try:
272 self.interact()
272 self.interact()
273 break
273 break
274 except KeyboardInterrupt:
274 except KeyboardInterrupt:
275 print("\nKeyboardInterrupt escaped interact()\n")
275 print("\nKeyboardInterrupt escaped interact()\n")
276
276
277 _inputhook = None
277 _inputhook = None
278 def inputhook(self, context):
278 def inputhook(self, context):
279 if self._inputhook is not None:
279 if self._inputhook is not None:
280 self._inputhook(context)
280 self._inputhook(context)
281
281
282 def enable_gui(self, gui=None):
282 def enable_gui(self, gui=None):
283 if gui:
283 if gui:
284 self._inputhook = get_inputhook_func(gui)
284 self._inputhook = get_inputhook_func(gui)
285 else:
285 else:
286 self._inputhook = None
286 self._inputhook = None
287
287
288 if __name__ == '__main__':
288 if __name__ == '__main__':
289 TerminalInteractiveShell.instance().interact()
289 TerminalInteractiveShell.instance().interact()
General Comments 0
You need to be logged in to leave comments. Login now