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>
16 #include "mutt_idna.h"
18 #define SORTCODE(x) (Sort & SORT_REVERSE) ? -(x) : x
20 /* function to use as discriminator when normal sort method is equal */
21 static sort_t *AuxSort = NULL;
23 #define AUXSORT(code,a,b) if (!code && AuxSort && !option(OPTAUXSORT)) { \
24 set_option(OPTAUXSORT); \
25 code = AuxSort(a,b); \
26 unset_option(OPTAUXSORT); \
29 code = (*((HEADER **)a))->index - (*((HEADER **)b))->index;
31 int compare_score (const void *a, const void *b)
33 HEADER **pa = (HEADER **) a;
34 HEADER **pb = (HEADER **) b;
35 int result = (*pb)->score - (*pa)->score; /* note that this is reverse */
37 AUXSORT (result, a, b);
38 return (SORTCODE (result));
41 int compare_size (const void *a, const void *b)
43 HEADER **pa = (HEADER **) a;
44 HEADER **pb = (HEADER **) b;
45 int result = (*pa)->content->length - (*pb)->content->length;
47 AUXSORT (result, a, b);
48 return (SORTCODE (result));
51 int compare_date_sent (const void *a, const void *b)
53 HEADER **pa = (HEADER **) a;
54 HEADER **pb = (HEADER **) b;
55 int result = (*pa)->date_sent - (*pb)->date_sent;
57 AUXSORT (result, a, b);
58 return (SORTCODE (result));
61 int compare_subject (const void *a, const void *b)
63 HEADER **pa = (HEADER **) a;
64 HEADER **pb = (HEADER **) b;
67 if (!(*pa)->env->real_subj) {
68 if (!(*pb)->env->real_subj)
69 rc = compare_date_sent (pa, pb);
73 else if (!(*pb)->env->real_subj)
76 rc = m_strcasecmp((*pa)->env->real_subj, (*pb)->env->real_subj);
78 return (SORTCODE (rc));
81 const char *mutt_get_name (address_t * a)
84 const char *name = "";
87 if (option (OPTREVALIAS) && (ali = alias_reverse_lookup(a))
93 name = (mutt_addr_for_display (a));
95 /* don't return NULL to avoid segfault when printing/comparing */
99 int compare_to (const void *a, const void *b)
101 HEADER **ppa = (HEADER **) a;
102 HEADER **ppb = (HEADER **) b;
107 /* mutt_get_name() will sometimes return a pointer to a static buffer.
108 * On the next call that pointer may get smashed so we copy the return value
109 * to our own memory space. */
111 m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->to));
112 m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->to));
114 result = m_strcasecmp(fa, fb);
115 AUXSORT (result, a, b);
116 return (SORTCODE (result));
119 int compare_from (const void *a, const void *b)
121 HEADER **ppa = (HEADER **) a;
122 HEADER **ppb = (HEADER **) b;
127 /* mutt_get_name() will sometimes return a pointer to a static buffer.
128 * On the next call that pointer may get smashed so we copy the return value
129 * to our own memory space. */
131 m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->from));
132 m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->from));
134 result = m_strcasecmp(fa, fb);
135 AUXSORT (result, a, b);
136 return (SORTCODE (result));
139 int compare_date_received (const void *a, const void *b)
141 HEADER **pa = (HEADER **) a;
142 HEADER **pb = (HEADER **) b;
143 int result = (*pa)->received - (*pb)->received;
145 AUXSORT (result, a, b);
146 return (SORTCODE (result));
149 int compare_order (const void *a, const void *b)
151 HEADER **ha = (HEADER **) a;
152 HEADER **hb = (HEADER **) b;
155 if ((*ha)->article_num && (*hb)->article_num) {
156 int result = (*ha)->article_num - (*hb)->article_num;
158 AUXSORT (result, a, b);
159 return (SORTCODE (result));
163 /* no need to auxsort because you will never have equality here */
164 return (SORTCODE ((*ha)->index - (*hb)->index));
167 int compare_spam (const void *a, const void *b)
169 HEADER **ppa = (HEADER **) a;
170 HEADER **ppb = (HEADER **) b;
175 /* Firstly, require spam attributes for both msgs */
176 /* to compare. Determine which msgs have one. */
177 ahas = (*ppa)->env && (*ppa)->env->spam;
178 bhas = (*ppb)->env && (*ppb)->env->spam;
180 /* If one msg has spam attr but other does not, sort the one with first. */
182 return (SORTCODE (1));
184 return (SORTCODE (-1));
186 /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
187 if (!ahas && !bhas) {
188 AUXSORT (result, a, b);
189 return (SORTCODE (result));
193 /* Both have spam attrs. */
195 /* preliminary numeric examination */
196 result = (strtoul ((*ppa)->env->spam->data, &aptr, 10) -
197 strtoul ((*ppb)->env->spam->data, &bptr, 10));
199 /* If either aptr or bptr is equal to data, there is no numeric */
200 /* value for that spam attribute. In this case, compare lexically. */
201 if ((aptr == (*ppa)->env->spam->data) || (bptr == (*ppb)->env->spam->data))
202 return (SORTCODE (m_strcmp(aptr, bptr)));
204 /* Otherwise, we have numeric value for both attrs. If these values */
205 /* are equal, then we first fall back upon string comparison, then */
206 /* upon auxiliary sort. */
208 result = m_strcmp(aptr, bptr);
210 AUXSORT (result, a, b);
213 return (SORTCODE (result));
216 sort_t *mutt_get_sort_func (int method)
218 switch (method & SORT_MASK) {
220 return (compare_date_received);
222 return (compare_order);
224 return (compare_date_sent);
226 return (compare_subject);
228 return (compare_from);
230 return (compare_size);
234 return (compare_score);
236 return (compare_spam);
243 void mutt_sort_headers (CONTEXT * ctx, int init)
247 THREAD *thread, *top;
250 unset_option (OPTNEEDRESORT);
255 if (!ctx->msgcount) {
256 /* this function gets called by mutt_sync_mailbox(), which may have just
257 * deleted all the messages. the virtual message numbers are not updated
258 * in that routine, so we must make sure to zero the vcount member.
261 mutt_clear_threads (ctx);
262 return; /* nothing to do! */
266 mutt_message _("Sorting mailbox...");
268 if (option (OPTNEEDRESCORE) && option (OPTSCORE)) {
269 for (i = 0; i < ctx->msgcount; i++)
270 mutt_score_message (ctx, ctx->hdrs[i], 1);
272 unset_option (OPTNEEDRESCORE);
274 if (option (OPTRESORTINIT)) {
275 unset_option (OPTRESORTINIT);
279 if (init && ctx->tree)
280 mutt_clear_threads (ctx);
282 if ((Sort & SORT_MASK) == SORT_THREADS) {
284 /* if $sort_aux changed after the mailbox is sorted, then all the
285 subthreads need to be resorted */
286 if (option (OPTSORTSUBTHREADS)) {
290 ctx->tree = mutt_sort_subthreads (ctx->tree, 1);
292 unset_option (OPTSORTSUBTHREADS);
294 mutt_sort_threads (ctx, init);
296 else if ((sortfunc = mutt_get_sort_func (Sort)) == NULL ||
297 (AuxSort = mutt_get_sort_func (SortAux)) == NULL) {
298 mutt_error _("Could not find sorting function! [report this bug]");
304 qsort ((void *) ctx->hdrs, ctx->msgcount, sizeof (HEADER *), sortfunc);
306 /* adjust the virtual message numbers */
308 for (i = 0; i < ctx->msgcount; i++) {
309 HEADER *cur = ctx->hdrs[i];
311 if (cur->virtual != -1
312 || (cur->collapsed && (!ctx->pattern || cur->limited))) {
313 cur->virtual = ctx->vcount;
314 ctx->v2r[ctx->vcount] = i;
320 /* re-collapse threads marked as collapsed */
321 if ((Sort & SORT_MASK) == SORT_THREADS) {
323 while ((thread = top) != NULL) {
324 while (!thread->message)
325 thread = thread->child;
329 mutt_collapse_thread (ctx, h);
332 mutt_set_virtual (ctx);