Show More
@@ -188,6 +188,15 b' class TerminalInteractiveShell(InteractiveShell):' | |||||
188 | allow_none=True |
|
188 | allow_none=True | |
189 | ).tag(config=True) |
|
189 | ).tag(config=True) | |
190 |
|
190 | |||
|
191 | auto_match = Bool( | |||
|
192 | False, | |||
|
193 | help=""" | |||
|
194 | Automatically add/delete closing bracket or quote when opening bracket or quote is entered/deleted. | |||
|
195 | Brackets: (), [], {} | |||
|
196 | Quotes: '', \"\" | |||
|
197 | """, | |||
|
198 | ).tag(config=True) | |||
|
199 | ||||
191 | mouse_support = Bool(False, |
|
200 | mouse_support = Bool(False, | |
192 | help="Enable mouse support in the prompt\n(Note: prevents selecting text with the mouse)" |
|
201 | help="Enable mouse support in the prompt\n(Note: prevents selecting text with the mouse)" | |
193 | ).tag(config=True) |
|
202 | ).tag(config=True) |
@@ -85,6 +85,10 b' def create_ipython_shortcuts(shell):' | |||||
85 |
|
85 | |||
86 | kb.add('f2', filter=has_focus(DEFAULT_BUFFER))(open_input_in_editor) |
|
86 | kb.add('f2', filter=has_focus(DEFAULT_BUFFER))(open_input_in_editor) | |
87 |
|
87 | |||
|
88 | @Condition | |||
|
89 | def auto_match(): | |||
|
90 | return shell.auto_match | |||
|
91 | ||||
88 | focused_insert = (vi_insert_mode | emacs_insert_mode) & has_focus(DEFAULT_BUFFER) |
|
92 | focused_insert = (vi_insert_mode | emacs_insert_mode) & has_focus(DEFAULT_BUFFER) | |
89 | _preceding_text_cache = {} |
|
93 | _preceding_text_cache = {} | |
90 | _following_text_cache = {} |
|
94 | _following_text_cache = {} | |
@@ -120,27 +124,27 b' def create_ipython_shortcuts(shell):' | |||||
120 | return condition |
|
124 | return condition | |
121 |
|
125 | |||
122 | # auto match |
|
126 | # auto match | |
123 | @kb.add("(", filter=focused_insert & following_text(r"[,)}\]]|$")) |
|
127 | @kb.add("(", filter=focused_insert & auto_match & following_text(r"[,)}\]]|$")) | |
124 | def _(event): |
|
128 | def _(event): | |
125 | event.current_buffer.insert_text("()") |
|
129 | event.current_buffer.insert_text("()") | |
126 | event.current_buffer.cursor_left() |
|
130 | event.current_buffer.cursor_left() | |
127 |
|
131 | |||
128 | @kb.add("[", filter=focused_insert & following_text(r"[,)}\]]|$")) |
|
132 | @kb.add("[", filter=focused_insert & auto_match & following_text(r"[,)}\]]|$")) | |
129 | def _(event): |
|
133 | def _(event): | |
130 | event.current_buffer.insert_text("[]") |
|
134 | event.current_buffer.insert_text("[]") | |
131 | event.current_buffer.cursor_left() |
|
135 | event.current_buffer.cursor_left() | |
132 |
|
136 | |||
133 | @kb.add("{", filter=focused_insert & following_text(r"[,)}\]]|$")) |
|
137 | @kb.add("{", filter=focused_insert & auto_match & following_text(r"[,)}\]]|$")) | |
134 | def _(event): |
|
138 | def _(event): | |
135 | event.current_buffer.insert_text("{}") |
|
139 | event.current_buffer.insert_text("{}") | |
136 | event.current_buffer.cursor_left() |
|
140 | event.current_buffer.cursor_left() | |
137 |
|
141 | |||
138 | @kb.add('"', filter=focused_insert & following_text(r"[,)}\]]|$")) |
|
142 | @kb.add('"', filter=focused_insert & auto_match & following_text(r"[,)}\]]|$")) | |
139 | def _(event): |
|
143 | def _(event): | |
140 | event.current_buffer.insert_text('""') |
|
144 | event.current_buffer.insert_text('""') | |
141 | event.current_buffer.cursor_left() |
|
145 | event.current_buffer.cursor_left() | |
142 |
|
146 | |||
143 | @kb.add("'", filter=focused_insert & following_text(r"[,)}\]]|$")) |
|
147 | @kb.add("'", filter=focused_insert & auto_match & following_text(r"[,)}\]]|$")) | |
144 | def _(event): |
|
148 | def _(event): | |
145 | event.current_buffer.insert_text("''") |
|
149 | event.current_buffer.insert_text("''") | |
146 | event.current_buffer.cursor_left() |
|
150 | event.current_buffer.cursor_left() | |
@@ -187,33 +191,48 b' def create_ipython_shortcuts(shell):' | |||||
187 | event.current_buffer.cursor_left() |
|
191 | event.current_buffer.cursor_left() | |
188 |
|
192 | |||
189 | # just move cursor |
|
193 | # just move cursor | |
190 | @kb.add(")", filter=focused_insert & following_text(r"^\)")) |
|
194 | @kb.add(")", filter=focused_insert & auto_match & following_text(r"^\)")) | |
191 | @kb.add("]", filter=focused_insert & following_text(r"^\]")) |
|
195 | @kb.add("]", filter=focused_insert & auto_match & following_text(r"^\]")) | |
192 | @kb.add("}", filter=focused_insert & following_text(r"^\}")) |
|
196 | @kb.add("}", filter=focused_insert & auto_match & following_text(r"^\}")) | |
193 | @kb.add('"', filter=focused_insert & following_text('^"')) |
|
197 | @kb.add('"', filter=focused_insert & auto_match & following_text('^"')) | |
194 | @kb.add("'", filter=focused_insert & following_text("^'")) |
|
198 | @kb.add("'", filter=focused_insert & auto_match & following_text("^'")) | |
195 | def _(event): |
|
199 | def _(event): | |
196 | event.current_buffer.cursor_right() |
|
200 | event.current_buffer.cursor_right() | |
197 |
|
201 | |||
198 | @kb.add( |
|
202 | @kb.add( | |
199 | "backspace", |
|
203 | "backspace", | |
200 | filter=focused_insert & preceding_text(r".*\($") & following_text(r"^\)"), |
|
204 | filter=focused_insert | |
|
205 | & preceding_text(r".*\($") | |||
|
206 | & auto_match | |||
|
207 | & following_text(r"^\)"), | |||
201 | ) |
|
208 | ) | |
202 | @kb.add( |
|
209 | @kb.add( | |
203 | "backspace", |
|
210 | "backspace", | |
204 | filter=focused_insert & preceding_text(r".*\[$") & following_text(r"^\]"), |
|
211 | filter=focused_insert | |
|
212 | & preceding_text(r".*\[$") | |||
|
213 | & auto_match | |||
|
214 | & following_text(r"^\]"), | |||
205 | ) |
|
215 | ) | |
206 | @kb.add( |
|
216 | @kb.add( | |
207 | "backspace", |
|
217 | "backspace", | |
208 | filter=focused_insert & preceding_text(r".*\{$") & following_text(r"^\}"), |
|
218 | filter=focused_insert | |
|
219 | & preceding_text(r".*\{$") | |||
|
220 | & auto_match | |||
|
221 | & following_text(r"^\}"), | |||
209 | ) |
|
222 | ) | |
210 | @kb.add( |
|
223 | @kb.add( | |
211 | "backspace", |
|
224 | "backspace", | |
212 | filter=focused_insert & preceding_text('.*"$') & following_text('^"'), |
|
225 | filter=focused_insert | |
|
226 | & preceding_text('.*"$') | |||
|
227 | & auto_match | |||
|
228 | & following_text('^"'), | |||
213 | ) |
|
229 | ) | |
214 | @kb.add( |
|
230 | @kb.add( | |
215 | "backspace", |
|
231 | "backspace", | |
216 | filter=focused_insert & preceding_text(r".*'$") & following_text(r"^'"), |
|
232 | filter=focused_insert | |
|
233 | & preceding_text(r".*'$") | |||
|
234 | & auto_match | |||
|
235 | & following_text(r"^'"), | |||
217 | ) |
|
236 | ) | |
218 | def _(event): |
|
237 | def _(event): | |
219 | event.current_buffer.delete() |
|
238 | event.current_buffer.delete() |
General Comments 0
You need to be logged in to leave comments.
Login now