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);
178 * 0 item was not printed ('cause of $sidebar_newmail_only)
181 static int make_sidebar_entry(WINDOW *sw, char *sbox, int idx, ssize_t len)
184 char no[STRING], entry[STRING];
185 int l_m = m_strlen(Maildir);
187 if (option(OPTSIDEBARNEWMAILONLY) && sbox && Context && Context->path
188 && m_strcmp(Context->path, sbox) && Incoming.arr[idx]->new == 0
192 m_strformat(no, sizeof(no), len, SidebarNumberFormat,
193 sidebar_number_format, idx, 0);
194 lencnt = m_strlen(no);
196 if (l_m > 0 && m_strncmp(sbox, Maildir, l_m) == 0 && m_strlen(sbox) > l_m)
199 if (Maildir[strlen(Maildir) - 1] != '/') {
203 sbox = basename(sbox);
206 snprintf(entry, sizeof(entry), "%*s", (int)len, no);
207 if (option(OPTSHORTENHIERARCHY) && m_strlen(sbox) > len - lencnt - 1) {
208 shortened_hierarchy(entry, sbox, len - lencnt - 1);
210 memcpy(entry, sbox, MIN(len - lencnt - 1, m_strlen(sbox)));
212 waddnstr(sw, entry, len);
217 /* returns folder name of currently
218 * selected folder for <sidebar-open>
220 const char *sidebar_get_current(void)
222 if (0 <= CurBuffy && CurBuffy < Incoming.len)
223 return Incoming.arr[CurBuffy]->path;
227 /* internally sets item to buf */
228 void sidebar_set_current(const char* buf)
230 int i = buffy_lookup(buf);
236 /* fix counters for a context
237 * FIXME since ctx must not be of our business, move it elsewhere
239 void sidebar_set_buffystats(CONTEXT *curContext)
244 if (!curContext || (i = buffy_lookup(curContext->path)) < 0)
246 tmp = Incoming.arr[i];
247 tmp->new = curContext->new;
248 tmp->msg_unread = curContext->unread;
249 tmp->msgcount = curContext->msgcount;
250 tmp->msg_flagged = curContext->flagged;
253 int sidebar_draw(void)
255 static short prev_show_value = -1;
259 /* initialize first time */
260 if (prev_show_value != option(OPTMBOXPANE)) {
261 if ((prev_show_value = option(OPTMBOXPANE))) {
262 /* after toggle: force recounting of all mail */
267 sw = ui_layout_sidebar_w();
272 SETCOLOR(sw, MT_COLOR_SIDEBAR);
273 mvwhline(sw, 0, 0, ACS_HLINE, x - 1);
274 mvwaddch(sw, 0, x - 1, ACS_TTEE);
277 for (int i = calc_boundaries(); i < Incoming.len && line < y - 1; i++) {
278 BUFFY *tmp = Incoming.arr[i];
281 SETCOLOR(sw, MT_COLOR_INDICATOR);
282 else if (tmp->new > 0)
283 SETCOLOR(sw, MT_COLOR_NEW);
284 else if (tmp->msg_flagged > 0)
285 SETCOLOR(sw, MT_COLOR_FLAGGED);
287 SETCOLOR(sw, MT_COLOR_NORMAL);
289 if (make_sidebar_entry(sw, tmp->path, i, x - 1)) {
290 SETCOLOR(sw, MT_COLOR_SIDEBAR);
291 waddch(sw, ACS_VLINE);
296 while (line < y - 1) {
297 SETCOLOR(sw, MT_COLOR_NORMAL);
298 whline(sw, ' ', x - 1);
299 SETCOLOR(sw, MT_COLOR_SIDEBAR);
300 mvwaddch(sw, line++, x - 1, ACS_VLINE);
303 SETCOLOR(sw, MT_COLOR_SIDEBAR);
304 mvwhline(sw, y - 1, 0, ACS_HLINE, x - 1);
305 mvwaddch(sw, y - 1, x - 1, ACS_BTEE);
309 void sidebar_scroll(int op)
315 case OP_SIDEBAR_NEXT:
316 if (!option(OPTSIDEBARNEWMAILONLY)) {
317 CurBuffy = (CurBuffy + 1) % Incoming.len;
321 case OP_SIDEBAR_NEXT_NEW:
322 for (int i = 1; i < Incoming.len; i++) {
323 if (Incoming.arr[(CurBuffy + i) % Incoming.len]->new > 0) {
324 CurBuffy = (CurBuffy + i) % Incoming.len;
330 case OP_SIDEBAR_PREV:
331 if (!option(OPTSIDEBARNEWMAILONLY)) {
333 CurBuffy = Incoming.len;
339 case OP_SIDEBAR_PREV_NEW:
340 for (int i = Incoming.len - 1; i > 0; i--) {
341 if (Incoming.arr[(CurBuffy + i) % Incoming.len]->new > 0) {
342 CurBuffy = (CurBuffy + i) % Incoming.len;
348 case OP_SIDEBAR_SCROLL_UP:
349 CurBuffy -= LINES - 3;
353 case OP_SIDEBAR_SCROLL_DOWN:
354 CurBuffy += LINES - 3;
355 if (CurBuffy >= Incoming.len)
356 CurBuffy = Incoming.len - 1;