1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
|
#+title: Emacs configuration
#+author: Aryadev Chavali
#+description: My Emacs configuration
#+property: header-args:emacs-lisp :tangle config.el :comments link :results none
#+startup: noindent
#+options: toc:nil num:t
#+latex_header:\usepackage[margin=1.0in]{geometry}
#+latex_class: article
#+latex_class_options: [a4paper,12pt]
* Introduction
:PROPERTIES:
:header-args:emacs-lisp: :tangle config.el :results none
:END:
Welcome to my Emacs configuration. You may be confused by the fact
it's a readable document with prose; this file serves as both
documentation *and* code. Here's an example of some Emacs Lisp code:
#+begin_src emacs-lisp
;;; config.el --- Compiled configuration from config.org -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Aryadev Chavali
;; Author: Aryadev Chavali <aryadev@aryadevchavali.com>
;; This program is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
;; FOR A PARTICULAR PURPOSE. See the MIT License for details.
;; You may distribute and modify this code under the terms of the MIT License,
;; which you should have received a copy of along with this program. If not,
;; please go to <https://opensource.org/license/MIT>.
;;; Commentary:
;; Welcome to my Emacs configuration. This file is considered volatile i.e. any
;; edits made to this file will be overwritten if and when the configuration is
;; next compiled.
;; To propagate edits from this file back to the literate document, call
;; (org-babel-detangle).
;;; Code:
#+end_src
So how does this work? [[file:elisp/literate.el][Literate]] is a
package that I designed myself which "compiles" my configuration and
links it all together. This document is compiled by collecting all
the Emacs Lisp blocks, concatenating them then writing it to
=config.el=, which is loaded as a standard Emacs Lisp file afterwards.
So all the prose is ignored in the final document.
This allows the document to act as both /source code/ and
/documentation/ at once. Pretty cool, right? This style of coding is
called /literate programming/. Donald Knuth
[[https://en.wikipedia.org/wiki/Literate_programming][really liked]]
the idea and I see why.
Some details about the configuration:
+ The ordering of sections is relevant: packages defined earlier can
be utilised by later packages
+ Sections tagged with =WAIT= are not compiled into the final document
(using :PROPERTIES:), usually with some explanation.
+ Some sections are essentially blog posts, so you may just want to
read the tangled output via ~(org-babel-tangle)~
* Basics
Let's setup a few absolute essentials:
+ My name and mail address
+ File encoding (no "\r" characters at the end of lines, please)
+ Where to store backup files (~backup-directory-alist~)
+ Auto refresh buffers when a change occurs (~auto-revert-mode~)
+ Yes or no questions are less painful (~y-or-n-p~)
+ Make the "kill ring" work seamlessly with the clipboard
+ Deleting files or directories "trashes" them instead
+ Font size based on the machine
+ Disable mouse usage where possible
+ Ensure when compiling the Emacs configuration, we only get messages
for really bad stuff
#+begin_src emacs-lisp
(use-package emacs
:demand t
:init
(setq user-full-name "Aryadev Chavali"
user-mail-address "aryadev@aryadevchavali.com"
buffer-file-coding-system 'utf-8-unix
save-buffer-coding-system 'utf-8-unix
backup-directory-alist `(("." . ,(no-littering-expand-var-file-name "saves/")))
global-auto-revert-non-file-buffers t
auto-revert-verbose nil
auto-revert-use-notify nil
select-enable-clipboard t
delete-by-moving-to-trash t
use-file-dialog nil
use-dialog-box nil
warning-minimum-level :emergency)
:config
(fset 'yes-or-no-p 'y-or-n-p)
(global-auto-revert-mode)
(set-face-attribute 'default nil :height
(pcase (system-name)
("ravenmaiden" 130)
(_ 120))))
#+end_src
* Custom functionality and libraries
This is custom Lisp I've written to help me out throughout the
configuration. Note that because it's setup so early I can use it
throughout the file.
** dash
Dash is an external library that provides a ton of Emacs Lisp
functions that make it a bit nicer to use.
#+begin_src emacs-lisp
(use-package dash
:straight t
:demand t)
#+end_src
** Procedure
An anonymous function (~lambda~) which takes no arguments is a
"procedure". This macro generates procedures, with the parameters of
the macro being the body of the procedure. The function is returned
quoted (as data rather than code), as that is the most common use of
this macro.
#+begin_src emacs-lisp
(defmacro proc (&rest BODY)
"For a given list of forms BODY, return a quoted 0 argument
lambda."
`(function (lambda nil ,@BODY)))
#+end_src
** Automatically run a command on saving
Sometimes you want a command to run when a file is saved, a classic
example being compiling a project after saving. To run a command
after saving, one may write the command as an Emacs Lisp function and
add it to the ~after-save-hook~ which essentially subscribes that
function to the ~after-save~ event. We can encapsulate these steps
in one macro, which is defined here.
#+begin_src emacs-lisp
(use-package simple
:defer t
:config
(defmacro create-auto-save (CONDITIONS &rest TO-RUN)
"Create a hook for after-save, where on CONDITIONS being met
TO-RUN is evaluated."
`(add-hook 'after-save-hook
(proc
(interactive)
(when ,CONDITIONS ,@TO-RUN)))))
#+end_src
** Clean buffer list
Clean all buffers except for those in ~clean-buffers-keep~.
#+begin_src emacs-lisp
(defconst clean-buffers-keep
(list "config.org" "*scratch*"
"*dashboard*" "*Messages*"
"*Warnings*" "*eshell*")
"List of buffer names to preserve.")
(defun clean-buffers ()
"Kill all buffers except any with names in CLEAN-BUFFERS-KEEP."
(interactive)
(let ((should-not-kill
#'(lambda (buf)
(member (buffer-name buf)
clean-buffers-keep))))
(-->
(buffer-list)
(cl-remove-if should-not-kill it)
(mapc #'kill-buffer it))))
#+end_src
** Custom window management
Generally speaking, applications that have some windowing features do
not have a lot of options for how those windows are placed. Emacs has
a window management system unlike any other piece of software I have
ever used with some incredible capabilities. Unfortunately, as a
result, it is quite complex to use.
The big idea is this table, ~display-buffer-alist~, which associates
regular expressions with "actions". The regular expressions are for
the name of buffers, and the actions are how the buffer should be
displayed. And there are a *lot* of ways to display buffers.
Here's an example record:
#+begin_src lisp
'("config.org"
(display-buffer-in-side-window)
(side . bottom))
#+end_src
This matches any buffer named =config.org=, displaying the buffer in a
side window to the bottom.
What I configure here is a ~use-package~ keyword, ~:display~, which
will allow me to write associations in ~display-buffer-alist~ really
easily.
2024-04-23: Found this option ~switch-to-buffer-obey-display-actions~
which makes manual buffer switches obey the same constraints via
~display-buffer-alist~ as creating the buffer automatically.
#+begin_src emacs-lisp
(use-package window
:demand t
:init
(setq switch-to-buffer-obey-display-actions nil)
(with-eval-after-load "use-package-core"
(add-to-list 'use-package-keywords ':display)
(defun use-package-normalize/:display (_name-symbol _keyword args)
"Normalise args for use in handler.
Don't do anything to the args here."
args)
(defun use-package-handler/:display (name _keyword args rest state)
(use-package-concat
(use-package-process-keywords name rest state)
(mapcar
#'(lambda (arg)
`(add-to-list 'display-buffer-alist
',arg))
args)))))
#+end_src
Here's some ~:display~ records for buffers that don't really have
configuration anywhere else in the file. These serve as good examples
on how to use the keyword.
#+begin_src emacs-lisp
(use-package window
:defer t
:display
("\\*Process List\\*"
(display-buffer-at-bottom)
(window-height . 0.25))
("\\*Async Shell Command\\*"
(display-buffer-at-bottom)
(window-height . 0.25)))
#+end_src
** add-to-list multiple times
I want to be able to add multiple items to a list in a single
expression. Here's a macro to do that for me.
#+begin_src emacs-lisp
(defmacro add-multiple-to-list (listvar &rest elements)
(cons
'progn
(cl-loop for element in elements
collect `(cl-pushnew ,element ,listvar))))
#+end_src
** Setting number of native jobs
Emacs has a native compilation capability to make things /even
faster/. In [[file:early-init.el][early-init.el]] I set the number of
native-workers to 4, which isn't necessarily optimal when
loading/compiling the rest of this file depending on the machine I
use. On my machines, which have 8 process throughput (4 cores + hyper
threading), 6-7 workers makes much more sense. On a machine I've
never used before, 3 seems to be a reasonable default.
#+begin_src emacs-lisp
(use-package comp
:init
(setq native-comp-async-jobs-number
(pcase (system-name)
("ravenmaiden" 6)
(_ 3))))
#+end_src
* Core packages
Here I configure packages, internal and external, which define either
critical infrastructure for the rest of the configuration or provide
key functionality disassociated from any specific environment.
** General - Bindings package
Vanilla Emacs has the ~bind-key~ function (and the ~bind-key*~ macro)
for this, but [[*Evil - Vim Emulation][Evil]] has it's own
~evil-define-key~. I'd like a unified interface for using both, which
is why I use =general=. General provides a set of very useful macros
for defining keys in a variety of different situations. One may
redefine any key in any keymap, bind over different Evil states, add
=which-key= documentation, create so-called "definers" which act as
wrapper macros over some pre-defined configuration, etc, all at the
same time.
Here I setup the rough outline of how bindings should be made in the
global scope, namely:
+ Use "SPC" as a "leader", the root of all general bindings
+ Use "\" as a local-leader, the root of all major mode specific
bindings
+ A few "definers" for the different sub bindings for the leader key
+ ~nmmap~ macro, for defining keys under both normal and motion
states.
#+begin_src emacs-lisp
(use-package general
:straight t
:demand t
:config
;; General which key definitions for leaders
(general-def
:states '(normal motion)
"SPC" 'nil
"\\" '(nil :which-key "Local leader")
"SPC a" '(nil :which-key "Applications")
"SPC b" '(nil :which-key "Buffers")
"SPC c" '(nil :which-key "Code")
"SPC d" '(nil :which-key "Directories")
"SPC f" '(nil :which-key "Files")
"SPC i" '(nil :which-key "Insert")
"SPC m" '(nil :which-key "Modes")
"SPC r" '(nil :which-key "Tabs")
"SPC s" '(nil :which-key "Search")
"SPC t" '(nil :which-key "Shell")
"SPC q" '(nil :which-key "Quit/Literate"))
(general-create-definer leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC")
(general-create-definer local-leader
:states '(normal motion)
:prefix "\\")
(general-create-definer code-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC c")
(general-create-definer file-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC f")
(general-create-definer shell-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC t")
(general-create-definer tab-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC r")
(general-create-definer mode-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC m")
(general-create-definer app-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC a")
(general-create-definer search-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC s")
(general-create-definer buffer-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC b")
(general-create-definer quit-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC q")
(general-create-definer insert-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC i")
(general-create-definer dir-leader
:states '(normal motion)
:keymaps 'override
:prefix "SPC d")
(general-create-definer general-nmmap
:states '(normal motion))
(defalias 'nmmap #'general-nmmap)
(general-evil-setup t))
#+end_src
*** Some binds for Emacs
Here are some bindings for Emacs using general and the definers
created previously. Here I bind stuff I don't care to make a separate
heading for, so it serves as both a dumping ground and as a great
set of examples on how to use general.
#+begin_src emacs-lisp
(use-package emacs
:init
;; this is for `duplicate-dwim'
(setq duplicate-line-final-position -1)
:general
("C-x d" #'delete-frame)
(:keymaps 'help-map
"l" #'find-library)
(nmmap
:keymaps 'override
"M-%" #'replace-regexp-as-diff
"M-o" #'duplicate-dwim
"M-;" #'comment-dwim
"gC" #'comment-dwim
"g=" #'align-regexp
"C--" #'text-scale-decrease
"C-=" #'text-scale-increase
"C-+" #'text-scale-adjust)
(leader
"SPC" '(execute-extended-command :which-key "M-x")
"R" `(revert-buffer :which-key "Revert buffer")
":" `(,(proc (interactive) (switch-to-buffer "*scratch*"))
:which-key "Switch to *scratch*")
"!" '(async-shell-command :which-key "Async shell command")
"h" '(help-command :which-key "Help"))
(mode-leader
"t" `(,(proc (interactive) (+oreo/load-theme))
:which-key "Reload current theme")
"T" `(,(proc (interactive) (+oreo/switch-theme))
:which-key "Iterate through themes"))
(code-leader
"F" `(,(proc (interactive) (find-file "~/Code/"))
:which-key "Open ~/Code/"))
(search-leader
"i" #'imenu)
(file-leader
"f" #'find-file
"P" (proc (interactive)
(find-file (concat user-emacs-directory "config.org")))
"F" #'find-file-other-window
"t" #'find-file-other-tab
"v" #'add-file-local-variable
"s" #'save-buffer)
(insert-leader
"c" #'insert-char)
(dir-leader
"v" #'add-dir-local-variable)
(buffer-leader
"b" #'switch-to-buffer
"d" #'kill-current-buffer
"c" #'kill-buffer-and-window
"K" #'kill-buffer
"j" #'next-buffer
"k" #'previous-buffer
"D" #'clean-buffers)
(quit-leader
"p" #'straight-pull-package
"b" #'straight-rebuild-package
"q" #'save-buffers-kill-terminal
"c" #'+literate/compile-config
"C" #'+literate/clean-config
"l" #'+literate/load-config))
#+end_src
** Evil - Vim emulation
My editor journey started off with Vim rather than Emacs, so my brain
has imprinted on its style. Emacs is super extensible so there exists
a package for porting Vim's modal editing style to Emacs, called evil
(Emacs Vi Layer).
However there are a lot of packages in Vim that provide greater
functionality, for example tpope's "vim-surround". Emacs has these
capabilities out of the box, but there are further packages which
integrate them into Evil.
*** Evil core
Setup the evil package, with some opinionated settings:
+ Switch ~evil-upcase~ and ~evil-downcase~ because I use ~evil-upcase~
more
+ Use 'T' character as an action for "transposing objects"
+ Swapping any two textual "objects" seems like a natural thing in
Vim considering the "verb-object" model most motions follow, but
by default Vim doesn't have the ability to do so. But Emacs can,
hence I can set these up.
+ Allow the Evil cursor to traverse End of Lines like the Emacs cursor
+ Do not move the cursor when exiting insert mode.
#+begin_src emacs-lisp
(use-package evil
:straight t
:demand t
:init
(setq evil-split-window-below t
evil-vsplit-window-right t
evil-undo-system #'undo-tree
evil-move-beyond-eol t
evil-move-cursor-back nil
evil-want-abbrev-expand-on-insert-exit t
evil-want-minibuffer t
evil-want-keybinding nil
evil-want-Y-yank-to-eol t)
:config
(evil-mode)
:general
(leader
"w" '(evil-window-map :which-key "Window")
"wT" #'window-swap-states
"wd" #'evil-window-delete
"w;" #'make-frame)
(nmmap
"K" #'man
"TAB" #'evil-jump-item
"r" #'evil-replace-state
"zC" #'hs-hide-level
"zO" #'hs-show-all)
(general-def
:keymaps 'override
:states '(normal motion visual)
"gu" #'evil-upcase
"gU" #'evil-downcase
"M-y" #'yank-pop
"T" 'nil)
(general-def
:keymaps 'override
:states '(normal motion visual)
:infix "T"
"w" #'transpose-words
"c" #'transpose-chars
"s" #'transpose-sentences
"p" #'transpose-paragraphs
"e" #'transpose-sexps
"l" #'transpose-lines))
#+end_src
*** Evil surround
A port for vim-surround, providing the ability to mutate delimiters
around some text.
#+begin_src emacs-lisp
(use-package evil-surround
:after evil
:straight t
:config
(global-evil-surround-mode))
#+end_src
*** Evil commentary
A port of vim-commentary, providing generalised commenting of objects.
#+begin_src emacs-lisp
(use-package evil-commentary
:after evil
:straight t
:config
(evil-commentary-mode))
#+end_src
*** Evil multi cursor
Setup for multi cursors in Evil mode, which is a bit of very nice
functionality. Don't let evil-mc setup it's own keymap because it
uses 'gr' as its prefix, which I don't like.
#+begin_src emacs-lisp
(use-package evil-mc
:after evil
:straight t
:init
(defvar evil-mc-key-map (make-sparse-keymap))
:general
(nmap
:infix "gz"
"m" 'evil-mc-make-all-cursors
"u" 'evil-mc-undo-last-added-cursor
"q" 'evil-mc-undo-all-cursors
"s" 'evil-mc-pause-cursors
"r" 'evil-mc-resume-cursors
"f" 'evil-mc-make-and-goto-first-cursor
"l" 'evil-mc-make-and-goto-last-cursor
"h" 'evil-mc-make-cursor-here
"j" 'evil-mc-make-cursor-move-next-line
"k" 'evil-mc-make-cursor-move-prev-line
"N" 'evil-mc-skip-and-goto-next-cursor
"P" 'evil-mc-skip-and-goto-prev-cursor
"n" 'evil-mc-skip-and-goto-next-match
"p" 'evil-mc-skip-and-goto-prev-match
"I" 'evil-mc-make-cursor-in-visual-selection-beg
"A" 'evil-mc-make-cursor-in-visual-selection-end
"d" #'evil-mc-make-and-goto-next-match)
:config
(global-evil-mc-mode))
#+end_src
*** Evil multi edit
Evil-ME provides a simpler parallel editing experience within the same
buffer. I use it in-tandem with Evil-MC, where I use Evil-ME for
textual changes and Evil-MC for more complex motions.
#+begin_src emacs-lisp
(use-package evil-multiedit
:straight t
:defer t
:init
(setq evil-multiedit-scope 'visible)
:general
(:states '(normal visual)
:keymaps 'override
"M-e" #'evil-multiedit-match-and-next
"M-E" #'evil-multiedit-match-and-prev))
#+end_src
*** Evil collection
Provides a community based set of keybindings for most modes in
Emacs. I don't necessarily like all my modes having these bindings
though, as I may disagree with some. So I use it in a mode to mode basis.
#+begin_src emacs-lisp
(use-package evil-collection
:straight t
:after evil)
#+end_src
*** Evil number
Increment/decrement a number at point like Vim does, but use bindings
that don't conflict with Emacs default.
#+begin_src emacs-lisp
(use-package evil-numbers
:straight t
:defer t
:general
(nmmap
"+" #'evil-numbers/inc-at-pt
"-" #'evil-numbers/dec-at-pt))
#+end_src
*** Evil goggles
Make it easier to notice edits and changes using Vim motions!
#+begin_src emacs-lisp
(use-package evil-goggles
:straight t
:after evil
:init
(setq evil-goggles-duration 0.1
evil-goggles-blocking-duration 0.1
evil-goggles-async-duration 0.9
evil-goggles-default-face 'pulsar-cyan)
:config
(evil-goggles-mode)
(evil-goggles-use-diff-faces))
#+end_src
** Text Completion
Emacs is a text based interface. All commands use textual input,
operate on text and produce text as output. A classic command is
~execute-extended-command~, which takes a command name as input then
executes it. Input is taken from the /minibuffer/.
A critical component of this interaction is text completion: given a
list of options and some user input, try to find an option that best
fits it. Out of the box, Emacs provides the ~completions-list~ to
help with selecting an option given some initial input, which can be
activated when the minibuffer is active using ~TAB~. This is quite a
handy interface on its own, but we can do much better.
So called "text completion frameworks" remodel the interaction with
the minibuffer to improve certain aspects of it. Emacs provides two
such packages out of the box: ido and icomplete. They both eschew the
~completions-list~, instead providing an incrementally adjusted list
of results based on the current input within the minibuffer itself.
IDO only covers a few text based commands, such as ~find-file~, while
~IComplete~ covers essentially all of them.
In terms of external packages, there exist a few. I used Ivy for a
few year, partially from the inertia of my Doom Emacs configuration.
I then moved to ~icomplete~, then to ~vertico~. The move to these
more minimal frameworks come from a similar school of thought as the
Unix Philosophy, but for Emacs' packages: do one thing and do it well.
While Ivy is a very good piece of software, certain aspects are done
better by standalone packages built for that purpose (such as
[[*rg][rg]]). ~vertico~ or ~icomplete~ are packages that only care
about the minibuffer and making interactions with it more pleasant,
and they do a great job at that.
*** Minibuffer
As described before, the minibuffer is the default text input/output
mechanism. Here are some basic binds that I need to work effectively
in it.
+ By default, the minibuffer is in insert state, with Escape going to
normal state. M-escape allows quick exits from the minibuffer while
in insert state
+ In normal state, escape exits the minibuffer
+ ~M-{j, k}~ for selecting elements
+ ~C-M-j~ for forcing the minibuffer to accept on the current
selection
+ ~<backtab>~ (shift + TAB) to switch to the completions list
#+begin_src emacs-lisp
(use-package minibuffer
:demand t
:init
(setq enable-recursive-minibuffers t)
:general
(imap
:keymaps 'minibuffer-local-map
"M-<escape>" #'abort-minibuffers)
(nmap
:keymaps 'minibuffer-local-map
"<escape>" #'abort-minibuffers)
(general-def
:states '(normal insert)
:keymaps 'minibuffer-local-map
"<backtab>" #'switch-to-completions
"RET" #'exit-minibuffer
"M-j" #'next-line-or-history-element
"M-k" #'previous-line-or-history-element))
#+end_src
I can also save the history of the minibuffer to make it easier to
replicate previous inputs.
#+begin_src emacs-lisp
(use-package savehist
:defer t
:config
(savehist-mode t))
#+end_src
*** Completions-list
The list of completions that comes by default with the minibuffer when
forcing it to complete some input. Here I just make some binds to
make that selection easier.
#+begin_src emacs-lisp
(use-package simple
:demand t
:display
("\\*Completions\\*"
(display-buffer-in-side-window)
(window-height . 0.3)
(side . bottom))
:general
(nmmap
:keymaps 'completion-list-mode-map
"l" #'next-completion
"h" #'previous-completion
"q" #'quit-window
"RET" #'choose-completion
"<backtab>" #'switch-to-minibuffer)
:init
(with-eval-after-load "evil"
(evil-set-initial-state 'completion-list-mode 'normal)))
#+end_src
*** Vertico
Vertico is a minimalist text completion framework for the minibuffer.
It's configuration is /so/ similar to IComplete that I essentially
copy-pasted it, and it does a great job. It's quite fast as well,
outperforming ~icomplete~ consistently when displaying results.
#+begin_src emacs-lisp
(use-package vertico
:straight t
:demand t
:init
(setq vertico-count 8
vertico-cycle t)
:config
(vertico-mode)
:general
(general-def
:state '(normal insert)
:keymaps 'vertico-map
"M-j" #'vertico-next
"M-k" #'vertico-previous
"RET" #'vertico-exit
"TAB" #'minibuffer-complete
"SPC" #'self-insert-command))
#+end_src
*** Consult
Consult provides some improved replacements for certain inbuilt
functions, and a few extensions as well. If we consider ivy/counsel
to be two separate packages, ivy being the completion framework and
counsel the extension package using ivy, consult would be the latter.
Unlike counsel, however, it isn't dependent on any one completion
framework making it more extensible and easier to use in different
situations.
#+begin_src emacs-lisp
(use-package consult
:straight t
:init
(setq consult-preview-excluded-buffers t
consult-preview-excluded-files '(".*"))
:general
(:states '(normal insert motion visual emacs)
[remap imenu] #'consult-imenu
[remap switch-to-buffer] #'consult-buffer)
(leader
"'" #'consult-register)
(search-leader
"s" #'consult-line)
:config
(defun consult-line-isearch-history (&rest _)
"Add latest `consult-line' search pattern to the isearch history.
This allows n and N to continue the search after `consult-line' exits.
From https://jmthornton.net/blog/p/consult-line-isearch-history, taken
2024-10-10 03:58 BST."
(when (and (bound-and-true-p evil-mode)
(eq evil-search-module 'isearch)
consult--line-history)
(let* ((pattern (car consult--line-history))
(regexp (if (string-prefix-p "\\_" pattern)
(substring pattern 2)
pattern)))
(add-to-history 'regexp-search-ring regexp)
(setq evil-ex-search-pattern (evil-ex-pattern regexp t nil nil))
(setq evil-ex-search-direction 'forward))))
(advice-add #'consult-line :after #'consult-line-isearch-history))
#+end_src
*** Amx
Amx is a fork of Smex that works to enhance the
~execute-extended-command~ interface. It provides a lot of niceties
such as presenting the key bind when looking for a command.
#+begin_src emacs-lisp
(use-package amx
:straight t
:defer 2
:init
(setq amx-backend 'auto)
:config
(amx-mode))
#+end_src
*** Orderless
Orderless sorting method for completion, probably one of the best
things ever.
#+begin_src emacs-lisp
(use-package orderless
:straight t
:after vertico
:config
(setq completion-styles '(orderless substring basic)
completion-category-defaults nil
completion-category-overrides
'((file (styles flex partial-completion substring)))))
#+end_src
*** Company
Company is the auto complete system I use. I don't like having heavy
setups for company as it only makes it slower to use. In this case,
just setup some evil binds for company.
#+begin_src emacs-lisp
(use-package company
:defer t
:straight t
:hook
(prog-mode-hook . company-mode)
(eshell-mode-hook . company-mode)
:general
(imap
:keymaps 'company-mode-map
"C-SPC" #'company-complete
"C-@" #'company-complete
"M-j" #'company-select-next
"M-k" #'company-select-previous))
#+end_src
** Pretty symbols
Prettify symbols mode allows users to declare "symbols" that replace
text within certain modes. It's eye candy in most cases, but can aid
comprehension for symbol heavy languages.
This configures a ~use-package~ keyword which makes declaring pretty
symbols for language modes incredibly easy. Checkout my [[*Emacs
lisp][Emacs lisp]] configuration for an example.
#+begin_src emacs-lisp
(use-package prog-mode
:demand t
:init
(setq prettify-symbols-unprettify-at-point t)
:config
(with-eval-after-load "use-package-core"
(add-to-list 'use-package-keywords ':pretty)
(defun use-package-normalize/:pretty (_name-symbol _keyword args)
args)
(defun use-package-handler/:pretty (name _keyword args rest state)
(use-package-concat
(use-package-process-keywords name rest state)
(mapcar
#'(lambda (arg)
(let ((mode (car arg))
(rest (cdr arg)))
`(add-hook
',mode
#'(lambda nil
(setq prettify-symbols-alist ',rest)
(prettify-symbols-mode)))))
args)))))
#+end_src
Here's a collection of keywords and possible associated symbols for
any prog language of choice. Mostly for reference and copying.
#+begin_example
("null" . "Ø")
("list" . "ℓ")
("string" . "𝕊")
("char" . "ℂ")
("int" . "ℤ")
("float" . "ℝ")
("!" . "¬")
("for" . "Σ")
("return" . "≡")
("reduce" . "↓")
("map" . "→")
("some" . "∃")
("every" . "∃")
("lambda" . "λ")
("function" . "ƒ")
("<=" . "≤")
(">=" . "≥")
#+end_example
** Tabs
Tabs in vscode are just like buffers in Emacs but way slower and
harder to use. Tabs in Emacs are essentially window layouts, similar
to instances in Tmux. With this setup I can use tabs quite
effectively.
#+begin_src emacs-lisp
(use-package tab-bar
:defer t
:hook (after-init-hook . tab-bar-mode)
:init
(setq tab-bar-close-button-show nil
tab-bar-format '(tab-bar-format-history
tab-bar-format-tabs tab-bar-separator)
tab-bar-show 1
tab-bar-auto-width t
tab-bar-auto-width-max '((100) 20)
tab-bar-auto-width-min '((20) 2))
:general
(tab-leader
"R" #'tab-rename
"c" #'tab-close
"d" #'tab-close
"f" #'tab-detach
"h" #'tab-move-to
"j" #'tab-next
"k" #'tab-previous
"l" #'tab-move
"n" #'tab-new
"r" #'tab-switch
"w" #'tab-window-detach))
#+end_src
** Registers
#+begin_src emacs-lisp
(use-package register
:config
(defmacro +register/jump-to (reg)
`(proc (interactive)
(jump-to-register ,reg)))
:general
(nmmap
"m" #'point-to-register
"'" #'jump-to-register
"g1" (+register/jump-to "1")
"g2" (+register/jump-to "2")
"g3" (+register/jump-to "3")
"g4" (+register/jump-to "4")
"g5" (+register/jump-to "5")
"g6" (+register/jump-to "6")
"g7" (+register/jump-to "7")
"g8" (+register/jump-to "8")
"g9" (+register/jump-to "9")))
#+end_src
** Auto typing
Snippets are a pretty nice way of automatically inserting code. Emacs
provides a few packages by default to do this, but there are great
packages to install as well.
Abbrevs and skeletons make up a popular solution within Emacs default.
Abbrevs are for simple expressions wherein the only input is the key,
and the output is some Elisp function. They provide a lot of inbuilt
functionality and are quite useful. Skeletons, on the other hand, are
for higher level insertions with user influence.
The popular external solution is Yasnippet. Yasnippet is a great
package for snippets, which I use heavily in programming and org-mode.
Here I setup the global mode for yasnippet and a collection of
snippets for ease of use.
*** Abbrevs
Just define a few abbrevs for various date-time operations. Also
define a macro that will assume a function for the expansion, helping
with abstracting a few things away.
#+begin_src emacs-lisp
(use-package abbrev
:defer t
:hook
(prog-mode-hook . abbrev-mode)
(text-mode-hook . abbrev-mode)
:init
(defmacro +abbrev/define-abbrevs (abbrev-table &rest abbrevs)
`(progn
,@(mapcar #'(lambda (abbrev)
`(define-abbrev
,abbrev-table
,(car abbrev)
""
(proc (insert ,(cadr abbrev)))))
abbrevs)))
(setq save-abbrevs nil)
:config
(+abbrev/define-abbrevs
global-abbrev-table
("sdate"
(format-time-string "%Y-%m-%d" (current-time)))
("stime"
(format-time-string "%H:%M:%S" (current-time)))
("sday"
(format-time-string "%A" (current-time)))
("smon"
(format-time-string "%B" (current-time)))))
#+end_src
*** Auto insert
Allows inserting text immediately upon creating a new buffer with a
given name, similar to template. Supports skeletons for inserting
text. To make it easier for later systems to define their own auto
inserts, I define a ~use-package~ keyword (~:auto-insert~) which
allows one to define an entry for ~auto-insert-alist~.
#+begin_src emacs-lisp
(use-package autoinsert
:demand t
:hook (after-init-hook . auto-insert-mode)
:config
(with-eval-after-load "use-package-core"
(add-to-list 'use-package-keywords ':auto-insert)
(defun use-package-normalize/:auto-insert (_name-symbol _keyword args)
args)
(defun use-package-handler/:auto-insert (name _keyword args rest state)
(use-package-concat
(use-package-process-keywords name rest state)
(mapcar
#'(lambda (arg)
`(add-to-list
'auto-insert-alist
',arg))
args)))))
#+end_src
*** Yasnippet
Look at the snippets [[file:../.config/yasnippet/snippets/][folder]]
for all snippets I've got.
#+begin_src emacs-lisp
(use-package yasnippet
:straight t
:defer t
:hook
(prog-mode-hook . yas-minor-mode)
(text-mode-hook . yas-minor-mode)
:general
(insert-leader
"i" #'yas-insert-snippet)
:config
(yas-load-directory (no-littering-expand-etc-file-name
"yasnippet/snippets")))
#+end_src
*** Hydra
Hydra is a great package by =abo-abo= (yes the same guy who made ivy
and swiper). There are two use-package declarations here: one for
~hydra~ itself, and the other for ~use-package-hydra~ which provides
the keyword ~:hydra~ in use-package declarations.
#+begin_src emacs-lisp
(use-package hydra
:straight t)
(use-package use-package-hydra
:straight t)
#+end_src
** Helpful
Helpful provides a modern interface for some common help commands. I
replace ~describe-function~, ~describe-variable~ and ~describe-key~ by
their helpful counterparts.
#+begin_src emacs-lisp
(use-package helpful
:straight t
:defer t
:commands (helpful-callable helpful-variable)
:general
([remap describe-function] #'helpful-callable
[remap describe-variable] #'helpful-variable
[remap describe-key] #'helpful-key)
:display
("\\*helpful.*"
(display-buffer-at-bottom)
(inhibit-duplicate-buffer . t)
(window-height . 0.25))
:config
(evil-define-key 'normal helpful-mode-map "q" #'quit-window))
#+end_src
** Avy and Ace
Avy is a package that provides "jump" functions. Given some input,
the current buffer is scanned and any matching candidates are given a
tag which the user can input to perform some action (usually moving
the cursor to that point).
*** Avy core
Setup avy with leader. As I use ~avy-goto-char-timer~ a lot, use the
~C-s~ bind which replaces isearch. Switch isearch to M-s in case I
need to use it.
#+begin_src emacs-lisp
(use-package avy
:straight t
:defer t
:general
(nmmap
:keymaps 'override
"C-s" #'avy-goto-char-timer
"M-s" #'isearch-forward
"gp" #'avy-copy-region
"gP" #'avy-move-region
"gl" #'avy-goto-line
"gw" #'avy-goto-word-1))
#+end_src
*** Ace window
Though evil provides a great many features in terms of window
management, ace window can provide some nicer chords for higher
management of windows (closing, switching, etc).
#+begin_src emacs-lisp
(use-package ace-window
:straight t
:defer t
:custom
(aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l))
:general
(nmmap
[remap evil-window-next] #'ace-window))
#+end_src
*** Ace link
Avy-style link following!
#+begin_src emacs-lisp
(use-package ace-link
:straight t
:defer t
:general
(nmmap
:keymaps 'override
"gL" #'ace-link))
#+end_src
** Save place
Saves current place in a buffer permanently, so on revisiting the file
(even in a different Emacs instance) you go back to the place you were
at last.
#+begin_src emacs-lisp
(use-package saveplace
:demand t
:config
(save-place-mode))
#+end_src
** Recentf
Recentf provides a method of keeping track of recently opened files.
#+begin_src emacs-lisp
(use-package recentf
:defer t
:hook (after-init-hook . recentf-mode)
:general
(file-leader
"r" #'recentf))
#+end_src
** Memory-report
New feature of Emacs-29, gives a rough report of memory usage with
some details. Useful to know on a long Emacs instance what could be
eating up memory.
#+begin_src emacs-lisp
(use-package memory-report
:defer t
:general
(leader
"qm" #'memory-report))
#+end_src
** Drag Stuff
Drag stuff around, like my favourite russian programmer (Tsoding).
Useful mechanism which works better than any vim motion.
#+begin_src emacs-lisp
(use-package drag-stuff
:straight t
:defer t
:general
(nmmap
"C-M-h" #'drag-stuff-left
"C-M-j" #'drag-stuff-down
"C-M-k" #'drag-stuff-up
"C-M-l" #'drag-stuff-right))
#+end_src
** Searching common directories
Using [[file:elisp/search.el][search.el]] I can search a set of
directories particularly efficiently.
#+begin_src emacs-lisp
(use-package search
:defer t
:load-path "elisp/"
:general
(search-leader
"a" #'+search/search-all)
(file-leader
"p" #'+search/find-file))
#+end_src
** Separedit
Edit anything anywhere all at once!
#+begin_src emacs-lisp
(use-package separedit
:defer t
:straight t
:general
(leader "e" #'separedit)
:init
(setq separedit-default-mode 'org-mode
separedit-remove-trailing-spaces-in-comment t))
#+end_src
* Aesthetics
General look and feel of Emacs (mostly disabling stuff I don't like).
** Themes
I have both a dark and light theme for differing situations. Here I
configure a timer which ensures I have a light theme during the day
and dark theme at night. I wrote my own themes by copying stuff I
like from other themes then modifying them. The dark theme is in
[[file:elisp/personal-solarized-theme.el][this file]] and the light
theme is in [[file:elisp/personal-light-theme.el][this file]].
#+begin_src emacs-lisp
(use-package custom
:defer t
:commands (+oreo/load-theme)
:hook (after-init-hook . +oreo/load-theme)
:init
(setq custom-theme-directory (concat user-emacs-directory "elisp/"))
(defvar +oreo/theme-list `(personal-light personal-solarized))
(defvar +oreo/theme 1)
:config
(defun +oreo/disable-other-themes ()
"Disable all other themes in +OREO/THEME-LIST excluding
+OREO/THEME."
(cl-loop
for theme in +oreo/theme-list
for i from 0
if (not (= i +oreo/theme))
do (disable-theme theme)))
(defun +oreo/load-theme ()
"Load +OREO/THEME, disabling all other themes to reduce conflict."
(mapc #'disable-theme custom-enabled-themes)
(+oreo/disable-other-themes)
(load-theme (nth +oreo/theme +oreo/theme-list) t))
(defun +oreo/switch-theme ()
"Flip between different themes set in +OREO/THEME-ALIST."
(setq +oreo/theme (mod (+ 1 +oreo/theme) (length +oreo/theme-list)))
(+oreo/load-theme))
(+oreo/load-theme))
#+end_src
** Startup screen
The default startup screen is quite bad in all honesty. While for a
first time user it can be very helpful in running the tutorial and
finding more about Emacs, for someone who's already configured it
there isn't much point.
The scratch buffer is created at boot. When the splash screen isn't
enabled, it is the first buffer a user sees. By default, it is in
~lisp-interaction-mode~, which allows one to prototype Emacs Lisp
code.
I mostly use the scratch buffer to hold snippets of code and to write
text (usually then copy-pasted into other applications). So
~text-mode~ is a good fit for that.
2024-06-04: I use to load [[*Org mode][org-mode]] in the scratch
buffer and it added 2 seconds of load time, so let's just use
fundamental mode and call it a day.
#+begin_src emacs-lisp
(use-package emacs
:defer t
:init
(setq inhibit-startup-screen t
inhibit-startup-echo-area-message user-login-name
initial-major-mode 'text-mode
initial-scratch-message ""
ring-bell-function 'ignore)
:config
(add-hook 'after-init-hook
(proc
(with-current-buffer "*scratch*"
(goto-char (point-max))
(-->
(emacs-init-time)
(format "Emacs v%s - %s\n" emacs-version it)
(insert it))))))
#+end_src
** Blinking cursor
Configure the blinking cursor.
#+begin_src emacs-lisp
(use-package frame
:defer t
:init
(setq blink-cursor-delay 0.2)
:config
(blink-cursor-mode))
#+end_src
** Mode line
The mode line is the little bar at the bottom of the buffer, just
above the minibuffer. It can store essentially any text, but
generally details about the current buffer (such as name, major mode,
etc) is placed there.
The default mode-line is... disgusting. It displays information in an
unintelligible format and seems to smash together a bunch of
information without much care for ordering. Most heartbreaking is
that *anything* can seemingly append new information to it without any
purview, which is *REALLY* annoying. It can be very overstimulating
to look at, without even being that immediately informative.
I've got a custom Emacs lisp package
([[file:elisp/better-mode-line.el][here]]) which sets up the default
mode line as a set of 3 segments: left, centre and right. It pads out
the mode line with space strings to achieve this.
#+begin_src emacs-lisp
(use-package better-mode-line
:load-path "elisp/"
:demand t
:init
(defun +mode-line/evil-state ()
"Returns either \"E\" if no evil-state is defined or the first character
of the evil state capitalised"
(with-eval-after-load "evil"
(if (bound-and-true-p evil-state)
(-->
(format "%s" evil-state)
(substring it 0 1)
(upcase it))
"E")))
(setq better-mode-line/left-segment
'(" " ;; Left padding
(:eval
(when (mode-line-window-selected-p)
'("%l:%c" ;; Line and column count
" "
"%p" ;; Percentage into buffer
"[" ;; Evil state
(:eval
(+mode-line/evil-state))
"]"))))
better-mode-line/centre-segment
'("%+" ;; Buffer state (changed or not)
"%b" ;; Buffer name
"(" ;; Major mode
(:eval (format "%s" major-mode))
")")
better-mode-line/right-segment
'((:eval
(when (mode-line-window-selected-p)
(format "%s %s"
(if (project-current) ;; Name of current project (if any)
(project-name
(project-current))
"")
(if vc-mode ;; Project and Git branch
vc-mode
""))
))
mode-line-misc-info ;; Any other information
(:eval ;; Compilation mode errors
(if (eq major-mode 'compilation-mode)
compilation-mode-line-errors))
" " ;; Right padding
))
:config
(better-mode-line/setup-mode-line))
#+end_src
** Fringes
Turning off borders in my window manager was a good idea, so I should
adjust the borders for Emacs, so called fringes. However, some things
like [[info:emacs#Compilation Mode][Compilation Mode]] do require
fringes to provide arrows on the left side of the window. Hence I
provide a minimal fringe style with only 10 pixels on the left
provided.
#+begin_src emacs-lisp
(fringe-mode (cons 10 0))
#+end_src
** Scrolling
When scrolling, editors generally try to keep the cursor on screen.
Emacs has some variables which ensure the cursor is a certain number
of lines above the bottom of the screen and below the top of the
screen when scrolling. Here I set the margin to 8 (so it'll start
correcting at 8) and scroll-conservatively to the same value so it'll
keep the cursor centred.
I also setup the ~pixel-scroll-mode~ to make scrolling nicer looking.
#+begin_src emacs-lisp
(use-package emacs
:init
(setq scroll-conservatively 8
scroll-margin 8
pixel-dead-time nil
pixel-scroll-precision-use-momentum nil
pixel-resolution-fine-flag t
fast-but-imprecise-scrolling t)
:config
(pixel-scroll-mode t)
(pixel-scroll-precision-mode t))
#+end_src
** Display line numbers
I don't really like line numbers, I find them similar to
[[*Fringes][fringes]] (useless space), but at least it provides some
information. Sometimes it can help with doing repeated commands so a
toggle option is necessary.
#+begin_src emacs-lisp
(use-package display-line-numbers
:defer t
:hook ((prog-mode-hook text-mode-hook) . display-line-numbers-mode)
:commands display-line-numbers-mode
:general
(mode-leader
"l" #'display-line-numbers-mode)
:init
(setq-default display-line-numbers-type 'relative))
#+end_src
** Pulsar
Similar to how [[*Evil goggles][Evil goggles]] highlights Evil
actions, pulsar provides more highlighting capabilities. Made by my
favourite Greek philosopher, Prot.
#+begin_src emacs-lisp
(use-package pulsar
:straight t
:init
(setq pulsar-face 'pulsar-cyan
pulsar-pulse-functions
'(next-buffer
previous-buffer
fill-paragraph
drag-stuff-right
drag-stuff-left
drag-stuff-up
drag-stuff-down
evil-goto-first-line
evil-goto-line
evil-scroll-down
evil-scroll-up
evil-scroll-page-down
evil-scroll-page-up
evil-window-left
evil-window-right
evil-window-up
evil-window-down
evil-forward-paragraph
evil-backward-paragraph
evil-fill-and-move
evil-join
org-forward-paragraph
org-backward-paragraph
org-fill-paragraph))
:config
(pulsar-global-mode 1))
#+end_src
** Zoom
Zoom provides a very useful capability: dynamic resizing of windows
based on which one is active. I prefer larger font sizes but make it
too large and it's difficult to have multiple buffers side by side.
This package allows larger font sizes and still have multiple buffers
side by side.
#+begin_src emacs-lisp
(use-package zoom
:straight t
:defer t
:hook (after-init-hook . zoom-mode)
:init
(setq zoom-size '(90 . 25)))
#+end_src
** Hide mode line
Custom minor mode to toggle the mode line. Check it out at
[[file:elisp/hide-mode-line.el][elisp/hide-mode-line.el]].
#+begin_src emacs-lisp
(use-package hide-mode-line
:load-path "elisp/"
:defer t
:general
(mode-leader
"m" #'global-hide-mode-line-mode))
#+end_src
** Olivetti
Olivetti provides a focus mode for Emacs, which makes it look a bit
nicer. It uses margins by default and centres using fill-column. I
actually really like olivetti mode particularly with my [[*Mode
line][centred mode-line]], so I also define a global minor mode which
enables it in all but the minibuffer.
#+begin_src emacs-lisp
(use-package olivetti
:straight t
:defer t
:general
(mode-leader
"o" #'olivetti-global-mode)
:init
(setq-default olivetti-body-width nil
olivetti-minimum-body-width 100
olivetti-style nil)
:config
(define-globalized-minor-mode olivetti-global-mode olivetti-mode
(lambda nil (unless (or (minibufferp)
(string= (buffer-name) "*which-key*"))
(olivetti-mode 1)))))
#+end_src
** All the Icons
Nice set of icons, for even more emojis.
#+begin_src emacs-lisp
(use-package all-the-icons
:straight t
:defer t
:general
(insert-leader
"e" #'all-the-icons-insert))
#+end_src
* Applications
Emacs is an operating system, now with a good text editor through
[[*Evil - Vim emulation][Evil]]. Let's configure some apps for it.
** EWW
Emacs Web Wowser is the inbuilt text based web browser for Emacs. It
can render images and basic CSS styles but doesn't have a JavaScript
engine, which makes sense as it's primarily a text interface.
#+begin_src emacs-lisp
(use-package eww
:defer t
:general
(app-leader
"w" #'eww)
(nmmap
:keymaps 'eww-mode-map
"w" #'evil-forward-word-begin
"Y" #'eww-copy-page-url)
:config
(with-eval-after-load "evil-collection"
(evil-collection-eww-setup)))
#+end_src
** Magit
Magit is *the* git porcelain for Emacs, which perfectly encapsulates
the git CLI. It's so good that some people are use Emacs just for it.
It's difficult to describe it well without using it and it integrates
so well with Emacs that there is very little need to use the git CLI
ever.
In this case I just need to setup the bindings for it. Also, define
an auto insert for commit messages so that I don't need to write
everything myself.
#+begin_src emacs-lisp
(use-package transient
:defer t
:straight (:host github :repo "magit/transient" :tag "v0.7.5"))
(use-package magit
:straight (:host github :repo "magit/magit" :tag "v4.1.0")
:defer t
:display
("magit:.*"
(display-buffer-same-window)
(inhibit-duplicate-buffer . t))
("magit-diff:.*"
(display-buffer-below-selected))
("magit-log:.*"
(display-buffer-same-window))
:general
(leader
"g" '(magit-dispatch :which-key "Magit"))
(code-leader
"b" #'magit-blame)
:init
(setq vc-follow-symlinks t
magit-blame-echo-style 'lines
magit-copy-revision-abbreviated t)
:config
(with-eval-after-load "evil"
(evil-set-initial-state 'magit-status-mode 'motion))
(with-eval-after-load "evil-collection"
(evil-collection-magit-setup)))
#+end_src
** Calendar
Calendar is a simple inbuilt application that helps with date
functionalities. I add functionality to copy dates from the calendar
to the kill ring and bind it to "Y".
#+begin_src emacs-lisp
(use-package calendar
:defer t
:commands (+calendar/copy-date +calendar/toggle-calendar)
:display
("\\*Calendar\\*"
(display-buffer-at-bottom)
(inhibit-duplicate-buffer . t)
(window-height . 0.17))
:general
(nmmap
:keymaps 'calendar-mode-map
"Y" #'+calendar/copy-date)
(app-leader
"d" #'calendar)
:config
(with-eval-after-load "evil-collection"
(evil-collection-calendar-setup))
(defun +calendar/copy-date ()
"Copy date under cursor into kill ring."
(interactive)
(if (use-region-p)
(call-interactively #'kill-ring-save)
(let ((date (calendar-cursor-to-date)))
(when date
(setq date (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date)))
(kill-new (format-time-string "%Y-%m-%d" date)))))))
#+end_src
** Mail
Mail is a funny thing; most people use it just for business or
advertising and it's come out of use in terms of personal
communication in the west for the most part (largely due to "social"
media applications). However, this isn't true for the open source and
free software movement who heavily use mail for communication.
Integrating mail into Emacs helps as I can send source code and
integrate it into my workflow just a bit better. There are a few
ways of doing this, both in built and via package.
*** Notmuch
Notmuch is an application for categorising some local mail system.
It's really fast, has tons of customisable functionality and has good
integration with Emacs. I use ~mbsync~ separately to pull my mail
from the remote server.
#+begin_src emacs-lisp
(use-package notmuch
:straight t
:defer t
:commands (notmuch +mail/flag-thread)
:general
(app-leader "m" #'notmuch)
(nmap
:keymaps 'notmuch-search-mode-map
"f" #'+mail/flag-thread)
:init
(defconst +mail/local-dir (no-littering-expand-var-file-name "mail/"))
(setq notmuch-show-logo nil
notmuch-search-oldest-first nil
notmuch-hello-sections '(notmuch-hello-insert-saved-searches
notmuch-hello-insert-alltags
notmuch-hello-insert-recent-searches)
notmuch-archive-tags '("-inbox" "-unread" "+archive")
message-auto-save-directory +mail/local-dir
message-directory +mail/local-dir)
(with-eval-after-load "evil-collection"
(evil-collection-notmuch-setup))
:config
(defun +mail/flag-thread (&optional unflag beg end)
(interactive (cons current-prefix-arg (notmuch-interactive-region)))
(notmuch-search-tag
(notmuch-tag-change-list '("-inbox" "+flagged") unflag) beg end)
(when (eq beg end)
(notmuch-search-next-thread))))
#+end_src
*** Smtpmail
Setup the smtpmail package, which is used when sending mail. Mostly
custom configuration for integration with other parts of Emacs' mail
system.
#+begin_src emacs-lisp
(use-package smtpmail
:defer t
:commands mail-send
:init
(setq-default
smtpmail-smtp-server "mail.aryadevchavali.com"
smtpmail-smtp-user "aryadev"
smtpmail-smtp-service 587
smtpmail-stream-type 'starttls
send-mail-function #'smtpmail-send-it
message-send-mail-function #'smtpmail-send-it))
#+end_src
*** Mail signature using fortune
Generate a mail signature using the ~fortune~ executable. Pretty
cool!
#+begin_src emacs-lisp
(use-package fortune
:init
(setq fortune-dir "/usr/share/fortune"
fortune-file "/usr/share/fortune/cookie")
:config
(defvar +mail/signature "---------------\nAryadev Chavali\n---------------\n%s")
(defun +mail/make-signature ()
(interactive)
(format +mail/signature
(with-temp-buffer
(let ((fortune-buffer-name (current-buffer)))
(fortune-in-buffer t)
(if (bolp) (delete-char -1))
(buffer-string)))))
(add-hook 'message-setup-hook
(lambda nil (setq message-signature (+mail/make-signature)))))
#+end_src
** Dired
Dired: Directory editor for Emacs. An incredibly nifty piece of
software which deeply integrates with Emacs as a whole. I can't think
of a better file management tool than this.
Here I setup dired with a few niceties
+ Hide details by default (no extra stuff from ~ls~)
+ Omit dot files by default (using ~dired-omit-mode~)
+ If I have two dired windows open, moving or copying files in one
dired instance will automatically target the other dired window
(~dired-dwim~)
+ If opening an application on a PDF file, suggest ~zathura~
+ Examine all the subdirectories within the same buffer
(~+dired/insert-all-subdirectories~)
#+begin_src emacs-lisp
(use-package dired
:defer t
:commands (dired find-dired)
:hook
(dired-mode-hook . auto-revert-mode)
(dired-mode-hook . dired-hide-details-mode)
(dired-mode-hook . dired-omit-mode)
:init
(setq-default dired-listing-switches "-AFBlu --group-directories-first"
dired-omit-files "^\\." ; dotfiles
dired-omit-verbose nil
dired-dwim-target t
dired-kill-when-opening-new-dired-buffer t)
(with-eval-after-load "evil-collection"
(evil-collection-dired-setup))
:general
(nmmap
:keymaps 'dired-mode-map
"SPC" nil
"SPC ," nil
"(" #'dired-hide-details-mode
")" #'dired-omit-mode
"T" #'dired-create-empty-file
"H" #'dired-up-directory
"L" #'dired-find-file)
(leader
"D" #'dired-jump)
(dir-leader
"f" #'find-dired
"d" #'dired
"D" #'dired-other-window
"i" #'image-dired
"b" `(,(proc (interactive)
(dired "~/Text/Books/"))
:which-key "Open Books"))
(local-leader
:keymaps 'dired-mode-map
"i" #'dired-maybe-insert-subdir
"I" #'+dired/insert-all-subdirectories
"o" #'dired-omit-mode
"k" #'dired-prev-subdir
"j" #'dired-next-subdir
"K" #'dired-kill-subdir
"m" #'dired-mark-files-regexp
"u" #'dired-undo)
:config
(add-multiple-to-list dired-guess-shell-alist-user
'("\\.pdf\\'" "zathura")
'("\\.epub\\'" "zathura")
'("\\.jpg\\'" "feh")
'("\\.png\\'" "feh")
'("\\.webm\\'" "mpv")
'("\\.mp[34]\\'" "mpv")
'("\\.mkv\\'" "mpv"))
(defun +dired/insert-all-subdirectories ()
"Insert all subdirectories currently viewable."
(interactive)
(dired-mark-directories nil)
(mapc #'dired-insert-subdir (dired-get-marked-files))
(dired-unmark-all-marks)))
#+end_src
*** image-dired
Image dired is a little cherry on top for Dired: the ability to look
through swathes of images in a centralised fashion while still being
able to do all the usual dired stuff as well is really cool.
#+begin_src emacs-lisp
(use-package dired
:defer t
:init
(setq image-dired-external-viewer "nsxiv")
:general
(nmmap
:keymaps 'image-dired-thumbnail-mode-map
"h" #'image-dired-backward-image
"l" #'image-dired-forward-image
"j" #'image-dired-next-line
"k" #'image-dired-previous-line
"H" #'image-dired-display-previous
"L" #'image-dired-display-next
"RET" #'image-dired-display-this
"m" #'image-dired-mark-thumb-original-file
"q" #'quit-window))
#+end_src
*** fd-dired
Uses fd for finding file results in a directory: ~find-dired~ ->
~fd-dired~.
#+begin_src emacs-lisp
(use-package fd-dired
:straight t
:after dired
:general
(dir-leader
"g" #'fd-dired))
#+end_src
*** wdired
Similar to [[*(Rip)grep][wgrep]] =wdired= provides
the ability to use Emacs motions and editing on file names. This
makes stuff like mass renaming and other file management tasks way
easier than even using the mark based system.
#+begin_src emacs-lisp
(use-package wdired
:after dired
:hook (wdired-mode-hook . undo-tree-mode)
:general
(nmmap
:keymaps 'dired-mode-map
"W" #'wdired-change-to-wdired-mode)
(nmmap
:keymaps 'wdired-mode-map
"ZZ" #'wdired-finish-edit
"ZQ" #'wdired-abort-changes)
:config
(eval-after-load "evil"
;; 2024-09-07: Why does evil-set-initial-state returning a list of modes for
;; normal state make eval-after-load evaluate as if it were an actual
;; expression?
(progn (evil-set-initial-state 'wdired-mode 'normal)
nil)))
#+end_src
*** dired-rsync
Rsync is a great way of transferring files around *nix machines, and I
use dired for all my file management concerns. So I should be able to
rsync stuff around if I want.
#+begin_src emacs-lisp
(use-package dired-rsync
:straight t
:after dired
:general
(nmmap
:keymaps 'dired-mode-map
"M-r" #'dired-rsync))
#+end_src
** EShell
*** Why EShell?
EShell is an integrated shell environment for Emacs, written in Emacs
Lisp. Henceforth I will argue that it is the best shell/command
interpreter to use in Emacs, so good that you should eschew any second
class terminal emulators (~term~, ~shell~, etc).
EShell is unlike the other alternatives in Emacs as it's a /shell/
first, not a terminal emulator (granted, with the ability to spoof
some aspects of a terminal emulator).
The killer benefits of EShell (which would appeal particularly to an
Emacs user) are a direct consequence of EShell being written in Emacs
Lisp:
- strong integration with Emacs utilities (such as ~dired~,
~find-file~, any read functions, etc)
- very extensible, easy to write new commands which leverage Emacs
commands as well as external utilities
- agnostic of platform: "eshell/cd" will call the underlying change
directory function for you, so commands will (usually) mean the same
thing regardless of platform
- this means as long as Emacs can run on an operating system, one
may run EShell
- mixing of Lisp and shell commands, with piping!
However, my favourite feature of EShell is the set of evaluators that
run on command input. Some of the benefits listed above come as a
consequence of this powerful feature.
The main evaluator for any expression for EShell evaluates an
expression by testing the first symbol against different namespaces.
The namespaces are ordered such that if a symbol is not found in one,
the next namespace is tested. These namespaces are:
- alias (defined in the [[file:.config/eshell/aliases][aliases
file]])
- "built-in" command i.e. in the ~eshell/~ namespace of functions
- external command
- Lisp function
You can direct EShell to use these latter two namespaces: any
expression delimited by parentheses is considered a Lisp expression,
and any expression delimited by curly braces is considered an external
command. You may even pipe the results of one into another, allowing
a deeper level of integration between Emacs Lisp and the shell!
*** EShell basics
Setup some niceties of any shell program and some evil-like movements
for easy shell usage, both in and out of insert mode.
NOTE: This mode doesn't allow you to set maps the normal way; you need
to set keybindings on eshell-mode-hook, otherwise it'll just overwrite
them.
#+begin_src emacs-lisp
(use-package eshell
:defer t
:display
("\\*.*eshell\\*"
(display-buffer-same-window)
(reusable-frames . t))
:init
(setq eshell-cmpl-ignore-case t
eshell-cd-on-directory t
eshell-cd-shows-directory nil
eshell-highlight-prompt nil)
(defun +eshell/good-clear ()
(interactive)
(eshell/clear-scrollback)
(eshell-send-input))
(add-hook
'eshell-mode-hook
(proc
(interactive)
;; (nmap
;; :keymaps 'eshell-mode-map
;; "0" #'eshell-bol
;; "I" (proc (interactive)
;; (eshell-bol)
;; (evil-insert 0)))
(general-def
:states '(normal insert visual)
:keymaps 'eshell-mode-map
"M-j" #'eshell-next-prompt
"M-k" #'eshell-previous-prompt
"C-j" #'eshell-next-matching-input-from-input
"C-k" #'eshell-previous-matching-input-from-input
"M-c" #'+eshell/good-clear)
(local-leader
:keymaps 'eshell-mode-map
"c" #'+eshell/good-clear
"k" #'eshell-kill-process))))
#+end_src
*** EShell prompt
Here I use my external library
[[file:elisp/eshell-prompt.el][eshell-prompt]], which provides a
dynamic prompt for EShell. Current features include:
- Git repository details (with difference from remote and number of
modified files)
- Current date and time
- A coloured prompt character which changes colour based on the exit
code of the previous command
NOTE: I don't defer this package because it doesn't use any EShell
internals without autoloading.
#+begin_src emacs-lisp
(use-package eshell-prompt
:load-path "elisp/"
:config
(defun +eshell/banner-message ()
(concat (shell-command-to-string "fortune") "\n"))
(setq eshell-prompt-function #'+eshell-prompt/make-prompt
eshell-banner-message '(+eshell/banner-message)))
#+end_src
*** EShell additions
Using my external library
[[file:elisp/eshell-additions.el][eshell-additions]], I get a few new
internal EShell commands and a command to open EShell at the current
working directory.
NOTE: I don't defer this package because it must be loaded *before*
EShell is. This is because any ~eshell/*~ functions need to be loaded
before launching it.
#+begin_src emacs-lisp
(use-package eshell-additions
:demand t
:load-path "elisp/"
:general
(shell-leader
"t" #'+eshell/open)
(leader
"T" #'+eshell/at-cwd))
#+end_src
*** EShell syntax highlighting
This package external package adds syntax highlighting to EShell
(disabling it for remote work). Doesn't require a lot of config
thankfully.
#+begin_src emacs-lisp
(use-package eshell-syntax-highlighting
:straight t
:after eshell
:hook (eshell-mode-hook . eshell-syntax-highlighting-mode))
#+end_src
** VTerm
There are a few times when EShell doesn't cut it, particularly in the
domain of TUI applications like ~cfdisk~. Emacs comes by default with
some terminal emulators that can run a system wide shell like SH or
ZSH (~shell~ and ~term~ for example), but they're pretty terrible.
~vterm~ is an external package using a shared library for terminal
emulation, and is much better than the default Emacs stuff.
Since my ZSH configuration enables vim emulation, using ~evil~ on top
of it would lead to some weird states. Instead, use the Emacs state
so vim emulation is completely controlled by the shell.
#+begin_src emacs-lisp
(use-package vterm
:straight t
:general
(shell-leader
"v" #'vterm)
:init
(with-eval-after-load "evil"
(evil-set-initial-state 'vterm-mode 'emacs)))
#+end_src
** (Rip)grep
Grep is a great piece of software, a necessary tool in any Linux
user's inventory. Out of the box Emacs has a family of functions
utilising grep which present results in a
[[*Compilation][compilation]] buffer: ~grep~ searches files, ~rgrep~
searches files in a directory using the ~find~ program and ~zgrep~
searches archives.
Ripgrep is a program that attempts to perform better than grep, and it
does. This is because of many optimisations, such as reading
=.gitignore= to exclude certain files from being searched. The
ripgrep package provides utilities to search projects and files. Of
course, this requires installing the rg binary which is available in
most distribution nowadays.
*** Grep
#+begin_src emacs-lisp
(use-package grep
:defer t
:display
("^\\*grep.*"
(display-buffer-reuse-window display-buffer-at-bottom)
(window-height . 0.35)
(reusable-frames . t))
:general
(search-leader
"g" #'grep-this-file
"c" #'grep-config-file
"d" #'rgrep)
(nmmap
:keymaps 'grep-mode-map
"0" #'evil-beginning-of-line
"q" #'quit-window
"i" #'wgrep-change-to-wgrep-mode
"c" #'recompile)
(nmmap
:keymaps 'wgrep-mode-map
"q" #'evil-record-macro
"ZZ" #'wgrep-finish-edit
"ZQ" #'wgrep-abort-changes)
:config
;; Without this wgrep doesn't work properly
(evil-set-initial-state 'grep-mode 'normal)
(defun grep-file (query filename)
(grep (format "grep --color=auto -nIiHZEe \"%s\" -- %s"
query filename)))
(defun grep-this-file ()
(interactive)
(let ((query (read-string "Search for: ")))
(if (buffer-file-name (current-buffer))
(grep-file query (buffer-file-name (current-buffer)))
(let ((temp-file (make-temp-file "temp-grep")))
(write-region (point-min) (point-max) temp-file)
(grep-file query temp-file)))))
(defun grep-config-file ()
(interactive)
(let ((query (read-string "Search for: " "^[*]+ .*")))
(grep-file query (concat user-emacs-directory "config.org")))))
#+end_src
*** rg
#+begin_src emacs-lisp
(use-package rg
:straight t
:defer t
:commands (+rg/project-todo)
:display
("^\\*\\*ripgrep\\*\\*"
(display-buffer-reuse-window display-buffer-at-bottom)
(window-height . 0.35))
:general
(search-leader
"r" #'rg)
(:keymaps 'project-prefix-map
"t" `(+rg/project-todo :which-key "Project TODOs"))
(nmmap
:keymaps 'rg-mode-map
"c" #'rg-recompile
"C" #'rg-rerun-toggle-case
"]]" #'rg-next-file
"[[" #'rg-prev-file
"q" #'quit-window
"i" #'wgrep-change-to-wgrep-mode)
:init
(setq rg-group-result t
rg-hide-command t
rg-show-columns nil
rg-show-header t
rg-custom-type-aliases nil
rg-default-alias-fallback "all"
rg-buffer-name "*ripgrep*")
:config
(defun +rg/project-todo ()
(interactive)
(rg "TODO" "*"
(if (project-current)
(project-root (project-current))
default-directory)))
(evil-set-initial-state 'rg-mode 'normal))
#+end_src
** WAIT Elfeed
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
Elfeed is the perfect RSS feed reader, integrated into Emacs
perfectly. I've got a set of feeds that I use for a large variety of
stuff, mostly media and entertainment. I've also bound "<leader> ar"
to elfeed for loading the system.
#+begin_src emacs-lisp
(use-package elfeed
:straight t
:general
(app-leader "r" #'elfeed)
(nmmap
:keymaps 'elfeed-search-mode-map
"gr" #'elfeed-update
"s" #'elfeed-search-live-filter
"<return>" #'elfeed-search-show-entry)
:init
(setq elfeed-db-directory (no-littering-expand-var-file-name "elfeed/"))
(setq +rss/feed-urls
'(("Arch Linux"
"https://www.archlinux.org/feeds/news/"
News Technology)
("The Onion"
"https://www.theonion.com/rss"
Social)
("Protesilaos Stavrou"
"https://www.youtube.com/@protesilaos"
YouTube Technology)
("Tsoding Daily"
"https://www.youtube.com/feeds/videos.xml?channel_id=UCrqM0Ym_NbK1fqeQG2VIohg"
YouTube Technology)
("Tsoding"
"https://www.youtube.com/feeds/videos.xml?channel_id=UCrqM0Ym_NbK1fqeQG2VIohg"
YouTube Technology)
("Nexpo"
"https://www.youtube.com/feeds/videos.xml?channel_id=UCpFFItkfZz1qz5PpHpqzYBw"
YouTube Stories)
("3B1B"
"https://www.youtube.com/feeds/videos.xml?channel_id=UCYO_jab_esuFRV4b17AJtAw"
YouTube)
("Fredrik Knusden"
"https://www.youtube.com/feeds/videos.xml?channel_id=UCbWcXB0PoqOsAvAdfzWMf0w"
YouTube Stories)
("Barely Sociable"
"https://www.youtube.com/feeds/videos.xml?channel_id=UC9PIn6-XuRKZ5HmYeu46AIw"
YouTube Stories)
("Atrocity Guide"
"https://www.youtube.com/feeds/videos.xml?channel_id=UCn8OYopT9e8tng-CGEWzfmw"
YouTube Stories)
("Hacker News"
"https://news.ycombinator.com/rss"
Social News Technology)
("Hacker Factor"
"https://www.hackerfactor.com/blog/index.php?/feeds/index.rss2"
Social)))
:config
(with-eval-after-load "evil-collection"
(evil-collection-elfeed-setup))
(setq elfeed-feeds (cl-map 'list #'(lambda (item)
(append (list (nth 1 item)) (cdr (cdr item))))
+rss/feed-urls))
(advice-add 'elfeed-search-show-entry :after #'+elfeed/dispatch-entry)
(defun +elfeed/dispatch-entry (entry)
"Process each type of entry differently.
e.g., you may want to open HN entries in eww."
(let ((url (elfeed-entry-link entry)))
(pcase url
((pred (string-match-p "https\\:\\/\\/www.youtube.com\\/watch"))
(mpv-play-url url))
(_ (eww url))))))
#+end_src
** IBuffer
IBuffer is the dired of buffers. Nothing much else to be said.
#+begin_src emacs-lisp
(use-package ibuffer
:defer t
:general
(buffer-leader
"i" #'ibuffer)
:config
(with-eval-after-load "evil-collection"
(evil-collection-ibuffer-setup)))
#+end_src
** Proced
Emacs has two systems for process management:
+ proced: a general 'top' like interface which allows general
management of linux processes
+ list-processes: a specific Emacs based system that lists processes
spawned by Emacs (similar to a top for Emacs specifically)
Core Proced config, just a few bindings and evil collection setup.
#+begin_src emacs-lisp
(use-package proced
:defer t
:general
(app-leader
"p" #'proced)
(nmap
:keymaps 'proced-mode-map
"za" #'proced-toggle-auto-update)
:display
("\\*Proced\\*"
(display-buffer-at-bottom)
(window-height . 0.25))
:init
(setq proced-auto-update-interval 5)
:config
(with-eval-after-load "evil-collection"
(evil-collection-proced-setup)))
#+end_src
** Calculator
~calc-mode~ is a calculator system within Emacs that provides a
diverse array of mathematical operations. It uses reverse polish
notation, but there is a standard infix algebraic notation mode so
don't be too shocked. It can do a surprising amount of stuff, such
as:
+ finding derivatives/integrals of generic equations
+ matrix operations
+ finding solutions for equations, such as for finite degree multi
variable polynomials
Perhaps most powerful is ~embedded-mode~. This allows one to perform
computation within a non ~calc-mode~ buffer. Surround any equation
with dollar signs and call ~(calc-embedded)~ with your cursor on it to
compute it. It'll replace the equation with the result it computed.
This is obviously incredibly useful; I don't even need to leave the
current buffer to perform some quick mathematics in it.
#+begin_src emacs-lisp
(use-package calc
:defer t
:display
("*Calculator*"
(display-buffer-at-bottom)
(window-height . 0.2))
:general
(app-leader
"c" #'calc-dispatch)
:init
(setq calc-algebraic-mode t)
:config
(with-eval-after-load "evil-collection"
(evil-collection-calc-setup)))
#+end_src
** Zone
Emacs' out of the box screensaver software.
#+begin_src emacs-lisp
(use-package zone
:defer t
:commands (zone)
:general
(leader
"z" #'zone)
:init
(setq zone-programs
[zone-pgm-drip
zone-pgm-drip-fretfully]))
#+end_src
** (Wo)man
Man pages are the user manuals for most software on Linux. Of course,
Emacs comes out of the box with a renderer for man pages and some
searching capabilities.
2023-08-17: `Man-notify-method' is the reason the `:display' record
doesn't work here. I think it's to do with how Man pages are rendered
or something, but very annoying as it's a break from standards!
2024-10-08: Man pages are rendered via a separate process, which is
why this is necessary.
#+begin_src emacs-lisp
(use-package man
:defer t
:init
(setq Man-notify-method 'thrifty)
:display
("\\*Man.*"
(display-buffer-reuse-mode-window display-buffer-same-window)
(mode . Man-mode))
:general
(file-leader
"m" #'man) ;; kinda like "find man page"
(nmmap
:keymaps 'Man-mode-map
"RET" #'man-follow))
#+end_src
** Info
Info is GNU's attempt at better man pages. Most Emacs packages have
info pages so I'd like nice navigation options.
#+begin_src emacs-lisp
(use-package info
:defer t
:general
(nmmap
:keymaps 'Info-mode-map
"h" #'evil-backward-char
"k" #'evil-previous-line
"l" #'evil-forward-char
"H" #'Info-history-back
"L" #'Info-history-forward
"C-j" #'Info-forward-node
"C-k" #'Info-backward-node
"RET" #'Info-follow-nearest-node
"m" #'Info-menu
"C-o" #'Info-history-back
"s" #'Info-search
"S" #'Info-search-case-sensitively
"i" #'Info-index
"a" #'info-apropos
"gj" #'Info-next
"gk" #'Info-prev
"g?" #'Info-summary
"q" #'quit-window)
:init
(with-eval-after-load "evil"
(evil-set-initial-state 'Info-mode 'normal)))
#+end_src
** WAIT gif-screencast
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
Little application that uses =gifsicle= to make essentially videos of
Emacs. Useful for demonstrating features.
#+begin_src emacs-lisp
(use-package gif-screencast
:straight t
:general
(app-leader
"x" #'gif-screencast-start-or-stop)
:init
(setq gif-screencast-output-directory (expand-file-name "~/Media/emacs/")))
#+end_src
** Image-mode
Image mode, for viewing images. Supports tons of formats, easy to use
and integrates slickly into image-dired. Of course,
#+begin_src emacs-lisp
(use-package image-mode
:defer t
:general
(nmmap
:keymaps 'image-mode-map
"+" #'image-increase-size
"-" #'image-decrease-size
"a" #'image-toggle-animation
"p+" #'image-increase-speed
"p-" #'image-increase-speed
"h" #'image-backward-hscroll
"j" #'image-next-line
"k" #'image-previous-line
"l" #'image-forward-hscroll))
#+end_src
** empv
Emacs MPV bindings, with very cool controls for queuing files for
playing.
#+begin_src emacs-lisp
(use-package empv
:straight t
:defer t
:init
(setq empv-audio-dir (expand-file-name "~/Media/audio")
empv-video-dir (expand-file-name "~/Media/videos")
empv-audio-file-extensions (list "mp3" "ogg" "wav" "m4a"
"flac" "aac" "opus")
empv-video-file-extensions (list "mkv" "mp4" "avi" "mov"
"webm"))
:hydra
(empv-hydra
nil "Hydra for EMPV"
("(" #'empv-chapter-prev
"chapter-prev" :column "playback")
(")" #'empv-chapter-next
"chapter-next" :column "playback")
("0" #'empv-volume-up
"volume-up" :column "playback")
("9" #'empv-volume-down
"volume-down" :column "playback")
("[" #'empv-playback-speed-down
"playback-speed-down" :column "playback")
("]" #'empv-playback-speed-up
"playback-speed-up" :column "playback")
("_" #'empv-toggle-video
"toggle-video" :column "playback")
("q" #'empv-exit
"exit" :column "playback")
("s" #'empv-seek
"seek" :column "playback")
("t" #'empv-toggle
"toggle" :column "playback")
("x" #'empv-chapter-select
"chapter-select" :column "playback")
("N" #'empv-playlist-prev
"playlist-prev" :column "playlist")
("C" #'empv-playlist-clear
"playlist-clear" :column "playlist")
("n" #'empv-playlist-next
"playlist-next" :column "playlist")
("p" #'empv-playlist-select
"playlist-select" :column "playlist")
("S" #'empv-playlist-shuffle
"playlist-shuffle" :column "playlist")
("a" #'empv-play-audio
"play-audio" :column "play")
("R" #'empv-play-random-channel
"play-random-channel" :column "play")
("d" #'empv-play-directory
"play-directory" :column "play")
("f" #'empv-play-file
"play-file" :column "play")
("o" #'empv-play-or-enqueue
"play-or-enqueue" :column "play")
("r" #'empv-play-radio
"play-radio" :column "play")
("v" #'empv-play-video
"play-video" :column "play")
("i" #'empv-display-current
"display-current" :column "misc")
("l" #'empv-log-current-radio-song-name
"log-current-radio-song-name" :column "misc")
("c" #'empv-copy-path
"copy-path" :column "misc")
("Y" #'empv-youtube-last-results
"youtube-last-results" :column "misc")
("y" #'empv-youtube
"youtube" :column "misc"))
:general
(app-leader
"e" #'empv-hydra/body))
#+end_src
** Grand Unified Debugger (GUD)
GUD is a system for debugging, hooking into processes and
providing an interface to the user all in Emacs. Here I define a
hydra which provides a ton of the useful =gud= keybindings that exist
in an Emacs-only map.
#+begin_src emacs-lisp
(use-package gud
:general
:after hydra
:hydra
(gud-hydra
(:hint nil) "Hydra for GUD"
("<" #'gud-up "Up"
:column "Control Flow")
(">" #'gud-down "Down"
:column "Control Flow")
("b" #'gud-break "Break"
:column "Breakpoints")
("d" #'gud-remove "Remove"
:column "Breakpoints")
("f" #'gud-finish "Finish"
:column "Control Flow")
("J" #'gud-jump "Jump"
:column "Control Flow")
("L" #'gud-refresh "Refresh"
:column "Misc")
("n" #'gud-next "Next"
:column "Control Flow")
("p" #'gud-print "Print"
:column "Misc")
("c" #'gud-cont "Cont"
:column "Control Flow")
("s" #'gud-step "Step"
:column "Control Flow")
("t" #'gud-tbreak "Tbreak"
:column "Control Flow")
("u" #'gud-until "Until"
:column "Control Flow")
("v" #'gud-go "Go"
:column "Control Flow")
("w" #'gud-watch "Watch"
:column "Breakpoints")
("TAB" #'gud-stepi "Stepi"
:column "Control Flow"))
:general
(code-leader "d" #'gud-hydra/body
"D" #'gud-gdb))
#+end_src
** WAIT esup
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
I used to be able to just use
[[file:elisp/profiler-dotemacs.el][profile-dotemacs.el]], when my
Emacs config was smaller, but now it tells me very little information
about where my setup is inefficient due to the literate config. Just
found this ~esup~ thing and it works perfectly, exactly how I would
prefer getting this kind of information. It runs an external Emacs
instance and collects information from it, so it doesn't require
restarting Emacs to profile, and I can compile my configuration in my
current instance to test it immediately.
2023-10-16: Unless I'm doing some optimisations or tests, I don't
really need this in my config at all times. Enable when needed.
#+begin_src emacs-lisp
(use-package esup
:straight t
:defer t
:general
(leader
"qe" #'esup))
#+end_src
* Text modes
Standard packages and configurations for text-mode and its derived
modes.
** Flyspell
Flyspell allows me to spell check text documents. I use it primarily
in org mode, as that is my preferred prose writing software, but I
also need it in commit messages and so on, thus it should really hook
into text-mode.
#+begin_src emacs-lisp
(use-package flyspell
:defer t
:hook ((org-mode-hook text-mode-hook) . flyspell-mode)
:general
(nmmap
:keymaps 'text-mode-map
"M-C" #'flyspell-correct-word-before-point
"M-c" #'flyspell-auto-correct-word)
(mode-leader
"s" #'flyspell-mode))
#+end_src
** Undo tree
Undo tree sits on top of Emacs' undo capabilities. It provides a nice
visual for the history of a buffer and is a great way to produce
branches of edits. This history may be saved to and loaded from the
disk, which makes Emacs a quasi version control system in and of
itself. The only feature left is describing changes...
#+begin_src emacs-lisp
(use-package undo-tree
:demand t
:straight t
:general
(leader
"u" #'undo-tree-visualize)
:init
(setq undo-tree-auto-save-history t
undo-tree-history-directory-alist backup-directory-alist)
:config
(global-undo-tree-mode))
#+end_src
** Whitespace
Deleting whitespace, highlighting when going beyond the 80th character
limit, all good stuff. I don't want to highlight whitespace for
general mode categories (Lisp shouldn't really have an 80 character
limit), so set it for specific modes need the help.
#+begin_src emacs-lisp
(use-package whitespace
:defer t
:general
(nmmap
"M--" #'whitespace-cleanup)
(mode-leader
"w" #'whitespace-mode)
:hook
(before-save-hook . whitespace-cleanup)
((c-mode-hook c++-mode-hook haskell-mode-hook python-mode-hook
org-mode-hook text-mode-hook js-mode-hook)
. whitespace-mode)
:init
(setq whitespace-line-column nil
whitespace-style '(face empty spaces tabs newline trailing lines-char
tab-mark)))
#+end_src
** Filling and displaying fills
The fill-column is the number of characters that should be in a single
line of text before doing a hard wrap. The default case is 80
characters for that l33t Unix hard terminal character limit. I like
different fill-columns for different modes: text modes should really
use 70 fill columns while code should stick to 80.
#+begin_src emacs-lisp
(use-package emacs
:init
(setq-default fill-column 80)
(add-hook 'text-mode-hook (proc (setq-local fill-column 70)))
:hook
(text-mode-hook . auto-fill-mode)
((c-mode-hook c++-mode-hook haskell-mode-hook python-mode-hook
org-mode-hook text-mode-hook js-mode-hook)
. display-fill-column-indicator-mode))
#+end_src
** Show-paren-mode
Show parenthesis for Emacs
#+begin_src emacs-lisp
(use-package paren
:hook (prog-mode-hook . show-paren-mode))
#+end_src
** Smartparens
Smartparens is a smarter electric-parens, it's much more aware of
context and easier to use.
#+begin_src emacs-lisp
(use-package smartparens
:straight t
:defer t
:hook
(prog-mode-hook . smartparens-mode)
(text-mode-hook . smartparens-mode)
:config
(setq sp-highlight-pair-overlay nil
sp-highlight-wrap-overlay t
sp-highlight-wrap-tag-overlay t)
(let ((unless-list '(sp-point-before-word-p
sp-point-after-word-p
sp-point-before-same-p)))
(sp-pair "'" nil :unless unless-list)
(sp-pair "\"" nil :unless unless-list))
(sp-local-pair sp-lisp-modes "(" ")" :unless '(:rem sp-point-before-same-p))
(require 'smartparens-config))
#+end_src
** Powerthesaurus
Modern package for thesaurus in Emacs with a transient + hydra.
#+begin_src emacs-lisp
(use-package powerthesaurus
:defer t
:straight t
:general
(search-leader
"w" #'powerthesaurus-transient))
#+end_src
** lorem ipsum
Sometimes you need placeholder text for some UI or document. Pretty
easy to guess what text I'd use.
#+begin_src emacs-lisp
(use-package lorem-ipsum
:straight t
:general
(insert-leader
"p" #'lorem-ipsum-insert-paragraphs))
#+end_src
* Programming packages
Packages that help with programming in general, providing IDE like
capabilities.
** Eldoc
Eldoc presents documentation to the user upon placing ones cursor upon
any symbol. This is very useful when programming as it:
- presents the arguments of functions while writing calls for them
- presents typing and documentation of variables
Eldoc box makes the help buffer a hovering box instead of printing it
in the minibuffer. A lot cleaner.
2024-05-31: Eldoc box is a bit useless now that I'm not using frames.
I prefer the use of the minibuffer for printing documentation now.
#+begin_src emacs-lisp
(use-package eldoc
:defer t
:hook (prog-mode-hook . eldoc-mode)
:init
(global-eldoc-mode 1)
:general
(leader
"h>" #'eldoc-doc-buffer))
#+end_src
** Flycheck
Flycheck is the checking system for Emacs. I don't necessarily like
having all my code checked all the time, so I haven't added a hook to
prog-mode as it would be better for me to decide when I want checking
and when I don't. Many times Flycheck is annoying when checking a
program, particularly one which isn't finished yet.
#+begin_src emacs-lisp
(use-package flycheck
:straight t
:defer t
:commands (flycheck-mode flycheck-list-errors)
:general
(mode-leader
"f" #'flycheck-mode)
(code-leader
"x" #'flycheck-list-errors
"j" #'flycheck-next-error
"k" #'flycheck-previous-error)
:display
("\\*Flycheck.*"
(display-buffer-at-bottom)
(window-height . 0.25))
:init
(setq-default flycheck-check-syntax-automatically '(save idle-change new-line mode-enabled))
:config
(with-eval-after-load "evil-collection"
(evil-collection-flycheck-setup)))
#+end_src
** Eglot
Eglot is package to communicate with LSP servers for better
programming capabilities. Interactions with a server provide results
to the client, done through JSON.
NOTE: Emacs 28.1 comes with better JSON parsing, which makes Eglot
much faster.
2023-03-26: I've found Eglot to be useful sometimes, but many of the
projects I work on don't require a heavy server setup to efficiently
edit and check for errors; Emacs provides a lot of functionality. So
by default I've disabled it, using =M-x eglot= to startup the LSP
server when I need it.
2024-06-27: In projects where I do use eglot and I know I will need it
regardless of file choice, I prefer setting it at the dir-local level
via an eval form. So I add to the safe values for the eval variable
to be set.
#+begin_src emacs-lisp
(use-package eglot
:defer t
:general
(code-leader
:keymaps 'eglot-mode-map
"f" #'eglot-format
"a" #'eglot-code-actions
"r" #'eglot-rename
"R" #'eglot-reconnect)
:init
(setq eglot-auto-shutdown t
eglot-stay-out-of '(flymake)
eglot-ignored-server-capabilities '(:documentHighlightProvider
:documentOnTypeFormattingProvider
:inlayHintProvider))
(add-to-list 'safe-local-variable-values '(eval eglot-ensure)))
#+end_src
** Indentation
By default, turn off tabs and set the tab width to two.
#+begin_src emacs-lisp
(setq-default indent-tabs-mode nil
tab-width 2)
#+end_src
However, if necessary later, define a function that may activate tabs locally.
#+begin_src emacs-lisp
(defun +oreo/use-tabs ()
(interactive)
(setq-local indent-tabs-mode t))
#+end_src
** Highlight todo items
TODO items are highlighted in org-mode, but not necessarily in every
mode. This minor mode highlights all TODO like items via a list of
strings to match. It also configures faces to use when highlighting.
I hook it to prog-mode.
#+begin_src emacs-lisp
(use-package hl-todo
:straight t
:after prog-mode
:hook (prog-mode-hook . hl-todo-mode)
:init
(setq hl-todo-keyword-faces
'(("TODO" . "#E50000")
("WIP" . "#ffa500")
("NOTE" . "#00CC00")
("FIXME" . "#d02090"))))
#+end_src
** Hide-show mode
Turn on ~hs-minor-mode~ for all prog-mode. This provides folds for
free.
#+begin_src emacs-lisp
(use-package hideshow
:defer t
:hook (prog-mode-hook . hs-minor-mode))
#+end_src
** Aggressive indenting
Essentially my dream editing experience: when I type stuff in, try and
indent it for me on the fly. Just checkout the
[[https://github.com/Malabarba/aggressive-indent-mode][page]], any
description I give won't do it justice.
#+begin_src emacs-lisp
(use-package aggressive-indent
:straight t
:demand t
:config
(add-multiple-to-list aggressive-indent-excluded-modes
'c-mode 'c++-mode 'cc-mode
'asm-mode 'js-mode 'typescript-mode
'tsx-mode 'typescript-ts-mode 'tsx-ts-mode
'tuareg-mode)
(global-aggressive-indent-mode))
#+end_src
** Compilation
Compilation mode is an incredibly useful subsystem of Emacs which
allows one to run arbitrary commands. If those commands produce
errors (particularly errors that have a filename, column and line)
compilation-mode can colourise these errors and help you navigate to
them.
Here I add some bindings and a filter which colourises the output of
compilation mode for ANSI escape sequences; the eyecandy is certainly
nice but it's very useful when dealing with tools that use those codes
so you can actually read the text.
#+begin_src emacs-lisp
(use-package compile
:defer t
:display
("\\*compilation\\*"
(display-buffer-reuse-window display-buffer-at-bottom)
(window-height . 0.1)
(reusable-frames . t))
:hydra
(move-error-hydra
(:hint nil) "Hydra for moving between errors"
("j" #'next-error)
("k" #'previous-error))
:general
(leader
"j" #'move-error-hydra/next-error
"k" #'move-error-hydra/previous-error)
(code-leader
"c" #'compile
"C" #'recompile)
(:keymaps 'compilation-mode-map
"g" nil) ;; by default this is recompile
(nmmap
:keymaps 'compilation-mode-map
"c" #'recompile)
:init
(setq compilation-scroll-output 'first-error
compilation-context-lines nil
next-error-highlight 'fringe-arrow)
:config
(add-hook 'compilation-filter-hook #'ansi-color-compilation-filter))
#+end_src
** xref
Find definitions, references and general objects using tags without
external packages. Provided out of the box with Emacs, but requires a
way of generating a =TAGS= file for your project (look at
[[*Project.el][Project.el]] for my way of doing so). A critical
component in a minimal setup for programming without heavier systems
like [[*Eglot][Eglot]].
#+begin_src emacs-lisp
(use-package xref
:defer t
:display
("\\*xref\\*"
(display-buffer-at-bottom)
(inhibit-duplicate-buffer . t)
(window-height . 0.3))
:general
(code-leader
"t" '(nil :which-key "Tags"))
(code-leader
:infix "t"
"t" #'xref-find-apropos
"d" #'xref-find-definitions
"r" #'xref-find-references)
(nmmap
:keymaps 'xref--xref-buffer-mode-map
"RET" #'xref-goto-xref
"J" #'xref-next-line
"K" #'xref-prev-line
"r" #'xref-query-replace-in-results
"gr" #'xref-revert-buffer
"q" #'quit-window))
#+end_src
** Project.el
An out of the box system for managing projects. Where possible we
should try to use Emacs defaults, so when setting up on a new computer
it takes a bit less time.
Here I:
+ Bind ~project-prefix-map~ to "<leader>p"
+ write a TAGS command, mimicking projectile's one, so I can quickly
generate them.
+ Bind that to "<leader>pr"
#+begin_src emacs-lisp
(use-package project
:defer t
:general
(:keymaps 'project-prefix-map
"r" #'+project/generate-tags)
(leader
"p" `(,project-prefix-map :which-key "Project"))
:config
(defun +project/command (folder)
(format "ctags -Re -f %sTAGS %s*"
folder folder))
(defun +project/root ()
(if (project-current)
(project-root (project-current))
default-directory))
(defun +project/generate-tags ()
(interactive)
(set-process-sentinel
(start-process-shell-command
"PROJECT-GENERATE-TAGS"
"*tags*"
(+project/command (+project/root)))
(lambda (p event)
(when (string= event "finished\n")
(message "Finished generating tags!")
(visit-tags-table (format "%sTAGS" (+project/root))))))))
#+end_src
** devdocs
When man pages aren't enough, you need some documentation lookup
system (basically whenever your using anything but C/C++/Bash).
[[https://devdocs.io][Devdocs]] is a great little website that
provides a ton of documentation sets. There's an Emacs package for it
which works well and downloads documentation sets to my machine, which
is nice.
#+begin_src emacs-lisp
(use-package devdocs
:straight t
:defer t
:general
(file-leader
"d" #'devdocs-lookup))
#+end_src
** rainbow-delimiters
Makes colours delimiters (parentheses) based on their depth in an
expression. Rainbow flag in your Lisp source code.
#+begin_src emacs-lisp
(use-package rainbow-delimiters
:defer t
:straight t
:general
(mode-leader "r" #'rainbow-delimiters-mode)
:hook
((lisp-mode-hook emacs-lisp-mode-hook racket-mode-hook) . rainbow-delimiters-mode))
#+end_src
** Licensing
Loads [[file:elisp/license.el][license.el]] for inserting licenses.
Licenses are important for distribution and attribution to be defined
clearly.
#+begin_src emacs-lisp
(use-package license
:demand t
:load-path "elisp/"
:general
(insert-leader
"l" #'+license/insert-copyright-notice
"L" #'+license/insert-complete-license))
#+end_src
** diff mode
Oh diffs; the way of the ancient ones. Nowadays we use our newfangled
"pull requests" and "cool web interfaces" to manage changes in our
code repositories, but old school projects use patches to make code
changes. They're a pain to distribute and can be very annoying to use
when applying them to code. Even then I somewhat like patches, if
only for their simplicity.
[[https://git.aryadevchavali.com/dwm][dwm]] uses patches for adding
new features and Emacs has great functionality to work with patches
effectively. Here I configure ~diff-mode~, which provides most of
this cool stuff, to be a bit more ergonomic with ~evil~.
#+begin_src emacs-lisp
(use-package diff-mode
:general
(nmmap
:keymaps 'diff-mode-map
"}" #'diff-hunk-next
"{" #'diff-hunk-prev
"RET" #'diff-goto-source))
#+end_src
* Org mode
Org is, at its most basic, a markup language. =org-mode= is a major
mode for Emacs to interpret org buffers. org-mode provides a lot of
capabilities, some are:
+ A complete table based spreadsheet system, with formulas (including
[[*Calculator][calc-mode]] integration)
+ Code blocks with proper syntax highlighting and editing experience
+ Evaluation
+ Export of code blocks to a variety of formats
+ Export of code blocks to a code file (so called "tangling", which
is what occurs in this document)
+ Feature complete scheduling system with [[*Calendar][calendar]]
integration
+ A clock-in system to time tasks
+ TODO system
+ Export to a variety of formats or make your own export engine using
the org AST.
+ Inline $\LaTeX$, with the ability to render the fragments on
demand within the buffer
+ Links to a variety of formats:
+ Websites (via http or https)
+ FTP
+ SSH
+ Files (even to a specific line)
+ Info pages
I'd argue this is a bit more than a markup language. Like
[[*Magit][Magit]], some use Emacs just for this system.
** Org Essentials
Org has a ton of settings to tweak, which change your experience quite
a bit. Here are mine, but this took a lot of just reading other
people's configurations and testing. I don't do a good job of
explaining how this works in all honesty, but it works well for me so
I'm not very bothered.
+ By default =~/Text= is my directory for text files. I actually have
a repository that manages this directory for agenda files and other
documents
+ Indentation in file should not be allowed, i.e. text indentation, as
that forces other editors to read it a certain way as well. It's
obtrusive hence it's off.
+ Org startup indented is on by default as most documents do benefit
from the indentation, but I do turn it off for some files via
~#+startup:noindent~
+ When opening an org document there can be a lot of headings, so I
set folding to just content
+ Org documents can also have a lot of latex previews, which make
opening some after a while a massive hassle. If I want to see the
preview, I'll do it myself, so turn it off.
+ Org manages windowing itself, to some extent, so I set those options
to be as unobtrusive as possible
+ Load languages I use in =src= blocks in org-mode (Emacs-lisp for
this configuration, C and Python)
#+begin_src emacs-lisp
(use-package org
:defer t
:init
(setq org-directory "~/Text/"
org-adapt-indentation nil
org-indent-mode nil
org-startup-indented nil
org-startup-folded 'showeverything
org-startup-with-latex-preview nil
org-imenu-depth 10
org-src-window-setup 'current-window
org-indirect-buffer-display 'current-window
org-link-frame-setup '((vm . vm-visit-folder-other-frame)
(vm-imap . vm-visit-imap-folder-other-frame)
(file . find-file))
org-babel-load-languages '((emacs-lisp . t)
(lisp . t)
(shell . t))))
#+end_src
** Org Latex
Org mode has deep integration with latex, can export to PDF and even
display latex fragments in the document directly. I setup the
pdf-process, code listing options via minted and the format options
for latex fragments.
#+begin_src emacs-lisp
(use-package org
:defer t
:init
(setq org-format-latex-options
'(:foreground default :background "Transparent" :scale 2
:html-foreground "Black" :html-background "Transparent"
:html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
org-latex-src-block-backend 'minted
org-latex-minted-langs '((emacs-lisp "common-lisp")
(ledger "text")
(cc "c++")
(cperl "perl")
(shell-script "bash")
(caml "ocaml"))
org-latex-packages-alist '(("" "minted"))
org-latex-pdf-process
(list (concat "latexmk -f -bibtex -pdf "
"-shell-escape -%latex -interaction=nonstopmode "
"-output-directory=%o %f"))
org-latex-minted-options
'(("style" "colorful")
("linenos")
("frame" "single")
("mathescape")
("fontfamily" "courier")
("samepage" "false")
("breaklines" "true")
("breakanywhere" "true"))))
#+end_src
** Org Core Variables
Tons of variables for org-mode, including a ton of latex ones. Can't
really explain because it sets up quite a lot of local stuff. Also I
copy pasted the majority of this, tweaking it till it felt good. Doom
Emacs was very helpful here.
#+begin_src emacs-lisp
(use-package org
:defer t
:init
(setq org-edit-src-content-indentation 0
org-eldoc-breadcrumb-separator " → "
org-enforce-todo-dependencies t
org-export-backends '(ascii html latex odt icalendar)
org-fontify-quote-and-verse-blocks t
org-fontify-whole-heading-line t
org-footnote-auto-label t
org-hide-emphasis-markers nil
org-hide-leading-stars t
org-image-actual-width nil
org-imenu-depth 10
org-link-descriptive nil
org-priority-faces '((?A . error) (?B . warning) (?C . success))
org-refile-targets '((nil . (:maxlevel . 2)))
org-tags-column 0
org-todo-keywords '((sequence "TODO" "WIP" "DONE")
(sequence "PROJ" "WAIT" "COMPLETE"))
org-use-sub-superscripts '{}))
#+end_src
** Org Core Functionality
Hooks, prettify-symbols and records for auto insertion.
#+begin_src emacs-lisp
(use-package org
:defer t
:hook
(org-mode-hook . prettify-symbols-mode)
:display
("\\*Org Src.*"
(display-buffer-same-window))
:auto-insert
(("\\.org\\'" . "Org skeleton")
"Enter title: "
"#+title: " str | (buffer-file-name) "\n"
"#+author: " (read-string "Enter author: ") | user-full-name "\n"
"#+description: " (read-string "Enter description: ") | "Description" "\n"
"#+date: " (format-time-string "%Y-%m-%d" (current-time)) "\n"
"* " _))
#+end_src
** Org Core Bindings
A load of bindings for org-mode which binds together a lot of
functionality. It's best to read it yourself; to describe it is to
write the code.
#+begin_src emacs-lisp
(use-package org
:defer t
:config
(with-eval-after-load "consult"
(general-def
:keymaps 'org-mode-map
[remap consult-imenu] #'consult-outline))
:general
(file-leader
"l" #'org-store-link
"i" #'org-insert-last-stored-link)
(code-leader
:keymaps 'emacs-lisp-mode-map
"D" #'org-babel-detangle)
(local-leader
:state '(normal motion)
:keymaps 'org-src-mode-map
"o" #'org-edit-src-exit)
(local-leader
:keymaps 'org-mode-map
"l" '(nil :which-key "Links")
"'" '(nil :which-key "Tables")
"c" '(nil :which-key "Clocks")
"r" #'org-refile
"d" #'org-date-from-calendar
"t" #'org-todo
"," #'org-priority
"T" #'org-babel-tangle
"i" #'org-insert-structure-template
"p" #'org-latex-preview
"s" #'org-property-action
"e" #'org-export-dispatch
"o" #'org-edit-special
"O" #'org-open-at-point)
(local-leader
:keymaps 'org-mode-map
:infix "l"
"i" #'org-insert-link
"l" #'org-open-at-point
"f" #'org-footnote-action)
(local-leader
:keymaps 'org-mode-map
:infix "'"
"a" #'org-table-align
"c" #'org-table-create
"f" #'org-table-edit-formulas
"t" #'org-table-toggle-coordinate-overlays
"s" #'org-table-sum
"e" #'org-table-calc-current-TBLFM
"E" #'org-table-eval-formula))
#+end_src
** Org Agenda
Org agenda provides a nice viewing for schedules. With org mode it's
a very tidy way to manage your time.
#+begin_src emacs-lisp
(use-package org-agenda
:defer t
:init
(defconst +org/agenda-root "~/Text/"
"Root directory for all agenda files")
(setq org-agenda-files (list (expand-file-name +org/agenda-root))
org-agenda-window-setup 'current-window
org-agenda-skip-deadline-prewarning-if-scheduled t
org-agenda-skip-scheduled-if-done t
org-agenda-skip-deadline-if-done t
org-agenda-start-with-entry-text-mode nil)
:config
(evil-set-initial-state 'org-agenda-mode 'normal)
:general
(file-leader
"a" `(,(proc (interactive)
(find-file (completing-read "Enter directory: " org-agenda-files nil t)))
:which-key "Open agenda directory"))
(app-leader
"a" #'org-agenda)
(nmmap
:keymaps 'org-agenda-mode-map
"zd" #'org-agenda-day-view
"zw" #'org-agenda-week-view
"zm" #'org-agenda-month-view
"gd" #'org-agenda-goto-date
"RET" #'org-agenda-switch-to
"J" #'org-agenda-later
"K" #'org-agenda-earlier
"t" #'org-agenda-todo
"." #'org-agenda-goto-today
"," #'org-agenda-goto-date
"q" #'org-agenda-quit
"r" #'org-agenda-redo))
#+end_src
** Org capture
Org capture provides a system for quickly "capturing" some information
into an org file. A classic example is creating a new TODO in a
todo file, where the bare minimum to record one is:
+ where was it recorded?
+ when was it recorded?
+ what is it?
Org capture provides a way to do that seamlessly without opening the
todo file directly.
#+begin_src emacs-lisp
(use-package org-capture
:defer t
:init
(setq
org-default-notes-file (concat org-directory "todo.org")
org-capture-templates
'(("t" "Todo" entry
(file "")
"* TODO %?
%T
%a")))
:general
(leader
"C" #'org-capture)
(nmmap
:keymaps 'org-capture-mode-map
"ZZ" #'org-capture-finalize
"ZR" #'org-capture-refile
"ZQ" #'org-capture-kill))
#+end_src
** WIP Org clock-in
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
Org provides a nice timekeeping system that allows for managing how
much time is taken per task. It even has an extensive reporting
system to see how much time you spend on specific tasks or overall.
#+begin_src emacs-lisp
(use-package org-clock
:after org
:general
(local-leader
:keymaps 'org-mode-map
:infix "c"
"d" #'org-clock-display
"c" #'org-clock-in
"o" #'org-clock-out
"r" #'org-clock-report))
#+end_src
** WAIT Org ref
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
For bibliographic stuff in $\LaTeX$ export.
#+begin_src emacs-lisp
(use-package org-ref
:straight t
:defer t
:init
(setq bibtex-files '("~/Text/bibliography.bib")
bibtex-completion-bibliography '("~/Text/bibliography.bib")
bibtex-completion-additional-search-fields '(keywords)))
#+end_src
*** Org ref ivy integration
Org ref requires ivy-bibtex to work properly with ivy, so we need to
set that up as well
#+begin_src emacs-lisp
(use-package ivy-bibtex
:straight t
:after org-ref
:config
(require 'org-ref-ivy))
#+end_src
** Org message
Org message allows for the use of org mode when composing mails,
generating HTML multipart emails. This integrates the WYSIWYG
experience with mail in Emacs while also providing powerful text
features with basically no learning curve (as long as you've already
learnt the basics of org).
#+begin_src emacs-lisp
(use-package org-msg
:straight t
:hook
(message-mode-hook . org-msg-mode)
(notmuch-message-mode-hook . org-msg-mode)
:config
(setq org-msg-options "html-postamble:nil H:5 num:nil ^:{} toc:nil author:nil email:nil \\n:t tex:dvipng"
org-msg-greeting-name-limit 3)
(add-to-list 'org-msg-enforce-css
'(img latex-fragment-inline
((transform . ,(format "translateY(-1px) scale(%.3f)"
(/ 1.0 (if (boundp 'preview-scale)
preview-scale 1.4))))
(margin . "0 -0.35em")))))
#+end_src
** Org for evil
Evil org for some nice bindings.
#+begin_src emacs-lisp
(use-package evil-org
:straight t
:defer t
:hook (org-mode-hook . evil-org-mode)
:general
(nmmap
:keymaps 'org-mode-map
"TAB" #'org-cycle))
#+end_src
** WAIT Org reveal
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
Org reveal allows one to export org files as HTML presentations via
reveal.js. Pretty nifty and it's easy to use.
#+begin_src emacs-lisp
(use-package ox-reveal
:straight t
:defer t
:init
(setq org-reveal-root "https://cdn.jsdelivr.net/npm/reveal.js"
org-reveal-theme "sky"))
#+end_src
** Org bookmark
I maintain a bookmarks file at =~/Text/bookmarks.org=. I would like
the ability to construct new bookmarks and open bookmarks. They may
be either articles I want to read, useful information documents or
just straight up youtube videos. So I wrote a
[[file:elisp/org-bookmark.el][library]] myself which does the
appropriate dispatching and work for me. Pretty sweet!
Also I define a template for org-capture here for bookmarks and add it
to the list ~org-capture-templates~.
#+begin_src emacs-lisp
(use-package org-bookmark
:defer t
:load-path "elisp/"
:general
(file-leader
"b" #'org-bookmark/open-bookmark)
:init
(with-eval-after-load "org-capture"
(add-to-list
'org-capture-templates
'("b" "Bookmark" entry
(file "bookmarks.org")
"* %? :bookmark:
%T
%^{url|%x}p
"
))))
#+end_src
* Languages
For a variety of (programming) languages Emacs comes with default
modes but this configures them as well as pulls any modes Emacs
doesn't come with.
** Makefile
Defines an auto-insert for Makefiles. Assumes C but it's very easy to
change it for C++.
#+begin_src emacs-lisp
(use-package make-mode
:defer t
:auto-insert
(("[mM]akefile\\'" . "Makefile skeleton")
""
"CC=gcc
OUT=main.out
LIBS=
ARGS=
RELEASE=0
GFLAGS=-Wall -Wextra -Werror -Wswitch-enum -std=c11
DFLAGS=-ggdb -fsanitize=address -fsanitize=undefined
RFLAGS=-O3
DEPFLAGS=-MT $@ -MMD -MP -MF
ifeq ($(RELEASE), 1)
CFLAGS=$(GFLAGS) $(RFLAGS)
else
CFLAGS=$(GFLAGS) $(DFLAGS)
endif
SRC=src
DIST=build
CODE=$(addprefix $(SRC)/, ) # add source files here
OBJECTS=$(CODE:$(SRC)/%.c=$(DIST)/%.o)
DEPDIR:=$(DIST)/dependencies
DEPS:=$(CODE:$(SRC)/%.c=$(DEPDIR):%.d) $(DEPDIR)/main.d
.PHONY: all
all: $(OUT)
$(OUT): $(DIST)/$(OUT)
$(DIST)/$(OUT): $(OBJECTS) $(DIST)/main.o | $(DIST)
$(CC) $(CFLAGS) $^ -o $@ $(LIBS)
$(DIST)/%.o: $(SRC)/%.c | $(DIST) $(DEPDIR)
$(CC) $(CFLAGS) $(DEPFLAGS) $(DEPDIR)/$*.d -c $< -o $@ $(LIBS)
.PHONY: run
run: $(DIST)/$(OUT)
./$^ $(ARGS)
.PHONY:
clean:
rm -rfv $(DIST)/*
$(DIST):
mkdir -p $(DIST)
$(DEPDIR):
mkdir -p $(DEPDIR)
-include $(DEPS)
"
_))
#+end_src
** WAIT SQL
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
The default SQL package provides support for connecting to common
database types (sqlite, mysql, etc) for auto completion and query
execution. I don't use SQL currently but whenever I need it it's
there.
#+begin_src emacs-lisp
(use-package sql
:defer t
:init
(setq sql-display-sqli-buffer-function nil))
#+end_src
** WAIT Ada
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
Check out [[file:elisp/ada-mode.el][ada-mode]], my custom ~ada-mode~
that replaces the default one. This mode just colourises stuff, and
uses eglot and a language server to do the hard work.
#+begin_src emacs-lisp
(use-package ada-mode
:load-path "elisp/"
:defer t
:config
(with-eval-after-load "eglot"
(add-hook 'ada-mode-hook #'eglot)))
#+end_src
** NHexl
Hexl-mode is the inbuilt package within Emacs to edit hex and binary
format buffers. There are a few problems with hexl-mode though,
including an annoying prompt on /revert-buffer/.
Thus, nhexl-mode! It comes with a few other improvements. Check out
the [[https://elpa.gnu.org/packages/nhexl-mode.html][page]] yourself.
#+begin_src emacs-lisp
(use-package nhexl-mode
:straight t
:defer t
:mode ("\\.bin" "\\.out"))
#+end_src
** C/C++
Setup for C and C++ modes, using Emacs' default package: cc-mode.
*** cc-mode
Tons of stuff, namely:
+ ~auto-fill-mode~ for 80 char limit
+ Some keybindings to make evil statement movement easy
+ Lots of pretty symbols
+ Indenting options and a nice (for me) code style for C
+ Auto inserts to get a C file going
#+begin_src emacs-lisp
(use-package cc-mode
:defer t
:hook
(c-mode-hook . auto-fill-mode)
(c++-mode-hook . auto-fill-mode)
:general
(:keymaps '(c-mode-map
c++-mode-map)
:states '(normal motion visual)
"(" #'c-beginning-of-statement
")" #'c-end-of-statement
"{" #'c-beginning-of-defun
"}" #'c-end-of-defun)
:init
(setq-default c-basic-offset 2)
(setq-default c-auto-newline nil)
(setq-default c-default-style '((other . "user")))
(defun +cc/copyright-notice ()
(let* ((lines (split-string (+license/copyright-notice) "\n"))
(copyright-line (car lines))
(rest (cdr lines)))
(concat
"* "
copyright-line
"\n"
(mapconcat
#'(lambda (x)
(if (string= x "")
""
(concat " * " x)))
rest
"\n"))))
:auto-insert
(("\\.c\\'" . "C skeleton")
""
"/" (+cc/copyright-notice) "\n\n"
" * Created: " (format-time-string "%Y-%m-%d") "\n"
" * Author: " user-full-name "\n"
" * Description: " _ "\n"
" */\n"
"\n")
(("\\.cpp\\'" "C++ skeleton")
""
"/" (+cc/copyright-notice) "\n\n"
" * Created: " (format-time-string "%Y-%m-%d") "\n"
" * Author: " user-full-name "\n"
" * Description: " _ "\n"
" */\n"
"\n")
(("\\.\\([Hh]\\|hh\\|hpp\\|hxx\\|h\\+\\+\\)\\'" . "C / C++ header")
(replace-regexp-in-string "[^A-Z0-9]" "_"
(string-replace "+" "P"
(upcase
(file-name-nondirectory buffer-file-name))))
"/" (+cc/copyright-notice) "\n\n"
" * Created: " (format-time-string "%Y-%m-%d") "\n"
" * Author: " user-full-name "\n"
" * Description: " _ "\n"
" */\n\n"
"#ifndef " str n "#define " str "\n\n" "\n\n#endif")
:config
(c-add-style
"user"
'((c-basic-offset . 2)
(c-comment-only-line-offset . 0)
(c-hanging-braces-alist (brace-list-open)
(brace-entry-open)
(substatement-open after)
(block-close . c-snug-do-while)
(arglist-cont-nonempty))
(c-cleanup-list brace-else-brace)
(c-offsets-alist
(statement-block-intro . +)
(substatement-open . 0)
(access-label . -)
(inline-open . 0)
(label . 0)
(statement-cont . +)))))
#+end_src
*** Clang format
clang-format is a program that formats C/C++ files. It's highly
configurable and quite fast. I have a root configuration in my
Dotfiles (check it out
[[file:~/Dotfiles/ClangFormat/).clang-format][here]].
Clang format comes inbuilt with clang/LLVM, so it's quite likely to be
on your machine.
#+begin_src emacs-lisp
(use-package clang-format
:load-path "/usr/share/clang/"
:defer t
:after cc-mode
:commands (+code/clang-format-region-or-buffer
clang-format-mode)
:general
(code-leader
:keymaps '(c-mode-map c++-mode-map)
"f" #'clang-format-buffer)
:config
(define-minor-mode clang-format-mode
"On save formats the current buffer via clang-format."
:lighter nil
(let ((save-func (proc (interactive)
(clang-format-buffer))))
(if clang-format-mode
(add-hook 'before-save-hook save-func nil t)
(remove-hook 'before-save-hook save-func t))))
(defun +code/clang-format-region-or-buffer ()
(interactive)
(if (mark)
(clang-format-region (region-beginning) (region-end))
(clang-format-buffer))))
#+end_src
*** cc org babel
To ensure org-babel executes language blocks of C/C++, I need to load
it as an option in ~org-babel-load-languages~.
#+begin_src emacs-lisp
(use-package org
:after cc-mode
:init
(org-babel-do-load-languages
'org-babel-load-languages
'((C . t))))
#+end_src
*** cc compile fsan
Sanitisers are a blessing for C/C++. An additional runtime on top of
the executable which catches stuff like undefined behaviour or memory
leaks make it super easy to see where and how code is failing.
However, by default, Emacs' compilation-mode doesn't understand the
logs =fsanitize= makes so you usually have to manually deal with it
yourself.
Compilation mode uses regular expressions to figure out whether
something is an error and how to navigate to the file where that error
is located. So adding support for =-fsanitize= is as simple as making
a regular expression which captures file names and digits
#+begin_src emacs-lisp
(use-package compile
:after cc-mode
:config
(add-to-list 'compilation-error-regexp-alist-alist
`(fsan ,(rx (and
line-start " #" digit " 0x" (1+ hex) " in "
(1+ (or word "_")) " "
(group (seq (* any) (or ".c" ".cpp" ".h" ".hpp"))) ":"
(group (+ digit))))
1 2))
(add-to-list 'compilation-error-regexp-alist
'fsan))
#+end_src
** Markdown
Why use Markdown when you have org-mode? Because LSP servers
sometimes format their documentation as markdown, which
[[*Eglot][Eglot]] can use to provide nicer views on docs!
#+begin_src emacs-lisp
(use-package markdown-mode
:defer t
:straight t)
#+end_src
** WAIT D
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
D is a systems level programming language with C-style syntax. I
think it has some interesting ideas such as a toggleable garbage
collector. Here I just install the D-mode package, enable ~org-babel~
execution of d-mode blocks and alias ~D-mode~ with ~d-mode~.
#+begin_src emacs-lisp
(use-package d-mode
:defer t
:straight t
:config
(fset 'D-mode 'd-mode)
(with-eval-after-load "org-mode"
(setf (alist-get 'd org-babel-load-languages) t)))
#+end_src
** WAIT Rust
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
Rust is the systems programming language that also does web stuff and
CLI programs and basically tries to be a jack of all trades. It's got
some interesting stuff but most importantly it's very new, so everyone
must learn it, right?
#+begin_src emacs-lisp
(use-package rust-mode
:straight t
:defer t
:general
(code-leader
:keymaps 'rust-mode-map
"f" #'rust-format-buffer)
(local-leader
:keymaps 'rust-mode-map
"c" #'rust-run-clippy)
:init
(setq rust-format-on-save t)
(with-eval-after-load "eglot"
(add-to-list 'eglot-server-programs '(rust-mode "rust-analyzer"))))
#+end_src
** WAIT Racket
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
A scheme with lots of stuff inside it. Using it for a language design
book so it's useful to have some Emacs binds for it.
#+begin_src emacs-lisp
(use-package racket-mode
:straight t
:defer t
:hook (racket-mode-hook . racket-xp-mode)
:display
("\\*Racket REPL*"
(display-buffer-at-bottom)
(window-height . 0.3))
:init
(setq racket-documentation-search-location 'local)
:general
(nmap
:keymaps 'racket-describe-mode-map
"q" #'quit-window)
(nmap
:keymaps 'racket-mode-map
"gr" #'racket-eval-last-sexp)
(local-leader
:keymaps '(racket-mode-map racket-repl-mode-map)
"d" #'racket-repl-describe)
(local-leader
:keymaps 'racket-mode-map
"r" #'racket-run
"i" #'racket-repl
"e" #'racket-send-definition
"sr" #'racket-send-region
"sd" #'racket-send-definition))
#+end_src
** WAIT CSharp
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
Haven't used C# in a while, but Emacs is alright for it with
omnisharp.
#+begin_src emacs-lisp
(use-package csharp-mode
:defer t)
#+end_src
** WAIT Java
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
I kinda dislike Java, but if necessary I will code in it. Just setup
a style and some pretty symbols. You can use LSP to get cooler
features to be fair.
#+begin_src emacs-lisp
(use-package ob-java
:defer t
:config
(with-eval-after-load "cc-mode"
(c-add-style
"java"
'((c-basic-offset . 4)
(c-comment-only-line-offset 0 . 0)
(c-offsets-alist
(inline-open . 0)
(topmost-intro-cont . +)
(statement-block-intro . +)
(knr-argdecl-intro . 5)
(substatement-open . 0)
(substatement-label . +)
(label . +)
(statement-case-open . +)
(statement-cont . +)
(arglist-intro . c-lineup-arglist-intro-after-paren)
(arglist-close . c-lineup-arglist)
(brace-list-intro first c-lineup-2nd-brace-entry-in-arglist c-lineup-class-decl-init-+ +)
(access-label . 0)
(inher-cont . c-lineup-java-inher)
(func-decl-cont . c-lineup-java-throws))))
(add-to-list 'c-default-style '(java-mode . "java")))
(with-eval-after-load "abbrev"
(define-abbrev-table 'java-mode-abbrev-table nil)
(add-hook 'java-mode-hook
(proc (setq-local local-abbrev-table java-mode-abbrev-table)))))
#+end_src
** WAIT Haskell
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
Haskell is a static lazy functional programming language (what a
mouthful). It's quite a beautiful language and really learning it will
change the way you think about programming. However, my preferred
functional language is still unfortunately Lisp so no extra brownie
points there.
Here I configure the REPL for Haskell via the
~haskell-interactive-mode~. I also load my custom package
[[file:elisp/haskell-multiedit.el][haskell-multiedit]] which allows a
user to create temporary ~haskell-mode~ buffers that, upon completion,
will run in the REPL. Even easier than making your own buffer.
#+begin_src emacs-lisp
(use-package haskell-mode
:straight t
:defer t
:hook
(haskell-mode-hook . haskell-indentation-mode)
(haskell-mode-hook . interactive-haskell-mode)
:display
("\\*haskell.**\\*"
(display-buffer-at-bottom)
(window-height . 0.3))
:general
(shell-leader
"h" #'haskell-interactive-bring)
(local-leader
:keymaps 'haskell-mode-map
"c" #'haskell-compile
"t" #'haskell-process-do-type)
(nmmap
:keymaps 'haskell-mode-map
"C-c C-c" #'haskell-process-load-file)
(local-leader
:keymaps 'haskell-interactive-mode-map
"c" #'haskell-interactive-mode-clear)
(imap
:keymaps 'haskell-interactive-mode-map
"M-k" #'haskell-interactive-mode-history-previous
"M-j" #'haskell-interactive-mode-history-next)
:init
(setq haskell-interactive-prompt "[λ] "
haskell-interactive-prompt-cont "{λ} "
haskell-interactive-popup-errors nil
haskell-stylish-on-save t
haskell-process-type 'auto)
:config
(load (concat user-emacs-directory "elisp/haskell-multiedit.el")))
#+end_src
** Python
Works well for python. If you have ~pyls~ it should be on your path, so
just run eglot if you need. But an LSP server is not necessary for a
lot of my time in python. Here I also setup org-babel for python
source code blocks.
#+begin_src emacs-lisp
(use-package python
:defer t
:general
(nmmap
:keymaps 'python-mode-map
"C-M-x" #'python-shell-send-defun)
(local-leader
:keymaps 'python-mode-map
"c" #'python-check)
(local-leader
:keymaps 'python-mode-map
:infix "e"
"e" #'python-shell-send-statement
"r" #'python-shell-send-region
"f" #'python-shell-send-buffer)
:pretty
(python-mode-hook
("None" . "Ø")
("list" . "ℓ")
("List" . "ℓ")
("str" . "𝕊")
("!" . "¬")
("for" . "∀")
("print" . "φ")
("lambda" . "λ")
("reduce" . "↓")
("map" . "→")
("return" . "≡")
("yield" . "≈"))
:init
(setq python-indent-offset 4)
:config
(with-eval-after-load "org-mode"
(setf (alist-get 'python org-babel-load-languages) t)))
#+end_src
*** Python shell
Setup for python shell, including a toggle option
#+begin_src emacs-lisp
(use-package python
:defer t
:commands +python/toggle-repl
:general
(shell-leader
"p" #'run-python)
:hook
(inferior-python-mode-hook . company-mode)
:display
("\\*Python\\*"
(display-buffer-at-bottom)
(window-height . 0.3)))
#+end_src
** YAML
YAML is a data language which is useful for config files.
#+begin_src emacs-lisp
(use-package yaml-mode
:defer t
:straight t)
#+end_src
** HTML/CSS/JS
Firstly, web mode for consistent colouring of syntax.
#+begin_src emacs-lisp
(use-package web-mode
:straight t
:defer t
:mode ("\\.html" . web-mode)
:mode ("\\.css" . web-mode)
:custom
((web-mode-code-indent-offset 2)
(web-mode-markup-indent-offset 2)
(web-mode-css-indent-offset 2)))
#+end_src
*** Emmet
Emmet for super speed code writing.
#+begin_src emacs-lisp
(use-package emmet-mode
:straight t
:defer t
:hook (web-mode-hook . emmet-mode)
:general
(imap
:keymaps 'emmet-mode-keymap
"TAB" #'emmet-expand-line
"M-j" #'emmet-next-edit-point
"M-k" #'emmet-prev-edit-point))
#+end_src
*** HTML Auto insert
An auto-insert for HTML buffers, which just adds some nice stuff.
#+begin_src emacs-lisp
(use-package web-mode
:defer t
:auto-insert
(("\\.html\\'" . "HTML Skeleton")
""
"<!doctype html>
<html lang=''>
<head>
<meta charset='utf-8'>
<title>"(read-string "Enter title: ") | """</title>
<meta name='description' content='" (read-string "Enter description: ") | "" "'>
<meta name='author' content='"user-full-name"'/>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='apple-touch-icon' href='/apple-touch-icon.png'>
<link rel='shortcut icon' href='/favicon.ico'/>
</head>
<body>
"
_
" </body>
</html>"))
#+end_src
*** Javascript Mode
A better mode for JavaScript that also has automatic integration with
eglot.
#+begin_src emacs-lisp
(use-package js
:defer t
:mode ("\\.js" . js-mode)
:hook (js-mode-hook . auto-fill-mode)
:init
(setq js-indent-level 2))
#+end_src
*** Typescript
A language that adds a build step to JavaScript projects for "static"
typing. It's nice because it adds good auto completion.
#+begin_src emacs-lisp
(use-package typescript-mode
:straight t
:defer t
:init
(setq typescript-indent-level 2))
#+end_src
** Scheme
Another Lisp but simpler than the rest. A beauty of engineering and
fun to write programs in. Here I setup ~geiser~, which is the
premiere way to interact with scheme REPLs.
#+begin_src emacs-lisp
(use-package geiser
:defer t
:straight t
:display
("\\*Geiser.*"
(display-buffer-reuse-mode-window display-buffer-at-bottom)
(window-height . 0.3))
:general
(shell-leader
"S" #'geiser)
(local-leader
:keymaps 'scheme-mode-map
"t" #'geiser
"m" #'geiser-doc-look-up-manual
"d" #'geiser-doc-symbol-at-point)
(local-leader
:keymaps 'scheme-mode-map
:infix "e"
"e" #'geiser-eval-last-sexp
"b" #'geiser-eval-buffer
"d" #'geiser-eval-definition
"r" #'geiser-eval-region)
:init
(with-eval-after-load "evil"
(evil-set-initial-state 'geiser-debug-mode-map 'emacs)))
(use-package geiser-guile
:defer t
:straight t)
#+end_src
** WAIT Ocaml
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
*** Ocaml Setup
Firstly, install ~opam~ and ~ocaml~. Then run the following script:
#+begin_src sh
opam install tuareg ocamlformat odoc utop merlin user-setup;
opam user-setup install;
mv ~/.emacs.d/opam-user-setup.el ~/.config/emacs/elisp;
rm -rf ~/.emacs.d ~/.emacs;
#+end_src
This sets up the necessary packages (particularly Emacs Lisp) and some
configuration that ensures Emacs is consistent with the user
installation. Notice the moving of =opam-user-setup.el= into
=~/.config/emacs/elisp=, which we'll use to setup the ocaml
experience.
*** Ocaml Configuration
Here I load the =opam-user-setup= package setup earlier, with some
neat tips from the default =~/.emacs= generated by ~opam user-setup
install~.
#+begin_src emacs-lisp
(use-package opam-user-setup
:defer t
:load-path "elisp/"
:mode ("\\.ml" . tuareg-mode)
:hook (tuareg-mode-hook . whitespace-mode)
:display
("\\*utop\\*"
(display-buffer-at-bottom)
(window-height . 0.3))
:general
(code-leader
:keymaps 'tuareg-mode-map
"f" #'+ocaml/format-buffer)
:config
(defun +ocaml/format-buffer ()
(interactive)
(when (eq major-mode 'tuareg-mode)
(let ((name (buffer-file-name (current-buffer)))
(format-str "ocamlformat -i --enable-outside-detected-project %s"))
(save-buffer)
(set-process-sentinel (start-process-shell-command "ocamlformat" "*ocamlformat*"
(format format-str name))
(lambda (p event)
(when (string= event "finished\n")
(revert-buffer nil t)
(message "[ocamlformat] Finished.")))))))
(add-to-list 'compilation-error-regexp-alist-alist
`(ocaml
"[Ff]ile \\(\"\\(.*?\\)\", line \\(-?[0-9]+\\)\\(, characters \\(-?[0-9]+\\)-\\([0-9]+\\)\\)?\\)\\(:\n\\(\\(Warning .*?\\)\\|\\(Error\\)\\):\\)?"
2 3 (5 . 6) (9 . 11) 1 (8 compilation-message-face)))
(add-to-list 'compilation-error-regexp-alist
'ocaml)
:general
(local-leader
:keymaps 'tuareg-mode-map
"u" #'utop)
(local-leader
:keymaps 'tuareg-mode-map
:infix "e"
"r" #'utop-eval-region
"e" #'utop-eval-phrase
"b" #'utop-eval-buffer))
(use-package merlin-eldoc
:straight t
:after opam-user-setup
:hook
(tuareg-mode-hook . merlin-eldoc-setup)
:init
(setq merlin-eldoc-occurrences nil))
#+end_src
** Common Lisp
Common Lisp is a dialect of Lisp, the most /common/ one around. Emacs
comes with builtin Lisp support, of course, and it's really good in
comparison to literally everything else. However, I wish it had a
better REPL...
*** WAIT Sly
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
Enter /SLY/. Sly is a fork of /SLIME/ and is *mandatory* for lisp
development on Emacs.
Here I just setup Sly to use ~sbcl~.
#+begin_src emacs-lisp
(use-package sly
:defer t
:straight t
:init
(setq inferior-lisp-program "sbcl"
sly-lisp-loop-body-forms-indentation 0)
:display
("\\*sly-db"
(display-buffer-at-bottom)
(window-height . 0.5))
("\\*sly-"
(display-buffer-at-bottom)
(window-height . 0.3))
:config
(evil-set-initial-state 'sly-db-mode 'normal)
(with-eval-after-load "org"
(setq-default org-babel-lisp-eval-fn #'sly-eval))
(with-eval-after-load "company"
(add-hook 'sly-mrepl-hook #'company-mode))
:general
(shell-leader
"s" #'sly)
(nmap
:keymaps 'lisp-mode-map
"gr" #'sly-eval-buffer
"gd" #'sly-edit-definition
"gR" #'sly-who-calls)
(local-leader
:keymaps 'lisp-mode-map
"a" '(sly-apropos :which-key "Apropos")
"d" '(sly-describe-symbol :which-key "Describe symbol")
"D" '(sly-documentation-lookup :which-key "Lookup on lispworks")
"l" '(sly-load-file :which-key "Load file")
"c" '(sly-compile-defun :which-key "Compile defun")
"C" '(sly-compile-file :which-key "Compile file")
"S" '(sly-mrepl-sync :which-key "Sync REPL"))
(local-leader
:keymaps 'lisp-mode-map
:infix "e"
"b" #'sly-eval-buffer
"e" #'sly-eval-last-expression
"f" #'sly-eval-defun
"r" #'sly-eval-region)
(nmap
:keymaps 'sly-mrepl-mode-map
"M-j" #'sly-mrepl-next-input-or-button
"M-k" #'sly-mrepl-previous-input-or-button)
(local-leader
:keymaps 'sly-mrepl-mode-map
"s" '(sly-mrepl-shortcut :which-key "Shortcut"))
(nmap
:keymaps 'sly-db-mode-map
"\C-i" 'sly-db-cycle
"g?" 'describe-mode
"S" 'sly-db-show-frame-source
"e" 'sly-db-eval-in-frame
"d" 'sly-db-pprint-eval-in-frame
"D" 'sly-db-disassemble
"i" 'sly-db-inspect-in-frame
"gj" 'sly-db-down
"gk" 'sly-db-up
(kbd "C-j") 'sly-db-down
(kbd "C-k") 'sly-db-up
"]]" 'sly-db-details-down
"[[" 'sly-db-details-up
(kbd "M-j") 'sly-db-details-down
(kbd "M-k") 'sly-db-details-up
"gg" 'sly-db-beginning-of-backtrace
"G" 'sly-db-end-of-backtrace
"t" 'sly-db-toggle-details
"gr" 'sly-db-restart-frame
"I" 'sly-db-invoke-restart-by-name
"R" 'sly-db-return-from-frame
"c" 'sly-db-continue
"s" 'sly-db-step
"n" 'sly-db-next
"o" 'sly-db-out
"b" 'sly-db-break-on-return
"a" 'sly-db-abort
"q" 'sly-db-quit
"A" 'sly-db-break-with-system-debugger
"B" 'sly-db-break-with-default-debugger
"P" 'sly-db-print-condition
"C" 'sly-db-inspect-condition
"g:" 'sly-interactive-eval
"0" 'sly-db-invoke-restart-0
"1" 'sly-db-invoke-restart-1
"2" 'sly-db-invoke-restart-2
"3" 'sly-db-invoke-restart-3
"4" 'sly-db-invoke-restart-4
"5" 'sly-db-invoke-restart-5
"6" 'sly-db-invoke-restart-6
"7" 'sly-db-invoke-restart-7
"8" 'sly-db-invoke-restart-8
"9" 'sly-db-invoke-restart-9)
(nmap
:keymaps 'sly-inspector-mode-map
"q" #'sly-inspector-quit))
#+end_src
*** WAIT Sly-ASDF
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
ASDF is the package declaration system that _most_ Common Lisp
programmers use. Here's a package which integrates some stuff into
SLY for ASDF.
#+begin_src emacs-lisp
(use-package sly-asdf
:straight t
:after sly)
#+end_src
*** Emacs lisp
Ligatures and bindings for (Emacs) Lisp. Pretty self declarative.
#+begin_src emacs-lisp
(use-package elisp-mode
:defer t
:pretty
(lisp-mode-hook
("lambda" . "λ")
("nil" . "Ø")
("<=" . "≤")
(">=" . "≥")
("defun" . "ƒ")
("loop" . "Σ")
("mapcar" . "→")
("reduce" . "↓")
("some" . "∃")
("every" . "∀"))
(emacs-lisp-mode-hook
("lambda" . "λ")
("nil" . "Ø")
("defun" . "ƒ")
("mapcar" . "→"))
:general
(:states '(normal motion visual)
:keymaps '(emacs-lisp-mode-map
lisp-mode-map
lisp-interaction-mode-map)
")" #'sp-next-sexp
"(" #'sp-previous-sexp)
(nmmap
:keymaps '(emacs-lisp-mode-map lisp-interaction-mode-map)
"gr" #'eval-buffer)
(vmap
:keymaps '(emacs-lisp-mode-map lisp-interaction-mode-map)
"gr" #'eval-region)
(local-leader
:keymaps '(emacs-lisp-mode-map lisp-interaction-mode-map)
"e" #'eval-last-sexp
"f" #'eval-defun))
#+end_src
*** WIP Hydra like Lispy
:PROPERTIES:
:header-args:emacs-lisp: :tangle no :results none
:END:
A [[*Hydra][Hydra]] which uses the ~Lispy~ package (by
abo-abo) to create a set of motions that allow movement around a lisp
file easily.
2024-04-18: Still working on this, quite rough around the edges.
#+begin_src emacs-lisp
(use-package lispy
:after (lisp-mode elisp-mode)
:hydra
(hydra-lispy
nil "Move around quickly in Lisp"
("h" #'lispy-left)
("j" ("t" #'lispy-teleport)
#'lispy-down)
("k" #'lispy-up)
("l" #'lispy-right)
("d" #'lispy-different)
("u" #'lispy-flow)
("o" #'lispy-oneline)
("m" #'lispy-multiline)
("N" #'lispy-narrow)
("W" #'lispy-widen)
("c" #'lispy-clone)
("fp" #'lispy-ace-paren)
("fs" #'lispy-ace-symbol :exit t)
("H" #'lispy-slurp)
("L" #'lispy-barf)
("M-h" #'lispy-move-left)
("M-j" #'lispy-move-down)
("M-k" #'lispy-move-up)
("M-l" #'lispy-move-right)
("C-g" nil))
:general
(nmmap
:keymaps '(emacs-lisp-mode-map lisp-mode-map)
"." #'hydra-lispy/body))
#+end_src
*** Lisp indent function
Add a new lisp indent function which indents newline lists more
appropriately.
#+begin_src emacs-lisp
(use-package lisp-mode
:defer t
:config
(defun +oreo/lisp-indent-function (indent-point state)
(let ((normal-indent (current-column))
(orig-point (point)))
(goto-char (1+ (elt state 1)))
(parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
(cond
;; car of form doesn't seem to be a symbol, or is a keyword
((and (elt state 2)
(or (not (looking-at "\\sw\\|\\s_"))
(looking-at ":")))
(if (not (> (save-excursion (forward-line 1) (point))
calculate-lisp-indent-last-sexp))
(progn (goto-char calculate-lisp-indent-last-sexp)
(beginning-of-line)
(parse-partial-sexp (point)
calculate-lisp-indent-last-sexp 0 t)))
;; Indent under the list or under the first sexp on the same
;; line as calculate-lisp-indent-last-sexp. Note that first
;; thing on that line has to be complete sexp since we are
;; inside the innermost containing sexp.
(backward-prefix-chars)
(current-column))
((and (save-excursion
(goto-char indent-point)
(skip-syntax-forward " ")
(not (looking-at ":")))
(save-excursion
(goto-char orig-point)
(looking-at ":")))
(save-excursion
(goto-char (+ 2 (elt state 1)))
(current-column)))
(t
(let ((function (buffer-substring (point)
(progn (forward-sexp 1) (point))))
method)
(setq method (or (function-get (intern-soft function)
'lisp-indent-function)
(get (intern-soft function) 'lisp-indent-hook)))
(cond ((or (eq method 'defun)
(and (null method)
(> (length function) 3)
(string-match "\\`def" function)))
(lisp-indent-defform state indent-point))
((integerp method)
(lisp-indent-specform method state
indent-point normal-indent))
(method
(funcall method indent-point state))))))))
(setq-default lisp-indent-function #'+oreo/lisp-indent-function))
#+end_src
|