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
|
;;; bufferlo.el --- Frame/Tab Local Buffer Lists with Persistence -*- lexical-binding: t -*-
;; Copyright (C) 2021-2025 Free Software Foundation, Inc.
;; Author: Florian Rommel <mail@florommel.de>
;; Stephane Marks <shipmints@gmail.com>
;; Maintainer: Florian Rommel <mail@florommel.de>
;; Stephane Marks <shipmints@gmail.com>
;; Url: https://github.com/florommel/bufferlo
;; Created: 2021-09-15
;; Version: 1.2
;; Package-Requires: ((emacs "29.1"))
;; Keywords: buffer frame tabs local
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; With bufferlo, every frame and tab (i.e., `tab-bar-mode' tab) has an
;; additional manageable local buffer list. A buffer is added to the local
;; buffer list when displayed in the frame/tab (e.g., by opening a new file in
;; the tab or by switching to the buffer from the global buffer list).
;; Using Emacs's built-in buffer-list frame parameter, bufferlo integrates
;; seamlessly with all standard frame and tab management facilities, including
;; undeletion of frames and tabs, tab duplication and moving, frame cloning,
;; and session persistence with `desktop' (though bufferlo frame and tab
;; bookmarks offer an alternative persistence method).
;; Bufferlo provides extensive management functions for its local lists and
;; offers features on top of switch-buffer functions, buffer menu, and
;; `ibuffer'. You can configure any command that selects a buffer to use the
;; local buffer list via bufferlo-anywhere-mode.
;; In addition, bufferlo offers lightweight Emacs bookmarks-based persistence
;; for frames, tabs, and sets of frames/tabs to help you manage your transient
;; workflows. Bufferlo bookmarks are compatible with built-in features such
;; as `bookmark-bmenu-list' and third-party packages such as consult which
;; offers consult-bookmark for interactive bookmark selection.
;;; Code:
(require 'seq)
(require 'tab-bar)
(require 'desktop)
(require 'bookmark)
(require 'ibuffer)
(require 'ibuf-ext)
(defgroup bufferlo nil
"Manage frame/tab-local buffer lists."
:group 'convenience)
(defcustom bufferlo-menu-bar-show t
"Show the bufferlo menu on the menu bar."
:package-version '(bufferlo . "1.1")
:type 'boolean)
(defcustom bufferlo-menu-bar-list-buffers 'both
"Show simple and/or `ibuffer' buffer list menu items.
Set to \\='both to show both.
Set to \\='simple to show simple only.
Set to \\='ibuffer to show `ibuffer' only."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Show both simple and `ibuffer'" both)
(const :tag "Show simple only" simple)
(const :tag "Show `ibuffer' only" ibuffer)))
(defcustom bufferlo-prefer-local-buffers t
"Use the frame `buffer-predicate' to prefer local buffers.
Without this option, buffers from across all frames are
presented. This means that a local buffer will be preferred to be
displayed when the current buffer disappears (buried or killed).
This also influences `next-buffer' and `previous-buffer'.
Set to \\='tabs for `next-buffer' and `previous-buffer' to respect
buffers local to the current tab, otherwise they will cycle
through buffers across the frame.
This variable must be set before enabling `bufferlo-mode'."
:package-version '(bufferlo . "0.8")
:type '(radio (const :tag "Prefer local buffers" t)
(const :tag "Prefer local tab buffers" tabs)
(const :tag "Display all buffers" nil)))
(defcustom bufferlo-include-buried-buffers t
"Include buried buffers in the local list (`bufferlo-buffer-list').
Use `bufferlo-bury' to remove and bury a buffer if this is set to t."
:package-version '(bufferlo . "0.8")
:type 'boolean)
(defcustom bufferlo-include-buffer-filters nil
"Buffers that should always get included in a new tab or frame.
This is a list of regular expressions that match buffer names.
This is applied on frame and tab creation. Included buffers can be
explicitly removed later.
This overrides buffers excluded by `bufferlo-exclude-buffer-filters'."
:package-version '(bufferlo . "0.8")
:type '(repeat regexp))
(defcustom bufferlo-exclude-buffer-filters '(".*")
"Buffers that should always get excluded in a new tab or frame.
This is a list of regular expressions that match buffer names.
This is applied on frame and tab creation. Excluded buffers can be
added explicitly later. Use `bufferlo-hidden-buffers' to permanently
hide buffers from the local list.
Buffers included by `bufferlo-include-buffer-filters' take precedence."
:package-version '(bufferlo . "0.8")
:type '(repeat regexp))
(defcustom bufferlo-hidden-buffers nil
"List of regexps matching names of buffers to hide in the local buffer lists.
Matching buffers are hidden even if displayed in the current frame or tab."
:package-version '(bufferlo . "0.8")
:type '(repeat regexp))
(defcustom bufferlo-kill-buffers-exclude-filters
'("\\` " "\\`\\*Messages\\*\\'" "\\`\\*scratch\\*\\'")
"Buffers that should not be killed by `bufferlo-kill-buffers'.
This is a list of regular expressions that match buffer names."
:package-version '(bufferlo . "0.8")
:type '(repeat regexp))
(defcustom bufferlo-kill-buffers-prompt nil
"If non-nil, confirm before killing local or orphan buffers."
:package-version '(bufferlo . "1.1")
:type 'boolean)
(defcustom bufferlo-kill-modified-buffers-policy nil
"Bufferlo behavior when killing modified or process buffers.
This policy applies to all bufferlo functions that entail killing buffers,
e.g., `bufferlo-kill-buffers', `bufferlo-kill-orphan-buffers',
`bufferlo-tab-close-kill-buffers', `bufferlo-delete-frame-kill-buffers'.
This policy is useful if `shell-mode' or `eshell-mode' buffers are
active in a bufferlo-controlled frame or tab.
nil means default Emacs behavior which may prompt. This may have
side effects.
\\='retain-modified means bufferlo will leave modified buffers as
is.
\\='retain-modified-kill-without-file-name will leave modified buffers
as is BUT will kill buffers that have no file name; this includes shell,
hidden, and special buffers that can not normally be saved.
\\='kill-modified instructs bufferlo to kill modified buffers
without remorse including those with running processes such as
`shell-mode' buffers."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Retain modified buffers" retain-modified)
(const :tag "Retain modified buffers BUT kill buffers without file names"
retain-modified-kill-without-file-name)
(const :tag "Kill modified buffers without prompting"
kill-modified)
(const :tag "Default Emacs behavior (will prompt)" nil)))
(defcustom bufferlo-bookmark-inhibit-bookmark-point nil
"If non-nil, inhibit point in bookmarks.
This is useful if `save-place-mode' mode is enabled and you want it to
handle the place restoration in bufferlo bookmarks."
:package-version '(bufferlo . "1.1")
:type 'boolean)
(defcustom bufferlo-bookmark-buffers-exclude-filters nil
"Buffers that should be excluded from bufferlo bookmarks.
This is a list of regular expressions to filter buffer names.
Buffers included by `bufferlo-bookmark-buffers-include-filters' take
precedence."
:package-version '(bufferlo . "1.1")
:type '(repeat regexp))
(defcustom bufferlo-bookmark-buffers-include-filters nil
"Buffers that should be stored in bufferlo bookmarks.
This is a list of regular expressions to filter buffer names.
This overrides buffers excluded by `bufferlo-bookmark-buffers-exclude-filters'."
:package-version '(bufferlo . "1.1")
:type '(repeat regexp))
(defcustom bufferlo-bookmark-frame-load-make-frame nil
"Frame bookmark loading frame and geometry policy.
If nil, reuse the existing frame.
If non-nil, create a new frame to hold a loaded frame bookmark.
If \\='restore-geometry, create a new frame and restore the frame
geometry to that when it was last saved.
If \\='reuse-restore-geometry, reuse the existing frame and restore the
frame geometry to that when it was last saved."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Make a new frame" t)
(const :tag "Make a new frame and restore its geometry"
restore-geometry)
(const :tag "Reuse the current frame, and restore its geometry"
reuse-restore-geometry)
(const :tag "Reuse the current frame" nil)))
(defcustom bufferlo-delete-frame-kill-buffers-prompt nil
"If non-nil, confirm before deleting the frame and killing buffers."
:package-version '(bufferlo . "1.1")
:type 'boolean)
(defcustom bufferlo-close-tab-kill-buffers-prompt nil
"If non-nil, confirm before closing the tab and killing buffers."
:package-version '(bufferlo . "1.1")
:type 'boolean)
(defcustom bufferlo-bookmark-frame-save-on-delete nil
"Control automatically saving the frame bookmark on frame deletion.
nil does not save the frame bookmark when deleting the frame.
t always saves the frame bookmark when deleting the frame.
Prompts for a new bookmark name if the frame is not associated with a bookmark.
\\='when-bookmarked saves the bookmark only if the frame is already associated
with a current bookmark.
\\='on-kill-buffers behaves like t but only for the function
`bufferlo-delete-frame-kill-buffers'.
\\='on-kill-buffers-when-bookmarked behaves like \\='when-bookmarked but only
for `bufferlo-delete-frame-kill-buffers'."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Do not save" nil)
(const :tag "Always save" t)
(const :tag "Only if the frame is already associated with a bookmark"
when-bookmarked)
(const :tag "Only if killing buffers"
on-kill-buffers)
(const :tag "Only if killing buffers and associated with a bookmark"
on-kill-buffers-when-bookmarked)))
(defcustom bufferlo-bookmark-tab-save-on-close nil
"Control automatically saving the tab bookmark on tab deletion.
nil does not save the tab bookmark when closing the tab.
t always saves the tab bookmark when closing the tab.
Prompts for a new bookmark name if the tab is not associated with a bookmark.
\\='when-bookmarked saves the bookmark only if the tab is already associated
with a current bookmark.
\\='on-kill-buffers behaves like t but only for the function
`bufferlo-tab-close-kill-buffers'.
\\='on-kill-buffers-when-bookmarked behaves like \\='when-bookmarked but only
for `bufferlo-tab-close-kill-buffers'."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Do not save" nil)
(const :tag "Always save" t)
(const :tag "Only if the tab is already associated with a bookmark"
when-bookmarked)
(const :tag "Only if killing buffers"
on-kill-buffers)
(const :tag "Only if killing buffers and associated with a bookmark"
on-kill-buffers-when-bookmarked)))
(defcustom bufferlo-bookmark-frame-load-policy 'prompt
"Control loading a frame bookmark into a already-bookmarked frame.
\\='prompt allows you to select a policy interactively.
\\='disallow-replace prevents accidental replacement of
already-bookmarked frames, with the exception that a bookmarked
frame may be reloaded to restore its state.
\\='replace-frame-retain-current-bookmark replaces the frame
content using the existing frame bookmark name.
\\='replace-frame-adopt-loaded-bookmark replaces the frame content
and adopts the loaded bookmark name.
\\='merge adds new frame bookmark tabs to the existing frame,
retaining the existing bookmark name.
This policy is useful when
`bufferlo-bookmark-frame-load-make-frame' is not enabled or frame
loading is not overridden with a prefix argument that suppresses
making a new frame."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Prompt" prompt)
(const :tag "Disallow" disallow-replace)
(const :tag "Replace frame, retain current bookmark name"
replace-frame-retain-current-bookmark)
(const :tag "Replace frame, adopt loaded bookmark name"
replace-frame-adopt-loaded-bookmark)
(const :tag "Merge" merge)))
(defcustom bufferlo-bookmark-frame-duplicate-policy 'prompt
"Control duplicate active frame bookmarks.
These options help you manage and avoid potential confusion from
duplicate active bookmarks.
\\='prompt allows you to select a policy interactively.
\\='allow allows duplicates.
\\='clear silently clears the frame bookmark.
\\='clear-warn issues a warning message about the frame losing
its bookmark.
\\='ignore bypasses the duplicate.
\\='raise will raise the frame with the existing bookmark. Raise does
not apply when restoring bookmark sets.
Note: \\='raise is considered \\='clear during `bookmark-set' loading."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Prompt" prompt)
(const :tag "Allow" allow)
(const :tag "Clear (silently)" clear)
(const :tag "Clear (with message)" clear-warn)
(const :tag "Ignore" ignore)
(const :tag "Raise" raise)))
(defcustom bufferlo-bookmark-frame-persist-frame-name nil
"If non-nil, store frame's name in its bookmark, restore when loading."
:package-version '(bufferlo . "1.2")
:type 'boolean)
(defcustom bufferlo-bookmark-tab-restore-explicit-name t
"If non-nil, restore the tab's explicit name when loading a tab bookmark."
:package-version '(bufferlo . "1.2")
:type 'boolean)
(defcustom bufferlo-bookmarks-load-tabs-make-frame nil
"If non-nil, make a new frame for tabs loaded by `bufferlo-bookmarks-load'.
If nil, tab bookmarks are loaded into the current frame."
:package-version '(bufferlo . "1.1")
:type 'boolean)
(defcustom bufferlo-bookmark-restore-tab-groups nil
"Control the restoration of tab groups in frame and tab bookmarks
nil never restores tab groups.
t always restores tab groups.
\\='tabs only restores tab groups for tab bookmarks.
\\='frames only restores tab groups for frame bookmarks."
:package-version '(bufferlo . "1.2")
:type '(radio (const :tag "Never" nil)
(const :tag "Always" t)
(const :tag "Only for tab bookmarks" tabs)
(const :tag "Only for frame bookmarks" frames)))
(defcustom bufferlo-bookmark-tab-replace-policy 'replace
"Control whether loaded tabs replace current tabs or occupy new tabs.
\\='prompt allows you to select a policy interactively.
\\='replace clears the current tab and overwrites its content
with the loaded tab.
\\='new loads tab bookmarks into new tabs, honoring the user
option `tab-bar-new-tab-to'."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Prompt" prompt)
(const :tag "Replace" replace)
(const :tag "New" new)))
(defcustom bufferlo-bookmark-tab-duplicate-policy 'prompt
"Control duplicate active tab bookmarks.
These options help you manage and avoid potential confusion from
duplicate active bookmarks.
\\='prompt allows you to select a policy interactively.
\\='allow allows duplicates.
\\='clear silently clears the tab bookmark which is natural
reified frame bookmark behavior.
\\='clear-warn issues a warning message about the tab losing its
bookmark.
\\='ignore bypasses the duplicate.
\\='raise raises the first found existing tab bookmark and its frame.
Raise does not apply when restoring bookmark sets or tabs embedded
within a frame bookmark.
Note: \\='raise is considered \\='clear during `bookmark-set' loading."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Prompt" prompt)
(const :tag "Allow" allow)
(const :tag "Clear (silently)" clear)
(const :tag "Clear (with message)" clear-warn)
(const :tag "Ignore" ignore)
(const :tag "Raise" raise)))
(defcustom bufferlo-bookmark-tab-in-bookmarked-frame-policy 'prompt
"Control how a tab bookmark is loaded into an already-bookmarked frame.
This also warns about setting a new frame bookmark on a frame
that has tab bookmarks, and vice versa setting a tab bookmark on
a bookmarked frame.
\\='clear will silently clear the tab bookmark which is natural
reified frame bookmark behavior.
\\='clear-warn issues a warning message about the tab losing its
bookmark.
\\='allow will retain the tab bookmark to enable it to be saved
or updated. Note that the frame bookmark always supersedes the tab
bookmark if the frame bookmark is saved."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Prompt" prompt)
(const :tag "Allow" allow)
(const :tag "Clear (silently)" clear)
(const :tag "Clear (with message)" clear-warn)))
(defcustom bufferlo-bookmark-tab-failed-buffer-policy nil
"Control failed buffer bookmark restore handling.
This controls the handling of buffers in the bookmark's local buffer list
whose individual bookmark could not be restored (e.g., because the file
does not exist anymore) and for buffers that were not bookmarkable at all.
\\='placeholder creates a unique placeholder buffer in place of the
buffer that could not be restored. By default, the placeholder buffer has
a special name. This buffer will not have a file associated with it.
Each bookmark gets its own unique buffer name.
\\='placeholder-orig creates a placeholder buffer with the original
buffer name. This buffer will not have a file associated with it.
If a buffer with the same name already exists, bufferlo does not create a
placeholder buffer but uses this buffer instead.
Use a string to select or create the buffer named by the string; e.g.,
\"*scratch*\".
Use a function that returns a buffer. The function is passed the
original buffer name that failed to load.
nil does not create placeholder buffers for failed bookmarks. However,
similar to the \\='placeholder-orig policy, if a buffer with the same name
exists in the Emacs session, bufferlo will use this buffer.
If all buffers fail to restore and no matching existing buffers are found,
the default buffer shown will be chosen by Emacs."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Placeholder" placeholder)
(const :tag "Placeholder w/failed buffer name" placeholder-orig)
(string :tag "Buffer to select")
(function :tag "Function to call")
(const :tag "Ignore" nil)))
(defcustom bufferlo-bookmarks-save-duplicates-policy 'prompt
"Control duplicates when saving all bookmarks.
\\='prompt allows you to select a policy interactively.
\\='allow will save potentially differing content for the same
bookmark name multiple times with the last-one-saved taking
precedence. A warning message indicates the names of duplicate
bookmarks.
\\='disallow prevents the potentially confusing of overwriting
bookmark content for the same bookmark names. A warning message
indicates the names of duplicate bookmarks.
Note: When using bufferlo's auto-save feature, and to avoid
repeated prompts and warnings, it is best to choose policies in
advance that prevent duplicate frame and tab bookmarks."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Prompt" prompt)
(const :tag "Allow" allow)
(const :tag "Disallow" disallow)))
(defcustom bufferlo-bookmarks-save-frame-policy 'all
"Control bufferlo bookmarks save frame selection behavior.
\\='current saves bookmarks on the current frame only.
\\='other saves bookmarks on non-current frames.
\\='all saves bookmarks across all frames."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Current frame" current)
(const :tag "Other frames" other)
(const :tag "All frames" all)))
(defcustom bufferlo-bookmarks-save-predicate-functions
(list #'bufferlo-bookmarks-save-all-p)
"Functions to filter active bufferlo bookmarks to save.
These are applied when `bufferlo-bookmarks-auto-save-interval' is > 0,
or manually via `bufferlo-bookmarks-save'. Functions are passed the
bufferlo bookmark name and invoked until the first positive result. Set
to `#'bufferlo-bookmarks-save-all-p' to save all bookmarks or provide
your own predicates (note: be sure to remove
`#'bufferlo-bookmarks-save-all-p' from the list)."
:package-version '(bufferlo . "1.1")
:type 'hook)
(defcustom bufferlo-bookmarks-load-predicate-functions nil
"Functions to filter stored bufferlo bookmarks to load.
These are applied in `bufferlo-bookmarks-load' which might also
be invoked at Emacs startup time using `window-setup-hook'.
Functions are passed the bufferlo bookmark name and invoked until
the first positive result. Set to
`#'bufferlo-bookmarks-load-all-p' to load all bookmarks or
provide your own predicates."
:package-version '(bufferlo . "1.1")
:type 'hook)
(defcustom bufferlo-bookmarks-save-at-emacs-exit 'nosave
"Bufferlo can save active bookmarks at Emacs exit.
\\='nosave does not save any active bookmarks.
\\='all saves all active bufferlo bookmarks.
\\='pred honors the filter predicates
in `bufferlo-bookmarks-save-predicate-functions'."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Do not save at exit" nosave)
(const :tag "Predicate-filtered bookmarks" pred)
(const :tag "All bookmarks" all)))
(defcustom bufferlo-bookmarks-load-at-emacs-startup 'noload
"Bufferlo can load stored bookmarks at Emacs startup.
\\='noload does not load any stored bookmarks.
\\='all loads all stored bufferlo bookmarks.
\\='pred honors the filter predicates in
`bufferlo-bookmarks-load-predicate-functions'.
Note that `bufferlo-mode' must be enabled before
`window-setup-hook' is invoked for this policy to take effect."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Do not load at startup" noload)
(const :tag "Predicate-filtered bookmarks" pred)
(const :tag "All bookmarks" all)))
(defcustom bufferlo-bookmarks-load-at-emacs-startup-tabs-make-frame nil
"If nil, the initial frame is reused for restored tabs.
If non-nil, a new frame is created for restored tabs."
:package-version '(bufferlo . "1.1")
:type 'boolean)
(defcustom bufferlo-ibuffer-bind-local-buffer-filter t
"If non-nil, bind the local buffer filter and the orphan filter in ibuffer.
The local buffer filter is bound to \"/ l\" and the orphan filter to \"/ L\"."
:package-version '(bufferlo . "0.8")
:type 'boolean)
(defcustom bufferlo-ibuffer-bind-keys t
"If non-nil, bind ibuffer convenience keys for bufferlo functions."
:package-version '(bufferlo . "1.1")
:type 'boolean)
(defcustom bufferlo-local-scratch-buffer-name "*local scratch*"
"Base name of the local scratch buffer.
Multiple frame/tabs will use `generate-new-buffer-name' (which
appends \"<N>\" to the name) in order to get a unique buffer.
Local scratch buffers are optional and not used by default.
Use the following functions to create and work with them:
`bufferlo-create-local-scratch-buffer',
`bufferlo-get-local-scratch-buffer',
`bufferlo-switch-to-local-scratch-buffer',
and `bufferlo-toggle-local-scratch-buffer'.
For example, create a dedicated local scratch buffer for all tabs and frames:
(setq \\='tab-bar-new-tab-choice #\\='bufferlo-create-local-scratch-buffer)
(add-hook \\='after-make-frame-functions
#\\='bufferlo-switch-to-local-scratch-buffer)
You can set this to \"*scratch*\"."
:package-version '(bufferlo . "0.8")
:type 'string)
(defcustom bufferlo-local-scratch-buffer-initial-major-mode nil
"The initial major mode for local scratch buffers.
If nil, the local scratch buffers' major mode is set to `initial-major-mode'."
:package-version '(bufferlo . "0.8")
:type 'function)
(defcustom bufferlo-anywhere-filter '(switch-to-buffer
bufferlo-switch-to-buffer
bufferlo-find-buffer
bufferlo-find-buffer-switch)
"The functions that use the local buffer list in `bufferlo-anywhere-mode'.
If `bufferlo-anywhere-filter-type' is set to `exclude', this is an exclude
filter (i.e., determines the functions that do not use the local buffer list).
If `bufferlo-anywhere-filter-type' is set to `include' (or any other value),
this is an include filter.
The value can either be a list of functions, or t (for all functions),
or a custom filter function that takes a function symbol as its argument and
returns whether the probed function should be filtered (non-nil) or
not-filtered (nil)."
:package-version '(bufferlo . "0.8")
:type '(choice (repeat :tag "Filter specific functions" function)
(const :tag "All functions" t)
(function :tag "Custom filter function")))
(defcustom bufferlo-anywhere-filter-type 'exclude
"Determines whether `bufferlo-anywhere-filter' is an include or exclude filter.
Set this to `include' or `exclude'."
:package-version '(bufferlo . "0.8")
:type '(radio (const :tag "Include filter" include)
(const :tag "Exclude filter" exclude)))
(defcustom bufferlo-bookmark-map-functions nil
"Functions to call for every local buffer when making a tab bookmark.
Each function takes a bookmark record as its argument. The corresponding
buffer is set as current buffer. Every function should return a valid
bookmark record or nil. The first function gets the buffer's default
bookmark record or nil if it is not bookmarkable. Subsequent functions
receive the bookmark record that the previous function returned as their
argument. The bookmark record of the last function is used as the
effective record. If the last function returns nil, no record for the
respective buffer is included in the frame or tab bookmark.
These functions are also called when creating a frame bookmark, since a
frame bookmark is a collection of tab bookmarks."
:package-version '(bufferlo . "0.8")
:type 'hook)
(defcustom bufferlo-bookmark-tab-handler-functions nil
"Abnormal hooks to call after a bookmark tab is handled.
Each function takes the following arguments:
bookmark-name: source bookmark name
effective-bookmark-name: nil, if tab bookmark cleared
tab: the handled tab
restored-buffer-names: list of restored buffer names
skipped-buffer-names: list of skipped buffer names"
:package-version '(bufferlo . "1.1")
:type 'hook)
(defcustom bufferlo-bookmark-frame-handler-functions nil
"Abnormal hooks to call after a bookmark frame is handled.
Each function takes the following arguments:
bookmark-name: source bookmark name
effective-bookmark-name: nil, if frame bookmark cleared
new-frame-p: t if this is a new frame, nil if a reused frame
frame: the handled frame"
:package-version '(bufferlo . "1.1")
:type 'hook)
(defcustom bufferlo-bookmark-set-handler-functions nil
"Abnormal hooks to call after a bookmark set is handled.
Each function takes the following arguments:
bookmark-name: source bookmark name
set-bookmark-names: bookmark names handled"
:package-version '(bufferlo . "1.1")
:type 'hook)
(defvar bufferlo--desktop-advice-active nil)
(defvar bufferlo--desktop-advice-active-force nil)
(defvar bufferlo--clear-buffer-lists-active nil)
(defvar bufferlo--bookmarks-auto-save-timer nil
"Timer to save bufferlo bookmarks.
This is controlled by `bufferlo-bookmarks-auto-save-interval'.")
(defun bufferlo--bookmarks-auto-save-timer-maybe-cancel ()
"Cancel and clear the bufferlo bookmark auto-save timer, if set."
(when (timerp bufferlo--bookmarks-auto-save-timer)
(cancel-timer bufferlo--bookmarks-auto-save-timer))
(setq bufferlo--bookmarks-auto-save-timer nil))
(defvar bufferlo-bookmarks-auto-save-interval) ; byte compiler
(defun bufferlo--bookmarks-auto-save-timer-maybe-start ()
"Start the bufferlo auto-save bookmarks timer, if needed."
(bufferlo--bookmarks-auto-save-timer-maybe-cancel)
(when (and (integerp bufferlo-bookmarks-auto-save-interval)
(> bufferlo-bookmarks-auto-save-interval 0))
(setq bufferlo--bookmarks-auto-save-timer
(run-with-timer
bufferlo-bookmarks-auto-save-interval
nil ; We reschedule in `bufferlo-bookmarks-save'.
#'bufferlo-bookmarks-save))))
(defcustom bufferlo-bookmarks-auto-save-interval 0
"Save bufferlo bookmarks every interval of this many seconds.
Set to 0 to disable the timer. Units are whole integer seconds."
:package-version '(bufferlo . "1.1")
:type 'natnum
:set (lambda (sym val)
(set-default sym val)
(bufferlo--bookmarks-auto-save-timer-maybe-start)))
(defcustom bufferlo-bookmarks-auto-save-messages nil
"Control messages from the interval auto saver.
\\=nil inhibits all messages.
\\=t shows all messages.
\\='saved shows a message only when bookmarks have been saved.
\\='notsaved shows a message only when bookmarks have not been saved."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "None" nil)
(const :tag "All" t)
(const :tag "Saved only" saved)
(const :tag "Not-saved only" notsaved)))
(defcustom bufferlo-set-restore-geometry-policy 'all
"Bufferlo frame restoration geometry policy.
This affects frame bookmarks inside a bookmark set.
\\='all restores both frame and tab bookmark frame geometries.
\\='frames restores only frame bookmark geometry.
\\='tab-frame restores only tab bookmark logical frame geometry."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "All" all)
(const :tag "Frames" frames)
(const :tag "Tabs" tab-frames)))
(defcustom bufferlo-set-restore-tabs-reuse-init-frame nil
"Restore first tabs from a bookmark set's frame to the current frame.
This affects the first frame of tab bookmarks from a bookmark set.
Subsequent frames of tab bookmarks are restored to their own frames."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Reuse" reuse)
(const :tag "Reuse & reset geometry" reuse-reset-geometry)
(const :tag "New frame" nil)))
(defcustom bufferlo-set-restore-ignore-already-active nil
"Ignore already active bookmarks when restoring a bookmark set.
\\='prompt offers to bulk ignore loading active bookmarks
\\='ignore always ignores loading active bookmarks
\\='nil means default to potential duplicate bookmark policies"
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Prompt" prompt)
(const :tag "Ignore" ignore)
(const :tag "Default duplicate policies" nil)))
(defcustom bufferlo-frameset-restore-geometry 'bufferlo
"Frameset restore geometry handling control.
\\='native uses Emacs built-in geometry handling.
\\='bufferlo uses bufferlo's geometry handling.
Set to nil to ignore geometry handling."
:package-version '(bufferlo . "1.1")
:type '(radio (const :tag "Emacs" native)
(const :tag "Bufferlo" bufferlo)
(const :tag "Ignore" nil)))
(defcustom bufferlo-frameset-save-filter nil
"Extra Emacs frame parameters to filter before saving a `frameset'.
Use this if you define custom frame parameters, or you use
packages that do, and you want to avoid storing such parameters
in bufferlo framesets."
:package-version '(bufferlo . "1.1")
:type '(repeat symbol))
(defcustom bufferlo-frameset-restore-filter nil
"Extra Emacs frame parameters to filter before restoring a `frameset'.
Use this if you define custom frame parameters, or you use
packages that do, and you want to ensure they are filtered in
advance of restoring bufferlo framesets."
:package-version '(bufferlo . "1.1")
:type '(repeat symbol))
(defcustom bufferlo-frameset-restore-function
#'bufferlo-frameset-restore-default
"Function to restore a frameset, which see `frameset-restore'.
It defaults to `bufferlo-frameset-restore-default'.
The function accepts a single parameter, the `frameset' to restore."
:package-version '(bufferlo . "1.1")
:type 'function)
(defcustom bufferlo-frameset-restore-parameters-function
#'bufferlo-frameset-restore-parameters-default
"Function to create parameters for `frameset-restore', which see.
The function should create a plist of the form:
(list :reuse-frames value
:force-display value
:force-onscreen value
:cleanup-frames value)
where each property is as documented by `frameset-restore'.
It defaults to `bufferlo-frameset-restore-parameters-default'."
:package-version '(bufferlo . "1.1")
:type 'function)
(defcustom bufferlo-frame-geometry-function
#'bufferlo-frame-geometry-default
"Function to produce a bufferlo-frame-geometry alist.
It defaults to `bufferlo-frame-geometry-default'.
The function takes one parameter, FRAME, for which geometry is to
be ascertained. See `bufferlo-frame-geometry-default' for
the returned alist form.
Replace this function with your own if the default produces
suboptimal results for your platform."
:package-version '(bufferlo . "1.1")
:type 'function)
(defcustom bufferlo-set-frame-geometry-function
#'bufferlo-set-frame-geometry-default
"Function to set frame geometry based on bufferlo-frame-geometry alist.
It defaults to `bufferlo-set-frame-geometry-default', which see for
parameters.
Replace this function with your own if the default produces
suboptimal results for your platform."
:package-version '(bufferlo . "1.1")
:type 'function)
(defcustom bufferlo-frame-sleep-for 0
"Window manager catch-up delay for changing frame parameters.
Delay is specified in seconds using `sleep-for', which see.
GTK/GNOME seems to need 0.3 seconds. YMMV.
No delay seems needed on macOS."
:package-version '(bufferlo . "1.1")
:type 'number)
(defcustom bufferlo-mode-line-prefix "Bfl"
"Bufferlo mode-line prefix."
:package-version '(bufferlo . "1.1")
:type 'string)
(defcustom bufferlo-mode-line-left-prefix "["
"Bufferlo mode-line left-hand prefix for an active tab/frame/set bookmark."
:package-version '(bufferlo . "1.1")
:type 'string)
(defcustom bufferlo-mode-line-right-suffix "]"
"Bufferlo mode-line right-hand prefix for an active tab/frame/set bookmark."
:package-version '(bufferlo . "1.1")
:type 'string)
(defcustom bufferlo-mode-line-frame-prefix "F:"
"Bufferlo mode-line frame prefix."
:package-version '(bufferlo . "1.1")
:type 'string)
(defcustom bufferlo-mode-line-tab-prefix "T:"
"Bufferlo mode-line tab prefix."
:package-version '(bufferlo . "1.1")
:type 'string)
(defcustom bufferlo-mode-line-set-active-prefix "S"
"Bufferlo mode-line frame prefix."
:package-version '(bufferlo . "1.1")
:type 'string)
(defcustom bufferlo-mode-line-delimiter "|"
"Delimiter between multiple bookmarks."
:package-version '(bufferlo . "1.1")
:type 'string)
(defvar bufferlo-mode) ; byte compiler
(defvar bufferlo-mode-line-menu) ; byte compiler
(defun bufferlo--mode-line-format-helper (abm str face)
"Bufferlo mode-line helper to add face and mouse features.
Where ABM is the current active bookmark, STR is the mode-line
string, FACE is the face for STR."
(propertize
str 'face face
'mouse-face 'mode-line-highlight
'help-echo (lambda (&rest _)
(concat
(unless (equal abm "")
(format "Active bufferlo bookmark: %s\n" abm))
"mouse-1: Display minor mode menu\n"
"mouse-2: Show help for minor mode"))
'keymap (let ((map (make-sparse-keymap)))
(define-key map [mode-line down-mouse-1]
bufferlo-mode-line-menu)
(define-key map [mode-line down-mouse-3]
bufferlo-mode-line-menu)
(define-key map [mode-line mouse-2]
(lambda ()
(interactive)
(describe-function 'bufferlo-mode)))
map)))
(defvar bufferlo--active-sets) ; byte compiler
(defun bufferlo--active-set-name-for-bookmark (bookmark-name)
"Return the active set name that BOOKMARK-NAME belongs to, or nil."
(unless (null bookmark-name)
(catch :set-name
(dolist (set bufferlo--active-sets)
(when (member bookmark-name
(alist-get 'bufferlo-bookmark-names (cdr set)))
(throw :set-name (car set)))))))
(defun bufferlo-mode-line-format ()
"Bufferlo mode-line format to display the current active frame or tab bookmark."
(when bufferlo-mode
(let* ((fbm (frame-parameter nil 'bufferlo-bookmark-frame-name))
(tbm (alist-get 'bufferlo-bookmark-tab-name
(tab-bar--current-tab-find
(frame-parameter nil 'tabs))))
(set-active (> (length bufferlo--active-sets) 0))
(cache (window-parameter nil 'bufferlo--mode-line-cache)))
(if (equal (cdr cache) (list fbm tbm set-active))
(car cache)
(let* ((abm (concat (when fbm (format "%s (Frame)" fbm))
(when (and fbm tbm) ", ")
(when tbm (format "%s (Tab)" tbm))))
(str (concat
(bufferlo--mode-line-format-helper
abm bufferlo-mode-line-prefix 'bufferlo-mode-line-face)
(when (and bufferlo-mode-line-left-prefix
(or set-active fbm tbm))
(bufferlo--mode-line-format-helper
abm
bufferlo-mode-line-left-prefix
'bufferlo-mode-line-face))
(when set-active
(bufferlo--mode-line-format-helper
abm
(concat bufferlo-mode-line-set-active-prefix
(bufferlo--active-set-name-for-bookmark fbm)
(bufferlo--active-set-name-for-bookmark tbm)
(when (or fbm tbm) bufferlo-mode-line-delimiter))
'bufferlo-mode-line-set-face))
(when fbm
(bufferlo--mode-line-format-helper
abm
(concat bufferlo-mode-line-frame-prefix
fbm
(when tbm bufferlo-mode-line-delimiter))
'bufferlo-mode-line-frame-bookmark-face))
(when tbm
(bufferlo--mode-line-format-helper
abm
(concat bufferlo-mode-line-tab-prefix tbm)
'bufferlo-mode-line-tab-bookmark-face))
(when (and bufferlo-mode-line-right-suffix
(or set-active fbm tbm))
(bufferlo--mode-line-format-helper
abm
bufferlo-mode-line-right-suffix
'bufferlo-mode-line-face))))
(str (if (string-empty-p str) "" (concat " " str))))
(set-window-parameter nil 'bufferlo--mode-line-cache
(list str fbm tbm set-active))
str)))))
(defvar bufferlo-mode-line)
(defun bufferlo--reset-mode-line ()
"Remove the current bufferlo mode line entry."
(setq mode-line-misc-info (delq (assq 'bufferlo-mode mode-line-misc-info)
mode-line-misc-info)))
(defun bufferlo--set-mode-line ()
"Set the current bufferlo mode line entry."
(setq mode-line-misc-info (cons (list 'bufferlo-mode bufferlo-mode-line)
mode-line-misc-info)))
(defcustom bufferlo-mode-line '(:eval (bufferlo-mode-line-format))
"Bufferlo mode line definition."
:package-version '(bufferlo . "1.1")
:type 'sexp
:set (lambda (variable value)
(bufferlo--reset-mode-line) ; do before we overwrite the value
(set-default variable value)
(bufferlo--set-mode-line))
:initialize #'custom-initialize-default
:risky t)
(defgroup bufferlo-faces nil
"Faces used in `bufferlo-mode'."
:package-version '(bufferlo . "1.1")
:group 'bufferlo
:group 'faces)
(defface bufferlo-mode-line-face nil
"`bufferlo-mode' mode-line base face."
:package-version '(bufferlo . "1.1"))
(defface bufferlo-mode-line-frame-bookmark-face
'((t :inherit bufferlo-mode-line-face))
"`bufferlo-mode' mode-line frame bookmark indicator face."
:package-version '(bufferlo . "1.1"))
(defface bufferlo-mode-line-tab-bookmark-face
'((t :inherit bufferlo-mode-line-face))
"`bufferlo-mode' mode-line tab bookmark indicator face."
:package-version '(bufferlo . "1.1"))
(defface bufferlo-mode-line-set-face
'((t :inherit bufferlo-mode-line-face))
"`bufferlo-mode' mode-line bookmark-set active indicator face."
:package-version '(bufferlo . "1.1"))
(defconst bufferlo--command-line-noload-prefix "--bufferlo-noload")
(defvar bufferlo--command-line-noload nil)
(defun bufferlo--parse-command-line ()
"Process bufferlo Emacs command-line arguments."
(when-let* ((pos (seq-position command-line-args
bufferlo--command-line-noload-prefix
#'string-equal)))
(setq bufferlo--command-line-noload pos)
(setq command-line-args (seq-remove-at-position command-line-args pos)))
(when (file-exists-p (locate-user-emacs-file "bufferlo-noload"))
(message "bufferlo-noload file found; inhibiting bufferlo bookmark loading")
(setq bufferlo--command-line-noload t)))
(defun -bufferlo--parse-command-line-test ()
"Internal test function for command-line processing."
(let ((command-line-args (list "/usr/bin/emacs" "--name" "foobar"
bufferlo--command-line-noload-prefix
"-T" "title")))
(setq bufferlo--command-line-noload nil)
(message "command-line-args=%s" command-line-args)
(message "bufferlo--command-line-noload=%s" bufferlo--command-line-noload)
(bufferlo--parse-command-line)
(message "bufferlo--command-line-noload=%s" bufferlo--command-line-noload)
(message "command-line-args=%s" command-line-args)))
(defvar bufferlo-mode-map (make-sparse-keymap)
"`bufferlo-mode' keymap.")
(defvar bufferlo--bookmark-handlers
(list
#'bufferlo--bookmark-tab-handler
#'bufferlo--bookmark-frame-handler
#'bufferlo--bookmark-set-handler)
"Bufferlo bookmark handlers.")
(defconst bufferlo--bookmark-type-names
'((tbm . "B-Tab")
(fbm . "B-Frame")
(sbm . "B-Set")))
;;;###autoload
(define-minor-mode bufferlo-mode
"Manage frame/tab-local buffers."
:global t
:require 'bufferlo
:init-value nil
:lighter nil
:keymap bufferlo-mode-map
(bufferlo--reset-mode-line)
(if bufferlo-mode
(progn
(unless after-init-time
(bufferlo--parse-command-line)) ; parse user-provided settings first
;; Prefer local buffers
(when bufferlo-prefer-local-buffers
(dolist (frame (frame-list))
(bufferlo--set-buffer-predicate frame))
(add-hook 'after-make-frame-functions
#'bufferlo--set-buffer-predicate))
(when (eq bufferlo-prefer-local-buffers 'tabs)
(bufferlo--set-switch-to-prev-buffer-skip))
;; Include/exclude buffers. See the
;; `bufferlo--tab-include-exclude-buffers-inhibit' comments for the
;; hook strategy employed here.
(add-hook 'tab-bar-tab-post-open-functions
#'bufferlo--tab-include-exclude-buffers)
(add-hook 'before-make-frame-hook
#'bufferlo--tab-include-exclude-buffers-inhibit)
(add-hook 'after-make-frame-functions
#'bufferlo--include-exclude-buffers)
;; Save/restore local buffer list
(advice-add #'window-state-get :around #'bufferlo--window-state-get)
(advice-add #'window-state-put :after #'bufferlo--window-state-put)
;; Desktop support
(advice-add #'frameset--restore-frame :around #'bufferlo--activate)
;; Duplicate/move tabs
(advice-add #'tab-bar-select-tab :around #'bufferlo--activate-force)
;; Clone & undelete frame
(advice-add #'clone-frame :around
#'bufferlo--clone-undelete-frame-advice)
(advice-add #'undelete-frame :around
#'bufferlo--clone-undelete-frame-advice)
;; Undo close tab duplicate check
(if (< emacs-major-version 31)
(advice-add #'tab-bar-undo-close-tab
:around #'bufferlo--tab-bar-undo-close-tab-advice)
(add-hook 'tab-bar-post-undo-close-tab-functions
#'bufferlo--tab-post-undo-close-tab-function))
;; Switch-tab workaround
(when (< emacs-major-version 31)
(advice-add #'tab-bar-select-tab
:around #'bufferlo--clear-buffer-lists-activate)
(advice-add #'tab-bar--tab
:after #'bufferlo--clear-buffer-lists))
;; Set up bookmarks save timer
(bufferlo--bookmarks-auto-save-timer-maybe-start)
;; kill-emacs-hook save bookmarks option
(when (not (eq bufferlo-bookmarks-save-at-emacs-exit 'nosave))
(add-hook 'kill-emacs-hook #'bufferlo--bookmarks-save-at-emacs-exit))
;; load bookmarks at startup option
(unless after-init-time
(when (and (not bufferlo--command-line-noload)
(not (eq bufferlo-bookmarks-load-at-emacs-startup 'noload)))
(add-hook 'window-setup-hook #'bufferlo--bookmarks-load-startup)))
;; Save bookmark on close-tab and delete-frame
(add-hook 'tab-bar-tab-pre-close-functions
#'bufferlo-bookmark--tab-save-on-close)
(add-hook 'delete-frame-functions
#'bufferlo-bookmark--frame-save-on-delete)
;; bookmark hook and advice
(add-hook 'bookmark-after-load-file-hook
#'bufferlo--bookmark-after-load-file-hook)
(advice-add #'bookmark--jump-via :around #'bufferlo--bookmark--jump-via-advice)
(advice-add #'bookmark-rename :around #'bufferlo--bookmark-rename-advice)
(advice-add #'bookmark-delete :around #'bufferlo--bookmark-delete-advice)
;; mode line
(bufferlo--set-mode-line))
;; Prefer local buffers
(dolist (frame (frame-list))
(bufferlo--reset-buffer-predicate frame))
(when (eq bufferlo-prefer-local-buffers 'tabs)
(bufferlo--reset-switch-to-prev-buffer-skip))
(remove-hook 'after-make-frame-functions #'bufferlo--set-buffer-predicate)
;; Include/exclude buffers
(remove-hook 'tab-bar-tab-post-open-functions
#'bufferlo--tab-include-exclude-buffers)
(remove-hook 'before-make-frame-hook
#'bufferlo--tab-include-exclude-buffers-inhibit)
(remove-hook 'after-make-frame-functions
#'bufferlo--include-exclude-buffers)
;; Save/restore local buffer list
(advice-remove #'window-state-get #'bufferlo--window-state-get)
(advice-remove #'window-state-put #'bufferlo--window-state-put)
;; Desktop support
(advice-remove #'frameset--restore-frame #'bufferlo--activate)
;; Duplicate/move tabs
(advice-remove #'tab-bar-select-tab #'bufferlo--activate-force)
;; Clone & undelete frame
(advice-remove #'clone-frame #'bufferlo--clone-undelete-frame-advice)
(advice-remove #'undelete-frame #'bufferlo--clone-undelete-frame-advice)
;; Undo close tab duplicate check
(if (< emacs-major-version 31)
(advice-remove #'tab-bar-undo-close-tab
#'bufferlo--tab-bar-undo-close-tab-advice)
(remove-hook 'tab-bar-post-undo-close-tab-functions
#'bufferlo--tab-post-undo-close-tab-function))
;; Switch-tab workaround
(when (< emacs-major-version 31)
(advice-remove #'tab-bar-select-tab #'bufferlo--clear-buffer-lists-activate)
(advice-remove #'tab-bar--tab #'bufferlo--clear-buffer-lists))
;; Cancel bookmarks save timer
(bufferlo--bookmarks-auto-save-timer-maybe-cancel)
;; kill-emacs-hook save bookmarks option
(remove-hook 'kill-emacs-hook #'bufferlo--bookmarks-save-at-emacs-exit)
;; load bookmarks at startup option
(remove-hook 'window-setup-hook #'bufferlo-bookmarks-load)
;; Save bookmark on close-tab and delete-frame
(remove-hook 'tab-bar-tab-pre-close-functions
#'bufferlo-bookmark--tab-save-on-close)
(remove-hook 'delete-frame-functions
#'bufferlo-bookmark--frame-save-on-delete)
;; bookmark hook and advice
(remove-hook 'bookmark-after-load-file-hook
#'bufferlo--bookmark-after-load-file-hook)
(advice-remove #'bookmark--jump-via #'bufferlo--bookmark--jump-via-advice)
(advice-remove #'bookmark-rename #'bufferlo--bookmark-rename-advice)
(advice-remove #'bookmark-delete #'bufferlo--bookmark-delete-advice)))
(defun bufferlo--current-bookmark-name ()
"Current bufferlo bookmark name, where frame beats tab."
(if-let* ((fbm (frame-parameter nil 'bufferlo-bookmark-frame-name)))
fbm
(if-let* ((tbm (alist-get 'bufferlo-bookmark-tab-name
(cdr (bufferlo--current-tab)))))
tbm
nil)))
(defvar bufferlo-menu-item-raise
'("Raise"
:help "Raise an open bufferlo bookmark"
:active (> (length (bufferlo--active-bookmarks)) 0)
:filter (lambda (&optional _)
(let* ((abms (bufferlo--active-bookmarks))
(abm-names (mapcar #'car abms))
(current-bm-name (bufferlo--current-bookmark-name)))
(mapcar (lambda (abm-name)
(vector abm-name
`(bufferlo--bookmark-raise-by-name ,abm-name)
:style 'radio
:selected (equal abm-name current-bm-name)))
abm-names)))))
(easy-menu-define bufferlo-mode-menu bufferlo-mode-map
"`bufferlo-mode' menu."
`("Bufferlo" :visible (and bufferlo-mode bufferlo-menu-bar-show)
["Buffer Management" :active nil]
["Local Buffers..." bufferlo-list-buffers :help "Display a list of local buffers" :visible (memq bufferlo-menu-bar-list-buffers '(both simple))]
["Orphan Buffers..." bufferlo-list-orphan-buffers :help "Display a list of orphan buffers" :visible (memq bufferlo-menu-bar-list-buffers '(both simple))]
["Local Buffers (ibuffer)..." bufferlo-ibuffer :help "Invoke `ibuffer' filtered for local buffers" :visible (memq bufferlo-menu-bar-list-buffers '(both ibuffer))]
["Orphan Buffers (ibuffer)..." bufferlo-ibuffer-orphans :help "Invoke `ibuffer' filtered for orphan buffers" :visible (memq bufferlo-menu-bar-list-buffers '(both ibuffer))]
["Clear Buffer Locals" bufferlo-clear :help "Clear the frame/tab's buffer list, except for the current buffer"]
["Remove Buffer from Locals..." bufferlo-remove :help "Remove buffer from the frame/tab's buffer list"]
["Remove Non Exclusives" bufferlo-remove-non-exclusive-buffers :help "Remove all buffers from the local buffer list that are not exclusive to it"]
["Bury and Remove from Locals" bufferlo-bury :help "Bury and remove the buffer specified by BUFFER-OR-NAME from the local list"]
["Kill Local Buffers..." bufferlo-kill-buffers :help "Kill the buffers of the frame/tab-local buffer list"]
["Kill Orphan Buffers..." bufferlo-kill-orphan-buffers :help "Kill all buffers that are not in any local list of a frame or tab"]
("Find/Switch"
["Find..." bufferlo-find-buffer :help "Switch to the frame/tab containing buffer in its local list"]
["Find & Switch..." bufferlo-find-buffer-switch :help "Switch to the frame/tab containing buffer and select the buffer"]
["Display..." bufferlo-switch-to-buffer :help "Display the selected buffer in the selected window"]
)
("*scratch*"
["*scratch*..." bufferlo-switch-to-scratch-buffer :help "Switch to the scratch buffer"]
["Local *scratch*..." bufferlo-switch-to-local-scratch-buffer :help "Switch to the local scratch buffer"]
["Toggle *scratch*..." bufferlo-toggle-local-scratch-buffer :help "Switch to the local scratch buffer or bury it if it is already selected"]
)
["Isolate Project" bufferlo-isolate-project :help "Isolate a project in the frame or tab" :active (project-current)]
"--"
["Bookmarks" :active nil]
["Load..." bufferlo-bms-load :help "Load specified bookmarks"]
["Save..." bufferlo-bms-save :help "Save specified bookmarks"]
["Close/Kill..." bufferlo-bms-close :help "Close/kill specified bookmarks"]
["Save Current" bufferlo-bm-save :help "Save the current tab bookmark"]
["Reload Current" bufferlo-bm-load :help "Reload a tab bookmark replacing existing state"]
["Close/Kill Current..." bufferlo-bm-close :help "Close the current tab bookmark and kill its buffers"]
["Raise..." bufferlo-bookmark-raise :help "Raise an active bufferlo bookmark"]
,bufferlo-menu-item-raise ; sub-menu of actives to select that also lives in the mode line
["Clear Actives..." bufferlo-clear-active-bookmarks :help "Clear active bookmarks"]
["Clear Active (if duped)" bufferlo-maybe-clear-active-bookmark :help "Clear active bookmark if already in use elsewhere"]
"--"
;; ["Tab Bookmarks" :active nil]
("Tab Bookmarks"
["Create..." bufferlo-bm-tab-save :help "Create a new tab bookmark"]
["Load..." bufferlo-bm-tab-load :help "Load a tab bookmark"]
["Save Current" bufferlo-bm-tab-save-curr :help "Save the current tab bookmark"]
["Reload Current" bufferlo-bm-tab-load-curr :help "Reload a tab bookmark replacing existing state"]
["Close/Kill Current" bufferlo-bm-tab-close-curr :help "Close the current tab bookmark and kill its buffers"]
)
;; "--"
;; ["Frame Bookmarks" :active nil]
("Frame Bookmarks"
["Create..." bufferlo-bm-frame-save :help "Create a new frame bookmark"]
["Load..." bufferlo-bm-frame-load :help "Load a frame bookmark"]
["Merge..." bufferlo-bm-frame-load-merge :help "Merge a frame bookmark tabs into the current frame"]
["Save Current" bufferlo-bm-frame-save-curr :help "Save the current frame bookmark"]
["Reload Current" bufferlo-bm-frame-load-curr :help "Reload a frame bookmark replacing existing state"]
["Close/Kill Current" bufferlo-bm-frame-close-curr :help "Close the current frame bookmark and kill its buffers"]
)
;; "--"
;; ["Bookmark Sets" :active nil]
("Bookmark Sets"
["Create..." bufferlo-set-save :help "Create a new bookmark set"]
["Load..." bufferlo-set-load :help "Load a bookmark set"]
["Save Current..." bufferlo-set-save-curr :help "Save the specified bookmark sets"]
["Add..." bufferlo-set-add :help "Add bookmark(s) to a set"]
["Remove..." bufferlo-set-remove :help "Remove bookmark(s) from a set"]
["Close/Kill..." bufferlo-set-close :help "Close the specified bookmark sets (kills frames, tabs, buffers)"]
["Clear..." bufferlo-set-clear :help "Clear the specified bookmark set (does not kill frames, tabs, buffers)"]
["List..." bufferlo-set-list :help "List the bookmarks in specified active bookmark sets"]
)
;; "--"
;; ["Bookmark Management" :active nil]
("Bookmark Management"
["Emacs Bookmarks..." bookmark-bmenu-list :help "Emacs bookmarks"]
["Rename Bookmark..." (lambda ()
(interactive)
(let ((last-nonmenu-event "")) ; (listp nil) returns t so we hack it to be nil
(call-interactively #'bookmark-rename)))
:help "Rename a bookmark"]
["Delete Bookmark..." (lambda ()
(interactive)
(let ((last-nonmenu-event "")) ; (listp nil) returns t so we hack it to be nil
(call-interactively #'bookmark-delete)))
:help "Delete a bookmark"]
)
"--"
;; customize
["Customize Bufferlo" (lambda () (interactive) (customize-group "bufferlo"))]))
(easy-menu-define bufferlo-mode-line-menu nil
"`bufferlo-mode' mode-line menu."
`("Bufferlo"
,bufferlo-menu-item-raise))
;; NOTE: Undocumented in `make-frame' that the current buffer cannot be
;; conventionally hidden (space as first character). `with-temp-buffer'
;; doesn't work either in this context (for the same reason).
;; TODO: In Emacs 31, bind expose-hidden-buffer t to allow hidden buffers.
(defmacro bufferlo--with-temp-buffer (&rest body)
"Execute BODY with \"*bufferlo temp buffer*\" current buffer."
(let ((buff-name (generate-new-buffer-name "*bufferlo temp buffer*")))
(with-current-buffer (get-buffer-create buff-name t)
(unwind-protect
`(progn ,@body)
(kill-buffer buff-name)))))
(defun bufferlo--make-frame (&optional restore-geometry)
"Make a new frame with `fullscreen' suppressed if RESTORE-GEOMETRY is non nil."
(make-frame (if restore-geometry '((fullscreen . nil)) nil)))
(defun bufferlo-local-buffer-p (buffer &optional frame tabnum include-hidden)
"Return non-nil if BUFFER is in the list of local buffers.
A non-nil value of FRAME selects a specific frame instead of the current one.
If TABNUM is nil, the current tab is used. If it is non-nil, it specifies
a tab index in the given frame. If INCLUDE-HIDDEN is set, include hidden
buffers, see `bufferlo-hidden-buffers'."
(memq buffer (bufferlo-buffer-list frame tabnum include-hidden)))
(defun bufferlo-non-local-buffer-p (buffer &optional frame tabnum include-hidden)
"Return non-nil if BUFFER is not in the list of local buffers.
A non-nil value of FRAME selects a specific frame instead of the current one.
If TABNUM is nil, the current tab is used. If it is non-nil, it specifies
a tab index in the given frame. If INCLUDE-HIDDEN is set, include hidden
buffers, see `bufferlo-hidden-buffers'."
(not (bufferlo-local-buffer-p buffer frame tabnum include-hidden)))
(defun bufferlo-local-buffers ()
"Return a list of local buffers for the current frame."
(seq-filter #'bufferlo-local-buffer-p
(buffer-list)))
(defun bufferlo-non-local-buffers ()
"Return a list of non-local buffers for the current frame."
(seq-filter #'bufferlo-non-local-buffer-p
(buffer-list)))
(defun bufferlo--clear-buffer-lists (&optional frame)
"This is a workaround advice function to fix tab-bar's tab switching behavior.
On `tab-bar-select-tab', if `wc-bl' or `wc-bbl' is nil, the function does not
set the corresponding `buffer-list' / `buried-buffer-list' frame parameters.
As a result the previous tab's values remain active.
To mitigate this, this functions clears `buffer-list' and `buried-buffer-list'.
It should be set up as an advice after `tab-bar--tab' and takes its FRAME
parameter. In addition, `bufferlo--clear-buffer-lists-activate' must be
set up as a advice around `tab-bar-select-tab' to activate this function
when `tab-bar--tab' is called from `tab-bar-select-tab."
(when bufferlo--clear-buffer-lists-active
(set-frame-parameter frame 'buffer-list nil)
(set-frame-parameter frame 'buried-buffer-list nil)))
(defun bufferlo--clear-buffer-lists-activate (oldfn &rest args)
"This should be set up as a advice around `tab-bar-select-tab'.
It activates clearing the buffer lists for `tab-bar--tab'
before calling OLDFN with ARGS. See `bufferlo--clear-buffer-lists'."
(let* ((bufferlo--clear-buffer-lists-active t)
(result (apply oldfn args)))
;; Occasionally it happens that a non-local buffer is shown in the tab,
;; after switching frames, primarily with empty tabs.
;; This workaround selects a buffer that is in the local list in such a case.
(unless (bufferlo-local-buffer-p (current-buffer) nil nil t)
(let ((buffer (or
(seq-find (lambda (b) (not (minibufferp b)))
(frame-parameter nil 'buffer-list))
(seq-find (lambda (b) (not (minibufferp b)))
(frame-parameter nil 'buried-buffer-list)))))
(switch-to-buffer buffer t t)))
result))
;; via window.el switch-to-prev-buffer-skip-p
;; (funcall skip window buffer bury-or-kill)
(defun bufferlo--switch-to-prev-buffer-skip-p (_window buffer _bury-or-kill)
"Restrict BUFFER to the current tab's locals for buffer switching.
Return non-nil if `switch-to-prev-buffer' (and and
`switch-to-next-buffer') should skip BUFFER. Includes hidden buffers.
If the frame parameter \\='buffer-list is nil, hence it has no local
buffers, do not skip."
(if (frame-parameter nil 'buffer-list)
(not (bufferlo-local-buffer-p buffer nil (tab-bar--current-tab-index) t))
nil))
(defvar bufferlo--switch-to-prev-buffer-skip-orig)
(defun bufferlo--set-switch-to-prev-buffer-skip ()
"Set the buffer predicate of FRAME to `bufferlo--buffer-predicate'."
(setq bufferlo--switch-to-prev-buffer-skip-orig switch-to-prev-buffer-skip)
(setq switch-to-prev-buffer-skip #'bufferlo--switch-to-prev-buffer-skip-p))
(defun bufferlo--reset-switch-to-prev-buffer-skip ()
"Reset `switch-to-prev-buffer-skip'."
(setq switch-to-prev-buffer-skip bufferlo--switch-to-prev-buffer-skip-orig))
(defun bufferlo--buffer-predicate (buffer)
"Return whether BUFFER is local to the current frame/tab.
Includes hidden buffers.
If the frame parameter \\='buffer-list is nil, hence it has no local
buffers, treat BUFFER as local."
(if (frame-parameter nil 'buffer-list)
(bufferlo-local-buffer-p buffer nil nil t)
t))
(defun bufferlo--set-buffer-predicate (frame)
"Set the buffer predicate of FRAME to `bufferlo--buffer-predicate'."
(set-frame-parameter frame 'buffer-predicate #'bufferlo--buffer-predicate))
(defun bufferlo--reset-buffer-predicate (frame)
"Reset the buffer predicate of FRAME if it is `bufferlo--buffer-predicate'."
(when (eq (frame-parameter frame 'buffer-predicate)
#'bufferlo--buffer-predicate)
(set-frame-parameter frame 'buffer-predicate nil)))
(defun bufferlo--merge-regexp-list (regexp-list)
"Merge a list of regular expressions REGEXP-LIST."
(mapconcat (lambda (x)
(concat "\\(?:" x "\\)"))
regexp-list "\\|"))
(defun bufferlo--include-exclude-buffers (frame)
"Include and exclude buffers from the local buffer list of FRAME."
;; We know this is called from a frame hook, so lift any previous tab-hook
;; inhibition. See below to understand this strategy.
(bufferlo--tab-include-exclude-buffers-inhibit 'uninhibit)
(let* ((include (bufferlo--merge-regexp-list
(append '("a^") bufferlo-include-buffer-filters)))
(exclude (bufferlo--merge-regexp-list
(append '("a^") bufferlo-exclude-buffer-filters)))
(buffers (bufferlo--current-buffers frame))
(buffers (seq-filter (lambda (b)
(not (string-match-p exclude (buffer-name b))))
buffers))
(incl-buffers (seq-filter (lambda (b)
(string-match-p include (buffer-name b)))
(buffer-list frame)))
(buffers (delete-dups (append buffers incl-buffers))))
;; FIXME: Currently all the included buffers are put into the 'buffer-list,
;; even if they were in the 'buried-buffer-list before.
(set-frame-parameter frame 'buffer-list
;; The current buffer must be always on the list,
;; otherwise the buffer list gets replaced later.
(push (if frame
(with-selected-frame frame (current-buffer))
(current-buffer))
buffers))
(set-frame-parameter frame 'buried-buffer-list nil)))
;; Starting in Emacs 31, `tab-bar-tab-post-open-functions' are called for the
;; initial tab on a frame, and prior to `after-make-frame-functions' when the
;; `tab-bar' is being set up during frame initialization. We ensure that
;; `bufferlo--tab-include-exclude-buffers' is skipped until
;; `bufferlo--include-exclude-buffers' is called after the frame is created,
;; and our curated `buffer-list' parameter is initialized for the frame/tab.
;; Therein after it is safe to call `bufferlo--tab-include-exclude-buffers'
;; for each subsequent new tab. This strategy is safe to use on Emacs < 31.
(defvar bufferlo--tab-include-exclude-buffers-inhibit nil
"Set to non-nil during `make-frame'.
This inhibits `bufferlo--tab-include-exclude-buffers' and is reset by
`bufferlo--include-exclude-buffers'. This cannot be let-bound unless we
advise `make-frame'.")
(defun bufferlo--tab-include-exclude-buffers-inhibit (&optional uninhibit)
"Inhibit `bufferlo--tab-include-exclude-buffers' in `before-make-frame-hook'.
`bufferlo--include-exclude-buffers' which lifts this inhibition is run
by `after-make-frame-functions'.
If optional argument UNINHIBIT is non-nil, the inhibition is disabled."
(setq bufferlo--tab-include-exclude-buffers-inhibit (not uninhibit)))
(defun bufferlo--tab-include-exclude-buffers (ignore)
"Include and exclude buffers from the buffer list of the current tab's frame.
Argument IGNORE is for compatibility with `tab-bar-tab-post-open-functions'."
(ignore ignore)
;; Reset the local buffer list unless we clone the tab (tab-duplicate).
(unless bufferlo--tab-include-exclude-buffers-inhibit
(unless (eq tab-bar-new-tab-choice 'clone)
(bufferlo--include-exclude-buffers nil))))
(defun bufferlo--current-buffers (frame)
"Get the buffers of the current tab in FRAME."
(seq-filter
#'buffer-live-p
(if bufferlo-include-buried-buffers
(append
(frame-parameter frame 'buffer-list)
(frame-parameter frame 'buried-buffer-list))
(frame-parameter frame 'buffer-list))))
(defun bufferlo--get-tab-buffers (tab)
"Extract buffers from the given TAB structure."
(seq-filter
#'buffer-live-p
(or
(if bufferlo-include-buried-buffers
(append
(cdr (assq 'wc-bl tab))
(cdr (assq 'wc-bbl tab)))
(cdr (assq 'wc-bl tab)))
;; fallback to bufferlo-buffer-list, managed by bufferlo--window-state-*
(mapcar #'get-buffer
(car (cdr (assq 'bufferlo-buffer-list (assq 'ws tab))))))))
(defun bufferlo--get-buffers (&optional frame tabnum)
"Get the buffers of tab TABNUM in FRAME.
If FRAME is nil, the current frame is selected.
If TABNUM is nil, the current tab is selected.
If TABNUM is \\='all, all tabs of the frame are selected."
(cond ((eq tabnum 'all)
(seq-uniq (seq-mapcat (lambda (tb)
(if (eq 'current-tab (car tb))
(bufferlo--current-buffers frame)
(bufferlo--get-tab-buffers tb)))
(funcall tab-bar-tabs-function frame))))
(tabnum
(let ((tab (nth tabnum (funcall tab-bar-tabs-function frame))))
(if (eq 'current-tab (car tab))
(bufferlo--current-buffers frame)
(bufferlo--get-tab-buffers tab))))
(t
(bufferlo--current-buffers frame))))
(defun bufferlo-buffer-list (&optional frame tabnum include-hidden)
"Return a list of all live buffers associated with the current frame and tab.
A non-nil value of FRAME selects a specific frame instead of the current one.
If TABNUM is nil, the current tab is used. If it is non-nil, it specifies
a tab index in the given frame. If INCLUDE-HIDDEN is set, include hidden
buffers, see `bufferlo-hidden-buffers'."
(let ((list (bufferlo--get-buffers frame tabnum)))
(if include-hidden
list
(seq-filter (lambda (buffer)
(let ((hidden (bufferlo--merge-regexp-list
(append '("a^") bufferlo-hidden-buffers))))
(not (string-match-p hidden (buffer-name buffer)))))
list))))
(defun bufferlo--window-state-get (oldfn &optional window writable)
"Save the frame's buffer list to the window state.
Used as advice around `window-state-get'. OLDFN is the original
function. WINDOW and WRITABLE are passed to the function."
(let ((ws (apply oldfn (list window writable))))
(let* ((buffers (bufferlo--current-buffers (window-frame window)))
(names (mapcar #'buffer-name buffers)))
(if names
(append ws (list (list 'bufferlo-buffer-list names)))
ws))))
(defun bufferlo--window-state-put (state &optional window _ignore)
"Restore the frame's buffer list from the window state.
Used as advice after `window-state-put'. STATE is the window state.
WINDOW is the window in question. IGNORE is not used and exists for
compatibility with the advised function."
;; We have to make sure that the window is live at this point.
;; `frameset-restore' may pass a window with a non-existing buffer
;; to `window-state-put', which in turn will delete that window
;; before the advice calls us.
;; This is not the case when we are called from `tab-bar-select-tab'.
(when (or bufferlo--desktop-advice-active-force
(and bufferlo--desktop-advice-active (window-live-p window)))
;; FIXME: Currently there is no distinction between buffers and
;; buried buffers for desktop.el.
(let ((bl (car (cdr (assq 'bufferlo-buffer-list state)))))
(set-frame-parameter (window-frame window) 'buffer-list
;; The current buffer must be always on the list,
;; otherwise the buffer list gets replaced later.
(cons (window-buffer window)
(mapcar #'get-buffer bl)))
(set-frame-parameter (window-frame window) 'buried-buffer-list
(list (window-buffer window))))))
(defun bufferlo--activate (oldfn &rest args)
"Activate the advice for `bufferlo--window-state-{get,put}'.
OLDFN is the original function. ARGS is for compatibility with
the advised functions."
(let ((bufferlo--desktop-advice-active t))
(apply oldfn args)))
(defun bufferlo--activate-force (oldfn &rest args)
"Activate the advice for `bufferlo--window-state-{get,put}'.
OLDFN is the original function. ARGS is for compatibility with
the advised functions."
(let ((bufferlo--desktop-advice-active t)
(bufferlo--desktop-advice-active-force t))
(apply oldfn args)))
(defun bufferlo--clone-undelete-frame-advice (oldfn &rest args)
"Activate the advice for `clone-frame' and `undelete-frame'.
OLDFN is the original function. ARGS is for compatibility with the
advised functions. Honors `bufferlo-bookmark-frame-duplicate-policy'."
(let ((bufferlo--desktop-advice-active t)
(bufferlo--desktop-advice-active-force t))
(apply oldfn args))
(when-let* ((bookmark-name (frame-parameter nil 'bufferlo-bookmark-frame-name))
(abm (assoc bookmark-name (bufferlo--active-bookmarks)))
(this+at-least-one-other
(> (seq-count (lambda (x) (equal bookmark-name (car x)))
(bufferlo--active-bookmarks))
1)))
(let ((msg nil))
(cl-labels
((msg-append (s) (setq msg (concat msg "; " s))))
(when
(catch :abort
;; `bufferlo--bookmark-get-duplicate-policy' throws :abort
(let ((duplicate-policy (bufferlo--bookmark-get-duplicate-policy
bookmark-name
"frame"
bufferlo-bookmark-frame-duplicate-policy
'undelete)))
(pcase duplicate-policy
('allow)
('clear
(setq bookmark-name nil))
('clear-warn
(setq bookmark-name nil)
(msg-append "cleared frame bookmark"))
('ignore
(throw :abort t))
('raise
;; NOTE: We throw nil here!
;; We delete the frame ourselves before raising.
(delete-frame)
(bufferlo--bookmark-raise abm)
(throw :abort nil)))
(set-frame-parameter nil 'bufferlo-bookmark-frame-name
bookmark-name))
(when msg
(message "Undelete frame bufferlo bookmark%s%s"
(if bookmark-name (format ": %s" bookmark-name) "")
(or msg "")))
nil)
(delete-frame))))))
(defun bufferlo--tab-post-undo-close-tab-function (tab)
"Handle `tab-bar-undo-close-tab' TAB.
Honors `bufferlo-bookmark-tab-duplicate-policy'."
(when-let* ((bookmark-name (alist-get 'bufferlo-bookmark-tab-name tab))
(this+at-least-one-other
(> (seq-count (lambda (x) (equal bookmark-name (car x)))
(bufferlo--active-bookmarks))
1)))
(let ((msg nil))
(cl-labels
((msg-append (s) (setq msg (concat msg "; " s))))
(when
(catch :abort
(let ((duplicate-policy (bufferlo--bookmark-get-duplicate-policy
bookmark-name "tab"
bufferlo-bookmark-tab-duplicate-policy
'undelete)))
(pcase duplicate-policy
('allow)
('clear
(setq bookmark-name nil))
('clear-warn
(setq bookmark-name nil)
(msg-append "cleared tab bookmark"))
('ignore
(throw :abort t))
('raise
;; NOTE: We throw nil here!
;; We delete the frame ourselves before raising.
(let (tab-bar-tab-prevent-close-functions)
(tab-bar-close-tab))
;; Find bookmark to raise; tab numbers changes when closing.
(bufferlo--bookmark-raise
(assoc bookmark-name (bufferlo--active-bookmarks)))
(throw :abort nil)))
(setf (alist-get 'bufferlo-bookmark-tab-name
(cdr tab))
bookmark-name))
(when msg
(message "Undo close tab bufferlo bookmark%s%s"
(if bookmark-name (format ": %s" bookmark-name) "")
(or msg "")))
nil)
(let (tab-bar-tab-prevent-close-functions)
(tab-bar-close-tab)))))))
(defun bufferlo--tab-bar-undo-close-tab-advice (oldfn &rest args)
"Activate the advice for `tab-bar-undo-close-tab'.
Pass ARGS to OLDFN."
(let ((bufferlo--desktop-advice-active t)
(bufferlo--desktop-advice-active-force t))
(apply oldfn args))
(bufferlo--tab-post-undo-close-tab-function (bufferlo--current-tab)))
(defsubst bufferlo--warn ()
"Warn if `bufferlo-mode' is not enabled."
(defvar bufferlo--warn-current-command nil)
(when (and (not bufferlo-mode)
(not (eq this-command bufferlo--warn-current-command)))
(setq bufferlo--warn-current-command this-command)
(message (format "Warning: %s: bufferlo-mode should be enabled"
this-command))))
(defun bufferlo-clear (&optional frame)
"Clear the frame/tab's buffer list, except for the current buffer.
If FRAME is nil, use the current frame."
(interactive)
(bufferlo--warn)
(set-frame-parameter frame 'buffer-list
(list (if frame
(with-selected-frame frame
(current-buffer))
(current-buffer))))
(set-frame-parameter frame 'buried-buffer-list nil))
(defun bufferlo-remove (buffer)
"Remove BUFFER from the frame/tab's buffer list."
(interactive
(list
(let ((lbs (mapcar (lambda (b) (buffer-name b))
(bufferlo-buffer-list))))
(read-buffer "Remove buffer: " nil t
(lambda (b) (member (car b) lbs))))))
(bufferlo--warn)
(let ((bl (frame-parameter nil 'buffer-list))
(bbl (frame-parameter nil 'buried-buffer-list))
(buffer (get-buffer buffer)))
(set-frame-parameter nil 'buffer-list (delq buffer bl))
(set-frame-parameter nil 'buried-buffer-list (delq buffer bbl))
;; Adapted from bury-buffer:
(cond
((or (not (eq buffer (window-buffer)))
;; Don't try to delete the minibuffer window, undedicate it
;; or switch to a previous buffer in it.
(window-minibuffer-p)))
((window--delete nil t))
(t
;; Switch to another buffer in window.
(set-window-dedicated-p nil nil)
(switch-to-prev-buffer nil 'bury)))
nil))
(defun bufferlo-remove-non-exclusive-buffers ()
"Remove all buffers from the local buffer list that are not exclusive to it."
(interactive)
(bufferlo--warn)
(dolist (buffer (bufferlo--get-exclusive-buffers nil nil t))
(bufferlo-remove buffer)))
(defun bufferlo-bury (&optional buffer-or-name)
"Bury and remove the buffer specified by BUFFER-OR-NAME from the local list.
If `bufferlo-include-buried-buffers' is set to nil then this has the same
effect as a simple `bury-buffer'."
(interactive)
(bufferlo--warn)
(let ((buffer (window-normalize-buffer buffer-or-name)))
(bury-buffer-internal buffer)
(bufferlo-remove buffer)))
(defun bufferlo--get-captured-buffers (&optional exclude-frame exclude-tabnum)
"Get all buffers that are in a local list of at least one frame or tab.
If EXCLUDE-FRAME is a frame, exclude the local buffer list of the tab with
the number EXCLUSIVE-TABNUM of this frame.
If EXCLUSIVE-TABNUM is nil, select the default tab.
If EXCLUSIVE-TABNUM is \\='all, select all tabs of the frame.
If EXCLUDE-FRAME is nil, do not exclude a local buffer list
and ignore EXCLUDE-TABNUM."
(let ((exclude-tab (when (and exclude-tabnum (not (eq exclude-tabnum 'all)))
(nth exclude-tabnum
(funcall tab-bar-tabs-function exclude-frame)))))
(cl-labels
((get-inactive-tabs-buffers (f)
(seq-mapcat
(lambda (tb)
(unless (and (eq f exclude-frame)
(or (eq exclude-tabnum 'all)
(eq tb exclude-tab)))
(bufferlo--get-tab-buffers tb)))
(funcall tab-bar-tabs-function f)))
(get-frames-buffers ()
(seq-mapcat
(lambda (f)
(unless (and (eq f exclude-frame)
(or (eq exclude-tabnum 'all)
(not exclude-tab)
(eq 'current-tab (car exclude-tab))))
(bufferlo--current-buffers f)))
(frame-list))))
(seq-uniq
(append (seq-mapcat #'get-inactive-tabs-buffers (frame-list))
(get-frames-buffers))))))
(defun bufferlo--get-orphan-buffers ()
"Get all buffers that are not in any local list of a frame or tab."
(seq-filter (lambda (b)
(not (memq b (bufferlo--get-captured-buffers))))
(buffer-list)))
(defun bufferlo--get-exclusive-buffers (&optional frame tabnum invert)
"Get all buffers that are exclusive to this frame and tab.
If FRAME is nil, use the current frame.
If TABNUM is nil, use the current tab.
If TABNUM is \\='all, kill all tabs of the frame.
If INVERT is non-nil, return the non-exclusive buffers instead."
(let ((other-bufs (bufferlo--get-captured-buffers (or frame (selected-frame))
tabnum))
(this-bufs (bufferlo--get-buffers frame tabnum)))
(seq-filter (if invert
(lambda (b) (memq b other-bufs))
(lambda (b) (not (memq b other-bufs))))
this-bufs)))
(defvar bufferlo--kill-buffer-frame-or-tab-closing nil
"Bind to non-nil when closing frame/tab and calling bufferlo kill buffers.")
(defun bufferlo--kill-buffer-safely (buffer)
"Kill BUFFER respecting that `replace-buffer-in-windows' might kill the frame."
;; bug#71386
;; These shenanigans are needed because `kill-buffer' calls
;; `replace-buffer-in-windows' which, without regard to other
;; tabs on the frame that contain "potential windows" in their
;; window configurations... "If that window is the only window
;; on its frame, delete its frame when there are other frames
;; left [on the terminal]." What it should do when a tab is
;; active, is either leave a placeholder buffer, or close the
;; tab and signal that it did either of those things.
;;
;; "In any other case, `replace-buffer-in-windows' calls
;; `switch-to-prev-buffer' to display some other buffer in that
;; window." If a bufferlo-managed tab has only a single buffer
;; and no `window-prev-buffers', we concoct one to retain the
;; window before closing the tab.
(let ((frame-auto-hide-function) ; inhibit automatic frame deletion
;; No interference for buffer replacement selection
(switch-to-prev-buffer-skip)
(switch-to-prev-buffer-skip-regexp))
(when (and (one-window-p 'no-mini)
(eq (window-deletable-p) 'frame))
;; If this is the final window on the frame, and the frame would be
;; deleted by kill-buffer / replace-buffer-in-windows, leave one live
;; buffer to prevent the frame being killed. The bufferlo calling
;; functions that close tabs and frames will handle tab/frame closing.
(switch-to-buffer " *bufferlo temp*" 'norecord 'force-same-window)
;; This prevents the hidden buffer from visually leaking when the
;; calling bufferlo function is not intending to close the tab or frame.
(unless bufferlo--kill-buffer-frame-or-tab-closing
(switch-to-prev-buffer)))
(kill-buffer buffer)))
(defun bufferlo--kill-buffer-forced (buffer)
"Forcibly kill BUFFER, even if modified."
(let ((kill-buffer-query-functions nil))
(with-current-buffer buffer
(set-buffer-modified-p nil)
(bufferlo--kill-buffer-safely buffer))))
(defun bufferlo--kill-buffer (buffer)
"Kill BUFFER respecting `bufferlo-kill-modified-buffers-policy'."
(condition-case-unless-debug err
(pcase bufferlo-kill-modified-buffers-policy
('retain-modified
(unless (buffer-modified-p buffer)
(bufferlo--kill-buffer-safely buffer)))
('retain-modified-kill-without-file-name
(if (not (buffer-file-name buffer))
(bufferlo--kill-buffer-forced buffer)
(unless (buffer-modified-p buffer)
;; No prompting (these are unmodified buffers)
(let ((kill-buffer-query-functions))
(bufferlo--kill-buffer-safely buffer)))))
('kill-modified
(bufferlo--kill-buffer-forced buffer))
(_ (bufferlo--kill-buffer-safely buffer)))
(error (message "Bufferlo could not kill buffer `%s': %S"
(buffer-name buffer) (error-message-string err)))))
(defun bufferlo-kill-buffers (&optional killall frame tabnum internal-too)
"Kill the buffers of the frame/tab-local buffer list.
By default, this will only kill buffers that are exclusive to the frame/tab.
If KILLALL (prefix argument) is given then buffers that are also present in the
local lists of other frames and tabs are killed too.
Buffers matching `bufferlo-kill-buffers-exclude-filters' are never killed.
If FRAME is nil, use the current frame.
If TABNUM is nil, use the current tab.
If TABNUM is \\='all, kill all tabs of the frame.
Ignores buffers whose names start with a space, unless optional
argument INTERNAL-TOO is non-nil."
(interactive "P")
(bufferlo--warn)
(when (or (not bufferlo-kill-buffers-prompt)
(y-or-n-p "Kill bufferlo local buffers? "))
(let* ((exclude (bufferlo--merge-regexp-list
(append '("a^") bufferlo-kill-buffers-exclude-filters)))
(kill-list (if killall
(bufferlo--get-buffers frame tabnum)
(bufferlo--get-exclusive-buffers frame tabnum)))
(buffers (seq-filter
(lambda (b)
(not (or
(and (not internal-too)
(string-prefix-p " " (buffer-name b)))
(string-match-p exclude (buffer-name b)))))
kill-list)))
(dolist (b buffers)
(bufferlo--kill-buffer b)))))
(defun bufferlo-kill-orphan-buffers (&optional internal-too)
"Kill all buffers that are not in any local list of a frame or tab.
Ignores buffers whose names start with a space, unless optional
argument INTERNAL-TOO is non-nil.
Buffers matching `bufferlo-kill-buffers-exclude-filters' are never killed."
(interactive)
(bufferlo--warn)
(when (or (not bufferlo-kill-buffers-prompt)
(y-or-n-p "Kill bufferlo local buffers? "))
(let* ((exclude (bufferlo--merge-regexp-list
(append '("a^") bufferlo-kill-buffers-exclude-filters)))
(buffers (seq-filter
(lambda (b)
(not (or
(and (not internal-too)
(string-prefix-p " " (buffer-name b)))
(string-match-p exclude (buffer-name b)))))
(bufferlo--get-orphan-buffers))))
(dolist (b buffers)
(bufferlo--kill-buffer b)))))
(defun bufferlo-delete-frame-kill-buffers (&optional frame internal-too)
"Delete a frame and kill the local buffers of its tabs.
If FRAME is nil, kill the current frame.
Ignores buffers whose names start with a space, unless optional
argument INTERNAL-TOO is non-nil."
(interactive)
(bufferlo--warn)
(setq frame (or frame (selected-frame)))
(let ((fbm (frame-parameter frame 'bufferlo-bookmark-frame-name)))
(when (or (not bufferlo-delete-frame-kill-buffers-prompt)
(y-or-n-p (format "Kill frame%s and its buffers? "
(if fbm
(format " `%s'" fbm)
""))))
(cl-labels
((save-as-current (frame)
;; We need this if called in a batch
(with-selected-frame frame
(bufferlo-bookmark-frame-save-current))))
(pcase bufferlo-bookmark-frame-save-on-delete
((or 't 'on-kill-buffers)
(when (y-or-n-p (format-message "Save frame bookmark `%s'? " fbm))
(save-as-current frame)))
((or 'when-bookmarked 'on-kill-buffers-when-bookmarked)
(when fbm (save-as-current frame))))
;; If batch, raise frame in case of prompts for buffers that need saving.
(raise-frame frame)
(let ((bufferlo-kill-buffers-prompt nil)
(bufferlo--kill-buffer-frame-or-tab-closing t))
(bufferlo-kill-buffers nil frame 'all internal-too))
;; kill-buffer calls replace-buffer-in-windows which will
;; delete windows *and* their frame so we have to test if
;; the frame in question is still live.
(when (frame-live-p frame)
;; Something could go wrong while trying to delete the frame.
;; Since we already killed all buffers we disconnect the bookmark.
;; This prevents the auto-save feature from overwriting an existing
;; bookmark with the empty buffer list.
(set-frame-parameter frame 'bufferlo-bookmark-frame-name nil)
;; TODO: Emacs 30 frame-deletable-p
;; account for top-level, non-child frames
(when (= 1 (length (seq-filter
(lambda (x) (null (frame-parameter x 'parent-frame)))
(frame-list))))
(make-frame)) ; leave one for the user
(let ((bufferlo-bookmark-frame-save-on-delete nil))
(delete-frame frame)))))))
(defun bufferlo-tab-close-kill-buffers (&optional killall internal-too)
"Close the current tab and kill the local buffers.
The optional arguments KILLALL and INTERNAL-TOO are passed to
`bufferlo-kill-buffers'."
(interactive "P")
(bufferlo--warn)
(let* ((orig-frame (selected-frame))
(orig-tab (bufferlo--current-tab))
(tbm (alist-get 'bufferlo-bookmark-tab-name orig-tab))
kill-buffer-closed-tab)
(when (or (not bufferlo-close-tab-kill-buffers-prompt)
(y-or-n-p (format "Kill tab%s and its buffers? "
(if tbm
(format " `%s'" tbm)
""))))
(pcase bufferlo-bookmark-tab-save-on-close
((or 't 'on-kill-buffers)
(when (y-or-n-p (format-message "Save tab bookmark `%s'? " tbm))
(bufferlo-bookmark-tab-save-current)))
((or 'when-bookmarked 'on-kill-buffers-when-bookmarked)
(when tbm (bufferlo-bookmark-tab-save tbm))))
(let* ((bufferlo-kill-buffers-prompt nil)
(bufferlo--kill-buffer-frame-or-tab-closing t)
;; Emacs 31 `window--delete' can call tab-bar-close-tab
(tab-bar-tab-prevent-close-functions nil)
(tab-bar-tab-pre-close-functions
(list (lambda (_tab _last-tab-p)
(setq kill-buffer-closed-tab t)))))
(bufferlo-kill-buffers killall nil nil internal-too))
(let ((bufferlo-bookmark-tab-save-on-close nil)
(tab-bar-close-last-tab-choice 'delete-frame))
;; Close the tab only if we can detect that it's hasn't been closed by
;; `'kill-buffer' (via `replace-buffer-in-windows',
;; `window-deletable-p', `window--delete').
(unless (or kill-buffer-closed-tab
(not (eq orig-frame (selected-frame)))
(not (eq orig-tab (bufferlo--current-tab))))
;; Catch errors in case this is the last tab on the last frame
(ignore-errors (tab-bar-close-tab)))))))
(defun bufferlo-isolate-project (&optional file-buffers-only)
"Isolate a project in the frame or tab.
Remove all buffers that do not belong to the current project from the
local buffer list. If FILE-BUFFERS-ONLY is non-nil or the prefix
argument is given, remove only buffers that visit a file. Buffers
matching `bufferlo-include-buffer-filters' are not removed."
(interactive "P")
(bufferlo--warn)
(if-let* ((curr-project (project-current))
(include (bufferlo--merge-regexp-list
(append '("a^") bufferlo-include-buffer-filters))))
(dolist (buffer (bufferlo-buffer-list))
(when (and (not (string-match-p include (buffer-name buffer)))
(not (equal curr-project
(with-current-buffer buffer (project-current))))
(or (not file-buffers-only) (buffer-file-name buffer)))
(bufferlo-remove buffer)))
(message "Current buffer is not part of a project")))
(defun bufferlo-find-buffer (buffer-or-name)
"Switch to the frame/tab containing BUFFER-OR-NAME in its local list.
If multiple frame or tabs contain the buffer, interactively prompt
for the to-be-selected frame and tab.
This does not select the buffer -- just the containing frame and tab."
(interactive "b")
(bufferlo--warn)
(let ((buffer (get-buffer buffer-or-name)))
(cl-labels
((search-tabs (f)
(let ((i 0))
(mapcar
(lambda (tab)
(setq i (1+ i))
(when (bufferlo-local-buffer-p buffer f (1- i) t)
(list f (frame-parameter f 'name)
(eq f (selected-frame))
i (cdr (assq 'name tab)))))
(funcall tab-bar-tabs-function f))))
(search-frames (f)
(unless (frame-parameter f 'no-accept-focus)
(if (funcall tab-bar-tabs-function f)
;; has tabs
(search-tabs f)
;; has no tabs
(when (bufferlo-local-buffer-p buffer f nil t)
(list (list f (frame-parameter f 'name)
(eq f (selected-frame))
nil nil)))))))
(let* ((candidates (seq-filter #'identity
(seq-mapcat #'search-frames (frame-list))))
(candidates (mapcar
(lambda (c)
(let ((sel (if (nth 2 c) " [this]" ""))
(frame-name (nth 1 c))
(frame-obj (nth 0 c))
(tab-index (nth 3 c))
(tab-name (nth 4 c)))
(if tab-index
(cons (format "Frame: %s (%s)%s Tab %s: %s"
frame-name frame-obj sel
tab-index tab-name)
c)
(cons (format "Frame: %s (%s)%s"
frame-name frame-obj sel)
c))))
candidates))
(selected (if (cdr candidates)
(completing-read
"Select frame/tab: "
candidates
nil 'require-match)
(caar candidates)))
(selected (assoc selected candidates)))
(if (not selected)
(message "Orphan: No frame/tab contains buffer '%s'" (buffer-name buffer))
(let ((frame (nth 1 selected))
(tab-index (nth 4 selected)))
(select-frame-set-input-focus frame)
(when tab-index
(tab-bar-select-tab tab-index))
frame))))))
(defun bufferlo-find-buffer-switch (buffer-or-name)
"Switch to the frame/tab containing BUFFER-OR-NAME and select the buffer.
This is like `bufferlo-find-buffer' but additionally selects the buffer.
If the buffer is already visible in a non-selected window, select it."
(interactive "b")
(bufferlo--warn)
(when (bufferlo-find-buffer buffer-or-name)
(if-let* ((w (seq-find
(lambda (w)
(eq (get-buffer buffer-or-name) (window-buffer w)))
(window-list))))
(select-window w)
(switch-to-buffer buffer-or-name))))
(defun bufferlo-get-local-scratch-buffer ()
"Get the local scratch buffer or create it if not already existent and return it."
(let ((buffer (seq-find (lambda (b)
(string-match-p
(concat
"^"
(regexp-quote bufferlo-local-scratch-buffer-name)
"\\(<[0-9]*>\\)?$")
(buffer-name b)))
(bufferlo-buffer-list nil nil t))))
(unless buffer
(setq buffer (get-buffer-create
(generate-new-buffer-name
bufferlo-local-scratch-buffer-name)))
(with-current-buffer buffer
(when (eq major-mode 'fundamental-mode)
(funcall (or bufferlo-local-scratch-buffer-initial-major-mode
initial-major-mode
#'ignore)))))
buffer))
(defun bufferlo-create-local-scratch-buffer ()
"Create a local scratch buffer and return it."
(get-buffer-create (generate-new-buffer-name
bufferlo-local-scratch-buffer-name)))
(defun bufferlo-switch-to-scratch-buffer (&optional frame)
"Switch to the scratch buffer.
When FRAME is non-nil, switch to the scratch buffer in the specified frame
instead of the current one."
(interactive)
(if frame
(with-selected-frame frame
(switch-to-buffer "*scratch*"))
(switch-to-buffer "*scratch*")))
(defun bufferlo-switch-to-local-scratch-buffer (&optional frame)
"Switch to the local scratch buffer.
When FRAME is non-nil, switch to the local scratch buffer in the specified frame
instead of the current one."
(interactive)
(if frame
(with-selected-frame frame
(switch-to-buffer (bufferlo-get-local-scratch-buffer)))
(switch-to-buffer (bufferlo-get-local-scratch-buffer))))
(defun bufferlo-toggle-local-scratch-buffer ()
"Switch to the local scratch buffer or bury it if it is already selected.
Creates a new local scratch buffer if none exists for this frame/tab."
(interactive)
(let ((buffer (bufferlo-get-local-scratch-buffer)))
(if (eq buffer (current-buffer))
(bury-buffer)
(switch-to-buffer buffer))))
(defun bufferlo-switch-to-buffer (buffer &optional norecord force-same-window)
"Display the BUFFER in the selected window.
Completion includes only local buffers.
This is the frame/tab-local equivalent to `switch-to-buffer'.
The arguments NORECORD and FORCE-SAME-WINDOW are passed to `switch-to-buffer'.
If the prefix argument is given, include all buffers."
(interactive
(list
(if current-prefix-arg
(read-buffer "Switch to buffer: " (other-buffer (current-buffer)) nil)
(let ((lbs (mapcar #'buffer-name (bufferlo-buffer-list))))
(read-buffer
"Switch to local buffer: " (other-buffer (current-buffer)) nil
(lambda (b) (member (if (stringp b) b (car b)) lbs)))))))
(bufferlo--warn)
(switch-to-buffer buffer norecord force-same-window))
(defvar-local bufferlo--buffer-menu-this-frame nil)
(defun bufferlo--local-buffer-list-this-frame ()
"Return the local buffer list of the buffer's frame."
(bufferlo-buffer-list bufferlo--buffer-menu-this-frame))
(defun bufferlo-list-buffers ()
"Display a list of local buffers."
(interactive)
(bufferlo--warn)
(display-buffer
(let* ((old-buffer (current-buffer))
(name (or (seq-find
(lambda (b)
(string-match-p
"\\`\\*Bufferlo Local Buffer List\\*\\(<[0-9]*>\\)?\\'"
(buffer-name b)))
(bufferlo-buffer-list))
(generate-new-buffer-name "*Bufferlo Local Buffer List*")))
(buffer (get-buffer-create name)))
(with-current-buffer buffer
(Buffer-menu-mode)
(setq bufferlo--buffer-menu-this-frame (selected-frame))
(setq Buffer-menu-files-only nil)
(setq Buffer-menu-buffer-list #'bufferlo--local-buffer-list-this-frame)
(setq Buffer-menu-filter-predicate nil)
(list-buffers--refresh
#'bufferlo--local-buffer-list-this-frame old-buffer)
(tabulated-list-print)
(revert-buffer))
buffer)))
(defun bufferlo-list-orphan-buffers ()
"Display a list of orphan buffers."
(interactive)
(bufferlo--warn)
(display-buffer
(let* ((old-buffer (current-buffer))
(name "*Bufferlo Orphan Buffer List*")
(buffer (get-buffer-create name)))
(with-current-buffer buffer
(Buffer-menu-mode)
(setq bufferlo--buffer-menu-this-frame (selected-frame))
(setq Buffer-menu-files-only nil)
(setq Buffer-menu-buffer-list #'bufferlo--get-orphan-buffers)
(setq Buffer-menu-filter-predicate nil)
(list-buffers--refresh #'bufferlo--get-orphan-buffers old-buffer)
(tabulated-list-print)
(revert-buffer))
buffer)))
(define-ibuffer-filter bufferlo-local-buffers
"Limit current view to local buffers."
(:description "local buffers" :reader nil)
(bufferlo-local-buffer-p buf))
(define-ibuffer-filter bufferlo-orphan-buffers
"Limit current view to orphan buffers."
(:description "orphan buffers" :reader nil)
(not (memq buf (bufferlo--get-captured-buffers))))
(when bufferlo-ibuffer-bind-local-buffer-filter
(define-key ibuffer--filter-map (kbd "l")
'ibuffer-filter-by-bufferlo-local-buffers)
(define-key ibuffer--filter-map (kbd "L")
'ibuffer-filter-by-bufferlo-orphan-buffers))
(defun bufferlo-ibuffer (&optional other-window-p noselect shrink)
"Invoke `ibuffer' filtered for local buffers.
Every frame/tab gets its own local bufferlo ibuffer buffer.
The parameters OTHER-WINDOW-P NOSELECT SHRINK are passed to `ibuffer'."
(interactive)
(bufferlo--warn)
(let ((name (or
(seq-find (lambda (b)
(string-match-p
"\\`\\*Bufferlo Ibuffer\\*\\(<[0-9]*>\\)?\\'"
(buffer-name b)))
(bufferlo-buffer-list))
(generate-new-buffer-name "*Bufferlo Ibuffer*"))))
(ibuffer other-window-p name '((bufferlo-local-buffers . nil))
noselect shrink)))
(defun bufferlo-ibuffer-orphans (&optional other-window-p noselect shrink)
"Invoke `ibuffer' filtered for orphan buffers.
The parameters OTHER-WINDOW-P NOSELECT SHRINK are passed to `ibuffer'."
(interactive)
(bufferlo--warn)
(let ((name "*Bufferlo Orphans Ibuffer*"))
(ibuffer other-window-p name '((bufferlo-orphan-buffers . nil))
noselect shrink)))
(defmacro bufferlo--ibuffer-do-wrapper ()
"Wrapper to support both <31 and 31+ `define-ibuffer-op macro'."
(if (< emacs-major-version 31)
'(define-ibuffer-op ibuffer-do-bufferlo-remove ()
"Remove marked buffers from bufferlo's local buffer list."
(
:active-opstring "remove from bufferlo locals"
:opstring "removed from bufferlo locals:"
:modifier-p t
:dangerous t
:complex t
:after (ibuffer-update nil t)
)
(when bufferlo-mode
(bufferlo-remove buf)
t))
(defun bufferlo--ibuffer-do-bufferlo-remove-prompt (op)
"`ibuffer' prompt helper for OP."
(let ((bookmark-name (bufferlo--current-bookmark-name)))
(format "%s from %slocals:" op
(if bookmark-name
(format-message "bufferlo bookmark `%s' " bookmark-name)
""))))
'(define-ibuffer-op ibuffer-do-bufferlo-remove ()
"Remove marked buffers from bufferlo's local buffer list."
(
:active-opstring (lambda () (bufferlo--ibuffer-do-bufferlo-remove-prompt "remove"))
:opstring (lambda () (bufferlo--ibuffer-do-bufferlo-remove-prompt "removed"))
:modifier-p t
:dangerous t
:complex t
:after (ibuffer-update nil t)
)
(when bufferlo-mode
(bufferlo-remove buf)
t))
))
(bufferlo--ibuffer-do-wrapper)
(when bufferlo-ibuffer-bind-keys
(define-key ibuffer-mode-map "-" #'ibuffer-do-bufferlo-remove))
(define-minor-mode bufferlo-anywhere-mode
"Frame/tab-local buffer lists anywhere you like.
Enables bufferlo's local buffer list for any function that interactively prompts
for buffers via `read-buffer'. By default this enables the local buffer list
for (almost) all functions. Customize `bufferlo-anywhere-filter' and
`bufferlo-anywhere-filter-type' to adapt the behavior.
This minor mode requires `bufferlo-mode' to be enabled.
You can use `bufferlo-anywhere-disable' to disable the local buffer list for
the next command, if the mode is enabled."
:global t
:require 'bufferlo
:init-value nil
:lighter nil
:keymap nil
(if bufferlo-anywhere-mode
(progn
(bufferlo--warn)
(advice-add #'call-interactively
:around #'bufferlo--interactive-advice))
(advice-remove #'call-interactively #'bufferlo--interactive-advice)))
(defvar bufferlo--anywhere-tmp-enabled nil)
(defvar bufferlo--anywhere-tmp-disabled nil)
(defvar bufferlo--anywhere-old-read-buffer-function nil)
(defvar bufferlo--anywhere-nested nil)
(defun bufferlo--interactive-advice (oldfn function &optional record-flags keys)
"Advice function for `call-interactively' for `bufferlo-anywhere-mode'.
Temporarily overrides the `read-buffer-function' to filter the
available buffers to bufferlo's local buffer list.
OLDFN is the original function.
FUNCTION is the interactively called functions.
RECORD-FLAGS and KEYS are passed to `call-interactively'."
(if (or bufferlo--anywhere-tmp-enabled
(and (not bufferlo--anywhere-tmp-disabled)
(xor (eq bufferlo-anywhere-filter-type 'exclude)
(cond
((eq bufferlo-anywhere-filter t) t)
((listp bufferlo-anywhere-filter)
(memq function bufferlo-anywhere-filter))
((functionp bufferlo-anywhere-filter)
(funcall bufferlo-anywhere-filter function))))))
(let* ((bufferlo--anywhere-old-read-buffer-function
;; Preserve the original `read-buffer-function' but not for
;; nested calls; otherwise we would save our own function.
(if bufferlo--anywhere-nested
bufferlo--anywhere-old-read-buffer-function
read-buffer-function))
(bufferlo--anywhere-nested t)
(read-buffer-function
(lambda (prompt &optional def require-match predicate)
(let ((read-buffer-function
bufferlo--anywhere-old-read-buffer-function))
(read-buffer prompt def require-match
(lambda (b)
(and (bufferlo-local-buffer-p
(get-buffer
(if (stringp b) b (car b))))
(or (not predicate)
(funcall predicate b)))))))))
(apply oldfn (list function record-flags keys)))
;; `call-interactively' can be nested, e.g., on M-x invocations.
;; Therefore, we restore the original value of the `read-buffer-function'
;; if we do not use bufferlo's local buffer list for this call.
(let ((read-buffer-function
(if bufferlo--anywhere-nested
bufferlo--anywhere-old-read-buffer-function
read-buffer-function)))
(apply oldfn (list function record-flags keys)))))
(defun bufferlo-anywhere-disable-prefix ()
"Disable `bufferlo-anywhere-mode' only for the next command.
Has no effect if `bufferlo-anywhere-mode' is not enabled.
Has no effect if the next command does not query for a buffer."
(interactive)
(let* ((command this-command)
(minibuffer-depth (minibuffer-depth))
(postfun (make-symbol "bufferlo--anywhere-reenable-next-command")))
(fset postfun
(lambda ()
(unless (or
;; from window.el:display-buffer-override-next-command
(> (minibuffer-depth) minibuffer-depth)
(eq this-command command))
(setq bufferlo--anywhere-tmp-disabled nil)
(remove-hook 'post-command-hook postfun))))
(setq bufferlo--anywhere-tmp-disabled t)
(add-hook 'post-command-hook postfun)))
(defun bufferlo-anywhere-enable-prefix ()
"Use bufferlo's local buffer list for the next command.
Has a similar effect as `bufferlo-anywhere-mode' but only for the next command.
Has no effect if the next command does not query for a buffer.
Can be used with or without `bufferlo-anywhere-mode' enabled.
In contrast to `bufferlo-anywhere-mode', this does not adhere to
`bufferlo-anywhere-filter'. Therefore, you can use it in conjunction with
`bufferlo-anywhere-mode' to temporarily disable the configured filters."
(interactive)
(let* ((command this-command)
(minibuffer-depth (minibuffer-depth))
(postfun (make-symbol "bufferlo--anywhere-disable-next-command")))
(fset postfun
(lambda ()
(unless (or
;; from window.el:display-buffer-override-next-command
(> (minibuffer-depth) minibuffer-depth)
(eq this-command command))
(setq bufferlo--anywhere-tmp-enabled nil)
(unless bufferlo-anywhere-mode
(advice-remove #'call-interactively
#'bufferlo--interactive-advice))
(remove-hook 'post-command-hook postfun))))
(setq bufferlo--anywhere-tmp-enabled t)
(unless bufferlo-anywhere-mode
(advice-add #'call-interactively :around #'bufferlo--interactive-advice))
(add-hook 'post-command-hook postfun)))
(defun bufferlo--bookmark-get-bookmark (bookmark-name-or-record)
"Guarded `bookmark-get-bookmark' for BOOKMARK-NAME-OR-RECORD.
`bookmark-get-bookmark' calls `error' which we want to trap.
Returns the bookmark record if succeeded, nil if failed."
(ignore-errors
(bookmark-get-bookmark bookmark-name-or-record)))
(defun bufferlo--bookmark-jump (bookmark)
"Guarded `bookmark-jump' for BOOKMARK.
It prevents the insertion of a fringe mark and calls `bookmark-jump' with an
empty (no-op) display-func."
(condition-case err
(let ((bookmark-fringe-mark nil))
(bookmark-jump bookmark #'ignore)
t)
(progn
(message (delay-warning 'bufferlo
(format "Error %S when jumping to bookmark %S"
err bookmark)))
nil)))
(defun bufferlo--bookmark-get-for-buffer (buffer)
"Get `buffer-name' and bookmark for BUFFER."
(with-current-buffer buffer
(let ((record (when (or (and (eq bookmark-make-record-function
#'bookmark-make-record-default)
(ignore-errors (bookmark-buffer-file-name)))
(not (eq bookmark-make-record-function
#'bookmark-make-record-default)))
(bookmark-make-record))))
(dolist (fn bufferlo-bookmark-map-functions)
(setq record (funcall fn record)))
(when (and
bufferlo-bookmark-inhibit-bookmark-point
record)
(bookmark-set-position record nil))
(list (buffer-name buffer) record))))
(defun bufferlo--bookmark-filter-buffers (&optional frame)
"Filter out buffers to exclude for bookmarks.
Filters the buffers according to `bufferlo-bookmark-buffers-exclude-filters' and
`bufferlo-bookmark-buffers-include-filters'. The argument FRAME determines the
local buffer list to use. If it is nil, the current frame is used."
(let* ((buffers (bufferlo-buffer-list frame nil t))
(buffers-excl (seq-remove
(lambda (buf)
(seq-filter
(lambda (regexp)
(string-match-p regexp (buffer-name buf)))
bufferlo-bookmark-buffers-exclude-filters))
buffers))
(buffers-incl (seq-filter
(lambda (buf)
(seq-filter
(lambda (regexp)
(string-match-p regexp (buffer-name buf)))
bufferlo-bookmark-buffers-include-filters))
buffers)))
(seq-union buffers-excl buffers-incl)))
(defun bufferlo--bookmark-get-for-buffers-in-tab (buffers)
"Get bookmarks for all BUFFERS of the selected tab in FRAME."
(seq-filter #'identity
(mapcar #'bufferlo--bookmark-get-for-buffer
buffers)))
(defun bufferlo--bookmark-tab-make ()
"Make the tab bookmark record for the current frame and tab."
(let ((filtered-buffers
(bufferlo--bookmark-filter-buffers))
(current-tab (bufferlo--current-tab)))
`((tab-explicit-name . ,(when (alist-get 'explicit-name current-tab)
(alist-get 'name current-tab)))
(tab-group . ,(alist-get 'group current-tab))
(buffer-bookmarks . ,(bufferlo--bookmark-get-for-buffers-in-tab
filtered-buffers))
(buffer-list . ,(mapcar #'buffer-name
filtered-buffers))
(window . ,(window-state-get (frame-root-window) 'writable))
(handler . ,#'bufferlo--bookmark-tab-handler))))
(defun bufferlo--ws-replace-buffer-names (ws replace-alist)
"Replace buffer names according to REPLACE-ALIST in the window state WS."
(mapc (lambda (el)
(when (memq (car el) '(leaf vc hc))
(bufferlo--ws-replace-buffer-names el replace-alist)))
(if (consp (car ws))
(list (cdr ws))
(cdr ws)))
;; Replace name in the buffer field
(when-let* ((buffer-loc (assq 'buffer ws))
(buffer (cadr buffer-loc))
(replace (assoc buffer replace-alist)))
(setf (cadr buffer-loc) (cdr replace)))
;; Replace names in the prev-buffers list
(when-let* ((prev-buffers-loc (assq 'prev-buffers ws))
(prev-buffers (cdr prev-buffers-loc)))
(setf (cdr prev-buffers-loc)
(mapcar (lambda (be)
(if-let ((replace (assoc (car be) replace-alist)))
(cons (cdr replace) (cdr be))
be))
prev-buffers))))
(defvar bufferlo--bookmark-set-loading nil
"Let bind to t when a bookmark set is being loaded.
This controls `bufferlo--bookmark-get-duplicate-policy' to inhibit raise
and quit which are cumbersome during set loading.")
(defun bufferlo--bookmark-get-duplicate-policy (bookmark-name thing default-policy mode &optional embedded-tab)
"Get the duplicate policy for THING BOOKMARK-NAME.
THING should be either \"frame\" or \"tab\".
Ask the user if DEFAULT-POLICY is set to \\='prompt.
MODE can be one of \\='load \\='save \\='undelete, depending on the
invoking action.
EMBEDDED-TAB is non-nil if the tab bookmark is embedded in a frame
bookmark. This functions throws :abort when the user quits.
The variable `bufferlo--bookmark-set-loading' should be non-nil if the
function is invoked as part of a bookmark set restoration.
The functions presents the user with the following options:
allow, clear, ignore, raise, help, quit
Depending on the inputs, not all of these options are visible.
In case of a duplicate tab:
- solo tab: all options
- undo close tab: all options
- solo tab in bookmarked frame (default: prompt): all options
- solo tab in bookmarked frame (default: no prompt): all options
- solo tab in a set (default: prompt): allow, clear, ignore, disallow raise
- solo tab in a set (default: no prompt), allow, clear, ignore, disallow raise
- embedded tab in a set: allow, clear, ignore,
disallow prompt (coerce to ignore), disallow raise (coerce to ignore)
In case of a duplicate frame:
- solo frame: all options
- undelete frame: all options
- frame in a set: allow, clear, ignore, disallow prompt (coerce to ignore),
disallow raise (coerce to ignore)"
(cond ((and bufferlo--bookmark-set-loading
(or embedded-tab (equal thing "frame")))
;; An embedded tab in a set, or a frame in a set:
;; allow, clear, ignore, disallow prompt (coerce to ignore),
;; disallow raise (coerce to ignore)
(pcase default-policy
('prompt 'ignore)
('allow default-policy)
('clear default-policy)
('clear-warn default-policy)
('ignore default-policy)
('raise 'ignore)))
((and bufferlo--bookmark-set-loading
(equal thing "tab")
(not (eq default-policy 'prompt)))
;; A solo tab in a set (and default: no prompt):
;; allow, clear, ignore, disallow prompt, disallow raise
(pcase default-policy
('allow default-policy)
('clear default-policy)
('clear-warn default-policy)
('ignore default-policy)
('raise 'ignore)))
((not (eq default-policy 'prompt))
;; A tab in a frame; do not prompt:
;; allow, clear, ignore, disallow raise (coerce to ignore)
(if embedded-tab
(pcase default-policy
('allow default-policy)
('clear default-policy)
('clear-warn default-policy)
('ignore default-policy)
('raise 'ignore))
;; allow all others
default-policy))
(t ; Prompt for a policy
(let* ((mode-text (pcase mode
('save
"Clear other bookmark")
('load
"Clear bookmark after loading")
('undelete ; invalid in bufferlo--bookmark-set-loading
"Clear bookmark after undeleting/undoing")))
;; A solo tab in a set (and default: prompt):
;; allow, clear, ignore, disallow raise
(disallow-raise (and bufferlo--bookmark-set-loading
(equal thing "tab")))
(question (concat (format-message "%s bookmark `%s' already active: "
(capitalize thing)
bookmark-name)
"Allow"
(format ", %s" mode-text) ; clear thing
", Ignore"
(unless disallow-raise
(format ", Raise existing"))
" "))
(a-allow `("allow" ?a "Allow duplicate"))
(a-clear `("clear" ?c
,(pcase mode
('save
(format "Clear the other %s's bookmark association"
thing))
('load
(format "Clear this %s's bookmark association after loading"
thing))
('undelete
(format "Clear this %s's bookmark association after undeleting/undoing"
thing)))))
(a-ignore `("ignore" ?i "Ignore duplicate"))
(a-raise `("raise" ?r
,(format "Raise the %s with the active bookmark and quit"
thing)))
(a-help `("help" ?h "Help"))
(a-quit `("quit" ?q ,(format "Quit to %s"
(if bufferlo--bookmark-set-loading
"ignore"
"abort"))))
;; An embedded tab in a set: all options
;; A solo tab in a set: allow, clear, ignore, disallow raise
(answers (if (or embedded-tab
(and bufferlo--bookmark-set-loading
(equal thing "tab")))
(list a-allow a-clear a-ignore a-help a-quit)
(list a-allow a-clear a-ignore a-raise a-help a-quit))))
(pcase (with-local-quit
(read-answer question answers))
("allow" 'allow)
("clear" 'clear)
("ignore" 'ignore)
("raise" 'raise)
(_ (if bufferlo--bookmark-set-loading
'ignore
(throw :abort t))))))))
(defun bufferlo--bookmark-tab-get-replace-policy ()
"Get the replace policy for tab bookmarks.
Prompt if `bufferlo-bookmark-tab-replace-policy' is set to \\='prompt.
This functions throws :abort when the user quits."
(if (not (eq bufferlo-bookmark-tab-replace-policy 'prompt))
bufferlo-bookmark-tab-replace-policy
(pcase (with-local-quit
(read-answer "Replace current tab, New tab "
'(("replace" ?o "Replace tab")
("new" ?n "New tab")
("help" ?h "Help")
("quit" ?q "Quit to abort"))))
("replace" 'replace)
("new" 'new)
(_ (throw :abort t)))))
(defun bufferlo--bookmark-tab-get-clear-policy (mode)
"Get the clear policy for tab bookmarks.
Prompt if `bufferlo-bookmark-tab-in-bookmarked-frame-policy' is
set to \\='prompt. This functions throws :abort when the user quits.
MODE is either \\='load, \\='save, or \\='save-frame, depending on the
invoking action. This functions throws :abort when the user quits."
(if (not (eq bufferlo-bookmark-tab-in-bookmarked-frame-policy 'prompt))
bufferlo-bookmark-tab-in-bookmarked-frame-policy
(pcase (with-local-quit
(read-answer
(concat
(pcase mode
('load "Tab bookmark conflicts with frame bookmark: ")
('save "Frame already bookmarked: ")
('save-frame "Tabs in this frame are bookmarked: "))
(format "Allow tab bookmark, Clear %s bookmark "
(if (eq mode 'save) "frame" "tab")))
`(("allow" ?a "Allow tab bookmark")
("clear" ?c ,(if (eq mode 'save)
"Clear frame bookmark, set tab bookmark"
"Clear tab bookmark"))
("help" ?h "Help")
("quit" ?q "Quit to abort"))))
("allow" 'allow)
("clear" 'clear)
(_ (throw :abort t)))))
(defun bufferlo--bookmark-insert-placeholer (orig-name)
"`bufferlo--bookmark-tab-handler' helper function.
Use ORIG-NAME to create a placeholder buffer for buffers that failed to
restore."
(let ((buffer-existed (get-buffer orig-name))
(fail-buffer
(cond
((eq 'placeholder
bufferlo-bookmark-tab-failed-buffer-policy)
;; Do not insert a placeholder for a placeholder
(if (string-match-p "\\`\\*bufferlo failed buffer .*\\*\\'" orig-name)
(generate-new-buffer orig-name)
(generate-new-buffer (format-message "*bufferlo failed buffer `%s'*"
orig-name))))
((eq 'placeholder-orig
bufferlo-bookmark-tab-failed-buffer-policy)
(get-buffer-create orig-name))
((stringp
bufferlo-bookmark-tab-failed-buffer-policy)
(get-buffer-create
bufferlo-bookmark-tab-failed-buffer-policy))
((functionp
bufferlo-bookmark-tab-failed-buffer-policy)
(funcall
bufferlo-bookmark-tab-failed-buffer-policy
orig-name))
(t nil))))
(when fail-buffer
(switch-to-buffer fail-buffer nil 'force-same-window)
(when (and (not buffer-existed)
(memq bufferlo-bookmark-tab-failed-buffer-policy
'(placeholder placeholder-orig)))
(insert
(format-message
"Bufferlo could not restore buffer `%s'" orig-name))
(set-buffer-modified-p nil)))))
(defvar bufferlo--bookmark-handler-no-message nil)
(defun bufferlo--bookmark-tab-handler (bookmark &optional not-jump no-message embedded-tab)
"Handle bufferlo tab bookmark.
The argument BOOKMARK is the to-be restored tab bookmark record created
via `bufferlo--bookmark-tab-make'.
NOT-JUMP should be non-nil when called directly vs. from `bookmark-jump'.
If the optional argument NO-MESSAGE is non-nil, inhibit the message
after successfully restoring the bookmark.
If EMBEDDED-TAB is non-nil, indicate that this bookmark is embedded in a
frame bookmark.
The variable `bufferlo--bookmark-set-loading' is consulted.
Returns nil on success, non-nil on abort."
(catch :abort
(let* ((bookmark-name (if (not embedded-tab)
(bookmark-name-from-full-record bookmark)
nil))
(orig-bookmark-name bookmark-name)
(abm (assoc bookmark-name (bufferlo--active-bookmarks)))
disconnect-tbm-p
restored-buffer-names
skipped-buffer-names
msg)
(cl-labels
((msg-append (s) (setq msg (concat msg "; " s))))
;; Bookmark already loaded in another tab?
(when abm
;; Throws :abort
(let ((duplicate-policy (bufferlo--bookmark-get-duplicate-policy
bookmark-name "tab"
bufferlo-bookmark-tab-duplicate-policy
'load
embedded-tab)))
(pcase duplicate-policy
('allow)
('clear
(setq bookmark-name nil))
('clear-warn
(setq bookmark-name nil)
(msg-append "cleared tab bookmark"))
('ignore
(throw :abort t))
('raise
(bufferlo--bookmark-raise abm)
(throw :abort t)))))
;; Bookmark not loaded as part of a frame bookmark or in a set?
;; Frame and set handlers manage tabs, so we don't do it here.
(unless (or embedded-tab bufferlo--bookmark-set-loading)
;; Handle an independent tab bookmark on a frame with an
;; active frame bookmark. Do this first, before a new tab is
;; created.
(when (and bookmark-name
(frame-parameter nil 'bufferlo-bookmark-frame-name))
;; Throws :abort
(let ((clear-policy (bufferlo--bookmark-tab-get-clear-policy 'load)))
(pcase clear-policy
('clear
(setq disconnect-tbm-p t))
('clear-warn
(setq disconnect-tbm-p t)
(msg-append "cleared tab bookmark")))))
;; Replace current tab or create new tab?
;; Throws :abort
(let ((replace-policy (bufferlo--bookmark-tab-get-replace-policy)))
(pcase replace-policy
('replace)
('new
(unless (consp current-prefix-arg) ; user new tab suppression
(let ((tab-bar-new-tab-choice t))
(tab-bar-new-tab-to)
(let ((current-tab (cdr (bufferlo--current-tab)))
(tab-explicit-name (alist-get 'tab-explicit-name bookmark))
(tab-group (alist-get 'tab-group bookmark)))
(when tab-explicit-name
(setf (alist-get 'name current-tab) tab-explicit-name)
(setf (alist-get 'explicit-name current-tab) t))
(when tab-group
(tab-bar-change-tab-group tab-group)))))))))
;; Do the real work: restore the tab.
;; NOTE: No :abort throws after this point.
(let* ((ws (copy-tree (alist-get 'window bookmark)))
(dummy (generate-new-buffer " *bufferlo dummy buffer*"))
(restore (lambda (bm)
(let ((orig-name (car bm))
(record (cadr bm))
(restore-failed))
(set-buffer dummy)
;; Test if bookmark-handler did not complain...
(setq restore-failed
(condition-case err
(progn
(funcall (or (bookmark-get-handler record)
'bookmark-default-handler)
record)
(run-hooks 'bookmark-after-jump-hook)
nil)
(error
(message "Bufferlo bookmark: Could not restore %s (error %s)"
orig-name err)
t)))
;; ...then test that the buffer actually changed.
(setq restore-failed (or restore-failed
(eq (current-buffer) dummy)))
(if restore-failed
(progn
(bufferlo--bookmark-insert-placeholer orig-name)
(push orig-name skipped-buffer-names))
(push orig-name restored-buffer-names))
(unless (eq (current-buffer) dummy)
;; Return a list of (cons <string> <buffer>).
;; The buffer may be renamed later (by uniquify).
;; Using the buffer name directly would not
;; account for this!
(cons orig-name (current-buffer))))))
(renamed (mapcar restore (alist-get 'buffer-bookmarks bookmark)))
(replace-renamed (lambda (b)
(if-let* ((replace
(assoc b renamed)))
(cdr replace) b)))
(bm-buffer-list (mapcar replace-renamed
(alist-get 'buffer-list bookmark)))
;; Some of the items may already be buffers after renaming.
;; Others are still buffer names (strings). These items had no
;; bookmark associated with them.
(bm-buffer-list (seq-filter #'get-buffer bm-buffer-list))
(bm-buffer-list (mapcar #'get-buffer bm-buffer-list)))
(kill-buffer dummy)
;; Note that we replace buffer names with buffers in ws.
;; `window-state-put' accepts this.
(bufferlo--ws-replace-buffer-names ws renamed)
;; Restore tab name
(when (or embedded-tab bufferlo-bookmark-tab-restore-explicit-name)
(when-let* ((tab-name (alist-get 'tab-explicit-name bookmark)))
(tab-bar-rename-tab tab-name)))
;; Restore tab group
(when (or (eq bufferlo-bookmark-restore-tab-groups t)
(and embedded-tab (eq bufferlo-bookmark-restore-tab-groups 'frames))
(and (not embedded-tab) (eq bufferlo-bookmark-restore-tab-groups 'tabs)))
(when-let* ((tab-group (alist-get 'tab-group bookmark)))
(tab-bar-change-tab-group tab-group)))
;; We do the following to work around two problems with
;; bookmark--jump-via. In older versions, when called
;; interactively and not through bufferlo commands, it calls a
;; display-function which could interfere with
;; window-state-put.
;;
;; In Emacs 31, bookmark--jump-via wraps the bookmark-handler
;; call with save-window-excursion which restores the
;; window-configuration after we've just restored the one from
;; the bookmark. We let bookmark--jump-via be evil and defer
;; window-state-put until after bookmark--jump-via is done.
(let ((bm-after-jump-hook-sym (gensym "bufferlo-bm-after-jump-"))
(frame (selected-frame))
(tab-number (1+ (tab-bar--current-tab-index)))
(buffer (current-buffer)))
(fset bm-after-jump-hook-sym
(lambda ()
(remove-hook 'bookmark-after-jump-hook bm-after-jump-hook-sym)
(with-selected-frame frame ; defensive
(let ((tab-bar-tab-post-select-functions))
(tab-bar-select-tab tab-number) ; defensive
(window-state-put ws (frame-root-window) 'safe)
(set-frame-parameter nil 'buffer-list bm-buffer-list)
(set-frame-parameter nil 'buried-buffer-list nil)
(setf (alist-get 'bufferlo-bookmark-tab-name
(cdr (bufferlo--current-tab)))
(unless disconnect-tbm-p bookmark-name))
(sit-for 0)
(run-hook-with-args
'bufferlo-bookmark-tab-handler-functions
bookmark-name
(unless disconnect-tbm-p bookmark-name)
(bufferlo--current-tab)
restored-buffer-names
skipped-buffer-names)
buffer))))
(add-hook 'bookmark-after-jump-hook bm-after-jump-hook-sym -99)
(when not-jump
(run-hooks 'bookmark-after-jump-hook))))
;; Log message
(unless (or no-message bufferlo--bookmark-handler-no-message)
(message "Restored bufferlo tab bookmark%s%s%s%s"
(if orig-bookmark-name (format ": %s" orig-bookmark-name) "")
(or msg "")
(if restored-buffer-names
(format " (%s)"
(mapconcat #'identity restored-buffer-names ", ")) "")
(if skipped-buffer-names
(format " (skipped: %s)"
(mapconcat #'identity skipped-buffer-names ", ")) "")))
;; Explicitly return success; abort returns non-nil
nil))))
;; We use a short name here as bookmark-bmenu-list hard codes width of 8 chars
(put #'bufferlo--bookmark-tab-handler 'bookmark-handler-type "B-Tab")
(put #'bufferlo--bookmark-tab-handler 'bookmark-inhibit 'insert)
(defun bufferlo--bookmark-frame-make ()
"Make a bufferlo frame bookmark record for the current frame."
(let ((tabs (funcall tab-bar-tabs-function))
(orig-tab (1+ (tab-bar--current-tab-index)))
(tab-bar-tab-post-select-functions)
tabs-to-bookmark)
(dotimes (i (length tabs))
(tab-bar-select-tab (1+ i))
(let* ((curr (alist-get 'current-tab tabs))
(name (alist-get 'name curr))
(explicit-name (alist-get 'explicit-name curr))
(tbm (bufferlo--bookmark-tab-make)))
(if explicit-name
(push (cons 'tab-name name) tbm)
(push (cons 'tab-name nil) tbm))
(push tbm tabs-to-bookmark)))
(tab-bar-select-tab orig-tab)
`((tabs . ,(reverse tabs-to-bookmark))
(current . ,orig-tab)
(bufferlo--frame-name . ,(when bufferlo-bookmark-frame-persist-frame-name
(frame-parameter nil 'name)))
(bufferlo--frame-geometry . ,(funcall bufferlo-frame-geometry-function
(selected-frame)))
(handler . ,#'bufferlo--bookmark-frame-handler))))
(defun bufferlo--bookmark-frame-get-load-policy ()
"Get the load policy for frame bookmarks.
Prompt if `bufferlo-bookmark-frame-load-policy' is set to \\='prompt.
This functions throws :abort when the user quits."
(if (not (eq bufferlo-bookmark-frame-load-policy 'prompt))
bufferlo-bookmark-frame-load-policy
(pcase (with-local-quit
(read-answer
(concat
"Current frame already bookmarked: "
"load and retain Current, Replace with new, Merge with existing ")
'(("current" ?c "Replace frame, retain the current bookmark")
("replace" ?r "Replace frame, adopt the loaded bookmark")
("merge" ?m "Merge the new tab content with the existing bookmark")
("help" ?h "Help")
("quit" ?q "Quit to abort"))))
("current" 'replace-frame-retain-current-bookmark)
("replace" 'replace-frame-adopt-loaded-bookmark)
("merge" 'merge)
(_ (throw :abort t)))))
(defun bufferlo--bookmark-frame-handler (bookmark &optional _not-jump no-message)
"Handle bufferlo frame bookmark.
The argument BOOKMARK is the to-be restored frame bookmark record
created via `bufferlo--bookmark-frame-make'.
The optional argument NO-MESSAGE inhibits the message after successfully
restoring the bookmark.
NOT-JUMP should be non-nil when called directly vs. from `bookmark-jump'.
Returns nil on success, non-nil on abort."
(catch :abort
(let* ((bookmark-name (bookmark-name-from-full-record bookmark))
(abm (assoc bookmark-name (bufferlo--active-bookmarks)))
(fbm (frame-parameter nil 'bufferlo-bookmark-frame-name))
(new-frame-p (and (not bufferlo--bookmark-set-loading)
(or
(eq bufferlo-bookmark-frame-load-make-frame
'restore-geometry)
(eq bufferlo-bookmark-frame-load-make-frame
t))
;; User make-frame suppression
(not (consp current-prefix-arg))
;; make-frame implied by functions like
;; `bookmark-jump-other-frame'
(not pop-up-frames)))
duplicate-policy
load-policy
msg)
(cl-labels
((msg-append (s) (setq msg (concat msg "; " s))))
;; Bookmark already loaded in another frame?
(when abm
(setq duplicate-policy (bufferlo--bookmark-get-duplicate-policy
bookmark-name "frame"
bufferlo-bookmark-frame-duplicate-policy 'load))
(pcase duplicate-policy
('ignore
(throw :abort t))
('raise
(bufferlo--bookmark-raise abm)
(throw :abort t))))
;; If new frame, no conflict; go with the bookmark's name.
(if new-frame-p
(setq fbm bookmark-name)
;; No currently active bookmark in the frame?
(if (not fbm)
;; Set active bookmark
(setq fbm bookmark-name)
;; Handle existing bookmark according to the load policy
(setq load-policy (bufferlo--bookmark-frame-get-load-policy))
(pcase load-policy
('disallow-replace
;; Allow reloads of existing bookmark
(when (not (equal fbm bookmark-name))
(unless no-message
(message "Frame already bookmarked as %s; not loaded." fbm))
(throw :abort t)))
('replace-frame-retain-current-bookmark
(msg-append (format "retained existing bookmark %s." fbm)))
('replace-frame-adopt-loaded-bookmark
(msg-append (format "adopted loaded bookmark %s." fbm))
(setq fbm bookmark-name))
('merge
(msg-append (format "merged tabs from bookmark %s."
bookmark-name))))))
;; Do the real work with the target frame selected
;; (current or newly created)
;; NOTE: No :abort throws after this point
(bufferlo--with-temp-buffer
(let ((frame (if new-frame-p
(bufferlo--make-frame
(eq bufferlo-bookmark-frame-load-make-frame
'restore-geometry))
(selected-frame))))
(with-selected-frame frame
;; Restore name
(when bufferlo-bookmark-frame-persist-frame-name
(when-let* ((frame-name (alist-get 'bufferlo--frame-name bookmark)))
(set-frame-name frame-name)))
;; Restore geometry
(when (and (display-graphic-p)
(or (and new-frame-p
(eq bufferlo-bookmark-frame-load-make-frame
'restore-geometry))
(and (not new-frame-p)
(eq bufferlo-bookmark-frame-load-make-frame
'reuse-restore-geometry))))
(when-let* ((fg (alist-get 'bufferlo--frame-geometry bookmark)))
(funcall bufferlo-set-frame-geometry-function fg)))
;; Clear existing tabs unless merging
(unless (eq load-policy 'merge)
(tab-bar-tabs-set nil))
;; Load tabs
(let ((first (if (eq load-policy 'merge) nil t))
(tab-bar-new-tab-choice t))
(mapc
(lambda (tbm)
(let ((orig-buffer (current-buffer)))
(unless first
(tab-bar-new-tab-to))
;; NOTE: This is defensive code. There should be no aborts
;; for an embedded-tab and bufferlo--bookmark-tab-handler
;; disallows raise for an embedded-tab to avoid selected
;; frame/tab state issues.. Embedded tabs in frames carry
;; no bookmarks of their own, so no duplicates are possible
;; and other user prompts are bypassed.
;;
;; Handler abort is non-nil.
(if (bufferlo--bookmark-tab-handler tbm 'not-jump
'no-message 'embedded-tab)
(if first
(let ((switch-to-buffer-obey-display-actions))
(switch-to-buffer orig-buffer 'no-record
'force-same-window))
(let (tab-bar-tab-prevent-close-functions)
(tab-bar-close-tab))))
(setq first nil)))
(alist-get 'tabs bookmark)))
;; NOTE: We might not find a 'current tab if the tab handler aborts
;; (should never happen).
(tab-bar-select-tab (alist-get 'current bookmark))
;; Handle duplicate frame bookmark
(when abm
(pcase duplicate-policy
;; Do nothing for 'allow or nil
('clear
(setq fbm nil))
('clear-warn
(setq fbm nil)
(msg-append "cleared frame bookmark"))))
(set-frame-parameter nil 'bufferlo-bookmark-frame-name fbm))
;; Select and raise the restored frame outside the context
;; of with-selected-frame
(select-frame-set-input-focus frame)
(run-hook-with-args
'bufferlo-bookmark-frame-handler-functions
bookmark-name
fbm
(or new-frame-p bufferlo--bookmark-set-loading)
frame)))
(unless (or new-frame-p bufferlo--bookmark-set-loading pop-up-frames)
;; Switch to the to-be-selected buffer in the current frame.
;; This is a workaround for bookmark-jump if called with display-func
;; set to something like pop-to-buffer-same-window (the default).
;; Without this, the previously selected buffer will leak into the
;; loaded frame bookmark.
(switch-to-buffer (window-buffer (frame-selected-window nil))))
;; Log message
(unless (or no-message bufferlo--bookmark-handler-no-message)
(message "Restored bufferlo frame bookmark%s%s"
(if bookmark-name (format ": %s" bookmark-name) "")
(or msg "")))
;; Explicitly return success; abort returns non-nil
nil))))
;; We use a short name here as bookmark-bmenu-list hard codes width of 8 chars
(put #'bufferlo--bookmark-frame-handler 'bookmark-handler-type "B-Frame")
(put #'bufferlo--bookmark-frame-handler 'bookmark-inhibit 'insert)
(defun bufferlo--bookmark-set-location (bookmark-name-or-record &optional location)
"Set the location of BOOKMARK-NAME-OR-RECORD to LOCATION or \\=\"\", if nil."
(bookmark-prop-set bookmark-name-or-record 'location (or location ""))
bookmark-name-or-record)
(defun bufferlo--bookmark-completion-table (candidates &optional no-sort)
"Completing read helper.
CANDIDATES should be a list from which to select candidates.
When non-nil, NO-SORT uses the natural order of the CANDIDATES list."
(lambda (string pred action)
(cond
((eq action 'metadata)
`(metadata . ,(cons '(category . bookmark)
(when no-sort
'((display-sort-function . identity))))))
(t
(complete-with-action action candidates string pred)))))
(defun bufferlo--bookmark-completing-read (prompt candidates &optional no-sort)
"Common bufferlo bookmark `completing-read'.
PROMPT is the prompt text ending with a space.
CANDIDATES are the prompt options to select.
When non-nil, NO-SORT uses the natural order of the CANDIDATES list."
(completing-read prompt
(bufferlo--bookmark-completion-table candidates no-sort)
nil 'require-match nil 'bufferlo-bookmark-history))
(defun bufferlo--bookmark-completing-read-multiple (prompt candidates &optional no-sort)
"Common bufferlo bookmark `completing-read-multiple'.
PROMPT is the prompt text ending with a space.
CANDIDATES are the prompt options to select.
When non-nil, NO-SORT uses the natural order of the CANDIDATES list."
(let* ((comps
(delete-dups
(completing-read-multiple
prompt
(bufferlo--bookmark-completion-table candidates no-sort)
nil 'require-match nil 'bufferlo-bookmark-history)))
(comps (seq-intersection
(mapcar (lambda (x) (substring-no-properties x)) comps)
candidates)))
comps))
;; filter out vs. frameset-persistent-filter-alist
(defvar bufferlo--frameset-save-filter
'(;; bufferlo parameters
bufferlo-bookmark-frame-name
;; Emacs parameters
alpha
alpha-background
auto-lower
auto-raise
background-color
background-mode
border-color
border-width
bottom-divider-width
buffer-predicate
child-frame-border-width
cursor-color
cursor-type
display
display-type
environment
explicit-name
font
font-parameter
foreground-color
horizontal-scroll-bars
icon-name
icon-type
inhibit-double-buffering
internal-border-width
;; last-focus-update
left-fringe
line-spacing
menu-bar-lines
minibuffer
modeline
mouse-color
;; name ; ???
no-accept-focus
no-focus-on-map
no-special-glyphs
ns-appearance
ns-transparent-titlebar
outer-window-id
override-redirect
right-divider-width
right-fringe
screen-gamma
scroll-bar-background
scroll-bar-foreground
scroll-bar-height
scroll-bar-width
shaded
skip-taskbar
sticky
tabs
tab-bar-lines
title
tool-bar-lines
tool-bar-position
tty
tty-type
undecorated
unsplittable
use-frame-synchronization
vertical-scroll-bars
visibility
wait-for-wm
z-group))
(defvar bufferlo--frameset-restore-filter
'(;; Emacs parameters
GUI:bottom
GUI:font
GUI:fullscreen
GUI:height
GUI:left
GUI:right
GUI:top
GUI:width
bottom
fontsize
frameset--text-pixel-height
frameset--text-pixel-width
fullscreen
height
left
right
top
width))
(defun bufferlo-frame-geometry-default (frame)
"Produce an alist for FRAME pixel-level geometry.
The alist is of the form:
((fullscreen . value) ; nil fullwidth fullheight fullboth maximized, etc.
(left . pixels)
(top . pixels)
(width . pixels)
(height . pixels))
Return nil if no pixel-level geometry is available; for example, if the
display is a tty."
(if (display-graphic-p frame)
`((fullscreen . ,(frame-parameter frame 'fullscreen))
(left . ,(frame-parameter frame 'left))
(top . ,(frame-parameter frame 'top))
(width . ,(frame-text-width frame))
(height .,(frame-text-height frame)))
nil))
(defun bufferlo-set-frame-geometry-default (frame-geometry &optional frame)
"Set FRAME-GEOMETRY as produced by `bufferlo-frame-geometry-default'.
Set geometry for FRAME or the current frame, if nil.
If `frame-geometry' contains a non-nil \\='fullscreen parameter, it is
applied after the pixelwise geometry. This accommodates \\='fullwidth
and \\='fullheight by first setting the respective height or width."
;; Some window managers need an extra display cycle for frame
;; changes to take effect from Emacs's perspective, so we add
;; needed sit-for calls.
(setq frame (or frame (selected-frame)))
(let-alist frame-geometry
;; The sleeps (sleep-for) wait for window managers to catch up.
;; Be defensive in case the geometry was stored from a tty.
(when (and .left .top .width .height)
(let ((frame-resize-pixelwise t)
(frame-inhibit-implied-resize t))
;; There's a bug in Emacs where if fullscreen is non-nil and an
;; explicit location and/or size is specified, fullscreen does not
;; reset to nil. We do that first.
(set-frame-parameter frame 'fullscreen nil)
(set-frame-position frame .left .top)
(sleep-for bufferlo-frame-sleep-for)
;; Clamp to the current display size to handle a too large frame.
(set-frame-size frame
(min .width (display-pixel-width))
(min .height (display-pixel-height))
'pixelwise)
(set-frame-parameter frame 'fullscreen .fullscreen)
(sleep-for bufferlo-frame-sleep-for)))))
(defvar bufferlo--active-sets nil
"Global active bufferlo sets.
This is an alist of the form:
((set-name ('bufferlo--bookmark-names . name-list))).")
(defun bufferlo--bookmark-set-make (active-bookmark-names tabsets frameset)
"Make a bufferlo bookmark set.
ACTIVE-BOOKMARK-NAMES defines the bookmarks for the stored
bookmark set.
TABSETS is a list of tab bookmark names organized in sub-lists
representing logical container frames.
FRAMESET is a bufferlo-filtered `frameset'."
(let ((bookmark-record (bookmark-make-record-default t t 0))) ; (&optional no-file no-context posn)
(bookmark-prop-set bookmark-record
'bufferlo-bookmark-names
(if (consp active-bookmark-names)
active-bookmark-names
(list active-bookmark-names)))
(bookmark-prop-set bookmark-record
'bufferlo-tabsets (prin1-to-string tabsets))
(bookmark-prop-set bookmark-record
'bufferlo-frameset (prin1-to-string frameset))
(bookmark-prop-set bookmark-record
'handler #'bufferlo--bookmark-set-handler)
bookmark-record))
(defun bufferlo-frameset-restore-parameters-default ()
"Function to create parameters for `frameset-restore', which see."
(list :reuse-frames nil
:force-display t
:force-onscreen (display-graphic-p)
:cleanup-frames nil))
(defun bufferlo-frameset-restore-default (frameset)
"Invoke `frameset-restore' with FRAMESET, which see."
(let ((params (funcall bufferlo-frameset-restore-parameters-function))
;; frameset-restore checks for fullscreen in frame parameters
;; and its handling is wonky and the restore filter has no
;; effect, so we remove it locally.
(default-frame-alist (assq-delete-all 'fullscreen
(copy-tree default-frame-alist))))
(with-temp-buffer
(ignore-errors
;; Sadly, frameset-restore returns neither a status nor a list
;; of restored frames.
(frameset-restore
frameset
:filters
(when (memq bufferlo-frameset-restore-geometry '(bufferlo nil))
(let ((filtered-alist
(copy-tree frameset-persistent-filter-alist)))
(mapc (lambda (sym) (setf (alist-get sym filtered-alist) :never))
(seq-union bufferlo--frameset-restore-filter
bufferlo-frameset-restore-filter))
filtered-alist))
:reuse-frames (plist-get params :reuse-frames)
:force-display (plist-get params :force-display)
:force-onscreen (plist-get params :force-onscreen)
:cleanup-frames (plist-get params :cleanup-frames))))))
(defun bufferlo--bookmark-set-handler (bookmark-record &optional _not-jump no-message)
"Handle bufferlo bookmark set.
The argument BOOKMARK-RECORD is the to-be restored bookmark set created
via `bufferlo--bookmark-set-make'. The optional argument NO-MESSAGE
inhibits the message after successfully restoring the bookmark. The
list of restored bookmarks indicates bookmarks that were already active
with an asterisk.
NOT-JUMP should be non-nil when called directly vs. from `bookmark-jump'.
Returns nil on success, non-nil on abort."
(let* ((bookmark-name (bookmark-name-from-full-record bookmark-record))
(bufferlo-bookmark-names (bookmark-prop-get bookmark-record
'bufferlo-bookmark-names))
(abms (bufferlo--active-bookmarks))
(abm-names (mapcar #'car abms))
(already-abm-names (seq-intersection
bufferlo-bookmark-names
abm-names))
ignore-already-active
(bufferlo--bookmark-set-loading t)
(tabsets-str (bookmark-prop-get bookmark-record 'bufferlo-tabsets))
(frameset-str (bookmark-prop-get bookmark-record 'bufferlo-frameset)))
(when (assoc bookmark-name bufferlo--active-sets)
(user-error "Bufferlo set `%s' is already active" bookmark-name))
(unless (readablep tabsets-str)
(error "Bufferlo bookmark set %s: unreadable tabsets"
bookmark-name))
(unless (readablep frameset-str)
(error "Bufferlo bookmark set %s: unreadable frameset"
bookmark-name))
(when already-abm-names
(pcase bufferlo-set-restore-ignore-already-active
('prompt
(setq ignore-already-active
(y-or-n-p "Ignore all already-active bookmarks? ")))
('ignore
(setq ignore-already-active t))
(_ (setq ignore-already-active nil))))
;; Restore tabsets (tabsets can be nil despite readablep)
(when-let* ((tabsets (car (read-from-string tabsets-str)))
(first-tab-frame t))
(bufferlo--with-temp-buffer
(dolist (tab-group tabsets)
(when (or (not first-tab-frame)
(and first-tab-frame
(not bufferlo-set-restore-tabs-reuse-init-frame)))
(select-frame (bufferlo--make-frame
(eq bufferlo-set-restore-tabs-reuse-init-frame
'reuse-reset-geometry))))
(when-let* ((fg (alist-get 'bufferlo--frame-geometry tab-group)))
(when (and
(display-graphic-p)
(memq bufferlo-set-restore-geometry-policy '(all tab-frames))
(or (not first-tab-frame)
(and first-tab-frame
(eq bufferlo-set-restore-tabs-reuse-init-frame
'reuse-reset-geometry))))
(funcall bufferlo-set-frame-geometry-function fg)))
(when-let* ((tbm-names (alist-get 'bufferlo--tbms tab-group)))
(let ((bufferlo-bookmark-tab-replace-policy 'replace)
(tab-bar-new-tab-choice t)
(first-tab
(or (not first-tab-frame)
(and first-tab-frame
(not bufferlo-set-restore-tabs-reuse-init-frame)))))
(dolist (tbm-name tbm-names)
(unless (and ignore-already-active
(member tbm-name already-abm-names))
(when-let* ((tbm-bookmark-record
(bufferlo--bookmark-get-bookmark tbm-name)))
(let ((orig-buffer (current-buffer)))
(unless first-tab
(tab-bar-new-tab-to))
;; NOTE: bufferlo--bookmark-tab-handler disallows raise for
;; embedded-tab to avoid selected frame/tab state issues
;; Handler abort is non-nil.
(when (bufferlo--bookmark-tab-handler tbm-bookmark-record
'not-jump)
(if first-tab
(let ((switch-to-buffer-obey-display-actions))
(switch-to-buffer orig-buffer 'no-record
'force-same-window))
(let (tab-bar-tab-prevent-close-functions)
(tab-bar-close-tab))))))
(setq first-tab nil)))))
(setq first-tab-frame nil)))
(select-frame-set-input-focus (selected-frame)))
;; Restore framesets (framesets can be nil despite readablep)
(when-let* ((frameset (car (read-from-string frameset-str))))
(unless (frameset-valid-p frameset)
(error "Bufferlo bookmark set %s: invalid frameset"
bookmark-name))
(funcall bufferlo-frameset-restore-function frameset)
(dolist (frame (frame-list))
(with-selected-frame frame
;; 'bufferlo--frame-to-restore is a hint from
;; `bufferlo-frameset-restore-default'
(when (frame-parameter nil 'bufferlo--frame-to-restore)
(when-let* ((fbm-name (frame-parameter
nil 'bufferlo--bookmark-frame-name))
(fbm-bookmark-record (bufferlo--bookmark-get-bookmark
fbm-name)))
(if (and ignore-already-active
(member fbm-name already-abm-names))
(delete-frame)
(let ((bufferlo-bookmark-frame-load-policy
'replace-frame-adopt-loaded-bookmark)
(bufferlo--bookmark-handler-no-message t))
;; NOTE: bufferlo--bookmark-frame-handler disallows raise
;; for frames in sets to avoid selected frame/tab state issues
;; Handler abort is non-nil.
(if (bufferlo--bookmark-frame-handler fbm-bookmark-record
'not-jump)
(delete-frame)
(when (and
(display-graphic-p frame)
(memq bufferlo-set-restore-geometry-policy
'(all frames)))
(when-let* ((fg (frame-parameter
nil 'bufferlo--frame-geometry)))
(funcall bufferlo-set-frame-geometry-function fg)))
(set-frame-parameter nil 'bufferlo--frame-to-restore nil))
(select-frame-set-input-focus (selected-frame)))))))))
;; Add the set to the active list
(push `(,bookmark-name (bufferlo-bookmark-names . ,bufferlo-bookmark-names))
bufferlo--active-sets)
(run-hook-with-args
'bufferlo-bookmark-set-handler-functions
bookmark-name
bufferlo-bookmark-names)
(unless (or no-message bufferlo--bookmark-handler-no-message)
;; Indicate bookmarks that were already active with an asterisk
(message "Restored bufferlo bookmark set %s (%s)"
bookmark-name
(mapconcat (lambda (x)
(if (member x already-abm-names)
(concat "*" x)
x))
bufferlo-bookmark-names
", ")))
;; Explicitly return success; abort returns non-nil
nil))
;; We use a short name here as bookmark-bmenu-list hard codes width of 8 chars
(put #'bufferlo--bookmark-set-handler 'bookmark-handler-type "B-Set")
(put #'bufferlo--bookmark-set-handler 'bookmark-inhibit 'insert)
(defun bufferlo--set-save (bookmark-name active-bookmark-names active-bookmarks
&optional no-overwrite no-message msg)
"Save a bufferlo bookmark set for the specified active bookmarks.
Store the set in BOOKMARK-NAME for the named bookmarks in
ACTIVE-BOOKMARK-NAMES represented in ACTIVE-BOOKMARKS.
Frame bookmarks are stored with their geometry for optional
restoration.
Tab bookmarks are stored in groups associated with their current
frame. New frames will be created to hold tab bookmarks in the
same grouping. Order may not be preserved. Tab frame geometry is
stored for optional restoration.
If NO-OVERWRITE is non-nil, record the new bookmark without throwing
away the old one. NO-MESSAGE inhibits the save status message. If MSG
is non-nil, it is added to the save message."
(let* ((abms (seq-filter
(lambda (x) (member (car x) active-bookmark-names))
active-bookmarks))
(tbms (seq-filter
(lambda (x) (eq (alist-get 'type (cadr x)) 'tbm))
abms))
(tbm-frame-groups (seq-group-by
(lambda (x) (alist-get 'frame (cadr x)))
tbms))
(fbms (seq-filter
(lambda (x) (eq (alist-get 'type (cadr x)) 'fbm))
abms))
(fbm-frames (mapcar (lambda (x) (alist-get 'frame (cadr x))) fbms))
(tabsets)
(frameset))
(when (= (length abms) 0)
(user-error "Specify at least one active bufferlo bookmark"))
(setq tabsets
(mapcar (lambda (group)
(let ((tbm-frame (car group))
(tbm-names (nreverse (mapcar #'car (cdr group)))))
`((bufferlo--frame-geometry
. ,(funcall bufferlo-frame-geometry-function tbm-frame))
(bufferlo--tbms . ,tbm-names))))
tbm-frame-groups))
(when fbm-frames
;; Set a flag we can use to identify restored frames (this
;; is removed in the handler during frame restoration). Save
;; frame geometries for more accurate restoration than
;; frameset-restore provides.
;;
;; Squirrel away 'bufferlo-bookmark-frame-name which we ask
;; frameset-save to filter out to avoid restored frames
;; being considered bookmarked as they need duplicate
;; detection.
(dolist (frame fbm-frames)
(set-frame-parameter frame 'bufferlo--frame-to-restore t)
(set-frame-parameter frame 'bufferlo--frame-geometry
(funcall bufferlo-frame-geometry-function
frame))
(set-frame-parameter frame 'bufferlo--bookmark-frame-name
(frame-parameter frame
'bufferlo-bookmark-frame-name)))
;; frameset-save squirrels away width/height text-pixels iff
;; fullscreen is not nil and frame-resize-pixelwise is t.
(let ((frame-resize-pixelwise t))
(setq frameset
(frameset-save
fbm-frames
:app 'bufferlo
:name bookmark-name
:predicate (lambda (x)
(not (frame-parameter x 'parent-frame)))
:filters
(let ((filtered-alist
(copy-tree frameset-persistent-filter-alist)))
(mapc (lambda (sym)
(setf (alist-get sym filtered-alist) :never))
(seq-union bufferlo--frameset-save-filter
bufferlo-frameset-save-filter))
filtered-alist)))))
(bookmark-store bookmark-name
(bufferlo--bookmark-set-location
(bufferlo--bookmark-set-make
active-bookmark-names tabsets frameset))
no-overwrite)
(unless no-message
(message "Saved bookmark set `%s' containing: %s%s"
bookmark-name
(mapconcat #'identity active-bookmark-names ", ")
(if msg msg "")))))
(defun bufferlo-set-save-interactive (bookmark-name
&optional no-overwrite no-message)
"Save a bufferlo bookmark set for selected active bookmarks.
Save the bookmark set under BOOKMARK-NAME.
Tab bookmarks are grouped based on their shared frame along with
the frame's geometry.
Frame bookmarks represent themselves.
If NO-OVERWRITE is non-nil, record the new bookmark without
throwing away the old one. NO-MESSAGE inhibits the save status
message.
Warn if this bookmark will overwrite a tab or frame bookmark, and clear
them, if active, when overwriting."
(interactive
(list (completing-read
"Save bufferlo bookmark set as: "
(bufferlo--bookmark-get-names #'bufferlo--bookmark-set-handler)
nil nil nil 'bufferlo-bookmark-set-history nil)))
(bufferlo--warn)
(catch :abort
(let* ((tab-bm-names (bufferlo--bookmark-get-names
#'bufferlo--bookmark-tab-handler))
(frame-bm-names (bufferlo--bookmark-get-names
#'bufferlo--bookmark-frame-handler))
(is-tab-bm (member bookmark-name tab-bm-names))
(is-frame-bm (member bookmark-name frame-bm-names))
clear-active-tab-bm
clear-active-frame-bm)
(cond
;; Overwriting a tab bookmark of the same name? If we proceed, and the
;; tab bookmark is active, mark it to deactivate, leaving its buffers
;; intact, and after we know the replacement bookmark will be saved.
(is-tab-bm
(if (y-or-n-p (format "Overwrite the bufferlo tab bookmark `%s'? "
bookmark-name))
(setq clear-active-tab-bm
(assoc bookmark-name (bufferlo--active-bookmarks nil 'tbm)))
(throw :abort t)))
;; Overwriting a frame bookmark of the same name? If so, and the frame
;; bookmark is active, mark it to deactivate, leaving its buffers
;; intact, and after we know the replacement bookmark will be saved.
(is-frame-bm
(if (y-or-n-p (format "Overwrite the bufferlo frame bookmark `%s'? "
bookmark-name))
(setq clear-active-frame-bm
(assoc bookmark-name (bufferlo--active-bookmarks nil 'fbm)))
(throw :abort t))))
;; Confirm if the about-to-be set bookmark is contained in a set, as it
;; will not be loadable once converted. (If this is a set, it will not
;; be in a set.)
(when-let* ((containing-sets (bufferlo--sets-containing-bookmark
bookmark-name)))
(unless (y-or-n-p (format
"Warning: `%s' is contained in set(s) %s and will be unloadable; proceed? "
bookmark-name (mapconcat #'identity containing-sets ", ")))
(throw :abort t)))
(let* ((abms (bufferlo--active-bookmarks))
(abm-names (mapcar #'car abms))
;; If we are about to overwrite a tab or frame bm, remove that
;; name from the list of bookmarks available for the new set.
(abm-names (if (or is-tab-bm
is-frame-bm)
(remove bookmark-name abm-names)
abm-names))
(comps (bufferlo--bookmark-completing-read-multiple
(format "Add bookmark(s) to %s: " bookmark-name) abm-names))
msg)
;; Do not create an empty bookmark set.
(when (not (length> comps 0))
(message "Select at least one bookmark to create the set")
(throw :abort t))
(cl-labels
((msg-append (s) (setq msg (concat msg "; " s))))
(cond
;; Clear the about-to-be overwritten tab bookmark.
(clear-active-tab-bm
(msg-append "cleared overwritten tab bookmark")
(bufferlo--clear-tab-bookmarks-by-name bookmark-name))
;; Clear the about-to-be overwritten frame bookmark.
(clear-active-frame-bm
(msg-append "cleared overwritten frame bookmark")
(bufferlo--clear-frame-bookmarks-by-name bookmark-name)))
;; Finally, save the bookmark, and replace the active bookmark.
(bufferlo--set-save bookmark-name comps abms no-overwrite no-message msg)
(setq bufferlo--active-sets
(assoc-delete-all bookmark-name bufferlo--active-sets #'equal))
(push
`(,bookmark-name (bufferlo-bookmark-names . ,comps))
bufferlo--active-sets))))))
(defun bufferlo--set-get-constituents (bsets abms)
"Get the constituents of the given `bookmark-sets' from the list of bookmarks.
BSETS is a list of the requested sets and ABMS is a list of all bookmarks to
consider (usually all active bookmarks)."
(let* ((abm-names (mapcar #'car abms))
(abm-names (seq-mapcat
(lambda (set-name)
(seq-intersection
(alist-get 'bufferlo-bookmark-names
(assoc set-name bufferlo--active-sets))
abm-names))
bsets)))
(seq-uniq abm-names)))
(defun bufferlo--sets-containing-bookmark (bookmark-name)
"Return a list of set bookmark names that contain BOOKMARK-NAME."
(let (sets)
(dolist (bookmark-set-name
(bufferlo--bookmark-get-names #'bufferlo--bookmark-set-handler))
(when-let* ((bookmark-record
(bufferlo--bookmark-get-bookmark bookmark-set-name)))
(when (member bookmark-name
(alist-get 'bufferlo-bookmark-names bookmark-record))
(push (car bookmark-record) sets))))
sets))
(defun bufferlo-set-save-current-interactive ()
"Save active constituents in selected `bookmark-sets'."
(interactive)
(bufferlo--warn)
(let* ((candidates (mapcar #'car bufferlo--active-sets))
(comps (bufferlo--bookmark-completing-read-multiple
"Select sets to save: "
candidates))
(abms (bufferlo--active-bookmarks))
(abm-names-to-save (bufferlo--set-get-constituents comps abms)))
(bufferlo--bookmarks-save abm-names-to-save abms)))
(defun bufferlo-set-add-interactive (bookmark-name)
"Select active bookmarks to add to an active bookmark set.
Amend the bookmark set BOOKMARK-NAME."
(interactive
(list (completing-read
"Bookmark set to add to: "
bufferlo--active-sets
nil nil nil 'bufferlo-bookmark-set-history nil)))
(bufferlo--warn)
(let* ((abms (bufferlo--active-bookmarks))
(abm-names
(seq-difference
(mapcar #'car abms)
(bufferlo--set-get-constituents (list bookmark-name) abms)))
(comps (bufferlo--bookmark-completing-read-multiple
(format "Add bookmark(s) to %s: " bookmark-name)
abm-names))
(bookmark-names (append
comps
(alist-get
'bufferlo-bookmark-names
(assoc bookmark-name bufferlo--active-sets)))))
(bufferlo--set-save bookmark-name bookmark-names abms)
(setf (alist-get 'bufferlo-bookmark-names
(alist-get bookmark-name
bufferlo--active-sets nil nil #'equal))
bookmark-names)))
(defun bufferlo-set-remove-interactive (bookmark-name)
"Select active bookmarks to remove from an active bookmark set.
Amend the bookmark set BOOKMARK-NAME."
(interactive
(list (completing-read
"Bookmark set to remove from: "
bufferlo--active-sets
nil nil nil 'bufferlo-bookmark-set-history nil)))
(bufferlo--warn)
(let* ((abms (bufferlo--active-bookmarks))
(abm-names
(bufferlo--set-get-constituents (list bookmark-name) abms))
(comps (bufferlo--bookmark-completing-read-multiple
(format "Remove bookmark(s) from %s: " bookmark-name)
abm-names))
(bookmark-names (seq-remove
(lambda (elt) (member elt comps))
abm-names)))
(when (= (length bookmark-names) 0)
(user-error "A bookmark set must retain at least one bookmark"))
(bufferlo--set-save bookmark-name bookmark-names abms)
(setf (alist-get 'bufferlo-bookmark-names
(alist-get bookmark-name
bufferlo--active-sets nil nil #'equal))
bookmark-names)))
(defun bufferlo-set-load-interactive ()
"Prompt for bufferlo set bookmarks to load."
(interactive)
(let ((current-prefix-arg '(64))) ; emulate C-u C-u C-u
(call-interactively 'bufferlo-bookmarks-load-interactive)))
(defun bufferlo--set-clear-all ()
"Clear all active `bookmark-sets'.
This does not close active frame and tab bookmarks."
(setq bufferlo--active-sets nil))
(defun bufferlo--set-clear (names)
"Clear active bookmarks set NAMES.
This does not close associated active frame and tab bookmarks."
(mapc (lambda (x)
(setq bufferlo--active-sets
(assoc-delete-all x bufferlo--active-sets)))
names))
(defun bufferlo-set-clear-interactive ()
"Clear the specified `bookmark-sets'.
Do not close sets' associated bookmarks or kill buffers."
(interactive)
(bufferlo--warn)
(let* ((candidates (mapcar #'car bufferlo--active-sets))
(comps (bufferlo--bookmark-completing-read-multiple
"Select sets to clear: "
candidates)))
(bufferlo--set-clear comps)))
(defun bufferlo-set-close-interactive ()
"Close the specified `bookmark-sets'.
Close each set's associated bookmarks and kill their buffers."
(interactive)
(bufferlo--warn)
(let* ((candidates (mapcar #'car bufferlo--active-sets))
(comps (bufferlo--bookmark-completing-read-multiple
"Select sets to close/kill: "
candidates))
(abms (bufferlo--active-bookmarks))
(abm-names-to-close (bufferlo--set-get-constituents comps abms)))
(bufferlo--close-active-bookmarks abm-names-to-close abms)
(bufferlo--set-clear comps)))
(defvar-keymap bufferlo--set-list-mode-map
:parent special-mode-map
"<mouse-1>" #'bufferlo--set-list-raise-bookmark-mouse
"RET" #'bufferlo--set-list-raise-bookmark-kb)
(define-derived-mode bufferlo--set-list-mode special-mode "bufferlo-set-list"
"Major mode for bufferlo set list."
(setq-local help-at-pt-display-when-idle t)
(help-at-pt-set-timer)
(cursor-intangible-mode)
(cursor-face-highlight-mode))
(defun bufferlo--set-list-raise-bookmark-mouse (event)
"Handle mouse EVENT."
(interactive "e")
(when-let* ((pos (event-start event))
(bname (get-text-property (posn-point pos) 'bookmark-name)))
(quit-window)
(bufferlo--bookmark-raise-by-name bname)))
(defun bufferlo--set-list-raise-bookmark-kb ()
"Handle keyboard event."
(interactive)
(when-let* ((bname (get-text-property (point) 'bookmark-name)))
(quit-window)
(bufferlo--bookmark-raise-by-name bname)))
(defconst bufferlo--set-list-buffer-name " *bufferlo set list*")
(defun bufferlo-set-list-interactive ()
"Enumerate the bookmarks in active `bookmark-sets'."
(interactive)
(bufferlo--warn)
(let* ((candidates (mapcar #'car bufferlo--active-sets))
(comps (bufferlo--bookmark-completing-read-multiple
"Select sets to enumerate: "
candidates)))
(let ((abms (bufferlo--active-bookmarks)))
(cl-labels
((intangible-text (&rest text)
(let* ((text (mapconcat #'identity text))
(len (length text)))
(add-text-properties 0 len
(list
'field t
'rear-nonsticky t
'front-sticky t
'intangible t
'cursor-intangible t
'inhibit-isearch t)
text)
text)))
(with-current-buffer (get-buffer-create bufferlo--set-list-buffer-name)
(let ((buffer-undo-list t))
(read-only-mode -1)
(erase-buffer))
(let ((start-point))
(insert (intangible-text
"----- Bufferlo Bookmarks Sets -----"
"\n"
"(RET or mouse-1 to raise a bookmark, q to quit)"
"\n"
"\n"))
(dolist (set-name (sort comps #'string<))
(insert (intangible-text
(format-message "Set `%s':\n" set-name)))
(unless start-point (setq start-point (1+ (point))))
(dolist (bname (sort
(alist-get 'bufferlo-bookmark-names
(assoc set-name bufferlo--active-sets))
#'string<))
(when-let* ((abm (cadr (assoc bname abms))))
(let* ((type (alist-get 'type abm))
(frame (alist-get 'frame abm))
(fname (or (frame-parameter frame 'explicit-name)
(frame-parameter frame 'name)))
(tab-number (alist-get 'tab-number abm))
(text (format " %-20s %-8s %-25s %s"
(truncate-string-to-width bname 20 nil nil t)
(alist-get type bufferlo--bookmark-type-names)
(truncate-string-to-width fname 25 nil nil t)
(if tab-number
(format "tab:%d" tab-number)
"")))
(len (length text)))
(add-text-properties 0 len
(list
'bookmark-name bname
'help-echo (format "RET or mouse-1: Raise bookmark %s" bname)
'kbd-help nil ; nil to use help-echo text
'mouse-face 'highlight
'cursor-face 'region)
text)
(insert text)
(insert (intangible-text "\n")))))
(insert (intangible-text "\n")))
(insert (intangible-text
"----- END -----"
"\n"))
(bufferlo--set-list-mode)
(goto-char start-point)
(pop-to-buffer (current-buffer) nil 'norecord)))))))
(defun bufferlo--bookmark-get-names (&rest handlers)
"Get the names of all existing bookmarks for HANDLERS."
(bookmark-maybe-load-default-file)
(mapcar
#'car
(seq-filter
(lambda (bm)
(memq (alist-get 'handler (cdr bm))
(or handlers bufferlo--bookmark-handlers)))
bookmark-alist)))
(defun bufferlo--current-tab ()
"Get the current tab record."
(tab-bar--current-tab-find))
(defun bufferlo--bookmark-tab-save (name &optional no-overwrite no-message msg)
"Save the current tab as a bookmark.
NAME is the bookmark's name. If NO-OVERWRITE is non-nil, record
the new bookmark without throwing away the old one. NO-MESSAGE
inhibits the save status message. If MSG is non-nil, it is added
to the save message.
This function operates on the current frame and its current tab."
(bookmark-store name (bufferlo--bookmark-set-location
(bufferlo--bookmark-tab-make))
no-overwrite)
(setf (alist-get 'bufferlo-bookmark-tab-name
(cdr (bufferlo--current-tab)))
name)
(unless no-message
(message "Saved bufferlo tab bookmark: %s%s" name (if msg msg ""))))
(defun bufferlo-bookmark-tab-save (name &optional no-overwrite no-message)
"Save the current tab as a bookmark.
NAME is the bookmark's name. If NO-OVERWRITE is non-nil, record
the new bookmark without throwing away the old one. NO-MESSAGE
inhibits the save status message.
This function persists the current tab's state:
The resulting bookmark stores the window configuration and the local
buffer list of the current tab. In addition, it saves the bookmark
state (not the contents) of the bookmarkable buffers in the tab's local
buffer list.
Use `bufferlo-bookmark-tab-in-bookmarked-frame-policy' to
influence how this function handles setting a tab bookmark in the
presence of a frame bookmark. Using both together is allowed, but
is not recommended.
Warn if this bookmark will overwrite a set or frame bookmark, and clear
them, if active, when overwriting."
(interactive
(list (completing-read
"Save bufferlo tab bookmark: "
(bufferlo--bookmark-get-names #'bufferlo--bookmark-tab-handler)
nil nil nil 'bufferlo-bookmark-tab-history
(alist-get 'bufferlo-bookmark-tab-name (bufferlo--current-tab)))))
(bufferlo--warn)
(catch :abort
(let* ((abm (assoc name (bufferlo--active-bookmarks)))
(tbm (alist-get 'bufferlo-bookmark-tab-name
(tab-bar--current-tab-find)))
(set-bm-names (bufferlo--bookmark-get-names
#'bufferlo--bookmark-set-handler))
(frame-bm-names (bufferlo--bookmark-get-names
#'bufferlo--bookmark-frame-handler))
(is-set-bm (member name set-bm-names))
(is-frame-bm (member name frame-bm-names))
clear-active-set-bm
clear-active-frame-bm
msg)
(cond
;; Overwriting a set bookmark of the same name? If so, and the set
;; bookmark is active, mark it to deactivate, leaving its bookmarks and
;; buffers intact, and after we know the replacement bookmark will be
;; saved.
(is-set-bm
(if (y-or-n-p (format "Overwrite the bufferlo set bookmark `%s'? " name))
(setq clear-active-set-bm
(assoc name bufferlo--active-sets))
(throw :abort t)))
;; Overwriting a frame bookmark of the same name? If so, and the frame
;; bookmark is active, mark it to deactivate, leaving its buffers
;; intact, and after we know the replacement bookmark will be saved.
(is-frame-bm
(if (y-or-n-p (format "Overwrite the bufferlo frame bookmark `%s'? " name))
(setq clear-active-frame-bm
(assoc name (bufferlo--active-bookmarks nil 'fbm)))
(throw :abort t))))
(cl-labels
((msg-append (s) (setq msg (concat msg "; " s))))
;; Only check policies when the bm is a tab bm, and not already
;; associated with this tab.
(unless (or is-set-bm
is-frame-bm
(and
tbm
(equal tbm (car abm))))
;; Bookmark already loaded in another tab?
(when abm
(pcase (bufferlo--bookmark-get-duplicate-policy
name "tab" bufferlo-bookmark-tab-duplicate-policy 'save)
('allow)
('clear
(bufferlo--clear-tab-bookmarks-by-name name))
('clear-warn
(bufferlo--clear-tab-bookmarks-by-name name)
(msg-append "cleared duplicate active tab bookmark"))
('ignore
(throw :abort t))
('raise
(bufferlo--bookmark-raise abm)
(throw :abort t))))
;; Tab inside a frame bookmark?
(when (frame-parameter nil 'bufferlo-bookmark-frame-name)
(pcase (bufferlo--bookmark-tab-get-clear-policy 'save)
('allow)
('clear
(set-frame-parameter nil 'bufferlo-bookmark-frame-name nil))
('clear-warn
(set-frame-parameter nil 'bufferlo-bookmark-frame-name nil)
(msg-append "cleared frame bookmark"))
(_ ))))
(cond
;; Clear the about-to-be overwritten set bookmark.
(clear-active-set-bm
(msg-append "cleared overwritten set bookmark")
(bufferlo--clear-set-bookmarks-by-name name))
;; Clear the about-to-be overwritten frame bookmark.
(clear-active-frame-bm
(msg-append "cleared overwritten frame bookmark")
(bufferlo--clear-frame-bookmarks-by-name name)))
;; Finally, save the bookmark
(bufferlo--bookmark-tab-save name no-overwrite no-message msg)))))
(defun bufferlo-bookmark-tab-load (name)
"Load a tab bookmark.
NAME is the bookmark's name.
`bufferlo-bookmark-tab-replace-policy' controls if the loaded
bookmark replaces the current tab or makes a new tab.
Specify a prefix argument to force reusing the current tab."
(interactive
(list (completing-read
"Load bufferlo tab bookmark: "
(bufferlo--bookmark-get-names #'bufferlo--bookmark-tab-handler)
nil 'require-match nil 'bufferlo-bookmark-tab-history
(alist-get 'bufferlo-bookmark-tab-name (bufferlo--current-tab)))))
(bufferlo--warn)
(when-let* ((bookmark-record (bufferlo--bookmark-get-bookmark name)))
(bufferlo--bookmark-tab-handler bookmark-record 'not-jump)))
(defun bufferlo-bookmark-tab-save-current ()
"Save the current tab to its associated bookmark.
The associated bookmark is determined by the name of the bookmark to
which the tab was last saved or (if not yet saved) from which it was
initially loaded. Performs an interactive bookmark selection if no
associated bookmark exists."
(interactive)
(bufferlo--warn)
(if-let* ((bm (alist-get 'bufferlo-bookmark-tab-name
(cdr (bufferlo--current-tab)))))
(bufferlo--bookmark-tab-save bm)
(call-interactively #'bufferlo-bookmark-tab-save)))
(defun bufferlo-bookmark-tab-load-current ()
"Load the current tab's associated bookmark.
The associated bookmark is determined by the name of the bookmark to
which the tab was last saved or (if not yet saved) from which it was
initially loaded. Performs an interactive bookmark selection if no
associated bookmark exists.
This reuses the current tab even if
`bufferlo-bookmark-tab-replace-policy' is set to \\='new."
(interactive)
(bufferlo--warn)
(if-let* ((bm (alist-get 'bufferlo-bookmark-tab-name
(cdr (bufferlo--current-tab)))))
;; On reload, always reuse the existing tab (don't make a new one)
(let ((bufferlo-bookmark-tab-replace-policy 'replace)
;; The bookmark is detected as a duplicate bookmark, allow it here
(bufferlo-bookmark-tab-duplicate-policy 'allow))
(bufferlo-bookmark-tab-load bm))
(call-interactively #'bufferlo-bookmark-tab-load)))
(defun bufferlo--clear-tab-bookmarks-by-name (bookmark-name)
"Clear BOOKMARK-NAME tab bookmarks across all frames and their tabs."
(dolist (frame (frame-list))
(tab-bar-tabs-set
(mapcar (lambda (tab)
(let ((tbm (alist-get 'bufferlo-bookmark-tab-name tab)))
(if (and tbm (equal tbm bookmark-name))
(assq-delete-all 'bufferlo-bookmark-tab-name tab)
tab)))
(funcall tab-bar-tabs-function frame))
frame)))
(defun bufferlo--clear-frame-bookmarks-by-name (bookmark-name)
"Clear BOOKMARK-NAME frame bookmarks across all frames."
(dolist (frame (frame-list))
(when (equal bookmark-name
(frame-parameter frame 'bufferlo-bookmark-frame-name))
(set-frame-parameter frame 'bufferlo-bookmark-frame-name nil))))
(defun bufferlo--clear-set-bookmarks-by-name (bookmark-name)
"Clear BOOKMARK-NAME set bookmark."
(setq bufferlo--active-sets
(assoc-delete-all bookmark-name bufferlo--active-sets #'equal)))
(defun bufferlo--bookmark-frame-save (name &optional no-overwrite no-message msg)
"Save the current frame as a bookmark.
NAME is the bookmark's name. If NO-OVERWRITE is non-nil, record
the new bookmark without throwing away the old one. If NO-MESSAGE
is non-nil, inhibit the save status message. If MSG is non-nil,
it is added to the save message."
(bookmark-store name (bufferlo--bookmark-set-location
(bufferlo--bookmark-frame-make))
no-overwrite)
(set-frame-parameter nil 'bufferlo-bookmark-frame-name name)
(unless no-message
(message "Saved bufferlo frame bookmark: %s%s" name (if msg msg ""))))
(defun bufferlo-bookmark-frame-save (name &optional no-overwrite no-message)
"Save the current frame as a bookmark.
NAME is the bookmark's name. If NO-OVERWRITE is non-nil, record
the new bookmark without throwing away the old one. If NO-MESSAGE
is non-nil, inhibit the save status message.
This function persists the current frame's state: The resulting bookmark
stores the frame's window configurations, active tabs, and the local
buffer lists those tabs. In addition, it saves the bookmark state (not
the contents) of the bookmarkable buffers for each tab.
Use `bufferlo-bookmark-tab-in-bookmarked-frame-policy' to
influence how this function handles setting a frame bookmark in
the presence of bookmarked tabs. Using both together is allowed,
but is not recommended.
Warn if this bookmark will overwrite a set or tab bookmark, and clear
them, if active, when overwriting."
(interactive
(list (completing-read
"Save bufferlo frame bookmark: "
(bufferlo--bookmark-get-names #'bufferlo--bookmark-frame-handler)
nil nil nil 'bufferlo-bookmark-frame-history
(frame-parameter nil 'bufferlo-bookmark-frame-name))))
(bufferlo--warn)
(catch :abort
(let* ((abm (assoc name (bufferlo--active-bookmarks)))
(fbm (frame-parameter nil 'bufferlo-bookmark-frame-name))
(set-bm-names (bufferlo--bookmark-get-names
#'bufferlo--bookmark-set-handler))
(tab-bm-names (bufferlo--bookmark-get-names
#'bufferlo--bookmark-tab-handler))
(is-set-bm (member name set-bm-names))
(is-tab-bm (member name tab-bm-names))
clear-active-set-bm
clear-active-tab-bm
msg)
(cond
;; Overwriting a set bookmark of the same name? If so, and the set
;; bookmark is active, mark it to deactivate, leaving its bookmarks and
;; buffers intact, and after we know the replacement bookmark will be
;; saved.
(is-set-bm
(if (y-or-n-p (format "Overwrite the bufferlo set bookmark `%s'? " name))
(setq clear-active-set-bm
(assoc name bufferlo--active-sets))
(throw :abort t)))
;; Overwriting a tab bookmark of the same name? If so, and the tab
;; bookmark is active, mark it to deactivate, leaving its buffers
;; intact, and after we know the replacement bookmark will be saved.
(is-tab-bm
(if (y-or-n-p (format "Overwrite the bufferlo tab bookmark `%s'? " name))
(setq clear-active-tab-bm
(assoc name (bufferlo--active-bookmarks nil 'tbm)))
(throw :abort t))))
(cl-labels
((msg-append (s) (setq msg (concat msg "; " s))))
;; Only check policies when bm is a frame bm, and is not already
;; associated with this frame.
(unless (or is-set-bm
is-tab-bm
(and
fbm
(equal fbm (car abm))))
;; Bookmark already loaded in another frame?
(when abm
(pcase (bufferlo--bookmark-get-duplicate-policy
name "frame" bufferlo-bookmark-frame-duplicate-policy 'save)
('allow)
('clear
(bufferlo--clear-frame-bookmarks-by-name name))
('clear-warn
(bufferlo--clear-frame-bookmarks-by-name name)
(msg-append "cleared duplicate active frame bookmark"))
('ignore
(throw :abort t))
('raise
(bufferlo--bookmark-raise abm)
(throw :abort t))))
;; Tab bookmarks in this frame?
(when (> (length
(bufferlo--active-bookmarks (list (selected-frame)) 'tbm))
0)
(pcase (bufferlo--bookmark-tab-get-clear-policy 'save-frame)
('clear
(let ((current-prefix-arg '(4))) ; emulate C-u
(bufferlo-clear-active-bookmarks (list (selected-frame)))))
('clear-warn
(let ((current-prefix-arg '(4))) ; emulate C-u
(bufferlo-clear-active-bookmarks (list (selected-frame))))
(msg-append "cleared tab bookmarks"))
('allow))))
(cond
;; Clear the about-to-be overwritten set bookmark.
(clear-active-set-bm
(msg-append "cleared overwritten set bookmark")
(bufferlo--clear-set-bookmarks-by-name name))
;; Clear the about-to-be overwritten tab bookmark.
(clear-active-tab-bm
(msg-append "cleared overwritten tab bookmark")
(bufferlo--clear-tab-bookmarks-by-name name)))
;; Finally, save the bookmark
(bufferlo--bookmark-frame-save name no-overwrite no-message msg)))))
(defun bufferlo-bookmark-frame-load (name)
"Load a frame bookmark.
NAME is the bookmark's name.
Replace the current frame's state if
`bufferlo-bookmark-frame-load-make-frame' is nil."
(interactive
(list (completing-read
"Load bufferlo frame bookmark: "
(bufferlo--bookmark-get-names #'bufferlo--bookmark-frame-handler)
nil 'require-match nil 'bufferlo-bookmark-frame-history
(frame-parameter nil 'bufferlo-bookmark-frame-name))))
(bufferlo--warn)
(when-let* ((bookmark-record (bufferlo--bookmark-get-bookmark name)))
(bufferlo--bookmark-frame-handler bookmark-record 'not-jump)))
(defun bufferlo-bookmark-frame-save-current ()
"Save the current frame to its associated bookmark.
The associated bookmark is determined by the name of the bookmark to
which the frame was last saved or (if not yet saved) from which it was
initially loaded. Performs an interactive bookmark selection if no
associated bookmark exists."
(interactive)
(bufferlo--warn)
(if-let* ((bm (frame-parameter nil 'bufferlo-bookmark-frame-name)))
(bufferlo--bookmark-frame-save bm)
(call-interactively #'bufferlo-bookmark-frame-save)))
(defun bufferlo-bookmark-frame-load-current ()
"Load the current frame's associated bookmark.
The associated bookmark is determined by the name of the bookmark to
which the frame was last saved or (if not yet saved) from which it was
initially loaded. Performs an interactive bookmark selection if no
associated bookmark exists."
(interactive)
(bufferlo--warn)
(if-let* ((bm (frame-parameter nil 'bufferlo-bookmark-frame-name)))
;; On reload, always resue the existing frame (don't make a new one)
(let ((bufferlo-bookmark-frame-load-make-frame nil)
(bufferlo-bookmark-frame-load-policy
'replace-frame-retain-current-bookmark)
;; The bookmark is detected as a duplicate bookmark, allow it here
(bufferlo-bookmark-frame-duplicate-policy 'allow))
(bufferlo-bookmark-frame-load bm))
(call-interactively #'bufferlo-bookmark-frame-load)))
(defun bufferlo-bookmark-frame-load-merge ()
"Load a bufferlo frame bookmark merging its content into the current frame."
(interactive)
(let ((bufferlo-bookmark-frame-duplicate-policy 'allow)
(bufferlo-bookmark-frame-load-policy 'merge)
(bufferlo-bookmark-frame-load-make-frame nil))
(call-interactively #'bufferlo-bookmark-frame-load)))
(defun bufferlo--active-bookmarks (&optional frames type)
"Produces an alist of active bufferlo bookmarks.
The alist is of the form:
((bookmark-name .
((type . type) (frame . frame) (tab-number . tab-number))) ...)
for the specified FRAMES,
filtered by TYPE, where type is:
\\='fbm for frame bookmarks which include frame only or
\\='tbm for tab bookmarks which include frame and tab numbers."
(let ((abms))
(dolist (frame (or frames (frame-list)))
(when-let* ((fbm (frame-parameter frame 'bufferlo-bookmark-frame-name)))
(when (or (null type) (eq type 'fbm))
(push (list fbm `((type . fbm)
(frame . ,frame)))
abms)))
(let ((tabs (funcall tab-bar-tabs-function frame)))
(dolist (tab tabs)
(when-let* ((tbm (alist-get 'bufferlo-bookmark-tab-name tab)))
(when (or (null type) (eq type 'tbm))
(push (list tbm `((type . tbm)
(frame . ,frame)
(tab-number . ,(1+ (tab-bar--tab-index
tab tabs frame)))))
abms))))))
abms))
(defun bufferlo-bookmarks-save-all-p (_bookmark-name)
"This predicate matches all bookmark names.
This is intended to be used in `bufferlo-bookmarks-save-predicate-functions'."
t)
(defun bufferlo-bookmarks-load-all-p (_bookmark-name)
"This predicate matches all bookmark names.
This is intended to be used in `bufferlo-bookmarks-load-predicate-functions'."
t)
(defun bufferlo--active-bookmark-duplicates ()
"Produce a list of duplicated active bookmark names."
(let ((abm-names (mapcar #'car (bufferlo--active-bookmarks))))
(seq-filter (lambda (abm)
(> (seq-count (lambda (x) (equal x abm)) abm-names) 1))
(seq-uniq abm-names))))
(defun bufferlo--list-duplicates (lst)
"Return unique duplicate elements from LST.
Equality test is \\='equal."
(let ((ht (make-hash-table :test 'equal :size (length lst))))
(mapc (lambda (x) (puthash x (if (gethash x ht) 'dupe t) ht)) lst)
(seq-uniq
(delq nil
(mapcar (lambda (x) (when (eq (gethash x ht) 'dupe) x)) lst)))))
(defun bufferlo--bookmarks-save (active-bookmark-names active-bookmarks &optional no-message)
"Save the bookmarks in ACTIVE-BOOKMARK-NAMES indexed by ACTIVE-BOOKMARKS.
Specify NO-MESSAGE to inhibit the bookmark save status message."
(let (bookmarks-saved)
;; Offer to reload before we update bookmark entries. NOTE: There is
;; still a potential race condition if the bookmark file changes via
;; another process during this loop and the user will be prompted below
;; when `bookmark-save' is called so we inhibit loading again around that
;; call.
(bookmark-maybe-load-default-file)
;; Inhibit built-in bookmark file saving and reloading until we're done.
(let (bookmark-save-flag
bookmark-watch-bookmark-file)
(dolist (abm-name active-bookmark-names)
(when-let* ((abm (assoc abm-name active-bookmarks))
(abm-type (alist-get 'type (cadr abm)))
(abm-frame (alist-get 'frame (cadr abm))))
(with-selected-frame abm-frame
(cond
((eq abm-type 'fbm)
(bufferlo--bookmark-frame-save abm-name nil t))
((eq abm-type 'tbm)
(let ((orig-tab-number (1+ (tab-bar--current-tab-index)))
tab-bar-tab-post-select-functions)
(tab-bar-select-tab (alist-get 'tab-number (cadr abm)))
(bufferlo--bookmark-tab-save abm-name nil t)
(tab-bar-select-tab orig-tab-number))))
(push abm-name bookmarks-saved)))))
(cond
(bookmarks-saved
(let ((inhibit-message (or no-message
(not (memq bufferlo-bookmarks-auto-save-messages
(list 'saved t))))))
(let (bookmark-watch-bookmark-file ; see NOTE: above
(start-time (current-time)))
(bookmark-save)
(message "Saved bufferlo bookmarks: %s, in %.2f second(s)"
(mapconcat 'identity bookmarks-saved ", ")
(float-time (time-subtract (current-time) start-time))))))
(t
(when (and (not no-message)
(memq bufferlo-bookmarks-auto-save-messages
(list 'notsaved t)))
(message "No bufferlo bookmarks saved."))))))
(defun bufferlo-bookmarks-save (&optional all)
"Save active bufferlo bookmarks.
This is invoked via an optional timer which runs according to
`bufferlo-bookmarks-auto-save-interval', or and is optionally invoked at
Emacs exit.
You may invoke this manually at any time to save active bookmarks;
however, doing so does not reset the save interval timer.
Each bookmark is filtered according to
`bufferlo-bookmarks-save-predicate-functions'.
Specify ALL to ignore the predicates and save every active bufferlo
bookmark or use a prefix argument across ALL frames, overriding
`bufferlo-bookmarks-save-frame-policy'.
Note: If there are duplicate active bufferlo bookmarks, the last one to
be saved will take precedence.
Duplicate bookmarks are handled according to
`bufferlo-bookmarks-save-duplicates-policy'."
(interactive)
(unwind-protect ; restart the timer in case the user quits any prompts
(catch :abort
(let* ((frames (if all
(frame-list)
(pcase bufferlo-bookmarks-save-frame-policy
('current
(list (selected-frame)))
('other
(seq-filter (lambda (x) (not (eq x (selected-frame))))
(frame-list)))
(_ (frame-list)))))
;; Get the active bookmarks for the frames captured by the current
;; bufferlo-bookmarks-save-frame-policy only
(abms (bufferlo--active-bookmarks frames))
;; Override bufferlo-bookmarks-save-predicate-functions on prefix arg
(bufferlo-bookmarks-save-predicate-functions
(if (or all (consp current-prefix-arg))
(list #'bufferlo-bookmarks-save-all-p)
bufferlo-bookmarks-save-predicate-functions))
;; Filter the bookmark names to save
(abm-names-to-save
(seq-filter (lambda (x) (not (null x)))
(mapcar
(lambda (abm)
(let ((abm-name (car abm)))
(when (run-hook-with-args-until-success
'bufferlo-bookmarks-save-predicate-functions
abm-name)
abm-name)))
abms)))
;; There may be open bookmarks that are duplicates
(dupes-to-save (bufferlo--list-duplicates abm-names-to-save))
;; We'll handle these bookmarks according to the duplicate-policy
(duplicate-policy bufferlo-bookmarks-save-duplicates-policy))
(when (> (length dupes-to-save) 0)
(when (eq duplicate-policy 'prompt)
(pcase (with-local-quit
(read-answer
(format "Duplicate active bookmarks %s: Allow to save, Disallow to cancel "
dupes-to-save)
'(("allow" ?a "Allow duplicate")
("disallow" ?d "Disallow duplicates; cancel saving")
("help" ?h "Help")
("quit" ?q "Quit with no changes"))))
("allow" (setq duplicate-policy 'allow))
("disallow" (setq duplicate-policy 'disallow))
(_ (throw :abort t))))
(pcase duplicate-policy
('allow)
(_ (throw :abort t))))
(bufferlo--bookmarks-save abm-names-to-save abms)))
;; Run the timer again only after this function is complete to avoid race
;; conditions with user prompts.
(bufferlo--bookmarks-auto-save-timer-maybe-start)))
(defun bufferlo-bookmark--frame-save-on-delete (frame)
"`frame-delete' advice for saving the current frame bookmark on deletion.
FRAME is the frame being deleted."
(let ((fbm (frame-parameter frame 'bufferlo-bookmark-frame-name)))
(pcase bufferlo-bookmark-frame-save-on-delete
('t
(when (y-or-n-p (if fbm
(format-message "Save frame bookmark `%s'? " fbm)
"Save new frame bookmark? "))
(bufferlo-bookmark-frame-save-current)))
('when-bookmarked
(when fbm (bufferlo--bookmark-frame-save fbm))))))
(defun bufferlo-bookmark--tab-save-on-close (tab _only)
"Function for saving the current tab bookmark on deletion.
Intended as a hook function for `tab-bar-tab-pre-close-functions'.
TAB is the tab being closed. _ONLY is for compatibility with the hook."
(let ((tbm (alist-get 'bufferlo-bookmark-tab-name tab)))
(pcase bufferlo-bookmark-tab-save-on-close
('t
(when (y-or-n-p (if tbm
(format-message "Save tab bookmark `%s'? " tbm)
"Save new tab bookmark? "))
(bufferlo-bookmark-tab-save-current)))
('when-bookmarked
(when tbm (bufferlo--bookmark-tab-save tbm))))))
(defun bufferlo--bookmarks-save-at-emacs-exit ()
"Save bufferlo bookmarks at Emacs exit.
This honors `bufferlo-bookmarks-save-at-emacs-exit' by predicate or
\\='all. Intended to be invoked via `kill-emacs-hook'."
(bufferlo--bookmarks-auto-save-timer-maybe-cancel)
(let ((bufferlo-bookmarks-save-predicate-functions
(if (eq bufferlo-bookmarks-save-at-emacs-exit 'all)
(list #'bufferlo-bookmarks-save-all-p)
bufferlo-bookmarks-save-predicate-functions)))
(bufferlo-bookmarks-save)))
(defun bufferlo--bookmarks-load-startup ()
"Load bookmarks at startup."
(let ((bufferlo-bookmarks-load-tabs-make-frame
bufferlo-bookmarks-load-at-emacs-startup-tabs-make-frame))
(run-with-idle-timer 0 nil (lambda ()
(bufferlo-bookmarks-load
(eq bufferlo-bookmarks-load-at-emacs-startup
'all))))))
(defun bufferlo-bookmarks-load (&optional all)
"Load stored bufferlo bookmarks.
Invoke manually or via `window-setup-hook' to restore bookmarks
at Emacs startup.
Each bookmark is filtered according to
`bufferlo-bookmarks-load-predicate-functions'.
ALL, or a prefix argument, ignores the load predicates and loads
all stored bufferlo bookmarks. Tab bookmarks are loaded into the
current or new frame according to
`bufferlo-bookmarks-load-tabs-make-frame'."
(interactive)
(let* ((bookmarks-loaded nil)
(bookmarks-failed nil)
(start-time (current-time))
(orig-frame (selected-frame))
;; NOTE: We might need a policy that controls how strict or
;; lax bulk bookmark loading can be. Via below, users get what
;; they implicitly ask for by loading all bookmarks.
(bufferlo-bookmark-frame-duplicate-policy 'allow)
(bufferlo-bookmark-tab-duplicate-policy 'allow)
(bufferlo-bookmark-tab-in-bookmarked-frame-policy 'allow)
(bufferlo-bookmarks-load-predicate-functions
(if (or all (consp current-prefix-arg))
(list #'bufferlo-bookmarks-load-all-p)
bufferlo-bookmarks-load-predicate-functions))
;; Reset current-prefix-arg to allow
;; `bufferlo--bookmark-frame-handler' to create frames should
;; that policy be set to do so.
(current-prefix-arg nil))
;; Load bookmark sets
(dolist (bookmark-name (bufferlo--bookmark-get-names
#'bufferlo--bookmark-set-handler))
(unless (assoc bookmark-name bufferlo--active-sets)
(when (run-hook-with-args-until-success
'bufferlo-bookmarks-load-predicate-functions bookmark-name)
(when-let* ((bookmark-record
(bufferlo--bookmark-get-bookmark bookmark-name)))
(if (bufferlo--bookmark-set-handler bookmark-record 'not-jump)
(push bookmark-name bookmarks-failed)
(push bookmark-name bookmarks-loaded))))))
;; Load tab bookmarks, making a new frame, if required (the
;; geometry of which is via the user's default-frame-alist)
(select-frame orig-frame) ; default frame for tabs
;; We handle making tabs in this loop manually
(let ((bufferlo-bookmark-tab-replace-policy 'replace)
(tab-bar-new-tab-choice t)
(new-tab-frame nil))
(dolist (bookmark-name (bufferlo--bookmark-get-names
#'bufferlo--bookmark-tab-handler))
(unless (assoc bookmark-name (bufferlo--active-bookmarks))
(when (run-hook-with-args-until-success
'bufferlo-bookmarks-load-predicate-functions bookmark-name)
(if (and bufferlo-bookmarks-load-tabs-make-frame
(not new-tab-frame))
(select-frame (setq new-tab-frame (make-frame)))
(tab-bar-new-tab-to))
(when-let* ((bookmark-record
(bufferlo--bookmark-get-bookmark bookmark-name)))
(if (bufferlo--bookmark-tab-handler bookmark-record 'not-jump)
(push bookmark-name bookmarks-failed)
(push bookmark-name bookmarks-loaded)))))))
;; Load frame bookmarks
(dolist (bookmark-name (bufferlo--bookmark-get-names
#'bufferlo--bookmark-frame-handler))
(unless (assoc bookmark-name (bufferlo--active-bookmarks))
(when (run-hook-with-args-until-success
'bufferlo-bookmarks-load-predicate-functions bookmark-name)
(when-let* ((bookmark-record
(bufferlo--bookmark-get-bookmark bookmark-name)))
(if (bufferlo--bookmark-frame-handler bookmark-record 'not-jump)
(push bookmark-name bookmarks-failed)
(push bookmark-name bookmarks-loaded))))))
;; Leave the user on the starting frame
(select-frame-set-input-focus orig-frame)
(when bookmarks-loaded
(message "Loaded bufferlo bookmarks: %s, in %.2f seconds%s"
(mapconcat #'identity bookmarks-loaded ", ")
(float-time (time-subtract (current-time) start-time))
(if bookmarks-failed
(concat "; failed to load: "
(mapconcat #'identity bookmarks-failed ", "))
"")))))
;; TODO: handle option to save? prefix arg to save or not save?
(defun bufferlo-bookmarks-close-interactive ()
"Prompt for active bufferlo bookmarks to close."
(interactive)
(bufferlo--warn)
(let* ((abms (bufferlo--active-bookmarks))
(abm-names (mapcar #'car abms))
(comps (bufferlo--bookmark-completing-read-multiple
"Close bookmark(s) without saving: " abm-names)))
(bufferlo--close-active-bookmarks comps abms)))
(defun bufferlo-bookmarks-save-interactive ()
"Prompt for active bufferlo bookmarks to save."
(interactive)
(bufferlo--warn)
(let* ((abms (bufferlo--active-bookmarks))
(abm-names (mapcar #'car abms))
(comps (bufferlo--bookmark-completing-read-multiple
"Save bookmark(s): " abm-names)))
(bufferlo--bookmarks-save comps abms)))
(defun bufferlo-bookmarks-load-interactive ()
"Prompt for bufferlo bookmarks to load.
Use a single prefix argument to narrow the candidates to frame
bookmarks, double for bookmarks, triple for bookmark sets."
(interactive)
(bufferlo--warn)
(let* ((bookmark-names
(apply 'bufferlo--bookmark-get-names
(cond
((and (consp current-prefix-arg)
(= (prefix-numeric-value current-prefix-arg) 4))
(list #'bufferlo--bookmark-frame-handler))
((and (consp current-prefix-arg)
(= (prefix-numeric-value current-prefix-arg) 16))
(list #'bufferlo--bookmark-tab-handler))
((and (consp current-prefix-arg)
(= (prefix-numeric-value current-prefix-arg) 64))
(list #'bufferlo--bookmark-set-handler))
(t bufferlo--bookmark-handlers))))
(comps (bufferlo--bookmark-completing-read-multiple
"Load bookmark(s): "
bookmark-names)))
(dolist (bookmark-name comps)
(when-let* ((bookmark-record
(bufferlo--bookmark-get-bookmark bookmark-name))
(bookmark-handler (bookmark-get-handler bookmark-record)))
(funcall bookmark-handler bookmark-record 'not-jump)))))
(defun bufferlo-maybe-clear-active-bookmark (&optional force)
"Clear the current frame and/or tab bufferlo bookmark.
This clears the active bookmark name only if there is another
active bufferlo bookmark with the same name and FORCE is nil.
This is useful if an active bookmark has been loaded more than
once, and especially if you use the auto save feature and want to
ensure that only one bookmark is active.
FORCE will clear the bookmark even if it is currently unique.
Specify a prefix argument to imply FORCE."
(interactive)
(let* ((fbm (frame-parameter nil 'bufferlo-bookmark-frame-name))
(tbm (alist-get 'bufferlo-bookmark-tab-name
(tab-bar--current-tab-find)))
(duplicate-fbm (> (length (seq-filter
(lambda (x) (equal fbm (car x)))
(bufferlo--active-bookmarks nil 'fbm)))
1))
(duplicate-tbm (> (length (seq-filter
(lambda (x) (equal tbm (car x)))
(bufferlo--active-bookmarks nil 'tbm)))
1)))
(when (or force (consp current-prefix-arg) duplicate-fbm)
(set-frame-parameter nil 'bufferlo-bookmark-frame-name nil))
(when (or force (consp current-prefix-arg) duplicate-tbm)
(setf (alist-get 'bufferlo-bookmark-tab-name
(cdr (bufferlo--current-tab)))
nil))))
(defun bufferlo-clear-active-bookmarks (&optional frames)
"Clear all active bufferlo frame and tab bookmarks.
This leaves all content untouched and does not impact stored bookmarks.
You will be prompted to confirm clearing (it cannot be undone)
unless a prefix argument is specified.
This is useful when you have accumulated a complex working set of
frames, tabs, buffers and want to save new bookmarks without
disturbing existing bookmarks, or where auto-saving is enabled
and you want to avoid overwriting stored bookmarks, perhaps with
transient work.
FRAMES is an optional list of frames on which to clear bookmarks
which defaults to all frames, if not specified."
(interactive)
(when (or (consp current-prefix-arg)
(y-or-n-p "Clear active bufferlo bookmarks? "))
(dolist (frame (or frames (frame-list)))
(set-frame-parameter frame 'bufferlo-bookmark-frame-name nil)
(dolist (tab (funcall tab-bar-tabs-function frame))
(setf (alist-get 'bufferlo-bookmark-tab-name tab) nil)))))
(defun bufferlo--close-active-bookmarks (active-bookmark-names active-bookmarks)
"Close the bookmarks in ACTIVE-BOOKMARK-NAMES indexed by ACTIVE-BOOKMARKS."
(let* ((abms (seq-filter
(lambda (x) (member (car x) active-bookmark-names))
active-bookmarks))
(tbms (seq-filter
(lambda (x) (eq 'tbm (alist-get 'type (cadr x))))
abms))
(fbms (seq-filter
(lambda (x) (eq 'fbm (alist-get 'type (cadr x))))
abms))
(orig-frame (selected-frame)))
;; Close tab bookmarks.
(let ((tab-bar-tab-post-select-functions))
;; We can't rely on tab names (they don't have to be unique) or tab
;; numbers to uniquely identify them (because they may change when other
;; tabs close), so we assign unique tags for the second pass, below.
;; They are removed in that pass. If the user quits when prompted, the
;; tags are cleared on all tabs before being reassigned in the first
;; pass..
;;
;; Clear any lingering tab tags on bookmarked tabs.
(dolist (frame (frame-list))
(dolist (tab (funcall tab-bar-tabs-function frame))
(when (alist-get 'bufferlo-bookmark-tab-name tab)
(setf (alist-get 'bufferlo--tab-tag (cdr tab)) nil))))
;; Now assign fresh tab tags.
(let ((tab-tag 0))
(dolist (tbm tbms)
(let ((tbm-frame (alist-get 'frame (cadr tbm)))
(tbm-tab-number (alist-get 'tab-number (cadr tbm))))
(with-selected-frame tbm-frame
(tab-bar-select-tab tbm-tab-number)
(setf (alist-get 'bufferlo--tab-tag (cadr tbm))
tab-tag)
(setf (alist-get 'bufferlo--tab-tag (cdr (bufferlo--current-tab)))
tab-tag)))
(setq tab-tag (1+ tab-tag))))
;; Use the tab tags to uniquely identify tabs to close.
(dolist (tbm tbms)
(let ((tbm-frame (alist-get 'frame (cadr tbm)))
(tab-tag (alist-get 'bufferlo--tab-tag (cadr tbm))))
(with-selected-frame tbm-frame
;; If called in a batch, raise frame in case of prompts for buffers
;; that need saving:
(select-frame-set-input-focus (selected-frame))
(when (catch :found ; catch exits with the matching tag tab selected
(let ((i 1)) ; tab-number is 1-based
(dolist (tab (funcall tab-bar-tabs-function))
(tab-bar-select-tab i)
(when-let* ((tag (alist-get 'bufferlo--tab-tag tab)))
(when (= tag tab-tag)
(setq tab (assq-delete-all 'bufferlo--tab-tag tab))
(throw :found i)))
(setq i (1+ i)))
nil))
(let ((bufferlo-kill-buffers-prompt nil)
(bufferlo-bookmark-tab-save-on-close nil)
(bufferlo-close-tab-kill-buffers-prompt nil))
(bufferlo-tab-close-kill-buffers)))))))
;; Close frame bookmarks.
(dolist (fbm fbms)
(let ((fbm-frame (alist-get 'frame (cadr fbm))))
(with-selected-frame fbm-frame
(let ((bufferlo-kill-buffers-prompt nil)
(bufferlo-bookmark-frame-save-on-delete nil)
(bufferlo-delete-frame-kill-buffers-prompt nil))
(bufferlo-delete-frame-kill-buffers)))))
;; Restore the original frame, if possible, in case any frames changed
;; during user prompts.
(when (frame-live-p orig-frame)
(select-frame-set-input-focus orig-frame))))
(defun bufferlo-bookmarks-close ()
"Close all active bufferlo frame and tab bookmarks and kill their buffers.
Bufferlo bookmark sets are cleared.
You will be prompted to save bookmarks using filter predicates or
save all.
A prefix argument inhibits the prompt and bypasses saving."
(interactive)
(bufferlo--warn)
(let* ((close t)
(abms (bufferlo--active-bookmarks))
(abm-names (mapcar #'car abms)))
(if (null abms)
(message "No active bufferlo bookmarks")
(unless (consp current-prefix-arg)
(pcase (with-local-quit
(read-answer
"Save bookmarks before closing them: All, Predicate, No save "
'(("all" ?a "Save all active bookmarks")
("pred" ?p "Save predicate-filtered bookmarks, if set")
("nosave" ?n "Don't save")
("help" ?h "Help")
("quit" ?q "Quit"))))
("all"
(bufferlo-bookmarks-save 'all))
("pred"
(bufferlo-bookmarks-save))
("nosave")
(_ (setq close nil))))
(when close
(bufferlo--close-active-bookmarks abm-names abms)
(bufferlo--set-clear-all)))))
(defun bufferlo--bookmark-raise (abm)
"Raise ABM's frame/tab."
(when-let* ((abm-type (alist-get 'type (cadr abm)))
(abm-frame (alist-get 'frame (cadr abm))))
(with-selected-frame abm-frame
(select-frame-set-input-focus (selected-frame))
(when (eq abm-type 'tbm)
(tab-bar-select-tab
(alist-get 'tab-number (cadr abm)))))))
(defun bufferlo--bookmark-raise-by-name (abm-name &optional abms)
"Raise bookmark's frame/tab by ABM-NAME in ABMS."
(setq abms (or abms (bufferlo--active-bookmarks)))
(when-let* ((abm (assoc abm-name abms)))
(bufferlo--bookmark-raise abm)))
(defun bufferlo-bookmark-raise ()
"Raise the selected bookmarked frame or tab.
Note: If there are duplicated bookmarks, the first one found is
raised."
(interactive)
(bufferlo--warn)
(let* ((abms (bufferlo--active-bookmarks))
(abm-names (mapcar #'car abms))
(comp (bufferlo--bookmark-completing-read
"Select a bookmark to raise: " abm-names)))
(bufferlo--bookmark-raise-by-name comp abms)))
;; DWIM convenience functions
(defun bufferlo-bookmark-save-current ()
"DWIM save current bufferlo bookmark.
Save the current bufferlo frame bookmark or tab bookmark,
prioritizing frame bookmarks over tab bookmarks, should both
exist.
Unlike `bufferlo-bookmark-frame-save-current' and
`bufferlo-bookmark-tab-save-current', this does not prompt to
save a new bookmark."
(interactive)
(bufferlo--warn)
(if-let* ((bm (frame-parameter nil 'bufferlo-bookmark-frame-name)))
(bufferlo-bookmark-frame-save bm)
(if-let* ((bm (alist-get 'bufferlo-bookmark-tab-name
(cdr (bufferlo--current-tab)))))
(bufferlo--bookmark-tab-save bm)
(message "No active bufferlo frame or tab bookmark to save."))))
(defun bufferlo-bookmark-load-current ()
"DWIM reload current bufferlo bookmark.
Load the current bufferlo frame bookmark or tab bookmark,
prioritizing frame bookmarks over tab bookmarks, should both
exist.
Unlike `bufferlo-bookmark-frame-load-current' and
`bufferlo-bookmark-tab-load-current', this does not prompt to
load a new bookmark."
(interactive)
(bufferlo--warn)
(if-let* ((bm (frame-parameter nil 'bufferlo-bookmark-frame-name)))
;; On reload, always resue the existing frame (don't make a new one)
(let ((bufferlo-bookmark-frame-load-make-frame nil)
(bufferlo-bookmark-frame-load-policy
'replace-frame-retain-current-bookmark)
;; The bookmark is detected as a duplicate bookmark, allow it here
(bufferlo-bookmark-frame-duplicate-policy 'allow))
(bufferlo-bookmark-frame-load bm))
(if-let* ((bm (alist-get 'bufferlo-bookmark-tab-name
(cdr (bufferlo--current-tab)))))
;; On reload, always resue the existing tab (don't make a new one)
(let ((bufferlo-bookmark-tab-replace-policy 'replace)
;; The bookmark is detected as a duplicate bookmark, allow it here
(bufferlo-bookmark-tab-duplicate-policy 'allow))
(bufferlo-bookmark-tab-load bm))
(message "No active bufferlo frame or tab bookmark to load."))))
(defun bufferlo-bookmark-close-current ()
"DWIM close current bufferlo bookmark and kill its buffers.
Close the current bufferlo frame bookmark or tab bookmark,
prioritizing frame bookmarks over tab bookmarks, should both
exist."
(interactive)
(bufferlo--warn)
(if-let* ((bm (frame-parameter nil 'bufferlo-bookmark-frame-name)))
(bufferlo-delete-frame-kill-buffers)
(if-let* ((bm (alist-get 'bufferlo-bookmark-tab-name
(cdr (bufferlo--current-tab)))))
(bufferlo-tab-close-kill-buffers)
(message "No active bufferlo frame or tab bookmark to close."))))
;;; bookmark hook and advice
(defun bufferlo--bookmark-after-load-file-hook ()
"Scan the loaded bookmarks list for missing active bufferlo bookmarks."
(let ((alist-bookmarks (bufferlo--bookmark-get-names)))
(dolist (bookmark (bufferlo--active-bookmarks))
(unless (member (car bookmark) alist-bookmarks)
(warn "Active bufferlo bookmark `%s' not present in loaded bookmarks"
(car bookmark))))))
;; (defun bookmark-set (&optional name no-overwrite)
;; (defun bookmark-set-no-overwrite (&optional name push-bookmark)
;; Leave these alone for now. They warn about duplicate bookmarks.
;; (defun bookmark--jump-via (bookmark-name-or-record display-function)
(defun bufferlo--bookmark--jump-via-advice (oldfn bookmark-name-or-record display-function)
"`bookmark--jump-via' advice to nullify `display-function'.
OLDFN BOOKMARK-NAME-OR-RECORD DISPLAY-FUNCTION"
(when-let* ((bookmark-record (bookmark-get-bookmark bookmark-name-or-record 'noerror)))
(if (rassoc (bookmark-type-from-full-record bookmark-record)
bufferlo--bookmark-type-names)
(let ((bookmark-fringe-mark nil))
(funcall oldfn bookmark-record #'ignore))
(funcall oldfn bookmark-record display-function))))
;; (defun bookmark-rename (old-name &optional new-name)
(defun bufferlo--bookmark-rename-advice (oldfn &optional old-name new-name)
"`bookmark-rename' advice to prevent renaming active bufferlo bookmarks.
OLDFN OLD-NAME NEW-NAME"
(interactive)
(when (called-interactively-p 'interactive)
(setq old-name (bookmark-completing-read "Old bookmark name")))
(if-let* ((abm (assoc old-name (bufferlo--active-bookmarks))))
(user-error
"%s is an active bufferlo bookmark--close its frame/tab, or clear it before renaming"
old-name)
(if (called-interactively-p 'interactive)
(funcall-interactively oldfn old-name new-name)
(funcall oldfn old-name new-name))))
;; (defun bookmark-delete (bookmark-name &optional batch)
(defun bufferlo--bookmark-delete-advice (oldfn &optional bookmark-name batch)
"`bookmark-delete' advice to prevent deleting active bufferlo bookmarks.
OLDFN BOOKMARK-NAME BATCH"
(interactive)
(when (called-interactively-p 'interactive)
(setq bookmark-name (bookmark-completing-read "Delete bookmark"
bookmark-current-bookmark)))
(if-let* ((abm (assoc bookmark-name (bufferlo--active-bookmarks))))
(user-error
"%s is an active bufferlo bookmark--close its frame/tab, or clear it before deleting"
bookmark-name)
(if (called-interactively-p 'interactive)
(funcall-interactively oldfn bookmark-name batch)
(funcall oldfn bookmark-name batch))))
;; (defun bookmark-delete-all (&optional no-confirm)
;; Leave this alone for now. It does prompt for confirmation.
;;; Aliases:
(defalias 'bufferlo-bms-load 'bufferlo-bookmarks-load-interactive)
(defalias 'bufferlo-bms-save 'bufferlo-bookmarks-save-interactive)
(defalias 'bufferlo-bms-close 'bufferlo-bookmarks-close-interactive)
(defalias 'bufferlo-bm-raise 'bufferlo-bookmark-raise)
(defalias 'bufferlo-bm-save 'bufferlo-bookmark-save-current)
(defalias 'bufferlo-bm-load 'bufferlo-bookmark-load-current)
(defalias 'bufferlo-bm-close 'bufferlo-bookmark-close-current)
(defalias 'bufferlo-bm-tab-save 'bufferlo-bookmark-tab-save)
(defalias 'bufferlo-bm-tab-save-curr 'bufferlo-bookmark-tab-save-current)
(defalias 'bufferlo-bm-tab-load 'bufferlo-bookmark-tab-load)
(defalias 'bufferlo-bm-tab-load-curr 'bufferlo-bookmark-tab-load-current)
(defalias 'bufferlo-bm-tab-close-curr 'bufferlo-tab-close-kill-buffers)
(defalias 'bufferlo-bm-frame-save 'bufferlo-bookmark-frame-save)
(defalias 'bufferlo-bm-frame-save-curr 'bufferlo-bookmark-frame-save-current)
(defalias 'bufferlo-bm-frame-load 'bufferlo-bookmark-frame-load)
(defalias 'bufferlo-bm-frame-load-curr 'bufferlo-bookmark-frame-load-current)
(defalias 'bufferlo-bm-frame-load-merge 'bufferlo-bookmark-frame-load-merge)
(defalias 'bufferlo-bm-frame-close-curr 'bufferlo-delete-frame-kill-buffers)
(defalias 'bufferlo-set-save 'bufferlo-set-save-interactive)
(defalias 'bufferlo-set-save-curr 'bufferlo-set-save-current-interactive)
(defalias 'bufferlo-set-add 'bufferlo-set-add-interactive)
(defalias 'bufferlo-set-remove 'bufferlo-set-remove-interactive)
(defalias 'bufferlo-set-load 'bufferlo-set-load-interactive)
(defalias 'bufferlo-set-close 'bufferlo-set-close-interactive)
(defalias 'bufferlo-set-clear 'bufferlo-set-clear-interactive)
(defalias 'bufferlo-set-list 'bufferlo-set-list-interactive)
(provide 'bufferlo)
;;; bufferlo.el ends here
|