NeoMutt  2024-12-12-19-ge4b57e
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
sort.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stddef.h>
33#include <sys/stat.h>
34#include "mutt/lib.h"
35#include "config/lib.h"
36#include "email/lib.h"
37#include "core/lib.h"
38#include "sort.h"
39#include "lib.h"
40#include "globals.h"
41
46{
50};
51
55static int browser_sort_subject(const void *a, const void *b, void *sdata)
56{
57 const struct FolderFile *pa = (const struct FolderFile *) a;
58 const struct FolderFile *pb = (const struct FolderFile *) b;
59
60 /* inbox should be sorted ahead of its siblings */
61 int rc = mutt_str_inbox_cmp(pa->name, pb->name);
62 if (rc == 0)
63 rc = mutt_str_coll(pa->name, pb->name);
64
65 return rc;
66}
67
73static int browser_sort_unsorted(const void *a, const void *b, void *sdata)
74{
75 const struct FolderFile *pa = (const struct FolderFile *) a;
76 const struct FolderFile *pb = (const struct FolderFile *) b;
77
78 return mutt_numeric_cmp(pa->gen, pb->gen);
79}
80
84static int browser_sort_desc(const void *a, const void *b, void *sdata)
85{
86 const struct FolderFile *pa = (const struct FolderFile *) a;
87 const struct FolderFile *pb = (const struct FolderFile *) b;
88
89 return mutt_str_coll(pa->desc, pb->desc);
90}
91
95static int browser_sort_date(const void *a, const void *b, void *sdata)
96{
97 const struct FolderFile *pa = (const struct FolderFile *) a;
98 const struct FolderFile *pb = (const struct FolderFile *) b;
99
100 return mutt_numeric_cmp(pa->mtime, pb->mtime);
101}
102
106static int browser_sort_size(const void *a, const void *b, void *sdata)
107{
108 const struct FolderFile *pa = (const struct FolderFile *) a;
109 const struct FolderFile *pb = (const struct FolderFile *) b;
110
111 return mutt_numeric_cmp(pa->size, pb->size);
112}
113
117static int browser_sort_count(const void *a, const void *b, void *sdata)
118{
119 const struct FolderFile *pa = (const struct FolderFile *) a;
120 const struct FolderFile *pb = (const struct FolderFile *) b;
121
122 int rc = 0;
123 if (pa->has_mailbox && pb->has_mailbox)
124 rc = mutt_numeric_cmp(pa->msg_count, pb->msg_count);
125 else if (pa->has_mailbox)
126 rc = -1;
127 else
128 rc = 1;
129
130 return rc;
131}
132
136static int browser_sort_new(const void *a, const void *b, void *sdata)
137{
138 const struct FolderFile *pa = (const struct FolderFile *) a;
139 const struct FolderFile *pb = (const struct FolderFile *) b;
140
141 int rc = 0;
142 if (pa->has_mailbox && pb->has_mailbox)
144 else if (pa->has_mailbox)
145 rc = -1;
146 else
147 rc = 1;
148
149 return rc;
150}
151
159static int browser_sort_helper(const void *a, const void *b, void *sdata)
160{
161 const struct FolderFile *pa = (const struct FolderFile *) a;
162 const struct FolderFile *pb = (const struct FolderFile *) b;
163 const struct CompareData *cd = (struct CompareData *) sdata;
164
165 if ((mutt_str_coll(pa->desc, "../") == 0) || (mutt_str_coll(pa->desc, "..") == 0))
166 return -1;
167 if ((mutt_str_coll(pb->desc, "../") == 0) || (mutt_str_coll(pb->desc, "..") == 0))
168 return 1;
169
170 if (cd->sort_dirs_first)
171 if (S_ISDIR(pa->mode) != S_ISDIR(pb->mode))
172 return S_ISDIR(pa->mode) ? -1 : 1;
173
174 int rc = cd->sort_fn(a, b, NULL);
175
176 return cd->sort_reverse ? -rc : rc;
177}
178
186void browser_sort(struct BrowserState *state)
187{
188 const enum EmailSortType c_browser_sort = cs_subset_sort(NeoMutt->sub, "browser_sort");
189 switch (c_browser_sort & SORT_MASK)
190 {
193 if (OptNews)
194 return;
196 default:
197 break;
198 }
199
200 sort_t f = NULL;
201 switch (c_browser_sort & SORT_MASK)
202 {
205 break;
208 break;
211 break;
214 break;
215 case BROWSER_SORT_NEW:
217 break;
220 break;
222 default:
224 break;
225 }
226
227 struct CompareData cd = {
228 .sort_fn = f,
229 .sort_reverse = c_browser_sort & SORT_REVERSE,
230 .sort_dirs_first = cs_subset_bool(NeoMutt->sub, "browser_sort_dirs_first"),
231 };
232
233 ARRAY_SORT(&state->entry, browser_sort_helper, &cd);
234}
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition: array.h:279
void browser_sort(struct BrowserState *state)
Sort the entries in the browser.
Definition: sort.c:186
@ BROWSER_SORT_ALPHA
Sort alphabetically by name.
Definition: sort.h:31
@ BROWSER_SORT_UNSORTED
Sort into the raw order.
Definition: sort.h:37
@ BROWSER_SORT_COUNT
Sort by total message count.
Definition: sort.h:32
@ BROWSER_SORT_DATE
Sort by date.
Definition: sort.h:33
@ BROWSER_SORT_NEW
Sort by count of new messages.
Definition: sort.h:35
@ BROWSER_SORT_SIZE
Sort by size.
Definition: sort.h:36
@ BROWSER_SORT_DESC
Sort by description.
Definition: sort.h:34
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:47
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:266
Convenience wrapper for the config headers.
#define SORT_MASK
Mask for the sort id.
Definition: sort.h:38
#define mutt_numeric_cmp(a, b)
Definition: sort.h:26
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort.h:39
Convenience wrapper for the core headers.
Structs that make up an email.
EmailSortType
Methods for sorting Emails.
Definition: sort.h:53
bool OptNews
(pseudo) used to change reader mode
Definition: globals.c:67
static int browser_sort_desc(const void *a, const void *b, void *sdata)
Compare two browser entries by their descriptions - Implements sort_t -.
Definition: sort.c:84
static int browser_sort_date(const void *a, const void *b, void *sdata)
Compare two browser entries by their date - Implements sort_t -.
Definition: sort.c:95
int mutt_str_inbox_cmp(const char *a, const char *b)
Do two folders share the same path and one is an inbox -.
Definition: string.c:887
static int browser_sort_count(const void *a, const void *b, void *sdata)
Compare two browser entries by their message count - Implements sort_t -.
Definition: sort.c:117
static int browser_sort_size(const void *a, const void *b, void *sdata)
Compare two browser entries by their size - Implements sort_t -.
Definition: sort.c:106
static int browser_sort_helper(const void *a, const void *b, void *sdata)
Helper to sort the items in the browser - Implements sort_t -.
Definition: sort.c:159
static int browser_sort_new(const void *a, const void *b, void *sdata)
Compare two browser entries by their new count - Implements sort_t -.
Definition: sort.c:136
static int browser_sort_subject(const void *a, const void *b, void *sdata)
Compare two browser entries by their subject - Implements sort_t -.
Definition: sort.c:55
static int browser_sort_unsorted(const void *a, const void *b, void *sdata)
Compare two browser entries by their order - Implements sort_t -.
Definition: sort.c:73
Convenience wrapper for the library headers.
#define FALLTHROUGH
Definition: lib.h:111
int mutt_str_coll(const char *a, const char *b)
Collate two strings (compare using locale), safely.
Definition: string.c:509
int(* sort_t)(const void *a, const void *b, void *sdata)
Definition: qsort_r.h:41
Sidebar sorting functions.
Key value store.
State of the file/mailbox browser.
Definition: lib.h:144
struct BrowserEntryArray entry
Array of files / dirs / mailboxes.
Definition: lib.h:145
Private data for browser_sort_helper()
Definition: sort.c:46
bool sort_dirs_first
$browser_sort_dirs_first = yes
Definition: sort.c:47
sort_t sort_fn
Function to perform $browser_sort.
Definition: sort.c:49
bool sort_reverse
$browser_sort contains 'reverse-'
Definition: sort.c:48
Browser entry representing a folder/dir.
Definition: lib.h:78
bool has_mailbox
This is a mailbox.
Definition: lib.h:98
char * name
Name of file/dir/mailbox.
Definition: lib.h:86
char * desc
Description of mailbox.
Definition: lib.h:87
off_t size
File size.
Definition: lib.h:80
int gen
Unique id, used for (un)sorting.
Definition: lib.h:105
time_t mtime
Modification time.
Definition: lib.h:81
int msg_count
total number of messages
Definition: lib.h:90
mode_t mode
File permissions.
Definition: lib.h:79
int msg_unread
number of unread messages
Definition: lib.h:91
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46