Show More
@@ -85,6 +85,120 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 | focused_insert = (vi_insert_mode | emacs_insert_mode) & has_focus(DEFAULT_BUFFER) | |||
|
89 | _preceding_text_cache = {} | |||
|
90 | _following_text_cache = {} | |||
|
91 | ||||
|
92 | ||||
|
93 | def preceding_text(pattern): | |||
|
94 | try: | |||
|
95 | return _preceding_text_cache[pattern] | |||
|
96 | except KeyError: | |||
|
97 | pass | |||
|
98 | m = re.compile(pattern) | |||
|
99 | ||||
|
100 | def _preceding_text(): | |||
|
101 | app = get_app() | |||
|
102 | return bool(m.match(app.current_buffer.document.current_line_before_cursor)) | |||
|
103 | ||||
|
104 | condition = Condition(_preceding_text) | |||
|
105 | _preceding_text_cache[pattern] = condition | |||
|
106 | return condition | |||
|
107 | ||||
|
108 | ||||
|
109 | def following_text(pattern): | |||
|
110 | try: | |||
|
111 | return _following_text_cache[pattern] | |||
|
112 | except KeyError: | |||
|
113 | pass | |||
|
114 | m = re.compile(pattern) | |||
|
115 | ||||
|
116 | def _following_text(): | |||
|
117 | app = get_app() | |||
|
118 | return bool(m.match(app.current_buffer.document.current_line_after_cursor)) | |||
|
119 | ||||
|
120 | condition = Condition(_following_text) | |||
|
121 | _following_text_cache[pattern] = condition | |||
|
122 | return condition | |||
|
123 | ||||
|
124 | ||||
|
125 | # auto match | |||
|
126 | @kb.add('(', filter=focused_insert & following_text(r"[,)}\]]|$")) | |||
|
127 | def _(event): | |||
|
128 | event.current_buffer.insert_text("()") | |||
|
129 | event.current_buffer.cursor_left() | |||
|
130 | ||||
|
131 | @kb.add('[', filter=focused_insert & following_text(r"[,)}\]]|$")) | |||
|
132 | def _(event): | |||
|
133 | event.current_buffer.insert_text("[]") | |||
|
134 | event.current_buffer.cursor_left() | |||
|
135 | ||||
|
136 | @kb.add('{', filter=focused_insert & following_text(r"[,)}\]]|$")) | |||
|
137 | def _(event): | |||
|
138 | event.current_buffer.insert_text("{}") | |||
|
139 | event.current_buffer.cursor_left() | |||
|
140 | ||||
|
141 | @kb.add('"', filter=focused_insert & following_text(r"[,)}\]]|$")) | |||
|
142 | def _(event): | |||
|
143 | event.current_buffer.insert_text('""') | |||
|
144 | event.current_buffer.cursor_left() | |||
|
145 | ||||
|
146 | @kb.add("'", filter=focused_insert & following_text(r"[,)}\]]|$")) | |||
|
147 | def _(event): | |||
|
148 | event.current_buffer.insert_text("''") | |||
|
149 | event.current_buffer.cursor_left() | |||
|
150 | ||||
|
151 | # raw string | |||
|
152 | @kb.add('(', filter=focused_insert & preceding_text(r".*(r|R)[\"'](-*)$")) | |||
|
153 | def _(event): | |||
|
154 | matches = re.match(r".*(r|R)[\"'](-*)", event.current_buffer.document.current_line_before_cursor) | |||
|
155 | dashes = matches.group(2) or "" | |||
|
156 | event.current_buffer.insert_text("()" + dashes) | |||
|
157 | event.current_buffer.cursor_left(len(dashes) + 1) | |||
|
158 | ||||
|
159 | @kb.add('[', filter=focused_insert & preceding_text(r".*(r|R)[\"'](-*)$")) | |||
|
160 | def _(event): | |||
|
161 | matches = re.match(r".*(r|R)[\"'](-*)", event.current_buffer.document.current_line_before_cursor) | |||
|
162 | dashes = matches.group(2) or "" | |||
|
163 | event.current_buffer.insert_text("[]" + dashes) | |||
|
164 | event.current_buffer.cursor_left(len(dashes) + 1) | |||
|
165 | ||||
|
166 | @kb.add('{', filter=focused_insert & preceding_text(r".*(r|R)[\"'](-*)$")) | |||
|
167 | def _(event): | |||
|
168 | matches = re.match(r".*(r|R)[\"'](-*)", event.current_buffer.document.current_line_before_cursor) | |||
|
169 | dashes = matches.group(2) or "" | |||
|
170 | event.current_buffer.insert_text("{}" + dashes) | |||
|
171 | event.current_buffer.cursor_left(len(dashes) + 1) | |||
|
172 | ||||
|
173 | @kb.add('"', filter=focused_insert & preceding_text(r".*(r|R)$")) | |||
|
174 | def _(event): | |||
|
175 | event.current_buffer.insert_text('""') | |||
|
176 | event.current_buffer.cursor_left() | |||
|
177 | ||||
|
178 | @kb.add("'", filter=focused_insert & preceding_text(r".*(r|R)$")) | |||
|
179 | def _(event): | |||
|
180 | event.current_buffer.insert_text("''") | |||
|
181 | event.current_buffer.cursor_left() | |||
|
182 | ||||
|
183 | # just move cursor | |||
|
184 | @kb.add(')', filter=focused_insert & following_text(r"^\)")) | |||
|
185 | @kb.add(']', filter=focused_insert & following_text(r"^\]")) | |||
|
186 | @kb.add('}', filter=focused_insert & following_text(r"^\}")) | |||
|
187 | @kb.add('"', filter=focused_insert & following_text("^\"")) | |||
|
188 | @kb.add("'", filter=focused_insert & following_text("^'")) | |||
|
189 | def _(event): | |||
|
190 | event.current_buffer.cursor_right() | |||
|
191 | ||||
|
192 | @kb.add('backspace', filter=focused_insert & preceding_text(r".*\($") & following_text(r"^\)")) | |||
|
193 | @kb.add('backspace', filter=focused_insert & preceding_text(r".*\[$") & following_text(r"^\]")) | |||
|
194 | @kb.add('backspace', filter=focused_insert & preceding_text(r".*\{$") & following_text(r"^\}")) | |||
|
195 | @kb.add('backspace', filter=focused_insert & preceding_text('.*"$') & following_text('^"')) | |||
|
196 | @kb.add('backspace', filter=focused_insert & preceding_text(r".*'$") & following_text(r"^'")) | |||
|
197 | def _(event): | |||
|
198 | event.current_buffer.delete() | |||
|
199 | event.current_buffer.delete_before_cursor() | |||
|
200 | ||||
|
201 | ||||
88 | if shell.display_completions == 'readlinelike': |
|
202 | if shell.display_completions == 'readlinelike': | |
89 | kb.add('c-i', filter=(has_focus(DEFAULT_BUFFER) |
|
203 | kb.add('c-i', filter=(has_focus(DEFAULT_BUFFER) | |
90 | & ~has_selection |
|
204 | & ~has_selection |
General Comments 0
You need to be logged in to leave comments.
Login now