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 = str_casecmp ((*pa)->env->real_subj, (*pb)->env->real_subj);
88 return (SORTCODE (rc));
91 const char *mutt_get_name (ADDRESS * 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 strncpy (fa, mutt_get_name ((*ppa)->env->to), sizeof (fa));
122 fa[sizeof (fa) - 1] = '\0';
124 strncpy (fb, mutt_get_name ((*ppb)->env->to), sizeof (fb));
125 fb[sizeof (fb) - 1] = '\0';
127 result = str_casecmp (fa, fb);
128 AUXSORT (result, a, b);
129 return (SORTCODE (result));
132 int compare_from (const void *a, const void *b)
134 HEADER **ppa = (HEADER **) a;
135 HEADER **ppb = (HEADER **) b;
140 /* mutt_get_name() will sometimes return a pointer to a static buffer.
141 * On the next call that pointer may get smashed so we copy the return value
142 * to our own memory space. */
144 strncpy (fa, mutt_get_name ((*ppa)->env->from), sizeof (fa));
145 fa[sizeof (fa) - 1] = '\0';
147 strncpy (fb, mutt_get_name ((*ppb)->env->from), sizeof (fb));
148 fb[sizeof (fb) - 1] = '\0';
150 result = str_casecmp (fa, fb);
151 AUXSORT (result, a, b);
152 return (SORTCODE (result));
155 int compare_date_received (const void *a, const void *b)
157 HEADER **pa = (HEADER **) a;
158 HEADER **pb = (HEADER **) b;
159 int result = (*pa)->received - (*pb)->received;
161 AUXSORT (result, a, b);
162 return (SORTCODE (result));
165 int compare_order (const void *a, const void *b)
167 HEADER **ha = (HEADER **) a;
168 HEADER **hb = (HEADER **) b;
171 if ((*ha)->article_num && (*hb)->article_num) {
172 int result = (*ha)->article_num - (*hb)->article_num;
174 AUXSORT (result, a, b);
175 return (SORTCODE (result));
179 /* no need to auxsort because you will never have equality here */
180 return (SORTCODE ((*ha)->index - (*hb)->index));
183 int compare_spam (const void *a, const void *b)
185 HEADER **ppa = (HEADER **) a;
186 HEADER **ppb = (HEADER **) b;
191 /* Firstly, require spam attributes for both msgs */
192 /* to compare. Determine which msgs have one. */
193 ahas = (*ppa)->env && (*ppa)->env->spam;
194 bhas = (*ppb)->env && (*ppb)->env->spam;
196 /* If one msg has spam attr but other does not, sort the one with first. */
198 return (SORTCODE (1));
200 return (SORTCODE (-1));
202 /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
203 if (!ahas && !bhas) {
204 AUXSORT (result, a, b);
205 return (SORTCODE (result));
209 /* Both have spam attrs. */
211 /* preliminary numeric examination */
212 result = (strtoul ((*ppa)->env->spam->data, &aptr, 10) -
213 strtoul ((*ppb)->env->spam->data, &bptr, 10));
215 /* If either aptr or bptr is equal to data, there is no numeric */
216 /* value for that spam attribute. In this case, compare lexically. */
217 if ((aptr == (*ppa)->env->spam->data) || (bptr == (*ppb)->env->spam->data))
218 return (SORTCODE (str_cmp (aptr, bptr)));
220 /* Otherwise, we have numeric value for both attrs. If these values */
221 /* are equal, then we first fall back upon string comparison, then */
222 /* upon auxiliary sort. */
224 result = str_cmp (aptr, bptr);
226 AUXSORT (result, a, b);
229 return (SORTCODE (result));
232 sort_t *mutt_get_sort_func (int method)
234 switch (method & SORT_MASK) {
236 return (compare_date_received);
238 return (compare_order);
240 return (compare_date_sent);
242 return (compare_subject);
244 return (compare_from);
246 return (compare_size);
250 return (compare_score);
252 return (compare_spam);
259 void mutt_sort_headers (CONTEXT * ctx, int init)
263 THREAD *thread, *top;
266 unset_option (OPTNEEDRESORT);
271 if (!ctx->msgcount) {
272 /* this function gets called by mutt_sync_mailbox(), which may have just
273 * deleted all the messages. the virtual message numbers are not updated
274 * in that routine, so we must make sure to zero the vcount member.
277 mutt_clear_threads (ctx);
278 return; /* nothing to do! */
282 mutt_message _("Sorting mailbox...");
284 if (option (OPTNEEDRESCORE) && option (OPTSCORE)) {
285 for (i = 0; i < ctx->msgcount; i++)
286 mutt_score_message (ctx, ctx->hdrs[i], 1);
288 unset_option (OPTNEEDRESCORE);
290 if (option (OPTRESORTINIT)) {
291 unset_option (OPTRESORTINIT);
295 if (init && ctx->tree)
296 mutt_clear_threads (ctx);
298 if ((Sort & SORT_MASK) == SORT_THREADS) {
300 /* if $sort_aux changed after the mailbox is sorted, then all the
301 subthreads need to be resorted */
302 if (option (OPTSORTSUBTHREADS)) {
306 ctx->tree = mutt_sort_subthreads (ctx->tree, 1);
308 unset_option (OPTSORTSUBTHREADS);
310 mutt_sort_threads (ctx, init);
312 else if ((sortfunc = mutt_get_sort_func (Sort)) == NULL ||
313 (AuxSort = mutt_get_sort_func (SortAux)) == NULL) {
314 mutt_error _("Could not find sorting function! [report this bug]");
320 qsort ((void *) ctx->hdrs, ctx->msgcount, sizeof (HEADER *), sortfunc);
322 /* adjust the virtual message numbers */
324 for (i = 0; i < ctx->msgcount; i++) {
325 HEADER *cur = ctx->hdrs[i];
327 if (cur->virtual != -1
328 || (cur->collapsed && (!ctx->pattern || cur->limited))) {
329 cur->virtual = ctx->vcount;
330 ctx->v2r[ctx->vcount] = i;
336 /* re-collapse threads marked as collapsed */
337 if ((Sort & SORT_MASK) == SORT_THREADS) {
339 while ((thread = top) != NULL) {
340 while (!thread->message)
341 thread = thread->child;
345 mutt_collapse_thread (ctx, h);
348 mutt_set_virtual (ctx);