diff options
Diffstat (limited to 'src/rbtree.cpp')
-rw-r--r-- | src/rbtree.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/rbtree.cpp b/src/rbtree.cpp index f1d870d..5b1a4e1 100644 --- a/src/rbtree.cpp +++ b/src/rbtree.cpp @@ -19,13 +19,71 @@ #include "config.h" #endif +#include <string.h> + #include <glib.h> #include <glib/gprintf.h> +#include "sciteco.h" #include "rbtree.h" +#include "interface.h" +#include "string-utils.h" namespace SciTECO { +template <StringCmpFunc StringCmp, StringNCmpFunc StringNCmp> +gchar * +RBTreeStringT<StringCmp, StringNCmp>:: +auto_complete(const gchar *key, gchar completed, gsize restrict_len) +{ + gsize key_len; + RBEntryString *first = NULL; + gsize prefix_len = 0; + gint prefixed_entries = 0; + gchar *insert = NULL; + + if (!key) + key = ""; + key_len = strlen(key); + + for (RBEntryString *cur = nfind(key); + cur && !StringNCmp(cur->key, key, key_len); + cur = (RBEntryString *)cur->next()) { + if (restrict_len && strlen(cur->key) != restrict_len) + continue; + + if (!first) + first = cur; + + gsize len = String::diff(first->key + key_len, + cur->key + key_len); + if (!prefix_len || len < prefix_len) + prefix_len = len; + + prefixed_entries++; + } + if (prefix_len > 0) + insert = g_strndup(first->key + key_len, prefix_len); + + if (!insert && prefixed_entries > 1) { + for (RBEntryString *cur = first; + cur && !StringNCmp(cur->key, key, key_len); + cur = (RBEntryString *)cur->next()) { + if (restrict_len && strlen(cur->key) != restrict_len) + continue; + + interface.popup_add(InterfaceCurrent::POPUP_PLAIN, + cur->key); + } + + interface.popup_show(); + } else if (prefixed_entries == 1) { + String::append(insert, completed); + } + + return insert; +} + template class RBTreeStringT<strcmp, strncmp>; template class RBTreeStringT<g_ascii_strcasecmp, g_ascii_strncasecmp>; |