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/mem.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/macros.h>
20 #include "mutt_menu.h"
21 #include "mutt_curses.h"
30 static int TopBuffy = 0;
31 static int CurBuffy = 0;
32 static int known_lines = 0;
33 static short initialized = 0;
34 static short prev_show_value;
36 /* computes first entry to be shown */
37 static void calc_boundaries (void) {
38 if (list_empty(Incoming))
40 if (CurBuffy < 0 || CurBuffy >= Incoming->length)
42 if (TopBuffy < 0 || TopBuffy >= Incoming->length)
45 if (option (OPTSIDEBARNEWMAILONLY)) {
47 TopBuffy = CurBuffy - 1;
49 if (((BUFFY*) Incoming->data[i])->new > 0)
53 } else if (known_lines>0)
54 TopBuffy = CurBuffy - (CurBuffy % known_lines);
59 static char *shortened_hierarchy (char *box, int maxlen)
62 char *last_dot = NULL;
63 int i, j, len = m_strlen(box);
66 if (!SidebarBoundary || !*SidebarBoundary)
67 return (m_strdup(box));
69 for (i = 0; i < len; ++i) {
70 if (strchr (SidebarBoundary, box[i])) {
78 new_box = p_new(char, maxlen + 1);
80 for (i = 1, j = 1; j < maxlen && i < len; ++i) {
81 if (strchr (SidebarBoundary, box[i])) {
82 new_box[j++] = box[i];
84 if (&box[i + 1] != last_dot || j + m_strlen(last_dot) > maxlen) {
85 new_box[j++] = box[i + 1];
88 strcat (&new_box[j], last_dot);
98 static const char* sidebar_number_format (char* dest, size_t destlen, char op,
99 const char* src, const char* fmt,
100 const char* ifstr, const char* elstr,
101 unsigned long data, format_flag flags) {
102 char tmp[SHORT_STRING];
103 BUFFY* b = (BUFFY*) Incoming->data[data];
104 int opt = flags & M_FORMAT_OPTIONAL;
105 int c = Context && !m_strcmp(Context->path, b->path);
111 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
112 snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
113 } else if ((c && Context->deleted == 0) || !c)
118 case 'f': /* for compatibility */
120 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
121 snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
122 } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
126 case 'c': /* for compatibility */
129 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
130 snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
131 } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
134 /* total shown, i.e. not hidden by limit */
137 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
138 snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
139 } else if ((c && Context->vcount == 0) || !c)
145 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
146 snprintf (dest, destlen, tmp, c ? Context->new : b->new);
147 } else if ((c && Context->new == 0) || (!c && b->new == 0))
153 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
154 snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
155 } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
161 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
162 snprintf (dest, destlen, tmp, c ? Context->tagged : 0);
163 } else if ((c && Context->tagged == 0) || !c)
169 mutt_FormatString (dest, destlen, ifstr, sidebar_number_format,
170 data, M_FORMAT_OPTIONAL);
171 else if (flags & M_FORMAT_OPTIONAL)
172 mutt_FormatString (dest, destlen, elstr, sidebar_number_format,
173 data, M_FORMAT_OPTIONAL);
177 int sidebar_need_count (void) {
178 if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
179 !SidebarNumberFormat || !*SidebarNumberFormat)
186 * 0 item was not printed ('cause of $sidebar_newmail_only)
189 int make_sidebar_entry (char* box, int idx, size_t len)
191 int shortened = 0, lencnt = 0;
192 char no[SHORT_STRING], entry[SHORT_STRING];
193 int l = m_strlen(ImapHomeNamespace);
194 int l_m = m_strlen(Maildir);
196 if (SidebarWidth > COLS)
199 if (option (OPTSIDEBARNEWMAILONLY) && box && Context && Context->path &&
200 m_strcmp(Context->path, box) && ((BUFFY*) Incoming->data[idx])->new == 0)
201 /* if $sidebar_newmail_only is set, don't display the
202 * box only if it's not the currently opened
203 * (i.e. always display the currently opened) */
206 mutt_FormatString (no, len, NONULL (SidebarNumberFormat),
207 sidebar_number_format, idx, M_FORMAT_OPTIONAL);
208 lencnt = m_strlen(no);
209 memset(&entry, ' ', sizeof(entry));
211 if (l > 0 && m_strncmp(box, ImapHomeNamespace, l) == 0 &&
213 box += l + 1; /* we're trimming the ImapHomeNamespace, the "+ 1" is for the separator */
215 if (l_m > 0 && m_strncmp(box, Maildir, l_m) == 0 &&
216 m_strlen(box) > l_m) {
218 if (Maildir[strlen(Maildir)-1]!='/') {
222 box = basename (box);
224 if (option (OPTSHORTENHIERARCHY) && m_strlen(box) > len-lencnt-1) {
225 box = shortened_hierarchy (box, len-lencnt-1);
229 m_strcpy(entry, len - lencnt, box);
230 entry[m_strlen(entry)] = ' ';
231 memcpy(entry + (len - lencnt), no, lencnt);
233 addnstr (entry, len);
241 /* returns folder name of currently
242 * selected folder for <sidebar-open>
244 const char* sidebar_get_current (void) {
245 if (list_empty(Incoming))
247 return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
250 /* internally sets item to buf */
251 void sidebar_set_current (const char* buf) {
252 int i = buffy_lookup (buf);
259 /* fix counters for a context
260 * FIXME since ctx must not be of our business, move it elsewhere
262 void sidebar_set_buffystats (CONTEXT* Context) {
265 if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
267 tmp = (BUFFY*) Incoming->data[i];
268 tmp->new = Context->new;
269 tmp->msg_unread = Context->unread;
270 tmp->msgcount = Context->msgcount;
271 tmp->msg_flagged = Context->flagged;
274 void sidebar_draw_frames (void) {
277 if (!option(OPTMBOXPANE) || SidebarWidth==0)
280 delim_len=m_strlen(NONULL(SidebarDelim));
282 /* draw vertical delimiter */
283 SETCOLOR (MT_COLOR_SIDEBAR);
284 for (i = 0; i < LINES-1; i++) {
285 move (i, SidebarWidth - delim_len);
286 if (option (OPTASCIICHARS))
287 addstr (NONULL (SidebarDelim));
288 else if (!option (OPTASCIICHARS) && !m_strcmp(SidebarDelim, "|"))
290 else if ((Charset_is_utf8) && !m_strcmp(SidebarDelim, "|"))
291 addstr ("\342\224\202");
293 addstr (NONULL (SidebarDelim));
296 /* fill "gaps" at top+bottom */
297 SETCOLOR(MT_COLOR_STATUS);
298 for (i=0; i<SidebarWidth; i++) {
300 * if we don't have $status_on_top and have $help, fill top
301 * gap with spaces to get bg color
303 if (option(OPTSTATUSONTOP) || option(OPTHELP)) {
308 * if we don't have $status_on_top or we have $help, fill bottom
309 * gap with spaces to get bg color
311 if (!option(OPTSTATUSONTOP) || option(OPTHELP)) {
316 SETCOLOR (MT_COLOR_NORMAL);
319 /* actually draws something
320 * FIXME this needs some clue when to do it
322 int sidebar_draw (int menu) {
323 int first_line = option (OPTSTATUSONTOP) ? 1 : option (OPTHELP) ? 1 : 0,
324 last_line = LINES - 2 + (option (OPTSTATUSONTOP) && !option (OPTHELP) ? 1 : 0),
327 size_t delim_len = m_strlen(SidebarDelim);
328 char blank[SHORT_STRING];
330 known_lines=last_line-first_line;
332 /* initialize first time */
334 prev_show_value = option (OPTMBOXPANE);
338 if (TopBuffy==0 || CurBuffy==0)
341 /* save or restore the value SidebarWidth */
342 if (prev_show_value != option (OPTMBOXPANE)) {
343 if (!prev_show_value && option (OPTMBOXPANE)) {
344 /* after toggle: force recounting of all mail */
347 prev_show_value = option (OPTMBOXPANE);
350 if (SidebarWidth > 0 && option (OPTMBOXPANE)
351 && m_strlen(SidebarDelim) >= SidebarWidth) {
352 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
354 unset_option (OPTMBOXPANE);
358 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
361 sidebar_draw_frames();
363 if (list_empty(Incoming))
366 /* actually print items */
367 for (i = TopBuffy, line=first_line; i < Incoming->length && line < last_line; i++) {
368 tmp = (BUFFY*) Incoming->data[i];
371 SETCOLOR (MT_COLOR_INDICATOR);
372 else if (tmp->new > 0)
373 SETCOLOR (MT_COLOR_NEW);
374 else if (tmp->msg_flagged > 0)
375 SETCOLOR (MT_COLOR_FLAGGED);
377 SETCOLOR (MT_COLOR_NORMAL);
380 line += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
383 SETCOLOR (MT_COLOR_NORMAL);
385 /* fill with blanks to bottom */
386 memset(&blank, ' ', sizeof(blank));
387 for (; line < last_line; line++) {
389 addnstr (blank, SidebarWidth-delim_len);
394 /* returns index of new item with new mail or -1 */
395 static int exist_next_new () {
397 if (list_empty(Incoming))
400 while (i < Incoming->length)
401 if (((BUFFY*) Incoming->data[i++])->new > 0)
406 /* returns index of prev item with new mail or -1 */
407 static int exist_prev_new () {
409 if (list_empty(Incoming))
413 if (((BUFFY*) Incoming->data[i--])->new > 0)
418 void sidebar_scroll (int op, int menu) {
421 if (!SidebarWidth || list_empty(Incoming))
425 case OP_SIDEBAR_NEXT:
426 if (!option (OPTSIDEBARNEWMAILONLY)) {
427 if (CurBuffy + 1 == Incoming->length) {
428 mutt_error (_("You are on the last mailbox."));
433 } /* the fall-through is intentional */
434 case OP_SIDEBAR_NEXT_NEW:
435 if ((i = exist_next_new ()) < 0) {
436 mutt_error (_("No next mailboxes with new mail."));
442 case OP_SIDEBAR_PREV:
443 if (!option (OPTSIDEBARNEWMAILONLY)) {
445 mutt_error (_("You are on the first mailbox."));
450 } /* the fall-through is intentional */
451 case OP_SIDEBAR_PREV_NEW:
452 if ((i = exist_prev_new ()) < 0) {
453 mutt_error (_("No previous mailbox with new mail."));
460 case OP_SIDEBAR_SCROLL_UP:
462 mutt_error (_("You are on the first mailbox."));
465 CurBuffy -= known_lines;
469 case OP_SIDEBAR_SCROLL_DOWN:
470 if (CurBuffy + 1 == Incoming->length) {
471 mutt_error (_("You are on the last mailbox."));
474 CurBuffy += known_lines;
475 if (CurBuffy >= Incoming->length)
476 CurBuffy = Incoming->length - 1;