diff -Nur --exclude=CVS balsa-cvs/libbalsa/folder-scanners.c balsa-imap-multiple/libbalsa/folder-scanners.c
--- balsa-cvs/libbalsa/folder-scanners.c	Tue Aug 21 11:02:12 2001
+++ balsa-imap-multiple/libbalsa/folder-scanners.c	Tue Sep 25 21:50:14 2001
@@ -204,6 +204,8 @@
 	    imap_browse ((char*)imap_path,  &state);
 	    g_free(imap_path);
 	}
+	/* let libmutt know to wait for the pipeline to clear */
+	imap_browse(NULL, &state);
 	g_list_foreach(list, (GFunc)g_free, NULL);
 	g_list_free(list); 
     }
diff -Nur --exclude=CVS balsa-cvs/libmutt/browser.h balsa-imap-multiple/libmutt/browser.h
--- balsa-cvs/libmutt/browser.h	Tue Aug 21 11:02:12 2001
+++ balsa-imap-multiple/libmutt/browser.h	Wed Sep 26 19:29:10 2001
@@ -51,6 +51,9 @@
   int noselect : 1;
   int marked : 1;
   int unmarked : 1;
+/* #ifdef LIBMUTT breaks libbalsa/folder-scanners.c */
+  LIST* seq_list;
+/* #endif */
   /*BALSA data begin */
   void * rnode;        /* points to a GNode */
   void * server;       /* points to a LibBalsaServer */
diff -Nur --exclude=CVS balsa-cvs/libmutt/imap/browse.c balsa-imap-multiple/libmutt/imap/browse.c
--- balsa-cvs/libmutt/imap/browse.c	Tue Aug 21 11:02:12 2001
+++ balsa-imap-multiple/libmutt/imap/browse.c	Wed Sep 26 19:36:54 2001
@@ -21,6 +21,9 @@
 
 #include <stdlib.h>
 #include <ctype.h>
+#ifdef LIBMUTT
+#include <sys/poll.h>
+#endif
 
 #include "mutt.h"
 #include "imap_private.h"
@@ -35,21 +38,32 @@
   state->entrymax = 256;
   state->entry = (struct folder_file *) 
     safe_malloc (sizeof (struct folder_file) * state->entrymax);
+#ifdef LIBMUTT
+  state->seq_list = mutt_new_list();
+#endif
 }
 
 void state_free (struct browser_state *state)
 {
   safe_free((void**)&state->entry);
   safe_free((void**)&state->folder);
+#ifdef LIBMUTT
+  mutt_free_list(&state->seq_list);
+#endif
 }
 /* #endif */
 
 /* -- forward declarations -- */
+#ifndef LIBMUTT
 static int browse_add_list_result (IMAP_DATA* idata, const char* cmd,
   struct browser_state* state, short isparent);
-#ifndef LIBMUTT
 static void imap_add_folder (char delim, char *folder, int noselect,
   int noinferiors, struct browser_state *state, short isparent);
+#else
+static void browse_add_list_result (IMAP_DATA* idata, const char* cmd,
+  struct browser_state* state);
+static int browse_handle_list_result(IMAP_DATA* idata,
+  struct browser_state* state);
 #endif
 static int compare_names(struct folder_file *a, struct folder_file *b);
 static int browse_get_namespace (IMAP_DATA *idata, char *nsbuf, int nsblen, 
@@ -61,7 +75,11 @@
  *   given a current folder to browse */
 int imap_browse (char* path, struct browser_state* state)
 {
+#ifdef LIBMUTT
+  static IMAP_DATA* idata = NULL;
+#else
   IMAP_DATA* idata;
+#endif
   char buf[LONG_STRING];
   char buf2[LONG_STRING];
   char nsbuf[LONG_STRING];
@@ -79,6 +97,19 @@
   int noselect;
   int noinferiors;
   IMAP_MBOX mx;
+#ifdef LIBMUTT
+  static struct pollfd imap_pollfd;
+
+  if (!path)
+  {
+    /* wait for pipeline to clear */
+    while (state->seq_list->next)
+      if (browse_handle_list_result(idata, state) < 0)
+	return -1;
+    idata = NULL;
+    return 0;
+  }
+#endif
 
   if (imap_parse_path (path, &mx))
   {
@@ -88,8 +119,18 @@
 
   strfcpy (list_cmd, option (OPTIMAPLSUB) ? "LSUB" : "LIST", sizeof (list_cmd));
 
+#ifdef LIBMUTT
+  if (!idata)
+  {
+    if (!(idata = imap_conn_find (&(mx.account), 0)))
+      goto fail;
+    imap_pollfd.fd = idata->conn->fd;
+    imap_pollfd.events = POLLIN;
+  }
+#else
   if (!(idata = imap_conn_find (&(mx.account), 0)))
     goto fail;
+#endif
 
   if (!mx.mbox)
   {
@@ -215,14 +256,28 @@
     state->folder = safe_strdup (buf);
   }
 
+#ifdef LIBMUTT
+  if (imap_parse_path (state->folder, &mx))
+  {
+    dprint (2, (debugfile,
+      "imap_browse: current folder %s makes no sense\n",
+      state->folder));
+    goto fail;
+  }
+#endif
+
   if (home_namespace && mbox[0] != '\0')
   {
     /* Listing the home namespace, so INBOX should be included. Home 
      * namespace is not "", so we have to list it explicitly. We ask the 
      * server to see if it has descendants. */
     dprint (2, (debugfile, "imap_init_browse: adding INBOX\n"));
+#ifdef LIBMUTT
+    browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state);
+#else
     if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
       goto fail;
+#endif
   }
 
   nsup = state->entrylen;
@@ -232,8 +287,12 @@
   imap_quote_string (buf2, sizeof (buf2), buf);
   dprint (3, (debugfile, "%s\n", buf2));
   snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
+#ifdef LIBMUTT
+  browse_add_list_result (idata, buf, state);
+#else
   if (browse_add_list_result (idata, buf, state, 0))
     goto fail;
+#endif
 
   mutt_clear_error ();
 
@@ -250,6 +309,14 @@
       }
   }
 
+#ifdef LIBMUTT
+  /* check pipeline */
+  while (state->seq_list->next && poll(&imap_pollfd, 1, 0) > 0
+		  && imap_pollfd.revents & POLLIN)
+    if (browse_handle_list_result(idata, state) < 0)
+      goto fail;
+#endif
+
   FREE (&mx.mbox);
   return 0;
 
@@ -341,7 +408,65 @@
   return -1;
 }
 
+#ifdef LIBMUTT
+static void browse_add_list_result (IMAP_DATA* idata, const char* cmd,
+  struct browser_state* state)
+{
+  imap_cmd_start (idata, cmd);
+  /* mutt_add_list allocates its own space for data, so no need to
+   * strdup idata->cmd.seq */
+  mutt_add_list(state->seq_list, idata->cmd.seq);
+}
+
+static int not_in_list(const char * s, LIST * l)
+{
+  LIST *previous = l;
+
+  /* mutt_lists have a head node with NULL data, so we never match
+   * it, and we can start with l->next */
+  for (l = l->next; l; l = l->next)
+  {
+    if (!strncmp(s, l->data, SEQLEN)) 
+    {
+      /* found: remove l from list */
+      previous->next = l->next;
+      l->next = NULL;
+      /* mutt_free_list frees the data and the list node */
+      mutt_free_list(&l);
+      return 0;
+    }
+    previous = l;
+  }
+  return 1;
+}
+
+static int browse_handle_list_result(IMAP_DATA* idata,
+  struct browser_state* state)
+{
+  char *name;
+  int noselect;
+  int noinferiors;
+
+  do 
+  {
+    if (imap_parse_list_response(idata, &name, &noselect, &noinferiors,
+        &idata->delim) != 0)
+    {
+      return -1;
+    }
+
+    if (name)
+    {
+      imap_add_folder (idata->delim, name, noselect, noinferiors, state,
+          0);
+    }
+  }
+  while (*idata->cmd.buf == '*'
+		  || not_in_list(idata->cmd.buf, state->seq_list));
 
+  return 0;
+}
+#else
 static int browse_add_list_result (IMAP_DATA* idata, const char* cmd,
   struct browser_state* state, short isparent)
 {
@@ -385,7 +510,6 @@
   return 0;
 }
 
-#ifndef LIBMUTT
 /* imap_add_folder: add a folder name to the browser list, formatting it as
  *   necessary. */
 static void imap_add_folder (char delim, char *folder, int noselect,
