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/lib-lib.h>
18 #include <lib-ui/curses.h>
19 #include <lib-ui/menu.h>
20 #include <lib-ui/sidebar.h>
27 static int TopBuffy = 0;
28 static int CurBuffy = 0;
29 static int known_lines = 0;
30 static short initialized = 0;
31 static short prev_show_value;
33 /* computes first entry to be shown */
34 static void calc_boundaries (void) {
37 if (CurBuffy < 0 || CurBuffy >= Incoming.len)
39 if (TopBuffy < 0 || TopBuffy >= Incoming.len)
42 if (option (OPTSIDEBARNEWMAILONLY)) {
44 TopBuffy = CurBuffy - 1;
46 if (Incoming.arr[i]->new > 0)
50 } else if (known_lines>0)
51 TopBuffy = CurBuffy - (CurBuffy % known_lines);
56 static char *shortened_hierarchy (char *hbox, int maxlen)
59 char *last_dot = NULL;
60 int i, j, len = m_strlen(hbox);
63 if (!SidebarBoundary || !*SidebarBoundary)
64 return (m_strdup(hbox));
66 for (i = 0; i < len; ++i) {
67 if (strchr (SidebarBoundary, hbox[i])) {
75 new_box = p_new(char, maxlen + 1);
77 for (i = 1, j = 1; j < maxlen && i < len; ++i) {
78 if (strchr (SidebarBoundary, hbox[i])) {
79 new_box[j++] = hbox[i];
81 if (&hbox[i + 1] != last_dot || j + m_strlen(last_dot) > maxlen) {
82 new_box[j++] = hbox[i + 1];
85 m_strcat(&new_box[j], maxlen + 1, last_dot);
92 return m_strdup(hbox);
95 static const char* sidebar_number_format (char* dest, ssize_t destlen, char op,
96 const char* src, const char* fmt,
97 const char* ifstr, const char* elstr,
98 anytype data, format_flag flags) {
100 BUFFY* b = Incoming.arr[data.i];
101 int opt = flags & M_FORMAT_OPTIONAL;
102 int c = Context && !m_strcmp(Context->path, b->path);
108 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
109 snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
110 } else if ((c && Context->deleted == 0) || !c)
115 case 'f': /* for compatibility */
117 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
118 snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
119 } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
123 case 'c': /* for compatibility */
126 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
127 snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
128 } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
131 /* total shown, i.e. not hidden by limit */
134 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
135 snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
136 } else if ((c && Context->vcount == 0) || !c)
142 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
143 snprintf (dest, destlen, tmp, c ? Context->new : b->new);
144 } else if ((c && Context->new == 0) || (!c && b->new == 0))
150 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
151 snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
152 } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
158 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
159 snprintf (dest, destlen, tmp, c ? Context->tagged : 0);
160 } else if ((c && Context->tagged == 0) || !c)
165 if (flags & M_FORMAT_OPTIONAL)
166 m_strformat(dest, destlen, opt ? ifstr : elstr, sidebar_number_format,
171 int sidebar_need_count (void) {
172 if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
173 !SidebarNumberFormat || !*SidebarNumberFormat)
180 * 0 item was not printed ('cause of $sidebar_newmail_only)
183 static int make_sidebar_entry (char* sbox, int idx, ssize_t len)
185 int shortened = 0, lencnt = 0;
186 char no[STRING], entry[STRING];
187 int l = m_strlen(ImapHomeNamespace);
188 int l_m = m_strlen(Maildir);
190 if (SidebarWidth > COLS)
193 if (option (OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path &&
194 m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0)
195 /* if $sidebar_newmail_only is set, don't display the
196 * box only if it's not the currently opened
197 * (i.e. always display the currently opened) */
200 m_strformat(no, len, SidebarNumberFormat, sidebar_number_format, idx, 0);
201 lencnt = m_strlen(no);
203 if (l > 0 && m_strncmp(sbox, ImapHomeNamespace, l) == 0 &&
205 sbox += l + 1; /* we're trimming the ImapHomeNamespace, the "+ 1" is for the separator */
207 if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 &&
208 m_strlen(sbox) > l_m) {
210 if (Maildir[strlen(Maildir)-1]!='/') {
214 sbox = basename (sbox);
216 if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len-lencnt-1) {
217 sbox = shortened_hierarchy (sbox, len-lencnt-1);
221 snprintf(entry, sizeof(entry), "%*s", (int)len, no);
222 memcpy(entry, sbox, MIN(len - 1, m_strlen(sbox)));
231 /* returns folder name of currently
232 * selected folder for <sidebar-open>
234 const char* sidebar_get_current (void) {
237 return Incoming.arr[CurBuffy]->path;
240 /* internally sets item to buf */
241 void sidebar_set_current (const char* buf) {
242 int i = buffy_lookup (buf);
249 /* fix counters for a context
250 * FIXME since ctx must not be of our business, move it elsewhere
252 void sidebar_set_buffystats (CONTEXT* curContext) {
255 if (!curContext || !Incoming.len || (i = buffy_lookup (curContext->path)) < 0)
257 tmp = Incoming.arr[i];
258 tmp->new = curContext->new;
259 tmp->msg_unread = curContext->unread;
260 tmp->msgcount = curContext->msgcount;
261 tmp->msg_flagged = curContext->flagged;
264 void sidebar_draw_frames (void) {
267 if (!option(OPTMBOXPANE) || SidebarWidth==0)
270 delim_len=m_strlen(NONULL(SidebarDelim));
272 /* draw vertical delimiter */
273 SETCOLOR (MT_COLOR_SIDEBAR);
274 for (i = 0; i < LINES-1; i++) {
275 move (i, SidebarWidth - delim_len);
276 if (option (OPTASCIICHARS))
277 addstr (NONULL (SidebarDelim));
278 else if (!option (OPTASCIICHARS) && !m_strcmp(SidebarDelim, "|"))
280 else if ((Charset_is_utf8) && !m_strcmp(SidebarDelim, "|"))
281 addstr ("\342\224\202");
283 addstr (NONULL (SidebarDelim));
286 /* fill "gaps" at top+bottom */
287 SETCOLOR(MT_COLOR_STATUS);
288 for (i=0; i<SidebarWidth; i++) {
290 * if we don't have $status_on_top and have $help, fill top
291 * gap with spaces to get bg color
293 if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
298 * if we don't have $status_on_top or we have $help, fill bottom
299 * gap with spaces to get bg color
301 if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
306 SETCOLOR (MT_COLOR_NORMAL);
309 /* actually draws something
310 * FIXME this needs some clue when to do it
312 int sidebar_draw (void) {
313 int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
314 last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
317 ssize_t delim_len = m_strlen(SidebarDelim);
320 known_lines=last_line-first_line;
322 /* initialize first time */
324 prev_show_value = option (OPTMBOXPANE);
328 if (TopBuffy==0 || CurBuffy==0)
331 /* save or restore the value SidebarWidth */
332 if (prev_show_value != option (OPTMBOXPANE)) {
333 if (!prev_show_value && option (OPTMBOXPANE)) {
334 /* after toggle: force recounting of all mail */
337 prev_show_value = option (OPTMBOXPANE);
340 if (SidebarWidth > 0 && option (OPTMBOXPANE)
341 && m_strlen(SidebarDelim) >= SidebarWidth) {
342 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
344 unset_option (OPTMBOXPANE);
348 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
351 sidebar_draw_frames();
356 /* actually print items */
357 for (i = TopBuffy, line=first_line; i < Incoming.len && line < last_line; i++) {
358 tmp = Incoming.arr[i];
361 SETCOLOR (MT_COLOR_INDICATOR);
362 else if (tmp->new > 0)
363 SETCOLOR (MT_COLOR_NEW);
364 else if (tmp->msg_flagged > 0)
365 SETCOLOR (MT_COLOR_FLAGGED);
367 SETCOLOR (MT_COLOR_NORMAL);
370 line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
373 SETCOLOR (MT_COLOR_NORMAL);
375 /* fill with blanks to bottom */
376 memset(&blank, ' ', sizeof(blank));
377 for (; line < last_line; line++) {
379 addnstr (blank, SidebarWidth-delim_len);
384 /* returns index of new item with new mail or -1 */
385 static int exist_next_new () {
390 while (i < Incoming.len)
391 if (Incoming.arr[i++]->new > 0)
396 /* returns index of prev item with new mail or -1 */
397 static int exist_prev_new () {
403 if (Incoming.arr[i--]->new > 0)
408 void sidebar_scroll (int op) {
411 if (!SidebarWidth || !Incoming.len)
415 case OP_SIDEBAR_NEXT:
416 if (!option (OPTSIDEBARNEWMAILONLY)) {
417 if (CurBuffy + 1 == Incoming.len) {
418 mutt_error (_("You are on the last mailbox."));
423 } /* the fall-through is intentional */
424 case OP_SIDEBAR_NEXT_NEW:
425 if ((i = exist_next_new ()) < 0) {
426 mutt_error (_("No next mailboxes with new mail."));
432 case OP_SIDEBAR_PREV:
433 if (!option (OPTSIDEBARNEWMAILONLY)) {
435 mutt_error (_("You are on the first mailbox."));
440 } /* the fall-through is intentional */
441 case OP_SIDEBAR_PREV_NEW:
442 if ((i = exist_prev_new ()) < 0) {
443 mutt_error (_("No previous mailbox with new mail."));
450 case OP_SIDEBAR_SCROLL_UP:
452 mutt_error (_("You are on the first mailbox."));
455 CurBuffy -= known_lines;
459 case OP_SIDEBAR_SCROLL_DOWN:
460 if (CurBuffy + 1 == Incoming.len) {
461 mutt_error (_("You are on the last mailbox."));
464 CurBuffy += known_lines;
465 if (CurBuffy >= Incoming.len)
466 CurBuffy = Incoming.len - 1;