From 5cf67c2d616e9d5721b5b9e4d93c5a934f0ecf62 Mon Sep 17 00:00:00 2001 From: Michael Forster Date: Thu, 9 Jun 2022 17:50:33 +0200 Subject: [PATCH] Use dedicated variable to configure selection size This addresses code review feedback to not couple the purely visual concept of cursor style with the logical concept of the selection size. Instead this now uses a dedicated variable `$fish_select_char_after_cursor` to determine whether to extend the selection beyond the cursor: * fish_select_char_after_cursor = 1 or unset -> extend selection * all other cases -> place the selection end that the cursor --- src/input.h | 3 --- src/reader.cpp | 23 ++++++++--------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/src/input.h b/src/input.h index f1f6cfd53..c2343baae 100644 --- a/src/input.h +++ b/src/input.h @@ -13,9 +13,6 @@ #define FISH_BIND_MODE_VAR L"fish_bind_mode" #define DEFAULT_BIND_MODE L"default" -#define FISH_CURSOR_VAR_PREFIX L"fish_cursor_" -#define FISH_CURSOR_LINE L"line" - class event_queue_peeker_t; class parser_t; diff --git a/src/reader.cpp b/src/reader.cpp index 0ba2ce571..1574dec51 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -780,8 +780,8 @@ class reader_data_t : public std::enable_shared_from_this { inputter(*parser_ref, conf.in), history(std::move(hist)) {} - /// Returns the width of the current cursor in characters - size_t cursor_width(); + /// Whether the selection should always include the character after the cursor. + bool select_char_after_cursor(); void update_buff_pos(editable_line_t *el, maybe_t new_pos = none_t()); void kill(editable_line_t *el, size_t begin_idx, size_t length, int mode, int newv); @@ -1048,16 +1048,9 @@ wcstring combine_command_and_autosuggestion(const wcstring &cmdline, return full_line; } -size_t reader_data_t::cursor_width() { - auto bind_mode = vars().get(FISH_BIND_MODE_VAR); - auto fish_cursor_var = FISH_CURSOR_VAR_PREFIX - + (bind_mode ? bind_mode->as_string() : DEFAULT_BIND_MODE); - if (auto cursor = vars().get(fish_cursor_var)) { - if (cursor->as_string() == FISH_CURSOR_LINE) { - return 0; - } - } - return 1; +bool reader_data_t::select_char_after_cursor() { + auto val = vars().get(L"fish_select_char_after_cursor"); + return !val || val->as_string() == L"1"; } /// Update the cursor position. @@ -1069,10 +1062,10 @@ void reader_data_t::update_buff_pos(editable_line_t *el, maybe_t new_pos if (el == &command_line && selection.has_value()) { if (selection->begin <= buff_pos) { selection->start = selection->begin; - selection->stop = buff_pos + cursor_width(); + selection->stop = buff_pos + (select_char_after_cursor() ? 1 : 0); } else { selection->start = buff_pos; - selection->stop = selection->begin + cursor_width(); + selection->stop = selection->begin + (select_char_after_cursor() ? 1 : 0); } } } @@ -3978,7 +3971,7 @@ void reader_data_t::handle_readline_command(readline_cmd_t c, readline_loop_stat size_t pos = command_line.position(); selection->begin = pos; selection->start = pos; - selection->stop = pos + cursor_width(); + selection->stop = pos + (select_char_after_cursor() ? 1 : 0); break; }