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 &&
223 box = basename (box);
225 if (option (OPTSHORTENHIERARCHY) && str_len (box) > len-lencnt-1) {
226 box = shortened_hierarchy (box, len-lencnt-1);
230 snprintf (entry, len-lencnt, "%s", box);
231 entry[str_len (entry)] = ' ';
232 strncpy (entry + (len - lencnt), no, lencnt);
234 addnstr (entry, len);
242 /* returns folder name of currently
243 * selected folder for <sidebar-open>
245 const char* sidebar_get_current (void) {
246 if (list_empty(Incoming))
248 return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
251 /* internally sets item to buf */
252 void sidebar_set_current (const char* buf) {
253 int i = buffy_lookup (buf);
258 /* fix counters for a context
259 * FIXME since ctx must not be of our business, move it elsewhere
261 void sidebar_set_buffystats (CONTEXT* Context) {
264 if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
266 tmp = (BUFFY*) Incoming->data[i];
267 tmp->new = Context->new;
268 tmp->msg_unread = Context->unread;
269 tmp->msgcount = Context->msgcount;
270 tmp->msg_flagged = Context->flagged;
273 /* actually draws something
274 * FIXME this needs some clue when to do it
276 int sidebar_draw (int menu)
279 int lines = option (OPTHELP) ? 1 : 0, draw_devider = 1, i = 0;
281 short delim_len = str_len (SidebarDelim);
282 char blank[SHORT_STRING];
284 /* initialize first time */
286 prev_show_value = option (OPTMBOXPANE);
287 saveSidebarWidth = SidebarWidth;
288 if (!option (OPTMBOXPANE)){
295 /* save or restore the value SidebarWidth */
296 if (prev_show_value != option (OPTMBOXPANE)) {
297 if (prev_show_value && !option (OPTMBOXPANE)) {
298 saveSidebarWidth = SidebarWidth;
301 else if (!prev_show_value && option (OPTMBOXPANE)) {
302 SidebarWidth = saveSidebarWidth;
303 /* after toggle: force recounting of all mail */
306 prev_show_value = option (OPTMBOXPANE);
309 if (SidebarWidth > 0 && option (OPTMBOXPANE)
310 && str_len (SidebarDelim) >= SidebarWidth) {
311 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
313 unset_option (OPTMBOXPANE);
317 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
319 /* draw devider only if necessary (if the sidebar becomes visible e.g.)*/
320 if (draw_devider == 1){
321 /* draw the divider */
322 SETCOLOR (MT_COLOR_SIDEBAR);
324 lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
326 move (lines, SidebarWidth - delim_len);
327 if (option (OPTASCIICHARS))
328 addstr (NONULL (SidebarDelim));
329 else if (!option (OPTASCIICHARS) && !str_cmp (SidebarDelim, "|"))
331 else if ((Charset_is_utf8) && !str_cmp (SidebarDelim, "|"))
332 addstr ("\342\224\202");
334 addstr (NONULL (SidebarDelim));
337 SETCOLOR (MT_COLOR_NORMAL);
339 if (list_empty(Incoming))
341 lines = option (OPTHELP) ? 1 : 0; /* go back to the top */
342 calc_boundaries (menu);
344 /* actually print items */
345 for (i = TopBuffy; i < Incoming->length && lines < LINES - 1 -
346 (menu != MENU_PAGER || option (OPTSTATUSONTOP)); i++) {
347 tmp = (BUFFY*) Incoming->data[i];
350 SETCOLOR (MT_COLOR_INDICATOR);
351 else if (tmp->new > 0)
352 SETCOLOR (MT_COLOR_NEW);
353 else if (tmp->msg_flagged > 0)
354 SETCOLOR (MT_COLOR_FLAGGED);
356 SETCOLOR (MT_COLOR_NORMAL);
359 lines += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
362 /* fill with blanks to bottom */
363 memset (&blank, ' ', sizeof (blank));
364 SETCOLOR (MT_COLOR_NORMAL);
365 for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++) {
367 addnstr (blank, SidebarWidth-delim_len);
372 /* returns index of new item with new mail or -1 */
373 static int exist_next_new () {
375 if (list_empty(Incoming))
378 while (i < Incoming->length)
379 if (((BUFFY*) Incoming->data[i++])->new > 0)
384 /* returns index of prev item with new mail or -1 */
385 static int exist_prev_new () {
387 if (list_empty(Incoming))
391 if (((BUFFY*) Incoming->data[i--])->new > 0)
396 void sidebar_scroll (int op, int menu) {
399 if (!SidebarWidth || list_empty(Incoming))
403 case OP_SIDEBAR_NEXT:
404 if (!option (OPTSIDEBARNEWMAILONLY)) {
405 if (CurBuffy + 1 == Incoming->length) {
406 mutt_error (_("You are on the last mailbox."));
411 } /* the fall-through is intentional */
412 case OP_SIDEBAR_NEXT_NEW:
413 if ((i = exist_next_new ()) < 0) {
414 mutt_error (_("No next mailboxes with new mail."));
420 case OP_SIDEBAR_PREV:
421 if (!option (OPTSIDEBARNEWMAILONLY)) {
423 mutt_error (_("You are on the first mailbox."));
428 } /* the fall-through is intentional */
429 case OP_SIDEBAR_PREV_NEW:
430 if ((i = exist_prev_new ()) < 0) {
431 mutt_error (_("No previous mailbox with new mail."));
438 case OP_SIDEBAR_SCROLL_UP:
440 mutt_error (_("You are on the first mailbox."));
443 CurBuffy -= known_lines;
447 case OP_SIDEBAR_SCROLL_DOWN:
448 if (CurBuffy + 1 == Incoming->length) {
449 mutt_error (_("You are on the last mailbox."));
452 CurBuffy += known_lines;
453 if (CurBuffy >= Incoming->length)
454 CurBuffy = Incoming->length - 1;
459 calc_boundaries (menu);