--- mutt-1.5.9/browser.c	2005-03-15 17:26:58.181613448 -0500
+++ mutt-danborn.1.5.9/browser.c	2005-03-15 17:25:24.747817544 -0500
@@ -31,6 +31,7 @@
 #ifdef USE_IMAP
 #include "imap.h"
 #endif
+#include "mx.h"
 
 #include <stdlib.h>
 #include <dirent.h>
@@ -144,16 +145,34 @@
 		   const char *fmt, const char *ifstring, const char *elsestring,
 		   unsigned long data, format_flag flags)
 {
-  char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
+  char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11],
+    tmppath[_POSIX_PATH_MAX];
   char date[16], *t_fmt;
   time_t tnow;
   FOLDER *folder = (FOLDER *) data;
   struct passwd *pw;
   struct group *gr;
-  int optional = (flags & M_FORMAT_OPTIONAL);
+  int optional = (flags & M_FORMAT_OPTIONAL), magic;
 
+  strfcpy(tmppath, folder->ff->name, _POSIX_PATH_MAX);
+  magic = mx_get_magic(mutt_expand_path(tmppath, _POSIX_PATH_MAX));
   switch (op)
   {
+     /* Display total number of messages for maildir folders. */
+     case 'c':
+       if (magic == M_MAILDIR)
+       {
+         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+         snprintf (dest, destlen, tmp, folder->ff->cur + folder->ff->new +
+                   folder->ff->del);
+       }
+       else
+       {
+         snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
+         snprintf (dest, destlen, tmp, ' ');
+       }
+       break;
+ 
     case 'C':
       snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
       snprintf (dest, destlen, tmp, folder->num + 1);
@@ -171,6 +190,20 @@
 	mutt_format_s (dest, destlen, fmt, "");
       break;
       
+     /* Display number of deleted messages for maildir folders. */
+     case 'D':
+       if (magic == M_MAILDIR && folder->ff->del > 0)
+       {
+         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+         snprintf (dest, destlen, tmp, folder->ff->del);
+       }
+       else
+       {
+         snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
+         snprintf (dest, destlen, tmp, ' ');
+       }
+       break;
+
     case 'f':
     {
       char *s;
@@ -242,21 +275,33 @@
       else
 	mutt_format_s (dest, destlen, fmt, "");
       break;
-      
+ 
+     case 'n':
+       /* Display number of new messages if mailbox is maildir or IMAP. */
+     {
+       if (magic == M_MAILDIR
+#ifdef USE_IMAP
+           || magic == M_IMAP
+#endif
+          )
+       {
+
+         if (folder->ff->new)
+         {
+           snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+           snprintf (dest, destlen, tmp, folder->ff->new);
+         }
+         else
+         {
+           snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
+           snprintf (dest, destlen, tmp, ' ');
+         }
+         break;
+       }
+     }
+     /* Fall through to 'N' for other mailbox types. */
+
     case 'N':
-#ifdef USE_IMAP
-      if (mx_is_imap (folder->ff->desc))
-      {
-	if (!optional)
-	{
-	  snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
-	  snprintf (dest, destlen, tmp, folder->ff->new);
-	}
-	else if (!folder->ff->new)
-	  optional = 0;
-	break;
-      }
-#endif
       snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
       snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
       break;
@@ -290,6 +335,20 @@
       else
 	mutt_format_s (dest, destlen, fmt, "");
       break;
+
+    /* Display number of non-new messages for maildir folders. */
+    case 'U':
+      if (magic == M_MAILDIR)
+      {
+        snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+        snprintf (dest, destlen, tmp, folder->ff->cur);
+      }
+      else
+      {
+        snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
+        snprintf (dest, destlen, tmp, ' ');
+      }
+      break;
       
     default:
       snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
@@ -306,7 +365,8 @@
 }
 
 static void add_folder (MUTTMENU *m, struct browser_state *state,
-			const char *name, const struct stat *s, int new)
+                        const char *name, const struct stat *s, int new,
+                        int cur, int del)
 {
   if (state->entrylen == state->entrymax)
   {
@@ -330,6 +390,8 @@
   }
 
   (state->entry)[state->entrylen].new = new;
+  (state->entry)[state->entrylen].cur = cur;
+  (state->entry)[state->entrylen].del = del;
   (state->entry)[state->entrylen].name = safe_strdup (name);
   (state->entry)[state->entrylen].desc = safe_strdup (name);
 #ifdef USE_IMAP
@@ -413,7 +475,10 @@
     tmp = Incoming;
     while (tmp && mutt_strcmp (buffer, tmp->path))
       tmp = tmp->next;
-    add_folder (menu, state, de->d_name, &s, (tmp) ? tmp->new : 0);
+     if (tmp)
+       add_folder (menu, state, de->d_name, &s, tmp->new, tmp->cur, tmp->del);
+     else
+       add_folder (menu, state, de->d_name, &s, 0, 0, 0);
   }
   closedir (dp);  
   browser_sort (state);
@@ -437,14 +502,14 @@
 #ifdef USE_IMAP
     if (mx_is_imap (tmp->path))
     {
-      add_folder (menu, state, tmp->path, NULL, tmp->new);
+      add_folder (menu, state, tmp->path, NULL, tmp->new, tmp->cur, tmp->del);
       continue;
     }
 #endif
 #ifdef USE_POP
     if (mx_is_pop (tmp->path))
     {
-      add_folder (menu, state, tmp->path, NULL, tmp->new);
+      add_folder (menu, state, tmp->path, NULL, tmp->new, tmp->cur, tmp->del);
       continue;
     }
 #endif
@@ -458,7 +523,7 @@
     strfcpy (buffer, NONULL(tmp->path), sizeof (buffer));
     mutt_pretty_mailbox (buffer);
 
-    add_folder (menu, state, buffer, &s, tmp->new);
+    add_folder (menu, state, buffer, &s, tmp->new, tmp->cur, tmp->del);
   }
   while ((tmp = tmp->next));
   browser_sort (state);
--- mutt-1.5.9/browser.h	2002-12-11 06:19:39.000000000 -0500
+++ mutt-danborn.1.5.9/browser.h	2005-02-12 22:06:43.000000000 -0500
@@ -31,6 +31,8 @@
   char *desc;
 
   unsigned short new;
+  unsigned short del; /* Number of messages marked deleted. */
+  unsigned short cur; /* Number of non-new, non-deleted messages. */
 #ifdef USE_IMAP
   char delim;
   
--- mutt-1.5.9/buffy.c	2005-02-03 13:47:52.000000000 -0500
+++ mutt-danborn.1.5.9/buffy.c	2005-02-12 22:06:43.000000000 -0500
@@ -264,8 +264,6 @@
   struct stat sb;
   struct dirent *de;
   DIR *dirp;
-  char path[_POSIX_PATH_MAX];
-  struct stat contex_sb;
   time_t t;
 
 #ifdef USE_IMAP
@@ -284,23 +282,10 @@
   BuffyTime = t;
   BuffyCount = 0;
   BuffyNotify = 0;
-
-#ifdef USE_IMAP
-  if (!Context || Context->magic != M_IMAP)
-#endif
-#ifdef USE_POP
-  if (!Context || Context->magic != M_POP)
-#endif
-  /* check device ID and serial number instead of comparing paths */
-  if (!Context || !Context->path || stat (Context->path, &contex_sb) != 0)
-  {
-    contex_sb.st_dev=0;
-    contex_sb.st_ino=0;
-  }
   
   for (tmp = Incoming; tmp; tmp = tmp->next)
   {
-    tmp->new = 0;
+    tmp->cur = tmp->new = tmp->del = 0;
 
 #ifdef USE_IMAP
     if (mx_is_imap (tmp->path))
@@ -325,95 +310,56 @@
       continue;
     }
 
-    /* check to see if the folder is the currently selected folder
-     * before polling */
-    if (!Context || !Context->path ||
-#if defined USE_IMAP || defined USE_POP
-	((
-#ifdef USE_IMAP
-	tmp->magic == M_IMAP
-#endif
-#ifdef USE_POP
-#ifdef USE_IMAP
-	||
-#endif
-	tmp->magic == M_POP
-#endif
-	) ? mutt_strcmp (tmp->path, Context->path) :
-#endif
-	 (sb.st_dev != contex_sb.st_dev || sb.st_ino != contex_sb.st_ino)
-#if defined USE_IMAP || defined USE_POP	 
-	    )
-#endif
-	)
-	
+    switch (tmp->magic)
     {
-      switch (tmp->magic)
-      {
-      case M_MBOX:
-      case M_MMDF:
-
-	if (STAT_CHECK)
-	{
-	  BuffyCount++;
-	  tmp->new = 1;
-	}
+     case M_MBOX:
+     case M_MMDF:
+ 
+       if (STAT_CHECK)
+       {
+         BuffyCount++;
+         tmp->new = 1;
+       }
 #ifdef BUFFY_SIZE
-	else
-	{
-	  /* some other program has deleted mail from the folder */
-	  tmp->size = (long) sb.st_size;
-	}
-#endif
-	if (tmp->newly_created &&
-	    (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
-	  tmp->newly_created = 0;
-
-	break;
-
-      case M_MAILDIR:
-
-	snprintf (path, sizeof (path), "%s/new", tmp->path);
-	if ((dirp = opendir (path)) == NULL)
-	{
-	  tmp->magic = 0;
-	  break;
-	}
-	while ((de = readdir (dirp)) != NULL)
-	{
-	  char *p;
-	  if (*de->d_name != '.' && 
-	      (!(p = strstr (de->d_name, ":2,")) || !strchr (p + 3, 'T')))
-	  {
-	    /* one new and undeleted message is enough */
-	    BuffyCount++;
-	    tmp->new = 1;
-	    break;
-	  }
-	}
-	closedir (dirp);
-	break;
-
-      case M_MH:
-	if ((tmp->new = mh_buffy (tmp->path)) > 0)
-	  BuffyCount++;
-	break;
+       else
+       {
+         /* some other program has deleted mail from the folder */
+         tmp->size = (long) sb.st_size;
+       }
+ #endif
+       if (tmp->newly_created &&
+           (sb.st_ctime != sb.st_mtime || sb.st_ctime != sb.st_atime))
+         tmp->newly_created = 0;
+
+       break;
+
+     case M_MAILDIR:
+       /* Count number of messages for maildir. */
+       if (maildir_count_buffy(tmp) != 0)
+         break;
+       if(tmp->new > 0)
+         BuffyCount++;
+       break;
+
+     case M_MH:
+       if ((tmp->new = mh_buffy (tmp->path)) > 0)
+         BuffyCount++;
+       break;
 	
 #ifdef USE_IMAP
-      case M_IMAP:
-	if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0)
-	  BuffyCount++;
-	else
-	  tmp->new = 0;
+    case M_IMAP:
+      if ((tmp->new = imap_mailbox_check (tmp->path, 1)) > 0)
+        BuffyCount++;
+      else
+        tmp->new = 0;
 
-	break;
+      break;
 #endif
 
 #ifdef USE_POP
-      case M_POP:
-	break;
+    case M_POP:
+      break;
 #endif
-      }
     }
 #ifdef BUFFY_SIZE
     else if (Context && Context->path)
@@ -422,14 +368,89 @@
 
     if (!tmp->new)
       tmp->notified = 0;
-    else if (!tmp->notified)
-      BuffyNotify++;
+    else {
+      /* Disable the buffy notification if the folder is the currently
+       * selected folder. */
+      struct stat contex_sb;
+#ifdef USE_IMAP
+      if (!Context || Context->magic != M_IMAP)
+#endif
+#ifdef USE_POP
+        if (!Context || Context->magic != M_POP)
+#endif
+           /* check device ID and serial number instead of comparing paths */
+           if (!Context || !Context->path ||
+               stat (Context->path, &contex_sb) != 0)
+           {
+             contex_sb.st_dev=0;
+             contex_sb.st_ino=0;
+           }
+      if (Context && Context->path &&
+#if defined USE_IMAP || defined USE_POP
+           ((
+ #ifdef USE_IMAP
+             tmp->magic == M_IMAP
+ #endif
+ #ifdef USE_POP
+ #ifdef USE_IMAP
+             ||
+ #endif
+             tmp->magic == M_POP
+ #endif
+             ) ? mutt_strcmp (tmp->path, Context->path) == 0 :
+ #endif
+            (sb.st_dev == contex_sb.st_dev && sb.st_ino == contex_sb.st_ino)
+ #if defined USE_IMAP || defined USE_POP	 
+ 	    )
+ #endif
+ 	)
+        tmp->notified = 1;
+      else if (!tmp->notified)
+        BuffyNotify++;
+     }
   }
 
   BuffyDoneTime = BuffyTime;
   return (BuffyCount);
 }
 
+/* Update the buffy for the current mailbox Context only. */
+int mutt_buffy_check_ctx (void)
+{
+  /* Search the Incoming list of buffies by comparing paths with Context->path.
+     Update the new message count in the matching buffy. */
+  BUFFY *buffy;
+  int ctxlen;
+
+  if (!Context || !Context->path)
+    return -1;
+  ctxlen = strlen(Context->path);
+  if (ctxlen > 0 && Context->magic == M_MAILDIR &&
+      Context->path[ctxlen - 1] == '/')
+    ctxlen--;
+  for (buffy = Incoming; buffy; buffy = buffy->next)
+  {
+    if (!buffy->path)
+      continue;
+    if (strncmp(Context->path, buffy->path, ctxlen) == 0)
+    {
+      if (buffy->new > 0 && Context->new <= 0)
+        BuffyCount--;
+      else if (buffy->new <= 0 && Context->new > 0)
+        BuffyCount++;
+      buffy->new = Context->new;
+      buffy->del = Context->deleted;
+      buffy->cur = Context->msgcount - Context->new - Context->deleted;
+      if (buffy->new <= 0)
+        buffy->notified = 0;
+      else
+        buffy->notified = 1;
+      break;
+    }
+  }
+  return 0;
+}
+
 int mutt_buffy_list (void)
 {
   BUFFY *tmp;
--- mutt-1.5.9/buffy.h	2002-12-11 06:19:39.000000000 -0500
+++ mutt-danborn.1.5.9/buffy.h	2005-02-12 22:06:43.000000000 -0500
@@ -28,6 +28,8 @@
 #endif				/* BUFFY_SIZE */
   struct buffy_t *next;
   short new;			/* mailbox has new mail */
+  short del;            /* Number of messages marked deleted. */
+  short cur;            /* Number of non-new, non-deleted messages. */
   short notified;		/* user has been notified */
   short magic;			/* mailbox type */
   short newly_created;		/* mbox or mmdf just popped into existence */
--- mutt-1.5.9/curs_main.c	2005-03-15 17:26:58.195611320 -0500
+++ mutt-danborn.1.5.9/curs_main.c	2005-03-15 17:25:24.825805688 -0500
@@ -499,6 +499,7 @@
 	  mutt_message _("New mail in this mailbox.");
 	  if (option (OPTBEEPNEW))
 	    beep ();
+      mutt_buffy_check_ctx ();
 	} else if (check == M_FLAGS)
 	  mutt_message _("Mailbox was externally modified.");
 
@@ -1723,6 +1724,7 @@
 	if (tag)
 	{
 	  mutt_tag_set_flag (M_DELETE, 1);
+	  mutt_tag_set_flag (M_NEW, 0);
 	  if (option (OPTDELETEUNTAG))
 	    mutt_tag_set_flag (M_TAG, 0);
 	  menu->redraw = REDRAW_INDEX;
@@ -1730,6 +1732,7 @@
 	else
 	{
 	  mutt_set_flag (Context, CURHDR, M_DELETE, 1);
+	  mutt_set_flag (Context, CURHDR, M_NEW, 0);
 	  if (option (OPTDELETEUNTAG))
 	    mutt_set_flag (Context, CURHDR, M_TAG, 0);
 	  if (option (OPTRESOLVE))
--- mutt-1.5.9/mh.c	2005-02-03 13:47:53.000000000 -0500
+++ mutt-danborn.1.5.9/mh.c	2005-02-12 22:06:43.000000000 -0500
@@ -1886,7 +1886,48 @@
 
   return NULL;
 }
-
+ 
+int maildir_count_dir (BUFFY *buf, char *path) {
+  DIR *dirp;
+  struct dirent *de;
+  if ((dirp = opendir (path)) == NULL)
+  {
+    buf->magic = 0;
+    return errno;
+  }
+  while ((de = readdir (dirp)) != NULL)
+  {
+    char *p;
+    if (*de->d_name != '.')
+    {
+      if (p = strstr (de->d_name, ":2,"))
+      {
+        if (strchr (p + 3, 'T'))
+          buf->del++;
+        else if (strchr (p + 3, 'S'))
+          buf->cur++;
+        else
+          buf->new++;
+      }
+      else
+        buf->new++;
+    }
+  }
+  closedir (dirp);
+  return 0;
+}
+ 
+int maildir_count_buffy (BUFFY *buf) {
+  char path[_POSIX_PATH_MAX];
+  int rv;
+  snprintf (path, sizeof (path), "%s/new", buf->path);
+  if ((rv = maildir_count_dir (buf, path)) != 0)
+    return rv;
+  snprintf (path, sizeof(path), "%s/cur", buf->path);
+  if ((rv = maildir_count_dir (buf, path)) != 0)
+    return rv;
+  return 0;
+}
 
 /*
  * Returns:
--- mutt-1.5.9/mx.c	2005-02-03 13:47:53.000000000 -0500
+++ mutt-danborn.1.5.9/mx.c	2005-02-12 22:06:43.000000000 -0500
@@ -1219,6 +1219,7 @@
     }
   }
 
+  mutt_buffy_check_ctx ();
   return (rc);
 }
 
--- mutt-1.5.9/PATCHES	2005-02-12 15:55:51.000000000 -0500
+++ mutt-danborn.1.5.9/PATCHES	2005-02-12 22:07:09.000000000 -0500
@@ -0,0 +1 @@
+mutt-danborn.1.5.8.patch
