about summary refs log tree commit diff stats
path: root/config.org
blob: 38b487cf91858fb675183f8143586493c7515895 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
#+TITLE: Emacs, emacs, emacs
#+AUTHOR: Case Duckworth
#+PROPERTY: header-args :tangle config.el :tangle-mode (identity #o444) :comments both :mkdirp yes
#+STARTUP: overview
#+EXPORT_FILE_NAME: README.md
#+OPTIONS: toc:nil
#+BANKRUPTCY_COUNT: 3.2
#+Time-stamp: <2021-01-14 20:38:43 acdw>

* Basics

** Disclaimer

   #+begin_src emacs-lisp :comments no
     ;; config.el -*- lexical-binding: t -*-
     ;; This file is automatically tangled from config.org.
     ;; Hand edits will be overwritten!
   #+end_src

** About me

   #+begin_src emacs-lisp
     (setq user-full-name "Case Duckworth"
	   user-mail-address "acdw@acdw.net")
   #+end_src

** Correct =exec-path=

   Straight depends on Git, so I need to tell Emacs where different paths are.

   #+begin_src emacs-lisp
     (let ((win-downloads "c:/Users/aduckworth/Downloads"))
       (dolist (path (list
		      ;; Linux
		      (expand-file-name "bin"
					user-emacs-directory)
		      (expand-file-name "~/bin")
		      (expand-file-name "~/.local/bin")
		      (expand-file-name "~/Scripts")
		      ;; Windows
		      (expand-file-name "emacs/bin"
					win-downloads)
		      (expand-file-name "m/usr/bin"
					win-downloads)
		      (expand-file-name "m/mingw64/bin"
					win-downloads)
		      (expand-file-name "PortableGit/bin"
					win-downloads)
		      (expand-file-name "PortableGit/usr/bin"
					win-downloads)))
	 (when (file-exists-p path)
	   (add-to-list 'exec-path path))))
   #+end_src

** Package management

*** Straight.el

    Straight can't bootstrap itself on Windows, so I've wrapped the
    bootstrap code from straight's repo in a function.

    #+begin_src emacs-lisp
      (defun acdw/bootstrap-straight ()
	"Bootstrap straight.el."
	(defvar bootstrap-version)
	(let ((bootstrap-file
	       (expand-file-name
		"straight/repos/straight.el/bootstrap.el"
		user-emacs-directory))
	      (bootstrap-version 5))
	  (unless (file-exists-p bootstrap-file)
	    (with-current-buffer
		(url-retrieve-synchronously
		 (concat
		  "https://raw.githubusercontent.com/"
		  "raxod502/straight.el/"
		  "develop/install.el")
		 'silent 'inhibit-cookies)
	      (goto-char (point-max))
	      (eval-print-last-sexp)))
	  (load bootstrap-file nil 'nomessage)))
    #+end_src

    Now, I'll /try/ running it regular-style, ignoring the errors.  If it
    doesn't work, I'll call git directly and clone the repo myself.

    #+begin_src emacs-lisp
      (unless (ignore-errors (acdw/bootstrap-straight))
	(message "%s" "Straight.el didn't bootstrap correctly.  Cloning directly...")
	(call-process "git" nil
		      (get-buffer-create "*bootstrap-straight-messages*") nil
		      "clone"
		      "https://github.com/raxod502/straight.el"
		      (expand-file-name "straight/repos/straight.el"
					user-emacs-directory))
	(acdw/bootstrap-straight))
    #+end_src

** Ease-of-configuring functions

*** Emulate use-package’s =:custom=

    #+begin_src emacs-lisp
      (defmacro cuss (var val &optional _docstring)
	"Basically, `:custom' from `use-package', but without `use-package'."
	(declare (doc-string 3)
		 (indent 2))
	`(funcall (or (get ',var 'custom-set) #'set-default)
		  ',var ,val))
    #+end_src

*** Emulate use-package’s =:custom-face=, but better

    #+begin_src emacs-lisp
      (defvar acdw--custom-faces ()
	"List of custom faces to run through acdw/set-custom-faces.")

      (defun acdw/set-custom-faces ()
	"Run `customize-set-faces' on `acdw--custom-faces'."
	(message "%s" "Customizing faces...")
	(apply #'custom-set-faces acdw--custom-faces))

      (defun cussface (spec)
	"Add SPEC to `acdw--custom-faces', and add a hook to run
      `acdw/set-custom-faces' after init."
	(add-to-list 'acdw--custom-faces spec)
	(add-hook 'after-init-hook #'acdw/set-custom-faces))
    #+end_src

*** Determine whether any Emacs frame is focused or not

    This comes in handy when I want to garbage collect, say, or save recent files.

    #+begin_src emacs-lisp
      (defun acdw/when-unfocused (func &rest args)
	"Run FUNC with ARGS only if all frames are out of focus."
	(if (seq-every-p #'null (mapcar #'frame-focus-state (frame-list)))
	    (apply func args)))
    #+end_src

*** Determine where I am

    I use Emacs at home, with Linux, and at work, with Windows.

    #+begin_src emacs-lisp
      (defmacro when-at (conditions &rest commands)
	"Only do COMMANDS when CONDITIONS are met.
      CONDITIONS are one of `:work', `:home', or a list beginning with
      the above and other conditions to check."
	(declare (indent 1))
	(let ((at-work (memq system-type '(ms-dos windows-nt)))
	      (at-home (memq system-type '(gnu gnu/linux gnu/kfreebsd))))
	  (pcase conditions
	    (:work `(when ',at-work ,@commands))
	    (:home `(when ',at-home ,@commands))
	    (`(:work ,others) `(when (and ',at-work ,others)
				 ,@commands))
	    (`(:home ,others) `(when (and ',at-home ,others)
				 ,@commands)))))
    #+end_src

** Clean =.emacs.d=

   #+begin_src emacs-lisp
     (straight-use-package 'no-littering)
     (require 'no-littering)
   #+end_src

*** Don’t clutter =init.el= with customizations

    #+begin_src emacs-lisp
      (with-eval-after-load 'no-littering 
	(cuss custom-file (no-littering-expand-etc-file-name "custom.el")))
    #+end_src

** Look and feel

*** Cursor

    #+begin_src emacs-lisp
      (cuss cursor-type 'bar
	"Show a vertical bar for the cursor.")

      (cuss cursor-in-non-selected-windows 'hbar
	"Show an empty box in inactive windows.")

      ;; Don't blink the cursor
      (blink-cursor-mode -1)
    #+end_src

*** Tool Bars

**** Tool bars and menu bars

     #+begin_src emacs-lisp
       (menu-bar-mode -1)
       (tool-bar-mode -1)
     #+end_src

**** Scroll bars

     #+begin_src emacs-lisp
       (scroll-bar-mode -1)
       (horizontal-scroll-bar-mode -1)
     #+end_src

*** Dialogs

    #+begin_src emacs-lisp
      (cuss use-dialog-box nil
	"Don't use dialog boxes to ask questions.")
    #+end_src

**** Yes or no questions

     #+begin_src emacs-lisp
       (fset 'yes-or-no-p #'y-or-n-p)
     #+end_src

**** The Bell

     from [[https://www.emacswiki.org/emacs/AlarmBell#h5o-3][EmacsWiki]].

     #+begin_src emacs-lisp
       (setq visible-bell nil
	     ring-bell-function 'flash-mode-line)

       (defun flash-mode-line ()
	 (invert-face 'mode-line)
	 (run-with-timer 0.1 nil #'invert-face 'mode-line))
     #+end_src

*** Frames

**** Frame titles

     #+begin_src emacs-lisp
       (cuss frame-title-format (concat invocation-name "@" system-name
					": %b %+%+ %f")) 
     #+end_src

**** Fringes

     #+begin_src emacs-lisp
       (cuss indicate-empty-lines t
	 "Show an indicator on the left fringe of empty lines past the
       end of the buffer.")
       (cuss indicate-buffer-boundaries 'right
	 "Indicate the beginning and end of the buffer and whether it
	 scrolls off-window in the right fringe.")

       (cuss visual-line-fringe-indicators '(left-curly-arrow nil)
	 "Indicate continuing lines with a curly arrow in the left fringe.")

       ;; redefine the `left-curly-arrow' indicator to be less distracting.
       (define-fringe-bitmap 'left-curly-arrow
	   [#b11000000
	    #b01100000
	    #b00110000
	    #b00011000])
     #+end_src

**** Minibuffer

     #+begin_src emacs-lisp
       (cuss minibuffer-prompt-properties
	   '(read-only t cursor-intangible t face minibuffer-prompt)
	 "Keep the cursor away from the minibuffer prompt.")
     #+end_src

**** Tabs

     #+begin_src emacs-lisp
       (cuss tab-bar-tab-name-function
	   #'tab-bar-tab-name-current-with-count
	 "Show the tab name as the name of the current buffer, plus a
	 count of the windows in the tab.")

       (cuss tab-bar-show 1
	 "Show the tab bar, when there's more than one tab.")
     #+end_src

*** Windows

**** Winner mode

     #+begin_src emacs-lisp
       (when (fboundp 'winner-mode)
	 (winner-mode +1))
     #+end_src

**** Switch windows or buffers if one window

from [[https://www.reddit.com/r/emacs/comments/kz347f/what_parts_of_your_config_do_you_like_best/gjlnp2c/][u/astoff1]].

     #+begin_src emacs-lisp
       (defun acdw/other-window-or-buffer ()
	 "Switch to other window, or previous buffer."
	 (interactive)
	 (if (eq (count-windows) 1)
	     (switch-to-buffer nil)
	   (other-window 1)))

       (global-set-key (kbd "M-o") #'acdw/other-window-or-buffer)
     #+end_src

**** Pop-up windows

     #+begin_src emacs-lisp
       (straight-use-package 'popwin)
       (popwin-mode +1)
     #+end_src

*** Buffers

**** Uniquify buffers

     #+begin_src emacs-lisp
       (require 'uniquify)
       (cuss uniquify-buffer-name-style 'forward
	 "Uniquify buffers' names by going up the path trees until they
       become unique.")
     #+end_src

**** Startup buffers

     #+begin_src emacs-lisp
       (cuss inhibit-startup-screen t
	 "Don't show Emacs' startup buffer.")

       (cuss initial-buffer-choice t
	 "Start with *scratch*.")

       (cuss initial-scratch-message ""
	 "Empty *scratch* buffer.")
     #+end_src

**** COMMENT Focus and move buffers directionally

     Commented for now because I really need to figure out the keybindings I want to use for this.  The real issue is the collisions between Org-mode, Windows, and Emacs’ normal bindings.

     #+begin_src emacs-lisp
       ;; change focus
       ;; for more on `ignore-error-wrapper', see
       ;; https://www.emacswiki.org/emacs/WindMove#h5o-3
       (defun ignore-error-wrapper (fn)
	 "Funtion return new function that ignore errors.
	  The function wraps a function with `ignore-errors' macro."
	 (lexical-let ((fn fn))
		      (lambda ()
			(interactive)
			(ignore-errors
			  (funcall fn)))))

       (global-set-key [S-left] (ignore-error-wrapper 'windmove-left))
       (global-set-key [S-right] (ignore-error-wrapper 'windmove-right))
       (global-set-key [S-up] (ignore-error-wrapper 'windmove-up))
       (global-set-key [S-down] (ignore-error-wrapper 'windmove-down))

       ;; shift buffers
       (straight-use-package 'buffer-move)

       (global-set-key (kbd "<C-S-up>")     'buf-move-up)
       (global-set-key (kbd "<C-S-down>")   'buf-move-down)
       (global-set-key (kbd "<C-S-left>")   'buf-move-left)
       (global-set-key (kbd "<C-S-right>")  'buf-move-right)
     #+end_src

**** Kill the current buffer 

     #+begin_src emacs-lisp
       (defun acdw/kill-a-buffer (&optional prefix)
	 "Kill a buffer based on the following rules:

       C-x k     ⇒ Kill current buffer & window
       C-u C-x k ⇒ Kill OTHER window and its buffer
       C-u C-u C-x C-k ⇒ Kill all other buffers and windows

       Prompt only if there are unsaved changes."
	 (interactive "P")
	 (pcase (or (car prefix) 0)
	   ;; C-x k     ⇒ Kill current buffer & window
	   (0  (kill-current-buffer)
	       (unless (one-window-p) (delete-window)))
	   ;; C-u C-x k ⇒ Kill OTHER window and its buffer
	   (4  (other-window 1)
	       (kill-current-buffer)
	       (unless (one-window-p) (delete-window)))
	   ;; C-u C-u C-x C-k ⇒ Kill all other buffers and windows
	   (16   (mapc 'kill-buffer (delq (current-buffer) (buffer-list)))
		 (delete-other-windows))))

       (define-key ctl-x-map "k" #'acdw/kill-a-buffer)
     #+end_src

***** Remap =C-x M-k= to bring up the buffer-killing menu

      #+begin_src emacs-lisp
	(define-key ctl-x-map (kbd "M-k") #'kill-buffer)
      #+end_src

**** Immortal =*scratch*= buffer

     #+begin_src emacs-lisp
       (defun immortal-scratch ()
	 (if (eq (current-buffer) (get-buffer "*scratch*"))
	     (progn (bury-buffer)
		    nil)
	   t))

       (add-hook 'kill-buffer-query-functions 'immortal-scratch)
     #+end_src

*** Modeline

**** Smart mode line

     #+begin_src emacs-lisp
       (straight-use-package 'smart-mode-line)

       (cuss sml/no-confirm-load-theme t
	 "Pass the NO-CONFIRM flag to `load-theme'.")

       (sml/setup)
     #+end_src

**** Rich minority

     Since this /comes/ with smart mode line, I’m just going to use it,
     instead of =diminish= or another package.  I do have to write this
     helper function, though, to add things to the whitelist.

     #+begin_src emacs-lisp
       (defun rm/whitelist-add (regexp)
	 "Add a REGEXP to the whitelist for `rich-minority'."
	 (if (listp 'rm--whitelist-regexps)
	     (add-to-list 'rm--whitelist-regexps regexp)
	   (setq rm--whitelist-regexps `(,regexp)))
	 (setq rm-whitelist
	       (mapconcat 'identity rm--whitelist-regexps "\\|")))

       (straight-use-package 'rich-minority)

       (rm/whitelist-add "^$")
     #+end_src

*** Theme

**** Modus Themes

     #+begin_src emacs-lisp
       (straight-use-package 'modus-themes)

       (cuss modus-themes-slanted-constructs t
	 "Use more slanted constructs.")
       (cuss modus-themes-bold-constructs t
	 "Use more bold constructs.")

       (cuss modus-themes-region 'bg-only
	 "Only highlight the background of the selected region.")

       (cuss modus-themes-org-blocks 'grayscale
	 "Show org-blocks with a grayscale background.")
       (cuss modus-themes-headings
	   '((1 . line)
	     (t . t))
	 "Highlight top headings with `line' style, and others by default.")

       (cuss modus-themes-scale-headings t
	 "Scale headings by the ratios below.")
       (cuss modus-themes-scale-1 1.1)
       (cuss modus-themes-scale-2 1.15)
       (cuss modus-themes-scale-3 1.21)
       (cuss modus-themes-scale-4 1.27)
       (cuss modus-themes-scale-5 1.33)

       (load-theme 'modus-operandi t)
     #+end_src

**** Change themes based on time of day

     #+begin_src emacs-lisp
       (cuss calendar-latitude 30.4515)
       (cuss calendar-longitude -91.1871)

       (defun acdw/run-with-sun (sunrise-command sunset-command)
	 "Run commands at sunrise and sunset."
	 (let* ((times-regex (rx (* nonl)
				 (: (any ?s ?S) "unrise") " "
				 (group (repeat 1 2 digit) ":"
					(repeat 1 2 digit)
					(: (any ?a ?A ?p ?P) (any ?m ?M)))
				 (* nonl)
				 (: (any ?s ?S) "unset") " "
				 (group (repeat 1 2 digit) ":"
					(repeat 1 2 digit)
					(: (any ?a ?A ?p ?P) (any ?m ?M)))
				 (* nonl)))
		(ss (sunrise-sunset))
		(_m (string-match times-regex ss))
		(sunrise-time (match-string 1 ss))
		(sunset-time (match-string 2 ss)))
	   (run-at-time sunrise-time (* 60 60 24) sunrise-command)
	   (run-at-time sunset-time (* 60 60 24) sunset-command)))

       (acdw/run-with-sun #'modus-themes-load-operandi #'modus-themes-load-vivendi)
     #+end_src

*** Fonts

**** Define fonts

     #+begin_src emacs-lisp
       (defun set-face-from-alternatives (face frame &rest fontspecs)
	 "Set FACE on FRAME from first available spec from FONTSPECS.
       FACE and FRAME work the same as with `set-face-attribute.'"
	 (catch :return
	   (dolist (spec fontspecs)
	     (when-let ((found (find-font (apply #'font-spec spec))))
	       (set-face-attribute face frame :font found)
	       (throw :return found)))))

       (defun acdw/setup-fonts ()
	 "Setup fonts.  This has to happen after the frame is setup for
       the first time, so it should be added to `window-setup-hook'.  It
       removes itself from that hook."
	 (interactive)
	 (when (display-graphic-p)
	   (dolist (face '(default fixed-pitch))
	     ;; fixed-pitch /is/ the default
	     (set-face-from-alternatives face nil
					 '(:family "Input Mono"
						   :slant normal
						   :weight normal
						   :height 110)
					 '(:family "Go Mono"
						   :slant normal
						   :weight normal
						   :height 100)
					 '(:family "Consolas"
						   :slant normal
						   :weight normal
						   :height 100)))
	   ;; variable-pitch is different
	   (set-face-from-alternatives 'variable-pitch nil
				       '(:family "Input Sans"
						 :slant normal
						 :weight normal)
				       '(:family "Georgia"
						 :slant normal
						 :weight normal)))

	 ;; remove myself from the hook
	 (remove-function after-focus-change-function #'acdw/setup-fonts))

       (add-function :before after-focus-change-function #'acdw/setup-fonts)
     #+end_src

**** Custom faces

     #+begin_src emacs-lisp
       (cussface '(font-lock-comment-face
		   ((t (:inherit (custom-comment italic variable-pitch))))))
     #+end_src

**** Line spacing

     #+begin_src emacs-lisp
       (cuss line-spacing 0.1
	 "Add 10% extra space below each line.")
     #+end_src

**** Underlines

     #+begin_src emacs-lisp
       (cuss x-underline-at-descent-line t
	 "Draw the underline at the same place as the descent line.")
     #+end_src

**** Unicode Fonts

     #+begin_src emacs-lisp
       (straight-use-package 'unicode-fonts)
       (require 'unicode-fonts)
       (unicode-fonts-setup)
     #+end_src

** Interactivity

*** Completing read

**** Shadow file names in =completing-read=.

     #+begin_src emacs-lisp
       (cuss file-name-shadow-properties '(invisible t))

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

**** Ignore case in =completing-read=

     #+begin_src emacs-lisp
       (cuss completion-ignore-case t)
       (cuss read-buffer-completion-ignore-case t)
       (cuss read-file-name-completion-ignore-case t)
     #+end_src

**** Minibuffer recursivity

     #+begin_src emacs-lisp
       (cuss enable-recursive-minibuffers t)
       (minibuffer-depth-indicate-mode +1)
     #+end_src

**** Selectrum

     #+begin_src emacs-lisp
       (straight-use-package 'selectrum)
       (require 'selectrum)
       (selectrum-mode +1)
     #+end_src

**** Prescient

     #+begin_src emacs-lisp
       (straight-use-package 'prescient)
       (require 'prescient)
       (prescient-persist-mode +1)

       (straight-use-package 'selectrum-prescient)
       (require 'selectrum-prescient)
       (selectrum-prescient-mode +1)
     #+end_src

**** Consult

     #+begin_src emacs-lisp
       (straight-use-package '(consult
			       :host github
			       :repo "minad/consult"
			       :files (:defaults "consult-pkg.el")))
       (require 'consult)

       (with-eval-after-load 'consult  
	 (define-key ctl-x-map "b" #'consult-buffer)
	 (define-key ctl-x-map (kbd "C-r") #'consult-buffer)
	 (define-key ctl-x-map "4b" #'consult-buffer-other-window)
	 (define-key ctl-x-map "5b" #'consult-buffer-other-frame)

	 (define-key goto-map "o" #'consult-outline)
	 (define-key goto-map "g" #'consult-line)
	 (define-key goto-map (kbd "M-g") #'consult-line)
	 (define-key goto-map "l" #'consult-line)
	 (define-key goto-map "m" #'consult-mark)
	 (define-key goto-map "i" #'consult-imenu)
	 (define-key goto-map "e" #'consult-error)

	 (global-set-key (kbd "M-y") #'consult-yank-pop)

	 (define-key help-map "a" #'consult-apropos)

	 (fset 'multi-occur #'consult-multi-occur))
     #+end_src

**** Marginalia

     #+begin_src emacs-lisp
       (straight-use-package '(marginalia
			       :host github
			       :repo "minad/marginalia"
			       :branch "main"))

       (cuss marginalia-annotators
	   '(marginalia-annotators-heavy
	     marginalia-annotators-light))

       (marginalia-mode +1)
     #+end_src

**** COMMENT Ido

     [[https://wandersoncferreira.github.io/blog/ido/][Let’s try this out]].

     #+begin_src emacs-lisp
       (defun ido-choose-from-recentf ()
	 "Use ido to select recently visited files."
	 (interactive)
	 (find-file (ido-completing-read "Open file: " recentf-list nil t)))

       (defun bk/go-straight-home ()
	 (interactive)
	 (cond
	  ((looking-back "~/") (insert "projects/"))
	  ((looking-back "/") (insert "~/"))
	  (:else (call-interactively 'self-insert-command))))

       (defun ido-disable-line-truncation ()
	 (set (make-local-variable 'truncate-lines) nil))

       (defun ido-define-keys ()
	 (define-key ido-completion-map (kbd "C-n") 'ido-next-match)
	 (define-key ido-completion-map (kbd "C-p") 'ido-prev-match))

       (setq ido-enable-flex-matching t
	     ido-use-filename-at-point nil
	     ido-create-new-buffer 'always
	     confirm-nonexistent-file-or-buffer nil
	     completion-ignored-extensions (cons "*.aux" completion-ignored-extensions)
	     max-mini-window-height 0.5
	     ido-enable-tramp-completion t
	     ido-auto-merge-work-directories-length -1
	     ido-confirm-unique-completion t
	     ido-default-file-method 'selected-window
	     ido-case-fold t
	     ido-show-dot-for-dired t
	     ido-everywhere t
	     ido-ignore-buffers (list (rx (or (and bos  " ")
					      (and bos
						   (or "*Completions*"
						       "*Compile-Log*"
						       "*Ido Completions*"
						       "*Shell Command Output*"
						       "*vc-diff*")
						   eos))))
	     ido-decorations (quote ("\n-> " "" "\n " "\n ..." "[" "]" "
	     [No match]" " [Matched]" " [Not readable]" " [Too big]" "
	     [Confirm]")))

       (with-eval-after-load 'ido
	 (define-key ido-common-completion-map (kbd "M-SPC") 'just-one-space)
	 (define-key ido-common-completion-map (kbd "SPC") 'self-insert-command)
	 (define-key ido-file-completion-map (kbd "~") 'bk/go-straight-home)

	 (add-hook 'ido-setup-hook 'ido-define-keys)

	 (add-hook 'ido-minibuffer-setup-hook 'ido-disable-line-truncation)

	 (set-default 'imenu-auto-rescan t)

	 (add-to-list 'ido-ignore-directories "target")
	 (add-to-list 'ido-ignore-directories "node_modules")
	 )

       (defun setup-ido-mode ()
	 (require 'ido)
	 (ido-mode +1)
	 (ido-everywhere +1))

       (add-hook 'after-init-hook #'setup-ido-mode)
     #+end_src

*** Completion

    #+begin_src emacs-lisp
      (global-set-key (kbd "M-/") #'hippie-expand)
    #+end_src

*** Garbage collection

    #+begin_src emacs-lisp
      (straight-use-package 'gcmh)
      (gcmh-mode +1)

      (defun dotfiles--gc-on-last-frame-out-of-focus ()
	"GC if all frames are inactive."
	(if (seq-every-p #'null (mapcar #'frame-focus-state (frame-list)))
	    (garbage-collect)))

      (add-function :after after-focus-change-function
		    #'dotfiles--gc-on-last-frame-out-of-focus)
    #+end_src

** Keyboard

*** =ESC= cancels all

    #+begin_src emacs-lisp
      (global-set-key (kbd "<escape>") #'keyboard-escape-quit)
    #+end_src

*** Personal prefix key: =C-z=

    #+begin_src emacs-lisp
      (defvar acdw/map
	(let ((map (make-sparse-keymap))
	      (c-z (global-key-binding "\C-z")))
	  (global-unset-key "\C-z")
	  (define-key global-map "\C-z" map)
	  (define-key map "\C-z" c-z)
	  map))

      (run-hooks 'acdw/map-defined-hook)
    #+end_src

*** Show keybindings

    #+begin_src emacs-lisp
      (straight-use-package 'which-key)
      (which-key-mode +1)
    #+end_src

** Mouse

*** Preserve screen position when scrolling with the mouse wheel

    from [[https://www.reddit.com/r/emacs/comments/km9by4/weekly_tipstricketc_thread/ghg2c9d/][u/TheFrenchPoulp]].

    #+begin_src emacs-lisp
      (advice-add 'mwheel-scroll :around #'me/mwheel-scroll)

      (defun me/mwheel-scroll (original &rest arguments)
	"Like `mwheel-scroll' but preserve screen position.
      See `scroll-preserve-screen-position'."
	(let ((scroll-preserve-screen-position :always))
	  (apply original arguments)))
    #+end_src

*** Scroll much faster

from [[https://github.com/mpereira/.emacs.d#make-cursor-movement-an-order-of-magnitude-faster][mpereira]], from somewhere else.

#+begin_src emacs-lisp
  (cuss auto-window-vscroll nil
    "Don't auto-adjust `window-vscroll' to view long lines.")

  (cuss fast-but-imprecise-scrolling t
    "Scroll fast, but possibly with inaccurate text rendering.")

  (cuss jit-lock-defer-time 0
    "Only defer font-locking when input is pending.")
#+end_src

** Persistence

*** Minibuffer history

    #+begin_src emacs-lisp
      (require 'savehist)

      (cuss savehist-additional-variables
	  '(kill-ring
	    search-ring
	    regexp-search-ring)
	"Other variables to save alongside the minibuffer history.")

      (cuss history-length t
	"Don't truncate history.")

      (cuss history-delete-duplicates t
	"Delete history duplicates.")

      (savehist-mode +1)
    #+end_src

*** File places

    #+begin_src emacs-lisp
      (require 'saveplace) ; this isn't required, but ... I like having it here

      (cuss save-place-forget-unreadable-files t
	"Don't check if files are readable or not.")

      (save-place-mode +1)
    #+end_src

*** Recent files

    #+begin_src emacs-lisp
      (require 'recentf)

      (cuss recentf-max-menu-items 100
	"The maximum number of items in the recentf menu.")
      (cuss recentf-max-saved-items nil
	"Don't limit the number of recent files.")

      (with-eval-after-load 'no-littering
	(add-to-list 'recentf-exclude no-littering-var-directory)
	(add-to-list 'recentf-exclude no-littering-etc-directory))

      (recentf-mode +1)

      ;; save recentf list when focusing away
      (defun acdw/maybe-save-recentf ()
	"Save `recentf-file' when out of focus, but only if we haven't
      in five minutes."
	(defvar recentf-last-save (time-convert nil 'integer)
	  "How long it's been since we last saved the recentf list.")

	(when (> (time-convert (time-since recentf-last-save) 'integer)
		 (* 60 5))
	  (setq recentf-last-save (time-convert nil 'integer))
	  (acdw/when-unfocused #'recentf-save-list)))

      (add-function :after after-focus-change-function
		    #'acdw/maybe-save-recentf)
    #+end_src

** Undo

   #+begin_src emacs-lisp
     (straight-use-package 'undo-fu)
     (require 'undo-fu)

     (global-set-key (kbd "C-/") #'undo-fu-only-undo)
     (global-set-key (kbd "C-?") #'undo-fu-only-redo)

     (straight-use-package 'undo-fu-session)
     (require 'undo-fu-session)

     (cuss undo-fu-session-incompatible-files
	 '("/COMMIT_EDITMSG\\'"
	   "/git-rebase-todo\\'")
       "A list of files that are incompatible with the concept of undo sessions.")

     (with-eval-after-load 'no-littering
       (let ((dir (no-littering-expand-var-file-name "undos")))
	 (make-directory dir 'parents)
	 (cuss undo-fu-session-directory dir)))

     (global-undo-fu-session-mode +1)
   #+end_src

** Files

*** Encoding

**** UTF-8

     from [[https://www.masteringemacs.org/article/working-coding-systems-unicode-emacs][Mastering Emacs]].

     #+begin_src emacs-lisp
       (prefer-coding-system 'utf-8)
       (set-default-coding-systems 'utf-8)
       (set-terminal-coding-system 'utf-8)
       (set-keyboard-coding-system 'utf-8)
       ;; backwards compatibility:
       ;; `default-buffer-file-coding-system' is deprecated in 23.2.
       (setq default-buffer-file-coding-system 'utf-8)

       ;; Treat clipboard as UTF-8 string first; compound text next, etc.
       (setq x-select-request-type '(UTF8_STRING COMPOUND_TEXT TEXT STRING))
     #+end_src

**** Convert all files to UNIX-style line endings

     from [[https://www.emacswiki.org/emacs/EndOfLineTips][Emacs Wiki]].

     #+begin_src emacs-lisp
       (defun ewiki/no-junk-please-were-unixish ()
	 "Convert line endings to UNIX, dammit."
	 (let ((coding-str (symbol-name buffer-file-coding-system)))
	   (when (string-match "-\\(?:dos\\|mac\\)$" coding-str)
	     (set-buffer-file-coding-system 'unix))))
     #+end_src

     I add it to the ~find-file-hook~ /and/ ~before-save-hook~ because I
     don't want to ever work with anything other than UNIX line endings
     ever again.  I just don't care.  Even Microsoft Notepad can handle
     UNIX line endings, so I don't want to hear it.

     #+begin_src emacs-lisp
       (add-hook 'find-file-hook #'ewiki/no-junk-please-were-unixish)
       (add-hook 'before-save-hook #'ewiki/no-junk-please-were-unixish)
     #+end_src

*** Backups

    #+begin_src emacs-lisp
      (cuss backup-by-copying 1)
      (cuss delete-old-versions -1)
      (cuss version-control t)
      (cuss vc-make-backup-files t)

      (with-eval-after-load 'no-littering
	(let ((dir (no-littering-expand-var-file-name "backup")))
	  (make-directory dir 'parents)
	  (cuss backup-directory-alist
	      `((".*" . ,dir)))))
    #+end_src

*** Auto-saves

    #+begin_src emacs-lisp
      ;; turn off auto-save-mode until we can set up the right directory for them
      (auto-save-mode -1)

      (with-eval-after-load 'no-littering
	(let ((dir (no-littering-expand-var-file-name "autosaves")))
	  (make-directory dir 'parents)
	  (cuss auto-save-file-name-transforms
	      `((".*" ,dir t))))

	(auto-save-mode +1))
    #+end_src

*** Super-save

    Because I like /overkill/, or at least … over-/saving/.

    #+begin_src emacs-lisp
      (straight-use-package 'super-save)

      (cuss super-save-remote-files nil
	"Don't super-save remote files.")

      (cuss super-save-exclude '(".gpg")
	"Ignore these files when saving.")

      (super-save-mode +1)
    #+end_src

*** Auto-revert buffers to files on disk

    #+begin_src emacs-lisp
      (global-auto-revert-mode +1)
    #+end_src

*** Add a timestamp to files

    #+begin_src emacs-lisp
      (add-hook 'before-save-hook #'time-stamp)
    #+end_src

*** Require a final new line

    #+begin_src emacs-lisp
      (cuss require-final-newline t)
    #+end_src

*** Edit files with =sudo=

    #+begin_src emacs-lisp
      (straight-use-package 'sudo-edit)

      (with-eval-after-load 'sudo-edit
	(define-key acdw/map (kbd "C-r") #'sudo-edit))
    #+end_src

**** Don’t add =/sudo:= files to =recentf=, though

     I’ve pretty much cribbed this from [[https://github.com/ncaq/recentf-remove-sudo-tramp-prefix/][recentf-remove-sudo-tramp-prefix]] – it’s a small enough package that I can just include it completely here.

     #+begin_src emacs-lisp
       ;; appease the compiler
       (declare-function tramp-tramp-file-p "tramp")
       (declare-function tramp-dissect-file-name "tramp")
       (declare-function tramp-file-name-method "tramp")
       (declare-function tramp-file-name-localname "tramp")

       (defun recentf-remove-sudo-tramp-prefix (path)
	 "Remove sudo from PATH."
	 (require 'tramp)
	 (if (tramp-tramp-file-p path)
	     (let ((tx (tramp-dissect-file-name path)))
	       (if (string-equal "sudo" (tramp-file-name-method tx))
		   (tramp-file-name-localname tx)
		 path))
	   path)

	 (defun recentf-remove-sudo-tramp-prefix-from-recentf-list ()
	   (require 'recentf)
	   (setq recentf-list
		 (mapcar #'recentf-remove-sudo-tramp-prefix recentf-list)))

	 (advice-add 'recentf-cleanup
		     :before #'recentf-remove-sudo-tramp-prefix-from-recentf-list))
     #+end_src

** Text editing

*** Operate visually on lines

    #+begin_src emacs-lisp
      (global-visual-line-mode +1)
    #+end_src

*** View long lines like filled lines in the beginning

    #+begin_src emacs-lisp
      (straight-use-package 'adaptive-wrap)

      (when (fboundp 'adaptive-wrap-prefix-mode)
	(defun acdw/activate-adaptive-wrap-prefix-mode ()
	  "Toggle `visual-line-mode' and `adaptive-wrap-prefix-mode' simultaneously."
	  (adaptive-wrap-prefix-mode (if visual-line-mode
					 +1
				       -1)))
	(add-hook 'visual-line-mode-hook #'acdw/activate-adaptive-wrap-prefix-mode))
    #+end_src

*** Stay snappy with long-lined files

    #+begin_src emacs-lisp
      (when (fboundp 'global-so-long-mode)
	(global-so-long-mode +1))
    #+end_src

*** Killing & Yanking

**** Replace selection when typing

     #+begin_src emacs-lisp
       (delete-selection-mode +1)
     #+end_src

**** Work better with the system clipboard

     #+begin_src emacs-lisp
       (cuss save-interprogram-paste-before-kill t
	 "Save existing clipboard text into the kill ring before
	 replacing it.")

       (cuss yank-pop-change-selection t
	 "Update the X selection when rotating the kill ring.")

       (cuss x-select-enable-clipboard t)
       (cuss x-select-enable-primary t)
       (cuss mouse-drag-copy-region t)
     #+end_src

**** Don’t append the same thing twice to the kill-ring

     #+begin_src emacs-lisp
       (cuss kill-do-not-save-duplicates t)
     #+end_src

*** Searching & Replacing

**** COMMENT Search with CtrlF

     For right now, I’m /just/ using Anzu – I don’t like parts of =isearch= but … CtrlF doesn’t match with that sweet replace flow.

     #+begin_src emacs-lisp
       (straight-use-package 'ctrlf)
       (ctrlf-mode +1)
     #+end_src

**** Replace with Anzu

     #+begin_src emacs-lisp
       (straight-use-package 'anzu)
       (require 'anzu)

       ;; show search count in the modeline
       (global-anzu-mode +1)

       (cuss anzu-replace-to-string-separator " → "
	 "What to separate the search from the replacement.")

       (global-set-key [remap query-replace] #'anzu-query-replace)
       (global-set-key [remap query-replace-regexp] #'anzu-query-replace-regexp)

       (define-key isearch-mode-map [remap isearch-query-replace]  #'anzu-isearch-query-replace)
       (define-key isearch-mode-map [remap isearch-query-replace-regexp] #'anzu-isearch-query-replace-regexp)


     #+end_src

*** Overwrite mode

    #+begin_src emacs-lisp
      (defun acdw/overwrite-mode-change-cursor ()
	(setq cursor-type (if overwrite-mode t 'bar)))

      (add-hook 'overwrite-mode-hook #'acdw/overwrite-mode-change-cursor)

      (rm/whitelist-add "Ovwrt")
    #+end_src

*** The Mark

    #+begin_src emacs-lisp
      (cuss set-mark-repeat-command-pop t
	"Repeat `set-mark-command' with a prefix argument, without
	repeatedly entering the prefix argument.")
    #+end_src

*** Whitespace

    #+begin_src emacs-lisp
      (cuss whitespace-style
	  '(empty ;; remove blank lines at the beginning and end of buffers
	    indentation ;; clean up indentation
	    space-before-tab ;; fix mixed spaces and tabs
	    space-after-tab))

      (defun acdw/whitespace-cleanup-maybe ()
	"Only cleanup whitespace when out-of-focus."
	(acdw/when-unfocused #'whitespace-cleanup))

      (add-hook 'before-save-hook #'acdw/whitespace-cleanup-maybe)
    #+end_src

*** Expand region

    #+begin_src emacs-lisp
      (straight-use-package 'expand-region)

      (global-set-key (kbd "C-=") #'er/expand-region)
    #+end_src

*** Move where I mean

    #+begin_src emacs-lisp
      (straight-use-package 'mwim)
      (require 'mwim)

      (cuss mwim-beginning-of-line-function 'beginning-of-visual-line)
      (cuss mwim-end-of-line-function 'end-of-visual-line)

      (global-set-key (kbd "C-a") #'mwim-beginning)
      (global-set-key (kbd "C-e") #'mwim-end)
    #+end_src

* Programming

** Prettify symbols

   #+begin_src emacs-lisp
     (cuss prettify-symbols-unprettify-at-point 'right-edge
       "Unprettify a symbol when inside it or next to it.")

     (add-hook 'prog-mode-hook #'prettify-symbols-mode)
   #+end_src

** Parentheses

*** Smart parentheses

    #+begin_src emacs-lisp
      (straight-use-package 'smartparens)
      (require 'smartparens-config)

      ;; replace show-paren

      (cuss sp-show-pair-delay 0
	"Don't delay before showing the pairs.")
      (cuss sp-show-pair-from-inside t
	"Highlight the enclosing pair when immediately inside.")

      (add-hook 'prog-mode-hook #'show-smartparens-mode +1)

      ;; enable strict smartparens in prog mode
      (add-hook 'prog-mode-hook #'smartparens-strict-mode)
    #+end_src

** Indent aggressively

   #+begin_src emacs-lisp
     (straight-use-package 'aggressive-indent)

     (global-aggressive-indent-mode +1)
   #+end_src

** Completion

   #+begin_src emacs-lisp
     (straight-use-package 'company)

     (add-hook 'prog-mode-hook #'company-mode)

     (cuss company-idle-delay 0.1
       "Show company sooner.")
     (cuss company-minimum-prefix-length 3
       "Don't try to complete short words.")

     (with-eval-after-load 'company
       (define-key company-active-map (kbd "C-n")
	 (lambda () (interactive) (company-complete-common-or-cycle +1)))
       (define-key company-active-map (kbd "C-p")
	 (lambda () (interactive) (company-complete-common-or-cycle -1))))
   #+end_src

*** Give it a frame and better help

    #+begin_src emacs-lisp
      (straight-use-package 'company-posframe)

      (with-eval-after-load 'company
	(company-posframe-mode +1))
    #+end_src

*** Prescient integration

    #+begin_src emacs-lisp
      (straight-use-package 'company-prescient)

      (add-hook 'company-mode-hook #'company-prescient-mode)
    #+end_src

** Language-specific packages

*** Executable scripts

    #+begin_src emacs-lisp
      (add-hook 'after-save-hook #'executable-make-buffer-file-executable-if-script-p)
    #+end_src

*** Emacs lisp

    #+begin_src emacs-lisp
      (cuss eval-expression-print-length nil
	"Don't truncate printed expressions by length.")
      (cuss eval-expression-print-level nil
	"Don't truncate printed expressions by level.")
    #+end_src

**** Eros (Evaluation Result OverlayS)

     #+begin_src emacs-lisp
       (straight-use-package 'eros)

       (cuss eros-eval-result-prefix ";; => "
	 "Prefix displayed before eros overlays.")

       (eros-mode +1)
     #+end_src

**** Indent Elisp like Common Lisp

#+begin_src emacs-lisp
  (require 'cl-lib)
  (setq lisp-indent-function 'common-lisp-indent-function)
  (put 'cl-flet 'common-lisp-indent-function
       (get 'flet 'common-lisp-indent-function))
  (put 'cl-labels 'common-lisp-indent-function
       (get 'labels 'common-lisp-indent-function))
  (put 'if 'common-lisp-indent-function 2)
  (put 'dotimes-protect 'common-lisp-indent-function
       (get 'when 'common-lisp-indent-function))
#+end_src

*** Janet

    #+begin_src emacs-lisp
      (straight-use-package 'janet-mode)
      (require 'janet-mode)

      (straight-use-package '(inf-janet
			      :host github
			      :repo "velkyel/inf-janet"))

      (add-hook 'janet-mode-hook #'inf-janet-minor-mode)
    #+end_src

*** INI

    #+begin_src emacs-lisp
      (straight-use-package 'ini-mode)

      (add-to-list 'auto-mode-alist
		   '("\\.ini\\'" . ini-mode))
    #+end_src

*** PHP

    see also [[https://sasanidas.gitlab.io/f-site/php-development/][this post by Fermin M]], it looks really useful.

    #+begin_src emacs-lisp
      (straight-use-package 'php-mode)
    #+end_src

* Writing

** Visual fill column

*** Fix scrolling in margins

    This has to be done /before/ loading the package.  It's included in =visual-fill-column=, too, but for some reason isn't loaded there.

    #+begin_src emacs-lisp
      (dolist (margin '(right-margin left-margin))
	(dolist (button '(mouse-1 mouse-2 mouse-3))
	  (global-set-key (vector margin button)
			  (global-key-binding (vector button)))))

      (mouse-wheel-mode +1)

      (when (bound-and-true-p mouse-wheel-mode)
	(dolist (margin '(right-margin left-margin))
	  (dolist (event '(mouse-wheel-down-event
			   mouse-wheel-up-event
			   wheel-down
			   wheel-up
			   mouse-4
			   mouse-5))
	    (global-set-key (vector margin event) #'mwheel-scroll))))
    #+end_src

*** Load the package

    #+begin_src emacs-lisp
      (straight-use-package 'visual-fill-column)

      (cuss visual-fill-column-center-text nil
	"Whether to center the text in the frame.")

      (cuss fill-column 84
	"Width of fill-column, and thus, visual-fill-column.")

      (advice-add 'text-scale-adjust
		  :after #'visual-fill-column-adjust)

      (global-visual-fill-column-mode +1)
    #+end_src

** Typographical niceties

*** Variable pitch in text-modes

    #+begin_src emacs-lisp
      (add-hook 'text-mode-hook #'variable-pitch-mode)
    #+end_src

*** Typo mode

    #+begin_src emacs-lisp
      (straight-use-package 'typo)

      (add-hook 'text-mode-hook #'typo-mode)

      ;; Disable `typo-mode' when inside an Org source block
      (with-eval-after-load 'typo
	(add-to-list 'typo-disable-electricity-functions
		     #'org-in-src-block-p))
    #+end_src

*** Show =^L= as a horizontal line

    #+begin_src emacs-lisp
      (straight-use-package 'form-feed)
      (global-form-feed-mode +1)
    #+end_src

** Word count

   #+begin_src emacs-lisp
     (straight-use-package 'wc-mode)

     (add-hook 'text-mode-hook #'wc-mode)

     (rm/whitelist-add "WC")
   #+end_src

* Applications

** Web browsing

   #+begin_src emacs-lisp
     (cuss browse-url-browser-function 'browse-url-firefox)
     (cuss browse-url-new-window-flag t
       "Always open a new browser window.")

     ;;(cuss browse-url-generic-program "firefox")
     (cuss browse-url-firefox-new-window-is-tab t
       "Or a new tab, in Firefox.")

     ;; we need to add Firefox to `exec-path' on Windows
     (when-at :work
       (add-to-list 'exec-path "c:/Program Files/Mozilla Firefox"))
   #+end_src

** Dired

*** Basic customization

    #+begin_src emacs-lisp
      (defun acdw/setup-dired-mode ()
	(hl-line-mode)
	(dired-hide-details-mode))

      ;; highlight the current line in dired.
      (add-hook 'dired-mode-hook #'acdw/setup-dired-mode)

      (cuss dired-recursive-copies 'always
	"Always recursively copy.")

      (cuss dired-recursive-deletes 'always
	"Always recursively delete.")

      (cuss delete-by-moving-to-trash t)

      (cuss dired-listing-switches "-alh"
	"Show (A)lmost all items, 
      (l)isted out, with (h)uman-readable sizes.")
    #+end_src

*** Expand subtrees

    
    #+begin_src emacs-lisp
      (straight-use-package 'dired-subtree)

      (with-eval-after-load 'dired
	(define-key dired-mode-map "i" #'dired-subtree-toggle))
    #+end_src

*** Collapse singleton directories

    #+begin_src emacs-lisp
      (straight-use-package 'dired-collapse)

      (add-hook 'dired-mode-hook #'dired-collapse-mode)
    #+end_src

*** Kill dired buffers

    from [[https://github.com/munen/emacs.d/][munen]].

    #+begin_src emacs-lisp
      (defun kill-dired-buffers ()
	"Kill all open dired buffers."
	(interactive)
	(mapc (lambda (buffer)
		(when (eq 'dired-mode (buffer-local-value 'major-mode buffer))
		  (kill-buffer buffer)))
	      (buffer-list)))
    #+end_src

** Org mode

   I’ve put org mode under Applications, as opposed to Writing, because it’s  more generally-applicable than that.

*** Basics

    #+begin_src emacs-lisp
      (straight-use-package 'org)

      (with-eval-after-load 'org
	(require 'org-tempo)
	(require 'ox-md)
	(define-key org-mode-map (kbd "M-n") #'outline-next-visible-heading)
	(define-key org-mode-map (kbd "M-p") #'outline-previous-visible-heading))

      (cuss org-hide-emphasis-markers t)
      (cuss org-fontify-done-headline t)
      (cuss org-fontify-whole-heading-line t)
      (cuss org-fontify-quote-and-verse-blocks t)
      (cuss org-pretty-entities t)
      (cuss org-src-tab-acts-natively t)
      (cuss org-src-fontify-natively t)
      (cuss org-src-window-setup 'current-window)
      (cuss org-confirm-babel-evaluate nil)
      (cuss org-directory "~/Org")
      (cuss org-ellipsis "…")
      (cuss org-catch-invisible-edits 'show)
      (cuss org-special-ctrl-a/e t)
      (cuss org-special-ctrl-k t)

      (cuss org-export-headline-levels 8
	"Maximum level of headlines to export /as/ a headline.")
    #+end_src

**** TODO configure =mwim= to work with Org mode

**** Tags

     #+begin_src emacs-lisp
       (cuss org-tags-column 0
	 "Show tags directly after the headline.
       This is the best-looking option with variable-pitch fonts.")

       (cussface
	'(org-tag
	  ((t
	    (:height 0.8 :weight normal :slant italic :foreground "grey40" :inherit
		     (variable-pitch))))))
     #+end_src

***** Align all tags in the buffer on changes

from [[https://github.com/mpereira/.emacs.d#align-all-tags-in-the-buffer-on-tag-changes][mpereira]].

#+begin_src emacs-lisp
  (defun acdw/org-align-all-tags ()
    "Align all org tags in the buffer."
    (interactive)
    (when (eq major-mode 'org-mode)
      (org-align-tags t)))

  (add-hook 'org-after-tags-change-hook #'acdw/org-align-all-tags)
#+end_src

**** Source blocks

     #+begin_src emacs-lisp
       (with-eval-after-load 'org-faces
	 (set-face-attribute 'org-block-begin-line nil
			     :height 0.85))
     #+end_src

**** Prettify

     #+begin_src emacs-lisp
       (defun acdw/org-mode-prettify ()
	 "Prettify `org-mode'."
	 (dolist (cell '(("[ ]" . ?☐) ("[X]" . ?☑) ("[-]" . ?◐)
			 ("#+BEGIN_SRC" . ?✎) ("#+begin_src" . ?✎)
			 ("#+END_SRC" . ?■) ("#+end_src" . ?■)))
	   (add-to-list 'prettify-symbols-alist cell :append))
	 (prettify-symbols-mode +1))

       (add-hook 'org-mode-hook #'acdw/org-mode-prettify)
     #+end_src

*** General

**** [[https://github.com/alphapapa/unpackaged.el#org-return-dwim][Org Return: DWIM]]

     #+begin_src emacs-lisp
   (defun unpackaged/org-element-descendant-of (type element)
     "Return non-nil if ELEMENT is a descendant of TYPE.
   TYPE should be an element type, like `item' or `paragraph'.
   ELEMENT should be a list like that returned by `org-element-context'."
     ;; MAYBE: Use `org-element-lineage'.
     (when-let* ((parent (org-element-property :parent element)))
       (or (eq type (car parent))
	   (unpackaged/org-element-descendant-of type parent))))

   ;;;###autoload
   (defun unpackaged/org-return-dwim (&optional default)
     "A helpful replacement for `org-return'.  With prefix, call `org-return'.

   On headings, move point to position after entry content.  In
   lists, insert a new item or end the list, with checkbox if
   appropriate.  In tables, insert a new row or end the table."
     ;; Inspired by John Kitchin: http://kitchingroup.cheme.cmu.edu/blog/2017/04/09/A-better-return-in-org-mode/
     (interactive "P")
     (if default
	 (org-return)
       (cond
	;; Act depending on context around point.

	;; NOTE: I prefer RET to not follow links, but by uncommenting this block, links will be
	;; followed.

	;; ((eq 'link (car (org-element-context)))
	;;  ;; Link: Open it.
	;;  (org-open-at-point-global))

	((org-at-heading-p)
	 ;; Heading: Move to position after entry content.
	 ;; NOTE: This is probably the most interesting feature of this function.
	 (let ((heading-start (org-entry-beginning-position)))
	   (goto-char (org-entry-end-position))
	   (cond ((and (org-at-heading-p)
		       (= heading-start (org-entry-beginning-position)))
		  ;; Entry ends on its heading; add newline after
		  (end-of-line)
		  (insert "\n\n"))
		 (t
		  ;; Entry ends after its heading; back up
		  (forward-line -1)
		  (end-of-line)
		  (when (org-at-heading-p)
		    ;; At the same heading
		    (forward-line)
		    (insert "\n")
		    (forward-line -1))
		  ;; FIXME: looking-back is supposed to be called with more arguments.
		  (while (not (looking-back (rx (repeat 3 (seq (optional blank) "\n"))) nil))
		    (insert "\n"))
		  (forward-line -1)))))

	((org-at-item-checkbox-p)
	 ;; Checkbox: Insert new item with checkbox.
	 (org-insert-todo-heading nil))

	((org-in-item-p)
	 ;; Plain list.  Yes, this gets a little complicated...
	 (let ((context (org-element-context)))
	   (if (or (eq 'plain-list (car context))  ; First item in list
		   (and (eq 'item (car context))
			(not (eq (org-element-property :contents-begin context)
				 (org-element-property :contents-end context))))
		   (unpackaged/org-element-descendant-of 'item context))  ; Element in list item, e.g. a link
	       ;; Non-empty item: Add new item.
	       (org-insert-item)
	     ;; Empty item: Close the list.
	     ;; TODO: Do this with org functions rather than operating on the text. Can't seem to find the right function.
	     (delete-region (line-beginning-position) (line-end-position))
	     (insert "\n"))))

	((when (fboundp 'org-inlinetask-in-task-p)
	   (org-inlinetask-in-task-p))
	 ;; Inline task: Don't insert a new heading.
	 (org-return))

	((org-at-table-p)
	 (cond ((save-excursion
		  (beginning-of-line)
		  ;; See `org-table-next-field'.
		  (cl-loop with end = (line-end-position)
			   for cell = (org-element-table-cell-parser)
			   always (equal (org-element-property :contents-begin cell)
					 (org-element-property :contents-end cell))
			   while (re-search-forward "|" end t)))
		;; Empty row: end the table.
		(delete-region (line-beginning-position) (line-end-position))
		(org-return))
	       (t
		;; Non-empty row: call `org-return'.
		(org-return))))
	(t
	 ;; All other cases: call `org-return'.
	 (org-return)))))

   (with-eval-after-load 'org
     (define-key org-mode-map (kbd "RET") #'unpackaged/org-return-dwim))
     #+end_src

**** Insert blank lines around headers

     from [[https://github.com/alphapapa/unpackaged.el#ensure-blank-lines-between-headings-and-before-contents][unpackaged.el]].

     #+begin_src emacs-lisp
       ;;;###autoload
       (defun unpackaged/org-fix-blank-lines (&optional prefix)
	 "Ensure that blank lines exist between headings and between headings and their contents.
       With prefix, operate on whole buffer. Ensures that blank lines
       exist after each headings's drawers."
	 (interactive "P")
	 (org-map-entries (lambda ()
			    (org-with-wide-buffer
			     ;; `org-map-entries' narrows the buffer, which prevents us
			     ;; from seeing newlines before the current heading, so we
			     ;; do this part widened.
			     (while (not (looking-back "\n\n" nil))
			       ;; Insert blank lines before heading.
			       (insert "\n")))
			    (let ((end (org-entry-end-position)))
			      ;; Insert blank lines before entry content
			      (forward-line)
			      (while (and (org-at-planning-p)
					  (< (point) (point-max)))
				;; Skip planning lines
				(forward-line))
			      (while (re-search-forward org-drawer-regexp end t)
				;; Skip drawers. You might think that `org-at-drawer-p'
				;; would suffice, but for some reason it doesn't work
				;; correctly when operating on hidden text.  This
				;; works, taken from `org-agenda-get-some-entry-text'.
				(re-search-forward "^[ \t]*:END:.*\n?" end t)
				(goto-char (match-end 0)))
			      (unless (or (= (point) (point-max))
					  (org-at-heading-p)
					  (looking-at-p "\n"))
				(insert "\n"))))
			  t (if prefix
				nil
			      'tree)))
     #+end_src

***** Add a before-save-hook

      #+begin_src emacs-lisp
	(defun cribbed/org-mode-fix-blank-lines ()
	  (when (eq major-mode 'org-mode)
	    (let ((current-prefix-arg 4)) ; Emulate C-u
	      (call-interactively 'unpackaged/org-fix-blank-lines))))

	(add-hook 'before-save-hook #'cribbed/org-mode-fix-blank-lines)
      #+end_src

*** Org Templates (=org-tempo=)

    #+begin_src emacs-lisp
      (with-eval-after-load 'org
	(add-to-list 'org-structure-template-alist
		     '("el" . "src emacs-lisp")))
    #+end_src

*** Org Agenda

    #+begin_src emacs-lisp
      (cuss org-agenda-files
	  (let ((list))
	    (dolist (file '(;; add more files to this list
			    "home.org"
			    "work.org")
			  list)
	      (push (expand-file-name file org-directory) list))))

      (define-key acdw/map (kbd "C-a") #'org-agenda)

      (cuss org-agenda-span 5
	"Show today + N days.")

      (cuss org-todo-keywords
	  '((sequence "RECUR(r)" "TODO(t)" "|" "DONE(d)")
	    (sequence "APPT(a)")
	    (sequence "|" "CANCELLED(c)")))

      (cuss org-agenda-skip-scheduled-if-done t)
      (cuss org-agenda-skip-deadline-if-done t)
      (cuss org-deadline-warning-days 0
	"Don't warn of an impending deadline.")

      (cuss org-log-into-drawer "LOGBOOK"
	"Log state changes into the LOGBOOK drawer, instead of after
	the headline.")
      (cuss org-log-done t
	"Save CLOSED timestamp when done.")
    #+end_src

*** Capture

**** Templates

    #+begin_src emacs-lisp
      (cuss org-default-notes-file (expand-file-name "inbox.org"
						     org-directory)
	"Put unfiled notes in ~/Org/inbox.org.")


      (cuss org-capture-templates
	  '(("w" "Work Todo") ;;; Work stuff
	    ("ws" "Small Business" entry
	     (file+headline "work.org" "Small Business")
	     "* TODO %?
      :PROPERTIES:
      :Via:
      :Note:
      :END:
      :LOGBOOK:
	- State \"TODO\"       from \"\"           %U
      :END:" :empty-lines 1)
	    ("wc" "Career Center" entry
	     (file+headline "work.org" "Career Center")
	     "* TODO %?
      :PROPERTIES:
      :Via:
      :Note:
      :END:
      :LOGBOOK:
	- State \"TODO\"       from \"\"           %U
      :END:" :empty-lines 1)
	    ("wg" "General" entry
	     (file+headline "work.org" "General")
	     "* TODO %?
      :PROPERTIES:
      :Via:
      :Note:
      :END:
      :LOGBOOK:
	- State \"TODO\"       from \"\"           %U
      :END:" :empty-lines 1)

	    ;;; Regular To-Dos
	    ("t" "Todo")
	    ("tt" "Todo" entry
	     (file+headline "home.org" "TODOs")
	     "* TODO %?
      :PROPERTIES:
      :Via:
      :Note:
      :END:
      :LOGBOOK:  
      - State \"TODO\"       from \"\"           %U
      :END:" :empty-lines 1)

	    ("td" "Todo with deadline" entry
	     (file+headline "home.org" "TODOs")
	     "* TODO %?
      DEADLINE: %^t
      :PROPERTIES:
      :Via:
      :Note:
      :END:
      :LOGBOOK:  
      - State \"TODO\"       from \"\"           %U
      :END:" :empty-lines 1)


	    ("g" "Gift idea" entry
	     (file+headline "home.org" "Gift ideas")
	     "* %^{Who?}
      ,* %^{What?}" :empty-lines 1)

	    ("d" "Diary entry" entry
	     (file+olp+datetree "diary.org")
	     "* %?
      Entered on %U

      " :empty-lines 1)))
    #+end_src

**** Keybindings

     #+begin_src emacs-lisp
       (require 'org-capture)

       (with-eval-after-load 'org-capture
	 (define-key acdw/map (kbd "C-c") #'org-capture))
     #+end_src

*** Include Org links in source code

    #+begin_src emacs-lisp
      (straight-use-package '(org-link-minor-mode
			      :host github
			      :repo "seanohalpin/org-link-minor-mode"))

      ;; enable in elisp buffers
      (add-hook 'emacs-lisp-mode-hook #'org-link-minor-mode)
    #+end_src

** Git

   #+begin_src emacs-lisp
     (straight-use-package 'magit)

     (define-key acdw/map "g" #'magit-status)
   #+end_src

*** Git file modes

    #+begin_src emacs-lisp
      (dolist (feat '(gitattributes-mode
		      gitconfig-mode
		      gitignore-mode))
	(straight-use-package feat)
	(require feat))
    #+end_src

** Beancount mode

   #+begin_src emacs-lisp
     (straight-use-package '(beancount-mode
			     :host github
			     :repo "beancount/beancount-mode"))
     (require 'beancount)

     (add-to-list 'auto-mode-alist '("\\.beancount\\'" . beancount-mode))

     (defun acdw/disable-aggressive-indent ()
       "Turn `aggressive-indent-mode' off for a buffer."
       (aggressive-indent-mode -1))

     (add-hook 'beancount-mode-hook #'outline-minor-mode)
     (add-hook 'beancount-mode-hook #'acdw/disable-aggressive-indent)

     (define-key beancount-mode-map (kbd "M-n") #'outline-next-visible-heading)
     (define-key beancount-mode-map (kbd "M-p") #'outline-previous-visible-heading)
   #+end_src

*** Company integration with company-ledger

    #+begin_src emacs-lisp
      (straight-use-package 'company-ledger)

      (with-eval-after-load 'company
	(add-to-list 'company-backends 'company-ledger))
    #+end_src

** PDF Tools

   I’m only enabling this at home for now, since it requires building stuff.

   #+begin_src emacs-lisp
     (defun acdw/disable-visual-fill-column-mode ()
       "Disable `visual-fill-column-mode'."
       (visual-fill-column-mode -1))

     (eval-when-compile
       (when-at :home
	 (straight-use-package 'pdf-tools)
	 (pdf-loader-install)

	 (add-hook 'pdf-view-mode-hook #'acdw/disable-visual-fill-column-mode)))
   #+end_src

** E-book tools

   #+begin_src emacs-lisp
     (straight-use-package 'nov)

     (add-to-list 'auto-mode-alist '("\\.epub\\'" . nov-mode))

     (cuss nov-text-width t
       "Disable text filling -- `visual-fill-column-mode' takes care
       of that.")

     (defun acdw/setup-nov-mode ()
       (visual-fill-column-mode +1)
       (setq visual-fill-column-center-text t)
       (text-scale-increase +1))

     (add-hook 'nov-mode-hook #'acdw/setup-nov-mode)
   #+end_src

** Email

   #+begin_src emacs-lisp
     (when-at (:home (executable-find "mu"))
       (add-to-list 'load-path
		    "/usr/share/emacs/site-lisp/mu4e")
       (require 'mu4e)

       (cuss mail-user-agent 'mu4e-user-agent)

       (cuss mu4e-headers-skip-duplicates t)
       (cuss mu4e-view-show-images t)
       (cuss mu4e-view-show-addresses t)
       (cuss mu4e-compose-format-flowed t)
       (cuss mu4e-change-filenames-when-moving t)
       (cuss mu4e-attachments-dir "~/Downloads")

       (cuss mu4e-maildir "~/.mail/fastmail")
       (cuss mu4e-refile-folder "/Archive")
       (cuss mu4e-sent-folder "/Sent")
       (cuss mu4e-drafts-folder "/Drafts")
       (cuss mu4e-trash-folder "/Trash")

       (fset 'my-move-to-trash "mTrash")
       (define-key mu4e-headers-mode-map (kbd "d") 'my-move-to-trash)
       (define-key mu4e-view-mode-map (kbd "d") 'my-move-to-trash)

       (cuss message-send-mail-function 'smtpmail-send-it)
       (cuss smtpmail-default-smtp-server "smtp.fastmail.com")
       (cuss smtpmail-smtp-server "smtp.fastmail.com")
       (cuss smtpmail-stream-type 'ssl)
       (cuss smtpmail-smtp-service 465)
       (cuss smtpmail-local-domain "acdw.net")
       (cuss mu4e-compose-signature
	   "Best,\nCase\n")

       (cuss mu4e-completing-read-function 'completing-read)
       (cuss message-kill-buffer-on-exit t)
       (cuss mu4e-confirm-quit nil)

       (cuss mu4e-bookmarks
	   '((:name "Unread"
	      :query
	      "flag:unread AND NOT flag:trashed AND NOT maildir:/Spam"
	      :key ?u)
	     (:name "Today"
	      :query 
	      "date:today..now and not flag:trashed and not maildir:/Spam"
	      :key ?t)
	     (:name "This week"
	      :query
	      "date:7d..now and not maildir:/Spam and not flag:trashed"
	      :hide-unread t
	      :key ?w)))

       (cuss mu4e-headers-fields
	   '((:human-date . 12)
	     (:flags . 6)
	     (:mailing-list . 10)
	     (:from-or-to . 22)
	     (:thread-subject)))

       (cuss mu4e-maildir-shortcuts
	   `(("/INBOX" . ?i)
	     (,mu4e-refile-folder . ?a)
	     (,mu4e-sent-folder . ?s)
	     (,mu4e-drafts-folder . ?d)
	     (,mu4e-trash-folder . ?t)))

       (cuss mu4e-get-mail-command (cond ((executable-find "mbsync")
					  "mbsync -a"))
	 "The command to update mail with.")
       (cuss mu4e-update-interval 300
	 "Update automatically every 5 minutes.")

       (defun acdw/setup-mu4e-headers-mode ()
	 (visual-line-mode -1))
       (add-hook 'mu4e-headers-mode #'acdw/setup-mu4e-headers-mode)

       (defun acdw/setup-mu4e-view-mode ()
	 (setq visual-fill-column-center-text t)
	 (visual-fill-column-mode +1))
       (add-hook 'mu4e-view-mode-hook #'acdw/setup-mu4e-view-mode)
       (add-hook 'mu4e-compose-mode-hook #'acdw/setup-mu4e-view-mode)

       (declare-function mu4e "mu4e")
       (mu4e +1))
   #+end_src

*** Add a keybinding

    #+begin_src emacs-lisp
      (defun acdw/mu4e-or-warn ()
	"If `mu4e' is around, run it, or tell the user it isn't."
	(interactive)
	(if (featurep 'mu4e)
	    (mu4e)
	  (warn "Mu4e isn't available :/.")))

      (define-key acdw/map "m" #'acdw/mu4e-or-warn)
    #+end_src

** Smolweb

*** A common function to make a cohesive smolweb experience

    #+begin_src emacs-lisp
      (defun acdw/setup-smolweb ()
	"Configure emacs to view the smolweb."
	(setq visual-fill-column-center-text t)
	(visual-fill-column-mode +1)
	(visual-line-mode +1)
	(variable-pitch-mode -1)
	(text-scale-increase +1))
    #+end_src

*** Elpher

    #+begin_src emacs-lisp
      (straight-use-package '(elpher
			      :repo "git://thelambdalab.xyz/elpher.git"))

      (with-eval-after-load 'no-littering
	(cuss elpher-certificate-directory
	    (no-littering-expand-var-file-name "elpher-certificates/")))

      (cuss elpher-ipv4-always t)

      (cussface '(elpher-gemini-heading1
		  ((t (:inherit (modus-theme-heading-1 variable-pitch))))))
      (cussface '(elpher-gemini-heading2
		  ((t (:inherit (modus-theme-heading-2 variable-pitch))))))
      (cussface '(elpher-gemini-heading3
		  ((t (:inherit (modus-theme-heading-3 variable-pitch))))))

      (defun elpher:eww-browse-url (original url &optional new-window)
	"Handle gemini/gopher links with eww."
	(cond ((string-match-p "\\`\\(gemini\\|gopher\\)://" url)
	       (require 'elpher)
	       (elpher-go url))
	      (t (funcall original url new-window))))
      (advice-add 'eww-browse-url :around 'elpher:eww-browse-url)

      (with-eval-after-load 'elpher
	(define-key elpher-mode-map "n" #'elpher-next-link)
	(define-key elpher-mode-map "p" #'elpher-prev-link)
	(define-key elpher-mode-map "o" #'elpher-follow-current-link)
	(define-key elpher-mode-map "G" #'elpher-go-current))

      (add-hook 'elpher-mode-hook #'acdw/setup-smolweb)

      (autoload 'elpher-bookmarks "elpher")
      (define-key acdw/map "e" #'elpher-bookmarks)
    #+end_src

*** Gemini-mode

    #+begin_src emacs-lisp
      (straight-use-package '(gemini-mode
			      :repo "https://git.carcosa.net/jmcbray/gemini.el.git"))

      (add-to-list 'auto-mode-alist
		   '("\\.\\(gemini\\|gmi\\)\\'" . gemini-mode))

      (cussface '(gemini-heading-face-1
		  ((t (:inherit (elpher-gemini-heading1))))))
      (cussface '(gemini-heading-face2
		  ((t (:inherit (elpher-gemini-heading2))))))
      (cussface '(gemini-heading-face3
		  ((t (:inherit (elpher-gemini-heading3))))))

      (add-hook 'gemini-mode-hook #'acdw/setup-smolweb)
    #+end_src

*** Gemini-write

    #+begin_src emacs-lisp
      (straight-use-package '(gemini-write
			      :repo "https://tildegit.org/acdw/gemini-write.git"))
      (require 'gemini-write)
    #+end_src

** RSS

*** elfeed

    #+begin_src emacs-lisp
      (straight-use-package 'elfeed)
      (require 'elfeed)
      (define-key acdw/map "w" 'elfeed)

      (cuss elfeed-use-curl (executable-find "curl"))
      (cuss elfeed-curl-extra-arguments '("--insecure")
	"Extra arguments for curl.")
      (elfeed-set-timeout (* 60 3))

      (defun acdw/setup-elfeed-show ()
	(setq visual-fill-column-center-text t)
	(visual-fill-column-mode +1))

      (add-hook 'elfeed-show-mode-hook #'acdw/setup-elfeed-show)
    #+end_src

*** elfeed-protocol

    #+begin_src emacs-lisp
      (straight-use-package 'elfeed-protocol)
      (require 'elfeed-protocol)

      (cuss elfeed-protocol-ttrss-maxsize 200)

      (cuss elfeed-feeds (list
			  (list "ttrss+https://acdw@rss.tildeverse.org"
				:use-authinfo t)))

      ;; Uncomment this line if elfeed is giving problems.
      ;; (setq elfeed-log-level 'debug)

      (elfeed-protocol-enable)

      (defun acdw/update-elfeed-protocol-feeds ()
	"Wrap various (non-working) protocol updaters.
      I could probably do this in a much more ... better way."
	(interactive)
	(elfeed-protocol-ttrss-reinit "https://rss.tildeverse.org"))

      (with-eval-after-load 'elfeed
	(define-key elfeed-search-mode-map "G" #'acdw/update-elfeed-protocol-feeds))
    #+end_src

* System integration

** Linux

*** Exec path from shell

    #+begin_src emacs-lisp
      (eval-when-compile
	(when-at :home
	  (straight-use-package 'exec-path-from-shell)
	  (defvar acdw/exec-path-from-shell-initialized nil
	    "Stores whether we've initialized or not.")
	  (unless acdw/exec-path-from-shell-initialized
	    (exec-path-from-shell-initialize)
	    (setq acdw/exec-path-from-shell-initialized (current-time)))))
    #+end_src

* Appendices
  :PROPERTIES:
  :header-args: :tangle LICENSE :comments no
  :END:

** Emacs' files

*** init.el

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

      (setq load-prefer-newer t)

      (let* (;; Speed up init
	     (gc-cons-threshold most-positive-fixnum)
	     (file-name-handler-alist nil)
	     ;; Config file names
	     (conf (expand-file-name "config"
				     user-emacs-directory))
	     (conf-el (concat conf ".el"))
	     (conf-org (concat conf ".org")))
	(unless (and (file-newer-than-file-p conf-el conf-org)
		     (load conf 'no-error))
	  ;; A plain require here just loads the older `org'
	  ;; in Emacs' install dir.  We need to add the newer
	  ;; one to the `load-path', hopefully that's all.
	  (add-to-list 'load-path (expand-file-name "straight/build/org"
						    user-emacs-directory))
	  (require 'org)
	  (org-babel-load-file conf-org)))
    #+end_src

*** early-init.el

    #+begin_src emacs-lisp :tangle early-init.el
      ;; early-init.el -*- no-byte-compile: t; -*-

      ;; I use `straight.el' instead of `package.el'.
      (setq package-enable-at-startup nil)

      ;; Don't resize the frame when loading fonts
      (setq frame-inhibit-implied-resize t)
      ;; Resize frame by pixels
      (setq frame-resize-pixelwise t)
    #+end_src

** Ease tangling and loading of Emacs' init

   #+begin_src emacs-lisp :tangle config.el
     (defun refresh-emacs (&optional disable-load)
       "Tangle `config.org', then byte-compile the resulting files.
     Then, load the byte-compilations unless passed with a prefix argument."
       (interactive "P")
       (let ((config (expand-file-name "config.org" user-emacs-directory)))
	 (save-mark-and-excursion
	   (with-current-buffer (find-file-noselect config)
	     (let ((prog-mode-hook nil))
	       ;; generate the readme
	       (when (file-newer-than-file-p config (expand-file-name
						     "README.md"
						     user-emacs-directory))
		 (message "%s" "Exporting README.md...")
		 (require 'ox-md)
		 (with-demoted-errors "Problem exporting README.md: %S"
		   (org-md-export-to-markdown)))
	       ;; tangle config.org
	       (when (file-newer-than-file-p config (expand-file-name
						     "config.el"
						     user-emacs-directory))
		 (message "%s" "Tangling config.org...")
		 (add-to-list 'load-path (expand-file-name "straight/build/org/"
							   user-emacs-directory))
		 (require 'org)
		 (let ((inits (org-babel-tangle)))
		   ;; byte-compile resulting files
		   (message "%s" "Byte-compiling...")
		   (dolist (f inits)
		     (when (string-match "\\.el\\'" f)
		       (byte-compile-file f (not disable-load)))))))))))
   #+end_src

** Ancillary scripts

*** emacsdc
    :PROPERTIES:
    :header-args: :mkdirp yes :tangle-mode (identity #o755)
    :END:

    Here's a wrapper script that'll start =emacs --daemon= if there isn't
    one, and then launch =emacsclient= with the arguments.  I'd recommend
    installing with either ~ln -s bin/emacsdc $HOME/.local/bin/~, or
    adding =$HOME/.local/bin= to your =$PATH=.
 
    #+begin_src sh :tangle bin/emacsdc :shebang "#!/bin/sh"
      if ! emacsclient -nc "$@" 2>/dev/null; then
	  emacs --daemon
	  emacsclient -nc "$@"
      fi
    #+end_src

*** Emacs.cmd
    :PROPERTIES:
    :header-args: :mkdirp yes
    :END:

    Here's a wrapper script that'll run Emacs on Windows, with a
    custom =$HOME=.  I have mine setup like this: Emacs is downloaded
    from [[https://mirrors.tripadvisor.com/gnu/emacs/windows/emacs-27/emacs-27.1-x86_64.zip][the GNU mirror]] and unzipped to =~/Downloads/emacs/=.  For
    some reason, Emacs by default sets =$HOME= to =%APPDATA%=, which
    doesn’t make a lot of sense to me.  I change it to
    =%USERPROFILE%\Downloads\home=, and then run Emacs with the
    supplied arguments.

    As far as creating a shortcut to the Desktop, you’ll have to do
    that yourself – /apparently/ Windows doesn’t have a built-in
    shortcut-creating software >:(.

    #+begin_src bat :tangle bin/Emacs.cmd
      REM Set variables
      set HOME=%USERPROFILE%\Downloads\home
      set EMACS=%USERPROFILE%\Downloads\emacs\bin\runemacs.exe

      REM Change the directory
      chdir %HOME%

      REM Run "Quick Mode"
      REM "%EMACS%" -Q %*

      REM Regular
      "%EMACS%" %*
    #+end_src

** License
   :PROPERTIES:
   :header-args: :tangle LICENSE :comments no
   :END:

   Copyright © 2020 Case Duckworth <acdw@acdw.net>

   This work is free.  You can redistribute it and/or modify it under the
   terms of the Do What the Fuck You Want To Public License, Version 2,
   as published by Sam Hocevar.  See the =LICENSE= file, tangled from the
   following source block, for details.

   #+begin_src text
     DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE

     Version 2, December 2004

     Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>

     Everyone is permitted to copy and distribute verbatim or modified copies of
     this license document, and changing it is allowed as long as the name is changed.

     DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE

     TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

     0. You just DO WHAT THE FUCK YOU WANT TO.
   #+end_src

*** Note on the license

    It's highly likely that the WTFPL is completely incompatible with the
    GPL, for what should be fairly obvious reasons.  To that, I say:

    *SUE ME, RMS!*

** TODO Local variables 

One day, I’m going to add a Local Variables block to the end of this file, which will add an =after-save-hook= to auto-tangle the file after saving.  But first I need to research how best to do that asynchronously.  So.