changeset 370:dd9e2eb52916

Add /buffer search_{backward,forward}
author Mikael Berthe <mikael@lilotux.net>
date Sun, 24 Jul 2005 22:56:42 +0100
parents 499170ed71c9
children bc54af8ed048
files mcabber/src/commands.c mcabber/src/hbuf.c mcabber/src/hbuf.h mcabber/src/screen.c mcabber/src/screen.h
diffstat 5 files changed, 82 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/commands.c	Sun Jul 24 22:31:31 2005 +0100
+++ b/mcabber/src/commands.c	Sun Jul 24 22:56:42 2005 +0100
@@ -127,6 +127,8 @@
   compl_add_category_word(COMPL_BUFFER, "bottom");
   compl_add_category_word(COMPL_BUFFER, "clear");
   compl_add_category_word(COMPL_BUFFER, "top");
+  compl_add_category_word(COMPL_BUFFER, "search_backward");
+  compl_add_category_word(COMPL_BUFFER, "search_forward");
 
   // Group category
   compl_add_category_word(COMPL_GROUP, "fold");
@@ -564,14 +566,32 @@
 
 static void do_buffer(char *arg)
 {
+  int search_dir = 0;
+
   if (!strcasecmp(arg, "top")) {
     scr_BufferTopBottom(-1);
   } else if (!strcasecmp(arg, "bottom")) {
     scr_BufferTopBottom(1);
   } else if (!strcasecmp(arg, "clear")) {
     scr_BufferClear();
+  } else if (!strncasecmp(arg, "search_backward", 15)) {
+    arg += 15;
+    if (*arg++ == ' ')
+      search_dir = -1;
+    else
+      scr_LogPrint("Missing parameter");
+  } else if (!strncasecmp(arg, "search_forward", 14)) {
+    arg += 14;
+    if (*arg++ == ' ')
+      search_dir = 1;
+    else
+      scr_LogPrint("Missing parameter");
   } else
     scr_LogPrint("Unrecognized parameter!");
+
+  // Is it a string search command?
+  if (search_dir)
+    scr_BufferSearch(search_dir, arg);
 }
 
 static void do_clear(char *arg)    // Alias for "/buffer clear"
--- a/mcabber/src/hbuf.c	Sun Jul 24 22:31:31 2005 +0100
+++ b/mcabber/src/hbuf.c	Sun Jul 24 22:56:42 2005 +0100
@@ -19,6 +19,7 @@
  * USA
  */
 
+#define _GNU_SOURCE  /* We need glibc for strptime */
 #include <string.h>
 
 #include "hbuf.h"
@@ -239,7 +240,7 @@
 }
 
 //  hbuf_get_lines(hbuf, n)
-// Returns an array of n *hbb_line pointers
+// Returns an array of n hbb_line pointers
 // (The first line will be the line currently pointed by hbuf)
 // Note: The caller should free the array and the text pointers after use.
 hbb_line **hbuf_get_lines(GList *hbuf, unsigned int n)
@@ -269,3 +270,27 @@
   return array;
 }
 
+//  hbuf_search(hbuf, direction, string)
+// Look backward/forward for a line containing string in the history buffer
+// Search starts at hbuf, and goes forward if direction == 1, backward if -1
+GList *hbuf_search(GList *hbuf, int direction, const char *string)
+{
+  hbuf_block *blk;
+
+  for (;;) {
+    if (direction > 0)
+      hbuf = g_list_next(hbuf);
+    else
+      hbuf = g_list_previous(hbuf);
+
+    if (!hbuf) break;
+
+    blk = (hbuf_block*)(hbuf->data);
+    // XXX blk->ptr is (maybe) not really correct, because the match should
+    // not be after ptr_end.  We should check that...
+    if (strcasestr(blk->ptr, string))
+      break;
+  }
+
+  return hbuf;
+}
--- a/mcabber/src/hbuf.h	Sun Jul 24 22:31:31 2005 +0100
+++ b/mcabber/src/hbuf.h	Sun Jul 24 22:56:42 2005 +0100
@@ -34,5 +34,6 @@
 GList *hbuf_previous_persistent(GList *l_line);
 
 hbb_line **hbuf_get_lines(GList *hbuf, unsigned int n);
+GList *hbuf_search(GList *hbuf, int direction, const char *string);
 
 #endif /* __HBUF_H__ */
--- a/mcabber/src/screen.c	Sun Jul 24 22:31:31 2005 +0100
+++ b/mcabber/src/screen.c	Sun Jul 24 22:56:42 2005 +0100
@@ -1005,6 +1005,40 @@
   doupdate();
 }
 
+//  scr_BufferSearch(direction, text)
+// Jump to the next line containing text
+// (backward search if direction == -1, forward if topbottom == 1)
+void scr_BufferSearch(int direction, const char *text)
+{
+  window_entry_t *win_entry;
+  GList *current_line, *search_res;
+
+  // Get win_entry
+  if (!current_buddy) return;
+  win_entry  = scr_SearchWindow(CURRENT_JID);
+  if (!win_entry) return;
+
+  if (win_entry->top)
+    current_line = win_entry->top;
+  else
+    current_line = g_list_last(win_entry->hbuf);
+
+  search_res = hbuf_search(current_line, direction, text);
+
+  if (search_res) {
+    win_entry->cleared = FALSE;
+    win_entry->top = search_res;
+
+    // Refresh the window
+    scr_UpdateWindow(win_entry);
+
+    // Finished :)
+    update_panels();
+    doupdate();
+  } else
+    scr_LogPrint("Search string not found");
+}
+
 //  scr_LogPrint(...)
 // Display a message in the log window.
 void scr_LogPrint(const char *fmt, ...)
--- a/mcabber/src/screen.h	Sun Jul 24 22:31:31 2005 +0100
+++ b/mcabber/src/screen.h	Sun Jul 24 22:56:42 2005 +0100
@@ -51,6 +51,7 @@
 void scr_RosterSearch(char *);
 void scr_BufferTopBottom(int topbottom);
 void scr_BufferClear(void);
+void scr_BufferSearch(int direction, const char *text);
 void scr_RosterUnreadMessage(int);
 void scr_RosterJumpAlternate(void);