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-ui/lib-ui.h>
18 #include <lib-ui/menu.h>
19 #include <lib-ui/sidebar.h>
26 static int TopBuffy = 0;
27 static int CurBuffy = 0;
28 static int known_lines = 0;
29 static short initialized = 0;
30 static short prev_show_value;
32 /* computes first entry to be shown */
33 static void calc_boundaries (void) {
36 if (CurBuffy < 0 || CurBuffy >= Incoming.len)
38 if (TopBuffy < 0 || TopBuffy >= Incoming.len)
41 if (option (OPTSIDEBARNEWMAILONLY)) {
43 TopBuffy = CurBuffy - 1;
45 if (Incoming.arr[i]->new > 0)
49 } else if (known_lines>0)
50 TopBuffy = CurBuffy - (CurBuffy % known_lines);
55 static char *shortened_hierarchy (char *hbox, int maxlen)
58 char *last_dot = NULL;
59 int i, j, len = m_strlen(hbox);
62 if (!SidebarBoundary || !*SidebarBoundary)
63 return (m_strdup(hbox));
65 for (i = 0; i < len; ++i) {
66 if (strchr (SidebarBoundary, hbox[i])) {
74 new_box = p_new(char, maxlen + 1);
76 for (i = 1, j = 1; j < maxlen && i < len; ++i) {
77 if (strchr (SidebarBoundary, hbox[i])) {
78 new_box[j++] = hbox[i];
80 if (&hbox[i + 1] != last_dot || j + m_strlen(last_dot) > maxlen) {
81 new_box[j++] = hbox[i + 1];
84 m_strcat(&new_box[j], maxlen + 1, last_dot);
91 return m_strdup(hbox);
95 sidebar_number_format(char* dest, ssize_t destlen,
96 char op, const char* src, const char* fmt,
97 const char* ifstr, const char* elstr,
98 anytype data, format_flag flags)
101 BUFFY* b = Incoming.arr[data.i];
102 int opt = flags & M_FORMAT_OPTIONAL;
103 int c = Context && !m_strcmp(Context->path, b->path);
109 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
110 snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
111 } else if ((c && Context->deleted == 0) || !c)
116 case 'f': /* for compatibility */
118 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
119 snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
120 } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
124 case 'c': /* for compatibility */
127 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
128 snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
129 } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
132 /* total shown, i.e. not hidden by limit */
135 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
136 snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
137 } else if ((c && Context->vcount == 0) || !c)
143 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
144 snprintf (dest, destlen, tmp, c ? Context->new : b->new);
145 } else if ((c && Context->new == 0) || (!c && b->new == 0))
151 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
152 snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
153 } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
159 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
160 snprintf (dest, destlen, tmp, c ? Context->tagged : 0);
161 } else if ((c && Context->tagged == 0) || !c)
166 if (flags & M_FORMAT_OPTIONAL)
167 m_strformat(dest, destlen, 0, opt ? ifstr : elstr,
168 sidebar_number_format, data, flags);
172 int sidebar_need_count (void) {
173 if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
174 !SidebarNumberFormat || !*SidebarNumberFormat)
181 * 0 item was not printed ('cause of $sidebar_newmail_only)
184 static int make_sidebar_entry (char* sbox, int idx, ssize_t len)
186 int shortened = 0, lencnt = 0;
187 char no[STRING], entry[STRING];
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, SidebarWidth, SidebarNumberFormat, sidebar_number_format, idx, 0);
201 lencnt = m_strlen(no);
203 if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 &&
204 m_strlen(sbox) > l_m) {
206 if (Maildir[strlen(Maildir)-1]!='/') {
210 sbox = basename (sbox);
212 if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len-lencnt-1) {
213 sbox = shortened_hierarchy (sbox, len-lencnt-1);
217 snprintf(entry, sizeof(entry), "%*s", (int)len, no);
218 memcpy(entry, sbox, MIN(len - 1, m_strlen(sbox)));
219 waddnstr(stdscr, entry, len);
227 /* returns folder name of currently
228 * selected folder for <sidebar-open>
230 const char* sidebar_get_current (void) {
233 return Incoming.arr[CurBuffy]->path;
236 /* internally sets item to buf */
237 void sidebar_set_current (const char* buf) {
238 int i = buffy_lookup (buf);
245 /* fix counters for a context
246 * FIXME since ctx must not be of our business, move it elsewhere
248 void sidebar_set_buffystats (CONTEXT* curContext) {
251 if (!curContext || !Incoming.len || (i = buffy_lookup (curContext->path)) < 0)
253 tmp = Incoming.arr[i];
254 tmp->new = curContext->new;
255 tmp->msg_unread = curContext->unread;
256 tmp->msgcount = curContext->msgcount;
257 tmp->msg_flagged = curContext->flagged;
260 void sidebar_draw_frames (void) {
263 if (!option(OPTMBOXPANE) || SidebarWidth==0)
266 delim_len=m_strlen(NONULL(SidebarDelim));
268 /* draw vertical delimiter */
269 SETCOLOR (MT_COLOR_SIDEBAR);
270 for (i = 0; i < LINES-1; i++) {
271 wmove (stdscr, i, SidebarWidth - delim_len);
272 if (!m_strcmp(SidebarDelim, "|"))
273 waddch (stdscr, ACS_VLINE);
275 waddstr (stdscr, NONULL (SidebarDelim));
278 /* fill "gaps" at top+bottom */
279 SETCOLOR(MT_COLOR_STATUS);
280 for (i=0; i<SidebarWidth; i++) {
282 * if we don't have $status_on_top and have $help, fill top
283 * gap with spaces to get bg color
285 if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
290 * if we don't have $status_on_top or we have $help, fill bottom
291 * gap with spaces to get bg color
293 if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
294 wmove(stdscr, LINES-2,i);
298 SETCOLOR (MT_COLOR_NORMAL);
301 /* actually draws something
302 * FIXME this needs some clue when to do it
304 int sidebar_draw (void) {
305 int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
306 last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
309 ssize_t delim_len = m_strlen(SidebarDelim);
312 known_lines=last_line-first_line;
314 /* initialize first time */
316 prev_show_value = option (OPTMBOXPANE);
320 if (TopBuffy==0 || CurBuffy==0)
323 /* save or restore the value SidebarWidth */
324 if (prev_show_value != option (OPTMBOXPANE)) {
325 if (!prev_show_value && option (OPTMBOXPANE)) {
326 /* after toggle: force recounting of all mail */
329 prev_show_value = option (OPTMBOXPANE);
332 if (SidebarWidth > 0 && option (OPTMBOXPANE)
333 && m_strlen(SidebarDelim) >= SidebarWidth) {
334 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
336 unset_option (OPTMBOXPANE);
340 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
343 sidebar_draw_frames();
348 /* actually print items */
349 for (i = TopBuffy, line=first_line; i < Incoming.len && line < last_line; i++) {
350 tmp = Incoming.arr[i];
353 SETCOLOR (MT_COLOR_INDICATOR);
354 else if (tmp->new > 0)
355 SETCOLOR (MT_COLOR_NEW);
356 else if (tmp->msg_flagged > 0)
357 SETCOLOR (MT_COLOR_FLAGGED);
359 SETCOLOR (MT_COLOR_NORMAL);
361 wmove (stdscr, line, 0);
362 line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
365 SETCOLOR (MT_COLOR_NORMAL);
367 /* fill with blanks to bottom */
368 memset(&blank, ' ', sizeof(blank));
369 for (; line < last_line; line++) {
370 wmove (stdscr, line, 0);
371 waddnstr (stdscr, blank, SidebarWidth-delim_len);
376 /* returns index of new item with new mail or -1 */
377 static int exist_next_new () {
382 while (i < Incoming.len)
383 if (Incoming.arr[i++]->new > 0)
388 /* returns index of prev item with new mail or -1 */
389 static int exist_prev_new () {
395 if (Incoming.arr[i--]->new > 0)
400 void sidebar_scroll (int op) {
403 if (!SidebarWidth || !Incoming.len)
407 case OP_SIDEBAR_NEXT:
408 if (!option (OPTSIDEBARNEWMAILONLY)) {
409 if (CurBuffy + 1 == Incoming.len) {
410 mutt_error (_("You are on the last mailbox."));
415 } /* the fall-through is intentional */
416 case OP_SIDEBAR_NEXT_NEW:
417 if ((i = exist_next_new ()) < 0) {
418 mutt_error (_("No next mailboxes with new mail."));
424 case OP_SIDEBAR_PREV:
425 if (!option (OPTSIDEBARNEWMAILONLY)) {
427 mutt_error (_("You are on the first mailbox."));
432 } /* the fall-through is intentional */
433 case OP_SIDEBAR_PREV_NEW:
434 if ((i = exist_prev_new ()) < 0) {
435 mutt_error (_("No previous mailbox with new mail."));
442 case OP_SIDEBAR_SCROLL_UP:
444 mutt_error (_("You are on the first mailbox."));
447 CurBuffy -= known_lines;
451 case OP_SIDEBAR_SCROLL_DOWN:
452 if (CurBuffy + 1 == Incoming.len) {
453 mutt_error (_("You are on the last mailbox."));
456 CurBuffy += known_lines;
457 if (CurBuffy >= Incoming.len)
458 CurBuffy = Incoming.len - 1;