NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sort.c File Reference

Assorted 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 "email/lib.h"
#include "core/lib.h"
#include "alias/lib.h"
#include "sort.h"
#include "globals.h"
#include "mutt_logging.h"
#include "mutt_thread.h"
#include "mview.h"
#include "mx.h"
#include "score.h"
#include "nntp/lib.h"
+ Include dependency graph for sort.c:

Go to the source code of this file.

Data Structures

struct  EmailCompare
 Context for compare_email_shim() More...
 

Functions

static int compare_email_shim (const void *a, const void *b, void *sdata)
 Helper to sort emails - Implements sort_t -.
 
static int compare_score (const struct Email *a, const struct Email *b, bool reverse)
 Compare two emails using their scores - Implements sort_mail_t -.
 
static int compare_size (const struct Email *a, const struct Email *b, bool reverse)
 Compare the size of two emails - Implements sort_mail_t -.
 
static int compare_date_sent (const struct Email *a, const struct Email *b, bool reverse)
 Compare the sent date of two emails - Implements sort_mail_t -.
 
static int compare_subject (const struct Email *a, const struct Email *b, bool reverse)
 Compare the subject of two emails - Implements sort_mail_t -.
 
const char * mutt_get_name (const struct Address *a)
 Pick the best name to display from an address.
 
static int compare_to (const struct Email *a, const struct Email *b, bool reverse)
 Compare the 'to' fields of two emails - Implements sort_mail_t -.
 
static int compare_from (const struct Email *a, const struct Email *b, bool reverse)
 Compare the 'from' fields of two emails - Implements sort_mail_t -.
 
static int compare_date_received (const struct Email *a, const struct Email *b, bool reverse)
 Compare the date received of two emails - Implements sort_mail_t -.
 
static int compare_order (const struct Email *a, const struct Email *b, bool reverse)
 Restore the 'unsorted' order of emails - Implements sort_mail_t -.
 
static int compare_spam (const struct Email *a, const struct Email *b, bool reverse)
 Compare the spam values of two emails - Implements sort_mail_t -.
 
static int compare_label (const struct Email *a, const struct Email *b, bool reverse)
 Compare the labels of two emails - Implements sort_mail_t -.
 
static sort_mail_t get_sort_func (enum SortType 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.
 

Detailed Description

Assorted sorting methods.

Authors
  • Michael R. Elkins
  • Pietro Cerutti
  • R Primus

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 134 of file sort.c.

135{
136 struct Address *ali = NULL;
137
138 if (a)
139 {
140 const bool c_reverse_alias = cs_subset_bool(NeoMutt->sub, "reverse_alias");
141 if (c_reverse_alias && (ali = alias_reverse_lookup(a)) && ali->personal)
142 return buf_string(ali->personal);
143 if (a->personal)
144 return buf_string(a->personal);
145 if (a->mailbox)
146 return mutt_addr_for_display(a);
147 }
148 /* don't return NULL to avoid segfault when printing/comparing */
149 return "";
150}
const char * mutt_addr_for_display(const struct Address *a)
Convert an Address for display purposes.
Definition: address.c:1009
struct Address * alias_reverse_lookup(const struct Address *addr)
Does the user have an alias for the given address.
Definition: reverse.c:105
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
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
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_sort_func()

static sort_mail_t get_sort_func ( enum SortType  method,
enum MailboxType  type 
)
static

Get the sort function for a given sort id.

Parameters
methodSort type, see SortType
typeThe Mailbox type
Return values
ptrsort function - Implements sort_mail_t

Definition at line 279 of file sort.c.

280{
281 switch (method)
282 {
283 case SORT_DATE:
284 return compare_date_sent;
285 case SORT_FROM:
286 return compare_from;
287 case SORT_LABEL:
288 return compare_label;
289 case SORT_ORDER:
290#ifdef USE_NNTP
291 if (type == MUTT_NNTP)
292 return nntp_compare_order;
293 else
294#endif
295 return compare_order;
296 case SORT_RECEIVED:
298 case SORT_SCORE:
299 return compare_score;
300 case SORT_SIZE:
301 return compare_size;
302 case SORT_SPAM:
303 return compare_spam;
304 case SORT_SUBJECT:
305 return compare_subject;
306 case SORT_TO:
307 return compare_to;
308 default:
309 mutt_error(_("Could not find sorting function [report this bug]"));
310 return NULL;
311 }
312 /* not reached */
313}
#define mutt_error(...)
Definition: logging2.h:92
static int compare_to(const struct Email *a, const struct Email *b, bool reverse)
Compare the 'to' fields of two emails - Implements sort_mail_t -.
Definition: sort.c:155
int nntp_compare_order(const struct Email *a, const struct Email *b, bool reverse)
Sort to mailbox order - Implements sort_mail_t -.
Definition: nntp.c:2305
static int compare_label(const struct Email *a, const struct Email *b, bool reverse)
Compare the labels of two emails - Implements sort_mail_t -.
Definition: sort.c:248
static int compare_spam(const struct Email *a, const struct Email *b, bool reverse)
Compare the spam values of two emails - Implements sort_mail_t -.
Definition: sort.c:199
static int compare_order(const struct Email *a, const struct Email *b, bool reverse)
Restore the 'unsorted' order of emails - Implements sort_mail_t -.
Definition: sort.c:190
static int compare_date_sent(const struct Email *a, const struct Email *b, bool reverse)
Compare the sent date of two emails - Implements sort_mail_t -.
Definition: sort.c:93
static int compare_subject(const struct Email *a, const struct Email *b, bool reverse)
Compare the subject of two emails - Implements sort_mail_t -.
Definition: sort.c:102
static int compare_score(const struct Email *a, const struct Email *b, bool reverse)
Compare two emails using their scores - Implements sort_mail_t -.
Definition: sort.c:75
static int compare_from(const struct Email *a, const struct Email *b, bool reverse)
Compare the 'from' fields of two emails - Implements sort_mail_t -.
Definition: sort.c:168
static int compare_size(const struct Email *a, const struct Email *b, bool reverse)
Compare the size of two emails - Implements sort_mail_t -.
Definition: sort.c:84
static int compare_date_received(const struct Email *a, const struct Email *b, bool reverse)
Compare the date received of two emails - Implements sort_mail_t -.
Definition: sort.c:181
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition: mailbox.h:49
#define _(a)
Definition: message.h:28
@ SORT_SUBJECT
Sort by the email's subject.
Definition: sort2.h:42
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:44
@ SORT_SPAM
Sort by the email's spam score.
Definition: sort2.h:53
@ SORT_LABEL
Sort by the emails label.
Definition: sort2.h:58
@ SORT_FROM
Sort by the email's From field.
Definition: sort2.h:43
@ SORT_SIZE
Sort by the size of the email.
Definition: sort2.h:40
@ SORT_RECEIVED
Sort by when the message were delivered locally.
Definition: sort2.h:46
@ SORT_TO
Sort by the email's To field.
Definition: sort2.h:47
@ SORT_DATE
Sort by the date the email was sent.
Definition: sort2.h:39
@ SORT_SCORE
Sort by the email's score.
Definition: sort2.h:48
+ 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 352 of file sort.c.

353{
354 if (!mv)
355 return;
356
357 struct Mailbox *m = mv->mailbox;
358 if (!m || !m->emails[0])
359 return;
360
361 OptNeedResort = false;
362
363 if (m->msg_count == 0)
364 {
365 /* this function gets called by mutt_sync_mailbox(), which may have just
366 * deleted all the messages. the virtual message numbers are not updated
367 * in that routine, so we must make sure to zero the vcount member. */
368 m->vcount = 0;
370 mv->vsize = 0;
371 return; /* nothing to do! */
372 }
373
374 if (m->verbose)
375 mutt_message(_("Sorting mailbox..."));
376
377 const bool c_score = cs_subset_bool(NeoMutt->sub, "score");
378 if (OptNeedRescore && c_score)
379 {
380 for (int i = 0; i < m->msg_count; i++)
381 {
382 struct Email *e = m->emails[i];
383 if (!e)
384 break;
385 mutt_score_message(m, e, true);
386 }
387 }
388 OptNeedRescore = false;
389
390 if (OptResortInit)
391 {
392 OptResortInit = false;
393 init = true;
394 }
395
396 if (init)
398
399 const bool threaded = mutt_using_threads();
400 if (threaded)
401 {
402 mutt_sort_threads(mv->threads, init);
403 }
404 else
405 {
406 struct EmailCompare cmp = { 0 };
407 cmp.type = mx_type(m);
408 cmp.sort = cs_subset_sort(NeoMutt->sub, "sort");
409 cmp.sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
410 mutt_qsort_r((void *) m->emails, m->msg_count, sizeof(struct Email *),
411 compare_email_shim, &cmp);
412 }
413
414 /* adjust the virtual message numbers */
415 m->vcount = 0;
416 for (int i = 0; i < m->msg_count; i++)
417 {
418 struct Email *e_cur = m->emails[i];
419 if (!e_cur)
420 break;
421
422 if ((e_cur->vnum != -1) || (e_cur->collapsed && e_cur->visible))
423 {
424 e_cur->vnum = m->vcount;
425 m->v2r[m->vcount] = i;
426 m->vcount++;
427 }
428 e_cur->msgno = i;
429 }
430
431 /* re-collapse threads marked as collapsed */
432 if (threaded)
433 {
435 mv->vsize = mutt_set_vnum(m);
436 }
437
438 if (m->verbose)
440
441 return;
442}
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:267
bool OptNeedRescore
(pseudo) set when the 'score' command is used
Definition: globals.c:73
bool OptResortInit
(pseudo) used to force the next resort to be from scratch
Definition: globals.c:81
bool OptNeedResort
(pseudo) used to force a re-sort
Definition: globals.c:74
#define mutt_message(...)
Definition: logging2.h:91
static int compare_email_shim(const void *a, const void *b, void *sdata)
Helper to sort emails - Implements sort_t -.
Definition: sort.c:64
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
void mutt_clear_threads(struct ThreadsContext *tctx)
Clear the threading of message in a mailbox.
Definition: mutt_thread.c:717
void mutt_thread_collapse_collapsed(struct ThreadsContext *tctx)
Re-collapse threads marked as collapsed.
Definition: mutt_thread.c:1765
void mutt_sort_threads(struct ThreadsContext *tctx, bool init)
Sort email threads.
Definition: mutt_thread.c:1028
off_t mutt_set_vnum(struct Mailbox *m)
Set the virtual index number of all the messages in a mailbox.
Definition: mutt_thread.c:1401
#define mutt_using_threads()
Definition: mutt_thread.h:112
enum MailboxType mx_type(struct Mailbox *m)
Return the type of the Mailbox.
Definition: mx.c:1857
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:66
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:161
Context for compare_email_shim()
Definition: sort.c:55
short sort_aux
Secondary sort.
Definition: sort.c:58
short sort
Primary sort.
Definition: sort.c:57
enum MailboxType type
Current mailbox type.
Definition: sort.c:56
The envelope/body of an email.
Definition: email.h:37
bool visible
Is this message part of the view?
Definition: email.h:120
bool collapsed
Is this message part of a collapsed thread?
Definition: email.h:119
bool threaded
Used for threading.
Definition: email.h:107
int vnum
Virtual message number.
Definition: email.h:113
int msgno
Number displayed to the user.
Definition: email.h:110
off_t vsize
Size (in bytes) of the messages shown.
Definition: mview.h:40
struct ThreadsContext * threads
Threads context.
Definition: mview.h:43
struct Mailbox * mailbox
Current Mailbox.
Definition: mview.h:50
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:116
+ Here is the call graph for this function:
+ Here is the caller graph for this function: