From e135da8a79fa2a96d836b18159b535d0f7294ecd Mon Sep 17 00:00:00 2001 From: Colomban Wendling Date: Wed, 9 Apr 2014 02:57:10 +0200 Subject: [PATCH] Fix replacing colors with "0x" prefix with length different than 6 We used to assume that if the selected text started with "0x" when inserting a color, we had to replace exactly 6 bytes after the "0x" prefix. Although this is generally the case as most color formats use 6 hexadecimal digits, it still would erase either too many or too few characters if actually replacing something shorter (i.e. "0xfff") or longer (i.e. "0xffffffffffff"). It could even partially override multi-byte characters if the 8th byte after the selection start was in the middle of a character, as the length was in bytes and not characters. Fix this by honoring the actual selection end. --- src/editor.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/editor.c b/src/editor.c index 0db81c475..f140afc20 100644 --- a/src/editor.c +++ b/src/editor.c @@ -4164,8 +4164,12 @@ void editor_insert_color(GeanyEditor *editor, const gchar *colour) if (sci_get_char_at(editor->sci, start) == '0' && sci_get_char_at(editor->sci, start + 1) == 'x') { + gint end = sci_get_selection_end(editor->sci); + sci_set_selection_start(editor->sci, start + 2); - sci_set_selection_end(editor->sci, start + 8); + /* we need to also re-set the selection end in case the anchor was located before + * the cursor, since set_selection_start() always moves the cursor, not the anchor */ + sci_set_selection_end(editor->sci, end); replacement++; /* skip the leading "0x" */ } else if (sci_get_char_at(editor->sci, start - 1) == '#') -- 2.11.4.GIT