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);
96 sidebar_number_format(char* dest, ssize_t destlen,
97 char op, const char* src, const char* fmt,
98 const char* ifstr, const char* elstr,
99 anytype data, format_flag flags)
102 BUFFY* b = Incoming.arr[data.i];
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)
167 if (flags & M_FORMAT_OPTIONAL)
168 m_strformat(dest, destlen, 0, opt ? ifstr : elstr,
169 sidebar_number_format, data, flags);
173 int sidebar_need_count (void) {
174 if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
175 !SidebarNumberFormat || !*SidebarNumberFormat)
182 * 0 item was not printed ('cause of $sidebar_newmail_only)
185 static int make_sidebar_entry (char* sbox, int idx, ssize_t len)
187 int shortened = 0, lencnt = 0;
188 char no[STRING], entry[STRING];
189 int l_m = m_strlen(Maildir);
191 if (SidebarWidth > COLS)
194 if (option (OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path &&
195 m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0)
196 /* if $sidebar_newmail_only is set, don't display the
197 * box only if it's not the currently opened
198 * (i.e. always display the currently opened) */
201 m_strformat(no, len, SidebarWidth, SidebarNumberFormat, sidebar_number_format, idx, 0);
202 lencnt = m_strlen(no);
204 if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 &&
205 m_strlen(sbox) > l_m) {
207 if (Maildir[strlen(Maildir)-1]!='/') {
211 sbox = basename (sbox);
213 if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len-lencnt-1) {
214 sbox = shortened_hierarchy (sbox, len-lencnt-1);
218 snprintf(entry, sizeof(entry), "%*s", (int)len, no);
219 memcpy(entry, sbox, MIN(len - 1, m_strlen(sbox)));
220 waddnstr(stdscr, entry, len);
228 /* returns folder name of currently
229 * selected folder for <sidebar-open>
231 const char* sidebar_get_current (void) {
234 return Incoming.arr[CurBuffy]->path;
237 /* internally sets item to buf */
238 void sidebar_set_current (const char* buf) {
239 int i = buffy_lookup (buf);
246 /* fix counters for a context
247 * FIXME since ctx must not be of our business, move it elsewhere
249 void sidebar_set_buffystats (CONTEXT* curContext) {
252 if (!curContext || !Incoming.len || (i = buffy_lookup (curContext->path)) < 0)
254 tmp = Incoming.arr[i];
255 tmp->new = curContext->new;
256 tmp->msg_unread = curContext->unread;
257 tmp->msgcount = curContext->msgcount;
258 tmp->msg_flagged = curContext->flagged;
261 void sidebar_draw_frames (void) {
264 if (!option(OPTMBOXPANE) || SidebarWidth==0)
267 delim_len=m_strlen(NONULL(SidebarDelim));
269 /* draw vertical delimiter */
270 SETCOLOR (MT_COLOR_SIDEBAR);
271 for (i = 0; i < LINES-1; i++) {
272 wmove (stdscr, i, SidebarWidth - delim_len);
273 if (!m_strcmp(SidebarDelim, "|"))
274 waddch (stdscr, ACS_VLINE);
276 waddstr (stdscr, NONULL (SidebarDelim));
279 /* fill "gaps" at top+bottom */
280 SETCOLOR(MT_COLOR_STATUS);
281 for (i=0; i<SidebarWidth; i++) {
283 * if we don't have $status_on_top and have $help, fill top
284 * gap with spaces to get bg color
286 if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
291 * if we don't have $status_on_top or we have $help, fill bottom
292 * gap with spaces to get bg color
294 if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
295 wmove(stdscr, LINES-2,i);
299 SETCOLOR (MT_COLOR_NORMAL);
302 /* actually draws something
303 * FIXME this needs some clue when to do it
305 int sidebar_draw (void) {
306 int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
307 last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
310 ssize_t delim_len = m_strlen(SidebarDelim);
313 known_lines=last_line-first_line;
315 /* initialize first time */
317 prev_show_value = option (OPTMBOXPANE);
321 if (TopBuffy==0 || CurBuffy==0)
324 /* save or restore the value SidebarWidth */
325 if (prev_show_value != option (OPTMBOXPANE)) {
326 if (!prev_show_value && option (OPTMBOXPANE)) {
327 /* after toggle: force recounting of all mail */
330 prev_show_value = option (OPTMBOXPANE);
333 if (SidebarWidth > 0 && option (OPTMBOXPANE)
334 && m_strlen(SidebarDelim) >= SidebarWidth) {
335 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
337 unset_option (OPTMBOXPANE);
341 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
344 sidebar_draw_frames();
349 /* actually print items */
350 for (i = TopBuffy, line=first_line; i < Incoming.len && line < last_line; i++) {
351 tmp = Incoming.arr[i];
354 SETCOLOR (MT_COLOR_INDICATOR);
355 else if (tmp->new > 0)
356 SETCOLOR (MT_COLOR_NEW);
357 else if (tmp->msg_flagged > 0)
358 SETCOLOR (MT_COLOR_FLAGGED);
360 SETCOLOR (MT_COLOR_NORMAL);
362 wmove (stdscr, line, 0);
363 line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
366 SETCOLOR (MT_COLOR_NORMAL);
368 /* fill with blanks to bottom */
369 memset(&blank, ' ', sizeof(blank));
370 for (; line < last_line; line++) {
371 wmove (stdscr, line, 0);
372 waddnstr (stdscr, blank, SidebarWidth-delim_len);
377 /* returns index of new item with new mail or -1 */
378 static int exist_next_new () {
383 while (i < Incoming.len)
384 if (Incoming.arr[i++]->new > 0)
389 /* returns index of prev item with new mail or -1 */
390 static int exist_prev_new () {
396 if (Incoming.arr[i--]->new > 0)
401 void sidebar_scroll (int op) {
404 if (!SidebarWidth || !Incoming.len)
408 case OP_SIDEBAR_NEXT:
409 if (!option (OPTSIDEBARNEWMAILONLY)) {
410 if (CurBuffy + 1 == Incoming.len) {
411 mutt_error (_("You are on the last mailbox."));
416 } /* the fall-through is intentional */
417 case OP_SIDEBAR_NEXT_NEW:
418 if ((i = exist_next_new ()) < 0) {
419 mutt_error (_("No next mailboxes with new mail."));
425 case OP_SIDEBAR_PREV:
426 if (!option (OPTSIDEBARNEWMAILONLY)) {
428 mutt_error (_("You are on the first mailbox."));
433 } /* the fall-through is intentional */
434 case OP_SIDEBAR_PREV_NEW:
435 if ((i = exist_prev_new ()) < 0) {
436 mutt_error (_("No previous mailbox with new mail."));
443 case OP_SIDEBAR_SCROLL_UP:
445 mutt_error (_("You are on the first mailbox."));
448 CurBuffy -= known_lines;
452 case OP_SIDEBAR_SCROLL_DOWN:
453 if (CurBuffy + 1 == Incoming.len) {
454 mutt_error (_("You are on the last mailbox."));
457 CurBuffy += known_lines;
458 if (CurBuffy >= Incoming.len)
459 CurBuffy = Incoming.len - 1;