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.
16 #include "mutt_menu.h"
17 #include "mutt_curses.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;
34 static short saveSidebarWidth;
36 /* computes first entry to be shown */
37 void calc_boundaries (int menu)
41 if (list_empty(Incoming))
43 /* correct known_lines if it has changed because of a window resize */
44 /* if (known_lines != LINES)
45 known_lines = LINES; */
47 lines = LINES - 2 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
49 if (option (OPTSIDEBARNEWMAILONLY)) {
51 TopBuffy = CurBuffy - 1;
53 if (((BUFFY*) Incoming->data[i])->new > 0)
58 TopBuffy = CurBuffy - (CurBuffy % lines);
63 static char *shortened_hierarchy (char *box, int maxlen)
66 char *last_dot = NULL;
67 int i, j, len = str_len (box);
70 if (!SidebarBoundary || !*SidebarBoundary)
71 return (str_dup (box));
73 for (i = 0; i < len; ++i) {
74 if (strchr (SidebarBoundary, box[i])) {
82 new_box = mem_malloc (maxlen + 1);
84 for (i = 1, j = 1; j < maxlen && i < len; ++i) {
85 if (strchr (SidebarBoundary, box[i])) {
86 new_box[j++] = box[i];
88 if (&box[i + 1] != last_dot || j + str_len (last_dot) > maxlen) {
89 new_box[j++] = box[i + 1];
92 strcat (&new_box[j], last_dot);
102 static const char* sidebar_number_format (char* dest, size_t destlen, char op,
103 const char* src, const char* fmt,
104 const char* ifstr, const char* elstr,
105 unsigned long data, format_flag flags) {
106 char tmp[SHORT_STRING];
107 BUFFY* b = (BUFFY*) Incoming->data[data];
108 int opt = flags & M_FORMAT_OPTIONAL;
109 int c = Context && str_eq (Context->path, b->path);
115 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
116 snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
117 } else if ((c && Context->deleted == 0) || !c)
122 case 'f': /* for compatibility */
124 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
125 snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
126 } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
130 case 'c': /* for compatibility */
133 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
134 snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
135 } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
138 /* total shown, i.e. not hidden by limit */
141 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
142 snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
143 } else if ((c && Context->vcount == 0) || !c)
149 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
150 snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
151 } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
157 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
158 snprintf (dest, destlen, tmp, c ? Context->tagged : 0);
159 } else if ((c && Context->tagged == 0) || !c)
165 mutt_FormatString (dest, destlen, ifstr, sidebar_number_format,
166 data, M_FORMAT_OPTIONAL);
167 else if (flags & M_FORMAT_OPTIONAL)
168 mutt_FormatString (dest, destlen, elstr, sidebar_number_format,
169 data, M_FORMAT_OPTIONAL);
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 int make_sidebar_entry (char* box, int idx, size_t len)
187 int shortened = 0, lencnt = 0;
188 char no[SHORT_STRING], entry[SHORT_STRING];
190 int l = str_len (ImapHomeNamespace);
192 int l_m = str_len (Maildir);
194 if (SidebarWidth > COLS)
197 if (option (OPTSIDEBARNEWMAILONLY) && box && Context && Context->path &&
198 !str_eq (Context->path, box) && ((BUFFY*) Incoming->data[idx])->new == 0)
199 /* if $sidebar_newmail_only is set, don't display the
200 * box only if it's not the currently opened
201 * (i.e. always display the currently opened) */
204 mutt_FormatString (no, len, NONULL (SidebarNumberFormat),
205 sidebar_number_format, idx, M_FORMAT_OPTIONAL);
206 lencnt = str_len (no);
207 memset (&entry, ' ', sizeof (entry));
210 if (l > 0 && str_ncmp (box, ImapHomeNamespace, l) == 0 &&
215 if (l_m > 0 && str_ncmp (box, Maildir, l_m) == 0 &&
219 box = basename (box);
221 if (option (OPTSHORTENHIERARCHY) && str_len (box) > len-lencnt-1) {
222 box = shortened_hierarchy (box, len-lencnt-1);
226 snprintf (entry, len-lencnt, "%s", box);
227 entry[str_len (entry)] = ' ';
228 strncpy (entry + (len - lencnt), no, lencnt);
230 addnstr (entry, len);
238 /* returns folder name of currently
239 * selected folder for <sidebar-open>
241 const char* sidebar_get_current (void) {
242 if (list_empty(Incoming))
244 return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
247 /* internally sets item to buf */
248 void sidebar_set_current (const char* buf) {
249 int i = buffy_lookup (buf);
254 /* fix counters for a context
255 * FIXME since ctx must not be of our business, move it elsewhere
257 void sidebar_set_buffystats (CONTEXT* Context) {
260 if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
262 tmp = (BUFFY*) Incoming->data[i];
263 tmp->new = Context->new;
264 tmp->msg_unread = Context->unread;
265 tmp->msgcount = Context->msgcount;
266 tmp->msg_flagged = Context->flagged;
269 /* actually draws something
270 * FIXME this needs some clue when to do it
272 int sidebar_draw (int menu)
275 int lines = option (OPTHELP) ? 1 : 0, draw_devider = 1, i = 0;
277 short delim_len = str_len (SidebarDelim);
278 char blank[SHORT_STRING];
280 /* initialize first time */
282 prev_show_value = option (OPTMBOXPANE);
283 saveSidebarWidth = SidebarWidth;
284 if (!option (OPTMBOXPANE)){
291 /* save or restore the value SidebarWidth */
292 if (prev_show_value != option (OPTMBOXPANE)) {
293 if (prev_show_value && !option (OPTMBOXPANE)) {
294 saveSidebarWidth = SidebarWidth;
297 else if (!prev_show_value && option (OPTMBOXPANE)) {
298 SidebarWidth = saveSidebarWidth;
299 /* after toggle: force recounting of all mail */
302 prev_show_value = option (OPTMBOXPANE);
305 if (SidebarWidth > 0 && option (OPTMBOXPANE)
306 && str_len (SidebarDelim) >= SidebarWidth) {
307 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
309 unset_option (OPTMBOXPANE);
313 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
315 /* draw devider only if necessary (if the sidebar becomes visible e.g.)*/
316 if (draw_devider == 1){
317 /* draw the divider */
318 SETCOLOR (MT_COLOR_SIDEBAR);
320 lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
322 move (lines, SidebarWidth - delim_len);
323 if (option (OPTASCIICHARS))
324 addstr (NONULL (SidebarDelim));
325 else if (!option (OPTASCIICHARS) && !str_cmp (SidebarDelim, "|"))
327 else if ((Charset_is_utf8) && !str_cmp (SidebarDelim, "|"))
328 addstr ("\342\224\202");
330 addstr (NONULL (SidebarDelim));
333 SETCOLOR (MT_COLOR_NORMAL);
335 if (list_empty(Incoming))
337 lines = option (OPTHELP) ? 1 : 0; /* go back to the top */
338 calc_boundaries (menu);
340 /* actually print items */
341 for (i = TopBuffy; i < Incoming->length && lines < LINES - 1 -
342 (menu != MENU_PAGER || option (OPTSTATUSONTOP)); i++) {
343 tmp = (BUFFY*) Incoming->data[i];
346 SETCOLOR (MT_COLOR_INDICATOR);
347 else if (tmp->new > 0)
348 SETCOLOR (MT_COLOR_NEW);
349 else if (tmp->msg_flagged > 0)
350 SETCOLOR (MT_COLOR_FLAGGED);
352 SETCOLOR (MT_COLOR_NORMAL);
355 lines += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
358 /* fill with blanks to bottom */
359 memset (&blank, ' ', sizeof (blank));
360 SETCOLOR (MT_COLOR_NORMAL);
361 for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++) {
363 addnstr (blank, SidebarWidth-delim_len);
368 /* returns index of new item with new mail or -1 */
369 static int exist_next_new () {
371 if (list_empty(Incoming))
374 while (i < Incoming->length)
375 if (((BUFFY*) Incoming->data[i++])->new > 0)
380 /* returns index of prev item with new mail or -1 */
381 static int exist_prev_new () {
383 if (list_empty(Incoming))
387 if (((BUFFY*) Incoming->data[i--])->new > 0)
392 void sidebar_scroll (int op, int menu) {
395 if (!SidebarWidth || list_empty(Incoming))
399 case OP_SIDEBAR_NEXT:
400 if (!option (OPTSIDEBARNEWMAILONLY)) {
401 if (CurBuffy + 1 == Incoming->length) {
402 mutt_error (_("You are on the last mailbox."));
407 } /* the fall-through is intentional */
408 case OP_SIDEBAR_NEXT_NEW:
409 if ((i = exist_next_new ()) < 0) {
410 mutt_error (_("No next mailboxes with new mail."));
416 case OP_SIDEBAR_PREV:
417 if (!option (OPTSIDEBARNEWMAILONLY)) {
419 mutt_error (_("You are on the first mailbox."));
424 } /* the fall-through is intentional */
425 case OP_SIDEBAR_PREV_NEW:
426 if ((i = exist_prev_new ()) < 0) {
427 mutt_error (_("No previous mailbox with new mail."));
434 case OP_SIDEBAR_SCROLL_UP:
436 mutt_error (_("You are on the first mailbox."));
439 CurBuffy -= known_lines;
443 case OP_SIDEBAR_SCROLL_DOWN:
444 if (CurBuffy + 1 == Incoming->length) {
445 mutt_error (_("You are on the last mailbox."));
448 CurBuffy += known_lines;
449 if (CurBuffy >= Incoming->length)
450 CurBuffy = Incoming->length - 1;
455 calc_boundaries (menu);