NeoMutt  2021-02-05
Teaching an old dog new tricks
DOXYGEN
status.c
Go to the documentation of this file.
1 
29 #include "config.h"
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include "mutt/lib.h"
34 #include "config/lib.h"
35 #include "core/lib.h"
36 #include "gui/lib.h"
37 #include "status.h"
38 #include "context.h"
39 #include "format_flags.h"
40 #include "mutt_globals.h"
41 #include "mutt_mailbox.h"
42 #include "mutt_menu.h"
43 #include "muttlib.h"
44 #include "options.h"
45 #include "protos.h"
46 #include "sort.h"
47 
48 /* These Config Variables are only used in status.c */
50 
58 static char *get_sort_str(char *buf, size_t buflen, enum SortType method)
59 {
60  snprintf(buf, buflen, "%s%s%s", (method & SORT_REVERSE) ? "reverse-" : "",
61  (method & SORT_LAST) ? "last-" : "",
63  return buf;
64 }
65 
70 {
71  struct Menu *menu;
72  struct Mailbox *m;
73 };
74 
103 static const char *status_format_str(char *buf, size_t buflen, size_t col, int cols,
104  char op, const char *src, const char *prec,
105  const char *if_str, const char *else_str,
106  intptr_t data, MuttFormatFlags flags)
107 {
108  char fmt[128], tmp[128];
109  bool optional = (flags & MUTT_FORMAT_OPTIONAL);
110  struct Menu *menu = ((struct MenuStatusLineData *) data)->menu;
111  struct Mailbox *m = ((struct MenuStatusLineData *) data)->m;
112 
113  *buf = '\0';
114  switch (op)
115  {
116  case 'b':
117  {
118  const int num = mutt_mailbox_check(m, 0);
119  if (!optional)
120  {
121  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
122  snprintf(buf, buflen, fmt, num);
123  }
124  else if (num == 0)
125  optional = false;
126  break;
127  }
128 
129  case 'd':
130  {
131  const int num = m ? m->msg_deleted : 0;
132  if (!optional)
133  {
134  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
135  snprintf(buf, buflen, fmt, num);
136  }
137  else if (num == 0)
138  optional = false;
139  break;
140  }
141 
142  case 'D':
143  // If there's a descriptive name, use it. Otherwise, fall-through
144  if (m && m->name)
145  {
146  mutt_str_copy(tmp, m->name, sizeof(tmp));
147  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
148  snprintf(buf, buflen, fmt, tmp);
149  break;
150  }
151  /* fallthrough */
152  case 'f':
153 #ifdef USE_COMP_MBOX
154  if (m && m->compress_info && (m->realpath[0] != '\0'))
155  {
156  mutt_str_copy(tmp, m->realpath, sizeof(tmp));
157  mutt_pretty_mailbox(tmp, sizeof(tmp));
158  }
159  else
160 #endif
161  if (m && (m->type == MUTT_NOTMUCH) && m->name)
162  {
163  mutt_str_copy(tmp, m->name, sizeof(tmp));
164  }
165  else if (m && !mutt_buffer_is_empty(&m->pathbuf))
166  {
167  mutt_str_copy(tmp, mailbox_path(m), sizeof(tmp));
168  mutt_pretty_mailbox(tmp, sizeof(tmp));
169  }
170  else
171  mutt_str_copy(tmp, _("(no mailbox)"), sizeof(tmp));
172 
173  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
174  snprintf(buf, buflen, fmt, tmp);
175  break;
176  case 'F':
177  {
178  const int num = m ? m->msg_flagged : 0;
179  if (!optional)
180  {
181  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
182  snprintf(buf, buflen, fmt, num);
183  }
184  else if (num == 0)
185  optional = false;
186  break;
187  }
188 
189  case 'h':
190  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
191  snprintf(buf, buflen, fmt, NONULL(ShortHostname));
192  break;
193 
194  case 'l':
195  {
196  const off_t num = m ? m->size : 0;
197  if (!optional)
198  {
199  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
200  mutt_str_pretty_size(tmp, sizeof(tmp), num);
201  snprintf(buf, buflen, fmt, tmp);
202  }
203  else if (num == 0)
204  optional = false;
205  break;
206  }
207 
208  case 'L':
209  if (!optional)
210  {
211  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
212  mutt_str_pretty_size(tmp, sizeof(tmp), Context ? Context->vsize : 0);
213  snprintf(buf, buflen, fmt, tmp);
214  }
215  else if (!ctx_has_limit(Context))
216  optional = false;
217  break;
218 
219  case 'm':
220  {
221  const int num = m ? m->msg_count : 0;
222  if (!optional)
223  {
224  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
225  snprintf(buf, buflen, fmt, num);
226  }
227  else if (num == 0)
228  optional = false;
229  break;
230  }
231 
232  case 'M':
233  if (!optional)
234  {
235  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
236  snprintf(buf, buflen, fmt, m ? m->vcount : 0);
237  }
238  else if (!ctx_has_limit(Context))
239  optional = false;
240  break;
241 
242  case 'n':
243  {
244  const int num = m ? m->msg_new : 0;
245  if (!optional)
246  {
247  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
248  snprintf(buf, buflen, fmt, num);
249  }
250  else if (num == 0)
251  optional = false;
252  break;
253  }
254 
255  case 'o':
256  {
257  const int num = m ? (m->msg_unread - m->msg_new) : 0;
258  if (!optional)
259  {
260  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
261  snprintf(buf, buflen, fmt, num);
262  }
263  else if (num == 0)
264  optional = false;
265  break;
266  }
267 
268  case 'p':
269  {
270  const int count = mutt_num_postponed(m, false);
271  if (!optional)
272  {
273  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
274  snprintf(buf, buflen, fmt, count);
275  }
276  else if (count == 0)
277  optional = false;
278  break;
279  }
280 
281  case 'P':
282  {
283  if (!menu)
284  break;
285  char *cp = NULL;
286  if (menu->top + menu->pagelen >= menu->max)
287  {
288  cp = menu->top ?
289  /* L10N: Status bar message: the end of the list emails is visible in the index */
290  _("end") :
291  /* L10N: Status bar message: all the emails are visible in the index */
292  _("all");
293  }
294  else
295  {
296  int count = (100 * (menu->top + menu->pagelen)) / menu->max;
297  snprintf(tmp, sizeof(tmp), "%d%%", count);
298  cp = tmp;
299  }
300  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
301  snprintf(buf, buflen, fmt, cp);
302  break;
303  }
304 
305  case 'r':
306  {
307  size_t i = 0;
308 
309  if (m)
310  {
311  i = OptAttachMsg ? 3 :
312  ((m->readonly || m->dontwrite) ? 2 :
313  (m->changed ||
314  /* deleted doesn't necessarily mean changed in IMAP */
315  (m->type != MUTT_IMAP && m->msg_deleted)) ?
316  1 :
317  0);
318  }
319 
320  if (!C_StatusChars || !C_StatusChars->len)
321  buf[0] = '\0';
322  else if (i >= C_StatusChars->len)
323  snprintf(buf, buflen, "%s", C_StatusChars->chars[0]);
324  else
325  snprintf(buf, buflen, "%s", C_StatusChars->chars[i]);
326  break;
327  }
328 
329  case 'R':
330  {
331  const int read = m ? (m->msg_count - m->msg_unread) : 0;
332  if (!optional)
333  {
334  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
335  snprintf(buf, buflen, fmt, read);
336  }
337  else if (read == 0)
338  optional = false;
339  break;
340  }
341 
342  case 's':
343  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
344  snprintf(buf, buflen, fmt, get_sort_str(tmp, sizeof(tmp), C_Sort));
345  break;
346 
347  case 'S':
348  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
349  snprintf(buf, buflen, fmt, get_sort_str(tmp, sizeof(tmp), C_SortAux));
350  break;
351 
352  case 't':
353  {
354  const int num = m ? m->msg_tagged : 0;
355  if (!optional)
356  {
357  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
358  snprintf(buf, buflen, fmt, num);
359  }
360  else if (num == 0)
361  optional = false;
362  break;
363  }
364 
365  case 'u':
366  if (!optional)
367  {
368  snprintf(fmt, sizeof(fmt), "%%%sd", prec);
369  snprintf(buf, buflen, fmt, m ? m->msg_unread : 0);
370  }
371  else if (!m || (m->msg_unread == 0))
372  optional = false;
373  break;
374 
375  case 'v':
376  snprintf(buf, buflen, "%s", mutt_make_version());
377  break;
378 
379  case 'V':
380  if (!optional)
381  {
382  snprintf(fmt, sizeof(fmt), "%%%ss", prec);
383  snprintf(buf, buflen, fmt, ctx_has_limit(Context) ? Context->pattern : "");
384  }
385  else if (!ctx_has_limit(Context))
386  optional = false;
387  break;
388 
389  case 0:
390  *buf = '\0';
391  return src;
392 
393  default:
394  snprintf(buf, buflen, "%%%s%c", prec, op);
395  break;
396  }
397 
398  if (optional)
399  {
400  mutt_expando_format(buf, buflen, col, cols, if_str, status_format_str,
401  (intptr_t) data, MUTT_FORMAT_NO_FLAGS);
402  }
403  else if (flags & MUTT_FORMAT_OPTIONAL)
404  {
405  mutt_expando_format(buf, buflen, col, cols, else_str, status_format_str,
406  (intptr_t) data, MUTT_FORMAT_NO_FLAGS);
407  }
408 
409  return src;
410 }
411 
420 void menu_status_line(char *buf, size_t buflen, struct Menu *menu,
421  struct Mailbox *m, const char *p)
422 {
423  struct MenuStatusLineData data = { .menu = menu, .m = m };
424  mutt_expando_format(buf, buflen, 0, menu ? menu->win_ibar->state.cols : buflen, p,
425  status_format_str, (intptr_t) &data, MUTT_FORMAT_NO_FLAGS);
426 }
Convenience wrapper for the gui headers.
The "current" mailbox.
Definition: context.h:38
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox&#39;s path string.
Definition: mailbox.h:204
void mutt_pretty_mailbox(char *buf, size_t buflen)
Shorten a mailbox path using &#39;~&#39; or &#39;=&#39;.
Definition: muttlib.c:523
enum MailboxType type
Mailbox type.
Definition: mailbox.h:105
uint8_t MuttFormatFlags
Flags for mutt_expando_format(), e.g. MUTT_FORMAT_FORCESUBJ.
Definition: format_flags.h:29
#define NONULL(x)
Definition: string2.h:37
int msg_count
Total number of messages.
Definition: mailbox.h:91
off_t size
Size of the Mailbox.
Definition: mailbox.h:87
SortType
Methods for sorting.
Definition: sort2.h:43
char ** chars
The array of multibyte character strings.
Definition: mbtable.h:39
GUI selectable list of items.
Definition: mutt_menu.h:52
int msg_deleted
Number of deleted messages.
Definition: mailbox.h:96
int msg_unread
Number of unread messages.
Definition: mailbox.h:92
The "currently-open" mailbox.
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:93
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:84
bool ctx_has_limit(const struct Context *ctx)
Is a limit active?
Definition: context.c:417
#define _(a)
Definition: message.h:28
struct MbTable * C_StatusChars
Config: Indicator characters for the status bar.
Definition: status.c:49
multibyte character table
Definition: mbtable.h:35
Flags to control mutt_expando_format()
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition: format_flags.h:30
int vcount
The number of virtual messages.
Definition: mailbox.h:102
Convenience wrapper for the config headers.
Assorted sorting methods.
Some miscellaneous functions.
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
char * name
A short name for the Mailbox.
Definition: mailbox.h:85
struct MuttWindow * win_ibar
Definition: mutt_menu.h:64
bool readonly
Don&#39;t allow changes to the mailbox.
Definition: mailbox.h:119
static char * get_sort_str(char *buf, size_t buflen, enum SortType method)
Get the sort method as a string.
Definition: status.c:58
Convenience wrapper for the core headers.
WHERE short C_Sort
Config: Sort method for the index.
Definition: sort.h:60
void menu_status_line(char *buf, size_t buflen, struct Menu *menu, struct Mailbox *m, const char *p)
Create the status line.
Definition: status.c:420
short cols
Number of columns, can be MUTT_WIN_SIZE_UNLIMITED.
Definition: mutt_window.h:57
off_t vsize
Size (in bytes) of the messages shown.
Definition: context.h:40
Prototypes for many functions.
WHERE short C_SortAux
Config: Secondary sort method for the index.
Definition: sort.h:61
struct WindowState state
Current state of the Window.
Definition: mutt_window.h:120
WHERE bool OptAttachMsg
(pseudo) used by attach-message
Definition: options.h:31
A mailbox.
Definition: mailbox.h:81
int mutt_mailbox_check(struct Mailbox *m_cur, int force)
Check all all Mailboxes for new mail.
Definition: mutt_mailbox.c:138
int top
Entry that is the top of the current page.
Definition: mutt_menu.h:75
WHERE char * ShortHostname
Short version of the hostname.
Definition: mutt_globals.h:50
void mutt_str_pretty_size(char *buf, size_t buflen, size_t num)
Display an abbreviated size, like 3.4K.
Definition: muttlib.c:1662
bool dontwrite
Don&#39;t write the mailbox on close.
Definition: mailbox.h:115
GUI present the user with a selectable list.
int msg_tagged
How many messages are tagged?
Definition: mailbox.h:97
int pagelen
Number of entries per screen.
Definition: mutt_menu.h:60
Data for creating a Menu line.
Definition: status.c:69
int max
Number of entries in the menu.
Definition: mutt_menu.h:57
&#39;Notmuch&#39; (virtual) Mailbox type
Definition: mailbox.h:54
GUI display a user-configurable status line.
uint8_t flags
e.g. MB_NORMAL
Definition: mailbox.h:134
int mutt_num_postponed(struct Mailbox *m, bool force)
Return the number of postponed messages.
Definition: postpone.c:89
const struct Mapping SortMethods[]
Sort methods for &#39;$sort&#39; for the index.
Definition: mutt_config.c:105
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:716
struct Mailbox * m
Definition: status.c:72
int msg_new
Number of new messages.
Definition: mailbox.h:95
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t callback, intptr_t data, MuttFormatFlags flags)
Expand expandos (x) in a string.
Definition: muttlib.c:773
void * compress_info
Compressed mbox module private data.
Definition: mailbox.h:124
#define MUTT_FORMAT_OPTIONAL
Allow optional field processing.
Definition: format_flags.h:33
bool changed
Mailbox has been modified.
Definition: mailbox.h:114
Hundreds of global variables to back the user variables.
Handling of global boolean variables.
struct Buffer pathbuf
Definition: mailbox.h:83
Convenience wrapper for the library headers.
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:81
bool mutt_buffer_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:252
&#39;IMAP&#39; Mailbox type
Definition: mailbox.h:53
char * pattern
Limit pattern string.
Definition: context.h:41
#define SORT_LAST
Sort thread by last-X, e.g. received date.
Definition: sort2.h:82
struct Menu * menu
Definition: status.c:71
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:80
int len
Number of characters.
Definition: mbtable.h:38
const char * mutt_make_version(void)
Generate the NeoMutt version string.
Definition: muttlib.c:1461
static const char * status_format_str(char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
Create the status bar string - Implements format_t.
Definition: status.c:103