2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "mutt_idna.h"
32 #define SORTCODE(x) ((option(OPTAUXSORT) ? SortAux : Sort) & SORT_REVERSE) ? -(x) : x
34 /* function to use as discriminator when normal sort method is equal */
35 static sort_t *AuxSort = NULL;
37 #define AUXSORT(code,a,b) \
38 if (!code && AuxSort && !option(OPTAUXSORT)) \
40 set_option(OPTAUXSORT); \
41 code = AuxSort(a,b); \
42 unset_option(OPTAUXSORT); \
47 code = (*((HEADER **)a))->index - (*((HEADER **)b))->index;
49 static int compare_score (const void *a, const void *b)
51 HEADER **pa = (HEADER **) a;
52 HEADER **pb = (HEADER **) b;
53 int result = (*pb)->score - (*pa)->score; /* note that this is reverse */
55 return (SORTCODE (result));
58 static int compare_size (const void *a, const void *b)
60 HEADER **pa = (HEADER **) a;
61 HEADER **pb = (HEADER **) b;
62 int result = (*pa)->content->length - (*pb)->content->length;
64 return (SORTCODE (result));
67 static int compare_date_sent (const void *a, const void *b)
69 HEADER **pa = (HEADER **) a;
70 HEADER **pb = (HEADER **) b;
71 int result = (*pa)->date_sent - (*pb)->date_sent;
73 return (SORTCODE (result));
76 static int compare_subject (const void *a, const void *b)
78 HEADER **pa = (HEADER **) a;
79 HEADER **pb = (HEADER **) b;
82 if (!(*pa)->env->real_subj)
84 if (!(*pb)->env->real_subj)
85 rc = compare_date_sent (pa, pb);
89 else if (!(*pb)->env->real_subj)
92 rc = mutt_strcasecmp ((*pa)->env->real_subj, (*pb)->env->real_subj);
94 return (SORTCODE (rc));
97 const char *mutt_get_name (ADDRESS *a)
103 if (option (OPTREVALIAS) && (ali = alias_reverse_lookup (a)) && ali->personal)
104 return ali->personal;
105 else if (a->personal)
108 return (mutt_addr_for_display (a));
110 /* don't return NULL to avoid segfault when printing/comparing */
114 static int compare_to (const void *a, const void *b)
116 HEADER **ppa = (HEADER **) a;
117 HEADER **ppb = (HEADER **) b;
118 char fa[SHORT_STRING];
122 strfcpy (fa, mutt_get_name ((*ppa)->env->to), SHORT_STRING);
123 fb = mutt_get_name ((*ppb)->env->to);
124 result = mutt_strncasecmp (fa, fb, SHORT_STRING);
126 return (SORTCODE (result));
129 static int compare_from (const void *a, const void *b)
131 HEADER **ppa = (HEADER **) a;
132 HEADER **ppb = (HEADER **) b;
133 char fa[SHORT_STRING];
137 strfcpy (fa, mutt_get_name ((*ppa)->env->from), SHORT_STRING);
138 fb = mutt_get_name ((*ppb)->env->from);
139 result = mutt_strncasecmp (fa, fb, SHORT_STRING);
141 return (SORTCODE (result));
144 static int compare_date_received (const void *a, const void *b)
146 HEADER **pa = (HEADER **) a;
147 HEADER **pb = (HEADER **) b;
148 int result = (*pa)->received - (*pb)->received;
150 return (SORTCODE (result));
153 static int compare_order (const void *a, const void *b)
155 HEADER **ha = (HEADER **) a;
156 HEADER **hb = (HEADER **) b;
158 /* no need to auxsort because you will never have equality here */
159 return (SORTCODE ((*ha)->index - (*hb)->index));
162 static int compare_spam (const void *a, const void *b)
164 HEADER **ppa = (HEADER **) a;
165 HEADER **ppb = (HEADER **) b;
171 /* Firstly, require spam attributes for both msgs */
172 /* to compare. Determine which msgs have one. */
173 ahas = (*ppa)->env && (*ppa)->env->spam;
174 bhas = (*ppb)->env && (*ppb)->env->spam;
176 /* If one msg has spam attr but other does not, sort the one with first. */
178 return (SORTCODE(1));
180 return (SORTCODE(-1));
182 /* Else, if neither has a spam attr, presume equality. Fall back on aux. */
185 AUXSORT(result, a, b);
186 return (SORTCODE(result));
190 /* Both have spam attrs. */
192 /* preliminary numeric examination */
193 difference = (strtod((*ppa)->env->spam->data, &aptr) -
194 strtod((*ppb)->env->spam->data, &bptr));
196 /* map double into comparison (-1, 0, or 1) */
197 result = (difference < 0.0 ? -1 : difference > 0.0 ? 1 : 0);
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(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. */
209 result = strcmp(aptr, bptr);
210 AUXSORT(result, a, b);
213 return (SORTCODE(result));
216 int compare_label (const void *a, const void *b)
218 HEADER **ppa = (HEADER **) a;
219 HEADER **ppb = (HEADER **) b;
220 int ahas, bhas, result = 0;
222 /* As with compare_spam, not all messages will have the x-label
223 * property. Blank X-Labels are treated as null in the index
224 * display, so we'll consider them as null for sort, too. */
225 ahas = (*ppa)->env && (*ppa)->env->x_label && *((*ppa)->env->x_label);
226 bhas = (*ppb)->env && (*ppb)->env->x_label && *((*ppb)->env->x_label);
228 /* First we bias toward a message with a label, if the other does not. */
230 return (SORTCODE(-1));
232 return (SORTCODE(1));
234 /* If neither has a label, use aux sort. */
237 AUXSORT(result, a, b);
238 return (SORTCODE(result));
241 /* If both have a label, we just do a lexical compare. */
242 result = mutt_strcasecmp((*ppa)->env->x_label, (*ppb)->env->x_label);
243 return (SORTCODE(result));
246 sort_t *mutt_get_sort_func (int method)
248 switch (method & SORT_MASK)
251 return (compare_date_received);
253 return (compare_order);
255 return (compare_date_sent);
257 return (compare_subject);
259 return (compare_from);
261 return (compare_size);
265 return (compare_score);
267 return (compare_spam);
269 return (compare_label);
276 void mutt_sort_headers (CONTEXT *ctx, int init)
280 THREAD *thread, *top;
283 unset_option (OPTNEEDRESORT);
290 /* this function gets called by mutt_sync_mailbox(), which may have just
291 * deleted all the messages. the virtual message numbers are not updated
292 * in that routine, so we must make sure to zero the vcount member.
296 mutt_clear_threads (ctx);
297 return; /* nothing to do! */
301 mutt_message _("Sorting mailbox...");
303 if (option (OPTNEEDRESCORE) && option (OPTSCORE))
305 for (i = 0; i < ctx->msgcount; i++)
306 mutt_score_message (ctx, ctx->hdrs[i], 1);
308 unset_option (OPTNEEDRESCORE);
310 if (option (OPTRESORTINIT))
312 unset_option (OPTRESORTINIT);
316 if (init && ctx->tree)
317 mutt_clear_threads (ctx);
319 if ((Sort & SORT_MASK) == SORT_THREADS)
322 /* if $sort_aux changed after the mailbox is sorted, then all the
323 subthreads need to be resorted */
324 if (option (OPTSORTSUBTHREADS))
329 ctx->tree = mutt_sort_subthreads (ctx->tree, 1);
331 unset_option (OPTSORTSUBTHREADS);
333 mutt_sort_threads (ctx, init);
335 else if ((sortfunc = mutt_get_sort_func (Sort)) == NULL ||
336 (AuxSort = mutt_get_sort_func (SortAux)) == NULL)
338 mutt_error _("Could not find sorting function! [report this bug]");
343 qsort ((void *) ctx->hdrs, ctx->msgcount, sizeof (HEADER *), sortfunc);
345 /* adjust the virtual message numbers */
347 for (i = 0; i < ctx->msgcount; i++)
349 HEADER *cur = ctx->hdrs[i];
350 if (cur->virtual != -1 || (cur->collapsed && (!ctx->pattern || cur->limited)))
352 cur->virtual = ctx->vcount;
353 ctx->v2r[ctx->vcount] = i;
359 /* re-collapse threads marked as collapsed */
360 if ((Sort & SORT_MASK) == SORT_THREADS)
363 while ((thread = top) != NULL)
365 while (!thread->message)
366 thread = thread->child;
370 mutt_collapse_thread (ctx, h);
373 mutt_set_virtual (ctx);