+muttsort
[www.jankratochvil.net.git] / project / muttsort / mutt-sort.diff
1 Index: thread.c
2 ===================================================================
3 RCS file: /home/roessler/cvs/mutt/thread.c,v
4 retrieving revision 2.15
5 diff -I! -ICVS -u -r2.15 thread.c
6 --- thread.c    2001/04/25 22:08:41     2.15
7 +++ thread.c    2001/07/26 02:18:11
8 @@ -535,23 +535,48 @@
9  
10  HEADER *mutt_sort_subthreads (HEADER *hdr, sort_t *func)
11  {
12 -  HEADER *top = NULL;
13 -  HEADER *t;
14 +  static HEADER **array = NULL;
15 +  static unsigned array_size = 0, array_alloc = 0;
16 +  unsigned array_alloc_base;
17 +  unsigned items, item;
18    
19 -  while (hdr)
20 +  if (!func || !hdr)
21 +    return hdr;
22 +
23 +  array_alloc_base = array_alloc;
24 +  for (item = array_alloc_base; hdr; hdr = hdr->next)
25    {
26 -    t = hdr;
27 -    hdr = hdr->next;
28 -    insert_message (&top, t, func);
29 -    if (t->child)
30 -      t->child = mutt_sort_subthreads (t->child, func);
31 +    if (item >= array_size)
32 +    {
33 +      array_size = 2 * MAX(array_size, 0x100);
34 +      safe_realloc ((void **) &array, array_size * sizeof (*array));
35 +    }
36 +    array[item++] = hdr;
37 +    if (hdr->child)
38 +    {
39 +      array_alloc = item;
40 +      hdr->child = mutt_sort_subthreads (hdr->child, func);
41 +    }
42    }
43 -  return top;
44 +  array_alloc = array_alloc_base;
45 +
46 +  items = item - array_alloc_base;
47 +  qsort ((void *) (array + array_alloc_base), items, sizeof (*array), func);
48 +
49 +  array[array_alloc_base          ]->prev = NULL;
50 +  array[array_alloc_base + items-1]->next = NULL;
51 +
52 +  for (item = array_alloc_base; item < array_alloc_base + items-1; item++)
53 +  {
54 +    array[item  ]->next = array[item+1];
55 +    array[item+1]->prev = array[item  ];
56 +  }
57 +
58 +  return array[array_alloc_base];
59  }
60  
61  void mutt_sort_threads (CONTEXT *ctx, int init)
62  {
63 -  sort_t *usefunc = NULL;
64    HEADER *tmp, *CUR;
65    int i, oldsort;
66    
67 @@ -562,10 +587,6 @@
68    oldsort = Sort;
69    Sort = SortAux;
70    
71 -  /* if the SORT_LAST bit is set, we save sorting for later */
72 -  if (!(Sort & SORT_LAST))
73 -    usefunc = mutt_get_sort_func (Sort);
74 -
75    for (i = 0; i < ctx->msgcount; i++)
76    {
77      CUR = ctx->hdrs[i];
78 @@ -580,7 +601,7 @@
79        CUR->subject_changed = 1;
80        unlink_message (&CUR->parent->child, CUR);
81        CUR->parent = NULL;
82 -      insert_message (&ctx->tree, CUR, usefunc);
83 +      insert_message (&ctx->tree, CUR, NULL);
84      }
85      else if (!CUR->threaded)
86      {
87 @@ -603,15 +624,19 @@
88          * parent) during the initial threading.
89          */
90         if (CUR->env->message_id)
91 -         move_descendants (tmp ? &tmp->child : &ctx->tree, CUR, usefunc);
92 +         move_descendants (tmp ? &tmp->child : &ctx->tree, CUR, NULL);
93        }
94 -      insert_message (tmp ? &tmp->child : &ctx->tree, CUR, usefunc);
95 +      insert_message (tmp ? &tmp->child : &ctx->tree, CUR, NULL);
96        CUR->threaded = 1;
97      }
98    }
99  
100    if (!option (OPTSTRICTTHREADS))
101 -    pseudo_threads (ctx, usefunc);
102 +    pseudo_threads (ctx, NULL);
103 +
104 +  /* if the SORT_LAST bit is not set, sort the whole tree now */
105 +  if (!(Sort & SORT_LAST))
106 +    ctx->tree = mutt_sort_subthreads (ctx->tree, mutt_get_sort_func (Sort));
107  
108    /* now that the whole tree is put together, we can sort by last-* */
109    if (Sort & SORT_LAST)