drop the builtin so called editor and the mailx feature we really dont
[apps/madmutt.git] / status.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/macros.h>
17 #include <lib-lib/mapping.h>
18
19 #include "mutt.h"
20 #include "mutt_menu.h"
21 #include "mutt_curses.h"
22 #include "sort.h"
23 #include "mx.h"
24 #include "buffy.h"
25
26
27 #include <string.h>
28 #include <ctype.h>
29 #include <unistd.h>
30
31 #define SW              (option(OPTMBOXPANE)?SidebarWidth:0)
32
33 static char *get_sort_str (char *buf, size_t buflen, int method)
34 {
35   snprintf (buf, buflen, "%s%s%s",
36             (method & SORT_REVERSE) ? "reverse-" : "",
37             (method & SORT_LAST) ? "last-" : "",
38             mutt_getnamebyvalue (method & SORT_MASK, SortMethods));
39   return buf;
40 }
41
42 /* %b = number of incoming folders with unread messages [option]
43  * %B = short mailbox path
44  * %d = number of deleted messages [option]
45  * %f = full mailbox path
46  * %F = number of flagged messages [option]
47  * %h = hostname
48  * %l = length of mailbox (in bytes) [option]
49  * %m = total number of messages [option]
50  * %M = number of messages shown (virutal message count when limiting) [option]
51  * %n = number of new messages [option]
52  * %p = number of postponed messages [option]
53  * %P = percent of way through index
54  * %r = readonly/wontwrite/changed flag
55  * %s = current sorting method ($sort)
56  * %S = current aux sorting method ($sort_aux)
57  * %t = # of tagged messages [option]
58  * %v = Mutt-ng version 
59  * %V = currently active limit pattern [option] */
60 static const char *status_format_str (char *buf, size_t buflen, char op,
61                                       const char *src, const char *prefix,
62                                       const char *ifstring,
63                                       const char *elsestring,
64                                       unsigned long data, format_flag flags)
65 {
66   char fmt[SHORT_STRING], tmp[SHORT_STRING];
67   const char *cp, *p;
68   int count, optional = (flags & M_FORMAT_OPTIONAL);
69   MUTTMENU *menu = (MUTTMENU *) data;
70
71   *buf = 0;
72   switch (op) {
73   case 'b':
74     if (!optional) {
75       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
76       snprintf (buf, buflen, fmt, buffy_check (0));
77     }
78     else if (!buffy_check (0))
79       optional = 0;
80     break;
81
82   case 'B':
83     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
84     if (Context && Context->compressinfo && Context->realpath) {
85       if ((p = strrchr (Context->realpath, '/')))
86         m_strcpy(tmp, sizeof(tmp), p + 1);
87       else
88         m_strcpy(tmp, sizeof(tmp), Context->realpath);
89     }
90     else
91     if (Context && Context->path) {
92       if ((p = strrchr (Context->path, '/')))
93         m_strcpy(tmp, sizeof(tmp), p + 1);
94       else
95         m_strcpy(tmp, sizeof(tmp), Context->path);
96     }
97     else
98       m_strcpy(tmp, sizeof(tmp), _("no mailbox"));
99     snprintf (buf, buflen, fmt, tmp);
100     break;
101
102   case 'd':
103     if (!optional) {
104       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
105       snprintf (buf, buflen, fmt, Context ? Context->deleted : 0);
106     }
107     else if (!Context || !Context->deleted)
108       optional = 0;
109     break;
110
111   case 'h':
112     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
113     snprintf (buf, buflen, fmt, NONULL (Hostname));
114     break;
115
116   case 'f':
117     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
118     if (Context && Context->compressinfo && Context->realpath) {
119       m_strcpy(tmp, sizeof(tmp), Context->realpath);
120       mutt_pretty_mailbox (tmp);
121     }
122     else
123     if (Context && Context->path) {
124       m_strcpy(tmp, sizeof(tmp), Context->path);
125       mutt_pretty_mailbox (tmp);
126     }
127     else
128       m_strcpy(tmp, sizeof(tmp), _("(no mailbox)"));
129     snprintf (buf, buflen, fmt, tmp);
130     break;
131
132   case 'F':
133     if (!optional) {
134       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
135       snprintf (buf, buflen, fmt, Context ? Context->flagged : 0);
136     }
137     else if (!Context || !Context->flagged)
138       optional = 0;
139     break;
140
141   case 'l':
142     if (!optional) {
143       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
144       mutt_pretty_size (tmp, sizeof (tmp), Context ? Context->size : 0);
145       snprintf (buf, buflen, fmt, tmp);
146     }
147     else if (!Context || !Context->size)
148       optional = 0;
149     break;
150
151   case 'L':
152     if (!optional) {
153       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
154       mutt_pretty_size (tmp, sizeof (tmp), Context ? Context->vsize : 0);
155       snprintf (buf, buflen, fmt, tmp);
156     }
157     else if (!Context || !Context->pattern)
158       optional = 0;
159     break;
160
161   case 'm':
162     if (!optional) {
163       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
164       snprintf (buf, buflen, fmt, Context ? Context->msgcount : 0);
165     }
166     else if (!Context || !Context->msgcount)
167       optional = 0;
168     break;
169
170   case 'M':
171     if (!optional) {
172       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
173       snprintf (buf, buflen, fmt, Context ? Context->vcount : 0);
174     }
175     else if (!Context || !Context->pattern)
176       optional = 0;
177     break;
178
179   case 'n':
180     if (!optional) {
181       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
182       snprintf (buf, buflen, fmt, Context ? Context->new : 0);
183     }
184     else if (!Context || Context->new <= 0)
185       optional = 0;
186     break;
187
188   case 'o':
189     if (!optional) {
190       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
191       snprintf (buf, buflen, fmt,
192                 Context ? Context->unread - Context->new : 0);
193     }
194     else if (!Context || !(Context->unread - Context->new))
195       optional = 0;
196     break;
197
198   case 'p':
199     count = mutt_num_postponed (0);
200     if (!optional) {
201       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
202       snprintf (buf, buflen, fmt, count);
203     }
204     else if (!count)
205       optional = 0;
206     break;
207
208   case 'P':
209     if (menu->top + menu->pagelen >= menu->max)
210       cp = menu->top ? "end" : "all";
211     else {
212       count = (100 * (menu->top + menu->pagelen)) / menu->max;
213       snprintf (tmp, sizeof (tmp), "%d%%", count);
214       cp = tmp;
215     }
216     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
217     snprintf (buf, buflen, fmt, cp);
218     break;
219
220   case 'r':
221     {
222       int i = 0;
223
224       if (Context) {
225         /* XXX: deleted doesn't necessarily mean changed in IMAP */
226         i = option (OPTATTACHMSG) ? 3
227             : ((Context->readonly || Context->dontwrite) ? 2
228                : (Context->changed || (Context-> magic != M_IMAP && Context->
229                                         deleted)) ? 1 : 0);
230       }
231
232       if (!StChars)
233         buf[0] = 0;
234       else if (i >= m_strlen(StChars))
235         buf[0] = StChars[0];
236       else
237         buf[0] = StChars[i];
238
239       buf[1] = 0;
240       break;
241     }
242
243   case 's':
244     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
245     snprintf (buf, buflen, fmt, get_sort_str (tmp, sizeof (tmp), Sort));
246     break;
247
248   case 'S':
249     snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
250     snprintf (buf, buflen, fmt, get_sort_str (tmp, sizeof (tmp), SortAux));
251     break;
252
253   case 't':
254     if (!optional) {
255       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
256       snprintf (buf, buflen, fmt, Context ? Context->tagged : 0);
257     }
258     else if (!Context || !Context->tagged)
259       optional = 0;
260     break;
261
262   case 'u':
263     if (!optional) {
264       snprintf (fmt, sizeof (fmt), "%%%sd", prefix);
265       snprintf (buf, buflen, fmt, Context ? Context->unread : 0);
266     }
267     else if (!Context || !Context->unread)
268       optional = 0;
269     break;
270
271   case 'v':
272     m_strcpy(buf, buflen, mutt_make_version(0));
273     break;
274
275   case 'V':
276     if (!optional) {
277       snprintf (fmt, sizeof (fmt), "%%%ss", prefix);
278       snprintf (buf, buflen, fmt,
279                 (Context && Context->pattern) ? Context->pattern : "");
280     }
281     else if (!Context || !Context->pattern)
282       optional = 0;
283     break;
284
285   case 0:
286     *buf = 0;
287     return (src);
288
289   default:
290     snprintf (buf, buflen, "%%%s%c", prefix, op);
291     break;
292   }
293
294   if (optional)
295     menu_status_line (buf, buflen, menu, ifstring);
296   else if (flags & M_FORMAT_OPTIONAL)
297     menu_status_line (buf, buflen, menu, elsestring);
298
299   return (src);
300 }
301
302 void menu_status_line (char* buf, size_t len, MUTTMENU* menu, const char* p) {
303   /*
304    * if we have enough space for buffer, format lines to $COLS-$SidebarWidth
305    * only to not wrap past end of screen
306    */
307   int width = COLS - SW;
308   mutt_FormatString (buf, (width >= len ? len : (width + 1)),
309                      p, status_format_str,
310                      (unsigned long) menu, 0);
311 }