Branch: refs/heads/master Author: LarsDW223 lars_paulsen@web.de Committer: LarsDW223 lars_paulsen@web.de Date: Tue, 16 Jan 2018 20:00:18 UTC Commit: d7e345e1556e5ee4116ced8fccebc1405a5bc24f https://github.com/geany/geany-plugins/commit/d7e345e1556e5ee4116ced8fccebc1...
Log Message: ----------- addons: re-factored function 'get_color_value_at_current_doc_position()' (thanks to b4n).
Modified Paths: -------------- addons/src/ao_colortip.c
Modified: addons/src/ao_colortip.c 68 lines changed, 28 insertions(+), 40 deletions(-) =================================================================== @@ -140,46 +140,34 @@ static gint contains_color_value(gchar *string, gint position, gint maxdist)
static gint get_color_value_at_current_doc_position(void) { - gchar *subtext; - gint start, end, pos, max, color = -1; - GeanyDocument *doc = document_get_current(); - - g_return_val_if_fail(doc != NULL, FALSE); - - /* Is position valid? */ - pos = sci_get_current_position(doc->editor->sci); - if (pos < 0) - { - return color; - } - max = SSM(doc->editor->sci, SCI_GETTEXTLENGTH, 0, 0); - - /* Calculate range */ - start = pos; - if (start >= 7) - { - start -= 7; - } - else - { - start = 0; - } - end = pos + 7; - if (end > max) - { - end = max; - } - - /* Get text in range and examine it */ - subtext = sci_get_contents_range(doc->editor->sci, start, end); - if (subtext != NULL) - { - pos = pos - start; - color = contains_color_value(subtext, pos, 1); - g_free(subtext); - } - - return color; + gint color = -1; + GeanyDocument *doc = document_get_current(); + gchar *word = editor_get_word_at_pos(doc->editor, -1, "0123456789abcdefABCDEF"); + + if (word) + { + switch (strlen (word)) + { + case 3: + color = ((g_ascii_xdigit_value(word[0]) * 0x11) << 16 | + (g_ascii_xdigit_value(word[1]) * 0x11) << 8 | + (g_ascii_xdigit_value(word[2]) * 0x11) << 0); + break; + case 6: + color = (g_ascii_xdigit_value(word[0]) << 20 | + g_ascii_xdigit_value(word[1]) << 16 | + g_ascii_xdigit_value(word[2]) << 12 | + g_ascii_xdigit_value(word[3]) << 8 | + g_ascii_xdigit_value(word[4]) << 4 | + g_ascii_xdigit_value(word[5]) << 0); + break; + default: + /* invalid color or other format */ + break; + } + } + + return color; }
static gboolean on_editor_button_press_event(GtkWidget *widget, GdkEventButton *event,
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).