about summary refs log tree commit diff stats
path: root/src/tools
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-06-23 01:23:44 +0100
committerJames Booth <boothj5@gmail.com>2013-06-23 01:23:44 +0100
commitf9b8da1afee6267cc1b09f036cd58e1d9326509c (patch)
tree01a1886747c492fa161e915a2164cab0873abdf4 /src/tools
parent1cf20b24911c3e02b4d29adc9499680cac449bc0 (diff)
downloadprofani-tty-f9b8da1afee6267cc1b09f036cd58e1d9326509c.tar.gz
Autocomplete /group add and /group remove contact paramater
closes #193
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/autocomplete.c56
-rw-r--r--src/tools/autocomplete.h2
2 files changed, 58 insertions, 0 deletions
diff --git a/src/tools/autocomplete.c b/src/tools/autocomplete.c
index d9477e99..e3829f7f 100644
--- a/src/tools/autocomplete.c
+++ b/src/tools/autocomplete.c
@@ -260,6 +260,62 @@ autocomplete_param_with_ac(char *input, int *size, char *command,
     return auto_msg;
 }
 
+char *
+autocomplete_param_no_with_func(char *input, int *size, char *command,
+    int arg_number, autocomplete_func func)
+{
+    char *result = NULL;
+    if (strncmp(input, command, strlen(command)) == 0 && (*size > strlen(command))) {
+        int i = 0;
+        int quote_count = 0;
+        char *found = NULL;
+        GString *result_str = NULL;
+
+        // copy and null terminate input, count quotes
+        gchar inp_cpy[*size];
+        for (i = 0; i < *size; i++) {
+            if (input[i] == '"') {
+                quote_count++;
+            }
+            inp_cpy[i] = input[i];
+        }
+        inp_cpy[i] = '\0';
+        g_strstrip(inp_cpy);
+
+        // count tokens
+        gchar **tokens = g_strsplit(inp_cpy, " ", 0);
+        int num_tokens = g_strv_length(tokens);
+
+        // if num tokens, or 2 quotes then candidate for autocompletion of last param
+        if (((num_tokens > arg_number - 1) && quote_count == 0) || quote_count == 2) {
+
+            gchar *comp_str = NULL;
+
+            // find start of autocompletion string
+            if (num_tokens > 3 && quote_count == 0) {
+                comp_str = g_strrstr(inp_cpy, tokens[arg_number - 1]);
+            } else {
+                comp_str = g_strrstr(inp_cpy, "\"");
+                comp_str = comp_str + 2;
+            }
+
+            // autocomplete param
+            if (comp_str != NULL) {
+                found = func(comp_str);
+                if (found != NULL) {
+                    result_str = g_string_new("");
+                    g_string_append(result_str, g_strndup(inp_cpy, strlen(inp_cpy) - strlen(comp_str)));
+                    g_string_append(result_str, found);
+                    result = result_str->str;
+                    g_string_free(result_str, FALSE);
+                    return result;
+                }
+            }
+        }
+    }
+
+    return NULL;
+}
 
 static gchar *
 _search_from(Autocomplete ac, GSList *curr)
diff --git a/src/tools/autocomplete.h b/src/tools/autocomplete.h
index 3f619298..a28601d0 100644
--- a/src/tools/autocomplete.h
+++ b/src/tools/autocomplete.h
@@ -47,5 +47,7 @@ char * autocomplete_param_with_func(char *input, int *size, char *command,
     autocomplete_func func);
 char * autocomplete_param_with_ac(char *input, int *size, char *command,
     Autocomplete ac);
+char * autocomplete_param_no_with_func(char *input, int *size, char *command,
+    int arg_number, autocomplete_func func);
 
 #endif