Skip to content

Commit

Permalink
[readline] Add CTRL-W shortcut to remove a word
Browse files Browse the repository at this point in the history
Signed-off-by: Marin Hannache <git@mareo.fr>
Modified-by: Michael Brown <mcb30@ipxe.org>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
Mareo authored and mcb30 committed Aug 19, 2014
1 parent 8b2942a commit 8ab9f3c
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/hci/editstring.c
Expand Up @@ -21,6 +21,7 @@ FILE_LICENCE ( GPL2_OR_LATER );

#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <ipxe/keys.h>
#include <ipxe/editstring.h>

Expand All @@ -37,6 +38,8 @@ static void insert_character ( struct edit_string *string,
unsigned int character ) __nonnull;
static void delete_character ( struct edit_string *string ) __nonnull;
static void backspace ( struct edit_string *string ) __nonnull;
static void previous_word ( struct edit_string *string ) __nonnull;
static void kill_word ( struct edit_string *string ) __nonnull;
static void kill_sol ( struct edit_string *string ) __nonnull;
static void kill_eol ( struct edit_string *string ) __nonnull;

Expand Down Expand Up @@ -110,10 +113,37 @@ static void backspace ( struct edit_string *string ) {
}
}

/**
* Move to start of previous word
*
* @v string Editable string
*/
static void previous_word ( struct edit_string *string ) {
while ( string->cursor &&
isspace ( string->buf[ string->cursor - 1 ] ) ) {
string->cursor--;
}
while ( string->cursor &&
( ! isspace ( string->buf[ string->cursor - 1 ] ) ) ) {
string->cursor--;
}
}

/**
* Delete to end of previous word
*
* @v string Editable string
*/
static void kill_word ( struct edit_string *string ) {
size_t old_cursor = string->cursor;
previous_word ( string );
insert_delete ( string, ( old_cursor - string->cursor ), NULL );
}

/**
* Delete to start of line
*
* @v string Editable string
* @v string Editable string
*/
static void kill_sol ( struct edit_string *string ) {
size_t old_cursor = string->cursor;
Expand Down Expand Up @@ -181,6 +211,10 @@ int edit_string ( struct edit_string *string, int key ) {
/* Delete character */
delete_character ( string );
break;
case CTRL_W:
/* Delete word */
kill_word ( string );
break;
case CTRL_U:
/* Delete to start of line */
kill_sol ( string );
Expand Down

0 comments on commit 8ab9f3c

Please sign in to comment.