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
| | ;;; org-element.el --- Parser And Applications for Org syntax
;; Copyright (C) 2012 Free Software Foundation, Inc.
;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
;; Keywords: outlines, hypermedia, calendar, wp
;; This program 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.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; This file is not part of GNU Emacs.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Org syntax can be divided into three categories: "Greater
;; elements", "Elements" and "Objects".
;;
;; Elements are related to the structure of the document. Indeed, all
;; elements are a cover for the document: each position within belongs
;; to at least one element.
;;
;; An element always starts and ends at the beginning of a line. With
;; a few exceptions (namely `babel-call', `clock', `headline', `item',
;; `keyword', `planning', `property-drawer' and `section' types), it
;; can also accept a fixed set of keywords as attributes. Those are
;; called "affiliated keywords" to distinguish them from other
;; keywords, which are full-fledged elements. Almost all affiliated
;; keywords are referenced in `org-element-affiliated-keywords'; the
;; others are export attributes and start with "ATTR_" prefix.
;;
;; Element containing other elements (and only elements) are called
;; greater elements. Concerned types are: `center-block', `drawer',
;; `dynamic-block', `footnote-definition', `headline', `inlinetask',
;; `item', `plain-list', `quote-block', `section' and `special-block'.
;;
;; Other element types are: `babel-call', `clock', `comment',
;; `comment-block', `example-block', `export-block', `fixed-width',
;; `horizontal-rule', `keyword', `latex-environment', `paragraph',
;; `planning', `property-drawer', `quote-section', `src-block',
;; `table', `table-row' and `verse-block'. Among them, `paragraph'
;; and `verse-block' types can contain Org objects and plain text.
;;
;; Objects are related to document's contents. Some of them are
;; recursive. Associated types are of the following: `bold', `code',
;; `entity', `export-snippet', `footnote-reference',
;; `inline-babel-call', `inline-src-block', `italic',
;; `latex-fragment', `line-break', `link', `macro', `radio-target',
;; `statistics-cookie', `strike-through', `subscript', `superscript',
;; `table-cell', `target', `timestamp', `underline' and `verbatim'.
;;
;; Some elements also have special properties whose value can hold
;; objects themselves (i.e. an item tag or an headline name). Such
;; values are called "secondary strings". Any object belongs to
;; either an element or a secondary string.
;;
;; Notwithstanding affiliated keywords, each greater element, element
;; and object has a fixed set of properties attached to it. Among
;; them, four are shared by all types: `:begin' and `:end', which
;; refer to the beginning and ending buffer positions of the
;; considered element or object, `:post-blank', which holds the number
;; of blank lines, or white spaces, at its end and `:parent' which
;; refers to the element or object containing it. Greater elements
;; and elements containing objects will also have `:contents-begin'
;; and `:contents-end' properties to delimit contents.
;;
;; Lisp-wise, an element or an object can be represented as a list.
;; It follows the pattern (TYPE PROPERTIES CONTENTS), where:
;; TYPE is a symbol describing the Org element or object.
;; PROPERTIES is the property list attached to it. See docstring of
;; appropriate parsing function to get an exhaustive
;; list.
;; CONTENTS is a list of elements, objects or raw strings contained
;; in the current element or object, when applicable.
;;
;; An Org buffer is a nested list of such elements and objects, whose
;; type is `org-data' and properties is nil.
;;
;; The first part of this file defines Org syntax, while the second
;; one provide accessors and setters functions.
;;
;; The next part implements a parser and an interpreter for each
;; element and object type in Org syntax.
;;
;; The following part creates a fully recursive buffer parser. It
;; also provides a tool to map a function to elements or objects
;; matching some criteria in the parse tree. Functions of interest
;; are `org-element-parse-buffer', `org-element-map' and, to a lesser
;; extent, `org-element-parse-secondary-string'.
;;
;; The penultimate part is the cradle of an interpreter for the
;; obtained parse tree: `org-element-interpret-data'.
;;
;; The library ends by furnishing `org-element-at-point' function, and
;; a way to give information about document structure around point
;; with `org-element-context'.
;;; Code:
(eval-when-compile
(require 'cl))
(require 'org)
\f
;;; Definitions And Rules
;;
;; Define elements, greater elements and specify recursive objects,
;; along with the affiliated keywords recognized. Also set up
;; restrictions on recursive objects combinations.
;;
;; These variables really act as a control center for the parsing
;; process.
(defconst org-element-paragraph-separate
(concat "^\\(?:"
;; Headlines, inlinetasks.
org-outline-regexp "\\|"
;; Footnote definitions.
"\\[\\(?:[0-9]+\\|fn:[-_[:word:]]+\\)\\]" "\\|"
"[ \t]*\\(?:"
;; Empty lines.
"$" "\\|"
;; Tables (any type).
"\\(?:|\\|\\+-[-+]\\)" "\\|"
;; Blocks (any type), Babel calls, drawers (any type),
;; fixed-width areas and keywords. Note: this is only an
;; indication and need some thorough check.
"[#:]" "\\|"
;; Horizontal rules.
"-\\{5,\\}[ \t]*$" "\\|"
;; LaTeX environments.
"\\\\\\(begin\\|end\\)" "\\|"
;; Planning and Clock lines.
(regexp-opt (list org-scheduled-string
org-deadline-string
org-closed-string
org-clock-string))
"\\|"
;; Lists.
(let ((term (case org-plain-list-ordered-item-terminator
(?\) ")") (?. "\\.") (otherwise "[.)]")))
(alpha (and org-alphabetical-lists "\\|[A-Za-z]")))
(concat "\\(?:[-+*]\\|\\(?:[0-9]+" alpha "\\)" term "\\)"
"\\(?:[ \t]\\|$\\)"))
"\\)\\)")
"Regexp to separate paragraphs in an Org buffer.
In the case of lines starting with \"#\" and \":\", this regexp
is not sufficient to know if point is at a paragraph ending. See
`org-element-paragraph-parser' for more information.")
(defconst org-element-all-elements
'(center-block clock comment comment-block drawer dynamic-block example-block
export-block fixed-width footnote-definition headline
horizontal-rule inlinetask item keyword latex-environment
babel-call paragraph plain-list planning property-drawer
quote-block quote-section section special-block src-block table
table-row verse-block)
"Complete list of element types.")
(defconst org-element-greater-elements
'(center-block drawer dynamic-block footnote-definition headline inlinetask
item plain-list quote-block section special-block table)
"List of recursive element types aka Greater Elements.")
(defconst org-element-all-successors
'(export-snippet footnote-reference inline-babel-call inline-src-block
latex-or-entity line-break link macro radio-target
statistics-cookie sub/superscript table-cell target
text-markup timestamp)
"Complete list of successors.")
(defconst org-element-object-successor-alist
'((subscript . sub/superscript) (superscript . sub/superscript)
(bold . text-markup) (code . text-markup) (italic . text-markup)
(strike-through . text-markup) (underline . text-markup)
(verbatim . text-markup) (entity . latex-or-entity)
(latex-fragment . latex-or-entity))
"Alist of translations between object type and successor name.
Sharing the same successor comes handy when, for example, the
regexp matching one object can also match the other object.")
(defconst org-element-all-objects
'(bold code entity export-snippet footnote-reference inline-babel-call
inline-src-block italic line-break latex-fragment link macro
radio-target statistics-cookie strike-through subscript superscript
table-cell target timestamp underline verbatim)
"Complete list of object types.")
(defconst org-element-recursive-objects
'(bold italic link macro subscript radio-target strike-through superscript
table-cell underline)
"List of recursive object types.")
(defconst org-element-block-name-alist
'(("CENTER" . org-element-center-block-parser)
("COMMENT" . org-element-comment-block-parser)
("EXAMPLE" . org-element-example-block-parser)
("QUOTE" . org-element-quote-block-parser)
("SRC" . org-element-src-block-parser)
("VERSE" . org-element-verse-block-parser))
"Alist between block names and the associated parsing function.
Names must be uppercase. Any block whose name has no association
is parsed with `org-element-special-block-parser'.")
(defconst org-element-affiliated-keywords
'("CAPTION" "DATA" "HEADER" "HEADERS" "LABEL" "NAME" "PLOT" "RESNAME" "RESULT"
"RESULTS" "SOURCE" "SRCNAME" "TBLNAME")
"List of affiliated keywords as strings.
By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
are affiliated keywords and need not to be in this list.")
(defconst org-element--affiliated-re
(format "[ \t]*#\\+%s:"
;; Regular affiliated keywords.
(format "\\(%s\\|ATTR_[-_A-Za-z0-9]+\\)\\(?:\\[\\(.*\\)\\]\\)?"
(regexp-opt org-element-affiliated-keywords)))
"Regexp matching any affiliated keyword.
Keyword name is put in match group 1. Moreover, if keyword
belongs to `org-element-dual-keywords', put the dual value in
match group 2.
Don't modify it, set `org-element-affiliated-keywords' instead.")
(defconst org-element-keyword-translation-alist
'(("DATA" . "NAME") ("LABEL" . "NAME") ("RESNAME" . "NAME")
("SOURCE" . "NAME") ("SRCNAME" . "NAME") ("TBLNAME" . "NAME")
("RESULT" . "RESULTS") ("HEADERS" . "HEADER"))
"Alist of usual translations for keywords.
The key is the old name and the value the new one. The property
holding their value will be named after the translated name.")
(defconst org-element-multiple-keywords '("HEADER")
"List of affiliated keywords that can occur more that once in an element.
Their value will be consed into a list of strings, which will be
returned as the value of the property.
This list is checked after translations have been applied. See
`org-element-keyword-translation-alist'.
By default, all keywords setting attributes (i.e. \"ATTR_LATEX\")
allow multiple occurrences and need not to be in this list.")
(defconst org-element-parsed-keywords '("AUTHOR" "CAPTION" "DATE" "TITLE")
"List of keywords whose value can be parsed.
Their value will be stored as a secondary string: a list of
strings and objects.
This list is checked after translations have been applied. See
`org-element-keyword-translation-alist'.")
(defconst org-element-dual-keywords '("CAPTION" "RESULTS")
"List of keywords which can have a secondary value.
In Org syntax, they can be written with optional square brackets
before the colons. For example, results keyword can be
associated to a hash value with the following:
#+RESULTS[hash-string]: some-source
This list is checked after translations have been applied. See
`org-element-keyword-translation-alist'.")
(defconst org-element-object-restrictions
'((bold export-snippet inline-babel-call inline-src-block latex-or-entity link
radio-target sub/superscript target text-markup timestamp)
(footnote-reference export-snippet footnote-reference inline-babel-call
inline-src-block latex-or-entity line-break link macro
radio-target sub/superscript target text-markup
timestamp)
(headline inline-babel-call inline-src-block latex-or-entity link macro
radio-target statistics-cookie sub/superscript target text-markup
timestamp)
(inlinetask inline-babel-call inline-src-block latex-or-entity link macro
radio-target sub/superscript target text-markup timestamp)
(italic export-snippet inline-babel-call inline-src-block latex-or-entity
link radio-target sub/superscript target text-markup timestamp)
(item export-snippet footnote-reference inline-babel-call latex-or-entity
link macro radio-target sub/superscript target text-markup)
(keyword latex-or-entity macro sub/superscript text-markup)
(link export-snippet inline-babel-call inline-src-block latex-or-entity link
sub/superscript text-markup)
(macro macro)
(paragraph export-snippet footnote-reference inline-babel-call
inline-src-block latex-or-entity line-break link macro
radio-target statistics-cookie sub/superscript target text-markup
timestamp)
(radio-target export-snippet latex-or-entity sub/superscript)
(strike-through export-snippet inline-babel-call inline-src-block
latex-or-entity link radio-target sub/superscript target
text-markup timestamp)
(subscript export-snippet inline-babel-call inline-src-block latex-or-entity
sub/superscript target text-markup)
(superscript export-snippet inline-babel-call inline-src-block
latex-or-entity sub/superscript target text-markup)
(table-cell export-snippet latex-or-entity link macro radio-target
sub/superscript target text-markup timestamp)
(table-row table-cell)
(underline export-snippet inline-babel-call inline-src-block latex-or-entity
link radio-target sub/superscript target text-markup timestamp)
(verse-block footnote-reference inline-babel-call inline-src-block
latex-or-entity line-break link macro radio-target
sub/superscript target text-markup timestamp))
"Alist of objects restrictions.
CAR is an element or object type containing objects and CDR is
a list of successors that will be called within an element or
object of such type.
For example, in a `radio-target' object, one can only find
entities, export snippets, latex-fragments, subscript and
superscript.
This alist also applies to secondary string. For example, an
`headline' type element doesn't directly contain objects, but
still has an entry since one of its properties (`:title') does.")
(defconst org-element-secondary-value-alist
'((headline . :title)
(inlinetask . :title)
(item . :tag)
(footnote-reference . :inline-definition))
"Alist between element types and location of secondary value.")
\f
;;; Accessors and Setters
;;
;; Provide four accessors: `org-element-type', `org-element-property'
;; `org-element-contents' and `org-element-restriction'.
;;
;; Setter functions allow to modify elements by side effect. There is
;; `org-element-put-property', `org-element-set-contents',
;; `org-element-set-element' and `org-element-adopt-element'. Note
;; that `org-element-set-element' and `org-element-adopt-elements' are
;; higher level functions since also update `:parent' property.
(defsubst org-element-type (element)
"Return type of ELEMENT.
The function returns the type of the element or object provided.
It can also return the following special value:
`plain-text' for a string
`org-data' for a complete document
nil in any other case."
(cond
((not (consp element)) (and (stringp element) 'plain-text))
((symbolp (car element)) (car element))))
(defsubst org-element-property (property element)
"Extract the value from the PROPERTY of an ELEMENT."
(plist-get (nth 1 element) property))
(defsubst org-element-contents (element)
"Extract contents from an ELEMENT."
(and (consp element) (nthcdr 2 element)))
(defsubst org-element-restriction (element)
"Return restriction associated to ELEMENT.
ELEMENT can be an element, an object or a symbol representing an
element or object type."
(cdr (assq (if (symbolp element) element (org-element-type element))
org-element-object-restrictions)))
(defsubst org-element-put-property (element property value)
"In ELEMENT set PROPERTY to VALUE.
Return modified element."
(when (consp element)
(setcar (cdr element) (plist-put (nth 1 element) property value)))
element)
(defsubst org-element-set-contents (element &rest contents)
"Set ELEMENT contents to CONTENTS.
Return modified element."
(cond ((not element) (list contents))
((cdr element) (setcdr (cdr element) contents))
(t (nconc element contents))))
(defsubst org-element-set-element (old new)
"Replace element or object OLD with element or object NEW.
The function takes care of setting `:parent' property for NEW."
;; Since OLD is going to be changed into NEW by side-effect, first
;; make sure that every element or object within NEW has OLD as
;; parent.
(mapc (lambda (blob) (org-element-put-property blob :parent old))
(org-element-contents new))
;; Transfer contents.
(apply 'org-element-set-contents old (org-element-contents new))
;; Ensure NEW has same parent as OLD, then overwrite OLD properties
;; with NEW's.
(org-element-put-property new :parent (org-element-property :parent old))
(setcar (cdr old) (nth 1 new))
;; Transfer type.
(setcar old (car new)))
(defsubst org-element-adopt-elements (parent &rest children)
"Append elements to the contents of another element.
PARENT is an element or object. CHILDREN can be elements,
objects, or a strings.
The function takes care of setting `:parent' property for CHILD.
Return parent element."
(if (not parent) children
;; Link every child to PARENT.
(mapc (lambda (child)
(unless (stringp child)
(org-element-put-property child :parent parent)))
children)
;; Add CHILDREN at the end of PARENT contents.
(apply 'org-element-set-contents
parent
(nconc (org-element-contents parent) children))
;; Return modified PARENT element.
parent))
\f
;;; Greater elements
;;
;; For each greater element type, we define a parser and an
;; interpreter.
;;
;; A parser returns the element or object as the list described above.
;; Most of them accepts no argument. Though, exceptions exist. Hence
;; every element containing a secondary string (see
;; `org-element-secondary-value-alist') will accept an optional
;; argument to toggle parsing of that secondary string. Moreover,
;; `item' parser requires current list's structure as its first
;; element.
;;
;; An interpreter accepts two arguments: the list representation of
;; the element or object, and its contents. The latter may be nil,
;; depending on the element or object considered. It returns the
;; appropriate Org syntax, as a string.
;;
;; Parsing functions must follow the naming convention:
;; org-element-TYPE-parser, where TYPE is greater element's type, as
;; defined in `org-element-greater-elements'.
;;
;; Similarly, interpreting functions must follow the naming
;; convention: org-element-TYPE-interpreter.
;;
;; With the exception of `headline' and `item' types, greater elements
;; cannot contain other greater elements of their own type.
;;
;; Beside implementing a parser and an interpreter, adding a new
;; greater element requires to tweak `org-element--current-element'.
;; Moreover, the newly defined type must be added to both
;; `org-element-all-elements' and `org-element-greater-elements'.
;;;; Center Block
(defun org-element-center-block-parser (limit)
"Parse a center block.
LIMIT bounds the search.
Return a list whose CAR is `center-block' and CDR is a plist
containing `:begin', `:end', `:hiddenp', `:contents-begin',
`:contents-end' and `:post-blank' keywords.
Assume point is at the beginning of the block."
(let ((case-fold-search t))
(if (not (save-excursion
(re-search-forward "^[ \t]*#\\+END_CENTER" limit t)))
;; Incomplete block: parse it as a paragraph.
(org-element-paragraph-parser limit)
(let ((block-end-line (match-beginning 0)))
(let* ((keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
;; Empty blocks have no contents.
(contents-begin (progn (forward-line)
(and (< (point) block-end-line)
(point))))
(contents-end (and contents-begin block-end-line))
(hidden (org-invisible-p2))
(pos-before-blank (progn (goto-char block-end-line)
(forward-line)
(point)))
(end (save-excursion (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'center-block
(nconc
(list :begin begin
:end end
:hiddenp hidden
:contents-begin contents-begin
:contents-end contents-end
:post-blank (count-lines pos-before-blank end))
(cadr keywords))))))))
(defun org-element-center-block-interpreter (center-block contents)
"Interpret CENTER-BLOCK element as Org syntax.
CONTENTS is the contents of the element."
(format "#+BEGIN_CENTER\n%s#+END_CENTER" contents))
;;;; Drawer
(defun org-element-drawer-parser (limit)
"Parse a drawer.
LIMIT bounds the search.
Return a list whose CAR is `drawer' and CDR is a plist containing
`:drawer-name', `:begin', `:end', `:hiddenp', `:contents-begin',
`:contents-end' and `:post-blank' keywords.
Assume point is at beginning of drawer."
(let ((case-fold-search t))
(if (not (save-excursion (re-search-forward "^[ \t]*:END:" limit t)))
;; Incomplete drawer: parse it as a paragraph.
(org-element-paragraph-parser limit)
(let ((drawer-end-line (match-beginning 0)))
(save-excursion
(let* ((case-fold-search t)
(name (progn (looking-at org-drawer-regexp)
(org-match-string-no-properties 1)))
(keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
;; Empty drawers have no contents.
(contents-begin (progn (forward-line)
(and (< (point) drawer-end-line)
(point))))
(contents-end (and contents-begin drawer-end-line))
(hidden (org-invisible-p2))
(pos-before-blank (progn (goto-char drawer-end-line)
(forward-line)
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'drawer
(nconc
(list :begin begin
:end end
:drawer-name name
:hiddenp hidden
:contents-begin contents-begin
:contents-end contents-end
:post-blank (count-lines pos-before-blank end))
(cadr keywords)))))))))
(defun org-element-drawer-interpreter (drawer contents)
"Interpret DRAWER element as Org syntax.
CONTENTS is the contents of the element."
(format ":%s:\n%s:END:"
(org-element-property :drawer-name drawer)
contents))
;;;; Dynamic Block
(defun org-element-dynamic-block-parser (limit)
"Parse a dynamic block.
LIMIT bounds the search.
Return a list whose CAR is `dynamic-block' and CDR is a plist
containing `:block-name', `:begin', `:end', `:hiddenp',
`:contents-begin', `:contents-end', `:arguments' and
`:post-blank' keywords.
Assume point is at beginning of dynamic block."
(let ((case-fold-search t))
(if (not (save-excursion (re-search-forward org-dblock-end-re limit t)))
;; Incomplete block: parse it as a paragraph.
(org-element-paragraph-parser limit)
(let ((block-end-line (match-beginning 0)))
(save-excursion
(let* ((name (progn (looking-at org-dblock-start-re)
(org-match-string-no-properties 1)))
(arguments (org-match-string-no-properties 3))
(keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
;; Empty blocks have no contents.
(contents-begin (progn (forward-line)
(and (< (point) block-end-line)
(point))))
(contents-end (and contents-begin block-end-line))
(hidden (org-invisible-p2))
(pos-before-blank (progn (goto-char block-end-line)
(forward-line)
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'dynamic-block
(nconc
(list :begin begin
:end end
:block-name name
:arguments arguments
:hiddenp hidden
:contents-begin contents-begin
:contents-end contents-end
:post-blank (count-lines pos-before-blank end))
(cadr keywords)))))))))
(defun org-element-dynamic-block-interpreter (dynamic-block contents)
"Interpret DYNAMIC-BLOCK element as Org syntax.
CONTENTS is the contents of the element."
(format "#+BEGIN: %s%s\n%s#+END:"
(org-element-property :block-name dynamic-block)
(let ((args (org-element-property :arguments dynamic-block)))
(and args (concat " " args)))
contents))
;;;; Footnote Definition
(defun org-element-footnote-definition-parser (limit)
"Parse a footnote definition.
LIMIT bounds the search.
Return a list whose CAR is `footnote-definition' and CDR is
a plist containing `:label', `:begin' `:end', `:contents-begin',
`:contents-end' and `:post-blank' keywords.
Assume point is at the beginning of the footnote definition."
(save-excursion
(let* ((label (progn (looking-at org-footnote-definition-re)
(org-match-string-no-properties 1)))
(keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(ending (save-excursion
(if (progn
(end-of-line)
(re-search-forward
(concat org-outline-regexp-bol "\\|"
org-footnote-definition-re "\\|"
"^[ \t]*$") limit 'move))
(match-beginning 0)
(point))))
(contents-begin (progn (search-forward "]")
(skip-chars-forward " \r\t\n" ending)
(and (/= (point) ending) (point))))
(contents-end (and contents-begin ending))
(end (progn (goto-char ending)
(skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'footnote-definition
(nconc
(list :label label
:begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:post-blank (count-lines ending end))
(cadr keywords))))))
(defun org-element-footnote-definition-interpreter (footnote-definition contents)
"Interpret FOOTNOTE-DEFINITION element as Org syntax.
CONTENTS is the contents of the footnote-definition."
(concat (format "[%s]" (org-element-property :label footnote-definition))
" "
contents))
;;;; Headline
(defun org-element-headline-parser (limit &optional raw-secondary-p)
"Parse an headline.
Return a list whose CAR is `headline' and CDR is a plist
containing `:raw-value', `:title', `:begin', `:end',
`:pre-blank', `:hiddenp', `:contents-begin' and `:contents-end',
`:level', `:priority', `:tags', `:todo-keyword',`:todo-type',
`:scheduled', `:deadline', `:timestamp', `:clock', `:category',
`:quotedp', `:archivedp', `:commentedp' and `:footnote-section-p'
keywords.
The plist also contains any property set in the property drawer,
with its name in lowercase, the underscores replaced with hyphens
and colons at the beginning (i.e. `:custom-id').
When RAW-SECONDARY-P is non-nil, headline's title will not be
parsed as a secondary string, but as a plain string instead.
Assume point is at beginning of the headline."
(save-excursion
(let* ((components (org-heading-components))
(level (nth 1 components))
(todo (nth 2 components))
(todo-type
(and todo (if (member todo org-done-keywords) 'done 'todo)))
(tags (let ((raw-tags (nth 5 components)))
(and raw-tags (org-split-string raw-tags ":"))))
(raw-value (nth 4 components))
(quotedp
(let ((case-fold-search nil))
(string-match (format "^%s +" org-quote-string) raw-value)))
(commentedp
(let ((case-fold-search nil))
(string-match (format "^%s +" org-comment-string) raw-value)))
(archivedp (member org-archive-tag tags))
(footnote-section-p (and org-footnote-section
(string= org-footnote-section raw-value)))
;; Normalize property names: ":SOME_PROP:" becomes
;; ":some-prop".
(standard-props (let (plist)
(mapc
(lambda (p)
(let ((p-name (downcase (car p))))
(while (string-match "_" p-name)
(setq p-name
(replace-match "-" nil nil p-name)))
(setq p-name (intern (concat ":" p-name)))
(setq plist
(plist-put plist p-name (cdr p)))))
(org-entry-properties nil 'standard))
plist))
(time-props (org-entry-properties nil 'special "CLOCK"))
(scheduled (cdr (assoc "SCHEDULED" time-props)))
(deadline (cdr (assoc "DEADLINE" time-props)))
(clock (cdr (assoc "CLOCK" time-props)))
(timestamp (cdr (assoc "TIMESTAMP" time-props)))
(begin (point))
(end (save-excursion (goto-char (org-end-of-subtree t t))))
(pos-after-head (progn (forward-line) (point)))
(contents-begin (save-excursion
(skip-chars-forward " \r\t\n" end)
(and (/= (point) end) (line-beginning-position))))
(hidden (org-invisible-p2))
(contents-end (and contents-begin
(progn (goto-char end)
(skip-chars-backward " \r\t\n")
(forward-line)
(point)))))
;; Clean RAW-VALUE from any quote or comment string.
(when (or quotedp commentedp)
(setq raw-value
(replace-regexp-in-string
(concat "\\(" org-quote-string "\\|" org-comment-string "\\) +")
""
raw-value)))
;; Clean TAGS from archive tag, if any.
(when archivedp (setq tags (delete org-archive-tag tags)))
(let ((headline
(list 'headline
(nconc
(list :raw-value raw-value
:begin begin
:end end
:pre-blank
(if (not contents-begin) 0
(count-lines pos-after-head contents-begin))
:hiddenp hidden
:contents-begin contents-begin
:contents-end contents-end
:level level
:priority (nth 3 components)
:tags tags
:todo-keyword todo
:todo-type todo-type
:scheduled scheduled
:deadline deadline
:timestamp timestamp
:clock clock
:post-blank (count-lines
(if (not contents-end) pos-after-head
(goto-char contents-end)
(forward-line)
(point))
end)
:footnote-section-p footnote-section-p
:archivedp archivedp
:commentedp commentedp
:quotedp quotedp)
standard-props))))
(org-element-put-property
headline :title
(if raw-secondary-p raw-value
(org-element-parse-secondary-string
raw-value (org-element-restriction 'headline) headline)))))))
(defun org-element-headline-interpreter (headline contents)
"Interpret HEADLINE element as Org syntax.
CONTENTS is the contents of the element."
(let* ((level (org-element-property :level headline))
(todo (org-element-property :todo-keyword headline))
(priority (org-element-property :priority headline))
(title (org-element-interpret-data
(org-element-property :title headline)))
(tags (let ((tag-list (if (org-element-property :archivedp headline)
(cons org-archive-tag
(org-element-property :tags headline))
(org-element-property :tags headline))))
(and tag-list
(format ":%s:" (mapconcat 'identity tag-list ":")))))
(commentedp (org-element-property :commentedp headline))
(quotedp (org-element-property :quotedp headline))
(pre-blank (or (org-element-property :pre-blank headline) 0))
(heading (concat (make-string level ?*)
(and todo (concat " " todo))
(and quotedp (concat " " org-quote-string))
(and commentedp (concat " " org-comment-string))
(and priority
(format " [#%s]" (char-to-string priority)))
(cond ((and org-footnote-section
(org-element-property
:footnote-section-p headline))
(concat " " org-footnote-section))
(title (concat " " title))))))
(concat heading
;; Align tags.
(when tags
(cond
((zerop org-tags-column) (format " %s" tags))
((< org-tags-column 0)
(concat
(make-string
(max (- (+ org-tags-column (length heading) (length tags))) 1)
? )
tags))
(t
(concat
(make-string (max (- org-tags-column (length heading)) 1) ? )
tags))))
(make-string (1+ pre-blank) 10)
contents)))
;;;; Inlinetask
(defun org-element-inlinetask-parser (limit &optional raw-secondary-p)
"Parse an inline task.
Return a list whose CAR is `inlinetask' and CDR is a plist
containing `:title', `:begin', `:end', `:hiddenp',
`:contents-begin' and `:contents-end', `:level', `:priority',
`:tags', `:todo-keyword', `:todo-type', `:scheduled',
`:deadline', `:timestamp', `:clock' and `:post-blank' keywords.
The plist also contains any property set in the property drawer,
with its name in lowercase, the underscores replaced with hyphens
and colons at the beginning (i.e. `:custom-id').
When optional argument RAW-SECONDARY-P is non-nil, inline-task's
title will not be parsed as a secondary string, but as a plain
string instead.
Assume point is at beginning of the inline task."
(save-excursion
(let* ((keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(components (org-heading-components))
(todo (nth 2 components))
(todo-type (and todo
(if (member todo org-done-keywords) 'done 'todo)))
(tags (let ((raw-tags (nth 5 components)))
(and raw-tags (org-split-string raw-tags ":"))))
;; Normalize property names: ":SOME_PROP:" becomes
;; ":some-prop".
(standard-props (let (plist)
(mapc
(lambda (p)
(let ((p-name (downcase (car p))))
(while (string-match "_" p-name)
(setq p-name
(replace-match "-" nil nil p-name)))
(setq p-name (intern (concat ":" p-name)))
(setq plist
(plist-put plist p-name (cdr p)))))
(org-entry-properties nil 'standard))
plist))
(time-props (org-entry-properties nil 'special "CLOCK"))
(scheduled (cdr (assoc "SCHEDULED" time-props)))
(deadline (cdr (assoc "DEADLINE" time-props)))
(clock (cdr (assoc "CLOCK" time-props)))
(timestamp (cdr (assoc "TIMESTAMP" time-props)))
(task-end (save-excursion
(end-of-line)
(and (re-search-forward "^\\*+ END" limit t)
(match-beginning 0))))
(contents-begin (progn (forward-line)
(and task-end (< (point) task-end) (point))))
(hidden (and contents-begin (org-invisible-p2)))
(contents-end (and contents-begin task-end))
(before-blank (if (not task-end) (point)
(goto-char task-end)
(forward-line)
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol))))
(inlinetask
(list 'inlinetask
(nconc
(list :begin begin
:end end
:hiddenp hidden
:contents-begin contents-begin
:contents-end contents-end
:level (nth 1 components)
:priority (nth 3 components)
:tags tags
:todo-keyword todo
:todo-type todo-type
:scheduled scheduled
:deadline deadline
:timestamp timestamp
:clock clock
:post-blank (count-lines before-blank end))
standard-props
(cadr keywords)))))
(org-element-put-property
inlinetask :title
(if raw-secondary-p (nth 4 components)
(org-element-parse-secondary-string
(nth 4 components)
(org-element-restriction 'inlinetask)
inlinetask))))))
(defun org-element-inlinetask-interpreter (inlinetask contents)
"Interpret INLINETASK element as Org syntax.
CONTENTS is the contents of inlinetask."
(let* ((level (org-element-property :level inlinetask))
(todo (org-element-property :todo-keyword inlinetask))
(priority (org-element-property :priority inlinetask))
(title (org-element-interpret-data
(org-element-property :title inlinetask)))
(tags (let ((tag-list (org-element-property :tags inlinetask)))
(and tag-list
(format ":%s:" (mapconcat 'identity tag-list ":")))))
(task (concat (make-string level ?*)
(and todo (concat " " todo))
(and priority
(format " [#%s]" (char-to-string priority)))
(and title (concat " " title)))))
(concat task
;; Align tags.
(when tags
(cond
((zerop org-tags-column) (format " %s" tags))
((< org-tags-column 0)
(concat
(make-string
(max (- (+ org-tags-column (length task) (length tags))) 1)
? )
tags))
(t
(concat
(make-string (max (- org-tags-column (length task)) 1) ? )
tags))))
;; Prefer degenerate inlinetasks when there are no
;; contents.
(when contents
(concat "\n"
contents
(make-string level ?*) " END")))))
;;;; Item
(defun org-element-item-parser (limit struct &optional raw-secondary-p)
"Parse an item.
STRUCT is the structure of the plain list.
Return a list whose CAR is `item' and CDR is a plist containing
`:bullet', `:begin', `:end', `:contents-begin', `:contents-end',
`:checkbox', `:counter', `:tag', `:structure', `:hiddenp' and
`:post-blank' keywords.
When optional argument RAW-SECONDARY-P is non-nil, item's tag, if
any, will not be parsed as a secondary string, but as a plain
string instead.
Assume point is at the beginning of the item."
(save-excursion
(beginning-of-line)
(looking-at org-list-full-item-re)
(let* ((begin (point))
(bullet (org-match-string-no-properties 1))
(checkbox (let ((box (org-match-string-no-properties 3)))
(cond ((equal "[ ]" box) 'off)
((equal "[X]" box) 'on)
((equal "[-]" box) 'trans))))
(counter (let ((c (org-match-string-no-properties 2)))
(save-match-data
(cond
((not c) nil)
((string-match "[A-Za-z]" c)
(- (string-to-char (upcase (match-string 0 c)))
64))
((string-match "[0-9]+" c)
(string-to-number (match-string 0 c)))))))
(end (save-excursion (goto-char (org-list-get-item-end begin struct))
(unless (bolp) (forward-line))
(point)))
(contents-begin
(progn (goto-char
;; Ignore tags in un-ordered lists: they are just
;; a part of item's body.
(if (and (match-beginning 4)
(save-match-data (string-match "[.)]" bullet)))
(match-beginning 4)
(match-end 0)))
(skip-chars-forward " \r\t\n" limit)
;; If first line isn't empty, contents really start
;; at the text after item's meta-data.
(if (= (point-at-bol) begin) (point) (point-at-bol))))
(hidden (progn (forward-line)
(and (not (= (point) end)) (org-invisible-p2))))
(contents-end (progn (goto-char end)
(skip-chars-backward " \r\t\n")
(forward-line)
(point)))
(item
(list 'item
(list :bullet bullet
:begin begin
:end end
;; CONTENTS-BEGIN and CONTENTS-END may be
;; mixed up in the case of an empty item
;; separated from the next by a blank line.
;; Thus ensure the former is always the
;; smallest.
:contents-begin (min contents-begin contents-end)
:contents-end (max contents-begin contents-end)
:checkbox checkbox
:counter counter
:hiddenp hidden
:structure struct
:post-blank (count-lines contents-end end)))))
(org-element-put-property
item :tag
(let ((raw-tag (org-list-get-tag begin struct)))
(and raw-tag
(if raw-secondary-p raw-tag
(org-element-parse-secondary-string
raw-tag (org-element-restriction 'item) item))))))))
(defun org-element-item-interpreter (item contents)
"Interpret ITEM element as Org syntax.
CONTENTS is the contents of the element."
(let* ((bullet (org-list-bullet-string (org-element-property :bullet item)))
(checkbox (org-element-property :checkbox item))
(counter (org-element-property :counter item))
(tag (let ((tag (org-element-property :tag item)))
(and tag (org-element-interpret-data tag))))
;; Compute indentation.
(ind (make-string (length bullet) 32))
(item-starts-with-par-p
(eq (org-element-type (car (org-element-contents item)))
'paragraph)))
;; Indent contents.
(concat
bullet
(and counter (format "[@%d] " counter))
(case checkbox
(on "[X] ")
(off "[ ] ")
(trans "[-] "))
(and tag (format "%s :: " tag))
(let ((contents (replace-regexp-in-string
"\\(^\\)[ \t]*\\S-" ind contents nil nil 1)))
(if item-starts-with-par-p (org-trim contents)
(concat "\n" contents))))))
;;;; Plain List
(defun org-element-plain-list-parser (limit &optional structure)
"Parse a plain list.
Optional argument STRUCTURE, when non-nil, is the structure of
the plain list being parsed.
Return a list whose CAR is `plain-list' and CDR is a plist
containing `:type', `:begin', `:end', `:contents-begin' and
`:contents-end', `:structure' and `:post-blank' keywords.
Assume point is at the beginning of the list."
(save-excursion
(let* ((struct (or structure (org-list-struct)))
(prevs (org-list-prevs-alist struct))
(parents (org-list-parents-alist struct))
(type (org-list-get-list-type (point) struct prevs))
(contents-begin (point))
(keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(contents-end
(progn (goto-char (org-list-get-list-end (point) struct prevs))
(unless (bolp) (forward-line))
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
;; Return value.
(list 'plain-list
(nconc
(list :type type
:begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:structure struct
:post-blank (count-lines contents-end end))
(cadr keywords))))))
(defun org-element-plain-list-interpreter (plain-list contents)
"Interpret PLAIN-LIST element as Org syntax.
CONTENTS is the contents of the element."
(with-temp-buffer
(insert contents)
(goto-char (point-min))
(org-list-repair)
(buffer-string)))
;;;; Quote Block
(defun org-element-quote-block-parser (limit)
"Parse a quote block.
LIMIT bounds the search.
Return a list whose CAR is `quote-block' and CDR is a plist
containing `:begin', `:end', `:hiddenp', `:contents-begin',
`:contents-end' and `:post-blank' keywords.
Assume point is at the beginning of the block."
(let ((case-fold-search t))
(if (not (save-excursion
(re-search-forward "^[ \t]*#\\+END_QUOTE" limit t)))
;; Incomplete block: parse it as a paragraph.
(org-element-paragraph-parser limit)
(let ((block-end-line (match-beginning 0)))
(save-excursion
(let* ((keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
;; Empty blocks have no contents.
(contents-begin (progn (forward-line)
(and (< (point) block-end-line)
(point))))
(contents-end (and contents-begin block-end-line))
(hidden (org-invisible-p2))
(pos-before-blank (progn (goto-char block-end-line)
(forward-line)
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'quote-block
(nconc
(list :begin begin
:end end
:hiddenp hidden
:contents-begin contents-begin
:contents-end contents-end
:post-blank (count-lines pos-before-blank end))
(cadr keywords)))))))))
(defun org-element-quote-block-interpreter (quote-block contents)
"Interpret QUOTE-BLOCK element as Org syntax.
CONTENTS is the contents of the element."
(format "#+BEGIN_QUOTE\n%s#+END_QUOTE" contents))
;;;; Section
(defun org-element-section-parser (limit)
"Parse a section.
LIMIT bounds the search.
Return a list whose CAR is `section' and CDR is a plist
containing `:begin', `:end', `:contents-begin', `contents-end'
and `:post-blank' keywords."
(save-excursion
;; Beginning of section is the beginning of the first non-blank
;; line after previous headline.
(org-with-limited-levels
(let ((begin (point))
(end (progn (goto-char limit) (point)))
(pos-before-blank (progn (skip-chars-backward " \r\t\n")
(forward-line)
(point))))
(list 'section
(list :begin begin
:end end
:contents-begin begin
:contents-end pos-before-blank
:post-blank (count-lines pos-before-blank end)))))))
(defun org-element-section-interpreter (section contents)
"Interpret SECTION element as Org syntax.
CONTENTS is the contents of the element."
contents)
;;;; Special Block
(defun org-element-special-block-parser (limit)
"Parse a special block.
LIMIT bounds the search.
Return a list whose CAR is `special-block' and CDR is a plist
containing `:type', `:begin', `:end', `:hiddenp',
`:contents-begin', `:contents-end' and `:post-blank' keywords.
Assume point is at the beginning of the block."
(let* ((case-fold-search t)
(type (progn (looking-at "[ \t]*#\\+BEGIN_\\(S-+\\)")
(upcase (match-string-no-properties 1)))))
(if (not (save-excursion
(re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
;; Incomplete block: parse it as a paragraph.
(org-element-paragraph-parser limit)
(let ((block-end-line (match-beginning 0)))
(save-excursion
(let* ((keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
;; Empty blocks have no contents.
(contents-begin (progn (forward-line)
(and (< (point) block-end-line)
(point))))
(contents-end (and contents-begin block-end-line))
(hidden (org-invisible-p2))
(pos-before-blank (progn (goto-char block-end-line)
(forward-line)
(point)))
(end (progn (org-skip-whitespace)
(if (eobp) (point) (point-at-bol)))))
(list 'special-block
(nconc
(list :type type
:begin begin
:end end
:hiddenp hidden
:contents-begin contents-begin
:contents-end contents-end
:post-blank (count-lines pos-before-blank end))
(cadr keywords)))))))))
(defun org-element-special-block-interpreter (special-block contents)
"Interpret SPECIAL-BLOCK element as Org syntax.
CONTENTS is the contents of the element."
(let ((block-type (org-element-property :type special-block)))
(format "#+BEGIN_%s\n%s#+END_%s" block-type contents block-type)))
\f
;;; Elements
;;
;; For each element, a parser and an interpreter are also defined.
;; Both follow the same naming convention used for greater elements.
;;
;; Also, as for greater elements, adding a new element type is done
;; through the following steps: implement a parser and an interpreter,
;; tweak `org-element--current-element' so that it recognizes the new
;; type and add that new type to `org-element-all-elements'.
;;
;; As a special case, when the newly defined type is a block type,
;; `org-element-block-name-alist' has to be modified accordingly.
;;;; Babel Call
(defun org-element-babel-call-parser (limit)
"Parse a babel call.
LIMIT bounds the search.
Return a list whose CAR is `babel-call' and CDR is a plist
containing `:begin', `:end', `:info' and `:post-blank' as
keywords."
(save-excursion
(let ((case-fold-search t)
(info (progn (looking-at org-babel-block-lob-one-liner-regexp)
(org-babel-lob-get-info)))
(begin (point-at-bol))
(pos-before-blank (progn (forward-line) (point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'babel-call
(list :begin begin
:end end
:info info
:post-blank (count-lines pos-before-blank end))))))
(defun org-element-babel-call-interpreter (babel-call contents)
"Interpret BABEL-CALL element as Org syntax.
CONTENTS is nil."
(let* ((babel-info (org-element-property :info babel-call))
(main (car babel-info))
(post-options (nth 1 babel-info)))
(concat "#+CALL: "
(if (not (string-match "\\[\\(\\[.*?\\]\\)\\]" main)) main
;; Remove redundant square brackets.
(replace-match (match-string 1 main) nil nil main))
(and post-options (format "[%s]" post-options)))))
;;;; Clock
(defun org-element-clock-parser (limit)
"Parse a clock.
LIMIT bounds the search.
Return a list whose CAR is `clock' and CDR is a plist containing
`:status', `:value', `:time', `:begin', `:end' and `:post-blank'
as keywords."
(save-excursion
(let* ((case-fold-search nil)
(begin (point))
(value (progn (search-forward org-clock-string (line-end-position) t)
(org-skip-whitespace)
(looking-at "\\[.*\\]")
(org-match-string-no-properties 0)))
(time (and (progn (goto-char (match-end 0))
(looking-at " +=> +\\(\\S-+\\)[ \t]*$"))
(org-match-string-no-properties 1)))
(status (if time 'closed 'running))
(post-blank (let ((before-blank (progn (forward-line) (point))))
(skip-chars-forward " \r\t\n" limit)
(unless (eobp) (beginning-of-line))
(count-lines before-blank (point))))
(end (point)))
(list 'clock
(list :status status
:value value
:time time
:begin begin
:end end
:post-blank post-blank)))))
(defun org-element-clock-interpreter (clock contents)
"Interpret CLOCK element as Org syntax.
CONTENTS is nil."
(concat org-clock-string " "
(org-element-property :value clock)
(let ((time (org-element-property :time clock)))
(and time
(concat " => "
(apply 'format
"%2s:%02s"
(org-split-string time ":")))))))
;;;; Comment
(defun org-element-comment-parser (limit)
"Parse a comment.
LIMIT bounds the search.
Return a list whose CAR is `comment' and CDR is a plist
containing `:begin', `:end', `:value' and `:post-blank'
keywords.
Assume point is at comment beginning."
(save-excursion
(let* ((keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(value (prog2 (looking-at "[ \t]*# ?")
(buffer-substring-no-properties
(match-end 0) (line-end-position))
(forward-line)))
(com-end
;; Get comments ending.
(progn
(while (and (< (point) limit) (looking-at "[ \t]*#\\( \\|$\\)"))
;; Accumulate lines without leading hash and first
;; whitespace.
(setq value
(concat value
"\n"
(buffer-substring-no-properties
(match-end 0) (line-end-position))))
(forward-line))
(point)))
(end (progn (goto-char com-end)
(skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'comment
(nconc
(list :begin begin
:end end
:value value
:post-blank (count-lines com-end end))
(cadr keywords))))))
(defun org-element-comment-interpreter (comment contents)
"Interpret COMMENT element as Org syntax.
CONTENTS is nil."
(replace-regexp-in-string "^" "# " (org-element-property :value comment)))
;;;; Comment Block
(defun org-element-comment-block-parser (limit)
"Parse an export block.
LIMIT bounds the search.
Return a list whose CAR is `comment-block' and CDR is a plist
containing `:begin', `:end', `:hiddenp', `:value' and
`:post-blank' keywords.
Assume point is at comment block beginning."
(let ((case-fold-search t))
(if (not (save-excursion
(re-search-forward "^[ \t]*#\\+END_COMMENT" limit t)))
;; Incomplete block: parse it as a paragraph.
(org-element-paragraph-parser limit)
(let ((contents-end (match-beginning 0)))
(save-excursion
(let* ((keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(contents-begin (progn (forward-line) (point)))
(hidden (org-invisible-p2))
(pos-before-blank (progn (goto-char contents-end)
(forward-line)
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol))))
(value (buffer-substring-no-properties
contents-begin contents-end)))
(list 'comment-block
(nconc
(list :begin begin
:end end
:value value
:hiddenp hidden
:post-blank (count-lines pos-before-blank end))
(cadr keywords)))))))))
(defun org-element-comment-block-interpreter (comment-block contents)
"Interpret COMMENT-BLOCK element as Org syntax.
CONTENTS is nil."
(format "#+BEGIN_COMMENT\n%s#+END_COMMENT"
(org-remove-indentation (org-element-property :value comment-block))))
;;;; Example Block
(defun org-element-example-block-parser (limit)
"Parse an example block.
LIMIT bounds the search.
Return a list whose CAR is `example-block' and CDR is a plist
containing `:begin', `:end', `:number-lines', `:preserve-indent',
`:retain-labels', `:use-labels', `:label-fmt', `:hiddenp',
`:switches', `:value' and `:post-blank' keywords."
(let ((case-fold-search t))
(if (not (save-excursion
(re-search-forward "^[ \t]*#\\+END_EXAMPLE" limit t)))
;; Incomplete block: parse it as a paragraph.
(org-element-paragraph-parser limit)
(let ((contents-end (match-beginning 0)))
(save-excursion
(let* ((switches
(progn (looking-at "^[ \t]*#\\+BEGIN_EXAMPLE\\(?: +\\(.*\\)\\)?")
(org-match-string-no-properties 1)))
;; Switches analysis
(number-lines (cond ((not switches) nil)
((string-match "-n\\>" switches) 'new)
((string-match "+n\\>" switches) 'continued)))
(preserve-indent (and switches (string-match "-i\\>" switches)))
;; Should labels be retained in (or stripped from) example
;; blocks?
(retain-labels
(or (not switches)
(not (string-match "-r\\>" switches))
(and number-lines (string-match "-k\\>" switches))))
;; What should code-references use - labels or
;; line-numbers?
(use-labels
(or (not switches)
(and retain-labels (not (string-match "-k\\>" switches)))))
(label-fmt (and switches
(string-match "-l +\"\\([^\"\n]+\\)\"" switches)
(match-string 1 switches)))
;; Standard block parsing.
(keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(contents-begin (progn (forward-line) (point)))
(hidden (org-invisible-p2))
(value (buffer-substring-no-properties contents-begin contents-end))
(pos-before-blank (progn (goto-char contents-end)
(forward-line)
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'example-block
(nconc
(list :begin begin
:end end
:value value
:switches switches
:number-lines number-lines
:preserve-indent preserve-indent
:retain-labels retain-labels
:use-labels use-labels
:label-fmt label-fmt
:hiddenp hidden
:post-blank (count-lines pos-before-blank end))
(cadr keywords)))))))))
(defun org-element-example-block-interpreter (example-block contents)
"Interpret EXAMPLE-BLOCK element as Org syntax.
CONTENTS is nil."
(let ((switches (org-element-property :switches example-block)))
(concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
(org-remove-indentation
(org-element-property :value example-block))
"#+END_EXAMPLE")))
;;;; Export Block
(defun org-element-export-block-parser (limit)
"Parse an export block.
LIMIT bounds the search.
Return a list whose CAR is `export-block' and CDR is a plist
containing `:begin', `:end', `:type', `:hiddenp', `:value' and
`:post-blank' keywords.
Assume point is at export-block beginning."
(let* ((case-fold-search t)
(type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)")
(upcase (org-match-string-no-properties 1)))))
(if (not (save-excursion
(re-search-forward (concat "^[ \t]*#\\+END_" type) limit t)))
;; Incomplete block: parse it as a paragraph.
(org-element-paragraph-parser limit)
(let ((contents-end (match-beginning 0)))
(save-excursion
(let* ((keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(contents-begin (progn (forward-line) (point)))
(hidden (org-invisible-p2))
(pos-before-blank (progn (goto-char contents-end)
(forward-line)
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol))))
(value (buffer-substring-no-properties contents-begin
contents-end)))
(list 'export-block
(nconc
(list :begin begin
:end end
:type type
:value value
:hiddenp hidden
:post-blank (count-lines pos-before-blank end))
(cadr keywords)))))))))
(defun org-element-export-block-interpreter (export-block contents)
"Interpret EXPORT-BLOCK element as Org syntax.
CONTENTS is nil."
(let ((type (org-element-property :type export-block)))
(concat (format "#+BEGIN_%s\n" type)
(org-element-property :value export-block)
(format "#+END_%s" type))))
;;;; Fixed-width
(defun org-element-fixed-width-parser (limit)
"Parse a fixed-width section.
LIMIT bounds the search.
Return a list whose CAR is `fixed-width' and CDR is a plist
containing `:begin', `:end', `:value' and `:post-blank' keywords.
Assume point is at the beginning of the fixed-width area."
(save-excursion
(let* ((keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
value
(end-area
(progn
(while (and (< (point) limit)
(looking-at "[ \t]*:\\( \\|$\\)"))
;; Accumulate text without starting colons.
(setq value
(concat value
(buffer-substring-no-properties
(match-end 0) (point-at-eol))
"\n"))
(forward-line))
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'fixed-width
(nconc
(list :begin begin
:end end
:value value
:post-blank (count-lines end-area end))
(cadr keywords))))))
(defun org-element-fixed-width-interpreter (fixed-width contents)
"Interpret FIXED-WIDTH element as Org syntax.
CONTENTS is nil."
(replace-regexp-in-string
"^" ": " (substring (org-element-property :value fixed-width) 0 -1)))
;;;; Horizontal Rule
(defun org-element-horizontal-rule-parser (limit)
"Parse an horizontal rule.
LIMIT bounds the search.
Return a list whose CAR is `horizontal-rule' and CDR is a plist
containing `:begin', `:end' and `:post-blank' keywords."
(save-excursion
(let* ((keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(post-hr (progn (forward-line) (point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'horizontal-rule
(nconc
(list :begin begin
:end end
:post-blank (count-lines post-hr end))
(cadr keywords))))))
(defun org-element-horizontal-rule-interpreter (horizontal-rule contents)
"Interpret HORIZONTAL-RULE element as Org syntax.
CONTENTS is nil."
"-----")
;;;; Keyword
(defun org-element-keyword-parser (limit)
"Parse a keyword at point.
LIMIT bounds the search.
Return a list whose CAR is `keyword' and CDR is a plist
containing `:key', `:value', `:begin', `:end' and `:post-blank'
keywords."
(save-excursion
(let* ((case-fold-search t)
(begin (point))
(key (progn (looking-at "[ \t]*#\\+\\(\\S-+\\):")
(upcase (org-match-string-no-properties 1))))
(value (org-trim (buffer-substring-no-properties
(match-end 0) (point-at-eol))))
(pos-before-blank (progn (forward-line) (point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'keyword
(list :key key
:value value
:begin begin
:end end
:post-blank (count-lines pos-before-blank end))))))
(defun org-element-keyword-interpreter (keyword contents)
"Interpret KEYWORD element as Org syntax.
CONTENTS is nil."
(format "#+%s: %s"
(org-element-property :key keyword)
(org-element-property :value keyword)))
;;;; Latex Environment
(defun org-element-latex-environment-parser (limit)
"Parse a LaTeX environment.
LIMIT bounds the search.
Return a list whose CAR is `latex-environment' and CDR is a plist
containing `:begin', `:end', `:value' and `:post-blank'
keywords.
Assume point is at the beginning of the latex environment."
(save-excursion
(let* ((case-fold-search t)
(code-begin (point))
(keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
(regexp-quote (match-string 1))))
(code-end
(progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env) limit t)
(forward-line)
(point)))
(value (buffer-substring-no-properties code-begin code-end))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'latex-environment
(nconc
(list :begin begin
:end end
:value value
:post-blank (count-lines code-end end))
(cadr keywords))))))
(defun org-element-latex-environment-interpreter (latex-environment contents)
"Interpret LATEX-ENVIRONMENT element as Org syntax.
CONTENTS is nil."
(org-element-property :value latex-environment))
;;;; Paragraph
(defun org-element-paragraph-parser (limit)
"Parse a paragraph.
LIMIT bounds the search.
Return a list whose CAR is `paragraph' and CDR is a plist
containing `:begin', `:end', `:contents-begin' and
`:contents-end' and `:post-blank' keywords.
Assume point is at the beginning of the paragraph."
(save-excursion
(let* ((contents-begin (point))
(keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(before-blank
(let ((case-fold-search t))
(end-of-line)
(re-search-forward org-element-paragraph-separate limit 'm)
(while (and (/= (point) limit)
(cond
;; Skip non-existent or incomplete drawer.
((save-excursion
(beginning-of-line)
(and (looking-at "[ \t]*:\\S-")
(or (not (looking-at org-drawer-regexp))
(not (save-excursion
(re-search-forward
"^[ \t]*:END:" limit t)))))))
;; Stop at comments.
((save-excursion
(beginning-of-line)
(not (looking-at "[ \t]*#\\S-"))) nil)
;; Skip incomplete dynamic blocks.
((save-excursion
(beginning-of-line)
(looking-at "[ \t]*#\\+BEGIN: "))
(not (save-excursion
(re-search-forward
"^[ \t]*\\+END:" limit t))))
;; Skip incomplete blocks.
((save-excursion
(beginning-of-line)
(looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)"))
(not (save-excursion
(re-search-forward
(concat "^[ \t]*#\\+END_"
(match-string 1))
limit t))))
;; Skip ill-formed keywords.
((not (save-excursion
(beginning-of-line)
(looking-at "[ \t]*#\\+\\S-+:"))))))
(re-search-forward org-element-paragraph-separate limit 'm))
(if (eobp) (point) (goto-char (line-beginning-position)))))
(contents-end (progn (skip-chars-backward " \r\t\n" contents-begin)
(forward-line)
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'paragraph
;; If paragraph has no affiliated keywords, it may not begin
;; at beginning of line if it starts an item.
(nconc
(list :begin (if (cadr keywords) begin contents-begin)
:end end
:contents-begin contents-begin
:contents-end contents-end
:post-blank (count-lines before-blank end))
(cadr keywords))))))
(defun org-element-paragraph-interpreter (paragraph contents)
"Interpret PARAGRAPH element as Org syntax.
CONTENTS is the contents of the element."
contents)
;;;; Planning
(defun org-element-planning-parser (limit)
"Parse a planning.
LIMIT bounds the search.
Return a list whose CAR is `planning' and CDR is a plist
containing `:closed', `:deadline', `:scheduled', `:begin', `:end'
and `:post-blank' keywords."
(save-excursion
(let* ((case-fold-search nil)
(begin (point))
(post-blank (let ((before-blank (progn (forward-line) (point))))
(skip-chars-forward " \r\t\n" limit)
(unless (eobp) (beginning-of-line))
(count-lines before-blank (point))))
(end (point))
closed deadline scheduled)
(goto-char begin)
(while (re-search-forward org-keyword-time-not-clock-regexp
(line-end-position) t)
(goto-char (match-end 1))
(org-skip-whitespace)
(let ((time (buffer-substring-no-properties
(1+ (point)) (1- (match-end 0))))
(keyword (match-string 1)))
(cond ((equal keyword org-closed-string) (setq closed time))
((equal keyword org-deadline-string) (setq deadline time))
(t (setq scheduled time)))))
(list 'planning
(list :closed closed
:deadline deadline
:scheduled scheduled
:begin begin
:end end
:post-blank post-blank)))))
(defun org-element-planning-interpreter (planning contents)
"Interpret PLANNING element as Org syntax.
CONTENTS is nil."
(mapconcat
'identity
(delq nil
(list (let ((closed (org-element-property :closed planning)))
(when closed (concat org-closed-string " [" closed "]")))
(let ((deadline (org-element-property :deadline planning)))
(when deadline (concat org-deadline-string " <" deadline ">")))
(let ((scheduled (org-element-property :scheduled planning)))
(when scheduled
(concat org-scheduled-string " <" scheduled ">")))))
" "))
;;;; Property Drawer
(defun org-element-property-drawer-parser (limit)
"Parse a property drawer.
LIMIT bounds the search.
Return a list whose CAR is `property-drawer' and CDR is a plist
containing `:begin', `:end', `:hiddenp', `:contents-begin',
`:contents-end', `:properties' and `:post-blank' keywords.
Assume point is at the beginning of the property drawer."
(save-excursion
(let ((case-fold-search t)
(begin (point))
(prop-begin (progn (forward-line) (point)))
(hidden (org-invisible-p2))
(properties
(let (val)
(while (not (looking-at "^[ \t]*:END:"))
(when (looking-at "[ \t]*:\\([A-Za-z][-_A-Za-z0-9]*\\):")
(push (cons (org-match-string-no-properties 1)
(org-trim
(buffer-substring-no-properties
(match-end 0) (point-at-eol))))
val))
(forward-line))
val))
(prop-end (progn (re-search-forward "^[ \t]*:END:" limit t)
(point-at-bol)))
(pos-before-blank (progn (forward-line) (point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'property-drawer
(list :begin begin
:end end
:hiddenp hidden
:properties properties
:post-blank (count-lines pos-before-blank end))))))
(defun org-element-property-drawer-interpreter (property-drawer contents)
"Interpret PROPERTY-DRAWER element as Org syntax.
CONTENTS is nil."
(let ((props (org-element-property :properties property-drawer)))
(concat
":PROPERTIES:\n"
(mapconcat (lambda (p)
(format org-property-format (format ":%s:" (car p)) (cdr p)))
(nreverse props) "\n")
"\n:END:")))
;;;; Quote Section
(defun org-element-quote-section-parser (limit)
"Parse a quote section.
LIMIT bounds the search.
Return a list whose CAR is `quote-section' and CDR is a plist
containing `:begin', `:end', `:value' and `:post-blank' keywords.
Assume point is at beginning of the section."
(save-excursion
(let* ((begin (point))
(end (progn (org-with-limited-levels (outline-next-heading))
(point)))
(pos-before-blank (progn (skip-chars-backward " \r\t\n")
(forward-line)
(point)))
(value (buffer-substring-no-properties begin pos-before-blank)))
(list 'quote-section
(list :begin begin
:end end
:value value
:post-blank (count-lines pos-before-blank end))))))
(defun org-element-quote-section-interpreter (quote-section contents)
"Interpret QUOTE-SECTION element as Org syntax.
CONTENTS is nil."
(org-element-property :value quote-section))
;;;; Src Block
(defun org-element-src-block-parser (limit)
"Parse a src block.
LIMIT bounds the search.
Return a list whose CAR is `src-block' and CDR is a plist
containing `:language', `:switches', `:parameters', `:begin',
`:end', `:hiddenp', `:number-lines', `:retain-labels',
`:use-labels', `:label-fmt', `:preserve-indent', `:value' and
`:post-blank' keywords.
Assume point is at the beginning of the block."
(let ((case-fold-search t))
(if (not (save-excursion (re-search-forward "^[ \t]*#\\+END_SRC" limit t)))
;; Incomplete block: parse it as a paragraph.
(org-element-paragraph-parser limit)
(let ((contents-end (match-beginning 0)))
(save-excursion
(let* ((keywords (org-element--collect-affiliated-keywords))
;; Get beginning position.
(begin (car keywords))
;; Get language as a string.
(language
(progn
(looking-at
(concat "^[ \t]*#\\+BEGIN_SRC"
"\\(?: +\\(\\S-+\\)\\)?"
"\\(\\(?: +\\(?:-l \".*?\"\\|[-+][A-Za-z]\\)\\)+\\)?"
"\\(.*\\)[ \t]*$"))
(org-match-string-no-properties 1)))
;; Get switches.
(switches (org-match-string-no-properties 2))
;; Get parameters.
(parameters (org-match-string-no-properties 3))
;; Switches analysis
(number-lines (cond ((not switches) nil)
((string-match "-n\\>" switches) 'new)
((string-match "+n\\>" switches) 'continued)))
(preserve-indent (and switches (string-match "-i\\>" switches)))
(label-fmt (and switches
(string-match "-l +\"\\([^\"\n]+\\)\"" switches)
(match-string 1 switches)))
;; Should labels be retained in (or stripped from)
;; src blocks?
(retain-labels
(or (not switches)
(not (string-match "-r\\>" switches))
(and number-lines (string-match "-k\\>" switches))))
;; What should code-references use - labels or
;; line-numbers?
(use-labels
(or (not switches)
(and retain-labels (not (string-match "-k\\>" switches)))))
;; Get visibility status.
(hidden (progn (forward-line) (org-invisible-p2)))
;; Retrieve code.
(value (buffer-substring-no-properties (point) contents-end))
(pos-before-blank (progn (goto-char contents-end)
(forward-line)
(point)))
;; Get position after ending blank lines.
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'src-block
(nconc
(list :language language
:switches (and (org-string-nw-p switches)
(org-trim switches))
:parameters (and (org-string-nw-p parameters)
(org-trim parameters))
:begin begin
:end end
:number-lines number-lines
:preserve-indent preserve-indent
:retain-labels retain-labels
:use-labels use-labels
:label-fmt label-fmt
:hiddenp hidden
:value value
:post-blank (count-lines pos-before-blank end))
(cadr keywords)))))))))
(defun org-element-src-block-interpreter (src-block contents)
"Interpret SRC-BLOCK element as Org syntax.
CONTENTS is nil."
(let ((lang (org-element-property :language src-block))
(switches (org-element-property :switches src-block))
(params (org-element-property :parameters src-block))
(value (let ((val (org-element-property :value src-block)))
(cond
(org-src-preserve-indentation val)
((zerop org-edit-src-content-indentation)
(org-remove-indentation val))
(t
(let ((ind (make-string
org-edit-src-content-indentation 32)))
(replace-regexp-in-string
"\\(^\\)[ \t]*\\S-" ind
(org-remove-indentation val) nil nil 1)))))))
(concat (format "#+BEGIN_SRC%s\n"
(concat (and lang (concat " " lang))
(and switches (concat " " switches))
(and params (concat " " params))))
value
"#+END_SRC")))
;;;; Table
(defun org-element-table-parser (limit)
"Parse a table at point.
LIMIT bounds the search.
Return a list whose CAR is `table' and CDR is a plist containing
`:begin', `:end', `:tblfm', `:type', `:contents-begin',
`:contents-end', `:value' and `:post-blank' keywords.
Assume point is at the beginning of the table."
(save-excursion
(let* ((case-fold-search t)
(table-begin (point))
(type (if (org-at-table.el-p) 'table.el 'org))
(keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(table-end (goto-char (marker-position (org-table-end t))))
(tblfm (let (acc)
(while (looking-at "[ \t]*#\\+TBLFM: +\\(.*\\)[ \t]*$")
(push (org-match-string-no-properties 1) acc)
(forward-line))
acc))
(pos-before-blank (point))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'table
(nconc
(list :begin begin
:end end
:type type
:tblfm tblfm
;; Only `org' tables have contents. `table.el' tables
;; use a `:value' property to store raw table as
;; a string.
:contents-begin (and (eq type 'org) table-begin)
:contents-end (and (eq type 'org) table-end)
:value (and (eq type 'table.el)
(buffer-substring-no-properties
table-begin table-end))
:post-blank (count-lines pos-before-blank end))
(cadr keywords))))))
(defun org-element-table-interpreter (table contents)
"Interpret TABLE element as Org syntax.
CONTENTS is nil."
(if (eq (org-element-property :type table) 'table.el)
(org-remove-indentation (org-element-property :value table))
(concat (with-temp-buffer (insert contents)
(org-table-align)
(buffer-string))
(mapconcat (lambda (fm) (concat "#+TBLFM: " fm))
(reverse (org-element-property :tblfm table))
"\n"))))
;;;; Table Row
(defun org-element-table-row-parser (limit)
"Parse table row at point.
LIMIT bounds the search.
Return a list whose CAR is `table-row' and CDR is a plist
containing `:begin', `:end', `:contents-begin', `:contents-end',
`:type' and `:post-blank' keywords."
(save-excursion
(let* ((type (if (looking-at "^[ \t]*|-") 'rule 'standard))
(begin (point))
;; A table rule has no contents. In that case, ensure
;; CONTENTS-BEGIN matches CONTENTS-END.
(contents-begin (and (eq type 'standard)
(search-forward "|")
(point)))
(contents-end (and (eq type 'standard)
(progn
(end-of-line)
(skip-chars-backward " \t")
(point))))
(end (progn (forward-line) (point))))
(list 'table-row
(list :type type
:begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:post-blank 0)))))
(defun org-element-table-row-interpreter (table-row contents)
"Interpret TABLE-ROW element as Org syntax.
CONTENTS is the contents of the table row."
(if (eq (org-element-property :type table-row) 'rule) "|-"
(concat "| " contents)))
;;;; Verse Block
(defun org-element-verse-block-parser (limit)
"Parse a verse block.
LIMIT bounds the search.
Return a list whose CAR is `verse-block' and CDR is a plist
containing `:begin', `:end', `:contents-begin', `:contents-end',
`:hiddenp' and `:post-blank' keywords.
Assume point is at beginning of the block."
(let ((case-fold-search t))
(if (not (save-excursion
(re-search-forward "^[ \t]*#\\+END_VERSE" limit t)))
;; Incomplete block: parse it as a paragraph.
(org-element-paragraph-parser limit)
(let ((contents-end (match-beginning 0)))
(save-excursion
(let* ((keywords (org-element--collect-affiliated-keywords))
(begin (car keywords))
(hidden (progn (forward-line) (org-invisible-p2)))
(contents-begin (point))
(pos-before-blank (progn (goto-char contents-end)
(forward-line)
(point)))
(end (progn (skip-chars-forward " \r\t\n" limit)
(if (eobp) (point) (point-at-bol)))))
(list 'verse-block
(nconc
(list :begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:hiddenp hidden
:post-blank (count-lines pos-before-blank end))
(cadr keywords)))))))))
(defun org-element-verse-block-interpreter (verse-block contents)
"Interpret VERSE-BLOCK element as Org syntax.
CONTENTS is verse block contents."
(format "#+BEGIN_VERSE\n%s#+END_VERSE" contents))
\f
;;; Objects
;;
;; Unlike to elements, interstices can be found between objects.
;; That's why, along with the parser, successor functions are provided
;; for each object. Some objects share the same successor (i.e. `code'
;; and `verbatim' objects).
;;
;; A successor must accept a single argument bounding the search. It
;; will return either a cons cell whose CAR is the object's type, as
;; a symbol, and CDR the position of its next occurrence, or nil.
;;
;; Successors follow the naming convention:
;; org-element-NAME-successor, where NAME is the name of the
;; successor, as defined in `org-element-all-successors'.
;;
;; Some object types (i.e. `italic') are recursive. Restrictions on
;; object types they can contain will be specified in
;; `org-element-object-restrictions'.
;;
;; Adding a new type of object is simple. Implement a successor,
;; a parser, and an interpreter for it, all following the naming
;; convention. Register type in `org-element-all-objects' and
;; successor in `org-element-all-successors'. Maybe tweak
;; restrictions about it, and that's it.
;;;; Bold
(defun org-element-bold-parser ()
"Parse bold object at point.
Return a list whose CAR is `bold' and CDR is a plist with
`:begin', `:end', `:contents-begin' and `:contents-end' and
`:post-blank' keywords.
Assume point is at the first star marker."
(save-excursion
(unless (bolp) (backward-char 1))
(looking-at org-emph-re)
(let ((begin (match-beginning 2))
(contents-begin (match-beginning 4))
(contents-end (match-end 4))
(post-blank (progn (goto-char (match-end 2))
(skip-chars-forward " \t")))
(end (point)))
(list 'bold
(list :begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:post-blank post-blank)))))
(defun org-element-bold-interpreter (bold contents)
"Interpret BOLD object as Org syntax.
CONTENTS is the contents of the object."
(format "*%s*" contents))
(defun org-element-text-markup-successor (limit)
"Search for the next text-markup object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is a symbol among `bold',
`italic', `underline', `strike-through', `code' and `verbatim'
and CDR is beginning position."
(save-excursion
(unless (bolp) (backward-char))
(when (re-search-forward org-emph-re limit t)
(let ((marker (match-string 3)))
(cons (cond
((equal marker "*") 'bold)
((equal marker "/") 'italic)
((equal marker "_") 'underline)
((equal marker "+") 'strike-through)
((equal marker "~") 'code)
((equal marker "=") 'verbatim)
(t (error "Unknown marker at %d" (match-beginning 3))))
(match-beginning 2))))))
;;;; Code
(defun org-element-code-parser ()
"Parse code object at point.
Return a list whose CAR is `code' and CDR is a plist with
`:value', `:begin', `:end' and `:post-blank' keywords.
Assume point is at the first tilde marker."
(save-excursion
(unless (bolp) (backward-char 1))
(looking-at org-emph-re)
(let ((begin (match-beginning 2))
(value (org-match-string-no-properties 4))
(post-blank (progn (goto-char (match-end 2))
(skip-chars-forward " \t")))
(end (point)))
(list 'code
(list :value value
:begin begin
:end end
:post-blank post-blank)))))
(defun org-element-code-interpreter (code contents)
"Interpret CODE object as Org syntax.
CONTENTS is nil."
(format "~%s~" (org-element-property :value code)))
;;;; Entity
(defun org-element-entity-parser ()
"Parse entity at point.
Return a list whose CAR is `entity' and CDR a plist with
`:begin', `:end', `:latex', `:latex-math-p', `:html', `:latin1',
`:utf-8', `:ascii', `:use-brackets-p' and `:post-blank' as
keywords.
Assume point is at the beginning of the entity."
(save-excursion
(looking-at "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)")
(let* ((value (org-entity-get (match-string 1)))
(begin (match-beginning 0))
(bracketsp (string= (match-string 2) "{}"))
(post-blank (progn (goto-char (match-end 1))
(when bracketsp (forward-char 2))
(skip-chars-forward " \t")))
(end (point)))
(list 'entity
(list :name (car value)
:latex (nth 1 value)
:latex-math-p (nth 2 value)
:html (nth 3 value)
:ascii (nth 4 value)
:latin1 (nth 5 value)
:utf-8 (nth 6 value)
:begin begin
:end end
:use-brackets-p bracketsp
:post-blank post-blank)))))
(defun org-element-entity-interpreter (entity contents)
"Interpret ENTITY object as Org syntax.
CONTENTS is nil."
(concat "\\"
(org-element-property :name entity)
(when (org-element-property :use-brackets-p entity) "{}")))
(defun org-element-latex-or-entity-successor (limit)
"Search for the next latex-fragment or entity object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `entity' or
`latex-fragment' and CDR is beginning position."
(save-excursion
(let ((matchers (plist-get org-format-latex-options :matchers))
;; ENTITY-RE matches both LaTeX commands and Org entities.
(entity-re
"\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))
(when (re-search-forward
(concat (mapconcat (lambda (e) (nth 1 (assoc e org-latex-regexps)))
matchers "\\|")
"\\|" entity-re)
limit t)
(goto-char (match-beginning 0))
(if (looking-at entity-re)
;; Determine if it's a real entity or a LaTeX command.
(cons (if (org-entity-get (match-string 1)) 'entity 'latex-fragment)
(match-beginning 0))
;; No entity nor command: point is at a LaTeX fragment.
;; Determine its type to get the correct beginning position.
(cons 'latex-fragment
(catch 'return
(mapc (lambda (e)
(when (looking-at (nth 1 (assoc e org-latex-regexps)))
(throw 'return
(match-beginning
(nth 2 (assoc e org-latex-regexps))))))
matchers)
(point))))))))
;;;; Export Snippet
(defun org-element-export-snippet-parser ()
"Parse export snippet at point.
Return a list whose CAR is `export-snippet' and CDR a plist with
`:begin', `:end', `:back-end', `:value' and `:post-blank' as
keywords.
Assume point is at the beginning of the snippet."
(save-excursion
(re-search-forward "@@\\([-A-Za-z0-9]+\\):" nil t)
(let* ((begin (match-beginning 0))
(back-end (org-match-string-no-properties 1))
(value (buffer-substring-no-properties
(point)
(progn (re-search-forward "@@" nil t) (match-beginning 0))))
(post-blank (skip-chars-forward " \t"))
(end (point)))
(list 'export-snippet
(list :back-end back-end
:value value
:begin begin
:end end
:post-blank post-blank)))))
(defun org-element-export-snippet-interpreter (export-snippet contents)
"Interpret EXPORT-SNIPPET object as Org syntax.
CONTENTS is nil."
(format "@@%s:%s@@"
(org-element-property :back-end export-snippet)
(org-element-property :value export-snippet)))
(defun org-element-export-snippet-successor (limit)
"Search for the next export-snippet object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `export-snippet' and CDR
its beginning position."
(save-excursion
(let (beg)
(when (and (re-search-forward "@@[-A-Za-z0-9]+:" limit t)
(setq beg (match-beginning 0))
(re-search-forward "@@" limit t))
(cons 'export-snippet beg)))))
;;;; Footnote Reference
(defun org-element-footnote-reference-parser ()
"Parse footnote reference at point.
Return a list whose CAR is `footnote-reference' and CDR a plist
with `:label', `:type', `:inline-definition', `:begin', `:end'
and `:post-blank' as keywords."
(save-excursion
(looking-at org-footnote-re)
(let* ((begin (point))
(label (or (org-match-string-no-properties 2)
(org-match-string-no-properties 3)
(and (match-string 1)
(concat "fn:" (org-match-string-no-properties 1)))))
(type (if (or (not label) (match-string 1)) 'inline 'standard))
(inner-begin (match-end 0))
(inner-end
(let ((count 1))
(forward-char)
(while (and (> count 0) (re-search-forward "[][]" nil t))
(if (equal (match-string 0) "[") (incf count) (decf count)))
(1- (point))))
(post-blank (progn (goto-char (1+ inner-end))
(skip-chars-forward " \t")))
(end (point))
(footnote-reference
(list 'footnote-reference
(list :label label
:type type
:begin begin
:end end
:post-blank post-blank))))
(org-element-put-property
footnote-reference :inline-definition
(and (eq type 'inline)
(org-element-parse-secondary-string
(buffer-substring inner-begin inner-end)
(org-element-restriction 'footnote-reference)
footnote-reference))))))
(defun org-element-footnote-reference-interpreter (footnote-reference contents)
"Interpret FOOTNOTE-REFERENCE object as Org syntax.
CONTENTS is nil."
(let ((label (or (org-element-property :label footnote-reference) "fn:"))
(def
(let ((inline-def
(org-element-property :inline-definition footnote-reference)))
(if (not inline-def) ""
(concat ":" (org-element-interpret-data inline-def))))))
(format "[%s]" (concat label def))))
(defun org-element-footnote-reference-successor (limit)
"Search for the next footnote-reference object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `footnote-reference' and
CDR is beginning position."
(save-excursion
(catch 'exit
(while (re-search-forward org-footnote-re limit t)
(save-excursion
(let ((beg (match-beginning 0))
(count 1))
(backward-char)
(while (re-search-forward "[][]" limit t)
(if (equal (match-string 0) "[") (incf count) (decf count))
(when (zerop count)
(throw 'exit (cons 'footnote-reference beg))))))))))
;;;; Inline Babel Call
(defun org-element-inline-babel-call-parser ()
"Parse inline babel call at point.
Return a list whose CAR is `inline-babel-call' and CDR a plist
with `:begin', `:end', `:info' and `:post-blank' as keywords.
Assume point is at the beginning of the babel call."
(save-excursion
(unless (bolp) (backward-char))
(looking-at org-babel-inline-lob-one-liner-regexp)
(let ((info (save-match-data (org-babel-lob-get-info)))
(begin (match-end 1))
(post-blank (progn (goto-char (match-end 0))
(skip-chars-forward " \t")))
(end (point)))
(list 'inline-babel-call
(list :begin begin
:end end
:info info
:post-blank post-blank)))))
(defun org-element-inline-babel-call-interpreter (inline-babel-call contents)
"Interpret INLINE-BABEL-CALL object as Org syntax.
CONTENTS is nil."
(let* ((babel-info (org-element-property :info inline-babel-call))
(main-source (car babel-info))
(post-options (nth 1 babel-info)))
(concat "call_"
(if (string-match "\\[\\(\\[.*?\\]\\)\\]" main-source)
;; Remove redundant square brackets.
(replace-match
(match-string 1 main-source) nil nil main-source)
main-source)
(and post-options (format "[%s]" post-options)))))
(defun org-element-inline-babel-call-successor (limit)
"Search for the next inline-babel-call object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `inline-babel-call' and
CDR is beginning position."
(save-excursion
;; Use a simplified version of
;; org-babel-inline-lob-one-liner-regexp as regexp for more speed.
(when (re-search-forward
"\\(?:babel\\|call\\)_\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\([^\n]*\\))\\(\\[\\(.*?\\)\\]\\)?"
limit t)
(cons 'inline-babel-call (match-beginning 0)))))
;;;; Inline Src Block
(defun org-element-inline-src-block-parser ()
"Parse inline source block at point.
LIMIT bounds the search.
Return a list whose CAR is `inline-src-block' and CDR a plist
with `:begin', `:end', `:language', `:value', `:parameters' and
`:post-blank' as keywords.
Assume point is at the beginning of the inline src block."
(save-excursion
(unless (bolp) (backward-char))
(looking-at org-babel-inline-src-block-regexp)
(let ((begin (match-beginning 1))
(language (org-match-string-no-properties 2))
(parameters (org-match-string-no-properties 4))
(value (org-match-string-no-properties 5))
(post-blank (progn (goto-char (match-end 0))
(skip-chars-forward " \t")))
(end (point)))
(list 'inline-src-block
(list :language language
:value value
:parameters parameters
:begin begin
:end end
:post-blank post-blank)))))
(defun org-element-inline-src-block-interpreter (inline-src-block contents)
"Interpret INLINE-SRC-BLOCK object as Org syntax.
CONTENTS is nil."
(let ((language (org-element-property :language inline-src-block))
(arguments (org-element-property :parameters inline-src-block))
(body (org-element-property :value inline-src-block)))
(format "src_%s%s{%s}"
language
(if arguments (format "[%s]" arguments) "")
body)))
(defun org-element-inline-src-block-successor (limit)
"Search for the next inline-babel-call element.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `inline-babel-call' and
CDR is beginning position."
(save-excursion
(when (re-search-forward org-babel-inline-src-block-regexp limit t)
(cons 'inline-src-block (match-beginning 1)))))
;;;; Italic
(defun org-element-italic-parser ()
"Parse italic object at point.
Return a list whose CAR is `italic' and CDR is a plist with
`:begin', `:end', `:contents-begin' and `:contents-end' and
`:post-blank' keywords.
Assume point is at the first slash marker."
(save-excursion
(unless (bolp) (backward-char 1))
(looking-at org-emph-re)
(let ((begin (match-beginning 2))
(contents-begin (match-beginning 4))
(contents-end (match-end 4))
(post-blank (progn (goto-char (match-end 2))
(skip-chars-forward " \t")))
(end (point)))
(list 'italic
(list :begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:post-blank post-blank)))))
(defun org-element-italic-interpreter (italic contents)
"Interpret ITALIC object as Org syntax.
CONTENTS is the contents of the object."
(format "/%s/" contents))
;;;; Latex Fragment
(defun org-element-latex-fragment-parser ()
"Parse latex fragment at point.
Return a list whose CAR is `latex-fragment' and CDR a plist with
`:value', `:begin', `:end', and `:post-blank' as keywords.
Assume point is at the beginning of the latex fragment."
(save-excursion
(let* ((begin (point))
(substring-match
(catch 'exit
(mapc (lambda (e)
(let ((latex-regexp (nth 1 (assoc e org-latex-regexps))))
(when (or (looking-at latex-regexp)
(and (not (bobp))
(save-excursion
(backward-char)
(looking-at latex-regexp))))
(throw 'exit (nth 2 (assoc e org-latex-regexps))))))
(plist-get org-format-latex-options :matchers))
;; None found: it's a macro.
(looking-at "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")
0))
(value (match-string-no-properties substring-match))
(post-blank (progn (goto-char (match-end substring-match))
(skip-chars-forward " \t")))
(end (point)))
(list 'latex-fragment
(list :value value
:begin begin
:end end
:post-blank post-blank)))))
(defun org-element-latex-fragment-interpreter (latex-fragment contents)
"Interpret LATEX-FRAGMENT object as Org syntax.
CONTENTS is nil."
(org-element-property :value latex-fragment))
;;;; Line Break
(defun org-element-line-break-parser ()
"Parse line break at point.
Return a list whose CAR is `line-break', and CDR a plist with
`:begin', `:end' and `:post-blank' keywords.
Assume point is at the beginning of the line break."
(list 'line-break (list :begin (point) :end (point-at-eol) :post-blank 0)))
(defun org-element-line-break-interpreter (line-break contents)
"Interpret LINE-BREAK object as Org syntax.
CONTENTS is nil."
"\\\\")
(defun org-element-line-break-successor (limit)
"Search for the next line-break object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `line-break' and CDR is
beginning position."
(save-excursion
(let ((beg (and (re-search-forward "[^\\\\]\\(\\\\\\\\\\)[ \t]*$" limit t)
(goto-char (match-beginning 1)))))
;; A line break can only happen on a non-empty line.
(when (and beg (re-search-backward "\\S-" (point-at-bol) t))
(cons 'line-break beg)))))
;;;; Link
(defun org-element-link-parser ()
"Parse link at point.
Return a list whose CAR is `link' and CDR a plist with `:type',
`:path', `:raw-link', `:begin', `:end', `:contents-begin',
`:contents-end' and `:post-blank' as keywords.
Assume point is at the beginning of the link."
(save-excursion
(let ((begin (point))
end contents-begin contents-end link-end post-blank path type
raw-link link)
(cond
;; Type 1: Text targeted from a radio target.
((and org-target-link-regexp (looking-at org-target-link-regexp))
(setq type "radio"
link-end (match-end 0)
path (org-match-string-no-properties 0)))
;; Type 2: Standard link, i.e. [[http://orgmode.org][homepage]]
((looking-at org-bracket-link-regexp)
(setq contents-begin (match-beginning 3)
contents-end (match-end 3)
link-end (match-end 0)
;; RAW-LINK is the original link.
raw-link (org-match-string-no-properties 1)
link (org-translate-link
(org-link-expand-abbrev
(org-link-unescape raw-link))))
;; Determine TYPE of link and set PATH accordingly.
(cond
;; File type.
((or (file-name-absolute-p link) (string-match "^\\.\\.?/" link))
(setq type "file" path link))
;; Explicit type (http, irc, bbdb...). See `org-link-types'.
((string-match org-link-re-with-space3 link)
(setq type (match-string 1 link) path (match-string 2 link)))
;; Id type: PATH is the id.
((string-match "^id:\\([-a-f0-9]+\\)" link)
(setq type "id" path (match-string 1 link)))
;; Code-ref type: PATH is the name of the reference.
((string-match "^(\\(.*\\))$" link)
(setq type "coderef" path (match-string 1 link)))
;; Custom-id type: PATH is the name of the custom id.
((= (aref link 0) ?#)
(setq type "custom-id" path (substring link 1)))
;; Fuzzy type: Internal link either matches a target, an
;; headline name or nothing. PATH is the target or
;; headline's name.
(t (setq type "fuzzy" path link))))
;; Type 3: Plain link, i.e. http://orgmode.org
((looking-at org-plain-link-re)
(setq raw-link (org-match-string-no-properties 0)
type (org-match-string-no-properties 1)
path (org-match-string-no-properties 2)
link-end (match-end 0)))
;; Type 4: Angular link, i.e. <http://orgmode.org>
((looking-at org-angle-link-re)
(setq raw-link (buffer-substring-no-properties
(match-beginning 1) (match-end 2))
type (org-match-string-no-properties 1)
path (org-match-string-no-properties 2)
link-end (match-end 0))))
;; In any case, deduce end point after trailing white space from
;; LINK-END variable.
(setq post-blank (progn (goto-char link-end) (skip-chars-forward " \t"))
end (point))
(list 'link
(list :type type
:path path
:raw-link (or raw-link path)
:begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:post-blank post-blank)))))
(defun org-element-link-interpreter (link contents)
"Interpret LINK object as Org syntax.
CONTENTS is the contents of the object, or nil."
(let ((type (org-element-property :type link))
(raw-link (org-element-property :raw-link link)))
(if (string= type "radio") raw-link
(format "[[%s]%s]"
raw-link
(if contents (format "[%s]" contents) "")))))
(defun org-element-link-successor (limit)
"Search for the next link object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `link' and CDR is
beginning position."
(save-excursion
(let ((link-regexp
(if (not org-target-link-regexp) org-any-link-re
(concat org-any-link-re "\\|" org-target-link-regexp))))
(when (re-search-forward link-regexp limit t)
(cons 'link (match-beginning 0))))))
;;;; Macro
(defun org-element-macro-parser ()
"Parse macro at point.
Return a list whose CAR is `macro' and CDR a plist with `:key',
`:args', `:begin', `:end', `:value' and `:post-blank' as
keywords.
Assume point is at the macro."
(save-excursion
(looking-at "{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}")
(let ((begin (point))
(key (downcase (org-match-string-no-properties 1)))
(value (org-match-string-no-properties 0))
(post-blank (progn (goto-char (match-end 0))
(skip-chars-forward " \t")))
(end (point))
(args (let ((args (org-match-string-no-properties 3)) args2)
(when args
(setq args (org-split-string args ","))
(while args
(while (string-match "\\\\\\'" (car args))
;; Repair bad splits.
(setcar (cdr args) (concat (substring (car args) 0 -1)
"," (nth 1 args)))
(pop args))
(push (pop args) args2))
(mapcar 'org-trim (nreverse args2))))))
(list 'macro
(list :key key
:value value
:args args
:begin begin
:end end
:post-blank post-blank)))))
(defun org-element-macro-interpreter (macro contents)
"Interpret MACRO object as Org syntax.
CONTENTS is nil."
(org-element-property :value macro))
(defun org-element-macro-successor (limit)
"Search for the next macro object.
LIMIT bounds the search.
Return value is cons cell whose CAR is `macro' and CDR is
beginning position."
(save-excursion
(when (re-search-forward
"{{{\\([a-zA-Z][-a-zA-Z0-9_]*\\)\\(([ \t\n]*\\([^\000]*?\\))\\)?}}}"
limit t)
(cons 'macro (match-beginning 0)))))
;;;; Radio-target
(defun org-element-radio-target-parser ()
"Parse radio target at point.
Return a list whose CAR is `radio-target' and CDR a plist with
`:begin', `:end', `:contents-begin', `:contents-end', `:value'
and `:post-blank' as keywords.
Assume point is at the radio target."
(save-excursion
(looking-at org-radio-target-regexp)
(let ((begin (point))
(contents-begin (match-beginning 1))
(contents-end (match-end 1))
(value (org-match-string-no-properties 1))
(post-blank (progn (goto-char (match-end 0))
(skip-chars-forward " \t")))
(end (point)))
(list 'radio-target
(list :begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:post-blank post-blank
:value value)))))
(defun org-element-radio-target-interpreter (target contents)
"Interpret TARGET object as Org syntax.
CONTENTS is the contents of the object."
(concat "<<<" contents ">>>"))
(defun org-element-radio-target-successor (limit)
"Search for the next radio-target object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `radio-target' and CDR
is beginning position."
(save-excursion
(when (re-search-forward org-radio-target-regexp limit t)
(cons 'radio-target (match-beginning 0)))))
;;;; Statistics Cookie
(defun org-element-statistics-cookie-parser ()
"Parse statistics cookie at point.
Return a list whose CAR is `statistics-cookie', and CDR a plist
with `:begin', `:end', `:value' and `:post-blank' keywords.
Assume point is at the beginning of the statistics-cookie."
(save-excursion
(looking-at "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]")
(let* ((begin (point))
(value (buffer-substring-no-properties
(match-beginning 0) (match-end 0)))
(post-blank (progn (goto-char (match-end 0))
(skip-chars-forward " \t")))
(end (point)))
(list 'statistics-cookie
(list :begin begin
:end end
:value value
:post-blank post-blank)))))
(defun org-element-statistics-cookie-interpreter (statistics-cookie contents)
"Interpret STATISTICS-COOKIE object as Org syntax.
CONTENTS is nil."
(org-element-property :value statistics-cookie))
(defun org-element-statistics-cookie-successor (limit)
"Search for the next statistics cookie object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `statistics-cookie' and
CDR is beginning position."
(save-excursion
(when (re-search-forward "\\[[0-9]*\\(%\\|/[0-9]*\\)\\]" limit t)
(cons 'statistics-cookie (match-beginning 0)))))
;;;; Strike-Through
(defun org-element-strike-through-parser ()
"Parse strike-through object at point.
Return a list whose CAR is `strike-through' and CDR is a plist
with `:begin', `:end', `:contents-begin' and `:contents-end' and
`:post-blank' keywords.
Assume point is at the first plus sign marker."
(save-excursion
(unless (bolp) (backward-char 1))
(looking-at org-emph-re)
(let ((begin (match-beginning 2))
(contents-begin (match-beginning 4))
(contents-end (match-end 4))
(post-blank (progn (goto-char (match-end 2))
(skip-chars-forward " \t")))
(end (point)))
(list 'strike-through
(list :begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:post-blank post-blank)))))
(defun org-element-strike-through-interpreter (strike-through contents)
"Interpret STRIKE-THROUGH object as Org syntax.
CONTENTS is the contents of the object."
(format "+%s+" contents))
;;;; Subscript
(defun org-element-subscript-parser ()
"Parse subscript at point.
Return a list whose CAR is `subscript' and CDR a plist with
`:begin', `:end', `:contents-begin', `:contents-end',
`:use-brackets-p' and `:post-blank' as keywords.
Assume point is at the underscore."
(save-excursion
(unless (bolp) (backward-char))
(let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp)
t
(not (looking-at org-match-substring-regexp))))
(begin (match-beginning 2))
(contents-begin (or (match-beginning 5)
(match-beginning 3)))
(contents-end (or (match-end 5) (match-end 3)))
(post-blank (progn (goto-char (match-end 0))
(skip-chars-forward " \t")))
(end (point)))
(list 'subscript
(list :begin begin
:end end
:use-brackets-p bracketsp
:contents-begin contents-begin
:contents-end contents-end
:post-blank post-blank)))))
(defun org-element-subscript-interpreter (subscript contents)
"Interpret SUBSCRIPT object as Org syntax.
CONTENTS is the contents of the object."
(format
(if (org-element-property :use-brackets-p subscript) "_{%s}" "_%s")
contents))
(defun org-element-sub/superscript-successor (limit)
"Search for the next sub/superscript object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is either `subscript' or
`superscript' and CDR is beginning position."
(save-excursion
(when (re-search-forward org-match-substring-regexp limit t)
(cons (if (string= (match-string 2) "_") 'subscript 'superscript)
(match-beginning 2)))))
;;;; Superscript
(defun org-element-superscript-parser ()
"Parse superscript at point.
Return a list whose CAR is `superscript' and CDR a plist with
`:begin', `:end', `:contents-begin', `:contents-end',
`:use-brackets-p' and `:post-blank' as keywords.
Assume point is at the caret."
(save-excursion
(unless (bolp) (backward-char))
(let ((bracketsp (if (looking-at org-match-substring-with-braces-regexp) t
(not (looking-at org-match-substring-regexp))))
(begin (match-beginning 2))
(contents-begin (or (match-beginning 5)
(match-beginning 3)))
(contents-end (or (match-end 5) (match-end 3)))
(post-blank (progn (goto-char (match-end 0))
(skip-chars-forward " \t")))
(end (point)))
(list 'superscript
(list :begin begin
:end end
:use-brackets-p bracketsp
:contents-begin contents-begin
:contents-end contents-end
:post-blank post-blank)))))
(defun org-element-superscript-interpreter (superscript contents)
"Interpret SUPERSCRIPT object as Org syntax.
CONTENTS is the contents of the object."
(format
(if (org-element-property :use-brackets-p superscript) "^{%s}" "^%s")
contents))
;;;; Table Cell
(defun org-element-table-cell-parser ()
"Parse table cell at point.
Return a list whose CAR is `table-cell' and CDR is a plist
containing `:begin', `:end', `:contents-begin', `:contents-end'
and `:post-blank' keywords."
(looking-at "[ \t]*\\(.*?\\)[ \t]*|")
(let* ((begin (match-beginning 0))
(end (match-end 0))
(contents-begin (match-beginning 1))
(contents-end (match-end 1)))
(list 'table-cell
(list :begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:post-blank 0))))
(defun org-element-table-cell-interpreter (table-cell contents)
"Interpret TABLE-CELL element as Org syntax.
CONTENTS is the contents of the cell, or nil."
(concat " " contents " |"))
(defun org-element-table-cell-successor (limit)
"Search for the next table-cell object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `table-cell' and CDR is
beginning position."
(when (looking-at "[ \t]*.*?[ \t]+|") (cons 'table-cell (point))))
;;;; Target
(defun org-element-target-parser ()
"Parse target at point.
Return a list whose CAR is `target' and CDR a plist with
`:begin', `:end', `:value' and `:post-blank' as keywords.
Assume point is at the target."
(save-excursion
(looking-at org-target-regexp)
(let ((begin (point))
(value (org-match-string-no-properties 1))
(post-blank (progn (goto-char (match-end 0))
(skip-chars-forward " \t")))
(end (point)))
(list 'target
(list :begin begin
:end end
:value value
:post-blank post-blank)))))
(defun org-element-target-interpreter (target contents)
"Interpret TARGET object as Org syntax.
CONTENTS is nil."
(format "<<%s>>" (org-element-property :value target)))
(defun org-element-target-successor (limit)
"Search for the next target object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `target' and CDR is
beginning position."
(save-excursion
(when (re-search-forward org-target-regexp limit t)
(cons 'target (match-beginning 0)))))
;;;; Timestamp
(defun org-element-timestamp-parser ()
"Parse time stamp at point.
Return a list whose CAR is `timestamp', and CDR a plist with
`:type', `:begin', `:end', `:value' and `:post-blank' keywords.
Assume point is at the beginning of the timestamp."
(save-excursion
(let* ((begin (point))
(activep (eq (char-after) ?<))
(main-value
(progn
(looking-at "[<[]\\(\\(%%\\)?.*?\\)[]>]\\(?:--[<[]\\(.*?\\)[]>]\\)?")
(match-string-no-properties 1)))
(range-end (match-string-no-properties 3))
(type (cond ((match-string 2) 'diary)
((and activep range-end) 'active-range)
(activep 'active)
(range-end 'inactive-range)
(t 'inactive)))
(post-blank (progn (goto-char (match-end 0))
(skip-chars-forward " \t")))
(end (point)))
(list 'timestamp
(list :type type
:value main-value
:range-end range-end
:begin begin
:end end
:post-blank post-blank)))))
(defun org-element-timestamp-interpreter (timestamp contents)
"Interpret TIMESTAMP object as Org syntax.
CONTENTS is nil."
(let ((type (org-element-property :type timestamp) ))
(concat
(format (if (memq type '(inactive inactive-range)) "[%s]" "<%s>")
(org-element-property :value timestamp))
(let ((range-end (org-element-property :range-end timestamp)))
(when range-end
(concat "--"
(format (if (eq type 'inactive-range) "[%s]" "<%s>")
range-end)))))))
(defun org-element-timestamp-successor (limit)
"Search for the next timestamp object.
LIMIT bounds the search.
Return value is a cons cell whose CAR is `timestamp' and CDR is
beginning position."
(save-excursion
(when (re-search-forward
(concat org-ts-regexp-both
"\\|"
"\\(?:<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
"\\|"
"\\(?:<%%\\(?:([^>\n]+)\\)>\\)")
limit t)
(cons 'timestamp (match-beginning 0)))))
;;;; Underline
(defun org-element-underline-parser ()
"Parse underline object at point.
Return a list whose CAR is `underline' and CDR is a plist with
`:begin', `:end', `:contents-begin' and `:contents-end' and
`:post-blank' keywords.
Assume point is at the first underscore marker."
(save-excursion
(unless (bolp) (backward-char 1))
(looking-at org-emph-re)
(let ((begin (match-beginning 2))
(contents-begin (match-beginning 4))
(contents-end (match-end 4))
(post-blank (progn (goto-char (match-end 2))
(skip-chars-forward " \t")))
(end (point)))
(list 'underline
(list :begin begin
:end end
:contents-begin contents-begin
:contents-end contents-end
:post-blank post-blank)))))
(defun org-element-underline-interpreter (underline contents)
"Interpret UNDERLINE object as Org syntax.
CONTENTS is the contents of the object."
(format "_%s_" contents))
;;;; Verbatim
(defun org-element-verbatim-parser ()
"Parse verbatim object at point.
Return a list whose CAR is `verbatim' and CDR is a plist with
`:value', `:begin', `:end' and `:post-blank' keywords.
Assume point is at the first equal sign marker."
(save-excursion
(unless (bolp) (backward-char 1))
(looking-at org-emph-re)
(let ((begin (match-beginning 2))
(value (org-match-string-no-properties 4))
(post-blank (progn (goto-char (match-end 2))
(skip-chars-forward " \t")))
(end (point)))
(list 'verbatim
(list :value value
:begin begin
:end end
:post-blank post-blank)))))
(defun org-element-verbatim-interpreter (verbatim contents)
"Interpret VERBATIM object as Org syntax.
CONTENTS is nil."
(format "=%s=" (org-element-property :value verbatim)))
\f
;;; Parsing Element Starting At Point
;;
;; `org-element--current-element' is the core function of this section.
;; It returns the Lisp representation of the element starting at
;; point.
;;
;; `org-element--current-element' makes use of special modes. They
;; are activated for fixed element chaining (i.e. `plain-list' >
;; `item') or fixed conditional element chaining (i.e. `headline' >
;; `section'). Special modes are: `first-section', `section',
;; `quote-section', `item' and `table-row'.
(defun org-element--current-element
(limit &optional granularity special structure)
"Parse the element starting at point.
LIMIT bounds the search.
Return value is a list like (TYPE PROPS) where TYPE is the type
of the element and PROPS a plist of properties associated to the
element.
Possible types are defined in `org-element-all-elements'.
Optional argument GRANULARITY determines the depth of the
recursion. Allowed values are `headline', `greater-element',
`element', `object' or nil. When it is broader than `object' (or
nil), secondary values will not be parsed, since they only
contain objects.
Optional argument SPECIAL, when non-nil, can be either
`first-section', `section', `quote-section', `table-row' and
`item'.
If STRUCTURE isn't provided but SPECIAL is set to `item', it will
be computed.
This function assumes point is always at the beginning of the
element it has to parse."
(save-excursion
;; If point is at an affiliated keyword, try moving to the
;; beginning of the associated element. If none is found, the
;; keyword is orphaned and will be treated as plain text.
(when (looking-at org-element--affiliated-re)
(let ((opoint (point)))
(while (looking-at org-element--affiliated-re) (forward-line))
(when (looking-at "[ \t]*$") (goto-char opoint))))
(let ((case-fold-search t)
;; Determine if parsing depth allows for secondary strings
;; parsing. It only applies to elements referenced in
;; `org-element-secondary-value-alist'.
(raw-secondary-p (and granularity (not (eq granularity 'object)))))
(cond
;; Item.
((eq special 'item)
(org-element-item-parser limit structure raw-secondary-p))
;; Quote Section.
((eq special 'quote-section) (org-element-quote-section-parser limit))
;; Table Row.
((eq special 'table-row) (org-element-table-row-parser limit))
;; Headline.
((org-with-limited-levels (org-at-heading-p))
(org-element-headline-parser limit raw-secondary-p))
;; Section (must be checked after headline).
((eq special 'section) (org-element-section-parser limit))
((eq special 'first-section)
(org-element-section-parser
(or (save-excursion (org-with-limited-levels (outline-next-heading)))
limit)))
;; Planning and Clock.
((and (looking-at org-planning-or-clock-line-re))
(if (equal (match-string 1) org-clock-string)
(org-element-clock-parser limit)
(org-element-planning-parser limit)))
;; Inlinetask.
((org-at-heading-p)
(org-element-inlinetask-parser limit raw-secondary-p))
;; LaTeX Environment.
((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
(if (save-excursion
(re-search-forward
(format "[ \t]*\\\\end{%s}[ \t]*"
(regexp-quote (match-string 1)))
nil t))
(org-element-latex-environment-parser limit)
(org-element-paragraph-parser limit)))
;; Drawer and Property Drawer.
((looking-at org-drawer-regexp)
(let ((name (match-string 1)))
(cond
((not (save-excursion
(re-search-forward "^[ \t]*:END:[ \t]*$" nil t)))
(org-element-paragraph-parser limit))
((equal "PROPERTIES" name)
(org-element-property-drawer-parser limit))
(t (org-element-drawer-parser limit)))))
;; Fixed Width
((looking-at "[ \t]*:\\( \\|$\\)")
(org-element-fixed-width-parser limit))
;; Inline Comments, Blocks, Babel Calls, Dynamic Blocks and
;; Keywords.
((looking-at "[ \t]*#")
(goto-char (match-end 0))
(cond ((looking-at "\\(?: \\|$\\)")
(beginning-of-line)
(org-element-comment-parser limit))
((looking-at "\\+BEGIN_\\(\\S-+\\)")
(beginning-of-line)
(let ((parser (assoc (upcase (match-string 1))
org-element-block-name-alist)))
(if parser (funcall (cdr parser) limit)
(org-element-special-block-parser limit))))
((looking-at "\\+CALL:")
(beginning-of-line)
(org-element-babel-call-parser limit))
((looking-at "\\+BEGIN:? ")
(beginning-of-line)
(org-element-dynamic-block-parser limit))
((looking-at "\\+\\S-+:")
(beginning-of-line)
(org-element-keyword-parser limit))
(t
(beginning-of-line)
(org-element-paragraph-parser limit))))
;; Footnote Definition.
((looking-at org-footnote-definition-re)
(org-element-footnote-definition-parser limit))
;; Horizontal Rule.
((looking-at "[ \t]*-\\{5,\\}[ \t]*$")
(org-element-horizontal-rule-parser limit))
;; Table.
((org-at-table-p t) (org-element-table-parser limit))
;; List.
((looking-at (org-item-re))
(org-element-plain-list-parser limit (or structure (org-list-struct))))
;; Default element: Paragraph.
(t (org-element-paragraph-parser limit))))))
;; Most elements can have affiliated keywords. When looking for an
;; element beginning, we want to move before them, as they belong to
;; that element, and, in the meantime, collect information they give
;; into appropriate properties. Hence the following function.
;;
;; Usage of optional arguments may not be obvious at first glance:
;;
;; - TRANS-LIST is used to polish keywords names that have evolved
;; during Org history. In example, even though =result= and
;; =results= coexist, we want to have them under the same =result=
;; property. It's also true for "srcname" and "name", where the
;; latter seems to be preferred nowadays (thus the "name" property).
;;
;; - CONSED allows to regroup multi-lines keywords under the same
;; property, while preserving their own identity. This is mostly
;; used for "attr_latex" and al.
;;
;; - PARSED prepares a keyword value for export. This is useful for
;; "caption". Objects restrictions for such keywords are defined in
;; `org-element-object-restrictions'.
;;
;; - DUALS is used to take care of keywords accepting a main and an
;; optional secondary values. For example "results" has its
;; source's name as the main value, and may have an hash string in
;; optional square brackets as the secondary one.
;;
;; A keyword may belong to more than one category.
(defun org-element--collect-affiliated-keywords
(&optional key-re trans-list consed parsed duals)
"Collect affiliated keywords before point.
Optional argument KEY-RE is a regexp matching keywords, which
puts matched keyword in group 1. It defaults to
`org-element--affiliated-re'.
TRANS-LIST is an alist where key is the keyword and value the
property name it should be translated to, without the colons. It
defaults to `org-element-keyword-translation-alist'.
CONSED is a list of strings. Any keyword belonging to that list
will have its value consed. The check is done after keyword
translation. It defaults to `org-element-multiple-keywords'.
PARSED is a list of strings. Any keyword member of this list
will have its value parsed. The check is done after keyword
translation. If a keyword is a member of both CONSED and PARSED,
it's value will be a list of parsed strings. It defaults to
`org-element-parsed-keywords'.
DUALS is a list of strings. Any keyword member of this list can
have two parts: one mandatory and one optional. Its value is
a cons cell whose CAR is the former, and the CDR the latter. If
a keyword is a member of both PARSED and DUALS, both values will
be parsed. It defaults to `org-element-dual-keywords'.
Return a list whose CAR is the position at the first of them and
CDR a plist of keywords and values."
(save-excursion
(let ((case-fold-search t)
(key-re (or key-re org-element--affiliated-re))
(trans-list (or trans-list org-element-keyword-translation-alist))
(consed (or consed org-element-multiple-keywords))
(parsed (or parsed org-element-parsed-keywords))
(duals (or duals org-element-dual-keywords))
;; RESTRICT is the list of objects allowed in parsed
;; keywords value.
(restrict (org-element-restriction 'keyword))
output)
(unless (bobp)
(while (and (not (bobp)) (progn (forward-line -1) (looking-at key-re)))
(let* ((raw-kwd (upcase (or (match-string 2) (match-string 1))))
;; Apply translation to RAW-KWD. From there, KWD is
;; the official keyword.
(kwd (or (cdr (assoc raw-kwd trans-list)) raw-kwd))
;; Find main value for any keyword.
(value
(save-match-data
(org-trim
(buffer-substring-no-properties
(match-end 0) (point-at-eol)))))
;; If KWD is a dual keyword, find its secondary
;; value. Maybe parse it.
(dual-value
(and (member kwd duals)
(let ((sec (org-match-string-no-properties 3)))
(if (or (not sec) (not (member kwd parsed))) sec
(org-element-parse-secondary-string sec restrict)))))
;; Attribute a property name to KWD.
(kwd-sym (and kwd (intern (concat ":" (downcase kwd))))))
;; Now set final shape for VALUE.
(when (member kwd parsed)
(setq value (org-element-parse-secondary-string value restrict)))
(when (member kwd duals)
;; VALUE is mandatory. Set it to nil if there is none.
(setq value (and value (cons value dual-value))))
;; Attributes are always consed.
(when (or (member kwd consed) (string-match "^ATTR_" kwd))
(setq value (cons value (plist-get output kwd-sym))))
;; Eventually store the new value in OUTPUT.
(setq output (plist-put output kwd-sym value))))
(unless (looking-at key-re) (forward-line 1)))
(list (point) output))))
\f
;;; The Org Parser
;;
;; The two major functions here are `org-element-parse-buffer', which
;; parses Org syntax inside the current buffer, taking into account
;; region, narrowing, or even visibility if specified, and
;; `org-element-parse-secondary-string', which parses objects within
;; a given string.
;;
;; The (almost) almighty `org-element-map' allows to apply a function
;; on elements or objects matching some type, and accumulate the
;; resulting values. In an export situation, it also skips unneeded
;; parts of the parse tree.
(defun org-element-parse-buffer (&optional granularity visible-only)
"Recursively parse the buffer and return structure.
If narrowing is in effect, only parse the visible part of the
buffer.
Optional argument GRANULARITY determines the depth of the
recursion. It can be set to the following symbols:
`headline' Only parse headlines.
`greater-element' Don't recurse into greater elements excepted
headlines and sections. Thus, elements
parsed are the top-level ones.
`element' Parse everything but objects and plain text.
`object' Parse the complete buffer (default).
When VISIBLE-ONLY is non-nil, don't parse contents of hidden
elements.
Assume buffer is in Org mode."
(save-excursion
(goto-char (point-min))
(org-skip-whitespace)
(org-element--parse-elements
(point-at-bol) (point-max)
;; Start in `first-section' mode so text before the first
;; headline belongs to a section.
'first-section nil granularity visible-only (list 'org-data nil))))
(defun org-element-parse-secondary-string (string restriction &optional parent)
"Recursively parse objects in STRING and return structure.
RESTRICTION is a symbol limiting the object types that will be
looked after.
Optional argument PARENT, when non-nil, is the element or object
containing the secondary string. It is used to set correctly
`:parent' property within the string."
(with-temp-buffer
(insert string)
(let ((secondary (org-element--parse-objects
(point-min) (point-max) nil restriction)))
(mapc (lambda (obj) (org-element-put-property obj :parent parent))
secondary))))
(defun org-element-map (data types fun &optional info first-match no-recursion)
"Map a function on selected elements or objects.
DATA is the parsed tree, as returned by, i.e,
`org-element-parse-buffer'. TYPES is a symbol or list of symbols
of elements or objects types. FUN is the function called on the
matching element or object. It must accept one arguments: the
element or object itself.
When optional argument INFO is non-nil, it should be a plist
holding export options. In that case, parts of the parse tree
not exportable according to that property list will be skipped.
When optional argument FIRST-MATCH is non-nil, stop at the first
match for which FUN doesn't return nil, and return that value.
Optional argument NO-RECURSION is a symbol or a list of symbols
representing elements or objects types. `org-element-map' won't
enter any recursive element or object whose type belongs to that
list. Though, FUN can still be applied on them.
Nil values returned from FUN do not appear in the results."
;; Ensure TYPES and NO-RECURSION are a list, even of one element.
(unless (listp types) (setq types (list types)))
(unless (listp no-recursion) (setq no-recursion (list no-recursion)))
;; Recursion depth is determined by --CATEGORY.
(let* ((--category
(catch 'found
(let ((category 'greater-elements))
(mapc (lambda (type)
(cond ((or (memq type org-element-all-objects)
(eq type 'plain-text))
;; If one object is found, the function
;; has to recurse into every object.
(throw 'found 'objects))
((not (memq type org-element-greater-elements))
;; If one regular element is found, the
;; function has to recurse, at least,
;; into every element it encounters.
(and (not (eq category 'elements))
(setq category 'elements)))))
types)
category)))
--acc
--walk-tree
(--walk-tree
(function
(lambda (--data)
;; Recursively walk DATA. INFO, if non-nil, is a plist
;; holding contextual information.
(let ((--type (org-element-type --data)))
(cond
((not --data))
;; Ignored element in an export context.
((and info (memq --data (plist-get info :ignore-list))))
;; Secondary string: only objects can be found there.
((not --type)
(when (eq --category 'objects) (mapc --walk-tree --data)))
;; Unconditionally enter parse trees.
((eq --type 'org-data)
(mapc --walk-tree (org-element-contents --data)))
(t
;; Check if TYPE is matching among TYPES. If so,
;; apply FUN to --DATA and accumulate return value
;; into --ACC (or exit if FIRST-MATCH is non-nil).
(when (memq --type types)
(let ((result (funcall fun --data)))
(cond ((not result))
(first-match (throw '--map-first-match result))
(t (push result --acc)))))
;; If --DATA has a secondary string that can contain
;; objects with their type among TYPES, look into it.
(when (eq --category 'objects)
(let ((sec-prop
(assq --type org-element-secondary-value-alist)))
(when sec-prop
(funcall --walk-tree
(org-element-property (cdr sec-prop) --data)))))
;; Determine if a recursion into --DATA is possible.
(cond
;; --TYPE is explicitly removed from recursion.
((memq --type no-recursion))
;; --DATA has no contents.
((not (org-element-contents --data)))
;; Looking for greater elements but --DATA is simply
;; an element or an object.
((and (eq --category 'greater-elements)
(not (memq --type org-element-greater-elements))))
;; Looking for elements but --DATA is an object.
((and (eq --category 'elements)
(memq --type org-element-all-objects)))
;; In any other case, map contents.
(t (mapc --walk-tree (org-element-contents --data)))))))))))
(catch '--map-first-match
(funcall --walk-tree data)
;; Return value in a proper order.
(nreverse --acc))))
;; The following functions are internal parts of the parser.
;;
;; The first one, `org-element--parse-elements' acts at the element's
;; level.
;;
;; The second one, `org-element--parse-objects' applies on all objects
;; of a paragraph or a secondary string. It uses
;; `org-element--get-next-object-candidates' to optimize the search of
;; the next object in the buffer.
;;
;; More precisely, that function looks for every allowed object type
;; first. Then, it discards failed searches, keeps further matches,
;; and searches again types matched behind point, for subsequent
;; calls. Thus, searching for a given type fails only once, and every
;; object is searched only once at top level (but sometimes more for
;; nested types).
(defun org-element--parse-elements
(beg end special structure granularity visible-only acc)
"Parse elements between BEG and END positions.
SPECIAL prioritize some elements over the others. It can be set
to `first-section', `quote-section', `section' `item' or
`table-row'.
When value is `item', STRUCTURE will be used as the current list
structure.
GRANULARITY determines the depth of the recursion. See
`org-element-parse-buffer' for more information.
When VISIBLE-ONLY is non-nil, don't parse contents of hidden
elements.
Elements are accumulated into ACC."
(save-excursion
(goto-char beg)
;; When parsing only headlines, skip any text before first one.
(when (and (eq granularity 'headline) (not (org-at-heading-p)))
(org-with-limited-levels (outline-next-heading)))
;; Main loop start.
(while (< (point) end)
;; Find current element's type and parse it accordingly to
;; its category.
(let* ((element (org-element--current-element
end granularity special structure))
(type (org-element-type element))
(cbeg (org-element-property :contents-begin element)))
(goto-char (org-element-property :end element))
;; Fill ELEMENT contents by side-effect.
(cond
;; If VISIBLE-ONLY is true and element is hidden or if it has
;; no contents, don't modify it.
((or (and visible-only (org-element-property :hiddenp element))
(not cbeg)))
;; Greater element: parse it between `contents-begin' and
;; `contents-end'. Make sure GRANULARITY allows the
;; recursion, or ELEMENT is an headline, in which case going
;; inside is mandatory, in order to get sub-level headings.
((and (memq type org-element-greater-elements)
(or (memq granularity '(element object nil))
(and (eq granularity 'greater-element)
(eq type 'section))
(eq type 'headline)))
(org-element--parse-elements
cbeg (org-element-property :contents-end element)
;; Possibly switch to a special mode.
(case type
(headline
(if (org-element-property :quotedp element) 'quote-section
'section))
(plain-list 'item)
(table 'table-row))
(org-element-property :structure element)
granularity visible-only element))
;; ELEMENT has contents. Parse objects inside, if
;; GRANULARITY allows it.
((memq granularity '(object nil))
(org-element--parse-objects
cbeg (org-element-property :contents-end element) element
(org-element-restriction type))))
(org-element-adopt-elements acc element)))
;; Return result.
acc))
(defun org-element--parse-objects (beg end acc restriction)
"Parse objects between BEG and END and return recursive structure.
Objects are accumulated in ACC.
RESTRICTION is a list of object types which are allowed in the
current object."
(let (candidates)
(save-excursion
(goto-char beg)
(while (and (< (point) end)
(setq candidates (org-element--get-next-object-candidates
end restriction candidates)))
(let ((next-object
(let ((pos (apply 'min (mapcar 'cdr candidates))))
(save-excursion
(goto-char pos)
(funcall (intern (format "org-element-%s-parser"
(car (rassq pos candidates)))))))))
;; 1. Text before any object. Untabify it.
(let ((obj-beg (org-element-property :begin next-object)))
(unless (= (point) obj-beg)
(setq acc
(org-element-adopt-elements
acc
(replace-regexp-in-string
"\t" (make-string tab-width ? )
(buffer-substring-no-properties (point) obj-beg))))))
;; 2. Object...
(let ((obj-end (org-element-property :end next-object))
(cont-beg (org-element-property :contents-begin next-object)))
;; Fill contents of NEXT-OBJECT by side-effect, if it has
;; a recursive type.
(when (and cont-beg
(memq (car next-object) org-element-recursive-objects))
(save-restriction
(narrow-to-region
cont-beg
(org-element-property :contents-end next-object))
(org-element--parse-objects
(point-min) (point-max) next-object
(org-element-restriction next-object))))
(setq acc (org-element-adopt-elements acc next-object))
(goto-char obj-end))))
;; 3. Text after last object. Untabify it.
(unless (= (point) end)
(setq acc
(org-element-adopt-elements
acc
(replace-regexp-in-string
"\t" (make-string tab-width ? )
(buffer-substring-no-properties (point) end)))))
;; Result.
acc)))
(defun org-element--get-next-object-candidates (limit restriction objects)
"Return an alist of candidates for the next object.
LIMIT bounds the search, and RESTRICTION narrows candidates to
some object types.
Return value is an alist whose CAR is position and CDR the object
type, as a symbol.
OBJECTS is the previous candidates alist."
(let (next-candidates types-to-search)
;; If no previous result, search every object type in RESTRICTION.
;; Otherwise, keep potential candidates (old objects located after
;; point) and ask to search again those which had matched before.
(if (not objects) (setq types-to-search restriction)
(mapc (lambda (obj)
(if (< (cdr obj) (point)) (push (car obj) types-to-search)
(push obj next-candidates)))
objects))
;; Call the appropriate successor function for each type to search
;; and accumulate matches.
(mapc
(lambda (type)
(let* ((successor-fun
(intern
(format "org-element-%s-successor"
(or (cdr (assq type org-element-object-successor-alist))
type))))
(obj (funcall successor-fun limit)))
(and obj (push obj next-candidates))))
types-to-search)
;; Return alist.
next-candidates))
\f
;;; Towards A Bijective Process
;;
;; The parse tree obtained with `org-element-parse-buffer' is really
;; a snapshot of the corresponding Org buffer. Therefore, it can be
;; interpreted and expanded into a string with canonical Org syntax.
;; Hence `org-element-interpret-data'.
;;
;; The function relies internally on
;; `org-element--interpret-affiliated-keywords'.
;;;###autoload
(defun org-element-interpret-data (data &optional parent)
"Interpret DATA as Org syntax.
DATA is a parse tree, an element, an object or a secondary string
to interpret.
Optional argument PARENT is used for recursive calls. It contains
the element or object containing data, or nil.
Return Org syntax as a string."
(let* ((type (org-element-type data))
(results
(cond
;; Secondary string.
((not type)
(mapconcat
(lambda (obj) (org-element-interpret-data obj parent))
data ""))
;; Full Org document.
((eq type 'org-data)
(mapconcat
(lambda (obj) (org-element-interpret-data obj parent))
(org-element-contents data) ""))
;; Plain text.
((stringp data) data)
;; Element/Object without contents.
((not (org-element-contents data))
(funcall (intern (format "org-element-%s-interpreter" type))
data nil))
;; Element/Object with contents.
(t
(let* ((greaterp (memq type org-element-greater-elements))
(objectp (and (not greaterp)
(memq type org-element-recursive-objects)))
(contents
(mapconcat
(lambda (obj) (org-element-interpret-data obj data))
(org-element-contents
(if (or greaterp objectp) data
;; Elements directly containing objects must
;; have their indentation normalized first.
(org-element-normalize-contents
data
;; When normalizing first paragraph of an
;; item or a footnote-definition, ignore
;; first line's indentation.
(and (eq type 'paragraph)
(equal data (car (org-element-contents parent)))
(memq (org-element-type parent)
'(footnote-definiton item))))))
"")))
(funcall (intern (format "org-element-%s-interpreter" type))
data
(if greaterp (org-element-normalize-contents contents)
contents)))))))
(if (memq type '(org-data plain-text nil)) results
;; Build white spaces. If no `:post-blank' property is
;; specified, assume its value is 0.
(let ((post-blank (or (org-element-property :post-blank data) 0)))
(if (memq type org-element-all-objects)
(concat results (make-string post-blank 32))
(concat
(org-element--interpret-affiliated-keywords data)
(org-element-normalize-string results)
(make-string post-blank 10)))))))
(defun org-element--interpret-affiliated-keywords (element)
"Return ELEMENT's affiliated keywords as Org syntax.
If there is no affiliated keyword, return the empty string."
(let ((keyword-to-org
(function
(lambda (key value)
(let (dual)
(when (member key org-element-dual-keywords)
(setq dual (cdr value) value (car value)))
(concat "#+" key
(and dual
(format "[%s]" (org-element-interpret-data dual)))
": "
(if (member key org-element-parsed-keywords)
(org-element-interpret-data value)
value)
"\n"))))))
(mapconcat
(lambda (prop)
(let ((value (org-element-property prop element))
(keyword (upcase (substring (symbol-name prop) 1))))
(when value
(if (or (member keyword org-element-multiple-keywords)
;; All attribute keywords can have multiple lines.
(string-match "^ATTR_" keyword))
(mapconcat (lambda (line) (funcall keyword-to-org keyword line))
value
"")
(funcall keyword-to-org keyword value)))))
;; List all ELEMENT's properties matching an attribute line or an
;; affiliated keyword, but ignore translated keywords since they
;; cannot belong to the property list.
(loop for prop in (nth 1 element) by 'cddr
when (let ((keyword (upcase (substring (symbol-name prop) 1))))
(or (string-match "^ATTR_" keyword)
(and
(member keyword org-element-affiliated-keywords)
(not (assoc keyword
org-element-keyword-translation-alist)))))
collect prop)
"")))
;; Because interpretation of the parse tree must return the same
;; number of blank lines between elements and the same number of white
;; space after objects, some special care must be given to white
;; spaces.
;;
;; The first function, `org-element-normalize-string', ensures any
;; string different from the empty string will end with a single
;; newline character.
;;
;; The second function, `org-element-normalize-contents', removes
;; global indentation from the contents of the current element.
(defun org-element-normalize-string (s)
"Ensure string S ends with a single newline character.
If S isn't a string return it unchanged. If S is the empty
string, return it. Otherwise, return a new string with a single
newline character at its end."
(cond
((not (stringp s)) s)
((string= "" s) "")
(t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
(replace-match "\n" nil nil s)))))
(defun org-element-normalize-contents (element &optional ignore-first)
"Normalize plain text in ELEMENT's contents.
ELEMENT must only contain plain text and objects.
If optional argument IGNORE-FIRST is non-nil, ignore first line's
indentation to compute maximal common indentation.
Return the normalized element that is element with global
indentation removed from its contents. The function assumes that
indentation is not done with TAB characters."
(let* (ind-list ; for byte-compiler
collect-inds ; for byte-compiler
(collect-inds
(function
;; Return list of indentations within BLOB. This is done by
;; walking recursively BLOB and updating IND-LIST along the
;; way. FIRST-FLAG is non-nil when the first string hasn't
;; been seen yet. It is required as this string is the only
;; one whose indentation doesn't happen after a newline
;; character.
(lambda (blob first-flag)
(mapc
(lambda (object)
(when (and first-flag (stringp object))
(setq first-flag nil)
(string-match "\\`\\( *\\)" object)
(let ((len (length (match-string 1 object))))
;; An indentation of zero means no string will be
;; modified. Quit the process.
(if (zerop len) (throw 'zero (setq ind-list nil))
(push len ind-list))))
(cond
((stringp object)
(let ((start 0))
;; Avoid matching blank or empty lines.
(while (and (string-match "\n\\( *\\)\\(.\\)" object start)
(not (equal (match-string 2 object) " ")))
(setq start (match-end 0))
(push (length (match-string 1 object)) ind-list))))
((memq (org-element-type object) org-element-recursive-objects)
(funcall collect-inds object first-flag))))
(org-element-contents blob))))))
;; Collect indentation list in ELEMENT. Possibly remove first
;; value if IGNORE-FIRST is non-nil.
(catch 'zero (funcall collect-inds element (not ignore-first)))
(if (not ind-list) element
;; Build ELEMENT back, replacing each string with the same
;; string minus common indentation.
(let* (build ; For byte compiler.
(build
(function
(lambda (blob mci first-flag)
;; Return BLOB with all its strings indentation
;; shortened from MCI white spaces. FIRST-FLAG is
;; non-nil when the first string hasn't been seen
;; yet.
(setcdr (cdr blob)
(mapcar
(lambda (object)
(when (and first-flag (stringp object))
(setq first-flag nil)
(setq object
(replace-regexp-in-string
(format "\\` \\{%d\\}" mci) "" object)))
(cond
((stringp object)
(replace-regexp-in-string
(format "\n \\{%d\\}" mci) "\n" object))
((memq (org-element-type object)
org-element-recursive-objects)
(funcall build object mci first-flag))
(t object)))
(org-element-contents blob)))
blob))))
(funcall build element (apply 'min ind-list) (not ignore-first))))))
\f
;;; The Toolbox
;;
;; The first move is to implement a way to obtain the smallest element
;; containing point. This is the job of `org-element-at-point'. It
;; basically jumps back to the beginning of section containing point
;; and moves, element after element, with
;; `org-element--current-element' until the container is found. Note:
;; When using `org-element-at-point', secondary values are never
;; parsed since the function focuses on elements, not on objects.
;;
;; At a deeper level, `org-element-context' lists all elements and
;; objects containing point.
;;
;; `org-element-nested-p' and `org-element-swap-A-B' may be used
;; internally by navigation and manipulation tools.
;;;###autoload
(defun org-element-at-point (&optional keep-trail)
"Determine closest element around point.
Return value is a list like (TYPE PROPS) where TYPE is the type
of the element and PROPS a plist of properties associated to the
element.
Possible types are defined in `org-element-all-elements'.
Properties depend on element or object type, but always
include :begin, :end, :parent and :post-blank properties.
As a special case, if point is at the very beginning of a list or
sub-list, returned element will be that list instead of the first
item. In the same way, if point is at the beginning of the first
row of a table, returned element will be the table instead of the
first row.
If optional argument KEEP-TRAIL is non-nil, the function returns
a list of of elements leading to element at point. The list's
CAR is always the element at point. Following positions contain
element's siblings, then parents, siblings of parents, until the
first element of current section."
(org-with-wide-buffer
;; If at an headline, parse it. It is the sole element that
;; doesn't require to know about context. Be sure to disallow
;; secondary string parsing, though.
(if (org-with-limited-levels (org-at-heading-p))
(progn
(beginning-of-line)
(if (not keep-trail) (org-element-headline-parser (point-max) t)
(list (org-element-headline-parser (point-max) t))))
;; Otherwise move at the beginning of the section containing
;; point.
(let ((origin (point))
(end (save-excursion
(org-with-limited-levels (outline-next-heading)) (point)))
element type special-flag trail struct prevs parent)
(org-with-limited-levels
(if (org-with-limited-levels (org-before-first-heading-p))
(goto-char (point-min))
(org-back-to-heading)
(forward-line)))
(org-skip-whitespace)
(beginning-of-line)
;; Parse successively each element, skipping those ending
;; before original position.
(catch 'exit
(while t
(setq element
(org-element--current-element end 'element special-flag struct)
type (car element))
(org-element-put-property element :parent parent)
(when keep-trail (push element trail))
(cond
;; 1. Skip any element ending before point. Also skip
;; element ending at point when we're sure that another
;; element has started.
((let ((elem-end (org-element-property :end element)))
(when (or (< elem-end origin)
(and (= elem-end origin) (/= elem-end end)))
(goto-char elem-end))))
;; 2. An element containing point is always the element at
;; point.
((not (memq type org-element-greater-elements))
(throw 'exit (if keep-trail trail element)))
;; 3. At any other greater element type, if point is
;; within contents, move into it.
(t
(let ((cbeg (org-element-property :contents-begin element))
(cend (org-element-property :contents-end element)))
(if (or (not cbeg) (not cend) (> cbeg origin) (< cend origin)
;; Create an anchor for tables and plain lists:
;; when point is at the very beginning of these
;; elements, ignoring affiliated keywords,
;; target them instead of their contents.
(and (= cbeg origin) (memq type '(plain-list table)))
;; When point is at contents end, do not move
;; into elements with an explicit ending, but
;; return that element instead.
(and (= cend origin)
(memq type
'(center-block
drawer dynamic-block inlinetask item
plain-list quote-block special-block))))
(throw 'exit (if keep-trail trail element))
(setq parent element)
(case type
(plain-list
(setq special-flag 'item
struct (org-element-property :structure element)))
(table (setq special-flag 'table-row))
(otherwise (setq special-flag nil)))
(setq end cend)
(goto-char cbeg)))))))))))
;;;###autoload
(defun org-element-context ()
"Return closest element or object around point.
Return value is a list like (TYPE PROPS) where TYPE is the type
of the element or object and PROPS a plist of properties
associated to it.
Possible types are defined in `org-element-all-elements' and
`org-element-all-objects'. Properties depend on element or
object type, but always include :begin, :end, :parent
and :post-blank properties."
(org-with-wide-buffer
(let* ((origin (point))
(element (org-element-at-point))
(type (car element))
end)
;; Check if point is inside an element containing objects or at
;; a secondary string. In that case, move to beginning of the
;; element or secondary string and set END to the other side.
(if (not (or (and (eq type 'item)
(let ((tag (org-element-property :tag element)))
(and tag
(progn
(beginning-of-line)
(search-forward tag (point-at-eol))
(goto-char (match-beginning 0))
(and (>= origin (point))
(<= origin
;; `1+' is required so some
;; successors can match
;; properly their object.
(setq end (1+ (match-end 0)))))))))
(and (memq type '(headline inlinetask))
(progn (beginning-of-line)
(skip-chars-forward "* ")
(setq end (point-at-eol))))
(and (memq type '(paragraph table-cell verse-block))
(let ((cbeg (org-element-property
:contents-begin element))
(cend (org-element-property
:contents-end element)))
(and (>= origin cbeg)
(<= origin cend)
(progn (goto-char cbeg) (setq end cend)))))))
element
(let ((restriction (org-element-restriction element))
(parent element)
candidates)
(catch 'exit
(while (setq candidates (org-element--get-next-object-candidates
end restriction candidates))
(let ((closest-cand (rassq (apply 'min (mapcar 'cdr candidates))
candidates)))
;; If ORIGIN is before next object in element, there's
;; no point in looking further.
(if (> (cdr closest-cand) origin) (throw 'exit element)
(let* ((object
(progn (goto-char (cdr closest-cand))
(funcall (intern (format "org-element-%s-parser"
(car closest-cand))))))
(cbeg (org-element-property :contents-begin object))
(cend (org-element-property :contents-end object)))
(cond
;; ORIGIN is after OBJECT, so skip it.
((< (org-element-property :end object) origin)
(goto-char (org-element-property :end object)))
;; ORIGIN is within a non-recursive object or at an
;; object boundaries: Return that object.
((or (not cbeg) (> cbeg origin) (< cend origin))
(throw 'exit
(org-element-put-property object :parent parent)))
;; Otherwise, move within current object and restrict
;; search to the end of its contents.
(t (goto-char cbeg)
(org-element-put-property object :parent parent)
(setq parent object end cend)))))))
parent))))))
(defsubst org-element-nested-p (elem-A elem-B)
"Non-nil when elements ELEM-A and ELEM-B are nested."
(let ((beg-A (org-element-property :begin elem-A))
(beg-B (org-element-property :begin elem-B))
(end-A (org-element-property :end elem-A))
(end-B (org-element-property :end elem-B)))
(or (and (>= beg-A beg-B) (<= end-A end-B))
(and (>= beg-B beg-A) (<= end-B end-A)))))
(defun org-element-swap-A-B (elem-A elem-B)
"Swap elements ELEM-A and ELEM-B.
Assume ELEM-B is after ELEM-A in the buffer. Leave point at the
end of ELEM-A."
(goto-char (org-element-property :begin elem-A))
;; There are two special cases when an element doesn't start at bol:
;; the first paragraph in an item or in a footnote definition.
(let ((specialp (not (bolp))))
;; Only a paragraph without any affiliated keyword can be moved at
;; ELEM-A position in such a situation. Note that the case of
;; a footnote definition is impossible: it cannot contain two
;; paragraphs in a row because it cannot contain a blank line.
(if (and specialp
(or (not (eq (org-element-type elem-B) 'paragraph))
(/= (org-element-property :begin elem-B)
(org-element-property :contents-begin elem-B))))
(error "Cannot swap elements"))
;; In a special situation, ELEM-A will have no indentation. We'll
;; give it ELEM-B's (which will in, in turn, have no indentation).
(let* ((ind-B (when specialp
(goto-char (org-element-property :begin elem-B))
(org-get-indentation)))
(beg-A (org-element-property :begin elem-A))
(end-A (save-excursion
(goto-char (org-element-property :end elem-A))
(skip-chars-backward " \r\t\n")
(point-at-eol)))
(beg-B (org-element-property :begin elem-B))
(end-B (save-excursion
(goto-char (org-element-property :end elem-B))
(skip-chars-backward " \r\t\n")
(point-at-eol)))
;; Store overlays responsible for visibility status. We
;; also need to store their boundaries as they will be
;; removed from buffer.
(overlays
(cons
(mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
(overlays-in beg-A end-A))
(mapcar (lambda (ov) (list ov (overlay-start ov) (overlay-end ov)))
(overlays-in beg-B end-B))))
;; Get contents.
(body-A (buffer-substring beg-A end-A))
(body-B (delete-and-extract-region beg-B end-B)))
(goto-char beg-B)
(when specialp
(setq body-B (replace-regexp-in-string "\\`[ \t]*" "" body-B))
(org-indent-to-column ind-B))
(insert body-A)
;; Restore ex ELEM-A overlays.
(let ((offset (- beg-B beg-A)))
(mapc (lambda (ov)
(move-overlay
(car ov) (+ (nth 1 ov) offset) (+ (nth 2 ov) offset)))
(car overlays))
(goto-char beg-A)
(delete-region beg-A end-A)
(insert body-B)
;; Restore ex ELEM-B overlays.
(mapc (lambda (ov)
(move-overlay
(car ov) (- (nth 1 ov) offset) (- (nth 2 ov) offset)))
(cdr overlays)))
(goto-char (org-element-property :end elem-B)))))
(provide 'org-element)
;;; org-element.el ends here
|