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.
17 #include "mutt_idna.h"
26 #define SORTCODE(x) (Sort & SORT_REVERSE) ? -(x) : x
28 /* function to use as discriminator when normal sort method is equal */
29 static sort_t *AuxSort = NULL;
31 #define AUXSORT(code,a,b) if (!code && AuxSort && !option(OPTAUXSORT)) { \
32 set_option(OPTAUXSORT); \
33 code = AuxSort(a,b); \
34 unset_option(OPTAUXSORT); \
37 code = (*((HEADER **)a))->index - (*((HEADER **)b))->index;
39 int compare_score (const void *a, const void *b)
41 HEADER **pa = (HEADER **) a;
42 HEADER **pb = (HEADER **) b;
43 int result = (*pb)->score - (*pa)->score; /* note that this is reverse */
45 AUXSORT (result, a, b);
46 return (SORTCODE (result));
49 int compare_size (const void *a, const void *b)
51 HEADER **pa = (HEADER **) a;
52 HEADER **pb = (HEADER **) b;
53 int result = (*pa)->content->length - (*pb)->content->length;
55 AUXSORT (result, a, b);
56 return (SORTCODE (result));
59 int compare_date_sent (const void *a, const void *b)
61 HEADER **pa = (HEADER **) a;
62 HEADER **pb = (HEADER **) b;
63 int result = (*pa)->date_sent - (*pb)->date_sent;
65 AUXSORT (result, a, b);
66 return (SORTCODE (result));
69 int compare_subject (const void *a, const void *b)
71 HEADER **pa = (HEADER **) a;
72 HEADER **pb = (HEADER **) b;
75 if (!(*pa)->env->real_subj) {
76 if (!(*pb)->env->real_subj)
77 rc = compare_date_sent (pa, pb);
81 else if (!(*pb)->env->real_subj)
84 rc = safe_strcasecmp ((*pa)->env->real_subj, (*pb)->env->real_subj);
86 return (SORTCODE (rc));
89 const char *mutt_get_name (ADDRESS * a)
92 const char *name = "";
95 if (option (OPTREVALIAS) && (ali = alias_reverse_lookup (a))
101 name = (mutt_addr_for_display (a));
103 /* don't return NULL to avoid segfault when printing/comparing */
107 int compare_to (const void *a, const void *b)
109 HEADER **ppa = (HEADER **) a;
110 HEADER **ppb = (HEADER **) b;
115 /* mutt_get_name() will sometimes return a pointer to a static buffer.
116 * On the next call that pointer may get smashed so we copy the return value
117 * to our own memory space. */
119 strncpy (fa, mutt_get_name ((*ppa)->env->to), sizeof (fa));
120 fa[sizeof (fa) - 1] = '\0';
122 strncpy (fb, mutt_get_name ((*ppb)->env->to), sizeof (fb));
123 fb[sizeof (fb) - 1] = '\0';
125 result = safe_strcasecmp (fa, fb);
126 AUXSORT (result, a, b);
127 return (SORTCODE (result));
130 int compare_from (const void *a, const void *b)
132 HEADER **ppa = (HEADER **) a;
133 HEADER **ppb = (HEADER **) b;
138 /* mutt_get_name() will sometimes return a pointer to a static buffer.
139 * On the next call that pointer may get smashed so we copy the return value
140 * to our own memory space. */
142 strncpy (fa, mutt_get_name ((*ppa)->env->from), sizeof (fa));
143 fa[sizeof (fa) - 1] = '\0';
145 strncpy (fb, mutt_get_name ((*ppb)->env->from), sizeof (fb));
146 fb[sizeof (fb) - 1] = '\0';
148 result = safe_strcasecmp (fa, fb);
149 AUXSORT (result, a, b);
150 return (SORTCODE (result));
153 int compare_date_received (const void *a, const void *b)
155 HEADER **pa = (HEADER **) a;
156 HEADER **pb = (HEADER **) b;
157 int result = (*pa)->received - (*pb)->received;
159 AUXSORT (result, a, b);
160 return (SORTCODE (result));
163 int compare_order (const void *a, const void *b)
165 HEADER **ha = (HEADER **) a;
166 HEADER **hb = (HEADER **) b;
169 if ((*ha)->article_num && (*hb)->article_num) {
170 int result = (*ha)->article_num - (*hb)->article_num;
172 AUXSORT (result, a, b);
173 return (SORTCODE (result));
177 /* no need to auxsort because you will never have equality here */
178 return (SORTCODE ((*ha)->index - (*hb)->index));
181 int compare_spam (const void *a, const void *b)
183 HEADER **ppa = (HEADER **) a;
184 HEADER **ppb = (HEADER **) b;
189 /* Firstly, require spam attributes for both msgs */
190 /* to compare. Determine which msgs have one. */
191 ahas = (*ppa)->env && (*ppa)->env->spam;
192 bhas = (*ppb)->env && (*ppb)->env->spam;
194 /* If one msg has spam attr but other does not, sort the one with first. */
196 return (SORTCODE (1));
198 return (SORTCODE (-1));
200 /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
201 if (!ahas && !bhas) {
202 AUXSORT (result, a, b);
203 return (SORTCODE (result));
207 /* Both have spam attrs. */
209 /* preliminary numeric examination */
210 result = (strtoul ((*ppa)->env->spam->data, &aptr, 10) -
211 strtoul ((*ppb)->env->spam->data, &bptr, 10));
213 /* If either aptr or bptr is equal to data, there is no numeric */
214 /* value for that spam attribute. In this case, compare lexically. */
215 if ((aptr == (*ppa)->env->spam->data) || (bptr == (*ppb)->env->spam->data))
216 return (SORTCODE (safe_strcmp (aptr, bptr)));
218 /* Otherwise, we have numeric value for both attrs. If these values */
219 /* are equal, then we first fall back upon string comparison, then */
220 /* upon auxiliary sort. */
222 result = safe_strcmp (aptr, bptr);
224 AUXSORT (result, a, b);
227 return (SORTCODE (result));
230 sort_t *mutt_get_sort_func (int method)
232 switch (method & SORT_MASK) {
234 return (compare_date_received);
236 return (compare_order);
238 return (compare_date_sent);
240 return (compare_subject);
242 return (compare_from);
244 return (compare_size);
248 return (compare_score);
250 return (compare_spam);
257 void mutt_sort_headers (CONTEXT * ctx, int init)
261 THREAD *thread, *top;
264 unset_option (OPTNEEDRESORT);
269 if (!ctx->msgcount) {
270 /* this function gets called by mutt_sync_mailbox(), which may have just
271 * deleted all the messages. the virtual message numbers are not updated
272 * in that routine, so we must make sure to zero the vcount member.
275 mutt_clear_threads (ctx);
276 return; /* nothing to do! */
280 mutt_message _("Sorting mailbox...");
282 if (option (OPTNEEDRESCORE) && option (OPTSCORE)) {
283 for (i = 0; i < ctx->msgcount; i++)
284 mutt_score_message (ctx, ctx->hdrs[i], 1);
286 unset_option (OPTNEEDRESCORE);
288 if (option (OPTRESORTINIT)) {
289 unset_option (OPTRESORTINIT);
293 if (init && ctx->tree)
294 mutt_clear_threads (ctx);
296 if ((Sort & SORT_MASK) == SORT_THREADS) {
298 /* if $sort_aux changed after the mailbox is sorted, then all the
299 subthreads need to be resorted */
300 if (option (OPTSORTSUBTHREADS)) {
304 ctx->tree = mutt_sort_subthreads (ctx->tree, 1);
306 unset_option (OPTSORTSUBTHREADS);
308 mutt_sort_threads (ctx, init);
310 else if ((sortfunc = mutt_get_sort_func (Sort)) == NULL ||
311 (AuxSort = mutt_get_sort_func (SortAux)) == NULL) {
312 mutt_error _("Could not find sorting function! [report this bug]");
318 qsort ((void *) ctx->hdrs, ctx->msgcount, sizeof (HEADER *), sortfunc);
320 /* adjust the virtual message numbers */
322 for (i = 0; i < ctx->msgcount; i++) {
323 HEADER *cur = ctx->hdrs[i];
325 if (cur->virtual != -1
326 || (cur->collapsed && (!ctx->pattern || cur->limited))) {
327 cur->virtual = ctx->vcount;
328 ctx->v2r[ctx->vcount] = i;
334 /* re-collapse threads marked as collapsed */
335 if ((Sort & SORT_MASK) == SORT_THREADS) {
337 while ((thread = top) != NULL) {
338 while (!thread->message)
339 thread = thread->child;
343 mutt_collapse_thread (ctx, h);
346 mutt_set_virtual (ctx);