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 if (CurBuffy < 0 || CurBuffy >= Incoming->length)
45 if (TopBuffy < 0 || TopBuffy >= Incoming->length)
47 /* correct known_lines if it has changed because of a window resize */
48 /* if (known_lines != LINES)
49 known_lines = LINES; */
51 lines = LINES - 2 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
53 if (option (OPTSIDEBARNEWMAILONLY)) {
55 TopBuffy = CurBuffy - 1;
57 if (((BUFFY*) Incoming->data[i])->new > 0)
62 TopBuffy = CurBuffy - (CurBuffy % lines);
67 static char *shortened_hierarchy (char *box, int maxlen)
70 char *last_dot = NULL;
71 int i, j, len = str_len (box);
74 if (!SidebarBoundary || !*SidebarBoundary)
75 return (str_dup (box));
77 for (i = 0; i < len; ++i) {
78 if (strchr (SidebarBoundary, box[i])) {
86 new_box = mem_malloc (maxlen + 1);
88 for (i = 1, j = 1; j < maxlen && i < len; ++i) {
89 if (strchr (SidebarBoundary, box[i])) {
90 new_box[j++] = box[i];
92 if (&box[i + 1] != last_dot || j + str_len (last_dot) > maxlen) {
93 new_box[j++] = box[i + 1];
96 strcat (&new_box[j], last_dot);
103 return str_dup (box);
106 static const char* sidebar_number_format (char* dest, size_t destlen, char op,
107 const char* src, const char* fmt,
108 const char* ifstr, const char* elstr,
109 unsigned long data, format_flag flags) {
110 char tmp[SHORT_STRING];
111 BUFFY* b = (BUFFY*) Incoming->data[data];
112 int opt = flags & M_FORMAT_OPTIONAL;
113 int c = Context && str_eq (Context->path, b->path);
119 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
120 snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
121 } else if ((c && Context->deleted == 0) || !c)
126 case 'f': /* for compatibility */
128 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
129 snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
130 } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
134 case 'c': /* for compatibility */
137 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
138 snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
139 } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
142 /* total shown, i.e. not hidden by limit */
145 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
146 snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
147 } else if ((c && Context->vcount == 0) || !c)
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];
194 int l = str_len (ImapHomeNamespace);
196 int l_m = str_len (Maildir);
198 if (SidebarWidth > COLS)
201 if (option (OPTSIDEBARNEWMAILONLY) && box && Context && Context->path &&
202 !str_eq (Context->path, box) && ((BUFFY*) Incoming->data[idx])->new == 0)
203 /* if $sidebar_newmail_only is set, don't display the
204 * box only if it's not the currently opened
205 * (i.e. always display the currently opened) */
208 mutt_FormatString (no, len, NONULL (SidebarNumberFormat),
209 sidebar_number_format, idx, M_FORMAT_OPTIONAL);
210 lencnt = str_len (no);
211 memset (&entry, ' ', sizeof (entry));
214 if (l > 0 && str_ncmp (box, ImapHomeNamespace, l) == 0 &&
216 box += l + 1; /* we're trimming the ImapHomeNamespace, the "+ 1" is for the separator */
219 if (l_m > 0 && str_ncmp (box, Maildir, l_m) == 0 &&
220 str_len (box) > l_m) {
222 if (Maildir[strlen(Maildir)-1]!='/') {
226 box = basename (box);
228 if (option (OPTSHORTENHIERARCHY) && str_len (box) > len-lencnt-1) {
229 box = shortened_hierarchy (box, len-lencnt-1);
233 snprintf (entry, len-lencnt, "%s", box);
234 entry[str_len (entry)] = ' ';
235 strncpy (entry + (len - lencnt), no, lencnt);
237 addnstr (entry, len);
245 /* returns folder name of currently
246 * selected folder for <sidebar-open>
248 const char* sidebar_get_current (void) {
249 if (list_empty(Incoming))
251 return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
254 /* internally sets item to buf */
255 void sidebar_set_current (const char* buf) {
256 int i = buffy_lookup (buf);
261 /* fix counters for a context
262 * FIXME since ctx must not be of our business, move it elsewhere
264 void sidebar_set_buffystats (CONTEXT* Context) {
267 if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
269 tmp = (BUFFY*) Incoming->data[i];
270 tmp->new = Context->new;
271 tmp->msg_unread = Context->unread;
272 tmp->msgcount = Context->msgcount;
273 tmp->msg_flagged = Context->flagged;
276 /* actually draws something
277 * FIXME this needs some clue when to do it
279 int sidebar_draw (int menu)
282 int lines = option (OPTHELP) ? 1 : 0, draw_devider = 1, i = 0;
284 short delim_len = str_len (SidebarDelim);
285 char blank[SHORT_STRING];
287 /* initialize first time */
289 prev_show_value = option (OPTMBOXPANE);
290 saveSidebarWidth = SidebarWidth;
291 if (!option (OPTMBOXPANE)){
298 /* save or restore the value SidebarWidth */
299 if (prev_show_value != option (OPTMBOXPANE)) {
300 if (prev_show_value && !option (OPTMBOXPANE)) {
301 saveSidebarWidth = SidebarWidth;
304 else if (!prev_show_value && option (OPTMBOXPANE)) {
305 SidebarWidth = saveSidebarWidth;
306 /* after toggle: force recounting of all mail */
309 prev_show_value = option (OPTMBOXPANE);
312 if (SidebarWidth > 0 && option (OPTMBOXPANE)
313 && str_len (SidebarDelim) >= SidebarWidth) {
314 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
316 unset_option (OPTMBOXPANE);
320 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
322 /* draw devider only if necessary (if the sidebar becomes visible e.g.)*/
323 if (draw_devider == 1){
324 /* draw the divider */
325 SETCOLOR (MT_COLOR_SIDEBAR);
327 lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
329 move (lines, SidebarWidth - delim_len);
330 if (option (OPTASCIICHARS))
331 addstr (NONULL (SidebarDelim));
332 else if (!option (OPTASCIICHARS) && !str_cmp (SidebarDelim, "|"))
334 else if ((Charset_is_utf8) && !str_cmp (SidebarDelim, "|"))
335 addstr ("\342\224\202");
337 addstr (NONULL (SidebarDelim));
340 SETCOLOR (MT_COLOR_NORMAL);
342 if (list_empty(Incoming))
344 lines = option (OPTHELP) ? 1 : 0; /* go back to the top */
345 calc_boundaries (menu);
347 /* actually print items */
348 for (i = TopBuffy; i < Incoming->length && lines < LINES - 1 -
349 (menu != MENU_PAGER || option (OPTSTATUSONTOP)); i++) {
350 tmp = (BUFFY*) Incoming->data[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);
362 lines += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
365 /* fill with blanks to bottom */
366 memset (&blank, ' ', sizeof (blank));
367 SETCOLOR (MT_COLOR_NORMAL);
368 for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++) {
370 addnstr (blank, SidebarWidth-delim_len);
375 /* returns index of new item with new mail or -1 */
376 static int exist_next_new () {
378 if (list_empty(Incoming))
381 while (i < Incoming->length)
382 if (((BUFFY*) Incoming->data[i++])->new > 0)
387 /* returns index of prev item with new mail or -1 */
388 static int exist_prev_new () {
390 if (list_empty(Incoming))
394 if (((BUFFY*) Incoming->data[i--])->new > 0)
399 void sidebar_scroll (int op, int menu) {
402 if (!SidebarWidth || list_empty(Incoming))
406 case OP_SIDEBAR_NEXT:
407 if (!option (OPTSIDEBARNEWMAILONLY)) {
408 if (CurBuffy + 1 == Incoming->length) {
409 mutt_error (_("You are on the last mailbox."));
414 } /* the fall-through is intentional */
415 case OP_SIDEBAR_NEXT_NEW:
416 if ((i = exist_next_new ()) < 0) {
417 mutt_error (_("No next mailboxes with new mail."));
423 case OP_SIDEBAR_PREV:
424 if (!option (OPTSIDEBARNEWMAILONLY)) {
426 mutt_error (_("You are on the first mailbox."));
431 } /* the fall-through is intentional */
432 case OP_SIDEBAR_PREV_NEW:
433 if ((i = exist_prev_new ()) < 0) {
434 mutt_error (_("No previous mailbox with new mail."));
441 case OP_SIDEBAR_SCROLL_UP:
443 mutt_error (_("You are on the first mailbox."));
446 CurBuffy -= known_lines;
450 case OP_SIDEBAR_SCROLL_DOWN:
451 if (CurBuffy + 1 == Incoming->length) {
452 mutt_error (_("You are on the last mailbox."));
455 CurBuffy += known_lines;
456 if (CurBuffy >= Incoming->length)
457 CurBuffy = Incoming->length - 1;
462 calc_boundaries (menu);