2 * Copyright (C) 1996-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 #define VISIBLE(hdr, ctx) (hdr->virtual >= 0 || (hdr->collapsed && (!ctx->pattern || hdr->limited)))
31 /* determine whether a is a descendant of b */
32 static int is_descendant (THREAD *a, THREAD *b)
43 /* Determines whether to display a message's subject. */
44 static int need_display_subject (CONTEXT *ctx, HEADER *hdr)
46 THREAD *tmp, *tree = hdr->thread;
48 /* if the user disabled subject hiding, display it */
49 if (!option (OPTHIDETHREADSUBJECT))
52 /* if our subject is different from our parent's, display it */
53 if (hdr->subject_changed)
56 /* if our subject is different from that of our closest previously displayed
57 * sibling, display the subject */
58 for (tmp = tree->prev; tmp; tmp = tmp->prev)
61 if (hdr && VISIBLE (hdr, ctx))
63 if (hdr->subject_changed)
70 /* if there is a parent-to-child subject change anywhere between us and our
71 * closest displayed ancestor, display the subject */
72 for (tmp = tree->parent; tmp; tmp = tmp->parent)
77 if (VISIBLE (hdr, ctx))
79 else if (hdr->subject_changed)
84 /* if we have no visible parent or previous sibling, display the subject */
88 static void linearize_tree (CONTEXT *ctx)
90 THREAD *tree = ctx->tree;
91 HEADER **array = ctx->hdrs + (Sort & SORT_REVERSE ? ctx->msgcount - 1 : 0);
95 while (!tree->message)
98 *array = tree->message;
99 array += Sort & SORT_REVERSE ? -1 : 1;
119 /* this calculates whether a node is the root of a subtree that has visible
120 * nodes, whether a node itself is visible, whether, if invisible, it has
121 * depth anyway, and whether any of its later siblings are roots of visible
122 * subtrees. while it's at it, it frees the old thread display, so we can
123 * skip parts of the tree in mutt_draw_tree() if we've decided here that we
124 * don't care about them any more.
126 static void calculate_visibility (CONTEXT *ctx, int *max_depth)
128 THREAD *tmp, *tree = ctx->tree;
129 int hide_top_missing = option (OPTHIDETOPMISSING) && !option (OPTHIDEMISSING);
130 int hide_top_limited = option (OPTHIDETOPLIMITED) && !option (OPTHIDELIMITED);
133 /* we walk each level backwards to make it easier to compute next_subtree_visible */
140 if (depth > *max_depth)
143 tree->subtree_visible = 0;
146 FREE (&tree->message->tree);
147 if (VISIBLE (tree->message, ctx))
151 tree->message->display_subject = need_display_subject (ctx, tree->message);
152 for (tmp = tree; tmp; tmp = tmp->parent)
154 if (tmp->subtree_visible)
157 tmp->subtree_visible = 2;
161 tmp->subtree_visible = 1;
167 tree->deep = !option (OPTHIDELIMITED);
173 tree->deep = !option (OPTHIDEMISSING);
175 tree->next_subtree_visible = tree->next && (tree->next->next_subtree_visible
176 || tree->next->subtree_visible);
188 while (tree && !tree->prev)
200 /* now fix up for the OPTHIDETOP* options if necessary */
201 if (hide_top_limited || hide_top_missing)
206 if (!tree->visible && tree->deep && tree->subtree_visible < 2
207 && ((tree->message && hide_top_limited) || (!tree->message && hide_top_missing)))
209 if (!tree->deep && tree->child && tree->subtree_visible)
215 while (tree && !tree->next)
226 /* Since the graphics characters have a value >255, I have to resort to
227 * using escape sequences to pass the information to print_enriched_string().
228 * These are the macros M_TREE_* defined in mutt.h.
230 * ncurses should automatically use the default ASCII characters instead of
231 * graphics chars on terminals which don't support them (see the man page
234 void mutt_draw_tree (CONTEXT *ctx)
236 char *pfx = NULL, *mypfx = NULL, *arrow = NULL, *myarrow = NULL, *new_tree;
237 char corner = (Sort & SORT_REVERSE) ? M_TREE_ULCORNER : M_TREE_LLCORNER;
238 char vtee = (Sort & SORT_REVERSE) ? M_TREE_BTEE : M_TREE_TTEE;
239 int depth = 0, start_depth = 0, max_depth = 0, width = option (OPTNARROWTREE) ? 1 : 2;
240 THREAD *nextdisp = NULL, *pseudo = NULL, *parent = NULL, *tree = ctx->tree;
242 /* Do the visibility calculations and free the old thread chars.
243 * From now on we can simply ignore invisible subtrees
245 calculate_visibility (ctx, &max_depth);
246 pfx = safe_malloc (width * max_depth + 2);
247 arrow = safe_malloc (width * max_depth + 2);
252 myarrow = arrow + (depth - start_depth - (start_depth ? 0 : 1)) * width;
253 if (depth && start_depth == depth)
254 myarrow[0] = nextdisp ? M_TREE_LTEE : corner;
255 else if (parent->message && !option (OPTHIDELIMITED))
256 myarrow[0] = M_TREE_HIDDEN;
257 else if (!parent->message && !option (OPTHIDEMISSING))
258 myarrow[0] = M_TREE_MISSING;
262 myarrow[1] = pseudo ? M_TREE_STAR
263 : (tree->duplicate_thread ? M_TREE_EQUALS : M_TREE_HLINE);
266 myarrow[width] = M_TREE_RARROW;
267 myarrow[width + 1] = 0;
268 new_tree = safe_malloc ((2 + depth * width));
271 strncpy (new_tree, pfx, (start_depth - 1) * width);
272 strfcpy (new_tree + (start_depth - 1) * width,
273 arrow, (1 + depth - start_depth) * width + 2);
276 strfcpy (new_tree, arrow, 2 + depth * width);
277 tree->message->tree = new_tree;
280 if (tree->child && depth)
282 mypfx = pfx + (depth - 1) * width;
283 mypfx[0] = nextdisp ? M_TREE_VLINE : M_TREE_SPACE;
285 mypfx[1] = M_TREE_SPACE;
292 if (tree->child && tree->subtree_visible)
300 /* we do this here because we need to make sure that the first child thread
301 * of the old tree that we deal with is actually displayed if any are,
302 * or we might set the parent variable wrong while going through it. */
303 while (!tree->subtree_visible && tree->next)
308 while (!tree->next && tree->parent)
312 if (tree == nextdisp)
319 if (start_depth == depth)
326 if (tree == nextdisp)
334 if (!pseudo && tree->fake_thread)
336 if (!nextdisp && tree->next_subtree_visible)
346 /* since we may be trying to attach as a pseudo-thread a THREAD that
347 * has no message, we have to make a list of all the subjects of its
348 * most immediate existing descendants. we also note the earliest
349 * date on any of the parents and put it in *dateptr. */
350 static LIST *make_subject_list (THREAD *cur, time_t *dateptr)
355 LIST *curlist, *oldlist, *newlist, *subjects = NULL;
360 while (!cur->message)
365 thisdate = option (OPTTHREADRECEIVED)
366 ? cur->message->received : cur->message->date_sent;
367 if (!*dateptr || thisdate < *dateptr)
371 env = cur->message->env;
372 if (env->real_subj &&
373 ((env->real_subj != env->subject) || (!option (OPTSORTRE))))
375 for (curlist = subjects, oldlist = NULL;
376 curlist; oldlist = curlist, curlist = curlist->next)
378 rc = mutt_strcmp (env->real_subj, curlist->data);
382 if (!curlist || rc > 0)
384 newlist = safe_calloc (1, sizeof (LIST));
385 newlist->data = env->real_subj;
388 newlist->next = oldlist->next;
389 oldlist->next = newlist;
393 newlist->next = subjects;
399 while (!cur->next && cur != start)
411 /* find the best possible match for a parent mesage based upon subject.
412 * if there are multiple matches, the one which was sent the latest, but
413 * before the current message, is used.
415 static THREAD *find_subject (CONTEXT *ctx, THREAD *cur)
417 struct hash_elem *ptr;
418 THREAD *tmp, *last = NULL;
420 LIST *subjects = NULL, *oldlist;
423 subjects = make_subject_list (cur, &date);
427 hash = hash_string ((unsigned char *) subjects->data,
428 ctx->subj_hash->nelem);
429 for (ptr = ctx->subj_hash->table[hash]; ptr; ptr = ptr->next)
431 tmp = ((HEADER *) ptr->data)->thread;
432 if (tmp != cur && /* don't match the same message */
433 !tmp->fake_thread && /* don't match pseudo threads */
434 tmp->message->subject_changed && /* only match interesting replies */
435 !is_descendant (tmp, cur) && /* don't match in the same thread */
436 (date >= (option (OPTTHREADRECEIVED) ?
437 tmp->message->received :
438 tmp->message->date_sent)) &&
440 (option (OPTTHREADRECEIVED) ?
441 (last->message->received < tmp->message->received) :
442 (last->message->date_sent < tmp->message->date_sent))) &&
443 tmp->message->env->real_subj &&
444 mutt_strcmp (subjects->data, tmp->message->env->real_subj) == 0)
445 last = tmp; /* best match so far */
449 subjects = subjects->next;
455 /* remove cur and its descendants from their current location.
456 * also make sure ancestors of cur no longer are sorted by the
457 * fact that cur is their descendant. */
458 static void unlink_message (THREAD **old, THREAD *cur)
463 cur->prev->next = cur->next;
468 cur->next->prev = cur->prev;
472 for (tmp = cur->parent; tmp && tmp->sort_key == cur->sort_key;
474 tmp->sort_key = NULL;
478 /* add cur as a prior sibling of *new, with parent newparent */
479 static void insert_message (THREAD **new, THREAD *newparent, THREAD *cur)
484 cur->parent = newparent;
490 /* thread by subject things that didn't get threaded by message-id */
491 static void pseudo_threads (CONTEXT *ctx)
493 THREAD *tree = ctx->tree, *top = tree;
494 THREAD *tmp, *cur, *parent, *curchild, *nextchild;
497 ctx->subj_hash = mutt_make_subj_hash (ctx);
503 if ((parent = find_subject (ctx, cur)) != NULL)
505 cur->fake_thread = 1;
506 unlink_message (&top, cur);
507 insert_message (&parent->child, parent, cur);
508 parent->sort_children = 1;
512 while (!tmp->message)
515 /* if the message we're attaching has pseudo-children, they
516 * need to be attached to its parent, so move them up a level.
517 * but only do this if they have the same real subject as the
518 * parent, since otherwise they rightly belong to the message
519 * we're attaching. */
521 || !mutt_strcmp (tmp->message->env->real_subj,
522 parent->message->env->real_subj))
524 tmp->message->subject_changed = 0;
526 for (curchild = tmp->child; curchild; )
528 nextchild = curchild->next;
529 if (curchild->fake_thread)
531 unlink_message (&tmp->child, curchild);
532 insert_message (&parent->child, parent, curchild);
534 curchild = nextchild;
538 while (!tmp->next && tmp != cur)
552 void mutt_clear_threads (CONTEXT *ctx)
556 for (i = 0; i < ctx->msgcount; i++)
558 ctx->hdrs[i]->thread = NULL;
559 ctx->hdrs[i]->threaded = 0;
563 if (ctx->thread_hash)
564 hash_destroy (&ctx->thread_hash, *free);
567 int compare_threads (const void *a, const void *b)
569 static sort_t *sort_func = NULL;
572 return ((*sort_func) (&(*((THREAD **) a))->sort_key,
573 &(*((THREAD **) b))->sort_key));
574 /* a hack to let us reset sort_func even though we can't
575 * have extra arguments because of qsort
580 sort_func = mutt_get_sort_func (Sort);
581 return (sort_func ? 1 : 0);
585 THREAD *mutt_sort_subthreads (THREAD *thread, int init)
587 THREAD **array, *sort_key, *top, *tmp;
589 int i, array_size, sort_top = 0;
591 /* we put things into the array backwards to save some cycles,
592 * but we want to have to move less stuff around if we're
593 * resorting, so we sort backwards and then put them back
594 * in reverse order so they're forwards
596 Sort ^= SORT_REVERSE;
597 if (!compare_threads (NULL, NULL))
602 array = safe_calloc ((array_size = 256), sizeof (THREAD *));
605 if (init || !thread->sort_key)
607 thread->sort_key = NULL;
610 thread->parent->sort_children = 1;
617 thread = thread->child;
622 /* if it has no children, it must be real. sort it on its own merits */
623 thread->sort_key = thread->message;
627 thread = thread->next;
632 while (!thread->next)
634 /* if it has siblings and needs to be sorted, sort it... */
635 if (thread->prev && (thread->parent ? thread->parent->sort_children : sort_top))
637 /* put them into the array */
638 for (i = 0; thread; i++, thread = thread->prev)
641 safe_realloc (&array, (array_size *= 2) * sizeof (THREAD *));
646 qsort ((void *) array, i, sizeof (THREAD *), *compare_threads);
648 /* attach them back together. make thread the last sibling. */
651 array[i - 1]->prev = NULL;
654 thread->parent->child = array[i - 1];
660 array[i - 1]->prev = array[i];
661 array[i]->next = array[i - 1];
668 thread = thread->parent;
670 if (!thread->sort_key || thread->sort_children)
672 /* make sort_key the first or last sibling, as appropriate */
673 sort_key = (!(Sort & SORT_LAST) ^ !(Sort & SORT_REVERSE)) ? thread->child : tmp;
675 /* we just sorted its children */
676 thread->sort_children = 0;
678 oldsort_key = thread->sort_key;
679 thread->sort_key = thread->message;
681 if (Sort & SORT_LAST)
683 if (!thread->sort_key
684 || ((((Sort & SORT_REVERSE) ? 1 : -1)
685 * compare_threads ((void *) &thread,
688 thread->sort_key = sort_key->sort_key;
690 else if (!thread->sort_key)
691 thread->sort_key = sort_key->sort_key;
693 /* if its sort_key has changed, we need to resort it and siblings */
694 if (oldsort_key != thread->sort_key)
697 thread->parent->sort_children = 1;
705 Sort ^= SORT_REVERSE;
711 thread = thread->next;
715 static void check_subjects (CONTEXT *ctx, int init)
721 for (i = 0; i < ctx->msgcount; i++)
724 if (cur->thread->check_subject)
725 cur->thread->check_subject = 0;
729 /* figure out which messages have subjects different than their parents' */
730 tmp = cur->thread->parent;
731 while (tmp && !tmp->message)
737 cur->subject_changed = 1;
738 else if (cur->env->real_subj && tmp->message->env->real_subj)
739 cur->subject_changed = mutt_strcmp (cur->env->real_subj,
740 tmp->message->env->real_subj) ? 1 : 0;
742 cur->subject_changed = (cur->env->real_subj
743 || tmp->message->env->real_subj) ? 1 : 0;
747 void mutt_sort_threads (CONTEXT *ctx, int init)
750 int i, oldsort, using_refs = 0;
751 THREAD *thread, *new, *tmp, top;
754 /* set Sort to the secondary method to support the set sort_aux=reverse-*
755 * settings. The sorting functions just look at the value of
761 if (!ctx->thread_hash)
765 ctx->thread_hash = hash_create (ctx->msgcount * 2);
767 /* we want a quick way to see if things are actually attached to the top of the
768 * thread tree or if they're just dangling, so we attach everything to a top
769 * node temporarily */
770 top.parent = top.next = top.prev = NULL;
771 top.child = ctx->tree;
772 for (thread = ctx->tree; thread; thread = thread->next)
773 thread->parent = ⊤
775 /* put each new message together with the matching messageless THREAD if it
776 * exists. otherwise, if there is a THREAD that already has a message, thread
777 * new message as an identical child. if we didn't attach the message to a
778 * THREAD, make a new one for it. */
779 for (i = 0; i < ctx->msgcount; i++)
785 if ((!init || option (OPTDUPTHREADS)) && cur->env->message_id)
786 thread = hash_find (ctx->thread_hash, cur->env->message_id);
790 if (thread && !thread->message)
792 /* this is a message which was missing before */
793 thread->message = cur;
794 cur->thread = thread;
795 thread->check_subject = 1;
797 /* mark descendants as needing subject_changed checked */
798 for (tmp = (thread->child ? thread->child : thread); tmp != thread; )
800 while (!tmp->message)
802 tmp->check_subject = 1;
803 while (!tmp->next && tmp != thread)
811 /* remove threading info above it based on its children, which we'll
812 * recalculate based on its headers. make sure not to leave
813 * dangling missing messages. note that we haven't kept track
814 * of what info came from its children and what from its siblings'
815 * children, so we just remove the stuff that's definitely from it */
818 tmp = thread->parent;
819 unlink_message (&tmp->child, thread);
820 thread->parent = NULL;
821 thread->sort_key = NULL;
822 thread->fake_thread = 0;
824 } while (thread != &top && !thread->child && !thread->message);
829 new = (option (OPTDUPTHREADS) ? thread : NULL);
831 thread = safe_calloc (1, sizeof (THREAD));
832 thread->message = cur;
833 thread->check_subject = 1;
834 cur->thread = thread;
835 hash_insert (ctx->thread_hash,
836 cur->env->message_id ? cur->env->message_id : "",
841 if (new->duplicate_thread)
844 thread = cur->thread;
846 insert_message (&new->child, new, thread);
847 thread->duplicate_thread = 1;
848 thread->message->threaded = 1;
854 /* unlink pseudo-threads because they might be children of newly
855 * arrived messages */
856 thread = cur->thread;
857 for (new = thread->child; new; )
860 if (new->fake_thread)
862 unlink_message (&thread->child, new);
863 insert_message (&top.child, &top, new);
864 new->fake_thread = 0;
871 /* thread by references */
872 for (i = 0; i < ctx->msgcount; i++)
879 thread = cur->thread;
886 /* look at the beginning of in-reply-to: */
887 if ((ref = cur->env->in_reply_to) != NULL)
891 ref = cur->env->references;
895 else if (using_refs == 1)
897 /* if there's no references header, use all the in-reply-to:
898 * data that we have. otherwise, use the first reference
899 * if it's different than the first in-reply-to, otherwise use
900 * the second reference (since at least eudora puts the most
901 * recent reference in in-reply-to and the rest in references)
903 if (!cur->env->references)
907 if (mutt_strcmp (ref->data, cur->env->references->data))
908 ref = cur->env->references;
910 ref = cur->env->references->next;
916 ref = ref->next; /* go on with references */
921 if ((new = hash_find (ctx->thread_hash, ref->data)) == NULL)
923 new = safe_calloc (1, sizeof (THREAD));
924 hash_insert (ctx->thread_hash, ref->data, new, 1);
928 if (new->duplicate_thread)
930 if (is_descendant (new, thread)) /* no loops! */
935 unlink_message (&top.child, thread);
936 insert_message (&new->child, new, thread);
938 if (thread->message || (thread->parent && thread->parent != &top))
943 insert_message (&top.child, &top, thread);
946 /* detach everything from the temporary top node */
947 for (thread = top.child; thread; thread = thread->next)
949 thread->parent = NULL;
951 ctx->tree = top.child;
953 check_subjects (ctx, init);
955 if (!option (OPTSTRICTTHREADS))
956 pseudo_threads (ctx);
960 ctx->tree = mutt_sort_subthreads (ctx->tree, init);
962 /* restore the oldsort order. */
965 /* Put the list into an array. */
966 linearize_tree (ctx);
968 /* Draw the thread tree. */
969 mutt_draw_tree (ctx);
973 static HEADER *find_virtual (THREAD *cur, int reverse)
977 if (cur->message && cur->message->virtual >= 0)
978 return (cur->message);
981 if ((cur = cur->child) == NULL)
984 while (reverse && cur->next)
989 if (cur->message && cur->message->virtual >= 0)
990 return (cur->message);
996 while (reverse && cur->next)
999 else if (reverse ? cur->prev : cur->next)
1000 cur = reverse ? cur->prev : cur->next;
1003 while (!(reverse ? cur->prev : cur->next))
1009 cur = reverse ? cur->prev : cur->next;
1015 int _mutt_aside_thread (HEADER *hdr, short dir, short subthreads)
1020 if ((Sort & SORT_MASK) != SORT_THREADS)
1022 mutt_error _("Threading is not enabled.");
1023 return (hdr->virtual);
1035 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0))
1037 while (!cur->next && cur->parent)
1042 while (!cur->prev && cur->parent)
1047 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0))
1054 tmp = find_virtual (cur, 0);
1064 tmp = find_virtual (cur, 1);
1068 return (tmp->virtual);
1071 int mutt_parent_message (CONTEXT *ctx, HEADER *hdr)
1075 if ((Sort & SORT_MASK) != SORT_THREADS)
1077 mutt_error _("Threading is not enabled.");
1078 return (hdr->virtual);
1081 for (thread = hdr->thread->parent; thread; thread = thread->parent)
1083 if ((hdr = thread->message) != NULL)
1085 if (VISIBLE (hdr, ctx))
1086 return (hdr->virtual);
1089 mutt_error _("Parent message is not visible in this limited view.");
1095 mutt_error _("Parent message is not available.");
1099 void mutt_set_virtual (CONTEXT *ctx)
1107 for (i = 0; i < ctx->msgcount; i++)
1110 if (cur->virtual >= 0)
1112 cur->virtual = ctx->vcount;
1113 ctx->v2r[ctx->vcount] = i;
1115 ctx->vsize += cur->content->length + cur->content->offset - cur->content->hdr_offset;
1116 cur->num_hidden = mutt_get_hidden (ctx, cur);
1121 int _mutt_traverse_thread (CONTEXT *ctx, HEADER *cur, int flag)
1123 THREAD *thread, *top;
1124 HEADER *roothdr = NULL;
1125 int final, reverse = (Sort & SORT_REVERSE), minmsgno;
1126 int num_hidden = 0, new = 0, old = 0;
1127 int min_unread_msgno = INT_MAX, min_unread = cur->virtual;
1128 #define CHECK_LIMIT (!ctx->pattern || cur->limited)
1130 if ((Sort & SORT_MASK) != SORT_THREADS && !(flag & M_THREAD_GET_HIDDEN))
1132 mutt_error (_("Threading is not enabled."));
1133 return (cur->virtual);
1136 final = cur->virtual;
1137 thread = cur->thread;
1138 while (thread->parent)
1139 thread = thread->parent;
1141 while (!thread->message)
1142 thread = thread->child;
1143 cur = thread->message;
1144 minmsgno = cur->msgno;
1146 if (!cur->read && CHECK_LIMIT)
1152 if (cur->msgno < min_unread_msgno)
1154 min_unread = cur->virtual;
1155 min_unread_msgno = cur->msgno;
1159 if (cur->virtual == -1 && CHECK_LIMIT)
1162 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1164 cur->pair = 0; /* force index entry's color to be re-evaluated */
1165 cur->collapsed = flag & M_THREAD_COLLAPSE;
1166 if (cur->virtual != -1)
1169 if (flag & M_THREAD_COLLAPSE)
1170 final = roothdr->virtual;
1174 if (thread == top && (thread = thread->child) == NULL)
1176 /* return value depends on action requested */
1177 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1179 else if (flag & M_THREAD_UNREAD)
1180 return ((old && new) ? new : (old ? old : new));
1181 else if (flag & M_THREAD_GET_HIDDEN)
1182 return (num_hidden);
1183 else if (flag & M_THREAD_NEXT_UNREAD)
1184 return (min_unread);
1189 cur = thread->message;
1193 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1195 cur->pair = 0; /* force index entry's color to be re-evaluated */
1196 cur->collapsed = flag & M_THREAD_COLLAPSE;
1197 if (!roothdr && CHECK_LIMIT)
1200 if (flag & M_THREAD_COLLAPSE)
1201 final = roothdr->virtual;
1204 if (reverse && (flag & M_THREAD_COLLAPSE) && (cur->msgno < minmsgno) && CHECK_LIMIT)
1206 minmsgno = cur->msgno;
1207 final = cur->virtual;
1210 if (flag & M_THREAD_COLLAPSE)
1218 cur->virtual = cur->msgno;
1223 if (!cur->read && CHECK_LIMIT)
1229 if (cur->msgno < min_unread_msgno)
1231 min_unread = cur->virtual;
1232 min_unread_msgno = cur->msgno;
1236 if (cur->virtual == -1 && CHECK_LIMIT)
1241 thread = thread->child;
1242 else if (thread->next)
1243 thread = thread->next;
1247 while (!thread->next)
1249 thread = thread->parent;
1258 thread = thread->next;
1262 /* return value depends on action requested */
1263 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1265 else if (flag & M_THREAD_UNREAD)
1266 return ((old && new) ? new : (old ? old : new));
1267 else if (flag & M_THREAD_GET_HIDDEN)
1268 return (num_hidden+1);
1269 else if (flag & M_THREAD_NEXT_UNREAD)
1270 return (min_unread);
1277 /* if flag is 0, we want to know how many messages
1278 * are in the thread. if flag is 1, we want to know
1279 * our position in the thread. */
1280 int mutt_messages_in_thread (CONTEXT *ctx, HEADER *hdr, int flag)
1285 if ((Sort & SORT_MASK) != SORT_THREADS || !hdr->thread)
1288 threads[0] = hdr->thread;
1289 while (threads[0]->parent)
1290 threads[0] = threads[0]->parent;
1292 threads[1] = flag ? hdr->thread : threads[0]->next;
1294 for (i = 0; i < ((flag || !threads[1]) ? 1 : 2); i++)
1296 while (!threads[i]->message)
1297 threads[i] = threads[i]->child;
1300 if (Sort & SORT_REVERSE)
1301 rc = threads[0]->message->msgno - (threads[1] ? threads[1]->message->msgno : -1);
1303 rc = (threads[1] ? threads[1]->message->msgno : ctx->msgcount) - threads[0]->message->msgno;
1312 HASH *mutt_make_id_hash (CONTEXT *ctx)
1318 hash = hash_create (ctx->msgcount * 2);
1320 for (i = 0; i < ctx->msgcount; i++)
1323 if (hdr->env->message_id)
1324 hash_insert (hash, hdr->env->message_id, hdr, 0);
1330 HASH *mutt_make_subj_hash (CONTEXT *ctx)
1336 hash = hash_create (ctx->msgcount * 2);
1338 for (i = 0; i < ctx->msgcount; i++)
1341 if (hdr->env->real_subj)
1342 hash_insert (hash, hdr->env->real_subj, hdr, 1);
1348 static void clean_references (THREAD *brk, THREAD *cur)
1354 for (; cur; cur = cur->next, done = 0)
1356 /* parse subthread recursively */
1357 clean_references (brk, cur->child);
1360 break; /* skip pseudo-message */
1362 /* Looking for the first bad reference according to the new threading.
1363 * Optimal since Mutt stores the references in reverse order, and the
1364 * first loop should match immediatly for mails respecting RFC2822. */
1365 for (p = brk; !done && p; p = p->parent)
1366 for (ref = cur->message->env->references; p->message && ref; ref = ref->next)
1367 if (!mutt_strcasecmp (ref->data, p->message->env->message_id))
1375 HEADER *h = cur->message;
1377 /* clearing the References: header from obsolete Message-Id(s) */
1378 mutt_free_list (&ref->next);
1380 #ifdef IMAP_EDIT_THREADS
1382 mutt_free_list (&h->new_env->references);
1384 h->new_env = mutt_new_envelope ();
1386 h->new_env->references = mutt_copy_list (h->env->references);
1389 h->refs_changed = h->changed = 1;
1394 void mutt_break_thread (HEADER *hdr)
1396 mutt_free_list (&hdr->env->in_reply_to);
1397 mutt_free_list (&hdr->env->references);
1398 hdr->irt_changed = hdr->refs_changed = hdr->changed = 1;
1400 #ifdef IMAP_EDIT_THREADS
1403 mutt_free_list (&hdr->new_env->in_reply_to);
1404 mutt_free_list (&hdr->new_env->references);
1407 hdr->new_env = mutt_new_envelope ();
1410 clean_references (hdr->thread, hdr->thread->child);
1413 static int link_threads (HEADER *parent, HEADER *child, CONTEXT *ctx)
1415 if (child == parent)
1418 mutt_break_thread (child);
1420 child->env->in_reply_to = mutt_new_list ();
1421 child->env->in_reply_to->data = safe_strdup (parent->env->message_id);
1423 #ifdef IMAP_EDIT_THREADS
1424 child->new_env->in_reply_to = mutt_new_list ();
1425 child->new_env->in_reply_to->data = safe_strdup (parent->env->message_id);
1428 mutt_set_flag (ctx, child, M_TAG, 0);
1430 child->irt_changed = child->changed = 1;
1434 int mutt_link_threads (HEADER *cur, HEADER *last, CONTEXT *ctx)
1440 for (i = 0; i < ctx->vcount; i++)
1441 if (ctx->hdrs[Context->v2r[i]]->tagged)
1442 changed |= link_threads (cur, ctx->hdrs[Context->v2r[i]], ctx);
1445 changed = link_threads (cur, last, ctx);