NeoMutt  2022-04-29-249-gaae397
Teaching an old dog new tricks
DOXYGEN
Sorting API

Prototype for generic comparison function, compatible with qsort() More...

Functions

static int alias_sort_name (const void *a, const void *b)
 Compare two Aliases by their short names - Implements sort_t -. More...
 
static int alias_sort_address (const void *a, const void *b)
 Compare two Aliases by their Addresses - Implements sort_t -. More...
 
static int alias_sort_unsort (const void *a, const void *b)
 Compare two Aliases by their original configuration position - Implements sort_t -. More...
 
static int browser_compare_subject (const void *a, const void *b)
 Compare the subject of two browser entries - Implements sort_t -. More...
 
static int browser_compare_order (const void *a, const void *b)
 Compare the order of creation of two browser entries - Implements sort_t -. More...
 
static int browser_compare_desc (const void *a, const void *b)
 Compare the descriptions of two browser entries - Implements sort_t -. More...
 
static int browser_compare_date (const void *a, const void *b)
 Compare the date of two browser entries - Implements sort_t -. More...
 
static int browser_compare_size (const void *a, const void *b)
 Compare the size of two browser entries - Implements sort_t -. More...
 
static int browser_compare_count (const void *a, const void *b)
 Compare the message count of two browser entries - Implements sort_t -. More...
 
static int browser_compare_count_new (const void *a, const void *b)
 Compare the new count of two browser entries - Implements sort_t -. More...
 
static int browser_compare (const void *a, const void *b)
 Sort the items in the browser - Implements sort_t -. More...
 
static int compare_uid (const void *a, const void *b)
 Compare two Emails by UID - Implements sort_t -. More...
 
static int maildir_cmp_inode (const void *a, const void *b)
 Compare two Maildirs by inode number - Implements sort_t -. More...
 
int mh_cmp_path (const void *a, const void *b)
 Compare two Maildirs by path - Implements sort_t -. More...
 
int commands_cmp (const void *a, const void *b)
 Compare two commands by name - Implements sort_t -. More...
 
static int sb_sort_count (const void *a, const void *b)
 Sort Sidebar entries by count - Implements sort_t -. More...
 
static int sb_sort_desc (const void *a, const void *b)
 Sort Sidebar entries by description - Implements sort_t -. More...
 
static int sb_sort_flagged (const void *a, const void *b)
 Sort Sidebar entries by flagged - Implements sort_t -. More...
 
static int sb_sort_path (const void *a, const void *b)
 Sort Sidebar entries by path - Implements sort_t -. More...
 
static int sb_sort_unread (const void *a, const void *b)
 Sort Sidebar entries by unread - Implements sort_t -. More...
 
static int sb_sort_order (const void *a, const void *b)
 Sort Sidebar entries by order of creation - Implements sort_t -. More...
 
static int sb_sort_unsorted (const void *a, const void *b)
 Sort Sidebar entries into their original order - Implements sort_t -. More...
 

Detailed Description

Prototype for generic comparison function, compatible with qsort()

Parameters
aFirst item
bSecond item
Return values
<0a precedes b
0a and b are identical
>0b precedes a

Function Documentation

◆ alias_sort_name()

static int alias_sort_name ( const void *  a,
const void *  b 
)
static

Compare two Aliases by their short names - Implements sort_t -.

Note
Non-visible Aliases are sorted to the end

Definition at line 47 of file sort.c.

48{
49 const struct AliasView *av_a = a;
50 const struct AliasView *av_b = b;
51
52 if (av_a->is_visible != av_b->is_visible)
53 return av_a->is_visible ? -1 : 1;
54
55 if (!av_a->is_visible)
56 return 0;
57
58 int r = mutt_str_coll(av_a->alias->name, av_b->alias->name);
59
60 return RSORT(r);
61}
#define RSORT(num)
Definition: sort.c:38
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:580
GUI data wrapping an Alias.
Definition: gui.h:36
bool is_visible
Is visible?
Definition: gui.h:43
struct Alias * alias
Alias.
Definition: gui.h:44
char * name
Short name.
Definition: alias.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_sort_address()

static int alias_sort_address ( const void *  a,
const void *  b 
)
static

Compare two Aliases by their Addresses - Implements sort_t -.

Note
Non-visible Aliases are sorted to the end

Definition at line 68 of file sort.c.

69{
70 const struct AliasView *av_a = a;
71 const struct AliasView *av_b = b;
72
73 const struct AddressList *al_a = &av_a->alias->addr;
74 const struct AddressList *al_b = &av_b->alias->addr;
75
76 if (av_a->is_visible != av_b->is_visible)
77 return av_a->is_visible ? -1 : 1;
78
79 if (!av_a->is_visible)
80 return 0;
81
82 int r;
83 if (al_a == al_b)
84 r = 0;
85 else if (!al_a)
86 r = -1;
87 else if (!al_b)
88 r = 1;
89 else
90 {
91 const struct Address *addr_a = TAILQ_FIRST(al_a);
92 const struct Address *addr_b = TAILQ_FIRST(al_b);
93 if (addr_a && addr_a->personal)
94 {
95 if (addr_b && addr_b->personal)
96 r = mutt_str_coll(addr_a->personal, addr_b->personal);
97 else
98 r = 1;
99 }
100 else if (addr_b && addr_b->personal)
101 r = -1;
102 else if (addr_a && addr_b)
103 r = mutt_str_coll(addr_a->mailbox, addr_b->mailbox);
104 else
105 r = 0;
106 }
107
108 return RSORT(r);
109}
#define TAILQ_FIRST(head)
Definition: queue.h:723
An email address.
Definition: address.h:36
char * mailbox
Mailbox and host address.
Definition: address.h:38
char * personal
Real name of address.
Definition: address.h:37
struct AddressList addr
List of Addresses the Alias expands to.
Definition: alias.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alias_sort_unsort()

static int alias_sort_unsort ( const void *  a,
const void *  b 
)
static

Compare two Aliases by their original configuration position - Implements sort_t -.

Note
Non-visible Aliases are sorted to the end

Definition at line 116 of file sort.c.

117{
118 const struct AliasView *av_a = a;
119 const struct AliasView *av_b = b;
120
121 if (av_a->is_visible != av_b->is_visible)
122 return av_a->is_visible ? -1 : 1;
123
124 if (!av_a->is_visible)
125 return 0;
126
127 int r = (av_a->orig_seq - av_b->orig_seq);
128
129 return RSORT(r);
130}
int orig_seq
Sequence in alias config file.
Definition: gui.h:38
+ Here is the caller graph for this function:

◆ browser_compare_subject()

static int browser_compare_subject ( const void *  a,
const void *  b 
)
static

Compare the subject of two browser entries - Implements sort_t -.

Definition at line 40 of file sort.c.

41{
42 const struct FolderFile *pa = (const struct FolderFile *) a;
43 const struct FolderFile *pb = (const struct FolderFile *) b;
44
45 /* inbox should be sorted ahead of its siblings */
46 int r = mutt_inbox_cmp(pa->name, pb->name);
47 if (r == 0)
48 r = mutt_str_coll(pa->name, pb->name);
49 const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
50 return (c_sort_browser & SORT_REVERSE) ? -r : r;
51}
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:292
int mutt_inbox_cmp(const char *a, const char *b)
Do two folders share the same path and one is an inbox.
Definition: muttlib.c:1605
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:79
Browser entry representing a folder/dir.
Definition: lib.h:73
char * name
Name of file/dir/mailbox.
Definition: lib.h:81
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ browser_compare_order()

static int browser_compare_order ( const void *  a,
const void *  b 
)
static

Compare the order of creation of two browser entries - Implements sort_t -.

Note
This only affects browsing mailboxes and is a no-op for folders.

Definition at line 58 of file sort.c.

59{
60 const struct FolderFile *pa = (const struct FolderFile *) a;
61 const struct FolderFile *pb = (const struct FolderFile *) b;
62
63 const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
64 return ((c_sort_browser & SORT_REVERSE) ? -1 : 1) * (pa->gen - pb->gen);
65}
int gen
Unique id, used for (un)sorting.
Definition: lib.h:102
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ browser_compare_desc()

static int browser_compare_desc ( const void *  a,
const void *  b 
)
static

Compare the descriptions of two browser entries - Implements sort_t -.

Definition at line 70 of file sort.c.

71{
72 const struct FolderFile *pa = (const struct FolderFile *) a;
73 const struct FolderFile *pb = (const struct FolderFile *) b;
74
75 int r = mutt_str_coll(pa->desc, pb->desc);
76
77 const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
78 return (c_sort_browser & SORT_REVERSE) ? -r : r;
79}
char * desc
Description of mailbox.
Definition: lib.h:82
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ browser_compare_date()

static int browser_compare_date ( const void *  a,
const void *  b 
)
static

Compare the date of two browser entries - Implements sort_t -.

Definition at line 84 of file sort.c.

85{
86 const struct FolderFile *pa = (const struct FolderFile *) a;
87 const struct FolderFile *pb = (const struct FolderFile *) b;
88
89 int r = pa->mtime - pb->mtime;
90
91 const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
92 return (c_sort_browser & SORT_REVERSE) ? -r : r;
93}
time_t mtime
Modification time.
Definition: lib.h:76
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ browser_compare_size()

static int browser_compare_size ( const void *  a,
const void *  b 
)
static

Compare the size of two browser entries - Implements sort_t -.

Definition at line 98 of file sort.c.

99{
100 const struct FolderFile *pa = (const struct FolderFile *) a;
101 const struct FolderFile *pb = (const struct FolderFile *) b;
102
103 int r = pa->size - pb->size;
104
105 const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
106 return (c_sort_browser & SORT_REVERSE) ? -r : r;
107}
off_t size
File size.
Definition: lib.h:75
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ browser_compare_count()

static int browser_compare_count ( const void *  a,
const void *  b 
)
static

Compare the message count of two browser entries - Implements sort_t -.

Definition at line 112 of file sort.c.

113{
114 const struct FolderFile *pa = (const struct FolderFile *) a;
115 const struct FolderFile *pb = (const struct FolderFile *) b;
116
117 int r = 0;
118 if (pa->has_mailbox && pb->has_mailbox)
119 r = pa->msg_count - pb->msg_count;
120 else if (pa->has_mailbox)
121 r = -1;
122 else
123 r = 1;
124
125 const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
126 return (c_sort_browser & SORT_REVERSE) ? -r : r;
127}
bool has_mailbox
This is a mailbox.
Definition: lib.h:95
int msg_count
total number of messages
Definition: lib.h:85
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ browser_compare_count_new()

static int browser_compare_count_new ( const void *  a,
const void *  b 
)
static

Compare the new count of two browser entries - Implements sort_t -.

Definition at line 132 of file sort.c.

133{
134 const struct FolderFile *pa = (const struct FolderFile *) a;
135 const struct FolderFile *pb = (const struct FolderFile *) b;
136
137 int r = 0;
138 if (pa->has_mailbox && pb->has_mailbox)
139 r = pa->msg_unread - pb->msg_unread;
140 else if (pa->has_mailbox)
141 r = -1;
142 else
143 r = 1;
144
145 const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
146 return (c_sort_browser & SORT_REVERSE) ? -r : r;
147}
int msg_unread
number of unread messages
Definition: lib.h:86
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ browser_compare()

static int browser_compare ( const void *  a,
const void *  b 
)
static

Sort the items in the browser - Implements sort_t -.

Wild compare function that calls the others. It's useful because it provides a way to tell "../" is always on the top of the list, independently of the sort method.

Definition at line 156 of file sort.c.

157{
158 const struct FolderFile *pa = (const struct FolderFile *) a;
159 const struct FolderFile *pb = (const struct FolderFile *) b;
160
161 if ((mutt_str_coll(pa->desc, "../") == 0) || (mutt_str_coll(pa->desc, "..") == 0))
162 return -1;
163 if ((mutt_str_coll(pb->desc, "../") == 0) || (mutt_str_coll(pb->desc, "..") == 0))
164 return 1;
165
166 const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
167 switch (c_sort_browser & SORT_MASK)
168 {
169 case SORT_COUNT:
170 return browser_compare_count(a, b);
171 case SORT_DATE:
172 return browser_compare_date(a, b);
173 case SORT_DESC:
174 return browser_compare_desc(a, b);
175 case SORT_SIZE:
176 return browser_compare_size(a, b);
177 case SORT_UNREAD:
178 return browser_compare_count_new(a, b);
179 case SORT_SUBJECT:
180 return browser_compare_subject(a, b);
181 default:
182 case SORT_ORDER:
183 return browser_compare_order(a, b);
184 }
185}
static int browser_compare_desc(const void *a, const void *b)
Compare the descriptions of two browser entries - Implements sort_t -.
Definition: sort.c:70
static int browser_compare_count_new(const void *a, const void *b)
Compare the new count of two browser entries - Implements sort_t -.
Definition: sort.c:132
static int browser_compare_order(const void *a, const void *b)
Compare the order of creation of two browser entries - Implements sort_t -.
Definition: sort.c:58
static int browser_compare_size(const void *a, const void *b)
Compare the size of two browser entries - Implements sort_t -.
Definition: sort.c:98
static int browser_compare_count(const void *a, const void *b)
Compare the message count of two browser entries - Implements sort_t -.
Definition: sort.c:112
static int browser_compare_date(const void *a, const void *b)
Compare the date of two browser entries - Implements sort_t -.
Definition: sort.c:84
static int browser_compare_subject(const void *a, const void *b)
Compare the subject of two browser entries - Implements sort_t -.
Definition: sort.c:40
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:78
@ SORT_SUBJECT
Sort by the email's subject.
Definition: sort2.h:46
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:48
@ SORT_SIZE
Sort by the size of the email.
Definition: sort2.h:44
@ SORT_DESC
Sort by the folder's description.
Definition: sort2.h:63
@ SORT_DATE
Sort by the date the email was sent.
Definition: sort2.h:43
@ SORT_COUNT
Sort by number of emails in a folder.
Definition: sort2.h:58
@ SORT_UNREAD
Sort by the number of unread emails.
Definition: sort2.h:59
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compare_uid()

static int compare_uid ( const void *  a,
const void *  b 
)
static

Compare two Emails by UID - Implements sort_t -.

Definition at line 904 of file imap.c.

905{
906 const struct Email *ea = *(struct Email const *const *) a;
907 const struct Email *eb = *(struct Email const *const *) b;
908
909 const unsigned int ua = imap_edata_get((struct Email *) ea)->uid;
910 const unsigned int ub = imap_edata_get((struct Email *) eb)->uid;
911
912 return mutt_numeric_cmp(ua, ub);
913}
struct ImapEmailData * imap_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:65
#define mutt_numeric_cmp(a, b)
Definition: sort.h:36
The envelope/body of an email.
Definition: email.h:37
unsigned int uid
32-bit Message UID
Definition: edata.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ maildir_cmp_inode()

static int maildir_cmp_inode ( const void *  a,
const void *  b 
)
static

Compare two Maildirs by inode number - Implements sort_t -.

Definition at line 495 of file maildir.c.

496{
497 const struct MdEmail *ma = *(struct MdEmail **) a;
498 const struct MdEmail *mb = *(struct MdEmail **) b;
499
500 return ma->inode - mb->inode;
501}
A Maildir Email helper.
Definition: mdemail.h:34
ino_t inode
Definition: mdemail.h:38
+ Here is the caller graph for this function:

◆ mh_cmp_path()

int mh_cmp_path ( const void *  a,
const void *  b 
)

Compare two Maildirs by path - Implements sort_t -.

Definition at line 558 of file mh.c.

559{
560 struct MdEmail const *const *pa = (struct MdEmail const *const *) a;
561 struct MdEmail const *const *pb = (struct MdEmail const *const *) b;
562 return strcmp((*pa)->email->path, (*pb)->email->path);
563}
+ Here is the caller graph for this function:

◆ commands_cmp()

int commands_cmp ( const void *  a,
const void *  b 
)

Compare two commands by name - Implements sort_t -.

Definition at line 146 of file mutt_commands.c.

147{
148 struct Command x = *(const struct Command *) a;
149 struct Command y = *(const struct Command *) b;
150
151 return strcmp(x.name, y.name);
152}
const char * name
Name of the command.
Definition: command.h:50
+ Here is the caller graph for this function:

◆ sb_sort_count()

static int sb_sort_count ( const void *  a,
const void *  b 
)
static

Sort Sidebar entries by count - Implements sort_t -.

Definition at line 44 of file sort.c.

45{
46 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
47 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
48 const struct Mailbox *m1 = sbe1->mailbox;
49 const struct Mailbox *m2 = sbe2->mailbox;
50
51 int rc = 0;
52 if (m1->msg_count == m2->msg_count)
54 else
55 rc = (m2->msg_count - m1->msg_count);
56
58 rc = -rc;
59 return rc;
60}
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:210
static bool sb_sort_reverse
An extra parameter to control sort order.
Definition: sort.c:39
A mailbox.
Definition: mailbox.h:79
int msg_count
Total number of messages.
Definition: mailbox.h:88
Info about folders in the sidebar.
Definition: private.h:41
struct Mailbox * mailbox
Mailbox this represents.
Definition: private.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sb_sort_desc()

static int sb_sort_desc ( const void *  a,
const void *  b 
)
static

Sort Sidebar entries by description - Implements sort_t -.

Definition at line 65 of file sort.c.

66{
67 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
68 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
69 const struct Mailbox *m1 = sbe1->mailbox;
70 const struct Mailbox *m2 = sbe2->mailbox;
71
72 int rc = mutt_str_cmp(m1->name, m2->name);
73
75 rc = -rc;
76 return rc;
77}
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:470
char * name
A short name for the Mailbox.
Definition: mailbox.h:82
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sb_sort_flagged()

static int sb_sort_flagged ( const void *  a,
const void *  b 
)
static

Sort Sidebar entries by flagged - Implements sort_t -.

Definition at line 82 of file sort.c.

83{
84 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
85 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
86 const struct Mailbox *m1 = sbe1->mailbox;
87 const struct Mailbox *m2 = sbe2->mailbox;
88
89 int rc = 0;
90 if (m1->msg_flagged == m2->msg_flagged)
92 else
93 rc = (m2->msg_flagged - m1->msg_flagged);
94
96 rc = -rc;
97 return rc;
98}
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:90
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sb_sort_path()

static int sb_sort_path ( const void *  a,
const void *  b 
)
static

Sort Sidebar entries by path - Implements sort_t -.

Definition at line 103 of file sort.c.

104{
105 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
106 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
107 const struct Mailbox *m1 = sbe1->mailbox;
108 const struct Mailbox *m2 = sbe2->mailbox;
109
110 int rc = 0;
112 if (rc == 0)
114
115 if (sb_sort_reverse)
116 rc = -rc;
117 return rc;
118}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sb_sort_unread()

static int sb_sort_unread ( const void *  a,
const void *  b 
)
static

Sort Sidebar entries by unread - Implements sort_t -.

Definition at line 123 of file sort.c.

124{
125 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
126 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
127 const struct Mailbox *m1 = sbe1->mailbox;
128 const struct Mailbox *m2 = sbe2->mailbox;
129
130 int rc = 0;
131 if (m1->msg_unread == m2->msg_unread)
133 else
134 rc = (m2->msg_unread - m1->msg_unread);
135
136 if (sb_sort_reverse)
137 rc = -rc;
138 return rc;
139}
int msg_unread
Number of unread messages.
Definition: mailbox.h:89
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sb_sort_order()

static int sb_sort_order ( const void *  a,
const void *  b 
)
static

Sort Sidebar entries by order of creation - Implements sort_t -.

Definition at line 144 of file sort.c.

145{
146 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
147 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
148 const struct Mailbox *m1 = sbe1->mailbox;
149 const struct Mailbox *m2 = sbe2->mailbox;
150
151 return (sb_sort_reverse ? -1 : 1) * (m1->gen - m2->gen);
152}
int gen
Generation number, for sorting.
Definition: mailbox.h:146
+ Here is the caller graph for this function:

◆ sb_sort_unsorted()

static int sb_sort_unsorted ( const void *  a,
const void *  b 
)
static

Sort Sidebar entries into their original order - Implements sort_t -.

Definition at line 157 of file sort.c.

158{
159 const struct SbEntry *sbe1 = *(struct SbEntry const *const *) a;
160 const struct SbEntry *sbe2 = *(struct SbEntry const *const *) b;
161
162 // This sort method isn't affected by the reverse flag
163 return (sbe1->mailbox->gen - sbe2->mailbox->gen);
164}
+ Here is the caller graph for this function: