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 = mutt_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 + mutt_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 && str_eq (Context->path, b->path);
113 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
114 snprintf (dest, destlen, tmp, c ? (Context->msgcount - Context->deleted) : 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 = mutt_strlen (ImapHomeNamespace);
161 if (SidebarWidth > COLS)
164 if (option (OPTSIDEBARNEWMAILONLY) && box && Context && Context->path &&
165 !str_eq (Context->path, box) && ((BUFFY*) Incoming->data[idx])->new == 0)
166 /* if $sidebar_newmail_only is set, don't display the
167 * box only if it's not the currently opened
168 * (i.e. always display the currently opened) */
171 mutt_FormatString (no, len, NONULL (SidebarNumberFormat),
172 sidebar_number_format, idx, M_FORMAT_OPTIONAL);
173 lencnt = mutt_strlen (no);
174 memset (&entry, ' ', sizeof (entry));
177 if (l > 0 && safe_strncmp (box, ImapHomeNamespace, l) == 0 &&
178 mutt_strlen (box) > l)
182 box = basename (box);
184 if (option (OPTSHORTENHIERARCHY) && mutt_strlen (box) > len-lencnt-1) {
185 box = shortened_hierarchy (box, len-lencnt-1);
189 snprintf (entry, len-lencnt, "%s", box);
190 entry[mutt_strlen (entry)] = ' ';
191 strncpy (entry + (len - lencnt), no, lencnt);
193 addnstr (entry, len);
201 /* returns folder name of currently
202 * selected folder for <sidebar-open>
204 const char* sidebar_get_current (void) {
205 if (list_empty(Incoming))
207 return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
210 /* internally sets item to buf */
211 void sidebar_set_current (const char* buf) {
212 int i = buffy_lookup (buf);
217 /* fix counters for a context
218 * FIXME since ctx must not be of our business, move it elsewhere
220 void sidebar_set_buffystats (CONTEXT* Context) {
223 if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
225 tmp = (BUFFY*) Incoming->data[i];
226 tmp->new = Context->new;
227 tmp->msg_unread = Context->unread;
228 tmp->msgcount = Context->msgcount;
229 tmp->msg_flagged = Context->flagged;
232 /* actually draws something
233 * FIXME this needs some clue when to do it
235 int sidebar_draw (int menu)
238 int lines = option (OPTHELP) ? 1 : 0, draw_devider = 1, i = 0;
240 short delim_len = mutt_strlen (SidebarDelim);
241 char blank[SHORT_STRING];
243 /* initialize first time */
245 prev_show_value = option (OPTMBOXPANE);
246 saveSidebarWidth = SidebarWidth;
247 if (!option (OPTMBOXPANE)){
254 /* save or restore the value SidebarWidth */
255 if (prev_show_value != option (OPTMBOXPANE)) {
256 if (prev_show_value && !option (OPTMBOXPANE)) {
257 saveSidebarWidth = SidebarWidth;
260 else if (!prev_show_value && option (OPTMBOXPANE)) {
261 SidebarWidth = saveSidebarWidth;
262 /* after toggle: force recounting of all mail */
265 prev_show_value = option (OPTMBOXPANE);
268 if (SidebarWidth > 0 && option (OPTMBOXPANE)
269 && mutt_strlen (SidebarDelim) >= SidebarWidth) {
270 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
272 unset_option (OPTMBOXPANE);
276 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
278 /* draw devider only if necessary (if the sidebar becomes visible e.g.)*/
279 if (draw_devider == 1){
280 /* draw the divider */
281 SETCOLOR (MT_COLOR_SIDEBAR);
283 lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
285 move (lines, SidebarWidth - delim_len);
286 if (option (OPTASCIICHARS))
287 addstr (NONULL (SidebarDelim));
288 else if (!option (OPTASCIICHARS) && !mutt_strcmp (SidebarDelim, "|"))
290 else if ((Charset_is_utf8) && !mutt_strcmp (SidebarDelim, "|"))
291 addstr ("\342\224\202");
293 addstr (NONULL (SidebarDelim));
296 SETCOLOR (MT_COLOR_NORMAL);
298 if (list_empty(Incoming))
300 lines = option (OPTHELP) ? 1 : 0; /* go back to the top */
301 calc_boundaries (menu);
303 /* actually print items */
304 for (i = TopBuffy; i < Incoming->length && lines < LINES - 1 -
305 (menu != MENU_PAGER || option (OPTSTATUSONTOP)); i++) {
306 tmp = (BUFFY*) Incoming->data[i];
309 SETCOLOR (MT_COLOR_INDICATOR);
310 else if (tmp->new > 0)
311 SETCOLOR (MT_COLOR_NEW);
312 else if (tmp->msg_flagged > 0)
313 SETCOLOR (MT_COLOR_FLAGGED);
315 SETCOLOR (MT_COLOR_NORMAL);
318 lines += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
321 /* fill with blanks to bottom */
322 memset (&blank, ' ', sizeof (blank));
323 SETCOLOR (MT_COLOR_NORMAL);
324 for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++) {
326 addnstr (blank, SidebarWidth-delim_len);
331 /* returns index of new item with new mail or -1 */
332 static int exist_next_new () {
334 if (list_empty(Incoming))
337 while (i < Incoming->length)
338 if (((BUFFY*) Incoming->data[i++])->new > 0)
343 /* returns index of prev item with new mail or -1 */
344 static int exist_prev_new () {
346 if (list_empty(Incoming))
350 if (((BUFFY*) Incoming->data[i--])->new > 0)
355 void sidebar_scroll (int op, int menu) {
358 if (!SidebarWidth || list_empty(Incoming))
362 case OP_SIDEBAR_NEXT:
363 if (!option (OPTSIDEBARNEWMAILONLY)) {
364 if (CurBuffy + 1 == Incoming->length) {
365 mutt_error (_("You are on the last mailbox."));
370 } /* the fall-through is intentional */
371 case OP_SIDEBAR_NEXT_NEW:
372 if ((i = exist_next_new ()) < 0) {
373 mutt_error (_("No next mailboxes with new mail."));
379 case OP_SIDEBAR_PREV:
380 if (!option (OPTSIDEBARNEWMAILONLY)) {
382 mutt_error (_("You are on the first mailbox."));
387 } /* the fall-through is intentional */
388 case OP_SIDEBAR_PREV_NEW:
389 if ((i = exist_prev_new ()) < 0) {
390 mutt_error (_("No previous mailbox with new mail."));
397 case OP_SIDEBAR_SCROLL_UP:
399 mutt_error (_("You are on the first mailbox."));
402 CurBuffy -= known_lines;
406 case OP_SIDEBAR_SCROLL_DOWN:
407 if (CurBuffy + 1 == Incoming->length) {
408 mutt_error (_("You are on the last mailbox."));
411 CurBuffy += known_lines;
412 if (CurBuffy >= Incoming->length)
413 CurBuffy = Incoming->length - 1;
418 calc_boundaries (menu);