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 = safe_strlen (box);
70 if (!SidebarBoundary || !*SidebarBoundary)
71 return (safe_strdup (box));
73 for (i = 0; i < len; ++i) {
74 if (strchr (SidebarBoundary, box[i])) {
82 new_box = safe_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 + safe_strlen (last_dot) > maxlen) {
89 new_box[j++] = box[i + 1];
92 strcat (&new_box[j], last_dot);
99 return safe_strdup (box);
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 && safe_strcmp (Context->path, b->path) == 0;
113 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
114 snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
118 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
119 snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
120 } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
125 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
126 snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
127 } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
133 mutt_FormatString (dest, destlen, ifstr, sidebar_number_format,
134 data, M_FORMAT_OPTIONAL);
135 else if (flags & M_FORMAT_OPTIONAL)
136 mutt_FormatString (dest, destlen, elstr, sidebar_number_format,
137 data, M_FORMAT_OPTIONAL);
141 int sidebar_need_count (void) {
142 if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
143 !SidebarNumberFormat || !*SidebarNumberFormat)
150 * 0 item was not printed ('cause of $sidebar_newmail_only)
153 int make_sidebar_entry (char* box, int idx, size_t len)
155 int shortened = 0, lencnt = 0;
156 char no[SHORT_STRING], entry[SHORT_STRING];
158 int l = safe_strlen (ImapHomeNamespace);
161 if (SidebarWidth > COLS)
164 if (option (OPTSIDEBARNEWMAILONLY) && box && Context && Context->path &&
165 safe_strcmp (Context->path, box) != 0 &&
166 ((BUFFY*) Incoming->data[idx])->new == 0)
167 /* if $sidebar_newmail_only is set, don't display the
168 * box only if it's not the currently opened
169 * (i.e. always display the currently opened) */
172 mutt_FormatString (no, len, NONULL (SidebarNumberFormat),
173 sidebar_number_format, idx, M_FORMAT_OPTIONAL);
174 lencnt = safe_strlen (no);
175 memset (&entry, ' ', sizeof (entry));
178 if (l > 0 && safe_strncmp (box, ImapHomeNamespace, l) == 0 &&
179 safe_strlen (box) > l)
183 box = basename (box);
185 if (option (OPTSHORTENHIERARCHY) && safe_strlen (box) > len-lencnt-1) {
186 box = shortened_hierarchy (box, len-lencnt-1);
190 snprintf (entry, len-lencnt, "%s", box);
191 entry[safe_strlen (entry)] = ' ';
192 strncpy (entry + (len - lencnt), no, lencnt);
194 addnstr (entry, len);
202 /* returns folder name of currently
203 * selected folder for <sidebar-open>
205 const char* sidebar_get_current (void) {
206 if (list_empty(Incoming))
208 return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
211 /* internally sets item to buf */
212 void sidebar_set_current (const char* buf) {
213 int i = buffy_lookup (buf);
218 /* fix counters for a context
219 * FIXME since ctx must not be of our business, move it elsewhere
221 void sidebar_set_buffystats (CONTEXT* Context) {
224 if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
226 tmp = (BUFFY*) Incoming->data[i];
227 tmp->new = Context->new;
228 tmp->msg_unread = Context->unread;
229 tmp->msgcount = Context->msgcount;
230 tmp->msg_flagged = Context->flagged;
233 /* actually draws something
234 * FIXME this needs some clue when to do it
236 int sidebar_draw (int menu)
239 int lines = option (OPTHELP) ? 1 : 0, draw_devider = 1, i = 0;
241 short delim_len = safe_strlen (SidebarDelim);
242 char blank[SHORT_STRING];
244 /* initialize first time */
246 prev_show_value = option (OPTMBOXPANE);
247 saveSidebarWidth = SidebarWidth;
248 if (!option (OPTMBOXPANE)){
255 /* save or restore the value SidebarWidth */
256 if (prev_show_value != option (OPTMBOXPANE)) {
257 if (prev_show_value && !option (OPTMBOXPANE)) {
258 saveSidebarWidth = SidebarWidth;
261 else if (!prev_show_value && option (OPTMBOXPANE)) {
262 SidebarWidth = saveSidebarWidth;
263 /* after toggle: force recounting of all mail */
266 prev_show_value = option (OPTMBOXPANE);
269 if (SidebarWidth > 0 && option (OPTMBOXPANE)
270 && safe_strlen (SidebarDelim) >= SidebarWidth) {
271 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
273 unset_option (OPTMBOXPANE);
277 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
279 /* draw devider only if necessary (if the sidebar becomes visible e.g.)*/
280 if (draw_devider == 1){
281 /* draw the divider */
282 SETCOLOR (MT_COLOR_SIDEBAR);
284 lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
286 move (lines, SidebarWidth - delim_len);
287 if (option (OPTASCIICHARS))
288 addstr (NONULL (SidebarDelim));
289 else if (!option (OPTASCIICHARS) && !safe_strcmp (SidebarDelim, "|"))
291 else if ((Charset_is_utf8) && !safe_strcmp (SidebarDelim, "|"))
292 addstr ("\342\224\202");
294 addstr (NONULL (SidebarDelim));
297 SETCOLOR (MT_COLOR_NORMAL);
299 if (list_empty(Incoming))
301 lines = option (OPTHELP) ? 1 : 0; /* go back to the top */
302 calc_boundaries (menu);
304 /* actually print items */
305 for (i = TopBuffy; i < Incoming->length && lines < LINES - 1 -
306 (menu != MENU_PAGER || option (OPTSTATUSONTOP)); i++) {
307 tmp = (BUFFY*) Incoming->data[i];
310 SETCOLOR (MT_COLOR_INDICATOR);
311 else if (tmp->msg_flagged > 0)
312 SETCOLOR (MT_COLOR_FLAGGED);
313 else if (tmp->new > 0)
314 SETCOLOR (MT_COLOR_NEW);
316 SETCOLOR (MT_COLOR_NORMAL);
319 lines += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
322 /* fill with blanks to bottom */
323 memset (&blank, ' ', sizeof (blank));
324 SETCOLOR (MT_COLOR_NORMAL);
325 for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++) {
327 addnstr (blank, SidebarWidth-delim_len);
332 /* returns index of new item with new mail or -1 */
333 static int exist_next_new () {
335 if (list_empty(Incoming))
338 while (i < Incoming->length)
339 if (((BUFFY*) Incoming->data[i++])->new > 0)
344 /* returns index of prev item with new mail or -1 */
345 static int exist_prev_new () {
347 if (list_empty(Incoming))
351 if (((BUFFY*) Incoming->data[i--])->new > 0)
356 void sidebar_scroll (int op, int menu) {
359 if (!SidebarWidth || list_empty(Incoming))
363 case OP_SIDEBAR_NEXT:
364 if (!option (OPTSIDEBARNEWMAILONLY)) {
365 if (CurBuffy + 1 == Incoming->length) {
366 mutt_error (_("You are on the last mailbox."));
371 } /* the fall-through is intentional */
372 case OP_SIDEBAR_NEXT_NEW:
373 if ((i = exist_next_new ()) < 0) {
374 mutt_error (_("No next mailboxes with new mail."));
380 case OP_SIDEBAR_PREV:
381 if (!option (OPTSIDEBARNEWMAILONLY)) {
383 mutt_error (_("You are on the first mailbox."));
388 } /* the fall-through is intentional */
389 case OP_SIDEBAR_PREV_NEW:
390 if ((i = exist_prev_new ()) < 0) {
391 mutt_error (_("No previous mailbox with new mail."));
398 case OP_SIDEBAR_SCROLL_UP:
400 mutt_error (_("You are on the first mailbox."));
403 CurBuffy -= known_lines;
407 case OP_SIDEBAR_SCROLL_DOWN:
408 if (CurBuffy + 1 == Incoming->length) {
409 mutt_error (_("You are on the last mailbox."));
412 CurBuffy += known_lines;
413 if (CurBuffy >= Incoming->length)
414 CurBuffy = Incoming->length - 1;
419 calc_boundaries (menu);