Skip to content
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

64 changes: 64 additions & 0 deletions lib/mp-readline/readline.c
Expand Up @@ -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
Expand All @@ -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 };
Expand Down Expand Up @@ -98,6 +102,33 @@ typedef struct _readline_t {

STATIC readline_t rl;

size_t get_word_cursor_pos(bool direction) {
Copy link
Member

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 an int taking value 1 or -1, then there's no need to convert it to cursor_pos_step, nor have FORWARD and BACKWARD constants.

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

char c should go below in the while loop.

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is not needed, can just be an else (saves code size).

in_leading_nonalphanum = false;
}
if (direction == FORWARD) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this check, just unconditionally test if cursor_pos >= rl.line->len

if (cursor_pos >= rl.line->len) {
break;
}
} else if (cursor_pos <= 0) {
break;
}
cursor_pos += cursor_pos_step;
}
return cursor_pos;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can probably return cursor_pos - rl.cursor_pos here, since all callers just want the number of chars skipped.

}

int readline_process_char(int c) {
size_t last_line_len = rl.line->len;
int redraw_step_back = 0;
Expand Down Expand Up @@ -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;
Expand Down