all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob a21587acbe0c053141ff20665c8cf957e0bdde98 236519 bytes (raw)
name: lisp/org-table.el 	 # note: path name is non-authoritative(*)

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
 
;;; org-table.el --- The Table Editor for Org        -*- lexical-binding: t; -*-

;; Copyright (C) 2004-2019 Free Software Foundation, Inc.

;; Author: Carsten Dominik <carsten at orgmode dot org>
;; Keywords: outlines, hypermedia, calendar, wp
;; Homepage: https://orgmode.org
;;
;; This file is part of GNU Emacs.
;;
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:

;; This file contains the table editor and spreadsheet for Org mode.

;; Watch out:  Here we are talking about two different kind of tables.
;; Most of the code is for the tables created with the Org mode table editor.
;; Sometimes, we talk about tables created and edited with the table.el
;; Emacs package.  We call the former org-type tables, and the latter
;; table.el-type tables.

;;; Code:

(require 'cl-lib)
(require 'org-macs)
(require 'org-compat)
(require 'org-keys)

(declare-function calc-eval "calc" (str &optional separator &rest args))
(declare-function org-at-timestamp-p "org" (&optional extended))
(declare-function org-delete-backward-char "org" (N))
(declare-function org-element-at-point "org-element" ())
(declare-function org-element-contents "org-element" (element))
(declare-function org-element-extract-element "org-element" (element))
(declare-function org-element-interpret-data "org-element" (data))
(declare-function org-element-lineage "org-element" (blob &optional types with-self))
(declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion with-affiliated))
(declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
(declare-function org-element-property "org-element" (property element))
(declare-function org-element-type "org-element" (element))
(declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
(declare-function org-export-create-backend "ox" (&rest rest) t)
(declare-function org-export-data-with-backend "ox" (data backend info))
(declare-function org-export-filter-apply-functions "ox" (filters value info))
(declare-function org-export-first-sibling-p "ox" (blob info))
(declare-function org-export-get-backend "ox" (name))
(declare-function org-export-get-environment "ox" (&optional backend subtreep ext-plist))
(declare-function org-export-install-filters "ox" (info))
(declare-function org-export-table-has-special-column-p "ox" (table))
(declare-function org-export-table-row-is-special-p "ox" (table-row info))
(declare-function org-id-find "org-id" (id &optional markerp))
(declare-function org-indent-line "org" ())
(declare-function org-load-modules-maybe "org" (&optional force))
(declare-function org-restart-font-lock "org" ())
(declare-function org-sort-remove-invisible "org" (s))
(declare-function org-time-stamp-format "org" (&optional long inactive))
(declare-function org-time-string-to-absolute "org" (s &optional daynr prefer buffer pos))
(declare-function org-time-string-to-time "org" (s))
(declare-function org-timestamp-up-day "org" (&optional arg))

(defvar constants-unit-system)
(defvar org-M-RET-may-split-line)
(defvar org-element-use-cache)
(defvar org-export-filters-alist)
(defvar org-finish-function)
(defvar org-inhibit-highlight-removal)
(defvar org-inhibit-startup)
(defvar org-selected-window)
(defvar org-self-insert-cluster-for-undo)
(defvar org-self-insert-command-undo-counter)
(defvar org-ts-regexp)
(defvar org-ts-regexp-both)
(defvar org-ts-regexp-inactive)
(defvar org-ts-regexp3)
(defvar org-window-configuration)
(defvar sort-fold-case)

\f
;;; Customizables

(defgroup org-table nil
  "Options concerning tables in Org mode."
  :tag "Org Table"
  :group 'org)

(defcustom orgtbl-optimized t
  "Non-nil means use the optimized table editor version for `orgtbl-mode'.

In the optimized version, the table editor takes over all simple keys that
normally just insert a character.  In tables, the characters are inserted
in a way to minimize disturbing the table structure (i.e. in overwrite mode
for empty fields).  Outside tables, the correct binding of the keys is
restored.

Changing this variable requires a restart of Emacs to become
effective."
  :group 'org-table
  :type 'boolean)

(defcustom orgtbl-radio-table-templates
  '((latex-mode "% BEGIN RECEIVE ORGTBL %n
% END RECEIVE ORGTBL %n
\\begin{comment}
#+ORGTBL: SEND %n orgtbl-to-latex :splice nil :skip 0
| | |
\\end{comment}\n")
    (texinfo-mode "@c BEGIN RECEIVE ORGTBL %n
@c END RECEIVE ORGTBL %n
@ignore
#+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
| | |
@end ignore\n")
    (html-mode "<!-- BEGIN RECEIVE ORGTBL %n -->
<!-- END RECEIVE ORGTBL %n -->
<!--
#+ORGTBL: SEND %n orgtbl-to-html :splice nil :skip 0
| | |
-->\n")
    (org-mode "#+ BEGIN RECEIVE ORGTBL %n
#+ END RECEIVE ORGTBL %n

#+ORGTBL: SEND %n orgtbl-to-orgtbl :splice nil :skip 0
| | |
"))
  "Templates for radio tables in different major modes.
Each template must define lines that will be treated as a comment and that
must contain the \"BEGIN RECEIVE ORGTBL %n\" and \"END RECEIVE ORGTBL\"
lines where \"%n\" will be replaced with the name of the table during
insertion of the template.  The transformed table will later be inserted
between these lines.

The template should also contain a minimal table in a multiline comment.
If multiline comments are not possible in the buffer language,
you can pack it into a string that will not be used when the code
is compiled or executed.  Above the table will you need a line with
the fixed string \"#+ORGTBL: SEND\", followed by instruction on how to
convert the table into a data structure useful in the
language of the buffer.  Check the manual for the section on
\"Translator functions\", and more generally check out
the Info node `(org)Tables in arbitrary syntax'.

All occurrences of %n in a template will be replaced with the name of the
table, obtained by prompting the user."
  :group 'org-table
  :type '(repeat
	  (list (symbol :tag "Major mode")
		(string :tag "Format"))))

(defgroup org-table-settings nil
  "Settings for tables in Org mode."
  :tag "Org Table Settings"
  :group 'org-table)

(defcustom org-table-default-size "5x2"
  "The default size for newly created tables, Columns x Rows."
  :group 'org-table-settings
  :type 'string)

(defcustom org-table-number-regexp
  "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%:]*\\|[<>]?[-+]?0[xX][[:xdigit:].]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$"
  "Regular expression for recognizing numbers in table columns.
If a table column contains mostly numbers, it will be aligned to the
right.  If not, it will be aligned to the left.

The default value of this option is a regular expression which allows
anything which looks remotely like a number as used in scientific
context.  For example, all of the following will be considered a
number:
    12    12.2    2.4e-08    2x10^12    4.034+-0.02    2.7(10)  >3.5

Other options offered by the customize interface are more restrictive."
  :group 'org-table-settings
  :type '(choice
	  (const :tag "Positive Integers"
		 "^[0-9]+$")
	  (const :tag "Integers"
		 "^[-+]?[0-9]+$")
	  (const :tag "Floating Point Numbers"
		 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
	  (const :tag "Floating Point Number or Integer"
		 "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
	  (const :tag "Exponential, Floating point, Integer"
		 "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
	  (const :tag "Very General Number-Like, including hex and Calc radix"
		 "^\\([<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*\\|[<>]?[-+]?0[xX][[:xdigit:].]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$")
	  (const :tag "Very General Number-Like, including hex and Calc radix, allows comma as decimal mark"
		 "^\\([<>]?[-+^.,0-9]*[0-9][-+^.0-9eEdDx()%]*\\|[<>]?[-+]?0[xX][[:xdigit:].]+\\|[<>]?[-+]?[0-9]+#[0-9a-zA-Z.]+\\|nan\\|[-+u]?inf\\)$")
	  (string :tag "Regexp:")))

(defcustom org-table-number-fraction 0.5
  "Fraction of numbers in a column required to make the column align right.
In a column all non-white fields are considered.  If at least
this fraction of fields is matched by `org-table-number-regexp',
alignment to the right border applies."
  :group 'org-table-settings
  :type 'number)

(defcustom org-table-formula-field-format "%s"
  "Format for fields which contain the result of a formula.
For example, using \"~%s~\" will display the result within tilde
characters.  Beware that modifying the display can prevent the
field from being used in another formula."
  :group 'org-table-settings
  :version "24.1"
  :type 'string)

(defgroup org-table-editing nil
  "Behavior of tables during editing in Org mode."
  :tag "Org Table Editing"
  :group 'org-table)

(defcustom org-table-automatic-realign t
  "Non-nil means automatically re-align table when pressing TAB or RETURN.
When nil, aligning is only done with `\\[org-table-align]', or after column
removal/insertion."
  :group 'org-table-editing
  :type 'boolean)

(defcustom org-table-auto-blank-field t
  "Non-nil means automatically blank table field when starting to type into it.
This only happens when typing immediately after a field motion
command (TAB, S-TAB or RET)."
  :group 'org-table-editing
  :type 'boolean)

(defcustom org-table-exit-follow-field-mode-when-leaving-table t
  "Non-nil means automatically exit the follow mode.
When nil, the follow mode will stay on and be active in any table
the cursor enters.  Since the table follow filed mode messes with the
window configuration, it is not recommended to set this variable to nil,
except maybe locally in a special file that has mostly tables with long
fields."
  :group 'org-table
  :version "24.1"
  :type 'boolean)

(defcustom org-table-fix-formulas-confirm nil
  "Whether the user should confirm when Org fixes formulas."
  :group 'org-table-editing
  :version "24.1"
  :type '(choice
	  (const :tag "with yes-or-no" yes-or-no-p)
	  (const :tag "with y-or-n" y-or-n-p)
	  (const :tag "no confirmation" nil)))

(defcustom org-table-tab-jumps-over-hlines t
  "Non-nil means tab in the last column of a table with jump over a hline.
If a horizontal separator line is following the current line,
`org-table-next-field' can either create a new row before that line, or jump
over the line.  When this option is nil, a new line will be created before
this line."
  :group 'org-table-editing
  :type 'boolean)

(defcustom org-table-shrunk-column-indicator "…"
  "String to be displayed in a shrunk column."
  :group 'org-table-editing
  :type 'string
  :package-version '(Org . "9.2")
  :safe (lambda (v) (and (stringp v) (not (equal v "")))))

(defgroup org-table-calculation nil
  "Options concerning tables in Org mode."
  :tag "Org Table Calculation"
  :group 'org-table)

(defcustom org-table-use-standard-references 'from
  "Non-nil means using table references like B3 instead of @3$2.
Possible values are:
nil     never use them
from    accept as input, do not present for editing
t       accept as input and present for editing"
  :group 'org-table-calculation
  :type '(choice
	  (const :tag "Never, don't even check user input for them" nil)
	  (const :tag "Always, both as user input, and when editing" t)
	  (const :tag "Convert user input, don't offer during editing" from)))

(defcustom org-table-copy-increment t
  "Non-nil means increment when copying current field with \
`\\[org-table-copy-down]'."
  :group 'org-table-calculation
  :version "26.1"
  :package-version '(Org . "8.3")
  :type '(choice
	  (const :tag "Use the difference between the current and the above fields" t)
	  (integer :tag "Use a number" 1)
	  (const :tag "Don't increment the value when copying a field" nil)))

(defcustom org-calc-default-modes
  '(calc-internal-prec 12
		       calc-float-format  (float 8)
		       calc-angle-mode    deg
		       calc-prefer-frac   nil
		       calc-symbolic-mode nil
		       calc-date-format (YYYY "-" MM "-" DD " " Www (" " hh ":" mm))
		       calc-display-working-message t)
  "List with Calc mode settings for use in `calc-eval' for table formulas.
The list must contain alternating symbols (Calc modes variables and values).
Don't remove any of the default settings, just change the values.  Org mode
relies on the variables to be present in the list."
  :group 'org-table-calculation
  :type 'plist)

(defcustom org-table-duration-custom-format 'hours
  "Format for the output of calc computations like $1+$2;t.
The default value is `hours', and will output the results as a
number of hours.  Other allowed values are `seconds', `minutes' and
`days', and the output will be a fraction of seconds, minutes or
days. `hh:mm' selects to use hours and minutes, ignoring seconds.
The `U' flag in a table formula will select this specific format for
a single formula."
  :group 'org-table-calculation
  :version "24.1"
  :type '(choice (symbol :tag "Seconds" 'seconds)
		 (symbol :tag "Minutes" 'minutes)
		 (symbol :tag "Hours  " 'hours)
		 (symbol :tag "Days   " 'days)
		 (symbol :tag "HH:MM  " 'hh:mm)))

(defcustom org-table-duration-hour-zero-padding t
  "Non-nil means hours in table duration computations should be zero-padded.
So this is about 08:32:34 versus 8:33:34."
  :group 'org-table-calculation
  :version "26.1"
  :package-version '(Org . "9.1")
  :type 'boolean
  :safe #'booleanp)

(defcustom org-table-formula-evaluate-inline t
  "Non-nil means TAB and RET evaluate a formula in current table field.
If the current field starts with an equal sign, it is assumed to be a formula
which should be evaluated as described in the manual and in the documentation
string of the command `org-table-eval-formula'.  This feature requires the
Emacs calc package.
When this variable is nil, formula calculation is only available through
the command `\\[org-table-eval-formula]'."
  :group 'org-table-calculation
  :type 'boolean)

(defcustom org-table-formula-use-constants t
  "Non-nil means interpret constants in formulas in tables.
A constant looks like `$c' or `$Grav' and will be replaced before evaluation
by the value given in `org-table-formula-constants', or by a value obtained
from the `constants.el' package."
  :group 'org-table-calculation
  :type 'boolean)

(defcustom org-table-formula-constants nil
  "Alist with constant names and values, for use in table formulas.
The car of each element is a name of a constant, without the `$' before it.
The cdr is the value as a string.  For example, if you'd like to use the
speed of light in a formula, you would configure

  (setq org-table-formula-constants \\='((\"c\" . \"299792458.\")))

and then use it in an equation like `$1*$c'.

Constants can also be defined on a per-file basis using a line like

#+CONSTANTS: c=299792458. pi=3.14 eps=2.4e-6"
  :group 'org-table-calculation
  :type '(repeat
	  (cons (string :tag "name")
		(string :tag "value"))))

(defcustom org-table-allow-automatic-line-recalculation t
  "Non-nil means lines marked with |#| or |*| will be recomputed automatically.
\\<org-mode-map>\
Automatically means when `TAB' or `RET' or `\\[org-ctrl-c-ctrl-c]' \
are pressed in the line."
  :group 'org-table-calculation
  :type 'boolean)

(defcustom org-table-relative-ref-may-cross-hline t
  "Non-nil means relative formula references may cross hlines.
Here are the allowed values:

nil    Relative references may not cross hlines.  They will reference the
       field next to the hline instead.  Coming from below, the reference
       will be to the field below the hline.  Coming from above, it will be
       to the field above.
t      Relative references may cross hlines.
error  An attempt to cross a hline will throw an error.

It is probably good to never set this variable to nil, for the sake of
portability of tables."
  :group 'org-table-calculation
  :type '(choice
	  (const :tag "Allow to cross" t)
	  (const :tag "Stick to hline" nil)
	  (const :tag "Error on attempt to cross" error)))

(defcustom org-table-formula-create-columns nil
  "Non-nil means evaluation of formula can add new columns.
When non-nil, evaluating an out-of-bounds field can insert as
many columns as needed.  When set to `warn', issue a warning when
doing so.  When set to `prompt', ask user before creating a new
column.  Otherwise, throw an error."
  :group 'org-table-calculation
  :package-version '(Org . "8.3")
  :type '(choice
	  (const :tag "Out-of-bounds field generates an error (default)" nil)
	  (const :tag "Out-of-bounds field silently adds columns as needed" t)
	  (const :tag "Out-of-bounds field adds columns, but issues a warning" warn)
	  (const :tag "Prompt user when setting an out-of-bounds field" prompt)))

(defgroup org-table-import-export nil
  "Options concerning table import and export in Org mode."
  :tag "Org Table Import Export"
  :group 'org-table)

(defcustom org-table-export-default-format "orgtbl-to-tsv"
  "Default export parameters for `org-table-export'.
These can be overridden for a specific table by setting the
TABLE_EXPORT_FORMAT property.  See the manual section on orgtbl
radio tables for the different export transformations and
available parameters."
  :group 'org-table-import-export
  :type 'string)

(defcustom org-table-convert-region-max-lines 999
  "Max lines that `org-table-convert-region' will attempt to process.

The function can be slow on larger regions; this safety feature
prevents it from hanging Emacs."
  :group 'org-table-import-export
  :type 'integer
  :package-version '(Org . "8.3"))

\f
;;; Regexps Constants

(defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  "Detect an org-type or table-type table.")

(defconst org-table-line-regexp "^[ \t]*|"
  "Detect an org-type table line.")

(defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  "Detect an org-type table line.")

(defconst org-table-hline-regexp "^[ \t]*|-"
  "Detect an org-type table hline.")

(defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  "Detect a table-type table hline.")

(defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  "Detect the first line outside a table when searching from within it.
This works for both table types.")

(defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  "Detect a #+TBLFM line.")

(defvar org-table-TBLFM-begin-regexp "^[ \t]*|.*\n[ \t]*#\\+TBLFM: ")

(defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  "Regexp matching a line marked for automatic recalculation.")

(defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  "Regexp matching a line marked for recalculation.")

(defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  "Regexp matching a line marked for calculation.")

(defconst org-table-border-regexp "^[ \t]*[^| \t]"
  "Regexp matching any line outside an Org table.")

(defconst org-table-range-regexp
  "@\\([-+]?I*[-+]?[0-9]*\\)\\(\\$[-+]?[0-9]+\\)?\\(\\.\\.@?\\([-+]?I*[-+]?[0-9]*\\)\\(\\$[-+]?[0-9]+\\)?\\)?"
  ;;   1                        2                    3          4                        5
  "Regular expression for matching ranges in formulas.")

(defconst org-table-range-regexp2
  (concat
   "\\(" "@[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)"
   "\\.\\."
   "\\(" "@?[-0-9I$&]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\|" "\\$[a-zA-Z0-9]+" "\\)")
  "Match a range for reference display.")

(defconst org-table-translate-regexp
  (concat "\\(" "@[-0-9I$]+" "\\|" "[a-zA-Z]\\{1,2\\}\\([0-9]+\\|&\\)" "\\)")
  "Match a reference that needs translation, for reference display.")

(defconst org-table-separator-space
  (propertize " " 'display '(space :relative-width 1))
  "Space used around fields when aligning the table.
This space serves as a segment separator for the purposes of the
bidirectional reordering.")

\f
;;; Internal Variables

(defvar org-table-last-highlighted-reference nil)

(defvar org-table-formula-history nil)

(defvar org-field-marker nil)
(defvar org-table-buffer-is-an nil)

(defvar-local org-table-formula-constants-local nil
  "Local version of `org-table-formula-constants'.")

(defvar org-table-may-need-update t
  "Indicates that a table might need an update.
This variable is set by `org-before-change-function'.
`org-table-align' sets it back to nil.")

(defvar orgtbl-after-send-table-hook nil
  "Hook for functions attaching to `C-c C-c', if the table is sent.
This can be used to add additional functionality after the table is sent
to the receiver position, otherwise, if table is not sent, the functions
are not run.")

(defvar org-table-column-names nil
  "Alist with column names, derived from the `!' line.
This variable is initialized with `org-table-analyze'.")

(defvar org-table-column-name-regexp nil
  "Regular expression matching the current column names.
This variable is initialized with `org-table-analyze'.")

(defvar org-table-local-parameters nil
  "Alist with parameter names, derived from the `$' line.
This variable is initialized with `org-table-analyze'.")

(defvar org-table-named-field-locations nil
  "Alist with locations of named fields.
Associations follow the pattern (NAME LINE COLUMN) where
  NAME is the name of the field as a string,
  LINE is the number of lines from the beginning of the table,
  COLUMN is the column of the field, as an integer.
This variable is initialized with `org-table-analyze'.")

(defvar org-table-current-line-types nil
  "Table row types in current table.
This variable is initialized with `org-table-analyze'.")

(defvar org-table-current-begin-pos nil
  "Current table begin position, as a marker.
This variable is initialized with `org-table-analyze'.")

(defvar org-table-current-ncol nil
  "Number of columns in current table.
This variable is initialized with `org-table-analyze'.")

(defvar org-table-dlines nil
  "Vector of data line line numbers in the current table.
Line numbers are counted from the beginning of the table.  This
variable is initialized with `org-table-analyze'.")

(defvar org-table-hlines nil
  "Vector of hline line numbers in the current table.
Line numbers are counted from the beginning of the table.  This
variable is initialized with `org-table-analyze'.")

(defvar org-table-aligned-begin-marker (make-marker)
  "Marker at the beginning of the table last aligned.
Used to check if cursor still is in that table, to minimize realignment.")

(defvar org-table-aligned-end-marker (make-marker)
  "Marker at the end of the table last aligned.
Used to check if cursor still is in that table, to minimize realignment.")

(defvar org-table-last-alignment nil
  "List of flags for flushright alignment, from the last re-alignment.
This is being used to correctly align a single field after TAB or RET.")

(defvar org-table-last-column-widths nil
  "List of max width of fields in each column.
This is being used to correctly align a single field after TAB or RET.")

(defvar-local org-table-formula-debug nil
  "Non-nil means debug table formulas.
When nil, simply write \"#ERROR\" in corrupted fields.")

(defvar-local org-table-overlay-coordinates nil
  "Overlay coordinates after each align of a table.")

(defvar org-last-recalc-line nil)

(defvar org-show-positions nil)

(defvar org-table-rectangle-overlays nil)

(defvar org-table-clip nil
  "Clipboard for table regions.")

(defvar org-timecnt nil)

(defvar org-recalc-commands nil
  "List of commands triggering the recalculation of a line.
Will be filled automatically during use.")

(defvar org-recalc-marks
  '((" " . "Unmarked: no special line, no automatic recalculation")
    ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
    ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
    ("!" . "Column name definition line.  Reference in formula as $name.")
    ("$" . "Parameter definition line name=value.  Reference in formula as $name.")
    ("_" . "Names for values in row below this one.")
    ("^" . "Names for values in row above this one.")))

(defvar org-tbl-calc-modes nil)

(defvar org-pos nil)

\f
;;; Macros and Inlined Functions

(defmacro org-table-with-shrunk-columns (&rest body)
  "Expand all columns before executing BODY, then shrink them again."
  (declare (debug (body)))
  (org-with-gensyms (shrunk-columns begin end)
    `(let ((,begin (copy-marker (org-table-begin)))
	   (,end (copy-marker (org-table-end) t))
	   (,shrunk-columns (org-table--list-shrunk-columns)))
       (org-with-point-at ,begin (org-table-expand ,begin ,end))
       (unwind-protect
	   (progn ,@body)
	 (org-table--shrink-columns ,shrunk-columns ,begin ,end)
	 (set-marker ,begin nil)
	 (set-marker ,end nil)))))

(defmacro org-table-with-shrunk-field (&rest body)
  "Save field shrunk state, execute BODY and restore state."
  (declare (debug (body)))
  (org-with-gensyms (end shrunk size)
    `(let* ((,shrunk (save-match-data (org-table--shrunk-field)))
	    (,end (and ,shrunk (copy-marker (overlay-end ,shrunk) t)))
	    (,size (and ,shrunk (- ,end (overlay-start ,shrunk)))))
       (when ,shrunk (delete-overlay ,shrunk))
       (unwind-protect (progn ,@body)
	 (when ,shrunk (move-overlay ,shrunk (- ,end ,size) ,end))))))

(defmacro org-table-save-field (&rest body)
  "Save current field; execute BODY; restore field.
Field is restored even in case of abnormal exit."
  (declare (debug (body)))
  (org-with-gensyms (line column)
    `(let ((,line (copy-marker (line-beginning-position)))
	   (,column (org-table-current-column)))
       (unwind-protect
	   (progn ,@body)
	 (goto-char ,line)
	 (org-table-goto-column ,column)
	 (set-marker ,line nil)))))

(defsubst org-table--set-calc-mode (var &optional value)
  (if (stringp var)
      (setq var (assoc var '(("D" calc-angle-mode deg)
			     ("R" calc-angle-mode rad)
			     ("F" calc-prefer-frac t)
			     ("S" calc-symbolic-mode t)))
	    value (nth 2 var) var (nth 1 var)))
  (if (memq var org-tbl-calc-modes)
      (setcar (cdr (memq var org-tbl-calc-modes)) value)
    (cons var (cons value org-tbl-calc-modes)))
  org-tbl-calc-modes)

\f
;;; Predicates

(defun org-at-TBLFM-p (&optional pos)
  "Non-nil when point (or POS) is in #+TBLFM line."
  (save-excursion
    (goto-char (or pos (point)))
    (beginning-of-line)
    (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
	 (eq (org-element-type (org-element-at-point)) 'table))))

(defun org-at-table-p (&optional table-type)
  "Non-nil if the cursor is inside an Org table.
If TABLE-TYPE is non-nil, also check for table.el-type tables."
  (and (org-match-line (if table-type "[ \t]*[|+]" "[ \t]*|"))
       (or (not (derived-mode-p 'org-mode))
	   (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
	     (and e (or table-type
			(eq 'org (org-element-property :type e))))))))

(defun org-at-table.el-p ()
  "Non-nil when point is at a table.el table."
  (and (org-match-line "[ \t]*[|+]")
       (let ((element (org-element-at-point)))
	 (and (eq (org-element-type element) 'table)
	      (eq (org-element-property :type element) 'table.el)))))

(defun org-at-table-hline-p ()
  "Non-nil when point is inside a hline in a table.
Assume point is already in a table."
  (org-match-line org-table-hline-regexp))

(defun org-table-check-inside-data-field (&optional noerror assume-table)
  "Non-nil when point is inside a table data field.
Raise an error otherwise, unless NOERROR is non-nil.  In that
case, return nil if point is not inside a data field.  When
optional argument ASSUME-TABLE is non-nil, assume point is within
a table."
  (cond ((and (or assume-table (org-at-table-p))
	      (not (save-excursion (skip-chars-backward " \t") (bolp)))
	      (not (org-at-table-hline-p))
	      (not (looking-at-p "[ \t]*$"))))
	(noerror nil)
	(t (user-error "Not in table data field"))))

\f
;;; Create, Import, and Convert Tables

;;;###autoload
(defun org-table-create-with-table.el ()
  "Use the table.el package to insert a new table.
If there is already a table at point, convert between Org tables
and table.el tables."
  (interactive)
  (require 'table)
  (cond
   ((and (org-at-table.el-p)
	 (y-or-n-p "Convert table to Org table? "))
    (org-table-convert))
   ((and (org-at-table-p)
	 (y-or-n-p "Convert table to table.el table? "))
    (org-table-align)
    (org-table-convert))
   (t (call-interactively 'table-insert))))

;;;###autoload
(defun org-table-create-or-convert-from-region (arg)
  "Convert region to table, or create an empty table.
If there is an active region, convert it to a table, using the function
`org-table-convert-region'.  See the documentation of that function
to learn how the prefix argument is interpreted to determine the field
separator.
If there is no such region, create an empty table with `org-table-create'."
  (interactive "P")
  (if (org-region-active-p)
      (org-table-convert-region (region-beginning) (region-end) arg)
    (org-table-create arg)))

;;;###autoload
(defun org-table-create (&optional size)
  "Query for a size and insert a table skeleton.
SIZE is a string Columns x Rows like for example \"3x2\"."
  (interactive "P")
  (unless size
    (setq size (read-string
		(concat "Table size Columns x Rows [e.g. "
			org-table-default-size "]: ")
		"" nil org-table-default-size)))

  (let* ((pos (point))
	 (indent (make-string (current-column) ?\ ))
	 (split (org-split-string size " *x *"))
	 (rows (string-to-number (nth 1 split)))
	 (columns (string-to-number (car split)))
	 (line (concat (apply 'concat indent "|" (make-list columns "  |"))
		       "\n")))
    (if (string-match "^[ \t]*$" (buffer-substring-no-properties
				  (point-at-bol) (point)))
	(beginning-of-line 1)
      (newline))
    ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
    (dotimes (_ rows) (insert line))
    (goto-char pos)
    (when (> rows 1)
      ;; Insert a hline after the first row.
      (end-of-line 1)
      (insert "\n|-")
      (goto-char pos))
    (org-table-align)))

;;;###autoload
(defun org-table-convert-region (beg0 end0 &optional separator)
  "Convert region to a table.

The region goes from BEG0 to END0, but these borders will be moved
slightly, to make sure a beginning of line in the first line is included.

SEPARATOR specifies the field separator in the lines.  It can have the
following values:

(4)     Use the comma as a field separator
(16)    Use a TAB as field separator
(64)    Prompt for a regular expression as field separator
integer  When a number, use that many spaces, or a TAB, as field separator
regexp   When a regular expression, use it to match the separator
nil      When nil, the command tries to be smart and figure out the
         separator in the following way:
         - when each line contains a TAB, assume TAB-separated material
         - when each line contains a comma, assume CSV material
         - else, assume one or more SPACE characters as separator."
  (interactive "r\nP")
  (let* ((beg (min beg0 end0))
	 (end (max beg0 end0))
	 re)
    (if (> (count-lines beg end) org-table-convert-region-max-lines)
	(user-error "Region is longer than `org-table-convert-region-max-lines' (%s) lines; not converting"
		    org-table-convert-region-max-lines)
      (when (equal separator '(64))
	(setq separator (read-regexp "Regexp for field separator")))
      (goto-char beg)
      (beginning-of-line 1)
      (setq beg (point-marker))
      (goto-char end)
      (if (bolp) (backward-char 1) (end-of-line 1))
      (setq end (point-marker))
      ;; Get the right field separator
      (unless separator
	(goto-char beg)
	(setq separator
	      (cond
	       ((not (re-search-forward "^[^\n\t]+$" end t)) '(16))
	       ((not (re-search-forward "^[^\n,]+$" end t)) '(4))
	       (t 1))))
      (goto-char beg)
      (if (equal separator '(4))
	  (while (< (point) end)
	    ;; parse the csv stuff
	    (cond
	     ((looking-at "^") (insert "| "))
	     ((looking-at "[ \t]*$") (replace-match " |") (beginning-of-line 2))
	     ((looking-at "[ \t]*\"\\([^\"\n]*\\)\"")
	      (replace-match "\\1")
	      (if (looking-at "\"") (insert "\"")))
	     ((looking-at "[^,\n]+") (goto-char (match-end 0)))
	     ((looking-at "[ \t]*,") (replace-match " | "))
	     (t (beginning-of-line 2))))
	(setq re (cond
		  ((equal separator '(4)) "^\\|\"?[ \t]*,[ \t]*\"?")
		  ((equal separator '(16)) "^\\|\t")
		  ((integerp separator)
		   (if (< separator 1)
		       (user-error "Number of spaces in separator must be >= 1")
		     (format "^ *\\| *\t *\\| \\{%d,\\}" separator)))
		  ((stringp separator)
		   (format "^ *\\|%s" separator))
		  (t (error "This should not happen"))))
	(while (re-search-forward re end t)
	  (replace-match "| " t t)))
      (goto-char beg)
      (org-table-align))))

;;;###autoload
(defun org-table-import (file separator)
  "Import FILE as a table.

The command tries to be smart and figure out the separator in the
following way:

  - when each line contains a TAB, assume TAB-separated material
  - when each line contains a comma, assume CSV material
  - else, assume one or more SPACE characters as separator.

When non-nil, SEPARATOR specifies the field separator in the
lines.  It can have the following values:

(4)     Use the comma as a field separator
(16)    Use a TAB as field separator
(64)    Prompt for a regular expression as field separator
integer When a number, use that many spaces, or a TAB, as field separator
regexp  When a regular expression, use it to match the separator."
  (interactive "f\nP")
  (unless (bolp) (insert "\n"))
  (let ((beg (point))
	(pm (point-max)))
    (insert-file-contents file)
    (org-table-convert-region beg (+ (point) (- (point-max) pm)) separator)))

(defun org-table-convert ()
  "Convert from Org table to table.el and back.
Obviously, this only works within limits.  When an Org table is converted
to table.el, all horizontal separator lines get lost, because table.el uses
these as cell boundaries and has no notion of horizontal lines.  A table.el
table can be converted to an Org table only if it does not do row or column
spanning.  Multiline cells will become multiple cells.  Beware, Org mode
does not test if the table can be successfully converted - it blindly
applies a recipe that works for simple tables."
  (interactive)
  (require 'table)
  (if (org-at-table.el-p)
      ;; convert to Org table
      (let ((beg (copy-marker (org-table-begin t)))
	    (end (copy-marker (org-table-end t))))
	(table-unrecognize-region beg end)
	(goto-char beg)
	(while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
	  (replace-match ""))
	(goto-char beg))
    (if (org-at-table-p)
	;; convert to table.el table
	(let ((beg (copy-marker (org-table-begin)))
	      (end (copy-marker (org-table-end))))
	  ;; first, get rid of all horizontal lines
	  (goto-char beg)
	  (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
	    (replace-match ""))
	  ;; insert a hline before first
	  (goto-char beg)
	  (org-table-insert-hline 'above)
	  (beginning-of-line -1)
	  ;; insert a hline after each line
	  (while (progn (beginning-of-line 3) (< (point) end))
	    (org-table-insert-hline))
	  (goto-char beg)
	  (setq end (move-marker end (org-table-end)))
	  ;; replace "+" at beginning and ending of hlines
	  (while (re-search-forward "^\\([ \t]*\\)|-" end t)
	    (replace-match "\\1+-"))
	  (goto-char beg)
	  (while (re-search-forward "-|[ \t]*$" end t)
	    (replace-match "-+"))
	  (goto-char beg)))))

\f
;;; Navigation and Structure Editing

;;;###autoload
(defun org-table-begin (&optional table-type)
  "Find the beginning of the table and return its position.
With a non-nil optional argument TABLE-TYPE, return the beginning
of a table.el-type table.  This function assumes point is on
a table."
  (cond (table-type
	 (org-element-property :post-affiliated (org-element-at-point)))
	((save-excursion
	   (and (re-search-backward org-table-border-regexp nil t)
		(line-beginning-position 2))))
	(t (point-min))))

;;;###autoload
(defun org-table-end (&optional table-type)
  "Find the end of the table and return its position.
With a non-nil optional argument TABLE-TYPE, return the end of
a table.el-type table.  This function assumes point is on
a table."
  (save-excursion
    (cond (table-type
	   (goto-char (org-element-property :end (org-element-at-point)))
	   (skip-chars-backward " \t\n")
	   (line-beginning-position 2))
	  ((re-search-forward org-table-border-regexp nil t)
	   (match-beginning 0))
	  ;; When the line right after the table is the last line in
	  ;; the buffer with trailing spaces but no final newline
	  ;; character, be sure to catch the correct ending at its
	  ;; beginning.  In any other case, ending is expected to be
	  ;; at point max.
	  (t (goto-char (point-max))
	     (skip-chars-backward " \t")
	     (if (bolp) (point) (line-end-position))))))

;;;###autoload
(defun org-table-next-field ()
  "Go to the next field in the current table, creating new lines as needed.
Before doing so, re-align the table if necessary."
  (interactive)
  (org-table-maybe-eval-formula)
  (org-table-maybe-recalculate-line)
  (when (and org-table-automatic-realign
	     org-table-may-need-update)
    (org-table-align))
  (let ((end (org-table-end)))
    (if (org-at-table-hline-p)
	(end-of-line 1))
    (condition-case nil
	(progn
	  (re-search-forward "|" end)
	  (if (looking-at "[ \t]*$")
	      (re-search-forward "|" end))
	  (if (and (looking-at "-")
		   org-table-tab-jumps-over-hlines
		   (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
	      (goto-char (match-beginning 1)))
	  (if (looking-at "-")
	      (progn
		(beginning-of-line 0)
		(org-table-insert-row 'below))
	    (if (looking-at " ") (forward-char 1))))
      (error
       (org-table-insert-row 'below)))))

;;;###autoload
(defun org-table-previous-field ()
  "Go to the previous field in the table.
Before doing so, re-align the table if necessary."
  (interactive)
  (org-table-justify-field-maybe)
  (org-table-maybe-recalculate-line)
  (when (and org-table-automatic-realign
	     org-table-may-need-update)
    (org-table-align))
  (when (org-at-table-hline-p)
    (end-of-line))
  (let ((start (org-table-begin))
	(origin (point)))
    (condition-case nil
	(progn
	  (search-backward "|" start nil 2)
	  (while (looking-at-p "|\\(?:-\\|[ \t]*$\\)")
	    (search-backward "|" start)))
      (error
       (goto-char origin)
       (user-error "Cannot move to previous table field"))))
  (when (looking-at "| ?")
    (goto-char (match-end 0))))

(defun org-table-beginning-of-field (&optional n)
  "Move to the beginning of the current table field.
If already at or before the beginning, move to the beginning of the
previous field.
With numeric argument N, move N-1 fields backward first."
  (interactive "p")
  (let ((pos (point)))
    (while (> n 1)
      (setq n (1- n))
      (org-table-previous-field))
    (if (not (re-search-backward "|" (point-at-bol 0) t))
	(user-error "No more table fields before the current")
      (goto-char (match-end 0))
      (and (looking-at " ") (forward-char 1)))
    (when (>= (point) pos) (org-table-beginning-of-field 2))))

(defun org-table-end-of-field (&optional n)
  "Move to the end of the current table field.
If already at or after the end, move to the end of the next table field.
With numeric argument N, move N-1 fields forward first."
  (interactive "p")
  (let ((pos (point)))
    (while (> n 1)
      (setq n (1- n))
      (org-table-next-field))
    (when (re-search-forward "|" (point-at-eol 1) t)
      (backward-char 1)
      (skip-chars-backward " ")
      (when (and (equal (char-before (point)) ?|) (equal (char-after (point)) ?\s))
	(forward-char 1)))
    (when (<= (point) pos) (org-table-end-of-field 2))))

;;;###autoload
(defun org-table-next-row ()
  "Go to the next row (same column) in the current table.
Before doing so, re-align the table if necessary."
  (interactive)
  (org-table-maybe-eval-formula)
  (org-table-maybe-recalculate-line)
  (if (and org-table-automatic-realign
	   org-table-may-need-update)
      (org-table-align))
  (let ((col (org-table-current-column)))
    (beginning-of-line 2)
    (unless (bolp) (insert "\n"))	;missing newline at eob
    (when (or (not (org-at-table-p))
	      (org-at-table-hline-p))
      (beginning-of-line 0)
      (org-table-insert-row 'below))
    (org-table-goto-column col)
    (skip-chars-backward "^|\n\r")
    (when (looking-at " ") (forward-char))))

(defun org-table-get (line column)
  "Get the field in table line LINE, column COLUMN.
If LINE is larger than the number of data lines in the table, the function
returns nil.  However, if COLUMN is too large, we will simply return an
empty string.
If LINE is nil, use the current line.
If COLUMN is nil, use the current column."
  (setq column (or column (org-table-current-column)))
  (save-excursion
    (and (or (not line) (org-table-goto-line line))
	 (org-trim (org-table-get-field column)))))

(defun org-table-put (line column value &optional align)
  "Put VALUE into line LINE, column COLUMN.
When ALIGN is set, also realign the table."
  (setq column (or column (org-table-current-column)))
  (prog1 (save-excursion
	   (and (or (not line) (org-table-goto-line line))
		(progn (org-table-goto-column column nil 'force) t)
		(org-table-get-field column value)))
    (and align (org-table-align))))

(defun org-table-current-line ()
  "Return the index of the current data line."
  (let ((pos (point)) (end (org-table-end)) (cnt 0))
    (save-excursion
      (goto-char (org-table-begin))
      (while (and (re-search-forward org-table-dataline-regexp end t)
		  (setq cnt (1+ cnt))
		  (< (point-at-eol) pos))))
    cnt))

(defun org-table-current-column ()
  "Return current column number."
  (interactive)
  (save-excursion
    (let ((pos (point)))
      (beginning-of-line)
      (if (not (search-forward "|" pos t)) 0
	(let ((column 1)
	      (separator (if (org-at-table-hline-p) "[+|]" "|")))
	  (while (re-search-forward separator pos t) (cl-incf column))
	  column)))))

(defun org-table-current-dline ()
  "Find out what table data line we are in.
Only data lines count for this."
  (save-excursion
    (let ((c 0)
	  (pos (line-beginning-position)))
      (goto-char (org-table-begin))
      (while (<= (point) pos)
	(when (looking-at org-table-dataline-regexp) (cl-incf c))
	(forward-line))
      c)))

(defun org-table-goto-line (N)
  "Go to the Nth data line in the current table.
Return t when the line exists, nil if it does not exist."
  (goto-char (org-table-begin))
  (let ((end (org-table-end)) (cnt 0))
    (while (and (re-search-forward org-table-dataline-regexp end t)
		(< (setq cnt (1+ cnt)) N)))
    (= cnt N)))

;;;###autoload
(defun org-table-blank-field ()
  "Blank the current table field or active region."
  (interactive)
  (org-table-check-inside-data-field)
  (if (and (called-interactively-p 'any) (org-region-active-p))
      (let (org-table-clip)
	(org-table-cut-region (region-beginning) (region-end)))
    (skip-chars-backward "^|")
    (backward-char 1)
    (if (looking-at "|[^|\n]+")
	(let* ((pos (match-beginning 0))
	       (match (match-string 0))
	       (len (org-string-width match)))
	  (replace-match (concat "|" (make-string (1- len) ?\ )))
	  (goto-char (+ 2 pos))
	  (substring match 1)))))

(defun org-table-get-field (&optional n replace)
  "Return the value of the field in column N of current row.
N defaults to current column.  If REPLACE is a string, replace
field with this value.  The return value is always the old
value."
  (when n (org-table-goto-column n))
  (skip-chars-backward "^|\n")
  (if (or (bolp) (looking-at-p "[ \t]*$"))
      ;; Before first column or after last one.
      ""
    (looking-at "[^|\r\n]*")
    (let* ((pos (match-beginning 0))
	   (val (buffer-substring pos (match-end 0))))
      (when replace
	(org-table-with-shrunk-field
	 (replace-match (if (equal replace "") " " replace) t t)))
      (goto-char (min (line-end-position) (1+ pos)))
      val)))

;;;###autoload
(defun org-table-field-info (_arg)
  "Show info about the current field, and highlight any reference at point."
  (interactive "P")
  (unless (org-at-table-p) (user-error "Not at a table"))
  (org-table-analyze)
  (save-excursion
    (let* ((pos (point))
	   (col (org-table-current-column))
	   (cname (car (rassoc (int-to-string col) org-table-column-names)))
	   (name (car (rassoc (list (count-lines org-table-current-begin-pos
						 (line-beginning-position))
				    col)
			      org-table-named-field-locations)))
	   (eql (org-table-expand-lhs-ranges
		 (mapcar
		  (lambda (e)
		    (cons (org-table-formula-handle-first/last-rc (car e))
			  (cdr e)))
		  (org-table-get-stored-formulas))))
	   (dline (org-table-current-dline))
	   (ref (format "@%d$%d" dline col))
	   (ref1 (org-table-convert-refs-to-an ref))
	   ;; Prioritize field formulas over column formulas.
	   (fequation (or (assoc name eql) (assoc ref eql)))
	   (cequation (assoc (format "$%d" col) eql))
	   (eqn (or fequation cequation)))
      (let ((p (and eqn (get-text-property 0 :orig-eqn (car eqn)))))
	(when p (setq eqn p)))
      (goto-char pos)
      (ignore-errors (org-table-show-reference 'local))
      (message "line @%d, col $%s%s, ref @%d$%d or %s%s%s"
	       dline col
	       (if cname (concat " or $" cname) "")
	       dline col ref1
	       (if name (concat " or $" name) "")
	       ;; FIXME: formula info not correct if special table line
	       (if eqn
		   (concat ", formula: "
			   (org-table-formula-to-user
			    (concat
			     (if (or (string-prefix-p "$" (car eqn))
				     (string-prefix-p "@" (car eqn)))
				 ""
			       "$")
			     (car eqn) "=" (cdr eqn))))
		 "")))))

(defun org-table-goto-field (ref &optional create-column-p)
  "Move point to a specific field in the current table.

REF is either the name of a field its absolute reference, as
a string.  No column is created unless CREATE-COLUMN-P is
non-nil.  If it is a function, it is called with the column
number as its argument as is used as a predicate to know if the
column can be created.

This function assumes the table is already analyzed (i.e., using
`org-table-analyze')."
  (let* ((coordinates
	  (cond
	   ((cdr (assoc ref org-table-named-field-locations)))
	   ((string-match "\\`@\\([1-9][0-9]*\\)\\$\\([1-9][0-9]*\\)\\'" ref)
	    (list (condition-case nil
		      (aref org-table-dlines
			    (string-to-number (match-string 1 ref)))
		    (error (user-error "Invalid row number in %s" ref)))
		  (string-to-number (match-string 2 ref))))
	   (t (user-error "Unknown field: %s" ref))))
	 (line (car coordinates))
	 (column (nth 1 coordinates))
	 (create-new-column (if (functionp create-column-p)
				(funcall create-column-p column)
			      create-column-p)))
    (when coordinates
      (goto-char org-table-current-begin-pos)
      (forward-line line)
      (org-table-goto-column column nil create-new-column))))

;;;###autoload
(defun org-table-goto-column (n &optional on-delim force)
  "Move the cursor to the Nth column in the current table line.
With optional argument ON-DELIM, stop with point before the left delimiter
of the field.
If there are less than N fields, just go to after the last delimiter.
However, when FORCE is non-nil, create new columns if necessary."
  (interactive "p")
  (beginning-of-line 1)
  (when (> n 0)
    (while (and (> (setq n (1- n)) -1)
		(or (search-forward "|" (point-at-eol) t)
		    (and force
			 (progn (end-of-line 1)
				(skip-chars-backward "^|")
				(insert " | ")
				t)))))
    (when (and force (not (looking-at ".*|")))
      (save-excursion (end-of-line 1) (insert " | ")))
    (if on-delim
	(backward-char 1)
      (if (looking-at " ") (forward-char 1)))))

;;;###autoload
(defun org-table-insert-column ()
  "Insert a new column into the table."
  (interactive)
  (unless (org-at-table-p) (user-error "Not at a table"))
  (org-table-find-dataline)
  (let ((col (max 1 (org-table-current-column)))
	(beg (org-table-begin))
	(end (copy-marker (org-table-end)))
	(shrunk-columns (org-table--list-shrunk-columns)))
    (org-table-expand beg end)
    (save-excursion
      (goto-char beg)
      (while (< (point) end)
	(unless (org-at-table-hline-p)
	  (org-table-goto-column col t)
	  (unless (search-forward "|" (line-end-position) t 2)
	    ;; Add missing vertical bar at the end of the row.
	    (end-of-line)
	    (insert "|"))
	  (insert "  |"))
	(forward-line)))
    (org-table-goto-column (1+ col))
    (org-table-align)
    ;; Shift appropriately stored shrunk column numbers, then hide the
    ;; columns again.
    (org-table--shrink-columns (mapcar (lambda (c) (if (<= c col) c (1+ c)))
				       shrunk-columns)
			       beg end)
    (set-marker end nil)
    ;; Fix TBLFM formulas, if desirable.
    (when (or (not org-table-fix-formulas-confirm)
	      (funcall org-table-fix-formulas-confirm "Fix formulas? "))
      (org-table-fix-formulas "$" nil (1- col) 1)
      (org-table-fix-formulas "$LR" nil (1- col) 1))))

(defun org-table-find-dataline ()
  "Find a data line in the current table, which is needed for column commands.
This function assumes point is in a table.  Raise an error when
there is no data row below."
  (or (not (org-at-table-hline-p))
      (let ((col (current-column))
	    (end (org-table-end)))
	(forward-line)
	(while (and (< (point) end) (org-at-table-hline-p))
	  (forward-line))
	(when (>= (point) end)
	  (user-error "Cannot find data row for column operation"))
	(org-move-to-column col)
	t)))

(defun org-table-line-to-dline (line &optional above)
  "Turn a buffer line number into a data line number.

If there is no data line in this line, return nil.

If there is no matching dline (most likely the reference was
a hline), the first dline below it is used.  When ABOVE is
non-nil, the one above is used."
  (let ((min 1)
	(max (1- (length org-table-dlines))))
    (cond ((or (> (aref org-table-dlines min) line)
	       (< (aref org-table-dlines max) line))
	   nil)
	  ((= line (aref org-table-dlines max)) max)
	  (t (catch 'exit
	       (while (> (- max min) 1)
		 (let* ((mean (/ (+ max min) 2))
			(v (aref org-table-dlines mean)))
		   (cond ((= v line) (throw 'exit mean))
			 ((> v line) (setq max mean))
			 (t (setq min mean)))))
	       (cond ((= line (aref org-table-dlines max)) max)
		     ((= line (aref org-table-dlines min)) min)
		     (above min)
		     (t max)))))))

(defun org-table--swap-cells (row1 col1 row2 col2)
  "Swap two cells indicated by the coordinates provided.
ROW1, COL1, ROW2, COL2 are integers indicating the row/column
position of the two cells that will be swapped in the table."
  (let ((content1 (org-table-get row1 col1))
	(content2 (org-table-get row2 col2)))
    (org-table-put row1 col1 content2)
    (org-table-put row2 col2 content1)))

(defun org-table--move-cell (direction)
  "Move the current cell in a cardinal direction.
DIRECTION is a symbol among `up', `down', `left', and `right'.
The contents the current cell are swapped with cell in the
indicated direction.  Raise an error if the move cannot be done."
  (let ((row-shift (pcase direction (`up -1) (`down 1) (_ 0)))
	(column-shift (pcase direction (`left -1) (`right 1) (_ 0))))
    (when (and (= 0 row-shift) (= 0 column-shift))
      (error "Invalid direction: %S" direction))
    ;; Initialize `org-table-current-ncol' and `org-table-dlines'.
    (org-table-analyze)
    (let* ((row (org-table-current-line))
	   (column (org-table-current-column))
	   (target-row (+ row row-shift))
	   (target-column (+ column column-shift))
	   (org-table-current-nrow (1- (length org-table-dlines))))
      (when (or (< target-column 1)
		(< target-row 1)
		(> target-column org-table-current-ncol)
		(> target-row org-table-current-nrow))
	(user-error "Cannot move cell further"))
      (org-table--swap-cells row column target-row target-column)
      (org-table-goto-line target-row)
      (org-table-goto-column target-column))))

;;;###autoload
(defun org-table-move-cell-up ()
  "Move a single cell up in a table.
Swap with anything in target cell."
  (interactive)
  (unless (org-table-check-inside-data-field)
    (error "No table at point"))
  (org-table--move-cell 'up)
  (org-table-align))

;;;###autoload
(defun org-table-move-cell-down ()
  "Move a single cell down in a table.
Swap with anything in target cell."
  (interactive)
  (unless (org-table-check-inside-data-field)
    (error "No table at point"))
  (org-table--move-cell 'down)
  (org-table-align))

;;;###autoload
(defun org-table-move-cell-left ()
  "Move a single cell left in a table.
Swap with anything in target cell."
  (interactive)
  (unless (org-table-check-inside-data-field)
    (error "No table at point"))
  (org-table--move-cell 'left)
  (org-table-align))

;;;###autoload
(defun org-table-move-cell-right ()
  "Move a single cell right in a table.
Swap with anything in target cell."
  (interactive)
  (unless (org-table-check-inside-data-field)
    (error "No table at point"))
  (org-table--move-cell 'right)
  (org-table-align))

;;;###autoload
(defun org-table-delete-column ()
  "Delete a column from the table."
  (interactive)
  (unless (org-at-table-p) (user-error "Not at a table"))
  (org-table-find-dataline)
  (org-table-check-inside-data-field nil t)
  (let* ((col (org-table-current-column))
	 (beg (org-table-begin))
	 (end (copy-marker (org-table-end)))
	 (shrunk-columns (remq col (org-table--list-shrunk-columns))))
    (org-table-expand beg end)
    (org-table-save-field
     (goto-char beg)
     (while (< (point) end)
       (if (org-at-table-hline-p)
	   nil
	 (org-table-goto-column col t)
	 (and (looking-at "|[^|\n]+|")
	      (replace-match "|")))
       (forward-line)))
    (org-table-goto-column (max 1 (1- col)))
    (org-table-align)
    ;; Shift appropriately stored shrunk column numbers, then hide the
    ;; columns again.
    (org-table--shrink-columns (mapcar (lambda (c) (if (< c col) c (1- c)))
				       shrunk-columns)
			       beg end)
    (set-marker end nil)
    ;; Fix TBLFM formulas, if desirable.
    (when (or (not org-table-fix-formulas-confirm)
	      (funcall org-table-fix-formulas-confirm "Fix formulas? "))
      (org-table-fix-formulas
       "$" (list (cons (number-to-string col) "INVALID")) col -1 col)
      (org-table-fix-formulas
       "$LR" (list (cons (number-to-string col) "INVALID")) col -1 col))))

;;;###autoload
(defun org-table-move-column-right ()
  "Move column to the right."
  (interactive)
  (org-table-move-column nil))

;;;###autoload
(defun org-table-move-column-left ()
  "Move column to the left."
  (interactive)
  (org-table-move-column 'left))

;;;###autoload
(defun org-table-move-column (&optional left)
  "Move the current column to the right.  With arg LEFT, move to the left."
  (interactive "P")
  (unless (org-at-table-p) (user-error "Not at a table"))
  (org-table-find-dataline)
  (org-table-check-inside-data-field nil t)
  (let* ((col (org-table-current-column))
	 (col1 (if left (1- col) col))
	 (colpos (if left (1- col) (1+ col)))
	 (beg (org-table-begin))
	 (end (copy-marker (org-table-end))))
    (when (and left (= col 1))
      (user-error "Cannot move column further left"))
    (when (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
      (user-error "Cannot move column further right"))
    (let ((shrunk-columns (org-table--list-shrunk-columns)))
      (org-table-expand beg end)
      (org-table-save-field
       (goto-char beg)
       (while (< (point) end)
	 (unless (org-at-table-hline-p)
	   (org-table-goto-column col1 t)
	   (when (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
	     (transpose-regions
	      (match-beginning 1) (match-end 1)
	      (match-beginning 2) (match-end 2))))
	 (forward-line)))
      (org-table-goto-column colpos)
      (org-table-align)
      ;; Shift appropriately stored shrunk column numbers, then shrink
      ;; the columns again.
      (org-table--shrink-columns
       (mapcar (lambda (c)
		 (cond ((and (= col c) left) (1- c))
		       ((= col c) (1+ c))
		       ((and (= col (1+ c)) left) (1+ c))
		       ((and (= col (1- c)) (not left) (1- c)))
		       (t c)))
	       shrunk-columns)
       beg end)
      (set-marker end nil)
      ;; Fix TBLFM formulas, if desirable.
      (when (or (not org-table-fix-formulas-confirm)
		(funcall org-table-fix-formulas-confirm "Fix formulas? "))
	(org-table-fix-formulas
	 "$" (list (cons (number-to-string col) (number-to-string colpos))
		   (cons (number-to-string colpos) (number-to-string col))))
	(org-table-fix-formulas
	 "$LR" (list
		(cons (number-to-string col) (number-to-string colpos))
		(cons (number-to-string colpos) (number-to-string col))))))))

;;;###autoload
(defun org-table-move-row-down ()
  "Move table row down."
  (interactive)
  (org-table-move-row nil))

;;;###autoload
(defun org-table-move-row-up ()
  "Move table row up."
  (interactive)
  (org-table-move-row 'up))

;;;###autoload
(defun org-table-move-row (&optional up)
  "Move the current table line down.  With arg UP, move it up."
  (interactive "P")
  (let* ((col (current-column))
	 (pos (point))
	 (hline1p (save-excursion (beginning-of-line 1)
				  (looking-at org-table-hline-regexp)))
	 (dline1 (org-table-current-dline))
	 (dline2 (+ dline1 (if up -1 1)))
	 (tonew (if up 0 2))
	 hline2p)
    (when (and up (= (point-min) (line-beginning-position)))
      (user-error "Cannot move row further"))
    (beginning-of-line tonew)
    (when (or (and (not up) (eobp)) (not (org-at-table-p)))
      (goto-char pos)
      (user-error "Cannot move row further"))
    (org-table-with-shrunk-columns
     (setq hline2p (looking-at org-table-hline-regexp))
     (goto-char pos)
     (let ((row (delete-and-extract-region (line-beginning-position)
					   (line-beginning-position 2))))
       (beginning-of-line tonew)
       (unless (bolp) (insert "\n"))	;at eob without a newline
       (insert row)
       (unless (bolp) (insert "\n"))	;missing final newline in ROW
       (beginning-of-line 0)
       (org-move-to-column col)
       (unless (or hline1p hline2p
		   (not (or (not org-table-fix-formulas-confirm)
			    (funcall org-table-fix-formulas-confirm
				     "Fix formulas? "))))
	 (org-table-fix-formulas
	  "@" (list
	       (cons (number-to-string dline1) (number-to-string dline2))
	       (cons (number-to-string dline2) (number-to-string dline1)))))))))

;;;###autoload
(defun org-table-insert-row (&optional arg)
  "Insert a new row above the current line into the table.
With prefix ARG, insert below the current line."
  (interactive "P")
  (unless (org-at-table-p) (user-error "Not at a table"))
  (org-table-with-shrunk-columns
   (let* ((line (buffer-substring (line-beginning-position) (line-end-position)))
	  (new (org-table-clean-line line)))
     ;; Fix the first field if necessary
     (when (string-match "^[ \t]*| *[#*$] *|" line)
       (setq new (replace-match (match-string 0 line) t t new)))
     (beginning-of-line (if arg 2 1))
     ;; Buffer may not end of a newline character, so ensure
     ;; (beginning-of-line 2) moves point to a new line.
     (unless (bolp) (insert "\n"))
     (let (org-table-may-need-update) (insert-before-markers new "\n"))
     (beginning-of-line 0)
     (re-search-forward "| ?" (line-end-position) t)
     (when (or org-table-may-need-update org-table-overlay-coordinates)
       (org-table-align))
     (when (or (not org-table-fix-formulas-confirm)
	       (funcall org-table-fix-formulas-confirm "Fix formulas? "))
       (org-table-fix-formulas "@" nil (1- (org-table-current-dline)) 1)))))

;;;###autoload
(defun org-table-insert-hline (&optional above)
  "Insert a horizontal-line below the current line into the table.
With prefix ABOVE, insert above the current line."
  (interactive "P")
  (unless (org-at-table-p) (user-error "Not at a table"))
  (when (eobp) (save-excursion (insert "\n")))
  (unless (string-match-p "|[ \t]*$" (org-current-line-string))
    (org-table-align))
  (org-table-with-shrunk-columns
   (let ((line (org-table-clean-line
		(buffer-substring (point-at-bol) (point-at-eol))))
	 (col (current-column)))
     (while (string-match "|\\( +\\)|" line)
       (setq line (replace-match
		   (concat "+" (make-string (- (match-end 1) (match-beginning 1))
					    ?-) "|") t t line)))
     (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
     (beginning-of-line (if above 1 2))
     (insert line "\n")
     (beginning-of-line (if above 1 -1))
     (org-move-to-column col)
     (when org-table-overlay-coordinates (org-table-align)))))

;;;###autoload
(defun org-table-hline-and-move (&optional same-column)
  "Insert a hline and move to the row below that line."
  (interactive "P")
  (let ((col (org-table-current-column)))
    (org-table-maybe-eval-formula)
    (org-table-maybe-recalculate-line)
    (org-table-insert-hline)
    (end-of-line 2)
    (if (looking-at "\n[ \t]*|-")
	(progn (insert "\n|") (org-table-align))
      (org-table-next-field))
    (if same-column (org-table-goto-column col))))

(defun org-table-clean-line (s)
  "Convert a table line S into a string with only \"|\" and space.
In particular, this does handle wide and invisible characters."
  (if (string-match "^[ \t]*|-" s)
      ;; It's a hline, just map the characters
      (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
    (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
      (setq s (replace-match
	       (concat "|" (make-string (org-string-width (match-string 1 s))
					?\ ) "|")
	       t t s)))
    s))

;;;###autoload
(defun org-table-kill-row ()
  "Delete the current row or horizontal line from the table."
  (interactive)
  (unless (org-at-table-p) (user-error "Not at a table"))
  (let ((col (current-column))
	(dline (and (not (org-match-line org-table-hline-regexp))
		    (org-table-current-dline))))
    (org-table-with-shrunk-columns
     (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
     (if (not (org-at-table-p)) (beginning-of-line 0))
     (org-move-to-column col)
     (when (and dline
		(or (not org-table-fix-formulas-confirm)
		    (funcall org-table-fix-formulas-confirm "Fix formulas? ")))
       (org-table-fix-formulas
	"@" (list (cons (number-to-string dline) "INVALID")) dline -1 dline)))))

;;;###autoload
(defun org-table-cut-region (beg end)
  "Copy region in table to the clipboard and blank all relevant fields.
If there is no active region, use just the field at point."
  (interactive (list
		(if (org-region-active-p) (region-beginning) (point))
		(if (org-region-active-p) (region-end) (point))))
  (org-table-copy-region beg end 'cut))

(defun org-table--increment-field (field previous)
  "Increment string FIELD according to PREVIOUS field.

Increment FIELD only if it is a string representing a number, per
Emacs Lisp syntax, a timestamp, or is either prefixed or suffixed
with a number.  In any other case, return FIELD as-is.

If PREVIOUS has the same structure as FIELD, e.g.,
a number-prefixed string with the same pattern, the increment
step is the difference between numbers (or timestamps, measured
in days) in PREVIOUS and FIELD.  Otherwise, it uses
`org-table-copy-increment', if the variable contains a number, or
default to 1.

The function assumes `org-table-copy-increment' is non-nil."
  (let* ((default-step (if (numberp org-table-copy-increment)
			   org-table-copy-increment
			 1))
	 (number-regexp			;Lisp read syntax for numbers
	  (rx (and string-start
		   (opt (any "+-"))
		   (or (and (one-or-more digit) (opt "."))
		       (and (zero-or-more digit) "." (one-or-more digit)))
		   (opt (any "eE") (opt (opt (any "+-")) (one-or-more digit)))
		   string-end)))
	 (number-prefix-regexp (rx (and string-start (one-or-more digit))))
	 (number-suffix-regexp (rx (and (one-or-more digit) string-end)))
	 (analyze
	  (lambda (field)
	    ;; Analyse string FIELD and return information related to
	    ;; increment or nil.  When non-nil, return value has the
	    ;; following scheme: (TYPE VALUE PATTERN) where
	    ;; - TYPE is a symbol among `number', `prefix', `suffix'
	    ;;   and `timestamp',
	    ;; - VALUE is a timestamp if TYPE is `timestamp', or
	    ;;   a number otherwise,
	    ;; - PATTERN is the field without its prefix, or suffix if
	    ;;   TYPE is either `prefix' or `suffix' , or nil
	    ;;   otherwise.
	    (cond ((not (org-string-nw-p field)) nil)
		  ((string-match-p number-regexp field)
		   (list 'number
			 (string-to-number field)
			 nil))
		  ((string-match number-prefix-regexp field)
		   (list 'prefix
			 (string-to-number (match-string 0 field))
			 (substring field (match-end 0))))
		  ((string-match number-suffix-regexp field)
		   (list 'suffix
			 (string-to-number (match-string 0 field))
			 (substring field 0 (match-beginning 0))))
		  ((string-match-p org-ts-regexp3 field)
		   (list 'timestamp field nil))
		  (t nil))))
	 (next-number-string
	  (lambda (n1 &optional n2)
	    ;; Increment number N1 and return it as a string.  If N2
	    ;; is also a number, deduce increment step from the
	    ;; difference between N1 and N2.  Otherwise, increment
	    ;; step is `default-step'.
	    (number-to-string (if n2 (+ n1 (- n1 n2)) (+ n1 default-step)))))
	 (shift-timestamp
	  (lambda (t1 &optional t2)
	    ;; Increment timestamp T1 and return it.  If T2 is also
	    ;; a timestamp, deduce increment step from the difference,
	    ;; in days, between T1 and T2.  Otherwise, increment by
	    ;; `default-step' days.
	    (with-temp-buffer
	      (insert t1)
	      (org-timestamp-up-day (if (not t2) default-step
				      (- (org-time-string-to-absolute t1)
					 (org-time-string-to-absolute t2))))
	      (buffer-string)))))
    ;; Check if both PREVIOUS and FIELD have the same type.  Also, if
    ;; the case of prefixed or suffixed numbers, make sure their
    ;; pattern, i.e., the part of the string without the prefix or the
    ;; suffix, is the same.
    (pcase (cons (funcall analyze field) (funcall analyze previous))
      (`((number ,n1 ,_) . (number ,n2 ,_))
       (funcall next-number-string n1 n2))
      (`((number ,n ,_) . ,_)
       (funcall next-number-string n))
      (`((prefix ,n1 ,p1) . (prefix ,n2 ,p2))
       (concat (funcall next-number-string n1 (and (equal p1 p2) n2)) p1))
      (`((prefix ,n ,p) . ,_)
       (concat (funcall next-number-string n) p))
      (`((suffix ,n1 ,p1) . (suffix ,n2 ,p2))
       (concat p1 (funcall next-number-string n1 (and (equal p1 p2) n2))))
      (`((suffix ,n ,p) . ,_)
       (concat p (funcall next-number-string n)))
      (`((timestamp ,t1 ,_) . (timestamp ,t2 ,_))
       (funcall shift-timestamp t1 t2))
      (`((timestamp ,t1 ,_) . ,_)
       (funcall shift-timestamp t1))
      (_ field))))

;;;###autoload
(defun org-table-copy-down (n)
  "Copy the value of the current field one row below.

If the field at the cursor is empty, copy the content of the
nearest non-empty field above.  With argument N, use the Nth
non-empty field.

If the current field is not empty, it is copied down to the next
row, and the cursor is moved with it.  Therefore, repeating this
command causes the column to be filled row-by-row.

If the variable `org-table-copy-increment' is non-nil and the
field is a number, a timestamp, or is either prefixed or suffixed
with a number, it will be incremented while copying.  By default,
increment by the difference between the value in the current
field and the one in the field above, if any.  To increment using
a fixed integer, set `org-table-copy-increment' to a number.  In
the case of a timestamp, increment by days.

However, when N is 0, do not increment the field at all."
  (interactive "p")
  (org-table-check-inside-data-field)
  (let* ((beg (org-table-begin))
	 (column (org-table-current-column))
	 (initial-field (save-excursion
			  (let ((f (org-string-nw-p (org-table-get-field))))
			    (and f (org-trim f)))))
	 field field-above next-field)
    (save-excursion
      ;; Get reference field.
      (if initial-field (setq field initial-field)
	(beginning-of-line)
	(setq field
	      (catch :exit
		(while (re-search-backward org-table-dataline-regexp beg t)
		  (let ((f (org-string-nw-p (org-table-get-field column))))
		    (cond ((and (> n 1) f) (cl-decf n))
			  (f (throw :exit (org-trim f)))
			  (t nil))
		    (beginning-of-line)))
		(user-error "No non-empty field found"))))
      ;; Check if increment is appropriate, and how it should be done.
      (when (and org-table-copy-increment (/= n 0))
	;; If increment step is not explicit, get non-empty field just
	;; above the field being incremented to guess it.
	(unless (numberp org-table-copy-increment)
	  (setq field-above
		(let ((f (unless (= beg (line-beginning-position))
			   (forward-line -1)
			   (not (org-at-table-hline-p))
			   (org-table-get-field column))))
		  (and (org-string-nw-p f)
		       (org-trim f)))))
	;; Compute next field.
	(setq next-field (org-table--increment-field field field-above))))
    ;; Since initial field in not empty, we modify row below instead.
    ;; Skip alignment since we do it at the end of the process anyway.
    (when initial-field
      (let ((org-table-may-need-update nil)) (org-table-next-row))
      (org-table-blank-field))
    ;; Insert the new field.  NEW-FIELD may be nil if
    ;; `org-table-increment' is nil, or N = 0.  In that case, copy
    ;; FIELD.
    (insert (or next-field field))
    (org-table-maybe-recalculate-line)
    (org-table-align)))

;;;###autoload
(defun org-table-copy-region (beg end &optional cut)
  "Copy rectangular region in table to clipboard.
A special clipboard is used which can only be accessed with
`org-table-paste-rectangle'.  Return the region copied, as a list
of lists of fields."
  (interactive (list
		(if (org-region-active-p) (region-beginning) (point))
		(if (org-region-active-p) (region-end) (point))
		current-prefix-arg))
  (goto-char (min beg end))
  (org-table-check-inside-data-field)
  (let ((beg (line-beginning-position))
	(c01 (org-table-current-column))
	region)
    (goto-char (max beg end))
    (org-table-check-inside-data-field nil t)
    (let* ((end (copy-marker (line-end-position)))
	   (c02 (org-table-current-column))
	   (column-start (min c01 c02))
	   (column-end (max c01 c02))
	   (column-number (1+ (- column-end column-start)))
	   (rpl (and cut "  ")))
      (goto-char beg)
      (while (< (point) end)
	(unless (org-at-table-hline-p)
	  ;; Collect every cell between COLUMN-START and COLUMN-END.
	  (let (cols)
	    (dotimes (c column-number)
	      (push (org-table-get-field (+ c column-start) rpl) cols))
	    (push (nreverse cols) region)))
	(forward-line))
      (set-marker end nil))
    (when cut (org-table-align))
    (message (substitute-command-keys "Cells in the region copied, use \
\\[org-table-paste-rectangle] to paste them in a table."))
    (setq org-table-clip (nreverse region))))

;;;###autoload
(defun org-table-paste-rectangle ()
  "Paste a rectangular region into a table.
The upper right corner ends up in the current field.  All involved fields
will be overwritten.  If the rectangle does not fit into the present table,
the table is enlarged as needed.  The process ignores horizontal separator
lines."
  (interactive)
  (unless (consp org-table-clip)
    (user-error "First cut/copy a region to paste!"))
  (org-table-check-inside-data-field)
  (let* ((column (org-table-current-column))
	 (org-table-automatic-realign nil))
    (org-table-save-field
     (dolist (row org-table-clip)
       (while (org-at-table-hline-p) (forward-line))
       ;; If we left the table, create a new row.
       (when (and (bolp) (not (looking-at "[ \t]*|")))
	 (end-of-line 0)
	 (org-table-next-field))
       (let ((c column))
	 (dolist (field row)
	   (org-table-goto-column c nil 'force)
	   (org-table-get-field nil field)
	   (cl-incf c)))
       (forward-line)))
    (org-table-align)))

\f
;;; Follow Field minor mode

(define-minor-mode org-table-follow-field-mode
  "Minor mode to make the table field editor window follow the cursor.
When this mode is active, the field editor window will always show the
current field.  The mode exits automatically when the cursor leaves the
table (but see `org-table-exit-follow-field-mode-when-leaving-table')."
  nil " TblFollow" nil
  (if org-table-follow-field-mode
      (add-hook 'post-command-hook 'org-table-follow-fields-with-editor
		'append 'local)
    (remove-hook 'post-command-hook 'org-table-follow-fields-with-editor 'local)
    (let* ((buf (get-buffer "*Org Table Edit Field*"))
	   (win (and buf (get-buffer-window buf))))
      (when win (delete-window win))
      (when buf
	(with-current-buffer buf
	  (move-marker org-field-marker nil))
	(kill-buffer buf)))))

;;;###autoload
(defun org-table-edit-field (arg)
  "Edit table field in a different window.
This is mainly useful for fields that contain hidden parts.

When called with a `\\[universal-argument]' prefix, just make the full field
visible so that it can be edited in place.

When called with a `\\[universal-argument] \\[universal-argument]' prefix, \
toggle `org-table-follow-field-mode'."
  (interactive "P")
  (unless (org-at-table-p) (user-error "Not at a table"))
  (cond
   ((equal arg '(16))
    (org-table-follow-field-mode (if org-table-follow-field-mode -1 1)))
   (arg
    (let ((b (save-excursion (skip-chars-backward "^|") (point)))
	  (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
      (remove-text-properties b e '(invisible t intangible t))
      (if (and (boundp 'font-lock-mode) font-lock-mode)
	  (font-lock-fontify-block))))
   (t
    (let ((pos (point-marker))
	  (coord
	   (if (eq org-table-use-standard-references t)
	       (concat (org-number-to-letters (org-table-current-column))
		       (int-to-string (org-table-current-dline)))
	     (concat "@" (int-to-string (org-table-current-dline))
		     "$" (int-to-string (org-table-current-column)))))
	  (field (org-table-get-field))
	  (cw (current-window-configuration))
	  p)
      (goto-char pos)
      (org-switch-to-buffer-other-window "*Org Table Edit Field*")
      (when (and (local-variable-p 'org-field-marker)
		 (markerp org-field-marker))
	(move-marker org-field-marker nil))
      (erase-buffer)
      (insert "#\n# Edit field " coord " and finish with C-c C-c\n#\n")
      (let ((org-inhibit-startup t)) (org-mode))
      (auto-fill-mode -1)
      (setq truncate-lines nil)
      (setq word-wrap t)
      (goto-char (setq p (point-max)))
      (insert (org-trim field))
      (remove-text-properties p (point-max) '(invisible t intangible t))
      (goto-char p)
      (setq-local org-finish-function 'org-table-finish-edit-field)
      (setq-local org-window-configuration cw)
      (setq-local org-field-marker pos)
      (message "Edit and finish with C-c C-c")))))

(defun org-table-follow-fields-with-editor ()
  (if (and org-table-exit-follow-field-mode-when-leaving-table
	   (not (org-at-table-p)))
      ;; We have left the table, exit the follow mode
      (org-table-follow-field-mode -1)
    (when (org-table-check-inside-data-field 'noerror)
      (let ((win (selected-window)))
	(org-table-edit-field nil)
	(org-fit-window-to-buffer)
	(select-window win)))))

(defun org-table-finish-edit-field ()
  "Finish editing a table data field.
Remove all newline characters, insert the result into the table, realign
the table and kill the editing buffer."
  (let ((pos org-field-marker)
	(cw org-window-configuration)
	(cb (current-buffer))
	text)
    (goto-char (point-min))
    (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
    (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
      (replace-match " "))
    (setq text (org-trim (buffer-string)))
    (set-window-configuration cw)
    (kill-buffer cb)
    (select-window (get-buffer-window (marker-buffer pos)))
    (goto-char pos)
    (move-marker pos nil)
    (org-table-check-inside-data-field)
    (org-table-get-field nil text)
    (org-table-align)
    (message "New field value inserted")))

\f
;;; Formulas

(defun org-table-current-field-formula (&optional key noerror)
  "Return the formula active for the current field.

Assumes that table is already analyzed.  If KEY is given, return
the key to this formula.  Otherwise return the formula preceded
with \"=\" or \":=\"."
  (let* ((line (count-lines org-table-current-begin-pos
			    (line-beginning-position)))
	 (row (org-table-line-to-dline line)))
    (cond
     (row
      (let* ((col (org-table-current-column))
	     (name (car (rassoc (list line col)
				org-table-named-field-locations)))
	     (scol (format "$%d" col))
	     (ref (format "@%d$%d" (org-table-current-dline) col))
	     (stored-list (org-table-get-stored-formulas noerror))
	     (ass (or (assoc name stored-list)
		      (assoc ref stored-list)
		      (assoc scol stored-list))))
	(cond (key (car ass))
	      (ass (concat (if (string-match-p "^[0-9]+$" (car ass)) "=" ":=")
			   (cdr ass))))))
     (noerror nil)
     (t (error "No formula active for the current field")))))

(defun org-table-get-formula (&optional equation named)
  "Read a formula from the minibuffer, offer stored formula as default.
When NAMED is non-nil, look for a named equation."
  (let* ((stored-list (org-table-get-stored-formulas))
	 (name (car (rassoc (list (count-lines org-table-current-begin-pos
					       (line-beginning-position))
				  (org-table-current-column))
			    org-table-named-field-locations)))
	 (ref (format "@%d$%d"
		      (org-table-current-dline)
		      (org-table-current-column)))
	 (scol (cond
		((not named) (format "$%d" (org-table-current-column)))
		((and name (not (string-match "\\`LR[0-9]+\\'" name))) name)
		(t ref)))
	 (name (or name ref))
	 (org-table-may-need-update nil)
	 (stored (cdr (assoc scol stored-list)))
	 (eq (cond
	      ((and stored equation (string-match-p "^ *=? *$" equation))
	       stored)
	      ((stringp equation) equation)
	      (t
	       (org-table-formula-from-user
		(read-string
		 (org-table-formula-to-user
		  (format "%s formula %s=" (if named "Field" "Column") scol))
		 (if stored (org-table-formula-to-user stored) "")
		 'org-table-formula-history)))))
	 mustsave)
    (unless (org-string-nw-p eq)
      ;; Remove formula.
      (setq stored-list (delq (assoc scol stored-list) stored-list))
      (org-table-store-formulas stored-list)
      (user-error "Formula removed"))
    (when (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
    (when (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
    (when (and name (not named))
      ;; We set the column equation, delete the named one.
      (setq stored-list (delq (assoc name stored-list) stored-list)
	    mustsave t))
    (if stored
	(setcdr (assoc scol stored-list) eq)
      (setq stored-list (cons (cons scol eq) stored-list)))
    (when (or mustsave (not (equal stored eq)))
      (org-table-store-formulas stored-list))
    eq))

(defun org-table-store-formulas (alist &optional location)
  "Store the list of formulas below the current table.
If optional argument LOCATION is a buffer position, insert it at
LOCATION instead."
  (save-excursion
    (if location
	(progn (goto-char location) (beginning-of-line))
      (goto-char (org-table-end)))
    (let ((case-fold-search t))
      (if (looking-at "\\([ \t]*\n\\)*[ \t]*\\(#\\+TBLFM:\\)\\(.*\n?\\)")
	  (progn
	    ;; Don't overwrite TBLFM, we might use text properties to
	    ;; store stuff.
	    (goto-char (match-beginning 3))
	    (delete-region (match-beginning 3) (match-end 0)))
	(org-indent-line)
	(insert (or (match-string 2) "#+TBLFM:")))
      (insert " "
	      (mapconcat (lambda (x) (concat (car x) "=" (cdr x)))
			 (sort alist #'org-table-formula-less-p)
			 "::")
	      "\n"))))

(defsubst org-table-formula-make-cmp-string (a)
  (when (string-match "\\`\\$[<>]" a)
    (let ((arrow (string-to-char (substring a 1))))
      ;; Fake a high number to make sure this is sorted at the end.
      (setq a (org-table-formula-handle-first/last-rc a))
      (setq a (format "$%d" (+ 10000
			       (if (= arrow ?<) -1000 0)
			       (string-to-number (substring a 1)))))))
  (when (string-match
	 "^\\(@\\([0-9]+\\)\\)?\\(\\$?\\([0-9]+\\)\\)?\\(\\$?[a-zA-Z0-9]+\\)?"
	 a)
    (concat
     (if (match-end 2)
	 (format "@%05d" (string-to-number (match-string 2 a))) "")
     (if (match-end 4)
	 (format "$%05d" (string-to-number (match-string 4 a))) "")
     (if (match-end 5)
	 (concat "@@" (match-string 5 a))))))

(defun org-table-formula-less-p (a b)
  "Compare two formulas for sorting."
  (let ((as (org-table-formula-make-cmp-string (car a)))
	(bs (org-table-formula-make-cmp-string (car b))))
    (and as bs (string< as bs))))

;;;###autoload
(defun org-table-get-stored-formulas (&optional noerror location)
  "Return an alist with the stored formulas directly after current table.
By default, only return active formulas, i.e., formulas located
on the first line after the table.  However, if optional argument
LOCATION is a buffer position, consider the formulas there."
  (save-excursion
    (if location
	(progn (goto-char location) (beginning-of-line))
      (goto-char (org-table-end)))
    (let ((case-fold-search t))
      (when (looking-at "\\([ \t]*\n\\)*[ \t]*#\\+TBLFM: *\\(.*\\)")
	(let ((strings (org-split-string (match-string-no-properties 2)
					 " *:: *"))
	      eq-alist seen)
	  (dolist (string strings (nreverse eq-alist))
	    (when (string-match "\\`\\(@[-+I<>0-9.$@]+\\|\\$\\([_a-zA-Z0-9]+\\|\
\[<>]+\\)\\) *= *\\(.*[^ \t]\\)"
				string)
	      (let ((lhs
		     (let ((m (match-string 1 string)))
		       (cond
			((not (match-end 2)) m)
			;; Is it a column reference?
			((string-match-p "\\`\\$\\([0-9]+\\|[<>]+\\)\\'" m) m)
			;; Since named columns are not possible in
			;; LHS, assume this is a named field.
			(t (match-string 2 string)))))
		    (rhs (match-string 3 string)))
		(push (cons lhs rhs) eq-alist)
		(cond
		 ((not (member lhs seen)) (push lhs seen))
		 (noerror
		  (message
		   "Double definition `%s=' in TBLFM line, please fix by hand"
		   lhs)
		  (ding)
		  (sit-for 2))
		 (t
		  (user-error
		   "Double definition `%s=' in TBLFM line, please fix by hand"
		   lhs)))))))))))

(defun org-table-fix-formulas (key replace &optional limit delta remove)
  "Modify the equations after the table structure has been edited.
KEY is \"@\" or \"$\".  REPLACE is an alist of numbers to replace.
For all numbers larger than LIMIT, shift them by DELTA."
  (save-excursion
    (goto-char (org-table-end))
    (while (let ((case-fold-search t)) (looking-at "[ \t]*#\\+tblfm:"))
      (let ((msg "The formulas in #+TBLFM have been updated")
	    (re (concat key "\\([0-9]+\\)"))
	    (re2
	     (when remove
	       (if (or (equal key "$") (equal key "$LR"))
		   (format "\\(@[0-9]+\\)?%s%d=.*?\\(::\\|$\\)"
			   (regexp-quote key) remove)
		 (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
	    s n a)
	(when remove
	  (while (re-search-forward re2 (point-at-eol) t)
	    (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
	      (if (equal (char-before (match-beginning 0)) ?.)
		  (user-error
		   "Change makes TBLFM term %s invalid, use undo to recover"
		   (match-string 0))
		(replace-match "")))))
	(while (re-search-forward re (point-at-eol) t)
	  (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
	    (setq s (match-string 1) n (string-to-number s))
	    (cond
	     ((setq a (assoc s replace))
	      (replace-match (concat key (cdr a)) t t)
	      (message msg))
	     ((and limit (> n limit))
	      (replace-match (concat key (int-to-string (+ n delta))) t t)
	      (message msg))))))
      (forward-line))))

;;;###autoload
(defun org-table-maybe-eval-formula ()
  "Check if the current field starts with \"=\" or \":=\".
If yes, store the formula and apply it."
  ;; We already know we are in a table.  Get field will only return a formula
  ;; when appropriate.  It might return a separator line, but no problem.
  (when org-table-formula-evaluate-inline
    (let* ((field (org-trim (or (org-table-get-field) "")))
	   named eq)
      (when (string-match "^:?=\\(.*[^=]\\)$" field)
	(setq named (equal (string-to-char field) ?:)
	      eq (match-string 1 field))
	(org-table-eval-formula (and named '(4))
				(org-table-formula-from-user eq))))))

;;;###autoload
(defun org-table-rotate-recalc-marks (&optional newchar)
  "Rotate the recalculation mark in the first column.
If in any row, the first field is not consistent with a mark,
insert a new column for the markers.
When there is an active region, change all the lines in the region,
after prompting for the marking character.
After each change, a message will be displayed indicating the meaning
of the new mark."
  (interactive)
  (unless (org-at-table-p) (user-error "Not at a table"))
  (let* ((region (org-region-active-p))
	 (l1 (and region
		  (save-excursion (goto-char (region-beginning))
				  (copy-marker (line-beginning-position)))))
	 (l2 (and region
		  (save-excursion (goto-char (region-end))
				  (copy-marker (line-beginning-position)))))
	 (l (copy-marker (line-beginning-position)))
	 (col (org-table-current-column))
	 (newchar (if region
		      (char-to-string
		       (read-char-exclusive
			"Change region to what mark?  Type # * ! $ or SPC: "))
		    newchar))
	 (no-special-column
	  (save-excursion
	    (goto-char (org-table-begin))
	    (re-search-forward
	     "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" (org-table-end) t))))
    (when (and newchar (not (assoc newchar org-recalc-marks)))
      (user-error "Invalid character `%s' in `org-table-rotate-recalc-marks'"
		  newchar))
    (when l1 (goto-char l1))
    (save-excursion
      (beginning-of-line)
      (unless (looking-at org-table-dataline-regexp)
	(user-error "Not at a table data line")))
    (when no-special-column
      (org-table-goto-column 1)
      (org-table-insert-column))
    (let ((previous-line-end (line-end-position))
	  (newchar
	   (save-excursion
	     (beginning-of-line)
	     (cond ((not (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")) "#")
		   (newchar)
		   (t (cadr (member (match-string 1)
				    (append (mapcar #'car org-recalc-marks)
					    '(" ")))))))))
      ;; Rotate mark in first row.
      (org-table-get-field 1 (format " %s " newchar))
      ;; Rotate marks in additional rows if a region is active.
      (when region
	(save-excursion
	  (forward-line)
	  (while (<= (point) l2)
	    (when (looking-at org-table-dataline-regexp)
	      (org-table-get-field 1 (format " %s " newchar)))
	    (forward-line))))
      ;; Only align if rotation actually changed lines' length.
      (when (/= previous-line-end (line-end-position)) (org-table-align)))
    (goto-char l)
    (org-table-goto-column (if no-special-column (1+ col) col))
    (when l1 (set-marker l1 nil))
    (when l2 (set-marker l2 nil))
    (set-marker l nil)
    (when (called-interactively-p 'interactive)
      (message "%s" (cdr (assoc newchar org-recalc-marks))))))

;;;###autoload
(defun org-table-maybe-recalculate-line ()
  "Recompute the current line if marked for it, and if we haven't just done it."
  (interactive)
  (and org-table-allow-automatic-line-recalculation
       (not (and (memq last-command org-recalc-commands)
		 (eq org-last-recalc-line (line-beginning-position))))
       (save-excursion (beginning-of-line 1)
		       (looking-at org-table-auto-recalculate-regexp))
       (org-table-recalculate) t))

;;;###autoload
(defun org-table-eval-formula (&optional arg equation
					 suppress-align suppress-const
					 suppress-store suppress-analysis)
  "Replace the table field value at the cursor by the result of a calculation.

In a table, this command replaces the value in the current field with the
result of a formula.  It also installs the formula as the \"current\" column
formula, by storing it in a special line below the table.  When called
with a `\\[universal-argument]' prefix the formula is installed as a \
field formula.

When called with a `\\[universal-argument] \\[universal-argument]' prefix, \
insert the active equation for the field
back into the current field, so that it can be edited there.  This is \
useful
in order to use \\<org-table-fedit-map>`\\[org-table-show-reference]' to \
check the referenced fields.

When called, the command first prompts for a formula, which is read in
the minibuffer.  Previously entered formulas are available through the
history list, and the last used formula is offered as a default.
These stored formulas are adapted correctly when moving, inserting, or
deleting columns with the corresponding commands.

The formula can be any algebraic expression understood by the Calc package.
For details, see the Org mode manual.

This function can also be called from Lisp programs and offers
additional arguments: EQUATION can be the formula to apply.  If this
argument is given, the user will not be prompted.

SUPPRESS-ALIGN is used to speed-up recursive calls by by-passing
unnecessary aligns.

SUPPRESS-CONST suppresses the interpretation of constants in the
formula, assuming that this has been done already outside the
function.

SUPPRESS-STORE means the formula should not be stored, either
because it is already stored, or because it is a modified
equation that should not overwrite the stored one.

SUPPRESS-ANALYSIS prevents analyzing the table and checking
location of point."
  (interactive "P")
  (unless suppress-analysis
    (org-table-check-inside-data-field nil t)
    (org-table-analyze))
  (if (equal arg '(16))
      (let ((eq (org-table-current-field-formula)))
	(org-table-get-field nil eq)
	(org-table-align)
	(setq org-table-may-need-update t))
    (let* (fields
	   (ndown (if (integerp arg) arg 1))
	   (org-table-automatic-realign nil)
	   (case-fold-search nil)
	   (down (> ndown 1))
	   (formula (if (and equation suppress-store)
			equation
		      (org-table-get-formula equation (equal arg '(4)))))
	   (n0 (org-table-current-column))
	   (org-tbl-calc-modes (copy-sequence org-calc-default-modes))
	   (numbers nil)	   ; was a variable, now fixed default
	   (keep-empty nil)
	   n form form0 formrpl formrg bw fmt x ev orig c lispp literal
	   duration duration-output-format)
      ;; Parse the format string.  Since we have a lot of modes, this is
      ;; a lot of work.  However, I think calc still uses most of the time.
      (if (string-match ";" formula)
	  (let ((tmp (org-split-string formula ";")))
	    (setq formula (car tmp)
		  fmt (concat (cdr (assoc "%" org-table-local-parameters))
			      (nth 1 tmp)))
	    (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
	      (setq c (string-to-char (match-string 1 fmt))
		    n (string-to-number (match-string 2 fmt)))
	      (if (= c ?p)
		  (setq org-tbl-calc-modes
			(org-table--set-calc-mode 'calc-internal-prec n))
		(setq org-tbl-calc-modes
		      (org-table--set-calc-mode
		       'calc-float-format
		       (list (cdr (assoc c '((?n . float) (?f . fix)
					     (?s . sci) (?e . eng))))
			     n))))
	      (setq fmt (replace-match "" t t fmt)))
	    (if (string-match "[tTU]" fmt)
		(let ((ff (match-string 0 fmt)))
		  (setq duration t numbers t
			duration-output-format
			(cond ((equal ff "T") nil)
			      ((equal ff "t") org-table-duration-custom-format)
			      ((equal ff "U") 'hh:mm))
			fmt (replace-match "" t t fmt))))
	    (if (string-match "N" fmt)
		(setq numbers t
		      fmt (replace-match "" t t fmt)))
	    (if (string-match "L" fmt)
		(setq literal t
		      fmt (replace-match "" t t fmt)))
	    (if (string-match "E" fmt)
		(setq keep-empty t
		      fmt (replace-match "" t t fmt)))
	    (while (string-match "[DRFS]" fmt)
	      (setq org-tbl-calc-modes
		    (org-table--set-calc-mode (match-string 0 fmt)))
	      (setq fmt (replace-match "" t t fmt)))
	    (unless (string-match "\\S-" fmt)
	      (setq fmt nil))))
      (when (and (not suppress-const) org-table-formula-use-constants)
	(setq formula (org-table-formula-substitute-names formula)))
      (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
      (setq formula (org-table-formula-handle-first/last-rc formula))
      (while (> ndown 0)
	(setq fields (org-split-string
		      (org-trim
		       (buffer-substring-no-properties
			(line-beginning-position) (line-end-position)))
		      " *| *"))
	;; replace fields with duration values if relevant
	(if duration
	    (setq fields
		  (mapcar (lambda (x) (org-table-time-string-to-seconds x))
			  fields)))
	(if (eq numbers t)
	    (setq fields (mapcar
			  (lambda (x)
			    (if (string-match "\\S-" x)
				(number-to-string (string-to-number x))
			      x))
			  fields)))
	(setq ndown (1- ndown))
	(setq form (copy-sequence formula)
	      lispp (and (> (length form) 2) (equal (substring form 0 2) "'(")))
	(if (and lispp literal) (setq lispp 'literal))

	;; Insert row and column number of formula result field
	(while (string-match "[@$]#" form)
	  (setq form
		(replace-match
		 (format "%d"
			 (save-match-data
			   (if (equal (substring form (match-beginning 0)
						 (1+ (match-beginning 0)))
				      "@")
			       (org-table-current-dline)
			     (org-table-current-column))))
		 t t form)))

	;; Check for old vertical references
	(org-table--error-on-old-row-references form)
	;; Insert remote references
	(setq form (org-table-remote-reference-indirection form))
	(while (string-match "\\<remote([ \t]*\\([^,)]+\\)[ \t]*,[ \t]*\\([^\n)]+\\))" form)
	  (setq form
		(replace-match
		 (save-match-data
		   (org-table-make-reference
		    (let ((rmtrng (org-table-get-remote-range
				   (match-string 1 form) (match-string 2 form))))
		      (if duration
			  (if (listp rmtrng)
			      (mapcar (lambda(x) (org-table-time-string-to-seconds x)) rmtrng)
			    (org-table-time-string-to-seconds rmtrng))
			rmtrng))
		    keep-empty numbers lispp))
		 t t form)))
	;; Insert complex ranges
	(while (and (string-match org-table-range-regexp form)
		    (> (length (match-string 0 form)) 1))
	  (setq formrg
		(save-match-data
		  (org-table-get-range
		   (match-string 0 form) org-table-current-begin-pos n0)))
	  (setq formrpl
		(save-match-data
		  (org-table-make-reference
		   ;; possibly handle durations
		   (if duration
		       (if (listp formrg)
			   (mapcar (lambda(x) (org-table-time-string-to-seconds x)) formrg)
			 (org-table-time-string-to-seconds formrg))
		     formrg)
		   keep-empty numbers lispp)))
	  (if (not (save-match-data
		     (string-match (regexp-quote form) formrpl)))
	      (setq form (replace-match formrpl t t form))
	    (user-error "Spreadsheet error: invalid reference \"%s\"" form)))
	;; Insert simple ranges, i.e. included in the current row.
	(while (string-match
		"\\$\\(\\([-+]\\)?[0-9]+\\)\\.\\.\\$\\(\\([-+]\\)?[0-9]+\\)"
		form)
	  (setq form
		(replace-match
		 (save-match-data
		   (org-table-make-reference
		    (cl-subseq fields
			       (+ (if (match-end 2) n0 0)
				  (string-to-number (match-string 1 form))
				  -1)
			       (+ (if (match-end 4) n0 0)
				  (string-to-number (match-string 3 form))))
		    keep-empty numbers lispp))
		 t t form)))
	(setq form0 form)
	;; Insert the references to fields in same row
	(while (string-match "\\$\\(\\([-+]\\)?[0-9]+\\)" form)
	  (setq n (+ (string-to-number (match-string 1 form))
		     (if (match-end 2) n0 0))
		x (nth (1- (if (= n 0) n0 (max n 1))) fields)
		formrpl (save-match-data
			  (org-table-make-reference
			   x keep-empty numbers lispp)))
	  (when (or (not x)
		    (save-match-data
		      (string-match (regexp-quote formula) formrpl)))
	    (user-error "Invalid field specifier \"%s\""
			(match-string 0 form)))
	  (setq form (replace-match formrpl t t form)))

	(if lispp
	    (setq ev (condition-case nil
			 (eval (eval (read form)))
		       (error "#ERROR"))
		  ev (if (numberp ev) (number-to-string ev) ev)
		  ev (if duration (org-table-time-seconds-to-string
				   (string-to-number ev)
				   duration-output-format) ev))

	  ;; Use <...> time-stamps so that Calc can handle them.
	  (setq form
		(replace-regexp-in-string org-ts-regexp-inactive "<\\1>" form))
	  ;; Internationalize local time-stamps by setting locale to
	  ;; "C".
	  (setq form
		(replace-regexp-in-string
		 org-ts-regexp
		 (lambda (ts)
		   (let ((system-time-locale "C"))
		     (format-time-string
		      (org-time-stamp-format
		       (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts))
		      (apply #'encode-time
			     (save-match-data (org-parse-time-string ts))))))
		 form t t))

	  (setq ev (if (and duration (string-match "^[0-9]+:[0-9]+\\(?::[0-9]+\\)?$" form))
		       form
		     (calc-eval (cons form org-tbl-calc-modes)
				(when (and (not keep-empty) numbers) 'num)))
		ev (if duration (org-table-time-seconds-to-string
				 (if (string-match "^[0-9]+:[0-9]+\\(?::[0-9]+\\)?$" ev)
				     (string-to-number (org-table-time-string-to-seconds ev))
				   (string-to-number ev))
				 duration-output-format)
		     ev)))

	(when org-table-formula-debug
	  (with-output-to-temp-buffer "*Substitution History*"
	    (princ (format "Substitution history of formula
Orig:   %s
$xyz->  %s
@r$c->  %s
$1->    %s\n" orig formula form0 form))
	    (if (consp ev)
		(princ (format "        %s^\nError:  %s"
			       (make-string (car ev) ?\-) (nth 1 ev)))
	      (princ (format "Result: %s\nFormat: %s\nFinal:  %s"
			     ev (or fmt "NONE")
			     (if fmt (format fmt (string-to-number ev)) ev)))))
	  (setq bw (get-buffer-window "*Substitution History*"))
	  (org-fit-window-to-buffer bw)
	  (unless (and (called-interactively-p 'any) (not ndown))
	    (unless (let (inhibit-redisplay)
		      (y-or-n-p "Debugging Formula.  Continue to next? "))
	      (org-table-align)
	      (user-error "Abort"))
	    (delete-window bw)
	    (message "")))
	(when (consp ev) (setq fmt nil ev "#ERROR"))
	(org-table-justify-field-maybe
	 (format org-table-formula-field-format
		 (cond
		  ((not (stringp ev)) ev)
		  (fmt (format fmt (string-to-number ev)))
		  ;; Replace any active time stamp in the result with
		  ;; an inactive one.  Dates in tables are likely
		  ;; piece of regular data, not meant to appear in the
		  ;; agenda.
		  (t (replace-regexp-in-string org-ts-regexp "[\\1]" ev)))))
	(if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
	    (call-interactively 'org-return)
	  (setq ndown 0)))
      (and down (org-table-maybe-recalculate-line))
      (or suppress-align (and org-table-may-need-update
			      (org-table-align))))))

(defun org-table-put-field-property (prop value)
  (save-excursion
    (put-text-property (progn (skip-chars-backward "^|") (point))
		       (progn (skip-chars-forward "^|") (point))
		       prop value)))

(defun org-table-get-range (desc &optional tbeg col highlight corners-only)
  "Get a calc vector from a column, according to descriptor DESC.

Optional arguments TBEG and COL can give the beginning of the table and
the current column, to avoid unnecessary parsing.

HIGHLIGHT means just highlight the range.

When CORNERS-ONLY is set, only return the corners of the range as
a list (line1 column1 line2 column2) where line1 and line2 are
line numbers relative to beginning of table, or TBEG, and column1
and column2 are table column numbers."
  (let* ((desc (if (string-match-p "\\`\\$[0-9]+\\.\\.\\$[0-9]+\\'" desc)
		   (replace-regexp-in-string "\\$" "@0$" desc)
		 desc))
	 (col (or col (org-table-current-column)))
	 (tbeg (or tbeg (org-table-begin)))
	 (thisline (count-lines tbeg (line-beginning-position))))
    (unless (string-match org-table-range-regexp desc)
      (user-error "Invalid table range specifier `%s'" desc))
    (let ((rangep (match-end 3))
	  (r1 (let ((r (and (match-end 1) (match-string 1 desc))))
		(or (save-match-data
		      (and (org-string-nw-p r)
			   (org-table--descriptor-line r thisline)))
		    thisline)))
	  (r2 (let ((r (and (match-end 4) (match-string 4 desc))))
		(or (save-match-data
		      (and (org-string-nw-p r)
			   (org-table--descriptor-line r thisline)))
		    thisline)))
	  (c1 (let ((c (and (match-end 2) (substring (match-string 2 desc) 1))))
		(if (or (not c) (= (string-to-number c) 0)) col
		  (+ (string-to-number c)
		     (if (memq (string-to-char c) '(?- ?+)) col 0)))))
	  (c2 (let ((c (and (match-end 5) (substring (match-string 5 desc) 1))))
		(if (or (not c) (= (string-to-number c) 0)) col
		  (+ (string-to-number c)
		     (if (memq (string-to-char c) '(?- ?+)) col 0))))))
      (save-excursion
	(if (and (not corners-only)
		 (or (not rangep) (and (= r1 r2) (= c1 c2))))
	    ;; Just one field.
	    (progn
	      (forward-line (- r1 thisline))
	      (while (not (looking-at org-table-dataline-regexp))
		(forward-line))
	      (prog1 (org-trim (org-table-get-field c1))
		(when highlight (org-table-highlight-rectangle))))
	  ;; A range, return a vector.  First sort the numbers to get
	  ;; a regular rectangle.
	  (let ((first-row (min r1 r2))
		(last-row (max r1 r2))
		(first-column (min c1 c2))
		(last-column (max c1 c2)))
	    (if corners-only (list first-row first-column last-row last-column)
	      ;; Copy the range values into a list.
	      (forward-line (- first-row thisline))
	      (while (not (looking-at org-table-dataline-regexp))
		(forward-line)
		(cl-incf first-row))
	      (org-table-goto-column first-column)
	      (let ((beg (point)))
		(forward-line (- last-row first-row))
		(while (not (looking-at org-table-dataline-regexp))
		  (forward-line -1))
		(org-table-goto-column last-column)
		(let ((end (point)))
		  (when highlight
		    (org-table-highlight-rectangle
		     beg (progn (skip-chars-forward "^|\n") (point))))
		  ;; Return string representation of calc vector.
		  (mapcar #'org-trim
			  (apply #'append
				 (org-table-copy-region beg end))))))))))))

(defun org-table--descriptor-line (desc cline)
  "Return relative line number corresponding to descriptor DESC.
The cursor is currently in relative line number CLINE."
  (if (string-match "\\`[0-9]+\\'" desc)
      (aref org-table-dlines (string-to-number desc))
    (when (or (not (string-match
		    "^\\(\\([-+]\\)?\\(I+\\)\\)?\\(\\([-+]\\)?\\([0-9]+\\)\\)?"
		    ;;  1  2          3           4  5          6
		    desc))
	      (and (not (match-end 3)) (not (match-end 6)))
	      (and (match-end 3) (match-end 6) (not (match-end 5))))
      (user-error "Invalid row descriptor `%s'" desc))
    (let* ((hn (and (match-end 3) (- (match-end 3) (match-beginning 3))))
	   (hdir (match-string 2 desc))
	   (odir (match-string 5 desc))
	   (on (and (match-end 6) (string-to-number (match-string 6 desc))))
	   (rel (and (match-end 6)
		     (or (and (match-end 1) (not (match-end 3)))
			 (match-end 5)))))
      (when (and hn (not hdir))
	(setq cline 0)
	(setq hdir "+")
	(when (eq (aref org-table-current-line-types 0) 'hline) (cl-decf hn)))
      (when (and (not hn) on (not odir)) (user-error "Should never happen"))
      (when hn
	(setq cline
	      (org-table--row-type 'hline hn cline (equal hdir "-") nil desc)))
      (when on
	(setq cline
	      (org-table--row-type 'dline on cline (equal odir "-") rel desc)))
      cline)))

(defun org-table--row-type (type n i backwards relative desc)
  "Return relative line of Nth row with type TYPE.
Search starts from relative line I.  When BACKWARDS in non-nil,
look before I.  When RELATIVE is non-nil, the reference is
relative.  DESC is the original descriptor that started the
search, as a string."
  (let ((l (length org-table-current-line-types)))
    (catch :exit
      (dotimes (_ n)
	(while (and (cl-incf i (if backwards -1 1))
		    (>= i 0)
		    (< i l)
		    (not (eq (aref org-table-current-line-types i) type))
		    ;; We are going to cross a hline.  Check if this is
		    ;; an authorized move.
		    (cond
		     ((not relative))
		     ((not (eq (aref org-table-current-line-types i) 'hline)))
		     ((eq org-table-relative-ref-may-cross-hline t))
		     ((eq org-table-relative-ref-may-cross-hline 'error)
		      (user-error "Row descriptor %s crosses hline" desc))
		     (t (cl-decf i (if backwards -1 1)) ; Step back.
			(throw :exit nil)))))))
    (cond ((or (< i 0) (>= i l))
	   (user-error "Row descriptor %s leads outside table" desc))
	  ;; The last hline doesn't exist.  Instead, point to last row
	  ;; in table.
	  ((= i (1- l)) (1- i))
	  (t i))))

(defun org-table--error-on-old-row-references (s)
  (when (string-match "&[-+0-9I]" s)
    (user-error "Formula contains old &row reference, please rewrite using @-syntax")))

(defun org-table-make-reference (elements keep-empty numbers lispp)
  "Convert list ELEMENTS to something appropriate to insert into formula.
KEEP-EMPTY indicated to keep empty fields, default is to skip them.
NUMBERS indicates that everything should be converted to numbers.
LISPP non-nil means to return something appropriate for a Lisp
list, `literal' is for the format specifier L."
  ;; Calc nan (not a number) is used for the conversion of the empty
  ;; field to a reference for several reasons: (i) It is accepted in a
  ;; Calc formula (e. g. "" or "()" would result in a Calc error).
  ;; (ii) In a single field (not in range) it can be distinguished
  ;; from "(nan)" which is the reference made from a single field
  ;; containing "nan".
  (if (stringp elements)
      ;; field reference
      (if lispp
	  (if (eq lispp 'literal)
	      elements
	    (if (and (eq elements "") (not keep-empty))
		""
	      (prin1-to-string
	       (if numbers (string-to-number elements) elements))))
	(if (string-match "\\S-" elements)
	    (progn
	      (when numbers (setq elements (number-to-string
					    (string-to-number elements))))
	      (concat "(" elements ")"))
	  (if (or (not keep-empty) numbers) "(0)" "nan")))
    ;; range reference
    (unless keep-empty
      (setq elements
	    (delq nil
		  (mapcar (lambda (x) (if (string-match "\\S-" x) x nil))
			  elements))))
    (setq elements (or elements '()))  ; if delq returns nil then we need '()
    (if lispp
	(mapconcat
	 (lambda (x)
	   (if (eq lispp 'literal)
	       x
	     (prin1-to-string (if numbers (string-to-number x) x))))
	 elements " ")
      (concat "[" (mapconcat
		   (lambda (x)
		     (if (string-match "\\S-" x)
			 (if numbers
			     (number-to-string (string-to-number x))
			   x)
		       (if (or (not keep-empty) numbers) "0" "nan")))
		   elements
		   ",") "]"))))

(defun org-table-message-once-per-second (t1 &rest args)
  "If there has been more than one second since T1, display message.
ARGS are passed as arguments to the `message' function.  Returns
current time if a message is printed, otherwise returns T1.  If
T1 is nil, always messages."
  (let ((curtime (current-time)))
    (if (or (not t1) (org-time-less-p 1 (org-time-subtract curtime t1)))
	(progn (apply 'message args)
	       curtime)
      t1)))

;;;###autoload
(defun org-table-recalculate (&optional all noalign)
  "Recalculate the current table line by applying all stored formulas.

With prefix arg ALL, do this for all lines in the table.

When called with a `\\[universal-argument] \\[universal-argument]' prefix, or \
if ALL is the symbol `iterate',
recompute the table until it no longer changes.

If NOALIGN is not nil, do not re-align the table after the computations
are done.  This is typically used internally to save time, if it is
known that the table will be realigned a little later anyway."
  (interactive "P")
  (unless (memq this-command org-recalc-commands)
    (push this-command org-recalc-commands))
  (unless (org-at-table-p) (user-error "Not at a table"))
  (if (or (eq all 'iterate) (equal all '(16)))
      (org-table-iterate)
    (org-table-analyze)
    (let* ((eqlist (sort (org-table-get-stored-formulas)
			 (lambda (a b) (string< (car a) (car b)))))
	   (inhibit-redisplay (not debug-on-error))
	   (line-re org-table-dataline-regexp)
	   (log-first-time (current-time))
	   (log-last-time log-first-time)
	   (cnt 0)
	   beg end eqlcol eqlfield)
      ;; Insert constants in all formulas.
      (when eqlist
	(org-table-with-shrunk-columns
	 (org-table-save-field
	  ;; Expand equations, then split the equation list between
	  ;; column formulas and field formulas.
	  (dolist (eq eqlist)
	    (let* ((rhs (org-table-formula-substitute-names
			 (org-table-formula-handle-first/last-rc (cdr eq))))
		   (old-lhs (car eq))
		   (lhs
		    (org-table-formula-handle-first/last-rc
		     (cond
		      ((string-match "\\`@-?I+" old-lhs)
		       (user-error "Can't assign to hline relative reference"))
		      ((string-match "\\`\\$[<>]" old-lhs)
		       (let ((new (org-table-formula-handle-first/last-rc
				   old-lhs)))
			 (when (assoc new eqlist)
			   (user-error "\"%s=\" formula tries to overwrite \
existing formula for column %s"
				       old-lhs
				       new))
			 new))
		      (t old-lhs)))))
	      (if (string-match-p "\\`\\$[0-9]+\\'" lhs)
		  (push (cons lhs rhs) eqlcol)
		(push (cons lhs rhs) eqlfield))))
	  (setq eqlcol (nreverse eqlcol))
	  ;; Expand ranges in lhs of formulas
	  (setq eqlfield (org-table-expand-lhs-ranges (nreverse eqlfield)))
	  ;; Get the correct line range to process.
	  (if all
	      (progn
		(setq end (copy-marker (org-table-end)))
		(goto-char (setq beg org-table-current-begin-pos))
		(cond
		 ((re-search-forward org-table-calculate-mark-regexp end t)
		  ;; This is a table with marked lines, compute selected
		  ;; lines.
		  (setq line-re org-table-recalculate-regexp))
		 ;; Move forward to the first non-header line.
		 ((and (re-search-forward org-table-dataline-regexp end t)
		       (re-search-forward org-table-hline-regexp end t)
		       (re-search-forward org-table-dataline-regexp end t))
		  (setq beg (match-beginning 0)))
		 ;; Just leave BEG at the start of the table.
		 (t nil)))
	    (setq beg (line-beginning-position)
		  end (copy-marker (line-beginning-position 2))))
	  (goto-char beg)
	  ;; Mark named fields untouchable.  Also check if several
	  ;; field/range formulas try to set the same field.
	  (remove-text-properties beg end '(:org-untouchable t))
	  (let ((current-line (count-lines org-table-current-begin-pos
					   (line-beginning-position)))
		seen-fields)
	    (dolist (eq eqlfield)
	      (let* ((name (car eq))
		     (location (assoc name org-table-named-field-locations))
		     (eq-line (or (nth 1 location)
				  (and (string-match "\\`@\\([0-9]+\\)" name)
				       (aref org-table-dlines
					     (string-to-number
					      (match-string 1 name))))))
		     (reference
		      (if location
			  ;; Turn field coordinates associated to NAME
			  ;; into an absolute reference.
			  (format "@%d$%d"
				  (org-table-line-to-dline eq-line)
				  (nth 2 location))
			name)))
		(when (member reference seen-fields)
		  (user-error "Several field/range formulas try to set %s"
			      reference))
		(push reference seen-fields)
		(when (or all (eq eq-line current-line))
		  (org-table-goto-field name)
		  (org-table-put-field-property :org-untouchable t)))))
	  ;; Evaluate the column formulas, but skip fields covered by
	  ;; field formulas.
	  (goto-char beg)
	  (while (re-search-forward line-re end t)
	    (unless (string-match "\\` *[_^!$/] *\\'" (org-table-get-field 1))
	      ;; Unprotected line, recalculate.
	      (cl-incf cnt)
	      (when all
		(setq log-last-time
		      (org-table-message-once-per-second
		       log-last-time
		       "Re-applying formulas to full table...(line %d)" cnt)))
	      (if (markerp org-last-recalc-line)
		  (move-marker org-last-recalc-line (line-beginning-position))
		(setq org-last-recalc-line
		      (copy-marker (line-beginning-position))))
	      (dolist (entry eqlcol)
		(goto-char org-last-recalc-line)
		(org-table-goto-column
		 (string-to-number (substring (car entry) 1)) nil 'force)
		(unless (get-text-property (point) :org-untouchable)
		  (org-table-eval-formula
		   nil (cdr entry) 'noalign 'nocst 'nostore 'noanalysis)))))
	  ;; Evaluate the field formulas.
	  (dolist (eq eqlfield)
	    (let ((reference (car eq))
		  (formula (cdr eq)))
	      (setq log-last-time
		    (org-table-message-once-per-second
		     (and all log-last-time)
		     "Re-applying formula to field: %s" (car eq)))
	      (org-table-goto-field
	       reference
	       ;; Possibly create a new column, as long as
	       ;; `org-table-formula-create-columns' allows it.
	       (let ((column-count (progn (end-of-line)
					  (1- (org-table-current-column)))))
		 (lambda (column)
		   (when (> column 1000)
		     (user-error "Formula column target too large"))
		   (and (> column column-count)
			(or (eq org-table-formula-create-columns t)
			    (and (eq org-table-formula-create-columns 'warn)
				 (progn
				   (org-display-warning
				    "Out-of-bounds formula added columns")
				   t))
			    (and (eq org-table-formula-create-columns 'prompt)
				 (yes-or-no-p
				  "Out-of-bounds formula.  Add columns? "))
			    (user-error
			     "Missing columns in the table.  Aborting"))))))
	      (org-table-eval-formula nil formula t t t t)))
	  ;; Clean up marker.
	  (set-marker end nil)))
	(unless noalign
	  (when org-table-may-need-update (org-table-align))
	  (when all
	    (org-table-message-once-per-second
	     log-first-time "Re-applying formulas to %d lines... done" cnt)))
	(org-table-message-once-per-second
	 (and all log-first-time) "Re-applying formulas... done")))))

;;;###autoload
(defun org-table-iterate (&optional arg)
  "Recalculate the table until it does not change anymore.
The maximum number of iterations is 10, but you can choose a different value
with the prefix ARG."
  (interactive "P")
  (let ((imax (if arg (prefix-numeric-value arg) 10))
	(i 0)
	(lasttbl (buffer-substring (org-table-begin) (org-table-end)))
	thistbl)
    (catch 'exit
      (while (< i imax)
	(setq i (1+ i))
	(org-table-recalculate 'all)
	(setq thistbl (buffer-substring (org-table-begin) (org-table-end)))
	(if (not (string= lasttbl thistbl))
	    (setq lasttbl thistbl)
	  (if (> i 1)
	      (message "Convergence after %d iterations" i)
	    (message "Table was already stable"))
	  (throw 'exit t)))
      (user-error "No convergence after %d iterations" i))))

;;;###autoload
(defun org-table-recalculate-buffer-tables ()
  "Recalculate all tables in the current buffer."
  (interactive)
  (org-with-wide-buffer
   (org-table-map-tables
    (lambda ()
      ;; Reason for separate `org-table-align': When repeating
      ;; (org-table-recalculate t) `org-table-may-need-update' gets in
      ;; the way.
      (org-table-recalculate t t)
      (org-table-align))
    t)))

;;;###autoload
(defun org-table-iterate-buffer-tables ()
  "Iterate all tables in the buffer, to converge inter-table dependencies."
  (interactive)
  (let* ((imax 10)
	 (i imax)
	 (checksum (md5 (buffer-string)))
	 c1)
    (org-with-wide-buffer
     (catch 'exit
       (while (> i 0)
	 (setq i (1- i))
	 (org-table-map-tables (lambda () (org-table-recalculate t t)) t)
	 (if (equal checksum (setq c1 (md5 (buffer-string))))
	     (progn
	       (org-table-map-tables #'org-table-align t)
	       (message "Convergence after %d iterations" (- imax i))
	       (throw 'exit t))
	   (setq checksum c1)))
       (org-table-map-tables #'org-table-align t)
       (user-error "No convergence after %d iterations" imax)))))

(defun org-table-calc-current-TBLFM (&optional arg)
  "Apply the #+TBLFM in the line at point to the table."
  (interactive "P")
  (unless (org-at-TBLFM-p) (user-error "Not at a #+TBLFM line"))
  (let ((formula (buffer-substring
		  (line-beginning-position)
		  (line-end-position))))
    (save-excursion
      ;; Insert a temporary formula at right after the table
      (goto-char (org-table-TBLFM-begin))
      (let ((s (point-marker)))
	(insert formula "\n")
	(let ((e (point-marker)))
	  ;; Recalculate the table.
	  (beginning-of-line 0)		; move to the inserted line
	  (skip-chars-backward " \r\n\t")
	  (unwind-protect
	      (org-call-with-arg #'org-table-recalculate (or arg t))
	    ;; Delete the formula inserted temporarily.
	    (delete-region s e)
	    (set-marker s nil)
	    (set-marker e nil)))))))

(defun org-table-TBLFM-begin ()
  "Find the beginning of the TBLFM lines and return its position.
Return nil when the beginning of TBLFM line was not found."
  (save-excursion
    (when (progn (forward-line 1)
		 (re-search-backward org-table-TBLFM-begin-regexp nil t))
      (line-beginning-position 2))))

(defun org-table-expand-lhs-ranges (equations)
  "Expand list of formulas.
If some of the RHS in the formulas are ranges or a row reference,
expand them to individual field equations for each field.  This
function assumes the table is already analyzed (i.e., using
`org-table-analyze')."
  (let (res)
    (dolist (e equations (nreverse res))
      (let ((lhs (car e))
	    (rhs (cdr e)))
	(cond
	 ((string-match-p "\\`@-?[-+0-9]+\\$-?[0-9]+\\'" lhs)
	  ;; This just refers to one fixed field.
	  (push e res))
	 ((string-match-p "\\`[a-zA-Z][_a-zA-Z0-9]*\\'" lhs)
	  ;; This just refers to one fixed named field.
	  (push e res))
	 ((string-match-p "\\`\\$[0-9]+\\'" lhs)
	  ;; Column formulas are treated specially and are not
	  ;; expanded.
	  (push e res))
	 ((string-match "\\`@[0-9]+\\'" lhs)
	  (dotimes (ic org-table-current-ncol)
	    (push (cons (propertize (format "%s$%d" lhs (1+ ic)) :orig-eqn e)
			rhs)
		  res)))
	 (t
	  (let* ((range (org-table-get-range
			 lhs org-table-current-begin-pos 1 nil 'corners))
		 (r1 (org-table-line-to-dline (nth 0 range)))
		 (c1 (nth 1 range))
		 (r2 (org-table-line-to-dline (nth 2 range) 'above))
		 (c2 (nth 3 range)))
	    (cl-loop for ir from r1 to r2 do
		     (cl-loop for ic from c1 to c2 do
			      (push (cons (propertize
					   (format "@%d$%d" ir ic) :orig-eqn e)
					  rhs)
				    res))))))))))

(defun org-table-formula-handle-first/last-rc (s)
  "Replace @<, @>, $<, $> with first/last row/column of the table.
So @< and $< will always be replaced with @1 and $1, respectively.
The advantage of these special markers are that structure editing of
the table will not change them, while @1 and $1 will be modified
when a line/row is swapped out of that privileged position.  So for
formulas that use a range of rows or columns, it may often be better
to anchor the formula with \"I\" row markers, or to offset from the
borders of the table using the @< @> $< $> makers."
  (let (n nmax len char (start 0))
    (while (string-match "\\([@$]\\)\\(<+\\|>+\\)\\|\\(remote([^)]+)\\)"
			 s start)
      (if (match-end 3)
	  (setq start (match-end 3))
	(setq nmax (if (equal (match-string 1 s) "@")
		       (1- (length org-table-dlines))
		     org-table-current-ncol)
	      len (- (match-end 2) (match-beginning 2))
	      char (string-to-char (match-string 2 s))
	      n (if (= char ?<)
		    len
		  (- nmax len -1)))
	(if (or (< n 1) (> n nmax))
	    (user-error "Reference \"%s\" in expression \"%s\" points outside table"
			(match-string 0 s) s))
	(setq start (match-beginning 0))
	(setq s (replace-match (format "%s%d" (match-string 1 s) n) t t s)))))
  s)

(defun org-table-formula-substitute-names (f)
  "Replace $const with values in string F."
  (let ((start 0)
	(pp (/= (string-to-char f) ?'))
	(duration (string-match-p ";.*[Tt].*\\'" f))
	(new (replace-regexp-in-string	; Check for column names.
	      org-table-column-name-regexp
	      (lambda (m)
		(concat "$" (cdr (assoc (match-string 1 m)
					org-table-column-names))))
	      f t t)))
    ;; Parameters and constants.
    (while (setq start
		 (string-match
		  "\\$\\([a-zA-Z][_a-zA-Z0-9]*\\)\\|\\(\\<remote([^)]*)\\)"
		  new start))
      (if (match-end 2) (setq start (match-end 2))
	(cl-incf start)
	;; When a duration is expected, convert value on the fly.
	(let ((value
	       (save-match-data
		 (let ((v (org-table-get-constant (match-string 1 new))))
		   (if (and (org-string-nw-p v) duration)
		       (org-table-time-string-to-seconds v)
		     v)))))
	  (when value
	    (setq new (replace-match
		       (concat (and pp "(") value (and pp ")")) t t new))))))
    (if org-table-formula-debug (propertize new :orig-formula f) new)))

(defun org-table-get-constant (const)
  "Find the value for a parameter or constant in a formula.
Parameters get priority."
  (or (cdr (assoc const org-table-local-parameters))
      (cdr (assoc const org-table-formula-constants-local))
      (cdr (assoc const org-table-formula-constants))
      (and (fboundp 'constants-get) (constants-get const))
      (and (string= (substring const 0 (min 5 (length const))) "PROP_")
	   (org-entry-get nil (substring const 5) 'inherit))
      "#UNDEFINED_NAME"))

(defvar org-table-fedit-map
  (let ((map (make-sparse-keymap)))
    (org-defkey map "\C-x\C-s"      'org-table-fedit-finish)
    (org-defkey map "\C-c\C-s"      'org-table-fedit-finish)
    (org-defkey map "\C-c\C-c"      'org-table-fedit-finish)
    (org-defkey map "\C-c'"         'org-table-fedit-finish)
    (org-defkey map "\C-c\C-q"      'org-table-fedit-abort)
    (org-defkey map "\C-c?"	    'org-table-show-reference)
    (org-defkey map [(meta shift up)]    'org-table-fedit-line-up)
    (org-defkey map [(meta shift down)]  'org-table-fedit-line-down)
    (org-defkey map [(shift up)]    'org-table-fedit-ref-up)
    (org-defkey map [(shift down)]  'org-table-fedit-ref-down)
    (org-defkey map [(shift left)]  'org-table-fedit-ref-left)
    (org-defkey map [(shift right)] 'org-table-fedit-ref-right)
    (org-defkey map [(meta up)]     'org-table-fedit-scroll-down)
    (org-defkey map [(meta down)]   'org-table-fedit-scroll)
    (org-defkey map [(meta tab)]    'lisp-complete-symbol)
    (org-defkey map "\M-\C-i"       'lisp-complete-symbol)
    (org-defkey map [(tab)]	    'org-table-fedit-lisp-indent)
    (org-defkey map "\C-i"	    'org-table-fedit-lisp-indent)
    (org-defkey map "\C-c\C-r" 'org-table-fedit-toggle-ref-type)
    (org-defkey map "\C-c}"    'org-table-fedit-toggle-coordinates)
    map))

(easy-menu-define org-table-fedit-menu org-table-fedit-map "Org Edit Formulas Menu"
  '("Edit-Formulas"
    ["Finish and Install" org-table-fedit-finish t]
    ["Finish, Install, and Apply" (org-table-fedit-finish t) :keys "C-u C-c C-c"]
    ["Abort" org-table-fedit-abort t]
    "--"
    ["Pretty-Print Lisp Formula" org-table-fedit-lisp-indent t]
    ["Complete Lisp Symbol" lisp-complete-symbol t]
    "--"
    "Shift Reference at Point"
    ["Up" org-table-fedit-ref-up t]
    ["Down" org-table-fedit-ref-down t]
    ["Left" org-table-fedit-ref-left t]
    ["Right" org-table-fedit-ref-right t]
    "-"
    "Change Test Row for Column Formulas"
    ["Up" org-table-fedit-line-up t]
    ["Down" org-table-fedit-line-down t]
    "--"
    ["Scroll Table Window" org-table-fedit-scroll t]
    ["Scroll Table Window down" org-table-fedit-scroll-down t]
    ["Show Table Grid" org-table-fedit-toggle-coordinates
     :style toggle :selected (with-current-buffer (marker-buffer org-pos)
			       org-table-overlay-coordinates)]
    "--"
    ["Standard Refs (B3 instead of @3$2)" org-table-fedit-toggle-ref-type
     :style toggle :selected org-table-buffer-is-an]))

(defvar org-table--fedit-source nil
  "Position of the TBLFM line being edited.")

;;;###autoload
(defun org-table-edit-formulas ()
  "Edit the formulas of the current table in a separate buffer."
  (interactive)
  (let ((at-tblfm (org-at-TBLFM-p)))
    (unless (or at-tblfm (org-at-table-p))
      (user-error "Not at a table"))
    (save-excursion
      ;; Move point within the table before analyzing it.
      (when at-tblfm (re-search-backward "^[ \t]*|"))
      (org-table-analyze))
    (let ((key (org-table-current-field-formula 'key 'noerror))
	  (eql (sort (org-table-get-stored-formulas t (and at-tblfm (point)))
		     #'org-table-formula-less-p))
	  (pos (point-marker))
	  (source (copy-marker (line-beginning-position)))
	  (startline 1)
	  (wc (current-window-configuration))
	  (sel-win (selected-window))
	  (titles '((column . "# Column Formulas\n")
		    (field . "# Field and Range Formulas\n")
		    (named . "# Named Field Formulas\n"))))
      (org-switch-to-buffer-other-window "*Edit Formulas*")
      (erase-buffer)
      ;; Keep global-font-lock-mode from turning on font-lock-mode
      (let ((font-lock-global-modes '(not fundamental-mode)))
	(fundamental-mode))
      (setq-local font-lock-global-modes (list 'not major-mode))
      (setq-local org-pos pos)
      (setq-local org-table--fedit-source source)
      (setq-local org-window-configuration wc)
      (setq-local org-selected-window sel-win)
      (use-local-map org-table-fedit-map)
      (add-hook 'post-command-hook #'org-table-fedit-post-command t t)
      (easy-menu-add org-table-fedit-menu)
      (setq startline (org-current-line))
      (dolist (entry eql)
	(let* ((type (cond
		      ((string-match "\\`\\$\\([0-9]+\\|[<>]+\\)\\'"
				     (car entry))
		       'column)
		      ((equal (string-to-char (car entry)) ?@) 'field)
		      (t 'named)))
	       (title (assq type titles)))
	  (when title
	    (unless (bobp) (insert "\n"))
	    (insert
	     (org-add-props (cdr title) nil 'face font-lock-comment-face))
	    (setq titles (remove title titles)))
	  (when (equal key (car entry)) (setq startline (org-current-line)))
	  (let ((s (concat
		    (if (memq (string-to-char (car entry)) '(?@ ?$)) "" "$")
		    (car entry) " = " (cdr entry) "\n")))
	    (remove-text-properties 0 (length s) '(face nil) s)
	    (insert s))))
      (when (eq org-table-use-standard-references t)
	(org-table-fedit-toggle-ref-type))
      (org-goto-line startline)
      (message "%s" (substitute-command-keys "\\<org-mode-map>\
Edit formulas, finish with `\\[org-ctrl-c-ctrl-c]' or `\\[org-edit-special]'.  \
See menu for more commands.")))))

(defun org-table-fedit-post-command ()
  (when (not (memq this-command '(lisp-complete-symbol)))
    (let ((win (selected-window)))
      (save-excursion
	(ignore-errors (org-table-show-reference))
	(select-window win)))))

(defun org-table-formula-to-user (s)
  "Convert a formula from internal to user representation."
  (if (eq org-table-use-standard-references t)
      (org-table-convert-refs-to-an s)
    s))

(defun org-table-formula-from-user (s)
  "Convert a formula from user to internal representation."
  (if org-table-use-standard-references
      (org-table-convert-refs-to-rc s)
    s))

(defun org-table-convert-refs-to-rc (s)
  "Convert spreadsheet references from A7 to @7$28.
Works for single references, but also for entire formulas and even the
full TBLFM line."
  (let ((start 0))
    (while (string-match "\\<\\([a-zA-Z]+\\)\\([0-9]+\\>\\|&\\)\\|\\(;[^\r\n:]+\\|\\<remote([^,)]*)\\)" s start)
      (cond
       ((match-end 3)
	;; format match, just advance
	(setq start (match-end 0)))
       ((and (> (match-beginning 0) 0)
	     (equal ?. (aref s (max (1- (match-beginning 0)) 0)))
	     (not (equal ?. (aref s (max (- (match-beginning 0) 2) 0)))))
	;; 3.e5 or something like this.
	(setq start (match-end 0)))
       ((or (> (- (match-end 1) (match-beginning 1)) 2)
	    ;; (member (match-string 1 s)
	    ;;	    '("arctan" "exp" "expm" "lnp" "log" "stir"))
	    )
	;; function name, just advance
	(setq start (match-end 0)))
       (t
	(setq start (match-beginning 0)
	      s (replace-match
		 (if (equal (match-string 2 s) "&")
		     (format "$%d" (org-letters-to-number (match-string 1 s)))
		   (format "@%d$%d"
			   (string-to-number (match-string 2 s))
			   (org-letters-to-number (match-string 1 s))))
		 t t s)))))
    s))

(defun org-table-convert-refs-to-an (s)
  "Convert spreadsheet references from to @7$28 to AB7.
Works for single references, but also for entire formulas and even the
full TBLFM line."
  (while (string-match "@\\([0-9]+\\)\\$\\([0-9]+\\)" s)
    (setq s (replace-match
	     (format "%s%d"
		     (org-number-to-letters
		      (string-to-number (match-string 2 s)))
		     (string-to-number (match-string 1 s)))
	     t t s)))
  (while (string-match "\\(^\\|[^0-9a-zA-Z]\\)\\$\\([0-9]+\\)" s)
    (setq s (replace-match (concat "\\1"
				   (org-number-to-letters
				    (string-to-number (match-string 2 s))) "&")
			   t nil s)))
  s)

(defun org-letters-to-number (s)
  "Convert a base 26 number represented by letters into an integer.
For example:  AB -> 28."
  (let ((n 0))
    (setq s (upcase s))
    (while (> (length s) 0)
      (setq n (+ (* n 26) (string-to-char s) (- ?A) 1)
	    s (substring s 1)))
    n))

(defun org-number-to-letters (n)
  "Convert an integer into a base 26 number represented by letters.
For example:  28 -> AB."
  (let ((s ""))
    (while (> n 0)
      (setq s (concat (char-to-string (+ (mod (1- n) 26) ?A)) s)
	    n (/ (1- n) 26)))
    s))

(defun org-table-time-string-to-seconds (s)
  "Convert a time string into numerical duration in seconds.
S can be a string matching either -?HH:MM:SS or -?HH:MM.
If S is a string representing a number, keep this number."
  (if (equal s "")
      s
    (let (hour minus min sec res)
      (cond
       ((and (string-match "\\(-?\\)\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\)" s))
	(setq minus (< 0 (length (match-string 1 s)))
	      hour (string-to-number (match-string 2 s))
	      min (string-to-number (match-string 3 s))
	      sec (string-to-number (match-string 4 s)))
	(if minus
	    (setq res (- (+ (* hour 3600) (* min 60) sec)))
	  (setq res (+ (* hour 3600) (* min 60) sec))))
       ((and (not (string-match org-ts-regexp-both s))
	     (string-match "\\(-?\\)\\([0-9]+\\):\\([0-9]+\\)" s))
	(setq minus (< 0 (length (match-string 1 s)))
	      hour (string-to-number (match-string 2 s))
	      min (string-to-number (match-string 3 s)))
	(if minus
	    (setq res (- (+ (* hour 3600) (* min 60))))
	  (setq res (+ (* hour 3600) (* min 60)))))
       (t (setq res (string-to-number s))))
      (number-to-string res))))

(defun org-table-time-seconds-to-string (secs &optional output-format)
  "Convert a number of seconds to a time string.
If OUTPUT-FORMAT is non-nil, return a number of days, hours,
minutes or seconds."
  (let* ((secs0 (abs secs))
	 (res
	  (cond ((eq output-format 'days)
		 (format "%.3f" (/ (float secs0) 86400)))
		((eq output-format 'hours)
		 (format "%.2f" (/ (float secs0) 3600)))
		((eq output-format 'minutes)
		 (format "%.1f" (/ (float secs0) 60)))
		((eq output-format 'seconds)
		 (format "%d" secs0))
		((eq output-format 'hh:mm)
		 ;; Ignore seconds
		 (substring (format-seconds
			     (if org-table-duration-hour-zero-padding
				 "%.2h:%.2m:%.2s" "%h:%.2m:%.2s")
			     secs0)
			    0 -3))
		(t (format-seconds
		    (if org-table-duration-hour-zero-padding
			"%.2h:%.2m:%.2s" "%h:%.2m:%.2s")
		    secs0)))))
    (if (< secs 0) (concat "-" res) res)))

(defun org-table-fedit-convert-buffer (function)
  "Convert all references in this buffer, using FUNCTION."
  (let ((origin (copy-marker (line-beginning-position))))
    (goto-char (point-min))
    (while (not (eobp))
      (insert (funcall function (buffer-substring (point) (line-end-position))))
      (delete-region (point) (line-end-position))
      (forward-line))
    (goto-char origin)
    (set-marker origin nil)))

(defun org-table-fedit-toggle-ref-type ()
  "Convert all references in the buffer from B3 to @3$2 and back."
  (interactive)
  (setq-local org-table-buffer-is-an (not org-table-buffer-is-an))
  (org-table-fedit-convert-buffer
   (if org-table-buffer-is-an
       'org-table-convert-refs-to-an 'org-table-convert-refs-to-rc))
  (message "Reference type switched to %s"
	   (if org-table-buffer-is-an "A1 etc" "@row$column")))

(defun org-table-fedit-ref-up ()
  "Shift the reference at point one row/hline up."
  (interactive)
  (org-table-fedit-shift-reference 'up))

(defun org-table-fedit-ref-down ()
  "Shift the reference at point one row/hline down."
  (interactive)
  (org-table-fedit-shift-reference 'down))

(defun org-table-fedit-ref-left ()
  "Shift the reference at point one field to the left."
  (interactive)
  (org-table-fedit-shift-reference 'left))

(defun org-table-fedit-ref-right ()
  "Shift the reference at point one field to the right."
  (interactive)
  (org-table-fedit-shift-reference 'right))

(defun org-table--rematch-and-replace (n &optional decr hline)
  "Re-match the group N, and replace it with the shifted reference."
  (or (match-end n) (user-error "Cannot shift reference in this direction"))
  (goto-char (match-beginning n))
  (and (looking-at (regexp-quote (match-string n)))
       (replace-match (org-table-shift-refpart (match-string 0) decr hline)
		      t t)))

(defun org-table-fedit-shift-reference (dir)
  (cond
   ((org-in-regexp "\\(\\<[a-zA-Z]\\)&")
    (if (memq dir '(left right))
	(org-table--rematch-and-replace 1 (eq dir 'left))
      (user-error "Cannot shift reference in this direction")))
   ((org-in-regexp "\\(\\<[a-zA-Z]\\{1,2\\}\\)\\([0-9]+\\)")
    ;; A B3-like reference
    (if (memq dir '(up down))
	(org-table--rematch-and-replace 2 (eq dir 'up))
      (org-table--rematch-and-replace 1 (eq dir 'left))))
   ((org-in-regexp
     "\\(@\\|\\.\\.\\)\\([-+]?\\(I+\\>\\|[0-9]+\\)\\)\\(\\$\\([-+]?[0-9]+\\)\\)?")
    ;; An internal reference
    (if (memq dir '(up down))
	(org-table--rematch-and-replace 2 (eq dir 'up) (match-end 3))
      (org-table--rematch-and-replace 5 (eq dir 'left))))))

(defun org-table-shift-refpart (ref &optional decr hline)
  "Shift a reference part REF.
If DECR is set, decrease the references row/column, else increase.
If HLINE is set, this may be a hline reference, it certainly is not
a translation reference."
  (save-match-data
    (let* ((sign (string-match "^[-+]" ref)) n)

      (if sign (setq sign (substring ref 0 1) ref (substring ref 1)))
      (cond
       ((and hline (string-match "^I+" ref))
	(setq n (string-to-number (concat sign (number-to-string (length ref)))))
	(setq n (+ n (if decr -1 1)))
	(if (= n 0) (setq n (+ n (if decr -1 1))))
	(if sign
	    (setq sign (if (< n 0) "-" "+") n (abs n))
	  (setq n (max 1 n)))
	(concat sign (make-string n ?I)))

       ((string-match "^[0-9]+" ref)
	(setq n (string-to-number (concat sign ref)))
	(setq n (+ n (if decr -1 1)))
	(if sign
	    (concat (if (< n 0) "-" "+") (number-to-string (abs n)))
	  (number-to-string (max 1 n))))

       ((string-match "^[a-zA-Z]+" ref)
	(org-number-to-letters
	 (max 1 (+ (org-letters-to-number ref) (if decr -1 1)))))

       (t (user-error "Cannot shift reference"))))))

(defun org-table-fedit-toggle-coordinates ()
  "Toggle the display of coordinates in the referenced table."
  (interactive)
  (let ((pos (marker-position org-pos)))
    (with-current-buffer (marker-buffer org-pos)
      (save-excursion
	(goto-char pos)
	(org-table-toggle-coordinate-overlays)))))

(defun org-table-fedit-finish (&optional arg)
  "Parse the buffer for formula definitions and install them.
With prefix ARG, apply the new formulas to the table."
  (interactive "P")
  (org-table-remove-rectangle-highlight)
  (when org-table-use-standard-references
    (org-table-fedit-convert-buffer 'org-table-convert-refs-to-rc)
    (setq org-table-buffer-is-an nil))
  (let ((pos org-pos)
	(sel-win org-selected-window)
	(source org-table--fedit-source)
	eql)
    (goto-char (point-min))
    (while (re-search-forward
	    "^\\(@[-+I<>0-9.$@]+\\|@?[0-9]+\\|\\$\\([a-zA-Z0-9]+\\|[<>]+\\)\\) *= *\\(.*\\(\n[ \t]+.*$\\)*\\)"
	    nil t)
      (let ((var (match-string 1))
	    (form (org-trim (match-string 3))))
	(unless (equal form "")
	  (while (string-match "[ \t]*\n[ \t]*" form)
	    (setq form (replace-match " " t t form)))
	  (when (assoc var eql)
	    (user-error "Double formulas for %s" var))
	  (push (cons var form) eql))))
    (set-window-configuration org-window-configuration)
    (select-window sel-win)
    (goto-char source)
    (org-table-store-formulas eql)
    (set-marker pos nil)
    (set-marker source nil)
    (kill-buffer "*Edit Formulas*")
    (if arg
	(org-table-recalculate 'all)
      (message "New formulas installed - press C-u C-c C-c to apply."))))

(defun org-table-fedit-abort ()
  "Abort editing formulas, without installing the changes."
  (interactive)
  (org-table-remove-rectangle-highlight)
  (let ((pos org-pos) (sel-win org-selected-window))
    (set-window-configuration org-window-configuration)
    (select-window sel-win)
    (goto-char pos)
    (move-marker pos nil)
    (message "Formula editing aborted without installing changes")))

(defun org-table-fedit-lisp-indent ()
  "Pretty-print and re-indent Lisp expressions in the Formula Editor."
  (interactive)
  (let ((pos (point)) beg end ind)
    (beginning-of-line 1)
    (cond
     ((looking-at "[ \t]")
      (goto-char pos)
      (call-interactively 'lisp-indent-line))
     ((looking-at "[$&@0-9a-zA-Z]+ *= *[^ \t\n']") (goto-char pos))
     ((not (fboundp 'pp-buffer))
      (user-error "Cannot pretty-print.  Command `pp-buffer' is not available"))
     ((looking-at "[$&@0-9a-zA-Z]+ *= *'(")
      (goto-char (- (match-end 0) 2))
      (setq beg (point))
      (setq ind (make-string (current-column) ?\ ))
      (condition-case nil (forward-sexp 1)
	(error
	 (user-error "Cannot pretty-print Lisp expression: Unbalanced parenthesis")))
      (setq end (point))
      (save-restriction
	(narrow-to-region beg end)
	(if (eq last-command this-command)
	    (progn
	      (goto-char (point-min))
	      (setq this-command nil)
	      (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
		(replace-match " ")))
	  (pp-buffer)
	  (untabify (point-min) (point-max))
	  (goto-char (1+ (point-min)))
	  (while (re-search-forward "^." nil t)
	    (beginning-of-line 1)
	    (insert ind))
	  (goto-char (point-max))
	  (org-delete-backward-char 1)))
      (goto-char beg))
     (t nil))))

(defun org-table-fedit-line-up ()
  "Move cursor one line up in the window showing the table."
  (interactive)
  (org-table-fedit-move 'previous-line))

(defun org-table-fedit-line-down ()
  "Move cursor one line down in the window showing the table."
  (interactive)
  (org-table-fedit-move 'next-line))

(defun org-table-fedit-move (command)
  "Move the cursor in the window showing the table.
Use COMMAND to do the motion, repeat if necessary to end up in a data line."
  (let ((org-table-allow-automatic-line-recalculation nil)
	(pos org-pos) (win (selected-window)) p)
    (select-window (get-buffer-window (marker-buffer org-pos)))
    (setq p (point))
    (call-interactively command)
    (while (and (org-at-table-p)
		(org-at-table-hline-p))
      (call-interactively command))
    (or (org-at-table-p) (goto-char p))
    (move-marker pos (point))
    (select-window win)))

(defun org-table-fedit-scroll (N)
  (interactive "p")
  (let ((other-window-scroll-buffer (marker-buffer org-pos)))
    (scroll-other-window N)))

(defun org-table-fedit-scroll-down (N)
  (interactive "p")
  (org-table-fedit-scroll (- N)))

(defun org-table-add-rectangle-overlay (beg end &optional face)
  "Add a new overlay."
  (let ((ov (make-overlay beg end)))
    (overlay-put ov 'face (or face 'secondary-selection))
    (push ov org-table-rectangle-overlays)))

(defun org-table-highlight-rectangle (&optional beg end face)
  "Highlight rectangular region in a table.
When buffer positions BEG and END are provided, use them to
delimit the region to highlight.  Otherwise, refer to point.  Use
FACE, when non-nil, for the highlight."
  (let* ((beg (or beg (point)))
	 (end (or end (point)))
	 (b (min beg end))
	 (e (max beg end))
	 (start-coordinates
	  (save-excursion
	    (goto-char b)
	    (cons (line-beginning-position) (org-table-current-column))))
	 (end-coordinates
	  (save-excursion
	    (goto-char e)
	    (cons (line-beginning-position) (org-table-current-column)))))
    (when (boundp 'org-show-positions)
      (setq org-show-positions (cons b (cons e org-show-positions))))
    (goto-char (car start-coordinates))
    (let ((column-start (min (cdr start-coordinates) (cdr end-coordinates)))
	  (column-end (max (cdr start-coordinates) (cdr end-coordinates)))
	  (last-row (car end-coordinates)))
      (while (<= (point) last-row)
	(when (looking-at org-table-dataline-regexp)
	  (org-table-goto-column column-start)
	  (skip-chars-backward "^|\n")
	  (let ((p (point)))
	    (org-table-goto-column column-end)
	    (skip-chars-forward "^|\n")
	    (org-table-add-rectangle-overlay p (point) face)))
	(forward-line)))
    (goto-char (car start-coordinates)))
  (add-hook 'before-change-functions #'org-table-remove-rectangle-highlight))

(defun org-table-remove-rectangle-highlight (&rest _ignore)
  "Remove the rectangle overlays."
  (unless org-inhibit-highlight-removal
    (remove-hook 'before-change-functions 'org-table-remove-rectangle-highlight)
    (mapc 'delete-overlay org-table-rectangle-overlays)
    (setq org-table-rectangle-overlays nil)))

(defvar-local org-table-coordinate-overlays nil
  "Collects the coordinate grid overlays, so that they can be removed.")

(defun org-table-overlay-coordinates ()
  "Add overlays to the table at point, to show row/column coordinates."
  (interactive)
  (mapc 'delete-overlay org-table-coordinate-overlays)
  (setq org-table-coordinate-overlays nil)
  (save-excursion
    (let ((id 0) (ih 0) hline eol str ov)
      (goto-char (org-table-begin))
      (while (org-at-table-p)
	(setq eol (point-at-eol))
	(setq ov (make-overlay (point-at-bol) (1+ (point-at-bol))))
	(push ov org-table-coordinate-overlays)
	(setq hline (looking-at org-table-hline-regexp))
	(setq str (if hline (format "I*%-2d" (setq ih (1+ ih)))
		    (format "%4d" (setq id (1+ id)))))
	(org-overlay-before-string ov str 'org-special-keyword 'evaporate)
	(when hline
	  (let ((ic 0))
	    (while (re-search-forward "[+|]\\(-+\\)" eol t)
	      (cl-incf ic)
	      (let* ((beg (1+ (match-beginning 0)))
		     (s1 (format "$%d" ic))
		     (s2 (org-number-to-letters ic))
		     (str (if (eq t org-table-use-standard-references) s2 s1))
		     (ov (make-overlay beg (+ beg (length str)))))
		(push ov org-table-coordinate-overlays)
		(org-overlay-display ov str 'org-special-keyword 'evaporate)))))
	(forward-line)))))

;;;###autoload
(defun org-table-toggle-coordinate-overlays ()
  "Toggle the display of Row/Column numbers in tables."
  (interactive)
  (setq org-table-overlay-coordinates (not org-table-overlay-coordinates))
  (message "Tables Row/Column numbers display turned %s"
	   (if org-table-overlay-coordinates "on" "off"))
  (when (and (org-at-table-p) org-table-overlay-coordinates)
    (org-table-align))
  (unless org-table-overlay-coordinates
    (mapc 'delete-overlay org-table-coordinate-overlays)
    (setq org-table-coordinate-overlays nil)))

;;;###autoload
(defun org-table-toggle-formula-debugger ()
  "Toggle the formula debugger in tables."
  (interactive)
  (setq org-table-formula-debug (not org-table-formula-debug))
  (message "Formula debugging has been turned %s"
	   (if org-table-formula-debug "on" "off")))

\f
;;; Columns Shrinking

(defun org-table--shrunk-field ()
  "Non-nil if current field is narrowed.
When non-nil, return the overlay narrowing the field."
  (cl-some (lambda (o)
	     (and (eq 'table-column-hide (overlay-get o 'org-overlay-type))
		  o))
	   (overlays-at (save-excursion
			  (skip-chars-forward (if (org-at-table-hline-p) "^+|"
						"^|")
					      (line-end-position))
			  (1- (point))))))

(defun org-table--list-shrunk-columns ()
  "List currently shrunk columns in table at point."
  (save-excursion
    ;; We really check shrunk columns in current row only.  It could
    ;; be wrong if all rows do not contain the same number of columns
    ;; (i.e. the table is not properly aligned).  As a consequence,
    ;; some columns may not be shrunk again upon aligning the table.
    ;;
    ;; For example, in the following table, cursor is on first row and
    ;; "<>" indicates a shrunk column.
    ;;
    ;; | |
    ;; | | <> |
    ;;
    ;; Aligning table from the first row will not shrink again the
    ;; second row, which was not visible initially.
    ;;
    ;; However, fixing it requires to check every row, which may be
    ;; slow on large tables.  Moreover, the hindrance of this
    ;; pathological case is very limited.
    (beginning-of-line)
    (search-forward "|")
    (let ((separator (if (org-at-table-hline-p) "+" "|"))
	  (column 1)
	  (shrunk (and (org-table--shrunk-field) (list 1)))
	  (end (line-end-position)))
      (while (search-forward separator end t)
	(cl-incf column)
	(when (org-table--shrunk-field) (push column shrunk)))
      (nreverse shrunk))))

(defun org-table--make-shrinking-overlay (start end display field &optional pre)
  "Create an overlay to shrink text between START and END.

Use string DISPLAY instead of the real text between the two
buffer positions.  FIELD is the real contents of the field, as
a string, or nil.  It is meant to be displayed upon moving the
mouse onto the overlay.

When optional argument PRE is non-nil, assume the overlay is
located at the beginning of the field, and prepend
`org-table-separator-space' to it.  Otherwise, concatenate
`org-table-shrunk-column-indicator' at its end.

Return the overlay."
  (let ((show-before-edit
	 (lambda (o &rest _)
	   ;; Removing one overlay removes all other overlays in the
	   ;; same column.
	   (mapc #'delete-overlay
		 (cdr (overlay-get o 'org-table-column-overlays)))))
	(o (make-overlay start end)))
    (overlay-put o 'insert-behind-hooks (list show-before-edit))
    (overlay-put o 'insert-in-front-hooks (list show-before-edit))
    (overlay-put o 'modification-hooks (list show-before-edit))
    (overlay-put o 'org-overlay-type 'table-column-hide)
    (when (stringp field) (overlay-put o 'help-echo field))
    ;; Make sure overlays stays on top of table coordinates overlays.
    ;; See `org-table-overlay-coordinates'.
    (overlay-put o 'priority 1)
    (let ((d (if pre (concat org-table-separator-space display)
	       (concat display org-table-shrunk-column-indicator))))
      (org-overlay-display o d 'org-table t))
    o))

(defun org-table--shrink-field (width align start end contents)
  "Shrink a table field to a specified width.

WIDTH is an integer representing the number of characters to
display, in addition to `org-table-shrunk-column-indicator'.
ALIGN is the alignment of the current column, as either \"l\",
\"c\" or \"r\".  START and END are, respectively, the beginning
and ending positions of the field.  CONTENTS is its trimmed
contents, as a string, or `hline' for table rules.

Real field is hidden under one or two overlays.  They have the
following properties:

  `org-overlay-type'

    Set to `table-column-hide'.  Used to identify overlays
    responsible for shrinking columns in a table.

  `org-table-column-overlays'

    It is a list with the pattern (siblings . COLUMN-OVERLAYS)
    where COLUMN-OVERLAYS is the list of all overlays hiding the
    same column.

Whenever the text behind or next to the overlay is modified, all
the overlays in the column are deleted, effectively displaying
the column again.

Return a list of overlays hiding the field, or nil if field is
already hidden."
  (cond
   ((= start end) nil)			;no field to narrow
   ((org-table--shrunk-field) nil)	;already shrunk
   ((= 0 width)				;shrink to one character
    (list (org-table--make-shrinking-overlay
	   start end "" (if (eq 'hline contents) "" contents))))
   ((eq contents 'hline)
    (list (org-table--make-shrinking-overlay
	   start end (make-string (1+ width) ?-) "")))
   ((equal contents "")			;no contents to hide
    (list
     (let ((w (org-string-width (buffer-substring start end)))
	   ;; We really want WIDTH + 2 whitespace, to include blanks
	   ;; around fields.
	   (full (+ 2 width)))
       (if (<= w full)
	   (org-table--make-shrinking-overlay
	    (1- end) end (make-string (- full w) ?\s) "")
	 (org-table--make-shrinking-overlay (- end (- w full) 1) end "" "")))))
   (t
    ;; If the field is not empty, display exactly WIDTH characters.
    ;; It can mean to partly hide the field, or extend it with virtual
    ;; blanks.  To that effect, we use one or two overlays.  The
    ;; first, optional, one may add or hide white spaces before the
    ;; contents of the field.  The other, mandatory, one cuts the
    ;; field or displays white spaces at the end of the field.  It
    ;; also always displays `org-table-shrunk-column-indicator'.
    (let* ((lead (org-with-point-at start (skip-chars-forward " ")))
	   (trail (org-with-point-at end (abs (skip-chars-backward " "))))
	   (contents-width (org-string-width
			    (buffer-substring (+ start lead) (- end trail)))))
      (cond
       ;; Contents are too large to fit in WIDTH character.  Limit, if
       ;; possible, blanks at the beginning of the field to a single
       ;; white space, and cut the field at an appropriate location.
       ((<= width contents-width)
	(let ((pre
	       (and (> lead 0)
		    (org-table--make-shrinking-overlay
		     start (+ start lead) "" contents t)))
	      (post
	       (org-table--make-shrinking-overlay
		;; Find cut location so that WIDTH characters are
		;; visible using dichotomy.
		(let* ((begin (+ start lead))
		       (lower begin)
		       (upper (1- end))
		       ;; Compensate the absence of leading space,
		       ;; thus preserving alignment.
		       (width (if (= lead 0) (1+ width) width)))
		  (catch :exit
		    (while (> (- upper lower) 1)
		      (let ((mean (+ (ash lower -1)
				     (ash upper -1)
				     (logand lower upper 1))))
			(pcase (org-string-width (buffer-substring begin mean))
			  ((pred (= width)) (throw :exit mean))
			  ((pred (< width)) (setq upper mean))
			  (_ (setq lower mean)))))
		    upper))
		end "" contents)))
	  (if pre (list pre post) (list post))))
       ;; Contents fit it WIDTH characters.  First compute number of
       ;; white spaces needed on each side of contents, then expand or
       ;; compact blanks on each side of the field in order to
       ;; preserve width and obey to alignment constraints.
       (t
	(let* ((required (- width contents-width))
	       (before
		(pcase align
		  ;; Compensate the absence of leading space, thus
		  ;; preserving alignment.
		  ((guard (= lead 0)) -1)
		  ("l" 0)
		  ("r" required)
		  ("c" (/ required 2))))
	       (after (- required before))
	       (pre
		(pcase (1- lead)
		  ((or (guard (= lead 0)) (pred (= before))) nil)
		  ((pred (< before))
		   (org-table--make-shrinking-overlay
		    start (+ start (- lead before)) "" contents t))
		  (_
		   (org-table--make-shrinking-overlay
		    start (1+ start)
		    (make-string (- before (1- lead)) ?\s)
		    contents t))))
	       (post
		(pcase (1- trail)
		  ((pred (= after))
		   (org-table--make-shrinking-overlay (1- end) end "" contents))
		  ((pred (< after))
		   (org-table--make-shrinking-overlay
		    (+ after (- end trail)) end "" contents))
		  (_
		   (org-table--make-shrinking-overlay
		    (1- end) end
		    (make-string (- after (1- trail)) ?\s)
		    contents)))))
	  (if pre (list pre post) (list post)))))))))

(defun org-table--read-column-selection (select max)
  "Read column selection select as a list of numbers.

SELECT is a string containing column ranges, separated by white
space characters, see `org-table-hide-column' for details.  MAX
is the maximum column number.

Return value is a sorted list of numbers.  Ignore any number
outside of the [1;MAX] range."
  (catch :all
    (sort
     (delete-dups
      (cl-mapcan
       (lambda (s)
	 (cond
	  ((member s '("-" "1-")) (throw :all (number-sequence 1 max)))
	  ((string-match-p "\\`[0-9]+\\'" s)
	   (let ((n (string-to-number s)))
	     (and (> n 0) (<= n max) (list n))))
	  ((string-match "\\`\\([0-9]+\\)?-\\([0-9]+\\)?\\'" s)
	   (let ((n (match-string 1 s))
		 (m (match-string 2 s)))
	     (number-sequence (if n (max 1 (string-to-number n))
				1)
			      (if m (min max (string-to-number m))
				max))))
	  (t nil)))			;invalid specification
       (split-string select)))
     #'<)))

(defun org-table--shrink-columns (columns beg end)
  "Shrink COLUMNS in a table.
COLUMNS is a sorted list of column numbers.  BEG and END are,
respectively, the beginning position and the end position of the
table."
  (org-with-wide-buffer
   (org-font-lock-ensure beg end)
   (dolist (c columns)
     (goto-char beg)
     (let ((align nil)
	   (width nil)
	   (fields nil))
       (while (< (point) end)
	 (catch :continue
	   (let* ((hline? (org-at-table-hline-p))
		  (separator (if hline? "+" "|")))
	     ;; Move to COLUMN.
	     (search-forward "|")
	     (or (= c 1)		;already there
		 (search-forward separator (line-end-position) t (1- c))
		 (throw :continue nil)) ;skip invalid columns
	     ;; Extract boundaries and contents from current field.
	     ;; Also set the column's width if we encounter a width
	     ;; cookie for the first time.
	     (let* ((start (point))
		    (end (progn
			   (skip-chars-forward (concat "^|" separator)
					       (line-end-position))
			   (point)))
		    (contents (if hline? 'hline
				(org-trim (buffer-substring start end)))))
	       (push (list start end contents) fields)
	       (when (and (not hline?)
			  (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)>\\'"
					contents))
		 (unless align (setq align (match-string 1 contents)))
		 (unless width
		   (setq width (string-to-number (match-string 2 contents))))))))
	 (forward-line))
       ;; Link overlays for current field to the other overlays in the
       ;; same column.
       (let ((chain (list 'siblings)))
	 (dolist (field fields)
	   (dolist (new (apply #'org-table--shrink-field
			       (or width 0) (or align "l") field))
	     (push new (cdr chain))
	     (overlay-put new 'org-table-column-overlays chain))))))))

;;;###autoload
(defun org-table-toggle-column-width (&optional arg)
  "Shrink or expand current column in an Org table.

If a width cookie specifies a width W for the column, the first
W visible characters are displayed.  Otherwise, the column is
shrunk to a single character.

When point is before the first column or after the last one, ask
for the columns to shrink or expand, as a list of ranges.
A column range can be one of the following patterns:

  N    column N only
  N-M  every column between N and M (both inclusive)
  N-   every column between N (inclusive) and the last column
  -M   every column between the first one and M (inclusive)
  -    every column

When optional argument ARG is a string, use it as white space
separated list of column ranges.

When called with `\\[universal-argument]' prefix, call \
`org-table-shrink', i.e.,
shrink columns with a width cookie and expand the others.

When called with `\\[universal-argument] \\[universal-argument]' \
prefix, expand all columns."
  (interactive "P")
  (unless (org-at-table-p) (user-error "Not in a table"))
  (let* ((begin (org-table-begin))
	 (end (org-table-end))
	 ;; Compute an upper bound for the number of columns.
	 ;; Nonexistent columns are ignored anyway.
	 (max-columns (/ (- (line-end-position) (line-beginning-position)) 2))
	 (shrunk (org-table--list-shrunk-columns))
	 (columns
	  (pcase arg
	    (`nil
	     (if (save-excursion
		   (skip-chars-backward "^|" (line-beginning-position))
		   (or (bolp) (looking-at-p "[ \t]*$")))
		 ;; Point is either before first column or past last
		 ;; one.  Ask for columns to operate on.
		 (org-table--read-column-selection
		  (read-string "Column ranges (e.g. 2-4 6-): ")
		  max-columns)
	       (list (org-table-current-column))))
	    ((pred stringp) (org-table--read-column-selection arg max-columns))
	    ((or `(4) `(16)) nil)
	    (_ (user-error "Invalid argument: %S" arg)))))
    (pcase arg
      (`(4) (org-table-shrink begin end))
      (`(16) (org-table-expand begin end))
      (_
       (org-table-expand begin end)
       (org-table--shrink-columns
	(cl-set-exclusive-or columns shrunk) begin end)))))

;;;###autoload
(defun org-table-shrink (&optional begin end)
  "Shrink all columns with a width cookie in the table at point.

Columns without a width cookie are expanded.

Optional arguments BEGIN and END, when non-nil, specify the
beginning and end position of the current table."
  (interactive)
  (unless (or begin (org-at-table-p)) (user-error "Not at a table"))
  (org-with-wide-buffer
   (let ((begin (or begin (org-table-begin)))
	 (end (or end (org-table-end)))
	 (regexp "|[ \t]*<[lrc]?[0-9]+>[ \t]*\\(|\\|$\\)")
	 (columns))
     (goto-char begin)
     (while (re-search-forward regexp end t)
       (goto-char (match-beginning 1))
       (cl-pushnew (org-table-current-column) columns))
     (org-table-expand begin end)
     ;; Make sure invisible characters in the table are at the right
     ;; place since column widths take them into account.
     (org-font-lock-ensure begin end)
     (org-table--shrink-columns (sort columns #'<) begin end))))

;;;###autoload
(defun org-table-expand (&optional begin end)
  "Expand all columns in the table at point.
Optional arguments BEGIN and END, when non-nil, specify the
beginning and end position of the current table."
  (interactive)
  (unless (or begin (org-at-table-p)) (user-error "Not at a table"))
  (org-with-wide-buffer
   (let ((begin (or begin (org-table-begin)))
	 (end (or end (org-table-end))))
     (remove-overlays begin end 'org-overlay-type 'table-column-hide))))

\f
;;; Generic Tools

;;;###autoload
(defun org-table-map-tables (f &optional quietly)
  "Apply function F to the start of all tables in the buffer."
  (org-with-point-at 1
    (while (re-search-forward org-table-line-regexp nil t)
      (let ((table (org-element-lineage (org-element-at-point) '(table) t)))
	(when table
	  (unless quietly
	    (message "Mapping tables: %d%%"
		     (floor (* 100.0 (point)) (buffer-size))))
	  (goto-char (org-element-property :post-affiliated table))
	  (let ((end (copy-marker (org-element-property :end table))))
	    (unwind-protect
		(progn (funcall f) (goto-char end))
	      (set-marker end nil)))))))
  (unless quietly (message "Mapping tables: done")))

;;;###autoload
(defun org-table-export (&optional file format)
  "Export table to a file, with configurable format.
Such a file can be imported into usual spreadsheet programs.

FILE can be the output file name.  If not given, it will be taken
from a TABLE_EXPORT_FILE property in the current entry or higher
up in the hierarchy, or the user will be prompted for a file
name.  FORMAT can be an export format, of the same kind as it
used when `-mode' sends a table in a different format.

The command suggests a format depending on TABLE_EXPORT_FORMAT,
whether it is set locally or up in the hierarchy, then on the
extension of the given file name, and finally on the variable
`org-table-export-default-format'."
  (interactive)
  (unless (org-at-table-p) (user-error "No table at point"))
  (org-table-align)	       ; Make sure we have everything we need.
  (let ((file (or file (org-entry-get (point) "TABLE_EXPORT_FILE" t))))
    (unless file
      (setq file (read-file-name "Export table to: "))
      (unless (or (not (file-exists-p file))
		  (y-or-n-p (format "Overwrite file %s? " file)))
	(user-error "File not written")))
    (when (file-directory-p file)
      (user-error "This is a directory path, not a file"))
    (when (and (buffer-file-name (buffer-base-buffer))
	       (file-equal-p
		(file-truename file)
		(file-truename (buffer-file-name (buffer-base-buffer)))))
      (user-error "Please specify a file name that is different from current"))
    (let ((fileext (concat (file-name-extension file) "$"))
	  (format (or format (org-entry-get (point) "TABLE_EXPORT_FORMAT" t))))
      (unless format
	(let* ((formats '("orgtbl-to-tsv" "orgtbl-to-csv" "orgtbl-to-latex"
			  "orgtbl-to-html" "orgtbl-to-generic"
			  "orgtbl-to-texinfo" "orgtbl-to-orgtbl"
			  "orgtbl-to-unicode"))
	       (deffmt-readable
		 (replace-regexp-in-string
		  "\t" "\\t"
		  (replace-regexp-in-string
		   "\n" "\\n"
		   (or (car (delq nil
				  (mapcar
				   (lambda (f)
				     (and (string-match-p fileext f) f))
				   formats)))
		       org-table-export-default-format)
		   t t) t t)))
	  (setq format
		(org-completing-read
		 "Format: " formats nil nil deffmt-readable))))
      (if (string-match "\\([^ \t\r\n]+\\)\\( +.*\\)?" format)
	  (let ((transform (intern (match-string 1 format)))
		(params (and (match-end 2)
			     (read (concat "(" (match-string 2 format) ")"))))
		(table (org-table-to-lisp
			(buffer-substring-no-properties
			 (org-table-begin) (org-table-end)))))
	    (unless (fboundp transform)
	      (user-error "No such transformation function %s" transform))
	    (let (buf)
	      (with-current-buffer (find-file-noselect file)
		(setq buf (current-buffer))
		(erase-buffer)
		(fundamental-mode)
		(insert (funcall transform table params) "\n")
		(save-buffer))
	      (kill-buffer buf))
	    (message "Export done."))
	(user-error "TABLE_EXPORT_FORMAT invalid")))))

;;;###autoload
(defun org-table--align-field (field width align)
  "Format FIELD according to column WIDTH and alignment ALIGN.
FIELD is a string.  WIDTH is a number.  ALIGN is either \"c\",
\"l\" or\"r\"."
  (let* ((spaces (- width (org-string-width field)))
	 (prefix (pcase align
		   ("l" "")
		   ("r" (make-string spaces ?\s))
		   ("c" (make-string (/ spaces 2) ?\s))))
	 (suffix (make-string (- spaces (length prefix)) ?\s)))
    (concat org-table-separator-space
	    prefix
	    field
	    suffix
	    org-table-separator-space)))

(defun org-table-align ()
  "Align the table at point by aligning all vertical bars."
  (interactive)
  (let ((beg (org-table-begin))
	(end (copy-marker (org-table-end))))
    (org-table-save-field
     ;; Make sure invisible characters in the table are at the right
     ;; place since column widths take them into account.
     (org-font-lock-ensure beg end)
     (move-marker org-table-aligned-begin-marker beg)
     (move-marker org-table-aligned-end-marker end)
     (goto-char beg)
     (org-table-with-shrunk-columns
      (let* ((indent (progn (looking-at "[ \t]*") (match-string 0)))
	     ;; Table's rows as lists of fields.  Rules are replaced
	     ;; by nil.  Trailing spaces are removed.
	     (fields (mapcar
		      (lambda (l)
			(and (not (string-match-p org-table-hline-regexp l))
			     (org-split-string l "[ \t]*|[ \t]*")))
		      (split-string (buffer-substring beg end) "\n" t)))
	     ;; Compute number of columns.  If the table contains no
	     ;; field, create a default table and bail out.
	     (columns-number
	      (if fields (apply #'max (mapcar #'length fields))
		(kill-region beg end)
		(org-table-create org-table-default-size)
		(user-error "Empty table - created default table")))
	     (widths nil)
	     (alignments nil))
	;; Compute alignment and width for each column.
	(dotimes (i columns-number)
	  (let* ((max-width 1)
		 (fixed-align? nil)
		 (numbers 0)
		 (non-empty 0))
	    (dolist (row fields)
	      (let ((cell (or (nth i row) "")))
		(setq max-width (max max-width (org-string-width cell)))
		(cond (fixed-align? nil)
		      ((equal cell "") nil)
		      ((string-match "\\`<\\([lrc]\\)[0-9]*>\\'" cell)
		       (setq fixed-align? (match-string 1 cell)))
		      (t
		       (cl-incf non-empty)
		       (when (string-match-p org-table-number-regexp cell)
			 (cl-incf numbers))))))
	    (push max-width widths)
	    (push (cond
		   (fixed-align?)
		   ((>= numbers (* org-table-number-fraction non-empty)) "r")
		   (t "l"))
		  alignments)))
	(setq widths (nreverse widths))
	(setq alignments (nreverse alignments))
	;; Store alignment of this table, for later editing of single
	;; fields.
	(setq org-table-last-alignment alignments)
	(setq org-table-last-column-widths widths)
	;; Build new table rows.  Only replace rows that actually
	;; changed.
	(dolist (row fields)
	  (let ((previous (buffer-substring (point) (line-end-position)))
		(new
		 (format "%s|%s|"
			 indent
			 (if (null row)	;horizontal rule
			     (mapconcat (lambda (w) (make-string (+ 2 w) ?-))
					widths
					"+")
			   (let ((cells	;add missing fields
				  (append row
					  (make-list (- columns-number
							(length row))
						     ""))))
			     (mapconcat #'identity
					(cl-mapcar #'org-table--align-field
						   cells
						   widths
						   alignments)
					"|"))))))
	    (if (equal new previous)
		(forward-line)
	      (insert new "\n")
	      (delete-region (point) (line-beginning-position 2)))))
	(set-marker end nil)
	(when org-table-overlay-coordinates (org-table-overlay-coordinates))
	(setq org-table-may-need-update nil))))))

;;;###autoload
(defun org-table-justify-field-maybe (&optional new)
  "Justify the current field, text to left, number to right.
Optional argument NEW may specify text to replace the current field content."
  (cond
   ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
   ((org-at-table-hline-p))
   ((and (not new)
	 (or (not (eq (marker-buffer org-table-aligned-begin-marker)
		      (current-buffer)))
	     (< (point) org-table-aligned-begin-marker)
	     (>= (point) org-table-aligned-end-marker)))
    ;; This is not the same table, force a full re-align.
    (setq org-table-may-need-update t))
   (t
    ;; Realign the current field, based on previous full realign.
    (let ((pos (point))
	  (col (org-table-current-column)))
      (when (> col 0)
	(skip-chars-backward "^|")
	(if (not (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)"))
	    (setq org-table-may-need-update t)
	  (let* ((align (nth (1- col) org-table-last-alignment))
		 (width (nth (1- col) org-table-last-column-widths))
		 (cell (match-string 0))
		 (field (match-string 1))
		 (properly-closed? (/= (match-beginning 2) (match-end 2)))
		 (new-cell
		  (save-match-data
		    (cond (org-table-may-need-update
			   (format " %s |" (or new field)))
			  ((not properly-closed?)
			   (setq org-table-may-need-update t)
			   (format " %s |" (or new field)))
			  ((not new)
			   (concat (org-table--align-field field width align)
				   "|"))
			  ((<= (org-string-width new) width)
			   (concat (org-table--align-field new width align)
				   "|"))
			  (t
			   (setq org-table-may-need-update t)
			   (format " %s |" new))))))
	    (unless (equal new-cell cell)
	      (let (org-table-may-need-update)
		(replace-match new-cell t t)))
	    (goto-char pos))))))))

;;;###autoload
(defun org-table-sort-lines
    (&optional with-case sorting-type getkey-func compare-func interactive?)
  "Sort table lines according to the column at point.

The position of point indicates the column to be used for
sorting, and the range of lines is the range between the nearest
horizontal separator lines, or the entire table of no such lines
exist.  If point is before the first column, you will be prompted
for the sorting column.  If there is an active region, the mark
specifies the first line and the sorting column, while point
should be in the last line to be included into the sorting.

The command then prompts for the sorting type which can be
alphabetically, numerically, or by time (as given in a time stamp
in the field, or as a HH:MM value).  Sorting in reverse order is
also possible.

With prefix argument WITH-CASE, alphabetic sorting will be case-sensitive
if the locale allows for it.

If SORTING-TYPE is specified when this function is called from a Lisp
program, no prompting will take place.  SORTING-TYPE must be a character,
any of (?a ?A ?n ?N ?t ?T ?f ?F) where the capital letters indicate that
sorting should be done in reverse order.

If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
a function to be called to extract the key.  It must return a value
that is compatible with COMPARE-FUNC, the function used to compare
entries.

A non-nil value for INTERACTIVE? is used to signal that this
function is being called interactively."
  (interactive (list current-prefix-arg nil nil nil t))
  (when (org-region-active-p) (goto-char (region-beginning)))
  ;; Point must be either within a field or before a data line.
  (save-excursion
    (skip-chars-backward " \t")
    (when (bolp) (search-forward "|" (line-end-position) t))
    (org-table-check-inside-data-field))
  ;; Set appropriate case sensitivity and column used for sorting.
  (let ((column (let ((c (org-table-current-column)))
		  (cond ((> c 0) c)
			(interactive?
			 (read-number "Use column N for sorting: "))
			(t 1))))
	(sorting-type
	 (or sorting-type
	     (read-char-exclusive "Sort Table: [a]lphabetic, [n]umeric, \
\[t]ime, [f]unc.  A/N/T/F means reversed: ")))
	(start (org-table-begin))
	(end (org-table-end)))
    (save-restriction
      ;; Narrow buffer to appropriate sorting area.
      (if (org-region-active-p)
	  (progn (goto-char (region-beginning))
		 (narrow-to-region
		  (point)
		  (save-excursion (goto-char (region-end))
				  (line-beginning-position 2))))
	(narrow-to-region
	 (save-excursion
	   (if (re-search-backward org-table-hline-regexp start t)
	       (line-beginning-position 2)
	     start))
	 (if (save-excursion (re-search-forward org-table-hline-regexp end t))
	     (match-beginning 0)
	   end)))
      ;; Determine arguments for `sort-subr'.  Also record original
      ;; position.  `org-table-save-field' cannot help here since
      ;; sorting is too much destructive.
      (let* ((coordinates
	      (cons (count-lines (point-min) (line-beginning-position))
		    (current-column)))
	     (extract-key-from-field
	      ;; Function to be called on the contents of the field
	      ;; used for sorting in the current row.
	      (cl-case sorting-type
		((?n ?N) #'string-to-number)
		((?a ?A) #'org-sort-remove-invisible)
		((?t ?T)
		 (lambda (f)
		   (cond ((string-match org-ts-regexp-both f)
			  (float-time
			   (org-time-string-to-time (match-string 0 f))))
			 ((org-duration-p f) (org-duration-to-minutes f))
			 ((string-match "\\<[0-9]+:[0-9]\\{2\\}\\>" f)
			  (org-duration-to-minutes (match-string 0 f)))
			 (t 0))))
		((?f ?F)
		 (or getkey-func
		     (and interactive?
			  (org-read-function "Function for extracting keys: "))
		     (error "Missing key extractor to sort rows")))
		(t (user-error "Invalid sorting type `%c'" sorting-type))))
	     (predicate
	      (cl-case sorting-type
		((?n ?N ?t ?T) #'<)
		((?a ?A) (if with-case #'org-string-collate-lessp
			   (lambda (s1 s2) (org-string-collate-lessp s1 s2 nil t))))
		((?f ?F)
		 (or compare-func
		     (and interactive?
			  (org-read-function
			   "Function for comparing keys (empty for default \
`sort-subr' predicate): "
			   'allow-empty))))))
	     (shrunk-columns (remq column (org-table--list-shrunk-columns))))
	(goto-char (point-min))
	(sort-subr (memq sorting-type '(?A ?N ?T ?F))
		   (lambda ()
		     (forward-line)
		     (while (and (not (eobp))
				 (not (looking-at org-table-dataline-regexp)))
		       (forward-line)))
		   #'end-of-line
		   (lambda ()
		     (funcall extract-key-from-field
			      (org-trim (org-table-get-field column))))
		   nil
		   predicate)
	;; Hide all columns but the one being sorted.
	(org-table--shrink-columns shrunk-columns start end)
	;; Move back to initial field.
	(forward-line (car coordinates))
	(move-to-column (cdr coordinates))))))

(defun org-table-transpose-table-at-point ()
  "Transpose Org table at point and eliminate hlines.
So a table like

| 1 | 2 | 4 | 5 |
|---+---+---+---|
| a | b | c | d |
| e | f | g | h |

will be transposed as

| 1 | a | e |
| 2 | b | f |
| 4 | c | g |
| 5 | d | h |

Note that horizontal lines disappear."
  (interactive)
  (let* ((table (delete 'hline (org-table-to-lisp)))
	 (dline_old (org-table-current-line))
	 (col_old (org-table-current-column))
	 (contents (mapcar (lambda (_)
			     (let ((tp table))
			       (mapcar
				(lambda (_)
				  (prog1
				      (pop (car tp))
				    (setq tp (cdr tp))))
				table)))
			   (car table))))
    (goto-char (org-table-begin))
    (re-search-forward "|")
    (backward-char)
    (delete-region (point) (org-table-end))
    (insert (mapconcat
	     (lambda(x)
	       (concat "| " (mapconcat 'identity x " | " ) "  |\n" ))
	     contents ""))
    (org-table-goto-line col_old)
    (org-table-goto-column dline_old))
  (org-table-align))

;;;###autoload
(defun org-table-wrap-region (arg)
  "Wrap several fields in a column like a paragraph.
This is useful if you'd like to spread the contents of a field over several
lines, in order to keep the table compact.

If there is an active region, and both point and mark are in the same column,
the text in the column is wrapped to minimum width for the given number of
lines.  Generally, this makes the table more compact.  A prefix ARG may be
used to change the number of desired lines.  For example, \
`C-2 \\[org-table-wrap-region]'
formats the selected text to two lines.  If the region was longer than two
lines, the remaining lines remain empty.  A negative prefix argument reduces
the current number of lines by that amount.  The wrapped text is pasted back
into the table.  If you formatted it to more lines than it was before, fields
further down in the table get overwritten - so you might need to make space in
the table first.

If there is no region, the current field is split at the cursor position and
the text fragment to the right of the cursor is prepended to the field one
line down.

If there is no region, but you specify a prefix ARG, the current field gets
blank, and the content is appended to the field above."
  (interactive "P")
  (org-table-check-inside-data-field)
  (if (org-region-active-p)
      ;; There is a region: fill as a paragraph.
      (let ((start (region-beginning)))
	(org-table-cut-region (region-beginning) (region-end))
	(when (> (length (car org-table-clip)) 1)
	  (user-error "Region must be limited to single column"))
	(let ((nlines (cond ((not arg) (length org-table-clip))
			    ((< arg 1) (+ (length org-table-clip) arg))
			    (t arg))))
	  (setq org-table-clip
		(mapcar #'list
			(org-wrap (mapconcat #'car org-table-clip " ")
				  nil
				  nlines))))
	(goto-char start)
	(org-table-paste-rectangle))
    ;; No region, split the current field at point.
    (unless (org-get-alist-option org-M-RET-may-split-line 'table)
      (skip-chars-forward "^\r\n|"))
    (cond
     (arg				; Combine with field above.
      (let ((s (org-table-blank-field))
	    (col (org-table-current-column)))
	(forward-line -1)
	(while (org-at-table-hline-p) (forward-line -1))
	(org-table-goto-column col)
	(skip-chars-forward "^|")
	(skip-chars-backward " ")
	(insert " " (org-trim s))
	(org-table-align)))
     ((looking-at "\\([^|]+\\)+|")	; Split field.
      (let ((s (match-string 1)))
	(replace-match " |")
	(goto-char (match-beginning 0))
	(org-table-next-row)
	(insert (org-trim s) " ")
	(org-table-align)))
     (t (org-table-next-row)))))

(defun org-table--number-for-summing (s)
  (let (n)
    (if (string-match "^ *|? *" s)
	(setq s (replace-match "" nil nil s)))
    (if (string-match " *|? *$" s)
	(setq s (replace-match "" nil nil s)))
    (setq n (string-to-number s))
    (cond
     ((and (string-match "0" s)
	   (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
     ((string-match "\\`[ \t]+\\'" s) nil)
     ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
      (let ((h (string-to-number (or (match-string 1 s) "0")))
	    (m (string-to-number (or (match-string 2 s) "0")))
	    (s (string-to-number (or (match-string 4 s) "0"))))
	(if (boundp 'org-timecnt) (setq org-timecnt (1+ org-timecnt)))
	(* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
     ((equal n 0) nil)
     (t n))))

;;;###autoload
(defun org-table-sum (&optional beg end nlast)
  "Sum numbers in region of current table column.
The result will be displayed in the echo area, and will be available
as kill to be inserted with \\[yank].

If there is an active region, it is interpreted as a rectangle and all
numbers in that rectangle will be summed.  If there is no active
region and point is located in a table column, sum all numbers in that
column.

If at least one number looks like a time HH:MM or HH:MM:SS, all other
numbers are assumed to be times as well (in decimal hours) and the
numbers are added as such.

If NLAST is a number, only the NLAST fields will actually be summed."
  (interactive)
  (save-excursion
    (let (col (org-timecnt 0) diff h m s org-table-clip)
      (cond
       ((and beg end))			; beg and end given explicitly
       ((org-region-active-p)
	(setq beg (region-beginning) end (region-end)))
       (t
	(setq col (org-table-current-column))
	(goto-char (org-table-begin))
	(unless (re-search-forward "^[ \t]*|[^-]" nil t)
	  (user-error "No table data"))
	(org-table-goto-column col)
	(setq beg (point))
	(goto-char (org-table-end))
	(unless (re-search-backward "^[ \t]*|[^-]" nil t)
	  (user-error "No table data"))
	(org-table-goto-column col)
	(setq end (point))))
      (let* ((items (apply 'append (org-table-copy-region beg end)))
	     (items1 (cond ((not nlast) items)
			   ((>= nlast (length items)) items)
			   (t (setq items (reverse items))
			      (setcdr (nthcdr (1- nlast) items) nil)
			      (nreverse items))))
	     (numbers (delq nil (mapcar #'org-table--number-for-summing
					items1)))
	     (res (apply '+ numbers))
	     (sres (if (= org-timecnt 0)
		       (number-to-string res)
		     (setq diff (* 3600 res)
			   h (floor diff 3600) diff (mod diff 3600)
			   m (floor diff 60) diff (mod diff 60)
			   s diff)
		     (format "%.0f:%02.0f:%02.0f" h m s))))
	(kill-new sres)
	(when (called-interactively-p 'interactive)
	  (message (substitute-command-keys
		    (format "Sum of %d items: %-20s     \
\(\\[yank] will insert result into buffer)"
			    (length numbers)
			    sres))))
	sres))))

;;;###autoload
(defun org-table-analyze ()
  "Analyze table at point and store results.

This function sets up the following dynamically scoped variables:

 `org-table-column-name-regexp',
 `org-table-column-names',
 `org-table-current-begin-pos',
 `org-table-current-line-types',
 `org-table-current-ncol',
 `org-table-dlines',
 `org-table-hlines',
 `org-table-local-parameters',
 `org-table-named-field-locations'."
  (let ((beg (org-table-begin))
	(end (org-table-end)))
    (save-excursion
      (goto-char beg)
      ;; Extract column names.
      (setq org-table-column-names nil)
      (when (save-excursion
	      (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t))
	(let ((c 1))
	  (dolist (name (org-split-string (match-string 1) " *| *"))
	    (cl-incf c)
	    (when (string-match "\\`[a-zA-Z][_a-zA-Z0-9]*\\'" name)
	      (push (cons name (int-to-string c)) org-table-column-names)))))
      (setq org-table-column-names (nreverse org-table-column-names))
      (setq org-table-column-name-regexp
	    (format "\\$\\(%s\\)\\>"
		    (regexp-opt (mapcar #'car org-table-column-names) t)))
      ;; Extract local parameters.
      (setq org-table-local-parameters nil)
      (save-excursion
	(while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
	  (dolist (field (org-split-string (match-string 1) " *| *"))
	    (when (string-match
		   "\\`\\([a-zA-Z][_a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
	      (push (cons (match-string 1 field) (match-string 2 field))
		    org-table-local-parameters)))))
      ;; Update named fields locations.  We minimize `count-lines'
      ;; processing by storing last known number of lines in LAST.
      (setq org-table-named-field-locations nil)
      (save-excursion
	(let ((last (cons (point) 0)))
	  (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
	    (let ((c (match-string 1))
		  (fields (org-split-string (match-string 2) " *| *")))
	      (save-excursion
		(forward-line (if (equal c "_") 1 -1))
		(let ((fields1
		       (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
			    (org-split-string (match-string 1) " *| *")))
		      (line (cl-incf (cdr last) (count-lines (car last) (point))))
		      (col 1))
		  (setcar last (point))	; Update last known position.
		  (while (and fields fields1)
		    (let ((field (pop fields))
			  (v (pop fields1)))
		      (cl-incf col)
		      (when (and (stringp field)
				 (stringp v)
				 (string-match "\\`[a-zA-Z][_a-zA-Z0-9]*\\'"
					       field))
			(push (cons field v) org-table-local-parameters)
			(push (list field line col)
			      org-table-named-field-locations))))))))))
      ;; Re-use existing markers when possible.
      (if (markerp org-table-current-begin-pos)
	  (move-marker org-table-current-begin-pos (point))
	(setq org-table-current-begin-pos (point-marker)))
      ;; Analyze the line types.
      (let ((l 0) hlines dlines types)
	(while (looking-at "[ \t]*|\\(-\\)?")
	  (push (if (match-end 1) 'hline 'dline) types)
	  (if (match-end 1) (push l hlines) (push l dlines))
	  (forward-line)
	  (cl-incf l))
	(push 'hline types) ; Add an imaginary extra hline to the end.
	(setq org-table-current-line-types (apply #'vector (nreverse types)))
	(setq org-table-dlines (apply #'vector (cons nil (nreverse dlines))))
	(setq org-table-hlines (apply #'vector (cons nil (nreverse hlines)))))
      ;; Get the number of columns from the first data line in table.
      (goto-char beg)
      (forward-line (aref org-table-dlines 1))
      (let* ((fields
	      (org-split-string
	       (buffer-substring (line-beginning-position) (line-end-position))
	       "[ \t]*|[ \t]*"))
	     (nfields (length fields))
	     al al2)
	(setq org-table-current-ncol nfields)
	(let ((last-dline
	       (aref org-table-dlines (1- (length org-table-dlines)))))
	  (dotimes (i nfields)
	    (let ((column (1+ i)))
	      (push (list (format "LR%d" column) last-dline column) al)
	      (push (cons (format "LR%d" column) (nth i fields)) al2))))
	(setq org-table-named-field-locations
	      (append org-table-named-field-locations al))
	(setq org-table-local-parameters
	      (append org-table-local-parameters al2))))))

(defun org-table--force-dataline ()
  "Move point to the closest data line in a table.
Raise an error if the table contains no data line.  Preserve
column when moving point."
  (unless (org-match-line org-table-dataline-regexp)
    (let* ((re org-table-dataline-regexp)
	   (column (current-column))
	   (p1 (save-excursion (re-search-forward re (org-table-end) t)))
	   (p2 (save-excursion (re-search-backward re (org-table-begin) t))))
      (cond ((and p1 p2)
	     (goto-char (if (< (abs (- p1 (point))) (abs (- p2 (point))))
			    p1
			  p2)))
	    ((or p1 p2) (goto-char (or p1 p2)))
	    (t (user-error "No table data line around here")))
      (org-move-to-column column))))

(defun org-table-show-reference (&optional local)
  "Show the location/value of the $ expression at point.
When LOCAL is non-nil, show references for the table at point."
  (interactive)
  (org-table-remove-rectangle-highlight)
  (when local (org-table-analyze))
  (catch 'exit
    (let ((pos (if local (point) org-pos))
	  (face2 'highlight)
	  (org-inhibit-highlight-removal t)
	  (win (selected-window))
	  (org-show-positions nil)
	  var name e what match dest)
      (setq what (cond
		  ((org-in-regexp "^@[0-9]+[ \t=]")
		   (setq match (concat (substring (match-string 0) 0 -1)
				       "$1.."
				       (substring (match-string 0) 0 -1)
				       "$100"))
		   'range)
		  ((or (org-in-regexp org-table-range-regexp2)
		       (org-in-regexp org-table-translate-regexp)
		       (org-in-regexp org-table-range-regexp))
		   (setq match
			 (save-match-data
			   (org-table-convert-refs-to-rc (match-string 0))))
		   'range)
		  ((org-in-regexp "\\$[a-zA-Z][a-zA-Z0-9]*") 'name)
		  ((org-in-regexp "\\$[0-9]+") 'column)
		  ((not local) nil)
		  (t (user-error "No reference at point")))
	    match (and what (or match (match-string 0))))
      (when (and  match (not (equal (match-beginning 0) (point-at-bol))))
	(org-table-add-rectangle-overlay (match-beginning 0) (match-end 0)
					 'secondary-selection))
      (add-hook 'before-change-functions
		#'org-table-remove-rectangle-highlight)
      (when (eq what 'name) (setq var (substring match 1)))
      (when (eq what 'range)
	(unless (eq (string-to-char match) ?@) (setq match (concat "@" match)))
	(setq match (org-table-formula-substitute-names match)))
      (unless local
	(save-excursion
	  (end-of-line)
	  (re-search-backward "^\\S-" nil t)
	  (beginning-of-line)
	  (when (looking-at "\\(\\$[0-9a-zA-Z]+\\|@[0-9]+\\$[0-9]+\\|[a-zA-Z]+\
\\([0-9]+\\|&\\)\\) *=")
	    (setq dest
		  (save-match-data
		    (org-table-convert-refs-to-rc (match-string 1))))
	    (org-table-add-rectangle-overlay
	     (match-beginning 1) (match-end 1) face2))))
      (if (and (markerp pos) (marker-buffer pos))
	  (if (get-buffer-window (marker-buffer pos))
	      (select-window (get-buffer-window (marker-buffer pos)))
	    (org-switch-to-buffer-other-window (get-buffer-window
						(marker-buffer pos)))))
      (goto-char pos)
      (org-table--force-dataline)
      (let ((table-start
	     (if local org-table-current-begin-pos (org-table-begin))))
	(when dest
	  (setq name (substring dest 1))
	  (cond
	   ((string-match-p "\\`\\$[a-zA-Z][a-zA-Z0-9]*" dest)
	    (org-table-goto-field dest))
	   ((string-match-p "\\`@\\([1-9][0-9]*\\)\\$\\([1-9][0-9]*\\)\\'"
			    dest)
	    (org-table-goto-field dest))
	   (t (org-table-goto-column (string-to-number name))))
	  (move-marker pos (point))
	  (org-table-highlight-rectangle nil nil face2))
	(cond
	 ((equal dest match))
	 ((not match))
	 ((eq what 'range)
	  (ignore-errors (org-table-get-range match table-start nil 'highlight)))
	 ((setq e (assoc var org-table-named-field-locations))
	  (org-table-goto-field var)
	  (org-table-highlight-rectangle)
	  (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
	 ((setq e (assoc var org-table-column-names))
	  (org-table-goto-column (string-to-number (cdr e)))
	  (org-table-highlight-rectangle)
	  (goto-char table-start)
	  (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
				 (org-table-end) t)
	      (progn
		(goto-char (match-beginning 1))
		(org-table-highlight-rectangle)
		(message "Named column (column %s)" (cdr e)))
	    (user-error "Column name not found")))
	 ((eq what 'column)
	  ;; Column number.
	  (org-table-goto-column (string-to-number (substring match 1)))
	  (org-table-highlight-rectangle)
	  (message "Column %s" (substring match 1)))
	 ((setq e (assoc var org-table-local-parameters))
	  (goto-char table-start)
	  (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
	      (progn
		(goto-char (match-beginning 1))
		(org-table-highlight-rectangle)
		(message "Local parameter."))
	    (user-error "Parameter not found")))
	 ((not var) (user-error "No reference at point"))
	 ((setq e (assoc var org-table-formula-constants-local))
	  (message "Local Constant: $%s=%s in #+CONSTANTS line."
		   var (cdr e)))
	 ((setq e (assoc var org-table-formula-constants))
	  (message "Constant: $%s=%s in `org-table-formula-constants'."
		   var (cdr e)))
	 ((setq e (and (fboundp 'constants-get) (constants-get var)))
	  (message "Constant: $%s=%s, from `constants.el'%s."
		   var e (format " (%s units)" constants-unit-system)))
	 (t (user-error "Undefined name $%s" var)))
	(goto-char pos)
	(when (and org-show-positions
		   (not (memq this-command '(org-table-fedit-scroll
					     org-table-fedit-scroll-down))))
	  (push pos org-show-positions)
	  (push table-start org-show-positions)
	  (let ((min (apply 'min org-show-positions))
		(max (apply 'max org-show-positions)))
	    (set-window-start (selected-window) min)
	    (goto-char max)
	    (or (pos-visible-in-window-p max)
		(set-window-start (selected-window) max)))))
      (select-window win))))

\f
;;; The Orgtbl minor mode

;; Define a minor mode which can be used in other modes in order to
;; integrate the Org table editor.

;; This is really a hack, because the Org table editor uses several
;; keys which normally belong to the major mode, for example the TAB
;; and RET keys.  Here is how it works: The minor mode defines all the
;; keys necessary to operate the table editor, but wraps the commands
;; into a function which tests if the cursor is currently inside
;; a table.  If that is the case, the table editor command is
;; executed.  However, when any of those keys is used outside a table,
;; the function uses `key-binding' to look up if the key has an
;; associated command in another currently active keymap (minor modes,
;; major mode, global), and executes that command.  There might be
;; problems if any of the keys used by the table editor is otherwise
;; used as a prefix key.

;; Another challenge is that the key binding for TAB can be tab or \C-i,
;; likewise the binding for RET can be return or \C-m.  Orgtbl-mode
;; addresses this by checking explicitly for both bindings.

;; The optimized version (see variable `orgtbl-optimized') takes over
;; all keys which are bound to `self-insert-command' in the *global map*.
;; Some modes bind other commands to simple characters, for example
;; AUCTeX binds the double quote to `Tex-insert-quote'.  With orgtbl-mode
;; active, this binding is ignored inside tables and replaced with a
;; modified self-insert.

(defvar orgtbl-mode-map (make-keymap)
  "Keymap for `orgtbl-mode'.")

(defvar org-old-auto-fill-inhibit-regexp nil
  "Local variable used by `orgtbl-mode'.")

(defconst orgtbl-line-start-regexp
  "[ \t]*\\(|\\|#\\+\\(tblfm\\|orgtbl\\|tblname\\):\\)"
  "Matches a line belonging to an orgtbl.")

(defconst orgtbl-extra-font-lock-keywords
  (list (list (concat "^" orgtbl-line-start-regexp ".*")
	      0 (quote 'org-table) 'prepend))
  "Extra `font-lock-keywords' to be added when `orgtbl-mode' is active.")

;;;###autoload
(defun turn-on-orgtbl ()
  "Unconditionally turn on `orgtbl-mode'."
  (require 'org-table)
  (orgtbl-mode 1))

;; Install it as a minor mode.
(put 'orgtbl-mode :included t)
(put 'orgtbl-mode :menu-tag "Org Table Mode")

(easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
      '("OrgTbl"
	["Create or convert" org-table-create-or-convert-from-region
	 :active (not (org-at-table-p)) :keys "C-c |" ]
	"--"
	["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
	["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
	["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
	["Next Row" org-return :active (org-at-table-p) :keys "RET"]
	"--"
	["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
	["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
	["Copy Field from Above"
	 org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
	"--"
	("Column"
	 ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
	 ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
	 ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
	 ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
	("Row"
	 ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
	 ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
	 ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
	 ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
	 ["Sort lines in region" org-table-sort-lines :active (org-at-table-p) :keys "C-c ^"]
	 "--"
	 ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
	("Rectangle"
	 ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
	 ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
	 ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
	 ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
	"--"
	("Radio tables"
	 ["Insert table template" orgtbl-insert-radio-table
	  (cl-assoc-if #'derived-mode-p orgtbl-radio-table-templates)]
	 ["Comment/uncomment table" orgtbl-toggle-comment t])
	"--"
	["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
	["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
	["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
	["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
	["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
	["Iterate all" (org-table-recalculate '(16)) :active (org-at-table-p) :keys "C-u C-u C-c *"]
	["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
	["Sum Column/Rectangle" org-table-sum
	 :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
	["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
	["Debug Formulas"
	 org-table-toggle-formula-debugger :active (org-at-table-p)
	 :keys "C-c {"
	 :style toggle :selected org-table-formula-debug]
	["Show Col/Row Numbers"
	 org-table-toggle-coordinate-overlays :active (org-at-table-p)
	 :keys "C-c }"
	 :style toggle :selected org-table-overlay-coordinates]
	"--"
	("Plot"
	 ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
	 ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))

;;;###autoload
(define-minor-mode orgtbl-mode
  "The Org mode table editor as a minor mode for use in other modes."
  :lighter " OrgTbl" :keymap orgtbl-mode-map
  (org-load-modules-maybe)
  (cond
   ((derived-mode-p 'org-mode)
    ;; Exit without error, in case some hook functions calls this by
    ;; accident in Org mode.
    (message "Orgtbl mode is not useful in Org mode, command ignored"))
   (orgtbl-mode
    (and (orgtbl-setup) (defun orgtbl-setup () nil)) ;; FIXME: Yuck!?!
    ;; Make sure we are first in minor-mode-map-alist
    (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
      ;; FIXME: maybe it should use emulation-mode-map-alists?
      (and c (setq minor-mode-map-alist
                   (cons c (delq c minor-mode-map-alist)))))
    (setq-local org-table-may-need-update t)
    (add-hook 'before-change-functions 'org-before-change-function
	      nil 'local)
    (setq-local org-old-auto-fill-inhibit-regexp
		auto-fill-inhibit-regexp)
    (setq-local auto-fill-inhibit-regexp
		(if auto-fill-inhibit-regexp
		    (concat orgtbl-line-start-regexp "\\|"
			    auto-fill-inhibit-regexp)
		  orgtbl-line-start-regexp))
    (when (fboundp 'font-lock-add-keywords)
      (font-lock-add-keywords nil orgtbl-extra-font-lock-keywords)
      (org-restart-font-lock))
    (easy-menu-add orgtbl-mode-menu))
   (t
    (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
    (remove-hook 'before-change-functions 'org-before-change-function t)
    (when (fboundp 'font-lock-remove-keywords)
      (font-lock-remove-keywords nil orgtbl-extra-font-lock-keywords)
      (org-restart-font-lock))
    (easy-menu-remove orgtbl-mode-menu)
    (force-mode-line-update 'all))))

(defun orgtbl-make-binding (fun n &rest keys)
  "Create a function for binding in the table minor mode.
FUN is the command to call inside a table.  N is used to create a unique
command name.  KEYS are keys that should be checked in for a command
to execute outside of tables."
  (eval
   (list 'defun
	 (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
	 '(arg)
	 (concat "In tables, run `" (symbol-name fun) "'.\n"
		 "Outside of tables, run the binding of `"
		 (mapconcat #'key-description keys "' or `")
		 "'.")
	 '(interactive "p")
	 (list 'if
	       '(org-at-table-p)
	       (list 'call-interactively (list 'quote fun))
	       (list 'let '(orgtbl-mode)
		     (list 'call-interactively
			   (append '(or)
				   (mapcar (lambda (k)
					     (list 'key-binding k))
					   keys)
				   '('orgtbl-error))))))))

(defun orgtbl-error ()
  "Error when there is no default binding for a table key."
  (interactive)
  (user-error "This key has no function outside tables"))

(defun orgtbl-setup ()
  "Setup orgtbl keymaps."
  (let ((nfunc 0)
	(bindings
	 '(([(meta shift left)]  org-table-delete-column)
	   ([(meta left)]	 org-table-move-column-left)
	   ([(meta right)]       org-table-move-column-right)
	   ([(meta shift right)] org-table-insert-column)
	   ([(meta shift up)]    org-table-kill-row)
	   ([(meta shift down)]  org-table-insert-row)
	   ([(meta up)]		 org-table-move-row-up)
	   ([(meta down)]	 org-table-move-row-down)
	   ("\C-c\C-w"		 org-table-cut-region)
	   ("\C-c\M-w"		 org-table-copy-region)
	   ("\C-c\C-y"		 org-table-paste-rectangle)
	   ("\C-c\C-w"           org-table-wrap-region)
	   ("\C-c-"		 org-table-insert-hline)
	   ("\C-c}"		 org-table-toggle-coordinate-overlays)
	   ("\C-c{"		 org-table-toggle-formula-debugger)
	   ("\C-m"		 org-table-next-row)
	   ([(shift return)]	 org-table-copy-down)
	   ("\C-c?"		 org-table-field-info)
	   ("\C-c "		 org-table-blank-field)
	   ("\C-c+"		 org-table-sum)
	   ("\C-c="		 org-table-eval-formula)
	   ("\C-c'"		 org-table-edit-formulas)
	   ("\C-c`"		 org-table-edit-field)
	   ("\C-c*"		 org-table-recalculate)
	   ("\C-c^"		 org-table-sort-lines)
	   ("\M-a"		 org-table-beginning-of-field)
	   ("\M-e"		 org-table-end-of-field)
	   ([(control ?#)]       org-table-rotate-recalc-marks)))
	elt key fun cmd)
    (while (setq elt (pop bindings))
      (setq nfunc (1+ nfunc))
      (setq key (org-key (car elt))
	    fun (nth 1 elt)
	    cmd (orgtbl-make-binding fun nfunc key))
      (org-defkey orgtbl-mode-map key cmd))

    ;; Special treatment needed for TAB, RET and DEL
    (org-defkey orgtbl-mode-map [(return)]
		(orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
    (org-defkey orgtbl-mode-map "\C-m"
		(orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
    (org-defkey orgtbl-mode-map [(tab)]
		(orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
    (org-defkey orgtbl-mode-map "\C-i"
		(orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)]))
    (org-defkey orgtbl-mode-map [(shift tab)]
		(orgtbl-make-binding 'org-table-previous-field 104
				     [(shift tab)] [(tab)] "\C-i"))
    (org-defkey orgtbl-mode-map [backspace]
		(orgtbl-make-binding 'org-delete-backward-char 109
				     [backspace] (kbd "DEL")))

    (org-defkey orgtbl-mode-map [S-iso-lefttab]
		(orgtbl-make-binding 'org-table-previous-field 107
				     [S-iso-lefttab] [backtab] [(shift tab)]
				     [(tab)] "\C-i"))

    (org-defkey orgtbl-mode-map [backtab]
		(orgtbl-make-binding 'org-table-previous-field 108
				     [backtab] [S-iso-lefttab] [(shift tab)]
				     [(tab)] "\C-i"))

    (org-defkey orgtbl-mode-map "\M-\C-m"
		(orgtbl-make-binding 'org-table-wrap-region 105
				     "\M-\C-m" [(meta return)]))
    (org-defkey orgtbl-mode-map [(meta return)]
		(orgtbl-make-binding 'org-table-wrap-region 106
				     [(meta return)] "\M-\C-m"))

    (org-defkey orgtbl-mode-map "\C-c\C-c" 'orgtbl-ctrl-c-ctrl-c)
    (org-defkey orgtbl-mode-map "\C-c|" 'orgtbl-create-or-convert-from-region)

    (when orgtbl-optimized
      ;; If the user wants maximum table support, we need to hijack
      ;; some standard editing functions
      (org-remap orgtbl-mode-map
		 'self-insert-command 'orgtbl-self-insert-command
		 'delete-char 'org-delete-char
		 'delete-backward-char 'org-delete-backward-char)
      (org-defkey orgtbl-mode-map "|" 'org-force-self-insert))
    t))

(defun orgtbl-ctrl-c-ctrl-c (arg)
  "If the cursor is inside a table, realign the table.
If it is a table to be sent away to a receiver, do it.
With prefix arg, also recompute table."
  (interactive "P")
  (let ((case-fold-search t) (pos (point)) action)
    (save-excursion
      (beginning-of-line 1)
      (setq action (cond
		    ((looking-at "[ \t]*#\\+ORGTBL:.*\n[ \t]*|") (match-end 0))
		    ((looking-at "[ \t]*|") pos)
		    ((looking-at "[ \t]*#\\+tblfm:") 'recalc))))
    (cond
     ((integerp action)
      (goto-char action)
      (org-table-maybe-eval-formula)
      (if arg
	  (call-interactively 'org-table-recalculate)
	(org-table-maybe-recalculate-line))
      (call-interactively 'org-table-align)
      (when (orgtbl-send-table 'maybe)
	(run-hooks 'orgtbl-after-send-table-hook)))
     ((eq action 'recalc)
      (save-excursion
	(beginning-of-line 1)
	(skip-chars-backward " \r\n\t")
	(if (org-at-table-p)
	    (org-call-with-arg 'org-table-recalculate t))))
     (t (let (orgtbl-mode)
	  (call-interactively (key-binding "\C-c\C-c")))))))

(defun orgtbl-create-or-convert-from-region (_arg)
  "Create table or convert region to table, if no conflicting binding.
This installs the table binding `C-c |', but only if there is no
conflicting binding to this key outside orgtbl-mode."
  (interactive "P")
  (let* (orgtbl-mode (cmd (key-binding "\C-c|")))
    (if cmd
	(call-interactively cmd)
      (call-interactively 'org-table-create-or-convert-from-region))))

(defun orgtbl-tab (arg)
  "Justification and field motion for `orgtbl-mode'."
  (interactive "P")
  (if arg (org-table-edit-field t)
    (org-table-justify-field-maybe)
    (org-table-next-field)))

(defun orgtbl-ret ()
  "Justification and field motion for `orgtbl-mode'."
  (interactive)
  (if (bobp)
      (newline)
    (org-table-justify-field-maybe)
    (org-table-next-row)))

(defun orgtbl-self-insert-command (N)
  "Like `self-insert-command', use overwrite-mode for whitespace in tables.
If the cursor is in a table looking at whitespace, the whitespace is
overwritten, and the table is not marked as requiring realignment."
  (interactive "p")
  (if (and (org-at-table-p)
	   (or
	    (and org-table-auto-blank-field
		 (member last-command
			 '(orgtbl-hijacker-command-100
			   orgtbl-hijacker-command-101
			   orgtbl-hijacker-command-102
			   orgtbl-hijacker-command-103
			   orgtbl-hijacker-command-104
			   orgtbl-hijacker-command-105
			   yas/expand))
		 (org-table-blank-field))
	    t)
	   (eq N 1)
	   (looking-at "[^|\n]* \\( \\)|"))
      (let (org-table-may-need-update)
	(delete-region (match-beginning 1) (match-end 1))
	(self-insert-command N))
    (setq org-table-may-need-update t)
    (let* (orgtbl-mode
	   a
	   (cmd (or (key-binding
		     (or (and (listp function-key-map)
			      (setq a (assoc last-input-event function-key-map))
			      (cdr a))
			 (vector last-input-event)))
		    'self-insert-command)))
      (call-interactively cmd)
      (if (and org-self-insert-cluster-for-undo
	       (eq cmd 'self-insert-command))
	  (if (not (eq last-command 'orgtbl-self-insert-command))
	      (setq org-self-insert-command-undo-counter 1)
	    (if (>= org-self-insert-command-undo-counter 20)
		(setq org-self-insert-command-undo-counter 1)
	      (and (> org-self-insert-command-undo-counter 0)
		   buffer-undo-list
		   (not (cadr buffer-undo-list)) ; remove nil entry
		   (setcdr buffer-undo-list (cddr buffer-undo-list)))
	      (setq org-self-insert-command-undo-counter
		    (1+ org-self-insert-command-undo-counter))))))))

;;;###autoload
(defvar orgtbl-exp-regexp "^\\([-+]?[0-9][0-9.]*\\)[eE]\\([-+]?[0-9]+\\)$"
  "Regular expression matching exponentials as produced by calc.")

(defun orgtbl-gather-send-defs ()
  "Gather a plist of :name, :transform, :params for each destination before
a radio table."
  (save-excursion
    (goto-char (org-table-begin))
    (let (rtn)
      (beginning-of-line 0)
      (while (looking-at "[ \t]*#\\+ORGTBL[: \t][ \t]*SEND[ \t]+\\([^ \t\r\n]+\\)[ \t]+\\([^ \t\r\n]+\\)\\([ \t]+.*\\)?")
	(let ((name (org-no-properties (match-string 1)))
	      (transform (intern (match-string 2)))
	      (params (if (match-end 3)
			  (read (concat "(" (match-string 3) ")")))))
	  (push (list :name name :transform transform :params params)
		rtn)
	  (beginning-of-line 0)))
      rtn)))

(defun orgtbl-send-replace-tbl (name text)
  "Find and replace table NAME with TEXT."
  (save-excursion
    (goto-char (point-min))
    (let* ((location-flag nil)
	   (name (regexp-quote name))
	   (begin-re (format "BEGIN +RECEIVE +ORGTBL +%s\\([ \t]\\|$\\)" name))
	   (end-re (format "END +RECEIVE +ORGTBL +%s\\([ \t]\\|$\\)" name)))
      (while (re-search-forward begin-re nil t)
	(unless location-flag (setq location-flag t))
	(let ((beg (line-beginning-position 2)))
	  (unless (re-search-forward end-re nil t)
	    (user-error "Cannot find end of receiver location at %d" beg))
	  (beginning-of-line)
	  (delete-region beg (point))
	  (insert text "\n")))
      (unless location-flag
	(user-error "No valid receiver location found in the buffer")))))

;;;###autoload
(defun org-table-to-lisp (&optional txt)
  "Convert the table at point to a Lisp structure.
The structure will be a list.  Each item is either the symbol `hline'
for a horizontal separator line, or a list of field values as strings.
The table is taken from the parameter TXT, or from the buffer at point."
  (unless (or txt (org-at-table-p)) (user-error "No table at point"))
  (let ((txt (or txt
		 (buffer-substring-no-properties (org-table-begin)
						 (org-table-end)))))
    (mapcar (lambda (x)
	      (if (string-match org-table-hline-regexp x) 'hline
		(org-split-string (org-trim x) "\\s-*|\\s-*")))
	    (org-split-string txt "[ \t]*\n[ \t]*"))))

(defun orgtbl-send-table (&optional maybe)
  "Send a transformed version of table at point to the receiver position.
With argument MAYBE, fail quietly if no transformation is defined
for this table."
  (interactive)
  (catch 'exit
    (unless (org-at-table-p) (user-error "Not at a table"))
    ;; when non-interactive, we assume align has just happened.
    (when (called-interactively-p 'any) (org-table-align))
    (let ((dests (orgtbl-gather-send-defs))
	  (table (org-table-to-lisp
		  (buffer-substring-no-properties (org-table-begin)
						  (org-table-end))))
	  (ntbl 0))
      (unless dests
	(if maybe (throw 'exit nil)
	  (user-error "Don't know how to transform this table")))
      (dolist (dest dests)
	(let ((name (plist-get dest :name))
	      (transform (plist-get dest :transform))
	      (params (plist-get dest :params)))
	  (unless (fboundp transform)
	    (user-error "No such transformation function %s" transform))
	  (orgtbl-send-replace-tbl name (funcall transform table params)))
	(cl-incf ntbl))
      (message "Table converted and installed at %d receiver location%s"
	       ntbl (if (> ntbl 1) "s" ""))
      (and (> ntbl 0) ntbl))))

(defun org-remove-by-index (list indices &optional i0)
  "Remove the elements in LIST with indices in INDICES.
First element has index 0, or I0 if given."
  (if (not indices)
      list
    (if (integerp indices) (setq indices (list indices)))
    (setq i0 (1- (or i0 0)))
    (delq :rm (mapcar (lambda (x)
			(setq i0 (1+ i0))
			(if (memq i0 indices) :rm x))
		      list))))

(defun orgtbl-toggle-comment ()
  "Comment or uncomment the orgtbl at point."
  (interactive)
  (let* ((case-fold-search t)
	 (re1 (concat "^" (regexp-quote comment-start) orgtbl-line-start-regexp))
	 (re2 (concat "^" orgtbl-line-start-regexp))
	 (commented (save-excursion (beginning-of-line 1)
				    (cond ((looking-at re1) t)
					  ((looking-at re2) nil)
					  (t (user-error "Not at an org table")))))
	 (re (if commented re1 re2))
	 beg end)
    (save-excursion
      (beginning-of-line 1)
      (while (looking-at re) (beginning-of-line 0))
      (beginning-of-line 2)
      (setq beg (point))
      (while (looking-at re) (beginning-of-line 2))
      (setq end (point)))
    (comment-region beg end (if commented '(4) nil))))

(defun orgtbl-insert-radio-table ()
  "Insert a radio table template appropriate for this major mode."
  (interactive)
  (let* ((e (cl-assoc-if #'derived-mode-p orgtbl-radio-table-templates))
	 (txt (nth 1 e))
	 name pos)
    (unless e (user-error "No radio table setup defined for %s" major-mode))
    (setq name (read-string "Table name: "))
    (while (string-match "%n" txt)
      (setq txt (replace-match name t t txt)))
    (or (bolp) (insert "\n"))
    (setq pos (point))
    (insert txt)
    (goto-char pos)))

;;;###autoload
(defun orgtbl-to-generic (table params)
  "Convert the orgtbl-mode TABLE to some other format.

This generic routine can be used for many standard cases.

TABLE is a list, each entry either the symbol `hline' for
a horizontal separator line, or a list of fields for that
line.  PARAMS is a property list of parameters that can
influence the conversion.

Valid parameters are:

:backend, :raw

  Export back-end used as a basis to transcode elements of the
  table, when no specific parameter applies to it.  It is also
  used to translate cells contents.  You can prevent this by
  setting :raw property to a non-nil value.

:splice

  When non-nil, only convert rows, not the table itself.  This is
  equivalent to setting to the empty string both :tstart
  and :tend, which see.

:skip

  When set to an integer N, skip the first N lines of the table.
  Horizontal separation lines do count for this parameter!

:skipcols

  List of columns that should be skipped.  If the table has
  a column with calculation marks, that column is automatically
  discarded beforehand.

:hline

  String to be inserted on horizontal separation lines.  May be
  nil to ignore these lines altogether.

:sep

  Separator between two fields, as a string.

Each in the following group may be either a string or a function
of no arguments returning a string:

:tstart, :tend

  Strings to start and end the table.  Ignored when :splice is t.

:lstart, :lend

  Strings to start and end a new table line.

:llstart, :llend

  Strings to start and end the last table line.  Default,
  respectively, to :lstart and :lend.

Each in the following group may be a string or a function of one
argument (either the cells in the current row, as a list of
strings, or the current cell) returning a string:

:lfmt

  Format string for an entire row, with enough %s to capture all
  fields.  When non-nil, :lstart, :lend, and :sep are ignored.

:llfmt

  Format for the entire last line, defaults to :lfmt.

:fmt

  A format to be used to wrap the field, should contain %s for
  the original field value.  For example, to wrap everything in
  dollars, you could use :fmt \"$%s$\".  This may also be
  a property list with column numbers and format strings, or
  functions, e.g.,

    (:fmt (2 \"$%s$\" 4 (lambda (c) (format \"$%s$\" c))))

:hlstart :hllstart :hlend :hllend :hsep :hlfmt :hllfmt :hfmt

 Same as above, specific for the header lines in the table.
 All lines before the first hline are treated as header.  If
 any of these is not present, the data line value is used.

This may be either a string or a function of two arguments:

:efmt

  Use this format to print numbers with exponential.  The format
  should have %s twice for inserting mantissa and exponent, for
  example \"%s\\\\times10^{%s}\".  This may also be a property
  list with column numbers and format strings or functions.
  :fmt will still be applied after :efmt."
  ;; Make sure `org-export-create-backend' is available.
  (require 'ox)
  (let* ((backend (plist-get params :backend))
	 (custom-backend
	  ;; Build a custom back-end according to PARAMS.  Before
	  ;; defining a translator, check if there is anything to do.
	  ;; When there isn't, let BACKEND handle the element.
	  (org-export-create-backend
	   :parent (or backend 'org)
	   :transcoders
	   `((table . ,(org-table--to-generic-table params))
	     (table-row . ,(org-table--to-generic-row params))
	     (table-cell . ,(org-table--to-generic-cell params))
	     ;; Macros are not going to be expanded.  However, no
	     ;; regular back-end has a transcoder for them.  We
	     ;; provide one so they are not ignored, but displayed
	     ;; as-is instead.
	     (macro . (lambda (m c i) (org-element-macro-interpreter m nil))))))
	 data info)
    ;; Store TABLE as Org syntax in DATA.  Tolerate non-string cells.
    ;; Initialize communication channel in INFO.
    (with-temp-buffer
      (let ((org-inhibit-startup t)) (org-mode))
      (let ((standard-output (current-buffer))
	    (org-element-use-cache nil))
	(dolist (e table)
	  (cond ((eq e 'hline) (princ "|--\n"))
		((consp e)
		 (princ "| ") (dolist (c e) (princ c) (princ " |"))
		 (princ "\n")))))
      ;; Add back-end specific filters, but not user-defined ones.  In
      ;; particular, make sure to call parse-tree filters on the
      ;; table.
      (setq info
	    (let ((org-export-filters-alist nil))
	      (org-export-install-filters
	       (org-combine-plists
		(org-export-get-environment backend nil params)
		`(:back-end ,(org-export-get-backend backend))))))
      (setq data
	    (org-export-filter-apply-functions
	     (plist-get info :filter-parse-tree)
	     (org-element-map (org-element-parse-buffer) 'table
	       #'identity nil t)
	     info)))
    (when (and backend (symbolp backend) (not (org-export-get-backend backend)))
      (user-error "Unknown :backend value"))
    (when (or (not backend) (plist-get info :raw)) (require 'ox-org))
    ;; Handle :skip parameter.
    (let ((skip (plist-get info :skip)))
      (when skip
	(unless (wholenump skip) (user-error "Wrong :skip value"))
	(let ((n 0))
	  (org-element-map data 'table-row
	    (lambda (row)
	      (if (>= n skip) t
		(org-element-extract-element row)
		(cl-incf n)
		nil))
	    nil t))))
    ;; Handle :skipcols parameter.
    (let ((skipcols (plist-get info :skipcols)))
      (when skipcols
	(unless (consp skipcols) (user-error "Wrong :skipcols value"))
	(org-element-map data 'table
	  (lambda (table)
	    (let ((specialp (org-export-table-has-special-column-p table)))
	      (dolist (row (org-element-contents table))
		(when (eq (org-element-property :type row) 'standard)
		  (let ((c 1))
		    (dolist (cell (nthcdr (if specialp 1 0)
					  (org-element-contents row)))
		      (when (memq c skipcols)
			(org-element-extract-element cell))
		      (cl-incf c))))))))))
    ;; Since we are going to export using a low-level mechanism,
    ;; ignore special column and special rows manually.
    (let ((special? (org-export-table-has-special-column-p data))
	  ignore)
      (org-element-map data (if special? '(table-cell table-row) 'table-row)
	(lambda (datum)
	  (when (if (eq (org-element-type datum) 'table-row)
		    (org-export-table-row-is-special-p datum nil)
		  (org-export-first-sibling-p datum nil))
	    (push datum ignore))))
      (setq info (plist-put info :ignore-list ignore)))
    ;; We use a low-level mechanism to export DATA so as to skip all
    ;; usual pre-processing and post-processing, i.e., hooks, Babel
    ;; code evaluation, include keywords and macro expansion.  Only
    ;; back-end specific filters are retained.
    (let ((output (org-export-data-with-backend data custom-backend info)))
      ;; Remove final newline.
      (if (org-string-nw-p output) (substring-no-properties output 0 -1) ""))))

(defun org-table--generic-apply (value name &optional with-cons &rest args)
  (cond ((null value) nil)
        ((functionp value) `(funcall ',value ,@args))
        ((stringp value)
	 (cond ((consp (car args)) `(apply #'format ,value ,@args))
	       (args `(format ,value ,@args))
	       (t value)))
	((and with-cons (consp value))
	 `(let ((val (cadr (memq column ',value))))
	    (cond ((null val) contents)
		  ((stringp val) (format val ,@args))
		  ((functionp val) (funcall val ,@args))
		  (t (user-error "Wrong %s value" ,name)))))
        (t (user-error "Wrong %s value" name))))

(defun org-table--to-generic-table (params)
  "Return custom table transcoder according to PARAMS.
PARAMS is a plist.  See `orgtbl-to-generic' for more
information."
  (let ((backend (plist-get params :backend))
	(splice (plist-get params :splice))
	(tstart (plist-get params :tstart))
	(tend (plist-get params :tend)))
    `(lambda (table contents info)
       (concat
	,(and tstart (not splice)
	      `(concat ,(org-table--generic-apply tstart ":tstart") "\n"))
	,(if (or (not backend) tstart tend splice) 'contents
	   `(org-export-with-backend ',backend table contents info))
	,(org-table--generic-apply (and (not splice) tend) ":tend")))))

(defun org-table--to-generic-row (params)
  "Return custom table row transcoder according to PARAMS.
PARAMS is a plist.  See `orgtbl-to-generic' for more
information."
  (let* ((backend (plist-get params :backend))
	 (lstart (plist-get params :lstart))
	 (llstart (plist-get params :llstart))
	 (hlstart (plist-get params :hlstart))
	 (hllstart (plist-get params :hllstart))
	 (lend (plist-get params :lend))
	 (llend (plist-get params :llend))
	 (hlend (plist-get params :hlend))
	 (hllend (plist-get params :hllend))
	 (lfmt (plist-get params :lfmt))
	 (llfmt (plist-get params :llfmt))
	 (hlfmt (plist-get params :hlfmt))
	 (hllfmt (plist-get params :hllfmt)))
    `(lambda (row contents info)
       (if (eq (org-element-property :type row) 'rule)
	   ,(cond
	     ((plist-member params :hline)
	      (org-table--generic-apply (plist-get params :hline) ":hline"))
	     (backend `(org-export-with-backend ',backend row nil info)))
	 (let ((headerp ,(and (or hlfmt hlstart hlend)
			      '(org-export-table-row-in-header-p row info)))
	       (last-header-p
		,(and (or hllfmt hllstart hllend)
		      '(org-export-table-row-ends-header-p row info)))
	       (lastp (not (org-export-get-next-element row info))))
	   (when contents
	     ;; Check if we can apply `:lfmt', `:llfmt', `:hlfmt', or
	     ;; `:hllfmt' to CONTENTS.  Otherwise, fallback on
	     ;; `:lstart', `:lend' and their relatives.
	     ,(let ((cells
		     '(org-element-map row 'table-cell
			(lambda (cell)
			  ;; Export all cells, without separators.
			  ;;
			  ;; Use `org-export-data-with-backend'
			  ;; instead of `org-export-data' to eschew
			  ;; cached values, which
			  ;; ignore :orgtbl-ignore-sep parameter.
			  (org-export-data-with-backend
			   cell
			   (plist-get info :back-end)
			   (org-combine-plists info '(:orgtbl-ignore-sep t))))
			info)))
		`(cond
		  ,(and hllfmt
			`(last-header-p ,(org-table--generic-apply
					  hllfmt ":hllfmt" nil cells)))
		  ,(and hlfmt
			`(headerp ,(org-table--generic-apply
				    hlfmt ":hlfmt" nil cells)))
		  ,(and llfmt
			`(lastp ,(org-table--generic-apply
				  llfmt ":llfmt" nil cells)))
		  (t
		   ,(if lfmt (org-table--generic-apply lfmt ":lfmt" nil cells)
		      `(concat
			(cond
			 ,(and
			   (or hllstart hllend)
			   `(last-header-p
			     (concat
			      ,(org-table--generic-apply hllstart ":hllstart")
			      contents
			      ,(org-table--generic-apply hllend ":hllend"))))
			 ,(and
			   (or hlstart hlend)
			   `(headerp
			     (concat
			      ,(org-table--generic-apply hlstart ":hlstart")
			      contents
			      ,(org-table--generic-apply hlend ":hlend"))))
			 ,(and
			   (or llstart llend)
			   `(lastp
			     (concat
			      ,(org-table--generic-apply llstart ":llstart")
			      contents
			      ,(org-table--generic-apply llend ":llend"))))
			 (t
			  ,(cond
			    ((or lstart lend)
			     `(concat
			       ,(org-table--generic-apply lstart ":lstart")
			       contents
			       ,(org-table--generic-apply lend ":lend")))
			    (backend
			     `(org-export-with-backend
			       ',backend row contents info))
			    (t 'contents)))))))))))))))

(defun org-table--to-generic-cell (params)
  "Return custom table cell transcoder according to PARAMS.
PARAMS is a plist.  See `orgtbl-to-generic' for more
information."
  (let* ((backend (plist-get params :backend))
	 (efmt (plist-get params :efmt))
	 (fmt (plist-get params :fmt))
	 (hfmt (plist-get params :hfmt))
	 (sep (plist-get params :sep))
	 (hsep (plist-get params :hsep)))
    `(lambda (cell contents info)
       ;; Make sure that contents are exported as Org data when :raw
       ;; parameter is non-nil.
       ,(when (and backend (plist-get params :raw))
	  `(setq contents
		 ;; Since we don't know what are the pseudo object
		 ;; types defined in backend, we cannot pass them to
		 ;; `org-element-interpret-data'.  As a consequence,
		 ;; they will be treated as pseudo elements, and will
		 ;; have newlines appended instead of spaces.
		 ;; Therefore, we must make sure :post-blank value is
		 ;; really turned into spaces.
		 (replace-regexp-in-string
		  "\n" " "
		  (org-trim
		   (org-element-interpret-data
		    (org-element-contents cell))))))

       (let ((headerp ,(and (or hfmt hsep)
			    '(org-export-table-row-in-header-p
			      (org-export-get-parent-element cell) info)))
	     (column
	      ;; Call costly `org-export-table-cell-address' only if
	      ;; absolutely necessary, i.e., if one
	      ;; of :fmt :efmt :hfmt has a "plist type" value.
	      ,(and (cl-some (lambda (v) (integerp (car-safe v)))
			     (list efmt hfmt fmt))
		    '(1+ (cdr (org-export-table-cell-address cell info))))))
	 (when contents
	   ;; Check if we can apply `:efmt' on CONTENTS.
	   ,(when efmt
	      `(when (string-match orgtbl-exp-regexp contents)
		 (let ((mantissa (match-string 1 contents))
		       (exponent (match-string 2 contents)))
		   (setq contents ,(org-table--generic-apply
				    efmt ":efmt" t 'mantissa 'exponent)))))
	   ;; Check if we can apply FMT (or HFMT) on CONTENTS.
	   (cond
	    ,(and hfmt `(headerp (setq contents ,(org-table--generic-apply
						  hfmt ":hfmt" t 'contents))))
	    ,(and fmt `(t (setq contents ,(org-table--generic-apply
					   fmt ":fmt" t 'contents))))))
	 ;; If a separator is provided, use it instead of BACKEND's.
	 ;; Separators are ignored when LFMT (or equivalent) is
	 ;; provided.
	 ,(cond
	   ((or hsep sep)
	    `(if (or ,(and (not sep) '(not headerp))
		     (plist-get info :orgtbl-ignore-sep)
		     (not (org-export-get-next-element cell info)))
		 ,(if (not backend) 'contents
		    `(org-export-with-backend ',backend cell contents info))
	       (concat contents
		       ,(if (and sep hsep) `(if headerp ,hsep ,sep)
			  (or hsep sep)))))
	   (backend `(org-export-with-backend ',backend cell contents info))
	   (t 'contents))))))

;;;###autoload
(defun orgtbl-to-tsv (table params)
  "Convert the orgtbl-mode table to TAB separated material."
  (orgtbl-to-generic table (org-combine-plists '(:sep "\t") params)))

;;;###autoload
(defun orgtbl-to-csv (table params)
  "Convert the orgtbl-mode table to CSV material.
This does take care of the proper quoting of fields with comma or quotes."
  (orgtbl-to-generic table
		     (org-combine-plists '(:sep "," :fmt org-quote-csv-field)
					 params)))

;;;###autoload
(defun orgtbl-to-latex (table params)
  "Convert the orgtbl-mode TABLE to LaTeX.

TABLE is a list, each entry either the symbol `hline' for
a horizontal separator line, or a list of fields for that line.
PARAMS is a property list of parameters that can influence the
conversion.  All parameters from `orgtbl-to-generic' are
supported.  It is also possible to use the following ones:

:booktabs

  When non-nil, use formal \"booktabs\" style.

:environment

  Specify environment to use, as a string.  If you use
  \"longtable\", you may also want to specify :language property,
  as a string, to get proper continuation strings."
  (require 'ox-latex)
  (orgtbl-to-generic
   table
   (org-combine-plists
    ;; Provide sane default values.
    (list :backend 'latex
	  :latex-default-table-mode 'table
	  :latex-tables-centered nil
	  :latex-tables-booktabs (plist-get params :booktabs)
	  :latex-table-scientific-notation nil
	  :latex-default-table-environment
	  (or (plist-get params :environment) "tabular"))
    params)))

;;;###autoload
(defun orgtbl-to-html (table params)
  "Convert the orgtbl-mode TABLE to HTML.

TABLE is a list, each entry either the symbol `hline' for
a horizontal separator line, or a list of fields for that line.
PARAMS is a property list of parameters that can influence the
conversion.  All parameters from `orgtbl-to-generic' are
supported.  It is also possible to use the following one:

:attributes

  Attributes and values, as a plist, which will be used in
  <table> tag."
  (require 'ox-html)
  (orgtbl-to-generic
   table
   (org-combine-plists
    ;; Provide sane default values.
    (list :backend 'html
	  :html-table-data-tags '("<td%s>" . "</td>")
	  :html-table-use-header-tags-for-first-column nil
	  :html-table-align-individual-fields t
	  :html-table-row-tags '("<tr>" . "</tr>")
	  :html-table-attributes
	  (if (plist-member params :attributes)
	      (plist-get params :attributes)
	    '(:border "2" :cellspacing "0" :cellpadding "6" :rules "groups"
		      :frame "hsides")))
    params)))

;;;###autoload
(defun orgtbl-to-texinfo (table params)
  "Convert the orgtbl-mode TABLE to Texinfo.

TABLE is a list, each entry either the symbol `hline' for
a horizontal separator line, or a list of fields for that line.
PARAMS is a property list of parameters that can influence the
conversion.  All parameters from `orgtbl-to-generic' are
supported.  It is also possible to use the following one:

:columns

  Column widths, as a string.  When providing column fractions,
  \"@columnfractions\" command can be omitted."
  (require 'ox-texinfo)
  (let ((output
	 (orgtbl-to-generic
	  table
	  (org-combine-plists
	   (list :backend 'texinfo
		 :texinfo-tables-verbatim nil
		 :texinfo-table-scientific-notation nil)
	   params)))
	(columns (let ((w (plist-get params :columns)))
		   (cond ((not w) nil)
			 ((string-match-p "{\\|@columnfractions " w) w)
			 (t (concat "@columnfractions " w))))))
    (if (not columns) output
      (replace-regexp-in-string
       "@multitable \\(.*\\)" columns output t nil 1))))

;;;###autoload
(defun orgtbl-to-orgtbl (table params)
  "Convert the orgtbl-mode TABLE into another orgtbl-mode table.

TABLE is a list, each entry either the symbol `hline' for
a horizontal separator line, or a list of fields for that line.
PARAMS is a property list of parameters that can influence the
conversion.  All parameters from `orgtbl-to-generic' are
supported.

Useful when slicing one table into many.  The :hline, :sep,
:lstart, and :lend provide orgtbl framing.  :tstart and :tend can
be set to provide ORGTBL directives for the generated table."
  (require 'ox-org)
  (orgtbl-to-generic table (org-combine-plists params (list :backend 'org))))

(defun orgtbl-to-table.el (table params)
  "Convert the orgtbl-mode TABLE into a table.el table.
TABLE is a list, each entry either the symbol `hline' for
a horizontal separator line, or a list of fields for that line.
PARAMS is a property list of parameters that can influence the
conversion.  All parameters from `orgtbl-to-generic' are
supported."
  (with-temp-buffer
    (insert (orgtbl-to-orgtbl table params))
    (org-table-align)
    (replace-regexp-in-string
     "-|" "-+"
     (replace-regexp-in-string "|-" "+-" (buffer-substring 1 (buffer-size))))))

(defun orgtbl-to-unicode (table params)
  "Convert the orgtbl-mode TABLE into a table with unicode characters.

TABLE is a list, each entry either the symbol `hline' for
a horizontal separator line, or a list of fields for that line.
PARAMS is a property list of parameters that can influence the
conversion.  All parameters from `orgtbl-to-generic' are
supported.  It is also possible to use the following ones:

:ascii-art

  When non-nil, use \"ascii-art-to-unicode\" package to translate
  the table.  You can download it here:
  http://gnuvola.org/software/j/aa2u/ascii-art-to-unicode.el.

:narrow

  When non-nil, narrow columns width than provided width cookie,
  using \"=>\" as an ellipsis, just like in an Org mode buffer."
  (require 'ox-ascii)
  (orgtbl-to-generic
   table
   (org-combine-plists
    (list :backend 'ascii
	  :ascii-charset 'utf-8
	  :ascii-table-widen-columns (not (plist-get params :narrow))
	  :ascii-table-use-ascii-art (plist-get params :ascii-art))
    params)))

;; Put the cursor in a column containing numerical values
;; of an Org table,
;; type C-c " a
;; A new column is added with a bar plot.
;; When the table is refreshed (C-u C-c *),
;; the plot is updated to reflect the new values.

(defun orgtbl-ascii-draw (value min max &optional width characters)
  "Draw an ascii bar in a table.
VALUE is the value to plot, it determines the width of the bar to draw.
MIN is the value that will be displayed as empty (zero width bar).
MAX is the value that will draw a bar filling all the WIDTH.
WIDTH is the span in characters from MIN to MAX.
CHARACTERS is a string that will compose the bar, with shades of grey
from pure white to pure black.  It defaults to a 10 characters string
of regular ascii characters."
  (let* ((width      (ceiling (or width 12)))
	 (characters (or characters " .:;c!lhVHW"))
	 (len        (1- (length characters)))
	 (value      (float (if (numberp value)
				value (string-to-number value))))
	 (relative   (/ (- value min) (- max min)))
	 (steps      (round (* relative width len))))
    (cond ((< steps             0) "too small")
	  ((> steps (* width len)) "too large")
	  (t (let* ((int-division (/ steps len))
		    (remainder    (- steps (* int-division len))))
	       (concat (make-string int-division (elt characters len))
		       (string (elt characters remainder))))))))

;;;###autoload
(defun orgtbl-ascii-plot (&optional ask)
  "Draw an ASCII bar plot in a column.

With cursor in a column containing numerical values, this function
will draw a plot in a new column.

ASK, if given, is a numeric prefix to override the default 12
characters width of the plot.  ASK may also be the `\\[universal-argument]' \
prefix,
which will prompt for the width."
  (interactive "P")
  (let ((col (org-table-current-column))
	(min  1e999)		 ; 1e999 will be converted to infinity
	(max -1e999)		 ; which is the desired result
	(table (org-table-to-lisp))
	(length
	 (cond ((consp ask)
		(read-number "Length of column " 12))
	       ((numberp ask) ask)
	       (t 12))))
    ;; Skip any hline a the top of table.
    (while (eq (car table) 'hline) (setq table (cdr table)))
    ;; Skip table header if any.
    (dolist (x (or (cdr (memq 'hline table)) table))
      (when (consp x)
	(setq x (nth (1- col) x))
	(when (string-match
	       "^[-+]?\\([0-9]*[.]\\)?[0-9]*\\([eE][+-]?[0-9]+\\)?$"
	       x)
	  (setq x (string-to-number x))
	  (when (> min x) (setq min x))
	  (when (< max x) (setq max x)))))
    (org-table-insert-column)
    (org-table-move-column-right)
    (org-table-store-formulas
     (cons
      (cons
       (concat "$" (number-to-string (1+ col)))
       (format "'(%s $%s %s %s %s)"
	       "orgtbl-ascii-draw" col min max length))
      (org-table-get-stored-formulas)))
    (org-table-recalculate t)))

;; Example of extension: unicode characters
;; Here are two examples of different styles.

;; Unicode block characters are used to give a smooth effect.
;; See http://en.wikipedia.org/wiki/Block_Elements
;; Use one of those drawing functions
;; - orgtbl-ascii-draw   (the default ascii)
;; - orgtbl-uc-draw-grid (unicode with a grid effect)
;; - orgtbl-uc-draw-cont (smooth unicode)

;; This is best viewed with the "DejaVu Sans Mono" font
;; (use M-x set-frame-font).

(defun orgtbl-uc-draw-grid (value min max &optional width)
  "Draw a bar in a table using block unicode characters.
It is a variant of orgtbl-ascii-draw with Unicode block
characters, for a smooth display.  Bars appear as grids (to the
extent the font allows)."
  ;; http://en.wikipedia.org/wiki/Block_Elements
  ;; best viewed with the "DejaVu Sans Mono" font.
  (orgtbl-ascii-draw value min max width
		     " \u258F\u258E\u258D\u258C\u258B\u258A\u2589"))

(defun orgtbl-uc-draw-cont (value min max &optional width)
  "Draw a bar in a table using block unicode characters.
It is a variant of orgtbl-ascii-draw with Unicode block
characters, for a smooth display.  Bars are solid (to the extent
the font allows)."
  (orgtbl-ascii-draw value min max width
		     " \u258F\u258E\u258D\u258C\u258B\u258A\u2589\u2588"))

(defun org-table-get-remote-range (name-or-id form)
  "Get a field value or a list of values in a range from table at ID.

NAME-OR-ID may be the name of a table in the current file as set
by a \"#+NAME:\" directive.  The first table following this line
will then be used.  Alternatively, it may be an ID referring to
any entry, also in a different file.  In this case, the first
table in that entry will be referenced.
FORM is a field or range descriptor like \"@2$3\" or \"B3\" or
\"@I$2..@II$2\".  All the references must be absolute, not relative.

The return value is either a single string for a single field, or a
list of the fields in the rectangle."
  (save-match-data
    (let ((case-fold-search t) (id-loc nil)
	  ;; Protect a bunch of variables from being overwritten by
	  ;; the context of the remote table.
	  org-table-column-names org-table-column-name-regexp
	  org-table-local-parameters org-table-named-field-locations
	  org-table-current-line-types
	  org-table-current-begin-pos org-table-dlines
	  org-table-current-ncol
	  org-table-hlines
	  org-table-last-column-widths
	  org-table-last-alignment
	  buffer loc)
      (setq form (org-table-convert-refs-to-rc form))
      (org-with-wide-buffer
       (goto-char (point-min))
       (if (re-search-forward
	    (concat "^[ \t]*#\\+\\(tbl\\)?name:[ \t]*"
		    (regexp-quote name-or-id) "[ \t]*$")
	    nil t)
	   (setq buffer (current-buffer) loc (match-beginning 0))
	 (setq id-loc (org-id-find name-or-id 'marker))
	 (unless (and id-loc (markerp id-loc))
	   (user-error "Can't find remote table \"%s\"" name-or-id))
	 (setq buffer (marker-buffer id-loc)
	       loc (marker-position id-loc))
	 (move-marker id-loc nil))
       (with-current-buffer buffer
	 (org-with-wide-buffer
	  (goto-char loc)
	  (forward-char 1)
	  (unless (and (re-search-forward "^\\(\\*+ \\)\\|^[ \t]*|" nil t)
		       (not (match-beginning 1)))
	    (user-error "Cannot find a table at NAME or ID %s" name-or-id))
	  (org-table-analyze)
	  (setq form (org-table-formula-substitute-names
		      (org-table-formula-handle-first/last-rc form)))
	  (if (and (string-match org-table-range-regexp form)
		   (> (length (match-string 0 form)) 1))
	      (org-table-get-range
	       (match-string 0 form) org-table-current-begin-pos 1)
	    form)))))))

(defun org-table-remote-reference-indirection (form)
  "Return formula with table remote references substituted by indirection.
For example \"remote($1, @>$2)\" => \"remote(year_2013, @>$1)\".
This indirection works only with the format @ROW$COLUMN.  The
format \"B3\" is not supported because it can not be
distinguished from a plain table name or ID."
  (let ((regexp
	 ;; Same as in `org-table-eval-formula'.
	 (concat "\\<remote([ \t]*\\("
		 ;; Allow "$1", "@<", "$-1", "@<<$1" etc.
		 "[@$][^ \t,]+"
		 "\\)[ \t]*,[ \t]*\\([^\n)]+\\))")))
    (replace-regexp-in-string
     regexp
     (lambda (m)
       (save-match-data
	 (let ((eq (org-table-formula-handle-first/last-rc (match-string 1 m))))
	   (org-table-get-range
	    (if (string-match-p "\\`\\$[0-9]+\\'" eq)
		(concat "@0" eq)
	      eq)))))
     form t t 1)))

(defmacro org-define-lookup-function (mode)
  (let ((mode-str (symbol-name mode))
	(first-p (eq mode 'first))
	(all-p (eq mode 'all)))
    (let ((plural-str (if all-p "s" "")))
      `(defun ,(intern (format "org-lookup-%s" mode-str)) (val s-list r-list &optional predicate)
	 ,(format "Find %s occurrence%s of VAL in S-LIST; return corresponding element%s of R-LIST.
If R-LIST is nil, return matching element%s of S-LIST.
If PREDICATE is not nil, use it instead of `equal' to match VAL.
Matching is done by (PREDICATE VAL S), where S is an element of S-LIST.
This function is generated by a call to the macro `org-define-lookup-function'."
		  mode-str plural-str plural-str plural-str)
	 (let ,(let ((lvars '((p (or predicate 'equal))
			      (sl s-list)
			      (rl (or r-list s-list))
			      (ret nil))))
		 (if first-p (cons '(match-p nil) lvars) lvars))
	   (while ,(if first-p '(and (not match-p) sl) 'sl)
	     (when (funcall p val (car sl))
	       ,(when first-p '(setq match-p t))
	       (let ((rval (car rl)))
		 (setq ret ,(if all-p '(append ret (list rval)) 'rval))))
	     (setq sl (cdr sl) rl (cdr rl)))
	   ret)))))

(org-define-lookup-function first)
(org-define-lookup-function last)
(org-define-lookup-function all)

(provide 'org-table)

;; Local variables:
;; generated-autoload-file: "org-loaddefs.el"
;; End:

;;; org-table.el ends here

debug log:

solving a21587acb ...
found a21587acb in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.