summary refs log tree commit diff stats
path: root/HACKING.md
blob: a961e2e476978fa1c301dfef823a54cbbbb4355c (plain) (blame)
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
Guidelines for Code Modification
================================

Coding Style
------------

* Use syntax compatible to both python 2.6+ and 3.1+.
* Use docstrings with pydoc in mind
* Follow the style guide for python code:
    http://www.python.org/dev/peps/pep-0008/
* Test the code with "doctest" where it makes sense
* when breaking backward compatibility with old configuration files or plugins,
  please include a temporary workaround code that provides a compatibility
  layer and mark it with a comment that includes the word "COMPAT".  For
  examples, grep the code for the word "COMPAT". :)


Patches
-------

Send patches, created with "git format-patch", to the email address

    hut@hut.pm

or open a pull request on GitHub.

Please use PGP-encryption for security-relevand patches or messages.  The UIDs
of my key are `huterich <hut@lavabit.com>` and `hut <hut@hut.pm>`, my
fingerprint is `1E9B 36EC 051F F6F7 FFC9  69A7 F08C E1E2 00FB 5CDF` and my full
pubkey is at the very bottom of this file.


Version Numbering
-----------------

Three numbers, A.B.C, where
* A changes on a rewrite
* B changes when major configuration incompatibilities occur
* C changes with each release


Starting Points
---------------

Good places to read about ranger internals are:

* ranger/core/actions.py
* ranger/container/fsobject.py

About the UI:

* ranger/gui/widgets/browsercolumn.py
* ranger/gui/widgets/browserview.py
* ranger/gui/ui.py


Common Changes
==============

Adding options
--------------

* Add a default value in rc.conf, along with a comment that describes the option.
* Add the option to the ALLOWED_SETTINGS dictionary in the file
  ranger/container/settings.py.  Make sure to sort in the new entry
  alphabetically.
* Add an entry in the man page by editing doc/ranger.pod, then rebuild the man
  page by running "make man" in the ranger root directory

The setting is now accessible with self.settings.my_option, assuming self is a
subclass of ranger.core.shared.SettingsAware.


Adding colorschemes
-------------------

* Copy ranger/colorschemes/default.py to ranger/colorschemes/myscheme.py
  and modify it according to your needs.  Alternatively, create a subclass of
  ranger.colorschemes.default.Default and override the "use" method, as it is
  done in the "Jungle" colorscheme.

* Add this line to your ~/.config/ranger/rc.conf:

    set colorscheme myscheme


Change which programs start which file types
--------------------------------------------

Edit the configuration file ~/.config/ranger/rifle.conf.  The default one can
be obtained by running "ranger --copy-config rifle".


Change which file extensions have which mime type
-------------------------------------------------

Modify ranger/data/mime.types.  You may also add your own entries to ~/.mime.types


Change which files are previewed in the auto preview
----------------------------------------------------

In ranger/container/file.py, change the constant PREVIEW_BLACKLIST


PGP key
=======

You may wish to send the author (hut@hut.pm) PGP-encrypted mails for
security-relevant messages.  This is the authors key.  Save everything from the
`BEGIN PGP PUBLIC KEY BLOCK` up until the `END PGP PUBLIC KEY BLOCK` message
into a file and import it with `gpg --import <filename>`.

```
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2

mQINBEyOeTcBEACwCWkaA9XTE+DtjaCj2mm1EXslelop0JQco+j/KC5haPsYJ0G3
6lH/UpoXNfYw6B84QxILjodK/uKMk5+1RFS2YX/HoypMOobQLV9LHqo4TvRNmwWA
u8K446nSE9CDx5yvWYkvj1HucV1GrKqNeDOPcbThRVOPasnwZwf5nmLaYWjn780t
nhiWP8OR75/EceyJM2cryASrqgfWRKZoLLioRd67qUxi8zVG9nZIwxIx2OgWvW30
VMsiMvHR6faSOFxBXecag/muu9y5kdYxYxK2YpWhNpxZq8H+dqx2l/0z45nom56A
c1aeL3/QoBpBm7GIaeRgcFEHhKAZI3oOQhLmQ8pbJ9WabhdyA7xbsdNrzMGLQGkY
qYMk3iwP129ciG9FyZlwqhW3WaQST7hGmC3QKfo+kGLiJZMox3QWKD/lPtmBt1Ax
VtDzOq3rbI8ab4OGa7wWJWKOgP+XaCHwy5yIVaCXMuotBqfFR4HJDL8WzfosJ0Du
/zjNDyGVER8iF5+Kq5tahtYmmK0T30HMPPvc1qMsZsFsCgVbmQqxr6PgcAvnlvTN
UWqGwd3G0BeBGiJrxlWHOsUz94eWCM/DILE2F0Io5SQXpK8FQ0BjeaWGv+NkgYZ+
WJ1wwcATgyGXD3GwABIwSZ8T/w55LRPP5R9sYd1SdlMHFeZ3SJ2Y8ZyC4QARAQAB
tBBodXQgPGh1dEBodXQucG0+iQI6BBMBCAAkAhsDBQsJCAcCBhUICQoLAgQWAgMB
Ah4BAheABQJWKKDPAhkBAAoJEPCM4eIA+1zfroMP/1NsAAlReX2WXf1voxKj83mi
Deae94dd76uVuc7YqQR0aB1WBb8yw8oXPzzCkHG7QKF9FjRWuiA1Y1z3KQmcnsXS
F6FVNistVA8Pq52DIR1TyULeN68/4m6C45CflnCMwWIP0rgQgWv0Bg7MhHtNbKTq
kbpNfXUc2OZRzjLN03Oe7+hMgn/EmPBBUARYQ4AlearCse1Sy6XA5Sl0T3R9XtlJ
/hstyNsOoBd2Q1lfeckP4NuHtEXsCF1eCTS/TGwKmls//RzKB3q3929TDazujKzK
eRpT+ZYbW5xBUrUkHdFswuzur3b7YzNjOWKM5Z4wjLJWtk+6fEKSbT6ZDIyX8UkX
WVcbO3PittJJFwqLkmbAvg9w4j3drqdP/sguJIjCjw1bHK/OgJ4SXhQITyBBs4R0
EUQLlIGi7eFywLPiQF9u0A6ufsPEto/5lPj0wZvpbtzbtTv/8yMrW9YfWhZ5aghw
CvTZlsLp3K97CS2Dpid+7PKrUO4/JqLwKpPHaocXBLGwPfqMOKoNCq4Z58ftk2BN
7Ku+aRN1L9rLK6mTaE388ng1pigArqDFtyOOtzCb4NKKtBleSNVTLYe87HyjRMbw
v8llkvssrpqprEmii50B0PWZQzfNFXBrg1jJ5LSz/v+mzt3yNn9ddurqd2rftvSo
z+VtvqISACgpnIwZb2UatBpodXRlcmljaCA8aHV0QGxhdmFiaXQuY29tPokCOAQT
AQIAIgUCTI55NwIbAwYLCQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQ8Izh4gD7
XN+VehAAkntarXp6vJ7oTVhfCmBgl9WsNEKtvt8JfGVqXrd/mkVblEENKPTqXcC+
TeO/n+hcXN7r0ZJEgGsHPBoz9uoShqpxCUYBCXKKf77ffMqemu3W2jNcCDN4+T4q
WSGC+NHcjmutjg78bM7CsznRGV6mDl9civrDNxMx2iELEoBImhdMCl8b/a8mOHcH
umzWps4xR0Bd0LNnnVzWk5TGLKxrRSa+Ee+MdhnZLFF5mth1Sgcrs4v+2mWMJD88
oWcS9sjK48Wo3dvNiRVh6VkBGn60LcYT7ggo7T6mdF1ZdmDY6mQO/yJuwXICsO3r
dOq58S3NrXrnvw7SLMmh5Wqu7xEjhVMgNmlI7yEmwd79wqW6qEDEP2Z8oe3t6CoS
XvnF4LzJ5ibX1WDcES0V05eSMKk8rK9JS4h3ytpWvOOs+SI7OnggiIU4ed+jUKgx
rtbxXkCc+CJaH5Ne3lmEJsifHGuRIqKld6nQK3bMNaNh2vFbvLOpH6BYKefDQblX
IieKNpVVAfdzeSq3d3EXvG21v78Cfh4FM6nc6kIVYA1CRTyNElhgh6FwdcV1RUlr
IQ4Zq+GJVnpDKucuWzAX980UjZlEZrZxGT+270Dqu2HpBPPhJsSoirvHGmMHTWnt
/oUsIEC7ba0yh+y0jFR3fJYn6NjML9O0SGrMC8cBSVTa1tCG+VWJARwEEAECAAYF
AlIwNVEACgkQ0/BQFp1SsougYwgAlYUKtlvw0/PlOOkzx7yfDdkmAMKpxCue1Wnt
hDCKTY49zT+iBqxVXbmELHqPGxwE8p/ACFqGP+vFAT1JvdftmQeehf98wi7kwtHm
ClBJPvIBY3Na6yZqYl3Q39cGzsrlldoMAPL/R28i+eY3KkivaHp6Y2LzzIIEUnKR
OCDpWpNjdIWT7q6pe14gSLeFiegpnZEGD1sQmniHbuwChc3ud17ojI2sFN/tbL6M
D3O1CfvEbC5XpgKirbsKY2UOiONq8YbwV62eAAx/HJWGLPrAZJH9wB4VP09+wOND
YiQ2pCotpEzjE2AMOD5NlPRizo/TwsgRZIVNPvYG3RaTbbMyBokCHAQQAQIABgUC
UsVNhAAKCRA1UveuA2o4tQW4D/9QG+gVxUroxr3Lx8T0xt+4eBM8skCDRfTj/tRg
/h2D/y1xXp84pik9zKuDYbvB+02IUF2CTm5oJirrX7Mdgti0GGIS2LN8a2aVSac3
Wqfv8P81v+edMxbOO7aYPD2eenbVCd3wNg18c2GrsTyw9FzcJHDfqc0lp16wnbUt
j4ZUMzY1KrN1TEC+Z0fHAmanUsj/i1eSIiGaJxzsSHRpUltv8OoiT2WeKIzlIVoA
bd92d6uLR9NTdXw2fdORJFvQQWEKbd26oZK7gLRtrYVm1VfwCN6lcVGEu9nSeg2W
o53C48TpfzE3XrEnSbAicsWV2z9QQv187sTbY6VJbXPpSlIhJU0aLh9bbsSyvmTs
QDtw4D28HdNrc1XClQgF7mjYC6VTlCUK9lTcmrACzfUillv+XD0Pr6VhtpcN78ie
1LxOG2BBLdu2Ww9GeR6pR6WAyUg28h5nqkBwvlkiufMbTJ80IaF5DmyYbUlFT21j
Ben6CvqstMngbcQ74DwUaO4iIvsMJ7WPWgaiVdH113a4Scl+WIbcE95q+Xcrg2n+
FidCeccD88bXH+c1Zu292NSQ+Kn0XvGbfY+1vFxJ6A+Gx03bTR4uOhKGWMS9IrSd
pxHedrDGn/DJfZKifgVhyzrFEvPvxifIJidbVr7wwUQGANtVdsHs1vVF/ZrCRGtH
ot8dSLkCDQRMjnk3ARAAqwzAE/LKceQXCctfwtNEBjcv/5ZFWmSWDx2wCuhAg1/p
veTWBHU4ClpGSvfOLYRfpy8KwgPFK9sOS3lTDCDEDJOi1DvThhkbby8hqVqc6/r3
q4YvQDaNtb+i3IxX3b66eXX36HzWBbMhHvZjywOE1SAbv4YLdOGh9Pf5wxnssFnR
eRkQ2UdmxUgD2TJKiyOQGkl1QCbvlIta0iz3ku92tqTXtM8iLG60KZX+mi/ckEOR
kh4reAGUCW3PvRCRXHnwnEuULkE5Q5haOtfqFJxEBgAV2CLUzsLU8/ATiHpjXQpN
8ybkVB5AlFZnLqc4VOxn7Wz+aP23X9yhSlaRkGZKrzs2gv/JJTg9zV/qPc/5L8LV
qiEDs0wFybDfmkUrYbNW3CtTTDA9vqqM/ifjxXcZbOBzXbXevJdYWy9CgWhhS+vS
W/uVE55yStQ2fdDJIqa9A4bqmEh6/w1Aloa3I1JPAikjbAv74n8zdnbCqa/+36Uo
YV0yHFHJPVzWAXbXR83LkVc9R1s5rlhXnuP51AZWI/KFOBWeBQ0/mXF9X1A3ayPD
yq5j8InU9ZbQ3IA9UgjhItRHZXY9waCOdAT/m79Ka+3YLTZyyc7NUWhu54dyixWJ
FE1xZ/hIAgvFxCYDeln29a+sSGmcd7jsx/9m61gWfnkkR2yIyfoJxdrlKR29LvEA
EQEAAYkCHwQYAQIACQUCTI55NwIbDAAKCRDwjOHiAPtc3yz1EACW4wzhsdvtQ7Xi
Zb8XHlSGyY8oMUCVxoUIw4qGO1KpzC8lnCyqinKi9KiNoBKlwHeOzpzwWD4K8uSl
WRmw2YyG0RTfSauzsKKA7DzhL2RD0BsG6R86U3cl7smPGqkxW/CDGYHbozAz2sB6
4f4HQ+PtEsefiBe+Fkb4t0QhID8jDzTriEsENi5ITD8LSH3qVyJuRllD6JxIS6Nz
7kFuo3+Ng3N6SPB6vR+Az/XlHwAoUAmE+AF/aukbwHlOmWnrpik0hCPzkdpOjhwc
+8URaQ5oCXjQv0C04Lzukn9SuSHI26btvtDbiGW0GCANV2/lIkJciZ21V+E9wHa7
+kUSLJO6XXa00EOSP5xStXEQ5bSroA1n6NllO19EigrvNXH34pyxPezL8UcWNyM4
8k3W7hv9uR0Axa7eupYYg/17zgRbV0gZjvvR+p+pCvnyOxd9aQWE0bjnSrgzWdgE
c8ksiQ6yH1ta99HzBFDwneaxlQsPN8OW1pI0XmCa25GSOXNf5IwgYwRhqVKnz7uu
j70MUp6UIpNcceBiWVrlyrF1QBM3iUsa50vQSAXN7qDlTELCu3NthpyyywV3CwML
kPlUuRpx8YXXP1sPjV3Zfek/1ILqgftpXMImkmNSn+vZ/F8uM7RtnIECkOGS/hWI
/Vp3S8rQGp/PJd/zIzW3VjwD4anL3g==
=n2ru
-----END PGP PUBLIC KEY BLOCK-----
```
ref='#n720'>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
# .tlv file generated by https://github.com/akkartik/teliva
# You may edit it if you are careful; however, you may see cryptic errors if you
# violate Teliva's assumptions.
#
# .tlv files are representations of Teliva programs. Teliva programs consist of
# sequences of definitions. Each definition is a table of key/value pairs. Keys
# and values are both strings.
#
# Lines in .tlv files always follow exactly one of the following forms:
# - comment lines at the top of the file starting with '#' at column 0
# - beginnings of definitions starting with '- ' at column 0, followed by a
#   key/value pair
# - key/value pairs consisting of '  ' at column 0, containing either a
#   spaceless value on the same line, or a multi-line value
# - multiline values indented by more than 2 spaces, starting with a '>'
#
# If these constraints are violated, Teliva may unceremoniously crash. Please
# report bugs at http://akkartik.name/contact
- __teliva_timestamp: original
  str_helpers:
    >-- some string helpers from http://lua-users.org/wiki/StringIndexing
    >
    >-- index characters using []
    >getmetatable('').__index = function(str,i)
    >  if type(i) == 'number' then
    >    return string.sub(str,i,i)
    >  else
    >    return string[i]
    >  end
    >end
    >
    >-- ranges using (), selected bytes using {}
    >getmetatable('').__call = function(str,i,j)
    >  if type(i)~='table' then
    >    return string.sub(str,i,j)
    >  else
    >    local t={}
    >    for k,v in ipairs(i) do
    >      t[k]=string.sub(str,v,v)
    >    end
    >    return table.concat(t)
    >  end
    >end
    >
    >-- iterate over an ordered sequence
    >function q(x)
    >  if type(x) == 'string' then
    >    return x:gmatch('.')
    >  else
    >    return ipairs(x)
    >  end
    >end
    >
    >-- insert within string
    >function string.insert(str1, str2, pos)
    >  return str1:sub(1,pos)..str2..str1:sub(pos+1)
    >end
    >
    >function string.remove(s, pos)
    >  return s:sub(1,pos-1)..s:sub(pos+1)
    >end
    >
    >-- TODO: backport utf-8 support from Lua 5.3
- __teliva_timestamp: original
  debugy:
    >debugy = 5
- __teliva_timestamp: original
  dbg:
    >-- helper for debug by print; overlay debug information towards the right
    >-- reset debugy every time you refresh screen
    >function dbg(window, s)
    >  local oldy = 0
    >  local oldx = 0
    >  oldy, oldx = window:getyx()
    >  window:mvaddstr(debugy, 60, s)
    >  debugy = debugy+1
    >  window:mvaddstr(oldy, oldx, '')
    >end
- __teliva_timestamp: original
  check_eq:
    >function check_eq(x, expected, msg)
    >  if eq(x, expected) then
    >    Window:addch('.')
    >  else
    >    print('F - '..msg)
    >    print('  expected '..str(expected)..' but got '..str(x))
    >    teliva_num_test_failures = teliva_num_test_failures + 1
    >    -- overlay first test failure on editors
    >    if teliva_first_failure == nil then
    >      teliva_first_failure = msg
    >    end
    >  end
    >end
- __teliva_timestamp: original
  eq:
    >function eq(a, b)
    >  if type(a) ~= type(b) then return false end
    >  if type(a) == 'table' then
    >    if #a ~= #b then return false end
    >    for k, v in pairs(a) do
    >      if b[k] ~= v then
    >        return false
    >      end
    >      return true
    >    end
    >  end
    >  return a == b
    >end
- __teliva_timestamp: original
  str:
    >-- smarter tostring
    >-- slow; used only for debugging
    >function str(x)
    >  if type(x) == 'table' then
    >    local result = ''
    >    result = result..#x..'{'
    >    for k, v in pairs(x) do
    >      result = result..str(k)..'='..str(v)..', '
    >    end
    >    result = result..'}'
    >    return result
    >  end
    >  return tostring(x)
    >end
- __teliva_timestamp: original
  map:
    >-- only for arrays
    >function map(l, f)
    >  result = {}
    >  for _, x in ipairs(l) do
    >    table.insert(result, f(x))
    >  end
    >  return result
    >end
- __teliva_timestamp: original
  reduce:
    >-- only for arrays
    >function reduce(l, f, init)
    >  result = init
    >  for _, x in ipairs(l) do
    >    result = f(result, x)
    >  end
    >  return result
    >end
- __teliva_timestamp: original
  filter:
    >-- only for arrays
    >function filter(l, f)
    >  result = {}
    >  for _, x in ipairs(l) do
    >    if f(x) then
    >      table.insert(result, x)
    >    end
    >  end
    >  return result
    >end
- __teliva_timestamp: original
  find_index:
    >function find_index(arr, x)
    >  for n, y in ipairs(arr) do
    >    if x == y then
    >      return n
    >    end
    >  end
    >end
- __teliva_timestamp: original
  trim:
    >function trim(s)
    >  return s:gsub('^%s*', ''):gsub('%s*$', '')
    >end
- __teliva_timestamp: original
  split:
    >function split(s, d)
    >  result = {}
    >  for match in (s..d):gmatch("(.-)"..d) do
    >    table.insert(result, match);
    >  end
    >  return result
    >end
- __teliva_timestamp: original
  sort_letters:
    >function sort_letters(s)
    >  tmp = {}
    >  for i=1,#s do
    >    table.insert(tmp, s[i])
    >  end
    >  table.sort(tmp)
    >  local result = ''
    >  for _, c in pairs(tmp) do
    >    result = result..c
    >  end
    >  return result
    >end
    >
    >function test_sort_letters(s)
    >  check_eq(sort_letters(''), '', 'test_sort_letters: empty')
    >  check_eq(sort_letters('ba'), 'ab', 'test_sort_letters: non-empty')
    >  check_eq(sort_letters('abba'), 'aabb', 'test_sort_letters: duplicates')
    >end
- __teliva_timestamp: original
  count_letters:
    >function count_letters(s)
    >  local result = {}
    >  for i=1,string.len(s) do
    >    local c = s[i]
    >    if result[c] == nil then
    >      result[c] = 1
    >    else
    >      result[c] = result[c] + 1
    >    end
    >  end
    >  return result
    >end
- __teliva_timestamp: original
  append:
    >-- concatenate list 'elems' into 'l', modifying 'l' in the process
    >function append(l, elems)
    >  for i=1,#elems do
    >    l[#l+1] = elems[i]
    >  end
    >end
- __teliva_timestamp: original
  menu:
    >-- To show app-specific hotkeys in the menu bar, add hotkey/command
    >-- arrays of strings to the menu array.
    >menu = {
    >  {'^e', 'edit'},
    >}
- __teliva_timestamp: original
  Window:
    >Window = curses.stdscr()
- __teliva_timestamp: original
  window:
    >-- constructor for fake screen and window
    >-- call it like this:
    >--   local w = window{
    >--     kbd=kbd('abc'),
    >--     scr=scr{h=5, w=4},
    >--   }
    >-- eventually it'll do everything a real ncurses window can
    >function window(h)
    >  h.__index = h
    >  setmetatable(h, h)
    >  h.__index = function(table, key)
    >    return rawget(h, key)
    >  end
    >  h.getch = function(self)
    >    return table.remove(h.kbd, 1)
    >  end
    >  h.addch = function(self, c)
    >    local scr = self.scr
    >    if scr.cursy <= scr.h then
    >      scr[scr.cursy][scr.cursx] = c
    >      scr.cursx = scr.cursx+1
    >      if scr.cursx > scr.w then
    >        scr.cursy = scr.cursy+1
    >        scr.cursx = 1
    >      end
    >    end
    >  end
    >  h.addstr = function(self, s)
    >    for i=1,string.len(s) do
    >      self:addch(s[i])
    >    end
    >  end
    >  h.mvaddch = function(self, y, x, c)
    >    self.scr.cursy = y
    >    self.scr.cursx = x
    >    self.addch(c)
    >  end
    >  h.mvaddstr = function(self, y, x, s)
    >    self.scr.cursy = y
    >    self.scr.cursx = x
    >    self:addstr(s)
    >  end
    >  return h
    >end
- __teliva_timestamp: original
  kbd:
    >function kbd(keys)
    >  local result = {}
    >  for i=1,string.len(keys) do
    >    table.insert(result, keys[i])
    >  end
    >  return result
    >end
- __teliva_timestamp: original
  scr:
    >function scr(props)
    >  props.cursx = 1
    >  props.cursy = 1
    >  for y=1,props.h do
    >    props[y] = {}
    >    for x=1,props.w do
    >      props[y][x] = ' '
    >    end
    >  end
    >  return props
    >end
- __teliva_timestamp: original
  check_screen:
    >function check_screen(window, contents, message)
    >  local x, y = 1, 1
    >  for i=1,string.len(contents) do
    >    check_eq(contents[i], window.scr[y][x], message..'/'..y..','..x)
    >    x = x+1
    >    if x > window.scr.w then
    >      y = y+1
    >      x = 1
    >    end
    >  end
    >end
    >
    >-- putting it all together, an example test of both keyboard and screen
    >function test_check_screen()
    >  local lines = {
    >    c='123',
    >    d='234',
    >    a='345',
    >    b='456',
    >  }
    >  local w = window{
    >    kbd=kbd('abc'),
    >    scr=scr{h=3, w=5},
    >  }
    >  local y = 1
    >  while true do
    >    local c = w:getch()
    >    if c == nil then break end
    >    w:mvaddstr(y, 1, lines[c])
    >    y = y+1
    >  end
    >  check_screen(w, '345  '..
    >                  '456  '..
    >                  '123  ',
    >              'test_check_screen')
    >end
- __teliva_timestamp: original
  spaces:
    >function spaces(n)
    >  for i=1,n do
    >    Window:addch(' ')
    >  end
    >end
- __teliva_timestamp: original
  init_colors:
    >function init_colors()
    >  -- light background
    >  curses.init_pair(view_settings.current_zettel_bg, 236, 230)
    >  curses.init_pair(1, 236, 250)
    >  curses.init_pair(2, 236, 252)
    >  -- dark background
    >--?   curses.init_pair(view_settings.current_zettel_bg, 252, 130)
    >--?   curses.init_pair(1, 252, 240)
    >--?   curses.init_pair(2, 252, 242)
    >end
- __teliva_timestamp: original
  main:
    >function main()
    >  init_colors()
    >  current_zettel_id = zettels.root
    >
    >  while true do
    >    render(Window)
    >    update(Window)
    >  end
    >end
- __teliva_timestamp: original
  depth:
    >function depth(zettel)
    >  local result = 0
    >  while zettel.parent do
    >    result = result+1
    >    zettel = zettel.parent
    >  end
    >  return result
    >end
- __teliva_timestamp: original
  render_zettel:
    >function render_zettel(window, bg, indent, starty, startx, zettel)
    >  window:attrset(curses.color_pair(bg))
    >  for y=0,view_settings.height-1 do
    >    for x=0,view_settings.width-1 do
    >      window:mvaddch(y+starty, x+startx, ' ')
    >    end
    >  end
    >  local y, x = 0, indent+1
    >  for i=1,#zettel.data do
    >    local c = zettel.data[i]
    >    if c == '\n' then
    >      y = y+1
    >      x = indent+1
    >    else
    >      window:mvaddstr(y+starty, x+startx, c)
    >      x = x+1
    >      if x >= startx + view_settings.width then
    >        y = y+1
    >        x = indent+1
    >      end
    >    end
    >    if y >= view_settings.height then
    >      break
    >    end
    >  end
    >end
- __teliva_timestamp: original
  current_zettel_id:
    >current_zettel_id = ''
- __teliva_timestamp: original
  view_settings:
    >view_settings = {
    >  -- dimensions for rendering a single zettel; extra text gets truncated
    >  width=50,
    >  height=3,
    >  -- spacing between zettels
    >  hmargin=1,
    >  vmargin=1,
    >  --
    >  indent=2,  -- how children of a zettel are indicated
    >  current_zettel_bg=3,  -- color pair index initialized in init_colors
    >}
- __teliva_timestamp: original
  zettels:
    >zettels = {
    >  root="a",
    >  a={
    >    data="abc\ndef",
    >    child="c",
    >    next="b",
    >  },
    >  b={
    >    data="ghi\njklm",
    >    prev="a",
    >  },
    >  c={
    >    data="c",
    >    parent="a",
    >    next="d",
    >  },
    >  d={
    >    data="d",
    >    parent="a",
    >    prev="c",
    >  }
    >}
- __teliva_timestamp: original
  render_state:
    >-- some information about what's been drawn on screen
    >render_state = {
    >  -- where the current zettel is, in units of zettels
    >  curr_h = 1,
    >  curr_w = 1,
    >  -- what zettel is at each position on screen, in units of zettels
    >  hw2id = {},
    >}
- __teliva_timestamp: original
  update:
    >function update(window)
    >  local key = window:getch()
    >  local curr = zettels[current_zettel_id]
    >  -- graph-based navigation
    >  if key == string.byte('j') then
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  elseif key == string.byte('k') then
    >    if curr.parent then current_zettel_id = curr.parent end
    >  elseif key == string.byte('h') then
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >  elseif key == string.byte('l') then
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  -- screen-based navigation
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  --
    >  elseif key == 5 then  -- ctrl-e
    >    editz(window)
    >  end
    >end
- __teliva_timestamp: original
  render:
    >function render(window)
    >  window:clear()
    >  local lines, cols = window:getmaxyx()
    >  local bg=1
    >  local y, x = 0, 0 -- units of characters (0-based)
    >  local w, h = 1, 1 -- units of zettels (1-based)
    >  -- render zettels depth-first, while tracking relative positions
    >  local done = {}
    >  local inprogress = {zettels.root}
    >  render_state.wh2id = {{}}
    >  while #inprogress > 0 do
    >    local currid = table.remove(inprogress)
    >    if not done[currid] then
    >      done[currid] = true
    >      table.insert(render_state.wh2id[w], currid)
    >      local zettel = zettels[currid]
    >      if currid == current_zettel_id then
    >        render_state.curr_w = w
    >        render_state.curr_h = h
    >      end
    >      local currbg = (currid == current_zettel_id) and view_settings.current_zettel_bg or bg
    >      render_zettel(window, currbg, depth(zettel) * view_settings.indent, y, x, zettel)
    >      if zettel.next then table.insert(inprogress, zettel.next) end
    >      if zettel.child then table.insert(inprogress, zettel.child) end
    >      bg = 3 - bg  -- toggle between color pairs 1 and 2
    >      y = y + view_settings.height + view_settings.vmargin
    >      h = h + 1
    >      if y + view_settings.height > lines then
    >        y = 0
    >        h = 1
    >        x = x + view_settings.width + view_settings.hmargin
    >        w = w + 1
    >        if x + view_settings.width > cols then break end
    >        table.insert(render_state.wh2id, {})
    >      end
    >    end
    >  end
    >  window:mvaddstr(lines-2, 0, '')
    >  for i=1,3 do
    >    window:attrset(curses.color_pair(i%2+1))
    >    window:addstr('')
    >    spaces(view_settings.width-string.len(''))
    >    window:attrset(curses.color_pair(0))
    >    window:addstr(' ')  -- margin
    >  end
    >  window:mvaddstr(lines-1, 0, '? ')
    >  window:refresh()
    >end
- __teliva_timestamp: original
  view_settings:
    >view_settings = {
    >  -- dimensions for rendering a single zettel; extra text gets truncated
    >  width=50,
    >  height=3,
    >  -- spacing between zettels
    >  hmargin=1,
    >  vmargin=1,
    >  --
    >  indent=2,  -- how children of a zettel are indicated
    >  current_zettel_bg=3,  -- color pair index initialized in init_colors
    >}
- __teliva_timestamp: original
  editz:
    >function editz(window)
    >  menu = { {'^e', 'back to browsing'},}
    >  local top = (render_state.curr_h - 1) * (view_settings.height + view_settings.vmargin)
    >  local bottom = top + view_settings.height
    >  local left = (render_state.curr_w - 1) * (view_settings.width + view_settings.hmargin)
    >  local right = left + view_settings.width
    >  local cursor = 1
    >  curses.curs_set(0)
    >  local quit = false
    >  while not quit do
    >    editz_render(window, zettels[current_zettel_id].data, cursor, top, bottom, left, right)
    >    quit, zettels[current_zettel_id].data, cursor = editz_update(window, zettels[current_zettel_id].data, cursor)
    >  end
    >  curses.curs_set(1)
    >end
- __teliva_timestamp: original
  editz_render:
    >function editz_render(window, s, cursor, top, minbottom, left, right)
    >  local h, w = window:getmaxyx()
    >  window:attrset(curses.color_pair(view_settings.current_zettel_bg))
    >  for y=top,minbottom-1 do
    >    for x=left,right-1 do
    >      window:mvaddch(y, x, ' ')
    >    end
    >  end
    >  local y, x = top, left + 1  -- left padding; TODO: indent
    >  window:mvaddstr(y, x, '')
    >  for i=1,string.len(s) do
    >    -- render character
    >    if i == cursor then
    >      if s[i] == '\n' then
    >        -- newline at cursor = render extra space in reverse video before jumping to new line
    >        window:attron(curses.A_REVERSE)
    >        window:addch(' ')
    >        window:attroff(curses.A_REVERSE)
    >      else
    >        -- most characters at cursor = render in reverse video
    >        window:attron(curses.A_REVERSE)
    >        window:addstr(s[i])
    >        window:attroff(curses.A_REVERSE)
    >      end
    >    else
    >      if s[i] ~= '\n' then
    >        window:addstr(s[i])
    >      end
    >    end
    >    -- update cursor position
    >    if s[i] == '\n' then
    >      if i == cursor then x = x + 1; end
    >      for col=x,right-1 do window:addch(' '); end
    >      x = left
    >      y = y + 1
    >      if y >= h-2 then return end
    >      window:mvaddstr(y, x, '')
    >      for col=x,right-1 do window:addch(' '); end
    >      x = left + 1  -- left padding; TODO: indent
    >      window:mvaddstr(y, x, '')
    >    else
    >      x = x + 1
    >      if x >= right then
    >        y = y + 1
    >        if y >= h-2 then return end
    >        x = left + 1  -- left padding; TODO: indent
    >        window:mvaddstr(y, x, '')
    >      end
    >    end
    >  end
    >  if cursor > string.len(s) then
    >    window:attron(curses.A_REVERSE)
    >    window:addch(' ')
    >    window:attroff(curses.A_REVERSE)
    >  else
    >    window:addch(' ')
    >  end
    >end
- __teliva_timestamp: original
  editz_update:
    >function editz_update(window, prose, cursor)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  if key == curses.KEY_LEFT then
    >    if cursor > 1 then
    >      cursor = cursor-1
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if cursor <= #prose then
    >      cursor = cursor+1
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    cursor = cursor_down(prose, cursor, w)
    >  elseif key == curses.KEY_UP then
    >    cursor = cursor_up(prose, cursor, w)
    >  elseif key == curses.KEY_BACKSPACE or key == 8 or key == 127 then  -- ctrl-h, ctrl-?, delete
    >    if cursor > 1 then
    >      cursor = cursor-1
    >      prose = prose:remove(cursor)
    >    end
    >  elseif key == 5 then  -- ctrl-e
    >    return true, prose, cursor
    >  elseif key == 10 or (key >= 32 and key < 127) then
    >    prose = prose:insert(string.char(key), cursor-1)
    >    cursor = cursor+1
    >  end
    >  return false, prose, cursor
    >end
- __teliva_timestamp: original
  cursor_down:
    >function cursor_down(s, old_idx, width)
    >  local max = string.len(s)
    >  local i = 1
    >  -- compute oldcol, the screen column of old_idx
    >  local oldcol = 0
    >  local col = 0
    >  while true do
    >    if i > max then
    >      -- abnormal old_idx
    >      return old_idx
    >    end
    >    if i == old_idx then
    >      oldcol = col
    >      break
    >    end
    >    if s[i] == '\n' then
    >      col = 0
    >    else
    >      col = col+1
    >    end
    >    i = i+1
    >  end
    >  -- skip rest of line
    >  while true do
    >    if i > max then
    >      -- current line is at bottom
    >      if col >= width then
    >        return i
    >      end
    >      return old_idx
    >    end
    >    if s[i] == '\n' then
    >      break
    >    end
    >    if i - old_idx >= width then
    >      return i
    >    end
    >    col = col+1
    >    i = i+1
    >  end
    >  -- compute index at same column on next line
    >  -- i is at a newline
    >  i = i+1
    >  col = 0
    >  while true do
    >    if i > max then
    >      -- next line is at bottom and is too short; position at end of it
    >      return i
    >    end
    >    if s[i] == '\n' then
    >      -- next line is too short; position at end of it
    >      return i
    >    end
    >    if col == oldcol then
    >      return i
    >    end
    >    col = col+1
    >    i = i+1
    >  end
    >end
    >
    >function test_cursor_down()
    >  -- lines that don't wrap
    >  check_eq(cursor_down('abc\ndef', 1, 5), 5, 'cursor_down: non-bottom line first char')
    >  check_eq(cursor_down('abc\ndef', 2, 5), 6, 'cursor_down: non-bottom line mid char')
    >  check_eq(cursor_down('abc\ndef', 3, 5), 7, 'cursor_down: non-bottom line final char')
    >  check_eq(cursor_down('abc\ndef', 4, 5), 8, 'cursor_down: non-bottom line end')
    >  check_eq(cursor_down('abc\ndef', 5, 5), 5, 'cursor_down: bottom line first char')
    >  check_eq(cursor_down('abc\ndef', 6, 5), 6, 'cursor_down: bottom line mid char')
    >  check_eq(cursor_down('abc\ndef', 7, 5), 7, 'cursor_down: bottom line final char')
    >  check_eq(cursor_down('abc\n\ndef', 2, 5), 5, 'cursor_down: to shorter line')
    >
    >  -- within a single wrapping line
    >  --   |abcde|  <-- wrap, no newline
    >  --   |fgh  |
    >  check_eq(cursor_down('abcdefgh', 1, 5), 6, 'cursor_down from wrapping line: first char')
    >  check_eq(cursor_down('abcdefgh', 2, 5), 7, 'cursor_down from wrapping line: mid char')
    >  check_eq(cursor_down('abcdefgh', 5, 5), 9, 'cursor_down from wrapping line: to shorter line')
    >
    >  -- within a single very long wrapping line
    >  --   |abcde|  <-- wrap, no newline
    >  --   |fghij|  <-- wrap, no newline
    >  --   |klm  |
    >  check_eq(cursor_down('abcdefghijklm', 1, 5), 6, 'cursor_down within wrapping line: first char')
    >  check_eq(cursor_down('abcdefghijklm', 2, 5), 7, 'cursor_down within wrapping line: mid char')
    >  check_eq(cursor_down('abcdefghijklm', 5, 5), 10, 'cursor_down within wrapping line: final char')
    >end
- __teliva_timestamp: original
  __teliva_note:
    >initial commit: show/edit zettels
  cursor_up:
    >function cursor_up(s, old_idx, width)
    >  local max = string.len(s)
    >  local i = 1
    >  -- compute oldcol, the screen column of old_idx
    >  local oldcol = 0
    >  local col = 0
    >  local newline_before_current_line = 0
    >  while true do
    >    if i > max or i == old_idx then
    >      oldcol = col
    >      break
    >    end
    >    if s[i] == '\n' then
    >      col = 0
    >      newline_before_current_line = i
    >    else
    >      col = col+1
    >      if col == width then
    >        col = 0
    >      end
    >    end
    >    i = i+1
    >  end
    >  -- find previous newline
    >  i = i-col-1
    >  if old_idx - newline_before_current_line > width then
    >    -- we're in a wrapped line
    >    return old_idx - width
    >  end
    >  -- scan back to start of previous line
    >  if s[i] == '\n' then
    >    i = i-1
    >  end
    >  while true do
    >    if i < 1 then
    >      -- current line is at top
    >      break
    >    end
    >    if s[i] == '\n' then
    >      break
    >    end
    >    i = i-1
    >  end
    >  -- i is at a newline
    >  i = i+1
    >  -- skip whole screen lines within previous line
    >  while newline_before_current_line - i > width do
    >    i = i + width
    >  end
    >  -- compute index at same column on previous screen line
    >  col = 0
    >  while true do
    >    if i > max then
    >      -- next line is at bottom and is too short; position at end of it
    >      return i
    >    end
    >    if s[i] == '\n' then
    >      -- next line is too short; position at end of it
    >      return i
    >    end
    >    if col == oldcol then
    >      return i
    >    end
    >    col = col+1
    >    i = i+1
    >  end
    >end
    >
    >function test_cursor_up()
    >  -- lines that don't wrap
    >  check_eq(cursor_up('abc\ndef', 1, 5), 1, 'cursor_up: top line first char')
    >  check_eq(cursor_up('abc\ndef', 2, 5), 2, 'cursor_up: top line mid char')
    >  check_eq(cursor_up('abc\ndef', 3, 5), 3, 'cursor_up: top line final char')
    >  check_eq(cursor_up('abc\ndef', 4, 5), 4, 'cursor_up: top line end')
    >  check_eq(cursor_up('abc\ndef', 5, 5), 1, 'cursor_up: non-top line first char')
    >  check_eq(cursor_up('abc\ndef', 6, 5), 2, 'cursor_up: non-top line mid char')
    >  check_eq(cursor_up('abc\ndef', 7, 5), 3, 'cursor_up: non-top line final char')
    >  check_eq(cursor_up('abc\ndef\n', 8, 5), 4, 'cursor_up: non-top line end')
    >  check_eq(cursor_up('ab\ndef\n', 7, 5), 3, 'cursor_up: to shorter line')
    >
    >  -- within a single wrapping line
    >  --   |abcde|  <-- wrap, no newline
    >  --   |fgh  |
    >  check_eq(cursor_up('abcdefgh', 6, 5), 1, 'cursor_up from wrapping line: first char')
    >  check_eq(cursor_up('abcdefgh', 7, 5), 2, 'cursor_up from wrapping line: mid char')
    >  check_eq(cursor_up('abcdefgh', 8, 5), 3, 'cursor_up from wrapping line: final char')
    >  check_eq(cursor_up('abcdefgh', 9, 5), 4, 'cursor_up from wrapping line: wrapped line end')
    >
    >  -- within a single very long wrapping line
    >  --   |abcde|  <-- wrap, no newline
    >  --   |fghij|  <-- wrap, no newline
    >  --   |klm  |
    >  check_eq(cursor_up('abcdefghijklm', 11, 5), 6, 'cursor_up within wrapping line: first char')
    >  check_eq(cursor_up('abcdefghijklm', 12, 5), 7, 'cursor_up within wrapping line: mid char')
    >  check_eq(cursor_up('abcdefghijklm', 13, 5), 8, 'cursor_up within wrapping line: final char')
    >  check_eq(cursor_up('abcdefghijklm', 14, 5), 9, 'cursor_up within wrapping line: wrapped line end')
    >
    >  -- from below to (the bottom of) a wrapping line
    >  --   |abcde|  <-- wrap, no newline
    >  --   |fg   |
    >  --   |hij  |
    >  check_eq(cursor_up('abcdefg\nhij', 9, 5), 6, 'cursor_up to wrapping line: first char')
    >  check_eq(cursor_up('abcdefg\nhij', 10, 5), 7, 'cursor_up to wrapping line: mid char')
    >  check_eq(cursor_up('abcdefg\nhij', 11, 5), 8, 'cursor_up to wrapping line: final char')
    >  check_eq(cursor_up('abcdefg\nhij', 12, 5), 8, 'cursor_up to wrapping line: to shorter line')
    >end
- __teliva_timestamp:
    >Wed Feb  9 08:15:25 2022
  render:
    >function render(window)
    >  window:clear()
    >  local lines, cols = window:getmaxyx()
    >  local bg=1
    >  local y, x = 0, 0 -- units of characters (0-based)
    >  local w, h = 1, 1 -- units of zettels (1-based)
    >  -- render zettels depth-first, while tracking relative positions
    >  local done = {}
    >  local inprogress = {zettels.root}
    >  render_state.wh2id = {{}}
    >  while #inprogress > 0 do
    >    local currid = table.remove(inprogress)
    >    if not done[currid] then
    >      done[currid] = true
    >      table.insert(render_state.wh2id[w], currid)
    >      local zettel = zettels[currid]
    >      if currid == current_zettel_id then
    >        render_state.curr_w = w
    >        render_state.curr_h = h
    >      end
    >      local currbg = (currid == current_zettel_id) and view_settings.current_zettel_bg or bg
    >      render_zettel(window, currbg, depth(zettel) * view_settings.indent, y, x, zettel)
    >      if zettel.next then table.insert(inprogress, zettel.next) end
    >      if zettel.child then table.insert(inprogress, zettel.child) end
    >      bg = 3 - bg  -- toggle between color pairs 1 and 2
    >      y = y + view_settings.height + view_settings.vmargin
    >      h = h + 1
    >      if y + view_settings.height > lines then
    >        y = 0
    >        h = 1
    >        x = x + view_settings.width + view_settings.hmargin
    >        w = w + 1
    >        if x + view_settings.width > cols then break end
    >        table.insert(render_state.wh2id, {})
    >      end
    >    end
    >  end
    >  window:mvaddstr(lines-1, 0, '')
    >  for i=1,3 do
    >    window:attrset(curses.color_pair(i%2+1))
    >    window:addstr('')
    >    spaces(view_settings.width-string.len(''))
    >    window:attrset(curses.color_pair(0))
    >    window:addstr(' ')  -- margin
    >  end
    >  window:refresh()
    >end
- __teliva_timestamp:
    >Wed Feb  9 08:15:35 2022
  main:
    >function main()
    >  init_colors()
    >  current_zettel_id = zettels.root
    >
    >  curses.curs_set(0)
    >  while true do
    >    render(Window)
    >    update(Window)
    >  end
    >end
- __teliva_timestamp:
    >Wed Feb  9 08:16:24 2022
  __teliva_note:
    >get rid of commandline
    >
    >There's a reason vim hides it. Confusing to have two cursors on screen.
  editz:
    >function editz(window)
    >  menu = { {'^e', 'back to browsing'},}
    >  local top = (render_state.curr_h - 1) * (view_settings.height + view_settings.vmargin)
    >  local bottom = top + view_settings.height
    >  local left = (render_state.curr_w - 1) * (view_settings.width + view_settings.hmargin)
    >  local right = left + view_settings.width
    >  local cursor = 1
    >  local quit = false
    >  while not quit do
    >    editz_render(window, zettels[current_zettel_id].data, cursor, top, bottom, left, right)
    >    quit, zettels[current_zettel_id].data, cursor = editz_update(window, zettels[current_zettel_id].data, cursor)
    >  end
    >end
- __teliva_timestamp:
    >Wed Feb  9 08:22:20 2022
  editz_render:
    >function editz_render(window, s, cursor, top, minbottom, left, right)
    >  local h, w = window:getmaxyx()
    >  local cursor_y, cursor_x = 0, 0
    >  window:attrset(curses.color_pair(view_settings.current_zettel_bg))
    >  for y=top,minbottom-1 do
    >    for x=left,right-1 do
    >      window:mvaddch(y, x, ' ')
    >    end
    >  end
    >  local y, x = top, left + 1  -- left padding; TODO: indent
    >  window:mvaddstr(y, x, '')
    >  for i=1,string.len(s) do
    >    if i == cursor then
    >      cursor_y = y
    >      cursor_x = x
    >    end
    >    if s[i] ~= '\n' then
    >      window:addstr(s[i])
    >      x = x + 1
    >      if x >= right then
    >        y = y + 1
    >        if y >= h-2 then return end
    >        x = left + 1  -- left padding; TODO: indent
    >        window:mvaddstr(y, x, '')
    >      end
    >    else
    >      for col=x+1,right-1 do window:addch(' '); end
    >      x = left
    >      y = y + 1
    >      if y >= h-2 then return end
    >      window:mvaddstr(y, x, '')
    >      for col=x,right-1 do window:addch(' '); end
    >      x = left + 1  -- left padding; TODO: indent
    >      window:mvaddstr(y, x, '')
    >    end
    >  end
    >  if cursor_y == 0 and cursor_x == 0 then
    >    cursor_y = y
    >    cursor_x = x
    >  end
    >  window:mvaddstr(cursor_y, cursor_x, '')
    >end
- __teliva_timestamp:
    >Wed Feb  9 08:25:05 2022
  editz:
    >function editz(window)
    >  local old_menu = menu
    >  menu = { {'^e', 'back to browsing'},}
    >  local top = (render_state.curr_h - 1) * (view_settings.height + view_settings.vmargin)
    >  local bottom = top + view_settings.height
    >  local left = (render_state.curr_w - 1) * (view_settings.width + view_settings.hmargin)
    >  local right = left + view_settings.width
    >  local cursor = string.len(zettels[current_zettel_id].data)+1
    >  local quit = false
    >  curses.curs_set(1)
    >  while not quit do
    >    editz_render(window, zettels[current_zettel_id].data, cursor, top, bottom, left, right)
    >    quit, zettels[current_zettel_id].data, cursor = editz_update(window, zettels[current_zettel_id].data, cursor)
    >  end
    >  curses.curs_set(0)
    >  menu = old_menu
    >end
- __teliva_timestamp:
    >Wed Feb  9 08:28:13 2022
  __teliva_note:
    >stop simulating the cursor
    >
    >editz_render is now much simpler
  editz_update:
    >function editz_update(window, prose, cursor)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  if key == curses.KEY_LEFT then
    >    if cursor > 1 then
    >      cursor = cursor-1
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if cursor <= #prose then
    >      cursor = cursor+1
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    cursor = cursor_down(prose, cursor, w)
    >  elseif key == curses.KEY_UP then
    >    cursor = cursor_up(prose, cursor, w)
    >  elseif key == curses.KEY_BACKSPACE or key == 8 or key == 127 then  -- ctrl-h, ctrl-?, delete
    >    if cursor > 1 then
    >      cursor = cursor-1
    >      prose = prose:remove(cursor)
    >    end
    >  elseif key == 5 then  -- ctrl-e
    >    return true, prose, cursor
    >  elseif key == 10 or (key >= 32 and key < 127) then
    >    prose = prose:insert(string.char(key), cursor-1)
    >    cursor = cursor+1
    >  end
    >  return false, prose, cursor
    >end
- __teliva_timestamp:
    >Wed Feb  9 17:55:52 2022
  menu:
    >-- To show app-specific hotkeys in the menu bar, add hotkey/command
    >-- arrays of strings to the menu array.
    >menu = {
    >  {'j', 'child'},
    >  {'k', 'parent'},
    >  {'l,h', 'next/prev sib'},
    >  {'e', 'edit'},
    >}
- __teliva_timestamp:
    >Wed Feb  9 17:56:18 2022
  __teliva_note:
    >no need for chords once we drop the commandline
  update:
    >function update(window)
    >  local key = window:getch()
    >  local curr = zettels[current_zettel_id]
    >  -- graph-based navigation
    >  if key == string.byte('j') then
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  elseif key == string.byte('k') then
    >    if curr.parent then current_zettel_id = curr.parent end
    >  elseif key == string.byte('h') then
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >  elseif key == string.byte('l') then
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  -- screen-based navigation
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  --
    >  elseif key == string.byte('e') then
    >    local old_menu = menu
    >    editz(window)
    >    menu = old_menu
    >  end
    >end
- __teliva_timestamp:
    >Wed Feb  9 18:00:42 2022
  menu:
    >-- To show app-specific hotkeys in the menu bar, add hotkey/command
    >-- arrays of strings to the menu array.
    >menu = {
    >  {'j', 'child'},
    >  {'k', 'parent'},
    >  {'l,h', 'next/prev sib'},
    >  {'e', 'edit'},
    >  {'a,b', 'insert sib'},
    >  {'c', 'insert child'},
    >}
- __teliva_timestamp:
    >Wed Feb  9 18:16:23 2022
  zettels:
    >zettels = {
    >  root="id1",
    >  final=4,
    >  id1={
    >    data="this is zettel A\n\nit has some text",
    >    child="id3",
    >    next="id2",
    >  },
    >  id2={
    >    data="this is a sibling of zettel A at the top level",
    >    prev="id1",
    >  },
    >  id3={
    >    data="this is zettel B, a child of A",
    >    parent="id1",
    >    next="id4",
    >  },
    >  id4={
    >    data="this is another child of zettel A, a sibling of B",
    >    parent="id1",
    >    prev="id3",
    >  }
    >}
- __teliva_timestamp:
    >Wed Feb  9 23:04:49 2022
  new_id:
    >function new_id()
    >  zettels.final = zettels.final+1
    >  local result = 'id'..tostring(zettels.final)
    >  zettels[result] = {}
    >  return result
    >end
- __teliva_timestamp:
    >Wed Feb  9 23:10:57 2022
  __teliva_note:
    >creating new zettels
    >
    >feels natural to immediately start editing them
  update:
    >function update(window)
    >  local key = window:getch()
    >  local curr = zettels[current_zettel_id]
    >  assert(curr, string.format('cursor fell off the edge of the world: %s', type(current_zettel_id)))
    >  -- graph-based navigation
    >  if key == string.byte('j') then
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  elseif key == string.byte('k') then
    >    if curr.parent then current_zettel_id = curr.parent end
    >  elseif key == string.byte('h') then
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >  elseif key == string.byte('l') then
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  -- screen-based navigation
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  --
    >  elseif key == string.byte('e') then
    >    editz(window)
    >  elseif key == string.byte('a') then
    >    -- insert sibling after
    >    local old = curr.next
    >    curr.next = new_id()
    >    local new = zettels[curr.next]
    >    new.data = ''
    >    new.next = old
    >    zettels[old].prev = curr.next
    >    new.prev = current_zettel_id
    >    assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    new.parent = curr.parent
    >    current_zettel_id = curr.next
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('b') then
    >    -- insert sibling before
    >    local old = curr.prev
    >    curr.prev = new_id()
    >    local new = zettels[curr.prev]
    >    new.data = ''
    >    new.prev = old
    >    zettels[old].next = curr.prev
    >    new.next = current_zettel_id
    >    assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    new.parent = curr.parent
    >    current_zettel_id = curr.prev
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('c') then
    >    -- insert child
    >    local old = curr.child
    >    curr.child = new_id()
    >    local new = zettels[curr.child]
    >    new.data = ''
    >    new.next = old
    >    assert(zettels[old].prev == nil, "first child shouldn't have a previous sibling")
    >    zettels[old].prev = curr.child
    >    new.parent = curr
    >    current_zettel_id = curr.child
    >    render(window) -- recompute render_state
    >    editz(window)
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 00:01:58 2022
  update:
    >function update(window)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  local curr = zettels[current_zettel_id]
    >  assert(curr, string.format('cursor fell off the edge of the world: %s', type(current_zettel_id)))
    >  -- graph-based navigation
    >  if key == string.byte('j') then
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  elseif key == string.byte('k') then
    >    if curr.parent then current_zettel_id = curr.parent end
    >  elseif key == string.byte('h') then
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >  elseif key == string.byte('l') then
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  -- screen-based navigation
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  --
    >  elseif key == string.byte('e') then
    >    editz(window)
    >  elseif key == string.byte('a') then
    >    -- insert sibling after
    >    local old = curr.next
    >    curr.next = new_id()
    >    local new = zettels[curr.next]
    >    new.data = ''
    >    new.next = old
    >    zettels[old].prev = curr.next
    >    new.prev = current_zettel_id
    >    assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    new.parent = curr.parent
    >    current_zettel_id = curr.next
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('b') then
    >    -- insert sibling before
    >    local old = curr.prev
    >    curr.prev = new_id()
    >    local new = zettels[curr.prev]
    >    new.data = ''
    >    new.prev = old
    >    zettels[old].next = curr.prev
    >    new.next = current_zettel_id
    >    assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    new.parent = curr.parent
    >    current_zettel_id = curr.prev
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('c') then
    >    -- insert child
    >    local old = curr.child
    >    curr.child = new_id()
    >    local new = zettels[curr.child]
    >    new.data = ''
    >    new.next = old
    >    assert(zettels[old].prev == nil, "first child shouldn't have a previous sibling")
    >    zettels[old].prev = curr.child
    >    new.parent = curr
    >    current_zettel_id = curr.child
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('x') then
    >    if view_settings.width > 5 then
    >      view_settings.width = view_settings.width - 5
    >    end
    >  elseif key == string.byte('X') then
    >    if view_settings.width < w-5 then
    >      view_settings.width = view_settings.width + 5
    >    end
    >  elseif key == string.byte('y') then
    >    if view_settings.height > 0 then
    >      view_settings.height = view_settings.height - 1
    >    end
    >  elseif key == string.byte('Y') then
    >    if view_settings.height < h-2 then
    >      view_settings.height = view_settings.height + 1
    >    end
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 00:02:35 2022
  menu:
    >-- To show app-specific hotkeys in the menu bar, add hotkey/command
    >-- arrays of strings to the menu array.
    >menu = {
    >  {'j', 'child'},
    >  {'k', 'parent'},
    >  {'l,h', 'next/prev sib'},
    >  {'e', 'edit'},
    >  {'a,b', 'insert sib'},
    >  {'c', 'insert child'},
    >  {'x,X,y,Y', 'resize'},
    >}
- __teliva_timestamp:
    >Thu Feb 10 06:57:51 2022
  __teliva_note:
    >squeeze menu to make way for next feature
  menu:
    >-- To show app-specific hotkeys in the menu bar, add hotkey/command
    >-- arrays of strings to the menu array.
    >menu = {
    >  {'a,b,c', 'insert sib/child'},
    >  {'e', 'edit'},
    >  {'j,k,l,h', 'move to child/parent/sib'},
    >  {'x,X,y,Y', 'resize'},
    >}
- __teliva_timestamp:
    >Thu Feb 10 07:00:46 2022
  __teliva_note:
    >bugfix: handle missing parent/child/sib
  update:
    >function update(window)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  local curr = zettels[current_zettel_id]
    >  assert(curr, string.format('cursor fell off the edge of the world: %s', type(current_zettel_id)))
    >  -- graph-based navigation
    >  if key == string.byte('j') then
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  elseif key == string.byte('k') then
    >    if curr.parent then current_zettel_id = curr.parent end
    >  elseif key == string.byte('h') then
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >  elseif key == string.byte('l') then
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  -- screen-based navigation
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  --
    >  elseif key == string.byte('e') then
    >    editz(window)
    >  elseif key == string.byte('a') then
    >    -- insert sibling after
    >    local old = curr.next
    >    curr.next = new_id()
    >    local new = zettels[curr.next]
    >    new.data = ''
    >    new.next = old
    >    new.prev = current_zettel_id
    >    if old then
    >      zettels[old].prev = curr.next
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.next
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('b') then
    >    -- insert sibling before
    >    local old = curr.prev
    >    curr.prev = new_id()
    >    local new = zettels[curr.prev]
    >    new.data = ''
    >    new.prev = old
    >    new.next = current_zettel_id
    >    if old then
    >      zettels[old].next = curr.prev
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.prev
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('c') then
    >    -- insert child
    >    local old = curr.child
    >    curr.child = new_id()
    >    local new = zettels[curr.child]
    >    new.data = ''
    >    new.next = old
    >    if old then
    >      assert(zettels[old].prev == nil, "first child shouldn't have a previous sibling")
    >      zettels[old].prev = curr.child
    >    end
    >    new.parent = curr
    >    current_zettel_id = curr.child
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('x') then
    >    if view_settings.width > 5 then
    >      view_settings.width = view_settings.width - 5
    >    end
    >  elseif key == string.byte('X') then
    >    if view_settings.width < w-5 then
    >      view_settings.width = view_settings.width + 5
    >    end
    >  elseif key == string.byte('y') then
    >    if view_settings.height > 0 then
    >      view_settings.height = view_settings.height - 1
    >    end
    >  elseif key == string.byte('Y') then
    >    if view_settings.height < h-2 then
    >      view_settings.height = view_settings.height + 1
    >    end
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 07:27:43 2022
  write_zettels:
    >function write_zettels(outfile)
    >  outfile:write(json.encode(zettels))
    >  outfile:close()
    >end
- __teliva_timestamp:
    >Thu Feb 10 07:28:30 2022
  read_zettels:
    >function read_zettels(infile)
    >  zettels = json.decode(infile:read('*a'))
    >  infile:close()
    >end
- __teliva_timestamp:
    >Thu Feb 10 07:30:25 2022
  __teliva_note:
    >saving/loading zettels to/from disk
  main:
    >function main()
    >  init_colors()
    >  curses.curs_set(0)  -- hide cursor except when editing
    >
    >  -- read zettels from disk if possible
    >  local infile = io.open('zet', 'r')
    >  if infile then
    >    read_zettels(infile)
    >  else
    >    local outfile = io.open('zet', 'w')
    >    if outfile then
    >      write_zettels(outfile)
    >    end
    >  end
    >  current_zettel_id = zettels.root
    >
    >  while true do
    >    render(Window)
    >    update(Window)
    >
    >    -- save zettels, but hold on to previous state on disk
    >    -- until last possible second
    >    local filename = os.tmpname()
    >    local outfile = io.open(filename, 'w')
    >    write_zettels(outfile)
    >    os.rename(filename, 'zet')
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 07:32:46 2022
  __teliva_note:
    >stop writing sample zettels to disk
    >
    >That was just for ease of testing write_zettels()
  main:
    >function main()
    >  init_colors()
    >  curses.curs_set(0)  -- hide cursor except when editing
    >
    >  local infile = io.open('zet', 'r')
    >  if infile then
    >    read_zettels(infile)
    >  end
    >  current_zettel_id = zettels.root
    >
    >  while true do
    >    render(Window)
    >    update(Window)
    >
    >    -- save zettels, but hold on to previous state on disk
    >    -- until last possible second
    >    local filename = os.tmpname()
    >    local outfile = io.open(filename, 'w')
    >    if outfile then
    >      write_zettels(outfile)
    >      os.rename(filename, 'zet')
    >    end
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 07:43:39 2022
  zettels:
    >-- initial state of the zettels
    >-- if you came here to clear the zettels,
    >-- delete everything (ctrl-k and ctrl-u will delete a whole line at a time)
    >-- until it looks like this:
    >--
    >--   zettels = {
    >--     root='id1',
    >--     final=1,
    >--     id1={
    >--       data='',
    >--     },
    >--   }
    >--
    >-- I don't yet trust any deletion feature I create to not mess up your data.
    >-- Besides, this is a good excuse to start making this app your own.
    >
    >zettels = {
    >  root='id1',
    >  final=5,
    >  id1={
    >    data='this is zettel A\n\nit has some text',
    >    child='id3',
    >    next='id2',
    >  },
    >  id2={
    >    data='this is a sibling of zettel A at the top level',
    >    prev='id1',
    >    next='id5',
    >  },
    >  id3={
    >    data='this is zettel B, a child of A',
    >    parent='id1',
    >    next='id4',
    >  },
    >  id4={
    >    data='this is another child of zettel A, a sibling of B',
    >    parent='id1',
    >    prev='id3',
    >  },
    >  id5={
    >    data="(To clean up these sample zettels, hit ctrl-u and edit 'zettels')\n\nI don't yet trust any deletion feature I create to not mess up your data.\nBesides, this is a good excuse to start making this app your own.)",
    >    prev='id2',
    >  },
    >}
- __teliva_timestamp:
    >Thu Feb 10 20:24:13 2022
  menu:
    >-- To show app-specific hotkeys in the menu bar, add hotkey/command
    >-- arrays of strings to the menu array.
    >menu = {
    >  {'a,b,c', 'insert'},
    >  {'e', 'edit'},
    >  {'j,k,l,h', 'move'},
    >  {'x,X,y,Y', 'resize'},
    >  {'s', 'stash'},
    >  {'t', 'link with stash'},
    >}
- __teliva_timestamp:
    >Thu Feb 10 20:25:14 2022
  stash:
    >stash = nil
- __teliva_timestamp:
    >Thu Feb 10 20:32:38 2022
  update:
    >function update(window)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  local curr = zettels[current_zettel_id]
    >  assert(curr, string.format('cursor fell off the edge of the world: %s', type(current_zettel_id)))
    >  -- move along the graph
    >  if key == string.byte('j') then
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  elseif key == string.byte('k') then
    >    if curr.parent then current_zettel_id = curr.parent end
    >  elseif key == string.byte('h') then
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >  elseif key == string.byte('l') then
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  -- move along the screen
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  -- mutations
    >  elseif key == string.byte('e') then
    >    editz(window)
    >  elseif key == string.byte('a') then
    >    -- insert sibling after
    >    local old = curr.next
    >    curr.next = new_id()
    >    local new = zettels[curr.next]
    >    new.data = ''
    >    new.next = old
    >    new.prev = current_zettel_id
    >    if old then
    >      zettels[old].prev = curr.next
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.next
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('b') then
    >    -- insert sibling before
    >    local old = curr.prev
    >    curr.prev = new_id()
    >    local new = zettels[curr.prev]
    >    new.data = ''
    >    new.prev = old
    >    new.next = current_zettel_id
    >    if old then
    >      zettels[old].next = curr.prev
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.prev
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('c') then
    >    -- insert child
    >    local old = curr.child
    >    curr.child = new_id()
    >    local new = zettels[curr.child]
    >    new.data = ''
    >    new.next = old
    >    if old then
    >      assert(zettels[old].prev == nil, "first child shouldn't have a previous sibling")
    >      zettels[old].prev = curr.child
    >    end
    >    new.parent = curr
    >    current_zettel_id = curr.child
    >    render(window) -- recompute render_state
    >    editz(window)
    >  -- cross-links
    >  elseif key == string.byte('s') then
    >    -- save zettel to a stash
    >    stash = current_zettel_id
    >  elseif key == string.byte('t') then
    >    -- cross-link a zettel bidirectionally with what's on the stash
    >    if curr.crosslinks then
    >      curr.crosslinks.a = stash
    >    else
    >      curr.crosslinks = {a=stash}
    >    end
    >  -- view settings
    >  elseif key == string.byte('x') then
    >    if view_settings.width > 5 then
    >      view_settings.width = view_settings.width - 5
    >    end
    >  elseif key == string.byte('X') then
    >    if view_settings.width < w-5 then
    >      view_settings.width = view_settings.width + 5
    >    end
    >  elseif key == string.byte('y') then
    >    if view_settings.height > 0 then
    >      view_settings.height = view_settings.height - 1
    >    end
    >  elseif key == string.byte('Y') then
    >    if view_settings.height < h-2 then
    >      view_settings.height = view_settings.height + 1
    >    end
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 20:39:15 2022
  render:
    >function render(window)
    >  window:clear()
    >  local lines, cols = window:getmaxyx()
    >  local bg=1
    >  local y, x = 0, 0 -- units of characters (0-based)
    >  local w, h = 1, 1 -- units of zettels (1-based)
    >  -- render zettels depth-first, while tracking relative positions
    >  local done = {}
    >  local inprogress = {zettels.root}
    >  render_state.wh2id = {{}}
    >  while #inprogress > 0 do
    >    local currid = table.remove(inprogress)
    >    if not done[currid] then
    >      done[currid] = true
    >      table.insert(render_state.wh2id[w], currid)
    >      local zettel = zettels[currid]
    >      if currid == current_zettel_id then
    >        render_state.curr_w = w
    >        render_state.curr_h = h
    >      end
    >      local currbg = (currid == current_zettel_id) and view_settings.current_zettel_bg or bg
    >      render_zettel(window, currbg, depth(zettel) * view_settings.indent, y, x, zettel)
    >      if zettel.next then table.insert(inprogress, zettel.next) end
    >      if zettel.child then table.insert(inprogress, zettel.child) end
    >      if zettel.crosslinks then
    >        for relation, target in pairs(zettel.crosslinks) do
    >          table.insert(inprogress, target)
    >        end
    >      end
    >      bg = 3 - bg  -- toggle between color pairs 1 and 2
    >      y = y + view_settings.height + view_settings.vmargin
    >      h = h + 1
    >      if y + view_settings.height > lines then
    >        y = 0
    >        h = 1
    >        x = x + view_settings.width + view_settings.hmargin
    >        w = w + 1
    >        if x + view_settings.width > cols then break end
    >        table.insert(render_state.wh2id, {})
    >      end
    >    end
    >  end
    >  window:mvaddstr(lines-1, 0, '')
    >  bg = 1
    >  x = 0
    >  for i=1,3 do
    >    local zettel = nil
    >    if i == 1 and stash then
    >      zettel = zettels[stash]
    >    end
    >    render_zettel(window, bg, 0, lines-1, x, zettel)
    >    bg = 3 - bg
    >    x = x + view_settings.width + view_settings.hmargin
    >  end
    >  window:refresh()
    >end
- __teliva_timestamp:
    >Thu Feb 10 20:40:08 2022
  __teliva_note:
    >initial support for cross-links
    >
    >Kinda confusing because zettels still show indent based on their
    >hierarchical location rather than the path they're rendered in.
  render_zettel:
    >function render_zettel(window, bg, indent, starty, startx, zettel)
    >  window:attrset(curses.color_pair(bg))
    >  for y=0,view_settings.height-1 do
    >    for x=0,view_settings.width-1 do
    >      window:mvaddch(y+starty, x+startx, ' ')
    >    end
    >  end
    >  local y, x = 0, indent+1
    >  local data = ''
    >  if zettel then
    >    data = zettel.data
    >  end
    >  for i=1,#data do
    >    local c = data[i]
    >    if c == '\n' then
    >      y = y+1
    >      x = indent+1
    >    else
    >      window:mvaddstr(y+starty, x+startx, c)
    >      x = x+1
    >      if x >= startx + view_settings.width then
    >        y = y+1
    >        x = indent+1
    >      end
    >    end
    >    if y >= view_settings.height then
    >      break
    >    end
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 20:44:29 2022
  __teliva_note:
    >looks better after dynamically recomputing depth while rendering
  render:
    >function render(window)
    >  window:clear()
    >  local lines, cols = window:getmaxyx()
    >  local bg=1
    >  local y, x = 0, 0 -- units of characters (0-based)
    >  local w, h = 1, 1 -- units of zettels (1-based)
    >  -- render zettels depth-first, while tracking relative positions
    >  local done = {}
    >  local inprogress = {{id=zettels.root,depth=0}}
    >  render_state.wh2id = {{}}
    >  while #inprogress > 0 do
    >    local curr = table.remove(inprogress)
    >    if not done[curr.id] then
    >      done[curr.id] = true
    >      table.insert(render_state.wh2id[w], curr.id)
    >      local zettel = zettels[curr.id]
    >      if curr.id == current_zettel_id then
    >        render_state.curr_w = w
    >        render_state.curr_h = h
    >      end
    >      local currbg = (curr.id == current_zettel_id) and view_settings.current_zettel_bg or bg
    >      render_zettel(window, currbg, curr.depth * view_settings.indent, y, x, zettel)
    >      if zettel.next then table.insert(inprogress, {id=zettel.next, depth=curr.depth}) end
    >      if zettel.child then table.insert(inprogress, {id=zettel.child, depth=curr.depth+1}) end
    >      if zettel.crosslinks then
    >        for relation, target in pairs(zettel.crosslinks) do
    >          table.insert(inprogress, {id=target, depth=curr.depth+1})
    >        end
    >      end
    >      bg = 3 - bg  -- toggle between color pairs 1 and 2
    >      y = y + view_settings.height + view_settings.vmargin
    >      h = h + 1
    >      if y + view_settings.height > lines then
    >        y = 0
    >        h = 1
    >        x = x + view_settings.width + view_settings.hmargin
    >        w = w + 1
    >        if x + view_settings.width > cols then break end
    >        table.insert(render_state.wh2id, {})
    >      end
    >    end
    >  end
    >  window:mvaddstr(lines-1, 0, '')
    >  bg = 1
    >  x = 0
    >  for i=1,3 do
    >    local zettel = nil
    >    if i == 1 and stash then
    >      zettel = zettels[stash]
    >    end
    >    render_zettel(window, bg, 0, lines-1, x, zettel)
    >    bg = 3 - bg
    >    x = x + view_settings.width + view_settings.hmargin
    >  end
    >  window:refresh()
    >end
- __teliva_timestamp:
    >Thu Feb 10 20:55:19 2022
  render_zettel:
    >function render_zettel(window, bg, indent, edge_label, starty, startx, zettel)
    >  window:attrset(curses.color_pair(bg))
    >  for y=0,view_settings.height-1 do
    >    for x=0,view_settings.width-1 do
    >      window:mvaddch(y+starty, x+startx, ' ')
    >    end
    >  end
    >  if indent > 1 then
    >    window:attrset(curses.color_pair(bg+1))  -- go from zettel color to its edge color
    >    window:mvaddstr(starty, startx+indent-1, edge_label)
    >    window:attrset(curses.color_pair(bg))
    >  end
    >  local y, x = 0, indent+1
    >  local data = ''
    >  if zettel then
    >    data = zettel.data
    >  end
    >  for i=1,#data do
    >    local c = data[i]
    >    if c == '\n' then
    >      y = y+1
    >      x = indent+1
    >    else
    >      window:mvaddstr(y+starty, x+startx, c)
    >      x = x+1
    >      if x >= startx + view_settings.width then
    >        y = y+1
    >        x = indent+1
    >      end
    >    end
    >    if y >= view_settings.height then
    >      break
    >    end
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 20:58:49 2022
  view_settings:
    >view_settings = {
    >  -- dimensions for rendering a single zettel; extra text gets truncated
    >  width=50,
    >  height=3,
    >  -- spacing between zettels
    >  hmargin=1,
    >  vmargin=1,
    >  --
    >  indent=2,  -- how children of a zettel are indicated
    >}
- __teliva_timestamp:
    >Thu Feb 10 20:59:18 2022
  render:
    >function render(window)
    >  window:clear()
    >  local lines, cols = window:getmaxyx()
    >  local bg=3
    >  local y, x = 0, 0 -- units of characters (0-based)
    >  local w, h = 1, 1 -- units of zettels (1-based)
    >  -- render zettels depth-first, while tracking relative positions
    >  local done = {}
    >  local inprogress = {{id=zettels.root,depth=0,edge=''}}
    >  render_state.wh2id = {{}}
    >  while #inprogress > 0 do
    >    local curr = table.remove(inprogress)
    >    if not done[curr.id] then
    >      done[curr.id] = true
    >      table.insert(render_state.wh2id[w], curr.id)
    >      local zettel = zettels[curr.id]
    >      if curr.id == current_zettel_id then
    >        render_state.curr_w = w
    >        render_state.curr_h = h
    >      end
    >      local currbg = (curr.id == current_zettel_id) and 1 or bg  -- 1 is the color pair for the current zettel
    >      render_zettel(window, currbg, curr.depth * view_settings.indent, curr.edge, y, x, zettel)
    >      if zettel.next then table.insert(inprogress, {id=zettel.next, depth=curr.depth, edge='|'}) end
    >      if zettel.child then table.insert(inprogress, {id=zettel.child, depth=curr.depth+1, edge='\\'}) end
    >      if zettel.crosslinks then
    >        for relation, target in pairs(zettel.crosslinks) do
    >          table.insert(inprogress, {id=target, depth=curr.depth+1, edge=relation})
    >        end
    >      end
    >      bg = 8 - bg  -- toggle between color pairs 3 and 5
    >      y = y + view_settings.height + view_settings.vmargin
    >      h = h + 1
    >      if y + view_settings.height > lines then
    >        y = 0
    >        h = 1
    >        x = x + view_settings.width + view_settings.hmargin
    >        w = w + 1
    >        if x + view_settings.width > cols then break end
    >        table.insert(render_state.wh2id, {})
    >      end
    >    end
    >  end
    >  window:mvaddstr(lines-1, 0, '')
    >  bg = 3
    >  x = 0
    >  for i=1,3 do
    >    local zettel = nil
    >    if i == 1 and stash then
    >      zettel = zettels[stash]
    >    end
    >    render_zettel(window, bg, 0, '', lines-1, x, zettel)
    >    bg = 8 - bg  -- toggle between color pairs 3 and 5
    >    x = x + view_settings.width + view_settings.hmargin
    >  end
    >  window:refresh()
    >end
- __teliva_timestamp:
    >Thu Feb 10 21:02:41 2022
  __teliva_note:
    >label the incoming edge for each zettel
    >
    >Is it a child, sibling or other cross-link?
  init_colors:
    >function init_colors()
    >  -- light background
    >    -- current zettel
    >  curses.init_pair(1, 236, 230)
    >  curses.init_pair(2, 1,   230)  -- edge label for current zettel
    >    -- non-current zettel #1
    >  curses.init_pair(3, 236, 250)
    >  curses.init_pair(4, 1,   250)  -- edge label for pair 3
    >    -- non-current zettel #2
    >  curses.init_pair(5, 236, 252)
    >  curses.init_pair(6, 1,   252)  -- edge label for pair 5
    >  -- dark background
    >--?     -- current zettel
    >--?   curses.init_pair(7, 252, 130)
    >--?     -- other zettels
    >--?   curses.init_pair(1, 252, 240)
    >--?   curses.init_pair(2, 252, 242)
    >--?     -- edge labels
    >--?   curses.init_pair(3, 1, 240)  -- same bg as pair 1
    >--?   curses.init_pair(4, 1, 242)  -- same bg as pair 2
    >--?   curses.init_pair(9, 1, 130)  -- same bg as pair 7 for current zettel
    >end
- __teliva_timestamp:
    >Thu Feb 10 21:11:35 2022
  menu:
    >-- To show app-specific hotkeys in the menu bar, add hotkey/command
    >-- arrays of strings to the menu array.
    >menu = {
    >  {'a,b,c', 'insert'},
    >  {'e', 'edit'},
    >  {'j,k,l,h', 'move'},
    >  {'x,X,y,Y', 'resize'},
    >  {'s', 'stash'},
    >  {'t', 'link with stash'},
    >  {'z', 'scroll'},
    >}
- __teliva_timestamp:
    >Thu Feb 10 21:13:19 2022
  main:
    >function main()
    >  init_colors()
    >  curses.curs_set(0)  -- hide cursor except when editing
    >
    >  local infile = io.open('zet', 'r')
    >  if infile then
    >    read_zettels(infile)
    >  end
    >  current_zettel_id = zettels.root  -- cursor
    >  view_settings.first_zettel = zettels.root  -- start rendering here
    >
    >  while true do
    >    render(Window)
    >    update(Window)
    >
    >    -- save zettels, but hold on to previous state on disk
    >    -- until last possible second
    >    local filename = os.tmpname()
    >    local outfile = io.open(filename, 'w')
    >    if outfile then
    >      write_zettels(outfile)
    >      os.rename(filename, 'zet')
    >    end
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 21:13:36 2022
  render:
    >function render(window)
    >  window:clear()
    >  local lines, cols = window:getmaxyx()
    >  local bg=3
    >  local y, x = 0, 0 -- units of characters (0-based)
    >  local w, h = 1, 1 -- units of zettels (1-based)
    >  -- render zettels depth-first, while tracking relative positions
    >  local done = {}
    >  local inprogress = {{id=view_settings.first_zettel,depth=0,edge=''}}
    >  render_state.wh2id = {{}}
    >  while #inprogress > 0 do
    >    local curr = table.remove(inprogress)
    >    if not done[curr.id] then
    >      done[curr.id] = true
    >      table.insert(render_state.wh2id[w], curr.id)
    >      local zettel = zettels[curr.id]
    >      if curr.id == current_zettel_id then
    >        render_state.curr_w = w
    >        render_state.curr_h = h
    >      end
    >      local currbg = (curr.id == current_zettel_id) and 1 or bg  -- 1 is the color pair for the current zettel
    >      render_zettel(window, currbg, curr.depth * view_settings.indent, curr.edge, y, x, zettel)
    >      if zettel.next then table.insert(inprogress, {id=zettel.next, depth=curr.depth, edge='|'}) end
    >      if zettel.child then table.insert(inprogress, {id=zettel.child, depth=curr.depth+1, edge='\\'}) end
    >      if zettel.crosslinks then
    >        for relation, target in pairs(zettel.crosslinks) do
    >          table.insert(inprogress, {id=target, depth=curr.depth+1, edge=relation})
    >        end
    >      end
    >      bg = 8 - bg  -- toggle between color pairs 3 and 5
    >      y = y + view_settings.height + view_settings.vmargin
    >      h = h + 1
    >      if y + view_settings.height > lines then
    >        y = 0
    >        h = 1
    >        x = x + view_settings.width + view_settings.hmargin
    >        w = w + 1
    >        if x + view_settings.width > cols then break end
    >        table.insert(render_state.wh2id, {})
    >      end
    >    end
    >  end
    >  window:mvaddstr(lines-1, 0, '')
    >  bg = 3
    >  x = 0
    >  for i=1,3 do
    >    local zettel = nil
    >    if i == 1 and stash then
    >      zettel = zettels[stash]
    >    end
    >    render_zettel(window, bg, 0, '', lines-1, x, zettel)
    >    bg = 8 - bg  -- toggle between color pairs 3 and 5
    >    x = x + view_settings.width + view_settings.hmargin
    >  end
    >  window:refresh()
    >end
- __teliva_timestamp:
    >Thu Feb 10 21:19:26 2022
  __teliva_note:
    >bugfix: cross-links should be bidirectional
  update:
    >function update(window)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  local curr = zettels[current_zettel_id]
    >  assert(curr, string.format('cursor fell off the edge of the world: %s', type(current_zettel_id)))
    >  -- move along the graph
    >  if key == string.byte('j') then
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  elseif key == string.byte('k') then
    >    if curr.parent then current_zettel_id = curr.parent end
    >  elseif key == string.byte('h') then
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >  elseif key == string.byte('l') then
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  -- move along the screen
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  -- mutations
    >  elseif key == string.byte('e') then
    >    editz(window)
    >  elseif key == string.byte('a') then
    >    -- insert sibling after
    >    local old = curr.next
    >    curr.next = new_id()
    >    local new = zettels[curr.next]
    >    new.data = ''
    >    new.next = old
    >    new.prev = current_zettel_id
    >    if old then
    >      zettels[old].prev = curr.next
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.next
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('b') then
    >    -- insert sibling before
    >    local old = curr.prev
    >    curr.prev = new_id()
    >    local new = zettels[curr.prev]
    >    new.data = ''
    >    new.prev = old
    >    new.next = current_zettel_id
    >    if old then
    >      zettels[old].next = curr.prev
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.prev
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('c') then
    >    -- insert child
    >    local old = curr.child
    >    curr.child = new_id()
    >    local new = zettels[curr.child]
    >    new.data = ''
    >    new.next = old
    >    if old then
    >      assert(zettels[old].prev == nil, "first child shouldn't have a previous sibling")
    >      zettels[old].prev = curr.child
    >    end
    >    new.parent = curr
    >    current_zettel_id = curr.child
    >    render(window) -- recompute render_state
    >    editz(window)
    >  -- cross-links
    >  elseif key == string.byte('s') then
    >    -- save zettel to a stash
    >    stash = current_zettel_id
    >  elseif key == string.byte('t') then
    >    -- cross-link a zettel bidirectionally with what's on the stash
    >    local insert_crosslink =
    >      function(a, rel, b_id)
    >        if a.crosslinks == nil then
    >          a.crosslinks = {}
    >        end
    >        a.crosslinks[rel] = b_id
    >      end
    >    insert_crosslink(curr, 'a', stash)
    >    insert_crosslink(zettels[stash], 'a', current_zettel_id)
    >  -- view settings
    >  elseif key == string.byte('x') then
    >    if view_settings.width > 5 then
    >      view_settings.width = view_settings.width - 5
    >    end
    >  elseif key == string.byte('X') then
    >    if view_settings.width < w-5 then
    >      view_settings.width = view_settings.width + 5
    >    end
    >  elseif key == string.byte('y') then
    >    if view_settings.height > 0 then
    >      view_settings.height = view_settings.height - 1
    >    end
    >  elseif key == string.byte('Y') then
    >    if view_settings.height < h-2 then
    >      view_settings.height = view_settings.height + 1
    >    end
    >  elseif key == string.byte('z') then
    >    -- scroll to show the current zettel at top of screen
    >    -- often has the effect of zooming in on its hierarchy
    >    view_settings.first_zettel = current_zettel_id
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 21:20:45 2022
  __teliva_note:
    >clear stash after linking
  update:
    >function update(window)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  local curr = zettels[current_zettel_id]
    >  assert(curr, string.format('cursor fell off the edge of the world: %s', type(current_zettel_id)))
    >  -- move along the graph
    >  if key == string.byte('j') then
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  elseif key == string.byte('k') then
    >    if curr.parent then current_zettel_id = curr.parent end
    >  elseif key == string.byte('h') then
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >  elseif key == string.byte('l') then
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  -- move along the screen
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  -- mutations
    >  elseif key == string.byte('e') then
    >    editz(window)
    >  elseif key == string.byte('a') then
    >    -- insert sibling after
    >    local old = curr.next
    >    curr.next = new_id()
    >    local new = zettels[curr.next]
    >    new.data = ''
    >    new.next = old
    >    new.prev = current_zettel_id
    >    if old then
    >      zettels[old].prev = curr.next
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.next
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('b') then
    >    -- insert sibling before
    >    local old = curr.prev
    >    curr.prev = new_id()
    >    local new = zettels[curr.prev]
    >    new.data = ''
    >    new.prev = old
    >    new.next = current_zettel_id
    >    if old then
    >      zettels[old].next = curr.prev
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.prev
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('c') then
    >    -- insert child
    >    local old = curr.child
    >    curr.child = new_id()
    >    local new = zettels[curr.child]
    >    new.data = ''
    >    new.next = old
    >    if old then
    >      assert(zettels[old].prev == nil, "first child shouldn't have a previous sibling")
    >      zettels[old].prev = curr.child
    >    end
    >    new.parent = curr
    >    current_zettel_id = curr.child
    >    render(window) -- recompute render_state
    >    editz(window)
    >  -- cross-links
    >  elseif key == string.byte('s') then
    >    -- save zettel to a stash
    >    stash = current_zettel_id
    >  elseif key == string.byte('t') then
    >    -- cross-link a zettel bidirectionally with what's on the stash
    >    local insert_crosslink =
    >      function(a, rel, b_id)
    >        if a.crosslinks == nil then
    >          a.crosslinks = {}
    >        end
    >        a.crosslinks[rel] = b_id
    >      end
    >    insert_crosslink(curr, 'a', stash)
    >    insert_crosslink(zettels[stash], 'a', current_zettel_id)
    >    stash = nil
    >  -- view settings
    >  elseif key == string.byte('x') then
    >    if view_settings.width > 5 then
    >      view_settings.width = view_settings.width - 5
    >    end
    >  elseif key == string.byte('X') then
    >    if view_settings.width < w-5 then
    >      view_settings.width = view_settings.width + 5
    >    end
    >  elseif key == string.byte('y') then
    >    if view_settings.height > 0 then
    >      view_settings.height = view_settings.height - 1
    >    end
    >  elseif key == string.byte('Y') then
    >    if view_settings.height < h-2 then
    >      view_settings.height = view_settings.height + 1
    >    end
    >  elseif key == string.byte('z') then
    >    -- scroll to show the current zettel at top of screen
    >    -- often has the effect of zooming in on its hierarchy
    >    view_settings.first_zettel = current_zettel_id
    >  end
    >end
- __teliva_timestamp:
    >Thu Feb 10 21:51:09 2022
  __teliva_note:
    >fix regression in editor
  editz_render:
    >function editz_render(window, s, cursor, top, minbottom, left, right)
    >  local h, w = window:getmaxyx()
    >  local cursor_y, cursor_x = 0, 0
    >  window:attrset(curses.color_pair(1)) -- 1 is the color combination for the current zettel
    >  for y=top,minbottom-1 do
    >    for x=left,right-1 do
    >      window:mvaddch(y, x, ' ')
    >    end
    >  end
    >  local y, x = top, left + 1  -- left padding; TODO: indent
    >  window:mvaddstr(y, x, '')
    >  for i=1,string.len(s) do
    >    if i == cursor then
    >      cursor_y = y
    >      cursor_x = x
    >    end
    >    if s[i] ~= '\n' then
    >      window:addstr(s[i])
    >      x = x + 1
    >      if x >= right then
    >        y = y + 1
    >        if y >= h-2 then return end
    >        x = left + 1  -- left padding; TODO: indent
    >        window:mvaddstr(y, x, '')
    >      end
    >    else
    >      for col=x+1,right-1 do window:addch(' '); end
    >      x = left
    >      y = y + 1
    >      if y >= h-2 then return end
    >      window:mvaddstr(y, x, '')
    >      for col=x,right-1 do window:addch(' '); end
    >      x = left + 1  -- left padding; TODO: indent
    >      window:mvaddstr(y, x, '')
    >    end
    >  end
    >  if cursor_y == 0 and cursor_x == 0 then
    >    cursor_y = y
    >    cursor_x = x
    >  end
    >  window:mvaddstr(cursor_y, cursor_x, '')
    >end
- __teliva_timestamp:
    >Fri Feb 11 01:33:31 2022
  __teliva_note:
    >support /tmp being on a separate volume
    >
    >also better error-checking
  main:
    >function main()
    >  init_colors()
    >  curses.curs_set(0)  -- hide cursor except when editing
    >
    >  local infile = io.open('zet', 'r')
    >  if infile then
    >    read_zettels(infile)
    >  end
    >  current_zettel_id = zettels.root  -- cursor
    >  view_settings.first_zettel = zettels.root  -- start rendering here
    >
    >  while true do
    >    render(Window)
    >    update(Window)
    >
    >    -- save zettels, but hold on to previous state on disk
    >    -- until last possible second
    >    local outfile = io.open('teliva_tmp', 'w')
    >    if outfile then
    >      write_zettels(outfile)
    >      local status, message = os.rename('teliva_tmp', 'zet')
    >      assert(status, message)  -- unceremoniously abort, but we hopefully only lost a little
    >    end
    >    -- TODO: what if io.open failed for a non-sandboxing related reason?!
    >    -- We could silently fail to save.
    >  end
    >end
- __teliva_timestamp:
    >Fri Feb 11 07:51:42 2022
  __teliva_note:
    >bugfix in parent link when inserting child
  update:
    >function update(window)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  local curr = zettels[current_zettel_id]
    >  assert(curr, string.format('cursor fell off the edge of the world: %s', type(current_zettel_id)))
    >  -- move along the graph
    >  if key == string.byte('j') then
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  elseif key == string.byte('k') then
    >    if curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >  elseif key == string.byte('h') then
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >  elseif key == string.byte('l') then
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >  -- move along the screen
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  -- mutations
    >  elseif key == string.byte('e') then
    >    editz(window)
    >  elseif key == string.byte('a') then
    >    -- insert sibling after
    >    local old = curr.next
    >    curr.next = new_id()
    >    local new = zettels[curr.next]
    >    new.data = ''
    >    new.next = old
    >    new.prev = current_zettel_id
    >    if old then
    >      zettels[old].prev = curr.next
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.next
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('b') then
    >    -- insert sibling before
    >    local old = curr.prev
    >    curr.prev = new_id()
    >    local new = zettels[curr.prev]
    >    new.data = ''
    >    new.prev = old
    >    new.next = current_zettel_id
    >    if old then
    >      zettels[old].next = curr.prev
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.prev
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('c') then
    >    -- insert child
    >    local old = curr.child
    >    curr.child = new_id()
    >    local new = zettels[curr.child]
    >    new.data = ''
    >    new.next = old
    >    if old then
    >      assert(zettels[old].prev == nil, "first child shouldn't have a previous sibling")
    >      zettels[old].prev = curr.child
    >    end
    >    new.parent = current_zettel_id
    >    current_zettel_id = curr.child
    >    render(window) -- recompute render_state
    >    editz(window)
    >  -- cross-links
    >  elseif key == string.byte('s') then
    >    -- save zettel to a stash
    >    stash = current_zettel_id
    >  elseif key == string.byte('t') then
    >    -- cross-link a zettel bidirectionally with what's on the stash
    >    local insert_crosslink =
    >      function(a, rel, b_id)
    >        if a.crosslinks == nil then
    >          a.crosslinks = {}
    >        end
    >        a.crosslinks[rel] = b_id
    >      end
    >    insert_crosslink(curr, 'a', stash)
    >    insert_crosslink(zettels[stash], 'a', current_zettel_id)
    >    stash = nil
    >  -- view settings
    >  elseif key == string.byte('x') then
    >    if view_settings.width > 5 then
    >      view_settings.width = view_settings.width - 5
    >    end
    >  elseif key == string.byte('X') then
    >    if view_settings.width < w-5 then
    >      view_settings.width = view_settings.width + 5
    >    end
    >  elseif key == string.byte('y') then
    >    if view_settings.height > 0 then
    >      view_settings.height = view_settings.height - 1
    >    end
    >  elseif key == string.byte('Y') then
    >    if view_settings.height < h-2 then
    >      view_settings.height = view_settings.height + 1
    >    end
    >  elseif key == string.byte('z') then
    >    -- scroll to show the current zettel at top of screen
    >    -- often has the effect of zooming in on its hierarchy
    >    view_settings.first_zettel = current_zettel_id
    >  end
    >end
- __teliva_timestamp:
    >Sat Feb 12 15:11:15 2022
  editz_update:
    >function editz_update(window, prose, cursor, original_prose)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  -- cursor movement
    >  if key == curses.KEY_LEFT then
    >    if cursor > 1 then
    >      cursor = cursor-1
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if cursor <= #prose then
    >      cursor = cursor+1
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    cursor = cursor_down(prose, cursor, w)
    >  elseif key == curses.KEY_UP then
    >    cursor = cursor_up(prose, cursor, w)
    >  elseif key == curses.KEY_BACKSPACE or key == 8 or key == 127 then  -- ctrl-h, ctrl-?, delete
    >    if cursor > 1 then
    >      cursor = cursor-1
    >      prose = prose:remove(cursor)
    >    end
    >  elseif key == 1 then  -- ctrl-a
    >  elseif key == 12 then  -- ctrl-l
    >  elseif key == 6 then  -- ctrl-f
    >  elseif key == 2 then  -- ctrl-b
    >  -- delete
    >  elseif key == 11 then  -- ctrl-k
    >  -- exit
    >  elseif key == 5 then  -- ctrl-e
    >    return true, prose, cursor
    >  elseif key == 7 then  -- ctrl-g
    >    return true, original_prose, cursor
    >  -- insert
    >  elseif key == 10 or (key >= 32 and key < 127) then
    >    prose = prose:insert(string.char(key), cursor-1)
    >    cursor = cursor+1
    >  end
    >  return false, prose, cursor
    >end
- __teliva_timestamp:
    >Sat Feb 12 15:11:33 2022
  editz:
    >function editz(window)
    >  local old_menu = menu
    >  menu = {
    >    {'^e', 'finish edit'},
    >    {'^g', 'cancel edit'},
    >    {'^a', '<<line'},
    >    {'^b', '<word'},
    >    {'^f', 'word>'},
    >    {'^l', 'line>>'},
    >    {'^k', 'del to line>>'},
    >  }
    >  local old_data = string.sub(zettels[current_zettel_id].data, 1)
    >  local top = (render_state.curr_h - 1) * (view_settings.height + view_settings.vmargin)
    >  local bottom = top + view_settings.height
    >  local left = (render_state.curr_w - 1) * (view_settings.width + view_settings.hmargin)
    >  local right = left + view_settings.width
    >  local cursor = string.len(zettels[current_zettel_id].data)+1
    >  local quit = false
    >  curses.curs_set(1)
    >  while not quit do
    >    editz_render(window, zettels[current_zettel_id].data, cursor, top, bottom, left, right)
    >    quit, zettels[current_zettel_id].data, cursor = editz_update(window, zettels[current_zettel_id].data, cursor, old_data)
    >  end
    >  curses.curs_set(0)
    >  menu = old_menu
    >end
- __teliva_timestamp:
    >Sat Feb 12 15:55:10 2022
  __teliva_note:
    >editor: move to start of line, move/delete to end of line
  editz_update:
    >function editz_update(window, prose, cursor, original_prose)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  -- cursor movement
    >  if key == curses.KEY_LEFT then
    >    if cursor > 1 then
    >      cursor = cursor-1
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if cursor <= #prose then
    >      cursor = cursor+1
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    cursor = cursor_down(prose, cursor, w)
    >  elseif key == curses.KEY_UP then
    >    cursor = cursor_up(prose, cursor, w)
    >  elseif key == curses.KEY_BACKSPACE or key == 8 or key == 127 then  -- ctrl-h, ctrl-?, delete
    >    if cursor > 1 then
    >      cursor = cursor-1
    >      prose = prose:remove(cursor)
    >    end
    >  elseif key == 1 then  -- ctrl-a
    >    while cursor > 1 do
    >      if prose[cursor-1] == '\n' then break end
    >      cursor = cursor-1
    >    end
    >  elseif key == 12 then  -- ctrl-l
    >    local max = string.len(prose)
    >    while cursor <= max and prose[cursor] ~= '\n' do
    >      cursor = cursor+1
    >    end
    >  elseif key == 6 then  -- ctrl-f
    >  elseif key == 2 then  -- ctrl-b
    >  -- delete
    >  elseif key == 11 then  -- ctrl-k
    >    while cursor <= string.len(prose) and prose[cursor] ~= '\n' do
    >      prose = prose:remove(cursor)
    >    end
    >  -- exit
    >  elseif key == 5 then  -- ctrl-e
    >    return true, prose, cursor
    >  elseif key == 7 then  -- ctrl-g
    >    return true, original_prose, cursor
    >  -- insert
    >  elseif key == 10 or (key >= 32 and key < 127) then
    >    prose = prose:insert(string.char(key), cursor-1)
    >    cursor = cursor+1
    >  end
    >  return false, prose, cursor
    >end
- __teliva_timestamp:
    >Sat Feb 12 17:01:45 2022
  __teliva_note:
    >editor: word-movement shortcuts
  editz_update:
    >function editz_update(window, prose, cursor, original_prose)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  -- cursor movement
    >  if key == curses.KEY_LEFT then
    >    if cursor > 1 then
    >      cursor = cursor-1
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if cursor <= #prose then
    >      cursor = cursor+1
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    cursor = cursor_down(prose, cursor, w)
    >  elseif key == curses.KEY_UP then
    >    cursor = cursor_up(prose, cursor, w)
    >  elseif key == curses.KEY_BACKSPACE or key == 8 or key == 127 then  -- ctrl-h, ctrl-?, delete
    >    if cursor > 1 then
    >      cursor = cursor-1
    >      prose = prose:remove(cursor)
    >    end
    >  elseif key == 1 then  -- ctrl-a
    >    -- to start of line
    >    while cursor > 1 do
    >      if prose[cursor-1] == '\n' then break end
    >      cursor = cursor-1
    >    end
    >  elseif key == 12 then  -- ctrl-l
    >    -- to end of line
    >    local max = string.len(prose)
    >    while cursor <= max and prose[cursor] ~= '\n' do
    >      cursor = cursor+1
    >    end
    >  elseif key == 6 then  -- ctrl-f
    >    -- to next word
    >    local max = string.len(prose)
    >    while cursor <= max and string.match(prose[cursor], '%w') do
    >      cursor = cursor+1
    >    end
    >    while cursor <= max and string.match(prose[cursor], '%W') do
    >      cursor = cursor+1
    >    end
    >  elseif key == 2 then  -- ctrl-b
    >    -- to previous word
    >    if cursor > string.len(prose) then
    >      cursor = string.len(prose)
    >    end
    >    while cursor > 1 and string.match(prose[cursor], '%W') do
    >      cursor = cursor-1
    >    end
    >    while cursor > 1 and string.match(prose[cursor], '%w') do
    >      cursor = cursor-1
    >    end
    >  -- delete
    >  elseif key == 11 then  -- ctrl-k
    >    while cursor <= string.len(prose) and prose[cursor] ~= '\n' do
    >      prose = prose:remove(cursor)
    >    end
    >  -- exit
    >  elseif key == 5 then  -- ctrl-e
    >    return true, prose, cursor
    >  elseif key == 7 then  -- ctrl-g
    >    return true, original_prose, cursor
    >  -- insert
    >  elseif key == 10 or (key >= 32 and key < 127) then
    >    prose = prose:insert(string.char(key), cursor-1)
    >    cursor = cursor+1
    >  end
    >  return false, prose, cursor
    >end
- __teliva_timestamp:
    >Sat Feb 12 17:12:27 2022
  editz_render:
    >function editz_render(window, s, cursor, top, minbottom, left, right)
    >  local h, w = window:getmaxyx()
    >  local cursor_y, cursor_x = 0, 0
    >  window:attrset(curses.color_pair(1)) -- 1 is the color combination for the current zettel
    >  for y=top,minbottom-1 do
    >    for x=left,right-1 do
    >      window:mvaddch(y, x, ' ')
    >    end
    >  end
    >  for x=left,right-1 do
    >    window:mvaddch(minbottom, x, ' ')
    >  end
    >  local y, x = top, left + 1  -- left padding; TODO: indent
    >  window:mvaddstr(y, x, '')
    >  for i=1,string.len(s) do
    >    if i == cursor then
    >      cursor_y = y
    >      cursor_x = x
    >    end
    >    if s[i] ~= '\n' then
    >      window:addstr(s[i])
    >      x = x + 1
    >      if x >= right then
    >        y = y + 1
    >        if y >= h-2 then return end
    >        x = left + 1  -- left padding; TODO: indent
    >        window:mvaddstr(y, x, '')
    >      end
    >    else
    >      for col=x+1,right-1 do window:addch(' '); end
    >      x = left
    >      y = y + 1
    >      if y >= h-2 then return end
    >      window:mvaddstr(y, x, '')
    >      for col=x,right-1 do window:addch(' '); end
    >      x = left + 1  -- left padding; TODO: indent
    >      window:mvaddstr(y, x, '')
    >    end
    >  end
    >  if cursor_y == 0 and cursor_x == 0 then
    >    cursor_y = y
    >    cursor_x = x
    >  end
    >  window:mvaddstr(cursor_y, cursor_x, '')
    >end
- __teliva_timestamp:
    >Sat Feb 12 17:15:15 2022
  render_state:
    >-- some information about what's been drawn on screen
    >render_state = {
    >  -- where the current zettel is, in units of zettels
    >  curr_h = 1,
    >  curr_w = 1,
    >  -- what zettel is at each position on screen, in units of zettels
    >  hw2id = {},
    >  -- list of zettels currently displayed
    >  displayed = {},
    >}
- __teliva_timestamp:
    >Sat Feb 12 17:16:20 2022
  render:
    >function render(window)
    >  window:clear()
    >  local lines, cols = window:getmaxyx()
    >  local bg=3
    >  local y, x = 0, 0 -- units of characters (0-based)
    >  local w, h = 1, 1 -- units of zettels (1-based)
    >  -- render zettels depth-first, while tracking relative positions
    >  local done = {}
    >  local inprogress = {{id=view_settings.first_zettel,depth=0,edge=''}}
    >  render_state.wh2id = {{}}
    >  render_state.displayed = {}
    >  while #inprogress > 0 do
    >    local curr = table.remove(inprogress)
    >    if not done[curr.id] then
    >      done[curr.id] = true
    >      render_state.displayed[curr.id] = true
    >      table.insert(render_state.wh2id[w], curr.id)
    >      local zettel = zettels[curr.id]
    >      if curr.id == current_zettel_id then
    >        render_state.curr_w = w
    >        render_state.curr_h = h
    >      end
    >      local currbg = (curr.id == current_zettel_id) and 1 or bg  -- 1 is the color pair for the current zettel
    >      render_zettel(window, currbg, curr.depth * view_settings.indent, curr.edge, y, x, zettel)
    >      if zettel.next then table.insert(inprogress, {id=zettel.next, depth=curr.depth, edge='|'}) end
    >      if zettel.child then table.insert(inprogress, {id=zettel.child, depth=curr.depth+1, edge='\\'}) end
    >      if zettel.crosslinks then
    >        for relation, target in pairs(zettel.crosslinks) do
    >          table.insert(inprogress, {id=target, depth=curr.depth+1, edge=relation})
    >        end
    >      end
    >      bg = 8 - bg  -- toggle between color pairs 3 and 5
    >      y = y + view_settings.height + view_settings.vmargin
    >      h = h + 1
    >      if y + view_settings.height > lines then
    >        y = 0
    >        h = 1
    >        x = x + view_settings.width + view_settings.hmargin
    >        w = w + 1
    >        if x + view_settings.width > cols then break end
    >        table.insert(render_state.wh2id, {})
    >      end
    >    end
    >  end
    >  window:mvaddstr(lines-1, 0, '')
    >  bg = 3
    >  x = 0
    >  for i=1,3 do
    >    local zettel = nil
    >    if i == 1 and stash then
    >      zettel = zettels[stash]
    >    end
    >    render_zettel(window, bg, 0, '', lines-1, x, zettel)
    >    bg = 8 - bg  -- toggle between color pairs 3 and 5
    >    x = x + view_settings.width + view_settings.hmargin
    >  end
    >  window:refresh()
    >end
- __teliva_timestamp:
    >Sat Feb 12 17:18:34 2022
  __teliva_note:
    >scroll as needed when moving along the graph
  update:
    >function update(window)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  local curr = zettels[current_zettel_id]
    >  assert(curr, string.format('cursor fell off the edge of the world: %s', type(current_zettel_id)))
    >  -- move along the graph
    >  if key == string.byte('j') then
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  elseif key == string.byte('k') then
    >    if curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  elseif key == string.byte('h') then
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  elseif key == string.byte('l') then
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  -- move along the screen
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  -- mutations
    >  elseif key == string.byte('e') then
    >    editz(window)
    >  elseif key == string.byte('a') then
    >    -- insert sibling after
    >    local old = curr.next
    >    curr.next = new_id()
    >    local new = zettels[curr.next]
    >    new.data = ''
    >    new.next = old
    >    new.prev = current_zettel_id
    >    if old then
    >      zettels[old].prev = curr.next
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.next
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('b') then
    >    -- insert sibling before
    >    local old = curr.prev
    >    curr.prev = new_id()
    >    local new = zettels[curr.prev]
    >    new.data = ''
    >    new.prev = old
    >    new.next = current_zettel_id
    >    if old then
    >      zettels[old].next = curr.prev
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.prev
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('c') then
    >    -- insert child
    >    local old = curr.child
    >    curr.child = new_id()
    >    local new = zettels[curr.child]
    >    new.data = ''
    >    new.next = old
    >    if old then
    >      assert(zettels[old].prev == nil, "first child shouldn't have a previous sibling")
    >      zettels[old].prev = curr.child
    >    end
    >    new.parent = current_zettel_id
    >    current_zettel_id = curr.child
    >    render(window) -- recompute render_state
    >    editz(window)
    >  -- cross-links
    >  elseif key == string.byte('s') then
    >    -- save zettel to a stash
    >    stash = current_zettel_id
    >  elseif key == string.byte('t') then
    >    -- cross-link a zettel bidirectionally with what's on the stash
    >    local insert_crosslink =
    >      function(a, rel, b_id)
    >        if a.crosslinks == nil then
    >          a.crosslinks = {}
    >        end
    >        a.crosslinks[rel] = b_id
    >      end
    >    insert_crosslink(curr, 'a', stash)
    >    insert_crosslink(zettels[stash], 'a', current_zettel_id)
    >    stash = nil
    >  -- view settings
    >  elseif key == string.byte('x') then
    >    if view_settings.width > 5 then
    >      view_settings.width = view_settings.width - 5
    >    end
    >  elseif key == string.byte('X') then
    >    if view_settings.width < w-5 then
    >      view_settings.width = view_settings.width + 5
    >    end
    >  elseif key == string.byte('y') then
    >    if view_settings.height > 0 then
    >      view_settings.height = view_settings.height - 1
    >    end
    >  elseif key == string.byte('Y') then
    >    if view_settings.height < h-2 then
    >      view_settings.height = view_settings.height + 1
    >    end
    >  elseif key == string.byte('z') then
    >    -- scroll to show the current zettel at top of screen
    >    -- often has the effect of zooming in on its hierarchy
    >    view_settings.first_zettel = current_zettel_id
    >  end
    >end
- __teliva_timestamp:
    >Sat Feb 12 17:23:33 2022
  __teliva_note:
    >editor 'k' shortcut: fall back to next sibling if needed
    >
    >Now we should be able to navigate either with j/k or h/l.
  update:
    >function update(window)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  local curr = zettels[current_zettel_id]
    >  assert(curr, string.format('cursor fell off the edge of the world: %s', type(current_zettel_id)))
    >  -- move along the graph
    >  if key == string.byte('j') then
    >    -- child or next sibling
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  elseif key == string.byte('k') then
    >    -- parent or previous sibling
    >    if curr.parent then
    >      current_zettel_id = curr.parent
    >    elseif curr.prev then
    >      current_zettel_id = curr.prev
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  elseif key == string.byte('h') then
    >    -- previous sibling or parent
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  elseif key == string.byte('l') then
    >    -- next sibling or next sibling of parent
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  -- move along the screen
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  -- mutations
    >  elseif key == string.byte('e') then
    >    editz(window)
    >  elseif key == string.byte('a') then
    >    -- insert sibling after
    >    local old = curr.next
    >    curr.next = new_id()
    >    local new = zettels[curr.next]
    >    new.data = ''
    >    new.next = old
    >    new.prev = current_zettel_id
    >    if old then
    >      zettels[old].prev = curr.next
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.next
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('b') then
    >    -- insert sibling before
    >    local old = curr.prev
    >    curr.prev = new_id()
    >    local new = zettels[curr.prev]
    >    new.data = ''
    >    new.prev = old
    >    new.next = current_zettel_id
    >    if old then
    >      zettels[old].next = curr.prev
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.prev
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('c') then
    >    -- insert child
    >    local old = curr.child
    >    curr.child = new_id()
    >    local new = zettels[curr.child]
    >    new.data = ''
    >    new.next = old
    >    if old then
    >      assert(zettels[old].prev == nil, "first child shouldn't have a previous sibling")
    >      zettels[old].prev = curr.child
    >    end
    >    new.parent = current_zettel_id
    >    current_zettel_id = curr.child
    >    render(window) -- recompute render_state
    >    editz(window)
    >  -- cross-links
    >  elseif key == string.byte('s') then
    >    -- save zettel to a stash
    >    stash = current_zettel_id
    >  elseif key == string.byte('t') then
    >    -- cross-link a zettel bidirectionally with what's on the stash
    >    local insert_crosslink =
    >      function(a, rel, b_id)
    >        if a.crosslinks == nil then
    >          a.crosslinks = {}
    >        end
    >        a.crosslinks[rel] = b_id
    >      end
    >    insert_crosslink(curr, 'a', stash)
    >    insert_crosslink(zettels[stash], 'a', current_zettel_id)
    >    stash = nil
    >  -- view settings
    >  elseif key == string.byte('x') then
    >    if view_settings.width > 5 then
    >      view_settings.width = view_settings.width - 5
    >    end
    >  elseif key == string.byte('X') then
    >    if view_settings.width < w-5 then
    >      view_settings.width = view_settings.width + 5
    >    end
    >  elseif key == string.byte('y') then
    >    if view_settings.height > 0 then
    >      view_settings.height = view_settings.height - 1
    >    end
    >  elseif key == string.byte('Y') then
    >    if view_settings.height < h-2 then
    >      view_settings.height = view_settings.height + 1
    >    end
    >  elseif key == string.byte('z') then
    >    -- scroll to show the current zettel at top of screen
    >    -- often has the effect of zooming in on its hierarchy
    >    view_settings.first_zettel = current_zettel_id
    >  end
    >end
- __teliva_timestamp:
    >Sat Feb 12 17:27:18 2022
  menu:
    >-- To show app-specific hotkeys in the menu bar, add hotkey/command
    >-- arrays of strings to the menu array.
    >menu = {
    >  {'a,b,c', 'insert'},
    >  {'e', 'edit'},
    >  {'j,k,l,h', 'move'},
    >  {'<', 'back'},
    >  {'x,X,y,Y', 'resize'},
    >  {'s', 'stash'},
    >  {'t', 'link with stash'},
    >  {'z', 'scroll'},
    >}
- __teliva_timestamp:
    >Sat Feb 12 17:57:15 2022
  update:
    >function update(window)
    >  local key = window:getch()
    >  local h, w = window:getmaxyx()
    >  local curr = zettels[current_zettel_id]
    >  assert(curr, string.format('cursor fell off the edge of the world: %s', type(current_zettel_id)))
    >  -- read from or write to render_state.history
    >  if key == string.byte('<') then
    >    -- previous zettel moved to
    >    -- does NOT undo mutations
    >    if #render_state.history > 0 then
    >      local previous_state = render_state.history[#render_state.history]
    >      view_settings.first_zettel = previous_state.first_zettel
    >      current_zettel_id = previous_state.cursor
    >      table.remove(render_state.history)
    >    end
    >    return
    >  end
    >  if key ~= string.byte('e') then
    >    table.insert(render_state.history, {first_zettel=view_settings.first_zettel, cursor=current_zettel_id})
    >  end
    >  -- move along the graph
    >  if key == string.byte('j') then
    >    -- child or next sibling
    >    if curr.child then
    >      current_zettel_id = curr.child
    >    elseif curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  elseif key == string.byte('k') then
    >    -- parent or previous sibling
    >    if curr.parent then
    >      current_zettel_id = curr.parent
    >    elseif curr.prev then
    >      current_zettel_id = curr.prev
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  elseif key == string.byte('h') then
    >    -- previous sibling or parent
    >    if curr.prev then
    >      current_zettel_id = curr.prev
    >    elseif curr.parent then
    >      current_zettel_id = curr.parent
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  elseif key == string.byte('l') then
    >    -- next sibling or next sibling of parent
    >    if curr.next then
    >      current_zettel_id = curr.next
    >    elseif curr.parent and zettels[curr.parent].next then
    >      current_zettel_id = zettels[curr.parent].next
    >    end
    >    -- scroll if necessary
    >    if not render_state.displayed[current_zettel_id] then
    >      view_settings.first_zettel = current_zettel_id
    >    end
    >  -- move along the screen
    >  elseif key == curses.KEY_UP then
    >    if render_state.curr_h > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h - 1]
    >    end
    >  elseif key == curses.KEY_DOWN then
    >    if render_state.wh2id[render_state.curr_w][render_state.curr_h + 1] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w][render_state.curr_h + 1]
    >    end
    >  elseif key == curses.KEY_LEFT then
    >    if render_state.curr_w > 1 then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w - 1][render_state.curr_h]
    >    end
    >  elseif key == curses.KEY_RIGHT then
    >    if render_state.wh2id[render_state.curr_w + 1] and render_state.wh2id[render_state.curr_w + 1][render_state.curr_h] then
    >      current_zettel_id = render_state.wh2id[render_state.curr_w + 1][render_state.curr_h]
    >    end
    >  -- mutations
    >  elseif key == string.byte('e') then
    >    editz(window)
    >  elseif key == string.byte('a') then
    >    -- insert sibling after
    >    local old = curr.next
    >    curr.next = new_id()
    >    local new = zettels[curr.next]
    >    new.data = ''
    >    new.next = old
    >    new.prev = current_zettel_id
    >    if old then
    >      zettels[old].prev = curr.next
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.next
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('b') then
    >    -- insert sibling before
    >    local old = curr.prev
    >    curr.prev = new_id()
    >    local new = zettels[curr.prev]
    >    new.data = ''
    >    new.prev = old
    >    new.next = current_zettel_id
    >    if old then
    >      zettels[old].next = curr.prev
    >      assert(curr.parent == zettels[old].parent, 'siblings should have same parent')
    >    end
    >    new.parent = curr.parent
    >    current_zettel_id = curr.prev
    >    render(window) -- recompute render_state
    >    editz(window)
    >  elseif key == string.byte('c') then
    >    -- insert child
    >    local old = curr.child
    >    curr.child = new_id()
    >    local new = zettels[curr.child]
    >    new.data = ''
    >    new.next = old
    >    if old then
    >      assert(zettels[old].prev == nil, "first child shouldn't have a previous sibling")
    >      zettels[old].prev = curr.child
    >    end
    >    new.parent = current_zettel_id
    >    current_zettel_id = curr.child
    >    render(window) -- recompute render_state
    >    editz(window)
    >  -- cross-links
    >  elseif key == string.byte('s') then
    >    -- save zettel to a stash
    >    stash = current_zettel_id
    >  elseif key == string.byte('t') then
    >    -- cross-link a zettel bidirectionally with what's on the stash
    >    local insert_crosslink =
    >      function(a, rel, b_id)
    >        if a.crosslinks == nil then
    >          a.crosslinks = {}
    >        end
    >        a.crosslinks[rel] = b_id
    >      end
    >    insert_crosslink(curr, 'a', stash)
    >    insert_crosslink(zettels[stash], 'a', current_zettel_id)
    >    stash = nil
    >  -- view settings
    >  elseif key == string.byte('x') then
    >    if view_settings.width > 5 then
    >      view_settings.width = view_settings.width - 5
    >    end
    >  elseif key == string.byte('X') then
    >    if view_settings.width < w-5 then
    >      view_settings.width = view_settings.width + 5
    >    end
    >  elseif key == string.byte('y') then
    >    if view_settings.height > 0 then
    >      view_settings.height = view_settings.height - 1
    >    end
    >  elseif key == string.byte('Y') then
    >    if view_settings.height < h-2 then
    >      view_settings.height = view_settings.height + 1
    >    end
    >  elseif key == string.byte('z') then
    >    -- scroll to show the current zettel at top of screen
    >    -- often has the effect of zooming in on its hierarchy
    >    view_settings.first_zettel = current_zettel_id
    >  end
    >end
- __teliva_timestamp:
    >Sat Feb 12 17:58:01 2022
  __teliva_note:
    >make cursor movements less risky using a back button '<'
  render_state:
    >-- some information about what's been drawn on screen
    >-- not saved between app restarts
    >render_state = {
    >  -- where the current zettel is, in units of zettels
    >  curr_h = 1,
    >  curr_w = 1,
    >  -- what zettel is at each position on screen, in units of zettels
    >  hw2id = {},
    >  -- list of zettels currently displayed
    >  displayed = {},
    >  -- history of screen render state
    >  history = {},  -- elems {first_zettel=view_settings.first_zettel, cursor=current_zettel_id}
    >}
- __teliva_timestamp:
    >Thu Feb 17 20:15:14 2022
  doc:blurb:
    >A rudimentary Zettelkasten app trying to hew very close to the original analog setup, as described by abramdemski:
    >
    >https://www.lesswrong.com/posts/NfdHG6oHBJ8Qxc26s/the-zettelkasten-method-1
    >
    >The key attributes of Zettelkasten seem to be:
    >- notes organized in small fragments called 'cards' that can't hold much text
    >- a tree-based organization using sibling and child cards, with the ability to insert children and siblings to any card, any time
    >- ability to cross-link any card to any other, turning the tree into a graph (but still with a strong sense of hierarchy)
    >
    >zet.tlv satisfies these properties, but isn't very intuitive or usable yet. Contributions appreciated.