NeoMutt  2023-05-17-16-g61469c
Teaching an old dog new tricks
DOXYGEN
sort.c
Go to the documentation of this file.
1
29#include "config.h"
30#include "mutt/lib.h"
31#include "config/lib.h"
32#include "core/lib.h"
33#include "lib.h"
34#include "globals.h"
35#include "muttlib.h"
36
40static int browser_compare_subject(const void *a, const void *b)
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 enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
50 return (c_sort_browser & SORT_REVERSE) ? -r : r;
51}
52
58static int browser_compare_order(const void *a, const void *b)
59{
60 const struct FolderFile *pa = (const struct FolderFile *) a;
61 const struct FolderFile *pb = (const struct FolderFile *) b;
62
63 const enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
64 return ((c_sort_browser & SORT_REVERSE) ? -1 : 1) * (pa->gen - pb->gen);
65}
66
70static int browser_compare_desc(const void *a, const void *b)
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 enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
78 return (c_sort_browser & SORT_REVERSE) ? -r : r;
79}
80
84static int browser_compare_date(const void *a, const void *b)
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 enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
92 return (c_sort_browser & SORT_REVERSE) ? -r : r;
93}
94
98static int browser_compare_size(const void *a, const void *b)
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 enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
106 return (c_sort_browser & SORT_REVERSE) ? -r : r;
107}
108
112static int browser_compare_count(const void *a, const void *b)
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 enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
126 return (c_sort_browser & SORT_REVERSE) ? -r : r;
127}
128
132static int browser_compare_count_new(const void *a, const void *b)
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 enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
146 return (c_sort_browser & SORT_REVERSE) ? -r : r;
147}
148
156static int browser_compare(const void *a, const void *b)
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 enum SortType 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}
186
194void browser_sort(struct BrowserState *state)
195{
196 const enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
197 switch (c_sort_browser & SORT_MASK)
198 {
199#ifdef USE_NNTP
200 case SORT_SIZE:
201 case SORT_DATE:
202 if (OptNews)
203 return;
204#endif
205 default:
206 break;
207 }
208
210}
#define ARRAY_SORT(head, fn)
Sort an array.
Definition: array.h:277
void browser_sort(struct BrowserState *state)
Sort the entries in the browser.
Definition: sort.c:194
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:292
Convenience wrapper for the config headers.
Convenience wrapper for the core headers.
bool OptNews
(pseudo) used to change reader mode
Definition: globals.c:79
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(const void *a, const void *b)
Sort the items in the browser - Implements sort_t -.
Definition: sort.c:156
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
Convenience wrapper for the library headers.
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:581
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:1579
Some miscellaneous functions.
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:74
SortType
Methods for sorting.
Definition: sort2.h:38
@ 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_SIZE
Sort by the size of the email.
Definition: sort2.h:40
@ SORT_DESC
Sort by the folder's description.
Definition: sort2.h:59
@ SORT_DATE
Sort by the date the email was sent.
Definition: sort2.h:39
@ SORT_COUNT
Sort by number of emails in a folder.
Definition: sort2.h:54
@ SORT_UNREAD
Sort by the number of unread emails.
Definition: sort2.h:55
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:75
Key value store.
State of the file/mailbox browser.
Definition: lib.h:111
struct BrowserStateEntry entry
Array of files / dirs / mailboxes.
Definition: lib.h:112
Browser entry representing a folder/dir.
Definition: lib.h:73
bool has_mailbox
This is a mailbox.
Definition: lib.h:95
char * name
Name of file/dir/mailbox.
Definition: lib.h:81
char * desc
Description of mailbox.
Definition: lib.h:82
off_t size
File size.
Definition: lib.h:75
int gen
Unique id, used for (un)sorting.
Definition: lib.h:102
time_t mtime
Modification time.
Definition: lib.h:76
int msg_count
total number of messages
Definition: lib.h:85
int msg_unread
number of unread messages
Definition: lib.h:86
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39