|
| 1 | +"""implementations of simple readline control sequences |
| 2 | +
|
| 3 | +just the ones that fit the model of transforming the current line |
| 4 | +and the cursor location |
| 5 | +in the order of description at http://www.bigsmoke.us/readline/shortcuts""" |
| 6 | + |
| 7 | +from bpython.scrollfrontend.friendly import NotImplementedError |
| 8 | +import re |
| 9 | +char_sequences = {} |
| 10 | + |
| 11 | +#TODO fix this - should use value in repl. |
| 12 | +# Sadly, this breaks the pure function aspect of backspace! |
| 13 | +INDENT = 4 |
| 14 | + |
| 15 | +#TODO make an object out of this so instances can have keybindings via config |
| 16 | + |
| 17 | +def on(seq): |
| 18 | + def add_to_char_sequences(func): |
| 19 | + char_sequences[seq] = func |
| 20 | + return func |
| 21 | + return add_to_char_sequences |
| 22 | + |
| 23 | +@on('[D') |
| 24 | +@on('') |
| 25 | +@on(chr(2)) |
| 26 | +@on('KEY_LEFT') |
| 27 | +def left_arrow(cursor_offset, line): |
| 28 | + return max(0, cursor_offset - 1), line |
| 29 | + |
| 30 | +@on('[C') |
| 31 | +@on('') |
| 32 | +@on(chr(6)) |
| 33 | +@on('KEY_RIGHT') |
| 34 | +def right_arrow(cursor_offset, line): |
| 35 | + return min(len(line), cursor_offset + 1), line |
| 36 | + |
| 37 | +@on('') |
| 38 | +@on('KEY_HOME') |
| 39 | +def beginning_of_line(cursor_offset, line): |
| 40 | + return 0, line |
| 41 | + |
| 42 | +@on('') |
| 43 | +@on('KEY_END') |
| 44 | +def end_of_line(cursor_offset, line): |
| 45 | + return len(line), line |
| 46 | + |
| 47 | +@on('f') |
| 48 | +@on('\x1bOC') |
| 49 | +def forward_word(cursor_offset, line): |
| 50 | + patt = r"\S\s" |
| 51 | + match = re.search(patt, line[cursor_offset:]+' ') |
| 52 | + delta = match.end() - 1 if match else 0 |
| 53 | + return (cursor_offset + delta, line) |
| 54 | + |
| 55 | +@on('b') |
| 56 | +@on('\x1bOD') |
| 57 | +def back_word(cursor_offset, line): |
| 58 | + return (last_word_pos(line[:cursor_offset]), line) |
| 59 | + |
| 60 | +def last_word_pos(string): |
| 61 | + """returns the start index of the last word of given string""" |
| 62 | + patt = r'\S\s' |
| 63 | + match = re.search(patt, string[::-1]) |
| 64 | + index = match and len(string) - match.end() + 1 |
| 65 | + return index or 0 |
| 66 | + |
| 67 | +@on('[3~') |
| 68 | +@on('KEY_DC') |
| 69 | +def delete(cursor_offset, line): |
| 70 | + return (cursor_offset, |
| 71 | + line[:cursor_offset] + line[cursor_offset+1:]) |
| 72 | + |
| 73 | +@on('') |
| 74 | +@on('') |
| 75 | +@on('KEY_BACKSPACE') |
| 76 | +def backspace(cursor_offset, line): |
| 77 | + if cursor_offset == 0: |
| 78 | + return cursor_offset, line |
| 79 | + if not line[:cursor_offset].strip(): #if just whitespace left of cursor |
| 80 | + front_white = len(line[:cursor_offset]) - len(line[:cursor_offset].lstrip()) |
| 81 | + to_delete = ((front_white - 1) % INDENT) + 1 |
| 82 | + return cursor_offset - to_delete, line[:to_delete] |
| 83 | + return (cursor_offset - 1, |
| 84 | + line[:cursor_offset - 1] + line[cursor_offset:]) |
| 85 | + |
| 86 | +@on('') |
| 87 | +def delete_from_cursor_back(cursor_offset, line): |
| 88 | + return 0, line[cursor_offset:] |
| 89 | + |
| 90 | +@on('') |
| 91 | +def delete_from_cursor_forward(cursor_offset, line): |
| 92 | + return cursor_offset, line[:cursor_offset] |
| 93 | + |
| 94 | +@on('d') |
| 95 | +def delete_rest_of_word(cursor_offset, line): |
| 96 | + raise NotImplementedError() |
| 97 | + |
| 98 | +@on('') |
| 99 | +def delete_word_to_cursor(cursor_offset, line): |
| 100 | + raise NotImplementedError() |
| 101 | + |
| 102 | +@on('y') |
| 103 | +def yank_prev_prev_killed_text(cursor_offset, line): |
| 104 | + raise NotImplementedError() |
| 105 | + |
| 106 | +@on('') |
| 107 | +def transpose_character_before_cursor(cursor_offset, line): |
| 108 | + raise NotImplementedError() |
| 109 | + |
| 110 | +@on('t') |
| 111 | +def transpose_word_before_cursor(cursor_offset, line): |
| 112 | + raise NotImplementedError() |
| 113 | + |
| 114 | +# bonus functions (not part of readline) |
| 115 | + |
| 116 | +@on('\x1b\x7f') |
| 117 | +@on('\xff') |
| 118 | +def delete_word_from_cursor_back(cursor_offset, line): |
| 119 | + raise NotImplementedError() |
| 120 | + |
| 121 | +def get_updated_char_sequences(key_dispatch, config): |
| 122 | + updated_char_sequences = dict(char_sequences) |
| 123 | + updated_char_sequences[key_dispatch[config.delete_key]] = backspace |
| 124 | + updated_char_sequences[key_dispatch[config.clear_word_key]] = delete_word_to_cursor |
| 125 | + updated_char_sequences[key_dispatch[config.clear_line_key]] = delete_from_cursor_back |
| 126 | + return updated_char_sequences |
| 127 | + |
| 128 | +if __name__ == '__main__': |
| 129 | + import doctest; doctest.testmod() |
| 130 | + from pprint import pprint |
| 131 | + pprint(char_sequences) |
| 132 | + |
| 133 | + |
0 commit comments