Skip to content

Commit 84568d9

Browse files
fix all curtsies keybindings
1 parent 4ec3690 commit 84568d9

File tree

4 files changed

+46
-58
lines changed

4 files changed

+46
-58
lines changed

bpython/curtsies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def main(args=None, locals_=None, banner=None):
6363
mainloop(config, locals_, banner, interp, paste, interactive=(not exec_args))
6464

6565
def mainloop(config, locals_, banner, interp=None, paste=None, interactive=True):
66-
with curtsies.input.Input(keynames='curses', sigint_event=True) as input_generator:
66+
with curtsies.input.Input(keynames='curtsies', sigint_event=True) as input_generator:
6767
with curtsies.window.CursorAwareWindow(
6868
sys.stdout,
6969
sys.stdin,

bpython/curtsiesfrontend/interaction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def process_event(self, e):
5757
self.add_normal_character(ee if len(ee) == 1 else ee[-1]) #strip control seq
5858
elif e in rl_char_sequences:
5959
self.cursor_offset_in_line, self._current_line = rl_char_sequences[e](self.cursor_offset_in_line, self._current_line)
60-
elif e == "":
60+
elif e == "<Ctrl-c>":
6161
raise KeyboardInterrupt()
62-
elif e == "":
62+
elif e == "<Ctrl-d>":
6363
raise SystemExit()
6464
elif self.in_prompt and e in ("\n", "\r"):
6565
line = self._current_line
@@ -71,7 +71,7 @@ def process_event(self, e):
7171
else:
7272
self.request_greenlet.switch(False)
7373
self.escape()
74-
elif e in ['\x1b']:
74+
elif e in ['<ESC>']:
7575
self.request_greenlet.switch(False)
7676
self.escape()
7777
else: # add normal character

bpython/curtsiesfrontend/manual_readline.py

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,36 @@ def add_to_char_sequences(func):
1717
return func
1818
return add_to_char_sequences
1919

20-
@on('\x1b[D')
21-
@on('\x02')
22-
@on('KEY_LEFT')
20+
@on('<Ctrl-b>')
21+
@on('<LEFT>')
2322
def left_arrow(cursor_offset, line):
2423
return max(0, cursor_offset - 1), line
2524

26-
@on('\x1b[C')
27-
@on('\x06')
28-
@on('KEY_RIGHT')
25+
@on('<Ctrl-f>')
26+
@on('<RIGHT>')
2927
def right_arrow(cursor_offset, line):
3028
return min(len(line), cursor_offset + 1), line
3129

32-
@on('\x01')
33-
@on('KEY_HOME')
30+
@on('<Ctrl-a>')
31+
@on('<HOME>')
3432
def beginning_of_line(cursor_offset, line):
3533
return 0, line
3634

37-
@on('\x05')
38-
@on('KEY_END')
35+
@on('<Ctrl-e>')
36+
@on('<END>')
3937
def end_of_line(cursor_offset, line):
4038
return len(line), line
4139

42-
@on('\x1bf')
43-
@on('\x1bOC')
44-
@on('\x1b[5C')
45-
@on('\x1b[1;5C')
40+
@on('<Esc-f>')
41+
@on('<Ctrl-RIGHT')
4642
def forward_word(cursor_offset, line):
4743
patt = r"\S\s"
4844
match = re.search(patt, line[cursor_offset:]+' ')
4945
delta = match.end() - 1 if match else 0
5046
return (cursor_offset + delta, line)
5147

52-
@on('\x1bb')
53-
@on('\x1bOD')
54-
@on('\x1bB')
55-
@on('\x1b[5D')
56-
@on('\x1b[1;5D')
48+
@on('<Esc-b>')
49+
@on('<Ctrl-LEFT>')
5750
def back_word(cursor_offset, line):
5851
return (last_word_pos(line[:cursor_offset]), line)
5952

@@ -64,15 +57,13 @@ def last_word_pos(string):
6457
index = match and len(string) - match.end() + 1
6558
return index or 0
6659

67-
@on('\x1b[3~')
68-
@on('KEY_DC')
60+
@on('<PADDELETE>')
6961
def delete(cursor_offset, line):
7062
return (cursor_offset,
7163
line[:cursor_offset] + line[cursor_offset+1:])
7264

73-
@on('\x08')
74-
@on('\x7f')
75-
@on('KEY_BACKSPACE')
65+
@on('<Ctrl-h>')
66+
@on('<BACKSPACE>')
7667
def backspace(cursor_offset, line):
7768
if cursor_offset == 0:
7869
return cursor_offset, line
@@ -83,59 +74,59 @@ def backspace(cursor_offset, line):
8374
return (cursor_offset - 1,
8475
line[:cursor_offset - 1] + line[cursor_offset:])
8576

86-
@on('\x15')
77+
@on('<Ctrl-o>')
8778
def delete_from_cursor_back(cursor_offset, line):
8879
return 0, line[cursor_offset:]
8980

90-
@on('\x0b')
81+
@on('<Ctrl-k>')
9182
def delete_from_cursor_forward(cursor_offset, line):
9283
return cursor_offset, line[:cursor_offset]
9384

94-
@on('\x1bd') # option-d
85+
@on('<Esc+d>') # option-d
9586
def delete_rest_of_word(cursor_offset, line):
9687
m = re.search(r'\w\b', line[cursor_offset:])
9788
if not m:
9889
return cursor_offset, line
9990
return cursor_offset, line[:cursor_offset] + line[m.start()+cursor_offset+1:]
10091

101-
@on('\x17')
92+
@on('<Ctrl-w>')
10293
def delete_word_to_cursor(cursor_offset, line):
10394
matches = list(re.finditer(r'\s\S', line[:cursor_offset]))
10495
start = matches[-1].start()+1 if matches else 0
10596
return start, line[:start] + line[cursor_offset:]
10697

107-
@on('\x1by')
98+
@on('<Esc+y>')
10899
def yank_prev_prev_killed_text(cursor_offset, line):
109100
return cursor_offset, line #TODO Not implemented
110101

111-
@on('\x14')
102+
@on('<Ctrl-t>')
112103
def transpose_character_before_cursor(cursor_offset, line):
113104
return (min(len(line), cursor_offset + 1),
114105
line[:cursor_offset-1] +
115106
(line[cursor_offset] if len(line) > cursor_offset else '') +
116107
line[cursor_offset - 1] +
117108
line[cursor_offset+1:])
118109

119-
@on('\x1bt')
110+
@on('<Esc+t>')
120111
def transpose_word_before_cursor(cursor_offset, line):
121112
return cursor_offset, line #TODO Not implemented
122113

123114
# bonus functions (not part of readline)
124115

125-
@on('\x1br')
116+
@on('<Esc+r>')
126117
def delete_line(cursor_offset, line):
127118
return 0, ""
128119

129-
@on('\x1bu')
120+
@on('<Esc-u>')
130121
def uppercase_next_word(cursor_offset, line):
131122
return cursor_offset, line #TODO Not implemented
132123

133-
@on('\x1bc')
124+
@on('<Esc-c>')
134125
def titlecase_next_word(cursor_offset, line):
135126
return cursor_offset, line #TODO Not implemented
136127

137-
@on('\x1b\x7f')
138-
@on('\xff')
128+
@on('<Esc+BACKSPACE>')
129+
@on('<Meta-BACKSPACE>')
139130
def delete_word_from_cursor_back(cursor_offset, line):
140131
"""Whatever my option-delete does in bash on my mac"""
141132
if not line:

bpython/curtsiesfrontend/repl.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def process_event(self, e):
6969
self.current_line = ''
7070
self.cursor_offset = 0
7171
self.repl.run_code_and_maybe_finish()
72-
elif e in ["\x1b"]: #ESC
72+
elif e in ["<ESC>"]:
7373
pass
7474
elif e in ["\n", "\r"]:
7575
line = self.current_line
@@ -342,7 +342,7 @@ def process_event(self, e):
342342
self.update_completion()
343343
return
344344

345-
elif (e in ("KEY_RIGHT", '\x06') and self.config.curtsies_right_arrow_completion
345+
elif (e in ("<RIGHT>", '<Ctrl-f>') and self.config.curtsies_right_arrow_completion
346346
and self.cursor_offset == len(self.current_line)):
347347
self.current_line += self.current_suggestion
348348
self.cursor_offset = len(self.current_line)
@@ -353,14 +353,14 @@ def process_event(self, e):
353353
self.update_completion()
354354

355355
# readline history commands
356-
elif e in ("KEY_UP",) + key_dispatch[self.config.up_one_line_key]:
356+
elif e in ("<UP>",) + key_dispatch[self.config.up_one_line_key]:
357357
self.rl_history.enter(self.current_line)
358358
self.current_line = self.rl_history.back(False,
359359
search=self.config.curtsies_right_arrow_completion)
360360
self.cursor_offset = len(self.current_line)
361361
self.update_completion()
362362

363-
elif e in ("KEY_DOWN",) + key_dispatch[self.config.down_one_line_key]:
363+
elif e in ("<DOWN>",) + key_dispatch[self.config.down_one_line_key]:
364364
self.rl_history.enter(self.current_line)
365365
self.current_line = self.rl_history.forward(False,
366366
search=self.config.curtsies_right_arrow_completion)
@@ -394,33 +394,34 @@ def process_event(self, e):
394394
self.focus_on_subprocess(['less', '-R', tmp.name])
395395
elif e in key_dispatch[self.config.suspend_key]:
396396
raise SystemExit()
397-
elif e in ("",) + key_dispatch[self.config.exit_key]:
397+
elif e in ("<Ctrl-d>",) + key_dispatch[self.config.exit_key]:
398398
if self.current_line == '':
399399
raise SystemExit()
400-
elif e in ("\n", "\r", "PAD_ENTER"):
400+
elif e in ("\n", "\r", "<PADENTER>", "<Ctrl-j>", "<Ctrl-m>"):
401401
self.on_enter()
402402
self.update_completion()
403-
elif e == '\t': # tab
403+
elif e == '<TAB>': # tab
404404
self.on_tab()
405-
elif e in ("KEY_BTAB",): # shift-tab
405+
elif e in ("<Shift-TAB>",):
406406
self.on_tab(back=True)
407407
elif e in key_dispatch[self.config.undo_key]: #ctrl-r for undo
408408
self.undo()
409409
self.update_completion()
410410
elif e in key_dispatch[self.config.save_key]: # ctrl-s for save
411411
g = greenlet.greenlet(self.write2file)
412412
g.switch()
413-
# F8 for pastebin
414-
elif e in key_dispatch[self.config.pastebin_key]:
413+
elif e in key_dispatch[self.config.pastebin_key]: # F8 for pastebin
415414
g = greenlet.greenlet(self.pastebin)
416415
g.switch()
417416
elif e in key_dispatch[self.config.external_editor_key]:
418417
self.send_session_to_external_editor()
419-
#TODO add PAD keys hack as in bpython.cli
420-
elif e in ["\x18"]:
418+
elif e in ["<Ctrl-x>"]:
421419
self.send_current_block_to_external_editor()
422-
elif e in ["\x1b"]: #ESC
420+
elif e in ["<ESC>"]: #ESC
423421
pass
422+
elif e in ["<SPACE>"]:
423+
self.add_normal_character(' ')
424+
self.update_completion()
424425
else:
425426
self.add_normal_character(e)
426427
self.update_completion()
@@ -1006,11 +1007,7 @@ def compress_paste_event(paste_event):
10061007
if not all(paste_event.events[0] == e for e in paste_event.events):
10071008
return None
10081009
event = paste_event.events[0]
1009-
if len(event) > 1:# basically "is there a special curses names for this key?"
1010-
return event
1011-
elif ord(event) < 0x20:
1012-
return event
1013-
elif event == '\x7f':
1010+
if len(event) > 1:# basically "is there a special curtsies names for this key?"
10141011
return event
10151012
else:
10161013
return None

0 commit comments

Comments
 (0)