NeoMutt  2025-12-11-1009-ga75d9e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sort.c File Reference

Email sorting methods. More...

#include "config.h"
#include <stdbool.h>
#include <stdlib.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "alias/lib.h"
#include "gui/lib.h"
#include "sort.h"
#include "nntp/lib.h"
#include "body.h"
#include "email.h"
#include "envelope.h"
#include "mutt_logging.h"
#include "mx.h"
#include "score.h"
Include dependency graph for sort.c:

Go to the source code of this file.

Data Structures

struct  EmailCompare
 Context for email_sort_shim(). More...

Functions

static int email_sort_shim (const void *a, const void *b, void *sdata)
 Helper to sort emails - Implements sort_t -.
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 -.
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 -.
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 -.
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 -.
const char * mutt_get_name (const struct Address *a)
 Pick the best name to display from an address.
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 -.
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 -.
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 -.
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 -.
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 -.
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 -.
static sort_email_t get_sort_func (enum EmailSortType method, enum MailboxType type)
 Get the sort function for a given sort id.
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 -.
void mutt_sort_headers (struct MailboxView *mv, bool init)
 Sort emails by their headers.
void mutt_sort_unsorted (struct Mailbox *m)
 Sort emails by their disk order.

Variables

bool OptNeedRescore
 (pseudo) set when the 'score' command is used
bool OptNeedResort
 (pseudo) used to force a re-sort
bool OptResortInit
 (pseudo) used to force the next resort to be from scratch

Detailed Description

Email sorting methods.

Authors
  • Michael R. Elkins
  • Richard Russon
  • Pietro Cerutti
  • R Primus
  • Eric Blake

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file sort.c.

Function Documentation

◆ mutt_get_name()

const char * mutt_get_name ( const struct Address * a)

Pick the best name to display from an address.

Parameters
aAddress to use
Return values
ptrDisplay name

This function uses:

  1. Alias for email address
  2. Personal name
  3. Email address

Definition at line 138 of file sort.c.

139{
140 struct Address *ali = NULL;
141
142 if (a)
143 {
144 const bool c_reverse_alias = cs_subset_bool(NeoMutt->sub, "reverse_alias");
145 if (c_reverse_alias && (ali = alias_reverse_lookup(a)) && ali->personal)
146 return buf_string(ali->personal);
147 if (a->personal)
148 return buf_string(a->personal);
149 if (a->mailbox)
150 return mutt_addr_for_display(a);
151 }
152 /* don't return NULL to avoid segfault when printing/comparing */
153 return "";
154}
const char * mutt_addr_for_display(const struct Address *a)
Convert an Address for display purposes.
Definition address.c:1023
struct Address * alias_reverse_lookup(const struct Address *addr)
Does the user have an alias for the given address.
Definition reverse.c:110
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
An email address.
Definition address.h:35
struct Buffer * personal
Real name of address.
Definition address.h:36
struct Buffer * mailbox
Mailbox and host address.
Definition address.h:37
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_sort_func()

sort_email_t get_sort_func ( enum EmailSortType method,
enum MailboxType type )
static

Get the sort function for a given sort id.

Parameters
methodSort type, see EmailSortType
typeThe Mailbox type
Return values
ptrsort function - Implements sort_email_t

Definition at line 287 of file sort.c.

288{
289 switch (method)
290 {
291 case EMAIL_SORT_DATE:
292 return email_sort_date;
295 case EMAIL_SORT_FROM:
296 return email_sort_from;
297 case EMAIL_SORT_LABEL:
298 return email_sort_label;
299 case EMAIL_SORT_SCORE:
300 return email_sort_score;
301 case EMAIL_SORT_SIZE:
302 return email_sort_size;
303 case EMAIL_SORT_SPAM:
304 return email_sort_spam;
306 return email_sort_subject;
307 case EMAIL_SORT_TO:
308 return email_sort_to;
310 if (type == MUTT_NNTP)
311 return nntp_sort_unsorted;
312 else
313 return email_sort_unsorted;
314 default:
315 mutt_error(_("Could not find sorting function [report this bug]"));
316 return NULL;
317 }
318 /* not reached */
319}
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition mailbox.h:48
@ EMAIL_SORT_LABEL
Sort by the emails label.
Definition sort.h:57
@ EMAIL_SORT_DATE_RECEIVED
Sort by when the message was 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
#define mutt_error(...)
Definition logging2.h:94
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:97
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:2364
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:203
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:88
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:79
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:159
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:106
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:172
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:254
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:185
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:194
#define _(a)
Definition message.h:28
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mutt_sort_headers()

void mutt_sort_headers ( struct MailboxView * mv,
bool init )

Sort emails by their headers.

Parameters
mvMailbox View
initIf true, rebuild the thread

Definition at line 358 of file sort.c.

359{
360 if (!mv)
361 return;
362
363 struct Mailbox *m = mv->mailbox;
364 if (!m || !m->emails[0])
365 return;
366
367 OptNeedResort = false;
368
369 if (m->msg_count == 0)
370 {
371 /* this function gets called by mutt_sync_mailbox(), which may have just
372 * deleted all the messages. the virtual message numbers are not updated
373 * in that routine, so we must make sure to zero the vcount member. */
374 m->vcount = 0;
376 mv->vsize = 0;
377 return; /* nothing to do! */
378 }
379
380 if (m->verbose)
381 mutt_message(_("Sorting mailbox..."));
382
383 const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
384 if (OptNeedRescore && c_score)
385 {
386 for (int i = 0; i < m->msg_count; i++)
387 {
388 struct Email *e = m->emails[i];
389 if (!e)
390 break;
391 mutt_score_message(m, e, true);
392 }
393 }
394 OptNeedRescore = false;
395
396 if (OptResortInit)
397 {
398 OptResortInit = false;
399 init = true;
400 }
401
402 if (init)
404
405 const bool threaded = mutt_using_threads();
406 if (threaded)
407 {
408 mutt_sort_threads(mv->threads, init);
409 }
410 else
411 {
412 struct EmailCompare cmp = { 0 };
413 cmp.type = mx_type(m);
414 cmp.sort = cs_subset_sort(NeoMutt->sub, "sort");
415 cmp.sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
416 mutt_qsort_r((void *) m->emails, m->msg_count, sizeof(struct Email *),
417 email_sort_shim, &cmp);
418 }
419
420 /* adjust the virtual message numbers */
421 m->vcount = 0;
422 for (int i = 0; i < m->msg_count; i++)
423 {
424 struct Email *e_cur = m->emails[i];
425 if (!e_cur)
426 break;
427
428 if ((e_cur->vnum != -1) || (e_cur->collapsed && e_cur->visible))
429 {
430 e_cur->vnum = m->vcount;
431 m->v2r[m->vcount] = i;
432 m->vcount++;
433 }
434 e_cur->msgno = i;
435 }
436
437 /* re-collapse threads marked as collapsed */
438 if (threaded)
439 {
441 mv->vsize = mutt_set_vnum(m);
442 }
443
444 if (m->verbose)
446}
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition helpers.c:266
bool OptNeedRescore
(pseudo) set when the 'score' command is used
Definition globals.c:51
bool OptResortInit
(pseudo) used to force the next resort to be from scratch
Definition globals.c:56
bool OptNeedResort
(pseudo) used to force a re-sort
Definition globals.c:52
#define mutt_message(...)
Definition logging2.h:93
static int email_sort_shim(const void *a, const void *b, void *sdata)
Helper to sort emails - Implements sort_t -.
Definition sort.c:68
void mutt_clear_threads(struct ThreadsContext *tctx)
Clear the threading of message in a mailbox.
Definition thread.c:810
void mutt_thread_collapse_collapsed(struct ThreadsContext *tctx)
Re-collapse threads marked as collapsed.
Definition thread.c:1891
void mutt_sort_threads(struct ThreadsContext *tctx, bool init)
Sort email threads.
Definition thread.c:1130
off_t mutt_set_vnum(struct Mailbox *m)
Set the virtual index number of all the messages in a mailbox.
Definition thread.c:1515
#define mutt_using_threads()
Definition thread.h:119
void mutt_clear_error(void)
Clear the message line (bottom line of screen).
enum MailboxType mx_type(struct Mailbox *m)
Return the type of the Mailbox.
Definition mx.c:1813
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:72
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition score.c:246
Context for email_sort_shim().
Definition sort.c:59
short sort_aux
Secondary sort.
Definition sort.c:62
short sort
Primary sort.
Definition sort.c:61
enum MailboxType type
Current mailbox type.
Definition sort.c:60
The envelope/body of an email.
Definition email.h:39
bool visible
Is this message part of the view?
Definition email.h:121
bool collapsed
Is this message part of a collapsed thread?
Definition email.h:120
bool threaded
Used for threading.
Definition email.h:108
int vnum
Virtual message number.
Definition email.h:114
int msgno
Number displayed to the user.
Definition email.h:111
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:81
int vcount
The number of virtual messages.
Definition mailbox.h:101
int * v2r
Mapping from virtual to real msgno.
Definition mailbox.h:100
int msg_count
Total number of messages.
Definition mailbox.h:90
struct Email ** emails
Array of Emails.
Definition mailbox.h:98
bool verbose
Display status messages?
Definition mailbox.h:119
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mutt_sort_unsorted()

void mutt_sort_unsorted ( struct Mailbox * m)

Sort emails by their disk order.

Parameters
mMailbox

Definition at line 452 of file sort.c.

453{
454 if (!m)
455 return;
456
457 struct EmailCompare cmp = { 0 };
458 cmp.type = mx_type(m);
460 mutt_qsort_r((void *) m->emails, m->msg_count, sizeof(struct Email *),
461 email_sort_shim, &cmp);
462}
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ OptNeedRescore

bool OptNeedRescore
extern

(pseudo) set when the 'score' command is used

Definition at line 51 of file globals.c.

◆ OptNeedResort

bool OptNeedResort
extern

(pseudo) used to force a re-sort

Definition at line 52 of file globals.c.

◆ OptResortInit

bool OptResortInit
extern

(pseudo) used to force the next resort to be from scratch

Definition at line 56 of file globals.c.