NeoMutt  2021-02-05-89-gabe350
Teaching an old dog new tricks
DOXYGEN
sort.h File Reference
#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 325 of file sort.c.

326 {
327  switch (method)
328  {
329  case SORT_DATE:
330  return compare_date_sent;
331  case SORT_FROM:
332  return compare_from;
333  case SORT_LABEL:
334  return compare_label;
335  case SORT_ORDER:
336 #ifdef USE_NNTP
338  return nntp_compare_order;
339  else
340 #endif
341  return compare_order;
342  case SORT_RECEIVED:
343  return compare_date_received;
344  case SORT_SCORE:
345  return compare_score;
346  case SORT_SIZE:
347  return compare_size;
348  case SORT_SPAM:
349  return compare_spam;
350  case SORT_SUBJECT:
351  return compare_subject;
352  case SORT_TO:
353  return compare_to;
354  default:
355  return NULL;
356  }
357  /* not reached */
358 }
+ 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 367 of file sort.c.

369 {
370  if (!m || !m->emails[0])
371  return;
372 
373  sort_t sortfunc = NULL;
374 
375  OptNeedResort = false;
376 
377  if (m->msg_count == 0)
378  {
379  /* this function gets called by mutt_sync_mailbox(), which may have just
380  * deleted all the messages. the virtual message numbers are not updated
381  * in that routine, so we must make sure to zero the vcount member. */
382  m->vcount = 0;
383  mutt_clear_threads(threads);
384  *vsize = 0;
385  return; /* nothing to do! */
386  }
387 
388  if (m->verbose)
389  mutt_message(_("Sorting mailbox..."));
390 
391  if (OptNeedRescore && C_Score)
392  {
393  for (int i = 0; i < m->msg_count; i++)
394  {
395  struct Email *e = m->emails[i];
396  if (!e)
397  break;
398  mutt_score_message(m, e, true);
399  }
400  }
401  OptNeedRescore = false;
402 
403  if (OptResortInit)
404  {
405  OptResortInit = false;
406  init = true;
407  }
408 
409  if (init)
410  mutt_clear_threads(threads);
411 
412  if ((C_Sort & SORT_MASK) == SORT_THREADS)
413  {
414  AuxSort = NULL;
415  /* if $sort_aux changed after the mailbox is sorted, then all the
416  * subthreads need to be resorted */
417  if (OptSortSubthreads)
418  {
419  int i = C_Sort;
420  C_Sort = C_SortAux;
421  mutt_sort_subthreads(threads, true);
422  C_Sort = i;
423  OptSortSubthreads = false;
424  }
425  mutt_sort_threads(threads, init);
426  }
427  else if (!(sortfunc = mutt_get_sort_func(C_Sort & SORT_MASK)) ||
429  {
430  mutt_error(_("Could not find sorting function [report this bug]"));
431  return;
432  }
433  else
434  {
435  qsort((void *) m->emails, m->msg_count, sizeof(struct Email *), sortfunc);
436  }
437 
438  /* adjust the virtual message numbers */
439  m->vcount = 0;
440  for (int i = 0; i < m->msg_count; i++)
441  {
442  struct Email *e_cur = m->emails[i];
443  if (!e_cur)
444  break;
445 
446  if ((e_cur->vnum != -1) || (e_cur->collapsed && e_cur->visible))
447  {
448  e_cur->vnum = m->vcount;
449  m->v2r[m->vcount] = i;
450  m->vcount++;
451  }
452  e_cur->msgno = i;
453  }
454 
455  /* re-collapse threads marked as collapsed */
456  if ((C_Sort & SORT_MASK) == SORT_THREADS)
457  {
459  *vsize = mutt_set_vnum(m);
460  }
461 
462  if (m->verbose)
464 
465  return;
466 }
+ 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 66 of file sort.c.

67 {
68  /* If the items compared equal by the main sort
69  * and we're not already doing an 'aux' sort... */
70  if ((retval == 0) && AuxSort && !OptAuxSort)
71  {
72  OptAuxSort = true;
73  retval = AuxSort(a, b);
74  OptAuxSort = false;
75  if (retval != 0)
76  return retval;
77  }
78  /* If the items still match, use their index positions
79  * to maintain a stable sort order */
80  if (retval == 0)
81  {
82  retval = (*((struct Email const *const *) a))->index -
83  (*((struct Email const *const *) b))->index;
84  }
85  return retval;
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 158 of file sort.c.

159 {
160  struct Address *ali = NULL;
161 
162  if (a)
163  {
164  if (C_ReverseAlias && (ali = alias_reverse_lookup(a)) && ali->personal)
165  return ali->personal;
166  if (a->personal)
167  return a->personal;
168  if (a->mailbox)
169  return mutt_addr_for_display(a);
170  }
171  /* don't return NULL to avoid segfault when printing/comparing */
172  return "";
173 }
+ 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 52 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.

compare_label
static int compare_label(const void *a, const void *b)
Compare the labels of two emails - Implements sort_t.
Definition: sort.c:290
compare_size
static int compare_size(const void *a, const void *b)
Compare the size of two emails - Implements sort_t.
Definition: sort.c:103
Email::msgno
int msgno
Number displayed to the user.
Definition: email.h:87
SORT_SPAM
@ SORT_SPAM
Sort by the email's spam score.
Definition: sort2.h:59
compare_order
static int compare_order(const void *a, const void *b)
Restore the 'unsorted' order of emails - Implements sort_t.
Definition: sort.c:222
compare_to
static int compare_to(const void *a, const void *b)
Compare the 'to' fields of two emails - Implements sort_t.
Definition: sort.c:178
SORT_TO
@ SORT_TO
Sort by the email's To field.
Definition: sort2.h:53
_
#define _(a)
Definition: message.h:28
SORT_THREADS
@ SORT_THREADS
Sort by email threads.
Definition: sort2.h:51
OptNeedResort
WHERE bool OptNeedResort
(pseudo) used to force a re-sort
Definition: options.h:43
Mailbox::v2r
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:101
Mailbox::emails
struct Email ** emails
Array of Emails.
Definition: mailbox.h:99
Address::personal
char * personal
Real name of address.
Definition: address.h:36
ctx_mailbox
struct Mailbox * ctx_mailbox(struct Context *ctx)
wrapper to get the mailbox in a Context, or NULL
Definition: context.c:429
OptAuxSort
WHERE bool OptAuxSort
(pseudo) using auxiliary sort function
Definition: options.h:35
Context
The "current" mailbox.
Definition: context.h:38
nntp_compare_order
int nntp_compare_order(const void *a, const void *b)
Sort to mailbox order - Implements sort_t.
Definition: nntp.c:2257
C_ReverseAlias
bool C_ReverseAlias
Config: Display the alias in the index, rather than the message's sender.
Definition: sort.c:52
mutt_sort_subthreads
void mutt_sort_subthreads(struct ThreadsContext *tctx, bool init)
Sort the children of a thread.
Definition: mutt_thread.c:687
mutt_score_message
void mutt_score_message(struct Mailbox *m, struct Email *e, bool upd_mbox)
Apply scoring to an email.
Definition: score.c:173
mutt_get_sort_func
sort_t mutt_get_sort_func(enum SortType method)
Get the sort function for a given sort id.
Definition: sort.c:325
SORT_MASK
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:80
sort_t
int(* sort_t)(const void *a, const void *b)
Prototype for a function to compare two emails.
Definition: sort.h:50
mutt_set_vnum
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
AuxSort
static sort_t AuxSort
Definition: sort.c:55
Mailbox::vcount
int vcount
The number of virtual messages.
Definition: mailbox.h:102
OptNeedRescore
WHERE bool OptNeedRescore
(pseudo) set when the 'score' command is used
Definition: options.h:42
C_Score
WHERE bool C_Score
Config: Use message scoring.
Definition: mutt_globals.h:159
compare_spam
static int compare_spam(const void *a, const void *b)
Compare the spam values of two emails - Implements sort_t.
Definition: sort.c:234
MUTT_NNTP
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition: mailbox.h:52
Mailbox::type
enum MailboxType type
Mailbox type.
Definition: mailbox.h:105
Email::visible
bool visible
Is this message part of the view?
Definition: email.h:74
SORT_SUBJECT
@ SORT_SUBJECT
Sort by the email's subject.
Definition: sort2.h:48
SORT_SCORE
@ SORT_SCORE
Sort by the email's score.
Definition: sort2.h:54
Mailbox::msg_count
int msg_count
Total number of messages.
Definition: mailbox.h:91
mutt_clear_error
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:113
Context::mailbox
struct Mailbox * mailbox
Definition: context.h:50
compare_subject
static int compare_subject(const void *a, const void *b)
Compare the subject of two emails - Implements sort_t.
Definition: sort.c:127
OptResortInit
WHERE bool OptResortInit
(pseudo) used to force the next resort to be from scratch
Definition: options.h:51
Mailbox::verbose
bool verbose
Display status messages?
Definition: mailbox.h:118
SORT_ORDER
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:50
Email::collapsed
bool collapsed
Is this message part of a collapsed thread?
Definition: email.h:73
Address::mailbox
char * mailbox
Mailbox and host address.
Definition: address.h:37
SORT_RECEIVED
@ SORT_RECEIVED
Sort by when the message were delivered locally.
Definition: sort2.h:52
compare_date_sent
static int compare_date_sent(const void *a, const void *b)
Compare the sent date of two emails - Implements sort_t.
Definition: sort.c:115
C_Sort
WHERE short C_Sort
Config: Sort method for the index.
Definition: sort.h:60
mutt_sort_threads
void mutt_sort_threads(struct ThreadsContext *tctx, bool init)
Sort email threads.
Definition: mutt_thread.c:870
mutt_thread_collapse_collapsed
void mutt_thread_collapse_collapsed(struct ThreadsContext *tctx)
re-collapse threads marked as collapsed
Definition: mutt_thread.c:1583
compare_from
static int compare_from(const void *a, const void *b)
Compare the 'from' fields of two emails - Implements sort_t.
Definition: sort.c:194
SORT_FROM
@ SORT_FROM
Sort by the email's From field.
Definition: sort2.h:49
compare_score
static int compare_score(const void *a, const void *b)
Compare two emails using their scores - Implements sort_t.
Definition: sort.c:91
OptSortSubthreads
WHERE bool OptSortSubthreads
(pseudo) used when $sort_aux changes
Definition: options.h:54
Email::vnum
int vnum
Virtual message number.
Definition: email.h:88
Email::index
int index
The absolute (unsorted) message number.
Definition: email.h:86
SORT_LABEL
@ SORT_LABEL
Sort by the emails label.
Definition: sort2.h:64
alias_reverse_lookup
struct Address * alias_reverse_lookup(const struct Address *addr)
Does the user have an alias for the given address.
Definition: reverse.c:105
Email
The envelope/body of an email.
Definition: email.h:37
SORT_SIZE
@ SORT_SIZE
Sort by the size of the email.
Definition: sort2.h:46
mutt_message
#define mutt_message(...)
Definition: logging.h:83
mutt_clear_threads
void mutt_clear_threads(struct ThreadsContext *tctx)
Clear the threading of message in a mailbox.
Definition: mutt_thread.c:637
C_SortAux
WHERE short C_SortAux
Config: Secondary sort method for the index.
Definition: sort.h:61
SORT_DATE
@ SORT_DATE
Sort by the date the email was sent.
Definition: sort2.h:45
compare_date_received
static int compare_date_received(const void *a, const void *b)
Compare the date received of two emails - Implements sort_t.
Definition: sort.c:210
Address
An email address.
Definition: address.h:34
mutt_addr_for_display
const char * mutt_addr_for_display(const struct Address *a)
Convert an Address for display purposes.
Definition: address.c:986
mutt_error
#define mutt_error(...)
Definition: logging.h:84