about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-06-04 23:59:09 +0100
committerJames Booth <boothj5@gmail.com>2012-06-04 23:59:09 +0100
commit6bb120200efa4a89ddc0506d2eb12a103e4e39a1 (patch)
treed6944493f17982af85f8fe19b622b1e7e2d68757
parent467b5cce7e6c6e830b0066f78edac699f4364d1f (diff)
downloadprofani-tty-6bb120200efa4a89ddc0506d2eb12a103e4e39a1.tar.gz
Added command autocomplete
-rw-r--r--Makefile2
-rw-r--r--command.c21
-rw-r--r--command.h2
-rw-r--r--input_win.c24
-rw-r--r--util.c11
-rw-r--r--util.h1
6 files changed, 59 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 3bb14787..e63a92b3 100644
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,7 @@ log.o: log.h
 windows.o: ui.h util.h contact_list.h preferences.h
 title_bar.o: ui.h
 status_bar.o: ui.h util.h
-input_win.o: ui.h preferences.h
+input_win.o: ui.h preferences.h util.h command.h
 jabber.o: jabber.h log.h ui.h contact_list.h
 profanity.o: log.h ui.h jabber.h command.h preferences.h \
 			 contact_list.h
diff --git a/command.c b/command.c
index f14cffb6..19fd1ab7 100644
--- a/command.c
+++ b/command.c
@@ -32,6 +32,7 @@
 #include "ui.h"
 #include "util.h"
 #include "preferences.h"
+#include "prof_autocomplete.h"
 
 static gboolean _handle_command(const char * const command, 
     const char * const inp);
@@ -59,6 +60,8 @@ struct cmd_t {
     gboolean (*func)(const char * const inp);
 };
 
+static PAutocomplete commands_ac;
+
 static struct cmd_t commands[] = {
     { "/away", _cmd_away },
     { "/beep", _cmd_set_beep },
@@ -108,9 +111,27 @@ gboolean process_input(char *inp)
 
 void command_init(void)
 {
+    commands_ac = p_autocomplete_new();
+
+    int i;
+    for (i = 0; i < num_cmds; i++) {
+        struct cmd_t *pcmd = commands+i;
+        p_autocomplete_add(commands_ac, (gchar *)pcmd->cmd);
+    }
+
     history_init();
 }
 
+char * cmd_complete(char *inp)
+{
+    return p_autocomplete_complete(commands_ac, inp);
+}
+
+void reset_command_completer(void)
+{
+    p_autocomplete_reset(commands_ac);
+}
+
 static gboolean _handle_command(const char * const command, const char * const inp)
 {
     int i;
diff --git a/command.h b/command.h
index 9da8838c..f2eef494 100644
--- a/command.h
+++ b/command.h
@@ -25,5 +25,7 @@
 
 void command_init(void);
 gboolean process_input(char *inp);
+char * cmd_complete(char *inp);
+void reset_command_completer(void);
 
 #endif
diff --git a/input_win.c b/input_win.c
index b4e14aa1..db5463ed 100644
--- a/input_win.c
+++ b/input_win.c
@@ -44,6 +44,8 @@
 #include "ui.h"
 #include "history.h"
 #include "preferences.h"
+#include "util.h"
+#include "command.h"
 
 static WINDOW *inp_win;
 
@@ -124,6 +126,7 @@ void inp_get_char(int *ch, char *input, int *size)
 
             reset_search_attempts();
             reset_login_search();
+            reset_command_completer();
         }
     }
 
@@ -234,7 +237,24 @@ static int _handle_edit(const int ch, char *input, int *size)
         return 1;
 
     case 9: // tab
-        if ((strncmp(input, "/msg ", 5) == 0) && (*size > 5)) {
+
+        // autocomplete commands
+        if ((strncmp(input, "/", 1) == 0) && (!str_contains(input, *size, ' '))) {
+            for(i = 0; i < *size; i++) {
+                inp_cpy[i] = input[i];
+            }
+            inp_cpy[i] = '\0';
+            found = cmd_complete(inp_cpy);
+            if (found != NULL) {
+                auto_msg = (char *) malloc((strlen(found) + 1) * sizeof(char));
+                strcpy(auto_msg, found);
+                _replace_input(input, auto_msg, size);
+                free(auto_msg);
+                free(found);
+            }
+
+        // autcomplete /msg recipient
+        } else if ((strncmp(input, "/msg ", 5) == 0) && (*size > 5)) {
             for(i = 5; i < *size; i++) {
                 inp_cpy[i-5] = input[i];
             }
@@ -248,6 +268,8 @@ static int _handle_edit(const int ch, char *input, int *size)
                 free(auto_msg);
                 free(found);
             }
+
+        // autocomplete /connect username
         } else if ((strncmp(input, "/connect ", 9) == 0) && (*size > 9)) {
             for(i = 9; i < *size; i++) {
                 inp_cpy[i-9] = input[i];
diff --git a/util.c b/util.c
index 58f4fbf1..909d4d39 100644
--- a/util.c
+++ b/util.c
@@ -78,3 +78,14 @@ char * str_replace (const char *string, const char *substr,
   
     return newstr;
 }
+
+int str_contains(char str[], int size, char ch)
+{
+    int i;
+    for (i = 0; i < size; i++) {
+        if (str[i] == ch)
+            return 1;
+    }
+
+    return 0;
+}
diff --git a/util.h b/util.h
index 4326fbb4..59469de0 100644
--- a/util.h
+++ b/util.h
@@ -26,5 +26,6 @@
 void get_time(char *thetime);
 char * str_replace(const char *string, const char *substr, 
     const char *replacement);
+int str_contains(char str[], int size, char ch);
 
 #endif