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.
16 #include "mutt_idna.h"
25 #define SORTCODE(x) (Sort & SORT_REVERSE) ? -(x) : x
27 /* function to use as discriminator when normal sort method is equal */
28 static sort_t *AuxSort = NULL;
30 #define AUXSORT(code,a,b) if (!code && AuxSort && !option(OPTAUXSORT)) { \
31 set_option(OPTAUXSORT); \
32 code = AuxSort(a,b); \
33 unset_option(OPTAUXSORT); \
36 code = (*((HEADER **)a))->index - (*((HEADER **)b))->index;
38 int compare_score (const void *a, const void *b)
40 HEADER **pa = (HEADER **) a;
41 HEADER **pb = (HEADER **) b;
42 int result = (*pb)->score - (*pa)->score; /* note that this is reverse */
44 AUXSORT (result, a, b);
45 return (SORTCODE (result));
48 int compare_size (const void *a, const void *b)
50 HEADER **pa = (HEADER **) a;
51 HEADER **pb = (HEADER **) b;
52 int result = (*pa)->content->length - (*pb)->content->length;
54 AUXSORT (result, a, b);
55 return (SORTCODE (result));
58 int compare_date_sent (const void *a, const void *b)
60 HEADER **pa = (HEADER **) a;
61 HEADER **pb = (HEADER **) b;
62 int result = (*pa)->date_sent - (*pb)->date_sent;
64 AUXSORT (result, a, b);
65 return (SORTCODE (result));
68 int compare_subject (const void *a, const void *b)
70 HEADER **pa = (HEADER **) a;
71 HEADER **pb = (HEADER **) b;
74 if (!(*pa)->env->real_subj) {
75 if (!(*pb)->env->real_subj)
76 rc = compare_date_sent (pa, pb);
80 else if (!(*pb)->env->real_subj)
83 rc = mutt_strcasecmp ((*pa)->env->real_subj, (*pb)->env->real_subj);
85 return (SORTCODE (rc));
88 const char *mutt_get_name (ADDRESS * a)
91 const char *name = "";
94 if (option (OPTREVALIAS) && (ali = alias_reverse_lookup (a))
100 name = (mutt_addr_for_display (a));
102 /* don't return NULL to avoid segfault when printing/comparing */
106 int compare_to (const void *a, const void *b)
108 HEADER **ppa = (HEADER **) a;
109 HEADER **ppb = (HEADER **) b;
114 /* mutt_get_name() will sometimes return a pointer to a static buffer.
115 * On the next call that pointer may get smashed so we copy the return value
116 * to our own memory space. */
118 strncpy (fa, mutt_get_name ((*ppa)->env->to), sizeof (fa));
119 fa[sizeof (fa) - 1] = '\0';
121 strncpy (fb, mutt_get_name ((*ppb)->env->to), sizeof (fb));
122 fb[sizeof (fb) - 1] = '\0';
124 result = mutt_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 strncpy (fa, mutt_get_name ((*ppa)->env->from), sizeof (fa));
142 fa[sizeof (fa) - 1] = '\0';
144 strncpy (fb, mutt_get_name ((*ppb)->env->from), sizeof (fb));
145 fb[sizeof (fb) - 1] = '\0';
147 result = mutt_strcasecmp (fa, fb);
148 AUXSORT (result, a, b);
149 return (SORTCODE (result));
152 int compare_date_received (const void *a, const void *b)
154 HEADER **pa = (HEADER **) a;
155 HEADER **pb = (HEADER **) b;
156 int result = (*pa)->received - (*pb)->received;
158 AUXSORT (result, a, b);
159 return (SORTCODE (result));
162 int compare_order (const void *a, const void *b)
164 HEADER **ha = (HEADER **) a;
165 HEADER **hb = (HEADER **) b;
168 if ((*ha)->article_num && (*hb)->article_num) {
169 int result = (*ha)->article_num - (*hb)->article_num;
171 AUXSORT (result, a, b);
172 return (SORTCODE (result));
176 /* no need to auxsort because you will never have equality here */
177 return (SORTCODE ((*ha)->index - (*hb)->index));
180 int compare_spam (const void *a, const void *b)
182 HEADER **ppa = (HEADER **) a;
183 HEADER **ppb = (HEADER **) b;
188 /* Firstly, require spam attributes for both msgs */
189 /* to compare. Determine which msgs have one. */
190 ahas = (*ppa)->env && (*ppa)->env->spam;
191 bhas = (*ppb)->env && (*ppb)->env->spam;
193 /* If one msg has spam attr but other does not, sort the one with first. */
195 return (SORTCODE (1));
197 return (SORTCODE (-1));
199 /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
200 if (!ahas && !bhas) {
201 AUXSORT (result, a, b);
202 return (SORTCODE (result));
206 /* Both have spam attrs. */
208 /* preliminary numeric examination */
209 result = (strtoul ((*ppa)->env->spam->data, &aptr, 10) -
210 strtoul ((*ppb)->env->spam->data, &bptr, 10));
212 /* If either aptr or bptr is equal to data, there is no numeric */
213 /* value for that spam attribute. In this case, compare lexically. */
214 if ((aptr == (*ppa)->env->spam->data) || (bptr == (*ppb)->env->spam->data))
215 return (SORTCODE (strcmp (aptr, bptr)));
217 /* Otherwise, we have numeric value for both attrs. If these values */
218 /* are equal, then we first fall back upon string comparison, then */
219 /* upon auxiliary sort. */
221 result = strcmp (aptr, bptr);
223 AUXSORT (result, a, b);
226 return (SORTCODE (result));
229 sort_t *mutt_get_sort_func (int method)
231 switch (method & SORT_MASK) {
233 return (compare_date_received);
235 return (compare_order);
237 return (compare_date_sent);
239 return (compare_subject);
241 return (compare_from);
243 return (compare_size);
247 return (compare_score);
249 return (compare_spam);
256 void mutt_sort_headers (CONTEXT * ctx, int init)
260 THREAD *thread, *top;
263 unset_option (OPTNEEDRESORT);
268 if (!ctx->msgcount) {
269 /* this function gets called by mutt_sync_mailbox(), which may have just
270 * deleted all the messages. the virtual message numbers are not updated
271 * in that routine, so we must make sure to zero the vcount member.
274 mutt_clear_threads (ctx);
275 return; /* nothing to do! */
279 mutt_message _("Sorting mailbox...");
281 if (option (OPTNEEDRESCORE) && option (OPTSCORE)) {
282 for (i = 0; i < ctx->msgcount; i++)
283 mutt_score_message (ctx, ctx->hdrs[i], 1);
285 unset_option (OPTNEEDRESCORE);
287 if (option (OPTRESORTINIT)) {
288 unset_option (OPTRESORTINIT);
292 if (init && ctx->tree)
293 mutt_clear_threads (ctx);
295 if ((Sort & SORT_MASK) == SORT_THREADS) {
297 /* if $sort_aux changed after the mailbox is sorted, then all the
298 subthreads need to be resorted */
299 if (option (OPTSORTSUBTHREADS)) {
303 ctx->tree = mutt_sort_subthreads (ctx->tree, 1);
305 unset_option (OPTSORTSUBTHREADS);
307 mutt_sort_threads (ctx, init);
309 else if ((sortfunc = mutt_get_sort_func (Sort)) == NULL ||
310 (AuxSort = mutt_get_sort_func (SortAux)) == NULL) {
311 mutt_error _("Could not find sorting function! [report this bug]");
317 qsort ((void *) ctx->hdrs, ctx->msgcount, sizeof (HEADER *), sortfunc);
319 /* adjust the virtual message numbers */
321 for (i = 0; i < ctx->msgcount; i++) {
322 HEADER *cur = ctx->hdrs[i];
324 if (cur->virtual != -1
325 || (cur->collapsed && (!ctx->pattern || cur->limited))) {
326 cur->virtual = ctx->vcount;
327 ctx->v2r[ctx->vcount] = i;
333 /* re-collapse threads marked as collapsed */
334 if ((Sort & SORT_MASK) == SORT_THREADS) {
336 while ((thread = top) != NULL) {
337 while (!thread->message)
338 thread = thread->child;
342 mutt_collapse_thread (ctx, h);
345 mutt_set_virtual (ctx);