NeoMutt  2024-10-02-37-gfa9146
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 "core/lib.h"
37#include "sort.h"
38#include "lib.h"
39#include "globals.h"
40
45{
49};
50
54static int browser_sort_subject(const void *a, const void *b, void *sdata)
55{
56 const struct FolderFile *pa = (const struct FolderFile *) a;
57 const struct FolderFile *pb = (const struct FolderFile *) b;
58
59 /* inbox should be sorted ahead of its siblings */
60 int rc = mutt_str_inbox_cmp(pa->name, pb->name);
61 if (rc == 0)
62 rc = mutt_str_coll(pa->name, pb->name);
63
64 return rc;
65}
66
72static int browser_sort_order(const void *a, const void *b, void *sdata)
73{
74 const struct FolderFile *pa = (const struct FolderFile *) a;
75 const struct FolderFile *pb = (const struct FolderFile *) b;
76
77 return mutt_numeric_cmp(pa->gen, pb->gen);
78}
79
83static int browser_sort_desc(const void *a, const void *b, void *sdata)
84{
85 const struct FolderFile *pa = (const struct FolderFile *) a;
86 const struct FolderFile *pb = (const struct FolderFile *) b;
87
88 return mutt_str_coll(pa->desc, pb->desc);
89}
90
94static int browser_sort_date(const void *a, const void *b, void *sdata)
95{
96 const struct FolderFile *pa = (const struct FolderFile *) a;
97 const struct FolderFile *pb = (const struct FolderFile *) b;
98
99 return mutt_numeric_cmp(pa->mtime, pb->mtime);
100}
101
105static int browser_sort_size(const void *a, const void *b, void *sdata)
106{
107 const struct FolderFile *pa = (const struct FolderFile *) a;
108 const struct FolderFile *pb = (const struct FolderFile *) b;
109
110 return mutt_numeric_cmp(pa->size, pb->size);
111}
112
116static int browser_sort_count(const void *a, const void *b, void *sdata)
117{
118 const struct FolderFile *pa = (const struct FolderFile *) a;
119 const struct FolderFile *pb = (const struct FolderFile *) b;
120
121 int rc = 0;
122 if (pa->has_mailbox && pb->has_mailbox)
123 rc = mutt_numeric_cmp(pa->msg_count, pb->msg_count);
124 else if (pa->has_mailbox)
125 rc = -1;
126 else
127 rc = 1;
128
129 return rc;
130}
131
135static int browser_sort_count_new(const void *a, const void *b, void *sdata)
136{
137 const struct FolderFile *pa = (const struct FolderFile *) a;
138 const struct FolderFile *pb = (const struct FolderFile *) b;
139
140 int rc = 0;
141 if (pa->has_mailbox && pb->has_mailbox)
143 else if (pa->has_mailbox)
144 rc = -1;
145 else
146 rc = 1;
147
148 return rc;
149}
150
158static int browser_sort_helper(const void *a, const void *b, void *sdata)
159{
160 const struct FolderFile *pa = (const struct FolderFile *) a;
161 const struct FolderFile *pb = (const struct FolderFile *) b;
162 const struct CompareData *cd = (struct CompareData *) sdata;
163
164 if ((mutt_str_coll(pa->desc, "../") == 0) || (mutt_str_coll(pa->desc, "..") == 0))
165 return -1;
166 if ((mutt_str_coll(pb->desc, "../") == 0) || (mutt_str_coll(pb->desc, "..") == 0))
167 return 1;
168
169 if (cd->sort_dirs_first)
170 if (S_ISDIR(pa->mode) != S_ISDIR(pb->mode))
171 return S_ISDIR(pa->mode) ? -1 : 1;
172
173 int rc = cd->sort_fn(a, b, NULL);
174
175 return cd->sort_reverse ? -rc : rc;
176}
177
185void browser_sort(struct BrowserState *state)
186{
187 const enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
188 switch (c_sort_browser & SORT_MASK)
189 {
190 case SORT_SIZE:
191 case SORT_DATE:
192 if (OptNews)
193 return;
195 default:
196 break;
197 }
198
199 sort_t f = NULL;
200 switch (c_sort_browser & SORT_MASK)
201 {
202 case SORT_COUNT:
204 break;
205 case SORT_DATE:
207 break;
208 case SORT_DESC:
210 break;
211 case SORT_SIZE:
213 break;
214 case SORT_UNREAD:
216 break;
217 case SORT_SUBJECT:
219 break;
220 default:
221 case SORT_ORDER:
223 break;
224 }
225
226 struct CompareData cd = {
227 .sort_fn = f,
228 .sort_reverse = c_sort_browser & SORT_REVERSE,
229 .sort_dirs_first = cs_subset_bool(NeoMutt->sub, "browser_sort_dirs_first"),
230 };
231
232 ARRAY_SORT(&state->entry, browser_sort_helper, &cd);
233}
#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:185
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.
Convenience wrapper for the core headers.
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:83
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:94
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:116
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:105
static int browser_sort_count_new(const void *a, const void *b, void *sdata)
Compare two browser entries by their new count - Implements sort_t -.
Definition: sort.c:135
static int browser_sort_order(const void *a, const void *b, void *sdata)
Compare two browser entries by their order - Implements sort_t -.
Definition: sort.c:72
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:158
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:54
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
#define SORT_MASK
Mask for the sort id.
Definition: sort2.h:70
SortType
Methods for sorting.
Definition: sort2.h:34
@ SORT_SUBJECT
Sort by the email's subject.
Definition: sort2.h:38
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:40
@ SORT_SIZE
Sort by the size of the email.
Definition: sort2.h:36
@ SORT_DESC
Sort by the folder's description.
Definition: sort2.h:55
@ SORT_DATE
Sort by the date the email was sent.
Definition: sort2.h:35
@ SORT_COUNT
Sort by number of emails in a folder.
Definition: sort2.h:50
@ SORT_UNREAD
Sort by the number of unread emails.
Definition: sort2.h:51
#define SORT_REVERSE
Reverse the order of the sort.
Definition: sort2.h:71
Assorted sorting methods.
#define mutt_numeric_cmp(a, b)
Definition: sort.h:35
Key value store.
State of the file/mailbox browser.
Definition: lib.h:143
struct BrowserEntryArray entry
Array of files / dirs / mailboxes.
Definition: lib.h:144
Private data for browser_sort_helper()
Definition: sort.c:45
bool sort_dirs_first
$browser_sort_dirs_first = yes
Definition: sort.c:46
sort_t sort_fn
Function to perform $browser_sort.
Definition: sort.c:48
bool sort_reverse
$browser_sort contains 'reverse-'
Definition: sort.c:47
Browser entry representing a folder/dir.
Definition: lib.h:77
bool has_mailbox
This is a mailbox.
Definition: lib.h:97
char * name
Name of file/dir/mailbox.
Definition: lib.h:85
char * desc
Description of mailbox.
Definition: lib.h:86
off_t size
File size.
Definition: lib.h:79
int gen
Unique id, used for (un)sorting.
Definition: lib.h:104
time_t mtime
Modification time.
Definition: lib.h:80
int msg_count
total number of messages
Definition: lib.h:89
mode_t mode
File permissions.
Definition: lib.h:78
int msg_unread
number of unread messages
Definition: lib.h:90
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46