| 1 |
""" |
| 2 |
GNU Readline-like line editing functions |
| 3 |
|
| 4 |
Only two public functions are exposed: |
| 5 |
|
| 6 |
- edit(), which returns the result of a key input on a line |
| 7 |
- keys(), which returns key names that correspond to commands |
| 8 |
|
| 9 |
Private functions corresponding to each implemented Readline command are |
| 10 |
accessible by replacing hyphens in the command name with underscores and |
| 11 |
prefixing an underscore, as in _end_of_line() for `end-of-line'. |
| 12 |
|
| 13 |
This module is especially well-suited to interfacing with Urwid, since |
| 14 |
the edit() function's `key' argument uses the same format that Urwid |
| 15 |
widget keypress() methods receive. |
| 16 |
|
| 17 |
Implemented commands and their correspondings keys are as follows: |
| 18 |
|
| 19 |
backward-char ctrl b, left |
| 20 |
backward-delete-char ctrl h, backspace |
| 21 |
backward-kill-word ctrl meta h, meta backspace |
| 22 |
backward-word meta b, meta left |
| 23 |
beginning-of-line ctrl a, home |
| 24 |
capitalize-word meta c |
| 25 |
delete-char ctrl d, delete |
| 26 |
delete-horizontal-space meta \\ |
| 27 |
downcase-word meta l |
| 28 |
end-of-line ctrl e, end |
| 29 |
forward-char ctrl f, right |
| 30 |
forward-word meta f, meta right |
| 31 |
kill-line ctrl k |
| 32 |
kill-word meta d, meta delete |
| 33 |
transpose-chars ctrl t |
| 34 |
transpose-words meta t |
| 35 |
unix-line-discard ctrl u |
| 36 |
unix-word-rubout ctrl w |
| 37 |
upcase-word meta u |
| 38 |
|
| 39 |
For more information about each command, see readline(3) or use pydoc on |
| 40 |
the specific private functions. |
| 41 |
""" |
| 42 |
|
| 43 |
__all__ = ['edit', 'keys'] |
| 44 |
|
| 45 |
|
| 46 |
def _backward_char(text, pos): |
| 47 |
"""Move pos back a character.""" |
| 48 |
return text, max(0, pos - 1) |
| 49 |
|
| 50 |
|
| 51 |
def _backward_delete_char(text, pos): |
| 52 |
"""Delete the character behind pos.""" |
| 53 |
if pos == 0: |
| 54 |
return text, pos |
| 55 |
return text[:pos - 1] + text[pos:], pos - 1 |
| 56 |
|
| 57 |
|
| 58 |
def _backward_kill_word(text, pos): |
| 59 |
"""" |
| 60 |
Kill the word behind pos. Word boundaries are the same as those |
| 61 |
used by _backward_word. |
| 62 |
""" |
| 63 |
text, new_pos = _backward_word(text, pos) |
| 64 |
return text[:new_pos] + text[pos:], new_pos |
| 65 |
|
| 66 |
|
| 67 |
def _backward_word(text, pos): |
| 68 |
""" |
| 69 |
Move pos back to the start of the current or previous word. Words |
| 70 |
are composed of alphanumeric characters (letters and digits). |
| 71 |
""" |
| 72 |
while pos > 0 and not text[pos - 1].isalnum(): |
| 73 |
pos -= 1 |
| 74 |
while pos > 0 and text[pos - 1].isalnum(): |
| 75 |
pos -= 1 |
| 76 |
return text, pos |
| 77 |
|
| 78 |
|
| 79 |
def _beginning_of_line(text, pos): |
| 80 |
"""Move pos to the start of text.""" |
| 81 |
return text, 0 |
| 82 |
|
| 83 |
|
| 84 |
def _capitalize_word(text, pos): |
| 85 |
"""Capitalize the current (or following) word.""" |
| 86 |
while pos < len(text) and not text[pos].isalnum(): |
| 87 |
pos += 1 |
| 88 |
if pos < len(text): |
| 89 |
text = text[:pos] + text[pos].upper() + text[pos + 1:] |
| 90 |
while pos < len(text) and text[pos].isalnum(): |
| 91 |
pos += 1 |
| 92 |
return text, pos |
| 93 |
|
| 94 |
|
| 95 |
def _delete_char(text, pos): |
| 96 |
"""Delete the character at pos.""" |
| 97 |
return text[:pos] + text[pos + 1:], pos |
| 98 |
|
| 99 |
|
| 100 |
def _delete_horizontal_space(text, pos): |
| 101 |
"""Delete all spaces and tabs around pos.""" |
| 102 |
while pos > 0 and text[pos - 1].isspace(): |
| 103 |
pos -= 1 |
| 104 |
end_pos = pos |
| 105 |
while end_pos < len(text) and text[end_pos].isspace(): |
| 106 |
end_pos += 1 |
| 107 |
return text[:pos] + text[end_pos:], pos |
| 108 |
|
| 109 |
|
| 110 |
def _downcase_word(text, pos): |
| 111 |
"""Lowercase the current (or following) word.""" |
| 112 |
text, new_pos = _forward_word(text, pos) |
| 113 |
return text[:pos] + text[pos:new_pos].lower() + text[new_pos:], new_pos |
| 114 |
|
| 115 |
|
| 116 |
def _end_of_line(text, pos): |
| 117 |
"""Move pos to the end of text.""" |
| 118 |
return text, len(text) |
| 119 |
|
| 120 |
|
| 121 |
def _forward_char(text, pos): |
| 122 |
"""Move pos forward a character.""" |
| 123 |
return text, min(pos + 1, len(text)) |
| 124 |
|
| 125 |
|
| 126 |
def _forward_word(text, pos): |
| 127 |
""" |
| 128 |
Move pos forward to the end of the next word. Words are composed of |
| 129 |
alphanumeric characters (letters and digits). |
| 130 |
""" |
| 131 |
while pos < len(text) and not text[pos].isalnum(): |
| 132 |
pos += 1 |
| 133 |
while pos < len(text) and text[pos].isalnum(): |
| 134 |
pos += 1 |
| 135 |
return text, pos |
| 136 |
|
| 137 |
|
| 138 |
def _kill_line(text, pos): |
| 139 |
"""Kill from pos to the end of text.""" |
| 140 |
return text[:pos], pos |
| 141 |
|
| 142 |
|
| 143 |
def _kill_word(text, pos): |
| 144 |
""" |
| 145 |
Kill from pos to the end of the current word, or if between words, |
| 146 |
to the end of the next word. Word boundaries are the same as those |
| 147 |
used by _forward_word. |
| 148 |
""" |
| 149 |
text, end_pos = _forward_word(text, pos) |
| 150 |
return text[:pos] + text[end_pos:], pos |
| 151 |
|
| 152 |
|
| 153 |
def _transpose_chars(text, pos): |
| 154 |
""" |
| 155 |
Drag the character before pos forward over the character at pos, |
| 156 |
moving pos forward as well. If pos is at the end of text, then this |
| 157 |
transposes the two characters before pos. |
| 158 |
""" |
| 159 |
if len(text) < 2 or pos == 0: |
| 160 |
return text, pos |
| 161 |
if pos == len(text): |
| 162 |
return text[:pos - 2] + text[pos - 1] + text[pos - 2], pos |
| 163 |
return text[:pos - 1] + text[pos] + text[pos - 1] + text[pos + 1:], pos + 1 |
| 164 |
|
| 165 |
|
| 166 |
def _transpose_words(text, pos): |
| 167 |
""" |
| 168 |
Drag the word before pos past the word after pos, moving pos over |
| 169 |
that word as well. If pos is at the end of text, this transposes the |
| 170 |
last two words in text. |
| 171 |
""" |
| 172 |
text, end2 = _forward_word(text, pos) |
| 173 |
text, start2 = _backward_word(text, end2) |
| 174 |
text, start1 = _backward_word(text, start2) |
| 175 |
text, end1 = _forward_word(text, start1) |
| 176 |
if start1 == start2: |
| 177 |
return text, pos |
| 178 |
return text[:start1] + text[start2:end2] + text[end1:start2:] + \ |
| 179 |
text[start1:end1] + text[end2:], end2 |
| 180 |
|
| 181 |
|
| 182 |
def _unix_line_discard(text, pos): |
| 183 |
"""Kill backward from pos to the beginning of text.""" |
| 184 |
return text[pos:], 0 |
| 185 |
|
| 186 |
|
| 187 |
def _unix_word_rubout(text, pos): |
| 188 |
""" |
| 189 |
Kill the word behind pos, using white space as a word boundary. |
| 190 |
""" |
| 191 |
words = text[:pos].rsplit(None, 1) |
| 192 |
if len(words) < 2: |
| 193 |
return text[pos:], 0 |
| 194 |
else: |
| 195 |
index = text.rfind(words[1], 0, pos) |
| 196 |
return text[:index] + text[pos:], index |
| 197 |
|
| 198 |
|
| 199 |
def _upcase_word(text, pos): |
| 200 |
"""Uppercase the current (or following) word.""" |
| 201 |
text, new_pos = _forward_word(text, pos) |
| 202 |
return text[:pos] + text[pos:new_pos].upper() + text[new_pos:], new_pos |
| 203 |
|
| 204 |
|
| 205 |
_key_bindings = { |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
'backspace': _backward_delete_char, |
| 220 |
'ctrl a': _beginning_of_line, |
| 221 |
'ctrl b': _backward_char, |
| 222 |
'ctrl d': _delete_char, |
| 223 |
'ctrl e': _end_of_line, |
| 224 |
'ctrl f': _forward_char, |
| 225 |
'ctrl h': _backward_delete_char, |
| 226 |
'ctrl k': _kill_line, |
| 227 |
'ctrl meta h': _backward_kill_word, |
| 228 |
'ctrl t': _transpose_chars, |
| 229 |
'ctrl u': _unix_line_discard, |
| 230 |
'ctrl w': _unix_word_rubout, |
| 231 |
'delete': _delete_char, |
| 232 |
'end': _end_of_line, |
| 233 |
'home': _beginning_of_line, |
| 234 |
'left': _backward_char, |
| 235 |
'meta \\': _delete_horizontal_space, |
| 236 |
'meta b': _backward_word, |
| 237 |
'meta backspace': _backward_kill_word, |
| 238 |
'meta c': _capitalize_word, |
| 239 |
'meta d': _kill_word, |
| 240 |
'meta delete': _kill_word, |
| 241 |
'meta f': _forward_word, |
| 242 |
'meta l': _downcase_word, |
| 243 |
'meta left': _backward_word, |
| 244 |
'meta right': _forward_word, |
| 245 |
'meta t': _transpose_words, |
| 246 |
'meta u': _upcase_word, |
| 247 |
'right': _forward_char, |
| 248 |
} |
| 249 |
|
| 250 |
_keys = frozenset(_key_bindings.keys()) |
| 251 |
|
| 252 |
|
| 253 |
def edit(text, pos, key): |
| 254 |
""" |
| 255 |
Process a key input in the context of a line, and return the |
| 256 |
resulting text and cursor position. |
| 257 |
|
| 258 |
`text' and `key' must be of type str or unicode, and `pos' must be |
| 259 |
an int in the range [0, len(text)]. |
| 260 |
|
| 261 |
If `key' is in keys(), the corresponding command is executed on the |
| 262 |
line. Otherwise, if `key' is a single character, that character is |
| 263 |
inserted at the cursor position. If neither condition is met, `text' |
| 264 |
and `pos' are returned unmodified. |
| 265 |
""" |
| 266 |
if key in _key_bindings: |
| 267 |
return _key_bindings[key](text, pos) |
| 268 |
elif len(key) == 1: |
| 269 |
return text[:pos] + key + text[pos:], pos + 1 |
| 270 |
else: |
| 271 |
return text, pos |
| 272 |
|
| 273 |
|
| 274 |
def keys(): |
| 275 |
""" |
| 276 |
Return a frozenset of strings that describe key inputs corresponding |
| 277 |
to line editing commands. |
| 278 |
""" |
| 279 |
return _keys |