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.
18 #include <lib-lib/lib-lib.h>
20 #include <lib-ui/curses.h>
21 #include <lib-ui/menu.h>
22 #include <lib-ui/sidebar.h>
29 static int TopBuffy = 0;
30 static int CurBuffy = 0;
31 static int known_lines = 0;
32 static short initialized = 0;
33 static short prev_show_value;
35 /* computes first entry to be shown */
36 static void calc_boundaries (void) {
37 if (list_empty(Incoming))
39 if (CurBuffy < 0 || CurBuffy >= Incoming->length)
41 if (TopBuffy < 0 || TopBuffy >= Incoming->length)
44 if (option (OPTSIDEBARNEWMAILONLY)) {
46 TopBuffy = CurBuffy - 1;
48 if (((BUFFY*) Incoming->data[i])->new > 0)
52 } else if (known_lines>0)
53 TopBuffy = CurBuffy - (CurBuffy % known_lines);
58 static char *shortened_hierarchy (char *box, int maxlen)
61 char *last_dot = NULL;
62 int i, j, len = m_strlen(box);
65 if (!SidebarBoundary || !*SidebarBoundary)
66 return (m_strdup(box));
68 for (i = 0; i < len; ++i) {
69 if (strchr (SidebarBoundary, box[i])) {
77 new_box = p_new(char, maxlen + 1);
79 for (i = 1, j = 1; j < maxlen && i < len; ++i) {
80 if (strchr (SidebarBoundary, box[i])) {
81 new_box[j++] = box[i];
83 if (&box[i + 1] != last_dot || j + m_strlen(last_dot) > maxlen) {
84 new_box[j++] = box[i + 1];
87 strcat (&new_box[j], last_dot);
97 static const char* sidebar_number_format (char* dest, ssize_t destlen, char op,
98 const char* src, const char* fmt,
99 const char* ifstr, const char* elstr,
100 unsigned long data, format_flag flags) {
101 char tmp[SHORT_STRING];
102 BUFFY* b = (BUFFY*) Incoming->data[data];
103 int opt = flags & M_FORMAT_OPTIONAL;
104 int c = Context && !m_strcmp(Context->path, b->path);
110 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
111 snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
112 } else if ((c && Context->deleted == 0) || !c)
117 case 'f': /* for compatibility */
119 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
120 snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
121 } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
125 case 'c': /* for compatibility */
128 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
129 snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
130 } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
133 /* total shown, i.e. not hidden by limit */
136 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
137 snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
138 } else if ((c && Context->vcount == 0) || !c)
144 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
145 snprintf (dest, destlen, tmp, c ? Context->new : b->new);
146 } else if ((c && Context->new == 0) || (!c && b->new == 0))
152 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
153 snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
154 } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
160 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
161 snprintf (dest, destlen, tmp, c ? Context->tagged : 0);
162 } else if ((c && Context->tagged == 0) || !c)
168 mutt_FormatString (dest, destlen, ifstr, sidebar_number_format,
169 data, M_FORMAT_OPTIONAL);
170 else if (flags & M_FORMAT_OPTIONAL)
171 mutt_FormatString (dest, destlen, elstr, sidebar_number_format,
172 data, M_FORMAT_OPTIONAL);
176 int sidebar_need_count (void) {
177 if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
178 !SidebarNumberFormat || !*SidebarNumberFormat)
185 * 0 item was not printed ('cause of $sidebar_newmail_only)
188 int make_sidebar_entry (char* box, int idx, ssize_t len)
190 int shortened = 0, lencnt = 0;
191 char no[SHORT_STRING], entry[SHORT_STRING];
192 int l = m_strlen(ImapHomeNamespace);
193 int l_m = m_strlen(Maildir);
195 if (SidebarWidth > COLS)
198 if (option (OPTSIDEBARNEWMAILONLY) && box && Context && Context->path &&
199 m_strcmp(Context->path, box) && ((BUFFY*) Incoming->data[idx])->new == 0)
200 /* if $sidebar_newmail_only is set, don't display the
201 * box only if it's not the currently opened
202 * (i.e. always display the currently opened) */
205 mutt_FormatString (no, len, NONULL (SidebarNumberFormat),
206 sidebar_number_format, idx, M_FORMAT_OPTIONAL);
207 lencnt = m_strlen(no);
208 memset(&entry, ' ', sizeof(entry));
210 if (l > 0 && m_strncmp(box, ImapHomeNamespace, l) == 0 &&
212 box += l + 1; /* we're trimming the ImapHomeNamespace, the "+ 1" is for the separator */
214 if (l_m > 0 && m_strncmp(box, Maildir, l_m) == 0 &&
215 m_strlen(box) > l_m) {
217 if (Maildir[strlen(Maildir)-1]!='/') {
221 box = basename (box);
223 if (option (OPTSHORTENHIERARCHY) && m_strlen(box) > len-lencnt-1) {
224 box = shortened_hierarchy (box, len-lencnt-1);
228 m_strcpy(entry, len - lencnt, box);
229 entry[m_strlen(entry)] = ' ';
230 memcpy(entry + (len - lencnt), no, lencnt);
232 addnstr (entry, len);
240 /* returns folder name of currently
241 * selected folder for <sidebar-open>
243 const char* sidebar_get_current (void) {
244 if (list_empty(Incoming))
246 return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
249 /* internally sets item to buf */
250 void sidebar_set_current (const char* buf) {
251 int i = buffy_lookup (buf);
258 /* fix counters for a context
259 * FIXME since ctx must not be of our business, move it elsewhere
261 void sidebar_set_buffystats (CONTEXT* Context) {
264 if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
266 tmp = (BUFFY*) Incoming->data[i];
267 tmp->new = Context->new;
268 tmp->msg_unread = Context->unread;
269 tmp->msgcount = Context->msgcount;
270 tmp->msg_flagged = Context->flagged;
273 void sidebar_draw_frames (void) {
276 if (!option(OPTMBOXPANE) || SidebarWidth==0)
279 delim_len=m_strlen(NONULL(SidebarDelim));
281 /* draw vertical delimiter */
282 SETCOLOR (MT_COLOR_SIDEBAR);
283 for (i = 0; i < LINES-1; i++) {
284 move (i, SidebarWidth - delim_len);
285 if (option (OPTASCIICHARS))
286 addstr (NONULL (SidebarDelim));
287 else if (!option (OPTASCIICHARS) && !m_strcmp(SidebarDelim, "|"))
289 else if ((Charset_is_utf8) && !m_strcmp(SidebarDelim, "|"))
290 addstr ("\342\224\202");
292 addstr (NONULL (SidebarDelim));
295 /* fill "gaps" at top+bottom */
296 SETCOLOR(MT_COLOR_STATUS);
297 for (i=0; i<SidebarWidth; i++) {
299 * if we don't have $status_on_top and have $help, fill top
300 * gap with spaces to get bg color
302 if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
307 * if we don't have $status_on_top or we have $help, fill bottom
308 * gap with spaces to get bg color
310 if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
315 SETCOLOR (MT_COLOR_NORMAL);
318 /* actually draws something
319 * FIXME this needs some clue when to do it
321 int sidebar_draw (int menu) {
322 int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
323 last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
326 ssize_t delim_len = m_strlen(SidebarDelim);
327 char blank[SHORT_STRING];
329 known_lines=last_line-first_line;
331 /* initialize first time */
333 prev_show_value = option (OPTMBOXPANE);
337 if (TopBuffy==0 || CurBuffy==0)
340 /* save or restore the value SidebarWidth */
341 if (prev_show_value != option (OPTMBOXPANE)) {
342 if (!prev_show_value && option (OPTMBOXPANE)) {
343 /* after toggle: force recounting of all mail */
346 prev_show_value = option (OPTMBOXPANE);
349 if (SidebarWidth > 0 && option (OPTMBOXPANE)
350 && m_strlen(SidebarDelim) >= SidebarWidth) {
351 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
353 unset_option (OPTMBOXPANE);
357 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
360 sidebar_draw_frames();
362 if (list_empty(Incoming))
365 /* actually print items */
366 for (i = TopBuffy, line=first_line; i < Incoming->length && line < last_line; i++) {
367 tmp = (BUFFY*) Incoming->data[i];
370 SETCOLOR (MT_COLOR_INDICATOR);
371 else if (tmp->new > 0)
372 SETCOLOR (MT_COLOR_NEW);
373 else if (tmp->msg_flagged > 0)
374 SETCOLOR (MT_COLOR_FLAGGED);
376 SETCOLOR (MT_COLOR_NORMAL);
379 line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
382 SETCOLOR (MT_COLOR_NORMAL);
384 /* fill with blanks to bottom */
385 memset(&blank, ' ', sizeof(blank));
386 for (; line < last_line; line++) {
388 addnstr (blank, SidebarWidth-delim_len);
393 /* returns index of new item with new mail or -1 */
394 static int exist_next_new () {
396 if (list_empty(Incoming))
399 while (i < Incoming->length)
400 if (((BUFFY*) Incoming->data[i++])->new > 0)
405 /* returns index of prev item with new mail or -1 */
406 static int exist_prev_new () {
408 if (list_empty(Incoming))
412 if (((BUFFY*) Incoming->data[i--])->new > 0)
417 void sidebar_scroll (int op, int menu) {
420 if (!SidebarWidth || list_empty(Incoming))
424 case OP_SIDEBAR_NEXT:
425 if (!option (OPTSIDEBARNEWMAILONLY)) {
426 if (CurBuffy + 1 == Incoming->length) {
427 mutt_error (_("You are on the last mailbox."));
432 } /* the fall-through is intentional */
433 case OP_SIDEBAR_NEXT_NEW:
434 if ((i = exist_next_new ()) < 0) {
435 mutt_error (_("No next mailboxes with new mail."));
441 case OP_SIDEBAR_PREV:
442 if (!option (OPTSIDEBARNEWMAILONLY)) {
444 mutt_error (_("You are on the first mailbox."));
449 } /* the fall-through is intentional */
450 case OP_SIDEBAR_PREV_NEW:
451 if ((i = exist_prev_new ()) < 0) {
452 mutt_error (_("No previous mailbox with new mail."));
459 case OP_SIDEBAR_SCROLL_UP:
461 mutt_error (_("You are on the first mailbox."));
464 CurBuffy -= known_lines;
468 case OP_SIDEBAR_SCROLL_DOWN:
469 if (CurBuffy + 1 == Incoming->length) {
470 mutt_error (_("You are on the last mailbox."));
473 CurBuffy += known_lines;
474 if (CurBuffy >= Incoming->length)
475 CurBuffy = Incoming->length - 1;