diff --git a/builtin_commandline.cpp b/builtin_commandline.cpp index ad3cf0c81..fb1c57453 100644 --- a/builtin_commandline.cpp +++ b/builtin_commandline.cpp @@ -472,14 +472,13 @@ static int builtin_commandline(parser_t &parser, wchar_t **argv) if (selection_mode) { - size_t sel_start, sel_stop; + size_t start, len; const wchar_t *buffer = reader_get_buffer(); - if(reader_get_selection_pos(sel_start, sel_stop)) + if(reader_get_selection(start, len)) { - size_t len = std::min(sel_stop - sel_start + 1, wcslen(buffer)); wchar_t *selection = new wchar_t[len + 1]; selection[len] = L'\0'; - selection = wcsncpy(selection, buffer + sel_start, len); + selection = wcsncpy(selection, buffer + start, len); append_format(stdout_buffer, selection); delete selection; diff --git a/input.cpp b/input.cpp index 75e0b33c2..a10bcc695 100644 --- a/input.cpp +++ b/input.cpp @@ -140,6 +140,7 @@ static const wchar_t * const name_arr[] = L"accept-autosuggestion", L"begin-selection", L"end-selection", + L"kill-selection" } ; @@ -231,7 +232,8 @@ static const wchar_t code_arr[] = R_SUPPRESS_AUTOSUGGESTION, R_ACCEPT_AUTOSUGGESTION, R_BEGIN_SELECTION, - R_END_SELECTION + R_END_SELECTION, + R_KILL_SELECTION } ; diff --git a/input.h b/input.h index 78f7ec72c..7eb9343a2 100644 --- a/input.h +++ b/input.h @@ -62,12 +62,13 @@ enum R_SUPPRESS_AUTOSUGGESTION, R_ACCEPT_AUTOSUGGESTION, R_BEGIN_SELECTION, - R_END_SELECTION + R_END_SELECTION, + R_KILL_SELECTION } ; #define R_MIN R_NULL -#define R_MAX R_END_SELECTION +#define R_MAX R_KILL_SELECTION /** Initialize the terminal by calling setupterm, and set up arrays diff --git a/reader.cpp b/reader.cpp index b9e6e4d47..a5cb411da 100644 --- a/reader.cpp +++ b/reader.cpp @@ -2436,7 +2436,7 @@ size_t reader_get_cursor_pos() return data->buff_pos; } -bool reader_get_selection_pos(size_t &start, size_t &stop) +bool reader_get_selection(size_t &start, size_t &len) { if (!data) { @@ -2449,7 +2449,7 @@ bool reader_get_selection_pos(size_t &start, size_t &stop) else { start = data->sel_start_pos; - stop = data->sel_stop_pos; + len = std::min(data->sel_stop_pos - data->sel_start_pos + 1, data->command_length()); return true; } } @@ -3850,6 +3850,17 @@ const wchar_t *reader_readline(void) break; } + case R_KILL_SELECTION: + { + bool newv = (last_char != R_KILL_SELECTION); + size_t start, len; + if(reader_get_selection(start, len)) + { + reader_kill(start, len, KILL_APPEND, newv); + } + break; + } + /* Other, if a normal character, we add it to the command */ default: { diff --git a/reader.h b/reader.h index 92c22a848..dac8b5ab9 100644 --- a/reader.h +++ b/reader.h @@ -120,7 +120,7 @@ size_t reader_get_cursor_pos(); Get the current selection range in the command line. Returns false if there is no active selection, true otherwise. */ -bool reader_get_selection_pos(size_t &start, size_t &stop); +bool reader_get_selection(size_t &start, size_t &len); /** Return the value of the interrupted flag, which is set by the sigint diff --git a/share/functions/fish_vi_key_bindings.fish b/share/functions/fish_vi_key_bindings.fish index e6ca32adf..03889da78 100644 --- a/share/functions/fish_vi_key_bindings.fish +++ b/share/functions/fish_vi_key_bindings.fish @@ -192,7 +192,10 @@ function fish_vi_key_bindings -d "vi-like key bindings for fish" bind -M visual -k left backward-char bind -M visual h backward-char bind -M visual l forward-char - bind -M visual '"*y' "commandline -s | xsel -p" + + bind -M visual -m default d kill-selection end-selection force-repaint + bind -M visual -m default y kill-selection yank end-selection force-repaint + bind -M visual -m default '"*y' "commandline -s | xsel -p" end-selection force-repaint bind -M visual -m default \cc end-selection force-repaint bind -M visual -m default \e end-selection force-repaint