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>
15 #include <lib-lib/macros.h>
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)
38 /* Determines whether to display a message's subject. */
39 static int need_display_subject (CONTEXT * ctx, HEADER * hdr)
41 THREAD *tmp, *tree = hdr->thread;
43 /* if the user disabled subject hiding, display it */
44 if (!option (OPTHIDETHREADSUBJECT))
47 /* if our subject is different from our parent's, display it */
48 if (hdr->subject_changed)
51 /* if our subject is different from that of our closest previously displayed
52 * sibling, display the subject */
53 for (tmp = tree->prev; tmp; tmp = tmp->prev) {
55 if (hdr && VISIBLE (hdr, ctx)) {
56 if (hdr->subject_changed)
63 /* if there is a parent-to-child subject change anywhere between us and our
64 * closest displayed ancestor, display the subject */
65 for (tmp = tree->parent; tmp; tmp = tmp->parent) {
68 if (VISIBLE (hdr, ctx))
70 else if (hdr->subject_changed)
75 /* if we have no visible parent or previous sibling, display the subject */
79 static void linearize_tree (CONTEXT * ctx)
81 THREAD *tree = ctx->tree;
82 HEADER **array = ctx->hdrs + (Sort & SORT_REVERSE ? ctx->msgcount - 1 : 0);
85 while (!tree->message)
88 *array = tree->message;
89 array += Sort & SORT_REVERSE ? -1 : 1;
106 /* this calculates whether a node is the root of a subtree that has visible
107 * nodes, whether a node itself is visible, whether, if invisible, it has
108 * depth anyway, and whether any of its later siblings are roots of visible
109 * subtrees. while it's at it, it frees the old thread display, so we can
110 * skip parts of the tree in mutt_draw_tree() if we've decided here that we
111 * don't care about them any more.
113 static void calculate_visibility (CONTEXT * ctx, int *max_depth)
115 THREAD *tmp, *tree = ctx->tree;
116 int hide_top_missing = option (OPTHIDETOPMISSING)
117 && !option (OPTHIDEMISSING);
118 int hide_top_limited = option (OPTHIDETOPLIMITED)
119 && !option (OPTHIDELIMITED);
122 /* we walk each level backwards to make it easier to compute next_subtree_visible */
128 if (depth > *max_depth)
131 tree->subtree_visible = 0;
133 p_delete(&tree->message->tree);
134 if (VISIBLE (tree->message, ctx)) {
137 tree->message->display_subject =
138 need_display_subject (ctx, tree->message);
139 for (tmp = tree; tmp; tmp = tmp->parent) {
140 if (tmp->subtree_visible) {
142 tmp->subtree_visible = 2;
146 tmp->subtree_visible = 1;
151 tree->deep = !option (OPTHIDELIMITED);
156 tree->deep = !option (OPTHIDEMISSING);
158 tree->next_subtree_visible = tree->next
159 && (tree->next->next_subtree_visible || tree->next->subtree_visible);
169 while (tree && !tree->prev) {
180 /* now fix up for the OPTHIDETOP* options if necessary */
181 if (hide_top_limited || hide_top_missing) {
184 if (!tree->visible && tree->deep && tree->subtree_visible < 2
185 && ((tree->message && hide_top_limited)
186 || (!tree->message && hide_top_missing)))
188 if (!tree->deep && tree->child && tree->subtree_visible)
193 while (tree && !tree->next)
204 /* Since the graphics characters have a value >255, I have to resort to
205 * using escape sequences to pass the information to print_enriched_string().
206 * These are the macros M_TREE_* defined in mutt.h.
208 * ncurses should automatically use the default ASCII characters instead of
209 * graphics chars on terminals which don't support them (see the man page
212 void mutt_draw_tree (CONTEXT * ctx)
214 char *pfx = NULL, *mypfx = NULL, *arrow = NULL, *myarrow = NULL, *new_tree;
215 char corner = (Sort & SORT_REVERSE) ? M_TREE_ULCORNER : M_TREE_LLCORNER;
216 char vtee = (Sort & SORT_REVERSE) ? M_TREE_BTEE : M_TREE_TTEE;
217 int depth = 0, start_depth = 0, max_depth = 0, width =
218 option (OPTNARROWTREE) ? 1 : 2;
219 THREAD *nextdisp = NULL, *pseudo = NULL, *parent = NULL, *tree = ctx->tree;
221 /* Do the visibility calculations and free the old thread chars.
222 * From now on we can simply ignore invisible subtrees
224 calculate_visibility (ctx, &max_depth);
225 pfx = p_new(char, width * max_depth + 2);
226 arrow = p_new(char, width * max_depth + 2);
229 myarrow = arrow + (depth - start_depth - (start_depth ? 0 : 1)) * width;
230 if (depth && start_depth == depth)
231 myarrow[0] = nextdisp ? M_TREE_LTEE : corner;
232 else if (parent->message && !option (OPTHIDELIMITED))
233 myarrow[0] = M_TREE_HIDDEN;
234 else if (!parent->message && !option (OPTHIDEMISSING))
235 myarrow[0] = M_TREE_MISSING;
239 myarrow[1] = pseudo ? M_TREE_STAR
240 : (tree->duplicate_thread ? M_TREE_EQUALS : M_TREE_HLINE);
242 myarrow[width] = M_TREE_RARROW;
243 myarrow[width + 1] = 0;
244 new_tree = p_new(char, (2 + depth * width));
245 if (start_depth > 1) {
246 memcpy(new_tree, pfx, (start_depth - 1) * width);
247 m_strcpy(new_tree + (start_depth - 1) * width,
248 (1 + depth - start_depth) * width + 2, arrow);
251 m_strcpy(new_tree, 2 + depth * width, arrow);
252 tree->message->tree = new_tree;
255 if (tree->child && depth) {
256 mypfx = pfx + (depth - 1) * width;
257 mypfx[0] = nextdisp ? M_TREE_VLINE : M_TREE_SPACE;
259 mypfx[1] = M_TREE_SPACE;
265 if (tree->child && tree->subtree_visible) {
272 /* we do this here because we need to make sure that the first child thread
273 * of the old tree that we deal with is actually displayed if any are,
274 * or we might set the parent variable wrong while going through it. */
275 while (!tree->subtree_visible && tree->next)
279 while (!tree->next && tree->parent) {
282 if (tree == nextdisp)
288 if (start_depth == depth)
295 if (tree == nextdisp)
303 if (!pseudo && tree->fake_thread)
305 if (!nextdisp && tree->next_subtree_visible)
315 /* since we may be trying to attach as a pseudo-thread a THREAD that
316 * has no message, we have to make a list of all the subjects of its
317 * most immediate existing descendants. we also note the earliest
318 * date on any of the parents and put it in *dateptr. */
319 static LIST *make_subject_list (THREAD * cur, time_t * dateptr)
324 LIST *curlist, *oldlist, *newlist, *subjects = NULL;
328 while (!cur->message)
332 thisdate = option (OPTTHREADRECEIVED)
333 ? cur->message->received : cur->message->date_sent;
334 if (!*dateptr || thisdate < *dateptr)
338 env = cur->message->env;
339 if (env->real_subj &&
340 ((env->real_subj != env->subject) || (!option (OPTSORTRE)))) {
341 for (curlist = subjects, oldlist = NULL;
342 curlist; oldlist = curlist, curlist = curlist->next) {
343 rc = m_strcmp(env->real_subj, curlist->data);
347 if (!curlist || rc > 0) {
348 newlist = p_new(LIST, 1);
349 newlist->data = env->real_subj;
351 newlist->next = oldlist->next;
352 oldlist->next = newlist;
355 newlist->next = subjects;
361 while (!cur->next && cur != start) {
372 /* find the best possible match for a parent mesage based upon subject.
373 * if there are multiple matches, the one which was sent the latest, but
374 * before the current message, is used.
376 static THREAD *find_subject (CONTEXT * ctx, THREAD * cur)
378 struct hash_elem *ptr;
379 THREAD *tmp, *last = NULL;
381 LIST *subjects = NULL, *oldlist;
384 subjects = make_subject_list (cur, &date);
387 hash = hash_string ((unsigned char *) subjects->data,
388 ctx->subj_hash->nelem);
389 for (ptr = ctx->subj_hash->table[hash]; ptr; ptr = ptr->next) {
390 tmp = ((HEADER *) ptr->data)->thread;
391 if (tmp != cur && /* don't match the same message */
392 !tmp->fake_thread && /* don't match pseudo threads */
393 tmp->message->subject_changed && /* only match interesting replies */
394 !is_descendant (tmp, cur) && /* don't match in the same thread */
395 (date >= (option (OPTTHREADRECEIVED) ?
396 tmp->message->received :
397 tmp->message->date_sent)) &&
399 (option (OPTTHREADRECEIVED) ?
400 (last->message->received < tmp->message->received) :
401 (last->message->date_sent < tmp->message->date_sent))) &&
402 tmp->message->env->real_subj &&
403 m_strcmp(subjects->data, tmp->message->env->real_subj) == 0)
404 last = tmp; /* best match so far */
408 subjects = subjects->next;
414 /* remove cur and its descendants from their current location.
415 * also make sure ancestors of cur no longer are sorted by the
416 * fact that cur is their descendant. */
417 static void unlink_message (THREAD ** old, THREAD * cur)
422 cur->prev->next = cur->next;
427 cur->next->prev = cur->prev;
430 for (tmp = cur->parent; tmp && tmp->sort_key == cur->sort_key;
432 tmp->sort_key = NULL;
436 /* add cur as a prior sibling of *new, with parent newparent */
437 static void insert_message (THREAD ** new, THREAD * newparent, THREAD * cur)
442 cur->parent = newparent;
448 /* thread by subject things that didn't get threaded by message-id */
449 static void pseudo_threads (CONTEXT * ctx)
451 THREAD *tree = ctx->tree, *top = tree;
452 THREAD *tmp, *cur, *parent, *curchild, *nextchild;
455 ctx->subj_hash = mutt_make_subj_hash (ctx);
460 if ((parent = find_subject (ctx, cur)) != NULL) {
461 cur->fake_thread = 1;
462 unlink_message (&top, cur);
463 insert_message (&parent->child, parent, cur);
464 parent->sort_children = 1;
467 while (!tmp->message)
470 /* if the message we're attaching has pseudo-children, they
471 * need to be attached to its parent, so move them up a level.
472 * but only do this if they have the same real subject as the
473 * parent, since otherwise they rightly belong to the message
474 * we're attaching. */
476 || !m_strcmp(tmp->message->env->real_subj,
477 parent->message->env->real_subj)) {
478 tmp->message->subject_changed = 0;
480 for (curchild = tmp->child; curchild;) {
481 nextchild = curchild->next;
482 if (curchild->fake_thread) {
483 unlink_message (&tmp->child, curchild);
484 insert_message (&parent->child, parent, curchild);
486 curchild = nextchild;
490 while (!tmp->next && tmp != cur) {
503 void mutt_clear_threads (CONTEXT * ctx)
507 for (i = 0; i < ctx->msgcount; i++) {
508 ctx->hdrs[i]->thread = NULL;
509 ctx->hdrs[i]->threaded = 0;
513 if (ctx->thread_hash)
514 hash_destroy (&ctx->thread_hash, free);
517 static int compare_threads (const void *a, const void *b)
519 static sort_t *sort_func = NULL;
522 return ((*sort_func) (&(*((THREAD **) a))->sort_key,
523 &(*((THREAD **) b))->sort_key));
524 /* a hack to let us reset sort_func even though we can't
525 * have extra arguments because of qsort
529 sort_func = mutt_get_sort_func (Sort);
530 return (sort_func ? 1 : 0);
534 THREAD *mutt_sort_subthreads (THREAD * thread, int init)
536 THREAD **array, *sort_key, *top, *tmp;
538 int i, array_size, sort_top = 0;
540 /* we put things into the array backwards to save some cycles,
541 * but we want to have to move less stuff around if we're
542 * resorting, so we sort backwards and then put them back
543 * in reverse order so they're forwards
545 Sort ^= SORT_REVERSE;
546 if (!compare_threads (NULL, NULL))
551 array = p_new(THREAD *, (array_size = 256));
553 if (init || !thread->sort_key) {
554 thread->sort_key = NULL;
557 thread->parent->sort_children = 1;
563 thread = thread->child;
567 /* if it has no children, it must be real. sort it on its own merits */
568 thread->sort_key = thread->message;
571 thread = thread->next;
576 while (!thread->next) {
577 /* if it has siblings and needs to be sorted, sort it... */
579 && (thread->parent ? thread->parent->sort_children : sort_top)) {
580 /* put them into the array */
581 for (i = 0; thread; i++, thread = thread->prev) {
583 p_realloc(&array, array_size *= 2);
588 qsort ((void *) array, i, sizeof (THREAD *), compare_threads);
590 /* attach them back together. make thread the last sibling. */
593 array[i - 1]->prev = NULL;
596 thread->parent->child = array[i - 1];
601 array[i - 1]->prev = array[i];
602 array[i]->next = array[i - 1];
606 if (thread->parent) {
608 thread = thread->parent;
610 if (!thread->sort_key || thread->sort_children) {
611 /* make sort_key the first or last sibling, as appropriate */
613 (!(Sort & SORT_LAST) ^ !(Sort & SORT_REVERSE)) ? thread->
616 /* we just sorted its children */
617 thread->sort_children = 0;
619 oldsort_key = thread->sort_key;
620 thread->sort_key = thread->message;
622 if (Sort & SORT_LAST) {
623 if (!thread->sort_key || ((((Sort & SORT_REVERSE) ? 1 : -1)
624 * compare_threads ((void *) &thread,
627 thread->sort_key = sort_key->sort_key;
629 else if (!thread->sort_key)
630 thread->sort_key = sort_key->sort_key;
632 /* if its sort_key has changed, we need to resort it and siblings */
633 if (oldsort_key != thread->sort_key) {
635 thread->parent->sort_children = 1;
642 Sort ^= SORT_REVERSE;
648 thread = thread->next;
652 static void check_subjects (CONTEXT * ctx, int init)
658 for (i = 0; i < ctx->msgcount; i++) {
660 if (cur->thread->check_subject)
661 cur->thread->check_subject = 0;
665 /* figure out which messages have subjects different than their parents' */
666 tmp = cur->thread->parent;
667 while (tmp && !tmp->message) {
672 cur->subject_changed = 1;
673 else if (cur->env->real_subj && tmp->message->env->real_subj)
674 cur->subject_changed = m_strcmp(cur->env->real_subj,
678 cur->subject_changed = (cur->env->real_subj
679 || tmp->message->env->real_subj) ? 1 : 0;
683 void mutt_sort_threads (CONTEXT * ctx, int init)
686 int i, oldsort, using_refs = 0;
687 THREAD *thread, *new, *tmp, top;
690 /* set Sort to the secondary method to support the set sort_aux=reverse-*
691 * settings. The sorting functions just look at the value of
697 if (!ctx->thread_hash)
701 ctx->thread_hash = hash_create (ctx->msgcount * 2);
703 /* we want a quick way to see if things are actually attached to the top of the
704 * thread tree or if they're just dangling, so we attach everything to a top
705 * node temporarily */
706 top.parent = top.next = top.prev = NULL;
707 top.child = ctx->tree;
708 for (thread = ctx->tree; thread; thread = thread->next)
709 thread->parent = ⊤
711 /* put each new message together with the matching messageless THREAD if it
712 * exists. otherwise, if there is a THREAD that already has a message, thread
713 * new message as an identical child. if we didn't attach the message to a
714 * THREAD, make a new one for it. */
715 for (i = 0; i < ctx->msgcount; i++) {
719 if ((!init || option (OPTDUPTHREADS)) && cur->env->message_id)
720 thread = hash_find (ctx->thread_hash, cur->env->message_id);
724 if (thread && !thread->message) {
725 /* this is a message which was missing before */
726 thread->message = cur;
727 cur->thread = thread;
728 thread->check_subject = 1;
730 /* mark descendants as needing subject_changed checked */
731 for (tmp = (thread->child ? thread->child : thread); tmp != thread;) {
732 while (!tmp->message)
734 tmp->check_subject = 1;
735 while (!tmp->next && tmp != thread)
741 if (thread->parent) {
742 /* remove threading info above it based on its children, which we'll
743 * recalculate based on its headers. make sure not to leave
744 * dangling missing messages. note that we haven't kept track
745 * of what info came from its children and what from its siblings'
746 * children, so we just remove the stuff that's definitely from it */
748 tmp = thread->parent;
749 unlink_message (&tmp->child, thread);
750 thread->parent = NULL;
751 thread->sort_key = NULL;
752 thread->fake_thread = 0;
754 } while (thread != &top && !thread->child && !thread->message);
758 new = (option (OPTDUPTHREADS) ? thread : NULL);
760 thread = p_new(THREAD, 1);
761 thread->message = cur;
762 thread->check_subject = 1;
763 cur->thread = thread;
764 hash_insert (ctx->thread_hash,
765 cur->env->message_id ? cur->env->message_id : "",
769 if (new->duplicate_thread)
772 thread = cur->thread;
774 insert_message (&new->child, new, thread);
775 thread->duplicate_thread = 1;
776 thread->message->threaded = 1;
781 /* unlink pseudo-threads because they might be children of newly
782 * arrived messages */
783 thread = cur->thread;
784 for (new = thread->child; new;) {
786 if (new->fake_thread) {
787 unlink_message (&thread->child, new);
788 insert_message (&top.child, &top, new);
789 new->fake_thread = 0;
796 /* thread by references */
797 for (i = 0; i < ctx->msgcount; i++) {
803 thread = cur->thread;
807 if (using_refs == 0) {
808 /* look at the beginning of in-reply-to: */
809 if ((ref = cur->env->in_reply_to) != NULL)
812 ref = cur->env->references;
816 else if (using_refs == 1) {
817 /* if there's no references header, use all the in-reply-to:
818 * data that we have. otherwise, use the first reference
819 * if it's different than the first in-reply-to, otherwise use
820 * the second reference (since at least eudora puts the most
821 * recent reference in in-reply-to and the rest in references)
823 if (!cur->env->references)
826 if (m_strcmp(ref->data, cur->env->references->data))
827 ref = cur->env->references;
829 ref = cur->env->references->next;
835 ref = ref->next; /* go on with references */
840 if ((new = hash_find (ctx->thread_hash, ref->data)) == NULL) {
841 new = p_new(THREAD, 1);
842 hash_insert (ctx->thread_hash, ref->data, new, 1);
845 if (new->duplicate_thread)
847 if (is_descendant (new, thread)) /* no loops! */
852 unlink_message (&top.child, thread);
853 insert_message (&new->child, new, thread);
855 if (thread->message || (thread->parent && thread->parent != &top))
860 insert_message (&top.child, &top, thread);
863 /* detach everything from the temporary top node */
864 for (thread = top.child; thread; thread = thread->next) {
865 thread->parent = NULL;
867 ctx->tree = top.child;
869 check_subjects (ctx, init);
871 if (!option (OPTSTRICTTHREADS))
872 pseudo_threads (ctx);
875 ctx->tree = mutt_sort_subthreads (ctx->tree, init);
877 /* restore the oldsort order. */
880 /* Put the list into an array. */
881 linearize_tree (ctx);
883 /* Draw the thread tree. */
884 mutt_draw_tree (ctx);
888 static HEADER *find_virtual (THREAD * cur, int reverse)
892 if (cur->message && cur->message->virtual >= 0)
893 return (cur->message);
896 if ((cur = cur->child) == NULL)
899 while (reverse && cur->next)
903 if (cur->message && cur->message->virtual >= 0)
904 return (cur->message);
909 while (reverse && cur->next)
912 else if (reverse ? cur->prev : cur->next)
913 cur = reverse ? cur->prev : cur->next;
915 while (!(reverse ? cur->prev : cur->next)) {
920 cur = reverse ? cur->prev : cur->next;
926 int _mutt_aside_thread (HEADER * hdr, short dir, short subthreads)
931 if ((Sort & SORT_MASK) != SORT_THREADS) {
932 mutt_error _("Threading is not enabled.");
934 return (hdr->virtual);
944 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0)) {
945 while (!cur->next && cur->parent)
949 while (!cur->prev && cur->parent)
954 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0)) {
959 tmp = find_virtual (cur, 0);
967 tmp = find_virtual (cur, 1);
971 return (tmp->virtual);
974 int mutt_parent_message (CONTEXT * ctx, HEADER * hdr)
978 if ((Sort & SORT_MASK) != SORT_THREADS) {
979 mutt_error _("Threading is not enabled.");
981 return (hdr->virtual);
984 for (thread = hdr->thread->parent; thread; thread = thread->parent) {
985 if ((hdr = thread->message) != NULL) {
986 if (VISIBLE (hdr, ctx))
987 return (hdr->virtual);
989 mutt_error _("Parent message is not visible in this limited view.");
996 mutt_error _("Parent message is not available.");
1001 void mutt_set_virtual (CONTEXT * ctx)
1009 for (i = 0; i < ctx->msgcount; i++) {
1011 if (cur->virtual >= 0) {
1012 cur->virtual = ctx->vcount;
1013 ctx->v2r[ctx->vcount] = i;
1016 cur->content->length + cur->content->offset -
1017 cur->content->hdr_offset;
1018 cur->num_hidden = mutt_get_hidden (ctx, cur);
1023 int _mutt_traverse_thread (CONTEXT * ctx, HEADER * cur, int flag)
1025 THREAD *thread, *top;
1026 HEADER *roothdr = NULL;
1027 int final, reverse = (Sort & SORT_REVERSE), minmsgno;
1028 int num_hidden = 0, new = 0, old = 0;
1029 int min_unread_msgno = INT_MAX, min_unread = cur->virtual;
1031 #define CHECK_LIMIT (!ctx->pattern || cur->limited)
1033 if ((Sort & SORT_MASK) != SORT_THREADS && !(flag & M_THREAD_GET_HIDDEN)) {
1034 mutt_error (_("Threading is not enabled."));
1035 return (cur->virtual);
1038 final = cur->virtual;
1039 thread = cur->thread;
1040 while (thread->parent)
1041 thread = thread->parent;
1043 while (!thread->message)
1044 thread = thread->child;
1045 cur = thread->message;
1046 minmsgno = cur->msgno;
1048 if (!cur->read && CHECK_LIMIT) {
1053 if (cur->msgno < min_unread_msgno) {
1054 min_unread = cur->virtual;
1055 min_unread_msgno = cur->msgno;
1059 if (cur->virtual == -1 && CHECK_LIMIT)
1062 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE)) {
1063 cur->pair = 0; /* force index entry's color to be re-evaluated */
1064 cur->collapsed = flag & M_THREAD_COLLAPSE;
1065 if (cur->virtual != -1) {
1067 if (flag & M_THREAD_COLLAPSE)
1068 final = roothdr->virtual;
1072 if (thread == top && (thread = thread->child) == NULL) {
1073 /* return value depends on action requested */
1074 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1076 else if (flag & M_THREAD_UNREAD)
1077 return ((old && new) ? new : (old ? old : new));
1078 else if (flag & M_THREAD_GET_HIDDEN)
1079 return (num_hidden);
1080 else if (flag & M_THREAD_NEXT_UNREAD)
1081 return (min_unread);
1085 cur = thread->message;
1088 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE)) {
1089 cur->pair = 0; /* force index entry's color to be re-evaluated */
1090 cur->collapsed = flag & M_THREAD_COLLAPSE;
1091 if (!roothdr && CHECK_LIMIT) {
1093 if (flag & M_THREAD_COLLAPSE)
1094 final = roothdr->virtual;
1097 if (reverse && (flag & M_THREAD_COLLAPSE) && (cur->msgno < minmsgno)
1099 minmsgno = cur->msgno;
1100 final = cur->virtual;
1103 if (flag & M_THREAD_COLLAPSE) {
1109 cur->virtual = cur->msgno;
1114 if (!cur->read && CHECK_LIMIT) {
1119 if (cur->msgno < min_unread_msgno) {
1120 min_unread = cur->virtual;
1121 min_unread_msgno = cur->msgno;
1125 if (cur->virtual == -1 && CHECK_LIMIT)
1130 thread = thread->child;
1131 else if (thread->next)
1132 thread = thread->next;
1136 while (!thread->next) {
1137 thread = thread->parent;
1138 if (thread == top) {
1145 thread = thread->next;
1149 /* return value depends on action requested */
1150 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1152 else if (flag & M_THREAD_UNREAD)
1153 return ((old && new) ? new : (old ? old : new));
1154 else if (flag & M_THREAD_GET_HIDDEN)
1155 return (num_hidden + 1);
1156 else if (flag & M_THREAD_NEXT_UNREAD)
1157 return (min_unread);
1164 /* if flag is 0, we want to know how many messages
1165 * are in the thread. if flag is 1, we want to know
1166 * our position in the thread. */
1167 int mutt_messages_in_thread (CONTEXT * ctx, HEADER * hdr, int flag)
1172 if ((Sort & SORT_MASK) != SORT_THREADS || !hdr->thread)
1175 threads[0] = hdr->thread;
1176 while (threads[0]->parent)
1177 threads[0] = threads[0]->parent;
1179 threads[1] = flag ? hdr->thread : threads[0]->next;
1181 for (i = 0; i < ((flag || !threads[1]) ? 1 : 2); i++) {
1182 while (!threads[i]->message)
1183 threads[i] = threads[i]->child;
1186 if (Sort & SORT_REVERSE)
1188 threads[0]->message->msgno -
1189 (threads[1] ? threads[1]->message->msgno : -1);
1192 (threads[1] ? threads[1]->message->msgno : ctx->msgcount) -
1193 threads[0]->message->msgno;
1202 HASH *mutt_make_id_hash (CONTEXT * ctx)
1208 hash = hash_create (ctx->msgcount * 2);
1210 for (i = 0; i < ctx->msgcount; i++) {
1212 if (hdr->env->message_id)
1213 hash_insert (hash, hdr->env->message_id, hdr, 0);
1219 HASH *mutt_make_subj_hash (CONTEXT * ctx)
1225 hash = hash_create (ctx->msgcount * 2);
1227 for (i = 0; i < ctx->msgcount; i++) {
1229 if (hdr->env->real_subj)
1230 hash_insert (hash, hdr->env->real_subj, hdr, 1);
1236 static void clean_references (THREAD * brk, THREAD * cur)
1242 for (; cur; cur = cur->next, done = 0) {
1243 /* parse subthread recursively */
1244 clean_references (brk, cur->child);
1247 break; /* skip pseudo-message */
1249 /* Looking for the first bad reference according to the new threading.
1250 * Optimal since Mutt stores the references in reverse order, and the
1251 * first loop should match immediatly for mails respecting RFC2822. */
1252 for (p = brk; !done && p; p = p->parent)
1253 for (ref = cur->message->env->references; p->message && ref;
1255 if (!m_strcasecmp(ref->data, p->message->env->message_id)) {
1261 HEADER *h = cur->message;
1263 /* clearing the References: header from obsolete Message-ID(s) */
1264 mutt_free_list (&ref->next);
1266 h->env->refs_changed = h->changed = 1;
1271 void mutt_break_thread (HEADER * hdr)
1273 mutt_free_list (&hdr->env->in_reply_to);
1274 mutt_free_list (&hdr->env->references);
1275 hdr->env->irt_changed = hdr->env->refs_changed = hdr->changed = 1;
1276 clean_references (hdr->thread, hdr->thread->child);
1279 static int link_threads (HEADER * parent, HEADER * child, CONTEXT * ctx)
1281 if (child == parent)
1284 mutt_break_thread (child);
1286 child->env->in_reply_to = mutt_new_list ();
1287 child->env->in_reply_to->data = m_strdup(parent->env->message_id);
1289 mutt_set_flag (ctx, child, M_TAG, 0);
1291 child->env->irt_changed = child->changed = 1;
1295 int mutt_link_threads (HEADER * cur, HEADER * last, CONTEXT * ctx)
1300 for (i = 0; i < ctx->vcount; i++)
1301 if (ctx->hdrs[Context->v2r[i]]->tagged)
1302 changed |= link_threads (cur, ctx->hdrs[Context->v2r[i]], ctx);
1305 changed = link_threads (cur, last, ctx);
1310 void mutt_adjust_subject (ENVELOPE* e) {
1311 regmatch_t pmatch[1];
1313 if (e && e->subject) {
1314 if (regexec (ReplyRegexp.rx, e->subject, 1, pmatch, 0) == 0)
1315 e->real_subj = e->subject + pmatch[0].rm_eo;
1317 e->real_subj = e->subject;
1321 void mutt_adjust_all_subjects (void) {
1324 if (!Context || !Context->msgcount)
1327 for (i = 0; i < Context->msgcount; i++)
1328 mutt_adjust_subject (Context->hdrs[i]->env);