NeoMutt  2024-11-14-138-ge5ca67
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sort.c
Go to the documentation of this file.
1
33#include "config.h"
34#include <stdbool.h>
35#include <stdlib.h>
36#include "mutt/lib.h"
37#include "address/lib.h"
38#include "config/lib.h"
39#include "core/lib.h"
40#include "alias/lib.h"
41#include "sort.h"
42#include "nntp/lib.h"
43#include "body.h"
44#include "email.h"
45#include "envelope.h"
46#include "mutt_logging.h"
47#include "mutt_thread.h"
48#include "mview.h"
49#include "mx.h"
50#include "score.h"
51
52extern bool OptNeedRescore;
53extern bool OptNeedResort;
54extern bool OptResortInit;
55
60{
62 short sort;
63 short sort_aux;
64};
65
69static int email_sort_shim(const void *a, const void *b, void *sdata)
70{
71 const struct Email *ea = *(struct Email const *const *) a;
72 const struct Email *eb = *(struct Email const *const *) b;
73 const struct EmailCompare *cmp = sdata;
74 return mutt_compare_emails(ea, eb, cmp->type, cmp->sort, cmp->sort_aux);
75}
76
80static int email_sort_score(const struct Email *a, const struct Email *b, bool reverse)
81{
82 int result = mutt_numeric_cmp(b->score, a->score); /* note that this is reverse */
83 return reverse ? -result : result;
84}
85
89static int email_sort_size(const struct Email *a, const struct Email *b, bool reverse)
90{
91 int result = mutt_numeric_cmp(a->body->length, b->body->length);
92 return reverse ? -result : result;
93}
94
98static int email_sort_date(const struct Email *a, const struct Email *b, bool reverse)
99{
100 int result = mutt_numeric_cmp(a->date_sent, b->date_sent);
101 return reverse ? -result : result;
102}
103
107static int email_sort_subject(const struct Email *a, const struct Email *b, bool reverse)
108{
109 int rc;
110
111 if (!a->env->real_subj)
112 {
113 if (!b->env->real_subj)
114 rc = email_sort_date(a, b, false);
115 else
116 rc = -1;
117 }
118 else if (!b->env->real_subj)
119 {
120 rc = 1;
121 }
122 else
123 {
124 rc = mutt_istr_cmp(a->env->real_subj, b->env->real_subj);
125 }
126 return reverse ? -rc : rc;
127}
128
139const char *mutt_get_name(const struct Address *a)
140{
141 struct Address *ali = NULL;
142
143 if (a)
144 {
145 const bool c_reverse_alias = cs_subset_bool(NeoMutt->sub, "reverse_alias");
146 if (c_reverse_alias && (ali = alias_reverse_lookup(a)) && ali->personal)
147 return buf_string(ali->personal);
148 if (a->personal)
149 return buf_string(a->personal);
150 if (a->mailbox)
151 return mutt_addr_for_display(a);
152 }
153 /* don't return NULL to avoid segfault when printing/comparing */
154 return "";
155}
156
160static int email_sort_to(const struct Email *a, const struct Email *b, bool reverse)
161{
162 char fa[128] = { 0 };
163
164 mutt_str_copy(fa, mutt_get_name(TAILQ_FIRST(&a->env->to)), sizeof(fa));
165 const char *fb = mutt_get_name(TAILQ_FIRST(&b->env->to));
166 int result = mutt_istrn_cmp(fa, fb, sizeof(fa));
167 return reverse ? -result : result;
168}
169
173static int email_sort_from(const struct Email *a, const struct Email *b, bool reverse)
174{
175 char fa[128] = { 0 };
176
177 mutt_str_copy(fa, mutt_get_name(TAILQ_FIRST(&a->env->from)), sizeof(fa));
178 const char *fb = mutt_get_name(TAILQ_FIRST(&b->env->from));
179 int result = mutt_istrn_cmp(fa, fb, sizeof(fa));
180 return reverse ? -result : result;
181}
182
186static int email_sort_date_received(const struct Email *a, const struct Email *b, bool reverse)
187{
188 int result = mutt_numeric_cmp(a->received, b->received);
189 return reverse ? -result : result;
190}
191
195static int email_sort_unsorted(const struct Email *a, const struct Email *b, bool reverse)
196{
197 int result = mutt_numeric_cmp(a->index, b->index);
198 return reverse ? -result : result;
199}
200
204static int email_sort_spam(const struct Email *a, const struct Email *b, bool reverse)
205{
206 char *aptr = NULL, *bptr = NULL;
207 int ahas, bhas;
208 int result = 0;
209 double difference;
210
211 /* Firstly, require spam attributes for both msgs */
212 /* to compare. Determine which msgs have one. */
213 ahas = a->env && !buf_is_empty(&a->env->spam);
214 bhas = b->env && !buf_is_empty(&b->env->spam);
215
216 /* If one msg has spam attr but other does not, sort the one with first. */
217 if (ahas && !bhas)
218 return reverse ? -1 : 1;
219 if (!ahas && bhas)
220 return reverse ? 1 : -1;
221
222 /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
223 if (!ahas && !bhas)
224 return 0;
225
226 /* Both have spam attrs. */
227
228 /* preliminary numeric examination */
229 difference = (strtod(a->env->spam.data, &aptr) - strtod(b->env->spam.data, &bptr));
230
231 /* map double into comparison (-1, 0, or 1) */
232 result = ((difference < 0.0) ? -1 : (difference > 0.0) ? 1 : 0);
233
234 /* If either aptr or bptr is equal to data, there is no numeric */
235 /* value for that spam attribute. In this case, compare lexically. */
236 if ((aptr == a->env->spam.data) || (bptr == b->env->spam.data))
237 {
238 result = mutt_str_cmp(aptr, bptr);
239 return reverse ? -result : result;
240 }
241
242 /* Otherwise, we have numeric value for both attrs. If these values */
243 /* are equal, then we first fall back upon string comparison, then */
244 /* upon auxiliary sort. */
245 if (result == 0)
246 result = mutt_str_cmp(aptr, bptr);
247 return reverse ? -result : result;
248}
249
253static int email_sort_label(const struct Email *a, const struct Email *b, bool reverse)
254{
255 int ahas, bhas, result = 0;
256
257 /* As with email_sort_spam, not all messages will have the x-label
258 * property. Blank X-Labels are treated as null in the index
259 * display, so we'll consider them as null for sort, too. */
260 ahas = a->env && a->env->x_label && *(a->env->x_label);
261 bhas = b->env && b->env->x_label && *(b->env->x_label);
262
263 /* First we bias toward a message with a label, if the other does not. */
264 if (ahas && !bhas)
265 return reverse ? 1 : -1;
266 if (!ahas && bhas)
267 return reverse ? -1 : 1;
268
269 /* If neither has a label, use aux sort. */
270 if (!ahas && !bhas)
271 return 0;
272
273 /* If both have a label, we just do a lexical compare. */
274 result = mutt_istr_cmp(a->env->x_label, b->env->x_label);
275 return reverse ? -result : result;
276}
277
285{
286 switch (method)
287 {
288 case EMAIL_SORT_DATE:
289 return email_sort_date;
292 case EMAIL_SORT_FROM:
293 return email_sort_from;
294 case EMAIL_SORT_LABEL:
295 return email_sort_label;
296 case EMAIL_SORT_SCORE:
297 return email_sort_score;
298 case EMAIL_SORT_SIZE:
299 return email_sort_size;
300 case EMAIL_SORT_SPAM:
301 return email_sort_spam;
303 return email_sort_subject;
304 case EMAIL_SORT_TO:
305 return email_sort_to;
307 if (type == MUTT_NNTP)
308 return nntp_sort_unsorted;
309 else
310 return email_sort_unsorted;
311 default:
312 mutt_error(_("Could not find sorting function [report this bug]"));
313 return NULL;
314 }
315 /* not reached */
316}
317
329int mutt_compare_emails(const struct Email *a, const struct Email *b,
330 enum MailboxType type, short sort, short sort_aux)
331{
332 sort_email_t func = get_sort_func(sort & SORT_MASK, type);
333 int rc = func(a, b, (sort & SORT_REVERSE) != 0);
334 if (rc == 0)
335 {
336 func = get_sort_func(sort_aux & SORT_MASK, type);
337 rc = func(a, b, (sort_aux & SORT_REVERSE) != 0);
338 }
339 if (rc == 0)
340 {
341 /* Fallback of last resort to preserve stable order; will only
342 * return 0 if a and b have the same index, which is probably a
343 * bug in the code. */
344 func = email_sort_unsorted;
345 rc = func(a, b, false);
346 }
347 return rc;
348}
349
355void mutt_sort_headers(struct MailboxView *mv, bool init)
356{
357 if (!mv)
358 return;
359
360 struct Mailbox *m = mv->mailbox;
361 if (!m || !m->emails[0])
362 return;
363
364 OptNeedResort = false;
365
366 if (m->msg_count == 0)
367 {
368 /* this function gets called by mutt_sync_mailbox(), which may have just
369 * deleted all the messages. the virtual message numbers are not updated
370 * in that routine, so we must make sure to zero the vcount member. */
371 m->vcount = 0;
373 mv->vsize = 0;
374 return; /* nothing to do! */
375 }
376
377 if (m->verbose)
378 mutt_message(_("Sorting mailbox..."));
379
380 const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
381 if (OptNeedRescore && c_score)
382 {
383 for (int i = 0; i < m->msg_count; i++)
384 {
385 struct Email *e = m->emails[i];
386 if (!e)
387 break;
388 mutt_score_message(m, e, true);
389 }
390 }
391 OptNeedRescore = false;
392
393 if (OptResortInit)
394 {
395 OptResortInit = false;
396 init = true;
397 }
398
399 if (init)
401
402 const bool threaded = mutt_using_threads();
403 if (threaded)
404 {
405 mutt_sort_threads(mv->threads, init);
406 }
407 else
408 {
409 struct EmailCompare cmp = { 0 };
410 cmp.type = mx_type(m);
411 cmp.sort = cs_subset_sort(NeoMutt->sub, "sort");
412 cmp.sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
413 mutt_qsort_r((void *) m->emails, m->msg_count, sizeof(struct Email *),
414 email_sort_shim, &cmp);
415 }
416
417 /* adjust the virtual message numbers */
418 m->vcount = 0;
419 for (int i = 0; i < m->msg_count; i++)
420 {
421 struct Email *e_cur = m->emails[i];
422 if (!e_cur)
423 break;
424
425 if ((e_cur->vnum != -1) || (e_cur->collapsed && e_cur->visible))
426 {
427 e_cur->vnum = m->vcount;
428 m->v2r[m->vcount] = i;
429 m->vcount++;
430 }
431 e_cur->msgno = i;
432 }
433
434 /* re-collapse threads marked as collapsed */
435 if (threaded)
436 {
438 mv->vsize = mutt_set_vnum(m);
439 }
440
441 if (m->verbose)
443}
444
450{
451 if (!m)
452 return;
453
454 struct EmailCompare cmp = { 0 };
455 cmp.type = mx_type(m);
457 mutt_qsort_r((void *) m->emails, m->msg_count, sizeof(struct Email *),
458 email_sort_shim, &cmp);
459}
const char * mutt_addr_for_display(const struct Address *a)
Convert an Address for display purposes.
Definition: address.c:1012
Email Address Handling.
Email Aliases.
struct Address * alias_reverse_lookup(const struct Address *addr)
Does the user have an alias for the given address.
Definition: reverse.c:105
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:266
Convenience wrapper for the config headers.
#define SORT_MASK
Mask for the sort id.
Definition: sort.h:38
#define mutt_numeric_cmp(a, b)
Definition: sort.h:26
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort.h:39
Convenience wrapper for the core headers.
MailboxType
Supported mailbox formats.
Definition: mailbox.h:41
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition: mailbox.h:49
const char * mutt_get_name(const struct Address *a)
Pick the best name to display from an address.
Definition: sort.c:139
bool OptNeedRescore
(pseudo) set when the 'score' command is used
Definition: globals.c:65
bool OptResortInit
(pseudo) used to force the next resort to be from scratch
Definition: globals.c:71
static sort_email_t get_sort_func(enum EmailSortType method, enum MailboxType type)
Get the sort function for a given sort id.
Definition: sort.c:284
void mutt_sort_headers(struct MailboxView *mv, bool init)
Sort emails by their headers.
Definition: sort.c:355
bool OptNeedResort
(pseudo) used to force a re-sort
Definition: globals.c:66
void mutt_sort_unsorted(struct Mailbox *m)
Sort emails by their disk order.
Definition: sort.c:449
int(* sort_email_t)(const struct Email *a, const struct Email *b, bool reverse)
Definition: sort.h:47
EmailSortType
Methods for sorting Emails.
Definition: sort.h:53
@ EMAIL_SORT_LABEL
Sort by the emails label.
Definition: sort.h:57
@ EMAIL_SORT_DATE_RECEIVED
Sort by when the message were delivered locally.
Definition: sort.h:55
@ EMAIL_SORT_SPAM
Sort by the email's spam score.
Definition: sort.h:60
@ EMAIL_SORT_SCORE
Sort by the email's score.
Definition: sort.h:58
@ EMAIL_SORT_DATE
Sort by the date the email was sent.
Definition: sort.h:54
@ EMAIL_SORT_SUBJECT
Sort by the email's subject.
Definition: sort.h:61
@ EMAIL_SORT_FROM
Sort by the email's From field.
Definition: sort.h:56
@ EMAIL_SORT_UNSORTED
Sort by the order the messages appear in the mailbox.
Definition: sort.h:64
@ EMAIL_SORT_SIZE
Sort by the size of the email.
Definition: sort.h:59
@ EMAIL_SORT_TO
Sort by the email's To field.
Definition: sort.h:63
Representation of an email.
Representation of an email header (envelope)
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_message(...)
Definition: logging2.h:91
int mutt_compare_emails(const struct Email *a, const struct Email *b, enum MailboxType type, short sort, short sort_aux)
Compare two emails using up to two sort methods -.
Definition: sort.c:329
static int email_sort_shim(const void *a, const void *b, void *sdata)
Helper to sort emails - Implements sort_t -.
Definition: sort.c:69
static int email_sort_date(const struct Email *a, const struct Email *b, bool reverse)
Compare the sent date of two emails - Implements sort_email_t -.
Definition: sort.c:98
int nntp_sort_unsorted(const struct Email *a, const struct Email *b, bool reverse)
Restore the 'unsorted' order of emails - Implements sort_email_t -.
Definition: nntp.c:2356
static int email_sort_spam(const struct Email *a, const struct Email *b, bool reverse)
Compare the spam values of two emails - Implements sort_email_t -.
Definition: sort.c:204
static int email_sort_size(const struct Email *a, const struct Email *b, bool reverse)
Compare the size of two emails - Implements sort_email_t -.
Definition: sort.c:89
static int email_sort_score(const struct Email *a, const struct Email *b, bool reverse)
Compare two emails using their scores - Implements sort_email_t -.
Definition: sort.c:80
static int email_sort_to(const struct Email *a, const struct Email *b, bool reverse)
Compare the 'to' fields of two emails - Implements sort_email_t -.
Definition: sort.c:160
static int email_sort_subject(const struct Email *a, const struct Email *b, bool reverse)
Compare the subject of two emails - Implements sort_email_t -.
Definition: sort.c:107
static int email_sort_from(const struct Email *a, const struct Email *b, bool reverse)
Compare the 'from' fields of two emails - Implements sort_email_t -.
Definition: sort.c:173
static int email_sort_label(const struct Email *a, const struct Email *b, bool reverse)
Compare the labels of two emails - Implements sort_email_t -.
Definition: sort.c:253
static int email_sort_date_received(const struct Email *a, const struct Email *b, bool reverse)
Compare the date received of two emails - Implements sort_email_t -.
Definition: sort.c:186
static int email_sort_unsorted(const struct Email *a, const struct Email *b, bool reverse)
Restore the 'unsorted' order of emails - Implements sort_email_t -.
Definition: sort.c:195
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:399
int mutt_istrn_cmp(const char *a, const char *b, size_t num)
Compare two strings ignoring case (to a maximum), safely.
Definition: string.c:439
int mutt_istr_cmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:412
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:581
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
NeoMutt Logging.
void mutt_clear_threads(struct ThreadsContext *tctx)
Clear the threading of message in a mailbox.
Definition: mutt_thread.c:719
void mutt_thread_collapse_collapsed(struct ThreadsContext *tctx)
Re-collapse threads marked as collapsed.
Definition: mutt_thread.c:1770
void mutt_sort_threads(struct ThreadsContext *tctx, bool init)
Sort email threads.
Definition: mutt_thread.c:1033
off_t mutt_set_vnum(struct Mailbox *m)
Set the virtual index number of all the messages in a mailbox.
Definition: mutt_thread.c:1406
Create/manipulate threading in emails.
#define mutt_using_threads()
Definition: mutt_thread.h:114
View of a Mailbox.
enum MailboxType mx_type(struct Mailbox *m)
Return the type of the Mailbox.
Definition: mx.c:1796
API for mailboxes.
Usenet network mailbox type; talk to an NNTP server.
void mutt_qsort_r(void *base, size_t nmemb, size_t size, sort_t compar, void *sdata)
Sort an array, where the comparator has access to opaque data rather than requiring global variables.
Definition: qsort_r.c:67
#define TAILQ_FIRST(head)
Definition: queue.h:741
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:165
Routines for adding user scores to emails.
Convenience wrapper for the send headers.
Sidebar sorting functions.
An email address.
Definition: address.h:36
struct Buffer * personal
Real name of address.
Definition: address.h:37
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
char * data
Pointer to data.
Definition: buffer.h:37
Context for email_sort_shim()
Definition: sort.c:60
short sort_aux
Secondary sort.
Definition: sort.c:63
short sort
Primary sort.
Definition: sort.c:62
enum MailboxType type
Current mailbox type.
Definition: sort.c:61
The envelope/body of an email.
Definition: email.h:39
bool visible
Is this message part of the view?
Definition: email.h:121
struct Envelope * env
Envelope information.
Definition: email.h:68
bool collapsed
Is this message part of a collapsed thread?
Definition: email.h:120
struct Body * body
List of MIME parts.
Definition: email.h:69
bool threaded
Used for threading.
Definition: email.h:108
time_t date_sent
Time when the message was sent (UTC)
Definition: email.h:60
int vnum
Virtual message number.
Definition: email.h:114
int score
Message score.
Definition: email.h:113
int msgno
Number displayed to the user.
Definition: email.h:111
int index
The absolute (unsorted) message number.
Definition: email.h:110
time_t received
Time when the message was placed in the mailbox.
Definition: email.h:61
struct AddressList to
Email's 'To' list.
Definition: envelope.h:60
struct Buffer spam
Spam header.
Definition: envelope.h:82
char * x_label
X-Label.
Definition: envelope.h:76
char *const real_subj
Offset of the real subject.
Definition: envelope.h:71
struct AddressList from
Email's 'From' list.
Definition: envelope.h:59
View of a Mailbox.
Definition: mview.h:40
off_t vsize
Size (in bytes) of the messages shown.
Definition: mview.h:41
struct ThreadsContext * threads
Threads context.
Definition: mview.h:44
struct Mailbox * mailbox
Current Mailbox.
Definition: mview.h:51
A mailbox.
Definition: mailbox.h:79
int vcount
The number of virtual messages.
Definition: mailbox.h:99
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:98
int msg_count
Total number of messages.
Definition: mailbox.h:88
struct Email ** emails
Array of Emails.
Definition: mailbox.h:96
bool verbose
Display status messages?
Definition: mailbox.h:117
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46