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)));
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 move (i, SidebarWidth - delim_len);
273 if (option (OPTASCIICHARS))
274 addstr (NONULL (SidebarDelim));
275 else if (!option (OPTASCIICHARS) && !m_strcmp(SidebarDelim, "|"))
277 else if ((Charset_is_utf8) && !m_strcmp(SidebarDelim, "|"))
278 addstr ("\342\224\202");
280 addstr (NONULL (SidebarDelim));
283 /* fill "gaps" at top+bottom */
284 SETCOLOR(MT_COLOR_STATUS);
285 for (i=0; i<SidebarWidth; i++) {
287 * if we don't have $status_on_top and have $help, fill top
288 * gap with spaces to get bg color
290 if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
295 * if we don't have $status_on_top or we have $help, fill bottom
296 * gap with spaces to get bg color
298 if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
303 SETCOLOR (MT_COLOR_NORMAL);
306 /* actually draws something
307 * FIXME this needs some clue when to do it
309 int sidebar_draw (void) {
310 int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
311 last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
314 ssize_t delim_len = m_strlen(SidebarDelim);
317 known_lines=last_line-first_line;
319 /* initialize first time */
321 prev_show_value = option (OPTMBOXPANE);
325 if (TopBuffy==0 || CurBuffy==0)
328 /* save or restore the value SidebarWidth */
329 if (prev_show_value != option (OPTMBOXPANE)) {
330 if (!prev_show_value && option (OPTMBOXPANE)) {
331 /* after toggle: force recounting of all mail */
334 prev_show_value = option (OPTMBOXPANE);
337 if (SidebarWidth > 0 && option (OPTMBOXPANE)
338 && m_strlen(SidebarDelim) >= SidebarWidth) {
339 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
341 unset_option (OPTMBOXPANE);
345 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
348 sidebar_draw_frames();
353 /* actually print items */
354 for (i = TopBuffy, line=first_line; i < Incoming.len && line < last_line; i++) {
355 tmp = Incoming.arr[i];
358 SETCOLOR (MT_COLOR_INDICATOR);
359 else if (tmp->new > 0)
360 SETCOLOR (MT_COLOR_NEW);
361 else if (tmp->msg_flagged > 0)
362 SETCOLOR (MT_COLOR_FLAGGED);
364 SETCOLOR (MT_COLOR_NORMAL);
367 line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
370 SETCOLOR (MT_COLOR_NORMAL);
372 /* fill with blanks to bottom */
373 memset(&blank, ' ', sizeof(blank));
374 for (; line < last_line; line++) {
376 addnstr (blank, SidebarWidth-delim_len);
381 /* returns index of new item with new mail or -1 */
382 static int exist_next_new () {
387 while (i < Incoming.len)
388 if (Incoming.arr[i++]->new > 0)
393 /* returns index of prev item with new mail or -1 */
394 static int exist_prev_new () {
400 if (Incoming.arr[i--]->new > 0)
405 void sidebar_scroll (int op) {
408 if (!SidebarWidth || !Incoming.len)
412 case OP_SIDEBAR_NEXT:
413 if (!option (OPTSIDEBARNEWMAILONLY)) {
414 if (CurBuffy + 1 == Incoming.len) {
415 mutt_error (_("You are on the last mailbox."));
420 } /* the fall-through is intentional */
421 case OP_SIDEBAR_NEXT_NEW:
422 if ((i = exist_next_new ()) < 0) {
423 mutt_error (_("No next mailboxes with new mail."));
429 case OP_SIDEBAR_PREV:
430 if (!option (OPTSIDEBARNEWMAILONLY)) {
432 mutt_error (_("You are on the first mailbox."));
437 } /* the fall-through is intentional */
438 case OP_SIDEBAR_PREV_NEW:
439 if ((i = exist_prev_new ()) < 0) {
440 mutt_error (_("No previous mailbox with new mail."));
447 case OP_SIDEBAR_SCROLL_UP:
449 mutt_error (_("You are on the first mailbox."));
452 CurBuffy -= known_lines;
456 case OP_SIDEBAR_SCROLL_DOWN:
457 if (CurBuffy + 1 == Incoming.len) {
458 mutt_error (_("You are on the last mailbox."));
461 CurBuffy += known_lines;
462 if (CurBuffy >= Incoming.len)
463 CurBuffy = Incoming.len - 1;