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.
15 #include <lib-ui/lib-ui.h>
18 #include <lib-ui/menu.h>
19 #include <lib-ui/sidebar.h>
26 static int CurBuffy = 0;
28 /* computes first entry to be shown */
29 static int calc_boundaries(void)
31 if (CurBuffy < 0 || CurBuffy >= Incoming.len)
34 if (option(OPTSIDEBARNEWMAILONLY)) {
37 for (int i = 0; i < CurBuffy; i++) {
38 n += Incoming.arr[i]->new > 0;
45 for (int i = CurBuffy - 1; i >= 0; i--) {
46 if (Incoming.arr[i]->new > 0 && --n <= 0) {
53 return CurBuffy - (CurBuffy % (LINES - 3));
57 static void shortened_hierarchy(char *dst, char *hbox, int maxlen)
59 int dots = 0, last_dot = 0, len = m_strlen(hbox);
61 if (m_strisempty(SidebarBoundary)) {
62 memcpy(dst, hbox, MIN(len, maxlen));
66 for (int i = 0; i < len; ++i) {
67 if (strchr(SidebarBoundary, hbox[i])) {
74 memcpy(dst, hbox, MIN(len, maxlen));
79 for (int i = 1, j = 1; i < len && j < maxlen - 1; ++i) {
80 if (!strchr(SidebarBoundary, hbox[i]))
83 if (len - i <= maxlen - j) {
84 memcpy(dst, hbox + i, len - i);
89 memcpy(dst, hbox + i, maxlen - j);
94 dst[j++] = hbox[i + 1];
99 sidebar_number_format(char* dest, ssize_t destlen,
100 char op, const char* src, const char* fmt,
101 const char* ifstr, const char* elstr,
102 anytype data, format_flag flags)
105 BUFFY* b = Incoming.arr[data.i];
106 int opt = flags & M_FORMAT_OPTIONAL;
107 int c = Context && !m_strcmp(Context->path, b->path);
113 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
114 snprintf (dest, destlen, tmp, c ? Context->deleted : 0);
115 } else if ((c && Context->deleted == 0) || !c)
120 case 'f': /* for compatibility */
122 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
123 snprintf (dest, destlen, tmp, c ? Context->flagged : b->msg_flagged);
124 } else if ((c && Context->flagged == 0) || (!c && b->msg_flagged == 0))
128 case 'c': /* for compatibility */
131 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
132 snprintf (dest, destlen, tmp, c ? Context->msgcount : b->msgcount);
133 } else if ((c && Context->msgcount == 0) || (!c && b->msgcount == 0))
136 /* total shown, i.e. not hidden by limit */
139 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
140 snprintf (dest, destlen, tmp, c ? Context->vcount : 0);
141 } else if ((c && Context->vcount == 0) || !c)
147 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
148 snprintf (dest, destlen, tmp, c ? Context->new : b->new);
149 } else if ((c && Context->new == 0) || (!c && b->new == 0))
155 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
156 snprintf (dest, destlen, tmp, c ? Context->unread : b->msg_unread);
157 } else if ((c && Context->unread == 0) || (!c && b->msg_unread == 0))
163 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
164 snprintf (dest, destlen, tmp, c ? Context->tagged : 0);
165 } else if ((c && Context->tagged == 0) || !c)
170 if (flags & M_FORMAT_OPTIONAL)
171 m_strformat(dest, destlen, 0, opt ? ifstr : elstr,
172 sidebar_number_format, data, flags);
176 int sidebar_need_count(void)
178 return sidebar_w && !m_strisempty(SidebarNumberFormat);
183 * 0 item was not printed ('cause of $sidebar_newmail_only)
186 static int make_sidebar_entry(WINDOW *sw, char *sbox, int idx, ssize_t len)
189 char no[STRING], entry[STRING];
190 int l_m = m_strlen(Maildir);
192 if (option(OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path
193 && m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0
197 m_strformat(no, sizeof(no), len, SidebarNumberFormat,
198 sidebar_number_format, idx, 0);
199 lencnt = m_strlen(no);
201 if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 && m_strlen(sbox) > l_m)
204 if (Maildir[strlen(Maildir) - 1] != '/') {
208 sbox = basename(sbox);
211 snprintf(entry, sizeof(entry), "%*s", (int)len, no);
212 if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len - lencnt - 1) {
213 shortened_hierarchy(entry, sbox, len - lencnt - 1);
215 memcpy(entry, sbox, MIN(len - lencnt - 1, m_strlen(sbox)));
217 waddnstr(sw, entry, len);
222 /* returns folder name of currently
223 * selected folder for <sidebar-open>
225 const char *sidebar_get_current(void)
227 if (0 <= CurBuffy && CurBuffy < Incoming.len)
228 return Incoming.arr[CurBuffy]->path;
232 /* internally sets item to buf */
233 void sidebar_set_current(const char* buf)
235 int i = buffy_lookup(buf);
241 /* fix counters for a context
242 * FIXME since ctx must not be of our business, move it elsewhere
244 void sidebar_set_buffystats(CONTEXT *curContext)
249 if (!curContext || (i = buffy_lookup(curContext->path)) < 0)
251 tmp = Incoming.arr[i];
252 tmp->new = curContext->new;
253 tmp->msg_unread = curContext->unread;
254 tmp->msgcount = curContext->msgcount;
255 tmp->msg_flagged = curContext->flagged;
258 int sidebar_draw(void)
260 static short prev_show_value = -1;
265 /* initialize first time */
266 if (prev_show_value != option(OPTMBOXPANE)) {
267 if ((prev_show_value = option(OPTMBOXPANE))) {
268 /* after toggle: force recounting of all mail */
273 sw = ui_layout_sidebar_w();
278 memset(&blank, ' ', sizeof(blank));
281 SETCOLOR(sw, MT_COLOR_STATUS);
282 waddnstr(sw, blank, x);
285 for (int i = calc_boundaries(); i < Incoming.len && line < y - 1; i++) {
286 BUFFY *tmp = Incoming.arr[i];
289 SETCOLOR(sw, MT_COLOR_INDICATOR);
290 else if (tmp->new > 0)
291 SETCOLOR(sw, MT_COLOR_NEW);
292 else if (tmp->msg_flagged > 0)
293 SETCOLOR(sw, MT_COLOR_FLAGGED);
295 SETCOLOR(sw, MT_COLOR_NORMAL);
297 if (make_sidebar_entry(sw, tmp->path, i, x - 1)) {
298 SETCOLOR(sw, MT_COLOR_SIDEBAR);
299 waddch(sw, ACS_VLINE);
304 while (line < y - 1) {
305 SETCOLOR(sw, MT_COLOR_NORMAL);
306 waddnstr(sw, blank, x - 1);
307 SETCOLOR(sw, MT_COLOR_SIDEBAR);
308 waddch(sw, ACS_VLINE);
312 SETCOLOR(sw, MT_COLOR_STATUS);
313 waddnstr(sw, blank, x);
314 SETCOLOR(sw, MT_COLOR_NORMAL);
318 void sidebar_scroll(int op)
324 case OP_SIDEBAR_NEXT:
325 if (!option(OPTSIDEBARNEWMAILONLY)) {
326 CurBuffy = (CurBuffy + 1) % Incoming.len;
330 case OP_SIDEBAR_NEXT_NEW:
331 for (int i = 1; i < Incoming.len; i++) {
332 if (Incoming.arr[(CurBuffy + i) % Incoming.len]->new > 0) {
333 CurBuffy = (CurBuffy + i) % Incoming.len;
339 case OP_SIDEBAR_PREV:
340 if (!option(OPTSIDEBARNEWMAILONLY)) {
342 CurBuffy = Incoming.len;
348 case OP_SIDEBAR_PREV_NEW:
349 for (int i = Incoming.len - 1; i > 0; i--) {
350 if (Incoming.arr[(CurBuffy + i) % Incoming.len]->new > 0) {
351 CurBuffy = (CurBuffy + i) % Incoming.len;
357 case OP_SIDEBAR_SCROLL_UP:
358 CurBuffy -= LINES - 3;
362 case OP_SIDEBAR_SCROLL_DOWN:
363 CurBuffy += LINES - 3;
364 if (CurBuffy >= Incoming.len)
365 CurBuffy = Incoming.len - 1;