Make layout windows visible, makes the whole thing way simpler.
[apps/madmutt.git] / lib-ui / sidebar.c
index 7c7d6b3..6f35e48 100644 (file)
@@ -28,8 +28,6 @@ static int CurBuffy = 0;
 /* computes first entry to be shown */
 static int calc_boundaries(void)
 {
 /* computes first entry to be shown */
 static int calc_boundaries(void)
 {
-    int TopBuffy = 0;
-
     if (CurBuffy < 0 || CurBuffy >= Incoming.len)
         CurBuffy = 0;
 
     if (CurBuffy < 0 || CurBuffy >= Incoming.len)
         CurBuffy = 0;
 
@@ -49,50 +47,52 @@ static int calc_boundaries(void)
                 return i;
             }
         }
                 return i;
             }
         }
+
+        return 0;
     } else {
     } else {
-        TopBuffy = CurBuffy - (CurBuffy % (LINES - 3));
+        return CurBuffy - (CurBuffy % (LINES - 3));
     }
     }
-
-    return TopBuffy < 0 ? 0 : TopBuffy;
 }
 
 }
 
-static char *shortened_hierarchy (char *hbox, int maxlen)
+static void shortened_hierarchy(char *dst, char *hbox, int maxlen)
 {
 {
-  int dots = 0;
-  char *last_dot = NULL;
-  int i, j, len = m_strlen(hbox);
-  char *new_box;
-
-  if (!SidebarBoundary || !*SidebarBoundary)
-    return (m_strdup(hbox));
-
-  for (i = 0; i < len; ++i) {
-    if (strchr (SidebarBoundary, hbox[i])) {
-      ++dots;
-      last_dot = &hbox[i];
+    int dots = 0, last_dot = 0, len = m_strlen(hbox);
+
+    if (m_strisempty(SidebarBoundary)) {
+        memcpy(dst, hbox, MIN(len, maxlen));
+        return;
     }
     }
-  }
 
 
-  if (last_dot) {
-    ++last_dot;
-    new_box = p_new(char, maxlen + 1);
-    new_box[0] = hbox[0];
-    for (i = 1, j = 1; j < maxlen && i < len; ++i) {
-      if (strchr (SidebarBoundary, hbox[i])) {
-        new_box[j++] = hbox[i];
-        new_box[j] = 0;
-        if (&hbox[i + 1] != last_dot || j + m_strlen(last_dot) > maxlen) {
-          new_box[j++] = hbox[i + 1];
-          new_box[j] = 0;
-        } else {
-          m_strcat(&new_box[j], maxlen + 1, last_dot);
-          break;
+    for (int i = 0; i < len; ++i) {
+        if (strchr(SidebarBoundary, hbox[i])) {
+            ++dots;
+            last_dot = i;
         }
         }
-      }
     }
     }
-    return new_box;
-  }
-  return m_strdup(hbox);
+
+    if (dots == 0) {
+        memcpy(dst, hbox, MIN(len, maxlen));
+        return;
+    }
+
+    dst[0] = hbox[0];
+    for (int i = 1, j = 1; i < len && j < maxlen - 1; ++i) {
+        if (!strchr(SidebarBoundary, hbox[i]))
+            continue;
+
+        if (len - i <= maxlen - j) {
+            memcpy(dst, hbox + i, len - i);
+            return;
+        }
+
+        if (i == last_dot) {
+            memcpy(dst, hbox + i, maxlen - j);
+            return;
+        }
+
+        dst[j++] = hbox[i];
+        dst[j++] = hbox[i + 1];
+    }
 }
 
 static const char *
 }
 
 static const char *
@@ -175,7 +175,7 @@ sidebar_number_format(char* dest, ssize_t destlen,
 
 int sidebar_need_count(void)
 {
 
 int sidebar_need_count(void)
 {
-    return ui_layout_sidebar_w() && !m_strisempty(SidebarNumberFormat);
+    return sidebar_w && !m_strisempty(SidebarNumberFormat);
 }
 
 /* print single item
 }
 
 /* print single item
@@ -185,12 +185,13 @@ int sidebar_need_count(void)
  */
 static int make_sidebar_entry(WINDOW *sw, char *sbox, int idx, ssize_t len)
 {
  */
 static int make_sidebar_entry(WINDOW *sw, char *sbox, int idx, ssize_t len)
 {
-    int shortened = 0, lencnt = 0;
+    int lencnt = 0;
     char no[STRING], entry[STRING];
     int l_m = m_strlen(Maildir);
 
     if (option(OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path
     char no[STRING], entry[STRING];
     int l_m = m_strlen(Maildir);
 
     if (option(OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path
-    &&  m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0)
+    &&  m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0
+    &&  idx != CurBuffy)
         return 0;
 
     m_strformat(no, sizeof(no), len, SidebarNumberFormat,
         return 0;
 
     m_strformat(no, sizeof(no), len, SidebarNumberFormat,
@@ -207,18 +208,14 @@ static int make_sidebar_entry(WINDOW *sw, char *sbox, int idx, ssize_t len)
         sbox = basename(sbox);
     }
 
         sbox = basename(sbox);
     }
 
+    snprintf(entry, sizeof(entry), "%*s", (int)len, no);
     if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len - lencnt - 1) {
     if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len - lencnt - 1) {
-        sbox = shortened_hierarchy(sbox, len - lencnt - 1);
-        shortened = 1;
+        shortened_hierarchy(entry, sbox, len - lencnt - 1);
+    } else {
+        memcpy(entry, sbox, MIN(len - lencnt - 1, m_strlen(sbox)));
     }
     }
-
-    snprintf(entry, sizeof(entry), "%*s", (int)len, no);
-    memcpy(entry, sbox, MIN(len, m_strlen(sbox)));
     waddnstr(sw, entry, len);
 
     waddnstr(sw, entry, len);
 
-    if (shortened)
-        p_delete(&sbox);
-
     return 1;
 }
 
     return 1;
 }
 
@@ -227,9 +224,9 @@ static int make_sidebar_entry(WINDOW *sw, char *sbox, int idx, ssize_t len)
  */
 const char *sidebar_get_current(void)
 {
  */
 const char *sidebar_get_current(void)
 {
-    if (!Incoming.len)
-        return NULL;
-    return Incoming.arr[CurBuffy]->path;
+    if (0 <= CurBuffy && CurBuffy < Incoming.len)
+        return Incoming.arr[CurBuffy]->path;
+    return NULL;
 }
 
 /* internally sets item to buf */
 }
 
 /* internally sets item to buf */
@@ -244,38 +241,33 @@ void sidebar_set_current(const char* buf)
 /* fix counters for a context
  * FIXME since ctx must not be of our business, move it elsewhere
  */
 /* fix counters for a context
  * FIXME since ctx must not be of our business, move it elsewhere
  */
-void sidebar_set_buffystats (CONTEXT* curContext) {
-  int i = 0;
-  BUFFY* tmp = NULL;
-  if (!curContext || !Incoming.len || (i = buffy_lookup (curContext->path)) < 0)
-    return;
-  tmp = Incoming.arr[i];
-  tmp->new = curContext->new;
-  tmp->msg_unread = curContext->unread;
-  tmp->msgcount = curContext->msgcount;
-  tmp->msg_flagged = curContext->flagged;
+void sidebar_set_buffystats(CONTEXT *curContext)
+{
+    int i = 0;
+    BUFFY *tmp;
+
+    if (!curContext || (i = buffy_lookup(curContext->path)) < 0)
+        return;
+    tmp = Incoming.arr[i];
+    tmp->new         = curContext->new;
+    tmp->msg_unread  = curContext->unread;
+    tmp->msgcount    = curContext->msgcount;
+    tmp->msg_flagged = curContext->flagged;
 }
 
 int sidebar_draw(void)
 {
 }
 
 int sidebar_draw(void)
 {
-    static short initialized = false, prev_show_value = 0;
-    int x, y, i, line;
+    static short prev_show_value = -1;
+    int x, y, line;
     char blank[STRING];
     WINDOW *sw;
 
     /* initialize first time */
     char blank[STRING];
     WINDOW *sw;
 
     /* initialize first time */
-    if (!initialized) {
-        prev_show_value = option(OPTMBOXPANE);
-        initialized = 1;
-    }
-
-    /* save or restore the value SidebarWidth */
     if (prev_show_value != option(OPTMBOXPANE)) {
     if (prev_show_value != option(OPTMBOXPANE)) {
-        if (!prev_show_value && option(OPTMBOXPANE)) {
+        if ((prev_show_value = option(OPTMBOXPANE))) {
             /* after toggle: force recounting of all mail */
             /* after toggle: force recounting of all mail */
-            buffy_check (2);
+            buffy_check(2);
         }
         }
-        prev_show_value = option(OPTMBOXPANE);
     }
 
     sw = ui_layout_sidebar_w();
     }
 
     sw = ui_layout_sidebar_w();
@@ -290,7 +282,7 @@ int sidebar_draw(void)
     waddnstr(sw, blank, x);
 
     line = 1;
     waddnstr(sw, blank, x);
 
     line = 1;
-    for (i = calc_boundaries(); i < Incoming.len && line < y - 1; i++) {
+    for (int i = calc_boundaries(); i < Incoming.len && line < y - 1; i++) {
         BUFFY *tmp = Incoming.arr[i];
 
         if (i == CurBuffy)
         BUFFY *tmp = Incoming.arr[i];
 
         if (i == CurBuffy)
@@ -320,7 +312,6 @@ int sidebar_draw(void)
     WSETCOLOR(sw, MT_COLOR_STATUS);
     waddnstr(sw, blank, x);
     WSETCOLOR(sw, MT_COLOR_NORMAL);
     WSETCOLOR(sw, MT_COLOR_STATUS);
     waddnstr(sw, blank, x);
     WSETCOLOR(sw, MT_COLOR_NORMAL);
-
     return 0;
 }
 
     return 0;
 }