##// END OF EJS Templates
Improve indentation of next line when using CTRL-o in terminal....
Matthias Bussonnier -
Show More
@@ -1,5 +1,16 b''
1 """
2 Module to define and register Terminal IPython shortcuts with
3 :any:`prompt_toolkit`
4 """
5
6 # Copyright (c) IPython Development Team.
7 # Distributed under the terms of the Modified BSD License.
8
9 import warnings
1 10 import signal
2 11 import sys
12 from typing import Callable
13
3 14
4 15 from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER
5 16 from prompt_toolkit.filters import (HasFocus, HasSelection, Condition,
@@ -60,7 +71,7 b' def register_ipython_shortcuts(registry, shell):'
60 71
61 72 registry.add_binding(Keys.ControlO,
62 73 filter=(HasFocus(DEFAULT_BUFFER)
63 & EmacsInsertMode()))(newline_with_copy_margin)
74 & EmacsInsertMode()))(newline_autoindent_outer(shell.input_splitter))
64 75
65 76 registry.add_binding(Keys.F2,
66 77 filter=HasFocus(DEFAULT_BUFFER)
@@ -166,11 +177,20 b' def force_exit(event):'
166 177 def indent_buffer(event):
167 178 event.current_buffer.insert_text(' ' * 4)
168 179
180 @undoc
169 181 def newline_with_copy_margin(event):
170 182 """
183 DEPRECATED since IPython 6.0
184
185 See :any:`newline_autoindent_outer` for a replacement.
186
171 187 Preserve margin and cursor position when using
172 188 Control-O to insert a newline in EMACS mode
173 189 """
190 warnings.warn("`newline_with_copy_margin(event)` is deprecated since IPython 6.0. "
191 "see `newline_autoindent_outer(shell)(event)` for a replacement.",
192 DeprecationWarning, stacklevel=2)
193
174 194 b = event.current_buffer
175 195 cursor_start_pos = b.document.cursor_position_col
176 196 b.newline(copy_margin=True)
@@ -180,6 +200,30 b' def newline_with_copy_margin(event):'
180 200 pos_diff = cursor_start_pos - cursor_end_pos
181 201 b.cursor_right(count=pos_diff)
182 202
203 def newline_autoindent_outer(inputsplitter) -> Callable[..., None]:
204 """
205 Return a function suitable for inserting a indented newline after the cursor.
206
207 Fancier version of deprecated ``newline_with_copy_margin`` which should
208 compute the correct indentation of the inserted line. That is to say, indent
209 by 4 extra space after a function definition, class definition, context
210 manager... And dedent by 4 space after ``pass``, ``return``, ``raise ...``.
211 """
212
213 def newline_autoindent(event):
214 """insert a newline after the cursor indented appropriately."""
215 b = event.current_buffer
216 d = b.document
217
218 if b.complete_state:
219 b.cancel_completion()
220 text = d.text[:d.cursor_position] + '\n'
221 _, indent = inputsplitter.check_complete(text)
222 b.insert_text('\n' + (' ' * (indent or 0)), move_cursor=False)
223
224 return newline_autoindent
225
226
183 227 def open_input_in_editor(event):
184 228 event.cli.current_buffer.tempfile_suffix = ".py"
185 229 event.cli.current_buffer.open_in_editor(event.cli)
General Comments 0
You need to be logged in to leave comments. Login now