unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob 05d9153a31de5a8fcf4790f7103f43c39440c7dc 276470 bytes (raw)
name: lisp/allout.el 	 # note: path name is non-authoritative(*)

   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
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
 
;;; allout.el --- extensive outline mode for use alone and with other modes

;; Copyright (C) 1992-1994, 2001-2020 Free Software Foundation, Inc.

;; Author: Ken Manheimer <ken dot manheimer at gmail...>
;; Created: Dec 1991 -- first release to usenet
;; Version: 2.3
;; Keywords: outlines, wp, languages, PGP, GnuPG
;; Website: http://myriadicity.net/software-and-systems/craft/emacs-allout

;; This file is part of GNU Emacs.

;; GNU Emacs 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 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs 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 GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Allout outline minor mode provides extensive outline formatting and
;; and manipulation beyond standard emacs outline mode.  Some features:
;;
;;  - Classic outline-mode topic-oriented navigation and exposure adjustment
;;  - Topic-oriented editing including coherent topic and subtopic
;;    creation, promotion, demotion, cut/paste across depths, etc.
;;  - Incremental search with dynamic exposure and reconcealment of text
;;  - Customizable bullet format -- enables programming-language specific
;;    outlining, for code-folding editing.  (Allout code itself is to try it;
;;    formatted as an outline -- do ESC-x eval-buffer in allout.el; but
;;    emacs local file variables need to be enabled when the
;;    file was visited -- see `enable-local-variables'.)
;;  - Configurable per-file initial exposure settings
;;  - Symmetric-key and key-pair topic encryption.  Encryption is via the
;;    Emacs 'epg' library.  See allout-toggle-current-subtree-encryption
;;    docstring.
;;  - Automatic topic-number maintenance
;;  - "Hot-spot" operation, for single-keystroke maneuvering and
;;    exposure control (see the allout-mode docstring)
;;  - Easy rendering of exposed portions into numbered, latex, indented, etc
;;    outline styles
;;  - Careful attention to whitespace -- enabling blank lines between items
;;    and maintenance of hanging indentation (in paragraph auto-fill and
;;    across topic promotion and demotion) of topic bodies consistent with
;;    indentation of their topic header.
;;
;; and more.
;;
;; See the `allout-mode' function's docstring for an introduction to the
;; mode.
;;
;; Directions to the latest development version and helpful notes are
;; available at http://myriadicity.net/Sundry/EmacsAllout .
;;
;; The outline menubar additions provide quick reference to many of the
;; features.  See the docstring of the variables `allout-layout' and
;; `allout-auto-activation' for details on automatic activation of
;; `allout-mode' as a minor mode.  (`allout-init' is deprecated in favor of
;; a purely customization-based method.)
;;
;; Note -- the lines beginning with `;;;_' are outline topic headers.
;;        Customize `allout-auto-activation' to enable, then revisit this
;;        buffer to give it a whirl.

;; ken manheimer (ken dot manheimer at gmail dot com)

;;; Code:

(declare-function epa-passphrase-callback-function
		  "epa" (context key-id handback))

;;;_* Dependency loads
(require 'overlay)
(eval-when-compile (require 'cl-lib))

;;;_* USER CUSTOMIZATION VARIABLES:

;;;_ > defgroup allout, allout-keybindings
(defgroup allout nil
  "Extensive outline minor-mode, for use stand-alone and with other modes.

See Allout Auto Activation for automatic activation."
  :prefix "allout-"
  :group 'outlines)
(defgroup allout-keybindings nil
  "Allout outline mode keyboard bindings configuration."
  :group 'allout)

;;;_ + Layout, Mode, and Topic Header Configuration

(defvar allout-command-prefix)          ; defined below

;;;_  > allout-keybindings incidentals:
;;;_   : internal key binding stuff - in this section for load-order.
;;;_    = allout-mode-map
(defvar allout-mode-map 'allout-mode-map
  "Keybindings place-holder for (allout) outline minor mode.

Do NOT set the value of this variable.  Instead, customize
`allout-command-prefix', `allout-prefixed-keybindings', and
`allout-unprefixed-keybindings'.")
;;;_    = allout-mode-map-value
(defvar allout-mode-map-value nil
  "Keymap for allout outline minor mode.

Do NOT set the value of this variable.  Instead, customize
`allout-command-prefix', `allout-prefixed-keybindings', and
`allout-unprefixed-keybindings'.")
;;;_    = make allout-mode-map-value an alias for allout-mode-map:
;; this needs to be revised when the value is changed, sigh.
(defalias 'allout-mode-map allout-mode-map-value)
;;;_   > allout-compose-and-institute-keymap (&optional varname value)
(defun allout-compose-and-institute-keymap (&optional varname value)
  "Create the allout keymap according to the keybinding specs, and set it.

Useful standalone or to effect customizations of the
respective allout-mode keybinding variables, `allout-command-prefix',
`allout-prefixed-keybindings', and `allout-unprefixed-keybindings'"
  ;; Set the customization variable, if any:
  (when varname
    (set-default varname value))
  (let ((map (make-sparse-keymap)))
    (when (boundp 'allout-prefixed-keybindings)
      ;; tolerate first definitions of the variables:
      (dolist (entry allout-prefixed-keybindings)
        (define-key map
          ;; XXX vector vs non-vector key descriptions?
          (vconcat allout-command-prefix
                   (car (read-from-string (car entry))))
          (cadr entry))))
    (when (boundp 'allout-unprefixed-keybindings)
      (dolist (entry allout-unprefixed-keybindings)
        (define-key map (car (read-from-string (car entry))) (cadr entry))))
    (substitute-key-definition 'beginning-of-line 'allout-beginning-of-line
                               map global-map)
    (substitute-key-definition 'move-beginning-of-line 'allout-beginning-of-line
                               map global-map)
    (substitute-key-definition 'end-of-line 'allout-end-of-line
                               map global-map)
    (substitute-key-definition 'move-end-of-line 'allout-end-of-line
                               map global-map)
    (allout-institute-keymap map)))
;;;_  > allout-institute-keymap (map)
(defun allout-institute-keymap (map)
  "Associate allout-mode bindings with allout as a minor mode."
  ;; Architecture:
  ;; allout-mode-map var is a keymap by virtue of being a defalias for
  ;; allout-mode-map-value, which has the actual keymap value.
  ;; allout-mode-map's symbol value is just 'allout-mode-map, so it can be
  ;; used in minor-mode-map-alist to indirect to the actual
  ;; allout-mode-map-var value, which can be adjusted and reassigned.

  ;; allout-mode-map-value for keymap reference in various places:
  (setq allout-mode-map-value map)
  ;; the function value keymap of allout-mode-map is used in
  ;; minor-mode-map-alist - update it:
  (fset allout-mode-map allout-mode-map-value))
;;;_  * initialize the mode map:
;; ensure that allout-mode-map has some setting even if allout-mode hasn't
;; been invoked:
(allout-compose-and-institute-keymap)
;;;_   = allout-command-prefix
(defcustom allout-command-prefix "\C-c "
  "Key sequence to be used as prefix for outline mode command key bindings.

Default is `\C-c<space>'; just `\C-c' is more short-and-sweet, if you're
willing to let allout use a bunch of \C-c keybindings."
  :type 'string
  :group 'allout-keybindings
  :set 'allout-compose-and-institute-keymap)
;;;_   = allout-keybindings-binding
(define-widget 'allout-keybindings-binding 'lazy
  "Structure of allout keybindings customization items."
  :type '(repeat
          (list (string :tag "Key" :value "[(meta control shift ?f)]")
                (function :tag "Function name"
                          :value allout-forward-current-level))))
;;;_   = allout-prefixed-keybindings
(defcustom allout-prefixed-keybindings
  '(("[(control ?n)]" allout-next-visible-heading)
    ("[(control ?p)]" allout-previous-visible-heading)
    ("[(control ?u)]" allout-up-current-level)
    ("[(control ?f)]" allout-forward-current-level)
    ("[(control ?b)]" allout-backward-current-level)
    ("[(control ?a)]" allout-beginning-of-current-entry)
    ("[(control ?e)]" allout-end-of-entry)
    ("[(control ?i)]" allout-show-children)
    ("[(control ?s)]" allout-show-current-subtree)
    ("[(control ?t)]" allout-toggle-current-subtree-exposure)
;; Let user customize if they want to preempt describe-prefix-bindings ^h use.
;;    ("[(control ?h)]" allout-hide-current-subtree)
    ("[?h]" allout-hide-current-subtree)
    ("[(control ?o)]" allout-show-current-entry)
    ("[?!]" allout-show-all)
    ("[?x]" allout-toggle-current-subtree-encryption)
    ("[? ]" allout-open-sibtopic)
    ("[?.]" allout-open-subtopic)
    ("[?,]" allout-open-supertopic)
    ("[?']" allout-shift-in)
    ("[?>]" allout-shift-in)
    ("[?<]" allout-shift-out)
    ("[(control ?m)]" allout-rebullet-topic)
    ("[?*]" allout-rebullet-current-heading)
    ("[?#]" allout-number-siblings)
    ("[(control ?k)]" allout-kill-topic)
    ("[(meta ?k)]" allout-copy-topic-as-kill)
    ("[?@]" allout-resolve-xref)
    ("[?=?c]" allout-copy-exposed-to-buffer)
    ("[?=?i]" allout-indented-exposed-to-buffer)
    ("[?=?t]" allout-latexify-exposed)
    ("[?=?p]" allout-flatten-exposed-to-buffer)
    )
  "Allout-mode key bindings that are prefixed with `allout-command-prefix'.

See `allout-unprefixed-keybindings' for the list of keybindings
that are not prefixed.

Use vector format for the keys:
  - put literal keys after a `?' question mark, eg: `?a', `?.'
  - enclose control, shift, or meta-modified keys as sequences within
    parentheses, with the literal key, as above, preceded by the name(s)
    of the modifiers, eg: [(control ?a)]
See the existing keys for examples.

Functions can be bound to multiple keys, but binding keys to
multiple functions will not work - the last binding for a key
prevails."
  :version "24.1"
  :type 'allout-keybindings-binding
  :group 'allout-keybindings
  :set 'allout-compose-and-institute-keymap
 )
;;;_   = allout-unprefixed-keybindings
(defcustom allout-unprefixed-keybindings
  '(("[(control ?k)]" allout-kill-line)
    ("[(meta ?k)]" allout-copy-line-as-kill)
    ("[(control ?y)]" allout-yank)
    ("[(meta ?y)]" allout-yank-pop)
    )
  "Allout-mode functions bound to keys without any added prefix.

This is in contrast to the majority of allout-mode bindings on
`allout-prefixed-bindings', whose bindings are created with a
preceding command key.

Use vector format for the keys:
  - put literal keys after a `?' question mark, eg: `?a', `?.'
  - enclose control, shift, or meta-modified keys as sequences within
    parentheses, with the literal key, as above, preceded by the name(s)
    of the modifiers, eg: [(control ?a)]
See the existing keys for examples."
  :version "24.1"
  :type 'allout-keybindings-binding
  :group 'allout-keybindings
  :set 'allout-compose-and-institute-keymap
  )

;;;_  > allout-auto-activation-helper (var value)
;;;###autoload
(defun allout-auto-activation-helper (var value)
  "Institute `allout-auto-activation'.

Intended to be used as the `allout-auto-activation' :set function."
  (set-default var value)
  (allout-setup))
;;;_  > allout-setup ()
;;;###autoload
(defun allout-setup ()
  "Do fundamental Emacs session for allout auto-activation.

Establishes allout processing as part of visiting a file if
`allout-auto-activation' is non-nil, or removes it otherwise.

The proper way to use this is through customizing the setting of
`allout-auto-activation'."
  (if (not allout-auto-activation)
      (remove-hook 'find-file-hook 'allout-find-file-hook)
      (add-hook 'find-file-hook 'allout-find-file-hook)))
;;;_  = allout-auto-activation
;;;###autoload
(defcustom allout-auto-activation nil
  "Configure allout outline mode auto-activation.

Control whether and how allout outline mode is automatically
activated when files are visited with non-nil buffer-specific
file variable `allout-layout'.

When allout-auto-activation is \"On\" (t), allout mode is
activated in buffers with non-nil `allout-layout', and the
specified layout is applied.

With value \"ask\", auto-mode-activation is enabled, and endorsement for
performing auto-layout is asked of the user each time.

With value \"activate\", only auto-mode-activation is enabled.
Auto-layout is not.

With value nil, inhibit any automatic allout-mode activation."
  :set 'allout-auto-activation-helper
  ;; FIXME: Using strings here is unusual and less efficient than symbols.
  :type '(choice (const :tag "On" t)
                (const :tag "Ask about layout" "ask")
                (const :tag "Mode only" "activate")
                (const :tag "Off" nil))
  :group 'allout)
(allout-setup)
;;;_  = allout-default-layout
(defcustom allout-default-layout '(-2 : 0)
  "Default allout outline layout specification.

This setting specifies the outline exposure to use when
`allout-layout' has the local value t.  This docstring describes the
layout specifications.

A list value specifies a default layout for the current buffer,
to be applied upon activation of `allout-mode'.  Any non-nil
value will automatically trigger `allout-mode', provided
`allout-auto-activation' has been customized to enable it.

The types of elements in the layout specification are:

 INTEGER -- dictate the relative depth to open the corresponding topic(s),
            where:
         -- negative numbers force the topic to be closed before opening
            to the absolute value of the number, so all siblings are open
            only to that level.
         -- positive numbers open to the relative depth indicated by the
            number, but do not force already opened subtopics to be closed.
         -- 0 means to close topic -- hide all subitems.
 :   -- repeat spec -- apply the preceding element to all siblings at
        current level, *up to* those siblings that would be covered by specs
        following the `:' on the list.  Ie, apply to all topics at level but
        trailing ones accounted for by trailing specs.  (Only the first of
        multiple colons at the same level is honored -- later ones are ignored.)
 *   -- completely exposes the topic, including bodies
 +   -- exposes all subtopics, but not the bodies
 -   -- exposes the body of the corresponding topic, but not subtopics
 LIST -- a nested layout spec, to be applied intricately to its
        corresponding item(s)

Examples:
 (-2 : 0)
	Collapse the top-level topics to show their children and
        grandchildren, but completely collapse the final top-level topic.
 (-1 () : 1 0)
	Close the first topic so only the immediate subtopics are shown,
        leave the subsequent topics exposed as they are until the
	second to last topic, which is exposed at least one level, and
        completely close the last topic.
 (-2 : -1 *)
        Expose children and grandchildren of all topics at current
	level except the last two; expose children of the second to
	last and completely expose the last one, including its subtopics.

See `allout-expose-topic' for more about the exposure process.

Also, allout's mode-specific provisions will make topic prefixes default
to the comment-start string, if any, of the language of the file.  This
is modulo the setting of `allout-use-mode-specific-leader', which see."
  :type 'allout-layout-type
  :group 'allout)
;;;_  : allout-layout-type
(define-widget 'allout-layout-type 'lazy
  "Allout layout format customization basic building blocks."
  :type '(repeat
          (choice (integer :tag "integer (<= zero is strict)")
                  (const :tag ": (repeat prior)" :)
                  (const :tag "* (completely expose)" *)
                  (const :tag "+ (expose all offspring, headlines only)" +)
                  (const :tag "- (expose topic body but not offspring)" -)
                  (allout-layout-type :tag "<Nested layout>"))))

;;;_  = allout-inhibit-auto-fill
(defcustom allout-inhibit-auto-fill nil
  "If non-nil, auto-fill will be inhibited in the allout buffers.

You can customize this setting to set it for all allout buffers, or set it
in individual buffers if you want to inhibit auto-fill only in particular
buffers.  (You could use a function on `allout-mode-hook' to inhibit
auto-fill according, eg, to the major mode.)

If you don't set this and auto-fill-mode is enabled, allout will use the
value that `normal-auto-fill-function', if any, when allout mode starts, or
else allout's special hanging-indent maintaining auto-fill function,
`allout-auto-fill'."
  :type 'boolean
  :group 'allout)
(make-variable-buffer-local 'allout-inhibit-auto-fill)
;;;_  = allout-inhibit-auto-fill-on-headline
(defcustom allout-inhibit-auto-fill-on-headline nil
  "If non-nil, auto-fill will be inhibited while on topic's header line."
  :version "24.1"
  :type 'boolean
  :group 'allout)
(make-variable-buffer-local 'allout-inhibit-auto-fill-on-headline)
;;;_  = allout-use-hanging-indents
(defcustom allout-use-hanging-indents t
  "If non-nil, topic body text auto-indent defaults to indent of the header.
Ie, it is indented to be just past the header prefix.  This is
relevant mostly for use with `indented-text-mode', or other situations
where auto-fill occurs."
  :type 'boolean
  :group 'allout)
(make-variable-buffer-local 'allout-use-hanging-indents)
;;;###autoload
(put 'allout-use-hanging-indents 'safe-local-variable 'booleanp)
;;;_  = allout-reindent-bodies
(defcustom allout-reindent-bodies (if allout-use-hanging-indents
				    'text)
  "Non-nil enables auto-adjust of topic body hanging indent with depth shifts.

When active, topic body lines that are indented even with or beyond
their topic header are reindented to correspond with depth shifts of
the header.

A value of t enables reindent in non-programming-code buffers, ie
those that do not have the variable `comment-start' set.  A value of
`force' enables reindent whether or not `comment-start' is set."
  :type '(choice (const nil) (const t) (const text) (const force))
  :group 'allout)

(make-variable-buffer-local 'allout-reindent-bodies)
;;;###autoload
(put 'allout-reindent-bodies 'safe-local-variable
     (lambda (x) (memq x '(nil t text force))))

;;;_  = allout-show-bodies
(defcustom allout-show-bodies nil
  "If non-nil, show entire body when exposing a topic, rather than
just the header."
  :type 'boolean
  :group 'allout)
(make-variable-buffer-local 'allout-show-bodies)
;;;###autoload
(put 'allout-show-bodies 'safe-local-variable 'booleanp)

;;;_  = allout-beginning-of-line-cycles
(defcustom allout-beginning-of-line-cycles t
  "If non-nil, \\[allout-beginning-of-line] will cycle through smart-placement options.

Cycling only happens on when the command is repeated, not when it
follows a different command.

Smart-placement means that repeated calls to this function will
advance as follows:

 - if the cursor is on a non-headline body line and not on the first column:
   then it goes to the first column
 - if the cursor is on the first column of a non-headline body line:
   then it goes to the start of the headline within the item body
 - if the cursor is on the headline and not the start of the headline:
   then it goes to the start of the headline
 - if the cursor is on the start of the headline:
   then it goes to the bullet character (for hotspot navigation)
 - if the cursor is on the bullet character:
   then it goes to the first column of that line (the headline)
 - if the cursor is on the first column of the headline:
   then it goes to the start of the headline within the item body.

In this fashion, you can use the beginning-of-line command to do
its normal job and then, when repeated, advance through the
entry, cycling back to start.

If this configuration variable is nil, then the cursor is just
advanced to the beginning of the line and remains there on
repeated calls."
  :type 'boolean :group 'allout)
;;;_  = allout-end-of-line-cycles
(defcustom allout-end-of-line-cycles t
  "If non-nil, \\[allout-end-of-line] will cycle through smart-placement options.

Cycling only happens on when the command is repeated, not when it
follows a different command.

Smart placement means that repeated calls to this function will
advance as follows:

 - if the cursor is not on the end-of-line,
   then it goes to the end-of-line
 - if the cursor is on the end-of-line but not the end-of-entry,
   then it goes to the end-of-entry, exposing it if necessary
 - if the cursor is on the end-of-entry,
   then it goes to the end of the head line

In this fashion, you can use the end-of-line command to do its
normal job and then, when repeated, advance through the entry,
cycling back to start.

If this configuration variable is nil, then the cursor is just
advanced to the end of the line and remains there on repeated
calls."
  :type 'boolean :group 'allout)

;;;_  = allout-header-prefix
(defcustom allout-header-prefix "."
;; this string is treated as literal match.  it will be `regexp-quote'd, so
;; one cannot use regular expressions to match varying header prefixes.
  "Leading string which helps distinguish topic headers.

Outline topic header lines are identified by a leading topic
header prefix, which mostly have the value of this var at their front.
Level 1 topics are exceptions.  They consist of only a single
character, which is typically set to the `allout-primary-bullet'."
  :type 'string
  :group 'allout)
(make-variable-buffer-local 'allout-header-prefix)
;;;###autoload
(put 'allout-header-prefix 'safe-local-variable 'stringp)
;;;_  = allout-primary-bullet
(defcustom allout-primary-bullet "*"
  "Bullet used for top-level outline topics.

Outline topic header lines are identified by a leading topic header
prefix, which is concluded by bullets that includes the value of this
var and the respective allout-*-bullets-string vars.

The value of an asterisk (`*') provides for backwards compatibility
with the original Emacs outline mode.  See `allout-plain-bullets-string'
and `allout-distinctive-bullets-string' for the range of available
bullets."
  :type 'string
  :group 'allout)
(make-variable-buffer-local 'allout-primary-bullet)
;;;###autoload
(put 'allout-primary-bullet 'safe-local-variable 'stringp)
;;;_  = allout-plain-bullets-string
(defcustom allout-plain-bullets-string ".,"
  "The bullets normally used in outline topic prefixes.

See `allout-distinctive-bullets-string' for the other kind of
bullets.

DO NOT include the close-square-bracket, `]', as a bullet.

Outline mode has to be reactivated in order for changes to the value
of this var to take effect."
  :type 'string
  :group 'allout)
(make-variable-buffer-local 'allout-plain-bullets-string)
;;;###autoload
(put 'allout-plain-bullets-string 'safe-local-variable 'stringp)
;;;_  = allout-distinctive-bullets-string
(defcustom allout-distinctive-bullets-string "*+-=>()[{}&!?#%\"X@$~_\\:;^"
  "Persistent outline header bullets used to distinguish special topics.

These bullets are distinguish topics with particular character.
They are not used by default in the topic creation routines, but
are offered as options when you modify topic creation with a
universal argument (\\[universal-argument]), or during rebulleting (\\[allout-rebullet-current-heading]).

Distinctive bullets are not cycled when topics are shifted or
otherwise automatically rebulleted, so their marking is
persistent until deliberately changed.  Their significance is
purely by convention, however.  Some conventions suggest
themselves:

 `(' - open paren -- an aside or incidental point
 `?' - question mark -- uncertain or outright question
 `!' - exclamation point/bang -- emphatic
 `[' - open square bracket -- meta-note, about item instead of item's subject
 `\"' - double quote -- a quotation or other citation
 `=' - equal sign -- an assignment, some kind of definition
 `^' - carat -- relates to something above

Some are more elusive, but their rationale may be recognizable:

 `+' - plus -- pending consideration, completion
 `_' - underscore -- done, completed
 `&' - ampersand -- addendum, furthermore

\(Some other non-plain bullets have special meaning to the
software.  By default:

 `~' marks encryptable topics -- see `allout-topic-encryption-bullet'
 `#' marks auto-numbered bullets -- see `allout-numbered-bullet'.)

See `allout-plain-bullets-string' for the standard, alternating
bullets.

You must run `allout-set-regexp' in order for outline mode to
adopt changes of this value.

DO NOT include the close-square-bracket, `]', on either of the bullet
strings."
  :type 'string
  :group 'allout)
(make-variable-buffer-local 'allout-distinctive-bullets-string)
;;;###autoload
(put 'allout-distinctive-bullets-string 'safe-local-variable 'stringp)

;;;_  = allout-use-mode-specific-leader
(defcustom allout-use-mode-specific-leader t
  "When non-nil, use mode-specific topic-header prefixes.

Allout outline mode will use the mode-specific `allout-mode-leaders' or
comment-start string, if any, to lead the topic prefix string, so topic
headers look like comments in the programming language.  It will also use
the comment-start string, with an `_' appended, for `allout-primary-bullet'.

String values are used as literals, not regular expressions, so
do not escape any regular-expression characters.

Value t means to first check for assoc value in `allout-mode-leaders'
alist, then use comment-start string, if any, then use default (`.').
\(See note about use of comment-start strings, below.)

Set to the symbol for either of `allout-mode-leaders' or
`comment-start' to use only one of them, respectively.

Value nil means to always use the default (`.') and leave
`allout-primary-bullet' unaltered.

comment-start strings that do not end in spaces are tripled in
the header-prefix, and an `_' underscore is tacked on the end, to
distinguish them from regular comment strings.  comment-start
strings that do end in spaces are not tripled, but an underscore
is substituted for the space.  [This presumes that the space is
for appearance, not comment syntax.  You can use
`allout-mode-leaders' to override this behavior, when
undesired.]"
  :type '(choice (const t) (const nil) string
		 (const allout-mode-leaders)
		 (const comment-start))
  :group 'allout)
;;;###autoload
(put 'allout-use-mode-specific-leader 'safe-local-variable
     (lambda (x) (or (memq x '(t nil allout-mode-leaders comment-start))
                      (stringp x))))
;;;_  = allout-mode-leaders
(defvar allout-mode-leaders '()
  "Specific allout-prefix leading strings per major modes.

Use this if the mode's comment-start string isn't what you
prefer, or if the mode lacks a comment-start string.  See
`allout-use-mode-specific-leader' for more details.

If you're constructing a string that will comment-out outline
structuring so it can be included in program code, append an extra
character, like an \"_\" underscore, to distinguish the lead string
from regular comments that start at the beginning-of-line.")

;;;_  = allout-old-style-prefixes
(defcustom allout-old-style-prefixes nil
  "When non-nil, use only old-and-crusty `outline-mode' `*' topic prefixes.

Non-nil restricts the topic creation and modification
functions to asterix-padded prefixes, so they look exactly
like the original Emacs-outline style prefixes.

Whatever the setting of this variable, both old and new style prefixes
are always respected by the topic maneuvering functions."
  :type 'boolean
  :group 'allout)
(make-variable-buffer-local 'allout-old-style-prefixes)
;;;###autoload
(put 'allout-old-style-prefixes 'safe-local-variable 'booleanp)
;;;_  = allout-stylish-prefixes -- alternating bullets
(defcustom allout-stylish-prefixes t
  "Do fancy stuff with topic prefix bullets according to level, etc.

Non-nil enables topic creation, modification, and repositioning
functions to vary the topic bullet char (the char that marks the topic
depth) just preceding the start of the topic text) according to level.
Otherwise, only asterisks (`*') and distinctive bullets are used.

This is how an outline can look (but sans indentation) with stylish
prefixes:

    * Top level
    .* A topic
    . + One level 3 subtopic
    .  . One level 4 subtopic
    .  . A second 4 subtopic
    . + Another level 3 subtopic
    .  #1 A numbered level 4 subtopic
    .  #2 Another
    .  ! Another level 4 subtopic with a different distinctive bullet
    .  #4 And another numbered level 4 subtopic

This would be an outline with stylish prefixes inhibited (but the
numbered and other distinctive bullets retained):

    * Top level
    .* A topic
    . * One level 3 subtopic
    .  * One level 4 subtopic
    .  * A second 4 subtopic
    . * Another level 3 subtopic
    .  #1 A numbered level 4 subtopic
    .  #2 Another
    .  ! Another level 4 subtopic with a different distinctive bullet
    .  #4 And another numbered level 4 subtopic

Stylish and constant prefixes (as well as old-style prefixes) are
always respected by the topic maneuvering functions, regardless of
this variable setting.

The setting of this var is not relevant when `allout-old-style-prefixes'
is non-nil."
  :type 'boolean
  :group 'allout)
(make-variable-buffer-local 'allout-stylish-prefixes)
;;;###autoload
(put 'allout-stylish-prefixes 'safe-local-variable 'booleanp)

;;;_  = allout-numbered-bullet
(defcustom allout-numbered-bullet "#"
  "String designating bullet of topics that have auto-numbering; nil for none.

Topics having this bullet have automatic maintenance of a sibling
sequence-number tacked on, just after the bullet.  Conventionally set
to \"#\", you can set it to a bullet of your choice.  A nil value
disables numbering maintenance."
  :type '(choice (const nil) string)
  :group 'allout)
(make-variable-buffer-local 'allout-numbered-bullet)
;;;###autoload
(put 'allout-numbered-bullet 'safe-local-variable 'string-or-null-p)
;;;_  = allout-file-xref-bullet
(defcustom allout-file-xref-bullet "@"
  "Bullet signifying file cross-references, for `allout-resolve-xref'.

Set this var to the bullet you want to use for file cross-references."
  :type '(choice (const nil) string)
  :group 'allout)
;;;###autoload
(put 'allout-file-xref-bullet 'safe-local-variable 'string-or-null-p)
;;;_  = allout-presentation-padding
(defcustom allout-presentation-padding 2
  "Presentation-format white-space padding factor, for greater indent."
  :type 'integer
  :group 'allout)

(make-variable-buffer-local 'allout-presentation-padding)
;;;###autoload
(put 'allout-presentation-padding 'safe-local-variable 'integerp)

;;;_  = allout-flattened-numbering-abbreviation
(define-obsolete-variable-alias 'allout-abbreviate-flattened-numbering
  'allout-flattened-numbering-abbreviation "24.1")
(defcustom allout-flattened-numbering-abbreviation nil
  "If non-nil, `allout-flatten-exposed-to-buffer' abbreviates topic
numbers to minimal amount with some context.  Otherwise, entire
numbers are always used."
  :version "24.1"
  :type 'boolean
  :group 'allout)

;;;_ + LaTeX formatting
;;;_  - allout-number-pages
(defcustom allout-number-pages nil
  "Non-nil turns on page numbering for LaTeX formatting of an outline."
  :type 'boolean
  :group 'allout)
;;;_  - allout-label-style
(defcustom allout-label-style "\\large\\bf"
  "Font and size of labels for LaTeX formatting of an outline."
  :type 'string
  :group 'allout)
;;;_  - allout-head-line-style
(defcustom allout-head-line-style "\\large\\sl "
  "Font and size of entries for LaTeX formatting of an outline."
  :type 'string
  :group 'allout)
;;;_  - allout-body-line-style
(defcustom allout-body-line-style " "
  "Font and size of entries for LaTeX formatting of an outline."
  :type 'string
  :group 'allout)
;;;_  - allout-title-style
(defcustom allout-title-style "\\Large\\bf"
  "Font and size of titles for LaTeX formatting of an outline."
  :type 'string
  :group 'allout)
;;;_  - allout-title
(defcustom allout-title '(or buffer-file-name (buffer-name))
  "Expression to evaluate to determine the title for LaTeX formatted copy."
  :type 'sexp
  :risky t
  :group 'allout)
;;;_  - allout-line-skip
(defcustom allout-line-skip ".05cm"
  "Space between lines for LaTeX formatting of an outline."
  :type 'string
  :group 'allout)
;;;_  - allout-indent
(defcustom allout-indent ".3cm"
  "LaTeX formatted depth-indent spacing."
  :type 'string
  :group 'allout)

;;;_ + Topic encryption
;;;_  = allout-encryption group
(defgroup allout-encryption nil
  "Settings for topic encryption features of allout outliner."
  :group 'allout)
;;;_  = allout-topic-encryption-bullet
(defcustom allout-topic-encryption-bullet "~"
  "Bullet signifying encryption of the entry's body."
  :type '(choice (const nil) string)
  :version "22.1"
  :group 'allout-encryption)
;;;_  = allout-encrypt-unencrypted-on-saves
(defcustom allout-encrypt-unencrypted-on-saves t
  "If non-nil, topics pending encryption are encrypted during buffer saves.

This prevents file-system exposure of un-encrypted contents of
items marked for encryption.

When non-nil, if the topic currently being edited is decrypted,
it will be encrypted for saving but automatically decrypted
before any subsequent user interaction, so it is once again clear
text for editing though the file system copy is encrypted.

\(Auto-saves are handled differently.  Buffers with plain-text
exposed encrypted topics are exempted from auto saves until all
such topics are encrypted.)"

  :type 'boolean
  :version "23.1"
  :group 'allout-encryption)
(make-variable-buffer-local 'allout-encrypt-unencrypted-on-saves)
(defvar allout-auto-save-temporarily-disabled nil
  "True while topic encryption is pending and auto-saving was active.

The value of `buffer-saved-size' at the time of decryption is used,
for restoring when all encryptions are established.")
(defvar allout-just-did-undo nil
  "True just after undo commands, until allout-post-command-business.")
(make-variable-buffer-local 'allout-just-did-undo)

;;;_ + Developer
;;;_  = allout-developer group
(defgroup allout-developer nil
  "Allout settings developers care about, including topic encryption and more."
  :group 'allout)
;;;_  = allout-run-unit-tests-on-load
(defcustom allout-run-unit-tests-on-load nil
  "When non-nil, unit tests will be run at end of loading the allout module.

Generally, allout code developers are the only ones who'll want to set this.

\(If set, this makes it an even better practice to exercise changes by
doing byte-compilation with a repeat count, so the file is loaded after
compilation.)

See `allout-run-unit-tests' to see what's run."
  :type 'boolean
  :group 'allout-developer)

;;;_ + Miscellaneous customization

;;;_  = allout-enable-file-variable-adjustment
(defcustom allout-enable-file-variable-adjustment t
  "If non-nil, some allout outline actions edit Emacs local file var text.

This can range from changes to existing entries, addition of new ones,
and creation of a new local variables section when necessary.

Emacs file variables adjustments are also inhibited if `enable-local-variables'
is nil.

Operations potentially causing edits include allout encryption routines.
For details, see `allout-toggle-current-subtree-encryption's docstring."
  :type 'boolean
  :group 'allout)
(make-variable-buffer-local 'allout-enable-file-variable-adjustment)

;;;_* CODE -- no user customizations below.

;;;_ #1 Internal Outline Formatting and Configuration
;;;_  : Version
;;;_   = allout-version
(defvar allout-version "2.3"
  "Version of currently loaded outline package.  (allout.el)")
;;;_   > allout-version
(defun allout-version (&optional here)
  "Return string describing the loaded outline version."
  (interactive "P")
  (let ((msg (concat "Allout Outline Mode v " allout-version)))
    (if here (insert msg))
    (message "%s" msg)
    msg))
;;;_  : Mode activation (defined here because it's referenced early)
;;;_   = allout-mode
(defvar allout-mode nil "Allout outline mode minor-mode flag.")
(make-variable-buffer-local 'allout-mode)
;;;_   = allout-layout nil
(defvar allout-layout nil            ; LEAVE GLOBAL VALUE NIL -- see docstring.
  "Buffer-specific setting for allout layout.

In buffers where this is non-nil (and if `allout-auto-activation'
has been customized to enable this behavior), `allout-mode' will be
automatically activated.  The layout dictated by the value will be used to
set the initial exposure when `allout-mode' is activated.

*You should not setq-default this variable non-nil unless you want every
visited file to be treated as an allout file.*

The value would typically be set by a file local variable.  For
example, the following lines at the bottom of an Emacs Lisp file:

;;;Local variables:
;;;allout-layout: (0 : -1 -1 0)
;;;End:

dictate activation of `allout-mode' mode when the file is visited
\(presuming proper `allout-auto-activation' customization),
followed by the equivalent of `(allout-expose-topic 0 : -1 -1 0)'.
\(This is the layout used for the allout.el source file.)

`allout-default-layout' describes the specification format.
`allout-layout' can additionally have the value t, in which
case the value of `allout-default-layout' is used.")
(make-variable-buffer-local 'allout-layout)
;;;###autoload
(put 'allout-layout 'safe-local-variable
     (lambda (x) (or (numberp x) (listp x) (memq x '(: * + -)))))

;;;_  : Topic header format
;;;_   = allout-regexp
(defvar allout-regexp ""
  "Regular expression to match the beginning of a heading line.

Any line whose beginning matches this regexp is considered a
heading.  This var is set according to the user configuration vars
by `allout-set-regexp'.")
(make-variable-buffer-local 'allout-regexp)
;;;_   = allout-bullets-string
(defvar allout-bullets-string ""
  "A string dictating the valid set of outline topic bullets.

This var should *not* be set by the user -- it is set by `allout-set-regexp',
and is produced from the elements of `allout-plain-bullets-string'
and `allout-distinctive-bullets-string'.")
(make-variable-buffer-local 'allout-bullets-string)
;;;_   = allout-bullets-string-len
(defvar allout-bullets-string-len 0
  "Length of current buffers' `allout-plain-bullets-string'.")
(make-variable-buffer-local 'allout-bullets-string-len)
;;;_   = allout-depth-specific-regexp
(defvar allout-depth-specific-regexp ""
  "Regular expression to match a heading line prefix for a particular depth.

This expression is used to search for depth-specific topic
headers at depth 2 and greater.  Use `allout-depth-one-regexp'
for to seek topics at depth one.

This var is set according to the user configuration vars by
`allout-set-regexp'.  It is prepared with format strings for two
decimal numbers, which should each be one less than the depth of the
topic prefix to be matched.")
(make-variable-buffer-local 'allout-depth-specific-regexp)
;;;_   = allout-depth-one-regexp
(defvar allout-depth-one-regexp ""
  "Regular expression to match a heading line prefix for depth one.

This var is set according to the user configuration vars by
`allout-set-regexp'.  It is prepared with format strings for two
decimal numbers, which should each be one less than the depth of the
topic prefix to be matched.")
(make-variable-buffer-local 'allout-depth-one-regexp)
;;;_   = allout-line-boundary-regexp
(defvar allout-line-boundary-regexp ()
  "`allout-regexp' prepended with a newline for the search target.

This is properly set by `allout-set-regexp'.")
(make-variable-buffer-local 'allout-line-boundary-regexp)
;;;_   = allout-bob-regexp
(defvar allout-bob-regexp ()
  "Like `allout-line-boundary-regexp', for headers at beginning of buffer.")
(make-variable-buffer-local 'allout-bob-regexp)
;;;_   = allout-header-subtraction
(defvar allout-header-subtraction (1- (length allout-header-prefix))
  "Allout-header prefix length to subtract when computing topic depth.")
(make-variable-buffer-local 'allout-header-subtraction)
;;;_   = allout-plain-bullets-string-len
(defvar allout-plain-bullets-string-len (length allout-plain-bullets-string)
  "Length of `allout-plain-bullets-string', updated by `allout-set-regexp'.")
(make-variable-buffer-local 'allout-plain-bullets-string-len)

;;;_   = allout-doublecheck-at-and-shallower
(defconst allout-doublecheck-at-and-shallower 3
  "Validate apparent topics of this depth and shallower as being non-aberrant.

Verified with `allout-aberrant-container-p'.  The usefulness of
this check is limited to shallow depths, because the
determination of aberrance is according to the mistaken item
being followed by a legitimate item of excessively greater depth.

The classic example of a mistaken item, for a standard allout
outline configuration, is a body line that begins with an `...'
ellipsis.  This happens to contain a legitimate depth-2 header
prefix, constituted by two `..' dots at the beginning of the
line.  The only thing that can distinguish it *in principle* from
a legitimate one is if the following real header is at a depth
that is discontinuous from the depth of 2 implied by the
ellipsis, ie depth 4 or more.  As the depth being tested gets
greater, the likelihood of this kind of disqualification is
lower, and the usefulness of this test is lower.

Extending the depth of the doublecheck increases the amount it is
applied, increasing the cost of the test - on casual estimation,
for outlines with many deep topics, geometrically (O(n)?).
Taken together with decreasing likelihood that the test will be
useful at greater depths, more modest doublecheck limits are more
suitably economical.")
;;;_   X allout-reset-header-lead (header-lead)
(defun allout-reset-header-lead (header-lead)
  "Reset the leading string used to identify topic headers."
  (interactive "sNew lead string: ")
  (setq allout-header-prefix header-lead)
  (setq allout-header-subtraction (1- (length allout-header-prefix)))
  (allout-set-regexp))
;;;_   X allout-lead-with-comment-string (header-lead)
(defun allout-lead-with-comment-string (&optional header-lead)
  "Set the topic-header leading string to specified string.

Useful for encapsulating outline structure in programming
language comments.  Returns the leading string."

  (interactive "P")
  (if (not (stringp header-lead))
      (setq header-lead (read-string
                         "String prefix for topic headers: ")))
  (setq allout-reindent-bodies nil)
  (allout-reset-header-lead header-lead)
  header-lead)
;;;_   > allout-infer-header-lead-and-primary-bullet ()
(defun allout-infer-header-lead-and-primary-bullet ()
  "Determine appropriate `allout-header-prefix' and `allout-primary-bullet'.

Works according to settings of:

       `comment-start'
       `allout-header-prefix' (default)
       `allout-use-mode-specific-leader'
and    `allout-mode-leaders'.

Apply this via (re)activation of `allout-mode', rather than
invoking it directly."
  (let* ((use-leader (and (boundp 'allout-use-mode-specific-leader)
			  (if (or (stringp allout-use-mode-specific-leader)
				  (memq allout-use-mode-specific-leader
					'(allout-mode-leaders
					  comment-start
					  t)))
			      allout-use-mode-specific-leader
			    ;; Oops -- garbled value, equate with effect of t:
			    t)))
	 (leader
	  (cond
	   ((not use-leader) nil)
	   ;; Use the explicitly designated leader:
	   ((stringp use-leader) use-leader)
	   (t (or (and (memq use-leader '(t allout-mode-leaders))
		       ;; Get it from outline mode leaders?
		       (cdr (assq major-mode allout-mode-leaders)))
		  ;; ... didn't get from allout-mode-leaders...
		  (and (memq use-leader '(t comment-start))
		       comment-start
		       ;; Use comment-start, maybe tripled, and with
		       ;; underscore:
		       (concat
			(if (string= " "
				     (substring comment-start
						(1- (length comment-start))))
			    ;; Use comment-start, sans trailing space:
			    (substring comment-start 0 -1)
			  (concat comment-start comment-start comment-start))
			;; ... and append underscore, whichever:
			"_")))))))
    (if (not leader)
	nil
      (setq allout-header-prefix leader)
      (if (not allout-old-style-prefixes)
          ;; setting allout-primary-bullet makes the top level topics use --
          ;; actually, be -- the special prefix:
          (setq allout-primary-bullet leader))
      allout-header-prefix)))
(defalias 'allout-infer-header-lead
  'allout-infer-header-lead-and-primary-bullet)
;;;_   > allout-infer-body-reindent ()
(defun allout-infer-body-reindent ()
  "Determine proper setting for `allout-reindent-bodies'.

Depends on default setting of `allout-reindent-bodies' (which see)
and presence of setting for `comment-start', to tell whether the
file is programming code."
  (if (and allout-reindent-bodies
	   comment-start
	   (not (eq 'force allout-reindent-bodies)))
      (setq allout-reindent-bodies nil)))
;;;_   > allout-set-regexp ()
(defun allout-set-regexp ()
  "Generate proper topic-header regexp form for outline functions.

Works with respect to `allout-plain-bullets-string' and
`allout-distinctive-bullets-string'.

Also refresh various data structures that hinge on the regexp."

  (interactive)
  ;; Derive allout-bullets-string from user configured components:
  (setq allout-bullets-string "")
  (let ((strings (list 'allout-plain-bullets-string
                       'allout-distinctive-bullets-string
                       'allout-primary-bullet))
        cur-string
        cur-len
        cur-char
        index)
    (while strings
      (setq index 0)
      (setq cur-len (length (setq cur-string (symbol-value (car strings)))))
      (while (< index cur-len)
        (setq cur-char (aref cur-string index))
        (setq allout-bullets-string
              (concat allout-bullets-string
                      (cond
                                        ; Single dash would denote a
                                        ; sequence, repeated denotes
                                        ; a dash:
                       ((eq cur-char ?-) "--")
                                        ; literal close-square-bracket
                                        ; doesn't work right in the
                                        ; expr, exclude it:
                       ((eq cur-char ?\]) "")
                       (t (regexp-quote  (char-to-string cur-char))))))
        (setq index (1+ index)))
      (setq strings (cdr strings)))
    )
  ;; Derive next for repeated use in allout-pending-bullet:
  (setq allout-plain-bullets-string-len (length allout-plain-bullets-string))
  (setq allout-header-subtraction (1- (length allout-header-prefix)))

  (let (new-part old-part formfeed-part)
    (setq new-part (concat "\\("
                           (regexp-quote allout-header-prefix)
                           "[ \t]*"
                           ;; already regexp-quoted in a custom way:
                           "[" allout-bullets-string "]"
                           "\\)")
          old-part (concat "\\("
                           (regexp-quote allout-primary-bullet)
                           "\\|"
                           (regexp-quote allout-header-prefix)
                           "\\)"
                           "+"
                           " ?[^" allout-primary-bullet "]")
          formfeed-part "\\(\^L\\)"

          allout-regexp (concat new-part
                                "\\|"
                                old-part
                                "\\|"
                                formfeed-part)

          allout-line-boundary-regexp (concat "\n" new-part
                                              "\\|"
                                              "\n" old-part
                                              "\\|"
                                              "\n" formfeed-part)

          allout-bob-regexp (concat "\\`" new-part
                                    "\\|"
                                    "\\`" old-part
                                    "\\|"
                                    "\\`" formfeed-part
                                    ))

    (setq allout-depth-specific-regexp
          (concat "\\(^\\|\\`\\)"
                  "\\("

                  ;; new-style spacers-then-bullet string:
                  "\\("
                  (allout-format-quote (regexp-quote allout-header-prefix))
                  " \\{%s\\}"
                  "[" (allout-format-quote allout-bullets-string) "]"
                  "\\)"

                  ;; old-style all-bullets string, if primary not multi-char:
                  (if (< 0 allout-header-subtraction)
                      ""
                    (concat "\\|\\("
                            (allout-format-quote
                             (regexp-quote allout-primary-bullet))
                            (allout-format-quote
                             (regexp-quote allout-primary-bullet))
                            (allout-format-quote
                             (regexp-quote allout-primary-bullet))
                            "\\{%s\\}"
                            ;; disqualify greater depths:
                            "[^"
                            (allout-format-quote allout-primary-bullet)
                            "]\\)"
                            ))
                  "\\)"
                  ))
    (setq allout-depth-one-regexp
          (concat "\\(^\\|\\`\\)"
                  "\\("

                  "\\("
                  (regexp-quote allout-header-prefix)
                  ;; disqualify any bullet char following any amount of
                  ;; intervening whitespace:
                  " *"
                  (concat "[^ " allout-bullets-string "]")
                  "\\)"
                  (if (< 0 allout-header-subtraction)
                      ;; Need not support anything like the old
                      ;; bullet style if the prefix is multi-char.
                      ""
                    (concat "\\|"
                            (regexp-quote allout-primary-bullet)
                            ;; disqualify deeper primary-bullet sequences:
                            "[^" allout-primary-bullet "]"))
                  "\\)"
                  ))))
(define-obsolete-function-alias 'set-allout-regexp 'allout-set-regexp "26.1")
;;;_  : Menu bar
(defvar allout-mode-exposure-menu)
(defvar allout-mode-editing-menu)
(defvar allout-mode-navigation-menu)
(defvar allout-mode-misc-menu)
(defun allout-produce-mode-menubar-entries ()
  (require 'easymenu)
  (easy-menu-define allout-mode-exposure-menu
		    allout-mode-map-value
		    "Allout outline exposure menu."
		    '("Exposure"
		      ["Show Entry" allout-show-current-entry t]
		      ["Show Children" allout-show-children t]
		      ["Show Subtree" allout-show-current-subtree t]
		      ["Hide Subtree" allout-hide-current-subtree t]
		      ["Hide Leaves" allout-hide-current-leaves t]
		      "----"
		      ["Show All" allout-show-all t]))
  (easy-menu-define allout-mode-editing-menu
		    allout-mode-map-value
		    "Allout outline editing menu."
		    '("Headings"
		      ["Open Sibling" allout-open-sibtopic t]
		      ["Open Subtopic" allout-open-subtopic t]
		      ["Open Supertopic" allout-open-supertopic t]
		      "----"
		      ["Shift Topic In" allout-shift-in t]
		      ["Shift Topic Out" allout-shift-out t]
		      ["Rebullet Topic" allout-rebullet-topic t]
		      ["Rebullet Heading" allout-rebullet-current-heading t]
		      ["Number Siblings" allout-number-siblings t]
		      "----"
                      ["Toggle Topic Encryption"
                       allout-toggle-current-subtree-encryption
                       (> (allout-current-depth) 1)]))
  (easy-menu-define allout-mode-navigation-menu
		    allout-mode-map-value
		    "Allout outline navigation menu."
		    '("Navigation"
		      ["Next Visible Heading" allout-next-visible-heading t]
		      ["Previous Visible Heading"
		       allout-previous-visible-heading t]
		      "----"
		      ["Up Level" allout-up-current-level t]
		      ["Forward Current Level" allout-forward-current-level t]
		      ["Backward Current Level"
		       allout-backward-current-level t]
		      "----"
		      ["Beginning of Entry"
		       allout-beginning-of-current-entry t]
		      ["End of Entry" allout-end-of-entry t]
		      ["End of Subtree" allout-end-of-current-subtree t]))
  (easy-menu-define allout-mode-misc-menu
		    allout-mode-map-value
		    "Allout outlines miscellaneous bindings."
		    '("Misc"
		      ["Version" allout-version t]
		      "----"
		      ["Duplicate Exposed" allout-copy-exposed-to-buffer t]
		      ["Duplicate Exposed, numbered"
		       allout-flatten-exposed-to-buffer t]
		      ["Duplicate Exposed, indented"
		       allout-indented-exposed-to-buffer t]
		      "----"
		      ["Set Header Lead" allout-reset-header-lead t]
		      ["Set New Exposure" allout-expose-topic t])))
;;;_  : Allout Modal-Variables Utilities
;;;_   = allout-mode-prior-settings
(defvar allout-mode-prior-settings nil
  "Internal `allout-mode' use; settings to be resumed on mode deactivation.

See `allout-add-resumptions' and `allout-do-resumptions'.")
(make-variable-buffer-local 'allout-mode-prior-settings)
;;;_   > allout-add-resumptions (&rest pairs)
(defun allout-add-resumptions (&rest pairs)
  "Set name/value PAIRS.

Old settings are preserved for later resumption using `allout-do-resumptions'.

The new values are set as a buffer local.  On resumption, the prior buffer
scope of the variable is restored along with its value.  If it was a void
buffer-local value, then it is left as nil on resumption.

The pairs are lists whose car is the name of the variable and car of the
cdr is the new value: `(some-var some-value)'.  The pairs can actually be
triples, where the third element qualifies the disposition of the setting,
as described further below.

If the optional third element is the symbol `extend', then the new value
created by `cons'ing the second element of the pair onto the front of the
existing value.

If the optional third element is the symbol `append', then the new value is
extended from the existing one by `append'ing a list containing the second
element of the pair onto the end of the existing value.

Extension, and resumptions in general, should not be used for hook
functions -- use the `local' mode of `add-hook' for that, instead.

The settings are stored on `allout-mode-prior-settings'."
  (while pairs
    (let* ((pair (pop pairs))
           (name (car pair))
           (value (cadr pair))
           (qualifier (if (> (length pair) 2)
                          (caddr pair)))
           prior-value)
      (if (not (symbolp name))
          (error "Pair's name, %S, must be a symbol, not %s"
                 name (type-of name)))
      (setq prior-value (condition-case nil
                            (symbol-value name)
                          (void-variable nil)))
      (when (not (assoc name allout-mode-prior-settings))
        ;; Not already added as a resumption, create the prior setting entry.
        (if (local-variable-p name (current-buffer))
            ;; is already local variable -- preserve the prior value:
            (push (list name prior-value) allout-mode-prior-settings)
          ;; wasn't local variable, indicate so for resumption by killing
          ;; local value, and make it local:
          (push (list name) allout-mode-prior-settings)
          (make-local-variable name)))
      (if qualifier
          (cond ((eq qualifier 'extend)
                 (if (not (listp prior-value))
                     (error "extension of non-list prior value attempted")
                   (set name (cons value prior-value))))
                ((eq qualifier 'append)
                 (if (not (listp prior-value))
                     (error "appending of non-list prior value attempted")
                   (set name (append prior-value (list value)))))
                (t (error "unrecognized setting qualifier `%s' encountered"
                          qualifier)))
        (set name value)))))
;;;_   > allout-do-resumptions ()
(defun allout-do-resumptions ()
  "Resume all name/value settings registered by `allout-add-resumptions'.

This is used when concluding allout-mode, to resume selected variables to
their settings before allout-mode was started."

    (while allout-mode-prior-settings
      (let* ((pair (pop allout-mode-prior-settings))
             (name (car pair))
             (value-cell (cdr pair)))
        (if (not value-cell)
            ;; Prior value was global:
            (kill-local-variable name)
          ;; Prior value was explicit:
          (set name (car value-cell))))))
;;;_  : Mode-specific incidentals
;;;_   > allout-unprotected (expr)
(defmacro allout-unprotected (expr)
  "Enable internal outline operations to alter invisible text."
  `(let ((inhibit-read-only (if (not buffer-read-only) t))
         (inhibit-field-text-motion t))
     ,expr))
;;;_   = allout-mode-hook
(defvar allout-mode-hook nil
  "Hook run when allout mode starts.")
;;;_   = allout-mode-deactivate-hook
(define-obsolete-variable-alias 'allout-mode-deactivate-hook
  'allout-mode-off-hook "24.1")
(defvar allout-mode-deactivate-hook nil
  "Hook run when allout mode ends.")
;;;_   = allout-exposure-category
(defvar allout-exposure-category nil
  "Symbol for use as allout invisible-text overlay category.")

;;;_   = allout-exposure-change-functions
(define-obsolete-variable-alias 'allout-exposure-change-hook
  'allout-exposure-change-functions "24.3")
(defcustom allout-exposure-change-functions nil
  "Abnormal hook run after allout outline subtree exposure changes.
It is run at the conclusion of `allout-flag-region'.

Functions on the hook must take three arguments:

 - FROM -- integer indicating the point at the start of the change.
 - TO -- integer indicating the point of the end of the change.
 - FLAG -- change mode: nil for exposure, otherwise concealment.

This hook might be invoked multiple times by a single command."
  :type 'hook
  :group 'allout
  :version "24.3")

;;;_   = allout-structure-added-functions
(define-obsolete-variable-alias 'allout-structure-added-hook
  'allout-structure-added-functions "24.3")
(defcustom allout-structure-added-functions nil
  "Abnormal hook run after adding items to an Allout outline.
Functions on the hook should take two arguments:

 - NEW-START -- integer indicating position of start of the first new item.
 - NEW-END -- integer indicating position of end of the last new item.

This hook might be invoked multiple times by a single command."
  :type 'hook
  :group 'allout
  :version "24.3")

;;;_   = allout-structure-deleted-functions
(define-obsolete-variable-alias 'allout-structure-deleted-hook
  'allout-structure-deleted-functions "24.3")
(defcustom allout-structure-deleted-functions nil
  "Abnormal hook run after deleting subtrees from an Allout outline.
Functions on the hook must take two arguments:

 - DEPTH -- integer indicating the depth of the subtree that was deleted.
 - REMOVED-FROM -- integer indicating the point where the subtree was removed.

Some edits that remove or invalidate items may be missed by this hook:
specifically edits that native allout routines do not control.

This hook might be invoked multiple times by a single command."
  :type 'hook
  :group 'allout
  :version "24.3")

;;;_   = allout-structure-shifted-functions
(define-obsolete-variable-alias 'allout-structure-shifted-hook
  'allout-structure-shifted-functions "24.3")
(defcustom allout-structure-shifted-functions nil
  "Abnormal hook run after shifting items in an Allout outline.
Functions on the hook should take two arguments:

 - DEPTH-CHANGE -- integer indicating depth increase, negative for decrease
 - START -- integer indicating the start point of the shifted parent item.

Some edits that shift items can be missed by this hook: specifically edits
that native allout routines do not control.

This hook might be invoked multiple times by a single command."
  :type 'hook
  :group 'allout
  :version "24.3")

;;;_   = allout-after-copy-or-kill-hook
(defcustom allout-after-copy-or-kill-hook nil
  "Normal hook run after copying outline text.."
  :type 'hook
  :group 'allout
  :version "24.3")

;;;_   = allout-post-undo-hook
(defcustom allout-post-undo-hook nil
  "Normal hook run after undo activity.
The item that's current when the hook is run *may* be the one
that was affected by the undo.."
  :type 'hook
  :group 'allout
  :version "24.3")

;;;_   = allout-outside-normal-auto-fill-function
(defvar allout-outside-normal-auto-fill-function nil
  "Value of `normal-auto-fill-function' outside of allout mode.

Used by `allout-auto-fill' to do the mandated `normal-auto-fill-function'
wrapped within allout's automatic `fill-prefix' setting.")
(make-variable-buffer-local 'allout-outside-normal-auto-fill-function)
;;;_   = prevent redundant activation by desktop mode:
(add-to-list 'desktop-minor-mode-handlers '(allout-mode . nil))
;;;_   = allout-after-save-decrypt
(defvar allout-after-save-decrypt nil
  "Internal variable, is nil or has the value of two points:

 - the location of a topic to be decrypted after saving is done
 - where to situate the cursor after the decryption is performed

This is used to decrypt the topic that was currently being edited, if it
was encrypted automatically as part of a file write or autosave.")
(make-variable-buffer-local 'allout-after-save-decrypt)
;;;_   = allout-encryption-plaintext-sanitization-regexps
(defvar allout-encryption-plaintext-sanitization-regexps nil
  "List of regexps whose matches are removed from plaintext before encryption.

This is for the sake of removing artifacts, like escapes, that are added on
and not actually part of the original plaintext.  The removal is done just
prior to encryption.

Entries must be symbols that are bound to the desired values.

Each value can be a regexp or a list with a regexp followed by a
substitution string.  If it's just a regexp, all its matches are removed
before the text is encrypted.  If it's a regexp and a substitution, the
substitution is used against the regexp matches, a la `replace-match'.")
(make-variable-buffer-local 'allout-encryption-plaintext-sanitization-regexps)
;;;_   = allout-encryption-ciphertext-rejection-regexps
(defvar allout-encryption-ciphertext-rejection-regexps nil
  "Variable for regexps matching plaintext to remove before encryption.

This is used to detect strings in encryption results that would
register as allout mode structural elements, for example, as a
topic prefix.

Entries must be symbols that are bound to the desired regexp values.

Encryptions that result in matches will be retried, up to
`allout-encryption-ciphertext-rejection-limit' times, after which
an error is raised.")

(make-variable-buffer-local 'allout-encryption-ciphertext-rejection-regexps)
;;;_   = allout-encryption-ciphertext-rejection-ceiling
(defvar allout-encryption-ciphertext-rejection-ceiling 5
  "Limit on number of times encryption ciphertext is rejected.

See `allout-encryption-ciphertext-rejection-regexps' for rejection reasons.")
(make-variable-buffer-local 'allout-encryption-ciphertext-rejection-ceiling)
;;;_   > allout-mode-p ()
;; Must define this macro above any uses, or byte compilation will lack
;; proper def, if file isn't loaded -- eg, during emacs build!
;;;###autoload
(defmacro allout-mode-p ()
  "Return t if `allout-mode' is active in current buffer."
  'allout-mode)
;;;_   > allout-write-contents-hook-handler ()
(defun allout-write-contents-hook-handler ()
  "Implement `allout-encrypt-unencrypted-on-saves' for file writes

Return nil if all goes smoothly, or else return an informative
message if an error is encountered.  The message will serve as a
non-nil return on `write-contents-functions' to prevent saving of
the buffer while it has decrypted content.

This behavior depends on Emacs versions that implement the
`write-contents-functions' hook."

  (if (or (not (allout-mode-p))
          (not (boundp 'allout-encrypt-unencrypted-on-saves))
          (not allout-encrypt-unencrypted-on-saves))
      nil
    (if (save-excursion (goto-char (point-min))
                        (allout-next-topic-pending-encryption))
        (progn
          (message "auto-encrypting pending topics")
          (sit-for 0)
          (condition-case failure
              (progn
                (setq allout-after-save-decrypt
                      (allout-encrypt-decrypted))
                ;; aok - return nil:
                nil)
            (error
             ;; whoops - probably some still-decrypted items, return non-nil:
             (let ((text (format (concat "%s contents write inhibited due to"
                                         " encrypted topic encryption error:"
                                         " %s")
                                 (buffer-name (current-buffer))
                                 failure)))
               (message text)(sit-for 2)
               text)))))
    ))
;;;_   > allout-after-saves-handler ()
(defun allout-after-saves-handler ()
  "Decrypt topic encrypted for save, if it's currently being edited.

Ie, if it was pending encryption and contained the point in its body before
the save.

We use values stored in `allout-after-save-decrypt' to locate the topic
and the place for the cursor after the decryption is done."
  (if (not (and (allout-mode-p)
                (boundp 'allout-after-save-decrypt)
                allout-after-save-decrypt))
      t
    (goto-char (car allout-after-save-decrypt))
    (let ((was-modified (buffer-modified-p)))
      (allout-toggle-subtree-encryption)
      (if (not was-modified)
          (set-buffer-modified-p nil)))
    (goto-char (cadr allout-after-save-decrypt))
    (setq allout-after-save-decrypt nil))
  )
;;;_   > allout-called-interactively-p ()
(defmacro allout-called-interactively-p ()
  "A version of `called-interactively-p' independent of Emacs version."
  ;; ... to ease maintenance of allout without betraying deprecation.
  (if (ignore-errors (called-interactively-p 'interactive) t)
      '(called-interactively-p 'interactive)
    '(called-interactively-p)))
;;;_   = allout-inhibit-aberrance-doublecheck nil
;; In some exceptional moments, disparate topic depths need to be allowed
;; momentarily, eg when one topic is being yanked into another and they're
;; about to be reconciled.  let-binding allout-inhibit-aberrance-doublecheck
;; prevents the aberrance doublecheck to allow, eg, the reconciliation
;; processing to happen in the presence of such discrepancies.  It should
;; almost never be needed, however.
(defvar allout-inhibit-aberrance-doublecheck nil
  "Internal state, for momentarily inhibits aberrance doublecheck.

This should only be momentarily let-bound non-nil, not set
non-nil in a lasting way.")

;;;_ #2 Mode environment and activation
;;;_  = allout-explicitly-deactivated
(defvar allout-explicitly-deactivated nil
  "If t, `allout-mode's last deactivation was deliberate.
So `allout-post-command-business' should not reactivate it...")
(make-variable-buffer-local 'allout-explicitly-deactivated)
;;;_  > allout-init (mode)
(defun allout-init (mode)
  "DEPRECATED - configure allout activation by customizing
`allout-auto-activation'.  This function remains around, limited
from what it did before, for backwards compatibility.

MODE is the activation mode - see `allout-auto-activation' for
valid values."
  (declare (obsolete allout-auto-activation "23.3"))
  (customize-set-variable 'allout-auto-activation (format "%s" mode))
  (format "%s" mode))

;;;_  > allout-setup-menubar ()
(defun allout-setup-menubar ()
  "Populate the current buffer's menubar with `allout-mode' stuff."
  (let ((menus (list allout-mode-exposure-menu
		     allout-mode-editing-menu
		     allout-mode-navigation-menu
		     allout-mode-misc-menu))
	cur)
    (while menus
      (setq cur (car menus)
	    menus (cdr menus))
      (easy-menu-add cur))))
;;;_  > allout-overlay-preparations
(defun allout-overlay-preparations ()
  "Set the properties of the allout invisible-text overlay and others."
  (setplist 'allout-exposure-category nil)
  (put 'allout-exposure-category 'invisible 'allout)
  (put 'allout-exposure-category 'evaporate t)
  ;; ??? We use isearch-open-invisible *and* isearch-mode-end-hook.  The
  ;; latter would be sufficient, but it seems that a separate behavior --
  ;; the _transient_ opening of invisible text during isearch -- is keyed to
  ;; presence of the isearch-open-invisible property -- even though this
  ;; property controls the isearch _arrival_ behavior.  This is the case at
  ;; least in emacs 21, 22.1, and xemacs 21.4.
  (put 'allout-exposure-category 'isearch-open-invisible
       'allout-isearch-end-handler)
  (put 'allout-exposure-category 'insert-in-front-hooks
       '(allout-overlay-insert-in-front-handler))
  (put 'allout-exposure-category 'modification-hooks
       '(allout-overlay-interior-modification-handler)))
;;;_  > define-minor-mode allout-mode
;;;_   : Defun:
;;;###autoload
(define-minor-mode allout-mode
;;;_    . Doc string:
  "Toggle Allout outline mode.

\\<allout-mode-map-value>
Allout outline mode is a minor mode that provides extensive
outline oriented formatting and manipulation.  It enables
structural editing of outlines, as well as navigation and
exposure.  It also is specifically aimed at accommodating
syntax-sensitive text like programming languages.  (For example,
see the allout code itself, which is organized as an allout
outline.)

In addition to typical outline navigation and exposure, allout includes:

 - topic-oriented authoring, including keystroke-based topic creation,
   repositioning, promotion/demotion, cut, and paste
 - incremental search with dynamic exposure and reconcealment of hidden text
 - adjustable format, so programming code can be developed in outline-structure
 - easy topic encryption and decryption, symmetric or key-pair
 - \"Hot-spot\" operation, for single-keystroke maneuvering and exposure control
 - integral outline layout, for automatic initial exposure when visiting a file
 - independent extensibility, using comprehensive exposure and authoring hooks

and many other features.

Below is a description of the key bindings, and then description
of special `allout-mode' features and terminology.  See also the
outline menubar additions for quick reference to many of the
features.  Customize `allout-auto-activation' to prepare your
Emacs session for automatic activation of `allout-mode'.

The bindings are those listed in `allout-prefixed-keybindings'
and `allout-unprefixed-keybindings'.  We recommend customizing
`allout-command-prefix' to use just `\\C-c' as the command
prefix, if the allout bindings don't conflict with any personal
bindings you have on \\C-c.  In any case, outline structure
navigation and authoring is simplified by positioning the cursor
on an item's bullet character, the \"hot-spot\" -- then you can
invoke allout commands with just the un-prefixed,
un-control-shifted command letters.  This is described further in
the HOT-SPOT Operation section.

        Exposure Control:
        ----------------
\\[allout-hide-current-subtree]   `allout-hide-current-subtree'
\\[allout-show-children] `allout-show-children'
\\[allout-show-current-subtree] `allout-show-current-subtree'
\\[allout-show-current-entry] `allout-show-current-entry'
\\[allout-show-all]   `allout-show-all'

        Navigation:
        ----------
\\[allout-next-visible-heading] `allout-next-visible-heading'
\\[allout-previous-visible-heading] `allout-previous-visible-heading'
\\[allout-up-current-level] `allout-up-current-level'
\\[allout-forward-current-level] `allout-forward-current-level'
\\[allout-backward-current-level] `allout-backward-current-level'
\\[allout-end-of-entry] `allout-end-of-entry'
\\[allout-beginning-of-current-entry] `allout-beginning-of-current-entry' (alternately, goes to hot-spot)
\\[allout-beginning-of-line]  `allout-beginning-of-line' -- like regular beginning-of-line, but
     if immediately repeated cycles to the beginning of the current item
     and then to the hot-spot (if `allout-beginning-of-line-cycles' is set).


        Topic Header Production:
        -----------------------
\\[allout-open-sibtopic] `allout-open-sibtopic' Create a new sibling after current topic.
\\[allout-open-subtopic]   `allout-open-subtopic' ... an offspring of current topic.
\\[allout-open-supertopic] `allout-open-supertopic' ... a sibling of the current topic's parent.

        Topic Level and Prefix Adjustment:
        ---------------------------------
\\[allout-shift-in] `allout-shift-in'   Shift current topic and all offspring deeper
\\[allout-shift-out] `allout-shift-out' ... less deep
\\[allout-rebullet-current-heading] `allout-rebullet-current-heading' Prompt for alternate bullet for
            current topic
\\[allout-rebullet-topic] `allout-rebullet-topic'   Reconcile bullets of topic and
            its offspring -- distinctive bullets are not changed, others
            are alternated according to nesting depth.
\\[allout-number-siblings] `allout-number-siblings'  Number bullets of topic and siblings --
           the offspring are not affected.
           With repeat count, revoke numbering.

        Topic-oriented Killing and Yanking:
        ----------------------------------
\\[allout-kill-topic] `allout-kill-topic'   Kill current topic, including offspring.
\\[allout-copy-topic-as-kill] `allout-copy-topic-as-kill' Copy current topic, including offspring.
\\[allout-kill-line]     `allout-kill-line'    Kill line, attending to outline structure.
\\[allout-copy-line-as-kill]     `allout-copy-line-as-kill' Copy line but don't delete it.
\\[allout-yank] `allout-yank'        Yank, adjusting depth of yanked topic to
                             depth of heading if yanking into bare topic
                             heading (ie, prefix sans text).
\\[allout-yank-pop]     `allout-yank-pop'       Is to `allout-yank' as `yank-pop' is to `yank'.

        Topic-oriented Encryption:
        -------------------------
\\[allout-toggle-current-subtree-encryption] `allout-toggle-current-subtree-encryption'
          Encrypt/Decrypt topic content

        Misc commands:
        -------------
M-x outlineify-sticky       Activate outline mode for current buffer,
                            and establish a default file-var setting
                            for `allout-layout'.
\\[allout-mark-topic]       `allout-mark-topic'
\\[allout-copy-exposed-to-buffer] `allout-copy-exposed-to-buffer'
                            Duplicate outline, sans concealed text, to
                            buffer with name derived from derived from that
                            of current buffer -- \"*BUFFERNAME exposed*\".
\\[allout-flatten-exposed-to-buffer] `allout-flatten-exposed-to-buffer'
                            Like above `copy-exposed', but convert topic
                            prefixes to section.subsection... numeric
                            format.
\\[customize-variable] allout-auto-activation
                            Prepare Emacs session for allout outline mode
                            auto-activation.

                  Topic Encryption

Outline mode supports gpg encryption of topics, with support for
symmetric and key-pair modes, and auto-encryption of topics
pending encryption on save.

Topics pending encryption are, by default, automatically
encrypted during file saves, including checkpoint saves, to avoid
exposing the plain text of encrypted topics in the file system.
If the content of the topic containing the cursor was encrypted
for a save, it is automatically decrypted for continued editing.

NOTE: A few GnuPG v2 versions improperly preserve incorrect
symmetric decryption keys, preventing entry of the correct key on
subsequent decryption attempts until the cache times-out.  That
can take several minutes.  (Decryption of other entries is not
affected.)  Upgrade your EasyPG version, if you can, and you can
deliberately clear your gpg-agent's cache by sending it a `-HUP'
signal.

See `allout-toggle-current-subtree-encryption' function docstring
and `allout-encrypt-unencrypted-on-saves' customization variable
for details.

                 HOT-SPOT Operation

Hot-spot operation provides a means for easy, single-keystroke outline
navigation and exposure control.

When the text cursor is positioned directly on the bullet character of
a topic, regular characters (a to z) invoke the commands of the
corresponding allout-mode keymap control chars.  For example, \"f\"
would invoke the command typically bound to \"C-c<space>C-f\"
\(\\[allout-forward-current-level] `allout-forward-current-level').

Thus, by positioning the cursor on a topic bullet, you can
execute the outline navigation and manipulation commands with a
single keystroke.  Regular navigation keys (eg, \\[forward-char], \\[next-line]) don't get
this special translation, so you can use them to get out of the
hot-spot and back to normal editing operation.

In allout-mode, the normal beginning-of-line command (\\[allout-beginning-of-line]) is
replaced with one that makes it easy to get to the hot-spot.  If you
repeat it immediately it cycles (if `allout-beginning-of-line-cycles'
is set) to the beginning of the item and then, if you hit it again
immediately, to the hot-spot.  Similarly, `allout-beginning-of-current-entry'
\(\\[allout-beginning-of-current-entry]) moves to the hot-spot when the cursor is already located
at the beginning of the current entry.

                             Extending Allout

Allout exposure and authoring activities all have associated
hooks, by which independent code can cooperate with allout
without changes to the allout core.  Here are key ones:

`allout-mode-hook'
`allout-mode-deactivate-hook' (deprecated)
`allout-mode-off-hook'
`allout-exposure-change-functions'
`allout-structure-added-functions'
`allout-structure-deleted-functions'
`allout-structure-shifted-functions'
`allout-after-copy-or-kill-hook'
`allout-post-undo-hook'

                            Terminology

Topic hierarchy constituents -- TOPICS and SUBTOPICS:

ITEM:   A unitary outline element, including the HEADER and ENTRY text.
TOPIC:  An ITEM and any ITEMs contained within it, ie having greater DEPTH
        and with no intervening items of lower DEPTH than the container.
CURRENT ITEM:
        The visible ITEM most immediately containing the cursor.
DEPTH:  The degree of nesting of an ITEM; it increases with containment.
        The DEPTH is determined by the HEADER PREFIX.  The DEPTH is also
        called the:
LEVEL:  The same as DEPTH.

ANCESTORS:
        Those ITEMs whose TOPICs contain an ITEM.
PARENT: An ITEM's immediate ANCESTOR.  It has a DEPTH one less than that
        of the ITEM.
OFFSPRING:
        The ITEMs contained within an ITEM's TOPIC.
SUBTOPIC:
        An OFFSPRING of its ANCESTOR TOPICs.
CHILD:
        An immediate SUBTOPIC of its PARENT.
SIBLINGS:
        TOPICs having the same PARENT and DEPTH.

Topic text constituents:

HEADER: The first line of an ITEM, include the ITEM PREFIX and HEADER
        text.
ENTRY:  The text content of an ITEM, before any OFFSPRING, but including
        the HEADER text and distinct from the ITEM PREFIX.
BODY:   Same as ENTRY.
PREFIX: The leading text of an ITEM which distinguishes it from normal
        ENTRY text.  Allout recognizes the outline structure according
        to the strict PREFIX format.  It consists of a PREFIX-LEAD string,
        PREFIX-PADDING, and a BULLET.  The BULLET might be followed by a
        number, indicating the ordinal number of the topic among its
        siblings, or an asterisk indicating encryption, plus an optional
        space.  After that is the ITEM HEADER text, which is not part of
        the PREFIX.

        The relative length of the PREFIX determines the nesting DEPTH
        of the ITEM.
PREFIX-LEAD:
        The string at the beginning of a HEADER PREFIX, by default a `.'.
        It can be customized by changing the setting of
        `allout-header-prefix' and then reinitializing `allout-mode'.

        When the PREFIX-LEAD is set to the comment-string of a
        programming language, outline structuring can be embedded in
        program code without interfering with processing of the text
        (by Emacs or the language processor) as program code.  This
        setting happens automatically when allout mode is used in
        programming-mode buffers.  See `allout-use-mode-specific-leader'
        docstring for more detail.
PREFIX-PADDING:
        Spaces or asterisks which separate the PREFIX-LEAD and the
        bullet, determining the ITEM's DEPTH.
BULLET: A character at the end of the ITEM PREFIX, it must be one of
        the characters listed on `allout-plain-bullets-string' or
        `allout-distinctive-bullets-string'.  When creating a TOPIC,
        plain BULLETs are by default used, according to the DEPTH of the
        TOPIC.  Choice among the distinctive BULLETs is offered when you
        provide a universal argument (\\[universal-argument]) to the
        TOPIC creation command, or when explicitly rebulleting a TOPIC.  The
        significance of the various distinctive bullets is purely by
        convention.  See the documentation for the above bullet strings for
        more details.
EXPOSURE:
        The state of a TOPIC which determines the on-screen visibility
        of its OFFSPRING and contained ENTRY text.
CONCEALED:
        TOPICs and ENTRY text whose EXPOSURE is inhibited.  Concealed
        text is represented by \"...\" ellipses.

        CONCEALED TOPICs are effectively collapsed within an ANCESTOR.
CLOSED: A TOPIC whose immediate OFFSPRING and body-text is CONCEALED.
OPEN:	A TOPIC that is not CLOSED, though its OFFSPRING or BODY may be."
;;;_    . Code
  :lighter " Allout"
  :keymap 'allout-mode-map

  (let ((use-layout (if (listp allout-layout)
                        allout-layout
                      allout-default-layout)))

    (if (not (allout-mode-p))
        (progn
          ;; Deactivation:

                                        ; Activation not explicitly
                                        ; requested, and either in
                                        ; active state or *de*activation
                                        ; specifically requested:
          (allout-do-resumptions)

          (remove-from-invisibility-spec '(allout . t))
          (remove-hook 'pre-command-hook 'allout-pre-command-business t)
          (remove-hook 'post-command-hook 'allout-post-command-business t)
          (remove-hook 'before-change-functions 'allout-before-change-handler t)
          (remove-hook 'isearch-mode-end-hook 'allout-isearch-end-handler t)
          (remove-hook 'write-contents-functions
                       'allout-write-contents-hook-handler t)

          (remove-overlays (point-min) (point-max)
                           'category 'allout-exposure-category))

      ;; Activating:
      (if allout-old-style-prefixes
          ;; Inhibit all the fancy formatting:
          (allout-add-resumptions '(allout-primary-bullet "*")))

      (allout-overlay-preparations)     ; Doesn't hurt to redo this.

      (allout-infer-header-lead-and-primary-bullet)
      (allout-infer-body-reindent)

      (allout-set-regexp)
      (allout-add-resumptions '(allout-encryption-ciphertext-rejection-regexps
                                allout-line-boundary-regexp
                                extend)
                              '(allout-encryption-ciphertext-rejection-regexps
                                allout-bob-regexp
                                extend))

      (allout-compose-and-institute-keymap)
      (allout-produce-mode-menubar-entries)

      (add-to-invisibility-spec '(allout . t))

      (allout-add-resumptions '(line-move-ignore-invisible t))
      (add-hook 'pre-command-hook 'allout-pre-command-business nil t)
      (add-hook 'post-command-hook 'allout-post-command-business nil t)
      (add-hook 'before-change-functions 'allout-before-change-handler nil t)
      (add-hook 'isearch-mode-end-hook 'allout-isearch-end-handler nil t)
      (add-hook 'write-contents-functions 'allout-write-contents-hook-handler
                nil t)

      ;; Stash auto-fill settings and adjust so custom allout auto-fill
      ;; func will be used if auto-fill is active or activated.  (The
      ;; custom func respects topic headline, maintains hanging-indents,
      ;; etc.)
      (allout-add-resumptions (list 'allout-former-auto-filler
                                    auto-fill-function)
                              ;; Register allout-auto-fill to be used if
                              ;; filling is active:
                              (list 'allout-outside-normal-auto-fill-function
                                    normal-auto-fill-function)
                              '(normal-auto-fill-function allout-auto-fill)
                              ;; Paragraphs are broken by topic headlines.
                              (list 'paragraph-start
                                    (concat paragraph-start "\\|^\\("
                                            allout-regexp "\\)"))
                              (list 'paragraph-separate
                                    (concat paragraph-separate "\\|^\\("
                                            allout-regexp "\\)")))
      (if (and auto-fill-function (not allout-inhibit-auto-fill))
          ;; allout-auto-fill will use the stashed values and so forth.
          (allout-add-resumptions '(auto-fill-function allout-auto-fill)))

      (allout-setup-menubar)

      ;; Do auto layout if warranted:
      (when (and allout-layout
                 allout-auto-activation
                 use-layout
                 (and (not (string= allout-auto-activation "activate"))
                      (if (string= allout-auto-activation "ask")
                          (if (y-or-n-p (format-message
                                         "Expose %s with layout `%s'? "
                                         (buffer-name) use-layout))
                              t
                            (message "Skipped %s layout." (buffer-name))
                            nil)
                        t)))
        (save-excursion
          (message "Adjusting `%s' exposure..." (buffer-name))
          (goto-char 0)
          (allout-this-or-next-heading)
          (condition-case err
              (progn
                (apply 'allout-expose-topic (list use-layout))
                (message "Adjusting `%s' exposure... done."
                         (buffer-name)))
            ;; Problem applying exposure -- notify user, but don't
            ;; interrupt, eg, file visit:
            (error (message "%s" (car (cdr err)))
                   (sit-for 1))))
        )                               ; when allout-layout
      )					; if (allout-mode-p)
    )                                   ; let (())
  )  					; define-minor-mode
;;;_  > allout-minor-mode alias
(defalias 'allout-minor-mode 'allout-mode)
;;;_  > allout-unload-function
(defun allout-unload-function ()
  "Unload the allout outline library."
  (save-current-buffer
    (dolist (buffer (buffer-list))
      (set-buffer buffer)
      (when (allout-mode-p) (allout-mode -1))))
  ;; continue standard unloading
  nil)

;;;_  - Position Assessment
;;;_   > allout-hidden-p (&optional pos)
(defsubst allout-hidden-p (&optional pos)
  "Non-nil if the character after point was made invisible by allout."
  (eq (get-char-property (or pos (point)) 'invisible) 'allout))

;;;_  > allout-overlay-insert-in-front-handler (ol after beg end
;;;                                                &optional prelen)
(defun allout-overlay-insert-in-front-handler (ol after beg _end
                                                  &optional _prelen)
  "Shift the overlay so stuff inserted in front of it is excluded."
  (if after
      ;; ??? Shouldn't moving the overlay should be unnecessary, if overlay
      ;;     front-advance on the overlay worked as expected?
      (move-overlay ol (1+ beg) (overlay-end ol))))
;;;_  > allout-overlay-interior-modification-handler (ol after beg end
;;;                                                      &optional prelen)
(defun allout-overlay-interior-modification-handler (ol after beg end
                                                        &optional _prelen)
  "Get confirmation before making arbitrary changes to invisible text.

We expose the invisible text and ask for confirmation.  Refusal or
`keyboard-quit' abandons the changes, with keyboard-quit additionally
reclosing the opened text.

No confirmation is necessary when `inhibit-read-only' is set -- eg, allout
internal functions use this feature cohesively bunch changes."

  (when (and (not inhibit-read-only) (not after))
    (let ((start (point))
          (ol-start (overlay-start ol))
          (ol-end (overlay-end ol))
          first)
      (goto-char beg)
      (while (< (point) end)
        (when (allout-hidden-p)
          (allout-show-to-offshoot)
          (if (allout-hidden-p)
              (save-excursion (forward-char 1)
                              (allout-show-to-offshoot)))
          (when (not first)
            (setq first (point))))
        (goto-char (next-char-property-change (1+ (point)) end)))
      (when first
        (goto-char first)
        (condition-case nil
            (if (not
                 (yes-or-no-p
                  (substitute-command-keys
                   (concat "Modify concealed text?  (\"no\" just aborts,"
                           " \\[keyboard-quit] also reconceals) "))))
                (progn (goto-char start)
                       (error "Concealed-text change refused")))
          (quit (allout-flag-region ol-start ol-end nil)
                (allout-flag-region ol-start ol-end t)
                (error "Concealed-text change abandoned, text reconcealed"))))
      (goto-char start))))
;;;_  > allout-before-change-handler (beg end)
(defun allout-before-change-handler (beg end)
  "Protect against changes to invisible text.

See `allout-overlay-interior-modification-handler' for details."

  (when (and (allout-mode-p) undo-in-progress)
    (setq allout-just-did-undo t)
    (if (allout-hidden-p)
        (allout-show-children))))
;;;_  > allout-isearch-end-handler (&optional overlay)
(defun allout-isearch-end-handler (&optional _overlay)
  "Reconcile allout outline exposure on arriving in hidden text after isearch.

Optional OVERLAY parameter is for when this function is used by
`isearch-open-invisible' overlay property.  It is otherwise unused, so this
function can also be used as an `isearch-mode-end-hook'."

  (if (and (allout-mode-p) (allout-hidden-p))
      (allout-show-to-offshoot)))

;;;_ #3 Internal Position State-Tracking -- "allout-recent-*" funcs
;; All the basic outline functions that directly do string matches to
;; evaluate heading prefix location set the variables
;; `allout-recent-prefix-beginning'  and `allout-recent-prefix-end'
;; when successful.  Functions starting with `allout-recent-' all
;; use this state, providing the means to avoid redundant searches
;; for just-established data.  This optimization can provide
;; significant speed improvement, but it must be employed carefully.
;;;_  = allout-recent-prefix-beginning
(defvar allout-recent-prefix-beginning 0
  "Buffer point of the start of the last topic prefix encountered.")
(make-variable-buffer-local 'allout-recent-prefix-beginning)
;;;_  = allout-recent-prefix-end
(defvar allout-recent-prefix-end 0
  "Buffer point of the end of the last topic prefix encountered.")
(make-variable-buffer-local 'allout-recent-prefix-end)
;;;_  = allout-recent-depth
(defvar allout-recent-depth 0
  "Depth of the last topic prefix encountered.")
(make-variable-buffer-local 'allout-recent-depth)
;;;_  = allout-recent-end-of-subtree
(defvar allout-recent-end-of-subtree 0
  "Buffer point last returned by `allout-end-of-current-subtree'.")
(make-variable-buffer-local 'allout-recent-end-of-subtree)
;;;_  > allout-prefix-data ()
(defsubst allout-prefix-data ()
  "Register allout-prefix state data.

For reference by `allout-recent' funcs.  Return
the new value of `allout-recent-prefix-beginning'."
  (setq allout-recent-prefix-end (or (match-end 1) (match-end 2) (match-end 3))
        allout-recent-prefix-beginning (or (match-beginning 1)
                                           (match-beginning 2)
                                           (match-beginning 3))
        allout-recent-depth (max 1 (- allout-recent-prefix-end
                                      allout-recent-prefix-beginning
                                      allout-header-subtraction)))
  allout-recent-prefix-beginning)
;;;_  > allout-nullify-prefix-data ()
(defsubst allout-nullify-prefix-data ()
  "Mark allout prefix data as being uninformative."
  (setq allout-recent-prefix-end (point)
        allout-recent-prefix-beginning (point)
        allout-recent-depth 0)
  allout-recent-prefix-beginning)
;;;_  > allout-recent-depth ()
(defsubst allout-recent-depth ()
  "Return depth of last heading encountered by an outline maneuvering function.

All outline functions which directly do string matches to assess
headings set the variables `allout-recent-prefix-beginning' and
`allout-recent-prefix-end' if successful.  This function uses those settings
to return the current depth."

  allout-recent-depth)
;;;_  > allout-recent-prefix ()
(defsubst allout-recent-prefix ()
  "Like `allout-recent-depth', but returns text of last encountered prefix.

All outline functions which directly do string matches to assess
headings set the variables `allout-recent-prefix-beginning' and
`allout-recent-prefix-end' if successful.  This function uses those settings
to return the current prefix."
  (buffer-substring-no-properties allout-recent-prefix-beginning
                                  allout-recent-prefix-end))
;;;_  > allout-recent-bullet ()
(defmacro allout-recent-bullet ()
  "Like `allout-recent-prefix', but returns bullet of last encountered prefix.

All outline functions which directly do string matches to assess
headings set the variables `allout-recent-prefix-beginning' and
`allout-recent-prefix-end' if successful.  This function uses those settings
to return the current depth of the most recently matched topic."
  '(buffer-substring-no-properties (1- allout-recent-prefix-end)
                                   allout-recent-prefix-end))

;;;_ #4 Navigation

;;;_  - Position Assessment
;;;_   : Location Predicates
;;;_    > allout-do-doublecheck ()
(defsubst allout-do-doublecheck ()
  "True if current item conditions qualify for checking on topic aberrance."
  (and
   ;; presume integrity of outline and yanked content during yank -- necessary
   ;; to allow for level disparity of yank location and yanked text:
   (not allout-inhibit-aberrance-doublecheck)
   ;; allout-doublecheck-at-and-shallower is ceiling for doublecheck:
   (<= allout-recent-depth allout-doublecheck-at-and-shallower)))
;;;_     > allout-aberrant-container-p ()
(defun allout-aberrant-container-p ()
  "True if topic, or next sibling with children, contains them discontinuously.

Discontinuous means an immediate offspring that is nested more
than one level deeper than the topic.

If topic has no offspring, then the next sibling with offspring will
determine whether or not this one is determined to be aberrant.

If true, then the allout-recent-* settings are calibrated on the
offspring that qualifies it as aberrant, ie with depth that
exceeds the topic by more than one."

  ;; This is most clearly understood when considering standard-prefix-leader
  ;; low-level topics, which can all too easily match text not intended as
  ;; headers.  For example, any line with a leading '.' or '*' and lacking a
  ;; following bullet qualifies without this protection.  (A sequence of
  ;; them can occur naturally, eg a typical textual bullet list.)  We
  ;; disqualify such low-level sequences when they are followed by a
  ;; discontinuously contained child, inferring that the sequences are not
  ;; actually connected with their prospective context.

  (let ((depth (allout-depth))
        (start-point (point))
        done aberrant)
    (save-match-data
      (save-excursion
        (while (and (not done)
                    (re-search-forward allout-line-boundary-regexp nil 0))
          (allout-prefix-data)
          (goto-char allout-recent-prefix-beginning)
          (cond
           ;; sibling -- continue:
           ((eq allout-recent-depth depth))
           ;; first offspring is excessive -- aberrant:
           ((> allout-recent-depth (1+ depth))
            (setq done t aberrant t))
           ;; next non-sibling is lower-depth -- not aberrant:
           (t (setq done t))))))
    (if aberrant
        aberrant
      (goto-char start-point)
      ;; recalibrate allout-recent-*
      (allout-depth)
      nil)))
;;;_    > allout-on-current-heading-p ()
(defun allout-on-current-heading-p ()
  "Return non-nil if point is on current visible topics' header line.

Actually, returns prefix beginning point."
  (save-excursion
    (allout-beginning-of-current-line)
    (save-match-data
      (and (looking-at allout-regexp)
           (allout-prefix-data)
           (or (not (allout-do-doublecheck))
               (not (allout-aberrant-container-p)))))))
;;;_    > allout-on-heading-p ()
(defalias 'allout-on-heading-p 'allout-on-current-heading-p)
;;;_    > allout-e-o-prefix-p ()
(defun allout-e-o-prefix-p ()
  "True if point is located where current topic prefix ends, heading begins."
  (and (save-match-data
        (save-excursion (let ((inhibit-field-text-motion t))
                          (beginning-of-line))
                        (looking-at allout-regexp))
       (= (point) (save-excursion (allout-end-of-prefix)(point))))))
;;;_   : Location attributes
;;;_    > allout-depth ()
(defun allout-depth ()
  "Return depth of topic most immediately containing point.

Does not do doublecheck for aberrant topic header.

Return zero if point is not within any topic.

Like `allout-current-depth', but respects hidden as well as visible topics."
  (save-excursion
    (let ((start-point (point)))
      (if (and (allout-goto-prefix)
               (not (< start-point (point))))
          allout-recent-depth
        (progn
          ;; Oops, no prefix, nullify it:
          (allout-nullify-prefix-data)
          ;; ... and return 0:
          0)))))
;;;_    > allout-current-depth ()
(defun allout-current-depth ()
  "Return depth of visible topic most immediately containing point.

Return zero if point is not within any topic."
  (save-excursion
    (if (allout-back-to-current-heading)
        (max 1
             (- allout-recent-prefix-end
                allout-recent-prefix-beginning
                allout-header-subtraction))
      0)))
;;;_    > allout-get-current-prefix ()
(defun allout-get-current-prefix ()
  "Topic prefix of the current topic."
  (save-excursion
    (if (allout-goto-prefix)
	(allout-recent-prefix))))
;;;_    > allout-get-bullet ()
(defun allout-get-bullet ()
  "Return bullet of containing topic (visible or not)."
  (save-excursion
    (and (allout-goto-prefix)
	 (allout-recent-bullet))))
;;;_    > allout-current-bullet ()
(defun allout-current-bullet ()
  "Return bullet of current (visible) topic heading, or none if none found."
  (condition-case nil
      (save-excursion
	(allout-back-to-current-heading)
	(buffer-substring-no-properties (- allout-recent-prefix-end 1)
                                        allout-recent-prefix-end))
    ;; Quick and dirty provision, ostensibly for missing bullet:
    (args-out-of-range nil))
  )
;;;_    > allout-get-prefix-bullet (prefix)
(defun allout-get-prefix-bullet (prefix)
  "Return the bullet of the header prefix string PREFIX."
  ;; Doesn't make sense if we're old-style prefixes, but this just
  ;; oughtn't be called then, so forget about it...
  (if (string-match allout-regexp prefix)
      (substring prefix (1- (match-end 2)) (match-end 2))))
;;;_    > allout-sibling-index (&optional depth)
(defun allout-sibling-index (&optional depth)
  "Item number of this prospective topic among its siblings.

If optional arg DEPTH is greater than current depth, then we're
opening a new level, and return 0.

If less than this depth, ascend to that depth and count..."

  (save-excursion
    (cond ((and depth (<= depth 0) 0))
          ((or (null depth) (= depth (allout-depth)))
           (let ((index 1))
             (while (allout-previous-sibling allout-recent-depth nil)
	       (setq index (1+ index)))
             index))
          ((< depth allout-recent-depth)
           (allout-ascend-to-depth depth)
           (allout-sibling-index))
          (0))))
;;;_    > allout-topic-flat-index ()
(defun allout-topic-flat-index ()
  "Return a list indicating point's numeric section.subsect.subsubsect...
Outermost is first."
  (let* ((depth (allout-depth))
	 (next-index (allout-sibling-index depth))
	 (rev-sibls nil))
    (while (> next-index 0)
      (setq rev-sibls (cons next-index rev-sibls))
      (setq depth (1- depth))
      (setq next-index (allout-sibling-index depth)))
    rev-sibls)
  )

;;;_  - Navigation routines
;;;_   > allout-beginning-of-current-line ()
(defun allout-beginning-of-current-line ()
  "Like beginning of line, but to visible text."

  ;; This combination of move-beginning-of-line and beginning-of-line is
  ;; deliberate, but the (beginning-of-line) may now be superfluous.
  (let ((inhibit-field-text-motion t))
    (move-beginning-of-line 1)
    (beginning-of-line)
    (while (and (not (bobp)) (or (not (bolp)) (allout-hidden-p)))
      (beginning-of-line)
      (if (or (allout-hidden-p) (not (bolp)))
          (forward-char -1)))))
;;;_   > allout-end-of-current-line ()
(defun allout-end-of-current-line ()
  "Move to the end of line, past concealed text if any."
  ;; This is for symmetry with `allout-beginning-of-current-line' --
  ;; `move-end-of-line' doesn't suffer the same problem as
  ;; `move-beginning-of-line'.
  (let ((inhibit-field-text-motion t))
    (end-of-line)
    (while (allout-hidden-p)
      (end-of-line)
      (if (allout-hidden-p) (forward-char 1)))))
;;;_   > allout-beginning-of-line ()
(defun allout-beginning-of-line ()
  "Beginning-of-line with `allout-beginning-of-line-cycles' behavior, if set."

  (interactive)

  (if (or (not allout-beginning-of-line-cycles)
          (not (equal last-command this-command)))
      (progn
        (if (and (not (bolp))
                 (allout-hidden-p (1- (point))))
            (goto-char (previous-single-char-property-change
                        (1- (point)) 'invisible)))
        (move-beginning-of-line 1))
    (allout-depth)
    (let ((beginning-of-body
           (save-excursion
             (while (and (allout-do-doublecheck)
                         (allout-aberrant-container-p)
                         (allout-previous-visible-heading 1)))
             (allout-beginning-of-current-entry)
             (point))))
      (cond ((= (current-column) 0)
             (goto-char beginning-of-body))
            ((< (point) beginning-of-body)
             (allout-beginning-of-current-line))
            ((= (point) beginning-of-body)
             (goto-char (allout-current-bullet-pos)))
            (t (allout-beginning-of-current-line)
               (if (< (point) beginning-of-body)
                   ;; we were on the headline after its start:
                   (goto-char beginning-of-body)))))))
;;;_   > allout-end-of-line ()
(defun allout-end-of-line ()
  "End-of-line with `allout-end-of-line-cycles' behavior, if set."

  (interactive)

  (if (or (not allout-end-of-line-cycles)
          (not (equal last-command this-command)))
      (allout-end-of-current-line)
    (let ((end-of-entry (save-excursion
                          (allout-end-of-entry)
                          (point))))
      (cond ((not (eolp))
             (allout-end-of-current-line))
            ((or (allout-hidden-p) (save-excursion
                                     (forward-char -1)
                                     (allout-hidden-p)))
             (allout-back-to-current-heading)
             (allout-show-current-entry)
             (allout-show-children)
             (allout-end-of-entry))
            ((>= (point) end-of-entry)
             (allout-back-to-current-heading)
             (allout-end-of-current-line))
            (t
             (if (not mark-active)
                 (push-mark))
             (allout-end-of-entry))))))

;;;_   > allout-mark-active-p ()
(defun allout-mark-active-p ()
  "True if the mark is currently or always active."
  (declare (obsolete nil "28.1"))
  mark-active)

;;;_   > allout-next-heading ()
(defsubst allout-next-heading ()
  "Move to the heading for the topic (possibly invisible) after this one.

Returns the location of the heading, or nil if none found.

We skip anomalous low-level topics, a la `allout-aberrant-container-p'."
  (save-match-data

    (if (looking-at allout-regexp)
        (forward-char 1))

    (when (re-search-forward allout-line-boundary-regexp nil 0)
      (allout-prefix-data)
      (goto-char allout-recent-prefix-beginning)
      (while (not (bolp))
        (forward-char -1))
      (and (allout-do-doublecheck)
           ;; this will set allout-recent-* on the first non-aberrant topic,
           ;; whether it's the current one or one that disqualifies it:
           (allout-aberrant-container-p))
      ;; this may or may not be the same as above depending on doublecheck:
      (goto-char allout-recent-prefix-beginning))))
;;;_   > allout-this-or-next-heading
(defun allout-this-or-next-heading ()
  "Position cursor on current or next heading."
  ;; A throwaway non-macro that is defined after allout-next-heading
  ;; and usable by allout-mode.
  (if (not (allout-goto-prefix-doublechecked)) (allout-next-heading)))
;;;_   > allout-previous-heading ()
(defun allout-previous-heading ()
  "Move to the prior (possibly invisible) heading line.

Return the location of the beginning of the heading, or nil if not found.

We skip anomalous low-level topics, a la `allout-aberrant-container-p'."

  (if (bobp)
      nil
    (let ((start-point (point)))
      ;; allout-goto-prefix-doublechecked calls us, so we can't use it here.
      (allout-goto-prefix)
      (save-match-data
        (when (or (re-search-backward allout-line-boundary-regexp nil 0)
                  (looking-at allout-bob-regexp))
          (goto-char (allout-prefix-data))
          (if (and (allout-do-doublecheck)
                   (allout-aberrant-container-p))
              (or (allout-previous-heading)
                  (and (goto-char start-point)
                       ;; recalibrate allout-recent-*:
                       (allout-depth)
                       nil))
            (point)))))))
;;;_   > allout-get-invisibility-overlay ()
(defun allout-get-invisibility-overlay ()
  "Return the overlay at point that dictates allout invisibility."
  (let ((overlays (overlays-at (point)))
        got)
    (while (and overlays (not got))
      (if (equal (overlay-get (car overlays) 'invisible) 'allout)
          (setq got (car overlays))
        (pop overlays)))
    got))
;;;_   > allout-back-to-visible-text ()
(defun allout-back-to-visible-text ()
  "Move to most recent prior character that is visible, and return point."
    (if (allout-hidden-p)
      (goto-char (overlay-start (allout-get-invisibility-overlay))))
    (point))

;;;_  - Subtree Charting
;;;_   " These routines either produce or assess charts, which are
;;; nested lists of the locations of topics within a subtree.
;;;
;;; Charts enable efficient subtree navigation by providing a reusable basis
;;; for elaborate, compound assessment and adjustment of a subtree.

;;;_   > allout-chart-subtree (&optional levels visible orig-depth prev-depth)
(defun allout-chart-subtree (&optional levels visible orig-depth prev-depth)
  "Produce a location \"chart\" of subtopics of the containing topic.

Optional argument LEVELS specifies a depth limit (relative to start
depth) for the chart.  Null LEVELS means no limit.

When optional argument VISIBLE is non-nil, the chart includes
only the visible subelements of the charted subjects.

The remaining optional args are for internal use by the function.

Point is left at the end of the subtree.

Charts are used to capture outline structure, so that outline-altering
routines need to assess the structure only once, and then use the chart
for their elaborate manipulations.

The chart entries for the topics are in reverse order, so the
last topic is listed first.  The entry for each topic consists of
an integer indicating the point at the beginning of the topic
prefix.  Charts for offspring consist of a list containing,
recursively, the charts for the respective subtopics.  The chart
for a topics' offspring precedes the entry for the topic itself.

The other function parameters are for internal recursion, and should
not be specified by external callers.  ORIG-DEPTH is depth of topic at
starting point, and PREV-DEPTH is depth of prior topic."

  (let ((original (not orig-depth))	; `orig-depth' set only in recursion.
	chart curr-depth)

    (if original			; Just starting?
					; Register initial settings and
					; position to first offspring:
	(progn (setq orig-depth (allout-depth))
	       (or prev-depth (setq prev-depth (1+ orig-depth)))
               (if visible
                   (allout-next-visible-heading 1)
                 (allout-next-heading))))

    ;; Loop over the current levels' siblings.  Besides being more
    ;; efficient than tail-recursing over a level, it avoids exceeding
    ;; the typically quite constrained Emacs max-lisp-eval-depth.
    ;;
    ;; Probably would speed things up to implement loop-based stack
    ;; operation rather than recursing for lower levels.  Bah.

    (while (and (not (eobp))
					; Still within original topic?
		(< orig-depth (setq curr-depth allout-recent-depth))
		(cond ((= prev-depth curr-depth)
		       ;; Register this one and move on:
		       (setq chart (cons allout-recent-prefix-beginning chart))
		       (if (and levels (<= levels 1))
			   ;; At depth limit -- skip sublevels:
			   (or (allout-next-sibling curr-depth)
			       ;; or no more siblings -- proceed to
			       ;; next heading at lesser depth:
			       (while (and (<= curr-depth
					       allout-recent-depth)
                                           (if visible
                                               (allout-next-visible-heading 1)
                                             (allout-next-heading)))))
                         (if visible
                             (allout-next-visible-heading 1)
                           (allout-next-heading))))

		      ((and (< prev-depth curr-depth)
			    (or (not levels)
				(> levels 0)))
		       ;; Recurse on deeper level of curr topic:
		       (setq chart
			     (cons (allout-chart-subtree (and levels
							       (1- levels))
                                                         visible
                                                         orig-depth
                                                         curr-depth)
				   chart))
		       ;; ... then continue with this one.
		       )

		      ;; ... else nil if we've ascended back to prev-depth.

		      )))

    (if original			; We're at the last sibling on
					; the original level.  Position
					; to the end of it:
	(progn (and (not (eobp)) (forward-char -1))
	       (and (= (preceding-char) ?\n)
		    (= (aref (buffer-substring (max 1 (- (point) 3))
                                               (point))
                             1)
                       ?\n)
		    (forward-char -1))
	       (setq allout-recent-end-of-subtree (point))))

    chart				; (nreverse chart) not necessary,
					; and maybe not preferable.
    ))
;;;_   > allout-chart-siblings (&optional start end)
(defun allout-chart-siblings (&optional _start _end)
  "Produce a list of locations of this and succeeding sibling topics.
Effectively a top-level chart of siblings.  See `allout-chart-subtree'
for an explanation of charts."
  (save-excursion
    (when (allout-goto-prefix-doublechecked)
      (let ((chart (list (point))))
        (while (allout-next-sibling)
          (setq chart (cons (point) chart)))
        (if chart (setq chart (nreverse chart)))))))
;;;_   > allout-chart-to-reveal (chart depth)
(defun allout-chart-to-reveal (chart depth)

  "Return a flat list of hidden points in subtree CHART, up to DEPTH.

If DEPTH is nil, include hidden points at any depth.

Note that point can be left at any of the points on chart, or at the
start point."

  (let (result here)
    (while (and (or (null depth) (> depth 0))
		chart)
      (setq here (car chart))
      (if (listp here)
	  (let ((further (allout-chart-to-reveal here (if (null depth)
                                                          depth
                                                        (1- depth)))))
	    ;; We're on the start of a subtree -- recurse with it, if there's
	    ;; more depth to go:
	    (if further (setq result (append further result)))
	    (setq chart (cdr chart)))
	(goto-char here)
        (if (allout-hidden-p)
	    (setq result (cons here result)))
	(setq chart (cdr chart))))
    result))
;;;_   X allout-chart-spec (chart spec &optional exposing)
;; (defun allout-chart-spec (chart spec &optional exposing)
;;   "Not yet (if ever) implemented.

;; Produce exposure directives given topic/subtree CHART and an exposure SPEC.

;; Exposure spec indicates the locations to be exposed and the prescribed
;; exposure status.  Optional arg EXPOSING is an integer, with 0
;; indicating pending concealment, anything higher indicating depth to
;; which subtopic headers should be exposed, and negative numbers
;; indicating (negative of) the depth to which subtopic headers and
;; bodies should be exposed.

;; The produced list can have two types of entries.  Bare numbers
;; indicate points in the buffer where topic headers that should be
;; exposed reside.

;;  - bare negative numbers indicates that the topic starting at the
;;    point which is the negative of the number should be opened,
;;    including their entries.
;;  - bare positive values indicate that this topic header should be
;;    opened.
;;  - Lists signify the beginning and end points of regions that should
;;    be flagged, and the flag to employ.  (For concealment: `(\?r)', and
;;    exposure:"
;;   (while spec
;;     (cond ((listp spec)
;; 	   )
;; 	  )
;;     (setq spec (cdr spec)))
;;   )

;;;_  - Within Topic
;;;_   > allout-goto-prefix ()
(defun allout-goto-prefix ()
  "Put point at beginning of immediately containing outline topic.

Goes to most immediate subsequent topic if none immediately containing.

Not sensitive to topic visibility.

Returns the point at the beginning of the prefix, or nil if none."

  (save-match-data
    (let (done)
      (while (and (not done)
                  (search-backward "\n" nil 1))
        (forward-char 1)
        (if (looking-at allout-regexp)
            (setq done (allout-prefix-data))
          (forward-char -1)))
      (if (bobp)
          (cond ((looking-at allout-regexp)
                 (allout-prefix-data))
                ((allout-next-heading))
                (done))
        done))))
;;;_   > allout-goto-prefix-doublechecked ()
(defun allout-goto-prefix-doublechecked ()
  "Put point at beginning of immediately containing outline topic.

Like `allout-goto-prefix', but shallow topics (according to
`allout-doublecheck-at-and-shallower') are checked and
disqualified for child containment discontinuity, according to
`allout-aberrant-container-p'."
  (if (allout-goto-prefix)
      (if (and (allout-do-doublecheck)
               (allout-aberrant-container-p))
          (allout-previous-heading)
        (point))))

;;;_   > allout-end-of-prefix ()
(defun allout-end-of-prefix (&optional ignore-decorations)
  "Position cursor at beginning of header text.

If optional IGNORE-DECORATIONS is non-nil, put just after bullet,
otherwise skip white space between bullet and ensuing text."

  (if (not (allout-goto-prefix-doublechecked))
      nil
    (goto-char allout-recent-prefix-end)
    (save-match-data
      (if ignore-decorations
          t
        (while (looking-at "[0-9]") (forward-char 1))
        (if (and (not (eolp)) (looking-at "\\s-")) (forward-char 1))))
    ;; Reestablish where we are:
    (allout-current-depth)))
;;;_   > allout-current-bullet-pos ()
(defun allout-current-bullet-pos ()
  "Return position of current (visible) topic's bullet."

  (if (not (allout-current-depth))
      nil
    (1- allout-recent-prefix-end)))
;;;_   > allout-back-to-current-heading (&optional interactive)
(defun allout-back-to-current-heading (&optional interactive)
  "Move to heading line of current topic, or beginning if not in a topic.

If interactive, we position at the end of the prefix.

Return value of resulting point, unless we started outside
of (before any) topics, in which case we return nil."

  (interactive "p")

  (allout-beginning-of-current-line)
  (let ((bol-point (point)))
    (when (allout-goto-prefix-doublechecked)
      (if (<= (point) bol-point)
          (progn
            (setq bol-point (point))
            (allout-beginning-of-current-line)
            (if (not (= bol-point (point)))
                (if (looking-at allout-regexp)
                    (allout-prefix-data)))
            (if interactive
                (allout-end-of-prefix)
              (point)))
        (goto-char (point-min))
        nil))))
;;;_   > allout-back-to-heading ()
(defalias 'allout-back-to-heading 'allout-back-to-current-heading)
;;;_   > allout-pre-next-prefix ()
(defun allout-pre-next-prefix ()
  "Skip forward to just before the next heading line.

Returns that character position."

  (if (allout-next-heading)
      (goto-char (1- allout-recent-prefix-beginning))))
;;;_   > allout-end-of-subtree (&optional current include-trailing-blank)
(defun allout-end-of-subtree (&optional current include-trailing-blank)
  "Put point at the end of the last leaf in the containing topic.

Optional CURRENT means put point at the end of the containing
visible topic.

Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
any, as part of the subtree.  Otherwise, that trailing blank will be
excluded as delimiting whitespace between topics.

Returns the value of point."
  (interactive "P")
  (if current
      (allout-back-to-current-heading)
    (allout-goto-prefix-doublechecked))
  (let ((level allout-recent-depth))
    (allout-next-heading)
    (while (and (not (eobp))
                (> allout-recent-depth level))
      (allout-next-heading))
    (if (eobp)
        (allout-end-of-entry)
      (forward-char -1))
    (if (and (not include-trailing-blank) (= ?\n (preceding-char)))
         (forward-char -1))
    (setq allout-recent-end-of-subtree (point))))
;;;_   > allout-end-of-current-subtree (&optional include-trailing-blank)
(defun allout-end-of-current-subtree (&optional include-trailing-blank)

  "Put point at end of last leaf in currently visible containing topic.

Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
any, as part of the subtree.  Otherwise, that trailing blank will be
excluded as delimiting whitespace between topics.

Returns the value of point."
  (interactive)
  (allout-end-of-subtree t include-trailing-blank))
;;;_   > allout-beginning-of-current-entry (&optional interactive)
(defun allout-beginning-of-current-entry (&optional interactive)
  "When not already there, position point at beginning of current topic header.

If already there, move cursor to bullet for hot-spot operation.
\(See `allout-mode' doc string for details of hot-spot operation.)"
  (interactive "p")
  (let ((start-point (point)))
    (move-beginning-of-line 1)
    (if (< 0 (allout-current-depth))
        (goto-char allout-recent-prefix-end)
      (goto-char (point-min)))
    (allout-end-of-prefix)
    (if (and interactive
	     (= (point) start-point))
	(goto-char (allout-current-bullet-pos)))))
;;;_   > allout-end-of-entry (&optional inclusive)
(defun allout-end-of-entry (&optional inclusive)
  "Position the point at the end of the current topics' entry.

Optional INCLUSIVE means also include trailing empty line, if any.  When
unset, whitespace between items separates them even when the items are
collapsed."
  (interactive)
  (allout-pre-next-prefix)
  (if (and (not inclusive) (not (bobp)) (= ?\n (preceding-char)))
      (forward-char -1))
  (point))
;;;_   > allout-end-of-current-heading ()
(defun allout-end-of-current-heading ()
  (interactive)
  (allout-beginning-of-current-entry)
  (search-forward "\n" nil t)
  (forward-char -1))
(defalias 'allout-end-of-heading 'allout-end-of-current-heading)
;;;_   > allout-get-body-text ()
(defun allout-get-body-text ()
  "Return the unmangled body text of the topic immediately containing point."
  (save-excursion
    (allout-end-of-prefix)
    (if (not (search-forward "\n" nil t))
        nil
      (backward-char 1)
      (let ((pre-body (point)))
        (if (not pre-body)
            nil
          (allout-end-of-entry t)
          (if (not (= pre-body (point)))
              (buffer-substring-no-properties (1+ pre-body) (point))))
        )
      )
    )
  )

;;;_  - Depth-wise
;;;_   > allout-ascend-to-depth (depth)
(defun allout-ascend-to-depth (depth)
  "Ascend to depth DEPTH, returning depth if successful, nil if not."
  (if (and (> depth 0)(<= depth (allout-depth)))
      (let (last-ascended)
        (while (and (< depth allout-recent-depth)
                    (setq last-ascended (allout-ascend))))
        (goto-char allout-recent-prefix-beginning)
        (if (allout-called-interactively-p) (allout-end-of-prefix))
        (and last-ascended allout-recent-depth))))
;;;_   > allout-ascend (&optional dont-move-if-unsuccessful)
(defun allout-ascend (&optional dont-move-if-unsuccessful)
  "Ascend one level, returning resulting depth if successful, nil if not.

Point is left at the beginning of the level whether or not
successful, unless optional DONT-MOVE-IF-UNSUCCESSFUL is set, in
which case point is returned to its original starting location."
  (if dont-move-if-unsuccessful
      (setq dont-move-if-unsuccessful (point)))
  (prog1
      (if (allout-beginning-of-level)
          (let ((bolevel (point))
                (bolevel-depth allout-recent-depth))
            (allout-previous-heading)
            (cond ((< allout-recent-depth bolevel-depth)
                   allout-recent-depth)
                  ((= allout-recent-depth bolevel-depth)
                   (if dont-move-if-unsuccessful
                       (goto-char dont-move-if-unsuccessful))
                   (allout-depth)
                   nil)
                  (t
                   ;; some topic after very first is lower depth than first:
                   (goto-char bolevel)
                   (allout-depth)
                   nil))))
    (if (allout-called-interactively-p) (allout-end-of-prefix))))
;;;_   > allout-descend-to-depth (depth)
(defun allout-descend-to-depth (depth)
  "Descend to depth DEPTH within current topic.

Returning depth if successful, nil if not."
  (let ((start-point (point))
        (start-depth (allout-depth)))
    (while
        (and (> (allout-depth) 0)
             (not (= depth allout-recent-depth)) ; ... not there yet
             (allout-next-heading)     ; ... go further
             (< start-depth allout-recent-depth))) ; ... still in topic
    (if (and (> (allout-depth) 0)
             (= allout-recent-depth depth))
        depth
      (goto-char start-point)
      nil))
  )
;;;_   > allout-up-current-level (arg)
(defun allout-up-current-level (_arg)
  "Move out ARG levels from current visible topic."
  (interactive "p")
  (let ((start-point (point)))
    (allout-back-to-current-heading)
    (if (not (allout-ascend))
        (progn (goto-char start-point)
               (error "Can't ascend past outermost level"))
      (if (allout-called-interactively-p) (allout-end-of-prefix))
      allout-recent-prefix-beginning)))

;;;_  - Linear
;;;_   > allout-next-sibling (&optional depth backward)
(defun allout-next-sibling (&optional depth backward)
  "Like `allout-forward-current-level', but respects invisible topics.

Traverse at optional DEPTH, or current depth if none specified.

Go backward if optional arg BACKWARD is non-nil.

Return the start point of the new topic if successful, nil otherwise."

  (if (if backward (bobp) (eobp))
      nil
    (let ((target-depth (or depth (allout-depth)))
          (start-point (point))
          (start-prefix-beginning allout-recent-prefix-beginning)
          (count 0)
          leaping
	  last-depth)
      (while (and
              ;; done too few single steps to resort to the leap routine:
              (not leaping)
              ;; not at limit:
              (not (if backward (bobp) (eobp)))
              ;; still traversable:
              (if backward (allout-previous-heading) (allout-next-heading))
              ;; we're below the target depth
              (> (setq last-depth allout-recent-depth) target-depth))
        (setq count (1+ count))
        (if (> count 7)                 ; lists are commonly 7 +- 2, right?-)
            (setq leaping t)))
      (cond (leaping
             (or (allout-next-sibling-leap target-depth backward)
                 (progn
                   (goto-char start-point)
                   (if depth (allout-depth) target-depth)
                   nil)))
            ((and (not (eobp))
                  (and (> (or last-depth (allout-depth)) 0)
                       (= allout-recent-depth target-depth))
                  (not (= start-prefix-beginning
                          allout-recent-prefix-beginning)))
             allout-recent-prefix-beginning)
            (t
             (goto-char start-point)
             (if depth (allout-depth) target-depth)
             nil)))))
;;;_   > allout-next-sibling-leap (&optional depth backward)
(defun allout-next-sibling-leap (&optional depth backward)
  "Like `allout-next-sibling', but by direct search for topic at depth.

Traverse at optional DEPTH, or current depth if none specified.

Go backward if optional arg BACKWARD is non-nil.

Return the start point of the new topic if successful, nil otherwise.

Costs more than regular `allout-next-sibling' for short traversals:

 - we have to check the prior (next, if traveling backwards)
   item to confirm connectivity with the prior topic, and
 - if confirmed, we have to reestablish the allout-recent-* settings with
   some extra navigation
 - if confirmation fails, we have to do more work to recover

It is an increasingly big win when there are many intervening
offspring before the next sibling, however, so
`allout-next-sibling' resorts to this if it finds itself in that
situation."

  (if (if backward (bobp) (eobp))
      nil
    (let* ((start-point (point))
           (target-depth (or depth (allout-depth)))
           (search-whitespace-regexp nil)
           (depth-biased (- target-depth 2))
           (expression (if (<= target-depth 1)
                           allout-depth-one-regexp
                         (format allout-depth-specific-regexp
                                 depth-biased depth-biased)))
           found
           done)
      (while (not done)
        (setq found (save-match-data
                      (if backward
                          (re-search-backward expression nil 'to-limit)
                        (forward-char 1)
                        (re-search-forward expression nil 'to-limit))))
        (if (and found (allout-aberrant-container-p))
            (setq found nil))
        (setq done (or found (if backward (bobp) (eobp)))))
      (if (not found)
          (progn (goto-char start-point)
                 nil)
        ;; rationale: if any intervening items were at a lower depth, we
        ;; would now be on the first offspring at the target depth -- ie,
        ;; the preceding item (per the search direction) must be at a
        ;; lesser depth.  that's all we need to check.
        (if backward (allout-next-heading) (allout-previous-heading))
        (if (< allout-recent-depth target-depth)
            ;; return to start and reestablish allout-recent-*:
            (progn
              (goto-char start-point)
              (allout-depth)
              nil)
          (goto-char found)
          ;; locate cursor and set allout-recent-*:
          (allout-goto-prefix))))))
;;;_   > allout-previous-sibling (&optional depth backward)
(defun allout-previous-sibling (&optional depth backward)
  "Like `allout-forward-current-level' backwards, respecting invisible topics.

Optional DEPTH specifies depth to traverse, default current depth.

Optional BACKWARD reverses direction.

Return depth if successful, nil otherwise."
  (allout-next-sibling depth (not backward))
  )
;;;_   > allout-snug-back ()
(defun allout-snug-back ()
  "Position cursor at end of previous topic.

Presumes point is at the start of a topic prefix."
 (if (or (bobp) (eobp))
     nil
   (forward-char -1))
 (if (or (bobp) (not (= ?\n (preceding-char))))
     nil
   (forward-char -1))
 (point))
;;;_   > allout-beginning-of-level ()
(defun allout-beginning-of-level ()
  "Go back to the first sibling at this level, visible or not."
  (allout-end-of-level 'backward))
;;;_   > allout-end-of-level (&optional backward)
(defun allout-end-of-level (&optional _backward)
  "Go to the last sibling at this level, visible or not."

  (let ((depth (allout-depth)))
    (while (allout-previous-sibling depth nil))
    (prog1 allout-recent-depth
      (if (allout-called-interactively-p) (allout-end-of-prefix)))))
;;;_   > allout-next-visible-heading (arg)
(defun allout-next-visible-heading (arg)
  "Move to the next ARGth visible heading line, backward if ARG is negative.

Move to buffer limit in indicated direction if headings are exhausted."

  (interactive "p")
  (let* ((inhibit-field-text-motion t)
         (backward (if (< arg 0) (setq arg (* -1 arg))))
	 (step (if backward -1 1))
         (progress (allout-current-bullet-pos))
	 prev got)

    (while (> arg 0)
      (while (and
              ;; Boundary condition:
              (not (if backward (bobp)(eobp)))
              ;; Move, skipping over all concealed lines in one fell swoop:
              (prog1 (condition-case nil (or (line-move step) t)
                       (error nil))
                (allout-beginning-of-current-line)
                ;; line-move can wind up on the same line if long.
                ;; when moving forward, that would yield no-progress
                (when (and (not backward)
                           (<= (point) progress))
                  ;; ensure progress by doing line-move from end-of-line:
                  (end-of-line)
                  (condition-case nil (or (line-move step) t)
                    (error nil))
                  (allout-beginning-of-current-line)
                  (setq progress (point))))
              ;; Deal with apparent header line:
              (save-match-data
                (if (not (looking-at allout-regexp))
                    ;; not a header line, keep looking:
                    t
                  (allout-prefix-data)
                  (if (and (allout-do-doublecheck)
                           (allout-aberrant-container-p))
                      ;; skip this aberrant prospective header line:
                      t
                    ;; this prospective headerline qualifies -- register:
                    (setq got allout-recent-prefix-beginning)
                    ;; and break the loop:
                    nil)))))
      ;; Register this got, it may be the last:
      (if got (setq prev got))
      (setq arg (1- arg)))
    (cond (got				; Last move was to a prefix:
           (allout-end-of-prefix))
	  (prev				; Last move wasn't, but prev was:
           (goto-char prev)
           (allout-end-of-prefix))
	  ((not backward) (end-of-line) nil))))
;;;_   > allout-previous-visible-heading (arg)
(defun allout-previous-visible-heading (arg)
  "Move to the previous heading line.

With argument, repeats or can move forward if negative.
A heading line is one that starts with a `*' (or that `allout-regexp'
matches)."
  (interactive "p")
  (prog1 (allout-next-visible-heading (- arg))
    (if (allout-called-interactively-p) (allout-end-of-prefix))))
;;;_   > allout-forward-current-level (arg)
(defun allout-forward-current-level (arg)
  "Position point at the next heading of the same level.

Takes optional repeat-count, goes backward if count is negative.

Returns resulting position, else nil if none found."
  (interactive "p")
  (let ((start-depth (allout-current-depth))
	(start-arg arg)
	(backward (> 0 arg)))
    (if (= 0 start-depth)
	(error "No siblings, not in a topic..."))
    (if backward (setq arg (* -1 arg)))
    (allout-back-to-current-heading)
    (while (and (not (zerop arg))
                (if backward
                    (allout-previous-sibling)
                  (allout-next-sibling)))
      (setq arg (1- arg)))
    (if (not (allout-called-interactively-p))
        nil
      (allout-end-of-prefix)
      (if (not (zerop arg))
          (error "Hit %s level %d topic, traversed %d of %d requested"
                 (if backward "first" "last")
                 allout-recent-depth
                 (- (abs start-arg) arg)
                 (abs start-arg))))))
;;;_   > allout-backward-current-level (arg)
(defun allout-backward-current-level (arg)
  "Inverse of `allout-forward-current-level'."
  (interactive "p")
  (if (allout-called-interactively-p)
      (let ((current-prefix-arg (* -1 arg)))
	(call-interactively 'allout-forward-current-level))
    (allout-forward-current-level (* -1 arg))))

;;;_ #5 Alteration

;;;_  - Fundamental
;;;_   = allout-post-goto-bullet
(defvar allout-post-goto-bullet nil
  "Outline internal var, for `allout-pre-command-business' hot-spot operation.

When set, tells post-processing to reposition on topic bullet, and
then unset it.  Set by `allout-pre-command-business' when implementing
hot-spot operation, where literal characters typed over a topic bullet
are mapped to the command of the corresponding control-key on the
`allout-mode-map-value'.")
(make-variable-buffer-local 'allout-post-goto-bullet)
;;;_   = allout-command-counter
(defvar allout-command-counter 0
  "Counter that monotonically increases in allout-mode buffers.

Set by `allout-pre-command-business', to support allout addons in
coordinating with allout activity.")
(make-variable-buffer-local 'allout-command-counter)
;;;_   = allout-this-command-hid-text
(defvar allout-this-command-hid-text nil
  "True if the most recent allout-mode command hid any text.")
(make-variable-buffer-local 'allout-this-command-hid-text)
;;;_   > allout-post-command-business ()
(defun allout-post-command-business ()
  "Outline `post-command-hook' function.

- Implement (and clear) `allout-post-goto-bullet', for hot-spot
  outline commands.

- Move the cursor to the beginning of the entry if it is hidden
  and collapsing activity just happened.

- If the command we're following was an undo, check for change in
  the status of encrypted items and adjust auto-save inhibitions
  accordingly.

- Decrypt topic currently being edited if it was encrypted for a save."

  (if (not (allout-mode-p))		; In allout-mode.
      nil

    (when allout-just-did-undo
      (setq allout-just-did-undo nil)
      (run-hooks 'allout-post-undo-hook)
      (cond ((and (= buffer-saved-size -1)
                  allout-auto-save-temporarily-disabled)
             ;; user possibly undid a decryption, disinhibit auto-save:
             (allout-maybe-resume-auto-save-info-after-encryption))
            ((save-excursion
               (save-restriction
                 (widen)
                 (goto-char (point-min))
                 (not (allout-next-topic-pending-encryption))))
             ;; plain-text encrypted items are present, inhibit auto-save:
             (allout-inhibit-auto-save-info-for-decryption (buffer-size)))))

    (if (and (boundp 'allout-after-save-decrypt)
             allout-after-save-decrypt)
        (allout-after-saves-handler))

    ;; Implement allout-post-goto-bullet, if set:
    (if (and allout-post-goto-bullet
	     (allout-current-bullet-pos))
	(progn (goto-char (allout-current-bullet-pos))
	       (setq allout-post-goto-bullet nil))
      (when (and (allout-hidden-p) allout-this-command-hid-text)
        (allout-beginning-of-current-entry)))))
;;;_   > allout-pre-command-business ()
(defun allout-pre-command-business ()
  "Outline `pre-command-hook' function for outline buffers.

Among other things, implements special behavior when the cursor is on the
topic bullet character.

When the cursor is on the bullet character, self-insert
characters are reinterpreted as the corresponding
control-character in the `allout-mode-map-value'.  The
`allout-mode' `post-command-hook' insures that the cursor which
has moved as a result of such reinterpretation is positioned on
the bullet character of the destination topic.

The upshot is that you can get easy, single (ie, unmodified) key
outline maneuvering operations by positioning the cursor on the bullet
char.  When in this mode you can use regular cursor-positioning
command/keystrokes to relocate the cursor off of a bullet character to
return to regular interpretation of self-insert characters."

  (if (not (allout-mode-p))
      nil
    (setq allout-command-counter (1+ allout-command-counter))
    (setq allout-this-command-hid-text nil)
    ;; Do hot-spot navigation.
    (if (and (eq this-command 'self-insert-command)
	     (eq (point)(allout-current-bullet-pos)))
        (allout-hotspot-key-handler))))
;;;_   > allout-hotspot-key-handler ()
(defun allout-hotspot-key-handler ()
  "Catchall handling of key bindings in hot-spots.

Translates unmodified keystrokes to corresponding allout commands, when
they would qualify if prefixed with the `allout-command-prefix', and sets
`this-command' accordingly.

Returns the qualifying command, if any, else nil."
  (interactive)
  (let* ((modified (event-modifiers last-command-event))
         (key-num (cond ((numberp last-command-event) last-command-event)
                        ;; for XEmacs character type:
                        ((and (fboundp 'characterp)
                              (apply 'characterp (list last-command-event)))
                         (apply 'char-to-int (list last-command-event)))
                        (t 0)))
         mapped-binding)

    (if (zerop key-num)
        nil

      (if (and
           ;; exclude control chars and escape:
           (not modified)
           (<= 33 key-num)
           (setq mapped-binding
                 (or
                  ;; try control-modified versions of keys:
                  (key-binding (vconcat allout-command-prefix
                                        (vector
                                         (if (and (<= 97 key-num) ; "a"
                                                  (>= 122 key-num)) ; "z"
                                             (- key-num 96) key-num)))
                               t)
                  ;; try non-modified versions of keys:
                  (key-binding (vconcat allout-command-prefix
                                        (vector key-num))
                               t))))
          ;; Qualified as an allout command -- do hot-spot operation.
          (setq allout-post-goto-bullet t)
        ;; accept-defaults nil, or else we get allout-item-icon-key-handler.
        (setq mapped-binding (key-binding (vector key-num))))

      (while (keymapp mapped-binding)
        (setq mapped-binding
              (lookup-key mapped-binding (vector (read-char)))))

      (when mapped-binding
        (setq this-command mapped-binding)))))

;;;_   > allout-find-file-hook ()
(defun allout-find-file-hook ()
  "Activate `allout-mode' on non-nil `allout-auto-activation', `allout-layout'.

See `allout-auto-activation' for setup instructions."
  (if (and allout-auto-activation
	   (not (allout-mode-p))
	   allout-layout)
      (allout-mode)))

;;;_  - Topic Format Assessment
;;;_   > allout-solicit-alternate-bullet (depth &optional current-bullet)
(defun allout-solicit-alternate-bullet (depth &optional current-bullet)

  "Prompt for and return a bullet char as an alternative to the current one.

Offer one suitable for current depth DEPTH as default."

  (let* ((default-bullet (or (and (stringp current-bullet) current-bullet)
                             (allout-bullet-for-depth depth)))
	 (sans-escapes (allout-regexp-sans-escapes allout-bullets-string))
	 choice)
    (save-excursion
      (goto-char (allout-current-bullet-pos))
      (setq choice (allout-solicit-char-in-string
                    (format-message
                     "Select bullet: %s (`%s' default): "
                     sans-escapes
                     (substring-no-properties default-bullet))
                    sans-escapes
                    t)))
    (message "")
    (if (string= choice "") default-bullet choice))
  )
;;;_   > allout-distinctive-bullet (bullet)
(defun allout-distinctive-bullet (bullet)
  "True if BULLET is one of those on `allout-distinctive-bullets-string'."
  (string-match (regexp-quote bullet) allout-distinctive-bullets-string))
;;;_   > allout-numbered-type-prefix (&optional prefix)
(defun allout-numbered-type-prefix (&optional prefix)
  "True if current header prefix bullet is numbered bullet."
  (and allout-numbered-bullet
        (string= allout-numbered-bullet
                 (if prefix
                     (allout-get-prefix-bullet prefix)
                   (allout-get-bullet)))))
;;;_   > allout-encrypted-type-prefix (&optional prefix)
(defun allout-encrypted-type-prefix (&optional prefix)
  "True if current header prefix bullet is for an encrypted entry (body)."
  (and allout-topic-encryption-bullet
        (string= allout-topic-encryption-bullet
                 (if prefix
                     (allout-get-prefix-bullet prefix)
                   (allout-get-bullet)))))
;;;_   > allout-bullet-for-depth (&optional depth)
(defun allout-bullet-for-depth (&optional depth)
  "Return outline topic bullet suited to optional DEPTH, or current depth."
  ;; Find bullet in plain-bullets-string modulo DEPTH.
  (if allout-stylish-prefixes
      (char-to-string (aref allout-plain-bullets-string
                            (% (max 0 (- depth 2))
                               allout-plain-bullets-string-len)))
    allout-primary-bullet)
  )

;;;_  - Topic Production
;;;_   > allout-make-topic-prefix (&optional prior-bullet
(defun allout-make-topic-prefix (&optional prior-bullet
                                            new
                                            depth
                                            instead
                                            number-control
                                            index)
  ;; Depth null means use current depth, non-null means we're either
  ;; opening a new topic after current topic, lower or higher, or we're
  ;; changing level of current topic.
  ;; Instead dominates specified bullet-char.
;;;_    . Doc string:
  "Generate a topic prefix suitable for optional arg DEPTH, or current depth.

All the arguments are optional.

PRIOR-BULLET indicates the bullet of the prefix being changed, or
nil if none.  This bullet may be preserved (other options
notwithstanding) if it is on the `allout-distinctive-bullets-string',
for instance.

Second arg NEW indicates that a new topic is being opened after the
topic at point, if non-nil.  Default bullet for new topics, eg, may
be set (contingent to other args) to numbered bullets if previous
sibling is one.  The implication otherwise is that the current topic
is being adjusted -- shifted or rebulleted -- and we don't consider
bullet or previous sibling.

Third arg DEPTH forces the topic prefix to that depth, regardless of
the current topics' depth.

If INSTEAD is:

- nil, then the bullet char for the context is used, per distinction or depth
- a (numeric) character, then character's string representation is used
- a string, then the user is asked for bullet with the first char as default
- anything else, the user is solicited with bullet char per context as default

\(INSTEAD overrides other options, including, eg, a distinctive
PRIOR-BULLET.)

Fifth arg, NUMBER-CONTROL, matters only if `allout-numbered-bullet'
is non-nil *and* no specific INSTEAD was specified.  Then
NUMBER-CONTROL non-nil forces prefix to either numbered or
unnumbered format, depending on the value of the sixth arg, INDEX.

\(Note that NUMBER-CONTROL does *not* apply to level 1 topics.  Sorry...)

If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then
the prefix of the topic is forced to be numbered.  Non-nil
NUMBER-CONTROL and nil INDEX forces non-numbered format on the
bullet.  Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means
that the index for the numbered prefix will be derived, by counting
siblings back to start of level.  If INDEX is a number, then that
number is used as the index for the numbered prefix (allowing, eg,
sequential renumbering to not require this function counting back the
index for each successive sibling)."
;;;_    . Code:
  ;; The options are ordered in likely frequency of use, most common
  ;; highest, least lowest.  Ie, more likely to be doing prefix
  ;; adjustments than soliciting, and yet more than numbering.
  ;; Current prefix is least dominant, but most likely to be commonly
  ;; specified...

  (let* (body
         numbering
         denumbering
         (depth (or depth (allout-depth)))
         (header-lead allout-header-prefix)
         (bullet-char

          ;; Getting value for bullet char is practically the whole job:

          (cond
                                        ; Simplest situation -- level 1:
           ((<= depth 1) (setq header-lead "") allout-primary-bullet)
                                        ; Simple, too: all asterisks:
           (allout-old-style-prefixes
            ;; Cheat -- make body the whole thing, null out header-lead and
            ;; bullet-char:
            (setq body (make-string depth
                                    (string-to-char allout-primary-bullet)))
            (setq header-lead "")
            "")

           ;; (Neither level 1 nor old-style, so we're space padding.
           ;; Sneak it in the condition of the next case, whatever it is.)

           ;; Solicitation overrides numbering and other cases:
           ((progn (setq body (make-string (- depth 2) ?\ ))
                   ;; The actual condition:
                   instead)
            (let ((got (cond ((stringp instead)
                              (if (> (length instead) 0)
                                  (allout-solicit-alternate-bullet
                                   depth (substring instead 0 1))))
                             ((characterp instead) (char-to-string instead))
                             (t (allout-solicit-alternate-bullet depth)))))
              ;; Gotta check whether we're numbering and got a numbered bullet:
              (setq numbering (and allout-numbered-bullet
                                   (not (and number-control (not index)))
                                   (string= got allout-numbered-bullet)))
              ;; Now return what we got, regardless:
              got))

           ;; Numbering invoked through args:
           ((and allout-numbered-bullet number-control)
            (if (setq numbering (not (setq denumbering (not index))))
                allout-numbered-bullet
              (if (and prior-bullet
                       (not (string= allout-numbered-bullet
                                     prior-bullet)))
                  prior-bullet
                (allout-bullet-for-depth depth))))

          ;;; Neither soliciting nor controlled numbering ;;;
             ;;; (may be controlled denumbering, tho) ;;;

           ;; Check wrt previous sibling:
           ((and new				  ; only check for new prefixes
                 (<= depth (allout-depth))
                 allout-numbered-bullet	      ; ... & numbering enabled
                 (not denumbering)
                 (let ((sibling-bullet
                        (save-excursion
                          ;; Locate correct sibling:
                          (or (>= depth (allout-depth))
                              (allout-ascend-to-depth depth))
                          (allout-get-bullet))))
                   (if (and sibling-bullet
                            (string= allout-numbered-bullet sibling-bullet))
                       (setq numbering sibling-bullet)))))

           ;; Distinctive prior bullet?
           ((and prior-bullet
                 (allout-distinctive-bullet prior-bullet)
                 ;; Either non-numbered:
                 (or (not (and allout-numbered-bullet
                               (string= prior-bullet allout-numbered-bullet)))
                     ;; or numbered, and not denumbering:
                     (setq numbering (not denumbering)))
                 ;; Here 'tis:
                 prior-bullet))

           ;; Else, standard bullet per depth:
           ((allout-bullet-for-depth depth)))))

    (concat header-lead
            body
            bullet-char
            (if numbering
                (format "%d" (cond ((and index (numberp index)) index)
                                   (new (1+ (allout-sibling-index depth)))
                                   ((allout-sibling-index))))))
    )
  )
;;;_   > allout-open-topic (relative-depth &optional before offer-recent-bullet)
(defun allout-open-topic (relative-depth &optional before offer-recent-bullet)
  "Open a new topic at depth DEPTH.

New topic is situated after current one, unless optional flag BEFORE
is non-nil, or unless current line is completely empty -- lacking even
whitespace -- in which case open is done on the current line.

When adding an offspring, it will be added immediately after the parent if
the other offspring are exposed, or after the last child if the offspring
are hidden.  (The intervening offspring will be exposed in the latter
case.)

If OFFER-RECENT-BULLET is true, offer to use the bullet of the prior sibling.

Nuances:

- Creation of new topics is with respect to the visible topic
  containing the cursor, regardless of intervening concealed ones.

- New headers are generally created after/before the body of a
  topic.  However, they are created right at cursor location if the
  cursor is on a blank line, even if that breaks the current topic
  body.  This is intentional, to provide a simple means for
  deliberately dividing topic bodies.

- Double spacing of topic lists is preserved.  Also, the first
  level two topic is created double-spaced (and so would be
  subsequent siblings, if that's left intact).  Otherwise,
  single-spacing is used.

- Creation of sibling or nested topics is with respect to the topic
  you're starting from, even when creating backwards.  This way you
  can easily create a sibling in front of the current topic without
  having to go to its preceding sibling, and then open forward
  from there."

  (allout-beginning-of-current-line)
  (save-match-data
    (let* ((inhibit-field-text-motion t)
           (depth (+ (allout-current-depth) relative-depth))
           (opening-on-blank (if (looking-at "^$")
                                 (not (setq before nil))))
           ;; bunch o vars set while computing ref-topic
           opening-numbered
           ref-depth
           ref-bullet
           (ref-topic (save-excursion
                        (cond ((< relative-depth 0)
                               (allout-ascend-to-depth depth))
                              ((>= relative-depth 1) nil)
                              (t (allout-back-to-current-heading)))
                        (setq ref-depth allout-recent-depth)
                        (setq ref-bullet
                              (if (> allout-recent-prefix-end 1)
                                  (allout-recent-bullet)
                                ""))
                        (setq opening-numbered
                              (save-excursion
                                (and allout-numbered-bullet
                                     (or (<= relative-depth 0)
                                         (allout-descend-to-depth depth))
                                     (if (allout-numbered-type-prefix)
                                         allout-numbered-bullet))))
                        (point)))
           dbl-space
           doing-beginning
           start end)

      (if (not opening-on-blank)
                                        ; Positioning and vertical
                                        ; padding -- only if not
                                        ; opening-on-blank:
          (progn
            (goto-char ref-topic)
            (setq dbl-space             ; Determine double space action:
                  (or (and (<= relative-depth 0) ; not descending;
                           (save-excursion
                             ;; at b-o-b or preceded by a blank line?
                             (or (> 0 (forward-line -1))
                                 (looking-at "^\\s-*$")
                                 (bobp)))
                           (save-excursion
                             ;; succeeded by a blank line?
                             (allout-end-of-current-subtree)
                             (looking-at "\n\n")))
                      (and (= ref-depth 1)
                           (or before
                               (= depth 1)
                               (save-excursion
                                 ;; Don't already have following
                                 ;; vertical padding:
                                 (not (allout-pre-next-prefix)))))))

            ;; Position to prior heading, if inserting backwards, and not
            ;; going outwards:
            (if (and before (>= relative-depth 0))
                (progn (allout-back-to-current-heading)
                       (setq doing-beginning (bobp))
                       (if (not (bobp))
                           (allout-previous-heading)))
              (if (and before (bobp))
                  (open-line 1)))

            (if (<= relative-depth 0)
                ;; Not going inwards, don't snug up:
                (if doing-beginning
                    (if (not dbl-space)
                        (open-line 1)
                      (open-line 2))
                  (if before
                      (progn (end-of-line)
                             (allout-pre-next-prefix)
                             (while (and (= ?\n (following-char))
                                         (save-excursion
                                           (forward-char 1)
                                           (allout-hidden-p)))
                               (forward-char 1))
                             (if (not (looking-at "^$"))
                                 (open-line 1)))
                    (allout-end-of-current-subtree)
                    (if (looking-at "\n\n") (forward-char 1))))
              ;; Going inwards -- double-space if first offspring is
              ;; double-spaced, otherwise snug up.
              (allout-end-of-entry)
              (if (eobp)
                  (newline 1)
                (line-move 1))
              (allout-beginning-of-current-line)
              (backward-char 1)
              (if (bolp)
                  ;; Blank lines between current header body and next
                  ;; header -- get to last substantive (non-white-space)
                  ;; line in body:
                  (progn (setq dbl-space t)
                         (re-search-backward "[^ \t\n]" nil t)))
              (if (looking-at "\n\n")
                  (setq dbl-space t))
              (if (save-excursion
                    (allout-next-heading)
                    (when (> allout-recent-depth ref-depth)
                      ;; This is an offspring.
                      (forward-line -1)
                      (looking-at "^\\s-*$")))
                  (progn (forward-line 1)
                         (open-line 1)
                         (forward-line 1)))
              (allout-end-of-current-line))

            ;;(if doing-beginning (goto-char doing-beginning))
            (if (not (bobp))
                ;; We insert a newline char rather than using open-line to
                ;; avoid rear-stickiness inheritance of read-only property.
                (progn (if (and (not (> depth ref-depth))
                                (not before))
                           (open-line 1)
                         (if (and (not dbl-space) (> depth ref-depth))
                             (newline 1)
                           (if dbl-space
                               (open-line 1)
                             (if (not before)
                                 (newline 1)))))
                       (if (and dbl-space (not (> relative-depth 0)))
                           (newline 1))
                       (if (and (not (eobp))
                                (or (not (bolp))
                                    (and (not (bobp))
                                         ;; bolp doesn't detect concealed
                                         ;; trailing newlines, compensate:
                                         (save-excursion
                                           (forward-char -1)
                                           (allout-hidden-p)))))
                           (forward-char 1))))
            ))
      (setq start (point))
      (insert (concat (allout-make-topic-prefix opening-numbered t depth)
                      " "))
      (setq end (1+ (point)))

      (allout-rebullet-heading (and offer-recent-bullet ref-bullet)
                               depth nil nil t)
      (if (> relative-depth 0)
          (save-excursion (goto-char ref-topic)
                          (allout-show-children)))
      (end-of-line)

      (run-hook-with-args 'allout-structure-added-functions start end)
      )
    )
  )
;;;_   > allout-open-subtopic (arg)
(defun allout-open-subtopic (arg)
  "Open new topic header at deeper level than the current one.

Negative universal ARG means to open deeper, but place the new topic
prior to the current one."
  (interactive "p")
  (allout-open-topic 1 (> 0 arg) (< 1 arg)))
;;;_   > allout-open-sibtopic (arg)
(defun allout-open-sibtopic (arg)
  "Open new topic header at same level as the current one.

Positive universal ARG means to use the bullet of the prior sibling.

Negative universal ARG means to place the new topic prior to the current
one."
  (interactive "p")
  (allout-open-topic 0 (> 0 arg) (not (= 1 arg))))
;;;_   > allout-open-supertopic (arg)
(defun allout-open-supertopic (arg)
  "Open new topic header at shallower level than the current one.

Negative universal ARG means to open shallower, but place the new
topic prior to the current one."

  (interactive "p")
  (allout-open-topic -1 (> 0 arg) (< 1 arg)))

;;;_  - Outline Alteration
;;;_   : Topic Modification
;;;_    = allout-former-auto-filler
(defvar allout-former-auto-filler nil
  "Name of modal fill function being wrapped by `allout-auto-fill'.")
;;;_    > allout-auto-fill ()
(defun allout-auto-fill ()
  "`allout-mode' autofill function.

Maintains outline hanging topic indentation if
`allout-use-hanging-indents' is set."

  (when (and (not allout-inhibit-auto-fill)
             (or (not allout-inhibit-auto-fill-on-headline)
                 (not (allout-on-current-heading-p))))
    (let ((fill-prefix (if allout-use-hanging-indents
                           ;; Check for topic header indentation:
                           (save-match-data
                             (save-excursion
                               (beginning-of-line)
                               (if (looking-at allout-regexp)
                                   ;; ... construct indentation to account for
                                   ;; length of topic prefix:
                                   (make-string (progn (allout-end-of-prefix)
                                                       (current-column))
                                                ?\ ))))))
          (use-auto-fill-function
           (if (and (eq allout-outside-normal-auto-fill-function
                        'allout-auto-fill)
                    (eq auto-fill-function 'allout-auto-fill))
               'do-auto-fill
             (or allout-outside-normal-auto-fill-function
                 auto-fill-function))))
      (if (or allout-former-auto-filler allout-use-hanging-indents)
          (funcall use-auto-fill-function)))))
;;;_    > allout-reindent-body (old-depth new-depth &optional number)
(defun allout-reindent-body (old-depth new-depth &optional _number)
  "Reindent body lines which were indented at OLD-DEPTH to NEW-DEPTH.

Optional arg NUMBER indicates numbering is being added, and it must
be accommodated.

Note that refill of indented paragraphs is not done."

  (save-excursion
    (allout-end-of-prefix)
    (let* ((new-margin (current-column))
	   excess old-indent-begin old-indent-end
	   ;; We want the column where the header-prefix text started
	   ;; *before* the prefix was changed, so we infer it relative
	   ;; to the new margin and the shift in depth:
	   (old-margin (+ old-depth (- new-margin new-depth))))

      ;; Process lines up to (but excluding) next topic header:
      (allout-unprotected
       (save-match-data
         (while
	     (and (re-search-forward "\n\\(\\s-*\\)"
				     nil
				     t)
		  ;; Register the indent data, before we reset the
		  ;; match data with a subsequent `looking-at':
		  (setq old-indent-begin (match-beginning 1)
			old-indent-end (match-end 1))
		  (not (looking-at allout-regexp)))
	   (if (> 0 (setq excess (- (- old-indent-end old-indent-begin)
                                    old-margin)))
	       ;; Text starts left of old margin -- don't adjust:
	       nil
	     ;; Text was hanging at or right of old left margin --
	     ;; reindent it, preserving its existing indentation
	     ;; beyond the old margin:
	     (delete-region old-indent-begin old-indent-end)
             (indent-to (+ new-margin excess (current-column))))))))))
;;;_    > allout-rebullet-current-heading (arg)
(defun allout-rebullet-current-heading (arg)
  "Solicit new bullet for current visible heading."
  (interactive "p")
  (let ((initial-col (current-column))
	(on-bullet (eq (point)(allout-current-bullet-pos)))
        from to
	(backwards (if (< arg 0)
		       (setq arg (* arg -1)))))
    (while (> arg 0)
      (save-excursion (allout-back-to-current-heading)
		      (allout-end-of-prefix)
                      (setq from allout-recent-prefix-beginning
                            to allout-recent-prefix-end)
		      (allout-rebullet-heading t	;;; instead
						nil	;;; depth
						nil	;;; number-control
						nil	;;; index
						t)	;;; do-successors
                      (run-hook-with-args 'allout-exposure-change-functions
                                          from to t))
      (setq arg (1- arg))
      (if (<= arg 0)
	  nil
	(setq initial-col nil)		; Override positioning back to init col
	(if (not backwards)
	    (allout-next-visible-heading 1)
	  (allout-goto-prefix-doublechecked)
	  (allout-next-visible-heading -1))))
    (message "Done.")
    (cond (on-bullet (goto-char (allout-current-bullet-pos)))
	  (initial-col (move-to-column initial-col)))))
;;;_    > allout-rebullet-heading (&optional instead ...)
(defun allout-rebullet-heading (&optional instead
                                           new-depth
                                           number-control
                                           index
                                           do-successors)

  "Adjust bullet of current topic prefix.

All args are optional.

If INSTEAD is:
- nil, then the bullet char for the context is used, per distinction or depth
- a (numeric) character, then character's string representation is used
- a string, then the user is asked for bullet with the first char as default
- anything else, the user is solicited with bullet char per context as default

Second arg DEPTH forces the topic prefix to that depth, regardless
of the topic's current depth.

Third arg NUMBER-CONTROL can force the prefix to or away from
numbered form.  It has effect only if `allout-numbered-bullet' is
non-nil and soliciting was not explicitly invoked (via first arg).
Its effect, numbering or denumbering, then depends on the setting
of the fourth arg, INDEX.

If NUMBER-CONTROL is non-nil and fourth arg INDEX is nil, then the
prefix of the topic is forced to be non-numbered.  Null index and
non-nil NUMBER-CONTROL forces denumbering.  Non-nil INDEX (and
non-nil NUMBER-CONTROL) forces a numbered-prefix form.  If non-nil
INDEX is a number, then that number is used for the numbered
prefix.  Non-nil and non-number means that the index for the
numbered prefix will be derived by allout-make-topic-prefix.

Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
siblings.

Cf vars `allout-stylish-prefixes', `allout-old-style-prefixes',
and `allout-numbered-bullet', which all affect the behavior of
this function."

  (let* ((current-depth (allout-depth))
         (new-depth (or new-depth current-depth))
         (mb allout-recent-prefix-beginning)
         (me allout-recent-prefix-end)
         (current-bullet (buffer-substring-no-properties (- me 1) me))
         (has-annotation (get-text-property mb 'allout-was-hidden))
         (new-prefix (allout-make-topic-prefix current-bullet
                                                nil
                                                new-depth
                                                instead
                                                number-control
                                                index)))

    ;; Is new one identical to old?
    (if (and (= current-depth new-depth)
             (string= current-bullet
                      (substring new-prefix (1- (length new-prefix)))))
	;; Nothing to do:
        t

      ;; New prefix probably different from old:
					; get rid of old one:
      (allout-unprotected (delete-region mb me))
      (goto-char mb)
					; Dispense with number if
					; numbered-bullet prefix:
      (save-match-data
        (if (and allout-numbered-bullet
                 (string= allout-numbered-bullet current-bullet)
                 (looking-at "[0-9]+"))
            (allout-unprotected
             (delete-region (match-beginning 0)(match-end 0)))))

      ;; convey 'allout-was-hidden annotation, if original had it:
      (if has-annotation
          (put-text-property 0 (length new-prefix) 'allout-was-hidden t
                             new-prefix))

					; Put in new prefix:
      (allout-unprotected (insert new-prefix))

      ;; Reindent the body if elected, margin changed, and not encrypted body:
      (if (and allout-reindent-bodies
	       (not (= new-depth current-depth))
               (not (allout-encrypted-topic-p)))
	  (allout-reindent-body current-depth new-depth))

      (run-hook-with-args 'allout-exposure-change-functions mb me nil)

      ;; Recursively rectify successive siblings of orig topic if
      ;; caller elected for it:
      (if do-successors
	  (save-excursion
	    (while (allout-next-sibling new-depth nil)
	      (setq index
		    (cond ((numberp index) (1+ index))
			  ((not number-control)  (allout-sibling-index))))
	      (if (allout-numbered-type-prefix)
		  (allout-rebullet-heading nil		;;; instead
					    new-depth	;;; new-depth
					    number-control;;; number-control
					    index	;;; index
					    nil)))))	;;;(dont!)do-successors
      )	; (if (and (= current-depth new-depth)...))
    ) ; let* ((current-depth (allout-depth))...)
  ) ; defun
;;;_    > allout-rebullet-topic (arg)
(defun allout-rebullet-topic (arg &optional sans-offspring)
  "Rebullet the visible topic containing point and all contained subtopics.

Descends into invisible as well as visible topics, however.

When optional SANS-OFFSPRING is non-nil, subtopics are not
shifted.  (Shifting a topic outwards without shifting its
offspring is disallowed, since this would create a \"containment
discontinuity\", where the depth difference between a topic and
its immediate offspring is greater than one.)

With repeat count, shift topic depth by that amount."
  (interactive "P")
  (let ((start-col (current-column)))
    (save-excursion
      ;; Normalize arg:
      (cond ((null arg) (setq arg 0))
            ((listp arg) (setq arg (car arg))))
      ;; Fill the user in, in case we're shifting a big topic:
      (if (not (zerop arg)) (message "Shifting..."))
      (allout-back-to-current-heading)
      (if (<= (+ allout-recent-depth arg) 0)
          (error "Attempt to shift topic below level 1"))
      (allout-rebullet-topic-grunt arg nil nil nil nil sans-offspring)
      (if (not (zerop arg)) (message "Shifting... done.")))
    (move-to-column (max 0 (+ start-col arg)))))
;;;_    > allout-rebullet-topic-grunt (&optional relative-depth ...)
(defun allout-rebullet-topic-grunt (&optional relative-depth
                                               starting-depth
                                               starting-point
                                               index
                                               do-successors
                                               sans-offspring)
  "Like `allout-rebullet-topic', but on nearest containing topic
\(visible or not).

See `allout-rebullet-heading' for rebulleting behavior.

All arguments are optional.

First arg RELATIVE-DEPTH means to shift the depth of the entire
topic that amount.

Several subsequent args are for internal recursive use by the function
itself: STARTING-DEPTH, STARTING-POINT, and INDEX.

Finally, if optional SANS-OFFSPRING is non-nil then the offspring
are not shifted.  (Shifting a topic outwards without shifting
its offspring is disallowed, since this would create a
\"containment discontinuity\", where the depth difference between
a topic and its immediate offspring is greater than one.)"

  ;; XXX the recursion here is peculiar, and in general the routine may
  ;; need simplification with refactoring.

  (if (and sans-offspring
           relative-depth
           (< relative-depth 0))
      (error (concat "Attempt to shift topic outwards without offspring,"
                     " would cause containment discontinuity.")))

  (let* ((relative-depth (or relative-depth 0))
         (new-depth (allout-depth))
         (starting-depth (or starting-depth new-depth))
         (on-starting-call  (null starting-point))
         (index (or index
                    ;; Leave index null on starting call, so rebullet-heading
                    ;; calculates it at what might be new depth:
                    (and (or (zerop relative-depth)
                             (not on-starting-call))
                         (allout-sibling-index))))
         (starting-index index)
         (moving-outwards (< 0 relative-depth))
         (starting-point (or starting-point (point)))
         (local-point (point)))

    ;; Sanity check for excessive promotion done only on starting call:
    (and on-starting-call
         moving-outwards
         (> 0 (+ starting-depth relative-depth))
         (error "Attempt to shift topic out beyond level 1"))

    (cond ((= starting-depth new-depth)
           ;; We're at depth to work on this one.

           ;; When shifting out we work on the children before working on
           ;; the parent to avoid interim `allout-aberrant-container-p'
           ;; aberrancy, and vice-versa when shifting in:
           (if (>= relative-depth 0)
               (allout-rebullet-heading nil
                                        (+ starting-depth relative-depth)
                                        nil		;;; number
                                        index
                                        nil)) ;;; do-successors
           (when (not sans-offspring)
             ;; ... and work on subsequent ones which are at greater depth:
             (setq index 0)
             (allout-next-heading)
             (while (and (not (eobp))
                         (< starting-depth (allout-depth)))
               (setq index (1+ index))
               (allout-rebullet-topic-grunt relative-depth
                                            (1+ starting-depth)
                                            starting-point
                                            index)))
           (when (< relative-depth 0)
             (save-excursion
               (goto-char local-point)
               (allout-rebullet-heading nil               ;;; instead
                                        (+ starting-depth relative-depth)
                                        nil		;;; number
                                        starting-index
                                        nil)))) ;;; do-successors

          ((< starting-depth new-depth)
           ;; Rare case -- subtopic more than one level deeper than parent.
           ;; Treat this one at an even deeper level:
           (allout-rebullet-topic-grunt relative-depth
                                         new-depth
                                         starting-point
                                         index
                                         sans-offspring)))

    (if on-starting-call
        (progn
          ;; Rectify numbering of former siblings of the adjusted topic,
          ;; if topic has changed depth
          (if (or do-successors
                  (and (not (zerop relative-depth))
                       (or (= allout-recent-depth starting-depth)
                           (= allout-recent-depth (+ starting-depth
                                                        relative-depth)))))
              (allout-rebullet-heading nil nil nil nil t))
          ;; Now rectify numbering of new siblings of the adjusted topic,
          ;; if depth has been changed:
          (progn (goto-char starting-point)
                 (if (not (zerop relative-depth))
                     (allout-rebullet-heading nil nil nil nil t)))))
    )
  )
;;;_    > allout-renumber-to-depth (&optional depth)
(defun allout-renumber-to-depth (&optional depth)
  "Renumber siblings at current depth.

Affects superior topics if optional arg DEPTH is less than current depth.

Returns final depth."

  ;; Proceed by level, processing subsequent siblings on each,
  ;; ascending until we get shallower than the start depth:

  (let ((ascender (allout-depth))
	was-eobp)
    (while (and (not (eobp))
		(allout-depth)
                (>= allout-recent-depth depth)
                (>= ascender depth))
                                        ; Skip over all topics at
                                        ; lesser depths, which can not
                                        ; have been disturbed:
      (while (and (not (setq was-eobp (eobp)))
		  (> allout-recent-depth ascender))
        (allout-next-heading))
                                        ; Prime ascender for ascension:
      (setq ascender (1- allout-recent-depth))
      (if (>= allout-recent-depth depth)
          (allout-rebullet-heading nil	;;; instead
                                    nil	;;; depth
                                    nil	;;; number-control
                                    nil	;;; index
                                    t)) ;;; do-successors
      (if was-eobp (goto-char (point-max)))))
  allout-recent-depth)
;;;_    > allout-number-siblings (&optional denumber)
(defun allout-number-siblings (&optional denumber)
  "Assign numbered topic prefix to this topic and its siblings.

With universal argument, denumber -- assign default bullet to this
topic and its siblings.

With repeated universal argument (`^U^U'), solicit bullet for each
rebulleting each topic at this level."

  (interactive "P")

  (save-excursion
    (allout-back-to-current-heading)
    (allout-beginning-of-level)
    (let ((depth allout-recent-depth)
	  (index (if (not denumber) 1))
          (use-bullet (equal '(16) denumber))
          (more t))
      (while more
        (allout-rebullet-heading use-bullet		;;; instead
                                  depth			;;; depth
                                  t			;;; number-control
                                  index			;;; index
                                  nil)			;;; do-successors
        (if index (setq index (1+ index)))
        (setq more (allout-next-sibling depth nil))))))
;;;_    > allout-shift-in (arg)
(defun allout-shift-in (arg)
  "Increase depth of current heading and any items collapsed within it.

With a negative argument, the item is shifted out using
`allout-shift-out', instead.

With an argument greater than one, shift-in the item but not its
offspring, making the item into a sibling of its former children,
and a child of sibling that formerly preceded it.

You are not allowed to shift the first offspring of a topic
inwards, because that would yield a \"containment
discontinuity\", where the depth difference between a topic and
its immediate offspring is greater than one.  The first topic in
the file can be adjusted to any positive depth, however."

  (interactive "p")
  (if (< arg 0)
      (allout-shift-out (* arg -1))
    ;; refuse to create a containment discontinuity:
    (save-excursion
      (allout-back-to-current-heading)
      (if (not (bobp))
          (let* ((current-depth allout-recent-depth)
                 (start-point (point))
                 (predecessor-depth (progn
                                      (forward-char -1)
                                      (allout-goto-prefix-doublechecked)
                                      (if (< (point) start-point)
                                          allout-recent-depth
                                        0))))
            (if (and (> predecessor-depth 0)
                     (> (1+ current-depth)
                        (1+ predecessor-depth)))
                (error (concat "Disallowed shift deeper than"
                               " containing topic's children."))
              (allout-back-to-current-heading)
              (if (< allout-recent-depth (1+ current-depth))
                  (allout-show-children))))))
    (let ((where (point)))
      (allout-rebullet-topic 1 (and (> arg 1) 'sans-offspring))
      (run-hook-with-args 'allout-structure-shifted-functions arg where))))
;;;_    > allout-shift-out (arg)
(defun allout-shift-out (arg)
  "Decrease depth of current heading and any topics collapsed within it.
This will make the item a sibling of its former container.

With a negative argument, the item is shifted in using
`allout-shift-in', instead.

With an argument greater than one, shift-out the item's offspring
but not the item itself, making the former children siblings of
the item.

With an argument greater than 1, the item's offspring are shifted
out without shifting the item.  This will make the immediate
subtopics into siblings of the item."
  (interactive "p")
  (if (< arg 0)
      (allout-shift-in (* arg -1))
    ;; Get proper exposure in this area:
    (save-excursion (if (allout-ascend)
                        (allout-show-children)))
    ;; Show collapsed children if there's a successor which will become
    ;; their sibling:
    (if (and (allout-current-topic-collapsed-p)
             (save-excursion (allout-next-sibling)))
        (allout-show-children))
    (let ((where (and (allout-depth) allout-recent-prefix-beginning)))
      (save-excursion
        (if (> arg 1)
            ;; Shift the offspring but not the topic:
            (let ((children-chart (allout-chart-subtree 1)))
              (if (listp (car children-chart))
                  ;; whoops:
                  (setq children-chart (flatten-tree children-chart)))
              (save-excursion
                (dolist (child-point children-chart)
                  (goto-char child-point)
                  (allout-shift-out 1))))
          (allout-rebullet-topic (* arg -1))))
      (run-hook-with-args 'allout-structure-shifted-functions (* arg -1) where))))
;;;_   : Surgery (kill-ring) functions with special provisions for outlines:
;;;_    > allout-kill-line (&optional arg)
(defun allout-kill-line (&optional arg)
  "Kill line, adjusting subsequent lines suitably for outline mode."

  (interactive "*P")

  (if (or (not (allout-mode-p))
          (not (bolp))
          (not (save-match-data (looking-at allout-regexp))))
      ;; Just do a regular kill:
      (kill-line arg)
    ;; Ah, have to watch out for adjustments:
    (let* ((beg (point))
           end
           (beg-hidden (allout-hidden-p))
           (end-hidden (save-excursion (allout-end-of-current-line)
                                       (setq end (point))
                                       (allout-hidden-p)))
           (depth (allout-depth)))

      (allout-annotate-hidden beg end)
      (unwind-protect
          (if (and (not beg-hidden) (not end-hidden))
              (allout-unprotected (kill-line arg))
            (kill-line arg))
        (run-hooks 'allout-after-copy-or-kill-hook)
        (allout-deannotate-hidden beg end)

        (if allout-numbered-bullet
            (save-excursion         ; Renumber subsequent topics if needed:
              (if (not (save-match-data (looking-at allout-regexp)))
                  (allout-next-heading))
              (allout-renumber-to-depth depth)))
        (run-hook-with-args 'allout-structure-deleted-functions depth (point))))))
;;;_    > allout-copy-line-as-kill ()
(defun allout-copy-line-as-kill ()
  "Like `allout-kill-topic', but save to kill ring instead of deleting."
  (interactive)
  (let ((buffer-read-only t))
    (condition-case nil
        (allout-kill-line)
      (buffer-read-only nil))))
;;;_    > allout-kill-topic ()
(defun allout-kill-topic ()
  "Kill topic together with subtopics.

Trailing whitespace is killed with a topic if that whitespace:

 - would separate the topic from a subsequent sibling
 - would separate the topic from the end of buffer
 - would not be added to whitespace already separating the topic from the
   previous one.

Topic exposure is marked with text-properties, to be used by
`allout-yank-processing' for exposure recovery."

  (interactive)
  (let* ((inhibit-field-text-motion t)
         (beg (prog1 (allout-back-to-current-heading) (beginning-of-line)))
         end
         (depth allout-recent-depth))
    (allout-end-of-current-subtree)
    (if (and (/= (current-column) 0) (not (eobp)))
        (forward-char 1))
    (if (not (eobp))
	(if (and (= (following-char) ?\n)
                 (or (save-excursion
                       (or (not (allout-next-heading))
                           (= depth allout-recent-depth)))
                     (and (> (- beg (point-min)) 3)
                          (string= (buffer-substring (- beg 2) beg) "\n\n"))))
	    (forward-char 1)))

    (allout-annotate-hidden beg (setq end (point)))
    (unwind-protect                     ; for possible barf-if-buffer-read-only.
        (allout-unprotected (kill-region beg end))
      (allout-deannotate-hidden beg end)
      (run-hooks 'allout-after-copy-or-kill-hook)

      (save-excursion
        (allout-renumber-to-depth depth))
      (run-hook-with-args 'allout-structure-deleted-functions depth (point)))))
;;;_    > allout-copy-topic-as-kill ()
(defun allout-copy-topic-as-kill ()
  "Like `allout-kill-topic', but save to kill ring instead of deleting."
  (interactive)
  (let ((buffer-read-only t))
    (condition-case nil
        (allout-kill-topic)
      (buffer-read-only (message "Topic copied...")))))
;;;_    > allout-annotate-hidden (begin end)
(defun allout-annotate-hidden (begin end)
  "Qualify text with properties to indicate exposure status."

  (let ((was-modified (buffer-modified-p))
        (buffer-read-only nil))
    (allout-deannotate-hidden begin end)
    (save-excursion
      (goto-char begin)
      (let (done next prev overlay)
        (while (not done)
          ;; at or advance to start of next hidden region:
          (if (not (allout-hidden-p))
              (setq next
                    (max (1+ (point))
                         (next-single-char-property-change (point)
                                                           'invisible
                                                           nil end))))
          (if (or (not next) (eq prev next))
              ;; still not at start of hidden area -- must not be any left.
              (setq done t)
            (goto-char next)
            (setq prev next)
            (if (not (allout-hidden-p))
                ;; still not at start of hidden area.
                (setq done t)
              (setq overlay (allout-get-invisibility-overlay))
              (setq next (overlay-end overlay)
                    prev next)
              ;; advance to end of this hidden area:
              (when next
                (goto-char next)
                (allout-unprotected
                 (let ((buffer-undo-list t))
                   (put-text-property (overlay-start overlay) next
                                      'allout-was-hidden t)))))))))
    (set-buffer-modified-p was-modified)))
;;;_    > allout-deannotate-hidden (begin end)
(defun allout-deannotate-hidden (begin end)
  "Remove allout hidden-text annotation between BEGIN and END."

  (allout-unprotected
   (let ((inhibit-read-only t)
         (buffer-undo-list t))
     (remove-text-properties begin (min end (point-max))
                             '(allout-was-hidden t)))))
;;;_    > allout-hide-by-annotation (begin end)
(defun allout-hide-by-annotation (begin end)
  "Translate text properties indicating exposure status into actual exposure."
  (save-excursion
    (goto-char begin)
    (let ((was-modified (buffer-modified-p))
          done next prev)
      (while (not done)
        ;; at or advance to start of next annotation:
        (if (not (get-text-property (point) 'allout-was-hidden))
            (setq next (next-single-char-property-change
                        (point) 'allout-was-hidden nil end)))
        (if (or (not next) (eq prev next))
            ;; no more or not advancing -- must not be any left.
            (setq done t)
          (goto-char next)
          (setq prev next)
          (if (not (get-text-property (point) 'allout-was-hidden))
              ;; still not at start of annotation.
              (setq done t)
            ;; advance to just after end of this annotation:
            (setq next (next-single-char-property-change
                        (point) 'allout-was-hidden nil end))
            (let ((o (make-overlay prev next nil 'front-advance)))
              (overlay-put o 'category 'allout-exposure-category)
              (overlay-put o 'evaporate t))
            (allout-deannotate-hidden prev next)
            (setq prev next)
            (if next (goto-char next)))))
      (set-buffer-modified-p was-modified))))
;;;_    > allout-yank-processing ()
(defun allout-yank-processing (&optional _arg)

  "Incidental allout-specific business to be done just after text yanks.

Does depth adjustment of yanked topics, when:

1 the stuff being yanked starts with a valid outline header prefix, and
2 it is being yanked at the end of a line which consists of only a valid
     topic prefix.

Also, adjusts numbering of subsequent siblings when appropriate.

Depth adjustment alters the depth of all the topics being yanked
the amount it takes to make the first topic have the depth of the
header into which it's being yanked.

The point is left in front of yanked, adjusted topics, rather than
at the end (and vice-versa with the mark).  Non-adjusted yanks,
however, are left exactly like normal, non-allout-specific yanks."

  (interactive "*P")
					; Get to beginning, leaving
					; region around subject:
  (if (< (mark-marker) (point))
      (exchange-point-and-mark))
  (save-match-data
    (let* ((subj-beg (point))
           (into-bol (bolp))
           (subj-end (mark-marker))
           ;; 'resituate' if yanking an entire topic into topic header:
           (resituate (and (let ((allout-inhibit-aberrance-doublecheck t))
                             (allout-e-o-prefix-p))
                           (looking-at allout-regexp)
                           (allout-prefix-data)))
           ;; `rectify-numbering' if resituating (where several topics may
           ;; be resituating) or yanking a topic into a topic slot (bol):
           (rectify-numbering (or resituate
                                  (and into-bol (looking-at allout-regexp)))))
      (if resituate
          ;; Yanking a topic into the start of a topic -- reconcile to fit:
          (let* ((inhibit-field-text-motion t)
                 (prefix-len (if (not (match-end 1))
                                 1
                               (- (match-end 1) subj-beg)))
                 (subj-depth allout-recent-depth)
                 (prefix-bullet (allout-recent-bullet))
                 (adjust-to-depth
                  ;; Nil if adjustment unnecessary, otherwise depth to which
                  ;; adjustment should be made:
                  (save-excursion
                    (and (goto-char subj-end)
                         (eolp)
                         (goto-char subj-beg)
                         (and (looking-at allout-regexp)
                              (progn
                                (beginning-of-line)
                                (not (= (point) subj-beg)))
                              (looking-at allout-regexp)
                              (allout-prefix-data))
                         allout-recent-depth)))
                 (more t))
            (setq rectify-numbering allout-numbered-bullet)
            (if adjust-to-depth
                                        ; Do the adjustment:
                (progn
                  (save-restriction
                    (narrow-to-region subj-beg subj-end)
                                        ; Trim off excessive blank
                                        ; line at end, if any:
                    (goto-char (point-max))
                    (if (looking-at "^$")
                        (allout-unprotected (delete-char -1)))
                                        ; Work backwards, with each
                                        ; shallowest level,
                                        ; successively excluding the
                                        ; last processed topic from
                                        ; the narrow region:
                    (while more
                      (allout-back-to-current-heading)
                                        ; go as high as we can in each bunch:
                      (while (allout-ascend t))
                      (save-excursion
                        (allout-unprotected
                         (allout-rebullet-topic-grunt (- adjust-to-depth
                                                         subj-depth)))
                        (allout-depth))
                      (if (setq more (not (bobp)))
                          (progn (widen)
                                 (forward-char -1)
                                 (narrow-to-region subj-beg (point))))))
                  ;; Remove new heading prefix:
                  (allout-unprotected
                   (progn
                     (delete-region (point) (+ (point)
                                               prefix-len
                                               (- adjust-to-depth
                                                  subj-depth)))
                                        ; and delete residual subj
                                        ; prefix digits and space:
                     (while (looking-at "[0-9]") (delete-char 1))
                     (delete-char -1)
                     (if (not (eolp))
                         (forward-char))))
                  ;; Assert new topic's bullet - minimal effort if unchanged:
                  (allout-rebullet-heading (string-to-char prefix-bullet)))
              (exchange-point-and-mark))))
      (if rectify-numbering
          (progn
            (save-excursion
                                        ; Give some preliminary feedback:
              (message "... reconciling numbers")
                                        ; ... and renumber, in case necessary:
              (goto-char subj-beg)
              (if (allout-goto-prefix-doublechecked)
                  (allout-unprotected
                   (allout-rebullet-heading nil          ;;; instead
                                            (allout-depth) ;;; depth
                                            nil ;;; number-control
                                            nil ;;; index
                                            t)))
              (message ""))))
      (if (or into-bol resituate)
          (allout-hide-by-annotation (point) (mark-marker))
        (allout-deannotate-hidden (mark-marker) (point)))
      (if (not resituate)
          (exchange-point-and-mark))
      (run-hook-with-args 'allout-structure-added-functions subj-beg subj-end))))
;;;_    > allout-yank (&optional arg)
(defun allout-yank (&optional arg)
  "`allout-mode' yank, with depth and numbering adjustment of yanked topics.

Non-topic yanks work no differently than normal yanks.

If a topic is being yanked into a bare topic prefix, the depth of the
yanked topic is adjusted to the depth of the topic prefix.

  1 we're yanking in an `allout-mode' buffer
  2 the stuff being yanked starts with a valid outline header prefix, and
  3 it is being yanked at the end of a line which consists of only a valid
    topic prefix.

If these conditions hold then the depth of the yanked topics are all
adjusted the amount it takes to make the first one at the depth of the
header into which it's being yanked.

The point is left in front of yanked, adjusted topics, rather than
at the end (and vice-versa with the mark).  Non-adjusted yanks,
however, (ones that don't qualify for adjustment) are handled
exactly like normal yanks.

Numbering of yanked topics, and the successive siblings at the depth
into which they're being yanked, is adjusted.

`allout-yank-pop' works with `allout-yank' just like normal `yank-pop'
works with normal `yank' in non-outline buffers."

  (interactive "*P")
  (setq this-command 'yank)
  (allout-unprotected
   (yank arg))
  (if (allout-mode-p)
      (allout-yank-processing)))
;;;_    > allout-yank-pop (&optional arg)
(defun allout-yank-pop (&optional arg)
  "Yank-pop like `allout-yank' when popping to bare outline prefixes.

Adapts level of popped topics to level of fresh prefix.

Note -- prefix changes to distinctive bullets will stick, if followed
by pops to non-distinctive yanks.  Bug..."

  (interactive "*p")
  (setq this-command 'yank)
  (yank-pop arg)
  (if (allout-mode-p)
      (allout-yank-processing)))

;;;_  - Specialty bullet functions
;;;_   : File Cross references
;;;_    > allout-resolve-xref ()
(defun allout-resolve-xref ()
  "Pop to file associated with current heading, if it has an xref bullet.

\(Works according to setting of `allout-file-xref-bullet')."
  (interactive)
  (if (not allout-file-xref-bullet)
      (error
       "Outline cross references disabled -- no `allout-file-xref-bullet'")
    (if (not (string= (allout-current-bullet) allout-file-xref-bullet))
        (error "Current heading lacks cross-reference bullet `%s'"
               allout-file-xref-bullet)
      (let ((inhibit-field-text-motion t)
            file-name)
        (save-match-data
          (save-excursion
            (let* ((text-start allout-recent-prefix-end)
                   (heading-end (point-at-eol)))
              (goto-char text-start)
              (setq file-name
                    (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
                        (buffer-substring (match-beginning 1)
                                          (match-end 1)))))))
        (setq file-name (expand-file-name file-name))
        (if (or (file-exists-p file-name)
                (if (file-writable-p file-name)
                    (y-or-n-p (format "%s not there, create one? "
                                      file-name))
                  (error "%s not found and can't be created" file-name)))
            (condition-case failure
                (find-file-other-window file-name)
              (error failure))
          (error "%s not found" file-name))
        )
      )
    )
  )

;;;_ #6 Exposure Control

;;;_  - Fundamental
;;;_   > allout-flag-region (from to flag)
(defun allout-flag-region (from to flag)
  "Conceal text between FROM and TO if FLAG is non-nil, else reveal it.
After the exposure changes are made, run the abnormal hook
`allout-exposure-change-functions' with the same arguments as
this function."

  ;; We use outline invisibility spec.
  (remove-overlays from to 'category 'allout-exposure-category)
  (when flag
    (let ((o (make-overlay from to nil 'front-advance)))
      (overlay-put o 'category 'allout-exposure-category)
      (overlay-put o 'evaporate t))
    (setq allout-this-command-hid-text t))
  (run-hook-with-args 'allout-exposure-change-functions from to flag))
;;;_   > allout-flag-current-subtree (flag)
(defun allout-flag-current-subtree (flag)
  "Conceal currently-visible topic's subtree if FLAG non-nil, else reveal it."

  (save-excursion
    (allout-back-to-current-heading)
    (let ((inhibit-field-text-motion t))
      (end-of-line))
    (allout-flag-region (point)
                        ;; Exposing must not leave trailing blanks hidden,
                        ;; but can leave them exposed when hiding, so we
                        ;; can use flag's inverse as the
                        ;; include-trailing-blank cue:
                        (allout-end-of-current-subtree (not flag))
                        flag)))

;;;_  - Topic-specific
;;;_   > allout-show-entry ()
(defun allout-show-entry ()
  "Like `allout-show-current-entry', but reveals entries in hidden topics.

This is a way to give restricted peek at a concealed locality without the
expense of exposing its context, but can leave the outline with aberrant
exposure.  `allout-show-offshoot' should be used after the peek to rectify
the exposure."

  (interactive)
  (save-excursion
    (let (beg end)
      (allout-goto-prefix-doublechecked)
      (setq beg (if (allout-hidden-p) (1- (point)) (point)))
      (setq end (allout-pre-next-prefix))
      (allout-flag-region beg end nil)
      (list beg end))))
;;;_   > allout-show-children (&optional level strict)
(defun allout-show-children (&optional level strict)

  "If point is visible, show all direct subheadings of this heading.

Otherwise, do `allout-show-to-offshoot', and then show subheadings.

Optional LEVEL specifies how many levels below the current level
should be shown, or all levels if t.  Default is 1.

Optional STRICT means don't resort to -show-to-offshoot, no matter
what.  This is basically so -show-to-offshoot, which is called by
this function, can employ the pure offspring-revealing capabilities of
it.

Returns point at end of subtree that was opened, if any.  (May get a
point of non-opened subtree?)"

  (interactive "p")
  (let ((start-point (point)))
    (if (and (not strict)
             (allout-hidden-p))

        (progn (allout-show-to-offshoot) ; Point's concealed, open to
                                        ; expose it.
               ;; Then recurse, but with "strict" set so we don't
               ;; infinite regress:
               (allout-show-children level t))

      (save-excursion
        (allout-beginning-of-current-line)
        (save-restriction
          (let* (depth
                 ;; translate the level spec for this routine to the ones
                 ;; used by -chart-subtree and -chart-to-reveal:
                 (chart-level (cond ((not level) 1)
                                    ((eq level t) nil)
                                    (t level)))
                 (chart (allout-chart-subtree chart-level))
                 (to-reveal (or (allout-chart-to-reveal chart chart-level)
                                ;; interactive, show discontinuous children:
                                (and chart
                                     (allout-called-interactively-p)
                                     (save-excursion
                                       (allout-back-to-current-heading)
                                       (setq depth (allout-current-depth))
                                       (and (allout-next-heading)
                                            (> allout-recent-depth
                                               (1+ depth))))
                                     (message
                                      "Discontinuous offspring; use `%s %s'%s."
                                      (substitute-command-keys
                                       "\\[universal-argument]")
                                      (substitute-command-keys
                                       "\\[allout-shift-out]")
                                      " to elevate them.")
                                     (allout-chart-to-reveal
                                      chart (- allout-recent-depth depth))))))
            (goto-char start-point)
            (when (and strict (allout-hidden-p))
              ;; Concealed root would already have been taken care of,
              ;; unless strict was set.
              (allout-flag-region (point) (allout-snug-back) nil)
              (when allout-show-bodies
                (goto-char (car to-reveal))
                (allout-show-current-entry)))
            (while to-reveal
              (goto-char (car to-reveal))
              (allout-flag-region (save-excursion (allout-snug-back) (point))
                                  (progn (search-forward "\n" nil t)
                                         (1- (point)))
                                  nil)
              (when allout-show-bodies
                (goto-char (car to-reveal))
                (allout-show-current-entry))
              (setq to-reveal (cdr to-reveal)))))))
    ;; Compensate for `save-excursion's maintenance of point
    ;; within invisible text:
    (goto-char start-point)))
;;;_   > allout-show-to-offshoot ()
(defun allout-show-to-offshoot ()
  "Like `allout-show-entry', but reveals all concealed ancestors, as well.

Useful for coherently exposing to a random point in a hidden region."
  (interactive)
  (save-excursion
    (let ((inhibit-field-text-motion t)
          (orig-pt (point))
	  (orig-pref (allout-goto-prefix-doublechecked))
	  (last-at (point))
	  (bag-it 0))
      (while (or (> bag-it 1) (allout-hidden-p))
        (while (allout-hidden-p)
          (move-beginning-of-line 1)
          (if (allout-hidden-p) (forward-char -1)))
	(if (= last-at (setq last-at (point)))
	    ;; Oops, we're not making any progress!  Show the current topic
	    ;; completely, and try one more time here, if we haven't already.
	    (progn (beginning-of-line)
		   (allout-show-current-subtree)
		   (goto-char orig-pt)
		   (setq bag-it (1+ bag-it))
                   (if (> bag-it 1)
                       (error "allout-show-to-offshoot: %s"
                              "Stumped by aberrant nesting.")))
          (if (> bag-it 0) (setq bag-it 0))
          (allout-show-children)
          (goto-char orig-pref)))
      (goto-char orig-pt)))
  (if (allout-hidden-p)
      (allout-show-entry)))
;;;_   > allout-hide-current-entry ()
(defun allout-hide-current-entry ()
  "Hide the body directly following this heading."
  (interactive)
  (allout-back-to-current-heading)
  (save-excursion
    (let ((inhibit-field-text-motion t))
      (end-of-line))
    (allout-flag-region (point)
                        (progn (allout-end-of-entry) (point))
                        t)))
;;;_   > allout-show-current-entry (&optional arg)
(defun allout-show-current-entry (&optional arg)
  "Show body following current heading, or hide entry with universal argument."

  (interactive "P")
  (if arg
      (allout-hide-current-entry)
    (save-excursion (allout-show-to-offshoot))
    (save-excursion
      (allout-flag-region (point)
                          (progn (allout-end-of-entry t) (point))
                          nil)
      )))
;;;_   > allout-show-current-subtree (&optional arg)
(defun allout-show-current-subtree (&optional arg)
  "Show everything within the current topic.
With a repeat-count, expose this topic and its siblings."
  (interactive "P")
  (save-excursion
    (if (<= (allout-current-depth) 0)
	;; Outside any topics -- try to get to the first:
	(if (not (allout-next-heading))
	    (error "No topics")
	  ;; got to first, outermost topic -- set to expose it and siblings:
	  (message "Above outermost topic -- exposing all.")
	  (allout-flag-region (point-min)(point-max) nil))
      (allout-beginning-of-current-line)
      (if (not arg)
	  (allout-flag-current-subtree nil)
	(allout-beginning-of-level)
	(allout-expose-topic '(* :))))))
;;;_   > allout-current-topic-collapsed-p (&optional include-single-liners)
(defun allout-current-topic-collapsed-p (&optional include-single-liners)
  "True if the currently visible containing topic is already collapsed.

Single line topics intrinsically can be considered as being both
collapsed and uncollapsed.  If optional INCLUDE-SINGLE-LINERS is
true, then single-line topics are considered to be collapsed.  By
default, they are treated as being uncollapsed."
  (save-match-data
    (save-excursion
      (and
       ;; Is the topic all on one line (allowing for trailing blank line)?
       (>= (progn (allout-back-to-current-heading)
                  (let ((inhibit-field-text-motion t))
                    (move-end-of-line 1))
                  (point))
           (allout-end-of-current-subtree (not (looking-at "\n\n"))))

       (or include-single-liners
           (progn (backward-char 1) (allout-hidden-p)))))))
;;;_   > allout-hide-current-subtree (&optional just-close)
(defun allout-hide-current-subtree (&optional just-close)
  "Close the current topic, or containing topic if this one is already closed.

If this topic is closed and it's a top level topic, close this topic
and its siblings.

If optional arg JUST-CLOSE is non-nil, do not close the parent or
siblings, even if the target topic is already closed."

  (interactive)
  (let* ((from (point))
         (sibs-msg "Top-level topic already closed -- closing siblings...")
         (current-exposed (not (allout-current-topic-collapsed-p t))))
    (cond (current-exposed (allout-flag-current-subtree t))
          (just-close nil)
          ((allout-ascend) (allout-hide-current-subtree))
          (t (goto-char 0)
             (message sibs-msg)
             (allout-goto-prefix-doublechecked)
             (allout-expose-topic '(0 :))
             (message (concat sibs-msg "  Done."))))
    (goto-char from)))
;;;_   > allout-toggle-current-subtree-exposure
(defun allout-toggle-current-subtree-exposure ()
  "Show or hide the current subtree depending on its current state."
  ;; thanks to tassilo for suggesting this.
  (interactive)
  (save-excursion
    (allout-back-to-heading)
    (if (allout-hidden-p (point-at-eol))
        (allout-show-current-subtree)
      (allout-hide-current-subtree))))
;;;_   > allout-show-current-branches ()
(defun allout-show-current-branches ()
  "Show all subheadings of this heading, but not their bodies."
  (interactive)
  (let ((inhibit-field-text-motion t))
    (beginning-of-line))
  (allout-show-children t))
;;;_   > allout-hide-current-leaves ()
(defun allout-hide-current-leaves ()
  "Hide the bodies of the current topic and all its offspring."
  (interactive)
  (allout-back-to-current-heading)
  (allout-hide-region-body (point) (progn (allout-end-of-current-subtree)
                                           (point))))

;;;_  - Region and beyond
;;;_   > allout-show-all ()
(defun allout-show-all ()
  "Show all of the text in the buffer."
  (interactive)
  (message "Exposing entire buffer...")
  (allout-flag-region (point-min) (point-max) nil)
  (message "Exposing entire buffer...  Done."))
;;;_   > allout-hide-bodies ()
(defun allout-hide-bodies ()
  "Hide all of buffer except headings."
  (interactive)
  (allout-hide-region-body (point-min) (point-max)))
;;;_   > allout-hide-region-body (start end)
(defun allout-hide-region-body (start end)
  "Hide all body lines in the region, but not headings."
  (save-match-data
    (save-excursion
      (save-restriction
        (narrow-to-region start end)
        (goto-char (point-min))
        (let ((inhibit-field-text-motion t))
          (while (not (eobp))
            (end-of-line)
            (allout-flag-region (point) (allout-end-of-entry) t)
            (if (not (eobp))
                (forward-char
                 (if (looking-at "\n\n")
                     2 1)))))))))

;;;_   > allout-expose-topic (spec)
(defun allout-expose-topic (spec)
  "Apply exposure specs to successive outline topic items.

Use the more convenient frontend, `allout-new-exposure', if you don't
need evaluation of the arguments, or even better, the `allout-layout'
variable-keyed mode-activation/auto-exposure feature of allout outline
mode.  See the respective documentation strings for more details.

Cursor is left at start position.

SPEC is either a number or a list.

Successive specs on a list are applied to successive sibling topics.

A simple spec (either a number, one of a few symbols, or the null
list) dictates the exposure for the corresponding topic.

Non-null lists recursively designate exposure specs for respective
subtopics of the current topic.

The `:' repeat spec is used to specify exposure for any number of
successive siblings, up to the trailing ones for which there are
explicit specs following the `:'.

Simple (numeric and null-list) specs are interpreted as follows:

 Numbers indicate the relative depth to open the corresponding topic.
     - negative numbers force the topic to be closed before opening to the
       absolute value of the number, so all siblings are open only to
       that level.
     - positive numbers open to the relative depth indicated by the
       number, but do not force already opened subtopics to be closed.
     - 0 means to close topic -- hide all offspring.
  :  - `repeat'
       apply prior element to all siblings at current level, *up to*
       those siblings that would be covered by specs following the `:'
       on the list.  Ie, apply to all topics at level but the last
       ones.  (Only first of multiple colons at same level is
       respected -- subsequent ones are discarded.)
  *  - completely opens the topic, including bodies.
  +  - shows all the sub headers, but not the bodies
  -  - exposes the body of the corresponding topic.

Examples:
\(allout-expose-topic \\='(-1 : 0))
	Close this and all following topics at current level, exposing
	only their immediate children, but close down the last topic
	at this current level completely.
\(allout-expose-topic \\='(-1 () : 1 0))
	Close current topic so only the immediate subtopics are shown;
	show the children in the second to last topic, and completely
	close the last one.
\(allout-expose-topic \\='(-2 : -1 *))
        Expose children and grandchildren of all topics at current
	level except the last two; expose children of the second to
	last and completely open the last one."

  (interactive "xExposure spec: ")
  (if (not (listp spec))
      nil
    (let ((depth (allout-depth))
	  (max-pos 0)
	  prev-elem curr-elem
	  stay)
      (while spec
	(setq prev-elem curr-elem
	      curr-elem (car spec)
	      spec (cdr spec))
	(cond				; Do current element:
	 ((null curr-elem) nil)
	 ((symbolp curr-elem)
	  (cond ((eq curr-elem '*) (allout-show-current-subtree)
		 (if (> allout-recent-end-of-subtree max-pos)
		     (setq max-pos allout-recent-end-of-subtree)))
                ((eq curr-elem '+)
                 (if (not (allout-hidden-p))
                     (save-excursion (allout-hide-current-subtree t)))
                 (allout-show-current-branches)
		 (if (> allout-recent-end-of-subtree max-pos)
		     (setq max-pos allout-recent-end-of-subtree)))
		((eq curr-elem '-) (allout-show-current-entry))
		((eq curr-elem ':)
		 (setq stay t)
		 ;; Expand the `repeat' spec to an explicit version,
		 ;; w.r.t. remaining siblings:
		 (let ((residue	   ; = # of sibs not covered by remaining spec
			;; Dang, could be nice to make use of the chart, sigh:
			(- (length (allout-chart-siblings))
			   (length spec))))
		   (if (< 0 residue)
		       ;; Some residue -- cover it with prev-elem:
		       (setq spec (append (make-list residue prev-elem)
					  spec)))))))
	 ((numberp curr-elem)
	  (if (and (>= 0 curr-elem) (not (allout-hidden-p)))
	      (save-excursion (allout-hide-current-subtree t)
			      (if (> 0 curr-elem)
				  nil
				(if (> allout-recent-end-of-subtree max-pos)
				    (setq max-pos
					  allout-recent-end-of-subtree)))))
	  (if (> (abs curr-elem) 0)
	      (progn (allout-show-children (abs curr-elem))
		     (if (> allout-recent-end-of-subtree max-pos)
			 (setq max-pos allout-recent-end-of-subtree)))))
	  ((listp curr-elem)
	   (if (allout-descend-to-depth (1+ depth))
	       (let ((got (allout-expose-topic curr-elem)))
		 (if (and got (> got max-pos)) (setq max-pos got))))))
	(cond (stay (setq stay nil))
	      ((listp (car spec)) nil)
	      ((> max-pos (point))
	       ;; Capitalize on max-pos state to get us nearer next sibling:
	       (progn (goto-char (min (point-max) max-pos))
		      (allout-next-heading)))
	      ((allout-next-sibling depth))))
      max-pos)))
;;;_   > allout-old-expose-topic (spec &rest followers)
(defun allout-old-expose-topic (spec &rest followers)

  "Deprecated.  Use `allout-expose-topic' (with different schema
format) instead.

Dictate wholesale exposure scheme for current topic, according to SPEC.

SPEC is either a number or a list.  Optional successive args
dictate exposure for subsequent siblings of current topic.

A simple spec (either a number, a special symbol, or the null list)
dictates the overall exposure for a topic.  Non null lists are
composite specs whose first element dictates the overall exposure for
a topic, with the subsequent elements in the list interpreted as specs
that dictate the exposure for the successive offspring of the topic.

Simple (numeric and null-list) specs are interpreted as follows:

 - Numbers indicate the relative depth to open the corresponding topic:
  - negative numbers force the topic to be close before opening to the
    absolute value of the number.
  - positive numbers just open to the relative depth indicated by the number.
  - 0 just closes
 - `*' completely opens the topic, including bodies.
 - `+' shows all the sub headers, but not the bodies
 - `-' exposes the body and immediate offspring of the corresponding topic.

If the spec is a list, the first element must be a number, which
dictates the exposure depth of the topic as a whole.  Subsequent
elements of the list are nested SPECs, dictating the specific exposure
for the corresponding offspring of the topic.

Optional FOLLOWERS arguments dictate exposure for succeeding siblings."

  (interactive "xExposure spec: ")
  (let ((inhibit-field-text-motion t)
        (depth (allout-current-depth))
	max-pos)
    (cond ((null spec) nil)
	  ((symbolp spec)
	   (if (eq spec '*) (allout-show-current-subtree))
	   (if (eq spec '+) (allout-show-current-branches))
	   (if (eq spec '-) (allout-show-current-entry)))
	  ((numberp spec)
	   (if (>= 0 spec)
	       (save-excursion (allout-hide-current-subtree t)
			       (end-of-line)
			       (if (or (not max-pos)
				       (> (point) max-pos))
				   (setq max-pos (point)))
			       (if (> 0 spec)
				   (setq spec (* -1 spec)))))
	   (if (> spec 0)
	     (allout-show-children spec)))
	  ((listp spec)
	   ;(let ((got (allout-old-expose-topic (car spec))))
	   ;  (if (and got (or (not max-pos) (> got max-pos)))
	   ;	 (setq max-pos got)))
	   (let ((new-depth  (+ (allout-current-depth) 1))
		 got)
	     (setq max-pos (allout-old-expose-topic (car spec)))
	     (setq spec (cdr spec))
	     (if (and spec
		      (allout-descend-to-depth new-depth)
		      (not (allout-hidden-p)))
		 (progn (setq got (apply 'allout-old-expose-topic spec))
			(if (and got (or (not max-pos) (> got max-pos)))
			    (setq max-pos got)))))))
    (while (and followers
		(progn (if (and max-pos (< (point) max-pos))
			   (progn (goto-char max-pos)
				  (setq max-pos nil)))
		       (end-of-line)
		       (allout-next-sibling depth)))
      (allout-old-expose-topic (car followers))
      (setq followers (cdr followers)))
    max-pos))
;;;_   > allout-new-exposure '()
(defmacro allout-new-exposure (&rest spec)
  "Literal frontend for `allout-expose-topic', doesn't evaluate arguments.
Some arguments that would need to be quoted in `allout-expose-topic'
need not be quoted in `allout-new-exposure'.

Cursor is left at start position.

Use this instead of obsolete `allout-exposure'.

Examples:
\(allout-new-exposure (-1 () () () 1) 0)
	Close current topic at current level so only the immediate
	subtopics are shown, except also show the children of the
	third subtopic; and close the next topic at the current level.
\(allout-new-exposure : -1 0)
	Close all topics at current level to expose only their
	immediate children, except for the last topic at the current
	level, in which even its immediate children are hidden.
\(allout-new-exposure -2 : -1 *)
        Expose children and grandchildren of first topic at current
	level, and expose children of subsequent topics at current
	level *except* for the last, which should be opened completely."
  `(save-excursion
     (if (not (or (allout-goto-prefix-doublechecked)
		  (allout-next-heading)))
	 (error "allout-new-exposure: Can't find any outline topics"))
     (allout-expose-topic ',spec)))

;;;_ #7 Systematic outline presentation -- copying, printing, flattening

;;;_  - Mapping and processing of topics
;;;_   ( See also Subtree Charting, in Navigation code.)
;;;_   > allout-stringify-flat-index (flat-index)
(defun allout-stringify-flat-index (flat-index &optional context)
  "Convert list representing section/subsection/... to document string.

Optional arg CONTEXT indicates interior levels to include."
  (let ((delim ".")
	result
	numstr
	(context-depth (or (and context 2) 1)))
    ;; Take care of the explicit context:
    (while (> context-depth 0)
      (setq numstr (int-to-string (car flat-index))
	    flat-index (cdr flat-index)
	    result (if flat-index
		       (cons delim (cons numstr result))
		       (cons numstr result))
	    context-depth (if flat-index (1- context-depth) 0)))
    (setq delim " ")
    ;; Take care of the indentation:
    (if flat-index
	(progn
	  (while flat-index
	    (setq result
		  (cons delim
			(cons (make-string
			       (1+ (truncate (if (zerop (car flat-index))
						 1
					       (log (car flat-index) 10))))
			       ? )
			      result)))
	    (setq flat-index (cdr flat-index)))
	  ;; Dispose of single extra delim:
	  (setq result (cdr result))))
    (apply 'concat result)))
;;;_   > allout-stringify-flat-index-plain (flat-index)
(defun allout-stringify-flat-index-plain (flat-index)
  "Convert list representing section/subsection/... to document string."
  (let ((delim ".")
	result)
	(while flat-index
	  (setq result (cons (int-to-string (car flat-index))
			     (if result
				 (cons delim result))))
	  (setq flat-index (cdr flat-index)))
    (apply 'concat result)))
;;;_   > allout-stringify-flat-index-indented (flat-index)
(defun allout-stringify-flat-index-indented (flat-index)
  "Convert list representing section/subsection/... to document string."
  (let ((delim ".")
	result
	numstr)
    ;; Take care of the explicit context:
    (setq numstr (int-to-string (car flat-index))
	  flat-index (cdr flat-index)
	  result (if flat-index
		     (cons delim (cons numstr result))
		   (cons numstr result)))
    (setq delim " ")
    ;; Take care of the indentation:
    (if flat-index
	(progn
	  (while flat-index
	    (setq result
		  (cons delim
			(cons (make-string
			       (1+ (truncate (if (zerop (car flat-index))
						 1
					       (log (car flat-index) 10))))
			       ? )
			      result)))
	    (setq flat-index (cdr flat-index)))
	  ;; Dispose of single extra delim:
	  (setq result (cdr result))))
    (apply 'concat result)))
;;;_   > allout-listify-exposed (&optional start end format)
(defun allout-listify-exposed (&optional start end format)

  "Produce a list representing exposed topics in current region.

This list can then be used by `allout-process-exposed' to manipulate
the subject region.

Optional START and END indicate bounds of region.

Optional arg, FORMAT, designates an alternate presentation form for
the prefix:

 list -- Present prefix as numeric section.subsection..., starting with
	section indicated by the list, innermost nesting first.
 `indent' (symbol) --  Convert header prefixes to all white space,
		       except for distinctive bullets.

The elements of the list produced are lists that represents a topic
header and body.  The elements of that list are:

 - a number representing the depth of the topic,
 - a string representing the header-prefix, including trailing whitespace and
   bullet.
 - a string representing the bullet character,
 - and a series of strings, each containing one line of the exposed
   portion of the topic entry."

  (interactive "r")
  (save-excursion
    (let*
        ((inhibit-field-text-motion t)
         ;; state vars:
         strings prefix result depth new-depth out gone-out bullet beg
		 next done)

      (goto-char start)
      (beginning-of-line)
      ;; Goto initial topic, and register preceding stuff, if any:
      (if (> (allout-goto-prefix-doublechecked) start)
	  ;; First topic follows beginning point -- register preliminary stuff:
	  (setq result
                (list (list 0 "" nil
                            (buffer-substring-no-properties start
                                                            (1- (point)))))))
      (while (and (not done)
		  (not (eobp))		; Loop until we've covered the region.
		  (not (> (point) end)))
	(setq depth allout-recent-depth 	; Current topics depth,
	      bullet (allout-recent-bullet)	; ... bullet,
	      prefix (allout-recent-prefix)
	      beg (progn (allout-end-of-prefix t) (point))) ; and beginning.
	(setq done			; The boundary for the current topic:
	      (not (allout-next-visible-heading 1)))
	(setq new-depth allout-recent-depth)
	(setq gone-out out
	      out (< new-depth depth))
	(beginning-of-line)
	(setq next (point))
	(goto-char beg)
	(setq strings nil)
	(while (> next (point))		; Get all the exposed text in
	  (setq strings
		(cons (buffer-substring-no-properties
		       beg
					;To hidden text or end of line:
		       (progn
                         (end-of-line)
                         (allout-back-to-visible-text)))
		      strings))
	  (when (< (point) next)      ; Resume from after hid text, if any.
            (line-move 1)
            (beginning-of-line))
	  (setq beg (point)))
	;; Accumulate list for this topic:
	(setq strings (nreverse strings))
	(setq result
	      (cons
	       (if format
		   (let ((special (if (string-match
				       (regexp-quote bullet)
				       allout-distinctive-bullets-string)
				      bullet)))
		     (cond ((listp format)
			    (list depth
				  (if allout-flattened-numbering-abbreviation
				      (allout-stringify-flat-index format
								    gone-out)
				      (allout-stringify-flat-index-plain
				       format))
				  strings
				  special))
			   ((eq format 'indent)
			    (if special
				(list depth
				      (concat (make-string (1+ depth) ? )
					      (substring prefix -1))
				      strings)
			      (list depth
				    (make-string depth ? )
				    strings)))
			   (t (error "allout-listify-exposed: %s %s"
				     "invalid format" format))))
		 (list depth prefix strings))
		    result))
	;; Reassess format, if any:
	(if (and format (listp format))
	    (cond ((= new-depth depth)
		   (setq format (cons (1+ (car format))
					  (cdr format))))
		  ((> new-depth depth)	; descending -- assume by 1:
		   (setq format (cons 1 format)))
		  (t
					; Pop the residue:
		   (while (< new-depth depth)
		       (setq format (cdr format))
		       (setq depth (1- depth)))
					; And increment the current one:
		     (setq format
			   (cons (1+ (or (car format)
					 -1))
				 (cdr format)))))))
      ;; Put the list with first at front, to last at back:
      (nreverse result))))

(define-obsolete-function-alias 'allout-region-active-p 'region-active-p "28.1")

;;_   > allout-process-exposed (&optional func from to frombuf
;;;					    tobuf format)
(defun allout-process-exposed (&optional func from to frombuf tobuf
					 format _start-num)
  "Map function on exposed parts of current topic; results to another buffer.

All args are options; default values itemized below.

Apply FUNCTION to exposed portions FROM position TO position in buffer
FROMBUF to buffer TOBUF.  Sixth optional arg, FORMAT, designates an
alternate presentation form:

 `flat' -- Present prefix as numeric section.subsection..., starting with
	 section indicated by the START-NUM, innermost nesting first.
 X`flat-indented' -- Prefix is like `flat' for first topic at each
 X		   level, but subsequent topics have only leaf topic
 X		   number, padded with blanks to line up with first.
 `indent' (symbol) --  Convert header prefixes to all white space,
		       except for distinctive bullets.

Defaults:
  FUNCTION:	`allout-insert-listified'
  FROM:		region start, if region active, else start of buffer
  TO:		region end, if region active, else end of buffer
  FROMBUF:	current buffer
  TOBUF:	buffer name derived: \"*current-buffer-name exposed*\"
  FORMAT:	nil"

					; Resolve arguments,
					; defaulting if necessary:
  (if (not func) (setq func 'allout-insert-listified))
  (if (not (and from to))
      (if (region-active-p)
	  (setq from (region-beginning) to (region-end))
	(setq from (point-min) to (point-max))))
  (if frombuf
      (if (not (bufferp frombuf))
	  ;; Specified but not a buffer -- get it:
	  (let ((got (get-buffer frombuf)))
	    (if (not got)
		(error "allout-process-exposed: source buffer %s not found."
		       frombuf)
	      (setq frombuf got))))
    ;; not specified -- default it:
    (setq frombuf (current-buffer)))
  (if tobuf
      (if (not (bufferp tobuf))
	  (setq tobuf (get-buffer-create tobuf)))
    ;; not specified -- default it:
    (setq tobuf (concat "*" (buffer-name frombuf) " exposed*")))
  (if (listp format)
      (nreverse format))

  (let* ((listified
	  (progn (set-buffer frombuf)
		 (allout-listify-exposed from to format))))
    (set-buffer tobuf)
    (mapc func listified)
    (pop-to-buffer tobuf)))

;;;_  - Copy exposed
;;;_   > allout-insert-listified (listified)
(defun allout-insert-listified (listified)
  "Insert contents of listified outline portion in current buffer.

LISTIFIED is a list representing each topic header and body:

 `(depth prefix text)'

or `(depth prefix text bullet-plus)'

If `bullet-plus' is specified, it is inserted just after the entire prefix."
  (setq listified (cdr listified))
  (let ((prefix (prog1
		    (car listified)
		  (setq listified (cdr listified))))
	(text (prog1
		  (car listified)
		(setq listified (cdr listified))))
	(bullet-plus (car listified)))
    (insert prefix)
    (if bullet-plus (insert (concat " " bullet-plus)))
    (while text
      (insert (car text))
      (if (setq text (cdr text))
	  (insert "\n")))
    (insert "\n")))
;;;_   > allout-copy-exposed-to-buffer (&optional arg tobuf format)
(defun allout-copy-exposed-to-buffer (&optional arg tobuf format)
  "Duplicate exposed portions of current outline to another buffer.

Other buffer has current buffers name with \" exposed\" appended to it.

With repeat count, copy the exposed parts of only the current topic.

Optional second arg TOBUF is target buffer name.

Optional third arg FORMAT, if non-nil, symbolically designates an
alternate presentation format for the outline:

 `flat'   - Convert topic header prefixes to numeric
	    section.subsection... identifiers.
 `indent' - Convert header prefixes to all white space, except for
	    distinctive bullets.
 `indent-flat' - The best of both - only the first of each level has
		 the full path, the rest have only the section number
		 of the leaf, preceded by the right amount of indentation."

  (interactive "P")
  (if (not tobuf)
      (setq tobuf (get-buffer-create (concat "*" (buffer-name) " exposed*"))))
  (let* ((start-pt (point))
	 (beg (if arg (allout-back-to-current-heading) (point-min)))
	 (end (if arg (allout-end-of-current-subtree) (point-max)))
	 (buf (current-buffer))
	 (start-list ()))
    (if (eq format 'flat)
	(setq format (if arg (save-excursion
				   (goto-char beg)
				   (allout-topic-flat-index))
			   '(1))))
    (with-current-buffer tobuf (erase-buffer))
    (allout-process-exposed 'allout-insert-listified
			     beg
			     end
			     (current-buffer)
			     tobuf
			     format start-list)
    (goto-char (point-min))
    (pop-to-buffer buf)
    (goto-char start-pt)))
;;;_   > allout-flatten-exposed-to-buffer (&optional arg tobuf)
(defun allout-flatten-exposed-to-buffer (&optional arg tobuf)
  "Present numeric outline of outline's exposed portions in another buffer.

The resulting outline is not compatible with outline mode -- use
`allout-copy-exposed-to-buffer' if you want that.

Use `allout-indented-exposed-to-buffer' for indented presentation.

With repeat count, copy the exposed portions of only current topic.

Other buffer has current buffer's name with \" exposed\" appended to
it, unless optional second arg TOBUF is specified, in which case it is
used verbatim."
  (interactive "P")
  (allout-copy-exposed-to-buffer arg tobuf 'flat))
;;;_   > allout-indented-exposed-to-buffer (&optional arg tobuf)
(defun allout-indented-exposed-to-buffer (&optional arg tobuf)
  "Present indented outline of outline's exposed portions in another buffer.

The resulting outline is not compatible with outline mode -- use
`allout-copy-exposed-to-buffer' if you want that.

Use `allout-flatten-exposed-to-buffer' for numeric sectional presentation.

With repeat count, copy the exposed portions of only current topic.

Other buffer has current buffer's name with \" exposed\" appended to
it, unless optional second arg TOBUF is specified, in which case it is
used verbatim."
  (interactive "P")
  (allout-copy-exposed-to-buffer arg tobuf 'indent))

;;;_  - LaTeX formatting
;;;_   > allout-latex-verb-quote (string &optional flow)
(defun allout-latex-verb-quote (string &optional _flow)
  "Return copy of STRING for literal reproduction across LaTeX processing.
Expresses the original characters (including carriage returns) of the
string across LaTeX processing."
  (mapconcat (function
	      (lambda (char)
		(cond ((memq char '(?\\ ?$ ?% ?# ?& ?{ ?} ?_ ?^ ?- ?*))
		       (concat "\\char" (number-to-string char) "{}"))
		      ((= char ?\n) "\\\\")
		      (t (char-to-string char)))))
	     string
	     ""))
;;;_   > allout-latex-verbatim-quote-curr-line ()
(defun allout-latex-verbatim-quote-curr-line ()
  "Express line for exact (literal) representation across LaTeX processing.

Adjust line contents so it is unaltered (from the original line)
across LaTeX processing, within the context of a `verbatim'
environment.  Leaves point at the end of the line."
  (let ((inhibit-field-text-motion t))
    (beginning-of-line)
    (let (;(beg (point))
          (end (point-at-eol)))
      (save-match-data
        (while (re-search-forward "\\\\"
  ;;"\\\\\\|\\{\\|\\}\\|\\_\\|\\$\\|\\\"\\|\\&\\|\\^\\|\\-\\|\\*\\|#"
                                  end	; bounded by end-of-line
                                  1) ; no matches, move to end & return nil
          (goto-char (match-beginning 2))
          (insert "\\")
          (setq end (1+ end))
          (goto-char (1+ (match-end 2))))))))
;;;_   > allout-insert-latex-header (buffer)
(defun allout-insert-latex-header (buffer)
  "Insert initial LaTeX commands at point in BUFFER."
  ;; Much of this is being derived from the stuff in appendix of E in
  ;; the TeXBook, pg 421.
  (set-buffer buffer)
  (let ((doc-style (format "\n\\documentstyle{%s}\n"
			   "report"))
	(page-numbering (if allout-number-pages
			    "\\pagestyle{empty}\n"
			  ""))
	(titlecmd (format "\\newcommand{\\titlecmd}[1]{{%s #1}}\n"
			  allout-title-style))
	(labelcmd (format "\\newcommand{\\labelcmd}[1]{{%s #1}}\n"
			  allout-label-style))
	(headlinecmd (format "\\newcommand{\\headlinecmd}[1]{{%s #1}}\n"
			     allout-head-line-style))
	(bodylinecmd (format "\\newcommand{\\bodylinecmd}[1]{{%s #1}}\n"
			     allout-body-line-style))
	(setlength (format "%s%s%s%s"
			   "\\newlength{\\stepsize}\n"
			   "\\setlength{\\stepsize}{"
			   allout-indent
			   "}\n"))
	(oneheadline (format "%s%s%s%s%s%s%s"
			     "\\newcommand{\\OneHeadLine}[3]{%\n"
			     "\\noindent%\n"
			     "\\hspace*{#2\\stepsize}%\n"
			     "\\labelcmd{#1}\\hspace*{.2cm}"
			     "\\headlinecmd{#3}\\\\["
			     allout-line-skip
			     "]\n}\n"))
	(onebodyline (format "%s%s%s%s%s%s"
			       "\\newcommand{\\OneBodyLine}[2]{%\n"
			       "\\noindent%\n"
			       "\\hspace*{#1\\stepsize}%\n"
			       "\\bodylinecmd{#2}\\\\["
			       allout-line-skip
			       "]\n}\n"))
	(begindoc "\\begin{document}\n\\begin{center}\n")
	(title (format "%s%s%s%s"
		       "\\titlecmd{"
		       (allout-latex-verb-quote (if allout-title
						(condition-case nil
						    (eval allout-title)
						  (error "<unnamed buffer>"))
					      "Unnamed Outline"))
		       "}\n"
		       "\\end{center}\n\n"))
	(hsize "\\hsize = 7.5 true in\n")
	(hoffset "\\hoffset = -1.5 true in\n")
	(vspace "\\vspace{.1cm}\n\n"))
    (insert (concat doc-style
		    page-numbering
		    titlecmd
		    labelcmd
		    headlinecmd
		    bodylinecmd
		    setlength
		    oneheadline
		    onebodyline
		    begindoc
		    title
		    hsize
		    hoffset
		    vspace)
	    )))
;;;_   > allout-insert-latex-trailer (buffer)
(defun allout-insert-latex-trailer (buffer)
  "Insert concluding LaTeX commands at point in BUFFER."
  (set-buffer buffer)
  (insert "\n\\end{document}\n"))
;;;_   > allout-latexify-one-item (depth prefix bullet text)
(defun allout-latexify-one-item (depth _prefix bullet text)
  "Insert LaTeX commands for formatting one outline item.

Args are the topics numeric DEPTH, the header PREFIX lead string, the
BULLET string, and a list of TEXT strings for the body."
  (let* ((head-line (if text (car text)))
	 (body-lines (cdr text))
	 (curr-line)
	 body-content bop)
					; Do the head line:
    (insert (concat "\\OneHeadLine{\\verb\1 "
                    (allout-latex-verb-quote bullet)
                    "\1}{"
                    depth
                    "}{\\verb\1 "
                    (if head-line
                        (allout-latex-verb-quote head-line)
                      "")
                    "\1}\n"))
    (if (not body-lines)
	nil
      ;;(insert "\\beginlines\n")
      (insert "\\begin{verbatim}\n")
      (while body-lines
	(setq curr-line (car body-lines))
	(if (and (not body-content)
		 (not (string-match "^\\s-*$" curr-line)))
	    (setq body-content t))
					; Mangle any occurrences of
					; "\end{verbatim}" in text,
					; it's special:
	(if (and body-content
		 (setq bop (string-match "\\\\end{verbatim}" curr-line)))
	    (setq curr-line (concat (substring curr-line 0 bop)
				    ">"
				    (substring curr-line bop))))
	;;(insert "|" (car body-lines) "|")
	(insert curr-line)
	(allout-latex-verbatim-quote-curr-line)
	(insert "\n")
	(setq body-lines (cdr body-lines)))
      (if body-content
	  (setq body-content nil)
	(forward-char -1)
	(insert "\\ ")
	(forward-char 1))
      ;;(insert "\\endlines\n")
      (insert "\\end{verbatim}\n")
      )))
;;;_   > allout-latexify-exposed (arg &optional tobuf)
(defun allout-latexify-exposed (arg &optional tobuf)
  "Format current topics exposed portions to TOBUF for LaTeX processing.
TOBUF defaults to a buffer named the same as the current buffer, but
with \"*\" prepended and \" latex-formed*\" appended.

With repeat count, copy the exposed portions of entire buffer."

  (interactive "P")
  (if (not tobuf)
      (setq tobuf
	    (get-buffer-create (concat "*" (buffer-name) " latexified*"))))
  (let* ((start-pt (point))
	 (beg (if arg (point-min) (allout-back-to-current-heading)))
	 (end (if arg (point-max) (allout-end-of-current-subtree)))
	 (buf (current-buffer)))
    (set-buffer tobuf)
    (erase-buffer)
    (allout-insert-latex-header tobuf)
    (goto-char (point-max))
    (allout-process-exposed 'allout-latexify-one-item
			     beg
			     end
			     buf
			     tobuf)
    (goto-char (point-max))
    (allout-insert-latex-trailer tobuf)
    (goto-char (point-min))
    (pop-to-buffer buf)
    (goto-char start-pt)))

;;;_ #8 Encryption
;;;_  > allout-toggle-current-subtree-encryption (&optional keymode-cue)
(defun allout-toggle-current-subtree-encryption (&optional keymode-cue)
  "Encrypt clear or decrypt encoded topic text.

Allout uses Emacs `epg' library to perform encryption.  Symmetric
and keypair encryption are supported.  All encryption is ascii
armored.

Entry encryption defaults to symmetric key mode unless keypair
recipients are associated with the file (see
`epa-file-encrypt-to') or the function is invoked with a
\(KEYMODE-CUE) universal argument greater than 1.

When encrypting, KEYMODE-CUE universal argument greater than 1
causes prompting for recipients for public-key keypair
encryption.  Selecting no recipients results in symmetric key
encryption.

Further, encrypting with a KEYMODE-CUE universal argument greater
than 4 - eg, preceded by a doubled Ctrl-U - causes association of
the specified recipients with the file, replacing those currently
associated with it.  This can be used to dissociate any
recipients with the file, by selecting no recipients in the
dialog.

Encrypted topic's bullets are set to a `~' to signal that the
contents of the topic (body and subtopics, but not heading) is
pending encryption or encrypted.  `*' asterisk immediately after
the bullet signals that the body is encrypted, its absence means
the topic is meant to be encrypted but is not currently.  When a
file with topics pending encryption is saved, topics pending
encryption are encrypted.  See `allout-encrypt-unencrypted-on-saves'
for auto-encryption specifics.

*NOTE WELL* that automatic encryption that happens during saves will
default to symmetric encryption -- you must deliberately (re)encrypt key-pair
encrypted topics if you want them to continue to use the key-pair cipher.

Level-one topics, with prefix consisting solely of an `*' asterisk, cannot be
encrypted.  If you want to encrypt the contents of a top-level topic, use
\\[allout-shift-in] to increase its depth."
  (interactive "P")
  (save-excursion
    (allout-back-to-current-heading)
    (allout-toggle-subtree-encryption keymode-cue)))
;;;_  > allout-toggle-subtree-encryption (&optional keymode-cue)
(defun allout-toggle-subtree-encryption (&optional keymode-cue)
  "Encrypt clear text or decrypt encoded topic contents (body and subtopics.)

Entry encryption defaults to symmetric key mode unless keypair
recipients are associated with the file (see
`epa-file-encrypt-to') or the function is invoked with a
\(KEYMODE-CUE) universal argument greater than 1.

When encrypting, KEYMODE-CUE universal argument greater than 1
causes prompting for recipients for public-key keypair
encryption.  Selecting no recipients results in symmetric key
encryption.

Further, encrypting with a KEYMODE-CUE universal argument greater
than 4 - eg, preceded by a doubled Ctrl-U - causes association of
the specified recipients with the file, replacing those currently
associated with it.  This can be used to dissociate any
recipients with the file, by selecting no recipients in the
dialog.

Encryption and decryption uses the Emacs `epg' library.

Encrypted text will be ascii-armored.

See `allout-toggle-current-subtree-encryption' for more details."

  (interactive "P")
  (save-excursion
    (allout-end-of-prefix t)

    (if (= allout-recent-depth 1)
        (error (concat "Cannot encrypt or decrypt level 1 topics -"
                       " shift it in to make it encryptable")))

    (let* ((allout-buffer (current-buffer))
           ;; for use with allout-auto-save-temporarily-disabled, if necessary:
           (was-buffer-saved-size buffer-saved-size)
           ;; Assess location:
           (bullet-pos allout-recent-prefix-beginning)
           (after-bullet-pos (point))
           (was-encrypted
            (progn (if (= (point-max) after-bullet-pos)
                       (error "no body to encrypt"))
                   (allout-encrypted-topic-p)))
           (was-collapsed (if (not (search-forward "\n" nil t))
                              nil
                            (backward-char 1)
                            (allout-hidden-p)))
           (subtree-beg (1+ (point)))
           (subtree-end (allout-end-of-subtree))
           (subject-text (buffer-substring-no-properties subtree-beg
                                                         subtree-end))
           (subtree-end-char (char-after (1- subtree-end)))
           (subtree-trailing-char (char-after subtree-end))
           ;; kluge -- result-text needs to be nil, but we also want to
           ;;         check for the error condition
           (result-text (if (or (string= "" subject-text)
                                (string= "\n" subject-text))
                            (error "No topic contents to %scrypt"
                                   (if was-encrypted "de" "en"))
                          nil))
           ;; Assess key parameters:
           (was-coding-system buffer-file-coding-system))

      (when (not was-encrypted)
        ;; ensure that non-ascii chars pending encryption are noticed before
        ;; they're encrypted, so the coding system is set to accommodate
        ;; them.
        (setq buffer-file-coding-system
              (select-safe-coding-system subtree-beg subtree-end))
        ;; if the coding system for the text being encrypted is different
        ;; from that prevailing, then there a real risk that the coding
        ;; system can't be noticed by emacs when the file is visited.  to
        ;; mitigate that, offer to preserve the coding system using a file
        ;; local variable.
        (if (and (not (equal buffer-file-coding-system
                             was-coding-system))
                 (yes-or-no-p
                  (format (concat "Register coding system %s as file local"
                                  " var?  Necessary when only encrypted text"
                                  " is in that coding system. ")
                          buffer-file-coding-system)))
            (allout-adjust-file-variable "buffer-file-coding-system"
                                         buffer-file-coding-system)))

      (setq result-text
            (allout-encrypt-string subject-text was-encrypted
                                   (current-buffer) keymode-cue))

       ;; Replace the subtree with the processed product.
      (allout-unprotected
       (progn
         (set-buffer allout-buffer)
         (delete-region subtree-beg subtree-end)
         (insert result-text)
         (if was-collapsed
             (allout-flag-region (1- subtree-beg) (point) t))
         ;; adjust trailing-blank-lines to preserve topic spacing:
         (if (not was-encrypted)
             (if (and (= subtree-end-char ?\n)
                      (= subtree-trailing-char ?\n))
                 (insert subtree-trailing-char)))
         ;; Ensure that the item has an encrypted-entry bullet:
         (if (not (string= (buffer-substring-no-properties
                            (1- after-bullet-pos) after-bullet-pos)
                           allout-topic-encryption-bullet))
             (progn (goto-char (1- after-bullet-pos))
                    (delete-char 1)
                    (insert allout-topic-encryption-bullet)))
         (if was-encrypted
             ;; Remove the is-encrypted bullet qualifier:
             (progn (goto-char after-bullet-pos)
                    (delete-char 1))
           ;; Add the is-encrypted bullet qualifier:
           (goto-char after-bullet-pos)
           (insert "*"))))

      ;; adjust buffer's auto-save eligibility:
      (if was-encrypted
          (allout-inhibit-auto-save-info-for-decryption was-buffer-saved-size)
        (allout-maybe-resume-auto-save-info-after-encryption))

      (run-hook-with-args 'allout-structure-added-functions
                          bullet-pos subtree-end))))

(declare-function epg-context-set-passphrase-callback "epg"
                  (context passphrase-callback))
(declare-function epg-list-keys "epg" (context &optional name mode))
(declare-function epg-decrypt-string "epg" (context cipher))
(declare-function epg-encrypt-string "epg"
                  (context plain recipients &optional sign always-trust))
(declare-function epg-user-id-string "epg" (user-id) t)
(declare-function epg-key-user-id-list "epg" (key) t)

;;;_  > allout-encrypt-string (text decrypt allout-buffer keymode-cue
;;;                                 &optional rejected)
(defun allout-encrypt-string (text decrypt allout-buffer keymode-cue
                                   &optional rejected)
  "Encrypt or decrypt message TEXT.

Returns the resulting string, or nil if the transformation fails.

If DECRYPT is true (default false), then decrypt instead of encrypt.

ALLOUT-BUFFER identifies the buffer containing the text.

Entry encryption defaults to symmetric key mode unless keypair
recipients are associated with the file (see
`epa-file-encrypt-to') or the function is invoked with a
\(KEYMODE-CUE) universal argument greater than 1.

When encrypting, KEYMODE-CUE universal argument greater than 1
causes prompting for recipients for public-key keypair
encryption.  Selecting no recipients results in symmetric key
encryption.

Further, encrypting with a KEYMODE-CUE universal argument greater
than 4 - eg, preceded by a doubled Ctrl-U - causes association of
the specified recipients with the file, replacing those currently
associated with it.  This can be used to dissociate any
recipients with the file, by selecting no recipients in the
dialog.

Optional REJECTED is for internal use, to convey the number of
rejections due to matches against
`allout-encryption-ciphertext-rejection-regexps', as limited by
`allout-encryption-ciphertext-rejection-ceiling'.

NOTE: A few GnuPG v2 versions improperly preserve incorrect
symmetric decryption keys, preventing entry of the correct key on
subsequent decryption attempts until the cache times-out.  That
can take several minutes.  (Decryption of other entries is not
affected.)  Upgrade your EasyPG version, if you can, and you can
deliberately clear your gpg-agent's cache by sending it a `-HUP'
signal."

  (require 'epg)
  (require 'epa)

  (let* ((epg-context (let* ((context (epg-make-context nil t)))
                        (epg-context-set-passphrase-callback
                         context #'epa-passphrase-callback-function)
                        context))

         (encoding (with-current-buffer allout-buffer
                     buffer-file-coding-system))
         (multibyte (with-current-buffer allout-buffer
                      enable-multibyte-characters))
         ;; "sanitization" avoids encryption results that are outline structure.
         (sani-regexps 'allout-encryption-plaintext-sanitization-regexps)
         (strip-plaintext-regexps (if (not decrypt)
                                      (allout-get-configvar-values
                                       sani-regexps)))
         (rejection-regexps 'allout-encryption-ciphertext-rejection-regexps)
         (reject-ciphertext-regexps (if (not decrypt)
                                        (allout-get-configvar-values
                                         rejection-regexps)))
         (rejected (or rejected 0))
         (rejections-left (- allout-encryption-ciphertext-rejection-ceiling
                             rejected))
         (keypair-mode (cond (decrypt 'decrypting)
                             ((<= (prefix-numeric-value keymode-cue) 1)
                              'default)
                             ((<= (prefix-numeric-value keymode-cue) 4)
                              'prompt)
                             ((> (prefix-numeric-value keymode-cue) 4)
                              'prompt-save)))
         (keypair-message (concat "Select encryption recipients.\n"
                                  "Symmetric encryption is done if no"
                                  " recipients are selected.  "))
         (encrypt-to (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to))
         recipients
         massaged-text
         result-text
         )

    ;; Massage the subject text for encoding and filtering.
    (with-temp-buffer
      (insert text)
      ;; convey the text characteristics of the original buffer:
      (set-buffer-multibyte multibyte)
      (when encoding
        (set-buffer-file-coding-system encoding)
        (if (not decrypt)
            (encode-coding-region (point-min) (point-max) encoding)))

      ;; remove sanitization regexps matches before encrypting:
      (when (and strip-plaintext-regexps (not decrypt))
        (dolist (re strip-plaintext-regexps)
          (let ((re (if (listp re) (car re) re))
                (replacement (if (listp re) (cadr re) "")))
            (goto-char (point-min))
            (save-match-data
              (while (re-search-forward re nil t)
                (replace-match replacement nil nil))))))
      (setq massaged-text (buffer-substring-no-properties (point-min)
                                                          (point-max))))
    ;; determine key mode and, if keypair, recipients:
    (setq recipients
          (pcase keypair-mode

            ('decrypting nil)

            ('default (if encrypt-to (epg-list-keys epg-context encrypt-to)))

            ((or 'prompt 'prompt-save)
             (save-window-excursion
               (epa-select-keys epg-context keypair-message)))))

    (setq result-text
          (if decrypt
              (condition-case err
                  (epg-decrypt-string epg-context
                                      (encode-coding-string massaged-text
                                                            (or encoding 'utf-8)))
                (epg-error
                 (signal 'egp-error
                         (cons (concat (cadr err) " - gpg version problem?")
                               (cddr err)))))
            (replace-regexp-in-string "\n$" ""
             (epg-encrypt-string epg-context
                                 (encode-coding-string massaged-text
                                                       (or encoding 'utf-8))
                                 recipients))))

    ;; validate result -- non-empty
    (if (not result-text)
        (error "%scryption failed." (if decrypt "De" "En")))


    (when (eq keypair-mode 'prompt-save)
      ;; set epa-file-encrypt-to in the buffer:
      (setq epa-file-encrypt-to (mapcar (lambda (key)
                                          (epg-user-id-string
                                           (car (epg-key-user-id-list key))))
                                        recipients))
      ;; change the file variable:
      (allout-adjust-file-variable "epa-file-encrypt-to" epa-file-encrypt-to))

    (cond
     ;; Retry (within limit) if ciphertext contains rejections:
     ((and (not decrypt)
           ;; Check for disqualification of this ciphertext:
           (let ((regexps reject-ciphertext-regexps)
                 reject-it)
             (while (and regexps (not reject-it))
               (setq reject-it (string-match (car regexps) result-text))
               (pop regexps))
             reject-it))
      (setq rejections-left (1- rejections-left))
      (if (<= rejections-left 0)
          (error (concat "Ciphertext rejected too many times"
                         " (%s), per `%s'")
                 allout-encryption-ciphertext-rejection-ceiling
                 'allout-encryption-ciphertext-rejection-regexps)
        ;; try again (gpg-agent may have the key cached):
        (allout-encrypt-string text decrypt allout-buffer keypair-mode
                               (1+ rejected))))

     ;; Barf if encryption yields extraordinary control chars:
     ((and (not decrypt)
           (string-match "[\C-a\C-k\C-o-\C-z\C-@]"
                         result-text))
      (error (concat "Encryption produced non-armored text, which"
                     "conflicts with allout mode -- reconfigure!")))
     (t result-text))))
;;;_  > allout-inhibit-auto-save-info-for-decryption
(defun allout-inhibit-auto-save-info-for-decryption (was-buffer-saved-size)
  "Temporarily prevent auto-saves in this buffer when an item is decrypted.

WAS-BUFFER-SAVED-SIZE is the value of `buffer-saved-size' *before*
the decryption."
  (when (not (or (= buffer-saved-size -1) (= was-buffer-saved-size -1)))
    (setq allout-auto-save-temporarily-disabled was-buffer-saved-size
          buffer-saved-size -1)))
;;;_  > allout-maybe-resume-auto-save-info-after-encryption ()
(defun allout-maybe-resume-auto-save-info-after-encryption ()
  "Restore auto-save info, *if* there are no topics pending encryption."
  (when (and allout-auto-save-temporarily-disabled
             (= buffer-saved-size -1)
             (save-excursion
               (save-restriction
                 (widen)
                 (goto-char (point-min))
                 (not (allout-next-topic-pending-encryption)))))
    (setq buffer-saved-size allout-auto-save-temporarily-disabled
          allout-auto-save-temporarily-disabled nil)))

;;;_  > allout-encrypted-topic-p ()
(defun allout-encrypted-topic-p ()
  "True if the current topic is encryptable and encrypted."
  (save-excursion
    (allout-end-of-prefix t)
    (and (string= (buffer-substring-no-properties (1- (point)) (point))
                  allout-topic-encryption-bullet)
         (save-match-data (looking-at "\\*")))
    )
  )
;;;_  > allout-next-topic-pending-encryption ()
(defun allout-next-topic-pending-encryption ()
  "Return the point of the next topic pending encryption, or nil if none.

Such a topic has the `allout-topic-encryption-bullet' without an
immediately following `*' that would mark the topic as being encrypted.
It must also have content."
  (let (done got content-beg)
    (save-match-data
      (while (not done)

        (if (not (re-search-forward
                  (format "\\(\\`\\|\n\\)%s *%s[^*]"
                          (regexp-quote allout-header-prefix)
                          (regexp-quote allout-topic-encryption-bullet))
                  nil t))
            (setq got nil
                  done t)
          (goto-char (setq got (match-beginning 0)))
          (when (= (following-char) ?\n) (forward-char 1))
          (setq got (point)))

        (cond ((not got)
               (setq done t))

              ((not (search-forward "\n"))
               (setq got nil
                     done t))

              ((eobp)
               (setq got nil
                     done t))

              (t
               (setq content-beg (point))
               (backward-char 1)
               (allout-end-of-subtree)
               (if (<= (point) content-beg)
                   ;; Continue looking
                   (setq got nil)
                 ;; Got it!
                 (setq done t)))
              )
        )
      (if got
          (goto-char got))
      )
    )
  )
;;;_  > allout-encrypt-decrypted ()
(defun allout-encrypt-decrypted ()
  "Encrypt topics pending encryption except those containing exemption point.

If a topic that is currently being edited was encrypted, we return a list
containing the location of the topic and the location of the cursor just
before the topic was encrypted.  This can be used, eg, to decrypt the topic
and exactly resituate the cursor if this is being done as part of a file
save.  See `allout-encrypt-unencrypted-on-saves' for more info."

  (interactive "p")
  (save-match-data
    (save-excursion
      (let* ((current-mark (point-marker))
             (current-mark-position (marker-position current-mark))
             was-modified
             bo-subtree
             editing-topic editing-point)
        (goto-char (point-min))
        (while (allout-next-topic-pending-encryption)
          (setq was-modified (buffer-modified-p))
          (when (save-excursion
                  (and (boundp 'allout-encrypt-unencrypted-on-saves)
                       allout-encrypt-unencrypted-on-saves
                       (setq bo-subtree (re-search-forward "$"))
                       (not (allout-hidden-p))
                       (>= current-mark (point))
                       (allout-end-of-current-subtree)
                       (<= current-mark (point))))
            (setq editing-topic (point)
                  ;; we had to wait for this 'til now so prior topics are
                  ;; encrypted, any relevant text shifts are in place:
                  editing-point (- current-mark-position
                                   (allout-count-trailing-whitespace-region
                                    bo-subtree current-mark-position))))
          (allout-toggle-subtree-encryption)
          (if (not was-modified)
              (set-buffer-modified-p nil))
          )
        (if (not was-modified)
            (set-buffer-modified-p nil))
        (if editing-topic (list editing-topic editing-point))
        )
      )
    )
  )

;;;_ #9 miscellaneous
;;;_  : Mode:
;;;_   > outlineify-sticky ()
;; outlinify-sticky is correct spelling; provide this alias for sticklers:
;;;###autoload
(defalias 'outlinify-sticky 'outlineify-sticky)
;;;###autoload
(defun outlineify-sticky (&optional _arg)
  "Activate outline mode and establish file var so it is started subsequently.

See `allout-layout' and customization of `allout-auto-activation'
for details on preparing Emacs for automatic allout activation."

  (interactive "P")

  (if (allout-mode-p) (allout-mode))    ; deactivate so we can re-activate...
  (allout-mode)

  (save-excursion
    (goto-char (point-min))
    (if (allout-goto-prefix)
	t
      (allout-open-topic 2)
      (insert (substitute-command-keys
               (concat "Dummy outline topic header -- see"
                       " `allout-mode' docstring: `\\[describe-mode]'.")))
      (allout-adjust-file-variable
       "allout-layout" (or allout-layout '(-1 : 0))))))
;;;_   > allout-file-vars-section-data ()
(defun allout-file-vars-section-data ()
  "Return data identifying the file-vars section, or nil if none.

Returns a list of the form (BEGINNING-POINT PREFIX-STRING SUFFIX-STRING)."
  ;; minimally gleaned from emacs 21.4 files.el hack-local-variables function.
  (let (beg prefix suffix)
    (save-excursion
      (goto-char (point-max))
      (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
      (if (let ((case-fold-search t))
	    (not (search-forward "Local Variables:" nil t)))
          nil
        (setq beg (- (point) 16))
        (setq suffix (buffer-substring-no-properties
                      (point)
                      (progn (if (search-forward "\n" nil t)
                                 (forward-char -1))
                             (point))))
        (setq prefix (buffer-substring-no-properties
                      (progn (if (search-backward "\n" nil t)
                                 (forward-char 1))
                             (point))
                      beg))
        (list beg prefix suffix))
      )
    )
  )
;;;_   > allout-adjust-file-variable (varname value)
(defun allout-adjust-file-variable (varname value)
  "Adjust the setting of an Emacs file variable named VARNAME to VALUE.

This activity is inhibited if either `enable-local-variables' or
`allout-enable-file-variable-adjustment' are nil.

When enabled, an entry for the variable is created if not already present,
or changed if established with a different value.  The section for the file
variables, itself, is created if not already present.  When created, the
section lines (including the section line) exist as second-level topics in
a top-level topic at the end of the file.

`enable-local-variables' must be true for any of this to happen."
  (if (not (and enable-local-variables
                allout-enable-file-variable-adjustment))
      nil
    (save-excursion
      (let ((inhibit-field-text-motion t)
            (section-data (allout-file-vars-section-data))
            beg prefix suffix)
        (if section-data
            (setq beg (car section-data)
                  prefix (cadr section-data)
                  suffix (car (cddr section-data)))
          ;; create the section
          (goto-char (point-max))
          (open-line 1)
          (allout-open-topic 0)
          (end-of-line)
          (insert "Local emacs vars.\n")
          (allout-open-topic 1)
          (setq beg (point)
                suffix ""
                prefix (buffer-substring-no-properties (progn
                                                         (beginning-of-line)
                                                         (point))
                                                       beg))
          (goto-char beg)
          (insert "Local variables:\n")
          (allout-open-topic 0)
          (insert "End:\n")
          )
        ;; look for existing entry or create one, leaving point for insertion
        ;; of new value:
        (goto-char beg)
        (allout-show-to-offshoot)
        (if (search-forward (concat "\n" prefix varname ":") nil t)
            (let* ((value-beg (point))
                   (line-end (progn (if (search-forward "\n" nil t)
                                        (forward-char -1))
                                    (point)))
                   (value-end (- line-end (length suffix))))
              (if (> value-end value-beg)
                  (delete-region value-beg value-end)))
          (end-of-line)
          (open-line 1)
          (forward-line 1)
          (insert (concat prefix varname ":")))
        (insert (format " %S%s" value suffix))
        )
      )
    )
  )
;;;_   > allout-get-configvar-values (varname)
(defun allout-get-configvar-values (configvar-name)
  "Return a list of values of the symbols in list bound to CONFIGVAR-NAME.

The user is prompted for removal of symbols that are unbound, and they
otherwise are ignored.

CONFIGVAR-NAME should be the name of the configuration variable,
not its value."

  (let ((configvar-value (symbol-value configvar-name))
        got)
    (dolist (sym configvar-value)
      (if (not (boundp sym))
          (if (yes-or-no-p (format-message
			    "%s entry `%s' is unbound -- remove it? "
			    configvar-name sym))
              (delq sym (symbol-value configvar-name)))
        (push (symbol-value sym) got)))
    (reverse got)))
;;;_  : Topics:
;;;_   > allout-mark-topic ()
(defun allout-mark-topic ()
  "Put the region around topic currently containing point."
  (interactive)
  (let ((inhibit-field-text-motion t))
    (beginning-of-line))
  (allout-goto-prefix-doublechecked)
  (push-mark)
  (allout-end-of-current-subtree)
  (exchange-point-and-mark))
;;;_  : UI:
;;;_   > allout-solicit-char-in-string (prompt string &optional do-defaulting)
(defun allout-solicit-char-in-string (prompt string &optional do-defaulting)
  "Solicit (with first arg PROMPT) choice of a character from string STRING.

Optional arg DO-DEFAULTING indicates to accept empty input (CR)."

  (let ((new-prompt prompt)
        got)

    (while (not got)
      (message "%s" new-prompt)

      ;; We do our own reading here, so we can circumvent, eg, special
      ;; treatment for `?' character.  (Oughta use minibuffer keymap instead.)
      (setq got
            (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))

      (setq got
	    (cond ((string-match (regexp-quote got) string) got)
		  ((and do-defaulting (string= got "\r"))
		   ;; Return empty string to default:
		   "")
		  ((string= got "\C-g") (signal 'quit nil))
		  (t
		   (setq new-prompt (concat prompt
					    got
					    " ...pick from: "
					    string
					    ""))
		   nil))))
      ;; got something out of loop -- return it:
      got)
  )
;;;_  : Strings:
;;;_   > allout-regexp-sans-escapes (string)
(defun allout-regexp-sans-escapes (regexp &optional successive-backslashes)
  "Return a copy of REGEXP with all character escapes stripped out.

Representations of actual backslashes -- `\\\\\\\\' -- are left as a
single backslash.

Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."

  (if (string= regexp "")
      ""
    ;; Set successive-backslashes to number if current char is
    ;; backslash, or else to nil:
    (setq successive-backslashes
	  (if (= (aref regexp 0) ?\\)
	      (if successive-backslashes (1+ successive-backslashes) 1)
	    nil))
    (if (or (not successive-backslashes) (= 2 successive-backslashes))
	;; Include first char:
	(concat (substring regexp 0 1)
		(allout-regexp-sans-escapes (substring regexp 1)))
      ;; Exclude first char, but maintain count:
      (allout-regexp-sans-escapes (substring regexp 1) successive-backslashes))))
;;;_   > allout-count-trailing-whitespace-region (beg end)
(defun allout-count-trailing-whitespace-region (beg end)
  "Return number of trailing whitespace chars between BEG and END.

If BEG is bigger than END we return 0."
  (if (> beg end)
      0
    (save-match-data
      (save-excursion
        (goto-char beg)
        (let ((count 0))
          (while (re-search-forward "[ 	][ 	]*$" end t)
            (goto-char (1+ (match-beginning 2)))
            (setq count (1+ count)))
          count)))))
;;;_   > allout-format-quote (string)
(defun allout-format-quote (string)
  "Return a copy of string with all \"%\" characters doubled."
  (apply 'concat
         (mapcar (lambda (char) (if (= char ?%) "%%" (char-to-string char)))
                 string)))
(define-obsolete-function-alias 'allout-flatten #'flatten-tree "27.1")
(define-obsolete-function-alias 'allout-mark-marker #'mark-marker "28.1")
(define-obsolete-function-alias 'allout-substring-no-properties
  #'substring-no-properties "28.1")
(define-obsolete-function-alias 'allout-select-safe-coding-system
  #'select-safe-coding-system "28.1")
(define-obsolete-function-alias 'allout-previous-single-char-property-change
  #'previous-single-char-property-change "28.1")
(define-obsolete-function-alias 'allout-next-single-char-property-change
  #'next-single-char-property-change "28.1")
;;;_ #10 Unfinished
;;;_  > allout-bullet-isearch (&optional bullet)
(defun allout-bullet-isearch (&optional bullet)
  "Isearch (regexp) for topic with bullet BULLET."
  (interactive)
  (if (not bullet)
      (setq bullet (allout-solicit-char-in-string
		    "ISearch for topic with bullet: "
		    (allout-regexp-sans-escapes allout-bullets-string))))

  (let ((isearch-regexp t)
	(isearch-string (concat "^"
				allout-header-prefix
				"[ \t]*"
				bullet)))
    (isearch-repeat 'forward)
    (isearch-mode t)))

;;;_ #11 Unit tests -- this should be last item before "Provide"
;;;_  > allout-run-unit-tests ()
(defun allout-run-unit-tests ()
  "Run the various allout unit tests."
  (message "Running allout tests...")
  (allout-test-resumptions)
  (message "Running allout tests...  Done.")
  (sit-for .5))
;;;_  : test resumptions:
;;;_   > allout-tests-obliterate-variable (name)
(defun allout-tests-obliterate-variable (name)
  "Completely unbind variable with NAME."
  (if (local-variable-p name (current-buffer)) (kill-local-variable name))
  (while (boundp name) (makunbound name)))
;;;_   > allout-test-resumptions ()
(defvar allout-tests-globally-unbound nil
  "Fodder for allout resumptions tests -- defvar just for byte compiler.")
(defvar allout-tests-globally-true nil
  "Fodder for allout resumptions tests -- defvar just for byte compiler.")
(defvar allout-tests-locally-true nil
  "Fodder for allout resumptions tests -- defvar just for byte compiler.")
(defun allout-test-resumptions ()
  ;; FIXME: Use ERT.
  "Exercise allout resumptions."
  ;; for each resumption case, we also test that the right local/global
  ;; scopes are affected during resumption effects:

  ;; ensure that previously unbound variables return to the unbound state.
  (with-temp-buffer
    (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
    (allout-add-resumptions '(allout-tests-globally-unbound t))
    (cl-assert (not (default-boundp 'allout-tests-globally-unbound)))
    (cl-assert (local-variable-p 'allout-tests-globally-unbound (current-buffer)))
    (cl-assert (boundp 'allout-tests-globally-unbound))
    (cl-assert (equal allout-tests-globally-unbound t))
    (allout-do-resumptions)
    (cl-assert (not (local-variable-p 'allout-tests-globally-unbound
                                   (current-buffer))))
    (cl-assert (not (boundp 'allout-tests-globally-unbound))))

  ;; ensure that variable with prior global value is resumed
  (with-temp-buffer
    (allout-tests-obliterate-variable 'allout-tests-globally-true)
    (setq allout-tests-globally-true t)
    (allout-add-resumptions '(allout-tests-globally-true nil))
    (cl-assert (equal (default-value 'allout-tests-globally-true) t))
    (cl-assert (local-variable-p 'allout-tests-globally-true (current-buffer)))
    (cl-assert (equal allout-tests-globally-true nil))
    (allout-do-resumptions)
    (cl-assert (not (local-variable-p 'allout-tests-globally-true
                                   (current-buffer))))
    (cl-assert (boundp 'allout-tests-globally-true))
    (cl-assert (equal allout-tests-globally-true t)))

  ;; ensure that prior local value is resumed
  (with-temp-buffer
    (allout-tests-obliterate-variable 'allout-tests-locally-true)
    (set (make-local-variable 'allout-tests-locally-true) t)
    (cl-assert (not (default-boundp 'allout-tests-locally-true))
            nil (concat "Test setup mistake -- variable supposed to"
                        " not have global binding, but it does."))
    (cl-assert (local-variable-p 'allout-tests-locally-true (current-buffer))
            nil (concat "Test setup mistake -- variable supposed to have"
                        " local binding, but it lacks one."))
    (allout-add-resumptions '(allout-tests-locally-true nil))
    (cl-assert (not (default-boundp 'allout-tests-locally-true)))
    (cl-assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
    (cl-assert (equal allout-tests-locally-true nil))
    (allout-do-resumptions)
    (cl-assert (boundp 'allout-tests-locally-true))
    (cl-assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
    (cl-assert (equal allout-tests-locally-true t))
    (cl-assert (not (default-boundp 'allout-tests-locally-true))))

  ;; ensure that last of multiple resumptions holds, for various scopes.
  (with-temp-buffer
    (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
    (allout-tests-obliterate-variable 'allout-tests-globally-true)
    (setq allout-tests-globally-true t)
    (allout-tests-obliterate-variable 'allout-tests-locally-true)
    (set (make-local-variable 'allout-tests-locally-true) t)
    (allout-add-resumptions '(allout-tests-globally-unbound t)
                            '(allout-tests-globally-true nil)
                            '(allout-tests-locally-true nil))
    (allout-add-resumptions '(allout-tests-globally-unbound 2)
                            '(allout-tests-globally-true 3)
                            '(allout-tests-locally-true 4))
    ;; reestablish many of the basic conditions are maintained after re-add:
    (cl-assert (not (default-boundp 'allout-tests-globally-unbound)))
    (cl-assert (local-variable-p 'allout-tests-globally-unbound (current-buffer)))
    (cl-assert (equal allout-tests-globally-unbound 2))
    (cl-assert (default-boundp 'allout-tests-globally-true))
    (cl-assert (local-variable-p 'allout-tests-globally-true (current-buffer)))
    (cl-assert (equal allout-tests-globally-true 3))
    (cl-assert (not (default-boundp 'allout-tests-locally-true)))
    (cl-assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
    (cl-assert (equal allout-tests-locally-true 4))
    (allout-do-resumptions)
    (cl-assert (not (local-variable-p 'allout-tests-globally-unbound
                                   (current-buffer))))
    (cl-assert (not (boundp 'allout-tests-globally-unbound)))
    (cl-assert (not (local-variable-p 'allout-tests-globally-true
                                   (current-buffer))))
    (cl-assert (boundp 'allout-tests-globally-true))
    (cl-assert (equal allout-tests-globally-true t))
    (cl-assert (boundp 'allout-tests-locally-true))
    (cl-assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
    (cl-assert (equal allout-tests-locally-true t))
    (cl-assert (not (default-boundp 'allout-tests-locally-true))))

  ;; ensure that deliberately unbinding registered variables doesn't foul things
  (with-temp-buffer
    (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
    (allout-tests-obliterate-variable 'allout-tests-globally-true)
    (setq allout-tests-globally-true t)
    (allout-tests-obliterate-variable 'allout-tests-locally-true)
    (set (make-local-variable 'allout-tests-locally-true) t)
    (allout-add-resumptions '(allout-tests-globally-unbound t)
                            '(allout-tests-globally-true nil)
                            '(allout-tests-locally-true nil))
    (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
    (allout-tests-obliterate-variable 'allout-tests-globally-true)
    (allout-tests-obliterate-variable 'allout-tests-locally-true)
    (allout-do-resumptions))
  )
;;;_  % Run unit tests if `allout-run-unit-tests-after-load' is true:
(when allout-run-unit-tests-on-load
  (allout-run-unit-tests))

;;;_ #12 Provide
(provide 'allout)

;;;_* Local emacs vars.
;; The following `allout-layout' local variable setting:
;;  - closes all topics from the first topic to just before the third-to-last,
;;  - shows the children of the third to last (config vars)
;;  - and the second to last (code section),
;;  - and closes the last topic (this local-variables section).
;;Local variables:
;;allout-layout: (0 : -1 -1 0)
;;End:

;;; allout.el ends here

debug log:

solving 05d9153a31 ...
found 05d9153a31 in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).