2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 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.
20 #define VISIBLE(hdr, ctx) (hdr->virtual >= 0 || (hdr->collapsed && (!ctx->pattern || hdr->limited)))
22 /* determine whether a is a descendant of b */
23 static int is_descendant (THREAD * a, THREAD * b)
33 /* Determines whether to display a message's subject. */
34 static int need_display_subject (CONTEXT * ctx, HEADER * hdr)
36 THREAD *tmp, *tree = hdr->thread;
38 /* if the user disabled subject hiding, display it */
39 if (!option (OPTHIDETHREADSUBJECT))
42 /* if our subject is different from our parent's, display it */
43 if (hdr->subject_changed)
46 /* if our subject is different from that of our closest previously displayed
47 * sibling, display the subject */
48 for (tmp = tree->prev; tmp; tmp = tmp->prev) {
50 if (hdr && VISIBLE (hdr, ctx)) {
51 if (hdr->subject_changed)
58 /* if there is a parent-to-child subject change anywhere between us and our
59 * closest displayed ancestor, display the subject */
60 for (tmp = tree->parent; tmp; tmp = tmp->parent) {
63 if (VISIBLE (hdr, ctx))
65 else if (hdr->subject_changed)
70 /* if we have no visible parent or previous sibling, display the subject */
74 static void linearize_tree (CONTEXT * ctx)
76 THREAD *tree = ctx->tree;
77 HEADER **array = ctx->hdrs + (Sort & SORT_REVERSE ? ctx->msgcount - 1 : 0);
80 while (!tree->message)
83 *array = tree->message;
84 array += Sort & SORT_REVERSE ? -1 : 1;
101 /* this calculates whether a node is the root of a subtree that has visible
102 * nodes, whether a node itself is visible, whether, if invisible, it has
103 * depth anyway, and whether any of its later siblings are roots of visible
104 * subtrees. while it's at it, it frees the old thread display, so we can
105 * skip parts of the tree in mutt_draw_tree() if we've decided here that we
106 * don't care about them any more.
108 static void calculate_visibility (CONTEXT * ctx, int *max_depth)
110 THREAD *tmp, *tree = ctx->tree;
111 int hide_top_missing = option (OPTHIDETOPMISSING)
112 && !option (OPTHIDEMISSING);
113 int hide_top_limited = option (OPTHIDETOPLIMITED)
114 && !option (OPTHIDELIMITED);
117 /* we walk each level backwards to make it easier to compute next_subtree_visible */
123 if (depth > *max_depth)
126 tree->subtree_visible = 0;
128 FREE (&tree->message->tree);
129 if (VISIBLE (tree->message, ctx)) {
132 tree->message->display_subject =
133 need_display_subject (ctx, tree->message);
134 for (tmp = tree; tmp; tmp = tmp->parent) {
135 if (tmp->subtree_visible) {
137 tmp->subtree_visible = 2;
141 tmp->subtree_visible = 1;
146 tree->deep = !option (OPTHIDELIMITED);
151 tree->deep = !option (OPTHIDEMISSING);
153 tree->next_subtree_visible = tree->next
154 && (tree->next->next_subtree_visible || tree->next->subtree_visible);
164 while (tree && !tree->prev) {
175 /* now fix up for the OPTHIDETOP* options if necessary */
176 if (hide_top_limited || hide_top_missing) {
179 if (!tree->visible && tree->deep && tree->subtree_visible < 2
180 && ((tree->message && hide_top_limited)
181 || (!tree->message && hide_top_missing)))
183 if (!tree->deep && tree->child && tree->subtree_visible)
188 while (tree && !tree->next)
199 /* Since the graphics characters have a value >255, I have to resort to
200 * using escape sequences to pass the information to print_enriched_string().
201 * These are the macros M_TREE_* defined in mutt.h.
203 * ncurses should automatically use the default ASCII characters instead of
204 * graphics chars on terminals which don't support them (see the man page
207 void mutt_draw_tree (CONTEXT * ctx)
209 char *pfx = NULL, *mypfx = NULL, *arrow = NULL, *myarrow = NULL, *new_tree;
210 char corner = (Sort & SORT_REVERSE) ? M_TREE_ULCORNER : M_TREE_LLCORNER;
211 char vtee = (Sort & SORT_REVERSE) ? M_TREE_BTEE : M_TREE_TTEE;
212 int depth = 0, start_depth = 0, max_depth = 0, width =
213 option (OPTNARROWTREE) ? 1 : 2;
214 THREAD *nextdisp = NULL, *pseudo = NULL, *parent = NULL, *tree = ctx->tree;
216 /* Do the visibility calculations and free the old thread chars.
217 * From now on we can simply ignore invisible subtrees
219 calculate_visibility (ctx, &max_depth);
220 pfx = safe_malloc (width * max_depth + 2);
221 arrow = safe_malloc (width * max_depth + 2);
224 myarrow = arrow + (depth - start_depth - (start_depth ? 0 : 1)) * width;
225 if (depth && start_depth == depth)
226 myarrow[0] = nextdisp ? M_TREE_LTEE : corner;
227 else if (parent->message && !option (OPTHIDELIMITED))
228 myarrow[0] = M_TREE_HIDDEN;
229 else if (!parent->message && !option (OPTHIDEMISSING))
230 myarrow[0] = M_TREE_MISSING;
234 myarrow[1] = pseudo ? M_TREE_STAR
235 : (tree->duplicate_thread ? M_TREE_EQUALS : M_TREE_HLINE);
237 myarrow[width] = M_TREE_RARROW;
238 myarrow[width + 1] = 0;
239 new_tree = safe_malloc ((2 + depth * width));
240 if (start_depth > 1) {
241 strncpy (new_tree, pfx, (start_depth - 1) * width);
242 strfcpy (new_tree + (start_depth - 1) * width,
243 arrow, (1 + depth - start_depth) * width + 2);
246 strfcpy (new_tree, arrow, 2 + depth * width);
247 tree->message->tree = new_tree;
250 if (tree->child && depth) {
251 mypfx = pfx + (depth - 1) * width;
252 mypfx[0] = nextdisp ? M_TREE_VLINE : M_TREE_SPACE;
254 mypfx[1] = M_TREE_SPACE;
260 if (tree->child && tree->subtree_visible) {
267 /* we do this here because we need to make sure that the first child thread
268 * of the old tree that we deal with is actually displayed if any are,
269 * or we might set the parent variable wrong while going through it. */
270 while (!tree->subtree_visible && tree->next)
274 while (!tree->next && tree->parent) {
277 if (tree == nextdisp)
283 if (start_depth == depth)
290 if (tree == nextdisp)
298 if (!pseudo && tree->fake_thread)
300 if (!nextdisp && tree->next_subtree_visible)
310 /* since we may be trying to attach as a pseudo-thread a THREAD that
311 * has no message, we have to make a list of all the subjects of its
312 * most immediate existing descendants. we also note the earliest
313 * date on any of the parents and put it in *dateptr. */
314 static LIST *make_subject_list (THREAD * cur, time_t * dateptr)
319 LIST *curlist, *oldlist, *newlist, *subjects = NULL;
323 while (!cur->message)
327 thisdate = option (OPTTHREADRECEIVED)
328 ? cur->message->received : cur->message->date_sent;
329 if (!*dateptr || thisdate < *dateptr)
333 env = cur->message->env;
334 if (env->real_subj &&
335 ((env->real_subj != env->subject) || (!option (OPTSORTRE)))) {
336 for (curlist = subjects, oldlist = NULL;
337 curlist; oldlist = curlist, curlist = curlist->next) {
338 rc = mutt_strcmp (env->real_subj, curlist->data);
342 if (!curlist || rc > 0) {
343 newlist = safe_calloc (1, sizeof (LIST));
344 newlist->data = env->real_subj;
346 newlist->next = oldlist->next;
347 oldlist->next = newlist;
350 newlist->next = subjects;
356 while (!cur->next && cur != start) {
367 /* find the best possible match for a parent mesage based upon subject.
368 * if there are multiple matches, the one which was sent the latest, but
369 * before the current message, is used.
371 static THREAD *find_subject (CONTEXT * ctx, THREAD * cur)
373 struct hash_elem *ptr;
374 THREAD *tmp, *last = NULL;
376 LIST *subjects = NULL, *oldlist;
379 subjects = make_subject_list (cur, &date);
382 hash = hash_string ((unsigned char *) subjects->data,
383 ctx->subj_hash->nelem);
384 for (ptr = ctx->subj_hash->table[hash]; ptr; ptr = ptr->next) {
385 tmp = ((HEADER *) ptr->data)->thread;
386 if (tmp != cur && /* don't match the same message */
387 !tmp->fake_thread && /* don't match pseudo threads */
388 tmp->message->subject_changed && /* only match interesting replies */
389 !is_descendant (tmp, cur) && /* don't match in the same thread */
390 (date >= (option (OPTTHREADRECEIVED) ?
391 tmp->message->received :
392 tmp->message->date_sent)) &&
394 (option (OPTTHREADRECEIVED) ?
395 (last->message->received < tmp->message->received) :
396 (last->message->date_sent < tmp->message->date_sent))) &&
397 tmp->message->env->real_subj &&
398 mutt_strcmp (subjects->data, tmp->message->env->real_subj) == 0)
399 last = tmp; /* best match so far */
403 subjects = subjects->next;
409 /* remove cur and its descendants from their current location.
410 * also make sure ancestors of cur no longer are sorted by the
411 * fact that cur is their descendant. */
412 static void unlink_message (THREAD ** old, THREAD * cur)
417 cur->prev->next = cur->next;
422 cur->next->prev = cur->prev;
425 for (tmp = cur->parent; tmp && tmp->sort_key == cur->sort_key;
427 tmp->sort_key = NULL;
431 /* add cur as a prior sibling of *new, with parent newparent */
432 static void insert_message (THREAD ** new, THREAD * newparent, THREAD * cur)
437 cur->parent = newparent;
443 /* thread by subject things that didn't get threaded by message-id */
444 static void pseudo_threads (CONTEXT * ctx)
446 THREAD *tree = ctx->tree, *top = tree;
447 THREAD *tmp, *cur, *parent, *curchild, *nextchild;
450 ctx->subj_hash = mutt_make_subj_hash (ctx);
455 if ((parent = find_subject (ctx, cur)) != NULL) {
456 cur->fake_thread = 1;
457 unlink_message (&top, cur);
458 insert_message (&parent->child, parent, cur);
459 parent->sort_children = 1;
462 while (!tmp->message)
465 /* if the message we're attaching has pseudo-children, they
466 * need to be attached to its parent, so move them up a level.
467 * but only do this if they have the same real subject as the
468 * parent, since otherwise they rightly belong to the message
469 * we're attaching. */
471 || !mutt_strcmp (tmp->message->env->real_subj,
472 parent->message->env->real_subj)) {
473 tmp->message->subject_changed = 0;
475 for (curchild = tmp->child; curchild;) {
476 nextchild = curchild->next;
477 if (curchild->fake_thread) {
478 unlink_message (&tmp->child, curchild);
479 insert_message (&parent->child, parent, curchild);
481 curchild = nextchild;
485 while (!tmp->next && tmp != cur) {
498 void mutt_clear_threads (CONTEXT * ctx)
502 for (i = 0; i < ctx->msgcount; i++) {
503 ctx->hdrs[i]->thread = NULL;
504 ctx->hdrs[i]->threaded = 0;
508 if (ctx->thread_hash)
509 hash_destroy (&ctx->thread_hash, free);
512 int compare_threads (const void *a, const void *b)
514 static sort_t *sort_func = NULL;
517 return ((*sort_func) (&(*((THREAD **) a))->sort_key,
518 &(*((THREAD **) b))->sort_key));
519 /* a hack to let us reset sort_func even though we can't
520 * have extra arguments because of qsort
524 sort_func = mutt_get_sort_func (Sort);
525 return (sort_func ? 1 : 0);
529 THREAD *mutt_sort_subthreads (THREAD * thread, int init)
531 THREAD **array, *sort_key, *top, *tmp;
533 int i, array_size, sort_top = 0;
535 /* we put things into the array backwards to save some cycles,
536 * but we want to have to move less stuff around if we're
537 * resorting, so we sort backwards and then put them back
538 * in reverse order so they're forwards
540 Sort ^= SORT_REVERSE;
541 if (!compare_threads (NULL, NULL))
546 array = safe_calloc ((array_size = 256), sizeof (THREAD *));
548 if (init || !thread->sort_key) {
549 thread->sort_key = NULL;
552 thread->parent->sort_children = 1;
558 thread = thread->child;
562 /* if it has no children, it must be real. sort it on its own merits */
563 thread->sort_key = thread->message;
566 thread = thread->next;
571 while (!thread->next) {
572 /* if it has siblings and needs to be sorted, sort it... */
574 && (thread->parent ? thread->parent->sort_children : sort_top)) {
575 /* put them into the array */
576 for (i = 0; thread; i++, thread = thread->prev) {
578 safe_realloc (&array, (array_size *= 2) * sizeof (THREAD *));
583 qsort ((void *) array, i, sizeof (THREAD *), compare_threads);
585 /* attach them back together. make thread the last sibling. */
588 array[i - 1]->prev = NULL;
591 thread->parent->child = array[i - 1];
596 array[i - 1]->prev = array[i];
597 array[i]->next = array[i - 1];
601 if (thread->parent) {
603 thread = thread->parent;
605 if (!thread->sort_key || thread->sort_children) {
606 /* make sort_key the first or last sibling, as appropriate */
608 (!(Sort & SORT_LAST) ^ !(Sort & SORT_REVERSE)) ? thread->
611 /* we just sorted its children */
612 thread->sort_children = 0;
614 oldsort_key = thread->sort_key;
615 thread->sort_key = thread->message;
617 if (Sort & SORT_LAST) {
618 if (!thread->sort_key || ((((Sort & SORT_REVERSE) ? 1 : -1)
619 * compare_threads ((void *) &thread,
622 thread->sort_key = sort_key->sort_key;
624 else if (!thread->sort_key)
625 thread->sort_key = sort_key->sort_key;
627 /* if its sort_key has changed, we need to resort it and siblings */
628 if (oldsort_key != thread->sort_key) {
630 thread->parent->sort_children = 1;
637 Sort ^= SORT_REVERSE;
643 thread = thread->next;
647 static void check_subjects (CONTEXT * ctx, int init)
653 for (i = 0; i < ctx->msgcount; i++) {
655 if (cur->thread->check_subject)
656 cur->thread->check_subject = 0;
660 /* figure out which messages have subjects different than their parents' */
661 tmp = cur->thread->parent;
662 while (tmp && !tmp->message) {
667 cur->subject_changed = 1;
668 else if (cur->env->real_subj && tmp->message->env->real_subj)
669 cur->subject_changed = mutt_strcmp (cur->env->real_subj,
673 cur->subject_changed = (cur->env->real_subj
674 || tmp->message->env->real_subj) ? 1 : 0;
678 void mutt_sort_threads (CONTEXT * ctx, int init)
681 int i, oldsort, using_refs = 0;
682 THREAD *thread, *new, *tmp, top;
685 /* set Sort to the secondary method to support the set sort_aux=reverse-*
686 * settings. The sorting functions just look at the value of
692 if (!ctx->thread_hash)
696 ctx->thread_hash = hash_create (ctx->msgcount * 2);
698 /* we want a quick way to see if things are actually attached to the top of the
699 * thread tree or if they're just dangling, so we attach everything to a top
700 * node temporarily */
701 top.parent = top.next = top.prev = NULL;
702 top.child = ctx->tree;
703 for (thread = ctx->tree; thread; thread = thread->next)
704 thread->parent = ⊤
706 /* put each new message together with the matching messageless THREAD if it
707 * exists. otherwise, if there is a THREAD that already has a message, thread
708 * new message as an identical child. if we didn't attach the message to a
709 * THREAD, make a new one for it. */
710 for (i = 0; i < ctx->msgcount; i++) {
714 if ((!init || option (OPTDUPTHREADS)) && cur->env->message_id)
715 thread = hash_find (ctx->thread_hash, cur->env->message_id);
719 if (thread && !thread->message) {
720 /* this is a message which was missing before */
721 thread->message = cur;
722 cur->thread = thread;
723 thread->check_subject = 1;
725 /* mark descendants as needing subject_changed checked */
726 for (tmp = (thread->child ? thread->child : thread); tmp != thread;) {
727 while (!tmp->message)
729 tmp->check_subject = 1;
730 while (!tmp->next && tmp != thread)
736 if (thread->parent) {
737 /* remove threading info above it based on its children, which we'll
738 * recalculate based on its headers. make sure not to leave
739 * dangling missing messages. note that we haven't kept track
740 * of what info came from its children and what from its siblings'
741 * children, so we just remove the stuff that's definitely from it */
743 tmp = thread->parent;
744 unlink_message (&tmp->child, thread);
745 thread->parent = NULL;
746 thread->sort_key = NULL;
747 thread->fake_thread = 0;
749 } while (thread != &top && !thread->child && !thread->message);
753 new = (option (OPTDUPTHREADS) ? thread : NULL);
755 thread = safe_calloc (1, sizeof (THREAD));
756 thread->message = cur;
757 thread->check_subject = 1;
758 cur->thread = thread;
759 hash_insert (ctx->thread_hash,
760 cur->env->message_id ? cur->env->message_id : "",
764 if (new->duplicate_thread)
767 thread = cur->thread;
769 insert_message (&new->child, new, thread);
770 thread->duplicate_thread = 1;
771 thread->message->threaded = 1;
776 /* unlink pseudo-threads because they might be children of newly
777 * arrived messages */
778 thread = cur->thread;
779 for (new = thread->child; new;) {
781 if (new->fake_thread) {
782 unlink_message (&thread->child, new);
783 insert_message (&top.child, &top, new);
784 new->fake_thread = 0;
791 /* thread by references */
792 for (i = 0; i < ctx->msgcount; i++) {
798 thread = cur->thread;
802 if (using_refs == 0) {
803 /* look at the beginning of in-reply-to: */
804 if ((ref = cur->env->in_reply_to) != NULL)
807 ref = cur->env->references;
811 else if (using_refs == 1) {
812 /* if there's no references header, use all the in-reply-to:
813 * data that we have. otherwise, use the first reference
814 * if it's different than the first in-reply-to, otherwise use
815 * the second reference (since at least eudora puts the most
816 * recent reference in in-reply-to and the rest in references)
818 if (!cur->env->references)
821 if (mutt_strcmp (ref->data, cur->env->references->data))
822 ref = cur->env->references;
824 ref = cur->env->references->next;
830 ref = ref->next; /* go on with references */
835 if ((new = hash_find (ctx->thread_hash, ref->data)) == NULL) {
836 new = safe_calloc (1, sizeof (THREAD));
837 hash_insert (ctx->thread_hash, ref->data, new, 1);
840 if (new->duplicate_thread)
842 if (is_descendant (new, thread)) /* no loops! */
847 unlink_message (&top.child, thread);
848 insert_message (&new->child, new, thread);
850 if (thread->message || (thread->parent && thread->parent != &top))
855 insert_message (&top.child, &top, thread);
858 /* detach everything from the temporary top node */
859 for (thread = top.child; thread; thread = thread->next) {
860 thread->parent = NULL;
862 ctx->tree = top.child;
864 check_subjects (ctx, init);
866 if (!option (OPTSTRICTTHREADS))
867 pseudo_threads (ctx);
870 ctx->tree = mutt_sort_subthreads (ctx->tree, init);
872 /* restore the oldsort order. */
875 /* Put the list into an array. */
876 linearize_tree (ctx);
878 /* Draw the thread tree. */
879 mutt_draw_tree (ctx);
883 static HEADER *find_virtual (THREAD * cur, int reverse)
887 if (cur->message && cur->message->virtual >= 0)
888 return (cur->message);
891 if ((cur = cur->child) == NULL)
894 while (reverse && cur->next)
898 if (cur->message && cur->message->virtual >= 0)
899 return (cur->message);
904 while (reverse && cur->next)
907 else if (reverse ? cur->prev : cur->next)
908 cur = reverse ? cur->prev : cur->next;
910 while (!(reverse ? cur->prev : cur->next)) {
915 cur = reverse ? cur->prev : cur->next;
921 int _mutt_aside_thread (HEADER * hdr, short dir, short subthreads)
926 if ((Sort & SORT_MASK) != SORT_THREADS) {
927 mutt_error _("Threading is not enabled.");
929 return (hdr->virtual);
939 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0)) {
940 while (!cur->next && cur->parent)
944 while (!cur->prev && cur->parent)
949 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0)) {
954 tmp = find_virtual (cur, 0);
962 tmp = find_virtual (cur, 1);
966 return (tmp->virtual);
969 int mutt_parent_message (CONTEXT * ctx, HEADER * hdr)
973 if ((Sort & SORT_MASK) != SORT_THREADS) {
974 mutt_error _("Threading is not enabled.");
976 return (hdr->virtual);
979 for (thread = hdr->thread->parent; thread; thread = thread->parent) {
980 if ((hdr = thread->message) != NULL) {
981 if (VISIBLE (hdr, ctx))
982 return (hdr->virtual);
984 mutt_error _("Parent message is not visible in this limited view.");
991 mutt_error _("Parent message is not available.");
996 void mutt_set_virtual (CONTEXT * ctx)
1004 for (i = 0; i < ctx->msgcount; i++) {
1006 if (cur->virtual >= 0) {
1007 cur->virtual = ctx->vcount;
1008 ctx->v2r[ctx->vcount] = i;
1011 cur->content->length + cur->content->offset -
1012 cur->content->hdr_offset;
1013 cur->num_hidden = mutt_get_hidden (ctx, cur);
1018 int _mutt_traverse_thread (CONTEXT * ctx, HEADER * cur, int flag)
1020 THREAD *thread, *top;
1021 HEADER *roothdr = NULL;
1022 int final, reverse = (Sort & SORT_REVERSE), minmsgno;
1023 int num_hidden = 0, new = 0, old = 0;
1024 int min_unread_msgno = INT_MAX, min_unread = cur->virtual;
1026 #define CHECK_LIMIT (!ctx->pattern || cur->limited)
1028 if ((Sort & SORT_MASK) != SORT_THREADS && !(flag & M_THREAD_GET_HIDDEN)) {
1029 mutt_error (_("Threading is not enabled."));
1030 return (cur->virtual);
1033 final = cur->virtual;
1034 thread = cur->thread;
1035 while (thread->parent)
1036 thread = thread->parent;
1038 while (!thread->message)
1039 thread = thread->child;
1040 cur = thread->message;
1041 minmsgno = cur->msgno;
1043 if (!cur->read && CHECK_LIMIT) {
1048 if (cur->msgno < min_unread_msgno) {
1049 min_unread = cur->virtual;
1050 min_unread_msgno = cur->msgno;
1054 if (cur->virtual == -1 && CHECK_LIMIT)
1057 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE)) {
1058 cur->pair = 0; /* force index entry's color to be re-evaluated */
1059 cur->collapsed = flag & M_THREAD_COLLAPSE;
1060 if (cur->virtual != -1) {
1062 if (flag & M_THREAD_COLLAPSE)
1063 final = roothdr->virtual;
1067 if (thread == top && (thread = thread->child) == NULL) {
1068 /* return value depends on action requested */
1069 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1071 else if (flag & M_THREAD_UNREAD)
1072 return ((old && new) ? new : (old ? old : new));
1073 else if (flag & M_THREAD_GET_HIDDEN)
1074 return (num_hidden);
1075 else if (flag & M_THREAD_NEXT_UNREAD)
1076 return (min_unread);
1080 cur = thread->message;
1083 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE)) {
1084 cur->pair = 0; /* force index entry's color to be re-evaluated */
1085 cur->collapsed = flag & M_THREAD_COLLAPSE;
1086 if (!roothdr && CHECK_LIMIT) {
1088 if (flag & M_THREAD_COLLAPSE)
1089 final = roothdr->virtual;
1092 if (reverse && (flag & M_THREAD_COLLAPSE) && (cur->msgno < minmsgno)
1094 minmsgno = cur->msgno;
1095 final = cur->virtual;
1098 if (flag & M_THREAD_COLLAPSE) {
1104 cur->virtual = cur->msgno;
1109 if (!cur->read && CHECK_LIMIT) {
1114 if (cur->msgno < min_unread_msgno) {
1115 min_unread = cur->virtual;
1116 min_unread_msgno = cur->msgno;
1120 if (cur->virtual == -1 && CHECK_LIMIT)
1125 thread = thread->child;
1126 else if (thread->next)
1127 thread = thread->next;
1131 while (!thread->next) {
1132 thread = thread->parent;
1133 if (thread == top) {
1140 thread = thread->next;
1144 /* return value depends on action requested */
1145 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1147 else if (flag & M_THREAD_UNREAD)
1148 return ((old && new) ? new : (old ? old : new));
1149 else if (flag & M_THREAD_GET_HIDDEN)
1150 return (num_hidden + 1);
1151 else if (flag & M_THREAD_NEXT_UNREAD)
1152 return (min_unread);
1159 /* if flag is 0, we want to know how many messages
1160 * are in the thread. if flag is 1, we want to know
1161 * our position in the thread. */
1162 int mutt_messages_in_thread (CONTEXT * ctx, HEADER * hdr, int flag)
1167 if ((Sort & SORT_MASK) != SORT_THREADS || !hdr->thread)
1170 threads[0] = hdr->thread;
1171 while (threads[0]->parent)
1172 threads[0] = threads[0]->parent;
1174 threads[1] = flag ? hdr->thread : threads[0]->next;
1176 for (i = 0; i < ((flag || !threads[1]) ? 1 : 2); i++) {
1177 while (!threads[i]->message)
1178 threads[i] = threads[i]->child;
1181 if (Sort & SORT_REVERSE)
1183 threads[0]->message->msgno -
1184 (threads[1] ? threads[1]->message->msgno : -1);
1187 (threads[1] ? threads[1]->message->msgno : ctx->msgcount) -
1188 threads[0]->message->msgno;
1197 HASH *mutt_make_id_hash (CONTEXT * ctx)
1203 hash = hash_create (ctx->msgcount * 2);
1205 for (i = 0; i < ctx->msgcount; i++) {
1207 if (hdr->env->message_id)
1208 hash_insert (hash, hdr->env->message_id, hdr, 0);
1214 HASH *mutt_make_subj_hash (CONTEXT * ctx)
1220 hash = hash_create (ctx->msgcount * 2);
1222 for (i = 0; i < ctx->msgcount; i++) {
1224 if (hdr->env->real_subj)
1225 hash_insert (hash, hdr->env->real_subj, hdr, 1);
1231 static void clean_references (THREAD * brk, THREAD * cur)
1237 for (; cur; cur = cur->next, done = 0) {
1238 /* parse subthread recursively */
1239 clean_references (brk, cur->child);
1242 break; /* skip pseudo-message */
1244 /* Looking for the first bad reference according to the new threading.
1245 * Optimal since Mutt stores the references in reverse order, and the
1246 * first loop should match immediatly for mails respecting RFC2822. */
1247 for (p = brk; !done && p; p = p->parent)
1248 for (ref = cur->message->env->references; p->message && ref;
1250 if (!mutt_strcasecmp (ref->data, p->message->env->message_id)) {
1256 HEADER *h = cur->message;
1258 /* clearing the References: header from obsolete Message-Id(s) */
1259 mutt_free_list (&ref->next);
1262 mutt_free_list (&h->new_env->references);
1264 h->new_env = mutt_new_envelope ();
1266 h->new_env->references = mutt_copy_list (h->env->references);
1268 h->refs_changed = h->changed = 1;
1273 void mutt_break_thread (HEADER * hdr)
1275 mutt_free_list (&hdr->env->in_reply_to);
1276 mutt_free_list (&hdr->env->references);
1277 hdr->irt_changed = hdr->refs_changed = hdr->changed = 1;
1280 mutt_free_list (&hdr->new_env->in_reply_to);
1281 mutt_free_list (&hdr->new_env->references);
1284 hdr->new_env = mutt_new_envelope ();
1286 clean_references (hdr->thread, hdr->thread->child);
1289 static int link_threads (HEADER * parent, HEADER * child, CONTEXT * ctx)
1291 if (child == parent)
1294 mutt_break_thread (child);
1296 child->env->in_reply_to = mutt_new_list ();
1297 child->env->in_reply_to->data = safe_strdup (parent->env->message_id);
1299 child->new_env->in_reply_to = mutt_new_list ();
1300 child->new_env->in_reply_to->data = safe_strdup (parent->env->message_id);
1302 mutt_set_flag (ctx, child, M_TAG, 0);
1304 child->irt_changed = child->changed = 1;
1308 int mutt_link_threads (HEADER * cur, HEADER * last, CONTEXT * ctx)
1313 for (i = 0; i < ctx->vcount; i++)
1314 if (ctx->hdrs[Context->v2r[i]]->tagged)
1315 changed |= link_threads (cur, ctx->hdrs[Context->v2r[i]], ctx);
1318 changed = link_threads (cur, last, ctx);