NeoMutt  2018-07-16 +2481-68dcde
Teaching an old dog new tricks
DOXYGEN
sort.c
Go to the documentation of this file.
1 
30 #include "config.h"
31 #include <stdbool.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include "mutt/mutt.h"
35 #include "address/lib.h"
36 #include "email/lib.h"
37 #include "core/lib.h"
38 #include "sort.h"
39 #include "alias.h"
40 #include "context.h"
41 #include "globals.h"
42 #include "mutt_logging.h"
43 #include "mutt_thread.h"
44 #include "options.h"
45 #include "score.h"
46 #ifdef USE_NNTP
47 #include "nntp/nntp.h"
48 #endif
49 
50 /* These Config Variables are only used in sort.c */
52 
53 /* function to use as discriminator when normal sort method is equal */
54 static sort_t *AuxSort = NULL;
55 
65 int perform_auxsort(int retval, const void *a, const void *b)
66 {
67  /* If the items compared equal by the main sort
68  * and we're not already doing an 'aux' sort... */
69  if ((retval == 0) && AuxSort && !OptAuxSort)
70  {
71  OptAuxSort = true;
72  retval = AuxSort(a, b);
73  OptAuxSort = false;
74  if (retval != 0)
75  return retval;
76  }
77  /* If the items still match, use their index positions
78  * to maintain a stable sort order */
79  if (retval == 0)
80  retval = (*((struct Email const *const *) a))->index -
81  (*((struct Email const *const *) b))->index;
82  return retval;
83 }
84 
88 static int compare_score(const void *a, const void *b)
89 {
90  struct Email const *const *pa = (struct Email const *const *) a;
91  struct Email const *const *pb = (struct Email const *const *) b;
92  int result = (*pb)->score - (*pa)->score; /* note that this is reverse */
93  result = perform_auxsort(result, a, b);
94  return SORT_CODE(result);
95 }
96 
100 static int compare_size(const void *a, const void *b)
101 {
102  struct Email const *const *pa = (struct Email const *const *) a;
103  struct Email const *const *pb = (struct Email const *const *) b;
104  int result = (*pa)->content->length - (*pb)->content->length;
105  result = perform_auxsort(result, a, b);
106  return SORT_CODE(result);
107 }
108 
112 static int compare_date_sent(const void *a, const void *b)
113 {
114  struct Email const *const *pa = (struct Email const *const *) a;
115  struct Email const *const *pb = (struct Email const *const *) b;
116  int result = (*pa)->date_sent - (*pb)->date_sent;
117  result = perform_auxsort(result, a, b);
118  return SORT_CODE(result);
119 }
120 
124 static int compare_subject(const void *a, const void *b)
125 {
126  struct Email const *const *pa = (struct Email const *const *) a;
127  struct Email const *const *pb = (struct Email const *const *) b;
128  int rc;
129 
130  if (!(*pa)->env->real_subj)
131  {
132  if (!(*pb)->env->real_subj)
133  rc = compare_date_sent(pa, pb);
134  else
135  rc = -1;
136  }
137  else if (!(*pb)->env->real_subj)
138  rc = 1;
139  else
140  rc = mutt_str_strcasecmp((*pa)->env->real_subj, (*pb)->env->real_subj);
141  rc = perform_auxsort(rc, a, b);
142  return SORT_CODE(rc);
143 }
144 
155 const char *mutt_get_name(const struct Address *a)
156 {
157  struct Address *ali = NULL;
158 
159  if (a)
160  {
161  if (C_ReverseAlias && (ali = mutt_alias_reverse_lookup(a)) && ali->personal)
162  return ali->personal;
163  else if (a->personal)
164  return a->personal;
165  else if (a->mailbox)
166  return mutt_addr_for_display(a);
167  }
168  /* don't return NULL to avoid segfault when printing/comparing */
169  return "";
170 }
171 
175 static int compare_to(const void *a, const void *b)
176 {
177  struct Email const *const *ppa = (struct Email const *const *) a;
178  struct Email const *const *ppb = (struct Email const *const *) b;
179  char fa[128];
180 
181  mutt_str_strfcpy(fa, mutt_get_name(TAILQ_FIRST(&(*ppa)->env->to)), sizeof(fa));
182  const char *fb = mutt_get_name(TAILQ_FIRST(&(*ppb)->env->to));
183  int result = mutt_str_strncasecmp(fa, fb, sizeof(fa));
184  result = perform_auxsort(result, a, b);
185  return SORT_CODE(result);
186 }
187 
191 static int compare_from(const void *a, const void *b)
192 {
193  struct Email const *const *ppa = (struct Email const *const *) a;
194  struct Email const *const *ppb = (struct Email const *const *) b;
195  char fa[128];
196 
197  mutt_str_strfcpy(fa, mutt_get_name(TAILQ_FIRST(&(*ppa)->env->from)), sizeof(fa));
198  const char *fb = mutt_get_name(TAILQ_FIRST(&(*ppb)->env->from));
199  int result = mutt_str_strncasecmp(fa, fb, sizeof(fa));
200  result = perform_auxsort(result, a, b);
201  return SORT_CODE(result);
202 }
203 
207 static int compare_date_received(const void *a, const void *b)
208 {
209  struct Email const *const *pa = (struct Email const *const *) a;
210  struct Email const *const *pb = (struct Email const *const *) b;
211  int result = (*pa)->received - (*pb)->received;
212  result = perform_auxsort(result, a, b);
213  return SORT_CODE(result);
214 }
215 
219 static int compare_order(const void *a, const void *b)
220 {
221  struct Email const *const *ea = (struct Email const *const *) a;
222  struct Email const *const *eb = (struct Email const *const *) b;
223 
224  /* no need to auxsort because you will never have equality here */
225  return SORT_CODE((*ea)->index - (*eb)->index);
226 }
227 
231 static int compare_spam(const void *a, const void *b)
232 {
233  struct Email const *const *ppa = (struct Email const *const *) a;
234  struct Email const *const *ppb = (struct Email const *const *) b;
235  char *aptr = NULL, *bptr = NULL;
236  int ahas, bhas;
237  int result = 0;
238  double difference;
239 
240  /* Firstly, require spam attributes for both msgs */
241  /* to compare. Determine which msgs have one. */
242  ahas = (*ppa)->env && !mutt_buffer_is_empty(&(*ppa)->env->spam);
243  bhas = (*ppb)->env && !mutt_buffer_is_empty(&(*ppb)->env->spam);
244 
245  /* If one msg has spam attr but other does not, sort the one with first. */
246  if (ahas && !bhas)
247  return SORT_CODE(1);
248  if (!ahas && bhas)
249  return SORT_CODE(-1);
250 
251  /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
252  if (!ahas && !bhas)
253  {
254  result = perform_auxsort(result, a, b);
255  return SORT_CODE(result);
256  }
257 
258  /* Both have spam attrs. */
259 
260  /* preliminary numeric examination */
261  difference =
262  (strtod((*ppa)->env->spam.data, &aptr) - strtod((*ppb)->env->spam.data, &bptr));
263 
264  /* map double into comparison (-1, 0, or 1) */
265  result = ((difference < 0.0) ? -1 : (difference > 0.0) ? 1 : 0);
266 
267  /* If either aptr or bptr is equal to data, there is no numeric */
268  /* value for that spam attribute. In this case, compare lexically. */
269  if ((aptr == (*ppa)->env->spam.data) || (bptr == (*ppb)->env->spam.data))
270  return SORT_CODE(strcmp(aptr, bptr));
271 
272  /* Otherwise, we have numeric value for both attrs. If these values */
273  /* are equal, then we first fall back upon string comparison, then */
274  /* upon auxiliary sort. */
275  if (result == 0)
276  {
277  result = strcmp(aptr, bptr);
278  result = perform_auxsort(result, a, b);
279  }
280 
281  return SORT_CODE(result);
282 }
283 
287 static int compare_label(const void *a, const void *b)
288 {
289  struct Email const *const *ppa = (struct Email const *const *) a;
290  struct Email const *const *ppb = (struct Email const *const *) b;
291  int ahas, bhas, result = 0;
292 
293  /* As with compare_spam, not all messages will have the x-label
294  * property. Blank X-Labels are treated as null in the index
295  * display, so we'll consider them as null for sort, too. */
296  ahas = (*ppa)->env && (*ppa)->env->x_label && *((*ppa)->env->x_label);
297  bhas = (*ppb)->env && (*ppb)->env->x_label && *((*ppb)->env->x_label);
298 
299  /* First we bias toward a message with a label, if the other does not. */
300  if (ahas && !bhas)
301  return SORT_CODE(-1);
302  if (!ahas && bhas)
303  return SORT_CODE(1);
304 
305  /* If neither has a label, use aux sort. */
306  if (!ahas && !bhas)
307  {
308  result = perform_auxsort(result, a, b);
309  return SORT_CODE(result);
310  }
311 
312  /* If both have a label, we just do a lexical compare. */
313  result = mutt_str_strcasecmp((*ppa)->env->x_label, (*ppb)->env->x_label);
314  return SORT_CODE(result);
315 }
316 
323 {
324  switch (method)
325  {
326  case SORT_DATE:
327  return compare_date_sent;
328  case SORT_FROM:
329  return compare_from;
330  case SORT_LABEL:
331  return compare_label;
332  case SORT_ORDER:
333 #ifdef USE_NNTP
334  if (Context && (Context->mailbox->magic == MUTT_NNTP))
335  return nntp_compare_order;
336  else
337 #endif
338  return compare_order;
339  case SORT_RECEIVED:
340  return compare_date_received;
341  case SORT_SCORE:
342  return compare_score;
343  case SORT_SIZE:
344  return compare_size;
345  case SORT_SPAM:
346  return compare_spam;
347  case SORT_SUBJECT:
348  return compare_subject;
349  case SORT_TO:
350  return compare_to;
351  default:
352  return NULL;
353  }
354  /* not reached */
355 }
356 
362 void mutt_sort_headers(struct Context *ctx, bool init)
363 {
364  struct Email *e = NULL;
365  struct MuttThread *thread = NULL, *top = NULL;
366  sort_t *sortfunc = NULL;
367 
368  OptNeedResort = false;
369 
370  if (!ctx)
371  return;
372 
373  if (ctx->mailbox->msg_count == 0)
374  {
375  /* this function gets called by mutt_sync_mailbox(), which may have just
376  * deleted all the messages. the virtual message numbers are not updated
377  * in that routine, so we must make sure to zero the vcount member. */
378  ctx->mailbox->vcount = 0;
379  ctx->vsize = 0;
380  mutt_clear_threads(ctx);
381  return; /* nothing to do! */
382  }
383 
384  if (!ctx->mailbox->quiet)
385  mutt_message(_("Sorting mailbox..."));
386 
387  if (OptNeedRescore && C_Score)
388  {
389  for (int i = 0; i < ctx->mailbox->msg_count; i++)
390  mutt_score_message(ctx->mailbox, ctx->mailbox->emails[i], true);
391  }
392  OptNeedRescore = false;
393 
394  if (OptResortInit)
395  {
396  OptResortInit = false;
397  init = true;
398  }
399 
400  if (init && ctx->tree)
401  mutt_clear_threads(ctx);
402 
403  if ((C_Sort & SORT_MASK) == SORT_THREADS)
404  {
405  AuxSort = NULL;
406  /* if $sort_aux changed after the mailbox is sorted, then all the
407  * subthreads need to be resorted */
408  if (OptSortSubthreads)
409  {
410  int i = C_Sort;
411  C_Sort = C_SortAux;
412  if (ctx->tree)
413  ctx->tree = mutt_sort_subthreads(ctx->tree, true);
414  C_Sort = i;
415  OptSortSubthreads = false;
416  }
417  mutt_sort_threads(ctx, init);
418  }
419  else if (!(sortfunc = mutt_get_sort_func(C_Sort & SORT_MASK)) ||
420  !(AuxSort = mutt_get_sort_func(C_SortAux & SORT_MASK)))
421  {
422  mutt_error(_("Could not find sorting function [report this bug]"));
423  return;
424  }
425  else
426  qsort((void *) ctx->mailbox->emails, ctx->mailbox->msg_count,
427  sizeof(struct Email *), sortfunc);
428 
429  /* adjust the virtual message numbers */
430  ctx->mailbox->vcount = 0;
431  for (int i = 0; i < ctx->mailbox->msg_count; i++)
432  {
433  struct Email *e_cur = ctx->mailbox->emails[i];
434  if ((e_cur->vnum != -1) || (e_cur->collapsed && (!ctx->pattern || e_cur->limited)))
435  {
436  e_cur->vnum = ctx->mailbox->vcount;
437  ctx->mailbox->v2r[ctx->mailbox->vcount] = i;
438  ctx->mailbox->vcount++;
439  }
440  e_cur->msgno = i;
441  }
442 
443  /* re-collapse threads marked as collapsed */
444  if ((C_Sort & SORT_MASK) == SORT_THREADS)
445  {
446  top = ctx->tree;
447  while ((thread = top))
448  {
449  while (!thread->message)
450  thread = thread->child;
451  e = thread->message;
452 
453  if (e->collapsed)
454  mutt_collapse_thread(ctx, e);
455  top = top->next;
456  }
457  mutt_set_vnum(ctx);
458  }
459 
460  if (!ctx->mailbox->quiet)
462 }
struct Email ** emails
Array of Emails.
Definition: mailbox.h:110
The "current" mailbox.
Definition: context.h:36
int sort_t(const void *a, const void *b)
typedef sort_t - Prototype for a function to compare two emails
Definition: sort.h:48
Sort by the date the email was sent.
Definition: sort.h:50
int msg_count
Total number of messages.
Definition: mailbox.h:102
The envelope/body of an email.
Definition: email.h:39
#define TAILQ_FIRST(head)
Definition: queue.h:717
void mutt_set_vnum(struct Context *ctx)
Set the virtual index number of all the messages in a mailbox.
Definition: mutt_thread.c:1186
&#39;NNTP&#39; (Usenet) Mailbox type
Definition: mailbox.h:51
Structs that make up an email.
struct MuttThread * tree
Top of thread tree.
Definition: context.h:42
The "currently-open" mailbox.
#define mutt_message(...)
Definition: logging.h:83
NeoMutt Logging.
WHERE bool OptNeedResort
(pseudo) used to force a re-sort
Definition: options.h:41
struct Body * content
List of MIME parts.
Definition: email.h:92
struct MuttThread * mutt_sort_subthreads(struct MuttThread *thread, bool init)
Sort the children of a thread.
Definition: mutt_thread.c:655
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:169
#define _(a)
Definition: message.h:28
WHERE bool OptAuxSort
(pseudo) using auxiliary sort function
Definition: options.h:33
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
static int compare_label(const void *a, const void *b)
Compare the labels of two emails - Implements sort_t.
Definition: sort.c:287
WHERE bool OptNeedRescore
(pseudo) set when the &#39;score&#39; command is used
Definition: options.h:40
static int compare_date_sent(const void *a, const void *b)
Compare the sent date of two emails - Implements sort_t.
Definition: sort.c:112
Representation of a single alias to an email address.
#define mutt_collapse_thread(ctx, e)
Definition: mutt_thread.h:57
int vcount
The number of virtual messages.
Definition: mailbox.h:113
Hundreds of global variables to back the user variables.
Email Address Handling.
Assorted sorting methods.
static int compare_order(const void *a, const void *b)
Restore the &#39;unsorted&#39; order of emails - Implements sort_t.
Definition: sort.c:219
Sort by the email&#39;s subject.
Definition: sort.h:53
const char * mutt_addr_for_display(const struct Address *a)
Convert an Address for display purposes.
Definition: address.c:976
void mutt_sort_threads(struct Context *ctx, bool init)
Sort email threads.
Definition: mutt_thread.c:833
struct Mailbox * mailbox
Definition: context.h:50
enum MailboxType magic
Mailbox type.
Definition: mailbox.h:116
struct Envelope * env
Envelope information.
Definition: email.h:91
Convenience wrapper for the core headers.
Sort by the email&#39;s spam score.
Definition: sort.h:64
struct Address * mutt_alias_reverse_lookup(const struct Address *a)
Does the user have an alias for the given address.
Definition: alias.c:542
bool limited
Is this message in a limited view?
Definition: email.h:76
WHERE short C_Sort
Config: Sort method for the index.
Definition: sort.h:58
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:109
int score
Message score.
Definition: email.h:90
time_t date_sent
Time when the message was sent (UTC)
Definition: email.h:83
off_t vsize
Definition: context.h:38
struct MuttThread * child
Child of this Thread.
Definition: thread.h:46
sort_t * mutt_get_sort_func(enum SortType method)
Get the sort function for a given sort id.
Definition: sort.c:322
int nntp_compare_order(const void *a, const void *b)
Sort to mailbox order - Implements sort_t.
Definition: nntp.c:2363
WHERE short C_SortAux
Config: Secondary sort method for the index.
Definition: sort.h:59
bool C_ReverseAlias
Config: Display the alias in the index, rather than the message&#39;s sender.
Definition: sort.c:51
int mutt_str_strncasecmp(const char *a, const char *b, size_t l)
Compare two strings ignoring case (to a maximum), safely.
Definition: string.c:656
void mutt_sort_headers(struct Context *ctx, bool init)
Sort emails by their headers.
Definition: sort.c:362
Sort by the email&#39;s To field.
Definition: sort.h:58
static int compare_date_received(const void *a, const void *b)
Compare the date received of two emails - Implements sort_t.
Definition: sort.c:207
Create/manipulate threading in emails.
LOFF_T length
length (in bytes) of attachment
Definition: body.h:45
Sort by the emails label.
Definition: sort.h:69
size_t mutt_str_strfcpy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:750
#define SORT_CODE(x)
Definition: sort.h:38
bool collapsed
Is this message part of a collapsed thread?
Definition: email.h:75
bool quiet
Inhibit status messages?
Definition: mailbox.h:129
int vnum
Virtual message number.
Definition: email.h:89
static int compare_spam(const void *a, const void *b)
Compare the spam values of two emails - Implements sort_t.
Definition: sort.c:231
WHERE bool OptResortInit
(pseudo) used to force the next resort to be from scratch
Definition: options.h:49
struct Email * message
Email this Thread refers to.
Definition: thread.h:49
void mutt_clear_threads(struct Context *ctx)
Clear the threading of message in a mailbox.
Definition: mutt_thread.c:602
Routines for adding user scores to emails.
An Email conversation.
Definition: thread.h:34
char * personal
Real name of address.
Definition: address.h:36
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:112
Sort by the email&#39;s From field.
Definition: sort.h:54
Sort by the size of the email.
Definition: sort.h:51
SortType
Methods for sorting.
Definition: sort.h:48
static int compare_size(const void *a, const void *b)
Compare the size of two emails - Implements sort_t.
Definition: sort.c:100
#define mutt_error(...)
Definition: logging.h:84
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:628
int index
The absolute (unsorted) message number.
Definition: email.h:87
static int compare_to(const void *a, const void *b)
Compare the &#39;to&#39; fields of two emails - Implements sort_t.
Definition: sort.c:175
WHERE bool C_Score
Config: Use message scoring.
Definition: globals.h:257
int perform_auxsort(int retval, const void *a, const void *b)
Compare two emails using the auxiliary sort method.
Definition: sort.c:65
static int compare_subject(const void *a, const void *b)
Compare the subject of two emails - Implements sort_t.
Definition: sort.c:124
WHERE bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition: options.h:52
Handling of global boolean variables.
Sort by when the message were delivered locally.
Definition: sort.h:57
bool mutt_buffer_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:252
static int compare_score(const void *a, const void *b)
Compare two emails using their scores - Implements sort_t.
Definition: sort.c:88
char * pattern
Limit pattern string.
Definition: context.h:39
Sort by email threads.
Definition: sort.h:56
char * x_label
X-Label.
Definition: envelope.h:72
Sort by the order the messages appear in the mailbox.
Definition: sort.h:55
static sort_t * AuxSort
Definition: sort.c:54
#define SORT_MASK
Mask for the sort id.
Definition: sort.h:85
time_t received
Time when the message was placed in the mailbox.
Definition: email.h:84
Usenet network mailbox type; talk to an NNTP server.
int msgno
Number displayed to the user.
Definition: email.h:88
Sort by the email&#39;s score.
Definition: sort.h:59
const char * mutt_get_name(const struct Address *a)
Pick the best name to display from an address.
Definition: sort.c:155
static int compare_from(const void *a, const void *b)
Compare the &#39;from&#39; fields of two emails - Implements sort_t.
Definition: sort.c:191