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 our subject is different from our parent's, display it */
45 if (hdr->subject_changed)
48 /* if our subject is different from that of our closest previously displayed
49 * sibling, display the subject */
50 for (tmp = tree->prev; tmp; tmp = tmp->prev)
53 if (hdr && VISIBLE (hdr, ctx))
55 if (hdr->subject_changed)
62 /* if there is a parent-to-child subject change anywhere between us and our
63 * closest displayed ancestor, display the subject */
64 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);
87 while (!tree->message)
90 *array = tree->message;
91 array += Sort & SORT_REVERSE ? -1 : 1;
111 /* this calculates whether a node is the root of a subtree that has visible
112 * nodes, whether a node itself is visible, whether, if invisible, it has
113 * depth anyway, and whether any of its later siblings are roots of visible
114 * subtrees. while it's at it, it frees the old thread display, so we can
115 * skip parts of the tree in mutt_draw_tree() if we've decided here that we
116 * don't care about them any more.
118 static void calculate_visibility (CONTEXT *ctx, int *max_depth)
120 THREAD *tmp, *tree = ctx->tree;
121 int hide_top_missing = option (OPTHIDETOPMISSING) && !option (OPTHIDEMISSING);
122 int hide_top_limited = option (OPTHIDETOPLIMITED) && !option (OPTHIDELIMITED);
125 /* we walk each level backwards to make it easier to compute next_subtree_visible */
132 if (depth > *max_depth)
135 tree->subtree_visible = 0;
138 FREE (&tree->message->tree);
139 if (VISIBLE (tree->message, ctx))
143 tree->message->display_subject = need_display_subject (ctx, tree->message);
144 for (tmp = tree; tmp; tmp = tmp->parent)
146 if (tmp->subtree_visible)
149 tmp->subtree_visible = 2;
153 tmp->subtree_visible = 1;
159 tree->deep = !option (OPTHIDELIMITED);
165 tree->deep = !option (OPTHIDEMISSING);
167 tree->next_subtree_visible = tree->next && (tree->next->next_subtree_visible
168 || tree->next->subtree_visible);
180 while (tree && !tree->prev)
192 /* now fix up for the OPTHIDETOP* options if necessary */
193 if (hide_top_limited || hide_top_missing)
198 if (!tree->visible && tree->deep && tree->subtree_visible < 2
199 && ((tree->message && hide_top_limited) || (!tree->message && hide_top_missing)))
201 if (!tree->deep && tree->child && tree->subtree_visible)
207 while (tree && !tree->next)
218 /* Since the graphics characters have a value >255, I have to resort to
219 * using escape sequences to pass the information to print_enriched_string().
220 * These are the macros M_TREE_* defined in mutt.h.
222 * ncurses should automatically use the default ASCII characters instead of
223 * graphics chars on terminals which don't support them (see the man page
226 void mutt_draw_tree (CONTEXT *ctx)
228 char *pfx = NULL, *mypfx = NULL, *arrow = NULL, *myarrow = NULL, *new_tree;
229 char corner = (Sort & SORT_REVERSE) ? M_TREE_ULCORNER : M_TREE_LLCORNER;
230 char vtee = (Sort & SORT_REVERSE) ? M_TREE_BTEE : M_TREE_TTEE;
231 int depth = 0, start_depth = 0, max_depth = 0, width = option (OPTNARROWTREE) ? 1 : 2;
232 THREAD *nextdisp = NULL, *pseudo = NULL, *parent = NULL, *tree = ctx->tree;
234 /* Do the visibility calculations and free the old thread chars.
235 * From now on we can simply ignore invisible subtrees
237 calculate_visibility (ctx, &max_depth);
238 pfx = safe_malloc (width * max_depth + 2);
239 arrow = safe_malloc (width * max_depth + 2);
244 myarrow = arrow + (depth - start_depth - (start_depth ? 0 : 1)) * width;
245 if (depth && start_depth == depth)
246 myarrow[0] = nextdisp ? M_TREE_LTEE : corner;
247 else if (parent->message && !option (OPTHIDELIMITED))
248 myarrow[0] = M_TREE_HIDDEN;
249 else if (!parent->message && !option (OPTHIDEMISSING))
250 myarrow[0] = M_TREE_MISSING;
254 myarrow[1] = pseudo ? M_TREE_STAR
255 : (tree->duplicate_thread ? M_TREE_EQUALS : M_TREE_HLINE);
258 myarrow[width] = M_TREE_RARROW;
259 myarrow[width + 1] = 0;
260 new_tree = safe_malloc ((2 + depth * width));
263 strncpy (new_tree, pfx, (start_depth - 1) * width);
264 strfcpy (new_tree + (start_depth - 1) * width,
265 arrow, (1 + depth - start_depth) * width + 2);
268 strfcpy (new_tree, arrow, 2 + depth * width);
269 tree->message->tree = new_tree;
272 if (tree->child && depth)
274 mypfx = pfx + (depth - 1) * width;
275 mypfx[0] = nextdisp ? M_TREE_VLINE : M_TREE_SPACE;
277 mypfx[1] = M_TREE_SPACE;
284 if (tree->child && tree->subtree_visible)
292 /* we do this here because we need to make sure that the first child thread
293 * of the old tree that we deal with is actually displayed if any are,
294 * or we might set the parent variable wrong while going through it. */
295 while (!tree->subtree_visible && tree->next)
300 while (!tree->next && tree->parent)
304 if (tree == nextdisp)
311 if (start_depth == depth)
318 if (tree == nextdisp)
326 if (!pseudo && tree->fake_thread)
328 if (!nextdisp && tree->next_subtree_visible)
338 /* since we may be trying to attach as a pseudo-thread a THREAD that
339 * has no message, we have to make a list of all the subjects of its
340 * most immediate existing descendants. we also note the earliest
341 * date on any of the parents and put it in *dateptr. */
342 static LIST *make_subject_list (THREAD *cur, time_t *dateptr)
347 LIST *curlist, *oldlist, *newlist, *subjects = NULL;
352 while (!cur->message)
357 thisdate = option (OPTTHREADRECEIVED)
358 ? cur->message->received : cur->message->date_sent;
359 if (!*dateptr || thisdate < *dateptr)
363 env = cur->message->env;
364 if (env->real_subj &&
365 ((env->real_subj != env->subject) || (!option (OPTSORTRE))))
367 for (curlist = subjects, oldlist = NULL;
368 curlist; oldlist = curlist, curlist = curlist->next)
370 rc = mutt_strcmp (env->real_subj, curlist->data);
374 if (!curlist || rc > 0)
376 newlist = safe_calloc (1, sizeof (LIST));
377 newlist->data = env->real_subj;
380 newlist->next = oldlist->next;
381 oldlist->next = newlist;
385 newlist->next = subjects;
391 while (!cur->next && cur != start)
403 /* find the best possible match for a parent mesage based upon subject.
404 * if there are multiple matches, the one which was sent the latest, but
405 * before the current message, is used.
407 static THREAD *find_subject (CONTEXT *ctx, THREAD *cur)
409 struct hash_elem *ptr;
410 THREAD *tmp, *last = NULL;
412 LIST *subjects = NULL, *oldlist;
415 subjects = make_subject_list (cur, &date);
419 hash = hash_string ((unsigned char *) subjects->data,
420 ctx->subj_hash->nelem);
421 for (ptr = ctx->subj_hash->table[hash]; ptr; ptr = ptr->next)
423 tmp = ((HEADER *) ptr->data)->thread;
424 if (tmp != cur && /* don't match the same message */
425 !tmp->fake_thread && /* don't match pseudo threads */
426 tmp->message->subject_changed && /* only match interesting replies */
427 !is_descendant (tmp, cur) && /* don't match in the same thread */
428 (date >= (option (OPTTHREADRECEIVED) ?
429 tmp->message->received :
430 tmp->message->date_sent)) &&
432 (option (OPTTHREADRECEIVED) ?
433 (last->message->received < tmp->message->received) :
434 (last->message->date_sent < tmp->message->date_sent))) &&
435 tmp->message->env->real_subj &&
436 mutt_strcmp (subjects->data, tmp->message->env->real_subj) == 0)
437 last = tmp; /* best match so far */
441 subjects = subjects->next;
447 /* remove cur and its descendants from their current location.
448 * also make sure ancestors of cur no longer are sorted by the
449 * fact that cur is their descendant. */
450 static void unlink_message (THREAD **old, THREAD *cur)
455 cur->prev->next = cur->next;
460 cur->next->prev = cur->prev;
464 for (tmp = cur->parent; tmp && tmp->sort_key == cur->sort_key;
466 tmp->sort_key = NULL;
470 /* add cur as a prior sibling of *new, with parent newparent */
471 static void insert_message (THREAD **new, THREAD *newparent, THREAD *cur)
476 cur->parent = newparent;
482 /* thread by subject things that didn't get threaded by message-id */
483 static void pseudo_threads (CONTEXT *ctx)
485 THREAD *tree = ctx->tree, *top = tree;
486 THREAD *tmp, *cur, *parent, *curchild, *nextchild;
489 ctx->subj_hash = mutt_make_subj_hash (ctx);
495 if ((parent = find_subject (ctx, cur)) != NULL)
497 cur->fake_thread = 1;
498 unlink_message (&top, cur);
499 insert_message (&parent->child, parent, cur);
500 parent->sort_children = 1;
504 while (!tmp->message)
507 /* if the message we're attaching has pseudo-children, they
508 * need to be attached to its parent, so move them up a level.
509 * but only do this if they have the same real subject as the
510 * parent, since otherwise they rightly belong to the message
511 * we're attaching. */
513 || !mutt_strcmp (tmp->message->env->real_subj,
514 parent->message->env->real_subj))
516 tmp->message->subject_changed = 0;
518 for (curchild = tmp->child; curchild; )
520 nextchild = curchild->next;
521 if (curchild->fake_thread)
523 unlink_message (&tmp->child, curchild);
524 insert_message (&parent->child, parent, curchild);
526 curchild = nextchild;
530 while (!tmp->next && tmp != cur)
544 void mutt_clear_threads (CONTEXT *ctx)
548 for (i = 0; i < ctx->msgcount; i++)
550 ctx->hdrs[i]->thread = NULL;
551 ctx->hdrs[i]->threaded = 0;
555 if (ctx->thread_hash)
556 hash_destroy (&ctx->thread_hash, *free);
559 int compare_threads (const void *a, const void *b)
561 static sort_t *sort_func = NULL;
564 return ((*sort_func) (&(*((THREAD **) a))->sort_key,
565 &(*((THREAD **) b))->sort_key));
566 /* a hack to let us reset sort_func even though we can't
567 * have extra arguments because of qsort
572 sort_func = mutt_get_sort_func (Sort);
573 return (sort_func ? 1 : 0);
577 THREAD *mutt_sort_subthreads (THREAD *thread, int init)
579 THREAD **array, *sort_key, *top, *tmp;
581 int i, array_size, sort_top = 0;
583 /* we put things into the array backwards to save some cycles,
584 * but we want to have to move less stuff around if we're
585 * resorting, so we sort backwards and then put them back
586 * in reverse order so they're forwards
588 Sort ^= SORT_REVERSE;
589 if (!compare_threads (NULL, NULL))
594 array = safe_calloc ((array_size = 256), sizeof (THREAD *));
597 if (init || !thread->sort_key)
599 thread->sort_key = NULL;
602 thread->parent->sort_children = 1;
609 thread = thread->child;
614 /* if it has no children, it must be real. sort it on its own merits */
615 thread->sort_key = thread->message;
619 thread = thread->next;
624 while (!thread->next)
626 /* if it has siblings and needs to be sorted, sort it... */
627 if (thread->prev && (thread->parent ? thread->parent->sort_children : sort_top))
629 /* put them into the array */
630 for (i = 0; thread; i++, thread = thread->prev)
633 safe_realloc (&array, (array_size *= 2) * sizeof (THREAD *));
638 qsort ((void *) array, i, sizeof (THREAD *), *compare_threads);
640 /* attach them back together. make thread the last sibling. */
643 array[i - 1]->prev = NULL;
646 thread->parent->child = array[i - 1];
652 array[i - 1]->prev = array[i];
653 array[i]->next = array[i - 1];
660 thread = thread->parent;
662 if (!thread->sort_key || thread->sort_children)
664 /* make sort_key the first or last sibling, as appropriate */
665 sort_key = (!(Sort & SORT_LAST) ^ !(Sort & SORT_REVERSE)) ? thread->child : tmp;
667 /* we just sorted its children */
668 thread->sort_children = 0;
670 oldsort_key = thread->sort_key;
671 thread->sort_key = thread->message;
673 if (Sort & SORT_LAST)
675 if (!thread->sort_key
676 || ((((Sort & SORT_REVERSE) ? 1 : -1)
677 * compare_threads ((void *) &thread,
680 thread->sort_key = sort_key->sort_key;
682 else if (!thread->sort_key)
683 thread->sort_key = sort_key->sort_key;
685 /* if its sort_key has changed, we need to resort it and siblings */
686 if (oldsort_key != thread->sort_key)
689 thread->parent->sort_children = 1;
697 Sort ^= SORT_REVERSE;
703 thread = thread->next;
707 static void check_subjects (CONTEXT *ctx, int init)
713 for (i = 0; i < ctx->msgcount; i++)
716 if (cur->thread->check_subject)
717 cur->thread->check_subject = 0;
721 /* figure out which messages have subjects different than their parents' */
722 tmp = cur->thread->parent;
723 while (tmp && !tmp->message)
729 cur->subject_changed = 1;
730 else if (cur->env->real_subj && tmp->message->env->real_subj)
731 cur->subject_changed = mutt_strcmp (cur->env->real_subj,
732 tmp->message->env->real_subj) ? 1 : 0;
734 cur->subject_changed = (cur->env->real_subj
735 || tmp->message->env->real_subj) ? 1 : 0;
739 void mutt_sort_threads (CONTEXT *ctx, int init)
742 int i, oldsort, using_refs = 0;
743 THREAD *thread, *new, *tmp, top;
746 /* set Sort to the secondary method to support the set sort_aux=reverse-*
747 * settings. The sorting functions just look at the value of
753 if (!ctx->thread_hash)
757 ctx->thread_hash = hash_create (ctx->msgcount * 2);
759 /* we want a quick way to see if things are actually attached to the top of the
760 * thread tree or if they're just dangling, so we attach everything to a top
761 * node temporarily */
762 top.parent = top.next = top.prev = NULL;
763 top.child = ctx->tree;
764 for (thread = ctx->tree; thread; thread = thread->next)
765 thread->parent = ⊤
767 /* put each new message together with the matching messageless THREAD if it
768 * exists. otherwise, if there is a THREAD that already has a message, thread
769 * new message as an identical child. if we didn't attach the message to a
770 * THREAD, make a new one for it. */
771 for (i = 0; i < ctx->msgcount; i++)
777 if ((!init || option (OPTDUPTHREADS)) && cur->env->message_id)
778 thread = hash_find (ctx->thread_hash, cur->env->message_id);
782 if (thread && !thread->message)
784 /* this is a message which was missing before */
785 thread->message = cur;
786 cur->thread = thread;
787 thread->check_subject = 1;
789 /* mark descendants as needing subject_changed checked */
790 for (tmp = (thread->child ? thread->child : thread); tmp != thread; )
792 while (!tmp->message)
794 tmp->check_subject = 1;
795 while (!tmp->next && tmp != thread)
803 /* remove threading info above it based on its children, which we'll
804 * recalculate based on its headers. make sure not to leave
805 * dangling missing messages. note that we haven't kept track
806 * of what info came from its children and what from its siblings'
807 * children, so we just remove the stuff that's definitely from it */
810 tmp = thread->parent;
811 unlink_message (&tmp->child, thread);
812 thread->parent = NULL;
813 thread->sort_key = NULL;
814 thread->fake_thread = 0;
816 } while (thread != &top && !thread->child && !thread->message);
821 new = (option (OPTDUPTHREADS) ? thread : NULL);
823 thread = safe_calloc (1, sizeof (THREAD));
824 thread->message = cur;
825 thread->check_subject = 1;
826 cur->thread = thread;
827 hash_insert (ctx->thread_hash,
828 cur->env->message_id ? cur->env->message_id : "",
833 if (new->duplicate_thread)
836 thread = cur->thread;
838 insert_message (&new->child, new, thread);
839 thread->duplicate_thread = 1;
840 thread->message->threaded = 1;
846 /* unlink pseudo-threads because they might be children of newly
847 * arrived messages */
848 thread = cur->thread;
849 for (new = thread->child; new; )
852 if (new->fake_thread)
854 unlink_message (&thread->child, new);
855 insert_message (&top.child, &top, new);
856 new->fake_thread = 0;
863 /* thread by references */
864 for (i = 0; i < ctx->msgcount; i++)
871 thread = cur->thread;
878 /* look at the beginning of in-reply-to: */
879 if ((ref = cur->env->in_reply_to) != NULL)
883 ref = cur->env->references;
887 else if (using_refs == 1)
889 /* if there's no references header, use all the in-reply-to:
890 * data that we have. otherwise, use the first reference
891 * if it's different than the first in-reply-to, otherwise use
892 * the second reference (since at least eudora puts the most
893 * recent reference in in-reply-to and the rest in references)
895 if (!cur->env->references)
899 if (mutt_strcmp (ref->data, cur->env->references->data))
900 ref = cur->env->references;
902 ref = cur->env->references->next;
908 ref = ref->next; /* go on with references */
913 if ((new = hash_find (ctx->thread_hash, ref->data)) == NULL)
915 new = safe_calloc (1, sizeof (THREAD));
916 hash_insert (ctx->thread_hash, ref->data, new, 1);
920 if (new->duplicate_thread)
922 if (is_descendant (new, thread)) /* no loops! */
927 unlink_message (&top.child, thread);
928 insert_message (&new->child, new, thread);
930 if (thread->message || (thread->parent && thread->parent != &top))
935 insert_message (&top.child, &top, thread);
938 /* detach everything from the temporary top node */
939 for (thread = top.child; thread; thread = thread->next)
941 thread->parent = NULL;
943 ctx->tree = top.child;
945 check_subjects (ctx, init);
947 if (!option (OPTSTRICTTHREADS))
948 pseudo_threads (ctx);
952 ctx->tree = mutt_sort_subthreads (ctx->tree, init);
954 /* restore the oldsort order. */
957 /* Put the list into an array. */
958 linearize_tree (ctx);
960 /* Draw the thread tree. */
961 mutt_draw_tree (ctx);
965 static HEADER *find_virtual (THREAD *cur, int reverse)
969 if (cur->message && cur->message->virtual >= 0)
970 return (cur->message);
973 if ((cur = cur->child) == NULL)
976 while (reverse && cur->next)
981 if (cur->message && cur->message->virtual >= 0)
982 return (cur->message);
988 while (reverse && cur->next)
991 else if (reverse ? cur->prev : cur->next)
992 cur = reverse ? cur->prev : cur->next;
995 while (!(reverse ? cur->prev : cur->next))
1001 cur = reverse ? cur->prev : cur->next;
1007 int _mutt_aside_thread (HEADER *hdr, short dir, short subthreads)
1012 if ((Sort & SORT_MASK) != SORT_THREADS)
1014 mutt_error _("Threading is not enabled.");
1015 return (hdr->virtual);
1027 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0))
1029 while (!cur->next && cur->parent)
1034 while (!cur->prev && cur->parent)
1039 if ((dir != 0) ^ ((Sort & SORT_REVERSE) != 0))
1046 tmp = find_virtual (cur, 0);
1056 tmp = find_virtual (cur, 1);
1060 return (tmp->virtual);
1063 int mutt_parent_message (CONTEXT *ctx, HEADER *hdr)
1067 if ((Sort & SORT_MASK) != SORT_THREADS)
1069 mutt_error _("Threading is not enabled.");
1070 return (hdr->virtual);
1073 for (thread = hdr->thread->parent; thread; thread = thread->parent)
1075 if ((hdr = thread->message) != NULL)
1077 if (VISIBLE (hdr, ctx))
1078 return (hdr->virtual);
1081 mutt_error _("Parent message is not visible in this limited view.");
1087 mutt_error _("Parent message is not available.");
1091 void mutt_set_virtual (CONTEXT *ctx)
1099 for (i = 0; i < ctx->msgcount; i++)
1102 if (cur->virtual >= 0)
1104 cur->virtual = ctx->vcount;
1105 ctx->v2r[ctx->vcount] = i;
1107 ctx->vsize += cur->content->length + cur->content->offset - cur->content->hdr_offset;
1108 cur->num_hidden = mutt_get_hidden (ctx, cur);
1113 int _mutt_traverse_thread (CONTEXT *ctx, HEADER *cur, int flag)
1115 THREAD *thread, *top;
1116 HEADER *roothdr = NULL;
1117 int final, reverse = (Sort & SORT_REVERSE), minmsgno;
1118 int num_hidden = 0, new = 0, old = 0;
1119 int min_unread_msgno = INT_MAX, min_unread = cur->virtual;
1120 #define CHECK_LIMIT (!ctx->pattern || cur->limited)
1122 if ((Sort & SORT_MASK) != SORT_THREADS && !(flag & M_THREAD_GET_HIDDEN))
1124 mutt_error (_("Threading is not enabled."));
1125 return (cur->virtual);
1128 final = cur->virtual;
1129 thread = cur->thread;
1130 while (thread->parent)
1131 thread = thread->parent;
1133 while (!thread->message)
1134 thread = thread->child;
1135 cur = thread->message;
1136 minmsgno = cur->msgno;
1138 if (!cur->read && CHECK_LIMIT)
1144 if (cur->msgno < min_unread_msgno)
1146 min_unread = cur->virtual;
1147 min_unread_msgno = cur->msgno;
1151 if (cur->virtual == -1 && CHECK_LIMIT)
1154 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1156 cur->pair = 0; /* force index entry's color to be re-evaluated */
1157 cur->collapsed = flag & M_THREAD_COLLAPSE;
1158 if (cur->virtual != -1)
1161 if (flag & M_THREAD_COLLAPSE)
1162 final = roothdr->virtual;
1166 if (thread == top && (thread = thread->child) == NULL)
1168 /* return value depends on action requested */
1169 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1171 else if (flag & M_THREAD_UNREAD)
1172 return ((old && new) ? new : (old ? old : new));
1173 else if (flag & M_THREAD_GET_HIDDEN)
1174 return (num_hidden);
1175 else if (flag & M_THREAD_NEXT_UNREAD)
1176 return (min_unread);
1181 cur = thread->message;
1185 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1187 cur->pair = 0; /* force index entry's color to be re-evaluated */
1188 cur->collapsed = flag & M_THREAD_COLLAPSE;
1189 if (!roothdr && CHECK_LIMIT)
1192 if (flag & M_THREAD_COLLAPSE)
1193 final = roothdr->virtual;
1196 if (reverse && (flag & M_THREAD_COLLAPSE) && (cur->msgno < minmsgno) && CHECK_LIMIT)
1198 minmsgno = cur->msgno;
1199 final = cur->virtual;
1202 if (flag & M_THREAD_COLLAPSE)
1210 cur->virtual = cur->msgno;
1215 if (!cur->read && CHECK_LIMIT)
1221 if (cur->msgno < min_unread_msgno)
1223 min_unread = cur->virtual;
1224 min_unread_msgno = cur->msgno;
1228 if (cur->virtual == -1 && CHECK_LIMIT)
1233 thread = thread->child;
1234 else if (thread->next)
1235 thread = thread->next;
1239 while (!thread->next)
1241 thread = thread->parent;
1250 thread = thread->next;
1254 /* return value depends on action requested */
1255 if (flag & (M_THREAD_COLLAPSE | M_THREAD_UNCOLLAPSE))
1257 else if (flag & M_THREAD_UNREAD)
1258 return ((old && new) ? new : (old ? old : new));
1259 else if (flag & M_THREAD_GET_HIDDEN)
1260 return (num_hidden+1);
1261 else if (flag & M_THREAD_NEXT_UNREAD)
1262 return (min_unread);
1269 /* if flag is 0, we want to know how many messages
1270 * are in the thread. if flag is 1, we want to know
1271 * our position in the thread. */
1272 int mutt_messages_in_thread (CONTEXT *ctx, HEADER *hdr, int flag)
1277 if ((Sort & SORT_MASK) != SORT_THREADS || !hdr->thread)
1280 threads[0] = hdr->thread;
1281 while (threads[0]->parent)
1282 threads[0] = threads[0]->parent;
1284 threads[1] = flag ? hdr->thread : threads[0]->next;
1286 for (i = 0; i < ((flag || !threads[1]) ? 1 : 2); i++)
1288 while (!threads[i]->message)
1289 threads[i] = threads[i]->child;
1292 if (Sort & SORT_REVERSE)
1293 rc = threads[0]->message->msgno - (threads[1] ? threads[1]->message->msgno : -1);
1295 rc = (threads[1] ? threads[1]->message->msgno : ctx->msgcount) - threads[0]->message->msgno;
1304 HASH *mutt_make_id_hash (CONTEXT *ctx)
1310 hash = hash_create (ctx->msgcount * 2);
1312 for (i = 0; i < ctx->msgcount; i++)
1315 if (hdr->env->message_id)
1316 hash_insert (hash, hdr->env->message_id, hdr, 0);
1322 HASH *mutt_make_subj_hash (CONTEXT *ctx)
1328 hash = hash_create (ctx->msgcount * 2);
1330 for (i = 0; i < ctx->msgcount; i++)
1333 if (hdr->env->real_subj)
1334 hash_insert (hash, hdr->env->real_subj, hdr, 1);
1340 static void clean_references (THREAD *brk, THREAD *cur)
1346 for (; cur; cur = cur->next, done = 0)
1348 /* parse subthread recursively */
1349 clean_references (brk, cur->child);
1352 break; /* skip pseudo-message */
1354 /* Looking for the first bad reference according to the new threading.
1355 * Optimal since Mutt stores the references in reverse order, and the
1356 * first loop should match immediatly for mails respecting RFC2822. */
1357 for (p = brk; !done && p; p = p->parent)
1358 for (ref = cur->message->env->references; p->message && ref; ref = ref->next)
1359 if (!mutt_strcasecmp (ref->data, p->message->env->message_id))
1367 HEADER *h = cur->message;
1369 /* clearing the References: header from obsolete Message-Id(s) */
1370 mutt_free_list (&ref->next);
1372 #ifdef IMAP_EDIT_THREADS
1374 mutt_free_list (&h->new_env->references);
1376 h->new_env = mutt_new_envelope ();
1378 h->new_env->references = mutt_copy_list (h->env->references);
1381 h->refs_changed = h->changed = 1;
1386 void mutt_break_thread (HEADER *hdr)
1388 mutt_free_list (&hdr->env->in_reply_to);
1389 mutt_free_list (&hdr->env->references);
1390 hdr->irt_changed = hdr->refs_changed = hdr->changed = 1;
1392 #ifdef IMAP_EDIT_THREADS
1395 mutt_free_list (&hdr->new_env->in_reply_to);
1396 mutt_free_list (&hdr->new_env->references);
1399 hdr->new_env = mutt_new_envelope ();
1402 clean_references (hdr->thread, hdr->thread->child);
1405 static int link_threads (HEADER *parent, HEADER *child, CONTEXT *ctx)
1407 if (child == parent)
1410 mutt_break_thread (child);
1412 child->env->in_reply_to = mutt_new_list ();
1413 child->env->in_reply_to->data = safe_strdup (parent->env->message_id);
1415 #ifdef IMAP_EDIT_THREADS
1416 child->new_env->in_reply_to = mutt_new_list ();
1417 child->new_env->in_reply_to->data = safe_strdup (parent->env->message_id);
1420 mutt_set_flag (ctx, child, M_TAG, 0);
1422 child->irt_changed = child->changed = 1;
1426 int mutt_link_threads (HEADER *cur, HEADER *last, CONTEXT *ctx)
1432 for (i = 0; i < ctx->vcount; i++)
1433 if (ctx->hdrs[Context->v2r[i]]->tagged)
1434 changed |= link_threads (cur, ctx->hdrs[Context->v2r[i]], ctx);
1437 changed = link_threads (cur, last, ctx);