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.
25 #define VISIBLE(hdr, ctx) (hdr->virtual >= 0 || (hdr->collapsed && (!ctx->pattern || hdr->limited)))
27 /* determine whether a is a descendant of b */
28 static int is_descendant (THREAD *a, THREAD *b)
39 /* Determines whether to display a message's subject. */
40 static int need_display_subject (CONTEXT *ctx, HEADER *hdr)
42 THREAD *tmp, *tree = hdr->thread;
44 /* if the user disabled subject hiding, display it */
45 if (!option (OPTHIDETHREADSUBJECT))
48 /* if our subject is different from our parent's, display it */
49 if (hdr->subject_changed)
52 /* if our subject is different from that of our closest previously displayed
53 * sibling, display the subject */
54 for (tmp = tree->prev; tmp; tmp = tmp->prev)
57 if (hdr && VISIBLE (hdr, ctx))
59 if (hdr->subject_changed)
66 /* if there is a parent-to-child subject change anywhere between us and our
67 * closest displayed ancestor, display the subject */
68 for (tmp = tree->parent; tmp; tmp = tmp->parent)
73 if (VISIBLE (hdr, ctx))
75 else if (hdr->subject_changed)
80 /* if we have no visible parent or previous sibling, display the subject */
84 static void linearize_tree (CONTEXT *ctx)
86 THREAD *tree = ctx->tree;
87 HEADER **array = ctx->hdrs + (Sort & SORT_REVERSE ? ctx->msgcount - 1 : 0);
91 while (!tree->message)
94 *array = tree->message;
95 array += Sort & SORT_REVERSE ? -1 : 1;
115 /* this calculates whether a node is the root of a subtree that has visible
116 * nodes, whether a node itself is visible, whether, if invisible, it has
117 * depth anyway, and whether any of its later siblings are roots of visible
118 * subtrees. while it's at it, it frees the old thread display, so we can
119 * skip parts of the tree in mutt_draw_tree() if we've decided here that we
120 * don't care about them any more.
122 static void calculate_visibility (CONTEXT *ctx, int *max_depth)
124 THREAD *tmp, *tree = ctx->tree;
125 int hide_top_missing = option (OPTHIDETOPMISSING) && !option (OPTHIDEMISSING);
126 int hide_top_limited = option (OPTHIDETOPLIMITED) && !option (OPTHIDELIMITED);
129 /* we walk each level backwards to make it easier to compute next_subtree_visible */
136 if (depth > *max_depth)
139 tree->subtree_visible = 0;
142 FREE (&tree->message->tree);
143 if (VISIBLE (tree->message, ctx))
147 tree->message->display_subject = need_display_subject (ctx, tree->message);
148 for (tmp = tree; tmp; tmp = tmp->parent)
150 if (tmp->subtree_visible)
153 tmp->subtree_visible = 2;
157 tmp->subtree_visible = 1;
163 tree->deep = !option (OPTHIDELIMITED);
169 tree->deep = !option (OPTHIDEMISSING);
171 tree->next_subtree_visible = tree->next && (tree->next->next_subtree_visible
172 || tree->next->subtree_visible);
184 while (tree && !tree->prev)
196 /* now fix up for the OPTHIDETOP* options if necessary */
197 if (hide_top_limited || hide_top_missing)
202 if (!tree->visible && tree->deep && tree->subtree_visible < 2
203 && ((tree->message && hide_top_limited) || (!tree->message && hide_top_missing)))
205 if (!tree->deep && tree->child && tree->subtree_visible)
211 while (tree && !tree->next)
222 /* Since the graphics characters have a value >255, I have to resort to
223 * using escape sequences to pass the information to print_enriched_string().
224 * These are the macros M_TREE_* defined in mutt.h.
226 * ncurses should automatically use the default ASCII characters instead of
227 * graphics chars on terminals which don't support them (see the man page
230 void mutt_draw_tree (CONTEXT *ctx)
232 char *pfx = NULL, *mypfx = NULL, *arrow = NULL, *myarrow = NULL, *new_tree;
233 char corner = (Sort & SORT_REVERSE) ? M_TREE_ULCORNER : M_TREE_LLCORNER;
234 char vtee = (Sort & SORT_REVERSE) ? M_TREE_BTEE : M_TREE_TTEE;
235 int depth = 0, start_depth = 0, max_depth = 0, width = option (OPTNARROWTREE) ? 1 : 2;
236 THREAD *nextdisp = NULL, *pseudo = NULL, *parent = NULL, *tree = ctx->tree;
238 /* Do the visibility calculations and free the old thread chars.
239 * From now on we can simply ignore invisible subtrees
241 calculate_visibility (ctx, &max_depth);
242 pfx = safe_malloc (width * max_depth + 2);
243 arrow = safe_malloc (width * max_depth + 2);
248 myarrow = arrow + (depth - start_depth - (start_depth ? 0 : 1)) * width;
249 if (depth && start_depth == depth)
250 myarrow[0] = nextdisp ? M_TREE_LTEE : corner;
251 else if (parent->message && !option (OPTHIDELIMITED))
252 myarrow[0] = M_TREE_HIDDEN;
253 else if (!parent->message && !option (OPTHIDEMISSING))
254 myarrow[0] = M_TREE_MISSING;
258 myarrow[1] = pseudo ? M_TREE_STAR
259 : (tree->duplicate_thread ? M_TREE_EQUALS : M_TREE_HLINE);
262 myarrow[width] = M_TREE_RARROW;
263 myarrow[width + 1] = 0;
264 new_tree = safe_malloc ((2 + depth * width));
267 strncpy (new_tree, pfx, (start_depth - 1) * width);
268 strfcpy (new_tree + (start_depth - 1) * width,
269 arrow, (1 + depth - start_depth) * width + 2);
272 strfcpy (new_tree, arrow, 2 + depth * width);
273 tree->message->tree = new_tree;
276 if (tree->child && depth)
278 mypfx = pfx + (depth - 1) * width;
279 mypfx[0] = nextdisp ? M_TREE_VLINE : M_TREE_SPACE;
281 mypfx[1] = M_TREE_SPACE;
288 if (tree->child && tree->subtree_visible)
296 /* we do this here because we need to make sure that the first child thread
297 * of the old tree that we deal with is actually displayed if any are,
298 * or we might set the parent variable wrong while going through it. */
299 while (!tree->subtree_visible && tree->next)
304 while (!tree->next && tree->parent)
308 if (tree == nextdisp)
315 if (start_depth == depth)
322 if (tree == nextdisp)
330 if (!pseudo && tree->fake_thread)
332 if (!nextdisp && tree->next_subtree_visible)
342 /* since we may be trying to attach as a pseudo-thread a THREAD that
343 * has no message, we have to make a list of all the subjects of its
344 * most immediate existing descendants. we also note the earliest
345 * date on any of the parents and put it in *dateptr. */
346 static LIST *make_subject_list (THREAD *cur, time_t *dateptr)
351 LIST *curlist, *oldlist, *newlist, *subjects = NULL;
356 while (!cur->message)
361 thisdate = option (OPTTHREADRECEIVED)
362 ? cur->message->received : cur->message->date_sent;
363 if (!*dateptr || thisdate < *dateptr)
367 env = cur->message->env;
368 if (env->real_subj &&
369 ((env->real_subj != env->subject) || (!option (OPTSORTRE))))
371 for (curlist = subjects, oldlist = NULL;
372 curlist; oldlist = curlist, curlist = curlist->next)
374 rc = mutt_strcmp (env->real_subj, curlist->data);
378 if (!curlist || rc > 0)
380 newlist = safe_calloc (1, sizeof (LIST));
381 newlist->data = env->real_subj;
384 newlist->next = oldlist->next;
385 oldlist->next = newlist;
389 newlist->next = subjects;
395 while (!cur->next && cur != start)
407 /* find the best possible match for a parent mesage based upon subject.
408 * if there are multiple matches, the one which was sent the latest, but
409 * before the current message, is used.
411 static THREAD *find_subject (CONTEXT *ctx, THREAD *cur)
413 struct hash_elem *ptr;
414 THREAD *tmp, *last = NULL;
416 LIST *subjects = NULL, *oldlist;
419 subjects = make_subject_list (cur, &date);
423 hash = hash_string ((unsigned char *) subjects->data,
424 ctx->subj_hash->nelem);
425 for (ptr = ctx->subj_hash->table[hash]; ptr; ptr = ptr->next)
427 tmp = ((HEADER *) ptr->data)->thread;
428 if (tmp != cur && /* don't match the same message */
429 !tmp->fake_thread && /* don't match pseudo threads */
430 tmp->message->subject_changed && /* only match interesting replies */
431 !is_descendant (tmp, cur) && /* don't match in the same thread */
432 (date >= (option (OPTTHREADRECEIVED) ?
433 tmp->message->received :
434 tmp->message->date_sent)) &&
436 (option (OPTTHREADRECEIVED) ?
437 (last->message->received < tmp->message->received) :
438 (last->message->date_sent < tmp->message->date_sent))) &&
439 tmp->message->env->real_subj &&
440 mutt_strcmp (subjects->data, tmp->message->env->real_subj) == 0)
441 last = tmp; /* best match so far */
445 subjects = subjects->next;
451 /* remove cur and its descendants from their current location.
452 * also make sure ancestors of cur no longer are sorted by the
453 * fact that cur is their descendant. */
454 static void unlink_message (THREAD **old, THREAD *cur)
459 cur->prev->next = cur->next;
464 cur->next->prev = cur->prev;
468 for (tmp = cur->parent; tmp && tmp->sort_key == cur->sort_key;
470 tmp->sort_key = NULL;
474 /* add cur as a prior sibling of *new, with parent newparent */
475 static void insert_message (THREAD **new, THREAD *newparent, THREAD *cur)
480 cur->parent = newparent;
486 /* thread by subject things that didn't get threaded by message-id */
487 static void pseudo_threads (CONTEXT *ctx)
489 THREAD *tree = ctx->tree, *top = tree;
490 THREAD *tmp, *cur, *parent, *curchild, *nextchild;
493 ctx->subj_hash = mutt_make_subj_hash (ctx);
499 if ((parent = find_subject (ctx, cur)) != NULL)
501 cur->fake_thread = 1;
502 unlink_message (&top, cur);
503 insert_message (&parent->child, parent, cur);
504 parent->sort_children = 1;
508 while (!tmp->message)
511 /* if the message we're attaching has pseudo-children, they
512 * need to be attached to its parent, so move them up a level.
513 * but only do this if they have the same real subject as the
514 * parent, since otherwise they rightly belong to the message
515 * we're attaching. */
517 || !mutt_strcmp (tmp->message->env->real_subj,
518 parent->message->env->real_subj))
520 tmp->message->subject_changed = 0;
522 for (curchild = tmp->child; curchild; )
524 nextchild = curchild->next;
525 if (curchild->fake_thread)
527 unlink_message (&tmp->child, curchild);
528 insert_message (&parent->child, parent, curchild);
530 curchild = nextchild;
534 while (!tmp->next && tmp != cur)
548 void mutt_clear_threads (CONTEXT *ctx)
552 for (i = 0; i < ctx->msgcount; i++)
554 ctx->hdrs[i]->thread = NULL;
555 ctx->hdrs[i]->threaded = 0;
559 if (ctx->thread_hash)
560 hash_destroy (&ctx->thread_hash, *free);
563 int compare_threads (const void *a, const void *b)
565 static sort_t *sort_func = NULL;
568 return ((*sort_func) (&(*((THREAD **) a))->sort_key,
569 &(*((THREAD **) b))->sort_key));
570 /* a hack to let us reset sort_func even though we can't
571 * have extra arguments because of qsort
576 sort_func = mutt_get_sort_func (Sort);
577 return (sort_func ? 1 : 0);
581 THREAD *mutt_sort_subthreads (THREAD *thread, int init)
583 THREAD **array, *sort_key, *top, *tmp;
585 int i, array_size, sort_top = 0;
587 /* we put things into the array backwards to save some cycles,
588 * but we want to have to move less stuff around if we're
589 * resorting, so we sort backwards and then put them back
590 * in reverse order so they're forwards
592 Sort ^= SORT_REVERSE;
593 if (!compare_threads (NULL, NULL))
598 array = safe_calloc ((array_size = 256), sizeof (THREAD *));
601 if (init || !thread->sort_key)
603 thread->sort_key = NULL;
606 thread->parent->sort_children = 1;
613 thread = thread->child;
618 /* if it has no children, it must be real. sort it on its own merits */
619 thread->sort_key = thread->message;
623 thread = thread->next;
628 while (!thread->next)
630 /* if it has siblings and needs to be sorted, sort it... */
631 if (thread->prev && (thread->parent ? thread->parent->sort_children : sort_top))
633 /* put them into the array */
634 for (i = 0; thread; i++, thread = thread->prev)
637 safe_realloc (&array, (array_size *= 2) * sizeof (THREAD *));
642 qsort ((void *) array, i, sizeof (THREAD *), *compare_threads);
644 /* attach them back together. make thread the last sibling. */
647 array[i - 1]->prev = NULL;
650 thread->parent->child = array[i - 1];
656 array[i - 1]->prev = array[i];
657 array[i]->next = array[i - 1];
664 thread = thread->parent;
666 if (!thread->sort_key || thread->sort_children)
668 /* make sort_key the first or last sibling, as appropriate */
669 sort_key = (!(Sort & SORT_LAST) ^ !(Sort & SORT_REVERSE)) ? thread->child : tmp;
671 /* we just sorted its children */
672 thread->sort_children = 0;
674 oldsort_key = thread->sort_key;
675 thread->sort_key = thread->message;
677 if (Sort & SORT_LAST)
679 if (!thread->sort_key
680 || ((((Sort & SORT_REVERSE) ? 1 : -1)
681 * compare_threads ((void *) &thread,
684 thread->sort_key = sort_key->sort_key;
686 else if (!thread->sort_key)
687 thread->sort_key = sort_key->sort_key;
689 /* if its sort_key has changed, we need to resort it and siblings */
690 if (oldsort_key != thread->sort_key)
693 thread->parent->sort_children = 1;
701 Sort ^= SORT_REVERSE;
707 thread = thread->next;
711 static void check_subjects (CONTEXT *ctx, int init)
717 for (i = 0; i < ctx->msgcount; i++)
720 if (cur->thread->check_subject)
721 cur->thread->check_subject = 0;
725 /* figure out which messages have subjects different than their parents' */
726 tmp = cur->thread->parent;
727 while (tmp && !tmp->message)
733 cur->subject_changed = 1;
734 else if (cur->env->real_subj && tmp->message->env->real_subj)
735 cur->subject_changed = mutt_strcmp (cur->env->real_subj,
736 tmp->message->env->real_subj) ? 1 : 0;
738 cur->subject_changed = (cur->env->real_subj
739 || tmp->message->env->real_subj) ? 1 : 0;
743 void mutt_sort_threads (CONTEXT *ctx, int init)
746 int i, oldsort, using_refs = 0;
747 THREAD *thread, *new, *tmp, top;
750 /* set Sort to the secondary method to support the set sort_aux=reverse-*
751 * settings. The sorting functions just look at the value of
757 if (!ctx->thread_hash)
761 ctx->thread_hash = hash_create (ctx->msgcount * 2);
763 /* we want a quick way to see if things are actually attached to the top of the
764 * thread tree or if they're just dangling, so we attach everything to a top
765 * node temporarily */
766 top.parent = top.next = top.prev = NULL;
767 top.child = ctx->tree;
768 for (thread = ctx->tree; thread; thread = thread->next)
769 thread->parent = ⊤
771 /* put each new message together with the matching messageless THREAD if it
772 * exists. otherwise, if there is a THREAD that already has a message, thread
773 * new message as an identical child. if we didn't attach the message to a
774 * THREAD, make a new one for it. */
775 for (i = 0; i < ctx->msgcount; i++)
781 if ((!init || option (OPTDUPTHREADS)) && cur->env->message_id)
782 thread = hash_find (ctx->thread_hash, cur->env->message_id);
786 if (thread && !thread->message)
788 /* this is a message which was missing before */
789 thread->message = cur;
790 cur->thread = thread;
791 thread->check_subject = 1;
793 /* mark descendants as needing subject_changed checked */
794 for (tmp = (thread->child ? thread->child : thread); tmp != thread; )
796 while (!tmp->message)
798 tmp->check_subject = 1;
799 while (!tmp->next && tmp != thread)
807 /* remove threading info above it based on its children, which we'll
808 * recalculate based on its headers. make sure not to leave
809 * dangling missing messages. note that we haven't kept track
810 * of what info came from its children and what from its siblings'
811 * children, so we just remove the stuff that's definitely from it */
814 tmp = thread->parent;
815 unlink_message (&tmp->child, thread);
816 thread->parent = NULL;
817 thread->sort_key = NULL;
818 thread->fake_thread = 0;
820 } while (thread != &top && !thread->child && !thread->message);
825 new = (option (OPTDUPTHREADS) ? thread : NULL);
827 thread = safe_calloc (1, sizeof (THREAD));
828 thread->message = cur;
829 thread->check_subject = 1;
830 cur->thread = thread;
831 hash_insert (ctx->thread_hash,
832 cur->env->message_id ? cur->env->message_id : "",
837 if (new->duplicate_thread)
840 thread = cur->thread;
842 insert_message (&new->child, new, thread);
843 thread->duplicate_thread = 1;
844 thread->message->threaded = 1;
850 /* unlink pseudo-threads because they might be children of newly
851 * arrived messages */
852 thread = cur->thread;
853 for (new = thread->child; new; )
856 if (new->fake_thread)
858 unlink_message (&thread->child, new);
859 insert_message (&top.child, &top, new);
860 new->fake_thread = 0;
867 /* thread by references */
868 for (i = 0; i < ctx->msgcount; i++)
875 thread = cur->thread;
882 /* look at the beginning of in-reply-to: */
883 if ((ref = cur->env->in_reply_to) != NULL)
887 ref = cur->env->references;
891 else if (using_refs == 1)
893 /* if there's no references header, use all the in-reply-to:
894 * data that we have. otherwise, use the first reference
895 * if it's different than the first in-reply-to, otherwise use
896 * the second reference (since at least eudora puts the most
897 * recent reference in in-reply-to and the rest in references)
899 if (!cur->env->references)
903 if (mutt_strcmp (ref->data, cur->env->references->data))
904 ref = cur->env->references;
906 ref = cur->env->references->next;
912 ref = ref->next; /* go on with references */
917 if ((new = hash_find (ctx->thread_hash, ref->data)) == NULL)
919 new = safe_calloc (1, sizeof (THREAD));
920 hash_insert (ctx->thread_hash, ref->data, new, 1);
924 if (new->duplicate_thread)
926 if (is_descendant (new, thread)) /* no loops! */
931 unlink_message (&top.child, thread);
932 insert_message (&new->child, new, thread);
934 if (thread->message || (thread->parent && thread->parent != &top))
939 insert_message (&top.child, &top, thread);
942 /* detach everything from the temporary top node */
943 for (thread = top.child; thread; thread = thread->next)
945 thread->parent = NULL;
947 ctx->tree = top.child;
949 check_subjects (ctx, init);
951 if (!option (OPTSTRICTTHREADS))
952 pseudo_threads (ctx);
956 ctx->tree = mutt_sort_subthreads (ctx->tree, init);
958 /* restore the oldsort order. */
961 /* Put the list into an array. */
962 linearize_tree (ctx);
964 /* Draw the thread tree. */
965 mutt_draw_tree (ctx);
969 static HEADER *find_virtual (THREAD *cur, int reverse)
973 if (cur->message && cur->message->virtual >= 0)
974 return (cur->message);
977 if ((cur = cur->child) == NULL)
980 while (reverse && cur->next)
985 if (cur->message && cur->message->virtual >= 0)
986 return (cur->message);
992 while (reverse && cur->next)
995 else if (reverse ? cur->prev : cur->next)
996 cur = reverse ? cur->prev : cur->next;
999 while (!(reverse ? cur->prev : cur->next))
1005 cur = reverse ? cur->prev : cur->next;
1011 int _mutt_aside_thread (HEADER *hdr, short dir, short subthreads)
1016 if ((Sort & SORT_MASK) != SORT_THREADS)
1018 mutt_error _("Threading is not enabled.");
1019 return (hdr->virtual);
1031 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0))
1033 while (!cur->next && cur->parent)
1038 while (!cur->prev && cur->parent)
1043 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0))
1050 tmp = find_virtual (cur, 0);
1060 tmp = find_virtual (cur, 1);
1064 return (tmp->virtual);
1067 int mutt_parent_message (CONTEXT *ctx, HEADER *hdr)
1071 if ((Sort & SORT_MASK) != SORT_THREADS)
1073 mutt_error _("Threading is not enabled.");
1074 return (hdr->virtual);
1077 for (thread = hdr->thread->parent; thread; thread = thread->parent)
1079 if ((hdr = thread->message) != NULL)
1081 if (VISIBLE (hdr, ctx))
1082 return (hdr->virtual);
1085 mutt_error _("Parent message is not visible in this limited view.");
1091 mutt_error _("Parent message is not available.");
1095 void mutt_set_virtual (CONTEXT *ctx)
1103 for (i = 0; i < ctx->msgcount; i++)
1106 if (cur->virtual >= 0)
1108 cur->virtual = ctx->vcount;
1109 ctx->v2r[ctx->vcount] = i;
1111 ctx->vsize += cur->content->length + cur->content->offset - cur->content->hdr_offset;
1112 cur->num_hidden = mutt_get_hidden (ctx, cur);
1117 int _mutt_traverse_thread (CONTEXT *ctx, HEADER *cur, int flag)
1119 THREAD *thread, *top;
1120 HEADER *roothdr = NULL;
1121 int final, reverse = (Sort & SORT_REVERSE), minmsgno;
1122 int num_hidden = 0, new = 0, old = 0;
1123 int min_unread_msgno = INT_MAX, min_unread = cur->virtual;
1124 #define CHECK_LIMIT (!ctx->pattern || cur->limited)
1126 if ((Sort & SORT_MASK) != SORT_THREADS && !(flag & M_THREAD_GET_HIDDEN))
1128 mutt_error (_("Threading is not enabled."));
1129 return (cur->virtual);
1132 final = cur->virtual;
1133 thread = cur->thread;
1134 while (thread->parent)
1135 thread = thread->parent;
1137 while (!thread->message)
1138 thread = thread->child;
1139 cur = thread->message;
1140 minmsgno = cur->msgno;
1142 if (!cur->read && CHECK_LIMIT)
1148 if (cur->msgno < min_unread_msgno)
1150 min_unread = cur->virtual;
1151 min_unread_msgno = cur->msgno;
1155 if (cur->virtual == -1 && CHECK_LIMIT)
1158 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1160 cur->pair = 0; /* force index entry's color to be re-evaluated */
1161 cur->collapsed = flag & M_THREAD_COLLAPSE;
1162 if (cur->virtual != -1)
1165 if (flag & M_THREAD_COLLAPSE)
1166 final = roothdr->virtual;
1170 if (thread == top && (thread = thread->child) == NULL)
1172 /* return value depends on action requested */
1173 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1175 else if (flag & M_THREAD_UNREAD)
1176 return ((old && new) ? new : (old ? old : new));
1177 else if (flag & M_THREAD_GET_HIDDEN)
1178 return (num_hidden);
1179 else if (flag & M_THREAD_NEXT_UNREAD)
1180 return (min_unread);
1185 cur = thread->message;
1189 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1191 cur->pair = 0; /* force index entry's color to be re-evaluated */
1192 cur->collapsed = flag & M_THREAD_COLLAPSE;
1193 if (!roothdr && CHECK_LIMIT)
1196 if (flag & M_THREAD_COLLAPSE)
1197 final = roothdr->virtual;
1200 if (reverse && (flag & M_THREAD_COLLAPSE) && (cur->msgno < minmsgno) && CHECK_LIMIT)
1202 minmsgno = cur->msgno;
1203 final = cur->virtual;
1206 if (flag & M_THREAD_COLLAPSE)
1214 cur->virtual = cur->msgno;
1219 if (!cur->read && CHECK_LIMIT)
1225 if (cur->msgno < min_unread_msgno)
1227 min_unread = cur->virtual;
1228 min_unread_msgno = cur->msgno;
1232 if (cur->virtual == -1 && CHECK_LIMIT)
1237 thread = thread->child;
1238 else if (thread->next)
1239 thread = thread->next;
1243 while (!thread->next)
1245 thread = thread->parent;
1254 thread = thread->next;
1258 /* return value depends on action requested */
1259 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1261 else if (flag & M_THREAD_UNREAD)
1262 return ((old && new) ? new : (old ? old : new));
1263 else if (flag & M_THREAD_GET_HIDDEN)
1264 return (num_hidden+1);
1265 else if (flag & M_THREAD_NEXT_UNREAD)
1266 return (min_unread);
1273 /* if flag is 0, we want to know how many messages
1274 * are in the thread. if flag is 1, we want to know
1275 * our position in the thread. */
1276 int mutt_messages_in_thread (CONTEXT *ctx, HEADER *hdr, int flag)
1281 if ((Sort & SORT_MASK) != SORT_THREADS || !hdr->thread)
1284 threads[0] = hdr->thread;
1285 while (threads[0]->parent)
1286 threads[0] = threads[0]->parent;
1288 threads[1] = flag ? hdr->thread : threads[0]->next;
1290 for (i = 0; i < ((flag || !threads[1]) ? 1 : 2); i++)
1292 while (!threads[i]->message)
1293 threads[i] = threads[i]->child;
1296 if (Sort & SORT_REVERSE)
1297 rc = threads[0]->message->msgno - (threads[1] ? threads[1]->message->msgno : -1);
1299 rc = (threads[1] ? threads[1]->message->msgno : ctx->msgcount) - threads[0]->message->msgno;
1308 HASH *mutt_make_id_hash (CONTEXT *ctx)
1314 hash = hash_create (ctx->msgcount * 2);
1316 for (i = 0; i < ctx->msgcount; i++)
1319 if (hdr->env->message_id)
1320 hash_insert (hash, hdr->env->message_id, hdr, 0);
1326 HASH *mutt_make_subj_hash (CONTEXT *ctx)
1332 hash = hash_create (ctx->msgcount * 2);
1334 for (i = 0; i < ctx->msgcount; i++)
1337 if (hdr->env->real_subj)
1338 hash_insert (hash, hdr->env->real_subj, hdr, 1);
1344 static void clean_references (THREAD *brk, THREAD *cur)
1350 for (; cur; cur = cur->next, done = 0)
1352 /* parse subthread recursively */
1353 clean_references (brk, cur->child);
1356 break; /* skip pseudo-message */
1358 /* Looking for the first bad reference according to the new threading.
1359 * Optimal since Mutt stores the references in reverse order, and the
1360 * first loop should match immediatly for mails respecting RFC2822. */
1361 for (p = brk; !done && p; p = p->parent)
1362 for (ref = cur->message->env->references; p->message && ref; ref = ref->next)
1363 if (!mutt_strcasecmp (ref->data, p->message->env->message_id))
1371 HEADER *h = cur->message;
1373 /* clearing the References: header from obsolete Message-Id(s) */
1374 mutt_free_list (&ref->next);
1376 #ifdef IMAP_EDIT_THREADS
1378 mutt_free_list (&h->new_env->references);
1380 h->new_env = mutt_new_envelope ();
1382 h->new_env->references = mutt_copy_list (h->env->references);
1385 h->refs_changed = h->changed = 1;
1390 void mutt_break_thread (HEADER *hdr)
1392 mutt_free_list (&hdr->env->in_reply_to);
1393 mutt_free_list (&hdr->env->references);
1394 hdr->irt_changed = hdr->refs_changed = hdr->changed = 1;
1396 #ifdef IMAP_EDIT_THREADS
1399 mutt_free_list (&hdr->new_env->in_reply_to);
1400 mutt_free_list (&hdr->new_env->references);
1403 hdr->new_env = mutt_new_envelope ();
1406 clean_references (hdr->thread, hdr->thread->child);
1409 static int link_threads (HEADER *parent, HEADER *child, CONTEXT *ctx)
1411 if (child == parent)
1414 mutt_break_thread (child);
1416 child->env->in_reply_to = mutt_new_list ();
1417 child->env->in_reply_to->data = safe_strdup (parent->env->message_id);
1419 #ifdef IMAP_EDIT_THREADS
1420 child->new_env->in_reply_to = mutt_new_list ();
1421 child->new_env->in_reply_to->data = safe_strdup (parent->env->message_id);
1424 mutt_set_flag (ctx, child, M_TAG, 0);
1426 child->irt_changed = child->changed = 1;
1430 int mutt_link_threads (HEADER *cur, HEADER *last, CONTEXT *ctx)
1436 for (i = 0; i < ctx->vcount; i++)
1437 if (ctx->hdrs[Context->v2r[i]]->tagged)
1438 changed |= link_threads (cur, ctx->hdrs[Context->v2r[i]], ctx);
1441 changed = link_threads (cur, last, ctx);