diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2022-09-18 23:26:49 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2022-09-18 23:26:49 +0200 |
commit | 01cf98e8fbd8411464fcedd97d7400d961f7e1f5 (patch) | |
tree | e806dd11db677e788e4991383fedc4c0c1ee02bc /src/gui | |
parent | d7c0e896b2d00e215cf3616edc16afeaeb60196f (diff) | |
download | weechat-01cf98e8fbd8411464fcedd97d7400d961f7e1f5.zip |
core: add key alt+backspace, change behavior of key ctrl+w (closes #559)
The key ctrl+w now deletes one word until whitespace.
The new key alt+backspace deletes one word (same behavior as ctrl+w in previous
releases).
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/curses/gui-curses-key.c | 3 | ||||
-rw-r--r-- | src/gui/gui-input.c | 84 | ||||
-rw-r--r-- | src/gui/gui-input.h | 1 |
3 files changed, 71 insertions, 17 deletions
diff --git a/src/gui/curses/gui-curses-key.c b/src/gui/curses/gui-curses-key.c index 4f4e4245c..6ca0f48b6 100644 --- a/src/gui/curses/gui-curses-key.c +++ b/src/gui/curses/gui-curses-key.c @@ -93,7 +93,8 @@ gui_key_default_bindings (int context) BIND(/* m-_ */ "meta-_", "/input redo"); BIND(/* <del> */ "meta2-3~", "/input delete_next_char"); BIND(/* ^D */ "ctrl-D", "/input delete_next_char"); - BIND(/* ^W */ "ctrl-W", "/input delete_previous_word"); + BIND(/* ^W */ "ctrl-W", "/input delete_previous_word_whitespace"); + BIND(/* m-<backspace> */ "meta-ctrl-?", "/input delete_previous_word"); BIND(/* ^X */ "ctrl-X", "/input switch_active_buffer"); BIND(/* m-x */ "meta-x", "/input zoom_merged_buffer"); BIND(/* m-d */ "meta-d", "/input delete_next_word"); diff --git a/src/gui/gui-input.c b/src/gui/gui-input.c index e8b6f5593..04b6a3fca 100644 --- a/src/gui/gui-input.c +++ b/src/gui/gui-input.c @@ -95,7 +95,7 @@ gui_input_replace_input (struct t_gui_buffer *buffer, const char *new_input) int size, length; char *input_utf8; - input_utf8 = strdup (new_input); + input_utf8 = strdup ((new_input) ? new_input : ""); if (input_utf8) { utf8_normalize (input_utf8, '?'); @@ -838,13 +838,44 @@ gui_input_delete_next_char (struct t_gui_buffer *buffer) } /* - * Deletes previous word (default key: ctrl-W). + * Delete the range between two positions and copy the content to the + * clipboard. + */ + +void +gui_input_delete_range (struct t_gui_buffer *buffer, + char *start, + char *end) +{ + int size_deleted, length_deleted; + + size_deleted = utf8_next_char (end) - start; + length_deleted = utf8_strnlen (start, size_deleted); + + gui_input_clipboard_copy (start, size_deleted); + + memmove (start, start + size_deleted, strlen (start + size_deleted)); + + if (gui_input_optimize_size ( + buffer, + buffer->input_buffer_size - size_deleted, + buffer->input_buffer_length - length_deleted)) + { + buffer->input_buffer[buffer->input_buffer_size] = '\0'; + buffer->input_buffer_pos -= length_deleted; + } + gui_input_text_changed_modifier_and_signal (buffer, + 1, /* save undo */ + 1); /* stop completion */ +} + +/* + * Deletes previous word (default key: alt-backspace). */ void gui_input_delete_previous_word (struct t_gui_buffer *buffer) { - int length_deleted, size_deleted; char *start, *string; if (buffer->input && (buffer->input_buffer_pos > 0)) @@ -872,24 +903,45 @@ gui_input_delete_previous_word (struct t_gui_buffer *buffer) else string = buffer->input_buffer; - size_deleted = utf8_next_char (start) - string; - length_deleted = utf8_strnlen (string, size_deleted); + gui_input_delete_range (buffer, string, start); + } +} - gui_input_clipboard_copy (string, size_deleted); +/* + * Deletes previous word until whitespace (default key: ctrl-W). + */ - memmove (string, string + size_deleted, strlen (string + size_deleted)); +void +gui_input_delete_previous_word_whitespace (struct t_gui_buffer *buffer) +{ + char *start, *string; - if (gui_input_optimize_size ( - buffer, - buffer->input_buffer_size - size_deleted, - buffer->input_buffer_length - length_deleted)) + if (buffer->input && (buffer->input_buffer_pos > 0)) + { + gui_buffer_undo_snap (buffer); + start = (char *)utf8_add_offset (buffer->input_buffer, + buffer->input_buffer_pos - 1); + string = start; + /* move to the left, skipping whitespace */ + while (string && string_is_whitespace_char (string)) { - buffer->input_buffer[buffer->input_buffer_size] = '\0'; - buffer->input_buffer_pos -= length_deleted; + string = (char *)utf8_prev_char (buffer->input_buffer, string); } - gui_input_text_changed_modifier_and_signal (buffer, - 1, /* save undo */ - 1); /* stop completion */ + /* move to the left until we reach a char which is not whitespace */ + if (string) + { + while (string && !string_is_whitespace_char (string)) + { + string = (char *)utf8_prev_char (buffer->input_buffer, string); + } + } + + if (string) + string = (char *)utf8_next_char (string); + else + string = buffer->input_buffer; + + gui_input_delete_range (buffer, string, start); } } diff --git a/src/gui/gui-input.h b/src/gui/gui-input.h index 6e98674b9..3fafb7357 100644 --- a/src/gui/gui-input.h +++ b/src/gui/gui-input.h @@ -56,6 +56,7 @@ extern void gui_input_search_stop (struct t_gui_buffer *buffer); extern void gui_input_delete_previous_char (struct t_gui_buffer *buffer); extern void gui_input_delete_next_char (struct t_gui_buffer *buffer); extern void gui_input_delete_previous_word (struct t_gui_buffer *buffer); +extern void gui_input_delete_previous_word_whitespace (struct t_gui_buffer *buffer); extern void gui_input_delete_next_word (struct t_gui_buffer *buffer); extern void gui_input_delete_beginning_of_line (struct t_gui_buffer *buffer); extern void gui_input_delete_end_of_line (struct t_gui_buffer *buffer); |