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 unsigned long data, format_flag flags) {
100 BUFFY* b = Incoming.arr[data];
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)
166 mutt_FormatString (dest, destlen, ifstr, sidebar_number_format,
167 data, M_FORMAT_OPTIONAL);
168 else if (flags & M_FORMAT_OPTIONAL)
169 mutt_FormatString (dest, destlen, elstr, sidebar_number_format,
170 data, M_FORMAT_OPTIONAL);
174 int sidebar_need_count (void) {
175 if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
176 !SidebarNumberFormat || !*SidebarNumberFormat)
183 * 0 item was not printed ('cause of $sidebar_newmail_only)
186 static int make_sidebar_entry (char* sbox, int idx, ssize_t len)
188 int shortened = 0, lencnt = 0;
189 char no[STRING], entry[STRING];
190 int l = m_strlen(ImapHomeNamespace);
191 int l_m = m_strlen(Maildir);
193 if (SidebarWidth > COLS)
196 if (option (OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path &&
197 m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0)
198 /* if $sidebar_newmail_only is set, don't display the
199 * box only if it's not the currently opened
200 * (i.e. always display the currently opened) */
203 mutt_FormatString (no, len, NONULL (SidebarNumberFormat),
204 sidebar_number_format, idx, M_FORMAT_OPTIONAL);
205 lencnt = m_strlen(no);
206 memset(&entry, ' ', sizeof(entry));
208 if (l > 0 && m_strncmp(sbox, ImapHomeNamespace, l) == 0 &&
210 sbox += l + 1; /* we're trimming the ImapHomeNamespace, the "+ 1" is for the separator */
212 if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 &&
213 m_strlen(sbox) > l_m) {
215 if (Maildir[strlen(Maildir)-1]!='/') {
219 sbox = basename (sbox);
221 if (option (OPTSHORTENHIERARCHY) && m_strlen(sbox) > len-lencnt-1) {
222 sbox = shortened_hierarchy (sbox, len-lencnt-1);
226 m_strcpy(entry, len - lencnt, sbox);
227 entry[m_strlen(entry)] = ' ';
228 memcpy(entry + (len - lencnt), no, lencnt);
230 addnstr (entry, len);
238 /* returns folder name of currently
239 * selected folder for <sidebar-open>
241 const char* sidebar_get_current (void) {
244 return Incoming.arr[CurBuffy]->path;
247 /* internally sets item to buf */
248 void sidebar_set_current (const char* buf) {
249 int i = buffy_lookup (buf);
256 /* fix counters for a context
257 * FIXME since ctx must not be of our business, move it elsewhere
259 void sidebar_set_buffystats (CONTEXT* curContext) {
262 if (!curContext || !Incoming.len || (i = buffy_lookup (curContext->path)) < 0)
264 tmp = Incoming.arr[i];
265 tmp->new = curContext->new;
266 tmp->msg_unread = curContext->unread;
267 tmp->msgcount = curContext->msgcount;
268 tmp->msg_flagged = curContext->flagged;
271 void sidebar_draw_frames (void) {
274 if (!option(OPTMBOXPANE) || SidebarWidth==0)
277 delim_len=m_strlen(NONULL(SidebarDelim));
279 /* draw vertical delimiter */
280 SETCOLOR (MT_COLOR_SIDEBAR);
281 for (i = 0; i < LINES-1; i++) {
282 move (i, SidebarWidth - delim_len);
283 if (option (OPTASCIICHARS))
284 addstr (NONULL (SidebarDelim));
285 else if (!option (OPTASCIICHARS) && !m_strcmp(SidebarDelim, "|"))
287 else if ((Charset_is_utf8) && !m_strcmp(SidebarDelim, "|"))
288 addstr ("\342\224\202");
290 addstr (NONULL (SidebarDelim));
293 /* fill "gaps" at top+bottom */
294 SETCOLOR(MT_COLOR_STATUS);
295 for (i=0; i<SidebarWidth; i++) {
297 * if we don't have $status_on_top and have $help, fill top
298 * gap with spaces to get bg color
300 if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
305 * if we don't have $status_on_top or we have $help, fill bottom
306 * gap with spaces to get bg color
308 if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
313 SETCOLOR (MT_COLOR_NORMAL);
316 /* actually draws something
317 * FIXME this needs some clue when to do it
319 int sidebar_draw (void) {
320 int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
321 last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
324 ssize_t delim_len = m_strlen(SidebarDelim);
327 known_lines=last_line-first_line;
329 /* initialize first time */
331 prev_show_value = option (OPTMBOXPANE);
335 if (TopBuffy==0 || CurBuffy==0)
338 /* save or restore the value SidebarWidth */
339 if (prev_show_value != option (OPTMBOXPANE)) {
340 if (!prev_show_value && option (OPTMBOXPANE)) {
341 /* after toggle: force recounting of all mail */
344 prev_show_value = option (OPTMBOXPANE);
347 if (SidebarWidth > 0 && option (OPTMBOXPANE)
348 && m_strlen(SidebarDelim) >= SidebarWidth) {
349 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
351 unset_option (OPTMBOXPANE);
355 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
358 sidebar_draw_frames();
363 /* actually print items */
364 for (i = TopBuffy, line=first_line; i < Incoming.len && line < last_line; i++) {
365 tmp = Incoming.arr[i];
368 SETCOLOR (MT_COLOR_INDICATOR);
369 else if (tmp->new > 0)
370 SETCOLOR (MT_COLOR_NEW);
371 else if (tmp->msg_flagged > 0)
372 SETCOLOR (MT_COLOR_FLAGGED);
374 SETCOLOR (MT_COLOR_NORMAL);
377 line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
380 SETCOLOR (MT_COLOR_NORMAL);
382 /* fill with blanks to bottom */
383 memset(&blank, ' ', sizeof(blank));
384 for (; line < last_line; line++) {
386 addnstr (blank, SidebarWidth-delim_len);
391 /* returns index of new item with new mail or -1 */
392 static int exist_next_new () {
397 while (i < Incoming.len)
398 if (Incoming.arr[i++]->new > 0)
403 /* returns index of prev item with new mail or -1 */
404 static int exist_prev_new () {
410 if (Incoming.arr[i--]->new > 0)
415 void sidebar_scroll (int op) {
418 if (!SidebarWidth || !Incoming.len)
422 case OP_SIDEBAR_NEXT:
423 if (!option (OPTSIDEBARNEWMAILONLY)) {
424 if (CurBuffy + 1 == Incoming.len) {
425 mutt_error (_("You are on the last mailbox."));
430 } /* the fall-through is intentional */
431 case OP_SIDEBAR_NEXT_NEW:
432 if ((i = exist_next_new ()) < 0) {
433 mutt_error (_("No next mailboxes with new mail."));
439 case OP_SIDEBAR_PREV:
440 if (!option (OPTSIDEBARNEWMAILONLY)) {
442 mutt_error (_("You are on the first mailbox."));
447 } /* the fall-through is intentional */
448 case OP_SIDEBAR_PREV_NEW:
449 if ((i = exist_prev_new ()) < 0) {
450 mutt_error (_("No previous mailbox with new mail."));
457 case OP_SIDEBAR_SCROLL_UP:
459 mutt_error (_("You are on the first mailbox."));
462 CurBuffy -= known_lines;
466 case OP_SIDEBAR_SCROLL_DOWN:
467 if (CurBuffy + 1 == Incoming.len) {
468 mutt_error (_("You are on the last mailbox."));
471 CurBuffy += known_lines;
472 if (CurBuffy >= Incoming.len)
473 CurBuffy = Incoming.len - 1;