#include "buffy.h"
#include "keymap.h"
-static int TopBuffy = 0;
static int CurBuffy = 0;
-static short initialized = 0;
-static short prev_show_value;
/* computes first entry to be shown */
-static void calc_boundaries(void)
+static int calc_boundaries(void)
{
- if (!Incoming.len)
- return;
+ int TopBuffy = 0;
+
if (CurBuffy < 0 || CurBuffy >= Incoming.len)
CurBuffy = 0;
- if (TopBuffy < 0 || TopBuffy >= Incoming.len)
- TopBuffy = 0;
-
- if (option (OPTSIDEBARNEWMAILONLY)) {
- int i = CurBuffy;
- TopBuffy = CurBuffy - 1;
- while (i >= 0) {
- if (Incoming.arr[i]->new > 0)
- TopBuffy = i;
- i--;
+
+ if (option(OPTSIDEBARNEWMAILONLY)) {
+ int n = 0;
+
+ for (int i = 0; i < CurBuffy; i++) {
+ n += Incoming.arr[i]->new > 0;
+ }
+
+ n %= (LINES - 3);
+ if (!n)
+ return CurBuffy;
+
+ for (int i = CurBuffy - 1; i >= 0; i--) {
+ if (Incoming.arr[i]->new > 0 && --n <= 0) {
+ return i;
+ }
}
} else {
TopBuffy = CurBuffy - (CurBuffy % (LINES - 3));
}
- if (TopBuffy < 0)
- TopBuffy = 0;
+
+ return TopBuffy < 0 ? 0 : TopBuffy;
}
static char *shortened_hierarchy (char *hbox, int maxlen)
int sidebar_need_count(void)
{
- return ui_layout_sidebar_w()
- && !m_strisempty(SidebarNumberFormat);
+ return ui_layout_sidebar_w() && !m_strisempty(SidebarNumberFormat);
}
/* print single item
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)
- /* if $sidebar_newmail_only is set, don't display the
- * box only if it's not the currently opened
- * (i.e. always display the currently opened) */
+ if (option(OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path
+ && m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0)
return 0;
m_strformat(no, sizeof(no), len, SidebarNumberFormat,
sidebar_number_format, idx, 0);
lencnt = m_strlen(no);
- if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 &&
- m_strlen(sbox) > l_m) {
+ if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 && m_strlen(sbox) > l_m)
+ {
sbox += l_m;
- if (Maildir[strlen(Maildir)-1]!='/') {
+ if (Maildir[strlen(Maildir) - 1] != '/') {
sbox += 1;
}
} else {
sbox = basename(sbox);
}
- if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len - lencnt) {
- sbox = shortened_hierarchy(sbox, len - lencnt);
+ if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len - lencnt - 1) {
+ sbox = shortened_hierarchy(sbox, len - lencnt - 1);
shortened = 1;
}
/* returns folder name of currently
* selected folder for <sidebar-open>
*/
-const char* sidebar_get_current (void) {
- if (!Incoming.len)
- return (NULL);
- return Incoming.arr[CurBuffy]->path;
+const char *sidebar_get_current(void)
+{
+ if (!Incoming.len)
+ return NULL;
+ return Incoming.arr[CurBuffy]->path;
}
/* internally sets item to buf */
-void sidebar_set_current (const char* buf) {
- int i = buffy_lookup (buf);
- if (i >= 0) {
- CurBuffy = i;
- calc_boundaries();
- }
+void sidebar_set_current(const char* buf)
+{
+ int i = buffy_lookup(buf);
+ if (i >= 0) {
+ CurBuffy = i;
+ }
}
/* fix counters for a context
tmp->msg_flagged = curContext->flagged;
}
-/* actually draws something
- * FIXME this needs some clue when to do it
- */
int sidebar_draw(void)
{
+ static short initialized = false, prev_show_value = 0;
int x, y, i, line;
char blank[STRING];
WINDOW *sw;
initialized = 1;
}
- if (TopBuffy == 0 || CurBuffy == 0)
- calc_boundaries();
-
/* save or restore the value SidebarWidth */
if (prev_show_value != option(OPTMBOXPANE)) {
if (!prev_show_value && option(OPTMBOXPANE)) {
WSETCOLOR(sw, MT_COLOR_STATUS);
waddnstr(sw, blank, x);
- for (i = TopBuffy, line = 1; i < Incoming.len && line < y; i++) {
+ line = 1;
+ for (i = calc_boundaries(); i < Incoming.len && line < y - 1; i++) {
BUFFY *tmp = Incoming.arr[i];
if (i == CurBuffy)
return 0;
}
-/* returns index of new item with new mail or -1 */
-static int exist_next_new (void) {
- int i = 0;
- if (!Incoming.len)
- return (-1);
- i = CurBuffy + 1;
- while (i < Incoming.len)
- if (Incoming.arr[i++]->new > 0)
- return (i-1);
- return (-1);
-}
-
-/* returns index of prev item with new mail or -1 */
-static int exist_prev_new (void) {
- int i = 0;
- if (!Incoming.len)
- return (-1);
- i = CurBuffy - 1;
- while (i >= 0)
- if (Incoming.arr[i--]->new > 0)
- return (i+1);
- return (-1);
-}
-
-void sidebar_scroll (int op) {
- int i = 0;
-
- if (!SidebarWidth || !Incoming.len)
- return;
-
- switch (op) {
- case OP_SIDEBAR_NEXT:
- if (!option (OPTSIDEBARNEWMAILONLY)) {
- if (CurBuffy + 1 == Incoming.len) {
- mutt_error (_("You are on the last mailbox."));
+void sidebar_scroll(int op)
+{
+ if (!Incoming.len)
return;
- }
- CurBuffy++;
- break;
- } /* the fall-through is intentional */
- case OP_SIDEBAR_NEXT_NEW:
- if ((i = exist_next_new ()) < 0) {
- mutt_error (_("No next mailboxes with new mail."));
- return;
- }
- else
- CurBuffy = i;
- break;
- case OP_SIDEBAR_PREV:
- if (!option (OPTSIDEBARNEWMAILONLY)) {
- if (CurBuffy == 0) {
- mutt_error (_("You are on the first mailbox."));
+
+ switch (op) {
+ case OP_SIDEBAR_NEXT:
+ if (!option(OPTSIDEBARNEWMAILONLY)) {
+ CurBuffy = (CurBuffy + 1) % Incoming.len;
+ break;
+ }
+ /* FALLTHROUGH */
+ case OP_SIDEBAR_NEXT_NEW:
+ for (int i = 1; i < Incoming.len; i++) {
+ if (Incoming.arr[(CurBuffy + i) % Incoming.len]->new > 0) {
+ CurBuffy = (CurBuffy + i) % Incoming.len;
+ break;
+ }
+ }
+ break;
+
+ case OP_SIDEBAR_PREV:
+ if (!option(OPTSIDEBARNEWMAILONLY)) {
+ if (!CurBuffy) {
+ CurBuffy = Incoming.len;
+ }
+ CurBuffy--;
+ break;
+ }
+ /* FALLTHROUGH */
+ case OP_SIDEBAR_PREV_NEW:
+ for (int i = Incoming.len - 1; i > 0; i--) {
+ if (Incoming.arr[(CurBuffy + i) % Incoming.len]->new > 0) {
+ CurBuffy = (CurBuffy + i) % Incoming.len;
+ break;
+ }
+ }
+ break;
+
+ case OP_SIDEBAR_SCROLL_UP:
+ CurBuffy -= LINES - 3;
+ if (CurBuffy < 0)
+ CurBuffy = 0;
+ break;
+ case OP_SIDEBAR_SCROLL_DOWN:
+ CurBuffy += LINES - 3;
+ if (CurBuffy >= Incoming.len)
+ CurBuffy = Incoming.len - 1;
+ break;
+ default:
return;
- }
- CurBuffy--;
- break;
- } /* the fall-through is intentional */
- case OP_SIDEBAR_PREV_NEW:
- if ((i = exist_prev_new ()) < 0) {
- mutt_error (_("No previous mailbox with new mail."));
- return;
}
- else
- CurBuffy = i;
- break;
-
- case OP_SIDEBAR_SCROLL_UP:
- if (CurBuffy == 0) {
- mutt_error (_("You are on the first mailbox."));
- return;
- }
- CurBuffy -= LINES - 3;
- if (CurBuffy < 0)
- CurBuffy = 0;
- break;
- case OP_SIDEBAR_SCROLL_DOWN:
- if (CurBuffy + 1 == Incoming.len) {
- mutt_error (_("You are on the last mailbox."));
- return;
- }
- CurBuffy += LINES - 3;
- if (CurBuffy >= Incoming.len)
- CurBuffy = Incoming.len - 1;
- break;
- default:
- return;
- }
- calc_boundaries ();
- sidebar_draw ();
+
+ sidebar_draw();
}