/
key_bindings.py
395 lines (307 loc) · 13.5 KB
/
key_bindings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
from __future__ import unicode_literals
import re
import os
from prompt_toolkit.application.current import get_app
from prompt_toolkit.application.run_in_terminal import run_in_terminal
from prompt_toolkit.eventloop import From, ensure_future
from prompt_toolkit.keys import Keys
from prompt_toolkit.key_binding.key_bindings import KeyBindings
from prompt_toolkit.filters import Condition, has_focus, \
emacs_insert_mode, vi_insert_mode, in_paste_mode, has_completions, completion_is_selected
from prompt_toolkit.filters import (
emacs_mode,
has_selection
)
from prompt_toolkit.enums import DEFAULT_BUFFER
from radian.settings import radian_settings as settings
from radian.document import cursor_in_string
from radian import get_app as get_radian_app
from rchitect.interface import roption
from six import text_type
default_focused = has_focus(DEFAULT_BUFFER)
insert_mode = vi_insert_mode | emacs_insert_mode
_prompt_mode_cache = {}
def prompt_mode(mode):
try:
return _prompt_mode_cache[mode]
except KeyError:
pass
app = get_radian_app()
condition = Condition(lambda: app.session.current_mode_name == mode)
_prompt_mode_cache[mode] = condition
return condition
_preceding_text_cache = {}
_following_text_cache = {}
def preceding_text(pattern):
try:
return _preceding_text_cache[pattern]
except KeyError:
pass
m = re.compile(pattern)
def _preceding_text():
app = get_app()
return bool(m.match(app.current_buffer.document.current_line_before_cursor))
condition = Condition(_preceding_text)
_preceding_text_cache[pattern] = condition
return condition
def following_text(pattern):
try:
return _following_text_cache[pattern]
except KeyError:
pass
m = re.compile(pattern)
def _following_text():
app = get_app()
return bool(m.match(app.current_buffer.document.current_line_after_cursor))
condition = Condition(_following_text)
_following_text_cache[pattern] = condition
return condition
@Condition
def string_scope():
app = get_app()
return cursor_in_string(app.current_buffer.document)
@Condition
def cursor_at_begin():
return get_app().current_buffer.cursor_position == 0
@Condition
def cursor_at_end():
app = get_app()
return app.current_buffer.cursor_position == len(app.current_buffer.text)
@Condition
def text_is_empty():
app = get_app()
return not app.current_buffer.text
@Condition
def auto_indentation():
return settings.auto_indentation
@Condition
def auto_match():
return settings.auto_match
@Condition
def has_complete_index():
app = get_app()
cs = app.current_buffer.complete_state
return cs and cs.complete_index is not None
def if_no_repeat(event):
return not event.is_repeat
def commit_text(event, text, add_history=True):
app = get_radian_app()
app.session.add_history = add_history
buf = event.current_buffer
buf.text = text
buf.validate_and_handle()
def newline(event, chars=["{", "[", "("]):
should_indent = event.current_buffer.document.char_before_cursor in chars
copy_margin = not in_paste_mode() and settings.auto_indentation
event.current_buffer.newline(copy_margin=copy_margin)
if should_indent and settings.auto_indentation:
tab_size = settings.tab_size
event.current_buffer.insert_text(" " * tab_size)
def create_prompt_key_bindings(prase_text_complete):
kb = KeyBindings()
handle = kb.add
@Condition
def prase_complete():
app = get_app()
return prase_text_complete(app.current_buffer.text)
@handle('c-j', filter=insert_mode & default_focused)
@handle('enter', filter=insert_mode & default_focused)
def _(event):
newline(event)
@handle('c-j', filter=insert_mode & default_focused & prase_complete)
@handle('enter', filter=insert_mode & default_focused & prase_complete)
def _(event):
event.current_buffer.validate_and_handle()
@handle('c-j', filter=insert_mode & default_focused & auto_match & preceding_text(r".*\{$") & following_text(r"^\}"))
@handle('enter', filter=insert_mode & default_focused & auto_match & preceding_text(r".*\{$") & following_text(r"^\}"))
def _(event):
copy_margin = not in_paste_mode() and settings.auto_indentation
event.current_buffer.newline(copy_margin=copy_margin)
if settings.auto_indentation:
tab_size = settings.tab_size
event.current_buffer.insert_text(" " * tab_size)
event.current_buffer.insert_text("\n")
event.current_buffer.cursor_position -= 1
# auto match
@handle('(', filter=insert_mode & default_focused & auto_match & following_text(r"[,)}\]]|$") & ~string_scope)
def _(event):
event.current_buffer.insert_text("()")
event.current_buffer.cursor_left()
@handle('[', filter=insert_mode & default_focused & auto_match & following_text(r"[,)}\]]|$") & ~string_scope)
def _(event):
event.current_buffer.insert_text("[]")
event.current_buffer.cursor_left()
@handle('{', filter=insert_mode & default_focused & auto_match & following_text(r"[,)}\]]|$") & ~string_scope)
def _(event):
event.current_buffer.insert_text("{}")
event.current_buffer.cursor_left()
@handle('"', filter=insert_mode & default_focused & auto_match & following_text(r"[,)}\]]|$") & ~string_scope)
def _(event):
event.current_buffer.insert_text('""')
event.current_buffer.cursor_left()
@handle("'", filter=insert_mode & default_focused & auto_match & following_text(r"[,)}\]]|$") & ~string_scope)
def _(event):
event.current_buffer.insert_text("''")
event.current_buffer.cursor_left()
# raw string
@handle('(', filter=insert_mode & default_focused & auto_match & preceding_text(r".*(r|R)[\"'](-*)$"))
def _(event):
matches = re.match(r".*(r|R)[\"'](-*)", event.current_buffer.document.current_line_before_cursor)
dashes = matches.group(2) or ""
event.current_buffer.insert_text("()" + dashes)
event.current_buffer.cursor_left(len(dashes) + 1)
@handle('[', filter=insert_mode & default_focused & auto_match & preceding_text(r".*(r|R)[\"'](-*)$"))
def _(event):
matches = re.match(r".*(r|R)[\"'](-*)", event.current_buffer.document.current_line_before_cursor)
dashes = matches.group(2) or ""
event.current_buffer.insert_text("[]" + dashes)
event.current_buffer.cursor_left(len(dashes) + 1)
@handle('{', filter=insert_mode & default_focused & auto_match & preceding_text(r".*(r|R)[\"'](-*)$"))
def _(event):
matches = re.match(r".*(r|R)[\"'](-*)", event.current_buffer.document.current_line_before_cursor)
dashes = matches.group(2) or ""
event.current_buffer.insert_text("{}" + dashes)
event.current_buffer.cursor_left(len(dashes) + 1)
@handle('"', filter=insert_mode & default_focused & auto_match & preceding_text(r".*(r|R)$") & ~string_scope)
def _(event):
event.current_buffer.insert_text('""')
event.current_buffer.cursor_left()
@handle("'", filter=insert_mode & default_focused & auto_match & preceding_text(r".*(r|R)$") & ~string_scope)
def _(event):
event.current_buffer.insert_text("''")
event.current_buffer.cursor_left()
# just move cursor
@handle(')', filter=insert_mode & default_focused & auto_match & following_text(r"^\)"))
@handle(']', filter=insert_mode & default_focused & auto_match & following_text(r"^\]"))
@handle('}', filter=insert_mode & default_focused & auto_match & following_text(r"^\}"))
@handle('"', filter=insert_mode & default_focused & auto_match & following_text("^\""))
@handle("'", filter=insert_mode & default_focused & auto_match & following_text("^'"))
def _(event):
event.current_buffer.cursor_right()
@handle('backspace', filter=insert_mode & default_focused & auto_match & preceding_text(r".*\($") & following_text(r"^\)"))
@handle('backspace', filter=insert_mode & default_focused & auto_match & preceding_text(r".*\[$") & following_text(r"^\]"))
@handle('backspace', filter=insert_mode & default_focused & auto_match & preceding_text(r".*\{$") & following_text(r"^\}"))
@handle('backspace', filter=insert_mode & default_focused & auto_match & preceding_text('.*"$') & following_text('^"'))
@handle('backspace', filter=insert_mode & default_focused & auto_match & preceding_text(r".*'$") & following_text(r"^'"))
def _(event):
event.current_buffer.delete()
event.current_buffer.delete_before_cursor()
# indentation
@handle('}', filter=insert_mode & default_focused & auto_indentation & preceding_text(r"^\s*$"))
@handle(']', filter=insert_mode & default_focused & auto_indentation & preceding_text(r"^\s*$"))
@handle(')', filter=insert_mode & default_focused & auto_indentation & preceding_text(r"^\s*$"))
def _(event):
text = event.current_buffer.document.text_before_cursor
textList = text.split("\n")
if len(textList) >= 2:
m = re.match(r"^\s*$", textList[-1])
if m:
current_indentation = m.group(0)
previous_indentation = re.match(r"^\s*", textList[-2]).group(0)
tab_size = settings.tab_size
if len(current_indentation) >= settings.tab_size and \
current_indentation == previous_indentation:
event.current_buffer.delete_before_cursor(tab_size)
event.current_buffer.insert_text(event.data)
@handle('backspace', filter=insert_mode & default_focused & preceding_text(r"^\s+$"))
def _(event):
tab_size = settings.tab_size
buf = event.current_buffer
leading_spaces = len(buf.document.text_before_cursor)
buf.delete_before_cursor(min(tab_size, leading_spaces))
@handle('tab', filter=insert_mode & default_focused & preceding_text(r"^\s*$"))
def _(event):
tab_size = settings.tab_size
event.current_buffer.insert_text(" " * tab_size)
# bracketed paste
@handle(Keys.BracketedPaste, filter=default_focused)
def _(event):
data = event.data
data = data.replace('\r\n', '\n')
data = data.replace('\r', '\n')
should_eval = data and data[-1] == "\n" and \
len(event.current_buffer.document.text_after_cursor) == 0
# todo: allow partial prase complete
if should_eval and prase_text_complete(data):
data = data.rstrip("\n")
event.current_buffer.insert_text(data)
event.current_buffer.validate_and_handle()
else:
event.current_buffer.insert_text(data)
return kb
# keybinds for both r mond and browse mode
def create_r_key_bindings(prase_text_complete):
kb = create_prompt_key_bindings(prase_text_complete)
handle = kb.add
# r mode
@handle(';', filter=insert_mode & default_focused & cursor_at_begin)
def _(event):
app = get_radian_app()
app.session.change_mode("shell")
return kb
def create_shell_key_bindings():
kb = KeyBindings()
handle = kb.add
# shell mode
@handle(
'backspace',
filter=insert_mode & default_focused & cursor_at_begin,
save_before=if_no_repeat)
def _(event):
app = get_radian_app()
app.session.change_mode("r")
@handle('c-j', filter=insert_mode & default_focused)
@handle('enter', filter=insert_mode & default_focused)
def _(event):
event.current_buffer.validate_and_handle()
return kb
def create_key_bindings(editor=""):
kb = KeyBindings()
handle = kb.add
# emit completion
@handle('c-j', filter=insert_mode & default_focused & completion_is_selected)
@handle('enter', filter=insert_mode & default_focused & completion_is_selected)
def _(event):
event.current_buffer.complete_state = None
# cancel completion
@handle('c-c', filter=default_focused & has_completions)
def _(event):
event.current_buffer.cancel_completion()
# new line
@handle('escape', 'enter', filter=emacs_insert_mode)
def _(event):
if event.current_buffer.text:
copy_margin = not in_paste_mode() and settings.auto_indentation
event.current_buffer.newline(copy_margin=copy_margin)
@handle('c-x', 'c-e', filter=emacs_mode & ~has_selection)
def _(event):
# match R behavior
editor = roption("editor")
if not editor or not isinstance(editor, text_type):
if 'VISUAL' in os.environ:
editor = os.environ['VISUAL']
elif 'EDITOR' in os.environ:
editor = os.environ['EDITOR']
if not editor:
editor = "vi"
buff = event.current_buffer
if editor:
orig_visual = os.environ['VISUAL'] if 'VISUAL' in os.environ else None
os.environ['VISUAL'] = editor
buff.open_in_editor()
if editor:
def run():
def cleanup():
if orig_visual:
os.environ['VISUAL'] = orig_visual
else:
del os.environ['VISUAL']
yield From(run_in_terminal(cleanup, in_executor=True))
ensure_future(run())
return kb
def map_key(key, value, mode="r", filter_str=""):
app = get_radian_app()
kb = app.session.modes[mode].prompt_key_bindings
@kb.add(*key, filter=insert_mode & default_focused, eager=True)
def _(event):
event.current_buffer.insert_text(value)