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)
46 lines = LINES - 2 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
47 if (option (OPTSIDEBARNEWMAILONLY)) {
49 TopBuffy = CurBuffy - 1;
51 if (((BUFFY*) Incoming->data[i])->new > 0)
56 TopBuffy = CurBuffy - (CurBuffy % lines);
61 static char *shortened_hierarchy (char *box)
64 char *last_dot = NULL;
68 if (!SidebarBoundary || !*SidebarBoundary)
69 return (safe_strdup (box));
71 for (i = 0; i < safe_strlen (box); ++i) {
72 if (strchr (SidebarBoundary, box[i]))
75 for (i = safe_strlen (box); i >= 0; i--)
76 if (strchr (SidebarBoundary, box[i])) {
82 new_box = safe_malloc (safe_strlen (last_dot) + 2 * dots + 1);
84 for (i = 1, j = 1; i < safe_strlen (box); ++i) {
85 if (strchr (SidebarBoundary, box[i])) {
86 new_box[j++] = box[i];
88 if (&box[i + 1] != last_dot) {
89 new_box[j++] = box[i + 1];
93 strcat (&new_box[j], last_dot);
100 return safe_strdup (box);
103 static const char* sidebar_number_format (char* dest, size_t destlen, char op,
104 const char* src, const char* fmt,
105 const char* ifstr, const char* elstr,
106 unsigned long data, format_flag flags) {
107 char tmp[SHORT_STRING];
108 BUFFY* b = (BUFFY*) Incoming->data[data];
109 int opt = flags & M_FORMAT_OPTIONAL;
110 int c = Context && safe_strcmp (Context->path, b->path) == 0;
114 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
115 snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
119 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
120 snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
121 } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
126 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
127 snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
128 } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
134 mutt_FormatString (dest, destlen, ifstr, sidebar_number_format,
135 data, M_FORMAT_OPTIONAL);
136 else if (flags & M_FORMAT_OPTIONAL)
137 mutt_FormatString (dest, destlen, elstr, sidebar_number_format,
138 data, M_FORMAT_OPTIONAL);
142 int sidebar_need_count (void) {
143 if (!option (OPTMBOXPANE) || SidebarWidth == 0 ||
144 !SidebarNumberFormat || !*SidebarNumberFormat)
151 * 0 item was not printed ('cause of $sidebar_newmail_only)
154 int make_sidebar_entry (char* box, int idx, size_t len)
156 int shortened = 0, lencnt = 0;
157 char no[SHORT_STRING], entry[SHORT_STRING];
159 int l = safe_strlen (ImapHomeNamespace);
162 if (SidebarWidth > COLS)
165 if (option (OPTSIDEBARNEWMAILONLY) && box && Context && Context->path &&
166 safe_strcmp (Context->path, box) != 0 &&
167 ((BUFFY*) Incoming->data[idx])->new == 0)
168 /* if $sidebar_newmail_only is set, don't display the
169 * box only if it's not the currently opened
170 * (i.e. always display the currently opened) */
173 mutt_FormatString (no, len, NONULL (SidebarNumberFormat),
174 sidebar_number_format, idx, M_FORMAT_OPTIONAL);
175 lencnt = safe_strlen (no);
176 memset (&entry, ' ', sizeof (entry));
179 if (l > 0 && safe_strncmp (box, ImapHomeNamespace, l) == 0 &&
180 safe_strlen (box) > l)
184 box = basename (box);
186 if (option (OPTSHORTENHIERARCHY) && safe_strlen (box) > len-lencnt-1) {
187 box = shortened_hierarchy (box);
191 snprintf (entry, len-lencnt-1, "%s", box);
192 entry[safe_strlen (entry)] = ' ';
193 strncpy (entry + (len - lencnt), no, lencnt);
195 addnstr (entry, len);
203 /* returns folder name of currently
204 * selected folder for <sidebar-open>
206 const char* sidebar_get_current (void) {
207 if (list_empty(Incoming))
209 return ((char*) ((BUFFY*) Incoming->data[CurBuffy])->path);
212 /* internally sets item to buf */
213 void sidebar_set_current (const char* buf) {
214 int i = buffy_lookup (buf);
219 /* fix counters for a context
220 * FIXME since ctx must not be of our business, move it elsewhere
222 void sidebar_set_buffystats (CONTEXT* Context) {
225 if (!Context || list_empty(Incoming) || (i = buffy_lookup (Context->path)) < 0)
227 tmp = (BUFFY*) Incoming->data[i];
228 tmp->new = Context->new;
229 tmp->msg_unread = Context->unread;
230 tmp->msgcount = Context->msgcount;
231 tmp->msg_flagged = Context->flagged;
234 /* actually draws something
235 * FIXME this needs some clue when to do it
237 int sidebar_draw (int menu)
240 int lines = option (OPTHELP) ? 1 : 0, draw_devider = 1, i = 0;
242 short delim_len = safe_strlen (SidebarDelim);
243 char blank[SHORT_STRING];
245 /* initialize first time */
247 prev_show_value = option (OPTMBOXPANE);
248 saveSidebarWidth = SidebarWidth;
249 if (!option (OPTMBOXPANE)){
256 /* save or restore the value SidebarWidth */
257 if (prev_show_value != option (OPTMBOXPANE)) {
258 if (prev_show_value && !option (OPTMBOXPANE)) {
259 saveSidebarWidth = SidebarWidth;
262 else if (!prev_show_value && option (OPTMBOXPANE)) {
263 SidebarWidth = saveSidebarWidth;
264 /* after toggle: force recounting of all mail */
265 mutt_buffy_check (2);
267 prev_show_value = option (OPTMBOXPANE);
270 if (SidebarWidth > 0 && option (OPTMBOXPANE)
271 && safe_strlen (SidebarDelim) >= SidebarWidth) {
272 mutt_error (_("Value for sidebar_delim is too long. Disabling sidebar."));
274 unset_option (OPTMBOXPANE);
278 if (SidebarWidth == 0 || !option (OPTMBOXPANE))
280 /* draw devider only if necessary (if the sidebar becomes visible e.g.)*/
281 if (draw_devider == 1){
282 /* draw the divider */
283 SETCOLOR (MT_COLOR_SIDEBAR);
285 lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP));
287 move (lines, SidebarWidth - delim_len);
288 if (option (OPTASCIICHARS))
289 addstr (NONULL (SidebarDelim));
290 else if (!option (OPTASCIICHARS) && !safe_strcmp (SidebarDelim, "|"))
292 else if ((Charset_is_utf8) && !safe_strcmp (SidebarDelim, "|"))
293 addstr ("\342\224\202");
295 addstr (NONULL (SidebarDelim));
298 SETCOLOR (MT_COLOR_NORMAL);
300 if (list_empty(Incoming))
302 lines = option (OPTHELP) ? 1 : 0; /* go back to the top */
303 calc_boundaries (menu);
305 /* actually print items */
306 for (i = TopBuffy; i < Incoming->length && lines < LINES - 1 -
307 (menu != MENU_PAGER || option (OPTSTATUSONTOP)); i++) {
308 tmp = (BUFFY*) Incoming->data[i];
311 SETCOLOR (MT_COLOR_INDICATOR);
312 else if (tmp->msg_flagged > 0)
313 SETCOLOR (MT_COLOR_FLAGGED);
314 else if (tmp->new > 0)
315 SETCOLOR (MT_COLOR_NEW);
317 SETCOLOR (MT_COLOR_NORMAL);
320 lines += make_sidebar_entry (tmp->path, i, SidebarWidth-delim_len);
323 /* fill with blanks to bottom */
324 memset (&blank, ' ', sizeof (blank));
325 SETCOLOR (MT_COLOR_NORMAL);
326 for (; lines < LINES - 1 - (menu != MENU_PAGER || option (OPTSTATUSONTOP)); lines++) {
328 addnstr (blank, SidebarWidth-delim_len);
333 /* returns index of new item with new mail or -1 */
334 static int exist_next_new () {
336 if (list_empty(Incoming))
339 while (i < Incoming->length)
340 if (((BUFFY*) Incoming->data[i++])->new > 0)
345 /* returns index of prev item with new mail or -1 */
346 static int exist_prev_new () {
348 if (list_empty(Incoming))
352 if (((BUFFY*) Incoming->data[i--])->new > 0)
357 void sidebar_scroll (int op, int menu) {
360 if (!SidebarWidth || list_empty(Incoming))
364 case OP_SIDEBAR_NEXT:
365 if (!option (OPTSIDEBARNEWMAILONLY)) {
366 if (CurBuffy + 1 == Incoming->length) {
367 mutt_error (_("You are on the last mailbox."));
372 } /* the fall-through is intentional */
373 case OP_SIDEBAR_NEXT_NEW:
374 if ((i = exist_next_new ()) < 0) {
375 mutt_error (_("No next mailboxes with new mail."));
381 case OP_SIDEBAR_PREV:
382 if (!option (OPTSIDEBARNEWMAILONLY)) {
384 mutt_error (_("You are on the first mailbox."));
389 } /* the fall-through is intentional */
390 case OP_SIDEBAR_PREV_NEW:
391 if ((i = exist_prev_new ()) < 0) {
392 mutt_error (_("No previous mailbox with new mail."));
399 case OP_SIDEBAR_SCROLL_UP:
401 mutt_error (_("You are on the first mailbox."));
404 CurBuffy -= known_lines;
408 case OP_SIDEBAR_SCROLL_DOWN:
409 if (TopBuffy + known_lines >= Incoming->length) {
410 mutt_error (_("You are on the last mailbox."));
413 CurBuffy += known_lines;
414 if (CurBuffy >= Incoming->length)
415 CurBuffy = Incoming->length;
420 calc_boundaries (menu);