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.
19 #include <lib-lib/lib-lib.h>
25 #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 = m_strcasecmp((*pa)->env->real_subj, (*pb)->env->real_subj);
87 return (SORTCODE (rc));
90 const char *mutt_get_name (address_t * 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 m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->to));
121 m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->to));
123 result = m_strcasecmp(fa, fb);
124 AUXSORT (result, a, b);
125 return (SORTCODE (result));
128 int compare_from (const void *a, const void *b)
130 HEADER **ppa = (HEADER **) a;
131 HEADER **ppb = (HEADER **) b;
136 /* mutt_get_name() will sometimes return a pointer to a static buffer.
137 * On the next call that pointer may get smashed so we copy the return value
138 * to our own memory space. */
140 m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->from));
141 m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->from));
143 result = m_strcasecmp(fa, fb);
144 AUXSORT (result, a, b);
145 return (SORTCODE (result));
148 int compare_date_received (const void *a, const void *b)
150 HEADER **pa = (HEADER **) a;
151 HEADER **pb = (HEADER **) b;
152 int result = (*pa)->received - (*pb)->received;
154 AUXSORT (result, a, b);
155 return (SORTCODE (result));
158 int compare_order (const void *a, const void *b)
160 HEADER **ha = (HEADER **) a;
161 HEADER **hb = (HEADER **) b;
164 if ((*ha)->article_num && (*hb)->article_num) {
165 int result = (*ha)->article_num - (*hb)->article_num;
167 AUXSORT (result, a, b);
168 return (SORTCODE (result));
172 /* no need to auxsort because you will never have equality here */
173 return (SORTCODE ((*ha)->index - (*hb)->index));
176 int compare_spam (const void *a, const void *b)
178 HEADER **ppa = (HEADER **) a;
179 HEADER **ppb = (HEADER **) b;
184 /* Firstly, require spam attributes for both msgs */
185 /* to compare. Determine which msgs have one. */
186 ahas = (*ppa)->env && (*ppa)->env->spam;
187 bhas = (*ppb)->env && (*ppb)->env->spam;
189 /* If one msg has spam attr but other does not, sort the one with first. */
191 return (SORTCODE (1));
193 return (SORTCODE (-1));
195 /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
196 if (!ahas && !bhas) {
197 AUXSORT (result, a, b);
198 return (SORTCODE (result));
202 /* Both have spam attrs. */
204 /* preliminary numeric examination */
205 result = (strtoul ((*ppa)->env->spam->data, &aptr, 10) -
206 strtoul ((*ppb)->env->spam->data, &bptr, 10));
208 /* If either aptr or bptr is equal to data, there is no numeric */
209 /* value for that spam attribute. In this case, compare lexically. */
210 if ((aptr == (*ppa)->env->spam->data) || (bptr == (*ppb)->env->spam->data))
211 return (SORTCODE (m_strcmp(aptr, bptr)));
213 /* Otherwise, we have numeric value for both attrs. If these values */
214 /* are equal, then we first fall back upon string comparison, then */
215 /* upon auxiliary sort. */
217 result = m_strcmp(aptr, bptr);
219 AUXSORT (result, a, b);
222 return (SORTCODE (result));
225 sort_t *mutt_get_sort_func (int method)
227 switch (method & SORT_MASK) {
229 return (compare_date_received);
231 return (compare_order);
233 return (compare_date_sent);
235 return (compare_subject);
237 return (compare_from);
239 return (compare_size);
243 return (compare_score);
245 return (compare_spam);
252 void mutt_sort_headers (CONTEXT * ctx, int init)
256 THREAD *thread, *top;
259 unset_option (OPTNEEDRESORT);
264 if (!ctx->msgcount) {
265 /* this function gets called by mutt_sync_mailbox(), which may have just
266 * deleted all the messages. the virtual message numbers are not updated
267 * in that routine, so we must make sure to zero the vcount member.
270 mutt_clear_threads (ctx);
271 return; /* nothing to do! */
275 mutt_message _("Sorting mailbox...");
277 if (option (OPTNEEDRESCORE) && option (OPTSCORE)) {
278 for (i = 0; i < ctx->msgcount; i++)
279 mutt_score_message (ctx, ctx->hdrs[i], 1);
281 unset_option (OPTNEEDRESCORE);
283 if (option (OPTRESORTINIT)) {
284 unset_option (OPTRESORTINIT);
288 if (init && ctx->tree)
289 mutt_clear_threads (ctx);
291 if ((Sort & SORT_MASK) == SORT_THREADS) {
293 /* if $sort_aux changed after the mailbox is sorted, then all the
294 subthreads need to be resorted */
295 if (option (OPTSORTSUBTHREADS)) {
299 ctx->tree = mutt_sort_subthreads (ctx->tree, 1);
301 unset_option (OPTSORTSUBTHREADS);
303 mutt_sort_threads (ctx, init);
305 else if ((sortfunc = mutt_get_sort_func (Sort)) == NULL ||
306 (AuxSort = mutt_get_sort_func (SortAux)) == NULL) {
307 mutt_error _("Could not find sorting function! [report this bug]");
313 qsort ((void *) ctx->hdrs, ctx->msgcount, sizeof (HEADER *), sortfunc);
315 /* adjust the virtual message numbers */
317 for (i = 0; i < ctx->msgcount; i++) {
318 HEADER *cur = ctx->hdrs[i];
320 if (cur->virtual != -1
321 || (cur->collapsed && (!ctx->pattern || cur->limited))) {
322 cur->virtual = ctx->vcount;
323 ctx->v2r[ctx->vcount] = i;
329 /* re-collapse threads marked as collapsed */
330 if ((Sort & SORT_MASK) == SORT_THREADS) {
332 while ((thread = top) != NULL) {
333 while (!thread->message)
334 thread = thread->child;
338 mutt_collapse_thread (ctx, h);
341 mutt_set_virtual (ctx);