changeset 102:2b4cc6bc5bf2

[/trunk] Changeset 116 by mikael * Can now complete command parameters :-)
author mikael
date Thu, 21 Apr 2005 19:06:27 +0000
parents 4f3ad00b5187
children 93dcc4e15d4a
files mcabber/src/commands.c mcabber/src/commands.h mcabber/src/main.c mcabber/src/screen.c
diffstat 4 files changed, 93 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/commands.c	Thu Apr 21 17:31:28 2005 +0000
+++ b/mcabber/src/commands.c	Thu Apr 21 19:06:27 2005 +0000
@@ -30,14 +30,6 @@
 #include "utils.h"
 
 
-// Command structure
-typedef struct {
-  char name[32];
-  const char *help;
-  guint completion_flags[2];
-  void *(*func)();
-} cmd;
-
 static GSList *Commands;
 
 //  cmd_add()
@@ -51,7 +43,7 @@
   n_cmd->completion_flags[0] = flags_row1;
   n_cmd->completion_flags[1] = flags_row2;
   n_cmd->func = f;
-  g_slist_append(Commands, n_cmd);
+  Commands = g_slist_append(Commands, n_cmd);
   // Add to completion CMD category
   compl_add_category_word(COMPL_CMD, name);
 }
@@ -86,6 +78,35 @@
   compl_add_category_word(COMPL_STATUS, "away");
 }
 
+//  cmd_get
+// Finds command in the command list structure.
+// Returns a pointer to the cmd entry, or NULL if command not found.
+cmd *cmd_get(char *command)
+{
+  char *p1, *p2;
+  char *com;
+  GSList *sl_com;
+  // Ignore leading '/'
+  for (p1 = command ; *p1 == '/' ; p1++)
+    ;
+  // Locate the end of the command
+  for (p2 = p1 ; *p2 && (*p2 != ' ') ; p2++)
+    ;
+  // Copy the clean command
+  com = g_strndup(p1, p2-p1);
+
+  // Look for command in the list
+  for (sl_com=Commands; sl_com; sl_com = g_slist_next(sl_com)) {
+    if (!strcasecmp(com, ((cmd*)sl_com->data)->name))
+      break;
+  }
+  g_free(com);
+
+  if (sl_com)       // Command has been found.
+    return (cmd*)sl_com->data;
+  return NULL;
+}
+
 //  send_message(msg)
 // Write the message in the buddy's window and send the message on
 // the network.
--- a/mcabber/src/commands.h	Thu Apr 21 17:31:28 2005 +0000
+++ b/mcabber/src/commands.h	Thu Apr 21 19:06:27 2005 +0000
@@ -3,7 +3,16 @@
 
 #include <glib.h>
 
+// Command structure
+typedef struct {
+  char name[32];
+  const char *help;
+  guint completion_flags[2];
+  void *(*func)();
+} cmd;
+
 void cmd_init(void);
+cmd *cmd_get(char *command);
 int  process_line(char *line);
 
 #endif /* __COMMANDS_H__ */
--- a/mcabber/src/main.c	Thu Apr 21 17:31:28 2005 +0000
+++ b/mcabber/src/main.c	Thu Apr 21 19:06:27 2005 +0000
@@ -9,7 +9,7 @@
 #include "jabglue.h"
 #include "screen.h"
 #include "parsecfg.h"
-//#include "roster.h"
+#include "roster.h"
 #include "commands.h"
 #include "lang.h"
 #include "utils.h"
--- a/mcabber/src/screen.c	Thu Apr 21 17:31:28 2005 +0000
+++ b/mcabber/src/screen.c	Thu Apr 21 19:06:27 2005 +0000
@@ -587,7 +587,8 @@
 // -1 -> normal text
 //  0 -> command
 //  1 -> parameter 1 (etc.)
-int which_row(void)
+//  If > 0, then *p_row is set to the beginning of the row
+int which_row(char **p_row)
 {
   int row = -1;
   char *p;
@@ -607,8 +608,10 @@
     }
     if (*p == '"' && *(p-1) != '\\') {
       quote = TRUE;
-    } else if (*p == ' ' && *(p-1) != ' ')
+    } else if (*p == ' ' && *(p-1) != ' ') {
       row++;
+      *p_row = p+1;
+    }
   }
   return row;
 }
@@ -630,13 +633,16 @@
 
 void scr_handle_tab(void)
 {
-  int row;
-  row = which_row();
+  int nrow;
+  cmd *com;
+  char *row;
+  const char *cchar;
 
-  if (row == -1) return;    // No completion if no leading slash
+  nrow = which_row(&row);
 
-  if (row == 0) {   // Command completion
-    const char *cchar;
+  if (nrow == -1) return;   // No completion if no leading slash
+
+  if (nrow == 0) {          // Command completion
     if (!completion_started) {
       GSList *list = compl_get_category_list(COMPL_CMD);
       if (list) {
@@ -650,7 +656,7 @@
           scr_insert_text(cchar);
         completion_started = TRUE;
       }
-    } else {
+    } else {    // Completion already initialized
       char *c;
       guint back = cancel_completion();
       // Remove $back chars
@@ -667,7 +673,45 @@
   }
 
   // Other completion, depending on the command
-  scr_LogPrint("I'm unable to complete that yet");
+  com = cmd_get(inputLine);
+  if (!com) {
+    scr_LogPrint("I cannot complete for this command");
+    return;
+  }
+  // Now we have the command and the column.
+  scr_LogPrint("CMD_FLAGR1=%d COL=%d row=[%s]", com->completion_flags[0], nrow, row);
+
+  // We can't have more than 2 parameters (we use 2 flags)
+  if (nrow > 2)
+    return;
+
+  // Dirty copy & paste
+    if (!completion_started) {
+      GSList *list = compl_get_category_list(com->completion_flags[nrow-1]);
+      if (list) {
+        char *prefix = g_strndup(row, ptr_inputline-row);
+        // Init completion
+        new_completion(prefix, list);
+        g_free(prefix);
+        // Now complete
+        cchar = complete();
+        if (cchar)
+          scr_insert_text(cchar);
+        completion_started = TRUE;
+      }
+    } else {    // Completion already initialized
+      char *c;
+      guint back = cancel_completion();
+      // Remove $back chars
+      ptr_inputline -= back;
+      c = ptr_inputline;
+      for ( ; *c ; c++)
+        *c = *(c+back);
+      // Now complete again
+      cchar = complete();
+      if (cchar)
+        scr_insert_text(cchar);
+    }
 }
 
 void scr_cancel_current_completion(void)