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.
23 #define VISIBLE(hdr, ctx) (hdr->virtual >= 0 || (hdr->collapsed && (!ctx->pattern || hdr->limited)))
25 /* determine whether a is a descendant of b */
26 static int is_descendant (THREAD * a, THREAD * b)
36 /* Determines whether to display a message's subject. */
37 static int need_display_subject (CONTEXT * ctx, HEADER * hdr)
39 THREAD *tmp, *tree = hdr->thread;
41 /* if the user disabled subject hiding, display it */
42 if (!option (OPTHIDETHREADSUBJECT))
45 /* if our subject is different from our parent's, display it */
46 if (hdr->subject_changed)
49 /* if our subject is different from that of our closest previously displayed
50 * sibling, display the subject */
51 for (tmp = tree->prev; tmp; tmp = tmp->prev) {
53 if (hdr && VISIBLE (hdr, ctx)) {
54 if (hdr->subject_changed)
61 /* if there is a parent-to-child subject change anywhere between us and our
62 * closest displayed ancestor, display the subject */
63 for (tmp = tree->parent; tmp; tmp = tmp->parent) {
66 if (VISIBLE (hdr, ctx))
68 else if (hdr->subject_changed)
73 /* if we have no visible parent or previous sibling, display the subject */
77 static void linearize_tree (CONTEXT * ctx)
79 THREAD *tree = ctx->tree;
80 HEADER **array = ctx->hdrs + (Sort & SORT_REVERSE ? ctx->msgcount - 1 : 0);
83 while (!tree->message)
86 *array = tree->message;
87 array += Sort & SORT_REVERSE ? -1 : 1;
104 /* this calculates whether a node is the root of a subtree that has visible
105 * nodes, whether a node itself is visible, whether, if invisible, it has
106 * depth anyway, and whether any of its later siblings are roots of visible
107 * subtrees. while it's at it, it frees the old thread display, so we can
108 * skip parts of the tree in mutt_draw_tree() if we've decided here that we
109 * don't care about them any more.
111 static void calculate_visibility (CONTEXT * ctx, int *max_depth)
113 THREAD *tmp, *tree = ctx->tree;
114 int hide_top_missing = option (OPTHIDETOPMISSING)
115 && !option (OPTHIDEMISSING);
116 int hide_top_limited = option (OPTHIDETOPLIMITED)
117 && !option (OPTHIDELIMITED);
120 /* we walk each level backwards to make it easier to compute next_subtree_visible */
126 if (depth > *max_depth)
129 tree->subtree_visible = 0;
131 FREE (&tree->message->tree);
132 if (VISIBLE (tree->message, ctx)) {
135 tree->message->display_subject =
136 need_display_subject (ctx, tree->message);
137 for (tmp = tree; tmp; tmp = tmp->parent) {
138 if (tmp->subtree_visible) {
140 tmp->subtree_visible = 2;
144 tmp->subtree_visible = 1;
149 tree->deep = !option (OPTHIDELIMITED);
154 tree->deep = !option (OPTHIDEMISSING);
156 tree->next_subtree_visible = tree->next
157 && (tree->next->next_subtree_visible || tree->next->subtree_visible);
167 while (tree && !tree->prev) {
178 /* now fix up for the OPTHIDETOP* options if necessary */
179 if (hide_top_limited || hide_top_missing) {
182 if (!tree->visible && tree->deep && tree->subtree_visible < 2
183 && ((tree->message && hide_top_limited)
184 || (!tree->message && hide_top_missing)))
186 if (!tree->deep && tree->child && tree->subtree_visible)
191 while (tree && !tree->next)
202 /* Since the graphics characters have a value >255, I have to resort to
203 * using escape sequences to pass the information to print_enriched_string().
204 * These are the macros M_TREE_* defined in mutt.h.
206 * ncurses should automatically use the default ASCII characters instead of
207 * graphics chars on terminals which don't support them (see the man page
210 void mutt_draw_tree (CONTEXT * ctx)
212 char *pfx = NULL, *mypfx = NULL, *arrow = NULL, *myarrow = NULL, *new_tree;
213 char corner = (Sort & SORT_REVERSE) ? M_TREE_ULCORNER : M_TREE_LLCORNER;
214 char vtee = (Sort & SORT_REVERSE) ? M_TREE_BTEE : M_TREE_TTEE;
215 int depth = 0, start_depth = 0, max_depth = 0, width =
216 option (OPTNARROWTREE) ? 1 : 2;
217 THREAD *nextdisp = NULL, *pseudo = NULL, *parent = NULL, *tree = ctx->tree;
219 /* Do the visibility calculations and free the old thread chars.
220 * From now on we can simply ignore invisible subtrees
222 calculate_visibility (ctx, &max_depth);
223 pfx = safe_malloc (width * max_depth + 2);
224 arrow = safe_malloc (width * max_depth + 2);
227 myarrow = arrow + (depth - start_depth - (start_depth ? 0 : 1)) * width;
228 if (depth && start_depth == depth)
229 myarrow[0] = nextdisp ? M_TREE_LTEE : corner;
230 else if (parent->message && !option (OPTHIDELIMITED))
231 myarrow[0] = M_TREE_HIDDEN;
232 else if (!parent->message && !option (OPTHIDEMISSING))
233 myarrow[0] = M_TREE_MISSING;
237 myarrow[1] = pseudo ? M_TREE_STAR
238 : (tree->duplicate_thread ? M_TREE_EQUALS : M_TREE_HLINE);
240 myarrow[width] = M_TREE_RARROW;
241 myarrow[width + 1] = 0;
242 new_tree = safe_malloc ((2 + depth * width));
243 if (start_depth > 1) {
244 strncpy (new_tree, pfx, (start_depth - 1) * width);
245 strfcpy (new_tree + (start_depth - 1) * width,
246 arrow, (1 + depth - start_depth) * width + 2);
249 strfcpy (new_tree, arrow, 2 + depth * width);
250 tree->message->tree = new_tree;
253 if (tree->child && depth) {
254 mypfx = pfx + (depth - 1) * width;
255 mypfx[0] = nextdisp ? M_TREE_VLINE : M_TREE_SPACE;
257 mypfx[1] = M_TREE_SPACE;
263 if (tree->child && tree->subtree_visible) {
270 /* we do this here because we need to make sure that the first child thread
271 * of the old tree that we deal with is actually displayed if any are,
272 * or we might set the parent variable wrong while going through it. */
273 while (!tree->subtree_visible && tree->next)
277 while (!tree->next && tree->parent) {
280 if (tree == nextdisp)
286 if (start_depth == depth)
293 if (tree == nextdisp)
301 if (!pseudo && tree->fake_thread)
303 if (!nextdisp && tree->next_subtree_visible)
313 /* since we may be trying to attach as a pseudo-thread a THREAD that
314 * has no message, we have to make a list of all the subjects of its
315 * most immediate existing descendants. we also note the earliest
316 * date on any of the parents and put it in *dateptr. */
317 static LIST *make_subject_list (THREAD * cur, time_t * dateptr)
322 LIST *curlist, *oldlist, *newlist, *subjects = NULL;
326 while (!cur->message)
330 thisdate = option (OPTTHREADRECEIVED)
331 ? cur->message->received : cur->message->date_sent;
332 if (!*dateptr || thisdate < *dateptr)
336 env = cur->message->env;
337 if (env->real_subj &&
338 ((env->real_subj != env->subject) || (!option (OPTSORTRE)))) {
339 for (curlist = subjects, oldlist = NULL;
340 curlist; oldlist = curlist, curlist = curlist->next) {
341 rc = mutt_strcmp (env->real_subj, curlist->data);
345 if (!curlist || rc > 0) {
346 newlist = safe_calloc (1, sizeof (LIST));
347 newlist->data = env->real_subj;
349 newlist->next = oldlist->next;
350 oldlist->next = newlist;
353 newlist->next = subjects;
359 while (!cur->next && cur != start) {
370 /* find the best possible match for a parent mesage based upon subject.
371 * if there are multiple matches, the one which was sent the latest, but
372 * before the current message, is used.
374 static THREAD *find_subject (CONTEXT * ctx, THREAD * cur)
376 struct hash_elem *ptr;
377 THREAD *tmp, *last = NULL;
379 LIST *subjects = NULL, *oldlist;
382 subjects = make_subject_list (cur, &date);
385 hash = hash_string ((unsigned char *) subjects->data,
386 ctx->subj_hash->nelem);
387 for (ptr = ctx->subj_hash->table[hash]; ptr; ptr = ptr->next) {
388 tmp = ((HEADER *) ptr->data)->thread;
389 if (tmp != cur && /* don't match the same message */
390 !tmp->fake_thread && /* don't match pseudo threads */
391 tmp->message->subject_changed && /* only match interesting replies */
392 !is_descendant (tmp, cur) && /* don't match in the same thread */
393 (date >= (option (OPTTHREADRECEIVED) ?
394 tmp->message->received :
395 tmp->message->date_sent)) &&
397 (option (OPTTHREADRECEIVED) ?
398 (last->message->received < tmp->message->received) :
399 (last->message->date_sent < tmp->message->date_sent))) &&
400 tmp->message->env->real_subj &&
401 mutt_strcmp (subjects->data, tmp->message->env->real_subj) == 0)
402 last = tmp; /* best match so far */
406 subjects = subjects->next;
412 /* remove cur and its descendants from their current location.
413 * also make sure ancestors of cur no longer are sorted by the
414 * fact that cur is their descendant. */
415 static void unlink_message (THREAD ** old, THREAD * cur)
420 cur->prev->next = cur->next;
425 cur->next->prev = cur->prev;
428 for (tmp = cur->parent; tmp && tmp->sort_key == cur->sort_key;
430 tmp->sort_key = NULL;
434 /* add cur as a prior sibling of *new, with parent newparent */
435 static void insert_message (THREAD ** new, THREAD * newparent, THREAD * cur)
440 cur->parent = newparent;
446 /* thread by subject things that didn't get threaded by message-id */
447 static void pseudo_threads (CONTEXT * ctx)
449 THREAD *tree = ctx->tree, *top = tree;
450 THREAD *tmp, *cur, *parent, *curchild, *nextchild;
453 ctx->subj_hash = mutt_make_subj_hash (ctx);
458 if ((parent = find_subject (ctx, cur)) != NULL) {
459 cur->fake_thread = 1;
460 unlink_message (&top, cur);
461 insert_message (&parent->child, parent, cur);
462 parent->sort_children = 1;
465 while (!tmp->message)
468 /* if the message we're attaching has pseudo-children, they
469 * need to be attached to its parent, so move them up a level.
470 * but only do this if they have the same real subject as the
471 * parent, since otherwise they rightly belong to the message
472 * we're attaching. */
474 || !mutt_strcmp (tmp->message->env->real_subj,
475 parent->message->env->real_subj)) {
476 tmp->message->subject_changed = 0;
478 for (curchild = tmp->child; curchild;) {
479 nextchild = curchild->next;
480 if (curchild->fake_thread) {
481 unlink_message (&tmp->child, curchild);
482 insert_message (&parent->child, parent, curchild);
484 curchild = nextchild;
488 while (!tmp->next && tmp != cur) {
501 void mutt_clear_threads (CONTEXT * ctx)
505 for (i = 0; i < ctx->msgcount; i++) {
506 ctx->hdrs[i]->thread = NULL;
507 ctx->hdrs[i]->threaded = 0;
511 if (ctx->thread_hash)
512 hash_destroy (&ctx->thread_hash, free);
515 int compare_threads (const void *a, const void *b)
517 static sort_t *sort_func = NULL;
520 return ((*sort_func) (&(*((THREAD **) a))->sort_key,
521 &(*((THREAD **) b))->sort_key));
522 /* a hack to let us reset sort_func even though we can't
523 * have extra arguments because of qsort
527 sort_func = mutt_get_sort_func (Sort);
528 return (sort_func ? 1 : 0);
532 THREAD *mutt_sort_subthreads (THREAD * thread, int init)
534 THREAD **array, *sort_key, *top, *tmp;
536 int i, array_size, sort_top = 0;
538 /* we put things into the array backwards to save some cycles,
539 * but we want to have to move less stuff around if we're
540 * resorting, so we sort backwards and then put them back
541 * in reverse order so they're forwards
543 Sort ^= SORT_REVERSE;
544 if (!compare_threads (NULL, NULL))
549 array = safe_calloc ((array_size = 256), sizeof (THREAD *));
551 if (init || !thread->sort_key) {
552 thread->sort_key = NULL;
555 thread->parent->sort_children = 1;
561 thread = thread->child;
565 /* if it has no children, it must be real. sort it on its own merits */
566 thread->sort_key = thread->message;
569 thread = thread->next;
574 while (!thread->next) {
575 /* if it has siblings and needs to be sorted, sort it... */
577 && (thread->parent ? thread->parent->sort_children : sort_top)) {
578 /* put them into the array */
579 for (i = 0; thread; i++, thread = thread->prev) {
581 safe_realloc (&array, (array_size *= 2) * sizeof (THREAD *));
586 qsort ((void *) array, i, sizeof (THREAD *), compare_threads);
588 /* attach them back together. make thread the last sibling. */
591 array[i - 1]->prev = NULL;
594 thread->parent->child = array[i - 1];
599 array[i - 1]->prev = array[i];
600 array[i]->next = array[i - 1];
604 if (thread->parent) {
606 thread = thread->parent;
608 if (!thread->sort_key || thread->sort_children) {
609 /* make sort_key the first or last sibling, as appropriate */
611 (!(Sort & SORT_LAST) ^ !(Sort & SORT_REVERSE)) ? thread->
614 /* we just sorted its children */
615 thread->sort_children = 0;
617 oldsort_key = thread->sort_key;
618 thread->sort_key = thread->message;
620 if (Sort & SORT_LAST) {
621 if (!thread->sort_key || ((((Sort & SORT_REVERSE) ? 1 : -1)
622 * compare_threads ((void *) &thread,
625 thread->sort_key = sort_key->sort_key;
627 else if (!thread->sort_key)
628 thread->sort_key = sort_key->sort_key;
630 /* if its sort_key has changed, we need to resort it and siblings */
631 if (oldsort_key != thread->sort_key) {
633 thread->parent->sort_children = 1;
640 Sort ^= SORT_REVERSE;
646 thread = thread->next;
650 static void check_subjects (CONTEXT * ctx, int init)
656 for (i = 0; i < ctx->msgcount; i++) {
658 if (cur->thread->check_subject)
659 cur->thread->check_subject = 0;
663 /* figure out which messages have subjects different than their parents' */
664 tmp = cur->thread->parent;
665 while (tmp && !tmp->message) {
670 cur->subject_changed = 1;
671 else if (cur->env->real_subj && tmp->message->env->real_subj)
672 cur->subject_changed = mutt_strcmp (cur->env->real_subj,
676 cur->subject_changed = (cur->env->real_subj
677 || tmp->message->env->real_subj) ? 1 : 0;
681 void mutt_sort_threads (CONTEXT * ctx, int init)
684 int i, oldsort, using_refs = 0;
685 THREAD *thread, *new, *tmp, top;
688 /* set Sort to the secondary method to support the set sort_aux=reverse-*
689 * settings. The sorting functions just look at the value of
695 if (!ctx->thread_hash)
699 ctx->thread_hash = hash_create (ctx->msgcount * 2);
701 /* we want a quick way to see if things are actually attached to the top of the
702 * thread tree or if they're just dangling, so we attach everything to a top
703 * node temporarily */
704 top.parent = top.next = top.prev = NULL;
705 top.child = ctx->tree;
706 for (thread = ctx->tree; thread; thread = thread->next)
707 thread->parent = ⊤
709 /* put each new message together with the matching messageless THREAD if it
710 * exists. otherwise, if there is a THREAD that already has a message, thread
711 * new message as an identical child. if we didn't attach the message to a
712 * THREAD, make a new one for it. */
713 for (i = 0; i < ctx->msgcount; i++) {
717 if ((!init || option (OPTDUPTHREADS)) && cur->env->message_id)
718 thread = hash_find (ctx->thread_hash, cur->env->message_id);
722 if (thread && !thread->message) {
723 /* this is a message which was missing before */
724 thread->message = cur;
725 cur->thread = thread;
726 thread->check_subject = 1;
728 /* mark descendants as needing subject_changed checked */
729 for (tmp = (thread->child ? thread->child : thread); tmp != thread;) {
730 while (!tmp->message)
732 tmp->check_subject = 1;
733 while (!tmp->next && tmp != thread)
739 if (thread->parent) {
740 /* remove threading info above it based on its children, which we'll
741 * recalculate based on its headers. make sure not to leave
742 * dangling missing messages. note that we haven't kept track
743 * of what info came from its children and what from its siblings'
744 * children, so we just remove the stuff that's definitely from it */
746 tmp = thread->parent;
747 unlink_message (&tmp->child, thread);
748 thread->parent = NULL;
749 thread->sort_key = NULL;
750 thread->fake_thread = 0;
752 } while (thread != &top && !thread->child && !thread->message);
756 new = (option (OPTDUPTHREADS) ? thread : NULL);
758 thread = safe_calloc (1, sizeof (THREAD));
759 thread->message = cur;
760 thread->check_subject = 1;
761 cur->thread = thread;
762 hash_insert (ctx->thread_hash,
763 cur->env->message_id ? cur->env->message_id : "",
767 if (new->duplicate_thread)
770 thread = cur->thread;
772 insert_message (&new->child, new, thread);
773 thread->duplicate_thread = 1;
774 thread->message->threaded = 1;
779 /* unlink pseudo-threads because they might be children of newly
780 * arrived messages */
781 thread = cur->thread;
782 for (new = thread->child; new;) {
784 if (new->fake_thread) {
785 unlink_message (&thread->child, new);
786 insert_message (&top.child, &top, new);
787 new->fake_thread = 0;
794 /* thread by references */
795 for (i = 0; i < ctx->msgcount; i++) {
801 thread = cur->thread;
805 if (using_refs == 0) {
806 /* look at the beginning of in-reply-to: */
807 if ((ref = cur->env->in_reply_to) != NULL)
810 ref = cur->env->references;
814 else if (using_refs == 1) {
815 /* if there's no references header, use all the in-reply-to:
816 * data that we have. otherwise, use the first reference
817 * if it's different than the first in-reply-to, otherwise use
818 * the second reference (since at least eudora puts the most
819 * recent reference in in-reply-to and the rest in references)
821 if (!cur->env->references)
824 if (mutt_strcmp (ref->data, cur->env->references->data))
825 ref = cur->env->references;
827 ref = cur->env->references->next;
833 ref = ref->next; /* go on with references */
838 if ((new = hash_find (ctx->thread_hash, ref->data)) == NULL) {
839 new = safe_calloc (1, sizeof (THREAD));
840 hash_insert (ctx->thread_hash, ref->data, new, 1);
843 if (new->duplicate_thread)
845 if (is_descendant (new, thread)) /* no loops! */
850 unlink_message (&top.child, thread);
851 insert_message (&new->child, new, thread);
853 if (thread->message || (thread->parent && thread->parent != &top))
858 insert_message (&top.child, &top, thread);
861 /* detach everything from the temporary top node */
862 for (thread = top.child; thread; thread = thread->next) {
863 thread->parent = NULL;
865 ctx->tree = top.child;
867 check_subjects (ctx, init);
869 if (!option (OPTSTRICTTHREADS))
870 pseudo_threads (ctx);
873 ctx->tree = mutt_sort_subthreads (ctx->tree, init);
875 /* restore the oldsort order. */
878 /* Put the list into an array. */
879 linearize_tree (ctx);
881 /* Draw the thread tree. */
882 mutt_draw_tree (ctx);
886 static HEADER *find_virtual (THREAD * cur, int reverse)
890 if (cur->message && cur->message->virtual >= 0)
891 return (cur->message);
894 if ((cur = cur->child) == NULL)
897 while (reverse && cur->next)
901 if (cur->message && cur->message->virtual >= 0)
902 return (cur->message);
907 while (reverse && cur->next)
910 else if (reverse ? cur->prev : cur->next)
911 cur = reverse ? cur->prev : cur->next;
913 while (!(reverse ? cur->prev : cur->next)) {
918 cur = reverse ? cur->prev : cur->next;
924 int _mutt_aside_thread (HEADER * hdr, short dir, short subthreads)
929 if ((Sort & SORT_MASK) != SORT_THREADS) {
930 mutt_error _("Threading is not enabled.");
932 return (hdr->virtual);
942 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0)) {
943 while (!cur->next && cur->parent)
947 while (!cur->prev && cur->parent)
952 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0)) {
957 tmp = find_virtual (cur, 0);
965 tmp = find_virtual (cur, 1);
969 return (tmp->virtual);
972 int mutt_parent_message (CONTEXT * ctx, HEADER * hdr)
976 if ((Sort & SORT_MASK) != SORT_THREADS) {
977 mutt_error _("Threading is not enabled.");
979 return (hdr->virtual);
982 for (thread = hdr->thread->parent; thread; thread = thread->parent) {
983 if ((hdr = thread->message) != NULL) {
984 if (VISIBLE (hdr, ctx))
985 return (hdr->virtual);
987 mutt_error _("Parent message is not visible in this limited view.");
994 mutt_error _("Parent message is not available.");
999 void mutt_set_virtual (CONTEXT * ctx)
1007 for (i = 0; i < ctx->msgcount; i++) {
1009 if (cur->virtual >= 0) {
1010 cur->virtual = ctx->vcount;
1011 ctx->v2r[ctx->vcount] = i;
1014 cur->content->length + cur->content->offset -
1015 cur->content->hdr_offset;
1016 cur->num_hidden = mutt_get_hidden (ctx, cur);
1021 int _mutt_traverse_thread (CONTEXT * ctx, HEADER * cur, int flag)
1023 THREAD *thread, *top;
1024 HEADER *roothdr = NULL;
1025 int final, reverse = (Sort & SORT_REVERSE), minmsgno;
1026 int num_hidden = 0, new = 0, old = 0;
1027 int min_unread_msgno = INT_MAX, min_unread = cur->virtual;
1029 #define CHECK_LIMIT (!ctx->pattern || cur->limited)
1031 if ((Sort & SORT_MASK) != SORT_THREADS && !(flag & M_THREAD_GET_HIDDEN)) {
1032 mutt_error (_("Threading is not enabled."));
1033 return (cur->virtual);
1036 final = cur->virtual;
1037 thread = cur->thread;
1038 while (thread->parent)
1039 thread = thread->parent;
1041 while (!thread->message)
1042 thread = thread->child;
1043 cur = thread->message;
1044 minmsgno = cur->msgno;
1046 if (!cur->read && CHECK_LIMIT) {
1051 if (cur->msgno < min_unread_msgno) {
1052 min_unread = cur->virtual;
1053 min_unread_msgno = cur->msgno;
1057 if (cur->virtual == -1 && CHECK_LIMIT)
1060 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE)) {
1061 cur->pair = 0; /* force index entry's color to be re-evaluated */
1062 cur->collapsed = flag & M_THREAD_COLLAPSE;
1063 if (cur->virtual != -1) {
1065 if (flag & M_THREAD_COLLAPSE)
1066 final = roothdr->virtual;
1070 if (thread == top && (thread = thread->child) == NULL) {
1071 /* return value depends on action requested */
1072 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1074 else if (flag & M_THREAD_UNREAD)
1075 return ((old && new) ? new : (old ? old : new));
1076 else if (flag & M_THREAD_GET_HIDDEN)
1077 return (num_hidden);
1078 else if (flag & M_THREAD_NEXT_UNREAD)
1079 return (min_unread);
1083 cur = thread->message;
1086 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE)) {
1087 cur->pair = 0; /* force index entry's color to be re-evaluated */
1088 cur->collapsed = flag & M_THREAD_COLLAPSE;
1089 if (!roothdr && CHECK_LIMIT) {
1091 if (flag & M_THREAD_COLLAPSE)
1092 final = roothdr->virtual;
1095 if (reverse && (flag & M_THREAD_COLLAPSE) && (cur->msgno < minmsgno)
1097 minmsgno = cur->msgno;
1098 final = cur->virtual;
1101 if (flag & M_THREAD_COLLAPSE) {
1107 cur->virtual = cur->msgno;
1112 if (!cur->read && CHECK_LIMIT) {
1117 if (cur->msgno < min_unread_msgno) {
1118 min_unread = cur->virtual;
1119 min_unread_msgno = cur->msgno;
1123 if (cur->virtual == -1 && CHECK_LIMIT)
1128 thread = thread->child;
1129 else if (thread->next)
1130 thread = thread->next;
1134 while (!thread->next) {
1135 thread = thread->parent;
1136 if (thread == top) {
1143 thread = thread->next;
1147 /* return value depends on action requested */
1148 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1150 else if (flag & M_THREAD_UNREAD)
1151 return ((old && new) ? new : (old ? old : new));
1152 else if (flag & M_THREAD_GET_HIDDEN)
1153 return (num_hidden + 1);
1154 else if (flag & M_THREAD_NEXT_UNREAD)
1155 return (min_unread);
1162 /* if flag is 0, we want to know how many messages
1163 * are in the thread. if flag is 1, we want to know
1164 * our position in the thread. */
1165 int mutt_messages_in_thread (CONTEXT * ctx, HEADER * hdr, int flag)
1170 if ((Sort & SORT_MASK) != SORT_THREADS || !hdr->thread)
1173 threads[0] = hdr->thread;
1174 while (threads[0]->parent)
1175 threads[0] = threads[0]->parent;
1177 threads[1] = flag ? hdr->thread : threads[0]->next;
1179 for (i = 0; i < ((flag || !threads[1]) ? 1 : 2); i++) {
1180 while (!threads[i]->message)
1181 threads[i] = threads[i]->child;
1184 if (Sort & SORT_REVERSE)
1186 threads[0]->message->msgno -
1187 (threads[1] ? threads[1]->message->msgno : -1);
1190 (threads[1] ? threads[1]->message->msgno : ctx->msgcount) -
1191 threads[0]->message->msgno;
1200 HASH *mutt_make_id_hash (CONTEXT * ctx)
1206 hash = hash_create (ctx->msgcount * 2);
1208 for (i = 0; i < ctx->msgcount; i++) {
1210 if (hdr->env->message_id)
1211 hash_insert (hash, hdr->env->message_id, hdr, 0);
1217 HASH *mutt_make_subj_hash (CONTEXT * ctx)
1223 hash = hash_create (ctx->msgcount * 2);
1225 for (i = 0; i < ctx->msgcount; i++) {
1227 if (hdr->env->real_subj)
1228 hash_insert (hash, hdr->env->real_subj, hdr, 1);
1234 static void clean_references (THREAD * brk, THREAD * cur)
1240 for (; cur; cur = cur->next, done = 0) {
1241 /* parse subthread recursively */
1242 clean_references (brk, cur->child);
1245 break; /* skip pseudo-message */
1247 /* Looking for the first bad reference according to the new threading.
1248 * Optimal since Mutt stores the references in reverse order, and the
1249 * first loop should match immediatly for mails respecting RFC2822. */
1250 for (p = brk; !done && p; p = p->parent)
1251 for (ref = cur->message->env->references; p->message && ref;
1253 if (!mutt_strcasecmp (ref->data, p->message->env->message_id)) {
1259 HEADER *h = cur->message;
1261 /* clearing the References: header from obsolete Message-Id(s) */
1262 mutt_free_list (&ref->next);
1265 mutt_free_list (&h->new_env->references);
1267 h->new_env = mutt_new_envelope ();
1269 h->new_env->references = mutt_copy_list (h->env->references);
1271 h->refs_changed = h->changed = 1;
1276 void mutt_break_thread (HEADER * hdr)
1278 mutt_free_list (&hdr->env->in_reply_to);
1279 mutt_free_list (&hdr->env->references);
1280 hdr->irt_changed = hdr->refs_changed = hdr->changed = 1;
1283 mutt_free_list (&hdr->new_env->in_reply_to);
1284 mutt_free_list (&hdr->new_env->references);
1287 hdr->new_env = mutt_new_envelope ();
1289 clean_references (hdr->thread, hdr->thread->child);
1292 static int link_threads (HEADER * parent, HEADER * child, CONTEXT * ctx)
1294 if (child == parent)
1297 mutt_break_thread (child);
1299 child->env->in_reply_to = mutt_new_list ();
1300 child->env->in_reply_to->data = safe_strdup (parent->env->message_id);
1302 child->new_env->in_reply_to = mutt_new_list ();
1303 child->new_env->in_reply_to->data = safe_strdup (parent->env->message_id);
1305 mutt_set_flag (ctx, child, M_TAG, 0);
1307 child->irt_changed = child->changed = 1;
1311 int mutt_link_threads (HEADER * cur, HEADER * last, CONTEXT * ctx)
1316 for (i = 0; i < ctx->vcount; i++)
1317 if (ctx->hdrs[Context->v2r[i]]->tagged)
1318 changed |= link_threads (cur, ctx->hdrs[Context->v2r[i]], ctx);
1321 changed = link_threads (cur, last, ctx);