2 * Copyright notice from original mutt:
3 * Copyright (C) ????-2004 Justin Hibbits <jrh29@po.cwru.edu>
4 * Copyright (C) 2004 Thomer M. Gil <mutt@thomer.com>
6 * Parts were written/modified by:
7 * Rocco Rutte <pdmef@cs.tu-berlin.de>
8 * Nico Golde <nico@ngolde.de>
10 * This file is part of mutt-ng, see http://www.muttng.org/.
11 * It's licensed under the GNU General Public License,
12 * please see the file GPL in the top level source directory.
16 #include "mutt_menu.h"
17 #include "mutt_curses.h"
24 /*BUFFY *CurBuffy = 0;*/
25 static BUFFY *TopBuffy = 0;
26 static BUFFY *BottomBuffy = 0;
27 static int known_lines = 0;
28 static short initialized = 0;
29 static int prev_show_value;
30 static short saveSidebarWidth;
31 static char *entry = 0;
33 static int quick_log10 (int n)
37 for (; n > 9; len++, n /= 10);
41 /* CurBuffy should contain a valid buffy
42 * mailbox before calling this function!!! */
43 void calc_boundaries (int menu)
45 BUFFY *tmp = Incoming;
47 int i, count, mailbox_position;
49 /* correct known_lines if it has changed because of a window resize */
50 if (known_lines != LINES) {
53 /* fix all the prev links on all the mailboxes
54 * FIXME move this over to buffy.c where it belongs */
55 for (; tmp->next != 0; tmp = tmp->next)
56 tmp->next->prev = tmp;
58 /* calculate the position of the current mailbox */
61 while (tmp != CurBuffy) {
65 /* calculate the size of the screen we can use */
66 count = LINES - 2 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
67 /* calculate the position of the current mailbox on the screen */
68 mailbox_position = position % count;
69 if (mailbox_position == 0)
70 mailbox_position = count;
71 /* determine topbuffy */
73 for (i = mailbox_position; i > 1; i--)
74 TopBuffy = TopBuffy->prev;
75 /* determine bottombuffy */
76 BottomBuffy = CurBuffy;
77 for (i = mailbox_position; i < count && BottomBuffy->next; i++)
78 BottomBuffy = BottomBuffy->next;
81 static char *shortened_hierarchy (char *box)
88 for (i = 0; i < mutt_strlen (box); ++i) {
91 else if (isupper (box[i]))
92 return (safe_strdup (box));
94 last_dot = strrchr (box, '.');
97 new_box = safe_malloc (mutt_strlen (last_dot) + 2 * dots + 1);
99 for (i = 1, j = 1; i < mutt_strlen (box); ++i) {
103 if (&box[i + 1] != last_dot) {
104 new_box[j++] = box[i + 1];
108 strcat (&new_box[j], last_dot);
115 return safe_strdup (box);
118 char *make_sidebar_entry (char *box, int size, int new, int flagged)
120 int i = 0, dlen, max, shortened = 0;
123 if (SidebarWidth > COLS)
126 dlen = mutt_strlen (SidebarDelim);
127 max = SidebarWidth - dlen - 1;
129 safe_realloc (&entry, SidebarWidth + 1);
130 entry[SidebarWidth] = 0;
131 for (; i < SidebarWidth; entry[i++] = ' ');
133 if (ImapHomeNamespace && mutt_strlen (ImapHomeNamespace) > 0) {
134 if (strncmp (box, ImapHomeNamespace, mutt_strlen (ImapHomeNamespace)) == 0
135 && strcmp (box, ImapHomeNamespace) != 0) {
136 box += mutt_strlen (ImapHomeNamespace) + 1;
140 max -= quick_log10 (size);
142 max -= quick_log10 (new) + 2;
144 max -= quick_log10 (flagged) + 2;
145 if (option (OPTSHORTENHIERARCHY) && mutt_strlen (box) > max) {
146 box = shortened_hierarchy (box);
149 i = mutt_strlen (box);
150 strncpy (entry, box, i < SidebarWidth - dlen ? i : SidebarWidth - dlen);
155 SidebarWidth - 5 - quick_log10 (size) - dlen - quick_log10 (new) -
156 quick_log10 (flagged);
159 snprintf (entry + offset, SidebarWidth - dlen - offset + 1,
160 "% d(%d)[%d]", size, new, flagged);
164 SidebarWidth - 3 - quick_log10 (size) - dlen - quick_log10 (new);
167 snprintf (entry + offset, SidebarWidth - dlen - offset + 1,
168 "% d(%d)", size, new);
174 SidebarWidth - 3 - quick_log10 (size) - dlen - quick_log10 (flagged);
177 snprintf (entry + offset, SidebarWidth - dlen - offset + 1,
178 "% d[%d]", size, flagged);
181 offset = SidebarWidth - 1 - quick_log10 (size) - dlen;
184 snprintf (entry + offset, SidebarWidth - dlen - offset + 1,
189 if (option (OPTSHORTENHIERARCHY) && shortened) {
195 void set_curbuffy (char buf[LONG_STRING])
197 BUFFY *tmp = CurBuffy = Incoming;
203 if (!strcmp (tmp->path, buf)) {
215 void set_buffystats (CONTEXT * Context)
217 BUFFY *tmp = Incoming;
222 if (strcmp (tmp->path, Context->path) == 0) {
223 tmp->new = Context->new;
224 tmp->msg_unread = Context->unread;
225 tmp->msgcount = Context->msgcount;
226 tmp->msg_flagged = Context->flagged;
233 int draw_sidebar (int menu)
236 int lines = option (OPTHELP) ? 1 : 0;
238 short delim_len = mutt_strlen (SidebarDelim);
240 /* initialize first time */
242 prev_show_value = option (OPTMBOXPANE);
243 saveSidebarWidth = SidebarWidth;
244 if (!option (OPTMBOXPANE))
249 /* save or restore the value SidebarWidth */
250 if (prev_show_value != option (OPTMBOXPANE)) {
251 if (prev_show_value && !option (OPTMBOXPANE)) {
252 saveSidebarWidth = SidebarWidth;
255 else if (!prev_show_value && option (OPTMBOXPANE)) {
256 SidebarWidth = saveSidebarWidth;
257 /* after toggle: force recounting of all mail */
258 mutt_buffy_check (2);
260 prev_show_value = option (OPTMBOXPANE);
263 if (SidebarWidth > 0 && option (OPTMBOXPANE)
264 && mutt_strlen (SidebarDelim) >= SidebarWidth) {
265 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
267 unset_option (OPTMBOXPANE);
271 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
274 /* draw the divider */
275 /* SETCOLOR(MT_COLOR_STATUS); */
276 SETCOLOR (MT_COLOR_SIDEBAR);
278 lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
280 move (lines, SidebarWidth - delim_len);
281 if (option (OPTASCIICHARS))
282 addstr (NONULL (SidebarDelim));
283 else if (!option (OPTASCIICHARS) && !strcmp (SidebarDelim, "|"))
285 else if ((Charset_is_utf8) && !strcmp (SidebarDelim, "|"))
286 addstr ("\342\224\202");
288 addstr (NONULL (SidebarDelim));
290 SETCOLOR (MT_COLOR_NORMAL);
294 lines = option (OPTHELP) ? 1 : 0; /* go back to the top */
299 if (known_lines != LINES || TopBuffy == 0 || BottomBuffy == 0)
301 calc_boundaries (menu);
306 tmp && lines < LINES - 1 - (menu != MENU_PAGER
307 || option (OPTSTATUSONTOP));
310 SETCOLOR (MT_COLOR_INDICATOR);
311 else if (tmp->msg_flagged > 0)
312 SETCOLOR (MT_COLOR_FLAGGED);
313 else if (tmp->msg_unread > 0)
314 SETCOLOR (MT_COLOR_NEW);
316 SETCOLOR (MT_COLOR_NORMAL);
319 if (option (OPTSIDEBARNEWMAILONLY)) {
320 if (tmp->msg_unread > 0) {
321 if (Context && !strcmp (tmp->path, Context->path)) {
322 printw ("%.*s", SidebarWidth - delim_len,
323 make_sidebar_entry (basename (tmp->path),
324 Context->msgcount, Context->unread,
326 tmp->msg_unread = Context->unread;
327 tmp->msgcount = Context->msgcount;
328 tmp->msg_flagged = Context->flagged;
331 printw ("%.*s", SidebarWidth - delim_len,
332 make_sidebar_entry (basename (tmp->path),
333 tmp->msgcount, tmp->msg_unread,
339 if (Context && !strcmp (tmp->path, Context->path)) {
340 printw ("%.*s", SidebarWidth - delim_len,
341 make_sidebar_entry (basename (tmp->path),
342 Context->msgcount, Context->unread,
344 tmp->msg_unread = Context->unread;
345 tmp->msgcount = Context->msgcount;
346 tmp->msg_flagged = Context->flagged;
349 printw ("%.*s", SidebarWidth - delim_len,
350 make_sidebar_entry (basename (tmp->path),
351 tmp->msgcount, tmp->msg_unread,
356 SETCOLOR (MT_COLOR_NORMAL);
357 for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
362 for (; i < SidebarWidth - delim_len; i++)
368 BUFFY *exist_next_new ()
370 BUFFY *tmp = CurBuffy;
374 while (tmp->next != NULL) {
382 BUFFY *exist_prev_new ()
384 BUFFY *tmp = CurBuffy;
388 while (tmp->prev != NULL) {
397 void scroll_sidebar (int op, int menu)
407 case OP_SIDEBAR_NEXT:
408 if (!option (OPTSIDEBARNEWMAILONLY)) {
409 if (CurBuffy->next == NULL) {
410 mutt_error (_("You are on the last mailbox."));
413 CurBuffy = CurBuffy->next;
415 } /* the fall-through is intentional */
416 case OP_SIDEBAR_NEXT_NEW:
417 if ((tmp = exist_next_new ()) == NULL) {
418 mutt_error (_("No next mailboxes with new mail."));
424 case OP_SIDEBAR_PREV:
425 if (!option (OPTSIDEBARNEWMAILONLY)) {
426 if (CurBuffy->prev == NULL) {
427 mutt_error (_("You are on the first mailbox."));
430 CurBuffy = CurBuffy->prev;
432 } /* the fall-through is intentional */
433 case OP_SIDEBAR_PREV_NEW:
434 if ((tmp = exist_prev_new ()) == NULL) {
435 mutt_error (_("No previous mailbox with new mail."));
442 case OP_SIDEBAR_SCROLL_UP:
444 if (CurBuffy != Incoming) {
445 calc_boundaries (menu);
446 CurBuffy = CurBuffy->prev;
449 case OP_SIDEBAR_SCROLL_DOWN:
450 CurBuffy = BottomBuffy;
451 if (CurBuffy->next) {
452 calc_boundaries (menu);
453 CurBuffy = CurBuffy->next;
459 calc_boundaries (menu);