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.
14 #include <lib-lib/mem.h>
26 #define VISIBLE(hdr, ctx) (hdr->virtual >= 0 || (hdr->collapsed && (!ctx->pattern || hdr->limited)))
28 /* determine whether a is a descendant of b */
29 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) {
56 if (hdr && VISIBLE (hdr, ctx)) {
57 if (hdr->subject_changed)
64 /* if there is a parent-to-child subject change anywhere between us and our
65 * closest displayed ancestor, display the subject */
66 for (tmp = tree->parent; tmp; tmp = tmp->parent) {
69 if (VISIBLE (hdr, ctx))
71 else if (hdr->subject_changed)
76 /* if we have no visible parent or previous sibling, display the subject */
80 static void linearize_tree (CONTEXT * ctx)
82 THREAD *tree = ctx->tree;
83 HEADER **array = ctx->hdrs + (Sort & SORT_REVERSE ? ctx->msgcount - 1 : 0);
86 while (!tree->message)
89 *array = tree->message;
90 array += Sort & SORT_REVERSE ? -1 : 1;
107 /* this calculates whether a node is the root of a subtree that has visible
108 * nodes, whether a node itself is visible, whether, if invisible, it has
109 * depth anyway, and whether any of its later siblings are roots of visible
110 * subtrees. while it's at it, it frees the old thread display, so we can
111 * skip parts of the tree in mutt_draw_tree() if we've decided here that we
112 * don't care about them any more.
114 static void calculate_visibility (CONTEXT * ctx, int *max_depth)
116 THREAD *tmp, *tree = ctx->tree;
117 int hide_top_missing = option (OPTHIDETOPMISSING)
118 && !option (OPTHIDEMISSING);
119 int hide_top_limited = option (OPTHIDETOPLIMITED)
120 && !option (OPTHIDELIMITED);
123 /* we walk each level backwards to make it easier to compute next_subtree_visible */
129 if (depth > *max_depth)
132 tree->subtree_visible = 0;
134 p_delete(&tree->message->tree);
135 if (VISIBLE (tree->message, ctx)) {
138 tree->message->display_subject =
139 need_display_subject (ctx, tree->message);
140 for (tmp = tree; tmp; tmp = tmp->parent) {
141 if (tmp->subtree_visible) {
143 tmp->subtree_visible = 2;
147 tmp->subtree_visible = 1;
152 tree->deep = !option (OPTHIDELIMITED);
157 tree->deep = !option (OPTHIDEMISSING);
159 tree->next_subtree_visible = tree->next
160 && (tree->next->next_subtree_visible || tree->next->subtree_visible);
170 while (tree && !tree->prev) {
181 /* now fix up for the OPTHIDETOP* options if necessary */
182 if (hide_top_limited || hide_top_missing) {
185 if (!tree->visible && tree->deep && tree->subtree_visible < 2
186 && ((tree->message && hide_top_limited)
187 || (!tree->message && hide_top_missing)))
189 if (!tree->deep && tree->child && tree->subtree_visible)
194 while (tree && !tree->next)
205 /* Since the graphics characters have a value >255, I have to resort to
206 * using escape sequences to pass the information to print_enriched_string().
207 * These are the macros M_TREE_* defined in mutt.h.
209 * ncurses should automatically use the default ASCII characters instead of
210 * graphics chars on terminals which don't support them (see the man page
213 void mutt_draw_tree (CONTEXT * ctx)
215 char *pfx = NULL, *mypfx = NULL, *arrow = NULL, *myarrow = NULL, *new_tree;
216 char corner = (Sort & SORT_REVERSE) ? M_TREE_ULCORNER : M_TREE_LLCORNER;
217 char vtee = (Sort & SORT_REVERSE) ? M_TREE_BTEE : M_TREE_TTEE;
218 int depth = 0, start_depth = 0, max_depth = 0, width =
219 option (OPTNARROWTREE) ? 1 : 2;
220 THREAD *nextdisp = NULL, *pseudo = NULL, *parent = NULL, *tree = ctx->tree;
222 /* Do the visibility calculations and free the old thread chars.
223 * From now on we can simply ignore invisible subtrees
225 calculate_visibility (ctx, &max_depth);
226 pfx = p_new(char, width * max_depth + 2);
227 arrow = p_new(char, width * max_depth + 2);
230 myarrow = arrow + (depth - start_depth - (start_depth ? 0 : 1)) * width;
231 if (depth && start_depth == depth)
232 myarrow[0] = nextdisp ? M_TREE_LTEE : corner;
233 else if (parent->message && !option (OPTHIDELIMITED))
234 myarrow[0] = M_TREE_HIDDEN;
235 else if (!parent->message && !option (OPTHIDEMISSING))
236 myarrow[0] = M_TREE_MISSING;
240 myarrow[1] = pseudo ? M_TREE_STAR
241 : (tree->duplicate_thread ? M_TREE_EQUALS : M_TREE_HLINE);
243 myarrow[width] = M_TREE_RARROW;
244 myarrow[width + 1] = 0;
245 new_tree = p_new(char, (2 + depth * width));
246 if (start_depth > 1) {
247 strncpy (new_tree, pfx, (start_depth - 1) * width);
248 strfcpy (new_tree + (start_depth - 1) * width,
249 arrow, (1 + depth - start_depth) * width + 2);
252 strfcpy (new_tree, arrow, 2 + depth * width);
253 tree->message->tree = new_tree;
256 if (tree->child && depth) {
257 mypfx = pfx + (depth - 1) * width;
258 mypfx[0] = nextdisp ? M_TREE_VLINE : M_TREE_SPACE;
260 mypfx[1] = M_TREE_SPACE;
266 if (tree->child && tree->subtree_visible) {
273 /* we do this here because we need to make sure that the first child thread
274 * of the old tree that we deal with is actually displayed if any are,
275 * or we might set the parent variable wrong while going through it. */
276 while (!tree->subtree_visible && tree->next)
280 while (!tree->next && tree->parent) {
283 if (tree == nextdisp)
289 if (start_depth == depth)
296 if (tree == nextdisp)
304 if (!pseudo && tree->fake_thread)
306 if (!nextdisp && tree->next_subtree_visible)
316 /* since we may be trying to attach as a pseudo-thread a THREAD that
317 * has no message, we have to make a list of all the subjects of its
318 * most immediate existing descendants. we also note the earliest
319 * date on any of the parents and put it in *dateptr. */
320 static LIST *make_subject_list (THREAD * cur, time_t * dateptr)
325 LIST *curlist, *oldlist, *newlist, *subjects = NULL;
329 while (!cur->message)
333 thisdate = option (OPTTHREADRECEIVED)
334 ? cur->message->received : cur->message->date_sent;
335 if (!*dateptr || thisdate < *dateptr)
339 env = cur->message->env;
340 if (env->real_subj &&
341 ((env->real_subj != env->subject) || (!option (OPTSORTRE)))) {
342 for (curlist = subjects, oldlist = NULL;
343 curlist; oldlist = curlist, curlist = curlist->next) {
344 rc = str_cmp (env->real_subj, curlist->data);
348 if (!curlist || rc > 0) {
349 newlist = p_new(LIST, 1);
350 newlist->data = env->real_subj;
352 newlist->next = oldlist->next;
353 oldlist->next = newlist;
356 newlist->next = subjects;
362 while (!cur->next && cur != start) {
373 /* find the best possible match for a parent mesage based upon subject.
374 * if there are multiple matches, the one which was sent the latest, but
375 * before the current message, is used.
377 static THREAD *find_subject (CONTEXT * ctx, THREAD * cur)
379 struct hash_elem *ptr;
380 THREAD *tmp, *last = NULL;
382 LIST *subjects = NULL, *oldlist;
385 subjects = make_subject_list (cur, &date);
388 hash = hash_string ((unsigned char *) subjects->data,
389 ctx->subj_hash->nelem);
390 for (ptr = ctx->subj_hash->table[hash]; ptr; ptr = ptr->next) {
391 tmp = ((HEADER *) ptr->data)->thread;
392 if (tmp != cur && /* don't match the same message */
393 !tmp->fake_thread && /* don't match pseudo threads */
394 tmp->message->subject_changed && /* only match interesting replies */
395 !is_descendant (tmp, cur) && /* don't match in the same thread */
396 (date >= (option (OPTTHREADRECEIVED) ?
397 tmp->message->received :
398 tmp->message->date_sent)) &&
400 (option (OPTTHREADRECEIVED) ?
401 (last->message->received < tmp->message->received) :
402 (last->message->date_sent < tmp->message->date_sent))) &&
403 tmp->message->env->real_subj &&
404 str_cmp (subjects->data, tmp->message->env->real_subj) == 0)
405 last = tmp; /* best match so far */
409 subjects = subjects->next;
415 /* remove cur and its descendants from their current location.
416 * also make sure ancestors of cur no longer are sorted by the
417 * fact that cur is their descendant. */
418 static void unlink_message (THREAD ** old, THREAD * cur)
423 cur->prev->next = cur->next;
428 cur->next->prev = cur->prev;
431 for (tmp = cur->parent; tmp && tmp->sort_key == cur->sort_key;
433 tmp->sort_key = NULL;
437 /* add cur as a prior sibling of *new, with parent newparent */
438 static void insert_message (THREAD ** new, THREAD * newparent, THREAD * cur)
443 cur->parent = newparent;
449 /* thread by subject things that didn't get threaded by message-id */
450 static void pseudo_threads (CONTEXT * ctx)
452 THREAD *tree = ctx->tree, *top = tree;
453 THREAD *tmp, *cur, *parent, *curchild, *nextchild;
456 ctx->subj_hash = mutt_make_subj_hash (ctx);
461 if ((parent = find_subject (ctx, cur)) != NULL) {
462 cur->fake_thread = 1;
463 unlink_message (&top, cur);
464 insert_message (&parent->child, parent, cur);
465 parent->sort_children = 1;
468 while (!tmp->message)
471 /* if the message we're attaching has pseudo-children, they
472 * need to be attached to its parent, so move them up a level.
473 * but only do this if they have the same real subject as the
474 * parent, since otherwise they rightly belong to the message
475 * we're attaching. */
477 || !str_cmp (tmp->message->env->real_subj,
478 parent->message->env->real_subj)) {
479 tmp->message->subject_changed = 0;
481 for (curchild = tmp->child; curchild;) {
482 nextchild = curchild->next;
483 if (curchild->fake_thread) {
484 unlink_message (&tmp->child, curchild);
485 insert_message (&parent->child, parent, curchild);
487 curchild = nextchild;
491 while (!tmp->next && tmp != cur) {
504 void mutt_clear_threads (CONTEXT * ctx)
508 for (i = 0; i < ctx->msgcount; i++) {
509 ctx->hdrs[i]->thread = NULL;
510 ctx->hdrs[i]->threaded = 0;
514 if (ctx->thread_hash)
515 hash_destroy (&ctx->thread_hash, free);
518 static int compare_threads (const void *a, const void *b)
520 static sort_t *sort_func = NULL;
523 return ((*sort_func) (&(*((THREAD **) a))->sort_key,
524 &(*((THREAD **) b))->sort_key));
525 /* a hack to let us reset sort_func even though we can't
526 * have extra arguments because of qsort
530 sort_func = mutt_get_sort_func (Sort);
531 return (sort_func ? 1 : 0);
535 THREAD *mutt_sort_subthreads (THREAD * thread, int init)
537 THREAD **array, *sort_key, *top, *tmp;
539 int i, array_size, sort_top = 0;
541 /* we put things into the array backwards to save some cycles,
542 * but we want to have to move less stuff around if we're
543 * resorting, so we sort backwards and then put them back
544 * in reverse order so they're forwards
546 Sort ^= SORT_REVERSE;
547 if (!compare_threads (NULL, NULL))
552 array = p_new(THREAD *, (array_size = 256));
554 if (init || !thread->sort_key) {
555 thread->sort_key = NULL;
558 thread->parent->sort_children = 1;
564 thread = thread->child;
568 /* if it has no children, it must be real. sort it on its own merits */
569 thread->sort_key = thread->message;
572 thread = thread->next;
577 while (!thread->next) {
578 /* if it has siblings and needs to be sorted, sort it... */
580 && (thread->parent ? thread->parent->sort_children : sort_top)) {
581 /* put them into the array */
582 for (i = 0; thread; i++, thread = thread->prev) {
584 mem_realloc (&array, (array_size *= 2) * sizeof (THREAD *));
589 qsort ((void *) array, i, sizeof (THREAD *), compare_threads);
591 /* attach them back together. make thread the last sibling. */
594 array[i - 1]->prev = NULL;
597 thread->parent->child = array[i - 1];
602 array[i - 1]->prev = array[i];
603 array[i]->next = array[i - 1];
607 if (thread->parent) {
609 thread = thread->parent;
611 if (!thread->sort_key || thread->sort_children) {
612 /* make sort_key the first or last sibling, as appropriate */
614 (!(Sort & SORT_LAST) ^ !(Sort & SORT_REVERSE)) ? thread->
617 /* we just sorted its children */
618 thread->sort_children = 0;
620 oldsort_key = thread->sort_key;
621 thread->sort_key = thread->message;
623 if (Sort & SORT_LAST) {
624 if (!thread->sort_key || ((((Sort & SORT_REVERSE) ? 1 : -1)
625 * compare_threads ((void *) &thread,
628 thread->sort_key = sort_key->sort_key;
630 else if (!thread->sort_key)
631 thread->sort_key = sort_key->sort_key;
633 /* if its sort_key has changed, we need to resort it and siblings */
634 if (oldsort_key != thread->sort_key) {
636 thread->parent->sort_children = 1;
643 Sort ^= SORT_REVERSE;
649 thread = thread->next;
653 static void check_subjects (CONTEXT * ctx, int init)
659 for (i = 0; i < ctx->msgcount; i++) {
661 if (cur->thread->check_subject)
662 cur->thread->check_subject = 0;
666 /* figure out which messages have subjects different than their parents' */
667 tmp = cur->thread->parent;
668 while (tmp && !tmp->message) {
673 cur->subject_changed = 1;
674 else if (cur->env->real_subj && tmp->message->env->real_subj)
675 cur->subject_changed = str_cmp (cur->env->real_subj,
679 cur->subject_changed = (cur->env->real_subj
680 || tmp->message->env->real_subj) ? 1 : 0;
684 void mutt_sort_threads (CONTEXT * ctx, int init)
687 int i, oldsort, using_refs = 0;
688 THREAD *thread, *new, *tmp, top;
691 /* set Sort to the secondary method to support the set sort_aux=reverse-*
692 * settings. The sorting functions just look at the value of
698 if (!ctx->thread_hash)
702 ctx->thread_hash = hash_create (ctx->msgcount * 2);
704 /* we want a quick way to see if things are actually attached to the top of the
705 * thread tree or if they're just dangling, so we attach everything to a top
706 * node temporarily */
707 top.parent = top.next = top.prev = NULL;
708 top.child = ctx->tree;
709 for (thread = ctx->tree; thread; thread = thread->next)
710 thread->parent = ⊤
712 /* put each new message together with the matching messageless THREAD if it
713 * exists. otherwise, if there is a THREAD that already has a message, thread
714 * new message as an identical child. if we didn't attach the message to a
715 * THREAD, make a new one for it. */
716 for (i = 0; i < ctx->msgcount; i++) {
720 if ((!init || option (OPTDUPTHREADS)) && cur->env->message_id)
721 thread = hash_find (ctx->thread_hash, cur->env->message_id);
725 if (thread && !thread->message) {
726 /* this is a message which was missing before */
727 thread->message = cur;
728 cur->thread = thread;
729 thread->check_subject = 1;
731 /* mark descendants as needing subject_changed checked */
732 for (tmp = (thread->child ? thread->child : thread); tmp != thread;) {
733 while (!tmp->message)
735 tmp->check_subject = 1;
736 while (!tmp->next && tmp != thread)
742 if (thread->parent) {
743 /* remove threading info above it based on its children, which we'll
744 * recalculate based on its headers. make sure not to leave
745 * dangling missing messages. note that we haven't kept track
746 * of what info came from its children and what from its siblings'
747 * children, so we just remove the stuff that's definitely from it */
749 tmp = thread->parent;
750 unlink_message (&tmp->child, thread);
751 thread->parent = NULL;
752 thread->sort_key = NULL;
753 thread->fake_thread = 0;
755 } while (thread != &top && !thread->child && !thread->message);
759 new = (option (OPTDUPTHREADS) ? thread : NULL);
761 thread = p_new(THREAD, 1);
762 thread->message = cur;
763 thread->check_subject = 1;
764 cur->thread = thread;
765 hash_insert (ctx->thread_hash,
766 cur->env->message_id ? cur->env->message_id : "",
770 if (new->duplicate_thread)
773 thread = cur->thread;
775 insert_message (&new->child, new, thread);
776 thread->duplicate_thread = 1;
777 thread->message->threaded = 1;
782 /* unlink pseudo-threads because they might be children of newly
783 * arrived messages */
784 thread = cur->thread;
785 for (new = thread->child; new;) {
787 if (new->fake_thread) {
788 unlink_message (&thread->child, new);
789 insert_message (&top.child, &top, new);
790 new->fake_thread = 0;
797 /* thread by references */
798 for (i = 0; i < ctx->msgcount; i++) {
804 thread = cur->thread;
808 if (using_refs == 0) {
809 /* look at the beginning of in-reply-to: */
810 if ((ref = cur->env->in_reply_to) != NULL)
813 ref = cur->env->references;
817 else if (using_refs == 1) {
818 /* if there's no references header, use all the in-reply-to:
819 * data that we have. otherwise, use the first reference
820 * if it's different than the first in-reply-to, otherwise use
821 * the second reference (since at least eudora puts the most
822 * recent reference in in-reply-to and the rest in references)
824 if (!cur->env->references)
827 if (str_cmp (ref->data, cur->env->references->data))
828 ref = cur->env->references;
830 ref = cur->env->references->next;
836 ref = ref->next; /* go on with references */
841 if ((new = hash_find (ctx->thread_hash, ref->data)) == NULL) {
842 new = p_new(THREAD, 1);
843 hash_insert (ctx->thread_hash, ref->data, new, 1);
846 if (new->duplicate_thread)
848 if (is_descendant (new, thread)) /* no loops! */
853 unlink_message (&top.child, thread);
854 insert_message (&new->child, new, thread);
856 if (thread->message || (thread->parent && thread->parent != &top))
861 insert_message (&top.child, &top, thread);
864 /* detach everything from the temporary top node */
865 for (thread = top.child; thread; thread = thread->next) {
866 thread->parent = NULL;
868 ctx->tree = top.child;
870 check_subjects (ctx, init);
872 if (!option (OPTSTRICTTHREADS))
873 pseudo_threads (ctx);
876 ctx->tree = mutt_sort_subthreads (ctx->tree, init);
878 /* restore the oldsort order. */
881 /* Put the list into an array. */
882 linearize_tree (ctx);
884 /* Draw the thread tree. */
885 mutt_draw_tree (ctx);
889 static HEADER *find_virtual (THREAD * cur, int reverse)
893 if (cur->message && cur->message->virtual >= 0)
894 return (cur->message);
897 if ((cur = cur->child) == NULL)
900 while (reverse && cur->next)
904 if (cur->message && cur->message->virtual >= 0)
905 return (cur->message);
910 while (reverse && cur->next)
913 else if (reverse ? cur->prev : cur->next)
914 cur = reverse ? cur->prev : cur->next;
916 while (!(reverse ? cur->prev : cur->next)) {
921 cur = reverse ? cur->prev : cur->next;
927 int _mutt_aside_thread (HEADER * hdr, short dir, short subthreads)
932 if ((Sort & SORT_MASK) != SORT_THREADS) {
933 mutt_error _("Threading is not enabled.");
935 return (hdr->virtual);
945 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0)) {
946 while (!cur->next && cur->parent)
950 while (!cur->prev && cur->parent)
955 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0)) {
960 tmp = find_virtual (cur, 0);
968 tmp = find_virtual (cur, 1);
972 return (tmp->virtual);
975 int mutt_parent_message (CONTEXT * ctx, HEADER * hdr)
979 if ((Sort & SORT_MASK) != SORT_THREADS) {
980 mutt_error _("Threading is not enabled.");
982 return (hdr->virtual);
985 for (thread = hdr->thread->parent; thread; thread = thread->parent) {
986 if ((hdr = thread->message) != NULL) {
987 if (VISIBLE (hdr, ctx))
988 return (hdr->virtual);
990 mutt_error _("Parent message is not visible in this limited view.");
997 mutt_error _("Parent message is not available.");
1002 void mutt_set_virtual (CONTEXT * ctx)
1010 for (i = 0; i < ctx->msgcount; i++) {
1012 if (cur->virtual >= 0) {
1013 cur->virtual = ctx->vcount;
1014 ctx->v2r[ctx->vcount] = i;
1017 cur->content->length + cur->content->offset -
1018 cur->content->hdr_offset;
1019 cur->num_hidden = mutt_get_hidden (ctx, cur);
1024 int _mutt_traverse_thread (CONTEXT * ctx, HEADER * cur, int flag)
1026 THREAD *thread, *top;
1027 HEADER *roothdr = NULL;
1028 int final, reverse = (Sort & SORT_REVERSE), minmsgno;
1029 int num_hidden = 0, new = 0, old = 0;
1030 int min_unread_msgno = INT_MAX, min_unread = cur->virtual;
1032 #define CHECK_LIMIT (!ctx->pattern || cur->limited)
1034 if ((Sort & SORT_MASK) != SORT_THREADS && !(flag & M_THREAD_GET_HIDDEN)) {
1035 mutt_error (_("Threading is not enabled."));
1036 return (cur->virtual);
1039 final = cur->virtual;
1040 thread = cur->thread;
1041 while (thread->parent)
1042 thread = thread->parent;
1044 while (!thread->message)
1045 thread = thread->child;
1046 cur = thread->message;
1047 minmsgno = cur->msgno;
1049 if (!cur->read && CHECK_LIMIT) {
1054 if (cur->msgno < min_unread_msgno) {
1055 min_unread = cur->virtual;
1056 min_unread_msgno = cur->msgno;
1060 if (cur->virtual == -1 && CHECK_LIMIT)
1063 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE)) {
1064 cur->pair = 0; /* force index entry's color to be re-evaluated */
1065 cur->collapsed = flag & M_THREAD_COLLAPSE;
1066 if (cur->virtual != -1) {
1068 if (flag & M_THREAD_COLLAPSE)
1069 final = roothdr->virtual;
1073 if (thread == top && (thread = thread->child) == NULL) {
1074 /* return value depends on action requested */
1075 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1077 else if (flag & M_THREAD_UNREAD)
1078 return ((old && new) ? new : (old ? old : new));
1079 else if (flag & M_THREAD_GET_HIDDEN)
1080 return (num_hidden);
1081 else if (flag & M_THREAD_NEXT_UNREAD)
1082 return (min_unread);
1086 cur = thread->message;
1089 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE)) {
1090 cur->pair = 0; /* force index entry's color to be re-evaluated */
1091 cur->collapsed = flag & M_THREAD_COLLAPSE;
1092 if (!roothdr && CHECK_LIMIT) {
1094 if (flag & M_THREAD_COLLAPSE)
1095 final = roothdr->virtual;
1098 if (reverse && (flag & M_THREAD_COLLAPSE) && (cur->msgno < minmsgno)
1100 minmsgno = cur->msgno;
1101 final = cur->virtual;
1104 if (flag & M_THREAD_COLLAPSE) {
1110 cur->virtual = cur->msgno;
1115 if (!cur->read && CHECK_LIMIT) {
1120 if (cur->msgno < min_unread_msgno) {
1121 min_unread = cur->virtual;
1122 min_unread_msgno = cur->msgno;
1126 if (cur->virtual == -1 && CHECK_LIMIT)
1131 thread = thread->child;
1132 else if (thread->next)
1133 thread = thread->next;
1137 while (!thread->next) {
1138 thread = thread->parent;
1139 if (thread == top) {
1146 thread = thread->next;
1150 /* return value depends on action requested */
1151 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1153 else if (flag & M_THREAD_UNREAD)
1154 return ((old && new) ? new : (old ? old : new));
1155 else if (flag & M_THREAD_GET_HIDDEN)
1156 return (num_hidden + 1);
1157 else if (flag & M_THREAD_NEXT_UNREAD)
1158 return (min_unread);
1165 /* if flag is 0, we want to know how many messages
1166 * are in the thread. if flag is 1, we want to know
1167 * our position in the thread. */
1168 int mutt_messages_in_thread (CONTEXT * ctx, HEADER * hdr, int flag)
1173 if ((Sort & SORT_MASK) != SORT_THREADS || !hdr->thread)
1176 threads[0] = hdr->thread;
1177 while (threads[0]->parent)
1178 threads[0] = threads[0]->parent;
1180 threads[1] = flag ? hdr->thread : threads[0]->next;
1182 for (i = 0; i < ((flag || !threads[1]) ? 1 : 2); i++) {
1183 while (!threads[i]->message)
1184 threads[i] = threads[i]->child;
1187 if (Sort & SORT_REVERSE)
1189 threads[0]->message->msgno -
1190 (threads[1] ? threads[1]->message->msgno : -1);
1193 (threads[1] ? threads[1]->message->msgno : ctx->msgcount) -
1194 threads[0]->message->msgno;
1203 HASH *mutt_make_id_hash (CONTEXT * ctx)
1209 hash = hash_create (ctx->msgcount * 2);
1211 for (i = 0; i < ctx->msgcount; i++) {
1213 if (hdr->env->message_id)
1214 hash_insert (hash, hdr->env->message_id, hdr, 0);
1220 HASH *mutt_make_subj_hash (CONTEXT * ctx)
1226 hash = hash_create (ctx->msgcount * 2);
1228 for (i = 0; i < ctx->msgcount; i++) {
1230 if (hdr->env->real_subj)
1231 hash_insert (hash, hdr->env->real_subj, hdr, 1);
1237 static void clean_references (THREAD * brk, THREAD * cur)
1243 for (; cur; cur = cur->next, done = 0) {
1244 /* parse subthread recursively */
1245 clean_references (brk, cur->child);
1248 break; /* skip pseudo-message */
1250 /* Looking for the first bad reference according to the new threading.
1251 * Optimal since Mutt stores the references in reverse order, and the
1252 * first loop should match immediatly for mails respecting RFC2822. */
1253 for (p = brk; !done && p; p = p->parent)
1254 for (ref = cur->message->env->references; p->message && ref;
1256 if (!str_casecmp (ref->data, p->message->env->message_id)) {
1262 HEADER *h = cur->message;
1264 /* clearing the References: header from obsolete Message-ID(s) */
1265 mutt_free_list (&ref->next);
1267 h->env->refs_changed = h->changed = 1;
1272 void mutt_break_thread (HEADER * hdr)
1274 mutt_free_list (&hdr->env->in_reply_to);
1275 mutt_free_list (&hdr->env->references);
1276 hdr->env->irt_changed = hdr->env->refs_changed = hdr->changed = 1;
1277 clean_references (hdr->thread, hdr->thread->child);
1280 static int link_threads (HEADER * parent, HEADER * child, CONTEXT * ctx)
1282 if (child == parent)
1285 mutt_break_thread (child);
1287 child->env->in_reply_to = mutt_new_list ();
1288 child->env->in_reply_to->data = str_dup (parent->env->message_id);
1290 mutt_set_flag (ctx, child, M_TAG, 0);
1292 child->env->irt_changed = child->changed = 1;
1296 int mutt_link_threads (HEADER * cur, HEADER * last, CONTEXT * ctx)
1301 for (i = 0; i < ctx->vcount; i++)
1302 if (ctx->hdrs[Context->v2r[i]]->tagged)
1303 changed |= link_threads (cur, ctx->hdrs[Context->v2r[i]], ctx);
1306 changed = link_threads (cur, last, ctx);
1311 void mutt_adjust_subject (ENVELOPE* e) {
1312 regmatch_t pmatch[1];
1314 if (e && e->subject) {
1315 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1316 e->real_subj = e->subject + pmatch[0].rm_eo;
1318 e->real_subj = e->subject;
1322 void mutt_adjust_all_subjects (void) {
1325 if (!Context || !Context->msgcount)
1328 for (i = 0; i < Context->msgcount; i++)
1329 mutt_adjust_subject (Context->hdrs[i]->env);