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.
10 #include <lib-lib/lib-lib.h>
12 #include <lib-ui/curses.h>
19 #include "mutt_idna.h"
21 #define SORTCODE(x) (Sort & SORT_REVERSE) ? -(x) : x
23 /* function to use as discriminator when normal sort method is equal */
24 static sort_t *AuxSort = NULL;
26 #define AUXSORT(code,a,b) if (!code && AuxSort && !option(OPTAUXSORT)) { \
27 set_option(OPTAUXSORT); \
28 code = AuxSort(a,b); \
29 unset_option(OPTAUXSORT); \
32 code = (*((HEADER **)a))->index - (*((HEADER **)b))->index;
34 static int compare_score (const void *a, const void *b)
36 HEADER **pa = (HEADER **) a;
37 HEADER **pb = (HEADER **) b;
38 int result = (*pb)->score - (*pa)->score; /* note that this is reverse */
40 AUXSORT (result, a, b);
41 return (SORTCODE (result));
44 static int compare_size (const void *a, const void *b)
46 HEADER **pa = (HEADER **) a;
47 HEADER **pb = (HEADER **) b;
48 int result = (*pa)->content->length - (*pb)->content->length;
50 AUXSORT (result, a, b);
51 return (SORTCODE (result));
54 static int compare_date_sent (const void *a, const void *b)
56 HEADER **pa = (HEADER **) a;
57 HEADER **pb = (HEADER **) b;
58 int result = (*pa)->date_sent - (*pb)->date_sent;
60 AUXSORT (result, a, b);
61 return (SORTCODE (result));
64 static int compare_subject (const void *a, const void *b)
66 HEADER **pa = (HEADER **) a;
67 HEADER **pb = (HEADER **) b;
70 if (!(*pa)->env->real_subj) {
71 if (!(*pb)->env->real_subj)
72 rc = compare_date_sent (pa, pb);
76 else if (!(*pb)->env->real_subj)
79 rc = m_strcasecmp((*pa)->env->real_subj, (*pb)->env->real_subj);
81 return (SORTCODE (rc));
84 const char *mutt_get_name (address_t * a)
87 const char *name = "";
90 if (option (OPTREVALIAS) && (ali = alias_reverse_lookup(a))
96 name = (mutt_addr_for_display (a));
98 /* don't return NULL to avoid segfault when printing/comparing */
102 static int compare_to (const void *a, const void *b)
104 HEADER **ppa = (HEADER **) a;
105 HEADER **ppb = (HEADER **) b;
110 /* mutt_get_name() will sometimes return a pointer to a static buffer.
111 * On the next call that pointer may get smashed so we copy the return value
112 * to our own memory space. */
114 m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->to));
115 m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->to));
117 result = m_strcasecmp(fa, fb);
118 AUXSORT (result, a, b);
119 return (SORTCODE (result));
122 static int compare_from (const void *a, const void *b)
124 HEADER **ppa = (HEADER **) a;
125 HEADER **ppb = (HEADER **) b;
130 /* mutt_get_name() will sometimes return a pointer to a static buffer.
131 * On the next call that pointer may get smashed so we copy the return value
132 * to our own memory space. */
134 m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->from));
135 m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->from));
137 result = m_strcasecmp(fa, fb);
138 AUXSORT (result, a, b);
139 return (SORTCODE (result));
142 static int compare_date_received (const void *a, const void *b)
144 HEADER **pa = (HEADER **) a;
145 HEADER **pb = (HEADER **) b;
146 int result = (*pa)->received - (*pb)->received;
148 AUXSORT (result, a, b);
149 return (SORTCODE (result));
152 static int compare_order (const void *a, const void *b)
154 HEADER **ha = (HEADER **) a;
155 HEADER **hb = (HEADER **) b;
158 if ((*ha)->article_num && (*hb)->article_num) {
159 int result = (*ha)->article_num - (*hb)->article_num;
161 AUXSORT (result, a, b);
162 return (SORTCODE (result));
166 /* no need to auxsort because you will never have equality here */
167 return (SORTCODE ((*ha)->index - (*hb)->index));
170 static int compare_spam (const void *a, const void *b)
172 HEADER **ppa = (HEADER **) a;
173 HEADER **ppb = (HEADER **) b;
178 /* Firstly, require spam attributes for both msgs */
179 /* to compare. Determine which msgs have one. */
180 ahas = (*ppa)->env && (*ppa)->env->spam;
181 bhas = (*ppb)->env && (*ppb)->env->spam;
183 /* If one msg has spam attr but other does not, sort the one with first. */
185 return (SORTCODE (1));
187 return (SORTCODE (-1));
189 /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
190 if (!ahas && !bhas) {
191 AUXSORT (result, a, b);
192 return (SORTCODE (result));
196 /* Both have spam attrs. */
198 /* preliminary numeric examination */
199 result = (strtoul ((*ppa)->env->spam->data, &aptr, 10) -
200 strtoul ((*ppb)->env->spam->data, &bptr, 10));
202 /* If either aptr or bptr is equal to data, there is no numeric */
203 /* value for that spam attribute. In this case, compare lexically. */
204 if ((aptr == (*ppa)->env->spam->data) || (bptr == (*ppb)->env->spam->data))
205 return (SORTCODE (m_strcmp(aptr, bptr)));
207 /* Otherwise, we have numeric value for both attrs. If these values */
208 /* are equal, then we first fall back upon string comparison, then */
209 /* upon auxiliary sort. */
211 result = m_strcmp(aptr, bptr);
213 AUXSORT (result, a, b);
216 return (SORTCODE (result));
219 sort_t *mutt_get_sort_func (int method)
221 switch (method & SORT_MASK) {
223 return (compare_date_received);
225 return (compare_order);
227 return (compare_date_sent);
229 return (compare_subject);
231 return (compare_from);
233 return (compare_size);
237 return (compare_score);
239 return (compare_spam);
246 void mutt_sort_headers (CONTEXT * ctx, int init)
250 THREAD *thread, *top;
253 unset_option (OPTNEEDRESORT);
258 if (!ctx->msgcount) {
259 /* this function gets called by mutt_sync_mailbox(), which may have just
260 * deleted all the messages. the virtual message numbers are not updated
261 * in that routine, so we must make sure to zero the vcount member.
264 mutt_clear_threads (ctx);
265 return; /* nothing to do! */
269 mutt_message _("Sorting mailbox...");
271 if (option (OPTNEEDRESCORE) && mod_score.enable) {
272 for (i = 0; i < ctx->msgcount; i++)
273 mutt_score_message (ctx, ctx->hdrs[i], 1);
275 unset_option (OPTNEEDRESCORE);
277 if (option (OPTRESORTINIT)) {
278 unset_option (OPTRESORTINIT);
282 if (init && ctx->tree)
283 mutt_clear_threads (ctx);
285 if ((Sort & SORT_MASK) == SORT_THREADS) {
287 /* if $sort_aux changed after the mailbox is sorted, then all the
288 subthreads need to be resorted */
289 if (option (OPTSORTSUBTHREADS)) {
293 ctx->tree = mutt_sort_subthreads (ctx->tree, 1);
295 unset_option (OPTSORTSUBTHREADS);
297 mutt_sort_threads (ctx, init);
299 else if ((sortfunc = mutt_get_sort_func (Sort)) == NULL ||
300 (AuxSort = mutt_get_sort_func (SortAux)) == NULL) {
301 mutt_error _("Could not find sorting function! [report this bug]");
307 qsort ((void *) ctx->hdrs, ctx->msgcount, sizeof (HEADER *), sortfunc);
309 /* adjust the virtual message numbers */
311 for (i = 0; i < ctx->msgcount; i++) {
312 HEADER *cur = ctx->hdrs[i];
314 if (cur->virtual != -1
315 || (cur->collapsed && (!ctx->pattern || cur->limited))) {
316 cur->virtual = ctx->vcount;
317 ctx->v2r[ctx->vcount] = i;
323 /* re-collapse threads marked as collapsed */
324 if ((Sort & SORT_MASK) == SORT_THREADS) {
326 while ((thread = top) != NULL) {
327 while (!thread->message)
328 thread = thread->child;
332 mutt_collapse_thread (ctx, h);
335 mutt_set_virtual (ctx);