aboutsummaryrefslogtreecommitdiff
path: root/config.org
blob: de73227bcc96f429d61bf24bd278ddb80519dad1 (plain)
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
#+title: My Literate Emacs Configuration
#+author: Daniel Pinkston
#+export_file_name: Literate Emacs
# #+setupfile: ~/Notes/denote/latex-setups/setup.org
#+setupfile: ~/Notes/denote/latex-setups/latex-setup.org
#+latex_compiler: xelatex
#+options: toc:t num:t
#+LATEX_SRC_BLOCK_BACKEND: listings

* Startup
** Early initialization
The ~early-init.el~ removes all the visual fluff like scrollbars and sets up very fundamental Emacs settings that need to be loaded before everything else.

#+begin_src emacs-lisp :tangle early-init.el
(setq inhibit-startup-message t)
(setq inhibit-startup-screen t)

(setq frame-resize-pixelwise t
      frame-inhibit-implied-resize t
      use-dialog-box t ; only for mouse events, which I seldom use
      use-file-dialog nil
      use-short-answers t
      inhibit-x-resources t
      inhibit-startup-echo-area-message user-login-name
      inhibit-startup-buffer-menu t)

(setq mode-line-misc-info
      (delete (assoc 'minor-mode-alist mode-line-misc-info) mode-line-misc-info))

(add-hook 'after-init-hook (lambda () (set-frame-name "home")))

;; Modes
(scroll-bar-mode -1)
(tool-bar-mode -1)
(tooltip-mode -1)
(set-fringe-mode 10)
(menu-bar-mode -1)
(tool-bar-mode 0)

;; settings for windows
(setq focus-follows-mouse t)

(add-to-list 'default-frame-alist '(alpha . (95 . 95)))

;; garbage collection
(setq gc-cons-threshold most-positive-fixnum
      gc-cons-percentage 0.5)

(defvar bard-emacs--file-name-handler-alist file-name-handler-alist)
(defvar bard-emacs--vc-handled-backends vc-handled-backends)

(add-hook 'emacs-startup-hook
          (lambda ()
            (setq gc-cons-threshold (* 1000 1000 8)
                  gc-cons-percentage 0.1
                  file-name-handler-alist bard-emacs--file-name-handler-alist
                  vc-handled-backends bard-emacs--vc-handled-backends)))

;; Package cache
(setq package-enable-at-startup t)

#+end_src

** Initialization
#+begin_src emacs-lisp :tangle init.el
  ;;; init.el --- init.el -*- lexical-binding: t -*-

  ;; Author: BardofSprites
  ;; Maintainer: BardofSprites
  ;; Version: 0.1.0
  ;; Package-Requires: ((emacs "28.2"))

  ;; This file is not part of GNU Emacs

  ;; 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:

  ;;; Code:

  ;; fix keymap
  (repeat-mode t)
  (global-set-key (kbd "C-z") nil)
  (global-set-key (kbd "M-z") nil)

  (global-set-key (kbd "C-z C-z") #'suspend-emacs)


  ;; Add the directories to the load path
  (mapc
   (lambda (string)
     (add-to-list 'load-path (locate-user-emacs-file string)))
   '("bard-elisp" "bard-emacs-modules" "old-ada"))

  ;; Clipboard saving
  (setq select-enable-clipboard t)

  ;; custom file is evil
  (setq custom-file (make-temp-file "emacs-custom-"))

  ;; No Backups
  (setq auto-save-default nil)
  (setq make-backup-files nil)
  (setq confirm-kill-emacs 'y-or-n-p)

  ;; native comp enabled
  (when (native-comp-available-p)
    (setq native-compile-prune-cache t))

  ;; enable/disable commands
  (mapc
   (lambda (command)
     (put command 'disabled nil))
   '(list-timers narrow-to-region narrow-to-page upcase-region downcase-region))

  (mapc
   (lambda (command)
     (put command 'disabled t))
   '(eshell project-eshell overwrite-mode iconify-frame diary))

  ;;; Packages
  (setq package-archives '(("elpa" . "https://elpa.gnu.org/packages/")
                           ("nongnu" . "https://elpa.nongnu.org/nongnu/")
                           ("melpa" . "https://melpa.org/packages/")))

  ;;; MACROS

  (defmacro bard/make-abbrev (table &rest definitions)
    "Expand abbrev DEFINITIONS for the given TABLE.
  DEFINITIONS is a sequence of (i) string pairs mapping the
  abbreviation to its expansion or (ii) a string and symbol pair
  making an abbreviation to a function."
    (declare (indent 1))
    (unless (zerop (% (length definitions) 2))
      (error "Uneven number of key+command pairs"))
    `(if (abbrev-table-p ,table)
         (progn
           ,@(mapcar
              (lambda (pair)
                (let ((abbrev (nth 0 pair))
                      (expansion (nth 1 pair)))
                  (if (stringp expansion)
                      `(define-abbrev ,table ,abbrev ,expansion)
                    `(define-abbrev ,table ,abbrev "" ,expansion))))
              (seq-split definitions 2)))
       (error "%s is not an abbrev table" ,table)))

  (require 'bard-emacs-anki)
  (require 'bard-emacs-calendar)
  (require 'bard-emacs-completion)
  (require 'bard-emacs-dired)
  ;; (require 'bard-emacs-email)
  (require 'bard-emacs-media)
  (require 'bard-emacs-eshell)
  (require 'bard-emacs-essentials)
  ;; (require 'bard-emacs-keyboard)
  (require 'bard-emacs-modeline)
  (require 'bard-emacs-org)
  (require 'bard-emacs-prog)
  (require 'bard-emacs-theme)
  (require 'bard-emacs-ui)
  (require 'bard-emacs-web)
  (require 'bard-emacs-window)
  (require 'bard-emacs-writing)

  (provide 'init)
  (put 'eshell 'disabled nil)

#+end_src
* Modules
** bard-emacs-anki
#+begin_quote
[[https://apps.ankiweb.net/][Anki]] is a flashcard program that helps you spend more time on challenging material, and less on what you already know.
#+end_quote

This is an emacs package that allows me to write flashcards in org mode. I talked about it in my EmacsConf 2024 presentation titled: [[https://www.bardman.dev/technology/emacsconf-2024/][A example of a cohesive student workflow in Emacs]].

#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-anki.el :mkdirp yes
(use-package anki-editor
  :ensure t
  :after org
  :bind (:map org-mode-map
              ("C-c M-i" . bard/anki-editor-cloze-region-auto-incr)
              ("C-c M-I" . bard/anki-editor-cloze-region-dont-incr)
              ("C-c M-r" . bard/anki-editor-reset-cloze-number)
              ("C-c M-p" . anki-editor-push-new-notes)
              ("C-c M-P" . anki-editor-push-notes))

  :hook (org-capture-after-finalize . bard/anki-editor-reset-cloze-number) ; Reset cloze-number after each capture.
  :config
  (setq anki-editor-create-decks t ;; Allow anki-editor to create a new deck if it doesn't exist
        anki-editor-org-tags-as-anki-tags t)

  (defun bard/anki-editor-cloze-region-auto-incr (&optional arg)
    "Cloze region without hint and increase card number."
    (interactive)
    (anki-editor-cloze-region my-anki-editor-cloze-number "")
    (setq my-anki-editor-cloze-number (1+ my-anki-editor-cloze-number))
    (forward-sexp))
  (defun bard/anki-editor-cloze-region-dont-incr (&optional arg)
    "Cloze region without hint using the previous card number."
    (interactive)
    (anki-editor-cloze-region (1- my-anki-editor-cloze-number) "")
    (forward-sexp))
  (defun bard/anki-editor-reset-cloze-number (&optional arg)
    "Reset cloze number to ARG or 1"
    (interactive)
    (setq my-anki-editor-cloze-number (or arg 1)))
  ;; Initialize
  (bard/anki-editor-reset-cloze-number)
  )

(provide 'bard-emacs-anki)
#+end_src

** bard-emacs-calendar
*** Load required libraries
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-calendar.el :mkdirp yes
  (require 'bard-calendar)
  (require 'hi-lock)
#+end_src

*** Org directory and agenda files
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-calendar.el :mkdirp yes
  (setq org-directory "~/Notes/denote/")
  ;; symlinked file to shorten denote file name in agenda buffers
  (setq org-agenda-files (list "~/Notes/denote/todo.org" "~/Notes/denote/uni.org"))
#+end_src

*** Org Calendar
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-calendar.el :mkdirp yes
  ;; Calendar
  (use-package calendar-mode
    :config
    (setq calendar-holidays (append calendar-holidays russian-holidays))
    :hook
    (calendar-today-visible . calendar-mark-today)
    (calendar-mode . denote-journal-calendar-mode)
    (calendar-today-visible . calendar-mark-holidays))
#+end_src

**** Orthodox Christian Holidays
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-calendar.el :mkdirp yes
  (use-package orthodox-christian-new-calendar-holidays
    :ensure t
    :config
    (setq holiday-other-holidays (append holiday-other-holidays orthodox-christian-new-calendar-holidays))

    (setq holiday-bahai-holidays nil
          holiday-christian-holidays nil
          holiday-islamic-holidays nil))
#+end_src

*** Org todo keywords
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-calendar.el :mkdirp yes
  ;; Org todo keywords - changed to using hl-todo faces fixed by modus/ef themes
  (setq org-todo-keywords
        '((sequence "TODO(t)" "EXTRA(e)" "INPROG(i)" "|" "DONE(d)" "KILLED(k)")
          (sequence "MEET(m)" "TENT(T)" "|" "MET(M)" "NOGO(n)")))

  (setq org-todo-keyword-faces
        '(("EXTRA" . (:inherit warning))
          ("TENT" . (:inherit warning))
          ("MEET" . (:inherit warning underline bold))
          ("INPROG" . (:inherit hi-yellow :weight bold))))

  (setq org-enforce-todo-dependencies t)

  ;; Automatically clock in
  (add-hook 'org-after-todo-state-change-hook #'bard/auto-clock-in)
#+end_src

*** Org clocking
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-calendar.el :mkdirp yes
  ;; clock tables
  (setq org-clock-clocktable-default-properties '(:maxlevel 7 :scope agenda)
        org-clock-persist 'history
        org-clock-mode-line-total 'current)
  (org-clock-persistence-insinuate)

  (use-package org
    ;; not really show what this does anymore
    :demand t
    :hook
    ((org-clock-out . bard/org-clock-update-mode-line)))
#+end_src

*** Org agenda settings
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-calendar.el :mkdirp yes
  (global-set-key (kbd "<f1>") 'bard/default-agenda)
  (global-set-key (kbd "M-<f1>") 'bard/choose-agenda)

  (setq org-agenda-include-diary t)
  (setq org-agenda-custom-commands
        `(("D" "Daily agenda and top priority tasks"
           ((tags-todo "!TODO"
                       ((org-agenda-overriding-header "Unscheduled Tasks \n")
                        (org-agenda-skip-function '(org-agenda-skip-entry-if 'timestamp))))
            (agenda "" ((org-agenda-span 1)
                        (org-agenda-start-day nil)
                        (org-deadline-warning-days 14)
                        ;; (org-scheduled-past-days 0)
                        (org-agenda-day-face-function (lambda (date) 'org-agenda-date))
                        (org-agenda-format-date "%A %-e %B %Y")
                        (org-agenda-overriding-header "Today's agenda \n")))
            (agenda "" ((org-agenda-span 8)
                        (org-calendar-holiday)
                        (org-deadline-warning-days 0)
                        (org-agenda-skip-function '(org-agenda-skip-entry-if 'todo 'done))
                        (org-agenda-overriding-header "Upcoming this week \n")))))
          ("Y" "Yearly view for all tasks"
           ((agenda "" ((org-agenda-span 365)
                        (org-deadline-warning-days 2)
                        (org-agenda-skip-function '(org-agenda-skip-entry-if 'todo 'done))
                        (org-agenda-overriding-header "Upcoming this Year\n")))))
          ("M" "Monthly view for all tasks"
           ((agenda "" ((org-agenda-span 31)
                        (org-deadline-warning-days 2)
                        (org-agenda-skip-function '(org-agenda-skip-entry-if 'todo 'done))
                        (org-agenda-overriding-header "Upcoming this month\n")))))))
#+end_src

*** Org habit
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-calendar.el :mkdirp yes
  (use-package org-habit
    :after org-agenda
    :config
    (setq org-habit-show-done-always-green t
          org-habit-show-habits t
          org-habit-show-all-today t))
#+end_src

*** Provide module
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-calendar.el :mkdirp yes
  (provide 'bard-emacs-calendar)
#+end_src

** bard-emacs-completion
*** Vertico completion framework
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  ;; minibuffer completion
  (use-package vertico
    :ensure t
    :config
    (setq vertico-scroll-margin 0)
    (setq vertico-cycle nil)
    (setq vertico-resize nil)
    (vertico-mode t)

    (with-eval-after-load 'rfn-eshadow
      ;; This works with `file-name-shadow-mode' enabled.  When you are in
      ;; a sub-directory and use, say, `find-file' to go to your home '~/'
      ;; or root '/' directory, Vertico will clear the old path to keep
      ;; only your current input.
      (add-hook 'rfn-eshadow-update-overlay-hook #'vertico-directory-tidy)))
#+end_src

*** Rfn eshadow
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (use-package rfn-eshadow
    :ensure nil
    :hook (minibuffer-setup . cursor-intangible-mode)
    :config
    (setq resize-mini-windows t)
    (setq read-answer-short t) ; also check `use-short-answers' for Emacs28
    (setq echo-keystrokes 0.25)

    ;; Do not allow the cursor to move inside the minibuffer prompt.  I
    ;; got this from the documentation of Daniel Mendler's Vertico
    ;; package: <https://github.com/minad/vertico>.
    (setq minibuffer-prompt-properties
          '(read-only t cursor-intangible t face minibuffer-prompt))

    ;; MCT has a variant of this built-in.
    (defun crm-indicator (args)
      (cons (format "[`completing-read-multiple': %s]  %s"
                    (propertize
                     (replace-regexp-in-string
                      "\\`\\[.*?]\\*\\|\\[.*?]\\*\\'" ""
                      crm-separator)
                     'face 'error)
                    (car args))
            (cdr args)))

    (advice-add #'completing-read-multiple :filter-args #'crm-indicator)

    (file-name-shadow-mode 1))
#+end_src

*** Marginalia
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (use-package marginalia
    :ensure t
    :init
    (marginalia-mode 1))
#+end_src

*** Orderless
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (use-package orderless
    :ensure t
    :config
    (setq completion-styles '(orderless basic)))
#+end_src

*** Tab completion
For a long time I really struggled with Emacs tab completion. It still only kind of works most of the time, but I don't want to tweak it to make it work. It works really well with LSP and using Lisp REPL's for languages like Clojure, Common Lisp, and Scheme. However for some languages like Haskell, without the LSP (which hasn't worked for me in the past), it does not work to the extent that I believe it should. Completion is something I'm kind of jealous of when it comes to Neovim vs Emacs.

**** UI for tab completion
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (use-package corfu
    :ensure t
    :init (global-corfu-mode)
    ;; I also have (setq tab-always-indent 'complete) for TAB to complete
    ;; when it does not need to perform an indentation change.
    :bind (:map corfu-map ("<tab>" . corfu-complete))
    :config
    (setq corfu-preview-current nil)
    (setq corfu-min-width 20)

    (setq corfu-popupinfo-delay nil)
    (corfu-popupinfo-mode 1) ; shows documentation after `corfu-popupinfo-delay'

    ;; Sort by input history (no need to modify `corfu-sort-function').
    (with-eval-after-load 'savehist
      (corfu-history-mode 1)
      (add-to-list 'savehist-additional-variables 'corfu-history)))
#+end_src

**** Compilation at point functions (CAPF)
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (use-package cape
    :ensure t
    :init
    (add-to-list 'completion-at-point-functions #'cape-file)
    (add-to-list 'completion-at-point-functions #'cape-dabbrev)
    (add-to-list 'completion-at-point-functions #'cape-keyword)
    (add-to-list 'completion-at-point-functions #'cape-abbrev))

  (use-package clang-capf
    :ensure t
    :config
    (defun bard/clang-capf-init ()
      "Add `clang-capf' to `completion-at-point-functions'."
      (add-hook 'completion-at-point-functions #'clang-capf nil t))

    (add-hook 'c-mode-hook #'bard/clang-capf-init))
#+end_src

**** Completion styles
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (use-package minibuffer
    :config
  ;;;; Completion styles
    (setq completion-styles '(basic substring initials flex orderless))

    (setq completion-category-defaults nil)

    (setq completion-category-overrides
          '((file (styles . (basic partial-completion orderless)))
            (bookmark (styles . (basic substring)))
            (library (styles . (basic substring)))
            (embark-keybinding (styles . (basic substring)))
            (imenu (styles . (basic substring orderless)))
            (consult-location (styles . (basic substring orderless)))
            (kill-ring (styles . (emacs22 orderless)))
            (eglot (styles . (emacs22 substring orderless))))))
#+end_src

*** Minibuffer enhancements
**** Consult
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (use-package consult
    :ensure t
    :defer 2
    :bind*
    ("C-x r b" . consult-bookmark)
    ("M-g M-g" . consult-goto-line)
    ("C-x b" . consult-buffer)
    ("M-s M-f" . consult-find)
    ("M-s M-g" . consult-grep)
    ("M-s M-h" . consult-history)
    ("M-s M-y" . consult-yank-pop)
    ("M-s M-o" . consult-outline)
    ("M-s M-l" . consult-line)
    ("M-s M-k" . consult-kmacro)
    ("M-s M-r" . consult-register)
    :config
    (setq consult-find-args
          (concat "find . -not ( "
                  "-path */.git* -prune "
                  "-or -path */.cache* -prune )")))
#+end_src

**** Embark
:PROPERTIES:
:ID:       ce728534-8fa7-4d4a-96e3-71cb02fc9af4
:END:
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (use-package embark
    :ensure t
    :bind*
    (("C-," . bard-embark-act-no-quit)
     ("C-." . bard-embark-act-quit))
    :config
    (require 'bard-embark)
    (setq embark-keymap-alist
          '((buffer bard-embark-buffer-map)
            (command bard-embark-command-map)
            (expression bard-embark-expression-map)
            (file bard-embark-file-map)
            (function bard-embark-function-map)
            (identifier bard-embark-identifier-map)
            (package bard-embark-package-map)
            (region bard-embark-region-map)
            (symbol bard-embark-symbol-map)
            (url bard-embark-url-map)
            (variable bard-embark-variable-map)
            (t embark-general-map)))

    (defun bard-embark-act-no-quit ()
      "Call `embark-act' but do not quit after the action."
      (interactive)
      (let ((embark-quit-after-action nil))
        (call-interactively #'embark-act)))

    (defun bard-embark-act-quit ()
      "Call `embark-act' and quit after the action."
      (interactive)
      (let ((embark-quit-after-action t))
        (call-interactively #'embark-act))
      (when (and (> (minibuffer-depth) 0)
                 (derived-mode-p 'completion-list-mode))
        (abort-recursive-edit)))


    (setq embark-confirm-act-all nil)
    ;; The prot-embark.el has an advice to further simplify the
    ;; minimal indicator.  It shows cycling, which I never want to see
    ;; or do.
    (setq embark-mixed-indicator-both nil)
    (setq embark-mixed-indicator-delay 0.1)
    (setq embark-indicators '(embark-mixed-indicator embark-highlight-indicator))
    (setq embark-verbose-indicator-nested nil) ; I think I don't have them, but I do not want them either
    (setq embark-verbose-indicator-buffer-sections '(bindings))
    (setq embark-verbose-indicator-excluded-actions
          '(embark-cycle embark-act-all embark-collect embark-export embark-insert)))
#+end_src

*** Save minibuffer history (~savehist~)
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (setq savehist-file (locate-user-emacs-file "savehist"))
  (setq history-length 100)
  (setq history-delete-duplicates t)
  (setq savehist-save-minibuffer-history t)
  (setq savehist-additional-variables '(register-alist kill-ring))
  (savehist-mode t)
#+end_src

*** Recent files (~recentf~)
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (recentf-mode t)
  (setq recentf-save-file (locate-user-emacs-file "recentf"))

  ;; save cursor place for code files
  (add-hook 'prog-mode-hook #'save-place-local-mode)
#+end_src

*** Abbreviations (~(d)abbrev-mode~)
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (setq abbrev-file-name (locate-user-emacs-file "abbrevs"))
  (setq only-global-abbrevs nil)

  (bard/make-abbrev global-abbrev-table
    "meweb" "https://bardman.dev"
    "megit" "https://github.com/BardofSprites"
    "meyt" "https://www.youtube.com/@bardmandev"
    "protweb" "https://protesilaos.com/")

  (bard/make-abbrev text-mode-abbrev-table
    "asciidoc"       "AsciiDoc"
    "auctex"         "AUCTeX"
    "cafe"           "café"
    "cliche"         "cliché"
    "clojurescript"  "ClojureScript"
    "emacsconf"      "EmacsConf"
    "github"         "GitHub"
    "gitlab"         "GitLab"
    "javascript"     "JavaScript"
    "latex"          "LaTeX"
    "libreplanet"    "LibrePlanet"
    "linkedin"       "LinkedIn"
    "paypal"         "PayPal"
    "sourcehut"      "SourceHut"
    "texmacs"        "TeXmacs"
    "typescript"     "TypeScript"
    "visavis"        "vis-à-vis"
    "vscode"         "Visual Studio Code"
    "youtube"        "YouTube"
    "Результат"      "=Результат Сегодняшний Битвый="
    "asf"            "and so on and so forth"
    "paragraph"      "¶"
    "em"             "—"
    "ua"             "↑"
    "da"             "↓"
    "ra"             "→"
    "la"             "←"
    "iff"            "⇔"
    "imp"            "⇒"
    "tf"             "∴"
    "xmonad"         "XMonad"
    "xmobar"         "XMobar")

  (dolist (hook '(text-mode-hook prog-mode-hook git-commit-mode-hook))
    (add-hook hook #'abbrev-mode))

  (remove-hook 'save-some-buffers-functions #'abbrev--possibly-save)
#+end_src

*** Provide module
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-completion.el :mkdirp yes
  (provide 'bard-emacs-completion)
  ;;; bard-emacs-completion.el ends here
#+end_src

** bard-emacs-dired
*** Dired subtree
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-dired.el :mkdirp yes
  (use-package dired-subtree
    :ensure t
    :config
    (setq dired-subtree-use-backgrounds nil))
#+end_src

*** Dired file preview
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-dired.el :mkdirp yes
  (use-package dired-preview
    :ensure t
    :bind
    (:map dired-mode-map
          ("P" . dired-preview-mode))
    :config
    (setq dired-preview-delay 0.1))
#+end_src

*** General dired settings
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-dired.el :mkdirp yes
  (use-package dired
    :bind*
    (("C-j" . dired-jump))
    :bind (:map dired-mode-map
                (("E" . emms-add-dired)
                 ("<tab>" . dired-subtree-toggle)
                 ("<backtab>" . dired-subtree-cycle)))
    :config
    (setq dired-guess-shell-alist-user ; those are the suggestions for ! and & in Dired
          '(("\\.\\(png\\|jpe?g\\|tiff\\)" "nsxiv" "feh" "xdg-open")
            ("\\.\\(mp[34]\\|m4a\\|ogg\\|flac\\|webm\\|mkv\\)" "mpv" "xdg-open")
  	      (".gif" "mpv --loop=inf")
            (".*" "xdg-open")))
    (setq dired-dwim-target t)
    (setq dired-listing-switches
          "-AgGFhlv --group-directories-first --time-style=long-iso")
    :hook
    ((dired-mode . dired-hide-details-mode)
     ;; attachments for email through dired
     (dired-mode . turn-on-gnus-dired-mode)))
#+end_src

*** Image dired
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-dired.el :mkdirp yes
  (use-package image-dired
    :bind
    (:map dired-mode-map
          ((")" . image-dired-dired-display-external)
           ("B" . bard/dired-set-background-with-feh)))
    :bind
    ("C-x C-d" . image-dired)
    :bind
    (:map image-dired-thumbnail-mode-map ("B" . bard/image-dired-set-background-with-feh))
    :config
    (define-advice image-dired-display-image (:override (file &optional _ignored))
      (setq file (expand-file-name file))
      (when (not (file-exists-p file))
        (error "No such file: %s" file))
      (let ((buf (get-buffer image-dired-display-image-buffer))
            (cur-win (selected-window)))
        (when buf
          (kill-buffer buf))
        (when-let ((buf (find-file-noselect file nil t)))
          (with-current-buffer buf
            (rename-buffer image-dired-display-image-buffer)
            (if (string-match (image-file-name-regexp) file)
                (image-dired-image-mode)
              ;; Support visiting PDF files.
              (normal-mode))
            (display-buffer buf))
          (select-window cur-win))))

    (setq image-dired-thumbnail-storage 'standard)
    (setq image-dired-external-viewer "nsxiv")
    (setq image-dired-thumb-size 80)
    (setq image-dired-thumb-margin 2)
    (setq image-dired-thumb-relief 0)
    (setq image-dired-thumbs-per-row 4)

    (defun bard/dired-set-background-with-feh ()
      "Set the selected image as the background using feh."
      (interactive)
      (let ((image-file (dired-get-file-for-visit)))
        (start-process "feh" nil "feh" "--bg-fill" image-file)
        (message "Background set to %s" image-file)))

    (defun bard/image-dired-set-background-with-feh ()
      "Set the selected image as the background using feh."
      (interactive)
      (let ((image-file (image-dired-original-file-name)))
        (start-process "feh" nil "feh" "--bg-fill" image-file)
        (message "Background set to %s" image-file))))
#+end_src

*** Running commands on dired files
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-dired.el :mkdirp yes
  ;; Taken from https://superuser.com/a/176629
  (defun bard/dired-do-command (command)
    "Run COMMAND on marked files. Any files not already open will be opened.
  After this command has been run, any buffers it's modified will remain
  open and unsaved."
    (interactive "CRun on marked files M-x ")
    (save-window-excursion
      (mapc (lambda (filename)
              (find-file filename)
              (call-interactively command))
            (dired-get-marked-files))))
#+end_src

*** Dired video thumbnails
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-dired.el :mkdirp yes
  (use-package dired-video-thumbnail
    :ensure t
    :vc (:url "https://github.com/captainflasmr/dired-video-thumbnail"
              :rev :newest)
    :bind (:map dired-mode-map
                ("C-t v" . dired-video-thumbnail)))
#+end_src

*** Provide module
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-dired.el :mkdirp yes
  (provide 'bard-emacs-dired)
#+end_src

** bard-emacs-email
*** Require email library
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-email.el :mkdirp yes
  (require 'bard-email)
#+end_src

*** Notmuch mail client settings
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-email.el :mkdirp yes
  (use-package notmuch
    :ensure t
    :config
    (define-key global-map (kbd "C-c m") #'notmuch)
    (setq notmuch-show-logo t
          notmuch-column-control 1.0
          notmuch-hello-auto-refresh t
          notmuch-hello-recent-searches-max 20
          notmuch-hello-thousands-separator ""
          notmuch-hello-sections '(notmuch-hello-insert-header notmuch-hello-insert-saved-searches notmuch-hello-insert-search notmuch-hello-insert-alltags)
          notmuch-show-all-tags-list t)

    (setq notmuch-search-oldest-first nil)
    (setq notmuch-show-seen-current-message t)

    (defun bard/notmuch-mua-empty-subject-check ()
      "Request confirmation before sending a message with empty subject."
      (when (and (null (message-field-value "Subject"))
                 (not (y-or-n-p "Subject is empty, send anyway? ")))
        (error "Sending message cancelled: empty subject")))
    (add-hook 'message-send-hook 'bard/notmuch-mua-empty-subject-check))
#+end_src

**** Notmuch saved searches
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-email.el :mkdirp yes
  (setq notmuch-show-empty-saved-searches t)
  (setq notmuch-saved-searches
        `(( :name "📥 inbox (all-mail)"
            :query "tag:inbox"
            :sort-order newest-first
            :key ,(kbd "i"))
          ( :name "💬 unread (inbox)"
            :query "tag:unread and tag:inbox"
            :sort-order newest-first
            :key ,(kbd "u"))
          ( :name "📝 To Do"
            :query "tag:todo"
            :sort-order oldest-first
            :key ,(kbd "t"))
          ( :name "🚩 flagged"
            :query "tag:flag"
            :sort-order newest-first
            :key ,(kbd "f"))
          ( :name "🐃 contributions"
            :query "tag:unread and tag:contrib"
            :sort-order newest-first
            :key ,(kbd "c"))
          ( :name "🐧 linux-related"
            :query "tag:unread and tag:linux"
            :sort-order newest-first
            :key ,(kbd "l"))
          ( :name "🚂 emacs developement"
            :query "tag:unread and tag:contrib"
            :sort-order newest-first
            :key ,(kbd "ed"))
          ( :name "🎨 emacs humanities"
            :query "tag:unread and tag:emacs-humanities"
            :sort-order newest-first
            :key ,(kbd "eh"))
          ( :name "🦄 emacs org-mode"
            :query "tag:unread and tag:emacs-org"
            :sort-order newest-first
            :key ,(kbd "eo"))))
#+end_src

**** Tagging keys
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-email.el :mkdirp yes
  (setq notmuch-tagging-keys
        `((,(kbd "d") prot-notmuch-mark-delete-tags "💥 Mark for deletion")
          (,(kbd "f") prot-notmuch-mark-flag-tags "🚩 Flag as important")
          (,(kbd "s") prot-notmuch-mark-spam-tags "🔥 Mark as spam")
          (,(kbd "r") ("-unread") "👁️‍🗨️ Mark as read")
          (,(kbd "u") ("+unread") "🗨️ Mark as unread")))

  (setq notmuch-archive-tags '("+archive")
        notmuch-message-replied-tags '("+replied")
        notmuch-message-forwarded-tags '("+forwarded")
        notmuch-show-mark-read-tags '("-unread")
        notmuch-draft-tags '("+draft")
        notmuch-draft-folder "drafts"
        notmuch-draft-save-plaintext 'ask)
#+end_src

*** Notmuch modeline indicator
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-email.el :mkdirp yes
  (use-package notmuch-indicator
    :ensure t
    :after notmuch
    :config
    (setq notmuch-indicator-args
          '(( :terms "tag:unread and tag:inbox"
              :label "[U] "
              :label-face prot-modeline-indicator-green
              :counter-face prot-modeline-indicator-green)
            ( :terms "tag:unread and tag:linux"
              :label "[L] "
              :label-face prot-modeline-indicator-cyan
              :counter-face prot-modeline-indicator-cyan)
            ( :terms "tag:unread and tag:emacs"
              :label "[E] "
              :label-face prot-modeline-indicator-blue
              :counter-face prot-modeline-indicator-blue))

          notmuch-indicator-refresh-count (* 60 3)
          notmuch-indicator-hide-empty-counters t
          notmuch-indicator-force-refresh-commands '(notmuch-refresh-this-buffer))
    (setq notmuch-indicator-add-to-mode-line-misc-info nil)
    (notmuch-indicator-mode t))
#+end_src

*** Org links for notmuch
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-email.el :mkdirp yes
  (use-package ol-notmuch
    :ensure t
    :after notmuch)
#+end_src

*** Send mail and smtp settings
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-email.el :mkdirp yes
  ;; use msmtp
  (setq sendmail-program "/usr/bin/msmtp"
        message-send-mail-function 'message-send-mail-with-sendmail
        message-sendmail-f-is-evil nil
        mail-specify-envelope-from t
        message-sendmail-envelope-from 'header
        mail-envelope-from t)
#+end_src

*** Provide module
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-email.el :mkdirp yes
  (provide 'bard-emacs-email)
#+end_src

** bard-emacs-eshell
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-eshell.el :mkdirp yes
(use-package eshell
  :ensure nil
  :bind
  (("C-z e" . eshell-switcher))
  :config
  (require 'bard-eshell)
  (require 'ffap)
  ;; (setq eshell-banner-message "Time for another recreational programming session.\n\n")
  (setq eshell-banner-message
        '(format "%s %s\n %s\n"
                 (propertize (format " %s " (string-trim (buffer-name)))
                             'face 'mode-line-highlight)
                 (propertize (current-time-string)
                             'face 'font-lock-keyword-face)
         (propertize "Time for another recreational programming session."
                     'face 'warning)))
  (setq bard/eshell-aliases
        '((g   . magit)
          (gl  . magit-log)
          (d   . dired)
          (o   . find-file)
          (oo . find-file-other-window)
          (vim . find-file)
          (l  . (lambda () (eshell/ls '-la)))
          (eshell/clear . eshell/clear-scrollback)))

  (mapc (lambda (alias)
          (defalias (car alias) (cdr alias)))
        bard/eshell-aliases))

(use-package eshell
  :ensure nil
  :after esh-mode
  :bind
  (:map eshell-mode-map
        ("C-c C-e" . prot-eshell-export)
        ("M-k"     . eshell-kill-input)
        ("C-c C-d"   . prot-eshell-complete-recent-dir)
        ("C-c C-h"   . prot-eshell-narrow-output-highlight-regexp)
        ("C-c C-f"   . bard/eshell-find-file-at-point)))

(provide 'bard-emacs-eshell)
#+end_src

** bard-emacs-essentials
*** Writable grep buffers (wgrep)
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  (use-package wgrep
    :ensure t
    :bind
    (:map wgrep-mode-map
  	    ("C-x C-s" . wgrep-save-all-buffers)
          ("C-x C-q" . wgrep-change-to-wgrep-mode)
          ("C-c C-c" . wgrep-finish-edit))
    :bind
    (:map grep-mode-map
          ("e" . wgrep-change-to-wgrep-mode)))
#+end_src

*** Multiple cursors
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  (use-package multiple-cursors
    :ensure t
    :config
    (setq mc/always-run-for-all t)
    :bind
    (("C-S-c C-S-c" . mc/edit-lines)
     ("C->" . mc/mark-next-like-this)
     ("C-<" . mc/mark-previous-like-this)
     ("C-c C" . mc/mark-all-like-this)
     ("C-\"". mc/skip-to-next-like-this)
     ("C-;" . mc/skip-to-previous-like-this)))
#+end_src

*** Expand region
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  (use-package expand-region
    :ensure t
    :bind ("C-=" . er/expand-region))
#+end_src

*** Substitute (simple text substitution)
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  (use-package substitute
    :ensure t
    :bind
    (("C-c s b" . substitute-target-below-point)
     ("C-c s a" . substitute-target-above-point)
     ("C-c s d" . substitute-target-in-defun)
     ("C-c s s" . substitute-target-in-buffer)))
#+end_src

*** Moving text around
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  (use-package move-text
    :ensure t
    :bind
    (:map prog-mode-map
          ("M-p" . move-text-up)
          ("M-n" . move-text-down)))
#+end_src

*** Saving session with desktop
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  ;; Desktop mode/session saving
  (setq desktop-path '("~/.emacs.d/desktop")
        desktop-dirname "~/.emacs.d/desktop/"
        desktop-base-file-name "emacs-desktop"
        desktop-save t
        desktop-restore-eager t
        desktop-restore-=frams t
        desktop-restory-in-current-display t
        desktop-files-not-to-save "\(^$\\|\\*scratch\\*\\|\\*Messages\\*\\|\\*dashboard\\*\\|\\*Async-native-compile-log\\*|\\*Music\\*)")
  ;; (desktop-save-mode t)
  (global-set-key (kbd "C-z s") 'desktop-save-in-desktop-dir)
  (global-set-key (kbd "C-z r") 'desktop-read)

#+end_src

*** Scratch buffers
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  ;; Text Scratch buffers
  (defun bard/new-org-buffer ()
    (interactive)
    (let ((xbuf (generate-new-buffer "*org*")))
      (switch-to-buffer xbuf)
      (funcall (quote org-mode))
      (text-scale-increase 1.5)
      xbuf))
  (define-key global-map (kbd "M-=") #'bard/new-org-buffer)

  (defun bard/new-plain-buffer ()
    (interactive)
    (let ((xbuf (generate-new-buffer "*plain*")))
      (switch-to-buffer xbuf)
      (text-scale-increase 1.5)
      xbuf))

  (define-key global-map (kbd "M--") #'bard/new-plain-buffer)

  ;; elisp scratch buffer

  (defun bard/new-elisp-buffer ()
    (interactive)
    (let ((xbuf (generate-new-buffer "*elisp*")))
      (switch-to-buffer xbuf)
      (funcall (quote emacs-lisp-mode))
      (text-scale-increase 1.5)
      xbuf))
#+end_src

*** Terminals
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  (defun bard/open-terminal-in-current-directory ()
    "Open a terminal in the current working directory."
    (interactive)
    (let ((default-directory default-directory))
      (term "/usr/bin/env bash")))

  ;; (define-key global-map (kbd "C-t") #'bard/open-terminal-in-current-directory)
  (define-key global-map (kbd "C-z t") #'bard/open-terminal-in-current-directory)

  (defun bard/open-terminal-emulator ()
    "Open a terminal in the current working directory."
    (interactive)
    (let ((default-directory default-directory))
      (start-process "st terminal" nil "st")))

  (define-key global-map (kbd "C-z C-t") 'bard/open-terminal-emulator)

  (define-key global-map (kbd "C-z C-s") #'bard/new-elisp-buffer)
#+end_src

*** Time Management
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  ;; Modeline
  (setq display-time-format "%Y-%m-%d (%a) %H:%M")
  (setq display-time-interval 60)
  (setq display-time-default-load-average nil)
  (setq display-time-mail-directory nil)
  (setq display-time-mail-function nil)
  (setq display-time-use-mail-icon nil)
  (setq display-time-mail-string nil)
  (setq display-time-mail-face nil)
  (setq display-time-string-forms
        '((propertize
           (format-time-string display-time-format now)
           'face 'display-time-date-and-time
           'help-echo (format-time-string "%a %b %e, %Y" now))
          " "))
  (display-time-mode 1)

  ;; world clock
  (setq world-clock-list
        '(("America/New_York" "New York")
  	    ("Europe/Moscow" "Moscow")
  	    ("Europe/London" "London")
  	    ("Asia/Tokyo" "Tokyo")))

  (setq world-clock-time-format "%Y-%m-%d %B (%A) %R %Z")

  ;; timer package
  (use-package tmr
    :ensure t
    :config
    (setq tmr-notification-urgency 'normal)
    (setq tmr-sound-file nil)
    (setq tmr-timer-finished-functions '(tmr-notification-notify
                                         tmr-print-message-for-finished-timer
                                         tmr-acknowledge-minibuffer))
    (setq tmr-descriptions-list 'tmr-description-history)
    (define-key global-map (kbd "C-c t l") 'tmr-tabulated-view)
    (define-key global-map (kbd "C-c t t") #'tmr)
    (define-key global-map (kbd "C-c t T") #'tmr-with-description)
    (define-key global-map (kbd "C-c t l") #'tmr-tabulated-view)
    (define-key global-map (kbd "C-c t c") #'tmr-clone)
    (define-key global-map (kbd "C-c t k") #'tmr-cancel)
    (define-key global-map (kbd "C-c t s") #'tmr-reschedule)
    (define-key global-map (kbd "C-c t e") #'tmr-edit-description)
    (define-key global-map (kbd "C-c t r") #'tmr-remove)
    (define-key global-map (kbd "C-c t R") #'tmr-remove-finished))
#+end_src

*** Running emacs as server
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  (require 'server)
  (setq server-client-instructions nil)
  (unless (server-running-p)
    (server-start))
#+end_src

*** Provide module
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-essentials.el :mkdirp yes
  (provide 'bard-emacs-essentials)
#+end_src

** bard-emacs-media
*** EMMS music player
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-media.el :mkdirp yes
  (use-package emms
    :ensure t
    ;; :demand t
    :bind
    (:map emms-playlist-mode-map
          ("A" . emms-add-directory)
          ("l" . emms-add-playlist)
          ("T" . emms-add-directory-tree)
          ("F" . emms-add-file)
          ("U" . emms-add-url)
          ("L" . emms-toggle-repeat-track)
          ("<mouse-3>" . emms-pause)
          ("<SPC>" . emms-pause)
          ("c" . bard/emms-recenter)
          ("P" . emms-playlist-mode-shift-track-up)
          ("N" . emms-playlist-mode-shift-track-down)
          ("Z" . bard/save-emms-watch-later)
          ("Y" . bard/emms-download-current-video))
    :bind (("<f8>" . emms)
           ("M-<f8>" . emms-browser))
    :hook
    (emms-playlist-mode . hl-line-mode)
    :config
    (emms-all)
    (emms-default-players)
    (emms-mpris-enable)
    (setq emms-player-list '(emms-player-mpv))
    ;; emms-info-functions '(emms-info-native)

    ;; (setq emms-browser-covers 'emms-browser-cache-thumbnail)

    (setq emms-volume-amixer-card 0)

    ;; center line function
    (defun bard/emms-recenter ()
      (interactive)
      (recenter-top-bottom)
      (emms-playlist-mode-center-current))

    ;; modeline
    (emms-mode-line-disable)
    (emms-playing-time-disable-display)

    ;; playlist saving
    (setq bard/emms-playlist-format 'm3u)
    (setq bard/watch-later-file "~/Videos/watch-later.m3u")
    )
#+end_src

*** PDF viewer (~pdf-tools~)
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-media.el :mkdirp yes
  (use-package pdf-tools
    :ensure t
    :config
    (pdf-tools-install)
    (add-to-list 'pdf-tools-enabled-modes #'pdf-view-themed-minor-mode))
#+end_src

*** Custom functions
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-media.el :mkdirp yes
  (use-package bard-media
    :ensure nil
    :config
    (require 'bard-media)
    :bind
    (("C-c o p" . bard/play-youtube-video)
     ("C-c o i" . bard/image-browser))
    )
#+end_src

*** Provide module
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-media.el :mkdirp yes
  (provide 'bard-emacs-media)
#+end_src

** bard-emacs-modeline
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-modeline.el :mkdirp yes
  (require 'bard-modeline)

  ;;; Mode line
  (setq mode-line-compact nil) ; Emacs 28
  (setq mode-line-right-align-edge 'right-margin)
  (setq-default mode-line-format
                '("%e"
                  prot-modeline-kbd-macro
                  prot-modeline-narrow
                  bard-modeline-centered-cursor
                  prot-modeline-input-method
                  prot-modeline-buffer-status
                  prot-modeline-window-dedicated-status
                  bard-evil-state-indicator
                  " "
                  prot-modeline-buffer-identification
                  "  "
                  prot-modeline-major-mode
                  prot-modeline-process
                  "  "
                  prot-modeline-vc-branch
                  "  "
                  prot-modeline-flymake
                  prot-modeline-eglot
                  bard-modeline-org-clock
                  "  "
                  mode-line-format-right-align
                  prot-modeline-notmuch-indicator
                  " "
                  prot-modeline-misc-info))

  (with-eval-after-load 'spacious-padding
    (defun prot/modeline-spacious-indicators ()
      "Set box attribute to `'prot-modeline-indicator-button' if spacious-padding is enabled."
      (if (bound-and-true-p spacious-padding-mode)
          (set-face-attribute 'prot-modeline-indicator-button nil :box t)
        (set-face-attribute 'prot-modeline-indicator-button nil :box 'unspecified)))

    ;; Run it at startup and then afterwards whenever
    ;; `spacious-padding-mode' is toggled on/off.
    (prot/modeline-spacious-indicators)

    (add-hook 'spacious-padding-mode-hook #'prot/modeline-spacious-indicators))

  (setq mode-line-right-align-edge 'window)

  (provide 'bard-emacs-modeline)

  ;;; bard-emacs-modeline.el ends here

#+end_src

** bard-emacs-org
*** Include required libraries
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-org.el :mkdirp yes
  (require 'org)
  (require 'ox)
  (require 'org-habit)
#+end_src

*** Main Configuration

#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-org.el :mkdirp yes
  (use-package org
    :defer nil
    :bind
    (:map org-mode-map
          ("C-M-a" . backward-paragraph)
          ("C-M-e" . forward-paragraph)
          ("C-c M-c" . count-words-region)
          ("C-c C-M-c" . count-words)
          ("C-c l" . org-id-get-create)
          ("C-c j" . org-goto)
          )
    :bind
    (("C-c c" . org-capture))
    :config
    (setq org-goto-interface 'outline-path-completion
          org-outline-path-complete-in-steps nil)
    (setq org-special-ctrl-a/e t)
    (setq safe-local-variable-values '((org-refile-targets (nil :maxlevel . 3)))))
#+end_src

#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-org.el :mkdirp yes
  (setq org-archive-location "~/Notes/denote/20240328T215840--archive__self.org::* Archive")
  (setq org-log-done 'time)
  (setq org-icalendar-include-todo t
        org-icalendar-include-body t
        org-icalendar-with-timestamps t
        org-icalendar-use-scheduled '(event-if-todo-not-done)
        org-icalendar-scheduled-summary-prefix "SCHEDULED: "
        org-icalendar-use-deadline '(event-if-todo-not-done)
        org-icalendar-deadline-summary-prefix "DEADLINE: ")
#+end_src

#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-org.el :mkdirp yes
  (setq org-structure-template-alist
        '(("c" . "center")
          ("x" . "example")
          ("d" . "definition")
          ("t" . "theorem")
          ("q" . "quote")
          ("v" . "verse")
          ("s" . "src")
          ("E" . "src emacs-lisp :results value code :lexical t") ; for code examples in notes
          ("z" . "src emacs-lisp :tangle FILENAME") ; tangle without making dir, below makes dir
          ("Z" . "src emacs-lisp :tangle FILENAME :mkdirp yes")))
  (setq org-ellipsis " ⤶")
#+end_src

*** Making org mode look nice
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-org.el :mkdirp yes
  (setq org-startup-indented t
        org-startup-folded 'showeverything
        org-hide-emphasis-markers t
        org-startup-with-inline-images t
        org-image-actual-width '(600)
        org-list-allow-alphabetical t
        org-insert-heading-respect-content t)

  (use-package org-bullets
    :ensure t
    :hook (org-mode . org-bullets-mode)
    :config
    (setq org-bullets-bullet-list '("◉" "○" "●" "🞛" "◇" "◆")))

#+end_src

*** Org export + latex
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-org.el :mkdirp yes
  (setq org-format-latex-options (plist-put org-format-latex-options :scale 1.5))

  (defun bard/org-export-on-save ()
    "Export current Org buffer to PDF and open it with auto-revert enabled."
    (when (derived-mode-p 'org-mode)
      (org-latex-export-to-pdf)))

  (define-minor-mode bard/org-auto-export-pdf-mode
    "Automatically export Org buffer to PDF on save."
    :lighter " AutoPDF"
    :group 'org
    (if bard/org-auto-export-pdf-mode
        (add-hook 'after-save-hook #'bard/org-export-on-save)
      (remove-hook 'after-save-hook #'bard/org-export-on-save)))

  (use-package auctex
    :ensure t)

  (use-package cdlatex
    :ensure t
    :hook (LaTeX-mode . turn-on-cdlatex)
    :bind (:map cdlatex-mode-map
                ("<tab>" . cdlatex-tab)))

  ;; latex editing niceness
  (use-package org-fragtog
    :ensure t)

  (with-eval-after-load 'ox-latex
    (add-to-list 'org-latex-classes
                 '("org-plain-latex"
                   "\\documentclass{article}
             [NO-DEFAULT-PACKAGES]
             [PACKAGES]
             [EXTRA]"
                   ("\\section{%s}" . "\\section*{%s}")
                   ("\\subsection{%s}" . "\\subsection*{%s}")
                   ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                   ("\\paragraph{%s}" . "\\paragraph*{%s}")
                   ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))))

  (setq org-latex-listings t)
  (setq org-latex-listings-options
        '(("basicstyle" "\\ttfamily")
          ("breakatwhitespace" "false")
          ("breakautoindent" "true")
          ("breaklines" "true")
          ("columns" "[c]fullflexible")
          ("commentstyle" "")
          ("emptylines" "*")
          ("extendedchars" "false")
          ("fancyvrb" "true")
          ("firstnumber" "auto")
          ("flexiblecolumns" "false")
          ("frame" "single")
          ("frameround" "tttt")
          ("identifierstyle" "")
          ("keepspaces" "true")
          ("keywordstyle" "")
          ("mathescape" "false")
          ("numbers" "left")
          ("numbers" "none")
          ("numbersep" "5pt")
          ("numberstyle" "\\tiny")
          ("resetmargins" "false")
          ("showlines" "true")
          ("showspaces" "false")
          ("showstringspaces" "false")
          ("showtabs" "true")
          ("stepnumber" "2")
          ("stringstyle" "")
          ("tab" "↹")
          ("tabsize" "4")
          ("texcl" "false")
          ("upquote" "false")))
#+end_src

*** Org capture
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-org.el :mkdirp yes
  (setq org-capture-bookmark nil
        org-id-link-to-org-use-id t)

  (require 'org-protocol)
  (setq org-capture-templates
        '(("t" "task" entry
           (file+olp
            "~/Notes/denote/20240328T215727--todo.org"
            "Inbox" "General tasks")
           "* TODO %?")
          ;; ("s" "Basic Statistics" entry
          ;;  (file+headline
          ;;   "~/Notes/denote/20240830T215644--statistics-flashcards__anki_stats.org" "Unsorted")
          ;;  "** %U %^g\n:PROPERTIES:\n:ANKI_NOTE_TYPE: Basic\n:ANKI_DECK: Statistics\n:END:\n*** Front\n %?\n*** Back\n\n")
          ;; ("S" "Cloze Statistics" entry
          ;;  (file+headline
          ;;   "~/Notes/denote/20240830T215644--statistics-flashcards__anki_stats.org" "Unsorted")
          ;;  "** %U %^g\n:PROPERTIES:\n:ANKI_NOTE_TYPE: Cloze\n:ANKI_DECK: Statistics\n:END:\n*** Text\n %?\n*** Hooray\n\n")
          ("c" "Basic Chemistry" entry
           (file+headline
            "~/Notes/denote/20251019T175402--chemistry-flashcards__anki_chem.org" "Unsorted")
           "** %U %^g\n:PROPERTIES:\n:ANKI_NOTE_TYPE: Basic\n:ANKI_DECK: Chemistry\n:END:\n*** Front\n%?\n*** Back\n\n")
          ("n" "common place note" entry
           (file "~/Notes/denote/20251023T182240--common-place-notes__topic.org")
           "* %^{Source}\n#+BEGIN_QUOTE\n%?\n#+END_QUOTE")
          ("z" "Protocol" entry
           (file+olp
            "~/Notes/denote/20240328T220037--media-tracker__media_topic.org" "Quotes")
           "* Source: [[%:link][%:description]]\n#+BEGIN_QUOTE\n%i\n#+END_QUOTE\n%?")
          ("Z" "Protocol Link" entry
           (file+olp
            "~/Notes/denote/20240328T220037--media-tracker__media_topic.org" "Watch/Read List")
           "* [[%:link][%:description]] \nCaptured On: %U \n%?")
          ("w" "Class outline" entry
           (file
            "~/Notes/denote/uni.org")
           (file
            "~/Notes/denote/templates/class-template.org"))
          ("p" "project idea" entry
           (file
            "~/Notes/denote/20250201T165619--project-ideas__idea_programming.org")
           "* %^{Project description}\n%?")))
#+end_src

*** Managing media
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-org.el :mkdirp yes
  ;; copy/paste images
  (use-package org-download
    :after org
    :defer nil
    :ensure t
    :custom
    (org-download-method 'directory)
    (org-download-image-dir "~/Notes/denote/Images")
    (org-download-heading-lvl 0)
    (org-download-timestamp "org_%Y%m%d-%H%M%S_")
    (org-download-screenshot-method "xclip -selection clipboard -t image/png -o > '%s'")
    :bind
    ("C-M-y" . org-download-screenshot)
    :config
    (require 'org-download))

  ;; pdf notes
  (use-package org-noter
    :ensure t)

  ;; links
  (use-package org-cliplink
    :ensure t
    :bind
    ("C-c p" . org-cliplink))

  (provide 'bard-emacs-org)
#+end_src

** bard-emacs-prog
*** General input setup
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-prog.el :mkdirp yes
(use-package electric
  :hook
  (prog-mode . electric-indent-local-mode)
  (prog-mode . electric-pair-local-mode))

(use-package paren
  :hook (prog-mode . show-paren-local-mode)
  :config
  (setq show-paren-style 'parenthesis)
  (setq show-paren-when-point-in-periphery nil)
  (setq show-paren-when-point-inside-paren nil)
  (setq show-paren-context-when-offscreen 'overlay))
#+end_src

*** Haskell
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-prog.el :mkdirp yes
  (use-package haskell-mode
    :ensure t
    :config
    (setq haskell-interactive-popup-errors nil))
#+end_src

*** C/C++
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-prog.el :mkdirp yes
  (use-package c-mode
    :config
    (setq-default c-basic-offset 4)
    (setq c-default-style '((c-mode . "gnu")
                            (java-mode . "java")
                            (awk-mode . "awk"))))

  (use-package ggtags
    :ensure t
    :config
    (add-hook 'c-mode-common-hook
              (lambda ()
                (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
                  (ggtags-mode 1)
                  (setq-local imenu-create-index-function #'ggtags-build-imenu-index)))))

  (use-package compile
    :ensure nil
    :defer 2
    :config
    (require 'bard-compile)
    (setq compilation-scroll-output t
          compilation-auto-jump-to-first-error nil))
#+end_src

*** Lisp development
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-prog.el :mkdirp yes
  (use-package clojure-mode
    :ensure t)

  (use-package cider
    :ensure t)

  (use-package sly
    :ensure t
    :config
    (setq inferior-lisp-program (executable-find "sbcl")))

  (use-package geiser
    :ensure t)

  (use-package geiser-gauche
    :ensure t)

  ;; parens packages
#+end_src

*** Catching errors
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-prog.el :mkdirp yes
  (use-package flycheck
    :ensure t
    :config
    (global-flycheck-mode t))
#+end_src

*** Version control
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-prog.el :mkdirp yes
  ;; Version control
  (use-package magit
    :ensure t
    :config
    (setq magit-repository-directories
          '(("~/Code"                    . 1)
            ("~/Repositories"            . 1)
            ("~/dotfiles-stow"           . 0)
            ("~/.emacs.d"                . 0)
            ("~/Pictures/wallpaper"      . 0)))
    :bind ("C-c g" . magit-status))
#+end_src

*** Ada-mode
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-prog.el :mkdirp yes
  ;; (use-package ada-mode
  ;;   :load-path "~/.emacs.d/old-ada"
  ;;   :bind
  ;;   (:map ada-mode-map
  ;; 	("C-j" . dired-jump)))
#+end_src

*** Provide module
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-prog.el :mkdirp yes
  (provide 'bard-emacs-prog)
#+end_src

** bard-emacs-theme
*** Load required libraries
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-theme.el :mkdirp yes
  (require 'bard-theme)
#+end_src

*** Set all themes to safe
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-theme.el :mkdirp yes
  ;; declare all themes as safe (i trust developers)
  (setq custom-safe-themes t)
#+end_src

*** Color theme
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-theme.el :mkdirp yes
  (use-package doom-themes
    :ensure t
    :config
    (setq doom-gruvbox-dark-variant "hard")
    (bard/select-theme 'doom-gruvbox))
#+end_src

*** Fonts
:PROPERTIES:
:ID:       542a0f2e-f79e-4272-af55-284c81b16fc3
:END:
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-theme.el :mkdirp yes
  (use-package fontaine
    :ensure nil
    :config
    ;; save file
    (setq fontaine-latest-state-file
          (locate-user-emacs-file "fontaine-latest-state.eld"))

    ;; Set last preset or fall back to desired style from `fontaine-presets'.
    (fontaine-set-preset (or (fontaine-restore-latest-preset) 'default))

    ;; The other side of `fontaine-restore-latest-preset'.
    (add-hook 'kill-emacs-hook #'fontaine-store-latest-preset)

    ;; preserve fonts when switching themes
    (dolist (hook '(modus-themes-after-load-theme-hook ef-themes-post-load-hook))
      (add-hook hook #'fontaine-apply-current-preset))

    (define-key global-map (kbd "C-c f") #'fontaine-set-preset))
#+end_src

#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-theme.el :mkdirp yes
  (use-package rainbow-mode
    :ensure t)
#+end_src

*** Binding for custom select theme function
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-theme.el :mkdirp yes
  (global-set-key (kbd "M-<f6>") #'bard/select-theme)
#+end_src

*** Mixed pitch faces for writing
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-theme.el :mkdirp yes
  (use-package mixed-pitch
    :ensure t
    :hook
    (org-mode . mixed-pitch-mode))
#+end_src

*** Spacious padding
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-theme.el :mkdirp yes
  (use-package spacious-padding
    :ensure t
    :config
    (setq spacious-padding-widths
          '( :internal-border-width 10
             :header-line-width 4
             :mode-line-width 6
             :tab-width 4
             :right-divider-width 1
             :left-fringe-width 0
             :right-fringe-width 0
             :scroll-bar-width 0))
    (spacious-padding-mode t))
#+end_src

*** Provide module
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-theme.el :mkdirp yes
  (provide 'bard-emacs-theme)
  ;;; bard-emacs-theme.el ends here
#+end_src

** bard-emacs-ui
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-ui.el :mkdirp yes
  (use-package whitespace
    :ensure nil
    :demand t
    :config
    (setq whitespace-style '(face
                             tabs
                             spaces
                             trailing
                             space-before-tab
                             newline indentation
                             empty space-after-tab
                             space-mark tab-mark))
    :hook
    (prog-mode . whitespace-mode))

  (use-package display-line-numbers
    :ensure nil
    :demand t
    :bind
    (("<f12>" . display-line-numbers-mode))
    :hook (prog-mode . display-line-numbers-mode)
    :config
    (setq display-line-numbers-type 'relative))
#+end_src

*** Todo keywords
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-ui.el :mkdirp yes
  (use-package hl-todo
    :ensure t
    :hook
    (prog-mode . hl-todo-mode)
    :config
    (setq hl-todo-highlight-punctuation ":"))
#+end_src

*** Keycasting
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-ui.el :mkdirp yes
  (use-package keycast
    :ensure t
    :commands (keycast-mode-line-mode keycast-header-line-mode keycast-tab-bar-mode keycast-log-mode)
    :init
    (setq keycast-mode-line-format "%2s%k%c%R")
    (setq keycast-mode-line-insert-after 'prot-modeline-vc-branch)
    (setq keycast-mode-line-window-predicate 'mode-line-window-selected-p)
    (setq keycast-mode-line-remove-tail-elements nil)
    :config
    (dolist (input '(self-insert-command org-self-insert-command))
      (add-to-list 'keycast-substitute-alist `(,input "." "Typing…")))

    (dolist (event '("<mouse-event>" "<mouse-movement>" "<mouse-2>" "<drag-mouse-1>" "<wheel-up>" "<wheel-down>" "<double-wheel-up>" "<double-wheel-down>" "<triple-wheel-up>" "<triple-wheel-down>" "<wheel-left>" "<wheel-right>" handle-select-window mouse-set-point  mouse-drag-region))
      (add-to-list 'keycast-substitute-alist `(,event nil nil))))
#+end_src

*** Provide module
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-ui.el :mkdirp yes
  (provide 'bard-emacs-ui)
  ;;; bard-emacs-ui.el ends here
#+end_src

** bard-emacs-web
*** IRC
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-web.el :mkdirp yes
  ;;; IRC
  (use-package circe
    :ensure t
    :config
    (setq auth-sources '("~/.authinfo.gpg"))

    (defun my-fetch-password (&rest params)
      (require 'auth-source)
      (let ((match (car (apply 'auth-source-search params))))
        (if match
            (let ((secret (plist-get match :secret)))
              (if (functionp secret)
                  (funcall secret)
                secret))
          (error "Password not found for %S" params))))

    (defun my-nickserv-password (server)
      (my-fetch-password :user "bardman" :machine "irc.libera.chat"))

    (setq circe-network-options
          '(("Libera Chat"
             :nick "bardman"
             :channels ("#emacs" "##anime" "#gentoo")
             :nickserv-password my-nickserv-password))))
#+end_src

*** RSS Feeds
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-web.el :mkdirp yes
  (use-package elfeed
    :ensure t
    :config
    (require 'bard-web)
    (global-set-key (kbd "C-c r") 'elfeed)
    (setq elfeed-search-filter "+unread")

    :bind
    (:map elfeed-search-mode-map
          ;; C-p for play now
          ("C-c C-p" . bard/play-elfeed-video)
          ;; C-e for EMMS
          ("C-c C-e" . bard/add-video-emms-queue)
          ;; C-w for watch later
          ("C-c C-w" . bard/add-video-watch-later)
          ;; F is for fetch
          ("F"       . elfeed-update)))

  (use-package elfeed-org
    :ensure t
    :init
    (elfeed-org)
    :config
    (setq rmh-elfeed-org-files (list "~/Notes/denote/feeds.org"
                                     "~/Notes/denote/youtube.org")))
#+end_src

*** Web Browsing (EWW and external)
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-web.el :mkdirp yes

  (use-package eww
    :defer t
    :config
    (setq browse-url-handlers
          '(("wikipedia\\.org" . eww-browse-url)
            ("github" . browse-url-default-browser)
            ("youtube.com" . browse-url-default-browser)
            ("reddit.com" . browse-url-default-browser)))

    ;; shr optimizations
    (setq shr-use-colors nil)
    (setq shr-use-fonts nil)
    (setq shr-max-image-proportion 0.6)
    (setq shr-image-animate nil)
    (setq shr-width fill-column)
    (setq shr-max-width fill-column)
    (setq shr-discard-aria-hidden t)
    (setq shr-cookie-policy nil)

    ;; eww
    (setq eww-search-prefix "https://duckduckgo.com/html/?q=")
    (setq eww-history-limit 150)
    (setq eww-use-external-browser-for-content-type
          "\\`\\(video/\\|audio\\)")
    :bind
    ("C-c w" . eww))

  (provide 'bard-emacs-web)
  ;;; bard-emacs-web.el ends here
#+end_src

** bard-emacs-window
*** Load required libraries
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-window.el :mkdirp yes
  (require 'bard-window)
#+end_src

#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-window.el :mkdirp yes
  (use-package emacs
    ;; configuration for window splits/window sizes
    :config
    (setq focus-follows-mouse t)
    (setq mouse-autoselect-window t)
    (setq window-combination-resize t)
    (setq even-window-sizes 'height-only)
    (setq window-sides-vertical nil)
    (setq switch-to-buffer-in-dedicated-window 'pop)
    (setq split-height-threshold 80
          split-width-threshold 125)
    (setq window-min-height 3
          window-min-width 30))
#+end_src

*** Moving windos
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-window.el :mkdirp yes
  (use-package windmove
    :bind*
    (("C-M-<up>" . windmove-up)
     ("C-M-<right>" . windmove-right)
     ("C-M-<down>" . windmove-down)
     ("C-M-<left>" . windmove-left)
     ("C-M-S-<up>" . windmove-swap-states-up)
     ("C-M-S-<right>" . windmove-swap-states-right)
     ("C-M-S-<down>" . windmove-swap-states-down)
     ("C-M-S-<left>" . windmove-swap-states-left)))
#+end_src

*** Window display rules (~display-buffer-alist~)
Watch [[https://protesilaos.com/codelog/2024-02-08-emacs-window-rules-display-buffer-alist/][Protesilaos Stavrou's video]] about this to understand it. My configuration Just Works™, so I don't really get how to change it that much anymore. He can explain it a lot better than I can.

#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-window.el :mkdirp yes
  (use-package emacs
    :config
    (setq display-buffer-alist
          `(;; no window
            ("\\`\\*Async Shell Command\\*\\'"
             (display-buffer-no-window))
            ("\\`\\*\\(Warnings\\|Compile-Log\\|Org Links\\)\\*\\'"
             (display-buffer-no-window)
             (allow-no-window . t))
            ;; bottom side window
            ("\\*Org \\(Select\\|Note\\)\\*" ; the `org-capture' key selection and `org-add-log-note'
             (display-buffer-in-side-window)
             (dedicated . t)
             (side . bottom)
             (slot . 0)
             (window-parameters . ((mode-line-format . none))))
            ;; bottom buffer (NOT side window)
            ((or . ((derived-mode . flymake-diagnostics-buffer-mode)
                    (derived-mode . flymake-project-diagnostics-mode)
                    (derived-mode . messages-buffer-mode)
                    (derived-mode . backtrace-mode)))
             (display-buffer-reuse-mode-window display-buffer-at-bottom)
             (window-height . 0.3)
             (dedicated . t)
             (preserve-size . (t . t)))
            ;; terminal popups
            (prot-window-shell-or-term-p
             (display-buffer-reuse-mode-window display-buffer-at-bottom)
             (mode . (shell-mode eshell-mode comint-mode))
             (body-function . prot-window-select-fit-size))
            ("\\magit: .*"
             (display-buffer-same-window)
             (inhibit-same-window . nil)
             (dedicated . t))
            ("\\*Org Agenda\\*"
             (display-buffer-same-window)
             (inhibit-same-window . nil)
             (dedicated . t))
            ("\\*cfw-calendar\\*"
             (display-buffer-same-window)
             (inhibit-same-window . nil)
             (dedicated . t))
            ("\\*image-dired\\*"
             (display-buffer-reuse-mode-window display-buffer-in-side-window)
             (side . bottom)
             (window-height . 0.5))
            ("\\*image-dired-display-image\\*"
             (display-buffer-reuse-mode-window display-buffer-in-side-window)
             (side . right)
             (window-width . 0.35))
            ;; ("\\*Embark Actions\\*"
            ;;  (display-buffer-reuse-mode-window display-buffer-below-selected)
            ;;  (window-height . fit-window-to-buffer)
            ;;  (window-parameters . ((no-other-window . t)
            ;;                        (mode-line-format . none))))
            ("\\*\\(Output\\|Register Preview\\).*"
             (display-buffer-reuse-mode-window display-buffer-at-bottom))
            ;; below current window
            ("\\(\\*Capture\\*\\|CAPTURE-.*\\)"
             (display-buffer-in-side-window)
             (dedicated . t)
             (side . bottom)
             (slot . 0)
             (window-parameters . ((mode-line-format . none))))
            ("\\*\\vc-\\(incoming\\|outgoing\\|git : \\).*"
             (display-buffer-reuse-mode-window display-buffer-below-selected)
             (window-height . 0.1)
             (dedicated . t)
             (preserve-size . (t . t)))
            ((derived-mode . reb-mode) ; M-x re-builder
             (display-buffer-reuse-mode-window display-buffer-below-selected)
             (window-height . 4) ; note this is literal lines, not relative
             (dedicated . t)
             (preserve-size . (t . t)))
            ((or . ((derived-mode . occur-mode)
                    (derived-mode . grep-mode)
                    (derived-mode . Buffer-menu-mode)
                    (derived-mode . log-view-mode)
                    (derived-mode . help-mode) ; See the hooks for `visual-line-mode'
                    "\\*\\(|Buffer List\\|Occur\\|vc-change-log\\|eldoc.*\\).*"
                    prot-window-shell-or-term-p
                    ;; ,world-clock-buffer-name
                    ))
             (prot-window-display-buffer-below-or-pop)
             (body-function . prot-window-select-fit-size))
            ("\\*\\(Calendar\\|Bookmark Annotation\\|ert\\).*"
             (display-buffer-reuse-mode-window display-buffer-below-selected)
             (dedicated . t)
             (window-height . fit-window-to-buffer))
            ("\\*ispell-top-choices\\*.*"
             (display-buffer-reuse-mode-window display-buffer-below-selected)
             (window-height . fit-window-to-buffer))
            )))
#+end_src

*** Frame
**** Keybinds
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-window.el :mkdirp yes
  (use-package frame
    :ensure nil
    :bind (("C-x u" . undelete-frame)
           ("C-x f" . other-frame-prefix)
           ("C-x w t" . tear-off-window)
           ("C-x w c" . clone-indirect-buffer-other-window))
    :hook (after-init . undelete-frame-mode))
#+end_src

**** Beframe
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-window.el :mkdirp yes
  (use-package beframe
    :ensure t
    :config
    (setq beframe-functions-in-frames '(project-prompt-project-dir
                                        notmuch))
    (setq beframe-create-frame-scratch-buffer nil)
    (setq beframe-global-buffers '("*scratch*" "*Messages*" "*Backtrace*"))
    (beframe-mode 1)

    (define-key global-map (kbd "C-x f") #'other-frame-prefix)
    (define-key global-map (kbd "C-c b") beframe-prefix-map)
    (define-key global-map (kbd "C-x C-b") #'beframe-buffer-menu)
    (define-key global-map (kbd "C-x B") #'select-frame-by-name)
    (define-key global-map (kbd "C-c b u") #'beframe-unassume-current-frame-buffers-selectively)
    (define-key global-map (kbd "C-c b a") #'beframe-assume-buffers-selectively-all-frames)

    ;; Consult integration
    (defvar consult-buffer-sources)
    (declare-function consult--buffer-state "consult")

    (with-eval-after-load 'consult
      (defface beframe-buffer
        '((t :inherit font-lock-string-face))
        "Face for `consult' framed buffers.")

      (defun my-beframe-buffer-names-sorted (&optional frame)
        "Return the list of buffers from `beframe-buffer-names' sorted by visibility.
       With optional argument FRAME, return the list of buffers of FRAME."
        (beframe-buffer-names frame :sort #'beframe-buffer-sort-visibility))

      (defvar beframe-consult-source
        `( :name     "Frame-specific buffers (current frame)"
           :narrow   ?F
           :category buffer
           :face     beframe-buffer
           :history  beframe-history
           :items    ,#'my-beframe-buffer-names-sorted
           :action   ,#'switch-to-buffer
           :state    ,#'consult--buffer-state))

      (add-to-list 'consult-buffer-sources 'beframe-consult-source)))

  (provide 'bard-emacs-window)
  ;;; bard-emacs-window.el ends here
#+end_src

*** Undoing window actions (~winner-mode~)
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-window.el :mkdirp yes
  (use-package winner-mode
    :init
    (winner-mode 1)
    :bind
    (("C-x <right>" . winner-redo)
     ("C-x <left>" . winner-undo)
     ("C-x C-n" . next-buffer)
     ("C-x C-p" . previous-buffer)
     ("C-x <up>" . next-buffer)
     ("C-x <down>" . previous-buffer)
     ("C-x w w" . bard/toggle-window-split)))
#+end_src

*** Ibuffer
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-window.el :mkdirp yes
  (use-package ibuffer
    :ensure nil
    :config
    (setq ibuffer-default-sorting-mode 'major-mode)
    (ibuffer-auto-mode t))
#+end_src

** bard-emacs-writing
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (use-package emacs
    :ensure nil
    :demand t
    :bind
    (("C-x i" . insert-char)
     ("M-z"   . zap-to-char)
     ("<f10>"  . toggle-input-method))
    :config
    ;; Sentence size
    (setq sentence-end-double-space nil)

    ;; Keyboard things
    (setq default-input-method "cyrillic-yawerty")
    (setq default-transient-input-method "cyrillic-yawerty"))

  ;; spell check
  (use-package text-mode
    :ensure nil
    :hook
    (text-mode . flyspell-mode))

  ;; Tab settings
  (use-package emacs
    :config
    (setq tab-always-indent 'complete)
    (setq tab-first-completion 'word-or-paren-or-punct)
    (setq-default tab-width 4
                  indent-tabs-mode nil))
#+end_src

*** Snippets
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (use-package yasnippet
    :ensure t
    :hook ((LaTeX-mode . yas-minor-mode)
           (post-self-insert . my/yas-try-expanding-auto-snippets))
    :config
    (setq yas-snippet-dirs '("~/.emacs.d/snippets"))
    (yas-global-mode t)
    (use-package warnings
      :config
      (cl-pushnew '(yasnippet backquote-change)
                  warning-suppress-types
                  :test 'equal))

    (setq yas-triggers-in-field t)

    ;; Function that tries to autoexpand YaSnippets
    ;; The double quoting is NOT a typo!
    (defun my/yas-try-expanding-auto-snippets ()
      (when (and (boundp 'yas-minor-mode) yas-minor-mode)
        (let ((yas-buffer-local-condition ''(require-snippet-condition . auto)))
          (yas-expand)))))

  (use-package yasnippet-capf
    :ensure t
    :after cape
    :config
    (add-to-list 'completion-at-point-functions #'yasnippet-capf))

  (use-package cdlatex
    :hook ((cdlatex-tab . yas-expand)
           (cdlatex-tab . cdlatex-in-yas-field))
    :config
    (use-package yasnippet
      :bind (:map yas-keymap
             ("<tab>" . yas-next-field-or-cdlatex)
             ("TAB" . yas-next-field-or-cdlatex))
      :config
      (defun cdlatex-in-yas-field ()
        ;; Check if we're at the end of the Yas field
        (when-let* ((_ (overlayp yas--active-field-overlay))
                    (end (overlay-end yas--active-field-overlay)))
          (if (>= (point) end)
              ;; Call yas-next-field if cdlatex can't expand here
              (let ((s (thing-at-point 'sexp)))
                (unless (and s (assoc (substring-no-properties s)
                                      cdlatex-command-alist-comb))
                  (yas-next-field-or-maybe-expand)
                  t))
            ;; otherwise expand and jump to the correct location
            (let (cdlatex-tab-hook minp)
              (setq minp
                    (min (save-excursion (cdlatex-tab)
                                         (point))
                         (overlay-end yas--active-field-overlay)))
              (goto-char minp) t))))

      (defun yas-next-field-or-cdlatex nil
        (interactive)
        "Jump to the next Yas field correctly with cdlatex active."
        (if
            (or (bound-and-true-p cdlatex-mode)
                (bound-and-true-p org-cdlatex-mode))
            (cdlatex-tab)
          (yas-next-field-or-maybe-expand)))))
#+end_src

*** Denote Note taking
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (use-package denote
    :ensure t
    :config
    (require 'bard-writing)
    (setq denote-directory "~/Notes/denote/")
    (setq denote-buffer-name-prefix "[Note] "
          denote-rename-buffer-format "%t %b")
    (setq denote-known-keywords
          '("emacs"
            "linux"
            "programming"
            "org"
            "school"
            "language"
            "history"
            "biology"
            ))

    (setq denote-templates
          '((default . "Related to — ")
            (todo . bard/denote-todo-template)))

    (setq denote-save-buffers t)
    (setq denote-prompts '(title keywords))
    (setq denote-sort-dired-extra-prompts nil)
    (setq denote-sort-dired-default-sort-component 'identifier)
    (setq denote-sort-dired-default-reverse-sort nil)

    ;; backlinks sidebar
    (setq denote-backlinks-display-buffer-action
          '((display-buffer-in-direction)
            (direction . right)
            (window-width . 0.33)
            (window-height . fit-window-to-buffer)
            (dedicated . t)))

    (denote-rename-buffer-mode 1)

    (require 'bard-writing)

    :hook
    (dired-mode . denote-dired-mode)
    :bind
    (("C-c n n" . denote)
     ("C-c n d" . denote-sort-dired)
     ("C-c n r" . denote-rename-file-using-front-matter)
     ("C-c n k" . denote-rename-file-keywords)
     ("C-c n I" . denote-add-links)
     ("C-c n b" . bard/consult-buffer-notes)   ; notes buffer
     ("C-c n B" . bard/ibuffer-notes)          ; notes buffer but more
     ("C-c n f" . bard/find-notes-file)        ; notes-find
     ("C-c n g" . bard/search-notes-directory))) ; notes-grep
#+end_src
**** Denote org mode extensions
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (use-package denote-org
    :ensure t
    )
#+end_src

**** Denote silo extension
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (use-package denote-silo
    :ensure t
    :config
    (setq denote-silo-directories '("~/Notes/denote"
                                    "~/Notes/Old Notes/")))
#+end_src

**** Denote sequence extension
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (use-package denote-sequence
    :ensure t
    :config
    (require 'bard-writing)
    :bind
    ("C-c n N" . denote-sequence)
    ("C-c n D" . denote-sequence-dired)
    ("C-c n <SPC>" . denote-sequence-region))
#+end_src

**** Denote journal extension
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (use-package denote-journal
    :ensure t
    :bind
    ("C-c n j" . denote-journal-new-or-existing-entry)
    :config
    (setq denote-journal-directory "~/Notes/denote/journal/")

    (setq denote-journal-title-format "Daily Tasks and Notes"))
#+end_src

*** Denote roam
[[https://github.com/BardofSprites/denote-roam][Denote-roam]] is my Emacs package that blends org roam and denote into one workflow.
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (use-package denote-roam
    :ensure nil
    :load-path "~/Code/denote-roam/"
    :bind
    ("C-c n i" . denote-roam-insert-or-create-node)  ; node insert
    ("C-c n o" . denote-roam-find-or-create-node)    ; node open
    :custom
    (denote-roam-include-journal nil)
    (denote-roam-directory "~/Notes/denote")
    :config
    (denote-roam-mode t))
#+end_src

*** Org roam
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (use-package org-roam
    :ensure t
    :custom
    (org-roam-directory (file-truename "~/Notes/denote"))
    :bind (("C-c n l" . org-roam-buffer-toggle))
    :config
    (setq org-roam-db-node-include-function
          (lambda ()
            (not (member "ATTACH" (org-get-tags)))))

    (org-roam-db-autosync-mode 1))

  (use-package org-roam-ui
    :ensure t
    :bind
    ("C-c n u" . org-roam-ui-open)
    :custom
    (org-roam-ui-open-on-start nil))
#+end_src

*** Focused writing environment
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  ;; Center line scrolling for focused writing
  (use-package emacs
    :config
    (define-minor-mode bard/scroll-center-cursor-mode
      "Toggle centered cursor scrolling behavior."
      :init-value nil
      :lighter " S="
      :global nil
      (if bard/scroll-center-cursor-mode
          (setq-local scroll-margin (* (frame-height) 2)
                      scroll-conservatively 0
                      maximum-scroll-margin 0.5)
        (dolist (local '(scroll-preserve-screen-position
                         scroll-conservatively
                         maximum-scroll-margin
                         scroll-margin))
          (kill-local-variable `,local))))
    (defun bard/cursor-centered-p ()
      "Check if `bard/scroll-center-cursor-mode` is currently active."
      (bound-and-true-p bard/scroll-center-cursor-mode))
    :bind
    (("C-c L" . bard/scroll-center-cursor-mode)))

  (use-package olivetti
    :ensure t
    :config
    (setq olivetti-minimum-body-width 90)
    (setq olivetti-recall-visual-line-mode-entry-state t)
    :hook
    ((olivetti-mode-on . (lambda () (olivetti-set-width 90)))
     ))

  ;; narrowing and focus mode
  (use-package logos
    :ensure t
    :config
    (defun logos-reveal-entry ()
      "Reveal Org or Outline entry."
      (cond
       ((and (eq major-mode 'org-mode)
             (org-at-heading-p))
        (org-show-subtree))
       ((or (eq major-mode 'outline-mode)
            (bound-and-true-p outline-minor-mode))
        (outline-show-subtree))))

    (setq logos-outlines-are-pages t)

    (setq logos-outline-regexp-alist
          `((emacs-lisp-mode . "^;;;+ ")
            (org-mode . "^\\* +")
            (t . ,(or outline-regexp logos--page-delimiter))))

    (setq-default logos-hide-cursor nil
                  logos-hide-mode-line nil
                  logos-hide-header-line t
                  logos-hide-buffer-boundaries t
                  logos-hide-fringe t
                  logos-variable-pitch t
                  logos-olivetti t)

    (defun bard/logos--recenter-top ()
      "Use `recenter' to reposition the view at the top."
      (unless (derived-mode-p 'prog-mode)
        (recenter 1))) ; Use 0 for the absolute top
    :hook
    ((logos-page-motion . bard/logos--recenter-top))
    :hook
    ((org-mode . logos-focus-mode)
     (markdown-mode . logos-focus-mode))
    :bind
    (("M-]" . logos-forward-page-dwim)
     ("M-[" . logos-backward-page-dwim)
     ("<f9>" . logos-focus-mode)
     ("C-x n n" . logos-narrow-dwim)))
#+end_src

*** Citations and bibliography
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (use-package citar
    :ensure t
    :bind
    ("C-c n c" . citar-open)
    :config
    (setq citar-bibliography '("~/Documents/bib/references.bib"))
    (setq org-cite-global-bibliography citar-bibliography
          org-cite-insert-processor 'citar
          org-cite-follow-processor 'citar
          org-cite-activate-processor 'citar)

    (setq citar-notes-paths '("~/Notes/denote"))
    (setq citar-library-paths '("~/Documents/Research Articles/"))
    ;; (setq citar-file-open-functions 'find-file)
    :hook
    (org-mode . citar-capf-setup)
    :bind (("C-c i" . citar-insert-citation)))

  (use-package citar-denote
    :ensure t
    :config
    (citar-denote-mode t)
    )

  (use-package citar-embark
    :ensure t
    )
#+end_src

*** Provide module
#+begin_src emacs-lisp :tangle bard-emacs-modules/bard-emacs-writing.el :mkdirp yes
  (provide 'bard-emacs-writing)
#+end_src

* Libraries
** bard-calendar
#+begin_src emacs-lisp :tangle bard-elisp/bard-calendar.el :mkdirp yes
  (require 'org)
#+end_src

*** Study session program spawning
I use a timer to study sometimes when I really don't lock in. It is a [[https://github.com/BardofSprites/dotfiles-stow/blob/master/bin/.local/bin/scripts/lisp/study.lisp][common lisp script]] that manages process of a timer called [[https://github.com/tsoding/sowon][sowon]].

#+begin_src emacs-lisp :tangle bard-elisp/bard-calendar.el :mkdirp yes
  (defun bard/auto-clock-in ()
    "Automatically clock in when task marked in progress (INPROG),
   and start study session."
    (when (equal (org-get-todo-state) "INPROG")
      (org-clock-in)
      (bard/study-session)))

  (defun bard/study-session ()
    "Prompt for study parameters, run study session, and clock out when done."
    (interactive)
    (let* ((study-time (read-string "Study time (minutes): "))
           (break-time (read-string "Break time (minutes): "))
           (sessions (read-string "Number of sessions: "))
           (command (format "study %s %s %s" study-time break-time sessions))
           (process (start-process-shell-command "study-session" "*study*" command)))
      (set-process-sentinel
       process
       (lambda (_proc event)
         (when (string= event "finished\n")
           (progn (org-clock-out)
                  (pop-to-buffer-same-window "todo.org")))))))
#+end_src

*** Org clock
#+begin_src emacs-lisp :tangle bard-elisp/bard-calendar.el :mkdirp yes
    (defun bard/org-clock-report ()
      "Generate an org clock report in a separate org buffer."
      (interactive)
      (bard/new-org-buffer)
      (org-clock-report))

    (defun bard/org-clock-update-mode-line ()
      "Update the modeline, not sure why I have this."
      (interactive)
      (setq org-mode-line-string nil)
      (force-mode-line-update))

    (defun bard/org-clock-task-string ()
      "Return a simplified org clock task string.
  Used in FVWM3 configuration to show clocked task in FVWMbuttons."
      (if (and (boundp 'org-mode-line-string)
               (not (string-equal "" org-mode-line-string))
               org-mode-line-string)
          (substring-no-properties org-mode-line-string)
        "No task clocked in"))
#+end_src

*** Open the calendar in its own window
I use this in window manager status bar to open a popup window to view the dates on the calendar.
#+begin_src emacs-lisp :tangle bard-elisp/bard-calendar.el :mkdirp yes
  (defun bard/open-calendar ()
    "Opens calendar as only window"
    (interactive)
    (calendar)
    (delete-other-windows))
#+end_src

*** Org agenda
#+begin_src emacs-lisp :tangle bard-elisp/bard-calendar.el :mkdirp yes
  (defun bard/choose-agenda ()
    "For viewing my custom agenda"
    (interactive)
    (let ((agenda-views '("Default" "Monthly" "Yearly")))
      (setq chosen-view (completing-read "Choose an agenda view: " agenda-views))
      (cond
       ((string= chosen-view "Yearly")
        (org-agenda nil "Y"))
       ((string= chosen-view "Monthly")
        (org-agenda nil "M"))
       ((string= chosen-view "Default")
        (org-agenda nil "D")))))

  (defun bard/default-agenda ()
    "For viewing my custom agenda"
    (interactive)
    (org-agenda nil "D"))
#+end_src

*** Provide library
#+begin_src emacs-lisp :tangle bard-elisp/bard-calendar.el :mkdirp yes
  (provide 'bard-calendar)
  ;;; bard-calendar.el ends here
#+end_src

** bard-compile
Code taken from [[http://endlessparentheses.com/ansi-colors-in-the-compilation-buffer-output.html][here]]. I need this to solve a problem I had with pros-cli back when I did robotics in high school. The program had a bunch of color codes that weren't properly displayed in the compliation output. I really like using ~M-x compile~ for C/C++ development specifically.
#+begin_src emacs-lisp :tangle bard-elisp/bard-compile.el :mkdirp yes
  (require 'ansi-color)
  (defun endless/colorize-compilation ()
    "Colorize from `compilation-filter-start' to `point'."
    (let ((inhibit-read-only t))
      (ansi-color-apply-on-region
       compilation-filter-start (point))))

  (add-hook 'compilation-filter-hook
            #'endless/colorize-compilation)

  ;; Stolen from (https://oleksandrmanzyuk.wordpress.com/2011/11/05/better-emacs-shell-part-i/)
  (defun regexp-alternatives (regexps)
    "Return the alternation of a list of regexps."
    (mapconcat (lambda (regexp)
                 (concat "\\(?:" regexp "\\)"))
               regexps "\\|"))

  (defvar non-sgr-control-sequence-regexp nil
    "Regexp that matches non-SGR control sequences.")

  (setq non-sgr-control-sequence-regexp
        (regexp-alternatives
         '(;; icon name escape sequences
           "\033\\][0-2];.*?\007"
           ;; non-SGR CSI escape sequences
           "\033\\[\\??[0-9;]*[^0-9;m]"
           ;; noop
           "\012\033\\[2K\033\\[1F"
           )))

  (defun filter-non-sgr-control-sequences-in-region (begin end)
    (save-excursion
      (goto-char begin)
      (while (re-search-forward
              non-sgr-control-sequence-regexp end t)
        (replace-match ""))))

  (defun filter-non-sgr-control-sequences-in-output (ignored)
    (let ((start-marker
           (or comint-last-output-start
               (point-min-marker)))
          (end-marker
           (process-mark
            (get-buffer-process (current-buffer)))))
      (filter-non-sgr-control-sequences-in-region
       start-marker
       end-marker)))

  (add-hook 'comint-output-filter-functions
            'filter-non-sgr-control-sequences-in-output)

  (provide 'bard-compile)

#+end_src

** bard-email
Word for word copy of Protesilaos notmuch library. Email in Emacs is already complicated enough, and I had enough of a hard time without his help.
#+begin_src emacs-lisp :tangle bard-elisp/bard-email.el :mkdirp yes
(require 'prot-common)
(eval-when-compile (require 'cl-lib))

(defgroup prot-notmuch ()
  "Extensions for notmuch.el."
  :group 'notmuch)

(defcustom prot-notmuch-delete-tag "del"
  "Single tag that applies to mail marked for deletion.
This is used by `prot-notmuch-delete-mail'."
  :type 'string
  :group 'prot-notmuch)

(defcustom prot-notmuch-mark-delete-tags
  `(,(format "+%s" prot-notmuch-delete-tag) "-inbox" "-unread")
  "List of tags to mark for deletion.
To actually delete email, refer to `prot-notmuch-delete-mail'."
  :type '(repeat string)
  :group 'prot-notmuch)

(defcustom prot-notmuch-mark-flag-tags '("+flag" "-unread")
  "List of tags to mark as important (flagged).
This gets the `notmuch-tag-flagged' face, if that is specified in
`notmuch-tag-formats'."
  :type '(repeat string)
  :group 'prot-notmuch)

(defcustom prot-notmuch-mark-spam-tags '("+spam" "-inbox" "-unread")
  "List of tags to mark as spam."
  :type '(repeat string)
  :group 'prot-notmuch)

(autoload 'notmuch-interactive-region "notmuch")
(autoload 'notmuch-tag-change-list "notmuch")
(autoload 'notmuch-search-next-thread "notmuch")
(autoload 'notmuch-search-tag "notmuch")

(defmacro prot-notmuch-search-tag-thread (name tags)
  "Produce NAME function parsing TAGS."
  (declare (indent defun))
  `(defun ,name (&optional untag beg end)
     ,(format
       "Mark with `%s' the currently selected thread.

Operate on each message in the currently selected thread.  With
optional BEG and END as points delimiting a region that
encompasses multiple threads, operate on all those messages
instead.

With optional prefix argument (\\[universal-argument]) as UNTAG,
reverse the application of the tags.

This function advances to the next thread when finished."
       tags)
     (interactive (cons current-prefix-arg (notmuch-interactive-region)))
     (when ,tags
       (notmuch-search-tag
        (notmuch-tag-change-list ,tags untag) beg end))
     (when (eq beg end)
       (notmuch-search-next-thread))))

(prot-notmuch-search-tag-thread
  prot-notmuch-search-delete-thread
  prot-notmuch-mark-delete-tags)

(prot-notmuch-search-tag-thread
  prot-notmuch-search-flag-thread
  prot-notmuch-mark-flag-tags)

(prot-notmuch-search-tag-thread
  prot-notmuch-search-spam-thread
  prot-notmuch-mark-spam-tags)

(defmacro prot-notmuch-show-tag-message (name tags)
  "Produce NAME function parsing TAGS."
  (declare (indent defun))
  `(defun ,name (&optional untag)
     ,(format
       "Apply `%s' to message.

With optional prefix argument (\\[universal-argument]) as UNTAG,
reverse the application of the tags."
       tags)
     (interactive "P")
     (when ,tags
       (apply 'notmuch-show-tag-message
	          (notmuch-tag-change-list ,tags untag)))))

(prot-notmuch-show-tag-message
  prot-notmuch-show-delete-message
  prot-notmuch-mark-delete-tags)

(prot-notmuch-show-tag-message
  prot-notmuch-show-flag-message
  prot-notmuch-mark-flag-tags)

(prot-notmuch-show-tag-message
  prot-notmuch-show-spam-message
  prot-notmuch-mark-spam-tags)

(defun prot-notmuch-delete-mail ()
  "Permanently delete mail marked as `prot-notmuch-delete-mail'.
Prompt for confirmation before carrying out the operation.

Do not attempt to refresh the index.  This will be done upon the
next invocation of 'notmuch new'."
  (interactive)
  (let* ((del-tag prot-notmuch-delete-tag)
         (count
          (string-to-number
           (with-temp-buffer
             (shell-command
              (format "notmuch count tag:%s" prot-notmuch-delete-tag) t)
             (buffer-substring-no-properties (point-min) (1- (point-max))))))
         (mail (if (> count 1) "mails" "mail")))
    (unless (> count 0)
      (user-error "No mail marked as `%s'" del-tag))
    (when (yes-or-no-p
           (format "Delete %d %s marked as `%s'?" count mail del-tag))
      (shell-command
       (format "notmuch search --output=files --format=text0 tag:%s | xargs -r0 rm" del-tag)
       t))))
(provide 'bard-email)

#+end_src

** bard-embark
Main embark configuration found [[id:ce728534-8fa7-4d4a-96e3-71cb02fc9af4][here]]. This is another one of the files I borrowed from Protesilaos, but renamed/modified so I can remember.

#+begin_src emacs-lisp :tangle bard-elisp/bard-embark.el :mkdirp yes
(require 'embark)

(defvar-keymap bard-embark-general-map
  :parent embark-general-map
  "i" #'embark-insert
  "w" #'embark-copy-as-kill
  "E" #'embark-export
  "S" #'embark-collect
  "A" #'embark-act-all
  "DEL" #'delete-region)

(defvar-keymap bard-embark-url-map
  :parent embark-general-map
  "b" #'browse-url
  "d" #'embark-download-url
  "e" #'eww)

(defvar-keymap bard-embark-buffer-map
  :parent embark-general-map
  "k" #'bard-simple-kill-buffer
  "o" #'switch-to-buffer-other-window
  "e" #'ediff-buffers)

(add-to-list 'embark-post-action-hooks (list 'bard-simple-kill-buffer 'embark--restart))

(defvar-keymap bard-embark-file-map
  :parent embark-general-map
  "f" #'find-file
  "j" #'embark-dired-jump
  "c" #'copy-file
  "e" #'ediff-files)

(defvar-keymap bard-embark-identifier-map
  :parent embark-general-map
  "h" #'display-local-help
  "." #'xref-find-definitions
  "o" #'occur)

(defvar-keymap bard-embark-command-map
  :parent embark-general-map
  "h" #'describe-command
  "." #'embark-find-definition)

(defvar-keymap bard-embark-expression-map
  :parent embark-general-map
  "e" #'pp-eval-expression
  "m" #'pp-macroexpand-expression)

(defvar-keymap bard-embark-function-map
  :parent embark-general-map
  "h" #'describe-function
  "." #'embark-find-definition)

(defvar-keymap bard-embark-package-map
  :parent embark-general-map
  "h" #'describe-package
  "i" #'package-install
  "d" #'package-delete
  "r" #'package-reinstall
  "b" #'embark-browse-package-url
  "w" #'embark-save-package-url)

(defvar-keymap bard-embark-symbol-map
  :parent embark-general-map
  "h" #'describe-symbol
  "." #'embark-find-definition)

(defvar-keymap bard-embark-variable-map
  :parent embark-general-map
  "h" #'describe-variable
  "." #'embark-find-definition)

(defvar-keymap bard-embark-region-map
  :parent embark-general-map
  "a" #'align-regexp
  "D" #'delete-duplicate-lines
  "f" #'flush-lines
  "i" #'epa-import-keys-region
  "d" #'epa-decrypt-armor-in-region
  "r" #'repunctuate-sentences
  "s" #'sort-lines
  "u" #'untabify)

;; The minimal indicator shows cycling options, but I have no use
;; for those.  I want it to be silent.
(defun bard-embark-no-minimal-indicator ())
(advice-add #'embark-minimal-indicator :override #'bard-embark-no-minimal-indicator)

(provide 'bard-embark)
#+end_src

** bard-eshell
#+begin_src emacs-lisp :tangle bard-elisp/bard-eshell.el :mkdirp yes
  (require 'cl-lib)
  (require 'eshell)
#+end_src

*** Eshell aliases
#+begin_src emacs-lisp :tangle bard-elisp/bard-eshell.el :mkdirp yes
  ;; aliases
  (setq bard/eshell-aliases
        '((g  . magit)
          (gl . magit-log)
          (d  . dired)
          (o  . find-file)
          (oo . find-file-other-window)
          (l  . (lambda () (eshell/ls '-la)))
          (eshell/clear . eshell/clear-scrollback)))

  (mapc (lambda (alias)
          (defalias (car alias) (cdr alias)))
        bard/eshell-aliases)
#+end_src

*** Prot-eshell code
I believe Protesilaos has stopped using eshell, but I went back in his git history to the time when he used it in a video and took some custom elisp from there.

#+begin_src emacs-lisp :tangle bard-elisp/bard-eshell.el :mkdirp yes
  (defun prot-eshell--cd (dir)
    "Routine to cd into DIR."
    (delete-region eshell-last-output-end (point-max))
    (when (> eshell-last-output-end (point))
      (goto-char eshell-last-output-end))
    (insert-and-inherit "cd " (eshell-quote-argument dir))
    (eshell-send-input))

  (defun prot-eshell-complete-recent-dir (dir &optional arg)
    "Switch to a recent Eshell directory.
  When called interactively, DIR is selected with completion from
  the elements of `eshell-last-dir-ring'.
  With optional ARG prefix argument (\\[universal-argument]) also
  open the directory in a `dired' buffer."
    (interactive
     (list
      (if-let ((dirs (ring-elements eshell-last-dir-ring)))
          (completing-read "Switch to recent dir: " dirs nil t)
        (user-error "There is no Eshell history for recent directories"))
      current-prefix-arg))
    (prot-eshell--cd dir)
    ;; UPDATE 2022-01-04 10:48 +0200: The idea for `dired-other-window'
    ;; was taken from Sean Whitton's `spw/eshell-cd-recent-dir'.  Check
    ;; Sean's dotfiles: <https://git.spwhitton.name/dotfiles>.
    (when arg
      (dired-other-window dir)))

  (defun bard/eshell-find-file-at-point ()
    "Run `find-file` to find file"
    (interactive)
    (let ((file (ffap-file-at-point)))
      (if file
          (find-file file)
        (user-error "No file at point"))))

  (defcustom prot-eshell-output-buffer "*Exported Eshell output*"
    "Name of buffer with the last output of Eshell command.
  Used by `prot-eshell-export'."
    :type 'string
    :group 'prot-eshell)

  (defcustom prot-eshell-output-delimiter "* * *"
    "Delimiter for successive `prot-eshell-export' outputs.
  This is formatted internally to have newline characters before
  and after it."
    :type 'string
    :group 'prot-eshell)

  (defun prot-eshell--command-prompt-output ()
    "Capture last command prompt and its output."
    (let ((beg (save-excursion
                 (goto-char (eshell-beginning-of-input))
                 (goto-char (point-at-bol)))))
      (when (derived-mode-p 'eshell-mode)
        (buffer-substring-no-properties beg (eshell-end-of-output)))))

  ;;;###autoload
  (defun prot-eshell-export ()
    "Produce a buffer with output of the last Eshell command.
  If `prot-eshell-output-buffer' does not exist, create it.  Else
  append to it, while separating multiple outputs with
  `prot-eshell-output-delimiter'."
    (interactive)
    (let ((eshell-output (prot-eshell--command-prompt-output)))
      (with-current-buffer (get-buffer-create prot-eshell-output-buffer)
        (let ((inhibit-read-only t))
          (goto-char (point-max))
          (unless (eq (point-min) (point-max))
            (insert (format "\n%s\n\n" prot-eshell-output-delimiter)))
          (goto-char (point-at-bol))
          (insert eshell-output)
          (switch-to-buffer-other-window (current-buffer))))))

  (defgroup bard-eshell-faces nil
    "Faces for my custom modeline."
    :group 'prot-eshell-faces)

  (defface bard-eshell-highlight-yellow-bg
    '((default :inherit (bold prot-modeline-indicator-button))
      (((class color) (min-colors 88) (background light))
       :background "#805000" :foreground "white")
      (((class color) (min-colors 88) (background dark))
       :background "#ffc800" :foreground "black")
      (t :background "yellow" :foreground "black"))
    "Face for modeline indicators with a background."
    :group 'bard-eshell-faces)

  (defun prot-eshell-narrow-output-highlight-regexp (regexp)
    "Narrow to last command output and highlight REGEXP."
    (interactive
     (list (read-regexp "Regexp to highlight" nil 'prot-eshell--output-highlight-history)))
    (narrow-to-region (eshell-beginning-of-output)
                      (eshell-end-of-output))
    (goto-char (point-min))
    (highlight-regexp regexp 'prot-eshell-highlight-yellow-bg)
    (message "%s to last output and highlighted '%s'"
             (propertize "Narrowed" 'face 'bold)
             (propertize regexp 'face 'italic)))

  (defun select-or-create (arg)
    "Commentary ARG."
    (if (string= arg "New eshell")
        (eshell t)
      (switch-to-buffer arg)))
  (defun eshell-switcher (&optional arg)
    "Commentary ARG."
    (interactive)
    (let* (
           (buffers (cl-remove-if-not (lambda (n) (eq (buffer-local-value 'major-mode n) 'eshell-mode)) (buffer-list)) )
           (names (mapcar (lambda (n) (buffer-name n)) buffers))
           (num-buffers (length buffers) )
           (in-eshellp (eq major-mode 'eshell-mode)))
      (cond ((eq num-buffers 0) (eshell (or arg t)))
            ((not in-eshellp) (switch-to-buffer (car buffers)))
            (t (select-or-create (completing-read "Select Shell:" (cons "New eshell" names)))))))
#+end_src

*** Prompt configuration
#+begin_src emacs-lisp :tangle bard-elisp/bard-eshell.el :mkdirp yes
  ;; taken from https://github.com/karthink/.emacs.d/blob/master/lisp/setup-shells.el
  (use-package eshell
    :defer
    :config
    (setq eshell-prompt-regexp "^.* λ "
          eshell-prompt-function #'bard/eshell-default-prompt-fn)

    (defun bard/eshell-default-prompt-fn ()
      "Generate the prompt string for eshell. Use for `eshell-prompt-function'."
      (concat (if (bobp) "" "\n")
              (let ((pwd (eshell/pwd)))
                (propertize (if (equal pwd "~")
                                pwd
                              (abbreviate-file-name pwd))
                            'face 'bard/eshell-prompt-pwd))
              (propertize (bard/eshell--current-git-branch)
                          'face 'bard/eshell-prompt-git-branch)
              (propertize " λ" 'face (if (zerop eshell-last-command-status) 'success 'error))
              " "))

    (defsubst bard/eshell--current-git-branch ()
      ;; TODO Refactor me
      (cl-destructuring-bind (status . output)
          (with-temp-buffer (cons
                             (or (call-process "git" nil t nil "symbolic-ref" "-q" "--short" "HEAD")
                                 (call-process "git" nil t nil "describe" "--all" "--always" "HEAD")
                                 -1)
                             (string-trim (buffer-string))))
        (if (equal status 0)
            (format " [%s]" output)
          "")))

    (defface bard/eshell-prompt-pwd '((t (:inherit font-lock-keyword-face)))
      "TODO"
      :group 'eshell)

    (defface bard/eshell-prompt-git-branch '((t (:inherit font-lock-builtin-face)))
      "TODO"
      :group 'eshell))
#+end_src

*** Provide library
#+begin_src emacs-lisp :tangle bard-elisp/bard-eshell.el :mkdirp yes
  (provide 'bard-eshell)
#+end_src

** bard-media
*** Load required libraries
#+begin_src emacs-lisp :tangle bard-elisp/bard-media.el :mkdirp yes
  (require 'cl-lib)
  (require 'seq)
  (require 'emms)
  (require 'image-dired)
  (require 'dired-x)
#+end_src

*** EMMS Convenience functions
Somewhat related to [[id:3ca0c5a0-4829-4411-a80b-f2fe69650ea0][Elfeed/EMMS Youtube Code]].

#+begin_src emacs-lisp :tangle bard-elisp/bard-media.el :mkdirp yes
  (defun bard/play-youtube-video ()
    "Play the YouTube URL at point or prompt for one if none is found."
    (interactive)
    (let* ((url-at-point (thing-at-point 'url t))
           (url (if (and url-at-point
                         (string-match-p "https?://\\(www\\.\\)?\\(youtube\\.com\\|youtu\\.be\\)" url-at-point))
                    url-at-point
                  (read-string "Enter YouTube URL: "))))
      (if (and url (string-match-p "https?://\\(www\\.\\)?\\(youtube\\.com\\|youtu\\.be\\)" url))
          (async-shell-command (format "mpv '%s'" url))
        (message "The URL is not a valid YouTube link: %s" url))))

  (defun bard/save-emms-watch-later ()
    "Save the current EMMS playlist to `bard/watch-later-file` using `bard/emms-playlist-format`."
    (interactive)
    (when (and bard/watch-later-file bard/emms-playlist-format)
      (emms-playlist-save bard/emms-playlist-format bard/watch-later-file)
      (message "Playlist saved to %s" bard/watch-later-file)))

  (defun bard/emms-download-current-video (destination)
    "Download the currently playing EMMS video and move it to DESTINATION."
    (interactive "DSelect destination directory: ")
    (require 'emms)
    (let* ((track (emms-playlist-current-selected-track))
           (url (emms-track-get track 'name))
           (default-directory (file-name-as-directory temporary-file-directory))
           (downloader (executable-find "yt-dlp"))
           (output-template "%(title)s.%(ext)s"))
      (unless downloader
        (error "yt-dlp or youtube-dl is not installed or not in PATH"))
      (unless (string-match-p "^https?://" url)
        (error "Current track is not a valid video URL"))

      (let ((cmd (format "%s -o \"%s\" \"%s\""
                         downloader output-template url)))
        (message "Downloading video from: %s" url)
        (let ((exit-code (shell-command cmd)))
          (if (not (eq exit-code 0))
              (error "Download failed, see *Messages* for details")
            ;; Move the downloaded file
            (let* ((downloaded-file (car (directory-files default-directory t ".*\\(mp4\\|mkv\\|webm\\)$" 'time)))
                   (target-path (expand-file-name (file-name-nondirectory downloaded-file) destination)))
              (rename-file downloaded-file target-path t)
              (message "Video saved to: %s" target-path)))))))
#+end_src

*** Interacting with ~nsxiv~ image browser through elisp
I made a video about this workflow [[https://www.youtube.com/watch?v=fEUQEK_uXk0][here]].

#+begin_src emacs-lisp :tangle bard-elisp/bard-media.el :mkdirp yes
  (defun bard/image-browser-choose (directory)
    "Open nsxiv in thumbnail mode on DIRECTORY.
  Asks the user whether to enable recursive mode and whether to output marked files to a buffer."
    (interactive "DSelect directory: ")
    (let* ((recursive (if (y-or-n-p "Recursive searching? ") "-r" ""))
           (stdout (if (y-or-n-p "Output marked files to buffer? ") "-o" ""))
           (full-dir (expand-file-name directory))
           (args (remove "" (list "nsxiv" "-t" stdout recursive full-dir))))
      ;; Pre-clear the output buffer if needed
      (when (string= stdout "-o")
        (with-current-buffer (get-buffer-create "*nsxiv*")
          (read-only-mode 0)
          (erase-buffer)))

      (message "Running: %s" (string-join args " "))

      (let ((process (apply #'start-process "nsxiv" "*nsxiv*" args)))
        (when (string= stdout "-o")
          (set-process-sentinel
           process
           (lambda (proc event)
             (when (string= event "finished\n")
               (with-current-buffer "*nsxiv*"
                 (read-only-mode nil)
                 (goto-char (point-min)))
               ;; Read marked files
               (let ((files (with-current-buffer "*nsxiv*"
                              (split-string (buffer-string) "\n" t))))
                 (bard/open-marked-in-dired files)))))
          (pop-to-buffer "*nsxiv*")))))

  (defun bard/open-marked-in-dired (files)
    "Open a list of FILES in an interactive Dired buffer."
    (if (and files (listp files))
        (dired (cons "*nsxiv-marked*" files))
      (message "No valid files to show in Dired.")))

  (defun bard/image-browser-marked ()
    "Open nsxiv on the marked files in Dired.
  Assumes that files have already been validated."
    (let ((files (dired-get-marked-files)))
      (message "Opening marked files: %s" (string-join files ", "))
      (apply #'start-process "nsxiv" "*nsxiv*" "nsxiv" "-t" files)))

  (defun bard/image-browser ()
    "Open nsxiv in a context-sensitive way:
  - If in Dired with marked files, open those with nsxiv.
  - If in Dired with no marked files, prompt for a directory.
  - If not in Dired, prompt for a directory."
    (interactive)
    (cond
     ;; In Dired and files are marked
     ((and (derived-mode-p 'dired-mode)
           (< 1 (length (dired-get-marked-files))))
      (message "Opening marked files from Dired...")
      (bard/image-browser-marked))

     ;; In Dired but no marked files
     ((derived-mode-p 'dired-mode)
      (message "No files marked in Dired. Prompting for directory...")
      (call-interactively #'bard/image-browser-choose))

     ;; Not in Dired
     (t
      (message "Not in Dired. Prompting for directory...")
      (call-interactively #'bard/image-browser-choose))))

  (provide 'bard-media.el)
#+end_src

** bard-modeline
This is another one of prot's libraries that I copied and modified a long time ago. The modifications this time were quite insignificant, but I did add some custom modules that I wanted. 

#+begin_src emacs-lisp :tangle bard-elisp/bard-modeline.el :mkdirp yes
  (require 'prot-common)

  (defgroup prot-modeline nil
    "Custom modeline that is stylistically close to the default."
    :group 'mode-line)

  (defgroup prot-modeline-faces nil
    "Faces for my custom modeline."
    :group 'prot-modeline)

  (defcustom prot-modeline-string-truncate-length 9
    "String length after which truncation should be done in small windows."
    :type 'natnum)

  (defun mode-line-window-selected-p ()
    "Return non-nil if we're updating the mode line for the selected window.
  This function is meant to be called in `:eval' mode line
  constructs to allow altering the look of the mode line depending
  on whether the mode line belongs to the currently selected window
  or not."
    (let ((window (selected-window)))
      (or (eq window (old-selected-window))
      (and (minibuffer-window-active-p (minibuffer-window))
           (with-selected-window (minibuffer-window)
             (eq window (minibuffer-selected-window)))))))

  ;;;; Faces

  (defface prot-modeline-indicator-button nil
    "Generic face used for indicators that have a background.
  Modify this face to, for example, add a :box attribute to all
  relevant indicators (combines nicely with my `spacious-padding'
  package).")

  (defface prot-modeline-indicator-red
    '((default :inherit bold)
      (((class color) (min-colors 88) (background light))
       :foreground "#880000")
      (((class color) (min-colors 88) (background dark))
       :foreground "#ff9f9f")
      (t :foreground "red"))
    "Face for modeline indicators (e.g. see my `notmuch-indicator')."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-red-bg
    '((default :inherit (bold prot-modeline-indicator-button))
      (((class color) (min-colors 88) (background light))
       :background "#aa1111" :foreground "white")
      (((class color) (min-colors 88) (background dark))
       :background "#ff9090" :foreground "black")
      (t :background "red" :foreground "black"))
    "Face for modeline indicators with a background."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-green
    '((default :inherit bold)
      (((class color) (min-colors 88) (background light))
       :foreground "#005f00")
      (((class color) (min-colors 88) (background dark))
       :foreground "#73fa7f")
      (t :foreground "green"))
    "Face for modeline indicators (e.g. see my `notmuch-indicator')."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-green-bg
    '((default :inherit (bold prot-modeline-indicator-button))
      (((class color) (min-colors 88) (background light))
       :background "#207b20" :foreground "white")
      (((class color) (min-colors 88) (background dark))
       :background "#77d077" :foreground "black")
      (t :background "green" :foreground "black"))
    "Face for modeline indicators with a background."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-yellow
    '((default :inherit bold)
      (((class color) (min-colors 88) (background light))
       :foreground "#6f4000")
      (((class color) (min-colors 88) (background dark))
       :foreground "#f0c526")
      (t :foreground "yellow"))
    "Face for modeline indicators (e.g. see my `notmuch-indicator')."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-yellow-bg
    '((default :inherit (bold prot-modeline-indicator-button))
      (((class color) (min-colors 88) (background light))
       :background "#805000" :foreground "white")
      (((class color) (min-colors 88) (background dark))
       :background "#ffc800" :foreground "black")
      (t :background "yellow" :foreground "black"))
    "Face for modeline indicators with a background."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-blue
    '((default :inherit bold)
      (((class color) (min-colors 88) (background light))
       :foreground "#00228a")
      (((class color) (min-colors 88) (background dark))
       :foreground "#88bfff")
      (t :foreground "blue"))
    "Face for modeline indicators (e.g. see my `notmuch-indicator')."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-blue-bg
    '((default :inherit (bold prot-modeline-indicator-button))
      (((class color) (min-colors 88) (background light))
       :background "#0000aa" :foreground "white")
      (((class color) (min-colors 88) (background dark))
       :background "#77aaff" :foreground "black")
      (t :background "blue" :foreground "black"))
    "Face for modeline indicators with a background."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-magenta
    '((default :inherit bold)
      (((class color) (min-colors 88) (background light))
       :foreground "#6a1aaf")
      (((class color) (min-colors 88) (background dark))
       :foreground "#e0a0ff")
      (t :foreground "magenta"))
    "Face for modeline indicators (e.g. see my `notmuch-indicator')."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-magenta-bg
    '((default :inherit (bold prot-modeline-indicator-button))
      (((class color) (min-colors 88) (background light))
       :background "#6f0f9f" :foreground "white")
      (((class color) (min-colors 88) (background dark))
       :background "#e3a2ff" :foreground "black")
      (t :background "magenta" :foreground "black"))
    "Face for modeline indicators with a background."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-cyan
    '((default :inherit bold)
      (((class color) (min-colors 88) (background light))
       :foreground "#004060")
      (((class color) (min-colors 88) (background dark))
       :foreground "#30b7cc")
      (t :foreground "cyan"))
    "Face for modeline indicators (e.g. see my `notmuch-indicator')."
    :group 'prot-modeline-faces)

  (defface prot-modeline-indicator-cyan-bg
    '((default :inherit (bold prot-modeline-indicator-button))
      (((class color) (min-colors 88) (background light))
       :background "#006080" :foreground "white")
      (((class color) (min-colors 88) (background dark))
       :background "#40c0e0" :foreground "black")
      (t :background "cyan" :foreground "black"))
    "Face for modeline indicators with a background."
    :group 'prot-modeline-faces)

  ;;;; Common helper functions

  (defun prot-modeline--string-truncate-p (str)
    "Return non-nil if STR should be truncated."
    (and (prot-common-window-small-p)
         (> (length str) prot-modeline-string-truncate-length)
         (not (one-window-p :no-minibuffer))))

  (defun prot-modeline--truncate-p ()
    "Return non-nil if truncation should happen.
  This is a more general and less stringent variant of
  `prot-modeline--string-truncate-p'."
    (and (prot-common-window-small-p)
         (not (one-window-p :no-minibuffer))))

  (defun prot-modeline-string-truncate (str)
    "Return truncated STR, if appropriate, else return STR.
  Truncation is done up to `prot-modeline-string-truncate-length'."
    (if (prot-modeline--string-truncate-p str)
        (concat (substring str 0 prot-modeline-string-truncate-length) "...")
      str))

  (defun prot-modeline-string-truncate-end (str)
    "Like `prot-modeline-string-truncate' but truncate from STR beginning."
    (if (prot-modeline--string-truncate-p str)
        (concat "..." (substring str (- prot-modeline-string-truncate-length)))
      str))

  (defun prot-modeline--first-char (str)
    "Return first character from STR."
    (substring str 0 1))

  (defun prot-modeline-string-abbreviate (str)
    "Abbreviate STR individual hyphen or underscore separated words.
  Also see `prot-modeline-string-abbreviate-but-last'."
    (if (prot-modeline--string-truncate-p str)
        (mapconcat #'prot-modeline--first-char (split-string str "[_-]") "-")
      str))

  (defun prot-modeline-string-abbreviate-but-last (str nthlast)
    "Abbreviate STR, keeping NTHLAST words intact.
  Also see `prot-modeline-string-abbreviate'."
    (if (prot-modeline--string-truncate-p str)
        (let* ((all-strings (split-string str "[_-]"))
               (nbutlast-strings (nbutlast (copy-sequence all-strings) nthlast))
               (last-strings (nreverse (ntake nthlast (nreverse (copy-sequence all-strings)))))
               (first-component (mapconcat #'prot-modeline--first-char nbutlast-strings "-"))
               (last-component (mapconcat #'identity last-strings "-")))
          (if (string-empty-p first-component)
              last-component
            (concat first-component "-" last-component)))
      str))

  ;;;; Keyboard macro indicator

  (defvar-local prot-modeline-kbd-macro
      '(:eval
        (when (and (mode-line-window-selected-p) defining-kbd-macro)
          (propertize " KMacro " 'face 'prot-modeline-indicator-blue-bg)))
    "Mode line construct displaying `mode-line-defining-kbd-macro'.
  Specific to the current window's mode line.")

  ;;;; Narrow indicator

  (defvar-local prot-modeline-narrow
      '(:eval
        (when (and (mode-line-window-selected-p)
                   (buffer-narrowed-p)
                   (not (derived-mode-p 'Info-mode 'help-mode 'special-mode 'message-mode)))
          (propertize " Narrow " 'face 'prot-modeline-indicator-cyan-bg)))
    "Mode line construct to report the multilingual environment.")

  ;;;; Centered cursor indicator
  (defvar-local bard-modeline-centered-cursor
      '(:eval
        (when (and (mode-line-window-selected-p)
                   (bard/cursor-centered-p)
                   (not (derived-mode-p 'Info-mode 'help-mode 'special-mode 'message-mode)))
          (propertize " Center " 'face 'prot-modeline-indicator-yellow-bg)))
    "Mode line construct to report the multilingual environment.")

  ;; FIXME: Combine these two functions one day...
  (defvar-local bard-modeline-ryo-modal-normal
      '(:eval
        (when (and (mode-line-window-selected-p)
                   (not (bard/ryo-insert-p))
                   (not (derived-mode-p 'Info-mode 'help-mode 'special-mode 'message-mode)))
          (propertize "<N>" 'face 'prot-modeline-indicator-magenta-bg))
        )
    "Mode line construct to show normal mode for ryo-modal.")

  (defvar-local bard-modeline-ryo-modal-insert
      '(:eval
        (when (and (mode-line-window-selected-p)
                   (bard/ryo-insert-p)
                   (not (derived-mode-p 'Info-mode 'help-mode 'special-mode 'message-mode)))
          (propertize "<I>" 'face 'prot-modeline-indicator-blue-bg))
        )
    "Mode line construct to show insert mode for ryo-modal.")

  ;;;; Input method

  (defvar-local prot-modeline-input-method
      '(:eval
        (when current-input-method-title
          (propertize (format " %s " current-input-method-title)
                      'face 'prot-modeline-indicator-green-bg
                      'mouse-face 'mode-line-highlight)))
    "Mode line construct to report the multilingual environment.")

  ;;;; Buffer status
  (defvar-local prot-modeline-buffer-status
      '(:eval
        (when (file-remote-p default-directory)
          (propertize " @ "
                      'face 'prot-modeline-indicator-red-bg
                      'mouse-face 'mode-line-highlight)))
    "Mode line construct for showing remote file name.")

  ;;;; Dedicated window

  (defvar-local prot-modeline-window-dedicated-status
      '(:eval
        (when (window-dedicated-p)
          (propertize " = "
                      'face 'prot-modeline-indicator-magenta-bg
                      'mouse-face 'mode-line-highlight)))
    "Mode line construct for dedicated window indicator.")

  ;;;; Buffer name and modified status

  (defun prot-modeline-buffer-identification-face ()
    "Return appropriate face or face list for `prot-modeline-buffer-identification'."
    (let ((file (buffer-file-name)))
      (cond
       ((and (mode-line-window-selected-p)
             file
             (buffer-modified-p))
        '(error italic mode-line-buffer-id))
       ((and file (buffer-modified-p))
        'italic)
       ((mode-line-window-selected-p)
        'mode-line-buffer-id))))

  (defun prot-modeline--buffer-name ()
    "Return `buffer-name', truncating it if necessary.
  See `prot-modeline-string-truncate'."
    (when-let ((name (buffer-name)))
      (prot-modeline-string-truncate name)))

  (defun prot-modeline-buffer-name ()
    "Return buffer name, with read-only indicator if relevant."
    (let ((name (prot-modeline--buffer-name)))
      (if buffer-read-only
          (format "%s %s" (char-to-string #xE0A2) name)
        name)))

  (defun prot-modeline-buffer-name-help-echo ()
    "Return `help-echo' value for `prot-modeline-buffer-identification'."
    (concat
     (propertize (buffer-name) 'face 'mode-line-buffer-id)
     "\n"
     (propertize
      (or (buffer-file-name)
          (format "No underlying file.\nDirectory is: %s" default-directory))
      'face 'font-lock-doc-face)))

  (defvar-local prot-modeline-buffer-identification
      '(:eval
        (propertize (prot-modeline-buffer-name)
                    'face (prot-modeline-buffer-identification-face)
                    'mouse-face 'mode-line-highlight
                    'help-echo (prot-modeline-buffer-name-help-echo)))
    "Mode line construct for identifying the buffer being displayed.
  Propertize the current buffer with the `mode-line-buffer-id'
  face.  Let other buffers have no face.")

  ;;;; Major mode

  (defun prot-modeline-major-mode-indicator ()
    "Return appropriate propertized mode line indicator for the major mode."
    (let ((indicator (cond
                      ((derived-mode-p 'text-mode) "§")
                      ((derived-mode-p 'prog-mode) "λ")
                      ((derived-mode-p 'term-mode) ">_")
                      ((derived-mode-p 'emms-playlist-mode) "♪")
                      (t "◦"))))
      (propertize indicator 'face 'shadow)))

  (defun prot-modeline-major-mode-name ()
    "Return capitalized `major-mode' without the -mode suffix."
    (capitalize (string-replace "-mode" "" (symbol-name major-mode))))

  (defun prot-modeline-major-mode-help-echo ()
    "Return `help-echo' value for `prot-modeline-major-mode'."
    (if-let ((parent (get major-mode 'derived-mode-parent)))
        (format "Symbol: `%s'.  Derived from: `%s'" major-mode parent)
      (format "Symbol: `%s'." major-mode)))

  (defvar-local prot-modeline-major-mode
      (list
       (propertize "%[" 'face 'prot-modeline-indicator-red)
       '(:eval
         (concat
          (prot-modeline-major-mode-indicator)
          " "
          (propertize
           (prot-modeline-string-abbreviate-but-last
            (prot-modeline-major-mode-name)
            2)
           'mouse-face 'mode-line-highlight
           'help-echo (prot-modeline-major-mode-help-echo))))
       (propertize "%]" 'face 'prot-modeline-indicator-red))
    "Mode line construct for displaying major modes.")

  (defvar-local prot-modeline-process
      (list '("" mode-line-process))
    "Mode line construct for the running process indicator.")

  ;;;; Git branch and diffstat

  (declare-function vc-git--symbolic-ref "vc-git" (file))

  (defun prot-modeline--vc-branch-name (file backend)
    "Return capitalized VC branch name for FILE with BACKEND."
    (when-let ((rev (vc-working-revision file backend))
               (branch (or (vc-git--symbolic-ref file)
                           (substring rev 0 7))))
      (capitalize branch)))

  (declare-function vc-git-working-revision "vc-git" (file))

  (defvar prot-modeline-vc-map
    (let ((map (make-sparse-keymap)))
      (define-key map [mode-line down-mouse-1] 'vc-diff)
      (define-key map [mode-line down-mouse-3] 'vc-root-diff)
      map)
    "Keymap to display on VC indicator.")

  (defun prot-modeline--vc-help-echo (file)
    "Return `help-echo' message for FILE tracked by VC."
    (format "Revision: %s\nmouse-1: `vc-diff'\nmouse-3: `vc-root-diff'"
            (vc-working-revision file)))

  (defun prot-modeline--vc-text (file branch &optional face)
    "Prepare text for Git controlled FILE, given BRANCH.
  With optional FACE, use it to propertize the BRANCH."
    (concat
     (propertize (char-to-string #xE0A0) 'face 'shadow)
     " "
     (propertize branch
                 'face face
                 'mouse-face 'mode-line-highlight
                 'help-echo (prot-modeline--vc-help-echo file)
                 'local-map prot-modeline-vc-map)
     ;; " "
     ;; (prot-modeline-diffstat file)
     ))

  (defun prot-modeline--vc-details (file branch &optional face)
    "Return Git BRANCH details for FILE, truncating it if necessary.
  The string is truncated if the width of the window is smaller
  than `split-width-threshold'."
    (prot-modeline-string-truncate
     (prot-modeline--vc-text file branch face)))

  (defvar prot-modeline--vc-faces
    '((added . vc-locally-added-state)
      (edited . vc-edited-state)
      (removed . vc-removed-state)
      (missing . vc-missing-state)
      (conflict . vc-conflict-state)
      (locked . vc-locked-state)
      (up-to-date . vc-up-to-date-state))
    "VC state faces.")

  (defun prot-modeline--vc-get-face (key)
    "Get face from KEY in `prot-modeline--vc-faces'."
     (alist-get key prot-modeline--vc-faces 'up-to-date))

  (defun prot-modeline--vc-face (file backend)
    "Return VC state face for FILE with BACKEND."
    (prot-modeline--vc-get-face (vc-state file backend)))

  (defvar-local prot-modeline-vc-branch
      '(:eval
        (when-let* (((mode-line-window-selected-p))
                    (file (buffer-file-name))
                    (backend (vc-backend file))
                    ;; ((vc-git-registered file))
                    (branch (prot-modeline--vc-branch-name file backend))
                    (face (prot-modeline--vc-face file backend)))
          (prot-modeline--vc-details file branch face)))
    "Mode line construct to return propertized VC branch.")

  ;;;; Flymake errors, warnings, notes

  (declare-function flymake--severity "flymake" (type))
  (declare-function flymake-diagnostic-type "flymake" (diag))

  ;; Based on `flymake--mode-line-counter'.
  (defun prot-modeline-flymake-counter (type)
    "Compute number of diagnostics in buffer with TYPE's severity.
  TYPE is usually keyword `:error', `:warning' or `:note'."
    (let ((count 0))
      (dolist (d (flymake-diagnostics))
        (when (= (flymake--severity type)
                 (flymake--severity (flymake-diagnostic-type d)))
          (cl-incf count)))
      (when (cl-plusp count)
        (number-to-string count))))

  (defvar prot-modeline-flymake-map
    (let ((map (make-sparse-keymap)))
      (define-key map [mode-line down-mouse-1] 'flymake-show-buffer-diagnostics)
      (define-key map [mode-line down-mouse-3] 'flymake-show-project-diagnostics)
      map)
    "Keymap to display on Flymake indicator.")

  (defmacro prot-modeline-flymake-type (type indicator &optional face)
    "Return function that handles Flymake TYPE with stylistic INDICATOR and FACE."
    `(defun ,(intern (format "prot-modeline-flymake-%s" type)) ()
       (when-let ((count (prot-modeline-flymake-counter
                          ,(intern (format ":%s" type)))))
         (concat
          (propertize ,indicator 'face 'shadow)
          (propertize count
                      'face ',(or face type)
                       'mouse-face 'mode-line-highlight
                       ;; FIXME 2023-07-03: Clicking on the text with
                       ;; this buffer and a single warning present, the
                       ;; diagnostics take up the entire frame.  Why?
                       'local-map prot-modeline-flymake-map
                       'help-echo "mouse-1: buffer diagnostics\nmouse-3: project diagnostics")))))

  (prot-modeline-flymake-type error "☣")
  (prot-modeline-flymake-type warning "!")
  (prot-modeline-flymake-type note "·" success)

  (defvar-local prot-modeline-flymake
      `(:eval
        (when (and (bound-and-true-p flymake-mode)
                   (mode-line-window-selected-p))
          (list
           ;; See the calls to the macro `prot-modeline-flymake-type'
           '(:eval (prot-modeline-flymake-error))
           '(:eval (prot-modeline-flymake-warning))
           '(:eval (prot-modeline-flymake-note)))))
    "Mode line construct displaying `flymake-mode-line-format'.
  Specific to the current window's mode line.")

  (with-eval-after-load 'eglot
    (setq mode-line-misc-info
          (delete '(eglot--managed-mode (" [" eglot--mode-line-format "] ")) mode-line-misc-info)))

  (defvar-local prot-modeline-eglot
      `(:eval
        (when (and (featurep 'eglot) (mode-line-window-selected-p))
          '(eglot--managed-mode eglot--mode-line-format)))
    "Mode line construct displaying Eglot information.
  Specific to the current window's mode line.")

  (defvar-local prot-modeline-notmuch-indicator
      '(notmuch-indicator-mode
        (" "
         (:eval (when (mode-line-window-selected-p)
                  notmuch-indicator--counters))))
    "The equivalent of `notmuch-indicator-mode-line-construct'.
  Display the indicator only on the focused window's mode line.")

  (defvar-local bard-evil-state-indicator
    '(:eval
      (when (and (bound-and-true-p evil-local-mode)
                 (mode-line-window-selected-p))
        (let ((state-label
               (pcase evil-state
                 ('normal  (propertize " <N>" 'face 'prot-modeline-indicator-green))
                 ('insert  (propertize " <I> " 'face 'prot-modeline-indicator-blue))
                 ('visual  (propertize " <V> " 'face 'prot-modeline-indicator-yellow))
                 ('replace (propertize " <R> " 'face 'prot-modeline-indicator-red))
                 ('emacs   (propertize " <E> " 'face 'prot-modeline-indicator-magenta))
                 ('motion  (propertize " <V> " 'face 'prot-modeline-indicator-cyan))
                 (_        (propertize " <> " 'face 'shadow)))))
          state-label)))
    "Modeline indicator for current Evil state.")


  ;;;; Org clock indicator
  (defun bard-modeline--org-clock-check-overrun ()
    "Check if the clock is overrun."
    (when org-clock-effort
      (> (org-clock-get-clocked-time)
         (org-duration-to-minutes org-clock-effort))))

  (defun bard-modeline--org-clock-string ()
    "Make a string for org-clock indicator."
    (let* ((clocked-mins (org-clock-get-clocked-time))
           (work-time (org-duration-from-minutes clocked-mins)))
      (if org-clock-effort
          (format "[%s/%s]"
                  work-time
                  org-clock-effort)
        (format "[%s]" work-time))))

  (defun bard-modeline--org-clock-choose-face ()
    "Make the background red when overrunning the clock."
    (if (bard-modeline--org-clock-check-overrun)
        'prot-modeline-indicator-red-bg
      'prot-modeline-indicator-green-bg))

  (defvar-local bard-modeline-org-clock
    '(:eval
      (when (and (featurep 'org)
                 (bound-and-true-p org-clock-current-task)
                 (mode-line-window-selected-p))
        (propertize
         (concat " ⊙ "
                 (prot-modeline-string-truncate
                  (bard-modeline--org-clock-string))
                 " ")
         'face (bard-modeline--org-clock-choose-face)
         'mouse-face 'mode-line-highlight
         'help-echo "Org clocked task\nmouse-1: Go to task"
         'local-map (let ((map (make-sparse-keymap)))
                      (define-key map [mode-line down-mouse-1]
                        #'org-clock-goto)
                      map))))
    "Mode line construct showing the current Org clocked task.")

  ;;;; Miscellaneous

  ;; (setq global-mode-string
  ;;       (remove 'org-mode-line-string global-mode-string))

  ;; (delq 'org-mode-line-string global-mode-string)

  (with-eval-after-load 'org
    (setq mode-line-misc-info
          (delete 'org-mode-line-string mode-line-misc-info)))

  (defvar-local prot-modeline-misc-info
      '(:eval
        (when (mode-line-window-selected-p)
          mode-line-misc-info))
    "Mode line construct displaying `mode-line-misc-info'.
  Specific to the current window's mode line.")

  ;;;; Risky local variables

  ;; NOTE 2023-04-28: The `risky-local-variable' is critical, as those
  ;; variables will not work without it.
  (dolist (construct '(prot-modeline-kbd-macro
                       prot-modeline-narrow
                       bard-modeline-centered-cursor
                       bard-evil-state-indicator
                       prot-modeline-input-method
                       bard-modeline-org-clock
                       prot-modeline-buffer-status
                       prot-modeline-window-dedicated-status
                       prot-modeline-evil
                       prot-modeline-buffer-identification
                       prot-modeline-major-mode
                       prot-modeline-process
                       prot-modeline-vc-branch
                       prot-modeline-flymake
                       prot-modeline-eglot
                       prot-modeline-misc-info
                       prot-modeline-notmuch-indicator))
    (put construct 'risky-local-variable t))

  (provide 'bard-modeline)
#+end_src

** bard-package
I renamed the functions from [[https://manueluberti.eu/posts/2021-09-01-package-report/][this post]] because otherwise I would forget I had them in my system. I keep personal/custom functions prefixed with ~bard/~ so I can look at them through minibuffer if I forgot the name. This shows me that they are not part of default Emacs.
#+begin_src emacs-lisp :tangle bard-elisp/bard-package.el :mkdirp yes
;; taken and renamed functions from 

(defun bard/package-report ()
  "Report total package counts grouped by archive."
  (interactive)
  (package-refresh-contents)
  (bard/display-package-report
   (let* ((arch-pkgs (bard/archive-packages))
          (counts (seq-sort-by #'cdr #'> (bard/archive-counts arch-pkgs)))
          (by-arch (seq-group-by #'car arch-pkgs)))
     (concat
      (format "Total packages: %s\n\n" (apply #'+ (mapcar #'cdr counts)))
      (mapconcat
       (lambda (archive)
         (concat "• "
                 (format "%s (%s)" (car archive) (cdr archive))
                 ": "
                 (mapconcat (lambda (ap-pair) (cdr ap-pair))
                            (alist-get (car archive) by-arch)
                            ", ")))
       counts
       "\n\n")))))

(defun bard/display-package-report (output)
  "Display OUTPUT in a popup buffer."
  (let ((buffer-name "*package-report*"))
    (with-help-window buffer-name
      (with-current-buffer buffer-name
        (visual-line-mode 1)
        (erase-buffer)
        (insert output)
        (goto-char (point-min))))))

(defun bard/archive-packages ()
  "Return a list of (archive . package) cons cells."
  (seq-reduce
   (lambda (res package)
     (let ((archive (package-desc-archive
                     (cadr (assq package package-archive-contents))))
           (pkg (symbol-name package)))
       (push (cons archive pkg) res)))
   (mapcar #'car package-alist)
   nil))

(defun bard/archive-counts (arch-pkgs)
  "Return a list of cons cells from alist ARCH-PKGS.
The cars are package archives, the cdrs are the number of
packages installed from each archive."
  (seq-reduce
   (lambda (counts key)
     (cons (cons key (+ 1 (or (cdr (assoc key counts)) 0)))
           (assoc-delete-all key counts)))
   (mapcar #'car arch-pkgs)
   nil))

(provide 'bard-package)

#+end_src

** bard-theme
*** Fontaine font presets
The main configuration for fontaine is found at [[id:542a0f2e-f79e-4272-af55-284c81b16fc3][bard-emacs-theme::Fonts]]. These are the presets for different fonts that I can switch between using ~C-c f~.

#+begin_src emacs-lisp :tangle bard-elisp/bard-theme.el :mkdirp yes
  (setq fontaine-presets
        '((default
           :default-height 140
           :default-family "Iosevka Comfy"
           :variable-pitch-family "Iosevka Comfy"
           :variable-pitch-height 1.0
           :fixed-pitch-family "Iosevka Comfy"
           :fixed-pitch-height 1.0
           :bold-weight bold
           )
          (tiny
           :inherit default
           :default-height 135)
          (wide
           :default-height 135
           :default-family "Iosevka Comfy Wide"
           :fixed-pitch-family "Iosevka Comfy Wide"
           :variable-pitch-family "Iosevka Comfy Wide Motion Duo")
          (prot
           :default-family "Iosevka Comfy Wide Motion"
           :default-height 130
           :default-weight medium
           :fixed-pitch-family "Iosevka Comfy Wide Motion"
           :variable-pitch-family "Iosevka Comfy Wide Motion Duo"
           :bold-weight extrabold)
          (mono
           :default-height 130
           :default-family "monospace"
           :fixed-pitch-family "monospace"
           :variable-pitch-family "Baskerville"
           :variable-pitch-height 140)
          (mono-large
           :inherit mono
           :default-height 150
           :variable-pitch-height 160)
          (mac
           :default-height 130
           :default-family "Monaco"
           :variable-pitch-family "Monaco"
           :fixed-pitch-family "Monaco")
          (large
           :inherit default
           :default-height 160)
          (huge
           :inherit default
           :default-height 180)
          (t
           :default-family "Monospace")))
#+end_src

*** Japanese CJK font
Explanation provided by [[https://tatsumoto-ren.github.io/blog/japanese-fonts.html][AJATT — Japanese fonts]]. Essentially: Chinese fonts and Japanese fonts look different for certain Kanji. Since I don't want Chinese glyphs prioritized in Emacs over Japanese ones, I set the font to the Japanese (JP) version of Noto CJK font which is a very accessible and popular font for Asian languages.

#+begin_src emacs-lisp :tangle bard-elisp/bard-theme.el :mkdirp yes
  (set-fontset-font
   t 'han
   (font-spec :family "Noto Serif CJK JP") nil 'prepend)
#+end_src

*** Switching themes
#+begin_src emacs-lisp :tangle bard-elisp/bard-theme.el :mkdirp yes
  (defun bard/disable-all-themes ()
    "disable all active themes."
    (interactive)
    (dolist (i custom-enabled-themes)
      (disable-theme i)))

  (defvar bard/after-theme-load-hook nil
    "Hook that runs after a new theme is loaded using `bard/select-theme`.")

  (dolist (hook '(bard/after-theme-load-hook))
    (add-hook hook #'fontaine-apply-current-preset)
    (add-hook hook #'logos-update-fringe-in-buffers))

  (defun bard/select-theme (&optional theme)
    "Enable the specified THEME, or prompt the user to select one if THEME is nil."
    (interactive
     (list
      (completing-read "Select theme: "
                       (mapcar 'symbol-name (custom-available-themes)))))
    (let* ((theme-symbol (if (symbolp theme) theme (intern theme)))
           (theme-name (symbol-name theme-symbol))
           (display-theme-name (if (string-suffix-p "-theme" theme-name)
                                   (substring theme-name 0 -6)
                                 theme-name))
           (colored-theme-name (propertize display-theme-name 'face '(:weight bold))))
      (bard/disable-all-themes)
      (load-theme theme-symbol t)
      (message "Loaded the %s theme" colored-theme-name)
      (run-hooks 'bard/after-theme-load-hook)))
#+end_src

*** Updating cursor for text mode
#+begin_src emacs-lisp :tangle bard-elisp/bard-theme.el :mkdirp yes
  (defvar my-last-cursor-type nil)

  (defun bard/update-cursor-type ()
    "Set cursor type to 'bar in text modes, 'box otherwise.
  leave it alone in pdf-view-mode."
    (unless (derived-mode-p 'pdf-view-mode)
      (let ((new-cursor (if (derived-mode-p 'text-mode) 'bar 'box)))
        (unless (eq my-last-cursor-type new-cursor)
          (setq cursor-type new-cursor)
          (setq my-last-cursor-type new-cursor)))))

  (add-hook 'post-command-hook #'bard/update-cursor-type)
#+end_src

*** Heading faces
#+begin_src emacs-lisp :tangle bard-elisp/bard-theme.el :mkdirp yes
  (defun bard/outline-heading-faces ()
    (set-face-attribute 'org-document-title nil
                        :inherit '(outline-1 variable-pitch)
                        :weight 'light
                        :height 1.5)

    (set-face-attribute 'org-level-1 nil
                        :inherit '(outline-2 variable-pitch)
                        :weight 'light
                        :height 1.3)

    (set-face-attribute 'org-level-2 nil
                        :inherit 'outline-3
                        :height 1.2)

    (set-face-attribute 'org-level-3 nil
                        :inherit '(outline-4 variable-pitch)
                        :height 1.1)

    (set-face-attribute 'org-level-4 nil
                        :inherit '(outline-5 variable-pitch)
                        :height 1.1)

    (set-face-attribute 'org-level-5 nil
                        :inherit '(outline-6 variable-pitch)
                        :height 1.1)

    (set-face-attribute 'org-level-6 nil
                        :inherit '(outline-6 variable-pitch)
                        :height 1.1)

    (set-face-attribute 'org-agenda-date nil
                        :inherit 'variable-pitch
                        :weight 'bold
                        :height 1.3)

    (set-face-attribute 'org-agenda-structure nil
                        :inherit 'variable-pitch
                        :weight 'bold
                        :height 1.5))

  (add-hook 'bard/after-theme-load-hook #'bard/outline-heading-faces)
#+end_src

*** Provide library
#+begin_src emacs-lisp :tangle bard-elisp/bard-theme.el :mkdirp yes
  (provide 'bard-theme)
#+end_src

** bard-web
*** Load required libraries
#+begin_src emacs-lisp :tangle bard-elisp/bard-web.el :mkdirp yes
  (require 'emms)
  (require 'elfeed-search)
#+end_src

*** Elfeed/EMMS YouTube code
:PROPERTIES:
:ID:       3ca0c5a0-4829-4411-a80b-f2fe69650ea0
:END:
I made a video about this here: https://bardman.dev/technology/elfeed.

#+begin_src emacs-lisp :tangle bard-elisp/bard-web.el :mkdirp yes
  (defun bard/play-elfeed-video ()
    "Play the URL of the entry at point in mpv if it's a YouTube video."
    (interactive)
    (let ((entry (elfeed-search-selected :single)))
      (if entry
          (let ((url (elfeed-entry-link entry)))
            (if (and url (string-match-p "https?://\\(www\\.\\)?youtube\\.com\\|youtu\\.be" url))
                (progn
                  (async-shell-command (format "mpv '%s'" url))
                  (elfeed-search-untag-all-unread))
              (message "The URL is not a YouTube link: %s" url)))
        (message "No entry selected in Elfeed."))))

  (defun bard/add-video-emms-queue ()
    "Play the URL of the entry at point in mpv if it's a YouTube video. Add it to EMMS queue."
    (interactive)
    (let ((entry (elfeed-search-selected :single)))
      (if entry
          (let ((url (elfeed-entry-link entry)))
            (if (and url (string-match-p "https?://\\(www\\.\\)?youtube\\.com\\|youtu\\.be" url))
                (let* ((playlist-name "Watch Later")
                       (playlist-buffer (get-buffer (format " *%s*" playlist-name))))
                  (unless playlist-buffer
                    (setq playlist-buffer (emms-playlist-new (format " *%s*" playlist-name))))
                  (emms-playlist-set-playlist-buffer playlist-buffer)
                  (emms-add-url url)
                  (elfeed-search-untag-all-unread)
                  (message "Added YouTube video to EMMS playlist: %s" url))
              (message "The URL is not a YouTube link: %s" url)))
        (message "No entry selected in Elfeed."))))

  (defun bard/add-video-watch-later ()
    "Add the current Elfeed YouTube entry URL to '~/Videos/watch-later.m3u' and mark it as read."
    (interactive)
    (let ((entry (elfeed-search-selected :single)))
      (if entry
          (let* ((url (elfeed-entry-link entry))
                 (watch-later-file (expand-file-name "~/Videos/watch-later.m3u")))
            (if (and url (string-match-p "https?://\\(www\\.\\)?youtube\\.com\\|youtu\\.be" url))
                (progn
                  (with-temp-buffer
                    (insert (concat url "\n"))
                    (append-to-file (point-min) (point-max) watch-later-file))
                  ;; Remove the 'unread tag from the entry directly
                  (setf (elfeed-entry-tags entry)
                        (remove 'unread (elfeed-entry-tags entry)))
                  ;; Force UI update
                  (when (derived-mode-p 'elfeed-search-mode)
                    (elfeed-search-update-entry entry))
                  (message "Added video to watch later: %s" url))
              (message "The URL is not a YouTube link: %s" url)))
        (message "No entry selected in Elfeed."))))
#+end_src

*** Provide library
#+begin_src emacs-lisp :tangle bard-elisp/bard-web.el :mkdirp yes
  (provide 'bard-web)
#+end_src

** bard-window
This module has some code from [[https://protesilaos.com/dotemacs][Protesilaos's dotemacs]], especially the [[https://protesilaos.com/emacs/dotemacs#h:35b8a0a5-c447-4301-a404-bc274596238d][prot-window.el file]]. I copied most of this code a long time ago, so his library may have changed since then. But for me it works, and I never had to toy with it. No reason to reinvent the wheel with this one.

#+begin_src emacs-lisp :tangle bard-elisp/bard-window.el :mkdirp yes
  (require 'prot-common)

  (defvar prot-window-window-sizes
    '( :max-height (lambda () (floor (frame-height) 3))
       :min-height 10
       :max-width (lambda () (floor (frame-width) 4))
       :min-width 20)
    "Property list of maximum and minimum window sizes.
  The property keys are `:max-height', `:min-height', `:max-width',
  and `:min-width'.  They all accept a value of either a
  number (integer or floating point) or a function.")

  (defun prot-window--get-window-size (key)
    "Extract the value of KEY from `prot-window-window-sizes'."
    (when-let ((value (plist-get prot-window-window-sizes key)))
      (cond
       ((functionp value)
        (funcall value))
       ((numberp value)
        value)
       (t
        (error "The value of `%s' is neither a number nor a function" key)))))

  (defun prot-window-select-fit-size (window)
    "Select WINDOW and resize it.
  The resize pertains to the maximum and minimum values for height
  and width, per `prot-window-window-sizes'.

  Use this as the `body-function' in a `display-buffer-alist' entry."
    (select-window window)
    (fit-window-to-buffer
     window
     (prot-window--get-window-size :max-height)
     (prot-window--get-window-size :min-height)
     (prot-window--get-window-size :max-width)
     (prot-window--get-window-size :min-width))
    ;; If we did not use `display-buffer-below-selected', then we must
    ;; be in a lateral window, which has more space.  Then we do not
    ;; want to dedicate the window to this buffer, because we will be
    ;; running out of space.
    (when (or (window-in-direction 'above) (window-in-direction 'below))
      (set-window-dedicated-p window t)))

  (defun prot-window--get-display-buffer-below-or-pop ()
    "Return list of functions for `prot-window-display-buffer-below-or-pop'."
    (list
     #'display-buffer-reuse-mode-window
     (if (or (prot-common-window-small-p)
             (prot-common-three-or-more-windows-p))
         #'display-buffer-below-selected
       #'display-buffer-pop-up-window)))

  (defun prot-window-display-buffer-below-or-pop (&rest args)
    "Display buffer below current window or pop a new window.
  The criterion for choosing to display the buffer below the
  current one is a non-nil return value for
  `prot-common-window-small-p'.

  Apply ARGS expected by the underlying `display-buffer' functions.

  This as the action function in a `display-buffer-alist' entry."
    (let ((functions (prot-window--get-display-buffer-below-or-pop)))
      (catch 'success
        (dolist (fn functions)
          (when (apply fn args)
            (throw 'success fn))))))

  ;; from protesilaos prot-shell library
  (defun prot-window-shell-or-term-p (buffer &rest _)
    "Check if BUFFER is a shell or terminal.
  This is a predicate function for `buffer-match-p', intended for
  use in `display-buffer-alist'."
    (when (string-match-p "\\*.*\\(e?shell\\|v?term\\|terminal\\).*" (buffer-name (get-buffer buffer)))
      (with-current-buffer buffer
        ;; REVIEW 2022-07-14: Is this robust?
        (and (not (derived-mode-p 'message-mode 'text-mode))
             (derived-mode-p 'eshell-mode 'shell-mode 'term-mode 'comint-mode 'fundamental-mode)))))
#+end_src

*** Tab bar
Code taken from [[https://github.com/hylophile/.files/blob/1f3f01e4e25b00f7b61eca286fcf4f865885090c/.config/doom/config.org#fancy-tab-bar][hylophile github]]. I do not use tabs very often, but when I do I like them to look a bit nicer than the default.

This is what the tabs look like with ~doom-gruvbox~ theme:

[[file:img/tabs.png]]


#+begin_src emacs-lisp :tangle bard-elisp/bard-window.el :mkdirp yes
  (defun hy/tab-bar-format-align-center ()
    "Align the rest of tab bar items centered."
    (let* ((rest (cdr (memq 'hy/tab-bar-format-align-center tab-bar-format)))
           (rest (tab-bar-format-list rest))
           (rest (mapconcat (lambda (item) (nth 2 item)) rest  ""))
           (hpos (progn
                   (add-face-text-property 0 (length rest) 'tab-bar t rest)
                   (string-pixel-width rest)))
           (hpos (+ hpos (/ (- (frame-inner-width) hpos) 2)))
           (str (propertize "​" 'display
                            ;; The `right' spec doesn't work on TTY frames
                            ;; when windows are split horizontally (bug#59620)
                            (if (window-system)
                                `(space :align-to (- right (,hpos)))
                              `(space :align-to (,(- (frame-inner-width) hpos)))))))
      `((align-center menu-item ,str ignore))))

  (setq tab-bar-tab-name-format-function #'hy/tab-bar-tab-name-format-default)
  (defun hy/tab-bar-tab-name-format-default (tab i)
    (let* ((hint (format "%d" i))
           (name (alist-get 'name tab))
           (dir (concat "(" (alist-get 'dir tab "") ")"))
           (name-format (concat
                         " "
                         (propertize hint 'face 'tab-bar-hint)
                         " "
                         name
                         " ")))
      (add-face-text-property
       0 (length name-format)
       (funcall tab-bar-tab-face-function tab)
       'append name-format)
      name-format))


  (setq tab-bar-tab-name-function #'hy/tab-bar-tab-name-current)
  (defun hy/tab-bar-tab-name-current ()
    (hy/shorten-string
     (hy/abbreviate-tab-name
      (buffer-name (window-buffer (or (minibuffer-selected-window)
                                      (and (window-minibuffer-p)
                                           (get-mru-window))))))
     25))

  (defun hy/set-tab-dir ()
    (setf (alist-get 'dir (cdr (tab-bar--current-tab-find)))
          (hy/tab-bar-dir)))

  (defun hy/abbreviate-directory-path (path)
    "Turns `~/code/test/t` into `~/c/t/project`."
    (let* ((directories (seq-filter (lambda (s) (not (string= s ""))) (split-string path "/")))
           (last-dir (car (last directories)))
           (abbreviated-dirs (mapcar (lambda (dir)
                                       (if (string= dir last-dir)
                                           dir
                                         (substring dir 0 (if (string-prefix-p "." dir) 2 1))))
                                     directories)))
      (mapconcat 'identity abbreviated-dirs "/")))

  (defun hy/tab-bar-dir ()
    (hy/shorten-string (hy/abbreviate-directory-path
                        (abbreviate-file-name
                         (or (projectile-project-root) default-directory)))
                       10
                       t))

  (defun hy/shorten-string (string max-length &optional at-start)
    (let ((len (length string)))
      (if (> len max-length)
          (if at-start
              (concat  "…" (substring string (- len max-length) len))
            (concat (substring string 0 max-length) "…"))
        string)))

  (defun hy/abbreviate-tab-name (name)
    (string-trim (replace-regexp-in-string
                  (rx (or "*" "helpful" "Org Src"))
                  "" name)))
#+end_src

*** Toggle window split direction
#+begin_src emacs-lisp :tangle bard-elisp/bard-window.el :mkdirp yes
  (defun bard/toggle-window-split ()
    "Toggle between horizontal and vertical window splits, preserving buffer layout."
    (interactive)
    (let ((current-buffers (mapcar #'window-buffer (window-list))) ; List of buffers in current windows
          (split-direction (if (= (window-width) (frame-width))
                               'vertical
                             'horizontal)))
      (delete-other-windows)
      ;; Toggle the split direction
      (if (eq split-direction 'horizontal)
          (split-window-vertically)
        (split-window-horizontally))
      ;; Restore buffers to the new windows
      (let ((windows (window-list)))
        (cl-loop for buffer in current-buffers
                 for window in windows
                 do (set-window-buffer window buffer)))))

  (provide 'bard-window)
#+end_src

** bard-writing
*** Load required libraries
#+begin_src emacs-lisp :tangle bard-elisp/bard-writing.el :mkdirp yes
  (require 'consult)
  (require 'beframe)
  (require 'calendar)
  (require 'org-roam-node)
  (require 'denote)
#+end_src

*** Note buffers
#+begin_src emacs-lisp :tangle bard-elisp/bard-writing.el :mkdirp yes
  (defvar bard/consult--source-notes
    `(:name     "Note Buffers"
                :narrow   ?n
                :category buffer
                :face     consult-buffer
                :history  buffer-name-history
                :items    ,(lambda ()
                             (mapcar #'buffer-name
                                     (seq-filter
                                      (lambda (buf)
                                        (string-prefix-p "[Note]" (buffer-name buf)))
                                      (beframe-buffer-list))))
                :action   ,#'switch-to-buffer
                :state    ,#'consult--buffer-state)
    "Consult source for note buffers (limited to beframe buffers).")

  (defun bard/consult-buffer-notes ()
    "Show `consult-buffer` limited to buffers starting with [Note]."
    (interactive)
    (consult-buffer '(bard/consult--source-notes)))

  (defun bard/ibuffer-notes ()
    "Open `ibuffer` limited to buffers starting with [Note]."
    (interactive)
    (ibuffer nil "*Ibuffer-Notes*"
             '((name . "^\\[Note\\]"))))
#+end_src

*** Searching notes
#+begin_src emacs-lisp :tangle bard-elisp/bard-writing.el :mkdirp yes
  (defun bard/find-notes-file ()
    (interactive)
    (consult-find "~/Notes/denote"))

  (defun bard/search-notes-directory ()
    (interactive)
    (consult-grep "~/Notes/denote"))
#+end_src

*** University Class jumping in ~uni.org~ file
#+begin_src emacs-lisp :tangle bard-elisp/bard-writing.el :mkdirp yes
  (defvar bard/class-dirs
    '(("CSE 130 " . "~/Documents/Uni/SPRING2026-CSE 130/")
      ("CSE 310 " . "~/Documents/Uni/SPRING2026-CSE 310/")
      ("ENGR 111" . "~/Documents/Uni/SPRING2026-ENGR 111/")
      ("MATH 206" . "~/Documents/Uni/SPRING2026-MATH 206/")
      ("PHYS 298" . "~/Documents/Uni/SPRING2026-PHYS 298/"))
    "Mapping of class names to their document directories.")

  (defvar bard/uni-notes-file "~/Notes/denote/uni.org"
    "Path to the main university org file.")

  (defun bard/jump-to-class (class)
    "Jump to CLASS heading in `bard/uni-notes-file` and open its dir in dired."
    (interactive
     (list (completing-read "Class: " (mapcar #'car bard/class-dirs))))
    (let* ((dir (cdr (assoc class bard/class-dirs))))
      ;; split windows
      (delete-other-windows)
      (let ((notes-window (selected-window))
            (dired-window (split-window-right)))
        ;; open notes file and jump to heading
        (with-selected-window notes-window
          (find-file bard/uni-notes-file)
          (widen)
          (goto-char (point-min))
          (message class)
          (search-forward class nil nil))
        ;; open dired in right window
        (with-selected-window dired-window
          (dired dir)))))

  (defun bard/jump-to-class-new-frame (class)
    "Open CLASS notes and dir in a new frame titled after CLASS, even with beframe."
    (interactive
     (list (completing-read "Class: " (mapcar #'car bard/class-dirs))))
    (let* ((dir (cdr (assoc class bard/class-dirs)))
           (frame (make-frame `((frame-title-format . ,class)))))
      (select-frame-set-input-focus frame)
      (delete-other-windows)
      (let ((notes-window (selected-window))
            (dired-window (split-window-right)))
        (with-selected-window notes-window
          (find-file bard/uni-notes-file)
          (widen)
          (goto-char (point-min))
          (search-forward class nil nil))
        (with-selected-window dired-window
          (dired dir)
          (beframe-rename-current-frame)))))

  ;; Optional: bind to a key
  (global-set-key (kbd "C-c u") #'bard/jump-to-class)
  (global-set-key (kbd "C-c U") #'bard/jump-to-class-new-frame)
#+end_src

*** Denote journal
I want to have a custom format for my daily journal file to start what I want to get done that day specifically. It helps me get focused on what needs to be accomplished on days when I have a lot of tasks.
#+begin_src emacs-lisp :tangle bard-elisp/bard-writing.el :mkdirp yes
  (defun bard/denote-todo-template ()
    "Return string for daily tasks heading in `denote-journal' entries."
    (with-temp-buffer
      (org-mode)
      (insert (format "* Tasks for %s\n** Время я потратил бездельничая\n\n* Notes for today\n\n* Clocktable\n"
                      (format-time-string "%Y-%m-%d (%a)")))
      (let ((org-clock-clocktable-default-properties
             '(:scope file :maxlevel 3 :link nil :compact t)))
        (org-clock-report))
      (buffer-string)))
#+end_src

*** Math input mode
#+begin_src emacs-lisp :tangle bard-elisp/bard-writing.el :mkdirp yes
  ;; Taken from: https://stackoverflow.com/a/75314192
  (defun add-multiple-into-list (lst items)
    "Add each item from ITEMS into LST."
    (dolist (item items)
      (add-to-list lst item)))

  (defun bard/cdlatex-add-math-symbols ()
    "Add functions into list."
    (add-multiple-into-list
     'cdlatex-math-symbol-alist-comb
     '((?V "\\vec"))))

  (define-minor-mode bard/org-math-mode
    "Enable features to write math in `org-mode'."
    :init-value nil
    :lighter " S="
    :global nil
    (org-fragtog-mode t)
    (org-cdlatex-mode t)
    (electric-pair-local-mode t)
    (bard/cdlatex-add-math-symbols)
    ;; sending math to calc from latex doc
    (define-key org-mode-map (kbd "C-S-e") #'latex-math-from-calc))

  ;; latex into calc
  (defun latex-math-from-calc ()
    "Evaluate `calc' on the contents of line at point."
    (interactive)
    (cond ((region-active-p)
           (let* ((beg (region-beginning))
                  (end (region-end))
                  (string (buffer-substring-no-properties beg end)))
             (kill-region beg end)
             (insert (calc-eval `(,string calc-language latex
                                          calc-prefer-frac t
                                          calc-angle-mode rad)))))
          (t (let ((l (thing-at-point 'line)))
               (end-of-line 1) (kill-line 0)
               (insert (calc-eval `(,l
                                    calc-language latex
                                    calc-prefer-frac t
                                    calc-angle-mode rad)))))))
#+end_src

*** Provide library
#+begin_src emacs-lisp :tangle bard-elisp/bard-writing.el :mkdirp yes
  (provide 'bard-writing)
#+end_src

** prot-common
This library is mostly used in the ~bard-modeline.el~ library. It is mostly unmodified from the time when I copied it from [[https://protesilaos.com/dotemacs][Protesilaos's Emacs configuration]] over 2 years ago.

#+begin_src emacs-lisp :tangle bard-elisp/prot-common.el :mkdirp yes
;;; prot-common.el --- Common functions for my dotemacs -*- lexical-binding: t -*-

;; Copyright (C) 2020-2023  Protesilaos Stavrou

;; Author: Protesilaos Stavrou <[email protected]>
;; URL: https://protesilaos.com/emacs/dotemacs
;; Version: 0.1.0
;; Package-Requires: ((emacs "30.1"))

;; This file is NOT part of GNU Emacs.

;; 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:
;;
;; Common functions for my Emacs: <https://protesilaos.com/emacs/dotemacs/>.
;;
;; Remember that every piece of Elisp that I write is for my own
;; educational and recreational purposes.  I am not a programmer and I
;; do not recommend that you copy any of this if you are not certain of
;; what it does.

;;; Code:

(eval-when-compile
  (require 'subr-x)
  (require 'cl-lib))

(defgroup prot-common ()
  "Auxiliary functions for my dotemacs."
  :group 'editing)

;;;###autoload
(defun prot-common-number-even-p (n)
  "Test if N is an even number."
  (if (numberp n)
      (= (% n 2) 0)
    (error "%s is not a number" n)))

;;;###autoload
(defun prot-common-number-integer-p (n)
  "Test if N is an integer."
  (if (integerp n)
      n
    (error "%s is not an integer" n)))

;;;###autoload
(defun prot-common-number-integer-positive-p (n)
  "Test if N is a positive integer."
  (if (prot-common-number-integer-p n)
      (> n 0)
    (error "%s is not a positive integer" n)))

;; Thanks to Gabriel for providing a cleaner version of
;; `prot-common-number-negative': <https://github.com/gabriel376>.
;;;###autoload
(defun prot-common-number-negative (n)
  "Make N negative."
  (if (and (numberp n) (> n 0))
      (* -1 n)
    (error "%s is not a valid positive number" n)))

;;;###autoload
(defun prot-common-reverse-percentage (number percent change-p)
  "Determine the original value of NUMBER given PERCENT.

CHANGE-P should specify the increase or decrease.  For simplicity,
nil means decrease while non-nil stands for an increase.

NUMBER must satisfy `numberp', while PERCENT must be `natnump'."
  (unless (numberp number)
    (user-error "NUMBER must satisfy numberp"))
  (unless (natnump percent)
    (user-error "PERCENT must satisfy natnump"))
  (let* ((pc (/ (float percent) 100))
         (pc-change (if change-p (+ 1 pc) pc))
         (n (if change-p pc-change (float (- 1 pc-change)))))
    ;; FIXME 2021-12-21: If float, round to 4 decimal points.
    (/ number n)))

;;;###autoload
(defun prot-common-percentage-change (n-original n-final)
  "Find percentage change between N-ORIGINAL and N-FINAL numbers.

When the percentage is not an integer, it is rounded to 4
floating points: 16.666666666666664 => 16.667."
  (unless (numberp n-original)
    (user-error "N-ORIGINAL must satisfy numberp"))
  (unless (numberp n-final)
    (user-error "N-FINAL must satisfy numberp"))
  (let* ((difference (float (abs (- n-original n-final))))
         (n (* (/ difference n-original) 100))
         (round (floor n)))
    ;; FIXME 2021-12-21: Any way to avoid the `string-to-number'?
    (if (> n round) (string-to-number (format "%0.4f" n)) round)))

;; REVIEW 2023-04-07 07:43 +0300: I just wrote the conversions from
;; seconds.  Hopefully they are correct, but I need to double check.
(defun prot-common-seconds-to-minutes (seconds)
  "Convert a number representing SECONDS to MM:SS notation."
  (let ((minutes (/ seconds 60))
        (seconds (% seconds 60)))
    (format "%.2d:%.2d" minutes seconds)))

(defun prot-common-seconds-to-hours (seconds)
  "Convert a number representing SECONDS to HH:MM:SS notation."
  (let* ((hours (/ seconds 3600))
         (minutes (/ (% seconds 3600) 60))
         (seconds (% seconds 60)))
    (format "%.2d:%.2d:%.2d" hours minutes seconds)))

;;;###autoload
(defun prot-common-seconds-to-minutes-or-hours (seconds)
  "Convert SECONDS to either minutes or hours, depending on the value."
  (if (> seconds 3599)
      (prot-common-seconds-to-hours seconds)
    (prot-common-seconds-to-minutes seconds)))

;;;###autoload
(defun prot-common-rotate-list-of-symbol (symbol)
  "Rotate list value of SYMBOL by moving its car to the end.
Return the first element before performing the rotation.

This means that if `sample-list' has an initial value of `(one
two three)', this function will first return `one' and update the
value of `sample-list' to `(two three one)'.  Subsequent calls
will continue rotating accordingly."
  (unless (symbolp symbol)
    (user-error "%s is not a symbol" symbol))
  (when-let* ((value (symbol-value symbol))
              (list (and (listp value) value))
              (first (car list)))
    (set symbol (append (cdr list) (list first)))
    first))

;;;###autoload
(defun prot-common-empty-buffer-p ()
  "Test whether the buffer is empty."
  (or (= (point-min) (point-max))
      (save-excursion
        (goto-char (point-min))
        (while (and (looking-at "^\\([a-zA-Z]+: ?\\)?$")
                    (zerop (forward-line 1))))
        (eobp))))

;;;###autoload
(defun prot-common-minor-modes-active ()
  "Return list of active minor modes for the current buffer."
  (let ((active-modes))
    (mapc (lambda (m)
            (when (and (boundp m) (symbol-value m))
              (push m active-modes)))
          minor-mode-list)
    active-modes))

;;;###autoload
(defun prot-common-truncate-lines-silently ()
  "Toggle line truncation without printing messages."
  (let ((inhibit-message t))
    (toggle-truncate-lines t)))

;; NOTE 2023-08-12: I tried the `clear-message-function', but it did
;; not work.  What I need is very simple and this gets the job done.
;;;###autoload
(defun prot-common-clear-minibuffer-message (&rest _)
  "Print an empty message to clear the echo area.
Use this as advice :after a noisy function."
  (message ""))

;;;###autoload
(defun prot-common-disable-hl-line ()
  "Disable Hl-Line-Mode (for hooks)."
  (hl-line-mode -1))

;;;###autoload
(defun prot-common-window-bounds ()
  "Return start and end points in the window as a cons cell."
  (cons (window-start) (window-end)))

;;;###autoload
(defun prot-common-page-p ()
  "Return non-nil if there is a `page-delimiter' in the buffer."
  (or (save-excursion (re-search-forward page-delimiter nil t))
      (save-excursion (re-search-backward page-delimiter nil t))))

;;;###autoload
(defun prot-common-window-small-p ()
  "Return non-nil if window is small.
Check if the `window-width' or `window-height' is less than
`split-width-threshold' and `split-height-threshold',
respectively."
  (or (and (numberp split-width-threshold)
           (< (window-total-width) split-width-threshold))
      (and (numberp split-height-threshold)
           (> (window-total-height) split-height-threshold))))

;;;###autoload
(defun prot-common-three-or-more-windows-p (&optional frame)
  "Return non-nil if three or more windows occupy FRAME.
If FRAME is non-nil, inspect the current frame."
  (>= (length (window-list frame :no-minibuffer)) 3))

;;;###autoload
(defun prot-common-read-data (file)
  "Read Elisp data from FILE."
  (with-temp-buffer
    (insert-file-contents file)
    (read (current-buffer))))

;;;###autoload
(defun prot-common-completion-category ()
  "Return completion category."
  (when-let ((window (active-minibuffer-window)))
    (with-current-buffer (window-buffer window)
      (completion-metadata-get
       (completion-metadata (buffer-substring-no-properties
                             (minibuffer-prompt-end)
                             (max (minibuffer-prompt-end) (point)))
                            minibuffer-completion-table
                            minibuffer-completion-predicate)
       'category))))

;; Thanks to Omar Antolín Camarena for providing this snippet!
;;;###autoload
(defun prot-common-completion-table (category candidates)
  "Pass appropriate metadata CATEGORY to completion CANDIDATES.

This is intended for bespoke functions that need to pass
completion metadata that can then be parsed by other
tools (e.g. `embark')."
  (lambda (string pred action)
    (if (eq action 'metadata)
        `(metadata (category . ,category))
      (complete-with-action action candidates string pred))))

;;;###autoload
(defun prot-common-completion-table-no-sort (category candidates)
  "Pass appropriate metadata CATEGORY to completion CANDIDATES.
Like `prot-common-completion-table' but also disable sorting."
  (lambda (string pred action)
    (if (eq action 'metadata)
        `(metadata (category . ,category)
                   (display-sort-function . ,#'identity))
      (complete-with-action action candidates string pred))))

;; Thanks to Igor Lima for the `prot-common-crm-exclude-selected-p':
;; <https://github.com/0x462e41>.
;; This is used as a filter predicate in the relevant prompts.
(defvar crm-separator)

;;;###autoload
(defun prot-common-crm-exclude-selected-p (input)
  "Filter out INPUT from `completing-read-multiple'.
Hide non-destructively the selected entries from the completion
table, thus avoiding the risk of inputting the same match twice.

To be used as the PREDICATE of `completing-read-multiple'."
  (if-let* ((pos (string-match-p crm-separator input))
            (rev-input (reverse input))
            (element (reverse
                      (substring rev-input 0
                                 (string-match-p crm-separator rev-input))))
            (flag t))
      (progn
        (while pos
          (if (string= (substring input 0 pos) element)
              (setq pos nil)
            (setq input (substring input (1+ pos))
                  pos (string-match-p crm-separator input)
                  flag (when pos t))))
        (not flag))
    t))

;; The `prot-common-line-regexp-p' and `prot-common--line-regexp-alist'
;; are contributed by Gabriel: <https://github.com/gabriel376>.  They
;; provide a more elegant approach to using a macro, as shown further
;; below.
(defvar prot-common--line-regexp-alist
  '((empty . "[\s\t]*$")
    (indent . "^[\s\t]+")
    (non-empty . "^.+$")
    (list . "^\\([\s\t#*+]+\\|[0-9]+[^\s]?[).]+\\)")
    (heading . "^[=-]+"))
  "Alist of regexp types used by `prot-common-line-regexp-p'.")

(defun prot-common-line-regexp-p (type &optional n)
  "Test for TYPE on line.
TYPE is the car of a cons cell in
`prot-common--line-regexp-alist'.  It matches a regular
expression.

With optional N, search in the Nth line from point."
  (save-excursion
    (goto-char (line-beginning-position))
    (and (not (bobp))
         (or (beginning-of-line n) t)
         (save-match-data
           (looking-at
            (alist-get type prot-common--line-regexp-alist))))))

;; The `prot-common-shell-command-with-exit-code-and-output' function is
;; courtesy of Harold Carr, who also sent a patch that improved
;; `prot-eww-download-html' (from the `prot-eww.el' library).
;;
;; More about Harold: <http://haroldcarr.com/about/>.
(defun prot-common-shell-command-with-exit-code-and-output (command &rest args)
  "Run COMMAND with ARGS.
Return the exit code and output in a list."
  (with-temp-buffer
    (list (apply 'call-process command nil (current-buffer) nil args)
          (buffer-string))))

(defvar prot-common-url-regexp
  (concat
   "~?\\<\\([-a-zA-Z0-9+&@#/%?=~_|!:,.;]*\\)"
   "[.@]"
   "\\([-a-zA-Z0-9+&@#/%?=~_|!:,.;]+\\)\\>/?")
  "Regular expression to match (most?) URLs or email addresses.")

(autoload 'auth-source-search "auth-source")

;;;###autoload
(defun prot-common-auth-get-field (host prop)
  "Find PROP in `auth-sources' for HOST entry."
  (when-let ((source (auth-source-search :host host)))
    (if (eq prop :secret)
        (funcall (plist-get (car source) prop))
      (plist-get (flatten-list source) prop))))

;;;###autoload
(defun prot-common-parse-file-as-list (file)
  "Return the contents of FILE as a list of strings.
Strings are split at newline characters and are then trimmed for
negative space.

Use this function to provide a list of candidates for
completion (per `completing-read')."
  (split-string
   (with-temp-buffer
     (insert-file-contents file)
     (buffer-substring-no-properties (point-min) (point-max)))
   "\n" :omit-nulls "[\s\f\t\n\r\v]+"))

(defun prot-common-ignore (&rest _)
  "Use this as override advice to make a function do nothing."
  nil)

;; NOTE 2023-06-02: The `prot-common-wcag-formula' and
;; `prot-common-contrast' are taken verbatim from my `modus-themes'
;; and renamed to have the prefix `prot-common-' instead of
;; `modus-themes-'.  This is all my code, of course, but I do it this
;; way to ensure that this file is self-contained in case someone
;; copies it.

;; This is the WCAG formula: <https://www.w3.org/TR/WCAG20-TECHS/G18.html>.
(defun prot-common-wcag-formula (hex)
  "Get WCAG value of color value HEX.
The value is defined in hexadecimal RGB notation, such #123456."
  (cl-loop for k in '(0.2126 0.7152 0.0722)
           for x in (color-name-to-rgb hex)
           sum (* k (if (<= x 0.03928)
                        (/ x 12.92)
                      (expt (/ (+ x 0.055) 1.055) 2.4)))))

;;;###autoload
(defun prot-common-contrast (c1 c2)
  "Measure WCAG contrast ratio between C1 and C2.
C1 and C2 are color values written in hexadecimal RGB."
  (let ((ct (/ (+ (prot-common-wcag-formula c1) 0.05)
               (+ (prot-common-wcag-formula c2) 0.05))))
    (max ct (/ ct))))

;;;; EXPERIMENTAL macros (not meant to be used anywhere)

;; TODO 2023-09-30: Try the same with `cl-defmacro' and &key
(defmacro prot-common-if (condition &rest consequences)
  "Separate the CONSEQUENCES of CONDITION semantically.
Like `if', `when', `unless' but done by using `:then' and `:else'
keywords.  The forms under each keyword of `:then' and `:else'
belong to the given subset of CONSEQUENCES.

- The absence of `:else' means: (if CONDITION (progn CONSEQUENCES)).
- The absence of `:then' means: (if CONDITION nil CONSEQUENCES).
- Otherwise: (if CONDITION (progn then-CONSEQUENCES) else-CONSEQUENCES)."
  (declare (indent 1))
  (let (then-consequences else-consequences last-kw)
    (dolist (elt consequences)
      (let ((is-keyword (keywordp elt)))
        (cond
         ((and (not is-keyword) (eq last-kw :then))
          (push elt then-consequences))
         ((and (not is-keyword) (eq last-kw :else))
          (push elt else-consequences))
         ((and is-keyword (eq elt :then))
          (setq last-kw :then))
         ((and is-keyword (eq elt :else))
          (setq last-kw :else)))))
    `(if ,condition
         ,(if then-consequences
              `(progn ,@(nreverse then-consequences))
            nil)
       ,@(nreverse else-consequences))))

(provide 'prot-common)
;;; prot-common.el ends here

#+end_src