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.
14 #include <lib-lib/macros.h>
15 #include <lib-lib/str.h>
20 #include "mutt_idna.h"
28 #define SORTCODE(x) (Sort & SORT_REVERSE) ? -(x) : x
30 /* function to use as discriminator when normal sort method is equal */
31 static sort_t *AuxSort = NULL;
33 #define AUXSORT(code,a,b) if (!code && AuxSort && !option(OPTAUXSORT)) { \
34 set_option(OPTAUXSORT); \
35 code = AuxSort(a,b); \
36 unset_option(OPTAUXSORT); \
39 code = (*((HEADER **)a))->index - (*((HEADER **)b))->index;
41 int compare_score (const void *a, const void *b)
43 HEADER **pa = (HEADER **) a;
44 HEADER **pb = (HEADER **) b;
45 int result = (*pb)->score - (*pa)->score; /* note that this is reverse */
47 AUXSORT (result, a, b);
48 return (SORTCODE (result));
51 int compare_size (const void *a, const void *b)
53 HEADER **pa = (HEADER **) a;
54 HEADER **pb = (HEADER **) b;
55 int result = (*pa)->content->length - (*pb)->content->length;
57 AUXSORT (result, a, b);
58 return (SORTCODE (result));
61 int compare_date_sent (const void *a, const void *b)
63 HEADER **pa = (HEADER **) a;
64 HEADER **pb = (HEADER **) b;
65 int result = (*pa)->date_sent - (*pb)->date_sent;
67 AUXSORT (result, a, b);
68 return (SORTCODE (result));
71 int compare_subject (const void *a, const void *b)
73 HEADER **pa = (HEADER **) a;
74 HEADER **pb = (HEADER **) b;
77 if (!(*pa)->env->real_subj) {
78 if (!(*pb)->env->real_subj)
79 rc = compare_date_sent (pa, pb);
83 else if (!(*pb)->env->real_subj)
86 rc = m_strcasecmp((*pa)->env->real_subj, (*pb)->env->real_subj);
88 return (SORTCODE (rc));
91 const char *mutt_get_name (address_t * a)
94 const char *name = "";
97 if (option (OPTREVALIAS) && (ali = alias_reverse_lookup (a))
100 else if (a->personal)
103 name = (mutt_addr_for_display (a));
105 /* don't return NULL to avoid segfault when printing/comparing */
109 int compare_to (const void *a, const void *b)
111 HEADER **ppa = (HEADER **) a;
112 HEADER **ppb = (HEADER **) b;
117 /* mutt_get_name() will sometimes return a pointer to a static buffer.
118 * On the next call that pointer may get smashed so we copy the return value
119 * to our own memory space. */
121 m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->to));
122 m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->to));
124 result = m_strcasecmp(fa, fb);
125 AUXSORT (result, a, b);
126 return (SORTCODE (result));
129 int compare_from (const void *a, const void *b)
131 HEADER **ppa = (HEADER **) a;
132 HEADER **ppb = (HEADER **) b;
137 /* mutt_get_name() will sometimes return a pointer to a static buffer.
138 * On the next call that pointer may get smashed so we copy the return value
139 * to our own memory space. */
141 m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->from));
142 m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->from));
144 result = m_strcasecmp(fa, fb);
145 AUXSORT (result, a, b);
146 return (SORTCODE (result));
149 int compare_date_received (const void *a, const void *b)
151 HEADER **pa = (HEADER **) a;
152 HEADER **pb = (HEADER **) b;
153 int result = (*pa)->received - (*pb)->received;
155 AUXSORT (result, a, b);
156 return (SORTCODE (result));
159 int compare_order (const void *a, const void *b)
161 HEADER **ha = (HEADER **) a;
162 HEADER **hb = (HEADER **) b;
165 if ((*ha)->article_num && (*hb)->article_num) {
166 int result = (*ha)->article_num - (*hb)->article_num;
168 AUXSORT (result, a, b);
169 return (SORTCODE (result));
173 /* no need to auxsort because you will never have equality here */
174 return (SORTCODE ((*ha)->index - (*hb)->index));
177 int compare_spam (const void *a, const void *b)
179 HEADER **ppa = (HEADER **) a;
180 HEADER **ppb = (HEADER **) b;
185 /* Firstly, require spam attributes for both msgs */
186 /* to compare. Determine which msgs have one. */
187 ahas = (*ppa)->env && (*ppa)->env->spam;
188 bhas = (*ppb)->env && (*ppb)->env->spam;
190 /* If one msg has spam attr but other does not, sort the one with first. */
192 return (SORTCODE (1));
194 return (SORTCODE (-1));
196 /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
197 if (!ahas && !bhas) {
198 AUXSORT (result, a, b);
199 return (SORTCODE (result));
203 /* Both have spam attrs. */
205 /* preliminary numeric examination */
206 result = (strtoul ((*ppa)->env->spam->data, &aptr, 10) -
207 strtoul ((*ppb)->env->spam->data, &bptr, 10));
209 /* If either aptr or bptr is equal to data, there is no numeric */
210 /* value for that spam attribute. In this case, compare lexically. */
211 if ((aptr == (*ppa)->env->spam->data) || (bptr == (*ppb)->env->spam->data))
212 return (SORTCODE (m_strcmp(aptr, bptr)));
214 /* Otherwise, we have numeric value for both attrs. If these values */
215 /* are equal, then we first fall back upon string comparison, then */
216 /* upon auxiliary sort. */
218 result = m_strcmp(aptr, bptr);
220 AUXSORT (result, a, b);
223 return (SORTCODE (result));
226 sort_t *mutt_get_sort_func (int method)
228 switch (method & SORT_MASK) {
230 return (compare_date_received);
232 return (compare_order);
234 return (compare_date_sent);
236 return (compare_subject);
238 return (compare_from);
240 return (compare_size);
244 return (compare_score);
246 return (compare_spam);
253 void mutt_sort_headers (CONTEXT * ctx, int init)
257 THREAD *thread, *top;
260 unset_option (OPTNEEDRESORT);
265 if (!ctx->msgcount) {
266 /* this function gets called by mutt_sync_mailbox(), which may have just
267 * deleted all the messages. the virtual message numbers are not updated
268 * in that routine, so we must make sure to zero the vcount member.
271 mutt_clear_threads (ctx);
272 return; /* nothing to do! */
276 mutt_message _("Sorting mailbox...");
278 if (option (OPTNEEDRESCORE) && option (OPTSCORE)) {
279 for (i = 0; i < ctx->msgcount; i++)
280 mutt_score_message (ctx, ctx->hdrs[i], 1);
282 unset_option (OPTNEEDRESCORE);
284 if (option (OPTRESORTINIT)) {
285 unset_option (OPTRESORTINIT);
289 if (init && ctx->tree)
290 mutt_clear_threads (ctx);
292 if ((Sort & SORT_MASK) == SORT_THREADS) {
294 /* if $sort_aux changed after the mailbox is sorted, then all the
295 subthreads need to be resorted */
296 if (option (OPTSORTSUBTHREADS)) {
300 ctx->tree = mutt_sort_subthreads (ctx->tree, 1);
302 unset_option (OPTSORTSUBTHREADS);
304 mutt_sort_threads (ctx, init);
306 else if ((sortfunc = mutt_get_sort_func (Sort)) == NULL ||
307 (AuxSort = mutt_get_sort_func (SortAux)) == NULL) {
308 mutt_error _("Could not find sorting function! [report this bug]");
314 qsort ((void *) ctx->hdrs, ctx->msgcount, sizeof (HEADER *), sortfunc);
316 /* adjust the virtual message numbers */
318 for (i = 0; i < ctx->msgcount; i++) {
319 HEADER *cur = ctx->hdrs[i];
321 if (cur->virtual != -1
322 || (cur->collapsed && (!ctx->pattern || cur->limited))) {
323 cur->virtual = ctx->vcount;
324 ctx->v2r[ctx->vcount] = i;
330 /* re-collapse threads marked as collapsed */
331 if ((Sort & SORT_MASK) == SORT_THREADS) {
333 while ((thread = top) != NULL) {
334 while (!thread->message)
335 thread = thread->child;
339 mutt_collapse_thread (ctx, h);
342 mutt_set_virtual (ctx);