NeoMutt  2021-10-29-43-g6b8931
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 48 of file sort.c.

49 {
50  const struct AliasView *av_a = a;
51  const struct AliasView *av_b = b;
52 
53  if (av_a->is_visible != av_b->is_visible)
54  return av_a->is_visible ? -1 : 1;
55 
56  if (!av_a->is_visible)
57  return 0;
58 
59  int r = mutt_str_coll(av_a->alias->name, av_b->alias->name);
60 
61  return RSORT(r);
62 }
#define RSORT(num)
Definition: sort.c:39
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:488
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 69 of file sort.c.

70 {
71  const struct AliasView *av_a = a;
72  const struct AliasView *av_b = b;
73 
74  const struct AddressList *al_a = &av_a->alias->addr;
75  const struct AddressList *al_b = &av_b->alias->addr;
76 
77  if (av_a->is_visible != av_b->is_visible)
78  return av_a->is_visible ? -1 : 1;
79 
80  if (!av_a->is_visible)
81  return 0;
82 
83  int r;
84  if (al_a == al_b)
85  r = 0;
86  else if (!al_a)
87  r = -1;
88  else if (!al_b)
89  r = 1;
90  else
91  {
92  const struct Address *addr_a = TAILQ_FIRST(al_a);
93  const struct Address *addr_b = TAILQ_FIRST(al_b);
94  if (addr_a && addr_a->personal)
95  {
96  if (addr_b && addr_b->personal)
97  r = mutt_str_coll(addr_a->personal, addr_b->personal);
98  else
99  r = 1;
100  }
101  else if (addr_b && addr_b->personal)
102  r = -1;
103  else if (addr_a && addr_b)
104  r = mutt_str_coll(addr_a->mailbox, addr_b->mailbox);
105  else
106  r = 0;
107  }
108 
109  return RSORT(r);
110 }
#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 117 of file sort.c.

118 {
119  const struct AliasView *av_a = a;
120  const struct AliasView *av_b = b;
121 
122  if (av_a->is_visible != av_b->is_visible)
123  return av_a->is_visible ? -1 : 1;
124 
125  if (!av_a->is_visible)
126  return 0;
127 
128  int r = (av_a->orig_seq - av_b->orig_seq);
129 
130  return RSORT(r);
131 }
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 188 of file browser.c.

189 {
190  const struct FolderFile *pa = (const struct FolderFile *) a;
191  const struct FolderFile *pb = (const struct FolderFile *) b;
192 
193  /* inbox should be sorted ahead of its siblings */
194  int r = mutt_inbox_cmp(pa->name, pb->name);
195  if (r == 0)
196  r = mutt_str_coll(pa->name, pb->name);
197  const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
198  return (c_sort_browser & SORT_REVERSE) ? -r : r;
199 }
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:1611
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:79
Browser entry representing a folder/dir.
Definition: browser.h:54
char * name
Name of file/dir/mailbox.
Definition: browser.h:62
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 206 of file browser.c.

207 {
208  const struct FolderFile *pa = (const struct FolderFile *) a;
209  const struct FolderFile *pb = (const struct FolderFile *) b;
210 
211  const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
212  return ((c_sort_browser & SORT_REVERSE) ? -1 : 1) * (pa->gen - pb->gen);
213 }
int gen
Unique id, used for (un)sorting.
Definition: browser.h:83
+ 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 218 of file browser.c.

219 {
220  const struct FolderFile *pa = (const struct FolderFile *) a;
221  const struct FolderFile *pb = (const struct FolderFile *) b;
222 
223  int r = mutt_str_coll(pa->desc, pb->desc);
224 
225  const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
226  return (c_sort_browser & SORT_REVERSE) ? -r : r;
227 }
char * desc
Description of mailbox.
Definition: browser.h:63
+ 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 232 of file browser.c.

233 {
234  const struct FolderFile *pa = (const struct FolderFile *) a;
235  const struct FolderFile *pb = (const struct FolderFile *) b;
236 
237  int r = pa->mtime - pb->mtime;
238 
239  const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
240  return (c_sort_browser & SORT_REVERSE) ? -r : r;
241 }
time_t mtime
Modification time.
Definition: browser.h:57
+ 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 246 of file browser.c.

247 {
248  const struct FolderFile *pa = (const struct FolderFile *) a;
249  const struct FolderFile *pb = (const struct FolderFile *) b;
250 
251  int r = pa->size - pb->size;
252 
253  const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
254  return (c_sort_browser & SORT_REVERSE) ? -r : r;
255 }
off_t size
File size.
Definition: browser.h:56
+ 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 260 of file browser.c.

261 {
262  const struct FolderFile *pa = (const struct FolderFile *) a;
263  const struct FolderFile *pb = (const struct FolderFile *) b;
264 
265  int r = 0;
266  if (pa->has_mailbox && pb->has_mailbox)
267  r = pa->msg_count - pb->msg_count;
268  else if (pa->has_mailbox)
269  r = -1;
270  else
271  r = 1;
272 
273  const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
274  return (c_sort_browser & SORT_REVERSE) ? -r : r;
275 }
bool has_mailbox
This is a mailbox.
Definition: browser.h:76
int msg_count
total number of messages
Definition: browser.h:66
+ 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 280 of file browser.c.

281 {
282  const struct FolderFile *pa = (const struct FolderFile *) a;
283  const struct FolderFile *pb = (const struct FolderFile *) b;
284 
285  int r = 0;
286  if (pa->has_mailbox && pb->has_mailbox)
287  r = pa->msg_unread - pb->msg_unread;
288  else if (pa->has_mailbox)
289  r = -1;
290  else
291  r = 1;
292 
293  const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
294  return (c_sort_browser & SORT_REVERSE) ? -r : r;
295 }
int msg_unread
number of unread messages
Definition: browser.h:67
+ 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 304 of file browser.c.

305 {
306  const struct FolderFile *pa = (const struct FolderFile *) a;
307  const struct FolderFile *pb = (const struct FolderFile *) b;
308 
309  if ((mutt_str_coll(pa->desc, "../") == 0) || (mutt_str_coll(pa->desc, "..") == 0))
310  return -1;
311  if ((mutt_str_coll(pb->desc, "../") == 0) || (mutt_str_coll(pb->desc, "..") == 0))
312  return 1;
313 
314  const short c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
315  switch (c_sort_browser & SORT_MASK)
316  {
317  case SORT_COUNT:
318  return browser_compare_count(a, b);
319  case SORT_DATE:
320  return browser_compare_date(a, b);
321  case SORT_DESC:
322  return browser_compare_desc(a, b);
323  case SORT_SIZE:
324  return browser_compare_size(a, b);
325  case SORT_UNREAD:
326  return browser_compare_count_new(a, b);
327  case SORT_SUBJECT:
328  return browser_compare_subject(a, b);
329  default:
330  case SORT_ORDER:
331  return browser_compare_order(a, b);
332  }
333 }
static int browser_compare_desc(const void *a, const void *b)
Compare the descriptions of two browser entries - Implements sort_t -.
Definition: browser.c:218
static int browser_compare_count_new(const void *a, const void *b)
Compare the new count of two browser entries - Implements sort_t -.
Definition: browser.c:280
static int browser_compare_order(const void *a, const void *b)
Compare the order of creation of two browser entries - Implements sort_t -.
Definition: browser.c:206
static int browser_compare_size(const void *a, const void *b)
Compare the size of two browser entries - Implements sort_t -.
Definition: browser.c:246
static int browser_compare_count(const void *a, const void *b)
Compare the message count of two browser entries - Implements sort_t -.
Definition: browser.c:260
static int browser_compare_date(const void *a, const void *b)
Compare the date of two browser entries - Implements sort_t -.
Definition: browser.c:232
static int browser_compare_subject(const void *a, const void *b)
Compare the subject of two browser entries - Implements sort_t -.
Definition: browser.c:188
#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  return imap_edata_get((struct Email *) ea)->uid -
909  imap_edata_get((struct Email *) eb)->uid;
910 }
struct ImapEmailData * imap_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:64
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 496 of file maildir.c.

497 {
498  const struct MdEmail *ma = *(struct MdEmail **) a;
499  const struct MdEmail *mb = *(struct MdEmail **) b;
500 
501  return ma->inode - mb->inode;
502 }
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 144 of file mutt_commands.c.

145 {
146  struct Command x = *(const struct Command *) a;
147  struct Command y = *(const struct Command *) b;
148 
149  return strcmp(x.name, y.name);
150 }
const char * name
Name of the command.
Definition: command.h:48
+ 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 
57  if (sb_sort_reverse)
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:215
static bool sb_sort_reverse
An extra parameter to control sort order.
Definition: sort.c:39
A mailbox.
Definition: mailbox.h:82
int msg_count
Total number of messages.
Definition: mailbox.h:91
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 
74  if (sb_sort_reverse)
75  rc = -rc;
76  return rc;
77 }
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:378
char * name
A short name for the Mailbox.
Definition: mailbox.h:85
+ 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 
95  if (sb_sort_reverse)
96  rc = -rc;
97  return rc;
98 }
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:93
+ 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)
113  rc = mutt_str_coll(mailbox_path(m1), mailbox_path(m2));
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)
132  rc = mutt_str_coll(mailbox_path(m1), mailbox_path(m2));
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:92
+ 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:150
+ 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: