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.
15 #include <lib-lib/mem.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/macros.h>
20 #include "mutt_menu.h"
21 #include "mutt_curses.h"
30 static int TopBuffy = 0;
31 static int CurBuffy = 0;
32 static int known_lines = 0;
33 static short initialized = 0;
34 static short prev_show_value;
36 /* computes first entry to be shown */
37 static void calc_boundaries (void) {
38 if (list_empty(Incoming))
40 if (CurBuffy < 0 || CurBuffy >= Incoming->length)
42 if (TopBuffy < 0 || TopBuffy >= Incoming->length)
45 if (option (OPTSIDEBARNEWMAILONLY)) {
47 TopBuffy = CurBuffy - 1;
49 if (((BUFFY*) Incoming->data[i])->new > 0)
53 } else if (known_lines>0)
54 TopBuffy = CurBuffy - (CurBuffy % known_lines);
59 static char *shortened_hierarchy (char *box, int maxlen)
62 char *last_dot = NULL;
63 int i, j, len = m_strlen(box);
66 if (!SidebarBoundary || !*SidebarBoundary)
67 return (m_strdup(box));
69 for (i = 0; i < len; ++i) {
70 if (strchr (SidebarBoundary, box[i])) {
78 new_box = p_new(char, maxlen + 1);
80 for (i = 1, j = 1; j < maxlen && i < len; ++i) {
81 if (strchr (SidebarBoundary, box[i])) {
82 new_box[j++] = box[i];
84 if (&box[i + 1] != last_dot || j + m_strlen(last_dot) > maxlen) {
85 new_box[j++] = box[i + 1];
88 strcat (&new_box[j], last_dot);
98 static const char* sidebar_number_format (char* dest, size_t destlen, char op,
99 const char* src, const char* fmt,
100 const char* ifstr, const char* elstr,
101 unsigned long data, format_flag flags) {
102 char tmp[SHORT_STRING];
103 BUFFY* b = (BUFFY*) Incoming->data[data];
104 int opt = flags & M_FORMAT_OPTIONAL;
105 int c = Context && str_eq (Context->path, b->path);
111 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
112 snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
113 } else if ((c && Context->deleted == 0) || !c)
118 case 'f': /* for compatibility */
120 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
121 snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
122 } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
126 case 'c': /* for compatibility */
129 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
130 snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
131 } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
134 /* total shown, i.e. not hidden by limit */
137 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
138 snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
139 } else if ((c && Context->vcount == 0) || !c)
145 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
146 snprintf (dest, destlen, tmp, c ? Context->new : b->new);
147 } else if ((c && Context->new == 0) || (!c && b->new == 0))
153 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
154 snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
155 } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
161 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
162 snprintf (dest, destlen, tmp, c ? Context->tagged : 0);
163 } else if ((c && Context->tagged == 0) || !c)
169 mutt_FormatString (dest, destlen, ifstr, sidebar_number_format,
170 data, M_FORMAT_OPTIONAL);
171 else if (flags & M_FORMAT_OPTIONAL)
172 mutt_FormatString (dest, destlen, elstr, sidebar_number_format,
173 data, M_FORMAT_OPTIONAL);
177 int sidebar_need_count (void) {
178 if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
179 !SidebarNumberFormat || !*SidebarNumberFormat)
186 * 0 item was not printed ('cause of $sidebar_newmail_only)
189 int make_sidebar_entry (char* box, int idx, size_t len)
191 int shortened = 0, lencnt = 0;
192 char no[SHORT_STRING], entry[SHORT_STRING];
194 int l = m_strlen(ImapHomeNamespace);
196 int l_m = m_strlen(Maildir);
198 if (SidebarWidth > COLS)
201 if (option (OPTSIDEBARNEWMAILONLY) && box && Context && Context->path &&
202 !str_eq (Context->path, box) && ((BUFFY*) Incoming->data[idx])->new == 0)
203 /* if $sidebar_newmail_only is set, don't display the
204 * box only if it's not the currently opened
205 * (i.e. always display the currently opened) */
208 mutt_FormatString (no, len, NONULL (SidebarNumberFormat),
209 sidebar_number_format, idx, M_FORMAT_OPTIONAL);
210 lencnt = m_strlen(no);
211 memset(&entry, ' ', sizeof(entry));
214 if (l > 0 && m_strncmp(box, ImapHomeNamespace, l) == 0 &&
216 box += l + 1; /* we're trimming the ImapHomeNamespace, the "+ 1" is for the separator */
219 if (l_m > 0 && m_strncmp(box, Maildir, l_m) == 0 &&
220 m_strlen(box) > l_m) {
222 if (Maildir[strlen(Maildir)-1]!='/') {
226 box = basename (box);
228 if (option (OPTSHORTENHIERARCHY) && m_strlen(box) > len-lencnt-1) {
229 box = shortened_hierarchy (box, len-lencnt-1);
233 m_strcpy(entry, len - lencnt, box);
234 entry[m_strlen(entry)] = ' ';
235 memcpy(entry + (len - lencnt), no, lencnt);
237 addnstr (entry, len);
245 /* returns folder name of currently
246 * selected folder for <sidebar-open>
248 const char* sidebar_get_current (void) {
249 if (list_empty(Incoming))
251 return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
254 /* internally sets item to buf */
255 void sidebar_set_current (const char* buf) {
256 int i = buffy_lookup (buf);
263 /* fix counters for a context
264 * FIXME since ctx must not be of our business, move it elsewhere
266 void sidebar_set_buffystats (CONTEXT* Context) {
269 if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
271 tmp = (BUFFY*) Incoming->data[i];
272 tmp->new = Context->new;
273 tmp->msg_unread = Context->unread;
274 tmp->msgcount = Context->msgcount;
275 tmp->msg_flagged = Context->flagged;
278 void sidebar_draw_frames (void) {
281 if (!option(OPTMBOXPANE) || SidebarWidth==0)
284 delim_len=m_strlen(NONULL(SidebarDelim));
286 /* draw vertical delimiter */
287 SETCOLOR (MT_COLOR_SIDEBAR);
288 for (i = 0; i < LINES-1; i++) {
289 move (i, SidebarWidth - delim_len);
290 if (option (OPTASCIICHARS))
291 addstr (NONULL (SidebarDelim));
292 else if (!option (OPTASCIICHARS) && !m_strcmp(SidebarDelim, "|"))
294 else if ((Charset_is_utf8) && !m_strcmp(SidebarDelim, "|"))
295 addstr ("\342\224\202");
297 addstr (NONULL (SidebarDelim));
300 /* fill "gaps" at top+bottom */
301 SETCOLOR(MT_COLOR_STATUS);
302 for (i=0; i<SidebarWidth; i++) {
304 * if we don't have $status_on_top and have $help, fill top
305 * gap with spaces to get bg color
307 if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
312 * if we don't have $status_on_top or we have $help, fill bottom
313 * gap with spaces to get bg color
315 if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
320 SETCOLOR (MT_COLOR_NORMAL);
323 /* actually draws something
324 * FIXME this needs some clue when to do it
326 int sidebar_draw (int menu) {
327 int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
328 last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
331 size_t delim_len = m_strlen(SidebarDelim);
332 char blank[SHORT_STRING];
334 known_lines=last_line-first_line;
336 /* initialize first time */
338 prev_show_value = option (OPTMBOXPANE);
342 if (TopBuffy==0 || CurBuffy==0)
345 /* save or restore the value SidebarWidth */
346 if (prev_show_value != option (OPTMBOXPANE)) {
347 if (!prev_show_value && option (OPTMBOXPANE)) {
348 /* after toggle: force recounting of all mail */
351 prev_show_value = option (OPTMBOXPANE);
354 if (SidebarWidth > 0 && option (OPTMBOXPANE)
355 && m_strlen(SidebarDelim) >= SidebarWidth) {
356 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
358 unset_option (OPTMBOXPANE);
362 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
365 sidebar_draw_frames();
367 if (list_empty(Incoming))
370 /* actually print items */
371 for (i = TopBuffy, line=first_line; i < Incoming->length && line < last_line; i++) {
372 tmp = (BUFFY*) Incoming->data[i];
375 SETCOLOR (MT_COLOR_INDICATOR);
376 else if (tmp->new > 0)
377 SETCOLOR (MT_COLOR_NEW);
378 else if (tmp->msg_flagged > 0)
379 SETCOLOR (MT_COLOR_FLAGGED);
381 SETCOLOR (MT_COLOR_NORMAL);
384 line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
387 SETCOLOR (MT_COLOR_NORMAL);
389 /* fill with blanks to bottom */
390 memset(&blank, ' ', sizeof(blank));
391 for (; line < last_line; line++) {
393 addnstr (blank, SidebarWidth-delim_len);
398 /* returns index of new item with new mail or -1 */
399 static int exist_next_new () {
401 if (list_empty(Incoming))
404 while (i < Incoming->length)
405 if (((BUFFY*) Incoming->data[i++])->new > 0)
410 /* returns index of prev item with new mail or -1 */
411 static int exist_prev_new () {
413 if (list_empty(Incoming))
417 if (((BUFFY*) Incoming->data[i--])->new > 0)
422 void sidebar_scroll (int op, int menu) {
425 if (!SidebarWidth || list_empty(Incoming))
429 case OP_SIDEBAR_NEXT:
430 if (!option (OPTSIDEBARNEWMAILONLY)) {
431 if (CurBuffy + 1 == Incoming->length) {
432 mutt_error (_("You are on the last mailbox."));
437 } /* the fall-through is intentional */
438 case OP_SIDEBAR_NEXT_NEW:
439 if ((i = exist_next_new ()) < 0) {
440 mutt_error (_("No next mailboxes with new mail."));
446 case OP_SIDEBAR_PREV:
447 if (!option (OPTSIDEBARNEWMAILONLY)) {
449 mutt_error (_("You are on the first mailbox."));
454 } /* the fall-through is intentional */
455 case OP_SIDEBAR_PREV_NEW:
456 if ((i = exist_prev_new ()) < 0) {
457 mutt_error (_("No previous mailbox with new mail."));
464 case OP_SIDEBAR_SCROLL_UP:
466 mutt_error (_("You are on the first mailbox."));
469 CurBuffy -= known_lines;
473 case OP_SIDEBAR_SCROLL_DOWN:
474 if (CurBuffy + 1 == Incoming->length) {
475 mutt_error (_("You are on the last mailbox."));
478 CurBuffy += known_lines;
479 if (CurBuffy >= Incoming->length)
480 CurBuffy = Incoming->length - 1;