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
|
;;; objed-objects.el --- Part of the objed package -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2019 Free Software Foundation, Inc.
;; Author: Clemens Radermacher <clemera@posteo.net>
;; Package-Requires: ((emacs "25") (cl-lib "0.5"))
;; Version: 0.8.1
;; Maintainer: Clemens Radermacher <clemera@posteo.net>
;; URL: https://github.com/clemera/objed
;; 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.
;; 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:
;;
;; Code for text objects used by `objed'.
;;; Code:
;; * Bytecomp
(require 'cl-lib)
(require 'subword)
(require 'face-remap)
;; info for byte-comp
(defvar objed-map)
(defvar objed-object-map)
(declare-function avy-process "ext:avy")
(declare-function avy--style-fn "ext:avy")
(declare-function avy-goto-char "ext:avy")
(declare-function stripe-buffer-mode "ext:stripe-buffer")
(declare-function sgml-skip-tag-backward "ext:sgml-mode")
(declare-function sgml-skip-tag-forward "ext:sgml-mode")
(declare-function org-at-heading-p "ext:org")
(declare-function org-back-to-heading "ext:org")
(declare-function outline-back-to-heading "ext:outline")
(declare-function outline-next-heading "ext:outline")
(declare-function outline-previous-heading "ext:outline")
(declare-function outline-next-visible-heading "ext:outline")
(declare-function outline-previous-visible-heading "ext:outline")
(declare-function objed--object-dispatch "ext:objed")
(declare-function objed-current-or-next-context "ext:objed")
(declare-function objed-current-or-previous-context "ext:objed")
(declare-function objed--get-current-state "ext:objed")
(declare-function objed--install-advices "ext:objed")
(declare-function objed--install-advices-for "ext:objed")
(declare-function objed-goto-next-identifier "ext:objed")
(declare-function objed-goto-prev-identifier "ext:objed")
(declare-function objed-next-identifier "ext:objed")
(declare-function objed-prev-identifier "ext:objed")
(declare-function objed-first-identifier "ext:objed")
(declare-function objed-last-identifier "ext:objed")
;; * Macros
(defvar-local objed--object nil
"The symbol of the current object.")
(eval-and-compile
(require 'rx)
(defun objed--get-regex-object (bregex eregex)
"Return regex object between BREGEX and EREGEX.
The inner object part will be the text between the matches for
those two expressions.
BREGEX is the regular expression for the start of the object. If
the regular expressions contains a group, any text which is part
of this group will belong to the inner object part.
EREGEX is the regular expression for the end of the object. If
the regular expressions contains a group, any text which is part
of this group will belong to the inner object part.
EREGEX can also be an empty string. In this case objects are
separated by the BREGEX expression and reach until the next one
or until the buffer end if no next instance can be found."
(let* ((obounds ())
(ibounds ())
(opos (point)))
(save-mark-and-excursion
;; try to move into object when at boundary
(if (looking-at bregex)
(goto-char (or (match-end 1)
(match-end 0)))
(if (looking-back eregex (line-beginning-position))
(goto-char (or (match-beginning 1)
(match-beginning 0)))
(re-search-forward eregex nil t)
(goto-char (or (match-beginning 1)
(match-beginning 0)))))
(when (and ;; goto possible start
(re-search-backward bregex nil t)
(push (or (match-beginning 1)
(match-end 0))
ibounds)
(push (match-beginning 0)
obounds)
;; goto possible end
(goto-char (or (match-end 1)
(match-end 0)))
(cond ((string= "" eregex)
;; no end provided, objects are separated by beginning
;; regex
(cond ((re-search-forward bregex nil t)
(push (match-beginning 0) obounds)
;; no way to tell so just skip ws
;; to have a sensible default
(goto-char (car obounds))
(objed--skip-ws t)
(push (point) ibounds))
(t
;; if there is no match that means the buffer is
;; the object end
(goto-char (point-max))
(push (point) obounds)
(objed--skip-ws t)
(push (point) ibounds))))
(t
(re-search-forward eregex nil t)
(push (or (match-end 1)
(match-beginning 0))
ibounds)
(push (match-end 0)
obounds)))
;; when point was within start and end
(<= (cadr obounds) opos (car obounds)))
(list (nreverse obounds)
(nreverse ibounds))))))
(defun objed--transform-pos-data (plist)
(cond ((and (plist-get plist :beg)
(or (plist-get plist :end)
(stringp (plist-get plist :beg))))
(let ((np nil)
(alt nil)
(skip nil))
;; filter :beg :end keywords
(dolist (item plist)
(if (memq item '(:beg :ibeg :end :iend))
(progn (push item alt)
(setq skip t))
(if (and skip
(not (keywordp item)))
(push item alt)
(push item np)
(setq skip nil))))
;; new and alternate plists
(setq np (nreverse np))
(setq alt (nreverse alt))
;; merge ... :get-obj "regex search"
(if (and (stringp (plist-get plist :beg))
(or (stringp (plist-get plist :end))
(not (plist-get plist :end))))
(let ((bregex (plist-get plist :beg))
(eregex (or (plist-get plist :end)
"")))
(append np
(list :try-prev)
(if (string= "" eregex)
(list `(when (re-search-backward ,bregex)
(goto-char (match-end 0))))
(list `(when (re-search-backward ,eregex)
(goto-char (match-beginning 0)))))
(list :try-next)
(list `(when (re-search-forward ,bregex)
(goto-char (match-end 0))))
(list :get-obj)
(list
`(objed--get-regex-object ,bregex
,eregex))))
;; merge ... :get-obj (objed-make-object :beg ... :end...)
(let ((make nil))
(dolist (el alt)
(when (keywordp el)
(progn
(push el make)
(push (plist-get alt el) make))))
(setq make (nreverse make))
(push 'objed-make-object make)
(append np
(list :get-obj)
;; TODO:save-mark-and-excursion still needed?
;; is wrapped already?
(list (append (list 'save-mark-and-excursion)
(list make))))))))
(t
(user-error "Malformed macro"))))
(defun objed--get-arg-plist (keylst valid &optional wrapped)
"Wraps any forms of keys in keylst in `progn' and returns property list.
KEYLST is the list of keys and forms for object creation. VALID
is a list of valid keyword for the returned list whic is a
property list where each key has an associated progn."
(let* ((keyw (pop keylst))
(vkeyw (and keyw (keywordp keyw) (memq keyw valid) keyw))
forms)
(cond ((memq vkeyw '(:mode :no-skip :commands))
;; skip
(objed--get-arg-plist (cdr keylst) valid wrapped))
(vkeyw
(while (and (not (keywordp (car keylst)))
keylst)
(push (pop keylst) forms))
(push keyw wrapped)
;; allowed to move point
(cond ((memq vkeyw '(:try-next :try-prev :ref))
(push `(let ((objed--block-p t))
,@(nreverse forms))
wrapped))
((memq vkeyw '(:beg :end :ibeg :iend))
(if (and (not (cdr forms))
(stringp (car forms)))
(push (car forms) wrapped)
(if (and (not (cdr forms))
(eq (caar forms) 'rx))
(push (macroexpand-1 (car forms))
wrapped)
(push `(let ((objed--block-p t))
,@(nreverse forms))
wrapped))))
(t
;; objed--block-p: dont run objeds advices here
(push `(let ((objed--block-p t))
(save-mark-and-excursion
,@(nreverse forms)))
wrapped)))
(objed--get-arg-plist keylst valid wrapped))
(keylst
(error "Malformed Object. Keyword %s not recognized" keyw))
(t
(nreverse wrapped))))))
(defmacro objed-define-object (package name &rest args)
"Declare a text object for `objed'.
Usage:
(objed-define-object package name
[:keyword [code-form]...]...)
This macro creates a command named objed-<name>-object. This
command can be used to activate objed for the defined object and
is used internally to query for information needed for objed
commands.
PACKAGE is the name of the package the object should be loaded
for. If non-nil this will defer loading until PACKAGE is
available.
NAME is a symbol which defines the name which will be used to
refer to this object. ARGS is a list of keyword arguments and
corresponding values according to the following descriptions:
:get-obj
Code to run which returns object positions as a list of the form:
((object-start object-end)
(inner-start inner-end))
The function `objed-make-object' can be used to create such a
list. For convenience it is also possible that the code returns a
cons cell of the bounds of object (like what the built-in
`bounds-of-thing-at-point' variations return). If inner positions
are omitted they are determined by `objed--inner-default'. If
there is no object at point the code should return nil.
:beg, :ibeg, :end, :iend
These keywords can be used instead of :get-obj above. The value
for each is the code to run which should return the point
position corresponding to the keyword. Point is allword to move
between the keyword expression. The code runs in the same order
the keywords are provided.
It is also possible to use only :beg and :end with regular
expressions to define an object. See `objed--get-regex-object'
for details of their format. If :end is omitted the regexp
provided by :beg separates the objects on its own. This can be
used for text objects which don't have an end marker.
:try-next (optional)
Code to run which moves point to the next available object.
If :no-skip is not set the code can assume it runs after point is
moved out to the end of the current one if any. This will get
called until :get-obj returns non-nil. To indicate that search
needs to be stopped, throw an error.
:try-prev (optional)
Code to run which moves point to the previous available object.
The code can assume it runs after point is moved to the beginning
of the current one if any. This will get called until :get-obj
returns non-nil. To indicate that search needs to be stopped,
throw an error.
:mode (optional)
Object defintions which use this keyword derive from an already
existing object with the same NAME. If given it should be a
symbol of a `major-mode'. Any keyword definitions of the mode
specific version will override the ones from the non mode
specific version.
:atp (optional)
Code to run which returns non-nil if point is right before the
object.
:ref (optional)
Code to run which returns an object symbol which can be used to
navigate references of an object. This defaults to the textual
content of an object.
:max-search-forward (optional)
Code to run which returns the maximal position an object of this type
is searched for (in forward direction).
:no-skip (optional)
If this keyword is provided with a non-nil value, the current object
is not skipped before search for the next one via :try-next.
:commands (optional)
If given the value should be a list of commands for which objed
should activate (when variable `objed-mode' is on) with the object beeing
defined."
(declare (indent 2))
(let* ((mode (plist-get args :mode))
(noskip (plist-get args :no-skip))
(commands (plist-get args :commands))
(fname (if mode
(intern (format "objed-%s-%s-object" name mode))
(intern (format "objed-%s-object" name))))
;; wrap code chunks
(args (objed--get-arg-plist
args
'(:mode :no-skip :max-search-forward :commands :atp :ref :get-obj :try-next :try-prev
:beg :ibeg :iend :end)))
;; transform to final form if necessary
(args (if (plist-get args :get-obj) args
(objed--transform-pos-data args)))
(arg (make-symbol "arg"))
(cbody nil)
(doc (format "%s object." (capitalize (symbol-name name))))
(atp (plist-get args :atp))
(max (plist-get args :max-search-forward))
(obj (plist-get args :get-obj))
(next (plist-get args :try-next))
(prev (plist-get args :try-prev))
(ref (plist-get args :ref)))
(unless mode
(push `((and (eq real-this-command ',fname)
(not ,arg))
(objed--object-dispatch ',name))
cbody))
(when atp
(push `((eq ,arg :atp)
,atp)
cbody))
(when max
(push `((eq ,arg :max-search-forward)
,max)
cbody))
(when ref
(push `((eq ,arg :ref)
,ref)
cbody))
(when obj
(push `((eq ,arg :get-obj)
(let ((pdata ,obj))
(if (and (consp pdata)
(not (consp (cdr pdata))))
(objed-make-object :obounds pdata)
pdata)))
cbody))
(when next
(push `((eq ,arg :try-next)
,next)
cbody))
(when prev
(push `((eq ,arg :try-prev)
,prev)
cbody))
(cond (mode
(let ((res (if package
(list `',package 'with-eval-after-load)
(list 'progn))))
(when noskip
(push `(put ',fname 'objed-no-skip t)
res))
(when commands
(push `(with-eval-after-load 'objed
(objed--install-advices-for ',commands ',name))
res))
;; catch all return arg if not present
(push `(t ,arg) cbody)
(push `(defun ,fname (,arg)
,doc
(cond ,@(nreverse cbody)))
res)
(nreverse res)))
(t
(let ((res (if package
(list `',package 'with-eval-after-load)
(list 'progn))))
(when noskip
(push `(put ',fname 'objed-no-skip t)
res))
(when commands
(push `(objed--install-advices-for ',commands ',name)
res))
(push `(defun ,fname (,arg)
,doc
(interactive "i")
(cond ,@(nreverse cbody)))
res)
(nreverse res))))))
(defun objed--define-kpair (map key name)
"Use MAP to define KEY for object NAME."
(let ((cmd (objed--name2func name 'nomode)))
(define-key map key cmd)))
(defun objed-define-global-object-keys (&rest kpairs)
"Define global object keys.
KPAIRS are pairs of the key and the object name."
(let ((map (default-value 'objed-object-map)))
(while kpairs
(objed--define-kpair map (pop kpairs) (pop kpairs)))))
(defun objed-define-local-object-keys (&rest kpairs)
"Define object keys locally for current buffer.
This function is intended to be used inside mode hooks to add
mode specific object bindings to the currently existing ones. If
you want to replace the object key bindings entirely with local
ones see `objed-define-local-object-keys*'.
KPAIRS are pairs of the key and the object name."
(unless (local-variable-p 'objed-map)
(setq-local objed-map
(make-composed-keymap nil (default-value 'objed-map))))
(unless (local-variable-p 'objed-object-map)
(setq-local objed-object-map
(make-composed-keymap nil (default-value 'objed-object-map))))
(while kpairs
(objed--define-kpair objed-object-map (pop kpairs) (pop kpairs)))
(let ((switchk (where-is-internal (default-value 'objed-object-map)
(default-value 'objed-map) t)))
(define-key objed-map switchk objed-object-map)))
(defun objed--init-local-map (lmap map)
"Initilize local LMAP to shadow bindings in MAP."
(map-keymap (lambda (ev def)
(if (keymapp def)
(objed--init-local-map lmap def)
(define-key lmap (vector ev) nil)))
map))
(defun objed-define-local-object-keys* (&rest kpairs)
"Define object keys locally for current buffer.
This function is intended to be used inside mode hooks and
creates new object key bindings for the current buffer. This
means any previous object key bindings bindings are replaced by
the bindings defined in KPAIRS.
If you only want to add or change a few mode specific bindings
see `objed-define-local-object-keys'.
KPAIRS are pairs of the key and the object name."
(unless (local-variable-p 'objed-map)
(setq-local objed-map
(make-composed-keymap nil (default-value 'objed-map))))
(setq-local objed-object-map (make-sparse-keymap))
(objed--init-local-map objed-object-map (default-value 'objed-map))
(while kpairs
(objed--define-kpair objed-object-map (pop kpairs) (pop kpairs)))
(let ((switchk (where-is-internal (default-value 'objed-object-map)
(default-value 'objed-map) t)))
(define-key objed-map switchk objed-object-map)))
(defmacro objed--with-narrow-for-text (&rest body)
"Execute BODY narrowed to string or comment."
`(save-restriction
;; stay inside
(unless (derived-mode-p 'text-mode)
(objed--narrow-if-string-or-comment))
,@body))
;; * Global vars saving object information
;; TODO: use defstruct object instead
(defvar objed--current-obj nil
"The current object position data.
Positions are stored in a list of the form:
((object-start object-end)
(inner-start inner-end))")
(defvar-local objed--obj-state nil
"The state used to get object positions.
Either the symbol `whole' or `inner'.")
(defvar objed--marked-ovs nil
"List of overlays of marked objects.")
;; * Internal object access functions
(defun objed--inside-object-p (obj)
"Return non-nil if point point inside object OBJ."
(let* ((objed--object obj)
(objed--obj-state 'whole)
(obj (if (symbolp obj)
(objed--get)
obj)))
(when (and obj (not (objed--distant-p obj)))
obj)))
(defun objed--beg (&optional obj)
"Get beginning position of object.
Ignores current object state. OBJ is the object to use and
defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(caar obj)))
(defun objed--end (&optional obj)
"Get end position of object.
Ignores current object state. OBJ is the object to use and
defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(cadr (car obj))))
(defun objed--object-at-point (obj &optional state)
"Return object data for OBJ and STATE a point.
Does return nil when there is no such object."
(let* ((objed--object obj)
(objed--obj-state (or state 'whole))
(o (ignore-errors (objed--object :get-obj))))
(when o
(if (eq state 'inner)
(nreverse o)
o))))
(defun objed--other (&optional obj)
"Return object position opposite to point.
Ignores current object state. OBJ is the object to use and
defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(if (/= (point) (objed--end))
(objed--end obj)
(objed--beg obj))))
(defun objed--min (&optional obj)
"Get minimal position of current object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(objed--apply #'min obj)))
(defun objed--max (&optional obj)
"Get maximal position of current object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(objed--apply #'max obj)))
(defvar objed--basic-objects
'(sexp line identifier word char region buffer)
"Basic objects.
Basic object are objects which have no next/previous or which
have their own movement commands.")
(defun objed--basic-p ()
"Return non-nil if current object is a basic object.
From basic objects `objed' starts expanding to context objects.
Thus this should be objects which have their own movement
commands."
(memq objed--object objed--basic-objects))
(defun objed--current (&optional obj)
"Get the current range of interest.
If the region is active the range is defined by the region bounds
otherwise the its the head of object OBJ which defaults to
`objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(cond ((region-active-p)
(list (region-beginning)
(if (objed--basic-p)
(region-end)
(max (objed--end)
(region-end)))))
(t
(car obj)))))
(defun objed--bounds (&optional obj)
"Get the current object bounds.
If the region is active bounds are the region bounds otherwise
the head of object OBJ which defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(if (region-active-p)
(cons (region-beginning) (region-end))
(cons (objed--beg obj)
(objed--end obj)))))
(defun objed--alt (&optional obj)
"Get the current tail of `objed--current-obj'.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(cadr obj)))
(defun objed--alt-beg (&optional obj)
"Get beginning position of tail of object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let* ((obj (or obj objed--current-obj))
(posn (objed--alt obj)))
(car posn)))
(defun objed--alt-end (&optional obj)
"Get end position of tail of object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let* ((obj (or obj objed--current-obj))
(posn (objed--alt obj)))
(cadr posn)))
(defun objed--obeg (&optional obj)
"Get beginning position of object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(if (objed--inner-p)
(objed--alt-beg obj)
(objed--beg obj))))
(defun objed--oend (&optional obj)
"Get end position of object.
OBJ is the object to use and defaults to `objed--current-obj'"
(let ((obj (or obj objed--current-obj)))
(if (objed--inner-p)
(objed--alt-end obj)
(objed--end obj))))
(defun objed--ibeg (&optional obj)
"Get inner beginning position of object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(if (objed--inner-p)
(objed--beg obj)
(objed--alt-beg obj))))
(defun objed--iend (&optional obj)
"Get inner end position of object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(if (objed--inner-p)
(objed--end obj)
(objed--alt-end obj))))
(defun objed--get-left-boundary ()
"Get left boundary of current object."
(buffer-substring (objed--obeg) (objed--ibeg)))
(defun objed--get-right-boundary ()
"Get right boundary of current object."
(buffer-substring (objed--iend) (objed--oend)))
(defun objed--object-string ()
"Return object string content."
(filter-buffer-substring (objed--beg)
(objed--end)))
(defun objed--at-object-p (obj)
"Return non nil when point is at object OBJ."
(funcall (objed--name2func obj) :atp))
(defun objed--goto-char (pos)
"Move to position POS possibly skipping leading whitespace."
(goto-char
(if (eq objed--object 'char)
pos
(objed--skip-forward pos 'ws))))
(defun objed--collect-backward (pos min &optional collf)
"Collect object positions in backward direction.
Start from position POS and stop at MIN position.
The returned list contains cons cells of the start positions of
the objects and the current window. If COLLF is t collect the
end positions instead. Leading or trailing ws are ignored by
default.
If COLLF is function it recieves the object as argument and
should return the data to collect."
(let ((cw (get-buffer-window))
(sobj nil)
(posns nil)
(objed--obj-state 'whole)
(obj nil))
(save-excursion
(goto-char pos)
(while (and (> pos min)
(setq obj (objed--get-prev (point)))
(not (equal obj sobj)))
(setq sobj obj)
(goto-char (setq pos (objed--beg obj)))
(cond ((and collf
(not (eq collf t)))
(push (funcall collf obj) posns))
(t
(push (cons (if (eq collf t)
(objed--skip-backward
(objed--max obj) 'ws)
(objed--skip-forward pos 'ws))
cw)
posns))))
posns)))
(defun objed--collect-forward (pos max &optional collf)
"Collect object positions in forward direction.
Start from position POS and stop at MAX position.
The returned list contains cons cells of the start positions of
the objects and the current window. If COLLF is t collect the
end positions instead. Leading or trailing ws are ignored by
default.
If COLLF is function it recieves the object as argument and
should return the data to collect."
(let ((cw (get-buffer-window))
(sobj nil)
(posns nil)
(objed--obj-state 'whole)
(obj nil))
(save-excursion
(goto-char pos)
(while (and (< pos max)
(setq obj (objed--get-next (point)))
(not (equal obj sobj)))
(setq sobj obj)
(if (objed--no-skipper-p)
(goto-char (setq pos (objed--beg obj)))
(goto-char (setq pos (objed--end obj))))
(cond ((and collf (not (eq collf t)))
(push (funcall collf obj) posns))
(t
(push (cons (if (eq collf t)
(objed--skip-backward
(objed--max obj) 'ws)
(objed--skip-forward (objed--beg obj) 'ws))
cw)
posns)))))
(setq posns (nreverse posns))))
(defun objed--no-skipper-p ()
"If current object should be skipped."
(get (objed--name2func objed--object)
'objed-no-skip))
(defun objed--collect-object-positions (beg end &optional fromp collf)
"Collect object positions.
Returns object positions between BEG and END.
If FROMP is non-nil collect from that position otherwise collect before
and after current object.
By default the returned list contains cons cells of the start
positions of the objects and the current window.
COLLF has the same meaning as for `objed--collect-forward' and
`objed--collect-backward'."
(save-restriction
(narrow-to-region beg end)
(append (objed--collect-backward
(or fromp (objed--min))
beg collf)
(objed--collect-forward
(or fromp (if (objed--no-skipper-p)
(objed--min) (objed--max)))
end collf))))
(defun objed--map (fun &optional obj beg end)
"Call FUN with object data for each object of current type.
Return a list of the results.
If OBJ is on-nil it should be the symbol of the object type to
search for. By default search the whole buffer, alternatively
provide BEG and END position for region to search."
(let ((objed--object (or obj objed--object)))
(objed--collect-object-positions
(or beg (point-min))
(or end (point-max))
(point) fun)))
(defun objed--objects (&optional obj beg end)
"Return list of all objects of current type.
If OBJ is on-nil it should be the symbol of the object type to
search for. By default search the whole buffer, alternatively
provide BEG and END position for region to search."
(objed--map #'identity obj beg end))
(defun objed--collect-object-lines ()
"Collect first lines of objects before and after current object.
Each string has its position as property pos on the first
character of the string."
(let (lines llb lle lb le)
(dolist (pos2win (objed--collect-object-positions
(point-min)
(point-max))
(nreverse lines))
(save-excursion
(setq lle le)
(setq llb lb)
(goto-char (car pos2win))
(let ((str (buffer-substring
(setq lb (objed--skip-forward
(line-beginning-position) 'ws))
(setq le (line-end-position)))))
;; one object per line
(when (and (or (not llb)
(< lle lb)
(not lle)
(> llb le))
(not (string= "" str)))
(push (cons str (point)) lines)))))))
(defvar avy-action)
(defvar avy-all-windows)
(defun objed--ace-until (&optional start back)
"Get position of object using `avy'.
Start at pos START. Default to forward unless BACK is non-nil."
(let* ((avy-action #'identity)
(avy-style 'at-full)
(avy-all-windows t)
(posns (if back
(objed--collect-backward
(or start (point))
(window-start) t)
(objed--collect-forward
(or start (point))
(window-end)))))
(save-excursion
(cond (posns
(let ((pos (if (> (length posns) 1)
(avy-process posns (avy--style-fn avy-style))
(caar posns))))
(when (integer-or-marker-p pos)
(objed--get back pos))))
(t
(prog1 nil
(message "No objects found.")))))))
(defun objed--get (&optional dir pos)
"Get object at current position.
Direction defaults to forward unless DIR is non-nil which means
to search backwards.
POS defaults to point. When no object is found at current
position returns the next accessible one in DIR. Object position
order depends on `objed--obj-state'. To exit early from search
objects can throw an error."
(save-excursion
(let ((darg (if dir :try-prev :try-next))
(max (if dir nil (objed--object :max-search-forward)))
(stop (if dir #'bobp #'eobp)))
(when pos
(goto-char pos))
(let (invisible break nobj obj)
;; while there is no new object found which is visible
;; and the buffer boundary is not reached
(while (and (not break)
(or (not max)
(<= (point) max))
(or (not (and (setq nobj (objed--object :get-obj))
(and nobj (not (equal obj nobj)))))
(and nobj
;; update the last seen one
(setq obj nobj)
(setq invisible
(objed--invisible-p (objed--beg nobj)))))
(not (funcall stop)))
(cond (invisible
(setq invisible nil)
(goto-char (if dir
(objed--prev-visible-point)
(objed--next-visible-point))))
(t
(let ((f (if dir '> '<))
(step (if dir -1 1)))
(unless (funcall stop)
(let ((pos (point)))
(objed--object darg)
;; if point has not moved
;; fallback to move one char
;; in right direction
(when (= (point) pos)
(forward-char step))
;; check for valid move direction to avoid inf. loop if
;; the code of object misbehaves
(when (funcall f (point) pos)
(setq break t))))))))
(if (objed--inner-p)
(nreverse nobj)
nobj)))))
(defun objed--copy-object (&optional obj)
"Make a deep copy of object OBJ.
OBJ defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(list (copy-sequence (car obj))
(copy-sequence (cadr obj)))))
(defun objed--get-object (o &optional s)
"Get object O with state S.
When object O is already current return it."
(if (eq objed--object o)
objed--current-obj
(let ((objed--object o)
(objed--obj-state (or s 'whole)))
(objed--get))))
(defun objed--name2func (name &optional no-mode)
"Return function name for object with NAME.
If NO-MODE is non-nil, ignore mode specific versions."
(let ((name (or (and (symbolp name) (symbol-name name))
name)))
(or (and (not no-mode)
(intern-soft (format "objed-%s-%s-object" name major-mode)))
(intern-soft (format "objed-%s-object" name))
(error "Object unknown: %s" name))))
(defun objed--object (query &optional obj no-mode)
"Call current object function with QUERY.
OBJ defaults to variable `objed--object'. If NO-MODE is non-nil ignore mode
specific versions of object."
(let* ((obj (or obj objed--object))
(objf (objed--name2func obj no-mode))
;; for calling the object func
(inhibit-message t)
(overriding-terminal-local-map nil)
(res (objed--handle-query query objf)))
(if (keywordp res)
;; basic inheritence...
(objed--object res obj t)
res)))
(defun objed--handle-query (query objf)
"Hand query QUERY for object function OBJF."
(cond ((memq query '(:try-next :try-prev))
(condition-case nil
(funcall objf query)
((search-failed end-of-buffer beginning-of-buffer scan-error)
(error "No %s %s found"
(if (eq query :try-next) "next" "previous")
objed--object))))
(t
(funcall objf query))))
(defun objed--next-visible-point (&optional pos)
"Get next visible position.
Should start from an invisible position POS. POS defaults to
point."
(next-single-char-property-change
(or pos (point)) 'invisible))
(defun objed--prev-visible-point (&optional pos)
"Get previous visible position.
Should start from an invisible position POS. POS defaults to
point."
(previous-single-char-property-change
(or pos (point)) 'invisible))
(defun objed--invisible-p (&optional pos)
"Check if point is at an invisible position.
Position POS defaults to point."
(cl-dolist (ol (overlays-at (or pos (point))))
;; see, reveal.el
;; overlay alive
(when (overlay-start ol)
(let ((inv (overlay-get ol 'invisible)))
(when (and inv
;; make sure it's actually invisible
(consp buffer-invisibility-spec)
(cdr (assq inv buffer-invisibility-spec)))
(cl-return t))))))
;; * Public access functions for objects
(defun objed-bounds-at-point (obj &optional state)
"Return beg and end position of object at point.
The positions are returned as a cons: (beg . end). OBJ is a
symbol of a known object. STATE is either `whole' or `inner' and
defaults to `whole'.
Does return nil when there is no such object at point."
(let ((o (objed--object-at-point obj state)))
(when o
(cons (objed--beg o)
(objed--end o)))))
(defun objed-bounds-at (pos obj &optional state)
"Return beg and end position of object at POS.
The positions are returned as a cons: (beg . end). OBJ is a
symbol of a known object. STATE is either `whole' or `inner' and
defaults to `whole'.
Does return nil when there is no such object at point."
(save-excursion
(goto-char pos)
(objed-bounds-at-point obj state)))
;; * Object creation/manipulation
(defun objed-make-empty-object (&optional pos)
"Return an empty object at POS which default to point."
(let ((pos (or pos (point))))
(list (list pos pos)
(list pos pos))))
(cl-defun objed-make-object (&key obounds beg end ibounds ibeg iend)
"Helper to create internal used object format from positions.
Positions of the whole object can be provided by BEG, END or a
cons cell OBOUNDS.
The positions of the inner part can be provided by IBEG, IEND or
a cons cell IBOUNDS. If inner positions are omitted
`objed--inner-default' is used to determine them."
(cl-assert (and (not (and obounds beg end))
(not (and ibounds ibeg iend))))
;; return nil
(when (or obounds
(and beg end
(not (= beg end))))
(cond ((and (integer-or-marker-p beg)
(integer-or-marker-p end)
(integer-or-marker-p ibeg)
(integer-or-marker-p iend))
(list (list (objed--pos-or-marker beg)
(objed--pos-or-marker end))
(list (objed--pos-or-marker ibeg)
(objed--pos-or-marker iend))))
((and (integer-or-marker-p beg)
(integer-or-marker-p end))
(cond ((consp ibounds)
(list (list (objed--pos-or-marker beg)
(objed--pos-or-marker end))
(list (objed--pos-or-marker (car ibounds))
(objed--pos-or-marker (cdr ibounds)))))
((or (functionp ibeg)
(functionp iend))
(list (list (objed--pos-or-marker beg)
(objed--pos-or-marker end))
(list (objed--pos-or-marker
(or (and (functionp ibeg)
(funcall ibeg beg))
ibeg))
(objed--pos-or-marker
(or (and (functionp iend)
(funcall iend end))
iend)))))
(t
(list (list (objed--pos-or-marker beg)
(objed--pos-or-marker end))
(objed--inner-default beg end)))))
((consp obounds)
(cond ((consp ibounds)
(list (list (objed--pos-or-marker (car obounds))
(objed--pos-or-marker (cdr obounds)))
(list (objed--pos-or-marker (car ibounds))
(objed--pos-or-marker (cdr ibounds)))))
((and (integer-or-marker-p ibeg)
(integer-or-marker-p iend))
(list (list (objed--pos-or-marker (car obounds))
(objed--pos-or-marker (cdr obounds)))
(list (objed--pos-or-marker ibeg)
(objed--pos-or-marker iend))))
((or (functionp ibeg)
(functionp iend))
(list (list (objed--pos-or-marker (car obounds))
(objed--pos-or-marker (cdr obounds)))
(list (objed--pos-or-marker
(or (and (functionp ibeg)
(funcall ibeg beg))
ibeg))
(objed--pos-or-marker
(or (and (functionp iend)
(funcall iend end))
iend)))))
(t
(list (list (objed--pos-or-marker (car obounds))
(objed--pos-or-marker (cdr obounds)))
(objed--inner-default (car obounds) (cdr obounds)))))))))
(cl-defun objed--change-to (&key beg end ibeg iend)
"Change position data of current object.
BEG: the beginning position
END: the end position
IBEG: the beginning position of the inner part
IEND: the end position of the inner part"
(let ((beg (and beg (objed--pos-or-marker beg)))
(end (and end (objed--pos-or-marker end)))
(ibeg (and ibeg (objed--pos-or-marker ibeg)))
(iend (and iend (objed--pos-or-marker iend))))
(cond ((eq objed--obj-state 'whole)
(when beg
(setf (car (car objed--current-obj)) beg))
(when end
(setf (car (cdar objed--current-obj)) end))
(when ibeg
(setf (car (cadr objed--current-obj)) ibeg))
(when iend
(setf (cadr (cadr objed--current-obj)) iend)))
((eq objed--obj-state 'inner)
(when ibeg
(setf (car (car objed--current-obj)) ibeg))
(when iend
(setf (car (cdar objed--current-obj)) iend))
(when beg
(setf (car (cadr objed--current-obj)) beg))
(when end
(setf (cadr (cadr objed--current-obj)) end)))
(t
(error "No valid `objed--obj-state'")))))
;; * Helpers to work with object format
(defun objed--update-current-object (&optional range)
"Update positions of current object.
Update `objed--current-obj' to RANGE which defaults to object at
point. If RANGE is a single item list only update the head of
current object position data."
(cond ((null range)
(unless objed--object
(setq objed--object 'char))
;; get current object at point
(setq objed--current-obj (objed--get)))
((and (consp range)
(not (consp (cdr range))))
(error "Wrong format for object data"))
(t
(setq objed--current-obj range))))
(defun objed--markify-current-object ()
"Convert current object into marker object."
(unless (markerp (objed--beg))
(objed--update-current-object
(objed-make-object
:ibeg (set-marker (make-marker)
(objed--ibeg))
:beg (set-marker (make-marker)
(objed--obeg))
:iend (set-marker (make-marker)
(objed--iend))
:end (set-marker (make-marker)
(objed--oend))))))
(defun objed--switch-to (o &optional state odata)
"Switch to object O.
STATE is the state for the object and defaults to whole. If ODATA
is non-nil it is used as object position data, otherwise
calculate the data of the object at current position using
`objed--get'."
(let ((odata (let* ((objed--object o)
(objed--obj-state (or state 'whole))
(tryb t))
(or odata
;; FIXME: all default objects should throw an error
;; if try-next, try-prev fails.
(condition-case nil
(or (objed--get)
(setq tryb nil)
(objed--get t))
(error
(when tryb
(ignore-errors (objed--get t)))))))))
(if odata
(setq objed--object o
objed--obj-state (or state 'whole)
objed--current-obj odata)
(prog1 nil
(message "No %s found." o)))))
(defun objed--get-next (&optional from)
"Get next object from position or object.
If FROM is a position search from there otherwise search starts
from end of object FROM."
(let ((obj (or from objed--current-obj)))
(save-excursion
(when (and obj (not (objed--no-skipper-p)))
(if (integer-or-marker-p obj)
(goto-char obj)
(goto-char (objed--max obj))))
(unless (eobp)
(when (and (not (number-or-marker-p from))
(<= (point) (objed--beg obj)))
(objed--skip-ws))
(ignore-errors
(objed--object :try-next)
(objed--get))))))
(defun objed--get-prev (&optional from)
"Get previous object from position or object.
If FROM is a position search from there otherwise search starts
from beginning of object FROM."
(let ((obj (or from objed--current-obj)))
(save-excursion
(when obj
(if (integer-or-marker-p obj)
(goto-char obj)
(goto-char (objed--min obj))))
(unless (bobp)
(ignore-errors
(objed--object :try-prev)
(objed--get t))))))
(defun objed--distant-p (o)
"Determine if point is outside object O."
(and o
(not (<= (objed--min o)
(point) (objed--max o)))))
(defun objed--do-all (f)
"Apply function F to all accessible objects.
F recieves object range as its argument which is determined
according to `objed--obj-state'."
(let ((obj nil)
(sobj nil)
(inhibit-message t)
(n 1))
;; current one
(funcall f objed--current-obj)
;; backward
(goto-char (objed--min))
(setq sobj objed--current-obj)
(while (and (> (point) (point-min))
(setq obj (objed--get-prev (point)))
(not (equal obj sobj)))
(goto-char (objed--min obj))
(funcall f obj)
(cl-incf n)
(setq sobj obj)
(goto-char (objed--min obj)))
;; FIXME
(when objed--marked-ovs
(setq objed--marked-ovs
(nreverse objed--marked-ovs)))
;; forward
(goto-char (objed--max))
(setq sobj objed--current-obj)
(while (and (< (point) (point-max))
(setq obj (objed--get-next (point)))
(not (equal obj sobj)))
(funcall f obj)
(cl-incf n)
(setq sobj obj)
(goto-char (objed--max obj)))
n))
(defun objed--in-p (c &optional inner)
"Return object name point is in.
C is a list of object names to test for. If INNER is given check
object data for inner state."
(let ((o nil))
(cl-dolist (cand c)
(when (and (setq o (objed--object :get-obj cand))
(if inner (setq o (nreverse o)) o)
(<= (objed--beg o)
(point)
(objed--end o)))
(cl-return cand)))))
(defun objed--at-p (c)
"Return object name at point.
C is a list of object names to test for."
(cl-dolist (cand c)
(when (objed--object :atp cand)
(cl-return cand))))
(defun objed--indentation-position ()
"Get buffer position of indentation on current line."
(save-excursion
(back-to-indentation)
(point)))
(defun objed--apply (func &optional obj)
"Apply function FUNC on postions of object.
OBJ defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(when obj
(apply func
(append (car obj)
(cadr obj))))))
(defun objed--merge (obj1 obj2)
"Merge objects OBJ1 and OBJ2 to build a new object."
(objed-make-object :beg (objed--min obj1)
:end (objed--max obj2)))
(defun objed--reverse ()
"Exchange current objects head and tail."
(if (objed--inner-p)
(setq objed--obj-state 'whole)
(setq objed--obj-state 'inner))
(objed--update-current-object
(nreverse objed--current-obj)))
(defun objed--inner-p ()
"Return non-nil if current objects state is inner."
(eq objed--obj-state 'inner))
(defun objed--goto-next (&optional arg)
"Move to the next object.
With postitive prefix argument ARG move to the nth next object."
(let ((arg (or arg 1))
(obj nil))
(dotimes (_ arg obj)
(when (setq obj (objed--get-next))
(objed--update-current-object obj)
(objed--goto-char (objed--beg obj))))))
(defun objed--goto-previous (&optional arg)
"Move to the previous object.
With postitive prefix argument ARG move to the nth previous
object."
(let ((arg (or arg 1))
(obj nil))
(dotimes (_ arg obj)
(when (setq obj (objed--get-prev))
(objed--update-current-object obj)
(objed--goto-char (objed--beg obj))))))
(defun objed--make-object-overlay (&optional obj)
"Create an overlay to mark current object.
OBJ is the object to use and defaults to `objed--current-obj'."
(let ((obj (or obj objed--current-obj)))
(objed--make-mark-overlay (objed--beg obj)
(objed--end obj))))
(defun objed--make-mark-overlay (beg end &optional face keep)
"Make an objed overaly over region between BEG, END.
Uses FACE `objed-mark' by default. If KEEP is non-nil keep
overlays without content."
(let ((ov (make-overlay beg end)))
(overlay-put ov 'objed t)
(overlay-put ov 'objed--object objed--object)
(overlay-put ov 'face (or face 'objed-mark))
(overlay-put ov 'evaporate (not keep))
(overlay-put ov 'rear-nonsticky t)
ov))
(defun objed--mark (beg end)
"Mark region between BEG and END."
(push (objed--make-mark-overlay beg end)
objed--marked-ovs))
(defun objed--mark-object (&optional obj append)
"Mark current object.
If OBJ is given use it instead `objed--current-obj' If APPEND is
non-nil append to the list of marked objects."
(let ((obj (or obj objed--current-obj)))
(if append
(setq objed--marked-ovs
(append objed--marked-ovs
(list (objed--make-object-overlay obj))))
(push (objed--make-object-overlay obj)
objed--marked-ovs))))
(defun objed-unmark-all ()
"Remove all marked objects."
(interactive)
(while objed--marked-ovs
(delete-overlay (pop objed--marked-ovs))))
(defun objed--mark-ovps (ovsps)
"Mark objects using position data OVSPS."
(dolist (el ovsps)
(objed--mark (car el) (cdr el))))
(defun objed--toggle-mark (&optional obj append)
"Unmark/Mark object.
If OBJ is given use it instead `objed--current-obj' If APPEND is
non-nil append to list of marked objects when marking."
(let ((obj (or obj objed--current-obj)))
(unless (objed--unmark-object obj)
(objed--mark-object obj append))))
(defun objed--unmark-object (&optional obj)
"Unmark objects within current object.
If OBJ is given use it instead `objed--current-obj'."
(let ((obj (or obj objed--current-obj))
(unmarked nil))
(cl-dolist (ov (overlays-in (objed--beg obj)
(objed--end obj))
unmarked)
(when (overlay-get ov 'objed)
(setq unmarked t)
(setq objed--marked-ovs
(delq ov objed--marked-ovs))
(delete-overlay ov)))
unmarked))
;; * Creating objects
(defvar objed--block-p nil
"Block advices installed by `objed'.")
(defun objed-bounds-from-region-cmd (cmd)
"Return buffer positions of region created by command CMD.
Like for `bounds-of-thing-at-point' the positions are returned as
a cons cell."
(let ((mark-active nil)
(last-command nil)
(inhibit-message t)
(objed--block-p t)
(message-log-max nil)
(current-prefix-arg nil))
(save-mark-and-excursion
(goto-char (line-beginning-position))
(objed--skip-ws)
(funcall-interactively cmd)
(cons (region-beginning) (region-end)))))
(defun objed--inner-default (beg end)
"Return positions for inner range.
BEG and END are the positions of the whole object.
Leading and trailing whitespace is skipped to determine the inner
positions."
(let ((ibeg (objed--skip-forward beg 'ws))
(iend (objed--skip-backward end 'ws)))
(if (<= beg ibeg iend end)
(list (objed--pos-or-marker ibeg)
(objed--pos-or-marker iend))
;; fallback
(list (objed--pos-or-marker (point))
(objed--pos-or-marker (1+ (point)))))))
(defun objed--pos-or-marker (pos)
"Return marker or position POS.
If `multiple-cursors-mode' is non-nil return marker for
position POS, otherwise just return POS."
(if (and (bound-and-true-p multiple-cursors-mode)
(not (markerp pos)))
(set-marker (make-marker) pos)
pos))
;; * Object definition helpers
(defun objed--in-string-p (&optional syn ignore-atp)
"Return non-nil if point is inside or at string.
If SYN is given use it instead of syntax at point.
If IGNORE-ATP is non-nil dont test if point is at a string
only if its withing one."
(let ((syn (or syn (syntax-ppss))))
(if (and (nth 3 syn)
(nth 8 syn))
(nth 8 syn)
(and (not ignore-atp)
(objed--at-string-p)))))
(defun objed--at-string-p ()
"Return non-nil if point is at string."
(let ((syn nil))
(and (not (nth 4 (syntax-ppss)))
(or (and (not (eobp))
(eq ?\" (char-syntax (char-after)))
(save-excursion
(nth 3 (setq syn (syntax-ppss (1+ (point))))))
(nth 8 syn))
(and (not (bobp))
(eq ?\" (char-syntax (char-before)))
(save-excursion
(nth 3 (setq syn (syntax-ppss (1- (point))))))
(nth 8 syn))))))
(defun objed--inner-string (&optional bounds)
"Return inners bounds of string at point.
If BOUNDS is given use it instead of string at point."
(let ((bounds (or bounds (objed--bounds-of-string-at-point))))
(when bounds
(let ((beg (save-excursion (goto-char (car bounds))
(while (eq ?\" (char-syntax (char-after)))
(forward-char 1))
(when (< (point) (cdr bounds))
(point))))
(end (save-excursion (goto-char (cdr bounds))
(while (eq ?\" (char-syntax (char-before)))
(forward-char -1))
(when (> (point) (car bounds))
(point)))))
(when (and beg end)
(cons beg end))))))
(defun objed--in-comment-p (&optional syn)
"Return non-nil when in point is in or at comment.
If SYN is given use it instead of syntax at point."
(let ((syn (or syn (syntax-ppss))))
(or (and (nth 4 syn)
(nth 8 syn))
(objed--at-comment-p))))
(defun objed--at-comment-p ()
"Return non-nil if point is at comment."
(let ((syn nil))
(or (save-excursion
(and (not (eobp))
(char-after)
(eq ?\< (char-syntax (char-after)))
(skip-syntax-forward "<")
(nth 4 (setq syn (syntax-ppss)))
(nth 8 syn)))
(save-excursion
(and (not (bobp))
(char-after)
(eq ?\> (char-syntax (char-after)))
(prog1 t (unless (bolp) (skip-syntax-backward ">")))
(nth 4 (setq syn (syntax-ppss)))
(nth 8 syn))))))
(defun objed--bounds-of-comment-at-point ()
"Return bounds of comment at point."
(let ((beg (objed--in-comment-p)))
(when beg
(cons beg
(save-excursion (goto-char beg)
(forward-comment 1)
(objed--skip-ws t)
(point))))))
(defun objed--bounds-of-string-at-point ()
"Return bounds of string at point."
(let* ((beg (objed--in-string-p))
(end (and beg (save-excursion
(goto-char beg)
(when (ignore-errors (forward-sexp 1) t)
(point))))))
(when (and beg end)
(cons beg
(save-excursion (goto-char beg)
(forward-sexp 1)
(point))))))
(defun objed--comment-block ()
"Return bounds of comment block at point if point in comment."
(let ((start (objed--in-comment-p)))
(when start
(save-excursion
(goto-char start)
(let ((beg (objed--skip-backward (point) nil 'comment))
(end (objed--skip-forward (point) nil 'comment)))
(goto-char end)
(skip-chars-forward " \t\r\n" (line-beginning-position 2))
(cons beg (point)))))))
(defun objed--inner-comment-block ()
"Get range of inner comment."
(let ((bounds (objed--comment-block)))
(cons (objed--skip-forward (car bounds)'ws)
(objed--skip-backward (cdr bounds)'ws))))
(defun objed--skip-ws (&optional back limit)
"Skip whitspace.
Defaults to forward, if BACK is non-nil skip backwards.
Skips until LIMIT."
(if back
(skip-chars-backward " \r\n\t" limit)
(skip-chars-forward " \r\n\t" limit)))
(defun objed--skip-forward (from &optional ws comment)
"Skip whitespace or comment forward from position FROM.
If WS is given skip whitespace. COMMENT non-nil to skip
comments."
(save-excursion
(goto-char from)
(cond (ws
(objed--skip-ws)
(if comment
(objed--skip-forward (point) nil 'comment)
(point)))
(comment
(let ((s (objed--in-comment-p)))
(when s
(goto-char s))
(while (and (not (eobp))
(forward-comment 1)))
(objed--skip-ws t)
(point))))))
(defun objed--skip-backward (from &optional ws comment)
"Skip whitespace or comment backward from position FROM.
If WS is given skip whitespace. COMMENT non-nil to skip
comments."
(save-excursion
(goto-char from)
(cond (ws
(objed--skip-ws t)
(if comment
(objed--skip-backward (point) nil 'comment)
(point)))
(comment
(let ((s (objed--in-comment-p)))
(when s
(goto-char s))
(while (and (not (bobp))
(forward-comment -1)))
(objed--skip-ws)
(point))))))
(defun objed--narrow-if-string-or-comment ()
"In comment or string narrow to them."
(let* ((bounds nil)
(ibounds (cond ((setq bounds (objed--bounds-of-string-at-point))
(objed--inner-string bounds))
((setq bounds (objed--bounds-of-comment-at-point))
;; include trailing ws
(objed--comment-block)))))
(when ibounds
(narrow-to-region (car ibounds) (cdr ibounds)))))
(defun objed--in-string-or-comment-p ()
"Test if point is inside a string or a comment."
(let* ((sp (syntax-ppss))
(begin (nth 8 sp)))
(when (or (eq (char-after begin) ?\")
(nth 4 sp))
begin)))
(defvar align-region-separate)
(defvar align-mode-rules-list)
(defvar align-rules-list)
(defvar align-exclude-rules-list)
(defvar align-mode-exclude-rules-list)
(declare-function align-region "ext:align")
(defun objed--get-align-sections ()
"Get region bounds of align sections."
(require 'align)
(let ((separator
(or (if (and (symbolp align-region-separate)
(boundp align-region-separate))
(symbol-value align-region-separate)
align-region-separate)
'entire))
(regions ()))
(align-region nil nil separator
(or align-mode-rules-list align-rules-list)
(or align-mode-exclude-rules-list align-exclude-rules-list)
(lambda (beg end mode)
(when (consp mode)
(push (cons beg end)
regions))))
regions))
;; * Object definitions
(objed-define-object nil char
:atp
(looking-at ".")
:get-obj
(if (eobp)
(objed-make-empty-object)
(objed-make-object :beg (point)
:end (1+ (point))))
:try-next
;; current one is skipped, for chars this means we are already at
;; the next..
(forward-char 0)
:try-prev
(forward-char -1))
(objed-define-object nil ace
:get-obj
(unless (eq objed--object 'ace-object)
(let ((stripe (and (bound-and-true-p stripe-buffer-mode)
stripe-buffer-mode)))
(when (fboundp 'stripe-buffer-mode)
(stripe-buffer-mode 1))
(unwind-protect
(objed-make-object
:beg (save-excursion
(call-interactively 'avy-goto-line)
;; indicate input
(redisplay)
(line-beginning-position))
:end (save-excursion
(when (fboundp 'stripe-buffer-mode)
(stripe-buffer-mode 1))
(call-interactively 'avy-goto-line)
(1+ (line-end-position))))
(unless (or stripe
(not (fboundp 'stripe-buffer-mode)))
(stripe-buffer-mode -1)))))
:try-next
(user-error "Not possible")
:try-prev
(user-error "Not possible"))
(objed-define-object nil trailing
:atp
(looking-at " ")
:get-obj
(objed-make-object
:beg (point)
:ibeg (point)
:end (progn (goto-char (objed--end))
(objed--skip-ws) (point))
:iend (point))
:try-next
(when (search-forward " " nil t)
(forward-char -1))
:try-prev
(search-backward " " nil t))
(objed-define-object nil leading
:atp
(looking-back " " (1- (point)))
:get-obj
(objed-make-object
:end (point)
:iend (point)
:beg (progn (goto-char (objed--beg))
(objed--skip-ws t) (point))
:ibeg (point))
:try-next
(when (search-forward " " nil t)
(forward-char -1))
:try-prev
(search-backward " " nil t))
(defun objed--inner-word-bounds ()
"Return bounds of subword at point."
(let* ((subword-mode t)
(superword-mode nil)
(find-word-boundary-function-table
subword-find-word-boundary-function-table))
(if (eq this-command 'forward-word)
(save-excursion
(forward-word -1)
(bounds-of-thing-at-point 'word))
(bounds-of-thing-at-point 'word))))
(objed-define-object nil word
:atp
(looking-at "\\<")
:ref
(when (equal (bounds-of-thing-at-point 'word)
(bounds-of-thing-at-point 'symbol))
'identifier)
:get-obj
(if (eobp)
(objed-make-empty-object)
(if (objed--inner-p)
;; don't confuse objed-next/prev which
;; use the outer bounds for navigation
;; but a word can contain multiple innner words
(objed-make-object
:obounds (objed--inner-word-bounds))
(objed-make-object
:obounds (bounds-of-thing-at-point 'word)
:ibounds (objed--inner-word-bounds))))
:try-next
(if (objed--inner-p)
(let* ((subword-mode t)
(superword-mode nil)
(find-word-boundary-function-table
subword-find-word-boundary-function-table))
(forward-word 1)
(forward-word -1))
(re-search-forward "\\<." nil t))
:try-prev
(if (objed--inner-p)
(let* ((subword-mode t)
(superword-mode nil)
(find-word-boundary-function-table
subword-find-word-boundary-function-table))
(forward-word -1))
(re-search-backward ".\\>" nil t)))
(defun objed--next-symbol ()
"Move to next symbol."
(re-search-forward "\\_<" nil t)
(let (syn start)
(while (setq start (nth 8 (setq syn (syntax-ppss))))
(goto-char start)
(when (cond ((nth 3 syn)
(forward-sexp 1) t)
((nth 4 syn)
(forward-comment 1) t))
(re-search-forward "\\_<." nil t)))))
(defun objed--prev-symbol ()
"Move to previous symbol."
(re-search-backward "\\_>" nil t)
(let (start)
(while (setq start (nth 8 (syntax-ppss)))
(goto-char start)
(re-search-backward "\\_>" nil t))))
(objed-define-object nil symbol
:atp
(and (not (objed--in-string-or-comment-p))
(or (looking-at "\\_<")
(looking-back "\\_>" 1)))
:ref 'identifier
:get-obj
(when (not (objed--in-string-or-comment-p))
(bounds-of-thing-at-point 'symbol))
:try-next
(objed--next-symbol)
:try-prev
(objed--prev-symbol))
(objed-define-object nil subword
:get-obj
(objed--inner-word-bounds)
:try-next
(let* ((subword-mode t)
(superword-mode nil)
(find-word-boundary-function-table
subword-find-word-boundary-function-table))
(forward-word 1)
(forward-word -1))
:try-prev
(let* ((subword-mode t)
(superword-mode nil)
(find-word-boundary-function-table
subword-find-word-boundary-function-table))
(forward-word -1)))
(defun objed--at-sexp-p ()
"Return sexp object if point at strutured expression."
(let* ((opos (point))
(objed--block-p t)
(real-this-command 'forward-sexp)
(instring (objed--in-string-p nil t))
(other nil)
(atp (or (when (or (bobp)
;; prevent the annoying "feature" that sexp
;; movement works across strings
(not instring)
(not (eq (char-syntax (char-after)) ?\")))
(save-excursion
(ignore-errors
(forward-sexp 1)
(setq other (point))
(forward-sexp -1)
(= (point) opos))))
(when (or (eobp)
;; prevent the annoying "feature" that sexp
;; movement works across strings
(not instring)
(not (eq (char-syntax (char-before)) ?\")))
(save-excursion
(ignore-errors
(forward-sexp -1)
(setq other (point))
(forward-sexp 1)
(= (point) opos)))))))
(when atp
(cons (min opos other)
(max opos other)))))
(objed-define-object nil sexp
:atp
(objed--at-sexp-p)
:ref
(let ((s (save-excursion
(goto-char (objed--beg))
(skip-syntax-forward ".'")
(bounds-of-thing-at-point 'symbol))))
(when (and s
(or (equal s (cons (objed--beg)
(objed--end)))
(equal s (cons (objed--alt-beg)
(objed--alt-end)))))
(goto-char (car s))
'identifier))
:get-obj
(let ((bounds (or (objed--at-sexp-p)
;; for commands which are not symetric
;; like C-M-f at beg of python funtions
(save-excursion
(ignore-errors
(let* ((pos (point))
(real-this-command 'forward-sexp))
(forward-sexp 1)
(when (/= pos (point))
(cons pos
(point)))))))))
(when bounds
(objed-make-object
:obounds bounds
:ibeg (1+ (car bounds))
:iend (1- (cdr bounds)))))
:try-next
(or (ignore-errors
(forward-sexp 1)
(forward-sexp -1) t)
(ignore-errors
(up-list 1)
t)
(ignore-errors
(forward-word 1)
(forward-sexp -1)
t))
:try-prev
(or (ignore-errors
(forward-sexp -1) t)
(ignore-errors
(up-list -1)
t)
(ignore-errors
(forward-word -1)
t)))
(objed-define-object nil file
:atp
(looking-at "/\\|\\\\")
:get-obj
(let* ((bounds (bounds-of-thing-at-point 'filename))
(file (and bounds (buffer-substring (car bounds) (cdr bounds)))))
(when (and file (string-match (rx (or (and bos (or "/" "\\"))
(and "." (* alnum) eos)))
file))
(objed-make-object :obounds bounds
:ibounds
(let ((ifile (or (file-name-directory file)
(file-name-sans-extension file))))
(when ifile
(goto-char (car bounds))
(search-forward ifile)
(cons (match-beginning 0) (match-end 0)))))))
:try-next
(re-search-forward (rx (or (or "/" "\\")
(and "." (* alnum))))
nil t)
:try-prev
(re-search-backward (rx (or (or "/" "\\")
(and "." (* alnum))))
nil t))
(objed-define-object nil email
:get-obj
(bounds-of-thing-at-point 'email)
:try-next
(re-search-forward "[a-z0-9!#$%&'*+/=?^_`{|}~-]@[a-z0-9]")
:try-prev
(re-search-backward "[a-z0-9!#$%&'*+/=?^_`{|}~-]@[a-z0-9]"))
(objed-define-object nil url
:get-obj
(let ((bounds (bounds-of-thing-at-point 'url)))
(when bounds
(objed-make-object :obounds bounds
:ibounds
(progn
(goto-char (car bounds))
(re-search-forward "https?://" nil t)
(cons (point)
(if (search-forward "/" (cdr bounds) t)
(1- (point))
(cdr bounds)))))))
:try-next
(re-search-forward "http")
:try-prev
(re-search-backward "http"))
(objed-define-object nil page
:atp
(looking-at page-delimiter)
:get-obj
(bounds-of-thing-at-point 'page)
:try-next
(forward-line 1)
:try-prev
(forward-line -1))
(objed-define-object nil defun
:get-obj
(objed-bounds-from-region-cmd #'mark-defun)
:try-next
(beginning-of-defun -1)
:try-prev
(beginning-of-defun 1))
(objed-define-object nil line
:atp
(or (looking-at "^")
(looking-back "^ *" (line-beginning-position)))
:get-obj
(if (eobp)
(objed-make-empty-object)
(objed-make-object :beg (line-beginning-position)
:end (save-excursion
;; include hidden parts...
(end-of-visible-line)
(if (eobp)
(point)
(1+ (point))))))
:try-next
(skip-chars-forward " \t\r\n")
:try-prev
(skip-chars-backward " \t\r\n"))
(objed-define-object nil textblock
:max-search-forward
(save-excursion
(objed--with-narrow-for-text
(forward-paragraph 1)
(point)))
:get-obj
(when (or (not (derived-mode-p 'prog-mode))
(derived-mode-p 'text-mode)
(objed--in-comment-p)
(objed--in-string-p))
(objed--with-narrow-for-text
(let ((bounds (objed--get-textblock-bounds)))
(when bounds
(objed-make-object :obounds bounds)))))
:try-next
(objed--with-narrow-for-text
(forward-sentence 1))
:try-prev
(objed--with-narrow-for-text
(forward-sentence -1)))
(defun objed--column (pos)
"Get column at position POS."
(save-excursion
(goto-char pos)
(current-column)))
(defun objed--get-indent-bounds (&optional inner block)
"Get range of indentation block.
If INNER is non-nil get the range for inner state. If BLOCK is
non-nil the indentation block can contain empty lines."
(let* ((oi (objed--indentation-position))
(ic (objed--column oi))
(pos nil)
(opos nil)
(beg (save-excursion
(while (and (not (bobp))
(progn
(forward-line -1)
(or (and block (looking-at "^ *$"))
(and (= (objed--column (setq pos (objed--indentation-position)))
ic)
(or (not inner) (not (looking-at " *$")))
;; dont accept empty
(or (not (eolp)) (not (bolp)))
(setq opos pos))))))
(prog1 (or opos oi)
(setq opos nil))))
(end (save-excursion
(save-excursion
(while (and (not (eobp))
(progn
(forward-line 1)
(or (and block (looking-at "^ *$"))
(and (= (objed--column (setq pos (objed--indentation-position)))
ic)
(or (not inner) (not (looking-at " *$")))
(or (not (eolp)) (not (bolp)))
(setq opos pos)))))))
(when opos
(goto-char opos))
(line-end-position))))
(when (and beg end)
(save-excursion
(goto-char beg)
(cons (line-beginning-position)
end)))))
(objed-define-object nil indent
:get-obj
(let ((bounds (objed--get-indent-bounds 'inner)))
(when bounds
(objed-make-object
:obounds
(cons (car bounds)
(if (eq (cdr bounds)
(point-max))
(cdr bounds)
(1+ (cdr bounds)))))))
:try-next
;;(error "No next indent")
(objed--skip-ws)
:try-prev
(objed--skip-ws t))
(objed-define-object nil block
:get-obj
(let ((bounds (objed--get-indent-bounds nil t)))
(when bounds
(objed-make-object
:obounds
(cons (car bounds)
(if (eq (cdr bounds)
(point-max))
(cdr bounds)
(1+ (cdr bounds)))))))
:try-next
(objed--skip-ws)
:try-prev
(objed--skip-ws t))
(objed-define-object nil sentence
:atp
(when (or (derived-mode-p 'text-mode)
(eq major-mode 'fundamental-mode)
(objed--at-comment-p)
(objed--in-string-or-comment-p))
(let ((ip (ignore-errors
(save-excursion
(forward-sentence)
(backward-sentence)
(point)))))
(and ip (= (point) ip))))
:max-search-forward
(save-excursion
(objed--with-narrow-for-text
(forward-paragraph 1)
(point)))
:get-obj
(when (or (derived-mode-p 'text-mode)
(eq major-mode 'fundamental-mode)
(objed--at-comment-p)
(objed--in-string-or-comment-p))
(objed--with-narrow-for-text
(let ((s (bounds-of-thing-at-point 'sentence)))
(when s
(goto-char (car s))
(objed-make-object
:beg (if (derived-mode-p 'text-mode)
(car s)
(while (or (< 0 (skip-syntax-forward "<"))
(< 0 (skip-chars-forward "[:space:]\n"))))
(point))
:ibeg (point)
:end (progn
(goto-char (cdr s))
;; include trailing space
(skip-chars-forward
"\s-"
(+ (point)
(if sentence-end-double-space
2
1)))
(point))
:iend (with-syntax-table text-mode-syntax-table
(skip-syntax-backward ".-" (car s))
(point)))))))
:try-next
(objed--with-narrow-for-text
;; call twice if at start of sentence.
(unless (>= (point)
(save-excursion (backward-sentence)
(forward-sentence)
(point)))
(forward-sentence 1))
(forward-sentence 1)
;; move to start
(forward-sentence -1))
:try-prev
(objed--with-narrow-for-text
;; call twice at end of sentence
(unless (<= (point)
(save-excursion (forward-sentence)
(backward-sentence)
(point)))
(backward-sentence 1))
(backward-sentence 1)))
(objed-define-object nil paragraph
:atp
(and (or (bobp)
(save-excursion (forward-line -1)
(looking-at "^ *$")))
(looking-back "^ *" (line-beginning-position)))
:get-obj
(objed-bounds-from-region-cmd #'mark-paragraph)
:try-next
(skip-chars-forward " \t\r\n")
:try-prev
(skip-chars-backward " \t\r\n"))
(defun objed--bounds-within-comment-or-string-p (bounds)
"Return non-nil if BOUNDS are inside comment or string."
(let ((cbounds (cond ((objed--in-string-p)
(objed--bounds-of-string-at-point))
((objed--in-comment-p)
(objed--comment-block)))))
(when cbounds
(objed--bounds-within-bounds-p bounds cbounds))))
(defun objed--bounds-within-bounds-p (b c)
"Return non-nil if bounds B are inside C."
(and (>= (car b) (car c))
(<= (cdr b) (cdr c))))
(defun objed--get-textblock-bounds ()
"Get cons cell of beginnine and end position of textblock."
(save-excursion
(let ((po nil)
(opos (point)))
(cl-letf (((symbol-function 'fill-region-as-paragraph)
(lambda (beg end &rest _)
(setq po (cons beg end))
(throw 'done t))))
;; let inner not move point in general?
(catch 'done
(fill-paragraph))
(when (consp po)
(goto-char (car po))
(objed--skip-ws)
(when (or (<= (point) opos (cdr po))
(<= (cdr po) opos (point)))
(cons (point) (cdr po))))))))
(objed-define-object nil buffer
:atp
(bobp)
:get-obj
(objed-bounds-from-region-cmd #'mark-whole-buffer))
(objed-define-object nil region
:get-obj
(objed-make-object
:beg (mark)
:end (point)))
(objed-define-object nil bracket
:atp
(unless (objed--in-string-or-comment-p)
(or (looking-at "(\\|\\[\\|{")
(looking-back ")\\|\\]\\|}" 1)))
:get-obj
(unless (objed--in-string-or-comment-p)
(cond ((and (not (bobp))
(eq (char-syntax (char-before)) ?\)))
(let ((end (point))
(beg (scan-sexps (point) -1)))
(objed-make-object :beg beg
:end end
:ibeg #'1+
:iend #'1-)))
((and (not (eobp))
(eq (char-syntax (char-after)) ?\())
(let ((beg (point))
(end (scan-sexps (point) 1)))
(objed-make-object :beg beg
:end end
:ibeg #'1+
:iend #'1-)))
(t
;; get bracket expression point is in
(let* ((beg (cadr (syntax-ppss)))
(end (when beg (scan-sexps beg 1))))
(objed-make-object :beg beg
:end end
:ibeg #'1+
:iend #'1-)))))
:try-next
(when (re-search-forward "(\\|\\[\\|{" nil t)
(forward-char -1))
:try-prev
(when (re-search-backward ")\\|\\]\\|}" nil t)
(forward-char 1)))
(objed-define-object nil string
:atp
(objed--at-string-p)
:get-obj
(let ((bounds (objed--bounds-of-string-at-point)))
(objed-make-object
:obounds bounds
:ibounds (and bounds (objed--inner-string bounds))))
:try-next
(let ((cont t)
(syn nil))
(while cont
(while (and (not (eobp))
(not (eq ?\" (char-syntax (char-after)))))
(forward-char 1))
(if (not (nth 4 (setq syn (syntax-ppss))))
(setq cont nil)
(goto-char (nth 8 syn))
(forward-comment 1))))
:try-prev
(let ((cont t)
(syn nil))
(while cont
(while (and (not (bobp))
(not (eq ?\" (char-syntax (char-before)))))
(forward-char -1))
(if (not (nth 4 (setq syn (syntax-ppss))))
(setq cont nil)
(goto-char (nth 8 syn))))))
(objed-define-object nil comment
:atp
(objed--at-comment-p)
:get-obj
(let ((bounds (objed--comment-block)))
(objed-make-object :obounds bounds
;; TODO: support multi char/multi line comments
;; act different for them?
:ibounds (when bounds
(goto-char (car bounds))
(skip-chars-forward
(format "%s \t" (or comment-start "")))
(cons (point)
(objed--skip-backward (cdr bounds) 'ws)))))
:try-next
(comment-search-forward (point-max) t)
:try-prev
(comment-search-backward (point-min) t))
;; TODO: get rid of this
(defvar objed--content-bounds nil
"Holding bounds of current object for `objed-reference'.")
(defun objed--next-content ()
"Search and set for next content object."
(let ((curr (apply 'buffer-substring (objed--current))))
(when objed--content-bounds
(goto-char (cdr objed--content-bounds)))
(if (search-forward curr nil t)
(setq objed--content-bounds
(cons (match-beginning 0) (match-end 0)))
(setq objed--content-bounds nil))))
(defun objed--prev-content ()
"Search and set for prev content object."
(let ((curr (apply 'buffer-substring (objed--current))))
(when objed--content-bounds
(goto-char (car objed--content-bounds)))
(if (search-backward curr nil t)
(setq objed--content-bounds
(cons (match-beginning 0) (match-end 0)))
(setq objed--content-bounds nil))))
;; special objects, for fallback of objed-reference
(objed-define-object nil content
:get-obj
;; has to be set by commands using this
(objed-make-object
:obounds
(or objed--content-bounds
(error "Last in current direction")))
:try-next
(objed--next-content)
:try-prev
(objed--prev-content))
(objed-define-object nil identifier
:atp (or (looking-at "\\_<")
(looking-back "\\_>" 1))
:get-obj
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(when bounds
(objed-make-object
:obounds bounds
:ibounds (save-excursion
;; use the symbol prefix by default
(goto-char (car bounds))
(objed--inner-word-bounds)))))
:try-next
(objed--next-identifier)
:try-prev
(objed--prev-identifier))
(defun objed--next-identifier ()
"Move to next identifier."
(let ((bds nil))
(if (not (setq bds (bounds-of-thing-at-point 'symbol)))
(re-search-forward "\\_<" nil t)
(let ((sym (and (or bds (setq bds (bounds-of-thing-at-point 'symbol)))
(buffer-substring (car bds) (cdr bds)))))
(when bds
(goto-char (cdr bds)))
(if (re-search-forward (format "\\_<%s\\_>"
(regexp-quote sym)) nil t)
(goto-char (match-beginning 0))
(goto-char (car bds))
(when (or (eq real-this-command #'objed-next-identifier)
(eq real-this-command #'objed-goto-next-identifier))
(objed-first-identifier)
(objed--flash-object 'match)))))))
(defun objed--flash-object (face &optional time)
"Flash current object using FACE for TIME (defaults to 0.4)."
(let ((cookie (face-remap-add-relative
'objed-hl face)))
(run-at-time (or time .3) nil
(lambda ()
(face-remap-remove-relative cookie)))))
(defun objed--prev-identifier ()
"Move to previous identifier."
(let ((bds nil))
(if (not (setq bds (bounds-of-thing-at-point 'symbol)))
(re-search-backward "\\_<" nil t)
(let ((sym (and (or bds (setq bds (bounds-of-thing-at-point 'symbol)))
(buffer-substring (car bds) (cdr bds)))))
(when bds
(when (looking-back "\\_>" 1)
(goto-char (car bds)))
(if (re-search-backward (format "\\_<%s\\_>"
(regexp-quote sym)) nil t)
(goto-char (match-beginning 0))
(goto-char (car bds))
(when (or (eq real-this-command #'objed-prev-identifier)
(eq real-this-command #'objed-goto-prev-identifier))
(objed-last-identifier)
(objed--flash-object 'match))))))))
(objed-define-object nil section
:atp
(and (bound-and-true-p outline-minor-mode)
(bolp) (not (string= "" outline-regexp))
(eq (aref comment-start 0) (char-after))
(looking-at (format "^%s" outline-regexp)))
:get-obj
(when (and (bound-and-true-p outline-minor-mode)
(ignore-errors (outline-back-to-heading) t))
(objed-make-object :beg (point)
:ibeg (line-end-position)
:end (progn
(outline-next-heading)
(or (eobp)
(move-end-of-line 0))
(point))
:iend (progn (skip-chars-backward " \t\r\n")
(point))))
:try-next
(when (bound-and-true-p outline-minor-mode)
(outline-next-heading))
:try-prev
(when (bound-and-true-p outline-minor-mode)
(outline-previous-heading)))
;; * Mode specific objects
(objed-define-object elisp-mode defun
:mode emacs-lisp-mode
:atp
(looking-at "^(")
:ref
(down-list 1)
(forward-symbol 2)
(forward-symbol -1)
'identifier
:try-next
;; (beginning-of-defun -1) does not work for adjacent toplevel
;; parens in lisp becaus try next is called after moving beyond the
(end-of-defun 1)
(beginning-of-defun 1)
:get-obj
(let ((bounds (objed-bounds-from-region-cmd #'mark-defun)))
(when bounds
(objed-make-object
:obounds bounds
:ibounds
(save-restriction
(narrow-to-region (car bounds) (cdr bounds))
(goto-char (car bounds))
(objed--skip-ws)
(ignore-errors
(cond ((looking-at "(defun")
(down-list 2)
(up-list 1)
(objed--skip-ws)
(when (objed--at-string-p)
(forward-sexp 1)
(objed--skip-ws))
(cons (point)
(progn (goto-char (point-max))
(down-list -1)
(point))))
(t
(cons (progn (down-list 1)
(point))
(progn (goto-char (point-max))
(down-list -1)
(point)))))))))))
(objed-define-object css-mode defun
:mode css-mode
:atp
(looking-at "^[^[:space:]]")
:try-next
(search-forward "{")
:try-prev
(search-backward "{")
:get-obj
(unless (objed--in-comment-p)
(let* ((pos (point))
(end (and (search-forward "}" nil t) (point)))
(beg (and end
(search-backward "{" nil t)
(or (and (re-search-backward "^ *$" nil t)
(1+ (match-end 0)))
(and (re-search-backward "^" nil t)
(line-beginning-position))))))
(when (and beg end
(<= beg pos end))
(objed-make-object
:beg beg
:ibeg (search-forward "{")
:end end
:iend (1- end))))))
(objed-define-object nil tag
:atp
(and (derived-mode-p 'sgml-mode)
(or (looking-at "<")
(looking-back ">" 1)))
:get-obj
;; TODO: fix sgml-backward not working
(when (derived-mode-p 'sgml-mode)
;; like with bracket detect at boundary
(objed-make-object
:beg (progn (unless (looking-at "<")
(sgml-skip-tag-backward 1))
(point))
:ibeg (save-excursion
(when (search-forward ">" nil t)
(point)))
:end (progn
(sgml-skip-tag-forward 1)
(point))
:iend (progn (when (search-backward "<" nil t)
(point)))))
:try-next
(search-forward "<" nil t)
:try-prev
(search-backward ">" nil t))
(defvar hl-line-overlay)
(defun objed--what-face (&optional pos)
"Return face at POS."
(let* ((pos (or pos (point)))
(ov (car (overlays-at (point) t)))
(face (or (and ov
;; exclude hl line
(not (eq hl-line-overlay ov))
(overlay-get ov 'face))
(get-char-property pos 'read-face-name)
(get-text-property pos 'face))))
(unless (keywordp (car-safe face)) (list face))))
(defvar objed--last-face nil)
;; from `evil-textobj-syntax'
(defun objed--get-face-range ()
"Return range of equal face before/after point."
(let ((point-face (objed--what-face))
(objed-cmd-alist (and (bound-and-true-p objed-cmd-alist)
objed-cmd-alist))
(backward-none-space-point (point)) ; last none white space char
(forward-none-space-point (point)) ; last none white space char
(start (point))
(end (point)))
(when (or (eq real-this-command #'objed-face-object)
(eq 'face (cdr (assq real-this-command objed-cmd-alist))))
(setq objed--last-face point-face))
(when (equal objed--last-face
(objed--what-face))
;; check chars backward,
;; stop when char is not white space and has different face
(save-excursion
(let ((continue t))
(while (and continue (>= (- (point) 1) (point-min)))
(backward-char)
(let ((backward-point-face (objed--what-face)))
(if (= 32 (char-syntax (char-after)))
(ignore)
(if (equal point-face backward-point-face)
(setq backward-none-space-point (point))
(setq continue nil)))))))
;; check chars forward,
;; stop when char is not white space and has different face
(save-excursion
(let ((continue t))
(while (and continue (< (+ (point) 1) (point-max)))
(forward-char)
(let ((forward-point-face (objed--what-face)))
(if (= 32 (char-syntax (char-after)))
(ignore)
(if (equal point-face forward-point-face)
(setq forward-none-space-point (point))
(setq continue nil)))))))
(progn (setq start backward-none-space-point)
(setq end forward-none-space-point)
(cons start (+ end 1))))))
(objed-define-object nil face
:get-obj
(objed--get-face-range)
;; TODO: search for next same face as current...
:try-next
(while (not (equal objed--last-face
(objed--what-face)))
(forward-char 1))
:try-prev
;; get out of current
(forward-char -1)
(while (not (equal objed--last-face
(objed--what-face)))
(forward-char -1)))
(defvar flycheck-mode)
(defvar flymake-mode)
(defvar flymake-wrap-around)
(declare-function flycheck-overlays-at "ext:flycheck")
(declare-function flycheck-next-error "ext:flycheck")
(declare-function flycheck-previous-error "ext:flycheck")
(declare-function flymake--overlays "ext:flymake")
(declare-function flymake-goto-next-error "ext:flymake")
(declare-function flymake-goto-prev-error "ext:flymake")
(defun objed--get-error-bounds ()
"Return linter error at point."
(cond ((bound-and-true-p flycheck-mode)
(let ((ov (car (flycheck-overlays-at (point)))))
(when ov
(cons (overlay-start ov)
(overlay-end ov)))))
(flymake-mode
(let ((ov (car (flymake--overlays :beg (point)))))
(when ov
(run-at-time 0 nil
#'message
(funcall (overlay-get ov 'help-echo)
(selected-window) ov (point)))
(cons (overlay-start ov)
(overlay-end ov)))))))
(defun objed--next-error ()
"Goto next linter error."
(cond ((bound-and-true-p flycheck-mode)
(flycheck-next-error))
(flymake-mode
(let ((flymake-wrap-around nil))
(flymake-goto-next-error 1)))))
(defun objed--previous-error ()
"Goto previous linter error."
(cond ((bound-and-true-p flycheck-mode)
(flycheck-previous-error))
(flymake-mode
(let ((flymake-wrap-around nil))
(flymake-goto-prev-error 1)))))
(objed-define-object nil error
:get-obj
(objed--get-error-bounds)
:try-next
(objed--next-error)
:try-prev
(objed--previous-error))
(declare-function org-mark-element "ext:org")
(objed-define-object org section
:mode org-mode
:atp
(org-at-heading-p)
:get-obj
(when (ignore-errors (org-back-to-heading) t)
(objed-make-object
:beg (point)
:ibeg (line-end-position)
:end (progn (outline-next-visible-heading 1)
(or (eobp) (move-end-of-line 0))
(point))
:iend (progn (skip-chars-backward " \t\r\n")
(point))))
:try-next
(outline-next-visible-heading 1)
:try-prev
(outline-previous-visible-heading 1))
;; blocks are "elements" in current mode
(objed-define-object org block
:mode org-mode
:get-obj
(let ((bounds (objed-bounds-from-region-cmd #'org-mark-element))
(ibeg nil)
(ibounds nil))
(when bounds
(goto-char (car bounds))
(setq ibounds (cl-dolist (lr '((":\\(PROPERTIES\\|LOGBOOK\\):" . ":END:")
("#\\+BEGIN_SRC.*$" . "#\\+END_SRC")
("#\\+begin_src.*$" . "#\\+end_src")
("^#\\+begin_example.*$" . "#\\+end_example$")
("^#\\+BEGIN_EXAMPLE.*$" . "#\\+END_EXAMPLE$")))
(when (and (re-search-forward (car lr) (cdr bounds) t)
(skip-chars-forward "\r\n \t" (line-beginning-position 2))
(setq ibeg (point))
(progn (goto-char (cdr bounds))
(re-search-backward (cdr lr) (car bounds) t)))
(skip-chars-backward " \t")
(cl-return (cons ibeg (point))))))
(goto-char (cdr bounds))
(skip-chars-backward "\r\n \t")
;; padding
(skip-chars-forward "\r\n" (1+ (1+ (point))))
(setf (cdr bounds) (point))
(objed-make-object :obounds bounds :ibounds ibounds))))
(declare-function org-table-beginning-of-field "ext:org")
(declare-function org-table-end-of-field "ext:org")
(objed-define-object org field
:beg (if (looking-back "| ?\\( *\\)" (line-beginning-position))
(match-beginning 1)
(org-table-beginning-of-field 1)
(point))
:end (if (looking-at "\\( *\\) ?|")
(match-end 1)
(org-table-end-of-field 1)
(point))
:try-next
(org-table-end-of-field 1)
:try-prev
(org-table-beginning-of-field 1))
(defvar comint-prompt-regexp)
(declare-function comint-next-prompt "ext:comint")
(declare-function comint-previous-prompt "ext:comint")
(objed-define-object nil output
:atp
(and (derived-mode-p 'comint-mode)
(looking-back comint-prompt-regexp
(save-excursion
(forward-line 0)
(point))))
:get-obj
(when (derived-mode-p 'comint-mode)
(let* ((pos (point))
(bounds (progn
(comint-next-prompt 1)
(move-end-of-line 0)
(if (> (point) pos)
(cons pos (point))
(move-end-of-line 2)
(cons
(line-beginning-position)
(point))))))
(objed-make-object
:obounds bounds
:ibounds (progn (goto-char (car bounds))
(forward-line 1)
(cons (point)
(progn (goto-char (cdr bounds))
(objed--skip-ws t)
(point)))))))
:try-next
(comint-next-prompt 1)
:try-prev
(comint-previous-prompt 1))
(declare-function python-nav-end-of-block "ext:python")
(declare-function python-nav-beginning-of-block "ext:python")
(declare-function python-nav-forward-block "ext:python")
(declare-function python-nav-backward-block "ext:python")
(declare-function python-mark-defun "ext:python")
(objed-define-object python defun
:mode python-mode
:no-skip t
:get-obj
(let ((obounds (objed-bounds-from-region-cmd #'python-mark-defun)))
(when obounds
(goto-char (car obounds))
(re-search-forward ": *\n +" nil t)
(objed-make-object :obounds obounds
:ibeg (point)
:iend (objed--skip-backward (cdr obounds) 'ws))))
:try-next
(beginning-of-defun -1)
:try-prev
(beginning-of-defun 1))
(objed-define-object python block
:mode python-mode
:commands (python-nav-backward-block python-nav-forward-block)
;; don't skip current object on navigation because
;; python blocks can contain other python blocks...
:no-skip t
:try-next
(python-nav-forward-block)
:try-prev
(python-nav-backward-block)
:beg
(python-nav-beginning-of-block)
(objed--skip-ws t (line-beginning-position))
(point)
:ibeg
(forward-line 1)
(objed--skip-ws)
(point)
:iend
(python-nav-end-of-block)
(point)
:end
(forward-line 1)
(point))
(provide 'objed-objects)
;;; objed-objects.el ends here
|