forked from neomutt/neomutt
/
mutt_thread.c
1780 lines (1597 loc) · 47 KB
/
mutt_thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file
* Create/manipulate threading in emails
*
* @authors
* Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page neo_mutt_thread Create/manipulate threading in emails
*
* Create/manipulate threading in emails
*/
#include "config.h"
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "mutt.h"
#include "mutt_thread.h"
#include "mx.h"
#include "options.h"
#include "protos.h"
#include "sort.h"
/**
* struct ThreadsContext - The "current" threading state
*/
struct ThreadsContext
{
struct Mailbox *mailbox; ///< Current mailbox
struct MuttThread *tree; ///< Top of thread tree
struct HashTable *hash; ///< Hash table for threads
short c_sort; ///< Last sort method
short c_sort_aux; ///< Last sort_aux method
};
/**
* UseThreadsMethods - Choices for '$use_threads' for the index
*/
static const struct Mapping UseThreadsMethods[] = {
// clang-format off
{ "unset", UT_UNSET },
{ "flat", UT_FLAT },
{ "threads", UT_THREADS },
{ "reverse", UT_REVERSE },
// aliases
{ "no", UT_FLAT },
{ "yes", UT_THREADS },
{ NULL, 0 },
// clang-format on
};
struct EnumDef UseThreadsTypeDef = {
"use_threads_type",
4,
(struct Mapping *) &UseThreadsMethods,
};
/**
* mutt_thread_style - Which threading style is active?
* @retval UT_FLAT No threading in use
* @retval UT_THREADS Normal threads (root above subthread)
* @retval UT_REVERSE Reverse threads (subthread above root)
*
* @note UT_UNSET is never returned; rather, this function considers the
* interaction between $use_threads and $sort.
*/
enum UseThreads mutt_thread_style(void)
{
const unsigned char c_use_threads =
cs_subset_enum(NeoMutt->sub, "use_threads");
const short c_sort = cs_subset_sort(NeoMutt->sub, "sort");
if (c_use_threads > UT_FLAT)
return c_use_threads;
if ((c_sort & SORT_MASK) != SORT_THREADS)
return UT_FLAT;
if (c_sort & SORT_REVERSE)
return UT_REVERSE;
return UT_THREADS;
}
/**
* get_use_threads_str - Convert UseThreads enum to string
* @param value Value to convert
* @retval ptr String form of value
*/
const char *get_use_threads_str(enum UseThreads value)
{
return mutt_map_get_name(value, UseThreadsMethods);
}
/**
* sort_validator - Validate values of "sort" - Implements ConfigDef::validator() - @ingroup cfg_def_validator
*/
int sort_validator(const struct ConfigSet *cs, const struct ConfigDef *cdef,
intptr_t value, struct Buffer *err)
{
if (((value & SORT_MASK) == SORT_THREADS) && (value & SORT_LAST))
{
mutt_buffer_printf(err, _("Cannot use 'last-' prefix with 'threads' for %s"), cdef->name);
return CSR_ERR_INVALID;
}
return CSR_SUCCESS;
}
/**
* is_visible - Is the message visible?
* @param e Email
* @retval true The message is not hidden in some way
*/
static bool is_visible(struct Email *e)
{
return e->vnum >= 0 || (e->collapsed && e->visible);
}
/**
* need_display_subject - Determines whether to display a message's subject
* @param e Email
* @retval true The subject should be displayed
*/
static bool need_display_subject(struct Email *e)
{
struct MuttThread *tmp = NULL;
struct MuttThread *tree = e->thread;
/* if the user disabled subject hiding, display it */
const bool c_hide_thread_subject =
cs_subset_bool(NeoMutt->sub, "hide_thread_subject");
if (!c_hide_thread_subject)
return true;
/* if our subject is different from our parent's, display it */
if (e->subject_changed)
return true;
/* if our subject is different from that of our closest previously displayed
* sibling, display the subject */
for (tmp = tree->prev; tmp; tmp = tmp->prev)
{
e = tmp->message;
if (e && is_visible(e))
{
if (e->subject_changed)
return true;
break;
}
}
/* if there is a parent-to-child subject change anywhere between us and our
* closest displayed ancestor, display the subject */
for (tmp = tree->parent; tmp; tmp = tmp->parent)
{
e = tmp->message;
if (e)
{
if (is_visible(e))
return false;
if (e->subject_changed)
return true;
}
}
/* if we have no visible parent or previous sibling, display the subject */
return true;
}
/**
* linearize_tree - Flatten an email thread
* @param tctx Threading context
*/
static void linearize_tree(struct ThreadsContext *tctx)
{
if (!tctx || !tctx->mailbox)
return;
struct Mailbox *m = tctx->mailbox;
const bool reverse = (mutt_thread_style() == UT_REVERSE);
struct MuttThread *tree = tctx->tree;
struct Email **array = m->emails + (reverse ? m->msg_count - 1 : 0);
while (tree)
{
while (!tree->message)
tree = tree->child;
*array = tree->message;
array += reverse ? -1 : 1;
if (tree->child)
tree = tree->child;
else
{
while (tree)
{
if (tree->next)
{
tree = tree->next;
break;
}
else
tree = tree->parent;
}
}
}
}
/**
* calculate_visibility - Are tree nodes visible
* @param tree Threads tree
* @param max_depth Maximum depth to check
*
* this calculates whether a node is the root of a subtree that has visible
* nodes, whether a node itself is visible, whether, if invisible, it has
* depth anyway, and whether any of its later siblings are roots of visible
* subtrees. while it's at it, it frees the old thread display, so we can
* skip parts of the tree in mutt_draw_tree() if we've decided here that we
* don't care about them any more.
*/
static void calculate_visibility(struct MuttThread *tree, int *max_depth)
{
if (!tree)
return;
struct MuttThread *tmp = NULL;
struct MuttThread *orig_tree = tree;
const bool c_hide_top_missing =
cs_subset_bool(NeoMutt->sub, "hide_top_missing");
const bool c_hide_missing = cs_subset_bool(NeoMutt->sub, "hide_missing");
int hide_top_missing = c_hide_top_missing && !c_hide_missing;
const bool c_hide_top_limited =
cs_subset_bool(NeoMutt->sub, "hide_top_limited");
const bool c_hide_limited = cs_subset_bool(NeoMutt->sub, "hide_limited");
int hide_top_limited = c_hide_top_limited && !c_hide_limited;
int depth = 0;
/* we walk each level backwards to make it easier to compute next_subtree_visible */
while (tree->next)
tree = tree->next;
*max_depth = 0;
while (true)
{
if (depth > *max_depth)
*max_depth = depth;
tree->subtree_visible = 0;
if (tree->message)
{
FREE(&tree->message->tree);
if (is_visible(tree->message))
{
tree->deep = true;
tree->visible = true;
tree->message->display_subject = need_display_subject(tree->message);
for (tmp = tree; tmp; tmp = tmp->parent)
{
if (tmp->subtree_visible)
{
tmp->deep = true;
tmp->subtree_visible = 2;
break;
}
else
tmp->subtree_visible = 1;
}
}
else
{
tree->visible = false;
tree->deep = !c_hide_limited;
}
}
else
{
tree->visible = false;
tree->deep = !c_hide_missing;
}
tree->next_subtree_visible =
tree->next && (tree->next->next_subtree_visible || tree->next->subtree_visible);
if (tree->child)
{
depth++;
tree = tree->child;
while (tree->next)
tree = tree->next;
}
else if (tree->prev)
tree = tree->prev;
else
{
while (tree && !tree->prev)
{
depth--;
tree = tree->parent;
}
if (!tree)
break;
tree = tree->prev;
}
}
/* now fix up for the OPTHIDETOP* options if necessary */
if (hide_top_limited || hide_top_missing)
{
tree = orig_tree;
while (true)
{
if (!tree->visible && tree->deep && (tree->subtree_visible < 2) &&
((tree->message && hide_top_limited) || (!tree->message && hide_top_missing)))
{
tree->deep = false;
}
if (!tree->deep && tree->child && tree->subtree_visible)
tree = tree->child;
else if (tree->next)
tree = tree->next;
else
{
while (tree && !tree->next)
tree = tree->parent;
if (!tree)
break;
tree = tree->next;
}
}
}
}
/**
* mutt_thread_ctx_init - Initialize a threading context
* @param m Current mailbox
* @retval ptr Threading context
*/
struct ThreadsContext *mutt_thread_ctx_init(struct Mailbox *m)
{
struct ThreadsContext *tctx = mutt_mem_calloc(1, sizeof(struct ThreadsContext));
tctx->mailbox = m;
tctx->tree = NULL;
tctx->hash = NULL;
return tctx;
}
/**
* mutt_thread_ctx_free - Finalize a threading context
* @param tctx Threading context to finalize
*/
void mutt_thread_ctx_free(struct ThreadsContext **tctx)
{
(*tctx)->mailbox = NULL;
mutt_hash_free(&(*tctx)->hash);
FREE(tctx);
}
/**
* mutt_draw_tree - Draw a tree of threaded emails
* @param tctx Threading context
*
* Since the graphics characters have a value >255, I have to resort to using
* escape sequences to pass the information to print_enriched_string(). These
* are the macros MUTT_TREE_* defined in mutt.h.
*
* ncurses should automatically use the default ASCII characters instead of
* graphics chars on terminals which don't support them (see the man page for
* curs_addch).
*/
void mutt_draw_tree(struct ThreadsContext *tctx)
{
char *pfx = NULL, *mypfx = NULL, *arrow = NULL, *myarrow = NULL, *new_tree = NULL;
const bool reverse = (mutt_thread_style() == UT_REVERSE);
enum TreeChar corner = reverse ? MUTT_TREE_ULCORNER : MUTT_TREE_LLCORNER;
enum TreeChar vtee = reverse ? MUTT_TREE_BTEE : MUTT_TREE_TTEE;
const bool c_narrow_tree = cs_subset_bool(NeoMutt->sub, "narrow_tree");
int depth = 0, start_depth = 0, max_depth = 0, width = c_narrow_tree ? 1 : 2;
struct MuttThread *nextdisp = NULL, *pseudo = NULL, *parent = NULL;
struct MuttThread *tree = tctx->tree;
/* Do the visibility calculations and free the old thread chars.
* From now on we can simply ignore invisible subtrees */
calculate_visibility(tree, &max_depth);
pfx = mutt_mem_malloc((width * max_depth) + 2);
arrow = mutt_mem_malloc((width * max_depth) + 2);
while (tree)
{
if (depth != 0)
{
myarrow = arrow + (depth - start_depth - ((start_depth != 0) ? 0 : 1)) * width;
const bool c_hide_limited = cs_subset_bool(NeoMutt->sub, "hide_limited");
const bool c_hide_missing = cs_subset_bool(NeoMutt->sub, "hide_missing");
if (start_depth == depth)
myarrow[0] = nextdisp ? MUTT_TREE_LTEE : corner;
else if (parent->message && !c_hide_limited)
myarrow[0] = MUTT_TREE_HIDDEN;
else if (!parent->message && !c_hide_missing)
myarrow[0] = MUTT_TREE_MISSING;
else
myarrow[0] = vtee;
if (width == 2)
{
myarrow[1] = pseudo ? MUTT_TREE_STAR :
(tree->duplicate_thread ? MUTT_TREE_EQUALS : MUTT_TREE_HLINE);
}
if (tree->visible)
{
myarrow[width] = MUTT_TREE_RARROW;
myarrow[width + 1] = 0;
new_tree = mutt_mem_malloc(((size_t) depth * width) + 2);
if (start_depth > 1)
{
strncpy(new_tree, pfx, (size_t) width * (start_depth - 1));
mutt_str_copy(new_tree + (start_depth - 1) * width, arrow,
(1 + depth - start_depth) * width + 2);
}
else
mutt_str_copy(new_tree, arrow, ((size_t) depth * width) + 2);
tree->message->tree = new_tree;
}
}
if (tree->child && (depth != 0))
{
mypfx = pfx + (depth - 1) * width;
mypfx[0] = nextdisp ? MUTT_TREE_VLINE : MUTT_TREE_SPACE;
if (width == 2)
mypfx[1] = MUTT_TREE_SPACE;
}
parent = tree;
nextdisp = NULL;
pseudo = NULL;
do
{
if (tree->child && tree->subtree_visible)
{
if (tree->deep)
depth++;
if (tree->visible)
start_depth = depth;
tree = tree->child;
/* we do this here because we need to make sure that the first child thread
* of the old tree that we deal with is actually displayed if any are,
* or we might set the parent variable wrong while going through it. */
while (!tree->subtree_visible && tree->next)
tree = tree->next;
}
else
{
while (!tree->next && tree->parent)
{
if (tree == pseudo)
pseudo = NULL;
if (tree == nextdisp)
nextdisp = NULL;
if (tree->visible)
start_depth = depth;
tree = tree->parent;
if (tree->deep)
{
if (start_depth == depth)
start_depth--;
depth--;
}
}
if (tree == pseudo)
pseudo = NULL;
if (tree == nextdisp)
nextdisp = NULL;
if (tree->visible)
start_depth = depth;
tree = tree->next;
if (!tree)
break;
}
if (!pseudo && tree->fake_thread)
pseudo = tree;
if (!nextdisp && tree->next_subtree_visible)
nextdisp = tree;
} while (!tree->deep);
}
FREE(&pfx);
FREE(&arrow);
}
/**
* make_subject_list - Create a sorted list of all subjects in a thread
* @param[out] subjects String List of subjects
* @param[in] cur Email Thread
* @param[out] dateptr Earliest date found in thread
*
* Since we may be trying to attach as a pseudo-thread a MuttThread that has no
* message, we have to make a list of all the subjects of its most immediate
* existing descendants.
*/
static void make_subject_list(struct ListHead *subjects, struct MuttThread *cur, time_t *dateptr)
{
struct MuttThread *start = cur;
struct Envelope *env = NULL;
time_t thisdate;
int rc = 0;
while (true)
{
while (!cur->message)
cur = cur->child;
if (dateptr)
{
const bool c_thread_received =
cs_subset_bool(NeoMutt->sub, "thread_received");
thisdate = c_thread_received ? cur->message->received : cur->message->date_sent;
if ((*dateptr == 0) || (thisdate < *dateptr))
*dateptr = thisdate;
}
env = cur->message->env;
const bool c_sort_re = cs_subset_bool(NeoMutt->sub, "sort_re");
if (env->real_subj && ((env->real_subj != env->subject) || !c_sort_re))
{
struct ListNode *np = NULL;
STAILQ_FOREACH(np, subjects, entries)
{
rc = mutt_str_cmp(env->real_subj, np->data);
if (rc >= 0)
break;
}
if (!np)
mutt_list_insert_head(subjects, env->real_subj);
else if (rc > 0)
mutt_list_insert_after(subjects, np, env->real_subj);
}
while (!cur->next && (cur != start))
{
cur = cur->parent;
}
if (cur == start)
break;
cur = cur->next;
}
}
/**
* find_subject - Find the best possible match for a parent based on subject
* @param m Mailbox
* @param cur Email to match
* @retval ptr Best match for a parent
*
* If there are multiple matches, the one which was sent the latest, but before
* the current message, is used.
*/
static struct MuttThread *find_subject(struct Mailbox *m, struct MuttThread *cur)
{
if (!m)
return NULL;
struct HashElem *ptr = NULL;
struct MuttThread *tmp = NULL, *last = NULL;
struct ListHead subjects = STAILQ_HEAD_INITIALIZER(subjects);
time_t date = 0;
make_subject_list(&subjects, cur, &date);
struct ListNode *np = NULL;
STAILQ_FOREACH(np, &subjects, entries)
{
for (ptr = mutt_hash_find_bucket(m->subj_hash, np->data); ptr; ptr = ptr->next)
{
const bool c_thread_received =
cs_subset_bool(NeoMutt->sub, "thread_received");
tmp = ((struct Email *) ptr->data)->thread;
if ((tmp != cur) && /* don't match the same message */
!tmp->fake_thread && /* don't match pseudo threads */
tmp->message->subject_changed && /* only match interesting replies */
!is_descendant(tmp, cur) && /* don't match in the same thread */
(date >= (c_thread_received ? tmp->message->received : tmp->message->date_sent)) &&
(!last || (c_thread_received ?
(last->message->received < tmp->message->received) :
(last->message->date_sent < tmp->message->date_sent))) &&
tmp->message->env->real_subj &&
mutt_str_equal(np->data, tmp->message->env->real_subj))
{
last = tmp; /* best match so far */
}
}
}
mutt_list_clear(&subjects);
return last;
}
/**
* make_subj_hash - Create a Hash Table for the email subjects
* @param m Mailbox
* @retval ptr Newly allocated Hash Table
*/
static struct HashTable *make_subj_hash(struct Mailbox *m)
{
if (!m)
return NULL;
struct HashTable *hash = mutt_hash_new(m->msg_count * 2, MUTT_HASH_ALLOW_DUPS);
for (int i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e || !e->env)
continue;
if (e->env->real_subj)
mutt_hash_insert(hash, e->env->real_subj, e);
}
return hash;
}
/**
* pseudo_threads - Thread messages by subject
* @param tctx Threading context
*
* Thread by subject things that didn't get threaded by message-id
*/
static void pseudo_threads(struct ThreadsContext *tctx)
{
if (!tctx || !tctx->mailbox)
return;
struct Mailbox *m = tctx->mailbox;
struct MuttThread *tree = tctx->tree;
struct MuttThread *top = tree;
struct MuttThread *tmp = NULL, *cur = NULL, *parent = NULL, *curchild = NULL,
*nextchild = NULL;
if (!m->subj_hash)
m->subj_hash = make_subj_hash(m);
while (tree)
{
cur = tree;
tree = tree->next;
parent = find_subject(m, cur);
if (parent)
{
cur->fake_thread = true;
unlink_message(&top, cur);
insert_message(&parent->child, parent, cur);
parent->sort_children = true;
tmp = cur;
while (true)
{
while (!tmp->message)
tmp = tmp->child;
/* if the message we're attaching has pseudo-children, they
* need to be attached to its parent, so move them up a level.
* but only do this if they have the same real subject as the
* parent, since otherwise they rightly belong to the message
* we're attaching. */
if ((tmp == cur) || mutt_str_equal(tmp->message->env->real_subj,
parent->message->env->real_subj))
{
tmp->message->subject_changed = false;
for (curchild = tmp->child; curchild;)
{
nextchild = curchild->next;
if (curchild->fake_thread)
{
unlink_message(&tmp->child, curchild);
insert_message(&parent->child, parent, curchild);
}
curchild = nextchild;
}
}
while (!tmp->next && (tmp != cur))
{
tmp = tmp->parent;
}
if (tmp == cur)
break;
tmp = tmp->next;
}
}
}
tctx->tree = top;
}
/**
* mutt_clear_threads - Clear the threading of message in a mailbox
* @param tctx Threading context
*/
void mutt_clear_threads(struct ThreadsContext *tctx)
{
if (!tctx || !tctx->mailbox || !tctx->mailbox->emails || !tctx->tree)
return;
for (int i = 0; i < tctx->mailbox->msg_count; i++)
{
struct Email *e = tctx->mailbox->emails[i];
if (!e)
break;
/* mailbox may have been only partially read */
e->thread = NULL;
e->threaded = false;
}
tctx->tree = NULL;
mutt_hash_free(&tctx->hash);
}
/**
* compare_threads - qsort_r() function for comparing email threads
* @param a First thread to compare
* @param b Second thread to compare
* @param arg ThreadsContext for how to compare
* @retval <0 a precedes b
* @retval 0 a and b are identical
* @retval >0 b precedes a
*/
static int compare_threads(const void *a, const void *b, void *arg)
{
const struct MuttThread *ta = *(struct MuttThread const *const *) a;
const struct MuttThread *tb = *(struct MuttThread const *const *) b;
const struct ThreadsContext *tctx = arg;
assert(ta->parent == tb->parent);
/* If c_sort ties, remember we are building the thread array in
* reverse from the index the mails had in the mailbox. */
if (ta->parent)
{
return mutt_compare_emails(ta->sort_aux_key, tb->sort_aux_key, mx_type(tctx->mailbox),
tctx->c_sort_aux, SORT_REVERSE | SORT_ORDER);
}
else
{
return mutt_compare_emails(ta->sort_thread_key, tb->sort_thread_key,
mx_type(tctx->mailbox), tctx->c_sort,
SORT_REVERSE | SORT_ORDER);
}
}
/**
* mutt_sort_subthreads - Sort the children of a thread
* @param tctx Threading context
* @param init If true, rebuild the thread
*/
static void mutt_sort_subthreads(struct ThreadsContext *tctx, bool init)
{
struct MuttThread *thread = tctx->tree;
if (!thread)
return;
struct MuttThread **array = NULL, *top = NULL, *tmp = NULL;
struct Email *sort_aux_key = NULL, *oldsort_aux_key = NULL;
struct Email *oldsort_thread_key = NULL;
int i, array_size;
bool sort_top = false;
/* we put things into the array backwards to save some cycles,
* but we want to have to move less stuff around if we're
* resorting, so we sort backwards and then put them back
* in reverse order so they're forwards */
const bool reverse = (mutt_thread_style() == UT_REVERSE);
short c_sort = cs_subset_sort(NeoMutt->sub, "sort");
short c_sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
if ((c_sort & SORT_MASK) == SORT_THREADS)
{
assert(!(c_sort & SORT_REVERSE) != reverse);
assert(cs_subset_enum(NeoMutt->sub, "use_threads") == UT_UNSET);
c_sort = c_sort_aux;
}
c_sort ^= SORT_REVERSE;
c_sort_aux ^= SORT_REVERSE;
if (init || tctx->c_sort != c_sort || tctx->c_sort_aux != c_sort_aux)
{
tctx->c_sort = c_sort;
tctx->c_sort_aux = c_sort_aux;
init = true;
}
top = thread;
array_size = 256;
array = mutt_mem_calloc(array_size, sizeof(struct MuttThread *));
while (true)
{
if (init || !thread->sort_thread_key || !thread->sort_aux_key)
{
thread->sort_thread_key = NULL;
thread->sort_aux_key = NULL;
if (thread->parent)
thread->parent->sort_children = true;
else
sort_top = true;
}
if (thread->child)
{
thread = thread->child;
continue;
}
else
{
/* if it has no children, it must be real. sort it on its own merits */
thread->sort_thread_key = thread->message;
thread->sort_aux_key = thread->message;
if (thread->next)
{
thread = thread->next;
continue;
}
}
while (!thread->next)
{
/* if it has siblings and needs to be sorted, sort it... */
if (thread->prev && (thread->parent ? thread->parent->sort_children : sort_top))
{
/* put them into the array */
for (i = 0; thread; i++, thread = thread->prev)
{
if (i >= array_size)
mutt_mem_realloc(&array, (array_size *= 2) * sizeof(struct MuttThread *));
array[i] = thread;
}
mutt_qsort_r((void *) array, i, sizeof(struct MuttThread *), compare_threads, tctx);
/* attach them back together. make thread the last sibling. */
thread = array[0];
thread->next = NULL;
array[i - 1]->prev = NULL;
if (thread->parent)
thread->parent->child = array[i - 1];
else
top = array[i - 1];
while (--i)
{
array[i - 1]->prev = array[i];
array[i]->next = array[i - 1];
}
}
if (thread->parent)
{
tmp = thread;
thread = thread->parent;
if (!thread->sort_thread_key || !thread->sort_aux_key || thread->sort_children)
{
/* we just sorted its children */
thread->sort_children = false;
oldsort_aux_key = thread->sort_aux_key;
oldsort_thread_key = thread->sort_thread_key;
/* update sort keys. sort_aux_key will be the first or last
* sibling, as appropriate... */
thread->sort_aux_key = thread->message;
sort_aux_key = ((!(c_sort_aux & SORT_LAST)) ^ (!(c_sort_aux & SORT_REVERSE))) ?
thread->child->sort_aux_key :
tmp->sort_aux_key;
if (c_sort_aux & SORT_LAST)
{
if (!thread->sort_aux_key ||
(mutt_compare_emails(thread->sort_aux_key, sort_aux_key, mx_type(tctx->mailbox),
c_sort_aux | SORT_REVERSE, SORT_ORDER) > 0))
{
thread->sort_aux_key = sort_aux_key;
}
}
else if (!thread->sort_aux_key)
thread->sort_aux_key = sort_aux_key;
/* ...but sort_thread_key may require searching the entire
* list of siblings */
if ((c_sort_aux & ~SORT_REVERSE) == (c_sort & ~SORT_REVERSE))
{
thread->sort_thread_key = thread->sort_aux_key;
}
else
{
if (thread->message)
{
thread->sort_thread_key = thread->message;
}
else if (reverse != (!(c_sort_aux & SORT_REVERSE)))
{
thread->sort_thread_key = tmp->sort_thread_key;
}
else
{
thread->sort_thread_key = thread->child->sort_thread_key;
}
if (c_sort & SORT_LAST)
{
for (tmp = thread->child; tmp; tmp = tmp->next)
{
if (tmp->sort_thread_key == thread->sort_thread_key)
continue;
if ((mutt_compare_emails(thread->sort_thread_key, tmp->sort_thread_key,
mx_type(tctx->mailbox),
c_sort | SORT_REVERSE, SORT_ORDER) > 0))
{
thread->sort_thread_key = tmp->sort_thread_key;
}
}
}
}
/* if a sort_key has changed, we need to resort it and siblings */
if ((oldsort_aux_key != thread->sort_aux_key) ||
(oldsort_thread_key != thread->sort_thread_key))
{
if (thread->parent)
thread->parent->sort_children = true;
else
sort_top = true;
}
}
}
else
{
FREE(&array);
tctx->tree = top;
return;
}
}
thread = thread->next;
}
}
/**
* check_subjects - Find out which emails' subjects differ from their parent's
* @param m Mailbox
* @param init If true, rebuild the thread
*/
static void check_subjects(struct Mailbox *m, bool init)
{
if (!m)
return;
for (int i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e || !e->thread)
continue;
if (e->thread->check_subject)
e->thread->check_subject = false;
else if (!init)
continue;
/* figure out which messages have subjects different than their parents' */
struct MuttThread *tmp = e->thread->parent;
while (tmp && !tmp->message)
{
tmp = tmp->parent;
}
if (!tmp)
e->subject_changed = true;
else if (e->env->real_subj && tmp->message->env->real_subj)
{
e->subject_changed = !mutt_str_equal(e->env->real_subj, tmp->message->env->real_subj);
}
else
{
e->subject_changed = (e->env->real_subj || tmp->message->env->real_subj);
}
}
}