changeset 173:09e24917059d

[/trunk] Changeset 185 by mikael * Command (input) line history
author mikael
date Wed, 04 May 2005 21:06:05 +0000
parents dabf8c3b475c
children 6354625e8fb2
files mcabber/src/TODO mcabber/src/screen.c mcabber/src/screen.h
diffstat 3 files changed, 67 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/TODO	Wed May 04 19:52:38 2005 +0000
+++ b/mcabber/src/TODO	Wed May 04 21:06:05 2005 +0000
@@ -7,7 +7,6 @@
 TODO:
 
 * Presence notification is always accepted.  We should ask...
-* Command line history (^P/^N)
 * Read history
 * Display status
 * Get timestamp for offline messages
@@ -18,7 +17,6 @@
 * Add a function in hbuf ~previous_persistent(hbuf *top) (to avoid loosing the
   top variable on a resize).
 * Show number of online contacts in folded groups
-* show (how?) we can scroll in roster if not all buddies are displayed
 
 * Commands! :-)
   - /roster <hide_offline|show_offline|top|bottom>
--- a/mcabber/src/screen.c	Wed May 04 19:52:38 2005 +0000
+++ b/mcabber/src/screen.c	Wed May 04 21:06:05 2005 +0000
@@ -47,6 +47,8 @@
 static char *ptr_inputline;
 static short int inputline_offset;
 static int  completion_started;
+static GList *cmdhisto;
+static GList *cmdhisto_cur;
 
 
 /* Functions */
@@ -952,6 +954,52 @@
   chatmode = enable;
 }
 
+//  scr_cmdhisto_addline()
+// Add a line to the inputLine history
+inline void scr_cmdhisto_addline(char *line)
+{
+  if (!line || !*line) return;
+
+  cmdhisto = g_list_append(cmdhisto, g_strdup(line));
+}
+
+//  scr_cmdhisto_prev()
+// Look for previous line beginning w/ the given mask in the inputLine history
+const char *scr_cmdhisto_prev(char *mask, guint len)
+{
+  GList *hl;
+  if (!cmdhisto_cur) {
+    hl = g_list_last(cmdhisto);
+  } else {
+    hl = g_list_previous(cmdhisto_cur);
+  }
+  while (hl) {
+    if (!strncmp((char*)hl->data, mask, len)) {
+      // Found a match
+      cmdhisto_cur = hl;
+      return (const char*)hl->data;
+    }
+    hl = g_list_previous(hl);
+  }
+  return NULL;
+}
+
+//  scr_cmdhisto_next()
+// Look for next line beginning w/ the given mask in the inputLine history
+const char *scr_cmdhisto_next(char *mask, guint len)
+{
+  GList *hl;
+  if (!cmdhisto_cur) return NULL;
+  hl = cmdhisto_cur;
+  while ((hl = g_list_next(hl)) != NULL)
+    if (!strncmp((char*)hl->data, mask, len)) {
+      // Found a match
+      cmdhisto_cur = hl;
+      return (const char*)hl->data;
+    }
+  return NULL;
+}
+
 //  which_row()
 // Tells which row our cursor is in, in the command line.
 // -1 -> normal text
@@ -1154,6 +1202,10 @@
       case '\n':  // Enter
           if (process_line(inputLine))
             return 255;
+          // Add line to history
+          scr_cmdhisto_addline(inputLine);
+          cmdhisto_cur = NULL;
+          // Reset the line
           ptr_inputline = inputLine;
           *ptr_inputline = 0;
           inputline_offset = 0;
@@ -1190,10 +1242,22 @@
           *ptr_inputline = 0;
           break;
       case 16:  // Ctrl-p
-          scr_LogPrint("Ctrl-p not yet implemented");
+          {
+            const char *l = scr_cmdhisto_prev(inputLine,
+                    ptr_inputline-inputLine);
+            if (l) {
+              strcpy(inputLine, l);
+            }
+          }
           break;
       case 14:  // Ctrl-n
-          scr_LogPrint("Ctrl-n not yet implemented");
+          {
+            const char *l = scr_cmdhisto_next(inputLine,
+                    ptr_inputline-inputLine);
+            if (l) {
+              strcpy(inputLine, l);
+            }
+          }
           break;
       case 27:  // ESC
           currentWindow = NULL;
--- a/mcabber/src/screen.h	Wed May 04 19:52:38 2005 +0000
+++ b/mcabber/src/screen.h	Wed May 04 21:06:05 2005 +0000
@@ -2,6 +2,7 @@
 #define __SCREEN_H__ 1
 
 #include <ncurses.h>
+#include <glib.h>
 
 //#define COLOR_NMSG      1
 #define COLOR_GENERAL   3