Skip to content

lib/mp-readline/readline.c: Added backward/forward/kill-word support. #5024

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

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions lib/mp-readline/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#include "py/mphal.h"
#include "lib/mp-readline/readline.h"

#if MICROPY_REPL_EMACS_KEYS
#include "py/unicode.h"
#endif

#if 0 // print debugging info
#define DEBUG_PRINT (1)
#define DEBUG_printf printf
Expand Down Expand Up @@ -98,6 +102,38 @@ typedef struct _readline_t {

STATIC readline_t rl;

#if MICROPY_REPL_EMACS_KEYS
#define FORWARD true
#define BACKWARD false

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.

}
#endif

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 +254,41 @@ int readline_process_char(int c) {
case '[':
rl.escape_seq = ESEQ_ESC_BRACKET;
break;
#if MICROPY_REPL_EMACS_KEYS
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;
}
#endif
case 'O':
rl.escape_seq = ESEQ_ESC_O;
break;
Expand Down
4 changes: 4 additions & 0 deletions lib/mp-readline/readline.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ void readline_init(vstr_t *line, const char *prompt);
void readline_note_newline(const char *prompt);
int readline_process_char(int c);

#if MICROPY_REPL_EMACS_KEYS
size_t get_word_cursor_pos(bool direction);
#endif

#endif // MICROPY_INCLUDED_LIB_MP_READLINE_READLINE_H
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy