From 15f7c88ecb3b071272a08fd9446c327c8ac60523 Mon Sep 17 00:00:00 2001
From: Thomas Roessler <roessler@does-not-exist.org>
Date: Mon, 25 Jul 2005 15:48:36 +0000
Subject: [PATCH] Get rid of IMAP-related special casing all over the thread
 editing code.

---
 copy.c      | 19 +++++++------------
 imap/imap.c |  2 +-
 mh.c        |  6 ++++--
 mutt.h      |  9 +++------
 muttlib.c   | 11 +++++++++--
 thread.c    | 30 +++---------------------------
 6 files changed, 27 insertions(+), 50 deletions(-)

diff --git a/copy.c b/copy.c
index 60ae50e9e..7dd415987 100644
--- a/copy.c
+++ b/copy.c
@@ -354,8 +354,9 @@ mutt_copy_header (FILE *in, HEADER *h, FILE *out, int flags, const char *prefix)
 {
   char buffer[SHORT_STRING];
 
-  flags |= (h->irt_changed ? CH_UPDATE_IRT : 0)
-         | (h->refs_changed ? CH_UPDATE_REFS : 0);
+  if (h->env)
+    flags |= (h->env->irt_changed ? CH_UPDATE_IRT : 0)
+      | (h->env->refs_changed ? CH_UPDATE_REFS : 0);
   
   if (mutt_copy_hdr (in, out, h->offset, h->content->offset, flags, prefix) == -1)
     return (-1);
@@ -379,15 +380,10 @@ mutt_copy_header (FILE *in, HEADER *h, FILE *out, int flags, const char *prefix)
   if (flags & CH_UPDATE)
   {
     if ((flags & CH_NOSTATUS) == 0)
-#ifdef USE_IMAP
-#define NEW_ENV new_env
-#else
-#define NEW_ENV env
-#endif
     {
-      if (h->irt_changed && h->NEW_ENV->in_reply_to)
+      if (h->env->irt_changed && h->env->in_reply_to)
       {
-	LIST *listp = h->NEW_ENV->in_reply_to;
+	LIST *listp = h->env->in_reply_to;
 
 	if (fputs ("In-Reply-To: ", out) == EOF)
 	  return (-1);
@@ -400,9 +396,9 @@ mutt_copy_header (FILE *in, HEADER *h, FILE *out, int flags, const char *prefix)
 	  return (-1);
       }
 
-      if (h->refs_changed && h->NEW_ENV->references)
+      if (h->env->refs_changed && h->env->references)
       {
-	LIST *listp = h->NEW_ENV->references, *refs = NULL, *t;
+	LIST *listp = h->env->references, *refs = NULL, *t;
 
 	if (fputs ("References: ", out) == EOF)
 	  return (-1);
@@ -427,7 +423,6 @@ mutt_copy_header (FILE *in, HEADER *h, FILE *out, int flags, const char *prefix)
 	if (fputc ('\n', out) == EOF)
 	  return (-1);
       }
-#undef NEW_ENV
 
       if (h->old || h->read)
       {
diff --git a/imap/imap.c b/imap/imap.c
index 7bcd8f092..6be188681 100644
--- a/imap/imap.c
+++ b/imap/imap.c
@@ -1068,7 +1068,7 @@ int imap_sync_mailbox (CONTEXT* ctx, int expunge, int* index_hint)
       /* if the message has been rethreaded or attachments have been deleted
        * we delete the message and reupload it.
        * This works better if we're expunging, of course. */
-      if (ctx->hdrs[n]->refs_changed || ctx->hdrs[n]->irt_changed ||
+      if ((ctx->hdrs[n]->env && (ctx->hdrs[n]->env->refs_changed || ctx->hdrs[n]->env->irt_changed)) ||
 	  ctx->hdrs[n]->attach_del)
       {
 	dprint (3, (debugfile, "imap_sync_mailbox: Attachments to be deleted, falling back to _mutt_save_message\n"));
diff --git a/mh.c b/mh.c
index 2c1c6343b..745fed335 100644
--- a/mh.c
+++ b/mh.c
@@ -1382,7 +1382,8 @@ static int mh_sync_message (CONTEXT * ctx, int msgno)
 {
   HEADER *h = ctx->hdrs[msgno];
 
-  if (h->attach_del || h->refs_changed || h->irt_changed)
+  if (h->attach_del || 
+      (h->env && (h->env->refs_changed || h->env->irt_changed)))
     if (mh_rewrite_message (ctx, msgno) != 0)
       return -1;
 
@@ -1393,7 +1394,8 @@ static int maildir_sync_message (CONTEXT * ctx, int msgno)
 {
   HEADER *h = ctx->hdrs[msgno];
 
-  if (h->attach_del || h->refs_changed || h->irt_changed)
+  if (h->attach_del || 
+      (h->env && (h->env->refs_changed || h->env->irt_changed)))
   {
     /* when doing attachment deletion/rethreading, fall back to the MH case. */
     if (mh_rewrite_message (ctx, msgno) != 0)
diff --git a/mutt.h b/mutt.h
index 03ec618ef..e8fe0989b 100644
--- a/mutt.h
+++ b/mutt.h
@@ -593,6 +593,9 @@ typedef struct envelope
   LIST *references;		/* message references (in reverse order) */
   LIST *in_reply_to;		/* in-reply-to header content */
   LIST *userhdrs;		/* user defined headers */
+
+  unsigned int irt_changed : 1; /* In-Reply-To changed to link/break threads */
+  unsigned int refs_changed : 1; /* References changed to break thread */
 } ENVELOPE;
 
 typedef struct parameter
@@ -709,8 +712,6 @@ typedef struct header
   unsigned int subject_changed : 1; 	/* used for threading */
   unsigned int threaded : 1;	    	/* used for threading */
   unsigned int display_subject : 1; 	/* used for threading */
-  unsigned int irt_changed : 1; /* In-Reply-To changed to link/break threads */
-  unsigned int refs_changed : 1; /* References changed to break thread */
   unsigned int recip_valid : 1;  	/* is_recipient is valid */
   unsigned int active : 1;	    	/* message is not to be removed */
   unsigned int trash : 1;		/* message is marked as trashed on disk.
@@ -751,10 +752,6 @@ typedef struct header
   char *tree;           	/* character string to print thread tree */
   struct thread *thread;
 
-#ifdef USE_IMAP
-  ENVELOPE *new_env;	/* envelope information for rethreading */
-#endif
-
 #ifdef MIXMASTER
   LIST *chain;
 #endif
diff --git a/muttlib.c b/muttlib.c
index 25c224cfa..3321ba9c5 100644
--- a/muttlib.c
+++ b/muttlib.c
@@ -693,8 +693,15 @@ void mutt_merge_envelopes(ENVELOPE* base, ENVELOPE** extra)
   MOVE_ELEM(supersedes);
   MOVE_ELEM(date);
   MOVE_ELEM(x_label);
-  MOVE_ELEM(references);
-  MOVE_ELEM(in_reply_to);
+  if (!base->refs_changed)
+  {
+    MOVE_ELEM(references);
+  }
+  if (!base->irt_changed)
+  {
+    MOVE_ELEM(in_reply_to);
+  }
+  
   /* real_subj is subordinate to subject */
   if (!base->subject)
   {
diff --git a/thread.c b/thread.c
index 37ec515a8..814f895ea 100644
--- a/thread.c
+++ b/thread.c
@@ -1377,16 +1377,7 @@ static void clean_references (THREAD *brk, THREAD *cur)
       /* clearing the References: header from obsolete Message-Id(s) */
       mutt_free_list (&ref->next);
 
-#ifdef USE_IMAP
-      if (h->new_env)
-	mutt_free_list (&h->new_env->references);
-      else
-	h->new_env = mutt_new_envelope ();
-
-      h->new_env->references = mutt_copy_list (h->env->references);
-#endif
-
-      h->refs_changed = h->changed = 1;
+      h->env->refs_changed = h->changed = 1;
     }
   }
 }
@@ -1395,17 +1386,7 @@ void mutt_break_thread (HEADER *hdr)
 {
   mutt_free_list (&hdr->env->in_reply_to);
   mutt_free_list (&hdr->env->references);
-  hdr->irt_changed = hdr->refs_changed = hdr->changed = 1;
-
-#ifdef USE_IMAP
-  if (hdr->new_env)
-  {
-    mutt_free_list (&hdr->new_env->in_reply_to);
-    mutt_free_list (&hdr->new_env->references);
-  }
-  else
-    hdr->new_env = mutt_new_envelope ();
-#endif
+  hdr->env->irt_changed = hdr->env->refs_changed = hdr->changed = 1;
 
   clean_references (hdr->thread, hdr->thread->child);
 }
@@ -1419,15 +1400,10 @@ static int link_threads (HEADER *parent, HEADER *child, CONTEXT *ctx)
 
   child->env->in_reply_to = mutt_new_list ();
   child->env->in_reply_to->data = safe_strdup (parent->env->message_id);
-
-#ifdef USE_IMAP
-  child->new_env->in_reply_to = mutt_new_list ();
-  child->new_env->in_reply_to->data = safe_strdup (parent->env->message_id);
-#endif
   
   mutt_set_flag (ctx, child, M_TAG, 0);
   
-  child->irt_changed = child->changed = 1;
+  child->env->irt_changed = child->changed = 1;
   return 1;
 }
 
-- 
2.40.0