NeoMutt  2020-09-25
Teaching an old dog new tricks
DOXYGEN
sort.h File Reference

Assorted sorting methods. More...

#include <stdbool.h>
#include <sys/types.h>
#include "config/lib.h"
#include "options.h"
#include "where.h"
+ Include dependency graph for sort.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define SORT_CODE(x)   ((OptAuxSort ? C_SortAux : C_Sort) & SORT_REVERSE) ? -(x) : x
 

Typedefs

typedef int(* sort_t) (const void *a, const void *b)
 Prototype for a function to compare two emails. More...
 

Functions

sort_t mutt_get_sort_func (enum SortType method)
 Get the sort function for a given sort id. More...
 
void mutt_sort_headers (struct Mailbox *m, struct ThreadsContext *threads, bool init, off_t *vsize)
 Sort emails by their headers. More...
 
int perform_auxsort (int retval, const void *a, const void *b)
 Compare two emails using the auxiliary sort method. More...
 
const char * mutt_get_name (const struct Address *a)
 Pick the best name to display from an address. More...
 

Variables

bool C_ReverseAlias
 Config: Display the alias in the index, rather than the message's sender. More...
 
WHERE short C_Sort
 Config: Sort method for the index. More...
 
WHERE short C_SortAux
 Config: Secondary sort method for the index. More...
 

Detailed Description

Assorted sorting methods.

Authors
  • Michael R. Elkins
  • Pietro Cerutti

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.h.

Macro Definition Documentation

◆ SORT_CODE

#define SORT_CODE (   x)    ((OptAuxSort ? C_SortAux : C_Sort) & SORT_REVERSE) ? -(x) : x

Definition at line 40 of file sort.h.

Typedef Documentation

◆ sort_t

typedef int(* sort_t) (const void *a, const void *b)

Prototype for a function to compare two emails.

Parameters
aFirst email
bSecond email
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 50 of file sort.h.

Function Documentation

◆ mutt_get_sort_func()

sort_t mutt_get_sort_func ( enum SortType  method)

Get the sort function for a given sort id.

Parameters
methodSort type, see SortType
Return values
ptrsort function - Implements sort_t

Definition at line 324 of file sort.c.

325 {
326  switch (method)
327  {
328  case SORT_DATE:
329  return compare_date_sent;
330  case SORT_FROM:
331  return compare_from;
332  case SORT_LABEL:
333  return compare_label;
334  case SORT_ORDER:
335 #ifdef USE_NNTP
336  if (Context && Context->mailbox && (Context->mailbox->type == MUTT_NNTP))
337  return nntp_compare_order;
338  else
339 #endif
340  return compare_order;
341  case SORT_RECEIVED:
342  return compare_date_received;
343  case SORT_SCORE:
344  return compare_score;
345  case SORT_SIZE:
346  return compare_size;
347  case SORT_SPAM:
348  return compare_spam;
349  case SORT_SUBJECT:
350  return compare_subject;
351  case SORT_TO:
352  return compare_to;
353  default:
354  return NULL;
355  }
356  /* not reached */
357 }
The "current" mailbox.
Definition: context.h:38
enum MailboxType type
Mailbox type.
Definition: mailbox.h:105
&#39;NNTP&#39; (Usenet) Mailbox type
Definition: mailbox.h:52
Sort by the email&#39;s From field.
Definition: sort2.h:50
Sort by the email&#39;s score.
Definition: sort2.h:55
Sort by the emails label.
Definition: sort2.h:65
static int compare_label(const void *a, const void *b)
Compare the labels of two emails - Implements sort_t.
Definition: sort.c:289
static int compare_date_sent(const void *a, const void *b)
Compare the sent date of two emails - Implements sort_t.
Definition: sort.c:114
static int compare_order(const void *a, const void *b)
Restore the &#39;unsorted&#39; order of emails - Implements sort_t.
Definition: sort.c:221
Sort by the size of the email.
Definition: sort2.h:47
struct Mailbox * mailbox
Definition: context.h:50
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:51
static int compare_date_received(const void *a, const void *b)
Compare the date received of two emails - Implements sort_t.
Definition: sort.c:209
int nntp_compare_order(const void *a, const void *b)
Sort to mailbox order - Implements sort_t.
Definition: nntp.c:2361
Sort by the email&#39;s spam score.
Definition: sort2.h:60
static int compare_spam(const void *a, const void *b)
Compare the spam values of two emails - Implements sort_t.
Definition: sort.c:233
Sort by the email&#39;s To field.
Definition: sort2.h:54
static int compare_size(const void *a, const void *b)
Compare the size of two emails - Implements sort_t.
Definition: sort.c:102
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:177
static int compare_subject(const void *a, const void *b)
Compare the subject of two emails - Implements sort_t.
Definition: sort.c:126
Sort by when the message were delivered locally.
Definition: sort2.h:53
Sort by the date the email was sent.
Definition: sort2.h:46
static int compare_score(const void *a, const void *b)
Compare two emails using their scores - Implements sort_t.
Definition: sort.c:90
Sort by the email&#39;s subject.
Definition: sort2.h:49
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:193
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_sort_headers()

void mutt_sort_headers ( struct Mailbox m,
struct ThreadsContext threads,
bool  init,
off_t *  vsize 
)

Sort emails by their headers.

Parameters
mMailbox
threadsThreads context
initIf true, rebuild the thread
[out]vsizeSize in bytes of the messages in view

Definition at line 366 of file sort.c.

368 {
369  if (!m || !m->emails[0])
370  return;
371 
372  sort_t sortfunc = NULL;
373 
374  OptNeedResort = false;
375 
376  if (m->msg_count == 0)
377  {
378  /* this function gets called by mutt_sync_mailbox(), which may have just
379  * deleted all the messages. the virtual message numbers are not updated
380  * in that routine, so we must make sure to zero the vcount member. */
381  m->vcount = 0;
382  mutt_clear_threads(threads);
383  *vsize = 0;
384  return; /* nothing to do! */
385  }
386 
387  if (m->verbose)
388  mutt_message(_("Sorting mailbox..."));
389 
390  if (OptNeedRescore && C_Score)
391  {
392  for (int i = 0; i < m->msg_count; i++)
393  {
394  struct Email *e = m->emails[i];
395  if (!e)
396  break;
397  mutt_score_message(m, e, true);
398  }
399  }
400  OptNeedRescore = false;
401 
402  if (OptResortInit)
403  {
404  OptResortInit = false;
405  init = true;
406  }
407 
408  if (init)
409  mutt_clear_threads(threads);
410 
411  if ((C_Sort & SORT_MASK) == SORT_THREADS)
412  {
413  AuxSort = NULL;
414  /* if $sort_aux changed after the mailbox is sorted, then all the
415  * subthreads need to be resorted */
416  if (OptSortSubthreads)
417  {
418  int i = C_Sort;
419  C_Sort = C_SortAux;
420  mutt_sort_subthreads(threads, true);
421  C_Sort = i;
422  OptSortSubthreads = false;
423  }
424  mutt_sort_threads(threads, init);
425  }
426  else if (!(sortfunc = mutt_get_sort_func(C_Sort & SORT_MASK)) ||
427  !(AuxSort = mutt_get_sort_func(C_SortAux & SORT_MASK)))
428  {
429  mutt_error(_("Could not find sorting function [report this bug]"));
430  return;
431  }
432  else
433  {
434  qsort((void *) m->emails, m->msg_count, sizeof(struct Email *), sortfunc);
435  }
436 
437  /* adjust the virtual message numbers */
438  m->vcount = 0;
439  for (int i = 0; i < m->msg_count; i++)
440  {
441  struct Email *e_cur = m->emails[i];
442  if (!e_cur)
443  break;
444 
445  if ((e_cur->vnum != -1) || (e_cur->collapsed && e_cur->visible))
446  {
447  e_cur->vnum = m->vcount;
448  m->v2r[m->vcount] = i;
449  m->vcount++;
450  }
451  e_cur->msgno = i;
452  }
453 
454  /* re-collapse threads marked as collapsed */
455  if ((C_Sort & SORT_MASK) == SORT_THREADS)
456  {
458  *vsize = mutt_set_vnum(m);
459  }
460 
461  if (m->verbose)
463 
464  return;
465 }
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
int msg_count
Total number of messages.
Definition: mailbox.h:91
The envelope/body of an email.
Definition: email.h:37
#define mutt_message(...)
Definition: logging.h:83
WHERE bool OptNeedResort
(pseudo) used to force a re-sort
Definition: options.h:43
void mutt_thread_collapse_collapsed(struct ThreadsContext *tctx)
re-collapse threads marked as collapsed
Definition: mutt_thread.c:1586
WHERE bool C_Score
Config: Use message scoring.
Definition: mutt_globals.h:158
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:173
#define _(a)
Definition: message.h:28
void mutt_sort_threads(struct ThreadsContext *tctx, bool init)
Sort email threads.
Definition: mutt_thread.c:870
WHERE bool OptNeedRescore
(pseudo) set when the &#39;score&#39; command is used
Definition: options.h:42
int vcount
The number of virtual messages.
Definition: mailbox.h:102
WHERE short C_Sort
Config: Sort method for the index.
Definition: sort.h:60
void mutt_clear_threads(struct ThreadsContext *tctx)
Clear the threading of message in a mailbox.
Definition: mutt_thread.c:637
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:113
int(* sort_t)(const void *a, const void *b)
Prototype for a function to compare two emails.
Definition: sort.h:50
bool visible
Is this message part of the view?
Definition: email.h:74
WHERE short C_SortAux
Config: Secondary sort method for the index.
Definition: sort.h:61
Sort by email threads.
Definition: sort2.h:52
static sort_t AuxSort
Definition: sort.c:54
bool collapsed
Is this message part of a collapsed thread?
Definition: email.h:73
bool verbose
Display status messages?
Definition: mailbox.h:118
int vnum
Virtual message number.
Definition: email.h:88
WHERE bool OptResortInit
(pseudo) used to force the next resort to be from scratch
Definition: options.h:51
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:101
#define mutt_error(...)
Definition: logging.h:84
void mutt_sort_subthreads(struct ThreadsContext *tctx, bool init)
Sort the children of a thread.
Definition: mutt_thread.c:687
WHERE bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition: options.h:54
off_t mutt_set_vnum(struct Mailbox *m)
Set the virtual index number of all the messages in a mailbox.
Definition: mutt_thread.c:1232
sort_t mutt_get_sort_func(enum SortType method)
Get the sort function for a given sort id.
Definition: sort.c:324
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:81
int msgno
Number displayed to the user.
Definition: email.h:87
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ perform_auxsort()

int perform_auxsort ( int  retval,
const void *  a,
const void *  b 
)

Compare two emails using the auxiliary sort method.

Parameters
retvalResult of normal sort method
aFirst email
bSecond email
Return values
-1a precedes b
0a and b are identical
1b precedes a

Definition at line 65 of file sort.c.

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  {
81  retval = (*((struct Email const *const *) a))->index -
82  (*((struct Email const *const *) b))->index;
83  }
84  return retval;
85 }
The envelope/body of an email.
Definition: email.h:37
WHERE bool OptAuxSort
(pseudo) using auxiliary sort function
Definition: options.h:35
static sort_t AuxSort
Definition: sort.c:54
int index
The absolute (unsorted) message number.
Definition: email.h:86
+ Here is the caller graph for this function:

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

158 {
159  struct Address *ali = NULL;
160 
161  if (a)
162  {
163  if (C_ReverseAlias && (ali = alias_reverse_lookup(a)) && ali->personal)
164  return ali->personal;
165  if (a->personal)
166  return a->personal;
167  if (a->mailbox)
168  return mutt_addr_for_display(a);
169  }
170  /* don't return NULL to avoid segfault when printing/comparing */
171  return "";
172 }
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
const char * mutt_addr_for_display(const struct Address *a)
Convert an Address for display purposes.
Definition: address.c:986
struct Address * alias_reverse_lookup(const struct Address *addr)
Does the user have an alias for the given address.
Definition: reverse.c:87
bool C_ReverseAlias
Config: Display the alias in the index, rather than the message&#39;s sender.
Definition: sort.c:51
char * personal
Real name of address.
Definition: address.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ C_ReverseAlias

bool C_ReverseAlias

Config: Display the alias in the index, rather than the message's sender.

Definition at line 51 of file sort.c.

◆ C_Sort

WHERE short C_Sort

Config: Sort method for the index.

Definition at line 60 of file sort.h.

◆ C_SortAux

WHERE short C_SortAux

Config: Secondary sort method for the index.

Definition at line 61 of file sort.h.