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
|
/* text.c
*
*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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 2
* 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, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
#include <string.h> /* strstr */
#include <sys/types.h>
#include <sys/stat.h>
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "DNA_action_types.h"
#include "DNA_armature_types.h"
#include "DNA_constraint_types.h"
#include "DNA_controller_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_text_types.h"
#include "BKE_depsgraph.h"
#include "BKE_global.h"
#include "BKE_library.h"
#include "BKE_main.h"
#include "BKE_node.h"
#include "BKE_text.h"
#include "BKE_utildefines.h"
#ifndef DISABLE_PYTHON
#include "BPY_extern.h"
#endif
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/***************/ /*
How Texts should work
--
A text should relate to a file as follows -
(Text *)->name should be the place where the
file will or has been saved.
(Text *)->flags has the following bits
TXT_ISDIRTY - should always be set if the file in mem. differs from
the file on disk, or if there is no file on disk.
TXT_ISMEM - should always be set if the Text has not been mapped to
a file, in which case (Text *)->name may be NULL or garbage.
TXT_ISEXT - should always be set if the Text is not to be written into
the .blend
TXT_ISSCRIPT - should be set if the user has designated the text
as a script. (NEW: this was unused, but now it is needed by
space handler script links (see header_view3d.c, for example)
->>> see also: /makesdna/DNA_text_types.h
Display
--
The st->top determines at what line the top of the text is displayed.
If the user moves the cursor the st containing that cursor should
be popped ... other st's retain their own top location.
Markers
--
The mrk->flags define the behaviour and relationships between markers. The
upper two bytes are used to hold a group ID, the lower two are normal flags. If
TMARK_EDITALL is set the group ID defines which other markers should be edited.
The mrk->clr field is used to visually group markers where the flags may not
match. A template system, for example, may allow editing of repeating tokens
(in one group) but include other marked positions (in another group) all in the
same template with the same colour.
Undo
--
Undo/Redo works by storing
events in a queue, and a pointer
to the current position in the
queue...
Events are stored using an
arbitrary op-code system
to keep track of
a) the two cursors (normal and selected)
b) input (visible and control (ie backspace))
input data is stored as its
ASCII value, the opcodes are
then selected to not conflict.
opcodes with data in between are
written at the beginning and end
of the data to allow undo and redo
to simply check the code at the current
undo position
*/ /***************/
/***/
static void txt_pop_first(Text *text);
static void txt_pop_last(Text *text);
static void txt_undo_add_op(Text *text, int op);
static void txt_undo_add_block(Text *text, int op, const char *buf);
static void txt_delete_line(Text *text, TextLine *line);
static void txt_delete_sel (Text *text);
static void txt_make_dirty (Text *text);
/***/
static unsigned char undoing;
/* allow to switch off undoing externally */
void txt_set_undostate(int u)
{
undoing = u;
}
int txt_get_undostate(void)
{
return undoing;
}
static void init_undo_text(Text *text)
{
text->undo_pos= -1;
text->undo_len= TXT_INIT_UNDO;
text->undo_buf= MEM_mallocN(text->undo_len, "undo buf");
}
void free_text(Text *text)
{
TextLine *tmp;
for (tmp= text->lines.first; tmp; tmp= tmp->next) {
MEM_freeN(tmp->line);
if (tmp->format)
MEM_freeN(tmp->format);
}
BLI_freelistN(&text->lines);
BLI_freelistN(&text->markers);
if(text->name) MEM_freeN(text->name);
MEM_freeN(text->undo_buf);
#ifndef DISABLE_PYTHON
if (text->compiled) BPY_free_compiled_text(text);
#endif
}
Text *add_empty_text(char *name)
{
Text *ta;
TextLine *tmp;
ta= alloc_libblock(&G.main->text, ID_TXT, name);
ta->id.us= 1;
ta->name= NULL;
init_undo_text(ta);
ta->nlines=1;
ta->flags= TXT_ISDIRTY | TXT_ISMEM;
ta->lines.first= ta->lines.last= NULL;
ta->markers.first= ta->markers.last= NULL;
tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line= (char*) MEM_mallocN(1, "textline_string");
tmp->format= NULL;
tmp->line[0]=0;
tmp->len= 0;
tmp->next= NULL;
tmp->prev= NULL;
BLI_addhead(&ta->lines, tmp);
ta->curl= ta->lines.first;
ta->curc= 0;
ta->sell= ta->lines.first;
ta->selc= 0;
return ta;
}
// this function removes any control characters from
// a textline
static void cleanup_textline(TextLine * tl)
{
int i;
for (i = 0; i < tl->len; i++ ) {
if (tl->line[i] < ' ' && tl->line[i] != '\t') {
memmove(tl->line + i, tl->line + i + 1, tl->len - i);
tl->len--;
i--;
}
}
}
int reopen_text(Text *text)
{
FILE *fp;
int i, llen, len, res;
unsigned char *buffer;
TextLine *tmp;
char sfile[FILE_MAXFILE];
char str[FILE_MAXDIR+FILE_MAXFILE];
struct stat st;
if (!text || !text->name) return 0;
BLI_strncpy(str, text->name, FILE_MAXDIR+FILE_MAXFILE);
BLI_convertstringcode(str, G.sce);
BLI_split_dirfile_basic(str, NULL, sfile);
fp= fopen(str, "r");
if(fp==NULL) return 0;
/* free memory: */
for (tmp= text->lines.first; tmp; tmp= tmp->next) {
MEM_freeN(tmp->line);
if (tmp->format) MEM_freeN(tmp->format);
}
BLI_freelistN(&text->lines);
text->lines.first= text->lines.last= NULL;
text->curl= text->sell= NULL;
/* clear undo buffer */
MEM_freeN(text->undo_buf);
init_undo_text(text);
fseek(fp, 0L, SEEK_END);
len= ftell(fp);
fseek(fp, 0L, SEEK_SET);
text->undo_pos= -1;
buffer= MEM_mallocN(len, "text_buffer");
// under windows fread can return less then len bytes because
// of CR stripping
len = fread(buffer, 1, len, fp);
fclose(fp);
res= stat(str, &st);
text->mtime= st.st_mtime;
text->nlines=0;
i=0;
llen=0;
for(i=0; i<len; i++) {
if (buffer[i]=='\n') {
tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line= (char*) MEM_mallocN(llen+1, "textline_string");
tmp->format= NULL;
if(llen) memcpy(tmp->line, &buffer[i-llen], llen);
tmp->line[llen]=0;
tmp->len= llen;
cleanup_textline(tmp);
BLI_addtail(&text->lines, tmp);
text->nlines++;
llen=0;
continue;
}
llen++;
}
if (llen!=0 || text->nlines==0) {
tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line= (char*) MEM_mallocN(llen+1, "textline_string");
tmp->format= NULL;
if(llen) memcpy(tmp->line, &buffer[i-llen], llen);
tmp->line[llen]=0;
tmp->len= llen;
cleanup_textline(tmp);
BLI_addtail(&text->lines, tmp);
text->nlines++;
}
text->curl= text->sell= text->lines.first;
text->curc= text->selc= 0;
MEM_freeN(buffer);
return 1;
}
Text *add_text(char *file, const char *relpath)
{
FILE *fp;
int i, llen, len, res;
unsigned char *buffer;
TextLine *tmp;
Text *ta;
char sfile[FILE_MAXFILE];
char str[FILE_MAXDIR+FILE_MAXFILE];
struct stat st;
BLI_strncpy(str, file, FILE_MAXDIR+FILE_MAXFILE);
if (relpath) /* can be NULL (bg mode) */
BLI_convertstringcode(str, relpath);
BLI_split_dirfile_basic(str, NULL, sfile);
fp= fopen(str, "r");
if(fp==NULL) return NULL;
ta= alloc_libblock(&G.main->text, ID_TXT, sfile);
ta->id.us= 1;
ta->lines.first= ta->lines.last= NULL;
ta->markers.first= ta->markers.last= NULL;
ta->curl= ta->sell= NULL;
fseek(fp, 0L, SEEK_END);
len= ftell(fp);
fseek(fp, 0L, SEEK_SET);
ta->name= MEM_mallocN(strlen(file)+1, "text_name");
strcpy(ta->name, file);
init_undo_text(ta);
buffer= MEM_mallocN(len, "text_buffer");
// under windows fread can return less then len bytes because
// of CR stripping
len = fread(buffer, 1, len, fp);
fclose(fp);
res= stat(str, &st);
ta->mtime= st.st_mtime;
ta->nlines=0;
i=0;
llen=0;
for(i=0; i<len; i++) {
if (buffer[i]=='\n') {
tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line= (char*) MEM_mallocN(llen+1, "textline_string");
tmp->format= NULL;
if(llen) memcpy(tmp->line, &buffer[i-llen], llen);
tmp->line[llen]=0;
tmp->len= llen;
cleanup_textline(tmp);
BLI_addtail(&ta->lines, tmp);
ta->nlines++;
llen=0;
continue;
}
llen++;
}
if (llen!=0 || ta->nlines==0) {
tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line= (char*) MEM_mallocN(llen+1, "textline_string");
tmp->format= NULL;
if(llen) memcpy(tmp->line, &buffer[i-llen], llen);
tmp->line[llen]=0;
tmp->len= llen;
cleanup_textline(tmp);
BLI_addtail(&ta->lines, tmp);
ta->nlines++;
}
ta->curl= ta->sell= ta->lines.first;
ta->curc= ta->selc= 0;
MEM_freeN(buffer);
return ta;
}
Text *copy_text(Text *ta)
{
Text *tan;
TextLine *line, *tmp;
tan= copy_libblock(ta);
tan->name= MEM_mallocN(strlen(ta->name)+1, "text_name");
strcpy(tan->name, ta->name);
tan->flags = ta->flags | TXT_ISDIRTY;
tan->lines.first= tan->lines.last= NULL;
tan->markers.first= tan->markers.last= NULL;
tan->curl= tan->sell= NULL;
tan->nlines= ta->nlines;
line= ta->lines.first;
/* Walk down, reconstructing */
while (line) {
tmp= (TextLine*) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line= MEM_mallocN(line->len+1, "textline_string");
tmp->format= NULL;
strcpy(tmp->line, line->line);
tmp->len= line->len;
BLI_addtail(&tan->lines, tmp);
line= line->next;
}
tan->curl= tan->sell= tan->lines.first;
tan->curc= tan->selc= 0;
init_undo_text(tan);
return tan;
}
void unlink_text(Main *bmain, Text *text)
{
bScreen *scr;
ScrArea *area;
SpaceLink *sl;
Scene *scene;
Object *ob;
bController *cont;
bConstraint *con;
short update;
/* dome */
for(scene=bmain->scene.first; scene; scene=scene->id.next)
if(scene->r.dometext == text)
scene->r.dometext = NULL;
for(ob=bmain->object.first; ob; ob=ob->id.next) {
/* game controllers */
for(cont=ob->controllers.first; cont; cont=cont->next) {
if(cont->type==CONT_PYTHON) {
bPythonCont *pc;
pc= cont->data;
if(pc->text==text) pc->text= NULL;
}
}
/* pyconstraints */
update = 0;
if(ob->type==OB_ARMATURE && ob->pose) {
bPoseChannel *pchan;
for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
for(con = pchan->constraints.first; con; con=con->next) {
if(con->type==CONSTRAINT_TYPE_PYTHON) {
bPythonConstraint *data = con->data;
if (data->text==text) data->text = NULL;
update = 1;
}
}
}
}
for(con = ob->constraints.first; con; con=con->next) {
if(con->type==CONSTRAINT_TYPE_PYTHON) {
bPythonConstraint *data = con->data;
if (data->text==text) data->text = NULL;
update = 1;
}
}
if(update)
DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
}
/* pynodes */
// XXX nodeDynamicUnlinkText(&text->id);
/* text space */
for(scr= bmain->screen.first; scr; scr= scr->id.next) {
for(area= scr->areabase.first; area; area= area->next) {
for(sl= area->spacedata.first; sl; sl= sl->next) {
if(sl->spacetype==SPACE_TEXT) {
SpaceText *st= (SpaceText*) sl;
if(st->text==text) {
st->text= NULL;
st->top= 0;
}
}
}
}
}
text->id.us= 0;
}
void clear_text(Text *text) /* called directly from rna */
{
int oldstate;
oldstate = txt_get_undostate( );
txt_set_undostate( 1 );
txt_sel_all( text );
txt_delete_sel(text);
txt_set_undostate( oldstate );
txt_make_dirty(text);
}
void write_text(Text *text, char *str) /* called directly from rna */
{
int oldstate;
oldstate = txt_get_undostate( );
txt_insert_buf( text, str );
txt_move_eof( text, 0 );
txt_set_undostate( oldstate );
txt_make_dirty(text);
}
/*****************************/
/* Editing utility functions */
/*****************************/
static void make_new_line (TextLine *line, char *newline)
{
if (line->line) MEM_freeN(line->line);
if (line->format) MEM_freeN(line->format);
line->line= newline;
line->len= strlen(newline);
line->format= NULL;
}
static TextLine *txt_new_line(char *str)
{
TextLine *tmp;
if(!str) str= "";
tmp= (TextLine *) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line= MEM_mallocN(strlen(str)+1, "textline_string");
tmp->format= NULL;
strcpy(tmp->line, str);
tmp->len= strlen(str);
tmp->next= tmp->prev= NULL;
return tmp;
}
static TextLine *txt_new_linen(const char *str, int n)
{
TextLine *tmp;
tmp= (TextLine *) MEM_mallocN(sizeof(TextLine), "textline");
tmp->line= MEM_mallocN(n+1, "textline_string");
tmp->format= NULL;
BLI_strncpy(tmp->line, (str)? str: "", n+1);
tmp->len= strlen(tmp->line);
tmp->next= tmp->prev= NULL;
return tmp;
}
void txt_clean_text (Text *text)
{
TextLine **top, **bot;
if (!text) return;
if (!text->lines.first) {
if (text->lines.last) text->lines.first= text->lines.last;
else text->lines.first= text->lines.last= txt_new_line(NULL);
}
if (!text->lines.last) text->lines.last= text->lines.first;
top= (TextLine **) &text->lines.first;
bot= (TextLine **) &text->lines.last;
while ((*top)->prev) *top= (*top)->prev;
while ((*bot)->next) *bot= (*bot)->next;
if(!text->curl) {
if(text->sell) text->curl= text->sell;
else text->curl= text->lines.first;
text->curc= 0;
}
if(!text->sell) {
text->sell= text->curl;
text->selc= 0;
}
}
int txt_get_span (TextLine *from, TextLine *to)
{
int ret=0;
TextLine *tmp= from;
if (!to || !from) return 0;
if (from==to) return 0;
/* Look forwards */
while (tmp) {
if (tmp == to) return ret;
ret++;
tmp= tmp->next;
}
/* Look backwards */
if (!tmp) {
tmp= from;
ret=0;
while(tmp) {
if (tmp == to) break;
ret--;
tmp= tmp->prev;
}
if(!tmp) ret=0;
}
return ret;
}
static void txt_make_dirty (Text *text)
{
text->flags |= TXT_ISDIRTY;
#ifndef DISABLE_PYTHON
if (text->compiled) BPY_free_compiled_text(text);
#endif
}
/* 0:whitespace, 1:punct, 2:alphanumeric */
static short txt_char_type (char ch)
{
if (ch <= ' ') return 0; /* 32 */
if (ch <= '/') return 1; /* 47 */
if (ch <= '9') return 2; /* 57 */
if (ch <= '@') return 1; /* 64 */
if (ch <= 'Z') return 2; /* 90 */
if (ch == '_') return 2; /* 95, dont delimit '_' */
if (ch <= '`') return 1; /* 96 */
if (ch <= 'z') return 2; /* 122 */
return 1;
}
/****************************/
/* Cursor utility functions */
/****************************/
static void txt_curs_cur (Text *text, TextLine ***linep, int **charp)
{
*linep= &text->curl; *charp= &text->curc;
}
static void txt_curs_sel (Text *text, TextLine ***linep, int **charp)
{
*linep= &text->sell; *charp= &text->selc;
}
static void txt_curs_first (Text *text, TextLine **linep, int *charp)
{
if (text->curl==text->sell) {
*linep= text->curl;
if (text->curc<text->selc) *charp= text->curc;
else *charp= text->selc;
} else if (txt_get_span(text->lines.first, text->curl)<txt_get_span(text->lines.first, text->sell)) {
*linep= text->curl;
*charp= text->curc;
} else {
*linep= text->sell;
*charp= text->selc;
}
}
/****************************/
/* Cursor movement functions */
/****************************/
void txt_move_up(Text *text, short sel)
{
TextLine **linep;
int *charp, old;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else { txt_pop_first(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
old= *charp;
if((*linep)->prev) {
*linep= (*linep)->prev;
if (*charp > (*linep)->len) {
*charp= (*linep)->len;
if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, (*linep)->next), old, txt_get_span(text->lines.first, *linep), (unsigned short) *charp);
} else {
if(!undoing) txt_undo_add_op(text, sel?UNDO_SUP:UNDO_CUP);
}
} else {
txt_move_bol(text, sel);
}
if(!sel) txt_pop_sel(text);
}
void txt_move_down(Text *text, short sel)
{
TextLine **linep;
int *charp, old;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else { txt_pop_last(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
old= *charp;
if((*linep)->next) {
*linep= (*linep)->next;
if (*charp > (*linep)->len) {
*charp= (*linep)->len;
if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, (*linep)->prev), old, txt_get_span(text->lines.first, *linep), (unsigned short)*charp);
} else
if(!undoing) txt_undo_add_op(text, sel?UNDO_SDOWN:UNDO_CDOWN);
} else {
txt_move_eol(text, sel);
}
if(!sel) txt_pop_sel(text);
}
void txt_move_left(Text *text, short sel)
{
TextLine **linep;
int *charp, oundoing= undoing;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else { txt_pop_first(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
undoing= 1;
if (*charp== 0) {
if ((*linep)->prev) {
txt_move_up(text, sel);
*charp= (*linep)->len;
}
} else {
(*charp)--;
}
undoing= oundoing;
if(!undoing) txt_undo_add_op(text, sel?UNDO_SLEFT:UNDO_CLEFT);
if(!sel) txt_pop_sel(text);
}
void txt_move_right(Text *text, short sel)
{
TextLine **linep;
int *charp, oundoing= undoing;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else { txt_pop_last(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
undoing= 1;
if (*charp== (*linep)->len) {
if ((*linep)->next) {
txt_move_down(text, sel);
*charp= 0;
}
} else {
(*charp)++;
}
undoing= oundoing;
if(!undoing) txt_undo_add_op(text, sel?UNDO_SRIGHT:UNDO_CRIGHT);
if(!sel) txt_pop_sel(text);
}
void txt_jump_left(Text *text, short sel)
{
TextLine **linep, *oldl;
int *charp, oldc, count, i;
unsigned char oldu;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else { txt_pop_first(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
oldl= *linep;
oldc= *charp;
oldu= undoing;
undoing= 1; /* Don't push individual moves to undo stack */
count= 0;
for (i=0; i<3; i++) {
if (count < 2) {
while (*charp>0 && txt_char_type((*linep)->line[*charp-1])==i) {
txt_move_left(text, sel);
count++;
}
}
}
if (count==0) txt_move_left(text, sel);
undoing= oldu;
if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, oldl), oldc, txt_get_span(text->lines.first, *linep), (unsigned short)*charp);
}
void txt_jump_right(Text *text, short sel)
{
TextLine **linep, *oldl;
int *charp, oldc, count, i;
unsigned char oldu;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else { txt_pop_last(text); txt_curs_cur(text, &linep, &charp); }
if (!*linep) return;
oldl= *linep;
oldc= *charp;
oldu= undoing;
undoing= 1; /* Don't push individual moves to undo stack */
count= 0;
for (i=0; i<3; i++) {
if (count < 2) {
while (*charp<(*linep)->len && txt_char_type((*linep)->line[*charp])==i) {
txt_move_right(text, sel);
count++;
}
}
}
if (count==0) txt_move_right(text, sel);
undoing= oldu;
if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, oldl), oldc, txt_get_span(text->lines.first, *linep), (unsigned short)*charp);
}
void txt_move_bol (Text *text, short sel)
{
TextLine **linep;
int *charp, old;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else txt_curs_cur(text, &linep, &charp);
if (!*linep) return;
old= *charp;
*charp= 0;
if(!sel) txt_pop_sel(text);
if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, *linep), old, txt_get_span(text->lines.first, *linep), (unsigned short)*charp);
}
void txt_move_eol (Text *text, short sel)
{
TextLine **linep;
int *charp, old;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else txt_curs_cur(text, &linep, &charp);
if (!*linep) return;
old= *charp;
*charp= (*linep)->len;
if(!sel) txt_pop_sel(text);
if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, *linep), old, txt_get_span(text->lines.first, *linep), (unsigned short)*charp);
}
void txt_move_bof (Text *text, short sel)
{
TextLine **linep;
int *charp, old;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else txt_curs_cur(text, &linep, &charp);
if (!*linep) return;
old= *charp;
*linep= text->lines.first;
*charp= 0;
if(!sel) txt_pop_sel(text);
if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, *linep), old, txt_get_span(text->lines.first, *linep), (unsigned short)*charp);
}
void txt_move_eof (Text *text, short sel)
{
TextLine **linep;
int *charp, old;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else txt_curs_cur(text, &linep, &charp);
if (!*linep) return;
old= *charp;
*linep= text->lines.last;
*charp= (*linep)->len;
if(!sel) txt_pop_sel(text);
if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, *linep), old, txt_get_span(text->lines.first, *linep), (unsigned short)*charp);
}
void txt_move_toline (Text *text, unsigned int line, short sel)
{
txt_move_to(text, line, 0, sel);
}
void txt_move_to (Text *text, unsigned int line, unsigned int ch, short sel)
{
TextLine **linep, *oldl;
int *charp, oldc;
unsigned int i;
if (!text) return;
if(sel) txt_curs_sel(text, &linep, &charp);
else txt_curs_cur(text, &linep, &charp);
if (!*linep) return;
oldc= *charp;
oldl= *linep;
*linep= text->lines.first;
for (i=0; i<line; i++) {
if ((*linep)->next) *linep= (*linep)->next;
else break;
}
if (ch>(*linep)->len)
ch= (*linep)->len;
*charp= ch;
if(!sel) txt_pop_sel(text);
if(!undoing) txt_undo_add_toop(text, sel?UNDO_STO:UNDO_CTO, txt_get_span(text->lines.first, oldl), oldc, txt_get_span(text->lines.first, *linep), (unsigned short)*charp);
}
/****************************/
/* Text selection functions */
/****************************/
static void txt_curs_swap (Text *text)
{
TextLine *tmpl;
int tmpc;
tmpl= text->curl;
text->curl= text->sell;
text->sell= tmpl;
tmpc= text->curc;
text->curc= text->selc;
text->selc= tmpc;
if(!undoing) txt_undo_add_op(text, UNDO_SWAP);
}
static void txt_pop_first (Text *text)
{
if (txt_get_span(text->curl, text->sell)<0 ||
(text->curl==text->sell && text->curc>text->selc)) {
txt_curs_swap(text);
}
if(!undoing) txt_undo_add_toop(text, UNDO_STO,
txt_get_span(text->lines.first, text->sell),
text->selc,
txt_get_span(text->lines.first, text->curl),
text->curc);
txt_pop_sel(text);
}
static void txt_pop_last (Text *text)
{
if (txt_get_span(text->curl, text->sell)>0 ||
(text->curl==text->sell && text->curc<text->selc)) {
txt_curs_swap(text);
}
if(!undoing) txt_undo_add_toop(text, UNDO_STO,
txt_get_span(text->lines.first, text->sell),
text->selc,
txt_get_span(text->lines.first, text->curl),
text->curc);
txt_pop_sel(text);
}
/* never used: CVS 1.19 */
/* static void txt_pop_selr (Text *text) */
void txt_pop_sel (Text *text)
{
text->sell= text->curl;
text->selc= text->curc;
}
void txt_order_cursors(Text *text)
{
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;
/* Flip so text->curl is before text->sell */
if (txt_get_span(text->curl, text->sell)<0 ||
(text->curl==text->sell && text->curc>text->selc))
txt_curs_swap(text);
}
int txt_has_sel(Text *text)
{
return ((text->curl!=text->sell) || (text->curc!=text->selc));
}
static void txt_delete_sel (Text *text)
{
TextLine *tmpl;
TextMarker *mrk;
char *buf;
int move, lineno;
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;
if (!txt_has_sel(text)) return;
txt_order_cursors(text);
if(!undoing) {
buf= txt_sel_to_buf(text);
txt_undo_add_block(text, UNDO_DBLOCK, buf);
MEM_freeN(buf);
}
buf= MEM_mallocN(text->curc+(text->sell->len - text->selc)+1, "textline_string");
if (text->curl != text->sell) {
txt_clear_marker_region(text, text->curl, text->curc, text->curl->len, 0, 0);
move= txt_get_span(text->curl, text->sell);
} else {
mrk= txt_find_marker_region(text, text->curl, text->curc, text->selc, 0, 0);
if (mrk && (mrk->start > text->curc || mrk->end < text->selc))
txt_clear_marker_region(text, text->curl, text->curc, text->selc, 0, 0);
move= 0;
}
mrk= txt_find_marker_region(text, text->sell, text->selc-1, text->sell->len, 0, 0);
if (mrk) {
lineno= mrk->lineno;
do {
mrk->lineno -= move;
if (mrk->start > text->curc) mrk->start -= text->selc - text->curc;
mrk->end -= text->selc - text->curc;
mrk= mrk->next;
} while (mrk && mrk->lineno==lineno);
}
strncpy(buf, text->curl->line, text->curc);
strcpy(buf+text->curc, text->sell->line + text->selc);
buf[text->curc+(text->sell->len - text->selc)]=0;
make_new_line(text->curl, buf);
tmpl= text->sell;
while (tmpl != text->curl) {
tmpl= tmpl->prev;
if (!tmpl) break;
txt_delete_line(text, tmpl->next);
}
text->sell= text->curl;
text->selc= text->curc;
}
void txt_sel_all (Text *text)
{
if (!text) return;
text->curl= text->lines.first;
text->curc= 0;
text->sell= text->lines.last;
text->selc= text->sell->len;
}
void txt_sel_line (Text *text)
{
if (!text) return;
if (!text->curl) return;
text->curc= 0;
text->sell= text->curl;
text->selc= text->sell->len;
}
/***************************/
/* Cut and paste functions */
/***************************/
char *txt_to_buf (Text *text)
{
int length;
TextLine *tmp, *linef, *linel;
int charf, charl;
char *buf;
if (!text) return NULL;
if (!text->curl) return NULL;
if (!text->sell) return NULL;
if (!text->lines.first) return NULL;
linef= text->lines.first;
charf= 0;
linel= text->lines.last;
charl= linel->len;
if (linef == text->lines.last) {
length= charl-charf;
buf= MEM_mallocN(length+2, "text buffer");
BLI_strncpy(buf, linef->line + charf, length+1);
buf[length]=0;
} else {
length= linef->len - charf;
length+= charl;
length+= 2; /* For the 2 '\n' */
tmp= linef->next;
while (tmp && tmp!= linel) {
length+= tmp->len+1;
tmp= tmp->next;
}
buf= MEM_mallocN(length+1, "cut buffer");
strncpy(buf, linef->line + charf, linef->len-charf);
length= linef->len - charf;
buf[length++]='\n';
tmp= linef->next;
while (tmp && tmp!=linel) {
strncpy(buf+length, tmp->line, tmp->len);
length+= tmp->len;
buf[length++]='\n';
tmp= tmp->next;
}
strncpy(buf+length, linel->line, charl);
length+= charl;
/* python compiler wants an empty end line */
buf[length++]='\n';
buf[length]=0;
}
return buf;
}
int txt_find_string(Text *text, char *findstr, int wrap)
{
TextLine *tl, *startl;
char *s= NULL;
int oldcl, oldsl, oldcc, oldsc;
if (!text || !text->curl || !text->sell) return 0;
txt_order_cursors(text);
oldcl= txt_get_span(text->lines.first, text->curl);
oldsl= txt_get_span(text->lines.first, text->sell);
tl= startl= text->sell;
oldcc= text->curc;
oldsc= text->selc;
s= strstr(&tl->line[text->selc], findstr);
while (!s) {
tl= tl->next;
if (!tl) {
if (wrap)
tl= text->lines.first;
else
break;
}
s= strstr(tl->line, findstr);
if (tl==startl)
break;
}
if (s) {
int newl= txt_get_span(text->lines.first, tl);
int newc= (int)(s-tl->line);
txt_move_to(text, newl, newc, 0);
txt_move_to(text, newl, newc + strlen(findstr), 1);
return 1;
} else
return 0;
}
char *txt_sel_to_buf (Text *text)
{
char *buf;
int length=0;
TextLine *tmp, *linef, *linel;
int charf, charl;
if (!text) return NULL;
if (!text->curl) return NULL;
if (!text->sell) return NULL;
if (text->curl==text->sell) {
linef= linel= text->curl;
if (text->curc < text->selc) {
charf= text->curc;
charl= text->selc;
} else{
charf= text->selc;
charl= text->curc;
}
} else if (txt_get_span(text->curl, text->sell)<0) {
linef= text->sell;
linel= text->curl;
charf= text->selc;
charl= text->curc;
} else {
linef= text->curl;
linel= text->sell;
charf= text->curc;
charl= text->selc;
}
if (linef == linel) {
length= charl-charf;
buf= MEM_mallocN(length+1, "sel buffer");
BLI_strncpy(buf, linef->line + charf, length+1);
} else {
length+= linef->len - charf;
length+= charl;
length++; /* For the '\n' */
tmp= linef->next;
while (tmp && tmp!= linel) {
length+= tmp->len+1;
tmp= tmp->next;
}
buf= MEM_mallocN(length+1, "sel buffer");
strncpy(buf, linef->line+ charf, linef->len-charf);
length= linef->len-charf;
buf[length++]='\n';
tmp= linef->next;
while (tmp && tmp!=linel) {
strncpy(buf+length, tmp->line, tmp->len);
length+= tmp->len;
buf[length++]='\n';
tmp= tmp->next;
}
strncpy(buf+length, linel->line, charl);
length+= charl;
buf[length]=0;
}
return buf;
}
void txt_insert_buf(Text *text, const char *in_buffer)
{
int i=0, l=0, j, u, len;
TextLine *add;
if (!text) return;
if (!in_buffer) return;
txt_delete_sel(text);
if(!undoing) txt_undo_add_block (text, UNDO_IBLOCK, in_buffer);
u= undoing;
undoing= 1;
/* Read the first line (or as close as possible */
while (in_buffer[i] && in_buffer[i]!='\n') {
txt_add_char(text, in_buffer[i]);
i++;
}
if (in_buffer[i]=='\n') txt_split_curline(text);
else { undoing = u; return; }
i++;
/* Read as many full lines as we can */
len= strlen(in_buffer);
while (i<len) {
l=0;
while (in_buffer[i] && in_buffer[i]!='\n') {
i++; l++;
}
if(in_buffer[i]=='\n') {
add= txt_new_linen(in_buffer +(i-l), l);
BLI_insertlinkbefore(&text->lines, text->curl, add);
i++;
} else {
for (j= i-l; j<i && j<(int)strlen(in_buffer); j++) {
txt_add_char(text, in_buffer[j]);
}
break;
}
}
undoing= u;
}
/******************/
/* Undo functions */
/******************/
static int max_undo_test(Text *text, int x)
{
while (text->undo_pos+x >= text->undo_len) {
if(text->undo_len*2 > TXT_MAX_UNDO) {
/* XXX error("Undo limit reached, buffer cleared\n"); */
MEM_freeN(text->undo_buf);
init_undo_text(text);
return 0;
} else {
void *tmp= text->undo_buf;
text->undo_buf= MEM_callocN(text->undo_len*2, "undo buf");
memcpy(text->undo_buf, tmp, text->undo_len);
text->undo_len*=2;
MEM_freeN(tmp);
}
}
return 1;
}
static void dump_buffer(Text *text)
{
int i= 0;
while (i++<text->undo_pos) printf("%d: %d %c\n", i, text->undo_buf[i], text->undo_buf[i]);
}
void txt_print_undo(Text *text)
{
int i= 0;
int op;
char *ops;
int linep, charp;
dump_buffer(text);
printf ("---< Undo Buffer >---\n");
printf ("UndoPosition is %d\n", text->undo_pos);
while (i<=text->undo_pos) {
op= text->undo_buf[i];
if (op==UNDO_CLEFT) {
ops= "Cursor left";
} else if (op==UNDO_CRIGHT) {
ops= "Cursor right";
} else if (op==UNDO_CUP) {
ops= "Cursor up";
} else if (op==UNDO_CDOWN) {
ops= "Cursor down";
} else if (op==UNDO_SLEFT) {
ops= "Selection left";
} else if (op==UNDO_SRIGHT) {
ops= "Selection right";
} else if (op==UNDO_SUP) {
ops= "Selection up";
} else if (op==UNDO_SDOWN) {
ops= "Selection down";
} else if (op==UNDO_STO) {
ops= "Selection ";
} else if (op==UNDO_CTO) {
ops= "Cursor ";
} else if (op==UNDO_INSERT) {
ops= "Insert";
} else if (op==UNDO_BS) {
ops= "Backspace";
} else if (op==UNDO_DEL) {
ops= "Delete";
} else if (op==UNDO_SWAP) {
ops= "Cursor swap";
} else if (op==UNDO_DBLOCK) {
ops= "Delete text block";
} else if (op==UNDO_IBLOCK) {
ops= "Insert text block";
} else if (op==UNDO_INDENT) {
ops= "Indent ";
} else if (op==UNDO_UNINDENT) {
ops= "Unindent ";
} else if (op==UNDO_COMMENT) {
ops= "Comment ";
} else if (op==UNDO_UNCOMMENT) {
ops= "Uncomment ";
} else {
ops= "Unknown";
}
printf ("Op (%o) at %d = %s", op, i, ops);
if (op==UNDO_INSERT || op==UNDO_BS || op==UNDO_DEL) {
i++;
printf (" - Char is %c", text->undo_buf[i]);
i++;
} else if (op==UNDO_STO || op==UNDO_CTO) {
i++;
charp= text->undo_buf[i]; i++;
charp= charp+(text->undo_buf[i]<<8); i++;
linep= text->undo_buf[i]; i++;
linep= linep+(text->undo_buf[i]<<8); i++;
linep= linep+(text->undo_buf[i]<<16); i++;
linep= linep+(text->undo_buf[i]<<24); i++;
printf ("to <%d, %d> ", linep, charp);
charp= text->undo_buf[i]; i++;
charp= charp+(text->undo_buf[i]<<8); i++;
linep= text->undo_buf[i]; i++;
linep= linep+(text->undo_buf[i]<<8); i++;
linep= linep+(text->undo_buf[i]<<16); i++;
linep= linep+(text->undo_buf[i]<<24); i++;
printf ("from <%d, %d>", linep, charp);
} else if (op==UNDO_DBLOCK || op==UNDO_IBLOCK) {
i++;
linep= text->undo_buf[i]; i++;
linep= linep+(text->undo_buf[i]<<8); i++;
linep= linep+(text->undo_buf[i]<<16); i++;
linep= linep+(text->undo_buf[i]<<24); i++;
printf (" (length %d) <", linep);
while (linep>0) {
putchar(text->undo_buf[i]);
linep--; i++;
}
linep= text->undo_buf[i]; i++;
linep= linep+(text->undo_buf[i]<<8); i++;
linep= linep+(text->undo_buf[i]<<16); i++;
linep= linep+(text->undo_buf[i]<<24); i++;
printf ("> (%d)", linep);
} else if (op==UNDO_INDENT || op==UNDO_UNINDENT) {
i++;
charp= text->undo_buf[i]; i++;
charp= charp+(text->undo_buf[i]<<8); i++;
linep= text->undo_buf[i]; i++;
linep= linep+(text->undo_buf[i]<<8); i++;
linep= linep+(text->undo_buf[i]<<16); i++;
linep= linep+(text->undo_buf[i]<<24); i++;
printf ("to <%d, %d> ", linep, charp);
charp= text->undo_buf[i]; i++;
charp= charp+(text->undo_buf[i]<<8); i++;
linep= text->undo_buf[i]; i++;
linep= linep+(text->undo_buf[i]<<8); i++;
linep= linep+(text->undo_buf[i]<<16); i++;
linep= linep+(text->undo_buf[i]<<24); i++;
printf ("from <%d, %d>", linep, charp);
}
printf (" %d\n", i);
i++;
}
}
static void txt_undo_add_op(Text *text, int op)
{
if(!max_undo_test(text, 2))
return;
text->undo_pos++;
text->undo_buf[text->undo_pos]= op;
text->undo_buf[text->undo_pos+1]= 0;
}
static void txt_undo_add_block(Text *text, int op, const char *buf)
{
int length;
length= strlen(buf);
if(!max_undo_test(text, length+11))
return;
text->undo_pos++;
text->undo_buf[text->undo_pos]= op;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (length)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (length>>8)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (length>>16)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (length>>24)&0xff;
text->undo_pos++;
strncpy(text->undo_buf+text->undo_pos, buf, length);
text->undo_pos+=length;
text->undo_buf[text->undo_pos]= (length)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (length>>8)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (length>>16)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (length>>24)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= op;
text->undo_buf[text->undo_pos+1]= 0;
}
void txt_undo_add_toop(Text *text, int op, unsigned int froml, unsigned short fromc, unsigned int tol, unsigned short toc)
{
if(!max_undo_test(text, 15))
return;
if (froml==tol && fromc==toc) return;
text->undo_pos++;
text->undo_buf[text->undo_pos]= op;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (fromc)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (fromc>>8)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (froml)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (froml>>8)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (froml>>16)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (froml>>24)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (toc)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (toc>>8)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (tol)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (tol>>8)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (tol>>16)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= (tol>>24)&0xff;
text->undo_pos++;
text->undo_buf[text->undo_pos]= op;
text->undo_buf[text->undo_pos+1]= 0;
}
static void txt_undo_add_charop(Text *text, int op, char c)
{
if(!max_undo_test(text, 4))
return;
text->undo_pos++;
text->undo_buf[text->undo_pos]= op;
text->undo_pos++;
text->undo_buf[text->undo_pos]= c;
text->undo_pos++;
text->undo_buf[text->undo_pos]= op;
text->undo_buf[text->undo_pos+1]= 0;
}
void txt_do_undo(Text *text)
{
int op= text->undo_buf[text->undo_pos];
unsigned int linep, i;
unsigned short charp;
TextLine *holdl;
int holdc, holdln;
char *buf;
if (text->undo_pos<0) {
return;
}
text->undo_pos--;
undoing= 1;
switch(op) {
case UNDO_CLEFT:
txt_move_right(text, 0);
break;
case UNDO_CRIGHT:
txt_move_left(text, 0);
break;
case UNDO_CUP:
txt_move_down(text, 0);
break;
case UNDO_CDOWN:
txt_move_up(text, 0);
break;
case UNDO_SLEFT:
txt_move_right(text, 1);
break;
case UNDO_SRIGHT:
txt_move_left(text, 1);
break;
case UNDO_SUP:
txt_move_down(text, 1);
break;
case UNDO_SDOWN:
txt_move_up(text, 1);
break;
case UNDO_CTO:
case UNDO_STO:
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
linep= text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
charp= text->undo_buf[text->undo_pos]; text->undo_pos--;
charp= (charp<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
if (op==UNDO_CTO) {
txt_move_toline(text, linep, 0);
text->curc= charp;
txt_pop_sel(text);
} else {
txt_move_toline(text, linep, 1);
text->selc= charp;
}
text->undo_pos--;
break;
case UNDO_INSERT:
txt_backspace_char(text);
text->undo_pos--;
text->undo_pos--;
break;
case UNDO_BS:
txt_add_char(text, text->undo_buf[text->undo_pos]);
text->undo_pos--;
text->undo_pos--;
break;
case UNDO_DEL:
txt_add_char(text, text->undo_buf[text->undo_pos]);
txt_move_left(text, 0);
text->undo_pos--;
text->undo_pos--;
break;
case UNDO_SWAP:
txt_curs_swap(text);
break;
case UNDO_DBLOCK:
linep= text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
buf= MEM_mallocN(linep+1, "dblock buffer");
for (i=0; i < linep; i++){
buf[(linep-1)-i]= text->undo_buf[text->undo_pos];
text->undo_pos--;
}
buf[i]= 0;
txt_curs_first(text, &holdl, &holdc);
holdln= txt_get_span(text->lines.first, holdl);
txt_insert_buf(text, buf);
MEM_freeN(buf);
text->curl= text->lines.first;
while (holdln>0) {
if(text->curl->next)
text->curl= text->curl->next;
holdln--;
}
text->curc= holdc;
linep= text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
text->undo_pos--;
break;
case UNDO_IBLOCK:
linep= text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep= (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
txt_delete_sel(text);
while (linep>0) {
txt_backspace_char(text);
text->undo_pos--;
linep--;
}
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
text->undo_pos--;
break;
case UNDO_INDENT:
case UNDO_UNINDENT:
case UNDO_COMMENT:
case UNDO_UNCOMMENT:
linep= text->undo_buf[text->undo_pos]; text->undo_pos--;
linep = (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep = (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep = (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
//linep is now the end line of the selection
charp = text->undo_buf[text->undo_pos]; text->undo_pos--;
charp = (charp<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
//charp is the last char selected or text->line->len
//set the selcetion for this now
text->selc = charp;
text->sell = text->lines.first;
for (i= 0; i < linep; i++) {
text->sell = text->sell->next;
}
linep= text->undo_buf[text->undo_pos]; text->undo_pos--;
linep = (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep = (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
linep = (linep<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
//first line to be selected
charp = text->undo_buf[text->undo_pos]; text->undo_pos--;
charp = (charp<<8)+text->undo_buf[text->undo_pos]; text->undo_pos--;
//first postion to be selected
text->curc = charp;
text->curl = text->lines.first;
for (i = 0; i < linep; i++) {
text->curl = text->curl->next;
}
if (op==UNDO_INDENT) {
unindent(text);
} else if (op== UNDO_UNINDENT) {
indent(text);
} else if (op == UNDO_COMMENT) {
uncomment(text);
} else if (op == UNDO_UNCOMMENT) {
comment(text);
}
text->undo_pos--;
break;
default:
//XXX error("Undo buffer error - resetting");
text->undo_pos= -1;
break;
}
/* next undo step may need evaluating */
if (text->undo_pos>=0) {
switch (text->undo_buf[text->undo_pos]) {
case UNDO_STO:
txt_do_undo(text);
txt_do_redo(text); /* selections need restoring */
break;
case UNDO_SWAP:
txt_do_undo(text); /* swaps should appear transparent */
break;
}
}
undoing= 0;
}
void txt_do_redo(Text *text)
{
char op;
unsigned int linep, i;
unsigned short charp;
char *buf;
text->undo_pos++;
op= text->undo_buf[text->undo_pos];
if (!op) {
text->undo_pos--;
return;
}
undoing= 1;
switch(op) {
case UNDO_CLEFT:
txt_move_left(text, 0);
break;
case UNDO_CRIGHT:
txt_move_right(text, 0);
break;
case UNDO_CUP:
txt_move_up(text, 0);
break;
case UNDO_CDOWN:
txt_move_down(text, 0);
break;
case UNDO_SLEFT:
txt_move_left(text, 1);
break;
case UNDO_SRIGHT:
txt_move_right(text, 1);
break;
case UNDO_SUP:
txt_move_up(text, 1);
break;
case UNDO_SDOWN:
txt_move_down(text, 1);
break;
case UNDO_INSERT:
text->undo_pos++;
txt_add_char(text, text->undo_buf[text->undo_pos]);
text->undo_pos++;
break;
case UNDO_BS:
text->undo_pos++;
txt_backspace_char(text);
text->undo_pos++;
break;
case UNDO_DEL:
text->undo_pos++;
txt_delete_char(text);
text->undo_pos++;
break;
case UNDO_SWAP:
txt_curs_swap(text);
txt_do_redo(text); /* swaps should appear transparent a*/
break;
case UNDO_CTO:
case UNDO_STO:
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
charp= text->undo_buf[text->undo_pos];
text->undo_pos++;
charp= charp+(text->undo_buf[text->undo_pos]<<8);
text->undo_pos++;
linep= text->undo_buf[text->undo_pos]; text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<8); text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<16); text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<24); text->undo_pos++;
if (op==UNDO_CTO) {
txt_move_toline(text, linep, 0);
text->curc= charp;
txt_pop_sel(text);
} else {
txt_move_toline(text, linep, 1);
text->selc= charp;
}
break;
case UNDO_DBLOCK:
text->undo_pos++;
linep= text->undo_buf[text->undo_pos]; text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<8); text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<16); text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<24); text->undo_pos++;
txt_delete_sel(text);
text->undo_pos+=linep;
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
text->undo_pos++;
break;
case UNDO_IBLOCK:
text->undo_pos++;
linep= text->undo_buf[text->undo_pos]; text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<8); text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<16); text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<24); text->undo_pos++;
buf= MEM_mallocN(linep+1, "iblock buffer");
memcpy (buf, &text->undo_buf[text->undo_pos], linep);
text->undo_pos+= linep;
buf[linep]= 0;
txt_insert_buf(text, buf);
MEM_freeN(buf);
linep= text->undo_buf[text->undo_pos]; text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<8); text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<16); text->undo_pos++;
linep= linep+(text->undo_buf[text->undo_pos]<<24); text->undo_pos++;
break;
case UNDO_INDENT:
case UNDO_UNINDENT:
case UNDO_COMMENT:
case UNDO_UNCOMMENT:
text->undo_pos++;
charp = text->undo_buf[text->undo_pos]; text->undo_pos++;
charp = charp+(text->undo_buf[text->undo_pos]<<8); text->undo_pos++;
//charp is the first char selected or 0
linep= text->undo_buf[text->undo_pos]; text->undo_pos++;
linep = linep+(text->undo_buf[text->undo_pos]<<8); text->undo_pos++;
linep = linep+(text->undo_buf[text->undo_pos]<<16); text->undo_pos++;
linep = linep+(text->undo_buf[text->undo_pos]<<24); text->undo_pos++;
//linep is now the first line of the selection
//set the selcetion for this now
text->curc = charp;
text->curl = text->lines.first;
for (i= 0; i < linep; i++) {
text->curl = text->curl->next;
}
charp = text->undo_buf[text->undo_pos]; text->undo_pos++;
charp = charp+(text->undo_buf[text->undo_pos]<<8); text->undo_pos++;
//last postion to be selected
linep= text->undo_buf[text->undo_pos]; text->undo_pos++;
linep = linep+(text->undo_buf[text->undo_pos]<<8); text->undo_pos++;
linep = linep+(text->undo_buf[text->undo_pos]<<16); text->undo_pos++;
linep = linep+(text->undo_buf[text->undo_pos]<<24); text->undo_pos++;
//Last line to be selected
text->selc = charp;
text->sell = text->lines.first;
for (i = 0; i < linep; i++) {
text->sell = text->sell->next;
}
if (op==UNDO_INDENT) {
indent(text);
} else if (op== UNDO_UNINDENT) {
unindent(text);
} else if (op == UNDO_COMMENT) {
comment(text);
} else if (op == UNDO_UNCOMMENT) {
uncomment(text);
}
break;
default:
//XXX error("Undo buffer error - resetting");
text->undo_pos= -1;
break;
}
undoing= 0;
}
/**************************/
/* Line editing functions */
/**************************/
void txt_split_curline (Text *text)
{
TextLine *ins;
TextMarker *mrk;
char *left, *right;
int lineno= -1;
if (!text) return;
if (!text->curl) return;
txt_delete_sel(text);
/* Move markers */
lineno= txt_get_span(text->lines.first, text->curl);
mrk= text->markers.first;
while (mrk) {
if (mrk->lineno==lineno && mrk->start>text->curc) {
mrk->lineno++;
mrk->start -= text->curc;
mrk->end -= text->curc;
} else if (mrk->lineno > lineno) {
mrk->lineno++;
}
mrk= mrk->next;
}
/* Make the two half strings */
left= MEM_mallocN(text->curc+1, "textline_string");
if (text->curc) memcpy(left, text->curl->line, text->curc);
left[text->curc]=0;
right= MEM_mallocN(text->curl->len - text->curc+1, "textline_string");
if (text->curl->len - text->curc) memcpy(right, text->curl->line+text->curc, text->curl->len-text->curc);
right[text->curl->len - text->curc]=0;
MEM_freeN(text->curl->line);
if (text->curl->format) MEM_freeN(text->curl->format);
/* Make the new TextLine */
ins= MEM_mallocN(sizeof(TextLine), "textline");
ins->line= left;
ins->format= NULL;
ins->len= text->curc;
text->curl->line= right;
text->curl->format= NULL;
text->curl->len= text->curl->len - text->curc;
BLI_insertlinkbefore(&text->lines, text->curl, ins);
text->curc=0;
txt_make_dirty(text);
txt_clean_text(text);
txt_pop_sel(text);
if(!undoing) txt_undo_add_charop(text, UNDO_INSERT, '\n');
}
static void txt_delete_line (Text *text, TextLine *line)
{
TextMarker *mrk=NULL, *nxt;
int lineno= -1;
if (!text) return;
if (!text->curl) return;
lineno= txt_get_span(text->lines.first, line);
mrk= text->markers.first;
while (mrk) {
nxt= mrk->next;
if (mrk->lineno==lineno)
BLI_freelinkN(&text->markers, mrk);
else if (mrk->lineno > lineno)
mrk->lineno--;
mrk= nxt;
}
BLI_remlink (&text->lines, line);
if (line->line) MEM_freeN(line->line);
if (line->format) MEM_freeN(line->format);
MEM_freeN(line);
txt_make_dirty(text);
txt_clean_text(text);
}
static void txt_combine_lines (Text *text, TextLine *linea, TextLine *lineb)
{
char *tmp;
TextMarker *mrk= NULL;
int lineno=-1;
if (!text) return;
if(!linea || !lineb) return;
mrk= txt_find_marker_region(text, lineb, 0, lineb->len, 0, 0);
if (mrk) {
lineno= mrk->lineno;
do {
mrk->lineno--;
mrk->start += linea->len;
mrk->end += linea->len;
mrk= mrk->next;
} while (mrk && mrk->lineno==lineno);
}
if (lineno==-1) lineno= txt_get_span(text->lines.first, lineb);
if (!mrk) mrk= text->markers.first;
tmp= MEM_mallocN(linea->len+lineb->len+1, "textline_string");
strcpy(tmp, linea->line);
strcat(tmp, lineb->line);
make_new_line(linea, tmp);
txt_delete_line(text, lineb);
txt_make_dirty(text);
txt_clean_text(text);
}
void txt_delete_char (Text *text)
{
char c='\n';
if (!text) return;
if (!text->curl) return;
if (txt_has_sel(text)) { /* deleting a selection */
txt_delete_sel(text);
txt_make_dirty(text);
return;
}
else if (text->curc== text->curl->len) { /* Appending two lines */
if (text->curl->next) {
txt_combine_lines(text, text->curl, text->curl->next);
txt_pop_sel(text);
}
} else { /* Just deleting a char */
int i= text->curc;
TextMarker *mrk= txt_find_marker_region(text, text->curl, i-1, text->curl->len, 0, 0);
if (mrk) {
int lineno= mrk->lineno;
if (mrk->end==i) {
if ((mrk->flags & TMARK_TEMP) && !(mrk->flags & TMARK_EDITALL)) {
txt_clear_markers(text, mrk->group, TMARK_TEMP);
} else {
BLI_freelinkN(&text->markers, mrk);
}
return;
}
do {
if (mrk->start>i) mrk->start--;
mrk->end--;
mrk= mrk->next;
} while (mrk && mrk->lineno==lineno);
}
c= text->curl->line[i];
while(i< text->curl->len) {
text->curl->line[i]= text->curl->line[i+1];
i++;
}
text->curl->len--;
txt_pop_sel(text);
}
txt_make_dirty(text);
txt_clean_text(text);
if(!undoing) txt_undo_add_charop(text, UNDO_DEL, c);
}
void txt_delete_word (Text *text)
{
txt_jump_right(text, 1);
txt_delete_sel(text);
}
void txt_backspace_char (Text *text)
{
char c='\n';
if (!text) return;
if (!text->curl) return;
if (txt_has_sel(text)) { /* deleting a selection */
txt_delete_sel(text);
txt_make_dirty(text);
return;
}
else if (text->curc==0) { /* Appending two lines */
if (!text->curl->prev) return;
text->curl= text->curl->prev;
text->curc= text->curl->len;
txt_combine_lines(text, text->curl, text->curl->next);
txt_pop_sel(text);
}
else { /* Just backspacing a char */
int i= text->curc-1;
TextMarker *mrk= txt_find_marker_region(text, text->curl, i, text->curl->len, 0, 0);
if (mrk) {
int lineno= mrk->lineno;
if (mrk->start==i+1) {
if ((mrk->flags & TMARK_TEMP) && !(mrk->flags & TMARK_EDITALL)) {
txt_clear_markers(text, mrk->group, TMARK_TEMP);
} else {
BLI_freelinkN(&text->markers, mrk);
}
return;
}
do {
if (mrk->start>i) mrk->start--;
mrk->end--;
mrk= mrk->next;
} while (mrk && mrk->lineno==lineno);
}
c= text->curl->line[i];
while(i< text->curl->len) {
text->curl->line[i]= text->curl->line[i+1];
i++;
}
text->curl->len--;
text->curc--;
txt_pop_sel(text);
}
txt_make_dirty(text);
txt_clean_text(text);
if(!undoing) txt_undo_add_charop(text, UNDO_BS, c);
}
void txt_backspace_word (Text *text)
{
txt_jump_left(text, 1);
txt_delete_sel(text);
}
/* Max spaces to replace a tab with, currently hardcoded to TXT_TABSIZE = 4.
* Used by txt_convert_tab_to_spaces, indent and unintent.
* Remember to change this string according to max tab size */
static char tab_to_spaces[] = " ";
static void txt_convert_tab_to_spaces (Text *text)
{
char *sb = &tab_to_spaces[text->curl->len % TXT_TABSIZE];
txt_insert_buf(text, sb);
}
int txt_add_char (Text *text, char add)
{
int len, lineno;
char *tmp;
TextMarker *mrk;
if (!text) return 0;
if (!text->curl) return 0;
if (add=='\n') {
txt_split_curline(text);
return 1;
}
/* insert spaces rather then tabs */
if (add == '\t' && text->flags & TXT_TABSTOSPACES) {
txt_convert_tab_to_spaces(text);
return 1;
}
txt_delete_sel(text);
mrk= txt_find_marker_region(text, text->curl, text->curc-1, text->curl->len, 0, 0);
if (mrk) {
lineno= mrk->lineno;
do {
if (mrk->start>text->curc) mrk->start++;
mrk->end++;
mrk= mrk->next;
} while (mrk && mrk->lineno==lineno);
}
tmp= MEM_mallocN(text->curl->len+2, "textline_string");
if(text->curc) memcpy(tmp, text->curl->line, text->curc);
tmp[text->curc]= add;
len= text->curl->len - text->curc;
if(len>0) memcpy(tmp+text->curc+1, text->curl->line+text->curc, len);
tmp[text->curl->len+1]=0;
make_new_line(text->curl, tmp);
text->curc++;
txt_pop_sel(text);
txt_make_dirty(text);
txt_clean_text(text);
if(!undoing) txt_undo_add_charop(text, UNDO_INSERT, add);
return 1;
}
void txt_delete_selected(Text *text)
{
txt_delete_sel(text);
txt_make_dirty(text);
}
int txt_replace_char (Text *text, char add)
{
char del;
if (!text) return 0;
if (!text->curl) return 0;
/* If text is selected or we're at the end of the line just use txt_add_char */
if (text->curc==text->curl->len || txt_has_sel(text) || add=='\n') {
TextMarker *mrk;
int i= txt_add_char(text, add);
mrk= txt_find_marker(text, text->curl, text->curc, 0, 0);
if (mrk && mrk->end==text->curc) mrk->end--;
return i;
}
del= text->curl->line[text->curc];
text->curl->line[text->curc]= (unsigned char) add;
text->curc++;
txt_pop_sel(text);
txt_make_dirty(text);
txt_clean_text(text);
/* Should probably create a new op for this */
if(!undoing) {
txt_undo_add_charop(text, UNDO_DEL, del);
txt_undo_add_charop(text, UNDO_INSERT, add);
}
return 1;
}
void indent(Text *text)
{
int len, num;
char *tmp;
char *add = "\t";
int indentlen = 1;
/* hardcoded: TXT_TABSIZE = 4 spaces: */
int spaceslen = TXT_TABSIZE;
/* insert spaces rather then tabs */
if (text->flags & TXT_TABSTOSPACES){
add = tab_to_spaces;
indentlen = spaceslen;
}
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;
num = 0;
while (TRUE)
{
tmp= MEM_mallocN(text->curl->len+indentlen+1, "textline_string");
text->curc = 0;
if(text->curc) memcpy(tmp, text->curl->line, text->curc); /* XXX never true, check prev line */
memcpy(tmp+text->curc, add, indentlen);
len= text->curl->len - text->curc;
if(len>0) memcpy(tmp+text->curc+indentlen, text->curl->line+text->curc, len);
tmp[text->curl->len+indentlen]= 0;
make_new_line(text->curl, tmp);
text->curc+= indentlen;
txt_make_dirty(text);
txt_clean_text(text);
if(text->curl == text->sell)
{
text->selc = text->sell->len;
break;
} else {
text->curl = text->curl->next;
num++;
}
}
text->curc = 0;
while( num > 0 )
{
text->curl = text->curl->prev;
num--;
}
if(!undoing)
{
txt_undo_add_toop(text, UNDO_INDENT, txt_get_span(text->lines.first, text->curl), text->curc, txt_get_span(text->lines.first, text->sell), text->selc);
}
}
void unindent(Text *text)
{
int num = 0;
char *remove = "\t";
int indent = 1;
/* hardcoded: TXT_TABSIZE = 4 spaces: */
int spaceslen = TXT_TABSIZE;
/* insert spaces rather then tabs */
if (text->flags & TXT_TABSTOSPACES){
remove = tab_to_spaces;
indent = spaceslen;
}
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;
while(TRUE)
{
int i = 0;
if (BLI_strncasecmp(text->curl->line, remove, indent) == 0)
{
while(i< text->curl->len) {
text->curl->line[i]= text->curl->line[i+indent];
i++;
}
text->curl->len-= indent;
}
txt_make_dirty(text);
txt_clean_text(text);
if(text->curl == text->sell)
{
text->selc = text->sell->len;
break;
} else {
text->curl = text->curl->next;
num++;
}
}
text->curc = 0;
while( num > 0 )
{
text->curl = text->curl->prev;
num--;
}
if(!undoing)
{
txt_undo_add_toop(text, UNDO_UNINDENT, txt_get_span(text->lines.first, text->curl), text->curc, txt_get_span(text->lines.first, text->sell), text->selc);
}
}
void comment(Text *text)
{
int len, num;
char *tmp;
char add = '#';
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;// Need to change this need to check if only one line is selected ot more then one
num = 0;
while (TRUE)
{
tmp= MEM_mallocN(text->curl->len+2, "textline_string");
text->curc = 0;
if(text->curc) memcpy(tmp, text->curl->line, text->curc);
tmp[text->curc]= add;
len= text->curl->len - text->curc;
if(len>0) memcpy(tmp+text->curc+1, text->curl->line+text->curc, len);
tmp[text->curl->len+1]=0;
make_new_line(text->curl, tmp);
text->curc++;
txt_make_dirty(text);
txt_clean_text(text);
if(text->curl == text->sell)
{
text->selc = text->sell->len;
break;
} else {
text->curl = text->curl->next;
num++;
}
}
text->curc = 0;
while( num > 0 )
{
text->curl = text->curl->prev;
num--;
}
if(!undoing)
{
txt_undo_add_toop(text, UNDO_COMMENT, txt_get_span(text->lines.first, text->curl), text->curc, txt_get_span(text->lines.first, text->sell), text->selc);
}
}
void uncomment(Text *text)
{
int num = 0;
char remove = '#';
if (!text) return;
if (!text->curl) return;
if (!text->sell) return;
while(TRUE)
{
int i = 0;
if (text->curl->line[i] == remove)
{
while(i< text->curl->len) {
text->curl->line[i]= text->curl->line[i+1];
i++;
}
text->curl->len--;
}
txt_make_dirty(text);
txt_clean_text(text);
if(text->curl == text->sell)
{
text->selc = text->sell->len;
break;
} else {
text->curl = text->curl->next;
num++;
}
}
text->curc = 0;
while( num > 0 )
{
text->curl = text->curl->prev;
num--;
}
if(!undoing)
{
txt_undo_add_toop(text, UNDO_UNCOMMENT, txt_get_span(text->lines.first, text->curl), text->curc, txt_get_span(text->lines.first, text->sell), text->selc);
}
}
int setcurr_tab (Text *text)
{
int i = 0;
int test = 0;
char *word = ":";
char *comm = "#";
char back_words[4][7] = {"return", "break", "pass", "yield"};
if (!text) return 0;
if (!text->curl) return 0;
while (text->curl->line[i] == '\t')
{
//we only count thos tabs that are before any text or before the curs;
if (i == text->curc)
{
return i;
} else {
i++;
}
}
if(strstr(text->curl->line, word))
{
//if we find a : then add a tab but not if it is in a comment
int a, indent = 0;
for(a=0; text->curl->line[a] != '\0'; a++)
{
if (text->curl->line[a]=='#') {
break;
} else if (text->curl->line[a]==':') {
indent = 1;
} else if (text->curl->line[a]==']') {
indent = 0;
}
}
if (indent) {
i++;
}
}
for(test=0; test < 4; test++)
{
//if there are these 4 key words then remove a tab because we are done with the block
if(strstr(text->curl->line, back_words[test]) && i > 0)
{
if(strcspn(text->curl->line, back_words[test]) < strcspn(text->curl->line, comm))
{
i--;
}
}
}
return i;
}
/*********************************/
/* Text marker utility functions */
/*********************************/
/* Creates and adds a marker to the list maintaining sorted order */
void txt_add_marker(Text *text, TextLine *line, int start, int end, char color[4], int group, int flags) {
TextMarker *tmp, *marker;
marker= MEM_mallocN(sizeof(TextMarker), "text_marker");
marker->lineno= txt_get_span(text->lines.first, line);
marker->start= MIN2(start, end);
marker->end= MAX2(start, end);
marker->group= group;
marker->flags= flags;
marker->color[0]= color[0];
marker->color[1]= color[1];
marker->color[2]= color[2];
marker->color[3]= color[3];
for (tmp=text->markers.last; tmp; tmp=tmp->prev)
if (tmp->lineno < marker->lineno || (tmp->lineno==marker->lineno && tmp->start < marker->start))
break;
if (tmp) BLI_insertlinkafter(&text->markers, tmp, marker);
else BLI_addhead(&text->markers, marker);
}
/* Returns the first matching marker on the specified line between two points.
If the group or flags fields are non-zero the returned flag must be in the
specified group and have at least the specified flags set. */
TextMarker *txt_find_marker_region(Text *text, TextLine *line, int start, int end, int group, int flags) {
TextMarker *marker, *next;
int lineno= txt_get_span(text->lines.first, line);
for (marker=text->markers.first; marker; marker=next) {
next= marker->next;
if (group && marker->group != group) continue;
else if ((marker->flags & flags) != flags) continue;
else if (marker->lineno < lineno) continue;
else if (marker->lineno > lineno) break;
if ((marker->start==marker->end && start<=marker->start && marker->start<=end) ||
(marker->start<end && marker->end>start))
return marker;
}
return NULL;
}
/* Clears all markers on the specified line between two points. If the group or
flags fields are non-zero the returned flag must be in the specified group
and have at least the specified flags set. */
short txt_clear_marker_region(Text *text, TextLine *line, int start, int end, int group, int flags) {
TextMarker *marker, *next;
int lineno= txt_get_span(text->lines.first, line);
short cleared= 0;
for (marker=text->markers.first; marker; marker=next) {
next= marker->next;
if (group && marker->group != group) continue;
else if ((marker->flags & flags) != flags) continue;
else if (marker->lineno < lineno) continue;
else if (marker->lineno > lineno) break;
if ((marker->start==marker->end && start<=marker->start && marker->start<=end) ||
(marker->start<end && marker->end>start)) {
BLI_freelinkN(&text->markers, marker);
cleared= 1;
}
}
return cleared;
}
/* Clears all markers in the specified group (if given) with at least the
specified flags set. Useful for clearing temporary markers (group=0,
flags=TMARK_TEMP) */
short txt_clear_markers(Text *text, int group, int flags) {
TextMarker *marker, *next;
short cleared= 0;
for (marker=text->markers.first; marker; marker=next) {
next= marker->next;
if ((!group || marker->group==group) &&
(marker->flags & flags) == flags) {
BLI_freelinkN(&text->markers, marker);
cleared= 1;
}
}
return cleared;
}
/* Finds the marker at the specified line and cursor position with at least the
specified flags set in the given group (if non-zero). */
TextMarker *txt_find_marker(Text *text, TextLine *line, int curs, int group, int flags) {
TextMarker *marker;
int lineno= txt_get_span(text->lines.first, line);
for (marker=text->markers.first; marker; marker=marker->next) {
if (group && marker->group != group) continue;
else if ((marker->flags & flags) != flags) continue;
else if (marker->lineno < lineno) continue;
else if (marker->lineno > lineno) break;
if (marker->start <= curs && curs <= marker->end)
return marker;
}
return NULL;
}
/* Finds the previous marker in the same group. If no other is found, the same
marker will be returned */
TextMarker *txt_prev_marker(Text *text, TextMarker *marker) {
TextMarker *tmp= marker;
while (tmp) {
if (tmp->prev) tmp= tmp->prev;
else tmp= text->markers.last;
if (tmp->group == marker->group)
return tmp;
}
return NULL; /* Only if marker==NULL */
}
/* Finds the next marker in the same group. If no other is found, the same
marker will be returned */
TextMarker *txt_next_marker(Text *text, TextMarker *marker) {
TextMarker *tmp= marker;
while (tmp) {
if (tmp->next) tmp= tmp->next;
else tmp= text->markers.first;
if (tmp->group == marker->group)
return tmp;
}
return NULL; /* Only if marker==NULL */
}
/*******************************/
/* Character utility functions */
/*******************************/
int text_check_bracket(char ch)
{
int a;
char opens[] = "([{";
char close[] = ")]}";
for(a=0; a<(sizeof(opens)-1); a++) {
if(ch==opens[a])
return a+1;
else if(ch==close[a])
return -(a+1);
}
return 0;
}
int text_check_delim(char ch)
{
int a;
char delims[] = "():\"\' ~!%^&*-+=[]{};/<>|.#\t,";
for(a=0; a<(sizeof(delims)-1); a++) {
if(ch==delims[a])
return 1;
}
return 0;
}
int text_check_digit(char ch)
{
if(ch < '0') return 0;
if(ch <= '9') return 1;
return 0;
}
int text_check_identifier(char ch)
{
if(ch < '0') return 0;
if(ch <= '9') return 1;
if(ch < 'A') return 0;
if(ch <= 'Z' || ch == '_') return 1;
if(ch < 'a') return 0;
if(ch <= 'z') return 1;
return 0;
}
int text_check_whitespace(char ch)
{
if(ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n')
return 1;
return 0;
}
|