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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
|
/**
* @file
* Create/manipulate threading in emails
*
* @authors
* Copyright (C) 2017 Peter Lewis <pete@muddygoat.org>
* Copyright (C) 2017-2023 Richard Russon <rich@flatcap.org>
* Copyright (C) 2017-2023 Pietro Cerutti <gahr@gahr.ch>
* Copyright (C) 2019 Federico Kircheis <federico.kircheis@gmail.com>
* Copyright (C) 2021 Eric Blake <eblake@redhat.com>
*
* @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 <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 "globals.h"
#include "mview.h"
#include "mx.h"
#include "protos.h"
/**
* 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
};
/// Data for the $use_threads enumeration
const 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 enum EmailSortType c_sort = cs_subset_sort(NeoMutt->sub, "sort");
if (c_use_threads > UT_FLAT)
return c_use_threads;
if ((c_sort & SORT_MASK) != EMAIL_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 the "sort" config variable - Implements ConfigDef::validator() - @ingroup cfg_def_validator
*/
int sort_validator(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
{
if (((value & SORT_MASK) == EMAIL_SORT_THREADS) && (value & SORT_LAST))
{
buf_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_view)
return;
struct Mailbox *m = tctx->mailbox_view->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 mv Mailbox view
* @retval ptr Threading context
*/
struct ThreadsContext *mutt_thread_ctx_init(struct MailboxView *mv)
{
struct ThreadsContext *tctx = MUTT_MEM_CALLOC(1, struct ThreadsContext);
tctx->mailbox_view = mv;
return tctx;
}
/**
* mutt_thread_ctx_free - Finalize a threading context
* @param ptr Threading context to free
*/
void mutt_thread_ctx_free(struct ThreadsContext **ptr)
{
if (!ptr || !*ptr)
{
return;
}
struct ThreadsContext *tctx = *ptr;
mutt_hash_free(&tctx->hash);
FREE(ptr);
}
/**
* 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, char);
arrow = MUTT_MEM_MALLOC((width * max_depth) + 2, char);
const bool c_hide_limited = cs_subset_bool(NeoMutt->sub, "hide_limited");
const bool c_hide_missing = cs_subset_bool(NeoMutt->sub, "hide_missing");
while (tree)
{
if (depth != 0)
{
myarrow = arrow + (depth - start_depth - ((start_depth != 0) ? 0 : 1)) * width;
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, char);
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;
const bool c_thread_received = cs_subset_bool(NeoMutt->sub, "thread_received");
const bool c_sort_re = cs_subset_bool(NeoMutt->sub, "sort_re");
while (true)
{
while (!cur->message)
cur = cur->child;
if (dateptr)
{
thisdate = c_thread_received ? cur->message->received : cur->message->date_sent;
if ((*dateptr == 0) || (thisdate < *dateptr))
*dateptr = thisdate;
}
env = cur->message->env;
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 *he = 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;
const bool c_thread_received = cs_subset_bool(NeoMutt->sub, "thread_received");
STAILQ_FOREACH(np, &subjects, entries)
{
for (he = mutt_hash_find_bucket(m->subj_hash, np->data); he; he = he->next)
{
tmp = ((struct Email *) he->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_view)
return;
struct Mailbox *m = tctx->mailbox_view->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->tree)
return;
struct MailboxView *mv = tctx->mailbox_view;
if (!mv)
return;
struct Mailbox *m = mv->mailbox;
if (!m || !m->emails)
return;
for (int i = 0; i < m->msg_count; i++)
{
struct Email *e = m->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 - Helper to sort email threads - Implements ::sort_t - @ingroup sort_api
*/
static int compare_threads(const void *a, const void *b, void *sdata)
{
const struct MuttThread *ta = *(struct MuttThread const *const *) a;
const struct MuttThread *tb = *(struct MuttThread const *const *) b;
const struct ThreadsContext *tctx = sdata;
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. */
struct Mailbox *m = tctx->mailbox_view->mailbox;
const enum MailboxType mtype = mx_type(m);
if (ta->parent)
{
return mutt_compare_emails(ta->sort_aux_key, tb->sort_aux_key, mtype,
tctx->c_sort_aux, SORT_REVERSE | EMAIL_SORT_UNSORTED);
}
else
{
return mutt_compare_emails(ta->sort_thread_key, tb->sort_thread_key, mtype,
tctx->c_sort, SORT_REVERSE | EMAIL_SORT_UNSORTED);
}
}
/**
* 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);
enum EmailSortType c_sort = cs_subset_sort(NeoMutt->sub, "sort");
enum EmailSortType c_sort_aux = cs_subset_sort(NeoMutt->sub, "sort_aux");
if ((c_sort & SORT_MASK) == EMAIL_SORT_THREADS)
{
ASSERT(!(c_sort & SORT_REVERSE) != reverse);
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, 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;
}
}
struct Mailbox *m = tctx->mailbox_view->mailbox;
const enum MailboxType mtype = mx_type(m);
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)
{
array_size *= 2;
MUTT_MEM_REALLOC(&array, array_size, 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, mtype,
c_sort_aux | SORT_REVERSE, EMAIL_SORT_UNSORTED) > 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, mtype,
c_sort | SORT_REVERSE, EMAIL_SORT_UNSORTED) > 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 mv Mailbox View
* @param init If true, rebuild the thread
*/
static void check_subjects(struct MailboxView *mv, bool init)
{
if (!mv)
return;
struct Mailbox *m = mv->mailbox;
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);
}
}
}
/**
* thread_hash_destructor - Free our hash table data - Implements ::hash_hdata_free_t - @ingroup hash_hdata_free_api
*/
static void thread_hash_destructor(int type, void *obj, intptr_t data)
{
FREE(&obj);
}
/**
* mutt_sort_threads - Sort email threads
* @param tctx Threading context
* @param init If true, rebuild the thread
*/
void mutt_sort_threads(struct ThreadsContext *tctx, bool init)
{
if (!tctx || !tctx->mailbox_view)
return;
struct MailboxView *mv = tctx->mailbox_view;
struct Mailbox *m = mv->mailbox;
struct Email *e = NULL;
int i, using_refs = 0;
struct MuttThread *thread = NULL, *tnew = NULL, *tmp = NULL;
struct MuttThread top = { 0 };
struct ListNode *ref = NULL;
ASSERT(m->msg_count > 0);
if (!tctx->hash)
init = true;
if (init)
{
tctx->hash = mutt_hash_new(m->msg_count * 2, MUTT_HASH_ALLOW_DUPS);
mutt_hash_set_destructor(tctx->hash, thread_hash_destructor, 0);
}
/* we want a quick way to see if things are actually attached to the top of the
* thread tree or if they're just dangling, so we attach everything to a top
* node temporarily */
top.parent = NULL;
top.next = NULL;
top.prev = NULL;
top.child = tctx->tree;
for (thread = tctx->tree; thread; thread = thread->next)
thread->parent = ⊤
/* put each new message together with the matching messageless MuttThread if it
* exists. otherwise, if there is a MuttThread that already has a message, thread
* new message as an identical child. if we didn't attach the message to a
* MuttThread, make a new one for it. */
const bool c_duplicate_threads = cs_subset_bool(NeoMutt->sub, "duplicate_threads");
for (i = 0; i < m->msg_count; i++)
{
e = m->emails[i];
if (!e)
continue;
if (e->thread)
{
/* unlink pseudo-threads because they might be children of newly
* arrived messages */
thread = e->thread;
for (tnew = thread->child; tnew;)
{
tmp = tnew->next;
if (tnew->fake_thread)
{
unlink_message(&thread->child, tnew);
insert_message(&top.child, &top, tnew);
tnew->fake_thread = false;
}
tnew = tmp;
}
}
else
{
if ((!init || c_duplicate_threads) && e->env->message_id)
thread = mutt_hash_find(tctx->hash, e->env->message_id);
else
thread = NULL;
if (thread && !thread->message)
{
/* this is a message which was missing before */
thread->message = e;
e->thread = thread;
thread->check_subject = true;
/* mark descendants as needing subject_changed checked */
for (tmp = (thread->child ? thread->child : thread); tmp != thread;)
{
while (!tmp->message)
tmp = tmp->child;
tmp->check_subject = true;
while (!tmp->next && (tmp != thread))
tmp = tmp->parent;
if (tmp != thread)
tmp = tmp->next;
}
if (thread->parent)
{
/* remove threading info above it based on its children, which we'll
* recalculate based on its headers. make sure not to leave
* dangling missing messages. note that we haven't kept track
* of what info came from its children and what from its siblings'
* children, so we just remove the stuff that's definitely from it */
do
{
tmp = thread->parent;
unlink_message(&tmp->child, thread);
thread->parent = NULL;
thread->sort_thread_key = NULL;
thread->sort_aux_key = NULL;
thread->fake_thread = false;
thread = tmp;
} while (thread != &top && !thread->child && !thread->message);
}
}
else
{
tnew = (c_duplicate_threads ? thread : NULL);
thread = MUTT_MEM_CALLOC(1, struct MuttThread);
thread->message = e;
thread->check_subject = true;
e->thread = thread;
mutt_hash_insert(tctx->hash, e->env->message_id ? e->env->message_id : "", thread);
if (tnew)
{
if (tnew->duplicate_thread)
tnew = tnew->parent;
thread = e->thread;
insert_message(&tnew->child, tnew, thread);
thread->duplicate_thread = true;
thread->message->threaded = true;
}
}
}
}
/* thread by references */
for (i = 0; i < m->msg_count; i++)
{
e = m->emails[i];
if (!e)
break;
if (e->threaded)
continue;
e->threaded = true;
thread = e->thread;
if (!thread)
continue;
using_refs = 0;
while (true)
{
if (using_refs == 0)
{
/* look at the beginning of in-reply-to: */
ref = STAILQ_FIRST(&e->env->in_reply_to);
if (ref)
{
using_refs = 1;
}
else
{
ref = STAILQ_FIRST(&e->env->references);
using_refs = 2;
}
}
else if (using_refs == 1)
{
/* if there's no references header, use all the in-reply-to:
* data that we have. otherwise, use the first reference
* if it's different than the first in-reply-to, otherwise use
* the second reference (since at least eudora puts the most
* recent reference in in-reply-to and the rest in references) */
if (STAILQ_EMPTY(&e->env->references))
{
ref = STAILQ_NEXT(ref, entries);
}
else
{
if (!mutt_str_equal(ref->data, STAILQ_FIRST(&e->env->references)->data))
ref = STAILQ_FIRST(&e->env->references);
else
ref = STAILQ_NEXT(STAILQ_FIRST(&e->env->references), entries);
using_refs = 2;
}
}
else
{
ref = STAILQ_NEXT(ref, entries); /* go on with references */
}
if (!ref)
break;
tnew = mutt_hash_find(tctx->hash, ref->data);
if (tnew)
{
if (tnew->duplicate_thread)
tnew = tnew->parent;
if (is_descendant(tnew, thread)) /* no loops! */
continue;
}
else
{
tnew = MUTT_MEM_CALLOC(1, struct MuttThread);
mutt_hash_insert(tctx->hash, ref->data, tnew);
}
if (thread->parent)
unlink_message(&top.child, thread);
insert_message(&tnew->child, tnew, thread);
thread = tnew;
if (thread->message || (thread->parent && (thread->parent != &top)))
break;
}
if (!thread->parent)
insert_message(&top.child, &top, thread);
}
/* detach everything from the temporary top node */
for (thread = top.child; thread; thread = thread->next)
{
thread->parent = NULL;
}
tctx->tree = top.child;
check_subjects(mv, init);
const bool c_strict_threads = cs_subset_bool(NeoMutt->sub, "strict_threads");
if (!c_strict_threads)
pseudo_threads(tctx);
/* if $sort_aux or similar changed after the mailbox is sorted, then
* all the subthreads need to be resorted */
if (tctx->tree)
{
mutt_sort_subthreads(tctx, init || OptSortSubthreads);
OptSortSubthreads = false;
/* Put the list into an array. */
linearize_tree(tctx);
/* Draw the thread tree. */
mutt_draw_tree(tctx);
}
}
/**
* mutt_aside_thread - Find the next/previous (sub)thread
* @param e Search from this Email
* @param forwards Direction to search: 'true' forwards, 'false' backwards
* @param subthreads Search subthreads: 'true' subthread, 'false' not
* @retval num Index into the virtual email table
* @retval -1 Error
*/
int mutt_aside_thread(struct Email *e, bool forwards, bool subthreads)
{
if (!e)
return -1;
struct MuttThread *cur = NULL;
struct Email *e_tmp = NULL;
const enum UseThreads threaded = mutt_thread_style();
if (threaded == UT_FLAT)
{
mutt_error(_("Threading is not enabled"));
return e->vnum;
}
cur = e->thread;
if (subthreads)
{
if (forwards ^ (threaded == UT_REVERSE))
{
while (!cur->next && cur->parent)
cur = cur->parent;
}
else
{
while (!cur->prev && cur->parent)
cur = cur->parent;
}
}
else
{
while (cur->parent)
cur = cur->parent;
}
if (forwards ^ (threaded == UT_REVERSE))
{
do
{
cur = cur->next;
if (!cur)
return -1;
e_tmp = find_virtual(cur, false);
} while (!e_tmp);
}
else
{
do
{
cur = cur->prev;
if (!cur)
return -1;
e_tmp = find_virtual(cur, true);
} while (!e_tmp);
}
return e_tmp->vnum;
}
/**
* mutt_parent_message - Find the parent of a message
* @param e Current Email
* @param find_root If true, find the root message
* @retval >=0 Virtual index number of parent/root message
* @retval -1 Error
*/
int mutt_parent_message(struct Email *e, bool find_root)
{
if (!e)
return -1;
struct MuttThread *thread = NULL;
struct Email *e_parent = NULL;
if (!mutt_using_threads())
{
mutt_error(_("Threading is not enabled"));
return e->vnum;
}
/* Root may be the current message */
if (find_root)
e_parent = e;
for (thread = e->thread->parent; thread; thread = thread->parent)
{
e = thread->message;
if (e)
{
e_parent = e;
if (!find_root)
break;
}
}
if (!e_parent)
{
mutt_error(_("Parent message is not available"));
return -1;
}
if (!is_visible(e_parent))
{
if (find_root)
mutt_error(_("Root message is not visible in this limited view"));
else
mutt_error(_("Parent message is not visible in this limited view"));
return -1;
}
return e_parent->vnum;
}
/**
* mutt_set_vnum - Set the virtual index number of all the messages in a mailbox
* @param m Mailbox
* @retval num Size in bytes of all messages shown
*/
off_t mutt_set_vnum(struct Mailbox *m)
{
if (!m)
return 0;
off_t vsize = 0;
const int padding = mx_msg_padding_size(m);
m->vcount = 0;
for (int i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e)
break;
if (e->vnum >= 0)
{
e->vnum = m->vcount;
m->v2r[m->vcount] = i;
m->vcount++;
vsize += e->body->length + e->body->offset - e->body->hdr_offset + padding;
}
}
return vsize;
}
/**
* mutt_traverse_thread - Recurse through an email thread, matching messages
* @param e_cur Current Email
* @param flag Flag to set, see #MuttThreadFlags
* @retval num Number of matches
*/
int mutt_traverse_thread(struct Email *e_cur, MuttThreadFlags flag)
{
struct MuttThread *thread = NULL, *top = NULL;
struct Email *e_root = NULL;
const enum UseThreads threaded = mutt_thread_style();
int final, reverse = (threaded == UT_REVERSE), minmsgno;
int num_hidden = 0, new_mail = 0, old_mail = 0;
bool flagged = false;
int min_unread_msgno = INT_MAX, min_unread = e_cur->vnum;
if (threaded == UT_FLAT)
{
mutt_error(_("Threading is not enabled"));
return e_cur->vnum;
}
if (!e_cur->thread)
{
return e_cur->vnum;
}
final = e_cur->vnum;
thread = e_cur->thread;
while (thread->parent)
thread = thread->parent;
top = thread;
while (!thread->message)
thread = thread->child;
e_cur = thread->message;
minmsgno = e_cur->msgno;
if (!e_cur->read && e_cur->visible)
{
if (e_cur->old)
old_mail = 2;
else
new_mail = 1;
if (e_cur->msgno < min_unread_msgno)
{
min_unread = e_cur->vnum;
min_unread_msgno = e_cur->msgno;
}
}
if (e_cur->flagged && e_cur->visible)
flagged = true;
if ((e_cur->vnum == -1) && e_cur->visible)
num_hidden++;
if (flag & (MUTT_THREAD_COLLAPSE | MUTT_THREAD_UNCOLLAPSE))
{
e_cur->attr_color = NULL; /* force index entry's color to be re-evaluated */
e_cur->collapsed = flag & MUTT_THREAD_COLLAPSE;
if (e_cur->vnum != -1)
{
e_root = e_cur;
if (flag & MUTT_THREAD_COLLAPSE)
final = e_root->vnum;
}
}
if ((thread == top) && !(thread = thread->child))
{
/* return value depends on action requested */
if (flag & (MUTT_THREAD_COLLAPSE | MUTT_THREAD_UNCOLLAPSE))
{
e_cur->num_hidden = num_hidden;
return final;
}
if (flag & MUTT_THREAD_UNREAD)
return (old_mail && new_mail) ? new_mail : (old_mail ? old_mail : new_mail);
if (flag & MUTT_THREAD_NEXT_UNREAD)
return min_unread;
if (flag & MUTT_THREAD_FLAGGED)
return flagged;
}
while (true)
{
e_cur = thread->message;
if (e_cur)
{
if (flag & (MUTT_THREAD_COLLAPSE | MUTT_THREAD_UNCOLLAPSE))
{
e_cur->attr_color = NULL; /* force index entry's color to be re-evaluated */
e_cur->collapsed = flag & MUTT_THREAD_COLLAPSE;
if (!e_root && e_cur->visible)
{
e_root = e_cur;
if (flag & MUTT_THREAD_COLLAPSE)
final = e_root->vnum;
}
if (reverse && (flag & MUTT_THREAD_COLLAPSE) &&
(e_cur->msgno < minmsgno) && e_cur->visible)
{
minmsgno = e_cur->msgno;
final = e_cur->vnum;
}
if (flag & MUTT_THREAD_COLLAPSE)
{
if (e_cur != e_root)
e_cur->vnum = -1;
}
else
{
if (e_cur->visible)
e_cur->vnum = e_cur->msgno;
}
}
if (!e_cur->read && e_cur->visible)
{
if (e_cur->old)
old_mail = 2;
else
new_mail = 1;
if (e_cur->msgno < min_unread_msgno)
{
min_unread = e_cur->vnum;
min_unread_msgno = e_cur->msgno;
}
}
if (e_cur->flagged && e_cur->visible)
flagged = true;
if ((e_cur->vnum == -1) && e_cur->visible)
num_hidden++;
}
if (thread->child)
{
thread = thread->child;
}
else if (thread->next)
{
thread = thread->next;
}
else
{
bool done = false;
while (!thread->next)
{
thread = thread->parent;
if (thread == top)
{
done = true;
break;
}
}
if (done)
break;
thread = thread->next;
}
}
/* re-traverse the thread and store num_hidden in all headers, with or
* without a virtual index. this will allow ~v to match all collapsed
* messages when switching sort order to non-threaded. */
if (flag & MUTT_THREAD_COLLAPSE)
{
thread = top;
while (true)
{
e_cur = thread->message;
if (e_cur)
e_cur->num_hidden = num_hidden + 1;
if (thread->child)
{
thread = thread->child;
}
else if (thread->next)
{
thread = thread->next;
}
else
{
bool done = false;
while (!thread->next)
{
thread = thread->parent;
if (thread == top)
{
done = true;
break;
}
}
if (done)
break;
thread = thread->next;
}
}
}
/* return value depends on action requested */
if (flag & (MUTT_THREAD_COLLAPSE | MUTT_THREAD_UNCOLLAPSE))
return final;
if (flag & MUTT_THREAD_UNREAD)
return (old_mail && new_mail) ? new_mail : (old_mail ? old_mail : new_mail);
if (flag & MUTT_THREAD_NEXT_UNREAD)
return min_unread;
if (flag & MUTT_THREAD_FLAGGED)
return flagged;
return 0;
}
/**
* mutt_messages_in_thread - Count the messages in a thread
* @param m Mailbox
* @param e Email
* @param mit Flag, e.g. #MIT_NUM_MESSAGES
* @retval num Number of message / Our position
*/
int mutt_messages_in_thread(struct Mailbox *m, struct Email *e, enum MessageInThread mit)
{
if (!m || !e)
return 1;
struct MuttThread *threads[2];
int rc;
const enum UseThreads threaded = mutt_thread_style();
if ((threaded == UT_FLAT) || !e->thread)
return 1;
threads[0] = e->thread;
while (threads[0]->parent)
threads[0] = threads[0]->parent;
threads[1] = (mit == MIT_POSITION) ? e->thread : threads[0]->next;
for (int i = 0; i < (((mit == MIT_POSITION) || !threads[1]) ? 1 : 2); i++)
{
while (!threads[i]->message)
threads[i] = threads[i]->child;
}
if (threaded == UT_REVERSE)
{
rc = threads[0]->message->msgno - (threads[1] ? threads[1]->message->msgno : -1);
}
else
{
rc = (threads[1] ? threads[1]->message->msgno : m->msg_count) -
threads[0]->message->msgno;
}
if (mit == MIT_POSITION)
rc += 1;
return rc;
}
/**
* mutt_make_id_hash - Create a Hash Table for message-ids
* @param m Mailbox
* @retval ptr Newly allocated Hash Table
*/
struct HashTable *mutt_make_id_hash(struct Mailbox *m)
{
struct HashTable *hash = mutt_hash_new(m->msg_count * 2, MUTT_HASH_NO_FLAGS);
for (int i = 0; i < m->msg_count; i++)
{
struct Email *e = m->emails[i];
if (!e || !e->env)
continue;
if (e->env->message_id)
mutt_hash_insert(hash, e->env->message_id, e);
}
return hash;
}
/**
* link_threads - Forcibly link messages together
* @param parent Parent Email
* @param child Child Email
* @param m Mailbox
* @retval true On success
*/
static bool link_threads(struct Email *parent, struct Email *child, struct Mailbox *m)
{
if (child == parent)
return false;
mutt_break_thread(child);
mutt_list_insert_head(&child->env->in_reply_to, mutt_str_dup(parent->env->message_id));
mutt_set_flag(m, child, MUTT_TAG, false, true);
child->changed = true;
child->env->changed |= MUTT_ENV_CHANGED_IRT;
return true;
}
/**
* mutt_link_threads - Forcibly link threads together
* @param parent Parent Email
* @param children Array of children Emails
* @param m Mailbox
* @retval true On success
*/
bool mutt_link_threads(struct Email *parent, struct EmailArray *children, struct Mailbox *m)
{
if (!parent || !children || !m)
return false;
bool changed = false;
struct Email **ep = NULL;
ARRAY_FOREACH(ep, children)
{
struct Email *e = *ep;
changed |= link_threads(parent, e, m);
}
return changed;
}
/**
* mutt_thread_collapse_collapsed - Re-collapse threads marked as collapsed
* @param tctx Threading context
*/
void mutt_thread_collapse_collapsed(struct ThreadsContext *tctx)
{
struct MuttThread *thread = NULL;
struct MuttThread *top = tctx->tree;
while ((thread = top))
{
while (!thread->message)
thread = thread->child;
struct Email *e = thread->message;
if (e->collapsed)
mutt_collapse_thread(e);
top = top->next;
}
}
/**
* mutt_thread_collapse - Toggle collapse
* @param tctx Threading context
* @param collapse Collapse / uncollapse
*/
void mutt_thread_collapse(struct ThreadsContext *tctx, bool collapse)
{
struct MuttThread *thread = NULL;
struct MuttThread *top = tctx->tree;
while ((thread = top))
{
while (!thread->message)
thread = thread->child;
struct Email *e = thread->message;
if (e->collapsed != collapse)
{
if (e->collapsed)
mutt_uncollapse_thread(e);
else if (mutt_thread_can_collapse(e))
mutt_collapse_thread(e);
}
top = top->next;
}
}
/**
* mutt_thread_can_collapse - Check whether a thread can be collapsed
* @param e Head of the thread
* @retval true Can be collapsed
* @retval false Cannot be collapsed
*/
bool mutt_thread_can_collapse(struct Email *e)
{
const bool c_collapse_flagged = cs_subset_bool(NeoMutt->sub, "collapse_flagged");
const bool c_collapse_unread = cs_subset_bool(NeoMutt->sub, "collapse_unread");
return (c_collapse_unread || !mutt_thread_contains_unread(e)) &&
(c_collapse_flagged || !mutt_thread_contains_flagged(e));
}
|