summaryrefslogtreecommitdiffhomepage
path: root/init.el
blob: 7995bcecbe2473e08ba5a241c5704500c301a6b2 (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
;; -*- lexical-binding: t; -*-

;;; Initialisation
;;;; Bootstrap and use package
(defvar jao-emacs-dir (expand-file-name "~/etc/emacs"))

(setq package-user-dir
      (expand-file-name (format "~/.emacs.d/elpa.%s" emacs-major-version))
      package-check-signature 'allow-unsigned)

(require 'package)
(setq package-archives
      '(("gnu-devel" . "https://elpa.gnu.org/devel/")
        ("nongnu-devel" . "https://elpa.nongnu.org/nongnu-devel/")
        ("melpa" . "https://melpa.org/packages/"))
      package-archive-priorities '(("gnu-devel" . 2)
                                   ("nongnu-devel" . 1)
                                   ("melpa" . 0)))

(package-initialize)

(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(require 'use-package)

(use-package gnu-elpa-keyring-update :ensure t)

;;;; .elc vs .el loading

(setq load-prefer-newer t)
(setq comp-async-report-warnings-errors nil
      warning-suppress-types '((comp)))

;;; Paths
(defvar jao-local-lisp-dir "~/lib/elisp"
  "Directory for external elisp libraries and repos")

(defvar jao-data-dir (expand-file-name "data" jao-emacs-dir)
  "Directory containing static data, such as images.")

(defun jao-data-file (file) (expand-file-name file jao-data-dir))

(setq jao-org-dir (expand-file-name "~/doc/org"))

(defvar jao-sink-dir
  (file-name-as-directory (expand-file-name "~/doc/sink"))
  "Directory used for downloads and such.")

(defvar jao-site-dir (expand-file-name "site" jao-emacs-dir))
(defun jao-site-el (basename &optional gpg)
  (expand-file-name (concat basename ".el" (when gpg ".gpg")) jao-site-dir))

(defun jao-load-site-el (basename &optional gpg)
  (let ((lf (jao-site-el basename gpg)))
    (if (file-exists-p lf)
        (load lf)
      (message "Attempted to load non existing %s" lf))))

(defun jao-exec-path (dir)
  (let ((fn (expand-file-name dir)))
    (add-to-list 'exec-path fn nil)
    (setenv "PATH" (concat fn ":" (getenv "PATH")))))

(defun jao-load-path (subdir)
  "Add to load path a subdir of `jao-local-lisp-dir'"
  (let ((path (expand-file-name subdir jao-local-lisp-dir)))
    (when (file-directory-p path) (add-to-list 'load-path path))))

;;;; load and info path initialisation
(add-to-list 'load-path jao-site-dir)
(add-to-list 'load-path jao-local-lisp-dir)
(add-to-list 'load-path (expand-file-name "custom" jao-emacs-dir))
(add-to-list 'load-path "/usr/local/share/emacs/site-lisp/")

(let ((libd (expand-file-name "lib" jao-emacs-dir)))
  (add-to-list 'load-path libd)
  (dolist (f (directory-files libd t "^[^.]+$"))
    (when (file-directory-p f) (add-to-list 'load-path f))))

(defvar jao-info-dir (expand-file-name "~/doc/info"))
(require 'info)
(add-to-list 'Info-directory-list jao-info-dir)

;;;; custom location of custom.el and co.
(setq custom-file (jao-site-el "custom"))
(load custom-file)
(setq custom-unlispify-tag-names nil)
(setq custom-buffer-done-kill t)
(setq custom-raised-buttons nil)

;;; Preamble
;;;; preamble (pre.el)
(jao-load-site-el "pre")

;;; System Utilities
;;;; persist
(require 'persist)

;;;; (no) backups
(setq vc-make-backup-files nil
      make-backup-files nil)

;;;; history (saveplace, recentf, savehist)
(require 'saveplace)
(setq save-place-file (expand-file-name "~/.emacs.d/cache/places"))
(save-place-mode 1)

(setq recentf-save-file (expand-file-name "~/.emacs.d/cache/recentf")
      recentf-max-saved-items 2000
      recentf-exclude '("/home/jao/\\.emacs\\.d/elpa.*/.*"
                        ".*/.git/COMMIT_EDITMSG"))

(require 'recentf)
(recentf-mode 1)


(setq savehist-file (expand-file-name "~/.emacs.d/cache/history"))
(require 'savehist)

(savehist-mode t)

(defun jao-unpropertize-kill-ring ()
  (setq kill-ring (mapcar #'substring-no-properties kill-ring)))

(add-hook 'kill-emacs-hook #'jao-unpropertize-kill-ring)

(setq savehist-additional-variables '(kill-ring search-ring regexp-search-ring)
      savehist-ignored-variables '(ido-file-history))

;;;; yes/no, bell, startup message

(setq use-short-answers t)
(setq inhibit-startup-message t)
(setq visible-bell t)

;;;; server
(setenv "EDITOR" "emacsclient")
(require 'server)
(unless (or (daemonp) (server-running-p)) (server-start))

;;;; timers
(put 'list-timers 'disabled nil)

;;;; tramp
(inhibit-remote-files)

;;;; sleep/awake
(use-package jao-sleep)
(jao-sleep-dbus-register)

;;;; process runners
(use-package jao-shell
  :demand t
  :config (jao-shell-def-exec jao-trayer "trayer.sh")
  :bind (("s-r" . jao-shell-exec)))

;;;; app launcher
(jao-load-path "app-launcher")
(use-package app-launcher
  :bind (("s-R" . app-launcher-run-app)))

;;;; brightness control
(jao-shell-def-exec jao-bright-set-up "brightnessctl" "-q" "s" "5%+")
(jao-shell-def-exec jao-bright-set-down "brightnessctl" "-q" "s" "5%-")

(defun jao-brightness ()
  (string-trim (or (cadr (jao-shell-cmd-lines "brightnessctl")) "(Unknown)")))

(defun jao-bright-show ()
  (interactive)
  (message "%s" (jao-brightness)))

(defun jao-bright-up ()
  (interactive)
  (jao-shell-exec "brightnessctl -q s 5%%+" t)
  (jao-bright-show))

(defun jao-bright-down ()
  (interactive)
  (jao-shell-exec "brightnessctl -q s 5%%-" t)
  (jao-bright-show))

;;;; keyboard
(when (> emacs-major-version 27)
  (use-package repeat
    :config (setq repeat-echo-function 'repeat-echo-mode-line
                  repeat-exit-key "SHIFT"
                  repeat-exit-timeout 2))
  (repeat-mode))

(defun jao-kb-toggle (&optional lyt)
  (interactive)
  (shell-command-to-string (or lyt
                               (if (jao-kb-toggled-p)
                                   "setxkbmap us"
                                 "setxkbmap us -variant intl"))))

(defun jao-kb-toggled-p ()
  (not (string-empty-p
        (shell-command-to-string "setxkbmap -query|grep variant"))))

(customize-set-variable 'default-input-method "catalan-prefix")
;; http://mbork.pl/2022-03-07_Transient_input_method
(customize-set-variable 'default-transient-input-method "TeX")

(setq echo-keystrokes 1
      suggest-key-bindings nil)

;;;; transient
(use-package transient
  :init (setq transient-show-popup t) ;; 2.0
  :demand t
  :config
  (transient-bind-q-to-quit))

(defmacro jao-transient-major-mode (mode &rest suffix)
  (declare (indent defun))
  (let ((mode (intern (format "%s-mode" mode)))
        (mmap (intern (format "%s-mode-map" mode)))
        (name (intern (format "jao-transient-%s" mode))))
    `(progn
       (transient-define-prefix ,name ()
         ,(format "Transient ops for %s" mode)
         [,(format "Operations for %s" mode) :if-derived ',mode ,@suffix])
       (define-key ,mmap (kbd "s-SPC") #',name)
       (define-key ,mmap (kbd "C-c SPC") #',name))))

(defmacro jao-transient-major-mode+1 (mode suffix)
  (declare (indent defun))
  (let ((name (intern (format "jao-transient-%s" mode))))
    (if (fboundp name)
        `(transient-append-suffix ',name '(0 -1) ,suffix)
      `(jao-transient-major-mode ,mode ,suffix))))

(defmacro jao-transient-major-mode+ (mode &rest suffixes)
  (declare (indent defun))
  `(progn ,@(mapcar (lambda (s) `(jao-transient-major-mode+1 ,mode ,s))
                    suffixes)))

;;;; disk monitoring
(use-package jao-dirmon
  :commands jao-dirmon-report)
;;;; mailcap
(use-package mailcap
  :config
  (add-to-list 'mailcap-mime-extensions '(".JPEG" . "image/jpeg"))
  (add-to-list 'mailcap-mime-extensions '(".JPG" . "image/jpeg"))

  (defun jao-icalendar-import-buffer (&optional no-kill)
    (let ((icalendar-import-format "%s%u%l%d"))
      (icalendar-import-buffer diary-file t nil))
    (unless no-kill (kill-buffer))
    (message "Event imported into diary"))

  (defun jao-icalendar-import-invite (file)
    (with-temp-buffer
      (insert-file-contents file)
      (jao-icalendar-import-buffer t)))

  :custom
  ((mailcap-user-mime-data
    `((jao-icalendar-import-buffer "application/ics")
      ("emacsclient -e '(jao-icalendar-import-invite \"%s\")'" "application/ics")
      (doc-view-mode "application/.*pdf" (display-graphic-p))
      ("zathura \"%s\"" "application/.*pdf")
      (image-mode "image/.*" (display-graphic-p))
      ("firefox %s && riverctl set-focused-tags 2" "text/html" jao-river-enabled)
      ("swayimg \"%s\"" "image/.*" jao-sway-enabled)
      ("imv-wayland \"%s\"" "image/.*" jao-wayland-enabled)
      ("imv-x11 \"%s\"" "image/.*")))))

;;; Crypto
;;;; PGP, EPG, passwords
(setq auth-source-debug nil)

(require 'auth-source)
(add-to-list 'auth-source-protocols '(local "local"))
(setq auth-sources '("~/.emacs.d/authinfo.gpg" "~/.netrc"))

(use-package epa-file
  :init (setq epa-file-cache-passphrase-for-symmetric-encryption t)
  :config (epa-file-enable))
(require 'epa-file)

(defun jao--get-user/password (h)
  (let ((item (car (auth-source-search :type 'netrc :host h :max 1))))
    (when item
      (let ((user (plist-get item :user))
            (pwd (plist-get item :secret)))
        (list user (when pwd (funcall pwd)))))))

(defun jao-call-with-auth (host fun)
  (let ((up (jao--get-user/password host)))
    (funcall fun (car up) (cadr up))))

(defmacro jao-with-auth (host usr pwd &rest body)
  (declare (indent defun))
  `(jao-call-with-auth ,host (lambda (,usr ,pwd) ,@body)))

;;;; pass
(use-package password-store-menu
  :ensure t
  :config (password-store-menu-enable)
  :custom (password-store-menu-key "C-c p"))

;;; Fonts and color themes
;;;; widgets
(setq widget-image-enable nil
      widget-link-prefix ""
      widget-link-suffix ""
      widget-button-prefix " "
      widget-button-suffix " "
      widget-push-button-prefix ""
      widget-push-button-suffix "")

;;;; nobreak char display
(setq nobreak-char-display nil)

;;;; vertical separator
(unless (display-graphic-p)
  (set-display-table-slot standard-display-table
                          'vertical-border
                          (make-glyph-code ?│)))

;;;; transparency
(defvar jao-transparent-only-bg (> emacs-major-version 28))

(defvar jao-frames-default-alpha
  (cond ((eq window-system 'pgtk) 85)
        (jao-transparent-only-bg 88)
        (t 85)))

(defvar jao-transparent-frame (< jao-frames-default-alpha 100))

(defun jao-alpha-parameters (&optional level)
  (let ((level (or level jao-frames-default-alpha)))
    (if jao-transparent-only-bg
        `((alpha-background . ,level) (alpha))
      `((alpha . ,(cons level level)) (alpha-background)))))

(defun jao-set-transparency (&optional level all)
  (interactive "nOpacity (0-100): ")
  (let ((level (or level jao-frames-default-alpha)))
    (setq jao-transparent-frame (< level 100))
    (if all
        (modify-all-frames-parameters (jao-alpha-parameters level))
      (modify-frame-parameters nil (jao-alpha-parameters level)))))

(defun jao-toggle-transparency (&optional all)
  (interactive "P")
  (let ((level (if jao-transparent-frame 100 jao-frames-default-alpha)))
    (jao-set-transparency level all)))

(jao-set-transparency)

;;;; themes
(defun jao-colors-scheme-dark-p ()
  (equal "dark" (getenv "JAO_COLOR_SCHEME")))

(defun jao-colors-scheme ()
  (if (jao-colors-scheme-dark-p) 'dark 'light))

(customize-set-variable 'frame-background-mode (jao-colors-scheme))

(setq custom-theme-directory
      (expand-file-name "lib/themes" jao-emacs-dir))

(require 'jao-themes)

(defvar jao-theme-dark 'jao-dark)
(defvar jao-theme-light 'jao-light)
(defvar jao-theme-term-dark 'modus-vivendi)
(defvar jao-theme-term-light 'jao-light-term)

(defun jao-themes-setup ()
  (let* ((dark (jao-colors-scheme-dark-p))
         (theme (cond ((and dark window-system) jao-theme-dark)
                      (dark jao-theme-term-dark)
                      (window-system jao-theme-light)
                      (t jao-theme-term-light))))
    (load-theme theme t)
    (modify-all-frames-parameters `((font . ,jao-themes-default-face)))))

(jao-themes-setup)

;;; Help system
;;;; help buffers
(setq help-window-select t
      help-window-keep-selected nil
      help-link-key-to-documentation t)

;;;; find-func/var/lib
(use-package find-func
  :bind (("C-h C-v" . find-variable)
         ("C-h C-f" . find-function)
         ("C-h C-k" . find-function-on-key)
         ("C-h C-l" . find-library)))

;;;; eldoc
(use-package eldoc
  :init (setq eldoc-minor-mode-string nil
              eldoc-idle-delay 0.1
              eldoc-echo-area-display-truncation-message nil
              eldoc-echo-area-use-multiline-p 5
              eldoc-echo-area-prefer-doc-buffer 'maybe
              eldoc-display-functions '(eldoc-display-in-echo-area))
  :config (global-eldoc-mode 1))

(defun jao-eldoc-toggle ()
  "Toggle eldoc's documentation buffer."
  (interactive)
  (let ((buffer (eldoc-doc-buffer)))
    (if-let (w (and buffer (get-buffer-window buffer)))
        (delete-window w)
      (eldoc-doc-buffer t))))

;;;; bookmarks
(setq bookmark-default-file "~/.emacs.d/emacs.bmk"
      bookmark-fringe-mark nil)

;;;; man pages
(use-package man
  :config (setq Man-notify-method 'pushy)) ;; pushy - same window

;;; Minibuffer
(use-package jao-minibuffer
  :init
  (if (jao-colors-scheme-dark-p)
      (setq jao-minibuffer-active-buffer-line-color "azure4"
            jao-minibuffer-inactive-buffer-line-color "grey25")
    (setq jao-minibuffer-active-buffer-line-color "burlywood3"
          jao-minibuffer-inactive-buffer-line-color "grey65"))
  (setq jao-minibuffer-adaptive-alignment nil)
  :commands (jao-minibuffer-add-variable
             jao-minibuffer-refresh
             jao-minibuffer-mode))

(setq enable-recursive-minibuffers t)
(require 'mb-depth)
(minibuffer-depth-indicate-mode 1)

(setq minibuffer-default-prompt-format " (default %s)")
(minibuffer-electric-default-mode 1)

(jao-minibuffer-mode 1)

;;; Mode line
;;;; config
(setq line-number-display-limit-width 250
      mode-line-position-column-format '(" %c")
      mode-line-position-line-format '(" %c %l")
      mode-line-end-spaces nil
      mode-line-percent-position
      '("%2l" (:eval (format " %d " (line-number-at-pos (point-max)))) "%2c"))

(line-number-mode -1)
(column-number-mode -1)

;;;; jao-mode-line
(defvar jao-mode-line-in-minibuffer t)

(use-package jao-mode-line
  :commands (jao-mode-line-add-to-minibuffer-left
             jao-mode-line-add-to-minibuffer-right
             jao-mode-line-remove-from-minibuffer))

;;;; time display
(setq world-clock-list
      '(("Europe/London" "Edinburgh")
        ("Europe/Paris" "Barcelona")
        ("Asia/Tokyo" "Tokyo")
        ("America/Los_Angeles" "Corvallis")
        ("America/New_York" "New York")))

(setq display-time-day-and-date nil
      display-time-24hr-format nil
      display-time-default-load-average nil
      display-time-format " %a %e %H:%M")

(defun jao-time-to-epoch (&optional s)
  "Transform a time string to an epoch integer in milliseconds."
  (interactive)
  (let ((s (or s (read-string "Time string: " (thing-at-point 'string)))))
    (message "%s = %s"
             s
             (round (* 1000 (time-to-seconds (parse-time-string s)))))))

(defun jao-epoch-to-time (&optional v)
  "Transform an epoch, given in milliseconds, to a time string."
  (interactive)
  (let ((v (or v (read-number "Milliseconds: " (thing-at-point 'number)))))
    (message "%s = %s" v
             (format-time-string "%Y-%m-%d %H:%M:%S"
                                 (seconds-to-time (/ v 1000.0))))))

;;;; mode line toggle
(use-package jao-mode-line
  :init
  (when (and window-system (not jao-mode-line-in-minibuffer))
    (add-to-list 'after-make-frame-functions #'jao-mode-line-hide-inactive)
    (add-hook 'after-init-hook #'jao-mode-line-toggle-inactive))
  :demand t
  :bind (("<home>" . jao-mode-line-toggle-inactive)
         ("<end>" . jao-mode-line-toggle)
         ("<insert>" . jao-mode-line-echo)))

;;;; diminish
(use-package diminish
  :ensure t
  :demand t
  :diminish ((auto-fill-function . " §")
             (auto-revert-mode . "")))

(use-package outline
  :diminish ((outline-minor-mode . "")))

;;;; battery
(use-package battery
  :init (setq battery-load-low 15
              battery-load-critical 8
              battery-mode-line-limit 40
              battery-echo-area-format
              "%L %r %B (%p%% load, remaining time %t)"
              battery-mode-line-format " 🔋%b%p% "))

(with-eval-after-load "jao-minibuffer"
  (if jao-mode-line-in-minibuffer
      (display-battery-mode 1)
    (jao-minibuffer-add-variable 'battery-mode-line-string 80)))

;;; Notifications
;;;; jao-notify
(use-package jao-notify
  :demand t
  :init (setq jao-notify-use-messages t))

;;;; tracking
(use-package tracking
  :demand t
  :init (setq tracking-position 'before-modes
              tracking-frame-behavior nil
              tracking-most-recent-first nil
              tracking-max-mode-line-entries 10
              tracking-sort-faces-first t
              tracking-shorten-modes '())
  :config
  (setq erc-track-enable-keybindings nil))

(use-package jao-tracking
  :demand t
  :init (setq jao-tracking-bkg (if (jao-colors-scheme-dark-p) "grey20" "grey93"))
  :config (jao-tracking-setup t))

;;;; ednc

(use-package ednc
  :ensure t
  :diminish nil)

(use-package jao-ednc
  :demand t
  :init (setq jao-ednc-use-tracking nil)
  :commands (jao-ednc-setup)
  :after ednc
  :config
  (jao-ednc-ignore-app "Firefox")
  (transient-define-prefix jao-transient-ednc ()
    ["Notifications"
     ("s" "show last" jao-ednc-show)
     ("S" "show all" jao-ednc-pop)
     ("n" "dismiss and show" jao-ednc-dismiss-and-show :transient t)
     ("d" "dismiss last" jao-ednc-dismiss)
     ("D" "dismiss all" jao-ednc-dismiss-all)
     ("i" "invoke last action" jao-ednc-invoke-last-action)])
  (global-set-key (kbd "s-n") #'jao-transient-ednc))

;;; Calendar, diary, weather
;;;; diary
(setq diary-file (expand-file-name "diary" jao-org-dir)
      diary-display-function 'diary-fancy-display
      diary-mail-addr "jao@localhost"
      diary-comment-start ";;"
      diary-comment-end "")

(add-hook 'diary-list-entries-hook 'diary-sort-entries t)

;;;; calendar
(setq appt-display-format nil)
(appt-activate 1)
(setq calendar-latitude 55.9533
      calendar-longitude -3.1883
      calendar-left-margin 4
      calendar-location-name "Edinburgh, Scotland"
      calendar-mark-diary-entries-flag t
      calendar-week-start-day 1 ;; 0 sunday
      calendar-date-echo-text '(format "ISO date: %s"
                                       (calendar-iso-date-string
                                        (list month day year))))

(setq calendar-holidays
      '((holiday-fixed 1 1 "New Year's Day")
        (holiday-fixed 4 1 "April Fools' Day")
        (holiday-float 5 0 2 "Mother's Day")
        (holiday-fixed 3 19 "Father's Day")
        (holiday-float 11 4 4 "Thanksgiving")
        (holiday-fixed 12 25 "Christmas")
        (holiday-chinese-new-year)
        (solar-equinoxes-solstices)
        (holiday-sexp calendar-daylight-savings-starts
                      (format "Daylight Saving Time Begins %s"
                              (solar-time-string
                               (/ calendar-daylight-savings-starts-time
                                  (float 60))
                               calendar-standard-time-zone-name)))
        (holiday-sexp calendar-daylight-savings-ends
                      (format "Daylight Saving Time Ends %s"
                              (solar-time-string
                               (/ calendar-daylight-savings-ends-time
                                  (float 60))
                               calendar-daylight-time-zone-name)))))

(add-to-list 'display-buffer-alist
             `(,(regexp-quote diary-fancy-buffer)
               (display-buffer-at-bottom)
               (window-parameters (mode-line-format . none))
               (window-height . fit-window-to-buffer)))

(defun jao-diary--select ()
  (switch-to-buffer diary-fancy-buffer))

(add-hook 'diary-fancy-display-mode-hook #'jao-diary--select)
(setq org-calendar-insert-diary-entry-key nil
      org-agenda-diary-file 'diary-file)

;;;; winttr
(defun jao-weather (&optional wide)
  (interactive "P")
  (if (not wide)
      (message "%s"
               (jao-shell-string "curl -s"
                                 "https://wttr.in/?format=%l++%m++%C+%c+%t+%w++%p"))
    (jao-afio-goto-scratch)
    (if-let ((b (get-buffer "*wttr*")))
        (progn (pop-to-buffer b)
               (term-send-string (get-buffer-process nil) "clear;curl wttr.in\n"))
      (jao-exec-in-term "curl wttr.in" "*wttr*"))))
(global-set-key (kbd "<f5>") #'jao-weather)

;;; Files, dired and scratch buffer
;;;; so-long
(setq large-file-warning-threshold (* 200 1024 1024))

;; (use-package so-long
;;   :ensure t
;;   :diminish)
;; (global-so-long-mode 1)

;;;; persistent scratch
(use-package persistent-scratch
  :ensure t
  :config (persistent-scratch-setup-default))

;;;; dired
(use-package dired
  :init
  (setq dired-recursive-deletes 'top
        dired-recursive-copies 'top
        dired-listing-switches "-alhF --group-directories-first"
        ls-lisp-dirs-first t
        dired-dwim-target t
        dired-kill-when-opening-new-dired-buffer t
        dired-mouse-drag-files t
        wdired-create-parent-directories t
        dired-guess-shell-alist-user
        '(;; ("\\.\\(png\\|jpe?g\\|tiff\\)" "feh" "xdg-open")
          ("\\.\\(mp[34]\\|m4a\\|ogg\\|flac\\|webm\\|mkv\\)" "mpv" "xdg-open")
	  (".*" "xdg-open")))

  (put 'dired-find-alternate-file 'disabled nil)
  :hook (dired-mode . turn-on-gnus-dired-mode)
  :bind (:map dired-mode-map
              ("C-c C-r" . wdired-change-to-wdired-mode)
              ("C-M-m" . gnus-dired-attach)))

(use-package dired-x :demand t)

(use-package find-dired
  :init (setq find-ls-option '("-print0 | xargs -0 ls -ld" . "-ld"))
  :bind ("C-c D" . find-name-dired))

(use-package dired-duplicates :ensure t)

;;; General editing
;;;; automatically uncompress
(require 'jka-compr)
(auto-compression-mode 1)

;;;; wgrep
(use-package wgrep :ensure t)
(require 'wgrep)

;;;; executable scripts
(add-hook 'after-save-hook
          'executable-make-buffer-file-executable-if-script-p)

;;;; spaces, tabs, kill
(setq kill-whole-line t)
(setq-default indent-tabs-mode nil)
(setq indent-tabs-width 4)
(setq-default default-tab-width 8)
(setq kill-read-only-ok t)
(setq view-read-only nil)

;;;; whitespace and filling column
(add-hook 'write-file-functions 'delete-trailing-whitespace)
(setq-default indicate-empty-lines nil)
(setq-default fill-column 78)
(setq comment-auto-fill-only-comments nil)

(use-package whitespace
  :init
  (setq whitespace-style '(face tabs trailing ;; lines-tail
                           empty  missing-newline-at-eof)
        whitespace-line-column 80)
  :hook (prog-mode . whitespace-mode)
  :diminish nil)

(use-package display-fill-column-indicator
  :init (setq-default display-fill-column-indicator-column 80)
  :hook (prog-mode . display-fill-column-indicator-mode))

;;;; visible mode
(use-package visible-mode
  :bind (("s-v" . visible-mode)))

;;;; changes
(use-package goto-chg
  :ensure t
  :bind (("C-." . goto-last-change)
         ("C-c ." . goto-last-change)
         ("C-c ," . goto-last-change-reverse)))

;;;; eval-and-replace
(defun fc-eval-and-replace ()
  "Replace the preceding sexp with its value."
  (interactive)
  (backward-kill-sexp)
  (condition-case nil
      (prin1 (eval (read (current-kill 0)))
             (current-buffer))
    (error (message "Invalid expression")
           (insert (current-kill 0)))))

 (global-set-key "\C-ce" 'fc-eval-and-replace)

;;;; skeletons and autoinsert
(use-package autoinsert
  :config
  (setq auto-insert-directory "~/.emacs.d/autoinsert/"
        auto-insert t
        auto-insert-query t)
  (setf (alist-get 'html-mode auto-insert-alist nil t) nil))
(add-hook 'find-file-hook #'auto-insert)

(use-package jao-skel
  :demand t
  :config
  (defvar flymake-allowed-file-name-masks nil)
  (require 'jao-skel-geiser)
  (require 'jao-skel-lisp)
  (require 'jao-skel-haskell)
  (require 'jao-skel-latex))

;;; Completion and search
;;;; completion
(require 'jao-custom-completion)

;;;; recoll
(jao-load-path "consult-recoll")

(defun jao-recoll-format (title url _mtype)
  (let* ((u (replace-regexp-in-string "file://" "" url))
         (u (replace-regexp-in-string "/home/jao/" "" u))
         (u (replace-regexp-in-string
             "\\(doc\\|org/doc\\|.emacs.d/gnus/Mail\\|var/mail\\)/" "" u)))
    (format "%s (%s)" ;; "%s (%s, %s)"
            title
            (propertize u 'face 'jao-themes-f01)
            ;; (propertize mtype 'face 'jao-themes-f01)
            )))

(defun jao-recoll-open-html (file &optional _page)
  (if (string-match-p "\.epub\\'" file)
      (find-file file)
    (jao-afio-goto-www)
    (if jao-afio-use-w3m (w3m-find-file file) (eww-open-file file))))

(defun jao-recoll-open-pdf (file &optional page)
  (if (string-match-p "/gnus/Mail/" file)
      (funcall (or (cdr (assoc-string "message/rfc822" consult-recoll-open-fns))
                   'find-file)
               file
               page)
    (jao-open-doc file page)))

(defun jao-recoll-consult-messages ()
  (interactive)
  (consult-recoll "mime:message "))

(defun jao-recoll-consult-docs ()
  (interactive)
  (consult-recoll (format "dir:%s/doc " jao-org-dir)))

(defun jao-recoll-consult-notes ()
  "Use consult-recoll to search notes."
  (interactive)
  (consult-recoll (format "dir:%s " jao-org-notes-dir)))

(use-package consult-recoll
  :commands (consult-recoll consult-recoll-embark-setup)
  :init (setq consult-recoll-open-fns
              '(("application/pdf" . jao-recoll-open-pdf)
                ("text/html" . jao-recoll-open-html))
              consult-recoll-search-flags 'query
              consult-recoll-inline-snippets t
              consult-recoll-format-candidate #'jao-recoll-format)
  :config
  (consult-customize consult-recoll :preview-key 'any)

  (transient-define-prefix jao-transient-recoll ()
    ["Consult recoll queries"
     ("r" "everywhere" consult-recoll)
     ("n" "on notes" jao-recoll-consult-notes)
     ("d" "on docs" jao-recoll-consult-docs)
     ("m" "on messages" jao-recoll-consult-messages)])

  :bind (("s-r" . #'jao-transient-recoll)))

(with-eval-after-load "embark" (consult-recoll-embark-setup))

;;; Buffers
;;;; cursor and mark
(transient-mark-mode -1)
(blink-cursor-mode -1)
(setopt cursor-in-non-selected-windows nil
        visible-cursor nil) ;; stop blinking in xterm

;;;; uniquifiy
(require 'uniquify)
(setq uniquify-buffer-name-style 'forward
      uniquify-trailing-separator-p t)

;;;; autosave
(setq auto-save-list-file-prefix "~/.emacs.d/auto-save-list/.saves-"
      auto-save-no-message t
      kill-buffer-delete-auto-save-files t)

(setq lock-file-name-transforms
      '(("\\`/.*/\\([^/]+\\)\\'" "/tmp/emacs-lock/\\1" t)))

;;;; autorevert
(setq auto-revert-check-vc-info nil)
(setq auto-revert-verbose nil)
(setq auto-revert-avoid-polling t)
(setq auto-revert-mode-text "")
(require 'autorevert)
(global-auto-revert-mode 1)

;;;; attached buffers
(defun jao-display-buffer-below-selected (buffer alist)
  (delete-other-windows-vertically)
  (display-buffer-below-selected buffer alist))

(defun jao-attached-buffer-entry (name-rx height)
  `(,name-rx (display-buffer-reuse-window
              jao-display-buffer-below-selected)
             (window-height . ,(or height 25))))

(defmacro jao-with-attached-buffer (name-rx height &rest body)
  (declare (indent defun))
  `(let ((display-buffer-alist '(,(jao-attached-buffer-entry name-rx height))))
     ,@body))

(defun jao-define-attached-buffer (name-rx &optional height)
  (add-to-list 'display-buffer-alist
               (jao-attached-buffer-entry name-rx height)))

(jao-define-attached-buffer "\\*eldoc\\( .*\\)?\\*" 0.33)

;;;; same mode
(defun jao-buffer-same-mode (&optional mode pre-fn switch-fn)
  (interactive)
  (let* ((mode (or mode major-mode))
         (modes (if (symbolp mode) (list mode) mode))
         (pred `(lambda (b)
                  (let ((b (get-buffer (if (consp b) (car b) b))))
                    (member (buffer-local-value 'major-mode b)
                            ',modes))))
         (buff (read-buffer "Buffer: " nil t pred)))
    (when pre-fn (funcall pre-fn))
    (if switch-fn (funcall switch-fn buff) (switch-to-buffer buff))))

(defun jao-buffer-same-mode-cmd (&optional pop)
  (interactive "P")
  (jao-buffer-same-mode nil nil (and pop #'pop-to-buffer)))

(global-set-key (kbd "C-c C-b") #'jao-buffer-same-mode-cmd)

;;;; projects
(use-package project :demand t)
(global-set-key "\C-xp" 'jao-prev-window)

;;;; buffer quit function (the triple ESC)
(setq buffer-quit-function (lambda () t))


;;;; redisplay escape hatch
;; (setq max-redisplay-ticks 2250000)
;;;; scrolling
(if window-system
    (setq scroll-preserve-screen-position 'always
          scroll-conservatively most-positive-fixnum
          scroll-margin 0
          scroll-step 2
          redisplay-skip-fontification-on-input t)
  (setq scroll-preserve-screen-position nil
        scroll-conservatively 0
        scroll-margin 0
        scroll-step 1
        redisplay-skip-fontification-on-input nil))

;;; Windows
;;;; splitting and switch
(setq split-height-threshold 80
      split-width-threshold 144
      display-buffer-avoid-small-windows 20)

(setq switch-to-buffer-preserve-window-point nil
      switch-to-buffer-obey-display-actions t
      switch-to-prev-buffer-skip 'this) ;; don't switch to a
                                        ;; buffer already visible in
                                        ;; this frame

(global-set-key (kbd "C-x _") #'delete-other-windows-vertically)

;;;; first window
(defvar jao-first-window--from nil)

(defun jao-first-window ()
  "Go to previous windows in frame, remembering where we were."
  (interactive)
  (let ((cb (current-buffer)))
    (if (eq (get-buffer-window cb) (select-window (frame-first-window)))
        (when jao-first-window--from (pop-to-buffer jao-first-window--from))
      (setq jao-first-window--from cb))))

(global-set-key (kbd "s-a") #'jao-first-window)
(global-set-key (kbd "M-a") #'jao-first-window)

;;;; window navigation (custom)
(defun jao-nth-window (n)
  (if (zerop n)
      'jao-first-window
    `(lambda ()
       (interactive)
       (select-window (frame-first-window))
       (dotimes (x ,n) (other-window 1)))))

(defun jao-prev-window ()
  "Go to previous window."
  (interactive)
  (other-window -1))

(defvar jao-prev-window-repeat-map
  (let ((map (make-sparse-keymap)))
    (define-key map "p" 'jao-prev-window)
    (define-key map "P" (lambda ()
                          (interactive)
                          (setq repeat-map 'jao-prev-window-repeat-map)
                          (other-window 1)))
    map)
  "Keymap to repeat `prev-window' key sequences.  Used in `repeat-mode'.")

(put 'jao-prev-window 'repeat-map jao-prev-window-repeat-map)

(mapc (lambda (n)
        (global-set-key (format "\C-c%s" (1+ n)) (jao-nth-window n)))
      '(0 1 2 3 4 5 6 7 8))

;; transposing windows
(defun transpose-windows (arg)
  "Transpose the buffers shown in two windows."
  (interactive "p")
  (let ((selector (if (>= arg 0) 'next-window 'previous-window)))
    (while (/= arg 0)
      (let ((this-win (window-buffer))
            (next-win (window-buffer (funcall selector))))
        (set-window-buffer (selected-window) next-win)
        (set-window-buffer (funcall selector) this-win)
        (select-window (funcall selector)))
      (setq arg (if (> arg 0) (1- arg) (1+ arg))))))

(define-key ctl-x-4-map (kbd "t") 'transpose-windows)

;;;; winner mode
(winner-mode 1)

;;; Frames
;;;; frame geometry
(setq frame-resize-pixelwise t)
(modify-all-frames-parameters
 `((horizontal-scroll-bars . nil)
   (vertical-scroll-bars . nil)
   (scroll-bar-width . 0)
   (menu-bar . nil)))

;;;; frame layout, title, etc
(setq frame-title-format '("%b - emacs"))
(use-package fringe)
(fringe-mode)

(menu-bar-mode -1)

;; (setting it to nil avoids mouse wrapping after other-frame)
(setq focus-follows-mouse (and window-system t))

(use-package scroll-bar)
(set-scroll-bar-mode nil)
(use-package tool-bar)
(tool-bar-mode -1)

(defalias 'jao-trisect 'jao-afio-trisect)

(defun jao-bisect ()
  (interactive)
  (jao-trisect t)
  (other-window 1)
  (delete-window))

;;;; afio
(use-package jao-afio
  :demand t
  :config (jao-afio-setup (not window-system))
  :bind (("C-c f" . 'jao-afio-goto-main)
         ("C-c g" . 'jao-afio-goto-mail)
         ("C-c w" . 'jao-afio-goto-www)
         ("C-c z" . 'jao-afio-goto-docs)
         ("C-c t" . 'jao-afio-goto-chats)
         ("C-c 0" . 'jao-afio-goto-scratch)
         ("M-o" . 'jao-afio-toggle)))
(add-hook 'jao-afio-switch-hook 'jao-minibuffer-refresh t)

(defun jao-current--frame-id ()
  (propertize (cond ((and (fboundp 'jao-exwm-enabled)
                          (jao-exwm-enabled-p)
                          (not (bound-and-true-p jao-exwm--use-afio))
                          (boundp 'exwm-workspace-current-index))
                     (format "F%s" exwm-workspace-current-index))
                    (t jao-afio-use-frames (or (jao-afio-frame-name) ""))
                    (t (format "%s" (or (jao-afio-frame-no) ""))))
              'face 'font-lock-warning-face))

(jao-minibuffer-add-variable '(jao-current--frame-id) 100)

;;; Writing and writing modes
;;;; copyright notices
(setq copyright-year-ranges t)
(add-hook 'write-file-functions 'copyright-update)

;;;; indent on yank
(defvar jao-auto-indent-modes
  '(emacs-lisp-mode ;; clojure-mode
    scheme-mode objc-mode
    tuareg-mode c-mode c++-mode
    tcl-mode sql-mode
    perl-mode cperl-mode
    java-mode jde-mode
    LaTeX-mode TeX-mode))

(defadvice yank (after indent-region activate)
  (if (member major-mode jao-auto-indent-modes)
      (indent-region (region-beginning) (region-end) nil)))

;;;; org mode
(require 'jao-custom-org)

;;;; blog
(require 'jao-custom-blog)

;;;; text-ish mode settings
;; SENTENCES separated by just one space
(setq sentence-end "[.?!][]\"')]*\\($\\|\t\\| \\)[ \t\n]*")
(setq sentence-end-double-space t)
;; copy rectangle
(defun kill-rectangle-save (start end)
  "Save the region-rectangle as the last killed one."
  (interactive "r")
  (require 'rect)           ; Make sure killed-rectangle is defvar'ed.
  (setq killed-rectangle (extract-rectangle start end))
  (message "Rectangle saved"))

;; text mode, autoinserts and write hooks
(setq default-major-mode 'text-mode)
(add-hook 'text-mode-hook 'turn-on-auto-fill)

;;;; dictionaries
(use-package dictionary
  :init (setq dictionary-use-single-buffer t
              dictionary-server "localhost")
  :commands (dictionary-search
             dictionary-match-words
             dictionary-lookup-definition
             dictionary
             dictionary-mouse-popup-matching-words
             dictionary-popup-matching-words
             dictionary-tooltip-mode
             global-dictionary-tooltip-mode)
  :bind (("C-c d" . dictionary-search)))

(use-package ispell
  :custom ((ispell-personal-dictionary
            (expand-file-name "~/.emacs.d/ispell.dict"))))

(use-package reverso
  :ensure t
  :init (setq reverso-languages '(english spanish french german)))

;; (use-package wordreference
;;   :ensure t
;;   :init (setq wordreference-target-lang "es"
;;               wordreference-source-lang "en")
;;   :bind (("C-c D" . wordreference-search)))

;;;; markdown
(use-package markdown-mode
  :ensure t
  :init (setq markdown-command '("pandoc" "--from=markdown" "--to=html5")
              markdown-asymmetric-header t
              markdown-enable-wiki-links t
              markdown-wiki-link-fontify-missing t
              markdown-enable-math  nil ;; toggle with M-x markdown-toggle-math
              markdown-link-space-sub-char "-"
              markdown-gfm-additional-languages '("whizzml" "flatline")
              markdown-hide-urls t
              markdown-hide-markup nil
              markdown-fontify-code-blocks-natively t
              markdown-fontify-whole-heading-line t
              markdown-unordered-list-item-prefix t)
  :hook (markdown-mode . outline-minor-mode)
  :config
  (dolist (u '("doc" "message" "notmuch"))
    (add-to-list 'markdown-uri-types u))
  (use-package markdown-toc :ensure t))

;; used by markdown mode to edit code blocks
(use-package edit-indirect :ensure t)

(dolist (ext '("\\.md$" "\\.markdown$"))
  (add-to-list 'auto-mode-alist (cons ext 'markdown-mode)))

;;;; TeX and LaTex
(use-package tex-site
  :ensure auctex
  :init
  (setq TeX-auto-save t)
  (setq TeX-parse-self t)
  (setq TeX-a4-paper t)
  (setq TeX-auto-local ".tex-auto-local")
  ;; Preferred view format: dvi, ps, pdf, pdfs
  (setq TeX-view-format "pdf")
  (setq-default TeX-master "../main") ; nil to ask
  (setq TeX-view-program-selection
        ;;  '((output-dvi "open")
        ;;    (output-pdf "open")
        ;;    (output-html "open"))
        '(((output-dvi has-no-display-manager) "dvi2tty")
          ((output-dvi style-pstricks) "dvips and gv")
          (output-dvi "xdvi")
          (output-pdf "xdg-open")
          (output-html "xdg-open")))
  ;; to make RefTeX faster for large documents, try these:
  (setq reftex-enable-partial-scans t)
  (setq reftex-save-parse-info t)
  (setq reftex-use-multiple-selection-buffers t)
  ;; to integrate with AUCTeX
  (setq reftex-plug-into-AUCTeX t)
  (setq reftex-ref-style-default-list
        '("Hyperref" "Varioref" "Fancyref"))
  (setq LaTeX-command "latex -shell-escape")
  (setq LaTeX-biblatex-use-Biber t)
  (setq bibtex-dialect 'biblatex)
  :config
  (add-hook 'TeX-after-compilation-finished-functions 'TeX-revert-document-buffer)
  (add-hook 'LaTeX-mode-hook 'turn-on-reftex))

;;; Browsing
;;;; variables
(defvar jao-browse-doc-use-emacs-p t)
(defvar jao-browse-url-function nil)
(defvar jao-browse-url-external-function nil)

;;;; url around point
(defun jao-url-around-point (&optional current-url)
  (or (and (fboundp 'w3m-anchor) (w3m-anchor))
      (shr-url-at-point nil)
      (ffap-url-at-point)
      (thing-at-point 'url)
      (when current-url
        (or (and (fboundp 'w3m-anchor) (w3m-anchor))
            (and (derived-mode-p 'eww-mode) (plist-get eww-data :url))))))

(defun jao--url-prompt (&optional prefix)
  (let* ((def (jao-url-around-point t))
         (prompt (concat prefix "URL" (if def (format " (%s): " def) ": "))))
    (read-string prompt nil nil def)))

;;;; downloads using wget
(defun jao-wget--get-title (filename)
  (or (and (derived-mode-p 'w3m-mode) (w3m-current-title))
      (plist-get eww-data :title)
      (and (not (string-blank-p (or filename "")))
           (subst-char-in-string ?- ?  (capitalize (file-name-base filename))))))

(defun jao-wget (url &optional user pwd)
  "Download URL using wget and kill a link for an org note."
  (let* ((def (file-name-nondirectory url))
         (pmt (format "Save %s to: " url))
         (read-file-name-function nil)
         (dest (expand-file-name
                (read-file-name pmt jao-sink-dir nil nil def)))
         (title (jao-wget--get-title dest))
         (src-url (or (jao-url-around-point t) (file-name-directory url)))
         (auth (when (and user pwd)
                 `(,(format "--http-user=%s" user)
                   ,(format "--http-password=%s" pwd))))
         (lnk (concat "doc:" (file-name-nondirectory dest))))
    (switch-to-buffer-other-window (get-buffer-create "*downloads*"))
    (erase-buffer)
    (kill-new (format "%s (from %s)"
                      (org-link-make-string lnk title)
                      (org-link-make-string src-url "here")))
    (apply 'make-term `("downloads" "wget" nil ,@auth "-O" ,dest ,url))))

(defun jao-download (url &optional pws)
  "Download URL using wget"
  (interactive (list (jao--url-prompt)))
  (when url
    (let ((usr (and pws (read-string "Login name: ")))
          (pwd (and pws (read-passwd "Password: "))))
      (jao-wget url usr pwd))))

(with-eval-after-load "embark"
  (define-key embark-url-map (kbd "d") #'jao-download))

;;;; video
(defvar jao-video--url-rx
  (format "^https?://\\(?:www\\.\\)?%s/.+"
          (regexp-opt '("youtu.be"
                        "youtube.com"
                        "blip.tv"
                        "vimeo.com"
                        "infoq.com")
                      t)))

(defvar jao-video--ext-rx
  (format "^https?://.+/.+\\.%s" (regexp-opt '("mp3" "webm" "mp4"))))

(defun jao-video--url-p (url)
  (or (string-match-p jao-video--url-rx url)
      (string-match-p jao-video--ext-rx url)))

(defun jao--remote-run (url prg)
  (let ((args (format "%s %s" prg (shell-quote-argument url))))
    (start-process-shell-command prg nil args)))

(defun jao--mpv (url &rest _args) (jao--remote-run url "mpv"))
(defun jao--vlc (url &rest _args) (jao--remote-run url "vlc"))

(defvar jao--video-player 'jao--mpv)

(defun jao-view-video (url)
  "Tries to stream a video from the current or given URL"
  (interactive (list (jao--url-prompt "Video ")))
  (when url (funcall jao--video-player url)))

(defun jao-maybe-view-video (url &rest _ignored)
  (interactive)
  (if (y-or-n-p "View video (y) or web page (n)? ")
      (jao-view-video url)
    (funcall jao-browse-url-function url)))

;;;; web browsers
(defun jao-www--buffer-p (b)
  (with-current-buffer b
    (or (derived-mode-p 'w3m-mode 'eww-mode)
        (and (boundp 'exwm-class-name)
             (member exwm-class-name '("vlc" "mpv"))))))

(require 'jao-custom-eww)
;; (require 'jao-custom-w3m)

;;;; browse-url
(require 'browse-url)

(setq browse-url-generic-program "~/bin/firehog")

(defun jao-browse-with-external-browser (&rest url)
  "Browse with external hogging"
  (interactive "s")
  (let ((url (or (car url) (jao-url-around-point))))
    (if (not url)
        (message "No URL at point")
      (cond ((and (jao-exwm-enabled-p) (fboundp 'jao-exwm-firefox))
             (jao-exwm-firefox))
            (jao-river-enabled (jao-river-to-ws 2))
            (jao-sway-enabled (jao-sway-firefox)))
      (browse-url-generic url))))
(setq jao-browse-url-external-function 'jao-browse-with-external-browser)

(defun jao--fln (url)
  (shell-quote-argument
   (if (string-match "^[^:]*:/*?\\(/?[^/].*\\)" url)
       (match-string-no-properties 1 url)
     url)))

(defun jao--browse-doc (url &rest _ignored)
  (let* ((url (substring-no-properties url))
         (file (jao--fln url)))
    (when file
      (unless (file-exists-p file)
        (error "File %s does not exist" file))
      (jao-open-doc file))))

(defun jao--make-file-rx (exts)
  (format "file:/?/?.+\\.%s$" (regexp-opt exts)))

(defvar jao--see-exts (jao--make-file-rx '("jpg" "jpeg" "png")))

(defvar jao--doc-exts
  (jao--make-file-rx '("ps" "ps.gz" "pdf" "dvi" "djvu" "chm")))

(defvar jao-browse-url-wget-exts
  '("ps" "pdf" "dvi" "djvu" "zip" "gz" "tgz"))

(defvar jao-browse-external-domains
  '("github.com" "gitlab.com" "slack.com" "spotify.com" "drive.google.com"
    "meet.google.com" "docs.google.com" "x.com" "twitter.com"
    "t.com" "linkedin.com" "bigml.com" "slack.com" "zoom.us"))

(defvar jao-browse--external-regexp
  (format "https?://.*%s\\(/.*\\)?"
          (regexp-opt jao-browse-external-domains)))

(defun jao-wget--regexp ()
  (concat "^http[s]?://.+\\(\\."
    (mapconcat 'identity jao-browse-url-wget-exts "\\|\\.")
    "\\)\\'"))

(defun jao--see (url &rest _r)
  (start-process-shell-command "see" nil (format "see %s" (jao--fln url))))

(defun jao--find-file-other-window (url &rest _)
  (find-file-other-window (jao--fln url)))

(defvar jao-browse--sound-rx
  (format "^https?://.*/.*\\.%s" (regexp-opt '("mp3" "flv"))))

(defun jao-browse-play-sound-url (url &rest _)
  (jao-mpc-add-or-play-url url))

(defun jao-browse-url-browse (&rest args)
  (apply jao-browse-url-function args))

(setq browse-url-handlers
      `((jao-video--url-p . jao-maybe-view-video)
        (,jao--doc-exts . jao--browse-doc)
        (,jao--see-exts . jao--see)
        ("^file://?.+\\.html?$" . ,jao-browse-url-function)
        ("^file://?" . jao--find-file-other-window)
        (,jao-browse--external-regexp . ,jao-browse-url-external-function)
        ("^https?://.*\\.gotomeeting\\.com\\.*" . browse-url-chrome)
        (,jao-browse--sound-rx . jao-browse-play-sound-url)
        (,(jao-wget--regexp) . jao-download)
        ("." . jao-browse-url-browse)))

(when (< emacs-major-version 28)
  (setf (alist-get 'jao-video--url-p browse-url-handlers nil t) nil)
  (setq browse-url-browser-function browse-url-handlers))

;;;; subscribe to rss using r2e
(autoload 'View-quit "view")

(defun jao-rss--find-url ()
  (save-excursion
    (when (derived-mode-p 'w3m-mode 'eww-mode)
      (if (fboundp 'w3m-view-source) (w3m-view-source) (eww-view-source)))
    (goto-char (point-min))
    (when (re-search-forward
           "type=\"application/\\(?:atom\\|rss\\)\\+xml\" +" nil t)
      (let ((url (save-excursion
                   (when (re-search-forward
                          "href=\"\\([^\n\"]+\\)\"" nil t)
                     (match-string-no-properties 1))))
            (title (when (re-search-forward
                          "\\(?:title=\"\\([^\n\"]+\\)\" +\\)" nil t)
                     (match-string-no-properties 1))))
        (cond ((derived-mode-p 'w3m-view-mode) (w3m-view-source))
              ((string-match-p ".*\\*eww-source\\b.*" (buffer-name))
               (View-quit)))
        (when url (cons url (or title "")))))))

(defun jao-rss2e-append (name url mbox)
  (with-current-buffer (find-file-noselect "~/.config/rss2email.cfg")
    (goto-char (point-max))
    (insert "[feed." name "]\nurl = " url)
    (insert "\nto = " mbox "+" name "@localhost")
    (insert "\nmaildir-mailbox = " mbox "\n\n")
    (save-buffer)))

(defun jao-rss--feeds-dirs ()
  (mapcar (lambda (d) (cadr (split-string d "\\.")))
          (directory-files "~/.emacs.d/gnus/Mail/" nil "^feeds")))

(defun jao-rss-subscribe (url)
  "Subscribe to a given RSS URL.  If URL not given, look for it."
  (interactive (list (or (jao-url-around-point)
                         (jao-rss--find-url)
                         (read-string "Feed URL: "))))
  (let* ((url+title (ensure-list url))
         (url (car url+title))
         (title (cdr url+title)))
    (unless url (error "No feeds found"))
    (let ((url (if (string-match "^feed:" url) (substring url 5) url)))
      (when (y-or-n-p (format "Subscribe to <%s>? " url))
        (let* ((name (read-string "Feed name: " title))
               (cats (cons "prog" (jao-notmuch--subtags "feeds")))
               (cat (completing-read "Category: " cats nil t))
               (subs (format "r2e add %s '%s' feeds.%s@localhost"
                             name url cat)))
          ;; (jao-rss2e-append name url cat)
          (shell-command-to-string subs)
          (shell-command (format "r2e run %s" name)))))))

;;; PDFs and other docs
;;;; open pdfs
(use-package jao-pdf :demand t)

(use-package saveplace-pdf-view
  :ensure t
  :demand t
  :after doc-view)

(setq jao-open-doc-fun 'jao-find-or-open)
(setq jao-org-open-pdf-fun 'jao-find-or-open)

(defun jao-zathura-open (file page)
  (let ((id (jao-x11-search-window (jao-pdf-zathura-title-rx file))))
    (if (string-blank-p id)
        (progn
          (when jao-xmonad-enabled (jao-x11-goto-ws 2))
          (jao-shell-exec (jao-pdf-zathura-open-cmd file page)))
      (let* ((page (if page (format " && xdotool type %dg" page) ""))
             (cmd (format "xdotool windowactivate %s%s" id page)))
        (jao-shell-exec cmd t)))))

(defun jao-x11-zathura-goto-org (&optional title no-ask)
  (let ((title (or title (jao-shell-string "xdotool"
                                           "getactivewindow"
                                           "getwindowname"))))
    (jao-org-open-from-zathura title no-ask)))

(defun jao-find-or-open (file &optional page height)
  (cond ((and jao-browse-doc-use-emacs-p window-system)
         (let* ((buffs (buffer-list))
                (b (catch 'done
                     (while buffs
                       (when (string-equal (buffer-file-name (car buffs)) file)
                         (throw 'done (car buffs)))
                       (setq buffs (cdr buffs))))))
           (jao-afio-goto-docs)
           (if b (pop-to-buffer b) (find-file file))
           (when page (jao-doc-view-goto-page page height))))
        (jao-river-enabled (jao-river-open-with-zathura file page))
        (jao-sway-enabled (jao-sway-open-with-zathura file page))
        (t (jao-zathura-open file page))))

(defun jao-open-doc (&optional file page height)
  (interactive)
  (when-let (file (or file
                      (read-file-name "Document: "
                                      (concat jao-org-dir "/doc/"))))
    (funcall jao-open-doc-fun file page height)))

(defun jao-select-pdf ()
  (interactive)
  (jao-buffer-same-mode '(pdf-view-mode doc-view-mode)
                        #'jao-afio-goto-docs))

(defun jao-open-with-zathura ()
  (interactive)
  (when-let (f buffer-file-name)
    (let ((p (jao-doc-view-current-page)))
      (cond (jao-river-enabled (jao-river-open-with-zathura f p))
            (jao-sway-enabled (jao-sway-open-with-zathura f p))
            (t (jao-zathura-open f p))))))

;; doc:// links for browse-url
(defun jao-open-doc-url (url &rest _)
  (when (string-match "doc://\\([^?]+\\)\\(\\?.*\\)?" url)
    (let ((file (match-string 1 url))
          (page (when-let* ((qs (match-string 2 url))
                            (long (> (length qs) 1))
                            (ps (url-parse-query-string (substring qs 1)))
                            (pn (cadr (assoc "page" ps))))
                  (string-to-number pn))))
      (jao-open-doc (expand-file-name (concat "doc/" file) jao-org-dir) page))))

(add-to-list 'browse-url-handlers (cons "^doc://.+" 'jao-open-doc-url))

;;;; doc-view
(use-package doc-view
  :init
  (setq doc-view-cache-directory "~/.emacs.d/cache/docview"
        doc-view-resolution 110
        doc-view-continuous t
        doc-view-conversion-refresh-interval 1
        doc-view-mupdf-use-svg t)
  :hook ((doc-view-mode . jao-doc-session-mark))
  :bind (:map doc-view-mode-map
              ("j" . doc-view-next-line-or-next-page)
              ("J" . doc-view-scroll-up-or-next-page)
              ("k" . doc-view-previous-line-or-previous-page)
              ("K" . doc-view-scroll-down-or-previous-page)
              ("z" . jao-open-with-zathura)))

(use-package jao-doc-session :demand t)

(use-package jao-doc-view
  :demand t
  :bind (:map doc-view-mode-map
              ("b" . jao-doc-view-back)
              ("B" . jao-doc-view-forward)
              ("S" . jao-doc-session-save)
              ("u" . jao-doc-view-visit-url)))

;;;; epub
(use-package nov
  :ensure t
  :after doc-view
  :init (setq nov-variable-pitch t
              nov-text-width nil)
  :config
  (add-to-list 'auto-mode-alist '("\\.epub\\'" . nov-mode))
  (defun jao-nov-register-session ()
    (jao-doc-session-mark nov-file-name))
  (add-hook 'nov-mode-hook #'jao-nov-register-session))

;;;; transient
(defun jao-org-pdf-goto-org-linking ()
  (interactive)
  (jao-org-pdf-goto-org 4))

(jao-transient-major-mode doc-view
  ["Notes"
   ("o" "notes file" jao-org-pdf-goto-org)
   ("O" "notes file, linking" jao-org-pdf-goto-org-linking)]
  ["Navigation"
   ("b" "back jump" jao-doc-view-back)
   ("B" "forward jump" jao-doc-view-back)
   ("u" "visit URL" jao-doc-view-visit-url)]
  ["Slices"
   ("cb" "bounding box" doc-view-set-slice-from-bounding-box)
   ("cm" "using mouse" doc-view-set-slice-using-mouse)]
  ["Session"
   ("s" "load session" jao-afio-open-pdf-session)
   ("S" "save session" jao-doc-session-save)
   ("d" "visit cache directory" doc-view-dired-cache)]
  ["External viewers"
   ("z" "open with zathura" jao-open-with-zathura)])

(with-eval-after-load "pdf-view"
  (jao-transient-major-mode pdf-view
    ["Notes"
     ("o" "notes file" jao-org-pdf-goto-org)
     ("O" "notes file, linking" jao-org-pdf-goto-org-linking)]
    ["Navigation"
     ("b" "back jump" pdf-history-backward)
     ("f" "forward jump" pdf-history-forward)]
    ["Session"
     ("s" "load session" jao-afio-open-pdf-session)
     ("S" "save session" jao-doc-session-save)]
    ["External viewers"
     ("z" "open with zathura" jao-open-with-zathura)]))

;; (transient-get-suffix 'jao-transient-pdf-view '(0 -1))

;;; Email
(require 'jao-custom-email)

;;; Shells and terms
;;;; shell modes
(setq sh-basic-offset 2)
;; translates ANSI colors into text-properties, for eshell
(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
(defvar jao-use-vterm nil)
(defvar jao-use-eat nil)

(add-to-list 'display-buffer-alist
             '("\\*Async Shell Command\\*" (display-buffer-no-window)))

;;;; eat
(use-package eat
  :ensure t
  :commands jao-exec-in-term
  :init (setq jao-use-eat t
              eat-kill-buffer-on-exit t
              eat-enable-yank-to-terminal t)
  :hook ((eshell-mode . eat-eshell-mode)
         (eshell-mode . eat-eshell-visual-command-mode))
  :diminish ((eat-eshell-mode . "")))

;;;; term
(defvar-local jao-term--cmd nil)

(defun jao-term--find (cmd)
  (seq-find (lambda (b)
              (with-current-buffer b
                (and (derived-mode-p 'eat-mode 'term-mode 'vterm-mode)
                     (string= (or jao-term--cmd "") cmd))))
            (buffer-list)))

(defun jao-exec-in-term (cmd &optional name)
  (interactive "SCommand")
  (require 'eat nil t)
  (cond ((and jao-use-vterm (fboundp 'jao-exec-in-vterm))
         (jao-exec-in-vterm cmd name))
        (jao-use-eat (let ((eat-term-name "xterm-256color"))
                       (with-current-buffer (eat cmd t)
                         (setq-local jao-term--cmd cmd))))
        (t (ansi-term "bash" name)
           (set-process-sentinel (get-buffer-process (current-buffer))
                                 (lambda (process event)
                                   (when (string= event "finished\n")
                                     (kill-buffer (process-buffer process)))))
           (setq-local jao-term--cmd cmd)
           (term-send-string nil (concat cmd " ; exit\n")))))

(defmacro jao-def-exec-in-term (name cmd &rest prelude)
  `(defun ,(intern (format "jao-term-%s" name)) (&optional term)
     (interactive "P")
     ,@prelude
     (let ((jao-use-vterm (if term (not jao-use-vterm) jao-use-vterm)))
       (if-let ((b (jao-term--find ,cmd)))
           (pop-to-buffer b)
         (jao-exec-in-term ,cmd ,(format "*%s*" name))))))

;;;; eshell
;;;;; basic custom
(use-package eshell
  :init
  (setq eshell-directory-name "~/.emacs.d/eshell"
        eshell-hist-ignoredups 'erase
        eshell-history-size 1000000
        eshell-error-if-no-glob nil)

  (defun jao-eshell--outline ()
    (setq-local outline-regexp eshell-prompt-regexp))

  :config (setq eshell-prompt-repeat-map nil)

  :hook (eshell-mode . jao-eshell--outline))

;;;;; colors
(autoload 'ansi-color-apply "ansi-color")
;; (add-hook 'eshell-preoutput-filter-functions 'ansi-color-filter-apply)
(add-hook 'eshell-preoutput-filter-functions 'ansi-color-apply)

(use-package eshell-syntax-highlighting
  :after esh-mode
  :ensure t
  :config
  ;; Enable in all Eshell buffers.
  (eshell-syntax-highlighting-global-mode +1))

;;;;; visual commands
(require 'em-term)

(dolist (c '("editor" "more" "wget" "dict" "vim" "links" "w3m" "guile"
             "zmore" "pager" "aptitude" "su" "htop" "top"
             "screen" "whizzml" "iex" "spt"))
  (add-to-list 'eshell-visual-commands c))

(setq eshell-visual-subcommands '(("git" "log" "diff" "show")
                                  ("sudo" "vim")
                                  ("rebar3" "shell"))
      eshell-destroy-buffer-when-process-dies nil
      eshell-escape-control-x t)

;;;;; bol
(defun jao-eshell-maybe-bol ()
  (interactive)
  (let ((p (point)))
    (eshell-bol)
    (if (= p (point))
        (beginning-of-line))))

;;;;; prompt
;; tracking git repos
(defun jao-eshell--git-dirty ()
  (shell-command-to-string "git diff-index --quiet HEAD -- || echo -n '*'"))

(use-package git-ps1-mode
  :ensure t
  :init (setq git-ps1-mode-showupstream "1"
              git-ps1-mode-showdirtystate "1"))

(defun jao-eshell--git-info ()
  (if (fboundp 'git-ps1-mode-get-current)
      (git-ps1-mode-get-current)
    (let ((desc (shell-command-to-string "git branch --no-color")))
      (when (string-match "^* \\(\\<.+\\>\\)" desc)
        (format "%s%s" (match-string 1 desc) (jao-eshell--git-dirty))))))

(defun jao-eshell--git-current-branch (suffix)
  (let ((desc (or (jao-eshell--git-info) "")))
    (cond ((and (string-empty-p desc) suffix) (format " (%s)" suffix))
          ((string-empty-p (or suffix "")) (format " (%s)" desc))
          (t (format " (%s %s)" desc suffix)))))

(defun jao-eshell--virtualenv ()
  (let ((venv (getenv "VIRTUAL_ENV")))
    (when (and venv (string-match ".*/\\([^/]+\\)/$" venv))
      (match-string-no-properties 1 venv))))

(defun jao-eshell-prompt-function ()
  (let* ((venv (jao-eshell--virtualenv))
         (venv (if venv (format "%s" venv) "")))
    (concat (abbreviate-file-name (eshell/pwd))
            (jao-eshell--git-current-branch venv)
            (if (= (user-uid) 0) " # " " $ "))))

(setq eshell-prompt-function 'jao-eshell-prompt-function)

;;;;; in-term
(defun eshell/in-term (prog &rest args)
  (switch-to-buffer
   (apply #'make-term (format "in-term %s %s" prog args) prog nil args))
  (term-mode)
  (term-char-mode))

;;;;; dir navigation
(use-package eshell-up
  :ensure t
  :config (setq eshell-up-print-parent-dir t))

(use-package eshell-autojump :ensure t)

;;;;; completion
(defun jao-eshell-completion-capf ()
  (let* ((b (save-excursion (eshell-bol) (point)))
         (c (bash-completion-dynamic-complete-nocomint b (point) t)))
    (when (and c (listp c))
      (append c '(:exclusive no)))))

(defun jao-eshell--set-up-completion ()
  (setq-local completion-styles '(basic partial-completion)
              completion-at-point-functions
              '(jao-eshell-completion-capf
                pcomplete-completions-at-point t)))

(use-package bash-completion
  :ensure t
  :hook (eshell-mode . jao-eshell--set-up-completion))

;;;;; toggle
(use-package jao-eshell-here
  :demand t
  :config (jao-define-attached-buffer "^\\*eshell" 0.5)
  :bind (("<f1>" . jao-eshell-here-toggle)
         ("C-<f1>" . jao-eshell-here-toggle-new)))

;;;;; workarounds
;; at some point, bash completion started insertig the TAB
;; after the commands ends
(defun jao-eshell--clean-prompt ()
  (eshell-bol)
  (ignore-errors (kill-line)))

(add-hook 'eshell-after-prompt-hook 'jao-eshell--clean-prompt)

;;;;; keybindings
(defun jao-eshell--kbds ()
  (define-key eshell-mode-map "\C-a" 'jao-eshell-maybe-bol)
  (define-key eshell-mode-map "\C-ci" 'consult-outline))

(jao-eshell--kbds)

;;; Version control and CI
;;;; vc options
(setq vc-follow-symlinks t)
(setq auto-revert-check-vc-info nil)

;;;; diff fringe indicators (diff-hl)
(use-package diff-hl
  :ensure t
  :custom ((diff-hl-draw-borders nil)
           (diff-hl-side 'right)
           (diff-hl-margin-symbols-alist
            '((insert . "█")
              (delete . "█")
              (change . "█")
              (unknown . "█")
              (ignored . "█"))))
  :config
  (map-keymap (lambda (_k cmd)
                (put cmd 'repeat-map 'diff-hl-command-map))
              diff-hl-command-map)
  (add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh))

(global-diff-hl-mode 1)
(unless (display-graphic-p) (diff-hl-margin-mode 1))

;;;; magit/forge
(use-package magit
  :ensure t
  :commands magit-status
  :init
  (setq magit-status-initial-section nil
        magit-define-global-key-bindings nil
        magit-completing-read-function 'magit-builtin-completing-read
        magit-display-buffer-function
        'magit-display-buffer-fullcolumn-most-v1
        magit-delete-by-moving-to-trash nil
        magit-last-seen-setup-instructions "1.4.0"
        magit-log-edit-confirm-cancellation t
        magit-omit-untracked-dir-contents t
        magit-process-connection-type nil
        magit-push-always-verify nil
        magit-repository-directories
        '(("/home/jao/usr/bigml" . 2)
          ("/home/jao/usr/jao" . 3)
          ("/usr/local/src" . 1))
        magit-save-repository-buffers 'dontask
        magit-section-visibility-indicator '("…" . t)
        magit-status-buffer-switch-function 'switch-to-buffer
        magit-status-show-hashes-in-headers t))

;;;; forge
(use-package forge
  :ensure t
  :after magit
  :init
  (setq forge-topic-list-limit (cons 100 -1)
        forge-pull-notifications nil)
  :config
  (use-package embark-vc :ensure t)
  :bind ((:map forge-topic-mode-map ("M-w" . copy-region-as-kill))))


;;;; code reviews
(use-package code-review
  :disabled t
  :ensure t
  :after forge
  :bind (:map magit-status-mode-map
              ("C-c C-r" . code-review-forge-pr-at-point)))

;;;; other git packages
(use-package git-timemachine :ensure t)

(use-package consult-git-log-grep
  :ensure t
  :custom (consult-git-log-grep-open-function #'magit-show-commit)
  :bind (("C-c K" . consult-git-grep)))

;; git config --local git-link.remote / git-link.branch
(use-package git-link :ensure t)
(use-package git-modes :ensure t)

;;;; jenkins
(use-package jenkins
  :ensure t
  :init
  ;; one also needs jenkins-api-token, jenkins-username and jenkins-url
  ;; optionally: jenkins-colwidth-id, jenkins-colwidth-last-status
  (setq jenkins-colwidth-name 35)
  :config
  (defun jao-jenkins-first-job (&rest _)
    (interactive)
    (goto-char (point-min))
    (when (re-search-forward "^- Job" nil t)
      (goto-char (match-beginning 0))))
  (add-hook 'jenkins-job-view-mode-hook #'jao-jenkins-first-job)
  (advice-add 'jenkins-job-render :after #'jao-jenkins-first-job)

  (defun jenkins-refresh-console-output ()
    (interactive)
    (let ((n (buffer-name)))
      (when (string-match "\\*jenkins-console-\\([^-]+\\)-\\(.+\\)\\*$" n)
        (jenkins-get-console-output (match-string 1 n) (match-string 2 n))
        (goto-char (point-max)))))

  :bind (:map jenkins-job-view-mode-map
              (("n" . next-line)
               ("p" . previous-line)
               ("f" . jao-jenkins-first-job)
               ("RET" . jenkins--show-console-output-from-job-screen))
              :map jenkins-console-output-mode-map
              (("n" . next-line)
               ("p" . previous-line)
               ("g" . jenkins-refresh-console-output))))

;;; Programming
;;;; automatic modes
(add-to-list 'auto-mode-alist '("\\.mix\\'" . hexl-mode))
(add-to-list 'auto-mode-alist '("\\.m4\\'" . m4-mode))
(add-to-list 'auto-mode-alist '("\\.am\\'" . makefile-mode))
(add-to-list 'auto-mode-alist '("\\.pl\\'\\|\\.pm\\'" . cperl-mode))

;;;; symbol overlay
(use-package symbol-overlay
  :ensure t
  :config
  (defun jao-symbol-reveal (&rest _)
    (when outline-minor-mode (outline-show-entry)))
  (advice-add 'symbol-overlay-basic-jump :after 'jao-symbol-reveal)

  (defun jao-symbol-put-and-next ()
    (interactive)
    (symbol-overlay-put)
    (symbol-overlay-jump-next))

  (defun jao-symbol-put-and-prev ()
    (interactive)
    (symbol-overlay-put)
    (symbol-overlay-jump-prev))

  :bind (:map prog-mode-map (("M-i" . symbol-overlay-put)
                             ("M-n" . jao-symbol-put-and-next)
                             ("M-p" . jao-symbol-put-and-prev)))
  :hook (prog-mode . symbol-overlay-mode)
  :diminish " ^")

;;;; eglot
(use-package eglot
  :bind (:map eglot-mode-map (("C-h ." . jao-eldoc-toggle))))

;;;; paredit and parens
(require 'paren)
(show-paren-mode t)
(setq show-paren-context-when-offscreen t
      show-paren-when-point-inside-paren nil)

(use-package paredit
  :ensure t
  :commands paredit-mode
  :hook ((pie-mode . paredit-mode)
         (scheme-mode . paredit-mode)
         (clojure-mode . paredit-mode)
         (emacs-lisp-mode . paredit-mode)
         ;; (eval-expression-minibuffer-setup . paredit-mode)
         (lisp-interaction-mode . disable-paredit-mode))
  :diminish ((paredit-mode . " þ")))

;;;; diff/ediff
(setq ediff-split-window-function 'split-window-horizontally)
(setq ediff-make-buffers-readonly-at-startup nil)
(setq ediff-window-setup-function 'ediff-setup-windows-plain)
(setq ediff-keep-variants nil)

;;;; compilation
;;;;; compilation mode options
(require 'compile)
(setq compilation-scroll-output t)
(setq compilation-error-regexp-alist
      (remove 'omake compilation-error-regexp-alist))
;; (add-hook 'compilation-mode-hook #'visual-line-mode)

;;;;; mode line (no "Compiling"!)
(require 'compile)
(diminish 'compilation-minor-mode " ‡")
(when (< emacs-major-version 27)
  (setcdr (assq 'compilation-in-progress minor-mode-alist) '(" ‡")))
(when (> emacs-major-version 26)
  (setcdr (assq 'compilation-in-progress mode-line-modes) '("‡ ")))

;;;;; colorizing compilation buffer
(setq compilation-message-face 'default)
(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)

;;;;; compilation commands
(use-package jao-compilation
  :commands jao-compilation-setup
  :bind (("C-c C" . compile)
         ("C-c c" . jao-compile)))
(jao-compilation-setup)

;;;;; next error
(setq next-error-find-buffer-function
      #'next-error-buffer-on-selected-frame
      next-error-verbose t)

;;;; flymake
(use-package flymake
  :ensure t
  :custom ((flymake-mode-line-format '(" " flymake-mode-line-counters)))
  :config
  (jao-define-attached-buffer "^\\*Flymake diagnostics .*\\*\\'")

  (transient-define-prefix jao-transient-flymake ()
    ["Flymake"
     ("d" "show diagnostics" flymake-show-buffer-diagnostics)
     ("i" "show diagnostic" flymake-show-diagnostic)
     ("n" "next error" flymake-goto-next-error)
     ("p" "previous error" flymake-goto-prev-error)
     ("c" "consult flymake" consult-flymake)])

  :bind (:map flymake-mode-map (("M-m" . jao-transient-flymake))))

;;;; workarounds
(setq c-type-finder-time-slot nil)

;;;; outline minor mode
(use-package outline
  :init (setq outline-minor-mode-use-buttons nil
              outline-minor-mode-use-margins nil
              outline-minor-mode-cycle t))

(defvar-local jao-outline-folded nil)
(dolist (v '(4 5 outline-show-only-headings))
  (add-to-list 'safe-local-variable-values `(outline-default-state . ,v)))

(defun jao-outline-minor-mode-hide-all (&optional arg)
  (interactive "P")
  (outline-hide-sublevels (if arg 5 4)))

(defun jao-outline-minor-expand-all ()
  (when jao-outline-minor-mode (outline-show-all)))

(defun jao-outline-minor-mode-toogle-fold (&optional arg)
  (interactive "P")
  (if (setq jao-outline-folded (not jao-outline-folded))
      (jao-outline-minor-mode-hide-all arg)
    (jao-outline-minor-expand-all)))

(use-package outline-minor-faces
  :ensure t
  :after outline)

(define-minor-mode jao-outline-minor-mode
  "Minor outline mode for programming languages"
  :lighter ""
  :keymap `((,(kbd "C-c C-n") . outline-next-visible-heading)
            (,(kbd "C-c C-p") . outline-previous-visible-heading)
            (,(kbd "C-c o") . consult-outline)
            (,(kbd "<f3>") . jao-outline-minor-mode-toogle-fold))
  (if jao-outline-minor-mode
      (progn (setq-local outline-level #'outline-level
                         outline-regexp (format "[%s]\\{3,\\} " comment-start))
             (outline-minor-mode 1)
             (outline-minor-faces-mode 1))
    (outline-minor-mode -1)
    (outline-minor-faces-mode -1)))

(add-hook 'find-function-after-hook #'jao-outline-minor-expand-all)

;;; Programming languages
;;;; Elisp
(add-hook 'emacs-lisp-mode-hook #'jao-outline-minor-mode)

(use-package edit-list :ensure t)
(use-package package-lint :ensure t)
;; (use-package tree-inspector :ensure t)

(defun elisp-disassemble (function)
  (interactive (list (function-called-at-point)))
  (disassemble function))

(defun elisp-pp (sexp)
  (with-output-to-temp-buffer "*Pp Eval Output*"
    (pp sexp)
    (with-current-buffer standard-output
      (emacs-lisp-mode))))

(defun elisp-macroexpand (form)
  (interactive (list (form-at-point 'sexp)))
  (elisp-pp (macroexpand form)))

(defun elisp-macroexpand-all (form)
  (interactive (list (form-at-point 'sexp)))
  (elisp-pp (macroexpand-all form)))

(defun elisp-find-definition (name)
  (interactive (list (thing-at-point 'symbol)))
  (cond (name
         (let ((symbol (intern-soft name))
               (search (lambda (fun sym)
                         (let* ((r (save-excursion (funcall fun sym)))
                                (buffer (car r))
                                (point (cdr r)))
                           (cond ((not point)
                                  (error "Found no definition for %s in %s"
                                         name buffer))
                                 (t
                                  (switch-to-buffer buffer)
                                  (goto-char point)
                                  (recenter 1)))))))
           (cond ((fboundp symbol)
                  (xref-push-marker-stack)
                  (funcall search 'find-function-noselect symbol))
                 ((boundp symbol)
                  (xref-push-marker-stack)
                  (funcall search 'find-variable-noselect symbol))
                 (t
                  (message "Symbol not bound: %S" symbol)))))
        (t (message "No symbol at point"))))


(defun elisp-bytecompile-and-load ()
  (interactive)
  (or buffer-file-name
      (error "The buffer must be saved in a file first"))
  (require 'bytecomp)
  ;; Recompile if file or buffer has changed since last compilation.
  (when  (and (buffer-modified-p)
              (y-or-n-p (format "save buffer %s first? " (buffer-name))))
    (save-buffer))
  (let ((filename (expand-file-name buffer-file-name)))
    (with-temp-buffer
      (byte-compile-file filename))))

(use-package elisp-mode
  :bind (:map emacs-lisp-mode-map
              (("C-c C-M" . emacs-lisp-macroexpand)
               ("C-c C-m" . elisp-macroexpand-all)
               ("C-c C-k" . elisp-bytecompile-and-load)
               ;; ("C-c C-p" . pp-eval-last-sexp)
               ("M-." . elisp-find-definition)
               ("M-," . pop-tag-mark)
               ("C-c <" . lc-show-package-summary))))

;;;; Clojure
(use-package clojure-mode
  :ensure t
  :config
  (defun jao-clojure--fix-things ()
    (setq-local completion-styles '(basic partial-completion emacs22))
    (eldoc-mode 1)
    (setq mode-name "λ"))
  :hook (clojure-mode . jao-clojure--fix-things))

(use-package cider
  :ensure t
  :commands cider-mode
  :init (setq cider-annotate-completion-candidates t
              cider-auto-select-error-buffer nil
              cider-auto-select-test-report-buffer nil
              cider-eldoc-display-for-symbol-at-point t
              cider-eldoc-ns-function #'identity ;; #'cider-last-ns-segment
              cider-enrich-classpath nil
              cider-lein-parameters "repl :headless :host localhost"
              cider-mode-line  " ÷"
              cider-prompt-for-symbol nil
              cider-repl-history-file
              (expand-file-name "~/.emacs.d/cache/cider.history")
              cider-repl-pop-to-buffer-on-connect nil
              cider-repl-use-pretty-printing t
              cider-show-error-buffer 'except-in-repl
              cider-test-show-report-on-success nil
              cider-use-fringe-indicators nil
              cider-use-overlays nil
              clojure-docstring-fill-column 72
              nrepl-prompt-to-kill-server-buffer-on-quit nil)
  :bind (("<f3>" . cider-selector)))

(with-eval-after-load "cider-test"
  (advice-add 'cider-scale-background-color :override
              (lambda () (frame-parameter nil 'background-color)))
  (setq cider-test-items-background-color
        (frame-parameter nil 'background-color)))

(use-package cider-macroexpansion
  :after cider
  :diminish " µ")

;;;; Geiser
(defun jao-org--set-geiser-impl () (setq-local geiser-repl--impl 'guile))
(add-hook 'org-mode-hook #'jao-org--set-geiser-impl)

(jao-load-path "geiser")
(use-package geiser
  :init
  (setq geiser-repl-history-filename "~/.emacs.d/cache/geiser-history"
        geiser-repl-startup-time 20000
        geiser-debug-auto-display-images t
        geiser-log-verbose t)
  :config
  (dolist (m '(geiser-repl-mode geiser-doc-mode geiser-debug-mode))
    (jao-define-attached-buffer `(major-mode . ,m) 0.4)))

(jao-load-path "geiser-guile")
(use-package geiser-guile)

(jao-load-path "geiser-chez")
(use-package geiser-chez)

;; (jao-load-path "geiser/mit")
;; (use-package geiser-mit)

;; (jao-load-path "geiser/chicken")
;; (use-package geiser-chicken)

;; (jao-load-path "geiser/chibi")
;; (use-package geiser-chibi)

;; (jao-load-path "geiser/gambit")
;; (use-package geiser-gambit)

;; (jao-load-path "geiser/gauche")
;; (use-package geiser-gauche)

;;;; Haskell
;;;;; packages
;; (jao-load-path "haskell-mode")

(use-package haskell-mode
  :ensure t
  :custom
  ((inferior-haskell-find-project-root t)
   (haskell-check-remember-last-command-p nil)
   (haskell-completing-read-function 'completing-read)
   (haskell-font-lock-symbols nil)
   (haskell-hoogle-command "hoogle")
   (haskell-interactive-popup-errors t)
   (haskell-process-auto-import-loaded-modules t)
   (haskell-process-log t)
   (haskell-process-suggest-remove-import-lines t)
   (haskell-process-suggest-hoogle-imports t)
   (haskell-process-type 'cabal-repl)
   (haskell-process-use-presentation-mode t)
   (haskell-stylish-on-save nil)
   (haskell-tags-on-save t))

  :init
  ;; For use with M-x align
  (require 'align)
  (add-to-list 'align-rules-list
               '(haskell-types
                 (regexp . "\\(\\s-+\\)\\(::\\|∷\\)\\s-+")
                 (modes quote (haskell-mode haskell-literate-mode))))
  (add-to-list 'align-rules-list
               '(haskell-assignment
                 (regexp . "\\(\\s-+\\)=\\s-+")
                 (modes quote (haskell-mode haskell-literate-mode))))
  (add-to-list 'align-rules-list
               '(haskell-arrows
                 (regexp . "\\(\\s-+\\)\\(->\\|→\\)\\s-+")
                 (modes quote (haskell-mode haskell-literate-mode))))
  (add-to-list 'align-rules-list
               '(haskell-left-arrows
                 (regexp . "\\(\\s-+\\)\\(<-\\|←\\)\\s-+")
                 (modes quote (haskell-mode haskell-literate-mode))))

  :config
  (defun jao-haskell-hoogle (no-info)
    (interactive "P")
    (haskell-hoogle (format "%s" (haskell-ident-at-point)) (not no-info)))

  (put 'haskell-process-args-cabal-repl
       'safe-local-variable
       (apply-partially #'seq-every-p #'stringp))

  (defun jao-haskell-eldoc (cb)
    (let ((msg (or (haskell-doc-current-info--interaction t)
                   (haskell-doc-sym-doc (haskell-ident-at-point))
                   "")))
      (funcall cb (replace-regexp-in-string "[\n ]+" " " msg))))

  (setq tags-revert-without-query t)

  (defun jao-haskell-mode ()
    (require 'haskell-doc)
    (setq-local eldoc-documentation-function 'eldoc-documentation-default
                eldoc-documentation-functions '(jao-haskell-eldoc))
    (eldoc-mode))

  (dolist (h '(jao-haskell-mode
               haskell-decl-scan-mode
               haskell-indentation-mode
               interactive-haskell-mode))
    (add-hook 'haskell-mode-hook h))

  (add-hook 'haskell-presentation-mode-hook (lambda () (whitespace-mode -1)))

  :bind (:map haskell-mode-map
              (("C-c C-d" . jao-haskell-hoogle)
               ("C-c C-s" . haskell-session-change-target)
               ("C-c h" . haskell-hoogle)
               ("C-c t" . haskell-doc-show-type)
               ("C-c C-e" . haskell-command-insert-language-pragma)
               ("C-M-n" . flymake-goto-next-error)
               ("C-M-p" . flymake-goto-prev-error)
               ("<f3>" . haskell-session-kill))))

(use-package hlint-refactor
  :ensure t
  :after haskell-mode
  :hook ((haskell-mode . hlint-refactor-mode))
  :bind (:map haskell-mode-map (("C-M-h" . 'hlint-refactor-refactor-at-point)
                                ("C-M-S-h" . 'hlint-refactor-refactor-buffer)))
  :diminish)

(use-package flymake-hlint
  :ensure t
  :after haskell-mode
  :hook ((haskell-mode . flymake-hlint-load)))


(use-package consult-hoogle
  :ensure t)

(require 'haskell)

(diminish 'interactive-haskell-mode " λ")
(diminish 'haskell-doc-mode)
(diminish 'haskell-decl-scan-mode)

(jao-define-attached-buffer "\\*hoogle\\*.*")
(jao-define-attached-buffer '(major-mode . haskell-interactive-mode) 0.33)
(jao-define-attached-buffer '(major-mode . haskell-presentation-mode) 0.25)

;;;;; transient
(jao-transient-major-mode haskell
  ["Imports"
   ("in" "Navigate imports" haskell-navigate-imports)
   ("if" "Format imports" haskell-mode-format-imports)
   ("is" "Sort imports" haskell-sort-imports)
   ("ia" "Align imports" haskell-align-imports)]
  ["Session"
   ("s" "Change the session's target" haskell-session-change-target)]
  ["Code"
   ("e" "insert language pragma" haskell-command-insert-language-pragma)
   ("v" "visit cabal file" haskell-cabal-visit-file)
   ("h" "hoogle" jao-haskell-hoogle)
   ("t" "show type" haskell-doc-show-type)]
  ["Flymake"
   ("n" "next error" flymake-goto-next-error)
   ("p" "previous error" flymake-goto-prev-error)])

;;;; Pie
(jao-load-path "pie")
(use-package pie
  :demand t
  :commands (pie-mode))

;;;; Prolog
;; (use-package ediprolog :ensure t)

(use-package prolog
  :ensure t
  :commands (run-prolog prolog-mode mercury-mode)
  :init (progn
          (setq prolog-system 'swi)
          (add-to-list 'auto-mode-alist '("\\.pl$" . prolog-mode))
          (setq prolog-consult-string '((t "[%f].")))
          (setq prolog-program-name
                '(((getenv "EPROLOG") (eval (getenv "EPROLOG")))
                  (eclipse "eclipse")
                  (mercury nil)
                  (sicstus "sicstus")
                  (swi "swipl")
                  (t "prolog")))))

;;;; Python
(use-package virtualenvwrapper
  :ensure t
  :config
  (venv-initialize-eshell)
  (jao-compilation-env "VIRTUAL_ENV"))

;;; Text/data formats
;;;; json
(use-package json-mode :ensure t)
;;;; yaml
(use-package yaml-mode :disabled t :ensure t)

;;; Graphics
;;;; images
(setq image-use-external-converter t
      image-cache-eviction-delay 120)
(setq widget-image-enable nil)

;;;; gnuplot
(use-package gnuplot
  :disabled t
  :ensure t
  :commands (gnuplot-mode gnuplot-make-buffer)
  :init (add-to-list 'auto-mode-alist '("\\.gp$" . gnuplot-mode)))

;;; Network
;;;; nm applet
(jao-shell-def-exec jao-nm-applet "nm-applet")

(defun jao-toggle-nm-applet ()
  (interactive)
  (or (jao-shell-kill-p "nm-applet") (jao-nm-applet)))

;;;; bluetooth
(use-package bluetooth :ensure t)

;;;; vpn
(use-package jao-mullvad :demand t)

;;;; ssh
(use-package tramp)
(defun jao-tramp-hosts ()
  (seq-uniq
   (mapcan (lambda (x)
             (remove nil (mapcar 'cadr (apply (car x) (cdr x)))))
           (tramp-get-completion-function "ssh"))
   #'string=))

(defun jao-ssh ()
  (interactive)
  (let ((h (completing-read "Host: " (jao-tramp-hosts))))
    (jao-afio-goto-scratch)
    (jao-exec-in-term (format "ssh %s" h) (format "*ssh %s*" h))))

;;; Chats
;;;; circe
(defvar jao-libera-channels ())
(defvar jao-oftc-channels ())
(defvar jao-bitlbee-channels ())
(defvar jao-slack-channels ())

(use-package circe
  :ensure t
  :bind (:map circe-channel-mode-map
              (("C-c C-a" . lui-track-jump-to-indicator)))
  :init
  (setq circe-chat-buffer-name "{target}"
        circe-default-realname "https://jao.io"
        circe-default-part-message ""
        circe-default-quit-message ""
        circe-ignore-list nil
        circe-server-coding-system '(undecided . undecided)
        circe-server-killed-confirmation 'ask-and-kill-all
        circe-server-auto-join-default-type :after-auth
        circe-format-say "({nick}) {body}"
        circe-format-self-say "(jao) {body}"
        circe-new-buffer-behavior 'ignore
        circe-new-buffer-behavior-ignore-auto-joins t
        circe-nickserv-ghost-style 'after-auth
        circe-prompt-string ": "
        circe-completion-suffix ", "
        circe-reduce-lurker-spam t

        circe-lagmon-mode-line-format-string "" ;;  "%.0f "
        circe-lagmon-mode-line-unknown-lag-string "" ;; "? "
        circe-lagmon-timer-tick 120
        circe-lagmon-reconnect-interval 180

        lui-max-buffer-size 30000
        lui-fill-column 80
        lui-time-stamp-position 'right
        lui-time-stamp-format "%H:%M"
        lui-flyspell-p nil

        lui-track-indicator (if window-system 'fringe 'bar)
        lui-track-behavior 'before-tracking-next-buffer)
  :config

  (defsubst jao-circe-nick-no () (length (circe-channel-nicks)))

  (define-minor-mode jao-circe-user-number-mode ""
    :lighter (:eval (format " [%s]" (jao-circe-nick-no))))

  (defun jao-circe-channel-hook ()
    ;; (setq header-line-format
    ;;       '(" %b" (:eval (format " - %s nicks" (jao-circe-nick-no)))))
    (jao-circe-user-number-mode 1))

  (add-hook 'circe-channel-mode-hook #'jao-circe-channel-hook)

  (defun circe-command-RECOVER (&rest _ignore)
    "Recover nick"
    (jao-with-auth "freenode" u p
      (circe-command-MSG "nickserv" (format "IDENTIFY %s %s" u p))
      (circe-command-MSG "nickserv" (format "GHOST %s" u))
      (circe-command-MSG "nickserv" (format "RELEASE %s" u))
      (circe-command-NICK u)))

  (defun circe-command-NNICKS (&rest _)
    "Echo number of nicks"
    (circe-display-server-message
     (format "%d nicks in this channel" (jao-circe-nick-no))))

  (defun circe-command-SENDFILE (line)
    "/sendfile for localslackirc"
    (circe-command-QUOTE (format "sendfile %s" line)))

  (advice-add 'circe-command-NAMES :after #'circe-command-NNICKS)

  (setq circe-network-options
        (list (jao-with-auth "libera" u p
                (list "Libera Chat" :nick u :channels jao-libera-channels
                      :tls t :sasl-username u :sasl-password p))
              (jao-with-auth "oftc" u p
                (list "OFTC"
                      :nick u :channels jao-oftc-channels :nickserv-password p
                      :tls t :sasl-username u :sasl-password p))
              (jao-with-auth "bitlbee" u p
                (list "Bitlbee" :host "127.0.0.1" :nick u
                      :channels jao-bitlbee-channels :lagmon-disabled t
                      :nickserv-password u :user p))
              (list "localslack" :host "127.0.0.1" :nick "jao"
                    :channels jao-slack-channels :port 9007
                    :lagmon-disabled t)
              (list "recoveryou" :host "127.0.0.1" :nick "jao"
                    :port 9008 :lagmon-disabled t)))

  (jao-shorten-modes 'circe-channel-mode
                     'circe-server-mode
                     'circe-query-mode)

  (enable-circe-display-images)
  (enable-lui-track)
  (circe-lagmon-mode))

;;;; telegram
(use-package telega
  :ensure t
  :custom
  (telega-use-tracking-for '(unmuted) ;; '(or unmuted mention)
   telega-rainbow-color-custom-for nil
   telega-msg-rainbow-title nil
   telega-sticker-set-download t
   telega-symbol-checkmark "·"
   telega-symbol-heavy-checkmark "×"
   telega-symbol-verified "*")
  :config
  (define-key global-map (kbd "C-c C-t") telega-prefix-map)
  (setq telega-chat-show-avatars nil
        telega-chat-prompt-insexp '(telega-ins "> ")
        telega-completing-read-function #'completing-read
        telega-root-show-avatars nil
        telega-emoji-use-images nil
        telega-temp-dir "/tmp/telega"
        telega-symbol-horizontal-bar
        (propertize "-" 'face 'jao-themes-f00)
        telega-symbol-vertical-bar
        (propertize "| " 'face 'jao-themes-dimm)
        telega-mode-line-string-format
        '(:eval (telega-mode-line-unread-unmuted))
        telega-use-images (display-graphic-p)
        telega-open-file-function #'jao--see
        telega-open-message-as-file
        (unless (display-graphic-p) '(photo video animation)))
  (with-eval-after-load "tracking"
    (jao-shorten-modes 'telega-chat-mode)
    (jao-tracking-faces 'telega-tracking))
  (telega-mode-line-mode 1))

(defun jao-telega ()
  (interactive)
  (jao-tracking-go-to-chats)
  (if (get-buffer telega-root-buffer-name)
      (pop-to-buffer telega-root-buffer-name)
    (telega)))

;;;; ement
(use-package ement
  :disabled t
  :ensure t
  :init (setq ement-save-sessions t
              ement-sessions-file (locate-user-emacs-file "cache/ement.el")
              ement-room-avatars nil
              ement-notify-dbus-p nil
              ement-room-left-margin-width 0
              ement-room-right-margin-width 11
              ement-room-timestamp-format "%H:%M"
              ement-room-timestamp-header-format "­­--------")

  :custom ((ement-room-message-format-spec "(%S) %B%r%R %t"))

  :config
  (defun jao-ement-track (event room session)
    (when (ement-notify--room-unread-p event room session)
      (when-let ((n (ement-room--buffer-name room))
                 (b (get-buffer n)))
        (tracking-add-buffer b))))

  (add-hook 'ement-event-hook #'jao-ement-track)
  (jao-shorten-modes 'ement-room-mode)
  (jao-tracking-cleaner "^\\*Ement Room: \\(.+\\)\\*" "@\\1"))

;;;; mastodon
(use-package mastodon
  :ensure t
  :init
  (setq mastodon-instance-url "https://fosstodon.org"
        mastodon-active-user "jao@gnu.org"
        mastodon-tl-position-after-update nil
        mastodon-toot-display-orig-in-reply-buffer t)
  :config
  ;; (defun jao-mastodon--setup ()
  ;;   (setq-local scroll-margin 12))

  ;; (add-hook 'mastodon-mode-hook #'jao-mastodon--setup)
  (with-eval-after-load "ewww"
    (define-key eww-mode-map (kbd "T") #'jao-mastodon-toot-url)))

(defun jao-mastodon-toot-url ()
  (interactive)
  (when-let (url (jao-url-around-point t))
    (jao-tracking-go-to-chats)
    (mastodon-toot--compose-buffer nil nil nil url)))

(defun jao-mastodon ()
  (interactive)
  (jao-afio-goto-chats)
  (mastodon))

;; https://0x0.st/XJ14.txt
(jao-transient-major-mode mastodon
 ["Timelines"
   ("H" "home"            mastodon-tl--get-home-timeline)
   ("L" "local"           mastodon-tl--get-local-timeline)
   ("F" "federated"       mastodon-tl--get-federated-timeline)
   ("K" "bookmarks"       mastodon-profile--view-bookmarks)
   ("V" "favorites"       mastodon-profile--view-favourites)
   ("'" "followed tags"   mastodon-tl--followed-tags-timeline)
   ("@" "mentions"        mastodon-notifications--get-mentions)
   ("N" "notifications"   mastodon-notifications-get)
   ("\\" "of remote host" mastodon-tl--get-remote-local-timeline)]

  ;; u                    mastodon-tl--update

  ["Search"
   ("s" "search"          mastodon-search--query)
   ("#" "tagged"          mastodon-tl--get-tag-timeline)
   ("\"" "followed tags"  mastodon-tl--list-followed-tags)
   ("I" "filter"          mastodon-views--view-filters)
   ("X" "lists"           mastodon-views--view-lists)]

  ["Toots"
   ("n" "next"            mastodon-tl--goto-next-item :transient t)
   ("p" "prev"            mastodon-tl--goto-prev-item :transient t)
   ("c" "spoiler"         mastodon-tl--toggle-spoiler-text-in-toot :transient t)
   ("T" "thread"          mastodon-tl--thread)
   ("b" "(un)boost"       mastodon-toot--toggle-boost :transient t)
   ("f" "(un)fav"         mastodon-toot--toggle-favourite :transient t)
   ("i" "(un)pin"         mastodon-toot--pin-toot-toggle :transient t)
   ("k" "(un)bookmark"    mastodon-toot--toggle-bookmark :transient t)
   ("v" "vote"            mastodon-tl--poll-vote)]

  ;; Z                    mastodon-tl--report-to-mods
  ;; o                    mastodon-toot--open-toot-url

  ["Own Toots"
   ("r" "replay"          mastodon-toot--reply)
   ("t" "write"           mastodon-toot)
   ("e" "edit"            mastodon-toot--edit-toot-at-point)
   ("d" "delete"          mastodon-toot--delete-toot)
   ("D" "del & redraft"   mastodon-toot--delete-and-redraft-toot)
   ("E" "show edits"      mastodon-toot--view-toot-edits)]

  ;; S                    mastodon-views--view-scheduled-toots

  ["Users"
   ("W" "follow"          mastodon-tl--follow-user)
   ("R" "follow req"      mastodon-views--view-follow-requests)
   ("G" "suggestions"     mastodon-views--view-follow-suggestions)
   ("M" "mute user"       mastodon-tl--mute-user)
   ("B" "block user"      mastodon-tl--block-user)
   ("m" "message user"    mastodon-tl--dm-user)
   ""
   ("," "favouriters"     mastodon-toot--list-toot-favouriters)
   ("." "boosters"        mastodon-toot--list-toot-boosters)]

  ;; S-RET                mastodon-tl--unmute-user
  ;; C-S-b                mastodon-tl--unblock-user

  ["Profiles"
   ("A" "author"          mastodon-profile--get-toot-author)
   ("P" "any user"        mastodon-profile--show-user)
   ("O" "own"             mastodon-profile--my-profile)
   ("U" "update own"      mastodon-profile--update-user-profile-note)]

  ["Misc"
   ("C" "copy URL"        mastodon-toot--copy-toot-url)
   ("?" "help"            describe-mode)
   ("q" "quit"            transient-quit-one)])

;;;; startup
(defun jao-chats (&optional p)
  (interactive "P")
  (when (or p (y-or-n-p "Connect to telegram? "))
    (telega))
  (when (and (fboundp 'ement-connect) (or p (y-or-n-p "Connect to matrix? ")))
    (unless (get-buffer "*Ement Rooms*")
      (jao-with-auth "matrix.org" u p (ement-connect :user-id u :password p))))
  (when (and (fboundp 'mastodon) (or p (y-or-n-p "Connect to mastodon? ")))
    (mastodon))
  (when (or p (y-or-n-p "Connect to libera? "))
    (unless (get-buffer "irc.libera.chat:6697")
      (circe "Libera Chat")))
  (when (or p (y-or-n-p "Connect to localslack? "))
    (unless (get-buffer "127.0.0.1:9007")
      (circe "localslack")))
  (when (or p (y-or-n-p "Connect to recoveryou? "))
    (unless (get-buffer "127.0.0.1:9008")
      (circe "recoveryou"))))

(defun jao-all-chats ()
  (interactive)
  (when jao-tracking-use-scratch
    (jao-afio-goto-chats)
    (delete-other-windows))
  (jao-chats t))

(defun jao-chats-telega ()
  (interactive)
  (jao-buffer-same-mode '(telega-root-mode telega-chat-mode)))

(defun jao-chats-slack ()
  (interactive)
  (jao-buffer-same-mode 'slack-message-buffer-mode))

(defun jao-chats-irc ()
  (interactive)
  (jao-buffer-same-mode '(circe-channel-mode circe-query-mode erc-mode)))

;;;; consult narrowing
(defvar jao-chat-buffer-source
  (list :name "chats"
        :category 'buffer
        :action (lambda (b) (jao-afio-pop-to-buffer 0 b))
        :hidden t
        :narrow (cons ?c "chats")
        :items (jao-consult--mode-buffers 'erc-mode
                                          'circe-channel-mode
                                          'circe-query-mode
                                          'signel-chat-mode
                                          'slack-message-buffer-mode
                                          'slack-thread-message-buffer-mode
                                          'telega-root-mode
                                          'telega-chat-mode
                                          'ement-room-mode
                                          'ement-room-list-mode)))
(with-eval-after-load "consult"
  (jao-consult-add-buffer-source 'jao-chat-buffer-source))
;;; Multimedia
;;;; video
(use-package ready-player :ensure t)
(ready-player-mode 1)
;;;; mixer
(defun jao-mixer-get-level (&optional dev nomsg)
  (interactive)
  (let* ((dev (or dev "Master"))
         (s (shell-command-to-string (format "amixer sget %s" dev)))
         (s (car (last (split-string s "\n" t)))))
    (when (string-match ".*Front .*\\[\\([0-9]+\\)%\\] .*" s)
      (let ((level (match-string 1 s)))
        (unless nomsg (message "%s level: %s%%" dev level))
        (string-to-number level)))))

(defun jao-mixer-set (dev v)
  (jao-shell-exec* t "amixer" "sset" dev v)
  (jao-mixer-get-level dev))

(defun jao-mixer-master-toggle ()
  (interactive)
  (jao-mixer-set "Master" "toggle"))

(defun jao-mixer-master-up ()
  (interactive)
  (jao-mixer-set "Master" "10%+"))

(defun jao-mixer-master-down ()
  (interactive)
  (jao-mixer-set "Master" "10%-"))

(defun jao-mixer-capture-up ()
  (interactive)
  (jao-mixer-set "Capture" "10%+"))

(defun jao-mixer-capture-down ()
  (interactive)
  (jao-mixer-set "Capture" "10%-"))

(jao-shell-def-exec jao-audio-applet "pasystray")

(defun jao-toggle-audio-applet ()
  (interactive)
  (or (jao-shell-kill-p "paystray") (jao-audio-applet)))

(global-set-key (kbd "<f4>") #'jao-toggle-audio-applet)

;;;; streaming aliases
(defalias 'jao-streaming-list #'ignore)
(defalias 'jao-streaming-like #'ignore)
(defalias 'jao-streaming-dislike #'ignore)
(defalias 'jao-streaming-toggle-shuffle #'ignore)
(defalias 'jao-streaming-lyrics #'ignore)
(defalias 'jao-streaming-toggle #'ignore)
(defalias 'jao-streaming-next #'ignore)
(defalias 'jao-streaming-prev #'ignore)
(defalias 'jao-streaming-current #'ignore)
(defalias 'jao-streaming-seek #'ignore)
(defalias 'jao-streaming-seek-back #'ignore)
(defalias 'jao-streaming-volume #'ignore)
(defalias 'jao-streaming-volume-down #'ignore)

;;;; mpris
(defun jao-mpris-lyrics (&optional force)
  (interactive "P")
  (jao-show-lyrics force #'jao-mpris-artist-title))

(defun jao-mpris-mopidy-p () (string= "mopidy "jao-mpris-player))

(defun jao-mpc-mopidy-playlist ()
  (interactive)
  (jao-mpc-show-playlist jao-mopidy-port))

(use-package jao-mpris :demand t)

(defun jao-mpris-setup-aliases ()
  (setq espotify-play-uri-function #'espotify-play-uri-with-dbus)
  ;; (setq jao-mpris-player "mopidy")
  (defalias 'jao-streaming-list #'jao-mpc-mopidy-playlist)
  (defalias 'jao-streaming-lyrics #'jao-mpris-lyrics)
  (defalias 'jao-streaming-toggle #'jao-mpris-play-pause)
  (defalias 'jao-streaming-next #'jao-mpris-next)
  (defalias 'jao-streaming-prev #'jao-mpris-previous)
  (defalias 'jao-streaming-current #'jao-mpris-show-osd)
  (defalias 'jao-streaming-seek #'jao-mpris-seek)
  (defalias 'jao-streaming-seek-back #'jao-mpris-seek-back)
  (defalias 'jao-streaming-volume #'jao-mpris-vol)
  (defalias 'jao-streaming-volume-down #'jao-mpris-vol-down))

(jao-mpris-register "playerctld" :session 70)
;; (jao-mpris-register "mopidy" :session 70)

;;;; mpc
(use-package jao-mpc
  :demand t
  :commands jao-mpc-setup)

(defvar jao-mopidy-port 6669)
(defvar jao-mpc-last-port jao-mpc-port)

(defun jao-mpc-toggle-port ()
  (interactive)
  (setq jao-mpc-port
        (if (equal jao-mpc-port jao-mopidy-port) 6600 jao-mopidy-port)
        jao-mpc-last-port jao-mpc-port))

(defsubst jao-mpc-mopidy-p () (equal jao-mpc-last-port jao-mopidy-port))

(jao-mpc-setup jao-mopidy-port 70)

(defun jao-mpc-pport (&optional mop)
  (cond ((or mop (jao-mpc-playing-p jao-mopidy-port)) jao-mopidy-port)
        ((jao-mpc-playing-p) 6600)
        (t jao-mpc-last-port)))

(defmacro jao-defun-play (name &optional mpc-name)
  (let ((arg (gensym)))
    `(defun ,(intern (format "jao-player-%s" name)) (&optional ,arg)
       (interactive "P")
       (,(intern (format "jao-mpc-%s" (or mpc-name name)))
        (setq jao-mpc-last-port (jao-mpc-pport ,arg))))))

(jao-defun-play toggle)
(jao-defun-play next)
(jao-defun-play previous)
(jao-defun-play stop)
(jao-defun-play echo echo-current-times)
(jao-defun-play list show-playlist)
(jao-defun-play info lyrics-track-data)
(jao-defun-play browse show-albums)
(jao-defun-play select-album)

(defun jao-player-seek (delta) (jao-mpc-seek delta (jao-mpc-pport)))

(defalias 'jao-player-connect 'jao-mpc-connect)
(defalias 'jao-player-play 'jao-mpc-play)

;;;; spotify
(jao-load-path "espotify")

(use-package espotify
  :demand t
  :init (setq espotify-service-name "mopidy"))

(use-package consult-spotify :demand t)

(defalias 'jao-streaming-album #'consult-spotify-album)
(defalias 'jao-streaming-track #'consult-spotify-track)
(defalias 'jao-streaming-artist #'consult-spotify-artist)
(defalias 'jao-streaming-playlist #'consult-spotify-playlist)

(jao-def-exec-in-term "ncmpcpp" "ncmpcpp" (jao-afio-goto-scratch))

;;;; spt
(use-package jao-spt
  :demand t
  :config
  (defun jao-spt-setup-aliases ()
    (setq espotify-play-uri-function #'jao-spt-play-uri)
    (defalias 'jao-streaming-list #'jao-term-spt)
    (defalias 'jao-streaming-lyrics #'jao-spt-show-lyrics)
    (defalias 'jao-streaming-toggle #'jao-spt-toggle)
    (defalias 'jao-streaming-next #'jao-spt-next)
    (defalias 'jao-streaming-prev #'jao-spt-previous)
    (defalias 'jao-streaming-current #'jao-spt-echo-current)
    (defalias 'jao-streaming-seek #'jao-spt-seek)
    (defalias 'jao-streaming-seek-back #'jao-spt-seek-back)
    (defalias 'jao-streaming-volume #'jao-spt-vol)
    (defalias 'jao-streaming-volume-down #'jao-spt-vol-down)
    (defalias 'jao-streaming-like #'jao-spt-like)
    (defalias 'jao-streaming-dislike #'jao-spt-dislike)
    (defalias 'jao-streaming-toggle-shuffle #'jao-spt-toggle-shuffle)))

(jao-def-exec-in-term "spt" "spt" (jao-afio-goto-scratch))

(defvar jao-spt-on t)

(defun jao-streaming-toggle-player ()
  (interactive)
  (if jao-spt-on
      (progn (setq jao-mpris-player "playerctld")
             (require 'jao-mpris)
             (jao-mpris-setup-aliases))
    (jao-spt-setup-aliases)
    (setq jao-mpris-player "spt"))
  (setq jao-spt-on (not jao-spt-on))
  (message "%s activated " jao-mpris-player))

(jao-streaming-toggle-player)

;;;; music transients
(require 'jao-lyrics)
(setq jao-lyrics-info-function #'jao-player-info)

(defun jao-player-seek-10 () (interactive) (jao-player-seek 10))
(defun jao-player-seek--10 () (interactive) (jao-player-seek -10))

(defun jao-streaming-clear () (interactive) (jao-mpc-clear jao-mopidy-port))

(defun jao-streaming-echo-current ()
  (interactive)
  (jao-mpc-echo-current jao-mopidy-port))

(defun jao-streaming-show-playlist ()
  (interactive)
  (jao-mpc-show-playlist jao-mopidy-port))

(use-package jao-random-album :demand t)

(jao-def-exec-in-term "aptitude" "aptitude" (jao-afio-goto-scratch))

(defun jao-toggle-pasystray-applet ()
  (interactive)
  (or (jao-shell-kill-p "pasystray") (jao-shell-exec "pasystray")))

(transient-define-prefix jao-transient-streaming ()
  [:description
   (lambda () (format "Streaming using %s" jao-mpris-player))
   ["Search" :if jao-mpris-mopidy-p
    ("a" "album" jao-streaming-album)
    ("A" "artist" jao-streaming-artist)
    ("t" "track" jao-streaming-track)
    ("P" "playlist" jao-streaming-playlist)]
   ["Play"
    ("s" "toggle" jao-streaming-toggle)
    ("n" "next" jao-streaming-next)
    ("p" "previous" jao-streaming-prev)
    ("T" "toggle player" jao-streaming-toggle-player)]
   ["Seek & shout"
    ("f" "seek fwd" jao-streaming-seek :transient t)
    ("F" "seek bwd" jao-streaming-seek-back :transient t)
    ("u" "up" jao-streaming-volume :transient t)
    ("d" "down" jao-streaming-volume-down :transient t)]
   ["Browse"
    ("l" "playing list" jao-streaming-list :if jao-mpris-mopidy-p)
    ("L" "lyrics" jao-streaming-lyrics)
    ("w" "currently playing" jao-streaming-current)]
   ["Act"  :if jao-mpris-mopidy-p
    ("k" "like" jao-streaming-like)
    ("K" "dislike" jao-streaming-dislike)
    ("S" "toggle shuffle" jao-streaming-toggle-shuffle)]])

(transient-define-prefix jao-transient-media ()
  [["Play"
    ("m" "toggle" jao-player-toggle)
    ("n" "next" jao-player-next)
    ("p" "previous" jao-player-previous)
    ("s" "select album" jao-player-select-album)]
   ["Seek and search"
    ("f" "seek fwd" jao-player-seek-10 :transient t)
    ("F" "seek bwd" jao-player-seek--10 :transient t)
    ("a" "search album" jao-mpc-select-album)
    ("S" "play stream" jao-mpc-play-stream)]
   ["Browse"
    ("b" "browse" jao-player-browse)
    ("l" "show play list" jao-player-list)
    ("L" "show lyrics" jao-show-lyrics)
    ("w" "now playing" jao-player-echo)]
   [:description
    (lambda ()
      (message "%s %s"
               (if (jao-mpc-mopidy-p) "mopidy" "mpd")
               (if (jao-mpc-playing-p)
                   (jao-mpc--current-timestr t)
                 (jao-mpc--current-str)))
      (format "Master %s%%" (jao-mixer-get-level nil t)))
    ("d" "down" jao-mixer-master-down :transient t)
    ("u" "up" jao-mixer-master-up :transient t)
    ("M" "toggle" jao-mixer-master-toggle)]
   [:description
    (lambda () (format "Capture %s%%" (jao-mixer-get-level "Capture" t)))
    ("D" "down" jao-mixer-capture-down :transient t)
    ("U" "up" jao-mixer-capture-up :transient t)]
   ["Utilities"
    ("c" "reconnect to mpd" jao-player-connect)
    ("N" "next random album" jao-random-album-next)
    ("r" (lambda ()
           (concat (if jao-random-album-active "dis" "en") "able random album"))
     jao-random-album-toggle)
    ;; ("P" (lambda () (concat "Toggle to " (if (jao-mpc-mopidy-p) "mpd" "mopidy")))
    ;;  jao-mpc-toggle-port)
    ("P" "pasystray" jao-toggle-pasystray-applet)]])

(global-set-key (kbd "s-m") #'jao-transient-media)

;;; Graphical window system
;;;; x11 utils
(defun jao-xdotool (arg-or-wait &rest args)
  (apply 'jao-shell-exec*
         (if (stringp arg-or-wait) "xdotool" arg-or-wait)
         (if (stringp arg-or-wait) arg-or-wait "xdotool")
         args))

(defsubst jao-xdotool-string (&rest args)
  (apply 'jao-shell-string "xdotool" args))

(defsubst jao-x11-focused-id () (jao-xdotool-string "getwindowfocus"))

(defsubst jao-x11-window-name (&optional wid)
  (jao-xdotool-string "getwindowname" (or wid (jao-x11-focused-id))))

(defsubst jao-x11-search-window (title)
  (jao-xdotool-string "search" "--name" title))

(defsubst jao-x11-goto-ws (n) (jao-xdotool t "set_desktop" (format "%s" n)))

;;;; exwm
(defvar jao-exwm-enabled nil)
(defun jao-exwm-enabled-p () jao-exwm-enabled)

(defun jao-exwm-enable ()
  (require 'jao-custom-exwm)
  (setq jao-exwm-enabled t)
  (display-time-mode -1)
  (jao-ednc-setup 95)
  (exwm-enable)
  (x-change-window-property "_XMONAD_TRAYPAD" "" nil nil nil nil 0)
  (jao-mode-line-add-to-minibuffer-left 90)
  (jao-xmobar-restart)
  (jao-trisect t))

;;;; xmonad
(defvar jao-xmonad-enabled (string= "xmonad" (or (getenv "wm") "")))
(defun jao-xmonad-enabled-p () jao-xmonad-enabled)

(defun jao-xmonad-enable ()
  (setq jao-browse-doc-use-emacs-p (display-graphic-p))
  (setq jao-mode-line-in-minibuffer nil)
  (display-battery-mode -1)
  (jao-trisect)
  (message "Welcome to xmonad"))

(when jao-xmonad-enabled
  (add-hook 'after-init-hook #'jao-xmonad-enable))

;;;; wayland

(use-package jao-wayland :demand t)

(defun jao-wayland-enable ()
  (interactive)
  (defalias 'x-change-window-property #'ignore)
  (jao-trisect)
  (message "Welcome to wayland"))

(defun jao-river-enable ()
  (jao-wayland-enable)
  (when (jao-shell-running-p "i3bar-river")
    (jao-tracking-set-log ""))
  (message "Welcome to river"))

(when jao-river-enabled
  (add-hook 'after-init-hook #'jao-river-enable t))

(when jao-sway-enabled
  (add-hook 'after-init-hook #'jao-wayland-enable t))

;;;; wallpaper
(defvar jao-wallpaper-dir "~/.wallpapers/")

(defvar jao-wallpaper-random-candidates
  '("wallpaper.jpg" "wallpaper2.jpg"))

(defvar jao-wallpaper-random-candidates-light
  '("wallpaper.jpg" "wallpaper2.jpg"))

(defvar jao-wallpaper-random-wake t
  "Set to t for getting a new wallpaper on awaking from sleep")

(defun jao-set-wallpaper (&optional path)
  (interactive)
  (let ((current (format "~/.wallpaper.%s" (jao-colors-scheme))))
    (when-let ((f (or (and path (expand-file-name path))
                      (read-file-name "Image: "
                                      jao-wallpaper-dir
                                      (file-symlink-p current)
                                      t))))
      (make-symbolic-link f current t)
      (cond (jao-river-enabled (jao-river-set-wallpaper f))
            (jao-sway-enabled (jao-sway-set-wallpaper f))
            (t (shell-command-to-string (format "xwallpaper --zoom %s" f)))))))

(defun jao-set-random-wallpaper ()
  (interactive)
  (when (or (called-interactively-p 'interactive)
            jao-wallpaper-random-wake)
    (let* ((ws (if (jao-colors-scheme-dark-p)
                   jao-wallpaper-random-candidates
                 jao-wallpaper-random-candidates-light))
           (f (seq-random-elt ws)))
      (jao-set-wallpaper (expand-file-name f jao-wallpaper-dir))
      (message "%s" f))))

(add-to-list 'jao-sleep-awake-functions #'jao-set-random-wallpaper)

;;;; screensaver and lock
(defun jao-screensaver-enabled ()
  (string= (jao-shell-string "xdg-screensaver status") "enabled"))

(defvar jao-screensaver--wid nil)
(defun jao-screensaver-toggle ()
  (interactive)
  (if (jao-screensaver-enabled)
      (let ((wid (jao-x11-focused-id)))
        (setq jao-screensaver--wid wid)
        (jao-shell-exec* t "xdg-screensaver" "suspend" wid))
    (jao-shell-exec* t "xdg-screensaver" "resume" jao-screensaver--wid)
    (setq jao-screensaver--wid nil))
  (jao-notify (format "Screensaver %s"
                      (jao-shell-string "xdg-screensaver status"))))

(jao-shell-def-exec jao-xlock-screen "xdg-screensaver" "activate")
(jao-shell-def-exec jao-suspend "sudo" "systemctl" "suspend")
(jao-shell-def-exec jao-poweroff "sudo" "systemctl" "poweroff")

(defun jao-lock-screen ()
  (interactive)
  (if jao-wayland-enabled
      (shell-command "swaylock -i ~/.lockimage")
    (jao-xlock-screen)))

(transient-define-prefix jao-transient-sleep ()
  ["Sleep"
   ("l" "lock screen" jao-lock-screen)
   ("z" "sleep" jao-suspend)
   ("u" (lambda ()
          (if (jao-screensaver-enabled) "suspend screensaver" "resume screensaver"))
    jao-screensaver-toggle)
   ("poof" "power-off" jao-poweroff)])

;;;; X clipboard
(setq select-enable-clipboard t
      select-enable-primary t
      selection-timeout 100
      xterm-select-active-regions t)

(use-package xclip
  :ensure t
  :init (setq xclip-method (if jao-wayland-enabled 'wl-copy 'xclip)))

(unless (display-graphic-p) (xclip-mode 1))

;;;; pop-up frames
(defun jao-open-in-x-frame (&optional width height)
  (interactive)
  (make-frame `((window-system . x)
                (name . "emacs popup")
                (width . ,(or width (window-width)))
                (height . ,(or height (window-height)))))
  (define-key (current-local-map) "q" #'delete-frame))

;;;; xmobar
(defun jao-xmobar-kill ()
  (interactive)
  (shell-command "killall xmobar-single"))

(defun jao-xmobar-restart ()
  (interactive)
  (jao-xmobar-kill)
  (start-process "" nil "xmobar-single" "-d"))

(use-package tab-bar
  :init (setq tab-bar-close-button-show nil
              tab-bar-show (> emacs-major-version 28)
              tab-bar-format ()))

(use-package xmobar
  :init (setq  xmobar-tab-bar t
               xmobar-tab-split "*"
               xmobar-tab-bar-format
               (if window-system
                   '(xmobar-left-string
                     tab-bar-format-align-right
                     xmobar-right-string)
                 '(xmobar-left-string
                   xmobar-elastic-space
                   xmobar-right-string))
               xmobar-command
               (if window-system '("xmobar-emacs" "-TAnsi") "xmobar-emacs")))

;;; Global transients
(defun jao-list-packages ()
  (interactive)
  (jao-afio-goto-scratch)
  (package-list-packages))

(defun jao-window-system-p ()
  (or jao-exwm-enabled jao-xmonad-enabled jao-wayland-enabled))

(defun jao-x11-p () (or jao-exwm-enabled jao-xmonad-enabled))

(defun jao-reveal ()
  (interactive)
  (cond ((or outline-minor-mode (derived-mode-p 'outline-mode ))
         (outline-show-entry))
        ((derived-mode-p 'org-mode) (org-reveal))))

(jao-def-exec-in-term "htop" "htop" (jao-afio-goto-scratch))

(transient-define-prefix jao-transient-utils ()
  "Global operations."
  [["Notes"
    ("n" "create new note" jao-org-notes-create)
    ("/" "open note" jao-org-notes-open)
    ("\\" "open note by tags" jao-org-notes-consult-tags)
    ("g" "ripgrep notes" jao-org-notes-consult-ripgrep)]
   ["Documents"
    ("dd" "go to doc" jao-select-pdf :if display-graphic-p)
    ("do" "open doc" jao-open-doc)
    ("dr" "search docs with recoll" jao-recoll-consult-docs)]
   ["Monitors"
    ("p" "htop" jao-term-htop)
    ("P" "pasytray" jao-toggle-pasystray-applet)
    ("x" "restart i3bar" jao-river-restart-i3bar :if jao-river-enabled-p)
    ("x" "restart xmobar" jao-xmobar-restart :if jao-exwm-enabled-p)
    ("x" "kill xmobar" jao-xmobar-kill :if jao-xmonad-enabled-p)]
   ["Network"
    ("S" "ssh" jao-ssh)
    ("b" "bluetooth" bluetooth-list-devices)
    ("c" "connect chats" jao-all-chats)
    ("m" "proton bridge" run-proton-bridge)
    ("v" "view video" jao-view-video)]
   ["Chats"
    ("t" "telegram" jao-chats-telega)
    ("s" "slack" jao-chats-slack)
    ("i"  "irc" jao-chats-irc)
    ("M" "mastodon" jao-mastodon)
    ("T" "telegram rooster" jao-telega)]
   ["Window system" :if jao-window-system-p
    ("w" "set wallpaper" jao-set-wallpaper)
    ("W" "set radom wallpaper" jao-set-random-wallpaper)
    ("B u" (lambda ()
             (let ((b (jao-brightness)))
               (format "bright up %s" (and (string-match ".*\\((.+)\\).*" b)
                                           (match-string 1 b)))))
     jao-bright-up :transient t)
    ("B d" "bright down" jao-bright-down :transient t)]
   ["Helpers"
    ("a" "aptitude" jao-term-aptitude)
    ("l" "packages" jao-list-packages)
    ("r" "reveal" jao-reveal)
    ("k" (lambda () (concat "keyboard" (when (jao-kb-toggled-p) "*")))
     jao-kb-toggle :if jao-x11-p)]])

(global-set-key (kbd "s-w") #'jao-transient-utils)

;;; Global key bindings
(defun jao-global-keybindings ()
  (interactive)
  (global-set-key (kbd "<f2>") #'magit-status)
  (global-set-key (kbd "C-x p") #'jao-prev-window)
  (global-set-key (kbd "C-x o") 'other-window)
  (global-set-key "\C-cj" #'join-line)
  (global-set-key "\C-cn" #'next-error)
  (global-set-key "\C-cq" #'auto-fill-mode)
  (global-set-key "\C-xr\M-w" #'kill-rectangle-save)
  (global-set-key "\C-c\C-z" #'comment-or-uncomment-region)
  (global-set-key "\C-z" #'comment-or-uncomment-region))

(jao-global-keybindings)

;;; Last minute (post.el)
(jao-load-site-el "post")