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, NONULL(SidebarNumberFormat), sidebar_number_format,
202 lencnt = m_strlen(no);
204 if (l > 0 && m_strncmp(sbox, ImapHomeNamespace, l) == 0 &&
206 sbox += l + 1; /* we're trimming the ImapHomeNamespace, the "+ 1" is for the separator */
208 if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 &&
209 m_strlen(sbox) > l_m) {
211 if (Maildir[strlen(Maildir)-1]!='/') {
215 sbox = basename (sbox);
217 if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len-lencnt-1) {
218 sbox = shortened_hierarchy (sbox, len-lencnt-1);
222 snprintf(entry, sizeof(entry), "%*s", (int)len, no);
223 memcpy(entry, sbox, MIN(len - 1, m_strlen(sbox)));
232 /* returns folder name of currently
233 * selected folder for <sidebar-open>
235 const char* sidebar_get_current (void) {
238 return Incoming.arr[CurBuffy]->path;
241 /* internally sets item to buf */
242 void sidebar_set_current (const char* buf) {
243 int i = buffy_lookup (buf);
250 /* fix counters for a context
251 * FIXME since ctx must not be of our business, move it elsewhere
253 void sidebar_set_buffystats (CONTEXT* curContext) {
256 if (!curContext || !Incoming.len || (i = buffy_lookup (curContext->path)) < 0)
258 tmp = Incoming.arr[i];
259 tmp->new = curContext->new;
260 tmp->msg_unread = curContext->unread;
261 tmp->msgcount = curContext->msgcount;
262 tmp->msg_flagged = curContext->flagged;
265 void sidebar_draw_frames (void) {
268 if (!option(OPTMBOXPANE) || SidebarWidth==0)
271 delim_len=m_strlen(NONULL(SidebarDelim));
273 /* draw vertical delimiter */
274 SETCOLOR (MT_COLOR_SIDEBAR);
275 for (i = 0; i < LINES-1; i++) {
276 move (i, SidebarWidth - delim_len);
277 if (option (OPTASCIICHARS))
278 addstr (NONULL (SidebarDelim));
279 else if (!option (OPTASCIICHARS) && !m_strcmp(SidebarDelim, "|"))
281 else if ((Charset_is_utf8) && !m_strcmp(SidebarDelim, "|"))
282 addstr ("\342\224\202");
284 addstr (NONULL (SidebarDelim));
287 /* fill "gaps" at top+bottom */
288 SETCOLOR(MT_COLOR_STATUS);
289 for (i=0; i<SidebarWidth; i++) {
291 * if we don't have $status_on_top and have $help, fill top
292 * gap with spaces to get bg color
294 if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
299 * if we don't have $status_on_top or we have $help, fill bottom
300 * gap with spaces to get bg color
302 if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
307 SETCOLOR (MT_COLOR_NORMAL);
310 /* actually draws something
311 * FIXME this needs some clue when to do it
313 int sidebar_draw (void) {
314 int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
315 last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
318 ssize_t delim_len = m_strlen(SidebarDelim);
321 known_lines=last_line-first_line;
323 /* initialize first time */
325 prev_show_value = option (OPTMBOXPANE);
329 if (TopBuffy==0 || CurBuffy==0)
332 /* save or restore the value SidebarWidth */
333 if (prev_show_value != option (OPTMBOXPANE)) {
334 if (!prev_show_value && option (OPTMBOXPANE)) {
335 /* after toggle: force recounting of all mail */
338 prev_show_value = option (OPTMBOXPANE);
341 if (SidebarWidth > 0 && option (OPTMBOXPANE)
342 && m_strlen(SidebarDelim) >= SidebarWidth) {
343 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
345 unset_option (OPTMBOXPANE);
349 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
352 sidebar_draw_frames();
357 /* actually print items */
358 for (i = TopBuffy, line=first_line; i < Incoming.len && line < last_line; i++) {
359 tmp = Incoming.arr[i];
362 SETCOLOR (MT_COLOR_INDICATOR);
363 else if (tmp->new > 0)
364 SETCOLOR (MT_COLOR_NEW);
365 else if (tmp->msg_flagged > 0)
366 SETCOLOR (MT_COLOR_FLAGGED);
368 SETCOLOR (MT_COLOR_NORMAL);
371 line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
374 SETCOLOR (MT_COLOR_NORMAL);
376 /* fill with blanks to bottom */
377 memset(&blank, ' ', sizeof(blank));
378 for (; line < last_line; line++) {
380 addnstr (blank, SidebarWidth-delim_len);
385 /* returns index of new item with new mail or -1 */
386 static int exist_next_new () {
391 while (i < Incoming.len)
392 if (Incoming.arr[i++]->new > 0)
397 /* returns index of prev item with new mail or -1 */
398 static int exist_prev_new () {
404 if (Incoming.arr[i--]->new > 0)
409 void sidebar_scroll (int op) {
412 if (!SidebarWidth || !Incoming.len)
416 case OP_SIDEBAR_NEXT:
417 if (!option (OPTSIDEBARNEWMAILONLY)) {
418 if (CurBuffy + 1 == Incoming.len) {
419 mutt_error (_("You are on the last mailbox."));
424 } /* the fall-through is intentional */
425 case OP_SIDEBAR_NEXT_NEW:
426 if ((i = exist_next_new ()) < 0) {
427 mutt_error (_("No next mailboxes with new mail."));
433 case OP_SIDEBAR_PREV:
434 if (!option (OPTSIDEBARNEWMAILONLY)) {
436 mutt_error (_("You are on the first mailbox."));
441 } /* the fall-through is intentional */
442 case OP_SIDEBAR_PREV_NEW:
443 if ((i = exist_prev_new ()) < 0) {
444 mutt_error (_("No previous mailbox with new mail."));
451 case OP_SIDEBAR_SCROLL_UP:
453 mutt_error (_("You are on the first mailbox."));
456 CurBuffy -= known_lines;
460 case OP_SIDEBAR_SCROLL_DOWN:
461 if (CurBuffy + 1 == Incoming.len) {
462 mutt_error (_("You are on the last mailbox."));
465 CurBuffy += known_lines;
466 if (CurBuffy >= Incoming.len)
467 CurBuffy = Incoming.len - 1;