New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib/mp-readline/readline.c: Added backward/forward/kill-word support. #5024
Changes from 2 commits
37a99fe
00b2a0e
489d48a
db7aa34
a2ce440
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
#include "py/mpstate.h" | ||
#include "py/repl.h" | ||
#include "py/mphal.h" | ||
#include "py/unicode.h" | ||
#include "lib/mp-readline/readline.h" | ||
|
||
#if 0 // print debugging info | ||
|
@@ -40,6 +41,9 @@ | |
#define DEBUG_printf(...) (void)0 | ||
#endif | ||
|
||
#define FORWARD true | ||
#define BACKWARD false | ||
|
||
#define READLINE_HIST_SIZE (MP_ARRAY_SIZE(MP_STATE_PORT(readline_hist))) | ||
|
||
enum { ESEQ_NONE, ESEQ_ESC, ESEQ_ESC_BRACKET, ESEQ_ESC_BRACKET_DIGIT, ESEQ_ESC_O }; | ||
|
@@ -98,6 +102,33 @@ typedef struct _readline_t { | |
|
||
STATIC readline_t rl; | ||
|
||
size_t get_word_cursor_pos(bool direction) { | ||
char cursor_pos_step = direction == FORWARD ? 1 : -1; | ||
char buf_idx_offset = direction == FORWARD ? 0 : -1; | ||
size_t cursor_pos = rl.cursor_pos; | ||
bool in_leading_nonalphanum = true; | ||
char c; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
while (1) { | ||
c = rl.line->buf[cursor_pos + buf_idx_offset]; | ||
if (!(unichar_isalpha(c) || unichar_isdigit(c))) { | ||
if (!in_leading_nonalphanum) { | ||
break; | ||
} | ||
} else if (in_leading_nonalphanum) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is not needed, can just be an |
||
in_leading_nonalphanum = false; | ||
} | ||
if (direction == FORWARD) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for this check, just unconditionally test if |
||
if (cursor_pos >= rl.line->len) { | ||
break; | ||
} | ||
} else if (cursor_pos <= 0) { | ||
break; | ||
} | ||
cursor_pos += cursor_pos_step; | ||
} | ||
return cursor_pos; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can probably return |
||
} | ||
|
||
int readline_process_char(int c) { | ||
size_t last_line_len = rl.line->len; | ||
int redraw_step_back = 0; | ||
|
@@ -218,6 +249,39 @@ int readline_process_char(int c) { | |
case '[': | ||
rl.escape_seq = ESEQ_ESC_BRACKET; | ||
break; | ||
case 'b': | ||
// backword-word (Alt-b) | ||
redraw_step_back = rl.cursor_pos - get_word_cursor_pos(BACKWARD); | ||
rl.escape_seq = ESEQ_NONE; | ||
break; | ||
case 'f': | ||
// forward-word (Alt-f) | ||
redraw_step_forward = get_word_cursor_pos(FORWARD) - rl.cursor_pos; | ||
rl.escape_seq = ESEQ_NONE; | ||
break; | ||
case 'd': { | ||
// kill-word (Alt-d) | ||
size_t num_chars; | ||
num_chars = get_word_cursor_pos(FORWARD) - rl.cursor_pos; | ||
if (num_chars) { | ||
vstr_cut_out_bytes(rl.line, rl.cursor_pos, num_chars); | ||
redraw_from_cursor = true; | ||
} | ||
rl.escape_seq = ESEQ_NONE; | ||
break; | ||
} | ||
case 127: { | ||
// backward-kill-word (Alt-Backspace) | ||
size_t num_chars; | ||
num_chars = rl.cursor_pos - get_word_cursor_pos(BACKWARD); | ||
if (num_chars) { | ||
vstr_cut_out_bytes(rl.line, rl.cursor_pos - num_chars, num_chars); | ||
redraw_step_back = num_chars; | ||
redraw_from_cursor = true; | ||
} | ||
rl.escape_seq = ESEQ_NONE; | ||
break; | ||
} | ||
case 'O': | ||
rl.escape_seq = ESEQ_ESC_O; | ||
break; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be made a
STATIC
function, since it's local to this file.Also
direction
can be anint
taking value 1 or -1, then there's no need to convert it tocursor_pos_step
, nor haveFORWARD
andBACKWARD
constants.