Rocco Rutte:
[apps/madmutt.git] / sidebar.c
index 5df3fd8..a0b888f 100644 (file)
--- a/sidebar.c
+++ b/sidebar.c
@@ -31,7 +31,6 @@ static int CurBuffy = 0;
 static int known_lines = 0;
 static short initialized = 0;
 static short prev_show_value;
-static short saveSidebarWidth;
 
 /* computes first entry to be shown */
 void calc_boundaries (int menu)
@@ -40,11 +39,12 @@ void calc_boundaries (int menu)
 
   if (list_empty(Incoming))
     return;
-  /* correct known_lines if it has changed because of a window resize */
-  /*  if (known_lines != LINES)
-    known_lines = LINES; */
-  
-  lines = LINES - 2 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
+  if (CurBuffy < 0 || CurBuffy >= Incoming->length)
+    CurBuffy = 0;
+  if (TopBuffy < 0 || TopBuffy >= Incoming->length)
+    TopBuffy = 0;
+
+  lines = LINES - 2 - !option (OPTHELP);
   known_lines = lines;
   if (option (OPTSIDEBARNEWMAILONLY)) {
     int i = CurBuffy;
@@ -79,7 +79,7 @@ static char *shortened_hierarchy (char *box, int maxlen)
 
   if (last_dot) {
     ++last_dot;
-    new_box = safe_malloc (maxlen + 1);
+    new_box = mem_malloc (maxlen + 1);
     new_box[0] = box[0];
     for (i = 1, j = 1; j < maxlen && i < len; ++i) {
       if (strchr (SidebarBoundary, box[i])) {
@@ -145,6 +145,14 @@ static const char* sidebar_number_format (char* dest, size_t destlen, char op,
       break;
     /* new */
     case 'n':
+      if (!opt) {
+        snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
+        snprintf (dest, destlen, tmp, c ? Context->new : b->new);
+      } else if ((c && Context->new == 0) || (!c && b->new == 0))
+        opt = 0;
+      break;
+    /* unread */
+    case 'u':
       if (!opt) {
         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
         snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
@@ -189,6 +197,7 @@ int make_sidebar_entry (char* box, int idx, size_t len)
 #if USE_IMAP
   int l = str_len (ImapHomeNamespace);
 #endif
+  int l_m = str_len (Maildir);
 
   if (SidebarWidth > COLS)
     SidebarWidth = COLS;
@@ -208,9 +217,16 @@ int make_sidebar_entry (char* box, int idx, size_t len)
 #if USE_IMAP
   if (l > 0 && str_ncmp (box, ImapHomeNamespace, l) == 0 && 
       str_len (box) > l)
-    box += l + 1;
+    box += l + 1; /* we're trimming the ImapHomeNamespace, the "+ 1" is for the separator */
   else
 #endif
+  if (l_m > 0 && str_ncmp (box, Maildir, l_m) == 0 && 
+      str_len (box) > l_m) {
+    box += l_m;
+    if (Maildir[strlen(Maildir)-1]!='/') {
+      box += 1;
+    }
+  } else
     box = basename (box);
 
   if (option (OPTSHORTENHIERARCHY) && str_len (box) > len-lencnt-1) {
@@ -225,7 +241,7 @@ int make_sidebar_entry (char* box, int idx, size_t len)
   addnstr (entry, len);
 
   if (shortened)
-    FREE(&box);
+    mem_free(&box);
 
   return (1);
 }
@@ -266,31 +282,31 @@ void sidebar_set_buffystats (CONTEXT* Context) {
  */
 int sidebar_draw (int menu)
 {
-
-  int lines = option (OPTHELP) ? 1 : 0, draw_devider = 1, i = 0;
+  int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0;
+  int last_line = LINES-1;
+  int draw_devider = 1, i = 0;
+  int line;
   BUFFY *tmp;
   short delim_len = str_len (SidebarDelim);
   char blank[SHORT_STRING];
 
+  if (option (OPTSTATUSONTOP)) {
+    last_line -= option (OPTHELP) ? 1 : 0;
+  } else {
+    last_line -= 1-(menu==MENU_PAGER);
+  }
+
   /* initialize first time */
   if (!initialized) {
     prev_show_value = option (OPTMBOXPANE);
-    saveSidebarWidth = SidebarWidth;
-    if (!option (OPTMBOXPANE)){
-      SidebarWidth = 0;
+    if (!option (OPTMBOXPANE))
       draw_devider = 1;
-    }
     initialized = 1;
   }
 
   /* save or restore the value SidebarWidth */
   if (prev_show_value != option (OPTMBOXPANE)) {
-    if (prev_show_value && !option (OPTMBOXPANE)) {
-      saveSidebarWidth = SidebarWidth;
-      SidebarWidth = 0;
-    }
-    else if (!prev_show_value && option (OPTMBOXPANE)) {
-      SidebarWidth = saveSidebarWidth;
+    if (!prev_show_value && option (OPTMBOXPANE)) {
       /* after toggle: force recounting of all mail */
       buffy_check (2);
     }
@@ -311,10 +327,8 @@ int sidebar_draw (int menu)
   if (draw_devider == 1){
     /* draw the divider */
     SETCOLOR (MT_COLOR_SIDEBAR);
-    for (lines = 1;
-         lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
-         lines++) {
-      move (lines, SidebarWidth - delim_len);
+    for (line = first_line; line < last_line; line++) {
+      move (line, SidebarWidth - delim_len);
       if (option (OPTASCIICHARS))
         addstr (NONULL (SidebarDelim));
       else if (!option (OPTASCIICHARS) && !str_cmp (SidebarDelim, "|"))
@@ -329,12 +343,11 @@ int sidebar_draw (int menu)
 
   if (list_empty(Incoming))
     return 0;
-  lines = option (OPTHELP) ? 1 : 0;     /* go back to the top */
+  line = first_line;
   calc_boundaries (menu);
 
   /* actually print items */
-  for (i = TopBuffy; i < Incoming->length && lines < LINES - 1 - 
-       (menu != MENU_PAGER || option (OPTSTATUSONTOP)); i++) {
+  for (i = TopBuffy; i < Incoming->length && line < last_line; i++) {
     tmp = (BUFFY*) Incoming->data[i];
 
     if (i == CurBuffy)
@@ -346,15 +359,15 @@ int sidebar_draw (int menu)
     else
       SETCOLOR (MT_COLOR_NORMAL);
 
-    move (lines, 0);
-    lines += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
+    move (line, 0);
+    line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
   }
 
   /* fill with blanks to bottom */
   memset (&blank, ' ', sizeof (blank));
   SETCOLOR (MT_COLOR_NORMAL);
-  for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++) {
-    move (lines, 0);
+  for (; line < last_line; line++) {
+    move (line, 0);
     addnstr (blank, SidebarWidth-delim_len);
   }
   return 0;