2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
17 #include "mutt_idna.h"
27 #define SORTCODE(x) (Sort & SORT_REVERSE) ? -(x) : x
29 /* function to use as discriminator when normal sort method is equal */
30 static sort_t *AuxSort = NULL;
32 #define AUXSORT(code,a,b) if (!code && AuxSort && !option(OPTAUXSORT)) { \
33 set_option(OPTAUXSORT); \
34 code = AuxSort(a,b); \
35 unset_option(OPTAUXSORT); \
38 code = (*((HEADER **)a))->index - (*((HEADER **)b))->index;
40 int compare_score (const void *a, const void *b)
42 HEADER **pa = (HEADER **) a;
43 HEADER **pb = (HEADER **) b;
44 int result = (*pb)->score - (*pa)->score; /* note that this is reverse */
46 AUXSORT (result, a, b);
47 return (SORTCODE (result));
50 int compare_size (const void *a, const void *b)
52 HEADER **pa = (HEADER **) a;
53 HEADER **pb = (HEADER **) b;
54 int result = (*pa)->content->length - (*pb)->content->length;
56 AUXSORT (result, a, b);
57 return (SORTCODE (result));
60 int compare_date_sent (const void *a, const void *b)
62 HEADER **pa = (HEADER **) a;
63 HEADER **pb = (HEADER **) b;
64 int result = (*pa)->date_sent - (*pb)->date_sent;
66 AUXSORT (result, a, b);
67 return (SORTCODE (result));
70 int compare_subject (const void *a, const void *b)
72 HEADER **pa = (HEADER **) a;
73 HEADER **pb = (HEADER **) b;
76 if (!(*pa)->env->real_subj) {
77 if (!(*pb)->env->real_subj)
78 rc = compare_date_sent (pa, pb);
82 else if (!(*pb)->env->real_subj)
85 rc = str_casecmp ((*pa)->env->real_subj, (*pb)->env->real_subj);
87 return (SORTCODE (rc));
90 const char *mutt_get_name (ADDRESS * a)
93 const char *name = "";
96 if (option (OPTREVALIAS) && (ali = alias_reverse_lookup (a))
102 name = (mutt_addr_for_display (a));
104 /* don't return NULL to avoid segfault when printing/comparing */
108 int compare_to (const void *a, const void *b)
110 HEADER **ppa = (HEADER **) a;
111 HEADER **ppb = (HEADER **) b;
116 /* mutt_get_name() will sometimes return a pointer to a static buffer.
117 * On the next call that pointer may get smashed so we copy the return value
118 * to our own memory space. */
120 strncpy (fa, mutt_get_name ((*ppa)->env->to), sizeof (fa));
121 fa[sizeof (fa) - 1] = '\0';
123 strncpy (fb, mutt_get_name ((*ppb)->env->to), sizeof (fb));
124 fb[sizeof (fb) - 1] = '\0';
126 result = str_casecmp (fa, fb);
127 AUXSORT (result, a, b);
128 return (SORTCODE (result));
131 int compare_from (const void *a, const void *b)
133 HEADER **ppa = (HEADER **) a;
134 HEADER **ppb = (HEADER **) b;
139 /* mutt_get_name() will sometimes return a pointer to a static buffer.
140 * On the next call that pointer may get smashed so we copy the return value
141 * to our own memory space. */
143 strncpy (fa, mutt_get_name ((*ppa)->env->from), sizeof (fa));
144 fa[sizeof (fa) - 1] = '\0';
146 strncpy (fb, mutt_get_name ((*ppb)->env->from), sizeof (fb));
147 fb[sizeof (fb) - 1] = '\0';
149 result = str_casecmp (fa, fb);
150 AUXSORT (result, a, b);
151 return (SORTCODE (result));
154 int compare_date_received (const void *a, const void *b)
156 HEADER **pa = (HEADER **) a;
157 HEADER **pb = (HEADER **) b;
158 int result = (*pa)->received - (*pb)->received;
160 AUXSORT (result, a, b);
161 return (SORTCODE (result));
164 int compare_order (const void *a, const void *b)
166 HEADER **ha = (HEADER **) a;
167 HEADER **hb = (HEADER **) b;
170 if ((*ha)->article_num && (*hb)->article_num) {
171 int result = (*ha)->article_num - (*hb)->article_num;
173 AUXSORT (result, a, b);
174 return (SORTCODE (result));
178 /* no need to auxsort because you will never have equality here */
179 return (SORTCODE ((*ha)->index - (*hb)->index));
182 int compare_spam (const void *a, const void *b)
184 HEADER **ppa = (HEADER **) a;
185 HEADER **ppb = (HEADER **) b;
190 /* Firstly, require spam attributes for both msgs */
191 /* to compare. Determine which msgs have one. */
192 ahas = (*ppa)->env && (*ppa)->env->spam;
193 bhas = (*ppb)->env && (*ppb)->env->spam;
195 /* If one msg has spam attr but other does not, sort the one with first. */
197 return (SORTCODE (1));
199 return (SORTCODE (-1));
201 /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
202 if (!ahas && !bhas) {
203 AUXSORT (result, a, b);
204 return (SORTCODE (result));
208 /* Both have spam attrs. */
210 /* preliminary numeric examination */
211 result = (strtoul ((*ppa)->env->spam->data, &aptr, 10) -
212 strtoul ((*ppb)->env->spam->data, &bptr, 10));
214 /* If either aptr or bptr is equal to data, there is no numeric */
215 /* value for that spam attribute. In this case, compare lexically. */
216 if ((aptr == (*ppa)->env->spam->data) || (bptr == (*ppb)->env->spam->data))
217 return (SORTCODE (str_cmp (aptr, bptr)));
219 /* Otherwise, we have numeric value for both attrs. If these values */
220 /* are equal, then we first fall back upon string comparison, then */
221 /* upon auxiliary sort. */
223 result = str_cmp (aptr, bptr);
225 AUXSORT (result, a, b);
228 return (SORTCODE (result));
231 sort_t *mutt_get_sort_func (int method)
233 switch (method & SORT_MASK) {
235 return (compare_date_received);
237 return (compare_order);
239 return (compare_date_sent);
241 return (compare_subject);
243 return (compare_from);
245 return (compare_size);
249 return (compare_score);
251 return (compare_spam);
258 void mutt_sort_headers (CONTEXT * ctx, int init)
262 THREAD *thread, *top;
265 unset_option (OPTNEEDRESORT);
270 if (!ctx->msgcount) {
271 /* this function gets called by mutt_sync_mailbox(), which may have just
272 * deleted all the messages. the virtual message numbers are not updated
273 * in that routine, so we must make sure to zero the vcount member.
276 mutt_clear_threads (ctx);
277 return; /* nothing to do! */
281 mutt_message _("Sorting mailbox...");
283 if (option (OPTNEEDRESCORE) && option (OPTSCORE)) {
284 for (i = 0; i < ctx->msgcount; i++)
285 mutt_score_message (ctx, ctx->hdrs[i], 1);
287 unset_option (OPTNEEDRESCORE);
289 if (option (OPTRESORTINIT)) {
290 unset_option (OPTRESORTINIT);
294 if (init && ctx->tree)
295 mutt_clear_threads (ctx);
297 if ((Sort & SORT_MASK) == SORT_THREADS) {
299 /* if $sort_aux changed after the mailbox is sorted, then all the
300 subthreads need to be resorted */
301 if (option (OPTSORTSUBTHREADS)) {
305 ctx->tree = mutt_sort_subthreads (ctx->tree, 1);
307 unset_option (OPTSORTSUBTHREADS);
309 mutt_sort_threads (ctx, init);
311 else if ((sortfunc = mutt_get_sort_func (Sort)) == NULL ||
312 (AuxSort = mutt_get_sort_func (SortAux)) == NULL) {
313 mutt_error _("Could not find sorting function! [report this bug]");
319 qsort ((void *) ctx->hdrs, ctx->msgcount, sizeof (HEADER *), sortfunc);
321 /* adjust the virtual message numbers */
323 for (i = 0; i < ctx->msgcount; i++) {
324 HEADER *cur = ctx->hdrs[i];
326 if (cur->virtual != -1
327 || (cur->collapsed && (!ctx->pattern || cur->limited))) {
328 cur->virtual = ctx->vcount;
329 ctx->v2r[ctx->vcount] = i;
335 /* re-collapse threads marked as collapsed */
336 if ((Sort & SORT_MASK) == SORT_THREADS) {
338 while ((thread = top) != NULL) {
339 while (!thread->message)
340 thread = thread->child;
344 mutt_collapse_thread (ctx, h);
347 mutt_set_virtual (ctx);